[Ada] tech debt: Clean up Uint fields, such as Esize
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobc8d4ec1abf3368ee51314d245873c31b1ddb16a6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2021, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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 Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Errout; use Errout;
36 with Eval_Fat; use Eval_Fat;
37 with Exp_Ch3; use Exp_Ch3;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Itypes; use Itypes;
46 with Layout; use Layout;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Case; use Sem_Case;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch7; use Sem_Ch7;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Elab; use Sem_Elab;
68 with Sem_Elim; use Sem_Elim;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_Smem; use Sem_Smem;
73 with Sem_Type; use Sem_Type;
74 with Sem_Util; use Sem_Util;
75 with Sem_Warn; use Sem_Warn;
76 with Stand; use Stand;
77 with Sinfo; use Sinfo;
78 with Sinfo.Nodes; use Sinfo.Nodes;
79 with Sinfo.Utils; use Sinfo.Utils;
80 with Sinput; use Sinput;
81 with Snames; use Snames;
82 with Targparm; use Targparm;
83 with Tbuild; use Tbuild;
84 with Ttypes; use Ttypes;
85 with Uintp; use Uintp;
86 with Urealp; use Urealp;
88 package body Sem_Ch3 is
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
94 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
95 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
96 -- abstract interface types implemented by a record type or a derived
97 -- record type.
99 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
100 -- When an access-to-subprogram type has pre/postconditions, we build a
101 -- subprogram that includes these contracts and is invoked by an indirect
102 -- call through the corresponding access type.
104 procedure Build_Derived_Type
105 (N : Node_Id;
106 Parent_Type : Entity_Id;
107 Derived_Type : Entity_Id;
108 Is_Completion : Boolean;
109 Derive_Subps : Boolean := True);
110 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
111 -- the N_Full_Type_Declaration node containing the derived type definition.
112 -- Parent_Type is the entity for the parent type in the derived type
113 -- definition and Derived_Type the actual derived type. Is_Completion must
114 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
115 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
116 -- completion of a private type declaration. If Is_Completion is set to
117 -- True, N is the completion of a private type declaration and Derived_Type
118 -- is different from the defining identifier inside N (i.e. Derived_Type /=
119 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
120 -- subprograms should be derived. The only case where this parameter is
121 -- False is when Build_Derived_Type is recursively called to process an
122 -- implicit derived full type for a type derived from a private type (in
123 -- that case the subprograms must only be derived for the private view of
124 -- the type).
126 -- ??? These flags need a bit of re-examination and re-documentation:
127 -- ??? are they both necessary (both seem related to the recursion)?
129 procedure Build_Derived_Access_Type
130 (N : Node_Id;
131 Parent_Type : Entity_Id;
132 Derived_Type : Entity_Id);
133 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
134 -- create an implicit base if the parent type is constrained or if the
135 -- subtype indication has a constraint.
137 procedure Build_Derived_Array_Type
138 (N : Node_Id;
139 Parent_Type : Entity_Id;
140 Derived_Type : Entity_Id);
141 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
142 -- create an implicit base if the parent type is constrained or if the
143 -- subtype indication has a constraint.
145 procedure Build_Derived_Concurrent_Type
146 (N : Node_Id;
147 Parent_Type : Entity_Id;
148 Derived_Type : Entity_Id);
149 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
150 -- protected type, inherit entries and protected subprograms, check
151 -- legality of discriminant constraints if any.
153 procedure Build_Derived_Enumeration_Type
154 (N : Node_Id;
155 Parent_Type : Entity_Id;
156 Derived_Type : Entity_Id);
157 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
158 -- type, we must create a new list of literals. Types derived from
159 -- Character and [Wide_]Wide_Character are special-cased.
161 procedure Build_Derived_Numeric_Type
162 (N : Node_Id;
163 Parent_Type : Entity_Id;
164 Derived_Type : Entity_Id);
165 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
166 -- an anonymous base type, and propagate constraint to subtype if needed.
168 procedure Build_Derived_Private_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Is_Completion : Boolean;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
175 -- because the parent may or may not have a completion, and the derivation
176 -- may itself be a completion.
178 procedure Build_Derived_Record_Type
179 (N : Node_Id;
180 Parent_Type : Entity_Id;
181 Derived_Type : Entity_Id;
182 Derive_Subps : Boolean := True);
183 -- Subsidiary procedure used for tagged and untagged record types
184 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
185 -- All parameters are as in Build_Derived_Type except that N, in
186 -- addition to being an N_Full_Type_Declaration node, can also be an
187 -- N_Private_Extension_Declaration node. See the definition of this routine
188 -- for much more info. Derive_Subps indicates whether subprograms should be
189 -- derived from the parent type. The only case where Derive_Subps is False
190 -- is for an implicit derived full type for a type derived from a private
191 -- type (see Build_Derived_Type).
193 procedure Build_Discriminal (Discrim : Entity_Id);
194 -- Create the discriminal corresponding to discriminant Discrim, that is
195 -- the parameter corresponding to Discrim to be used in initialization
196 -- procedures for the type where Discrim is a discriminant. Discriminals
197 -- are not used during semantic analysis, and are not fully defined
198 -- entities until expansion. Thus they are not given a scope until
199 -- initialization procedures are built.
201 function Build_Discriminant_Constraints
202 (T : Entity_Id;
203 Def : Node_Id;
204 Derived_Def : Boolean := False) return Elist_Id;
205 -- Validate discriminant constraints and return the list of the constraints
206 -- in order of discriminant declarations, where T is the discriminated
207 -- unconstrained type. Def is the N_Subtype_Indication node where the
208 -- discriminants constraints for T are specified. Derived_Def is True
209 -- when building the discriminant constraints in a derived type definition
210 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
211 -- type and Def is the constraint "(xxx)" on T and this routine sets the
212 -- Corresponding_Discriminant field of the discriminants in the derived
213 -- type D to point to the corresponding discriminants in the parent type T.
215 procedure Build_Discriminated_Subtype
216 (T : Entity_Id;
217 Def_Id : Entity_Id;
218 Elist : Elist_Id;
219 Related_Nod : Node_Id;
220 For_Access : Boolean := False);
221 -- Subsidiary procedure to Constrain_Discriminated_Type and to
222 -- Process_Incomplete_Dependents. Given
224 -- T (a possibly discriminated base type)
225 -- Def_Id (a very partially built subtype for T),
227 -- the call completes Def_Id to be the appropriate E_*_Subtype.
229 -- The Elist is the list of discriminant constraints if any (it is set
230 -- to No_Elist if T is not a discriminated type, and to an empty list if
231 -- T has discriminants but there are no discriminant constraints). The
232 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
233 -- The For_Access says whether or not this subtype is really constraining
234 -- an access type.
236 function Build_Scalar_Bound
237 (Bound : Node_Id;
238 Par_T : Entity_Id;
239 Der_T : Entity_Id) return Node_Id;
240 -- The bounds of a derived scalar type are conversions of the bounds of
241 -- the parent type. Optimize the representation if the bounds are literals.
242 -- Needs a more complete spec--what are the parameters exactly, and what
243 -- exactly is the returned value, and how is Bound affected???
245 procedure Check_Access_Discriminant_Requires_Limited
246 (D : Node_Id;
247 Loc : Node_Id);
248 -- Check the restriction that the type to which an access discriminant
249 -- belongs must be a concurrent type or a descendant of a type with
250 -- the reserved word 'limited' in its declaration.
252 procedure Check_Anonymous_Access_Component
253 (Typ_Decl : Node_Id;
254 Typ : Entity_Id;
255 Prev : Entity_Id;
256 Comp_Def : Node_Id;
257 Access_Def : Node_Id);
258 -- Ada 2005 AI-382: an access component in a record definition can refer to
259 -- the enclosing record, in which case it denotes the type itself, and not
260 -- the current instance of the type. We create an anonymous access type for
261 -- the component, and flag it as an access to a component, so accessibility
262 -- checks are properly performed on it. The declaration of the access type
263 -- is placed ahead of that of the record to prevent order-of-elaboration
264 -- circularity issues in Gigi. We create an incomplete type for the record
265 -- declaration, which is the designated type of the anonymous access.
267 procedure Check_Anonymous_Access_Components
268 (Typ_Decl : Node_Id;
269 Typ : Entity_Id;
270 Prev : Entity_Id;
271 Comp_List : Node_Id);
272 -- Call Check_Anonymous_Access_Component on Comp_List
274 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
275 -- Check that, if a new discriminant is used in a constraint defining the
276 -- parent subtype of a derivation, its subtype is statically compatible
277 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
279 procedure Check_Delta_Expression (E : Node_Id);
280 -- Check that the expression represented by E is suitable for use as a
281 -- delta expression, i.e. it is of real type and is static.
283 procedure Check_Digits_Expression (E : Node_Id);
284 -- Check that the expression represented by E is suitable for use as a
285 -- digits expression, i.e. it is of integer type, positive and static.
287 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
288 -- Validate the initialization of an object declaration. T is the required
289 -- type, and Exp is the initialization expression.
291 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
292 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
294 procedure Check_Or_Process_Discriminants
295 (N : Node_Id;
296 T : Entity_Id;
297 Prev : Entity_Id := Empty);
298 -- If N is the full declaration of the completion T of an incomplete or
299 -- private type, check its discriminants (which are already known to be
300 -- conformant with those of the partial view, see Find_Type_Name),
301 -- otherwise process them. Prev is the entity of the partial declaration,
302 -- if any.
304 procedure Check_Real_Bound (Bound : Node_Id);
305 -- Check given bound for being of real type and static. If not, post an
306 -- appropriate message, and rewrite the bound with the real literal zero.
308 procedure Constant_Redeclaration
309 (Id : Entity_Id;
310 N : Node_Id;
311 T : out Entity_Id);
312 -- Various checks on legality of full declaration of deferred constant.
313 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
314 -- node. The caller has not yet set any attributes of this entity.
316 function Contain_Interface
317 (Iface : Entity_Id;
318 Ifaces : Elist_Id) return Boolean;
319 -- Ada 2005: Determine whether Iface is present in the list Ifaces
321 procedure Convert_Scalar_Bounds
322 (N : Node_Id;
323 Parent_Type : Entity_Id;
324 Derived_Type : Entity_Id;
325 Loc : Source_Ptr);
326 -- For derived scalar types, convert the bounds in the type definition to
327 -- the derived type, and complete their analysis. Given a constraint of the
328 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
329 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
330 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
331 -- subtype are conversions of those bounds to the derived_type, so that
332 -- their typing is consistent.
334 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
335 -- Copies attributes from array base type T2 to array base type T1. Copies
336 -- only attributes that apply to base types, but not subtypes.
338 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
339 -- Copies attributes from array subtype T2 to array subtype T1. Copies
340 -- attributes that apply to both subtypes and base types.
342 procedure Create_Constrained_Components
343 (Subt : Entity_Id;
344 Decl_Node : Node_Id;
345 Typ : Entity_Id;
346 Constraints : Elist_Id);
347 -- Build the list of entities for a constrained discriminated record
348 -- subtype. If a component depends on a discriminant, replace its subtype
349 -- using the discriminant values in the discriminant constraint. Subt
350 -- is the defining identifier for the subtype whose list of constrained
351 -- entities we will create. Decl_Node is the type declaration node where
352 -- we will attach all the itypes created. Typ is the base discriminated
353 -- type for the subtype Subt. Constraints is the list of discriminant
354 -- constraints for Typ.
356 function Constrain_Component_Type
357 (Comp : Entity_Id;
358 Constrained_Typ : Entity_Id;
359 Related_Node : Node_Id;
360 Typ : Entity_Id;
361 Constraints : Elist_Id) return Entity_Id;
362 -- Given a discriminated base type Typ, a list of discriminant constraints,
363 -- Constraints, for Typ and a component Comp of Typ, create and return the
364 -- type corresponding to Etype (Comp) where all discriminant references
365 -- are replaced with the corresponding constraint. If Etype (Comp) contains
366 -- no discriminant references then it is returned as-is. Constrained_Typ
367 -- is the final constrained subtype to which the constrained component
368 -- belongs. Related_Node is the node where we attach all created itypes.
370 procedure Constrain_Access
371 (Def_Id : in out Entity_Id;
372 S : Node_Id;
373 Related_Nod : Node_Id);
374 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
375 -- an anonymous type created for a subtype indication. In that case it is
376 -- created in the procedure and attached to Related_Nod.
378 procedure Constrain_Array
379 (Def_Id : in out Entity_Id;
380 SI : Node_Id;
381 Related_Nod : Node_Id;
382 Related_Id : Entity_Id;
383 Suffix : Character);
384 -- Apply a list of index constraints to an unconstrained array type. The
385 -- first parameter is the entity for the resulting subtype. A value of
386 -- Empty for Def_Id indicates that an implicit type must be created, but
387 -- creation is delayed (and must be done by this procedure) because other
388 -- subsidiary implicit types must be created first (which is why Def_Id
389 -- is an in/out parameter). The second parameter is a subtype indication
390 -- node for the constrained array to be created (e.g. something of the
391 -- form string (1 .. 10)). Related_Nod gives the place where this type
392 -- has to be inserted in the tree. The Related_Id and Suffix parameters
393 -- are used to build the associated Implicit type name.
395 procedure Constrain_Concurrent
396 (Def_Id : in out Entity_Id;
397 SI : Node_Id;
398 Related_Nod : Node_Id;
399 Related_Id : Entity_Id;
400 Suffix : Character);
401 -- Apply list of discriminant constraints to an unconstrained concurrent
402 -- type.
404 -- SI is the N_Subtype_Indication node containing the constraint and
405 -- the unconstrained type to constrain.
407 -- Def_Id is the entity for the resulting constrained subtype. A value
408 -- of Empty for Def_Id indicates that an implicit type must be created,
409 -- but creation is delayed (and must be done by this procedure) because
410 -- other subsidiary implicit types must be created first (which is why
411 -- Def_Id is an in/out parameter).
413 -- Related_Nod gives the place where this type has to be inserted
414 -- in the tree.
416 -- The last two arguments are used to create its external name if needed.
418 function Constrain_Corresponding_Record
419 (Prot_Subt : Entity_Id;
420 Corr_Rec : Entity_Id;
421 Related_Nod : Node_Id) return Entity_Id;
422 -- When constraining a protected type or task type with discriminants,
423 -- constrain the corresponding record with the same discriminant values.
425 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id);
426 -- Constrain a decimal fixed point type with a digits constraint and/or a
427 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
429 procedure Constrain_Discriminated_Type
430 (Def_Id : Entity_Id;
431 S : Node_Id;
432 Related_Nod : Node_Id;
433 For_Access : Boolean := False);
434 -- Process discriminant constraints of composite type. Verify that values
435 -- have been provided for all discriminants, that the original type is
436 -- unconstrained, and that the types of the supplied expressions match
437 -- the discriminant types. The first three parameters are like in routine
438 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
439 -- of For_Access.
441 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id);
442 -- Constrain an enumeration type with a range constraint. This is identical
443 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
445 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id);
446 -- Constrain a floating point type with either a digits constraint
447 -- and/or a range constraint, building a E_Floating_Point_Subtype.
449 procedure Constrain_Index
450 (Index : Node_Id;
451 S : Node_Id;
452 Related_Nod : Node_Id;
453 Related_Id : Entity_Id;
454 Suffix : Character;
455 Suffix_Index : Pos);
456 -- Process an index constraint S in a constrained array declaration. The
457 -- constraint can be a subtype name, or a range with or without an explicit
458 -- subtype mark. The index is the corresponding index of the unconstrained
459 -- array. The Related_Id and Suffix parameters are used to build the
460 -- associated Implicit type name.
462 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id);
463 -- Build subtype of a signed or modular integer type
465 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id);
466 -- Constrain an ordinary fixed point type with a range constraint, and
467 -- build an E_Ordinary_Fixed_Point_Subtype entity.
469 procedure Copy_And_Swap (Priv, Full : Entity_Id);
470 -- Copy the Priv entity into the entity of its full declaration then swap
471 -- the two entities in such a manner that the former private type is now
472 -- seen as a full type.
474 procedure Decimal_Fixed_Point_Type_Declaration
475 (T : Entity_Id;
476 Def : Node_Id);
477 -- Create a new decimal fixed point type, and apply the constraint to
478 -- obtain a subtype of this new type.
480 procedure Complete_Private_Subtype
481 (Priv : Entity_Id;
482 Full : Entity_Id;
483 Full_Base : Entity_Id;
484 Related_Nod : Node_Id);
485 -- Complete the implicit full view of a private subtype by setting the
486 -- appropriate semantic fields. If the full view of the parent is a record
487 -- type, build constrained components of subtype.
489 procedure Derive_Progenitor_Subprograms
490 (Parent_Type : Entity_Id;
491 Tagged_Type : Entity_Id);
492 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
493 -- operations of progenitors of Tagged_Type, and replace the subsidiary
494 -- subtypes with Tagged_Type, to build the specs of the inherited interface
495 -- primitives. The derived primitives are aliased to those of the
496 -- interface. This routine takes care also of transferring to the full view
497 -- subprograms associated with the partial view of Tagged_Type that cover
498 -- interface primitives.
500 procedure Derived_Standard_Character
501 (N : Node_Id;
502 Parent_Type : Entity_Id;
503 Derived_Type : Entity_Id);
504 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
505 -- derivations from types Standard.Character and Standard.Wide_Character.
507 procedure Derived_Type_Declaration
508 (T : Entity_Id;
509 N : Node_Id;
510 Is_Completion : Boolean);
511 -- Process a derived type declaration. Build_Derived_Type is invoked
512 -- to process the actual derived type definition. Parameters N and
513 -- Is_Completion have the same meaning as in Build_Derived_Type.
514 -- T is the N_Defining_Identifier for the entity defined in the
515 -- N_Full_Type_Declaration node N, that is T is the derived type.
517 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
518 -- Insert each literal in symbol table, as an overloadable identifier. Each
519 -- enumeration type is mapped into a sequence of integers, and each literal
520 -- is defined as a constant with integer value. If any of the literals are
521 -- character literals, the type is a character type, which means that
522 -- strings are legal aggregates for arrays of components of the type.
524 function Expand_To_Stored_Constraint
525 (Typ : Entity_Id;
526 Constraint : Elist_Id) return Elist_Id;
527 -- Given a constraint (i.e. a list of expressions) on the discriminants of
528 -- Typ, expand it into a constraint on the stored discriminants and return
529 -- the new list of expressions constraining the stored discriminants.
531 function Find_Type_Of_Object
532 (Obj_Def : Node_Id;
533 Related_Nod : Node_Id) return Entity_Id;
534 -- Get type entity for object referenced by Obj_Def, attaching the implicit
535 -- types generated to Related_Nod.
537 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
538 -- Create a new float and apply the constraint to obtain subtype of it
540 function Has_Range_Constraint (N : Node_Id) return Boolean;
541 -- Given an N_Subtype_Indication node N, return True if a range constraint
542 -- is present, either directly, or as part of a digits or delta constraint.
543 -- In addition, a digits constraint in the decimal case returns True, since
544 -- it establishes a default range if no explicit range is present.
546 function Inherit_Components
547 (N : Node_Id;
548 Parent_Base : Entity_Id;
549 Derived_Base : Entity_Id;
550 Is_Tagged : Boolean;
551 Inherit_Discr : Boolean;
552 Discs : Elist_Id) return Elist_Id;
553 -- Called from Build_Derived_Record_Type to inherit the components of
554 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
555 -- For more information on derived types and component inheritance please
556 -- consult the comment above the body of Build_Derived_Record_Type.
558 -- N is the original derived type declaration
560 -- Is_Tagged is set if we are dealing with tagged types
562 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
563 -- Parent_Base, otherwise no discriminants are inherited.
565 -- Discs gives the list of constraints that apply to Parent_Base in the
566 -- derived type declaration. If Discs is set to No_Elist, then we have
567 -- the following situation:
569 -- type Parent (D1..Dn : ..) is [tagged] record ...;
570 -- type Derived is new Parent [with ...];
572 -- which gets treated as
574 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
576 -- For untagged types the returned value is an association list. The list
577 -- starts from the association (Parent_Base => Derived_Base), and then it
578 -- contains a sequence of the associations of the form
580 -- (Old_Component => New_Component),
582 -- where Old_Component is the Entity_Id of a component in Parent_Base and
583 -- New_Component is the Entity_Id of the corresponding component in
584 -- Derived_Base. For untagged records, this association list is needed when
585 -- copying the record declaration for the derived base. In the tagged case
586 -- the value returned is irrelevant.
588 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
589 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
590 -- Determine whether subprogram Subp is a procedure subject to pragma
591 -- Extensions_Visible with value False and has at least one controlling
592 -- parameter of mode OUT.
594 function Is_Private_Primitive (Prim : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- When applied to a primitive subprogram Prim, returns True if Prim is
597 -- declared as a private operation within a package or generic package,
598 -- and returns False otherwise.
600 function Is_Valid_Constraint_Kind
601 (T_Kind : Type_Kind;
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
613 procedure Ordinary_Fixed_Point_Type_Declaration
614 (T : Entity_Id;
615 Def : Node_Id);
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
619 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
620 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
621 -- In_Default_Expr can be properly adjusted.
623 procedure Prepare_Private_Subtype_Completion
624 (Id : Entity_Id;
625 Related_Nod : Node_Id);
626 -- Id is a subtype of some private type. Creates the full declaration
627 -- associated with Id whenever possible, i.e. when the full declaration
628 -- of the base type is already known. Records each subtype into
629 -- Private_Dependents of the base type.
631 procedure Process_Incomplete_Dependents
632 (N : Node_Id;
633 Full_T : Entity_Id;
634 Inc_T : Entity_Id);
635 -- Process all entities that depend on an incomplete type. There include
636 -- subtypes, subprogram types that mention the incomplete type in their
637 -- profiles, and subprogram with access parameters that designate the
638 -- incomplete type.
640 -- Inc_T is the defining identifier of an incomplete type declaration, its
641 -- Ekind is E_Incomplete_Type.
643 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
645 -- Full_T is N's defining identifier.
647 -- Subtypes of incomplete types with discriminants are completed when the
648 -- parent type is. This is simpler than private subtypes, because they can
649 -- only appear in the same scope, and there is no need to exchange views.
650 -- Similarly, access_to_subprogram types may have a parameter or a return
651 -- type that is an incomplete type, and that must be replaced with the
652 -- full type.
654 -- If the full type is tagged, subprogram with access parameters that
655 -- designated the incomplete may be primitive operations of the full type,
656 -- and have to be processed accordingly.
658 procedure Process_Real_Range_Specification (Def : Node_Id);
659 -- Given the type definition for a real type, this procedure processes and
660 -- checks the real range specification of this type definition if one is
661 -- present. If errors are found, error messages are posted, and the
662 -- Real_Range_Specification of Def is reset to Empty.
664 procedure Record_Type_Declaration
665 (T : Entity_Id;
666 N : Node_Id;
667 Prev : Entity_Id);
668 -- Process a record type declaration (for both untagged and tagged
669 -- records). Parameters T and N are exactly like in procedure
670 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
671 -- for this routine. If this is the completion of an incomplete type
672 -- declaration, Prev is the entity of the incomplete declaration, used for
673 -- cross-referencing. Otherwise Prev = T.
675 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
676 -- This routine is used to process the actual record type definition (both
677 -- for untagged and tagged records). Def is a record type definition node.
678 -- This procedure analyzes the components in this record type definition.
679 -- Prev_T is the entity for the enclosing record type. It is provided so
680 -- that its Has_Task flag can be set if any of the component have Has_Task
681 -- set. If the declaration is the completion of an incomplete type
682 -- declaration, Prev_T is the original incomplete type, whose full view is
683 -- the record type.
685 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
686 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
687 -- first create the list of components for the derived type from that of
688 -- the parent by means of Inherit_Components and then build a copy of the
689 -- declaration tree of the parent with the help of the mapping returned by
690 -- Inherit_Components, which will for example be used to validate record
691 -- representation clauses given for the derived type. If the parent type
692 -- is private and has discriminants, the ancestor discriminants used in the
693 -- inheritance are that of the private declaration, whereas the ancestor
694 -- discriminants present in the declaration tree of the parent are that of
695 -- the full declaration; as a consequence, the remapping done during the
696 -- copy will leave the references to the ancestor discriminants unchanged
697 -- in the declaration tree and they need to be fixed up. If the derived
698 -- type has a known discriminant part, then the remapping done during the
699 -- copy will only create references to the stored discriminants and they
700 -- need to be replaced with references to the non-stored discriminants.
702 procedure Set_Fixed_Range
703 (E : Entity_Id;
704 Loc : Source_Ptr;
705 Lo : Ureal;
706 Hi : Ureal);
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
711 procedure Set_Scalar_Range_For_Subtype
712 (Def_Id : Entity_Id;
713 R : Node_Id;
714 Subt : Entity_Id);
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
730 (E : Entity_Id);
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
738 -----------------------
739 -- Access_Definition --
740 -----------------------
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
751 begin
752 if Is_Entry (Current_Scope)
753 and then Is_Task_Type (Etype (Scope (Current_Scope)))
754 then
755 Error_Msg_N ("task entries cannot have access parameters", N);
756 return Empty;
757 end if;
759 -- Ada 2005: For an object declaration the corresponding anonymous
760 -- type is declared in the current scope.
762 -- If the access definition is the return type of another access to
763 -- function, scope is the current one, because it is the one of the
764 -- current type declaration, except for the pathological case below.
766 if Nkind (Related_Nod) in
767 N_Object_Declaration | N_Access_Function_Definition
768 then
769 Anon_Scope := Current_Scope;
771 -- A pathological case: function returning access functions that
772 -- return access functions, etc. Each anonymous access type created
773 -- is in the enclosing scope of the outermost function.
775 declare
776 Par : Node_Id;
778 begin
779 Par := Related_Nod;
780 while Nkind (Par) in
781 N_Access_Function_Definition | N_Access_Definition
782 loop
783 Par := Parent (Par);
784 end loop;
786 if Nkind (Par) = N_Function_Specification then
787 Anon_Scope := Scope (Defining_Entity (Par));
788 end if;
789 end;
791 -- For the anonymous function result case, retrieve the scope of the
792 -- function specification's associated entity rather than using the
793 -- current scope. The current scope will be the function itself if the
794 -- formal part is currently being analyzed, but will be the parent scope
795 -- in the case of a parameterless function, and we always want to use
796 -- the function's parent scope. Finally, if the function is a child
797 -- unit, we must traverse the tree to retrieve the proper entity.
799 elsif Nkind (Related_Nod) = N_Function_Specification
800 and then Nkind (Parent (N)) /= N_Parameter_Specification
801 then
802 -- If the current scope is a protected type, the anonymous access
803 -- is associated with one of the protected operations, and must
804 -- be available in the scope that encloses the protected declaration.
805 -- Otherwise the type is in the scope enclosing the subprogram.
807 -- If the function has formals, the return type of a subprogram
808 -- declaration is analyzed in the scope of the subprogram (see
809 -- Process_Formals) and thus the protected type, if present, is
810 -- the scope of the current function scope.
812 if Ekind (Current_Scope) = E_Protected_Type then
813 Enclosing_Prot_Type := Current_Scope;
815 elsif Ekind (Current_Scope) = E_Function
816 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
817 then
818 Enclosing_Prot_Type := Scope (Current_Scope);
819 end if;
821 if Present (Enclosing_Prot_Type) then
822 Anon_Scope := Scope (Enclosing_Prot_Type);
824 else
825 Anon_Scope := Scope (Defining_Entity (Related_Nod));
826 end if;
828 -- For an access type definition, if the current scope is a child
829 -- unit it is the scope of the type.
831 elsif Is_Compilation_Unit (Current_Scope) then
832 Anon_Scope := Current_Scope;
834 -- For access formals, access components, and access discriminants, the
835 -- scope is that of the enclosing declaration,
837 else
838 Anon_Scope := Scope (Current_Scope);
839 end if;
841 Anon_Type :=
842 Create_Itype
843 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
845 if All_Present (N)
846 and then Ada_Version >= Ada_2005
847 then
848 Error_Msg_N ("ALL not permitted for anonymous access types", N);
849 end if;
851 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
852 -- the corresponding semantic routine
854 if Present (Access_To_Subprogram_Definition (N)) then
855 Access_Subprogram_Declaration
856 (T_Name => Anon_Type,
857 T_Def => Access_To_Subprogram_Definition (N));
859 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
860 Mutate_Ekind
861 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
862 else
863 Mutate_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
864 end if;
866 Set_Can_Use_Internal_Rep
867 (Anon_Type, not Always_Compatible_Rep_On_Target);
869 -- If the anonymous access is associated with a protected operation,
870 -- create a reference to it after the enclosing protected definition
871 -- because the itype will be used in the subsequent bodies.
873 -- If the anonymous access itself is protected, a full type
874 -- declaratiton will be created for it, so that the equivalent
875 -- record type can be constructed. For further details, see
876 -- Replace_Anonymous_Access_To_Protected-Subprogram.
878 if Ekind (Current_Scope) = E_Protected_Type
879 and then not Protected_Present (Access_To_Subprogram_Definition (N))
880 then
881 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
882 end if;
884 return Anon_Type;
885 end if;
887 Find_Type (Subtype_Mark (N));
888 Desig_Type := Entity (Subtype_Mark (N));
890 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
891 Set_Etype (Anon_Type, Anon_Type);
893 -- Make sure the anonymous access type has size and alignment fields
894 -- set, as required by gigi. This is necessary in the case of the
895 -- Task_Body_Procedure.
897 if not Has_Private_Component (Desig_Type) then
898 Layout_Type (Anon_Type);
899 end if;
901 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
902 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
903 -- the null value is allowed. In Ada 95 the null value is never allowed.
905 if Ada_Version >= Ada_2005 then
906 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
907 else
908 Set_Can_Never_Be_Null (Anon_Type, True);
909 end if;
911 -- The anonymous access type is as public as the discriminated type or
912 -- subprogram that defines it. It is imported (for back-end purposes)
913 -- if the designated type is.
915 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
917 -- Ada 2005 (AI-231): Propagate the access-constant attribute
919 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
921 -- The context is either a subprogram declaration, object declaration,
922 -- or an access discriminant, in a private or a full type declaration.
923 -- In the case of a subprogram, if the designated type is incomplete,
924 -- the operation will be a primitive operation of the full type, to be
925 -- updated subsequently. If the type is imported through a limited_with
926 -- clause, the subprogram is not a primitive operation of the type
927 -- (which is declared elsewhere in some other scope).
929 if Ekind (Desig_Type) = E_Incomplete_Type
930 and then not From_Limited_With (Desig_Type)
931 and then Is_Overloadable (Current_Scope)
932 then
933 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
934 Set_Has_Delayed_Freeze (Current_Scope);
935 end if;
937 -- If the designated type is limited and class-wide, the object might
938 -- contain tasks, so we create a Master entity for the declaration. This
939 -- must be done before expansion of the full declaration, because the
940 -- declaration may include an expression that is an allocator, whose
941 -- expansion needs the proper Master for the created tasks.
943 if Expander_Active
944 and then Nkind (Related_Nod) = N_Object_Declaration
945 then
946 if Is_Limited_Record (Desig_Type)
947 and then Is_Class_Wide_Type (Desig_Type)
948 then
949 Build_Class_Wide_Master (Anon_Type);
951 -- Similarly, if the type is an anonymous access that designates
952 -- tasks, create a master entity for it in the current context.
954 elsif Has_Task (Desig_Type)
955 and then Comes_From_Source (Related_Nod)
956 then
957 Build_Master_Entity (Defining_Identifier (Related_Nod));
958 Build_Master_Renaming (Anon_Type);
959 end if;
960 end if;
962 -- For a private component of a protected type, it is imperative that
963 -- the back-end elaborate the type immediately after the protected
964 -- declaration, because this type will be used in the declarations
965 -- created for the component within each protected body, so we must
966 -- create an itype reference for it now.
968 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
969 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
971 -- Similarly, if the access definition is the return result of a
972 -- function, create an itype reference for it because it will be used
973 -- within the function body. For a regular function that is not a
974 -- compilation unit, insert reference after the declaration. For a
975 -- protected operation, insert it after the enclosing protected type
976 -- declaration. In either case, do not create a reference for a type
977 -- obtained through a limited_with clause, because this would introduce
978 -- semantic dependencies.
980 -- Similarly, do not create a reference if the designated type is a
981 -- generic formal, because no use of it will reach the backend.
983 elsif Nkind (Related_Nod) = N_Function_Specification
984 and then not From_Limited_With (Desig_Type)
985 and then not Is_Generic_Type (Desig_Type)
986 then
987 if Present (Enclosing_Prot_Type) then
988 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
990 elsif Is_List_Member (Parent (Related_Nod))
991 and then Nkind (Parent (N)) /= N_Parameter_Specification
992 then
993 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
994 end if;
996 -- Finally, create an itype reference for an object declaration of an
997 -- anonymous access type. This is strictly necessary only for deferred
998 -- constants, but in any case will avoid out-of-scope problems in the
999 -- back-end.
1001 elsif Nkind (Related_Nod) = N_Object_Declaration then
1002 Build_Itype_Reference (Anon_Type, Related_Nod);
1003 end if;
1005 return Anon_Type;
1006 end Access_Definition;
1008 -----------------------------------
1009 -- Access_Subprogram_Declaration --
1010 -----------------------------------
1012 procedure Access_Subprogram_Declaration
1013 (T_Name : Entity_Id;
1014 T_Def : Node_Id)
1016 procedure Check_For_Premature_Usage (Def : Node_Id);
1017 -- Check that type T_Name is not used, directly or recursively, as a
1018 -- parameter or a return type in Def. Def is either a subtype, an
1019 -- access_definition, or an access_to_subprogram_definition.
1021 -------------------------------
1022 -- Check_For_Premature_Usage --
1023 -------------------------------
1025 procedure Check_For_Premature_Usage (Def : Node_Id) is
1026 Param : Node_Id;
1028 begin
1029 -- Check for a subtype mark
1031 if Nkind (Def) in N_Has_Etype then
1032 if Etype (Def) = T_Name then
1033 Error_Msg_N
1034 ("type& cannot be used before the end of its declaration",
1035 Def);
1036 end if;
1038 -- If this is not a subtype, then this is an access_definition
1040 elsif Nkind (Def) = N_Access_Definition then
1041 if Present (Access_To_Subprogram_Definition (Def)) then
1042 Check_For_Premature_Usage
1043 (Access_To_Subprogram_Definition (Def));
1044 else
1045 Check_For_Premature_Usage (Subtype_Mark (Def));
1046 end if;
1048 -- The only cases left are N_Access_Function_Definition and
1049 -- N_Access_Procedure_Definition.
1051 else
1052 if Present (Parameter_Specifications (Def)) then
1053 Param := First (Parameter_Specifications (Def));
1054 while Present (Param) loop
1055 Check_For_Premature_Usage (Parameter_Type (Param));
1056 Next (Param);
1057 end loop;
1058 end if;
1060 if Nkind (Def) = N_Access_Function_Definition then
1061 Check_For_Premature_Usage (Result_Definition (Def));
1062 end if;
1063 end if;
1064 end Check_For_Premature_Usage;
1066 -- Local variables
1068 Formals : constant List_Id := Parameter_Specifications (T_Def);
1069 Formal : Entity_Id;
1070 D_Ityp : Node_Id;
1071 Desig_Type : constant Entity_Id :=
1072 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1074 -- Start of processing for Access_Subprogram_Declaration
1076 begin
1077 -- Associate the Itype node with the inner full-type declaration or
1078 -- subprogram spec or entry body. This is required to handle nested
1079 -- anonymous declarations. For example:
1081 -- procedure P
1082 -- (X : access procedure
1083 -- (Y : access procedure
1084 -- (Z : access T)))
1086 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1087 while Nkind (D_Ityp) not in N_Full_Type_Declaration
1088 | N_Private_Type_Declaration
1089 | N_Private_Extension_Declaration
1090 | N_Procedure_Specification
1091 | N_Function_Specification
1092 | N_Entry_Body
1093 | N_Object_Declaration
1094 | N_Object_Renaming_Declaration
1095 | N_Formal_Object_Declaration
1096 | N_Formal_Type_Declaration
1097 | N_Task_Type_Declaration
1098 | N_Protected_Type_Declaration
1099 loop
1100 D_Ityp := Parent (D_Ityp);
1101 pragma Assert (D_Ityp /= Empty);
1102 end loop;
1104 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1106 if Nkind (D_Ityp) in N_Procedure_Specification | N_Function_Specification
1107 then
1108 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1110 elsif Nkind (D_Ityp) in N_Full_Type_Declaration
1111 | N_Object_Declaration
1112 | N_Object_Renaming_Declaration
1113 | N_Formal_Type_Declaration
1114 then
1115 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1116 end if;
1118 if Nkind (T_Def) = N_Access_Function_Definition then
1119 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1120 declare
1121 Acc : constant Node_Id := Result_Definition (T_Def);
1123 begin
1124 if Present (Access_To_Subprogram_Definition (Acc))
1125 and then
1126 Protected_Present (Access_To_Subprogram_Definition (Acc))
1127 then
1128 Set_Etype
1129 (Desig_Type,
1130 Replace_Anonymous_Access_To_Protected_Subprogram
1131 (T_Def));
1133 else
1134 Set_Etype
1135 (Desig_Type,
1136 Access_Definition (T_Def, Result_Definition (T_Def)));
1137 end if;
1138 end;
1140 else
1141 Analyze (Result_Definition (T_Def));
1143 declare
1144 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1146 begin
1147 -- If a null exclusion is imposed on the result type, then
1148 -- create a null-excluding itype (an access subtype) and use
1149 -- it as the function's Etype.
1151 if Is_Access_Type (Typ)
1152 and then Null_Exclusion_In_Return_Present (T_Def)
1153 then
1154 Set_Etype (Desig_Type,
1155 Create_Null_Excluding_Itype
1156 (T => Typ,
1157 Related_Nod => T_Def,
1158 Scope_Id => Current_Scope));
1160 else
1161 if From_Limited_With (Typ) then
1163 -- AI05-151: Incomplete types are allowed in all basic
1164 -- declarations, including access to subprograms.
1166 if Ada_Version >= Ada_2012 then
1167 null;
1169 else
1170 Error_Msg_NE
1171 ("illegal use of incomplete type&",
1172 Result_Definition (T_Def), Typ);
1173 end if;
1175 elsif Ekind (Current_Scope) = E_Package
1176 and then In_Private_Part (Current_Scope)
1177 then
1178 if Ekind (Typ) = E_Incomplete_Type then
1179 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1181 elsif Is_Class_Wide_Type (Typ)
1182 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1183 then
1184 Append_Elmt
1185 (Desig_Type, Private_Dependents (Etype (Typ)));
1186 end if;
1187 end if;
1189 Set_Etype (Desig_Type, Typ);
1190 end if;
1191 end;
1192 end if;
1194 if not Is_Type (Etype (Desig_Type)) then
1195 Error_Msg_N
1196 ("expect type in function specification",
1197 Result_Definition (T_Def));
1198 end if;
1200 else
1201 Set_Etype (Desig_Type, Standard_Void_Type);
1202 end if;
1204 if Present (Formals) then
1205 Push_Scope (Desig_Type);
1207 -- Some special tests here. These special tests can be removed
1208 -- if and when Itypes always have proper parent pointers to their
1209 -- declarations???
1211 -- Special test 1) Link defining_identifier of formals. Required by
1212 -- First_Formal to provide its functionality.
1214 declare
1215 F : Node_Id;
1217 begin
1218 F := First (Formals);
1220 while Present (F) loop
1221 if No (Parent (Defining_Identifier (F))) then
1222 Set_Parent (Defining_Identifier (F), F);
1223 end if;
1225 Next (F);
1226 end loop;
1227 end;
1229 Process_Formals (Formals, Parent (T_Def));
1231 -- Special test 2) End_Scope requires that the parent pointer be set
1232 -- to something reasonable, but Itypes don't have parent pointers. So
1233 -- we set it and then unset it ???
1235 Set_Parent (Desig_Type, T_Name);
1236 End_Scope;
1237 Set_Parent (Desig_Type, Empty);
1238 end if;
1240 -- Check for premature usage of the type being defined
1242 Check_For_Premature_Usage (T_Def);
1244 -- The return type and/or any parameter type may be incomplete. Mark the
1245 -- subprogram_type as depending on the incomplete type, so that it can
1246 -- be updated when the full type declaration is seen. This only applies
1247 -- to incomplete types declared in some enclosing scope, not to limited
1248 -- views from other packages.
1250 -- Prior to Ada 2012, access to functions can only have in_parameters.
1252 if Present (Formals) then
1253 Formal := First_Formal (Desig_Type);
1254 while Present (Formal) loop
1255 if Ekind (Formal) /= E_In_Parameter
1256 and then Nkind (T_Def) = N_Access_Function_Definition
1257 and then Ada_Version < Ada_2012
1258 then
1259 Error_Msg_N ("functions can only have IN parameters", Formal);
1260 end if;
1262 if Ekind (Etype (Formal)) = E_Incomplete_Type
1263 and then In_Open_Scopes (Scope (Etype (Formal)))
1264 then
1265 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1266 Set_Has_Delayed_Freeze (Desig_Type);
1267 end if;
1269 Next_Formal (Formal);
1270 end loop;
1271 end if;
1273 -- Check whether an indirect call without actuals may be possible. This
1274 -- is used when resolving calls whose result is then indexed.
1276 May_Need_Actuals (Desig_Type);
1278 -- If the return type is incomplete, this is legal as long as the type
1279 -- is declared in the current scope and will be completed in it (rather
1280 -- than being part of limited view).
1282 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1283 and then not Has_Delayed_Freeze (Desig_Type)
1284 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1285 then
1286 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1287 Set_Has_Delayed_Freeze (Desig_Type);
1288 end if;
1290 Check_Delayed_Subprogram (Desig_Type);
1292 if Protected_Present (T_Def) then
1293 Mutate_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1294 Set_Convention (Desig_Type, Convention_Protected);
1295 else
1296 Mutate_Ekind (T_Name, E_Access_Subprogram_Type);
1297 end if;
1299 Set_Can_Use_Internal_Rep (T_Name,
1300 not Always_Compatible_Rep_On_Target);
1301 Set_Etype (T_Name, T_Name);
1302 Reinit_Size_Align (T_Name);
1303 Set_Directly_Designated_Type (T_Name, Desig_Type);
1305 -- If the access_to_subprogram is not declared at the library level,
1306 -- it can only point to subprograms that are at the same or deeper
1307 -- accessibility level. The corresponding subprogram type might
1308 -- require an activation record when compiling for C.
1310 Set_Needs_Activation_Record (Desig_Type,
1311 not Is_Library_Level_Entity (T_Name));
1313 Generate_Reference_To_Formals (T_Name);
1315 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1317 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1319 Check_Restriction (No_Access_Subprograms, T_Def);
1321 Create_Extra_Formals (Desig_Type);
1322 end Access_Subprogram_Declaration;
1324 ----------------------------
1325 -- Access_Type_Declaration --
1326 ----------------------------
1328 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1330 procedure Setup_Access_Type (Desig_Typ : Entity_Id);
1331 -- After type declaration is analysed with T being an incomplete type,
1332 -- this routine will mutate the kind of T to the appropriate access type
1333 -- and set its directly designated type to Desig_Typ.
1335 -----------------------
1336 -- Setup_Access_Type --
1337 -----------------------
1339 procedure Setup_Access_Type (Desig_Typ : Entity_Id) is
1340 begin
1341 if All_Present (Def) or else Constant_Present (Def) then
1342 Mutate_Ekind (T, E_General_Access_Type);
1343 else
1344 Mutate_Ekind (T, E_Access_Type);
1345 end if;
1347 Set_Directly_Designated_Type (T, Desig_Typ);
1348 end Setup_Access_Type;
1350 -- Local variables
1352 P : constant Node_Id := Parent (Def);
1353 S : constant Node_Id := Subtype_Indication (Def);
1355 Full_Desig : Entity_Id;
1357 -- Start of processing for Access_Type_Declaration
1359 begin
1360 -- Check for permissible use of incomplete type
1362 if Nkind (S) /= N_Subtype_Indication then
1364 Analyze (S);
1366 if Nkind (S) in N_Has_Entity
1367 and then Present (Entity (S))
1368 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1369 then
1370 Setup_Access_Type (Desig_Typ => Entity (S));
1372 -- If the designated type is a limited view, we cannot tell if
1373 -- the full view contains tasks, and there is no way to handle
1374 -- that full view in a client. We create a master entity for the
1375 -- scope, which will be used when a client determines that one
1376 -- is needed.
1378 if From_Limited_With (Entity (S))
1379 and then not Is_Class_Wide_Type (Entity (S))
1380 then
1381 Build_Master_Entity (T);
1382 Build_Master_Renaming (T);
1383 end if;
1385 else
1386 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1387 end if;
1389 -- If the access definition is of the form: ACCESS NOT NULL ..
1390 -- the subtype indication must be of an access type. Create
1391 -- a null-excluding subtype of it.
1393 if Null_Excluding_Subtype (Def) then
1394 if not Is_Access_Type (Entity (S)) then
1395 Error_Msg_N ("null exclusion must apply to access type", Def);
1397 else
1398 declare
1399 Loc : constant Source_Ptr := Sloc (S);
1400 Decl : Node_Id;
1401 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1403 begin
1404 Decl :=
1405 Make_Subtype_Declaration (Loc,
1406 Defining_Identifier => Nam,
1407 Subtype_Indication =>
1408 New_Occurrence_Of (Entity (S), Loc));
1409 Set_Null_Exclusion_Present (Decl);
1410 Insert_Before (Parent (Def), Decl);
1411 Analyze (Decl);
1412 Set_Entity (S, Nam);
1413 end;
1414 end if;
1415 end if;
1417 else
1418 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1419 end if;
1421 if not Error_Posted (T) then
1422 Full_Desig := Designated_Type (T);
1424 if Base_Type (Full_Desig) = T then
1425 Error_Msg_N ("access type cannot designate itself", S);
1427 -- In Ada 2005, the type may have a limited view through some unit in
1428 -- its own context, allowing the following circularity that cannot be
1429 -- detected earlier.
1431 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1432 then
1433 Error_Msg_N
1434 ("access type cannot designate its own class-wide type", S);
1436 -- Clean up indication of tagged status to prevent cascaded errors
1438 Set_Is_Tagged_Type (T, False);
1439 end if;
1441 Set_Etype (T, T);
1443 -- For SPARK, check that the designated type is compatible with
1444 -- respect to volatility with the access type.
1446 if SPARK_Mode /= Off
1447 and then Comes_From_Source (T)
1448 then
1449 -- ??? UNIMPLEMENTED
1450 -- In the case where the designated type is incomplete at this
1451 -- point, performing this check here is harmless but the check
1452 -- will need to be repeated when the designated type is complete.
1454 -- The preceding call to Comes_From_Source is needed because the
1455 -- FE sometimes introduces implicitly declared access types. See,
1456 -- for example, the expansion of nested_po.ads in OA28-015.
1458 Check_Volatility_Compatibility
1459 (Full_Desig, T, "designated type", "access type",
1460 Srcpos_Bearer => T);
1461 end if;
1462 end if;
1464 -- If the type has appeared already in a with_type clause, it is frozen
1465 -- and the pointer size is already set. Else, initialize.
1467 if not From_Limited_With (T) then
1468 Reinit_Size_Align (T);
1469 end if;
1471 -- Note that Has_Task is always false, since the access type itself
1472 -- is not a task type. See Einfo for more description on this point.
1473 -- Exactly the same consideration applies to Has_Controlled_Component
1474 -- and to Has_Protected.
1476 Set_Has_Task (T, False);
1477 Set_Has_Protected (T, False);
1478 Set_Has_Timing_Event (T, False);
1479 Set_Has_Controlled_Component (T, False);
1481 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1482 -- problems where an incomplete view of this entity has been previously
1483 -- established by a limited with and an overlaid version of this field
1484 -- (Stored_Constraint) was initialized for the incomplete view.
1486 -- This reset is performed in most cases except where the access type
1487 -- has been created for the purposes of allocating or deallocating a
1488 -- build-in-place object. Such access types have explicitly set pools
1489 -- and finalization masters.
1491 if No (Associated_Storage_Pool (T)) then
1492 Set_Finalization_Master (T, Empty);
1493 end if;
1495 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1496 -- attributes
1498 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1499 Set_Is_Access_Constant (T, Constant_Present (Def));
1500 end Access_Type_Declaration;
1502 ----------------------------------
1503 -- Add_Interface_Tag_Components --
1504 ----------------------------------
1506 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1507 Loc : constant Source_Ptr := Sloc (N);
1508 L : List_Id;
1509 Last_Tag : Node_Id;
1511 procedure Add_Tag (Iface : Entity_Id);
1512 -- Add tag for one of the progenitor interfaces
1514 -------------
1515 -- Add_Tag --
1516 -------------
1518 procedure Add_Tag (Iface : Entity_Id) is
1519 Decl : Node_Id;
1520 Def : Node_Id;
1521 Tag : Entity_Id;
1522 Offset : Entity_Id;
1524 begin
1525 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1527 -- This is a reasonable place to propagate predicates
1529 if Has_Predicates (Iface) then
1530 Set_Has_Predicates (Typ);
1531 end if;
1533 Def :=
1534 Make_Component_Definition (Loc,
1535 Aliased_Present => True,
1536 Subtype_Indication =>
1537 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1539 Tag := Make_Temporary (Loc, 'V');
1541 Decl :=
1542 Make_Component_Declaration (Loc,
1543 Defining_Identifier => Tag,
1544 Component_Definition => Def);
1546 Analyze_Component_Declaration (Decl);
1548 Set_Analyzed (Decl);
1549 Mutate_Ekind (Tag, E_Component);
1550 Set_Is_Tag (Tag);
1551 Set_Is_Aliased (Tag);
1552 Set_Is_Independent (Tag);
1553 Set_Related_Type (Tag, Iface);
1554 Reinit_Component_Location (Tag);
1556 pragma Assert (Is_Frozen (Iface));
1558 Set_DT_Entry_Count (Tag,
1559 DT_Entry_Count (First_Entity (Iface)));
1561 if No (Last_Tag) then
1562 Prepend (Decl, L);
1563 else
1564 Insert_After (Last_Tag, Decl);
1565 end if;
1567 Last_Tag := Decl;
1569 -- If the ancestor has discriminants we need to give special support
1570 -- to store the offset_to_top value of the secondary dispatch tables.
1571 -- For this purpose we add a supplementary component just after the
1572 -- field that contains the tag associated with each secondary DT.
1574 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1575 Def :=
1576 Make_Component_Definition (Loc,
1577 Subtype_Indication =>
1578 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1580 Offset := Make_Temporary (Loc, 'V');
1582 Decl :=
1583 Make_Component_Declaration (Loc,
1584 Defining_Identifier => Offset,
1585 Component_Definition => Def);
1587 Analyze_Component_Declaration (Decl);
1589 Set_Analyzed (Decl);
1590 Mutate_Ekind (Offset, E_Component);
1591 Set_Is_Aliased (Offset);
1592 Set_Is_Independent (Offset);
1593 Set_Related_Type (Offset, Iface);
1594 Reinit_Component_Location (Offset);
1595 Insert_After (Last_Tag, Decl);
1596 Last_Tag := Decl;
1597 end if;
1598 end Add_Tag;
1600 -- Local variables
1602 Elmt : Elmt_Id;
1603 Ext : Node_Id;
1604 Comp : Node_Id;
1606 -- Start of processing for Add_Interface_Tag_Components
1608 begin
1609 if not RTE_Available (RE_Interface_Tag) then
1610 Error_Msg_N
1611 ("(Ada 2005) interface types not supported by this run-time!", N);
1612 return;
1613 end if;
1615 if Ekind (Typ) /= E_Record_Type
1616 or else (Is_Concurrent_Record_Type (Typ)
1617 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1618 or else (not Is_Concurrent_Record_Type (Typ)
1619 and then No (Interfaces (Typ))
1620 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1621 then
1622 return;
1623 end if;
1625 -- Find the current last tag
1627 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1628 Ext := Record_Extension_Part (Type_Definition (N));
1629 else
1630 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1631 Ext := Type_Definition (N);
1632 end if;
1634 Last_Tag := Empty;
1636 if not (Present (Component_List (Ext))) then
1637 Set_Null_Present (Ext, False);
1638 L := New_List;
1639 Set_Component_List (Ext,
1640 Make_Component_List (Loc,
1641 Component_Items => L,
1642 Null_Present => False));
1643 else
1644 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1645 L := Component_Items
1646 (Component_List
1647 (Record_Extension_Part
1648 (Type_Definition (N))));
1649 else
1650 L := Component_Items
1651 (Component_List
1652 (Type_Definition (N)));
1653 end if;
1655 -- Find the last tag component
1657 Comp := First (L);
1658 while Present (Comp) loop
1659 if Nkind (Comp) = N_Component_Declaration
1660 and then Is_Tag (Defining_Identifier (Comp))
1661 then
1662 Last_Tag := Comp;
1663 end if;
1665 Next (Comp);
1666 end loop;
1667 end if;
1669 -- At this point L references the list of components and Last_Tag
1670 -- references the current last tag (if any). Now we add the tag
1671 -- corresponding with all the interfaces that are not implemented
1672 -- by the parent.
1674 if Present (Interfaces (Typ)) then
1675 Elmt := First_Elmt (Interfaces (Typ));
1676 while Present (Elmt) loop
1677 Add_Tag (Node (Elmt));
1678 Next_Elmt (Elmt);
1679 end loop;
1680 end if;
1681 end Add_Interface_Tag_Components;
1683 -------------------------------------
1684 -- Add_Internal_Interface_Entities --
1685 -------------------------------------
1687 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1688 Elmt : Elmt_Id;
1689 Iface : Entity_Id;
1690 Iface_Elmt : Elmt_Id;
1691 Iface_Prim : Entity_Id;
1692 Ifaces_List : Elist_Id;
1693 New_Subp : Entity_Id := Empty;
1694 Prim : Entity_Id;
1695 Restore_Scope : Boolean := False;
1697 begin
1698 pragma Assert (Ada_Version >= Ada_2005
1699 and then Is_Record_Type (Tagged_Type)
1700 and then Is_Tagged_Type (Tagged_Type)
1701 and then Has_Interfaces (Tagged_Type)
1702 and then not Is_Interface (Tagged_Type));
1704 -- Ensure that the internal entities are added to the scope of the type
1706 if Scope (Tagged_Type) /= Current_Scope then
1707 Push_Scope (Scope (Tagged_Type));
1708 Restore_Scope := True;
1709 end if;
1711 Collect_Interfaces (Tagged_Type, Ifaces_List);
1713 Iface_Elmt := First_Elmt (Ifaces_List);
1714 while Present (Iface_Elmt) loop
1715 Iface := Node (Iface_Elmt);
1717 -- Originally we excluded here from this processing interfaces that
1718 -- are parents of Tagged_Type because their primitives are located
1719 -- in the primary dispatch table (and hence no auxiliary internal
1720 -- entities are required to handle secondary dispatch tables in such
1721 -- case). However, these auxiliary entities are also required to
1722 -- handle derivations of interfaces in formals of generics (see
1723 -- Derive_Subprograms).
1725 Elmt := First_Elmt (Primitive_Operations (Iface));
1726 while Present (Elmt) loop
1727 Iface_Prim := Node (Elmt);
1729 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1730 Prim :=
1731 Find_Primitive_Covering_Interface
1732 (Tagged_Type => Tagged_Type,
1733 Iface_Prim => Iface_Prim);
1735 if No (Prim) and then Serious_Errors_Detected > 0 then
1736 goto Continue;
1737 end if;
1739 pragma Assert (Present (Prim));
1741 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1742 -- differs from the name of the interface primitive then it is
1743 -- a private primitive inherited from a parent type. In such
1744 -- case, given that Tagged_Type covers the interface, the
1745 -- inherited private primitive becomes visible. For such
1746 -- purpose we add a new entity that renames the inherited
1747 -- private primitive.
1749 if Chars (Prim) /= Chars (Iface_Prim) then
1750 pragma Assert (Has_Suffix (Prim, 'P'));
1751 Derive_Subprogram
1752 (New_Subp => New_Subp,
1753 Parent_Subp => Iface_Prim,
1754 Derived_Type => Tagged_Type,
1755 Parent_Type => Iface);
1756 Set_Alias (New_Subp, Prim);
1757 Set_Is_Abstract_Subprogram
1758 (New_Subp, Is_Abstract_Subprogram (Prim));
1759 end if;
1761 Derive_Subprogram
1762 (New_Subp => New_Subp,
1763 Parent_Subp => Iface_Prim,
1764 Derived_Type => Tagged_Type,
1765 Parent_Type => Iface);
1767 declare
1768 Anc : Entity_Id;
1769 begin
1770 if Is_Inherited_Operation (Prim)
1771 and then Present (Alias (Prim))
1772 then
1773 Anc := Alias (Prim);
1774 else
1775 Anc := Overridden_Operation (Prim);
1776 end if;
1778 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1779 -- nonconforming preconditions in both an ancestor and
1780 -- a progenitor operation.
1782 -- If the operation is a primitive wrapper it is an explicit
1783 -- (overriding) operqtion and all is fine.
1785 if Present (Anc)
1786 and then Has_Non_Trivial_Precondition (Anc)
1787 and then Has_Non_Trivial_Precondition (Iface_Prim)
1788 then
1789 if Is_Abstract_Subprogram (Prim)
1790 or else
1791 (Ekind (Prim) = E_Procedure
1792 and then Nkind (Parent (Prim)) =
1793 N_Procedure_Specification
1794 and then Null_Present (Parent (Prim)))
1795 or else Is_Primitive_Wrapper (Prim)
1796 then
1797 null;
1799 -- The operation is inherited and must be overridden
1801 elsif not Comes_From_Source (Prim) then
1802 Error_Msg_NE
1803 ("&inherits non-conforming preconditions and must "
1804 & "be overridden (RM 6.1.1 (10-16))",
1805 Parent (Tagged_Type), Prim);
1806 end if;
1807 end if;
1808 end;
1810 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1811 -- associated with interface types. These entities are
1812 -- only registered in the list of primitives of its
1813 -- corresponding tagged type because they are only used
1814 -- to fill the contents of the secondary dispatch tables.
1815 -- Therefore they are removed from the homonym chains.
1817 Set_Is_Hidden (New_Subp);
1818 Set_Is_Internal (New_Subp);
1819 Set_Alias (New_Subp, Prim);
1820 Set_Is_Abstract_Subprogram
1821 (New_Subp, Is_Abstract_Subprogram (Prim));
1822 Set_Interface_Alias (New_Subp, Iface_Prim);
1824 -- If the returned type is an interface then propagate it to
1825 -- the returned type. Needed by the thunk to generate the code
1826 -- which displaces "this" to reference the corresponding
1827 -- secondary dispatch table in the returned object.
1829 if Is_Interface (Etype (Iface_Prim)) then
1830 Set_Etype (New_Subp, Etype (Iface_Prim));
1831 end if;
1833 -- Internal entities associated with interface types are only
1834 -- registered in the list of primitives of the tagged type.
1835 -- They are only used to fill the contents of the secondary
1836 -- dispatch tables. Therefore they are not needed in the
1837 -- homonym chains.
1839 Remove_Homonym (New_Subp);
1841 -- Hidden entities associated with interfaces must have set
1842 -- the Has_Delay_Freeze attribute to ensure that, in case
1843 -- of locally defined tagged types (or compiling with static
1844 -- dispatch tables generation disabled) the corresponding
1845 -- entry of the secondary dispatch table is filled when such
1846 -- an entity is frozen.
1848 Set_Has_Delayed_Freeze (New_Subp);
1849 end if;
1851 <<Continue>>
1852 Next_Elmt (Elmt);
1853 end loop;
1855 Next_Elmt (Iface_Elmt);
1856 end loop;
1858 if Restore_Scope then
1859 Pop_Scope;
1860 end if;
1861 end Add_Internal_Interface_Entities;
1863 -----------------------------------
1864 -- Analyze_Component_Declaration --
1865 -----------------------------------
1867 procedure Analyze_Component_Declaration (N : Node_Id) is
1868 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1869 Id : constant Entity_Id := Defining_Identifier (N);
1870 E : constant Node_Id := Expression (N);
1871 Typ : constant Node_Id :=
1872 Subtype_Indication (Component_Definition (N));
1873 T : Entity_Id;
1874 P : Entity_Id;
1876 function Contains_POC (Constr : Node_Id) return Boolean;
1877 -- Determines whether a constraint uses the discriminant of a record
1878 -- type thus becoming a per-object constraint (POC).
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1881 -- Typ is the type of the current component, check whether this type is
1882 -- a limited type. Used to validate declaration against that of
1883 -- enclosing record.
1885 ------------------
1886 -- Contains_POC --
1887 ------------------
1889 function Contains_POC (Constr : Node_Id) return Boolean is
1890 begin
1891 -- Prevent cascaded errors
1893 if Error_Posted (Constr) then
1894 return False;
1895 end if;
1897 case Nkind (Constr) is
1898 when N_Attribute_Reference =>
1899 return Attribute_Name (Constr) = Name_Access
1900 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1902 when N_Discriminant_Association =>
1903 return Denotes_Discriminant (Expression (Constr));
1905 when N_Identifier =>
1906 return Denotes_Discriminant (Constr);
1908 when N_Index_Or_Discriminant_Constraint =>
1909 declare
1910 IDC : Node_Id;
1912 begin
1913 IDC := First (Constraints (Constr));
1914 while Present (IDC) loop
1916 -- One per-object constraint is sufficient
1918 if Contains_POC (IDC) then
1919 return True;
1920 end if;
1922 Next (IDC);
1923 end loop;
1925 return False;
1926 end;
1928 when N_Range =>
1929 return Denotes_Discriminant (Low_Bound (Constr))
1930 or else
1931 Denotes_Discriminant (High_Bound (Constr));
1933 when N_Range_Constraint =>
1934 return Denotes_Discriminant (Range_Expression (Constr));
1936 when others =>
1937 return False;
1938 end case;
1939 end Contains_POC;
1941 ----------------------
1942 -- Is_Known_Limited --
1943 ----------------------
1945 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1946 P : constant Entity_Id := Etype (Typ);
1947 R : constant Entity_Id := Root_Type (Typ);
1949 begin
1950 if Is_Limited_Record (Typ) then
1951 return True;
1953 -- If the root type is limited (and not a limited interface) so is
1954 -- the current type.
1956 elsif Is_Limited_Record (R)
1957 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1958 then
1959 return True;
1961 -- Else the type may have a limited interface progenitor, but a
1962 -- limited record parent that is not an interface.
1964 elsif R /= P
1965 and then Is_Limited_Record (P)
1966 and then not Is_Interface (P)
1967 then
1968 return True;
1970 else
1971 return False;
1972 end if;
1973 end Is_Known_Limited;
1975 -- Start of processing for Analyze_Component_Declaration
1977 begin
1978 Generate_Definition (Id);
1979 Enter_Name (Id);
1981 if Present (Typ) then
1982 T := Find_Type_Of_Object
1983 (Subtype_Indication (Component_Definition (N)), N);
1985 -- Ada 2005 (AI-230): Access Definition case
1987 else
1988 pragma Assert (Present
1989 (Access_Definition (Component_Definition (N))));
1991 T := Access_Definition
1992 (Related_Nod => N,
1993 N => Access_Definition (Component_Definition (N)));
1994 Set_Is_Local_Anonymous_Access (T);
1996 -- Ada 2005 (AI-254)
1998 if Present (Access_To_Subprogram_Definition
1999 (Access_Definition (Component_Definition (N))))
2000 and then Protected_Present (Access_To_Subprogram_Definition
2001 (Access_Definition
2002 (Component_Definition (N))))
2003 then
2004 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2005 end if;
2006 end if;
2008 -- If the subtype is a constrained subtype of the enclosing record,
2009 -- (which must have a partial view) the back-end does not properly
2010 -- handle the recursion. Rewrite the component declaration with an
2011 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2012 -- the tree directly because side effects have already been removed from
2013 -- discriminant constraints.
2015 if Ekind (T) = E_Access_Subtype
2016 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2017 and then Comes_From_Source (T)
2018 and then Nkind (Parent (T)) = N_Subtype_Declaration
2019 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2020 then
2021 Rewrite
2022 (Subtype_Indication (Component_Definition (N)),
2023 New_Copy_Tree (Subtype_Indication (Parent (T))));
2024 T := Find_Type_Of_Object
2025 (Subtype_Indication (Component_Definition (N)), N);
2026 end if;
2028 -- If the component declaration includes a default expression, then we
2029 -- check that the component is not of a limited type (RM 3.7(5)),
2030 -- and do the special preanalysis of the expression (see section on
2031 -- "Handling of Default and Per-Object Expressions" in the spec of
2032 -- package Sem).
2034 if Present (E) then
2035 Preanalyze_Default_Expression (E, T);
2036 Check_Initialization (T, E);
2038 if Ada_Version >= Ada_2005
2039 and then Ekind (T) = E_Anonymous_Access_Type
2040 and then Etype (E) /= Any_Type
2041 then
2042 -- Check RM 3.9.2(9): "if the expected type for an expression is
2043 -- an anonymous access-to-specific tagged type, then the object
2044 -- designated by the expression shall not be dynamically tagged
2045 -- unless it is a controlling operand in a call on a dispatching
2046 -- operation"
2048 if Is_Tagged_Type (Directly_Designated_Type (T))
2049 and then
2050 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2051 and then
2052 Ekind (Directly_Designated_Type (Etype (E))) =
2053 E_Class_Wide_Type
2054 then
2055 Error_Msg_N
2056 ("access to specific tagged type required (RM 3.9.2(9))", E);
2057 end if;
2059 -- (Ada 2005: AI-230): Accessibility check for anonymous
2060 -- components
2062 if Type_Access_Level (Etype (E)) >
2063 Deepest_Type_Access_Level (T)
2064 then
2065 Error_Msg_N
2066 ("expression has deeper access level than component " &
2067 "(RM 3.10.2 (12.2))", E);
2068 end if;
2070 -- The initialization expression is a reference to an access
2071 -- discriminant. The type of the discriminant is always deeper
2072 -- than any access type.
2074 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2075 and then Is_Entity_Name (E)
2076 and then Ekind (Entity (E)) = E_In_Parameter
2077 and then Present (Discriminal_Link (Entity (E)))
2078 then
2079 Error_Msg_N
2080 ("discriminant has deeper accessibility level than target",
2082 end if;
2083 end if;
2084 end if;
2086 -- The parent type may be a private view with unknown discriminants,
2087 -- and thus unconstrained. Regular components must be constrained.
2089 if not Is_Definite_Subtype (T)
2090 and then Chars (Id) /= Name_uParent
2091 then
2092 if Is_Class_Wide_Type (T) then
2093 Error_Msg_N
2094 ("class-wide subtype with unknown discriminants" &
2095 " in component declaration",
2096 Subtype_Indication (Component_Definition (N)));
2097 else
2098 Error_Msg_N
2099 ("unconstrained subtype in component declaration",
2100 Subtype_Indication (Component_Definition (N)));
2101 end if;
2103 -- Components cannot be abstract, except for the special case of
2104 -- the _Parent field (case of extending an abstract tagged type)
2106 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2107 Error_Msg_N ("type of a component cannot be abstract", N);
2108 end if;
2110 Set_Etype (Id, T);
2112 if Aliased_Present (Component_Definition (N)) then
2113 Set_Is_Aliased (Id);
2115 -- AI12-001: All aliased objects are considered to be specified as
2116 -- independently addressable (RM C.6(8.1/4)).
2118 Set_Is_Independent (Id);
2119 end if;
2121 -- The component declaration may have a per-object constraint, set
2122 -- the appropriate flag in the defining identifier of the subtype.
2124 if Present (Subtype_Indication (Component_Definition (N))) then
2125 declare
2126 Sindic : constant Node_Id :=
2127 Subtype_Indication (Component_Definition (N));
2128 begin
2129 if Nkind (Sindic) = N_Subtype_Indication
2130 and then Present (Constraint (Sindic))
2131 and then Contains_POC (Constraint (Sindic))
2132 then
2133 Set_Has_Per_Object_Constraint (Id);
2134 end if;
2135 end;
2136 end if;
2138 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2139 -- out some static checks.
2141 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2142 Null_Exclusion_Static_Checks (N);
2143 end if;
2145 -- If this component is private (or depends on a private type), flag the
2146 -- record type to indicate that some operations are not available.
2148 P := Private_Component (T);
2150 if Present (P) then
2152 -- Check for circular definitions
2154 if P = Any_Type then
2155 Set_Etype (Id, Any_Type);
2157 -- There is a gap in the visibility of operations only if the
2158 -- component type is not defined in the scope of the record type.
2160 elsif Scope (P) = Scope (Current_Scope) then
2161 null;
2163 elsif Is_Limited_Type (P) then
2164 Set_Is_Limited_Composite (Current_Scope);
2166 else
2167 Set_Is_Private_Composite (Current_Scope);
2168 end if;
2169 end if;
2171 if P /= Any_Type
2172 and then Is_Limited_Type (T)
2173 and then Chars (Id) /= Name_uParent
2174 and then Is_Tagged_Type (Current_Scope)
2175 then
2176 if Is_Derived_Type (Current_Scope)
2177 and then not Is_Known_Limited (Current_Scope)
2178 then
2179 Error_Msg_N
2180 ("extension of nonlimited type cannot have limited components",
2183 if Is_Interface (Root_Type (Current_Scope)) then
2184 Error_Msg_N
2185 ("\limitedness is not inherited from limited interface", N);
2186 Error_Msg_N ("\add LIMITED to type indication", N);
2187 end if;
2189 Explain_Limited_Type (T, N);
2190 Set_Etype (Id, Any_Type);
2191 Set_Is_Limited_Composite (Current_Scope, False);
2193 elsif not Is_Derived_Type (Current_Scope)
2194 and then not Is_Limited_Record (Current_Scope)
2195 and then not Is_Concurrent_Type (Current_Scope)
2196 then
2197 Error_Msg_N
2198 ("nonlimited tagged type cannot have limited components", N);
2199 Explain_Limited_Type (T, N);
2200 Set_Etype (Id, Any_Type);
2201 Set_Is_Limited_Composite (Current_Scope, False);
2202 end if;
2203 end if;
2205 -- If the component is an unconstrained task or protected type with
2206 -- discriminants, the component and the enclosing record are limited
2207 -- and the component is constrained by its default values. Compute
2208 -- its actual subtype, else it may be allocated the maximum size by
2209 -- the backend, and possibly overflow.
2211 if Is_Concurrent_Type (T)
2212 and then not Is_Constrained (T)
2213 and then Has_Discriminants (T)
2214 and then not Has_Discriminants (Current_Scope)
2215 then
2216 declare
2217 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2219 begin
2220 Set_Etype (Id, Act_T);
2222 -- Rewrite component definition to use the constrained subtype
2224 Rewrite (Component_Definition (N),
2225 Make_Component_Definition (Loc,
2226 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2227 end;
2228 end if;
2230 Set_Original_Record_Component (Id, Id);
2232 if Has_Aspects (N) then
2233 Analyze_Aspect_Specifications (N, Id);
2234 end if;
2236 Analyze_Dimension (N);
2237 end Analyze_Component_Declaration;
2239 --------------------------
2240 -- Analyze_Declarations --
2241 --------------------------
2243 procedure Analyze_Declarations (L : List_Id) is
2244 Decl : Node_Id;
2246 procedure Adjust_Decl;
2247 -- Adjust Decl not to include implicit label declarations, since these
2248 -- have strange Sloc values that result in elaboration check problems.
2249 -- (They have the sloc of the label as found in the source, and that
2250 -- is ahead of the current declarative part).
2252 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2253 -- Create the subprogram bodies which verify the run-time semantics of
2254 -- the pragmas listed below for each elibigle type found in declarative
2255 -- list Decls. The pragmas are:
2257 -- Default_Initial_Condition
2258 -- Invariant
2259 -- Type_Invariant
2261 -- Context denotes the owner of the declarative list.
2263 procedure Check_Entry_Contracts;
2264 -- Perform a preanalysis of the pre- and postconditions of an entry
2265 -- declaration. This must be done before full resolution and creation
2266 -- of the parameter block, etc. to catch illegal uses within the
2267 -- contract expression. Full analysis of the expression is done when
2268 -- the contract is processed.
2270 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2271 -- Check if a nested package has entities within it that rely on library
2272 -- level private types where the full view has not been completed for
2273 -- the purposes of checking if it is acceptable to freeze an expression
2274 -- function at the point of declaration.
2276 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2277 -- Determine whether Body_Decl denotes the body of a late controlled
2278 -- primitive (either Initialize, Adjust or Finalize). If this is the
2279 -- case, add a proper spec if the body lacks one. The spec is inserted
2280 -- before Body_Decl and immediately analyzed.
2282 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2283 -- Spec_Id is the entity of a package that may define abstract states,
2284 -- and in the case of a child unit, whose ancestors may define abstract
2285 -- states. If the states have partial visible refinement, remove the
2286 -- partial visibility of each constituent at the end of the package
2287 -- spec and body declarations.
2289 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2290 -- Spec_Id is the entity of a package that may define abstract states.
2291 -- If the states have visible refinement, remove the visibility of each
2292 -- constituent at the end of the package body declaration.
2294 procedure Resolve_Aspects;
2295 -- Utility to resolve the expressions of aspects at the end of a list of
2296 -- declarations, or before a declaration that freezes previous entities,
2297 -- such as in a subprogram body.
2299 -----------------
2300 -- Adjust_Decl --
2301 -----------------
2303 procedure Adjust_Decl is
2304 begin
2305 while Present (Prev (Decl))
2306 and then Nkind (Decl) = N_Implicit_Label_Declaration
2307 loop
2308 Prev (Decl);
2309 end loop;
2310 end Adjust_Decl;
2312 ----------------------------
2313 -- Build_Assertion_Bodies --
2314 ----------------------------
2316 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2317 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2318 -- Create the subprogram bodies which verify the run-time semantics
2319 -- of the pragmas listed below for type Typ. The pragmas are:
2321 -- Default_Initial_Condition
2322 -- Invariant
2323 -- Type_Invariant
2325 -------------------------------------
2326 -- Build_Assertion_Bodies_For_Type --
2327 -------------------------------------
2329 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2330 begin
2331 if Nkind (Context) = N_Package_Specification then
2333 -- Preanalyze and resolve the class-wide invariants of an
2334 -- interface at the end of whichever declarative part has the
2335 -- interface type. Note that an interface may be declared in
2336 -- any non-package declarative part, but reaching the end of
2337 -- such a declarative part will always freeze the type and
2338 -- generate the invariant procedure (see Freeze_Type).
2340 if Is_Interface (Typ) then
2342 -- Interfaces are treated as the partial view of a private
2343 -- type, in order to achieve uniformity with the general
2344 -- case. As a result, an interface receives only a "partial"
2345 -- invariant procedure, which is never called.
2347 if Has_Own_Invariants (Typ) then
2348 Build_Invariant_Procedure_Body
2349 (Typ => Typ,
2350 Partial_Invariant => True);
2351 end if;
2353 elsif Decls = Visible_Declarations (Context) then
2354 -- Preanalyze and resolve the invariants of a private type
2355 -- at the end of the visible declarations to catch potential
2356 -- errors. Inherited class-wide invariants are not included
2357 -- because they have already been resolved.
2359 if Ekind (Typ) in E_Limited_Private_Type
2360 | E_Private_Type
2361 | E_Record_Type_With_Private
2362 and then Has_Own_Invariants (Typ)
2363 then
2364 Build_Invariant_Procedure_Body
2365 (Typ => Typ,
2366 Partial_Invariant => True);
2367 end if;
2369 -- Preanalyze and resolve the Default_Initial_Condition
2370 -- assertion expression at the end of the declarations to
2371 -- catch any errors.
2373 if Ekind (Typ) in E_Limited_Private_Type
2374 | E_Private_Type
2375 | E_Record_Type_With_Private
2376 and then Has_Own_DIC (Typ)
2377 then
2378 Build_DIC_Procedure_Body
2379 (Typ => Typ,
2380 Partial_DIC => True);
2381 end if;
2383 elsif Decls = Private_Declarations (Context) then
2385 -- Preanalyze and resolve the invariants of a private type's
2386 -- full view at the end of the private declarations to catch
2387 -- potential errors.
2389 if (not Is_Private_Type (Typ)
2390 or else Present (Underlying_Full_View (Typ)))
2391 and then Has_Private_Declaration (Typ)
2392 and then Has_Invariants (Typ)
2393 then
2394 Build_Invariant_Procedure_Body (Typ);
2395 end if;
2397 if (not Is_Private_Type (Typ)
2398 or else Present (Underlying_Full_View (Typ)))
2399 and then Has_Private_Declaration (Typ)
2400 and then Has_DIC (Typ)
2401 then
2402 Build_DIC_Procedure_Body (Typ);
2403 end if;
2404 end if;
2405 end if;
2406 end Build_Assertion_Bodies_For_Type;
2408 -- Local variables
2410 Decl : Node_Id;
2411 Decl_Id : Entity_Id;
2413 -- Start of processing for Build_Assertion_Bodies
2415 begin
2416 Decl := First (Decls);
2417 while Present (Decl) loop
2418 if Is_Declaration (Decl) then
2419 Decl_Id := Defining_Entity (Decl);
2421 if Is_Type (Decl_Id) then
2422 Build_Assertion_Bodies_For_Type (Decl_Id);
2423 end if;
2424 end if;
2426 Next (Decl);
2427 end loop;
2428 end Build_Assertion_Bodies;
2430 ---------------------------
2431 -- Check_Entry_Contracts --
2432 ---------------------------
2434 procedure Check_Entry_Contracts is
2435 ASN : Node_Id;
2436 Ent : Entity_Id;
2437 Exp : Node_Id;
2439 begin
2440 Ent := First_Entity (Current_Scope);
2441 while Present (Ent) loop
2443 -- This only concerns entries with pre/postconditions
2445 if Ekind (Ent) = E_Entry
2446 and then Present (Contract (Ent))
2447 and then Present (Pre_Post_Conditions (Contract (Ent)))
2448 then
2449 ASN := Pre_Post_Conditions (Contract (Ent));
2450 Push_Scope (Ent);
2451 Install_Formals (Ent);
2453 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2454 -- is performed on a copy of the pragma expression, to prevent
2455 -- modifying the original expression.
2457 while Present (ASN) loop
2458 if Nkind (ASN) = N_Pragma then
2459 Exp :=
2460 New_Copy_Tree
2461 (Expression
2462 (First (Pragma_Argument_Associations (ASN))));
2463 Set_Parent (Exp, ASN);
2465 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2466 end if;
2468 ASN := Next_Pragma (ASN);
2469 end loop;
2471 End_Scope;
2472 end if;
2474 Next_Entity (Ent);
2475 end loop;
2476 end Check_Entry_Contracts;
2478 ----------------------------------
2479 -- Contains_Lib_Incomplete_Type --
2480 ----------------------------------
2482 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2483 Curr : Entity_Id;
2485 begin
2486 -- Avoid looking through scopes that do not meet the precondition of
2487 -- Pkg not being within a library unit spec.
2489 if not Is_Compilation_Unit (Pkg)
2490 and then not Is_Generic_Instance (Pkg)
2491 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2492 then
2493 -- Loop through all entities in the current scope to identify
2494 -- an entity that depends on a private type.
2496 Curr := First_Entity (Pkg);
2497 loop
2498 if Nkind (Curr) in N_Entity
2499 and then Depends_On_Private (Curr)
2500 then
2501 return True;
2502 end if;
2504 exit when Last_Entity (Current_Scope) = Curr;
2505 Next_Entity (Curr);
2506 end loop;
2507 end if;
2509 return False;
2510 end Contains_Lib_Incomplete_Type;
2512 --------------------------------------
2513 -- Handle_Late_Controlled_Primitive --
2514 --------------------------------------
2516 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2517 Body_Spec : constant Node_Id := Specification (Body_Decl);
2518 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2519 Loc : constant Source_Ptr := Sloc (Body_Id);
2520 Params : constant List_Id :=
2521 Parameter_Specifications (Body_Spec);
2522 Spec : Node_Id;
2523 Spec_Id : Entity_Id;
2524 Typ : Node_Id;
2526 begin
2527 -- Consider only procedure bodies whose name matches one of the three
2528 -- controlled primitives.
2530 if Nkind (Body_Spec) /= N_Procedure_Specification
2531 or else Chars (Body_Id) not in Name_Adjust
2532 | Name_Finalize
2533 | Name_Initialize
2534 then
2535 return;
2537 -- A controlled primitive must have exactly one formal which is not
2538 -- an anonymous access type.
2540 elsif List_Length (Params) /= 1 then
2541 return;
2542 end if;
2544 Typ := Parameter_Type (First (Params));
2546 if Nkind (Typ) = N_Access_Definition then
2547 return;
2548 end if;
2550 Find_Type (Typ);
2552 -- The type of the formal must be derived from [Limited_]Controlled
2554 if not Is_Controlled (Entity (Typ)) then
2555 return;
2556 end if;
2558 -- Check whether a specification exists for this body. We do not
2559 -- analyze the spec of the body in full, because it will be analyzed
2560 -- again when the body is properly analyzed, and we cannot create
2561 -- duplicate entries in the formals chain. We look for an explicit
2562 -- specification because the body may be an overriding operation and
2563 -- an inherited spec may be present.
2565 Spec_Id := Current_Entity (Body_Id);
2567 while Present (Spec_Id) loop
2568 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2569 and then Scope (Spec_Id) = Current_Scope
2570 and then Present (First_Formal (Spec_Id))
2571 and then No (Next_Formal (First_Formal (Spec_Id)))
2572 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2573 and then Comes_From_Source (Spec_Id)
2574 then
2575 return;
2576 end if;
2578 Spec_Id := Homonym (Spec_Id);
2579 end loop;
2581 -- At this point the body is known to be a late controlled primitive.
2582 -- Generate a matching spec and insert it before the body. Note the
2583 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2584 -- tree in this case.
2586 Spec := Copy_Separate_Tree (Body_Spec);
2588 -- Ensure that the subprogram declaration does not inherit the null
2589 -- indicator from the body as we now have a proper spec/body pair.
2591 Set_Null_Present (Spec, False);
2593 -- Ensure that the freeze node is inserted after the declaration of
2594 -- the primitive since its expansion will freeze the primitive.
2596 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2598 Insert_Before_And_Analyze (Body_Decl, Decl);
2599 end Handle_Late_Controlled_Primitive;
2601 ----------------------------------------
2602 -- Remove_Partial_Visible_Refinements --
2603 ----------------------------------------
2605 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2606 State_Elmt : Elmt_Id;
2607 begin
2608 if Present (Abstract_States (Spec_Id)) then
2609 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2610 while Present (State_Elmt) loop
2611 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2612 Next_Elmt (State_Elmt);
2613 end loop;
2614 end if;
2616 -- For a child unit, also hide the partial state refinement from
2617 -- ancestor packages.
2619 if Is_Child_Unit (Spec_Id) then
2620 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2621 end if;
2622 end Remove_Partial_Visible_Refinements;
2624 --------------------------------
2625 -- Remove_Visible_Refinements --
2626 --------------------------------
2628 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2629 State_Elmt : Elmt_Id;
2630 begin
2631 if Present (Abstract_States (Spec_Id)) then
2632 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2633 while Present (State_Elmt) loop
2634 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2635 Next_Elmt (State_Elmt);
2636 end loop;
2637 end if;
2638 end Remove_Visible_Refinements;
2640 ---------------------
2641 -- Resolve_Aspects --
2642 ---------------------
2644 procedure Resolve_Aspects is
2645 E : Entity_Id;
2647 begin
2648 E := First_Entity (Current_Scope);
2649 while Present (E) loop
2650 Resolve_Aspect_Expressions (E);
2652 -- Now that the aspect expressions have been resolved, if this is
2653 -- at the end of the visible declarations, we can set the flag
2654 -- Known_To_Have_Preelab_Init properly on types declared in the
2655 -- visible part, which is needed for checking whether full types
2656 -- in the private part satisfy the Preelaborable_Initialization
2657 -- aspect of the partial view. We can't wait for the creation of
2658 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2659 -- freeze point may occur after the end of the package declaration
2660 -- (in the case of nested packages).
2662 if Is_Type (E)
2663 and then L = Visible_Declarations (Parent (L))
2664 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2665 then
2666 declare
2667 ASN : constant Node_Id :=
2668 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2669 Expr : constant Node_Id := Expression (ASN);
2670 begin
2671 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2672 -- expression, or if the expression is True (or was folded
2673 -- to True), or if the expression is a conjunction of one or
2674 -- more Preelaborable_Initialization attributes applied to
2675 -- formal types and wasn't folded to False. (Note that
2676 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2677 -- Original_Node if needed, hence test for Standard_False.)
2679 if not Present (Expr)
2680 or else (Is_Entity_Name (Expr)
2681 and then Entity (Expr) = Standard_True)
2682 or else
2683 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2684 and then
2685 not (Is_Entity_Name (Expr)
2686 and then Entity (Expr) = Standard_False))
2687 then
2688 Set_Known_To_Have_Preelab_Init (E);
2689 end if;
2690 end;
2691 end if;
2693 Next_Entity (E);
2694 end loop;
2695 end Resolve_Aspects;
2697 -- Local variables
2699 Context : Node_Id := Empty;
2700 Ctrl_Typ : Entity_Id := Empty;
2701 Freeze_From : Entity_Id := Empty;
2702 Next_Decl : Node_Id;
2704 -- Start of processing for Analyze_Declarations
2706 begin
2707 Decl := First (L);
2708 while Present (Decl) loop
2710 -- Complete analysis of declaration
2712 Analyze (Decl);
2713 Next_Decl := Next (Decl);
2715 if No (Freeze_From) then
2716 Freeze_From := First_Entity (Current_Scope);
2717 end if;
2719 -- Remember if the declaration we just processed is the full type
2720 -- declaration of a controlled type (to handle late overriding of
2721 -- initialize, adjust or finalize).
2723 if Nkind (Decl) = N_Full_Type_Declaration
2724 and then Is_Controlled (Defining_Identifier (Decl))
2725 then
2726 Ctrl_Typ := Defining_Identifier (Decl);
2727 end if;
2729 -- At the end of a declarative part, freeze remaining entities
2730 -- declared in it. The end of the visible declarations of package
2731 -- specification is not the end of a declarative part if private
2732 -- declarations are present. The end of a package declaration is a
2733 -- freezing point only if it a library package. A task definition or
2734 -- protected type definition is not a freeze point either. Finally,
2735 -- we do not freeze entities in generic scopes, because there is no
2736 -- code generated for them and freeze nodes will be generated for
2737 -- the instance.
2739 -- The end of a package instantiation is not a freeze point, but
2740 -- for now we make it one, because the generic body is inserted
2741 -- (currently) immediately after. Generic instantiations will not
2742 -- be a freeze point once delayed freezing of bodies is implemented.
2743 -- (This is needed in any case for early instantiations ???).
2745 if No (Next_Decl) then
2746 if Nkind (Parent (L)) = N_Component_List then
2747 null;
2749 elsif Nkind (Parent (L)) in
2750 N_Protected_Definition | N_Task_Definition
2751 then
2752 Check_Entry_Contracts;
2754 elsif Nkind (Parent (L)) /= N_Package_Specification then
2755 if Nkind (Parent (L)) = N_Package_Body then
2756 Freeze_From := First_Entity (Current_Scope);
2757 end if;
2759 -- There may have been several freezing points previously,
2760 -- for example object declarations or subprogram bodies, but
2761 -- at the end of a declarative part we check freezing from
2762 -- the beginning, even though entities may already be frozen,
2763 -- in order to perform visibility checks on delayed aspects.
2765 Adjust_Decl;
2767 -- If the current scope is a generic subprogram body. Skip the
2768 -- generic formal parameters that are not frozen here.
2770 if Is_Subprogram (Current_Scope)
2771 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2772 N_Generic_Subprogram_Declaration
2773 and then Present (First_Entity (Current_Scope))
2774 then
2775 while Is_Generic_Formal (Freeze_From) loop
2776 Next_Entity (Freeze_From);
2777 end loop;
2779 Freeze_All (Freeze_From, Decl);
2780 Freeze_From := Last_Entity (Current_Scope);
2782 else
2783 -- For declarations in a subprogram body there is no issue
2784 -- with name resolution in aspect specifications.
2786 Freeze_All (First_Entity (Current_Scope), Decl);
2787 Freeze_From := Last_Entity (Current_Scope);
2788 end if;
2790 -- Current scope is a package specification
2792 elsif Scope (Current_Scope) /= Standard_Standard
2793 and then not Is_Child_Unit (Current_Scope)
2794 and then No (Generic_Parent (Parent (L)))
2795 then
2796 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2797 -- resolved at the end of the immediately enclosing declaration
2798 -- list (AI05-0183-1).
2800 Resolve_Aspects;
2802 elsif L /= Visible_Declarations (Parent (L))
2803 or else Is_Empty_List (Private_Declarations (Parent (L)))
2804 then
2805 Adjust_Decl;
2807 -- End of a package declaration
2809 -- This is a freeze point because it is the end of a
2810 -- compilation unit.
2812 Freeze_All (First_Entity (Current_Scope), Decl);
2813 Freeze_From := Last_Entity (Current_Scope);
2815 -- At the end of the visible declarations the expressions in
2816 -- aspects of all entities declared so far must be resolved.
2817 -- The entities themselves might be frozen later, and the
2818 -- generated pragmas and attribute definition clauses analyzed
2819 -- in full at that point, but name resolution must take place
2820 -- now.
2821 -- In addition to being the proper semantics, this is mandatory
2822 -- within generic units, because global name capture requires
2823 -- those expressions to be analyzed, given that the generated
2824 -- pragmas do not appear in the original generic tree.
2826 elsif Serious_Errors_Detected = 0 then
2827 Resolve_Aspects;
2828 end if;
2830 -- If next node is a body then freeze all types before the body.
2831 -- An exception occurs for some expander-generated bodies. If these
2832 -- are generated at places where in general language rules would not
2833 -- allow a freeze point, then we assume that the expander has
2834 -- explicitly checked that all required types are properly frozen,
2835 -- and we do not cause general freezing here. This special circuit
2836 -- is used when the encountered body is marked as having already
2837 -- been analyzed.
2839 -- In all other cases (bodies that come from source, and expander
2840 -- generated bodies that have not been analyzed yet), freeze all
2841 -- types now. Note that in the latter case, the expander must take
2842 -- care to attach the bodies at a proper place in the tree so as to
2843 -- not cause unwanted freezing at that point.
2845 -- It is also necessary to check for a case where both an expression
2846 -- function is used and the current scope depends on an incomplete
2847 -- private type from a library unit, otherwise premature freezing of
2848 -- the private type will occur.
2850 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2851 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2852 or else not Was_Expression_Function (Next_Decl))
2853 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2854 and then not Contains_Lib_Incomplete_Type
2855 (Current_Scope)))
2856 then
2857 -- When a controlled type is frozen, the expander generates stream
2858 -- and controlled-type support routines. If the freeze is caused
2859 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2860 -- expander will end up using the wrong version of these routines,
2861 -- as the body has not been processed yet. To remedy this, detect
2862 -- a late controlled primitive and create a proper spec for it.
2863 -- This ensures that the primitive will override its inherited
2864 -- counterpart before the freeze takes place.
2866 -- If the declaration we just processed is a body, do not attempt
2867 -- to examine Next_Decl as the late primitive idiom can only apply
2868 -- to the first encountered body.
2870 -- ??? A cleaner approach may be possible and/or this solution
2871 -- could be extended to general-purpose late primitives.
2873 if Present (Ctrl_Typ) then
2875 -- No need to continue searching for late body overriding if
2876 -- the controlled type is already frozen.
2878 if Is_Frozen (Ctrl_Typ) then
2879 Ctrl_Typ := Empty;
2881 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2882 Handle_Late_Controlled_Primitive (Next_Decl);
2883 end if;
2884 end if;
2886 Adjust_Decl;
2888 -- The generated body of an expression function does not freeze,
2889 -- unless it is a completion, in which case only the expression
2890 -- itself freezes. This is handled when the body itself is
2891 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2893 Freeze_All (Freeze_From, Decl);
2894 Freeze_From := Last_Entity (Current_Scope);
2895 end if;
2897 Decl := Next_Decl;
2898 end loop;
2900 -- Post-freezing actions
2902 if Present (L) then
2903 Context := Parent (L);
2905 -- Certain contract annotations have forward visibility semantics and
2906 -- must be analyzed after all declarative items have been processed.
2907 -- This timing ensures that entities referenced by such contracts are
2908 -- visible.
2910 -- Analyze the contract of an immediately enclosing package spec or
2911 -- body first because other contracts may depend on its information.
2913 if Nkind (Context) = N_Package_Body then
2914 Analyze_Package_Body_Contract (Defining_Entity (Context));
2916 elsif Nkind (Context) = N_Package_Specification then
2917 Analyze_Package_Contract (Defining_Entity (Context));
2918 end if;
2920 -- Analyze the contracts of various constructs in the declarative
2921 -- list.
2923 Analyze_Contracts (L);
2925 if Nkind (Context) = N_Package_Body then
2927 -- Ensure that all abstract states and objects declared in the
2928 -- state space of a package body are utilized as constituents.
2930 Check_Unused_Body_States (Defining_Entity (Context));
2932 -- State refinements are visible up to the end of the package body
2933 -- declarations. Hide the state refinements from visibility to
2934 -- restore the original state conditions.
2936 Remove_Visible_Refinements (Corresponding_Spec (Context));
2937 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2939 elsif Nkind (Context) = N_Package_Specification then
2941 -- Partial state refinements are visible up to the end of the
2942 -- package spec declarations. Hide the partial state refinements
2943 -- from visibility to restore the original state conditions.
2945 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2946 end if;
2948 -- Verify that all abstract states found in any package declared in
2949 -- the input declarative list have proper refinements. The check is
2950 -- performed only when the context denotes a block, entry, package,
2951 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2953 Check_State_Refinements (Context);
2955 -- Create the subprogram bodies which verify the run-time semantics
2956 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2957 -- types within the current declarative list. This ensures that all
2958 -- assertion expressions are preanalyzed and resolved at the end of
2959 -- the declarative part. Note that the resolution happens even when
2960 -- freezing does not take place.
2962 Build_Assertion_Bodies (L, Context);
2963 end if;
2964 end Analyze_Declarations;
2966 -----------------------------------
2967 -- Analyze_Full_Type_Declaration --
2968 -----------------------------------
2970 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2971 Def : constant Node_Id := Type_Definition (N);
2972 Def_Id : constant Entity_Id := Defining_Identifier (N);
2973 T : Entity_Id;
2974 Prev : Entity_Id;
2976 Is_Remote : constant Boolean :=
2977 (Is_Remote_Types (Current_Scope)
2978 or else Is_Remote_Call_Interface (Current_Scope))
2979 and then not (In_Private_Part (Current_Scope)
2980 or else In_Package_Body (Current_Scope));
2982 procedure Check_Nonoverridable_Aspects;
2983 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2984 -- be overridden, and can only be confirmed on derivation.
2986 procedure Check_Ops_From_Incomplete_Type;
2987 -- If there is a tagged incomplete partial view of the type, traverse
2988 -- the primitives of the incomplete view and change the type of any
2989 -- controlling formals and result to indicate the full view. The
2990 -- primitives will be added to the full type's primitive operations
2991 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2992 -- is called from Process_Incomplete_Dependents).
2994 ----------------------------------
2995 -- Check_Nonoverridable_Aspects --
2996 ----------------------------------
2998 procedure Check_Nonoverridable_Aspects is
2999 function Get_Aspect_Spec
3000 (Specs : List_Id;
3001 Aspect_Name : Name_Id) return Node_Id;
3002 -- Check whether a list of aspect specifications includes an entry
3003 -- for a specific aspect. The list is either that of a partial or
3004 -- a full view.
3006 ---------------------
3007 -- Get_Aspect_Spec --
3008 ---------------------
3010 function Get_Aspect_Spec
3011 (Specs : List_Id;
3012 Aspect_Name : Name_Id) return Node_Id
3014 Spec : Node_Id;
3016 begin
3017 Spec := First (Specs);
3018 while Present (Spec) loop
3019 if Chars (Identifier (Spec)) = Aspect_Name then
3020 return Spec;
3021 end if;
3022 Next (Spec);
3023 end loop;
3025 return Empty;
3026 end Get_Aspect_Spec;
3028 -- Local variables
3030 Prev_Aspects : constant List_Id :=
3031 Aspect_Specifications (Parent (Def_Id));
3032 Par_Type : Entity_Id;
3033 Prev_Aspect : Node_Id;
3035 -- Start of processing for Check_Nonoverridable_Aspects
3037 begin
3038 -- Get parent type of derived type. Note that Prev is the entity in
3039 -- the partial declaration, but its contents are now those of full
3040 -- view, while Def_Id reflects the partial view.
3042 if Is_Private_Type (Def_Id) then
3043 Par_Type := Etype (Full_View (Def_Id));
3044 else
3045 Par_Type := Etype (Def_Id);
3046 end if;
3048 -- If there is an inherited Implicit_Dereference, verify that it is
3049 -- made explicit in the partial view.
3051 if Has_Discriminants (Base_Type (Par_Type))
3052 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3053 and then Present (Discriminant_Specifications (Parent (Prev)))
3054 and then Present (Get_Reference_Discriminant (Par_Type))
3055 then
3056 Prev_Aspect :=
3057 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3059 if No (Prev_Aspect)
3060 and then Present
3061 (Discriminant_Specifications
3062 (Original_Node (Parent (Prev))))
3063 then
3064 Error_Msg_N
3065 ("type does not inherit implicit dereference", Prev);
3067 else
3068 -- If one of the views has the aspect specified, verify that it
3069 -- is consistent with that of the parent.
3071 declare
3072 Cur_Discr : constant Entity_Id :=
3073 Get_Reference_Discriminant (Prev);
3074 Par_Discr : constant Entity_Id :=
3075 Get_Reference_Discriminant (Par_Type);
3077 begin
3078 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3079 Error_Msg_N
3080 ("aspect inconsistent with that of parent", N);
3081 end if;
3083 -- Check that specification in partial view matches the
3084 -- inherited aspect. Compare names directly because aspect
3085 -- expression may not be analyzed.
3087 if Present (Prev_Aspect)
3088 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3089 and then Chars (Expression (Prev_Aspect)) /=
3090 Chars (Cur_Discr)
3091 then
3092 Error_Msg_N
3093 ("aspect inconsistent with that of parent", N);
3094 end if;
3095 end;
3096 end if;
3097 end if;
3099 -- What about other nonoverridable aspects???
3100 end Check_Nonoverridable_Aspects;
3102 ------------------------------------
3103 -- Check_Ops_From_Incomplete_Type --
3104 ------------------------------------
3106 procedure Check_Ops_From_Incomplete_Type is
3107 Elmt : Elmt_Id;
3108 Formal : Entity_Id;
3109 Op : Entity_Id;
3111 begin
3112 if Prev /= T
3113 and then Ekind (Prev) = E_Incomplete_Type
3114 and then Is_Tagged_Type (Prev)
3115 and then Is_Tagged_Type (T)
3116 and then Present (Primitive_Operations (Prev))
3117 then
3118 Elmt := First_Elmt (Primitive_Operations (Prev));
3119 while Present (Elmt) loop
3120 Op := Node (Elmt);
3122 Formal := First_Formal (Op);
3123 while Present (Formal) loop
3124 if Etype (Formal) = Prev then
3125 Set_Etype (Formal, T);
3126 end if;
3128 Next_Formal (Formal);
3129 end loop;
3131 if Etype (Op) = Prev then
3132 Set_Etype (Op, T);
3133 end if;
3135 Next_Elmt (Elmt);
3136 end loop;
3137 end if;
3138 end Check_Ops_From_Incomplete_Type;
3140 -- Start of processing for Analyze_Full_Type_Declaration
3142 begin
3143 Prev := Find_Type_Name (N);
3145 -- The full view, if present, now points to the current type. If there
3146 -- is an incomplete partial view, set a link to it, to simplify the
3147 -- retrieval of primitive operations of the type.
3149 -- Ada 2005 (AI-50217): If the type was previously decorated when
3150 -- imported through a LIMITED WITH clause, it appears as incomplete
3151 -- but has no full view.
3153 if Ekind (Prev) = E_Incomplete_Type
3154 and then Present (Full_View (Prev))
3155 then
3156 T := Full_View (Prev);
3157 Set_Incomplete_View (N, Parent (Prev));
3158 else
3159 T := Prev;
3160 end if;
3162 Set_Is_Pure (T, Is_Pure (Current_Scope));
3164 -- We set the flag Is_First_Subtype here. It is needed to set the
3165 -- corresponding flag for the Implicit class-wide-type created
3166 -- during tagged types processing.
3168 Set_Is_First_Subtype (T, True);
3170 -- Only composite types other than array types are allowed to have
3171 -- discriminants.
3173 case Nkind (Def) is
3175 -- For derived types, the rule will be checked once we've figured
3176 -- out the parent type.
3178 when N_Derived_Type_Definition =>
3179 null;
3181 -- For record types, discriminants are allowed.
3183 when N_Record_Definition =>
3184 null;
3186 when others =>
3187 if Present (Discriminant_Specifications (N)) then
3188 Error_Msg_N
3189 ("elementary or array type cannot have discriminants",
3190 Defining_Identifier
3191 (First (Discriminant_Specifications (N))));
3192 end if;
3193 end case;
3195 -- Elaborate the type definition according to kind, and generate
3196 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3197 -- already done (this happens during the reanalysis that follows a call
3198 -- to the high level optimizer).
3200 if not Analyzed (T) then
3201 Set_Analyzed (T);
3203 -- Set the SPARK mode from the current context
3205 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3206 Set_SPARK_Pragma_Inherited (T);
3208 case Nkind (Def) is
3209 when N_Access_To_Subprogram_Definition =>
3210 Access_Subprogram_Declaration (T, Def);
3212 -- If this is a remote access to subprogram, we must create the
3213 -- equivalent fat pointer type, and related subprograms.
3215 if Is_Remote then
3216 Process_Remote_AST_Declaration (N);
3217 end if;
3219 -- Validate categorization rule against access type declaration
3220 -- usually a violation in Pure unit, Shared_Passive unit.
3222 Validate_Access_Type_Declaration (T, N);
3224 -- If the type has contracts, we create the corresponding
3225 -- wrapper at once, before analyzing the aspect specifications,
3226 -- so that pre/postconditions can be handled directly on the
3227 -- generated wrapper.
3229 if Ada_Version >= Ada_2022
3230 and then Present (Aspect_Specifications (N))
3231 then
3232 Build_Access_Subprogram_Wrapper (N);
3233 end if;
3235 when N_Access_To_Object_Definition =>
3236 Access_Type_Declaration (T, Def);
3238 -- Validate categorization rule against access type declaration
3239 -- usually a violation in Pure unit, Shared_Passive unit.
3241 Validate_Access_Type_Declaration (T, N);
3243 -- If we are in a Remote_Call_Interface package and define a
3244 -- RACW, then calling stubs and specific stream attributes
3245 -- must be added.
3247 if Is_Remote
3248 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3249 then
3250 Add_RACW_Features (Def_Id);
3251 end if;
3253 when N_Array_Type_Definition =>
3254 Array_Type_Declaration (T, Def);
3256 when N_Derived_Type_Definition =>
3257 Derived_Type_Declaration (T, N, T /= Def_Id);
3259 -- Inherit predicates from parent, and protect against illegal
3260 -- derivations.
3262 if Is_Type (T) and then Has_Predicates (T) then
3263 Set_Has_Predicates (Def_Id);
3264 end if;
3266 -- Save the scenario for examination by the ABE Processing
3267 -- phase.
3269 Record_Elaboration_Scenario (N);
3271 when N_Enumeration_Type_Definition =>
3272 Enumeration_Type_Declaration (T, Def);
3274 when N_Floating_Point_Definition =>
3275 Floating_Point_Type_Declaration (T, Def);
3277 when N_Decimal_Fixed_Point_Definition =>
3278 Decimal_Fixed_Point_Type_Declaration (T, Def);
3280 when N_Ordinary_Fixed_Point_Definition =>
3281 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3283 when N_Signed_Integer_Type_Definition =>
3284 Signed_Integer_Type_Declaration (T, Def);
3286 when N_Modular_Type_Definition =>
3287 Modular_Type_Declaration (T, Def);
3289 when N_Record_Definition =>
3290 Record_Type_Declaration (T, N, Prev);
3292 -- If declaration has a parse error, nothing to elaborate.
3294 when N_Error =>
3295 null;
3297 when others =>
3298 raise Program_Error;
3299 end case;
3300 end if;
3302 if Etype (T) = Any_Type then
3303 return;
3304 end if;
3306 -- Set the primitives list of the full type and its base type when
3307 -- needed. T may be E_Void in cases of earlier errors, and in that
3308 -- case we bypass this.
3310 if Ekind (T) /= E_Void
3311 and then not Present (Direct_Primitive_Operations (T))
3312 then
3313 if Etype (T) = T then
3314 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3316 -- If Etype of T is the base type (as opposed to a parent type) and
3317 -- already has an associated list of primitive operations, then set
3318 -- T's primitive list to the base type's list. Otherwise, create a
3319 -- new empty primitives list and share the list between T and its
3320 -- base type. The lists need to be shared in common between the two.
3322 elsif Etype (T) = Base_Type (T) then
3324 if not Present (Direct_Primitive_Operations (Base_Type (T))) then
3325 Set_Direct_Primitive_Operations
3326 (Base_Type (T), New_Elmt_List);
3327 end if;
3329 Set_Direct_Primitive_Operations
3330 (T, Direct_Primitive_Operations (Base_Type (T)));
3332 -- Case where the Etype is a parent type, so we need a new primitives
3333 -- list for T.
3335 else
3336 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3337 end if;
3338 end if;
3340 -- Some common processing for all types
3342 Set_Depends_On_Private (T, Has_Private_Component (T));
3343 Check_Ops_From_Incomplete_Type;
3345 -- Both the declared entity, and its anonymous base type if one was
3346 -- created, need freeze nodes allocated.
3348 declare
3349 B : constant Entity_Id := Base_Type (T);
3351 begin
3352 -- In the case where the base type differs from the first subtype, we
3353 -- pre-allocate a freeze node, and set the proper link to the first
3354 -- subtype. Freeze_Entity will use this preallocated freeze node when
3355 -- it freezes the entity.
3357 -- This does not apply if the base type is a generic type, whose
3358 -- declaration is independent of the current derived definition.
3360 if B /= T and then not Is_Generic_Type (B) then
3361 Ensure_Freeze_Node (B);
3362 Set_First_Subtype_Link (Freeze_Node (B), T);
3363 end if;
3365 -- A type that is imported through a limited_with clause cannot
3366 -- generate any code, and thus need not be frozen. However, an access
3367 -- type with an imported designated type needs a finalization list,
3368 -- which may be referenced in some other package that has non-limited
3369 -- visibility on the designated type. Thus we must create the
3370 -- finalization list at the point the access type is frozen, to
3371 -- prevent unsatisfied references at link time.
3373 if not From_Limited_With (T) or else Is_Access_Type (T) then
3374 Set_Has_Delayed_Freeze (T);
3375 end if;
3376 end;
3378 -- Case where T is the full declaration of some private type which has
3379 -- been swapped in Defining_Identifier (N).
3381 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3382 Process_Full_View (N, T, Def_Id);
3384 -- Record the reference. The form of this is a little strange, since
3385 -- the full declaration has been swapped in. So the first parameter
3386 -- here represents the entity to which a reference is made which is
3387 -- the "real" entity, i.e. the one swapped in, and the second
3388 -- parameter provides the reference location.
3390 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3391 -- since we don't want a complaint about the full type being an
3392 -- unwanted reference to the private type
3394 declare
3395 B : constant Boolean := Has_Pragma_Unreferenced (T);
3396 begin
3397 Set_Has_Pragma_Unreferenced (T, False);
3398 Generate_Reference (T, T, 'c');
3399 Set_Has_Pragma_Unreferenced (T, B);
3400 end;
3402 Set_Completion_Referenced (Def_Id);
3404 -- For completion of incomplete type, process incomplete dependents
3405 -- and always mark the full type as referenced (it is the incomplete
3406 -- type that we get for any real reference).
3408 elsif Ekind (Prev) = E_Incomplete_Type then
3409 Process_Incomplete_Dependents (N, T, Prev);
3410 Generate_Reference (Prev, Def_Id, 'c');
3411 Set_Completion_Referenced (Def_Id);
3413 -- If not private type or incomplete type completion, this is a real
3414 -- definition of a new entity, so record it.
3416 else
3417 Generate_Definition (Def_Id);
3418 end if;
3420 -- Propagate any pending access types whose finalization masters need to
3421 -- be fully initialized from the partial to the full view. Guard against
3422 -- an illegal full view that remains unanalyzed.
3424 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3425 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3426 end if;
3428 if Chars (Scope (Def_Id)) = Name_System
3429 and then Chars (Def_Id) = Name_Address
3430 and then In_Predefined_Unit (N)
3431 then
3432 Set_Is_Descendant_Of_Address (Def_Id);
3433 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3434 Set_Is_Descendant_Of_Address (Prev);
3435 end if;
3437 Set_Optimize_Alignment_Flags (Def_Id);
3438 Check_Eliminated (Def_Id);
3440 -- If the declaration is a completion and aspects are present, apply
3441 -- them to the entity for the type which is currently the partial
3442 -- view, but which is the one that will be frozen.
3444 if Has_Aspects (N) then
3446 -- In most cases the partial view is a private type, and both views
3447 -- appear in different declarative parts. In the unusual case where
3448 -- the partial view is incomplete, perform the analysis on the
3449 -- full view, to prevent freezing anomalies with the corresponding
3450 -- class-wide type, which otherwise might be frozen before the
3451 -- dispatch table is built.
3453 if Prev /= Def_Id
3454 and then Ekind (Prev) /= E_Incomplete_Type
3455 then
3456 Analyze_Aspect_Specifications (N, Prev);
3458 -- Normal case
3460 else
3461 Analyze_Aspect_Specifications (N, Def_Id);
3462 end if;
3463 end if;
3465 if Is_Derived_Type (Prev)
3466 and then Def_Id /= Prev
3467 then
3468 Check_Nonoverridable_Aspects;
3469 end if;
3470 end Analyze_Full_Type_Declaration;
3472 ----------------------------------
3473 -- Analyze_Incomplete_Type_Decl --
3474 ----------------------------------
3476 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3477 F : constant Boolean := Is_Pure (Current_Scope);
3478 T : Entity_Id;
3480 begin
3481 Generate_Definition (Defining_Identifier (N));
3483 -- Process an incomplete declaration. The identifier must not have been
3484 -- declared already in the scope. However, an incomplete declaration may
3485 -- appear in the private part of a package, for a private type that has
3486 -- already been declared.
3488 -- In this case, the discriminants (if any) must match
3490 T := Find_Type_Name (N);
3492 Mutate_Ekind (T, E_Incomplete_Type);
3493 Set_Etype (T, T);
3494 Set_Is_First_Subtype (T);
3495 Reinit_Size_Align (T);
3497 -- Set the SPARK mode from the current context
3499 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3500 Set_SPARK_Pragma_Inherited (T);
3502 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3503 -- incomplete types.
3505 if Tagged_Present (N) then
3506 Set_Is_Tagged_Type (T, True);
3507 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3508 Make_Class_Wide_Type (T);
3509 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3510 end if;
3512 Set_Stored_Constraint (T, No_Elist);
3514 if Present (Discriminant_Specifications (N)) then
3515 Push_Scope (T);
3516 Process_Discriminants (N);
3517 End_Scope;
3518 end if;
3520 -- If the type has discriminants, nontrivial subtypes may be declared
3521 -- before the full view of the type. The full views of those subtypes
3522 -- will be built after the full view of the type.
3524 Set_Private_Dependents (T, New_Elmt_List);
3525 Set_Is_Pure (T, F);
3526 end Analyze_Incomplete_Type_Decl;
3528 -----------------------------------
3529 -- Analyze_Interface_Declaration --
3530 -----------------------------------
3532 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3533 CW : constant Entity_Id := Class_Wide_Type (T);
3535 begin
3536 Set_Is_Tagged_Type (T);
3537 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3539 Set_Is_Limited_Record (T, Limited_Present (Def)
3540 or else Task_Present (Def)
3541 or else Protected_Present (Def)
3542 or else Synchronized_Present (Def));
3544 -- Type is abstract if full declaration carries keyword, or if previous
3545 -- partial view did.
3547 Set_Is_Abstract_Type (T);
3548 Set_Is_Interface (T);
3550 -- Type is a limited interface if it includes the keyword limited, task,
3551 -- protected, or synchronized.
3553 Set_Is_Limited_Interface
3554 (T, Limited_Present (Def)
3555 or else Protected_Present (Def)
3556 or else Synchronized_Present (Def)
3557 or else Task_Present (Def));
3559 Set_Interfaces (T, New_Elmt_List);
3560 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3562 -- Complete the decoration of the class-wide entity if it was already
3563 -- built (i.e. during the creation of the limited view)
3565 if Present (CW) then
3566 Set_Is_Interface (CW);
3567 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3568 end if;
3570 -- Check runtime support for synchronized interfaces
3572 if Is_Concurrent_Interface (T)
3573 and then not RTE_Available (RE_Select_Specific_Data)
3574 then
3575 Error_Msg_CRT ("synchronized interfaces", T);
3576 end if;
3577 end Analyze_Interface_Declaration;
3579 -----------------------------
3580 -- Analyze_Itype_Reference --
3581 -----------------------------
3583 -- Nothing to do. This node is placed in the tree only for the benefit of
3584 -- back end processing, and has no effect on the semantic processing.
3586 procedure Analyze_Itype_Reference (N : Node_Id) is
3587 begin
3588 pragma Assert (Is_Itype (Itype (N)));
3589 null;
3590 end Analyze_Itype_Reference;
3592 --------------------------------
3593 -- Analyze_Number_Declaration --
3594 --------------------------------
3596 procedure Analyze_Number_Declaration (N : Node_Id) is
3597 E : constant Node_Id := Expression (N);
3598 Id : constant Entity_Id := Defining_Identifier (N);
3599 Index : Interp_Index;
3600 It : Interp;
3601 T : Entity_Id;
3603 begin
3604 Generate_Definition (Id);
3605 Enter_Name (Id);
3607 -- This is an optimization of a common case of an integer literal
3609 if Nkind (E) = N_Integer_Literal then
3610 Set_Is_Static_Expression (E, True);
3611 Set_Etype (E, Universal_Integer);
3613 Set_Etype (Id, Universal_Integer);
3614 Mutate_Ekind (Id, E_Named_Integer);
3615 Set_Is_Frozen (Id, True);
3617 Set_Debug_Info_Needed (Id);
3618 return;
3619 end if;
3621 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3623 -- Process expression, replacing error by integer zero, to avoid
3624 -- cascaded errors or aborts further along in the processing
3626 -- Replace Error by integer zero, which seems least likely to cause
3627 -- cascaded errors.
3629 if E = Error then
3630 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3631 Set_Error_Posted (E);
3632 end if;
3634 Analyze (E);
3636 -- Verify that the expression is static and numeric. If
3637 -- the expression is overloaded, we apply the preference
3638 -- rule that favors root numeric types.
3640 if not Is_Overloaded (E) then
3641 T := Etype (E);
3642 if Has_Dynamic_Predicate_Aspect (T) then
3643 Error_Msg_N
3644 ("subtype has dynamic predicate, "
3645 & "not allowed in number declaration", N);
3646 end if;
3648 else
3649 T := Any_Type;
3651 Get_First_Interp (E, Index, It);
3652 while Present (It.Typ) loop
3653 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3654 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3655 then
3656 if T = Any_Type then
3657 T := It.Typ;
3659 elsif Is_Universal_Numeric_Type (It.Typ) then
3660 -- Choose universal interpretation over any other
3662 T := It.Typ;
3663 exit;
3664 end if;
3665 end if;
3667 Get_Next_Interp (Index, It);
3668 end loop;
3669 end if;
3671 if Is_Integer_Type (T) then
3672 Resolve (E, T);
3673 Set_Etype (Id, Universal_Integer);
3674 Mutate_Ekind (Id, E_Named_Integer);
3676 elsif Is_Real_Type (T) then
3678 -- Because the real value is converted to universal_real, this is a
3679 -- legal context for a universal fixed expression.
3681 if T = Universal_Fixed then
3682 declare
3683 Loc : constant Source_Ptr := Sloc (N);
3684 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3685 Subtype_Mark =>
3686 New_Occurrence_Of (Universal_Real, Loc),
3687 Expression => Relocate_Node (E));
3689 begin
3690 Rewrite (E, Conv);
3691 Analyze (E);
3692 end;
3694 elsif T = Any_Fixed then
3695 Error_Msg_N ("illegal context for mixed mode operation", E);
3697 -- Expression is of the form : universal_fixed * integer. Try to
3698 -- resolve as universal_real.
3700 T := Universal_Real;
3701 Set_Etype (E, T);
3702 end if;
3704 Resolve (E, T);
3705 Set_Etype (Id, Universal_Real);
3706 Mutate_Ekind (Id, E_Named_Real);
3708 else
3709 Wrong_Type (E, Any_Numeric);
3710 Resolve (E, T);
3712 Set_Etype (Id, T);
3713 Mutate_Ekind (Id, E_Constant);
3714 Set_Never_Set_In_Source (Id, True);
3715 Set_Is_True_Constant (Id, True);
3716 return;
3717 end if;
3719 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3720 Set_Etype (E, Etype (Id));
3721 end if;
3723 if not Is_OK_Static_Expression (E) then
3724 Flag_Non_Static_Expr
3725 ("non-static expression used in number declaration!", E);
3726 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3727 Set_Etype (E, Any_Type);
3728 end if;
3730 Analyze_Dimension (N);
3731 end Analyze_Number_Declaration;
3733 --------------------------------
3734 -- Analyze_Object_Declaration --
3735 --------------------------------
3737 -- WARNING: This routine manages Ghost regions. Return statements must be
3738 -- replaced by gotos which jump to the end of the routine and restore the
3739 -- Ghost mode.
3741 procedure Analyze_Object_Declaration (N : Node_Id) is
3742 Loc : constant Source_Ptr := Sloc (N);
3743 Id : constant Entity_Id := Defining_Identifier (N);
3744 Next_Decl : constant Node_Id := Next (N);
3746 Act_T : Entity_Id;
3747 T : Entity_Id;
3749 E : Node_Id := Expression (N);
3750 -- E is set to Expression (N) throughout this routine. When Expression
3751 -- (N) is modified, E is changed accordingly.
3753 procedure Check_Dynamic_Object (Typ : Entity_Id);
3754 -- A library-level object with nonstatic discriminant constraints may
3755 -- require dynamic allocation. The declaration is illegal if the
3756 -- profile includes the restriction No_Implicit_Heap_Allocations.
3758 procedure Check_For_Null_Excluding_Components
3759 (Obj_Typ : Entity_Id;
3760 Obj_Decl : Node_Id);
3761 -- Verify that each null-excluding component of object declaration
3762 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3763 -- a compile-time warning if this is not the case.
3765 function Count_Tasks (T : Entity_Id) return Uint;
3766 -- This function is called when a non-generic library level object of a
3767 -- task type is declared. Its function is to count the static number of
3768 -- tasks declared within the type (it is only called if Has_Task is set
3769 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3770 -- or a variant record type is encountered, Check_Restriction is called
3771 -- indicating the count is unknown.
3773 function Delayed_Aspect_Present return Boolean;
3774 -- If the declaration has an expression that is an aggregate, and it
3775 -- has aspects that require delayed analysis, the resolution of the
3776 -- aggregate must be deferred to the freeze point of the object. This
3777 -- special processing was created for address clauses, but it must
3778 -- also apply to address aspects. This must be done before the aspect
3779 -- specifications are analyzed because we must handle the aggregate
3780 -- before the analysis of the object declaration is complete.
3782 -- Any other relevant delayed aspects on object declarations ???
3784 --------------------------
3785 -- Check_Dynamic_Object --
3786 --------------------------
3788 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3789 Comp : Entity_Id;
3790 Obj_Type : Entity_Id;
3792 begin
3793 Obj_Type := Typ;
3795 if Is_Private_Type (Obj_Type)
3796 and then Present (Full_View (Obj_Type))
3797 then
3798 Obj_Type := Full_View (Obj_Type);
3799 end if;
3801 if Known_Static_Esize (Obj_Type) then
3802 return;
3803 end if;
3805 if Restriction_Active (No_Implicit_Heap_Allocations)
3806 and then Expander_Active
3807 and then Has_Discriminants (Obj_Type)
3808 then
3809 Comp := First_Component (Obj_Type);
3810 while Present (Comp) loop
3811 if Known_Static_Esize (Etype (Comp))
3812 or else Size_Known_At_Compile_Time (Etype (Comp))
3813 then
3814 null;
3816 elsif not Discriminated_Size (Comp)
3817 and then Comes_From_Source (Comp)
3818 then
3819 Error_Msg_NE
3820 ("component& of non-static size will violate restriction "
3821 & "No_Implicit_Heap_Allocation?", N, Comp);
3823 elsif Is_Record_Type (Etype (Comp)) then
3824 Check_Dynamic_Object (Etype (Comp));
3825 end if;
3827 Next_Component (Comp);
3828 end loop;
3829 end if;
3830 end Check_Dynamic_Object;
3832 -----------------------------------------
3833 -- Check_For_Null_Excluding_Components --
3834 -----------------------------------------
3836 procedure Check_For_Null_Excluding_Components
3837 (Obj_Typ : Entity_Id;
3838 Obj_Decl : Node_Id)
3840 procedure Check_Component
3841 (Comp_Typ : Entity_Id;
3842 Comp_Decl : Node_Id := Empty;
3843 Array_Comp : Boolean := False);
3844 -- Apply a compile-time null-exclusion check on a component denoted
3845 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3846 -- subcomponents (if any).
3848 ---------------------
3849 -- Check_Component --
3850 ---------------------
3852 procedure Check_Component
3853 (Comp_Typ : Entity_Id;
3854 Comp_Decl : Node_Id := Empty;
3855 Array_Comp : Boolean := False)
3857 Comp : Entity_Id;
3858 T : Entity_Id;
3860 begin
3861 -- Do not consider internally-generated components or those that
3862 -- are already initialized.
3864 if Present (Comp_Decl)
3865 and then (not Comes_From_Source (Comp_Decl)
3866 or else Present (Expression (Comp_Decl)))
3867 then
3868 return;
3869 end if;
3871 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3872 and then Present (Full_View (Comp_Typ))
3873 then
3874 T := Full_View (Comp_Typ);
3875 else
3876 T := Comp_Typ;
3877 end if;
3879 -- Verify a component of a null-excluding access type
3881 if Is_Access_Type (T)
3882 and then Can_Never_Be_Null (T)
3883 then
3884 if Comp_Decl = Obj_Decl then
3885 Null_Exclusion_Static_Checks
3886 (N => Obj_Decl,
3887 Comp => Empty,
3888 Array_Comp => Array_Comp);
3890 else
3891 Null_Exclusion_Static_Checks
3892 (N => Obj_Decl,
3893 Comp => Comp_Decl,
3894 Array_Comp => Array_Comp);
3895 end if;
3897 -- Check array components
3899 elsif Is_Array_Type (T) then
3901 -- There is no suitable component when the object is of an
3902 -- array type. However, a namable component may appear at some
3903 -- point during the recursive inspection, but not at the top
3904 -- level. At the top level just indicate array component case.
3906 if Comp_Decl = Obj_Decl then
3907 Check_Component (Component_Type (T), Array_Comp => True);
3908 else
3909 Check_Component (Component_Type (T), Comp_Decl);
3910 end if;
3912 -- Verify all components of type T
3914 -- Note: No checks are performed on types with discriminants due
3915 -- to complexities involving variants. ???
3917 elsif (Is_Concurrent_Type (T)
3918 or else Is_Incomplete_Or_Private_Type (T)
3919 or else Is_Record_Type (T))
3920 and then not Has_Discriminants (T)
3921 then
3922 Comp := First_Component (T);
3923 while Present (Comp) loop
3924 Check_Component (Etype (Comp), Parent (Comp));
3926 Next_Component (Comp);
3927 end loop;
3928 end if;
3929 end Check_Component;
3931 -- Start processing for Check_For_Null_Excluding_Components
3933 begin
3934 Check_Component (Obj_Typ, Obj_Decl);
3935 end Check_For_Null_Excluding_Components;
3937 -----------------
3938 -- Count_Tasks --
3939 -----------------
3941 function Count_Tasks (T : Entity_Id) return Uint is
3942 C : Entity_Id;
3943 X : Node_Id;
3944 V : Uint;
3946 begin
3947 if Is_Task_Type (T) then
3948 return Uint_1;
3950 elsif Is_Record_Type (T) then
3951 if Has_Discriminants (T) then
3952 Check_Restriction (Max_Tasks, N);
3953 return Uint_0;
3955 else
3956 V := Uint_0;
3957 C := First_Component (T);
3958 while Present (C) loop
3959 V := V + Count_Tasks (Etype (C));
3960 Next_Component (C);
3961 end loop;
3963 return V;
3964 end if;
3966 elsif Is_Array_Type (T) then
3967 X := First_Index (T);
3968 V := Count_Tasks (Component_Type (T));
3969 while Present (X) loop
3970 C := Etype (X);
3972 if not Is_OK_Static_Subtype (C) then
3973 Check_Restriction (Max_Tasks, N);
3974 return Uint_0;
3975 else
3976 V := V * (UI_Max (Uint_0,
3977 Expr_Value (Type_High_Bound (C)) -
3978 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3979 end if;
3981 Next_Index (X);
3982 end loop;
3984 return V;
3986 else
3987 return Uint_0;
3988 end if;
3989 end Count_Tasks;
3991 ----------------------------
3992 -- Delayed_Aspect_Present --
3993 ----------------------------
3995 function Delayed_Aspect_Present return Boolean is
3996 A : Node_Id;
3997 A_Id : Aspect_Id;
3999 begin
4000 if Present (Aspect_Specifications (N)) then
4001 A := First (Aspect_Specifications (N));
4003 while Present (A) loop
4004 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4006 if A_Id = Aspect_Address then
4008 -- Set flag on object entity, for later processing at
4009 -- the freeze point.
4011 Set_Has_Delayed_Aspects (Id);
4012 return True;
4013 end if;
4015 Next (A);
4016 end loop;
4017 end if;
4019 return False;
4020 end Delayed_Aspect_Present;
4022 -- Local variables
4024 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4025 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4026 -- Save the Ghost-related attributes to restore on exit
4028 Prev_Entity : Entity_Id := Empty;
4029 Related_Id : Entity_Id;
4030 Full_View_Present : Boolean := False;
4032 -- Start of processing for Analyze_Object_Declaration
4034 begin
4035 -- There are three kinds of implicit types generated by an
4036 -- object declaration:
4038 -- 1. Those generated by the original Object Definition
4040 -- 2. Those generated by the Expression
4042 -- 3. Those used to constrain the Object Definition with the
4043 -- expression constraints when the definition is unconstrained.
4045 -- They must be generated in this order to avoid order of elaboration
4046 -- issues. Thus the first step (after entering the name) is to analyze
4047 -- the object definition.
4049 if Constant_Present (N) then
4050 Prev_Entity := Current_Entity_In_Scope (Id);
4052 if Present (Prev_Entity)
4053 and then
4054 -- If the homograph is an implicit subprogram, it is overridden
4055 -- by the current declaration.
4057 ((Is_Overloadable (Prev_Entity)
4058 and then Is_Inherited_Operation (Prev_Entity))
4060 -- The current object is a discriminal generated for an entry
4061 -- family index. Even though the index is a constant, in this
4062 -- particular context there is no true constant redeclaration.
4063 -- Enter_Name will handle the visibility.
4065 or else
4066 (Is_Discriminal (Id)
4067 and then Ekind (Discriminal_Link (Id)) =
4068 E_Entry_Index_Parameter)
4070 -- The current object is the renaming for a generic declared
4071 -- within the instance.
4073 or else
4074 (Ekind (Prev_Entity) = E_Package
4075 and then Nkind (Parent (Prev_Entity)) =
4076 N_Package_Renaming_Declaration
4077 and then not Comes_From_Source (Prev_Entity)
4078 and then
4079 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4081 -- The entity may be a homonym of a private component of the
4082 -- enclosing protected object, for which we create a local
4083 -- renaming declaration. The declaration is legal, even if
4084 -- useless when it just captures that component.
4086 or else
4087 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4088 and then Nkind (Parent (Prev_Entity)) =
4089 N_Object_Renaming_Declaration))
4090 then
4091 Prev_Entity := Empty;
4092 end if;
4093 end if;
4095 if Present (Prev_Entity) then
4097 -- The object declaration is Ghost when it completes a deferred Ghost
4098 -- constant.
4100 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4102 Constant_Redeclaration (Id, N, T);
4104 Generate_Reference (Prev_Entity, Id, 'c');
4105 Set_Completion_Referenced (Id);
4107 if Error_Posted (N) then
4109 -- Type mismatch or illegal redeclaration; do not analyze
4110 -- expression to avoid cascaded errors.
4112 T := Find_Type_Of_Object (Object_Definition (N), N);
4113 Set_Etype (Id, T);
4114 Mutate_Ekind (Id, E_Variable);
4115 goto Leave;
4116 end if;
4118 -- In the normal case, enter identifier at the start to catch premature
4119 -- usage in the initialization expression.
4121 else
4122 Generate_Definition (Id);
4123 Enter_Name (Id);
4125 Mark_Coextensions (N, Object_Definition (N));
4127 T := Find_Type_Of_Object (Object_Definition (N), N);
4129 if Nkind (Object_Definition (N)) = N_Access_Definition
4130 and then Present
4131 (Access_To_Subprogram_Definition (Object_Definition (N)))
4132 and then Protected_Present
4133 (Access_To_Subprogram_Definition (Object_Definition (N)))
4134 then
4135 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4136 end if;
4138 if Error_Posted (Id) then
4139 Set_Etype (Id, T);
4140 Mutate_Ekind (Id, E_Variable);
4141 goto Leave;
4142 end if;
4143 end if;
4145 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4146 -- out some static checks.
4148 if Ada_Version >= Ada_2005 then
4150 -- In case of aggregates we must also take care of the correct
4151 -- initialization of nested aggregates bug this is done at the
4152 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4154 if Can_Never_Be_Null (T) then
4155 if Present (Expression (N))
4156 and then Nkind (Expression (N)) = N_Aggregate
4157 then
4158 null;
4160 elsif Comes_From_Source (Id) then
4161 declare
4162 Save_Typ : constant Entity_Id := Etype (Id);
4163 begin
4164 Set_Etype (Id, T); -- Temp. decoration for static checks
4165 Null_Exclusion_Static_Checks (N);
4166 Set_Etype (Id, Save_Typ);
4167 end;
4168 end if;
4170 -- We might be dealing with an object of a composite type containing
4171 -- null-excluding components without an aggregate, so we must verify
4172 -- that such components have default initialization.
4174 else
4175 Check_For_Null_Excluding_Components (T, N);
4176 end if;
4177 end if;
4179 -- Object is marked pure if it is in a pure scope
4181 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4183 -- If deferred constant, make sure context is appropriate. We detect
4184 -- a deferred constant as a constant declaration with no expression.
4185 -- A deferred constant can appear in a package body if its completion
4186 -- is by means of an interface pragma.
4188 if Constant_Present (N) and then No (E) then
4190 -- A deferred constant may appear in the declarative part of the
4191 -- following constructs:
4193 -- blocks
4194 -- entry bodies
4195 -- extended return statements
4196 -- package specs
4197 -- package bodies
4198 -- subprogram bodies
4199 -- task bodies
4201 -- When declared inside a package spec, a deferred constant must be
4202 -- completed by a full constant declaration or pragma Import. In all
4203 -- other cases, the only proper completion is pragma Import. Extended
4204 -- return statements are flagged as invalid contexts because they do
4205 -- not have a declarative part and so cannot accommodate the pragma.
4207 if Ekind (Current_Scope) = E_Return_Statement then
4208 Error_Msg_N
4209 ("invalid context for deferred constant declaration (RM 7.4)",
4211 Error_Msg_N
4212 ("\declaration requires an initialization expression",
4214 Set_Constant_Present (N, False);
4216 -- In Ada 83, deferred constant must be of private type
4218 elsif not Is_Private_Type (T) then
4219 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4220 Error_Msg_N
4221 ("(Ada 83) deferred constant must be private type", N);
4222 end if;
4223 end if;
4225 -- If not a deferred constant, then the object declaration freezes
4226 -- its type, unless the object is of an anonymous type and has delayed
4227 -- aspects. In that case the type is frozen when the object itself is.
4229 else
4230 Check_Fully_Declared (T, N);
4232 if Has_Delayed_Aspects (Id)
4233 and then Is_Array_Type (T)
4234 and then Is_Itype (T)
4235 then
4236 Set_Has_Delayed_Freeze (T);
4237 else
4238 Freeze_Before (N, T);
4239 end if;
4240 end if;
4242 -- If the object was created by a constrained array definition, then
4243 -- set the link in both the anonymous base type and anonymous subtype
4244 -- that are built to represent the array type to point to the object.
4246 if Nkind (Object_Definition (Declaration_Node (Id))) =
4247 N_Constrained_Array_Definition
4248 then
4249 Set_Related_Array_Object (T, Id);
4250 Set_Related_Array_Object (Base_Type (T), Id);
4251 end if;
4253 -- Check for protected objects not at library level
4255 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4256 Check_Restriction (No_Local_Protected_Objects, Id);
4257 end if;
4259 -- Check for violation of No_Local_Timing_Events
4261 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4262 Check_Restriction (No_Local_Timing_Events, Id);
4263 end if;
4265 -- The actual subtype of the object is the nominal subtype, unless
4266 -- the nominal one is unconstrained and obtained from the expression.
4268 Act_T := T;
4270 if Is_Library_Level_Entity (Id) then
4271 Check_Dynamic_Object (T);
4272 end if;
4274 -- Process initialization expression if present and not in error
4276 if Present (E) and then E /= Error then
4278 -- Generate an error in case of CPP class-wide object initialization.
4279 -- Required because otherwise the expansion of the class-wide
4280 -- assignment would try to use 'size to initialize the object
4281 -- (primitive that is not available in CPP tagged types).
4283 if Is_Class_Wide_Type (Act_T)
4284 and then
4285 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4286 or else
4287 (Present (Full_View (Root_Type (Etype (Act_T))))
4288 and then
4289 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4290 then
4291 Error_Msg_N
4292 ("predefined assignment not available for 'C'P'P tagged types",
4294 end if;
4296 Mark_Coextensions (N, E);
4297 Analyze (E);
4299 -- In case of errors detected in the analysis of the expression,
4300 -- decorate it with the expected type to avoid cascaded errors.
4302 if No (Etype (E)) then
4303 Set_Etype (E, T);
4304 end if;
4306 -- If an initialization expression is present, then we set the
4307 -- Is_True_Constant flag. It will be reset if this is a variable
4308 -- and it is indeed modified.
4310 Set_Is_True_Constant (Id, True);
4312 -- If we are analyzing a constant declaration, set its completion
4313 -- flag after analyzing and resolving the expression.
4315 if Constant_Present (N) then
4316 Set_Has_Completion (Id);
4317 end if;
4319 -- Set type and resolve (type may be overridden later on). Note:
4320 -- Ekind (Id) must still be E_Void at this point so that incorrect
4321 -- early usage within E is properly diagnosed.
4323 Set_Etype (Id, T);
4325 -- If the expression is an aggregate we must look ahead to detect
4326 -- the possible presence of an address clause, and defer resolution
4327 -- and expansion of the aggregate to the freeze point of the entity.
4329 -- This is not always legal because the aggregate may contain other
4330 -- references that need freezing, e.g. references to other entities
4331 -- with address clauses. In any case, when compiling with -gnatI the
4332 -- presence of the address clause must be ignored.
4334 if Comes_From_Source (N)
4335 and then Expander_Active
4336 and then Nkind (E) = N_Aggregate
4337 and then
4338 ((Present (Following_Address_Clause (N))
4339 and then not Ignore_Rep_Clauses)
4340 or else Delayed_Aspect_Present)
4341 then
4342 Set_Etype (E, T);
4344 -- If the aggregate is limited it will be built in place, and its
4345 -- expansion is deferred until the object declaration is expanded.
4347 -- This is also required when generating C code to ensure that an
4348 -- object with an alignment or address clause can be initialized
4349 -- by means of component by component assignments.
4351 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4352 Set_Expansion_Delayed (E);
4353 end if;
4355 else
4356 -- If the expression is a formal that is a "subprogram pointer"
4357 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4358 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4359 -- the corresponding check, as is done for assignments.
4361 if Is_Entity_Name (E)
4362 and then Present (Entity (E))
4363 and then Is_Formal (Entity (E))
4364 and then
4365 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4366 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4367 then
4368 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4369 end if;
4371 Resolve (E, T);
4372 end if;
4374 -- No further action needed if E is a call to an inlined function
4375 -- which returns an unconstrained type and it has been expanded into
4376 -- a procedure call. In that case N has been replaced by an object
4377 -- declaration without initializing expression and it has been
4378 -- analyzed (see Expand_Inlined_Call).
4380 if Back_End_Inlining
4381 and then Expander_Active
4382 and then Nkind (E) = N_Function_Call
4383 and then Nkind (Name (E)) in N_Has_Entity
4384 and then Is_Inlined (Entity (Name (E)))
4385 and then not Is_Constrained (Etype (E))
4386 and then Analyzed (N)
4387 and then No (Expression (N))
4388 then
4389 goto Leave;
4390 end if;
4392 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4393 -- node (which was marked already-analyzed), we need to set the type
4394 -- to something other than Any_Access in order to keep gigi happy.
4396 if Etype (E) = Any_Access then
4397 Set_Etype (E, T);
4398 end if;
4400 -- If the object is an access to variable, the initialization
4401 -- expression cannot be an access to constant.
4403 if Is_Access_Type (T)
4404 and then not Is_Access_Constant (T)
4405 and then Is_Access_Type (Etype (E))
4406 and then Is_Access_Constant (Etype (E))
4407 then
4408 Error_Msg_N
4409 ("access to variable cannot be initialized with an "
4410 & "access-to-constant expression", E);
4411 end if;
4413 if not Assignment_OK (N) then
4414 Check_Initialization (T, E);
4415 end if;
4417 Check_Unset_Reference (E);
4419 -- If this is a variable, then set current value. If this is a
4420 -- declared constant of a scalar type with a static expression,
4421 -- indicate that it is always valid.
4423 if not Constant_Present (N) then
4424 if Compile_Time_Known_Value (E) then
4425 Set_Current_Value (Id, E);
4426 end if;
4428 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4429 Set_Is_Known_Valid (Id);
4431 -- If it is a constant initialized with a valid nonstatic entity,
4432 -- the constant is known valid as well, and can inherit the subtype
4433 -- of the entity if it is a subtype of the given type. This info
4434 -- is preserved on the actual subtype of the constant.
4436 elsif Is_Scalar_Type (T)
4437 and then Is_Entity_Name (E)
4438 and then Is_Known_Valid (Entity (E))
4439 and then In_Subrange_Of (Etype (Entity (E)), T)
4440 then
4441 Set_Is_Known_Valid (Id);
4442 Mutate_Ekind (Id, E_Constant);
4443 Set_Actual_Subtype (Id, Etype (Entity (E)));
4444 end if;
4446 -- Deal with setting of null flags
4448 if Is_Access_Type (T) then
4449 if Known_Non_Null (E) then
4450 Set_Is_Known_Non_Null (Id, True);
4451 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4452 Set_Is_Known_Null (Id, True);
4453 end if;
4454 end if;
4456 -- Check incorrect use of dynamically tagged expressions
4458 if Is_Tagged_Type (T) then
4459 Check_Dynamically_Tagged_Expression
4460 (Expr => E,
4461 Typ => T,
4462 Related_Nod => N);
4463 end if;
4465 Apply_Scalar_Range_Check (E, T);
4466 Apply_Static_Length_Check (E, T);
4468 -- A formal parameter of a specific tagged type whose related
4469 -- subprogram is subject to pragma Extensions_Visible with value
4470 -- "False" cannot be implicitly converted to a class-wide type by
4471 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4472 -- not consider internally generated expressions.
4474 if Is_Class_Wide_Type (T)
4475 and then Comes_From_Source (E)
4476 and then Is_EVF_Expression (E)
4477 then
4478 Error_Msg_N
4479 ("formal parameter cannot be implicitly converted to "
4480 & "class-wide type when Extensions_Visible is False", E);
4481 end if;
4482 end if;
4484 -- If the No_Streams restriction is set, check that the type of the
4485 -- object is not, and does not contain, any subtype derived from
4486 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4487 -- Has_Stream just for efficiency reasons. There is no point in
4488 -- spending time on a Has_Stream check if the restriction is not set.
4490 if Restriction_Check_Required (No_Streams) then
4491 if Has_Stream (T) then
4492 Check_Restriction (No_Streams, N);
4493 end if;
4494 end if;
4496 -- Deal with predicate check before we start to do major rewriting. It
4497 -- is OK to initialize and then check the initialized value, since the
4498 -- object goes out of scope if we get a predicate failure. Note that we
4499 -- do this in the analyzer and not the expander because the analyzer
4500 -- does some substantial rewriting in some cases.
4502 -- We need a predicate check if the type has predicates that are not
4503 -- ignored, and if either there is an initializing expression, or for
4504 -- default initialization when we have at least one case of an explicit
4505 -- default initial value (including via a Default_Value or
4506 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4507 -- an internal declaration whose initialization comes later (as for an
4508 -- aggregate expansion) or a deferred constant.
4509 -- If expression is an aggregate it may be expanded into assignments
4510 -- and the declaration itself is marked with No_Initialization, but
4511 -- the predicate still applies.
4513 if not Suppress_Assignment_Checks (N)
4514 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4515 and then
4516 (not No_Initialization (N)
4517 or else (Present (E) and then Nkind (E) = N_Aggregate))
4518 and then
4519 (Present (E)
4520 or else
4521 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4522 and then not (Constant_Present (N) and then No (E))
4523 then
4524 -- If the type has a static predicate and the expression is known at
4525 -- compile time, see if the expression satisfies the predicate.
4526 -- In the case of a static expression, this must be done even if
4527 -- the predicate is not enabled (as per static expression rules).
4529 if Present (E) then
4530 Check_Expression_Against_Static_Predicate (E, T);
4531 end if;
4533 -- Do not perform further predicate-related checks unless
4534 -- predicates are enabled for the subtype.
4536 if not Predicate_Enabled (T) then
4537 null;
4539 -- If the type is a null record and there is no explicit initial
4540 -- expression, no predicate check applies.
4542 elsif No (E) and then Is_Null_Record_Type (T) then
4543 null;
4545 -- Do not generate a predicate check if the initialization expression
4546 -- is a type conversion because the conversion has been subjected to
4547 -- the same check. This is a small optimization which avoid redundant
4548 -- checks.
4550 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4551 null;
4553 else
4554 -- The check must be inserted after the expanded aggregate
4555 -- expansion code, if any.
4557 declare
4558 Check : constant Node_Id :=
4559 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4561 begin
4562 if No (Next_Decl) then
4563 Append_To (List_Containing (N), Check);
4564 else
4565 Insert_Before (Next_Decl, Check);
4566 end if;
4567 end;
4568 end if;
4569 end if;
4571 -- Case of unconstrained type
4573 if not Is_Definite_Subtype (T) then
4575 -- Nothing to do in deferred constant case
4577 if Constant_Present (N) and then No (E) then
4578 null;
4580 -- Case of no initialization present
4582 elsif No (E) then
4583 if No_Initialization (N) then
4584 null;
4586 elsif Is_Class_Wide_Type (T) then
4587 Error_Msg_N
4588 ("initialization required in class-wide declaration", N);
4590 else
4591 Error_Msg_N
4592 ("unconstrained subtype not allowed (need initialization)",
4593 Object_Definition (N));
4595 if Is_Record_Type (T) and then Has_Discriminants (T) then
4596 Error_Msg_N
4597 ("\provide initial value or explicit discriminant values",
4598 Object_Definition (N));
4600 Error_Msg_NE
4601 ("\or give default discriminant values for type&",
4602 Object_Definition (N), T);
4604 elsif Is_Array_Type (T) then
4605 Error_Msg_N
4606 ("\provide initial value or explicit array bounds",
4607 Object_Definition (N));
4608 end if;
4609 end if;
4611 -- Case of initialization present but in error. Set initial
4612 -- expression as absent (but do not make above complaints).
4614 elsif E = Error then
4615 Set_Expression (N, Empty);
4616 E := Empty;
4618 -- Case of initialization present
4620 else
4621 -- Unconstrained variables not allowed in Ada 83
4623 if Ada_Version = Ada_83
4624 and then not Constant_Present (N)
4625 and then Comes_From_Source (Object_Definition (N))
4626 then
4627 Error_Msg_N
4628 ("(Ada 83) unconstrained variable not allowed",
4629 Object_Definition (N));
4630 end if;
4632 -- Now we constrain the variable from the initializing expression
4634 -- If the expression is an aggregate, it has been expanded into
4635 -- individual assignments. Retrieve the actual type from the
4636 -- expanded construct.
4638 if Is_Array_Type (T)
4639 and then No_Initialization (N)
4640 and then Nkind (Original_Node (E)) = N_Aggregate
4641 then
4642 Act_T := Etype (E);
4644 -- In case of class-wide interface object declarations we delay
4645 -- the generation of the equivalent record type declarations until
4646 -- its expansion because there are cases in they are not required.
4648 elsif Is_Interface (T) then
4649 null;
4651 -- If the type is an unchecked union, no subtype can be built from
4652 -- the expression. Rewrite declaration as a renaming, which the
4653 -- back-end can handle properly. This is a rather unusual case,
4654 -- because most unchecked_union declarations have default values
4655 -- for discriminants and are thus not indefinite.
4657 elsif Is_Unchecked_Union (T) then
4658 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4659 Mutate_Ekind (Id, E_Constant);
4660 else
4661 Mutate_Ekind (Id, E_Variable);
4662 end if;
4664 -- If the expression is an aggregate it contains the required
4665 -- discriminant values but it has not been resolved yet, so do
4666 -- it now, and treat it as the initial expression of an object
4667 -- declaration, rather than a renaming.
4669 if Nkind (E) = N_Aggregate then
4670 Analyze_And_Resolve (E, T);
4672 else
4673 Rewrite (N,
4674 Make_Object_Renaming_Declaration (Loc,
4675 Defining_Identifier => Id,
4676 Subtype_Mark => New_Occurrence_Of (T, Loc),
4677 Name => E));
4679 Set_Renamed_Object (Id, E);
4680 Freeze_Before (N, T);
4681 Set_Is_Frozen (Id);
4682 goto Leave;
4683 end if;
4685 else
4686 -- Ensure that the generated subtype has a unique external name
4687 -- when the related object is public. This guarantees that the
4688 -- subtype and its bounds will not be affected by switches or
4689 -- pragmas that may offset the internal counter due to extra
4690 -- generated code.
4692 if Is_Public (Id) then
4693 Related_Id := Id;
4694 else
4695 Related_Id := Empty;
4696 end if;
4698 -- If the object has an unconstrained array subtype with fixed
4699 -- lower bound, then sliding to that bound may be needed.
4701 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4702 Expand_Sliding_Conversion (E, T);
4703 end if;
4705 Expand_Subtype_From_Expr
4706 (N => N,
4707 Unc_Type => T,
4708 Subtype_Indic => Object_Definition (N),
4709 Exp => E,
4710 Related_Id => Related_Id);
4712 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4713 end if;
4715 -- Propagate attributes to full view when needed
4717 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4719 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4720 then
4721 Full_View_Present := True;
4722 end if;
4724 if Full_View_Present then
4725 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4726 end if;
4728 if Aliased_Present (N) then
4729 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4731 if Full_View_Present then
4732 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4733 end if;
4734 end if;
4736 Freeze_Before (N, Act_T);
4737 Freeze_Before (N, T);
4738 end if;
4740 elsif Is_Array_Type (T)
4741 and then No_Initialization (N)
4742 and then (Nkind (Original_Node (E)) = N_Aggregate
4743 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4744 and then Nkind (Original_Node (Expression
4745 (Original_Node (E)))) = N_Aggregate))
4746 then
4747 if not Is_Entity_Name (Object_Definition (N)) then
4748 Act_T := Etype (E);
4749 Check_Compile_Time_Size (Act_T);
4751 if Aliased_Present (N) then
4752 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4753 end if;
4754 end if;
4756 -- When the given object definition and the aggregate are specified
4757 -- independently, and their lengths might differ do a length check.
4758 -- This cannot happen if the aggregate is of the form (others =>...)
4760 if not Is_Constrained (T) then
4761 null;
4763 elsif Nkind (E) = N_Raise_Constraint_Error then
4765 -- Aggregate is statically illegal. Place back in declaration
4767 Set_Expression (N, E);
4768 Set_No_Initialization (N, False);
4770 elsif T = Etype (E) then
4771 null;
4773 elsif Nkind (E) = N_Aggregate
4774 and then Present (Component_Associations (E))
4775 and then Present (Choice_List (First (Component_Associations (E))))
4776 and then
4777 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4778 N_Others_Choice
4779 then
4780 null;
4782 else
4783 Apply_Length_Check (E, T);
4784 end if;
4786 -- If the type is limited unconstrained with defaulted discriminants and
4787 -- there is no expression, then the object is constrained by the
4788 -- defaults, so it is worthwhile building the corresponding subtype.
4790 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4791 and then not Is_Constrained (T)
4792 and then Has_Discriminants (T)
4793 then
4794 if No (E) then
4795 Act_T := Build_Default_Subtype (T, N);
4796 else
4797 -- Ada 2005: A limited object may be initialized by means of an
4798 -- aggregate. If the type has default discriminants it has an
4799 -- unconstrained nominal type, Its actual subtype will be obtained
4800 -- from the aggregate, and not from the default discriminants.
4802 Act_T := Etype (E);
4803 end if;
4805 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4807 elsif Nkind (E) = N_Function_Call
4808 and then Constant_Present (N)
4809 and then Has_Unconstrained_Elements (Etype (E))
4810 then
4811 -- The back-end has problems with constants of a discriminated type
4812 -- with defaults, if the initial value is a function call. We
4813 -- generate an intermediate temporary that will receive a reference
4814 -- to the result of the call. The initialization expression then
4815 -- becomes a dereference of that temporary.
4817 Remove_Side_Effects (E);
4819 -- If this is a constant declaration of an unconstrained type and
4820 -- the initialization is an aggregate, we can use the subtype of the
4821 -- aggregate for the declared entity because it is immutable.
4823 elsif not Is_Constrained (T)
4824 and then Has_Discriminants (T)
4825 and then Constant_Present (N)
4826 and then not Has_Unchecked_Union (T)
4827 and then Nkind (E) = N_Aggregate
4828 then
4829 Act_T := Etype (E);
4830 end if;
4832 -- Check No_Wide_Characters restriction
4834 Check_Wide_Character_Restriction (T, Object_Definition (N));
4836 -- Indicate this is not set in source. Certainly true for constants, and
4837 -- true for variables so far (will be reset for a variable if and when
4838 -- we encounter a modification in the source).
4840 Set_Never_Set_In_Source (Id);
4842 -- Now establish the proper kind and type of the object
4844 if Ekind (Id) = E_Void then
4845 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
4846 end if;
4848 if Constant_Present (N) then
4849 Mutate_Ekind (Id, E_Constant);
4850 Set_Is_True_Constant (Id);
4852 else
4853 Mutate_Ekind (Id, E_Variable);
4855 -- A variable is set as shared passive if it appears in a shared
4856 -- passive package, and is at the outer level. This is not done for
4857 -- entities generated during expansion, because those are always
4858 -- manipulated locally.
4860 if Is_Shared_Passive (Current_Scope)
4861 and then Is_Library_Level_Entity (Id)
4862 and then Comes_From_Source (Id)
4863 then
4864 Set_Is_Shared_Passive (Id);
4865 Check_Shared_Var (Id, T, N);
4866 end if;
4868 -- Set Has_Initial_Value if initializing expression present. Note
4869 -- that if there is no initializing expression, we leave the state
4870 -- of this flag unchanged (usually it will be False, but notably in
4871 -- the case of exception choice variables, it will already be true).
4873 if Present (E) then
4874 Set_Has_Initial_Value (Id);
4875 end if;
4876 end if;
4878 -- Set the SPARK mode from the current context (may be overwritten later
4879 -- with explicit pragma).
4881 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4882 Set_SPARK_Pragma_Inherited (Id);
4884 -- Preserve relevant elaboration-related attributes of the context which
4885 -- are no longer available or very expensive to recompute once analysis,
4886 -- resolution, and expansion are over.
4888 Mark_Elaboration_Attributes
4889 (N_Id => Id,
4890 Checks => True,
4891 Warnings => True);
4893 -- Initialize alignment and size and capture alignment setting
4895 Reinit_Alignment (Id);
4896 Reinit_Esize (Id);
4897 Set_Optimize_Alignment_Flags (Id);
4899 -- Deal with aliased case
4901 if Aliased_Present (N) then
4902 Set_Is_Aliased (Id);
4904 -- AI12-001: All aliased objects are considered to be specified as
4905 -- independently addressable (RM C.6(8.1/4)).
4907 Set_Is_Independent (Id);
4909 -- If the object is aliased and the type is unconstrained with
4910 -- defaulted discriminants and there is no expression, then the
4911 -- object is constrained by the defaults, so it is worthwhile
4912 -- building the corresponding subtype.
4914 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4915 -- unconstrained, then only establish an actual subtype if the
4916 -- nominal subtype is indefinite. In definite cases the object is
4917 -- unconstrained in Ada 2005.
4919 if No (E)
4920 and then Is_Record_Type (T)
4921 and then not Is_Constrained (T)
4922 and then Has_Discriminants (T)
4923 and then (Ada_Version < Ada_2005
4924 or else not Is_Definite_Subtype (T))
4925 then
4926 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4927 end if;
4928 end if;
4930 -- Now we can set the type of the object
4932 Set_Etype (Id, Act_T);
4934 -- Non-constant object is marked to be treated as volatile if type is
4935 -- volatile and we clear the Current_Value setting that may have been
4936 -- set above. Doing so for constants isn't required and might interfere
4937 -- with possible uses of the object as a static expression in contexts
4938 -- incompatible with volatility (e.g. as a case-statement alternative).
4940 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4941 Set_Treat_As_Volatile (Id);
4942 Set_Current_Value (Id, Empty);
4943 end if;
4945 -- Deal with controlled types
4947 if Has_Controlled_Component (Etype (Id))
4948 or else Is_Controlled (Etype (Id))
4949 then
4950 if not Is_Library_Level_Entity (Id) then
4951 Check_Restriction (No_Nested_Finalization, N);
4952 else
4953 Validate_Controlled_Object (Id);
4954 end if;
4955 end if;
4957 if Has_Task (Etype (Id)) then
4958 Check_Restriction (No_Tasking, N);
4960 -- Deal with counting max tasks
4962 -- Nothing to do if inside a generic
4964 if Inside_A_Generic then
4965 null;
4967 -- If library level entity, then count tasks
4969 elsif Is_Library_Level_Entity (Id) then
4970 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4972 -- If not library level entity, then indicate we don't know max
4973 -- tasks and also check task hierarchy restriction and blocking
4974 -- operation (since starting a task is definitely blocking).
4976 else
4977 Check_Restriction (Max_Tasks, N);
4978 Check_Restriction (No_Task_Hierarchy, N);
4979 Check_Potentially_Blocking_Operation (N);
4980 end if;
4982 -- A rather specialized test. If we see two tasks being declared
4983 -- of the same type in the same object declaration, and the task
4984 -- has an entry with an address clause, we know that program error
4985 -- will be raised at run time since we can't have two tasks with
4986 -- entries at the same address.
4988 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4989 declare
4990 E : Entity_Id;
4992 begin
4993 E := First_Entity (Etype (Id));
4994 while Present (E) loop
4995 if Ekind (E) = E_Entry
4996 and then Present (Get_Attribute_Definition_Clause
4997 (E, Attribute_Address))
4998 then
4999 Error_Msg_Warn := SPARK_Mode /= On;
5000 Error_Msg_N
5001 ("more than one task with same entry address<<", N);
5002 Error_Msg_N ("\Program_Error [<<", N);
5003 Insert_Action (N,
5004 Make_Raise_Program_Error (Loc,
5005 Reason => PE_Duplicated_Entry_Address));
5006 exit;
5007 end if;
5009 Next_Entity (E);
5010 end loop;
5011 end;
5012 end if;
5013 end if;
5015 -- Some simple constant-propagation: if the expression is a constant
5016 -- string initialized with a literal, share the literal. This avoids
5017 -- a run-time copy.
5019 if Present (E)
5020 and then Is_Entity_Name (E)
5021 and then Ekind (Entity (E)) = E_Constant
5022 and then Base_Type (Etype (E)) = Standard_String
5023 then
5024 declare
5025 Val : constant Node_Id := Constant_Value (Entity (E));
5026 begin
5027 if Present (Val) and then Nkind (Val) = N_String_Literal then
5028 Rewrite (E, New_Copy (Val));
5029 end if;
5030 end;
5031 end if;
5033 -- Another optimization: if the nominal subtype is unconstrained and
5034 -- the expression is a function call that returns an unconstrained
5035 -- type, rewrite the declaration as a renaming of the result of the
5036 -- call. The exceptions below are cases where the copy is expected,
5037 -- either by the back end (Aliased case) or by the semantics, as for
5038 -- initializing controlled types or copying tags for class-wide types.
5040 if Present (E)
5041 and then Nkind (E) = N_Explicit_Dereference
5042 and then Nkind (Original_Node (E)) = N_Function_Call
5043 and then not Is_Library_Level_Entity (Id)
5044 and then not Is_Constrained (Underlying_Type (T))
5045 and then not Is_Aliased (Id)
5046 and then not Is_Class_Wide_Type (T)
5047 and then not Is_Controlled (T)
5048 and then not Has_Controlled_Component (Base_Type (T))
5049 and then Expander_Active
5050 then
5051 Rewrite (N,
5052 Make_Object_Renaming_Declaration (Loc,
5053 Defining_Identifier => Id,
5054 Access_Definition => Empty,
5055 Subtype_Mark => New_Occurrence_Of
5056 (Base_Type (Etype (Id)), Loc),
5057 Name => E));
5059 Set_Renamed_Object (Id, E);
5061 -- Force generation of debugging information for the constant and for
5062 -- the renamed function call.
5064 Set_Debug_Info_Needed (Id);
5065 Set_Debug_Info_Needed (Entity (Prefix (E)));
5066 end if;
5068 if Present (Prev_Entity)
5069 and then Is_Frozen (Prev_Entity)
5070 and then not Error_Posted (Id)
5071 then
5072 Error_Msg_N ("full constant declaration appears too late", N);
5073 end if;
5075 Check_Eliminated (Id);
5077 -- Deal with setting In_Private_Part flag if in private part
5079 if Ekind (Scope (Id)) = E_Package
5080 and then In_Private_Part (Scope (Id))
5081 then
5082 Set_In_Private_Part (Id);
5083 end if;
5085 <<Leave>>
5086 -- Initialize the refined state of a variable here because this is a
5087 -- common destination for legal and illegal object declarations.
5089 if Ekind (Id) = E_Variable then
5090 Set_Encapsulating_State (Id, Empty);
5091 end if;
5093 if Has_Aspects (N) then
5094 Analyze_Aspect_Specifications (N, Id);
5095 end if;
5097 Analyze_Dimension (N);
5099 -- Verify whether the object declaration introduces an illegal hidden
5100 -- state within a package subject to a null abstract state.
5102 if Ekind (Id) = E_Variable then
5103 Check_No_Hidden_State (Id);
5104 end if;
5106 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5107 end Analyze_Object_Declaration;
5109 ---------------------------
5110 -- Analyze_Others_Choice --
5111 ---------------------------
5113 -- Nothing to do for the others choice node itself, the semantic analysis
5114 -- of the others choice will occur as part of the processing of the parent
5116 procedure Analyze_Others_Choice (N : Node_Id) is
5117 pragma Warnings (Off, N);
5118 begin
5119 null;
5120 end Analyze_Others_Choice;
5122 -------------------------------------------
5123 -- Analyze_Private_Extension_Declaration --
5124 -------------------------------------------
5126 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5127 Indic : constant Node_Id := Subtype_Indication (N);
5128 T : constant Entity_Id := Defining_Identifier (N);
5129 Iface : Entity_Id;
5130 Iface_Elmt : Elmt_Id;
5131 Parent_Base : Entity_Id;
5132 Parent_Type : Entity_Id;
5134 begin
5135 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5137 if Is_Non_Empty_List (Interface_List (N)) then
5138 declare
5139 Intf : Node_Id;
5140 T : Entity_Id;
5142 begin
5143 Intf := First (Interface_List (N));
5144 while Present (Intf) loop
5145 T := Find_Type_Of_Subtype_Indic (Intf);
5147 Diagnose_Interface (Intf, T);
5148 Next (Intf);
5149 end loop;
5150 end;
5151 end if;
5153 Generate_Definition (T);
5155 -- For other than Ada 2012, just enter the name in the current scope
5157 if Ada_Version < Ada_2012 then
5158 Enter_Name (T);
5160 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5161 -- case of private type that completes an incomplete type.
5163 else
5164 declare
5165 Prev : Entity_Id;
5167 begin
5168 Prev := Find_Type_Name (N);
5170 pragma Assert (Prev = T
5171 or else (Ekind (Prev) = E_Incomplete_Type
5172 and then Present (Full_View (Prev))
5173 and then Full_View (Prev) = T));
5174 end;
5175 end if;
5177 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5178 Parent_Base := Base_Type (Parent_Type);
5180 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5181 Mutate_Ekind (T, Ekind (Parent_Type));
5182 Set_Etype (T, Any_Type);
5183 goto Leave;
5185 elsif not Is_Tagged_Type (Parent_Type) then
5186 Error_Msg_N
5187 ("parent of type extension must be a tagged type", Indic);
5188 goto Leave;
5190 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5191 Error_Msg_N ("premature derivation of incomplete type", Indic);
5192 goto Leave;
5194 elsif Is_Concurrent_Type (Parent_Type) then
5195 Error_Msg_N
5196 ("parent type of a private extension cannot be a synchronized "
5197 & "tagged type (RM 3.9.1 (3/1))", N);
5199 Set_Etype (T, Any_Type);
5200 Mutate_Ekind (T, E_Limited_Private_Type);
5201 Set_Private_Dependents (T, New_Elmt_List);
5202 Set_Error_Posted (T);
5203 goto Leave;
5204 end if;
5206 Check_Wide_Character_Restriction (Parent_Type, Indic);
5208 -- Perhaps the parent type should be changed to the class-wide type's
5209 -- specific type in this case to prevent cascading errors ???
5211 if Is_Class_Wide_Type (Parent_Type) then
5212 Error_Msg_N
5213 ("parent of type extension must not be a class-wide type", Indic);
5214 goto Leave;
5215 end if;
5217 if (not Is_Package_Or_Generic_Package (Current_Scope)
5218 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5219 or else In_Private_Part (Current_Scope)
5220 then
5221 Error_Msg_N ("invalid context for private extension", N);
5222 end if;
5224 -- Set common attributes
5226 Set_Is_Pure (T, Is_Pure (Current_Scope));
5227 Set_Scope (T, Current_Scope);
5228 Mutate_Ekind (T, E_Record_Type_With_Private);
5229 Reinit_Size_Align (T);
5230 Set_Default_SSO (T);
5231 Set_No_Reordering (T, No_Component_Reordering);
5233 Set_Etype (T, Parent_Base);
5234 Propagate_Concurrent_Flags (T, Parent_Base);
5236 Set_Convention (T, Convention (Parent_Type));
5237 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5238 Set_Is_First_Subtype (T);
5239 Make_Class_Wide_Type (T);
5241 -- Set the SPARK mode from the current context
5243 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5244 Set_SPARK_Pragma_Inherited (T);
5246 if Unknown_Discriminants_Present (N) then
5247 Set_Discriminant_Constraint (T, No_Elist);
5248 end if;
5250 Build_Derived_Record_Type (N, Parent_Type, T);
5252 -- A private extension inherits the Default_Initial_Condition pragma
5253 -- coming from any parent type within the derivation chain.
5255 if Has_DIC (Parent_Type) then
5256 Set_Has_Inherited_DIC (T);
5257 end if;
5259 -- A private extension inherits any class-wide invariants coming from a
5260 -- parent type or an interface. Note that the invariant procedure of the
5261 -- parent type should not be inherited because the private extension may
5262 -- define invariants of its own.
5264 if Has_Inherited_Invariants (Parent_Type)
5265 or else Has_Inheritable_Invariants (Parent_Type)
5266 then
5267 Set_Has_Inherited_Invariants (T);
5269 elsif Present (Interfaces (T)) then
5270 Iface_Elmt := First_Elmt (Interfaces (T));
5271 while Present (Iface_Elmt) loop
5272 Iface := Node (Iface_Elmt);
5274 if Has_Inheritable_Invariants (Iface) then
5275 Set_Has_Inherited_Invariants (T);
5276 exit;
5277 end if;
5279 Next_Elmt (Iface_Elmt);
5280 end loop;
5281 end if;
5283 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5284 -- synchronized formal derived type.
5286 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5287 Set_Is_Limited_Record (T);
5289 -- Formal derived type case
5291 if Is_Generic_Type (T) then
5293 -- The parent must be a tagged limited type or a synchronized
5294 -- interface.
5296 if (not Is_Tagged_Type (Parent_Type)
5297 or else not Is_Limited_Type (Parent_Type))
5298 and then
5299 (not Is_Interface (Parent_Type)
5300 or else not Is_Synchronized_Interface (Parent_Type))
5301 then
5302 Error_Msg_NE
5303 ("parent type of & must be tagged limited or synchronized",
5304 N, T);
5305 end if;
5307 -- The progenitors (if any) must be limited or synchronized
5308 -- interfaces.
5310 if Present (Interfaces (T)) then
5311 Iface_Elmt := First_Elmt (Interfaces (T));
5312 while Present (Iface_Elmt) loop
5313 Iface := Node (Iface_Elmt);
5315 if not Is_Limited_Interface (Iface)
5316 and then not Is_Synchronized_Interface (Iface)
5317 then
5318 Error_Msg_NE
5319 ("progenitor & must be limited or synchronized",
5320 N, Iface);
5321 end if;
5323 Next_Elmt (Iface_Elmt);
5324 end loop;
5325 end if;
5327 -- Regular derived extension, the parent must be a limited or
5328 -- synchronized interface.
5330 else
5331 if not Is_Interface (Parent_Type)
5332 or else (not Is_Limited_Interface (Parent_Type)
5333 and then not Is_Synchronized_Interface (Parent_Type))
5334 then
5335 Error_Msg_NE
5336 ("parent type of & must be limited interface", N, T);
5337 end if;
5338 end if;
5340 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5341 -- extension with a synchronized parent must be explicitly declared
5342 -- synchronized, because the full view will be a synchronized type.
5343 -- This must be checked before the check for limited types below,
5344 -- to ensure that types declared limited are not allowed to extend
5345 -- synchronized interfaces.
5347 elsif Is_Interface (Parent_Type)
5348 and then Is_Synchronized_Interface (Parent_Type)
5349 and then not Synchronized_Present (N)
5350 then
5351 Error_Msg_NE
5352 ("private extension of& must be explicitly synchronized",
5353 N, Parent_Type);
5355 elsif Limited_Present (N) then
5356 Set_Is_Limited_Record (T);
5358 if not Is_Limited_Type (Parent_Type)
5359 and then
5360 (not Is_Interface (Parent_Type)
5361 or else not Is_Limited_Interface (Parent_Type))
5362 then
5363 Error_Msg_NE ("parent type& of limited extension must be limited",
5364 N, Parent_Type);
5365 end if;
5366 end if;
5368 -- Remember that its parent type has a private extension. Used to warn
5369 -- on public primitives of the parent type defined after its private
5370 -- extensions (see Check_Dispatching_Operation).
5372 Set_Has_Private_Extension (Parent_Type);
5374 <<Leave>>
5375 if Has_Aspects (N) then
5376 Analyze_Aspect_Specifications (N, T);
5377 end if;
5378 end Analyze_Private_Extension_Declaration;
5380 ---------------------------------
5381 -- Analyze_Subtype_Declaration --
5382 ---------------------------------
5384 procedure Analyze_Subtype_Declaration
5385 (N : Node_Id;
5386 Skip : Boolean := False)
5388 Id : constant Entity_Id := Defining_Identifier (N);
5389 T : Entity_Id;
5391 begin
5392 Generate_Definition (Id);
5393 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5394 Reinit_Size_Align (Id);
5396 -- The following guard condition on Enter_Name is to handle cases where
5397 -- the defining identifier has already been entered into the scope but
5398 -- the declaration as a whole needs to be analyzed.
5400 -- This case in particular happens for derived enumeration types. The
5401 -- derived enumeration type is processed as an inserted enumeration type
5402 -- declaration followed by a rewritten subtype declaration. The defining
5403 -- identifier, however, is entered into the name scope very early in the
5404 -- processing of the original type declaration and therefore needs to be
5405 -- avoided here, when the created subtype declaration is analyzed. (See
5406 -- Build_Derived_Types)
5408 -- This also happens when the full view of a private type is derived
5409 -- type with constraints. In this case the entity has been introduced
5410 -- in the private declaration.
5412 -- Finally this happens in some complex cases when validity checks are
5413 -- enabled, where the same subtype declaration may be analyzed twice.
5414 -- This can happen if the subtype is created by the preanalysis of
5415 -- an attribute tht gives the range of a loop statement, and the loop
5416 -- itself appears within an if_statement that will be rewritten during
5417 -- expansion.
5419 if Skip
5420 or else (Present (Etype (Id))
5421 and then (Is_Private_Type (Etype (Id))
5422 or else Is_Task_Type (Etype (Id))
5423 or else Is_Rewrite_Substitution (N)))
5424 then
5425 null;
5427 elsif Current_Entity (Id) = Id then
5428 null;
5430 else
5431 Enter_Name (Id);
5432 end if;
5434 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5436 -- Class-wide equivalent types of records with unknown discriminants
5437 -- involve the generation of an itype which serves as the private view
5438 -- of a constrained record subtype. In such cases the base type of the
5439 -- current subtype we are processing is the private itype. Use the full
5440 -- of the private itype when decorating various attributes.
5442 if Is_Itype (T)
5443 and then Is_Private_Type (T)
5444 and then Present (Full_View (T))
5445 then
5446 T := Full_View (T);
5447 end if;
5449 -- Inherit common attributes
5451 Set_Is_Volatile (Id, Is_Volatile (T));
5452 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5453 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5454 Set_Convention (Id, Convention (T));
5456 -- If ancestor has predicates then so does the subtype, and in addition
5457 -- we must delay the freeze to properly arrange predicate inheritance.
5459 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5460 -- in which T = ID, so the above tests and assignments do nothing???
5462 if Has_Predicates (T)
5463 or else (Present (Ancestor_Subtype (T))
5464 and then Has_Predicates (Ancestor_Subtype (T)))
5465 then
5466 Set_Has_Predicates (Id);
5467 Set_Has_Delayed_Freeze (Id);
5469 -- Generated subtypes inherit the predicate function from the parent
5470 -- (no aspects to examine on the generated declaration).
5472 if not Comes_From_Source (N) then
5473 Mutate_Ekind (Id, Ekind (T));
5475 if Present (Predicate_Function (Id)) then
5476 null;
5478 elsif Present (Predicate_Function (T)) then
5479 Set_Predicate_Function (Id, Predicate_Function (T));
5481 elsif Present (Ancestor_Subtype (T))
5482 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5483 then
5484 Set_Predicate_Function (Id,
5485 Predicate_Function (Ancestor_Subtype (T)));
5486 end if;
5487 end if;
5488 end if;
5490 -- In the case where there is no constraint given in the subtype
5491 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5492 -- semantic attributes must be established here.
5494 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5495 Set_Etype (Id, Base_Type (T));
5497 case Ekind (T) is
5498 when Array_Kind =>
5499 Mutate_Ekind (Id, E_Array_Subtype);
5500 Copy_Array_Subtype_Attributes (Id, T);
5502 when Decimal_Fixed_Point_Kind =>
5503 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5504 Set_Digits_Value (Id, Digits_Value (T));
5505 Set_Delta_Value (Id, Delta_Value (T));
5506 Set_Scale_Value (Id, Scale_Value (T));
5507 Set_Small_Value (Id, Small_Value (T));
5508 Set_Scalar_Range (Id, Scalar_Range (T));
5509 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5510 Set_Is_Constrained (Id, Is_Constrained (T));
5511 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5512 Copy_RM_Size (To => Id, From => T);
5514 when Enumeration_Kind =>
5515 Mutate_Ekind (Id, E_Enumeration_Subtype);
5516 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5517 Set_Scalar_Range (Id, Scalar_Range (T));
5518 Set_Is_Character_Type (Id, Is_Character_Type (T));
5519 Set_Is_Constrained (Id, Is_Constrained (T));
5520 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5521 Copy_RM_Size (To => Id, From => T);
5523 when Ordinary_Fixed_Point_Kind =>
5524 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5525 Set_Scalar_Range (Id, Scalar_Range (T));
5526 Set_Small_Value (Id, Small_Value (T));
5527 Set_Delta_Value (Id, Delta_Value (T));
5528 Set_Is_Constrained (Id, Is_Constrained (T));
5529 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5530 Copy_RM_Size (To => Id, From => T);
5532 when Float_Kind =>
5533 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5534 Set_Scalar_Range (Id, Scalar_Range (T));
5535 Set_Digits_Value (Id, Digits_Value (T));
5536 Set_Is_Constrained (Id, Is_Constrained (T));
5538 -- If the floating point type has dimensions, these will be
5539 -- inherited subsequently when Analyze_Dimensions is called.
5541 when Signed_Integer_Kind =>
5542 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5543 Set_Scalar_Range (Id, Scalar_Range (T));
5544 Set_Is_Constrained (Id, Is_Constrained (T));
5545 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5546 Copy_RM_Size (To => Id, From => T);
5548 when Modular_Integer_Kind =>
5549 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5550 Set_Scalar_Range (Id, Scalar_Range (T));
5551 Set_Is_Constrained (Id, Is_Constrained (T));
5552 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5553 Copy_RM_Size (To => Id, From => T);
5555 when Class_Wide_Kind =>
5556 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5557 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5558 Set_Cloned_Subtype (Id, T);
5559 Set_Is_Tagged_Type (Id, True);
5560 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5561 Set_Has_Unknown_Discriminants
5562 (Id, True);
5563 Set_No_Tagged_Streams_Pragma
5564 (Id, No_Tagged_Streams_Pragma (T));
5566 if Ekind (T) = E_Class_Wide_Subtype then
5567 Set_Equivalent_Type (Id, Equivalent_Type (T));
5568 end if;
5570 when E_Record_Subtype
5571 | E_Record_Type
5573 Mutate_Ekind (Id, E_Record_Subtype);
5575 -- Subtype declarations introduced for formal type parameters
5576 -- in generic instantiations should inherit the Size value of
5577 -- the type they rename.
5579 if Present (Generic_Parent_Type (N)) then
5580 Copy_RM_Size (To => Id, From => T);
5581 end if;
5583 if Ekind (T) = E_Record_Subtype
5584 and then Present (Cloned_Subtype (T))
5585 then
5586 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5587 else
5588 Set_Cloned_Subtype (Id, T);
5589 end if;
5591 Set_First_Entity (Id, First_Entity (T));
5592 Set_Last_Entity (Id, Last_Entity (T));
5593 Set_Has_Discriminants (Id, Has_Discriminants (T));
5594 Set_Is_Constrained (Id, Is_Constrained (T));
5595 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5596 Set_Has_Implicit_Dereference
5597 (Id, Has_Implicit_Dereference (T));
5598 Set_Has_Unknown_Discriminants
5599 (Id, Has_Unknown_Discriminants (T));
5601 if Has_Discriminants (T) then
5602 Set_Discriminant_Constraint
5603 (Id, Discriminant_Constraint (T));
5604 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5606 elsif Has_Unknown_Discriminants (Id) then
5607 Set_Discriminant_Constraint (Id, No_Elist);
5608 end if;
5610 if Is_Tagged_Type (T) then
5611 Set_Is_Tagged_Type (Id, True);
5612 Set_No_Tagged_Streams_Pragma
5613 (Id, No_Tagged_Streams_Pragma (T));
5614 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5615 Set_Direct_Primitive_Operations
5616 (Id, Direct_Primitive_Operations (T));
5617 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5619 if Is_Interface (T) then
5620 Set_Is_Interface (Id);
5621 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5622 end if;
5623 end if;
5625 when Private_Kind =>
5626 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5627 Set_Has_Discriminants (Id, Has_Discriminants (T));
5628 Set_Is_Constrained (Id, Is_Constrained (T));
5629 Set_First_Entity (Id, First_Entity (T));
5630 Set_Last_Entity (Id, Last_Entity (T));
5631 Set_Private_Dependents (Id, New_Elmt_List);
5632 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5633 Set_Has_Implicit_Dereference
5634 (Id, Has_Implicit_Dereference (T));
5635 Set_Has_Unknown_Discriminants
5636 (Id, Has_Unknown_Discriminants (T));
5637 Set_Known_To_Have_Preelab_Init
5638 (Id, Known_To_Have_Preelab_Init (T));
5640 if Is_Tagged_Type (T) then
5641 Set_Is_Tagged_Type (Id);
5642 Set_No_Tagged_Streams_Pragma (Id,
5643 No_Tagged_Streams_Pragma (T));
5644 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5645 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5646 Set_Direct_Primitive_Operations (Id,
5647 Direct_Primitive_Operations (T));
5648 end if;
5650 -- In general the attributes of the subtype of a private type
5651 -- are the attributes of the partial view of parent. However,
5652 -- the full view may be a discriminated type, and the subtype
5653 -- must share the discriminant constraint to generate correct
5654 -- calls to initialization procedures.
5656 if Has_Discriminants (T) then
5657 Set_Discriminant_Constraint
5658 (Id, Discriminant_Constraint (T));
5659 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5661 elsif Present (Full_View (T))
5662 and then Has_Discriminants (Full_View (T))
5663 then
5664 Set_Discriminant_Constraint
5665 (Id, Discriminant_Constraint (Full_View (T)));
5666 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5668 -- This would seem semantically correct, but apparently
5669 -- generates spurious errors about missing components ???
5671 -- Set_Has_Discriminants (Id);
5672 end if;
5674 Prepare_Private_Subtype_Completion (Id, N);
5676 -- If this is the subtype of a constrained private type with
5677 -- discriminants that has got a full view and we also have
5678 -- built a completion just above, show that the completion
5679 -- is a clone of the full view to the back-end.
5681 if Has_Discriminants (T)
5682 and then not Has_Unknown_Discriminants (T)
5683 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5684 and then Present (Full_View (T))
5685 and then Present (Full_View (Id))
5686 then
5687 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5688 end if;
5690 when Access_Kind =>
5691 Mutate_Ekind (Id, E_Access_Subtype);
5692 Set_Is_Constrained (Id, Is_Constrained (T));
5693 Set_Is_Access_Constant
5694 (Id, Is_Access_Constant (T));
5695 Set_Directly_Designated_Type
5696 (Id, Designated_Type (T));
5697 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5699 -- A Pure library_item must not contain the declaration of a
5700 -- named access type, except within a subprogram, generic
5701 -- subprogram, task unit, or protected unit, or if it has
5702 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5704 if Comes_From_Source (Id)
5705 and then In_Pure_Unit
5706 and then not In_Subprogram_Task_Protected_Unit
5707 and then not No_Pool_Assigned (Id)
5708 then
5709 Error_Msg_N
5710 ("named access types not allowed in pure unit", N);
5711 end if;
5713 when Concurrent_Kind =>
5714 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5715 Set_Corresponding_Record_Type (Id,
5716 Corresponding_Record_Type (T));
5717 Set_First_Entity (Id, First_Entity (T));
5718 Set_First_Private_Entity (Id, First_Private_Entity (T));
5719 Set_Has_Discriminants (Id, Has_Discriminants (T));
5720 Set_Is_Constrained (Id, Is_Constrained (T));
5721 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5722 Set_Last_Entity (Id, Last_Entity (T));
5724 if Is_Tagged_Type (T) then
5725 Set_No_Tagged_Streams_Pragma
5726 (Id, No_Tagged_Streams_Pragma (T));
5727 end if;
5729 if Has_Discriminants (T) then
5730 Set_Discriminant_Constraint
5731 (Id, Discriminant_Constraint (T));
5732 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5733 end if;
5735 when Incomplete_Kind =>
5736 if Ada_Version >= Ada_2005 then
5738 -- In Ada 2005 an incomplete type can be explicitly tagged:
5739 -- propagate indication. Note that we also have to include
5740 -- subtypes for Ada 2012 extended use of incomplete types.
5742 Mutate_Ekind (Id, E_Incomplete_Subtype);
5743 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5744 Set_Private_Dependents (Id, New_Elmt_List);
5746 if Is_Tagged_Type (Id) then
5747 Set_No_Tagged_Streams_Pragma
5748 (Id, No_Tagged_Streams_Pragma (T));
5749 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5750 end if;
5752 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5753 -- incomplete type visible through a limited with clause.
5755 if From_Limited_With (T)
5756 and then Present (Non_Limited_View (T))
5757 then
5758 Set_From_Limited_With (Id);
5759 Set_Non_Limited_View (Id, Non_Limited_View (T));
5761 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5762 -- to the private dependents of the original incomplete
5763 -- type for future transformation.
5765 else
5766 Append_Elmt (Id, Private_Dependents (T));
5767 end if;
5769 -- If the subtype name denotes an incomplete type an error
5770 -- was already reported by Process_Subtype.
5772 else
5773 Set_Etype (Id, Any_Type);
5774 end if;
5776 when others =>
5777 raise Program_Error;
5778 end case;
5780 -- If there is no constraint in the subtype indication, the
5781 -- declared entity inherits predicates from the parent.
5783 Inherit_Predicate_Flags (Id, T);
5784 end if;
5786 -- When prefixed calls are enabled for untagged types, the subtype
5787 -- shares the primitive operations of its base type.
5789 if Extensions_Allowed then
5790 Set_Direct_Primitive_Operations
5791 (Id, Direct_Primitive_Operations (Base_Type (T)));
5792 end if;
5794 if Etype (Id) = Any_Type then
5795 goto Leave;
5796 end if;
5798 -- Some common processing on all types
5800 Set_Size_Info (Id, T);
5801 Set_First_Rep_Item (Id, First_Rep_Item (T));
5803 -- If the parent type is a generic actual, so is the subtype. This may
5804 -- happen in a nested instance. Why Comes_From_Source test???
5806 if not Comes_From_Source (N) then
5807 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5808 end if;
5810 -- If this is a subtype declaration for an actual in an instance,
5811 -- inherit static and dynamic predicates if any.
5813 -- If declaration has no aspect specifications, inherit predicate
5814 -- info as well. Unclear how to handle the case of both specified
5815 -- and inherited predicates ??? Other inherited aspects, such as
5816 -- invariants, should be OK, but the combination with later pragmas
5817 -- may also require special merging.
5819 if Has_Predicates (T)
5820 and then Present (Predicate_Function (T))
5821 and then
5822 ((In_Instance and then not Comes_From_Source (N))
5823 or else No (Aspect_Specifications (N)))
5824 then
5825 -- Inherit Subprograms_For_Type from the full view, if present
5827 if Present (Full_View (T))
5828 and then Subprograms_For_Type (Full_View (T)) /= No_Elist
5829 then
5830 Set_Subprograms_For_Type
5831 (Id, Subprograms_For_Type (Full_View (T)));
5832 else
5833 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5834 end if;
5836 -- If the current declaration created both a private and a full view,
5837 -- then propagate Predicate_Function to the latter as well.
5839 if Present (Full_View (Id))
5840 and then No (Predicate_Function (Full_View (Id)))
5841 then
5842 Set_Subprograms_For_Type
5843 (Full_View (Id), Subprograms_For_Type (Id));
5844 end if;
5846 if Has_Static_Predicate (T) then
5847 Set_Has_Static_Predicate (Id);
5848 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5849 end if;
5850 end if;
5852 -- If the base type is a scalar type, or else if there is no
5853 -- constraint, the atomic flag is inherited by the subtype.
5854 -- Ditto for the Independent aspect.
5856 if Is_Scalar_Type (Id)
5857 or else Is_Entity_Name (Subtype_Indication (N))
5858 then
5859 Set_Is_Atomic (Id, Is_Atomic (T));
5860 Set_Is_Independent (Id, Is_Independent (T));
5861 end if;
5863 -- Remaining processing depends on characteristics of base type
5865 T := Etype (Id);
5867 Set_Is_Immediately_Visible (Id, True);
5868 Set_Depends_On_Private (Id, Has_Private_Component (T));
5869 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5871 if Is_Interface (T) then
5872 Set_Is_Interface (Id);
5873 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5874 end if;
5876 if Present (Generic_Parent_Type (N))
5877 and then
5878 (Nkind (Parent (Generic_Parent_Type (N))) /=
5879 N_Formal_Type_Declaration
5880 or else Nkind (Formal_Type_Definition
5881 (Parent (Generic_Parent_Type (N)))) /=
5882 N_Formal_Private_Type_Definition)
5883 then
5884 if Is_Tagged_Type (Id) then
5886 -- If this is a generic actual subtype for a synchronized type,
5887 -- the primitive operations are those of the corresponding record
5888 -- for which there is a separate subtype declaration.
5890 if Is_Concurrent_Type (Id) then
5891 null;
5892 elsif Is_Class_Wide_Type (Id) then
5893 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5894 else
5895 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5896 end if;
5898 elsif Scope (Etype (Id)) /= Standard_Standard then
5899 Derive_Subprograms (Generic_Parent_Type (N), Id);
5900 end if;
5901 end if;
5903 if Is_Private_Type (T) and then Present (Full_View (T)) then
5904 Conditional_Delay (Id, Full_View (T));
5906 -- The subtypes of components or subcomponents of protected types
5907 -- do not need freeze nodes, which would otherwise appear in the
5908 -- wrong scope (before the freeze node for the protected type). The
5909 -- proper subtypes are those of the subcomponents of the corresponding
5910 -- record.
5912 elsif Ekind (Scope (Id)) /= E_Protected_Type
5913 and then Present (Scope (Scope (Id))) -- error defense
5914 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5915 then
5916 Conditional_Delay (Id, T);
5917 end if;
5919 -- If we have a subtype of an incomplete type whose full type is a
5920 -- derived numeric type, we need to have a freeze node for the subtype.
5921 -- Otherwise gigi will complain while computing the (static) bounds of
5922 -- the subtype.
5924 if Is_Itype (T)
5925 and then Is_Elementary_Type (Id)
5926 and then Etype (Id) /= Id
5927 then
5928 declare
5929 Partial : constant Entity_Id :=
5930 Incomplete_Or_Partial_View (First_Subtype (Id));
5931 begin
5932 if Present (Partial)
5933 and then Ekind (Partial) = E_Incomplete_Type
5934 then
5935 Set_Has_Delayed_Freeze (Id);
5936 end if;
5937 end;
5938 end if;
5940 -- Check that Constraint_Error is raised for a scalar subtype indication
5941 -- when the lower or upper bound of a non-null range lies outside the
5942 -- range of the type mark. Likewise for an array subtype, but check the
5943 -- compatibility for each index.
5945 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5946 declare
5947 Indic_Typ : constant Entity_Id :=
5948 Etype (Subtype_Mark (Subtype_Indication (N)));
5949 Subt_Index : Node_Id;
5950 Target_Index : Node_Id;
5952 begin
5953 if Is_Scalar_Type (Etype (Id))
5954 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
5955 then
5956 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
5958 elsif Is_Array_Type (Etype (Id))
5959 and then Present (First_Index (Id))
5960 then
5961 Subt_Index := First_Index (Id);
5962 Target_Index := First_Index (Indic_Typ);
5964 while Present (Subt_Index) loop
5965 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
5966 and then Is_Scalar_Type (Entity (Subt_Index)))
5967 or else Nkind (Subt_Index) = N_Subtype_Indication)
5968 and then
5969 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5970 then
5971 Apply_Range_Check
5972 (Scalar_Range (Etype (Subt_Index)),
5973 Etype (Target_Index),
5974 Insert_Node => N);
5975 end if;
5977 Next_Index (Subt_Index);
5978 Next_Index (Target_Index);
5979 end loop;
5980 end if;
5981 end;
5982 end if;
5984 Set_Optimize_Alignment_Flags (Id);
5985 Check_Eliminated (Id);
5987 <<Leave>>
5988 if Has_Aspects (N) then
5989 Analyze_Aspect_Specifications (N, Id);
5990 end if;
5992 Analyze_Dimension (N);
5994 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5995 -- indications on composite types where the constraints are dynamic.
5996 -- Note that object declarations and aggregates generate implicit
5997 -- subtype declarations, which this covers. One special case is that the
5998 -- implicitly generated "=" for discriminated types includes an
5999 -- offending subtype declaration, which is harmless, so we ignore it
6000 -- here.
6002 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6003 declare
6004 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6005 begin
6006 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6007 and then not (Is_Internal (Id)
6008 and then Is_TSS (Scope (Id),
6009 TSS_Composite_Equality))
6010 and then not Within_Init_Proc
6011 and then not All_Composite_Constraints_Static (Cstr)
6012 then
6013 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6014 end if;
6015 end;
6016 end if;
6017 end Analyze_Subtype_Declaration;
6019 --------------------------------
6020 -- Analyze_Subtype_Indication --
6021 --------------------------------
6023 procedure Analyze_Subtype_Indication (N : Node_Id) is
6024 T : constant Entity_Id := Subtype_Mark (N);
6025 R : constant Node_Id := Range_Expression (Constraint (N));
6027 begin
6028 Analyze (T);
6030 if R /= Error then
6031 Analyze (R);
6032 Set_Etype (N, Etype (R));
6033 Resolve (R, Entity (T));
6034 else
6035 Set_Error_Posted (R);
6036 Set_Error_Posted (T);
6037 end if;
6038 end Analyze_Subtype_Indication;
6040 --------------------------
6041 -- Analyze_Variant_Part --
6042 --------------------------
6044 procedure Analyze_Variant_Part (N : Node_Id) is
6045 Discr_Name : Node_Id;
6046 Discr_Type : Entity_Id;
6048 procedure Process_Variant (A : Node_Id);
6049 -- Analyze declarations for a single variant
6051 package Analyze_Variant_Choices is
6052 new Generic_Analyze_Choices (Process_Variant);
6053 use Analyze_Variant_Choices;
6055 ---------------------
6056 -- Process_Variant --
6057 ---------------------
6059 procedure Process_Variant (A : Node_Id) is
6060 CL : constant Node_Id := Component_List (A);
6061 begin
6062 if not Null_Present (CL) then
6063 Analyze_Declarations (Component_Items (CL));
6065 if Present (Variant_Part (CL)) then
6066 Analyze (Variant_Part (CL));
6067 end if;
6068 end if;
6069 end Process_Variant;
6071 -- Start of processing for Analyze_Variant_Part
6073 begin
6074 Discr_Name := Name (N);
6075 Analyze (Discr_Name);
6077 -- If Discr_Name bad, get out (prevent cascaded errors)
6079 if Etype (Discr_Name) = Any_Type then
6080 return;
6081 end if;
6083 -- Check invalid discriminant in variant part
6085 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6086 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6087 end if;
6089 Discr_Type := Etype (Entity (Discr_Name));
6091 if not Is_Discrete_Type (Discr_Type) then
6092 Error_Msg_N
6093 ("discriminant in a variant part must be of a discrete type",
6094 Name (N));
6095 return;
6096 end if;
6098 -- Now analyze the choices, which also analyzes the declarations that
6099 -- are associated with each choice.
6101 Analyze_Choices (Variants (N), Discr_Type);
6103 -- Note: we used to instantiate and call Check_Choices here to check
6104 -- that the choices covered the discriminant, but it's too early to do
6105 -- that because of statically predicated subtypes, whose analysis may
6106 -- be deferred to their freeze point which may be as late as the freeze
6107 -- point of the containing record. So this call is now to be found in
6108 -- Freeze_Record_Declaration.
6110 end Analyze_Variant_Part;
6112 ----------------------------
6113 -- Array_Type_Declaration --
6114 ----------------------------
6116 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6117 Component_Def : constant Node_Id := Component_Definition (Def);
6118 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6119 P : constant Node_Id := Parent (Def);
6120 Element_Type : Entity_Id;
6121 Implicit_Base : Entity_Id;
6122 Index : Node_Id;
6123 Nb_Index : Pos;
6124 Priv : Entity_Id;
6125 Related_Id : Entity_Id;
6126 Has_FLB_Index : Boolean := False;
6128 begin
6129 if Nkind (Def) = N_Constrained_Array_Definition then
6130 Index := First (Discrete_Subtype_Definitions (Def));
6131 else
6132 Index := First (Subtype_Marks (Def));
6133 end if;
6135 -- Find proper names for the implicit types which may be public. In case
6136 -- of anonymous arrays we use the name of the first object of that type
6137 -- as prefix.
6139 if No (T) then
6140 Related_Id := Defining_Identifier (P);
6141 else
6142 Related_Id := T;
6143 end if;
6145 Nb_Index := 1;
6146 while Present (Index) loop
6147 Analyze (Index);
6149 -- Test for odd case of trying to index a type by the type itself
6151 if Is_Entity_Name (Index) and then Entity (Index) = T then
6152 Error_Msg_N ("type& cannot be indexed by itself", Index);
6153 Set_Entity (Index, Standard_Boolean);
6154 Set_Etype (Index, Standard_Boolean);
6155 end if;
6157 -- Add a subtype declaration for each index of private array type
6158 -- declaration whose type is also private. For example:
6160 -- package Pkg is
6161 -- type Index is private;
6162 -- private
6163 -- type Table is array (Index) of ...
6164 -- end;
6166 -- This is currently required by the expander for the internally
6167 -- generated equality subprogram of records with variant parts in
6168 -- which the type of some component is such a private type. And it
6169 -- also helps semantic analysis in peculiar cases where the array
6170 -- type is referenced from an instance but not the index directly.
6172 if Is_Package_Or_Generic_Package (Current_Scope)
6173 and then In_Private_Part (Current_Scope)
6174 and then Has_Private_Declaration (Etype (Index))
6175 and then Scope (Etype (Index)) = Current_Scope
6176 then
6177 declare
6178 Loc : constant Source_Ptr := Sloc (Def);
6179 Decl : Node_Id;
6180 New_E : Entity_Id;
6182 begin
6183 New_E := Make_Temporary (Loc, 'T');
6184 Set_Is_Internal (New_E);
6186 Decl :=
6187 Make_Subtype_Declaration (Loc,
6188 Defining_Identifier => New_E,
6189 Subtype_Indication =>
6190 New_Occurrence_Of (Etype (Index), Loc));
6192 Insert_Before (Parent (Def), Decl);
6193 Analyze (Decl);
6194 Set_Etype (Index, New_E);
6196 -- If the index is a range or a subtype indication it carries
6197 -- no entity. Example:
6199 -- package Pkg is
6200 -- type T is private;
6201 -- private
6202 -- type T is new Natural;
6203 -- Table : array (T(1) .. T(10)) of Boolean;
6204 -- end Pkg;
6206 -- Otherwise the type of the reference is its entity.
6208 if Is_Entity_Name (Index) then
6209 Set_Entity (Index, New_E);
6210 end if;
6211 end;
6212 end if;
6214 Make_Index (Index, P, Related_Id, Nb_Index);
6216 -- In the case where we have an unconstrained array with an index
6217 -- given by a subtype_indication, this is necessarily a "fixed lower
6218 -- bound" index. We change the upper bound of that index to the upper
6219 -- bound of the index's subtype (denoted by the subtype_mark), since
6220 -- that upper bound was originally set by the parser to be the same
6221 -- as the lower bound. In truth, that upper bound corresponds to
6222 -- a box ("<>"), and could be set to Empty, but it's convenient to
6223 -- set it to the upper bound to avoid needing to add special tests
6224 -- in various places for an Empty upper bound, and in any case that
6225 -- accurately characterizes the index's range of values.
6227 if Nkind (Def) = N_Unconstrained_Array_Definition
6228 and then Nkind (Index) = N_Subtype_Indication
6229 then
6230 declare
6231 Index_Subtype_High_Bound : constant Entity_Id :=
6232 Type_High_Bound (Entity (Subtype_Mark (Index)));
6233 begin
6234 Set_High_Bound (Range_Expression (Constraint (Index)),
6235 Index_Subtype_High_Bound);
6237 -- Record that the array type has one or more indexes with
6238 -- a fixed lower bound.
6240 Has_FLB_Index := True;
6242 -- Mark the index as belonging to an array type with a fixed
6243 -- lower bound.
6245 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6246 end;
6247 end if;
6249 -- Check error of subtype with predicate for index type
6251 Bad_Predicated_Subtype_Use
6252 ("subtype& has predicate, not allowed as index subtype",
6253 Index, Etype (Index));
6255 -- Move to next index
6257 Next (Index);
6258 Nb_Index := Nb_Index + 1;
6259 end loop;
6261 -- Process subtype indication if one is present
6263 if Present (Component_Typ) then
6264 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6265 Set_Etype (Component_Typ, Element_Type);
6267 -- Ada 2005 (AI-230): Access Definition case
6269 else pragma Assert (Present (Access_Definition (Component_Def)));
6271 -- Indicate that the anonymous access type is created by the
6272 -- array type declaration.
6274 Element_Type := Access_Definition
6275 (Related_Nod => P,
6276 N => Access_Definition (Component_Def));
6277 Set_Is_Local_Anonymous_Access (Element_Type);
6279 -- Propagate the parent. This field is needed if we have to generate
6280 -- the master_id associated with an anonymous access to task type
6281 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6283 Copy_Parent (To => Element_Type, From => T);
6285 -- Ada 2005 (AI-230): In case of components that are anonymous access
6286 -- types the level of accessibility depends on the enclosing type
6287 -- declaration
6289 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6291 -- Ada 2005 (AI-254)
6293 declare
6294 CD : constant Node_Id :=
6295 Access_To_Subprogram_Definition
6296 (Access_Definition (Component_Def));
6297 begin
6298 if Present (CD) and then Protected_Present (CD) then
6299 Element_Type :=
6300 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6301 end if;
6302 end;
6303 end if;
6305 -- Constrained array case
6307 if No (T) then
6308 -- We might be creating more than one itype with the same Related_Id,
6309 -- e.g. for an array object definition and its initial value. Give
6310 -- them unique suffixes, because GNATprove require distinct types to
6311 -- have different names.
6313 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6314 end if;
6316 if Nkind (Def) = N_Constrained_Array_Definition then
6318 if Ekind (T) in Incomplete_Or_Private_Kind then
6319 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6320 else
6321 pragma Assert (Ekind (T) = E_Void);
6322 end if;
6324 -- Establish Implicit_Base as unconstrained base type
6326 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6328 Set_Etype (Implicit_Base, Implicit_Base);
6329 Set_Scope (Implicit_Base, Current_Scope);
6330 Set_Has_Delayed_Freeze (Implicit_Base);
6331 Set_Default_SSO (Implicit_Base);
6333 -- The constrained array type is a subtype of the unconstrained one
6335 Mutate_Ekind (T, E_Array_Subtype);
6336 Reinit_Size_Align (T);
6337 Set_Etype (T, Implicit_Base);
6338 Set_Scope (T, Current_Scope);
6339 Set_Is_Constrained (T);
6340 Set_First_Index (T,
6341 First (Discrete_Subtype_Definitions (Def)));
6342 Set_Has_Delayed_Freeze (T);
6344 -- Complete setup of implicit base type
6346 pragma Assert (not Known_Component_Size (Implicit_Base));
6347 Set_Component_Type (Implicit_Base, Element_Type);
6348 Set_Finalize_Storage_Only
6349 (Implicit_Base,
6350 Finalize_Storage_Only (Element_Type));
6351 Set_First_Index (Implicit_Base, First_Index (T));
6352 Set_Has_Controlled_Component
6353 (Implicit_Base,
6354 Has_Controlled_Component (Element_Type)
6355 or else Is_Controlled (Element_Type));
6356 Set_Packed_Array_Impl_Type
6357 (Implicit_Base, Empty);
6359 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6361 -- Unconstrained array case
6363 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6365 if Ekind (T) in Incomplete_Or_Private_Kind then
6366 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6367 else
6368 pragma Assert (Ekind (T) = E_Void);
6369 end if;
6371 Mutate_Ekind (T, E_Array_Type);
6372 Reinit_Size_Align (T);
6373 Set_Etype (T, T);
6374 Set_Scope (T, Current_Scope);
6375 pragma Assert (not Known_Component_Size (T));
6376 Set_Is_Constrained (T, False);
6377 Set_Is_Fixed_Lower_Bound_Array_Subtype
6378 (T, Has_FLB_Index);
6379 Set_First_Index (T, First (Subtype_Marks (Def)));
6380 Set_Has_Delayed_Freeze (T, True);
6381 Propagate_Concurrent_Flags (T, Element_Type);
6382 Set_Has_Controlled_Component (T, Has_Controlled_Component
6383 (Element_Type)
6384 or else
6385 Is_Controlled (Element_Type));
6386 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6387 (Element_Type));
6388 Set_Default_SSO (T);
6389 end if;
6391 -- Common attributes for both cases
6393 Set_Component_Type (Base_Type (T), Element_Type);
6394 Set_Packed_Array_Impl_Type (T, Empty);
6396 if Aliased_Present (Component_Definition (Def)) then
6397 Set_Has_Aliased_Components (Etype (T));
6399 -- AI12-001: All aliased objects are considered to be specified as
6400 -- independently addressable (RM C.6(8.1/4)).
6402 Set_Has_Independent_Components (Etype (T));
6403 end if;
6405 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6406 -- array type to ensure that objects of this type are initialized.
6408 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6409 Set_Can_Never_Be_Null (T);
6411 if Null_Exclusion_Present (Component_Definition (Def))
6413 -- No need to check itypes because in their case this check was
6414 -- done at their point of creation
6416 and then not Is_Itype (Element_Type)
6417 then
6418 Error_Msg_N
6419 ("`NOT NULL` not allowed (null already excluded)",
6420 Subtype_Indication (Component_Definition (Def)));
6421 end if;
6422 end if;
6424 Priv := Private_Component (Element_Type);
6426 if Present (Priv) then
6428 -- Check for circular definitions
6430 if Priv = Any_Type then
6431 Set_Component_Type (Etype (T), Any_Type);
6433 -- There is a gap in the visibility of operations on the composite
6434 -- type only if the component type is defined in a different scope.
6436 elsif Scope (Priv) = Current_Scope then
6437 null;
6439 elsif Is_Limited_Type (Priv) then
6440 Set_Is_Limited_Composite (Etype (T));
6441 Set_Is_Limited_Composite (T);
6442 else
6443 Set_Is_Private_Composite (Etype (T));
6444 Set_Is_Private_Composite (T);
6445 end if;
6446 end if;
6448 -- A syntax error in the declaration itself may lead to an empty index
6449 -- list, in which case do a minimal patch.
6451 if No (First_Index (T)) then
6452 Error_Msg_N ("missing index definition in array type declaration", T);
6454 declare
6455 Indexes : constant List_Id :=
6456 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6457 begin
6458 Set_Discrete_Subtype_Definitions (Def, Indexes);
6459 Set_First_Index (T, First (Indexes));
6460 return;
6461 end;
6462 end if;
6464 -- Create a concatenation operator for the new type. Internal array
6465 -- types created for packed entities do not need such, they are
6466 -- compatible with the user-defined type.
6468 if Number_Dimensions (T) = 1
6469 and then not Is_Packed_Array_Impl_Type (T)
6470 then
6471 New_Concatenation_Op (T);
6472 end if;
6474 -- In the case of an unconstrained array the parser has already verified
6475 -- that all the indexes are unconstrained but we still need to make sure
6476 -- that the element type is constrained.
6478 if not Is_Definite_Subtype (Element_Type) then
6479 Error_Msg_N
6480 ("unconstrained element type in array declaration",
6481 Subtype_Indication (Component_Def));
6483 elsif Is_Abstract_Type (Element_Type) then
6484 Error_Msg_N
6485 ("the type of a component cannot be abstract",
6486 Subtype_Indication (Component_Def));
6487 end if;
6489 -- There may be an invariant declared for the component type, but
6490 -- the construction of the component invariant checking procedure
6491 -- takes place during expansion.
6492 end Array_Type_Declaration;
6494 ------------------------------------------------------
6495 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6496 ------------------------------------------------------
6498 function Replace_Anonymous_Access_To_Protected_Subprogram
6499 (N : Node_Id) return Entity_Id
6501 Loc : constant Source_Ptr := Sloc (N);
6503 Curr_Scope : constant Scope_Stack_Entry :=
6504 Scope_Stack.Table (Scope_Stack.Last);
6506 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6508 Acc : Node_Id;
6509 -- Access definition in declaration
6511 Comp : Node_Id;
6512 -- Object definition or formal definition with an access definition
6514 Decl : Node_Id;
6515 -- Declaration of anonymous access to subprogram type
6517 Spec : Node_Id;
6518 -- Original specification in access to subprogram
6520 P : Node_Id;
6522 begin
6523 Set_Is_Internal (Anon);
6525 case Nkind (N) is
6526 when N_Constrained_Array_Definition
6527 | N_Component_Declaration
6528 | N_Unconstrained_Array_Definition
6530 Comp := Component_Definition (N);
6531 Acc := Access_Definition (Comp);
6533 when N_Discriminant_Specification =>
6534 Comp := Discriminant_Type (N);
6535 Acc := Comp;
6537 when N_Parameter_Specification =>
6538 Comp := Parameter_Type (N);
6539 Acc := Comp;
6541 when N_Access_Function_Definition =>
6542 Comp := Result_Definition (N);
6543 Acc := Comp;
6545 when N_Object_Declaration =>
6546 Comp := Object_Definition (N);
6547 Acc := Comp;
6549 when N_Function_Specification =>
6550 Comp := Result_Definition (N);
6551 Acc := Comp;
6553 when others =>
6554 raise Program_Error;
6555 end case;
6557 Spec := Access_To_Subprogram_Definition (Acc);
6559 Decl :=
6560 Make_Full_Type_Declaration (Loc,
6561 Defining_Identifier => Anon,
6562 Type_Definition => Copy_Separate_Tree (Spec));
6564 Mark_Rewrite_Insertion (Decl);
6566 -- Insert the new declaration in the nearest enclosing scope. If the
6567 -- parent is a body and N is its return type, the declaration belongs
6568 -- in the enclosing scope. Likewise if N is the type of a parameter.
6570 P := Parent (N);
6572 if Nkind (N) = N_Function_Specification
6573 and then Nkind (P) = N_Subprogram_Body
6574 then
6575 P := Parent (P);
6576 elsif Nkind (N) = N_Parameter_Specification
6577 and then Nkind (P) in N_Subprogram_Specification
6578 and then Nkind (Parent (P)) = N_Subprogram_Body
6579 then
6580 P := Parent (Parent (P));
6581 end if;
6583 while Present (P) and then not Has_Declarations (P) loop
6584 P := Parent (P);
6585 end loop;
6587 pragma Assert (Present (P));
6589 if Nkind (P) = N_Package_Specification then
6590 Prepend (Decl, Visible_Declarations (P));
6591 else
6592 Prepend (Decl, Declarations (P));
6593 end if;
6595 -- Replace the anonymous type with an occurrence of the new declaration.
6596 -- In all cases the rewritten node does not have the null-exclusion
6597 -- attribute because (if present) it was already inherited by the
6598 -- anonymous entity (Anon). Thus, in case of components we do not
6599 -- inherit this attribute.
6601 if Nkind (N) = N_Parameter_Specification then
6602 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6603 Set_Etype (Defining_Identifier (N), Anon);
6604 Set_Null_Exclusion_Present (N, False);
6606 elsif Nkind (N) = N_Object_Declaration then
6607 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6608 Set_Etype (Defining_Identifier (N), Anon);
6610 elsif Nkind (N) = N_Access_Function_Definition then
6611 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6613 elsif Nkind (N) = N_Function_Specification then
6614 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6615 Set_Etype (Defining_Unit_Name (N), Anon);
6617 else
6618 Rewrite (Comp,
6619 Make_Component_Definition (Loc,
6620 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6621 end if;
6623 Mark_Rewrite_Insertion (Comp);
6625 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6626 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6627 and then not Is_Type (Current_Scope))
6628 then
6630 -- Declaration can be analyzed in the current scope.
6632 Analyze (Decl);
6634 else
6635 -- Temporarily remove the current scope (record or subprogram) from
6636 -- the stack to add the new declarations to the enclosing scope.
6637 -- The anonymous entity is an Itype with the proper attributes.
6639 Scope_Stack.Decrement_Last;
6640 Analyze (Decl);
6641 Set_Is_Itype (Anon);
6642 Set_Associated_Node_For_Itype (Anon, N);
6643 Scope_Stack.Append (Curr_Scope);
6644 end if;
6646 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6647 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6648 return Anon;
6649 end Replace_Anonymous_Access_To_Protected_Subprogram;
6651 -------------------------------------
6652 -- Build_Access_Subprogram_Wrapper --
6653 -------------------------------------
6655 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6656 Loc : constant Source_Ptr := Sloc (Decl);
6657 Id : constant Entity_Id := Defining_Identifier (Decl);
6658 Type_Def : constant Node_Id := Type_Definition (Decl);
6659 Specs : constant List_Id :=
6660 Parameter_Specifications (Type_Def);
6661 Profile : constant List_Id := New_List;
6662 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6664 Contracts : constant List_Id := New_List;
6665 Form_P : Node_Id;
6666 New_P : Node_Id;
6667 New_Decl : Node_Id;
6668 Spec : Node_Id;
6670 procedure Replace_Type_Name (Expr : Node_Id);
6671 -- In the expressions for contract aspects, replace occurrences of the
6672 -- access type with the name of the subprogram entity, as needed, e.g.
6673 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6674 -- remain on the original access type declaration. What about expanded
6675 -- names denoting formals, whose prefix in source is the type name ???
6677 -----------------------
6678 -- Replace_Type_Name --
6679 -----------------------
6681 procedure Replace_Type_Name (Expr : Node_Id) is
6682 function Process (N : Node_Id) return Traverse_Result;
6683 function Process (N : Node_Id) return Traverse_Result is
6684 begin
6685 if Nkind (N) = N_Attribute_Reference
6686 and then Is_Entity_Name (Prefix (N))
6687 and then Chars (Prefix (N)) = Chars (Id)
6688 then
6689 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6690 end if;
6692 return OK;
6693 end Process;
6695 procedure Traverse is new Traverse_Proc (Process);
6696 begin
6697 Traverse (Expr);
6698 end Replace_Type_Name;
6700 begin
6701 if Ekind (Id) in E_Access_Subprogram_Type
6702 | E_Access_Protected_Subprogram_Type
6703 | E_Anonymous_Access_Protected_Subprogram_Type
6704 | E_Anonymous_Access_Subprogram_Type
6705 then
6706 null;
6708 else
6709 Error_Msg_N
6710 ("illegal pre/postcondition on access type", Decl);
6711 return;
6712 end if;
6714 declare
6715 Asp : Node_Id;
6716 A_Id : Aspect_Id;
6717 Cond : Node_Id;
6718 Expr : Node_Id;
6720 begin
6721 Asp := First (Aspect_Specifications (Decl));
6722 while Present (Asp) loop
6723 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6724 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6725 Cond := Asp;
6726 Expr := Expression (Cond);
6727 Replace_Type_Name (Expr);
6728 Next (Asp);
6730 Remove (Cond);
6731 Append (Cond, Contracts);
6733 else
6734 Next (Asp);
6735 end if;
6736 end loop;
6737 end;
6739 -- If there are no contract aspects, no need for a wrapper.
6741 if Is_Empty_List (Contracts) then
6742 return;
6743 end if;
6745 Form_P := First (Specs);
6747 while Present (Form_P) loop
6748 New_P := New_Copy_Tree (Form_P);
6749 Set_Defining_Identifier (New_P,
6750 Make_Defining_Identifier
6751 (Loc, Chars (Defining_Identifier (Form_P))));
6752 Append (New_P, Profile);
6753 Next (Form_P);
6754 end loop;
6756 -- Add to parameter specifications the access parameter that is passed
6757 -- in from an indirect call.
6759 Append (
6760 Make_Parameter_Specification (Loc,
6761 Defining_Identifier => Make_Temporary (Loc, 'P'),
6762 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6763 Profile);
6765 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6766 Spec :=
6767 Make_Procedure_Specification (Loc,
6768 Defining_Unit_Name => Subp,
6769 Parameter_Specifications => Profile);
6770 Mutate_Ekind (Subp, E_Procedure);
6771 else
6772 Spec :=
6773 Make_Function_Specification (Loc,
6774 Defining_Unit_Name => Subp,
6775 Parameter_Specifications => Profile,
6776 Result_Definition =>
6777 New_Copy_Tree
6778 (Result_Definition (Type_Definition (Decl))));
6779 Mutate_Ekind (Subp, E_Function);
6780 end if;
6782 New_Decl :=
6783 Make_Subprogram_Declaration (Loc, Specification => Spec);
6784 Set_Aspect_Specifications (New_Decl, Contracts);
6785 Set_Is_Wrapper (Subp);
6787 -- The wrapper is declared in the freezing actions to facilitate its
6788 -- identification and thus avoid handling it as a primitive operation
6789 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
6790 -- may be handled as a dispatching operation and erroneously registered
6791 -- in a dispatch table.
6793 if not GNATprove_Mode then
6794 Ensure_Freeze_Node (Id);
6795 Append_Freeze_Actions (Id, New_List (New_Decl));
6797 -- Under GNATprove mode there is no such problem but we do not declare
6798 -- it in the freezing actions since they are not analyzed under this
6799 -- mode.
6801 else
6802 Insert_After (Decl, New_Decl);
6803 end if;
6805 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6806 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6807 end Build_Access_Subprogram_Wrapper;
6809 -------------------------------
6810 -- Build_Derived_Access_Type --
6811 -------------------------------
6813 procedure Build_Derived_Access_Type
6814 (N : Node_Id;
6815 Parent_Type : Entity_Id;
6816 Derived_Type : Entity_Id)
6818 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6820 Desig_Type : Entity_Id;
6821 Discr : Entity_Id;
6822 Discr_Con_Elist : Elist_Id;
6823 Discr_Con_El : Elmt_Id;
6824 Subt : Entity_Id;
6826 begin
6827 -- Set the designated type so it is available in case this is an access
6828 -- to a self-referential type, e.g. a standard list type with a next
6829 -- pointer. Will be reset after subtype is built.
6831 Set_Directly_Designated_Type
6832 (Derived_Type, Designated_Type (Parent_Type));
6834 Subt := Process_Subtype (S, N);
6836 if Nkind (S) /= N_Subtype_Indication
6837 and then Subt /= Base_Type (Subt)
6838 then
6839 Mutate_Ekind (Derived_Type, E_Access_Subtype);
6840 end if;
6842 if Ekind (Derived_Type) = E_Access_Subtype then
6843 declare
6844 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6845 Ibase : constant Entity_Id :=
6846 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6847 Svg_Chars : constant Name_Id := Chars (Ibase);
6848 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6849 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6851 begin
6852 Copy_Node (Pbase, Ibase);
6854 -- Restore Itype status after Copy_Node
6856 Set_Is_Itype (Ibase);
6857 Set_Associated_Node_For_Itype (Ibase, N);
6859 Set_Chars (Ibase, Svg_Chars);
6860 Set_Prev_Entity (Ibase, Svg_Prev_E);
6861 Set_Next_Entity (Ibase, Svg_Next_E);
6862 Set_Sloc (Ibase, Sloc (Derived_Type));
6863 Set_Scope (Ibase, Scope (Derived_Type));
6864 Set_Freeze_Node (Ibase, Empty);
6865 Set_Is_Frozen (Ibase, False);
6866 Set_Comes_From_Source (Ibase, False);
6867 Set_Is_First_Subtype (Ibase, False);
6869 Set_Etype (Ibase, Pbase);
6870 Set_Etype (Derived_Type, Ibase);
6871 end;
6872 end if;
6874 Set_Directly_Designated_Type
6875 (Derived_Type, Designated_Type (Subt));
6877 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6878 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6879 Set_Size_Info (Derived_Type, Parent_Type);
6880 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
6881 Set_Depends_On_Private (Derived_Type,
6882 Has_Private_Component (Derived_Type));
6883 Conditional_Delay (Derived_Type, Subt);
6885 if Is_Access_Subprogram_Type (Derived_Type)
6886 and then Is_Base_Type (Derived_Type)
6887 then
6888 Set_Can_Use_Internal_Rep
6889 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6890 end if;
6892 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6893 -- that it is not redundant.
6895 if Null_Exclusion_Present (Type_Definition (N)) then
6896 Set_Can_Never_Be_Null (Derived_Type);
6898 elsif Can_Never_Be_Null (Parent_Type) then
6899 Set_Can_Never_Be_Null (Derived_Type);
6900 end if;
6902 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6903 -- the root type for this information.
6905 -- Apply range checks to discriminants for derived record case
6906 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6908 Desig_Type := Designated_Type (Derived_Type);
6910 if Is_Composite_Type (Desig_Type)
6911 and then (not Is_Array_Type (Desig_Type))
6912 and then Has_Discriminants (Desig_Type)
6913 and then Base_Type (Desig_Type) /= Desig_Type
6914 then
6915 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6916 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6918 Discr := First_Discriminant (Base_Type (Desig_Type));
6919 while Present (Discr_Con_El) loop
6920 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6921 Next_Elmt (Discr_Con_El);
6922 Next_Discriminant (Discr);
6923 end loop;
6924 end if;
6925 end Build_Derived_Access_Type;
6927 ------------------------------
6928 -- Build_Derived_Array_Type --
6929 ------------------------------
6931 procedure Build_Derived_Array_Type
6932 (N : Node_Id;
6933 Parent_Type : Entity_Id;
6934 Derived_Type : Entity_Id)
6936 Loc : constant Source_Ptr := Sloc (N);
6937 Tdef : constant Node_Id := Type_Definition (N);
6938 Indic : constant Node_Id := Subtype_Indication (Tdef);
6939 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6940 Implicit_Base : Entity_Id := Empty;
6941 New_Indic : Node_Id;
6943 procedure Make_Implicit_Base;
6944 -- If the parent subtype is constrained, the derived type is a subtype
6945 -- of an implicit base type derived from the parent base.
6947 ------------------------
6948 -- Make_Implicit_Base --
6949 ------------------------
6951 procedure Make_Implicit_Base is
6952 begin
6953 Implicit_Base :=
6954 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6956 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
6957 Set_Etype (Implicit_Base, Parent_Base);
6959 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6960 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6962 Set_Has_Delayed_Freeze (Implicit_Base, True);
6963 end Make_Implicit_Base;
6965 -- Start of processing for Build_Derived_Array_Type
6967 begin
6968 if not Is_Constrained (Parent_Type) then
6969 if Nkind (Indic) /= N_Subtype_Indication then
6970 Mutate_Ekind (Derived_Type, E_Array_Type);
6972 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6973 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6975 Set_Has_Delayed_Freeze (Derived_Type, True);
6977 else
6978 Make_Implicit_Base;
6979 Set_Etype (Derived_Type, Implicit_Base);
6981 New_Indic :=
6982 Make_Subtype_Declaration (Loc,
6983 Defining_Identifier => Derived_Type,
6984 Subtype_Indication =>
6985 Make_Subtype_Indication (Loc,
6986 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6987 Constraint => Constraint (Indic)));
6989 Rewrite (N, New_Indic);
6990 Analyze (N);
6991 end if;
6993 else
6994 if Nkind (Indic) /= N_Subtype_Indication then
6995 Make_Implicit_Base;
6997 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
6998 Set_Etype (Derived_Type, Implicit_Base);
6999 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7001 else
7002 Error_Msg_N ("illegal constraint on constrained type", Indic);
7003 end if;
7004 end if;
7006 -- If parent type is not a derived type itself, and is declared in
7007 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7008 -- the new type's concatenation operator since Derive_Subprograms
7009 -- will not inherit the parent's operator. If the parent type is
7010 -- unconstrained, the operator is of the unconstrained base type.
7012 if Number_Dimensions (Parent_Type) = 1
7013 and then not Is_Limited_Type (Parent_Type)
7014 and then not Is_Derived_Type (Parent_Type)
7015 and then not Is_Package_Or_Generic_Package
7016 (Scope (Base_Type (Parent_Type)))
7017 then
7018 if not Is_Constrained (Parent_Type)
7019 and then Is_Constrained (Derived_Type)
7020 then
7021 New_Concatenation_Op (Implicit_Base);
7022 else
7023 New_Concatenation_Op (Derived_Type);
7024 end if;
7025 end if;
7026 end Build_Derived_Array_Type;
7028 -----------------------------------
7029 -- Build_Derived_Concurrent_Type --
7030 -----------------------------------
7032 procedure Build_Derived_Concurrent_Type
7033 (N : Node_Id;
7034 Parent_Type : Entity_Id;
7035 Derived_Type : Entity_Id)
7037 Loc : constant Source_Ptr := Sloc (N);
7038 Def : constant Node_Id := Type_Definition (N);
7039 Indic : constant Node_Id := Subtype_Indication (Def);
7041 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7042 Corr_Decl : Node_Id;
7043 Corr_Decl_Needed : Boolean;
7044 -- If the derived type has fewer discriminants than its parent, the
7045 -- corresponding record is also a derived type, in order to account for
7046 -- the bound discriminants. We create a full type declaration for it in
7047 -- this case.
7049 Constraint_Present : constant Boolean :=
7050 Nkind (Indic) = N_Subtype_Indication;
7052 D_Constraint : Node_Id;
7053 New_Constraint : Elist_Id := No_Elist;
7054 Old_Disc : Entity_Id;
7055 New_Disc : Entity_Id;
7056 New_N : Node_Id;
7058 begin
7059 Set_Stored_Constraint (Derived_Type, No_Elist);
7060 Corr_Decl_Needed := False;
7061 Old_Disc := Empty;
7063 if Present (Discriminant_Specifications (N))
7064 and then Constraint_Present
7065 then
7066 Old_Disc := First_Discriminant (Parent_Type);
7067 New_Disc := First (Discriminant_Specifications (N));
7068 while Present (New_Disc) and then Present (Old_Disc) loop
7069 Next_Discriminant (Old_Disc);
7070 Next (New_Disc);
7071 end loop;
7072 end if;
7074 if Present (Old_Disc) and then Expander_Active then
7076 -- The new type has fewer discriminants, so we need to create a new
7077 -- corresponding record, which is derived from the corresponding
7078 -- record of the parent, and has a stored constraint that captures
7079 -- the values of the discriminant constraints. The corresponding
7080 -- record is needed only if expander is active and code generation is
7081 -- enabled.
7083 -- The type declaration for the derived corresponding record has the
7084 -- same discriminant part and constraints as the current declaration.
7085 -- Copy the unanalyzed tree to build declaration.
7087 Corr_Decl_Needed := True;
7088 New_N := Copy_Separate_Tree (N);
7090 Corr_Decl :=
7091 Make_Full_Type_Declaration (Loc,
7092 Defining_Identifier => Corr_Record,
7093 Discriminant_Specifications =>
7094 Discriminant_Specifications (New_N),
7095 Type_Definition =>
7096 Make_Derived_Type_Definition (Loc,
7097 Subtype_Indication =>
7098 Make_Subtype_Indication (Loc,
7099 Subtype_Mark =>
7100 New_Occurrence_Of
7101 (Corresponding_Record_Type (Parent_Type), Loc),
7102 Constraint =>
7103 Constraint
7104 (Subtype_Indication (Type_Definition (New_N))))));
7105 end if;
7107 -- Copy Storage_Size and Relative_Deadline variables if task case
7109 if Is_Task_Type (Parent_Type) then
7110 Set_Storage_Size_Variable (Derived_Type,
7111 Storage_Size_Variable (Parent_Type));
7112 Set_Relative_Deadline_Variable (Derived_Type,
7113 Relative_Deadline_Variable (Parent_Type));
7114 end if;
7116 if Present (Discriminant_Specifications (N)) then
7117 Push_Scope (Derived_Type);
7118 Check_Or_Process_Discriminants (N, Derived_Type);
7120 if Constraint_Present then
7121 New_Constraint :=
7122 Expand_To_Stored_Constraint
7123 (Parent_Type,
7124 Build_Discriminant_Constraints
7125 (Parent_Type, Indic, True));
7126 end if;
7128 End_Scope;
7130 elsif Constraint_Present then
7132 -- Build an unconstrained derived type and rewrite the derived type
7133 -- as a subtype of this new base type.
7135 declare
7136 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7137 New_Base : Entity_Id;
7138 New_Decl : Node_Id;
7139 New_Indic : Node_Id;
7141 begin
7142 New_Base :=
7143 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7145 New_Decl :=
7146 Make_Full_Type_Declaration (Loc,
7147 Defining_Identifier => New_Base,
7148 Type_Definition =>
7149 Make_Derived_Type_Definition (Loc,
7150 Abstract_Present => Abstract_Present (Def),
7151 Limited_Present => Limited_Present (Def),
7152 Subtype_Indication =>
7153 New_Occurrence_Of (Parent_Base, Loc)));
7155 Mark_Rewrite_Insertion (New_Decl);
7156 Insert_Before (N, New_Decl);
7157 Analyze (New_Decl);
7159 New_Indic :=
7160 Make_Subtype_Indication (Loc,
7161 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7162 Constraint => Relocate_Node (Constraint (Indic)));
7164 Rewrite (N,
7165 Make_Subtype_Declaration (Loc,
7166 Defining_Identifier => Derived_Type,
7167 Subtype_Indication => New_Indic));
7169 Analyze (N);
7170 return;
7171 end;
7172 end if;
7174 -- By default, operations and private data are inherited from parent.
7175 -- However, in the presence of bound discriminants, a new corresponding
7176 -- record will be created, see below.
7178 Set_Has_Discriminants
7179 (Derived_Type, Has_Discriminants (Parent_Type));
7180 Set_Corresponding_Record_Type
7181 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7183 -- Is_Constrained is set according the parent subtype, but is set to
7184 -- False if the derived type is declared with new discriminants.
7186 Set_Is_Constrained
7187 (Derived_Type,
7188 (Is_Constrained (Parent_Type) or else Constraint_Present)
7189 and then not Present (Discriminant_Specifications (N)));
7191 if Constraint_Present then
7192 if not Has_Discriminants (Parent_Type) then
7193 Error_Msg_N ("untagged parent must have discriminants", N);
7195 elsif Present (Discriminant_Specifications (N)) then
7197 -- Verify that new discriminants are used to constrain old ones
7199 D_Constraint := First (Constraints (Constraint (Indic)));
7201 Old_Disc := First_Discriminant (Parent_Type);
7203 while Present (D_Constraint) loop
7204 if Nkind (D_Constraint) /= N_Discriminant_Association then
7206 -- Positional constraint. If it is a reference to a new
7207 -- discriminant, it constrains the corresponding old one.
7209 if Nkind (D_Constraint) = N_Identifier then
7210 New_Disc := First_Discriminant (Derived_Type);
7211 while Present (New_Disc) loop
7212 exit when Chars (New_Disc) = Chars (D_Constraint);
7213 Next_Discriminant (New_Disc);
7214 end loop;
7216 if Present (New_Disc) then
7217 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7218 end if;
7219 end if;
7221 Next_Discriminant (Old_Disc);
7223 -- if this is a named constraint, search by name for the old
7224 -- discriminants constrained by the new one.
7226 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7228 -- Find new discriminant with that name
7230 New_Disc := First_Discriminant (Derived_Type);
7231 while Present (New_Disc) loop
7232 exit when
7233 Chars (New_Disc) = Chars (Expression (D_Constraint));
7234 Next_Discriminant (New_Disc);
7235 end loop;
7237 if Present (New_Disc) then
7239 -- Verify that new discriminant renames some discriminant
7240 -- of the parent type, and associate the new discriminant
7241 -- with one or more old ones that it renames.
7243 declare
7244 Selector : Node_Id;
7246 begin
7247 Selector := First (Selector_Names (D_Constraint));
7248 while Present (Selector) loop
7249 Old_Disc := First_Discriminant (Parent_Type);
7250 while Present (Old_Disc) loop
7251 exit when Chars (Old_Disc) = Chars (Selector);
7252 Next_Discriminant (Old_Disc);
7253 end loop;
7255 if Present (Old_Disc) then
7256 Set_Corresponding_Discriminant
7257 (New_Disc, Old_Disc);
7258 end if;
7260 Next (Selector);
7261 end loop;
7262 end;
7263 end if;
7264 end if;
7266 Next (D_Constraint);
7267 end loop;
7269 New_Disc := First_Discriminant (Derived_Type);
7270 while Present (New_Disc) loop
7271 if No (Corresponding_Discriminant (New_Disc)) then
7272 Error_Msg_NE
7273 ("new discriminant& must constrain old one", N, New_Disc);
7275 -- If a new discriminant is used in the constraint, then its
7276 -- subtype must be statically compatible with the subtype of
7277 -- the parent discriminant (RM 3.7(15)).
7279 else
7280 Check_Constraining_Discriminant
7281 (New_Disc, Corresponding_Discriminant (New_Disc));
7282 end if;
7284 Next_Discriminant (New_Disc);
7285 end loop;
7286 end if;
7288 elsif Present (Discriminant_Specifications (N)) then
7289 Error_Msg_N
7290 ("missing discriminant constraint in untagged derivation", N);
7291 end if;
7293 -- The entity chain of the derived type includes the new discriminants
7294 -- but shares operations with the parent.
7296 if Present (Discriminant_Specifications (N)) then
7297 Old_Disc := First_Discriminant (Parent_Type);
7298 while Present (Old_Disc) loop
7299 if No (Next_Entity (Old_Disc))
7300 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7301 then
7302 Link_Entities
7303 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7304 exit;
7305 end if;
7307 Next_Discriminant (Old_Disc);
7308 end loop;
7310 else
7311 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7312 if Has_Discriminants (Parent_Type) then
7313 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7314 Set_Discriminant_Constraint (
7315 Derived_Type, Discriminant_Constraint (Parent_Type));
7316 end if;
7317 end if;
7319 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7321 Set_Has_Completion (Derived_Type);
7323 if Corr_Decl_Needed then
7324 Set_Stored_Constraint (Derived_Type, New_Constraint);
7325 Insert_After (N, Corr_Decl);
7326 Analyze (Corr_Decl);
7327 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7328 end if;
7329 end Build_Derived_Concurrent_Type;
7331 ------------------------------------
7332 -- Build_Derived_Enumeration_Type --
7333 ------------------------------------
7335 procedure Build_Derived_Enumeration_Type
7336 (N : Node_Id;
7337 Parent_Type : Entity_Id;
7338 Derived_Type : Entity_Id)
7340 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7341 -- When the type declaration includes a constraint, we generate
7342 -- a subtype declaration of an anonymous base type, with the constraint
7343 -- given in the original type declaration. Conceptually, the bounds
7344 -- are converted to the new base type, and this conversion freezes
7345 -- (prematurely) that base type, when the bounds are simply literals.
7346 -- As a result, a representation clause for the derived type is then
7347 -- rejected or ignored. This procedure recognizes the simple case of
7348 -- literal bounds, which allows us to indicate that the conversions
7349 -- are not freeze points, and the subsequent representation clause
7350 -- can be accepted.
7351 -- A similar approach might be used to resolve the long-standing
7352 -- problem of premature freezing of derived numeric types ???
7354 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7355 begin
7356 return Nkind (B) = N_Type_Conversion
7357 and then Is_Entity_Name (Expression (B))
7358 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7359 end Bound_Belongs_To_Type;
7361 Loc : constant Source_Ptr := Sloc (N);
7362 Def : constant Node_Id := Type_Definition (N);
7363 Indic : constant Node_Id := Subtype_Indication (Def);
7364 Implicit_Base : Entity_Id;
7365 Literal : Entity_Id;
7366 New_Lit : Entity_Id;
7367 Literals_List : List_Id;
7368 Type_Decl : Node_Id;
7369 Hi, Lo : Node_Id;
7370 Rang_Expr : Node_Id;
7372 begin
7373 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7374 -- not have explicit literals lists we need to process types derived
7375 -- from them specially. This is handled by Derived_Standard_Character.
7376 -- If the parent type is a generic type, there are no literals either,
7377 -- and we construct the same skeletal representation as for the generic
7378 -- parent type.
7380 if Is_Standard_Character_Type (Parent_Type) then
7381 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7383 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7384 declare
7385 Lo : Node_Id;
7386 Hi : Node_Id;
7388 begin
7389 if Nkind (Indic) /= N_Subtype_Indication then
7390 Lo :=
7391 Make_Attribute_Reference (Loc,
7392 Attribute_Name => Name_First,
7393 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7394 Set_Etype (Lo, Derived_Type);
7396 Hi :=
7397 Make_Attribute_Reference (Loc,
7398 Attribute_Name => Name_Last,
7399 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7400 Set_Etype (Hi, Derived_Type);
7402 Set_Scalar_Range (Derived_Type,
7403 Make_Range (Loc,
7404 Low_Bound => Lo,
7405 High_Bound => Hi));
7406 else
7408 -- Analyze subtype indication and verify compatibility
7409 -- with parent type.
7411 if Base_Type (Process_Subtype (Indic, N)) /=
7412 Base_Type (Parent_Type)
7413 then
7414 Error_Msg_N
7415 ("illegal constraint for formal discrete type", N);
7416 end if;
7417 end if;
7418 end;
7420 else
7421 -- If a constraint is present, analyze the bounds to catch
7422 -- premature usage of the derived literals.
7424 if Nkind (Indic) = N_Subtype_Indication
7425 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7426 then
7427 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7428 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7429 end if;
7431 -- Introduce an implicit base type for the derived type even if there
7432 -- is no constraint attached to it, since this seems closer to the
7433 -- Ada semantics. Build a full type declaration tree for the derived
7434 -- type using the implicit base type as the defining identifier. Then
7435 -- build a subtype declaration tree which applies the constraint (if
7436 -- any) have it replace the derived type declaration.
7438 Literal := First_Literal (Parent_Type);
7439 Literals_List := New_List;
7440 while Present (Literal)
7441 and then Ekind (Literal) = E_Enumeration_Literal
7442 loop
7443 -- Literals of the derived type have the same representation as
7444 -- those of the parent type, but this representation can be
7445 -- overridden by an explicit representation clause. Indicate
7446 -- that there is no explicit representation given yet. These
7447 -- derived literals are implicit operations of the new type,
7448 -- and can be overridden by explicit ones.
7450 if Nkind (Literal) = N_Defining_Character_Literal then
7451 New_Lit :=
7452 Make_Defining_Character_Literal (Loc, Chars (Literal));
7453 else
7454 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7455 end if;
7457 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7458 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7459 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7460 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7461 Set_Alias (New_Lit, Literal);
7462 Set_Is_Known_Valid (New_Lit, True);
7464 Append (New_Lit, Literals_List);
7465 Next_Literal (Literal);
7466 end loop;
7468 Implicit_Base :=
7469 Make_Defining_Identifier (Sloc (Derived_Type),
7470 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7472 -- Indicate the proper nature of the derived type. This must be done
7473 -- before analysis of the literals, to recognize cases when a literal
7474 -- may be hidden by a previous explicit function definition (cf.
7475 -- c83031a).
7477 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7478 Set_Etype (Derived_Type, Implicit_Base);
7480 Type_Decl :=
7481 Make_Full_Type_Declaration (Loc,
7482 Defining_Identifier => Implicit_Base,
7483 Discriminant_Specifications => No_List,
7484 Type_Definition =>
7485 Make_Enumeration_Type_Definition (Loc, Literals_List));
7487 Mark_Rewrite_Insertion (Type_Decl);
7488 Insert_Before (N, Type_Decl);
7489 Analyze (Type_Decl);
7491 -- The anonymous base now has a full declaration, but this base
7492 -- is not a first subtype.
7494 Set_Is_First_Subtype (Implicit_Base, False);
7496 -- After the implicit base is analyzed its Etype needs to be changed
7497 -- to reflect the fact that it is derived from the parent type which
7498 -- was ignored during analysis. We also set the size at this point.
7500 Set_Etype (Implicit_Base, Parent_Type);
7502 Set_Size_Info (Implicit_Base, Parent_Type);
7503 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7504 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7506 -- Copy other flags from parent type
7508 Set_Has_Non_Standard_Rep
7509 (Implicit_Base, Has_Non_Standard_Rep
7510 (Parent_Type));
7511 Set_Has_Pragma_Ordered
7512 (Implicit_Base, Has_Pragma_Ordered
7513 (Parent_Type));
7514 Set_Has_Delayed_Freeze (Implicit_Base);
7516 -- Process the subtype indication including a validation check on the
7517 -- constraint, if any. If a constraint is given, its bounds must be
7518 -- implicitly converted to the new type.
7520 if Nkind (Indic) = N_Subtype_Indication then
7521 declare
7522 R : constant Node_Id :=
7523 Range_Expression (Constraint (Indic));
7525 begin
7526 if Nkind (R) = N_Range then
7527 Hi := Build_Scalar_Bound
7528 (High_Bound (R), Parent_Type, Implicit_Base);
7529 Lo := Build_Scalar_Bound
7530 (Low_Bound (R), Parent_Type, Implicit_Base);
7532 else
7533 -- Constraint is a Range attribute. Replace with explicit
7534 -- mention of the bounds of the prefix, which must be a
7535 -- subtype.
7537 Analyze (Prefix (R));
7538 Hi :=
7539 Convert_To (Implicit_Base,
7540 Make_Attribute_Reference (Loc,
7541 Attribute_Name => Name_Last,
7542 Prefix =>
7543 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7545 Lo :=
7546 Convert_To (Implicit_Base,
7547 Make_Attribute_Reference (Loc,
7548 Attribute_Name => Name_First,
7549 Prefix =>
7550 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7551 end if;
7552 end;
7554 else
7555 Hi :=
7556 Build_Scalar_Bound
7557 (Type_High_Bound (Parent_Type),
7558 Parent_Type, Implicit_Base);
7559 Lo :=
7560 Build_Scalar_Bound
7561 (Type_Low_Bound (Parent_Type),
7562 Parent_Type, Implicit_Base);
7563 end if;
7565 Rang_Expr :=
7566 Make_Range (Loc,
7567 Low_Bound => Lo,
7568 High_Bound => Hi);
7570 -- If we constructed a default range for the case where no range
7571 -- was given, then the expressions in the range must not freeze
7572 -- since they do not correspond to expressions in the source.
7573 -- However, if the type inherits predicates the expressions will
7574 -- be elaborated earlier and must freeze.
7576 if (Nkind (Indic) /= N_Subtype_Indication
7577 or else
7578 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7579 and then not Has_Predicates (Derived_Type)
7580 then
7581 Set_Must_Not_Freeze (Lo);
7582 Set_Must_Not_Freeze (Hi);
7583 Set_Must_Not_Freeze (Rang_Expr);
7584 end if;
7586 Rewrite (N,
7587 Make_Subtype_Declaration (Loc,
7588 Defining_Identifier => Derived_Type,
7589 Subtype_Indication =>
7590 Make_Subtype_Indication (Loc,
7591 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7592 Constraint =>
7593 Make_Range_Constraint (Loc,
7594 Range_Expression => Rang_Expr))));
7596 Analyze (N);
7598 -- Propagate the aspects from the original type declaration to the
7599 -- declaration of the implicit base.
7601 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7603 -- Apply a range check. Since this range expression doesn't have an
7604 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7605 -- this right???
7607 if Nkind (Indic) = N_Subtype_Indication then
7608 Apply_Range_Check
7609 (Range_Expression (Constraint (Indic)), Parent_Type,
7610 Source_Typ => Entity (Subtype_Mark (Indic)));
7611 end if;
7612 end if;
7613 end Build_Derived_Enumeration_Type;
7615 --------------------------------
7616 -- Build_Derived_Numeric_Type --
7617 --------------------------------
7619 procedure Build_Derived_Numeric_Type
7620 (N : Node_Id;
7621 Parent_Type : Entity_Id;
7622 Derived_Type : Entity_Id)
7624 Loc : constant Source_Ptr := Sloc (N);
7625 Tdef : constant Node_Id := Type_Definition (N);
7626 Indic : constant Node_Id := Subtype_Indication (Tdef);
7627 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7628 No_Constraint : constant Boolean := Nkind (Indic) /=
7629 N_Subtype_Indication;
7630 Implicit_Base : Entity_Id;
7632 Lo : Node_Id;
7633 Hi : Node_Id;
7635 begin
7636 -- Process the subtype indication including a validation check on
7637 -- the constraint if any.
7639 Discard_Node (Process_Subtype (Indic, N));
7641 -- Introduce an implicit base type for the derived type even if there
7642 -- is no constraint attached to it, since this seems closer to the Ada
7643 -- semantics.
7645 Implicit_Base :=
7646 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7648 Set_Etype (Implicit_Base, Parent_Base);
7649 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7650 Set_Size_Info (Implicit_Base, Parent_Base);
7651 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7652 Set_Parent (Implicit_Base, Parent (Derived_Type));
7653 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7654 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7656 -- Set RM Size for discrete type or decimal fixed-point type
7657 -- Ordinary fixed-point is excluded, why???
7659 if Is_Discrete_Type (Parent_Base)
7660 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7661 then
7662 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7663 end if;
7665 Set_Has_Delayed_Freeze (Implicit_Base);
7667 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7668 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7670 Set_Scalar_Range (Implicit_Base,
7671 Make_Range (Loc,
7672 Low_Bound => Lo,
7673 High_Bound => Hi));
7675 if Has_Infinities (Parent_Base) then
7676 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7677 end if;
7679 -- The Derived_Type, which is the entity of the declaration, is a
7680 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7681 -- absence of an explicit constraint.
7683 Set_Etype (Derived_Type, Implicit_Base);
7685 -- If we did not have a constraint, then the Ekind is set from the
7686 -- parent type (otherwise Process_Subtype has set the bounds)
7688 if No_Constraint then
7689 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7690 end if;
7692 -- If we did not have a range constraint, then set the range from the
7693 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7695 if No_Constraint or else not Has_Range_Constraint (Indic) then
7696 Set_Scalar_Range (Derived_Type,
7697 Make_Range (Loc,
7698 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7699 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7700 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7702 if Has_Infinities (Parent_Type) then
7703 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7704 end if;
7706 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7707 end if;
7709 Set_Is_Descendant_Of_Address (Derived_Type,
7710 Is_Descendant_Of_Address (Parent_Type));
7711 Set_Is_Descendant_Of_Address (Implicit_Base,
7712 Is_Descendant_Of_Address (Parent_Type));
7714 -- Set remaining type-specific fields, depending on numeric type
7716 if Is_Modular_Integer_Type (Parent_Type) then
7717 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7719 Set_Non_Binary_Modulus
7720 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7722 Set_Is_Known_Valid
7723 (Implicit_Base, Is_Known_Valid (Parent_Base));
7725 elsif Is_Floating_Point_Type (Parent_Type) then
7727 -- Digits of base type is always copied from the digits value of
7728 -- the parent base type, but the digits of the derived type will
7729 -- already have been set if there was a constraint present.
7731 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7732 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7734 if No_Constraint then
7735 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7736 end if;
7738 elsif Is_Fixed_Point_Type (Parent_Type) then
7740 -- Small of base type and derived type are always copied from the
7741 -- parent base type, since smalls never change. The delta of the
7742 -- base type is also copied from the parent base type. However the
7743 -- delta of the derived type will have been set already if a
7744 -- constraint was present.
7746 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7747 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7748 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7750 if No_Constraint then
7751 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7752 end if;
7754 -- The scale and machine radix in the decimal case are always
7755 -- copied from the parent base type.
7757 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7758 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7759 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7761 Set_Machine_Radix_10
7762 (Derived_Type, Machine_Radix_10 (Parent_Base));
7763 Set_Machine_Radix_10
7764 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7766 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7768 if No_Constraint then
7769 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7771 else
7772 -- the analysis of the subtype_indication sets the
7773 -- digits value of the derived type.
7775 null;
7776 end if;
7777 end if;
7778 end if;
7780 if Is_Integer_Type (Parent_Type) then
7781 Set_Has_Shift_Operator
7782 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7783 end if;
7785 -- The type of the bounds is that of the parent type, and they
7786 -- must be converted to the derived type.
7788 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7790 -- The implicit_base should be frozen when the derived type is frozen,
7791 -- but note that it is used in the conversions of the bounds. For fixed
7792 -- types we delay the determination of the bounds until the proper
7793 -- freezing point. For other numeric types this is rejected by GCC, for
7794 -- reasons that are currently unclear (???), so we choose to freeze the
7795 -- implicit base now. In the case of integers and floating point types
7796 -- this is harmless because subsequent representation clauses cannot
7797 -- affect anything, but it is still baffling that we cannot use the
7798 -- same mechanism for all derived numeric types.
7800 -- There is a further complication: actually some representation
7801 -- clauses can affect the implicit base type. For example, attribute
7802 -- definition clauses for stream-oriented attributes need to set the
7803 -- corresponding TSS entries on the base type, and this normally
7804 -- cannot be done after the base type is frozen, so the circuitry in
7805 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7806 -- and not use Set_TSS in this case.
7808 -- There are also consequences for the case of delayed representation
7809 -- aspects for some cases. For example, a Size aspect is delayed and
7810 -- should not be evaluated to the freeze point. This early freezing
7811 -- means that the size attribute evaluation happens too early???
7813 if Is_Fixed_Point_Type (Parent_Type) then
7814 Conditional_Delay (Implicit_Base, Parent_Type);
7815 else
7816 Freeze_Before (N, Implicit_Base);
7817 end if;
7818 end Build_Derived_Numeric_Type;
7820 --------------------------------
7821 -- Build_Derived_Private_Type --
7822 --------------------------------
7824 procedure Build_Derived_Private_Type
7825 (N : Node_Id;
7826 Parent_Type : Entity_Id;
7827 Derived_Type : Entity_Id;
7828 Is_Completion : Boolean;
7829 Derive_Subps : Boolean := True)
7831 Loc : constant Source_Ptr := Sloc (N);
7832 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7833 Par_Scope : constant Entity_Id := Scope (Par_Base);
7834 Full_N : constant Node_Id := New_Copy_Tree (N);
7835 Full_Der : Entity_Id := New_Copy (Derived_Type);
7836 Full_P : Entity_Id;
7838 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7839 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7840 -- present (they cannot be both present for the same type), or Empty.
7842 procedure Build_Full_Derivation;
7843 -- Build full derivation, i.e. derive from the full view
7845 procedure Copy_And_Build;
7846 -- Copy derived type declaration, replace parent with its full view,
7847 -- and build derivation
7849 -------------------------
7850 -- Available_Full_View --
7851 -------------------------
7853 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7854 begin
7855 if Present (Full_View (Typ)) then
7856 return Full_View (Typ);
7858 elsif Present (Underlying_Full_View (Typ)) then
7860 -- We should be called on a type with an underlying full view
7861 -- only by means of the recursive call made in Copy_And_Build
7862 -- through the first call to Build_Derived_Type, or else if
7863 -- the parent scope is being analyzed because we are deriving
7864 -- a completion.
7866 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7868 return Underlying_Full_View (Typ);
7870 else
7871 return Empty;
7872 end if;
7873 end Available_Full_View;
7875 ---------------------------
7876 -- Build_Full_Derivation --
7877 ---------------------------
7879 procedure Build_Full_Derivation is
7880 begin
7881 -- If parent scope is not open, install the declarations
7883 if not In_Open_Scopes (Par_Scope) then
7884 Install_Private_Declarations (Par_Scope);
7885 Install_Visible_Declarations (Par_Scope);
7886 Copy_And_Build;
7887 Uninstall_Declarations (Par_Scope);
7889 -- If parent scope is open and in another unit, and parent has a
7890 -- completion, then the derivation is taking place in the visible
7891 -- part of a child unit. In that case retrieve the full view of
7892 -- the parent momentarily.
7894 elsif not In_Same_Source_Unit (N, Parent_Type)
7895 and then Present (Full_View (Parent_Type))
7896 then
7897 Full_P := Full_View (Parent_Type);
7898 Exchange_Declarations (Parent_Type);
7899 Copy_And_Build;
7900 Exchange_Declarations (Full_P);
7902 -- Otherwise it is a local derivation
7904 else
7905 Copy_And_Build;
7906 end if;
7907 end Build_Full_Derivation;
7909 --------------------
7910 -- Copy_And_Build --
7911 --------------------
7913 procedure Copy_And_Build is
7914 Full_Parent : Entity_Id := Parent_Type;
7916 begin
7917 -- If the parent is itself derived from another private type,
7918 -- installing the private declarations has not affected its
7919 -- privacy status, so use its own full view explicitly.
7921 if Is_Private_Type (Full_Parent)
7922 and then Present (Full_View (Full_Parent))
7923 then
7924 Full_Parent := Full_View (Full_Parent);
7925 end if;
7927 -- If the full view is itself derived from another private type
7928 -- and has got an underlying full view, and this is done for a
7929 -- completion, i.e. to build the underlying full view of the type,
7930 -- then use this underlying full view. We cannot do that if this
7931 -- is not a completion, i.e. to build the full view of the type,
7932 -- because this would break the privacy of the parent type, except
7933 -- if the parent scope is being analyzed because we are deriving a
7934 -- completion.
7936 if Is_Private_Type (Full_Parent)
7937 and then Present (Underlying_Full_View (Full_Parent))
7938 and then (Is_Completion or else In_Private_Part (Par_Scope))
7939 then
7940 Full_Parent := Underlying_Full_View (Full_Parent);
7941 end if;
7943 -- For private, record, concurrent, access and almost all enumeration
7944 -- types, the derivation from the full view requires a fully-fledged
7945 -- declaration. In the other cases, just use an itype.
7947 if Is_Private_Type (Full_Parent)
7948 or else Is_Record_Type (Full_Parent)
7949 or else Is_Concurrent_Type (Full_Parent)
7950 or else Is_Access_Type (Full_Parent)
7951 or else
7952 (Is_Enumeration_Type (Full_Parent)
7953 and then not Is_Standard_Character_Type (Full_Parent)
7954 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7955 then
7956 -- Copy and adjust declaration to provide a completion for what
7957 -- is originally a private declaration. Indicate that full view
7958 -- is internally generated.
7960 Set_Comes_From_Source (Full_N, False);
7961 Set_Comes_From_Source (Full_Der, False);
7962 Set_Parent (Full_Der, Full_N);
7963 Set_Defining_Identifier (Full_N, Full_Der);
7965 -- If there are no constraints, adjust the subtype mark
7967 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7968 N_Subtype_Indication
7969 then
7970 Set_Subtype_Indication
7971 (Type_Definition (Full_N),
7972 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7973 end if;
7975 Insert_After (N, Full_N);
7977 -- Build full view of derived type from full view of parent which
7978 -- is now installed. Subprograms have been derived on the partial
7979 -- view, the completion does not derive them anew.
7981 if Is_Record_Type (Full_Parent) then
7983 -- If parent type is tagged, the completion inherits the proper
7984 -- primitive operations.
7986 if Is_Tagged_Type (Parent_Type) then
7987 Build_Derived_Record_Type
7988 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7989 else
7990 Build_Derived_Record_Type
7991 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7992 end if;
7994 else
7995 -- If the parent type is private, this is not a completion and
7996 -- we build the full derivation recursively as a completion.
7998 Build_Derived_Type
7999 (Full_N, Full_Parent, Full_Der,
8000 Is_Completion => Is_Private_Type (Full_Parent),
8001 Derive_Subps => False);
8002 end if;
8004 -- The full declaration has been introduced into the tree and
8005 -- processed in the step above. It should not be analyzed again
8006 -- (when encountered later in the current list of declarations)
8007 -- to prevent spurious name conflicts. The full entity remains
8008 -- invisible.
8010 Set_Analyzed (Full_N);
8012 else
8013 Full_Der :=
8014 Make_Defining_Identifier (Sloc (Derived_Type),
8015 Chars => Chars (Derived_Type));
8016 Set_Is_Itype (Full_Der);
8017 Set_Associated_Node_For_Itype (Full_Der, N);
8018 Set_Parent (Full_Der, N);
8019 Build_Derived_Type
8020 (N, Full_Parent, Full_Der,
8021 Is_Completion => False, Derive_Subps => False);
8022 end if;
8024 Set_Has_Private_Declaration (Full_Der);
8025 Set_Has_Private_Declaration (Derived_Type);
8027 Set_Scope (Full_Der, Scope (Derived_Type));
8028 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8029 Set_Has_Size_Clause (Full_Der, False);
8030 Set_Has_Alignment_Clause (Full_Der, False);
8031 Set_Has_Delayed_Freeze (Full_Der);
8032 Set_Is_Frozen (Full_Der, False);
8033 Set_Freeze_Node (Full_Der, Empty);
8034 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8035 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8037 -- The convention on the base type may be set in the private part
8038 -- and not propagated to the subtype until later, so we obtain the
8039 -- convention from the base type of the parent.
8041 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8042 end Copy_And_Build;
8044 -- Start of processing for Build_Derived_Private_Type
8046 begin
8047 if Is_Tagged_Type (Parent_Type) then
8048 Full_P := Full_View (Parent_Type);
8050 -- A type extension of a type with unknown discriminants is an
8051 -- indefinite type that the back-end cannot handle directly.
8052 -- We treat it as a private type, and build a completion that is
8053 -- derived from the full view of the parent, and hopefully has
8054 -- known discriminants.
8056 -- If the full view of the parent type has an underlying record view,
8057 -- use it to generate the underlying record view of this derived type
8058 -- (required for chains of derivations with unknown discriminants).
8060 -- Minor optimization: we avoid the generation of useless underlying
8061 -- record view entities if the private type declaration has unknown
8062 -- discriminants but its corresponding full view has no
8063 -- discriminants.
8065 if Has_Unknown_Discriminants (Parent_Type)
8066 and then Present (Full_P)
8067 and then (Has_Discriminants (Full_P)
8068 or else Present (Underlying_Record_View (Full_P)))
8069 and then not In_Open_Scopes (Par_Scope)
8070 and then Expander_Active
8071 then
8072 declare
8073 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8074 New_Ext : constant Node_Id :=
8075 Copy_Separate_Tree
8076 (Record_Extension_Part (Type_Definition (N)));
8077 Decl : Node_Id;
8079 begin
8080 Build_Derived_Record_Type
8081 (N, Parent_Type, Derived_Type, Derive_Subps);
8083 -- Build anonymous completion, as a derivation from the full
8084 -- view of the parent. This is not a completion in the usual
8085 -- sense, because the current type is not private.
8087 Decl :=
8088 Make_Full_Type_Declaration (Loc,
8089 Defining_Identifier => Full_Der,
8090 Type_Definition =>
8091 Make_Derived_Type_Definition (Loc,
8092 Subtype_Indication =>
8093 New_Copy_Tree
8094 (Subtype_Indication (Type_Definition (N))),
8095 Record_Extension_Part => New_Ext));
8097 -- If the parent type has an underlying record view, use it
8098 -- here to build the new underlying record view.
8100 if Present (Underlying_Record_View (Full_P)) then
8101 pragma Assert
8102 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8103 = N_Identifier);
8104 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8105 Underlying_Record_View (Full_P));
8106 end if;
8108 Install_Private_Declarations (Par_Scope);
8109 Install_Visible_Declarations (Par_Scope);
8110 Insert_Before (N, Decl);
8112 -- Mark entity as an underlying record view before analysis,
8113 -- to avoid generating the list of its primitive operations
8114 -- (which is not really required for this entity) and thus
8115 -- prevent spurious errors associated with missing overriding
8116 -- of abstract primitives (overridden only for Derived_Type).
8118 Mutate_Ekind (Full_Der, E_Record_Type);
8119 Set_Is_Underlying_Record_View (Full_Der);
8120 Set_Default_SSO (Full_Der);
8121 Set_No_Reordering (Full_Der, No_Component_Reordering);
8123 Analyze (Decl);
8125 pragma Assert (Has_Discriminants (Full_Der)
8126 and then not Has_Unknown_Discriminants (Full_Der));
8128 Uninstall_Declarations (Par_Scope);
8130 -- Freeze the underlying record view, to prevent generation of
8131 -- useless dispatching information, which is simply shared with
8132 -- the real derived type.
8134 Set_Is_Frozen (Full_Der);
8136 -- If the derived type has access discriminants, create
8137 -- references to their anonymous types now, to prevent
8138 -- back-end problems when their first use is in generated
8139 -- bodies of primitives.
8141 declare
8142 E : Entity_Id;
8144 begin
8145 E := First_Entity (Full_Der);
8147 while Present (E) loop
8148 if Ekind (E) = E_Discriminant
8149 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8150 then
8151 Build_Itype_Reference (Etype (E), Decl);
8152 end if;
8154 Next_Entity (E);
8155 end loop;
8156 end;
8158 -- Set up links between real entity and underlying record view
8160 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8161 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8162 end;
8164 -- If discriminants are known, build derived record
8166 else
8167 Build_Derived_Record_Type
8168 (N, Parent_Type, Derived_Type, Derive_Subps);
8169 end if;
8171 return;
8173 elsif Has_Discriminants (Parent_Type) then
8175 -- Build partial view of derived type from partial view of parent.
8176 -- This must be done before building the full derivation because the
8177 -- second derivation will modify the discriminants of the first and
8178 -- the discriminants are chained with the rest of the components in
8179 -- the full derivation.
8181 Build_Derived_Record_Type
8182 (N, Parent_Type, Derived_Type, Derive_Subps);
8184 -- Build the full derivation if this is not the anonymous derived
8185 -- base type created by Build_Derived_Record_Type in the constrained
8186 -- case (see point 5. of its head comment) since we build it for the
8187 -- derived subtype.
8189 if Present (Available_Full_View (Parent_Type))
8190 and then not Is_Itype (Derived_Type)
8191 then
8192 declare
8193 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8194 Discr : Entity_Id;
8195 Last_Discr : Entity_Id;
8197 begin
8198 -- If this is not a completion, construct the implicit full
8199 -- view by deriving from the full view of the parent type.
8200 -- But if this is a completion, the derived private type
8201 -- being built is a full view and the full derivation can
8202 -- only be its underlying full view.
8204 Build_Full_Derivation;
8206 if not Is_Completion then
8207 Set_Full_View (Derived_Type, Full_Der);
8208 else
8209 Set_Underlying_Full_View (Derived_Type, Full_Der);
8210 Set_Is_Underlying_Full_View (Full_Der);
8211 end if;
8213 if not Is_Base_Type (Derived_Type) then
8214 Set_Full_View (Der_Base, Base_Type (Full_Der));
8215 end if;
8217 -- Copy the discriminant list from full view to the partial
8218 -- view (base type and its subtype). Gigi requires that the
8219 -- partial and full views have the same discriminants.
8221 -- Note that since the partial view points to discriminants
8222 -- in the full view, their scope will be that of the full
8223 -- view. This might cause some front end problems and need
8224 -- adjustment???
8226 Discr := First_Discriminant (Base_Type (Full_Der));
8227 Set_First_Entity (Der_Base, Discr);
8229 loop
8230 Last_Discr := Discr;
8231 Next_Discriminant (Discr);
8232 exit when No (Discr);
8233 end loop;
8235 Set_Last_Entity (Der_Base, Last_Discr);
8236 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8237 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8238 end;
8239 end if;
8241 elsif Present (Available_Full_View (Parent_Type))
8242 and then Has_Discriminants (Available_Full_View (Parent_Type))
8243 then
8244 if Has_Unknown_Discriminants (Parent_Type)
8245 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8246 N_Subtype_Indication
8247 then
8248 Error_Msg_N
8249 ("cannot constrain type with unknown discriminants",
8250 Subtype_Indication (Type_Definition (N)));
8251 return;
8252 end if;
8254 -- If this is not a completion, construct the implicit full view by
8255 -- deriving from the full view of the parent type. But if this is a
8256 -- completion, the derived private type being built is a full view
8257 -- and the full derivation can only be its underlying full view.
8259 Build_Full_Derivation;
8261 if not Is_Completion then
8262 Set_Full_View (Derived_Type, Full_Der);
8263 else
8264 Set_Underlying_Full_View (Derived_Type, Full_Der);
8265 Set_Is_Underlying_Full_View (Full_Der);
8266 end if;
8268 -- In any case, the primitive operations are inherited from the
8269 -- parent type, not from the internal full view.
8271 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8273 if Derive_Subps then
8274 Derive_Subprograms (Parent_Type, Derived_Type);
8275 end if;
8277 Set_Stored_Constraint (Derived_Type, No_Elist);
8278 Set_Is_Constrained
8279 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8281 else
8282 -- Untagged type, No discriminants on either view
8284 if Nkind (Subtype_Indication (Type_Definition (N))) =
8285 N_Subtype_Indication
8286 then
8287 Error_Msg_N
8288 ("illegal constraint on type without discriminants", N);
8289 end if;
8291 if Present (Discriminant_Specifications (N))
8292 and then Present (Available_Full_View (Parent_Type))
8293 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8294 then
8295 Error_Msg_N ("cannot add discriminants to untagged type", N);
8296 end if;
8298 Set_Stored_Constraint (Derived_Type, No_Elist);
8299 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8301 Set_Is_Controlled_Active
8302 (Derived_Type, Is_Controlled_Active (Parent_Type));
8304 Set_Disable_Controlled
8305 (Derived_Type, Disable_Controlled (Parent_Type));
8307 Set_Has_Controlled_Component
8308 (Derived_Type, Has_Controlled_Component (Parent_Type));
8310 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8312 if not Is_Controlled (Parent_Type) then
8313 Set_Finalize_Storage_Only
8314 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8315 end if;
8317 -- If this is not a completion, construct the implicit full view by
8318 -- deriving from the full view of the parent type. But if this is a
8319 -- completion, the derived private type being built is a full view
8320 -- and the full derivation can only be its underlying full view.
8322 -- ??? If the parent type is untagged private and its completion is
8323 -- tagged, this mechanism will not work because we cannot derive from
8324 -- the tagged full view unless we have an extension.
8326 if Present (Available_Full_View (Parent_Type))
8327 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8328 and then not Error_Posted (N)
8329 then
8330 Build_Full_Derivation;
8332 if not Is_Completion then
8333 Set_Full_View (Derived_Type, Full_Der);
8334 else
8335 Set_Underlying_Full_View (Derived_Type, Full_Der);
8336 Set_Is_Underlying_Full_View (Full_Der);
8337 end if;
8338 end if;
8339 end if;
8341 Set_Has_Unknown_Discriminants (Derived_Type,
8342 Has_Unknown_Discriminants (Parent_Type));
8344 if Is_Private_Type (Derived_Type) then
8345 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8346 end if;
8348 -- If the parent base type is in scope, add the derived type to its
8349 -- list of private dependents, because its full view may become
8350 -- visible subsequently (in a nested private part, a body, or in a
8351 -- further child unit).
8353 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8354 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8356 -- Check for unusual case where a type completed by a private
8357 -- derivation occurs within a package nested in a child unit, and
8358 -- the parent is declared in an ancestor.
8360 if Is_Child_Unit (Scope (Current_Scope))
8361 and then Is_Completion
8362 and then In_Private_Part (Current_Scope)
8363 and then Scope (Parent_Type) /= Current_Scope
8365 -- Note that if the parent has a completion in the private part,
8366 -- (which is itself a derivation from some other private type)
8367 -- it is that completion that is visible, there is no full view
8368 -- available, and no special processing is needed.
8370 and then Present (Full_View (Parent_Type))
8371 then
8372 -- In this case, the full view of the parent type will become
8373 -- visible in the body of the enclosing child, and only then will
8374 -- the current type be possibly non-private. Build an underlying
8375 -- full view that will be installed when the enclosing child body
8376 -- is compiled.
8378 if Present (Underlying_Full_View (Derived_Type)) then
8379 Full_Der := Underlying_Full_View (Derived_Type);
8380 else
8381 Build_Full_Derivation;
8382 Set_Underlying_Full_View (Derived_Type, Full_Der);
8383 Set_Is_Underlying_Full_View (Full_Der);
8384 end if;
8386 -- The full view will be used to swap entities on entry/exit to
8387 -- the body, and must appear in the entity list for the package.
8389 Append_Entity (Full_Der, Scope (Derived_Type));
8390 end if;
8391 end if;
8392 end Build_Derived_Private_Type;
8394 -------------------------------
8395 -- Build_Derived_Record_Type --
8396 -------------------------------
8398 -- 1. INTRODUCTION
8400 -- Ideally we would like to use the same model of type derivation for
8401 -- tagged and untagged record types. Unfortunately this is not quite
8402 -- possible because the semantics of representation clauses is different
8403 -- for tagged and untagged records under inheritance. Consider the
8404 -- following:
8406 -- type R (...) is [tagged] record ... end record;
8407 -- type T (...) is new R (...) [with ...];
8409 -- The representation clauses for T can specify a completely different
8410 -- record layout from R's. Hence the same component can be placed in two
8411 -- very different positions in objects of type T and R. If R and T are
8412 -- tagged types, representation clauses for T can only specify the layout
8413 -- of non inherited components, thus components that are common in R and T
8414 -- have the same position in objects of type R and T.
8416 -- This has two implications. The first is that the entire tree for R's
8417 -- declaration needs to be copied for T in the untagged case, so that T
8418 -- can be viewed as a record type of its own with its own representation
8419 -- clauses. The second implication is the way we handle discriminants.
8420 -- Specifically, in the untagged case we need a way to communicate to Gigi
8421 -- what are the real discriminants in the record, while for the semantics
8422 -- we need to consider those introduced by the user to rename the
8423 -- discriminants in the parent type. This is handled by introducing the
8424 -- notion of stored discriminants. See below for more.
8426 -- Fortunately the way regular components are inherited can be handled in
8427 -- the same way in tagged and untagged types.
8429 -- To complicate things a bit more the private view of a private extension
8430 -- cannot be handled in the same way as the full view (for one thing the
8431 -- semantic rules are somewhat different). We will explain what differs
8432 -- below.
8434 -- 2. DISCRIMINANTS UNDER INHERITANCE
8436 -- The semantic rules governing the discriminants of derived types are
8437 -- quite subtle.
8439 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8440 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8442 -- If parent type has discriminants, then the discriminants that are
8443 -- declared in the derived type are [3.4 (11)]:
8445 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8446 -- there is one;
8448 -- o Otherwise, each discriminant of the parent type (implicitly declared
8449 -- in the same order with the same specifications). In this case, the
8450 -- discriminants are said to be "inherited", or if unknown in the parent
8451 -- are also unknown in the derived type.
8453 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8455 -- o The parent subtype must be constrained;
8457 -- o If the parent type is not a tagged type, then each discriminant of
8458 -- the derived type must be used in the constraint defining a parent
8459 -- subtype. [Implementation note: This ensures that the new discriminant
8460 -- can share storage with an existing discriminant.]
8462 -- For the derived type each discriminant of the parent type is either
8463 -- inherited, constrained to equal some new discriminant of the derived
8464 -- type, or constrained to the value of an expression.
8466 -- When inherited or constrained to equal some new discriminant, the
8467 -- parent discriminant and the discriminant of the derived type are said
8468 -- to "correspond".
8470 -- If a discriminant of the parent type is constrained to a specific value
8471 -- in the derived type definition, then the discriminant is said to be
8472 -- "specified" by that derived type definition.
8474 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8476 -- We have spoken about stored discriminants in point 1 (introduction)
8477 -- above. There are two sorts of stored discriminants: implicit and
8478 -- explicit. As long as the derived type inherits the same discriminants as
8479 -- the root record type, stored discriminants are the same as regular
8480 -- discriminants, and are said to be implicit. However, if any discriminant
8481 -- in the root type was renamed in the derived type, then the derived
8482 -- type will contain explicit stored discriminants. Explicit stored
8483 -- discriminants are discriminants in addition to the semantically visible
8484 -- discriminants defined for the derived type. Stored discriminants are
8485 -- used by Gigi to figure out what are the physical discriminants in
8486 -- objects of the derived type (see precise definition in einfo.ads).
8487 -- As an example, consider the following:
8489 -- type R (D1, D2, D3 : Int) is record ... end record;
8490 -- type T1 is new R;
8491 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8492 -- type T3 is new T2;
8493 -- type T4 (Y : Int) is new T3 (Y, 99);
8495 -- The following table summarizes the discriminants and stored
8496 -- discriminants in R and T1 through T4:
8498 -- Type Discrim Stored Discrim Comment
8499 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8500 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8501 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8502 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8503 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8505 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8506 -- find the corresponding discriminant in the parent type, while
8507 -- Original_Record_Component (abbreviated ORC below) the actual physical
8508 -- component that is renamed. Finally the field Is_Completely_Hidden
8509 -- (abbreviated ICH below) is set for all explicit stored discriminants
8510 -- (see einfo.ads for more info). For the above example this gives:
8512 -- Discrim CD ORC ICH
8513 -- ^^^^^^^ ^^ ^^^ ^^^
8514 -- D1 in R empty itself no
8515 -- D2 in R empty itself no
8516 -- D3 in R empty itself no
8518 -- D1 in T1 D1 in R itself no
8519 -- D2 in T1 D2 in R itself no
8520 -- D3 in T1 D3 in R itself no
8522 -- X1 in T2 D3 in T1 D3 in T2 no
8523 -- X2 in T2 D1 in T1 D1 in T2 no
8524 -- D1 in T2 empty itself yes
8525 -- D2 in T2 empty itself yes
8526 -- D3 in T2 empty itself yes
8528 -- X1 in T3 X1 in T2 D3 in T3 no
8529 -- X2 in T3 X2 in T2 D1 in T3 no
8530 -- D1 in T3 empty itself yes
8531 -- D2 in T3 empty itself yes
8532 -- D3 in T3 empty itself yes
8534 -- Y in T4 X1 in T3 D3 in T4 no
8535 -- D1 in T4 empty itself yes
8536 -- D2 in T4 empty itself yes
8537 -- D3 in T4 empty itself yes
8539 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8541 -- Type derivation for tagged types is fairly straightforward. If no
8542 -- discriminants are specified by the derived type, these are inherited
8543 -- from the parent. No explicit stored discriminants are ever necessary.
8544 -- The only manipulation that is done to the tree is that of adding a
8545 -- _parent field with parent type and constrained to the same constraint
8546 -- specified for the parent in the derived type definition. For instance:
8548 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8549 -- type T1 is new R with null record;
8550 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8552 -- are changed into:
8554 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8555 -- _parent : R (D1, D2, D3);
8556 -- end record;
8558 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8559 -- _parent : T1 (X2, 88, X1);
8560 -- end record;
8562 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8563 -- ORC and ICH fields are:
8565 -- Discrim CD ORC ICH
8566 -- ^^^^^^^ ^^ ^^^ ^^^
8567 -- D1 in R empty itself no
8568 -- D2 in R empty itself no
8569 -- D3 in R empty itself no
8571 -- D1 in T1 D1 in R D1 in R no
8572 -- D2 in T1 D2 in R D2 in R no
8573 -- D3 in T1 D3 in R D3 in R no
8575 -- X1 in T2 D3 in T1 D3 in R no
8576 -- X2 in T2 D1 in T1 D1 in R no
8578 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8580 -- Regardless of whether we dealing with a tagged or untagged type
8581 -- we will transform all derived type declarations of the form
8583 -- type T is new R (...) [with ...];
8584 -- or
8585 -- subtype S is R (...);
8586 -- type T is new S [with ...];
8587 -- into
8588 -- type BT is new R [with ...];
8589 -- subtype T is BT (...);
8591 -- That is, the base derived type is constrained only if it has no
8592 -- discriminants. The reason for doing this is that GNAT's semantic model
8593 -- assumes that a base type with discriminants is unconstrained.
8595 -- Note that, strictly speaking, the above transformation is not always
8596 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8598 -- procedure B34011A is
8599 -- type REC (D : integer := 0) is record
8600 -- I : Integer;
8601 -- end record;
8603 -- package P is
8604 -- type T6 is new Rec;
8605 -- function F return T6;
8606 -- end P;
8608 -- use P;
8609 -- package Q6 is
8610 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8611 -- end Q6;
8613 -- The definition of Q6.U is illegal. However transforming Q6.U into
8615 -- type BaseU is new T6;
8616 -- subtype U is BaseU (Q6.F.I)
8618 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8619 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8620 -- the transformation described above.
8622 -- There is another instance where the above transformation is incorrect.
8623 -- Consider:
8625 -- package Pack is
8626 -- type Base (D : Integer) is tagged null record;
8627 -- procedure P (X : Base);
8629 -- type Der is new Base (2) with null record;
8630 -- procedure P (X : Der);
8631 -- end Pack;
8633 -- Then the above transformation turns this into
8635 -- type Der_Base is new Base with null record;
8636 -- -- procedure P (X : Base) is implicitly inherited here
8637 -- -- as procedure P (X : Der_Base).
8639 -- subtype Der is Der_Base (2);
8640 -- procedure P (X : Der);
8641 -- -- The overriding of P (X : Der_Base) is illegal since we
8642 -- -- have a parameter conformance problem.
8644 -- To get around this problem, after having semantically processed Der_Base
8645 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8646 -- Discriminant_Constraint from Der so that when parameter conformance is
8647 -- checked when P is overridden, no semantic errors are flagged.
8649 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8651 -- Regardless of whether we are dealing with a tagged or untagged type
8652 -- we will transform all derived type declarations of the form
8654 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8655 -- type T is new R [with ...];
8656 -- into
8657 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8659 -- The reason for such transformation is that it allows us to implement a
8660 -- very clean form of component inheritance as explained below.
8662 -- Note that this transformation is not achieved by direct tree rewriting
8663 -- and manipulation, but rather by redoing the semantic actions that the
8664 -- above transformation will entail. This is done directly in routine
8665 -- Inherit_Components.
8667 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8669 -- In both tagged and untagged derived types, regular non discriminant
8670 -- components are inherited in the derived type from the parent type. In
8671 -- the absence of discriminants component, inheritance is straightforward
8672 -- as components can simply be copied from the parent.
8674 -- If the parent has discriminants, inheriting components constrained with
8675 -- these discriminants requires caution. Consider the following example:
8677 -- type R (D1, D2 : Positive) is [tagged] record
8678 -- S : String (D1 .. D2);
8679 -- end record;
8681 -- type T1 is new R [with null record];
8682 -- type T2 (X : positive) is new R (1, X) [with null record];
8684 -- As explained in 6. above, T1 is rewritten as
8685 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8686 -- which makes the treatment for T1 and T2 identical.
8688 -- What we want when inheriting S, is that references to D1 and D2 in R are
8689 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8690 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8691 -- with either discriminant references in the derived type or expressions.
8692 -- This replacement is achieved as follows: before inheriting R's
8693 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8694 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8695 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8696 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8697 -- by String (1 .. X).
8699 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8701 -- We explain here the rules governing private type extensions relevant to
8702 -- type derivation. These rules are explained on the following example:
8704 -- type D [(...)] is new A [(...)] with private; <-- partial view
8705 -- type D [(...)] is new P [(...)] with null record; <-- full view
8707 -- Type A is called the ancestor subtype of the private extension.
8708 -- Type P is the parent type of the full view of the private extension. It
8709 -- must be A or a type derived from A.
8711 -- The rules concerning the discriminants of private type extensions are
8712 -- [7.3(10-13)]:
8714 -- o If a private extension inherits known discriminants from the ancestor
8715 -- subtype, then the full view must also inherit its discriminants from
8716 -- the ancestor subtype and the parent subtype of the full view must be
8717 -- constrained if and only if the ancestor subtype is constrained.
8719 -- o If a partial view has unknown discriminants, then the full view may
8720 -- define a definite or an indefinite subtype, with or without
8721 -- discriminants.
8723 -- o If a partial view has neither known nor unknown discriminants, then
8724 -- the full view must define a definite subtype.
8726 -- o If the ancestor subtype of a private extension has constrained
8727 -- discriminants, then the parent subtype of the full view must impose a
8728 -- statically matching constraint on those discriminants.
8730 -- This means that only the following forms of private extensions are
8731 -- allowed:
8733 -- type D is new A with private; <-- partial view
8734 -- type D is new P with null record; <-- full view
8736 -- If A has no discriminants than P has no discriminants, otherwise P must
8737 -- inherit A's discriminants.
8739 -- type D is new A (...) with private; <-- partial view
8740 -- type D is new P (:::) with null record; <-- full view
8742 -- P must inherit A's discriminants and (...) and (:::) must statically
8743 -- match.
8745 -- subtype A is R (...);
8746 -- type D is new A with private; <-- partial view
8747 -- type D is new P with null record; <-- full view
8749 -- P must have inherited R's discriminants and must be derived from A or
8750 -- any of its subtypes.
8752 -- type D (..) is new A with private; <-- partial view
8753 -- type D (..) is new P [(:::)] with null record; <-- full view
8755 -- No specific constraints on P's discriminants or constraint (:::).
8756 -- Note that A can be unconstrained, but the parent subtype P must either
8757 -- be constrained or (:::) must be present.
8759 -- type D (..) is new A [(...)] with private; <-- partial view
8760 -- type D (..) is new P [(:::)] with null record; <-- full view
8762 -- P's constraints on A's discriminants must statically match those
8763 -- imposed by (...).
8765 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8767 -- The full view of a private extension is handled exactly as described
8768 -- above. The model chose for the private view of a private extension is
8769 -- the same for what concerns discriminants (i.e. they receive the same
8770 -- treatment as in the tagged case). However, the private view of the
8771 -- private extension always inherits the components of the parent base,
8772 -- without replacing any discriminant reference. Strictly speaking this is
8773 -- incorrect. However, Gigi never uses this view to generate code so this
8774 -- is a purely semantic issue. In theory, a set of transformations similar
8775 -- to those given in 5. and 6. above could be applied to private views of
8776 -- private extensions to have the same model of component inheritance as
8777 -- for non private extensions. However, this is not done because it would
8778 -- further complicate private type processing. Semantically speaking, this
8779 -- leaves us in an uncomfortable situation. As an example consider:
8781 -- package Pack is
8782 -- type R (D : integer) is tagged record
8783 -- S : String (1 .. D);
8784 -- end record;
8785 -- procedure P (X : R);
8786 -- type T is new R (1) with private;
8787 -- private
8788 -- type T is new R (1) with null record;
8789 -- end;
8791 -- This is transformed into:
8793 -- package Pack is
8794 -- type R (D : integer) is tagged record
8795 -- S : String (1 .. D);
8796 -- end record;
8797 -- procedure P (X : R);
8798 -- type T is new R (1) with private;
8799 -- private
8800 -- type BaseT is new R with null record;
8801 -- subtype T is BaseT (1);
8802 -- end;
8804 -- (strictly speaking the above is incorrect Ada)
8806 -- From the semantic standpoint the private view of private extension T
8807 -- should be flagged as constrained since one can clearly have
8809 -- Obj : T;
8811 -- in a unit withing Pack. However, when deriving subprograms for the
8812 -- private view of private extension T, T must be seen as unconstrained
8813 -- since T has discriminants (this is a constraint of the current
8814 -- subprogram derivation model). Thus, when processing the private view of
8815 -- a private extension such as T, we first mark T as unconstrained, we
8816 -- process it, we perform program derivation and just before returning from
8817 -- Build_Derived_Record_Type we mark T as constrained.
8819 -- ??? Are there are other uncomfortable cases that we will have to
8820 -- deal with.
8822 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8824 -- Types that are derived from a visible record type and have a private
8825 -- extension present other peculiarities. They behave mostly like private
8826 -- types, but if they have primitive operations defined, these will not
8827 -- have the proper signatures for further inheritance, because other
8828 -- primitive operations will use the implicit base that we define for
8829 -- private derivations below. This affect subprogram inheritance (see
8830 -- Derive_Subprograms for details). We also derive the implicit base from
8831 -- the base type of the full view, so that the implicit base is a record
8832 -- type and not another private type, This avoids infinite loops.
8834 procedure Build_Derived_Record_Type
8835 (N : Node_Id;
8836 Parent_Type : Entity_Id;
8837 Derived_Type : Entity_Id;
8838 Derive_Subps : Boolean := True)
8840 Discriminant_Specs : constant Boolean :=
8841 Present (Discriminant_Specifications (N));
8842 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8843 Loc : constant Source_Ptr := Sloc (N);
8844 Private_Extension : constant Boolean :=
8845 Nkind (N) = N_Private_Extension_Declaration;
8846 Assoc_List : Elist_Id;
8847 Constraint_Present : Boolean;
8848 Constrs : Elist_Id;
8849 Discrim : Entity_Id;
8850 Indic : Node_Id;
8851 Inherit_Discrims : Boolean := False;
8852 Last_Discrim : Entity_Id;
8853 New_Base : Entity_Id;
8854 New_Decl : Node_Id;
8855 New_Discrs : Elist_Id;
8856 New_Indic : Node_Id;
8857 Parent_Base : Entity_Id;
8858 Save_Etype : Entity_Id;
8859 Save_Discr_Constr : Elist_Id;
8860 Save_Next_Entity : Entity_Id;
8861 Type_Def : Node_Id;
8863 Discs : Elist_Id := New_Elmt_List;
8864 -- An empty Discs list means that there were no constraints in the
8865 -- subtype indication or that there was an error processing it.
8867 procedure Check_Generic_Ancestors;
8868 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8869 -- cannot be declared at a deeper level than its parent type is
8870 -- removed. The check on derivation within a generic body is also
8871 -- relaxed, but there's a restriction that a derived tagged type
8872 -- cannot be declared in a generic body if it's derived directly
8873 -- or indirectly from a formal type of that generic. This applies
8874 -- to progenitors as well.
8876 -----------------------------
8877 -- Check_Generic_Ancestors --
8878 -----------------------------
8880 procedure Check_Generic_Ancestors is
8881 Ancestor_Type : Entity_Id;
8882 Intf_List : List_Id;
8883 Intf_Name : Node_Id;
8885 procedure Check_Ancestor;
8886 -- For parent and progenitors.
8888 --------------------
8889 -- Check_Ancestor --
8890 --------------------
8892 procedure Check_Ancestor is
8893 begin
8894 -- If the derived type does have a formal type as an ancestor
8895 -- then it's an error if the derived type is declared within
8896 -- the body of the generic unit that declares the formal type
8897 -- in its generic formal part. It's sufficient to check whether
8898 -- the ancestor type is declared inside the same generic body
8899 -- as the derived type (such as within a nested generic spec),
8900 -- in which case the derivation is legal. If the formal type is
8901 -- declared outside of that generic body, then it's certain
8902 -- that the derived type is declared within the generic body
8903 -- of the generic unit declaring the formal type.
8905 if Is_Generic_Type (Ancestor_Type)
8906 and then Enclosing_Generic_Body (Ancestor_Type) /=
8907 Enclosing_Generic_Body (Derived_Type)
8908 then
8909 Error_Msg_NE
8910 ("ancestor type& is formal type of enclosing"
8911 & " generic unit (RM 3.9.1 (4/2))",
8912 Indic, Ancestor_Type);
8913 end if;
8914 end Check_Ancestor;
8916 begin
8917 if Nkind (N) = N_Private_Extension_Declaration then
8918 Intf_List := Interface_List (N);
8919 else
8920 Intf_List := Interface_List (Type_Definition (N));
8921 end if;
8923 if Present (Enclosing_Generic_Body (Derived_Type)) then
8924 Ancestor_Type := Parent_Type;
8926 while not Is_Generic_Type (Ancestor_Type)
8927 and then Etype (Ancestor_Type) /= Ancestor_Type
8928 loop
8929 Ancestor_Type := Etype (Ancestor_Type);
8930 end loop;
8932 Check_Ancestor;
8934 if Present (Intf_List) then
8935 Intf_Name := First (Intf_List);
8936 while Present (Intf_Name) loop
8937 Ancestor_Type := Entity (Intf_Name);
8938 Check_Ancestor;
8939 Next (Intf_Name);
8940 end loop;
8941 end if;
8942 end if;
8943 end Check_Generic_Ancestors;
8945 -- Start of processing for Build_Derived_Record_Type
8947 begin
8948 if Ekind (Parent_Type) = E_Record_Type_With_Private
8949 and then Present (Full_View (Parent_Type))
8950 and then Has_Discriminants (Parent_Type)
8951 then
8952 Parent_Base := Base_Type (Full_View (Parent_Type));
8953 else
8954 Parent_Base := Base_Type (Parent_Type);
8955 end if;
8957 -- If the parent type is declared as a subtype of another private
8958 -- type with inherited discriminants, its generated base type is
8959 -- itself a record subtype. To further inherit the constraint we
8960 -- need to use its own base to have an unconstrained type on which
8961 -- to apply the inherited constraint.
8963 if Ekind (Parent_Base) = E_Record_Subtype then
8964 Parent_Base := Base_Type (Parent_Base);
8965 end if;
8967 -- AI05-0115: if this is a derivation from a private type in some
8968 -- other scope that may lead to invisible components for the derived
8969 -- type, mark it accordingly.
8971 if Is_Private_Type (Parent_Type) then
8972 if Scope (Parent_Base) = Scope (Derived_Type) then
8973 null;
8975 elsif In_Open_Scopes (Scope (Parent_Base))
8976 and then In_Private_Part (Scope (Parent_Base))
8977 then
8978 null;
8980 else
8981 Set_Has_Private_Ancestor (Derived_Type);
8982 end if;
8984 else
8985 Set_Has_Private_Ancestor
8986 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8987 end if;
8989 -- Before we start the previously documented transformations, here is
8990 -- little fix for size and alignment of tagged types. Normally when we
8991 -- derive type D from type P, we copy the size and alignment of P as the
8992 -- default for D, and in the absence of explicit representation clauses
8993 -- for D, the size and alignment are indeed the same as the parent.
8995 -- But this is wrong for tagged types, since fields may be added, and
8996 -- the default size may need to be larger, and the default alignment may
8997 -- need to be larger.
8999 -- We therefore reset the size and alignment fields in the tagged case.
9000 -- Note that the size and alignment will in any case be at least as
9001 -- large as the parent type (since the derived type has a copy of the
9002 -- parent type in the _parent field)
9004 -- The type is also marked as being tagged here, which is needed when
9005 -- processing components with a self-referential anonymous access type
9006 -- in the call to Check_Anonymous_Access_Components below. Note that
9007 -- this flag is also set later on for completeness.
9009 if Is_Tagged then
9010 Set_Is_Tagged_Type (Derived_Type);
9011 Reinit_Size_Align (Derived_Type);
9012 end if;
9014 -- STEP 0a: figure out what kind of derived type declaration we have
9016 if Private_Extension then
9017 Type_Def := N;
9018 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9019 Set_Default_SSO (Derived_Type);
9020 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9022 else
9023 Type_Def := Type_Definition (N);
9025 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9026 -- Parent_Base can be a private type or private extension. However,
9027 -- for tagged types with an extension the newly added fields are
9028 -- visible and hence the Derived_Type is always an E_Record_Type.
9029 -- (except that the parent may have its own private fields).
9030 -- For untagged types we preserve the Ekind of the Parent_Base.
9032 if Present (Record_Extension_Part (Type_Def)) then
9033 Mutate_Ekind (Derived_Type, E_Record_Type);
9034 Set_Default_SSO (Derived_Type);
9035 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9037 -- Create internal access types for components with anonymous
9038 -- access types.
9040 if Ada_Version >= Ada_2005 then
9041 Check_Anonymous_Access_Components
9042 (N, Derived_Type, Derived_Type,
9043 Component_List (Record_Extension_Part (Type_Def)));
9044 end if;
9046 else
9047 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9048 end if;
9049 end if;
9051 -- Indic can either be an N_Identifier if the subtype indication
9052 -- contains no constraint or an N_Subtype_Indication if the subtype
9053 -- indication has a constraint. In either case it can include an
9054 -- interface list.
9056 Indic := Subtype_Indication (Type_Def);
9057 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9059 -- Check that the type has visible discriminants. The type may be
9060 -- a private type with unknown discriminants whose full view has
9061 -- discriminants which are invisible.
9063 if Constraint_Present then
9064 if not Has_Discriminants (Parent_Base)
9065 or else
9066 (Has_Unknown_Discriminants (Parent_Base)
9067 and then Is_Private_Type (Parent_Base))
9068 then
9069 Error_Msg_N
9070 ("invalid constraint: type has no discriminant",
9071 Constraint (Indic));
9073 Constraint_Present := False;
9074 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9076 elsif Is_Constrained (Parent_Type) then
9077 Error_Msg_N
9078 ("invalid constraint: parent type is already constrained",
9079 Constraint (Indic));
9081 Constraint_Present := False;
9082 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9083 end if;
9084 end if;
9086 -- STEP 0b: If needed, apply transformation given in point 5. above
9088 if not Private_Extension
9089 and then Has_Discriminants (Parent_Type)
9090 and then not Discriminant_Specs
9091 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9092 then
9093 -- First, we must analyze the constraint (see comment in point 5.)
9094 -- The constraint may come from the subtype indication of the full
9095 -- declaration.
9097 if Constraint_Present then
9098 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9100 -- If there is no explicit constraint, there might be one that is
9101 -- inherited from a constrained parent type. In that case verify that
9102 -- it conforms to the constraint in the partial view. In perverse
9103 -- cases the parent subtypes of the partial and full view can have
9104 -- different constraints.
9106 elsif Present (Stored_Constraint (Parent_Type)) then
9107 New_Discrs := Stored_Constraint (Parent_Type);
9109 else
9110 New_Discrs := No_Elist;
9111 end if;
9113 if Has_Discriminants (Derived_Type)
9114 and then Has_Private_Declaration (Derived_Type)
9115 and then Present (Discriminant_Constraint (Derived_Type))
9116 and then Present (New_Discrs)
9117 then
9118 -- Verify that constraints of the full view statically match
9119 -- those given in the partial view.
9121 declare
9122 C1, C2 : Elmt_Id;
9124 begin
9125 C1 := First_Elmt (New_Discrs);
9126 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9127 while Present (C1) and then Present (C2) loop
9128 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9129 or else
9130 (Is_OK_Static_Expression (Node (C1))
9131 and then Is_OK_Static_Expression (Node (C2))
9132 and then
9133 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9134 then
9135 null;
9137 else
9138 if Constraint_Present then
9139 Error_Msg_N
9140 ("constraint not conformant to previous declaration",
9141 Node (C1));
9142 else
9143 Error_Msg_N
9144 ("constraint of full view is incompatible "
9145 & "with partial view", N);
9146 end if;
9147 end if;
9149 Next_Elmt (C1);
9150 Next_Elmt (C2);
9151 end loop;
9152 end;
9153 end if;
9155 -- Insert and analyze the declaration for the unconstrained base type
9157 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9159 New_Decl :=
9160 Make_Full_Type_Declaration (Loc,
9161 Defining_Identifier => New_Base,
9162 Type_Definition =>
9163 Make_Derived_Type_Definition (Loc,
9164 Abstract_Present => Abstract_Present (Type_Def),
9165 Limited_Present => Limited_Present (Type_Def),
9166 Subtype_Indication =>
9167 New_Occurrence_Of (Parent_Base, Loc),
9168 Record_Extension_Part =>
9169 Relocate_Node (Record_Extension_Part (Type_Def)),
9170 Interface_List => Interface_List (Type_Def)));
9172 Set_Parent (New_Decl, Parent (N));
9173 Mark_Rewrite_Insertion (New_Decl);
9174 Insert_Before (N, New_Decl);
9176 -- In the extension case, make sure ancestor is frozen appropriately
9177 -- (see also non-discriminated case below).
9179 if Present (Record_Extension_Part (Type_Def))
9180 or else Is_Interface (Parent_Base)
9181 then
9182 Freeze_Before (New_Decl, Parent_Type);
9183 end if;
9185 -- Note that this call passes False for the Derive_Subps parameter
9186 -- because subprogram derivation is deferred until after creating
9187 -- the subtype (see below).
9189 Build_Derived_Type
9190 (New_Decl, Parent_Base, New_Base,
9191 Is_Completion => False, Derive_Subps => False);
9193 -- ??? This needs re-examination to determine whether the
9194 -- above call can simply be replaced by a call to Analyze.
9196 Set_Analyzed (New_Decl);
9198 -- Insert and analyze the declaration for the constrained subtype
9200 if Constraint_Present then
9201 New_Indic :=
9202 Make_Subtype_Indication (Loc,
9203 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9204 Constraint => Relocate_Node (Constraint (Indic)));
9206 else
9207 declare
9208 Constr_List : constant List_Id := New_List;
9209 C : Elmt_Id;
9210 Expr : Node_Id;
9212 begin
9213 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9214 while Present (C) loop
9215 Expr := Node (C);
9217 -- It is safe here to call New_Copy_Tree since we called
9218 -- Force_Evaluation on each constraint previously
9219 -- in Build_Discriminant_Constraints.
9221 Append (New_Copy_Tree (Expr), To => Constr_List);
9223 Next_Elmt (C);
9224 end loop;
9226 New_Indic :=
9227 Make_Subtype_Indication (Loc,
9228 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9229 Constraint =>
9230 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9231 end;
9232 end if;
9234 Rewrite (N,
9235 Make_Subtype_Declaration (Loc,
9236 Defining_Identifier => Derived_Type,
9237 Subtype_Indication => New_Indic));
9239 Analyze (N);
9241 -- Derivation of subprograms must be delayed until the full subtype
9242 -- has been established, to ensure proper overriding of subprograms
9243 -- inherited by full types. If the derivations occurred as part of
9244 -- the call to Build_Derived_Type above, then the check for type
9245 -- conformance would fail because earlier primitive subprograms
9246 -- could still refer to the full type prior the change to the new
9247 -- subtype and hence would not match the new base type created here.
9248 -- Subprograms are not derived, however, when Derive_Subps is False
9249 -- (since otherwise there could be redundant derivations).
9251 if Derive_Subps then
9252 Derive_Subprograms (Parent_Type, Derived_Type);
9253 end if;
9255 -- For tagged types the Discriminant_Constraint of the new base itype
9256 -- is inherited from the first subtype so that no subtype conformance
9257 -- problem arise when the first subtype overrides primitive
9258 -- operations inherited by the implicit base type.
9260 if Is_Tagged then
9261 Set_Discriminant_Constraint
9262 (New_Base, Discriminant_Constraint (Derived_Type));
9263 end if;
9265 return;
9266 end if;
9268 -- If we get here Derived_Type will have no discriminants or it will be
9269 -- a discriminated unconstrained base type.
9271 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9273 if Is_Tagged then
9275 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9276 -- The declaration of a specific descendant of an interface type
9277 -- freezes the interface type (RM 13.14).
9279 if not Private_Extension or else Is_Interface (Parent_Base) then
9280 Freeze_Before (N, Parent_Type);
9281 end if;
9283 if Ada_Version >= Ada_2005 then
9284 Check_Generic_Ancestors;
9286 elsif Type_Access_Level (Derived_Type) /=
9287 Type_Access_Level (Parent_Type)
9288 and then not Is_Generic_Type (Derived_Type)
9289 then
9290 if Is_Controlled (Parent_Type) then
9291 Error_Msg_N
9292 ("controlled type must be declared at the library level",
9293 Indic);
9294 else
9295 Error_Msg_N
9296 ("type extension at deeper accessibility level than parent",
9297 Indic);
9298 end if;
9300 else
9301 declare
9302 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9303 begin
9304 if Present (GB)
9305 and then GB /= Enclosing_Generic_Body (Parent_Base)
9306 then
9307 Error_Msg_NE
9308 ("parent type of& must not be outside generic body"
9309 & " (RM 3.9.1(4))",
9310 Indic, Derived_Type);
9311 end if;
9312 end;
9313 end if;
9314 end if;
9316 -- Ada 2005 (AI-251)
9318 if Ada_Version >= Ada_2005 and then Is_Tagged then
9320 -- "The declaration of a specific descendant of an interface type
9321 -- freezes the interface type" (RM 13.14).
9323 declare
9324 Iface : Node_Id;
9325 begin
9326 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9327 Iface := First (Interface_List (Type_Def));
9328 while Present (Iface) loop
9329 Freeze_Before (N, Etype (Iface));
9330 Next (Iface);
9331 end loop;
9332 end if;
9333 end;
9334 end if;
9336 -- STEP 1b : preliminary cleanup of the full view of private types
9338 -- If the type is already marked as having discriminants, then it's the
9339 -- completion of a private type or private extension and we need to
9340 -- retain the discriminants from the partial view if the current
9341 -- declaration has Discriminant_Specifications so that we can verify
9342 -- conformance. However, we must remove any existing components that
9343 -- were inherited from the parent (and attached in Copy_And_Swap)
9344 -- because the full type inherits all appropriate components anyway, and
9345 -- we do not want the partial view's components interfering.
9347 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9348 Discrim := First_Discriminant (Derived_Type);
9349 loop
9350 Last_Discrim := Discrim;
9351 Next_Discriminant (Discrim);
9352 exit when No (Discrim);
9353 end loop;
9355 Set_Last_Entity (Derived_Type, Last_Discrim);
9357 -- In all other cases wipe out the list of inherited components (even
9358 -- inherited discriminants), it will be properly rebuilt here.
9360 else
9361 Set_First_Entity (Derived_Type, Empty);
9362 Set_Last_Entity (Derived_Type, Empty);
9363 end if;
9365 -- STEP 1c: Initialize some flags for the Derived_Type
9367 -- The following flags must be initialized here so that
9368 -- Process_Discriminants can check that discriminants of tagged types do
9369 -- not have a default initial value and that access discriminants are
9370 -- only specified for limited records. For completeness, these flags are
9371 -- also initialized along with all the other flags below.
9373 -- AI-419: Limitedness is not inherited from an interface parent, so to
9374 -- be limited in that case the type must be explicitly declared as
9375 -- limited. However, task and protected interfaces are always limited.
9377 if Limited_Present (Type_Def) then
9378 Set_Is_Limited_Record (Derived_Type);
9380 elsif Is_Limited_Record (Parent_Type)
9381 or else (Present (Full_View (Parent_Type))
9382 and then Is_Limited_Record (Full_View (Parent_Type)))
9383 then
9384 if not Is_Interface (Parent_Type)
9385 or else Is_Concurrent_Interface (Parent_Type)
9386 then
9387 Set_Is_Limited_Record (Derived_Type);
9388 end if;
9389 end if;
9391 -- STEP 2a: process discriminants of derived type if any
9393 Push_Scope (Derived_Type);
9395 if Discriminant_Specs then
9396 Set_Has_Unknown_Discriminants (Derived_Type, False);
9398 -- The following call initializes fields Has_Discriminants and
9399 -- Discriminant_Constraint, unless we are processing the completion
9400 -- of a private type declaration.
9402 Check_Or_Process_Discriminants (N, Derived_Type);
9404 -- For untagged types, the constraint on the Parent_Type must be
9405 -- present and is used to rename the discriminants.
9407 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9408 Error_Msg_N ("untagged parent must have discriminants", Indic);
9410 elsif not Is_Tagged and then not Constraint_Present then
9411 Error_Msg_N
9412 ("discriminant constraint needed for derived untagged records",
9413 Indic);
9415 -- Otherwise the parent subtype must be constrained unless we have a
9416 -- private extension.
9418 elsif not Constraint_Present
9419 and then not Private_Extension
9420 and then not Is_Constrained (Parent_Type)
9421 then
9422 Error_Msg_N
9423 ("unconstrained type not allowed in this context", Indic);
9425 elsif Constraint_Present then
9426 -- The following call sets the field Corresponding_Discriminant
9427 -- for the discriminants in the Derived_Type.
9429 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9431 -- For untagged types all new discriminants must rename
9432 -- discriminants in the parent. For private extensions new
9433 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9435 Discrim := First_Discriminant (Derived_Type);
9436 while Present (Discrim) loop
9437 if not Is_Tagged
9438 and then No (Corresponding_Discriminant (Discrim))
9439 then
9440 Error_Msg_N
9441 ("new discriminants must constrain old ones", Discrim);
9443 elsif Private_Extension
9444 and then Present (Corresponding_Discriminant (Discrim))
9445 then
9446 Error_Msg_N
9447 ("only static constraints allowed for parent"
9448 & " discriminants in the partial view", Indic);
9449 exit;
9450 end if;
9452 -- If a new discriminant is used in the constraint, then its
9453 -- subtype must be statically compatible with the subtype of
9454 -- the parent discriminant (RM 3.7(15)).
9456 if Present (Corresponding_Discriminant (Discrim)) then
9457 Check_Constraining_Discriminant
9458 (Discrim, Corresponding_Discriminant (Discrim));
9459 end if;
9461 Next_Discriminant (Discrim);
9462 end loop;
9464 -- Check whether the constraints of the full view statically
9465 -- match those imposed by the parent subtype [7.3(13)].
9467 if Present (Stored_Constraint (Derived_Type)) then
9468 declare
9469 C1, C2 : Elmt_Id;
9471 begin
9472 C1 := First_Elmt (Discs);
9473 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9474 while Present (C1) and then Present (C2) loop
9475 if not
9476 Fully_Conformant_Expressions (Node (C1), Node (C2))
9477 then
9478 Error_Msg_N
9479 ("not conformant with previous declaration",
9480 Node (C1));
9481 end if;
9483 Next_Elmt (C1);
9484 Next_Elmt (C2);
9485 end loop;
9486 end;
9487 end if;
9488 end if;
9490 -- STEP 2b: No new discriminants, inherit discriminants if any
9492 else
9493 if Private_Extension then
9494 Set_Has_Unknown_Discriminants
9495 (Derived_Type,
9496 Has_Unknown_Discriminants (Parent_Type)
9497 or else Unknown_Discriminants_Present (N));
9499 -- The partial view of the parent may have unknown discriminants,
9500 -- but if the full view has discriminants and the parent type is
9501 -- in scope they must be inherited.
9503 elsif Has_Unknown_Discriminants (Parent_Type)
9504 and then
9505 (not Has_Discriminants (Parent_Type)
9506 or else not In_Open_Scopes (Scope (Parent_Base)))
9507 then
9508 Set_Has_Unknown_Discriminants (Derived_Type);
9509 end if;
9511 if not Has_Unknown_Discriminants (Derived_Type)
9512 and then not Has_Unknown_Discriminants (Parent_Base)
9513 and then Has_Discriminants (Parent_Type)
9514 then
9515 Inherit_Discrims := True;
9516 Set_Has_Discriminants
9517 (Derived_Type, True);
9518 Set_Discriminant_Constraint
9519 (Derived_Type, Discriminant_Constraint (Parent_Base));
9520 end if;
9522 -- The following test is true for private types (remember
9523 -- transformation 5. is not applied to those) and in an error
9524 -- situation.
9526 if Constraint_Present then
9527 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9528 end if;
9530 -- For now mark a new derived type as constrained only if it has no
9531 -- discriminants. At the end of Build_Derived_Record_Type we properly
9532 -- set this flag in the case of private extensions. See comments in
9533 -- point 9. just before body of Build_Derived_Record_Type.
9535 Set_Is_Constrained
9536 (Derived_Type,
9537 not (Inherit_Discrims
9538 or else Has_Unknown_Discriminants (Derived_Type)));
9539 end if;
9541 -- STEP 3: initialize fields of derived type
9543 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9544 Set_Stored_Constraint (Derived_Type, No_Elist);
9546 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9547 -- but cannot be interfaces
9549 if not Private_Extension
9550 and then Ekind (Derived_Type) /= E_Private_Type
9551 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9552 then
9553 if Interface_Present (Type_Def) then
9554 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9555 end if;
9557 Set_Interfaces (Derived_Type, No_Elist);
9558 end if;
9560 -- Fields inherited from the Parent_Type
9562 Set_Has_Specified_Layout
9563 (Derived_Type, Has_Specified_Layout (Parent_Type));
9564 Set_Is_Limited_Composite
9565 (Derived_Type, Is_Limited_Composite (Parent_Type));
9566 Set_Is_Private_Composite
9567 (Derived_Type, Is_Private_Composite (Parent_Type));
9569 if Is_Tagged_Type (Parent_Type) then
9570 Set_No_Tagged_Streams_Pragma
9571 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9572 end if;
9574 -- Fields inherited from the Parent_Base
9576 Set_Has_Controlled_Component
9577 (Derived_Type, Has_Controlled_Component (Parent_Base));
9578 Set_Has_Non_Standard_Rep
9579 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9580 Set_Has_Primitive_Operations
9581 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9583 -- Set fields for private derived types
9585 if Is_Private_Type (Derived_Type) then
9586 Set_Depends_On_Private (Derived_Type, True);
9587 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9588 end if;
9590 -- Inherit fields for non-private types. If this is the completion of a
9591 -- derivation from a private type, the parent itself is private and the
9592 -- attributes come from its full view, which must be present.
9594 if Is_Record_Type (Derived_Type) then
9595 declare
9596 Parent_Full : Entity_Id;
9598 begin
9599 if Is_Private_Type (Parent_Base)
9600 and then not Is_Record_Type (Parent_Base)
9601 then
9602 Parent_Full := Full_View (Parent_Base);
9603 else
9604 Parent_Full := Parent_Base;
9605 end if;
9607 Set_Component_Alignment
9608 (Derived_Type, Component_Alignment (Parent_Full));
9609 Set_C_Pass_By_Copy
9610 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9611 Set_Has_Complex_Representation
9612 (Derived_Type, Has_Complex_Representation (Parent_Full));
9614 -- For untagged types, inherit the layout by default to avoid
9615 -- costly changes of representation for type conversions.
9617 if not Is_Tagged then
9618 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9619 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9620 end if;
9621 end;
9622 end if;
9624 -- When prefixed-call syntax is allowed for untagged types, initialize
9625 -- the list of primitive operations to an empty list.
9627 if Extensions_Allowed and then not Is_Tagged then
9628 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9629 end if;
9631 -- Set fields for tagged types
9633 if Is_Tagged then
9634 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9636 -- All tagged types defined in Ada.Finalization are controlled
9638 if Chars (Scope (Derived_Type)) = Name_Finalization
9639 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9640 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9641 then
9642 Set_Is_Controlled_Active (Derived_Type);
9643 else
9644 Set_Is_Controlled_Active
9645 (Derived_Type, Is_Controlled_Active (Parent_Base));
9646 end if;
9648 -- Minor optimization: there is no need to generate the class-wide
9649 -- entity associated with an underlying record view.
9651 if not Is_Underlying_Record_View (Derived_Type) then
9652 Make_Class_Wide_Type (Derived_Type);
9653 end if;
9655 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9657 if Has_Discriminants (Derived_Type)
9658 and then Constraint_Present
9659 then
9660 Set_Stored_Constraint
9661 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9662 end if;
9664 if Ada_Version >= Ada_2005 then
9665 declare
9666 Ifaces_List : Elist_Id;
9668 begin
9669 -- Checks rules 3.9.4 (13/2 and 14/2)
9671 if Comes_From_Source (Derived_Type)
9672 and then not Is_Private_Type (Derived_Type)
9673 and then Is_Interface (Parent_Type)
9674 and then not Is_Interface (Derived_Type)
9675 then
9676 if Is_Task_Interface (Parent_Type) then
9677 Error_Msg_N
9678 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9679 Derived_Type);
9681 elsif Is_Protected_Interface (Parent_Type) then
9682 Error_Msg_N
9683 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9684 Derived_Type);
9685 end if;
9686 end if;
9688 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9690 Check_Interfaces (N, Type_Def);
9692 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9693 -- not already in the parents.
9695 Collect_Interfaces
9696 (T => Derived_Type,
9697 Ifaces_List => Ifaces_List,
9698 Exclude_Parents => True);
9700 Set_Interfaces (Derived_Type, Ifaces_List);
9702 -- If the derived type is the anonymous type created for
9703 -- a declaration whose parent has a constraint, propagate
9704 -- the interface list to the source type. This must be done
9705 -- prior to the completion of the analysis of the source type
9706 -- because the components in the extension may contain current
9707 -- instances whose legality depends on some ancestor.
9709 if Is_Itype (Derived_Type) then
9710 declare
9711 Def : constant Node_Id :=
9712 Associated_Node_For_Itype (Derived_Type);
9713 begin
9714 if Present (Def)
9715 and then Nkind (Def) = N_Full_Type_Declaration
9716 then
9717 Set_Interfaces
9718 (Defining_Identifier (Def), Ifaces_List);
9719 end if;
9720 end;
9721 end if;
9723 -- A type extension is automatically Ghost when one of its
9724 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9725 -- also inherited when the parent type is Ghost, but this is
9726 -- done in Build_Derived_Type as the mechanism also handles
9727 -- untagged derivations.
9729 if Implements_Ghost_Interface (Derived_Type) then
9730 Set_Is_Ghost_Entity (Derived_Type);
9731 end if;
9732 end;
9733 end if;
9734 end if;
9736 -- STEP 4: Inherit components from the parent base and constrain them.
9737 -- Apply the second transformation described in point 6. above.
9739 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9740 or else not Has_Discriminants (Parent_Type)
9741 or else not Is_Constrained (Parent_Type)
9742 then
9743 Constrs := Discs;
9744 else
9745 Constrs := Discriminant_Constraint (Parent_Type);
9746 end if;
9748 Assoc_List :=
9749 Inherit_Components
9750 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9752 -- STEP 5a: Copy the parent record declaration for untagged types
9754 Set_Has_Implicit_Dereference
9755 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9757 if not Is_Tagged then
9759 -- Discriminant_Constraint (Derived_Type) has been properly
9760 -- constructed. Save it and temporarily set it to Empty because we
9761 -- do not want the call to New_Copy_Tree below to mess this list.
9763 if Has_Discriminants (Derived_Type) then
9764 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9765 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9766 else
9767 Save_Discr_Constr := No_Elist;
9768 end if;
9770 -- Save the Etype field of Derived_Type. It is correctly set now,
9771 -- but the call to New_Copy tree may remap it to point to itself,
9772 -- which is not what we want. Ditto for the Next_Entity field.
9774 Save_Etype := Etype (Derived_Type);
9775 Save_Next_Entity := Next_Entity (Derived_Type);
9777 -- Assoc_List maps all stored discriminants in the Parent_Base to
9778 -- stored discriminants in the Derived_Type. It is fundamental that
9779 -- no types or itypes with discriminants other than the stored
9780 -- discriminants appear in the entities declared inside
9781 -- Derived_Type, since the back end cannot deal with it.
9783 New_Decl :=
9784 New_Copy_Tree
9785 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9786 Copy_Dimensions_Of_Components (Derived_Type);
9788 -- Restore the fields saved prior to the New_Copy_Tree call
9789 -- and compute the stored constraint.
9791 Set_Etype (Derived_Type, Save_Etype);
9792 Link_Entities (Derived_Type, Save_Next_Entity);
9794 if Has_Discriminants (Derived_Type) then
9795 Set_Discriminant_Constraint
9796 (Derived_Type, Save_Discr_Constr);
9797 Set_Stored_Constraint
9798 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9800 Replace_Discriminants (Derived_Type, New_Decl);
9801 end if;
9803 -- Insert the new derived type declaration
9805 Rewrite (N, New_Decl);
9807 -- STEP 5b: Complete the processing for record extensions in generics
9809 -- There is no completion for record extensions declared in the
9810 -- parameter part of a generic, so we need to complete processing for
9811 -- these generic record extensions here. The Record_Type_Definition call
9812 -- will change the Ekind of the components from E_Void to E_Component.
9814 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9815 Record_Type_Definition (Empty, Derived_Type);
9817 -- STEP 5c: Process the record extension for non private tagged types
9819 elsif not Private_Extension then
9820 Expand_Record_Extension (Derived_Type, Type_Def);
9822 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9823 -- implemented interfaces if we are in expansion mode
9825 if Expander_Active
9826 and then Has_Interfaces (Derived_Type)
9827 then
9828 Add_Interface_Tag_Components (N, Derived_Type);
9829 end if;
9831 -- Analyze the record extension
9833 Record_Type_Definition
9834 (Record_Extension_Part (Type_Def), Derived_Type);
9835 end if;
9837 End_Scope;
9839 -- Nothing else to do if there is an error in the derivation.
9840 -- An unusual case: the full view may be derived from a type in an
9841 -- instance, when the partial view was used illegally as an actual
9842 -- in that instance, leading to a circular definition.
9844 if Etype (Derived_Type) = Any_Type
9845 or else Etype (Parent_Type) = Derived_Type
9846 then
9847 return;
9848 end if;
9850 -- Set delayed freeze and then derive subprograms, we need to do
9851 -- this in this order so that derived subprograms inherit the
9852 -- derived freeze if necessary.
9854 Set_Has_Delayed_Freeze (Derived_Type);
9856 if Derive_Subps then
9857 Derive_Subprograms (Parent_Type, Derived_Type);
9858 end if;
9860 -- If we have a private extension which defines a constrained derived
9861 -- type mark as constrained here after we have derived subprograms. See
9862 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9864 if Private_Extension and then Inherit_Discrims then
9865 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9866 Set_Is_Constrained (Derived_Type, True);
9867 Set_Discriminant_Constraint (Derived_Type, Discs);
9869 elsif Is_Constrained (Parent_Type) then
9870 Set_Is_Constrained
9871 (Derived_Type, True);
9872 Set_Discriminant_Constraint
9873 (Derived_Type, Discriminant_Constraint (Parent_Type));
9874 end if;
9875 end if;
9877 -- Update the class-wide type, which shares the now-completed entity
9878 -- list with its specific type. In case of underlying record views,
9879 -- we do not generate the corresponding class wide entity.
9881 if Is_Tagged
9882 and then not Is_Underlying_Record_View (Derived_Type)
9883 then
9884 Set_First_Entity
9885 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9886 Set_Last_Entity
9887 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9888 end if;
9890 Check_Function_Writable_Actuals (N);
9891 end Build_Derived_Record_Type;
9893 ------------------------
9894 -- Build_Derived_Type --
9895 ------------------------
9897 procedure Build_Derived_Type
9898 (N : Node_Id;
9899 Parent_Type : Entity_Id;
9900 Derived_Type : Entity_Id;
9901 Is_Completion : Boolean;
9902 Derive_Subps : Boolean := True)
9904 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9906 begin
9907 -- Set common attributes
9909 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
9910 and then Ekind (Parent_Base) in Modular_Integer_Kind | Array_Kind
9911 then
9912 Reinit_Field_To_Zero (Derived_Type, F_Stored_Constraint);
9913 end if;
9915 Set_Scope (Derived_Type, Current_Scope);
9916 Set_Etype (Derived_Type, Parent_Base);
9917 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9918 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9920 Set_Size_Info (Derived_Type, Parent_Type);
9921 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
9923 Set_Is_Controlled_Active
9924 (Derived_Type, Is_Controlled_Active (Parent_Type));
9926 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9927 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9928 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9930 if Is_Tagged_Type (Derived_Type) then
9931 Set_No_Tagged_Streams_Pragma
9932 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9933 end if;
9935 -- If the parent has primitive routines and may have not-seen-yet aspect
9936 -- specifications (e.g., a Pack pragma), then set the derived type link
9937 -- in order to later diagnose "early derivation" issues. If in different
9938 -- compilation units, then "early derivation" cannot be an issue (and we
9939 -- don't like interunit references that go in the opposite direction of
9940 -- semantic dependencies).
9942 if Has_Primitive_Operations (Parent_Type)
9943 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9944 Enclosing_Comp_Unit_Node (Derived_Type)
9945 then
9946 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9947 end if;
9949 -- If the parent type is a private subtype, the convention on the base
9950 -- type may be set in the private part, and not propagated to the
9951 -- subtype until later, so we obtain the convention from the base type.
9953 Set_Convention (Derived_Type, Convention (Parent_Base));
9955 if Is_Tagged_Type (Derived_Type)
9956 and then Present (Class_Wide_Type (Derived_Type))
9957 then
9958 Set_Convention (Class_Wide_Type (Derived_Type),
9959 Convention (Class_Wide_Type (Parent_Base)));
9960 end if;
9962 -- Set SSO default for record or array type
9964 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9965 and then Is_Base_Type (Derived_Type)
9966 then
9967 Set_Default_SSO (Derived_Type);
9968 end if;
9970 -- A derived type inherits the Default_Initial_Condition pragma coming
9971 -- from any parent type within the derivation chain.
9973 if Has_DIC (Parent_Type) then
9974 Set_Has_Inherited_DIC (Derived_Type);
9975 end if;
9977 -- A derived type inherits any class-wide invariants coming from a
9978 -- parent type or an interface. Note that the invariant procedure of
9979 -- the parent type should not be inherited because the derived type may
9980 -- define invariants of its own.
9982 if not Is_Interface (Derived_Type) then
9983 if Has_Inherited_Invariants (Parent_Type)
9984 or else Has_Inheritable_Invariants (Parent_Type)
9985 then
9986 Set_Has_Inherited_Invariants (Derived_Type);
9988 elsif Is_Concurrent_Type (Derived_Type)
9989 or else Is_Tagged_Type (Derived_Type)
9990 then
9991 declare
9992 Iface : Entity_Id;
9993 Ifaces : Elist_Id;
9994 Iface_Elmt : Elmt_Id;
9996 begin
9997 Collect_Interfaces
9998 (T => Derived_Type,
9999 Ifaces_List => Ifaces,
10000 Exclude_Parents => True);
10002 if Present (Ifaces) then
10003 Iface_Elmt := First_Elmt (Ifaces);
10004 while Present (Iface_Elmt) loop
10005 Iface := Node (Iface_Elmt);
10007 if Has_Inheritable_Invariants (Iface) then
10008 Set_Has_Inherited_Invariants (Derived_Type);
10009 exit;
10010 end if;
10012 Next_Elmt (Iface_Elmt);
10013 end loop;
10014 end if;
10015 end;
10016 end if;
10017 end if;
10019 -- We similarly inherit predicates. Note that for scalar derived types
10020 -- the predicate is inherited from the first subtype, and not from its
10021 -- (anonymous) base type.
10023 if Has_Predicates (Parent_Type)
10024 or else Has_Predicates (First_Subtype (Parent_Type))
10025 then
10026 Set_Has_Predicates (Derived_Type);
10027 end if;
10029 -- The derived type inherits representation clauses from the parent
10030 -- type, and from any interfaces.
10032 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10034 declare
10035 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10036 begin
10037 while Present (Iface) loop
10038 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10039 Next (Iface);
10040 end loop;
10041 end;
10043 -- If the parent type has delayed rep aspects, then mark the derived
10044 -- type as possibly inheriting a delayed rep aspect.
10046 if Has_Delayed_Rep_Aspects (Parent_Type) then
10047 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10048 end if;
10050 -- A derived type becomes Ghost when its parent type is also Ghost
10051 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10052 -- directly inherited because the Ghost policy in effect may differ.
10054 if Is_Ghost_Entity (Parent_Type) then
10055 Set_Is_Ghost_Entity (Derived_Type);
10056 end if;
10058 -- Type dependent processing
10060 case Ekind (Parent_Type) is
10061 when Numeric_Kind =>
10062 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10064 when Array_Kind =>
10065 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10067 when Class_Wide_Kind
10068 | E_Record_Subtype
10069 | E_Record_Type
10071 Build_Derived_Record_Type
10072 (N, Parent_Type, Derived_Type, Derive_Subps);
10073 return;
10075 when Enumeration_Kind =>
10076 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10078 when Access_Kind =>
10079 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10081 when Incomplete_Or_Private_Kind =>
10082 Build_Derived_Private_Type
10083 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10085 -- For discriminated types, the derivation includes deriving
10086 -- primitive operations. For others it is done below.
10088 if Is_Tagged_Type (Parent_Type)
10089 or else Has_Discriminants (Parent_Type)
10090 or else (Present (Full_View (Parent_Type))
10091 and then Has_Discriminants (Full_View (Parent_Type)))
10092 then
10093 return;
10094 end if;
10096 when Concurrent_Kind =>
10097 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10099 when others =>
10100 raise Program_Error;
10101 end case;
10103 -- Nothing more to do if some error occurred
10105 if Etype (Derived_Type) = Any_Type then
10106 return;
10107 end if;
10109 -- If not already set, initialize the derived type's list of primitive
10110 -- operations to an empty element list.
10112 if not Present (Direct_Primitive_Operations (Derived_Type)) then
10113 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10115 -- If Etype of the derived type is the base type (as opposed to
10116 -- a parent type) and doesn't have an associated list of primitive
10117 -- operations, then set the base type's primitive list to the
10118 -- derived type's list. The lists need to be shared in common
10119 -- between the two.
10121 if Etype (Derived_Type) = Base_Type (Derived_Type)
10122 and then
10123 not Present (Direct_Primitive_Operations (Etype (Derived_Type)))
10124 then
10125 Set_Direct_Primitive_Operations
10126 (Etype (Derived_Type),
10127 Direct_Primitive_Operations (Derived_Type));
10128 end if;
10129 end if;
10131 -- Set delayed freeze and then derive subprograms, we need to do this
10132 -- in this order so that derived subprograms inherit the derived freeze
10133 -- if necessary.
10135 Set_Has_Delayed_Freeze (Derived_Type);
10137 if Derive_Subps then
10138 Derive_Subprograms (Parent_Type, Derived_Type);
10139 end if;
10141 Set_Has_Primitive_Operations
10142 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10143 end Build_Derived_Type;
10145 -----------------------
10146 -- Build_Discriminal --
10147 -----------------------
10149 procedure Build_Discriminal (Discrim : Entity_Id) is
10150 D_Minal : Entity_Id;
10151 CR_Disc : Entity_Id;
10153 begin
10154 -- A discriminal has the same name as the discriminant
10156 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10158 Mutate_Ekind (D_Minal, E_In_Parameter);
10159 Set_Mechanism (D_Minal, Default_Mechanism);
10160 Set_Etype (D_Minal, Etype (Discrim));
10161 Set_Scope (D_Minal, Current_Scope);
10162 Set_Parent (D_Minal, Parent (Discrim));
10164 Set_Discriminal (Discrim, D_Minal);
10165 Set_Discriminal_Link (D_Minal, Discrim);
10167 -- For task types, build at once the discriminants of the corresponding
10168 -- record, which are needed if discriminants are used in entry defaults
10169 -- and in family bounds.
10171 if Is_Concurrent_Type (Current_Scope)
10172 or else
10173 Is_Limited_Type (Current_Scope)
10174 then
10175 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10177 Mutate_Ekind (CR_Disc, E_In_Parameter);
10178 Set_Mechanism (CR_Disc, Default_Mechanism);
10179 Set_Etype (CR_Disc, Etype (Discrim));
10180 Set_Scope (CR_Disc, Current_Scope);
10181 Set_Discriminal_Link (CR_Disc, Discrim);
10182 Set_CR_Discriminant (Discrim, CR_Disc);
10183 end if;
10184 end Build_Discriminal;
10186 ------------------------------------
10187 -- Build_Discriminant_Constraints --
10188 ------------------------------------
10190 function Build_Discriminant_Constraints
10191 (T : Entity_Id;
10192 Def : Node_Id;
10193 Derived_Def : Boolean := False) return Elist_Id
10195 C : constant Node_Id := Constraint (Def);
10196 Nb_Discr : constant Nat := Number_Discriminants (T);
10198 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10199 -- Saves the expression corresponding to a given discriminant in T
10201 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10202 -- Return the Position number within array Discr_Expr of a discriminant
10203 -- D within the discriminant list of the discriminated type T.
10205 procedure Process_Discriminant_Expression
10206 (Expr : Node_Id;
10207 D : Entity_Id);
10208 -- If this is a discriminant constraint on a partial view, do not
10209 -- generate an overflow check on the discriminant expression. The check
10210 -- will be generated when constraining the full view. Otherwise the
10211 -- backend creates duplicate symbols for the temporaries corresponding
10212 -- to the expressions to be checked, causing spurious assembler errors.
10214 ------------------
10215 -- Pos_Of_Discr --
10216 ------------------
10218 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10219 Disc : Entity_Id;
10221 begin
10222 Disc := First_Discriminant (T);
10223 for J in Discr_Expr'Range loop
10224 if Disc = D then
10225 return J;
10226 end if;
10228 Next_Discriminant (Disc);
10229 end loop;
10231 -- Note: Since this function is called on discriminants that are
10232 -- known to belong to the discriminated type, falling through the
10233 -- loop with no match signals an internal compiler error.
10235 raise Program_Error;
10236 end Pos_Of_Discr;
10238 -------------------------------------
10239 -- Process_Discriminant_Expression --
10240 -------------------------------------
10242 procedure Process_Discriminant_Expression
10243 (Expr : Node_Id;
10244 D : Entity_Id)
10246 BDT : constant Entity_Id := Base_Type (Etype (D));
10248 begin
10249 -- If this is a discriminant constraint on a partial view, do
10250 -- not generate an overflow on the discriminant expression. The
10251 -- check will be generated when constraining the full view.
10253 if Is_Private_Type (T)
10254 and then Present (Full_View (T))
10255 then
10256 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10257 else
10258 Analyze_And_Resolve (Expr, BDT);
10259 end if;
10260 end Process_Discriminant_Expression;
10262 -- Declarations local to Build_Discriminant_Constraints
10264 Discr : Entity_Id;
10265 E : Entity_Id;
10266 Elist : constant Elist_Id := New_Elmt_List;
10268 Constr : Node_Id;
10269 Expr : Node_Id;
10270 Id : Node_Id;
10271 Position : Nat;
10272 Found : Boolean;
10274 Discrim_Present : Boolean := False;
10276 -- Start of processing for Build_Discriminant_Constraints
10278 begin
10279 -- The following loop will process positional associations only.
10280 -- For a positional association, the (single) discriminant is
10281 -- implicitly specified by position, in textual order (RM 3.7.2).
10283 Discr := First_Discriminant (T);
10284 Constr := First (Constraints (C));
10285 for D in Discr_Expr'Range loop
10286 exit when Nkind (Constr) = N_Discriminant_Association;
10288 if No (Constr) then
10289 Error_Msg_N ("too few discriminants given in constraint", C);
10290 return New_Elmt_List;
10292 elsif Nkind (Constr) = N_Range
10293 or else (Nkind (Constr) = N_Attribute_Reference
10294 and then Attribute_Name (Constr) = Name_Range)
10295 then
10296 Error_Msg_N
10297 ("a range is not a valid discriminant constraint", Constr);
10298 Discr_Expr (D) := Error;
10300 elsif Nkind (Constr) = N_Subtype_Indication then
10301 Error_Msg_N
10302 ("a subtype indication is not a valid discriminant constraint",
10303 Constr);
10304 Discr_Expr (D) := Error;
10306 else
10307 Process_Discriminant_Expression (Constr, Discr);
10308 Discr_Expr (D) := Constr;
10309 end if;
10311 Next_Discriminant (Discr);
10312 Next (Constr);
10313 end loop;
10315 if No (Discr) and then Present (Constr) then
10316 Error_Msg_N ("too many discriminants given in constraint", Constr);
10317 return New_Elmt_List;
10318 end if;
10320 -- Named associations can be given in any order, but if both positional
10321 -- and named associations are used in the same discriminant constraint,
10322 -- then positional associations must occur first, at their normal
10323 -- position. Hence once a named association is used, the rest of the
10324 -- discriminant constraint must use only named associations.
10326 while Present (Constr) loop
10328 -- Positional association forbidden after a named association
10330 if Nkind (Constr) /= N_Discriminant_Association then
10331 Error_Msg_N ("positional association follows named one", Constr);
10332 return New_Elmt_List;
10334 -- Otherwise it is a named association
10336 else
10337 -- E records the type of the discriminants in the named
10338 -- association. All the discriminants specified in the same name
10339 -- association must have the same type.
10341 E := Empty;
10343 -- Search the list of discriminants in T to see if the simple name
10344 -- given in the constraint matches any of them.
10346 Id := First (Selector_Names (Constr));
10347 while Present (Id) loop
10348 Found := False;
10350 -- If Original_Discriminant is present, we are processing a
10351 -- generic instantiation and this is an instance node. We need
10352 -- to find the name of the corresponding discriminant in the
10353 -- actual record type T and not the name of the discriminant in
10354 -- the generic formal. Example:
10356 -- generic
10357 -- type G (D : int) is private;
10358 -- package P is
10359 -- subtype W is G (D => 1);
10360 -- end package;
10361 -- type Rec (X : int) is record ... end record;
10362 -- package Q is new P (G => Rec);
10364 -- At the point of the instantiation, formal type G is Rec
10365 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10366 -- which really looks like "subtype W is Rec (D => 1);" at
10367 -- the point of instantiation, we want to find the discriminant
10368 -- that corresponds to D in Rec, i.e. X.
10370 if Present (Original_Discriminant (Id))
10371 and then In_Instance
10372 then
10373 Discr := Find_Corresponding_Discriminant (Id, T);
10374 Found := True;
10376 else
10377 Discr := First_Discriminant (T);
10378 while Present (Discr) loop
10379 if Chars (Discr) = Chars (Id) then
10380 Found := True;
10381 exit;
10382 end if;
10384 Next_Discriminant (Discr);
10385 end loop;
10387 if not Found then
10388 Error_Msg_N ("& does not match any discriminant", Id);
10389 return New_Elmt_List;
10391 -- If the parent type is a generic formal, preserve the
10392 -- name of the discriminant for subsequent instances.
10393 -- see comment at the beginning of this if statement.
10395 elsif Is_Generic_Type (Root_Type (T)) then
10396 Set_Original_Discriminant (Id, Discr);
10397 end if;
10398 end if;
10400 Position := Pos_Of_Discr (T, Discr);
10402 if Present (Discr_Expr (Position)) then
10403 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10405 else
10406 -- Each discriminant specified in the same named association
10407 -- must be associated with a separate copy of the
10408 -- corresponding expression.
10410 if Present (Next (Id)) then
10411 Expr := New_Copy_Tree (Expression (Constr));
10412 Set_Parent (Expr, Parent (Expression (Constr)));
10413 else
10414 Expr := Expression (Constr);
10415 end if;
10417 Discr_Expr (Position) := Expr;
10418 Process_Discriminant_Expression (Expr, Discr);
10419 end if;
10421 -- A discriminant association with more than one discriminant
10422 -- name is only allowed if the named discriminants are all of
10423 -- the same type (RM 3.7.1(8)).
10425 if E = Empty then
10426 E := Base_Type (Etype (Discr));
10428 elsif Base_Type (Etype (Discr)) /= E then
10429 Error_Msg_N
10430 ("all discriminants in an association " &
10431 "must have the same type", Id);
10432 end if;
10434 Next (Id);
10435 end loop;
10436 end if;
10438 Next (Constr);
10439 end loop;
10441 -- A discriminant constraint must provide exactly one value for each
10442 -- discriminant of the type (RM 3.7.1(8)).
10444 for J in Discr_Expr'Range loop
10445 if No (Discr_Expr (J)) then
10446 Error_Msg_N ("too few discriminants given in constraint", C);
10447 return New_Elmt_List;
10448 end if;
10449 end loop;
10451 -- Determine if there are discriminant expressions in the constraint
10453 for J in Discr_Expr'Range loop
10454 if Denotes_Discriminant
10455 (Discr_Expr (J), Check_Concurrent => True)
10456 then
10457 Discrim_Present := True;
10458 exit;
10459 end if;
10460 end loop;
10462 -- Build an element list consisting of the expressions given in the
10463 -- discriminant constraint and apply the appropriate checks. The list
10464 -- is constructed after resolving any named discriminant associations
10465 -- and therefore the expressions appear in the textual order of the
10466 -- discriminants.
10468 Discr := First_Discriminant (T);
10469 for J in Discr_Expr'Range loop
10470 if Discr_Expr (J) /= Error then
10471 Append_Elmt (Discr_Expr (J), Elist);
10473 -- If any of the discriminant constraints is given by a
10474 -- discriminant and we are in a derived type declaration we
10475 -- have a discriminant renaming. Establish link between new
10476 -- and old discriminant. The new discriminant has an implicit
10477 -- dereference if the old one does.
10479 if Denotes_Discriminant (Discr_Expr (J)) then
10480 if Derived_Def then
10481 declare
10482 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10484 begin
10485 Set_Corresponding_Discriminant (New_Discr, Discr);
10486 Set_Has_Implicit_Dereference (New_Discr,
10487 Has_Implicit_Dereference (Discr));
10488 end;
10489 end if;
10491 -- Force the evaluation of non-discriminant expressions.
10492 -- If we have found a discriminant in the constraint 3.4(26)
10493 -- and 3.8(18) demand that no range checks are performed are
10494 -- after evaluation. If the constraint is for a component
10495 -- definition that has a per-object constraint, expressions are
10496 -- evaluated but not checked either. In all other cases perform
10497 -- a range check.
10499 else
10500 if Discrim_Present then
10501 null;
10503 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10504 and then Has_Per_Object_Constraint
10505 (Defining_Identifier (Parent (Parent (Def))))
10506 then
10507 null;
10509 elsif Is_Access_Type (Etype (Discr)) then
10510 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10512 else
10513 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10514 end if;
10516 -- If the value of the discriminant may be visible in
10517 -- another unit or child unit, create an external name
10518 -- for it. We use the name of the object or component
10519 -- that carries the discriminated subtype. The code
10520 -- below may generate external symbols for the discriminant
10521 -- expression when not strictly needed, which is harmless.
10523 if Expander_Active
10524 and then Comes_From_Source (Def)
10525 and then not Is_Subprogram (Current_Scope)
10526 then
10527 declare
10528 Id : Entity_Id := Empty;
10529 begin
10530 if Nkind (Parent (Def)) = N_Object_Declaration then
10531 Id := Defining_Identifier (Parent (Def));
10533 elsif Nkind (Parent (Def)) = N_Component_Definition
10534 and then
10535 Nkind (Parent (Parent (Def)))
10536 = N_Component_Declaration
10537 then
10538 Id := Defining_Identifier (Parent (Parent (Def)));
10539 end if;
10541 if Present (Id) then
10542 Force_Evaluation (
10543 Discr_Expr (J),
10544 Related_Id => Id,
10545 Discr_Number => J);
10546 else
10547 Force_Evaluation (Discr_Expr (J));
10548 end if;
10549 end;
10550 else
10551 Force_Evaluation (Discr_Expr (J));
10552 end if;
10553 end if;
10555 -- Check that the designated type of an access discriminant's
10556 -- expression is not a class-wide type unless the discriminant's
10557 -- designated type is also class-wide.
10559 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10560 and then not Is_Class_Wide_Type
10561 (Designated_Type (Etype (Discr)))
10562 and then Etype (Discr_Expr (J)) /= Any_Type
10563 and then Is_Class_Wide_Type
10564 (Designated_Type (Etype (Discr_Expr (J))))
10565 then
10566 Wrong_Type (Discr_Expr (J), Etype (Discr));
10568 elsif Is_Access_Type (Etype (Discr))
10569 and then not Is_Access_Constant (Etype (Discr))
10570 and then Is_Access_Type (Etype (Discr_Expr (J)))
10571 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10572 then
10573 Error_Msg_NE
10574 ("constraint for discriminant& must be access to variable",
10575 Def, Discr);
10576 end if;
10577 end if;
10579 Next_Discriminant (Discr);
10580 end loop;
10582 return Elist;
10583 end Build_Discriminant_Constraints;
10585 ---------------------------------
10586 -- Build_Discriminated_Subtype --
10587 ---------------------------------
10589 procedure Build_Discriminated_Subtype
10590 (T : Entity_Id;
10591 Def_Id : Entity_Id;
10592 Elist : Elist_Id;
10593 Related_Nod : Node_Id;
10594 For_Access : Boolean := False)
10596 Has_Discrs : constant Boolean := Has_Discriminants (T);
10597 Constrained : constant Boolean :=
10598 (Has_Discrs
10599 and then not Is_Empty_Elmt_List (Elist)
10600 and then not Is_Class_Wide_Type (T))
10601 or else Is_Constrained (T);
10603 begin
10604 if Ekind (T) = E_Record_Type then
10605 Mutate_Ekind (Def_Id, E_Record_Subtype);
10607 -- Inherit preelaboration flag from base, for types for which it
10608 -- may have been set: records, private types, protected types.
10610 Set_Known_To_Have_Preelab_Init
10611 (Def_Id, Known_To_Have_Preelab_Init (T));
10613 elsif Ekind (T) = E_Task_Type then
10614 Mutate_Ekind (Def_Id, E_Task_Subtype);
10616 elsif Ekind (T) = E_Protected_Type then
10617 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10618 Set_Known_To_Have_Preelab_Init
10619 (Def_Id, Known_To_Have_Preelab_Init (T));
10621 elsif Is_Private_Type (T) then
10622 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10623 Set_Known_To_Have_Preelab_Init
10624 (Def_Id, Known_To_Have_Preelab_Init (T));
10626 -- Private subtypes may have private dependents
10628 Set_Private_Dependents (Def_Id, New_Elmt_List);
10630 elsif Is_Class_Wide_Type (T) then
10631 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10633 else
10634 -- Incomplete type. Attach subtype to list of dependents, to be
10635 -- completed with full view of parent type, unless is it the
10636 -- designated subtype of a record component within an init_proc.
10637 -- This last case arises for a component of an access type whose
10638 -- designated type is incomplete (e.g. a Taft Amendment type).
10639 -- The designated subtype is within an inner scope, and needs no
10640 -- elaboration, because only the access type is needed in the
10641 -- initialization procedure.
10643 if Ekind (T) = E_Incomplete_Type then
10644 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10645 else
10646 Mutate_Ekind (Def_Id, Ekind (T));
10647 end if;
10649 if For_Access and then Within_Init_Proc then
10650 null;
10651 else
10652 Append_Elmt (Def_Id, Private_Dependents (T));
10653 end if;
10654 end if;
10656 Set_Etype (Def_Id, T);
10657 Reinit_Size_Align (Def_Id);
10658 Set_Has_Discriminants (Def_Id, Has_Discrs);
10659 Set_Is_Constrained (Def_Id, Constrained);
10661 Set_First_Entity (Def_Id, First_Entity (T));
10662 Set_Last_Entity (Def_Id, Last_Entity (T));
10663 Set_Has_Implicit_Dereference
10664 (Def_Id, Has_Implicit_Dereference (T));
10665 Set_Has_Pragma_Unreferenced_Objects
10666 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10668 -- If the subtype is the completion of a private declaration, there may
10669 -- have been representation clauses for the partial view, and they must
10670 -- be preserved. Build_Derived_Type chains the inherited clauses with
10671 -- the ones appearing on the extension. If this comes from a subtype
10672 -- declaration, all clauses are inherited.
10674 if No (First_Rep_Item (Def_Id)) then
10675 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10676 end if;
10678 if Is_Tagged_Type (T) then
10679 Set_Is_Tagged_Type (Def_Id);
10680 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10681 Make_Class_Wide_Type (Def_Id);
10682 end if;
10684 Set_Stored_Constraint (Def_Id, No_Elist);
10686 if Has_Discrs then
10687 Set_Discriminant_Constraint (Def_Id, Elist);
10688 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10689 end if;
10691 if Is_Tagged_Type (T) then
10693 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10694 -- concurrent record type (which has the list of primitive
10695 -- operations).
10697 if Ada_Version >= Ada_2005
10698 and then Is_Concurrent_Type (T)
10699 then
10700 Set_Corresponding_Record_Type (Def_Id,
10701 Corresponding_Record_Type (T));
10702 else
10703 Set_Direct_Primitive_Operations (Def_Id,
10704 Direct_Primitive_Operations (T));
10705 end if;
10707 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10708 end if;
10710 -- Subtypes introduced by component declarations do not need to be
10711 -- marked as delayed, and do not get freeze nodes, because the semantics
10712 -- verifies that the parents of the subtypes are frozen before the
10713 -- enclosing record is frozen.
10715 if not Is_Type (Scope (Def_Id)) then
10716 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10718 if Is_Private_Type (T)
10719 and then Present (Full_View (T))
10720 then
10721 Conditional_Delay (Def_Id, Full_View (T));
10722 else
10723 Conditional_Delay (Def_Id, T);
10724 end if;
10725 end if;
10727 if Is_Record_Type (T) then
10728 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10730 if Has_Discrs
10731 and then not Is_Empty_Elmt_List (Elist)
10732 and then not For_Access
10733 then
10734 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10736 elsif not Is_Private_Type (T) then
10737 Set_Cloned_Subtype (Def_Id, T);
10738 end if;
10739 end if;
10740 end Build_Discriminated_Subtype;
10742 ---------------------------
10743 -- Build_Itype_Reference --
10744 ---------------------------
10746 procedure Build_Itype_Reference
10747 (Ityp : Entity_Id;
10748 Nod : Node_Id)
10750 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10751 begin
10753 -- Itype references are only created for use by the back-end
10755 if Inside_A_Generic then
10756 return;
10757 else
10758 Set_Itype (IR, Ityp);
10760 -- If Nod is a library unit entity, then Insert_After won't work,
10761 -- because Nod is not a member of any list. Therefore, we use
10762 -- Add_Global_Declaration in this case. This can happen if we have a
10763 -- build-in-place library function, child unit or not.
10765 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10766 or else (Nkind (Nod) in
10767 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
10768 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10769 then
10770 Add_Global_Declaration (IR);
10771 else
10772 Insert_After (Nod, IR);
10773 end if;
10774 end if;
10775 end Build_Itype_Reference;
10777 ------------------------
10778 -- Build_Scalar_Bound --
10779 ------------------------
10781 function Build_Scalar_Bound
10782 (Bound : Node_Id;
10783 Par_T : Entity_Id;
10784 Der_T : Entity_Id) return Node_Id
10786 New_Bound : Entity_Id;
10788 begin
10789 -- Note: not clear why this is needed, how can the original bound
10790 -- be unanalyzed at this point? and if it is, what business do we
10791 -- have messing around with it? and why is the base type of the
10792 -- parent type the right type for the resolution. It probably is
10793 -- not. It is OK for the new bound we are creating, but not for
10794 -- the old one??? Still if it never happens, no problem.
10796 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10798 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
10799 New_Bound := New_Copy (Bound);
10800 Set_Etype (New_Bound, Der_T);
10801 Set_Analyzed (New_Bound);
10803 elsif Is_Entity_Name (Bound) then
10804 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10806 -- The following is almost certainly wrong. What business do we have
10807 -- relocating a node (Bound) that is presumably still attached to
10808 -- the tree elsewhere???
10810 else
10811 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10812 end if;
10814 Set_Etype (New_Bound, Der_T);
10815 return New_Bound;
10816 end Build_Scalar_Bound;
10818 -------------------------------
10819 -- Check_Abstract_Overriding --
10820 -------------------------------
10822 procedure Check_Abstract_Overriding (T : Entity_Id) is
10823 Alias_Subp : Entity_Id;
10824 Elmt : Elmt_Id;
10825 Op_List : Elist_Id;
10826 Subp : Entity_Id;
10827 Type_Def : Node_Id;
10829 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10830 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10831 -- which has pragma Implemented already set. Check whether Subp's entity
10832 -- kind conforms to the implementation kind of the overridden routine.
10834 procedure Check_Pragma_Implemented
10835 (Subp : Entity_Id;
10836 Iface_Subp : Entity_Id);
10837 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10838 -- Iface_Subp and both entities have pragma Implemented already set on
10839 -- them. Check whether the two implementation kinds are conforming.
10841 procedure Inherit_Pragma_Implemented
10842 (Subp : Entity_Id;
10843 Iface_Subp : Entity_Id);
10844 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10845 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10846 -- Propagate the implementation kind of Iface_Subp to Subp.
10848 ------------------------------
10849 -- Check_Pragma_Implemented --
10850 ------------------------------
10852 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10853 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10854 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10855 Subp_Alias : constant Entity_Id := Alias (Subp);
10856 Contr_Typ : Entity_Id;
10857 Impl_Subp : Entity_Id;
10859 begin
10860 -- Subp must have an alias since it is a hidden entity used to link
10861 -- an interface subprogram to its overriding counterpart.
10863 pragma Assert (Present (Subp_Alias));
10865 -- Handle aliases to synchronized wrappers
10867 Impl_Subp := Subp_Alias;
10869 if Is_Primitive_Wrapper (Impl_Subp) then
10870 Impl_Subp := Wrapped_Entity (Impl_Subp);
10871 end if;
10873 -- Extract the type of the controlling formal
10875 Contr_Typ := Etype (First_Formal (Subp_Alias));
10877 if Is_Concurrent_Record_Type (Contr_Typ) then
10878 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10879 end if;
10881 -- An interface subprogram whose implementation kind is By_Entry must
10882 -- be implemented by an entry.
10884 if Impl_Kind = Name_By_Entry
10885 and then Ekind (Impl_Subp) /= E_Entry
10886 then
10887 Error_Msg_Node_2 := Iface_Alias;
10888 Error_Msg_NE
10889 ("type & must implement abstract subprogram & with an entry",
10890 Subp_Alias, Contr_Typ);
10892 elsif Impl_Kind = Name_By_Protected_Procedure then
10894 -- An interface subprogram whose implementation kind is By_
10895 -- Protected_Procedure cannot be implemented by a primitive
10896 -- procedure of a task type.
10898 if Ekind (Contr_Typ) /= E_Protected_Type then
10899 Error_Msg_Node_2 := Contr_Typ;
10900 Error_Msg_NE
10901 ("interface subprogram & cannot be implemented by a "
10902 & "primitive procedure of task type &",
10903 Subp_Alias, Iface_Alias);
10905 -- An interface subprogram whose implementation kind is By_
10906 -- Protected_Procedure must be implemented by a procedure.
10908 elsif Ekind (Impl_Subp) /= E_Procedure then
10909 Error_Msg_Node_2 := Iface_Alias;
10910 Error_Msg_NE
10911 ("type & must implement abstract subprogram & with a "
10912 & "procedure", Subp_Alias, Contr_Typ);
10914 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10915 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10916 then
10917 Error_Msg_Name_1 := Impl_Kind;
10918 Error_Msg_N
10919 ("overriding operation& must have synchronization%",
10920 Subp_Alias);
10921 end if;
10923 -- If primitive has Optional synchronization, overriding operation
10924 -- must match if it has an explicit synchronization.
10926 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10927 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10928 then
10929 Error_Msg_Name_1 := Impl_Kind;
10930 Error_Msg_N
10931 ("overriding operation& must have synchronization%", Subp_Alias);
10932 end if;
10933 end Check_Pragma_Implemented;
10935 ------------------------------
10936 -- Check_Pragma_Implemented --
10937 ------------------------------
10939 procedure Check_Pragma_Implemented
10940 (Subp : Entity_Id;
10941 Iface_Subp : Entity_Id)
10943 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10944 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10946 begin
10947 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10948 -- and overriding subprogram are different. In general this is an
10949 -- error except when the implementation kind of the overridden
10950 -- subprograms is By_Any or Optional.
10952 if Iface_Kind /= Subp_Kind
10953 and then Iface_Kind /= Name_By_Any
10954 and then Iface_Kind /= Name_Optional
10955 then
10956 if Iface_Kind = Name_By_Entry then
10957 Error_Msg_N
10958 ("incompatible implementation kind, overridden subprogram " &
10959 "is marked By_Entry", Subp);
10960 else
10961 Error_Msg_N
10962 ("incompatible implementation kind, overridden subprogram " &
10963 "is marked By_Protected_Procedure", Subp);
10964 end if;
10965 end if;
10966 end Check_Pragma_Implemented;
10968 --------------------------------
10969 -- Inherit_Pragma_Implemented --
10970 --------------------------------
10972 procedure Inherit_Pragma_Implemented
10973 (Subp : Entity_Id;
10974 Iface_Subp : Entity_Id)
10976 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10977 Loc : constant Source_Ptr := Sloc (Subp);
10978 Impl_Prag : Node_Id;
10980 begin
10981 -- Since the implementation kind is stored as a representation item
10982 -- rather than a flag, create a pragma node.
10984 Impl_Prag :=
10985 Make_Pragma (Loc,
10986 Chars => Name_Implemented,
10987 Pragma_Argument_Associations => New_List (
10988 Make_Pragma_Argument_Association (Loc,
10989 Expression => New_Occurrence_Of (Subp, Loc)),
10991 Make_Pragma_Argument_Association (Loc,
10992 Expression => Make_Identifier (Loc, Iface_Kind))));
10994 -- The pragma doesn't need to be analyzed because it is internally
10995 -- built. It is safe to directly register it as a rep item since we
10996 -- are only interested in the characters of the implementation kind.
10998 Record_Rep_Item (Subp, Impl_Prag);
10999 end Inherit_Pragma_Implemented;
11001 -- Start of processing for Check_Abstract_Overriding
11003 begin
11004 Op_List := Primitive_Operations (T);
11006 -- Loop to check primitive operations
11008 Elmt := First_Elmt (Op_List);
11009 while Present (Elmt) loop
11010 Subp := Node (Elmt);
11011 Alias_Subp := Alias (Subp);
11013 -- Inherited subprograms are identified by the fact that they do not
11014 -- come from source, and the associated source location is the
11015 -- location of the first subtype of the derived type.
11017 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11018 -- subprograms that "require overriding".
11020 -- Special exception, do not complain about failure to override the
11021 -- stream routines _Input and _Output, as well as the primitive
11022 -- operations used in dispatching selects since we always provide
11023 -- automatic overridings for these subprograms.
11025 -- The partial view of T may have been a private extension, for
11026 -- which inherited functions dispatching on result are abstract.
11027 -- If the full view is a null extension, there is no need for
11028 -- overriding in Ada 2005, but wrappers need to be built for them
11029 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11031 if Is_Null_Extension (T)
11032 and then Has_Controlling_Result (Subp)
11033 and then Ada_Version >= Ada_2005
11034 and then Present (Alias_Subp)
11035 and then not Comes_From_Source (Subp)
11036 and then not Is_Abstract_Subprogram (Alias_Subp)
11037 and then not Is_Access_Type (Etype (Subp))
11038 then
11039 null;
11041 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11042 -- processing because this check is done with the aliased
11043 -- entity
11045 elsif Present (Interface_Alias (Subp)) then
11046 null;
11048 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11049 -- of a visible private primitive inherited from an ancestor with
11050 -- the aspect Type_Invariant'Class, unless the inherited primitive
11051 -- is abstract.
11053 elsif not Is_Abstract_Subprogram (Subp)
11054 and then not Comes_From_Source (Subp) -- An inherited subprogram
11055 and then Requires_Overriding (Subp)
11056 and then Present (Alias_Subp)
11057 and then Has_Invariants (Etype (T))
11058 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11059 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11060 and then Is_Private_Primitive (Alias_Subp)
11061 then
11062 Error_Msg_NE
11063 ("inherited private primitive & must be overridden", T, Subp);
11064 Error_Msg_N
11065 ("\because ancestor type has 'Type_'Invariant''Class " &
11066 "(RM 7.3.2(6.1))", T);
11068 elsif (Is_Abstract_Subprogram (Subp)
11069 or else Requires_Overriding (Subp)
11070 or else
11071 (Has_Controlling_Result (Subp)
11072 and then Present (Alias_Subp)
11073 and then not Comes_From_Source (Subp)
11074 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11075 and then not Is_TSS (Subp, TSS_Stream_Input)
11076 and then not Is_TSS (Subp, TSS_Stream_Output)
11077 and then not Is_Abstract_Type (T)
11078 and then not Is_Predefined_Interface_Primitive (Subp)
11080 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11081 -- with abstract interface types because the check will be done
11082 -- with the aliased entity (otherwise we generate a duplicated
11083 -- error message).
11085 and then not Present (Interface_Alias (Subp))
11086 then
11087 if Present (Alias_Subp) then
11089 -- Only perform the check for a derived subprogram when the
11090 -- type has an explicit record extension. This avoids incorrect
11091 -- flagging of abstract subprograms for the case of a type
11092 -- without an extension that is derived from a formal type
11093 -- with a tagged actual (can occur within a private part).
11095 -- Ada 2005 (AI-391): In the case of an inherited function with
11096 -- a controlling result of the type, the rule does not apply if
11097 -- the type is a null extension (unless the parent function
11098 -- itself is abstract, in which case the function must still be
11099 -- be overridden). The expander will generate an overriding
11100 -- wrapper function calling the parent subprogram (see
11101 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11103 Type_Def := Type_Definition (Parent (T));
11105 if Nkind (Type_Def) = N_Derived_Type_Definition
11106 and then Present (Record_Extension_Part (Type_Def))
11107 and then
11108 (Ada_Version < Ada_2005
11109 or else not Is_Null_Extension (T)
11110 or else Ekind (Subp) = E_Procedure
11111 or else not Has_Controlling_Result (Subp)
11112 or else Is_Abstract_Subprogram (Alias_Subp)
11113 or else Requires_Overriding (Subp)
11114 or else Is_Access_Type (Etype (Subp)))
11115 then
11116 -- Avoid reporting error in case of abstract predefined
11117 -- primitive inherited from interface type because the
11118 -- body of internally generated predefined primitives
11119 -- of tagged types are generated later by Freeze_Type
11121 if Is_Interface (Root_Type (T))
11122 and then Is_Abstract_Subprogram (Subp)
11123 and then Is_Predefined_Dispatching_Operation (Subp)
11124 and then not Comes_From_Source (Ultimate_Alias (Subp))
11125 then
11126 null;
11128 -- A null extension is not obliged to override an inherited
11129 -- procedure subject to pragma Extensions_Visible with value
11130 -- False and at least one controlling OUT parameter
11131 -- (SPARK RM 6.1.7(6)).
11133 elsif Is_Null_Extension (T)
11134 and then Is_EVF_Procedure (Subp)
11135 then
11136 null;
11138 -- Subprogram renamings cannot be overridden
11140 elsif Comes_From_Source (Subp)
11141 and then Present (Alias (Subp))
11142 then
11143 null;
11145 -- Skip reporting the error on Ada 2022 only subprograms
11146 -- that require overriding if we are not in Ada 2022 mode.
11148 elsif Ada_Version < Ada_2022
11149 and then Requires_Overriding (Subp)
11150 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11151 then
11152 null;
11154 else
11155 Error_Msg_NE
11156 ("type must be declared abstract or & overridden",
11157 T, Subp);
11159 -- Traverse the whole chain of aliased subprograms to
11160 -- complete the error notification. This is especially
11161 -- useful for traceability of the chain of entities when
11162 -- the subprogram corresponds with an interface
11163 -- subprogram (which may be defined in another package).
11165 if Present (Alias_Subp) then
11166 declare
11167 E : Entity_Id;
11169 begin
11170 E := Subp;
11171 while Present (Alias (E)) loop
11173 -- Avoid reporting redundant errors on entities
11174 -- inherited from interfaces
11176 if Sloc (E) /= Sloc (T) then
11177 Error_Msg_Sloc := Sloc (E);
11178 Error_Msg_NE
11179 ("\& has been inherited #", T, Subp);
11180 end if;
11182 E := Alias (E);
11183 end loop;
11185 Error_Msg_Sloc := Sloc (E);
11187 -- AI05-0068: report if there is an overriding
11188 -- non-abstract subprogram that is invisible.
11190 if Is_Hidden (E)
11191 and then not Is_Abstract_Subprogram (E)
11192 then
11193 Error_Msg_NE
11194 ("\& subprogram# is not visible",
11195 T, Subp);
11197 -- Clarify the case where a non-null extension must
11198 -- override inherited procedure subject to pragma
11199 -- Extensions_Visible with value False and at least
11200 -- one controlling OUT param.
11202 elsif Is_EVF_Procedure (E) then
11203 Error_Msg_NE
11204 ("\& # is subject to Extensions_Visible False",
11205 T, Subp);
11207 else
11208 Error_Msg_NE
11209 ("\& has been inherited from subprogram #",
11210 T, Subp);
11211 end if;
11212 end;
11213 end if;
11214 end if;
11216 -- Ada 2005 (AI-345): Protected or task type implementing
11217 -- abstract interfaces.
11219 elsif Is_Concurrent_Record_Type (T)
11220 and then Present (Interfaces (T))
11221 then
11222 -- There is no need to check here RM 9.4(11.9/3) since we
11223 -- are processing the corresponding record type and the
11224 -- mode of the overriding subprograms was verified by
11225 -- Check_Conformance when the corresponding concurrent
11226 -- type declaration was analyzed.
11228 Error_Msg_NE
11229 ("interface subprogram & must be overridden", T, Subp);
11231 -- Examine primitive operations of synchronized type to find
11232 -- homonyms that have the wrong profile.
11234 declare
11235 Prim : Entity_Id;
11237 begin
11238 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11239 while Present (Prim) loop
11240 if Chars (Prim) = Chars (Subp) then
11241 Error_Msg_NE
11242 ("profile is not type conformant with prefixed "
11243 & "view profile of inherited operation&",
11244 Prim, Subp);
11245 end if;
11247 Next_Entity (Prim);
11248 end loop;
11249 end;
11250 end if;
11252 else
11253 Error_Msg_Node_2 := T;
11254 Error_Msg_N
11255 ("abstract subprogram& not allowed for type&", Subp);
11257 -- Also post unconditional warning on the type (unconditional
11258 -- so that if there are more than one of these cases, we get
11259 -- them all, and not just the first one).
11261 Error_Msg_Node_2 := Subp;
11262 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11263 end if;
11265 -- A subprogram subject to pragma Extensions_Visible with value
11266 -- "True" cannot override a subprogram subject to the same pragma
11267 -- with value "False" (SPARK RM 6.1.7(5)).
11269 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11270 and then Present (Overridden_Operation (Subp))
11271 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11272 Extensions_Visible_False
11273 then
11274 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11275 Error_Msg_N
11276 ("subprogram & with Extensions_Visible True cannot override "
11277 & "subprogram # with Extensions_Visible False", Subp);
11278 end if;
11280 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11282 -- Subp is an expander-generated procedure which maps an interface
11283 -- alias to a protected wrapper. The interface alias is flagged by
11284 -- pragma Implemented. Ensure that Subp is a procedure when the
11285 -- implementation kind is By_Protected_Procedure or an entry when
11286 -- By_Entry.
11288 if Ada_Version >= Ada_2012
11289 and then Is_Hidden (Subp)
11290 and then Present (Interface_Alias (Subp))
11291 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11292 then
11293 Check_Pragma_Implemented (Subp);
11294 end if;
11296 -- Subp is an interface primitive which overrides another interface
11297 -- primitive marked with pragma Implemented.
11299 if Ada_Version >= Ada_2012
11300 and then Present (Overridden_Operation (Subp))
11301 and then Has_Rep_Pragma
11302 (Overridden_Operation (Subp), Name_Implemented)
11303 then
11304 -- If the overriding routine is also marked by Implemented, check
11305 -- that the two implementation kinds are conforming.
11307 if Has_Rep_Pragma (Subp, Name_Implemented) then
11308 Check_Pragma_Implemented
11309 (Subp => Subp,
11310 Iface_Subp => Overridden_Operation (Subp));
11312 -- Otherwise the overriding routine inherits the implementation
11313 -- kind from the overridden subprogram.
11315 else
11316 Inherit_Pragma_Implemented
11317 (Subp => Subp,
11318 Iface_Subp => Overridden_Operation (Subp));
11319 end if;
11320 end if;
11322 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11323 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11324 -- for procedures, since this is our pragma.
11326 if Present (Overridden_Operation (Subp))
11327 and then No_Return (Overridden_Operation (Subp))
11328 then
11330 -- If the subprogram is a renaming, check that the renamed
11331 -- subprogram is No_Return.
11333 if Present (Renamed_Or_Alias (Subp)) then
11334 if not No_Return (Renamed_Or_Alias (Subp)) then
11335 Error_Msg_NE ("subprogram & must be No_Return",
11336 Subp,
11337 Renamed_Or_Alias (Subp));
11338 Error_Msg_N ("\since renaming & overrides No_Return "
11339 & "subprogram (RM 6.5.1(6/2))",
11340 Subp);
11341 end if;
11343 -- Make sure that the subprogram itself is No_Return.
11345 elsif not No_Return (Subp) then
11346 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11347 Error_Msg_N
11348 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11349 Subp);
11350 end if;
11351 end if;
11353 -- If the operation is a wrapper for a synchronized primitive, it
11354 -- may be called indirectly through a dispatching select. We assume
11355 -- that it will be referenced elsewhere indirectly, and suppress
11356 -- warnings about an unused entity.
11358 if Is_Primitive_Wrapper (Subp)
11359 and then Present (Wrapped_Entity (Subp))
11360 then
11361 Set_Referenced (Wrapped_Entity (Subp));
11362 end if;
11364 Next_Elmt (Elmt);
11365 end loop;
11366 end Check_Abstract_Overriding;
11368 ------------------------------------------------
11369 -- Check_Access_Discriminant_Requires_Limited --
11370 ------------------------------------------------
11372 procedure Check_Access_Discriminant_Requires_Limited
11373 (D : Node_Id;
11374 Loc : Node_Id)
11376 begin
11377 -- A discriminant_specification for an access discriminant shall appear
11378 -- only in the declaration for a task or protected type, or for a type
11379 -- with the reserved word 'limited' in its definition or in one of its
11380 -- ancestors (RM 3.7(10)).
11382 -- AI-0063: The proper condition is that type must be immutably limited,
11383 -- or else be a partial view.
11385 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11386 if Is_Limited_View (Current_Scope)
11387 or else
11388 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11389 and then Limited_Present (Parent (Current_Scope)))
11390 then
11391 null;
11393 else
11394 Error_Msg_N
11395 ("access discriminants allowed only for limited types", Loc);
11396 end if;
11397 end if;
11398 end Check_Access_Discriminant_Requires_Limited;
11400 -----------------------------------
11401 -- Check_Aliased_Component_Types --
11402 -----------------------------------
11404 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11405 C : Entity_Id;
11407 begin
11408 -- ??? Also need to check components of record extensions, but not
11409 -- components of protected types (which are always limited).
11411 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11412 -- types to be unconstrained. This is safe because it is illegal to
11413 -- create access subtypes to such types with explicit discriminant
11414 -- constraints.
11416 if not Is_Limited_Type (T) then
11417 if Ekind (T) = E_Record_Type then
11418 C := First_Component (T);
11419 while Present (C) loop
11420 if Is_Aliased (C)
11421 and then Has_Discriminants (Etype (C))
11422 and then not Is_Constrained (Etype (C))
11423 and then not In_Instance_Body
11424 and then Ada_Version < Ada_2005
11425 then
11426 Error_Msg_N
11427 ("aliased component must be constrained (RM 3.6(11))",
11429 end if;
11431 Next_Component (C);
11432 end loop;
11434 elsif Ekind (T) = E_Array_Type then
11435 if Has_Aliased_Components (T)
11436 and then Has_Discriminants (Component_Type (T))
11437 and then not Is_Constrained (Component_Type (T))
11438 and then not In_Instance_Body
11439 and then Ada_Version < Ada_2005
11440 then
11441 Error_Msg_N
11442 ("aliased component type must be constrained (RM 3.6(11))",
11444 end if;
11445 end if;
11446 end if;
11447 end Check_Aliased_Component_Types;
11449 --------------------------------------
11450 -- Check_Anonymous_Access_Component --
11451 --------------------------------------
11453 procedure Check_Anonymous_Access_Component
11454 (Typ_Decl : Node_Id;
11455 Typ : Entity_Id;
11456 Prev : Entity_Id;
11457 Comp_Def : Node_Id;
11458 Access_Def : Node_Id)
11460 Loc : constant Source_Ptr := Sloc (Comp_Def);
11461 Anon_Access : Entity_Id;
11462 Acc_Def : Node_Id;
11463 Decl : Node_Id;
11464 Type_Def : Node_Id;
11466 procedure Build_Incomplete_Type_Declaration;
11467 -- If the record type contains components that include an access to the
11468 -- current record, then create an incomplete type declaration for the
11469 -- record, to be used as the designated type of the anonymous access.
11470 -- This is done only once, and only if there is no previous partial
11471 -- view of the type.
11473 function Designates_T (Subt : Node_Id) return Boolean;
11474 -- Check whether a node designates the enclosing record type, or 'Class
11475 -- of that type
11477 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11478 -- Check whether an access definition includes a reference to
11479 -- the enclosing record type. The reference can be a subtype mark
11480 -- in the access definition itself, a 'Class attribute reference, or
11481 -- recursively a reference appearing in a parameter specification
11482 -- or result definition of an access_to_subprogram definition.
11484 --------------------------------------
11485 -- Build_Incomplete_Type_Declaration --
11486 --------------------------------------
11488 procedure Build_Incomplete_Type_Declaration is
11489 Decl : Node_Id;
11490 Inc_T : Entity_Id;
11491 H : Entity_Id;
11493 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11494 -- it's "is new ... with record" or else "is tagged record ...".
11496 Typ_Def : constant Node_Id :=
11497 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11498 then Type_Definition (Typ_Decl) else Empty);
11499 Is_Tagged : constant Boolean :=
11500 Present (Typ_Def)
11501 and then
11502 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11503 and then
11504 Present (Record_Extension_Part (Typ_Def)))
11505 or else
11506 (Nkind (Typ_Def) = N_Record_Definition
11507 and then Tagged_Present (Typ_Def)));
11509 begin
11510 -- If there is a previous partial view, no need to create a new one
11511 -- If the partial view, given by Prev, is incomplete, If Prev is
11512 -- a private declaration, full declaration is flagged accordingly.
11514 if Prev /= Typ then
11515 if Is_Tagged then
11516 Make_Class_Wide_Type (Prev);
11517 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11518 Set_Etype (Class_Wide_Type (Typ), Typ);
11519 end if;
11521 return;
11523 elsif Has_Private_Declaration (Typ) then
11525 -- If we refer to T'Class inside T, and T is the completion of a
11526 -- private type, then make sure the class-wide type exists.
11528 if Is_Tagged then
11529 Make_Class_Wide_Type (Typ);
11530 end if;
11532 return;
11534 -- If there was a previous anonymous access type, the incomplete
11535 -- type declaration will have been created already.
11537 elsif Present (Current_Entity (Typ))
11538 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11539 and then Full_View (Current_Entity (Typ)) = Typ
11540 then
11541 if Is_Tagged
11542 and then Comes_From_Source (Current_Entity (Typ))
11543 and then not Is_Tagged_Type (Current_Entity (Typ))
11544 then
11545 Make_Class_Wide_Type (Typ);
11546 Error_Msg_N
11547 ("incomplete view of tagged type should be declared tagged??",
11548 Parent (Current_Entity (Typ)));
11549 end if;
11550 return;
11552 else
11553 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11554 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11556 -- Type has already been inserted into the current scope. Remove
11557 -- it, and add incomplete declaration for type, so that subsequent
11558 -- anonymous access types can use it. The entity is unchained from
11559 -- the homonym list and from immediate visibility. After analysis,
11560 -- the entity in the incomplete declaration becomes immediately
11561 -- visible in the record declaration that follows.
11563 H := Current_Entity (Typ);
11565 if H = Typ then
11566 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11567 else
11568 while Present (H)
11569 and then Homonym (H) /= Typ
11570 loop
11571 H := Homonym (Typ);
11572 end loop;
11574 Set_Homonym (H, Homonym (Typ));
11575 end if;
11577 Insert_Before (Typ_Decl, Decl);
11578 Analyze (Decl);
11579 Set_Full_View (Inc_T, Typ);
11581 if Is_Tagged then
11583 -- Create a common class-wide type for both views, and set the
11584 -- Etype of the class-wide type to the full view.
11586 Make_Class_Wide_Type (Inc_T);
11587 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11588 Set_Etype (Class_Wide_Type (Typ), Typ);
11589 end if;
11590 end if;
11591 end Build_Incomplete_Type_Declaration;
11593 ------------------
11594 -- Designates_T --
11595 ------------------
11597 function Designates_T (Subt : Node_Id) return Boolean is
11598 Type_Id : constant Name_Id := Chars (Typ);
11600 function Names_T (Nam : Node_Id) return Boolean;
11601 -- The record type has not been introduced in the current scope
11602 -- yet, so we must examine the name of the type itself, either
11603 -- an identifier T, or an expanded name of the form P.T, where
11604 -- P denotes the current scope.
11606 -------------
11607 -- Names_T --
11608 -------------
11610 function Names_T (Nam : Node_Id) return Boolean is
11611 begin
11612 if Nkind (Nam) = N_Identifier then
11613 return Chars (Nam) = Type_Id;
11615 elsif Nkind (Nam) = N_Selected_Component then
11616 if Chars (Selector_Name (Nam)) = Type_Id then
11617 if Nkind (Prefix (Nam)) = N_Identifier then
11618 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11620 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11621 return Chars (Selector_Name (Prefix (Nam))) =
11622 Chars (Current_Scope);
11623 else
11624 return False;
11625 end if;
11627 else
11628 return False;
11629 end if;
11631 else
11632 return False;
11633 end if;
11634 end Names_T;
11636 -- Start of processing for Designates_T
11638 begin
11639 if Nkind (Subt) = N_Identifier then
11640 return Chars (Subt) = Type_Id;
11642 -- Reference can be through an expanded name which has not been
11643 -- analyzed yet, and which designates enclosing scopes.
11645 elsif Nkind (Subt) = N_Selected_Component then
11646 if Names_T (Subt) then
11647 return True;
11649 -- Otherwise it must denote an entity that is already visible.
11650 -- The access definition may name a subtype of the enclosing
11651 -- type, if there is a previous incomplete declaration for it.
11653 else
11654 Find_Selected_Component (Subt);
11655 return
11656 Is_Entity_Name (Subt)
11657 and then Scope (Entity (Subt)) = Current_Scope
11658 and then
11659 (Chars (Base_Type (Entity (Subt))) = Type_Id
11660 or else
11661 (Is_Class_Wide_Type (Entity (Subt))
11662 and then
11663 Chars (Etype (Base_Type (Entity (Subt)))) =
11664 Type_Id));
11665 end if;
11667 -- A reference to the current type may appear as the prefix of
11668 -- a 'Class attribute.
11670 elsif Nkind (Subt) = N_Attribute_Reference
11671 and then Attribute_Name (Subt) = Name_Class
11672 then
11673 return Names_T (Prefix (Subt));
11675 else
11676 return False;
11677 end if;
11678 end Designates_T;
11680 ----------------
11681 -- Mentions_T --
11682 ----------------
11684 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11685 Param_Spec : Node_Id;
11687 Acc_Subprg : constant Node_Id :=
11688 Access_To_Subprogram_Definition (Acc_Def);
11690 begin
11691 if No (Acc_Subprg) then
11692 return Designates_T (Subtype_Mark (Acc_Def));
11693 end if;
11695 -- Component is an access_to_subprogram: examine its formals,
11696 -- and result definition in the case of an access_to_function.
11698 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11699 while Present (Param_Spec) loop
11700 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11701 and then Mentions_T (Parameter_Type (Param_Spec))
11702 then
11703 return True;
11705 elsif Designates_T (Parameter_Type (Param_Spec)) then
11706 return True;
11707 end if;
11709 Next (Param_Spec);
11710 end loop;
11712 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11713 if Nkind (Result_Definition (Acc_Subprg)) =
11714 N_Access_Definition
11715 then
11716 return Mentions_T (Result_Definition (Acc_Subprg));
11717 else
11718 return Designates_T (Result_Definition (Acc_Subprg));
11719 end if;
11720 end if;
11722 return False;
11723 end Mentions_T;
11725 -- Start of processing for Check_Anonymous_Access_Component
11727 begin
11728 if Present (Access_Def) and then Mentions_T (Access_Def) then
11729 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
11731 Build_Incomplete_Type_Declaration;
11732 Anon_Access := Make_Temporary (Loc, 'S');
11734 -- Create a declaration for the anonymous access type: either
11735 -- an access_to_object or an access_to_subprogram.
11737 if Present (Acc_Def) then
11738 if Nkind (Acc_Def) = N_Access_Function_Definition then
11739 Type_Def :=
11740 Make_Access_Function_Definition (Loc,
11741 Parameter_Specifications =>
11742 Parameter_Specifications (Acc_Def),
11743 Result_Definition => Result_Definition (Acc_Def));
11744 else
11745 Type_Def :=
11746 Make_Access_Procedure_Definition (Loc,
11747 Parameter_Specifications =>
11748 Parameter_Specifications (Acc_Def));
11749 end if;
11751 else
11752 Type_Def :=
11753 Make_Access_To_Object_Definition (Loc,
11754 Subtype_Indication =>
11755 Relocate_Node (Subtype_Mark (Access_Def)));
11757 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
11758 Set_All_Present (Type_Def, All_Present (Access_Def));
11759 end if;
11761 Set_Null_Exclusion_Present
11762 (Type_Def, Null_Exclusion_Present (Access_Def));
11764 Decl :=
11765 Make_Full_Type_Declaration (Loc,
11766 Defining_Identifier => Anon_Access,
11767 Type_Definition => Type_Def);
11769 Insert_Before (Typ_Decl, Decl);
11770 Analyze (Decl);
11772 -- If an access to subprogram, create the extra formals
11774 if Present (Acc_Def) then
11775 Create_Extra_Formals (Designated_Type (Anon_Access));
11776 end if;
11778 if Nkind (Comp_Def) = N_Component_Definition then
11779 Rewrite (Comp_Def,
11780 Make_Component_Definition (Loc,
11781 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
11782 else
11783 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
11784 Rewrite (Comp_Def,
11785 Make_Discriminant_Specification (Loc,
11786 Defining_Identifier => Defining_Identifier (Comp_Def),
11787 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
11788 end if;
11790 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11791 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11792 else
11793 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
11794 end if;
11796 Set_Is_Local_Anonymous_Access (Anon_Access);
11797 end if;
11798 end Check_Anonymous_Access_Component;
11800 ---------------------------------------
11801 -- Check_Anonymous_Access_Components --
11802 ---------------------------------------
11804 procedure Check_Anonymous_Access_Components
11805 (Typ_Decl : Node_Id;
11806 Typ : Entity_Id;
11807 Prev : Entity_Id;
11808 Comp_List : Node_Id)
11810 Comp : Node_Id;
11811 begin
11812 if No (Comp_List) then
11813 return;
11814 end if;
11816 Comp := First (Component_Items (Comp_List));
11817 while Present (Comp) loop
11818 if Nkind (Comp) = N_Component_Declaration then
11819 Check_Anonymous_Access_Component
11820 (Typ_Decl, Typ, Prev,
11821 Component_Definition (Comp),
11822 Access_Definition (Component_Definition (Comp)));
11823 end if;
11825 Next (Comp);
11826 end loop;
11828 if Present (Variant_Part (Comp_List)) then
11829 declare
11830 V : Node_Id;
11831 begin
11832 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11833 while Present (V) loop
11834 Check_Anonymous_Access_Components
11835 (Typ_Decl, Typ, Prev, Component_List (V));
11836 Next_Non_Pragma (V);
11837 end loop;
11838 end;
11839 end if;
11840 end Check_Anonymous_Access_Components;
11842 ----------------------
11843 -- Check_Completion --
11844 ----------------------
11846 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11847 E : Entity_Id;
11849 procedure Post_Error;
11850 -- Post error message for lack of completion for entity E
11852 ----------------
11853 -- Post_Error --
11854 ----------------
11856 procedure Post_Error is
11857 procedure Missing_Body;
11858 -- Output missing body message
11860 ------------------
11861 -- Missing_Body --
11862 ------------------
11864 procedure Missing_Body is
11865 begin
11866 -- Spec is in same unit, so we can post on spec
11868 if In_Same_Source_Unit (Body_Id, E) then
11869 Error_Msg_N ("missing body for &", E);
11871 -- Spec is in a separate unit, so we have to post on the body
11873 else
11874 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11875 end if;
11876 end Missing_Body;
11878 -- Start of processing for Post_Error
11880 begin
11881 if not Comes_From_Source (E) then
11882 if Ekind (E) in E_Task_Type | E_Protected_Type then
11884 -- It may be an anonymous protected type created for a
11885 -- single variable. Post error on variable, if present.
11887 declare
11888 Var : Entity_Id;
11890 begin
11891 Var := First_Entity (Current_Scope);
11892 while Present (Var) loop
11893 exit when Etype (Var) = E
11894 and then Comes_From_Source (Var);
11896 Next_Entity (Var);
11897 end loop;
11899 if Present (Var) then
11900 E := Var;
11901 end if;
11902 end;
11903 end if;
11904 end if;
11906 -- If a generated entity has no completion, then either previous
11907 -- semantic errors have disabled the expansion phase, or else we had
11908 -- missing subunits, or else we are compiling without expansion,
11909 -- or else something is very wrong.
11911 if not Comes_From_Source (E) then
11912 pragma Assert
11913 (Serious_Errors_Detected > 0
11914 or else Configurable_Run_Time_Violations > 0
11915 or else Subunits_Missing
11916 or else not Expander_Active);
11917 return;
11919 -- Here for source entity
11921 else
11922 -- Here if no body to post the error message, so we post the error
11923 -- on the declaration that has no completion. This is not really
11924 -- the right place to post it, think about this later ???
11926 if No (Body_Id) then
11927 if Is_Type (E) then
11928 Error_Msg_NE
11929 ("missing full declaration for }", Parent (E), E);
11930 else
11931 Error_Msg_NE ("missing body for &", Parent (E), E);
11932 end if;
11934 -- Package body has no completion for a declaration that appears
11935 -- in the corresponding spec. Post error on the body, with a
11936 -- reference to the non-completed declaration.
11938 else
11939 Error_Msg_Sloc := Sloc (E);
11941 if Is_Type (E) then
11942 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11944 elsif Is_Overloadable (E)
11945 and then Current_Entity_In_Scope (E) /= E
11946 then
11947 -- It may be that the completion is mistyped and appears as
11948 -- a distinct overloading of the entity.
11950 declare
11951 Candidate : constant Entity_Id :=
11952 Current_Entity_In_Scope (E);
11953 Decl : constant Node_Id :=
11954 Unit_Declaration_Node (Candidate);
11956 begin
11957 if Is_Overloadable (Candidate)
11958 and then Ekind (Candidate) = Ekind (E)
11959 and then Nkind (Decl) = N_Subprogram_Body
11960 and then Acts_As_Spec (Decl)
11961 then
11962 Check_Type_Conformant (Candidate, E);
11964 else
11965 Missing_Body;
11966 end if;
11967 end;
11969 else
11970 Missing_Body;
11971 end if;
11972 end if;
11973 end if;
11974 end Post_Error;
11976 -- Local variables
11978 Pack_Id : constant Entity_Id := Current_Scope;
11980 -- Start of processing for Check_Completion
11982 begin
11983 E := First_Entity (Pack_Id);
11984 while Present (E) loop
11985 if Is_Intrinsic_Subprogram (E) then
11986 null;
11988 -- The following situation requires special handling: a child unit
11989 -- that appears in the context clause of the body of its parent:
11991 -- procedure Parent.Child (...);
11993 -- with Parent.Child;
11994 -- package body Parent is
11996 -- Here Parent.Child appears as a local entity, but should not be
11997 -- flagged as requiring completion, because it is a compilation
11998 -- unit.
12000 -- Ignore missing completion for a subprogram that does not come from
12001 -- source (including the _Call primitive operation of RAS types,
12002 -- which has to have the flag Comes_From_Source for other purposes):
12003 -- we assume that the expander will provide the missing completion.
12004 -- In case of previous errors, other expansion actions that provide
12005 -- bodies for null procedures with not be invoked, so inhibit message
12006 -- in those cases.
12008 -- Note that E_Operator is not in the list that follows, because
12009 -- this kind is reserved for predefined operators, that are
12010 -- intrinsic and do not need completion.
12012 elsif Ekind (E) in E_Function
12013 | E_Procedure
12014 | E_Generic_Function
12015 | E_Generic_Procedure
12016 then
12017 if Has_Completion (E) then
12018 null;
12020 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12021 null;
12023 elsif Is_Subprogram (E)
12024 and then (not Comes_From_Source (E)
12025 or else Chars (E) = Name_uCall)
12026 then
12027 null;
12029 elsif
12030 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12031 then
12032 null;
12034 elsif Nkind (Parent (E)) = N_Procedure_Specification
12035 and then Null_Present (Parent (E))
12036 and then Serious_Errors_Detected > 0
12037 then
12038 null;
12040 else
12041 Post_Error;
12042 end if;
12044 elsif Is_Entry (E) then
12045 if not Has_Completion (E)
12046 and then Ekind (Scope (E)) = E_Protected_Type
12047 then
12048 Post_Error;
12049 end if;
12051 elsif Is_Package_Or_Generic_Package (E) then
12052 if Unit_Requires_Body (E) then
12053 if not Has_Completion (E)
12054 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12055 N_Compilation_Unit
12056 then
12057 Post_Error;
12058 end if;
12060 elsif not Is_Child_Unit (E) then
12061 May_Need_Implicit_Body (E);
12062 end if;
12064 -- A formal incomplete type (Ada 2012) does not require a completion;
12065 -- other incomplete type declarations do.
12067 elsif Ekind (E) = E_Incomplete_Type then
12068 if No (Underlying_Type (E))
12069 and then not Is_Generic_Type (E)
12070 then
12071 Post_Error;
12072 end if;
12074 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12075 if not Has_Completion (E) then
12076 Post_Error;
12077 end if;
12079 -- A single task declared in the current scope is a constant, verify
12080 -- that the body of its anonymous type is in the same scope. If the
12081 -- task is defined elsewhere, this may be a renaming declaration for
12082 -- which no completion is needed.
12084 elsif Ekind (E) = E_Constant then
12085 if Ekind (Etype (E)) = E_Task_Type
12086 and then not Has_Completion (Etype (E))
12087 and then Scope (Etype (E)) = Current_Scope
12088 then
12089 Post_Error;
12090 end if;
12092 elsif Ekind (E) = E_Record_Type then
12093 if Is_Tagged_Type (E) then
12094 Check_Abstract_Overriding (E);
12095 Check_Conventions (E);
12096 end if;
12098 Check_Aliased_Component_Types (E);
12100 elsif Ekind (E) = E_Array_Type then
12101 Check_Aliased_Component_Types (E);
12103 end if;
12105 Next_Entity (E);
12106 end loop;
12107 end Check_Completion;
12109 -------------------------------------
12110 -- Check_Constraining_Discriminant --
12111 -------------------------------------
12113 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12115 New_Type : constant Entity_Id := Etype (New_Disc);
12116 Old_Type : Entity_Id;
12118 begin
12119 -- If the record type contains an array constrained by the discriminant
12120 -- but with some different bound, the compiler tries to create a smaller
12121 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12122 -- In this case, where the discriminant type is a scalar type, the check
12123 -- must use the original discriminant type in the parent declaration.
12125 if Is_Scalar_Type (New_Type) then
12126 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12127 else
12128 Old_Type := Etype (Old_Disc);
12129 end if;
12131 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12132 Error_Msg_N
12133 ("subtype must be statically compatible with parent discriminant",
12134 New_Disc);
12136 if not Predicates_Compatible (New_Type, Old_Type) then
12137 Error_Msg_N
12138 ("\subtype predicate is not compatible with parent discriminant",
12139 New_Disc);
12140 end if;
12141 end if;
12142 end Check_Constraining_Discriminant;
12144 ------------------------------------
12145 -- Check_CPP_Type_Has_No_Defaults --
12146 ------------------------------------
12148 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12149 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12150 Clist : Node_Id;
12151 Comp : Node_Id;
12153 begin
12154 -- Obtain the component list
12156 if Nkind (Tdef) = N_Record_Definition then
12157 Clist := Component_List (Tdef);
12158 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12159 Clist := Component_List (Record_Extension_Part (Tdef));
12160 end if;
12162 -- Check all components to ensure no default expressions
12164 if Present (Clist) then
12165 Comp := First (Component_Items (Clist));
12166 while Present (Comp) loop
12167 if Present (Expression (Comp)) then
12168 Error_Msg_N
12169 ("component of imported 'C'P'P type cannot have "
12170 & "default expression", Expression (Comp));
12171 end if;
12173 Next (Comp);
12174 end loop;
12175 end if;
12176 end Check_CPP_Type_Has_No_Defaults;
12178 ----------------------------
12179 -- Check_Delta_Expression --
12180 ----------------------------
12182 procedure Check_Delta_Expression (E : Node_Id) is
12183 begin
12184 if not (Is_Real_Type (Etype (E))) then
12185 Wrong_Type (E, Any_Real);
12187 elsif not Is_OK_Static_Expression (E) then
12188 Flag_Non_Static_Expr
12189 ("non-static expression used for delta value!", E);
12191 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12192 Error_Msg_N ("delta expression must be positive", E);
12194 else
12195 return;
12196 end if;
12198 -- If any of above errors occurred, then replace the incorrect
12199 -- expression by the real 0.1, which should prevent further errors.
12201 Rewrite (E,
12202 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12203 Analyze_And_Resolve (E, Standard_Float);
12204 end Check_Delta_Expression;
12206 -----------------------------
12207 -- Check_Digits_Expression --
12208 -----------------------------
12210 procedure Check_Digits_Expression (E : Node_Id) is
12211 begin
12212 if not (Is_Integer_Type (Etype (E))) then
12213 Wrong_Type (E, Any_Integer);
12215 elsif not Is_OK_Static_Expression (E) then
12216 Flag_Non_Static_Expr
12217 ("non-static expression used for digits value!", E);
12219 elsif Expr_Value (E) <= 0 then
12220 Error_Msg_N ("digits value must be greater than zero", E);
12222 else
12223 return;
12224 end if;
12226 -- If any of above errors occurred, then replace the incorrect
12227 -- expression by the integer 1, which should prevent further errors.
12229 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12230 Analyze_And_Resolve (E, Standard_Integer);
12232 end Check_Digits_Expression;
12234 --------------------------
12235 -- Check_Initialization --
12236 --------------------------
12238 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12239 begin
12240 -- Special processing for limited types
12242 if Is_Limited_Type (T)
12243 and then not In_Instance
12244 and then not In_Inlined_Body
12245 then
12246 if not OK_For_Limited_Init (T, Exp) then
12248 -- In GNAT mode, this is just a warning, to allow it to be evilly
12249 -- turned off. Otherwise it is a real error.
12251 if GNAT_Mode then
12252 Error_Msg_N
12253 ("??cannot initialize entities of limited type!", Exp);
12255 elsif Ada_Version < Ada_2005 then
12257 -- The side effect removal machinery may generate illegal Ada
12258 -- code to avoid the usage of access types and 'reference in
12259 -- SPARK mode. Since this is legal code with respect to theorem
12260 -- proving, do not emit the error.
12262 if GNATprove_Mode
12263 and then Nkind (Exp) = N_Function_Call
12264 and then Nkind (Parent (Exp)) = N_Object_Declaration
12265 and then not Comes_From_Source
12266 (Defining_Identifier (Parent (Exp)))
12267 then
12268 null;
12270 else
12271 Error_Msg_N
12272 ("cannot initialize entities of limited type", Exp);
12273 Explain_Limited_Type (T, Exp);
12274 end if;
12276 else
12277 -- Specialize error message according to kind of illegal
12278 -- initial expression. We check the Original_Node to cover
12279 -- cases where the initialization expression of an object
12280 -- declaration generated by the compiler has been rewritten
12281 -- (such as for dispatching calls).
12283 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12284 and then
12285 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12286 then
12287 -- No error for internally-generated object declarations,
12288 -- which can come from build-in-place assignment statements.
12290 if Nkind (Parent (Exp)) = N_Object_Declaration
12291 and then not Comes_From_Source
12292 (Defining_Identifier (Parent (Exp)))
12293 then
12294 null;
12296 else
12297 Error_Msg_N
12298 ("illegal context for call to function with limited "
12299 & "result", Exp);
12300 end if;
12302 else
12303 Error_Msg_N
12304 ("initialization of limited object requires aggregate or "
12305 & "function call", Exp);
12306 end if;
12307 end if;
12308 end if;
12309 end if;
12311 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12312 -- set unless we can be sure that no range check is required.
12314 if not Expander_Active
12315 and then Is_Scalar_Type (T)
12316 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12317 then
12318 Set_Do_Range_Check (Exp);
12319 end if;
12320 end Check_Initialization;
12322 ----------------------
12323 -- Check_Interfaces --
12324 ----------------------
12326 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12327 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12329 Iface : Node_Id;
12330 Iface_Def : Node_Id;
12331 Iface_Typ : Entity_Id;
12332 Parent_Node : Node_Id;
12334 Is_Task : Boolean := False;
12335 -- Set True if parent type or any progenitor is a task interface
12337 Is_Protected : Boolean := False;
12338 -- Set True if parent type or any progenitor is a protected interface
12340 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12341 -- Check that a progenitor is compatible with declaration. If an error
12342 -- message is output, it is posted on Error_Node.
12344 ------------------
12345 -- Check_Ifaces --
12346 ------------------
12348 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12349 Iface_Id : constant Entity_Id :=
12350 Defining_Identifier (Parent (Iface_Def));
12351 Type_Def : Node_Id;
12353 begin
12354 if Nkind (N) = N_Private_Extension_Declaration then
12355 Type_Def := N;
12356 else
12357 Type_Def := Type_Definition (N);
12358 end if;
12360 if Is_Task_Interface (Iface_Id) then
12361 Is_Task := True;
12363 elsif Is_Protected_Interface (Iface_Id) then
12364 Is_Protected := True;
12365 end if;
12367 if Is_Synchronized_Interface (Iface_Id) then
12369 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12370 -- extension derived from a synchronized interface must explicitly
12371 -- be declared synchronized, because the full view will be a
12372 -- synchronized type.
12374 if Nkind (N) = N_Private_Extension_Declaration then
12375 if not Synchronized_Present (N) then
12376 Error_Msg_NE
12377 ("private extension of& must be explicitly synchronized",
12378 N, Iface_Id);
12379 end if;
12381 -- However, by 3.9.4(16/2), a full type that is a record extension
12382 -- is never allowed to derive from a synchronized interface (note
12383 -- that interfaces must be excluded from this check, because those
12384 -- are represented by derived type definitions in some cases).
12386 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12387 and then not Interface_Present (Type_Definition (N))
12388 then
12389 Error_Msg_N ("record extension cannot derive from synchronized "
12390 & "interface", Error_Node);
12391 end if;
12392 end if;
12394 -- Check that the characteristics of the progenitor are compatible
12395 -- with the explicit qualifier in the declaration.
12396 -- The check only applies to qualifiers that come from source.
12397 -- Limited_Present also appears in the declaration of corresponding
12398 -- records, and the check does not apply to them.
12400 if Limited_Present (Type_Def)
12401 and then not
12402 Is_Concurrent_Record_Type (Defining_Identifier (N))
12403 then
12404 if Is_Limited_Interface (Parent_Type)
12405 and then not Is_Limited_Interface (Iface_Id)
12406 then
12407 Error_Msg_NE
12408 ("progenitor & must be limited interface",
12409 Error_Node, Iface_Id);
12411 elsif
12412 (Task_Present (Iface_Def)
12413 or else Protected_Present (Iface_Def)
12414 or else Synchronized_Present (Iface_Def))
12415 and then Nkind (N) /= N_Private_Extension_Declaration
12416 and then not Error_Posted (N)
12417 then
12418 Error_Msg_NE
12419 ("progenitor & must be limited interface",
12420 Error_Node, Iface_Id);
12421 end if;
12423 -- Protected interfaces can only inherit from limited, synchronized
12424 -- or protected interfaces.
12426 elsif Nkind (N) = N_Full_Type_Declaration
12427 and then Protected_Present (Type_Def)
12428 then
12429 if Limited_Present (Iface_Def)
12430 or else Synchronized_Present (Iface_Def)
12431 or else Protected_Present (Iface_Def)
12432 then
12433 null;
12435 elsif Task_Present (Iface_Def) then
12436 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12437 & "from task interface", Error_Node);
12439 else
12440 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12441 & "from non-limited interface", Error_Node);
12442 end if;
12444 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12445 -- limited and synchronized.
12447 elsif Synchronized_Present (Type_Def) then
12448 if Limited_Present (Iface_Def)
12449 or else Synchronized_Present (Iface_Def)
12450 then
12451 null;
12453 elsif Protected_Present (Iface_Def)
12454 and then Nkind (N) /= N_Private_Extension_Declaration
12455 then
12456 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12457 & "from protected interface", Error_Node);
12459 elsif Task_Present (Iface_Def)
12460 and then Nkind (N) /= N_Private_Extension_Declaration
12461 then
12462 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12463 & "from task interface", Error_Node);
12465 elsif not Is_Limited_Interface (Iface_Id) then
12466 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12467 & "from non-limited interface", Error_Node);
12468 end if;
12470 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12471 -- synchronized or task interfaces.
12473 elsif Nkind (N) = N_Full_Type_Declaration
12474 and then Task_Present (Type_Def)
12475 then
12476 if Limited_Present (Iface_Def)
12477 or else Synchronized_Present (Iface_Def)
12478 or else Task_Present (Iface_Def)
12479 then
12480 null;
12482 elsif Protected_Present (Iface_Def) then
12483 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12484 & "protected interface", Error_Node);
12486 else
12487 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12488 & "non-limited interface", Error_Node);
12489 end if;
12490 end if;
12491 end Check_Ifaces;
12493 -- Start of processing for Check_Interfaces
12495 begin
12496 if Is_Interface (Parent_Type) then
12497 if Is_Task_Interface (Parent_Type) then
12498 Is_Task := True;
12500 elsif Is_Protected_Interface (Parent_Type) then
12501 Is_Protected := True;
12502 end if;
12503 end if;
12505 if Nkind (N) = N_Private_Extension_Declaration then
12507 -- Check that progenitors are compatible with declaration
12509 Iface := First (Interface_List (Def));
12510 while Present (Iface) loop
12511 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12513 Parent_Node := Parent (Base_Type (Iface_Typ));
12514 Iface_Def := Type_Definition (Parent_Node);
12516 if not Is_Interface (Iface_Typ) then
12517 Diagnose_Interface (Iface, Iface_Typ);
12518 else
12519 Check_Ifaces (Iface_Def, Iface);
12520 end if;
12522 Next (Iface);
12523 end loop;
12525 if Is_Task and Is_Protected then
12526 Error_Msg_N
12527 ("type cannot derive from task and protected interface", N);
12528 end if;
12530 return;
12531 end if;
12533 -- Full type declaration of derived type.
12534 -- Check compatibility with parent if it is interface type
12536 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12537 and then Is_Interface (Parent_Type)
12538 then
12539 Parent_Node := Parent (Parent_Type);
12541 -- More detailed checks for interface varieties
12543 Check_Ifaces
12544 (Iface_Def => Type_Definition (Parent_Node),
12545 Error_Node => Subtype_Indication (Type_Definition (N)));
12546 end if;
12548 Iface := First (Interface_List (Def));
12549 while Present (Iface) loop
12550 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12552 Parent_Node := Parent (Base_Type (Iface_Typ));
12553 Iface_Def := Type_Definition (Parent_Node);
12555 if not Is_Interface (Iface_Typ) then
12556 Diagnose_Interface (Iface, Iface_Typ);
12558 else
12559 -- "The declaration of a specific descendant of an interface
12560 -- type freezes the interface type" RM 13.14
12562 Freeze_Before (N, Iface_Typ);
12563 Check_Ifaces (Iface_Def, Error_Node => Iface);
12564 end if;
12566 Next (Iface);
12567 end loop;
12569 if Is_Task and Is_Protected then
12570 Error_Msg_N
12571 ("type cannot derive from task and protected interface", N);
12572 end if;
12573 end Check_Interfaces;
12575 ------------------------------------
12576 -- Check_Or_Process_Discriminants --
12577 ------------------------------------
12579 -- If an incomplete or private type declaration was already given for the
12580 -- type, the discriminants may have already been processed if they were
12581 -- present on the incomplete declaration. In this case a full conformance
12582 -- check has been performed in Find_Type_Name, and we then recheck here
12583 -- some properties that can't be checked on the partial view alone.
12584 -- Otherwise we call Process_Discriminants.
12586 procedure Check_Or_Process_Discriminants
12587 (N : Node_Id;
12588 T : Entity_Id;
12589 Prev : Entity_Id := Empty)
12591 begin
12592 if Has_Discriminants (T) then
12594 -- Discriminants are already set on T if they were already present
12595 -- on the partial view. Make them visible to component declarations.
12597 declare
12598 D : Entity_Id;
12599 -- Discriminant on T (full view) referencing expr on partial view
12601 Prev_D : Entity_Id;
12602 -- Entity of corresponding discriminant on partial view
12604 New_D : Node_Id;
12605 -- Discriminant specification for full view, expression is
12606 -- the syntactic copy on full view (which has been checked for
12607 -- conformance with partial view), only used here to post error
12608 -- message.
12610 begin
12611 D := First_Discriminant (T);
12612 New_D := First (Discriminant_Specifications (N));
12613 while Present (D) loop
12614 Prev_D := Current_Entity (D);
12615 Set_Current_Entity (D);
12616 Set_Is_Immediately_Visible (D);
12617 Set_Homonym (D, Prev_D);
12619 -- Handle the case where there is an untagged partial view and
12620 -- the full view is tagged: must disallow discriminants with
12621 -- defaults, unless compiling for Ada 2012, which allows a
12622 -- limited tagged type to have defaulted discriminants (see
12623 -- AI05-0214). However, suppress error here if it was already
12624 -- reported on the default expression of the partial view.
12626 if Is_Tagged_Type (T)
12627 and then Present (Expression (Parent (D)))
12628 and then (not Is_Limited_Type (Current_Scope)
12629 or else Ada_Version < Ada_2012)
12630 and then not Error_Posted (Expression (Parent (D)))
12631 then
12632 if Ada_Version >= Ada_2012 then
12633 Error_Msg_N
12634 ("discriminants of nonlimited tagged type cannot have "
12635 & "defaults",
12636 Expression (New_D));
12637 else
12638 Error_Msg_N
12639 ("discriminants of tagged type cannot have defaults",
12640 Expression (New_D));
12641 end if;
12642 end if;
12644 -- Ada 2005 (AI-230): Access discriminant allowed in
12645 -- non-limited record types.
12647 if Ada_Version < Ada_2005 then
12649 -- This restriction gets applied to the full type here. It
12650 -- has already been applied earlier to the partial view.
12652 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12653 end if;
12655 Next_Discriminant (D);
12656 Next (New_D);
12657 end loop;
12658 end;
12660 elsif Present (Discriminant_Specifications (N)) then
12661 Process_Discriminants (N, Prev);
12662 end if;
12663 end Check_Or_Process_Discriminants;
12665 ----------------------
12666 -- Check_Real_Bound --
12667 ----------------------
12669 procedure Check_Real_Bound (Bound : Node_Id) is
12670 begin
12671 if not Is_Real_Type (Etype (Bound)) then
12672 Error_Msg_N
12673 ("bound in real type definition must be of real type", Bound);
12675 elsif not Is_OK_Static_Expression (Bound) then
12676 Flag_Non_Static_Expr
12677 ("non-static expression used for real type bound!", Bound);
12679 else
12680 return;
12681 end if;
12683 Rewrite
12684 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12685 Analyze (Bound);
12686 Resolve (Bound, Standard_Float);
12687 end Check_Real_Bound;
12689 ------------------------------
12690 -- Complete_Private_Subtype --
12691 ------------------------------
12693 procedure Complete_Private_Subtype
12694 (Priv : Entity_Id;
12695 Full : Entity_Id;
12696 Full_Base : Entity_Id;
12697 Related_Nod : Node_Id)
12699 Save_Next_Entity : Entity_Id;
12700 Save_Homonym : Entity_Id;
12702 begin
12703 -- Set semantic attributes for (implicit) private subtype completion.
12704 -- If the full type has no discriminants, then it is a copy of the
12705 -- full view of the base. Otherwise, it is a subtype of the base with
12706 -- a possible discriminant constraint. Save and restore the original
12707 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12708 -- not corrupt the entity chain.
12710 Save_Next_Entity := Next_Entity (Full);
12711 Save_Homonym := Homonym (Priv);
12713 if Is_Private_Type (Full_Base)
12714 or else Is_Record_Type (Full_Base)
12715 or else Is_Concurrent_Type (Full_Base)
12716 then
12717 Copy_Node (Priv, Full);
12719 -- Note that the Etype of the full view is the same as the Etype of
12720 -- the partial view. In this fashion, the subtype has access to the
12721 -- correct view of the parent.
12723 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12724 Set_Has_Unknown_Discriminants
12725 (Full, Has_Unknown_Discriminants (Full_Base));
12726 Set_First_Entity (Full, First_Entity (Full_Base));
12727 Set_Last_Entity (Full, Last_Entity (Full_Base));
12729 -- If the underlying base type is constrained, we know that the
12730 -- full view of the subtype is constrained as well (the converse
12731 -- is not necessarily true).
12733 if Is_Constrained (Full_Base) then
12734 Set_Is_Constrained (Full);
12735 end if;
12737 else
12738 Copy_Node (Full_Base, Full);
12740 -- The following subtlety with the Etype of the full view needs to be
12741 -- taken into account here. One could think that it must naturally be
12742 -- set to the base type of the full base:
12744 -- Set_Etype (Full, Base_Type (Full_Base));
12746 -- so that the full view becomes a subtype of the full base when the
12747 -- latter is a base type, which must for example happen when the full
12748 -- base is declared as derived type. That's also correct if the full
12749 -- base is declared as an array type, or a floating-point type, or a
12750 -- fixed-point type, or a signed integer type, as these declarations
12751 -- create an implicit base type and a first subtype so the Etype of
12752 -- the full views must be the implicit base type. But that's wrong
12753 -- if the full base is declared as an access type, or an enumeration
12754 -- type, or a modular integer type, as these declarations directly
12755 -- create a base type, i.e. with Etype pointing to itself. Moreover
12756 -- the full base being declared in the private part, i.e. when the
12757 -- views are swapped, the end result is that the Etype of the full
12758 -- base is set to its private view in this case and that we need to
12759 -- propagate this setting to the full view in order for the subtype
12760 -- to be compatible with the base type.
12762 if Is_Base_Type (Full_Base)
12763 and then (Is_Derived_Type (Full_Base)
12764 or else Ekind (Full_Base) in Array_Kind
12765 or else Ekind (Full_Base) in Fixed_Point_Kind
12766 or else Ekind (Full_Base) in Float_Kind
12767 or else Ekind (Full_Base) in Signed_Integer_Kind)
12768 then
12769 Set_Etype (Full, Full_Base);
12770 end if;
12772 Set_Chars (Full, Chars (Priv));
12773 Set_Sloc (Full, Sloc (Priv));
12774 Conditional_Delay (Full, Priv);
12775 end if;
12777 Link_Entities (Full, Save_Next_Entity);
12778 Set_Homonym (Full, Save_Homonym);
12779 Set_Associated_Node_For_Itype (Full, Related_Nod);
12781 if Ekind (Full) in Incomplete_Or_Private_Kind then
12782 Reinit_Field_To_Zero (Full, F_Private_Dependents);
12783 end if;
12785 -- Set common attributes for all subtypes: kind, convention, etc.
12787 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12788 Set_Convention (Full, Convention (Full_Base));
12789 Set_Is_First_Subtype (Full, False);
12790 Set_Scope (Full, Scope (Priv));
12791 Set_Size_Info (Full, Full_Base);
12792 Copy_RM_Size (To => Full, From => Full_Base);
12793 Set_Is_Itype (Full);
12795 -- A subtype of a private-type-without-discriminants, whose full-view
12796 -- has discriminants with default expressions, is not constrained.
12798 if not Has_Discriminants (Priv) then
12799 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12801 if Has_Discriminants (Full_Base) then
12802 Set_Discriminant_Constraint
12803 (Full, Discriminant_Constraint (Full_Base));
12805 -- The partial view may have been indefinite, the full view
12806 -- might not be.
12808 Set_Has_Unknown_Discriminants
12809 (Full, Has_Unknown_Discriminants (Full_Base));
12810 end if;
12811 end if;
12813 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12814 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12816 -- Freeze the private subtype entity if its parent is delayed, and not
12817 -- already frozen. We skip this processing if the type is an anonymous
12818 -- subtype of a record component, or is the corresponding record of a
12819 -- protected type, since these are processed when the enclosing type
12820 -- is frozen. If the parent type is declared in a nested package then
12821 -- the freezing of the private and full views also happens later.
12823 if not Is_Type (Scope (Full)) then
12824 if Is_Itype (Priv)
12825 and then In_Same_Source_Unit (Full, Full_Base)
12826 and then Scope (Full_Base) /= Scope (Full)
12827 then
12828 Set_Has_Delayed_Freeze (Full);
12829 Set_Has_Delayed_Freeze (Priv);
12831 else
12832 Set_Has_Delayed_Freeze (Full,
12833 Has_Delayed_Freeze (Full_Base)
12834 and then not Is_Frozen (Full_Base));
12835 end if;
12836 end if;
12838 Set_Freeze_Node (Full, Empty);
12839 Set_Is_Frozen (Full, False);
12841 if Has_Discriminants (Full) then
12842 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12843 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12845 if Has_Unknown_Discriminants (Full) then
12846 Set_Discriminant_Constraint (Full, No_Elist);
12847 end if;
12848 end if;
12850 if Ekind (Full_Base) = E_Record_Type
12851 and then Has_Discriminants (Full_Base)
12852 and then Has_Discriminants (Priv) -- might not, if errors
12853 and then not Has_Unknown_Discriminants (Priv)
12854 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12855 then
12856 Create_Constrained_Components
12857 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12859 -- If the full base is itself derived from private, build a congruent
12860 -- subtype of its underlying full view, for use by the back end.
12862 elsif Is_Private_Type (Full_Base)
12863 and then Present (Underlying_Full_View (Full_Base))
12864 then
12865 declare
12866 Underlying_Full_Base : constant Entity_Id
12867 := Underlying_Full_View (Full_Base);
12868 Underlying_Full : constant Entity_Id
12869 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12870 begin
12871 Set_Is_Itype (Underlying_Full);
12872 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12873 Complete_Private_Subtype
12874 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12875 Set_Underlying_Full_View (Full, Underlying_Full);
12876 Set_Is_Underlying_Full_View (Underlying_Full);
12877 end;
12879 elsif Is_Record_Type (Full_Base) then
12881 -- Show Full is simply a renaming of Full_Base
12883 Set_Cloned_Subtype (Full, Full_Base);
12884 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12886 -- Propagate predicates
12888 Propagate_Predicate_Attributes (Full, Full_Base);
12889 end if;
12891 -- It is unsafe to share the bounds of a scalar type, because the Itype
12892 -- is elaborated on demand, and if a bound is nonstatic, then different
12893 -- orders of elaboration in different units will lead to different
12894 -- external symbols.
12896 if Is_Scalar_Type (Full_Base) then
12897 Set_Scalar_Range (Full,
12898 Make_Range (Sloc (Related_Nod),
12899 Low_Bound =>
12900 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12901 High_Bound =>
12902 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12904 -- This completion inherits the bounds of the full parent, but if
12905 -- the parent is an unconstrained floating point type, so is the
12906 -- completion.
12908 if Is_Floating_Point_Type (Full_Base) then
12909 Set_Includes_Infinities
12910 (Scalar_Range (Full), Has_Infinities (Full_Base));
12911 end if;
12912 end if;
12914 -- ??? It seems that a lot of fields are missing that should be copied
12915 -- from Full_Base to Full. Here are some that are introduced in a
12916 -- non-disruptive way but a cleanup is necessary.
12918 if Is_Tagged_Type (Full_Base) then
12919 Set_Is_Tagged_Type (Full);
12920 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12922 Set_Direct_Primitive_Operations
12923 (Full, Direct_Primitive_Operations (Full_Base));
12924 Set_No_Tagged_Streams_Pragma
12925 (Full, No_Tagged_Streams_Pragma (Full_Base));
12927 if Is_Interface (Full_Base) then
12928 Set_Is_Interface (Full);
12929 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
12930 end if;
12932 -- Inherit class_wide type of full_base in case the partial view was
12933 -- not tagged. Otherwise it has already been created when the private
12934 -- subtype was analyzed.
12936 if No (Class_Wide_Type (Full)) then
12937 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12938 end if;
12940 -- If this is a subtype of a protected or task type, constrain its
12941 -- corresponding record, unless this is a subtype without constraints,
12942 -- i.e. a simple renaming as with an actual subtype in an instance.
12944 elsif Is_Concurrent_Type (Full_Base) then
12945 if Has_Discriminants (Full)
12946 and then Present (Corresponding_Record_Type (Full_Base))
12947 and then
12948 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12949 then
12950 Set_Corresponding_Record_Type (Full,
12951 Constrain_Corresponding_Record
12952 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12954 else
12955 Set_Corresponding_Record_Type (Full,
12956 Corresponding_Record_Type (Full_Base));
12957 end if;
12958 end if;
12960 -- Link rep item chain, and also setting of Has_Predicates from private
12961 -- subtype to full subtype, since we will need these on the full subtype
12962 -- to create the predicate function. Note that the full subtype may
12963 -- already have rep items, inherited from the full view of the base
12964 -- type, so we must be sure not to overwrite these entries.
12966 declare
12967 Append : Boolean;
12968 Item : Node_Id;
12969 Next_Item : Node_Id;
12970 Priv_Item : Node_Id;
12972 begin
12973 Item := First_Rep_Item (Full);
12974 Priv_Item := First_Rep_Item (Priv);
12976 -- If no existing rep items on full type, we can just link directly
12977 -- to the list of items on the private type, if any exist.. Same if
12978 -- the rep items are only those inherited from the base
12980 if (No (Item)
12981 or else Nkind (Item) /= N_Aspect_Specification
12982 or else Entity (Item) = Full_Base)
12983 and then Present (First_Rep_Item (Priv))
12984 then
12985 Set_First_Rep_Item (Full, Priv_Item);
12987 -- Otherwise, search to the end of items currently linked to the full
12988 -- subtype and append the private items to the end. However, if Priv
12989 -- and Full already have the same list of rep items, then the append
12990 -- is not done, as that would create a circularity.
12992 -- The partial view may have a predicate and the rep item lists of
12993 -- both views agree when inherited from the same ancestor. In that
12994 -- case, simply propagate the list from one view to the other.
12995 -- A more complex analysis needed here ???
12997 elsif Present (Priv_Item)
12998 and then Item = Next_Rep_Item (Priv_Item)
12999 then
13000 Set_First_Rep_Item (Full, Priv_Item);
13002 elsif Item /= Priv_Item then
13003 Append := True;
13004 loop
13005 Next_Item := Next_Rep_Item (Item);
13006 exit when No (Next_Item);
13007 Item := Next_Item;
13009 -- If the private view has aspect specifications, the full view
13010 -- inherits them. Since these aspects may already have been
13011 -- attached to the full view during derivation, do not append
13012 -- them if already present.
13014 if Item = First_Rep_Item (Priv) then
13015 Append := False;
13016 exit;
13017 end if;
13018 end loop;
13020 -- And link the private type items at the end of the chain
13022 if Append then
13023 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13024 end if;
13025 end if;
13026 end;
13028 -- Make sure Has_Predicates is set on full type if it is set on the
13029 -- private type. Note that it may already be set on the full type and
13030 -- if so, we don't want to unset it. Similarly, propagate information
13031 -- about delayed aspects, because the corresponding pragmas must be
13032 -- analyzed when one of the views is frozen. This last step is needed
13033 -- in particular when the full type is a scalar type for which an
13034 -- anonymous base type is constructed.
13036 -- The predicate functions are generated either at the freeze point
13037 -- of the type or at the end of the visible part, and we must avoid
13038 -- generating them twice.
13040 Propagate_Predicate_Attributes (Full, Priv);
13042 if Has_Delayed_Aspects (Priv) then
13043 Set_Has_Delayed_Aspects (Full);
13044 end if;
13045 end Complete_Private_Subtype;
13047 ----------------------------
13048 -- Constant_Redeclaration --
13049 ----------------------------
13051 procedure Constant_Redeclaration
13052 (Id : Entity_Id;
13053 N : Node_Id;
13054 T : out Entity_Id)
13056 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13057 Obj_Def : constant Node_Id := Object_Definition (N);
13058 New_T : Entity_Id;
13060 procedure Check_Possible_Deferred_Completion
13061 (Prev_Id : Entity_Id;
13062 Prev_Obj_Def : Node_Id;
13063 Curr_Obj_Def : Node_Id);
13064 -- Determine whether the two object definitions describe the partial
13065 -- and the full view of a constrained deferred constant. Generate
13066 -- a subtype for the full view and verify that it statically matches
13067 -- the subtype of the partial view.
13069 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13070 -- If deferred constant is an access type initialized with an allocator,
13071 -- check whether there is an illegal recursion in the definition,
13072 -- through a default value of some record subcomponent. This is normally
13073 -- detected when generating init procs, but requires this additional
13074 -- mechanism when expansion is disabled.
13076 ----------------------------------------
13077 -- Check_Possible_Deferred_Completion --
13078 ----------------------------------------
13080 procedure Check_Possible_Deferred_Completion
13081 (Prev_Id : Entity_Id;
13082 Prev_Obj_Def : Node_Id;
13083 Curr_Obj_Def : Node_Id)
13085 begin
13086 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
13087 and then Present (Constraint (Prev_Obj_Def))
13088 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
13089 and then Present (Constraint (Curr_Obj_Def))
13090 then
13091 declare
13092 Loc : constant Source_Ptr := Sloc (N);
13093 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13094 Decl : constant Node_Id :=
13095 Make_Subtype_Declaration (Loc,
13096 Defining_Identifier => Def_Id,
13097 Subtype_Indication =>
13098 Relocate_Node (Curr_Obj_Def));
13100 begin
13101 Insert_Before_And_Analyze (N, Decl);
13102 Set_Etype (Id, Def_Id);
13104 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
13105 Error_Msg_Sloc := Sloc (Prev_Id);
13106 Error_Msg_N ("subtype does not statically match deferred "
13107 & "declaration #", N);
13108 end if;
13109 end;
13110 end if;
13111 end Check_Possible_Deferred_Completion;
13113 ---------------------------------
13114 -- Check_Recursive_Declaration --
13115 ---------------------------------
13117 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13118 Comp : Entity_Id;
13120 begin
13121 if Is_Record_Type (Typ) then
13122 Comp := First_Component (Typ);
13123 while Present (Comp) loop
13124 if Comes_From_Source (Comp) then
13125 if Present (Expression (Parent (Comp)))
13126 and then Is_Entity_Name (Expression (Parent (Comp)))
13127 and then Entity (Expression (Parent (Comp))) = Prev
13128 then
13129 Error_Msg_Sloc := Sloc (Parent (Comp));
13130 Error_Msg_NE
13131 ("illegal circularity with declaration for & #",
13132 N, Comp);
13133 return;
13135 elsif Is_Record_Type (Etype (Comp)) then
13136 Check_Recursive_Declaration (Etype (Comp));
13137 end if;
13138 end if;
13140 Next_Component (Comp);
13141 end loop;
13142 end if;
13143 end Check_Recursive_Declaration;
13145 -- Start of processing for Constant_Redeclaration
13147 begin
13148 if Nkind (Parent (Prev)) = N_Object_Declaration then
13149 if Nkind (Object_Definition
13150 (Parent (Prev))) = N_Subtype_Indication
13151 then
13152 -- Find type of new declaration. The constraints of the two
13153 -- views must match statically, but there is no point in
13154 -- creating an itype for the full view.
13156 if Nkind (Obj_Def) = N_Subtype_Indication then
13157 Find_Type (Subtype_Mark (Obj_Def));
13158 New_T := Entity (Subtype_Mark (Obj_Def));
13160 else
13161 Find_Type (Obj_Def);
13162 New_T := Entity (Obj_Def);
13163 end if;
13165 T := Etype (Prev);
13167 else
13168 -- The full view may impose a constraint, even if the partial
13169 -- view does not, so construct the subtype.
13171 New_T := Find_Type_Of_Object (Obj_Def, N);
13172 T := New_T;
13173 end if;
13175 else
13176 -- Current declaration is illegal, diagnosed below in Enter_Name
13178 T := Empty;
13179 New_T := Any_Type;
13180 end if;
13182 -- If previous full declaration or a renaming declaration exists, or if
13183 -- a homograph is present, let Enter_Name handle it, either with an
13184 -- error or with the removal of an overridden implicit subprogram.
13185 -- The previous one is a full declaration if it has an expression
13186 -- (which in the case of an aggregate is indicated by the Init flag).
13188 if Ekind (Prev) /= E_Constant
13189 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13190 or else Present (Expression (Parent (Prev)))
13191 or else Has_Init_Expression (Parent (Prev))
13192 or else Present (Full_View (Prev))
13193 then
13194 Enter_Name (Id);
13196 -- Verify that types of both declarations match, or else that both types
13197 -- are anonymous access types whose designated subtypes statically match
13198 -- (as allowed in Ada 2005 by AI-385).
13200 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13201 and then
13202 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13203 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13204 or else Is_Access_Constant (Etype (New_T)) /=
13205 Is_Access_Constant (Etype (Prev))
13206 or else Can_Never_Be_Null (Etype (New_T)) /=
13207 Can_Never_Be_Null (Etype (Prev))
13208 or else Null_Exclusion_Present (Parent (Prev)) /=
13209 Null_Exclusion_Present (Parent (Id))
13210 or else not Subtypes_Statically_Match
13211 (Designated_Type (Etype (Prev)),
13212 Designated_Type (Etype (New_T))))
13213 then
13214 Error_Msg_Sloc := Sloc (Prev);
13215 Error_Msg_N ("type does not match declaration#", N);
13216 Set_Full_View (Prev, Id);
13217 Set_Etype (Id, Any_Type);
13219 -- A deferred constant whose type is an anonymous array is always
13220 -- illegal (unless imported). A detailed error message might be
13221 -- helpful for Ada beginners.
13223 if Nkind (Object_Definition (Parent (Prev)))
13224 = N_Constrained_Array_Definition
13225 and then Nkind (Object_Definition (N))
13226 = N_Constrained_Array_Definition
13227 then
13228 Error_Msg_N ("\each anonymous array is a distinct type", N);
13229 Error_Msg_N ("a deferred constant must have a named type",
13230 Object_Definition (Parent (Prev)));
13231 end if;
13233 elsif
13234 Null_Exclusion_Present (Parent (Prev))
13235 and then not Null_Exclusion_Present (N)
13236 then
13237 Error_Msg_Sloc := Sloc (Prev);
13238 Error_Msg_N ("null-exclusion does not match declaration#", N);
13239 Set_Full_View (Prev, Id);
13240 Set_Etype (Id, Any_Type);
13242 -- If so, process the full constant declaration
13244 else
13245 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13246 -- the deferred declaration is constrained, then the subtype defined
13247 -- by the subtype_indication in the full declaration shall match it
13248 -- statically.
13250 Check_Possible_Deferred_Completion
13251 (Prev_Id => Prev,
13252 Prev_Obj_Def => Object_Definition (Parent (Prev)),
13253 Curr_Obj_Def => Obj_Def);
13255 Set_Full_View (Prev, Id);
13256 Set_Is_Public (Id, Is_Public (Prev));
13257 Set_Is_Internal (Id);
13258 Append_Entity (Id, Current_Scope);
13260 -- Check ALIASED present if present before (RM 7.4(7))
13262 if Is_Aliased (Prev)
13263 and then not Aliased_Present (N)
13264 then
13265 Error_Msg_Sloc := Sloc (Prev);
13266 Error_Msg_N ("ALIASED required (see declaration #)", N);
13267 end if;
13269 -- Check that placement is in private part and that the incomplete
13270 -- declaration appeared in the visible part.
13272 if Ekind (Current_Scope) = E_Package
13273 and then not In_Private_Part (Current_Scope)
13274 then
13275 Error_Msg_Sloc := Sloc (Prev);
13276 Error_Msg_N
13277 ("full constant for declaration # must be in private part", N);
13279 elsif Ekind (Current_Scope) = E_Package
13280 and then
13281 List_Containing (Parent (Prev)) /=
13282 Visible_Declarations (Package_Specification (Current_Scope))
13283 then
13284 Error_Msg_N
13285 ("deferred constant must be declared in visible part",
13286 Parent (Prev));
13287 end if;
13289 if Is_Access_Type (T)
13290 and then Nkind (Expression (N)) = N_Allocator
13291 then
13292 Check_Recursive_Declaration (Designated_Type (T));
13293 end if;
13295 -- A deferred constant is a visible entity. If type has invariants,
13296 -- verify that the initial value satisfies them. This is not done in
13297 -- GNATprove mode, as GNATprove handles invariant checks itself.
13299 if Has_Invariants (T)
13300 and then Present (Invariant_Procedure (T))
13301 and then not GNATprove_Mode
13302 then
13303 Insert_After (N,
13304 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13305 end if;
13306 end if;
13307 end Constant_Redeclaration;
13309 ----------------------
13310 -- Constrain_Access --
13311 ----------------------
13313 procedure Constrain_Access
13314 (Def_Id : in out Entity_Id;
13315 S : Node_Id;
13316 Related_Nod : Node_Id)
13318 T : constant Entity_Id := Entity (Subtype_Mark (S));
13319 Desig_Type : constant Entity_Id := Designated_Type (T);
13320 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
13321 Constraint_OK : Boolean := True;
13323 begin
13324 if Is_Array_Type (Desig_Type) then
13325 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13327 elsif (Is_Record_Type (Desig_Type)
13328 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13329 and then not Is_Constrained (Desig_Type)
13330 then
13331 -- If this is a constrained access definition for a record
13332 -- component, we leave the type as an unconstrained access,
13333 -- and mark the component so that its actual type is built
13334 -- at a point of use (e.g., an assignment statement). This
13335 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13337 if Desig_Type = Current_Scope
13338 and then No (Def_Id)
13339 then
13340 Desig_Subtype :=
13341 Create_Itype
13342 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13343 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13344 Def_Id := Entity (Subtype_Mark (S));
13346 -- We indicate that the component has a per-object constraint
13347 -- for treatment at a point of use, even though the constraint
13348 -- may be independent of discriminants of the enclosing type.
13350 if Nkind (Related_Nod) = N_Component_Declaration then
13351 Set_Has_Per_Object_Constraint
13352 (Defining_Identifier (Related_Nod));
13353 end if;
13355 -- This call added to ensure that the constraint is analyzed
13356 -- (needed for a B test). Note that we still return early from
13357 -- this procedure to avoid recursive processing.
13359 Constrain_Discriminated_Type
13360 (Desig_Subtype, S, Related_Nod, For_Access => True);
13361 return;
13362 end if;
13364 -- Enforce rule that the constraint is illegal if there is an
13365 -- unconstrained view of the designated type. This means that the
13366 -- partial view (either a private type declaration or a derivation
13367 -- from a private type) has no discriminants. (Defect Report
13368 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13370 -- Rule updated for Ada 2005: The private type is said to have
13371 -- a constrained partial view, given that objects of the type
13372 -- can be declared. Furthermore, the rule applies to all access
13373 -- types, unlike the rule concerning default discriminants (see
13374 -- RM 3.7.1(7/3))
13376 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13377 and then Has_Private_Declaration (Desig_Type)
13378 and then In_Open_Scopes (Scope (Desig_Type))
13379 and then Has_Discriminants (Desig_Type)
13380 then
13381 declare
13382 Pack : constant Node_Id :=
13383 Unit_Declaration_Node (Scope (Desig_Type));
13384 Decls : List_Id;
13385 Decl : Node_Id;
13387 begin
13388 if Nkind (Pack) = N_Package_Declaration then
13389 Decls := Visible_Declarations (Specification (Pack));
13390 Decl := First (Decls);
13391 while Present (Decl) loop
13392 if (Nkind (Decl) = N_Private_Type_Declaration
13393 and then Chars (Defining_Identifier (Decl)) =
13394 Chars (Desig_Type))
13396 or else
13397 (Nkind (Decl) = N_Full_Type_Declaration
13398 and then
13399 Chars (Defining_Identifier (Decl)) =
13400 Chars (Desig_Type)
13401 and then Is_Derived_Type (Desig_Type)
13402 and then
13403 Has_Private_Declaration (Etype (Desig_Type)))
13404 then
13405 if No (Discriminant_Specifications (Decl)) then
13406 Error_Msg_N
13407 ("cannot constrain access type if designated "
13408 & "type has constrained partial view", S);
13409 end if;
13411 exit;
13412 end if;
13414 Next (Decl);
13415 end loop;
13416 end if;
13417 end;
13418 end if;
13420 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13421 For_Access => True);
13423 elsif Is_Concurrent_Type (Desig_Type)
13424 and then not Is_Constrained (Desig_Type)
13425 then
13426 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13428 else
13429 Error_Msg_N ("invalid constraint on access type", S);
13431 -- We simply ignore an invalid constraint
13433 Desig_Subtype := Desig_Type;
13434 Constraint_OK := False;
13435 end if;
13437 if No (Def_Id) then
13438 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13439 else
13440 Mutate_Ekind (Def_Id, E_Access_Subtype);
13441 end if;
13443 if Constraint_OK then
13444 Set_Etype (Def_Id, Base_Type (T));
13446 if Is_Private_Type (Desig_Type) then
13447 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13448 end if;
13449 else
13450 Set_Etype (Def_Id, Any_Type);
13451 end if;
13453 Set_Size_Info (Def_Id, T);
13454 Set_Is_Constrained (Def_Id, Constraint_OK);
13455 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13456 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13457 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13459 Conditional_Delay (Def_Id, T);
13461 -- AI-363 : Subtypes of general access types whose designated types have
13462 -- default discriminants are disallowed. In instances, the rule has to
13463 -- be checked against the actual, of which T is the subtype. In a
13464 -- generic body, the rule is checked assuming that the actual type has
13465 -- defaulted discriminants.
13467 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13468 if Ekind (Base_Type (T)) = E_General_Access_Type
13469 and then Has_Defaulted_Discriminants (Desig_Type)
13470 then
13471 if Ada_Version < Ada_2005 then
13472 Error_Msg_N
13473 ("access subtype of general access type would not " &
13474 "be allowed in Ada 2005?y?", S);
13475 else
13476 Error_Msg_N
13477 ("access subtype of general access type not allowed", S);
13478 end if;
13480 Error_Msg_N ("\discriminants have defaults", S);
13482 elsif Is_Access_Type (T)
13483 and then Is_Generic_Type (Desig_Type)
13484 and then Has_Discriminants (Desig_Type)
13485 and then In_Package_Body (Current_Scope)
13486 then
13487 if Ada_Version < Ada_2005 then
13488 Error_Msg_N
13489 ("access subtype would not be allowed in generic body "
13490 & "in Ada 2005?y?", S);
13491 else
13492 Error_Msg_N
13493 ("access subtype not allowed in generic body", S);
13494 end if;
13496 Error_Msg_N
13497 ("\designated type is a discriminated formal", S);
13498 end if;
13499 end if;
13500 end Constrain_Access;
13502 ---------------------
13503 -- Constrain_Array --
13504 ---------------------
13506 procedure Constrain_Array
13507 (Def_Id : in out Entity_Id;
13508 SI : Node_Id;
13509 Related_Nod : Node_Id;
13510 Related_Id : Entity_Id;
13511 Suffix : Character)
13513 C : constant Node_Id := Constraint (SI);
13514 Number_Of_Constraints : Nat := 0;
13515 Index : Node_Id;
13516 S, T : Entity_Id;
13517 Constraint_OK : Boolean := True;
13518 Is_FLB_Array_Subtype : Boolean := False;
13520 begin
13521 T := Entity (Subtype_Mark (SI));
13523 if Is_Access_Type (T) then
13524 T := Designated_Type (T);
13525 end if;
13527 -- If an index constraint follows a subtype mark in a subtype indication
13528 -- then the type or subtype denoted by the subtype mark must not already
13529 -- impose an index constraint. The subtype mark must denote either an
13530 -- unconstrained array type or an access type whose designated type
13531 -- is such an array type... (RM 3.6.1)
13533 if Is_Constrained (T) then
13534 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13535 Constraint_OK := False;
13537 else
13538 S := First (Constraints (C));
13539 while Present (S) loop
13540 Number_Of_Constraints := Number_Of_Constraints + 1;
13541 Next (S);
13542 end loop;
13544 -- In either case, the index constraint must provide a discrete
13545 -- range for each index of the array type and the type of each
13546 -- discrete range must be the same as that of the corresponding
13547 -- index. (RM 3.6.1)
13549 if Number_Of_Constraints /= Number_Dimensions (T) then
13550 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13551 Constraint_OK := False;
13553 else
13554 S := First (Constraints (C));
13555 Index := First_Index (T);
13556 Analyze (Index);
13558 -- Apply constraints to each index type
13560 for J in 1 .. Number_Of_Constraints loop
13561 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13563 -- If the subtype of the index has been set to indicate that
13564 -- it has a fixed lower bound, then record that the subtype's
13565 -- entity will need to be marked as being a fixed-lower-bound
13566 -- array subtype.
13568 if S = First (Constraints (C)) then
13569 Is_FLB_Array_Subtype :=
13570 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13572 -- If the parent subtype (or should this be Etype of that?)
13573 -- is an FLB array subtype, we flag an error, because we
13574 -- don't currently allow subtypes of such subtypes to
13575 -- specify a fixed lower bound for any of their indexes,
13576 -- even if the index of the parent subtype is a "range <>"
13577 -- index.
13579 if Is_FLB_Array_Subtype
13580 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13581 then
13582 Error_Msg_NE
13583 ("index with fixed lower bound not allowed for subtype "
13584 & "of fixed-lower-bound }", S, T);
13586 Is_FLB_Array_Subtype := False;
13587 end if;
13589 elsif Is_FLB_Array_Subtype
13590 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13591 then
13592 Error_Msg_NE
13593 ("constrained index not allowed for fixed-lower-bound "
13594 & "subtype of}", S, T);
13596 elsif not Is_FLB_Array_Subtype
13597 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13598 then
13599 Error_Msg_NE
13600 ("index with fixed lower bound not allowed for "
13601 & "constrained subtype of}", S, T);
13602 end if;
13604 Next (Index);
13605 Next (S);
13606 end loop;
13608 end if;
13609 end if;
13611 if No (Def_Id) then
13612 Def_Id :=
13613 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13614 Set_Parent (Def_Id, Related_Nod);
13616 else
13617 Mutate_Ekind (Def_Id, E_Array_Subtype);
13618 end if;
13620 Set_Size_Info (Def_Id, (T));
13621 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13622 Set_Etype (Def_Id, Base_Type (T));
13624 if Constraint_OK then
13625 Set_First_Index (Def_Id, First (Constraints (C)));
13626 else
13627 Set_First_Index (Def_Id, First_Index (T));
13628 end if;
13630 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13631 Set_Is_Fixed_Lower_Bound_Array_Subtype
13632 (Def_Id, Is_FLB_Array_Subtype);
13633 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13634 Set_Is_Independent (Def_Id, Is_Independent (T));
13635 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13637 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13638 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13640 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13641 -- We need to initialize the attribute because if Def_Id is previously
13642 -- analyzed through a limited_with clause, it will have the attributes
13643 -- of an incomplete type, one of which is an Elist that overlaps the
13644 -- Packed_Array_Impl_Type field.
13646 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13648 -- Build a freeze node if parent still needs one. Also make sure that
13649 -- the Depends_On_Private status is set because the subtype will need
13650 -- reprocessing at the time the base type does, and also we must set a
13651 -- conditional delay.
13653 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13654 Conditional_Delay (Def_Id, T);
13655 end Constrain_Array;
13657 ------------------------------
13658 -- Constrain_Component_Type --
13659 ------------------------------
13661 function Constrain_Component_Type
13662 (Comp : Entity_Id;
13663 Constrained_Typ : Entity_Id;
13664 Related_Node : Node_Id;
13665 Typ : Entity_Id;
13666 Constraints : Elist_Id) return Entity_Id
13668 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13669 Compon_Type : constant Entity_Id := Etype (Comp);
13671 function Build_Constrained_Array_Type
13672 (Old_Type : Entity_Id) return Entity_Id;
13673 -- If Old_Type is an array type, one of whose indexes is constrained
13674 -- by a discriminant, build an Itype whose constraint replaces the
13675 -- discriminant with its value in the constraint.
13677 function Build_Constrained_Discriminated_Type
13678 (Old_Type : Entity_Id) return Entity_Id;
13679 -- Ditto for record components. Handle the case where the constraint
13680 -- is a conversion of the discriminant value, introduced during
13681 -- expansion.
13683 function Build_Constrained_Access_Type
13684 (Old_Type : Entity_Id) return Entity_Id;
13685 -- Ditto for access types. Makes use of previous two functions, to
13686 -- constrain designated type.
13688 function Is_Discriminant (Expr : Node_Id) return Boolean;
13689 -- Returns True if Expr is a discriminant
13691 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13692 -- Find the value of a discriminant named by Discr_Expr in Constraints
13694 -----------------------------------
13695 -- Build_Constrained_Access_Type --
13696 -----------------------------------
13698 function Build_Constrained_Access_Type
13699 (Old_Type : Entity_Id) return Entity_Id
13701 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13702 Itype : Entity_Id;
13703 Desig_Subtype : Entity_Id;
13704 Scop : Entity_Id;
13706 begin
13707 -- If the original access type was not embedded in the enclosing
13708 -- type definition, there is no need to produce a new access
13709 -- subtype. In fact every access type with an explicit constraint
13710 -- generates an itype whose scope is the enclosing record.
13712 if not Is_Type (Scope (Old_Type)) then
13713 return Old_Type;
13715 elsif Is_Array_Type (Desig_Type) then
13716 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13718 elsif Has_Discriminants (Desig_Type) then
13720 -- This may be an access type to an enclosing record type for
13721 -- which we are constructing the constrained components. Return
13722 -- the enclosing record subtype. This is not always correct,
13723 -- but avoids infinite recursion. ???
13725 Desig_Subtype := Any_Type;
13727 for J in reverse 0 .. Scope_Stack.Last loop
13728 Scop := Scope_Stack.Table (J).Entity;
13730 if Is_Type (Scop)
13731 and then Base_Type (Scop) = Base_Type (Desig_Type)
13732 then
13733 Desig_Subtype := Scop;
13734 end if;
13736 exit when not Is_Type (Scop);
13737 end loop;
13739 if Desig_Subtype = Any_Type then
13740 Desig_Subtype :=
13741 Build_Constrained_Discriminated_Type (Desig_Type);
13742 end if;
13744 else
13745 return Old_Type;
13746 end if;
13748 if Desig_Subtype /= Desig_Type then
13750 -- The Related_Node better be here or else we won't be able
13751 -- to attach new itypes to a node in the tree.
13753 pragma Assert (Present (Related_Node));
13755 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13757 Set_Etype (Itype, Base_Type (Old_Type));
13758 Set_Size_Info (Itype, (Old_Type));
13759 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13760 Set_Depends_On_Private (Itype, Has_Private_Component
13761 (Old_Type));
13762 Set_Is_Access_Constant (Itype, Is_Access_Constant
13763 (Old_Type));
13765 -- The new itype needs freezing when it depends on a not frozen
13766 -- type and the enclosing subtype needs freezing.
13768 if Has_Delayed_Freeze (Constrained_Typ)
13769 and then not Is_Frozen (Constrained_Typ)
13770 then
13771 Conditional_Delay (Itype, Base_Type (Old_Type));
13772 end if;
13774 return Itype;
13776 else
13777 return Old_Type;
13778 end if;
13779 end Build_Constrained_Access_Type;
13781 ----------------------------------
13782 -- Build_Constrained_Array_Type --
13783 ----------------------------------
13785 function Build_Constrained_Array_Type
13786 (Old_Type : Entity_Id) return Entity_Id
13788 Lo_Expr : Node_Id;
13789 Hi_Expr : Node_Id;
13790 Old_Index : Node_Id;
13791 Range_Node : Node_Id;
13792 Constr_List : List_Id;
13794 Need_To_Create_Itype : Boolean := False;
13796 begin
13797 Old_Index := First_Index (Old_Type);
13798 while Present (Old_Index) loop
13799 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13801 if Is_Discriminant (Lo_Expr)
13802 or else
13803 Is_Discriminant (Hi_Expr)
13804 then
13805 Need_To_Create_Itype := True;
13806 exit;
13807 end if;
13809 Next_Index (Old_Index);
13810 end loop;
13812 if Need_To_Create_Itype then
13813 Constr_List := New_List;
13815 Old_Index := First_Index (Old_Type);
13816 while Present (Old_Index) loop
13817 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13819 if Is_Discriminant (Lo_Expr) then
13820 Lo_Expr := Get_Discr_Value (Lo_Expr);
13821 end if;
13823 if Is_Discriminant (Hi_Expr) then
13824 Hi_Expr := Get_Discr_Value (Hi_Expr);
13825 end if;
13827 Range_Node :=
13828 Make_Range
13829 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13831 Append (Range_Node, To => Constr_List);
13833 Next_Index (Old_Index);
13834 end loop;
13836 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13838 else
13839 return Old_Type;
13840 end if;
13841 end Build_Constrained_Array_Type;
13843 ------------------------------------------
13844 -- Build_Constrained_Discriminated_Type --
13845 ------------------------------------------
13847 function Build_Constrained_Discriminated_Type
13848 (Old_Type : Entity_Id) return Entity_Id
13850 Expr : Node_Id;
13851 Constr_List : List_Id;
13852 Old_Constraint : Elmt_Id;
13854 Need_To_Create_Itype : Boolean := False;
13856 begin
13857 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13858 while Present (Old_Constraint) loop
13859 Expr := Node (Old_Constraint);
13861 if Is_Discriminant (Expr) then
13862 Need_To_Create_Itype := True;
13863 exit;
13865 -- After expansion of discriminated task types, the value
13866 -- of the discriminant may be converted to a run-time type
13867 -- for restricted run-times. Propagate the value of the
13868 -- discriminant as well, so that e.g. the secondary stack
13869 -- component has a static constraint. Necessary for LLVM.
13871 elsif Nkind (Expr) = N_Type_Conversion
13872 and then Is_Discriminant (Expression (Expr))
13873 then
13874 Need_To_Create_Itype := True;
13875 exit;
13876 end if;
13878 Next_Elmt (Old_Constraint);
13879 end loop;
13881 if Need_To_Create_Itype then
13882 Constr_List := New_List;
13884 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13885 while Present (Old_Constraint) loop
13886 Expr := Node (Old_Constraint);
13888 if Is_Discriminant (Expr) then
13889 Expr := Get_Discr_Value (Expr);
13891 elsif Nkind (Expr) = N_Type_Conversion
13892 and then Is_Discriminant (Expression (Expr))
13893 then
13894 Expr := New_Copy_Tree (Expr);
13895 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13896 end if;
13898 Append (New_Copy_Tree (Expr), To => Constr_List);
13900 Next_Elmt (Old_Constraint);
13901 end loop;
13903 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13905 else
13906 return Old_Type;
13907 end if;
13908 end Build_Constrained_Discriminated_Type;
13910 ---------------------
13911 -- Get_Discr_Value --
13912 ---------------------
13914 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
13915 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
13916 -- Entity of a discriminant that appear as a standalone expression in
13917 -- the constraint of a component.
13919 D : Entity_Id;
13920 E : Elmt_Id;
13922 begin
13923 -- The discriminant may be declared for the type, in which case we
13924 -- find it by iterating over the list of discriminants. If the
13925 -- discriminant is inherited from a parent type, it appears as the
13926 -- corresponding discriminant of the current type. This will be the
13927 -- case when constraining an inherited component whose constraint is
13928 -- given by a discriminant of the parent.
13930 D := First_Discriminant (Typ);
13931 E := First_Elmt (Constraints);
13933 while Present (D) loop
13934 if D = Discr_Id
13935 or else D = CR_Discriminant (Discr_Id)
13936 or else Corresponding_Discriminant (D) = Discr_Id
13937 then
13938 return Node (E);
13939 end if;
13941 Next_Discriminant (D);
13942 Next_Elmt (E);
13943 end loop;
13945 -- The Corresponding_Discriminant mechanism is incomplete, because
13946 -- the correspondence between new and old discriminants is not one
13947 -- to one: one new discriminant can constrain several old ones. In
13948 -- that case, scan sequentially the stored_constraint, the list of
13949 -- discriminants of the parents, and the constraints.
13951 -- Previous code checked for the present of the Stored_Constraint
13952 -- list for the derived type, but did not use it at all. Should it
13953 -- be present when the component is a discriminated task type?
13955 if Is_Derived_Type (Typ)
13956 and then Scope (Discr_Id) = Etype (Typ)
13957 then
13958 D := First_Discriminant (Etype (Typ));
13959 E := First_Elmt (Constraints);
13960 while Present (D) loop
13961 if D = Discr_Id then
13962 return Node (E);
13963 end if;
13965 Next_Discriminant (D);
13966 Next_Elmt (E);
13967 end loop;
13968 end if;
13970 -- Something is wrong if we did not find the value
13972 raise Program_Error;
13973 end Get_Discr_Value;
13975 ---------------------
13976 -- Is_Discriminant --
13977 ---------------------
13979 function Is_Discriminant (Expr : Node_Id) return Boolean is
13980 Discrim_Scope : Entity_Id;
13982 begin
13983 if Denotes_Discriminant (Expr) then
13984 Discrim_Scope := Scope (Entity (Expr));
13986 -- Either we have a reference to one of Typ's discriminants,
13988 pragma Assert (Discrim_Scope = Typ
13990 -- or to the discriminants of the parent type, in the case
13991 -- of a derivation of a tagged type with variants.
13993 or else Discrim_Scope = Etype (Typ)
13994 or else Full_View (Discrim_Scope) = Etype (Typ)
13996 -- or same as above for the case where the discriminants
13997 -- were declared in Typ's private view.
13999 or else (Is_Private_Type (Discrim_Scope)
14000 and then Chars (Discrim_Scope) = Chars (Typ))
14002 -- or else we are deriving from the full view and the
14003 -- discriminant is declared in the private entity.
14005 or else (Is_Private_Type (Typ)
14006 and then Chars (Discrim_Scope) = Chars (Typ))
14008 -- Or we are constrained the corresponding record of a
14009 -- synchronized type that completes a private declaration.
14011 or else (Is_Concurrent_Record_Type (Typ)
14012 and then
14013 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14015 -- or we have a class-wide type, in which case make sure the
14016 -- discriminant found belongs to the root type.
14018 or else (Is_Class_Wide_Type (Typ)
14019 and then Etype (Typ) = Discrim_Scope));
14021 return True;
14022 end if;
14024 -- In all other cases we have something wrong
14026 return False;
14027 end Is_Discriminant;
14029 -- Start of processing for Constrain_Component_Type
14031 begin
14032 if Nkind (Parent (Comp)) = N_Component_Declaration
14033 and then Comes_From_Source (Parent (Comp))
14034 and then Comes_From_Source
14035 (Subtype_Indication (Component_Definition (Parent (Comp))))
14036 and then
14037 Is_Entity_Name
14038 (Subtype_Indication (Component_Definition (Parent (Comp))))
14039 then
14040 return Compon_Type;
14042 elsif Is_Array_Type (Compon_Type) then
14043 return Build_Constrained_Array_Type (Compon_Type);
14045 elsif Has_Discriminants (Compon_Type) then
14046 return Build_Constrained_Discriminated_Type (Compon_Type);
14048 elsif Is_Access_Type (Compon_Type) then
14049 return Build_Constrained_Access_Type (Compon_Type);
14051 else
14052 return Compon_Type;
14053 end if;
14054 end Constrain_Component_Type;
14056 --------------------------
14057 -- Constrain_Concurrent --
14058 --------------------------
14060 -- For concurrent types, the associated record value type carries the same
14061 -- discriminants, so when we constrain a concurrent type, we must constrain
14062 -- the corresponding record type as well.
14064 procedure Constrain_Concurrent
14065 (Def_Id : in out Entity_Id;
14066 SI : Node_Id;
14067 Related_Nod : Node_Id;
14068 Related_Id : Entity_Id;
14069 Suffix : Character)
14071 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14072 -- case of a private subtype (needed when only doing semantic analysis).
14074 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14075 T_Val : Entity_Id;
14077 begin
14078 if Is_Access_Type (T_Ent) then
14079 T_Ent := Designated_Type (T_Ent);
14080 end if;
14082 T_Val := Corresponding_Record_Type (T_Ent);
14084 if Present (T_Val) then
14086 if No (Def_Id) then
14087 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14089 -- Elaborate itype now, as it may be used in a subsequent
14090 -- synchronized operation in another scope.
14092 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14093 Build_Itype_Reference (Def_Id, Related_Nod);
14094 end if;
14095 end if;
14097 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14098 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14100 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14101 Set_Corresponding_Record_Type (Def_Id,
14102 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14104 else
14105 -- If there is no associated record, expansion is disabled and this
14106 -- is a generic context. Create a subtype in any case, so that
14107 -- semantic analysis can proceed.
14109 if No (Def_Id) then
14110 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14111 end if;
14113 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14114 end if;
14115 end Constrain_Concurrent;
14117 ------------------------------------
14118 -- Constrain_Corresponding_Record --
14119 ------------------------------------
14121 function Constrain_Corresponding_Record
14122 (Prot_Subt : Entity_Id;
14123 Corr_Rec : Entity_Id;
14124 Related_Nod : Node_Id) return Entity_Id
14126 T_Sub : constant Entity_Id :=
14127 Create_Itype
14128 (Ekind => E_Record_Subtype,
14129 Related_Nod => Related_Nod,
14130 Related_Id => Corr_Rec,
14131 Suffix => 'C',
14132 Suffix_Index => -1);
14134 begin
14135 Set_Etype (T_Sub, Corr_Rec);
14136 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14137 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14138 Set_Is_Constrained (T_Sub, True);
14139 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14140 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14142 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14143 Set_Discriminant_Constraint
14144 (T_Sub, Discriminant_Constraint (Prot_Subt));
14145 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14146 Create_Constrained_Components
14147 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14148 end if;
14150 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14152 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14153 Conditional_Delay (T_Sub, Corr_Rec);
14155 else
14156 -- This is a component subtype: it will be frozen in the context of
14157 -- the enclosing record's init_proc, so that discriminant references
14158 -- are resolved to discriminals. (Note: we used to skip freezing
14159 -- altogether in that case, which caused errors downstream for
14160 -- components of a bit packed array type).
14162 Set_Has_Delayed_Freeze (T_Sub);
14163 end if;
14165 return T_Sub;
14166 end Constrain_Corresponding_Record;
14168 -----------------------
14169 -- Constrain_Decimal --
14170 -----------------------
14172 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14173 T : constant Entity_Id := Entity (Subtype_Mark (S));
14174 C : constant Node_Id := Constraint (S);
14175 Loc : constant Source_Ptr := Sloc (C);
14176 Range_Expr : Node_Id;
14177 Digits_Expr : Node_Id;
14178 Digits_Val : Uint;
14179 Bound_Val : Ureal;
14181 begin
14182 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14184 if Nkind (C) = N_Range_Constraint then
14185 Range_Expr := Range_Expression (C);
14186 Digits_Val := Digits_Value (T);
14188 else
14189 pragma Assert (Nkind (C) = N_Digits_Constraint);
14191 Digits_Expr := Digits_Expression (C);
14192 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14194 Check_Digits_Expression (Digits_Expr);
14195 Digits_Val := Expr_Value (Digits_Expr);
14197 if Digits_Val > Digits_Value (T) then
14198 Error_Msg_N
14199 ("digits expression is incompatible with subtype", C);
14200 Digits_Val := Digits_Value (T);
14201 end if;
14203 if Present (Range_Constraint (C)) then
14204 Range_Expr := Range_Expression (Range_Constraint (C));
14205 else
14206 Range_Expr := Empty;
14207 end if;
14208 end if;
14210 Set_Etype (Def_Id, Base_Type (T));
14211 Set_Size_Info (Def_Id, (T));
14212 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14213 Set_Delta_Value (Def_Id, Delta_Value (T));
14214 Set_Scale_Value (Def_Id, Scale_Value (T));
14215 Set_Small_Value (Def_Id, Small_Value (T));
14216 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14217 Set_Digits_Value (Def_Id, Digits_Val);
14219 -- Manufacture range from given digits value if no range present
14221 if No (Range_Expr) then
14222 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14223 Range_Expr :=
14224 Make_Range (Loc,
14225 Low_Bound =>
14226 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14227 High_Bound =>
14228 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14229 end if;
14231 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14232 Set_Discrete_RM_Size (Def_Id);
14234 -- Unconditionally delay the freeze, since we cannot set size
14235 -- information in all cases correctly until the freeze point.
14237 Set_Has_Delayed_Freeze (Def_Id);
14238 end Constrain_Decimal;
14240 ----------------------------------
14241 -- Constrain_Discriminated_Type --
14242 ----------------------------------
14244 procedure Constrain_Discriminated_Type
14245 (Def_Id : Entity_Id;
14246 S : Node_Id;
14247 Related_Nod : Node_Id;
14248 For_Access : Boolean := False)
14250 E : Entity_Id := Entity (Subtype_Mark (S));
14251 T : Entity_Id;
14253 procedure Fixup_Bad_Constraint;
14254 -- Called after finding a bad constraint, and after having posted an
14255 -- appropriate error message. The goal is to leave type Def_Id in as
14256 -- reasonable state as possible.
14258 --------------------------
14259 -- Fixup_Bad_Constraint --
14260 --------------------------
14262 procedure Fixup_Bad_Constraint is
14263 begin
14264 -- Set a reasonable Ekind for the entity, including incomplete types.
14266 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14268 -- Set Etype to the known type, to reduce chances of cascaded errors
14270 Set_Etype (Def_Id, E);
14271 Set_Error_Posted (Def_Id);
14272 end Fixup_Bad_Constraint;
14274 -- Local variables
14276 C : Node_Id;
14277 Constr : Elist_Id := New_Elmt_List;
14279 -- Start of processing for Constrain_Discriminated_Type
14281 begin
14282 C := Constraint (S);
14284 -- A discriminant constraint is only allowed in a subtype indication,
14285 -- after a subtype mark. This subtype mark must denote either a type
14286 -- with discriminants, or an access type whose designated type is a
14287 -- type with discriminants. A discriminant constraint specifies the
14288 -- values of these discriminants (RM 3.7.2(5)).
14290 T := Base_Type (Entity (Subtype_Mark (S)));
14292 if Is_Access_Type (T) then
14293 T := Designated_Type (T);
14294 end if;
14296 -- In an instance it may be necessary to retrieve the full view of a
14297 -- type with unknown discriminants, or a full view with defaulted
14298 -- discriminants. In other contexts the constraint is illegal.
14300 if In_Instance
14301 and then Is_Private_Type (T)
14302 and then Present (Full_View (T))
14303 and then
14304 (Has_Unknown_Discriminants (T)
14305 or else
14306 (not Has_Discriminants (T)
14307 and then Has_Defaulted_Discriminants (Full_View (T))))
14308 then
14309 T := Full_View (T);
14310 E := Full_View (E);
14311 end if;
14313 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14314 -- generating an error for access-to-incomplete subtypes.
14316 if Ada_Version >= Ada_2005
14317 and then Ekind (T) = E_Incomplete_Type
14318 and then Nkind (Parent (S)) = N_Subtype_Declaration
14319 and then not Is_Itype (Def_Id)
14320 then
14321 -- A little sanity check: emit an error message if the type has
14322 -- discriminants to begin with. Type T may be a regular incomplete
14323 -- type or imported via a limited with clause.
14325 if Has_Discriminants (T)
14326 or else (From_Limited_With (T)
14327 and then Present (Non_Limited_View (T))
14328 and then Nkind (Parent (Non_Limited_View (T))) =
14329 N_Full_Type_Declaration
14330 and then Present (Discriminant_Specifications
14331 (Parent (Non_Limited_View (T)))))
14332 then
14333 Error_Msg_N
14334 ("(Ada 2005) incomplete subtype may not be constrained", C);
14335 else
14336 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14337 end if;
14339 Fixup_Bad_Constraint;
14340 return;
14342 -- Check that the type has visible discriminants. The type may be
14343 -- a private type with unknown discriminants whose full view has
14344 -- discriminants which are invisible.
14346 elsif not Has_Discriminants (T)
14347 or else
14348 (Has_Unknown_Discriminants (T)
14349 and then Is_Private_Type (T))
14350 then
14351 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14352 Fixup_Bad_Constraint;
14353 return;
14355 elsif Is_Constrained (E)
14356 or else (Ekind (E) = E_Class_Wide_Subtype
14357 and then Present (Discriminant_Constraint (E)))
14358 then
14359 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14360 Fixup_Bad_Constraint;
14361 return;
14362 end if;
14364 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14365 -- applies to the base type.
14367 T := Base_Type (T);
14369 Constr := Build_Discriminant_Constraints (T, S);
14371 -- If the list returned was empty we had an error in building the
14372 -- discriminant constraint. We have also already signalled an error
14373 -- in the incomplete type case
14375 if Is_Empty_Elmt_List (Constr) then
14376 Fixup_Bad_Constraint;
14377 return;
14378 end if;
14380 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14381 end Constrain_Discriminated_Type;
14383 ---------------------------
14384 -- Constrain_Enumeration --
14385 ---------------------------
14387 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14388 T : constant Entity_Id := Entity (Subtype_Mark (S));
14389 C : constant Node_Id := Constraint (S);
14391 begin
14392 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14394 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14396 Set_Etype (Def_Id, Base_Type (T));
14397 Set_Size_Info (Def_Id, (T));
14398 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14399 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14401 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14403 Set_Discrete_RM_Size (Def_Id);
14404 end Constrain_Enumeration;
14406 ----------------------
14407 -- Constrain_Float --
14408 ----------------------
14410 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14411 T : constant Entity_Id := Entity (Subtype_Mark (S));
14412 C : Node_Id;
14413 D : Node_Id;
14414 Rais : Node_Id;
14416 begin
14417 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14419 Set_Etype (Def_Id, Base_Type (T));
14420 Set_Size_Info (Def_Id, (T));
14421 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14423 -- Process the constraint
14425 C := Constraint (S);
14427 -- Digits constraint present
14429 if Nkind (C) = N_Digits_Constraint then
14430 Check_Restriction (No_Obsolescent_Features, C);
14432 if Warn_On_Obsolescent_Feature then
14433 Error_Msg_N
14434 ("subtype digits constraint is an " &
14435 "obsolescent feature (RM J.3(8))?j?", C);
14436 end if;
14438 D := Digits_Expression (C);
14439 Analyze_And_Resolve (D, Any_Integer);
14440 Check_Digits_Expression (D);
14441 Set_Digits_Value (Def_Id, Expr_Value (D));
14443 -- Check that digits value is in range. Obviously we can do this
14444 -- at compile time, but it is strictly a runtime check, and of
14445 -- course there is an ACVC test that checks this.
14447 if Digits_Value (Def_Id) > Digits_Value (T) then
14448 Error_Msg_Uint_1 := Digits_Value (T);
14449 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14450 Rais :=
14451 Make_Raise_Constraint_Error (Sloc (D),
14452 Reason => CE_Range_Check_Failed);
14453 Insert_Action (Declaration_Node (Def_Id), Rais);
14454 end if;
14456 C := Range_Constraint (C);
14458 -- No digits constraint present
14460 else
14461 Set_Digits_Value (Def_Id, Digits_Value (T));
14462 end if;
14464 -- Range constraint present
14466 if Nkind (C) = N_Range_Constraint then
14467 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14469 -- No range constraint present
14471 else
14472 pragma Assert (No (C));
14473 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14474 end if;
14476 Set_Is_Constrained (Def_Id);
14477 end Constrain_Float;
14479 ---------------------
14480 -- Constrain_Index --
14481 ---------------------
14483 procedure Constrain_Index
14484 (Index : Node_Id;
14485 S : Node_Id;
14486 Related_Nod : Node_Id;
14487 Related_Id : Entity_Id;
14488 Suffix : Character;
14489 Suffix_Index : Pos)
14491 Def_Id : Entity_Id;
14492 R : Node_Id := Empty;
14493 T : constant Entity_Id := Etype (Index);
14494 Is_FLB_Index : Boolean := False;
14496 begin
14497 Def_Id :=
14498 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14499 Set_Etype (Def_Id, Base_Type (T));
14501 if Nkind (S) = N_Range
14502 or else
14503 (Nkind (S) = N_Attribute_Reference
14504 and then Attribute_Name (S) = Name_Range)
14505 then
14506 -- A Range attribute will be transformed into N_Range by Resolve
14508 -- If a range has an Empty upper bound, then remember that for later
14509 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14510 -- flag, and also set the upper bound of the range to the index
14511 -- subtype's upper bound rather than leaving it Empty. In truth,
14512 -- that upper bound corresponds to a box ("<>"), but it's convenient
14513 -- to set it to the upper bound to avoid needing to add special tests
14514 -- in various places for an Empty upper bound, and in any case it
14515 -- accurately characterizes the index's range of values.
14517 if Nkind (S) = N_Range and then not Present (High_Bound (S)) then
14518 Is_FLB_Index := True;
14519 Set_High_Bound (S, Type_High_Bound (T));
14520 end if;
14522 R := S;
14524 Process_Range_Expr_In_Decl (R, T);
14526 if not Error_Posted (S)
14527 and then
14528 (Nkind (S) /= N_Range
14529 or else not Covers (T, (Etype (Low_Bound (S))))
14530 or else not Covers (T, (Etype (High_Bound (S)))))
14531 then
14532 if Base_Type (T) /= Any_Type
14533 and then Etype (Low_Bound (S)) /= Any_Type
14534 and then Etype (High_Bound (S)) /= Any_Type
14535 then
14536 Error_Msg_N ("range expected", S);
14537 end if;
14538 end if;
14540 elsif Nkind (S) = N_Subtype_Indication then
14542 -- The parser has verified that this is a discrete indication
14544 Resolve_Discrete_Subtype_Indication (S, T);
14545 Bad_Predicated_Subtype_Use
14546 ("subtype& has predicate, not allowed in index constraint",
14547 S, Entity (Subtype_Mark (S)));
14549 R := Range_Expression (Constraint (S));
14551 -- Capture values of bounds and generate temporaries for them if
14552 -- needed, since checks may cause duplication of the expressions
14553 -- which must not be reevaluated.
14555 -- The forced evaluation removes side effects from expressions, which
14556 -- should occur also in GNATprove mode. Otherwise, we end up with
14557 -- unexpected insertions of actions at places where this is not
14558 -- supposed to occur, e.g. on default parameters of a call.
14560 if Expander_Active or GNATprove_Mode then
14561 Force_Evaluation
14562 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14563 Force_Evaluation
14564 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14565 end if;
14567 elsif Nkind (S) = N_Discriminant_Association then
14569 -- Syntactically valid in subtype indication
14571 Error_Msg_N ("invalid index constraint", S);
14572 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14573 return;
14575 -- Subtype_Mark case, no anonymous subtypes to construct
14577 else
14578 Analyze (S);
14580 if Is_Entity_Name (S) then
14581 if not Is_Type (Entity (S)) then
14582 Error_Msg_N ("expect subtype mark for index constraint", S);
14584 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14585 Wrong_Type (S, Base_Type (T));
14587 -- Check error of subtype with predicate in index constraint
14589 else
14590 Bad_Predicated_Subtype_Use
14591 ("subtype& has predicate, not allowed in index constraint",
14592 S, Entity (S));
14593 end if;
14595 return;
14597 else
14598 Error_Msg_N ("invalid index constraint", S);
14599 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14600 return;
14601 end if;
14602 end if;
14604 -- Complete construction of the Itype
14606 if Is_Modular_Integer_Type (T) then
14607 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14609 elsif Is_Integer_Type (T) then
14610 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14612 else
14613 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14614 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14615 Set_First_Literal (Def_Id, First_Literal (T));
14616 end if;
14618 Set_Size_Info (Def_Id, (T));
14619 Copy_RM_Size (To => Def_Id, From => T);
14620 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14622 -- If this is a range for a fixed-lower-bound subtype, then set the
14623 -- index itype's low bound to the FLB and the index itype's upper bound
14624 -- to the high bound of the parent array type's index subtype. Also,
14625 -- mark the itype as an FLB index subtype.
14627 if Nkind (S) = N_Range and then Is_FLB_Index then
14628 Set_Scalar_Range
14629 (Def_Id,
14630 Make_Range (Sloc (S),
14631 Low_Bound => Low_Bound (S),
14632 High_Bound => Type_High_Bound (T)));
14633 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14635 else
14636 Set_Scalar_Range (Def_Id, R);
14637 end if;
14639 Set_Etype (S, Def_Id);
14640 Set_Discrete_RM_Size (Def_Id);
14641 end Constrain_Index;
14643 -----------------------
14644 -- Constrain_Integer --
14645 -----------------------
14647 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14648 T : constant Entity_Id := Entity (Subtype_Mark (S));
14649 C : constant Node_Id := Constraint (S);
14651 begin
14652 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14654 if Is_Modular_Integer_Type (T) then
14655 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14656 else
14657 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14658 end if;
14660 Set_Etype (Def_Id, Base_Type (T));
14661 Set_Size_Info (Def_Id, (T));
14662 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14663 Set_Discrete_RM_Size (Def_Id);
14664 end Constrain_Integer;
14666 ------------------------------
14667 -- Constrain_Ordinary_Fixed --
14668 ------------------------------
14670 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14671 T : constant Entity_Id := Entity (Subtype_Mark (S));
14672 C : Node_Id;
14673 D : Node_Id;
14674 Rais : Node_Id;
14676 begin
14677 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14678 Set_Etype (Def_Id, Base_Type (T));
14679 Set_Size_Info (Def_Id, (T));
14680 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14681 Set_Small_Value (Def_Id, Small_Value (T));
14683 -- Process the constraint
14685 C := Constraint (S);
14687 -- Delta constraint present
14689 if Nkind (C) = N_Delta_Constraint then
14690 Check_Restriction (No_Obsolescent_Features, C);
14692 if Warn_On_Obsolescent_Feature then
14693 Error_Msg_S
14694 ("subtype delta constraint is an " &
14695 "obsolescent feature (RM J.3(7))?j?");
14696 end if;
14698 D := Delta_Expression (C);
14699 Analyze_And_Resolve (D, Any_Real);
14700 Check_Delta_Expression (D);
14701 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14703 -- Check that delta value is in range. Obviously we can do this
14704 -- at compile time, but it is strictly a runtime check, and of
14705 -- course there is an ACVC test that checks this.
14707 if Delta_Value (Def_Id) < Delta_Value (T) then
14708 Error_Msg_N ("??delta value is too small", D);
14709 Rais :=
14710 Make_Raise_Constraint_Error (Sloc (D),
14711 Reason => CE_Range_Check_Failed);
14712 Insert_Action (Declaration_Node (Def_Id), Rais);
14713 end if;
14715 C := Range_Constraint (C);
14717 -- No delta constraint present
14719 else
14720 Set_Delta_Value (Def_Id, Delta_Value (T));
14721 end if;
14723 -- Range constraint present
14725 if Nkind (C) = N_Range_Constraint then
14726 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14728 -- No range constraint present
14730 else
14731 pragma Assert (No (C));
14732 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14733 end if;
14735 Set_Discrete_RM_Size (Def_Id);
14737 -- Unconditionally delay the freeze, since we cannot set size
14738 -- information in all cases correctly until the freeze point.
14740 Set_Has_Delayed_Freeze (Def_Id);
14741 end Constrain_Ordinary_Fixed;
14743 -----------------------
14744 -- Contain_Interface --
14745 -----------------------
14747 function Contain_Interface
14748 (Iface : Entity_Id;
14749 Ifaces : Elist_Id) return Boolean
14751 Iface_Elmt : Elmt_Id;
14753 begin
14754 if Present (Ifaces) then
14755 Iface_Elmt := First_Elmt (Ifaces);
14756 while Present (Iface_Elmt) loop
14757 if Node (Iface_Elmt) = Iface then
14758 return True;
14759 end if;
14761 Next_Elmt (Iface_Elmt);
14762 end loop;
14763 end if;
14765 return False;
14766 end Contain_Interface;
14768 ---------------------------
14769 -- Convert_Scalar_Bounds --
14770 ---------------------------
14772 procedure Convert_Scalar_Bounds
14773 (N : Node_Id;
14774 Parent_Type : Entity_Id;
14775 Derived_Type : Entity_Id;
14776 Loc : Source_Ptr)
14778 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14780 Lo : Node_Id;
14781 Hi : Node_Id;
14782 Rng : Node_Id;
14784 begin
14785 -- Defend against previous errors
14787 if No (Scalar_Range (Derived_Type)) then
14788 Check_Error_Detected;
14789 return;
14790 end if;
14792 Lo := Build_Scalar_Bound
14793 (Type_Low_Bound (Derived_Type),
14794 Parent_Type, Implicit_Base);
14796 Hi := Build_Scalar_Bound
14797 (Type_High_Bound (Derived_Type),
14798 Parent_Type, Implicit_Base);
14800 Rng :=
14801 Make_Range (Loc,
14802 Low_Bound => Lo,
14803 High_Bound => Hi);
14805 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14807 Set_Parent (Rng, N);
14808 Set_Scalar_Range (Derived_Type, Rng);
14810 -- Analyze the bounds
14812 Analyze_And_Resolve (Lo, Implicit_Base);
14813 Analyze_And_Resolve (Hi, Implicit_Base);
14815 -- Analyze the range itself, except that we do not analyze it if
14816 -- the bounds are real literals, and we have a fixed-point type.
14817 -- The reason for this is that we delay setting the bounds in this
14818 -- case till we know the final Small and Size values (see circuit
14819 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14821 if Is_Fixed_Point_Type (Parent_Type)
14822 and then Nkind (Lo) = N_Real_Literal
14823 and then Nkind (Hi) = N_Real_Literal
14824 then
14825 return;
14827 -- Here we do the analysis of the range
14829 -- Note: we do this manually, since if we do a normal Analyze and
14830 -- Resolve call, there are problems with the conversions used for
14831 -- the derived type range.
14833 else
14834 Set_Etype (Rng, Implicit_Base);
14835 Set_Analyzed (Rng, True);
14836 end if;
14837 end Convert_Scalar_Bounds;
14839 -------------------
14840 -- Copy_And_Swap --
14841 -------------------
14843 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14844 begin
14845 -- Initialize new full declaration entity by copying the pertinent
14846 -- fields of the corresponding private declaration entity.
14848 -- We temporarily set Ekind to a value appropriate for a type to
14849 -- avoid assert failures in Einfo from checking for setting type
14850 -- attributes on something that is not a type. Ekind (Priv) is an
14851 -- appropriate choice, since it allowed the attributes to be set
14852 -- in the first place. This Ekind value will be modified later.
14854 Mutate_Ekind (Full, Ekind (Priv));
14856 -- Also set Etype temporarily to Any_Type, again, in the absence
14857 -- of errors, it will be properly reset, and if there are errors,
14858 -- then we want a value of Any_Type to remain.
14860 Set_Etype (Full, Any_Type);
14862 -- Now start copying attributes
14864 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14866 if Has_Discriminants (Full) then
14867 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14868 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14869 end if;
14871 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14872 Set_Homonym (Full, Homonym (Priv));
14873 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14874 Set_Is_Public (Full, Is_Public (Priv));
14875 Set_Is_Pure (Full, Is_Pure (Priv));
14876 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14877 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14878 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14879 Set_Has_Pragma_Unreferenced_Objects
14880 (Full, Has_Pragma_Unreferenced_Objects
14881 (Priv));
14883 Conditional_Delay (Full, Priv);
14885 if Is_Tagged_Type (Full) then
14886 Set_Direct_Primitive_Operations
14887 (Full, Direct_Primitive_Operations (Priv));
14888 Set_No_Tagged_Streams_Pragma
14889 (Full, No_Tagged_Streams_Pragma (Priv));
14891 if Is_Base_Type (Priv) then
14892 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14893 end if;
14894 end if;
14896 Set_Is_Volatile (Full, Is_Volatile (Priv));
14897 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14898 Set_Scope (Full, Scope (Priv));
14899 Set_Prev_Entity (Full, Prev_Entity (Priv));
14900 Set_Next_Entity (Full, Next_Entity (Priv));
14901 Set_First_Entity (Full, First_Entity (Priv));
14902 Set_Last_Entity (Full, Last_Entity (Priv));
14904 -- If access types have been recorded for later handling, keep them in
14905 -- the full view so that they get handled when the full view freeze
14906 -- node is expanded.
14908 if Present (Freeze_Node (Priv))
14909 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14910 then
14911 Ensure_Freeze_Node (Full);
14912 Set_Access_Types_To_Process
14913 (Freeze_Node (Full),
14914 Access_Types_To_Process (Freeze_Node (Priv)));
14915 end if;
14917 -- Swap the two entities. Now Private is the full type entity and Full
14918 -- is the private one. They will be swapped back at the end of the
14919 -- private part. This swapping ensures that the entity that is visible
14920 -- in the private part is the full declaration.
14922 Exchange_Entities (Priv, Full);
14923 Append_Entity (Full, Scope (Full));
14924 end Copy_And_Swap;
14926 -------------------------------------
14927 -- Copy_Array_Base_Type_Attributes --
14928 -------------------------------------
14930 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14931 begin
14932 Set_Component_Alignment (T1, Component_Alignment (T2));
14933 Set_Component_Type (T1, Component_Type (T2));
14934 Set_Component_Size (T1, Component_Size (T2));
14935 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14936 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14937 Propagate_Concurrent_Flags (T1, T2);
14938 Set_Is_Packed (T1, Is_Packed (T2));
14939 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14940 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14941 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14942 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14943 end Copy_Array_Base_Type_Attributes;
14945 -----------------------------------
14946 -- Copy_Array_Subtype_Attributes --
14947 -----------------------------------
14949 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14950 begin
14951 Set_Size_Info (T1, T2);
14953 Set_First_Index (T1, First_Index (T2));
14954 Set_Is_Aliased (T1, Is_Aliased (T2));
14955 Set_Is_Atomic (T1, Is_Atomic (T2));
14956 Set_Is_Independent (T1, Is_Independent (T2));
14957 Set_Is_Volatile (T1, Is_Volatile (T2));
14958 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14959 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14960 Set_Is_Constrained (T1, Is_Constrained (T2));
14961 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14962 Inherit_Rep_Item_Chain (T1, T2);
14963 Set_Convention (T1, Convention (T2));
14964 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14965 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14966 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14967 end Copy_Array_Subtype_Attributes;
14969 -----------------------------------
14970 -- Create_Constrained_Components --
14971 -----------------------------------
14973 procedure Create_Constrained_Components
14974 (Subt : Entity_Id;
14975 Decl_Node : Node_Id;
14976 Typ : Entity_Id;
14977 Constraints : Elist_Id)
14979 Loc : constant Source_Ptr := Sloc (Subt);
14980 Comp_List : constant Elist_Id := New_Elmt_List;
14981 Parent_Type : constant Entity_Id := Etype (Typ);
14982 Assoc_List : constant List_Id := New_List;
14984 Discr_Val : Elmt_Id;
14985 Errors : Boolean;
14986 New_C : Entity_Id;
14987 Old_C : Entity_Id;
14988 Is_Static : Boolean := True;
14989 Is_Compile_Time_Known : Boolean := True;
14991 procedure Collect_Fixed_Components (Typ : Entity_Id);
14992 -- Collect parent type components that do not appear in a variant part
14994 procedure Create_All_Components;
14995 -- Iterate over Comp_List to create the components of the subtype
14997 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14998 -- Creates a new component from Old_Compon, copying all the fields from
14999 -- it, including its Etype, inserts the new component in the Subt entity
15000 -- chain and returns the new component.
15002 function Is_Variant_Record (T : Entity_Id) return Boolean;
15003 -- If true, and discriminants are static, collect only components from
15004 -- variants selected by discriminant values.
15006 ------------------------------
15007 -- Collect_Fixed_Components --
15008 ------------------------------
15010 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15011 begin
15012 -- Build association list for discriminants, and find components of the
15013 -- variant part selected by the values of the discriminants.
15015 Old_C := First_Discriminant (Typ);
15016 Discr_Val := First_Elmt (Constraints);
15017 while Present (Old_C) loop
15018 Append_To (Assoc_List,
15019 Make_Component_Association (Loc,
15020 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15021 Expression => New_Copy (Node (Discr_Val))));
15023 Next_Elmt (Discr_Val);
15024 Next_Discriminant (Old_C);
15025 end loop;
15027 -- The tag and the possible parent component are unconditionally in
15028 -- the subtype.
15030 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15031 Old_C := First_Component (Typ);
15032 while Present (Old_C) loop
15033 if Chars (Old_C) in Name_uTag | Name_uParent then
15034 Append_Elmt (Old_C, Comp_List);
15035 end if;
15037 Next_Component (Old_C);
15038 end loop;
15039 end if;
15040 end Collect_Fixed_Components;
15042 ---------------------------
15043 -- Create_All_Components --
15044 ---------------------------
15046 procedure Create_All_Components is
15047 Comp : Elmt_Id;
15049 begin
15050 Comp := First_Elmt (Comp_List);
15051 while Present (Comp) loop
15052 Old_C := Node (Comp);
15053 New_C := Create_Component (Old_C);
15055 Set_Etype
15056 (New_C,
15057 Constrain_Component_Type
15058 (Old_C, Subt, Decl_Node, Typ, Constraints));
15059 Set_Is_Public (New_C, Is_Public (Subt));
15061 Next_Elmt (Comp);
15062 end loop;
15063 end Create_All_Components;
15065 ----------------------
15066 -- Create_Component --
15067 ----------------------
15069 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15070 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15072 begin
15073 if Ekind (Old_Compon) = E_Discriminant
15074 and then Is_Completely_Hidden (Old_Compon)
15075 then
15076 -- This is a shadow discriminant created for a discriminant of
15077 -- the parent type, which needs to be present in the subtype.
15078 -- Give the shadow discriminant an internal name that cannot
15079 -- conflict with that of visible components.
15081 Set_Chars (New_Compon, New_Internal_Name ('C'));
15082 end if;
15084 -- Set the parent so we have a proper link for freezing etc. This is
15085 -- not a real parent pointer, since of course our parent does not own
15086 -- up to us and reference us, we are an illegitimate child of the
15087 -- original parent.
15089 Set_Parent (New_Compon, Parent (Old_Compon));
15091 -- We do not want this node marked as Comes_From_Source, since
15092 -- otherwise it would get first class status and a separate cross-
15093 -- reference line would be generated. Illegitimate children do not
15094 -- rate such recognition.
15096 Set_Comes_From_Source (New_Compon, False);
15098 -- But it is a real entity, and a birth certificate must be properly
15099 -- registered by entering it into the entity list, and setting its
15100 -- scope to the given subtype. This turns out to be useful for the
15101 -- LLVM code generator, but that scope is not used otherwise.
15103 Enter_Name (New_Compon);
15104 Set_Scope (New_Compon, Subt);
15106 return New_Compon;
15107 end Create_Component;
15109 -----------------------
15110 -- Is_Variant_Record --
15111 -----------------------
15113 function Is_Variant_Record (T : Entity_Id) return Boolean is
15114 begin
15115 return Nkind (Parent (T)) = N_Full_Type_Declaration
15116 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
15117 and then Present (Component_List (Type_Definition (Parent (T))))
15118 and then
15119 Present
15120 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
15121 end Is_Variant_Record;
15123 -- Start of processing for Create_Constrained_Components
15125 begin
15126 pragma Assert (Subt /= Base_Type (Subt));
15127 pragma Assert (Typ = Base_Type (Typ));
15129 Set_First_Entity (Subt, Empty);
15130 Set_Last_Entity (Subt, Empty);
15132 -- Check whether constraint is fully static, in which case we can
15133 -- optimize the list of components.
15135 Discr_Val := First_Elmt (Constraints);
15136 while Present (Discr_Val) loop
15137 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15138 Is_Static := False;
15140 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15141 Is_Compile_Time_Known := False;
15142 exit;
15143 end if;
15144 end if;
15146 Next_Elmt (Discr_Val);
15147 end loop;
15149 Set_Has_Static_Discriminants (Subt, Is_Static);
15151 Push_Scope (Subt);
15153 -- Inherit the discriminants of the parent type
15155 Add_Discriminants : declare
15156 Num_Disc : Nat;
15157 Num_Stor : Nat;
15159 begin
15160 Num_Disc := 0;
15161 Old_C := First_Discriminant (Typ);
15163 while Present (Old_C) loop
15164 Num_Disc := Num_Disc + 1;
15165 New_C := Create_Component (Old_C);
15166 Set_Is_Public (New_C, Is_Public (Subt));
15167 Next_Discriminant (Old_C);
15168 end loop;
15170 -- For an untagged derived subtype, the number of discriminants may
15171 -- be smaller than the number of inherited discriminants, because
15172 -- several of them may be renamed by a single new discriminant or
15173 -- constrained. In this case, add the hidden discriminants back into
15174 -- the subtype, because they need to be present if the optimizer of
15175 -- the GCC 4.x back-end decides to break apart assignments between
15176 -- objects using the parent view into member-wise assignments.
15178 Num_Stor := 0;
15180 if Is_Derived_Type (Typ)
15181 and then not Is_Tagged_Type (Typ)
15182 then
15183 Old_C := First_Stored_Discriminant (Typ);
15185 while Present (Old_C) loop
15186 Num_Stor := Num_Stor + 1;
15187 Next_Stored_Discriminant (Old_C);
15188 end loop;
15189 end if;
15191 if Num_Stor > Num_Disc then
15193 -- Find out multiple uses of new discriminants, and add hidden
15194 -- components for the extra renamed discriminants. We recognize
15195 -- multiple uses through the Corresponding_Discriminant of a
15196 -- new discriminant: if it constrains several old discriminants,
15197 -- this field points to the last one in the parent type. The
15198 -- stored discriminants of the derived type have the same name
15199 -- as those of the parent.
15201 declare
15202 Constr : Elmt_Id;
15203 New_Discr : Entity_Id;
15204 Old_Discr : Entity_Id;
15206 begin
15207 Constr := First_Elmt (Stored_Constraint (Typ));
15208 Old_Discr := First_Stored_Discriminant (Typ);
15209 while Present (Constr) loop
15210 if Is_Entity_Name (Node (Constr))
15211 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15212 then
15213 New_Discr := Entity (Node (Constr));
15215 if Chars (Corresponding_Discriminant (New_Discr)) /=
15216 Chars (Old_Discr)
15217 then
15218 -- The new discriminant has been used to rename a
15219 -- subsequent old discriminant. Introduce a shadow
15220 -- component for the current old discriminant.
15222 New_C := Create_Component (Old_Discr);
15223 Set_Original_Record_Component (New_C, Old_Discr);
15224 end if;
15226 else
15227 -- The constraint has eliminated the old discriminant.
15228 -- Introduce a shadow component.
15230 New_C := Create_Component (Old_Discr);
15231 Set_Original_Record_Component (New_C, Old_Discr);
15232 end if;
15234 Next_Elmt (Constr);
15235 Next_Stored_Discriminant (Old_Discr);
15236 end loop;
15237 end;
15238 end if;
15239 end Add_Discriminants;
15241 if Is_Compile_Time_Known
15242 and then Is_Variant_Record (Typ)
15243 then
15244 Collect_Fixed_Components (Typ);
15245 Gather_Components
15246 (Typ,
15247 Component_List (Type_Definition (Parent (Typ))),
15248 Governed_By => Assoc_List,
15249 Into => Comp_List,
15250 Report_Errors => Errors,
15251 Allow_Compile_Time => True);
15252 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15254 Create_All_Components;
15256 -- If the subtype declaration is created for a tagged type derivation
15257 -- with constraints, we retrieve the record definition of the parent
15258 -- type to select the components of the proper variant.
15260 elsif Is_Compile_Time_Known
15261 and then Is_Tagged_Type (Typ)
15262 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15263 and then
15264 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15265 and then Is_Variant_Record (Parent_Type)
15266 then
15267 Collect_Fixed_Components (Typ);
15268 Gather_Components
15269 (Typ,
15270 Component_List (Type_Definition (Parent (Parent_Type))),
15271 Governed_By => Assoc_List,
15272 Into => Comp_List,
15273 Report_Errors => Errors,
15274 Allow_Compile_Time => True);
15276 -- Note: previously there was a check at this point that no errors
15277 -- were detected. As a consequence of AI05-220 there may be an error
15278 -- if an inherited discriminant that controls a variant has a non-
15279 -- static constraint.
15281 -- If the tagged derivation has a type extension, collect all the
15282 -- new relevant components therein via Gather_Components.
15284 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15285 then
15286 Gather_Components
15287 (Typ,
15288 Component_List
15289 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15290 Governed_By => Assoc_List,
15291 Into => Comp_List,
15292 Report_Errors => Errors,
15293 Allow_Compile_Time => True,
15294 Include_Interface_Tag => True);
15295 end if;
15297 Create_All_Components;
15299 else
15300 -- If discriminants are not static, or if this is a multi-level type
15301 -- extension, we have to include all components of the parent type.
15303 Old_C := First_Component (Typ);
15304 while Present (Old_C) loop
15305 New_C := Create_Component (Old_C);
15307 Set_Etype
15308 (New_C,
15309 Constrain_Component_Type
15310 (Old_C, Subt, Decl_Node, Typ, Constraints));
15311 Set_Is_Public (New_C, Is_Public (Subt));
15313 Next_Component (Old_C);
15314 end loop;
15315 end if;
15317 End_Scope;
15318 end Create_Constrained_Components;
15320 ------------------------------------------
15321 -- Decimal_Fixed_Point_Type_Declaration --
15322 ------------------------------------------
15324 procedure Decimal_Fixed_Point_Type_Declaration
15325 (T : Entity_Id;
15326 Def : Node_Id)
15328 Loc : constant Source_Ptr := Sloc (Def);
15329 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15330 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15331 Max_Digits : constant Nat :=
15332 (if System_Max_Integer_Size = 128 then 38 else 18);
15333 -- Maximum number of digits that can be represented in an integer
15335 Implicit_Base : Entity_Id;
15336 Digs_Val : Uint;
15337 Delta_Val : Ureal;
15338 Scale_Val : Uint;
15339 Bound_Val : Ureal;
15341 begin
15342 Check_Restriction (No_Fixed_Point, Def);
15344 -- Create implicit base type
15346 Implicit_Base :=
15347 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15348 Set_Etype (Implicit_Base, Implicit_Base);
15350 -- Analyze and process delta expression
15352 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15354 Check_Delta_Expression (Delta_Expr);
15355 Delta_Val := Expr_Value_R (Delta_Expr);
15357 -- Check delta is power of 10, and determine scale value from it
15359 declare
15360 Val : Ureal;
15362 begin
15363 Scale_Val := Uint_0;
15364 Val := Delta_Val;
15366 if Val < Ureal_1 then
15367 while Val < Ureal_1 loop
15368 Val := Val * Ureal_10;
15369 Scale_Val := Scale_Val + 1;
15370 end loop;
15372 if Scale_Val > Max_Digits then
15373 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15374 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15375 Scale_Val := UI_From_Int (Max_Digits);
15376 end if;
15378 else
15379 while Val > Ureal_1 loop
15380 Val := Val / Ureal_10;
15381 Scale_Val := Scale_Val - 1;
15382 end loop;
15384 if Scale_Val < -Max_Digits then
15385 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15386 Error_Msg_N ("scale is less than minimum value of ^", Def);
15387 Scale_Val := UI_From_Int (-Max_Digits);
15388 end if;
15389 end if;
15391 if Val /= Ureal_1 then
15392 Error_Msg_N ("delta expression must be a power of 10", Def);
15393 Delta_Val := Ureal_10 ** (-Scale_Val);
15394 end if;
15395 end;
15397 -- Set delta, scale and small (small = delta for decimal type)
15399 Set_Delta_Value (Implicit_Base, Delta_Val);
15400 Set_Scale_Value (Implicit_Base, Scale_Val);
15401 Set_Small_Value (Implicit_Base, Delta_Val);
15403 -- Analyze and process digits expression
15405 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15406 Check_Digits_Expression (Digs_Expr);
15407 Digs_Val := Expr_Value (Digs_Expr);
15409 if Digs_Val > Max_Digits then
15410 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15411 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15412 Digs_Val := UI_From_Int (Max_Digits);
15413 end if;
15415 Set_Digits_Value (Implicit_Base, Digs_Val);
15416 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15418 -- Set range of base type from digits value for now. This will be
15419 -- expanded to represent the true underlying base range by Freeze.
15421 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15423 -- Note: We leave Esize unset for now, size will be set at freeze
15424 -- time. We have to do this for ordinary fixed-point, because the size
15425 -- depends on the specified small, and we might as well do the same for
15426 -- decimal fixed-point.
15428 pragma Assert (not Known_Esize (Implicit_Base));
15430 -- If there are bounds given in the declaration use them as the
15431 -- bounds of the first named subtype.
15433 if Present (Real_Range_Specification (Def)) then
15434 declare
15435 RRS : constant Node_Id := Real_Range_Specification (Def);
15436 Low : constant Node_Id := Low_Bound (RRS);
15437 High : constant Node_Id := High_Bound (RRS);
15438 Low_Val : Ureal;
15439 High_Val : Ureal;
15441 begin
15442 Analyze_And_Resolve (Low, Any_Real);
15443 Analyze_And_Resolve (High, Any_Real);
15444 Check_Real_Bound (Low);
15445 Check_Real_Bound (High);
15446 Low_Val := Expr_Value_R (Low);
15447 High_Val := Expr_Value_R (High);
15449 if Low_Val < (-Bound_Val) then
15450 Error_Msg_N
15451 ("range low bound too small for digits value", Low);
15452 Low_Val := -Bound_Val;
15453 end if;
15455 if High_Val > Bound_Val then
15456 Error_Msg_N
15457 ("range high bound too large for digits value", High);
15458 High_Val := Bound_Val;
15459 end if;
15461 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15462 end;
15464 -- If no explicit range, use range that corresponds to given
15465 -- digits value. This will end up as the final range for the
15466 -- first subtype.
15468 else
15469 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15470 end if;
15472 -- Complete entity for first subtype. The inheritance of the rep item
15473 -- chain ensures that SPARK-related pragmas are not clobbered when the
15474 -- decimal fixed point type acts as a full view of a private type.
15476 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15477 Set_Etype (T, Implicit_Base);
15478 Set_Size_Info (T, Implicit_Base);
15479 Inherit_Rep_Item_Chain (T, Implicit_Base);
15480 Set_Digits_Value (T, Digs_Val);
15481 Set_Delta_Value (T, Delta_Val);
15482 Set_Small_Value (T, Delta_Val);
15483 Set_Scale_Value (T, Scale_Val);
15484 Set_Is_Constrained (T);
15485 end Decimal_Fixed_Point_Type_Declaration;
15487 -----------------------------------
15488 -- Derive_Progenitor_Subprograms --
15489 -----------------------------------
15491 procedure Derive_Progenitor_Subprograms
15492 (Parent_Type : Entity_Id;
15493 Tagged_Type : Entity_Id)
15495 E : Entity_Id;
15496 Elmt : Elmt_Id;
15497 Iface : Entity_Id;
15498 Iface_Alias : Entity_Id;
15499 Iface_Elmt : Elmt_Id;
15500 Iface_Subp : Entity_Id;
15501 New_Subp : Entity_Id := Empty;
15502 Prim_Elmt : Elmt_Id;
15503 Subp : Entity_Id;
15504 Typ : Entity_Id;
15506 begin
15507 pragma Assert (Ada_Version >= Ada_2005
15508 and then Is_Record_Type (Tagged_Type)
15509 and then Is_Tagged_Type (Tagged_Type)
15510 and then Has_Interfaces (Tagged_Type));
15512 -- Step 1: Transfer to the full-view primitives associated with the
15513 -- partial-view that cover interface primitives. Conceptually this
15514 -- work should be done later by Process_Full_View; done here to
15515 -- simplify its implementation at later stages. It can be safely
15516 -- done here because interfaces must be visible in the partial and
15517 -- private view (RM 7.3(7.3/2)).
15519 -- Small optimization: This work is only required if the parent may
15520 -- have entities whose Alias attribute reference an interface primitive.
15521 -- Such a situation may occur if the parent is an abstract type and the
15522 -- primitive has not been yet overridden or if the parent is a generic
15523 -- formal type covering interfaces.
15525 -- If the tagged type is not abstract, it cannot have abstract
15526 -- primitives (the only entities in the list of primitives of
15527 -- non-abstract tagged types that can reference abstract primitives
15528 -- through its Alias attribute are the internal entities that have
15529 -- attribute Interface_Alias, and these entities are generated later
15530 -- by Add_Internal_Interface_Entities).
15532 if In_Private_Part (Current_Scope)
15533 and then (Is_Abstract_Type (Parent_Type)
15534 or else
15535 Is_Generic_Type (Parent_Type))
15536 then
15537 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15538 while Present (Elmt) loop
15539 Subp := Node (Elmt);
15541 -- At this stage it is not possible to have entities in the list
15542 -- of primitives that have attribute Interface_Alias.
15544 pragma Assert (No (Interface_Alias (Subp)));
15546 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15548 if Is_Interface (Typ) then
15549 E := Find_Primitive_Covering_Interface
15550 (Tagged_Type => Tagged_Type,
15551 Iface_Prim => Subp);
15553 if Present (E)
15554 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15555 then
15556 Replace_Elmt (Elmt, E);
15557 Remove_Homonym (Subp);
15558 end if;
15559 end if;
15561 Next_Elmt (Elmt);
15562 end loop;
15563 end if;
15565 -- Step 2: Add primitives of progenitors that are not implemented by
15566 -- parents of Tagged_Type.
15568 if Present (Interfaces (Base_Type (Tagged_Type))) then
15569 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15570 while Present (Iface_Elmt) loop
15571 Iface := Node (Iface_Elmt);
15573 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15574 while Present (Prim_Elmt) loop
15575 Iface_Subp := Node (Prim_Elmt);
15576 Iface_Alias := Ultimate_Alias (Iface_Subp);
15578 -- Exclude derivation of predefined primitives except those
15579 -- that come from source, or are inherited from one that comes
15580 -- from source. Required to catch declarations of equality
15581 -- operators of interfaces. For example:
15583 -- type Iface is interface;
15584 -- function "=" (Left, Right : Iface) return Boolean;
15586 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15587 or else Comes_From_Source (Iface_Alias)
15588 then
15589 E :=
15590 Find_Primitive_Covering_Interface
15591 (Tagged_Type => Tagged_Type,
15592 Iface_Prim => Iface_Subp);
15594 -- If not found we derive a new primitive leaving its alias
15595 -- attribute referencing the interface primitive.
15597 if No (E) then
15598 Derive_Subprogram
15599 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15601 -- Ada 2012 (AI05-0197): If the covering primitive's name
15602 -- differs from the name of the interface primitive then it
15603 -- is a private primitive inherited from a parent type. In
15604 -- such case, given that Tagged_Type covers the interface,
15605 -- the inherited private primitive becomes visible. For such
15606 -- purpose we add a new entity that renames the inherited
15607 -- private primitive.
15609 elsif Chars (E) /= Chars (Iface_Subp) then
15610 pragma Assert (Has_Suffix (E, 'P'));
15611 Derive_Subprogram
15612 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15613 Set_Alias (New_Subp, E);
15614 Set_Is_Abstract_Subprogram (New_Subp,
15615 Is_Abstract_Subprogram (E));
15617 -- Propagate to the full view interface entities associated
15618 -- with the partial view.
15620 elsif In_Private_Part (Current_Scope)
15621 and then Present (Alias (E))
15622 and then Alias (E) = Iface_Subp
15623 and then
15624 List_Containing (Parent (E)) /=
15625 Private_Declarations
15626 (Specification
15627 (Unit_Declaration_Node (Current_Scope)))
15628 then
15629 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15630 end if;
15631 end if;
15633 Next_Elmt (Prim_Elmt);
15634 end loop;
15636 Next_Elmt (Iface_Elmt);
15637 end loop;
15638 end if;
15639 end Derive_Progenitor_Subprograms;
15641 -----------------------
15642 -- Derive_Subprogram --
15643 -----------------------
15645 procedure Derive_Subprogram
15646 (New_Subp : out Entity_Id;
15647 Parent_Subp : Entity_Id;
15648 Derived_Type : Entity_Id;
15649 Parent_Type : Entity_Id;
15650 Actual_Subp : Entity_Id := Empty)
15652 Formal : Entity_Id;
15653 -- Formal parameter of parent primitive operation
15655 Formal_Of_Actual : Entity_Id;
15656 -- Formal parameter of actual operation, when the derivation is to
15657 -- create a renaming for a primitive operation of an actual in an
15658 -- instantiation.
15660 New_Formal : Entity_Id;
15661 -- Formal of inherited operation
15663 Visible_Subp : Entity_Id := Parent_Subp;
15665 function Is_Private_Overriding return Boolean;
15666 -- If Subp is a private overriding of a visible operation, the inherited
15667 -- operation derives from the overridden op (even though its body is the
15668 -- overriding one) and the inherited operation is visible now. See
15669 -- sem_disp to see the full details of the handling of the overridden
15670 -- subprogram, which is removed from the list of primitive operations of
15671 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15672 -- and used to diagnose abstract operations that need overriding in the
15673 -- derived type.
15675 procedure Replace_Type (Id, New_Id : Entity_Id);
15676 -- When the type is an anonymous access type, create a new access type
15677 -- designating the derived type.
15679 procedure Set_Derived_Name;
15680 -- This procedure sets the appropriate Chars name for New_Subp. This
15681 -- is normally just a copy of the parent name. An exception arises for
15682 -- type support subprograms, where the name is changed to reflect the
15683 -- name of the derived type, e.g. if type foo is derived from type bar,
15684 -- then a procedure barDA is derived with a name fooDA.
15686 ---------------------------
15687 -- Is_Private_Overriding --
15688 ---------------------------
15690 function Is_Private_Overriding return Boolean is
15691 Prev : Entity_Id;
15693 begin
15694 -- If the parent is not a dispatching operation there is no
15695 -- need to investigate overridings
15697 if not Is_Dispatching_Operation (Parent_Subp) then
15698 return False;
15699 end if;
15701 -- The visible operation that is overridden is a homonym of the
15702 -- parent subprogram. We scan the homonym chain to find the one
15703 -- whose alias is the subprogram we are deriving.
15705 Prev := Current_Entity (Parent_Subp);
15706 while Present (Prev) loop
15707 if Ekind (Prev) = Ekind (Parent_Subp)
15708 and then Alias (Prev) = Parent_Subp
15709 and then Scope (Parent_Subp) = Scope (Prev)
15710 and then not Is_Hidden (Prev)
15711 then
15712 Visible_Subp := Prev;
15713 return True;
15714 end if;
15716 Prev := Homonym (Prev);
15717 end loop;
15719 return False;
15720 end Is_Private_Overriding;
15722 ------------------
15723 -- Replace_Type --
15724 ------------------
15726 procedure Replace_Type (Id, New_Id : Entity_Id) is
15727 Id_Type : constant Entity_Id := Etype (Id);
15728 Acc_Type : Entity_Id;
15729 Par : constant Node_Id := Parent (Derived_Type);
15731 begin
15732 -- When the type is an anonymous access type, create a new access
15733 -- type designating the derived type. This itype must be elaborated
15734 -- at the point of the derivation, not on subsequent calls that may
15735 -- be out of the proper scope for Gigi, so we insert a reference to
15736 -- it after the derivation.
15738 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15739 declare
15740 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15742 begin
15743 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15744 and then Present (Full_View (Desig_Typ))
15745 and then not Is_Private_Type (Parent_Type)
15746 then
15747 Desig_Typ := Full_View (Desig_Typ);
15748 end if;
15750 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15752 -- Ada 2005 (AI-251): Handle also derivations of abstract
15753 -- interface primitives.
15755 or else (Is_Interface (Desig_Typ)
15756 and then not Is_Class_Wide_Type (Desig_Typ))
15757 then
15758 Acc_Type := New_Copy (Id_Type);
15759 Set_Etype (Acc_Type, Acc_Type);
15760 Set_Scope (Acc_Type, New_Subp);
15762 -- Set size of anonymous access type. If we have an access
15763 -- to an unconstrained array, this is a fat pointer, so it
15764 -- is sizes at twice addtress size.
15766 if Is_Array_Type (Desig_Typ)
15767 and then not Is_Constrained (Desig_Typ)
15768 then
15769 Init_Size (Acc_Type, 2 * System_Address_Size);
15771 -- Other cases use a thin pointer
15773 else
15774 Init_Size (Acc_Type, System_Address_Size);
15775 end if;
15777 -- Set remaining characterstics of anonymous access type
15779 Reinit_Alignment (Acc_Type);
15780 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15782 Set_Etype (New_Id, Acc_Type);
15783 Set_Scope (New_Id, New_Subp);
15785 -- Create a reference to it
15787 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15789 else
15790 Set_Etype (New_Id, Id_Type);
15791 end if;
15792 end;
15794 -- In Ada2012, a formal may have an incomplete type but the type
15795 -- derivation that inherits the primitive follows the full view.
15797 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15798 or else
15799 (Ekind (Id_Type) = E_Record_Type_With_Private
15800 and then Present (Full_View (Id_Type))
15801 and then
15802 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15803 or else
15804 (Ada_Version >= Ada_2012
15805 and then Ekind (Id_Type) = E_Incomplete_Type
15806 and then Full_View (Id_Type) = Parent_Type)
15807 then
15808 -- Constraint checks on formals are generated during expansion,
15809 -- based on the signature of the original subprogram. The bounds
15810 -- of the derived type are not relevant, and thus we can use
15811 -- the base type for the formals. However, the return type may be
15812 -- used in a context that requires that the proper static bounds
15813 -- be used (a case statement, for example) and for those cases
15814 -- we must use the derived type (first subtype), not its base.
15816 -- If the derived_type_definition has no constraints, we know that
15817 -- the derived type has the same constraints as the first subtype
15818 -- of the parent, and we can also use it rather than its base,
15819 -- which can lead to more efficient code.
15821 if Etype (Id) = Parent_Type then
15822 if Is_Scalar_Type (Parent_Type)
15823 and then
15824 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15825 then
15826 Set_Etype (New_Id, Derived_Type);
15828 elsif Nkind (Par) = N_Full_Type_Declaration
15829 and then
15830 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15831 and then
15832 Is_Entity_Name
15833 (Subtype_Indication (Type_Definition (Par)))
15834 then
15835 Set_Etype (New_Id, Derived_Type);
15837 else
15838 Set_Etype (New_Id, Base_Type (Derived_Type));
15839 end if;
15841 else
15842 Set_Etype (New_Id, Base_Type (Derived_Type));
15843 end if;
15845 else
15846 Set_Etype (New_Id, Etype (Id));
15847 end if;
15848 end Replace_Type;
15850 ----------------------
15851 -- Set_Derived_Name --
15852 ----------------------
15854 procedure Set_Derived_Name is
15855 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15856 begin
15857 if Nm = TSS_Null then
15858 Set_Chars (New_Subp, Chars (Parent_Subp));
15859 else
15860 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15861 end if;
15862 end Set_Derived_Name;
15864 -- Start of processing for Derive_Subprogram
15866 begin
15867 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15868 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
15870 -- Check whether the inherited subprogram is a private operation that
15871 -- should be inherited but not yet made visible. Such subprograms can
15872 -- become visible at a later point (e.g., the private part of a public
15873 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15874 -- following predicate is true, then this is not such a private
15875 -- operation and the subprogram simply inherits the name of the parent
15876 -- subprogram. Note the special check for the names of controlled
15877 -- operations, which are currently exempted from being inherited with
15878 -- a hidden name because they must be findable for generation of
15879 -- implicit run-time calls.
15881 if not Is_Hidden (Parent_Subp)
15882 or else Is_Internal (Parent_Subp)
15883 or else Is_Private_Overriding
15884 or else Is_Internal_Name (Chars (Parent_Subp))
15885 or else (Is_Controlled (Parent_Type)
15886 and then Chars (Parent_Subp) in Name_Adjust
15887 | Name_Finalize
15888 | Name_Initialize)
15889 then
15890 Set_Derived_Name;
15892 -- An inherited dispatching equality will be overridden by an internally
15893 -- generated one, or by an explicit one, so preserve its name and thus
15894 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15895 -- private operation it may become invisible if the full view has
15896 -- progenitors, and the dispatch table will be malformed.
15897 -- We check that the type is limited to handle the anomalous declaration
15898 -- of Limited_Controlled, which is derived from a non-limited type, and
15899 -- which is handled specially elsewhere as well.
15901 elsif Chars (Parent_Subp) = Name_Op_Eq
15902 and then Is_Dispatching_Operation (Parent_Subp)
15903 and then Etype (Parent_Subp) = Standard_Boolean
15904 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15905 and then
15906 Etype (First_Formal (Parent_Subp)) =
15907 Etype (Next_Formal (First_Formal (Parent_Subp)))
15908 then
15909 Set_Derived_Name;
15911 -- If parent is hidden, this can be a regular derivation if the
15912 -- parent is immediately visible in a non-instantiating context,
15913 -- or if we are in the private part of an instance. This test
15914 -- should still be refined ???
15916 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15917 -- operation as a non-visible operation in cases where the parent
15918 -- subprogram might not be visible now, but was visible within the
15919 -- original generic, so it would be wrong to make the inherited
15920 -- subprogram non-visible now. (Not clear if this test is fully
15921 -- correct; are there any cases where we should declare the inherited
15922 -- operation as not visible to avoid it being overridden, e.g., when
15923 -- the parent type is a generic actual with private primitives ???)
15925 -- (they should be treated the same as other private inherited
15926 -- subprograms, but it's not clear how to do this cleanly). ???
15928 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15929 and then Is_Immediately_Visible (Parent_Subp)
15930 and then not In_Instance)
15931 or else In_Instance_Not_Visible
15932 then
15933 Set_Derived_Name;
15935 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15936 -- overrides an interface primitive because interface primitives
15937 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15939 elsif Ada_Version >= Ada_2005
15940 and then Is_Dispatching_Operation (Parent_Subp)
15941 and then Present (Covered_Interface_Op (Parent_Subp))
15942 then
15943 Set_Derived_Name;
15945 -- Otherwise, the type is inheriting a private operation, so enter it
15946 -- with a special name so it can't be overridden. See also below, where
15947 -- we check for this case, and if so avoid setting Requires_Overriding.
15949 else
15950 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15951 end if;
15953 Set_Parent (New_Subp, Parent (Derived_Type));
15955 if Present (Actual_Subp) then
15956 Replace_Type (Actual_Subp, New_Subp);
15957 else
15958 Replace_Type (Parent_Subp, New_Subp);
15959 end if;
15961 Conditional_Delay (New_Subp, Parent_Subp);
15963 -- If we are creating a renaming for a primitive operation of an
15964 -- actual of a generic derived type, we must examine the signature
15965 -- of the actual primitive, not that of the generic formal, which for
15966 -- example may be an interface. However the name and initial value
15967 -- of the inherited operation are those of the formal primitive.
15969 Formal := First_Formal (Parent_Subp);
15971 if Present (Actual_Subp) then
15972 Formal_Of_Actual := First_Formal (Actual_Subp);
15973 else
15974 Formal_Of_Actual := Empty;
15975 end if;
15977 while Present (Formal) loop
15978 New_Formal := New_Copy (Formal);
15980 -- Extra formals are not inherited from a limited interface parent
15981 -- since limitedness is not inherited in such case (AI-419) and this
15982 -- affects the extra formals.
15984 if Is_Limited_Interface (Parent_Type) then
15985 Set_Extra_Formal (New_Formal, Empty);
15986 Set_Extra_Accessibility (New_Formal, Empty);
15987 end if;
15989 -- Normally we do not go copying parents, but in the case of
15990 -- formals, we need to link up to the declaration (which is the
15991 -- parameter specification), and it is fine to link up to the
15992 -- original formal's parameter specification in this case.
15994 Set_Parent (New_Formal, Parent (Formal));
15995 Append_Entity (New_Formal, New_Subp);
15997 if Present (Formal_Of_Actual) then
15998 Replace_Type (Formal_Of_Actual, New_Formal);
15999 Next_Formal (Formal_Of_Actual);
16000 else
16001 Replace_Type (Formal, New_Formal);
16002 end if;
16004 Next_Formal (Formal);
16005 end loop;
16007 -- Extra formals are shared between the parent subprogram and the
16008 -- derived subprogram (implicit in the above copy of formals), unless
16009 -- the parent type is a limited interface type; hence we must inherit
16010 -- also the reference to the first extra formal. When the parent type is
16011 -- an interface the extra formals will be added when the subprogram is
16012 -- frozen (see Freeze.Freeze_Subprogram).
16014 if not Is_Limited_Interface (Parent_Type) then
16015 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16017 if Ekind (New_Subp) = E_Function then
16018 Set_Extra_Accessibility_Of_Result (New_Subp,
16019 Extra_Accessibility_Of_Result (Parent_Subp));
16020 end if;
16021 end if;
16023 -- If this derivation corresponds to a tagged generic actual, then
16024 -- primitive operations rename those of the actual. Otherwise the
16025 -- primitive operations rename those of the parent type, If the parent
16026 -- renames an intrinsic operator, so does the new subprogram. We except
16027 -- concatenation, which is always properly typed, and does not get
16028 -- expanded as other intrinsic operations.
16030 if No (Actual_Subp) then
16031 if Is_Intrinsic_Subprogram (Parent_Subp) then
16032 Set_Is_Intrinsic_Subprogram (New_Subp);
16034 if Present (Alias (Parent_Subp))
16035 and then Chars (Parent_Subp) /= Name_Op_Concat
16036 then
16037 Set_Alias (New_Subp, Alias (Parent_Subp));
16038 else
16039 Set_Alias (New_Subp, Parent_Subp);
16040 end if;
16042 else
16043 Set_Alias (New_Subp, Parent_Subp);
16044 end if;
16046 else
16047 Set_Alias (New_Subp, Actual_Subp);
16048 end if;
16050 -- Derived subprograms of a tagged type must inherit the convention
16051 -- of the parent subprogram (a requirement of AI-117). Derived
16052 -- subprograms of untagged types simply get convention Ada by default.
16054 -- If the derived type is a tagged generic formal type with unknown
16055 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16057 -- However, if the type is derived from a generic formal, the further
16058 -- inherited subprogram has the convention of the non-generic ancestor.
16059 -- Otherwise there would be no way to override the operation.
16060 -- (This is subject to forthcoming ARG discussions).
16062 if Is_Tagged_Type (Derived_Type) then
16063 if Is_Generic_Type (Derived_Type)
16064 and then Has_Unknown_Discriminants (Derived_Type)
16065 then
16066 Set_Convention (New_Subp, Convention_Intrinsic);
16068 else
16069 if Is_Generic_Type (Parent_Type)
16070 and then Has_Unknown_Discriminants (Parent_Type)
16071 then
16072 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16073 else
16074 Set_Convention (New_Subp, Convention (Parent_Subp));
16075 end if;
16076 end if;
16077 end if;
16079 -- Predefined controlled operations retain their name even if the parent
16080 -- is hidden (see above), but they are not primitive operations if the
16081 -- ancestor is not visible, for example if the parent is a private
16082 -- extension completed with a controlled extension. Note that a full
16083 -- type that is controlled can break privacy: the flag Is_Controlled is
16084 -- set on both views of the type.
16086 if Is_Controlled (Parent_Type)
16087 and then Chars (Parent_Subp) in Name_Initialize
16088 | Name_Adjust
16089 | Name_Finalize
16090 and then Is_Hidden (Parent_Subp)
16091 and then not Is_Visibly_Controlled (Parent_Type)
16092 then
16093 Set_Is_Hidden (New_Subp);
16094 end if;
16096 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16097 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16099 if Ekind (Parent_Subp) = E_Procedure then
16100 Set_Is_Valued_Procedure
16101 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16102 else
16103 Set_Has_Controlling_Result
16104 (New_Subp, Has_Controlling_Result (Parent_Subp));
16105 end if;
16107 -- No_Return must be inherited properly. If this is overridden in the
16108 -- case of a dispatching operation, then the check is made later in
16109 -- Check_Abstract_Overriding that the overriding operation is also
16110 -- No_Return (no such check is required for the nondispatching case).
16112 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16114 -- If the parent subprogram is marked as Ghost, then so is the derived
16115 -- subprogram. The ghost policy for the derived subprogram is set from
16116 -- the effective ghost policy at the point of derived type declaration.
16118 if Is_Ghost_Entity (Parent_Subp) then
16119 Set_Is_Ghost_Entity (New_Subp);
16120 end if;
16122 -- A derived function with a controlling result is abstract. If the
16123 -- Derived_Type is a nonabstract formal generic derived type, then
16124 -- inherited operations are not abstract: the required check is done at
16125 -- instantiation time. If the derivation is for a generic actual, the
16126 -- function is not abstract unless the actual is.
16128 if Is_Generic_Type (Derived_Type)
16129 and then not Is_Abstract_Type (Derived_Type)
16130 then
16131 null;
16133 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16134 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16135 -- that functions with controlling access results of record extensions
16136 -- with a null extension part require overriding (AI95-00391/06).
16138 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16139 -- implementing the rule of RM 7.3.2(6.1/4).
16141 -- A subprogram subject to pragma Extensions_Visible with value False
16142 -- requires overriding if the subprogram has at least one controlling
16143 -- OUT parameter (SPARK RM 6.1.7(6)).
16145 elsif Ada_Version >= Ada_2005
16146 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16147 or else (Is_Tagged_Type (Derived_Type)
16148 and then Etype (New_Subp) = Derived_Type
16149 and then not Is_Null_Extension (Derived_Type))
16150 or else (Is_Tagged_Type (Derived_Type)
16151 and then Ekind (Etype (New_Subp)) =
16152 E_Anonymous_Access_Type
16153 and then Designated_Type (Etype (New_Subp)) =
16154 Derived_Type)
16155 or else (Comes_From_Source (Alias (New_Subp))
16156 and then Is_EVF_Procedure (Alias (New_Subp)))
16158 -- AI12-0042: Set Requires_Overriding when a type extension
16159 -- inherits a private operation that is visible at the
16160 -- point of extension (Has_Private_Ancestor is False) from
16161 -- an ancestor that has Type_Invariant'Class, and when the
16162 -- type extension is in a visible part (the latter as
16163 -- clarified by AI12-0382).
16165 or else
16166 (not Has_Private_Ancestor (Derived_Type)
16167 and then Has_Invariants (Parent_Type)
16168 and then
16169 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16170 and then
16171 Class_Present
16172 (Get_Pragma (Parent_Type, Pragma_Invariant))
16173 and then Is_Private_Primitive (Parent_Subp)
16174 and then In_Visible_Part (Scope (Derived_Type))))
16176 and then No (Actual_Subp)
16177 then
16178 if not Is_Tagged_Type (Derived_Type)
16179 or else Is_Abstract_Type (Derived_Type)
16180 or else Is_Abstract_Subprogram (Alias (New_Subp))
16181 then
16182 Set_Is_Abstract_Subprogram (New_Subp);
16184 -- If the Chars of the new subprogram is different from that of the
16185 -- parent's one, it means that we entered it with a special name so
16186 -- it can't be overridden (see above). In that case we had better not
16187 -- *require* it to be overridden. This is the case where the parent
16188 -- type inherited the operation privately, so there's no danger of
16189 -- dangling dispatching.
16191 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16192 Set_Requires_Overriding (New_Subp);
16193 end if;
16195 elsif Ada_Version < Ada_2005
16196 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16197 or else (Is_Tagged_Type (Derived_Type)
16198 and then Etype (New_Subp) = Derived_Type
16199 and then No (Actual_Subp)))
16200 then
16201 Set_Is_Abstract_Subprogram (New_Subp);
16203 -- AI05-0097 : an inherited operation that dispatches on result is
16204 -- abstract if the derived type is abstract, even if the parent type
16205 -- is concrete and the derived type is a null extension.
16207 elsif Has_Controlling_Result (Alias (New_Subp))
16208 and then Is_Abstract_Type (Etype (New_Subp))
16209 then
16210 Set_Is_Abstract_Subprogram (New_Subp);
16212 -- Finally, if the parent type is abstract we must verify that all
16213 -- inherited operations are either non-abstract or overridden, or that
16214 -- the derived type itself is abstract (this check is performed at the
16215 -- end of a package declaration, in Check_Abstract_Overriding). A
16216 -- private overriding in the parent type will not be visible in the
16217 -- derivation if we are not in an inner package or in a child unit of
16218 -- the parent type, in which case the abstractness of the inherited
16219 -- operation is carried to the new subprogram.
16221 elsif Is_Abstract_Type (Parent_Type)
16222 and then not In_Open_Scopes (Scope (Parent_Type))
16223 and then Is_Private_Overriding
16224 and then Is_Abstract_Subprogram (Visible_Subp)
16225 then
16226 if No (Actual_Subp) then
16227 Set_Alias (New_Subp, Visible_Subp);
16228 Set_Is_Abstract_Subprogram (New_Subp, True);
16230 else
16231 -- If this is a derivation for an instance of a formal derived
16232 -- type, abstractness comes from the primitive operation of the
16233 -- actual, not from the operation inherited from the ancestor.
16235 Set_Is_Abstract_Subprogram
16236 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16237 end if;
16238 end if;
16240 New_Overloaded_Entity (New_Subp, Derived_Type);
16242 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16243 -- preconditions and the derived type is abstract, the derived operation
16244 -- is abstract as well if parent subprogram is not abstract or null.
16246 if Is_Abstract_Type (Derived_Type)
16247 and then Has_Non_Trivial_Precondition (Parent_Subp)
16248 and then Present (Interfaces (Derived_Type))
16249 then
16251 -- Add useful attributes of subprogram before the freeze point,
16252 -- in case freezing is delayed or there are previous errors.
16254 Set_Is_Dispatching_Operation (New_Subp);
16256 declare
16257 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16259 begin
16260 if Present (Iface_Prim)
16261 and then Has_Non_Trivial_Precondition (Iface_Prim)
16262 then
16263 Set_Is_Abstract_Subprogram (New_Subp);
16264 end if;
16265 end;
16266 end if;
16268 -- Check for case of a derived subprogram for the instantiation of a
16269 -- formal derived tagged type, if so mark the subprogram as dispatching
16270 -- and inherit the dispatching attributes of the actual subprogram. The
16271 -- derived subprogram is effectively renaming of the actual subprogram,
16272 -- so it needs to have the same attributes as the actual.
16274 if Present (Actual_Subp)
16275 and then Is_Dispatching_Operation (Actual_Subp)
16276 then
16277 Set_Is_Dispatching_Operation (New_Subp);
16279 if Present (DTC_Entity (Actual_Subp)) then
16280 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16281 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16282 end if;
16283 end if;
16285 -- Indicate that a derived subprogram does not require a body and that
16286 -- it does not require processing of default expressions.
16288 Set_Has_Completion (New_Subp);
16289 Set_Default_Expressions_Processed (New_Subp);
16291 if Ekind (New_Subp) = E_Function then
16292 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16293 end if;
16295 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16296 -- primitive subprogram S of a type T, then the aspect is inherited
16297 -- by the corresponding primitive subprogram of each descendant of T.
16299 if Is_Tagged_Type (Derived_Type)
16300 and then Is_Dispatching_Operation (New_Subp)
16301 and then Has_Yield_Aspect (Alias (New_Subp))
16302 then
16303 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16304 end if;
16306 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16307 end Derive_Subprogram;
16309 ------------------------
16310 -- Derive_Subprograms --
16311 ------------------------
16313 procedure Derive_Subprograms
16314 (Parent_Type : Entity_Id;
16315 Derived_Type : Entity_Id;
16316 Generic_Actual : Entity_Id := Empty)
16318 Op_List : constant Elist_Id :=
16319 Collect_Primitive_Operations (Parent_Type);
16321 function Check_Derived_Type return Boolean;
16322 -- Check that all the entities derived from Parent_Type are found in
16323 -- the list of primitives of Derived_Type exactly in the same order.
16325 procedure Derive_Interface_Subprogram
16326 (New_Subp : out Entity_Id;
16327 Subp : Entity_Id;
16328 Actual_Subp : Entity_Id);
16329 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16330 -- (which is an interface primitive). If Generic_Actual is present then
16331 -- Actual_Subp is the actual subprogram corresponding with the generic
16332 -- subprogram Subp.
16334 ------------------------
16335 -- Check_Derived_Type --
16336 ------------------------
16338 function Check_Derived_Type return Boolean is
16339 E : Entity_Id;
16340 Elmt : Elmt_Id;
16341 List : Elist_Id;
16342 New_Subp : Entity_Id;
16343 Op_Elmt : Elmt_Id;
16344 Subp : Entity_Id;
16346 begin
16347 -- Traverse list of entities in the current scope searching for
16348 -- an incomplete type whose full-view is derived type.
16350 E := First_Entity (Scope (Derived_Type));
16351 while Present (E) and then E /= Derived_Type loop
16352 if Ekind (E) = E_Incomplete_Type
16353 and then Present (Full_View (E))
16354 and then Full_View (E) = Derived_Type
16355 then
16356 -- Disable this test if Derived_Type completes an incomplete
16357 -- type because in such case more primitives can be added
16358 -- later to the list of primitives of Derived_Type by routine
16359 -- Process_Incomplete_Dependents
16361 return True;
16362 end if;
16364 Next_Entity (E);
16365 end loop;
16367 List := Collect_Primitive_Operations (Derived_Type);
16368 Elmt := First_Elmt (List);
16370 Op_Elmt := First_Elmt (Op_List);
16371 while Present (Op_Elmt) loop
16372 Subp := Node (Op_Elmt);
16373 New_Subp := Node (Elmt);
16375 -- At this early stage Derived_Type has no entities with attribute
16376 -- Interface_Alias. In addition, such primitives are always
16377 -- located at the end of the list of primitives of Parent_Type.
16378 -- Therefore, if found we can safely stop processing pending
16379 -- entities.
16381 exit when Present (Interface_Alias (Subp));
16383 -- Handle hidden entities
16385 if not Is_Predefined_Dispatching_Operation (Subp)
16386 and then Is_Hidden (Subp)
16387 then
16388 if Present (New_Subp)
16389 and then Primitive_Names_Match (Subp, New_Subp)
16390 then
16391 Next_Elmt (Elmt);
16392 end if;
16394 else
16395 if not Present (New_Subp)
16396 or else Ekind (Subp) /= Ekind (New_Subp)
16397 or else not Primitive_Names_Match (Subp, New_Subp)
16398 then
16399 return False;
16400 end if;
16402 Next_Elmt (Elmt);
16403 end if;
16405 Next_Elmt (Op_Elmt);
16406 end loop;
16408 return True;
16409 end Check_Derived_Type;
16411 ---------------------------------
16412 -- Derive_Interface_Subprogram --
16413 ---------------------------------
16415 procedure Derive_Interface_Subprogram
16416 (New_Subp : out Entity_Id;
16417 Subp : Entity_Id;
16418 Actual_Subp : Entity_Id)
16420 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16421 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16423 begin
16424 pragma Assert (Is_Interface (Iface_Type));
16426 Derive_Subprogram
16427 (New_Subp => New_Subp,
16428 Parent_Subp => Iface_Subp,
16429 Derived_Type => Derived_Type,
16430 Parent_Type => Iface_Type,
16431 Actual_Subp => Actual_Subp);
16433 -- Given that this new interface entity corresponds with a primitive
16434 -- of the parent that was not overridden we must leave it associated
16435 -- with its parent primitive to ensure that it will share the same
16436 -- dispatch table slot when overridden. We must set the Alias to Subp
16437 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16438 -- (in case we inherited Subp from Iface_Type via a nonabstract
16439 -- generic formal type).
16441 if No (Actual_Subp) then
16442 Set_Alias (New_Subp, Subp);
16444 declare
16445 T : Entity_Id := Find_Dispatching_Type (Subp);
16446 begin
16447 while Etype (T) /= T loop
16448 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16449 Set_Is_Abstract_Subprogram (New_Subp, False);
16450 exit;
16451 end if;
16453 T := Etype (T);
16454 end loop;
16455 end;
16457 -- For instantiations this is not needed since the previous call to
16458 -- Derive_Subprogram leaves the entity well decorated.
16460 else
16461 pragma Assert (Alias (New_Subp) = Actual_Subp);
16462 null;
16463 end if;
16464 end Derive_Interface_Subprogram;
16466 -- Local variables
16468 Alias_Subp : Entity_Id;
16469 Act_List : Elist_Id;
16470 Act_Elmt : Elmt_Id;
16471 Act_Subp : Entity_Id := Empty;
16472 Elmt : Elmt_Id;
16473 Need_Search : Boolean := False;
16474 New_Subp : Entity_Id := Empty;
16475 Parent_Base : Entity_Id;
16476 Subp : Entity_Id;
16478 -- Start of processing for Derive_Subprograms
16480 begin
16481 if Ekind (Parent_Type) = E_Record_Type_With_Private
16482 and then Has_Discriminants (Parent_Type)
16483 and then Present (Full_View (Parent_Type))
16484 then
16485 Parent_Base := Full_View (Parent_Type);
16486 else
16487 Parent_Base := Parent_Type;
16488 end if;
16490 if Present (Generic_Actual) then
16491 Act_List := Collect_Primitive_Operations (Generic_Actual);
16492 Act_Elmt := First_Elmt (Act_List);
16493 else
16494 Act_List := No_Elist;
16495 Act_Elmt := No_Elmt;
16496 end if;
16498 -- Derive primitives inherited from the parent. Note that if the generic
16499 -- actual is present, this is not really a type derivation, it is a
16500 -- completion within an instance.
16502 -- Case 1: Derived_Type does not implement interfaces
16504 if not Is_Tagged_Type (Derived_Type)
16505 or else (not Has_Interfaces (Derived_Type)
16506 and then not (Present (Generic_Actual)
16507 and then Has_Interfaces (Generic_Actual)))
16508 then
16509 Elmt := First_Elmt (Op_List);
16510 while Present (Elmt) loop
16511 Subp := Node (Elmt);
16513 -- Literals are derived earlier in the process of building the
16514 -- derived type, and are skipped here.
16516 if Ekind (Subp) = E_Enumeration_Literal then
16517 null;
16519 -- The actual is a direct descendant and the common primitive
16520 -- operations appear in the same order.
16522 -- If the generic parent type is present, the derived type is an
16523 -- instance of a formal derived type, and within the instance its
16524 -- operations are those of the actual. We derive from the formal
16525 -- type but make the inherited operations aliases of the
16526 -- corresponding operations of the actual.
16528 else
16529 pragma Assert (No (Node (Act_Elmt))
16530 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16531 and then
16532 Type_Conformant
16533 (Subp, Node (Act_Elmt),
16534 Skip_Controlling_Formals => True)));
16536 Derive_Subprogram
16537 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16539 if Present (Act_Elmt) then
16540 Next_Elmt (Act_Elmt);
16541 end if;
16542 end if;
16544 Next_Elmt (Elmt);
16545 end loop;
16547 -- Case 2: Derived_Type implements interfaces
16549 else
16550 -- If the parent type has no predefined primitives we remove
16551 -- predefined primitives from the list of primitives of generic
16552 -- actual to simplify the complexity of this algorithm.
16554 if Present (Generic_Actual) then
16555 declare
16556 Has_Predefined_Primitives : Boolean := False;
16558 begin
16559 -- Check if the parent type has predefined primitives
16561 Elmt := First_Elmt (Op_List);
16562 while Present (Elmt) loop
16563 Subp := Node (Elmt);
16565 if Is_Predefined_Dispatching_Operation (Subp)
16566 and then not Comes_From_Source (Ultimate_Alias (Subp))
16567 then
16568 Has_Predefined_Primitives := True;
16569 exit;
16570 end if;
16572 Next_Elmt (Elmt);
16573 end loop;
16575 -- Remove predefined primitives of Generic_Actual. We must use
16576 -- an auxiliary list because in case of tagged types the value
16577 -- returned by Collect_Primitive_Operations is the value stored
16578 -- in its Primitive_Operations attribute (and we don't want to
16579 -- modify its current contents).
16581 if not Has_Predefined_Primitives then
16582 declare
16583 Aux_List : constant Elist_Id := New_Elmt_List;
16585 begin
16586 Elmt := First_Elmt (Act_List);
16587 while Present (Elmt) loop
16588 Subp := Node (Elmt);
16590 if not Is_Predefined_Dispatching_Operation (Subp)
16591 or else Comes_From_Source (Subp)
16592 then
16593 Append_Elmt (Subp, Aux_List);
16594 end if;
16596 Next_Elmt (Elmt);
16597 end loop;
16599 Act_List := Aux_List;
16600 end;
16601 end if;
16603 Act_Elmt := First_Elmt (Act_List);
16604 Act_Subp := Node (Act_Elmt);
16605 end;
16606 end if;
16608 -- Stage 1: If the generic actual is not present we derive the
16609 -- primitives inherited from the parent type. If the generic parent
16610 -- type is present, the derived type is an instance of a formal
16611 -- derived type, and within the instance its operations are those of
16612 -- the actual. We derive from the formal type but make the inherited
16613 -- operations aliases of the corresponding operations of the actual.
16615 Elmt := First_Elmt (Op_List);
16616 while Present (Elmt) loop
16617 Subp := Node (Elmt);
16618 Alias_Subp := Ultimate_Alias (Subp);
16620 -- Do not derive internal entities of the parent that link
16621 -- interface primitives with their covering primitive. These
16622 -- entities will be added to this type when frozen.
16624 if Present (Interface_Alias (Subp)) then
16625 goto Continue;
16626 end if;
16628 -- If the generic actual is present find the corresponding
16629 -- operation in the generic actual. If the parent type is a
16630 -- direct ancestor of the derived type then, even if it is an
16631 -- interface, the operations are inherited from the primary
16632 -- dispatch table and are in the proper order. If we detect here
16633 -- that primitives are not in the same order we traverse the list
16634 -- of primitive operations of the actual to find the one that
16635 -- implements the interface primitive.
16637 if Need_Search
16638 or else
16639 (Present (Generic_Actual)
16640 and then Present (Act_Subp)
16641 and then not
16642 (Primitive_Names_Match (Subp, Act_Subp)
16643 and then
16644 Type_Conformant (Subp, Act_Subp,
16645 Skip_Controlling_Formals => True)))
16646 then
16647 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16648 Use_Full_View => True));
16650 -- Remember that we need searching for all pending primitives
16652 Need_Search := True;
16654 -- Handle entities associated with interface primitives
16656 if Present (Alias_Subp)
16657 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16658 and then not Is_Predefined_Dispatching_Operation (Subp)
16659 then
16660 -- Search for the primitive in the homonym chain
16662 Act_Subp :=
16663 Find_Primitive_Covering_Interface
16664 (Tagged_Type => Generic_Actual,
16665 Iface_Prim => Alias_Subp);
16667 -- Previous search may not locate primitives covering
16668 -- interfaces defined in generics units or instantiations.
16669 -- (it fails if the covering primitive has formals whose
16670 -- type is also defined in generics or instantiations).
16671 -- In such case we search in the list of primitives of the
16672 -- generic actual for the internal entity that links the
16673 -- interface primitive and the covering primitive.
16675 if No (Act_Subp)
16676 and then Is_Generic_Type (Parent_Type)
16677 then
16678 -- This code has been designed to handle only generic
16679 -- formals that implement interfaces that are defined
16680 -- in a generic unit or instantiation. If this code is
16681 -- needed for other cases we must review it because
16682 -- (given that it relies on Original_Location to locate
16683 -- the primitive of Generic_Actual that covers the
16684 -- interface) it could leave linked through attribute
16685 -- Alias entities of unrelated instantiations).
16687 pragma Assert
16688 (Is_Generic_Unit
16689 (Scope (Find_Dispatching_Type (Alias_Subp)))
16690 or else
16691 Instantiation_Depth
16692 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16694 declare
16695 Iface_Prim_Loc : constant Source_Ptr :=
16696 Original_Location (Sloc (Alias_Subp));
16698 Elmt : Elmt_Id;
16699 Prim : Entity_Id;
16701 begin
16702 Elmt :=
16703 First_Elmt (Primitive_Operations (Generic_Actual));
16705 Search : while Present (Elmt) loop
16706 Prim := Node (Elmt);
16708 if Present (Interface_Alias (Prim))
16709 and then Original_Location
16710 (Sloc (Interface_Alias (Prim))) =
16711 Iface_Prim_Loc
16712 then
16713 Act_Subp := Alias (Prim);
16714 exit Search;
16715 end if;
16717 Next_Elmt (Elmt);
16718 end loop Search;
16719 end;
16720 end if;
16722 pragma Assert (Present (Act_Subp)
16723 or else Is_Abstract_Type (Generic_Actual)
16724 or else Serious_Errors_Detected > 0);
16726 -- Handle predefined primitives plus the rest of user-defined
16727 -- primitives
16729 else
16730 Act_Elmt := First_Elmt (Act_List);
16731 while Present (Act_Elmt) loop
16732 Act_Subp := Node (Act_Elmt);
16734 exit when Primitive_Names_Match (Subp, Act_Subp)
16735 and then Type_Conformant
16736 (Subp, Act_Subp,
16737 Skip_Controlling_Formals => True)
16738 and then No (Interface_Alias (Act_Subp));
16740 Next_Elmt (Act_Elmt);
16741 end loop;
16743 if No (Act_Elmt) then
16744 Act_Subp := Empty;
16745 end if;
16746 end if;
16747 end if;
16749 -- Case 1: If the parent is a limited interface then it has the
16750 -- predefined primitives of synchronized interfaces. However, the
16751 -- actual type may be a non-limited type and hence it does not
16752 -- have such primitives.
16754 if Present (Generic_Actual)
16755 and then not Present (Act_Subp)
16756 and then Is_Limited_Interface (Parent_Base)
16757 and then Is_Predefined_Interface_Primitive (Subp)
16758 then
16759 null;
16761 -- Case 2: Inherit entities associated with interfaces that were
16762 -- not covered by the parent type. We exclude here null interface
16763 -- primitives because they do not need special management.
16765 -- We also exclude interface operations that are renamings. If the
16766 -- subprogram is an explicit renaming of an interface primitive,
16767 -- it is a regular primitive operation, and the presence of its
16768 -- alias is not relevant: it has to be derived like any other
16769 -- primitive.
16771 elsif Present (Alias (Subp))
16772 and then Nkind (Unit_Declaration_Node (Subp)) /=
16773 N_Subprogram_Renaming_Declaration
16774 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16775 and then not
16776 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16777 and then Null_Present (Parent (Alias_Subp)))
16778 then
16779 -- If this is an abstract private type then we transfer the
16780 -- derivation of the interface primitive from the partial view
16781 -- to the full view. This is safe because all the interfaces
16782 -- must be visible in the partial view. Done to avoid adding
16783 -- a new interface derivation to the private part of the
16784 -- enclosing package; otherwise this new derivation would be
16785 -- decorated as hidden when the analysis of the enclosing
16786 -- package completes.
16788 if Is_Abstract_Type (Derived_Type)
16789 and then In_Private_Part (Current_Scope)
16790 and then Has_Private_Declaration (Derived_Type)
16791 then
16792 declare
16793 Partial_View : Entity_Id;
16794 Elmt : Elmt_Id;
16795 Ent : Entity_Id;
16797 begin
16798 Partial_View := First_Entity (Current_Scope);
16799 loop
16800 exit when No (Partial_View)
16801 or else (Has_Private_Declaration (Partial_View)
16802 and then
16803 Full_View (Partial_View) = Derived_Type);
16805 Next_Entity (Partial_View);
16806 end loop;
16808 -- If the partial view was not found then the source code
16809 -- has errors and the derivation is not needed.
16811 if Present (Partial_View) then
16812 Elmt :=
16813 First_Elmt (Primitive_Operations (Partial_View));
16814 while Present (Elmt) loop
16815 Ent := Node (Elmt);
16817 if Present (Alias (Ent))
16818 and then Ultimate_Alias (Ent) = Alias (Subp)
16819 then
16820 Append_Elmt
16821 (Ent, Primitive_Operations (Derived_Type));
16822 exit;
16823 end if;
16825 Next_Elmt (Elmt);
16826 end loop;
16828 -- If the interface primitive was not found in the
16829 -- partial view then this interface primitive was
16830 -- overridden. We add a derivation to activate in
16831 -- Derive_Progenitor_Subprograms the machinery to
16832 -- search for it.
16834 if No (Elmt) then
16835 Derive_Interface_Subprogram
16836 (New_Subp => New_Subp,
16837 Subp => Subp,
16838 Actual_Subp => Act_Subp);
16839 end if;
16840 end if;
16841 end;
16842 else
16843 Derive_Interface_Subprogram
16844 (New_Subp => New_Subp,
16845 Subp => Subp,
16846 Actual_Subp => Act_Subp);
16847 end if;
16849 -- Case 3: Common derivation
16851 else
16852 Derive_Subprogram
16853 (New_Subp => New_Subp,
16854 Parent_Subp => Subp,
16855 Derived_Type => Derived_Type,
16856 Parent_Type => Parent_Base,
16857 Actual_Subp => Act_Subp);
16858 end if;
16860 -- No need to update Act_Elm if we must search for the
16861 -- corresponding operation in the generic actual
16863 if not Need_Search
16864 and then Present (Act_Elmt)
16865 then
16866 Next_Elmt (Act_Elmt);
16867 Act_Subp := Node (Act_Elmt);
16868 end if;
16870 <<Continue>>
16871 Next_Elmt (Elmt);
16872 end loop;
16874 -- Inherit additional operations from progenitors. If the derived
16875 -- type is a generic actual, there are not new primitive operations
16876 -- for the type because it has those of the actual, and therefore
16877 -- nothing needs to be done. The renamings generated above are not
16878 -- primitive operations, and their purpose is simply to make the
16879 -- proper operations visible within an instantiation.
16881 if No (Generic_Actual) then
16882 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16883 end if;
16884 end if;
16886 -- Final check: Direct descendants must have their primitives in the
16887 -- same order. We exclude from this test untagged types and instances
16888 -- of formal derived types. We skip this test if we have already
16889 -- reported serious errors in the sources.
16891 pragma Assert (not Is_Tagged_Type (Derived_Type)
16892 or else Present (Generic_Actual)
16893 or else Serious_Errors_Detected > 0
16894 or else Check_Derived_Type);
16895 end Derive_Subprograms;
16897 --------------------------------
16898 -- Derived_Standard_Character --
16899 --------------------------------
16901 procedure Derived_Standard_Character
16902 (N : Node_Id;
16903 Parent_Type : Entity_Id;
16904 Derived_Type : Entity_Id)
16906 Loc : constant Source_Ptr := Sloc (N);
16907 Def : constant Node_Id := Type_Definition (N);
16908 Indic : constant Node_Id := Subtype_Indication (Def);
16909 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16910 Implicit_Base : constant Entity_Id :=
16911 Create_Itype
16912 (E_Enumeration_Type, N, Derived_Type, 'B');
16914 Lo : Node_Id;
16915 Hi : Node_Id;
16917 begin
16918 Discard_Node (Process_Subtype (Indic, N));
16920 Set_Etype (Implicit_Base, Parent_Base);
16921 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16922 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16924 Set_Is_Character_Type (Implicit_Base, True);
16925 Set_Has_Delayed_Freeze (Implicit_Base);
16927 -- The bounds of the implicit base are the bounds of the parent base.
16928 -- Note that their type is the parent base.
16930 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16931 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16933 Set_Scalar_Range (Implicit_Base,
16934 Make_Range (Loc,
16935 Low_Bound => Lo,
16936 High_Bound => Hi));
16938 Conditional_Delay (Derived_Type, Parent_Type);
16940 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
16941 Set_Etype (Derived_Type, Implicit_Base);
16942 Set_Size_Info (Derived_Type, Parent_Type);
16944 if not Known_RM_Size (Derived_Type) then
16945 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16946 end if;
16948 Set_Is_Character_Type (Derived_Type, True);
16950 if Nkind (Indic) /= N_Subtype_Indication then
16952 -- If no explicit constraint, the bounds are those
16953 -- of the parent type.
16955 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16956 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16957 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16958 end if;
16960 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16962 -- Because the implicit base is used in the conversion of the bounds, we
16963 -- have to freeze it now. This is similar to what is done for numeric
16964 -- types, and it equally suspicious, but otherwise a nonstatic bound
16965 -- will have a reference to an unfrozen type, which is rejected by Gigi
16966 -- (???). This requires specific care for definition of stream
16967 -- attributes. For details, see comments at the end of
16968 -- Build_Derived_Numeric_Type.
16970 Freeze_Before (N, Implicit_Base);
16971 end Derived_Standard_Character;
16973 ------------------------------
16974 -- Derived_Type_Declaration --
16975 ------------------------------
16977 procedure Derived_Type_Declaration
16978 (T : Entity_Id;
16979 N : Node_Id;
16980 Is_Completion : Boolean)
16982 Parent_Type : Entity_Id;
16984 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16985 -- Check whether the parent type is a generic formal, or derives
16986 -- directly or indirectly from one.
16988 ------------------------
16989 -- Comes_From_Generic --
16990 ------------------------
16992 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16993 begin
16994 if Is_Generic_Type (Typ) then
16995 return True;
16997 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16998 return True;
17000 elsif Is_Private_Type (Typ)
17001 and then Present (Full_View (Typ))
17002 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17003 then
17004 return True;
17006 elsif Is_Generic_Actual_Type (Typ) then
17007 return True;
17009 else
17010 return False;
17011 end if;
17012 end Comes_From_Generic;
17014 -- Local variables
17016 Def : constant Node_Id := Type_Definition (N);
17017 Iface_Def : Node_Id;
17018 Indic : constant Node_Id := Subtype_Indication (Def);
17019 Extension : constant Node_Id := Record_Extension_Part (Def);
17020 Parent_Node : Node_Id;
17021 Taggd : Boolean;
17023 -- Start of processing for Derived_Type_Declaration
17025 begin
17026 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17028 if SPARK_Mode = On
17029 and then Is_Tagged_Type (Parent_Type)
17030 then
17031 declare
17032 Partial_View : constant Entity_Id :=
17033 Incomplete_Or_Partial_View (Parent_Type);
17035 begin
17036 -- If the partial view was not found then the parent type is not
17037 -- a private type. Otherwise check if the partial view is a tagged
17038 -- private type.
17040 if Present (Partial_View)
17041 and then Is_Private_Type (Partial_View)
17042 and then not Is_Tagged_Type (Partial_View)
17043 then
17044 Error_Msg_NE
17045 ("cannot derive from & declared as untagged private "
17046 & "(SPARK RM 3.4(1))", N, Partial_View);
17047 end if;
17048 end;
17049 end if;
17051 -- Ada 2005 (AI-251): In case of interface derivation check that the
17052 -- parent is also an interface.
17054 if Interface_Present (Def) then
17055 if not Is_Interface (Parent_Type) then
17056 Diagnose_Interface (Indic, Parent_Type);
17058 else
17059 Parent_Node := Parent (Base_Type (Parent_Type));
17060 Iface_Def := Type_Definition (Parent_Node);
17062 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17063 -- other limited interfaces.
17065 if Limited_Present (Def) then
17066 if Limited_Present (Iface_Def) then
17067 null;
17069 elsif Protected_Present (Iface_Def) then
17070 Error_Msg_NE
17071 ("descendant of & must be declared as a protected "
17072 & "interface", N, Parent_Type);
17074 elsif Synchronized_Present (Iface_Def) then
17075 Error_Msg_NE
17076 ("descendant of & must be declared as a synchronized "
17077 & "interface", N, Parent_Type);
17079 elsif Task_Present (Iface_Def) then
17080 Error_Msg_NE
17081 ("descendant of & must be declared as a task interface",
17082 N, Parent_Type);
17084 else
17085 Error_Msg_N
17086 ("(Ada 2005) limited interface cannot inherit from "
17087 & "non-limited interface", Indic);
17088 end if;
17090 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17091 -- from non-limited or limited interfaces.
17093 elsif not Protected_Present (Def)
17094 and then not Synchronized_Present (Def)
17095 and then not Task_Present (Def)
17096 then
17097 if Limited_Present (Iface_Def) then
17098 null;
17100 elsif Protected_Present (Iface_Def) then
17101 Error_Msg_NE
17102 ("descendant of & must be declared as a protected "
17103 & "interface", N, Parent_Type);
17105 elsif Synchronized_Present (Iface_Def) then
17106 Error_Msg_NE
17107 ("descendant of & must be declared as a synchronized "
17108 & "interface", N, Parent_Type);
17110 elsif Task_Present (Iface_Def) then
17111 Error_Msg_NE
17112 ("descendant of & must be declared as a task interface",
17113 N, Parent_Type);
17114 else
17115 null;
17116 end if;
17117 end if;
17118 end if;
17119 end if;
17121 if Is_Tagged_Type (Parent_Type)
17122 and then Is_Concurrent_Type (Parent_Type)
17123 and then not Is_Interface (Parent_Type)
17124 then
17125 Error_Msg_N
17126 ("parent type of a record extension cannot be a synchronized "
17127 & "tagged type (RM 3.9.1 (3/1))", N);
17128 Set_Etype (T, Any_Type);
17129 return;
17130 end if;
17132 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17133 -- interfaces
17135 if Is_Tagged_Type (Parent_Type)
17136 and then Is_Non_Empty_List (Interface_List (Def))
17137 then
17138 declare
17139 Intf : Node_Id;
17140 T : Entity_Id;
17142 begin
17143 Intf := First (Interface_List (Def));
17144 while Present (Intf) loop
17145 T := Find_Type_Of_Subtype_Indic (Intf);
17147 if not Is_Interface (T) then
17148 Diagnose_Interface (Intf, T);
17150 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17151 -- a limited type from having a nonlimited progenitor.
17153 elsif (Limited_Present (Def)
17154 or else (not Is_Interface (Parent_Type)
17155 and then Is_Limited_Type (Parent_Type)))
17156 and then not Is_Limited_Interface (T)
17157 then
17158 Error_Msg_NE
17159 ("progenitor interface& of limited type must be limited",
17160 N, T);
17161 end if;
17163 Next (Intf);
17164 end loop;
17165 end;
17167 -- Check consistency of any nonoverridable aspects that are
17168 -- inherited from multiple sources.
17170 Check_Inherited_Nonoverridable_Aspects
17171 (Inheritor => T,
17172 Interface_List => Interface_List (Def),
17173 Parent_Type => Parent_Type);
17174 end if;
17176 if Parent_Type = Any_Type
17177 or else Etype (Parent_Type) = Any_Type
17178 or else (Is_Class_Wide_Type (Parent_Type)
17179 and then Etype (Parent_Type) = T)
17180 then
17181 -- If Parent_Type is undefined or illegal, make new type into a
17182 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17183 -- errors. If this is a self-definition, emit error now.
17185 if T = Parent_Type or else T = Etype (Parent_Type) then
17186 Error_Msg_N ("type cannot be used in its own definition", Indic);
17187 end if;
17189 Mutate_Ekind (T, Ekind (Parent_Type));
17190 Set_Etype (T, Any_Type);
17191 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17193 if Is_Tagged_Type (T)
17194 and then Is_Record_Type (T)
17195 then
17196 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17197 end if;
17199 return;
17200 end if;
17202 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17203 -- an interface is special because the list of interfaces in the full
17204 -- view can be given in any order. For example:
17206 -- type A is interface;
17207 -- type B is interface and A;
17208 -- type D is new B with private;
17209 -- private
17210 -- type D is new A and B with null record; -- 1 --
17212 -- In this case we perform the following transformation of -1-:
17214 -- type D is new B and A with null record;
17216 -- If the parent of the full-view covers the parent of the partial-view
17217 -- we have two possible cases:
17219 -- 1) They have the same parent
17220 -- 2) The parent of the full-view implements some further interfaces
17222 -- In both cases we do not need to perform the transformation. In the
17223 -- first case the source program is correct and the transformation is
17224 -- not needed; in the second case the source program does not fulfill
17225 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17226 -- later.
17228 -- This transformation not only simplifies the rest of the analysis of
17229 -- this type declaration but also simplifies the correct generation of
17230 -- the object layout to the expander.
17232 if In_Private_Part (Current_Scope)
17233 and then Is_Interface (Parent_Type)
17234 then
17235 declare
17236 Iface : Node_Id;
17237 Partial_View : Entity_Id;
17238 Partial_View_Parent : Entity_Id;
17239 New_Iface : Node_Id;
17241 begin
17242 -- Look for the associated private type declaration
17244 Partial_View := Incomplete_Or_Partial_View (T);
17246 -- If the partial view was not found then the source code has
17247 -- errors and the transformation is not needed.
17249 if Present (Partial_View) then
17250 Partial_View_Parent := Etype (Partial_View);
17252 -- If the parent of the full-view covers the parent of the
17253 -- partial-view we have nothing else to do.
17255 if Interface_Present_In_Ancestor
17256 (Parent_Type, Partial_View_Parent)
17257 then
17258 null;
17260 -- Traverse the list of interfaces of the full-view to look
17261 -- for the parent of the partial-view and perform the tree
17262 -- transformation.
17264 else
17265 Iface := First (Interface_List (Def));
17266 while Present (Iface) loop
17267 if Etype (Iface) = Etype (Partial_View) then
17268 Rewrite (Subtype_Indication (Def),
17269 New_Copy (Subtype_Indication
17270 (Parent (Partial_View))));
17272 New_Iface :=
17273 Make_Identifier (Sloc (N), Chars (Parent_Type));
17274 Append (New_Iface, Interface_List (Def));
17276 -- Analyze the transformed code
17278 Derived_Type_Declaration (T, N, Is_Completion);
17279 return;
17280 end if;
17282 Next (Iface);
17283 end loop;
17284 end if;
17285 end if;
17286 end;
17287 end if;
17289 -- Only composite types other than array types are allowed to have
17290 -- discriminants.
17292 if Present (Discriminant_Specifications (N)) then
17293 if (Is_Elementary_Type (Parent_Type)
17294 or else
17295 Is_Array_Type (Parent_Type))
17296 and then not Error_Posted (N)
17297 then
17298 Error_Msg_N
17299 ("elementary or array type cannot have discriminants",
17300 Defining_Identifier (First (Discriminant_Specifications (N))));
17302 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17303 -- only if we are not already processing a malformed syntax tree.
17305 if Is_Type (T) then
17306 Set_Has_Discriminants (T, False);
17307 end if;
17308 end if;
17309 end if;
17311 -- In Ada 83, a derived type defined in a package specification cannot
17312 -- be used for further derivation until the end of its visible part.
17313 -- Note that derivation in the private part of the package is allowed.
17315 if Ada_Version = Ada_83
17316 and then Is_Derived_Type (Parent_Type)
17317 and then In_Visible_Part (Scope (Parent_Type))
17318 then
17319 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17320 Error_Msg_N
17321 ("(Ada 83) premature use of type for derivation", Indic);
17322 end if;
17323 end if;
17325 -- Check for early use of incomplete or private type
17327 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17328 Error_Msg_N ("premature derivation of incomplete type", Indic);
17329 return;
17331 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17332 and then not Comes_From_Generic (Parent_Type))
17333 or else Has_Private_Component (Parent_Type)
17334 then
17335 -- The ancestor type of a formal type can be incomplete, in which
17336 -- case only the operations of the partial view are available in the
17337 -- generic. Subsequent checks may be required when the full view is
17338 -- analyzed to verify that a derivation from a tagged type has an
17339 -- extension.
17341 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17342 null;
17344 elsif No (Underlying_Type (Parent_Type))
17345 or else Has_Private_Component (Parent_Type)
17346 then
17347 Error_Msg_N
17348 ("premature derivation of derived or private type", Indic);
17350 -- Flag the type itself as being in error, this prevents some
17351 -- nasty problems with subsequent uses of the malformed type.
17353 Set_Error_Posted (T);
17355 -- Check that within the immediate scope of an untagged partial
17356 -- view it's illegal to derive from the partial view if the
17357 -- full view is tagged. (7.3(7))
17359 -- We verify that the Parent_Type is a partial view by checking
17360 -- that it is not a Full_Type_Declaration (i.e. a private type or
17361 -- private extension declaration), to distinguish a partial view
17362 -- from a derivation from a private type which also appears as
17363 -- E_Private_Type. If the parent base type is not declared in an
17364 -- enclosing scope there is no need to check.
17366 elsif Present (Full_View (Parent_Type))
17367 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17368 and then not Is_Tagged_Type (Parent_Type)
17369 and then Is_Tagged_Type (Full_View (Parent_Type))
17370 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17371 then
17372 Error_Msg_N
17373 ("premature derivation from type with tagged full view",
17374 Indic);
17375 end if;
17376 end if;
17378 -- Check that form of derivation is appropriate
17380 Taggd := Is_Tagged_Type (Parent_Type);
17382 -- Set the parent type to the class-wide type's specific type in this
17383 -- case to prevent cascading errors
17385 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17386 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17387 Set_Etype (T, Etype (Parent_Type));
17388 return;
17389 end if;
17391 if Present (Extension) and then not Taggd then
17392 Error_Msg_N
17393 ("type derived from untagged type cannot have extension", Indic);
17395 elsif No (Extension) and then Taggd then
17397 -- If this declaration is within a private part (or body) of a
17398 -- generic instantiation then the derivation is allowed (the parent
17399 -- type can only appear tagged in this case if it's a generic actual
17400 -- type, since it would otherwise have been rejected in the analysis
17401 -- of the generic template).
17403 if not Is_Generic_Actual_Type (Parent_Type)
17404 or else In_Visible_Part (Scope (Parent_Type))
17405 then
17406 if Is_Class_Wide_Type (Parent_Type) then
17407 Error_Msg_N
17408 ("parent type must not be a class-wide type", Indic);
17410 -- Use specific type to prevent cascaded errors.
17412 Parent_Type := Etype (Parent_Type);
17414 else
17415 Error_Msg_N
17416 ("type derived from tagged type must have extension", Indic);
17417 end if;
17418 end if;
17419 end if;
17421 -- AI-443: Synchronized formal derived types require a private
17422 -- extension. There is no point in checking the ancestor type or
17423 -- the progenitors since the construct is wrong to begin with.
17425 if Ada_Version >= Ada_2005
17426 and then Is_Generic_Type (T)
17427 and then Present (Original_Node (N))
17428 then
17429 declare
17430 Decl : constant Node_Id := Original_Node (N);
17432 begin
17433 if Nkind (Decl) = N_Formal_Type_Declaration
17434 and then Nkind (Formal_Type_Definition (Decl)) =
17435 N_Formal_Derived_Type_Definition
17436 and then Synchronized_Present (Formal_Type_Definition (Decl))
17437 and then No (Extension)
17439 -- Avoid emitting a duplicate error message
17441 and then not Error_Posted (Indic)
17442 then
17443 Error_Msg_N
17444 ("synchronized derived type must have extension", N);
17445 end if;
17446 end;
17447 end if;
17449 if Null_Exclusion_Present (Def)
17450 and then not Is_Access_Type (Parent_Type)
17451 then
17452 Error_Msg_N ("null exclusion can only apply to an access type", N);
17453 end if;
17455 Check_Wide_Character_Restriction (Parent_Type, Indic);
17457 -- Avoid deriving parent primitives of underlying record views
17459 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17460 Derive_Subps => not Is_Underlying_Record_View (T));
17462 -- AI-419: The parent type of an explicitly limited derived type must
17463 -- be a limited type or a limited interface.
17465 if Limited_Present (Def) then
17466 Set_Is_Limited_Record (T);
17468 if Is_Interface (T) then
17469 Set_Is_Limited_Interface (T);
17470 end if;
17472 if not Is_Limited_Type (Parent_Type)
17473 and then
17474 (not Is_Interface (Parent_Type)
17475 or else not Is_Limited_Interface (Parent_Type))
17476 then
17477 -- AI05-0096: a derivation in the private part of an instance is
17478 -- legal if the generic formal is untagged limited, and the actual
17479 -- is non-limited.
17481 if Is_Generic_Actual_Type (Parent_Type)
17482 and then In_Private_Part (Current_Scope)
17483 and then
17484 not Is_Tagged_Type
17485 (Generic_Parent_Type (Parent (Parent_Type)))
17486 then
17487 null;
17489 else
17490 Error_Msg_NE
17491 ("parent type& of limited type must be limited",
17492 N, Parent_Type);
17493 end if;
17494 end if;
17495 end if;
17496 end Derived_Type_Declaration;
17498 ------------------------
17499 -- Diagnose_Interface --
17500 ------------------------
17502 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17503 begin
17504 if not Is_Interface (E) and then E /= Any_Type then
17505 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17506 end if;
17507 end Diagnose_Interface;
17509 ----------------------------------
17510 -- Enumeration_Type_Declaration --
17511 ----------------------------------
17513 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17514 Ev : Uint;
17515 L : Node_Id;
17516 R_Node : Node_Id;
17517 B_Node : Node_Id;
17519 begin
17520 -- Create identifier node representing lower bound
17522 B_Node := New_Node (N_Identifier, Sloc (Def));
17523 L := First (Literals (Def));
17524 Set_Chars (B_Node, Chars (L));
17525 Set_Entity (B_Node, L);
17526 Set_Etype (B_Node, T);
17527 Set_Is_Static_Expression (B_Node, True);
17529 R_Node := New_Node (N_Range, Sloc (Def));
17530 Set_Low_Bound (R_Node, B_Node);
17532 Mutate_Ekind (T, E_Enumeration_Type);
17533 Set_First_Literal (T, L);
17534 Set_Etype (T, T);
17535 Set_Is_Constrained (T);
17537 Ev := Uint_0;
17539 -- Loop through literals of enumeration type setting pos and rep values
17540 -- except that if the Ekind is already set, then it means the literal
17541 -- was already constructed (case of a derived type declaration and we
17542 -- should not disturb the Pos and Rep values.
17544 while Present (L) loop
17545 if Ekind (L) /= E_Enumeration_Literal then
17546 Mutate_Ekind (L, E_Enumeration_Literal);
17547 Set_Enumeration_Pos (L, Ev);
17548 Set_Enumeration_Rep (L, Ev);
17549 Set_Is_Known_Valid (L, True);
17550 end if;
17552 Set_Etype (L, T);
17553 New_Overloaded_Entity (L);
17554 Generate_Definition (L);
17555 Set_Convention (L, Convention_Intrinsic);
17557 -- Case of character literal
17559 if Nkind (L) = N_Defining_Character_Literal then
17560 Set_Is_Character_Type (T, True);
17562 -- Check violation of No_Wide_Characters
17564 if Restriction_Check_Required (No_Wide_Characters) then
17565 Get_Name_String (Chars (L));
17567 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17568 Check_Restriction (No_Wide_Characters, L);
17569 end if;
17570 end if;
17571 end if;
17573 Ev := Ev + 1;
17574 Next (L);
17575 end loop;
17577 -- Now create a node representing upper bound
17579 B_Node := New_Node (N_Identifier, Sloc (Def));
17580 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17581 Set_Entity (B_Node, Last (Literals (Def)));
17582 Set_Etype (B_Node, T);
17583 Set_Is_Static_Expression (B_Node, True);
17585 Set_High_Bound (R_Node, B_Node);
17587 -- Initialize various fields of the type. Some of this information
17588 -- may be overwritten later through rep. clauses.
17590 Set_Scalar_Range (T, R_Node);
17591 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17592 Set_Enum_Esize (T);
17593 Set_Enum_Pos_To_Rep (T, Empty);
17595 -- Set Discard_Names if configuration pragma set, or if there is
17596 -- a parameterless pragma in the current declarative region
17598 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17599 Set_Discard_Names (T);
17600 end if;
17602 -- Process end label if there is one
17604 if Present (Def) then
17605 Process_End_Label (Def, 'e', T);
17606 end if;
17607 end Enumeration_Type_Declaration;
17609 ---------------------------------
17610 -- Expand_To_Stored_Constraint --
17611 ---------------------------------
17613 function Expand_To_Stored_Constraint
17614 (Typ : Entity_Id;
17615 Constraint : Elist_Id) return Elist_Id
17617 Explicitly_Discriminated_Type : Entity_Id;
17618 Expansion : Elist_Id;
17619 Discriminant : Entity_Id;
17621 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17622 -- Find the nearest type that actually specifies discriminants
17624 ---------------------------------
17625 -- Type_With_Explicit_Discrims --
17626 ---------------------------------
17628 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17629 Typ : constant E := Base_Type (Id);
17631 begin
17632 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17633 if Present (Full_View (Typ)) then
17634 return Type_With_Explicit_Discrims (Full_View (Typ));
17635 end if;
17637 else
17638 if Has_Discriminants (Typ) then
17639 return Typ;
17640 end if;
17641 end if;
17643 if Etype (Typ) = Typ then
17644 return Empty;
17645 elsif Has_Discriminants (Typ) then
17646 return Typ;
17647 else
17648 return Type_With_Explicit_Discrims (Etype (Typ));
17649 end if;
17651 end Type_With_Explicit_Discrims;
17653 -- Start of processing for Expand_To_Stored_Constraint
17655 begin
17656 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17657 return No_Elist;
17658 end if;
17660 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17662 if No (Explicitly_Discriminated_Type) then
17663 return No_Elist;
17664 end if;
17666 Expansion := New_Elmt_List;
17668 Discriminant :=
17669 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17670 while Present (Discriminant) loop
17671 Append_Elmt
17672 (Get_Discriminant_Value
17673 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17674 To => Expansion);
17675 Next_Stored_Discriminant (Discriminant);
17676 end loop;
17678 return Expansion;
17679 end Expand_To_Stored_Constraint;
17681 ---------------------------
17682 -- Find_Hidden_Interface --
17683 ---------------------------
17685 function Find_Hidden_Interface
17686 (Src : Elist_Id;
17687 Dest : Elist_Id) return Entity_Id
17689 Iface : Entity_Id;
17690 Iface_Elmt : Elmt_Id;
17692 begin
17693 if Present (Src) and then Present (Dest) then
17694 Iface_Elmt := First_Elmt (Src);
17695 while Present (Iface_Elmt) loop
17696 Iface := Node (Iface_Elmt);
17698 if Is_Interface (Iface)
17699 and then not Contain_Interface (Iface, Dest)
17700 then
17701 return Iface;
17702 end if;
17704 Next_Elmt (Iface_Elmt);
17705 end loop;
17706 end if;
17708 return Empty;
17709 end Find_Hidden_Interface;
17711 --------------------
17712 -- Find_Type_Name --
17713 --------------------
17715 function Find_Type_Name (N : Node_Id) return Entity_Id is
17716 Id : constant Entity_Id := Defining_Identifier (N);
17717 New_Id : Entity_Id;
17718 Prev : Entity_Id;
17719 Prev_Par : Node_Id;
17721 procedure Check_Duplicate_Aspects;
17722 -- Check that aspects specified in a completion have not been specified
17723 -- already in the partial view.
17725 procedure Tag_Mismatch;
17726 -- Diagnose a tagged partial view whose full view is untagged. We post
17727 -- the message on the full view, with a reference to the previous
17728 -- partial view. The partial view can be private or incomplete, and
17729 -- these are handled in a different manner, so we determine the position
17730 -- of the error message from the respective slocs of both.
17732 -----------------------------
17733 -- Check_Duplicate_Aspects --
17734 -----------------------------
17736 procedure Check_Duplicate_Aspects is
17737 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17738 -- Return the corresponding aspect of the partial view which matches
17739 -- the aspect id of Asp. Return Empty is no such aspect exists.
17741 -----------------------------
17742 -- Get_Partial_View_Aspect --
17743 -----------------------------
17745 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17746 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17747 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17748 Prev_Asp : Node_Id;
17750 begin
17751 if Present (Prev_Asps) then
17752 Prev_Asp := First (Prev_Asps);
17753 while Present (Prev_Asp) loop
17754 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17755 return Prev_Asp;
17756 end if;
17758 Next (Prev_Asp);
17759 end loop;
17760 end if;
17762 return Empty;
17763 end Get_Partial_View_Aspect;
17765 -- Local variables
17767 Full_Asps : constant List_Id := Aspect_Specifications (N);
17768 Full_Asp : Node_Id;
17769 Part_Asp : Node_Id;
17771 -- Start of processing for Check_Duplicate_Aspects
17773 begin
17774 if Present (Full_Asps) then
17775 Full_Asp := First (Full_Asps);
17776 while Present (Full_Asp) loop
17777 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17779 -- An aspect and its class-wide counterpart are two distinct
17780 -- aspects and may apply to both views of an entity.
17782 if Present (Part_Asp)
17783 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17784 then
17785 Error_Msg_N
17786 ("aspect already specified in private declaration",
17787 Full_Asp);
17789 Remove (Full_Asp);
17790 return;
17791 end if;
17793 if Has_Discriminants (Prev)
17794 and then not Has_Unknown_Discriminants (Prev)
17795 and then Get_Aspect_Id (Full_Asp) =
17796 Aspect_Implicit_Dereference
17797 then
17798 Error_Msg_N
17799 ("cannot specify aspect if partial view has known "
17800 & "discriminants", Full_Asp);
17801 end if;
17803 Next (Full_Asp);
17804 end loop;
17805 end if;
17806 end Check_Duplicate_Aspects;
17808 ------------------
17809 -- Tag_Mismatch --
17810 ------------------
17812 procedure Tag_Mismatch is
17813 begin
17814 if Sloc (Prev) < Sloc (Id) then
17815 if Ada_Version >= Ada_2012
17816 and then Nkind (N) = N_Private_Type_Declaration
17817 then
17818 Error_Msg_NE
17819 ("declaration of private } must be a tagged type", Id, Prev);
17820 else
17821 Error_Msg_NE
17822 ("full declaration of } must be a tagged type", Id, Prev);
17823 end if;
17825 else
17826 if Ada_Version >= Ada_2012
17827 and then Nkind (N) = N_Private_Type_Declaration
17828 then
17829 Error_Msg_NE
17830 ("declaration of private } must be a tagged type", Prev, Id);
17831 else
17832 Error_Msg_NE
17833 ("full declaration of } must be a tagged type", Prev, Id);
17834 end if;
17835 end if;
17836 end Tag_Mismatch;
17838 -- Start of processing for Find_Type_Name
17840 begin
17841 -- Find incomplete declaration, if one was given
17843 Prev := Current_Entity_In_Scope (Id);
17845 -- New type declaration
17847 if No (Prev) then
17848 Enter_Name (Id);
17849 return Id;
17851 -- Previous declaration exists
17853 else
17854 Prev_Par := Parent (Prev);
17856 -- Error if not incomplete/private case except if previous
17857 -- declaration is implicit, etc. Enter_Name will emit error if
17858 -- appropriate.
17860 if not Is_Incomplete_Or_Private_Type (Prev) then
17861 Enter_Name (Id);
17862 New_Id := Id;
17864 -- Check invalid completion of private or incomplete type
17866 elsif Nkind (N) not in N_Full_Type_Declaration
17867 | N_Task_Type_Declaration
17868 | N_Protected_Type_Declaration
17869 and then
17870 (Ada_Version < Ada_2012
17871 or else not Is_Incomplete_Type (Prev)
17872 or else Nkind (N) not in N_Private_Type_Declaration
17873 | N_Private_Extension_Declaration)
17874 then
17875 -- Completion must be a full type declarations (RM 7.3(4))
17877 Error_Msg_Sloc := Sloc (Prev);
17878 Error_Msg_NE ("invalid completion of }", Id, Prev);
17880 -- Set scope of Id to avoid cascaded errors. Entity is never
17881 -- examined again, except when saving globals in generics.
17883 Set_Scope (Id, Current_Scope);
17884 New_Id := Id;
17886 -- If this is a repeated incomplete declaration, no further
17887 -- checks are possible.
17889 if Nkind (N) = N_Incomplete_Type_Declaration then
17890 return Prev;
17891 end if;
17893 -- Case of full declaration of incomplete type
17895 elsif Ekind (Prev) = E_Incomplete_Type
17896 and then (Ada_Version < Ada_2012
17897 or else No (Full_View (Prev))
17898 or else not Is_Private_Type (Full_View (Prev)))
17899 then
17900 -- Indicate that the incomplete declaration has a matching full
17901 -- declaration. The defining occurrence of the incomplete
17902 -- declaration remains the visible one, and the procedure
17903 -- Get_Full_View dereferences it whenever the type is used.
17905 if Present (Full_View (Prev)) then
17906 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17907 end if;
17909 Set_Full_View (Prev, Id);
17910 Append_Entity (Id, Current_Scope);
17911 Set_Is_Public (Id, Is_Public (Prev));
17912 Set_Is_Internal (Id);
17913 New_Id := Prev;
17915 -- If the incomplete view is tagged, a class_wide type has been
17916 -- created already. Use it for the private type as well, in order
17917 -- to prevent multiple incompatible class-wide types that may be
17918 -- created for self-referential anonymous access components.
17920 if Is_Tagged_Type (Prev)
17921 and then Present (Class_Wide_Type (Prev))
17922 then
17923 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
17924 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17926 -- Type of the class-wide type is the current Id. Previously
17927 -- this was not done for private declarations because of order-
17928 -- of-elaboration issues in the back end, but gigi now handles
17929 -- this properly.
17931 Set_Etype (Class_Wide_Type (Id), Id);
17932 end if;
17934 -- Case of full declaration of private type
17936 else
17937 -- If the private type was a completion of an incomplete type then
17938 -- update Prev to reference the private type
17940 if Ada_Version >= Ada_2012
17941 and then Ekind (Prev) = E_Incomplete_Type
17942 and then Present (Full_View (Prev))
17943 and then Is_Private_Type (Full_View (Prev))
17944 then
17945 Prev := Full_View (Prev);
17946 Prev_Par := Parent (Prev);
17947 end if;
17949 if Nkind (N) = N_Full_Type_Declaration
17950 and then Nkind (Type_Definition (N)) in
17951 N_Record_Definition | N_Derived_Type_Definition
17952 and then Interface_Present (Type_Definition (N))
17953 then
17954 Error_Msg_N
17955 ("completion of private type cannot be an interface", N);
17956 end if;
17958 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17959 if Etype (Prev) /= Prev then
17961 -- Prev is a private subtype or a derived type, and needs
17962 -- no completion.
17964 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17965 New_Id := Id;
17967 elsif Ekind (Prev) = E_Private_Type
17968 and then Nkind (N) in N_Task_Type_Declaration
17969 | N_Protected_Type_Declaration
17970 then
17971 Error_Msg_N
17972 ("completion of nonlimited type cannot be limited", N);
17974 elsif Ekind (Prev) = E_Record_Type_With_Private
17975 and then Nkind (N) in N_Task_Type_Declaration
17976 | N_Protected_Type_Declaration
17977 then
17978 if not Is_Limited_Record (Prev) then
17979 Error_Msg_N
17980 ("completion of nonlimited type cannot be limited", N);
17982 elsif No (Interface_List (N)) then
17983 Error_Msg_N
17984 ("completion of tagged private type must be tagged",
17986 end if;
17987 end if;
17989 -- Ada 2005 (AI-251): Private extension declaration of a task
17990 -- type or a protected type. This case arises when covering
17991 -- interface types.
17993 elsif Nkind (N) in N_Task_Type_Declaration
17994 | N_Protected_Type_Declaration
17995 then
17996 null;
17998 elsif Nkind (N) /= N_Full_Type_Declaration
17999 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18000 then
18001 Error_Msg_N
18002 ("full view of private extension must be an extension", N);
18004 elsif not (Abstract_Present (Parent (Prev)))
18005 and then Abstract_Present (Type_Definition (N))
18006 then
18007 Error_Msg_N
18008 ("full view of non-abstract extension cannot be abstract", N);
18009 end if;
18011 if not In_Private_Part (Current_Scope) then
18012 Error_Msg_N
18013 ("declaration of full view must appear in private part", N);
18014 end if;
18016 if Ada_Version >= Ada_2012 then
18017 Check_Duplicate_Aspects;
18018 end if;
18020 Copy_And_Swap (Prev, Id);
18021 Set_Has_Private_Declaration (Prev);
18022 Set_Has_Private_Declaration (Id);
18024 -- AI12-0133: Indicate whether we have a partial view with
18025 -- unknown discriminants, in which case initialization of objects
18026 -- of the type do not receive an invariant check.
18028 Set_Partial_View_Has_Unknown_Discr
18029 (Prev, Has_Unknown_Discriminants (Id));
18031 -- Preserve aspect and iterator flags that may have been set on
18032 -- the partial view.
18034 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18035 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18037 -- If no error, propagate freeze_node from private to full view.
18038 -- It may have been generated for an early operational item.
18040 if Present (Freeze_Node (Id))
18041 and then Serious_Errors_Detected = 0
18042 and then No (Full_View (Id))
18043 then
18044 Set_Freeze_Node (Prev, Freeze_Node (Id));
18045 Set_Freeze_Node (Id, Empty);
18046 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18047 end if;
18049 Set_Full_View (Id, Prev);
18050 New_Id := Prev;
18051 end if;
18053 -- Verify that full declaration conforms to partial one
18055 if Is_Incomplete_Or_Private_Type (Prev)
18056 and then Present (Discriminant_Specifications (Prev_Par))
18057 then
18058 if Present (Discriminant_Specifications (N)) then
18059 if Ekind (Prev) = E_Incomplete_Type then
18060 Check_Discriminant_Conformance (N, Prev, Prev);
18061 else
18062 Check_Discriminant_Conformance (N, Prev, Id);
18063 end if;
18065 else
18066 Error_Msg_N
18067 ("missing discriminants in full type declaration", N);
18069 -- To avoid cascaded errors on subsequent use, share the
18070 -- discriminants of the partial view.
18072 Set_Discriminant_Specifications (N,
18073 Discriminant_Specifications (Prev_Par));
18074 end if;
18075 end if;
18077 -- A prior untagged partial view can have an associated class-wide
18078 -- type due to use of the class attribute, and in this case the full
18079 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18080 -- of incomplete tagged declarations, but we check for it.
18082 if Is_Type (Prev)
18083 and then (Is_Tagged_Type (Prev)
18084 or else Present (Class_Wide_Type (Prev)))
18085 then
18086 -- Ada 2012 (AI05-0162): A private type may be the completion of
18087 -- an incomplete type.
18089 if Ada_Version >= Ada_2012
18090 and then Is_Incomplete_Type (Prev)
18091 and then Nkind (N) in N_Private_Type_Declaration
18092 | N_Private_Extension_Declaration
18093 then
18094 -- No need to check private extensions since they are tagged
18096 if Nkind (N) = N_Private_Type_Declaration
18097 and then not Tagged_Present (N)
18098 then
18099 Tag_Mismatch;
18100 end if;
18102 -- The full declaration is either a tagged type (including
18103 -- a synchronized type that implements interfaces) or a
18104 -- type extension, otherwise this is an error.
18106 elsif Nkind (N) in N_Task_Type_Declaration
18107 | N_Protected_Type_Declaration
18108 then
18109 if No (Interface_List (N)) and then not Error_Posted (N) then
18110 Tag_Mismatch;
18111 end if;
18113 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18115 -- Indicate that the previous declaration (tagged incomplete
18116 -- or private declaration) requires the same on the full one.
18118 if not Tagged_Present (Type_Definition (N)) then
18119 Tag_Mismatch;
18120 Set_Is_Tagged_Type (Id);
18121 end if;
18123 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18124 if No (Record_Extension_Part (Type_Definition (N))) then
18125 Error_Msg_NE
18126 ("full declaration of } must be a record extension",
18127 Prev, Id);
18129 -- Set some attributes to produce a usable full view
18131 Set_Is_Tagged_Type (Id);
18132 end if;
18134 else
18135 Tag_Mismatch;
18136 end if;
18137 end if;
18139 if Present (Prev)
18140 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18141 and then Present (Premature_Use (Parent (Prev)))
18142 then
18143 Error_Msg_Sloc := Sloc (N);
18144 Error_Msg_N
18145 ("\full declaration #", Premature_Use (Parent (Prev)));
18146 end if;
18148 return New_Id;
18149 end if;
18150 end Find_Type_Name;
18152 -------------------------
18153 -- Find_Type_Of_Object --
18154 -------------------------
18156 function Find_Type_Of_Object
18157 (Obj_Def : Node_Id;
18158 Related_Nod : Node_Id) return Entity_Id
18160 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18161 P : Node_Id := Parent (Obj_Def);
18162 T : Entity_Id;
18163 Nam : Name_Id;
18165 begin
18166 -- If the parent is a component_definition node we climb to the
18167 -- component_declaration node
18169 if Nkind (P) = N_Component_Definition then
18170 P := Parent (P);
18171 end if;
18173 -- Case of an anonymous array subtype
18175 if Def_Kind in N_Array_Type_Definition then
18176 T := Empty;
18177 Array_Type_Declaration (T, Obj_Def);
18179 -- Create an explicit subtype whenever possible
18181 elsif Nkind (P) /= N_Component_Declaration
18182 and then Def_Kind = N_Subtype_Indication
18183 then
18184 -- Base name of subtype on object name, which will be unique in
18185 -- the current scope.
18187 -- If this is a duplicate declaration, return base type, to avoid
18188 -- generating duplicate anonymous types.
18190 if Error_Posted (P) then
18191 Analyze (Subtype_Mark (Obj_Def));
18192 return Entity (Subtype_Mark (Obj_Def));
18193 end if;
18195 Nam :=
18196 New_External_Name
18197 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18199 T := Make_Defining_Identifier (Sloc (P), Nam);
18201 -- If In_Spec_Expression, for example within a pre/postcondition,
18202 -- provide enough information for use of the subtype without
18203 -- depending on full analysis and freezing, which will happen when
18204 -- building the correspondiing subprogram.
18206 if In_Spec_Expression then
18207 Analyze (Subtype_Mark (Obj_Def));
18209 declare
18210 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18211 Decl : constant Node_Id :=
18212 Make_Subtype_Declaration (Sloc (P),
18213 Defining_Identifier => T,
18214 Subtype_Indication => Relocate_Node (Obj_Def));
18215 begin
18216 Set_Etype (T, Base_T);
18217 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18218 Set_Parent (T, Obj_Def);
18220 if Ekind (T) = E_Array_Subtype then
18221 Set_First_Index (T, First_Index (Base_T));
18222 Set_Is_Constrained (T);
18224 elsif Ekind (T) = E_Record_Subtype then
18225 Set_First_Entity (T, First_Entity (Base_T));
18226 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18227 Set_Is_Constrained (T);
18228 end if;
18230 Insert_Before (Related_Nod, Decl);
18231 end;
18233 return T;
18234 end if;
18236 -- When generating code, insert subtype declaration ahead of
18237 -- declaration that generated it.
18239 Insert_Action (Obj_Def,
18240 Make_Subtype_Declaration (Sloc (P),
18241 Defining_Identifier => T,
18242 Subtype_Indication => Relocate_Node (Obj_Def)));
18244 -- This subtype may need freezing, and this will not be done
18245 -- automatically if the object declaration is not in declarative
18246 -- part. Since this is an object declaration, the type cannot always
18247 -- be frozen here. Deferred constants do not freeze their type
18248 -- (which often enough will be private).
18250 if Nkind (P) = N_Object_Declaration
18251 and then Constant_Present (P)
18252 and then No (Expression (P))
18253 then
18254 null;
18256 -- Here we freeze the base type of object type to catch premature use
18257 -- of discriminated private type without a full view.
18259 else
18260 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18261 end if;
18263 -- Ada 2005 AI-406: the object definition in an object declaration
18264 -- can be an access definition.
18266 elsif Def_Kind = N_Access_Definition then
18267 T := Access_Definition (Related_Nod, Obj_Def);
18269 Set_Is_Local_Anonymous_Access
18270 (T, Ada_Version < Ada_2012
18271 or else Nkind (P) /= N_Object_Declaration
18272 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18274 -- Otherwise, the object definition is just a subtype_mark
18276 else
18277 T := Process_Subtype (Obj_Def, Related_Nod);
18278 end if;
18280 return T;
18281 end Find_Type_Of_Object;
18283 --------------------------------
18284 -- Find_Type_Of_Subtype_Indic --
18285 --------------------------------
18287 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18288 Typ : Entity_Id;
18290 begin
18291 -- Case of subtype mark with a constraint
18293 if Nkind (S) = N_Subtype_Indication then
18294 Find_Type (Subtype_Mark (S));
18295 Typ := Entity (Subtype_Mark (S));
18297 if not
18298 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18299 then
18300 Error_Msg_N
18301 ("incorrect constraint for this kind of type", Constraint (S));
18302 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18303 end if;
18305 -- Otherwise we have a subtype mark without a constraint
18307 elsif Error_Posted (S) then
18308 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18309 return Any_Type;
18311 else
18312 Find_Type (S);
18313 Typ := Entity (S);
18314 end if;
18316 return Typ;
18317 end Find_Type_Of_Subtype_Indic;
18319 -------------------------------------
18320 -- Floating_Point_Type_Declaration --
18321 -------------------------------------
18323 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18324 Digs : constant Node_Id := Digits_Expression (Def);
18325 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18326 Digs_Val : Uint;
18327 Base_Typ : Entity_Id;
18328 Implicit_Base : Entity_Id;
18330 function Can_Derive_From (E : Entity_Id) return Boolean;
18331 -- Find if given digits value, and possibly a specified range, allows
18332 -- derivation from specified type
18334 procedure Convert_Bound (B : Node_Id);
18335 -- If specified, the bounds must be static but may be of different
18336 -- types. They must be converted into machine numbers of the base type,
18337 -- in accordance with RM 4.9(38).
18339 function Find_Base_Type return Entity_Id;
18340 -- Find a predefined base type that Def can derive from, or generate
18341 -- an error and substitute Long_Long_Float if none exists.
18343 ---------------------
18344 -- Can_Derive_From --
18345 ---------------------
18347 function Can_Derive_From (E : Entity_Id) return Boolean is
18348 Spec : constant Entity_Id := Real_Range_Specification (Def);
18350 begin
18351 -- Check specified "digits" constraint
18353 if Digs_Val > Digits_Value (E) then
18354 return False;
18355 end if;
18357 -- Check for matching range, if specified
18359 if Present (Spec) then
18360 if Expr_Value_R (Type_Low_Bound (E)) >
18361 Expr_Value_R (Low_Bound (Spec))
18362 then
18363 return False;
18364 end if;
18366 if Expr_Value_R (Type_High_Bound (E)) <
18367 Expr_Value_R (High_Bound (Spec))
18368 then
18369 return False;
18370 end if;
18371 end if;
18373 return True;
18374 end Can_Derive_From;
18376 -------------------
18377 -- Convert_Bound --
18378 --------------------
18380 procedure Convert_Bound (B : Node_Id) is
18381 begin
18382 -- If the bound is not a literal it can only be static if it is
18383 -- a static constant, possibly of a specified type.
18385 if Is_Entity_Name (B)
18386 and then Ekind (Entity (B)) = E_Constant
18387 then
18388 Rewrite (B, Constant_Value (Entity (B)));
18389 end if;
18391 if Nkind (B) = N_Real_Literal then
18392 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18393 Set_Is_Machine_Number (B);
18394 Set_Etype (B, Base_Typ);
18395 end if;
18396 end Convert_Bound;
18398 --------------------
18399 -- Find_Base_Type --
18400 --------------------
18402 function Find_Base_Type return Entity_Id is
18403 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18405 begin
18406 -- Iterate over the predefined types in order, returning the first
18407 -- one that Def can derive from.
18409 while Present (Choice) loop
18410 if Can_Derive_From (Node (Choice)) then
18411 return Node (Choice);
18412 end if;
18414 Next_Elmt (Choice);
18415 end loop;
18417 -- If we can't derive from any existing type, use Long_Long_Float
18418 -- and give appropriate message explaining the problem.
18420 if Digs_Val > Max_Digs_Val then
18421 -- It might be the case that there is a type with the requested
18422 -- range, just not the combination of digits and range.
18424 Error_Msg_N
18425 ("no predefined type has requested range and precision",
18426 Real_Range_Specification (Def));
18428 else
18429 Error_Msg_N
18430 ("range too large for any predefined type",
18431 Real_Range_Specification (Def));
18432 end if;
18434 return Standard_Long_Long_Float;
18435 end Find_Base_Type;
18437 -- Start of processing for Floating_Point_Type_Declaration
18439 begin
18440 Check_Restriction (No_Floating_Point, Def);
18442 -- Create an implicit base type
18444 Implicit_Base :=
18445 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18447 -- Analyze and verify digits value
18449 Analyze_And_Resolve (Digs, Any_Integer);
18450 Check_Digits_Expression (Digs);
18451 Digs_Val := Expr_Value (Digs);
18453 -- Process possible range spec and find correct type to derive from
18455 Process_Real_Range_Specification (Def);
18457 -- Check that requested number of digits is not too high.
18459 if Digs_Val > Max_Digs_Val then
18461 -- The check for Max_Base_Digits may be somewhat expensive, as it
18462 -- requires reading System, so only do it when necessary.
18464 declare
18465 Max_Base_Digits : constant Uint :=
18466 Expr_Value
18467 (Expression
18468 (Parent (RTE (RE_Max_Base_Digits))));
18470 begin
18471 if Digs_Val > Max_Base_Digits then
18472 Error_Msg_Uint_1 := Max_Base_Digits;
18473 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18475 elsif No (Real_Range_Specification (Def)) then
18476 Error_Msg_Uint_1 := Max_Digs_Val;
18477 Error_Msg_N ("types with more than ^ digits need range spec "
18478 & "(RM 3.5.7(6))", Digs);
18479 end if;
18480 end;
18481 end if;
18483 -- Find a suitable type to derive from or complain and use a substitute
18485 Base_Typ := Find_Base_Type;
18487 -- If there are bounds given in the declaration use them as the bounds
18488 -- of the type, otherwise use the bounds of the predefined base type
18489 -- that was chosen based on the Digits value.
18491 if Present (Real_Range_Specification (Def)) then
18492 Set_Scalar_Range (T, Real_Range_Specification (Def));
18493 Set_Is_Constrained (T);
18495 Convert_Bound (Type_Low_Bound (T));
18496 Convert_Bound (Type_High_Bound (T));
18498 else
18499 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18500 end if;
18502 -- Complete definition of implicit base and declared first subtype. The
18503 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18504 -- are not clobbered when the floating point type acts as a full view of
18505 -- a private type.
18507 Set_Etype (Implicit_Base, Base_Typ);
18508 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18509 Set_Size_Info (Implicit_Base, Base_Typ);
18510 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18511 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18512 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18513 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18515 Mutate_Ekind (T, E_Floating_Point_Subtype);
18516 Set_Etype (T, Implicit_Base);
18517 Set_Size_Info (T, Implicit_Base);
18518 Set_RM_Size (T, RM_Size (Implicit_Base));
18519 Inherit_Rep_Item_Chain (T, Implicit_Base);
18521 if Digs_Val >= Uint_1 then
18522 Set_Digits_Value (T, Digs_Val);
18523 else
18524 pragma Assert (Serious_Errors_Detected > 0); null;
18525 end if;
18526 end Floating_Point_Type_Declaration;
18528 ----------------------------
18529 -- Get_Discriminant_Value --
18530 ----------------------------
18532 -- This is the situation:
18534 -- There is a non-derived type
18536 -- type T0 (Dx, Dy, Dz...)
18538 -- There are zero or more levels of derivation, with each derivation
18539 -- either purely inheriting the discriminants, or defining its own.
18541 -- type Ti is new Ti-1
18542 -- or
18543 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18544 -- or
18545 -- subtype Ti is ...
18547 -- The subtype issue is avoided by the use of Original_Record_Component,
18548 -- and the fact that derived subtypes also derive the constraints.
18550 -- This chain leads back from
18552 -- Typ_For_Constraint
18554 -- Typ_For_Constraint has discriminants, and the value for each
18555 -- discriminant is given by its corresponding Elmt of Constraints.
18557 -- Discriminant is some discriminant in this hierarchy
18559 -- We need to return its value
18561 -- We do this by recursively searching each level, and looking for
18562 -- Discriminant. Once we get to the bottom, we start backing up
18563 -- returning the value for it which may in turn be a discriminant
18564 -- further up, so on the backup we continue the substitution.
18566 function Get_Discriminant_Value
18567 (Discriminant : Entity_Id;
18568 Typ_For_Constraint : Entity_Id;
18569 Constraint : Elist_Id) return Node_Id
18571 function Root_Corresponding_Discriminant
18572 (Discr : Entity_Id) return Entity_Id;
18573 -- Given a discriminant, traverse the chain of inherited discriminants
18574 -- and return the topmost discriminant.
18576 function Search_Derivation_Levels
18577 (Ti : Entity_Id;
18578 Discrim_Values : Elist_Id;
18579 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18580 -- This is the routine that performs the recursive search of levels
18581 -- as described above.
18583 -------------------------------------
18584 -- Root_Corresponding_Discriminant --
18585 -------------------------------------
18587 function Root_Corresponding_Discriminant
18588 (Discr : Entity_Id) return Entity_Id
18590 D : Entity_Id;
18592 begin
18593 D := Discr;
18594 while Present (Corresponding_Discriminant (D)) loop
18595 D := Corresponding_Discriminant (D);
18596 end loop;
18598 return D;
18599 end Root_Corresponding_Discriminant;
18601 ------------------------------
18602 -- Search_Derivation_Levels --
18603 ------------------------------
18605 function Search_Derivation_Levels
18606 (Ti : Entity_Id;
18607 Discrim_Values : Elist_Id;
18608 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18610 Assoc : Elmt_Id;
18611 Disc : Entity_Id;
18612 Result : Node_Or_Entity_Id;
18613 Result_Entity : Node_Id;
18615 begin
18616 -- If inappropriate type, return Error, this happens only in
18617 -- cascaded error situations, and we want to avoid a blow up.
18619 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18620 return Error;
18621 end if;
18623 -- Look deeper if possible. Use Stored_Constraints only for
18624 -- untagged types. For tagged types use the given constraint.
18625 -- This asymmetry needs explanation???
18627 if not Stored_Discrim_Values
18628 and then Present (Stored_Constraint (Ti))
18629 and then not Is_Tagged_Type (Ti)
18630 then
18631 Result :=
18632 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18634 else
18635 declare
18636 Td : Entity_Id := Etype (Ti);
18638 begin
18639 -- If the parent type is private, the full view may include
18640 -- renamed discriminants, and it is those stored values that
18641 -- may be needed (the partial view never has more information
18642 -- than the full view).
18644 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18645 Td := Full_View (Td);
18646 end if;
18648 if Td = Ti then
18649 Result := Discriminant;
18651 else
18652 if Present (Stored_Constraint (Ti)) then
18653 Result :=
18654 Search_Derivation_Levels
18655 (Td, Stored_Constraint (Ti), True);
18656 else
18657 Result :=
18658 Search_Derivation_Levels
18659 (Td, Discrim_Values, Stored_Discrim_Values);
18660 end if;
18661 end if;
18662 end;
18663 end if;
18665 -- Extra underlying places to search, if not found above. For
18666 -- concurrent types, the relevant discriminant appears in the
18667 -- corresponding record. For a type derived from a private type
18668 -- without discriminant, the full view inherits the discriminants
18669 -- of the full view of the parent.
18671 if Result = Discriminant then
18672 if Is_Concurrent_Type (Ti)
18673 and then Present (Corresponding_Record_Type (Ti))
18674 then
18675 Result :=
18676 Search_Derivation_Levels (
18677 Corresponding_Record_Type (Ti),
18678 Discrim_Values,
18679 Stored_Discrim_Values);
18681 elsif Is_Private_Type (Ti)
18682 and then not Has_Discriminants (Ti)
18683 and then Present (Full_View (Ti))
18684 and then Etype (Full_View (Ti)) /= Ti
18685 then
18686 Result :=
18687 Search_Derivation_Levels (
18688 Full_View (Ti),
18689 Discrim_Values,
18690 Stored_Discrim_Values);
18691 end if;
18692 end if;
18694 -- If Result is not a (reference to a) discriminant, return it,
18695 -- otherwise set Result_Entity to the discriminant.
18697 if Nkind (Result) = N_Defining_Identifier then
18698 pragma Assert (Result = Discriminant);
18699 Result_Entity := Result;
18701 else
18702 if not Denotes_Discriminant (Result) then
18703 return Result;
18704 end if;
18706 Result_Entity := Entity (Result);
18707 end if;
18709 -- See if this level of derivation actually has discriminants because
18710 -- tagged derivations can add them, hence the lower levels need not
18711 -- have any.
18713 if not Has_Discriminants (Ti) then
18714 return Result;
18715 end if;
18717 -- Scan Ti's discriminants for Result_Entity, and return its
18718 -- corresponding value, if any.
18720 Result_Entity := Original_Record_Component (Result_Entity);
18722 Assoc := First_Elmt (Discrim_Values);
18724 if Stored_Discrim_Values then
18725 Disc := First_Stored_Discriminant (Ti);
18726 else
18727 Disc := First_Discriminant (Ti);
18728 end if;
18730 while Present (Disc) loop
18732 -- If no further associations return the discriminant, value will
18733 -- be found on the second pass.
18735 if No (Assoc) then
18736 return Result;
18737 end if;
18739 if Original_Record_Component (Disc) = Result_Entity then
18740 return Node (Assoc);
18741 end if;
18743 Next_Elmt (Assoc);
18745 if Stored_Discrim_Values then
18746 Next_Stored_Discriminant (Disc);
18747 else
18748 Next_Discriminant (Disc);
18749 end if;
18750 end loop;
18752 -- Could not find it
18754 return Result;
18755 end Search_Derivation_Levels;
18757 -- Local Variables
18759 Result : Node_Or_Entity_Id;
18761 -- Start of processing for Get_Discriminant_Value
18763 begin
18764 -- ??? This routine is a gigantic mess and will be deleted. For the
18765 -- time being just test for the trivial case before calling recurse.
18767 -- We are now celebrating the 20th anniversary of this comment!
18769 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18770 declare
18771 D : Entity_Id;
18772 E : Elmt_Id;
18774 begin
18775 D := First_Discriminant (Typ_For_Constraint);
18776 E := First_Elmt (Constraint);
18777 while Present (D) loop
18778 if Chars (D) = Chars (Discriminant) then
18779 return Node (E);
18780 end if;
18782 Next_Discriminant (D);
18783 Next_Elmt (E);
18784 end loop;
18785 end;
18786 end if;
18788 Result := Search_Derivation_Levels
18789 (Typ_For_Constraint, Constraint, False);
18791 -- ??? hack to disappear when this routine is gone
18793 if Nkind (Result) = N_Defining_Identifier then
18794 declare
18795 D : Entity_Id;
18796 E : Elmt_Id;
18798 begin
18799 D := First_Discriminant (Typ_For_Constraint);
18800 E := First_Elmt (Constraint);
18801 while Present (D) loop
18802 if Root_Corresponding_Discriminant (D) = Discriminant then
18803 return Node (E);
18804 end if;
18806 Next_Discriminant (D);
18807 Next_Elmt (E);
18808 end loop;
18809 end;
18810 end if;
18812 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18813 return Result;
18814 end Get_Discriminant_Value;
18816 --------------------------
18817 -- Has_Range_Constraint --
18818 --------------------------
18820 function Has_Range_Constraint (N : Node_Id) return Boolean is
18821 C : constant Node_Id := Constraint (N);
18823 begin
18824 if Nkind (C) = N_Range_Constraint then
18825 return True;
18827 elsif Nkind (C) = N_Digits_Constraint then
18828 return
18829 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18830 or else Present (Range_Constraint (C));
18832 elsif Nkind (C) = N_Delta_Constraint then
18833 return Present (Range_Constraint (C));
18835 else
18836 return False;
18837 end if;
18838 end Has_Range_Constraint;
18840 ------------------------
18841 -- Inherit_Components --
18842 ------------------------
18844 function Inherit_Components
18845 (N : Node_Id;
18846 Parent_Base : Entity_Id;
18847 Derived_Base : Entity_Id;
18848 Is_Tagged : Boolean;
18849 Inherit_Discr : Boolean;
18850 Discs : Elist_Id) return Elist_Id
18852 Assoc_List : constant Elist_Id := New_Elmt_List;
18854 procedure Inherit_Component
18855 (Old_C : Entity_Id;
18856 Plain_Discrim : Boolean := False;
18857 Stored_Discrim : Boolean := False);
18858 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18859 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18860 -- True, Old_C is a stored discriminant. If they are both false then
18861 -- Old_C is a regular component.
18863 -----------------------
18864 -- Inherit_Component --
18865 -----------------------
18867 procedure Inherit_Component
18868 (Old_C : Entity_Id;
18869 Plain_Discrim : Boolean := False;
18870 Stored_Discrim : Boolean := False)
18872 procedure Set_Anonymous_Type (Id : Entity_Id);
18873 -- Id denotes the entity of an access discriminant or anonymous
18874 -- access component. Set the type of Id to either the same type of
18875 -- Old_C or create a new one depending on whether the parent and
18876 -- the child types are in the same scope.
18878 ------------------------
18879 -- Set_Anonymous_Type --
18880 ------------------------
18882 procedure Set_Anonymous_Type (Id : Entity_Id) is
18883 Old_Typ : constant Entity_Id := Etype (Old_C);
18885 begin
18886 if Scope (Parent_Base) = Scope (Derived_Base) then
18887 Set_Etype (Id, Old_Typ);
18889 -- The parent and the derived type are in two different scopes.
18890 -- Reuse the type of the original discriminant / component by
18891 -- copying it in order to preserve all attributes.
18893 else
18894 declare
18895 Typ : constant Entity_Id := New_Copy (Old_Typ);
18897 begin
18898 Set_Etype (Id, Typ);
18900 -- Since we do not generate component declarations for
18901 -- inherited components, associate the itype with the
18902 -- derived type.
18904 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18905 Set_Scope (Typ, Derived_Base);
18906 end;
18907 end if;
18908 end Set_Anonymous_Type;
18910 -- Local variables and constants
18912 New_C : constant Entity_Id := New_Copy (Old_C);
18914 Corr_Discrim : Entity_Id;
18915 Discrim : Entity_Id;
18917 -- Start of processing for Inherit_Component
18919 begin
18920 pragma Assert (not Is_Tagged or not Stored_Discrim);
18922 Set_Parent (New_C, Parent (Old_C));
18924 -- Regular discriminants and components must be inserted in the scope
18925 -- of the Derived_Base. Do it here.
18927 if not Stored_Discrim then
18928 Enter_Name (New_C);
18929 end if;
18931 -- For tagged types the Original_Record_Component must point to
18932 -- whatever this field was pointing to in the parent type. This has
18933 -- already been achieved by the call to New_Copy above.
18935 if not Is_Tagged then
18936 Set_Original_Record_Component (New_C, New_C);
18937 Set_Corresponding_Record_Component (New_C, Old_C);
18938 end if;
18940 -- Set the proper type of an access discriminant
18942 if Ekind (New_C) = E_Discriminant
18943 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18944 then
18945 Set_Anonymous_Type (New_C);
18946 end if;
18948 -- If we have inherited a component then see if its Etype contains
18949 -- references to Parent_Base discriminants. In this case, replace
18950 -- these references with the constraints given in Discs. We do not
18951 -- do this for the partial view of private types because this is
18952 -- not needed (only the components of the full view will be used
18953 -- for code generation) and cause problem. We also avoid this
18954 -- transformation in some error situations.
18956 if Ekind (New_C) = E_Component then
18958 -- Set the proper type of an anonymous access component
18960 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18961 Set_Anonymous_Type (New_C);
18963 elsif (Is_Private_Type (Derived_Base)
18964 and then not Is_Generic_Type (Derived_Base))
18965 or else (Is_Empty_Elmt_List (Discs)
18966 and then not Expander_Active)
18967 then
18968 Set_Etype (New_C, Etype (Old_C));
18970 else
18971 -- The current component introduces a circularity of the
18972 -- following kind:
18974 -- limited with Pack_2;
18975 -- package Pack_1 is
18976 -- type T_1 is tagged record
18977 -- Comp : access Pack_2.T_2;
18978 -- ...
18979 -- end record;
18980 -- end Pack_1;
18982 -- with Pack_1;
18983 -- package Pack_2 is
18984 -- type T_2 is new Pack_1.T_1 with ...;
18985 -- end Pack_2;
18987 Set_Etype
18988 (New_C,
18989 Constrain_Component_Type
18990 (Old_C, Derived_Base, N, Parent_Base, Discs));
18991 end if;
18992 end if;
18994 -- In derived tagged types it is illegal to reference a non
18995 -- discriminant component in the parent type. To catch this, mark
18996 -- these components with an Ekind of E_Void. This will be reset in
18997 -- Record_Type_Definition after processing the record extension of
18998 -- the derived type.
19000 -- If the declaration is a private extension, there is no further
19001 -- record extension to process, and the components retain their
19002 -- current kind, because they are visible at this point.
19004 if Is_Tagged and then Ekind (New_C) = E_Component
19005 and then Nkind (N) /= N_Private_Extension_Declaration
19006 then
19007 Mutate_Ekind (New_C, E_Void);
19008 end if;
19010 if Plain_Discrim then
19011 Set_Corresponding_Discriminant (New_C, Old_C);
19012 Build_Discriminal (New_C);
19014 -- If we are explicitly inheriting a stored discriminant it will be
19015 -- completely hidden.
19017 elsif Stored_Discrim then
19018 Set_Corresponding_Discriminant (New_C, Empty);
19019 Set_Discriminal (New_C, Empty);
19020 Set_Is_Completely_Hidden (New_C);
19022 -- Set the Original_Record_Component of each discriminant in the
19023 -- derived base to point to the corresponding stored that we just
19024 -- created.
19026 Discrim := First_Discriminant (Derived_Base);
19027 while Present (Discrim) loop
19028 Corr_Discrim := Corresponding_Discriminant (Discrim);
19030 -- Corr_Discrim could be missing in an error situation
19032 if Present (Corr_Discrim)
19033 and then Original_Record_Component (Corr_Discrim) = Old_C
19034 then
19035 Set_Original_Record_Component (Discrim, New_C);
19036 Set_Corresponding_Record_Component (Discrim, Empty);
19037 end if;
19039 Next_Discriminant (Discrim);
19040 end loop;
19042 Append_Entity (New_C, Derived_Base);
19043 end if;
19045 if not Is_Tagged then
19046 Append_Elmt (Old_C, Assoc_List);
19047 Append_Elmt (New_C, Assoc_List);
19048 end if;
19049 end Inherit_Component;
19051 -- Variables local to Inherit_Component
19053 Loc : constant Source_Ptr := Sloc (N);
19055 Parent_Discrim : Entity_Id;
19056 Stored_Discrim : Entity_Id;
19057 D : Entity_Id;
19058 Component : Entity_Id;
19060 -- Start of processing for Inherit_Components
19062 begin
19063 if not Is_Tagged then
19064 Append_Elmt (Parent_Base, Assoc_List);
19065 Append_Elmt (Derived_Base, Assoc_List);
19066 end if;
19068 -- Inherit parent discriminants if needed
19070 if Inherit_Discr then
19071 Parent_Discrim := First_Discriminant (Parent_Base);
19072 while Present (Parent_Discrim) loop
19073 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19074 Next_Discriminant (Parent_Discrim);
19075 end loop;
19076 end if;
19078 -- Create explicit stored discrims for untagged types when necessary
19080 if not Has_Unknown_Discriminants (Derived_Base)
19081 and then Has_Discriminants (Parent_Base)
19082 and then not Is_Tagged
19083 and then
19084 (not Inherit_Discr
19085 or else First_Discriminant (Parent_Base) /=
19086 First_Stored_Discriminant (Parent_Base))
19087 then
19088 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19089 while Present (Stored_Discrim) loop
19090 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19091 Next_Stored_Discriminant (Stored_Discrim);
19092 end loop;
19093 end if;
19095 -- See if we can apply the second transformation for derived types, as
19096 -- explained in point 6. in the comments above Build_Derived_Record_Type
19097 -- This is achieved by appending Derived_Base discriminants into Discs,
19098 -- which has the side effect of returning a non empty Discs list to the
19099 -- caller of Inherit_Components, which is what we want. This must be
19100 -- done for private derived types if there are explicit stored
19101 -- discriminants, to ensure that we can retrieve the values of the
19102 -- constraints provided in the ancestors.
19104 if Inherit_Discr
19105 and then Is_Empty_Elmt_List (Discs)
19106 and then Present (First_Discriminant (Derived_Base))
19107 and then
19108 (not Is_Private_Type (Derived_Base)
19109 or else Is_Completely_Hidden
19110 (First_Stored_Discriminant (Derived_Base))
19111 or else Is_Generic_Type (Derived_Base))
19112 then
19113 D := First_Discriminant (Derived_Base);
19114 while Present (D) loop
19115 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19116 Next_Discriminant (D);
19117 end loop;
19118 end if;
19120 -- Finally, inherit non-discriminant components unless they are not
19121 -- visible because defined or inherited from the full view of the
19122 -- parent. Don't inherit the _parent field of the parent type.
19124 Component := First_Entity (Parent_Base);
19125 while Present (Component) loop
19127 -- Ada 2005 (AI-251): Do not inherit components associated with
19128 -- secondary tags of the parent.
19130 if Ekind (Component) = E_Component
19131 and then Present (Related_Type (Component))
19132 then
19133 null;
19135 elsif Ekind (Component) /= E_Component
19136 or else Chars (Component) = Name_uParent
19137 then
19138 null;
19140 -- If the derived type is within the parent type's declarative
19141 -- region, then the components can still be inherited even though
19142 -- they aren't visible at this point. This can occur for cases
19143 -- such as within public child units where the components must
19144 -- become visible upon entering the child unit's private part.
19146 elsif not Is_Visible_Component (Component)
19147 and then not In_Open_Scopes (Scope (Parent_Base))
19148 then
19149 null;
19151 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19152 then
19153 null;
19155 else
19156 Inherit_Component (Component);
19157 end if;
19159 Next_Entity (Component);
19160 end loop;
19162 -- For tagged derived types, inherited discriminants cannot be used in
19163 -- component declarations of the record extension part. To achieve this
19164 -- we mark the inherited discriminants as not visible.
19166 if Is_Tagged and then Inherit_Discr then
19167 D := First_Discriminant (Derived_Base);
19168 while Present (D) loop
19169 Set_Is_Immediately_Visible (D, False);
19170 Next_Discriminant (D);
19171 end loop;
19172 end if;
19174 return Assoc_List;
19175 end Inherit_Components;
19177 ----------------------
19178 -- Is_EVF_Procedure --
19179 ----------------------
19181 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19182 Formal : Entity_Id;
19184 begin
19185 -- Examine the formals of an Extensions_Visible False procedure looking
19186 -- for a controlling OUT parameter.
19188 if Ekind (Subp) = E_Procedure
19189 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19190 then
19191 Formal := First_Formal (Subp);
19192 while Present (Formal) loop
19193 if Ekind (Formal) = E_Out_Parameter
19194 and then Is_Controlling_Formal (Formal)
19195 then
19196 return True;
19197 end if;
19199 Next_Formal (Formal);
19200 end loop;
19201 end if;
19203 return False;
19204 end Is_EVF_Procedure;
19206 --------------------------
19207 -- Is_Private_Primitive --
19208 --------------------------
19210 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19211 Prim_Scope : constant Entity_Id := Scope (Prim);
19212 Priv_Entity : Entity_Id;
19213 begin
19214 if Is_Package_Or_Generic_Package (Prim_Scope) then
19215 Priv_Entity := First_Private_Entity (Prim_Scope);
19217 while Present (Priv_Entity) loop
19218 if Priv_Entity = Prim then
19219 return True;
19220 end if;
19222 Next_Entity (Priv_Entity);
19223 end loop;
19224 end if;
19226 return False;
19227 end Is_Private_Primitive;
19229 ------------------------------
19230 -- Is_Valid_Constraint_Kind --
19231 ------------------------------
19233 function Is_Valid_Constraint_Kind
19234 (T_Kind : Type_Kind;
19235 Constraint_Kind : Node_Kind) return Boolean
19237 begin
19238 case T_Kind is
19239 when Enumeration_Kind
19240 | Integer_Kind
19242 return Constraint_Kind = N_Range_Constraint;
19244 when Decimal_Fixed_Point_Kind =>
19245 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19247 when Ordinary_Fixed_Point_Kind =>
19248 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19250 when Float_Kind =>
19251 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19253 when Access_Kind
19254 | Array_Kind
19255 | Class_Wide_Kind
19256 | Concurrent_Kind
19257 | Private_Kind
19258 | E_Incomplete_Type
19259 | E_Record_Subtype
19260 | E_Record_Type
19262 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19264 when others =>
19265 return True; -- Error will be detected later
19266 end case;
19267 end Is_Valid_Constraint_Kind;
19269 --------------------------
19270 -- Is_Visible_Component --
19271 --------------------------
19273 function Is_Visible_Component
19274 (C : Entity_Id;
19275 N : Node_Id := Empty) return Boolean
19277 Original_Comp : Entity_Id := Empty;
19278 Original_Type : Entity_Id;
19279 Type_Scope : Entity_Id;
19281 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19282 -- Check whether parent type of inherited component is declared locally,
19283 -- possibly within a nested package or instance. The current scope is
19284 -- the derived record itself.
19286 -------------------
19287 -- Is_Local_Type --
19288 -------------------
19290 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19291 begin
19292 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19293 end Is_Local_Type;
19295 -- Start of processing for Is_Visible_Component
19297 begin
19298 if Ekind (C) in E_Component | E_Discriminant then
19299 Original_Comp := Original_Record_Component (C);
19300 end if;
19302 if No (Original_Comp) then
19304 -- Premature usage, or previous error
19306 return False;
19308 else
19309 Original_Type := Scope (Original_Comp);
19310 Type_Scope := Scope (Base_Type (Scope (C)));
19311 end if;
19313 -- This test only concerns tagged types
19315 if not Is_Tagged_Type (Original_Type) then
19317 -- Check if this is a renamed discriminant (hidden either by the
19318 -- derived type or by some ancestor), unless we are analyzing code
19319 -- generated by the expander since it may reference such components
19320 -- (for example see the expansion of Deep_Adjust).
19322 if Ekind (C) = E_Discriminant and then Present (N) then
19323 return
19324 not Comes_From_Source (N)
19325 or else not Is_Completely_Hidden (C);
19326 else
19327 return True;
19328 end if;
19330 -- If it is _Parent or _Tag, there is no visibility issue
19332 elsif not Comes_From_Source (Original_Comp) then
19333 return True;
19335 -- Discriminants are visible unless the (private) type has unknown
19336 -- discriminants. If the discriminant reference is inserted for a
19337 -- discriminant check on a full view it is also visible.
19339 elsif Ekind (Original_Comp) = E_Discriminant
19340 and then
19341 (not Has_Unknown_Discriminants (Original_Type)
19342 or else (Present (N)
19343 and then Nkind (N) = N_Selected_Component
19344 and then Nkind (Prefix (N)) = N_Type_Conversion
19345 and then not Comes_From_Source (Prefix (N))))
19346 then
19347 return True;
19349 -- If the component has been declared in an ancestor which is currently
19350 -- a private type, then it is not visible. The same applies if the
19351 -- component's containing type is not in an open scope and the original
19352 -- component's enclosing type is a visible full view of a private type
19353 -- (which can occur in cases where an attempt is being made to reference
19354 -- a component in a sibling package that is inherited from a visible
19355 -- component of a type in an ancestor package; the component in the
19356 -- sibling package should not be visible even though the component it
19357 -- inherited from is visible), but instance bodies are not subject to
19358 -- this second case since they have the Has_Private_View mechanism to
19359 -- ensure proper visibility. This does not apply however in the case
19360 -- where the scope of the type is a private child unit, or when the
19361 -- parent comes from a local package in which the ancestor is currently
19362 -- visible. The latter suppression of visibility is needed for cases
19363 -- that are tested in B730006.
19365 elsif Is_Private_Type (Original_Type)
19366 or else
19367 (not Is_Private_Descendant (Type_Scope)
19368 and then not In_Open_Scopes (Type_Scope)
19369 and then Has_Private_Declaration (Original_Type)
19370 and then not In_Instance_Body)
19371 then
19372 -- If the type derives from an entity in a formal package, there
19373 -- are no additional visible components.
19375 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19376 N_Formal_Package_Declaration
19377 then
19378 return False;
19380 -- if we are not in the private part of the current package, there
19381 -- are no additional visible components.
19383 elsif Ekind (Scope (Current_Scope)) = E_Package
19384 and then not In_Private_Part (Scope (Current_Scope))
19385 then
19386 return False;
19387 else
19388 return
19389 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19390 and then In_Open_Scopes (Scope (Original_Type))
19391 and then Is_Local_Type (Type_Scope);
19392 end if;
19394 -- There is another weird way in which a component may be invisible when
19395 -- the private and the full view are not derived from the same ancestor.
19396 -- Here is an example :
19398 -- type A1 is tagged record F1 : integer; end record;
19399 -- type A2 is new A1 with record F2 : integer; end record;
19400 -- type T is new A1 with private;
19401 -- private
19402 -- type T is new A2 with null record;
19404 -- In this case, the full view of T inherits F1 and F2 but the private
19405 -- view inherits only F1
19407 else
19408 declare
19409 Ancestor : Entity_Id := Scope (C);
19411 begin
19412 loop
19413 if Ancestor = Original_Type then
19414 return True;
19416 -- The ancestor may have a partial view of the original type,
19417 -- but if the full view is in scope, as in a child body, the
19418 -- component is visible.
19420 elsif In_Private_Part (Scope (Original_Type))
19421 and then Full_View (Ancestor) = Original_Type
19422 then
19423 return True;
19425 elsif Ancestor = Etype (Ancestor) then
19427 -- No further ancestors to examine
19429 return False;
19430 end if;
19432 Ancestor := Etype (Ancestor);
19433 end loop;
19434 end;
19435 end if;
19436 end Is_Visible_Component;
19438 --------------------------
19439 -- Make_Class_Wide_Type --
19440 --------------------------
19442 procedure Make_Class_Wide_Type (T : Entity_Id) is
19443 CW_Type : Entity_Id;
19444 CW_Name : Name_Id;
19445 Next_E : Entity_Id;
19446 Prev_E : Entity_Id;
19448 begin
19449 if Present (Class_Wide_Type (T)) then
19451 -- The class-wide type is a partially decorated entity created for a
19452 -- unanalyzed tagged type referenced through a limited with clause.
19453 -- When the tagged type is analyzed, its class-wide type needs to be
19454 -- redecorated. Note that we reuse the entity created by Decorate_
19455 -- Tagged_Type in order to preserve all links.
19457 if Materialize_Entity (Class_Wide_Type (T)) then
19458 CW_Type := Class_Wide_Type (T);
19459 Set_Materialize_Entity (CW_Type, False);
19461 -- The class wide type can have been defined by the partial view, in
19462 -- which case everything is already done.
19464 else
19465 return;
19466 end if;
19468 -- Default case, we need to create a new class-wide type
19470 else
19471 CW_Type :=
19472 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19473 end if;
19475 -- Inherit root type characteristics
19477 CW_Name := Chars (CW_Type);
19478 Next_E := Next_Entity (CW_Type);
19479 Prev_E := Prev_Entity (CW_Type);
19480 Copy_Node (T, CW_Type);
19481 Set_Comes_From_Source (CW_Type, False);
19482 Set_Chars (CW_Type, CW_Name);
19483 Set_Parent (CW_Type, Parent (T));
19484 Set_Prev_Entity (CW_Type, Prev_E);
19485 Set_Next_Entity (CW_Type, Next_E);
19487 -- Ensure we have a new freeze node for the class-wide type. The partial
19488 -- view may have freeze action of its own, requiring a proper freeze
19489 -- node, and the same freeze node cannot be shared between the two
19490 -- types.
19492 Set_Has_Delayed_Freeze (CW_Type);
19493 Set_Freeze_Node (CW_Type, Empty);
19495 -- Customize the class-wide type: It has no prim. op., it cannot be
19496 -- abstract, its Etype points back to the specific root type, and it
19497 -- cannot have any invariants.
19499 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19500 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19502 elsif Ekind (CW_Type) in Concurrent_Kind then
19503 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19504 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19506 if Ekind (CW_Type) in Task_Kind then
19507 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19508 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19509 end if;
19511 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19512 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19513 end if;
19514 end if;
19516 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19517 Set_Is_Tagged_Type (CW_Type, True);
19518 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19519 Set_Is_Abstract_Type (CW_Type, False);
19520 Set_Is_Constrained (CW_Type, False);
19521 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19522 Set_Default_SSO (CW_Type);
19523 Set_Has_Inheritable_Invariants (CW_Type, False);
19524 Set_Has_Inherited_Invariants (CW_Type, False);
19525 Set_Has_Own_Invariants (CW_Type, False);
19527 if Ekind (T) = E_Class_Wide_Subtype then
19528 Set_Etype (CW_Type, Etype (Base_Type (T)));
19529 else
19530 Set_Etype (CW_Type, T);
19531 end if;
19533 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19535 -- If this is the class_wide type of a constrained subtype, it does
19536 -- not have discriminants.
19538 Set_Has_Discriminants (CW_Type,
19539 Has_Discriminants (T) and then not Is_Constrained (T));
19541 Set_Has_Unknown_Discriminants (CW_Type, True);
19542 Set_Class_Wide_Type (T, CW_Type);
19543 Set_Equivalent_Type (CW_Type, Empty);
19545 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19547 Set_Class_Wide_Type (CW_Type, CW_Type);
19548 end Make_Class_Wide_Type;
19550 ----------------
19551 -- Make_Index --
19552 ----------------
19554 procedure Make_Index
19555 (N : Node_Id;
19556 Related_Nod : Node_Id;
19557 Related_Id : Entity_Id := Empty;
19558 Suffix_Index : Pos := 1)
19560 R : Node_Id;
19561 T : Entity_Id;
19562 Def_Id : Entity_Id := Empty;
19563 Found : Boolean := False;
19565 begin
19566 -- For a discrete range used in a constrained array definition and
19567 -- defined by a range, an implicit conversion to the predefined type
19568 -- INTEGER is assumed if each bound is either a numeric literal, a named
19569 -- number, or an attribute, and the type of both bounds (prior to the
19570 -- implicit conversion) is the type universal_integer. Otherwise, both
19571 -- bounds must be of the same discrete type, other than universal
19572 -- integer; this type must be determinable independently of the
19573 -- context, but using the fact that the type must be discrete and that
19574 -- both bounds must have the same type.
19576 -- Character literals also have a universal type in the absence of
19577 -- of additional context, and are resolved to Standard_Character.
19579 if Nkind (N) = N_Range then
19581 -- The index is given by a range constraint. The bounds are known
19582 -- to be of a consistent type.
19584 if not Is_Overloaded (N) then
19585 T := Etype (N);
19587 -- For universal bounds, choose the specific predefined type
19589 if T = Universal_Integer then
19590 T := Standard_Integer;
19592 elsif T = Any_Character then
19593 Ambiguous_Character (Low_Bound (N));
19595 T := Standard_Character;
19596 end if;
19598 -- The node may be overloaded because some user-defined operators
19599 -- are available, but if a universal interpretation exists it is
19600 -- also the selected one.
19602 elsif Universal_Interpretation (N) = Universal_Integer then
19603 T := Standard_Integer;
19605 else
19606 T := Any_Type;
19608 declare
19609 Ind : Interp_Index;
19610 It : Interp;
19612 begin
19613 Get_First_Interp (N, Ind, It);
19614 while Present (It.Typ) loop
19615 if Is_Discrete_Type (It.Typ) then
19617 if Found
19618 and then not Covers (It.Typ, T)
19619 and then not Covers (T, It.Typ)
19620 then
19621 Error_Msg_N ("ambiguous bounds in discrete range", N);
19622 exit;
19623 else
19624 T := It.Typ;
19625 Found := True;
19626 end if;
19627 end if;
19629 Get_Next_Interp (Ind, It);
19630 end loop;
19632 if T = Any_Type then
19633 Error_Msg_N ("discrete type required for range", N);
19634 Set_Etype (N, Any_Type);
19635 return;
19637 elsif T = Universal_Integer then
19638 T := Standard_Integer;
19639 end if;
19640 end;
19641 end if;
19643 if not Is_Discrete_Type (T) then
19644 Error_Msg_N ("discrete type required for range", N);
19645 Set_Etype (N, Any_Type);
19646 return;
19647 end if;
19649 -- If the range bounds are "T'First .. T'Last" where T is a name of a
19650 -- discrete type, then use T as the type of the index.
19652 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19653 and then Attribute_Name (Low_Bound (N)) = Name_First
19654 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19655 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19657 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19658 and then Attribute_Name (High_Bound (N)) = Name_Last
19659 and then Is_Entity_Name (Prefix (High_Bound (N)))
19660 and then Entity (Prefix (High_Bound (N))) = Def_Id
19661 then
19662 Def_Id := Entity (Prefix (Low_Bound (N)));
19663 end if;
19665 R := N;
19666 Process_Range_Expr_In_Decl (R, T);
19668 elsif Nkind (N) = N_Subtype_Indication then
19670 -- The index is given by a subtype with a range constraint
19672 T := Base_Type (Entity (Subtype_Mark (N)));
19674 if not Is_Discrete_Type (T) then
19675 Error_Msg_N ("discrete type required for range", N);
19676 Set_Etype (N, Any_Type);
19677 return;
19678 end if;
19680 R := Range_Expression (Constraint (N));
19682 Resolve (R, T);
19683 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19685 elsif Nkind (N) = N_Attribute_Reference then
19687 -- Catch beginner's error (use of attribute other than 'Range)
19689 if Attribute_Name (N) /= Name_Range then
19690 Error_Msg_N ("expect attribute ''Range", N);
19691 Set_Etype (N, Any_Type);
19692 return;
19693 end if;
19695 -- If the node denotes the range of a type mark, that is also the
19696 -- resulting type, and we do not need to create an Itype for it.
19698 if Is_Entity_Name (Prefix (N))
19699 and then Comes_From_Source (N)
19700 and then Is_Discrete_Type (Entity (Prefix (N)))
19701 then
19702 Def_Id := Entity (Prefix (N));
19703 end if;
19705 Analyze_And_Resolve (N);
19706 T := Etype (N);
19707 R := N;
19709 -- If none of the above, must be a subtype. We convert this to a
19710 -- range attribute reference because in the case of declared first
19711 -- named subtypes, the types in the range reference can be different
19712 -- from the type of the entity. A range attribute normalizes the
19713 -- reference and obtains the correct types for the bounds.
19715 -- This transformation is in the nature of an expansion, is only
19716 -- done if expansion is active. In particular, it is not done on
19717 -- formal generic types, because we need to retain the name of the
19718 -- original index for instantiation purposes.
19720 else
19721 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19722 Error_Msg_N ("invalid subtype mark in discrete range", N);
19723 Set_Etype (N, Any_Integer);
19724 return;
19726 else
19727 -- The type mark may be that of an incomplete type. It is only
19728 -- now that we can get the full view, previous analysis does
19729 -- not look specifically for a type mark.
19731 Set_Entity (N, Get_Full_View (Entity (N)));
19732 Set_Etype (N, Entity (N));
19733 Def_Id := Entity (N);
19735 if not Is_Discrete_Type (Def_Id) then
19736 Error_Msg_N ("discrete type required for index", N);
19737 Set_Etype (N, Any_Type);
19738 return;
19739 end if;
19740 end if;
19742 if Expander_Active then
19743 Rewrite (N,
19744 Make_Attribute_Reference (Sloc (N),
19745 Attribute_Name => Name_Range,
19746 Prefix => Relocate_Node (N)));
19748 -- The original was a subtype mark that does not freeze. This
19749 -- means that the rewritten version must not freeze either.
19751 Set_Must_Not_Freeze (N);
19752 Set_Must_Not_Freeze (Prefix (N));
19753 Analyze_And_Resolve (N);
19754 T := Etype (N);
19755 R := N;
19757 -- If expander is inactive, type is legal, nothing else to construct
19759 else
19760 return;
19761 end if;
19762 end if;
19764 if not Is_Discrete_Type (T) then
19765 Error_Msg_N ("discrete type required for range", N);
19766 Set_Etype (N, Any_Type);
19767 return;
19769 elsif T = Any_Type then
19770 Set_Etype (N, Any_Type);
19771 return;
19772 end if;
19774 -- We will now create the appropriate Itype to describe the range, but
19775 -- first a check. If we originally had a subtype, then we just label
19776 -- the range with this subtype. Not only is there no need to construct
19777 -- a new subtype, but it is wrong to do so for two reasons:
19779 -- 1. A legality concern, if we have a subtype, it must not freeze,
19780 -- and the Itype would cause freezing incorrectly
19782 -- 2. An efficiency concern, if we created an Itype, it would not be
19783 -- recognized as the same type for the purposes of eliminating
19784 -- checks in some circumstances.
19786 -- We signal this case by setting the subtype entity in Def_Id
19788 if No (Def_Id) then
19789 Def_Id :=
19790 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19791 Set_Etype (Def_Id, Base_Type (T));
19793 if Is_Signed_Integer_Type (T) then
19794 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
19796 elsif Is_Modular_Integer_Type (T) then
19797 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
19799 else
19800 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
19801 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19802 Set_First_Literal (Def_Id, First_Literal (T));
19803 end if;
19805 Set_Size_Info (Def_Id, (T));
19806 Set_RM_Size (Def_Id, RM_Size (T));
19807 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19809 Set_Scalar_Range (Def_Id, R);
19810 Conditional_Delay (Def_Id, T);
19812 -- In the subtype indication case inherit properties of the parent
19814 if Nkind (N) = N_Subtype_Indication then
19816 -- It is enough to inherit predicate flags and not the predicate
19817 -- functions, because predicates on an index type are illegal
19818 -- anyway and the flags are enough to detect them.
19820 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19822 -- If the immediate parent of the new subtype is nonstatic, then
19823 -- the subtype we create is nonstatic as well, even if its bounds
19824 -- are static.
19826 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
19827 Set_Is_Non_Static_Subtype (Def_Id);
19828 end if;
19829 end if;
19831 Set_Parent (Def_Id, N);
19832 end if;
19834 -- Final step is to label the index with this constructed type
19836 Set_Etype (N, Def_Id);
19837 end Make_Index;
19839 ------------------------------
19840 -- Modular_Type_Declaration --
19841 ------------------------------
19843 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19844 Mod_Expr : constant Node_Id := Expression (Def);
19845 M_Val : Uint;
19847 procedure Set_Modular_Size (Bits : Int);
19848 -- Sets RM_Size to Bits, and Esize to normal word size above this
19850 ----------------------
19851 -- Set_Modular_Size --
19852 ----------------------
19854 procedure Set_Modular_Size (Bits : Int) is
19855 Siz : Int;
19857 begin
19858 Set_RM_Size (T, UI_From_Int (Bits));
19860 if Bits < System_Max_Binary_Modulus_Power then
19861 Siz := 8;
19863 while Siz < 128 loop
19864 exit when Bits <= Siz;
19865 Siz := Siz * 2;
19866 end loop;
19868 Set_Esize (T, UI_From_Int (Siz));
19870 else
19871 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
19872 end if;
19874 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19875 Set_Is_Known_Valid (T);
19876 end if;
19877 end Set_Modular_Size;
19879 -- Start of processing for Modular_Type_Declaration
19881 begin
19882 -- If the mod expression is (exactly) 2 * literal, where literal is
19883 -- 128 or less, then almost certainly the * was meant to be **. Warn.
19885 if Warn_On_Suspicious_Modulus_Value
19886 and then Nkind (Mod_Expr) = N_Op_Multiply
19887 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19888 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19889 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19890 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
19891 then
19892 Error_Msg_N
19893 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19894 end if;
19896 -- Proceed with analysis of mod expression
19898 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19900 if Ekind (T) in Incomplete_Or_Private_Kind then
19901 Reinit_Field_To_Zero (T, F_Stored_Constraint);
19902 end if;
19904 Set_Etype (T, T);
19905 Mutate_Ekind (T, E_Modular_Integer_Type);
19906 Reinit_Alignment (T);
19907 Set_Is_Constrained (T);
19909 if not Is_OK_Static_Expression (Mod_Expr) then
19910 Flag_Non_Static_Expr
19911 ("non-static expression used for modular type bound!", Mod_Expr);
19912 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19913 else
19914 M_Val := Expr_Value (Mod_Expr);
19915 end if;
19917 if M_Val < 1 then
19918 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19919 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19920 end if;
19922 if M_Val > 2 ** Standard_Long_Integer_Size then
19923 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19924 end if;
19926 Set_Modulus (T, M_Val);
19928 -- Create bounds for the modular type based on the modulus given in
19929 -- the type declaration and then analyze and resolve those bounds.
19931 Set_Scalar_Range (T,
19932 Make_Range (Sloc (Mod_Expr),
19933 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19934 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19936 -- Properly analyze the literals for the range. We do this manually
19937 -- because we can't go calling Resolve, since we are resolving these
19938 -- bounds with the type, and this type is certainly not complete yet.
19940 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19941 Set_Etype (High_Bound (Scalar_Range (T)), T);
19942 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19943 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19945 -- Loop through powers of two to find number of bits required
19947 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19949 -- Binary case
19951 if M_Val = 2 ** Bits then
19952 Set_Modular_Size (Bits);
19953 return;
19955 -- Nonbinary case
19957 elsif M_Val < 2 ** Bits then
19958 Set_Non_Binary_Modulus (T);
19960 if Bits > System_Max_Nonbinary_Modulus_Power then
19961 Error_Msg_Uint_1 :=
19962 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19963 Error_Msg_F
19964 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19965 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19966 return;
19968 else
19969 -- In the nonbinary case, set size as per RM 13.3(55)
19971 Set_Modular_Size (Bits);
19972 return;
19973 end if;
19974 end if;
19976 end loop;
19978 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19979 -- so we just signal an error and set the maximum size.
19981 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19982 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19984 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19985 Reinit_Alignment (T);
19987 end Modular_Type_Declaration;
19989 --------------------------
19990 -- New_Concatenation_Op --
19991 --------------------------
19993 procedure New_Concatenation_Op (Typ : Entity_Id) is
19994 Loc : constant Source_Ptr := Sloc (Typ);
19995 Op : Entity_Id;
19997 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19998 -- Create abbreviated declaration for the formal of a predefined
19999 -- Operator 'Op' of type 'Typ'
20001 --------------------
20002 -- Make_Op_Formal --
20003 --------------------
20005 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20006 Formal : Entity_Id;
20007 begin
20008 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20009 Set_Etype (Formal, Typ);
20010 Set_Mechanism (Formal, Default_Mechanism);
20011 return Formal;
20012 end Make_Op_Formal;
20014 -- Start of processing for New_Concatenation_Op
20016 begin
20017 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20019 Mutate_Ekind (Op, E_Operator);
20020 Set_Scope (Op, Current_Scope);
20021 Set_Etype (Op, Typ);
20022 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20023 Set_Is_Immediately_Visible (Op);
20024 Set_Is_Intrinsic_Subprogram (Op);
20025 Set_Has_Completion (Op);
20026 Append_Entity (Op, Current_Scope);
20028 Set_Name_Entity_Id (Name_Op_Concat, Op);
20030 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20031 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20032 end New_Concatenation_Op;
20034 -------------------------
20035 -- OK_For_Limited_Init --
20036 -------------------------
20038 -- ???Check all calls of this, and compare the conditions under which it's
20039 -- called.
20041 function OK_For_Limited_Init
20042 (Typ : Entity_Id;
20043 Exp : Node_Id) return Boolean
20045 begin
20046 return Is_CPP_Constructor_Call (Exp)
20047 or else (Ada_Version >= Ada_2005
20048 and then not Debug_Flag_Dot_L
20049 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20050 end OK_For_Limited_Init;
20052 -------------------------------
20053 -- OK_For_Limited_Init_In_05 --
20054 -------------------------------
20056 function OK_For_Limited_Init_In_05
20057 (Typ : Entity_Id;
20058 Exp : Node_Id) return Boolean
20060 begin
20061 -- An object of a limited interface type can be initialized with any
20062 -- expression of a nonlimited descendant type. However this does not
20063 -- apply if this is a view conversion of some other expression. This
20064 -- is checked below.
20066 if Is_Class_Wide_Type (Typ)
20067 and then Is_Limited_Interface (Typ)
20068 and then not Is_Limited_Type (Etype (Exp))
20069 and then Nkind (Exp) /= N_Type_Conversion
20070 then
20071 return True;
20072 end if;
20074 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20075 -- case of limited aggregates (including extension aggregates), and
20076 -- function calls. The function call may have been given in prefixed
20077 -- notation, in which case the original node is an indexed component.
20078 -- If the function is parameterless, the original node was an explicit
20079 -- dereference. The function may also be parameterless, in which case
20080 -- the source node is just an identifier.
20082 -- A branch of a conditional expression may have been removed if the
20083 -- condition is statically known. This happens during expansion, and
20084 -- thus will not happen if previous errors were encountered. The check
20085 -- will have been performed on the chosen branch, which replaces the
20086 -- original conditional expression.
20088 if No (Exp) then
20089 return True;
20090 end if;
20092 case Nkind (Original_Node (Exp)) is
20093 when N_Aggregate
20094 | N_Extension_Aggregate
20095 | N_Function_Call
20096 | N_Op
20098 return True;
20100 when N_Identifier =>
20101 return Present (Entity (Original_Node (Exp)))
20102 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20104 when N_Qualified_Expression =>
20105 return
20106 OK_For_Limited_Init_In_05
20107 (Typ, Expression (Original_Node (Exp)));
20109 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20110 -- with a function call, the expander has rewritten the call into an
20111 -- N_Type_Conversion node to force displacement of the pointer to
20112 -- reference the component containing the secondary dispatch table.
20113 -- Otherwise a type conversion is not a legal context.
20114 -- A return statement for a build-in-place function returning a
20115 -- synchronized type also introduces an unchecked conversion.
20117 when N_Type_Conversion
20118 | N_Unchecked_Type_Conversion
20120 return not Comes_From_Source (Exp)
20121 and then
20122 -- If the conversion has been rewritten, check Original_Node
20124 ((Original_Node (Exp) /= Exp
20125 and then
20126 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
20128 -- Otherwise, check the expression of the compiler-generated
20129 -- conversion (which is a conversion that we want to ignore
20130 -- for purposes of the limited-initialization restrictions).
20132 or else
20133 (Original_Node (Exp) = Exp
20134 and then
20135 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
20137 when N_Explicit_Dereference
20138 | N_Indexed_Component
20139 | N_Selected_Component
20141 return Nkind (Exp) = N_Function_Call;
20143 -- A use of 'Input is a function call, hence allowed. Normally the
20144 -- attribute will be changed to a call, but the attribute by itself
20145 -- can occur with -gnatc.
20147 when N_Attribute_Reference =>
20148 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20150 -- "return raise ..." is OK
20152 when N_Raise_Expression =>
20153 return True;
20155 -- For a case expression, all dependent expressions must be legal
20157 when N_Case_Expression =>
20158 declare
20159 Alt : Node_Id;
20161 begin
20162 Alt := First (Alternatives (Original_Node (Exp)));
20163 while Present (Alt) loop
20164 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20165 return False;
20166 end if;
20168 Next (Alt);
20169 end loop;
20171 return True;
20172 end;
20174 -- For an if expression, all dependent expressions must be legal
20176 when N_If_Expression =>
20177 declare
20178 Then_Expr : constant Node_Id :=
20179 Next (First (Expressions (Original_Node (Exp))));
20180 Else_Expr : constant Node_Id := Next (Then_Expr);
20181 begin
20182 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20183 and then
20184 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20185 end;
20187 when others =>
20188 return False;
20189 end case;
20190 end OK_For_Limited_Init_In_05;
20192 -------------------------------------------
20193 -- Ordinary_Fixed_Point_Type_Declaration --
20194 -------------------------------------------
20196 procedure Ordinary_Fixed_Point_Type_Declaration
20197 (T : Entity_Id;
20198 Def : Node_Id)
20200 Loc : constant Source_Ptr := Sloc (Def);
20201 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20202 RRS : constant Node_Id := Real_Range_Specification (Def);
20203 Implicit_Base : Entity_Id;
20204 Delta_Val : Ureal;
20205 Small_Val : Ureal;
20206 Low_Val : Ureal;
20207 High_Val : Ureal;
20209 begin
20210 Check_Restriction (No_Fixed_Point, Def);
20212 -- Create implicit base type
20214 Implicit_Base :=
20215 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20216 Set_Etype (Implicit_Base, Implicit_Base);
20218 -- Analyze and process delta expression
20220 Analyze_And_Resolve (Delta_Expr, Any_Real);
20222 Check_Delta_Expression (Delta_Expr);
20223 Delta_Val := Expr_Value_R (Delta_Expr);
20225 Set_Delta_Value (Implicit_Base, Delta_Val);
20227 -- Compute default small from given delta, which is the largest power
20228 -- of two that does not exceed the given delta value.
20230 declare
20231 Tmp : Ureal;
20232 Scale : Int;
20234 begin
20235 Tmp := Ureal_1;
20236 Scale := 0;
20238 if Delta_Val < Ureal_1 then
20239 while Delta_Val < Tmp loop
20240 Tmp := Tmp / Ureal_2;
20241 Scale := Scale + 1;
20242 end loop;
20244 else
20245 loop
20246 Tmp := Tmp * Ureal_2;
20247 exit when Tmp > Delta_Val;
20248 Scale := Scale - 1;
20249 end loop;
20250 end if;
20252 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20253 end;
20255 Set_Small_Value (Implicit_Base, Small_Val);
20257 -- If no range was given, set a dummy range
20259 if RRS <= Empty_Or_Error then
20260 Low_Val := -Small_Val;
20261 High_Val := Small_Val;
20263 -- Otherwise analyze and process given range
20265 else
20266 declare
20267 Low : constant Node_Id := Low_Bound (RRS);
20268 High : constant Node_Id := High_Bound (RRS);
20270 begin
20271 Analyze_And_Resolve (Low, Any_Real);
20272 Analyze_And_Resolve (High, Any_Real);
20273 Check_Real_Bound (Low);
20274 Check_Real_Bound (High);
20276 -- Obtain and set the range
20278 Low_Val := Expr_Value_R (Low);
20279 High_Val := Expr_Value_R (High);
20281 if Low_Val > High_Val then
20282 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20283 end if;
20284 end;
20285 end if;
20287 -- The range for both the implicit base and the declared first subtype
20288 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20289 -- set a temporary range in place. Note that the bounds of the base
20290 -- type will be widened to be symmetrical and to fill the available
20291 -- bits when the type is frozen.
20293 -- We could do this with all discrete types, and probably should, but
20294 -- we absolutely have to do it for fixed-point, since the end-points
20295 -- of the range and the size are determined by the small value, which
20296 -- could be reset before the freeze point.
20298 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20299 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20301 -- Complete definition of first subtype. The inheritance of the rep item
20302 -- chain ensures that SPARK-related pragmas are not clobbered when the
20303 -- ordinary fixed point type acts as a full view of a private type.
20305 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20306 Set_Etype (T, Implicit_Base);
20307 Reinit_Size_Align (T);
20308 Inherit_Rep_Item_Chain (T, Implicit_Base);
20309 Set_Small_Value (T, Small_Val);
20310 Set_Delta_Value (T, Delta_Val);
20311 Set_Is_Constrained (T);
20312 end Ordinary_Fixed_Point_Type_Declaration;
20314 ----------------------------------
20315 -- Preanalyze_Assert_Expression --
20316 ----------------------------------
20318 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20319 begin
20320 In_Assertion_Expr := In_Assertion_Expr + 1;
20321 Preanalyze_Spec_Expression (N, T);
20322 In_Assertion_Expr := In_Assertion_Expr - 1;
20323 end Preanalyze_Assert_Expression;
20325 -----------------------------------
20326 -- Preanalyze_Default_Expression --
20327 -----------------------------------
20329 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20330 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20331 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20333 begin
20334 In_Default_Expr := True;
20335 In_Spec_Expression := True;
20337 Preanalyze_With_Freezing_And_Resolve (N, T);
20339 In_Default_Expr := Save_In_Default_Expr;
20340 In_Spec_Expression := Save_In_Spec_Expression;
20341 end Preanalyze_Default_Expression;
20343 --------------------------------
20344 -- Preanalyze_Spec_Expression --
20345 --------------------------------
20347 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20348 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20349 begin
20350 In_Spec_Expression := True;
20351 Preanalyze_And_Resolve (N, T);
20352 In_Spec_Expression := Save_In_Spec_Expression;
20353 end Preanalyze_Spec_Expression;
20355 ----------------------------------------
20356 -- Prepare_Private_Subtype_Completion --
20357 ----------------------------------------
20359 procedure Prepare_Private_Subtype_Completion
20360 (Id : Entity_Id;
20361 Related_Nod : Node_Id)
20363 Id_B : constant Entity_Id := Base_Type (Id);
20364 Full_B : constant Entity_Id := Full_View (Id_B);
20365 Full : Entity_Id;
20367 begin
20368 if Present (Full_B) then
20370 -- The Base_Type is already completed, we can complete the subtype
20371 -- now. We have to create a new entity with the same name, Thus we
20372 -- can't use Create_Itype.
20374 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20375 Set_Is_Itype (Full);
20376 Set_Associated_Node_For_Itype (Full, Related_Nod);
20377 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20378 Set_Full_View (Id, Full);
20379 end if;
20381 -- The parent subtype may be private, but the base might not, in some
20382 -- nested instances. In that case, the subtype does not need to be
20383 -- exchanged. It would still be nice to make private subtypes and their
20384 -- bases consistent at all times ???
20386 if Is_Private_Type (Id_B) then
20387 Append_Elmt (Id, Private_Dependents (Id_B));
20388 end if;
20389 end Prepare_Private_Subtype_Completion;
20391 ---------------------------
20392 -- Process_Discriminants --
20393 ---------------------------
20395 procedure Process_Discriminants
20396 (N : Node_Id;
20397 Prev : Entity_Id := Empty)
20399 Elist : constant Elist_Id := New_Elmt_List;
20400 Id : Node_Id;
20401 Discr : Node_Id;
20402 Discr_Number : Uint;
20403 Discr_Type : Entity_Id;
20404 Default_Present : Boolean := False;
20405 Default_Not_Present : Boolean := False;
20407 begin
20408 -- A composite type other than an array type can have discriminants.
20409 -- On entry, the current scope is the composite type.
20411 -- The discriminants are initially entered into the scope of the type
20412 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20413 -- use, as explained at the end of this procedure.
20415 Discr := First (Discriminant_Specifications (N));
20416 while Present (Discr) loop
20417 Enter_Name (Defining_Identifier (Discr));
20419 -- For navigation purposes we add a reference to the discriminant
20420 -- in the entity for the type. If the current declaration is a
20421 -- completion, place references on the partial view. Otherwise the
20422 -- type is the current scope.
20424 if Present (Prev) then
20426 -- The references go on the partial view, if present. If the
20427 -- partial view has discriminants, the references have been
20428 -- generated already.
20430 if not Has_Discriminants (Prev) then
20431 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20432 end if;
20433 else
20434 Generate_Reference
20435 (Current_Scope, Defining_Identifier (Discr), 'd');
20436 end if;
20438 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20439 Check_Anonymous_Access_Component
20440 (Typ_Decl => N,
20441 Typ => Defining_Identifier (N),
20442 Prev => Prev,
20443 Comp_Def => Discr,
20444 Access_Def => Discriminant_Type (Discr));
20446 -- if Check_Anonymous_Access_Component replaced Discr then
20447 -- its Original_Node points to the old Discr and the access type
20448 -- for Discr_Type has already been created.
20450 if Original_Node (Discr) /= Discr then
20451 Discr_Type := Etype (Discriminant_Type (Discr));
20452 else
20453 Discr_Type :=
20454 Access_Definition (Discr, Discriminant_Type (Discr));
20456 -- Ada 2005 (AI-254)
20458 if Present (Access_To_Subprogram_Definition
20459 (Discriminant_Type (Discr)))
20460 and then Protected_Present (Access_To_Subprogram_Definition
20461 (Discriminant_Type (Discr)))
20462 then
20463 Discr_Type :=
20464 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20465 end if;
20466 end if;
20467 else
20468 Find_Type (Discriminant_Type (Discr));
20469 Discr_Type := Etype (Discriminant_Type (Discr));
20471 if Error_Posted (Discriminant_Type (Discr)) then
20472 Discr_Type := Any_Type;
20473 end if;
20474 end if;
20476 -- Handling of discriminants that are access types
20478 if Is_Access_Type (Discr_Type) then
20480 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20481 -- limited record types
20483 if Ada_Version < Ada_2005 then
20484 Check_Access_Discriminant_Requires_Limited
20485 (Discr, Discriminant_Type (Discr));
20486 end if;
20488 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20489 Error_Msg_N
20490 ("(Ada 83) access discriminant not allowed", Discr);
20491 end if;
20493 -- If not access type, must be a discrete type
20495 elsif not Is_Discrete_Type (Discr_Type) then
20496 Error_Msg_N
20497 ("discriminants must have a discrete or access type",
20498 Discriminant_Type (Discr));
20499 end if;
20501 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20503 -- If a discriminant specification includes the assignment compound
20504 -- delimiter followed by an expression, the expression is the default
20505 -- expression of the discriminant; the default expression must be of
20506 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20507 -- a default expression, we do the special preanalysis, since this
20508 -- expression does not freeze (see section "Handling of Default and
20509 -- Per-Object Expressions" in spec of package Sem).
20511 if Present (Expression (Discr)) then
20512 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20514 -- Legaity checks
20516 if Nkind (N) = N_Formal_Type_Declaration then
20517 Error_Msg_N
20518 ("discriminant defaults not allowed for formal type",
20519 Expression (Discr));
20521 -- Flag an error for a tagged type with defaulted discriminants,
20522 -- excluding limited tagged types when compiling for Ada 2012
20523 -- (see AI05-0214).
20525 elsif Is_Tagged_Type (Current_Scope)
20526 and then (not Is_Limited_Type (Current_Scope)
20527 or else Ada_Version < Ada_2012)
20528 and then Comes_From_Source (N)
20529 then
20530 -- Note: see similar test in Check_Or_Process_Discriminants, to
20531 -- handle the (illegal) case of the completion of an untagged
20532 -- view with discriminants with defaults by a tagged full view.
20533 -- We skip the check if Discr does not come from source, to
20534 -- account for the case of an untagged derived type providing
20535 -- defaults for a renamed discriminant from a private untagged
20536 -- ancestor with a tagged full view (ACATS B460006).
20538 if Ada_Version >= Ada_2012 then
20539 Error_Msg_N
20540 ("discriminants of nonlimited tagged type cannot have"
20541 & " defaults",
20542 Expression (Discr));
20543 else
20544 Error_Msg_N
20545 ("discriminants of tagged type cannot have defaults",
20546 Expression (Discr));
20547 end if;
20549 else
20550 Default_Present := True;
20551 Append_Elmt (Expression (Discr), Elist);
20553 -- Tag the defining identifiers for the discriminants with
20554 -- their corresponding default expressions from the tree.
20556 Set_Discriminant_Default_Value
20557 (Defining_Identifier (Discr), Expression (Discr));
20558 end if;
20560 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20561 -- gets set unless we can be sure that no range check is required.
20563 if not Expander_Active
20564 and then not
20565 Is_In_Range
20566 (Expression (Discr), Discr_Type, Assume_Valid => True)
20567 then
20568 Set_Do_Range_Check (Expression (Discr));
20569 end if;
20571 -- No default discriminant value given
20573 else
20574 Default_Not_Present := True;
20575 end if;
20577 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20578 -- Discr_Type but with the null-exclusion attribute
20580 if Ada_Version >= Ada_2005 then
20582 -- Ada 2005 (AI-231): Static checks
20584 if Can_Never_Be_Null (Discr_Type) then
20585 Null_Exclusion_Static_Checks (Discr);
20587 elsif Is_Access_Type (Discr_Type)
20588 and then Null_Exclusion_Present (Discr)
20590 -- No need to check itypes because in their case this check
20591 -- was done at their point of creation
20593 and then not Is_Itype (Discr_Type)
20594 then
20595 if Can_Never_Be_Null (Discr_Type) then
20596 Error_Msg_NE
20597 ("`NOT NULL` not allowed (& already excludes null)",
20598 Discr,
20599 Discr_Type);
20600 end if;
20602 Set_Etype (Defining_Identifier (Discr),
20603 Create_Null_Excluding_Itype
20604 (T => Discr_Type,
20605 Related_Nod => Discr));
20607 -- Check for improper null exclusion if the type is otherwise
20608 -- legal for a discriminant.
20610 elsif Null_Exclusion_Present (Discr)
20611 and then Is_Discrete_Type (Discr_Type)
20612 then
20613 Error_Msg_N
20614 ("null exclusion can only apply to an access type", Discr);
20615 end if;
20617 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20618 -- can't have defaults. Synchronized types, or types that are
20619 -- explicitly limited are fine, but special tests apply to derived
20620 -- types in generics: in a generic body we have to assume the
20621 -- worst, and therefore defaults are not allowed if the parent is
20622 -- a generic formal private type (see ACATS B370001).
20624 if Is_Access_Type (Discr_Type) and then Default_Present then
20625 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20626 or else Is_Limited_Record (Current_Scope)
20627 or else Is_Concurrent_Type (Current_Scope)
20628 or else Is_Concurrent_Record_Type (Current_Scope)
20629 or else Ekind (Current_Scope) = E_Limited_Private_Type
20630 then
20631 if not Is_Derived_Type (Current_Scope)
20632 or else not Is_Generic_Type (Etype (Current_Scope))
20633 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20634 or else Limited_Present
20635 (Type_Definition (Parent (Current_Scope)))
20636 then
20637 null;
20639 else
20640 Error_Msg_N
20641 ("access discriminants of nonlimited types cannot "
20642 & "have defaults", Expression (Discr));
20643 end if;
20645 elsif Present (Expression (Discr)) then
20646 Error_Msg_N
20647 ("(Ada 2005) access discriminants of nonlimited types "
20648 & "cannot have defaults", Expression (Discr));
20649 end if;
20650 end if;
20651 end if;
20653 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20654 -- This check is relevant only when SPARK_Mode is on as it is not a
20655 -- standard Ada legality rule. The only way for a discriminant to be
20656 -- effectively volatile is to have an effectively volatile type, so
20657 -- we check this directly, because the Ekind of Discr might not be
20658 -- set yet (to help preventing cascaded errors on derived types).
20660 if SPARK_Mode = On
20661 and then Is_Effectively_Volatile (Discr_Type)
20662 then
20663 Error_Msg_N ("discriminant cannot be volatile", Discr);
20664 end if;
20666 Next (Discr);
20667 end loop;
20669 -- An element list consisting of the default expressions of the
20670 -- discriminants is constructed in the above loop and used to set
20671 -- the Discriminant_Constraint attribute for the type. If an object
20672 -- is declared of this (record or task) type without any explicit
20673 -- discriminant constraint given, this element list will form the
20674 -- actual parameters for the corresponding initialization procedure
20675 -- for the type.
20677 Set_Discriminant_Constraint (Current_Scope, Elist);
20678 Set_Stored_Constraint (Current_Scope, No_Elist);
20680 -- Default expressions must be provided either for all or for none
20681 -- of the discriminants of a discriminant part. (RM 3.7.1)
20683 if Default_Present and then Default_Not_Present then
20684 Error_Msg_N
20685 ("incomplete specification of defaults for discriminants", N);
20686 end if;
20688 -- The use of the name of a discriminant is not allowed in default
20689 -- expressions of a discriminant part if the specification of the
20690 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20692 -- To detect this, the discriminant names are entered initially with an
20693 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20694 -- attempt to use a void entity (for example in an expression that is
20695 -- type-checked) produces the error message: premature usage. Now after
20696 -- completing the semantic analysis of the discriminant part, we can set
20697 -- the Ekind of all the discriminants appropriately.
20699 Discr := First (Discriminant_Specifications (N));
20700 Discr_Number := Uint_1;
20701 while Present (Discr) loop
20702 Id := Defining_Identifier (Discr);
20704 if Ekind (Id) = E_In_Parameter then
20705 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
20706 end if;
20708 Mutate_Ekind (Id, E_Discriminant);
20709 Reinit_Component_Location (Id);
20710 Reinit_Esize (Id);
20711 Set_Discriminant_Number (Id, Discr_Number);
20713 -- Make sure this is always set, even in illegal programs
20715 Set_Corresponding_Discriminant (Id, Empty);
20717 -- Initialize the Original_Record_Component to the entity itself.
20718 -- Inherit_Components will propagate the right value to
20719 -- discriminants in derived record types.
20721 Set_Original_Record_Component (Id, Id);
20723 -- Create the discriminal for the discriminant
20725 Build_Discriminal (Id);
20727 Next (Discr);
20728 Discr_Number := Discr_Number + 1;
20729 end loop;
20731 Set_Has_Discriminants (Current_Scope);
20732 end Process_Discriminants;
20734 -----------------------
20735 -- Process_Full_View --
20736 -----------------------
20738 -- WARNING: This routine manages Ghost regions. Return statements must be
20739 -- replaced by gotos which jump to the end of the routine and restore the
20740 -- Ghost mode.
20742 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20743 procedure Collect_Implemented_Interfaces
20744 (Typ : Entity_Id;
20745 Ifaces : Elist_Id);
20746 -- Ada 2005: Gather all the interfaces that Typ directly or
20747 -- inherently implements. Duplicate entries are not added to
20748 -- the list Ifaces.
20750 ------------------------------------
20751 -- Collect_Implemented_Interfaces --
20752 ------------------------------------
20754 procedure Collect_Implemented_Interfaces
20755 (Typ : Entity_Id;
20756 Ifaces : Elist_Id)
20758 Iface : Entity_Id;
20759 Iface_Elmt : Elmt_Id;
20761 begin
20762 -- Abstract interfaces are only associated with tagged record types
20764 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20765 return;
20766 end if;
20768 -- Recursively climb to the ancestors
20770 if Etype (Typ) /= Typ
20772 -- Protect the frontend against wrong cyclic declarations like:
20774 -- type B is new A with private;
20775 -- type C is new A with private;
20776 -- private
20777 -- type B is new C with null record;
20778 -- type C is new B with null record;
20780 and then Etype (Typ) /= Priv_T
20781 and then Etype (Typ) /= Full_T
20782 then
20783 -- Keep separate the management of private type declarations
20785 if Ekind (Typ) = E_Record_Type_With_Private then
20787 -- Handle the following illegal usage:
20788 -- type Private_Type is tagged private;
20789 -- private
20790 -- type Private_Type is new Type_Implementing_Iface;
20792 if Present (Full_View (Typ))
20793 and then Etype (Typ) /= Full_View (Typ)
20794 then
20795 if Is_Interface (Etype (Typ)) then
20796 Append_Unique_Elmt (Etype (Typ), Ifaces);
20797 end if;
20799 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20800 end if;
20802 -- Non-private types
20804 else
20805 if Is_Interface (Etype (Typ)) then
20806 Append_Unique_Elmt (Etype (Typ), Ifaces);
20807 end if;
20809 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20810 end if;
20811 end if;
20813 -- Handle entities in the list of abstract interfaces
20815 if Present (Interfaces (Typ)) then
20816 Iface_Elmt := First_Elmt (Interfaces (Typ));
20817 while Present (Iface_Elmt) loop
20818 Iface := Node (Iface_Elmt);
20820 pragma Assert (Is_Interface (Iface));
20822 if not Contain_Interface (Iface, Ifaces) then
20823 Append_Elmt (Iface, Ifaces);
20824 Collect_Implemented_Interfaces (Iface, Ifaces);
20825 end if;
20827 Next_Elmt (Iface_Elmt);
20828 end loop;
20829 end if;
20830 end Collect_Implemented_Interfaces;
20832 -- Local variables
20834 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20835 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20836 -- Save the Ghost-related attributes to restore on exit
20838 Full_Indic : Node_Id;
20839 Full_Parent : Entity_Id;
20840 Priv_Parent : Entity_Id;
20842 -- Start of processing for Process_Full_View
20844 begin
20845 Mark_And_Set_Ghost_Completion (N, Priv_T);
20847 -- First some sanity checks that must be done after semantic
20848 -- decoration of the full view and thus cannot be placed with other
20849 -- similar checks in Find_Type_Name
20851 if not Is_Limited_Type (Priv_T)
20852 and then (Is_Limited_Type (Full_T)
20853 or else Is_Limited_Composite (Full_T))
20854 then
20855 if In_Instance then
20856 null;
20857 else
20858 Error_Msg_N
20859 ("completion of nonlimited type cannot be limited", Full_T);
20860 Explain_Limited_Type (Full_T, Full_T);
20861 end if;
20863 elsif Is_Abstract_Type (Full_T)
20864 and then not Is_Abstract_Type (Priv_T)
20865 then
20866 Error_Msg_N
20867 ("completion of nonabstract type cannot be abstract", Full_T);
20869 elsif Is_Tagged_Type (Priv_T)
20870 and then Is_Limited_Type (Priv_T)
20871 and then not Is_Limited_Type (Full_T)
20872 then
20873 -- If pragma CPP_Class was applied to the private declaration
20874 -- propagate the limitedness to the full-view
20876 if Is_CPP_Class (Priv_T) then
20877 Set_Is_Limited_Record (Full_T);
20879 -- GNAT allow its own definition of Limited_Controlled to disobey
20880 -- this rule in order in ease the implementation. This test is safe
20881 -- because Root_Controlled is defined in a child of System that
20882 -- normal programs are not supposed to use.
20884 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20885 Set_Is_Limited_Composite (Full_T);
20886 else
20887 Error_Msg_N
20888 ("completion of limited tagged type must be limited", Full_T);
20889 end if;
20891 elsif Is_Generic_Type (Priv_T) then
20892 Error_Msg_N ("generic type cannot have a completion", Full_T);
20893 end if;
20895 -- Check that ancestor interfaces of private and full views are
20896 -- consistent. We omit this check for synchronized types because
20897 -- they are performed on the corresponding record type when frozen.
20899 if Ada_Version >= Ada_2005
20900 and then Is_Tagged_Type (Priv_T)
20901 and then Is_Tagged_Type (Full_T)
20902 and then not Is_Concurrent_Type (Full_T)
20903 then
20904 declare
20905 Iface : Entity_Id;
20906 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20907 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20909 begin
20910 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20911 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20913 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20914 -- an interface type if and only if the full type is descendant
20915 -- of the interface type (AARM 7.3 (7.3/2)).
20917 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20919 if Present (Iface) then
20920 Error_Msg_NE
20921 ("interface in partial view& not implemented by full type "
20922 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20923 end if;
20925 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20927 if Present (Iface) then
20928 Error_Msg_NE
20929 ("interface & not implemented by partial view "
20930 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20931 end if;
20932 end;
20933 end if;
20935 if Is_Tagged_Type (Priv_T)
20936 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20937 and then Is_Derived_Type (Full_T)
20938 then
20939 Priv_Parent := Etype (Priv_T);
20941 -- The full view of a private extension may have been transformed
20942 -- into an unconstrained derived type declaration and a subtype
20943 -- declaration (see build_derived_record_type for details).
20945 if Nkind (N) = N_Subtype_Declaration then
20946 Full_Indic := Subtype_Indication (N);
20947 Full_Parent := Etype (Base_Type (Full_T));
20948 else
20949 Full_Indic := Subtype_Indication (Type_Definition (N));
20950 Full_Parent := Etype (Full_T);
20951 end if;
20953 -- Check that the parent type of the full type is a descendant of
20954 -- the ancestor subtype given in the private extension. If either
20955 -- entity has an Etype equal to Any_Type then we had some previous
20956 -- error situation [7.3(8)].
20958 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20959 goto Leave;
20961 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20962 -- any order. Therefore we don't have to check that its parent must
20963 -- be a descendant of the parent of the private type declaration.
20965 elsif Is_Interface (Priv_Parent)
20966 and then Is_Interface (Full_Parent)
20967 then
20968 null;
20970 -- Ada 2005 (AI-251): If the parent of the private type declaration
20971 -- is an interface there is no need to check that it is an ancestor
20972 -- of the associated full type declaration. The required tests for
20973 -- this case are performed by Build_Derived_Record_Type.
20975 elsif not Is_Interface (Base_Type (Priv_Parent))
20976 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20977 then
20978 Error_Msg_N
20979 ("parent of full type must descend from parent of private "
20980 & "extension", Full_Indic);
20982 -- First check a formal restriction, and then proceed with checking
20983 -- Ada rules. Since the formal restriction is not a serious error, we
20984 -- don't prevent further error detection for this check, hence the
20985 -- ELSE.
20987 else
20988 -- Check the rules of 7.3(10): if the private extension inherits
20989 -- known discriminants, then the full type must also inherit those
20990 -- discriminants from the same (ancestor) type, and the parent
20991 -- subtype of the full type must be constrained if and only if
20992 -- the ancestor subtype of the private extension is constrained.
20994 if No (Discriminant_Specifications (Parent (Priv_T)))
20995 and then not Has_Unknown_Discriminants (Priv_T)
20996 and then Has_Discriminants (Base_Type (Priv_Parent))
20997 then
20998 declare
20999 Priv_Indic : constant Node_Id :=
21000 Subtype_Indication (Parent (Priv_T));
21002 Priv_Constr : constant Boolean :=
21003 Is_Constrained (Priv_Parent)
21004 or else
21005 Nkind (Priv_Indic) = N_Subtype_Indication
21006 or else
21007 Is_Constrained (Entity (Priv_Indic));
21009 Full_Constr : constant Boolean :=
21010 Is_Constrained (Full_Parent)
21011 or else
21012 Nkind (Full_Indic) = N_Subtype_Indication
21013 or else
21014 Is_Constrained (Entity (Full_Indic));
21016 Priv_Discr : Entity_Id;
21017 Full_Discr : Entity_Id;
21019 begin
21020 Priv_Discr := First_Discriminant (Priv_Parent);
21021 Full_Discr := First_Discriminant (Full_Parent);
21022 while Present (Priv_Discr) and then Present (Full_Discr) loop
21023 if Original_Record_Component (Priv_Discr) =
21024 Original_Record_Component (Full_Discr)
21025 or else
21026 Corresponding_Discriminant (Priv_Discr) =
21027 Corresponding_Discriminant (Full_Discr)
21028 then
21029 null;
21030 else
21031 exit;
21032 end if;
21034 Next_Discriminant (Priv_Discr);
21035 Next_Discriminant (Full_Discr);
21036 end loop;
21038 if Present (Priv_Discr) or else Present (Full_Discr) then
21039 Error_Msg_N
21040 ("full view must inherit discriminants of the parent "
21041 & "type used in the private extension", Full_Indic);
21043 elsif Priv_Constr and then not Full_Constr then
21044 Error_Msg_N
21045 ("parent subtype of full type must be constrained",
21046 Full_Indic);
21048 elsif Full_Constr and then not Priv_Constr then
21049 Error_Msg_N
21050 ("parent subtype of full type must be unconstrained",
21051 Full_Indic);
21052 end if;
21053 end;
21055 -- Check the rules of 7.3(12): if a partial view has neither
21056 -- known or unknown discriminants, then the full type
21057 -- declaration shall define a definite subtype.
21059 elsif not Has_Unknown_Discriminants (Priv_T)
21060 and then not Has_Discriminants (Priv_T)
21061 and then not Is_Constrained (Full_T)
21062 then
21063 Error_Msg_N
21064 ("full view must define a constrained type if partial view "
21065 & "has no discriminants", Full_T);
21066 end if;
21068 -- Do we implement the following properly???
21069 -- If the ancestor subtype of a private extension has constrained
21070 -- discriminants, then the parent subtype of the full view shall
21071 -- impose a statically matching constraint on those discriminants
21072 -- [7.3(13)].
21073 end if;
21075 else
21076 -- For untagged types, verify that a type without discriminants is
21077 -- not completed with an unconstrained type. A separate error message
21078 -- is produced if the full type has defaulted discriminants.
21080 if Is_Definite_Subtype (Priv_T)
21081 and then not Is_Definite_Subtype (Full_T)
21082 then
21083 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21084 Error_Msg_NE
21085 ("full view of& not compatible with declaration#",
21086 Full_T, Priv_T);
21088 if not Is_Tagged_Type (Full_T) then
21089 Error_Msg_N
21090 ("\one is constrained, the other unconstrained", Full_T);
21091 end if;
21092 end if;
21093 end if;
21095 -- AI-419: verify that the use of "limited" is consistent
21097 declare
21098 Orig_Decl : constant Node_Id := Original_Node (N);
21100 begin
21101 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21102 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21103 and then Nkind
21104 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21105 then
21106 if not Limited_Present (Parent (Priv_T))
21107 and then not Synchronized_Present (Parent (Priv_T))
21108 and then Limited_Present (Type_Definition (Orig_Decl))
21109 then
21110 Error_Msg_N
21111 ("full view of non-limited extension cannot be limited", N);
21113 -- Conversely, if the partial view carries the limited keyword,
21114 -- the full view must as well, even if it may be redundant.
21116 elsif Limited_Present (Parent (Priv_T))
21117 and then not Limited_Present (Type_Definition (Orig_Decl))
21118 then
21119 Error_Msg_N
21120 ("full view of limited extension must be explicitly limited",
21122 end if;
21123 end if;
21124 end;
21126 -- Ada 2005 (AI-443): A synchronized private extension must be
21127 -- completed by a task or protected type.
21129 if Ada_Version >= Ada_2005
21130 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21131 and then Synchronized_Present (Parent (Priv_T))
21132 and then not Is_Concurrent_Type (Full_T)
21133 then
21134 Error_Msg_N ("full view of synchronized extension must " &
21135 "be synchronized type", N);
21136 end if;
21138 -- Ada 2005 AI-363: if the full view has discriminants with
21139 -- defaults, it is illegal to declare constrained access subtypes
21140 -- whose designated type is the current type. This allows objects
21141 -- of the type that are declared in the heap to be unconstrained.
21143 if not Has_Unknown_Discriminants (Priv_T)
21144 and then not Has_Discriminants (Priv_T)
21145 and then Has_Defaulted_Discriminants (Full_T)
21146 then
21147 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21148 Set_Has_Constrained_Partial_View (Priv_T);
21149 end if;
21151 -- Create a full declaration for all its subtypes recorded in
21152 -- Private_Dependents and swap them similarly to the base type. These
21153 -- are subtypes that have been define before the full declaration of
21154 -- the private type. We also swap the entry in Private_Dependents list
21155 -- so we can properly restore the private view on exit from the scope.
21157 declare
21158 Priv_Elmt : Elmt_Id;
21159 Priv_Scop : Entity_Id;
21160 Priv : Entity_Id;
21161 Full : Entity_Id;
21163 begin
21164 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21165 while Present (Priv_Elmt) loop
21166 Priv := Node (Priv_Elmt);
21167 Priv_Scop := Scope (Priv);
21169 if Ekind (Priv) in E_Private_Subtype
21170 | E_Limited_Private_Subtype
21171 | E_Record_Subtype_With_Private
21172 then
21173 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21174 Set_Is_Itype (Full);
21175 Set_Parent (Full, Parent (Priv));
21176 Set_Associated_Node_For_Itype (Full, N);
21178 -- Now we need to complete the private subtype, but since the
21179 -- base type has already been swapped, we must also swap the
21180 -- subtypes (and thus, reverse the arguments in the call to
21181 -- Complete_Private_Subtype). Also note that we may need to
21182 -- re-establish the scope of the private subtype.
21184 Copy_And_Swap (Priv, Full);
21186 if not In_Open_Scopes (Priv_Scop) then
21187 Push_Scope (Priv_Scop);
21189 else
21190 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21192 Priv_Scop := Empty;
21193 end if;
21195 Complete_Private_Subtype (Full, Priv, Full_T, N);
21196 Set_Full_View (Full, Priv);
21198 if Present (Priv_Scop) then
21199 Pop_Scope;
21200 end if;
21202 Replace_Elmt (Priv_Elmt, Full);
21203 end if;
21205 Next_Elmt (Priv_Elmt);
21206 end loop;
21207 end;
21209 declare
21210 Disp_Typ : Entity_Id;
21211 Full_List : Elist_Id;
21212 Prim : Entity_Id;
21213 Prim_Elmt : Elmt_Id;
21214 Priv_List : Elist_Id;
21216 function Contains
21217 (E : Entity_Id;
21218 L : Elist_Id) return Boolean;
21219 -- Determine whether list L contains element E
21221 --------------
21222 -- Contains --
21223 --------------
21225 function Contains
21226 (E : Entity_Id;
21227 L : Elist_Id) return Boolean
21229 List_Elmt : Elmt_Id;
21231 begin
21232 List_Elmt := First_Elmt (L);
21233 while Present (List_Elmt) loop
21234 if Node (List_Elmt) = E then
21235 return True;
21236 end if;
21238 Next_Elmt (List_Elmt);
21239 end loop;
21241 return False;
21242 end Contains;
21244 -- Start of processing
21246 begin
21247 -- If the private view was tagged, copy the new primitive operations
21248 -- from the private view to the full view.
21250 if Is_Tagged_Type (Full_T) then
21251 if Is_Tagged_Type (Priv_T) then
21252 Priv_List := Primitive_Operations (Priv_T);
21253 Prim_Elmt := First_Elmt (Priv_List);
21255 -- In the case of a concurrent type completing a private tagged
21256 -- type, primitives may have been declared in between the two
21257 -- views. These subprograms need to be wrapped the same way
21258 -- entries and protected procedures are handled because they
21259 -- cannot be directly shared by the two views.
21261 if Is_Concurrent_Type (Full_T) then
21262 declare
21263 Conc_Typ : constant Entity_Id :=
21264 Corresponding_Record_Type (Full_T);
21265 Curr_Nod : Node_Id := Parent (Conc_Typ);
21266 Wrap_Spec : Node_Id;
21268 begin
21269 while Present (Prim_Elmt) loop
21270 Prim := Node (Prim_Elmt);
21272 if Comes_From_Source (Prim)
21273 and then not Is_Abstract_Subprogram (Prim)
21274 then
21275 Wrap_Spec :=
21276 Make_Subprogram_Declaration (Sloc (Prim),
21277 Specification =>
21278 Build_Wrapper_Spec
21279 (Subp_Id => Prim,
21280 Obj_Typ => Conc_Typ,
21281 Formals =>
21282 Parameter_Specifications
21283 (Parent (Prim))));
21285 Insert_After (Curr_Nod, Wrap_Spec);
21286 Curr_Nod := Wrap_Spec;
21288 Analyze (Wrap_Spec);
21290 -- Remove the wrapper from visibility to avoid
21291 -- spurious conflict with the wrapped entity.
21293 Set_Is_Immediately_Visible
21294 (Defining_Entity (Specification (Wrap_Spec)),
21295 False);
21296 end if;
21298 Next_Elmt (Prim_Elmt);
21299 end loop;
21301 goto Leave;
21302 end;
21304 -- For non-concurrent types, transfer explicit primitives, but
21305 -- omit those inherited from the parent of the private view
21306 -- since they will be re-inherited later on.
21308 else
21309 Full_List := Primitive_Operations (Full_T);
21310 while Present (Prim_Elmt) loop
21311 Prim := Node (Prim_Elmt);
21313 if Comes_From_Source (Prim)
21314 and then not Contains (Prim, Full_List)
21315 then
21316 Append_Elmt (Prim, Full_List);
21317 end if;
21319 Next_Elmt (Prim_Elmt);
21320 end loop;
21321 end if;
21323 -- Untagged private view
21325 else
21326 Full_List := Primitive_Operations (Full_T);
21328 -- In this case the partial view is untagged, so here we locate
21329 -- all of the earlier primitives that need to be treated as
21330 -- dispatching (those that appear between the two views). Note
21331 -- that these additional operations must all be new operations
21332 -- (any earlier operations that override inherited operations
21333 -- of the full view will already have been inserted in the
21334 -- primitives list, marked by Check_Operation_From_Private_View
21335 -- as dispatching. Note that implicit "/=" operators are
21336 -- excluded from being added to the primitives list since they
21337 -- shouldn't be treated as dispatching (tagged "/=" is handled
21338 -- specially).
21340 Prim := Next_Entity (Full_T);
21341 while Present (Prim) and then Prim /= Priv_T loop
21342 if Ekind (Prim) in E_Procedure | E_Function then
21343 Disp_Typ := Find_Dispatching_Type (Prim);
21345 if Disp_Typ = Full_T
21346 and then (Chars (Prim) /= Name_Op_Ne
21347 or else Comes_From_Source (Prim))
21348 then
21349 Check_Controlling_Formals (Full_T, Prim);
21351 if Is_Suitable_Primitive (Prim)
21352 and then not Is_Dispatching_Operation (Prim)
21353 then
21354 Append_Elmt (Prim, Full_List);
21355 Set_Is_Dispatching_Operation (Prim);
21356 Set_DT_Position_Value (Prim, No_Uint);
21357 end if;
21359 elsif Is_Dispatching_Operation (Prim)
21360 and then Disp_Typ /= Full_T
21361 then
21362 -- Verify that it is not otherwise controlled by a
21363 -- formal or a return value of type T.
21365 Check_Controlling_Formals (Disp_Typ, Prim);
21366 end if;
21367 end if;
21369 Next_Entity (Prim);
21370 end loop;
21371 end if;
21373 -- For the tagged case, the two views can share the same primitive
21374 -- operations list and the same class-wide type. Update attributes
21375 -- of the class-wide type which depend on the full declaration.
21377 if Is_Tagged_Type (Priv_T) then
21378 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21379 Set_Class_Wide_Type
21380 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21382 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21383 end if;
21385 -- For untagged types, copy the primitives across from the private
21386 -- view to the full view (when extensions are allowed), for support
21387 -- of prefixed calls (when extensions are enabled).
21389 elsif Extensions_Allowed then
21390 Priv_List := Primitive_Operations (Priv_T);
21391 Prim_Elmt := First_Elmt (Priv_List);
21393 Full_List := Primitive_Operations (Full_T);
21394 while Present (Prim_Elmt) loop
21395 Prim := Node (Prim_Elmt);
21396 Append_Elmt (Prim, Full_List);
21397 Next_Elmt (Prim_Elmt);
21398 end loop;
21399 end if;
21400 end;
21402 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21404 if Known_To_Have_Preelab_Init (Priv_T) then
21406 -- Case where there is a pragma Preelaborable_Initialization. We
21407 -- always allow this in predefined units, which is cheating a bit,
21408 -- but it means we don't have to struggle to meet the requirements in
21409 -- the RM for having Preelaborable Initialization. Otherwise we
21410 -- require that the type meets the RM rules. But we can't check that
21411 -- yet, because of the rule about overriding Initialize, so we simply
21412 -- set a flag that will be checked at freeze time.
21414 if not In_Predefined_Unit (Full_T) then
21415 Set_Must_Have_Preelab_Init (Full_T);
21416 end if;
21417 end if;
21419 -- If pragma CPP_Class was applied to the private type declaration,
21420 -- propagate it now to the full type declaration.
21422 if Is_CPP_Class (Priv_T) then
21423 Set_Is_CPP_Class (Full_T);
21424 Set_Convention (Full_T, Convention_CPP);
21426 -- Check that components of imported CPP types do not have default
21427 -- expressions.
21429 Check_CPP_Type_Has_No_Defaults (Full_T);
21430 end if;
21432 -- If the private view has user specified stream attributes, then so has
21433 -- the full view.
21435 -- Why the test, how could these flags be already set in Full_T ???
21437 if Has_Specified_Stream_Read (Priv_T) then
21438 Set_Has_Specified_Stream_Read (Full_T);
21439 end if;
21441 if Has_Specified_Stream_Write (Priv_T) then
21442 Set_Has_Specified_Stream_Write (Full_T);
21443 end if;
21445 if Has_Specified_Stream_Input (Priv_T) then
21446 Set_Has_Specified_Stream_Input (Full_T);
21447 end if;
21449 if Has_Specified_Stream_Output (Priv_T) then
21450 Set_Has_Specified_Stream_Output (Full_T);
21451 end if;
21453 -- Propagate Default_Initial_Condition-related attributes from the
21454 -- partial view to the full view.
21456 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21458 -- And to the underlying full view, if any
21460 if Is_Private_Type (Full_T)
21461 and then Present (Underlying_Full_View (Full_T))
21462 then
21463 Propagate_DIC_Attributes
21464 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21465 end if;
21467 -- Propagate invariant-related attributes from the partial view to the
21468 -- full view.
21470 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21472 -- And to the underlying full view, if any
21474 if Is_Private_Type (Full_T)
21475 and then Present (Underlying_Full_View (Full_T))
21476 then
21477 Propagate_Invariant_Attributes
21478 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21479 end if;
21481 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21482 -- in the full view without advertising the inheritance in the partial
21483 -- view. This can only occur when the partial view has no parent type
21484 -- and the full view has an interface as a parent. Any other scenarios
21485 -- are illegal because implemented interfaces must match between the
21486 -- two views.
21488 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21489 declare
21490 Full_Par : constant Entity_Id := Etype (Full_T);
21491 Priv_Par : constant Entity_Id := Etype (Priv_T);
21493 begin
21494 if not Is_Interface (Priv_Par)
21495 and then Is_Interface (Full_Par)
21496 and then Has_Inheritable_Invariants (Full_Par)
21497 then
21498 Error_Msg_N
21499 ("hidden inheritance of class-wide type invariants not "
21500 & "allowed", N);
21501 end if;
21502 end;
21503 end if;
21505 -- Propagate predicates to full type, and predicate function if already
21506 -- defined. It is not clear that this can actually happen? the partial
21507 -- view cannot be frozen yet, and the predicate function has not been
21508 -- built. Still it is a cheap check and seems safer to make it.
21510 Propagate_Predicate_Attributes (Full_T, Priv_T);
21512 if Is_Private_Type (Full_T)
21513 and then Present (Underlying_Full_View (Full_T))
21514 then
21515 Propagate_Predicate_Attributes
21516 (Underlying_Full_View (Full_T), Priv_T);
21517 end if;
21519 <<Leave>>
21520 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21521 end Process_Full_View;
21523 -----------------------------------
21524 -- Process_Incomplete_Dependents --
21525 -----------------------------------
21527 procedure Process_Incomplete_Dependents
21528 (N : Node_Id;
21529 Full_T : Entity_Id;
21530 Inc_T : Entity_Id)
21532 Inc_Elmt : Elmt_Id;
21533 Priv_Dep : Entity_Id;
21534 New_Subt : Entity_Id;
21536 Disc_Constraint : Elist_Id;
21538 begin
21539 if No (Private_Dependents (Inc_T)) then
21540 return;
21541 end if;
21543 -- Itypes that may be generated by the completion of an incomplete
21544 -- subtype are not used by the back-end and not attached to the tree.
21545 -- They are created only for constraint-checking purposes.
21547 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21548 while Present (Inc_Elmt) loop
21549 Priv_Dep := Node (Inc_Elmt);
21551 if Ekind (Priv_Dep) = E_Subprogram_Type then
21553 -- An Access_To_Subprogram type may have a return type or a
21554 -- parameter type that is incomplete. Replace with the full view.
21556 if Etype (Priv_Dep) = Inc_T then
21557 Set_Etype (Priv_Dep, Full_T);
21558 end if;
21560 declare
21561 Formal : Entity_Id;
21563 begin
21564 Formal := First_Formal (Priv_Dep);
21565 while Present (Formal) loop
21566 if Etype (Formal) = Inc_T then
21567 Set_Etype (Formal, Full_T);
21568 end if;
21570 Next_Formal (Formal);
21571 end loop;
21572 end;
21574 elsif Is_Overloadable (Priv_Dep) then
21576 -- If a subprogram in the incomplete dependents list is primitive
21577 -- for a tagged full type then mark it as a dispatching operation,
21578 -- check whether it overrides an inherited subprogram, and check
21579 -- restrictions on its controlling formals. Note that a protected
21580 -- operation is never dispatching: only its wrapper operation
21581 -- (which has convention Ada) is.
21583 if Is_Tagged_Type (Full_T)
21584 and then Is_Primitive (Priv_Dep)
21585 and then Convention (Priv_Dep) /= Convention_Protected
21586 then
21587 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21588 Set_Is_Dispatching_Operation (Priv_Dep);
21589 Check_Controlling_Formals (Full_T, Priv_Dep);
21590 end if;
21592 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21594 -- Can happen during processing of a body before the completion
21595 -- of a TA type. Ignore, because spec is also on dependent list.
21597 return;
21599 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21600 -- corresponding subtype of the full view.
21602 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21603 and then Comes_From_Source (Priv_Dep)
21604 then
21605 Set_Subtype_Indication
21606 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21607 Reinit_Field_To_Zero
21608 (Priv_Dep, F_Private_Dependents,
21609 Old_Ekind => E_Incomplete_Subtype);
21610 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21611 Set_Etype (Priv_Dep, Full_T);
21612 Set_Analyzed (Parent (Priv_Dep), False);
21614 -- Reanalyze the declaration, suppressing the call to Enter_Name
21615 -- to avoid duplicate names.
21617 Analyze_Subtype_Declaration
21618 (N => Parent (Priv_Dep),
21619 Skip => True);
21621 -- Dependent is a subtype
21623 else
21624 -- We build a new subtype indication using the full view of the
21625 -- incomplete parent. The discriminant constraints have been
21626 -- elaborated already at the point of the subtype declaration.
21628 New_Subt := Create_Itype (E_Void, N);
21630 if Has_Discriminants (Full_T) then
21631 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21632 else
21633 Disc_Constraint := No_Elist;
21634 end if;
21636 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21637 Set_Full_View (Priv_Dep, New_Subt);
21638 end if;
21640 Next_Elmt (Inc_Elmt);
21641 end loop;
21642 end Process_Incomplete_Dependents;
21644 --------------------------------
21645 -- Process_Range_Expr_In_Decl --
21646 --------------------------------
21648 procedure Process_Range_Expr_In_Decl
21649 (R : Node_Id;
21650 T : Entity_Id;
21651 Subtyp : Entity_Id := Empty;
21652 Check_List : List_Id := No_List)
21654 Lo, Hi : Node_Id;
21655 R_Checks : Check_Result;
21656 Insert_Node : Node_Id;
21657 Def_Id : Entity_Id;
21659 begin
21660 Analyze_And_Resolve (R, Base_Type (T));
21662 if Nkind (R) = N_Range then
21663 Lo := Low_Bound (R);
21664 Hi := High_Bound (R);
21666 -- Validity checks on the range of a quantified expression are
21667 -- delayed until the construct is transformed into a loop.
21669 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21670 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21671 then
21672 null;
21674 -- We need to ensure validity of the bounds here, because if we
21675 -- go ahead and do the expansion, then the expanded code will get
21676 -- analyzed with range checks suppressed and we miss the check.
21678 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21679 -- the temporaries generated by routine Remove_Side_Effects by means
21680 -- of validity checks must use the same names. When a range appears
21681 -- in the parent of a generic, the range is processed with checks
21682 -- disabled as part of the generic context and with checks enabled
21683 -- for code generation purposes. This leads to link issues as the
21684 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21685 -- template sees the temporaries generated by Remove_Side_Effects.
21687 else
21688 Validity_Check_Range (R, Subtyp);
21689 end if;
21691 -- If there were errors in the declaration, try and patch up some
21692 -- common mistakes in the bounds. The cases handled are literals
21693 -- which are Integer where the expected type is Real and vice versa.
21694 -- These corrections allow the compilation process to proceed further
21695 -- along since some basic assumptions of the format of the bounds
21696 -- are guaranteed.
21698 if Etype (R) = Any_Type then
21699 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21700 Rewrite (Lo,
21701 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21703 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21704 Rewrite (Hi,
21705 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21707 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21708 Rewrite (Lo,
21709 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21711 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21712 Rewrite (Hi,
21713 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21714 end if;
21716 Set_Etype (Lo, T);
21717 Set_Etype (Hi, T);
21718 end if;
21720 -- If the bounds of the range have been mistakenly given as string
21721 -- literals (perhaps in place of character literals), then an error
21722 -- has already been reported, but we rewrite the string literal as a
21723 -- bound of the range's type to avoid blowups in later processing
21724 -- that looks at static values.
21726 if Nkind (Lo) = N_String_Literal then
21727 Rewrite (Lo,
21728 Make_Attribute_Reference (Sloc (Lo),
21729 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21730 Attribute_Name => Name_First));
21731 Analyze_And_Resolve (Lo);
21732 end if;
21734 if Nkind (Hi) = N_String_Literal then
21735 Rewrite (Hi,
21736 Make_Attribute_Reference (Sloc (Hi),
21737 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21738 Attribute_Name => Name_First));
21739 Analyze_And_Resolve (Hi);
21740 end if;
21742 -- If bounds aren't scalar at this point then exit, avoiding
21743 -- problems with further processing of the range in this procedure.
21745 if not Is_Scalar_Type (Etype (Lo)) then
21746 return;
21747 end if;
21749 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21750 -- then range of the base type. Here we check whether the bounds
21751 -- are in the range of the subtype itself. Note that if the bounds
21752 -- represent the null range the Constraint_Error exception should
21753 -- not be raised.
21755 -- Capture values of bounds and generate temporaries for them
21756 -- if needed, before applying checks, since checks may cause
21757 -- duplication of the expression without forcing evaluation.
21759 -- The forced evaluation removes side effects from expressions,
21760 -- which should occur also in GNATprove mode. Otherwise, we end up
21761 -- with unexpected insertions of actions at places where this is
21762 -- not supposed to occur, e.g. on default parameters of a call.
21764 if Expander_Active or GNATprove_Mode then
21766 -- Call Force_Evaluation to create declarations as needed
21767 -- to deal with side effects, and also create typ_FIRST/LAST
21768 -- entities for bounds if we have a subtype name.
21770 -- Note: we do this transformation even if expansion is not
21771 -- active if we are in GNATprove_Mode since the transformation
21772 -- is in general required to ensure that the resulting tree has
21773 -- proper Ada semantics.
21775 Force_Evaluation
21776 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21777 Force_Evaluation
21778 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21779 end if;
21781 -- We use a flag here instead of suppressing checks on the type
21782 -- because the type we check against isn't necessarily the place
21783 -- where we put the check.
21785 R_Checks := Get_Range_Checks (R, T);
21787 -- Look up tree to find an appropriate insertion point. We can't
21788 -- just use insert_actions because later processing depends on
21789 -- the insertion node. Prior to Ada 2012 the insertion point could
21790 -- only be a declaration or a loop, but quantified expressions can
21791 -- appear within any context in an expression, and the insertion
21792 -- point can be any statement, pragma, or declaration.
21794 Insert_Node := Parent (R);
21795 while Present (Insert_Node) loop
21796 exit when
21797 Nkind (Insert_Node) in N_Declaration
21798 and then
21799 Nkind (Insert_Node) not in N_Component_Declaration
21800 | N_Loop_Parameter_Specification
21801 | N_Function_Specification
21802 | N_Procedure_Specification;
21804 exit when Nkind (Insert_Node) in
21805 N_Later_Decl_Item |
21806 N_Statement_Other_Than_Procedure_Call |
21807 N_Procedure_Call_Statement |
21808 N_Pragma;
21810 Insert_Node := Parent (Insert_Node);
21811 end loop;
21813 if Present (Insert_Node) then
21815 -- Case of loop statement. Verify that the range is part of the
21816 -- subtype indication of the iteration scheme.
21818 if Nkind (Insert_Node) = N_Loop_Statement then
21819 declare
21820 Indic : Node_Id;
21822 begin
21823 Indic := Parent (R);
21824 while Present (Indic)
21825 and then Nkind (Indic) /= N_Subtype_Indication
21826 loop
21827 Indic := Parent (Indic);
21828 end loop;
21830 if Present (Indic) then
21831 Def_Id := Etype (Subtype_Mark (Indic));
21833 Insert_Range_Checks
21834 (R_Checks,
21835 Insert_Node,
21836 Def_Id,
21837 Sloc (Insert_Node),
21838 Do_Before => True);
21839 end if;
21840 end;
21842 -- Case of declarations. If the declaration is for a type and
21843 -- involves discriminants, the checks are premature at the
21844 -- declaration point and need to wait for the expansion of the
21845 -- initialization procedure, which will pass in the list to put
21846 -- them on; otherwise, the checks are done at the declaration
21847 -- point and there is no need to do them again in the
21848 -- initialization procedure.
21850 elsif Nkind (Insert_Node) in N_Declaration then
21851 Def_Id := Defining_Identifier (Insert_Node);
21853 if (Ekind (Def_Id) = E_Record_Type
21854 and then Depends_On_Discriminant (R))
21855 or else
21856 (Ekind (Def_Id) = E_Protected_Type
21857 and then Has_Discriminants (Def_Id))
21858 then
21859 if Present (Check_List) then
21860 Append_Range_Checks
21861 (R_Checks,
21862 Check_List, Def_Id, Sloc (Insert_Node));
21863 end if;
21865 else
21866 if No (Check_List) then
21867 Insert_Range_Checks
21868 (R_Checks,
21869 Insert_Node, Def_Id, Sloc (Insert_Node));
21870 end if;
21871 end if;
21873 -- Case of statements. Drop the checks, as the range appears in
21874 -- the context of a quantified expression. Insertion will take
21875 -- place when expression is expanded.
21877 else
21878 null;
21879 end if;
21880 end if;
21882 -- Case of other than an explicit N_Range node
21884 -- The forced evaluation removes side effects from expressions, which
21885 -- should occur also in GNATprove mode. Otherwise, we end up with
21886 -- unexpected insertions of actions at places where this is not
21887 -- supposed to occur, e.g. on default parameters of a call.
21889 elsif Expander_Active or GNATprove_Mode then
21890 Get_Index_Bounds (R, Lo, Hi);
21891 Force_Evaluation (Lo);
21892 Force_Evaluation (Hi);
21893 end if;
21894 end Process_Range_Expr_In_Decl;
21896 --------------------------------------
21897 -- Process_Real_Range_Specification --
21898 --------------------------------------
21900 procedure Process_Real_Range_Specification (Def : Node_Id) is
21901 Spec : constant Node_Id := Real_Range_Specification (Def);
21902 Lo : Node_Id;
21903 Hi : Node_Id;
21904 Err : Boolean := False;
21906 procedure Analyze_Bound (N : Node_Id);
21907 -- Analyze and check one bound
21909 -------------------
21910 -- Analyze_Bound --
21911 -------------------
21913 procedure Analyze_Bound (N : Node_Id) is
21914 begin
21915 Analyze_And_Resolve (N, Any_Real);
21917 if not Is_OK_Static_Expression (N) then
21918 Flag_Non_Static_Expr
21919 ("bound in real type definition is not static!", N);
21920 Err := True;
21921 end if;
21922 end Analyze_Bound;
21924 -- Start of processing for Process_Real_Range_Specification
21926 begin
21927 if Present (Spec) then
21928 Lo := Low_Bound (Spec);
21929 Hi := High_Bound (Spec);
21930 Analyze_Bound (Lo);
21931 Analyze_Bound (Hi);
21933 -- If error, clear away junk range specification
21935 if Err then
21936 Set_Real_Range_Specification (Def, Empty);
21937 end if;
21938 end if;
21939 end Process_Real_Range_Specification;
21941 ---------------------
21942 -- Process_Subtype --
21943 ---------------------
21945 function Process_Subtype
21946 (S : Node_Id;
21947 Related_Nod : Node_Id;
21948 Related_Id : Entity_Id := Empty;
21949 Suffix : Character := ' ') return Entity_Id
21951 procedure Check_Incomplete (T : Node_Id);
21952 -- Called to verify that an incomplete type is not used prematurely
21954 ----------------------
21955 -- Check_Incomplete --
21956 ----------------------
21958 procedure Check_Incomplete (T : Node_Id) is
21959 begin
21960 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21962 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21963 and then
21964 not (Ada_Version >= Ada_2005
21965 and then
21966 (Nkind (Parent (T)) = N_Subtype_Declaration
21967 or else (Nkind (Parent (T)) = N_Subtype_Indication
21968 and then Nkind (Parent (Parent (T))) =
21969 N_Subtype_Declaration)))
21970 then
21971 Error_Msg_N ("invalid use of type before its full declaration", T);
21972 end if;
21973 end Check_Incomplete;
21975 -- Local variables
21977 P : Node_Id;
21978 Def_Id : Entity_Id;
21979 Error_Node : Node_Id;
21980 Full_View_Id : Entity_Id;
21981 Subtype_Mark_Id : Entity_Id;
21983 May_Have_Null_Exclusion : Boolean;
21985 -- Start of processing for Process_Subtype
21987 begin
21988 -- Case of no constraints present
21990 if Nkind (S) /= N_Subtype_Indication then
21991 Find_Type (S);
21993 -- No way to proceed if the subtype indication is malformed. This
21994 -- will happen for example when the subtype indication in an object
21995 -- declaration is missing altogether and the expression is analyzed
21996 -- as if it were that indication.
21998 if not Is_Entity_Name (S) then
21999 return Any_Type;
22000 end if;
22002 Check_Incomplete (S);
22003 P := Parent (S);
22005 -- The following mirroring of assertion in Null_Exclusion_Present is
22006 -- ugly, can't we have a range, a static predicate or even a flag???
22008 May_Have_Null_Exclusion :=
22009 Present (P)
22010 and then
22011 Nkind (P) in N_Access_Definition
22012 | N_Access_Function_Definition
22013 | N_Access_Procedure_Definition
22014 | N_Access_To_Object_Definition
22015 | N_Allocator
22016 | N_Component_Definition
22017 | N_Derived_Type_Definition
22018 | N_Discriminant_Specification
22019 | N_Formal_Object_Declaration
22020 | N_Function_Specification
22021 | N_Object_Declaration
22022 | N_Object_Renaming_Declaration
22023 | N_Parameter_Specification
22024 | N_Subtype_Declaration;
22026 -- Ada 2005 (AI-231): Static check
22028 if Ada_Version >= Ada_2005
22029 and then May_Have_Null_Exclusion
22030 and then Null_Exclusion_Present (P)
22031 and then Nkind (P) /= N_Access_To_Object_Definition
22032 and then not Is_Access_Type (Entity (S))
22033 then
22034 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22035 end if;
22037 -- Create an Itype that is a duplicate of Entity (S) but with the
22038 -- null-exclusion attribute.
22040 if May_Have_Null_Exclusion
22041 and then Is_Access_Type (Entity (S))
22042 and then Null_Exclusion_Present (P)
22044 -- No need to check the case of an access to object definition.
22045 -- It is correct to define double not-null pointers.
22047 -- Example:
22048 -- type Not_Null_Int_Ptr is not null access Integer;
22049 -- type Acc is not null access Not_Null_Int_Ptr;
22051 and then Nkind (P) /= N_Access_To_Object_Definition
22052 then
22053 if Can_Never_Be_Null (Entity (S)) then
22054 case Nkind (Related_Nod) is
22055 when N_Full_Type_Declaration =>
22056 if Nkind (Type_Definition (Related_Nod))
22057 in N_Array_Type_Definition
22058 then
22059 Error_Node :=
22060 Subtype_Indication
22061 (Component_Definition
22062 (Type_Definition (Related_Nod)));
22063 else
22064 Error_Node :=
22065 Subtype_Indication (Type_Definition (Related_Nod));
22066 end if;
22068 when N_Subtype_Declaration =>
22069 Error_Node := Subtype_Indication (Related_Nod);
22071 when N_Object_Declaration =>
22072 Error_Node := Object_Definition (Related_Nod);
22074 when N_Component_Declaration =>
22075 Error_Node :=
22076 Subtype_Indication (Component_Definition (Related_Nod));
22078 when N_Allocator =>
22079 Error_Node := Expression (Related_Nod);
22081 when others =>
22082 pragma Assert (False);
22083 Error_Node := Related_Nod;
22084 end case;
22086 Error_Msg_NE
22087 ("`NOT NULL` not allowed (& already excludes null)",
22088 Error_Node,
22089 Entity (S));
22090 end if;
22092 Set_Etype (S,
22093 Create_Null_Excluding_Itype
22094 (T => Entity (S),
22095 Related_Nod => P));
22096 Set_Entity (S, Etype (S));
22097 end if;
22099 return Entity (S);
22101 -- Case of constraint present, so that we have an N_Subtype_Indication
22102 -- node (this node is created only if constraints are present).
22104 else
22105 Find_Type (Subtype_Mark (S));
22107 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22108 and then not
22109 (Nkind (Parent (S)) = N_Subtype_Declaration
22110 and then Is_Itype (Defining_Identifier (Parent (S))))
22111 then
22112 Check_Incomplete (Subtype_Mark (S));
22113 end if;
22115 P := Parent (S);
22116 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22118 -- Explicit subtype declaration case
22120 if Nkind (P) = N_Subtype_Declaration then
22121 Def_Id := Defining_Identifier (P);
22123 -- Explicit derived type definition case
22125 elsif Nkind (P) = N_Derived_Type_Definition then
22126 Def_Id := Defining_Identifier (Parent (P));
22128 -- Implicit case, the Def_Id must be created as an implicit type.
22129 -- The one exception arises in the case of concurrent types, array
22130 -- and access types, where other subsidiary implicit types may be
22131 -- created and must appear before the main implicit type. In these
22132 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22133 -- has not yet been called to create Def_Id.
22135 else
22136 if Is_Array_Type (Subtype_Mark_Id)
22137 or else Is_Concurrent_Type (Subtype_Mark_Id)
22138 or else Is_Access_Type (Subtype_Mark_Id)
22139 then
22140 Def_Id := Empty;
22142 -- For the other cases, we create a new unattached Itype,
22143 -- and set the indication to ensure it gets attached later.
22145 else
22146 Def_Id :=
22147 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22148 end if;
22149 end if;
22151 -- If the kind of constraint is invalid for this kind of type,
22152 -- then give an error, and then pretend no constraint was given.
22154 if not Is_Valid_Constraint_Kind
22155 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22156 then
22157 Error_Msg_N
22158 ("incorrect constraint for this kind of type", Constraint (S));
22160 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22162 -- Set Ekind of orphan itype, to prevent cascaded errors
22164 if Present (Def_Id) then
22165 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22166 end if;
22168 -- Make recursive call, having got rid of the bogus constraint
22170 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22171 end if;
22173 -- Remaining processing depends on type. Select on Base_Type kind to
22174 -- ensure getting to the concrete type kind in the case of a private
22175 -- subtype (needed when only doing semantic analysis).
22177 case Ekind (Base_Type (Subtype_Mark_Id)) is
22178 when Access_Kind =>
22180 -- If this is a constraint on a class-wide type, discard it.
22181 -- There is currently no way to express a partial discriminant
22182 -- constraint on a type with unknown discriminants. This is
22183 -- a pathology that the ACATS wisely decides not to test.
22185 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22186 if Comes_From_Source (S) then
22187 Error_Msg_N
22188 ("constraint on class-wide type ignored??",
22189 Constraint (S));
22190 end if;
22192 if Nkind (P) = N_Subtype_Declaration then
22193 Set_Subtype_Indication (P,
22194 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22195 end if;
22197 return Subtype_Mark_Id;
22198 end if;
22200 Constrain_Access (Def_Id, S, Related_Nod);
22202 if Expander_Active
22203 and then Is_Itype (Designated_Type (Def_Id))
22204 and then Nkind (Related_Nod) = N_Subtype_Declaration
22205 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22206 then
22207 Build_Itype_Reference
22208 (Designated_Type (Def_Id), Related_Nod);
22209 end if;
22211 when Array_Kind =>
22212 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22214 when Decimal_Fixed_Point_Kind =>
22215 Constrain_Decimal (Def_Id, S);
22217 when Enumeration_Kind =>
22218 Constrain_Enumeration (Def_Id, S);
22220 when Ordinary_Fixed_Point_Kind =>
22221 Constrain_Ordinary_Fixed (Def_Id, S);
22223 when Float_Kind =>
22224 Constrain_Float (Def_Id, S);
22226 when Integer_Kind =>
22227 Constrain_Integer (Def_Id, S);
22229 when Class_Wide_Kind
22230 | E_Incomplete_Type
22231 | E_Record_Subtype
22232 | E_Record_Type
22234 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22236 if Ekind (Def_Id) = E_Incomplete_Type then
22237 Set_Private_Dependents (Def_Id, New_Elmt_List);
22238 end if;
22240 when Private_Kind =>
22242 -- A private type with unknown discriminants may be completed
22243 -- by an unconstrained array type.
22245 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22246 and then Present (Full_View (Subtype_Mark_Id))
22247 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22248 then
22249 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22251 -- ... but more commonly is completed by a discriminated record
22252 -- type.
22254 else
22255 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22256 end if;
22258 -- The base type may be private but Def_Id may be a full view
22259 -- in an instance.
22261 if Is_Private_Type (Def_Id) then
22262 Set_Private_Dependents (Def_Id, New_Elmt_List);
22263 end if;
22265 -- In case of an invalid constraint prevent further processing
22266 -- since the type constructed is missing expected fields.
22268 if Etype (Def_Id) = Any_Type then
22269 return Def_Id;
22270 end if;
22272 -- If the full view is that of a task with discriminants,
22273 -- we must constrain both the concurrent type and its
22274 -- corresponding record type. Otherwise we will just propagate
22275 -- the constraint to the full view, if available.
22277 if Present (Full_View (Subtype_Mark_Id))
22278 and then Has_Discriminants (Subtype_Mark_Id)
22279 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22280 then
22281 Full_View_Id :=
22282 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22284 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22285 Constrain_Concurrent (Full_View_Id, S,
22286 Related_Nod, Related_Id, Suffix);
22287 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22288 Set_Full_View (Def_Id, Full_View_Id);
22290 -- Introduce an explicit reference to the private subtype,
22291 -- to prevent scope anomalies in gigi if first use appears
22292 -- in a nested context, e.g. a later function body.
22293 -- Should this be generated in other contexts than a full
22294 -- type declaration?
22296 if Is_Itype (Def_Id)
22297 and then
22298 Nkind (Parent (P)) = N_Full_Type_Declaration
22299 then
22300 Build_Itype_Reference (Def_Id, Parent (P));
22301 end if;
22303 else
22304 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22305 end if;
22307 when Concurrent_Kind =>
22308 Constrain_Concurrent (Def_Id, S,
22309 Related_Nod, Related_Id, Suffix);
22311 when others =>
22312 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22313 end case;
22315 -- Size, Alignment, Representation aspects and Convention are always
22316 -- inherited from the base type.
22318 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22319 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22320 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22322 -- The anonymous subtype created for the subtype indication
22323 -- inherits the predicates of the parent.
22325 if Has_Predicates (Subtype_Mark_Id) then
22326 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22328 -- Indicate where the predicate function may be found
22330 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22331 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22332 end if;
22333 end if;
22335 return Def_Id;
22336 end if;
22337 end Process_Subtype;
22339 -----------------------------
22340 -- Record_Type_Declaration --
22341 -----------------------------
22343 procedure Record_Type_Declaration
22344 (T : Entity_Id;
22345 N : Node_Id;
22346 Prev : Entity_Id)
22348 Def : constant Node_Id := Type_Definition (N);
22349 Is_Tagged : Boolean;
22350 Tag_Comp : Entity_Id;
22352 begin
22353 -- These flags must be initialized before calling Process_Discriminants
22354 -- because this routine makes use of them.
22356 Mutate_Ekind (T, E_Record_Type);
22357 Set_Etype (T, T);
22358 Reinit_Size_Align (T);
22359 Set_Interfaces (T, No_Elist);
22360 Set_Stored_Constraint (T, No_Elist);
22361 Set_Default_SSO (T);
22362 Set_No_Reordering (T, No_Component_Reordering);
22364 -- Normal case
22366 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22367 -- The flag Is_Tagged_Type might have already been set by
22368 -- Find_Type_Name if it detected an error for declaration T. This
22369 -- arises in the case of private tagged types where the full view
22370 -- omits the word tagged.
22372 Is_Tagged :=
22373 Tagged_Present (Def)
22374 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22376 Set_Is_Limited_Record (T, Limited_Present (Def));
22378 if Is_Tagged then
22379 Set_Is_Tagged_Type (T, True);
22380 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22381 end if;
22383 -- Type is abstract if full declaration carries keyword, or if
22384 -- previous partial view did.
22386 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22387 or else Abstract_Present (Def));
22389 else
22390 Is_Tagged := True;
22391 Analyze_Interface_Declaration (T, Def);
22393 if Present (Discriminant_Specifications (N)) then
22394 Error_Msg_N
22395 ("interface types cannot have discriminants",
22396 Defining_Identifier
22397 (First (Discriminant_Specifications (N))));
22398 end if;
22399 end if;
22401 -- First pass: if there are self-referential access components,
22402 -- create the required anonymous access type declarations, and if
22403 -- need be an incomplete type declaration for T itself.
22405 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22407 if Ada_Version >= Ada_2005
22408 and then Present (Interface_List (Def))
22409 then
22410 Check_Interfaces (N, Def);
22412 declare
22413 Ifaces_List : Elist_Id;
22415 begin
22416 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22417 -- already in the parents.
22419 Collect_Interfaces
22420 (T => T,
22421 Ifaces_List => Ifaces_List,
22422 Exclude_Parents => True);
22424 Set_Interfaces (T, Ifaces_List);
22425 end;
22426 end if;
22428 -- Records constitute a scope for the component declarations within.
22429 -- The scope is created prior to the processing of these declarations.
22430 -- Discriminants are processed first, so that they are visible when
22431 -- processing the other components. The Ekind of the record type itself
22432 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22434 -- Enter record scope
22436 Push_Scope (T);
22438 -- If an incomplete or private type declaration was already given for
22439 -- the type, then this scope already exists, and the discriminants have
22440 -- been declared within. We must verify that the full declaration
22441 -- matches the incomplete one.
22443 Check_Or_Process_Discriminants (N, T, Prev);
22445 Set_Is_Constrained (T, not Has_Discriminants (T));
22446 Set_Has_Delayed_Freeze (T, True);
22448 -- For tagged types add a manually analyzed component corresponding
22449 -- to the component _tag, the corresponding piece of tree will be
22450 -- expanded as part of the freezing actions if it is not a CPP_Class.
22452 if Is_Tagged then
22454 -- Do not add the tag unless we are in expansion mode
22456 if Expander_Active then
22457 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22458 Enter_Name (Tag_Comp);
22460 Mutate_Ekind (Tag_Comp, E_Component);
22461 Set_Is_Tag (Tag_Comp);
22462 Set_Is_Aliased (Tag_Comp);
22463 Set_Is_Independent (Tag_Comp);
22464 Set_Etype (Tag_Comp, RTE (RE_Tag));
22465 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22466 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22467 Reinit_Component_Location (Tag_Comp);
22469 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22470 -- implemented interfaces.
22472 if Has_Interfaces (T) then
22473 Add_Interface_Tag_Components (N, T);
22474 end if;
22475 end if;
22477 Make_Class_Wide_Type (T);
22478 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22479 end if;
22481 -- We must suppress range checks when processing record components in
22482 -- the presence of discriminants, since we don't want spurious checks to
22483 -- be generated during their analysis, but Suppress_Range_Checks flags
22484 -- must be reset the after processing the record definition.
22486 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22487 -- couldn't we just use the normal range check suppression method here.
22488 -- That would seem cleaner ???
22490 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22491 Set_Kill_Range_Checks (T, True);
22492 Record_Type_Definition (Def, Prev);
22493 Set_Kill_Range_Checks (T, False);
22494 else
22495 Record_Type_Definition (Def, Prev);
22496 end if;
22498 -- Exit from record scope
22500 End_Scope;
22502 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22503 -- the implemented interfaces and associate them an aliased entity.
22505 if Is_Tagged
22506 and then not Is_Empty_List (Interface_List (Def))
22507 then
22508 Derive_Progenitor_Subprograms (T, T);
22509 end if;
22511 Check_Function_Writable_Actuals (N);
22512 end Record_Type_Declaration;
22514 ----------------------------
22515 -- Record_Type_Definition --
22516 ----------------------------
22518 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22519 Component : Entity_Id;
22520 Ctrl_Components : Boolean := False;
22521 Final_Storage_Only : Boolean;
22522 T : Entity_Id;
22524 begin
22525 if Ekind (Prev_T) = E_Incomplete_Type then
22526 T := Full_View (Prev_T);
22527 else
22528 T := Prev_T;
22529 end if;
22531 Final_Storage_Only := not Is_Controlled (T);
22533 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22534 -- type declaration.
22536 if Parent_Kind (Def) = N_Derived_Type_Definition
22537 and then Limited_Present (Parent (Def))
22538 then
22539 Set_Is_Limited_Record (T);
22540 end if;
22542 -- If the component list of a record type is defined by the reserved
22543 -- word null and there is no discriminant part, then the record type has
22544 -- no components and all records of the type are null records (RM 3.7)
22545 -- This procedure is also called to process the extension part of a
22546 -- record extension, in which case the current scope may have inherited
22547 -- components.
22549 if Present (Def)
22550 and then Present (Component_List (Def))
22551 and then not Null_Present (Component_List (Def))
22552 then
22553 Analyze_Declarations (Component_Items (Component_List (Def)));
22555 if Present (Variant_Part (Component_List (Def))) then
22556 Analyze (Variant_Part (Component_List (Def)));
22557 end if;
22558 end if;
22560 -- After completing the semantic analysis of the record definition,
22561 -- record components, both new and inherited, are accessible. Set their
22562 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22563 -- whose Ekind may be void.
22565 Component := First_Entity (Current_Scope);
22566 while Present (Component) loop
22567 if Ekind (Component) = E_Void
22568 and then not Is_Itype (Component)
22569 then
22570 Mutate_Ekind (Component, E_Component);
22571 Reinit_Component_Location (Component);
22572 end if;
22574 Propagate_Concurrent_Flags (T, Etype (Component));
22576 if Ekind (Component) /= E_Component then
22577 null;
22579 -- Do not set Has_Controlled_Component on a class-wide equivalent
22580 -- type. See Make_CW_Equivalent_Type.
22582 elsif not Is_Class_Wide_Equivalent_Type (T)
22583 and then (Has_Controlled_Component (Etype (Component))
22584 or else (Chars (Component) /= Name_uParent
22585 and then Is_Controlled (Etype (Component))))
22586 then
22587 Set_Has_Controlled_Component (T, True);
22588 Final_Storage_Only :=
22589 Final_Storage_Only
22590 and then Finalize_Storage_Only (Etype (Component));
22591 Ctrl_Components := True;
22592 end if;
22594 Next_Entity (Component);
22595 end loop;
22597 -- A Type is Finalize_Storage_Only only if all its controlled components
22598 -- are also.
22600 if Ctrl_Components then
22601 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22602 end if;
22604 -- Place reference to end record on the proper entity, which may
22605 -- be a partial view.
22607 if Present (Def) then
22608 Process_End_Label (Def, 'e', Prev_T);
22609 end if;
22610 end Record_Type_Definition;
22612 ---------------------------
22613 -- Replace_Discriminants --
22614 ---------------------------
22616 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22617 function Process (N : Node_Id) return Traverse_Result;
22619 -------------
22620 -- Process --
22621 -------------
22623 function Process (N : Node_Id) return Traverse_Result is
22624 Comp : Entity_Id;
22626 begin
22627 if Nkind (N) = N_Discriminant_Specification then
22628 Comp := First_Discriminant (Typ);
22629 while Present (Comp) loop
22630 if Original_Record_Component (Comp) = Defining_Identifier (N)
22631 or else Chars (Comp) = Chars (Defining_Identifier (N))
22632 then
22633 Set_Defining_Identifier (N, Comp);
22634 exit;
22635 end if;
22637 Next_Discriminant (Comp);
22638 end loop;
22640 elsif Nkind (N) = N_Variant_Part then
22641 Comp := First_Discriminant (Typ);
22642 while Present (Comp) loop
22643 if Original_Record_Component (Comp) = Entity (Name (N))
22644 or else Chars (Comp) = Chars (Name (N))
22645 then
22646 -- Make sure to preserve the type coming from the parent on
22647 -- the Name, even if the subtype of the discriminant can be
22648 -- constrained, so that discrete choices inherited from the
22649 -- parent in the variant part are not flagged as violating
22650 -- the constraints of the subtype.
22652 declare
22653 Typ : constant Entity_Id := Etype (Name (N));
22654 begin
22655 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22656 Set_Etype (Name (N), Typ);
22657 end;
22658 exit;
22659 end if;
22661 Next_Discriminant (Comp);
22662 end loop;
22663 end if;
22665 return OK;
22666 end Process;
22668 procedure Replace is new Traverse_Proc (Process);
22670 -- Start of processing for Replace_Discriminants
22672 begin
22673 Replace (Decl);
22674 end Replace_Discriminants;
22676 -------------------------------
22677 -- Set_Completion_Referenced --
22678 -------------------------------
22680 procedure Set_Completion_Referenced (E : Entity_Id) is
22681 begin
22682 -- If in main unit, mark entity that is a completion as referenced,
22683 -- warnings go on the partial view when needed.
22685 if In_Extended_Main_Source_Unit (E) then
22686 Set_Referenced (E);
22687 end if;
22688 end Set_Completion_Referenced;
22690 ---------------------
22691 -- Set_Default_SSO --
22692 ---------------------
22694 procedure Set_Default_SSO (T : Entity_Id) is
22695 begin
22696 case Opt.Default_SSO is
22697 when ' ' =>
22698 null;
22699 when 'L' =>
22700 Set_SSO_Set_Low_By_Default (T, True);
22701 when 'H' =>
22702 Set_SSO_Set_High_By_Default (T, True);
22703 when others =>
22704 raise Program_Error;
22705 end case;
22706 end Set_Default_SSO;
22708 ---------------------
22709 -- Set_Fixed_Range --
22710 ---------------------
22712 -- The range for fixed-point types is complicated by the fact that we
22713 -- do not know the exact end points at the time of the declaration. This
22714 -- is true for three reasons:
22716 -- A size clause may affect the fudging of the end-points.
22717 -- A small clause may affect the values of the end-points.
22718 -- We try to include the end-points if it does not affect the size.
22720 -- This means that the actual end-points must be established at the
22721 -- point when the type is frozen. Meanwhile, we first narrow the range
22722 -- as permitted (so that it will fit if necessary in a small specified
22723 -- size), and then build a range subtree with these narrowed bounds.
22724 -- Set_Fixed_Range constructs the range from real literal values, and
22725 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22727 -- The parent of this range is set to point to the entity so that it is
22728 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22729 -- other scalar types, which are just pointers to the range in the
22730 -- original tree, this would otherwise be an orphan).
22732 -- The tree is left unanalyzed. When the type is frozen, the processing
22733 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22734 -- analyzed, and uses this as an indication that it should complete
22735 -- work on the range (it will know the final small and size values).
22737 procedure Set_Fixed_Range
22738 (E : Entity_Id;
22739 Loc : Source_Ptr;
22740 Lo : Ureal;
22741 Hi : Ureal)
22743 S : constant Node_Id :=
22744 Make_Range (Loc,
22745 Low_Bound => Make_Real_Literal (Loc, Lo),
22746 High_Bound => Make_Real_Literal (Loc, Hi));
22747 begin
22748 Set_Scalar_Range (E, S);
22749 Set_Parent (S, E);
22751 -- Before the freeze point, the bounds of a fixed point are universal
22752 -- and carry the corresponding type.
22754 Set_Etype (Low_Bound (S), Universal_Real);
22755 Set_Etype (High_Bound (S), Universal_Real);
22756 end Set_Fixed_Range;
22758 ----------------------------------
22759 -- Set_Scalar_Range_For_Subtype --
22760 ----------------------------------
22762 procedure Set_Scalar_Range_For_Subtype
22763 (Def_Id : Entity_Id;
22764 R : Node_Id;
22765 Subt : Entity_Id)
22767 Kind : constant Entity_Kind := Ekind (Def_Id);
22769 begin
22770 -- Defend against previous error
22772 if Nkind (R) = N_Error then
22773 return;
22774 end if;
22776 Set_Scalar_Range (Def_Id, R);
22778 -- We need to link the range into the tree before resolving it so
22779 -- that types that are referenced, including importantly the subtype
22780 -- itself, are properly frozen (Freeze_Expression requires that the
22781 -- expression be properly linked into the tree). Of course if it is
22782 -- already linked in, then we do not disturb the current link.
22784 if No (Parent (R)) then
22785 Set_Parent (R, Def_Id);
22786 end if;
22788 -- Reset the kind of the subtype during analysis of the range, to
22789 -- catch possible premature use in the bounds themselves.
22791 Mutate_Ekind (Def_Id, E_Void);
22792 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22793 Mutate_Ekind (Def_Id, Kind);
22794 end Set_Scalar_Range_For_Subtype;
22796 --------------------------------------------------------
22797 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22798 --------------------------------------------------------
22800 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22801 (E : Entity_Id)
22803 begin
22804 -- Make sure set if encountered during Expand_To_Stored_Constraint
22806 Set_Stored_Constraint (E, No_Elist);
22808 -- Give it the right value
22810 if Is_Constrained (E) and then Has_Discriminants (E) then
22811 Set_Stored_Constraint (E,
22812 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22813 end if;
22814 end Set_Stored_Constraint_From_Discriminant_Constraint;
22816 -------------------------------------
22817 -- Signed_Integer_Type_Declaration --
22818 -------------------------------------
22820 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22821 Implicit_Base : Entity_Id;
22822 Base_Typ : Entity_Id;
22823 Lo_Val : Uint;
22824 Hi_Val : Uint;
22825 Errs : Boolean := False;
22826 Lo : Node_Id;
22827 Hi : Node_Id;
22829 function Can_Derive_From (E : Entity_Id) return Boolean;
22830 -- Determine whether given bounds allow derivation from specified type
22832 procedure Check_Bound (Expr : Node_Id);
22833 -- Check bound to make sure it is integral and static. If not, post
22834 -- appropriate error message and set Errs flag
22836 ---------------------
22837 -- Can_Derive_From --
22838 ---------------------
22840 -- Note we check both bounds against both end values, to deal with
22841 -- strange types like ones with a range of 0 .. -12341234.
22843 function Can_Derive_From (E : Entity_Id) return Boolean is
22844 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22845 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22846 begin
22847 return Lo <= Lo_Val and then Lo_Val <= Hi
22848 and then
22849 Lo <= Hi_Val and then Hi_Val <= Hi;
22850 end Can_Derive_From;
22852 -----------------
22853 -- Check_Bound --
22854 -----------------
22856 procedure Check_Bound (Expr : Node_Id) is
22857 begin
22858 -- If a range constraint is used as an integer type definition, each
22859 -- bound of the range must be defined by a static expression of some
22860 -- integer type, but the two bounds need not have the same integer
22861 -- type (Negative bounds are allowed.) (RM 3.5.4)
22863 if not Is_Integer_Type (Etype (Expr)) then
22864 Error_Msg_N
22865 ("integer type definition bounds must be of integer type", Expr);
22866 Errs := True;
22868 elsif not Is_OK_Static_Expression (Expr) then
22869 Flag_Non_Static_Expr
22870 ("non-static expression used for integer type bound!", Expr);
22871 Errs := True;
22873 -- Otherwise the bounds are folded into literals
22875 elsif Is_Entity_Name (Expr) then
22876 Fold_Uint (Expr, Expr_Value (Expr), True);
22877 end if;
22878 end Check_Bound;
22880 -- Start of processing for Signed_Integer_Type_Declaration
22882 begin
22883 -- Create an anonymous base type
22885 Implicit_Base :=
22886 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22888 -- Analyze and check the bounds, they can be of any integer type
22890 Lo := Low_Bound (Def);
22891 Hi := High_Bound (Def);
22893 -- Arbitrarily use Integer as the type if either bound had an error
22895 if Hi = Error or else Lo = Error then
22896 Base_Typ := Any_Integer;
22897 Set_Error_Posted (T, True);
22898 Errs := True;
22900 -- Here both bounds are OK expressions
22902 else
22903 Analyze_And_Resolve (Lo, Any_Integer);
22904 Analyze_And_Resolve (Hi, Any_Integer);
22906 Check_Bound (Lo);
22907 Check_Bound (Hi);
22909 if Errs then
22910 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
22911 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
22912 end if;
22914 -- Find type to derive from
22916 Lo_Val := Expr_Value (Lo);
22917 Hi_Val := Expr_Value (Hi);
22919 if Can_Derive_From (Standard_Short_Short_Integer) then
22920 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22922 elsif Can_Derive_From (Standard_Short_Integer) then
22923 Base_Typ := Base_Type (Standard_Short_Integer);
22925 elsif Can_Derive_From (Standard_Integer) then
22926 Base_Typ := Base_Type (Standard_Integer);
22928 elsif Can_Derive_From (Standard_Long_Integer) then
22929 Base_Typ := Base_Type (Standard_Long_Integer);
22931 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22932 Check_Restriction (No_Long_Long_Integers, Def);
22933 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22935 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
22936 Check_Restriction (No_Long_Long_Integers, Def);
22937 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
22939 else
22940 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
22941 Error_Msg_N ("integer type definition bounds out of range", Def);
22942 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
22943 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
22944 end if;
22945 end if;
22947 -- Set the type of the bounds to the implicit base: we cannot set it to
22948 -- the new type, because this would be a forward reference for the code
22949 -- generator and, if the original type is user-defined, this could even
22950 -- lead to spurious semantic errors. Furthermore we do not set it to be
22951 -- universal, because this could make it much larger than needed here.
22953 if not Errs then
22954 Set_Etype (Lo, Implicit_Base);
22955 Set_Etype (Hi, Implicit_Base);
22956 end if;
22958 -- Complete both implicit base and declared first subtype entities. The
22959 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22960 -- are not clobbered when the signed integer type acts as a full view of
22961 -- a private type.
22963 Set_Etype (Implicit_Base, Base_Typ);
22964 Set_Size_Info (Implicit_Base, Base_Typ);
22965 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22966 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22967 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22969 Mutate_Ekind (T, E_Signed_Integer_Subtype);
22970 Set_Etype (T, Implicit_Base);
22971 Set_Size_Info (T, Implicit_Base);
22972 Inherit_Rep_Item_Chain (T, Implicit_Base);
22973 Set_Scalar_Range (T, Def);
22974 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22975 Set_Is_Constrained (T);
22976 end Signed_Integer_Type_Declaration;
22978 end Sem_Ch3;