Debian Codegraph @

gcc/ada/sem_ch3.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Debug; use Debug;
32 with Elists; use Elists;
33 with Einfo; use Einfo;
34 with Einfo.Entities; use Einfo.Entities;
35 with Einfo.Utils; use Einfo.Utils;
36 with Errout; use Errout;
37 with Eval_Fat; use Eval_Fat;
38 with Exp_Ch3; use Exp_Ch3;
39 with Exp_Ch9; use Exp_Ch9;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Expander; use Expander;
45 with Fmap;
46 with Freeze; use Freeze;
47 with Ghost; use Ghost;
48 with Itypes; use Itypes;
49 with Layout; use Layout;
50 with Lib; use Lib;
51 with Lib.Writ;
52 with Lib.Xref; use Lib.Xref;
53 with Mutably_Tagged; use Mutably_Tagged;
54 with Namet; use Namet;
55 with Nlists; use Nlists;
56 with Nmake; use Nmake;
57 with Opt; use Opt;
58 with Osint;
59 with Restrict; use Restrict;
60 with Rident; use Rident;
61 with Rtsfind; use Rtsfind;
62 with Sem; use Sem;
63 with Sem_Aux; use Sem_Aux;
64 with Sem_Case; use Sem_Case;
65 with Sem_Cat; use Sem_Cat;
66 with Sem_Ch6; use Sem_Ch6;
67 with Sem_Ch7; use Sem_Ch7;
68 with Sem_Ch8; use Sem_Ch8;
69 with Sem_Ch10; use Sem_Ch10;
70 with Sem_Ch13; use Sem_Ch13;
71 with Sem_Dim; use Sem_Dim;
72 with Sem_Disp; use Sem_Disp;
73 with Sem_Dist; use Sem_Dist;
74 with Sem_Elab; use Sem_Elab;
75 with Sem_Elim; use Sem_Elim;
76 with Sem_Eval; use Sem_Eval;
77 with Sem_Mech; use Sem_Mech;
78 with Sem_Res; use Sem_Res;
79 with Sem_Smem; use Sem_Smem;
80 with Sem_Type; use Sem_Type;
81 with Sem_Util; use Sem_Util;
82 with Sem_Warn; use Sem_Warn;
83 with Stand; use Stand;
84 with Sinfo; use Sinfo;
85 with Sinfo.Nodes; use Sinfo.Nodes;
86 with Sinfo.Utils; use Sinfo.Utils;
87 with Sinput; use Sinput;
88 with Sinput.L;
89 with Snames; use Snames;
90 with Stringt;
91 with Strub; use Strub;
92 with System.OS_Lib;
93 with Targparm; use Targparm;
94 with Tbuild; use Tbuild;
95 with Ttypes; use Ttypes;
96 with Uintp; use Uintp;
97 with Urealp; use Urealp;
98 with Warnsw; use Warnsw;
99
100 package body Sem_Ch3 is
101
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
105
106 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
107 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
108 -- abstract interface types implemented by a record type or a derived
109 -- record type.
110
111 procedure Build_Derived_Type
112 (N : Node_Id;
113 Parent_Type : Entity_Id;
114 Derived_Type : Entity_Id;
115 Is_Completion : Boolean;
116 Derive_Subps : Boolean := True);
117 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
118 -- the N_Full_Type_Declaration node containing the derived type definition.
119 -- Parent_Type is the entity for the parent type in the derived type
120 -- definition and Derived_Type the actual derived type. Is_Completion must
121 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
122 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
123 -- completion of a private type declaration. If Is_Completion is set to
124 -- True, N is the completion of a private type declaration and Derived_Type
125 -- is different from the defining identifier inside N (i.e. Derived_Type /=
126 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
127 -- subprograms should be derived. The only case where this parameter is
128 -- False is when Build_Derived_Type is recursively called to process an
129 -- implicit derived full type for a type derived from a private type (in
130 -- that case the subprograms must only be derived for the private view of
131 -- the type).
132 --
133 -- ??? These flags need a bit of re-examination and re-documentation:
134 -- ??? are they both necessary (both seem related to the recursion)?
135
136 procedure Build_Derived_Access_Type
137 (N : Node_Id;
138 Parent_Type : Entity_Id;
139 Derived_Type : Entity_Id);
140 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
141 -- create an implicit base if the parent type is constrained or if the
142 -- subtype indication has a constraint.
143
144 procedure Build_Derived_Array_Type
145 (N : Node_Id;
146 Parent_Type : Entity_Id;
147 Derived_Type : Entity_Id);
148 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
149 -- create an implicit base if the parent type is constrained or if the
150 -- subtype indication has a constraint.
151
152 procedure Build_Derived_Concurrent_Type
153 (N : Node_Id;
154 Parent_Type : Entity_Id;
155 Derived_Type : Entity_Id);
156 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
157 -- protected type, inherit entries and protected subprograms, check
158 -- legality of discriminant constraints if any.
159
160 procedure Build_Derived_Enumeration_Type
161 (N : Node_Id;
162 Parent_Type : Entity_Id;
163 Derived_Type : Entity_Id);
164 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
165 -- type, we must create a new list of literals. Types derived from
166 -- Character and [Wide_]Wide_Character are special-cased.
167
168 procedure Build_Derived_Numeric_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id);
172 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
173 -- an anonymous base type, and propagate constraint to subtype if needed.
174
175 procedure Build_Derived_Private_Type
176 (N : Node_Id;
177 Parent_Type : Entity_Id;
178 Derived_Type : Entity_Id;
179 Is_Completion : Boolean;
180 Derive_Subps : Boolean := True);
181 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
182 -- because the parent may or may not have a completion, and the derivation
183 -- may itself be a completion.
184
185 procedure Build_Derived_Record_Type
186 (N : Node_Id;
187 Parent_Type : Entity_Id;
188 Derived_Type : Entity_Id;
189 Derive_Subps : Boolean := True);
190 -- Subsidiary procedure used for tagged and untagged record types
191 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
192 -- All parameters are as in Build_Derived_Type except that N, in
193 -- addition to being an N_Full_Type_Declaration node, can also be an
194 -- N_Private_Extension_Declaration node. See the definition of this routine
195 -- for much more info. Derive_Subps indicates whether subprograms should be
196 -- derived from the parent type. The only case where Derive_Subps is False
197 -- is for an implicit derived full type for a type derived from a private
198 -- type (see Build_Derived_Type).
199
200 procedure Build_Discriminal (Discrim : Entity_Id);
201 -- Create the discriminal corresponding to discriminant Discrim, that is
202 -- the parameter corresponding to Discrim to be used in initialization
203 -- procedures for the type where Discrim is a discriminant. Discriminals
204 -- are not used during semantic analysis, and are not fully defined
205 -- entities until expansion. Thus they are not given a scope until
206 -- initialization procedures are built.
207
208 function Build_Discriminant_Constraints
209 (T : Entity_Id;
210 Def : Node_Id;
211 Derived_Def : Boolean := False) return Elist_Id;
212 -- Validate discriminant constraints and return the list of the constraints
213 -- in order of discriminant declarations, where T is the discriminated
214 -- unconstrained type. Def is the N_Subtype_Indication node where the
215 -- discriminants constraints for T are specified. Derived_Def is True
216 -- when building the discriminant constraints in a derived type definition
217 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
218 -- type and Def is the constraint "(xxx)" on T and this routine sets the
219 -- Corresponding_Discriminant field of the discriminants in the derived
220 -- type D to point to the corresponding discriminants in the parent type T.
221
222 procedure Build_Discriminated_Subtype
223 (T : Entity_Id;
224 Def_Id : Entity_Id;
225 Elist : Elist_Id;
226 Related_Nod : Node_Id;
227 For_Access : Boolean := False);
228 -- Subsidiary procedure to Constrain_Discriminated_Type and to
229 -- Process_Incomplete_Dependents. Given
230 --
231 -- T (a possibly discriminated base type)
232 -- Def_Id (a very partially built subtype for T),
233 --
234 -- the call completes Def_Id to be the appropriate E_*_Subtype.
235 --
236 -- The Elist is the list of discriminant constraints if any (it is set
237 -- to No_Elist if T is not a discriminated type, and to an empty list if
238 -- T has discriminants but there are no discriminant constraints). The
239 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
240 -- The For_Access says whether or not this subtype is really constraining
241 -- an access type.
242
243 function Build_Scalar_Bound
244 (Bound : Node_Id;
245 Par_T : Entity_Id;
246 Der_T : Entity_Id) return Node_Id;
247 -- The bounds of a derived scalar type are conversions of the bounds of
248 -- the parent type. Optimize the representation if the bounds are literals.
249 -- Needs a more complete spec--what are the parameters exactly, and what
250 -- exactly is the returned value, and how is Bound affected???
251
252 procedure Check_Access_Discriminant_Requires_Limited
253 (D : Node_Id;
254 Loc : Node_Id);
255 -- Check the restriction that the type to which an access discriminant
256 -- belongs must be a concurrent type or a descendant of a type with
257 -- the reserved word 'limited' in its declaration.
258
259 procedure Check_Anonymous_Access_Component
260 (Typ_Decl : Node_Id;
261 Typ : Entity_Id;
262 Prev : Entity_Id;
263 Comp_Def : Node_Id;
264 Access_Def : Node_Id);
265 -- Ada 2005 AI-382: an access component in a record definition can refer to
266 -- the enclosing record, in which case it denotes the type itself, and not
267 -- the current instance of the type. We create an anonymous access type for
268 -- the component, and flag it as an access to a component, so accessibility
269 -- checks are properly performed on it. The declaration of the access type
270 -- is placed ahead of that of the record to prevent order-of-elaboration
271 -- circularity issues in Gigi. We create an incomplete type for the record
272 -- declaration, which is the designated type of the anonymous access.
273
274 procedure Check_Anonymous_Access_Components
275 (Typ_Decl : Node_Id;
276 Typ : Entity_Id;
277 Prev : Entity_Id;
278 Comp_List : Node_Id);
279 -- Call Check_Anonymous_Access_Component on Comp_List
280
281 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
282 -- Check that, if a new discriminant is used in a constraint defining the
283 -- parent subtype of a derivation, its subtype is statically compatible
284 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
285
286 procedure Check_Delta_Expression (E : Node_Id);
287 -- Check that the expression represented by E is suitable for use as a
288 -- delta expression, i.e. it is of real type and is static.
289
290 procedure Check_Digits_Expression (E : Node_Id);
291 -- Check that the expression represented by E is suitable for use as a
292 -- digits expression, i.e. it is of integer type, positive and static.
293
294 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
295 -- Validate the initialization of an object declaration. T is the required
296 -- type, and Exp is the initialization expression.
297
298 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
299 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
300
301 procedure Check_Or_Process_Discriminants
302 (N : Node_Id;
303 T : Entity_Id;
304 Prev : Entity_Id := Empty);
305 -- If N is the full declaration of the completion T of an incomplete or
306 -- private type, check its discriminants (which are already known to be
307 -- conformant with those of the partial view, see Find_Type_Name),
308 -- otherwise process them. Prev is the entity of the partial declaration,
309 -- if any.
310
311 procedure Check_Real_Bound (Bound : Node_Id);
312 -- Check given bound for being of real type and static. If not, post an
313 -- appropriate message, and rewrite the bound with the real literal zero.
314
315 procedure Constant_Redeclaration
316 (Id : Entity_Id;
317 N : Node_Id;
318 T : out Entity_Id);
319 -- Various checks on legality of full declaration of deferred constant.
320 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
321 -- node. The caller has not yet set any attributes of this entity.
322
323 function Contain_Interface
324 (Iface : Entity_Id;
325 Ifaces : Elist_Id) return Boolean;
326 -- Ada 2005: Determine whether Iface is present in the list Ifaces
327
328 procedure Convert_Scalar_Bounds
329 (N : Node_Id;
330 Parent_Type : Entity_Id;
331 Derived_Type : Entity_Id;
332 Loc : Source_Ptr);
333 -- For derived scalar types, convert the bounds in the type definition to
334 -- the derived type, and complete their analysis. Given a constraint of the
335 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
336 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
337 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
338 -- subtype are conversions of those bounds to the derived_type, so that
339 -- their typing is consistent.
340
341 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
342 -- Copies attributes from array base type T2 to array base type T1. Copies
343 -- only attributes that apply to base types, but not subtypes.
344
345 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
346 -- Copies attributes from array subtype T2 to array subtype T1. Copies
347 -- attributes that apply to both subtypes and base types.
348
349 procedure Create_Constrained_Components
350 (Subt : Entity_Id;
351 Decl_Node : Node_Id;
352 Typ : Entity_Id;
353 Constraints : Elist_Id);
354 -- Build the list of entities for a constrained discriminated record
355 -- subtype. If a component depends on a discriminant, replace its subtype
356 -- using the discriminant values in the discriminant constraint. Subt
357 -- is the defining identifier for the subtype whose list of constrained
358 -- entities we will create. Decl_Node is the type declaration node where
359 -- we will attach all the itypes created. Typ is the base discriminated
360 -- type for the subtype Subt. Constraints is the list of discriminant
361 -- constraints for Typ.
362
363 function Constrain_Component_Type
364 (Comp : Entity_Id;
365 Constrained_Typ : Entity_Id;
366 Related_Node : Node_Id;
367 Typ : Entity_Id;
368 Constraints : Elist_Id) return Entity_Id;
369 -- Given a discriminated base type Typ, a list of discriminant constraints,
370 -- Constraints, for Typ and a component Comp of Typ, create and return the
371 -- type corresponding to Etype (Comp) where all discriminant references
372 -- are replaced with the corresponding constraint. If Etype (Comp) contains
373 -- no discriminant references then it is returned as-is. Constrained_Typ
374 -- is the final constrained subtype to which the constrained component
375 -- belongs. Related_Node is the node where we attach all created itypes.
376
377 procedure Constrain_Access
378 (Def_Id : in out Entity_Id;
379 S : Node_Id;
380 Related_Nod : Node_Id);
381 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
382 -- an anonymous type created for a subtype indication. In that case it is
383 -- created in the procedure and attached to Related_Nod.
384
385 procedure Constrain_Array
386 (Def_Id : in out Entity_Id;
387 SI : Node_Id;
388 Related_Nod : Node_Id;
389 Related_Id : Entity_Id;
390 Suffix : Character);
391 -- Apply a list of index constraints to an unconstrained array type. The
392 -- first parameter is the entity for the resulting subtype. A value of
393 -- Empty for Def_Id indicates that an implicit type must be created, but
394 -- creation is delayed (and must be done by this procedure) because other
395 -- subsidiary implicit types must be created first (which is why Def_Id
396 -- is an in/out parameter). The second parameter is a subtype indication
397 -- node for the constrained array to be created (e.g. something of the
398 -- form string (1 .. 10)). Related_Nod gives the place where this type
399 -- has to be inserted in the tree. The Related_Id and Suffix parameters
400 -- are used to build the associated Implicit type name.
401
402 procedure Constrain_Concurrent
403 (Def_Id : in out Entity_Id;
404 SI : Node_Id;
405 Related_Nod : Node_Id;
406 Related_Id : Entity_Id;
407 Suffix : Character);
408 -- Apply list of discriminant constraints to an unconstrained concurrent
409 -- type.
410 --
411 -- SI is the N_Subtype_Indication node containing the constraint and
412 -- the unconstrained type to constrain.
413 --
414 -- Def_Id is the entity for the resulting constrained subtype. A value
415 -- of Empty for Def_Id indicates that an implicit type must be created,
416 -- but creation is delayed (and must be done by this procedure) because
417 -- other subsidiary implicit types must be created first (which is why
418 -- Def_Id is an in/out parameter).
419 --
420 -- Related_Nod gives the place where this type has to be inserted
421 -- in the tree.
422 --
423 -- The last two arguments are used to create its external name if needed.
424
425 function Constrain_Corresponding_Record
426 (Prot_Subt : Entity_Id;
427 Corr_Rec : Entity_Id;
428 Related_Nod : Node_Id) return Entity_Id;
429 -- When constraining a protected type or task type with discriminants,
430 -- constrain the corresponding record with the same discriminant values.
431
432 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id);
433 -- Constrain a decimal fixed point type with a digits constraint and/or a
434 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
435
436 procedure Constrain_Discriminated_Type
437 (Def_Id : Entity_Id;
438 S : Node_Id;
439 Related_Nod : Node_Id;
440 For_Access : Boolean := False);
441 -- Process discriminant constraints of composite type. Verify that values
442 -- have been provided for all discriminants, that the original type is
443 -- unconstrained, and that the types of the supplied expressions match
444 -- the discriminant types. The first three parameters are like in routine
445 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
446 -- of For_Access.
447
448 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id);
449 -- Constrain an enumeration type with a range constraint. This is identical
450 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
451
452 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id);
453 -- Constrain a floating point type with either a digits constraint
454 -- and/or a range constraint, building a E_Floating_Point_Subtype.
455
456 procedure Constrain_Index
457 (Index : Node_Id;
458 S : Node_Id;
459 Related_Nod : Node_Id;
460 Related_Id : Entity_Id;
461 Suffix : Character;
462 Suffix_Index : Pos);
463 -- Process an index constraint S in a constrained array declaration. The
464 -- constraint can be a subtype name, or a range with or without an explicit
465 -- subtype mark. The index is the corresponding index of the unconstrained
466 -- array. The Related_Id and Suffix parameters are used to build the
467 -- associated Implicit type name.
468
469 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id);
470 -- Build subtype of a signed or modular integer type
471
472 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id);
473 -- Constrain an ordinary fixed point type with a range constraint, and
474 -- build an E_Ordinary_Fixed_Point_Subtype entity.
475
476 procedure Copy_And_Swap (Priv, Full : Entity_Id);
477 -- Copy the Priv entity into the entity of its full declaration then swap
478 -- the two entities in such a manner that the former private type is now
479 -- seen as a full type.
480
481 procedure Decimal_Fixed_Point_Type_Declaration
482 (T : Entity_Id;
483 Def : Node_Id);
484 -- Create a new decimal fixed point type, and apply the constraint to
485 -- obtain a subtype of this new type.
486
487 procedure Complete_Private_Subtype
488 (Priv : Entity_Id;
489 Full : Entity_Id;
490 Full_Base : Entity_Id;
491 Related_Nod : Node_Id);
492 -- Complete the implicit full view of a private subtype by setting the
493 -- appropriate semantic fields. If the full view of the parent is a record
494 -- type, build constrained components of subtype.
495
496 procedure Derive_Progenitor_Subprograms
497 (Parent_Type : Entity_Id;
498 Tagged_Type : Entity_Id);
499 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
500 -- operations of progenitors of Tagged_Type, and replace the subsidiary
501 -- subtypes with Tagged_Type, to build the specs of the inherited interface
502 -- primitives. The derived primitives are aliased to those of the
503 -- interface. This routine takes care also of transferring to the full view
504 -- subprograms associated with the partial view of Tagged_Type that cover
505 -- interface primitives.
506
507 procedure Derived_Standard_Character
508 (N : Node_Id;
509 Parent_Type : Entity_Id;
510 Derived_Type : Entity_Id);
511 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
512 -- derivations from types Standard.Character and Standard.Wide_Character.
513
514 procedure Derived_Type_Declaration
515 (T : Entity_Id;
516 N : Node_Id;
517 Is_Completion : Boolean);
518 -- Process a derived type declaration. Build_Derived_Type is invoked
519 -- to process the actual derived type definition. Parameters N and
520 -- Is_Completion have the same meaning as in Build_Derived_Type.
521 -- T is the N_Defining_Identifier for the entity defined in the
522 -- N_Full_Type_Declaration node N, that is T is the derived type.
523
524 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
525 -- Insert each literal in symbol table, as an overloadable identifier. Each
526 -- enumeration type is mapped into a sequence of integers, and each literal
527 -- is defined as a constant with integer value. If any of the literals are
528 -- character literals, the type is a character type, which means that
529 -- strings are legal aggregates for arrays of components of the type.
530
531 function Expand_To_Stored_Constraint
532 (Typ : Entity_Id;
533 Constraint : Elist_Id) return Elist_Id;
534 -- Given a constraint (i.e. a list of expressions) on the discriminants of
535 -- Typ, expand it into a constraint on the stored discriminants and return
536 -- the new list of expressions constraining the stored discriminants.
537
538 function Find_Type_Of_Object
539 (Obj_Def : Node_Id;
540 Related_Nod : Node_Id) return Entity_Id;
541 -- Get type entity for object referenced by Obj_Def, attaching the implicit
542 -- types generated to Related_Nod.
543
544 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
545 -- Create a new float and apply the constraint to obtain subtype of it
546
547 function Has_Range_Constraint (N : Node_Id) return Boolean;
548 -- Given an N_Subtype_Indication node N, return True if a range constraint
549 -- is present, either directly, or as part of a digits or delta constraint.
550 -- In addition, a digits constraint in the decimal case returns True, since
551 -- it establishes a default range if no explicit range is present.
552
553 function Inherit_Components
554 (N : Node_Id;
555 Parent_Base : Entity_Id;
556 Derived_Base : Entity_Id;
557 Is_Tagged : Boolean;
558 Inherit_Discr : Boolean;
559 Discs : Elist_Id) return Elist_Id;
560 -- Called from Build_Derived_Record_Type to inherit the components of
561 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
562 -- For more information on derived types and component inheritance please
563 -- consult the comment above the body of Build_Derived_Record_Type.
564 --
565 -- N is the original derived type declaration
566 --
567 -- Is_Tagged is set if we are dealing with tagged types
568 --
569 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
570 -- Parent_Base, otherwise no discriminants are inherited.
571 --
572 -- Discs gives the list of constraints that apply to Parent_Base in the
573 -- derived type declaration. If Discs is set to No_Elist, then we have
574 -- the following situation:
575 --
576 -- type Parent (D1..Dn : ..) is [tagged] record ...;
577 -- type Derived is new Parent [with ...];
578 --
579 -- which gets treated as
580 --
581 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
582 --
583 -- For untagged types the returned value is an association list. The list
584 -- starts from the association (Parent_Base => Derived_Base), and then it
585 -- contains a sequence of the associations of the form
586 --
587 -- (Old_Component => New_Component),
588 --
589 -- where Old_Component is the Entity_Id of a component in Parent_Base and
590 -- New_Component is the Entity_Id of the corresponding component in
591 -- Derived_Base. For untagged records, this association list is needed when
592 -- copying the record declaration for the derived base. In the tagged case
593 -- the value returned is irrelevant.
594
595 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
596 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
597 -- Determine whether subprogram Subp is a procedure subject to pragma
598 -- Extensions_Visible with value False and has at least one controlling
599 -- parameter of mode OUT.
600
601 function Is_Private_Primitive (Prim : Entity_Id) return Boolean;
602 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
603 -- When applied to a primitive subprogram Prim, returns True if Prim is
604 -- declared as a private operation within a package or generic package,
605 -- and returns False otherwise.
606
607 function Is_Valid_Constraint_Kind
608 (T_Kind : Type_Kind;
609 Constraint_Kind : Node_Kind) return Boolean;
610 -- Returns True if it is legal to apply the given kind of constraint to the
611 -- given kind of type (index constraint to an array type, for example).
612
613 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
614 -- Create new modular type. Verify that modulus is in bounds
615
616 procedure New_Concatenation_Op (Typ : Entity_Id);
617 -- Create an abbreviated declaration for an operator in order to
618 -- materialize concatenation on array types.
619
620 procedure Ordinary_Fixed_Point_Type_Declaration
621 (T : Entity_Id;
622 Def : Node_Id);
623 -- Create a new ordinary fixed point type, and apply the constraint to
624 -- obtain subtype of it.
625
626 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
627 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
628 -- In_Default_Expr can be properly adjusted.
629
630 procedure Prepare_Private_Subtype_Completion
631 (Id : Entity_Id;
632 Related_Nod : Node_Id);
633 -- Id is a subtype of some private type. Creates the full declaration
634 -- associated with Id whenever possible, i.e. when the full declaration
635 -- of the base type is already known. Records each subtype into
636 -- Private_Dependents of the base type.
637
638 procedure Process_Incomplete_Dependents
639 (N : Node_Id;
640 Full_T : Entity_Id;
641 Inc_T : Entity_Id);
642 -- Process all entities that depend on an incomplete type. There include
643 -- subtypes, subprogram types that mention the incomplete type in their
644 -- profiles, and subprogram with access parameters that designate the
645 -- incomplete type.
646
647 -- Inc_T is the defining identifier of an incomplete type declaration, its
648 -- Ekind is E_Incomplete_Type.
649 --
650 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
651 --
652 -- Full_T is N's defining identifier.
653 --
654 -- Subtypes of incomplete types with discriminants are completed when the
655 -- parent type is. This is simpler than private subtypes, because they can
656 -- only appear in the same scope, and there is no need to exchange views.
657 -- Similarly, access_to_subprogram types may have a parameter or a return
658 -- type that is an incomplete type, and that must be replaced with the
659 -- full type.
660 --
661 -- If the full type is tagged, subprogram with access parameters that
662 -- designated the incomplete may be primitive operations of the full type,
663 -- and have to be processed accordingly.
664
665 procedure Process_Real_Range_Specification (Def : Node_Id);
666 -- Given the type definition for a real type, this procedure processes and
667 -- checks the real range specification of this type definition if one is
668 -- present. If errors are found, error messages are posted, and the
669 -- Real_Range_Specification of Def is reset to Empty.
670
671 procedure Record_Type_Declaration
672 (T : Entity_Id;
673 N : Node_Id;
674 Prev : Entity_Id);
675 -- Process a record type declaration (for both untagged and tagged
676 -- records). Parameters T and N are exactly like in procedure
677 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
678 -- for this routine. If this is the completion of an incomplete type
679 -- declaration, Prev is the entity of the incomplete declaration, used for
680 -- cross-referencing. Otherwise Prev = T.
681
682 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
683 -- This routine is used to process the actual record type definition (both
684 -- for untagged and tagged records). Def is a record type definition node.
685 -- This procedure analyzes the components in this record type definition.
686 -- Prev_T is the entity for the enclosing record type. It is provided so
687 -- that its Has_Task flag can be set if any of the component have Has_Task
688 -- set. If the declaration is the completion of an incomplete type
689 -- declaration, Prev_T is the original incomplete type, whose full view is
690 -- the record type.
691
692 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
693 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
694 -- first create the list of components for the derived type from that of
695 -- the parent by means of Inherit_Components and then build a copy of the
696 -- declaration tree of the parent with the help of the mapping returned by
697 -- Inherit_Components, which will for example be used to validate record
698 -- representation clauses given for the derived type. If the parent type
699 -- is private and has discriminants, the ancestor discriminants used in the
700 -- inheritance are that of the private declaration, whereas the ancestor
701 -- discriminants present in the declaration tree of the parent are that of
702 -- the full declaration; as a consequence, the remapping done during the
703 -- copy will leave the references to the ancestor discriminants unchanged
704 -- in the declaration tree and they need to be fixed up. If the derived
705 -- type has a known discriminant part, then the remapping done during the
706 -- copy will only create references to the stored discriminants and they
707 -- need to be replaced with references to the non-stored discriminants.
708
709 procedure Set_Fixed_Range
710 (E : Entity_Id;
711 Loc : Source_Ptr;
712 Lo : Ureal;
713 Hi : Ureal);
714 -- Build a range node with the given bounds and set it as the Scalar_Range
715 -- of the given fixed-point type entity. Loc is the source location used
716 -- for the constructed range. See body for further details.
717
718 procedure Set_Scalar_Range_For_Subtype
719 (Def_Id : Entity_Id;
720 R : Node_Id;
721 Subt : Entity_Id);
722 -- This routine is used to set the scalar range field for a subtype given
723 -- Def_Id, the entity for the subtype, and R, the range expression for the
724 -- scalar range. Subt provides the parent subtype to be used to analyze,
725 -- resolve, and check the given range.
726
727 procedure Set_Default_SSO (T : Entity_Id);
728 -- T is the entity for an array or record being declared. This procedure
729 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
730 -- to the setting of Opt.Default_SSO.
731
732 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
733 -- Create a new signed integer entity, and apply the constraint to obtain
734 -- the required first named subtype of this type.
735
736 procedure Set_Stored_Constraint_From_Discriminant_Constraint
737 (E : Entity_Id);
738 -- E is some record type. This routine computes E's Stored_Constraint
739 -- from its Discriminant_Constraint.
740
741 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
742 -- Check that an entity in a list of progenitors is an interface,
743 -- emit error otherwise.
744
745 procedure Warn_On_Inherently_Limited_Type (E : Entity_Id);
746 -- Emit a warning if a record type that does not have a limited keyword in
747 -- its definition has any components that are limited (which implicitly
748 -- make the type limited).
749
750 -----------------------
751 -- Access_Definition --
752 -----------------------
753
754 function Access_Definition
755 (Related_Nod : Node_Id;
756 N : Node_Id) return Entity_Id
757 is
758 Anon_Type : Entity_Id;
759 Anon_Scope : Entity_Id;
760 Desig_Type : Entity_Id;
761 Enclosing_Prot_Type : Entity_Id := Empty;
762
763 begin
764 -- Ada 2005: For an object declaration the corresponding anonymous
765 -- type is declared in the current scope.
766
767 -- If the access definition is the return type of another access to
768 -- function, scope is the current one, because it is the one of the
769 -- current type declaration, except for the pathological case below.
770
771 if Nkind (Related_Nod) in
772 N_Object_Declaration | N_Access_Function_Definition
773 then
774 Anon_Scope := Current_Scope;
775
776 -- A pathological case: function returning access functions that
777 -- return access functions, etc. Each anonymous access type created
778 -- is in the enclosing scope of the outermost function.
779
780 declare
781 Par : Node_Id;
782
783 begin
784 Par := Related_Nod;
785 while Nkind (Par) in
786 N_Access_Function_Definition | N_Access_Definition
787 loop
788 Par := Parent (Par);
789 end loop;
790
791 if Nkind (Par) = N_Function_Specification then
792 Anon_Scope := Scope (Defining_Entity (Par));
793 end if;
794 end;
795
796 -- For the anonymous function result case, retrieve the scope of the
797 -- function specification's associated entity rather than using the
798 -- current scope. The current scope will be the function itself if the
799 -- formal part is currently being analyzed, but will be the parent scope
800 -- in the case of a parameterless function, and we always want to use
801 -- the function's parent scope. Finally, if the function is a child
802 -- unit, we must traverse the tree to retrieve the proper entity.
803
804 elsif Nkind (Related_Nod) = N_Function_Specification
805 and then Nkind (Parent (N)) /= N_Parameter_Specification
806 then
807 -- If the current scope is a protected type, the anonymous access
808 -- is associated with one of the protected operations, and must
809 -- be available in the scope that encloses the protected declaration.
810 -- Otherwise the type is in the scope enclosing the subprogram.
811
812 -- If the function has formals, the return type of a subprogram
813 -- declaration is analyzed in the scope of the subprogram (see
814 -- Process_Formals) and thus the protected type, if present, is
815 -- the scope of the current function scope.
816
817 if Ekind (Current_Scope) = E_Protected_Type then
818 Enclosing_Prot_Type := Current_Scope;
819
820 elsif Ekind (Current_Scope) = E_Function
821 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
822 then
823 Enclosing_Prot_Type := Scope (Current_Scope);
824 end if;
825
826 if Present (Enclosing_Prot_Type) then
827 Anon_Scope := Scope (Enclosing_Prot_Type);
828
829 else
830 Anon_Scope := Scope (Defining_Entity (Related_Nod));
831 end if;
832
833 -- For an access type definition, if the current scope is a child
834 -- unit it is the scope of the type.
835
836 elsif Is_Compilation_Unit (Current_Scope) then
837 Anon_Scope := Current_Scope;
838
839 -- For access formals, access components, and access discriminants, the
840 -- scope is that of the enclosing declaration,
841
842 else
843 Anon_Scope := Scope (Current_Scope);
844 end if;
845
846 Anon_Type :=
847 Create_Itype
848 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
849
850 if All_Present (N)
851 and then Ada_Version >= Ada_2005
852 then
853 Error_Msg_N ("ALL not permitted for anonymous access types", N);
854 end if;
855
856 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
857 -- the corresponding semantic routine
858
859 if Present (Access_To_Subprogram_Definition (N)) then
860 Access_Subprogram_Declaration
861 (T_Name => Anon_Type,
862 T_Def => Access_To_Subprogram_Definition (N));
863
864 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
865 Mutate_Ekind
866 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
867 else
868 Mutate_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
869 end if;
870
871 -- If the anonymous access is associated with a protected operation,
872 -- create a reference to it after the enclosing protected definition
873 -- because the itype will be used in the subsequent bodies.
874
875 -- If the anonymous access itself is protected, a full type
876 -- declaratiton will be created for it, so that the equivalent
877 -- record type can be constructed. For further details, see
878 -- Replace_Anonymous_Access_To_Protected-Subprogram.
879
880 if Ekind (Current_Scope) = E_Protected_Type
881 and then not Protected_Present (Access_To_Subprogram_Definition (N))
882 then
883 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
884 end if;
885
886 return Anon_Type;
887 end if;
888
889 Find_Type (Subtype_Mark (N));
890 Desig_Type := Entity (Subtype_Mark (N));
891
892 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
893 Set_Etype (Anon_Type, Anon_Type);
894
895 -- Make sure the anonymous access type has size and alignment fields
896 -- set, as required by gigi. This is necessary in the case of the
897 -- Task_Body_Procedure.
898
899 if not Has_Private_Component (Desig_Type) then
900 Layout_Type (Anon_Type);
901 end if;
902
903 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
904 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
905 -- the null value is allowed. In Ada 95 the null value is never allowed.
906
907 if Ada_Version >= Ada_2005 then
908 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
909 else
910 Set_Can_Never_Be_Null (Anon_Type, True);
911 end if;
912
913 -- The anonymous access type is as public as the discriminated type or
914 -- subprogram that defines it. It is imported (for back-end purposes)
915 -- if the designated type is.
916
917 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
918
919 -- Ada 2005 (AI-231): Propagate the access-constant attribute
920
921 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
922
923 -- The context is either a subprogram declaration, object declaration,
924 -- or an access discriminant, in a private or a full type declaration.
925 -- In the case of a subprogram, if the designated type is incomplete,
926 -- the operation will be a primitive operation of the full type, to be
927 -- updated subsequently. If the type is imported through a limited_with
928 -- clause, the subprogram is not a primitive operation of the type
929 -- (which is declared elsewhere in some other scope).
930
931 if Ekind (Desig_Type) = E_Incomplete_Type
932 and then not From_Limited_With (Desig_Type)
933 and then Is_Overloadable (Current_Scope)
934 then
935 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
936 Set_Has_Delayed_Freeze (Current_Scope);
937 end if;
938
939 -- If the designated type is limited and class-wide, the object might
940 -- contain tasks, so we create a Master entity for the declaration. This
941 -- must be done before expansion of the full declaration, because the
942 -- declaration may include an expression that is an allocator, whose
943 -- expansion needs the proper Master for the created tasks.
944
945 if Expander_Active
946 and then Nkind (Related_Nod) = N_Object_Declaration
947 then
948 if Is_Limited_Record (Desig_Type)
949 and then Is_Class_Wide_Type (Desig_Type)
950 then
951 Build_Class_Wide_Master (Anon_Type);
952
953 -- Similarly, if the type is an anonymous access that designates
954 -- tasks, create a master entity for it in the current context.
955
956 elsif Has_Task (Desig_Type)
957 and then Comes_From_Source (Related_Nod)
958 then
959 Build_Master_Entity (Defining_Identifier (Related_Nod));
960 Build_Master_Renaming (Anon_Type);
961 end if;
962 end if;
963
964 -- For a private component of a protected type, it is imperative that
965 -- the back-end elaborate the type immediately after the protected
966 -- declaration, because this type will be used in the declarations
967 -- created for the component within each protected body, so we must
968 -- create an itype reference for it now.
969
970 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
971 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
972
973 -- Similarly, if the access definition is the return result of a
974 -- function, create an itype reference for it because it will be used
975 -- within the function body. For a regular function that is not a
976 -- compilation unit, insert reference after the declaration. For a
977 -- protected operation, insert it after the enclosing protected type
978 -- declaration. In either case, do not create a reference for a type
979 -- obtained through a limited_with clause, because this would introduce
980 -- semantic dependencies.
981
982 -- Similarly, do not create a reference if the designated type is a
983 -- generic formal, because no use of it will reach the backend.
984
985 elsif Nkind (Related_Nod) = N_Function_Specification
986 and then not From_Limited_With (Desig_Type)
987 and then not Is_Generic_Type (Desig_Type)
988 then
989 if Present (Enclosing_Prot_Type) then
990 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
991
992 elsif Is_List_Member (Parent (Related_Nod))
993 and then Nkind (Parent (N)) /= N_Parameter_Specification
994 then
995 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
996 end if;
997
998 -- Finally, create an itype reference for an object declaration of an
999 -- anonymous access type. This is strictly necessary only for deferred
1000 -- constants, but in any case will avoid out-of-scope problems in the
1001 -- back-end.
1002
1003 elsif Nkind (Related_Nod) = N_Object_Declaration then
1004 Build_Itype_Reference (Anon_Type, Related_Nod);
1005 end if;
1006
1007 return Anon_Type;
1008 end Access_Definition;
1009
1010 -----------------------------------
1011 -- Access_Subprogram_Declaration --
1012 -----------------------------------
1013
1014 procedure Access_Subprogram_Declaration
1015 (T_Name : Entity_Id;
1016 T_Def : Node_Id)
1017 is
1018 procedure Check_For_Premature_Usage (Def : Node_Id);
1019 -- Check that type T_Name is not used, directly or recursively, as a
1020 -- parameter or a return type in Def. Def is either a subtype, an
1021 -- access_definition, or an access_to_subprogram_definition.
1022
1023 -------------------------------
1024 -- Check_For_Premature_Usage --
1025 -------------------------------
1026
1027 procedure Check_For_Premature_Usage (Def : Node_Id) is
1028 Param : Node_Id;
1029
1030 begin
1031 -- Check for a subtype mark
1032
1033 if Nkind (Def) in N_Has_Etype then
1034 if Etype (Def) = T_Name then
1035 Error_Msg_N
1036 ("type& cannot be used before the end of its declaration",
1037 Def);
1038 end if;
1039
1040 -- If this is not a subtype, then this is an access_definition
1041
1042 elsif Nkind (Def) = N_Access_Definition then
1043 if Present (Access_To_Subprogram_Definition (Def)) then
1044 Check_For_Premature_Usage
1045 (Access_To_Subprogram_Definition (Def));
1046 else
1047 Check_For_Premature_Usage (Subtype_Mark (Def));
1048 end if;
1049
1050 -- The only cases left are N_Access_Function_Definition and
1051 -- N_Access_Procedure_Definition.
1052
1053 else
1054 if Present (Parameter_Specifications (Def)) then
1055 Param := First (Parameter_Specifications (Def));
1056 while Present (Param) loop
1057 Check_For_Premature_Usage (Parameter_Type (Param));
1058 Next (Param);
1059 end loop;
1060 end if;
1061
1062 if Nkind (Def) = N_Access_Function_Definition then
1063 Check_For_Premature_Usage (Result_Definition (Def));
1064 end if;
1065 end if;
1066 end Check_For_Premature_Usage;
1067
1068 -- Local variables
1069
1070 Formals : constant List_Id := Parameter_Specifications (T_Def);
1071 Formal : Entity_Id;
1072 D_Ityp : Node_Id;
1073 Desig_Type : constant Entity_Id :=
1074 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1075
1076 -- Start of processing for Access_Subprogram_Declaration
1077
1078 begin
1079 -- Associate the Itype node with the inner full-type declaration or
1080 -- subprogram spec or entry body. This is required to handle nested
1081 -- anonymous declarations. For example:
1082
1083 -- procedure P
1084 -- (X : access procedure
1085 -- (Y : access procedure
1086 -- (Z : access T)))
1087
1088 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1089 while Nkind (D_Ityp) not in N_Full_Type_Declaration
1090 | N_Private_Type_Declaration
1091 | N_Private_Extension_Declaration
1092 | N_Procedure_Specification
1093 | N_Function_Specification
1094 | N_Entry_Body
1095 | N_Object_Declaration
1096 | N_Object_Renaming_Declaration
1097 | N_Formal_Object_Declaration
1098 | N_Formal_Type_Declaration
1099 | N_Task_Type_Declaration
1100 | N_Protected_Type_Declaration
1101 loop
1102 D_Ityp := Parent (D_Ityp);
1103 if No (D_Ityp) then
1104 raise Program_Error;
1105 end if;
1106 end loop;
1107
1108 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1109
1110 if Nkind (D_Ityp) in N_Procedure_Specification | N_Function_Specification
1111 then
1112 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1113
1114 elsif Nkind (D_Ityp) in N_Full_Type_Declaration
1115 | N_Object_Declaration
1116 | N_Object_Renaming_Declaration
1117 | N_Formal_Type_Declaration
1118 then
1119 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1120 end if;
1121
1122 if Nkind (T_Def) = N_Access_Function_Definition then
1123 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1124 declare
1125 Acc : constant Node_Id := Result_Definition (T_Def);
1126
1127 begin
1128 if Present (Access_To_Subprogram_Definition (Acc))
1129 and then
1130 Protected_Present (Access_To_Subprogram_Definition (Acc))
1131 then
1132 Set_Etype
1133 (Desig_Type,
1134 Replace_Anonymous_Access_To_Protected_Subprogram
1135 (T_Def));
1136
1137 else
1138 Set_Etype
1139 (Desig_Type,
1140 Access_Definition (T_Def, Result_Definition (T_Def)));
1141 end if;
1142 end;
1143
1144 else
1145 Analyze (Result_Definition (T_Def));
1146
1147 declare
1148 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1149
1150 begin
1151 -- If a null exclusion is imposed on the result type, then
1152 -- create a null-excluding itype (an access subtype) and use
1153 -- it as the function's Etype.
1154
1155 if Is_Access_Type (Typ)
1156 and then Null_Exclusion_In_Return_Present (T_Def)
1157 then
1158 Set_Etype (Desig_Type,
1159 Create_Null_Excluding_Itype
1160 (T => Typ,
1161 Related_Nod => T_Def,
1162 Scope_Id => Current_Scope));
1163
1164 else
1165 if From_Limited_With (Typ) then
1166
1167 -- AI05-151: Incomplete types are allowed in all basic
1168 -- declarations, including access to subprograms.
1169
1170 if Ada_Version >= Ada_2012 then
1171 null;
1172
1173 else
1174 Error_Msg_NE
1175 ("illegal use of incomplete type&",
1176 Result_Definition (T_Def), Typ);
1177 end if;
1178
1179 elsif Ekind (Current_Scope) = E_Package
1180 and then In_Private_Part (Current_Scope)
1181 then
1182 if Ekind (Typ) = E_Incomplete_Type then
1183 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1184
1185 elsif Is_Class_Wide_Type (Typ)
1186 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1187 then
1188 Append_Elmt
1189 (Desig_Type, Private_Dependents (Etype (Typ)));
1190 end if;
1191 end if;
1192
1193 Set_Etype (Desig_Type, Typ);
1194 end if;
1195 end;
1196 end if;
1197
1198 if not Is_Type (Etype (Desig_Type)) then
1199 Error_Msg_N
1200 ("expect type in function specification",
1201 Result_Definition (T_Def));
1202 end if;
1203
1204 else
1205 Set_Etype (Desig_Type, Standard_Void_Type);
1206 end if;
1207
1208 if Present (Formals) then
1209 Push_Scope (Desig_Type);
1210
1211 -- Some special tests here. These special tests can be removed
1212 -- if and when Itypes always have proper parent pointers to their
1213 -- declarations???
1214
1215 -- Special test 1) Link defining_identifier of formals. Required by
1216 -- First_Formal to provide its functionality.
1217
1218 declare
1219 F : Node_Id;
1220
1221 begin
1222 F := First (Formals);
1223
1224 while Present (F) loop
1225 if No (Parent (Defining_Identifier (F))) then
1226 Set_Parent (Defining_Identifier (F), F);
1227 end if;
1228
1229 Next (F);
1230 end loop;
1231 end;
1232
1233 Process_Formals (Formals, Parent (T_Def));
1234
1235 -- Special test 2) End_Scope requires that the parent pointer be set
1236 -- to something reasonable, but Itypes don't have parent pointers. So
1237 -- we set it and then unset it ???
1238
1239 Set_Parent (Desig_Type, T_Name);
1240 End_Scope;
1241 Set_Parent (Desig_Type, Empty);
1242 end if;
1243
1244 -- Check for premature usage of the type being defined
1245
1246 Check_For_Premature_Usage (T_Def);
1247
1248 -- The return type and/or any parameter type may be incomplete. Mark the
1249 -- subprogram_type as depending on the incomplete type, so that it can
1250 -- be updated when the full type declaration is seen. This only applies
1251 -- to incomplete types declared in some enclosing scope, not to limited
1252 -- views from other packages.
1253
1254 -- Prior to Ada 2012, all parameters of an access-to-function type must
1255 -- be of mode 'in'.
1256
1257 if Present (Formals) then
1258 Formal := First_Formal (Desig_Type);
1259 while Present (Formal) loop
1260 if Ekind (Formal) /= E_In_Parameter
1261 and then Nkind (T_Def) = N_Access_Function_Definition
1262 and then Ada_Version < Ada_2012
1263 then
1264 Error_Msg_N ("functions can only have IN parameters", Formal);
1265 end if;
1266
1267 if Ekind (Etype (Formal)) = E_Incomplete_Type
1268 and then In_Open_Scopes (Scope (Etype (Formal)))
1269 then
1270 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1271 Set_Has_Delayed_Freeze (Desig_Type);
1272 end if;
1273
1274 Next_Formal (Formal);
1275 end loop;
1276 end if;
1277
1278 -- Check whether an indirect call without actuals may be possible. This
1279 -- is used when resolving calls whose result is then indexed.
1280
1281 May_Need_Actuals (Desig_Type);
1282
1283 -- If the return type is incomplete, this is legal as long as the type
1284 -- is declared in the current scope and will be completed in it (rather
1285 -- than being part of limited view).
1286
1287 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1288 and then not Has_Delayed_Freeze (Desig_Type)
1289 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1290 then
1291 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1292 Set_Has_Delayed_Freeze (Desig_Type);
1293 end if;
1294
1295 Check_Delayed_Subprogram (Desig_Type);
1296
1297 if Protected_Present (T_Def) then
1298 Mutate_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1299 Set_Convention (Desig_Type, Convention_Protected);
1300 else
1301 Mutate_Ekind (T_Name, E_Access_Subprogram_Type);
1302 end if;
1303
1304 Set_Can_Use_Internal_Rep (T_Name,
1305 not Always_Compatible_Rep_On_Target);
1306 Set_Etype (T_Name, T_Name);
1307 Reinit_Size_Align (T_Name);
1308 Set_Directly_Designated_Type (T_Name, Desig_Type);
1309
1310 -- If the access_to_subprogram is not declared at the library level,
1311 -- it can only point to subprograms that are at the same or deeper
1312 -- accessibility level. The corresponding subprogram type might
1313 -- require an activation record when compiling for C.
1314
1315 Set_Needs_Activation_Record (Desig_Type,
1316 not Is_Library_Level_Entity (T_Name));
1317
1318 Generate_Reference_To_Formals (T_Name);
1319
1320 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1321
1322 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1323
1324 Check_Restriction (No_Access_Subprograms, T_Def);
1325
1326 -- Addition of extra formals must be delayed till the freeze point so
1327 -- that we know the convention.
1328 end Access_Subprogram_Declaration;
1329
1330 ----------------------------
1331 -- Access_Type_Declaration --
1332 ----------------------------
1333
1334 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1335
1336 procedure Setup_Access_Type (Desig_Typ : Entity_Id);
1337 -- After type declaration is analysed with T being an incomplete type,
1338 -- this routine will mutate the kind of T to the appropriate access type
1339 -- and set its directly designated type to Desig_Typ.
1340
1341 -----------------------
1342 -- Setup_Access_Type --
1343 -----------------------
1344
1345 procedure Setup_Access_Type (Desig_Typ : Entity_Id) is
1346 begin
1347 if All_Present (Def) or else Constant_Present (Def) then
1348 Mutate_Ekind (T, E_General_Access_Type);
1349 else
1350 Mutate_Ekind (T, E_Access_Type);
1351 end if;
1352
1353 Set_Directly_Designated_Type (T, Desig_Typ);
1354 end Setup_Access_Type;
1355
1356 -- Local variables
1357
1358 P : constant Node_Id := Parent (Def);
1359 S : constant Node_Id := Subtype_Indication (Def);
1360
1361 Full_Desig : Entity_Id;
1362
1363 -- Start of processing for Access_Type_Declaration
1364
1365 begin
1366 -- Check for permissible use of incomplete type
1367
1368 if Nkind (S) /= N_Subtype_Indication then
1369
1370 Analyze (S);
1371
1372 if Nkind (S) in N_Has_Entity
1373 and then Present (Entity (S))
1374 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1375 then
1376 Setup_Access_Type (Desig_Typ => Entity (S));
1377
1378 -- If the designated type is a limited view, we cannot tell if
1379 -- the full view contains tasks, and there is no way to handle
1380 -- that full view in a client. We create a master entity for the
1381 -- scope, which will be used when a client determines that one
1382 -- is needed.
1383
1384 if From_Limited_With (Entity (S))
1385 and then not Is_Class_Wide_Type (Entity (S))
1386 then
1387 Build_Master_Entity (T);
1388 Build_Master_Renaming (T);
1389 end if;
1390
1391 else
1392 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1393 end if;
1394
1395 -- If the access definition is of the form: ACCESS NOT NULL ..
1396 -- the subtype indication must be of an access type. Create
1397 -- a null-excluding subtype of it.
1398
1399 if Null_Excluding_Subtype (Def) then
1400 if not Is_Access_Type (Entity (S)) then
1401 Error_Msg_N ("null exclusion must apply to access type", Def);
1402
1403 else
1404 declare
1405 Loc : constant Source_Ptr := Sloc (S);
1406 Decl : Node_Id;
1407 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1408
1409 begin
1410 Decl :=
1411 Make_Subtype_Declaration (Loc,
1412 Defining_Identifier => Nam,
1413 Null_Exclusion_Present => True,
1414 Subtype_Indication =>
1415 New_Occurrence_Of (Entity (S), Loc));
1416 Insert_Before (Parent (Def), Decl);
1417 Analyze (Decl);
1418 Set_Entity (S, Nam);
1419 end;
1420 end if;
1421 end if;
1422
1423 else
1424 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1425 end if;
1426
1427 if not Error_Posted (T) then
1428 Full_Desig := Designated_Type (T);
1429
1430 if Base_Type (Full_Desig) = T then
1431 Error_Msg_N ("access type cannot designate itself", S);
1432
1433 -- In Ada 2005, the type may have a limited view through some unit in
1434 -- its own context, allowing the following circularity that cannot be
1435 -- detected earlier.
1436
1437 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1438 then
1439 Error_Msg_N
1440 ("access type cannot designate its own class-wide type", S);
1441
1442 -- Clean up indication of tagged status to prevent cascaded errors
1443
1444 Set_Is_Tagged_Type (T, False);
1445 end if;
1446
1447 Set_Etype (T, T);
1448 end if;
1449
1450 -- If the type has appeared already in a with_type clause, it is frozen
1451 -- and the pointer size is already set. Else, initialize.
1452
1453 if not From_Limited_With (T) then
1454 Reinit_Size_Align (T);
1455 end if;
1456
1457 -- Note that Has_Task is always false, since the access type itself
1458 -- is not a task type. See Einfo for more description on this point.
1459 -- Exactly the same consideration applies to Has_Controlled_Component
1460 -- and to Has_Protected.
1461
1462 Set_Has_Task (T, False);
1463 Set_Has_Protected (T, False);
1464 Set_Has_Timing_Event (T, False);
1465 Set_Has_Controlled_Component (T, False);
1466
1467 -- Initialize field Finalization_Collection explicitly to Empty to avoid
1468 -- problems where an incomplete view of this entity has been previously
1469 -- established by a limited with and an overlaid version of this field
1470 -- (Stored_Constraint) was initialized for the incomplete view.
1471
1472 -- This reset is performed in most cases except where the access type
1473 -- has been created for the purposes of allocating or deallocating a
1474 -- build-in-place object. Such access types have explicitly set pools
1475 -- and finalization collections.
1476
1477 if No (Associated_Storage_Pool (T)) then
1478 Set_Finalization_Collection (T, Empty);
1479 end if;
1480
1481 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1482 -- attributes
1483
1484 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1485 Set_Is_Access_Constant (T, Constant_Present (Def));
1486 end Access_Type_Declaration;
1487
1488 ----------------------------------
1489 -- Add_Interface_Tag_Components --
1490 ----------------------------------
1491
1492 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1493 Loc : constant Source_Ptr := Sloc (N);
1494 L : List_Id;
1495 Last_Tag : Node_Id;
1496
1497 procedure Add_Tag (Iface : Entity_Id);
1498 -- Add tag for one of the progenitor interfaces
1499
1500 -------------
1501 -- Add_Tag --
1502 -------------
1503
1504 procedure Add_Tag (Iface : Entity_Id) is
1505 Decl : Node_Id;
1506 Def : Node_Id;
1507 Tag : Entity_Id;
1508 Offset : Entity_Id;
1509
1510 begin
1511 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1512
1513 -- This is a reasonable place to propagate predicates
1514
1515 if Has_Predicates (Iface) then
1516 Set_Has_Predicates (Typ);
1517 end if;
1518
1519 Def :=
1520 Make_Component_Definition (Loc,
1521 Aliased_Present => True,
1522 Subtype_Indication =>
1523 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1524
1525 Tag := Make_Temporary (Loc, 'V');
1526
1527 Decl :=
1528 Make_Component_Declaration (Loc,
1529 Defining_Identifier => Tag,
1530 Component_Definition => Def);
1531
1532 Analyze_Component_Declaration (Decl);
1533
1534 Set_Analyzed (Decl);
1535 Mutate_Ekind (Tag, E_Component);
1536 Set_Is_Tag (Tag);
1537 Set_Is_Aliased (Tag);
1538 Set_Is_Independent (Tag);
1539 Set_Related_Type (Tag, Iface);
1540 Reinit_Component_Location (Tag);
1541
1542 pragma Assert (Is_Frozen (Iface));
1543
1544 Set_DT_Entry_Count (Tag,
1545 DT_Entry_Count (First_Entity (Iface)));
1546
1547 if No (Last_Tag) then
1548 Prepend (Decl, L);
1549 else
1550 Insert_After (Last_Tag, Decl);
1551 end if;
1552
1553 Last_Tag := Decl;
1554
1555 -- If the ancestor has discriminants we need to give special support
1556 -- to store the offset_to_top value of the secondary dispatch tables.
1557 -- For this purpose we add a supplementary component just after the
1558 -- field that contains the tag associated with each secondary DT.
1559
1560 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1561 Def :=
1562 Make_Component_Definition (Loc,
1563 Subtype_Indication =>
1564 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1565
1566 Offset := Make_Temporary (Loc, 'V');
1567
1568 Decl :=
1569 Make_Component_Declaration (Loc,
1570 Defining_Identifier => Offset,
1571 Component_Definition => Def);
1572
1573 Analyze_Component_Declaration (Decl);
1574
1575 Set_Analyzed (Decl);
1576 Mutate_Ekind (Offset, E_Component);
1577 Set_Is_Aliased (Offset);
1578 Set_Is_Independent (Offset);
1579 Set_Related_Type (Offset, Iface);
1580 Reinit_Component_Location (Offset);
1581 Insert_After (Last_Tag, Decl);
1582 Last_Tag := Decl;
1583 end if;
1584 end Add_Tag;
1585
1586 -- Local variables
1587
1588 Elmt : Elmt_Id;
1589 Ext : Node_Id;
1590 Comp : Node_Id;
1591
1592 -- Start of processing for Add_Interface_Tag_Components
1593
1594 begin
1595 if not RTE_Available (RE_Interface_Tag) then
1596 Error_Msg_N
1597 ("(Ada 2005) interface types not supported by this run-time!", N);
1598 return;
1599 end if;
1600
1601 if Ekind (Typ) /= E_Record_Type
1602 or else (Is_Concurrent_Record_Type (Typ)
1603 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1604 or else (not Is_Concurrent_Record_Type (Typ)
1605 and then No (Interfaces (Typ))
1606 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1607 then
1608 return;
1609 end if;
1610
1611 -- Find the current last tag
1612
1613 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1614 Ext := Record_Extension_Part (Type_Definition (N));
1615 else
1616 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1617 Ext := Type_Definition (N);
1618 end if;
1619
1620 Last_Tag := Empty;
1621
1622 if No (Component_List (Ext)) then
1623 Set_Null_Present (Ext, False);
1624 L := New_List;
1625 Set_Component_List (Ext,
1626 Make_Component_List (Loc,
1627 Component_Items => L,
1628 Null_Present => False));
1629 else
1630 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1631 L := Component_Items
1632 (Component_List
1633 (Record_Extension_Part
1634 (Type_Definition (N))));
1635 else
1636 L := Component_Items
1637 (Component_List
1638 (Type_Definition (N)));
1639 end if;
1640
1641 -- Find the last tag component
1642
1643 Comp := First (L);
1644 while Present (Comp) loop
1645 if Nkind (Comp) = N_Component_Declaration
1646 and then Is_Tag (Defining_Identifier (Comp))
1647 then
1648 Last_Tag := Comp;
1649 end if;
1650
1651 Next (Comp);
1652 end loop;
1653 end if;
1654
1655 -- At this point L references the list of components and Last_Tag
1656 -- references the current last tag (if any). Now we add the tag
1657 -- corresponding with all the interfaces that are not implemented
1658 -- by the parent.
1659
1660 if Present (Interfaces (Typ)) then
1661 Elmt := First_Elmt (Interfaces (Typ));
1662 while Present (Elmt) loop
1663 Add_Tag (Node (Elmt));
1664 Next_Elmt (Elmt);
1665 end loop;
1666 end if;
1667 end Add_Interface_Tag_Components;
1668
1669 -------------------------------------
1670 -- Add_Internal_Interface_Entities --
1671 -------------------------------------
1672
1673 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1674
1675 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean;
1676 -- Determine if an error has been posted in some formal of Subp.
1677
1678 -----------------------------
1679 -- Error_Posted_In_Formals --
1680 -----------------------------
1681
1682 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean is
1683 Formal : Entity_Id := First_Formal (Subp);
1684
1685 begin
1686 while Present (Formal) loop
1687 if Error_Posted (Formal) then
1688 return True;
1689 end if;
1690
1691 Next_Formal (Formal);
1692 end loop;
1693
1694 return False;
1695 end Error_Posted_In_Formals;
1696
1697 -- Local variables
1698
1699 Elmt : Elmt_Id;
1700 Iface : Entity_Id;
1701 Iface_Elmt : Elmt_Id;
1702 Iface_Prim : Entity_Id;
1703 Ifaces_List : Elist_Id;
1704 New_Subp : Entity_Id := Empty;
1705 Prim : Entity_Id;
1706 Restore_Scope : Boolean := False;
1707
1708 begin
1709 pragma Assert (Ada_Version >= Ada_2005
1710 and then Is_Record_Type (Tagged_Type)
1711 and then Is_Tagged_Type (Tagged_Type)
1712 and then Has_Interfaces (Tagged_Type)
1713 and then not Is_Interface (Tagged_Type));
1714
1715 -- Ensure that the internal entities are added to the scope of the type
1716
1717 if Scope (Tagged_Type) /= Current_Scope then
1718 Push_Scope (Scope (Tagged_Type));
1719 Restore_Scope := True;
1720 end if;
1721
1722 Collect_Interfaces (Tagged_Type, Ifaces_List);
1723
1724 Iface_Elmt := First_Elmt (Ifaces_List);
1725 while Present (Iface_Elmt) loop
1726 Iface := Node (Iface_Elmt);
1727
1728 -- Originally we excluded here from this processing interfaces that
1729 -- are parents of Tagged_Type because their primitives are located
1730 -- in the primary dispatch table (and hence no auxiliary internal
1731 -- entities are required to handle secondary dispatch tables in such
1732 -- case). However, these auxiliary entities are also required to
1733 -- handle derivations of interfaces in formals of generics (see
1734 -- Derive_Subprograms).
1735
1736 Elmt := First_Elmt (Primitive_Operations (Iface));
1737 while Present (Elmt) loop
1738 Iface_Prim := Node (Elmt);
1739
1740 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1741 Prim :=
1742 Find_Primitive_Covering_Interface
1743 (Tagged_Type => Tagged_Type,
1744 Iface_Prim => Iface_Prim);
1745
1746 if No (Prim) and then Serious_Errors_Detected > 0 then
1747 goto Continue;
1748 end if;
1749
1750 pragma Assert (Present (Prim));
1751
1752 -- Check subtype conformance; we skip this check if errors have
1753 -- been reported in the primitive (or in the formals of the
1754 -- primitive) because Find_Primitive_Covering_Interface relies
1755 -- on the subprogram Type_Conformant to locate the primitive,
1756 -- and reports errors if the formals don't match.
1757
1758 if not Error_Posted (Prim)
1759 and then not Error_Posted_In_Formals (Prim)
1760 then
1761 declare
1762 Alias_Prim : Entity_Id;
1763 Alias_Typ : Entity_Id;
1764 Err_Loc : Node_Id := Empty;
1765 Ret_Type : Entity_Id;
1766
1767 begin
1768 -- For inherited primitives, in case of reporting an
1769 -- error, the error must be reported on this primitive
1770 -- (i.e. in the name of its type declaration); otherwise
1771 -- the error would be reported in the formal of the
1772 -- alias primitive defined on its parent type.
1773
1774 if Nkind (Parent (Prim)) = N_Full_Type_Declaration then
1775 Err_Loc := Prim;
1776 end if;
1777
1778 -- Check subtype conformance of procedures, functions
1779 -- with matching return type, or functions not returning
1780 -- interface types.
1781
1782 if Ekind (Prim) = E_Procedure
1783 or else Etype (Iface_Prim) = Etype (Prim)
1784 or else not Is_Interface (Etype (Iface_Prim))
1785 then
1786 Check_Subtype_Conformant
1787 (New_Id => Prim,
1788 Old_Id => Iface_Prim,
1789 Err_Loc => Err_Loc,
1790 Skip_Controlling_Formals => True);
1791
1792 -- Check subtype conformance of functions returning an
1793 -- interface type; temporarily force both entities to
1794 -- return the same type. Required because subprogram
1795 -- Subtype_Conformant does not handle this case.
1796
1797 else
1798 Ret_Type := Etype (Iface_Prim);
1799 Set_Etype (Iface_Prim, Etype (Prim));
1800
1801 Check_Subtype_Conformant
1802 (New_Id => Prim,
1803 Old_Id => Iface_Prim,
1804 Err_Loc => Err_Loc,
1805 Skip_Controlling_Formals => True);
1806
1807 Set_Etype (Iface_Prim, Ret_Type);
1808 end if;
1809
1810 -- Complete the error when reported on inherited
1811 -- primitives.
1812
1813 if Nkind (Parent (Prim)) = N_Full_Type_Declaration
1814 and then (Error_Posted (Prim)
1815 or else Error_Posted_In_Formals (Prim))
1816 and then Present (Alias (Prim))
1817 then
1818 Alias_Prim := Ultimate_Alias (Prim);
1819 Alias_Typ := Find_Dispatching_Type (Alias_Prim);
1820
1821 if Alias_Typ /= Tagged_Type
1822 and then Is_Ancestor (Alias_Typ, Tagged_Type)
1823 then
1824 Error_Msg_Sloc := Sloc (Alias_Prim);
1825 Error_Msg_N
1826 ("in primitive inherited from #!", Prim);
1827 end if;
1828 end if;
1829 end;
1830 end if;
1831
1832 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1833 -- differs from the name of the interface primitive then it is
1834 -- a private primitive inherited from a parent type. In such
1835 -- case, given that Tagged_Type covers the interface, the
1836 -- inherited private primitive becomes visible. For such
1837 -- purpose we add a new entity that renames the inherited
1838 -- private primitive.
1839
1840 if Chars (Prim) /= Chars (Iface_Prim) then
1841 pragma Assert (Has_Suffix (Prim, 'P'));
1842 Derive_Subprogram
1843 (New_Subp => New_Subp,
1844 Parent_Subp => Iface_Prim,
1845 Derived_Type => Tagged_Type,
1846 Parent_Type => Iface);
1847 Set_Alias (New_Subp, Prim);
1848 Set_Is_Abstract_Subprogram
1849 (New_Subp, Is_Abstract_Subprogram (Prim));
1850 end if;
1851
1852 Derive_Subprogram
1853 (New_Subp => New_Subp,
1854 Parent_Subp => Iface_Prim,
1855 Derived_Type => Tagged_Type,
1856 Parent_Type => Iface);
1857
1858 declare
1859 Anc : Entity_Id;
1860 begin
1861 if Is_Inherited_Operation (Prim)
1862 and then Present (Alias (Prim))
1863 then
1864 Anc := Alias (Prim);
1865 else
1866 Anc := Overridden_Operation (Prim);
1867 end if;
1868
1869 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1870 -- nonconforming preconditions in both an ancestor and
1871 -- a progenitor operation.
1872
1873 -- If the operation is a primitive wrapper it is an explicit
1874 -- (overriding) operqtion and all is fine.
1875
1876 if Present (Anc)
1877 and then Has_Non_Trivial_Precondition (Anc)
1878 and then Has_Non_Trivial_Precondition (Iface_Prim)
1879 then
1880 if Is_Abstract_Subprogram (Prim)
1881 or else
1882 (Ekind (Prim) = E_Procedure
1883 and then Nkind (Parent (Prim)) =
1884 N_Procedure_Specification
1885 and then Null_Present (Parent (Prim)))
1886 or else Is_Primitive_Wrapper (Prim)
1887 then
1888 null;
1889
1890 -- The operation is inherited and must be overridden
1891
1892 elsif not Comes_From_Source (Prim) then
1893 Error_Msg_NE
1894 ("&inherits non-conforming preconditions and must "
1895 & "be overridden (RM 6.1.1 (10-16))",
1896 Parent (Tagged_Type), Prim);
1897 end if;
1898 end if;
1899 end;
1900
1901 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1902 -- associated with interface types. These entities are
1903 -- only registered in the list of primitives of its
1904 -- corresponding tagged type because they are only used
1905 -- to fill the contents of the secondary dispatch tables.
1906 -- Therefore they are removed from the homonym chains.
1907
1908 Set_Is_Hidden (New_Subp);
1909 Set_Is_Internal (New_Subp);
1910 Set_Alias (New_Subp, Prim);
1911 Set_Is_Abstract_Subprogram
1912 (New_Subp, Is_Abstract_Subprogram (Prim));
1913 Set_Interface_Alias (New_Subp, Iface_Prim);
1914
1915 -- If the returned type is an interface then propagate it to
1916 -- the returned type. Needed by the thunk to generate the code
1917 -- which displaces "this" to reference the corresponding
1918 -- secondary dispatch table in the returned object.
1919
1920 if Is_Interface (Etype (Iface_Prim)) then
1921 Set_Etype (New_Subp, Etype (Iface_Prim));
1922 end if;
1923
1924 -- Internal entities associated with interface types are only
1925 -- registered in the list of primitives of the tagged type.
1926 -- They are only used to fill the contents of the secondary
1927 -- dispatch tables. Therefore they are not needed in the
1928 -- homonym chains.
1929
1930 Remove_Homonym (New_Subp);
1931
1932 -- Hidden entities associated with interfaces must have set
1933 -- the Has_Delay_Freeze attribute to ensure that, in case
1934 -- of locally defined tagged types (or compiling with static
1935 -- dispatch tables generation disabled) the corresponding
1936 -- entry of the secondary dispatch table is filled when such
1937 -- an entity is frozen.
1938
1939 Set_Has_Delayed_Freeze (New_Subp);
1940 end if;
1941
1942 <<Continue>>
1943 Next_Elmt (Elmt);
1944 end loop;
1945
1946 Next_Elmt (Iface_Elmt);
1947 end loop;
1948
1949 if Restore_Scope then
1950 Pop_Scope;
1951 end if;
1952 end Add_Internal_Interface_Entities;
1953
1954 -----------------------------------
1955 -- Analyze_Component_Declaration --
1956 -----------------------------------
1957
1958 procedure Analyze_Component_Declaration (N : Node_Id) is
1959 Id : constant Entity_Id := Defining_Identifier (N);
1960 E : constant Node_Id := Expression (N);
1961 Typ : constant Node_Id :=
1962 Subtype_Indication (Component_Definition (N));
1963 T : Entity_Id;
1964 P : Entity_Id;
1965
1966 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1967 -- Typ is the type of the current component, check whether this type is
1968 -- a limited type. Used to validate declaration against that of
1969 -- enclosing record.
1970
1971 procedure Add_Range_Checks (Subt_Indic : Node_Id);
1972 -- Adds range constraint checks for a subtype indication
1973
1974 ----------------------
1975 -- Is_Known_Limited --
1976 ----------------------
1977
1978 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1979 P : constant Entity_Id := Etype (Typ);
1980 R : constant Entity_Id := Root_Type (Typ);
1981
1982 begin
1983 if Is_Limited_Record (Typ) then
1984 return True;
1985
1986 -- If the root type is limited (and not a limited interface) so is
1987 -- the current type.
1988
1989 elsif Is_Limited_Record (R)
1990 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1991 then
1992 return True;
1993
1994 -- Else the type may have a limited interface progenitor, but a
1995 -- limited record parent that is not an interface.
1996
1997 elsif R /= P
1998 and then Is_Limited_Record (P)
1999 and then not Is_Interface (P)
2000 then
2001 return True;
2002
2003 else
2004 return False;
2005 end if;
2006 end Is_Known_Limited;
2007
2008 ----------------------
2009 -- Add_Range_Checks --
2010 ----------------------
2011
2012 procedure Add_Range_Checks (Subt_Indic : Node_Id)
2013 is
2014
2015 begin
2016 if Present (Subt_Indic) and then
2017 Nkind (Subt_Indic) = N_Subtype_Indication and then
2018 Nkind (Constraint (Subt_Indic)) = N_Index_Or_Discriminant_Constraint
2019 then
2020
2021 declare
2022 Typ : constant Entity_Id := Entity (Subtype_Mark (Subt_Indic));
2023 Indic_Typ : constant Entity_Id := Underlying_Type (Typ);
2024 Subt_Index : Node_Id;
2025 Target_Index : Node_Id;
2026 begin
2027
2028 if Present (Indic_Typ) and then Is_Array_Type (Indic_Typ) then
2029
2030 Target_Index := First_Index (Indic_Typ);
2031 Subt_Index := First (Constraints (Constraint (Subt_Indic)));
2032
2033 while Present (Target_Index) loop
2034 if Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
2035 and then Is_Scalar_Type (Entity (Subt_Index))
2036 and then
2037 Nkind (Scalar_Range (Entity (Subt_Index))) = N_Range
2038 then
2039 Apply_Range_Check
2040 (Expr => Scalar_Range (Entity (Subt_Index)),
2041 Target_Typ => Etype (Target_Index),
2042 Insert_Node => Subt_Indic);
2043 end if;
2044
2045 Next (Subt_Index);
2046 Next_Index (Target_Index);
2047 end loop;
2048 end if;
2049 end;
2050 end if;
2051 end Add_Range_Checks;
2052
2053 -- Start of processing for Analyze_Component_Declaration
2054
2055 begin
2056 Generate_Definition (Id);
2057 Enter_Name (Id);
2058
2059 if Present (Typ) then
2060 T := Find_Type_Of_Object
2061 (Subtype_Indication (Component_Definition (N)), N);
2062
2063 -- Ada 2005 (AI-230): Access Definition case
2064
2065 else
2066 pragma Assert (Present
2067 (Access_Definition (Component_Definition (N))));
2068
2069 T := Access_Definition
2070 (Related_Nod => N,
2071 N => Access_Definition (Component_Definition (N)));
2072 Set_Is_Local_Anonymous_Access (T);
2073
2074 -- Ada 2005 (AI-254)
2075
2076 if Present (Access_To_Subprogram_Definition
2077 (Access_Definition (Component_Definition (N))))
2078 and then Protected_Present (Access_To_Subprogram_Definition
2079 (Access_Definition
2080 (Component_Definition (N))))
2081 then
2082 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2083 end if;
2084 end if;
2085
2086 -- If the subtype is a constrained subtype of the enclosing record,
2087 -- (which must have a partial view) the back-end does not properly
2088 -- handle the recursion. Rewrite the component declaration with an
2089 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2090 -- the tree directly because side effects have already been removed from
2091 -- discriminant constraints.
2092
2093 if Ekind (T) = E_Access_Subtype
2094 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2095 and then Comes_From_Source (T)
2096 and then Nkind (Parent (T)) = N_Subtype_Declaration
2097 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2098 then
2099 Rewrite
2100 (Subtype_Indication (Component_Definition (N)),
2101 New_Copy_Tree (Subtype_Indication (Parent (T))));
2102 T := Find_Type_Of_Object
2103 (Subtype_Indication (Component_Definition (N)), N);
2104 end if;
2105
2106 -- If the component declaration includes a default expression, then we
2107 -- check that the component is not of a limited type (RM 3.7(5)),
2108 -- and do the special preanalysis of the expression (see section on
2109 -- "Handling of Default and Per-Object Expressions" in the spec of
2110 -- package Sem).
2111
2112 if Present (E) then
2113 Preanalyze_Default_Expression (E, T);
2114 Check_Initialization (T, E);
2115
2116 if Ada_Version >= Ada_2005
2117 and then Ekind (T) = E_Anonymous_Access_Type
2118 and then Etype (E) /= Any_Type
2119 then
2120 -- Check RM 3.9.2(9): "if the expected type for an expression is
2121 -- an anonymous access-to-specific tagged type, then the object
2122 -- designated by the expression shall not be dynamically tagged
2123 -- unless it is a controlling operand in a call on a dispatching
2124 -- operation"
2125
2126 if Is_Tagged_Type (Directly_Designated_Type (T))
2127 and then
2128 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2129 and then
2130 Ekind (Directly_Designated_Type (Etype (E))) =
2131 E_Class_Wide_Type
2132 then
2133 Error_Msg_N
2134 ("access to specific tagged type required (RM 3.9.2(9))", E);
2135 end if;
2136
2137 -- (Ada 2005: AI-230): Accessibility check for anonymous
2138 -- components
2139
2140 if Type_Access_Level (Etype (E)) >
2141 Deepest_Type_Access_Level (T)
2142 then
2143 Error_Msg_N
2144 ("expression has deeper access level than component " &
2145 "(RM 3.10.2 (12.2))", E);
2146 end if;
2147
2148 -- The initialization expression is a reference to an access
2149 -- discriminant. The type of the discriminant is always deeper
2150 -- than any access type.
2151
2152 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2153 and then Is_Entity_Name (E)
2154 and then Ekind (Entity (E)) = E_In_Parameter
2155 and then Present (Discriminal_Link (Entity (E)))
2156 then
2157 Error_Msg_N
2158 ("discriminant has deeper accessibility level than target",
2159 E);
2160 end if;
2161 end if;
2162 end if;
2163
2164 -- The parent type may be a private view with unknown discriminants,
2165 -- and thus unconstrained. Regular components must be constrained.
2166
2167 if not Is_Definite_Subtype (T)
2168 and then not Is_Mutably_Tagged_Type (T)
2169 and then Chars (Id) /= Name_uParent
2170 then
2171 if Is_Class_Wide_Type (T) then
2172 Error_Msg_N
2173 ("class-wide subtype with unknown discriminants" &
2174 " in component declaration",
2175 Subtype_Indication (Component_Definition (N)));
2176 else
2177 Error_Msg_N
2178 ("unconstrained subtype in component declaration",
2179 Subtype_Indication (Component_Definition (N)));
2180 end if;
2181
2182 -- Components cannot be abstract, except for the special case of
2183 -- the _Parent field (case of extending an abstract tagged type)
2184
2185 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2186 Error_Msg_N ("type of a component cannot be abstract", N);
2187 end if;
2188
2189 Set_Etype (Id, T);
2190
2191 if Aliased_Present (Component_Definition (N)) then
2192 Set_Is_Aliased (Id);
2193
2194 -- AI12-001: All aliased objects are considered to be specified as
2195 -- independently addressable (RM C.6(8.1/4)).
2196
2197 Set_Is_Independent (Id);
2198 end if;
2199
2200 -- The component declaration may have a per-object constraint, set
2201 -- the appropriate flag in the defining identifier of the subtype.
2202
2203 if Has_Discriminant_Dependent_Constraint (Id) then
2204 Set_Has_Per_Object_Constraint (Id);
2205 end if;
2206
2207 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2208 -- out some static checks.
2209
2210 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2211 Null_Exclusion_Static_Checks (N);
2212 end if;
2213
2214 -- If this component is private (or depends on a private type), flag the
2215 -- record type to indicate that some operations are not available.
2216
2217 P := Private_Component (T);
2218
2219 if Present (P) then
2220
2221 -- Check for circular definitions
2222
2223 if P = Any_Type then
2224 Set_Etype (Id, Any_Type);
2225
2226 -- There is a gap in the visibility of operations only if the
2227 -- component type is not defined in the scope of the record type.
2228
2229 elsif Scope (P) = Scope (Current_Scope) then
2230 null;
2231
2232 elsif Is_Limited_Type (P) then
2233 Set_Is_Limited_Composite (Current_Scope);
2234
2235 else
2236 Set_Is_Private_Composite (Current_Scope);
2237 end if;
2238 end if;
2239
2240 if P /= Any_Type
2241 and then Is_Limited_Type (T)
2242 and then Chars (Id) /= Name_uParent
2243 and then Is_Tagged_Type (Current_Scope)
2244 then
2245 if Is_Derived_Type (Current_Scope)
2246 and then not Is_Known_Limited (Current_Scope)
2247 then
2248 Error_Msg_N
2249 ("extension of nonlimited type cannot have limited components",
2250 N);
2251
2252 if Is_Interface (Root_Type (Current_Scope)) then
2253 Error_Msg_N
2254 ("\limitedness is not inherited from limited interface", N);
2255 Error_Msg_N ("\add LIMITED to type indication", N);
2256 end if;
2257
2258 Explain_Limited_Type (T, N);
2259 Set_Etype (Id, Any_Type);
2260 Set_Is_Limited_Composite (Current_Scope, False);
2261
2262 elsif not Is_Derived_Type (Current_Scope)
2263 and then not Is_Limited_Record (Current_Scope)
2264 and then not Is_Concurrent_Type (Current_Scope)
2265 then
2266 Error_Msg_N
2267 ("nonlimited tagged type cannot have limited components", N);
2268 Explain_Limited_Type (T, N);
2269 Set_Etype (Id, Any_Type);
2270 Set_Is_Limited_Composite (Current_Scope, False);
2271 end if;
2272 end if;
2273
2274 Set_Original_Record_Component (Id, Id);
2275
2276 Analyze_Aspect_Specifications (N, Id);
2277
2278 Analyze_Dimension (N);
2279
2280 Add_Range_Checks (Subtype_Indication (Component_Definition (N)));
2281
2282 end Analyze_Component_Declaration;
2283
2284 --------------------------
2285 -- Analyze_Declarations --
2286 --------------------------
2287
2288 procedure Analyze_Declarations (L : List_Id) is
2289 Decl : Node_Id;
2290
2291 procedure Adjust_Decl;
2292 -- Adjust Decl not to include implicit label declarations, since these
2293 -- have strange Sloc values that result in elaboration check problems.
2294 -- (They have the sloc of the label as found in the source, and that
2295 -- is ahead of the current declarative part).
2296
2297 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2298 -- Create the subprogram bodies which verify the run-time semantics of
2299 -- the pragmas listed below for each elibigle type found in declarative
2300 -- list Decls. The pragmas are:
2301 --
2302 -- Default_Initial_Condition
2303 -- Invariant
2304 -- Type_Invariant
2305 --
2306 -- Context denotes the owner of the declarative list.
2307
2308 procedure Check_Entry_Contracts;
2309 -- Perform a preanalysis of the pre- and postconditions of an entry
2310 -- declaration. This must be done before full resolution and creation
2311 -- of the parameter block, etc. to catch illegal uses within the
2312 -- contract expression. Full analysis of the expression is done when
2313 -- the contract is processed.
2314
2315 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2316 -- Check if a nested package has entities within it that rely on library
2317 -- level private types where the full view has not been completed for
2318 -- the purposes of checking if it is acceptable to freeze an expression
2319 -- function at the point of declaration.
2320
2321 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2322 -- Determine whether Body_Decl denotes the body of a late controlled
2323 -- primitive (either Initialize, Adjust or Finalize). If this is the
2324 -- case, add a proper spec if the body lacks one. The spec is inserted
2325 -- before Body_Decl and immediately analyzed.
2326
2327 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2328 -- Spec_Id is the entity of a package that may define abstract states,
2329 -- and in the case of a child unit, whose ancestors may define abstract
2330 -- states. If the states have partial visible refinement, remove the
2331 -- partial visibility of each constituent at the end of the package
2332 -- spec and body declarations.
2333
2334 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2335 -- Spec_Id is the entity of a package that may define abstract states.
2336 -- If the states have visible refinement, remove the visibility of each
2337 -- constituent at the end of the package body declaration.
2338
2339 procedure Resolve_Aspects;
2340 -- Utility to resolve the expressions of aspects at the end of a list of
2341 -- declarations, or before a declaration that freezes previous entities,
2342 -- such as in a subprogram body.
2343
2344 -----------------
2345 -- Adjust_Decl --
2346 -----------------
2347
2348 procedure Adjust_Decl is
2349 begin
2350 while Present (Prev (Decl))
2351 and then Nkind (Decl) = N_Implicit_Label_Declaration
2352 loop
2353 Prev (Decl);
2354 end loop;
2355 end Adjust_Decl;
2356
2357 ----------------------------
2358 -- Build_Assertion_Bodies --
2359 ----------------------------
2360
2361 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2362 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2363 -- Create the subprogram bodies which verify the run-time semantics
2364 -- of the pragmas listed below for type Typ. The pragmas are:
2365 --
2366 -- Default_Initial_Condition
2367 -- Invariant
2368 -- Type_Invariant
2369
2370 -------------------------------------
2371 -- Build_Assertion_Bodies_For_Type --
2372 -------------------------------------
2373
2374 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2375 begin
2376 if Nkind (Context) = N_Package_Specification then
2377
2378 -- Preanalyze and resolve the class-wide invariants of an
2379 -- interface at the end of whichever declarative part has the
2380 -- interface type. Note that an interface may be declared in
2381 -- any non-package declarative part, but reaching the end of
2382 -- such a declarative part will always freeze the type and
2383 -- generate the invariant procedure (see Freeze_Type).
2384
2385 if Is_Interface (Typ) then
2386
2387 -- Interfaces are treated as the partial view of a private
2388 -- type, in order to achieve uniformity with the general
2389 -- case. As a result, an interface receives only a "partial"
2390 -- invariant procedure, which is never called.
2391
2392 if Has_Own_Invariants (Typ) then
2393 Build_Invariant_Procedure_Body
2394 (Typ => Typ,
2395 Partial_Invariant => True);
2396 end if;
2397
2398 elsif Decls = Visible_Declarations (Context) then
2399 -- Preanalyze and resolve the invariants of a private type
2400 -- at the end of the visible declarations to catch potential
2401 -- errors. Inherited class-wide invariants are not included
2402 -- because they have already been resolved.
2403
2404 if Ekind (Typ) in E_Limited_Private_Type
2405 | E_Private_Type
2406 | E_Record_Type_With_Private
2407 and then Has_Own_Invariants (Typ)
2408 then
2409 Build_Invariant_Procedure_Body
2410 (Typ => Typ,
2411 Partial_Invariant => True);
2412 end if;
2413
2414 -- Preanalyze and resolve the Default_Initial_Condition
2415 -- assertion expression at the end of the declarations to
2416 -- catch any errors.
2417
2418 if Ekind (Typ) in E_Limited_Private_Type
2419 | E_Private_Type
2420 | E_Record_Type_With_Private
2421 and then Has_Own_DIC (Typ)
2422 then
2423 Build_DIC_Procedure_Body
2424 (Typ => Typ,
2425 Partial_DIC => True);
2426 end if;
2427
2428 elsif Decls = Private_Declarations (Context) then
2429
2430 -- Preanalyze and resolve the invariants of a private type's
2431 -- full view at the end of the private declarations to catch
2432 -- potential errors.
2433
2434 if (not Is_Private_Type (Typ)
2435 or else Present (Underlying_Full_View (Typ)))
2436 and then Has_Private_Declaration (Typ)
2437 and then Has_Invariants (Typ)
2438 then
2439 Build_Invariant_Procedure_Body (Typ);
2440 end if;
2441
2442 if (not Is_Private_Type (Typ)
2443 or else Present (Underlying_Full_View (Typ)))
2444 and then Has_Private_Declaration (Typ)
2445 and then Has_DIC (Typ)
2446 then
2447 Build_DIC_Procedure_Body (Typ);
2448 end if;
2449 end if;
2450 end if;
2451 end Build_Assertion_Bodies_For_Type;
2452
2453 -- Local variables
2454
2455 Decl : Node_Id;
2456 Decl_Id : Entity_Id;
2457
2458 -- Start of processing for Build_Assertion_Bodies
2459
2460 begin
2461 Decl := First (Decls);
2462 while Present (Decl) loop
2463 if Is_Declaration (Decl) then
2464 Decl_Id := Defining_Entity (Decl);
2465
2466 if Is_Type (Decl_Id) then
2467 Build_Assertion_Bodies_For_Type (Decl_Id);
2468 end if;
2469 end if;
2470
2471 Next (Decl);
2472 end loop;
2473 end Build_Assertion_Bodies;
2474
2475 ---------------------------
2476 -- Check_Entry_Contracts --
2477 ---------------------------
2478
2479 procedure Check_Entry_Contracts is
2480 ASN : Node_Id;
2481 Ent : Entity_Id;
2482 Exp : Node_Id;
2483
2484 begin
2485 Ent := First_Entity (Current_Scope);
2486 while Present (Ent) loop
2487
2488 -- This only concerns entries with pre/postconditions
2489
2490 if Ekind (Ent) = E_Entry
2491 and then Present (Contract (Ent))
2492 and then Present (Pre_Post_Conditions (Contract (Ent)))
2493 then
2494 ASN := Pre_Post_Conditions (Contract (Ent));
2495 Push_Scope (Ent);
2496 Install_Formals (Ent);
2497
2498 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2499 -- is performed on a copy of the pragma expression, to prevent
2500 -- modifying the original expression.
2501
2502 while Present (ASN) loop
2503 if Nkind (ASN) = N_Pragma then
2504 Exp :=
2505 New_Copy_Tree
2506 (Expression
2507 (First (Pragma_Argument_Associations (ASN))));
2508 Set_Parent (Exp, ASN);
2509
2510 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2511 end if;
2512
2513 ASN := Next_Pragma (ASN);
2514 end loop;
2515
2516 End_Scope;
2517 end if;
2518
2519 Next_Entity (Ent);
2520 end loop;
2521 end Check_Entry_Contracts;
2522
2523 ----------------------------------
2524 -- Contains_Lib_Incomplete_Type --
2525 ----------------------------------
2526
2527 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2528 Curr : Entity_Id;
2529
2530 begin
2531 -- Avoid looking through scopes that do not meet the precondition of
2532 -- Pkg not being within a library unit spec.
2533
2534 if not Is_Compilation_Unit (Pkg)
2535 and then not Is_Generic_Instance (Pkg)
2536 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2537 then
2538 -- Loop through all entities in the current scope to identify
2539 -- an entity that depends on a private type.
2540
2541 Curr := First_Entity (Pkg);
2542 loop
2543 if Nkind (Curr) in N_Entity
2544 and then Depends_On_Private (Curr)
2545 then
2546 return True;
2547 end if;
2548
2549 exit when Last_Entity (Current_Scope) = Curr;
2550 Next_Entity (Curr);
2551 end loop;
2552 end if;
2553
2554 return False;
2555 end Contains_Lib_Incomplete_Type;
2556
2557 --------------------------------------
2558 -- Handle_Late_Controlled_Primitive --
2559 --------------------------------------
2560
2561 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2562 Body_Spec : constant Node_Id := Specification (Body_Decl);
2563 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2564 Loc : constant Source_Ptr := Sloc (Body_Id);
2565 Params : constant List_Id :=
2566 Parameter_Specifications (Body_Spec);
2567 Spec : Node_Id;
2568 Spec_Id : Entity_Id;
2569 Typ : Node_Id;
2570
2571 begin
2572 -- Consider only procedure bodies whose name matches one of the three
2573 -- controlled primitives.
2574
2575 if Nkind (Body_Spec) /= N_Procedure_Specification
2576 or else Chars (Body_Id) not in Name_Adjust
2577 | Name_Finalize
2578 | Name_Initialize
2579 then
2580 return;
2581
2582 -- A controlled primitive must have exactly one formal which is not
2583 -- an anonymous access type.
2584
2585 elsif List_Length (Params) /= 1 then
2586 return;
2587 end if;
2588
2589 Typ := Parameter_Type (First (Params));
2590
2591 if Nkind (Typ) = N_Access_Definition then
2592 return;
2593 end if;
2594
2595 Find_Type (Typ);
2596
2597 -- The type of the formal must be derived from [Limited_]Controlled
2598
2599 if not Is_Controlled (Entity (Typ)) then
2600 return;
2601 end if;
2602
2603 -- Check whether a specification exists for this body. We do not
2604 -- analyze the spec of the body in full, because it will be analyzed
2605 -- again when the body is properly analyzed, and we cannot create
2606 -- duplicate entries in the formals chain. We look for an explicit
2607 -- specification because the body may be an overriding operation and
2608 -- an inherited spec may be present.
2609
2610 Spec_Id := Current_Entity (Body_Id);
2611
2612 while Present (Spec_Id) loop
2613 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2614 and then Scope (Spec_Id) = Current_Scope
2615 and then Present (First_Formal (Spec_Id))
2616 and then No (Next_Formal (First_Formal (Spec_Id)))
2617 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2618 and then Comes_From_Source (Spec_Id)
2619 then
2620 return;
2621 end if;
2622
2623 Spec_Id := Homonym (Spec_Id);
2624 end loop;
2625
2626 -- At this point the body is known to be a late controlled primitive.
2627 -- Generate a matching spec and insert it before the body. Note the
2628 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2629 -- tree in this case.
2630
2631 Spec := Copy_Separate_Tree (Body_Spec);
2632
2633 -- Ensure that the subprogram declaration does not inherit the null
2634 -- indicator from the body as we now have a proper spec/body pair.
2635
2636 Set_Null_Present (Spec, False);
2637
2638 -- Ensure that the freeze node is inserted after the declaration of
2639 -- the primitive since its expansion will freeze the primitive.
2640
2641 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2642
2643 Insert_Before_And_Analyze (Body_Decl, Decl);
2644 end Handle_Late_Controlled_Primitive;
2645
2646 ----------------------------------------
2647 -- Remove_Partial_Visible_Refinements --
2648 ----------------------------------------
2649
2650 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2651 State_Elmt : Elmt_Id;
2652 begin
2653 if Present (Abstract_States (Spec_Id)) then
2654 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2655 while Present (State_Elmt) loop
2656 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2657 Next_Elmt (State_Elmt);
2658 end loop;
2659 end if;
2660
2661 -- For a child unit, also hide the partial state refinement from
2662 -- ancestor packages.
2663
2664 if Is_Child_Unit (Spec_Id) then
2665 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2666 end if;
2667 end Remove_Partial_Visible_Refinements;
2668
2669 --------------------------------
2670 -- Remove_Visible_Refinements --
2671 --------------------------------
2672
2673 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2674 State_Elmt : Elmt_Id;
2675 begin
2676 if Present (Abstract_States (Spec_Id)) then
2677 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2678 while Present (State_Elmt) loop
2679 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2680 Next_Elmt (State_Elmt);
2681 end loop;
2682 end if;
2683 end Remove_Visible_Refinements;
2684
2685 ---------------------
2686 -- Resolve_Aspects --
2687 ---------------------
2688
2689 procedure Resolve_Aspects is
2690 E : Entity_Id;
2691
2692 begin
2693 E := First_Entity (Current_Scope);
2694 while Present (E) loop
2695 Resolve_Aspect_Expressions (E);
2696
2697 -- Now that the aspect expressions have been resolved, if this is
2698 -- at the end of the visible declarations, we can set the flag
2699 -- Known_To_Have_Preelab_Init properly on types declared in the
2700 -- visible part, which is needed for checking whether full types
2701 -- in the private part satisfy the Preelaborable_Initialization
2702 -- aspect of the partial view. We can't wait for the creation of
2703 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2704 -- freeze point may occur after the end of the package declaration
2705 -- (in the case of nested packages).
2706
2707 if Is_Type (E)
2708 and then L = Visible_Declarations (Parent (L))
2709 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2710 then
2711 declare
2712 ASN : constant Node_Id :=
2713 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2714 Expr : constant Node_Id := Expression (ASN);
2715 begin
2716 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2717 -- expression, or if the expression is True (or was folded
2718 -- to True), or if the expression is a conjunction of one or
2719 -- more Preelaborable_Initialization attributes applied to
2720 -- formal types and wasn't folded to False. (Note that
2721 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2722 -- Original_Node if needed, hence test for Standard_False.)
2723
2724 if No (Expr)
2725 or else (Is_Entity_Name (Expr)
2726 and then Entity (Expr) = Standard_True)
2727 or else
2728 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2729 and then
2730 not (Is_Entity_Name (Expr)
2731 and then Entity (Expr) = Standard_False))
2732 then
2733 Set_Known_To_Have_Preelab_Init (E);
2734 end if;
2735 end;
2736 end if;
2737
2738 Next_Entity (E);
2739 end loop;
2740 end Resolve_Aspects;
2741
2742 -- Local variables
2743
2744 Context : Node_Id := Empty;
2745 Ctrl_Typ : Entity_Id := Empty;
2746 Freeze_From : Entity_Id := Empty;
2747 Next_Decl : Node_Id;
2748
2749 -- Start of processing for Analyze_Declarations
2750
2751 begin
2752 Decl := First (L);
2753 while Present (Decl) loop
2754
2755 -- Complete analysis of declaration
2756
2757 Analyze (Decl);
2758 Next_Decl := Next (Decl);
2759
2760 if No (Freeze_From) then
2761 Freeze_From := First_Entity (Current_Scope);
2762 end if;
2763
2764 -- Remember if the declaration we just processed is the full type
2765 -- declaration of a controlled type (to handle late overriding of
2766 -- initialize, adjust or finalize).
2767
2768 if Nkind (Decl) = N_Full_Type_Declaration
2769 and then Is_Controlled (Defining_Identifier (Decl))
2770 then
2771 Ctrl_Typ := Defining_Identifier (Decl);
2772 end if;
2773
2774 -- At the end of a declarative part, freeze remaining entities
2775 -- declared in it. The end of the visible declarations of package
2776 -- specification is not the end of a declarative part if private
2777 -- declarations are present. The end of a package declaration is a
2778 -- freezing point only if it a library package. A task definition or
2779 -- protected type definition is not a freeze point either. Finally,
2780 -- we do not freeze entities in generic scopes, because there is no
2781 -- code generated for them and freeze nodes will be generated for
2782 -- the instance.
2783
2784 -- The end of a package instantiation is not a freeze point, but
2785 -- for now we make it one, because the generic body is inserted
2786 -- (currently) immediately after. Generic instantiations will not
2787 -- be a freeze point once delayed freezing of bodies is implemented.
2788 -- (This is needed in any case for early instantiations ???).
2789
2790 if No (Next_Decl) then
2791 if Nkind (Parent (L)) = N_Component_List then
2792 null;
2793
2794 elsif Nkind (Parent (L)) in
2795 N_Protected_Definition | N_Task_Definition
2796 then
2797 Check_Entry_Contracts;
2798
2799 elsif Nkind (Parent (L)) /= N_Package_Specification then
2800 if Nkind (Parent (L)) = N_Package_Body then
2801 Freeze_From := First_Entity (Current_Scope);
2802 end if;
2803
2804 -- There may have been several freezing points previously,
2805 -- for example object declarations or subprogram bodies, but
2806 -- at the end of a declarative part we check freezing from
2807 -- the beginning, even though entities may already be frozen,
2808 -- in order to perform visibility checks on delayed aspects.
2809
2810 Adjust_Decl;
2811
2812 -- If the current scope is a generic subprogram body. Skip the
2813 -- generic formal parameters that are not frozen here.
2814
2815 if Is_Subprogram (Current_Scope)
2816 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2817 N_Generic_Subprogram_Declaration
2818 and then Present (First_Entity (Current_Scope))
2819 then
2820 while Is_Generic_Formal (Freeze_From) loop
2821 Next_Entity (Freeze_From);
2822 end loop;
2823
2824 Freeze_All (Freeze_From, Decl);
2825 Freeze_From := Last_Entity (Current_Scope);
2826
2827 else
2828 -- For declarations in a subprogram body there is no issue
2829 -- with name resolution in aspect specifications.
2830
2831 Freeze_All (First_Entity (Current_Scope), Decl);
2832 Freeze_From := Last_Entity (Current_Scope);
2833 end if;
2834
2835 -- Current scope is a package specification
2836
2837 elsif Scope (Current_Scope) /= Standard_Standard
2838 and then not Is_Child_Unit (Current_Scope)
2839 and then No (Generic_Parent (Parent (L)))
2840 then
2841 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2842 -- resolved at the end of the immediately enclosing declaration
2843 -- list (AI05-0183-1).
2844
2845 Resolve_Aspects;
2846
2847 elsif L /= Visible_Declarations (Parent (L))
2848 or else Is_Empty_List (Private_Declarations (Parent (L)))
2849 then
2850 Adjust_Decl;
2851
2852 -- End of a package declaration
2853
2854 -- This is a freeze point because it is the end of a
2855 -- compilation unit.
2856
2857 Freeze_All (First_Entity (Current_Scope), Decl);
2858 Freeze_From := Last_Entity (Current_Scope);
2859
2860 -- At the end of the visible declarations the expressions in
2861 -- aspects of all entities declared so far must be resolved.
2862 -- The entities themselves might be frozen later, and the
2863 -- generated pragmas and attribute definition clauses analyzed
2864 -- in full at that point, but name resolution must take place
2865 -- now.
2866 -- In addition to being the proper semantics, this is mandatory
2867 -- within generic units, because global name capture requires
2868 -- those expressions to be analyzed, given that the generated
2869 -- pragmas do not appear in the original generic tree.
2870
2871 elsif Serious_Errors_Detected = 0 then
2872 Resolve_Aspects;
2873 end if;
2874
2875 -- If next node is a body then freeze all types before the body.
2876 -- An exception occurs for some expander-generated bodies. If these
2877 -- are generated at places where in general language rules would not
2878 -- allow a freeze point, then we assume that the expander has
2879 -- explicitly checked that all required types are properly frozen,
2880 -- and we do not cause general freezing here. This special circuit
2881 -- is used when the encountered body is marked as having already
2882 -- been analyzed.
2883
2884 -- In all other cases (bodies that come from source, and expander
2885 -- generated bodies that have not been analyzed yet), freeze all
2886 -- types now. Note that in the latter case, the expander must take
2887 -- care to attach the bodies at a proper place in the tree so as to
2888 -- not cause unwanted freezing at that point.
2889
2890 -- It is also necessary to check for a case where both an expression
2891 -- function is used and the current scope depends on an incomplete
2892 -- private type from a library unit, otherwise premature freezing of
2893 -- the private type will occur.
2894
2895 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2896 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2897 or else not Was_Expression_Function (Next_Decl))
2898 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2899 and then not Contains_Lib_Incomplete_Type
2900 (Current_Scope)))
2901 then
2902 -- When a controlled type is frozen, the expander generates stream
2903 -- and controlled-type support routines. If the freeze is caused
2904 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2905 -- expander will end up using the wrong version of these routines,
2906 -- as the body has not been processed yet. To remedy this, detect
2907 -- a late controlled primitive and create a proper spec for it.
2908 -- This ensures that the primitive will override its inherited
2909 -- counterpart before the freeze takes place.
2910
2911 -- If the declaration we just processed is a body, do not attempt
2912 -- to examine Next_Decl as the late primitive idiom can only apply
2913 -- to the first encountered body.
2914
2915 -- ??? A cleaner approach may be possible and/or this solution
2916 -- could be extended to general-purpose late primitives.
2917
2918 if Present (Ctrl_Typ) then
2919
2920 -- No need to continue searching for late body overriding if
2921 -- the controlled type is already frozen.
2922
2923 if Is_Frozen (Ctrl_Typ) then
2924 Ctrl_Typ := Empty;
2925
2926 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2927 Handle_Late_Controlled_Primitive (Next_Decl);
2928 end if;
2929 end if;
2930
2931 Adjust_Decl;
2932
2933 -- The generated body of an expression function does not freeze,
2934 -- unless it is a completion, in which case only the expression
2935 -- itself freezes. This is handled when the body itself is
2936 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2937
2938 Freeze_All (Freeze_From, Decl);
2939 Freeze_From := Last_Entity (Current_Scope);
2940 end if;
2941
2942 Decl := Next_Decl;
2943 end loop;
2944
2945 -- Post-freezing actions
2946
2947 if Present (L) then
2948 Context := Parent (L);
2949
2950 -- Certain contract annotations have forward visibility semantics and
2951 -- must be analyzed after all declarative items have been processed.
2952 -- This timing ensures that entities referenced by such contracts are
2953 -- visible.
2954
2955 -- Analyze the contract of an immediately enclosing package spec or
2956 -- body first because other contracts may depend on its information.
2957
2958 if Nkind (Context) = N_Package_Body then
2959 Analyze_Package_Body_Contract (Defining_Entity (Context));
2960
2961 elsif Nkind (Context) = N_Package_Specification then
2962 Analyze_Package_Contract (Defining_Entity (Context));
2963 end if;
2964
2965 -- Analyze the contracts of various constructs in the declarative
2966 -- list.
2967
2968 Analyze_Contracts (L);
2969
2970 if Nkind (Context) = N_Package_Body then
2971
2972 -- Ensure that all abstract states and objects declared in the
2973 -- state space of a package body are utilized as constituents.
2974
2975 Check_Unused_Body_States (Defining_Entity (Context));
2976
2977 -- State refinements are visible up to the end of the package body
2978 -- declarations. Hide the state refinements from visibility to
2979 -- restore the original state conditions.
2980
2981 Remove_Visible_Refinements (Corresponding_Spec (Context));
2982 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2983
2984 elsif Nkind (Context) = N_Package_Specification then
2985
2986 -- Partial state refinements are visible up to the end of the
2987 -- package spec declarations. Hide the partial state refinements
2988 -- from visibility to restore the original state conditions.
2989
2990 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2991 end if;
2992
2993 -- Verify that all abstract states found in any package declared in
2994 -- the input declarative list have proper refinements. The check is
2995 -- performed only when the context denotes a block, entry, package,
2996 -- protected, subprogram, or task body (SPARK RM 7.1.4(4) and SPARK
2997 -- RM 7.2.2(3)).
2998
2999 Check_State_Refinements (Context);
3000
3001 -- Create the subprogram bodies which verify the run-time semantics
3002 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
3003 -- types within the current declarative list. This ensures that all
3004 -- assertion expressions are preanalyzed and resolved at the end of
3005 -- the declarative part. Note that the resolution happens even when
3006 -- freezing does not take place.
3007
3008 Build_Assertion_Bodies (L, Context);
3009 end if;
3010 end Analyze_Declarations;
3011
3012 -----------------------------------
3013 -- Analyze_Full_Type_Declaration --
3014 -----------------------------------
3015
3016 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
3017 Def : constant Node_Id := Type_Definition (N);
3018 Def_Id : constant Entity_Id := Defining_Identifier (N);
3019 T : Entity_Id;
3020 Prev : Entity_Id;
3021
3022 Is_Remote : constant Boolean :=
3023 (Is_Remote_Types (Current_Scope)
3024 or else Is_Remote_Call_Interface (Current_Scope))
3025 and then not (In_Private_Part (Current_Scope)
3026 or else In_Package_Body (Current_Scope));
3027
3028 procedure Check_Nonoverridable_Aspects;
3029 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
3030 -- be overridden, and can only be confirmed on derivation.
3031
3032 procedure Check_Ops_From_Incomplete_Type;
3033 -- If there is a tagged incomplete partial view of the type, traverse
3034 -- the primitives of the incomplete view and change the type of any
3035 -- controlling formals and result to indicate the full view. The
3036 -- primitives will be added to the full type's primitive operations
3037 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
3038 -- is called from Process_Incomplete_Dependents).
3039
3040 ----------------------------------
3041 -- Check_Nonoverridable_Aspects --
3042 ----------------------------------
3043
3044 procedure Check_Nonoverridable_Aspects is
3045 function Get_Aspect_Spec
3046 (Specs : List_Id;
3047 Aspect_Name : Name_Id) return Node_Id;
3048 -- Check whether a list of aspect specifications includes an entry
3049 -- for a specific aspect. The list is either that of a partial or
3050 -- a full view.
3051
3052 ---------------------
3053 -- Get_Aspect_Spec --
3054 ---------------------
3055
3056 function Get_Aspect_Spec
3057 (Specs : List_Id;
3058 Aspect_Name : Name_Id) return Node_Id
3059 is
3060 Spec : Node_Id;
3061
3062 begin
3063 Spec := First (Specs);
3064 while Present (Spec) loop
3065 if Chars (Identifier (Spec)) = Aspect_Name then
3066 return Spec;
3067 end if;
3068 Next (Spec);
3069 end loop;
3070
3071 return Empty;
3072 end Get_Aspect_Spec;
3073
3074 -- Local variables
3075
3076 Prev_Aspects : constant List_Id :=
3077 Aspect_Specifications (Parent (Def_Id));
3078 Par_Type : Entity_Id;
3079 Prev_Aspect : Node_Id;
3080
3081 -- Start of processing for Check_Nonoverridable_Aspects
3082
3083 begin
3084 -- Get parent type of derived type. Note that Prev is the entity in
3085 -- the partial declaration, but its contents are now those of full
3086 -- view, while Def_Id reflects the partial view.
3087
3088 if Is_Private_Type (Def_Id) then
3089 Par_Type := Etype (Full_View (Def_Id));
3090 else
3091 Par_Type := Etype (Def_Id);
3092 end if;
3093
3094 -- If there is an inherited Implicit_Dereference, verify that it is
3095 -- made explicit in the partial view.
3096
3097 if Has_Discriminants (Base_Type (Par_Type))
3098 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3099 and then Present (Discriminant_Specifications (Parent (Prev)))
3100 and then Present (Get_Reference_Discriminant (Par_Type))
3101 then
3102 Prev_Aspect :=
3103 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3104
3105 if No (Prev_Aspect)
3106 and then Present
3107 (Discriminant_Specifications
3108 (Original_Node (Parent (Prev))))
3109 then
3110 Error_Msg_N
3111 ("type does not inherit implicit dereference", Prev);
3112
3113 else
3114 -- If one of the views has the aspect specified, verify that it
3115 -- is consistent with that of the parent.
3116
3117 declare
3118 Cur_Discr : constant Entity_Id :=
3119 Get_Reference_Discriminant (Prev);
3120 Par_Discr : constant Entity_Id :=
3121 Get_Reference_Discriminant (Par_Type);
3122
3123 begin
3124 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3125 Error_Msg_N
3126 ("aspect inconsistent with that of parent", N);
3127 end if;
3128
3129 -- Check that specification in partial view matches the
3130 -- inherited aspect. Compare names directly because aspect
3131 -- expression may not be analyzed.
3132
3133 if Present (Prev_Aspect)
3134 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3135 and then Chars (Expression (Prev_Aspect)) /=
3136 Chars (Cur_Discr)
3137 then
3138 Error_Msg_N
3139 ("aspect inconsistent with that of parent", N);
3140 end if;
3141 end;
3142 end if;
3143 end if;
3144
3145 -- What about other nonoverridable aspects???
3146 end Check_Nonoverridable_Aspects;
3147
3148 ------------------------------------
3149 -- Check_Ops_From_Incomplete_Type --
3150 ------------------------------------
3151
3152 procedure Check_Ops_From_Incomplete_Type is
3153 Elmt : Elmt_Id;
3154 Formal : Entity_Id;
3155 Op : Entity_Id;
3156
3157 begin
3158 if Prev /= T
3159 and then Ekind (Prev) = E_Incomplete_Type
3160 and then Is_Tagged_Type (Prev)
3161 and then Is_Tagged_Type (T)
3162 and then Present (Primitive_Operations (Prev))
3163 then
3164 Elmt := First_Elmt (Primitive_Operations (Prev));
3165 while Present (Elmt) loop
3166 Op := Node (Elmt);
3167
3168 Formal := First_Formal (Op);
3169 while Present (Formal) loop
3170 if Etype (Formal) = Prev then
3171 Set_Etype (Formal, T);
3172 end if;
3173
3174 Next_Formal (Formal);
3175 end loop;
3176
3177 if Etype (Op) = Prev then
3178 Set_Etype (Op, T);
3179 end if;
3180
3181 Next_Elmt (Elmt);
3182 end loop;
3183 end if;
3184 end Check_Ops_From_Incomplete_Type;
3185
3186 -- Start of processing for Analyze_Full_Type_Declaration
3187
3188 begin
3189 Prev := Find_Type_Name (N);
3190
3191 -- The full view, if present, now points to the current type. If there
3192 -- is an incomplete partial view, set a link to it, to simplify the
3193 -- retrieval of primitive operations of the type.
3194
3195 -- Ada 2005 (AI-50217): If the type was previously decorated when
3196 -- imported through a LIMITED WITH clause, it appears as incomplete
3197 -- but has no full view.
3198
3199 if Ekind (Prev) = E_Incomplete_Type
3200 and then Present (Full_View (Prev))
3201 then
3202 T := Full_View (Prev);
3203 Set_Incomplete_View (N, Prev);
3204 else
3205 T := Prev;
3206 end if;
3207
3208 Set_Is_Pure (T, Is_Pure (Current_Scope));
3209
3210 -- We set the flag Is_First_Subtype here. It is needed to set the
3211 -- corresponding flag for the Implicit class-wide-type created
3212 -- during tagged types processing.
3213
3214 Set_Is_First_Subtype (T, True);
3215
3216 -- Only composite types other than array types are allowed to have
3217 -- discriminants.
3218
3219 case Nkind (Def) is
3220
3221 -- For derived types, the rule will be checked once we've figured
3222 -- out the parent type.
3223
3224 when N_Derived_Type_Definition =>
3225 null;
3226
3227 -- For record types, discriminants are allowed.
3228
3229 when N_Record_Definition =>
3230 null;
3231
3232 when others =>
3233 if Present (Discriminant_Specifications (N)) then
3234 Error_Msg_N
3235 ("elementary or array type cannot have discriminants",
3236 Defining_Identifier
3237 (First (Discriminant_Specifications (N))));
3238 end if;
3239 end case;
3240
3241 -- Elaborate the type definition according to kind, and generate
3242 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3243 -- already done (this happens during the reanalysis that follows a call
3244 -- to the high level optimizer).
3245
3246 if not Analyzed (T) then
3247 Set_Analyzed (T);
3248
3249 -- Set the SPARK mode from the current context
3250
3251 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3252 Set_SPARK_Pragma_Inherited (T);
3253
3254 case Nkind (Def) is
3255 when N_Access_To_Subprogram_Definition =>
3256 Access_Subprogram_Declaration (T, Def);
3257
3258 -- If this is a remote access to subprogram, we must create the
3259 -- equivalent fat pointer type, and related subprograms.
3260
3261 if Is_Remote then
3262 Process_Remote_AST_Declaration (N);
3263 end if;
3264
3265 -- Validate categorization rule against access type declaration
3266 -- usually a violation in Pure unit, Shared_Passive unit.
3267
3268 Validate_Access_Type_Declaration (T, N);
3269
3270 -- If the type has contracts, we create the corresponding
3271 -- wrapper at once, before analyzing the aspect specifications,
3272 -- so that pre/postconditions can be handled directly on the
3273 -- generated wrapper.
3274
3275 if Ada_Version >= Ada_2022
3276 and then Present (Aspect_Specifications (N))
3277 and then Expander_Active
3278 then
3279 Build_Access_Subprogram_Wrapper (N);
3280 end if;
3281
3282 when N_Access_To_Object_Definition =>
3283 Access_Type_Declaration (T, Def);
3284
3285 -- Validate categorization rule against access type declaration
3286 -- usually a violation in Pure unit, Shared_Passive unit.
3287
3288 Validate_Access_Type_Declaration (T, N);
3289
3290 -- If we are in a Remote_Call_Interface package and define a
3291 -- RACW, then calling stubs and specific stream attributes
3292 -- must be added.
3293
3294 if Is_Remote
3295 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3296 then
3297 Add_RACW_Features (Def_Id);
3298 end if;
3299
3300 when N_Array_Type_Definition =>
3301 Array_Type_Declaration (T, Def);
3302
3303 when N_Derived_Type_Definition =>
3304 Derived_Type_Declaration (T, N, T /= Def_Id);
3305
3306 -- Save the scenario for examination by the ABE Processing
3307 -- phase.
3308
3309 Record_Elaboration_Scenario (N);
3310
3311 when N_Enumeration_Type_Definition =>
3312 Enumeration_Type_Declaration (T, Def);
3313
3314 when N_Floating_Point_Definition =>
3315 Floating_Point_Type_Declaration (T, Def);
3316
3317 when N_Decimal_Fixed_Point_Definition =>
3318 Decimal_Fixed_Point_Type_Declaration (T, Def);
3319
3320 when N_Ordinary_Fixed_Point_Definition =>
3321 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3322
3323 when N_Signed_Integer_Type_Definition =>
3324 Signed_Integer_Type_Declaration (T, Def);
3325
3326 when N_Modular_Type_Definition =>
3327 Modular_Type_Declaration (T, Def);
3328
3329 when N_Record_Definition =>
3330 Record_Type_Declaration (T, N, Prev);
3331
3332 -- If declaration has a parse error, nothing to elaborate.
3333
3334 when N_Error =>
3335 null;
3336
3337 when others =>
3338 raise Program_Error;
3339 end case;
3340 end if;
3341
3342 if Etype (T) = Any_Type then
3343 return;
3344 end if;
3345
3346 -- Set the primitives list of the full type and its base type when
3347 -- needed. T may be E_Void in cases of earlier errors, and in that
3348 -- case we bypass this.
3349
3350 if Ekind (T) /= E_Void then
3351 if No (Direct_Primitive_Operations (T)) then
3352 if Etype (T) = T then
3353 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3354
3355 -- If Etype of T is the base type (as opposed to a parent type)
3356 -- and already has an associated list of primitive operations,
3357 -- then set T's primitive list to the base type's list. Otherwise,
3358 -- create a new empty primitives list and share the list between
3359 -- T and its base type. The lists need to be shared in common.
3360
3361 elsif Etype (T) = Base_Type (T) then
3362
3363 if No (Direct_Primitive_Operations (Base_Type (T))) then
3364 Set_Direct_Primitive_Operations
3365 (Base_Type (T), New_Elmt_List);
3366 end if;
3367
3368 Set_Direct_Primitive_Operations
3369 (T, Direct_Primitive_Operations (Base_Type (T)));
3370
3371 -- Case where the Etype is a parent type, so we need a new
3372 -- primitives list for T.
3373
3374 else
3375 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3376 end if;
3377
3378 -- If T already has a Direct_Primitive_Operations list but its
3379 -- base type doesn't then set the base type's list to T's list.
3380
3381 elsif No (Direct_Primitive_Operations (Base_Type (T))) then
3382 Set_Direct_Primitive_Operations
3383 (Base_Type (T), Direct_Primitive_Operations (T));
3384 end if;
3385 end if;
3386
3387 -- Some common processing for all types
3388
3389 Set_Depends_On_Private (T, Has_Private_Component (T));
3390 Check_Ops_From_Incomplete_Type;
3391
3392 -- Both the declared entity, and its anonymous base type if one was
3393 -- created, need freeze nodes allocated.
3394
3395 declare
3396 B : constant Entity_Id := Base_Type (T);
3397
3398 begin
3399 -- In the case where the base type differs from the first subtype, we
3400 -- pre-allocate a freeze node, and set the proper link to the first
3401 -- subtype. Freeze_Entity will use this preallocated freeze node when
3402 -- it freezes the entity.
3403
3404 -- This does not apply if the base type is a generic type, whose
3405 -- declaration is independent of the current derived definition.
3406
3407 if B /= T and then not Is_Generic_Type (B) then
3408 Ensure_Freeze_Node (B);
3409 Set_First_Subtype_Link (Freeze_Node (B), T);
3410 end if;
3411
3412 -- A type that is imported through a limited_with clause cannot
3413 -- generate any code, and thus need not be frozen. However, an access
3414 -- type with an imported designated type needs a finalization list,
3415 -- which may be referenced in some other package that has non-limited
3416 -- visibility on the designated type. Thus we must create the
3417 -- finalization list at the point the access type is frozen, to
3418 -- prevent unsatisfied references at link time.
3419
3420 if not From_Limited_With (T) or else Is_Access_Type (T) then
3421 Set_Has_Delayed_Freeze (T);
3422 end if;
3423 end;
3424
3425 -- Case where T is the full declaration of some private type which has
3426 -- been swapped in Defining_Identifier (N).
3427
3428 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3429 Process_Full_View (N, T, Def_Id);
3430
3431 -- Record the reference. The form of this is a little strange, since
3432 -- the full declaration has been swapped in. So the first parameter
3433 -- here represents the entity to which a reference is made which is
3434 -- the "real" entity, i.e. the one swapped in, and the second
3435 -- parameter provides the reference location.
3436
3437 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3438 -- since we don't want a complaint about the full type being an
3439 -- unwanted reference to the private type
3440
3441 declare
3442 B : constant Boolean := Has_Pragma_Unreferenced (T);
3443 begin
3444 Set_Has_Pragma_Unreferenced (T, False);
3445 Generate_Reference (T, T, 'c');
3446 Set_Has_Pragma_Unreferenced (T, B);
3447 end;
3448
3449 Set_Completion_Referenced (Def_Id);
3450
3451 -- For completion of incomplete type, process incomplete dependents
3452 -- and always mark the full type as referenced (it is the incomplete
3453 -- type that we get for any real reference).
3454
3455 elsif Ekind (Prev) = E_Incomplete_Type then
3456 Process_Incomplete_Dependents (N, T, Prev);
3457 Generate_Reference (Prev, Def_Id, 'c');
3458 Set_Completion_Referenced (Def_Id);
3459
3460 -- If not private type or incomplete type completion, this is a real
3461 -- definition of a new entity, so record it.
3462
3463 else
3464 Generate_Definition (Def_Id);
3465 end if;
3466
3467 if Chars (Scope (Def_Id)) = Name_System
3468 and then Chars (Def_Id) = Name_Address
3469 and then In_Predefined_Unit (N)
3470 then
3471 Set_Is_Descendant_Of_Address (Def_Id);
3472 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3473 Set_Is_Descendant_Of_Address (Prev);
3474 end if;
3475
3476 Set_Optimize_Alignment_Flags (Def_Id);
3477 Check_Eliminated (Def_Id);
3478
3479 -- If the declaration is a completion and aspects are present, apply
3480 -- them to the entity for the type which is currently the partial
3481 -- view, but which is the one that will be frozen.
3482
3483 -- In most cases the partial view is a private type, and both views
3484 -- appear in different declarative parts. In the unusual case where
3485 -- the partial view is incomplete, perform the analysis on the
3486 -- full view, to prevent freezing anomalies with the corresponding
3487 -- class-wide type, which otherwise might be frozen before the
3488 -- dispatch table is built.
3489
3490 if Prev /= Def_Id
3491 and then Ekind (Prev) /= E_Incomplete_Type
3492 then
3493 Analyze_Aspect_Specifications (N, Prev);
3494
3495 -- Normal case
3496
3497 else
3498 Analyze_Aspect_Specifications (N, Def_Id);
3499 end if;
3500
3501 if Is_Derived_Type (Prev)
3502 and then Def_Id /= Prev
3503 then
3504 Check_Nonoverridable_Aspects;
3505 end if;
3506
3507 -- Check for tagged type declaration at library level
3508
3509 if Is_Tagged_Type (T)
3510 and then not Is_Library_Level_Entity (T)
3511 then
3512 Check_Restriction (No_Local_Tagged_Types, T);
3513 end if;
3514
3515 -- Derived tagged types inherit aspect First_Controlling_Parameter
3516 -- from their parent type and also from implemented interface types.
3517 -- We implicitly perform inheritance here and will check for the
3518 -- explicit confirming pragma or aspect in the sources when this type
3519 -- is frozen (required for pragmas since they are placed at any place
3520 -- after the type declaration; otherwise, when the pragma is used after
3521 -- some non-first-controlling-parameter primitive, the reported errors
3522 -- and warning would differ when the pragma is used).
3523
3524 if Is_Tagged_Type (T)
3525 and then Is_Derived_Type (T)
3526 and then not Has_First_Controlling_Parameter_Aspect (T)
3527 then
3528 pragma Assert (Etype (T) /= T);
3529
3530 if Has_First_Controlling_Parameter_Aspect (Etype (T)) then
3531 Set_Has_First_Controlling_Parameter_Aspect (T);
3532
3533 elsif Present (Interfaces (T))
3534 and then not Is_Empty_Elmt_List (Interfaces (T))
3535 then
3536 declare
3537 Elmt : Elmt_Id := First_Elmt (Interfaces (T));
3538 Iface : Entity_Id;
3539
3540 begin
3541 while Present (Elmt) loop
3542 Iface := Node (Elmt);
3543
3544 if Has_First_Controlling_Parameter_Aspect (Iface) then
3545 Set_Has_First_Controlling_Parameter_Aspect (T);
3546 exit;
3547 end if;
3548
3549 Next_Elmt (Elmt);
3550 end loop;
3551 end;
3552 end if;
3553 end if;
3554 end Analyze_Full_Type_Declaration;
3555
3556 ----------------------------------
3557 -- Analyze_Incomplete_Type_Decl --
3558 ----------------------------------
3559
3560 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3561 F : constant Boolean := Is_Pure (Current_Scope);
3562 T : Entity_Id;
3563
3564 begin
3565 Generate_Definition (Defining_Identifier (N));
3566
3567 -- Process an incomplete declaration. The identifier must not have been
3568 -- declared already in the scope. However, an incomplete declaration may
3569 -- appear in the private part of a package, for a private type that has
3570 -- already been declared.
3571
3572 -- In this case, the discriminants (if any) must match
3573
3574 T := Find_Type_Name (N);
3575
3576 Mutate_Ekind (T, E_Incomplete_Type);
3577 Set_Etype (T, T);
3578 Set_Is_First_Subtype (T);
3579 Reinit_Size_Align (T);
3580
3581 -- Set the SPARK mode from the current context
3582
3583 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3584 Set_SPARK_Pragma_Inherited (T);
3585
3586 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3587 -- incomplete types.
3588
3589 if Tagged_Present (N) then
3590 Set_Is_Tagged_Type (T, True);
3591 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3592 Make_Class_Wide_Type (T);
3593 end if;
3594
3595 -- Initialize the list of primitive operations to an empty list,
3596 -- to cover tagged types as well as untagged types. For untagged
3597 -- types this is used either to analyze the call as legal when
3598 -- GNAT extensions are allowed, or to give better error messages.
3599
3600 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3601
3602 Set_Stored_Constraint (T, No_Elist);
3603
3604 if Present (Discriminant_Specifications (N)) then
3605 Push_Scope (T);
3606 Process_Discriminants (N);
3607 End_Scope;
3608 end if;
3609
3610 -- If the type has discriminants, nontrivial subtypes may be declared
3611 -- before the full view of the type. The full views of those subtypes
3612 -- will be built after the full view of the type.
3613
3614 Set_Private_Dependents (T, New_Elmt_List);
3615 Set_Is_Pure (T, F);
3616 end Analyze_Incomplete_Type_Decl;
3617
3618 -----------------------------------
3619 -- Analyze_Interface_Declaration --
3620 -----------------------------------
3621
3622 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3623 CW : constant Entity_Id := Class_Wide_Type (T);
3624
3625 begin
3626 Set_Is_Tagged_Type (T);
3627 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3628
3629 Set_Is_Limited_Record (T, Limited_Present (Def)
3630 or else Task_Present (Def)
3631 or else Protected_Present (Def)
3632 or else Synchronized_Present (Def));
3633
3634 -- Type is abstract if full declaration carries keyword, or if previous
3635 -- partial view did.
3636
3637 Set_Is_Abstract_Type (T);
3638 Set_Is_Interface (T);
3639
3640 -- Type is a limited interface if it includes the keyword limited, task,
3641 -- protected, or synchronized.
3642
3643 Set_Is_Limited_Interface
3644 (T, Limited_Present (Def)
3645 or else Protected_Present (Def)
3646 or else Synchronized_Present (Def)
3647 or else Task_Present (Def));
3648
3649 Set_Interfaces (T, New_Elmt_List);
3650 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3651
3652 -- Complete the decoration of the class-wide entity if it was already
3653 -- built (i.e. during the creation of the limited view)
3654
3655 if Present (CW) then
3656 Set_Is_Interface (CW);
3657 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3658 end if;
3659
3660 -- Check runtime support for synchronized interfaces
3661
3662 if Is_Concurrent_Interface (T)
3663 and then not RTE_Available (RE_Select_Specific_Data)
3664 then
3665 Error_Msg_CRT ("synchronized interfaces", T);
3666 end if;
3667 end Analyze_Interface_Declaration;
3668
3669 -----------------------------
3670 -- Analyze_Itype_Reference --
3671 -----------------------------
3672
3673 -- Nothing to do. This node is placed in the tree only for the benefit of
3674 -- back end processing, and has no effect on the semantic processing.
3675
3676 procedure Analyze_Itype_Reference (N : Node_Id) is
3677 begin
3678 pragma Assert (Is_Itype (Itype (N)));
3679 null;
3680 end Analyze_Itype_Reference;
3681
3682 --------------------------------
3683 -- Analyze_Number_Declaration --
3684 --------------------------------
3685
3686 procedure Analyze_Number_Declaration (N : Node_Id) is
3687 E : Node_Id := Expression (N);
3688 Id : constant Entity_Id := Defining_Identifier (N);
3689 Index : Interp_Index;
3690 It : Interp;
3691 T : Entity_Id;
3692
3693 begin
3694 Generate_Definition (Id);
3695 Enter_Name (Id);
3696
3697 -- This is an optimization of a common case of an integer literal
3698
3699 if Nkind (E) = N_Integer_Literal then
3700 Set_Is_Static_Expression (E, True);
3701 Set_Etype (E, Universal_Integer);
3702
3703 Set_Etype (Id, Universal_Integer);
3704 Mutate_Ekind (Id, E_Named_Integer);
3705 Set_Is_Frozen (Id, True);
3706
3707 Set_Debug_Info_Needed (Id);
3708 return;
3709 end if;
3710
3711 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3712
3713 -- Replace Error by integer zero, which seems least likely to cause
3714 -- cascaded errors.
3715
3716 if E = Error then
3717 pragma Assert (Serious_Errors_Detected > 0);
3718 E := Make_Integer_Literal (Sloc (N), Uint_0);
3719 Set_Expression (N, E);
3720 Set_Error_Posted (E);
3721 end if;
3722
3723 Analyze (E);
3724
3725 -- Verify that the expression is static and numeric. If
3726 -- the expression is overloaded, we apply the preference
3727 -- rule that favors root numeric types.
3728
3729 if not Is_Overloaded (E) then
3730 T := Etype (E);
3731 if Has_Dynamic_Predicate_Aspect (T)
3732 or else Has_Ghost_Predicate_Aspect (T)
3733 then
3734 Error_Msg_N
3735 ("subtype has non-static predicate, "
3736 & "not allowed in number declaration", N);
3737 end if;
3738
3739 else
3740 T := Any_Type;
3741
3742 Get_First_Interp (E, Index, It);
3743 while Present (It.Typ) loop
3744 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3745 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3746 then
3747 if T = Any_Type then
3748 T := It.Typ;
3749
3750 elsif Is_Universal_Numeric_Type (It.Typ) then
3751 -- Choose universal interpretation over any other
3752
3753 T := It.Typ;
3754 exit;
3755 end if;
3756 end if;
3757
3758 Get_Next_Interp (Index, It);
3759 end loop;
3760 end if;
3761
3762 if Is_Integer_Type (T) then
3763 Resolve (E, T);
3764 Set_Etype (Id, Universal_Integer);
3765 Mutate_Ekind (Id, E_Named_Integer);
3766
3767 elsif Is_Real_Type (T) then
3768
3769 -- Because the real value is converted to universal_real, this is a
3770 -- legal context for a universal fixed expression.
3771
3772 if T = Universal_Fixed then
3773 declare
3774 Loc : constant Source_Ptr := Sloc (N);
3775 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3776 Subtype_Mark =>
3777 New_Occurrence_Of (Universal_Real, Loc),
3778 Expression => Relocate_Node (E));
3779
3780 begin
3781 Rewrite (E, Conv);
3782 Analyze (E);
3783 end;
3784
3785 elsif T = Any_Fixed then
3786 Error_Msg_N ("illegal context for mixed mode operation", E);
3787
3788 -- Expression is of the form : universal_fixed * integer. Try to
3789 -- resolve as universal_real.
3790
3791 T := Universal_Real;
3792 Set_Etype (E, T);
3793 end if;
3794
3795 Resolve (E, T);
3796 Set_Etype (Id, Universal_Real);
3797 Mutate_Ekind (Id, E_Named_Real);
3798
3799 else
3800 Wrong_Type (E, Any_Numeric);
3801 Resolve (E, T);
3802
3803 Set_Etype (Id, T);
3804 Mutate_Ekind (Id, E_Constant);
3805 Set_Never_Set_In_Source (Id, True);
3806 Set_Is_True_Constant (Id, True);
3807 return;
3808 end if;
3809
3810 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3811 Set_Etype (E, Etype (Id));
3812 end if;
3813
3814 if not Is_OK_Static_Expression (E) then
3815 Flag_Non_Static_Expr
3816 ("non-static expression used in number declaration!", E);
3817 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3818 Set_Etype (E, Any_Type);
3819 end if;
3820
3821 Analyze_Dimension (N);
3822 end Analyze_Number_Declaration;
3823
3824 --------------------------------
3825 -- Analyze_Object_Declaration --
3826 --------------------------------
3827
3828 -- WARNING: This routine manages Ghost regions. Return statements must be
3829 -- replaced by gotos which jump to the end of the routine and restore the
3830 -- Ghost mode.
3831
3832 procedure Analyze_Object_Declaration (N : Node_Id) is
3833 Loc : constant Source_Ptr := Sloc (N);
3834 Id : constant Entity_Id := Defining_Identifier (N);
3835 Next_Decl : constant Node_Id := Next (N);
3836
3837 Act_T : Entity_Id;
3838 T : Entity_Id;
3839
3840 E : Node_Id := Expression (N);
3841 -- E is set to Expression (N) throughout this routine. When Expression
3842 -- (N) is modified, E is changed accordingly.
3843
3844 procedure Apply_External_Initialization
3845 (Specification : N_Aspect_Specification_Id);
3846 -- Transform N with the effects of the External_Initialization aspect
3847 -- specified by Specification. Note that Specification is removed from
3848 -- N's list of aspects.
3849
3850 procedure Check_Dynamic_Object (Typ : Entity_Id);
3851 -- A library-level object with nonstatic discriminant constraints may
3852 -- require dynamic allocation. The declaration is illegal if the
3853 -- profile includes the restriction No_Implicit_Heap_Allocations.
3854
3855 procedure Check_For_Null_Excluding_Components
3856 (Obj_Typ : Entity_Id;
3857 Obj_Decl : Node_Id);
3858 -- Verify that each null-excluding component of object declaration
3859 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3860 -- a compile-time warning if this is not the case.
3861
3862 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
3863 -- Check that the return subtype indication properly matches the result
3864 -- subtype of the function in an extended return object declaration, as
3865 -- required by RM 6.5(5.1/2-5.3/2).
3866
3867 function Count_Tasks (T : Entity_Id) return Uint;
3868 -- This function is called when a non-generic library level object of a
3869 -- task type is declared. Its function is to count the static number of
3870 -- tasks declared within the type (it is only called if Has_Task is set
3871 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3872 -- or a variant record type is encountered, Check_Restriction is called
3873 -- indicating the count is unknown.
3874
3875 -----------------------------------
3876 -- Apply_External_Initialization --
3877 -----------------------------------
3878
3879 procedure Apply_External_Initialization
3880 (Specification : N_Aspect_Specification_Id)
3881 is
3882 Def : constant Node_Id := Expression (Specification);
3883
3884 Expr : N_Subexpr_Id;
3885
3886 Data_Path : File_Name_Type;
3887 begin
3888 Remove (Specification);
3889
3890 Error_Msg_GNAT_Extension
3891 ("External_Initialization aspect", Sloc (Specification));
3892
3893 if Present (E) then
3894 Error_Msg_N
3895 ("initialization expression not allowed for object with aspect "
3896 & "External_Initialization", Specification);
3897 return;
3898 end if;
3899
3900 Set_Has_Init_Expression (N);
3901 Set_Expression (N, Error);
3902 E := Error;
3903
3904 case Is_OK_Static_Expression_Of_Type (Def, Standard_String) is
3905 when Static =>
3906 null;
3907
3908 when Not_Static =>
3909 Error_Msg_N
3910 ("External_Initialization aspect expects a static string",
3911 Specification);
3912 return;
3913
3914 when Invalid =>
3915 return;
3916 end case;
3917
3918 if not (Is_String_Type (T)
3919 or else Is_RTE (Base_Type (T), RE_Stream_Element_Array))
3920 then
3921 Error_Msg_N
3922 ("External_Initialization aspect can only be applied to objects "
3923 & "of string types or type Ada.Streams.Stream_Element_Array",
3924 Specification);
3925 return;
3926 end if;
3927
3928 declare
3929 S : constant String :=
3930 Stringt.To_String (Strval (Expr_Value_S (Def)));
3931 begin
3932 if System.OS_Lib.Is_Absolute_Path (S) then
3933 Data_Path := Name_Find (S);
3934 else
3935 declare
3936 Current_File_Name : constant File_Name_Type :=
3937 Unit_File_Name (Current_Sem_Unit);
3938
3939 Current_File_Path : constant File_Name_Type :=
3940 Fmap.Mapped_Path_Name (Current_File_Name);
3941
3942 Current_File_Directory : constant File_Name_Type :=
3943 Osint.Get_Directory (Current_File_Path);
3944
3945 Absolute_Dir : constant String :=
3946 System.OS_Lib.Normalize_Pathname
3947 (Get_Name_String (Current_File_Directory),
3948 Resolve_Links => False);
3949
3950 Data_Path_String : constant String :=
3951 Absolute_Dir
3952 & System.OS_Lib.Directory_Separator
3953 & Stringt.To_String (Strval (Def));
3954
3955 begin
3956 Data_Path := Name_Find (Data_Path_String);
3957 end;
3958 end if;
3959 end;
3960
3961 begin
3962 declare
3963 Source_File_I : constant Source_File_Index :=
3964 Sinput.L.Load_Source_File (Data_Path);
3965 begin
3966 if Source_File_I <= No_Source_File then
3967 Error_Msg_N ("cannot find input file", Specification);
3968 return;
3969 end if;
3970
3971 Lib.Writ.Add_Preprocessing_Dependency (Source_File_I);
3972
3973 Expr :=
3974 Make_External_Initializer
3975 (Sloc (Specification), Source_File_I);
3976 end;
3977 exception
3978 when Constraint_Error =>
3979 -- The most likely cause for a constraint error is a file
3980 -- whose size does not fit into Integer. We could modify
3981 -- Load_Source_File to report that error with a special
3982 -- exception???
3983 Error_Msg_N
3984 ("External_Initialization file exceeds maximum length",
3985 Specification);
3986 return;
3987 end;
3988
3989 Set_Expression (N, Expr);
3990 E := Expr;
3991 end Apply_External_Initialization;
3992
3993 --------------------------
3994 -- Check_Dynamic_Object --
3995 --------------------------
3996
3997 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3998 Comp : Entity_Id;
3999 Obj_Type : Entity_Id;
4000
4001 begin
4002 Obj_Type := Typ;
4003
4004 if Is_Private_Type (Obj_Type)
4005 and then Present (Full_View (Obj_Type))
4006 then
4007 Obj_Type := Full_View (Obj_Type);
4008 end if;
4009
4010 if Known_Static_Esize (Obj_Type) then
4011 return;
4012 end if;
4013
4014 if Restriction_Active (No_Implicit_Heap_Allocations)
4015 and then Expander_Active
4016 and then Has_Discriminants (Obj_Type)
4017 then
4018 Comp := First_Component (Obj_Type);
4019 while Present (Comp) loop
4020 if Known_Static_Esize (Etype (Comp))
4021 or else Size_Known_At_Compile_Time (Etype (Comp))
4022 then
4023 null;
4024
4025 elsif Is_Record_Type (Etype (Comp)) then
4026 Check_Dynamic_Object (Etype (Comp));
4027
4028 elsif not Discriminated_Size (Comp)
4029 and then Comes_From_Source (Comp)
4030 then
4031 Error_Msg_NE
4032 ("component& of non-static size will violate restriction "
4033 & "No_Implicit_Heap_Allocation?", N, Comp);
4034
4035 end if;
4036
4037 Next_Component (Comp);
4038 end loop;
4039 end if;
4040 end Check_Dynamic_Object;
4041
4042 -----------------------------------------
4043 -- Check_For_Null_Excluding_Components --
4044 -----------------------------------------
4045
4046 procedure Check_For_Null_Excluding_Components
4047 (Obj_Typ : Entity_Id;
4048 Obj_Decl : Node_Id)
4049 is
4050 procedure Check_Component
4051 (Comp_Typ : Entity_Id;
4052 Comp_Decl : Node_Id := Empty;
4053 Array_Comp : Boolean := False);
4054 -- Apply a compile-time null-exclusion check on a component denoted
4055 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
4056 -- subcomponents (if any).
4057
4058 ---------------------
4059 -- Check_Component --
4060 ---------------------
4061
4062 procedure Check_Component
4063 (Comp_Typ : Entity_Id;
4064 Comp_Decl : Node_Id := Empty;
4065 Array_Comp : Boolean := False)
4066 is
4067 Comp : Entity_Id;
4068 T : Entity_Id;
4069
4070 begin
4071 -- Do not consider internally-generated components or those that
4072 -- are already initialized.
4073
4074 if Present (Comp_Decl)
4075 and then (not Comes_From_Source (Comp_Decl)
4076 or else Present (Expression (Comp_Decl)))
4077 then
4078 return;
4079 end if;
4080
4081 if Is_Incomplete_Or_Private_Type (Comp_Typ)
4082 and then Present (Full_View (Comp_Typ))
4083 then
4084 T := Full_View (Comp_Typ);
4085 else
4086 T := Comp_Typ;
4087 end if;
4088
4089 -- Verify a component of a null-excluding access type
4090
4091 if Is_Access_Type (T)
4092 and then Can_Never_Be_Null (T)
4093 then
4094 if Comp_Decl = Obj_Decl then
4095 Null_Exclusion_Static_Checks
4096 (N => Obj_Decl,
4097 Comp => Empty,
4098 Array_Comp => Array_Comp);
4099
4100 else
4101 Null_Exclusion_Static_Checks
4102 (N => Obj_Decl,
4103 Comp => Comp_Decl,
4104 Array_Comp => Array_Comp);
4105 end if;
4106
4107 -- Check array components
4108
4109 elsif Is_Array_Type (T) then
4110
4111 -- There is no suitable component when the object is of an
4112 -- array type. However, a namable component may appear at some
4113 -- point during the recursive inspection, but not at the top
4114 -- level. At the top level just indicate array component case.
4115
4116 if Comp_Decl = Obj_Decl then
4117 Check_Component (Component_Type (T), Array_Comp => True);
4118 else
4119 Check_Component (Component_Type (T), Comp_Decl);
4120 end if;
4121
4122 -- Verify all components of type T
4123
4124 -- Note: No checks are performed on types with discriminants due
4125 -- to complexities involving variants. ???
4126
4127 elsif (Is_Concurrent_Type (T)
4128 or else Is_Incomplete_Or_Private_Type (T)
4129 or else Is_Record_Type (T))
4130 and then not Has_Discriminants (T)
4131 then
4132 Comp := First_Component (T);
4133 while Present (Comp) loop
4134 Check_Component (Etype (Comp), Parent (Comp));
4135
4136 Next_Component (Comp);
4137 end loop;
4138 end if;
4139 end Check_Component;
4140
4141 -- Start processing for Check_For_Null_Excluding_Components
4142
4143 begin
4144 Check_Component (Obj_Typ, Obj_Decl);
4145 end Check_For_Null_Excluding_Components;
4146
4147 -------------------------------------
4148 -- Check_Return_Subtype_Indication --
4149 -------------------------------------
4150
4151 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
4152 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
4153 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4154 Func_Id : constant Entity_Id := Return_Applies_To (Scope (Obj_Id));
4155 R_Typ : constant Entity_Id := Etype (Func_Id);
4156 Indic : constant Node_Id :=
4157 Object_Definition (Original_Node (Obj_Decl));
4158
4159 procedure Error_No_Match (N : Node_Id);
4160 -- Output error messages for case where types do not statically
4161 -- match. N is the location for the messages.
4162
4163 --------------------
4164 -- Error_No_Match --
4165 --------------------
4166
4167 procedure Error_No_Match (N : Node_Id) is
4168 begin
4169 Error_Msg_N
4170 ("subtype must statically match function result subtype", N);
4171
4172 if not Predicates_Match (Obj_Typ, R_Typ) then
4173 Error_Msg_Node_2 := R_Typ;
4174 Error_Msg_NE
4175 ("\predicate of& does not match predicate of&",
4176 N, Obj_Typ);
4177 end if;
4178 end Error_No_Match;
4179
4180 -- Start of processing for Check_Return_Subtype_Indication
4181
4182 begin
4183 -- First, avoid cascaded errors
4184
4185 if Error_Posted (Obj_Decl) or else Error_Posted (Indic) then
4186 return;
4187 end if;
4188
4189 -- "return access T" case; check that the return statement also has
4190 -- "access T", and that the subtypes statically match:
4191 -- if this is an access to subprogram the signatures must match.
4192
4193 if Is_Anonymous_Access_Type (R_Typ) then
4194 if Is_Anonymous_Access_Type (Obj_Typ) then
4195 if Ekind (Designated_Type (Obj_Typ)) /= E_Subprogram_Type
4196 then
4197 if Base_Type (Designated_Type (Obj_Typ)) /=
4198 Base_Type (Designated_Type (R_Typ))
4199 or else not Subtypes_Statically_Match (Obj_Typ, R_Typ)
4200 then
4201 Error_No_Match (Subtype_Mark (Indic));
4202 end if;
4203
4204 else
4205 -- For two anonymous access to subprogram types, the types
4206 -- themselves must be type conformant.
4207
4208 if not Conforming_Types
4209 (Obj_Typ, R_Typ, Fully_Conformant)
4210 then
4211 Error_No_Match (Indic);
4212 end if;
4213 end if;
4214
4215 else
4216 Error_Msg_N ("must use anonymous access type", Indic);
4217 end if;
4218
4219 -- If the return object is of an anonymous access type, then report
4220 -- an error if the function's result type is not also anonymous.
4221
4222 elsif Is_Anonymous_Access_Type (Obj_Typ) then
4223 pragma Assert (not Is_Anonymous_Access_Type (R_Typ));
4224 Error_Msg_N
4225 ("anonymous access not allowed for function with named access "
4226 & "result", Indic);
4227
4228 -- Subtype indication case: check that the return object's type is
4229 -- covered by the result type, and that the subtypes statically match
4230 -- when the result subtype is constrained. Also handle record types
4231 -- with unknown discriminants for which we have built the underlying
4232 -- record view. Coverage is needed to allow specific-type return
4233 -- objects when the result type is class-wide (see AI05-32).
4234
4235 elsif Covers (Base_Type (R_Typ), Base_Type (Obj_Typ))
4236 or else (Is_Underlying_Record_View (Base_Type (Obj_Typ))
4237 and then
4238 Covers
4239 (Base_Type (R_Typ),
4240 Underlying_Record_View (Base_Type (Obj_Typ))))
4241 then
4242 -- A null exclusion may be present on the return type, on the
4243 -- function specification, on the object declaration or on the
4244 -- subtype itself.
4245
4246 if Is_Access_Type (R_Typ)
4247 and then
4248 (Can_Never_Be_Null (R_Typ)
4249 or else Null_Exclusion_Present (Parent (Func_Id))) /=
4250 Can_Never_Be_Null (Obj_Typ)
4251 then
4252 Error_No_Match (Indic);
4253 end if;
4254
4255 -- AI05-103: for elementary types, subtypes must statically match
4256
4257 if Is_Constrained (R_Typ) or else Is_Access_Type (R_Typ) then
4258 if not Subtypes_Statically_Match (Obj_Typ, R_Typ) then
4259 Error_No_Match (Indic);
4260 end if;
4261
4262 -- If the result subtype of the function is defined by a
4263 -- subtype_mark, the return_subtype_indication shall be a
4264 -- subtype_indication. The subtype defined by the subtype_
4265 -- indication shall be statically compatible with the result
4266 -- subtype of the function (RM 6.5(5.3/5)).
4267
4268 -- We exclude the extended return statement of the predefined
4269 -- stream input to avoid reporting spurious errors, because its
4270 -- code is expanded on the basis of the base type (see subprogram
4271 -- Stream_Base_Type).
4272
4273 elsif Nkind (Indic) = N_Subtype_Indication
4274 and then not Subtypes_Statically_Compatible (Obj_Typ, R_Typ)
4275 and then not Is_TSS (Func_Id, TSS_Stream_Input)
4276 then
4277 Error_Msg_N
4278 ("result subtype must be statically compatible with the " &
4279 "function result type", Indic);
4280
4281 if not Predicates_Compatible (Obj_Typ, R_Typ) then
4282 Error_Msg_NE
4283 ("\predicate on result subtype is not compatible with &",
4284 Indic, R_Typ);
4285 end if;
4286 end if;
4287
4288 -- All remaining cases are illegal
4289
4290 -- Note: previous versions of this subprogram allowed the return
4291 -- value to be the ancestor of the return type if the return type
4292 -- was a null extension. This was plainly incorrect.
4293
4294 else
4295 Error_Msg_N
4296 ("wrong type for return_subtype_indication", Indic);
4297 end if;
4298 end Check_Return_Subtype_Indication;
4299
4300 -----------------
4301 -- Count_Tasks --
4302 -----------------
4303
4304 function Count_Tasks (T : Entity_Id) return Uint is
4305 C : Entity_Id;
4306 X : Node_Id;
4307 V : Uint;
4308
4309 begin
4310 if Is_Task_Type (T) then
4311 return Uint_1;
4312
4313 elsif Is_Record_Type (T) then
4314 if Has_Discriminants (T) then
4315 Check_Restriction (Max_Tasks, N);
4316 return Uint_0;
4317
4318 else
4319 V := Uint_0;
4320 C := First_Component (T);
4321 while Present (C) loop
4322 V := V + Count_Tasks (Etype (C));
4323 Next_Component (C);
4324 end loop;
4325
4326 return V;
4327 end if;
4328
4329 elsif Is_Array_Type (T) then
4330 X := First_Index (T);
4331 V := Count_Tasks (Component_Type (T));
4332 while Present (X) loop
4333 C := Etype (X);
4334
4335 if not Is_OK_Static_Subtype (C) then
4336 Check_Restriction (Max_Tasks, N);
4337 return Uint_0;
4338 else
4339 V := V * (UI_Max (Uint_0,
4340 Expr_Value (Type_High_Bound (C)) -
4341 Expr_Value (Type_Low_Bound (C)) + Uint_1));
4342 end if;
4343
4344 Next_Index (X);
4345 end loop;
4346
4347 return V;
4348
4349 else
4350 return Uint_0;
4351 end if;
4352 end Count_Tasks;
4353
4354 -- Local variables
4355
4356 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4357 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4358 -- Save the Ghost-related attributes to restore on exit
4359
4360 Prev_Entity : Entity_Id := Empty;
4361 Related_Id : Entity_Id;
4362
4363 -- Start of processing for Analyze_Object_Declaration
4364
4365 begin
4366 if Constant_Present (N) then
4367 Mutate_Ekind (Id, E_Constant);
4368 Set_Is_True_Constant (Id);
4369 else
4370 Mutate_Ekind (Id, E_Variable);
4371 end if;
4372
4373 -- Indicate this is not set in source. Certainly true for constants, and
4374 -- true for variables so far (will be reset for a variable if and when
4375 -- we encounter a modification in the source).
4376
4377 Set_Never_Set_In_Source (Id);
4378
4379 -- There are three kinds of implicit types generated by an
4380 -- object declaration:
4381
4382 -- 1. Those generated by the original Object Definition
4383
4384 -- 2. Those generated by the Expression
4385
4386 -- 3. Those used to constrain the Object Definition with the
4387 -- expression constraints when the definition is unconstrained.
4388
4389 -- They must be generated in this order to avoid order of elaboration
4390 -- issues. Thus the first step (after entering the name) is to analyze
4391 -- the object definition.
4392
4393 if Constant_Present (N) then
4394 Prev_Entity := Current_Entity_In_Scope (Id);
4395
4396 if Present (Prev_Entity)
4397 and then
4398 -- If the homograph is an implicit subprogram, it is overridden
4399 -- by the current declaration.
4400
4401 ((Is_Overloadable (Prev_Entity)
4402 and then Is_Inherited_Operation (Prev_Entity))
4403
4404 -- The current object is a discriminal generated for an entry
4405 -- family index. Even though the index is a constant, in this
4406 -- particular context there is no true constant redeclaration.
4407 -- Enter_Name will handle the visibility.
4408
4409 or else
4410 (Is_Discriminal (Id)
4411 and then Ekind (Discriminal_Link (Id)) =
4412 E_Entry_Index_Parameter)
4413
4414 -- The current object is the renaming for a generic declared
4415 -- within the instance.
4416
4417 or else
4418 (Ekind (Prev_Entity) = E_Package
4419 and then Nkind (Parent (Prev_Entity)) =
4420 N_Package_Renaming_Declaration
4421 and then not Comes_From_Source (Prev_Entity)
4422 and then
4423 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4424
4425 -- The entity may be a homonym of a private component of the
4426 -- enclosing protected object, for which we create a local
4427 -- renaming declaration. The declaration is legal, even if
4428 -- useless when it just captures that component.
4429
4430 or else
4431 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4432 and then Nkind (Parent (Prev_Entity)) =
4433 N_Object_Renaming_Declaration))
4434 then
4435 Prev_Entity := Empty;
4436 end if;
4437 end if;
4438
4439 if Present (Prev_Entity) then
4440
4441 -- The object declaration is Ghost when it completes a deferred Ghost
4442 -- constant.
4443
4444 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4445
4446 Constant_Redeclaration (Id, N, T);
4447
4448 Generate_Reference (Prev_Entity, Id, 'c');
4449 Set_Completion_Referenced (Id);
4450
4451 if Error_Posted (N) then
4452
4453 -- Type mismatch or illegal redeclaration; do not analyze
4454 -- expression to avoid cascaded errors.
4455
4456 T := Find_Type_Of_Object (Object_Definition (N), N);
4457 Set_Etype (Id, T);
4458 goto Leave;
4459 end if;
4460
4461 -- In the normal case, enter identifier at the start to catch premature
4462 -- usage in the initialization expression.
4463
4464 else
4465 Generate_Definition (Id);
4466 Enter_Name (Id);
4467
4468 Mark_Coextensions (N, Object_Definition (N));
4469
4470 T := Find_Type_Of_Object (Object_Definition (N), N);
4471
4472 if Nkind (Object_Definition (N)) = N_Access_Definition
4473 and then Present
4474 (Access_To_Subprogram_Definition (Object_Definition (N)))
4475 and then Protected_Present
4476 (Access_To_Subprogram_Definition (Object_Definition (N)))
4477 then
4478 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4479 end if;
4480
4481 if Error_Posted (Id) then
4482 Set_Etype (Id, T);
4483 goto Leave;
4484 end if;
4485 end if;
4486
4487 declare
4488 S : constant Opt_N_Aspect_Specification_Id :=
4489 Find_Aspect (Id, Aspect_External_Initialization);
4490 begin
4491 if Present (S) then
4492 Apply_External_Initialization (S);
4493 end if;
4494 end;
4495
4496 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4497 -- out some static checks.
4498
4499 if Ada_Version >= Ada_2005 then
4500
4501 -- In case of aggregates we must also take care of the correct
4502 -- initialization of nested aggregates bug this is done at the
4503 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4504
4505 if Can_Never_Be_Null (T) then
4506 if Present (Expression (N))
4507 and then Nkind (Expression (N)) = N_Aggregate
4508 then
4509 null;
4510
4511 elsif Comes_From_Source (Id) then
4512 declare
4513 Save_Typ : constant Entity_Id := Etype (Id);
4514 begin
4515 Set_Etype (Id, T); -- Temp. decoration for static checks
4516 Null_Exclusion_Static_Checks (N);
4517 Set_Etype (Id, Save_Typ);
4518 end;
4519 end if;
4520
4521 -- We might be dealing with an object of a composite type containing
4522 -- null-excluding components without an aggregate, so we must verify
4523 -- that such components have default initialization.
4524
4525 else
4526 Check_For_Null_Excluding_Components (T, N);
4527 end if;
4528 end if;
4529
4530 -- Object is marked pure if it is in a pure scope
4531
4532 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4533
4534 -- If deferred constant, make sure context is appropriate. We detect
4535 -- a deferred constant as a constant declaration with no expression.
4536 -- A deferred constant can appear in a package body if its completion
4537 -- is by means of an interface pragma.
4538
4539 if Constant_Present (N) and then No (E) then
4540
4541 -- A deferred constant may appear in the declarative part of the
4542 -- following constructs:
4543
4544 -- blocks
4545 -- entry bodies
4546 -- extended return statements
4547 -- package specs
4548 -- package bodies
4549 -- subprogram bodies
4550 -- task bodies
4551
4552 -- When declared inside a package spec, a deferred constant must be
4553 -- completed by a full constant declaration or pragma Import. In all
4554 -- other cases, the only proper completion is pragma Import. Extended
4555 -- return statements are flagged as invalid contexts because they do
4556 -- not have a declarative part and so cannot accommodate the pragma.
4557
4558 if Ekind (Current_Scope) = E_Return_Statement then
4559 Error_Msg_N
4560 ("invalid context for deferred constant declaration (RM 7.4)",
4561 N);
4562 Error_Msg_N
4563 ("\declaration requires an initialization expression",
4564 N);
4565 Set_Constant_Present (N, False);
4566
4567 -- In Ada 83, deferred constant must be of private type
4568
4569 elsif not Is_Private_Type (T) then
4570 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4571 Error_Msg_N
4572 ("(Ada 83) deferred constant must be private type", N);
4573 end if;
4574 end if;
4575
4576 -- If not a deferred constant, then the object declaration freezes
4577 -- its type, unless the object is of an anonymous type and has delayed
4578 -- aspects (in that case the type is frozen when the object itself is)
4579 -- or the context is a spec expression.
4580
4581 else
4582 Check_Fully_Declared (T, N);
4583
4584 if Has_Delayed_Aspects (Id)
4585 and then Is_Array_Type (T)
4586 and then Is_Itype (T)
4587 then
4588 Set_Has_Delayed_Freeze (T);
4589
4590 elsif not Preanalysis_Active then
4591 Freeze_Before (N, T);
4592 end if;
4593 end if;
4594
4595 -- If the object was created by a constrained array definition, then
4596 -- set the link in both the anonymous base type and anonymous subtype
4597 -- that are built to represent the array type to point to the object.
4598
4599 if Nkind (Object_Definition (Declaration_Node (Id))) =
4600 N_Constrained_Array_Definition
4601 then
4602 Set_Related_Array_Object (T, Id);
4603 Set_Related_Array_Object (Base_Type (T), Id);
4604 end if;
4605
4606 -- Check for protected objects not at library level
4607
4608 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4609 Check_Restriction (No_Local_Protected_Objects, Id);
4610 end if;
4611
4612 -- Check for violation of No_Local_Timing_Events
4613
4614 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4615 Check_Restriction (No_Local_Timing_Events, Id);
4616 end if;
4617
4618 -- The actual subtype of the object is the nominal subtype, unless
4619 -- the nominal one is unconstrained and obtained from the expression.
4620
4621 Act_T := T;
4622
4623 if Is_Library_Level_Entity (Id) then
4624 Check_Dynamic_Object (T);
4625 end if;
4626
4627 -- Process initialization expression if present and not in error
4628
4629 if Present (E) and then E /= Error then
4630
4631 -- Generate an error in case of CPP class-wide object initialization.
4632 -- Required because otherwise the expansion of the class-wide
4633 -- assignment would try to use 'size to initialize the object
4634 -- (primitive that is not available in CPP tagged types).
4635
4636 if Is_Class_Wide_Type (Act_T)
4637 and then
4638 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4639 or else
4640 (Present (Full_View (Root_Type (Etype (Act_T))))
4641 and then
4642 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4643 then
4644 Error_Msg_N
4645 ("predefined assignment not available for 'C'P'P tagged types",
4646 E);
4647 end if;
4648
4649 Mark_Coextensions (N, E);
4650 Analyze (E);
4651
4652 -- In case of errors detected in the analysis of the expression,
4653 -- decorate it with the expected type to avoid cascaded errors.
4654
4655 if No (Etype (E)) then
4656 Set_Etype (E, T);
4657 end if;
4658
4659 -- If we are analyzing a constant declaration, set its completion
4660 -- flag after analyzing and resolving the expression.
4661
4662 if Constant_Present (N) then
4663 Set_Has_Completion (Id);
4664
4665 -- Set Has_Initial_Value if initialization expression present. Note
4666 -- that if there is no initializing expression, we leave the state
4667 -- of this flag unchanged (usually it will be False, but notably in
4668 -- the case of exception choice variables, it will already be true).
4669
4670 -- Set Is_True_Constant if initialization expression is present. It
4671 -- will be reset if the variable is indeed modified.
4672
4673 else
4674 Set_Has_Initial_Value (Id);
4675 Set_Is_True_Constant (Id);
4676 end if;
4677
4678 -- Set type and resolve (type may be overridden later on). Note:
4679 -- Ekind (Id) must still be E_Void at this point so that incorrect
4680 -- early usage within E is properly diagnosed.
4681
4682 Set_Etype (Id, T);
4683
4684 -- If the expression is a formal that is a "subprogram pointer"
4685 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4686 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4687 -- the corresponding check, as is done for assignments.
4688
4689 if Is_Entity_Name (E)
4690 and then Present (Entity (E))
4691 and then Is_Formal (Entity (E))
4692 and then
4693 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4694 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4695 then
4696 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4697 end if;
4698
4699 Resolve (E, T);
4700
4701 -- No further action needed if E is a call to an inlined function
4702 -- which returns an unconstrained type and it has been expanded into
4703 -- a procedure call. In that case N has been replaced by an object
4704 -- declaration without initializing expression and it has been
4705 -- analyzed (see Expand_Inlined_Call).
4706
4707 if Back_End_Inlining
4708 and then Expander_Active
4709 and then Nkind (E) = N_Function_Call
4710 and then Is_Entity_Name (Name (E))
4711 and then Is_Inlined (Entity (Name (E)))
4712 and then not Is_Constrained (Etype (E))
4713 and then No (Expression (N))
4714 and then Analyzed (N)
4715 then
4716 goto Leave;
4717 end if;
4718
4719 -- No further action needed if E is a conditional expression and N
4720 -- has been replaced by a renaming declaration during its expansion
4721 -- (see Expand_N_Case_Expression and Expand_N_If_Expression).
4722
4723 if Expander_Active
4724 and then Nkind (E) in N_Case_Expression | N_If_Expression
4725 and then Nkind (N) = N_Object_Renaming_Declaration
4726 then
4727 goto Leave;
4728 end if;
4729
4730 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4731 -- node (which was marked already-analyzed), we need to set the type
4732 -- to something else than Universal_Access to keep gigi happy.
4733
4734 if Etype (E) = Universal_Access then
4735 Set_Etype (E, T);
4736 end if;
4737
4738 -- If the object is an access to variable, the initialization
4739 -- expression cannot be an access to constant.
4740
4741 if Is_Access_Type (T)
4742 and then not Is_Access_Constant (T)
4743 and then Is_Access_Type (Etype (E))
4744 and then Is_Access_Constant (Etype (E))
4745 then
4746 Error_Msg_N
4747 ("access to variable cannot be initialized with an "
4748 & "access-to-constant expression", E);
4749 end if;
4750
4751 if not Assignment_OK (N) then
4752 Check_Initialization (T, E);
4753 end if;
4754
4755 Check_Unset_Reference (E);
4756
4757 -- If this is a variable, then set current value. If this is a
4758 -- declared constant of a scalar type with a static expression,
4759 -- indicate that it is always valid.
4760
4761 if not Constant_Present (N) then
4762 if Compile_Time_Known_Value (E) then
4763 Set_Current_Value (Id, E);
4764 end if;
4765
4766 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4767 Set_Is_Known_Valid (Id);
4768
4769 -- If it is a constant initialized with a valid nonstatic entity,
4770 -- the constant is known valid as well, and can inherit the subtype
4771 -- of the entity if it is a subtype of the given type. This info
4772 -- is preserved on the actual subtype of the constant.
4773
4774 elsif Is_Scalar_Type (T)
4775 and then Is_Entity_Name (E)
4776 and then Is_Known_Valid (Entity (E))
4777 and then In_Subrange_Of (Etype (Entity (E)), T)
4778 then
4779 Set_Is_Known_Valid (Id);
4780 Set_Actual_Subtype (Id, Etype (Entity (E)));
4781 end if;
4782
4783 -- Deal with setting of null flags
4784
4785 if Is_Access_Type (T) then
4786 if Known_Non_Null (E) then
4787 Set_Is_Known_Non_Null (Id, True);
4788 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4789 Set_Is_Known_Null (Id, True);
4790 end if;
4791 end if;
4792
4793 -- Check incorrect use of dynamically tagged expressions
4794
4795 if Is_Tagged_Type (T) then
4796 Check_Dynamically_Tagged_Expression
4797 (Expr => E,
4798 Typ => T,
4799 Related_Nod => N);
4800 end if;
4801
4802 Apply_Scalar_Range_Check (E, T);
4803 Apply_Static_Length_Check (E, T);
4804
4805 -- A formal parameter of a specific tagged type whose related
4806 -- subprogram is subject to pragma Extensions_Visible with value
4807 -- "False" cannot be implicitly converted to a class-wide type by
4808 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4809 -- not consider internally generated expressions.
4810
4811 if Is_Class_Wide_Type (T)
4812 and then Comes_From_Source (E)
4813 and then Is_EVF_Expression (E)
4814 then
4815 Error_Msg_N
4816 ("formal parameter cannot be implicitly converted to "
4817 & "class-wide type when Extensions_Visible is False", E);
4818 end if;
4819 end if;
4820
4821 -- If the No_Streams restriction is set, check that the type of the
4822 -- object is not, and does not contain, any subtype derived from
4823 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4824 -- Has_Stream just for efficiency reasons. There is no point in
4825 -- spending time on a Has_Stream check if the restriction is not set.
4826
4827 if Restriction_Check_Required (No_Streams) then
4828 if Has_Stream (T) then
4829 Check_Restriction (No_Streams, N);
4830 end if;
4831 end if;
4832
4833 -- Deal with predicate check before we start to do major rewriting. It
4834 -- is OK to initialize and then check the initialized value, since the
4835 -- object goes out of scope if we get a predicate failure. Note that we
4836 -- do this in the analyzer and not the expander because the analyzer
4837 -- does some substantial rewriting in some cases.
4838
4839 -- We need a predicate check if the type has predicates that are not
4840 -- ignored, and if either there is an initializing expression, or for
4841 -- default initialization when we have at least one case of an explicit
4842 -- default initial value (including via a Default_Value or
4843 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4844 -- an internal declaration whose initialization comes later (as for an
4845 -- aggregate expansion) or a deferred constant.
4846 -- If expression is an aggregate it may be expanded into assignments
4847 -- and the declaration itself is marked with No_Initialization, but
4848 -- the predicate still applies.
4849
4850 if not Suppress_Assignment_Checks (N)
4851 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4852 and then
4853 (not No_Initialization (N)
4854 or else (Present (E) and then Nkind (E) = N_Aggregate))
4855 and then
4856 (Present (E)
4857 or else
4858 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4859 and then not (Constant_Present (N) and then No (E))
4860 then
4861 -- If the type has a static predicate and the expression is known at
4862 -- compile time, see if the expression satisfies the predicate.
4863 -- In the case of a static expression, this must be done even if
4864 -- the predicate is not enabled (as per static expression rules).
4865
4866 if Present (E) then
4867 Check_Expression_Against_Static_Predicate (E, T);
4868 end if;
4869
4870 -- Do not perform further predicate-related checks unless
4871 -- predicates are enabled for the subtype.
4872
4873 if not Predicate_Enabled (T) then
4874 null;
4875
4876 -- If the type is a null record and there is no explicit initial
4877 -- expression, no predicate check applies.
4878
4879 elsif No (E) and then Is_Null_Record_Type (T) then
4880 null;
4881
4882 -- If there is an address clause for this object, do not generate a
4883 -- predicate check here. It will be generated later, at the freezng
4884 -- point. It would be wrong to generate references to the object
4885 -- here, before the address has been determined.
4886
4887 elsif Has_Aspect (Id, Aspect_Address)
4888 or else Present (Following_Address_Clause (N))
4889 then
4890 null;
4891
4892 -- Do not generate a predicate check if the initialization expression
4893 -- is a type conversion whose target subtype statically matches the
4894 -- object's subtype because the conversion has been subjected to the
4895 -- same check. This is a small optimization which avoids redundant
4896 -- checks.
4897
4898 elsif Present (E)
4899 and then Nkind (E) in N_Type_Conversion
4900 and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
4901 then
4902 null;
4903
4904 else
4905 -- The check must be inserted after the expanded aggregate
4906 -- expansion code, if any.
4907
4908 declare
4909 Check : constant Node_Id :=
4910 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4911 begin
4912 if No (Next_Decl) then
4913 Append_To (List_Containing (N), Check);
4914 else
4915 Insert_Before (Next_Decl, Check);
4916 end if;
4917 end;
4918 end if;
4919 end if;
4920
4921 -- Case of unconstrained type
4922
4923 if not Is_Definite_Subtype (T) then
4924
4925 -- Nothing to do in deferred constant case
4926
4927 if Constant_Present (N) and then No (E) then
4928 null;
4929
4930 -- Case of no initialization present
4931
4932 elsif No (E) then
4933 if No_Initialization (N) then
4934 null;
4935
4936 elsif Is_Class_Wide_Type (T) then
4937
4938 -- Case of a mutably tagged type
4939
4940 if Is_Mutably_Tagged_Type (T) then
4941 Act_T := Class_Wide_Equivalent_Type (T);
4942
4943 Rewrite (Object_Definition (N),
4944 New_Occurrence_Of (Act_T, Loc));
4945
4946 Insert_After (N,
4947 Make_Procedure_Call_Statement (Loc,
4948 Name => New_Occurrence_Of (Init_Proc (Etype (T)), Loc),
4949 Parameter_Associations => New_List (
4950 Unchecked_Convert_To
4951 (Etype (T), New_Occurrence_Of (Id, Loc)))));
4952
4953 Freeze_Before (N, Act_T);
4954
4955 -- Otherwise an initial expression is required
4956
4957 else
4958 Error_Msg_N
4959 ("initialization required in class-wide declaration", N);
4960 end if;
4961
4962 else
4963 Error_Msg_N
4964 ("unconstrained subtype not allowed (need initialization)",
4965 Object_Definition (N));
4966
4967 if Is_Record_Type (T) and then Has_Discriminants (T) then
4968 Error_Msg_N
4969 ("\provide initial value or explicit discriminant values",
4970 Object_Definition (N));
4971
4972 Error_Msg_NE
4973 ("\or give default discriminant values for type&",
4974 Object_Definition (N), T);
4975
4976 elsif Is_Array_Type (T) then
4977 Error_Msg_N
4978 ("\provide initial value or explicit array bounds",
4979 Object_Definition (N));
4980 end if;
4981 end if;
4982
4983 -- Case of initialization present but in error
4984
4985 elsif E = Error then
4986 null;
4987
4988 -- Case of initialization present
4989
4990 else
4991 -- Unconstrained variables not allowed in Ada 83
4992
4993 if Ada_Version = Ada_83
4994 and then not Constant_Present (N)
4995 and then Comes_From_Source (Object_Definition (N))
4996 then
4997 Error_Msg_N
4998 ("(Ada 83) unconstrained variable not allowed",
4999 Object_Definition (N));
5000 end if;
5001
5002 -- Now we constrain the variable from the initializing expression
5003
5004 -- If the expression is an aggregate, it has been expanded into
5005 -- individual assignments. Retrieve the actual type from the
5006 -- expanded construct.
5007
5008 if Is_Array_Type (T)
5009 and then No_Initialization (N)
5010 and then Nkind (Unqualify (Original_Node (E))) = N_Aggregate
5011 then
5012 Act_T := Etype (E);
5013
5014 -- In case of class-wide interface object declarations we delay
5015 -- the generation of the equivalent record type declarations until
5016 -- its expansion because there are cases in they are not required.
5017
5018 elsif Is_Interface (T) then
5019 null;
5020
5021 -- If the type is an unchecked union, no subtype can be built from
5022 -- the expression. Rewrite declaration as a renaming, which the
5023 -- back-end can handle properly. This is a rather unusual case,
5024 -- because most unchecked_union declarations have default values
5025 -- for discriminants and are thus not indefinite.
5026
5027 elsif Is_Unchecked_Union (T) then
5028 -- If the expression is an aggregate it contains the required
5029 -- discriminant values but it has not been resolved yet, so do
5030 -- it now, and treat it as the initial expression of an object
5031 -- declaration, rather than a renaming.
5032
5033 if Nkind (E) = N_Aggregate then
5034 Analyze_And_Resolve (E, T);
5035
5036 else
5037 Rewrite (N,
5038 Make_Object_Renaming_Declaration (Loc,
5039 Defining_Identifier => Id,
5040 Subtype_Mark => New_Occurrence_Of (T, Loc),
5041 Name => E));
5042
5043 Set_Renamed_Object (Id, E);
5044 Freeze_Before (N, T);
5045 Set_Is_Frozen (Id);
5046 goto Leave;
5047 end if;
5048
5049 -- Rewrite mutably tagged class-wide type declarations to be that
5050 -- of the corresponding class-wide equivalent type.
5051
5052 elsif Is_Mutably_Tagged_Type (T) then
5053 Act_T := Class_Wide_Equivalent_Type (T);
5054
5055 Rewrite (Object_Definition (N),
5056 New_Occurrence_Of (Act_T, Loc));
5057
5058 Freeze_Before (N, Act_T);
5059
5060 else
5061 -- Ensure that the generated subtype has a unique external name
5062 -- when the related object is public. This guarantees that the
5063 -- subtype and its bounds will not be affected by switches or
5064 -- pragmas that may offset the internal counter due to extra
5065 -- generated code.
5066
5067 if Is_Public (Id) then
5068 Related_Id := Id;
5069 else
5070 Related_Id := Empty;
5071 end if;
5072
5073 -- If the object has an unconstrained array subtype with fixed
5074 -- lower bound, then sliding to that bound may be needed.
5075
5076 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
5077 Expand_Sliding_Conversion (E, T);
5078 end if;
5079
5080 if In_Spec_Expression and then In_Declare_Expr > 0 then
5081 -- It is too early to be doing expansion-ish things,
5082 -- so exit early. But we have to set Ekind (Id) now so
5083 -- that subsequent uses of this entity are not rejected
5084 -- via the same mechanism that (correctly) rejects
5085 -- "X : Integer := X;".
5086 goto Leave;
5087 end if;
5088
5089 Expand_Subtype_From_Expr
5090 (N => N,
5091 Unc_Type => T,
5092 Subtype_Indic => Object_Definition (N),
5093 Exp => E,
5094 Related_Id => Related_Id);
5095
5096 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
5097 end if;
5098
5099 if Act_T /= T then
5100 declare
5101 Full_Act_T : constant Entity_Id :=
5102 (if Is_Private_Type (Act_T)
5103 then Full_View (Act_T)
5104 else Empty);
5105 -- Propagate attributes to full view when needed
5106
5107 begin
5108 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
5109
5110 if Present (Full_Act_T) then
5111 Set_Is_Constr_Subt_For_U_Nominal (Full_Act_T);
5112 end if;
5113
5114 -- If the object is aliased, then it may be pointed to by an
5115 -- access-to-unconstrained-array value, which means that it
5116 -- must be allocated with its bounds.
5117
5118 if Aliased_Present (N)
5119 and then (Is_Array_Type (Act_T)
5120 or else (Present (Full_Act_T)
5121 and then Is_Array_Type (Full_Act_T)))
5122 then
5123 Set_Is_Constr_Array_Subt_With_Bounds (Act_T);
5124
5125 if Present (Full_Act_T) then
5126 Set_Is_Constr_Array_Subt_With_Bounds (Full_Act_T);
5127 end if;
5128 end if;
5129
5130 Freeze_Before (N, Act_T);
5131 end;
5132 end if;
5133
5134 Freeze_Before (N, T);
5135 end if;
5136
5137 elsif Is_Array_Type (T)
5138 and then No_Initialization (N)
5139 and then Nkind (Unqualify (Original_Node (E))) = N_Aggregate
5140 then
5141 if not Is_Entity_Name (Object_Definition (N)) then
5142 Act_T := Etype (E);
5143 Check_Compile_Time_Size (Act_T);
5144 end if;
5145
5146 -- When the given object definition and the aggregate are specified
5147 -- independently, and their lengths might differ do a length check.
5148 -- This cannot happen if the aggregate is of the form (others =>...)
5149
5150 if Nkind (E) = N_Raise_Constraint_Error then
5151
5152 -- Aggregate is statically illegal. Place back in declaration
5153
5154 Set_Expression (N, E);
5155 Set_No_Initialization (N, False);
5156
5157 elsif T = Etype (E) then
5158 null;
5159
5160 elsif Nkind (E) = N_Aggregate
5161 and then Present (Component_Associations (E))
5162 and then Present (Choice_List (First (Component_Associations (E))))
5163 and then
5164 Nkind (First (Choice_List (First (Component_Associations (E))))) =
5165 N_Others_Choice
5166 then
5167 null;
5168
5169 else
5170 Apply_Length_Check (E, T);
5171 end if;
5172
5173 -- When possible, and not a deferred constant, build the default subtype
5174
5175 elsif Build_Default_Subtype_OK (T)
5176 and then (not Constant_Present (N) or else Present (E))
5177 then
5178 if No (E) then
5179 Act_T := Build_Default_Subtype (T, N);
5180 else
5181 -- Ada 2005: A limited object may be initialized by means of an
5182 -- aggregate. If the type has default discriminants it has an
5183 -- unconstrained nominal type, Its actual subtype will be obtained
5184 -- from the aggregate, and not from the default discriminants.
5185
5186 Act_T := Etype (E);
5187 end if;
5188
5189 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
5190 Freeze_Before (N, Act_T);
5191
5192 -- If this is a constant declaration of an unconstrained type and
5193 -- the initialization is an aggregate, we can use the subtype of the
5194 -- aggregate for the declared entity because it is immutable.
5195
5196 elsif not Is_Constrained (T)
5197 and then Has_Discriminants (T)
5198 and then Constant_Present (N)
5199 and then not Has_Unchecked_Union (T)
5200 and then Nkind (E) = N_Aggregate
5201 then
5202 Act_T := Etype (E);
5203 end if;
5204
5205 -- Check No_Wide_Characters restriction
5206
5207 Check_Wide_Character_Restriction (T, Object_Definition (N));
5208
5209 -- Now establish the proper kind and type of the object
5210
5211 if Ekind (Id) = E_Void then
5212 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5213 end if;
5214
5215 -- A variable is set as shared passive if it appears in a shared
5216 -- passive package, and is at the outer level. This is not done for
5217 -- entities generated during expansion, because those are always
5218 -- manipulated locally.
5219
5220 if not Constant_Present (N)
5221 and then Is_Shared_Passive (Current_Scope)
5222 and then Is_Library_Level_Entity (Id)
5223 and then Comes_From_Source (Id)
5224 then
5225 Set_Is_Shared_Passive (Id);
5226 Check_Shared_Var (Id, T, N);
5227 end if;
5228
5229 -- Set the SPARK mode from the current context (may be overwritten later
5230 -- with explicit pragma).
5231
5232 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5233 Set_SPARK_Pragma_Inherited (Id);
5234
5235 -- Preserve relevant elaboration-related attributes of the context which
5236 -- are no longer available or very expensive to recompute once analysis,
5237 -- resolution, and expansion are over.
5238
5239 Mark_Elaboration_Attributes
5240 (N_Id => Id,
5241 Checks => True,
5242 Warnings => True);
5243
5244 -- Initialize alignment and size and capture alignment setting
5245
5246 Reinit_Alignment (Id);
5247 Reinit_Esize (Id);
5248 Set_Optimize_Alignment_Flags (Id);
5249
5250 -- Deal with aliased case
5251
5252 if Aliased_Present (N) then
5253 Set_Is_Aliased (Id);
5254
5255 -- AI12-001: All aliased objects are considered to be specified as
5256 -- independently addressable (RM C.6(8.1/4)).
5257
5258 Set_Is_Independent (Id);
5259
5260 -- If the object is aliased and the type is unconstrained with
5261 -- defaulted discriminants and there is no expression, then the
5262 -- object is constrained by the defaults, so it is worthwhile
5263 -- building the corresponding subtype.
5264
5265 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5266 -- unconstrained, then only establish an actual subtype if the
5267 -- nominal subtype is indefinite. In definite cases the object is
5268 -- unconstrained in Ada 2005.
5269
5270 if No (E)
5271 and then Is_Record_Type (T)
5272 and then not Is_Constrained (T)
5273 and then Has_Discriminants (T)
5274 and then (Ada_Version < Ada_2005
5275 or else not Is_Definite_Subtype (T))
5276 then
5277 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5278 end if;
5279 end if;
5280
5281 -- Now we can set the type of the object
5282
5283 Set_Etype (Id, Act_T);
5284
5285 -- Non-constant object is marked to be treated as volatile if type is
5286 -- volatile and we clear the Current_Value setting that may have been
5287 -- set above. Doing so for constants isn't required and might interfere
5288 -- with possible uses of the object as a static expression in contexts
5289 -- incompatible with volatility (e.g. as a case-statement alternative).
5290
5291 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5292 Set_Treat_As_Volatile (Id);
5293 Set_Current_Value (Id, Empty);
5294 end if;
5295
5296 -- Deal with controlled types
5297
5298 if Has_Controlled_Component (Etype (Id))
5299 or else Is_Controlled (Etype (Id))
5300 then
5301 if not Is_Library_Level_Entity (Id) then
5302 Check_Restriction (No_Nested_Finalization, N);
5303 else
5304 Validate_Controlled_Object (Id);
5305 end if;
5306 end if;
5307
5308 -- If the type of a constrained array has an unconstrained first
5309 -- subtype, its Finalize_Address primitive expects the address of
5310 -- an object with a dope vector (see Make_Finalize_Address_Stmts).
5311
5312 if Is_Constr_Array_Subt_Of_Unc_With_Controlled (Etype (Id)) then
5313 Set_Is_Constr_Array_Subt_With_Bounds (Etype (Id));
5314 end if;
5315
5316 if Has_Task (Etype (Id)) then
5317 Check_Restriction (No_Tasking, N);
5318
5319 -- Deal with counting max tasks
5320
5321 -- Nothing to do if inside a generic
5322
5323 if Inside_A_Generic then
5324 null;
5325
5326 -- If library level entity, then count tasks
5327
5328 elsif Is_Library_Level_Entity (Id) then
5329 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5330
5331 -- If not library level entity, then indicate we don't know max
5332 -- tasks and also check task hierarchy restriction and blocking
5333 -- operation (since starting a task is definitely blocking).
5334
5335 else
5336 Check_Restriction (Max_Tasks, N);
5337 Check_Restriction (No_Task_Hierarchy, N);
5338 Check_Potentially_Blocking_Operation (N);
5339 end if;
5340
5341 -- A rather specialized test. If we see two tasks being declared
5342 -- of the same type in the same object declaration, and the task
5343 -- has an entry with an address clause, we know that program error
5344 -- will be raised at run time since we can't have two tasks with
5345 -- entries at the same address.
5346
5347 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5348 declare
5349 E : Entity_Id;
5350
5351 begin
5352 E := First_Entity (Etype (Id));
5353 while Present (E) loop
5354 if Ekind (E) = E_Entry
5355 and then Present (Address_Clause (E))
5356 then
5357 Error_Msg_Warn := SPARK_Mode /= On;
5358 Error_Msg_N
5359 ("more than one task with same entry address<<", N);
5360 Error_Msg_N ("\Program_Error [<<", N);
5361 Insert_Action (N,
5362 Make_Raise_Program_Error (Loc,
5363 Reason => PE_Duplicated_Entry_Address));
5364 exit;
5365 end if;
5366
5367 Next_Entity (E);
5368 end loop;
5369 end;
5370 end if;
5371 end if;
5372
5373 -- Check specific legality rules for a return object
5374
5375 if Is_Return_Object (Id) then
5376 Check_Return_Subtype_Indication (N);
5377 end if;
5378
5379 -- Some simple constant-propagation: if the expression is a constant
5380 -- string initialized with a literal, share the literal. This avoids
5381 -- a run-time copy.
5382
5383 if Present (E)
5384 and then Is_Entity_Name (E)
5385 and then Ekind (Entity (E)) = E_Constant
5386 and then Base_Type (Etype (E)) = Standard_String
5387 then
5388 declare
5389 Val : constant Node_Id := Constant_Value (Entity (E));
5390 begin
5391 if Present (Val) and then Nkind (Val) = N_String_Literal then
5392 Rewrite (E, New_Copy (Val));
5393 end if;
5394 end;
5395 end if;
5396
5397 if Present (Prev_Entity)
5398 and then Is_Frozen (Prev_Entity)
5399 and then not Error_Posted (Id)
5400 then
5401 Error_Msg_N ("full constant declaration appears too late", N);
5402 end if;
5403
5404 Check_Eliminated (Id);
5405
5406 -- Deal with setting In_Private_Part flag if in private part
5407
5408 if Ekind (Scope (Id)) = E_Package
5409 and then In_Private_Part (Scope (Id))
5410 then
5411 Set_In_Private_Part (Id);
5412 end if;
5413
5414 <<Leave>>
5415 -- Initialize the refined state of a variable here because this is a
5416 -- common destination for legal and illegal object declarations.
5417
5418 if Ekind (Id) = E_Variable then
5419 Set_Encapsulating_State (Id, Empty);
5420 end if;
5421
5422 Analyze_Aspect_Specifications (N, Id);
5423
5424 Analyze_Dimension (N);
5425
5426 -- Verify whether the object declaration introduces an illegal hidden
5427 -- state within a package subject to a null abstract state.
5428
5429 if Ekind (Id) = E_Variable then
5430 Check_No_Hidden_State (Id);
5431 end if;
5432
5433 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5434 end Analyze_Object_Declaration;
5435
5436 ---------------------------
5437 -- Analyze_Others_Choice --
5438 ---------------------------
5439
5440 -- Nothing to do for the others choice node itself, the semantic analysis
5441 -- of the others choice will occur as part of the processing of the parent
5442
5443 procedure Analyze_Others_Choice (N : Node_Id) is
5444 pragma Warnings (Off, N);
5445 begin
5446 null;
5447 end Analyze_Others_Choice;
5448
5449 -------------------------------------------
5450 -- Analyze_Private_Extension_Declaration --
5451 -------------------------------------------
5452
5453 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5454 Indic : constant Node_Id := Subtype_Indication (N);
5455 T : constant Entity_Id := Defining_Identifier (N);
5456 Iface : Entity_Id;
5457 Iface_Elmt : Elmt_Id;
5458 Parent_Base : Entity_Id;
5459 Parent_Type : Entity_Id;
5460
5461 begin
5462 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5463
5464 if Is_Non_Empty_List (Interface_List (N)) then
5465 declare
5466 Intf : Node_Id;
5467 T : Entity_Id;
5468
5469 begin
5470 Intf := First (Interface_List (N));
5471 while Present (Intf) loop
5472 T := Find_Type_Of_Subtype_Indic (Intf);
5473
5474 Diagnose_Interface (Intf, T);
5475 Next (Intf);
5476 end loop;
5477 end;
5478 end if;
5479
5480 Generate_Definition (T);
5481
5482 -- For other than Ada 2012, just enter the name in the current scope
5483
5484 if Ada_Version < Ada_2012 then
5485 Enter_Name (T);
5486
5487 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5488 -- case of private type that completes an incomplete type.
5489
5490 else
5491 declare
5492 Prev : Entity_Id;
5493
5494 begin
5495 Prev := Find_Type_Name (N);
5496
5497 pragma Assert (Prev = T
5498 or else (Ekind (Prev) = E_Incomplete_Type
5499 and then Present (Full_View (Prev))
5500 and then Full_View (Prev) = T));
5501 end;
5502 end if;
5503
5504 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5505 Parent_Base := Base_Type (Parent_Type);
5506
5507 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5508 Mutate_Ekind (T, Ekind (Parent_Type));
5509 Set_Etype (T, Any_Type);
5510 goto Leave;
5511
5512 elsif not Is_Tagged_Type (Parent_Type) then
5513 Error_Msg_N
5514 ("parent of type extension must be a tagged type", Indic);
5515 goto Leave;
5516
5517 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5518 Error_Msg_N ("premature derivation of incomplete type", Indic);
5519 goto Leave;
5520
5521 elsif Is_Concurrent_Type (Parent_Type) then
5522 Error_Msg_N
5523 ("parent type of a private extension cannot be a synchronized "
5524 & "tagged type (RM 3.9.1 (3/1))", N);
5525
5526 Set_Etype (T, Any_Type);
5527 Mutate_Ekind (T, E_Limited_Private_Type);
5528 Set_Private_Dependents (T, New_Elmt_List);
5529 Set_Error_Posted (T);
5530 goto Leave;
5531 end if;
5532
5533 Check_Wide_Character_Restriction (Parent_Type, Indic);
5534
5535 -- Perhaps the parent type should be changed to the class-wide type's
5536 -- specific type in this case to prevent cascading errors ???
5537
5538 if Is_Class_Wide_Type (Parent_Type) then
5539 Error_Msg_N
5540 ("parent of type extension must not be a class-wide type", Indic);
5541 goto Leave;
5542 end if;
5543
5544 if (not Is_Package_Or_Generic_Package (Current_Scope)
5545 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5546 or else In_Private_Part (Current_Scope)
5547 then
5548 Error_Msg_N ("invalid context for private extension", N);
5549 end if;
5550
5551 -- Set common attributes
5552
5553 Set_Is_Pure (T, Is_Pure (Current_Scope));
5554 Set_Scope (T, Current_Scope);
5555 Mutate_Ekind (T, E_Record_Type_With_Private);
5556 Reinit_Size_Align (T);
5557 Set_Default_SSO (T);
5558 Set_No_Reordering (T, No_Component_Reordering);
5559 Set_Etype (T, Parent_Base);
5560 Set_Convention (T, Convention (Parent_Type));
5561 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5562 Set_Is_First_Subtype (T);
5563
5564 -- Set the SPARK mode from the current context
5565
5566 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5567 Set_SPARK_Pragma_Inherited (T);
5568
5569 if Unknown_Discriminants_Present (N) then
5570 Set_Discriminant_Constraint (T, No_Elist);
5571 end if;
5572
5573 Build_Derived_Record_Type (N, Parent_Type, T);
5574
5575 -- A private extension inherits the Default_Initial_Condition pragma
5576 -- coming from any parent type within the derivation chain.
5577
5578 if Has_DIC (Parent_Type) then
5579 Set_Has_Inherited_DIC (T);
5580 end if;
5581
5582 -- A private extension inherits any class-wide invariants coming from a
5583 -- parent type or an interface. Note that the invariant procedure of the
5584 -- parent type should not be inherited because the private extension may
5585 -- define invariants of its own.
5586
5587 if Has_Inherited_Invariants (Parent_Type)
5588 or else Has_Inheritable_Invariants (Parent_Type)
5589 then
5590 Set_Has_Inherited_Invariants (T);
5591
5592 elsif Present (Interfaces (T)) then
5593 Iface_Elmt := First_Elmt (Interfaces (T));
5594 while Present (Iface_Elmt) loop
5595 Iface := Node (Iface_Elmt);
5596
5597 if Has_Inheritable_Invariants (Iface) then
5598 Set_Has_Inherited_Invariants (T);
5599 exit;
5600 end if;
5601
5602 Next_Elmt (Iface_Elmt);
5603 end loop;
5604 end if;
5605
5606 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5607 -- synchronized formal derived type.
5608
5609 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5610 Set_Is_Limited_Record (T);
5611
5612 -- Formal derived type case
5613
5614 if Is_Generic_Type (T) then
5615
5616 -- The parent must be a tagged limited type or a synchronized
5617 -- interface.
5618
5619 if (not Is_Tagged_Type (Parent_Type)
5620 or else not Is_Limited_Type (Parent_Type))
5621 and then
5622 (not Is_Interface (Parent_Type)
5623 or else not Is_Synchronized_Interface (Parent_Type))
5624 then
5625 Error_Msg_NE
5626 ("parent type of & must be tagged limited or synchronized",
5627 N, T);
5628 end if;
5629
5630 -- The progenitors (if any) must be limited or synchronized
5631 -- interfaces.
5632
5633 if Present (Interfaces (T)) then
5634 Iface_Elmt := First_Elmt (Interfaces (T));
5635 while Present (Iface_Elmt) loop
5636 Iface := Node (Iface_Elmt);
5637
5638 if not Is_Limited_Interface (Iface)
5639 and then not Is_Synchronized_Interface (Iface)
5640 then
5641 Error_Msg_NE
5642 ("progenitor & must be limited or synchronized",
5643 N, Iface);
5644 end if;
5645
5646 Next_Elmt (Iface_Elmt);
5647 end loop;
5648 end if;
5649
5650 -- Regular derived extension, the parent must be a limited or
5651 -- synchronized interface.
5652
5653 else
5654 if not Is_Interface (Parent_Type)
5655 or else (not Is_Limited_Interface (Parent_Type)
5656 and then not Is_Synchronized_Interface (Parent_Type))
5657 then
5658 Error_Msg_NE
5659 ("parent type of & must be limited interface", N, T);
5660 end if;
5661 end if;
5662
5663 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5664 -- extension with a synchronized parent must be explicitly declared
5665 -- synchronized, because the full view will be a synchronized type.
5666 -- This must be checked before the check for limited types below,
5667 -- to ensure that types declared limited are not allowed to extend
5668 -- synchronized interfaces.
5669
5670 elsif Is_Interface (Parent_Type)
5671 and then Is_Synchronized_Interface (Parent_Type)
5672 and then not Synchronized_Present (N)
5673 then
5674 Error_Msg_NE
5675 ("private extension of& must be explicitly synchronized",
5676 N, Parent_Type);
5677
5678 elsif Limited_Present (N) then
5679 Set_Is_Limited_Record (T);
5680
5681 if not Is_Limited_Type (Parent_Type)
5682 and then
5683 (not Is_Interface (Parent_Type)
5684 or else not Is_Limited_Interface (Parent_Type))
5685 then
5686 Error_Msg_NE ("parent type& of limited extension must be limited",
5687 N, Parent_Type);
5688 end if;
5689 end if;
5690
5691 -- Remember that its parent type has a private extension. Used to warn
5692 -- on public primitives of the parent type defined after its private
5693 -- extensions (see Check_Dispatching_Operation).
5694
5695 Set_Has_Private_Extension (Parent_Type);
5696
5697 <<Leave>>
5698 Analyze_Aspect_Specifications (N, T);
5699 end Analyze_Private_Extension_Declaration;
5700
5701 ---------------------------------
5702 -- Analyze_Subtype_Declaration --
5703 ---------------------------------
5704
5705 procedure Analyze_Subtype_Declaration
5706 (N : Node_Id;
5707 Skip : Boolean := False)
5708 is
5709 Id : constant Entity_Id := Defining_Identifier (N);
5710 T : Entity_Id;
5711
5712 begin
5713 Generate_Definition (Id);
5714 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5715 Reinit_Size_Align (Id);
5716
5717 -- The following guard condition on Enter_Name is to handle cases where
5718 -- the defining identifier has already been entered into the scope but
5719 -- the declaration as a whole needs to be analyzed.
5720
5721 -- This case in particular happens for derived enumeration types. The
5722 -- derived enumeration type is processed as an inserted enumeration type
5723 -- declaration followed by a rewritten subtype declaration. The defining
5724 -- identifier, however, is entered into the name scope very early in the
5725 -- processing of the original type declaration and therefore needs to be
5726 -- avoided here, when the created subtype declaration is analyzed. (See
5727 -- Build_Derived_Types)
5728
5729 -- This also happens when the full view of a private type is a derived
5730 -- type with constraints. In this case the entity has been introduced
5731 -- in the private declaration.
5732
5733 -- Finally this happens in some complex cases when validity checks are
5734 -- enabled, where the same subtype declaration may be analyzed twice.
5735 -- This can happen if the subtype is created by the preanalysis of
5736 -- an attribute that gives the range of a loop statement, and the loop
5737 -- itself appears within an if_statement that will be rewritten during
5738 -- expansion.
5739
5740 if Skip
5741 or else (Present (Etype (Id))
5742 and then (Is_Private_Type (Etype (Id))
5743 or else Is_Task_Type (Etype (Id))
5744 or else Is_Rewrite_Substitution (N)))
5745 then
5746 null;
5747
5748 elsif Current_Entity (Id) = Id then
5749 null;
5750
5751 else
5752 Enter_Name (Id);
5753 end if;
5754
5755 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5756
5757 -- Class-wide equivalent types of records with unknown discriminants
5758 -- involve the generation of an itype which serves as the private view
5759 -- of a constrained record subtype. In such cases the base type of the
5760 -- current subtype we are processing is the private itype. Use the full
5761 -- of the private itype when decorating various attributes.
5762
5763 if Is_Itype (T)
5764 and then Is_Private_Type (T)
5765 and then Present (Full_View (T))
5766 then
5767 T := Full_View (T);
5768 end if;
5769
5770 -- Inherit common attributes
5771
5772 Set_Is_Volatile (Id, Is_Volatile (T));
5773 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5774 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5775 Set_Convention (Id, Convention (T));
5776
5777 -- If ancestor has predicates then so does the subtype, and in addition
5778 -- we must delay the freeze to properly arrange predicate inheritance.
5779
5780 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5781 -- in which T = ID, so the above tests and assignments do nothing???
5782
5783 if Has_Predicates (T)
5784 or else (Present (Ancestor_Subtype (T))
5785 and then Has_Predicates (Ancestor_Subtype (T)))
5786 then
5787 Set_Has_Predicates (Id);
5788 Set_Has_Delayed_Freeze (Id);
5789
5790 -- Generated subtypes inherit the predicate function from the parent
5791 -- (no aspects to examine on the generated declaration).
5792
5793 if not Comes_From_Source (N) then
5794 Mutate_Ekind (Id, Ekind (T));
5795
5796 if Present (Predicate_Function (Id)) then
5797 null;
5798
5799 elsif Present (Predicate_Function (T)) then
5800 Set_Predicate_Function (Id, Predicate_Function (T));
5801
5802 elsif Present (Ancestor_Subtype (T))
5803 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5804 then
5805 Set_Predicate_Function (Id,
5806 Predicate_Function (Ancestor_Subtype (T)));
5807 end if;
5808 end if;
5809 end if;
5810
5811 -- In the case where there is no constraint given in the subtype
5812 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5813 -- semantic attributes must be established here.
5814
5815 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5816 Set_Etype (Id, Base_Type (T));
5817
5818 case Ekind (T) is
5819 when Array_Kind =>
5820 Mutate_Ekind (Id, E_Array_Subtype);
5821 Copy_Array_Subtype_Attributes (Id, T);
5822 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5823
5824 when Decimal_Fixed_Point_Kind =>
5825 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5826 Set_Digits_Value (Id, Digits_Value (T));
5827 Set_Delta_Value (Id, Delta_Value (T));
5828 Set_Scale_Value (Id, Scale_Value (T));
5829 Set_Small_Value (Id, Small_Value (T));
5830 Set_Scalar_Range (Id, Scalar_Range (T));
5831 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5832 Set_Is_Constrained (Id, Is_Constrained (T));
5833 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5834 Copy_RM_Size (To => Id, From => T);
5835
5836 when Enumeration_Kind =>
5837 Mutate_Ekind (Id, E_Enumeration_Subtype);
5838 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5839 Set_Scalar_Range (Id, Scalar_Range (T));
5840 Set_Is_Character_Type (Id, Is_Character_Type (T));
5841 Set_Is_Constrained (Id, Is_Constrained (T));
5842 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5843 Copy_RM_Size (To => Id, From => T);
5844
5845 when Ordinary_Fixed_Point_Kind =>
5846 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5847 Set_Scalar_Range (Id, Scalar_Range (T));
5848 Set_Small_Value (Id, Small_Value (T));
5849 Set_Delta_Value (Id, Delta_Value (T));
5850 Set_Is_Constrained (Id, Is_Constrained (T));
5851 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5852 Copy_RM_Size (To => Id, From => T);
5853
5854 when Float_Kind =>
5855 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5856 Set_Scalar_Range (Id, Scalar_Range (T));
5857 Set_Digits_Value (Id, Digits_Value (T));
5858 Set_Is_Constrained (Id, Is_Constrained (T));
5859
5860 -- If the floating point type has dimensions, these will be
5861 -- inherited subsequently when Analyze_Dimensions is called.
5862
5863 when Signed_Integer_Kind =>
5864 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5865 Set_Scalar_Range (Id, Scalar_Range (T));
5866 Set_Is_Constrained (Id, Is_Constrained (T));
5867 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5868 Copy_RM_Size (To => Id, From => T);
5869
5870 when Modular_Integer_Kind =>
5871 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5872 Set_Scalar_Range (Id, Scalar_Range (T));
5873 Set_Is_Constrained (Id, Is_Constrained (T));
5874 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5875 Copy_RM_Size (To => Id, From => T);
5876
5877 when Class_Wide_Kind =>
5878 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5879 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5880 Set_Cloned_Subtype (Id, T);
5881 Set_Is_Tagged_Type (Id, True);
5882 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5883 Set_Has_Unknown_Discriminants
5884 (Id, True);
5885 Set_No_Tagged_Streams_Pragma
5886 (Id, No_Tagged_Streams_Pragma (T));
5887
5888 if Ekind (T) = E_Class_Wide_Subtype then
5889 Set_Equivalent_Type (Id, Equivalent_Type (T));
5890 end if;
5891
5892 when E_Record_Subtype
5893 | E_Record_Type
5894 =>
5895 Mutate_Ekind (Id, E_Record_Subtype);
5896
5897 -- Subtype declarations introduced for formal type parameters
5898 -- in generic instantiations should inherit the Size value of
5899 -- the type they rename.
5900
5901 if Present (Generic_Parent_Type (N)) then
5902 Copy_RM_Size (To => Id, From => T);
5903 end if;
5904
5905 if Ekind (T) = E_Record_Subtype
5906 and then Present (Cloned_Subtype (T))
5907 then
5908 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5909 else
5910 Set_Cloned_Subtype (Id, T);
5911 end if;
5912
5913 Set_First_Entity (Id, First_Entity (T));
5914 Set_Last_Entity (Id, Last_Entity (T));
5915 Set_Has_Discriminants (Id, Has_Discriminants (T));
5916 Set_Is_Constrained (Id, Is_Constrained (T));
5917 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5918 Set_Has_Implicit_Dereference
5919 (Id, Has_Implicit_Dereference (T));
5920 Set_Has_Unknown_Discriminants
5921 (Id, Has_Unknown_Discriminants (T));
5922
5923 if Has_Discriminants (T) then
5924 Set_Discriminant_Constraint
5925 (Id, Discriminant_Constraint (T));
5926 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5927
5928 elsif Has_Unknown_Discriminants (Id) then
5929 Set_Discriminant_Constraint (Id, No_Elist);
5930 end if;
5931
5932 if Is_Tagged_Type (T) then
5933 Set_Is_Tagged_Type (Id, True);
5934 Set_No_Tagged_Streams_Pragma
5935 (Id, No_Tagged_Streams_Pragma (T));
5936 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5937 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5938
5939 if Is_Interface (T) then
5940 Set_Is_Interface (Id);
5941 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5942 end if;
5943 end if;
5944
5945 when Private_Kind =>
5946 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5947 Set_Has_Discriminants (Id, Has_Discriminants (T));
5948 Set_Is_Constrained (Id, Is_Constrained (T));
5949 Set_First_Entity (Id, First_Entity (T));
5950 Set_Last_Entity (Id, Last_Entity (T));
5951 Set_Private_Dependents (Id, New_Elmt_List);
5952 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5953 Set_Has_Implicit_Dereference
5954 (Id, Has_Implicit_Dereference (T));
5955 Set_Has_Unknown_Discriminants
5956 (Id, Has_Unknown_Discriminants (T));
5957 Set_Known_To_Have_Preelab_Init
5958 (Id, Known_To_Have_Preelab_Init (T));
5959
5960 if Is_Tagged_Type (T) then
5961 Set_Is_Tagged_Type (Id);
5962 Set_No_Tagged_Streams_Pragma (Id,
5963 No_Tagged_Streams_Pragma (T));
5964 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5965 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5966 end if;
5967
5968 -- In general the attributes of the subtype of a private type
5969 -- are the attributes of the partial view of parent. However,
5970 -- the full view may be a discriminated type, and the subtype
5971 -- must share the discriminant constraint to generate correct
5972 -- calls to initialization procedures.
5973
5974 if Has_Discriminants (T) then
5975 Set_Discriminant_Constraint
5976 (Id, Discriminant_Constraint (T));
5977 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5978
5979 elsif Present (Full_View (T))
5980 and then Has_Discriminants (Full_View (T))
5981 then
5982 Set_Discriminant_Constraint
5983 (Id, Discriminant_Constraint (Full_View (T)));
5984 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5985
5986 -- This would seem semantically correct, but apparently
5987 -- generates spurious errors about missing components ???
5988
5989 -- Set_Has_Discriminants (Id);
5990 end if;
5991
5992 Prepare_Private_Subtype_Completion (Id, N);
5993
5994 -- If this is the subtype of a constrained private type with
5995 -- discriminants that has got a full view and we also have
5996 -- built a completion just above, show that the completion
5997 -- is a clone of the full view to the back-end.
5998
5999 if Has_Discriminants (T)
6000 and then not Has_Unknown_Discriminants (T)
6001 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
6002 and then Present (Full_View (T))
6003 and then Present (Full_View (Id))
6004 then
6005 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
6006 end if;
6007
6008 when Access_Kind =>
6009 Mutate_Ekind (Id, E_Access_Subtype);
6010 Set_Is_Constrained (Id, Is_Constrained (T));
6011 Set_Is_Access_Constant
6012 (Id, Is_Access_Constant (T));
6013 Set_Directly_Designated_Type
6014 (Id, Designated_Type (T));
6015 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
6016
6017 -- A Pure library_item must not contain the declaration of a
6018 -- named access type, except within a subprogram, generic
6019 -- subprogram, task unit, or protected unit, or if it has
6020 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
6021
6022 if Comes_From_Source (Id)
6023 and then In_Pure_Unit
6024 and then not In_Subprogram_Task_Protected_Unit
6025 and then not No_Pool_Assigned (Id)
6026 then
6027 Error_Msg_N
6028 ("named access types not allowed in pure unit", N);
6029 end if;
6030
6031 when Concurrent_Kind =>
6032 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
6033 Set_Corresponding_Record_Type (Id,
6034 Corresponding_Record_Type (T));
6035 Set_First_Entity (Id, First_Entity (T));
6036 Set_First_Private_Entity (Id, First_Private_Entity (T));
6037 Set_Has_Discriminants (Id, Has_Discriminants (T));
6038 Set_Is_Constrained (Id, Is_Constrained (T));
6039 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
6040 Set_Last_Entity (Id, Last_Entity (T));
6041
6042 if Is_Tagged_Type (T) then
6043 Set_No_Tagged_Streams_Pragma
6044 (Id, No_Tagged_Streams_Pragma (T));
6045 end if;
6046
6047 if Has_Discriminants (T) then
6048 Set_Discriminant_Constraint
6049 (Id, Discriminant_Constraint (T));
6050 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
6051 end if;
6052
6053 when Incomplete_Kind =>
6054 if Ada_Version >= Ada_2005 then
6055
6056 -- In Ada 2005 an incomplete type can be explicitly tagged:
6057 -- propagate indication. Note that we also have to include
6058 -- subtypes for Ada 2012 extended use of incomplete types.
6059
6060 Mutate_Ekind (Id, E_Incomplete_Subtype);
6061 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
6062 Set_Private_Dependents (Id, New_Elmt_List);
6063
6064 if Is_Tagged_Type (Id) then
6065 Set_No_Tagged_Streams_Pragma
6066 (Id, No_Tagged_Streams_Pragma (T));
6067 end if;
6068
6069 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
6070 -- incomplete type visible through a limited with clause.
6071
6072 if From_Limited_With (T)
6073 and then Present (Non_Limited_View (T))
6074 then
6075 Set_From_Limited_With (Id);
6076 Set_Non_Limited_View (Id, Non_Limited_View (T));
6077
6078 -- Ada 2005 (AI-412): Add the regular incomplete subtype
6079 -- to the private dependents of the original incomplete
6080 -- type for future transformation.
6081
6082 else
6083 Append_Elmt (Id, Private_Dependents (T));
6084 end if;
6085
6086 -- If the subtype name denotes an incomplete type an error
6087 -- was already reported by Process_Subtype.
6088
6089 else
6090 Set_Etype (Id, Any_Type);
6091 end if;
6092
6093 when others =>
6094 raise Program_Error;
6095 end case;
6096
6097 -- If there is no constraint in the subtype indication, the
6098 -- declared entity inherits predicates from the parent.
6099
6100 Inherit_Predicate_Flags (Id, T);
6101 end if;
6102
6103 if Etype (Id) = Any_Type then
6104 goto Leave;
6105 end if;
6106
6107 -- When prefixed calls are enabled for untagged types, the subtype
6108 -- shares the primitive operations of its base type. Do this even
6109 -- when GNAT extensions are not allowed, in order to give better
6110 -- error messages.
6111
6112 Set_Direct_Primitive_Operations
6113 (Id, Direct_Primitive_Operations (Base_Type (T)));
6114
6115 -- Some common processing on all types
6116
6117 Set_Size_Info (Id, T);
6118 Set_First_Rep_Item (Id, First_Rep_Item (T));
6119
6120 -- If the parent type is a generic actual, so is the subtype. This may
6121 -- happen in a nested instance. Why Comes_From_Source test???
6122
6123 if not Comes_From_Source (N) then
6124 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
6125 end if;
6126
6127 -- If this is a subtype declaration for an actual in an instance,
6128 -- inherit static and dynamic predicates if any.
6129
6130 if Has_Predicates (T)
6131 and then Present (Predicate_Function (T))
6132 and then In_Instance
6133 and then not Comes_From_Source (N)
6134 then
6135 -- Inherit Subprograms_For_Type from the full view, if present
6136
6137 if Present (Full_View (T))
6138 and then Present (Subprograms_For_Type (Full_View (T)))
6139 then
6140 Set_Subprograms_For_Type
6141 (Id, Subprograms_For_Type (Full_View (T)));
6142 else
6143 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
6144 end if;
6145
6146 -- If the current declaration created both a private and a full view,
6147 -- then propagate Predicate_Function to the latter as well.
6148
6149 if Present (Full_View (Id))
6150 and then No (Predicate_Function (Full_View (Id)))
6151 then
6152 Set_Subprograms_For_Type
6153 (Full_View (Id), Subprograms_For_Type (Id));
6154 end if;
6155
6156 if Has_Static_Predicate (T) then
6157 Set_Has_Static_Predicate (Id);
6158 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
6159 end if;
6160 end if;
6161
6162 -- If the base type is a scalar type, or else if there is no
6163 -- constraint, the atomic flag is inherited by the subtype.
6164 -- Ditto for the Independent aspect.
6165
6166 if Is_Scalar_Type (Id)
6167 or else Is_Entity_Name (Subtype_Indication (N))
6168 then
6169 Set_Is_Atomic (Id, Is_Atomic (T));
6170 Set_Is_Independent (Id, Is_Independent (T));
6171 end if;
6172
6173 -- Remaining processing depends on characteristics of base type
6174
6175 T := Etype (Id);
6176
6177 Set_Is_Immediately_Visible (Id, True);
6178 Set_Depends_On_Private (Id, Has_Private_Component (T));
6179 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6180
6181 if Is_Interface (T) then
6182 Set_Is_Interface (Id);
6183 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6184 end if;
6185
6186 if Present (Generic_Parent_Type (N))
6187 and then
6188 (Nkind (Parent (Generic_Parent_Type (N))) /=
6189 N_Formal_Type_Declaration
6190 or else Nkind (Formal_Type_Definition
6191 (Parent (Generic_Parent_Type (N)))) /=
6192 N_Formal_Private_Type_Definition)
6193 then
6194 if Is_Tagged_Type (Id) then
6195
6196 -- If this is a generic actual subtype for a synchronized type,
6197 -- the primitive operations are those of the corresponding record
6198 -- for which there is a separate subtype declaration.
6199
6200 if Is_Concurrent_Type (Id) then
6201 null;
6202 elsif Is_Class_Wide_Type (Id) then
6203 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6204 else
6205 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6206 end if;
6207
6208 elsif Scope (Etype (Id)) /= Standard_Standard then
6209 Derive_Subprograms (Generic_Parent_Type (N), Id);
6210 end if;
6211 end if;
6212
6213 if Is_Private_Type (T) and then Present (Full_View (T)) then
6214 Conditional_Delay (Id, Full_View (T));
6215
6216 -- The subtypes of components or subcomponents of protected types
6217 -- do not need freeze nodes, which would otherwise appear in the
6218 -- wrong scope (before the freeze node for the protected type). The
6219 -- proper subtypes are those of the subcomponents of the corresponding
6220 -- record.
6221
6222 elsif Ekind (Scope (Id)) /= E_Protected_Type
6223 and then Present (Scope (Scope (Id))) -- error defense
6224 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6225 then
6226 Conditional_Delay (Id, T);
6227 end if;
6228
6229 -- If we have a subtype of an incomplete type whose full type is a
6230 -- derived numeric type, we need to have a freeze node for the subtype.
6231 -- Otherwise gigi will complain while computing the (static) bounds of
6232 -- the subtype.
6233
6234 if Is_Itype (T)
6235 and then Is_Elementary_Type (Id)
6236 and then Etype (Id) /= Id
6237 then
6238 declare
6239 Partial : constant Entity_Id :=
6240 Incomplete_Or_Partial_View (First_Subtype (Id));
6241 begin
6242 if Present (Partial)
6243 and then Ekind (Partial) = E_Incomplete_Type
6244 then
6245 Set_Has_Delayed_Freeze (Id);
6246 end if;
6247 end;
6248 end if;
6249
6250 -- Check that Constraint_Error is raised for a scalar subtype indication
6251 -- when the lower or upper bound of a non-null range lies outside the
6252 -- range of the type mark. Likewise for an array subtype, but check the
6253 -- compatibility for each index.
6254
6255 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6256 declare
6257 Indic_Typ : constant Entity_Id :=
6258 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6259 Subt_Index : Node_Id;
6260 Target_Index : Node_Id;
6261
6262 begin
6263 if Is_Scalar_Type (Etype (Id))
6264 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6265 then
6266 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6267
6268 elsif Is_Array_Type (Etype (Id))
6269 and then Present (First_Index (Id))
6270 then
6271 Subt_Index := First_Index (Id);
6272 Target_Index := First_Index (Indic_Typ);
6273
6274 while Present (Subt_Index) loop
6275 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6276 and then Is_Scalar_Type (Entity (Subt_Index)))
6277 or else Nkind (Subt_Index) = N_Subtype_Indication)
6278 and then
6279 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6280 then
6281 Apply_Range_Check
6282 (Scalar_Range (Etype (Subt_Index)),
6283 Etype (Target_Index),
6284 Insert_Node => N);
6285 end if;
6286
6287 Next_Index (Subt_Index);
6288 Next_Index (Target_Index);
6289 end loop;
6290 end if;
6291 end;
6292 end if;
6293
6294 Set_Optimize_Alignment_Flags (Id);
6295 Check_Eliminated (Id);
6296
6297 <<Leave>>
6298 Analyze_Aspect_Specifications (N, Id);
6299
6300 Analyze_Dimension (N);
6301
6302 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6303 -- indications on composite types where the constraints are dynamic.
6304 -- Note that object declarations and aggregates generate implicit
6305 -- subtype declarations, which this covers. One special case is that the
6306 -- implicitly generated "=" for discriminated types includes an
6307 -- offending subtype declaration, which is harmless, so we ignore it
6308 -- here.
6309
6310 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6311 declare
6312 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6313 begin
6314 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6315 and then not (Is_Internal (Id)
6316 and then Is_TSS (Scope (Id),
6317 TSS_Composite_Equality))
6318 and then not Within_Init_Proc
6319 and then not All_Composite_Constraints_Static (Cstr)
6320 then
6321 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6322 end if;
6323 end;
6324 end if;
6325 end Analyze_Subtype_Declaration;
6326
6327 --------------------------------
6328 -- Analyze_Subtype_Indication --
6329 --------------------------------
6330
6331 procedure Analyze_Subtype_Indication (N : Node_Id) is
6332 T : constant Entity_Id := Subtype_Mark (N);
6333 R : constant Node_Id := Range_Expression (Constraint (N));
6334
6335 begin
6336 Analyze (T);
6337
6338 if R = Error then
6339 Set_Error_Posted (R);
6340 Set_Error_Posted (T);
6341 else
6342 Analyze (R);
6343 Set_Etype (N, Etype (R));
6344 Resolve (R, Entity (T));
6345 end if;
6346 end Analyze_Subtype_Indication;
6347
6348 --------------------------
6349 -- Analyze_Variant_Part --
6350 --------------------------
6351
6352 procedure Analyze_Variant_Part (N : Node_Id) is
6353 Discr_Name : Node_Id;
6354 Discr_Type : Entity_Id;
6355
6356 procedure Process_Variant (A : Node_Id);
6357 -- Analyze declarations for a single variant
6358
6359 package Analyze_Variant_Choices is
6360 new Generic_Analyze_Choices (Process_Variant);
6361 use Analyze_Variant_Choices;
6362
6363 ---------------------
6364 -- Process_Variant --
6365 ---------------------
6366
6367 procedure Process_Variant (A : Node_Id) is
6368 CL : constant Node_Id := Component_List (A);
6369 begin
6370 if not Null_Present (CL) then
6371 Analyze_Declarations (Component_Items (CL));
6372
6373 if Present (Variant_Part (CL)) then
6374 Analyze (Variant_Part (CL));
6375 end if;
6376 end if;
6377 end Process_Variant;
6378
6379 -- Start of processing for Analyze_Variant_Part
6380
6381 begin
6382 Discr_Name := Name (N);
6383 Analyze (Discr_Name);
6384
6385 -- If Discr_Name bad, get out (prevent cascaded errors)
6386
6387 if Etype (Discr_Name) = Any_Type then
6388 return;
6389 end if;
6390
6391 -- Check invalid discriminant in variant part
6392
6393 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6394 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6395 end if;
6396
6397 Discr_Type := Etype (Entity (Discr_Name));
6398
6399 if not Is_Discrete_Type (Discr_Type) then
6400 Error_Msg_N
6401 ("discriminant in a variant part must be of a discrete type",
6402 Name (N));
6403 return;
6404 end if;
6405
6406 -- Now analyze the choices, which also analyzes the declarations that
6407 -- are associated with each choice.
6408
6409 Analyze_Choices (Variants (N), Discr_Type);
6410
6411 -- Note: we used to instantiate and call Check_Choices here to check
6412 -- that the choices covered the discriminant, but it's too early to do
6413 -- that because of statically predicated subtypes, whose analysis may
6414 -- be deferred to their freeze point which may be as late as the freeze
6415 -- point of the containing record. So this call is now to be found in
6416 -- Freeze_Record_Declaration.
6417
6418 end Analyze_Variant_Part;
6419
6420 ----------------------------
6421 -- Array_Type_Declaration --
6422 ----------------------------
6423
6424 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6425 Component_Def : constant Node_Id := Component_Definition (Def);
6426 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6427 P : constant Node_Id := Parent (Def);
6428 Element_Type : Entity_Id;
6429 Implicit_Base : Entity_Id;
6430 Index : Node_Id;
6431 Nb_Index : Pos;
6432 Priv : Entity_Id;
6433 Related_Id : Entity_Id;
6434 Has_FLB_Index : Boolean := False;
6435
6436 begin
6437 if Nkind (Def) = N_Constrained_Array_Definition then
6438 Index := First (Discrete_Subtype_Definitions (Def));
6439 else
6440 Index := First (Subtype_Marks (Def));
6441 end if;
6442
6443 -- Find proper names for the implicit types which may be public. In case
6444 -- of anonymous arrays we use the name of the first object of that type
6445 -- as prefix.
6446
6447 if No (T) then
6448 Related_Id := Defining_Identifier (P);
6449 else
6450 Related_Id := T;
6451 end if;
6452
6453 Nb_Index := 1;
6454 while Present (Index) loop
6455 Analyze (Index);
6456
6457 -- Test for odd case of trying to index a type by the type itself
6458
6459 if Is_Entity_Name (Index) and then Entity (Index) = T then
6460 Error_Msg_N ("type& cannot be indexed by itself", Index);
6461 Set_Entity (Index, Standard_Boolean);
6462 Set_Etype (Index, Standard_Boolean);
6463 end if;
6464
6465 -- Add a subtype declaration for each index of private array type
6466 -- declaration whose type is also private. For example:
6467
6468 -- package Pkg is
6469 -- type Index is private;
6470 -- private
6471 -- type Table is array (Index) of ...
6472 -- end;
6473
6474 -- This is currently required by the expander for the internally
6475 -- generated equality subprogram of records with variant parts in
6476 -- which the type of some component is such a private type. And it
6477 -- also helps semantic analysis in peculiar cases where the array
6478 -- type is referenced from an instance but not the index directly.
6479
6480 if Is_Package_Or_Generic_Package (Current_Scope)
6481 and then In_Private_Part (Current_Scope)
6482 and then Has_Private_Declaration (Etype (Index))
6483 and then Scope (Etype (Index)) = Current_Scope
6484 then
6485 declare
6486 Loc : constant Source_Ptr := Sloc (Def);
6487 Decl : Node_Id;
6488 New_E : Entity_Id;
6489
6490 begin
6491 New_E := Make_Temporary (Loc, 'T');
6492 Set_Is_Internal (New_E);
6493
6494 Decl :=
6495 Make_Subtype_Declaration (Loc,
6496 Defining_Identifier => New_E,
6497 Subtype_Indication =>
6498 New_Occurrence_Of (Etype (Index), Loc));
6499
6500 Insert_Before (Parent (Def), Decl);
6501 Analyze (Decl);
6502 Set_Etype (Index, New_E);
6503
6504 -- If the index is a range or a subtype indication it carries
6505 -- no entity. Example:
6506
6507 -- package Pkg is
6508 -- type T is private;
6509 -- private
6510 -- type T is new Natural;
6511 -- Table : array (T(1) .. T(10)) of Boolean;
6512 -- end Pkg;
6513
6514 -- Otherwise the type of the reference is its entity.
6515
6516 if Is_Entity_Name (Index) then
6517 Set_Entity (Index, New_E);
6518 end if;
6519 end;
6520 end if;
6521
6522 Make_Index (Index, P, Related_Id, Nb_Index);
6523
6524 -- In the case where we have an unconstrained array with an index
6525 -- given by a subtype_indication, this is necessarily a "fixed lower
6526 -- bound" index. We change the upper bound of that index to the upper
6527 -- bound of the index's subtype (denoted by the subtype_mark), since
6528 -- that upper bound was originally set by the parser to be the same
6529 -- as the lower bound. In truth, that upper bound corresponds to
6530 -- a box ("<>"), and could be set to Empty, but it's convenient to
6531 -- set it to the upper bound to avoid needing to add special tests
6532 -- in various places for an Empty upper bound, and in any case that
6533 -- accurately characterizes the index's range of values.
6534
6535 if Nkind (Def) = N_Unconstrained_Array_Definition
6536 and then Nkind (Index) = N_Subtype_Indication
6537 then
6538 declare
6539 Index_Subtype_High_Bound : constant Entity_Id :=
6540 Type_High_Bound (Entity (Subtype_Mark (Index)));
6541 begin
6542 Set_High_Bound (Range_Expression (Constraint (Index)),
6543 Index_Subtype_High_Bound);
6544
6545 -- Record that the array type has one or more indexes with
6546 -- a fixed lower bound.
6547
6548 Has_FLB_Index := True;
6549
6550 -- Mark the index as belonging to an array type with a fixed
6551 -- lower bound.
6552
6553 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6554 end;
6555 end if;
6556
6557 -- Check error of subtype with predicate for index type
6558
6559 Bad_Predicated_Subtype_Use
6560 ("subtype& has predicate, not allowed as index subtype",
6561 Index, Etype (Index));
6562
6563 -- Move to next index
6564
6565 Next (Index);
6566 Nb_Index := Nb_Index + 1;
6567 end loop;
6568
6569 -- Process subtype indication if one is present
6570
6571 if Present (Component_Typ) then
6572 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6573 Set_Etype (Component_Typ, Element_Type);
6574
6575 -- Ada 2005 (AI-230): Access Definition case
6576
6577 else pragma Assert (Present (Access_Definition (Component_Def)));
6578
6579 -- Indicate that the anonymous access type is created by the
6580 -- array type declaration.
6581
6582 Element_Type := Access_Definition
6583 (Related_Nod => P,
6584 N => Access_Definition (Component_Def));
6585 Set_Is_Local_Anonymous_Access (Element_Type);
6586
6587 -- Ada 2005 (AI-230): In case of components that are anonymous access
6588 -- types the level of accessibility depends on the enclosing type
6589 -- declaration
6590
6591 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6592
6593 -- Ada 2005 (AI-254)
6594
6595 declare
6596 CD : constant Node_Id :=
6597 Access_To_Subprogram_Definition
6598 (Access_Definition (Component_Def));
6599 begin
6600 if Present (CD) and then Protected_Present (CD) then
6601 Element_Type :=
6602 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6603 end if;
6604 end;
6605 end if;
6606
6607 if No (T) then
6608 -- We might be creating more than one itype with the same Related_Id,
6609 -- e.g. for an array object definition and its initial value. Give
6610 -- them unique suffixes, because GNATprove require distinct types to
6611 -- have different names.
6612
6613 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6614 end if;
6615
6616 -- Constrained array case
6617
6618 if Nkind (Def) = N_Constrained_Array_Definition then
6619 Index := First (Discrete_Subtype_Definitions (Def));
6620
6621 -- Establish Implicit_Base as unconstrained base type
6622
6623 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6624
6625 Set_Etype (Implicit_Base, Implicit_Base);
6626 Set_Scope (Implicit_Base, Current_Scope);
6627 Set_First_Index (Implicit_Base, Index);
6628 Set_Has_Delayed_Freeze (Implicit_Base);
6629
6630 -- The constrained array type is a subtype of the unconstrained one
6631
6632 Mutate_Ekind (T, E_Array_Subtype);
6633 Reinit_Size_Align (T);
6634 Set_Etype (T, Implicit_Base);
6635 Set_Scope (T, Current_Scope);
6636 Set_First_Index (T, Index);
6637 Set_Has_Delayed_Freeze (T);
6638 Set_Is_Constrained (T);
6639
6640 -- Unconstrained array case
6641
6642 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6643 Mutate_Ekind (T, E_Array_Type);
6644 Reinit_Size_Align (T);
6645 Set_Etype (T, T);
6646 Set_Scope (T, Current_Scope);
6647 Set_First_Index (T, First (Subtype_Marks (Def)));
6648 Set_Has_Delayed_Freeze (T);
6649 Set_Is_Fixed_Lower_Bound_Array_Subtype
6650 (T, Has_FLB_Index);
6651 end if;
6652
6653 -- Common attributes for both cases
6654
6655 Set_Component_Type (Etype (T), Element_Type);
6656
6657 if Aliased_Present (Component_Definition (Def)) then
6658 Set_Has_Aliased_Components (Etype (T));
6659
6660 -- AI12-001: All aliased objects are considered to be specified as
6661 -- independently addressable (RM C.6(8.1/4)).
6662
6663 Set_Has_Independent_Components (Etype (T));
6664 end if;
6665
6666 pragma Assert (not Known_Component_Size (Etype (T)));
6667
6668 Propagate_Concurrent_Flags (Etype (T), Element_Type);
6669 Propagate_Controlled_Flags (Etype (T), Element_Type, Comp => True);
6670
6671 Set_Default_SSO (Etype (T));
6672
6673 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6674 -- array type to ensure that objects of this type are initialized.
6675
6676 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6677 Set_Can_Never_Be_Null (T);
6678
6679 if Null_Exclusion_Present (Component_Definition (Def))
6680
6681 -- No need to check itypes because in their case this check was
6682 -- done at their point of creation
6683
6684 and then not Is_Itype (Element_Type)
6685 then
6686 Error_Msg_N
6687 ("`NOT NULL` not allowed (null already excluded)",
6688 Subtype_Indication (Component_Definition (Def)));
6689 end if;
6690 end if;
6691
6692 Priv := Private_Component (Element_Type);
6693
6694 if Present (Priv) then
6695
6696 -- Check for circular definitions
6697
6698 if Priv = Any_Type then
6699 Set_Component_Type (Etype (T), Any_Type);
6700
6701 -- There is a gap in the visibility of operations on the composite
6702 -- type only if the component type is defined in a different scope.
6703
6704 elsif Scope (Priv) = Current_Scope then
6705 null;
6706
6707 elsif Is_Limited_Type (Priv) then
6708 Set_Is_Limited_Composite (Etype (T));
6709 Set_Is_Limited_Composite (T);
6710 else
6711 Set_Is_Private_Composite (Etype (T));
6712 Set_Is_Private_Composite (T);
6713 end if;
6714 end if;
6715
6716 -- A syntax error in the declaration itself may lead to an empty index
6717 -- list, in which case do a minimal patch.
6718
6719 if No (First_Index (T)) then
6720 Error_Msg_N ("missing index definition in array type declaration", T);
6721
6722 declare
6723 Indexes : constant List_Id :=
6724 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6725 begin
6726 Set_Discrete_Subtype_Definitions (Def, Indexes);
6727 Set_First_Index (T, First (Indexes));
6728 return;
6729 end;
6730 end if;
6731
6732 -- Create a concatenation operator for the new type. Internal array
6733 -- types created for packed entities do not need such, they are
6734 -- compatible with the user-defined type.
6735
6736 if Number_Dimensions (T) = 1
6737 and then not Is_Packed_Array_Impl_Type (T)
6738 then
6739 New_Concatenation_Op (T);
6740 end if;
6741
6742 -- In the case of an unconstrained array the parser has already verified
6743 -- that all the indexes are unconstrained but we still need to make sure
6744 -- that the element type is constrained.
6745
6746 if Is_Mutably_Tagged_Type (Element_Type) then
6747 Set_Component_Type (T,
6748 Class_Wide_Equivalent_Type (Element_Type));
6749
6750 elsif not Is_Definite_Subtype (Element_Type) then
6751 Error_Msg_N
6752 ("unconstrained element type in array declaration",
6753 Subtype_Indication (Component_Def));
6754
6755 elsif Is_Abstract_Type (Element_Type) then
6756 Error_Msg_N
6757 ("the type of a component cannot be abstract",
6758 Subtype_Indication (Component_Def));
6759 end if;
6760
6761 -- There may be an invariant declared for the component type, but
6762 -- the construction of the component invariant checking procedure
6763 -- takes place during expansion.
6764 end Array_Type_Declaration;
6765
6766 ------------------------------------------------------
6767 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6768 ------------------------------------------------------
6769
6770 function Replace_Anonymous_Access_To_Protected_Subprogram
6771 (N : Node_Id) return Entity_Id
6772 is
6773 Loc : constant Source_Ptr := Sloc (N);
6774
6775 Curr_Scope : constant Scope_Stack_Entry :=
6776 Scope_Stack.Table (Scope_Stack.Last);
6777
6778 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6779
6780 Acc : Node_Id;
6781 -- Access definition in declaration
6782
6783 Comp : Node_Id;
6784 -- Object definition or formal definition with an access definition
6785
6786 Decl : Node_Id;
6787 -- Declaration of anonymous access to subprogram type
6788
6789 Spec : Node_Id;
6790 -- Original specification in access to subprogram
6791
6792 P : Node_Id;
6793
6794 begin
6795 Set_Is_Internal (Anon);
6796
6797 case Nkind (N) is
6798 when N_Constrained_Array_Definition
6799 | N_Component_Declaration
6800 | N_Unconstrained_Array_Definition
6801 =>
6802 Comp := Component_Definition (N);
6803 Acc := Access_Definition (Comp);
6804
6805 when N_Discriminant_Specification =>
6806 Comp := Discriminant_Type (N);
6807 Acc := Comp;
6808
6809 when N_Parameter_Specification =>
6810 Comp := Parameter_Type (N);
6811 Acc := Comp;
6812
6813 when N_Access_Function_Definition =>
6814 Comp := Result_Definition (N);
6815 Acc := Comp;
6816
6817 when N_Object_Declaration =>
6818 Comp := Object_Definition (N);
6819 Acc := Comp;
6820
6821 when N_Function_Specification =>
6822 Comp := Result_Definition (N);
6823 Acc := Comp;
6824
6825 when others =>
6826 raise Program_Error;
6827 end case;
6828
6829 Spec := Access_To_Subprogram_Definition (Acc);
6830
6831 Decl :=
6832 Make_Full_Type_Declaration (Loc,
6833 Defining_Identifier => Anon,
6834 Type_Definition => Copy_Separate_Tree (Spec));
6835
6836 Mark_Rewrite_Insertion (Decl);
6837
6838 -- Insert the new declaration in the nearest enclosing scope. If the
6839 -- parent is a body and N is its return type, the declaration belongs
6840 -- in the enclosing scope. Likewise if N is the type of a parameter.
6841
6842 P := Parent (N);
6843
6844 if Nkind (N) = N_Function_Specification
6845 and then Nkind (P) = N_Subprogram_Body
6846 then
6847 P := Parent (P);
6848 elsif Nkind (N) = N_Parameter_Specification
6849 and then Nkind (P) in N_Subprogram_Specification
6850 and then Nkind (Parent (P)) = N_Subprogram_Body
6851 then
6852 P := Parent (Parent (P));
6853 end if;
6854
6855 while Present (P) and then not Has_Declarations (P) loop
6856 P := Parent (P);
6857 end loop;
6858
6859 pragma Assert (Present (P));
6860
6861 if Nkind (P) = N_Package_Specification then
6862 Prepend (Decl, Visible_Declarations (P));
6863 else
6864 Prepend (Decl, Declarations (P));
6865 end if;
6866
6867 -- Replace the anonymous type with an occurrence of the new declaration.
6868 -- In all cases the rewritten node does not have the null-exclusion
6869 -- attribute because (if present) it was already inherited by the
6870 -- anonymous entity (Anon). Thus, in case of components we do not
6871 -- inherit this attribute.
6872
6873 if Nkind (N) = N_Parameter_Specification then
6874 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6875 Set_Etype (Defining_Identifier (N), Anon);
6876 Set_Null_Exclusion_Present (N, False);
6877
6878 elsif Nkind (N) = N_Object_Declaration then
6879 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6880 Set_Etype (Defining_Identifier (N), Anon);
6881
6882 elsif Nkind (N) = N_Access_Function_Definition then
6883 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6884
6885 elsif Nkind (N) = N_Function_Specification then
6886 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6887 Set_Etype (Defining_Unit_Name (N), Anon);
6888
6889 else
6890 Rewrite (Comp,
6891 Make_Component_Definition (Loc,
6892 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6893 end if;
6894
6895 Mark_Rewrite_Insertion (Comp);
6896
6897 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6898 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6899 and then not Is_Type (Current_Scope))
6900 then
6901
6902 -- Declaration can be analyzed in the current scope.
6903
6904 Analyze (Decl);
6905
6906 else
6907 -- Temporarily remove the current scope (record or subprogram) from
6908 -- the stack to add the new declarations to the enclosing scope.
6909 -- The anonymous entity is an Itype with the proper attributes.
6910
6911 Scope_Stack.Decrement_Last;
6912 Analyze (Decl);
6913 Set_Is_Itype (Anon);
6914 Set_Associated_Node_For_Itype (Anon, N);
6915 Scope_Stack.Append (Curr_Scope);
6916 end if;
6917
6918 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6919 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6920 return Anon;
6921 end Replace_Anonymous_Access_To_Protected_Subprogram;
6922
6923 -------------------------------------
6924 -- Build_Access_Subprogram_Wrapper --
6925 -------------------------------------
6926
6927 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6928 Id : constant Entity_Id := Defining_Identifier (Decl);
6929 Loc : constant Source_Ptr := Sloc (Decl);
6930 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6931 Type_Def : constant Node_Id := Type_Definition (Decl);
6932 Specs : constant List_Id := Parameter_Specifications (Type_Def);
6933
6934 function Build_Access_Subprogram_Wrapper_Declaration return Node_Id;
6935 -- Build the declaration and the specification of the wrapper
6936
6937 -------------------------------------------------
6938 -- Build_Access_Subprogram_Wrapper_Declaration --
6939 -------------------------------------------------
6940
6941 function Build_Access_Subprogram_Wrapper_Declaration return Node_Id is
6942 Form_P : Node_Id;
6943 New_Decl : Node_Id;
6944 New_P : Node_Id;
6945 Profile : constant List_Id := New_List;
6946 Spec : Node_Id;
6947
6948 begin
6949 Form_P := First (Specs);
6950
6951 while Present (Form_P) loop
6952 New_P := New_Copy_Tree (Form_P);
6953 Set_Defining_Identifier (New_P,
6954 Make_Defining_Identifier
6955 (Loc, Chars (Defining_Identifier (Form_P))));
6956 Append (New_P, Profile);
6957 Next (Form_P);
6958 end loop;
6959
6960 -- Add to parameter specifications the access parameter that is
6961 -- passed in from an indirect call.
6962
6963 Append (
6964 Make_Parameter_Specification (Loc,
6965 Defining_Identifier => Make_Temporary (Loc, 'P'),
6966 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6967 Profile);
6968
6969 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6970 Spec :=
6971 Make_Procedure_Specification (Loc,
6972 Defining_Unit_Name => Subp,
6973 Parameter_Specifications => Profile);
6974 Mutate_Ekind (Subp, E_Procedure);
6975 else
6976 Spec :=
6977 Make_Function_Specification (Loc,
6978 Defining_Unit_Name => Subp,
6979 Parameter_Specifications => Profile,
6980 Result_Definition =>
6981 New_Copy_Tree (Result_Definition (Type_Def)));
6982 Mutate_Ekind (Subp, E_Function);
6983 end if;
6984
6985 New_Decl :=
6986 Make_Subprogram_Declaration (Loc, Specification => Spec);
6987
6988 Set_Is_Wrapper (Subp);
6989
6990 -- The wrapper is declared in the freezing actions to facilitate its
6991 -- identification and thus avoid handling it as a primitive operation
6992 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise
6993 -- it may be handled as a dispatching operation and erroneously
6994 -- registered in a dispatch table.
6995
6996 Append_Freeze_Action (Id, New_Decl);
6997
6998 return New_Decl;
6999 end Build_Access_Subprogram_Wrapper_Declaration;
7000
7001 procedure Replace_Type_Name (Expr : Node_Id);
7002 -- In the expressions for contract aspects, replace occurrences of the
7003 -- access type with the name of the subprogram entity, as needed, e.g.
7004 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
7005 -- remain on the original access type declaration. What about expanded
7006 -- names denoting formals, whose prefix in source is the type name ???
7007
7008 -----------------------
7009 -- Replace_Type_Name --
7010 -----------------------
7011
7012 procedure Replace_Type_Name (Expr : Node_Id) is
7013 function Process (N : Node_Id) return Traverse_Result;
7014 function Process (N : Node_Id) return Traverse_Result is
7015 begin
7016 if Nkind (N) = N_Attribute_Reference
7017 and then Is_Entity_Name (Prefix (N))
7018 and then Chars (Prefix (N)) = Chars (Id)
7019 then
7020 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
7021 end if;
7022
7023 return OK;
7024 end Process;
7025
7026 procedure Traverse is new Traverse_Proc (Process);
7027 begin
7028 Traverse (Expr);
7029 end Replace_Type_Name;
7030
7031 -- Local variables
7032
7033 Has_Wrapper : constant Boolean :=
7034 Present
7035 (Access_Subprogram_Wrapper (Designated_Type (Id)));
7036 Contracts : List_Id := No_List;
7037 Wrapper_Decl : Node_Id;
7038 Pragmas : List_Id := No_List;
7039
7040 -- Start of processing for Build_Access_Subprogram_Wrapper
7041
7042 begin
7043 if Ekind (Id) in E_Access_Subprogram_Type
7044 | E_Access_Protected_Subprogram_Type
7045 | E_Anonymous_Access_Protected_Subprogram_Type
7046 | E_Anonymous_Access_Subprogram_Type
7047 then
7048 null;
7049
7050 else
7051 Error_Msg_N
7052 ("illegal pre/postcondition on access type", Decl);
7053 return;
7054 end if;
7055
7056 -- Current state: We are processing the full-type declaration of
7057 -- this access-to-subprogram type. Collect its pre/postconditions
7058 -- and replace occurrences of the access type with the name of the
7059 -- subprogram entity.
7060
7061 if not Is_Frozen (Id) then
7062 if Present (Aspect_Specifications (Decl)) then
7063 declare
7064 Asp : Node_Id;
7065 A_Id : Aspect_Id;
7066 New_Contract : Node_Id;
7067
7068 begin
7069 Asp := First (Aspect_Specifications (Decl));
7070 while Present (Asp) loop
7071 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
7072
7073 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
7074 New_Contract := New_Copy_Tree (Asp);
7075 Append_New (New_Contract, Contracts);
7076 Replace_Type_Name (Expression (New_Contract));
7077 end if;
7078
7079 Next (Asp);
7080 end loop;
7081 end;
7082 end if;
7083
7084 -- No wrapper is needed if there are no contract aspects
7085
7086 if Is_Empty_List (Contracts) then
7087 return;
7088 end if;
7089
7090 -- Build the wrapper declaration, propagate the aspects, and link
7091 -- the wrapper with its access-type declaration.
7092
7093 Wrapper_Decl := Build_Access_Subprogram_Wrapper_Declaration;
7094 Set_Aspect_Specifications (Wrapper_Decl, Contracts);
7095 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
7096
7097 -- Build the body of this wrapper
7098
7099 Build_Access_Subprogram_Wrapper_Body (Decl, Wrapper_Decl);
7100
7101 -- Current status: We are freezing the access-to-subprogram type entity.
7102 -- Collect its pragmas pre/postconditions that come from the sources,
7103 -- and replace occurrences of the access type with the name of the
7104 -- subprogram entity.
7105
7106 else
7107 if Present (Contract (Designated_Type (Id))) then
7108 declare
7109 Prag : Node_Id;
7110 New_Pragma : Node_Id;
7111
7112 begin
7113 Prag := Pre_Post_Conditions (Contract (Designated_Type (Id)));
7114
7115 while Present (Prag) loop
7116 if Comes_From_Source (Prag) then
7117 New_Pragma := New_Copy_Tree (Prag);
7118 Append_New (New_Pragma, Pragmas);
7119 Replace_Type_Name
7120 (First (Pragma_Argument_Associations (New_Pragma)));
7121
7122 -- Force reanalyzing the copy since it will be applied to
7123 -- the wrapper.
7124
7125 Set_Analyzed (New_Pragma, False);
7126 end if;
7127
7128 Prag := Next_Pragma (Prag);
7129 end loop;
7130 end;
7131
7132 -- No wrapper is needed if there are no pre/postcondition pragmas
7133
7134 if Is_Empty_List (Pragmas) then
7135 return;
7136 end if;
7137
7138 -- Build the wrapper, if not previously done, and propagate the
7139 -- pragmas to the wrapper spec. The access-to-subprogram type
7140 -- declaration might have a precondition aspect and a
7141 -- postcondition pragma, or vice versa. In such cases,
7142 -- Build_Access_Subprogram_Wrapper is called twice: (1) when
7143 -- processing the full-type declaration (building the wrapper
7144 -- with the aspect), and (2) when freezing the type (adding the
7145 -- pragmas after the spec of the wrapper).
7146
7147 if not Has_Wrapper then
7148 Wrapper_Decl := Build_Access_Subprogram_Wrapper_Declaration;
7149 Insert_List_After (Wrapper_Decl, Pragmas);
7150
7151 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
7152 Build_Access_Subprogram_Wrapper_Body (Decl, Wrapper_Decl);
7153 else
7154 Wrapper_Decl :=
7155 Parent
7156 (Parent (Access_Subprogram_Wrapper (Designated_Type (Id))));
7157 Insert_List_After (Wrapper_Decl, Pragmas);
7158 end if;
7159 end if;
7160 end if;
7161 end Build_Access_Subprogram_Wrapper;
7162
7163 -------------------------------
7164 -- Build_Derived_Access_Type --
7165 -------------------------------
7166
7167 procedure Build_Derived_Access_Type
7168 (N : Node_Id;
7169 Parent_Type : Entity_Id;
7170 Derived_Type : Entity_Id)
7171 is
7172 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
7173
7174 Desig_Type : Entity_Id;
7175 Discr : Entity_Id;
7176 Discr_Con_Elist : Elist_Id;
7177 Discr_Con_El : Elmt_Id;
7178 Subt : Entity_Id;
7179
7180 begin
7181 -- Set the designated type so it is available in case this is an access
7182 -- to a self-referential type, e.g. a standard list type with a next
7183 -- pointer. Will be reset after subtype is built.
7184
7185 Set_Directly_Designated_Type
7186 (Derived_Type, Designated_Type (Parent_Type));
7187
7188 Subt := Process_Subtype (S, N);
7189
7190 if Nkind (S) /= N_Subtype_Indication
7191 and then Subt /= Base_Type (Subt)
7192 then
7193 Mutate_Ekind (Derived_Type, E_Access_Subtype);
7194 end if;
7195
7196 if Ekind (Derived_Type) = E_Access_Subtype then
7197 declare
7198 Pbase : constant Entity_Id := Base_Type (Parent_Type);
7199 Ibase : constant Entity_Id :=
7200 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
7201 Svg_Chars : constant Name_Id := Chars (Ibase);
7202 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
7203 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
7204
7205 begin
7206 Copy_Node (Pbase, Ibase);
7207
7208 -- Restore Itype status after Copy_Node
7209
7210 Set_Is_Itype (Ibase);
7211 Set_Associated_Node_For_Itype (Ibase, N);
7212
7213 Set_Chars (Ibase, Svg_Chars);
7214 Set_Prev_Entity (Ibase, Svg_Prev_E);
7215 Set_Next_Entity (Ibase, Svg_Next_E);
7216 Set_Sloc (Ibase, Sloc (Derived_Type));
7217 Set_Scope (Ibase, Scope (Derived_Type));
7218 Set_Freeze_Node (Ibase, Empty);
7219 Set_Is_Frozen (Ibase, False);
7220 Set_Comes_From_Source (Ibase, False);
7221 Set_Is_First_Subtype (Ibase, False);
7222
7223 Set_Etype (Ibase, Pbase);
7224 Set_Etype (Derived_Type, Ibase);
7225 end;
7226 end if;
7227
7228 Set_Directly_Designated_Type
7229 (Derived_Type, Designated_Type (Subt));
7230
7231 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7232 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7233 Set_Size_Info (Derived_Type, Parent_Type);
7234 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7235 Set_Depends_On_Private (Derived_Type,
7236 Has_Private_Component (Derived_Type));
7237 Conditional_Delay (Derived_Type, Subt);
7238
7239 if Is_Access_Subprogram_Type (Derived_Type)
7240 and then Is_Base_Type (Derived_Type)
7241 then
7242 Set_Can_Use_Internal_Rep
7243 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7244 end if;
7245
7246 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7247 -- that it is not redundant.
7248
7249 if Null_Exclusion_Present (Type_Definition (N)) then
7250 Set_Can_Never_Be_Null (Derived_Type);
7251
7252 elsif Can_Never_Be_Null (Parent_Type) then
7253 Set_Can_Never_Be_Null (Derived_Type);
7254 end if;
7255
7256 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7257 -- the root type for this information.
7258
7259 -- Apply range checks to discriminants for derived record case
7260 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7261
7262 Desig_Type := Designated_Type (Derived_Type);
7263
7264 if Is_Composite_Type (Desig_Type)
7265 and then not Is_Array_Type (Desig_Type)
7266 and then Has_Discriminants (Desig_Type)
7267 and then Base_Type (Desig_Type) /= Desig_Type
7268 then
7269 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7270 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7271
7272 Discr := First_Discriminant (Base_Type (Desig_Type));
7273 while Present (Discr_Con_El) loop
7274 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7275 Next_Elmt (Discr_Con_El);
7276 Next_Discriminant (Discr);
7277 end loop;
7278 end if;
7279 end Build_Derived_Access_Type;
7280
7281 ------------------------------
7282 -- Build_Derived_Array_Type --
7283 ------------------------------
7284
7285 procedure Build_Derived_Array_Type
7286 (N : Node_Id;
7287 Parent_Type : Entity_Id;
7288 Derived_Type : Entity_Id)
7289 is
7290 Loc : constant Source_Ptr := Sloc (N);
7291 Tdef : constant Node_Id := Type_Definition (N);
7292 Indic : constant Node_Id := Subtype_Indication (Tdef);
7293 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7294 Implicit_Base : Entity_Id := Empty;
7295 New_Indic : Node_Id;
7296
7297 procedure Make_Implicit_Base;
7298 -- If the parent subtype is constrained, the derived type is a subtype
7299 -- of an implicit base type derived from the parent base.
7300
7301 ------------------------
7302 -- Make_Implicit_Base --
7303 ------------------------
7304
7305 procedure Make_Implicit_Base is
7306 begin
7307 Implicit_Base :=
7308 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7309
7310 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7311 Set_Etype (Implicit_Base, Parent_Base);
7312
7313 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7314 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7315
7316 Set_Has_Delayed_Freeze (Implicit_Base, True);
7317 end Make_Implicit_Base;
7318
7319 -- Start of processing for Build_Derived_Array_Type
7320
7321 begin
7322 if not Is_Constrained (Parent_Type) then
7323 if Nkind (Indic) /= N_Subtype_Indication then
7324 Mutate_Ekind (Derived_Type, E_Array_Type);
7325
7326 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7327 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7328
7329 Set_Has_Delayed_Freeze (Derived_Type, True);
7330
7331 else
7332 Make_Implicit_Base;
7333 Set_Etype (Derived_Type, Implicit_Base);
7334
7335 New_Indic :=
7336 Make_Subtype_Declaration (Loc,
7337 Defining_Identifier => Derived_Type,
7338 Subtype_Indication =>
7339 Make_Subtype_Indication (Loc,
7340 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7341 Constraint => Constraint (Indic)));
7342
7343 Rewrite (N, New_Indic);
7344
7345 -- Keep the aspects from the original node
7346
7347 Move_Aspects (Original_Node (N), N);
7348
7349 Analyze (N);
7350 end if;
7351
7352 else
7353 if Nkind (Indic) /= N_Subtype_Indication then
7354 Make_Implicit_Base;
7355
7356 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7357 Set_Etype (Derived_Type, Implicit_Base);
7358 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7359
7360 else
7361 Error_Msg_N ("illegal constraint on constrained type", Indic);
7362 end if;
7363 end if;
7364
7365 -- If parent type is not a derived type itself, and is declared in
7366 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7367 -- the new type's concatenation operator since Derive_Subprograms
7368 -- will not inherit the parent's operator. If the parent type is
7369 -- unconstrained, the operator is of the unconstrained base type.
7370
7371 if Number_Dimensions (Parent_Type) = 1
7372 and then not Is_Limited_Type (Parent_Type)
7373 and then not Is_Derived_Type (Parent_Type)
7374 and then not Is_Package_Or_Generic_Package
7375 (Scope (Base_Type (Parent_Type)))
7376 then
7377 if not Is_Constrained (Parent_Type)
7378 and then Is_Constrained (Derived_Type)
7379 then
7380 New_Concatenation_Op (Implicit_Base);
7381 else
7382 New_Concatenation_Op (Derived_Type);
7383 end if;
7384 end if;
7385 end Build_Derived_Array_Type;
7386
7387 -----------------------------------
7388 -- Build_Derived_Concurrent_Type --
7389 -----------------------------------
7390
7391 procedure Build_Derived_Concurrent_Type
7392 (N : Node_Id;
7393 Parent_Type : Entity_Id;
7394 Derived_Type : Entity_Id)
7395 is
7396 Loc : constant Source_Ptr := Sloc (N);
7397 Def : constant Node_Id := Type_Definition (N);
7398 Indic : constant Node_Id := Subtype_Indication (Def);
7399
7400 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7401 Corr_Decl : Node_Id := Empty;
7402 Corr_Decl_Needed : Boolean;
7403 -- If the derived type has fewer discriminants than its parent, the
7404 -- corresponding record is also a derived type, in order to account for
7405 -- the bound discriminants. We create a full type declaration for it in
7406 -- this case.
7407
7408 Constraint_Present : constant Boolean :=
7409 Nkind (Indic) = N_Subtype_Indication;
7410
7411 D_Constraint : Node_Id;
7412 New_Constraint : Elist_Id := No_Elist;
7413 Old_Disc : Entity_Id;
7414 New_Disc : Entity_Id;
7415 New_N : Node_Id;
7416
7417 begin
7418 Set_Stored_Constraint (Derived_Type, No_Elist);
7419 Corr_Decl_Needed := False;
7420 Old_Disc := Empty;
7421
7422 if Present (Discriminant_Specifications (N))
7423 and then Constraint_Present
7424 then
7425 Old_Disc := First_Discriminant (Parent_Type);
7426 New_Disc := First (Discriminant_Specifications (N));
7427 while Present (New_Disc) and then Present (Old_Disc) loop
7428 Next_Discriminant (Old_Disc);
7429 Next (New_Disc);
7430 end loop;
7431 end if;
7432
7433 if Present (Old_Disc) and then Expander_Active then
7434
7435 -- The new type has fewer discriminants, so we need to create a new
7436 -- corresponding record, which is derived from the corresponding
7437 -- record of the parent, and has a stored constraint that captures
7438 -- the values of the discriminant constraints. The corresponding
7439 -- record is needed only if expander is active and code generation is
7440 -- enabled.
7441
7442 -- The type declaration for the derived corresponding record has the
7443 -- same discriminant part and constraints as the current declaration.
7444 -- Copy the unanalyzed tree to build declaration.
7445
7446 Corr_Decl_Needed := True;
7447 New_N := Copy_Separate_Tree (N);
7448
7449 Corr_Decl :=
7450 Make_Full_Type_Declaration (Loc,
7451 Defining_Identifier => Corr_Record,
7452 Discriminant_Specifications =>
7453 Discriminant_Specifications (New_N),
7454 Type_Definition =>
7455 Make_Derived_Type_Definition (Loc,
7456 Subtype_Indication =>
7457 Make_Subtype_Indication (Loc,
7458 Subtype_Mark =>
7459 New_Occurrence_Of
7460 (Corresponding_Record_Type (Parent_Type), Loc),
7461 Constraint =>
7462 Constraint
7463 (Subtype_Indication (Type_Definition (New_N))))));
7464 end if;
7465
7466 -- Copy Storage_Size and Relative_Deadline variables if task case
7467
7468 if Is_Task_Type (Parent_Type) then
7469 Set_Storage_Size_Variable (Derived_Type,
7470 Storage_Size_Variable (Parent_Type));
7471 Set_Relative_Deadline_Variable (Derived_Type,
7472 Relative_Deadline_Variable (Parent_Type));
7473 end if;
7474
7475 if Present (Discriminant_Specifications (N)) then
7476 Push_Scope (Derived_Type);
7477 Check_Or_Process_Discriminants (N, Derived_Type);
7478
7479 if Constraint_Present then
7480 New_Constraint :=
7481 Expand_To_Stored_Constraint
7482 (Parent_Type,
7483 Build_Discriminant_Constraints
7484 (Parent_Type, Indic, True));
7485 end if;
7486
7487 End_Scope;
7488
7489 elsif Constraint_Present then
7490
7491 -- Build an unconstrained derived type and rewrite the derived type
7492 -- as a subtype of this new base type.
7493
7494 declare
7495 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7496 New_Base : Entity_Id;
7497 New_Decl : Node_Id;
7498 New_Indic : Node_Id;
7499
7500 begin
7501 New_Base :=
7502 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7503
7504 New_Decl :=
7505 Make_Full_Type_Declaration (Loc,
7506 Defining_Identifier => New_Base,
7507 Type_Definition =>
7508 Make_Derived_Type_Definition (Loc,
7509 Abstract_Present => Abstract_Present (Def),
7510 Limited_Present => Limited_Present (Def),
7511 Subtype_Indication =>
7512 New_Occurrence_Of (Parent_Base, Loc)));
7513
7514 Mark_Rewrite_Insertion (New_Decl);
7515 Insert_Before (N, New_Decl);
7516 Analyze (New_Decl);
7517
7518 New_Indic :=
7519 Make_Subtype_Indication (Loc,
7520 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7521 Constraint => Relocate_Node (Constraint (Indic)));
7522
7523 Rewrite (N,
7524 Make_Subtype_Declaration (Loc,
7525 Defining_Identifier => Derived_Type,
7526 Subtype_Indication => New_Indic));
7527
7528 -- Keep the aspects from the original node
7529
7530 Move_Aspects (Original_Node (N), N);
7531
7532 Analyze (N);
7533 return;
7534 end;
7535 end if;
7536
7537 -- By default, operations and private data are inherited from parent.
7538 -- However, in the presence of bound discriminants, a new corresponding
7539 -- record will be created, see below.
7540
7541 Set_Has_Discriminants
7542 (Derived_Type, Has_Discriminants (Parent_Type));
7543 Set_Corresponding_Record_Type
7544 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7545
7546 -- Is_Constrained is set according the parent subtype, but is set to
7547 -- False if the derived type is declared with new discriminants.
7548
7549 Set_Is_Constrained
7550 (Derived_Type,
7551 (Is_Constrained (Parent_Type) or else Constraint_Present)
7552 and then No (Discriminant_Specifications (N)));
7553
7554 if Constraint_Present then
7555 if not Has_Discriminants (Parent_Type) then
7556 Error_Msg_N ("untagged parent must have discriminants", N);
7557
7558 elsif Present (Discriminant_Specifications (N)) then
7559
7560 -- Verify that new discriminants are used to constrain old ones
7561
7562 D_Constraint := First (Constraints (Constraint (Indic)));
7563
7564 Old_Disc := First_Discriminant (Parent_Type);
7565
7566 while Present (D_Constraint) loop
7567 if Nkind (D_Constraint) /= N_Discriminant_Association then
7568
7569 -- Positional constraint. If it is a reference to a new
7570 -- discriminant, it constrains the corresponding old one.
7571
7572 if Nkind (D_Constraint) = N_Identifier then
7573 New_Disc := First_Discriminant (Derived_Type);
7574 while Present (New_Disc) loop
7575 exit when Chars (New_Disc) = Chars (D_Constraint);
7576 Next_Discriminant (New_Disc);
7577 end loop;
7578
7579 if Present (New_Disc) then
7580 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7581 end if;
7582 end if;
7583
7584 Next_Discriminant (Old_Disc);
7585
7586 -- if this is a named constraint, search by name for the old
7587 -- discriminants constrained by the new one.
7588
7589 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7590
7591 -- Find new discriminant with that name
7592
7593 New_Disc := First_Discriminant (Derived_Type);
7594 while Present (New_Disc) loop
7595 exit when
7596 Chars (New_Disc) = Chars (Expression (D_Constraint));
7597 Next_Discriminant (New_Disc);
7598 end loop;
7599
7600 if Present (New_Disc) then
7601
7602 -- Verify that new discriminant renames some discriminant
7603 -- of the parent type, and associate the new discriminant
7604 -- with one or more old ones that it renames.
7605
7606 declare
7607 Selector : Node_Id;
7608
7609 begin
7610 Selector := First (Selector_Names (D_Constraint));
7611 while Present (Selector) loop
7612 Old_Disc := First_Discriminant (Parent_Type);
7613 while Present (Old_Disc) loop
7614 exit when Chars (Old_Disc) = Chars (Selector);
7615 Next_Discriminant (Old_Disc);
7616 end loop;
7617
7618 if Present (Old_Disc) then
7619 Set_Corresponding_Discriminant
7620 (New_Disc, Old_Disc);
7621 end if;
7622
7623 Next (Selector);
7624 end loop;
7625 end;
7626 end if;
7627 end if;
7628
7629 Next (D_Constraint);
7630 end loop;
7631
7632 New_Disc := First_Discriminant (Derived_Type);
7633 while Present (New_Disc) loop
7634 if No (Corresponding_Discriminant (New_Disc)) then
7635 Error_Msg_NE
7636 ("new discriminant& must constrain old one", N, New_Disc);
7637
7638 -- If a new discriminant is used in the constraint, then its
7639 -- subtype must be statically compatible with the subtype of
7640 -- the parent discriminant (RM 3.7(15)).
7641
7642 else
7643 Check_Constraining_Discriminant
7644 (New_Disc, Corresponding_Discriminant (New_Disc));
7645 end if;
7646
7647 Next_Discriminant (New_Disc);
7648 end loop;
7649 end if;
7650
7651 elsif Present (Discriminant_Specifications (N)) then
7652 Error_Msg_N
7653 ("missing discriminant constraint in untagged derivation", N);
7654 end if;
7655
7656 -- The entity chain of the derived type includes the new discriminants
7657 -- but shares operations with the parent.
7658
7659 if Present (Discriminant_Specifications (N)) then
7660 Old_Disc := First_Discriminant (Parent_Type);
7661 while Present (Old_Disc) loop
7662 if No (Next_Entity (Old_Disc))
7663 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7664 then
7665 Link_Entities
7666 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7667 exit;
7668 end if;
7669
7670 Next_Discriminant (Old_Disc);
7671 end loop;
7672
7673 else
7674 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7675 if Has_Discriminants (Parent_Type) then
7676 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7677 Set_Discriminant_Constraint (
7678 Derived_Type, Discriminant_Constraint (Parent_Type));
7679 end if;
7680 end if;
7681
7682 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7683
7684 Set_Has_Completion (Derived_Type);
7685
7686 if Corr_Decl_Needed then
7687 Set_Stored_Constraint (Derived_Type, New_Constraint);
7688 Insert_After (N, Corr_Decl);
7689 Analyze (Corr_Decl);
7690 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7691 end if;
7692 end Build_Derived_Concurrent_Type;
7693
7694 ------------------------------------
7695 -- Build_Derived_Enumeration_Type --
7696 ------------------------------------
7697
7698 procedure Build_Derived_Enumeration_Type
7699 (N : Node_Id;
7700 Parent_Type : Entity_Id;
7701 Derived_Type : Entity_Id)
7702 is
7703 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7704 -- When the type declaration includes a constraint, we generate
7705 -- a subtype declaration of an anonymous base type, with the constraint
7706 -- given in the original type declaration. Conceptually, the bounds
7707 -- are converted to the new base type, and this conversion freezes
7708 -- (prematurely) that base type, when the bounds are simply literals.
7709 -- As a result, a representation clause for the derived type is then
7710 -- rejected or ignored. This procedure recognizes the simple case of
7711 -- literal bounds, which allows us to indicate that the conversions
7712 -- are not freeze points, and the subsequent representation clause
7713 -- can be accepted.
7714 -- A similar approach might be used to resolve the long-standing
7715 -- problem of premature freezing of derived numeric types ???
7716
7717 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7718 begin
7719 return Nkind (B) = N_Type_Conversion
7720 and then Is_Entity_Name (Expression (B))
7721 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7722 end Bound_Belongs_To_Type;
7723
7724 Loc : constant Source_Ptr := Sloc (N);
7725 Def : constant Node_Id := Type_Definition (N);
7726 Indic : constant Node_Id := Subtype_Indication (Def);
7727 Implicit_Base : Entity_Id;
7728 Literal : Entity_Id;
7729 New_Lit : Entity_Id;
7730 Literals_List : List_Id;
7731 Type_Decl : Node_Id;
7732 Hi, Lo : Node_Id;
7733 Rang_Expr : Node_Id;
7734
7735 begin
7736 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7737 -- not have explicit literals lists we need to process types derived
7738 -- from them specially. This is handled by Derived_Standard_Character.
7739 -- If the parent type is a generic type, there are no literals either,
7740 -- and we construct the same skeletal representation as for the generic
7741 -- parent type.
7742
7743 if Is_Standard_Character_Type (Parent_Type) then
7744 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7745
7746 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7747 declare
7748 Lo : Node_Id;
7749 Hi : Node_Id;
7750
7751 begin
7752 if Nkind (Indic) /= N_Subtype_Indication then
7753 Lo :=
7754 Make_Attribute_Reference (Loc,
7755 Attribute_Name => Name_First,
7756 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7757 Set_Etype (Lo, Derived_Type);
7758
7759 Hi :=
7760 Make_Attribute_Reference (Loc,
7761 Attribute_Name => Name_Last,
7762 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7763 Set_Etype (Hi, Derived_Type);
7764
7765 Set_Scalar_Range (Derived_Type,
7766 Make_Range (Loc,
7767 Low_Bound => Lo,
7768 High_Bound => Hi));
7769 else
7770
7771 -- Analyze subtype indication and verify compatibility
7772 -- with parent type.
7773
7774 if Base_Type (Process_Subtype (Indic, N)) /=
7775 Base_Type (Parent_Type)
7776 then
7777 Error_Msg_N
7778 ("illegal constraint for formal discrete type", N);
7779 end if;
7780 end if;
7781 end;
7782
7783 else
7784 -- If a constraint is present, analyze the bounds to catch
7785 -- premature usage of the derived literals.
7786
7787 if Nkind (Indic) = N_Subtype_Indication
7788 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7789 then
7790 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7791 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7792 end if;
7793
7794 -- Create an implicit base type for the derived type even if there
7795 -- is no constraint attached to it, since this seems closer to the
7796 -- Ada semantics. Use an Itype like for the implicit base type of
7797 -- other kinds of derived type, but build a full type declaration
7798 -- for it so as to analyze the new literals properly. Then build a
7799 -- subtype declaration tree which applies the constraint (if any)
7800 -- and have it replace the derived type declaration.
7801
7802 Literal := First_Literal (Parent_Type);
7803 Literals_List := New_List;
7804 while Present (Literal)
7805 and then Ekind (Literal) = E_Enumeration_Literal
7806 loop
7807 -- Literals of the derived type have the same representation as
7808 -- those of the parent type, but this representation can be
7809 -- overridden by an explicit representation clause. Indicate
7810 -- that there is no explicit representation given yet. These
7811 -- derived literals are implicit operations of the new type,
7812 -- and can be overridden by explicit ones.
7813
7814 if Nkind (Literal) = N_Defining_Character_Literal then
7815 New_Lit :=
7816 Make_Defining_Character_Literal (Loc, Chars (Literal));
7817 else
7818 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7819 end if;
7820
7821 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7822 Set_Is_Not_Self_Hidden (New_Lit);
7823 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7824 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7825 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7826 Set_Alias (New_Lit, Literal);
7827 Set_Is_Known_Valid (New_Lit, True);
7828
7829 Append (New_Lit, Literals_List);
7830 Next_Literal (Literal);
7831 end loop;
7832
7833 Implicit_Base :=
7834 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7835
7836 -- Indicate the proper nature of the derived type. This must be done
7837 -- before analysis of the literals, to recognize cases when a literal
7838 -- may be hidden by a previous explicit function definition (cf.
7839 -- c83031a).
7840
7841 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7842 Set_Etype (Derived_Type, Implicit_Base);
7843
7844 Type_Decl :=
7845 Make_Full_Type_Declaration (Loc,
7846 Defining_Identifier => Implicit_Base,
7847 Type_Definition =>
7848 Make_Enumeration_Type_Definition (Loc, Literals_List));
7849
7850 -- Do not insert the declarationn, just analyze it in the context
7851
7852 Set_Parent (Type_Decl, Parent (N));
7853 Analyze (Type_Decl);
7854
7855 -- The anonymous base now has a full declaration, but this base
7856 -- is not a first subtype.
7857
7858 Set_Is_First_Subtype (Implicit_Base, False);
7859
7860 -- After the implicit base is analyzed its Etype needs to be changed
7861 -- to reflect the fact that it is derived from the parent type which
7862 -- was ignored during analysis. We also set the size at this point.
7863
7864 Set_Etype (Implicit_Base, Parent_Type);
7865
7866 Set_Size_Info (Implicit_Base, Parent_Type);
7867 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7868 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7869
7870 -- Copy other flags from parent type
7871
7872 Set_Has_Non_Standard_Rep
7873 (Implicit_Base, Has_Non_Standard_Rep
7874 (Parent_Type));
7875 Set_Has_Pragma_Ordered
7876 (Implicit_Base, Has_Pragma_Ordered
7877 (Parent_Type));
7878 Set_Has_Delayed_Freeze (Implicit_Base);
7879
7880 -- Process the subtype indication including a validation check on the
7881 -- constraint, if any. If a constraint is given, its bounds must be
7882 -- implicitly converted to the new type.
7883
7884 if Nkind (Indic) = N_Subtype_Indication then
7885 declare
7886 R : constant Node_Id :=
7887 Range_Expression (Constraint (Indic));
7888
7889 begin
7890 if Nkind (R) = N_Range then
7891 Hi := Build_Scalar_Bound
7892 (High_Bound (R), Parent_Type, Implicit_Base);
7893 Lo := Build_Scalar_Bound
7894 (Low_Bound (R), Parent_Type, Implicit_Base);
7895
7896 else
7897 -- Constraint is a Range attribute. Replace with explicit
7898 -- mention of the bounds of the prefix, which must be a
7899 -- subtype.
7900
7901 Analyze (Prefix (R));
7902 Hi :=
7903 Convert_To (Implicit_Base,
7904 Make_Attribute_Reference (Loc,
7905 Attribute_Name => Name_Last,
7906 Prefix =>
7907 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7908
7909 Lo :=
7910 Convert_To (Implicit_Base,
7911 Make_Attribute_Reference (Loc,
7912 Attribute_Name => Name_First,
7913 Prefix =>
7914 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7915 end if;
7916 end;
7917
7918 else
7919 Hi :=
7920 Build_Scalar_Bound
7921 (Type_High_Bound (Parent_Type),
7922 Parent_Type, Implicit_Base);
7923 Lo :=
7924 Build_Scalar_Bound
7925 (Type_Low_Bound (Parent_Type),
7926 Parent_Type, Implicit_Base);
7927 end if;
7928
7929 Rang_Expr :=
7930 Make_Range (Loc,
7931 Low_Bound => Lo,
7932 High_Bound => Hi);
7933
7934 -- If we constructed a default range for the case where no range
7935 -- was given, then the expressions in the range must not freeze
7936 -- since they do not correspond to expressions in the source.
7937 -- However, if the type inherits predicates the expressions will
7938 -- be elaborated earlier and must freeze.
7939
7940 if (Nkind (Indic) /= N_Subtype_Indication
7941 or else
7942 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7943 and then not Has_Predicates (Derived_Type)
7944 then
7945 Set_Must_Not_Freeze (Lo);
7946 Set_Must_Not_Freeze (Hi);
7947 Set_Must_Not_Freeze (Rang_Expr);
7948 end if;
7949
7950 Rewrite (N,
7951 Make_Subtype_Declaration (Loc,
7952 Defining_Identifier => Derived_Type,
7953 Subtype_Indication =>
7954 Make_Subtype_Indication (Loc,
7955 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7956 Constraint =>
7957 Make_Range_Constraint (Loc,
7958 Range_Expression => Rang_Expr))));
7959
7960 -- Keep the aspects from the orignal node
7961
7962 Move_Aspects (Original_Node (N), N);
7963
7964 Analyze (N);
7965
7966 -- Propagate the aspects from the original type declaration to the
7967 -- declaration of the implicit base.
7968
7969 Copy_Aspects (From => N, To => Type_Decl);
7970
7971 -- Apply a range check. Since this range expression doesn't have an
7972 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7973 -- this right???
7974
7975 if Nkind (Indic) = N_Subtype_Indication then
7976 Apply_Range_Check
7977 (Range_Expression (Constraint (Indic)), Parent_Type,
7978 Source_Typ => Entity (Subtype_Mark (Indic)));
7979 end if;
7980 end if;
7981 end Build_Derived_Enumeration_Type;
7982
7983 --------------------------------
7984 -- Build_Derived_Numeric_Type --
7985 --------------------------------
7986
7987 procedure Build_Derived_Numeric_Type
7988 (N : Node_Id;
7989 Parent_Type : Entity_Id;
7990 Derived_Type : Entity_Id)
7991 is
7992 Loc : constant Source_Ptr := Sloc (N);
7993 Tdef : constant Node_Id := Type_Definition (N);
7994 Indic : constant Node_Id := Subtype_Indication (Tdef);
7995 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7996 No_Constraint : constant Boolean := Nkind (Indic) /=
7997 N_Subtype_Indication;
7998 Implicit_Base : Entity_Id;
7999
8000 Lo : Node_Id;
8001 Hi : Node_Id;
8002
8003 begin
8004 -- Process the subtype indication including a validation check on
8005 -- the constraint if any.
8006
8007 Discard_Node (Process_Subtype (Indic, N));
8008
8009 -- Introduce an implicit base type for the derived type even if there
8010 -- is no constraint attached to it, since this seems closer to the Ada
8011 -- semantics.
8012
8013 Implicit_Base :=
8014 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
8015
8016 Set_Etype (Implicit_Base, Parent_Base);
8017 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
8018 Set_Size_Info (Implicit_Base, Parent_Base);
8019 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
8020 Set_Parent (Implicit_Base, Parent (Derived_Type));
8021 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
8022 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
8023
8024 -- Set RM Size for discrete type or decimal fixed-point type
8025 -- Ordinary fixed-point is excluded, why???
8026
8027 if Is_Discrete_Type (Parent_Base)
8028 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
8029 then
8030 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
8031 end if;
8032
8033 Set_Has_Delayed_Freeze (Implicit_Base);
8034
8035 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
8036 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
8037
8038 Set_Scalar_Range (Implicit_Base,
8039 Make_Range (Loc,
8040 Low_Bound => Lo,
8041 High_Bound => Hi));
8042
8043 if Has_Infinities (Parent_Base) then
8044 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
8045 end if;
8046
8047 -- The Derived_Type, which is the entity of the declaration, is a
8048 -- subtype of the implicit base. Its Ekind is a subtype, even in the
8049 -- absence of an explicit constraint.
8050
8051 Set_Etype (Derived_Type, Implicit_Base);
8052
8053 -- If we did not have a constraint, then the Ekind is set from the
8054 -- parent type (otherwise Process_Subtype has set the bounds)
8055
8056 if No_Constraint then
8057 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
8058 end if;
8059
8060 -- If we did not have a range constraint, then set the range from the
8061 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
8062
8063 if No_Constraint or else not Has_Range_Constraint (Indic) then
8064 Set_Scalar_Range (Derived_Type,
8065 Make_Range (Loc,
8066 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
8067 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
8068 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8069
8070 if Has_Infinities (Parent_Type) then
8071 Set_Includes_Infinities (Scalar_Range (Derived_Type));
8072 end if;
8073
8074 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
8075 end if;
8076
8077 Set_Is_Descendant_Of_Address (Derived_Type,
8078 Is_Descendant_Of_Address (Parent_Type));
8079 Set_Is_Descendant_Of_Address (Implicit_Base,
8080 Is_Descendant_Of_Address (Parent_Type));
8081
8082 -- Set remaining type-specific fields, depending on numeric type
8083
8084 if Is_Modular_Integer_Type (Parent_Type) then
8085 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
8086
8087 Set_Non_Binary_Modulus
8088 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
8089
8090 Set_Is_Known_Valid
8091 (Implicit_Base, Is_Known_Valid (Parent_Base));
8092
8093 elsif Is_Floating_Point_Type (Parent_Type) then
8094
8095 -- Digits of base type is always copied from the digits value of
8096 -- the parent base type, but the digits of the derived type will
8097 -- already have been set if there was a constraint present.
8098
8099 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
8100 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
8101
8102 if No_Constraint then
8103 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
8104 end if;
8105
8106 elsif Is_Fixed_Point_Type (Parent_Type) then
8107
8108 -- Small of base type and derived type are always copied from the
8109 -- parent base type, since smalls never change. The delta of the
8110 -- base type is also copied from the parent base type. However the
8111 -- delta of the derived type will have been set already if a
8112 -- constraint was present.
8113
8114 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
8115 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
8116 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
8117
8118 if No_Constraint then
8119 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
8120 end if;
8121
8122 -- The scale and machine radix in the decimal case are always
8123 -- copied from the parent base type.
8124
8125 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
8126 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
8127 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
8128
8129 Set_Machine_Radix_10
8130 (Derived_Type, Machine_Radix_10 (Parent_Base));
8131 Set_Machine_Radix_10
8132 (Implicit_Base, Machine_Radix_10 (Parent_Base));
8133
8134 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
8135
8136 if No_Constraint then
8137 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
8138
8139 else
8140 -- the analysis of the subtype_indication sets the
8141 -- digits value of the derived type.
8142
8143 null;
8144 end if;
8145 end if;
8146 end if;
8147
8148 if Is_Integer_Type (Parent_Type) then
8149 Set_Has_Shift_Operator
8150 (Implicit_Base, Has_Shift_Operator (Parent_Type));
8151 end if;
8152
8153 -- The type of the bounds is that of the parent type, and they
8154 -- must be converted to the derived type.
8155
8156 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
8157 end Build_Derived_Numeric_Type;
8158
8159 --------------------------------
8160 -- Build_Derived_Private_Type --
8161 --------------------------------
8162
8163 procedure Build_Derived_Private_Type
8164 (N : Node_Id;
8165 Parent_Type : Entity_Id;
8166 Derived_Type : Entity_Id;
8167 Is_Completion : Boolean;
8168 Derive_Subps : Boolean := True)
8169 is
8170 Loc : constant Source_Ptr := Sloc (N);
8171 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
8172 Par_Scope : constant Entity_Id := Scope (Par_Base);
8173 Full_N : constant Node_Id := New_Copy_Tree (N);
8174 Full_Der : Entity_Id := New_Copy (Derived_Type);
8175 Full_P : Entity_Id;
8176
8177 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
8178 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
8179 -- present (they cannot be both present for the same type), or Empty.
8180
8181 procedure Build_Full_Derivation;
8182 -- Build full derivation, i.e. derive from the full view
8183
8184 procedure Copy_And_Build;
8185 -- Copy derived type declaration, replace parent with its full view,
8186 -- and build derivation
8187
8188 -------------------------
8189 -- Available_Full_View --
8190 -------------------------
8191
8192 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
8193 begin
8194 if Present (Full_View (Typ)) then
8195 return Full_View (Typ);
8196
8197 elsif Present (Underlying_Full_View (Typ)) then
8198
8199 -- We should be called on a type with an underlying full view
8200 -- only by means of the recursive call made in Copy_And_Build
8201 -- through the first call to Build_Derived_Type, or else if
8202 -- the parent scope is being analyzed because we are deriving
8203 -- a completion.
8204
8205 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
8206
8207 return Underlying_Full_View (Typ);
8208
8209 else
8210 return Empty;
8211 end if;
8212 end Available_Full_View;
8213
8214 ---------------------------
8215 -- Build_Full_Derivation --
8216 ---------------------------
8217
8218 procedure Build_Full_Derivation is
8219 begin
8220 -- If parent scope is not open, install the declarations
8221
8222 if not In_Open_Scopes (Par_Scope) then
8223 Install_Private_Declarations (Par_Scope);
8224 Install_Visible_Declarations (Par_Scope);
8225 Copy_And_Build;
8226 Uninstall_Declarations (Par_Scope);
8227
8228 -- If parent scope is open and in another unit, and parent has a
8229 -- completion, then the derivation is taking place in the visible
8230 -- part of a child unit. In that case retrieve the full view of
8231 -- the parent momentarily.
8232
8233 elsif not In_Same_Source_Unit (N, Parent_Type)
8234 and then Present (Full_View (Parent_Type))
8235 then
8236 Full_P := Full_View (Parent_Type);
8237 Exchange_Declarations (Parent_Type);
8238 Copy_And_Build;
8239 Exchange_Declarations (Full_P);
8240
8241 -- Otherwise it is a local derivation
8242
8243 else
8244 Copy_And_Build;
8245 end if;
8246 end Build_Full_Derivation;
8247
8248 --------------------
8249 -- Copy_And_Build --
8250 --------------------
8251
8252 procedure Copy_And_Build is
8253 Full_Parent : Entity_Id := Parent_Type;
8254
8255 begin
8256 -- If the parent is itself derived from another private type,
8257 -- installing the private declarations has not affected its
8258 -- privacy status, so use its own full view explicitly.
8259
8260 if Is_Private_Type (Full_Parent)
8261 and then Present (Full_View (Full_Parent))
8262 then
8263 Full_Parent := Full_View (Full_Parent);
8264 end if;
8265
8266 -- If the full view is itself derived from another private type
8267 -- and has got an underlying full view, and this is done for a
8268 -- completion, i.e. to build the underlying full view of the type,
8269 -- then use this underlying full view. We cannot do that if this
8270 -- is not a completion, i.e. to build the full view of the type,
8271 -- because this would break the privacy of the parent type, except
8272 -- if the parent scope is being analyzed because we are deriving a
8273 -- completion.
8274
8275 if Is_Private_Type (Full_Parent)
8276 and then Present (Underlying_Full_View (Full_Parent))
8277 and then (Is_Completion or else In_Private_Part (Par_Scope))
8278 then
8279 Full_Parent := Underlying_Full_View (Full_Parent);
8280 end if;
8281
8282 -- For private, record, concurrent, access and almost all enumeration
8283 -- types, the derivation from the full view requires a fully-fledged
8284 -- declaration. In the other cases, just use an itype.
8285
8286 if Is_Private_Type (Full_Parent)
8287 or else Is_Record_Type (Full_Parent)
8288 or else Is_Concurrent_Type (Full_Parent)
8289 or else Is_Access_Type (Full_Parent)
8290 or else
8291 (Is_Enumeration_Type (Full_Parent)
8292 and then not Is_Standard_Character_Type (Full_Parent)
8293 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8294 then
8295 -- Copy and adjust declaration to provide a completion for what
8296 -- is originally a private declaration. Indicate that full view
8297 -- is internally generated.
8298
8299 Set_Comes_From_Source (Full_N, False);
8300 Set_Comes_From_Source (Full_Der, False);
8301 Set_Parent (Full_Der, Full_N);
8302 Set_Defining_Identifier (Full_N, Full_Der);
8303
8304 -- If there are no constraints, adjust the subtype mark
8305
8306 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8307 N_Subtype_Indication
8308 then
8309 Set_Subtype_Indication
8310 (Type_Definition (Full_N),
8311 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8312 end if;
8313
8314 Insert_After (N, Full_N);
8315
8316 -- Build full view of derived type from full view of parent which
8317 -- is now installed. Subprograms have been derived on the partial
8318 -- view, the completion does not derive them anew.
8319
8320 if Is_Record_Type (Full_Parent) then
8321
8322 -- If parent type is tagged, the completion inherits the proper
8323 -- primitive operations.
8324
8325 if Is_Tagged_Type (Parent_Type) then
8326 Build_Derived_Record_Type
8327 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8328 else
8329 Build_Derived_Record_Type
8330 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8331 end if;
8332
8333 else
8334 -- If the parent type is private, this is not a completion and
8335 -- we build the full derivation recursively as a completion.
8336
8337 Build_Derived_Type
8338 (Full_N, Full_Parent, Full_Der,
8339 Is_Completion => Is_Private_Type (Full_Parent),
8340 Derive_Subps => False);
8341 end if;
8342
8343 -- The full declaration has been introduced into the tree and
8344 -- processed in the step above. It should not be analyzed again
8345 -- (when encountered later in the current list of declarations)
8346 -- to prevent spurious name conflicts. The full entity remains
8347 -- invisible.
8348
8349 Set_Analyzed (Full_N);
8350
8351 else
8352 Full_Der :=
8353 Make_Defining_Identifier (Sloc (Derived_Type),
8354 Chars => Chars (Derived_Type));
8355 Set_Is_Itype (Full_Der);
8356 Set_Associated_Node_For_Itype (Full_Der, N);
8357 Set_Parent (Full_Der, N);
8358 Build_Derived_Type
8359 (N, Full_Parent, Full_Der,
8360 Is_Completion => False, Derive_Subps => False);
8361 Set_Is_Not_Self_Hidden (Full_Der);
8362 end if;
8363
8364 Set_Has_Private_Declaration (Full_Der);
8365 Set_Has_Private_Declaration (Derived_Type);
8366
8367 Set_Scope (Full_Der, Scope (Derived_Type));
8368 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8369 Set_Has_Size_Clause (Full_Der, False);
8370 Set_Has_Alignment_Clause (Full_Der, False);
8371 Set_Has_Delayed_Freeze (Full_Der);
8372 Set_Is_Frozen (Full_Der, False);
8373 Set_Freeze_Node (Full_Der, Empty);
8374 Set_Depends_On_Private
8375 (Full_Der, Has_Private_Component (Full_Der));
8376 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8377 Set_Is_Implicit_Full_View (Full_Der);
8378
8379 -- The convention on the base type may be set in the private part
8380 -- and not propagated to the subtype until later, so we obtain the
8381 -- convention from the base type of the parent.
8382
8383 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8384 end Copy_And_Build;
8385
8386 -- Start of processing for Build_Derived_Private_Type
8387
8388 begin
8389 if Is_Tagged_Type (Parent_Type) then
8390 Full_P := Full_View (Parent_Type);
8391
8392 -- A type extension of a type with unknown discriminants is an
8393 -- indefinite type that the back-end cannot handle directly.
8394 -- We treat it as a private type, and build a completion that is
8395 -- derived from the full view of the parent, and hopefully has
8396 -- known discriminants.
8397
8398 -- If the full view of the parent type has an underlying record view,
8399 -- use it to generate the underlying record view of this derived type
8400 -- (required for chains of derivations with unknown discriminants).
8401
8402 -- Minor optimization: we avoid the generation of useless underlying
8403 -- record view entities if the private type declaration has unknown
8404 -- discriminants but its corresponding full view has no
8405 -- discriminants.
8406
8407 if Has_Unknown_Discriminants (Parent_Type)
8408 and then Present (Full_P)
8409 and then (Has_Discriminants (Full_P)
8410 or else Present (Underlying_Record_View (Full_P)))
8411 and then not In_Open_Scopes (Par_Scope)
8412 and then Expander_Active
8413 then
8414 declare
8415 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8416 New_Ext : constant Node_Id :=
8417 Copy_Separate_Tree
8418 (Record_Extension_Part (Type_Definition (N)));
8419 Decl : Node_Id;
8420
8421 begin
8422 Build_Derived_Record_Type
8423 (N, Parent_Type, Derived_Type, Derive_Subps);
8424
8425 -- Build anonymous completion, as a derivation from the full
8426 -- view of the parent. This is not a completion in the usual
8427 -- sense, because the current type is not private.
8428
8429 Decl :=
8430 Make_Full_Type_Declaration (Loc,
8431 Defining_Identifier => Full_Der,
8432 Type_Definition =>
8433 Make_Derived_Type_Definition (Loc,
8434 Subtype_Indication =>
8435 New_Copy_Tree
8436 (Subtype_Indication (Type_Definition (N))),
8437 Record_Extension_Part => New_Ext));
8438
8439 -- If the parent type has an underlying record view, use it
8440 -- here to build the new underlying record view.
8441
8442 if Present (Underlying_Record_View (Full_P)) then
8443 pragma Assert
8444 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8445 = N_Identifier);
8446 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8447 Underlying_Record_View (Full_P));
8448 end if;
8449
8450 Install_Private_Declarations (Par_Scope);
8451 Install_Visible_Declarations (Par_Scope);
8452 Insert_Before (N, Decl);
8453
8454 -- Mark entity as an underlying record view before analysis,
8455 -- to avoid generating the list of its primitive operations
8456 -- (which is not really required for this entity) and thus
8457 -- prevent spurious errors associated with missing overriding
8458 -- of abstract primitives (overridden only for Derived_Type).
8459
8460 Mutate_Ekind (Full_Der, E_Record_Type);
8461 Set_Is_Underlying_Record_View (Full_Der);
8462 Set_Default_SSO (Full_Der);
8463 Set_No_Reordering (Full_Der, No_Component_Reordering);
8464
8465 Analyze (Decl);
8466
8467 pragma
8468 Assert
8469 ((Has_Discriminants (Full_Der)
8470 and then not Has_Unknown_Discriminants (Full_Der))
8471 or else Serious_Errors_Detected > 0);
8472
8473 Uninstall_Declarations (Par_Scope);
8474
8475 if Etype (Full_Der) = Any_Type then
8476 pragma Assert (Serious_Errors_Detected > 0);
8477 return;
8478 end if;
8479
8480 -- Freeze the underlying record view, to prevent generation of
8481 -- useless dispatching information, which is simply shared with
8482 -- the real derived type.
8483
8484 Set_Is_Frozen (Full_Der);
8485
8486 -- If the derived type has access discriminants, create
8487 -- references to their anonymous types now, to prevent
8488 -- back-end problems when their first use is in generated
8489 -- bodies of primitives.
8490
8491 declare
8492 E : Entity_Id;
8493
8494 begin
8495 E := First_Entity (Full_Der);
8496
8497 while Present (E) loop
8498 if Ekind (E) = E_Discriminant
8499 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8500 then
8501 Build_Itype_Reference (Etype (E), Decl);
8502 end if;
8503
8504 Next_Entity (E);
8505 end loop;
8506 end;
8507
8508 -- Set up links between real entity and underlying record view
8509
8510 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8511 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8512 end;
8513
8514 -- If discriminants are known, build derived record
8515
8516 else
8517 Build_Derived_Record_Type
8518 (N, Parent_Type, Derived_Type, Derive_Subps);
8519 end if;
8520
8521 return;
8522
8523 elsif Has_Discriminants (Parent_Type) then
8524
8525 -- Build partial view of derived type from partial view of parent.
8526 -- This must be done before building the full derivation because the
8527 -- second derivation will modify the discriminants of the first and
8528 -- the discriminants are chained with the rest of the components in
8529 -- the full derivation.
8530
8531 Build_Derived_Record_Type
8532 (N, Parent_Type, Derived_Type, Derive_Subps);
8533
8534 -- Build the full derivation if this is not the anonymous derived
8535 -- base type created by Build_Derived_Record_Type in the constrained
8536 -- case (see point 5. of its head comment) since we build it for the
8537 -- derived subtype.
8538
8539 if Present (Available_Full_View (Parent_Type))
8540 and then not Is_Itype (Derived_Type)
8541 then
8542 declare
8543 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8544 Discr : Entity_Id;
8545 Last_Discr : Entity_Id;
8546
8547 begin
8548 -- If this is not a completion, construct the implicit full
8549 -- view by deriving from the full view of the parent type.
8550 -- But if this is a completion, the derived private type
8551 -- being built is a full view and the full derivation can
8552 -- only be its underlying full view.
8553
8554 Build_Full_Derivation;
8555
8556 if not Is_Completion then
8557 Set_Full_View (Derived_Type, Full_Der);
8558 else
8559 Set_Underlying_Full_View (Derived_Type, Full_Der);
8560 Set_Is_Underlying_Full_View (Full_Der);
8561 end if;
8562
8563 if not Is_Base_Type (Derived_Type) then
8564 Set_Full_View (Der_Base, Base_Type (Full_Der));
8565 end if;
8566
8567 -- Copy the discriminant list from full view to the partial
8568 -- view (base type and its subtype). Gigi requires that the
8569 -- partial and full views have the same discriminants.
8570
8571 -- Note that since the partial view points to discriminants
8572 -- in the full view, their scope will be that of the full
8573 -- view. This might cause some front end problems and need
8574 -- adjustment???
8575
8576 Discr := First_Discriminant (Base_Type (Full_Der));
8577 Set_First_Entity (Der_Base, Discr);
8578
8579 loop
8580 Last_Discr := Discr;
8581 Next_Discriminant (Discr);
8582 exit when No (Discr);
8583 end loop;
8584
8585 Set_Last_Entity (Der_Base, Last_Discr);
8586 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8587 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8588 end;
8589 end if;
8590
8591 elsif Present (Available_Full_View (Parent_Type))
8592 and then Has_Discriminants (Available_Full_View (Parent_Type))
8593 then
8594 if Has_Unknown_Discriminants (Parent_Type)
8595 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8596 N_Subtype_Indication
8597 then
8598 Error_Msg_N
8599 ("cannot constrain type with unknown discriminants",
8600 Subtype_Indication (Type_Definition (N)));
8601 return;
8602 end if;
8603
8604 -- If this is not a completion, construct the implicit full view by
8605 -- deriving from the full view of the parent type. But if this is a
8606 -- completion, the derived private type being built is a full view
8607 -- and the full derivation can only be its underlying full view.
8608
8609 Build_Full_Derivation;
8610
8611 if not Is_Completion then
8612 Set_Full_View (Derived_Type, Full_Der);
8613 else
8614 Set_Underlying_Full_View (Derived_Type, Full_Der);
8615 Set_Is_Underlying_Full_View (Full_Der);
8616 end if;
8617
8618 -- In any case, the primitive operations are inherited from the
8619 -- parent type, not from the internal full view.
8620
8621 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8622
8623 if Derive_Subps then
8624 -- Initialize the list of primitive operations to an empty list,
8625 -- to cover tagged types as well as untagged types. For untagged
8626 -- types this is used either to analyze the call as legal when
8627 -- GNAT extensions are allowed, or to give better error messages.
8628
8629 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8630
8631 Derive_Subprograms (Parent_Type, Derived_Type);
8632 end if;
8633
8634 Set_Stored_Constraint (Derived_Type, No_Elist);
8635 Set_Is_Constrained
8636 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8637
8638 else
8639 -- Untagged type, No discriminants on either view
8640
8641 if Nkind (Subtype_Indication (Type_Definition (N))) =
8642 N_Subtype_Indication
8643 then
8644 Error_Msg_N
8645 ("illegal constraint on type without discriminants", N);
8646 end if;
8647
8648 if Present (Discriminant_Specifications (N))
8649 and then Present (Available_Full_View (Parent_Type))
8650 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8651 then
8652 Error_Msg_N ("cannot add discriminants to untagged type", N);
8653 end if;
8654
8655 Set_Stored_Constraint (Derived_Type, No_Elist);
8656 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8657
8658 -- If this is not a completion, construct the implicit full view by
8659 -- deriving from the full view of the parent type. But if this is a
8660 -- completion, the derived private type being built is a full view
8661 -- and the full derivation can only be its underlying full view.
8662
8663 -- ??? If the parent type is untagged private and its completion is
8664 -- tagged, this mechanism will not work because we cannot derive from
8665 -- the tagged full view unless we have an extension.
8666
8667 if Present (Available_Full_View (Parent_Type))
8668 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8669 and then not Error_Posted (N)
8670 then
8671 Build_Full_Derivation;
8672
8673 if not Is_Completion then
8674 Set_Full_View (Derived_Type, Full_Der);
8675 else
8676 Set_Underlying_Full_View (Derived_Type, Full_Der);
8677 Set_Is_Underlying_Full_View (Full_Der);
8678 end if;
8679 end if;
8680 end if;
8681
8682 Set_Has_Unknown_Discriminants (Derived_Type,
8683 Has_Unknown_Discriminants (Parent_Type));
8684
8685 if Is_Private_Type (Derived_Type) then
8686 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8687 end if;
8688
8689 -- If the parent base type is in scope, add the derived type to its
8690 -- list of private dependents, because its full view may become
8691 -- visible subsequently (in a nested private part, a body, or in a
8692 -- further child unit).
8693
8694 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8695 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8696
8697 -- Check for unusual case where a type completed by a private
8698 -- derivation occurs within a package nested in a child unit, and
8699 -- the parent is declared in an ancestor.
8700
8701 if Is_Child_Unit (Scope (Current_Scope))
8702 and then Is_Completion
8703 and then In_Private_Part (Current_Scope)
8704 and then Scope (Parent_Type) /= Current_Scope
8705
8706 -- Note that if the parent has a completion in the private part,
8707 -- (which is itself a derivation from some other private type)
8708 -- it is that completion that is visible, there is no full view
8709 -- available, and no special processing is needed.
8710
8711 and then Present (Full_View (Parent_Type))
8712 then
8713 -- In this case, the full view of the parent type will become
8714 -- visible in the body of the enclosing child, and only then will
8715 -- the current type be possibly non-private. Build an underlying
8716 -- full view that will be installed when the enclosing child body
8717 -- is compiled.
8718
8719 if Present (Underlying_Full_View (Derived_Type)) then
8720 Full_Der := Underlying_Full_View (Derived_Type);
8721 else
8722 Build_Full_Derivation;
8723 Set_Underlying_Full_View (Derived_Type, Full_Der);
8724 Set_Is_Underlying_Full_View (Full_Der);
8725 end if;
8726
8727 -- The full view will be used to swap entities on entry/exit to
8728 -- the body, and must appear in the entity list for the package.
8729
8730 Append_Entity (Full_Der, Scope (Derived_Type));
8731 end if;
8732 end if;
8733 end Build_Derived_Private_Type;
8734
8735 -------------------------------
8736 -- Build_Derived_Record_Type --
8737 -------------------------------
8738
8739 -- 1. INTRODUCTION
8740
8741 -- Ideally we would like to use the same model of type derivation for
8742 -- tagged and untagged record types. Unfortunately this is not quite
8743 -- possible because the semantics of representation clauses is different
8744 -- for tagged and untagged records under inheritance. Consider the
8745 -- following:
8746
8747 -- type R (...) is [tagged] record ... end record;
8748 -- type T (...) is new R (...) [with ...];
8749
8750 -- The representation clauses for T can specify a completely different
8751 -- record layout from R's. Hence the same component can be placed in two
8752 -- very different positions in objects of type T and R. If R and T are
8753 -- tagged types, representation clauses for T can only specify the layout
8754 -- of non inherited components, thus components that are common in R and T
8755 -- have the same position in objects of type R and T.
8756
8757 -- This has two implications. The first is that the entire tree for R's
8758 -- declaration needs to be copied for T in the untagged case, so that T
8759 -- can be viewed as a record type of its own with its own representation
8760 -- clauses. The second implication is the way we handle discriminants.
8761 -- Specifically, in the untagged case we need a way to communicate to Gigi
8762 -- what are the real discriminants in the record, while for the semantics
8763 -- we need to consider those introduced by the user to rename the
8764 -- discriminants in the parent type. This is handled by introducing the
8765 -- notion of stored discriminants. See below for more.
8766
8767 -- Fortunately the way regular components are inherited can be handled in
8768 -- the same way in tagged and untagged types.
8769
8770 -- To complicate things a bit more the private view of a private extension
8771 -- cannot be handled in the same way as the full view (for one thing the
8772 -- semantic rules are somewhat different). We will explain what differs
8773 -- below.
8774
8775 -- 2. DISCRIMINANTS UNDER INHERITANCE
8776
8777 -- The semantic rules governing the discriminants of derived types are
8778 -- quite subtle.
8779
8780 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8781 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8782
8783 -- If parent type has discriminants, then the discriminants that are
8784 -- declared in the derived type are [3.4 (11)]:
8785
8786 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8787 -- there is one;
8788
8789 -- o Otherwise, each discriminant of the parent type (implicitly declared
8790 -- in the same order with the same specifications). In this case, the
8791 -- discriminants are said to be "inherited", or if unknown in the parent
8792 -- are also unknown in the derived type.
8793
8794 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8795
8796 -- o The parent subtype must be constrained;
8797
8798 -- o If the parent type is not a tagged type, then each discriminant of
8799 -- the derived type must be used in the constraint defining a parent
8800 -- subtype. [Implementation note: This ensures that the new discriminant
8801 -- can share storage with an existing discriminant.]
8802
8803 -- For the derived type each discriminant of the parent type is either
8804 -- inherited, constrained to equal some new discriminant of the derived
8805 -- type, or constrained to the value of an expression.
8806
8807 -- When inherited or constrained to equal some new discriminant, the
8808 -- parent discriminant and the discriminant of the derived type are said
8809 -- to "correspond".
8810
8811 -- If a discriminant of the parent type is constrained to a specific value
8812 -- in the derived type definition, then the discriminant is said to be
8813 -- "specified" by that derived type definition.
8814
8815 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8816
8817 -- We have spoken about stored discriminants in point 1 (introduction)
8818 -- above. There are two sorts of stored discriminants: implicit and
8819 -- explicit. As long as the derived type inherits the same discriminants as
8820 -- the root record type, stored discriminants are the same as regular
8821 -- discriminants, and are said to be implicit. However, if any discriminant
8822 -- in the root type was renamed in the derived type, then the derived
8823 -- type will contain explicit stored discriminants. Explicit stored
8824 -- discriminants are discriminants in addition to the semantically visible
8825 -- discriminants defined for the derived type. Stored discriminants are
8826 -- used by Gigi to figure out what are the physical discriminants in
8827 -- objects of the derived type (see precise definition in einfo.ads).
8828 -- As an example, consider the following:
8829
8830 -- type R (D1, D2, D3 : Int) is record ... end record;
8831 -- type T1 is new R;
8832 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8833 -- type T3 is new T2;
8834 -- type T4 (Y : Int) is new T3 (Y, 99);
8835
8836 -- The following table summarizes the discriminants and stored
8837 -- discriminants in R and T1 through T4:
8838
8839 -- Type Discrim Stored Discrim Comment
8840 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8841 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8842 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8843 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8844 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8845
8846 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8847 -- find the corresponding discriminant in the parent type, while
8848 -- Original_Record_Component (abbreviated ORC below) the actual physical
8849 -- component that is renamed. Finally the field Is_Completely_Hidden
8850 -- (abbreviated ICH below) is set for all explicit stored discriminants
8851 -- (see einfo.ads for more info). For the above example this gives:
8852
8853 -- Discrim CD ORC ICH
8854 -- ^^^^^^^ ^^ ^^^ ^^^
8855 -- D1 in R empty itself no
8856 -- D2 in R empty itself no
8857 -- D3 in R empty itself no
8858
8859 -- D1 in T1 D1 in R itself no
8860 -- D2 in T1 D2 in R itself no
8861 -- D3 in T1 D3 in R itself no
8862
8863 -- X1 in T2 D3 in T1 D3 in T2 no
8864 -- X2 in T2 D1 in T1 D1 in T2 no
8865 -- D1 in T2 empty itself yes
8866 -- D2 in T2 empty itself yes
8867 -- D3 in T2 empty itself yes
8868
8869 -- X1 in T3 X1 in T2 D3 in T3 no
8870 -- X2 in T3 X2 in T2 D1 in T3 no
8871 -- D1 in T3 empty itself yes
8872 -- D2 in T3 empty itself yes
8873 -- D3 in T3 empty itself yes
8874
8875 -- Y in T4 X1 in T3 D3 in T4 no
8876 -- D1 in T4 empty itself yes
8877 -- D2 in T4 empty itself yes
8878 -- D3 in T4 empty itself yes
8879
8880 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8881
8882 -- Type derivation for tagged types is fairly straightforward. If no
8883 -- discriminants are specified by the derived type, these are inherited
8884 -- from the parent. No explicit stored discriminants are ever necessary.
8885 -- The only manipulation that is done to the tree is that of adding a
8886 -- _parent field with parent type and constrained to the same constraint
8887 -- specified for the parent in the derived type definition. For instance:
8888
8889 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8890 -- type T1 is new R with null record;
8891 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8892
8893 -- are changed into:
8894
8895 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8896 -- _parent : R (D1, D2, D3);
8897 -- end record;
8898
8899 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8900 -- _parent : T1 (X2, 88, X1);
8901 -- end record;
8902
8903 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8904 -- ORC and ICH fields are:
8905
8906 -- Discrim CD ORC ICH
8907 -- ^^^^^^^ ^^ ^^^ ^^^
8908 -- D1 in R empty itself no
8909 -- D2 in R empty itself no
8910 -- D3 in R empty itself no
8911
8912 -- D1 in T1 D1 in R D1 in R no
8913 -- D2 in T1 D2 in R D2 in R no
8914 -- D3 in T1 D3 in R D3 in R no
8915
8916 -- X1 in T2 D3 in T1 D3 in R no
8917 -- X2 in T2 D1 in T1 D1 in R no
8918
8919 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8920 --
8921 -- Regardless of whether we are dealing with a tagged or untagged type
8922 -- we will transform all derived type declarations of the form
8923 --
8924 -- type T is new R (...) [with ...];
8925 -- or
8926 -- subtype S is R (...);
8927 -- type T is new S [with ...];
8928 -- into
8929 -- type BT is new R [with ...];
8930 -- subtype T is BT (...);
8931 --
8932 -- That is, the base derived type is constrained only if it has no
8933 -- discriminants. The reason for doing this is that GNAT's semantic model
8934 -- assumes that a base type with discriminants is unconstrained.
8935 --
8936 -- Note that, strictly speaking, the above transformation is not always
8937 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8938 --
8939 -- procedure B34011A is
8940 -- type REC (D : integer := 0) is record
8941 -- I : Integer;
8942 -- end record;
8943
8944 -- package P is
8945 -- type T6 is new Rec;
8946 -- function F return T6;
8947 -- end P;
8948
8949 -- use P;
8950 -- package Q6 is
8951 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8952 -- end Q6;
8953 --
8954 -- The definition of Q6.U is illegal. However transforming Q6.U into
8955
8956 -- type BaseU is new T6;
8957 -- subtype U is BaseU (Q6.F.I)
8958
8959 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8960 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8961 -- the transformation described above.
8962
8963 -- There is another instance where the above transformation is incorrect.
8964 -- Consider:
8965
8966 -- package Pack is
8967 -- type Base (D : Integer) is tagged null record;
8968 -- procedure P (X : Base);
8969
8970 -- type Der is new Base (2) with null record;
8971 -- procedure P (X : Der);
8972 -- end Pack;
8973
8974 -- Then the above transformation turns this into
8975
8976 -- type Der_Base is new Base with null record;
8977 -- -- procedure P (X : Base) is implicitly inherited here
8978 -- -- as procedure P (X : Der_Base).
8979
8980 -- subtype Der is Der_Base (2);
8981 -- procedure P (X : Der);
8982 -- -- The overriding of P (X : Der_Base) is illegal since we
8983 -- -- have a parameter conformance problem.
8984
8985 -- To get around this problem, after having semantically processed Der_Base
8986 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8987 -- Discriminant_Constraint from Der so that when parameter conformance is
8988 -- checked when P is overridden, no semantic errors are flagged.
8989
8990 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8991
8992 -- Regardless of whether we are dealing with a tagged or untagged type
8993 -- we will transform all derived type declarations of the form
8994
8995 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8996 -- type T is new R [with ...];
8997 -- into
8998 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8999
9000 -- The reason for such transformation is that it allows us to implement a
9001 -- very clean form of component inheritance as explained below.
9002
9003 -- Note that this transformation is not achieved by direct tree rewriting
9004 -- and manipulation, but rather by redoing the semantic actions that the
9005 -- above transformation will entail. This is done directly in routine
9006 -- Inherit_Components.
9007
9008 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
9009
9010 -- In both tagged and untagged derived types, regular non discriminant
9011 -- components are inherited in the derived type from the parent type. In
9012 -- the absence of discriminants component, inheritance is straightforward
9013 -- as components can simply be copied from the parent.
9014
9015 -- If the parent has discriminants, inheriting components constrained with
9016 -- these discriminants requires caution. Consider the following example:
9017
9018 -- type R (D1, D2 : Positive) is [tagged] record
9019 -- S : String (D1 .. D2);
9020 -- end record;
9021
9022 -- type T1 is new R [with null record];
9023 -- type T2 (X : positive) is new R (1, X) [with null record];
9024
9025 -- As explained in 6. above, T1 is rewritten as
9026 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
9027 -- which makes the treatment for T1 and T2 identical.
9028
9029 -- What we want when inheriting S, is that references to D1 and D2 in R are
9030 -- replaced with references to their correct constraints, i.e. D1 and D2 in
9031 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
9032 -- with either discriminant references in the derived type or expressions.
9033 -- This replacement is achieved as follows: before inheriting R's
9034 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
9035 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
9036 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
9037 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
9038 -- by String (1 .. X).
9039
9040 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
9041
9042 -- We explain here the rules governing private type extensions relevant to
9043 -- type derivation. These rules are explained on the following example:
9044
9045 -- type D [(...)] is new A [(...)] with private; <-- partial view
9046 -- type D [(...)] is new P [(...)] with null record; <-- full view
9047
9048 -- Type A is called the ancestor subtype of the private extension.
9049 -- Type P is the parent type of the full view of the private extension. It
9050 -- must be A or a type derived from A.
9051
9052 -- The rules concerning the discriminants of private type extensions are
9053 -- [7.3(10-13)]:
9054
9055 -- o If a private extension inherits known discriminants from the ancestor
9056 -- subtype, then the full view must also inherit its discriminants from
9057 -- the ancestor subtype and the parent subtype of the full view must be
9058 -- constrained if and only if the ancestor subtype is constrained.
9059
9060 -- o If a partial view has unknown discriminants, then the full view may
9061 -- define a definite or an indefinite subtype, with or without
9062 -- discriminants.
9063
9064 -- o If a partial view has neither known nor unknown discriminants, then
9065 -- the full view must define a definite subtype.
9066
9067 -- o If the ancestor subtype of a private extension has constrained
9068 -- discriminants, then the parent subtype of the full view must impose a
9069 -- statically matching constraint on those discriminants.
9070
9071 -- This means that only the following forms of private extensions are
9072 -- allowed:
9073
9074 -- type D is new A with private; <-- partial view
9075 -- type D is new P with null record; <-- full view
9076
9077 -- If A has no discriminants than P has no discriminants, otherwise P must
9078 -- inherit A's discriminants.
9079
9080 -- type D is new A (...) with private; <-- partial view
9081 -- type D is new P (:::) with null record; <-- full view
9082
9083 -- P must inherit A's discriminants and (...) and (:::) must statically
9084 -- match.
9085
9086 -- subtype A is R (...);
9087 -- type D is new A with private; <-- partial view
9088 -- type D is new P with null record; <-- full view
9089
9090 -- P must have inherited R's discriminants and must be derived from A or
9091 -- any of its subtypes.
9092
9093 -- type D (..) is new A with private; <-- partial view
9094 -- type D (..) is new P [(:::)] with null record; <-- full view
9095
9096 -- No specific constraints on P's discriminants or constraint (:::).
9097 -- Note that A can be unconstrained, but the parent subtype P must either
9098 -- be constrained or (:::) must be present.
9099
9100 -- type D (..) is new A [(...)] with private; <-- partial view
9101 -- type D (..) is new P [(:::)] with null record; <-- full view
9102
9103 -- P's constraints on A's discriminants must statically match those
9104 -- imposed by (...).
9105
9106 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
9107
9108 -- The full view of a private extension is handled exactly as described
9109 -- above. The model chose for the private view of a private extension is
9110 -- the same for what concerns discriminants (i.e. they receive the same
9111 -- treatment as in the tagged case). However, the private view of the
9112 -- private extension always inherits the components of the parent base,
9113 -- without replacing any discriminant reference. Strictly speaking this is
9114 -- incorrect. However, Gigi never uses this view to generate code so this
9115 -- is a purely semantic issue. In theory, a set of transformations similar
9116 -- to those given in 5. and 6. above could be applied to private views of
9117 -- private extensions to have the same model of component inheritance as
9118 -- for non private extensions. However, this is not done because it would
9119 -- further complicate private type processing. Semantically speaking, this
9120 -- leaves us in an uncomfortable situation. As an example consider:
9121
9122 -- package Pack is
9123 -- type R (D : integer) is tagged record
9124 -- S : String (1 .. D);
9125 -- end record;
9126 -- procedure P (X : R);
9127 -- type T is new R (1) with private;
9128 -- private
9129 -- type T is new R (1) with null record;
9130 -- end;
9131
9132 -- This is transformed into:
9133
9134 -- package Pack is
9135 -- type R (D : integer) is tagged record
9136 -- S : String (1 .. D);
9137 -- end record;
9138 -- procedure P (X : R);
9139 -- type T is new R (1) with private;
9140 -- private
9141 -- type BaseT is new R with null record;
9142 -- subtype T is BaseT (1);
9143 -- end;
9144
9145 -- (strictly speaking the above is incorrect Ada)
9146
9147 -- From the semantic standpoint the private view of private extension T
9148 -- should be flagged as constrained since one can clearly have
9149 --
9150 -- Obj : T;
9151 --
9152 -- in a unit withing Pack. However, when deriving subprograms for the
9153 -- private view of private extension T, T must be seen as unconstrained
9154 -- since T has discriminants (this is a constraint of the current
9155 -- subprogram derivation model). Thus, when processing the private view of
9156 -- a private extension such as T, we first mark T as unconstrained, we
9157 -- process it, we perform program derivation and just before returning from
9158 -- Build_Derived_Record_Type we mark T as constrained.
9159
9160 -- ??? Are there are other uncomfortable cases that we will have to
9161 -- deal with.
9162
9163 -- 10. RECORD_TYPE_WITH_PRIVATE complications
9164
9165 -- Types that are derived from a visible record type and have a private
9166 -- extension present other peculiarities. They behave mostly like private
9167 -- types, but if they have primitive operations defined, these will not
9168 -- have the proper signatures for further inheritance, because other
9169 -- primitive operations will use the implicit base that we define for
9170 -- private derivations below. This affect subprogram inheritance (see
9171 -- Derive_Subprograms for details). We also derive the implicit base from
9172 -- the base type of the full view, so that the implicit base is a record
9173 -- type and not another private type, This avoids infinite loops.
9174
9175 procedure Build_Derived_Record_Type
9176 (N : Node_Id;
9177 Parent_Type : Entity_Id;
9178 Derived_Type : Entity_Id;
9179 Derive_Subps : Boolean := True)
9180 is
9181 Discriminant_Specs : constant Boolean :=
9182 Present (Discriminant_Specifications (N));
9183 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
9184 Loc : constant Source_Ptr := Sloc (N);
9185 Private_Extension : constant Boolean :=
9186 Nkind (N) = N_Private_Extension_Declaration;
9187 Assoc_List : Elist_Id;
9188 Constraint_Present : Boolean;
9189 Constrs : Elist_Id;
9190 Discrim : Entity_Id;
9191 Indic : Node_Id;
9192 Inherit_Discrims : Boolean := False;
9193 Last_Discrim : Entity_Id;
9194 New_Base : Entity_Id;
9195 New_Decl : Node_Id;
9196 New_Discrs : Elist_Id;
9197 New_Indic : Node_Id;
9198 Parent_Base : Entity_Id;
9199 Save_Etype : Entity_Id;
9200 Save_Discr_Constr : Elist_Id;
9201 Save_Next_Entity : Entity_Id;
9202 Type_Def : Node_Id;
9203
9204 Discs : Elist_Id := New_Elmt_List;
9205 -- An empty Discs list means that there were no constraints in the
9206 -- subtype indication or that there was an error processing it.
9207
9208 procedure Check_Generic_Ancestors;
9209 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
9210 -- cannot be declared at a deeper level than its parent type is
9211 -- removed. The check on derivation within a generic body is also
9212 -- relaxed, but there's a restriction that a derived tagged type
9213 -- cannot be declared in a generic body if it's derived directly
9214 -- or indirectly from a formal type of that generic. This applies
9215 -- to progenitors as well.
9216
9217 -----------------------------
9218 -- Check_Generic_Ancestors --
9219 -----------------------------
9220
9221 procedure Check_Generic_Ancestors is
9222 Ancestor_Type : Entity_Id;
9223 Intf_List : List_Id;
9224 Intf_Name : Node_Id;
9225
9226 procedure Check_Ancestor;
9227 -- For parent and progenitors.
9228
9229 --------------------
9230 -- Check_Ancestor --
9231 --------------------
9232
9233 procedure Check_Ancestor is
9234 begin
9235 -- If the derived type does have a formal type as an ancestor
9236 -- then it's an error if the derived type is declared within
9237 -- the body of the generic unit that declares the formal type
9238 -- in its generic formal part. It's sufficient to check whether
9239 -- the ancestor type is declared inside the same generic body
9240 -- as the derived type (such as within a nested generic spec),
9241 -- in which case the derivation is legal. If the formal type is
9242 -- declared outside of that generic body, then it's certain
9243 -- that the derived type is declared within the generic body
9244 -- of the generic unit declaring the formal type.
9245
9246 if Is_Generic_Type (Ancestor_Type)
9247 and then Enclosing_Generic_Body (Ancestor_Type) /=
9248 Enclosing_Generic_Body (Derived_Type)
9249 then
9250 Error_Msg_NE
9251 ("ancestor type& is formal type of enclosing"
9252 & " generic unit (RM 3.9.1 (4/2))",
9253 Indic, Ancestor_Type);
9254 end if;
9255 end Check_Ancestor;
9256
9257 begin
9258 if Nkind (N) = N_Private_Extension_Declaration then
9259 Intf_List := Interface_List (N);
9260 else
9261 Intf_List := Interface_List (Type_Definition (N));
9262 end if;
9263
9264 if Present (Enclosing_Generic_Body (Derived_Type)) then
9265 Ancestor_Type := Parent_Type;
9266
9267 while not Is_Generic_Type (Ancestor_Type)
9268 and then Etype (Ancestor_Type) /= Ancestor_Type
9269 loop
9270 Ancestor_Type := Etype (Ancestor_Type);
9271 end loop;
9272
9273 Check_Ancestor;
9274
9275 if Present (Intf_List) then
9276 Intf_Name := First (Intf_List);
9277 while Present (Intf_Name) loop
9278 Ancestor_Type := Entity (Intf_Name);
9279 Check_Ancestor;
9280 Next (Intf_Name);
9281 end loop;
9282 end if;
9283 end if;
9284 end Check_Generic_Ancestors;
9285
9286 -- Start of processing for Build_Derived_Record_Type
9287
9288 begin
9289 -- If the parent type is a private extension with discriminants, we
9290 -- need to have an unconstrained type on which to apply the inherited
9291 -- constraint, so we get to the full view. However, this means that the
9292 -- derived type and its implicit base type created below will not point
9293 -- to the same view of their respective parent type and, thus, special
9294 -- glue code like Exp_Ch7.Convert_View is needed to bridge this gap.
9295
9296 if Ekind (Parent_Type) = E_Record_Type_With_Private
9297 and then Has_Discriminants (Parent_Type)
9298 and then Present (Full_View (Parent_Type))
9299 then
9300 Parent_Base := Base_Type (Full_View (Parent_Type));
9301 else
9302 Parent_Base := Base_Type (Parent_Type);
9303 end if;
9304
9305 -- If the parent type is declared as a subtype of another private
9306 -- type with inherited discriminants, its generated base type is
9307 -- itself a record subtype. To further inherit the constraint we
9308 -- need to use its own base to have an unconstrained type on which
9309 -- to apply the inherited constraint.
9310
9311 if Ekind (Parent_Base) = E_Record_Subtype then
9312 Parent_Base := Base_Type (Parent_Base);
9313 end if;
9314
9315 -- If the parent base is a private type and only its full view has
9316 -- discriminants, use the full view's base type.
9317
9318 -- This can happen when we are deriving from a subtype of a derived type
9319 -- of a private type derived from a discriminated type with known
9320 -- discriminant:
9321 --
9322 -- package Pkg;
9323 -- type Root_Type(I: Positive) is record
9324 -- ...
9325 -- end record;
9326 -- type Bounded_Root_Type is private;
9327 -- private
9328 -- type Bounded_Root_Type is new Root_Type(10);
9329 -- end Pkg;
9330 --
9331 -- package Pkg2 is
9332 -- type Constrained_Root_Type is new Pkg.Bounded_Root_Type;
9333 -- end Pkg2;
9334 -- subtype Sub_Base is Pkg2.Constrained_Root_Type;
9335 -- type New_Der_Type is new Sub_Base;
9336
9337 if Is_Private_Type (Parent_Base)
9338 and then Present (Full_View (Parent_Base))
9339 and then not Has_Discriminants (Parent_Base)
9340 and then Has_Discriminants (Full_View (Parent_Base))
9341 then
9342 Parent_Base := Base_Type (Full_View (Parent_Base));
9343 end if;
9344
9345 -- AI05-0115: if this is a derivation from a private type in some
9346 -- other scope that may lead to invisible components for the derived
9347 -- type, mark it accordingly.
9348
9349 if Is_Private_Type (Parent_Type) then
9350 if Scope (Parent_Base) = Scope (Derived_Type) then
9351 null;
9352
9353 elsif In_Open_Scopes (Scope (Parent_Base))
9354 and then In_Private_Part (Scope (Parent_Base))
9355 then
9356 null;
9357
9358 else
9359 Set_Has_Private_Ancestor (Derived_Type);
9360 end if;
9361
9362 else
9363 Set_Has_Private_Ancestor
9364 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9365 end if;
9366
9367 -- Before we start the previously documented transformations, here is
9368 -- little fix for size and alignment of tagged types. Normally when we
9369 -- derive type D from type P, we copy the size and alignment of P as the
9370 -- default for D, and in the absence of explicit representation clauses
9371 -- for D, the size and alignment are indeed the same as the parent.
9372
9373 -- But this is wrong for tagged types, since fields may be added, and
9374 -- the default size may need to be larger, and the default alignment may
9375 -- need to be larger.
9376
9377 -- We therefore reset the size and alignment fields in the tagged case.
9378 -- Note that the size and alignment will in any case be at least as
9379 -- large as the parent type (since the derived type has a copy of the
9380 -- parent type in the _parent field)
9381
9382 -- The type is also marked as being tagged here, which is needed when
9383 -- processing components with a self-referential anonymous access type
9384 -- in the call to Check_Anonymous_Access_Components below. Note that
9385 -- this flag is also set later on for completeness.
9386
9387 if Is_Tagged then
9388 Set_Is_Tagged_Type (Derived_Type);
9389 Reinit_Size_Align (Derived_Type);
9390 end if;
9391
9392 -- STEP 0a: figure out what kind of derived type declaration we have
9393
9394 if Private_Extension then
9395 Type_Def := N;
9396 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9397 Set_Default_SSO (Derived_Type);
9398 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9399
9400 else
9401 Type_Def := Type_Definition (N);
9402
9403 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9404 -- Parent_Base can be a private type or private extension. However,
9405 -- for tagged types with an extension the newly added fields are
9406 -- visible and hence the Derived_Type is always an E_Record_Type.
9407 -- (except that the parent may have its own private fields).
9408 -- For untagged types we preserve the Ekind of the Parent_Base.
9409
9410 if Present (Record_Extension_Part (Type_Def)) then
9411 Mutate_Ekind (Derived_Type, E_Record_Type);
9412 Set_Default_SSO (Derived_Type);
9413 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9414
9415 -- Create internal access types for components with anonymous
9416 -- access types.
9417
9418 if Ada_Version >= Ada_2005 then
9419 Check_Anonymous_Access_Components
9420 (N, Derived_Type, Derived_Type,
9421 Component_List (Record_Extension_Part (Type_Def)));
9422 end if;
9423
9424 else
9425 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9426 end if;
9427 end if;
9428
9429 -- Indic can either be an N_Identifier if the subtype indication
9430 -- contains no constraint or an N_Subtype_Indication if the subtype
9431 -- indication has a constraint. In either case it can include an
9432 -- interface list.
9433
9434 Indic := Subtype_Indication (Type_Def);
9435 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9436
9437 -- Check that the type has visible discriminants. The type may be
9438 -- a private type with unknown discriminants whose full view has
9439 -- discriminants which are invisible.
9440
9441 if Constraint_Present then
9442 if not Has_Discriminants (Parent_Base)
9443 or else
9444 (Has_Unknown_Discriminants (Parent_Type)
9445 and then Is_Private_Type (Parent_Type))
9446 then
9447 Error_Msg_N
9448 ("invalid constraint: type has no discriminant",
9449 Constraint (Indic));
9450
9451 Constraint_Present := False;
9452 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9453
9454 elsif Is_Constrained (Parent_Type) then
9455 Error_Msg_N
9456 ("invalid constraint: parent type is already constrained",
9457 Constraint (Indic));
9458
9459 Constraint_Present := False;
9460 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9461 end if;
9462 end if;
9463
9464 -- STEP 0b: If needed, apply transformation given in point 5. above
9465
9466 if not Private_Extension
9467 and then Has_Discriminants (Parent_Type)
9468 and then not Discriminant_Specs
9469 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9470 then
9471 -- First, we must analyze the constraint (see comment in point 5.)
9472 -- The constraint may come from the subtype indication of the full
9473 -- declaration. Temporarily set the state of the Derived_Type to
9474 -- "self-hidden" (see RM-8.3(17)).
9475
9476 if Constraint_Present then
9477 pragma Assert (Is_Not_Self_Hidden (Derived_Type));
9478 Set_Is_Not_Self_Hidden (Derived_Type, False);
9479 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9480 Set_Is_Not_Self_Hidden (Derived_Type);
9481
9482 -- If there is no explicit constraint, there might be one that is
9483 -- inherited from a constrained parent type. In that case verify that
9484 -- it conforms to the constraint in the partial view. In perverse
9485 -- cases the parent subtypes of the partial and full view can have
9486 -- different constraints.
9487
9488 elsif Present (Stored_Constraint (Parent_Type)) then
9489 New_Discrs := Stored_Constraint (Parent_Type);
9490
9491 else
9492 New_Discrs := No_Elist;
9493 end if;
9494
9495 if Has_Discriminants (Derived_Type)
9496 and then Has_Private_Declaration (Derived_Type)
9497 and then Present (Discriminant_Constraint (Derived_Type))
9498 and then Present (New_Discrs)
9499 then
9500 -- Verify that constraints of the full view statically match
9501 -- those given in the partial view.
9502
9503 declare
9504 C1, C2 : Elmt_Id;
9505
9506 begin
9507 C1 := First_Elmt (New_Discrs);
9508 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9509 while Present (C1) and then Present (C2) loop
9510 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9511 or else
9512 (Is_OK_Static_Expression (Node (C1))
9513 and then Is_OK_Static_Expression (Node (C2))
9514 and then
9515 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9516 then
9517 null;
9518
9519 else
9520 if Constraint_Present then
9521 Error_Msg_N
9522 ("constraint not conformant to previous declaration",
9523 Node (C1));
9524 else
9525 Error_Msg_N
9526 ("constraint of full view is incompatible "
9527 & "with partial view", N);
9528 end if;
9529 end if;
9530
9531 Next_Elmt (C1);
9532 Next_Elmt (C2);
9533 end loop;
9534 end;
9535 end if;
9536
9537 -- Insert and analyze the declaration for the unconstrained base type
9538
9539 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9540
9541 New_Decl :=
9542 Make_Full_Type_Declaration (Loc,
9543 Defining_Identifier => New_Base,
9544 Type_Definition =>
9545 Make_Derived_Type_Definition (Loc,
9546 Abstract_Present => Abstract_Present (Type_Def),
9547 Limited_Present => Limited_Present (Type_Def),
9548 Subtype_Indication =>
9549 New_Occurrence_Of (Parent_Base, Loc),
9550 Record_Extension_Part =>
9551 Relocate_Node (Record_Extension_Part (Type_Def)),
9552 Interface_List => Interface_List (Type_Def)));
9553
9554 Set_Parent (New_Decl, Parent (N));
9555 Mark_Rewrite_Insertion (New_Decl);
9556 Insert_Before (N, New_Decl);
9557
9558 -- In the extension case, make sure ancestor is frozen appropriately
9559 -- (see also non-discriminated case below).
9560
9561 if Present (Record_Extension_Part (Type_Def))
9562 or else Is_Interface (Parent_Base)
9563 then
9564 Freeze_Before (New_Decl, Parent_Type);
9565 end if;
9566
9567 -- Note that this call passes False for the Derive_Subps parameter
9568 -- because subprogram derivation is deferred until after creating
9569 -- the subtype (see below).
9570
9571 Build_Derived_Type
9572 (New_Decl, Parent_Base, New_Base,
9573 Is_Completion => False, Derive_Subps => False);
9574
9575 Set_Is_Implicit_Full_View (New_Base);
9576
9577 -- ??? This needs re-examination to determine whether the
9578 -- following call can simply be replaced by a call to Analyze.
9579
9580 Set_Analyzed (New_Decl);
9581
9582 -- Insert and analyze the declaration for the constrained subtype
9583
9584 if Constraint_Present then
9585 New_Indic :=
9586 Make_Subtype_Indication (Loc,
9587 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9588 Constraint => Relocate_Node (Constraint (Indic)));
9589
9590 else
9591 declare
9592 Constr_List : constant List_Id := New_List;
9593 C : Elmt_Id;
9594 Expr : Node_Id;
9595
9596 begin
9597 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9598 while Present (C) loop
9599 Expr := Node (C);
9600
9601 -- It is safe here to call New_Copy_Tree since we called
9602 -- Force_Evaluation on each constraint previously
9603 -- in Build_Discriminant_Constraints.
9604
9605 Append (New_Copy_Tree (Expr), To => Constr_List);
9606
9607 Next_Elmt (C);
9608 end loop;
9609
9610 New_Indic :=
9611 Make_Subtype_Indication (Loc,
9612 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9613 Constraint =>
9614 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9615 end;
9616 end if;
9617
9618 Rewrite (N,
9619 Make_Subtype_Declaration (Loc,
9620 Defining_Identifier => Derived_Type,
9621 Subtype_Indication => New_Indic));
9622
9623 -- Keep the aspects from the original node
9624
9625 Move_Aspects (Original_Node (N), N);
9626
9627 Analyze (N);
9628
9629 -- Derivation of subprograms must be delayed until the full subtype
9630 -- has been established, to ensure proper overriding of subprograms
9631 -- inherited by full types. If the derivations occurred as part of
9632 -- the call to Build_Derived_Type above, then the check for type
9633 -- conformance would fail because earlier primitive subprograms
9634 -- could still refer to the full type prior the change to the new
9635 -- subtype and hence would not match the new base type created here.
9636 -- Subprograms are not derived, however, when Derive_Subps is False
9637 -- (since otherwise there could be redundant derivations).
9638
9639 if Derive_Subps then
9640 Derive_Subprograms (Parent_Type, Derived_Type);
9641 end if;
9642
9643 -- For tagged types the Discriminant_Constraint of the new base itype
9644 -- is inherited from the first subtype so that no subtype conformance
9645 -- problem arise when the first subtype overrides primitive
9646 -- operations inherited by the implicit base type.
9647
9648 if Is_Tagged then
9649 Set_Discriminant_Constraint
9650 (New_Base, Discriminant_Constraint (Derived_Type));
9651 end if;
9652
9653 return;
9654 end if;
9655
9656 -- If we get here Derived_Type will have no discriminants or it will be
9657 -- a discriminated unconstrained base type.
9658
9659 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9660
9661 if Is_Tagged then
9662
9663 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9664 -- The declaration of a specific descendant of an interface type
9665 -- freezes the interface type (RM 13.14).
9666
9667 if not Private_Extension or else Is_Interface (Parent_Base) then
9668 Freeze_Before (N, Parent_Type);
9669 end if;
9670
9671 if Ada_Version >= Ada_2005 then
9672 Check_Generic_Ancestors;
9673
9674 elsif Type_Access_Level (Derived_Type) /=
9675 Type_Access_Level (Parent_Type)
9676 and then not Is_Generic_Type (Derived_Type)
9677 then
9678 if Is_Controlled (Parent_Type) then
9679 Error_Msg_N
9680 ("controlled type must be declared at the library level",
9681 Indic);
9682 else
9683 Error_Msg_N
9684 ("type extension at deeper accessibility level than parent",
9685 Indic);
9686 end if;
9687
9688 else
9689 declare
9690 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9691 begin
9692 if Present (GB)
9693 and then GB /= Enclosing_Generic_Body (Parent_Base)
9694 then
9695 Error_Msg_NE
9696 ("parent type of& must not be outside generic body"
9697 & " (RM 3.9.1(4))",
9698 Indic, Derived_Type);
9699 end if;
9700 end;
9701 end if;
9702 end if;
9703
9704 -- Ada 2005 (AI-251)
9705
9706 if Ada_Version >= Ada_2005 and then Is_Tagged then
9707
9708 -- "The declaration of a specific descendant of an interface type
9709 -- freezes the interface type" (RM 13.14).
9710
9711 declare
9712 Iface : Node_Id;
9713 begin
9714 Iface := First (Interface_List (Type_Def));
9715 while Present (Iface) loop
9716 Freeze_Before (N, Etype (Iface));
9717 Next (Iface);
9718 end loop;
9719 end;
9720 end if;
9721
9722 -- STEP 1b : preliminary cleanup of the full view of private types
9723
9724 -- If the type is already marked as having discriminants, then it's the
9725 -- completion of a private type or private extension and we need to
9726 -- retain the discriminants from the partial view if the current
9727 -- declaration has Discriminant_Specifications so that we can verify
9728 -- conformance. However, we must remove any existing components that
9729 -- were inherited from the parent (and attached in Copy_And_Swap)
9730 -- because the full type inherits all appropriate components anyway, and
9731 -- we do not want the partial view's components interfering.
9732
9733 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9734 Discrim := First_Discriminant (Derived_Type);
9735 loop
9736 Last_Discrim := Discrim;
9737 Next_Discriminant (Discrim);
9738 exit when No (Discrim);
9739 end loop;
9740
9741 Set_Last_Entity (Derived_Type, Last_Discrim);
9742
9743 -- In all other cases wipe out the list of inherited components (even
9744 -- inherited discriminants), it will be properly rebuilt here.
9745
9746 else
9747 Set_First_Entity (Derived_Type, Empty);
9748 Set_Last_Entity (Derived_Type, Empty);
9749 end if;
9750
9751 -- STEP 1c: Initialize some flags for the Derived_Type
9752
9753 -- The following flags must be initialized here so that
9754 -- Process_Discriminants can check that discriminants of tagged types do
9755 -- not have a default initial value and that access discriminants are
9756 -- only specified for limited records. For completeness, these flags are
9757 -- also initialized along with all the other flags below.
9758
9759 -- AI-419: Limitedness is not inherited from an interface parent, so to
9760 -- be limited in that case the type must be explicitly declared as
9761 -- limited, or synchronized. While task and protected interfaces are
9762 -- always limited, a synchronized private extension might not inherit
9763 -- from such interfaces, and so we also need to recognize the
9764 -- explicit limitedness implied by a synchronized private extension
9765 -- that does not derive from a synchronized interface (see RM-7.3(6/2)).
9766
9767 if Limited_Present (Type_Def)
9768 or else Synchronized_Present (Type_Def)
9769 then
9770 Set_Is_Limited_Record (Derived_Type);
9771
9772 elsif Is_Limited_Record (Parent_Type)
9773 or else (Present (Full_View (Parent_Type))
9774 and then Is_Limited_Record (Full_View (Parent_Type)))
9775 then
9776 if not Is_Interface (Parent_Type)
9777 or else Is_Concurrent_Interface (Parent_Type)
9778 then
9779 Set_Is_Limited_Record (Derived_Type);
9780 end if;
9781 end if;
9782
9783 -- STEP 2a: process discriminants of derived type if any
9784
9785 Push_Scope (Derived_Type);
9786
9787 if Discriminant_Specs then
9788 Set_Has_Unknown_Discriminants (Derived_Type, False);
9789
9790 -- The following call to Check_Or_Process_Discriminants initializes
9791 -- fields Has_Discriminants and Discriminant_Constraint, unless we
9792 -- are processing the completion of a private type declaration.
9793 -- Temporarily set the state of the Derived_Type to "self-hidden"
9794 -- (see RM-8.3(17)), unless it is already the case.
9795
9796 if Is_Not_Self_Hidden (Derived_Type) then
9797 Set_Is_Not_Self_Hidden (Derived_Type, False);
9798 Check_Or_Process_Discriminants (N, Derived_Type);
9799 Set_Is_Not_Self_Hidden (Derived_Type);
9800 else
9801 Check_Or_Process_Discriminants (N, Derived_Type);
9802 end if;
9803
9804 -- For untagged types, the constraint on the Parent_Type must be
9805 -- present and is used to rename the discriminants.
9806
9807 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9808 Error_Msg_N ("untagged parent must have discriminants", Indic);
9809
9810 elsif not Is_Tagged and then not Constraint_Present then
9811 Error_Msg_N
9812 ("discriminant constraint needed for derived untagged records",
9813 Indic);
9814
9815 -- Otherwise the parent subtype must be constrained unless we have a
9816 -- private extension.
9817
9818 elsif not Constraint_Present
9819 and then not Private_Extension
9820 and then not Is_Constrained (Parent_Type)
9821 then
9822 Error_Msg_N
9823 ("unconstrained type not allowed in this context", Indic);
9824
9825 elsif Constraint_Present then
9826 -- The following call sets the field Corresponding_Discriminant
9827 -- for the discriminants in the Derived_Type.
9828
9829 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9830
9831 -- For untagged types all new discriminants must rename
9832 -- discriminants in the parent. For private extensions new
9833 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9834
9835 Discrim := First_Discriminant (Derived_Type);
9836 while Present (Discrim) loop
9837 if not Is_Tagged
9838 and then No (Corresponding_Discriminant (Discrim))
9839 then
9840 Error_Msg_N
9841 ("new discriminants must constrain old ones", Discrim);
9842
9843 elsif Private_Extension
9844 and then Present (Corresponding_Discriminant (Discrim))
9845 then
9846 Error_Msg_N
9847 ("only static constraints allowed for parent"
9848 & " discriminants in the partial view", Indic);
9849 exit;
9850 end if;
9851
9852 -- If a new discriminant is used in the constraint, then its
9853 -- subtype must be statically compatible with the subtype of
9854 -- the parent discriminant (RM 3.7(15)).
9855
9856 if Present (Corresponding_Discriminant (Discrim)) then
9857 Check_Constraining_Discriminant
9858 (Discrim, Corresponding_Discriminant (Discrim));
9859 end if;
9860
9861 Next_Discriminant (Discrim);
9862 end loop;
9863
9864 -- Check whether the constraints of the full view statically
9865 -- match those imposed by the parent subtype [7.3(13)].
9866
9867 if Present (Stored_Constraint (Derived_Type)) then
9868 declare
9869 C1, C2 : Elmt_Id;
9870
9871 begin
9872 C1 := First_Elmt (Discs);
9873 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9874 while Present (C1) and then Present (C2) loop
9875 if not
9876 Fully_Conformant_Expressions (Node (C1), Node (C2))
9877 then
9878 Error_Msg_N
9879 ("not conformant with previous declaration",
9880 Node (C1));
9881 end if;
9882
9883 Next_Elmt (C1);
9884 Next_Elmt (C2);
9885 end loop;
9886 end;
9887 end if;
9888 end if;
9889
9890 -- STEP 2b: No new discriminants, inherit discriminants if any
9891
9892 else
9893 if Private_Extension then
9894 Set_Has_Unknown_Discriminants
9895 (Derived_Type,
9896 Has_Unknown_Discriminants (Parent_Type)
9897 or else Unknown_Discriminants_Present (N));
9898
9899 -- The partial view of the parent may have unknown discriminants,
9900 -- but if the full view has discriminants and the parent type is
9901 -- in scope they must be inherited.
9902
9903 elsif Has_Unknown_Discriminants (Parent_Type)
9904 and then
9905 (not Has_Discriminants (Parent_Type)
9906 or else not In_Open_Scopes (Scope (Parent_Base)))
9907 then
9908 Set_Has_Unknown_Discriminants (Derived_Type);
9909 end if;
9910
9911 if not Has_Unknown_Discriminants (Derived_Type)
9912 and then not Has_Unknown_Discriminants (Parent_Base)
9913 and then Has_Discriminants (Parent_Type)
9914 then
9915 Inherit_Discrims := True;
9916 Set_Has_Discriminants
9917 (Derived_Type, True);
9918 Set_Discriminant_Constraint
9919 (Derived_Type, Discriminant_Constraint (Parent_Base));
9920 end if;
9921
9922 -- The following test is true for private types (remember
9923 -- transformation 5. is not applied to those) and in an error
9924 -- situation.
9925
9926 if Constraint_Present then
9927 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9928 end if;
9929
9930 -- For now mark a new derived type as constrained only if it has no
9931 -- discriminants. At the end of Build_Derived_Record_Type we properly
9932 -- set this flag in the case of private extensions. See comments in
9933 -- point 9. just before body of Build_Derived_Record_Type.
9934
9935 Set_Is_Constrained
9936 (Derived_Type,
9937 not (Inherit_Discrims
9938 or else Has_Unknown_Discriminants (Derived_Type)));
9939 end if;
9940
9941 -- STEP 3: initialize fields of derived type
9942
9943 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9944 Set_Stored_Constraint (Derived_Type, No_Elist);
9945
9946 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9947 -- but cannot be interfaces
9948
9949 if not Private_Extension
9950 and then Ekind (Derived_Type) /= E_Private_Type
9951 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9952 then
9953 if Interface_Present (Type_Def) then
9954 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9955 end if;
9956
9957 Set_Interfaces (Derived_Type, No_Elist);
9958 end if;
9959
9960 -- Fields inherited from the Parent_Type
9961
9962 Set_Has_Specified_Layout
9963 (Derived_Type, Has_Specified_Layout (Parent_Type));
9964 Set_Is_Limited_Composite
9965 (Derived_Type, Is_Limited_Composite (Parent_Type));
9966 Set_Is_Private_Composite
9967 (Derived_Type, Is_Private_Composite (Parent_Type));
9968
9969 if Is_Tagged_Type (Parent_Type) then
9970 Set_No_Tagged_Streams_Pragma
9971 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9972 end if;
9973
9974 -- Fields inherited from the Parent_Base
9975
9976 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9977 Propagate_Controlled_Flags (Derived_Type, Parent_Base, Deriv => True);
9978
9979 Set_Has_Non_Standard_Rep
9980 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9981 Set_Has_Primitive_Operations
9982 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9983
9984 -- Set fields for private derived types
9985
9986 if Is_Private_Type (Derived_Type) then
9987 Set_Depends_On_Private (Derived_Type, True);
9988 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9989 end if;
9990
9991 -- Inherit fields for non-private types. If this is the completion of a
9992 -- derivation from a private type, the parent itself is private and the
9993 -- attributes come from its full view, which must be present.
9994
9995 if Is_Record_Type (Derived_Type) then
9996 declare
9997 Parent_Full : Entity_Id;
9998
9999 begin
10000 if Is_Private_Type (Parent_Base)
10001 and then not Is_Record_Type (Parent_Base)
10002 then
10003 Parent_Full := Full_View (Parent_Base);
10004 else
10005 Parent_Full := Parent_Base;
10006 end if;
10007
10008 Set_Component_Alignment
10009 (Derived_Type, Component_Alignment (Parent_Full));
10010 Set_C_Pass_By_Copy
10011 (Derived_Type, C_Pass_By_Copy (Parent_Full));
10012 Set_Has_Complex_Representation
10013 (Derived_Type, Has_Complex_Representation (Parent_Full));
10014
10015 -- For untagged types, inherit the layout by default to avoid
10016 -- costly changes of representation for type conversions.
10017
10018 if not Is_Tagged then
10019 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
10020 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
10021 end if;
10022 end;
10023 end if;
10024
10025 -- Initialize the list of primitive operations to an empty list,
10026 -- to cover tagged types as well as untagged types. For untagged
10027 -- types this is used either to analyze the call as legal when
10028 -- GNAT extensions are allowed, or to give better error messages.
10029
10030 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10031
10032 -- Set fields for tagged types
10033
10034 if Is_Tagged then
10035 -- Minor optimization: there is no need to generate the class-wide
10036 -- entity associated with an underlying record view.
10037
10038 if not Is_Underlying_Record_View (Derived_Type) then
10039 Make_Class_Wide_Type (Derived_Type);
10040 end if;
10041
10042 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
10043
10044 if Has_Discriminants (Derived_Type)
10045 and then Constraint_Present
10046 then
10047 Set_Stored_Constraint
10048 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
10049 end if;
10050
10051 if Ada_Version >= Ada_2005 then
10052 declare
10053 Ifaces_List : Elist_Id;
10054
10055 begin
10056 -- Checks rules 3.9.4 (13/2 and 14/2)
10057
10058 if Comes_From_Source (Derived_Type)
10059 and then not Is_Private_Type (Derived_Type)
10060 and then Is_Interface (Parent_Type)
10061 and then not Is_Interface (Derived_Type)
10062 then
10063 if Is_Task_Interface (Parent_Type) then
10064 Error_Msg_N
10065 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
10066 Derived_Type);
10067
10068 elsif Is_Protected_Interface (Parent_Type) then
10069 Error_Msg_N
10070 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
10071 Derived_Type);
10072 end if;
10073 end if;
10074
10075 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
10076
10077 Check_Interfaces (N, Type_Def);
10078
10079 -- Ada 2005 (AI-251): Collect the list of progenitors that are
10080 -- not already in the parents.
10081
10082 Collect_Interfaces
10083 (T => Derived_Type,
10084 Ifaces_List => Ifaces_List,
10085 Exclude_Parents => True);
10086
10087 Set_Interfaces (Derived_Type, Ifaces_List);
10088
10089 -- If the derived type is the anonymous type created for
10090 -- a declaration whose parent has a constraint, propagate
10091 -- the interface list to the source type. This must be done
10092 -- prior to the completion of the analysis of the source type
10093 -- because the components in the extension may contain current
10094 -- instances whose legality depends on some ancestor.
10095
10096 if Is_Itype (Derived_Type) then
10097 declare
10098 Def : constant Node_Id :=
10099 Associated_Node_For_Itype (Derived_Type);
10100 begin
10101 if Present (Def)
10102 and then Nkind (Def) = N_Full_Type_Declaration
10103 then
10104 Set_Interfaces
10105 (Defining_Identifier (Def), Ifaces_List);
10106 end if;
10107 end;
10108 end if;
10109
10110 -- A type extension is automatically Ghost when one of its
10111 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
10112 -- also inherited when the parent type is Ghost, but this is
10113 -- done in Build_Derived_Type as the mechanism also handles
10114 -- untagged derivations.
10115
10116 if Implements_Ghost_Interface (Derived_Type) then
10117 Set_Is_Ghost_Entity (Derived_Type);
10118 end if;
10119 end;
10120 end if;
10121 end if;
10122
10123 -- STEP 4: Inherit components from the parent base and constrain them.
10124 -- Apply the second transformation described in point 6. above.
10125
10126 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
10127 or else not Has_Discriminants (Parent_Type)
10128 or else not Is_Constrained (Parent_Type)
10129 then
10130 Constrs := Discs;
10131 else
10132 Constrs := Discriminant_Constraint (Parent_Type);
10133 end if;
10134
10135 Assoc_List :=
10136 Inherit_Components
10137 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
10138
10139 -- STEP 5a: Copy the parent record declaration for untagged types
10140
10141 Set_Has_Implicit_Dereference
10142 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
10143
10144 if not Is_Tagged then
10145
10146 -- Discriminant_Constraint (Derived_Type) has been properly
10147 -- constructed. Save it and temporarily set it to Empty because we
10148 -- do not want the call to New_Copy_Tree below to mess this list.
10149
10150 if Has_Discriminants (Derived_Type) then
10151 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
10152 Set_Discriminant_Constraint (Derived_Type, No_Elist);
10153 else
10154 Save_Discr_Constr := No_Elist;
10155 end if;
10156
10157 -- Save the Etype field of Derived_Type. It is correctly set now,
10158 -- but the call to New_Copy tree may remap it to point to itself,
10159 -- which is not what we want. Ditto for the Next_Entity field.
10160
10161 Save_Etype := Etype (Derived_Type);
10162 Save_Next_Entity := Next_Entity (Derived_Type);
10163
10164 -- Assoc_List maps all stored discriminants in the Parent_Base to
10165 -- stored discriminants in the Derived_Type. It is fundamental that
10166 -- no types or itypes with discriminants other than the stored
10167 -- discriminants appear in the entities declared inside
10168 -- Derived_Type, since the back end cannot deal with it.
10169
10170 New_Decl :=
10171 New_Copy_Tree
10172 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
10173 Copy_Dimensions_Of_Components (Derived_Type);
10174
10175 -- Restore the fields saved prior to the New_Copy_Tree call
10176 -- and compute the stored constraint.
10177
10178 Set_Etype (Derived_Type, Save_Etype);
10179 Link_Entities (Derived_Type, Save_Next_Entity);
10180
10181 if Has_Discriminants (Derived_Type) then
10182 Set_Discriminant_Constraint
10183 (Derived_Type, Save_Discr_Constr);
10184 Set_Stored_Constraint
10185 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
10186
10187 Replace_Discriminants (Derived_Type, New_Decl);
10188 end if;
10189
10190 -- Relocate the aspects from the original type
10191
10192 Remove_Aspects (New_Decl);
10193 Move_Aspects (N, New_Decl);
10194
10195 -- Insert the new derived type declaration
10196
10197 Rewrite (N, New_Decl);
10198
10199 -- STEP 5b: Complete the processing for record extensions in generics
10200
10201 -- There is no completion for record extensions declared in the
10202 -- parameter part of a generic, so we need to complete processing for
10203 -- these generic record extensions here. Record_Type_Definition will
10204 -- set the Is_Not_Self_Hidden flag.
10205
10206 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
10207 Record_Type_Definition (Empty, Derived_Type);
10208
10209 -- STEP 5c: Process the record extension for non private tagged types
10210
10211 elsif not Private_Extension then
10212 Expand_Record_Extension (Derived_Type, Type_Def);
10213
10214 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
10215 -- implemented interfaces if we are in expansion mode
10216
10217 if Expander_Active
10218 and then Has_Interfaces (Derived_Type)
10219 then
10220 Add_Interface_Tag_Components (N, Derived_Type);
10221 end if;
10222
10223 -- Analyze the record extension
10224
10225 Record_Type_Definition
10226 (Record_Extension_Part (Type_Def), Derived_Type);
10227 end if;
10228
10229 End_Scope;
10230
10231 -- Nothing else to do if there is an error in the derivation.
10232 -- An unusual case: the full view may be derived from a type in an
10233 -- instance, when the partial view was used illegally as an actual
10234 -- in that instance, leading to a circular definition.
10235
10236 if Etype (Derived_Type) = Any_Type
10237 or else Etype (Parent_Type) = Derived_Type
10238 then
10239 return;
10240 end if;
10241
10242 -- Set delayed freeze and then derive subprograms, we need to do
10243 -- this in this order so that derived subprograms inherit the
10244 -- derived freeze if necessary.
10245
10246 Set_Has_Delayed_Freeze (Derived_Type);
10247
10248 if Derive_Subps then
10249 Derive_Subprograms (Parent_Type, Derived_Type);
10250 end if;
10251
10252 -- If we have a private extension which defines a constrained derived
10253 -- type mark as constrained here after we have derived subprograms. See
10254 -- comment on point 9. just above the body of Build_Derived_Record_Type.
10255
10256 if Private_Extension and then Inherit_Discrims then
10257 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
10258 Set_Is_Constrained (Derived_Type, True);
10259 Set_Discriminant_Constraint (Derived_Type, Discs);
10260
10261 elsif Is_Constrained (Parent_Type) then
10262 Set_Is_Constrained
10263 (Derived_Type, True);
10264 Set_Discriminant_Constraint
10265 (Derived_Type, Discriminant_Constraint (Parent_Type));
10266 end if;
10267 end if;
10268
10269 -- Update the class-wide type, which shares the now-completed entity
10270 -- list with its specific type. In case of underlying record views,
10271 -- we do not generate the corresponding class wide entity.
10272
10273 if Is_Tagged
10274 and then not Is_Underlying_Record_View (Derived_Type)
10275 then
10276 Set_First_Entity
10277 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
10278 Set_Last_Entity
10279 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
10280 end if;
10281
10282 Check_Function_Writable_Actuals (N);
10283 end Build_Derived_Record_Type;
10284
10285 ------------------------
10286 -- Build_Derived_Type --
10287 ------------------------
10288
10289 procedure Build_Derived_Type
10290 (N : Node_Id;
10291 Parent_Type : Entity_Id;
10292 Derived_Type : Entity_Id;
10293 Is_Completion : Boolean;
10294 Derive_Subps : Boolean := True)
10295 is
10296 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10297
10298 begin
10299 -- Set common attributes
10300
10301 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10302 and then Ekind (Parent_Base) in Elementary_Kind
10303 then
10304 Reinit_Field_To_Zero (Derived_Type, F_Discriminant_Constraint);
10305 end if;
10306
10307 Set_Scope (Derived_Type, Current_Scope);
10308 Set_Etype (Derived_Type, Parent_Base);
10309 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10310
10311 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10312 Propagate_Controlled_Flags (Derived_Type, Parent_Base, Deriv => True);
10313
10314 Set_Size_Info (Derived_Type, Parent_Type);
10315 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10316
10317 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10318 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10319
10320 if Is_Tagged_Type (Derived_Type) then
10321 Set_No_Tagged_Streams_Pragma
10322 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10323 end if;
10324
10325 -- If the parent has primitive routines and may have not-seen-yet aspect
10326 -- specifications (e.g., a Pack pragma), then set the derived type link
10327 -- in order to later diagnose "early derivation" issues. If in different
10328 -- compilation units, then "early derivation" cannot be an issue (and we
10329 -- don't like interunit references that go in the opposite direction of
10330 -- semantic dependencies).
10331
10332 if Has_Primitive_Operations (Parent_Type)
10333 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10334 Enclosing_Comp_Unit_Node (Derived_Type)
10335 then
10336 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10337 end if;
10338
10339 -- If the parent type is a private subtype, the convention on the base
10340 -- type may be set in the private part, and not propagated to the
10341 -- subtype until later, so we obtain the convention from the base type.
10342
10343 Set_Convention (Derived_Type, Convention (Parent_Base));
10344
10345 if Is_Tagged_Type (Derived_Type)
10346 and then Present (Class_Wide_Type (Derived_Type))
10347 then
10348 Set_Convention (Class_Wide_Type (Derived_Type),
10349 Convention (Class_Wide_Type (Parent_Base)));
10350 end if;
10351
10352 -- Set SSO default for record or array type
10353
10354 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10355 and then Is_Base_Type (Derived_Type)
10356 then
10357 Set_Default_SSO (Derived_Type);
10358 end if;
10359
10360 -- A derived type inherits the Default_Initial_Condition pragma coming
10361 -- from any parent type within the derivation chain.
10362
10363 if Has_DIC (Parent_Type) then
10364 Set_Has_Inherited_DIC (Derived_Type);
10365 end if;
10366
10367 -- A derived type inherits any class-wide invariants coming from a
10368 -- parent type or an interface. Note that the invariant procedure of
10369 -- the parent type should not be inherited because the derived type may
10370 -- define invariants of its own.
10371
10372 if not Is_Interface (Derived_Type) then
10373 if Has_Inherited_Invariants (Parent_Type)
10374 or else Has_Inheritable_Invariants (Parent_Type)
10375 then
10376 Set_Has_Inherited_Invariants (Derived_Type);
10377
10378 elsif Is_Concurrent_Type (Derived_Type)
10379 or else Is_Tagged_Type (Derived_Type)
10380 then
10381 declare
10382 Iface : Entity_Id;
10383 Ifaces : Elist_Id;
10384 Iface_Elmt : Elmt_Id;
10385
10386 begin
10387 Collect_Interfaces
10388 (T => Derived_Type,
10389 Ifaces_List => Ifaces,
10390 Exclude_Parents => True);
10391
10392 if Present (Ifaces) then
10393 Iface_Elmt := First_Elmt (Ifaces);
10394 while Present (Iface_Elmt) loop
10395 Iface := Node (Iface_Elmt);
10396
10397 if Has_Inheritable_Invariants (Iface) then
10398 Set_Has_Inherited_Invariants (Derived_Type);
10399 exit;
10400 end if;
10401
10402 Next_Elmt (Iface_Elmt);
10403 end loop;
10404 end if;
10405 end;
10406 end if;
10407 end if;
10408
10409 -- We similarly inherit predicates
10410
10411 Inherit_Predicate_Flags (Derived_Type, Parent_Type, Only_Flags => True);
10412
10413 -- The derived type inherits representation clauses from the parent
10414 -- type, and from any interfaces.
10415
10416 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10417
10418 declare
10419 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10420 begin
10421 while Present (Iface) loop
10422 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10423 Next (Iface);
10424 end loop;
10425 end;
10426
10427 -- If the parent type has delayed rep aspects, then mark the derived
10428 -- type as possibly inheriting a delayed rep aspect.
10429
10430 if Has_Delayed_Rep_Aspects (Parent_Type) then
10431 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10432 end if;
10433
10434 -- A derived type becomes Ghost when its parent type is also Ghost
10435 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10436 -- directly inherited because the Ghost policy in effect may differ.
10437
10438 if Is_Ghost_Entity (Parent_Type) then
10439 Set_Is_Ghost_Entity (Derived_Type);
10440 end if;
10441
10442 -- Type dependent processing
10443
10444 case Ekind (Parent_Type) is
10445 when Numeric_Kind =>
10446 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10447
10448 when Array_Kind =>
10449 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10450
10451 when Class_Wide_Kind
10452 | E_Record_Subtype
10453 | E_Record_Type
10454 =>
10455 Build_Derived_Record_Type
10456 (N, Parent_Type, Derived_Type, Derive_Subps);
10457 return;
10458
10459 when Enumeration_Kind =>
10460 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10461
10462 when Access_Kind =>
10463 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10464
10465 when Incomplete_Or_Private_Kind =>
10466 Build_Derived_Private_Type
10467 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10468
10469 -- For discriminated types, the derivation includes deriving
10470 -- primitive operations. For others it is done below.
10471
10472 if Is_Tagged_Type (Parent_Type)
10473 or else Has_Discriminants (Parent_Type)
10474 or else (Present (Full_View (Parent_Type))
10475 and then Has_Discriminants (Full_View (Parent_Type)))
10476 then
10477 return;
10478 end if;
10479
10480 when Concurrent_Kind =>
10481 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10482
10483 when others =>
10484 raise Program_Error;
10485 end case;
10486
10487 -- Nothing more to do if some error occurred
10488
10489 if Etype (Derived_Type) = Any_Type then
10490 return;
10491 end if;
10492
10493 -- If not already set, initialize the derived type's list of primitive
10494 -- operations to an empty element list.
10495
10496 if No (Direct_Primitive_Operations (Derived_Type)) then
10497 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10498
10499 -- If Etype of the derived type is the base type (as opposed to
10500 -- a parent type) and doesn't have an associated list of primitive
10501 -- operations, then set the base type's primitive list to the
10502 -- derived type's list. The lists need to be shared in common
10503 -- between the two.
10504
10505 if Etype (Derived_Type) = Base_Type (Derived_Type)
10506 and then No (Direct_Primitive_Operations (Etype (Derived_Type)))
10507 then
10508 Set_Direct_Primitive_Operations
10509 (Etype (Derived_Type),
10510 Direct_Primitive_Operations (Derived_Type));
10511 end if;
10512 end if;
10513
10514 -- Set delayed freeze and then derive subprograms, we need to do this
10515 -- in this order so that derived subprograms inherit the derived freeze
10516 -- if necessary.
10517
10518 Set_Has_Delayed_Freeze (Derived_Type);
10519
10520 if Derive_Subps then
10521 Derive_Subprograms (Parent_Type, Derived_Type);
10522 end if;
10523
10524 Set_Has_Primitive_Operations
10525 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10526 end Build_Derived_Type;
10527
10528 -----------------------
10529 -- Build_Discriminal --
10530 -----------------------
10531
10532 procedure Build_Discriminal (Discrim : Entity_Id) is
10533 D_Minal : Entity_Id;
10534 CR_Disc : Entity_Id;
10535
10536 begin
10537 -- A discriminal has the same name as the discriminant
10538
10539 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10540
10541 Mutate_Ekind (D_Minal, E_In_Parameter);
10542 Set_Mechanism (D_Minal, Default_Mechanism);
10543 Set_Etype (D_Minal, Etype (Discrim));
10544 Set_Scope (D_Minal, Current_Scope);
10545 Set_Parent (D_Minal, Parent (Discrim));
10546
10547 Set_Discriminal (Discrim, D_Minal);
10548 Set_Discriminal_Link (D_Minal, Discrim);
10549
10550 -- For task types, build at once the discriminants of the corresponding
10551 -- record, which are needed if discriminants are used in entry defaults
10552 -- and in family bounds.
10553
10554 if Is_Concurrent_Type (Current_Scope)
10555 or else
10556 Is_Limited_Type (Current_Scope)
10557 then
10558 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10559
10560 Mutate_Ekind (CR_Disc, E_In_Parameter);
10561 Set_Mechanism (CR_Disc, Default_Mechanism);
10562 Set_Etype (CR_Disc, Etype (Discrim));
10563 Set_Scope (CR_Disc, Current_Scope);
10564 Set_Discriminal_Link (CR_Disc, Discrim);
10565 Set_CR_Discriminant (Discrim, CR_Disc);
10566 end if;
10567 end Build_Discriminal;
10568
10569 ------------------------------------
10570 -- Build_Discriminant_Constraints --
10571 ------------------------------------
10572
10573 function Build_Discriminant_Constraints
10574 (T : Entity_Id;
10575 Def : Node_Id;
10576 Derived_Def : Boolean := False) return Elist_Id
10577 is
10578 C : constant Node_Id := Constraint (Def);
10579 Nb_Discr : constant Nat := Number_Discriminants (T);
10580
10581 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10582 -- Saves the expression corresponding to a given discriminant in T
10583
10584 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10585 -- Return the Position number within array Discr_Expr of a discriminant
10586 -- D within the discriminant list of the discriminated type T.
10587
10588 procedure Process_Discriminant_Expression
10589 (Expr : Node_Id;
10590 D : Entity_Id);
10591 -- If this is a discriminant constraint on a partial view, do not
10592 -- generate an overflow check on the discriminant expression. The check
10593 -- will be generated when constraining the full view. Otherwise the
10594 -- backend creates duplicate symbols for the temporaries corresponding
10595 -- to the expressions to be checked, causing spurious assembler errors.
10596
10597 ------------------
10598 -- Pos_Of_Discr --
10599 ------------------
10600
10601 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10602 Disc : Entity_Id;
10603
10604 begin
10605 Disc := First_Discriminant (T);
10606 for J in Discr_Expr'Range loop
10607 if Disc = D then
10608 return J;
10609 end if;
10610
10611 Next_Discriminant (Disc);
10612 end loop;
10613
10614 -- Note: Since this function is called on discriminants that are
10615 -- known to belong to the discriminated type, falling through the
10616 -- loop with no match signals an internal compiler error.
10617
10618 raise Program_Error;
10619 end Pos_Of_Discr;
10620
10621 -------------------------------------
10622 -- Process_Discriminant_Expression --
10623 -------------------------------------
10624
10625 procedure Process_Discriminant_Expression
10626 (Expr : Node_Id;
10627 D : Entity_Id)
10628 is
10629 BDT : constant Entity_Id := Base_Type (Etype (D));
10630
10631 begin
10632 -- If this is a discriminant constraint on a partial view, do
10633 -- not generate an overflow on the discriminant expression. The
10634 -- check will be generated when constraining the full view.
10635
10636 if Is_Private_Type (T)
10637 and then Present (Full_View (T))
10638 then
10639 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10640 else
10641 Analyze_And_Resolve (Expr, BDT);
10642 end if;
10643 end Process_Discriminant_Expression;
10644
10645 -- Declarations local to Build_Discriminant_Constraints
10646
10647 Discr : Entity_Id;
10648 E : Entity_Id;
10649 Elist : constant Elist_Id := New_Elmt_List;
10650
10651 Constr : Node_Id;
10652 Expr : Node_Id;
10653 Id : Node_Id;
10654 Position : Nat;
10655 Found : Boolean;
10656
10657 Discrim_Present : Boolean := False;
10658
10659 -- Start of processing for Build_Discriminant_Constraints
10660
10661 begin
10662 -- The following loop will process positional associations only.
10663 -- For a positional association, the (single) discriminant is
10664 -- implicitly specified by position, in textual order (RM 3.7.2).
10665
10666 Discr := First_Discriminant (T);
10667 Constr := First (Constraints (C));
10668 for D in Discr_Expr'Range loop
10669 exit when Nkind (Constr) = N_Discriminant_Association;
10670
10671 if No (Constr) then
10672 Error_Msg_N ("too few discriminants given in constraint", C);
10673 return New_Elmt_List;
10674
10675 elsif Nkind (Constr) = N_Range
10676 or else (Nkind (Constr) = N_Attribute_Reference
10677 and then Attribute_Name (Constr) = Name_Range)
10678 then
10679 Error_Msg_N
10680 ("a range is not a valid discriminant constraint", Constr);
10681 Discr_Expr (D) := Error;
10682
10683 elsif Nkind (Constr) = N_Subtype_Indication then
10684 Error_Msg_N
10685 ("a subtype indication is not a valid discriminant constraint",
10686 Constr);
10687 Discr_Expr (D) := Error;
10688
10689 else
10690 Process_Discriminant_Expression (Constr, Discr);
10691 Discr_Expr (D) := Constr;
10692 end if;
10693
10694 Next_Discriminant (Discr);
10695 Next (Constr);
10696 end loop;
10697
10698 if No (Discr) and then Present (Constr) then
10699 Error_Msg_N ("too many discriminants given in constraint", Constr);
10700 return New_Elmt_List;
10701 end if;
10702
10703 -- Named associations can be given in any order, but if both positional
10704 -- and named associations are used in the same discriminant constraint,
10705 -- then positional associations must occur first, at their normal
10706 -- position. Hence once a named association is used, the rest of the
10707 -- discriminant constraint must use only named associations.
10708
10709 while Present (Constr) loop
10710
10711 -- Positional association forbidden after a named association
10712
10713 if Nkind (Constr) /= N_Discriminant_Association then
10714 Error_Msg_N ("positional association follows named one", Constr);
10715 return New_Elmt_List;
10716
10717 -- Otherwise it is a named association
10718
10719 else
10720 -- E records the type of the discriminants in the named
10721 -- association. All the discriminants specified in the same name
10722 -- association must have the same type.
10723
10724 E := Empty;
10725
10726 -- Search the list of discriminants in T to see if the simple name
10727 -- given in the constraint matches any of them.
10728
10729 Id := First (Selector_Names (Constr));
10730 while Present (Id) loop
10731 Found := False;
10732
10733 -- If Original_Discriminant is present, we are processing a
10734 -- generic instantiation and this is an instance node. We need
10735 -- to find the name of the corresponding discriminant in the
10736 -- actual record type T and not the name of the discriminant in
10737 -- the generic formal. Example:
10738
10739 -- generic
10740 -- type G (D : int) is private;
10741 -- package P is
10742 -- subtype W is G (D => 1);
10743 -- end package;
10744 -- type Rec (X : int) is record ... end record;
10745 -- package Q is new P (G => Rec);
10746
10747 -- At the point of the instantiation, formal type G is Rec
10748 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10749 -- which really looks like "subtype W is Rec (D => 1);" at
10750 -- the point of instantiation, we want to find the discriminant
10751 -- that corresponds to D in Rec, i.e. X.
10752
10753 if Present (Original_Discriminant (Id))
10754 and then In_Instance
10755 then
10756 Discr := Find_Corresponding_Discriminant (Id, T);
10757 Found := True;
10758
10759 else
10760 Discr := First_Discriminant (T);
10761 while Present (Discr) loop
10762 if Chars (Discr) = Chars (Id) then
10763 Found := True;
10764 exit;
10765 end if;
10766
10767 Next_Discriminant (Discr);
10768 end loop;
10769
10770 if not Found then
10771 Error_Msg_N ("& does not match any discriminant", Id);
10772 return New_Elmt_List;
10773
10774 -- If the parent type is a generic formal, preserve the
10775 -- name of the discriminant for subsequent instances.
10776 -- see comment at the beginning of this if statement.
10777
10778 elsif Is_Generic_Type (Root_Type (T)) then
10779 Set_Original_Discriminant (Id, Discr);
10780 end if;
10781 end if;
10782
10783 Position := Pos_Of_Discr (T, Discr);
10784
10785 if Present (Discr_Expr (Position)) then
10786 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10787
10788 else
10789 -- Each discriminant specified in the same named association
10790 -- must be associated with a separate copy of the
10791 -- corresponding expression.
10792
10793 if Present (Next (Id)) then
10794 Expr := New_Copy_Tree (Expression (Constr));
10795 Set_Parent (Expr, Parent (Expression (Constr)));
10796 else
10797 Expr := Expression (Constr);
10798 end if;
10799
10800 Discr_Expr (Position) := Expr;
10801 Process_Discriminant_Expression (Expr, Discr);
10802 end if;
10803
10804 -- A discriminant association with more than one discriminant
10805 -- name is only allowed if the named discriminants are all of
10806 -- the same type (RM 3.7.1(8)).
10807
10808 if E = Empty then
10809 E := Base_Type (Etype (Discr));
10810
10811 elsif Base_Type (Etype (Discr)) /= E then
10812 Error_Msg_N
10813 ("all discriminants in an association " &
10814 "must have the same type", Id);
10815 end if;
10816
10817 Next (Id);
10818 end loop;
10819 end if;
10820
10821 Next (Constr);
10822 end loop;
10823
10824 -- A discriminant constraint must provide exactly one value for each
10825 -- discriminant of the type (RM 3.7.1(8)).
10826
10827 for J in Discr_Expr'Range loop
10828 if No (Discr_Expr (J)) then
10829 Error_Msg_N ("too few discriminants given in constraint", C);
10830 return New_Elmt_List;
10831 end if;
10832 end loop;
10833
10834 -- Determine if there are discriminant expressions in the constraint
10835
10836 for J in Discr_Expr'Range loop
10837 if Denotes_Discriminant
10838 (Discr_Expr (J), Check_Concurrent => True)
10839 then
10840 Discrim_Present := True;
10841 exit;
10842 end if;
10843 end loop;
10844
10845 -- Build an element list consisting of the expressions given in the
10846 -- discriminant constraint and apply the appropriate checks. The list
10847 -- is constructed after resolving any named discriminant associations
10848 -- and therefore the expressions appear in the textual order of the
10849 -- discriminants.
10850
10851 Discr := First_Discriminant (T);
10852 for J in Discr_Expr'Range loop
10853 if Discr_Expr (J) /= Error then
10854 Append_Elmt (Discr_Expr (J), Elist);
10855
10856 -- If any of the discriminant constraints is given by a
10857 -- discriminant and we are in a derived type declaration we
10858 -- have a discriminant renaming. Establish link between new
10859 -- and old discriminant. The new discriminant has an implicit
10860 -- dereference if the old one does.
10861
10862 if Denotes_Discriminant (Discr_Expr (J)) then
10863 if Derived_Def then
10864 declare
10865 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10866
10867 begin
10868 Set_Corresponding_Discriminant (New_Discr, Discr);
10869 Set_Has_Implicit_Dereference (New_Discr,
10870 Has_Implicit_Dereference (Discr));
10871 end;
10872 end if;
10873
10874 -- Force the evaluation of non-discriminant expressions.
10875 -- If we have found a discriminant in the constraint 3.4(26)
10876 -- and 3.8(18) demand that no range checks are performed are
10877 -- after evaluation. If the constraint is for a component
10878 -- definition that has a per-object constraint, expressions are
10879 -- evaluated but not checked either. In all other cases perform
10880 -- a range check.
10881
10882 else
10883 if Discrim_Present then
10884 null;
10885
10886 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10887 and then Has_Per_Object_Constraint
10888 (Defining_Identifier (Parent (Parent (Def))))
10889 then
10890 null;
10891
10892 elsif Is_Access_Type (Etype (Discr)) then
10893 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10894
10895 else
10896 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10897 end if;
10898
10899 -- If the value of the discriminant may be visible in
10900 -- another unit or child unit, create an external name
10901 -- for it. We use the name of the object or component
10902 -- that carries the discriminated subtype. The code
10903 -- below may generate external symbols for the discriminant
10904 -- expression when not strictly needed, which is harmless.
10905
10906 if Expander_Active
10907 and then Comes_From_Source (Def)
10908 and then not Is_Subprogram (Current_Scope)
10909 then
10910 declare
10911 Id : Entity_Id := Empty;
10912 begin
10913 if Nkind (Parent (Def)) = N_Object_Declaration then
10914 Id := Defining_Identifier (Parent (Def));
10915
10916 elsif Nkind (Parent (Def)) = N_Component_Definition
10917 and then
10918 Nkind (Parent (Parent (Def)))
10919 = N_Component_Declaration
10920 then
10921 Id := Defining_Identifier (Parent (Parent (Def)));
10922 end if;
10923
10924 if Present (Id) then
10925 Force_Evaluation (
10926 Discr_Expr (J),
10927 Related_Id => Id,
10928 Discr_Number => J);
10929 else
10930 Force_Evaluation (Discr_Expr (J));
10931 end if;
10932 end;
10933 else
10934 Force_Evaluation (Discr_Expr (J));
10935 end if;
10936 end if;
10937
10938 -- Check that the designated type of an access discriminant's
10939 -- expression is not a class-wide type unless the discriminant's
10940 -- designated type is also class-wide.
10941
10942 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10943 and then not Is_Class_Wide_Type
10944 (Designated_Type (Etype (Discr)))
10945 and then Etype (Discr_Expr (J)) /= Any_Type
10946 and then Is_Class_Wide_Type
10947 (Designated_Type (Etype (Discr_Expr (J))))
10948 then
10949 Wrong_Type (Discr_Expr (J), Etype (Discr));
10950
10951 elsif Is_Access_Type (Etype (Discr))
10952 and then not Is_Access_Constant (Etype (Discr))
10953 and then Is_Access_Type (Etype (Discr_Expr (J)))
10954 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10955 then
10956 Error_Msg_NE
10957 ("constraint for discriminant& must be access to variable",
10958 Def, Discr);
10959 end if;
10960 end if;
10961
10962 Next_Discriminant (Discr);
10963 end loop;
10964
10965 return Elist;
10966 end Build_Discriminant_Constraints;
10967
10968 ---------------------------------
10969 -- Build_Discriminated_Subtype --
10970 ---------------------------------
10971
10972 procedure Build_Discriminated_Subtype
10973 (T : Entity_Id;
10974 Def_Id : Entity_Id;
10975 Elist : Elist_Id;
10976 Related_Nod : Node_Id;
10977 For_Access : Boolean := False)
10978 is
10979 Has_Discrs : constant Boolean := Has_Discriminants (T);
10980 Constrained : constant Boolean :=
10981 (Has_Discrs
10982 and then not Is_Empty_Elmt_List (Elist)
10983 and then not Is_Class_Wide_Type (T))
10984 or else Is_Constrained (T);
10985
10986 begin
10987 if Ekind (T) = E_Record_Type then
10988 Mutate_Ekind (Def_Id, E_Record_Subtype);
10989
10990 -- Inherit preelaboration flag from base, for types for which it
10991 -- may have been set: records, private types, protected types.
10992
10993 Set_Known_To_Have_Preelab_Init
10994 (Def_Id, Known_To_Have_Preelab_Init (T));
10995
10996 elsif Ekind (T) = E_Task_Type then
10997 Mutate_Ekind (Def_Id, E_Task_Subtype);
10998
10999 elsif Ekind (T) = E_Protected_Type then
11000 Mutate_Ekind (Def_Id, E_Protected_Subtype);
11001 Set_Known_To_Have_Preelab_Init
11002 (Def_Id, Known_To_Have_Preelab_Init (T));
11003
11004 elsif Is_Private_Type (T) then
11005 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11006 Set_Known_To_Have_Preelab_Init
11007 (Def_Id, Known_To_Have_Preelab_Init (T));
11008
11009 -- Private subtypes may have private dependents
11010
11011 Set_Private_Dependents (Def_Id, New_Elmt_List);
11012
11013 elsif Is_Class_Wide_Type (T) then
11014 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
11015
11016 else
11017 -- Incomplete type. Attach subtype to list of dependents, to be
11018 -- completed with full view of parent type, unless is it the
11019 -- designated subtype of a record component within an init_proc.
11020 -- This last case arises for a component of an access type whose
11021 -- designated type is incomplete (e.g. a Taft Amendment type).
11022 -- The designated subtype is within an inner scope, and needs no
11023 -- elaboration, because only the access type is needed in the
11024 -- initialization procedure.
11025
11026 if Ekind (T) = E_Incomplete_Type then
11027 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
11028 else
11029 Mutate_Ekind (Def_Id, Ekind (T));
11030 end if;
11031
11032 if For_Access and then Within_Init_Proc then
11033 null;
11034 else
11035 Append_Elmt (Def_Id, Private_Dependents (T));
11036 end if;
11037 end if;
11038
11039 Set_Etype (Def_Id, T);
11040 Reinit_Size_Align (Def_Id);
11041 Set_Has_Discriminants (Def_Id, Has_Discrs);
11042 Set_Is_Constrained (Def_Id, Constrained);
11043
11044 Set_First_Entity (Def_Id, First_Entity (T));
11045 Set_Last_Entity (Def_Id, Last_Entity (T));
11046 Set_Has_Implicit_Dereference
11047 (Def_Id, Has_Implicit_Dereference (T));
11048 Set_Has_Pragma_Unreferenced_Objects
11049 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
11050
11051 -- If the subtype is the completion of a private declaration, there may
11052 -- have been representation clauses for the partial view, and they must
11053 -- be preserved. Build_Derived_Type chains the inherited clauses with
11054 -- the ones appearing on the extension. If this comes from a subtype
11055 -- declaration, all clauses are inherited.
11056
11057 if No (First_Rep_Item (Def_Id)) then
11058 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11059 end if;
11060
11061 if Is_Tagged_Type (T) then
11062 Set_Is_Tagged_Type (Def_Id);
11063 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
11064 Make_Class_Wide_Type (Def_Id);
11065 end if;
11066
11067 -- When prefixed calls are enabled for untagged types, the subtype
11068 -- shares the primitive operations of its base type. Do this even
11069 -- when GNAT extensions are not allowed, in order to give better
11070 -- error messages.
11071
11072 Set_Direct_Primitive_Operations
11073 (Def_Id, Direct_Primitive_Operations (T));
11074
11075 Set_Stored_Constraint (Def_Id, No_Elist);
11076
11077 if Has_Discrs then
11078 Set_Discriminant_Constraint (Def_Id, Elist);
11079 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
11080 end if;
11081
11082 if Is_Tagged_Type (T) then
11083
11084 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
11085 -- concurrent record type.
11086
11087 if Ada_Version >= Ada_2005 and then Is_Concurrent_Type (T) then
11088 Set_Corresponding_Record_Type
11089 (Def_Id, Corresponding_Record_Type (T));
11090 end if;
11091
11092 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
11093 end if;
11094
11095 -- Subtypes introduced by component declarations do not need to be
11096 -- marked as delayed, and do not get freeze nodes, because the semantics
11097 -- verifies that the parents of the subtypes are frozen before the
11098 -- enclosing record is frozen.
11099
11100 if not Is_Type (Scope (Def_Id)) then
11101 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
11102
11103 if Is_Private_Type (T)
11104 and then Present (Full_View (T))
11105 then
11106 Conditional_Delay (Def_Id, Full_View (T));
11107 else
11108 Conditional_Delay (Def_Id, T);
11109 end if;
11110 end if;
11111
11112 if Is_Record_Type (T) then
11113 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
11114
11115 if Has_Discrs
11116 and then not Is_Empty_Elmt_List (Elist)
11117 and then not For_Access
11118 then
11119 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
11120
11121 elsif not Is_Private_Type (T) then
11122 Set_Cloned_Subtype (Def_Id, T);
11123 end if;
11124 end if;
11125 end Build_Discriminated_Subtype;
11126
11127 ---------------------------
11128 -- Build_Itype_Reference --
11129 ---------------------------
11130
11131 procedure Build_Itype_Reference
11132 (Ityp : Entity_Id;
11133 Nod : Node_Id)
11134 is
11135 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
11136 begin
11137 -- Itype references are only created for use by the back-end
11138
11139 if Inside_A_Generic then
11140 return;
11141 else
11142 Set_Itype (IR, Ityp);
11143
11144 -- If Nod is a library unit entity, then Insert_After won't work,
11145 -- because Nod is not a member of any list. Therefore, we use
11146 -- Add_Global_Declaration in this case. This can happen if we have a
11147 -- build-in-place library function, child unit or not.
11148
11149 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
11150 or else (Nkind (Nod) in
11151 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
11152 and then Is_Compilation_Unit (Defining_Entity (Nod)))
11153 then
11154 Add_Global_Declaration (IR);
11155 else
11156 Insert_After (Nod, IR);
11157 end if;
11158 end if;
11159 end Build_Itype_Reference;
11160
11161 ------------------------
11162 -- Build_Scalar_Bound --
11163 ------------------------
11164
11165 function Build_Scalar_Bound
11166 (Bound : Node_Id;
11167 Par_T : Entity_Id;
11168 Der_T : Entity_Id) return Node_Id
11169 is
11170 New_Bound : Entity_Id;
11171
11172 begin
11173 -- Note: not clear why this is needed, how can the original bound
11174 -- be unanalyzed at this point? and if it is, what business do we
11175 -- have messing around with it? and why is the base type of the
11176 -- parent type the right type for the resolution. It probably is
11177 -- not. It is OK for the new bound we are creating, but not for
11178 -- the old one??? Still if it never happens, no problem.
11179
11180 Analyze_And_Resolve (Bound, Base_Type (Par_T));
11181
11182 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
11183 New_Bound := New_Copy (Bound);
11184 Set_Etype (New_Bound, Der_T);
11185 Set_Analyzed (New_Bound);
11186
11187 elsif Is_Entity_Name (Bound) then
11188 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
11189
11190 -- The following is almost certainly wrong. What business do we have
11191 -- relocating a node (Bound) that is presumably still attached to
11192 -- the tree elsewhere???
11193
11194 else
11195 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
11196 end if;
11197
11198 Set_Etype (New_Bound, Der_T);
11199 return New_Bound;
11200 end Build_Scalar_Bound;
11201
11202 -------------------------------
11203 -- Check_Abstract_Overriding --
11204 -------------------------------
11205
11206 procedure Check_Abstract_Overriding (T : Entity_Id) is
11207 Alias_Subp : Entity_Id;
11208 Elmt : Elmt_Id;
11209 Op_List : Elist_Id;
11210 Subp : Entity_Id;
11211 Type_Def : Node_Id;
11212
11213 procedure Check_Pragma_Implemented (Subp : Entity_Id);
11214 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
11215 -- which has pragma Implemented already set. Check whether Subp's entity
11216 -- kind conforms to the implementation kind of the overridden routine.
11217
11218 procedure Check_Pragma_Implemented
11219 (Subp : Entity_Id;
11220 Iface_Subp : Entity_Id);
11221 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
11222 -- Iface_Subp and both entities have pragma Implemented already set on
11223 -- them. Check whether the two implementation kinds are conforming.
11224
11225 procedure Inherit_Pragma_Implemented
11226 (Subp : Entity_Id;
11227 Iface_Subp : Entity_Id);
11228 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
11229 -- subprogram Iface_Subp which has been marked by pragma Implemented.
11230 -- Propagate the implementation kind of Iface_Subp to Subp.
11231
11232 ------------------------------
11233 -- Check_Pragma_Implemented --
11234 ------------------------------
11235
11236 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
11237 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
11238 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
11239 Subp_Alias : constant Entity_Id := Alias (Subp);
11240 Contr_Typ : Entity_Id;
11241 Impl_Subp : Entity_Id;
11242
11243 begin
11244 -- Subp must have an alias since it is a hidden entity used to link
11245 -- an interface subprogram to its overriding counterpart.
11246
11247 pragma Assert (Present (Subp_Alias));
11248
11249 -- Handle aliases to synchronized wrappers
11250
11251 Impl_Subp := Subp_Alias;
11252
11253 if Is_Primitive_Wrapper (Impl_Subp) then
11254 Impl_Subp := Wrapped_Entity (Impl_Subp);
11255 end if;
11256
11257 -- Extract the type of the controlling formal
11258
11259 Contr_Typ := Etype (First_Formal (Subp_Alias));
11260
11261 if Is_Concurrent_Record_Type (Contr_Typ) then
11262 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
11263 end if;
11264
11265 -- An interface subprogram whose implementation kind is By_Entry must
11266 -- be implemented by an entry.
11267
11268 if Impl_Kind = Name_By_Entry
11269 and then Ekind (Impl_Subp) /= E_Entry
11270 then
11271 Error_Msg_Node_2 := Iface_Alias;
11272 Error_Msg_NE
11273 ("type & must implement abstract subprogram & with an entry",
11274 Subp_Alias, Contr_Typ);
11275
11276 elsif Impl_Kind = Name_By_Protected_Procedure then
11277
11278 -- An interface subprogram whose implementation kind is By_
11279 -- Protected_Procedure cannot be implemented by a primitive
11280 -- procedure of a task type.
11281
11282 if Ekind (Contr_Typ) /= E_Protected_Type then
11283 Error_Msg_Node_2 := Contr_Typ;
11284 Error_Msg_NE
11285 ("interface subprogram & cannot be implemented by a "
11286 & "primitive procedure of task type &",
11287 Subp_Alias, Iface_Alias);
11288
11289 -- An interface subprogram whose implementation kind is By_
11290 -- Protected_Procedure must be implemented by a procedure.
11291
11292 elsif Ekind (Impl_Subp) /= E_Procedure then
11293 Error_Msg_Node_2 := Iface_Alias;
11294 Error_Msg_NE
11295 ("type & must implement abstract subprogram & with a "
11296 & "procedure", Subp_Alias, Contr_Typ);
11297
11298 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11299 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11300 then
11301 Error_Msg_Name_1 := Impl_Kind;
11302 Error_Msg_N
11303 ("overriding operation& must have synchronization%",
11304 Subp_Alias);
11305 end if;
11306
11307 -- If primitive has Optional synchronization, overriding operation
11308 -- must match if it has an explicit synchronization.
11309
11310 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11311 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11312 then
11313 Error_Msg_Name_1 := Impl_Kind;
11314 Error_Msg_N
11315 ("overriding operation& must have synchronization%", Subp_Alias);
11316 end if;
11317 end Check_Pragma_Implemented;
11318
11319 ------------------------------
11320 -- Check_Pragma_Implemented --
11321 ------------------------------
11322
11323 procedure Check_Pragma_Implemented
11324 (Subp : Entity_Id;
11325 Iface_Subp : Entity_Id)
11326 is
11327 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11328 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11329
11330 begin
11331 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11332 -- and overriding subprogram are different. In general this is an
11333 -- error except when the implementation kind of the overridden
11334 -- subprograms is By_Any or Optional.
11335
11336 if Iface_Kind /= Subp_Kind
11337 and then Iface_Kind /= Name_By_Any
11338 and then Iface_Kind /= Name_Optional
11339 then
11340 if Iface_Kind = Name_By_Entry then
11341 Error_Msg_N
11342 ("incompatible implementation kind, overridden subprogram " &
11343 "is marked By_Entry", Subp);
11344 else
11345 Error_Msg_N
11346 ("incompatible implementation kind, overridden subprogram " &
11347 "is marked By_Protected_Procedure", Subp);
11348 end if;
11349 end if;
11350 end Check_Pragma_Implemented;
11351
11352 --------------------------------
11353 -- Inherit_Pragma_Implemented --
11354 --------------------------------
11355
11356 procedure Inherit_Pragma_Implemented
11357 (Subp : Entity_Id;
11358 Iface_Subp : Entity_Id)
11359 is
11360 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11361 Loc : constant Source_Ptr := Sloc (Subp);
11362 Impl_Prag : Node_Id;
11363
11364 begin
11365 -- Since the implementation kind is stored as a representation item
11366 -- rather than a flag, create a pragma node.
11367
11368 Impl_Prag :=
11369 Make_Pragma (Loc,
11370 Chars => Name_Implemented,
11371 Pragma_Argument_Associations => New_List (
11372 Make_Pragma_Argument_Association (Loc,
11373 Expression => New_Occurrence_Of (Subp, Loc)),
11374
11375 Make_Pragma_Argument_Association (Loc,
11376 Expression => Make_Identifier (Loc, Iface_Kind))));
11377
11378 -- The pragma doesn't need to be analyzed because it is internally
11379 -- built. It is safe to directly register it as a rep item since we
11380 -- are only interested in the characters of the implementation kind.
11381
11382 Record_Rep_Item (Subp, Impl_Prag);
11383 end Inherit_Pragma_Implemented;
11384
11385 -- Start of processing for Check_Abstract_Overriding
11386
11387 begin
11388 Op_List := Primitive_Operations (T);
11389
11390 -- Loop to check primitive operations
11391
11392 Elmt := First_Elmt (Op_List);
11393 while Present (Elmt) loop
11394 Subp := Node (Elmt);
11395 Alias_Subp := Alias (Subp);
11396
11397 -- If the parent type is untagged, then no overriding error checks
11398 -- are needed (such as in the case of an implicit full type for
11399 -- a derived type whose parent is an untagged private type with
11400 -- a tagged full type).
11401
11402 if not Is_Tagged_Type (Etype (T)) then
11403 null;
11404
11405 -- Inherited subprograms are identified by the fact that they do not
11406 -- come from source, and the associated source location is the
11407 -- location of the first subtype of the derived type.
11408
11409 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11410 -- subprograms that "require overriding".
11411
11412 -- Special exception, do not complain about failure to override the
11413 -- stream routines _Input and _Output, as well as the primitive
11414 -- operations used in dispatching selects since we always provide
11415 -- automatic overridings for these subprograms.
11416
11417 -- The partial view of T may have been a private extension, for
11418 -- which inherited functions dispatching on result are abstract.
11419 -- If the full view is a null extension, there is no need for
11420 -- overriding in Ada 2005, but wrappers need to be built for them
11421 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11422
11423 elsif Is_Null_Extension (T)
11424 and then Has_Controlling_Result (Subp)
11425 and then Ada_Version >= Ada_2005
11426 and then Present (Alias_Subp)
11427 and then not Comes_From_Source (Subp)
11428 and then not Is_Abstract_Subprogram (Alias_Subp)
11429 and then not Is_Access_Type (Etype (Subp))
11430 then
11431 null;
11432
11433 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11434 -- processing because this check is done with the aliased
11435 -- entity
11436
11437 elsif Present (Interface_Alias (Subp)) then
11438 null;
11439
11440 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11441 -- of a visible private primitive inherited from an ancestor with
11442 -- the aspect Type_Invariant'Class, unless the inherited primitive
11443 -- is abstract.
11444
11445 elsif not Is_Abstract_Subprogram (Subp)
11446 and then not Comes_From_Source (Subp) -- An inherited subprogram
11447 and then Requires_Overriding (Subp)
11448 and then Present (Alias_Subp)
11449 and then Has_Invariants (Etype (T))
11450 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11451 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11452 and then Is_Private_Primitive (Alias_Subp)
11453 then
11454 Error_Msg_NE
11455 ("inherited private primitive & must be overridden", T, Subp);
11456 Error_Msg_N
11457 ("\because ancestor type has 'Type_'Invariant''Class " &
11458 "(RM 7.3.2(6.1))", T);
11459
11460 elsif (Is_Abstract_Subprogram (Subp)
11461 or else Requires_Overriding (Subp)
11462 or else
11463 (Has_Controlling_Result (Subp)
11464 and then Present (Alias_Subp)
11465 and then not Comes_From_Source (Subp)
11466 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11467 and then not Is_TSS (Subp, TSS_Stream_Input)
11468 and then not Is_TSS (Subp, TSS_Stream_Output)
11469 and then not Is_Abstract_Type (T)
11470 and then not Is_Predefined_Interface_Primitive (Subp)
11471
11472 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11473 -- with abstract interface types because the check will be done
11474 -- with the aliased entity (otherwise we generate a duplicated
11475 -- error message).
11476
11477 and then No (Interface_Alias (Subp))
11478 then
11479 if Present (Alias_Subp) then
11480
11481 -- Only perform the check for a derived subprogram when the
11482 -- type has an explicit record extension. This avoids incorrect
11483 -- flagging of abstract subprograms for the case of a type
11484 -- without an extension that is derived from a formal type
11485 -- with a tagged actual (can occur within a private part).
11486
11487 -- Ada 2005 (AI-391): In the case of an inherited function with
11488 -- a controlling result of the type, the rule does not apply if
11489 -- the type is a null extension (unless the parent function
11490 -- itself is abstract, in which case the function must still be
11491 -- be overridden). The expander will generate an overriding
11492 -- wrapper function calling the parent subprogram (see
11493 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11494
11495 Type_Def := Type_Definition (Parent (T));
11496
11497 if Nkind (Type_Def) = N_Derived_Type_Definition
11498 and then Present (Record_Extension_Part (Type_Def))
11499 and then
11500 (Ada_Version < Ada_2005
11501 or else not Is_Null_Extension (T)
11502 or else Ekind (Subp) = E_Procedure
11503 or else not Has_Controlling_Result (Subp)
11504 or else Is_Abstract_Subprogram (Alias_Subp)
11505 or else Requires_Overriding (Subp)
11506 or else Is_Access_Type (Etype (Subp)))
11507 then
11508 -- Avoid reporting error in case of abstract predefined
11509 -- primitive inherited from interface type because the
11510 -- body of internally generated predefined primitives
11511 -- of tagged types are generated later by Freeze_Type
11512
11513 if Is_Interface (Root_Type (T))
11514 and then Is_Abstract_Subprogram (Subp)
11515 and then Is_Predefined_Dispatching_Operation (Subp)
11516 and then not Comes_From_Source (Ultimate_Alias (Subp))
11517 then
11518 null;
11519
11520 -- A null extension is not obliged to override an inherited
11521 -- procedure subject to pragma Extensions_Visible with value
11522 -- False and at least one controlling OUT parameter
11523 -- (SPARK RM 6.1.7(6)).
11524
11525 elsif Is_Null_Extension (T)
11526 and then Is_EVF_Procedure (Subp)
11527 then
11528 null;
11529
11530 -- Subprogram renamings cannot be overridden
11531
11532 elsif Comes_From_Source (Subp)
11533 and then Present (Alias (Subp))
11534 then
11535 null;
11536
11537 -- Skip reporting the error on Ada 2022 only subprograms
11538 -- that require overriding if we are not in Ada 2022 mode.
11539
11540 elsif Ada_Version < Ada_2022
11541 and then Requires_Overriding (Subp)
11542 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11543 then
11544 null;
11545
11546 else
11547 Error_Msg_NE
11548 ("type must be declared abstract or & overridden",
11549 T, Subp);
11550
11551 -- Traverse the whole chain of aliased subprograms to
11552 -- complete the error notification. This is especially
11553 -- useful for traceability of the chain of entities when
11554 -- the subprogram corresponds with an interface
11555 -- subprogram (which may be defined in another package).
11556
11557 if Present (Alias_Subp) then
11558 declare
11559 E : Entity_Id;
11560
11561 begin
11562 E := Subp;
11563 while Present (Alias (E)) loop
11564
11565 -- Avoid reporting redundant errors on entities
11566 -- inherited from interfaces
11567
11568 if Sloc (E) /= Sloc (T) then
11569 Error_Msg_Sloc := Sloc (E);
11570 Error_Msg_NE
11571 ("\& has been inherited #", T, Subp);
11572 end if;
11573
11574 E := Alias (E);
11575 end loop;
11576
11577 Error_Msg_Sloc := Sloc (E);
11578
11579 -- AI05-0068: report if there is an overriding
11580 -- non-abstract subprogram that is invisible.
11581
11582 if Is_Hidden (E)
11583 and then not Is_Abstract_Subprogram (E)
11584 then
11585 Error_Msg_NE
11586 ("\& subprogram# is not visible",
11587 T, Subp);
11588
11589 -- Clarify the case where a non-null extension must
11590 -- override inherited procedure subject to pragma
11591 -- Extensions_Visible with value False and at least
11592 -- one controlling OUT param.
11593
11594 elsif Is_EVF_Procedure (E) then
11595 Error_Msg_NE
11596 ("\& # is subject to Extensions_Visible False",
11597 T, Subp);
11598
11599 else
11600 Error_Msg_NE
11601 ("\& has been inherited from subprogram #",
11602 T, Subp);
11603 end if;
11604 end;
11605 end if;
11606 end if;
11607
11608 -- Ada 2005 (AI-345): Protected or task type implementing
11609 -- abstract interfaces.
11610
11611 elsif Is_Concurrent_Record_Type (T)
11612 and then Present (Interfaces (T))
11613 then
11614 -- There is no need to check here RM 9.4(11.9/3) since we
11615 -- are processing the corresponding record type and the
11616 -- mode of the overriding subprograms was verified by
11617 -- Check_Conformance when the corresponding concurrent
11618 -- type declaration was analyzed.
11619
11620 Error_Msg_NE
11621 ("interface subprogram & must be overridden", T, Subp);
11622
11623 -- Examine primitive operations of synchronized type to find
11624 -- homonyms that have the wrong profile.
11625
11626 declare
11627 Prim : Entity_Id;
11628
11629 begin
11630 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11631 while Present (Prim) loop
11632 if Chars (Prim) = Chars (Subp) then
11633 Error_Msg_NE
11634 ("profile is not type conformant with prefixed "
11635 & "view profile of inherited operation&",
11636 Prim, Subp);
11637 end if;
11638
11639 Next_Entity (Prim);
11640 end loop;
11641 end;
11642 end if;
11643
11644 else
11645 Error_Msg_Node_2 := T;
11646 Error_Msg_N
11647 ("abstract subprogram& not allowed for type&", Subp);
11648
11649 -- Also post unconditional warning on the type (unconditional
11650 -- so that if there are more than one of these cases, we get
11651 -- them all, and not just the first one).
11652
11653 Error_Msg_Node_2 := Subp;
11654 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11655 end if;
11656
11657 -- A subprogram subject to pragma Extensions_Visible with value
11658 -- "True" cannot override a subprogram subject to the same pragma
11659 -- with value "False" (SPARK RM 6.1.7(5)).
11660
11661 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11662 and then Present (Overridden_Operation (Subp))
11663 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11664 Extensions_Visible_False
11665 then
11666 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11667 Error_Msg_N
11668 ("subprogram & with Extensions_Visible True cannot override "
11669 & "subprogram # with Extensions_Visible False", Subp);
11670 end if;
11671
11672 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11673
11674 -- Subp is an expander-generated procedure which maps an interface
11675 -- alias to a protected wrapper. The interface alias is flagged by
11676 -- pragma Implemented. Ensure that Subp is a procedure when the
11677 -- implementation kind is By_Protected_Procedure or an entry when
11678 -- By_Entry.
11679
11680 if Ada_Version >= Ada_2012
11681 and then Is_Hidden (Subp)
11682 and then Present (Interface_Alias (Subp))
11683 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11684 then
11685 Check_Pragma_Implemented (Subp);
11686 end if;
11687
11688 -- Subp is an interface primitive which overrides another interface
11689 -- primitive marked with pragma Implemented.
11690
11691 if Ada_Version >= Ada_2012
11692 and then Present (Overridden_Operation (Subp))
11693 and then Has_Rep_Pragma
11694 (Overridden_Operation (Subp), Name_Implemented)
11695 then
11696 -- If the overriding routine is also marked by Implemented, check
11697 -- that the two implementation kinds are conforming.
11698
11699 if Has_Rep_Pragma (Subp, Name_Implemented) then
11700 Check_Pragma_Implemented
11701 (Subp => Subp,
11702 Iface_Subp => Overridden_Operation (Subp));
11703
11704 -- Otherwise the overriding routine inherits the implementation
11705 -- kind from the overridden subprogram.
11706
11707 else
11708 Inherit_Pragma_Implemented
11709 (Subp => Subp,
11710 Iface_Subp => Overridden_Operation (Subp));
11711 end if;
11712 end if;
11713
11714 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11715 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11716 -- for procedures, since this is our pragma.
11717
11718 if Present (Overridden_Operation (Subp))
11719 and then No_Return (Overridden_Operation (Subp))
11720 then
11721
11722 -- If the subprogram is a renaming, check that the renamed
11723 -- subprogram is No_Return.
11724
11725 if Present (Renamed_Or_Alias (Subp)) then
11726 if not No_Return (Renamed_Or_Alias (Subp)) then
11727 Error_Msg_NE ("subprogram & must be No_Return",
11728 Subp,
11729 Renamed_Or_Alias (Subp));
11730 Error_Msg_N ("\since renaming & overrides No_Return "
11731 & "subprogram (RM 6.5.1(6/2))",
11732 Subp);
11733 end if;
11734
11735 -- Make sure that the subprogram itself is No_Return.
11736
11737 elsif not No_Return (Subp) then
11738 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11739 Error_Msg_N
11740 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11741 Subp);
11742 end if;
11743 end if;
11744
11745 -- If the operation is a wrapper for a synchronized primitive, it
11746 -- may be called indirectly through a dispatching select. We assume
11747 -- that it will be referenced elsewhere indirectly, and suppress
11748 -- warnings about an unused entity.
11749
11750 if Is_Primitive_Wrapper (Subp)
11751 and then Present (Wrapped_Entity (Subp))
11752 then
11753 Set_Referenced (Wrapped_Entity (Subp));
11754 end if;
11755
11756 Next_Elmt (Elmt);
11757 end loop;
11758 end Check_Abstract_Overriding;
11759
11760 ------------------------------------------------
11761 -- Check_Access_Discriminant_Requires_Limited --
11762 ------------------------------------------------
11763
11764 procedure Check_Access_Discriminant_Requires_Limited
11765 (D : Node_Id;
11766 Loc : Node_Id)
11767 is
11768 begin
11769 -- A discriminant_specification for an access discriminant shall appear
11770 -- only in the declaration for a task or protected type, or for a type
11771 -- with the reserved word 'limited' in its definition or in one of its
11772 -- ancestors (RM 3.7(10)).
11773
11774 -- AI-0063: The proper condition is that type must be immutably limited,
11775 -- or else be a partial view.
11776
11777 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11778 if Is_Inherently_Limited_Type (Current_Scope)
11779 or else
11780 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11781 and then Limited_Present (Parent (Current_Scope)))
11782 then
11783 null;
11784
11785 else
11786 Error_Msg_N
11787 ("access discriminants allowed only for limited types", Loc);
11788 end if;
11789 end if;
11790 end Check_Access_Discriminant_Requires_Limited;
11791
11792 -----------------------------------
11793 -- Check_Aliased_Component_Types --
11794 -----------------------------------
11795
11796 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11797 C : Entity_Id;
11798
11799 begin
11800 -- ??? Also need to check components of record extensions, but not
11801 -- components of protected types (which are always limited).
11802
11803 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11804 -- types to be unconstrained. This is safe because it is illegal to
11805 -- create access subtypes to such types with explicit discriminant
11806 -- constraints.
11807
11808 if not Is_Limited_Type (T) then
11809 if Ekind (T) = E_Record_Type then
11810 C := First_Component (T);
11811 while Present (C) loop
11812 if Is_Aliased (C)
11813 and then Has_Discriminants (Etype (C))
11814 and then not Is_Constrained (Etype (C))
11815 and then not In_Instance_Body
11816 and then Ada_Version < Ada_2005
11817 then
11818 Error_Msg_N
11819 ("aliased component must be constrained (RM 3.6(11))",
11820 C);
11821 end if;
11822
11823 Next_Component (C);
11824 end loop;
11825
11826 elsif Ekind (T) = E_Array_Type then
11827 if Has_Aliased_Components (T)
11828 and then Has_Discriminants (Component_Type (T))
11829 and then not Is_Constrained (Component_Type (T))
11830 and then not In_Instance_Body
11831 and then Ada_Version < Ada_2005
11832 then
11833 Error_Msg_N
11834 ("aliased component type must be constrained (RM 3.6(11))",
11835 T);
11836 end if;
11837 end if;
11838 end if;
11839 end Check_Aliased_Component_Types;
11840
11841 --------------------------------------
11842 -- Check_Anonymous_Access_Component --
11843 --------------------------------------
11844
11845 procedure Check_Anonymous_Access_Component
11846 (Typ_Decl : Node_Id;
11847 Typ : Entity_Id;
11848 Prev : Entity_Id;
11849 Comp_Def : Node_Id;
11850 Access_Def : Node_Id)
11851 is
11852 Loc : constant Source_Ptr := Sloc (Comp_Def);
11853 Anon_Access : Entity_Id;
11854 Acc_Def : Node_Id;
11855 Decl : Node_Id;
11856 Type_Def : Node_Id;
11857
11858 procedure Build_Incomplete_Type_Declaration;
11859 -- If the record type contains components that include an access to the
11860 -- current record, then create an incomplete type declaration for the
11861 -- record, to be used as the designated type of the anonymous access.
11862 -- This is done only once, and only if there is no previous partial
11863 -- view of the type.
11864
11865 function Designates_T (Subt : Node_Id) return Boolean;
11866 -- Check whether a node designates the enclosing record type, or 'Class
11867 -- of that type
11868
11869 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11870 -- Check whether an access definition includes a reference to
11871 -- the enclosing record type. The reference can be a subtype mark
11872 -- in the access definition itself, a 'Class attribute reference, or
11873 -- recursively a reference appearing in a parameter specification
11874 -- or result definition of an access_to_subprogram definition.
11875
11876 --------------------------------------
11877 -- Build_Incomplete_Type_Declaration --
11878 --------------------------------------
11879
11880 procedure Build_Incomplete_Type_Declaration is
11881 Decl : Node_Id;
11882 Inc_T : Entity_Id;
11883 H : Entity_Id;
11884
11885 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11886 -- it's "is new ... with record" or else "is tagged record ...".
11887
11888 Typ_Def : constant Node_Id :=
11889 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11890 then Type_Definition (Typ_Decl) else Empty);
11891 Is_Tagged : constant Boolean :=
11892 Present (Typ_Def)
11893 and then
11894 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11895 and then
11896 Present (Record_Extension_Part (Typ_Def)))
11897 or else
11898 (Nkind (Typ_Def) = N_Record_Definition
11899 and then Tagged_Present (Typ_Def)));
11900
11901 begin
11902 -- If there is a previous partial view, no need to create a new one
11903 -- If the partial view, given by Prev, is incomplete, If Prev is
11904 -- a private declaration, full declaration is flagged accordingly.
11905
11906 if Prev /= Typ then
11907 if Is_Tagged then
11908 Make_Class_Wide_Type (Prev);
11909 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11910 Set_Etype (Class_Wide_Type (Typ), Typ);
11911 end if;
11912
11913 return;
11914
11915 elsif Has_Private_Declaration (Typ) then
11916
11917 -- If we refer to T'Class inside T, and T is the completion of a
11918 -- private type, then make sure the class-wide type exists.
11919
11920 if Is_Tagged then
11921 Make_Class_Wide_Type (Typ);
11922 end if;
11923
11924 return;
11925
11926 -- If there was a previous anonymous access type, the incomplete
11927 -- type declaration will have been created already.
11928
11929 elsif Present (Current_Entity (Typ))
11930 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11931 and then Full_View (Current_Entity (Typ)) = Typ
11932 then
11933 if Is_Tagged
11934 and then Comes_From_Source (Current_Entity (Typ))
11935 and then not Is_Tagged_Type (Current_Entity (Typ))
11936 then
11937 Make_Class_Wide_Type (Typ);
11938 Error_Msg_N
11939 ("incomplete view of tagged type should be declared tagged??",
11940 Parent (Current_Entity (Typ)));
11941 end if;
11942 return;
11943
11944 else
11945 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11946 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11947
11948 -- Type has already been inserted into the current scope. Remove
11949 -- it, and add incomplete declaration for type, so that subsequent
11950 -- anonymous access types can use it. The entity is unchained from
11951 -- the homonym list and from immediate visibility. After analysis,
11952 -- the entity in the incomplete declaration becomes immediately
11953 -- visible in the record declaration that follows.
11954
11955 H := Current_Entity (Typ);
11956
11957 if H = Typ then
11958 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11959
11960 else
11961 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11962 H := Homonym (Typ);
11963 end loop;
11964
11965 Set_Homonym (H, Homonym (Typ));
11966 end if;
11967
11968 Insert_Before (Typ_Decl, Decl);
11969 Analyze (Decl);
11970 Set_Full_View (Inc_T, Typ);
11971 Set_Incomplete_View (Typ_Decl, Inc_T);
11972
11973 -- If the type is tagged, create a common class-wide type for
11974 -- both views, and set the Etype of the class-wide type to the
11975 -- full view.
11976
11977 if Is_Tagged then
11978 Make_Class_Wide_Type (Inc_T);
11979 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11980 Set_Etype (Class_Wide_Type (Typ), Typ);
11981 end if;
11982
11983 -- If the scope is a package with a limited view, create a shadow
11984 -- entity for the incomplete type like Build_Limited_Views, so as
11985 -- to make it possible for Remove_Limited_With_Unit to reinstall
11986 -- this incomplete type as the visible entity.
11987
11988 if Ekind (Scope (Inc_T)) = E_Package
11989 and then Present (Limited_View (Scope (Inc_T)))
11990 then
11991 declare
11992 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11993
11994 begin
11995 -- This is modeled on Build_Shadow_Entity
11996
11997 Set_Chars (Shadow, Chars (Inc_T));
11998 Set_Parent (Shadow, Decl);
11999 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
12000 Set_Is_Internal (Shadow);
12001 Set_From_Limited_With (Shadow);
12002 Set_Non_Limited_View (Shadow, Inc_T);
12003 Set_Private_Dependents (Shadow, New_Elmt_List);
12004
12005 if Is_Tagged then
12006 Set_Non_Limited_View
12007 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
12008 end if;
12009
12010 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
12011 end;
12012 end if;
12013 end if;
12014 end Build_Incomplete_Type_Declaration;
12015
12016 ------------------
12017 -- Designates_T --
12018 ------------------
12019
12020 function Designates_T (Subt : Node_Id) return Boolean is
12021 Type_Id : constant Name_Id := Chars (Typ);
12022
12023 function Names_T (Nam : Node_Id) return Boolean;
12024 -- The record type has not been introduced in the current scope
12025 -- yet, so we must examine the name of the type itself, either
12026 -- an identifier T, or an expanded name of the form P.T, where
12027 -- P denotes the current scope.
12028
12029 -------------
12030 -- Names_T --
12031 -------------
12032
12033 function Names_T (Nam : Node_Id) return Boolean is
12034 begin
12035 if Nkind (Nam) = N_Identifier then
12036 return Chars (Nam) = Type_Id;
12037
12038 elsif Nkind (Nam) = N_Selected_Component then
12039 if Chars (Selector_Name (Nam)) = Type_Id then
12040 if Nkind (Prefix (Nam)) = N_Identifier then
12041 return Chars (Prefix (Nam)) = Chars (Current_Scope);
12042
12043 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
12044 return Chars (Selector_Name (Prefix (Nam))) =
12045 Chars (Current_Scope);
12046 else
12047 return False;
12048 end if;
12049
12050 else
12051 return False;
12052 end if;
12053
12054 else
12055 return False;
12056 end if;
12057 end Names_T;
12058
12059 -- Start of processing for Designates_T
12060
12061 begin
12062 if Nkind (Subt) = N_Identifier then
12063 return Chars (Subt) = Type_Id;
12064
12065 -- Reference can be through an expanded name which has not been
12066 -- analyzed yet, and which designates enclosing scopes.
12067
12068 elsif Nkind (Subt) = N_Selected_Component then
12069 if Names_T (Subt) then
12070 return True;
12071
12072 -- Otherwise it must denote an entity that is already visible.
12073 -- The access definition may name a subtype of the enclosing
12074 -- type, if there is a previous incomplete declaration for it.
12075
12076 else
12077 Find_Selected_Component (Subt);
12078 return
12079 Is_Entity_Name (Subt)
12080 and then Scope (Entity (Subt)) = Current_Scope
12081 and then
12082 (Chars (Base_Type (Entity (Subt))) = Type_Id
12083 or else
12084 (Is_Class_Wide_Type (Entity (Subt))
12085 and then
12086 Chars (Etype (Base_Type (Entity (Subt)))) =
12087 Type_Id));
12088 end if;
12089
12090 -- A reference to the current type may appear as the prefix of
12091 -- a 'Class attribute.
12092
12093 elsif Nkind (Subt) = N_Attribute_Reference
12094 and then Attribute_Name (Subt) = Name_Class
12095 then
12096 return Names_T (Prefix (Subt));
12097
12098 else
12099 return False;
12100 end if;
12101 end Designates_T;
12102
12103 ----------------
12104 -- Mentions_T --
12105 ----------------
12106
12107 function Mentions_T (Acc_Def : Node_Id) return Boolean is
12108 Param_Spec : Node_Id;
12109
12110 Acc_Subprg : constant Node_Id :=
12111 Access_To_Subprogram_Definition (Acc_Def);
12112
12113 begin
12114 if No (Acc_Subprg) then
12115 return Designates_T (Subtype_Mark (Acc_Def));
12116 end if;
12117
12118 -- Component is an access_to_subprogram: examine its formals,
12119 -- and result definition in the case of an access_to_function.
12120
12121 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
12122 while Present (Param_Spec) loop
12123 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
12124 and then Mentions_T (Parameter_Type (Param_Spec))
12125 then
12126 return True;
12127
12128 elsif Designates_T (Parameter_Type (Param_Spec)) then
12129 return True;
12130 end if;
12131
12132 Next (Param_Spec);
12133 end loop;
12134
12135 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
12136 if Nkind (Result_Definition (Acc_Subprg)) =
12137 N_Access_Definition
12138 then
12139 return Mentions_T (Result_Definition (Acc_Subprg));
12140 else
12141 return Designates_T (Result_Definition (Acc_Subprg));
12142 end if;
12143 end if;
12144
12145 return False;
12146 end Mentions_T;
12147
12148 -- Start of processing for Check_Anonymous_Access_Component
12149
12150 begin
12151 if Present (Access_Def) and then Mentions_T (Access_Def) then
12152 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
12153
12154 Build_Incomplete_Type_Declaration;
12155 Anon_Access := Make_Temporary (Loc, 'S');
12156
12157 -- Create a declaration for the anonymous access type: either
12158 -- an access_to_object or an access_to_subprogram.
12159
12160 if Present (Acc_Def) then
12161 if Nkind (Acc_Def) = N_Access_Function_Definition then
12162 Type_Def :=
12163 Make_Access_Function_Definition (Loc,
12164 Parameter_Specifications =>
12165 Parameter_Specifications (Acc_Def),
12166 Result_Definition => Result_Definition (Acc_Def));
12167 else
12168 Type_Def :=
12169 Make_Access_Procedure_Definition (Loc,
12170 Parameter_Specifications =>
12171 Parameter_Specifications (Acc_Def));
12172 end if;
12173
12174 else
12175 Type_Def :=
12176 Make_Access_To_Object_Definition (Loc,
12177 All_Present => All_Present (Access_Def),
12178 Constant_Present => Constant_Present (Access_Def),
12179 Subtype_Indication =>
12180 Relocate_Node (Subtype_Mark (Access_Def)));
12181 end if;
12182
12183 Set_Null_Exclusion_Present
12184 (Type_Def, Null_Exclusion_Present (Access_Def));
12185
12186 Decl :=
12187 Make_Full_Type_Declaration (Loc,
12188 Defining_Identifier => Anon_Access,
12189 Type_Definition => Type_Def);
12190
12191 Insert_Before (Typ_Decl, Decl);
12192 Analyze (Decl);
12193
12194 -- At first sight we could add here the extra formals of an access to
12195 -- subprogram; however, it must delayed till the freeze point so that
12196 -- we know the convention.
12197
12198 if Nkind (Comp_Def) = N_Component_Definition then
12199 Rewrite (Comp_Def,
12200 Make_Component_Definition (Loc,
12201 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
12202 else
12203 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
12204 Rewrite (Comp_Def,
12205 Make_Discriminant_Specification (Loc,
12206 Defining_Identifier => Defining_Identifier (Comp_Def),
12207 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
12208 end if;
12209
12210 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
12211 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
12212 else
12213 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
12214 end if;
12215
12216 Set_Is_Local_Anonymous_Access (Anon_Access);
12217 end if;
12218 end Check_Anonymous_Access_Component;
12219
12220 ---------------------------------------
12221 -- Check_Anonymous_Access_Components --
12222 ---------------------------------------
12223
12224 procedure Check_Anonymous_Access_Components
12225 (Typ_Decl : Node_Id;
12226 Typ : Entity_Id;
12227 Prev : Entity_Id;
12228 Comp_List : Node_Id)
12229 is
12230 Comp : Node_Id;
12231 begin
12232 if No (Comp_List) then
12233 return;
12234 end if;
12235
12236 Set_Is_Not_Self_Hidden (Typ);
12237
12238 Comp := First (Component_Items (Comp_List));
12239 while Present (Comp) loop
12240 if Nkind (Comp) = N_Component_Declaration then
12241 Check_Anonymous_Access_Component
12242 (Typ_Decl, Typ, Prev,
12243 Component_Definition (Comp),
12244 Access_Definition (Component_Definition (Comp)));
12245 end if;
12246
12247 Next (Comp);
12248 end loop;
12249
12250 if Present (Variant_Part (Comp_List)) then
12251 declare
12252 V : Node_Id;
12253 begin
12254 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
12255 while Present (V) loop
12256 Check_Anonymous_Access_Components
12257 (Typ_Decl, Typ, Prev, Component_List (V));
12258 Next_Non_Pragma (V);
12259 end loop;
12260 end;
12261 end if;
12262 end Check_Anonymous_Access_Components;
12263
12264 ----------------------
12265 -- Check_Completion --
12266 ----------------------
12267
12268 procedure Check_Completion (Body_Id : Node_Id := Empty) is
12269 E : Entity_Id;
12270
12271 procedure Post_Error;
12272 -- Post error message for lack of completion for entity E
12273
12274 ----------------
12275 -- Post_Error --
12276 ----------------
12277
12278 procedure Post_Error is
12279 procedure Missing_Body;
12280 -- Output missing body message
12281
12282 ------------------
12283 -- Missing_Body --
12284 ------------------
12285
12286 procedure Missing_Body is
12287 begin
12288 -- Spec is in same unit, so we can post on spec
12289
12290 if In_Same_Source_Unit (Body_Id, E) then
12291 Error_Msg_N ("missing body for &", E);
12292
12293 -- Spec is in a separate unit, so we have to post on the body
12294
12295 else
12296 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12297 end if;
12298 end Missing_Body;
12299
12300 -- Start of processing for Post_Error
12301
12302 begin
12303 if not Comes_From_Source (E) then
12304 if Ekind (E) in E_Task_Type | E_Protected_Type then
12305
12306 -- It may be an anonymous protected type created for a
12307 -- single variable. Post error on variable, if present.
12308
12309 declare
12310 Var : Entity_Id;
12311
12312 begin
12313 Var := First_Entity (Current_Scope);
12314 while Present (Var) loop
12315 exit when Etype (Var) = E
12316 and then Comes_From_Source (Var);
12317
12318 Next_Entity (Var);
12319 end loop;
12320
12321 if Present (Var) then
12322 E := Var;
12323 end if;
12324 end;
12325 end if;
12326 end if;
12327
12328 -- If a generated entity has no completion, then either previous
12329 -- semantic errors have disabled the expansion phase, or else we had
12330 -- missing subunits, or else we are compiling without expansion,
12331 -- or else something is very wrong.
12332
12333 if not Comes_From_Source (E) then
12334 pragma Assert
12335 (Serious_Errors_Detected > 0
12336 or else Configurable_Run_Time_Violations > 0
12337 or else Subunits_Missing
12338 or else not Expander_Active);
12339 return;
12340
12341 -- Here for source entity
12342
12343 else
12344 -- Here if no body to post the error message, so we post the error
12345 -- on the declaration that has no completion. This is not really
12346 -- the right place to post it, think about this later ???
12347
12348 if No (Body_Id) then
12349 if Is_Type (E) then
12350 Error_Msg_NE
12351 ("missing full declaration for }", Parent (E), E);
12352 else
12353 Error_Msg_NE ("missing body for &", Parent (E), E);
12354 end if;
12355
12356 -- Package body has no completion for a declaration that appears
12357 -- in the corresponding spec. Post error on the body, with a
12358 -- reference to the non-completed declaration.
12359
12360 else
12361 Error_Msg_Sloc := Sloc (E);
12362
12363 if Is_Type (E) then
12364 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12365
12366 elsif Is_Overloadable (E)
12367 and then Current_Entity_In_Scope (E) /= E
12368 then
12369 -- It may be that the completion is mistyped and appears as
12370 -- a distinct overloading of the entity.
12371
12372 declare
12373 Candidate : constant Entity_Id :=
12374 Current_Entity_In_Scope (E);
12375 Decl : constant Node_Id :=
12376 Unit_Declaration_Node (Candidate);
12377
12378 begin
12379 if Is_Overloadable (Candidate)
12380 and then Ekind (Candidate) = Ekind (E)
12381 and then Nkind (Decl) = N_Subprogram_Body
12382 and then Acts_As_Spec (Decl)
12383 then
12384 Check_Type_Conformant (Candidate, E);
12385
12386 else
12387 Missing_Body;
12388 end if;
12389 end;
12390
12391 else
12392 Missing_Body;
12393 end if;
12394 end if;
12395 end if;
12396 end Post_Error;
12397
12398 -- Local variables
12399
12400 Pack_Id : constant Entity_Id := Current_Scope;
12401
12402 -- Start of processing for Check_Completion
12403
12404 begin
12405 E := First_Entity (Pack_Id);
12406 while Present (E) loop
12407 if Is_Intrinsic_Subprogram (E) then
12408 null;
12409
12410 -- The following situation requires special handling: a child unit
12411 -- that appears in the context clause of the body of its parent:
12412
12413 -- procedure Parent.Child (...);
12414
12415 -- with Parent.Child;
12416 -- package body Parent is
12417
12418 -- Here Parent.Child appears as a local entity, but should not be
12419 -- flagged as requiring completion, because it is a compilation
12420 -- unit.
12421
12422 -- Ignore missing completion for a subprogram that does not come from
12423 -- source (including the _Call primitive operation of RAS types,
12424 -- which has to have the flag Comes_From_Source for other purposes):
12425 -- we assume that the expander will provide the missing completion.
12426 -- In case of previous errors, other expansion actions that provide
12427 -- bodies for null procedures with not be invoked, so inhibit message
12428 -- in those cases.
12429
12430 -- Note that E_Operator is not in the list that follows, because
12431 -- this kind is reserved for predefined operators, that are
12432 -- intrinsic and do not need completion.
12433
12434 elsif Ekind (E) in E_Function
12435 | E_Procedure
12436 | E_Generic_Function
12437 | E_Generic_Procedure
12438 then
12439 if Has_Completion (E) then
12440 null;
12441
12442 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12443 null;
12444
12445 elsif Is_Subprogram (E)
12446 and then (not Comes_From_Source (E)
12447 or else Chars (E) = Name_uCall)
12448 then
12449 null;
12450
12451 elsif
12452 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12453 then
12454 null;
12455
12456 elsif Nkind (Parent (E)) = N_Procedure_Specification
12457 and then Null_Present (Parent (E))
12458 and then Serious_Errors_Detected > 0
12459 then
12460 null;
12461
12462 else
12463 Post_Error;
12464 end if;
12465
12466 elsif Is_Entry (E) then
12467 if not Has_Completion (E)
12468 and then Ekind (Scope (E)) = E_Protected_Type
12469 then
12470 Post_Error;
12471 end if;
12472
12473 elsif Is_Package_Or_Generic_Package (E) then
12474 if Unit_Requires_Body (E) then
12475 if not Has_Completion (E)
12476 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12477 N_Compilation_Unit
12478 then
12479 Post_Error;
12480 end if;
12481
12482 elsif not Is_Child_Unit (E) then
12483 May_Need_Implicit_Body (E);
12484 end if;
12485
12486 -- A formal incomplete type (Ada 2012) does not require a completion;
12487 -- other incomplete type declarations do.
12488
12489 elsif Ekind (E) = E_Incomplete_Type then
12490 if No (Underlying_Type (E))
12491 and then not Is_Generic_Type (E)
12492 then
12493 Post_Error;
12494 end if;
12495
12496 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12497 if not Has_Completion (E) then
12498 Post_Error;
12499 end if;
12500
12501 -- A single task declared in the current scope is a constant, verify
12502 -- that the body of its anonymous type is in the same scope. If the
12503 -- task is defined elsewhere, this may be a renaming declaration for
12504 -- which no completion is needed.
12505
12506 elsif Ekind (E) = E_Constant then
12507 if Ekind (Etype (E)) = E_Task_Type
12508 and then not Has_Completion (Etype (E))
12509 and then Scope (Etype (E)) = Current_Scope
12510 then
12511 Post_Error;
12512 end if;
12513
12514 elsif Ekind (E) = E_Record_Type then
12515 if Is_Tagged_Type (E) then
12516 Check_Abstract_Overriding (E);
12517 Check_Conventions (E);
12518 end if;
12519
12520 Check_Aliased_Component_Types (E);
12521
12522 elsif Ekind (E) = E_Array_Type then
12523 Check_Aliased_Component_Types (E);
12524
12525 end if;
12526
12527 Next_Entity (E);
12528 end loop;
12529 end Check_Completion;
12530
12531 -------------------------------------
12532 -- Check_Constraining_Discriminant --
12533 -------------------------------------
12534
12535 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12536 is
12537 New_Type : constant Entity_Id := Etype (New_Disc);
12538 Old_Type : Entity_Id;
12539
12540 begin
12541 -- If the record type contains an array constrained by the discriminant
12542 -- but with some different bound, the compiler tries to create a smaller
12543 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12544 -- In this case, where the discriminant type is a scalar type, the check
12545 -- must use the original discriminant type in the parent declaration.
12546
12547 if Is_Scalar_Type (New_Type) then
12548 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12549 else
12550 Old_Type := Etype (Old_Disc);
12551 end if;
12552
12553 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12554 Error_Msg_N
12555 ("subtype must be statically compatible with parent discriminant",
12556 New_Disc);
12557
12558 if not Predicates_Compatible (New_Type, Old_Type) then
12559 Error_Msg_N
12560 ("\subtype predicate is not compatible with parent discriminant",
12561 New_Disc);
12562 end if;
12563 end if;
12564 end Check_Constraining_Discriminant;
12565
12566 ------------------------------------
12567 -- Check_CPP_Type_Has_No_Defaults --
12568 ------------------------------------
12569
12570 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12571 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12572 Clist : Node_Id;
12573 Comp : Node_Id;
12574
12575 begin
12576 -- Obtain the component list
12577
12578 if Nkind (Tdef) = N_Record_Definition then
12579 Clist := Component_List (Tdef);
12580 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12581 Clist := Component_List (Record_Extension_Part (Tdef));
12582 end if;
12583
12584 -- Check all components to ensure no default expressions
12585
12586 if Present (Clist) then
12587 Comp := First_Non_Pragma (Component_Items (Clist));
12588 while Present (Comp) loop
12589 if Present (Expression (Comp)) then
12590 Error_Msg_N
12591 ("component of imported 'C'P'P type cannot have "
12592 & "default expression", Expression (Comp));
12593 end if;
12594
12595 Next_Non_Pragma (Comp);
12596 end loop;
12597 end if;
12598 end Check_CPP_Type_Has_No_Defaults;
12599
12600 ----------------------------
12601 -- Check_Delta_Expression --
12602 ----------------------------
12603
12604 procedure Check_Delta_Expression (E : Node_Id) is
12605 begin
12606 if not Is_Real_Type (Etype (E)) then
12607 Wrong_Type (E, Any_Real);
12608
12609 elsif not Is_OK_Static_Expression (E) then
12610 Flag_Non_Static_Expr
12611 ("non-static expression used for delta value!", E);
12612
12613 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12614 Error_Msg_N ("delta expression must be positive", E);
12615
12616 else
12617 return;
12618 end if;
12619
12620 -- If any of above errors occurred, then replace the incorrect
12621 -- expression by the real 0.1, which should prevent further errors.
12622
12623 Rewrite (E,
12624 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12625 Analyze_And_Resolve (E, Standard_Float);
12626 end Check_Delta_Expression;
12627
12628 -----------------------------
12629 -- Check_Digits_Expression --
12630 -----------------------------
12631
12632 procedure Check_Digits_Expression (E : Node_Id) is
12633 begin
12634 if not Is_Integer_Type (Etype (E)) then
12635 Wrong_Type (E, Any_Integer);
12636
12637 elsif not Is_OK_Static_Expression (E) then
12638 Flag_Non_Static_Expr
12639 ("non-static expression used for digits value!", E);
12640
12641 elsif Expr_Value (E) <= 0 then
12642 Error_Msg_N ("digits value must be greater than zero", E);
12643
12644 else
12645 return;
12646 end if;
12647
12648 -- If any of above errors occurred, then replace the incorrect
12649 -- expression by the integer 1, which should prevent further errors.
12650
12651 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12652 Analyze_And_Resolve (E, Standard_Integer);
12653
12654 end Check_Digits_Expression;
12655
12656 --------------------------
12657 -- Check_Initialization --
12658 --------------------------
12659
12660 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12661 begin
12662 -- Special processing for limited types
12663
12664 if Is_Limited_Type (T)
12665 and then not In_Instance
12666 and then not In_Inlined_Body
12667 then
12668 if not OK_For_Limited_Init (T, Exp) then
12669
12670 -- In GNAT mode, this is just a warning, to allow it to be evilly
12671 -- turned off. Otherwise it is a real error.
12672
12673 if GNAT_Mode then
12674 Error_Msg_N
12675 ("??cannot initialize entities of limited type!", Exp);
12676
12677 elsif Ada_Version < Ada_2005 then
12678
12679 -- The side effect removal machinery may generate illegal Ada
12680 -- code to avoid the usage of access types and 'reference in
12681 -- SPARK mode. Since this is legal code with respect to theorem
12682 -- proving, do not emit the error.
12683
12684 if GNATprove_Mode
12685 and then Nkind (Exp) = N_Function_Call
12686 and then Nkind (Parent (Exp)) = N_Object_Declaration
12687 and then not Comes_From_Source
12688 (Defining_Identifier (Parent (Exp)))
12689 then
12690 null;
12691
12692 else
12693 Error_Msg_N
12694 ("cannot initialize entities of limited type", Exp);
12695 Explain_Limited_Type (T, Exp);
12696 end if;
12697
12698 else
12699 -- Specialize error message according to kind of illegal
12700 -- initial expression. We check the Original_Node to cover
12701 -- cases where the initialization expression of an object
12702 -- declaration generated by the compiler has been rewritten
12703 -- (such as for dispatching calls).
12704
12705 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12706 and then
12707 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12708 then
12709 -- No error for internally-generated object declarations,
12710 -- which can come from build-in-place assignment statements.
12711
12712 if Nkind (Parent (Exp)) = N_Object_Declaration
12713 and then not Comes_From_Source
12714 (Defining_Identifier (Parent (Exp)))
12715 then
12716 null;
12717
12718 else
12719 Error_Msg_N
12720 ("illegal context for call to function with limited "
12721 & "result", Exp);
12722 end if;
12723
12724 else
12725 Error_Msg_N
12726 ("initialization of limited object requires aggregate or "
12727 & "function call", Exp);
12728 end if;
12729 end if;
12730 end if;
12731 end if;
12732
12733 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12734 -- set unless we can be sure that no range check is required.
12735
12736 if not Expander_Active
12737 and then Is_Scalar_Type (T)
12738 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12739 then
12740 Set_Do_Range_Check (Exp);
12741 end if;
12742 end Check_Initialization;
12743
12744 ----------------------
12745 -- Check_Interfaces --
12746 ----------------------
12747
12748 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12749 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12750
12751 Iface : Node_Id;
12752 Iface_Def : Node_Id;
12753 Iface_Typ : Entity_Id;
12754 Parent_Node : Node_Id;
12755
12756 Is_Task : Boolean := False;
12757 -- Set True if parent type or any progenitor is a task interface
12758
12759 Is_Protected : Boolean := False;
12760 -- Set True if parent type or any progenitor is a protected interface
12761
12762 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12763 -- Check that a progenitor is compatible with declaration. If an error
12764 -- message is output, it is posted on Error_Node.
12765
12766 ------------------
12767 -- Check_Ifaces --
12768 ------------------
12769
12770 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12771 Iface_Id : constant Entity_Id :=
12772 Defining_Identifier (Parent (Iface_Def));
12773 Type_Def : Node_Id;
12774
12775 begin
12776 if Nkind (N) = N_Private_Extension_Declaration then
12777 Type_Def := N;
12778 else
12779 Type_Def := Type_Definition (N);
12780 end if;
12781
12782 if Is_Task_Interface (Iface_Id) then
12783 Is_Task := True;
12784
12785 elsif Is_Protected_Interface (Iface_Id) then
12786 Is_Protected := True;
12787 end if;
12788
12789 if Is_Synchronized_Interface (Iface_Id) then
12790
12791 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12792 -- extension derived from a synchronized interface must explicitly
12793 -- be declared synchronized, because the full view will be a
12794 -- synchronized type.
12795
12796 if Nkind (N) = N_Private_Extension_Declaration then
12797 if not Synchronized_Present (N) then
12798 Error_Msg_NE
12799 ("private extension of& must be explicitly synchronized",
12800 N, Iface_Id);
12801 end if;
12802
12803 -- However, by 3.9.4(16/2), a full type that is a record extension
12804 -- is never allowed to derive from a synchronized interface (note
12805 -- that interfaces must be excluded from this check, because those
12806 -- are represented by derived type definitions in some cases).
12807
12808 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12809 and then not Interface_Present (Type_Definition (N))
12810 then
12811 Error_Msg_N ("record extension cannot derive from synchronized "
12812 & "interface", Error_Node);
12813 end if;
12814 end if;
12815
12816 -- Check that the characteristics of the progenitor are compatible
12817 -- with the explicit qualifier in the declaration.
12818 -- The check only applies to qualifiers that come from source.
12819 -- Limited_Present also appears in the declaration of corresponding
12820 -- records, and the check does not apply to them.
12821
12822 if Limited_Present (Type_Def)
12823 and then not
12824 Is_Concurrent_Record_Type (Defining_Identifier (N))
12825 then
12826 if Is_Limited_Interface (Parent_Type)
12827 and then not Is_Limited_Interface (Iface_Id)
12828 then
12829 Error_Msg_NE
12830 ("progenitor & must be limited interface",
12831 Error_Node, Iface_Id);
12832
12833 elsif
12834 (Task_Present (Iface_Def)
12835 or else Protected_Present (Iface_Def)
12836 or else Synchronized_Present (Iface_Def))
12837 and then Nkind (N) /= N_Private_Extension_Declaration
12838 and then not Error_Posted (N)
12839 then
12840 Error_Msg_NE
12841 ("progenitor & must be limited interface",
12842 Error_Node, Iface_Id);
12843 end if;
12844
12845 -- Protected interfaces can only inherit from limited, synchronized
12846 -- or protected interfaces.
12847
12848 elsif Nkind (N) = N_Full_Type_Declaration
12849 and then Protected_Present (Type_Def)
12850 then
12851 if Limited_Present (Iface_Def)
12852 or else Synchronized_Present (Iface_Def)
12853 or else Protected_Present (Iface_Def)
12854 then
12855 null;
12856
12857 elsif Task_Present (Iface_Def) then
12858 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12859 & "from task interface", Error_Node);
12860
12861 else
12862 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12863 & "from non-limited interface", Error_Node);
12864 end if;
12865
12866 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12867 -- limited and synchronized.
12868
12869 elsif Synchronized_Present (Type_Def) then
12870 if Limited_Present (Iface_Def)
12871 or else Synchronized_Present (Iface_Def)
12872 then
12873 null;
12874
12875 elsif Protected_Present (Iface_Def)
12876 and then Nkind (N) /= N_Private_Extension_Declaration
12877 then
12878 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12879 & "from protected interface", Error_Node);
12880
12881 elsif Task_Present (Iface_Def)
12882 and then Nkind (N) /= N_Private_Extension_Declaration
12883 then
12884 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12885 & "from task interface", Error_Node);
12886
12887 elsif not Is_Limited_Interface (Iface_Id) then
12888 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12889 & "from non-limited interface", Error_Node);
12890 end if;
12891
12892 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12893 -- synchronized or task interfaces.
12894
12895 elsif Nkind (N) = N_Full_Type_Declaration
12896 and then Task_Present (Type_Def)
12897 then
12898 if Limited_Present (Iface_Def)
12899 or else Synchronized_Present (Iface_Def)
12900 or else Task_Present (Iface_Def)
12901 then
12902 null;
12903
12904 elsif Protected_Present (Iface_Def) then
12905 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12906 & "protected interface", Error_Node);
12907
12908 else
12909 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12910 & "non-limited interface", Error_Node);
12911 end if;
12912 end if;
12913 end Check_Ifaces;
12914
12915 -- Start of processing for Check_Interfaces
12916
12917 begin
12918 if Is_Interface (Parent_Type) then
12919 if Is_Task_Interface (Parent_Type) then
12920 Is_Task := True;
12921
12922 elsif Is_Protected_Interface (Parent_Type) then
12923 Is_Protected := True;
12924 end if;
12925 end if;
12926
12927 if Nkind (N) = N_Private_Extension_Declaration then
12928
12929 -- Check that progenitors are compatible with declaration
12930
12931 Iface := First (Interface_List (Def));
12932 while Present (Iface) loop
12933 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12934
12935 Parent_Node := Parent (Base_Type (Iface_Typ));
12936 Iface_Def := Type_Definition (Parent_Node);
12937
12938 if not Is_Interface (Iface_Typ) then
12939 Diagnose_Interface (Iface, Iface_Typ);
12940 else
12941 Check_Ifaces (Iface_Def, Iface);
12942 end if;
12943
12944 Next (Iface);
12945 end loop;
12946
12947 if Is_Task and Is_Protected then
12948 Error_Msg_N
12949 ("type cannot derive from task and protected interface", N);
12950 end if;
12951
12952 return;
12953 end if;
12954
12955 -- Full type declaration of derived type.
12956 -- Check compatibility with parent if it is interface type
12957
12958 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12959 and then Is_Interface (Parent_Type)
12960 then
12961 Parent_Node := Parent (Parent_Type);
12962
12963 -- More detailed checks for interface varieties
12964
12965 Check_Ifaces
12966 (Iface_Def => Type_Definition (Parent_Node),
12967 Error_Node => Subtype_Indication (Type_Definition (N)));
12968 end if;
12969
12970 Iface := First (Interface_List (Def));
12971 while Present (Iface) loop
12972 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12973
12974 Parent_Node := Parent (Base_Type (Iface_Typ));
12975 Iface_Def := Type_Definition (Parent_Node);
12976
12977 if not Is_Interface (Iface_Typ) then
12978 Diagnose_Interface (Iface, Iface_Typ);
12979
12980 else
12981 -- "The declaration of a specific descendant of an interface
12982 -- type freezes the interface type" RM 13.14
12983
12984 Freeze_Before (N, Iface_Typ);
12985 Check_Ifaces (Iface_Def, Error_Node => Iface);
12986 end if;
12987
12988 Next (Iface);
12989 end loop;
12990
12991 if Is_Task and Is_Protected then
12992 Error_Msg_N
12993 ("type cannot derive from task and protected interface", N);
12994 end if;
12995 end Check_Interfaces;
12996
12997 ------------------------------------
12998 -- Check_Or_Process_Discriminants --
12999 ------------------------------------
13000
13001 -- If an incomplete or private type declaration was already given for the
13002 -- type, the discriminants may have already been processed if they were
13003 -- present on the incomplete declaration. In this case a full conformance
13004 -- check has been performed in Find_Type_Name, and we then recheck here
13005 -- some properties that can't be checked on the partial view alone.
13006 -- Otherwise we call Process_Discriminants.
13007
13008 procedure Check_Or_Process_Discriminants
13009 (N : Node_Id;
13010 T : Entity_Id;
13011 Prev : Entity_Id := Empty)
13012 is
13013 begin
13014 if Has_Discriminants (T) then
13015
13016 -- Discriminants are already set on T if they were already present
13017 -- on the partial view. Make them visible to component declarations.
13018
13019 declare
13020 D : Entity_Id;
13021 -- Discriminant on T (full view) referencing expr on partial view
13022
13023 Prev_D : Entity_Id;
13024 -- Entity of corresponding discriminant on partial view
13025
13026 New_D : Node_Id;
13027 -- Discriminant specification for full view, expression is
13028 -- the syntactic copy on full view (which has been checked for
13029 -- conformance with partial view), only used here to post error
13030 -- message.
13031
13032 begin
13033 D := First_Discriminant (T);
13034 New_D := First (Discriminant_Specifications (N));
13035 while Present (D) loop
13036 Prev_D := Current_Entity (D);
13037 Set_Current_Entity (D);
13038 Set_Is_Immediately_Visible (D);
13039 Set_Homonym (D, Prev_D);
13040
13041 -- Handle the case where there is an untagged partial view and
13042 -- the full view is tagged: must disallow discriminants with
13043 -- defaults, unless compiling for Ada 2012, which allows a
13044 -- limited tagged type to have defaulted discriminants (see
13045 -- AI05-0214). However, suppress error here if it was already
13046 -- reported on the default expression of the partial view.
13047
13048 if Is_Tagged_Type (T)
13049 and then Present (Expression (Parent (D)))
13050 and then (not Is_Limited_Type (Current_Scope)
13051 or else Ada_Version < Ada_2012)
13052 and then not Error_Posted (Expression (Parent (D)))
13053 then
13054 if Ada_Version >= Ada_2012 then
13055 Error_Msg_N
13056 ("discriminants of nonlimited tagged type cannot have "
13057 & "defaults",
13058 Expression (New_D));
13059 else
13060 Error_Msg_N
13061 ("discriminants of tagged type cannot have defaults",
13062 Expression (New_D));
13063 end if;
13064 end if;
13065
13066 -- Ada 2005 (AI-230): Access discriminant allowed in
13067 -- non-limited record types.
13068
13069 if Ada_Version < Ada_2005 then
13070
13071 -- This restriction gets applied to the full type here. It
13072 -- has already been applied earlier to the partial view.
13073
13074 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
13075 end if;
13076
13077 Next_Discriminant (D);
13078 Next (New_D);
13079 end loop;
13080 end;
13081
13082 elsif Present (Discriminant_Specifications (N)) then
13083 Process_Discriminants (N, Prev);
13084 end if;
13085 end Check_Or_Process_Discriminants;
13086
13087 ----------------------
13088 -- Check_Real_Bound --
13089 ----------------------
13090
13091 procedure Check_Real_Bound (Bound : Node_Id) is
13092 begin
13093 if not Is_Real_Type (Etype (Bound)) then
13094 Error_Msg_N
13095 ("bound in real type definition must be of real type", Bound);
13096
13097 elsif not Is_OK_Static_Expression (Bound) then
13098 Flag_Non_Static_Expr
13099 ("non-static expression used for real type bound!", Bound);
13100
13101 else
13102 return;
13103 end if;
13104
13105 Rewrite
13106 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
13107 Analyze (Bound);
13108 Resolve (Bound, Standard_Float);
13109 end Check_Real_Bound;
13110
13111 ------------------------------
13112 -- Complete_Private_Subtype --
13113 ------------------------------
13114
13115 procedure Complete_Private_Subtype
13116 (Priv : Entity_Id;
13117 Full : Entity_Id;
13118 Full_Base : Entity_Id;
13119 Related_Nod : Node_Id)
13120 is
13121 Save_Next_Entity : Entity_Id;
13122 Save_Homonym : Entity_Id;
13123
13124 begin
13125 -- Set semantic attributes for (implicit) private subtype completion.
13126 -- If the full type has no discriminants, then it is a copy of the
13127 -- full view of the base. Otherwise, it is a subtype of the base with
13128 -- a possible discriminant constraint. Save and restore the original
13129 -- Next_Entity field of full to ensure that the calls to Copy_Node do
13130 -- not corrupt the entity chain.
13131
13132 Save_Next_Entity := Next_Entity (Full);
13133 Save_Homonym := Homonym (Priv);
13134
13135 if Is_Private_Type (Full_Base)
13136 or else Is_Record_Type (Full_Base)
13137 or else Is_Concurrent_Type (Full_Base)
13138 then
13139 Copy_Node (Priv, Full);
13140
13141 -- Note that the Etype of the full view is the same as the Etype of
13142 -- the partial view. In this fashion, the subtype has access to the
13143 -- correct view of the parent.
13144
13145 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
13146 Set_Has_Unknown_Discriminants
13147 (Full, Has_Unknown_Discriminants (Full_Base));
13148 Set_First_Entity (Full, First_Entity (Full_Base));
13149 Set_Last_Entity (Full, Last_Entity (Full_Base));
13150
13151 -- If the underlying base type is constrained, we know that the
13152 -- full view of the subtype is constrained as well (the converse
13153 -- is not necessarily true).
13154
13155 if Is_Constrained (Full_Base) then
13156 Set_Is_Constrained (Full);
13157 end if;
13158
13159 else
13160 Copy_Node (Full_Base, Full);
13161
13162 -- The following subtlety with the Etype of the full view needs to be
13163 -- taken into account here. One could think that it must naturally be
13164 -- set to the base type of the full base:
13165
13166 -- Set_Etype (Full, Base_Type (Full_Base));
13167
13168 -- so that the full view becomes a subtype of the full base when the
13169 -- latter is a base type, which must for example happen when the full
13170 -- base is declared as derived type. That's also correct if the full
13171 -- base is declared as an array type, or a floating-point type, or a
13172 -- fixed-point type, or a signed integer type, as these declarations
13173 -- create an implicit base type and a first subtype so the Etype of
13174 -- the full views must be the implicit base type. But that's wrong
13175 -- if the full base is declared as an access type, or an enumeration
13176 -- type, or a modular integer type, as these declarations directly
13177 -- create a base type, i.e. with Etype pointing to itself. Moreover
13178 -- the full base being declared in the private part, i.e. when the
13179 -- views are swapped, the end result is that the Etype of the full
13180 -- base is set to its private view in this case and that we need to
13181 -- propagate this setting to the full view in order for the subtype
13182 -- to be compatible with the base type.
13183
13184 if Is_Base_Type (Full_Base)
13185 and then (Is_Derived_Type (Full_Base)
13186 or else Ekind (Full_Base) in Array_Kind
13187 or else Ekind (Full_Base) in Fixed_Point_Kind
13188 or else Ekind (Full_Base) in Float_Kind
13189 or else Ekind (Full_Base) in Signed_Integer_Kind)
13190 then
13191 Set_Etype (Full, Full_Base);
13192 end if;
13193
13194 Set_Chars (Full, Chars (Priv));
13195 Set_Sloc (Full, Sloc (Priv));
13196 Conditional_Delay (Full, Priv);
13197 end if;
13198
13199 Link_Entities (Full, Save_Next_Entity);
13200 Set_Homonym (Full, Save_Homonym);
13201 Set_Associated_Node_For_Itype (Full, Related_Nod);
13202
13203 if Ekind (Full) in Incomplete_Or_Private_Kind then
13204 Reinit_Field_To_Zero (Full, F_Private_Dependents);
13205 end if;
13206
13207 -- Set common attributes for all subtypes: kind, convention, etc.
13208
13209 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
13210 Set_Is_Not_Self_Hidden (Full);
13211 Set_Convention (Full, Convention (Full_Base));
13212 Set_Is_First_Subtype (Full, False);
13213 Set_Scope (Full, Scope (Priv));
13214 Set_Size_Info (Full, Full_Base);
13215 Copy_RM_Size (To => Full, From => Full_Base);
13216 Set_Is_Itype (Full);
13217
13218 -- A subtype of a private-type-without-discriminants, whose full-view
13219 -- has discriminants with default expressions, is not constrained.
13220
13221 if not Has_Discriminants (Priv) then
13222 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
13223
13224 if Has_Discriminants (Full_Base) then
13225 Set_Discriminant_Constraint
13226 (Full, Discriminant_Constraint (Full_Base));
13227
13228 -- The partial view may have been indefinite, the full view
13229 -- might not be.
13230
13231 Set_Has_Unknown_Discriminants
13232 (Full, Has_Unknown_Discriminants (Full_Base));
13233 end if;
13234 end if;
13235
13236 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
13237 Set_Depends_On_Private (Full, Has_Private_Component (Full));
13238
13239 -- When prefixed calls are enabled for untagged types, the subtype
13240 -- shares the primitive operations of its base type. Do this even
13241 -- when GNAT extensions are not allowed, in order to give better
13242 -- error messages.
13243
13244 Set_Direct_Primitive_Operations
13245 (Full, Direct_Primitive_Operations (Full_Base));
13246
13247 -- Freeze the private subtype entity if its parent is delayed, and not
13248 -- already frozen. We skip this processing if the type is an anonymous
13249 -- subtype of a record component, or is the corresponding record of a
13250 -- protected type, since these are processed when the enclosing type
13251 -- is frozen. If the parent type is declared in a nested package then
13252 -- the freezing of the private and full views also happens later.
13253
13254 if not Is_Type (Scope (Full)) then
13255 if Is_Itype (Priv)
13256 and then In_Same_Source_Unit (Full, Full_Base)
13257 and then Scope (Full_Base) /= Scope (Full)
13258 then
13259 Set_Has_Delayed_Freeze (Full);
13260 Set_Has_Delayed_Freeze (Priv);
13261
13262 else
13263 Set_Has_Delayed_Freeze (Full,
13264 Has_Delayed_Freeze (Full_Base)
13265 and then not Is_Frozen (Full_Base));
13266 end if;
13267 end if;
13268
13269 Set_Freeze_Node (Full, Empty);
13270 Set_Is_Frozen (Full, False);
13271
13272 if Has_Discriminants (Full) then
13273 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
13274 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
13275
13276 if Has_Unknown_Discriminants (Full) then
13277 Set_Discriminant_Constraint (Full, No_Elist);
13278 end if;
13279 end if;
13280
13281 if Ekind (Full_Base) = E_Record_Type
13282 and then Has_Discriminants (Full_Base)
13283 and then Has_Discriminants (Priv) -- might not, if errors
13284 and then not Has_Unknown_Discriminants (Priv)
13285 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13286 then
13287 Create_Constrained_Components
13288 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13289
13290 -- If the full base is itself derived from private, build a congruent
13291 -- subtype of its underlying full view, for use by the back end.
13292
13293 elsif Is_Private_Type (Full_Base)
13294 and then Present (Underlying_Full_View (Full_Base))
13295 then
13296 declare
13297 Underlying_Full_Base : constant Entity_Id :=
13298 Underlying_Full_View (Full_Base);
13299 Underlying_Full : constant Entity_Id :=
13300 Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13301 begin
13302 Set_Is_Itype (Underlying_Full);
13303 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13304 Complete_Private_Subtype
13305 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13306 Set_Underlying_Full_View (Full, Underlying_Full);
13307 Set_Is_Underlying_Full_View (Underlying_Full);
13308 end;
13309
13310 elsif Is_Record_Type (Full_Base) then
13311
13312 -- Show Full is simply a renaming of Full_Base
13313
13314 Set_Cloned_Subtype (Full, Full_Base);
13315 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13316
13317 -- Propagate predicates
13318
13319 Propagate_Predicate_Attributes (Full, Full_Base);
13320 end if;
13321
13322 -- It is unsafe to share the bounds of a scalar type, because the Itype
13323 -- is elaborated on demand, and if a bound is nonstatic, then different
13324 -- orders of elaboration in different units will lead to different
13325 -- external symbols.
13326
13327 if Is_Scalar_Type (Full_Base) then
13328 Set_Scalar_Range (Full,
13329 Make_Range (Sloc (Related_Nod),
13330 Low_Bound =>
13331 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13332 High_Bound =>
13333 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13334
13335 -- This completion inherits the bounds of the full parent, but if
13336 -- the parent is an unconstrained floating point type, so is the
13337 -- completion.
13338
13339 if Is_Floating_Point_Type (Full_Base) then
13340 Set_Includes_Infinities
13341 (Scalar_Range (Full), Has_Infinities (Full_Base));
13342 end if;
13343 end if;
13344
13345 -- ??? It seems that a lot of fields are missing that should be copied
13346 -- from Full_Base to Full. Here are some that are introduced in a
13347 -- non-disruptive way but a cleanup is necessary.
13348
13349 if Is_Tagged_Type (Full_Base) then
13350 Set_Is_Tagged_Type (Full);
13351 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13352
13353 Set_No_Tagged_Streams_Pragma
13354 (Full, No_Tagged_Streams_Pragma (Full_Base));
13355
13356 if Is_Interface (Full_Base) then
13357 Set_Is_Interface (Full);
13358 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13359 end if;
13360
13361 -- Inherit class_wide type of full_base in case the partial view was
13362 -- not tagged. Otherwise it has already been created when the private
13363 -- subtype was analyzed.
13364
13365 if No (Class_Wide_Type (Full)) then
13366 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13367 end if;
13368
13369 -- If this is a subtype of a protected or task type, constrain its
13370 -- corresponding record, unless this is a subtype without constraints,
13371 -- i.e. a simple renaming as with an actual subtype in an instance.
13372
13373 elsif Is_Concurrent_Type (Full_Base) then
13374 if Has_Discriminants (Full)
13375 and then Present (Corresponding_Record_Type (Full_Base))
13376 and then
13377 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13378 then
13379 Set_Corresponding_Record_Type (Full,
13380 Constrain_Corresponding_Record
13381 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13382
13383 else
13384 Set_Corresponding_Record_Type (Full,
13385 Corresponding_Record_Type (Full_Base));
13386 end if;
13387 end if;
13388
13389 -- Link rep item chain, and also setting of Has_Predicates from private
13390 -- subtype to full subtype, since we will need these on the full subtype
13391 -- to create the predicate function. Note that the full subtype may
13392 -- already have rep items, inherited from the full view of the base
13393 -- type, so we must be sure not to overwrite these entries.
13394
13395 declare
13396 Append : Boolean;
13397 Item : Node_Id;
13398 Next_Item : Node_Id;
13399 Priv_Item : Node_Id;
13400
13401 begin
13402 Item := First_Rep_Item (Full);
13403 Priv_Item := First_Rep_Item (Priv);
13404
13405 -- If no existing rep items on full type, we can just link directly
13406 -- to the list of items on the private type, if any exist.. Same if
13407 -- the rep items are only those inherited from the base
13408
13409 if (No (Item)
13410 or else Nkind (Item) /= N_Aspect_Specification
13411 or else Entity (Item) = Full_Base)
13412 and then Present (First_Rep_Item (Priv))
13413 then
13414 Set_First_Rep_Item (Full, Priv_Item);
13415
13416 -- Otherwise, search to the end of items currently linked to the full
13417 -- subtype and append the private items to the end. However, if Priv
13418 -- and Full already have the same list of rep items, then the append
13419 -- is not done, as that would create a circularity.
13420 --
13421 -- The partial view may have a predicate and the rep item lists of
13422 -- both views agree when inherited from the same ancestor. In that
13423 -- case, simply propagate the list from one view to the other.
13424 -- A more complex analysis needed here ???
13425
13426 elsif Present (Priv_Item)
13427 and then Item = Next_Rep_Item (Priv_Item)
13428 then
13429 Set_First_Rep_Item (Full, Priv_Item);
13430
13431 elsif Item /= Priv_Item then
13432 Append := True;
13433 loop
13434 Next_Item := Next_Rep_Item (Item);
13435 exit when No (Next_Item);
13436 Item := Next_Item;
13437
13438 -- If the private view has aspect specifications, the full view
13439 -- inherits them. Since these aspects may already have been
13440 -- attached to the full view during derivation, do not append
13441 -- them if already present.
13442
13443 if Item = First_Rep_Item (Priv) then
13444 Append := False;
13445 exit;
13446 end if;
13447 end loop;
13448
13449 -- And link the private type items at the end of the chain
13450
13451 if Append then
13452 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13453 end if;
13454 end if;
13455 end;
13456
13457 -- Make sure Has_Predicates is set on full type if it is set on the
13458 -- private type. Note that it may already be set on the full type and
13459 -- if so, we don't want to unset it. Similarly, propagate information
13460 -- about delayed aspects, because the corresponding pragmas must be
13461 -- analyzed when one of the views is frozen. This last step is needed
13462 -- in particular when the full type is a scalar type for which an
13463 -- anonymous base type is constructed.
13464
13465 -- The predicate functions are generated either at the freeze point
13466 -- of the type or at the end of the visible part, and we must avoid
13467 -- generating them twice.
13468
13469 Propagate_Predicate_Attributes (Full, Priv);
13470
13471 if Has_Delayed_Aspects (Priv) then
13472 Set_Has_Delayed_Aspects (Full);
13473 end if;
13474 end Complete_Private_Subtype;
13475
13476 ----------------------------
13477 -- Constant_Redeclaration --
13478 ----------------------------
13479
13480 procedure Constant_Redeclaration
13481 (Id : Entity_Id;
13482 N : Node_Id;
13483 T : out Entity_Id)
13484 is
13485 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13486 Obj_Def : constant Node_Id := Object_Definition (N);
13487 New_T : Entity_Id;
13488
13489 procedure Check_Possible_Deferred_Completion
13490 (Prev_Id : Entity_Id;
13491 Curr_Obj_Def : Node_Id);
13492 -- Determine whether the two object definitions describe the partial
13493 -- and the full view of a constrained deferred constant. Generate
13494 -- a subtype for the full view and verify that it statically matches
13495 -- the subtype of the partial view.
13496
13497 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13498 -- If deferred constant is an access type initialized with an allocator,
13499 -- check whether there is an illegal recursion in the definition,
13500 -- through a default value of some record subcomponent. This is normally
13501 -- detected when generating init procs, but requires this additional
13502 -- mechanism when expansion is disabled.
13503
13504 ----------------------------------------
13505 -- Check_Possible_Deferred_Completion --
13506 ----------------------------------------
13507
13508 procedure Check_Possible_Deferred_Completion
13509 (Prev_Id : Entity_Id;
13510 Curr_Obj_Def : Node_Id)
13511 is
13512 Curr_Typ : Entity_Id;
13513 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13514 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13515 Mismatch : Boolean := False;
13516 begin
13517 if Anon_Acc then
13518 null;
13519 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13520 declare
13521 Loc : constant Source_Ptr := Sloc (N);
13522 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13523 Decl : constant Node_Id :=
13524 Make_Subtype_Declaration (Loc,
13525 Defining_Identifier => Def_Id,
13526 Subtype_Indication =>
13527 Relocate_Node (Curr_Obj_Def));
13528
13529 begin
13530 Insert_Before_And_Analyze (N, Decl);
13531 Set_Etype (Id, Def_Id);
13532 Curr_Typ := Def_Id;
13533 end;
13534 else
13535 Curr_Typ := Etype (Curr_Obj_Def);
13536 end if;
13537
13538 if Anon_Acc then
13539 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13540 Mismatch := True;
13541 elsif Has_Null_Exclusion (Prev_Typ)
13542 and then not Null_Exclusion_Present (Curr_Obj_Def)
13543 then
13544 Mismatch := True;
13545 end if;
13546 -- ??? Another check needed: mismatch if disagreement
13547 -- between designated types/profiles .
13548 else
13549 Mismatch :=
13550 Is_Constrained (Prev_Typ)
13551 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13552 end if;
13553
13554 if Mismatch then
13555 Error_Msg_Sloc := Sloc (Prev_Id);
13556 Error_Msg_N ("subtype does not statically match deferred "
13557 & "declaration #", N);
13558 end if;
13559 end Check_Possible_Deferred_Completion;
13560
13561 ---------------------------------
13562 -- Check_Recursive_Declaration --
13563 ---------------------------------
13564
13565 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13566 Comp : Entity_Id;
13567
13568 begin
13569 if Is_Record_Type (Typ) then
13570 Comp := First_Component (Typ);
13571 while Present (Comp) loop
13572 if Comes_From_Source (Comp) then
13573 if Present (Expression (Parent (Comp)))
13574 and then Is_Entity_Name (Expression (Parent (Comp)))
13575 and then Entity (Expression (Parent (Comp))) = Prev
13576 then
13577 Error_Msg_Sloc := Sloc (Parent (Comp));
13578 Error_Msg_NE
13579 ("illegal circularity with declaration for & #",
13580 N, Comp);
13581 return;
13582
13583 elsif Is_Record_Type (Etype (Comp)) then
13584 Check_Recursive_Declaration (Etype (Comp));
13585 end if;
13586 end if;
13587
13588 Next_Component (Comp);
13589 end loop;
13590 end if;
13591 end Check_Recursive_Declaration;
13592
13593 -- Start of processing for Constant_Redeclaration
13594
13595 begin
13596 if Nkind (Parent (Prev)) = N_Object_Declaration then
13597 if Nkind (Object_Definition
13598 (Parent (Prev))) = N_Subtype_Indication
13599 then
13600 -- Find type of new declaration. The constraints of the two
13601 -- views must match statically, but there is no point in
13602 -- creating an itype for the full view.
13603
13604 if Nkind (Obj_Def) = N_Subtype_Indication then
13605 Find_Type (Subtype_Mark (Obj_Def));
13606 New_T := Entity (Subtype_Mark (Obj_Def));
13607
13608 else
13609 Find_Type (Obj_Def);
13610 New_T := Entity (Obj_Def);
13611 end if;
13612
13613 T := Etype (Prev);
13614
13615 else
13616 -- The full view may impose a constraint, even if the partial
13617 -- view does not, so construct the subtype.
13618
13619 New_T := Find_Type_Of_Object (Obj_Def, N);
13620 T := New_T;
13621 end if;
13622
13623 else
13624 -- Current declaration is illegal, diagnosed below in Enter_Name
13625
13626 T := Empty;
13627 New_T := Any_Type;
13628 end if;
13629
13630 -- If previous full declaration or a renaming declaration exists, or if
13631 -- a homograph is present, let Enter_Name handle it, either with an
13632 -- error or with the removal of an overridden implicit subprogram.
13633 -- The previous one is a full declaration if it has an expression
13634 -- (which in the case of an aggregate is indicated by the Init flag).
13635
13636 if Ekind (Prev) /= E_Constant
13637 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13638 or else Present (Expression (Parent (Prev)))
13639 or else Has_Init_Expression (Parent (Prev))
13640 or else Present (Full_View (Prev))
13641 then
13642 Enter_Name (Id);
13643
13644 -- Verify that types of both declarations match, or else that both types
13645 -- are anonymous access types whose designated subtypes statically match
13646 -- (as allowed in Ada 2005 by AI-385).
13647
13648 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13649 and then
13650 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13651 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13652 or else Is_Access_Constant (Etype (New_T)) /=
13653 Is_Access_Constant (Etype (Prev))
13654 or else Can_Never_Be_Null (Etype (New_T)) /=
13655 Can_Never_Be_Null (Etype (Prev))
13656 or else Null_Exclusion_Present (Parent (Prev)) /=
13657 Null_Exclusion_Present (Parent (Id))
13658 or else not Subtypes_Statically_Match
13659 (Designated_Type (Etype (Prev)),
13660 Designated_Type (Etype (New_T))))
13661 then
13662 Error_Msg_Sloc := Sloc (Prev);
13663 Error_Msg_N ("type does not match declaration#", N);
13664 Set_Full_View (Prev, Id);
13665 Set_Etype (Id, Any_Type);
13666
13667 -- A deferred constant whose type is an anonymous array is always
13668 -- illegal (unless imported). A detailed error message might be
13669 -- helpful for Ada beginners.
13670
13671 if Nkind (Object_Definition (Parent (Prev)))
13672 = N_Constrained_Array_Definition
13673 and then Nkind (Object_Definition (N))
13674 = N_Constrained_Array_Definition
13675 then
13676 Error_Msg_N ("\each anonymous array is a distinct type", N);
13677 Error_Msg_N ("a deferred constant must have a named type",
13678 Object_Definition (Parent (Prev)));
13679 end if;
13680
13681 elsif
13682 Null_Exclusion_Present (Parent (Prev))
13683 and then not Null_Exclusion_Present (N)
13684 then
13685 Error_Msg_Sloc := Sloc (Prev);
13686 Error_Msg_N ("null-exclusion does not match declaration#", N);
13687 Set_Full_View (Prev, Id);
13688 Set_Etype (Id, Any_Type);
13689
13690 -- If so, process the full constant declaration
13691
13692 else
13693 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13694 -- the deferred declaration is constrained, then the subtype defined
13695 -- by the subtype_indication in the full declaration shall match it
13696 -- statically.
13697
13698 Check_Possible_Deferred_Completion
13699 (Prev_Id => Prev,
13700 Curr_Obj_Def => Obj_Def);
13701
13702 Set_Full_View (Prev, Id);
13703 Set_Is_Public (Id, Is_Public (Prev));
13704 Set_Is_Internal (Id);
13705 Append_Entity (Id, Current_Scope);
13706
13707 -- Check ALIASED present if present before (RM 7.4(7))
13708
13709 if Is_Aliased (Prev)
13710 and then not Aliased_Present (N)
13711 then
13712 Error_Msg_Sloc := Sloc (Prev);
13713 Error_Msg_N ("ALIASED required (see declaration #)", N);
13714 end if;
13715
13716 -- Check that placement is in private part and that the incomplete
13717 -- declaration appeared in the visible part.
13718
13719 if Ekind (Current_Scope) = E_Package
13720 and then not In_Private_Part (Current_Scope)
13721 then
13722 Error_Msg_Sloc := Sloc (Prev);
13723 Error_Msg_N
13724 ("full constant for declaration # must be in private part", N);
13725
13726 elsif Ekind (Current_Scope) = E_Package
13727 and then
13728 List_Containing (Parent (Prev)) /=
13729 Visible_Declarations (Package_Specification (Current_Scope))
13730 then
13731 Error_Msg_N
13732 ("deferred constant must be declared in visible part",
13733 Parent (Prev));
13734 end if;
13735
13736 if Is_Access_Type (T)
13737 and then Nkind (Expression (N)) = N_Allocator
13738 then
13739 Check_Recursive_Declaration (Designated_Type (T));
13740 end if;
13741
13742 -- A deferred constant is a visible entity. If type has invariants,
13743 -- verify that the initial value satisfies them. This is not done in
13744 -- GNATprove mode, as GNATprove handles invariant checks itself.
13745
13746 if Has_Invariants (T)
13747 and then Present (Invariant_Procedure (T))
13748 and then not GNATprove_Mode
13749 then
13750 Insert_After (N,
13751 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13752 end if;
13753 end if;
13754 end Constant_Redeclaration;
13755
13756 ----------------------
13757 -- Constrain_Access --
13758 ----------------------
13759
13760 procedure Constrain_Access
13761 (Def_Id : in out Entity_Id;
13762 S : Node_Id;
13763 Related_Nod : Node_Id)
13764 is
13765 T : constant Entity_Id := Entity (Subtype_Mark (S));
13766 Desig_Type : constant Entity_Id := Designated_Type (T);
13767 Desig_Subtype : Entity_Id;
13768 Constraint_OK : Boolean := True;
13769
13770 begin
13771 if Is_Array_Type (Desig_Type) then
13772 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13773 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13774
13775 elsif (Is_Record_Type (Desig_Type)
13776 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13777 and then not Is_Constrained (Desig_Type)
13778 then
13779 -- If this is a constrained access definition for a record
13780 -- component, we leave the type as an unconstrained access,
13781 -- and mark the component so that its actual type is built
13782 -- at a point of use (e.g., an assignment statement). This
13783 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13784
13785 if Desig_Type = Current_Scope
13786 and then No (Def_Id)
13787 then
13788 Desig_Subtype :=
13789 Create_Itype
13790 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13791 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13792 Def_Id := Entity (Subtype_Mark (S));
13793
13794 -- We indicate that the component has a per-object constraint
13795 -- for treatment at a point of use, even though the constraint
13796 -- may be independent of discriminants of the enclosing type.
13797
13798 if Nkind (Related_Nod) = N_Component_Declaration then
13799 Set_Has_Per_Object_Constraint
13800 (Defining_Identifier (Related_Nod));
13801 end if;
13802
13803 -- This call added to ensure that the constraint is analyzed
13804 -- (needed for a B test). Note that we still return early from
13805 -- this procedure to avoid recursive processing.
13806
13807 Constrain_Discriminated_Type
13808 (Desig_Subtype, S, Related_Nod, For_Access => True);
13809 return;
13810 end if;
13811
13812 -- Enforce rule that the constraint is illegal if there is an
13813 -- unconstrained view of the designated type. This means that the
13814 -- partial view (either a private type declaration or a derivation
13815 -- from a private type) has no discriminants. (Defect Report
13816 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13817
13818 -- Rule updated for Ada 2005: The private type is said to have
13819 -- a constrained partial view, given that objects of the type
13820 -- can be declared. Furthermore, the rule applies to all access
13821 -- types, unlike the rule concerning default discriminants (see
13822 -- RM 3.7.1(7/3))
13823
13824 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13825 and then Has_Private_Declaration (Desig_Type)
13826 and then In_Open_Scopes (Scope (Desig_Type))
13827 and then Has_Discriminants (Desig_Type)
13828 then
13829 declare
13830 Pack : constant Node_Id :=
13831 Unit_Declaration_Node (Scope (Desig_Type));
13832 Decls : List_Id;
13833 Decl : Node_Id;
13834
13835 begin
13836 if Nkind (Pack) = N_Package_Declaration then
13837 Decls := Visible_Declarations (Specification (Pack));
13838 Decl := First (Decls);
13839 while Present (Decl) loop
13840 if (Nkind (Decl) = N_Private_Type_Declaration
13841 and then Chars (Defining_Identifier (Decl)) =
13842 Chars (Desig_Type))
13843
13844 or else
13845 (Nkind (Decl) = N_Full_Type_Declaration
13846 and then
13847 Chars (Defining_Identifier (Decl)) =
13848 Chars (Desig_Type)
13849 and then Is_Derived_Type (Desig_Type)
13850 and then
13851 Has_Private_Declaration (Etype (Desig_Type)))
13852 then
13853 if No (Discriminant_Specifications (Decl)) then
13854 Error_Msg_N
13855 ("cannot constrain access type if designated "
13856 & "type has constrained partial view", S);
13857 end if;
13858
13859 exit;
13860 end if;
13861
13862 Next (Decl);
13863 end loop;
13864 end if;
13865 end;
13866 end if;
13867
13868 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13869 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13870 For_Access => True);
13871
13872 elsif Is_Concurrent_Type (Desig_Type)
13873 and then not Is_Constrained (Desig_Type)
13874 then
13875 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13876 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13877
13878 else
13879 Error_Msg_N ("invalid constraint on access type", S);
13880
13881 -- We simply ignore an invalid constraint
13882
13883 Desig_Subtype := Desig_Type;
13884 Constraint_OK := False;
13885 end if;
13886
13887 if No (Def_Id) then
13888 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13889 else
13890 Mutate_Ekind (Def_Id, E_Access_Subtype);
13891 end if;
13892
13893 if Constraint_OK then
13894 Set_Etype (Def_Id, Base_Type (T));
13895
13896 if Is_Private_Type (Desig_Type) then
13897 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13898 end if;
13899 else
13900 Set_Etype (Def_Id, Any_Type);
13901 end if;
13902
13903 Set_Size_Info (Def_Id, T);
13904 Set_Is_Constrained (Def_Id, Constraint_OK);
13905 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13906 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13907 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13908 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13909
13910 Conditional_Delay (Def_Id, T);
13911
13912 -- AI-363 : Subtypes of general access types whose designated types have
13913 -- default discriminants are disallowed. In instances, the rule has to
13914 -- be checked against the actual, of which T is the subtype. In a
13915 -- generic body, the rule is checked assuming that the actual type has
13916 -- defaulted discriminants.
13917
13918 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13919 if Ekind (Base_Type (T)) = E_General_Access_Type
13920 and then Has_Defaulted_Discriminants (Desig_Type)
13921 then
13922 if Ada_Version < Ada_2005 then
13923 Error_Msg_N
13924 ("access subtype of general access type would not " &
13925 "be allowed in Ada 2005?y?", S);
13926 else
13927 Error_Msg_N
13928 ("access subtype of general access type not allowed", S);
13929 end if;
13930
13931 Error_Msg_N ("\discriminants have defaults", S);
13932
13933 elsif Is_Access_Type (T)
13934 and then Is_Generic_Type (Desig_Type)
13935 and then Has_Discriminants (Desig_Type)
13936 and then In_Package_Body (Current_Scope)
13937 then
13938 if Ada_Version < Ada_2005 then
13939 Error_Msg_N
13940 ("access subtype would not be allowed in generic body "
13941 & "in Ada 2005?y?", S);
13942 else
13943 Error_Msg_N
13944 ("access subtype not allowed in generic body", S);
13945 end if;
13946
13947 Error_Msg_N
13948 ("\designated type is a discriminated formal", S);
13949 end if;
13950 end if;
13951 end Constrain_Access;
13952
13953 ---------------------
13954 -- Constrain_Array --
13955 ---------------------
13956
13957 procedure Constrain_Array
13958 (Def_Id : in out Entity_Id;
13959 SI : Node_Id;
13960 Related_Nod : Node_Id;
13961 Related_Id : Entity_Id;
13962 Suffix : Character)
13963 is
13964 C : constant Node_Id := Constraint (SI);
13965 Number_Of_Constraints : constant Nat := List_Length (Constraints (C));
13966 Index : Node_Id;
13967 S, T : Entity_Id;
13968 Constraint_OK : Boolean := True;
13969 Is_FLB_Array_Subtype : Boolean := False;
13970
13971 begin
13972 T := Entity (Subtype_Mark (SI));
13973
13974 if Is_Access_Type (T) then
13975 T := Designated_Type (T);
13976 end if;
13977
13978 T := Underlying_Type (T);
13979
13980 -- If an index constraint follows a subtype mark in a subtype indication
13981 -- then the type or subtype denoted by the subtype mark must not already
13982 -- impose an index constraint. The subtype mark must denote either an
13983 -- unconstrained array type or an access type whose designated type
13984 -- is such an array type... (RM 3.6.1)
13985
13986 if Is_Constrained (T) then
13987 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13988 Constraint_OK := False;
13989
13990 else
13991 -- In either case, the index constraint must provide a discrete
13992 -- range for each index of the array type and the type of each
13993 -- discrete range must be the same as that of the corresponding
13994 -- index. (RM 3.6.1)
13995
13996 if Number_Of_Constraints /= Number_Dimensions (T) then
13997 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13998 Constraint_OK := False;
13999
14000 else
14001 S := First (Constraints (C));
14002 Index := First_Index (T);
14003 Analyze (Index);
14004
14005 -- Apply constraints to each index type
14006
14007 for J in 1 .. Number_Of_Constraints loop
14008 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
14009
14010 -- If the subtype of the index has been set to indicate that
14011 -- it has a fixed lower bound, then record that the subtype's
14012 -- entity will need to be marked as being a fixed-lower-bound
14013 -- array subtype.
14014
14015 if S = First (Constraints (C)) then
14016 Is_FLB_Array_Subtype :=
14017 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
14018
14019 -- If the parent subtype (or should this be Etype of that?)
14020 -- is an FLB array subtype, we flag an error, because we
14021 -- don't currently allow subtypes of such subtypes to
14022 -- specify a fixed lower bound for any of their indexes,
14023 -- even if the index of the parent subtype is a "range <>"
14024 -- index.
14025
14026 if Is_FLB_Array_Subtype
14027 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
14028 then
14029 Error_Msg_NE
14030 ("index with fixed lower bound not allowed for subtype "
14031 & "of fixed-lower-bound }", S, T);
14032
14033 Is_FLB_Array_Subtype := False;
14034 end if;
14035
14036 elsif Is_FLB_Array_Subtype
14037 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
14038 then
14039 Error_Msg_NE
14040 ("constrained index not allowed for fixed-lower-bound "
14041 & "subtype of}", S, T);
14042
14043 elsif not Is_FLB_Array_Subtype
14044 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
14045 then
14046 Error_Msg_NE
14047 ("index with fixed lower bound not allowed for "
14048 & "constrained subtype of}", S, T);
14049 end if;
14050
14051 Next (Index);
14052 Next (S);
14053 end loop;
14054
14055 end if;
14056 end if;
14057
14058 if No (Def_Id) then
14059 Def_Id :=
14060 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
14061 Set_Parent (Def_Id, Related_Nod);
14062
14063 else
14064 Mutate_Ekind (Def_Id, E_Array_Subtype);
14065 end if;
14066
14067 Set_Size_Info (Def_Id, (T));
14068 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14069 Set_Etype (Def_Id, Base_Type (T));
14070
14071 if Constraint_OK then
14072 Set_First_Index (Def_Id, First (Constraints (C)));
14073 else
14074 Set_First_Index (Def_Id, First_Index (T));
14075 end if;
14076
14077 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
14078 Set_Is_Fixed_Lower_Bound_Array_Subtype
14079 (Def_Id, Is_FLB_Array_Subtype);
14080 Set_Is_Aliased (Def_Id, Is_Aliased (T));
14081 Set_Is_Independent (Def_Id, Is_Independent (T));
14082 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14083
14084 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
14085 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
14086
14087 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
14088 -- We need to initialize the attribute because if Def_Id is previously
14089 -- analyzed through a limited_with clause, it will have the attributes
14090 -- of an incomplete type, one of which is an Elist that overlaps the
14091 -- Packed_Array_Impl_Type field.
14092
14093 Set_Packed_Array_Impl_Type (Def_Id, Empty);
14094
14095 -- Build a freeze node if parent still needs one. Also make sure that
14096 -- the Depends_On_Private status is set because the subtype will need
14097 -- reprocessing at the time the base type does, and also we must set a
14098 -- conditional delay.
14099
14100 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
14101 Conditional_Delay (Def_Id, T);
14102 end Constrain_Array;
14103
14104 ------------------------------
14105 -- Constrain_Component_Type --
14106 ------------------------------
14107
14108 function Constrain_Component_Type
14109 (Comp : Entity_Id;
14110 Constrained_Typ : Entity_Id;
14111 Related_Node : Node_Id;
14112 Typ : Entity_Id;
14113 Constraints : Elist_Id) return Entity_Id
14114 is
14115 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
14116 Compon_Type : constant Entity_Id := Etype (Comp);
14117
14118 function Build_Constrained_Array_Type
14119 (Old_Type : Entity_Id) return Entity_Id;
14120 -- If Old_Type is an array type, one of whose indexes is constrained
14121 -- by a discriminant, build an Itype whose constraint replaces the
14122 -- discriminant with its value in the constraint.
14123
14124 function Build_Constrained_Discriminated_Type
14125 (Old_Type : Entity_Id) return Entity_Id;
14126 -- Ditto for record components. Handle the case where the constraint
14127 -- is a conversion of the discriminant value, introduced during
14128 -- expansion.
14129
14130 function Build_Constrained_Access_Type
14131 (Old_Type : Entity_Id) return Entity_Id;
14132 -- Ditto for access types. Makes use of previous two functions, to
14133 -- constrain designated type.
14134
14135 function Is_Discriminant (Expr : Node_Id) return Boolean;
14136 -- Returns True if Expr is a discriminant
14137
14138 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
14139 -- Find the value of a discriminant named by Discr_Expr in Constraints
14140
14141 -----------------------------------
14142 -- Build_Constrained_Access_Type --
14143 -----------------------------------
14144
14145 function Build_Constrained_Access_Type
14146 (Old_Type : Entity_Id) return Entity_Id
14147 is
14148 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
14149 Itype : Entity_Id;
14150 Desig_Subtype : Entity_Id;
14151 Scop : Entity_Id;
14152
14153 begin
14154 -- If the original access type was not embedded in the enclosing
14155 -- type definition, there is no need to produce a new access
14156 -- subtype. In fact every access type with an explicit constraint
14157 -- generates an itype whose scope is the enclosing record.
14158
14159 if not Is_Type (Scope (Old_Type)) then
14160 return Old_Type;
14161
14162 elsif Is_Array_Type (Desig_Type) then
14163 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
14164
14165 elsif Has_Discriminants (Desig_Type) then
14166
14167 -- This may be an access type to an enclosing record type for
14168 -- which we are constructing the constrained components. Return
14169 -- the enclosing record subtype. This is not always correct,
14170 -- but avoids infinite recursion. ???
14171
14172 Desig_Subtype := Any_Type;
14173
14174 for J in reverse 0 .. Scope_Stack.Last loop
14175 Scop := Scope_Stack.Table (J).Entity;
14176
14177 if Is_Type (Scop)
14178 and then Base_Type (Scop) = Base_Type (Desig_Type)
14179 then
14180 Desig_Subtype := Scop;
14181 end if;
14182
14183 exit when not Is_Type (Scop);
14184 end loop;
14185
14186 if Desig_Subtype = Any_Type then
14187 Desig_Subtype :=
14188 Build_Constrained_Discriminated_Type (Desig_Type);
14189 end if;
14190
14191 else
14192 return Old_Type;
14193 end if;
14194
14195 if Desig_Subtype /= Desig_Type then
14196
14197 -- The Related_Node better be here or else we won't be able
14198 -- to attach new itypes to a node in the tree.
14199
14200 pragma Assert (Present (Related_Node));
14201
14202 Itype := Create_Itype (E_Access_Subtype, Related_Node);
14203
14204 Set_Etype (Itype, Base_Type (Old_Type));
14205 Set_Size_Info (Itype, (Old_Type));
14206 Set_Directly_Designated_Type (Itype, Desig_Subtype);
14207 Set_Depends_On_Private (Itype, Has_Private_Component
14208 (Old_Type));
14209 Set_Is_Access_Constant (Itype, Is_Access_Constant
14210 (Old_Type));
14211
14212 -- The new itype needs freezing when it depends on a not frozen
14213 -- type and the enclosing subtype needs freezing.
14214
14215 if Has_Delayed_Freeze (Constrained_Typ)
14216 and then not Is_Frozen (Constrained_Typ)
14217 then
14218 Conditional_Delay (Itype, Base_Type (Old_Type));
14219 end if;
14220
14221 return Itype;
14222
14223 else
14224 return Old_Type;
14225 end if;
14226 end Build_Constrained_Access_Type;
14227
14228 ----------------------------------
14229 -- Build_Constrained_Array_Type --
14230 ----------------------------------
14231
14232 function Build_Constrained_Array_Type
14233 (Old_Type : Entity_Id) return Entity_Id
14234 is
14235 Lo_Expr : Node_Id;
14236 Hi_Expr : Node_Id;
14237 Old_Index : Node_Id;
14238 Range_Node : Node_Id;
14239 Constr_List : List_Id;
14240
14241 Need_To_Create_Itype : Boolean := False;
14242
14243 begin
14244 Old_Index := First_Index (Old_Type);
14245 while Present (Old_Index) loop
14246 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14247
14248 if Is_Discriminant (Lo_Expr)
14249 or else
14250 Is_Discriminant (Hi_Expr)
14251 then
14252 Need_To_Create_Itype := True;
14253 exit;
14254 end if;
14255
14256 Next_Index (Old_Index);
14257 end loop;
14258
14259 if Need_To_Create_Itype then
14260 Constr_List := New_List;
14261
14262 Old_Index := First_Index (Old_Type);
14263 while Present (Old_Index) loop
14264 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14265
14266 if Is_Discriminant (Lo_Expr) then
14267 Lo_Expr := Get_Discr_Value (Lo_Expr);
14268 end if;
14269
14270 if Is_Discriminant (Hi_Expr) then
14271 Hi_Expr := Get_Discr_Value (Hi_Expr);
14272 end if;
14273
14274 Range_Node :=
14275 Make_Range
14276 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14277
14278 Append (Range_Node, To => Constr_List);
14279
14280 Next_Index (Old_Index);
14281 end loop;
14282
14283 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14284
14285 else
14286 return Old_Type;
14287 end if;
14288 end Build_Constrained_Array_Type;
14289
14290 ------------------------------------------
14291 -- Build_Constrained_Discriminated_Type --
14292 ------------------------------------------
14293
14294 function Build_Constrained_Discriminated_Type
14295 (Old_Type : Entity_Id) return Entity_Id
14296 is
14297 Expr : Node_Id;
14298 Constr_List : List_Id;
14299 Old_Constraint : Elmt_Id;
14300
14301 Need_To_Create_Itype : Boolean := False;
14302
14303 begin
14304 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14305 while Present (Old_Constraint) loop
14306 Expr := Node (Old_Constraint);
14307
14308 if Is_Discriminant (Expr) then
14309 Need_To_Create_Itype := True;
14310 exit;
14311
14312 -- After expansion of discriminated task types, the value
14313 -- of the discriminant may be converted to a run-time type
14314 -- for restricted run-times. Propagate the value of the
14315 -- discriminant as well, so that e.g. the secondary stack
14316 -- component has a static constraint. Necessary for LLVM.
14317
14318 elsif Nkind (Expr) = N_Type_Conversion
14319 and then Is_Discriminant (Expression (Expr))
14320 then
14321 Need_To_Create_Itype := True;
14322 exit;
14323 end if;
14324
14325 Next_Elmt (Old_Constraint);
14326 end loop;
14327
14328 if Need_To_Create_Itype then
14329 Constr_List := New_List;
14330
14331 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14332 while Present (Old_Constraint) loop
14333 Expr := Node (Old_Constraint);
14334
14335 if Is_Discriminant (Expr) then
14336 Expr := Get_Discr_Value (Expr);
14337
14338 elsif Nkind (Expr) = N_Type_Conversion
14339 and then Is_Discriminant (Expression (Expr))
14340 then
14341 Expr := New_Copy_Tree (Expr);
14342 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14343 end if;
14344
14345 Append (New_Copy_Tree (Expr), To => Constr_List);
14346
14347 Next_Elmt (Old_Constraint);
14348 end loop;
14349
14350 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14351
14352 else
14353 return Old_Type;
14354 end if;
14355 end Build_Constrained_Discriminated_Type;
14356
14357 ---------------------
14358 -- Get_Discr_Value --
14359 ---------------------
14360
14361 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14362 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14363 -- Entity of a discriminant that appear as a standalone expression in
14364 -- the constraint of a component.
14365
14366 D : Entity_Id;
14367 E : Elmt_Id;
14368
14369 begin
14370 -- The discriminant may be declared for the type, in which case we
14371 -- find it by iterating over the list of discriminants. If the
14372 -- discriminant is inherited from a parent type, it appears as the
14373 -- corresponding discriminant of the current type. This will be the
14374 -- case when constraining an inherited component whose constraint is
14375 -- given by a discriminant of the parent.
14376
14377 D := First_Discriminant (Typ);
14378 E := First_Elmt (Constraints);
14379
14380 while Present (D) loop
14381 if D = Discr_Id
14382 or else D = CR_Discriminant (Discr_Id)
14383 or else Corresponding_Discriminant (D) = Discr_Id
14384 then
14385 return New_Copy_Tree (Node (E));
14386 end if;
14387
14388 Next_Discriminant (D);
14389 Next_Elmt (E);
14390 end loop;
14391
14392 -- The Corresponding_Discriminant mechanism is incomplete, because
14393 -- the correspondence between new and old discriminants is not one
14394 -- to one: one new discriminant can constrain several old ones. In
14395 -- that case, scan sequentially the stored_constraint, the list of
14396 -- discriminants of the parents, and the constraints.
14397
14398 -- Previous code checked for the present of the Stored_Constraint
14399 -- list for the derived type, but did not use it at all. Should it
14400 -- be present when the component is a discriminated task type?
14401
14402 if Is_Derived_Type (Typ)
14403 and then Scope (Discr_Id) = Etype (Typ)
14404 then
14405 D := First_Discriminant (Etype (Typ));
14406 E := First_Elmt (Constraints);
14407 while Present (D) loop
14408 if D = Discr_Id then
14409 return New_Copy_Tree (Node (E));
14410 end if;
14411
14412 Next_Discriminant (D);
14413 Next_Elmt (E);
14414 end loop;
14415 end if;
14416
14417 -- Something is wrong if we did not find the value
14418
14419 raise Program_Error;
14420 end Get_Discr_Value;
14421
14422 ---------------------
14423 -- Is_Discriminant --
14424 ---------------------
14425
14426 function Is_Discriminant (Expr : Node_Id) return Boolean is
14427 Discrim_Scope : Entity_Id;
14428
14429 begin
14430 if Denotes_Discriminant (Expr) then
14431 Discrim_Scope := Scope (Entity (Expr));
14432
14433 -- Either we have a reference to one of Typ's discriminants,
14434
14435 pragma Assert (Discrim_Scope = Typ
14436
14437 -- or to the discriminants of the parent type, in the case
14438 -- of a derivation of a tagged type with variants.
14439
14440 or else Discrim_Scope = Etype (Typ)
14441 or else Full_View (Discrim_Scope) = Etype (Typ)
14442
14443 -- or same as above for the case where the discriminants
14444 -- were declared in Typ's private view.
14445
14446 or else (Is_Private_Type (Discrim_Scope)
14447 and then Chars (Discrim_Scope) = Chars (Typ))
14448
14449 -- or else we are deriving from the full view and the
14450 -- discriminant is declared in the private entity.
14451
14452 or else (Is_Private_Type (Typ)
14453 and then Chars (Discrim_Scope) = Chars (Typ))
14454
14455 -- Or we are constrained the corresponding record of a
14456 -- synchronized type that completes a private declaration.
14457
14458 or else (Is_Concurrent_Record_Type (Typ)
14459 and then
14460 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14461
14462 -- or we have a class-wide type, in which case make sure the
14463 -- discriminant found belongs to the root type.
14464
14465 or else (Is_Class_Wide_Type (Typ)
14466 and then Etype (Typ) = Discrim_Scope));
14467
14468 return True;
14469 end if;
14470
14471 -- In all other cases we have something wrong
14472
14473 return False;
14474 end Is_Discriminant;
14475
14476 -- Start of processing for Constrain_Component_Type
14477
14478 begin
14479 if Nkind (Parent (Comp)) = N_Component_Declaration
14480 and then Comes_From_Source (Parent (Comp))
14481 and then Comes_From_Source
14482 (Subtype_Indication (Component_Definition (Parent (Comp))))
14483 and then
14484 Is_Entity_Name
14485 (Subtype_Indication (Component_Definition (Parent (Comp))))
14486 then
14487 return Compon_Type;
14488
14489 elsif Is_Array_Type (Compon_Type) then
14490 return Build_Constrained_Array_Type (Compon_Type);
14491
14492 elsif Has_Discriminants (Compon_Type) then
14493 return Build_Constrained_Discriminated_Type (Compon_Type);
14494
14495 elsif Is_Access_Type (Compon_Type) then
14496 return Build_Constrained_Access_Type (Compon_Type);
14497
14498 else
14499 return Compon_Type;
14500 end if;
14501 end Constrain_Component_Type;
14502
14503 --------------------------
14504 -- Constrain_Concurrent --
14505 --------------------------
14506
14507 -- For concurrent types, the associated record value type carries the same
14508 -- discriminants, so when we constrain a concurrent type, we must constrain
14509 -- the corresponding record type as well.
14510
14511 procedure Constrain_Concurrent
14512 (Def_Id : in out Entity_Id;
14513 SI : Node_Id;
14514 Related_Nod : Node_Id;
14515 Related_Id : Entity_Id;
14516 Suffix : Character)
14517 is
14518 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14519 -- case of a private subtype (needed when only doing semantic analysis).
14520
14521 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14522 T_Val : Entity_Id;
14523
14524 begin
14525 if Is_Access_Type (T_Ent) then
14526 T_Ent := Designated_Type (T_Ent);
14527 end if;
14528
14529 T_Val := Corresponding_Record_Type (T_Ent);
14530
14531 if Present (T_Val) then
14532
14533 if No (Def_Id) then
14534 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14535
14536 -- Elaborate itype now, as it may be used in a subsequent
14537 -- synchronized operation in another scope.
14538
14539 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14540 Build_Itype_Reference (Def_Id, Related_Nod);
14541 end if;
14542 end if;
14543
14544 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14545 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14546
14547 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14548 Set_Corresponding_Record_Type (Def_Id,
14549 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14550
14551 else
14552 -- If there is no associated record, expansion is disabled and this
14553 -- is a generic context. Create a subtype in any case, so that
14554 -- semantic analysis can proceed.
14555
14556 if No (Def_Id) then
14557 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14558 end if;
14559
14560 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14561 end if;
14562 end Constrain_Concurrent;
14563
14564 ------------------------------------
14565 -- Constrain_Corresponding_Record --
14566 ------------------------------------
14567
14568 function Constrain_Corresponding_Record
14569 (Prot_Subt : Entity_Id;
14570 Corr_Rec : Entity_Id;
14571 Related_Nod : Node_Id) return Entity_Id
14572 is
14573 T_Sub : constant Entity_Id :=
14574 Create_Itype
14575 (Ekind => E_Record_Subtype,
14576 Related_Nod => Related_Nod,
14577 Related_Id => Corr_Rec,
14578 Suffix => 'C',
14579 Suffix_Index => -1);
14580
14581 begin
14582 Set_Etype (T_Sub, Corr_Rec);
14583 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14584 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14585 Set_Class_Wide_Type (T_Sub, Class_Wide_Type (Corr_Rec));
14586 Set_Is_Constrained (T_Sub, True);
14587 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14588 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14589 Set_Direct_Primitive_Operations
14590 (T_Sub, Direct_Primitive_Operations (Corr_Rec));
14591
14592 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14593 Set_Discriminant_Constraint
14594 (T_Sub, Discriminant_Constraint (Prot_Subt));
14595 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14596 Create_Constrained_Components
14597 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14598 end if;
14599
14600 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14601
14602 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14603 Conditional_Delay (T_Sub, Corr_Rec);
14604
14605 else
14606 -- This is a component subtype: it will be frozen in the context of
14607 -- the enclosing record's init_proc, so that discriminant references
14608 -- are resolved to discriminals. (Note: we used to skip freezing
14609 -- altogether in that case, which caused errors downstream for
14610 -- components of a bit packed array type).
14611
14612 Set_Has_Delayed_Freeze (T_Sub);
14613 end if;
14614
14615 return T_Sub;
14616 end Constrain_Corresponding_Record;
14617
14618 -----------------------
14619 -- Constrain_Decimal --
14620 -----------------------
14621
14622 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14623 T : constant Entity_Id := Entity (Subtype_Mark (S));
14624 C : constant Node_Id := Constraint (S);
14625 Loc : constant Source_Ptr := Sloc (C);
14626 Range_Expr : Node_Id;
14627 Digits_Expr : Node_Id;
14628 Digits_Val : Uint;
14629 Bound_Val : Ureal;
14630
14631 begin
14632 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14633
14634 if Nkind (C) = N_Range_Constraint then
14635 Range_Expr := Range_Expression (C);
14636 Digits_Val := Digits_Value (T);
14637
14638 else
14639 pragma Assert (Nkind (C) = N_Digits_Constraint);
14640
14641 Digits_Expr := Digits_Expression (C);
14642 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14643
14644 Check_Digits_Expression (Digits_Expr);
14645 Digits_Val := Expr_Value (Digits_Expr);
14646
14647 if Digits_Val > Digits_Value (T) then
14648 Error_Msg_N
14649 ("digits expression is incompatible with subtype", C);
14650 Digits_Val := Digits_Value (T);
14651 end if;
14652
14653 if Present (Range_Constraint (C)) then
14654 Range_Expr := Range_Expression (Range_Constraint (C));
14655 else
14656 Range_Expr := Empty;
14657 end if;
14658 end if;
14659
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_Delta_Value (Def_Id, Delta_Value (T));
14664 Set_Scale_Value (Def_Id, Scale_Value (T));
14665 Set_Small_Value (Def_Id, Small_Value (T));
14666 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14667 Set_Digits_Value (Def_Id, Digits_Val);
14668
14669 -- Manufacture range from given digits value if no range present
14670
14671 if No (Range_Expr) then
14672 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14673 Range_Expr :=
14674 Make_Range (Loc,
14675 Low_Bound =>
14676 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14677 High_Bound =>
14678 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14679 end if;
14680
14681 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14682 Set_Discrete_RM_Size (Def_Id);
14683
14684 -- Unconditionally delay the freeze, since we cannot set size
14685 -- information in all cases correctly until the freeze point.
14686
14687 Set_Has_Delayed_Freeze (Def_Id);
14688 end Constrain_Decimal;
14689
14690 ----------------------------------
14691 -- Constrain_Discriminated_Type --
14692 ----------------------------------
14693
14694 procedure Constrain_Discriminated_Type
14695 (Def_Id : Entity_Id;
14696 S : Node_Id;
14697 Related_Nod : Node_Id;
14698 For_Access : Boolean := False)
14699 is
14700 E : Entity_Id := Entity (Subtype_Mark (S));
14701 T : Entity_Id;
14702
14703 procedure Fixup_Bad_Constraint;
14704 -- Called after finding a bad constraint, and after having posted an
14705 -- appropriate error message. The goal is to leave type Def_Id in as
14706 -- reasonable state as possible.
14707
14708 --------------------------
14709 -- Fixup_Bad_Constraint --
14710 --------------------------
14711
14712 procedure Fixup_Bad_Constraint is
14713 begin
14714 -- Set a reasonable Ekind for the entity, including incomplete types.
14715
14716 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14717
14718 -- Set Etype to the known type, to reduce chances of cascaded errors
14719
14720 Set_Etype (Def_Id, E);
14721 Set_Error_Posted (Def_Id);
14722 end Fixup_Bad_Constraint;
14723
14724 -- Local variables
14725
14726 C : Node_Id;
14727 Constr : Elist_Id := New_Elmt_List;
14728
14729 -- Start of processing for Constrain_Discriminated_Type
14730
14731 begin
14732 C := Constraint (S);
14733
14734 -- A discriminant constraint is only allowed in a subtype indication,
14735 -- after a subtype mark. This subtype mark must denote either a type
14736 -- with discriminants, or an access type whose designated type is a
14737 -- type with discriminants. A discriminant constraint specifies the
14738 -- values of these discriminants (RM 3.7.2(5)).
14739
14740 T := Base_Type (Entity (Subtype_Mark (S)));
14741
14742 if Is_Access_Type (T) then
14743 T := Designated_Type (T);
14744 end if;
14745
14746 -- In an instance it may be necessary to retrieve the full view of a
14747 -- type with unknown discriminants, or a full view with defaulted
14748 -- discriminants. In other contexts the constraint is illegal.
14749 -- This relaxation of legality checking may also be needed in
14750 -- compiler-generated Put_Image or streaming subprograms (hence
14751 -- the Comes_From_Source test).
14752
14753 if (In_Instance or not Comes_From_Source (S))
14754 and then Is_Private_Type (T)
14755 and then Present (Full_View (T))
14756 and then
14757 (Has_Unknown_Discriminants (T)
14758 or else
14759 (not Has_Discriminants (T)
14760 and then Has_Defaulted_Discriminants (Full_View (T))))
14761 then
14762 T := Full_View (T);
14763 E := Full_View (E);
14764 end if;
14765
14766 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14767 -- generating an error for access-to-incomplete subtypes.
14768
14769 if Ada_Version >= Ada_2005
14770 and then Ekind (T) = E_Incomplete_Type
14771 and then Nkind (Parent (S)) = N_Subtype_Declaration
14772 and then not Is_Itype (Def_Id)
14773 then
14774 -- A little sanity check: emit an error message if the type has
14775 -- discriminants to begin with. Type T may be a regular incomplete
14776 -- type or imported via a limited with clause.
14777
14778 if Has_Discriminants (T)
14779 or else (From_Limited_With (T)
14780 and then Present (Non_Limited_View (T))
14781 and then Nkind (Parent (Non_Limited_View (T))) =
14782 N_Full_Type_Declaration
14783 and then Present (Discriminant_Specifications
14784 (Parent (Non_Limited_View (T)))))
14785 then
14786 Error_Msg_N
14787 ("(Ada 2005) incomplete subtype may not be constrained", C);
14788 else
14789 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14790 end if;
14791
14792 Fixup_Bad_Constraint;
14793 return;
14794
14795 -- Check that the type has visible discriminants. The type may be
14796 -- a private type with unknown discriminants whose full view has
14797 -- discriminants which are invisible.
14798
14799 elsif not Has_Discriminants (T)
14800 or else
14801 (Has_Unknown_Discriminants (T)
14802 and then Is_Private_Type (T))
14803 then
14804 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14805 Fixup_Bad_Constraint;
14806 return;
14807
14808 elsif Is_Constrained (E)
14809 or else (Ekind (E) = E_Class_Wide_Subtype
14810 and then Present (Discriminant_Constraint (E)))
14811 then
14812 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14813 Fixup_Bad_Constraint;
14814 return;
14815 end if;
14816
14817 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14818 -- applies to the base type.
14819
14820 T := Base_Type (T);
14821
14822 Constr := Build_Discriminant_Constraints (T, S);
14823
14824 -- If the list returned was empty we had an error in building the
14825 -- discriminant constraint. We have also already signalled an error
14826 -- in the incomplete type case
14827
14828 if Is_Empty_Elmt_List (Constr) then
14829 Fixup_Bad_Constraint;
14830 return;
14831 end if;
14832
14833 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14834 end Constrain_Discriminated_Type;
14835
14836 ---------------------------
14837 -- Constrain_Enumeration --
14838 ---------------------------
14839
14840 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14841 T : constant Entity_Id := Entity (Subtype_Mark (S));
14842 C : constant Node_Id := Constraint (S);
14843
14844 begin
14845 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14846
14847 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14848 Set_Etype (Def_Id, Base_Type (T));
14849 Set_Size_Info (Def_Id, (T));
14850 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14851 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14852
14853 -- Inherit the chain of representation items instead of replacing it
14854 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14855 -- the derived type as a subtype declaration and the former needs to
14856 -- preserve existing representation items (see Build_Derived_Type).
14857
14858 Inherit_Rep_Item_Chain (Def_Id, T);
14859
14860 Set_Discrete_RM_Size (Def_Id);
14861 end Constrain_Enumeration;
14862
14863 ----------------------
14864 -- Constrain_Float --
14865 ----------------------
14866
14867 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14868 T : constant Entity_Id := Entity (Subtype_Mark (S));
14869 C : Node_Id;
14870 D : Node_Id;
14871 Rais : Node_Id;
14872
14873 begin
14874 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14875
14876 Set_Etype (Def_Id, Base_Type (T));
14877 Set_Size_Info (Def_Id, (T));
14878 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14879
14880 -- Process the constraint
14881
14882 C := Constraint (S);
14883
14884 -- Digits constraint present
14885
14886 if Nkind (C) = N_Digits_Constraint then
14887 Check_Restriction (No_Obsolescent_Features, C);
14888
14889 if Warn_On_Obsolescent_Feature then
14890 Error_Msg_N
14891 ("subtype digits constraint is an " &
14892 "obsolescent feature (RM J.3(8))?j?", C);
14893 end if;
14894
14895 D := Digits_Expression (C);
14896 Analyze_And_Resolve (D, Any_Integer);
14897 Check_Digits_Expression (D);
14898 Set_Digits_Value (Def_Id, Expr_Value (D));
14899
14900 -- Check that digits value is in range. Obviously we can do this
14901 -- at compile time, but it is strictly a runtime check, and of
14902 -- course there is an ACVC test that checks this.
14903
14904 if Digits_Value (Def_Id) > Digits_Value (T) then
14905 Error_Msg_Uint_1 := Digits_Value (T);
14906 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14907 Rais :=
14908 Make_Raise_Constraint_Error (Sloc (D),
14909 Reason => CE_Range_Check_Failed);
14910 Insert_Action (Declaration_Node (Def_Id), Rais);
14911 end if;
14912
14913 C := Range_Constraint (C);
14914
14915 -- No digits constraint present
14916
14917 else
14918 Set_Digits_Value (Def_Id, Digits_Value (T));
14919 end if;
14920
14921 -- Range constraint present
14922
14923 if Nkind (C) = N_Range_Constraint then
14924 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14925
14926 -- No range constraint present
14927
14928 else
14929 pragma Assert (No (C));
14930 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14931 end if;
14932
14933 Set_Is_Constrained (Def_Id);
14934 end Constrain_Float;
14935
14936 ---------------------
14937 -- Constrain_Index --
14938 ---------------------
14939
14940 procedure Constrain_Index
14941 (Index : Node_Id;
14942 S : Node_Id;
14943 Related_Nod : Node_Id;
14944 Related_Id : Entity_Id;
14945 Suffix : Character;
14946 Suffix_Index : Pos)
14947 is
14948 Def_Id : Entity_Id;
14949 R : Node_Id := Empty;
14950 T : constant Entity_Id := Etype (Index);
14951 Is_FLB_Index : Boolean := False;
14952
14953 begin
14954 Def_Id :=
14955 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14956 Set_Etype (Def_Id, Base_Type (T));
14957
14958 if Nkind (S) = N_Range
14959 or else
14960 (Nkind (S) = N_Attribute_Reference
14961 and then Attribute_Name (S) = Name_Range)
14962 then
14963 -- A Range attribute will be transformed into N_Range by Resolve
14964
14965 -- If a range has an Empty upper bound, then remember that for later
14966 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14967 -- flag, and also set the upper bound of the range to the index
14968 -- subtype's upper bound rather than leaving it Empty. In truth,
14969 -- that upper bound corresponds to a box ("<>"), but it's convenient
14970 -- to set it to the upper bound to avoid needing to add special tests
14971 -- in various places for an Empty upper bound, and in any case it
14972 -- accurately characterizes the index's range of values.
14973
14974 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14975 Is_FLB_Index := True;
14976 Set_High_Bound (S, Type_High_Bound (T));
14977 end if;
14978
14979 R := S;
14980
14981 Process_Range_Expr_In_Decl (R, T);
14982
14983 if not Error_Posted (S)
14984 and then
14985 (Nkind (S) /= N_Range
14986 or else not Covers (T, (Etype (Low_Bound (S))))
14987 or else not Covers (T, (Etype (High_Bound (S)))))
14988 then
14989 if Base_Type (T) /= Any_Type
14990 and then Etype (Low_Bound (S)) /= Any_Type
14991 and then Etype (High_Bound (S)) /= Any_Type
14992 then
14993 Error_Msg_N ("range expected", S);
14994 end if;
14995 end if;
14996
14997 elsif Nkind (S) = N_Subtype_Indication then
14998
14999 -- The parser has verified that this is a discrete indication
15000
15001 Resolve_Discrete_Subtype_Indication (S, T);
15002 Bad_Predicated_Subtype_Use
15003 ("subtype& has predicate, not allowed in index constraint",
15004 S, Entity (Subtype_Mark (S)));
15005
15006 R := Range_Expression (Constraint (S));
15007
15008 -- Capture values of bounds and generate temporaries for them if
15009 -- needed, since checks may cause duplication of the expressions
15010 -- which must not be reevaluated.
15011
15012 -- The forced evaluation removes side effects from expressions, which
15013 -- should occur also in GNATprove mode. Otherwise, we end up with
15014 -- unexpected insertions of actions at places where this is not
15015 -- supposed to occur, e.g. on default parameters of a call.
15016
15017 if Expander_Active or GNATprove_Mode then
15018 Force_Evaluation
15019 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
15020 Force_Evaluation
15021 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
15022 end if;
15023
15024 elsif Nkind (S) = N_Discriminant_Association then
15025
15026 -- Syntactically valid in subtype indication
15027
15028 Error_Msg_N ("invalid index constraint", S);
15029 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
15030 return;
15031
15032 -- Subtype_Mark case, no anonymous subtypes to construct
15033
15034 else
15035 Analyze (S);
15036
15037 if Is_Entity_Name (S) then
15038 if not Is_Type (Entity (S)) then
15039 Error_Msg_N ("expect subtype mark for index constraint", S);
15040
15041 elsif Base_Type (Entity (S)) /= Base_Type (T) then
15042 Wrong_Type (S, Base_Type (T));
15043
15044 -- Check error of subtype with predicate in index constraint
15045
15046 else
15047 Bad_Predicated_Subtype_Use
15048 ("subtype& has predicate, not allowed in index constraint",
15049 S, Entity (S));
15050 end if;
15051
15052 return;
15053
15054 else
15055 Error_Msg_N ("invalid index constraint", S);
15056 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
15057 return;
15058 end if;
15059 end if;
15060
15061 -- Complete construction of the Itype
15062
15063 if Is_Modular_Integer_Type (T) then
15064 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
15065
15066 elsif Is_Integer_Type (T) then
15067 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
15068
15069 else
15070 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
15071 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
15072 Set_First_Literal (Def_Id, First_Literal (T));
15073 end if;
15074
15075 Set_Size_Info (Def_Id, (T));
15076 Copy_RM_Size (To => Def_Id, From => T);
15077 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15078
15079 -- If this is a range for a fixed-lower-bound subtype, then set the
15080 -- index itype's low bound to the FLB and the index itype's upper bound
15081 -- to the high bound of the parent array type's index subtype. Also,
15082 -- set the Etype of the new scalar range and mark the itype as an FLB
15083 -- index subtype.
15084
15085 if Nkind (S) = N_Range and then Is_FLB_Index then
15086 Set_Scalar_Range
15087 (Def_Id,
15088 Make_Range (Sloc (S),
15089 Low_Bound => Low_Bound (S),
15090 High_Bound => Type_High_Bound (T)));
15091 Set_Etype (Scalar_Range (Def_Id), Etype (Index));
15092 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
15093
15094 else
15095 Set_Scalar_Range (Def_Id, R);
15096 end if;
15097
15098 Set_Etype (S, Def_Id);
15099 Set_Discrete_RM_Size (Def_Id);
15100 end Constrain_Index;
15101
15102 -----------------------
15103 -- Constrain_Integer --
15104 -----------------------
15105
15106 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
15107 T : constant Entity_Id := Entity (Subtype_Mark (S));
15108 C : constant Node_Id := Constraint (S);
15109
15110 begin
15111 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
15112
15113 if Is_Modular_Integer_Type (T) then
15114 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
15115 else
15116 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
15117 end if;
15118
15119 Set_Etype (Def_Id, Base_Type (T));
15120 Set_Size_Info (Def_Id, (T));
15121 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15122 Set_Discrete_RM_Size (Def_Id);
15123 end Constrain_Integer;
15124
15125 ------------------------------
15126 -- Constrain_Ordinary_Fixed --
15127 ------------------------------
15128
15129 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
15130 T : constant Entity_Id := Entity (Subtype_Mark (S));
15131 C : Node_Id;
15132 D : Node_Id;
15133 Rais : Node_Id;
15134
15135 begin
15136 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
15137 Set_Etype (Def_Id, Base_Type (T));
15138 Set_Size_Info (Def_Id, (T));
15139 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15140 Set_Small_Value (Def_Id, Small_Value (T));
15141
15142 -- Process the constraint
15143
15144 C := Constraint (S);
15145
15146 -- Delta constraint present
15147
15148 if Nkind (C) = N_Delta_Constraint then
15149 Check_Restriction (No_Obsolescent_Features, C);
15150
15151 if Warn_On_Obsolescent_Feature then
15152 Error_Msg_S
15153 ("subtype delta constraint is an " &
15154 "obsolescent feature (RM J.3(7))?j?");
15155 end if;
15156
15157 D := Delta_Expression (C);
15158 Analyze_And_Resolve (D, Any_Real);
15159 Check_Delta_Expression (D);
15160 Set_Delta_Value (Def_Id, Expr_Value_R (D));
15161
15162 -- Check that delta value is in range. Obviously we can do this
15163 -- at compile time, but it is strictly a runtime check, and of
15164 -- course there is an ACVC test that checks this.
15165
15166 if Delta_Value (Def_Id) < Delta_Value (T) then
15167 Error_Msg_N ("??delta value is too small", D);
15168 Rais :=
15169 Make_Raise_Constraint_Error (Sloc (D),
15170 Reason => CE_Range_Check_Failed);
15171 Insert_Action (Declaration_Node (Def_Id), Rais);
15172 end if;
15173
15174 C := Range_Constraint (C);
15175
15176 -- No delta constraint present
15177
15178 else
15179 Set_Delta_Value (Def_Id, Delta_Value (T));
15180 end if;
15181
15182 -- Range constraint present
15183
15184 if Nkind (C) = N_Range_Constraint then
15185 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
15186
15187 -- No range constraint present
15188
15189 else
15190 pragma Assert (No (C));
15191 Set_Scalar_Range (Def_Id, Scalar_Range (T));
15192 end if;
15193
15194 Set_Discrete_RM_Size (Def_Id);
15195
15196 -- Unconditionally delay the freeze, since we cannot set size
15197 -- information in all cases correctly until the freeze point.
15198
15199 Set_Has_Delayed_Freeze (Def_Id);
15200 end Constrain_Ordinary_Fixed;
15201
15202 -----------------------
15203 -- Contain_Interface --
15204 -----------------------
15205
15206 function Contain_Interface
15207 (Iface : Entity_Id;
15208 Ifaces : Elist_Id) return Boolean
15209 is
15210 Iface_Elmt : Elmt_Id;
15211
15212 begin
15213 if Present (Ifaces) then
15214 Iface_Elmt := First_Elmt (Ifaces);
15215 while Present (Iface_Elmt) loop
15216 if Node (Iface_Elmt) = Iface then
15217 return True;
15218 end if;
15219
15220 Next_Elmt (Iface_Elmt);
15221 end loop;
15222 end if;
15223
15224 return False;
15225 end Contain_Interface;
15226
15227 ---------------------------
15228 -- Convert_Scalar_Bounds --
15229 ---------------------------
15230
15231 procedure Convert_Scalar_Bounds
15232 (N : Node_Id;
15233 Parent_Type : Entity_Id;
15234 Derived_Type : Entity_Id;
15235 Loc : Source_Ptr)
15236 is
15237 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
15238
15239 Lo : Node_Id;
15240 Hi : Node_Id;
15241 Rng : Node_Id;
15242
15243 begin
15244 -- Defend against previous errors
15245
15246 if No (Scalar_Range (Derived_Type)) then
15247 Check_Error_Detected;
15248 return;
15249 end if;
15250
15251 Lo := Build_Scalar_Bound
15252 (Type_Low_Bound (Derived_Type),
15253 Parent_Type, Implicit_Base);
15254
15255 Hi := Build_Scalar_Bound
15256 (Type_High_Bound (Derived_Type),
15257 Parent_Type, Implicit_Base);
15258
15259 Rng :=
15260 Make_Range (Loc,
15261 Low_Bound => Lo,
15262 High_Bound => Hi);
15263
15264 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
15265
15266 Set_Parent (Rng, N);
15267 Set_Scalar_Range (Derived_Type, Rng);
15268
15269 -- Analyze the bounds
15270
15271 Analyze_And_Resolve (Lo, Implicit_Base);
15272 Analyze_And_Resolve (Hi, Implicit_Base);
15273
15274 -- Analyze the range itself, except that we do not analyze it if
15275 -- the bounds are real literals, and we have a fixed-point type.
15276 -- The reason for this is that we delay setting the bounds in this
15277 -- case till we know the final Small and Size values (see circuit
15278 -- in Freeze.Freeze_Fixed_Point_Type for further details).
15279
15280 if Is_Fixed_Point_Type (Parent_Type)
15281 and then Nkind (Lo) = N_Real_Literal
15282 and then Nkind (Hi) = N_Real_Literal
15283 then
15284 return;
15285
15286 -- Here we do the analysis of the range
15287
15288 -- Note: we do this manually, since if we do a normal Analyze and
15289 -- Resolve call, there are problems with the conversions used for
15290 -- the derived type range.
15291
15292 else
15293 Set_Etype (Rng, Implicit_Base);
15294 Set_Analyzed (Rng, True);
15295 end if;
15296 end Convert_Scalar_Bounds;
15297
15298 -------------------
15299 -- Copy_And_Swap --
15300 -------------------
15301
15302 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15303 begin
15304 -- Initialize new full declaration entity by copying the pertinent
15305 -- fields of the corresponding private declaration entity.
15306
15307 -- We temporarily set Ekind to a value appropriate for a type to
15308 -- avoid assert failures in Einfo from checking for setting type
15309 -- attributes on something that is not a type. Ekind (Priv) is an
15310 -- appropriate choice, since it allowed the attributes to be set
15311 -- in the first place. This Ekind value will be modified later.
15312
15313 Mutate_Ekind (Full, Ekind (Priv));
15314
15315 -- Also set Etype temporarily to Any_Type, again, in the absence
15316 -- of errors, it will be properly reset, and if there are errors,
15317 -- then we want a value of Any_Type to remain.
15318
15319 Set_Etype (Full, Any_Type);
15320
15321 -- Now start copying attributes
15322
15323 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15324
15325 if Has_Discriminants (Full) then
15326 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15327 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15328 end if;
15329
15330 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15331 Set_Homonym (Full, Homonym (Priv));
15332 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15333 Set_Is_Public (Full, Is_Public (Priv));
15334 Set_Is_Pure (Full, Is_Pure (Priv));
15335 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15336 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15337 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15338 Set_Has_Pragma_Unreferenced_Objects
15339 (Full, Has_Pragma_Unreferenced_Objects
15340 (Priv));
15341
15342 Conditional_Delay (Full, Priv);
15343
15344 if Is_Tagged_Type (Full) then
15345 Set_Direct_Primitive_Operations
15346 (Full, Direct_Primitive_Operations (Priv));
15347 Set_No_Tagged_Streams_Pragma
15348 (Full, No_Tagged_Streams_Pragma (Priv));
15349
15350 if Is_Base_Type (Priv) then
15351 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15352 end if;
15353 end if;
15354
15355 Set_Is_Volatile (Full, Is_Volatile (Priv));
15356 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15357 Set_Scope (Full, Scope (Priv));
15358 Set_Prev_Entity (Full, Prev_Entity (Priv));
15359 Set_Next_Entity (Full, Next_Entity (Priv));
15360 Set_First_Entity (Full, First_Entity (Priv));
15361 Set_Last_Entity (Full, Last_Entity (Priv));
15362
15363 -- If access types have been recorded for later handling, keep them in
15364 -- the full view so that they get handled when the full view freeze
15365 -- node is expanded.
15366
15367 if Present (Freeze_Node (Priv))
15368 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15369 then
15370 Ensure_Freeze_Node (Full);
15371 Set_Access_Types_To_Process
15372 (Freeze_Node (Full),
15373 Access_Types_To_Process (Freeze_Node (Priv)));
15374 end if;
15375
15376 -- Swap the two entities. Now Private is the full type entity and Full
15377 -- is the private one. They will be swapped back at the end of the
15378 -- private part. This swapping ensures that the entity that is visible
15379 -- in the private part is the full declaration.
15380
15381 Exchange_Entities (Priv, Full);
15382 Set_Is_Not_Self_Hidden (Priv);
15383 Append_Entity (Full, Scope (Full));
15384 end Copy_And_Swap;
15385
15386 -------------------------------------
15387 -- Copy_Array_Base_Type_Attributes --
15388 -------------------------------------
15389
15390 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15391 begin
15392 Set_Component_Alignment (T1, Component_Alignment (T2));
15393 Set_Component_Type (T1, Component_Type (T2));
15394 Set_Component_Size (T1, Component_Size (T2));
15395 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15396 Propagate_Concurrent_Flags (T1, T2);
15397 Propagate_Controlled_Flags (T1, T2);
15398 Set_Is_Packed (T1, Is_Packed (T2));
15399 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15400 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15401 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15402 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15403 end Copy_Array_Base_Type_Attributes;
15404
15405 -----------------------------------
15406 -- Copy_Array_Subtype_Attributes --
15407 -----------------------------------
15408
15409 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15410 -- let it be recreated during freezing for the sake of better debug info.
15411
15412 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15413 begin
15414 Set_Size_Info (T1, T2);
15415
15416 Set_First_Index (T1, First_Index (T2));
15417 Set_Is_Aliased (T1, Is_Aliased (T2));
15418 Set_Is_Atomic (T1, Is_Atomic (T2));
15419 Set_Is_Independent (T1, Is_Independent (T2));
15420 Set_Is_Volatile (T1, Is_Volatile (T2));
15421 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15422 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15423 Set_Is_Constrained (T1, Is_Constrained (T2));
15424 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15425 Inherit_Rep_Item_Chain (T1, T2);
15426 Set_Convention (T1, Convention (T2));
15427 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15428 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15429 end Copy_Array_Subtype_Attributes;
15430
15431 -----------------------------------
15432 -- Create_Constrained_Components --
15433 -----------------------------------
15434
15435 procedure Create_Constrained_Components
15436 (Subt : Entity_Id;
15437 Decl_Node : Node_Id;
15438 Typ : Entity_Id;
15439 Constraints : Elist_Id)
15440 is
15441 Loc : constant Source_Ptr := Sloc (Subt);
15442 Comp_List : constant Elist_Id := New_Elmt_List;
15443 Parent_Type : constant Entity_Id := Etype (Typ);
15444
15445 Assoc_List : List_Id;
15446 Discr_Val : Elmt_Id;
15447 Errors : Boolean;
15448 New_C : Entity_Id;
15449 Old_C : Entity_Id;
15450 Is_Static : Boolean := True;
15451 Is_Compile_Time_Known : Boolean := True;
15452
15453 procedure Collect_Fixed_Components (Typ : Entity_Id);
15454 -- Collect parent type components that do not appear in a variant part
15455
15456 procedure Create_All_Components;
15457 -- Iterate over Comp_List to create the components of the subtype
15458
15459 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15460 -- Creates a new component from Old_Compon, copying all the fields from
15461 -- it, including its Etype, inserts the new component in the Subt entity
15462 -- chain and returns the new component.
15463
15464 function Is_Variant_Record (T : Entity_Id) return Boolean;
15465 -- If true, and discriminants are static, collect only components from
15466 -- variants selected by discriminant values.
15467
15468 ------------------------------
15469 -- Collect_Fixed_Components --
15470 ------------------------------
15471
15472 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15473 begin
15474 -- Build association list for discriminants, and find components of
15475 -- the variant part selected by the values of the discriminants.
15476
15477 Assoc_List := New_List;
15478
15479 Old_C := First_Discriminant (Typ);
15480 Discr_Val := First_Elmt (Constraints);
15481 while Present (Old_C) loop
15482 Append_To (Assoc_List,
15483 Make_Component_Association (Loc,
15484 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15485 Expression => New_Copy (Node (Discr_Val))));
15486
15487 Next_Elmt (Discr_Val);
15488 Next_Discriminant (Old_C);
15489 end loop;
15490
15491 -- The tag and the possible parent component are unconditionally in
15492 -- the subtype.
15493
15494 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15495 Old_C := First_Component (Typ);
15496 while Present (Old_C) loop
15497 if Chars (Old_C) in Name_uTag | Name_uParent then
15498 Append_Elmt (Old_C, Comp_List);
15499 end if;
15500
15501 Next_Component (Old_C);
15502 end loop;
15503 end if;
15504 end Collect_Fixed_Components;
15505
15506 ---------------------------
15507 -- Create_All_Components --
15508 ---------------------------
15509
15510 procedure Create_All_Components is
15511 Comp : Elmt_Id;
15512
15513 begin
15514 Comp := First_Elmt (Comp_List);
15515 while Present (Comp) loop
15516 Old_C := Node (Comp);
15517 New_C := Create_Component (Old_C);
15518
15519 Set_Etype
15520 (New_C,
15521 Constrain_Component_Type
15522 (Old_C, Subt, Decl_Node, Typ, Constraints));
15523 Set_Is_Public (New_C, Is_Public (Subt));
15524
15525 Next_Elmt (Comp);
15526 end loop;
15527 end Create_All_Components;
15528
15529 ----------------------
15530 -- Create_Component --
15531 ----------------------
15532
15533 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15534 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15535
15536 begin
15537 if Ekind (Old_Compon) = E_Discriminant
15538 and then Is_Completely_Hidden (Old_Compon)
15539 then
15540 -- This is a shadow discriminant created for a discriminant of
15541 -- the parent type, which needs to be present in the subtype.
15542 -- Give the shadow discriminant an internal name that cannot
15543 -- conflict with that of visible components.
15544
15545 Set_Chars (New_Compon, New_Internal_Name ('C'));
15546 end if;
15547
15548 -- Set the parent so we have a proper link for freezing etc. This is
15549 -- not a real parent pointer, since of course our parent does not own
15550 -- up to us and reference us, we are an illegitimate child of the
15551 -- original parent.
15552
15553 Set_Parent (New_Compon, Parent (Old_Compon));
15554
15555 -- We do not want this node marked as Comes_From_Source, since
15556 -- otherwise it would get first class status and a separate cross-
15557 -- reference line would be generated. Illegitimate children do not
15558 -- rate such recognition.
15559
15560 Set_Comes_From_Source (New_Compon, False);
15561
15562 -- But it is a real entity, and a birth certificate must be properly
15563 -- registered by entering it into the entity list, and setting its
15564 -- scope to the given subtype. This turns out to be useful for the
15565 -- LLVM code generator, but that scope is not used otherwise.
15566
15567 Enter_Name (New_Compon);
15568 Set_Scope (New_Compon, Subt);
15569
15570 return New_Compon;
15571 end Create_Component;
15572
15573 -----------------------
15574 -- Is_Variant_Record --
15575 -----------------------
15576
15577 function Is_Variant_Record (T : Entity_Id) return Boolean is
15578 Decl : constant Node_Id := Parent (T);
15579 begin
15580 return Nkind (Decl) = N_Full_Type_Declaration
15581 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15582 and then Present (Component_List (Type_Definition (Decl)))
15583 and then
15584 Present (Variant_Part (Component_List (Type_Definition (Decl))));
15585 end Is_Variant_Record;
15586
15587 -- Start of processing for Create_Constrained_Components
15588
15589 begin
15590 pragma Assert (Subt /= Base_Type (Subt));
15591 pragma Assert (Typ = Base_Type (Typ));
15592
15593 Set_First_Entity (Subt, Empty);
15594 Set_Last_Entity (Subt, Empty);
15595
15596 -- Check whether constraint is fully static, in which case we can
15597 -- optimize the list of components.
15598
15599 Discr_Val := First_Elmt (Constraints);
15600 while Present (Discr_Val) loop
15601 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15602 Is_Static := False;
15603
15604 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15605 Is_Compile_Time_Known := False;
15606 exit;
15607 end if;
15608 end if;
15609
15610 Next_Elmt (Discr_Val);
15611 end loop;
15612
15613 Set_Has_Static_Discriminants (Subt, Is_Static);
15614
15615 Push_Scope (Subt);
15616
15617 -- Inherit the discriminants of the parent type
15618
15619 Add_Discriminants : declare
15620 Num_Disc : Nat;
15621 Num_Stor : Nat;
15622
15623 begin
15624 Num_Disc := 0;
15625 Old_C := First_Discriminant (Typ);
15626
15627 while Present (Old_C) loop
15628 Num_Disc := Num_Disc + 1;
15629 New_C := Create_Component (Old_C);
15630 Set_Is_Public (New_C, Is_Public (Subt));
15631 Next_Discriminant (Old_C);
15632 end loop;
15633
15634 -- For an untagged derived subtype, the number of discriminants may
15635 -- be smaller than the number of inherited discriminants, because
15636 -- several of them may be renamed by a single new discriminant or
15637 -- constrained. In this case, add the hidden discriminants back into
15638 -- the subtype, because they need to be present if the optimizer of
15639 -- the GCC 4.x back-end decides to break apart assignments between
15640 -- objects using the parent view into member-wise assignments.
15641
15642 Num_Stor := 0;
15643
15644 if Is_Derived_Type (Typ)
15645 and then not Is_Tagged_Type (Typ)
15646 then
15647 Old_C := First_Stored_Discriminant (Typ);
15648
15649 while Present (Old_C) loop
15650 Num_Stor := Num_Stor + 1;
15651 Next_Stored_Discriminant (Old_C);
15652 end loop;
15653 end if;
15654
15655 if Num_Stor > Num_Disc then
15656
15657 -- Find out multiple uses of new discriminants, and add hidden
15658 -- components for the extra renamed discriminants. We recognize
15659 -- multiple uses through the Corresponding_Discriminant of a
15660 -- new discriminant: if it constrains several old discriminants,
15661 -- this field points to the last one in the parent type. The
15662 -- stored discriminants of the derived type have the same name
15663 -- as those of the parent.
15664
15665 declare
15666 Constr : Elmt_Id;
15667 New_Discr : Entity_Id;
15668 Old_Discr : Entity_Id;
15669
15670 begin
15671 Constr := First_Elmt (Stored_Constraint (Typ));
15672 Old_Discr := First_Stored_Discriminant (Typ);
15673 while Present (Constr) loop
15674 if Is_Entity_Name (Node (Constr))
15675 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15676 then
15677 New_Discr := Entity (Node (Constr));
15678
15679 if Chars (Corresponding_Discriminant (New_Discr)) /=
15680 Chars (Old_Discr)
15681 then
15682 -- The new discriminant has been used to rename a
15683 -- subsequent old discriminant. Introduce a shadow
15684 -- component for the current old discriminant.
15685
15686 New_C := Create_Component (Old_Discr);
15687 Set_Original_Record_Component (New_C, Old_Discr);
15688 end if;
15689
15690 else
15691 -- The constraint has eliminated the old discriminant.
15692 -- Introduce a shadow component.
15693
15694 New_C := Create_Component (Old_Discr);
15695 Set_Original_Record_Component (New_C, Old_Discr);
15696 end if;
15697
15698 Next_Elmt (Constr);
15699 Next_Stored_Discriminant (Old_Discr);
15700 end loop;
15701 end;
15702 end if;
15703 end Add_Discriminants;
15704
15705 if Is_Compile_Time_Known
15706 and then Is_Variant_Record (Typ)
15707 then
15708 Collect_Fixed_Components (Typ);
15709 Gather_Components
15710 (Typ,
15711 Component_List (Type_Definition (Parent (Typ))),
15712 Governed_By => Assoc_List,
15713 Into => Comp_List,
15714 Report_Errors => Errors,
15715 Allow_Compile_Time => True);
15716 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15717
15718 Create_All_Components;
15719
15720 -- If the subtype declaration is created for a tagged type derivation
15721 -- with constraints, we retrieve the record definition of the parent
15722 -- type to select the components of the proper variant.
15723
15724 elsif Is_Compile_Time_Known
15725 and then Is_Tagged_Type (Typ)
15726 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15727 and then
15728 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15729 and then Is_Variant_Record (Parent_Type)
15730 then
15731 Collect_Fixed_Components (Typ);
15732 Gather_Components
15733 (Typ,
15734 Component_List (Type_Definition (Parent (Parent_Type))),
15735 Governed_By => Assoc_List,
15736 Into => Comp_List,
15737 Report_Errors => Errors,
15738 Allow_Compile_Time => True);
15739
15740 -- Note: previously there was a check at this point that no errors
15741 -- were detected. As a consequence of AI05-220 there may be an error
15742 -- if an inherited discriminant that controls a variant has a non-
15743 -- static constraint.
15744
15745 -- If the tagged derivation has a type extension, collect all the
15746 -- new relevant components therein via Gather_Components.
15747
15748 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15749 then
15750 Gather_Components
15751 (Typ,
15752 Component_List
15753 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15754 Governed_By => Assoc_List,
15755 Into => Comp_List,
15756 Report_Errors => Errors,
15757 Allow_Compile_Time => True,
15758 Include_Interface_Tag => True);
15759 end if;
15760
15761 Create_All_Components;
15762
15763 else
15764 -- If discriminants are not static, or if this is a multi-level type
15765 -- extension, we have to include all components of the parent type.
15766
15767 Old_C := First_Component (Typ);
15768 while Present (Old_C) loop
15769 New_C := Create_Component (Old_C);
15770
15771 Set_Etype
15772 (New_C,
15773 Constrain_Component_Type
15774 (Old_C, Subt, Decl_Node, Typ, Constraints));
15775 Set_Is_Public (New_C, Is_Public (Subt));
15776
15777 Next_Component (Old_C);
15778 end loop;
15779 end if;
15780
15781 End_Scope;
15782 end Create_Constrained_Components;
15783
15784 ------------------------------------------
15785 -- Decimal_Fixed_Point_Type_Declaration --
15786 ------------------------------------------
15787
15788 procedure Decimal_Fixed_Point_Type_Declaration
15789 (T : Entity_Id;
15790 Def : Node_Id)
15791 is
15792 Loc : constant Source_Ptr := Sloc (Def);
15793 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15794 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15795 Max_Digits : constant Nat :=
15796 (if System_Max_Integer_Size = 128 then 38 else 18);
15797 -- Maximum number of digits that can be represented in an integer
15798
15799 Implicit_Base : Entity_Id;
15800 Digs_Val : Uint;
15801 Delta_Val : Ureal;
15802 Scale_Val : Uint;
15803 Bound_Val : Ureal;
15804
15805 begin
15806 Check_Restriction (No_Fixed_Point, Def);
15807
15808 -- Create implicit base type
15809
15810 Implicit_Base :=
15811 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15812 Set_Etype (Implicit_Base, Implicit_Base);
15813
15814 -- Analyze and process delta expression
15815
15816 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15817
15818 Check_Delta_Expression (Delta_Expr);
15819 Delta_Val := Expr_Value_R (Delta_Expr);
15820
15821 -- Check delta is power of 10, and determine scale value from it
15822
15823 declare
15824 Val : Ureal;
15825
15826 begin
15827 Scale_Val := Uint_0;
15828 Val := Delta_Val;
15829
15830 if Val < Ureal_1 then
15831 while Val < Ureal_1 loop
15832 Val := Val * Ureal_10;
15833 Scale_Val := Scale_Val + 1;
15834 end loop;
15835
15836 if Scale_Val > Max_Digits then
15837 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15838 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15839 Scale_Val := UI_From_Int (Max_Digits);
15840 end if;
15841
15842 else
15843 while Val > Ureal_1 loop
15844 Val := Val / Ureal_10;
15845 Scale_Val := Scale_Val - 1;
15846 end loop;
15847
15848 if Scale_Val < -Max_Digits then
15849 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15850 Error_Msg_N ("scale is less than minimum value of ^", Def);
15851 Scale_Val := UI_From_Int (-Max_Digits);
15852 end if;
15853 end if;
15854
15855 if Val /= Ureal_1 then
15856 Error_Msg_N ("delta expression must be a power of 10", Def);
15857 Delta_Val := Ureal_10 ** (-Scale_Val);
15858 end if;
15859 end;
15860
15861 -- Set delta, scale and small (small = delta for decimal type)
15862
15863 Set_Delta_Value (Implicit_Base, Delta_Val);
15864 Set_Scale_Value (Implicit_Base, Scale_Val);
15865 Set_Small_Value (Implicit_Base, Delta_Val);
15866
15867 -- Analyze and process digits expression
15868
15869 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15870 Check_Digits_Expression (Digs_Expr);
15871 Digs_Val := Expr_Value (Digs_Expr);
15872
15873 if Digs_Val > Max_Digits then
15874 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15875 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15876 Digs_Val := UI_From_Int (Max_Digits);
15877 end if;
15878
15879 Set_Digits_Value (Implicit_Base, Digs_Val);
15880 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15881
15882 -- Set range of base type from digits value for now. This will be
15883 -- expanded to represent the true underlying base range by Freeze.
15884
15885 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15886
15887 -- Note: We leave Esize unset for now, size will be set at freeze
15888 -- time. We have to do this for ordinary fixed-point, because the size
15889 -- depends on the specified small, and we might as well do the same for
15890 -- decimal fixed-point.
15891
15892 pragma Assert (not Known_Esize (Implicit_Base));
15893
15894 -- If there are bounds given in the declaration use them as the
15895 -- bounds of the first named subtype.
15896
15897 if Present (Real_Range_Specification (Def)) then
15898 declare
15899 RRS : constant Node_Id := Real_Range_Specification (Def);
15900 Low : constant Node_Id := Low_Bound (RRS);
15901 High : constant Node_Id := High_Bound (RRS);
15902 Low_Val : Ureal;
15903 High_Val : Ureal;
15904
15905 begin
15906 Analyze_And_Resolve (Low, Any_Real);
15907 Analyze_And_Resolve (High, Any_Real);
15908 Check_Real_Bound (Low);
15909 Check_Real_Bound (High);
15910 Low_Val := Expr_Value_R (Low);
15911 High_Val := Expr_Value_R (High);
15912
15913 if Low_Val < (-Bound_Val) then
15914 Error_Msg_N
15915 ("range low bound too small for digits value", Low);
15916 Low_Val := -Bound_Val;
15917 end if;
15918
15919 if High_Val > Bound_Val then
15920 Error_Msg_N
15921 ("range high bound too large for digits value", High);
15922 High_Val := Bound_Val;
15923 end if;
15924
15925 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15926 end;
15927
15928 -- If no explicit range, use range that corresponds to given
15929 -- digits value. This will end up as the final range for the
15930 -- first subtype.
15931
15932 else
15933 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15934 end if;
15935
15936 -- Complete entity for first subtype. The inheritance of the rep item
15937 -- chain ensures that SPARK-related pragmas are not clobbered when the
15938 -- decimal fixed point type acts as a full view of a private type.
15939
15940 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15941 Set_Etype (T, Implicit_Base);
15942 Set_Size_Info (T, Implicit_Base);
15943 Inherit_Rep_Item_Chain (T, Implicit_Base);
15944 Set_Digits_Value (T, Digs_Val);
15945 Set_Delta_Value (T, Delta_Val);
15946 Set_Small_Value (T, Delta_Val);
15947 Set_Scale_Value (T, Scale_Val);
15948 Set_Is_Constrained (T);
15949 end Decimal_Fixed_Point_Type_Declaration;
15950
15951 -----------------------------------
15952 -- Derive_Progenitor_Subprograms --
15953 -----------------------------------
15954
15955 procedure Derive_Progenitor_Subprograms
15956 (Parent_Type : Entity_Id;
15957 Tagged_Type : Entity_Id)
15958 is
15959 E : Entity_Id;
15960 Elmt : Elmt_Id;
15961 Iface : Entity_Id;
15962 Iface_Alias : Entity_Id;
15963 Iface_Elmt : Elmt_Id;
15964 Iface_Subp : Entity_Id;
15965 New_Subp : Entity_Id := Empty;
15966 Prim_Elmt : Elmt_Id;
15967 Subp : Entity_Id;
15968 Typ : Entity_Id;
15969
15970 begin
15971 pragma Assert (Ada_Version >= Ada_2005
15972 and then Is_Record_Type (Tagged_Type)
15973 and then Is_Tagged_Type (Tagged_Type)
15974 and then Has_Interfaces (Tagged_Type));
15975
15976 -- Step 1: Transfer to the full-view primitives associated with the
15977 -- partial-view that cover interface primitives. Conceptually this
15978 -- work should be done later by Process_Full_View; done here to
15979 -- simplify its implementation at later stages. It can be safely
15980 -- done here because interfaces must be visible in the partial and
15981 -- private view (RM 7.3(7.3/2)).
15982
15983 -- Small optimization: This work is only required if the parent may
15984 -- have entities whose Alias attribute reference an interface primitive.
15985 -- Such a situation may occur if the parent is an abstract type and the
15986 -- primitive has not been yet overridden or if the parent is a generic
15987 -- formal type covering interfaces.
15988
15989 -- If the tagged type is not abstract, it cannot have abstract
15990 -- primitives (the only entities in the list of primitives of
15991 -- non-abstract tagged types that can reference abstract primitives
15992 -- through its Alias attribute are the internal entities that have
15993 -- attribute Interface_Alias, and these entities are generated later
15994 -- by Add_Internal_Interface_Entities).
15995
15996 if In_Private_Part (Current_Scope)
15997 and then (Is_Abstract_Type (Parent_Type)
15998 or else
15999 Is_Generic_Type (Parent_Type))
16000 then
16001 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
16002 while Present (Elmt) loop
16003 Subp := Node (Elmt);
16004
16005 -- At this stage it is not possible to have entities in the list
16006 -- of primitives that have attribute Interface_Alias.
16007
16008 pragma Assert (No (Interface_Alias (Subp)));
16009
16010 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
16011
16012 if Is_Interface (Typ) then
16013 E := Find_Primitive_Covering_Interface
16014 (Tagged_Type => Tagged_Type,
16015 Iface_Prim => Subp);
16016
16017 if Present (E)
16018 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
16019 then
16020 Replace_Elmt (Elmt, E);
16021 Remove_Homonym (Subp);
16022 end if;
16023 end if;
16024
16025 Next_Elmt (Elmt);
16026 end loop;
16027 end if;
16028
16029 -- Step 2: Add primitives of progenitors that are not implemented by
16030 -- parents of Tagged_Type.
16031
16032 if Present (Interfaces (Base_Type (Tagged_Type))) then
16033 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
16034 while Present (Iface_Elmt) loop
16035 Iface := Node (Iface_Elmt);
16036
16037 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
16038 while Present (Prim_Elmt) loop
16039 Iface_Subp := Node (Prim_Elmt);
16040 Iface_Alias := Ultimate_Alias (Iface_Subp);
16041
16042 -- Exclude derivation of predefined primitives except those
16043 -- that come from source, or are inherited from one that comes
16044 -- from source. Required to catch declarations of equality
16045 -- operators of interfaces. For example:
16046
16047 -- type Iface is interface;
16048 -- function "=" (Left, Right : Iface) return Boolean;
16049
16050 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
16051 or else Comes_From_Source (Iface_Alias)
16052 then
16053 E :=
16054 Find_Primitive_Covering_Interface
16055 (Tagged_Type => Tagged_Type,
16056 Iface_Prim => Iface_Subp);
16057
16058 -- If not found we derive a new primitive leaving its alias
16059 -- attribute referencing the interface primitive.
16060
16061 if No (E) then
16062 Derive_Subprogram
16063 (New_Subp, Iface_Subp, Tagged_Type, Iface);
16064
16065 -- Ada 2012 (AI05-0197): If the covering primitive's name
16066 -- differs from the name of the interface primitive then it
16067 -- is a private primitive inherited from a parent type. In
16068 -- such case, given that Tagged_Type covers the interface,
16069 -- the inherited private primitive becomes visible. For such
16070 -- purpose we add a new entity that renames the inherited
16071 -- private primitive.
16072
16073 elsif Chars (E) /= Chars (Iface_Subp) then
16074 pragma Assert (Has_Suffix (E, 'P'));
16075 Derive_Subprogram
16076 (New_Subp, Iface_Subp, Tagged_Type, Iface);
16077 Set_Alias (New_Subp, E);
16078 Set_Is_Abstract_Subprogram (New_Subp,
16079 Is_Abstract_Subprogram (E));
16080
16081 -- Propagate to the full view interface entities associated
16082 -- with the partial view.
16083
16084 elsif In_Private_Part (Current_Scope)
16085 and then Present (Alias (E))
16086 and then Alias (E) = Iface_Subp
16087 and then
16088 List_Containing (Parent (E)) /=
16089 Private_Declarations
16090 (Specification
16091 (Unit_Declaration_Node (Current_Scope)))
16092 then
16093 Append_Elmt (E, Primitive_Operations (Tagged_Type));
16094 end if;
16095 end if;
16096
16097 Next_Elmt (Prim_Elmt);
16098 end loop;
16099
16100 Next_Elmt (Iface_Elmt);
16101 end loop;
16102 end if;
16103 end Derive_Progenitor_Subprograms;
16104
16105 -----------------------
16106 -- Derive_Subprogram --
16107 -----------------------
16108
16109 procedure Derive_Subprogram
16110 (New_Subp : out Entity_Id;
16111 Parent_Subp : Entity_Id;
16112 Derived_Type : Entity_Id;
16113 Parent_Type : Entity_Id;
16114 Actual_Subp : Entity_Id := Empty)
16115 is
16116 Formal : Entity_Id;
16117 -- Formal parameter of parent primitive operation
16118
16119 Formal_Of_Actual : Entity_Id;
16120 -- Formal parameter of actual operation, when the derivation is to
16121 -- create a renaming for a primitive operation of an actual in an
16122 -- instantiation.
16123
16124 New_Formal : Entity_Id;
16125 -- Formal of inherited operation
16126
16127 Visible_Subp : Entity_Id := Parent_Subp;
16128
16129 function Is_Private_Overriding return Boolean;
16130 -- If Subp is a private overriding of a visible operation, the inherited
16131 -- operation derives from the overridden op (even though its body is the
16132 -- overriding one) and the inherited operation is visible now. See
16133 -- sem_disp to see the full details of the handling of the overridden
16134 -- subprogram, which is removed from the list of primitive operations of
16135 -- the type. The overridden subprogram is saved locally in Visible_Subp,
16136 -- and used to diagnose abstract operations that need overriding in the
16137 -- derived type.
16138
16139 procedure Replace_Type (Id, New_Id : Entity_Id);
16140 -- Set the Etype of New_Id to the appropriate subtype determined from
16141 -- the Etype of Id, following (RM 3.4 (18, 19, 20, 21)). Id is either
16142 -- the parent type's primitive subprogram or one of its formals, and
16143 -- New_Id is the corresponding entity for the derived type. When the
16144 -- Etype of Id is an anonymous access type, create a new access type
16145 -- designating the derived type.
16146
16147 procedure Set_Derived_Name;
16148 -- This procedure sets the appropriate Chars name for New_Subp. This
16149 -- is normally just a copy of the parent name. An exception arises for
16150 -- type support subprograms, where the name is changed to reflect the
16151 -- name of the derived type, e.g. if type foo is derived from type bar,
16152 -- then a procedure barDA is derived with a name fooDA.
16153
16154 ---------------------------
16155 -- Is_Private_Overriding --
16156 ---------------------------
16157
16158 function Is_Private_Overriding return Boolean is
16159 Prev : Entity_Id;
16160
16161 begin
16162 -- If the parent is not a dispatching operation there is no
16163 -- need to investigate overridings
16164
16165 if not Is_Dispatching_Operation (Parent_Subp) then
16166 return False;
16167 end if;
16168
16169 -- The visible operation that is overridden is a homonym of the
16170 -- parent subprogram. We scan the homonym chain to find the one
16171 -- whose alias is the subprogram we are deriving.
16172
16173 Prev := Current_Entity (Parent_Subp);
16174 while Present (Prev) loop
16175 if Ekind (Prev) = Ekind (Parent_Subp)
16176 and then Alias (Prev) = Parent_Subp
16177 and then Scope (Parent_Subp) = Scope (Prev)
16178 and then not Is_Hidden (Prev)
16179 then
16180 Visible_Subp := Prev;
16181 return True;
16182 end if;
16183
16184 Prev := Homonym (Prev);
16185 end loop;
16186
16187 return False;
16188 end Is_Private_Overriding;
16189
16190 ------------------
16191 -- Replace_Type --
16192 ------------------
16193
16194 procedure Replace_Type (Id, New_Id : Entity_Id) is
16195 Id_Type : constant Entity_Id := Etype (Id);
16196 Par : constant Node_Id := Parent (Derived_Type);
16197
16198 begin
16199 -- When the type is an anonymous access type, create a new access
16200 -- type designating the derived type. This itype must be elaborated
16201 -- at the point of the derivation, not on subsequent calls that may
16202 -- be out of the proper scope for Gigi, so we insert a reference to
16203 -- it after the derivation.
16204
16205 if Ekind (Id_Type) = E_Anonymous_Access_Type then
16206 declare
16207 Acc_Type : Entity_Id;
16208 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
16209
16210 begin
16211 if Ekind (Desig_Typ) = E_Record_Type_With_Private
16212 and then Present (Full_View (Desig_Typ))
16213 and then not Is_Private_Type (Parent_Type)
16214 then
16215 Desig_Typ := Full_View (Desig_Typ);
16216 end if;
16217
16218 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
16219
16220 -- Ada 2005 (AI-251): Handle also derivations of abstract
16221 -- interface primitives.
16222
16223 or else (Is_Interface (Desig_Typ)
16224 and then not Is_Class_Wide_Type (Desig_Typ))
16225 then
16226 Acc_Type := New_Copy (Id_Type);
16227 Set_Etype (Acc_Type, Acc_Type);
16228 Set_Scope (Acc_Type, New_Subp);
16229
16230 -- Set size of anonymous access type. If we have an access
16231 -- to an unconstrained array, this is a fat pointer, so it
16232 -- is sizes at twice addtress size.
16233
16234 if Is_Array_Type (Desig_Typ)
16235 and then not Is_Constrained (Desig_Typ)
16236 then
16237 Init_Size (Acc_Type, 2 * System_Address_Size);
16238
16239 -- Other cases use a thin pointer
16240
16241 else
16242 Init_Size (Acc_Type, System_Address_Size);
16243 end if;
16244
16245 -- Set remaining characterstics of anonymous access type
16246
16247 Reinit_Alignment (Acc_Type);
16248 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
16249
16250 Set_Etype (New_Id, Acc_Type);
16251 Set_Scope (New_Id, New_Subp);
16252
16253 -- Create a reference to it
16254
16255 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
16256
16257 else
16258 Set_Etype (New_Id, Id_Type);
16259 end if;
16260 end;
16261
16262 -- In Ada2012, a formal may have an incomplete type but the type
16263 -- derivation that inherits the primitive follows the full view.
16264
16265 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
16266 or else
16267 (Ekind (Id_Type) = E_Record_Type_With_Private
16268 and then Present (Full_View (Id_Type))
16269 and then
16270 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
16271 or else
16272 (Ada_Version >= Ada_2012
16273 and then Ekind (Id_Type) = E_Incomplete_Type
16274 and then Full_View (Id_Type) = Parent_Type)
16275 then
16276 -- Constraint checks on formals are generated during expansion,
16277 -- based on the signature of the original subprogram. The bounds
16278 -- of the derived type are not relevant, and thus we can use
16279 -- the base type for the formals. However, the return type may be
16280 -- used in a context that requires that the proper static bounds
16281 -- be used (a case statement, for example) and for those cases
16282 -- we must use the derived type (first subtype), not its base.
16283
16284 -- If the derived_type_definition has no constraints, we know that
16285 -- the derived type has the same constraints as the first subtype
16286 -- of the parent, and we can also use it rather than its base,
16287 -- which can lead to more efficient code.
16288
16289 if Id_Type = Parent_Type then
16290 if Is_Scalar_Type (Parent_Type)
16291 and then
16292 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16293 then
16294 Set_Etype (New_Id, Derived_Type);
16295
16296 elsif Nkind (Par) = N_Full_Type_Declaration
16297 and then
16298 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16299 and then
16300 Is_Entity_Name
16301 (Subtype_Indication (Type_Definition (Par)))
16302 then
16303 Set_Etype (New_Id, Derived_Type);
16304
16305 else
16306 Set_Etype (New_Id, Base_Type (Derived_Type));
16307 end if;
16308
16309 else
16310 Set_Etype (New_Id, Base_Type (Derived_Type));
16311 end if;
16312
16313 else
16314 Set_Etype (New_Id, Id_Type);
16315 end if;
16316 end Replace_Type;
16317
16318 ----------------------
16319 -- Set_Derived_Name --
16320 ----------------------
16321
16322 procedure Set_Derived_Name is
16323 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16324 begin
16325 if Nm = TSS_Null then
16326 Set_Chars (New_Subp, Chars (Parent_Subp));
16327 else
16328 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16329 end if;
16330 end Set_Derived_Name;
16331
16332 -- Start of processing for Derive_Subprogram
16333
16334 begin
16335 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16336 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16337 Set_Is_Not_Self_Hidden (New_Subp);
16338
16339 -- Check whether the inherited subprogram is a private operation that
16340 -- should be inherited but not yet made visible. Such subprograms can
16341 -- become visible at a later point (e.g., the private part of a public
16342 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16343 -- following predicate is true, then this is not such a private
16344 -- operation and the subprogram simply inherits the name of the parent
16345 -- subprogram. Note the special check for the names of controlled
16346 -- operations, which are currently exempted from being inherited with
16347 -- a hidden name because they must be findable for generation of
16348 -- implicit run-time calls.
16349
16350 if not Is_Hidden (Parent_Subp)
16351 or else Is_Internal (Parent_Subp)
16352 or else Is_Private_Overriding
16353 or else Is_Internal_Name (Chars (Parent_Subp))
16354 or else (Is_Controlled (Parent_Type)
16355 and then Chars (Parent_Subp) in Name_Adjust
16356 | Name_Finalize
16357 | Name_Initialize)
16358 then
16359 Set_Derived_Name;
16360
16361 -- An inherited dispatching equality will be overridden by an internally
16362 -- generated one, or by an explicit one, so preserve its name and thus
16363 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16364 -- private operation it may become invisible if the full view has
16365 -- progenitors, and the dispatch table will be malformed.
16366 -- We check that the type is limited to handle the anomalous declaration
16367 -- of Limited_Controlled, which is derived from a non-limited type, and
16368 -- which is handled specially elsewhere as well.
16369
16370 elsif Chars (Parent_Subp) = Name_Op_Eq
16371 and then Is_Dispatching_Operation (Parent_Subp)
16372 and then Etype (Parent_Subp) = Standard_Boolean
16373 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16374 and then
16375 Etype (First_Formal (Parent_Subp)) =
16376 Etype (Next_Formal (First_Formal (Parent_Subp)))
16377 then
16378 Set_Derived_Name;
16379
16380 -- If parent is hidden, this can be a regular derivation if the
16381 -- parent is immediately visible in a non-instantiating context,
16382 -- or if we are in the private part of an instance. This test
16383 -- should still be refined ???
16384
16385 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16386 -- operation as a non-visible operation in cases where the parent
16387 -- subprogram might not be visible now, but was visible within the
16388 -- original generic, so it would be wrong to make the inherited
16389 -- subprogram non-visible now. (Not clear if this test is fully
16390 -- correct; are there any cases where we should declare the inherited
16391 -- operation as not visible to avoid it being overridden, e.g., when
16392 -- the parent type is a generic actual with private primitives ???)
16393
16394 -- (they should be treated the same as other private inherited
16395 -- subprograms, but it's not clear how to do this cleanly). ???
16396
16397 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16398 and then Is_Immediately_Visible (Parent_Subp)
16399 and then not In_Instance)
16400 or else In_Instance_Not_Visible
16401 then
16402 Set_Derived_Name;
16403
16404 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16405 -- overrides an interface primitive because interface primitives
16406 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16407
16408 elsif Ada_Version >= Ada_2005
16409 and then Is_Dispatching_Operation (Parent_Subp)
16410 and then Present (Covered_Interface_Op (Parent_Subp))
16411 then
16412 Set_Derived_Name;
16413
16414 -- Otherwise, the type is inheriting a private operation, so enter it
16415 -- with a special name so it can't be overridden. See also below, where
16416 -- we check for this case, and if so avoid setting Requires_Overriding.
16417
16418 else
16419 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16420 end if;
16421
16422 Set_Parent (New_Subp, Parent (Derived_Type));
16423
16424 if Present (Actual_Subp) then
16425 Replace_Type (Actual_Subp, New_Subp);
16426 else
16427 Replace_Type (Parent_Subp, New_Subp);
16428 end if;
16429
16430 Conditional_Delay (New_Subp, Parent_Subp);
16431
16432 -- If we are creating a renaming for a primitive operation of an
16433 -- actual of a generic derived type, we must examine the signature
16434 -- of the actual primitive, not that of the generic formal, which for
16435 -- example may be an interface. However the name and initial value
16436 -- of the inherited operation are those of the formal primitive.
16437
16438 Formal := First_Formal (Parent_Subp);
16439
16440 if Present (Actual_Subp) then
16441 Formal_Of_Actual := First_Formal (Actual_Subp);
16442 else
16443 Formal_Of_Actual := Empty;
16444 end if;
16445
16446 while Present (Formal) loop
16447 New_Formal := New_Copy (Formal);
16448
16449 -- Extra formals are not inherited from a limited interface parent
16450 -- since limitedness is not inherited in such case (AI-419) and this
16451 -- affects the extra formals.
16452
16453 if Is_Limited_Interface (Parent_Type) then
16454 Set_Extra_Formal (New_Formal, Empty);
16455 Set_Extra_Accessibility (New_Formal, Empty);
16456 end if;
16457
16458 -- Normally we do not go copying parents, but in the case of
16459 -- formals, we need to link up to the declaration (which is the
16460 -- parameter specification), and it is fine to link up to the
16461 -- original formal's parameter specification in this case.
16462
16463 Set_Parent (New_Formal, Parent (Formal));
16464 Append_Entity (New_Formal, New_Subp);
16465
16466 if Present (Formal_Of_Actual) then
16467 Replace_Type (Formal_Of_Actual, New_Formal);
16468 Next_Formal (Formal_Of_Actual);
16469 else
16470 Replace_Type (Formal, New_Formal);
16471 end if;
16472
16473 Next_Formal (Formal);
16474 end loop;
16475
16476 -- Extra formals are shared between the parent subprogram and this
16477 -- internal entity built by Derive_Subprogram (implicit in the above
16478 -- copy of formals), unless the parent type is a limited interface type;
16479 -- hence we must inherit also the reference to the first extra formal.
16480 -- When the parent type is an interface, the extra formals will be added
16481 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16482
16483 if not Is_Limited_Interface (Parent_Type) then
16484 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16485
16486 if Ekind (New_Subp) = E_Function then
16487 Set_Extra_Accessibility_Of_Result (New_Subp,
16488 Extra_Accessibility_Of_Result (Parent_Subp));
16489 end if;
16490 end if;
16491
16492 -- If this derivation corresponds to a tagged generic actual, then
16493 -- primitive operations rename those of the actual. Otherwise the
16494 -- primitive operations rename those of the parent type, If the parent
16495 -- renames an intrinsic operator, so does the new subprogram. We except
16496 -- concatenation, which is always properly typed, and does not get
16497 -- expanded as other intrinsic operations.
16498
16499 if No (Actual_Subp) then
16500 if Is_Intrinsic_Subprogram (Parent_Subp) then
16501 Set_Convention (New_Subp, Convention_Intrinsic);
16502 Set_Is_Intrinsic_Subprogram (New_Subp);
16503
16504 if Present (Alias (Parent_Subp))
16505 and then Chars (Parent_Subp) /= Name_Op_Concat
16506 then
16507 Set_Alias (New_Subp, Alias (Parent_Subp));
16508 else
16509 Set_Alias (New_Subp, Parent_Subp);
16510 end if;
16511
16512 else
16513 Set_Alias (New_Subp, Parent_Subp);
16514 end if;
16515
16516 else
16517 Set_Alias (New_Subp, Actual_Subp);
16518 end if;
16519
16520 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16521
16522 -- Derived subprograms of a tagged type must inherit the convention
16523 -- of the parent subprogram (a requirement of AI95-117). Derived
16524 -- subprograms of untagged types simply get convention Ada by default.
16525
16526 -- If the derived type is a tagged generic formal type with unknown
16527 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16528
16529 -- However, if the type is derived from a generic formal, the further
16530 -- inherited subprogram has the convention of the non-generic ancestor.
16531 -- Otherwise there would be no way to override the operation.
16532 -- (This is subject to forthcoming ARG discussions).
16533
16534 if Is_Tagged_Type (Derived_Type) then
16535 if Is_Generic_Type (Derived_Type)
16536 and then Has_Unknown_Discriminants (Derived_Type)
16537 then
16538 Set_Convention (New_Subp, Convention_Intrinsic);
16539
16540 else
16541 if Is_Generic_Type (Parent_Type)
16542 and then Has_Unknown_Discriminants (Parent_Type)
16543 then
16544 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16545 else
16546 Set_Convention (New_Subp, Convention (Parent_Subp));
16547 end if;
16548 end if;
16549 end if;
16550
16551 -- Predefined controlled operations retain their name even if the parent
16552 -- is hidden (see above), but they are not primitive operations if the
16553 -- ancestor is not visible, for example if the parent is a private
16554 -- extension completed with a controlled extension. Note that a full
16555 -- type that is controlled can break privacy: the flag Is_Controlled is
16556 -- set on both views of the type.
16557
16558 if Is_Controlled (Parent_Type)
16559 and then Chars (Parent_Subp) in Name_Initialize
16560 | Name_Adjust
16561 | Name_Finalize
16562 and then Is_Hidden (Parent_Subp)
16563 and then not Is_Visibly_Controlled (Parent_Type)
16564 then
16565 Set_Is_Hidden (New_Subp);
16566 end if;
16567
16568 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16569 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16570
16571 if Ekind (Parent_Subp) = E_Procedure then
16572 Set_Is_Valued_Procedure
16573 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16574 else
16575 Set_Has_Controlling_Result
16576 (New_Subp, Has_Controlling_Result (Parent_Subp));
16577 end if;
16578
16579 -- No_Return must be inherited properly. If this is overridden in the
16580 -- case of a dispatching operation, then the check is made later in
16581 -- Check_Abstract_Overriding that the overriding operation is also
16582 -- No_Return (no such check is required for the nondispatching case).
16583
16584 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16585
16586 -- If the parent subprogram is marked as Ghost, then so is the derived
16587 -- subprogram. The ghost policy for the derived subprogram is set from
16588 -- the effective ghost policy at the point of derived type declaration.
16589
16590 if Is_Ghost_Entity (Parent_Subp) then
16591 Set_Is_Ghost_Entity (New_Subp);
16592 end if;
16593
16594 -- A derived function with a controlling result is abstract. If the
16595 -- Derived_Type is a nonabstract formal generic derived type, then
16596 -- inherited operations are not abstract: the required check is done at
16597 -- instantiation time. If the derivation is for a generic actual, the
16598 -- function is not abstract unless the actual is.
16599
16600 if Is_Generic_Type (Derived_Type)
16601 and then not Is_Abstract_Type (Derived_Type)
16602 then
16603 null;
16604
16605 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16606 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16607 -- that functions with controlling access results of record extensions
16608 -- with a null extension part require overriding (AI95-00391/06).
16609
16610 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16611 -- implementing the rule of RM 7.3.2(6.1/4).
16612
16613 -- A subprogram subject to pragma Extensions_Visible with value False
16614 -- requires overriding if the subprogram has at least one controlling
16615 -- OUT parameter (SPARK RM 6.1.7(6)).
16616
16617 elsif Ada_Version >= Ada_2005
16618 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16619 or else (Is_Tagged_Type (Derived_Type)
16620 and then Etype (New_Subp) = Derived_Type
16621 and then not Is_Null_Extension (Derived_Type))
16622 or else (Is_Tagged_Type (Derived_Type)
16623 and then Ekind (Etype (New_Subp)) =
16624 E_Anonymous_Access_Type
16625 and then Designated_Type (Etype (New_Subp)) =
16626 Derived_Type)
16627 or else (Comes_From_Source (Alias (New_Subp))
16628 and then Is_EVF_Procedure (Alias (New_Subp)))
16629
16630 -- AI12-0042: Set Requires_Overriding when a type extension
16631 -- inherits a private operation that is visible at the
16632 -- point of extension (Has_Private_Ancestor is False) from
16633 -- an ancestor that has Type_Invariant'Class, and when the
16634 -- type extension is in a visible part (the latter as
16635 -- clarified by AI12-0382).
16636
16637 or else
16638 (not Has_Private_Ancestor (Derived_Type)
16639 and then Has_Invariants (Parent_Type)
16640 and then
16641 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16642 and then
16643 Class_Present
16644 (Get_Pragma (Parent_Type, Pragma_Invariant))
16645 and then Is_Private_Primitive (Parent_Subp)
16646 and then In_Visible_Part (Scope (Derived_Type))))
16647
16648 and then No (Actual_Subp)
16649 then
16650 if not Is_Tagged_Type (Derived_Type)
16651 or else Is_Abstract_Type (Derived_Type)
16652 or else Is_Abstract_Subprogram (Alias (New_Subp))
16653 then
16654 Set_Is_Abstract_Subprogram (New_Subp);
16655
16656 -- If the Chars of the new subprogram is different from that of the
16657 -- parent's one, it means that we entered it with a special name so
16658 -- it can't be overridden (see above). In that case we had better not
16659 -- *require* it to be overridden. This is the case where the parent
16660 -- type inherited the operation privately, so there's no danger of
16661 -- dangling dispatching.
16662
16663 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16664 Set_Requires_Overriding (New_Subp);
16665 end if;
16666
16667 elsif Ada_Version < Ada_2005
16668 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16669 or else (Is_Tagged_Type (Derived_Type)
16670 and then Etype (New_Subp) = Derived_Type
16671 and then No (Actual_Subp)))
16672 then
16673 Set_Is_Abstract_Subprogram (New_Subp);
16674
16675 -- AI05-0097 : an inherited operation that dispatches on result is
16676 -- abstract if the derived type is abstract, even if the parent type
16677 -- is concrete and the derived type is a null extension.
16678
16679 elsif Has_Controlling_Result (Alias (New_Subp))
16680 and then Is_Abstract_Type (Etype (New_Subp))
16681 then
16682 Set_Is_Abstract_Subprogram (New_Subp);
16683
16684 -- Finally, if the parent type is abstract we must verify that all
16685 -- inherited operations are either non-abstract or overridden, or that
16686 -- the derived type itself is abstract (this check is performed at the
16687 -- end of a package declaration, in Check_Abstract_Overriding). A
16688 -- private overriding in the parent type will not be visible in the
16689 -- derivation if we are not in an inner package or in a child unit of
16690 -- the parent type, in which case the abstractness of the inherited
16691 -- operation is carried to the new subprogram.
16692
16693 elsif Is_Abstract_Type (Parent_Type)
16694 and then not In_Open_Scopes (Scope (Parent_Type))
16695 and then Is_Private_Overriding
16696 and then Is_Abstract_Subprogram (Visible_Subp)
16697 then
16698 if No (Actual_Subp) then
16699 Set_Alias (New_Subp, Visible_Subp);
16700 Set_Is_Abstract_Subprogram (New_Subp, True);
16701
16702 else
16703 -- If this is a derivation for an instance of a formal derived
16704 -- type, abstractness comes from the primitive operation of the
16705 -- actual, not from the operation inherited from the ancestor.
16706
16707 Set_Is_Abstract_Subprogram
16708 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16709 end if;
16710 end if;
16711
16712 New_Overloaded_Entity (New_Subp, Derived_Type);
16713
16714 -- RM 6.1.1(15): If a subprogram inherits nonconforming class-wide
16715 -- preconditions and the derived type is abstract, the derived operation
16716 -- is abstract as well if parent subprogram is not abstract or null.
16717
16718 if Is_Abstract_Type (Derived_Type)
16719 and then Has_Non_Trivial_Precondition (Parent_Subp)
16720 and then Present (Interfaces (Derived_Type))
16721 then
16722
16723 -- Add useful attributes of subprogram before the freeze point,
16724 -- in case freezing is delayed or there are previous errors.
16725
16726 Set_Is_Dispatching_Operation (New_Subp);
16727
16728 declare
16729 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16730
16731 begin
16732 if Present (Iface_Prim)
16733 and then Has_Non_Trivial_Precondition (Iface_Prim)
16734 then
16735 Set_Is_Abstract_Subprogram (New_Subp);
16736 end if;
16737 end;
16738 end if;
16739
16740 -- Check for case of a derived subprogram for the instantiation of a
16741 -- formal derived tagged type, if so mark the subprogram as dispatching
16742 -- and inherit the dispatching attributes of the actual subprogram. The
16743 -- derived subprogram is effectively renaming of the actual subprogram,
16744 -- so it needs to have the same attributes as the actual.
16745
16746 if Present (Actual_Subp)
16747 and then Is_Dispatching_Operation (Actual_Subp)
16748 then
16749 Set_Is_Dispatching_Operation (New_Subp);
16750
16751 if Present (DTC_Entity (Actual_Subp)) then
16752 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16753 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16754 end if;
16755 end if;
16756
16757 -- Indicate that a derived subprogram does not require a body and that
16758 -- it does not require processing of default expressions.
16759
16760 Set_Has_Completion (New_Subp);
16761 Set_Default_Expressions_Processed (New_Subp);
16762
16763 if Ekind (New_Subp) = E_Function then
16764 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16765 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16766 end if;
16767
16768 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16769 -- primitive subprogram S of a type T, then the aspect is inherited
16770 -- by the corresponding primitive subprogram of each descendant of T.
16771
16772 if Is_Tagged_Type (Derived_Type)
16773 and then Is_Dispatching_Operation (New_Subp)
16774 and then Has_Yield_Aspect (Alias (New_Subp))
16775 then
16776 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16777 end if;
16778
16779 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16780 end Derive_Subprogram;
16781
16782 ------------------------
16783 -- Derive_Subprograms --
16784 ------------------------
16785
16786 procedure Derive_Subprograms
16787 (Parent_Type : Entity_Id;
16788 Derived_Type : Entity_Id;
16789 Generic_Actual : Entity_Id := Empty)
16790 is
16791 Op_List : constant Elist_Id :=
16792 Collect_Primitive_Operations (Parent_Type);
16793
16794 function Check_Derived_Type return Boolean;
16795 -- Check that all the entities derived from Parent_Type are found in
16796 -- the list of primitives of Derived_Type exactly in the same order.
16797
16798 procedure Derive_Interface_Subprogram
16799 (New_Subp : out Entity_Id;
16800 Subp : Entity_Id;
16801 Actual_Subp : Entity_Id);
16802 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16803 -- (which is an interface primitive). If Generic_Actual is present then
16804 -- Actual_Subp is the actual subprogram corresponding with the generic
16805 -- subprogram Subp.
16806
16807 ------------------------
16808 -- Check_Derived_Type --
16809 ------------------------
16810
16811 function Check_Derived_Type return Boolean is
16812 E : Entity_Id;
16813 Derived_Elmt : Elmt_Id;
16814 Derived_Op : Entity_Id;
16815 Derived_Ops : Elist_Id;
16816 Parent_Elmt : Elmt_Id;
16817 Parent_Op : Entity_Id;
16818
16819 begin
16820 -- Traverse list of entities in the current scope searching for
16821 -- an incomplete type whose full-view is derived type.
16822
16823 E := First_Entity (Scope (Derived_Type));
16824 while Present (E) and then E /= Derived_Type loop
16825 if Ekind (E) = E_Incomplete_Type
16826 and then Present (Full_View (E))
16827 and then Full_View (E) = Derived_Type
16828 then
16829 -- Disable this test if Derived_Type completes an incomplete
16830 -- type because in such case more primitives can be added
16831 -- later to the list of primitives of Derived_Type by routine
16832 -- Process_Incomplete_Dependents.
16833
16834 return True;
16835 end if;
16836
16837 Next_Entity (E);
16838 end loop;
16839
16840 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16841
16842 Derived_Elmt := First_Elmt (Derived_Ops);
16843 Parent_Elmt := First_Elmt (Op_List);
16844 while Present (Parent_Elmt) loop
16845 Parent_Op := Node (Parent_Elmt);
16846 Derived_Op := Node (Derived_Elmt);
16847
16848 -- At this early stage Derived_Type has no entities with attribute
16849 -- Interface_Alias. In addition, such primitives are always
16850 -- located at the end of the list of primitives of Parent_Type.
16851 -- Therefore, if found we can safely stop processing pending
16852 -- entities.
16853
16854 exit when Present (Interface_Alias (Parent_Op));
16855
16856 -- Handle hidden entities
16857
16858 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16859 and then Is_Hidden (Parent_Op)
16860 then
16861 if Present (Derived_Op)
16862 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16863 then
16864 Next_Elmt (Derived_Elmt);
16865 end if;
16866
16867 else
16868 if No (Derived_Op)
16869 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16870 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16871 then
16872 return False;
16873 end if;
16874
16875 Next_Elmt (Derived_Elmt);
16876 end if;
16877
16878 Next_Elmt (Parent_Elmt);
16879 end loop;
16880
16881 return True;
16882 end Check_Derived_Type;
16883
16884 ---------------------------------
16885 -- Derive_Interface_Subprogram --
16886 ---------------------------------
16887
16888 procedure Derive_Interface_Subprogram
16889 (New_Subp : out Entity_Id;
16890 Subp : Entity_Id;
16891 Actual_Subp : Entity_Id)
16892 is
16893 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16894 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16895
16896 begin
16897 pragma Assert (Is_Interface (Iface_Type));
16898
16899 Derive_Subprogram
16900 (New_Subp => New_Subp,
16901 Parent_Subp => Iface_Subp,
16902 Derived_Type => Derived_Type,
16903 Parent_Type => Iface_Type,
16904 Actual_Subp => Actual_Subp);
16905
16906 -- Given that this new interface entity corresponds with a primitive
16907 -- of the parent that was not overridden we must leave it associated
16908 -- with its parent primitive to ensure that it will share the same
16909 -- dispatch table slot when overridden. We must set the Alias to Subp
16910 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16911 -- (in case we inherited Subp from Iface_Type via a nonabstract
16912 -- generic formal type).
16913
16914 if No (Actual_Subp) then
16915 Set_Alias (New_Subp, Subp);
16916
16917 declare
16918 T : Entity_Id := Find_Dispatching_Type (Subp);
16919 begin
16920 while Etype (T) /= T loop
16921 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16922 Set_Is_Abstract_Subprogram (New_Subp, False);
16923 exit;
16924 end if;
16925
16926 T := Etype (T);
16927 end loop;
16928 end;
16929
16930 -- For instantiations this is not needed since the previous call to
16931 -- Derive_Subprogram leaves the entity well decorated.
16932
16933 else
16934 pragma Assert (Alias (New_Subp) = Actual_Subp);
16935 null;
16936 end if;
16937 end Derive_Interface_Subprogram;
16938
16939 -- Local variables
16940
16941 Alias_Subp : Entity_Id;
16942 Act_List : Elist_Id;
16943 Act_Elmt : Elmt_Id;
16944 Act_Subp : Entity_Id := Empty;
16945 Elmt : Elmt_Id;
16946 Need_Search : Boolean := False;
16947 New_Subp : Entity_Id;
16948 Parent_Base : Entity_Id;
16949 Subp : Entity_Id;
16950
16951 -- Start of processing for Derive_Subprograms
16952
16953 begin
16954 if Ekind (Parent_Type) = E_Record_Type_With_Private
16955 and then Has_Discriminants (Parent_Type)
16956 and then Present (Full_View (Parent_Type))
16957 then
16958 Parent_Base := Full_View (Parent_Type);
16959 else
16960 Parent_Base := Parent_Type;
16961 end if;
16962
16963 if Present (Generic_Actual) then
16964 Act_List := Collect_Primitive_Operations (Generic_Actual);
16965 Act_Elmt := First_Elmt (Act_List);
16966 else
16967 Act_List := No_Elist;
16968 Act_Elmt := No_Elmt;
16969 end if;
16970
16971 -- Derive primitives inherited from the parent. Note that if the generic
16972 -- actual is present, this is not really a type derivation, it is a
16973 -- completion within an instance.
16974
16975 -- Case 1: Derived_Type does not implement interfaces
16976
16977 if not Is_Tagged_Type (Derived_Type)
16978 or else (not Has_Interfaces (Derived_Type)
16979 and then not (Present (Generic_Actual)
16980 and then Has_Interfaces (Generic_Actual)))
16981 then
16982 Elmt := First_Elmt (Op_List);
16983 while Present (Elmt) loop
16984 Subp := Node (Elmt);
16985
16986 -- Literals are derived earlier in the process of building the
16987 -- derived type, and are skipped here.
16988
16989 if Ekind (Subp) = E_Enumeration_Literal then
16990 null;
16991
16992 -- The actual is a direct descendant and the common primitive
16993 -- operations appear in the same order.
16994
16995 -- If the generic parent type is present, the derived type is an
16996 -- instance of a formal derived type, and within the instance its
16997 -- operations are those of the actual. We derive from the formal
16998 -- type but make the inherited operations aliases of the
16999 -- corresponding operations of the actual.
17000
17001 else
17002 pragma Assert (No (Node (Act_Elmt))
17003 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
17004 and then
17005 Type_Conformant
17006 (Subp, Node (Act_Elmt),
17007 Skip_Controlling_Formals => True)));
17008
17009 Derive_Subprogram
17010 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
17011
17012 if Present (Act_Elmt) then
17013 Next_Elmt (Act_Elmt);
17014 end if;
17015 end if;
17016
17017 Next_Elmt (Elmt);
17018 end loop;
17019
17020 -- Case 2: Derived_Type implements interfaces
17021
17022 else
17023 -- If the parent type has no predefined primitives we remove
17024 -- predefined primitives from the list of primitives of generic
17025 -- actual to simplify the complexity of this algorithm.
17026
17027 if Present (Generic_Actual) then
17028 declare
17029 Has_Predefined_Primitives : Boolean := False;
17030
17031 begin
17032 -- Check if the parent type has predefined primitives
17033
17034 Elmt := First_Elmt (Op_List);
17035 while Present (Elmt) loop
17036 Subp := Node (Elmt);
17037
17038 if Is_Predefined_Dispatching_Operation (Subp)
17039 and then not Comes_From_Source (Ultimate_Alias (Subp))
17040 then
17041 Has_Predefined_Primitives := True;
17042 exit;
17043 end if;
17044
17045 Next_Elmt (Elmt);
17046 end loop;
17047
17048 -- Remove predefined primitives of Generic_Actual. We must use
17049 -- an auxiliary list because in case of tagged types the value
17050 -- returned by Collect_Primitive_Operations is the value stored
17051 -- in its Primitive_Operations attribute (and we don't want to
17052 -- modify its current contents).
17053
17054 if not Has_Predefined_Primitives then
17055 declare
17056 Aux_List : constant Elist_Id := New_Elmt_List;
17057
17058 begin
17059 Elmt := First_Elmt (Act_List);
17060 while Present (Elmt) loop
17061 Subp := Node (Elmt);
17062
17063 if not Is_Predefined_Dispatching_Operation (Subp)
17064 or else Comes_From_Source (Subp)
17065 then
17066 Append_Elmt (Subp, Aux_List);
17067 end if;
17068
17069 Next_Elmt (Elmt);
17070 end loop;
17071
17072 Act_List := Aux_List;
17073 end;
17074 end if;
17075
17076 Act_Elmt := First_Elmt (Act_List);
17077 Act_Subp := Node (Act_Elmt);
17078 end;
17079 end if;
17080
17081 -- Stage 1: If the generic actual is not present we derive the
17082 -- primitives inherited from the parent type. If the generic parent
17083 -- type is present, the derived type is an instance of a formal
17084 -- derived type, and within the instance its operations are those of
17085 -- the actual. We derive from the formal type but make the inherited
17086 -- operations aliases of the corresponding operations of the actual.
17087
17088 Elmt := First_Elmt (Op_List);
17089 while Present (Elmt) loop
17090 Subp := Node (Elmt);
17091 Alias_Subp := Ultimate_Alias (Subp);
17092
17093 -- Do not derive internal entities of the parent that link
17094 -- interface primitives with their covering primitive. These
17095 -- entities will be added to this type when frozen.
17096
17097 if Present (Interface_Alias (Subp)) then
17098 goto Continue;
17099 end if;
17100
17101 -- If the generic actual is present find the corresponding
17102 -- operation in the generic actual. If the parent type is a
17103 -- direct ancestor of the derived type then, even if it is an
17104 -- interface, the operations are inherited from the primary
17105 -- dispatch table and are in the proper order. If we detect here
17106 -- that primitives are not in the same order we traverse the list
17107 -- of primitive operations of the actual to find the one that
17108 -- implements the interface primitive.
17109
17110 if Need_Search
17111 or else
17112 (Present (Generic_Actual)
17113 and then Present (Act_Subp)
17114 and then not
17115 (Primitive_Names_Match (Subp, Act_Subp)
17116 and then
17117 Type_Conformant (Subp, Act_Subp,
17118 Skip_Controlling_Formals => True)))
17119 then
17120 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
17121 Use_Full_View => True));
17122
17123 -- Remember that we need searching for all pending primitives
17124
17125 Need_Search := True;
17126
17127 -- Handle entities associated with interface primitives
17128
17129 if Present (Alias_Subp)
17130 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
17131 and then not Is_Predefined_Dispatching_Operation (Subp)
17132 then
17133 -- Search for the primitive in the homonym chain
17134
17135 Act_Subp :=
17136 Find_Primitive_Covering_Interface
17137 (Tagged_Type => Generic_Actual,
17138 Iface_Prim => Alias_Subp);
17139
17140 -- Previous search may not locate primitives covering
17141 -- interfaces defined in generics units or instantiations.
17142 -- (it fails if the covering primitive has formals whose
17143 -- type is also defined in generics or instantiations).
17144 -- In such case we search in the list of primitives of the
17145 -- generic actual for the internal entity that links the
17146 -- interface primitive and the covering primitive.
17147
17148 if No (Act_Subp)
17149 and then Is_Generic_Type (Parent_Type)
17150 then
17151 -- This code has been designed to handle only generic
17152 -- formals that implement interfaces that are defined
17153 -- in a generic unit or instantiation. If this code is
17154 -- needed for other cases we must review it because
17155 -- (given that it relies on Original_Location to locate
17156 -- the primitive of Generic_Actual that covers the
17157 -- interface) it could leave linked through attribute
17158 -- Alias entities of unrelated instantiations).
17159
17160 pragma Assert
17161 (Is_Generic_Unit
17162 (Scope (Find_Dispatching_Type (Alias_Subp)))
17163 or else
17164 Instantiation_Location
17165 (Sloc (Find_Dispatching_Type (Alias_Subp)))
17166 /= No_Location);
17167 declare
17168 Iface_Prim_Loc : constant Source_Ptr :=
17169 Original_Location (Sloc (Alias_Subp));
17170
17171 Elmt : Elmt_Id;
17172 Prim : Entity_Id;
17173
17174 begin
17175 Elmt :=
17176 First_Elmt (Primitive_Operations (Generic_Actual));
17177
17178 Search : while Present (Elmt) loop
17179 Prim := Node (Elmt);
17180
17181 if Present (Interface_Alias (Prim))
17182 and then Original_Location
17183 (Sloc (Interface_Alias (Prim))) =
17184 Iface_Prim_Loc
17185 then
17186 Act_Subp := Alias (Prim);
17187 exit Search;
17188 end if;
17189
17190 Next_Elmt (Elmt);
17191 end loop Search;
17192 end;
17193 end if;
17194
17195 pragma Assert (Present (Act_Subp)
17196 or else Is_Abstract_Type (Generic_Actual)
17197 or else Serious_Errors_Detected > 0);
17198
17199 -- Handle predefined primitives plus the rest of user-defined
17200 -- primitives
17201
17202 else
17203 Act_Elmt := First_Elmt (Act_List);
17204 while Present (Act_Elmt) loop
17205 Act_Subp := Node (Act_Elmt);
17206
17207 exit when Primitive_Names_Match (Subp, Act_Subp)
17208 and then Type_Conformant
17209 (Subp, Act_Subp,
17210 Skip_Controlling_Formals => True)
17211 and then No (Interface_Alias (Act_Subp));
17212
17213 Next_Elmt (Act_Elmt);
17214 end loop;
17215
17216 if No (Act_Elmt) then
17217 Act_Subp := Empty;
17218 end if;
17219 end if;
17220 end if;
17221
17222 -- Case 1: If the parent is a limited interface then it has the
17223 -- predefined primitives of synchronized interfaces. However, the
17224 -- actual type may be a non-limited type and hence it does not
17225 -- have such primitives.
17226
17227 if Present (Generic_Actual)
17228 and then No (Act_Subp)
17229 and then Is_Limited_Interface (Parent_Base)
17230 and then Is_Predefined_Interface_Primitive (Subp)
17231 then
17232 null;
17233
17234 -- Case 2: Inherit entities associated with interfaces that were
17235 -- not covered by the parent type. We exclude here null interface
17236 -- primitives because they do not need special management.
17237
17238 -- We also exclude interface operations that are renamings. If the
17239 -- subprogram is an explicit renaming of an interface primitive,
17240 -- it is a regular primitive operation, and the presence of its
17241 -- alias is not relevant: it has to be derived like any other
17242 -- primitive.
17243
17244 elsif Present (Alias (Subp))
17245 and then Nkind (Unit_Declaration_Node (Subp)) /=
17246 N_Subprogram_Renaming_Declaration
17247 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
17248 and then not
17249 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
17250 and then Null_Present (Parent (Alias_Subp)))
17251 then
17252 -- If this is an abstract private type then we transfer the
17253 -- derivation of the interface primitive from the partial view
17254 -- to the full view. This is safe because all the interfaces
17255 -- must be visible in the partial view. Done to avoid adding
17256 -- a new interface derivation to the private part of the
17257 -- enclosing package; otherwise this new derivation would be
17258 -- decorated as hidden when the analysis of the enclosing
17259 -- package completes.
17260
17261 if Is_Abstract_Type (Derived_Type)
17262 and then In_Private_Part (Current_Scope)
17263 and then Has_Private_Declaration (Derived_Type)
17264 then
17265 declare
17266 Partial_View : Entity_Id;
17267 Elmt : Elmt_Id;
17268 Ent : Entity_Id;
17269
17270 begin
17271 Partial_View := First_Entity (Current_Scope);
17272 loop
17273 exit when No (Partial_View)
17274 or else (Has_Private_Declaration (Partial_View)
17275 and then
17276 Full_View (Partial_View) = Derived_Type);
17277
17278 Next_Entity (Partial_View);
17279 end loop;
17280
17281 -- If the partial view was not found then the source code
17282 -- has errors and the derivation is not needed.
17283
17284 if Present (Partial_View) then
17285 Elmt :=
17286 First_Elmt (Primitive_Operations (Partial_View));
17287 while Present (Elmt) loop
17288 Ent := Node (Elmt);
17289
17290 if Present (Alias (Ent))
17291 and then Ultimate_Alias (Ent) = Alias (Subp)
17292 then
17293 Append_Elmt
17294 (Ent, Primitive_Operations (Derived_Type));
17295 exit;
17296 end if;
17297
17298 Next_Elmt (Elmt);
17299 end loop;
17300
17301 -- If the interface primitive was not found in the
17302 -- partial view then this interface primitive was
17303 -- overridden. We add a derivation to activate in
17304 -- Derive_Progenitor_Subprograms the machinery to
17305 -- search for it.
17306
17307 if No (Elmt) then
17308 Derive_Interface_Subprogram
17309 (New_Subp => New_Subp,
17310 Subp => Subp,
17311 Actual_Subp => Act_Subp);
17312 end if;
17313 end if;
17314 end;
17315 else
17316 Derive_Interface_Subprogram
17317 (New_Subp => New_Subp,
17318 Subp => Subp,
17319 Actual_Subp => Act_Subp);
17320 end if;
17321
17322 -- Case 3: Common derivation
17323
17324 else
17325 Derive_Subprogram
17326 (New_Subp => New_Subp,
17327 Parent_Subp => Subp,
17328 Derived_Type => Derived_Type,
17329 Parent_Type => Parent_Base,
17330 Actual_Subp => Act_Subp);
17331 end if;
17332
17333 -- No need to update Act_Elm if we must search for the
17334 -- corresponding operation in the generic actual
17335
17336 if not Need_Search
17337 and then Present (Act_Elmt)
17338 then
17339 Next_Elmt (Act_Elmt);
17340 Act_Subp := Node (Act_Elmt);
17341 end if;
17342
17343 <<Continue>>
17344 Next_Elmt (Elmt);
17345 end loop;
17346
17347 -- Inherit additional operations from progenitors. If the derived
17348 -- type is a generic actual, there are not new primitive operations
17349 -- for the type because it has those of the actual, and therefore
17350 -- nothing needs to be done. The renamings generated above are not
17351 -- primitive operations, and their purpose is simply to make the
17352 -- proper operations visible within an instantiation.
17353
17354 if No (Generic_Actual) then
17355 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17356 end if;
17357 end if;
17358
17359 -- Final check: Direct descendants must have their primitives in the
17360 -- same order. We exclude from this test untagged types and instances
17361 -- of formal derived types. We skip this test if we have already
17362 -- reported serious errors in the sources.
17363
17364 pragma Assert (not Is_Tagged_Type (Derived_Type)
17365 or else Present (Generic_Actual)
17366 or else Serious_Errors_Detected > 0
17367 or else Check_Derived_Type);
17368 end Derive_Subprograms;
17369
17370 --------------------------------
17371 -- Derived_Standard_Character --
17372 --------------------------------
17373
17374 procedure Derived_Standard_Character
17375 (N : Node_Id;
17376 Parent_Type : Entity_Id;
17377 Derived_Type : Entity_Id)
17378 is
17379 Loc : constant Source_Ptr := Sloc (N);
17380 Def : constant Node_Id := Type_Definition (N);
17381 Indic : constant Node_Id := Subtype_Indication (Def);
17382 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17383 Implicit_Base : constant Entity_Id :=
17384 Create_Itype
17385 (E_Enumeration_Type, N, Derived_Type, 'B');
17386
17387 Lo : Node_Id;
17388 Hi : Node_Id;
17389
17390 begin
17391 Discard_Node (Process_Subtype (Indic, N));
17392
17393 Set_Etype (Implicit_Base, Parent_Base);
17394 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17395 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17396
17397 Set_Is_Character_Type (Implicit_Base, True);
17398 Set_Has_Delayed_Freeze (Implicit_Base);
17399
17400 -- The bounds of the implicit base are the bounds of the parent base.
17401 -- Note that their type is the parent base.
17402
17403 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17404 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17405
17406 Set_Scalar_Range (Implicit_Base,
17407 Make_Range (Loc,
17408 Low_Bound => Lo,
17409 High_Bound => Hi));
17410
17411 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17412 Set_Etype (Derived_Type, Implicit_Base);
17413 Set_Size_Info (Derived_Type, Parent_Type);
17414
17415 if not Known_RM_Size (Derived_Type) then
17416 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17417 end if;
17418
17419 Set_Is_Character_Type (Derived_Type, True);
17420
17421 if Nkind (Indic) /= N_Subtype_Indication then
17422
17423 -- If no explicit constraint, the bounds are those
17424 -- of the parent type.
17425
17426 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17427 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17428 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17429 end if;
17430
17431 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17432 end Derived_Standard_Character;
17433
17434 ------------------------------
17435 -- Derived_Type_Declaration --
17436 ------------------------------
17437
17438 procedure Derived_Type_Declaration
17439 (T : Entity_Id;
17440 N : Node_Id;
17441 Is_Completion : Boolean)
17442 is
17443 Parent_Type : Entity_Id;
17444
17445 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17446 -- Check whether the parent type is a generic formal, or derives
17447 -- directly or indirectly from one.
17448
17449 ------------------------
17450 -- Comes_From_Generic --
17451 ------------------------
17452
17453 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17454 begin
17455 if Is_Generic_Type (Typ) then
17456 return True;
17457
17458 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17459 return True;
17460
17461 elsif Is_Private_Type (Typ)
17462 and then Present (Full_View (Typ))
17463 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17464 then
17465 return True;
17466
17467 elsif Is_Generic_Actual_Type (Typ) then
17468 return True;
17469
17470 else
17471 return False;
17472 end if;
17473 end Comes_From_Generic;
17474
17475 -- Local variables
17476
17477 Def : constant Node_Id := Type_Definition (N);
17478 Iface_Def : Node_Id;
17479 Indic : constant Node_Id := Subtype_Indication (Def);
17480 Extension : constant Node_Id := Record_Extension_Part (Def);
17481 Parent_Node : Node_Id;
17482 Taggd : Boolean;
17483
17484 -- Start of processing for Derived_Type_Declaration
17485
17486 begin
17487 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17488
17489 -- Ada 2005 (AI-251): In case of interface derivation check that the
17490 -- parent is also an interface.
17491
17492 if Interface_Present (Def) then
17493 if not Is_Interface (Parent_Type) then
17494 Diagnose_Interface (Indic, Parent_Type);
17495
17496 else
17497 Parent_Node := Parent (Base_Type (Parent_Type));
17498 Iface_Def := Type_Definition (Parent_Node);
17499
17500 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17501 -- other limited interfaces.
17502
17503 if Limited_Present (Def) then
17504 if Limited_Present (Iface_Def) then
17505 null;
17506
17507 elsif Protected_Present (Iface_Def) then
17508 Error_Msg_NE
17509 ("descendant of & must be declared as a protected "
17510 & "interface", N, Parent_Type);
17511
17512 elsif Synchronized_Present (Iface_Def) then
17513 Error_Msg_NE
17514 ("descendant of & must be declared as a synchronized "
17515 & "interface", N, Parent_Type);
17516
17517 elsif Task_Present (Iface_Def) then
17518 Error_Msg_NE
17519 ("descendant of & must be declared as a task interface",
17520 N, Parent_Type);
17521
17522 else
17523 Error_Msg_N
17524 ("(Ada 2005) limited interface cannot inherit from "
17525 & "non-limited interface", Indic);
17526 end if;
17527
17528 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17529 -- from non-limited or limited interfaces.
17530
17531 elsif not Protected_Present (Def)
17532 and then not Synchronized_Present (Def)
17533 and then not Task_Present (Def)
17534 then
17535 if Limited_Present (Iface_Def) then
17536 null;
17537
17538 elsif Protected_Present (Iface_Def) then
17539 Error_Msg_NE
17540 ("descendant of & must be declared as a protected "
17541 & "interface", N, Parent_Type);
17542
17543 elsif Synchronized_Present (Iface_Def) then
17544 Error_Msg_NE
17545 ("descendant of & must be declared as a synchronized "
17546 & "interface", N, Parent_Type);
17547
17548 elsif Task_Present (Iface_Def) then
17549 Error_Msg_NE
17550 ("descendant of & must be declared as a task interface",
17551 N, Parent_Type);
17552 else
17553 null;
17554 end if;
17555 end if;
17556 end if;
17557 end if;
17558
17559 if Is_Tagged_Type (Parent_Type)
17560 and then Is_Concurrent_Type (Parent_Type)
17561 and then not Is_Interface (Parent_Type)
17562 then
17563 Error_Msg_N
17564 ("parent type of a record extension cannot be a synchronized "
17565 & "tagged type (RM 3.9.1 (3/1))", N);
17566 Set_Etype (T, Any_Type);
17567 return;
17568 end if;
17569
17570 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17571 -- interfaces
17572
17573 if Is_Tagged_Type (Parent_Type)
17574 and then Is_Non_Empty_List (Interface_List (Def))
17575 then
17576 declare
17577 Intf : Node_Id;
17578 T : Entity_Id;
17579
17580 begin
17581 Intf := First (Interface_List (Def));
17582 while Present (Intf) loop
17583 T := Find_Type_Of_Subtype_Indic (Intf);
17584
17585 if not Is_Interface (T) then
17586 Diagnose_Interface (Intf, T);
17587
17588 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17589 -- a limited type from having a nonlimited progenitor.
17590
17591 elsif (Limited_Present (Def)
17592 or else (not Is_Interface (Parent_Type)
17593 and then Is_Limited_Type (Parent_Type)))
17594 and then not Is_Limited_Interface (T)
17595 then
17596 Error_Msg_NE
17597 ("progenitor interface& of limited type must be limited",
17598 N, T);
17599 end if;
17600
17601 Next (Intf);
17602 end loop;
17603 end;
17604
17605 -- Check consistency of any nonoverridable aspects that are
17606 -- inherited from multiple sources.
17607
17608 Check_Inherited_Nonoverridable_Aspects
17609 (Inheritor => T,
17610 Interface_List => Interface_List (Def),
17611 Parent_Type => Parent_Type);
17612 end if;
17613
17614 if Parent_Type = Any_Type
17615 or else Etype (Parent_Type) = Any_Type
17616 or else (Is_Class_Wide_Type (Parent_Type)
17617 and then Etype (Parent_Type) = T)
17618 then
17619 -- If Parent_Type is undefined or illegal, make new type into a
17620 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17621 -- errors. If this is a self-definition, emit error now.
17622
17623 if T = Parent_Type or else T = Etype (Parent_Type) then
17624 Error_Msg_N ("type cannot be used in its own definition", Indic);
17625 end if;
17626
17627 Mutate_Ekind (T, Ekind (Parent_Type));
17628 Set_Etype (T, Any_Type);
17629 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17630
17631 -- Initialize the list of primitive operations to an empty list,
17632 -- to cover tagged types as well as untagged types. For untagged
17633 -- types this is used either to analyze the call as legal when
17634 -- GNAT extensions are allowed, or to give better error messages.
17635
17636 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17637
17638 return;
17639 end if;
17640
17641 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17642 -- an interface is special because the list of interfaces in the full
17643 -- view can be given in any order. For example:
17644
17645 -- type A is interface;
17646 -- type B is interface and A;
17647 -- type D is new B with private;
17648 -- private
17649 -- type D is new A and B with null record; -- 1 --
17650
17651 -- In this case we perform the following transformation of -1-:
17652
17653 -- type D is new B and A with null record;
17654
17655 -- If the parent of the full-view covers the parent of the partial-view
17656 -- we have two possible cases:
17657
17658 -- 1) They have the same parent
17659 -- 2) The parent of the full-view implements some further interfaces
17660
17661 -- In both cases we do not need to perform the transformation. In the
17662 -- first case the source program is correct and the transformation is
17663 -- not needed; in the second case the source program does not fulfill
17664 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17665 -- later.
17666
17667 -- This transformation not only simplifies the rest of the analysis of
17668 -- this type declaration but also simplifies the correct generation of
17669 -- the object layout to the expander.
17670
17671 if In_Private_Part (Current_Scope)
17672 and then Is_Interface (Parent_Type)
17673 then
17674 declare
17675 Partial_View : Entity_Id;
17676 Partial_View_Parent : Entity_Id;
17677
17678 function Reorder_Interfaces return Boolean;
17679 -- Look for an interface in the full view's interface list that
17680 -- matches the parent type of the partial view, and when found,
17681 -- rewrite the full view's parent with the partial view's parent,
17682 -- append the full view's original parent to the interface list,
17683 -- recursively call Derived_Type_Definition on the full type, and
17684 -- return True. If a match is not found, return False.
17685
17686 ------------------------
17687 -- Reorder_Interfaces --
17688 ------------------------
17689
17690 function Reorder_Interfaces return Boolean is
17691 Iface : Node_Id;
17692 New_Iface : Node_Id;
17693
17694 begin
17695 Iface := First (Interface_List (Def));
17696 while Present (Iface) loop
17697 if Etype (Iface) = Etype (Partial_View) then
17698 Rewrite (Subtype_Indication (Def),
17699 New_Copy (Subtype_Indication (Parent (Partial_View))));
17700
17701 New_Iface :=
17702 Make_Identifier (Sloc (N), Chars (Parent_Type));
17703 Rewrite (Iface, New_Iface);
17704
17705 -- Analyze the transformed code
17706
17707 Derived_Type_Declaration (T, N, Is_Completion);
17708 return True;
17709 end if;
17710
17711 Next (Iface);
17712 end loop;
17713 return False;
17714 end Reorder_Interfaces;
17715
17716 begin
17717 -- Look for the associated private type declaration
17718
17719 Partial_View := Incomplete_Or_Partial_View (T);
17720
17721 -- If the partial view was not found then the source code has
17722 -- errors and the transformation is not needed.
17723
17724 if Present (Partial_View) then
17725 Partial_View_Parent := Etype (Partial_View);
17726
17727 -- If the parent of the full-view covers the parent of the
17728 -- partial-view we have nothing else to do.
17729
17730 if Interface_Present_In_Ancestor
17731 (Parent_Type, Partial_View_Parent)
17732 then
17733 null;
17734
17735 -- Traverse the list of interfaces of the full view to look
17736 -- for the parent of the partial view and reorder the
17737 -- interfaces to match the order in the partial view,
17738 -- if needed.
17739
17740 else
17741
17742 if Reorder_Interfaces then
17743 -- Having the interfaces listed in any order is legal.
17744 -- However, the compiler does not properly handle
17745 -- different orders between partial and full views in
17746 -- generic units. We give a warning about the order
17747 -- mismatch, so the user can work around this problem.
17748
17749 Error_Msg_N ("??full declaration does not respect " &
17750 "partial declaration order", T);
17751 Error_Msg_N ("\??consider reordering", T);
17752
17753 return;
17754 end if;
17755 end if;
17756 end if;
17757 end;
17758 end if;
17759
17760 -- Only composite types other than array types are allowed to have
17761 -- discriminants.
17762
17763 if Present (Discriminant_Specifications (N)) then
17764 if (Is_Elementary_Type (Parent_Type)
17765 or else
17766 Is_Array_Type (Parent_Type))
17767 and then not Error_Posted (N)
17768 then
17769 Error_Msg_N
17770 ("elementary or array type cannot have discriminants",
17771 Defining_Identifier (First (Discriminant_Specifications (N))));
17772
17773 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17774 -- only if we are not already processing a malformed syntax tree.
17775
17776 if Is_Type (T) then
17777 Set_Has_Discriminants (T, False);
17778 end if;
17779 end if;
17780 end if;
17781
17782 -- In Ada 83, a derived type defined in a package specification cannot
17783 -- be used for further derivation until the end of its visible part.
17784 -- Note that derivation in the private part of the package is allowed.
17785
17786 if Ada_Version = Ada_83
17787 and then Is_Derived_Type (Parent_Type)
17788 and then In_Visible_Part (Scope (Parent_Type))
17789 then
17790 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17791 Error_Msg_N
17792 ("(Ada 83) premature use of type for derivation", Indic);
17793 end if;
17794 end if;
17795
17796 -- Check for early use of incomplete or private type
17797
17798 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17799 Error_Msg_N ("premature derivation of incomplete type", Indic);
17800 return;
17801
17802 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17803 and then not Comes_From_Generic (Parent_Type))
17804 or else Has_Private_Component (Parent_Type)
17805 then
17806 -- The ancestor type of a formal type can be incomplete, in which
17807 -- case only the operations of the partial view are available in the
17808 -- generic. Subsequent checks may be required when the full view is
17809 -- analyzed to verify that a derivation from a tagged type has an
17810 -- extension.
17811
17812 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17813 null;
17814
17815 elsif No (Underlying_Type (Parent_Type))
17816 or else Has_Private_Component (Parent_Type)
17817 then
17818 Error_Msg_N
17819 ("premature derivation of derived or private type", Indic);
17820
17821 -- Flag the type itself as being in error, this prevents some
17822 -- nasty problems with subsequent uses of the malformed type.
17823
17824 Set_Error_Posted (T);
17825
17826 -- Check that within the immediate scope of an untagged partial
17827 -- view it's illegal to derive from the partial view if the
17828 -- full view is tagged. (7.3(7))
17829
17830 -- We verify that the Parent_Type is a partial view by checking
17831 -- that it is not a Full_Type_Declaration (i.e. a private type or
17832 -- private extension declaration), to distinguish a partial view
17833 -- from a derivation from a private type which also appears as
17834 -- E_Private_Type. If the parent base type is not declared in an
17835 -- enclosing scope there is no need to check.
17836
17837 elsif Present (Full_View (Parent_Type))
17838 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17839 and then not Is_Tagged_Type (Parent_Type)
17840 and then Is_Tagged_Type (Full_View (Parent_Type))
17841 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17842 then
17843 Error_Msg_N
17844 ("premature derivation from type with tagged full view",
17845 Indic);
17846 end if;
17847 end if;
17848
17849 -- Check that form of derivation is appropriate
17850
17851 Taggd := Is_Tagged_Type (Parent_Type);
17852
17853 -- Set the parent type to the class-wide type's specific type in this
17854 -- case to prevent cascading errors
17855
17856 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17857 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17858 Set_Etype (T, Etype (Parent_Type));
17859 return;
17860 end if;
17861
17862 if Present (Extension) and then not Taggd then
17863 Error_Msg_N
17864 ("type derived from untagged type cannot have extension", Indic);
17865
17866 elsif No (Extension) and then Taggd then
17867
17868 -- If this declaration is within a private part (or body) of a
17869 -- generic instantiation then the derivation is allowed (the parent
17870 -- type can only appear tagged in this case if it's a generic actual
17871 -- type, since it would otherwise have been rejected in the analysis
17872 -- of the generic template).
17873
17874 if not Is_Generic_Actual_Type (Parent_Type)
17875 or else In_Visible_Part (Scope (Parent_Type))
17876 then
17877 if Is_Class_Wide_Type (Parent_Type) then
17878 Error_Msg_N
17879 ("parent type must not be a class-wide type", Indic);
17880
17881 -- Use specific type to prevent cascaded errors.
17882
17883 Parent_Type := Etype (Parent_Type);
17884
17885 else
17886 Error_Msg_N
17887 ("type derived from tagged type must have extension", Indic);
17888 end if;
17889 end if;
17890 end if;
17891
17892 -- AI-443: Synchronized formal derived types require a private
17893 -- extension. There is no point in checking the ancestor type or
17894 -- the progenitors since the construct is wrong to begin with.
17895
17896 if Ada_Version >= Ada_2005
17897 and then Is_Generic_Type (T)
17898 and then Present (Original_Node (N))
17899 then
17900 declare
17901 Decl : constant Node_Id := Original_Node (N);
17902
17903 begin
17904 if Nkind (Decl) = N_Formal_Type_Declaration
17905 and then Nkind (Formal_Type_Definition (Decl)) =
17906 N_Formal_Derived_Type_Definition
17907 and then Synchronized_Present (Formal_Type_Definition (Decl))
17908 and then No (Extension)
17909
17910 -- Avoid emitting a duplicate error message
17911
17912 and then not Error_Posted (Indic)
17913 then
17914 Error_Msg_N
17915 ("synchronized derived type must have extension", N);
17916 end if;
17917 end;
17918 end if;
17919
17920 if Null_Exclusion_Present (Def)
17921 and then not Is_Access_Type (Parent_Type)
17922 then
17923 Error_Msg_N ("null exclusion can only apply to an access type", N);
17924 end if;
17925
17926 Check_Wide_Character_Restriction (Parent_Type, Indic);
17927
17928 -- Avoid deriving parent primitives of underlying record views
17929
17930 Set_Is_Not_Self_Hidden (T);
17931
17932 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17933 Derive_Subps => not Is_Underlying_Record_View (T));
17934
17935 -- Check for special mutably tagged type declarations
17936
17937 if Is_Tagged_Type (Parent_Type)
17938 and then not Error_Posted (T)
17939 then
17940 declare
17941 Actions : List_Id;
17942 CW_Typ : constant Entity_Id := Class_Wide_Type (T);
17943 Root_Class_Typ : constant Entity_Id :=
17944 Class_Wide_Type (Root_Type (Parent_Type));
17945 begin
17946 -- Perform various checks when we are indeed looking at a
17947 -- mutably tagged declaration.
17948
17949 if Present (Root_Class_Typ)
17950 and then Is_Mutably_Tagged_Type (Root_Class_Typ)
17951 then
17952 -- Verify the level of the descendant's declaration is not
17953 -- deeper than the root type since this could cause leaking
17954 -- of the type.
17955
17956 if Scope (Root_Class_Typ) /= Scope (T)
17957 and then Deepest_Type_Access_Level (Root_Class_Typ)
17958 < Deepest_Type_Access_Level (T)
17959 then
17960 Error_Msg_NE
17961 ("descendant of mutably tagged type cannot be deeper than"
17962 & " its root", N, Root_Type (T));
17963
17964 elsif Present (Incomplete_Or_Partial_View (T))
17965 and then Is_Tagged_Type (Incomplete_Or_Partial_View (T))
17966 then
17967 Error_Msg_N
17968 ("descendant of mutably tagged type cannot a have partial"
17969 & " view which is tagged", N);
17970
17971 -- Mutably tagged types cannot have discriminants
17972
17973 elsif Present (Discriminant_Specifications (N)) then
17974 Error_Msg_N
17975 ("descendant of mutably tagged type cannot have"
17976 & " discriminates", N);
17977
17978 elsif Present (Interfaces (T))
17979 and then not Is_Empty_Elmt_List (Interfaces (T))
17980 then
17981 Error_Msg_N
17982 ("descendant of mutably tagged type cannot implement"
17983 & " an interface", N);
17984
17985 -- We have a valid descendant type
17986
17987 else
17988 -- Set inherited attributes
17989
17990 Set_Has_Size_Clause (CW_Typ);
17991 Set_RM_Size (CW_Typ, RM_Size (Root_Class_Typ));
17992 Set_Is_Mutably_Tagged_Type (CW_Typ);
17993
17994 -- Generate a new class-wide equivalent type
17995
17996 Set_Class_Wide_Equivalent_Type (CW_Typ,
17997 Make_CW_Equivalent_Type (CW_Typ, Empty, Actions));
17998
17999 Insert_List_After_And_Analyze (N, Actions);
18000
18001 -- Add a Compile_Time_Error size check at the freeze point
18002
18003 Append_Freeze_Action (T,
18004 Make_CW_Size_Compile_Check (T, Root_Class_Typ));
18005 end if;
18006 end if;
18007 end;
18008 end if;
18009
18010 -- AI-419: The parent type of an explicitly limited derived type must
18011 -- be a limited type or a limited interface.
18012
18013 if Limited_Present (Def) then
18014 Set_Is_Limited_Record (T);
18015
18016 if Is_Interface (T) then
18017 Set_Is_Limited_Interface (T);
18018 end if;
18019
18020 if not Is_Limited_Type (Parent_Type)
18021 and then
18022 (not Is_Interface (Parent_Type)
18023 or else not Is_Limited_Interface (Parent_Type))
18024 then
18025 -- AI05-0096: a derivation in the private part of an instance is
18026 -- legal if the generic formal is untagged limited, and the actual
18027 -- is non-limited.
18028
18029 if Is_Generic_Actual_Type (Parent_Type)
18030 and then In_Private_Part (Current_Scope)
18031 and then
18032 not Is_Tagged_Type
18033 (Generic_Parent_Type (Parent (Parent_Type)))
18034 then
18035 null;
18036
18037 else
18038 Error_Msg_NE
18039 ("parent type& of limited type must be limited",
18040 N, Parent_Type);
18041 end if;
18042 end if;
18043 end if;
18044 end Derived_Type_Declaration;
18045
18046 ------------------------
18047 -- Diagnose_Interface --
18048 ------------------------
18049
18050 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
18051 begin
18052 if not Is_Interface (E) and then E /= Any_Type then
18053 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
18054 end if;
18055 end Diagnose_Interface;
18056
18057 ----------------------------------
18058 -- Enumeration_Type_Declaration --
18059 ----------------------------------
18060
18061 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18062 Ev : Uint;
18063 L : Node_Id;
18064 R_Node : Node_Id;
18065 B_Node : Node_Id;
18066
18067 begin
18068 -- Create identifier node representing lower bound
18069
18070 B_Node := New_Node (N_Identifier, Sloc (Def));
18071 L := First (Literals (Def));
18072 Set_Chars (B_Node, Chars (L));
18073 Set_Entity (B_Node, L);
18074 Set_Etype (B_Node, T);
18075 Set_Is_Static_Expression (B_Node, True);
18076
18077 R_Node := New_Node (N_Range, Sloc (Def));
18078 Set_Low_Bound (R_Node, B_Node);
18079
18080 Mutate_Ekind (T, E_Enumeration_Type);
18081 Set_First_Literal (T, L);
18082 Set_Etype (T, T);
18083 Set_Is_Constrained (T);
18084
18085 Ev := Uint_0;
18086
18087 -- Loop through literals of enumeration type setting pos and rep values
18088 -- except that if the Ekind is already set, then it means the literal
18089 -- was already constructed (case of a derived type declaration and we
18090 -- should not disturb the Pos and Rep values.
18091
18092 while Present (L) loop
18093 if Ekind (L) /= E_Enumeration_Literal then
18094 Mutate_Ekind (L, E_Enumeration_Literal);
18095 Set_Is_Not_Self_Hidden (L);
18096 Set_Enumeration_Pos (L, Ev);
18097 Set_Enumeration_Rep (L, Ev);
18098 Set_Is_Known_Valid (L, True);
18099 end if;
18100
18101 Set_Etype (L, T);
18102 New_Overloaded_Entity (L);
18103 Generate_Definition (L);
18104 Set_Convention (L, Convention_Intrinsic);
18105
18106 -- Case of character literal
18107
18108 if Nkind (L) = N_Defining_Character_Literal then
18109 Set_Is_Character_Type (T, True);
18110
18111 -- Check violation of No_Wide_Characters
18112
18113 if Restriction_Check_Required (No_Wide_Characters) then
18114 Get_Name_String (Chars (L));
18115
18116 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
18117 Check_Restriction (No_Wide_Characters, L);
18118 end if;
18119 end if;
18120 end if;
18121
18122 Ev := Ev + 1;
18123 Next (L);
18124 end loop;
18125
18126 -- Now create a node representing upper bound
18127
18128 B_Node := New_Node (N_Identifier, Sloc (Def));
18129 Set_Chars (B_Node, Chars (Last (Literals (Def))));
18130 Set_Entity (B_Node, Last (Literals (Def)));
18131 Set_Etype (B_Node, T);
18132 Set_Is_Static_Expression (B_Node, True);
18133
18134 Set_High_Bound (R_Node, B_Node);
18135
18136 -- Initialize various fields of the type. Some of this information
18137 -- may be overwritten later through rep. clauses.
18138
18139 Set_Scalar_Range (T, R_Node);
18140 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
18141 Set_Enum_Esize (T);
18142 Set_Enum_Pos_To_Rep (T, Empty);
18143
18144 -- Set Discard_Names if configuration pragma set, or if there is
18145 -- a parameterless pragma in the current declarative region
18146
18147 if Global_Discard_Names or else Discard_Names (Scope (T)) then
18148 Set_Discard_Names (T);
18149 end if;
18150
18151 -- Process end label if there is one
18152
18153 if Present (Def) then
18154 Process_End_Label (Def, 'e', T);
18155 end if;
18156 end Enumeration_Type_Declaration;
18157
18158 ---------------------------------
18159 -- Expand_To_Stored_Constraint --
18160 ---------------------------------
18161
18162 function Expand_To_Stored_Constraint
18163 (Typ : Entity_Id;
18164 Constraint : Elist_Id) return Elist_Id
18165 is
18166 Explicitly_Discriminated_Type : Entity_Id;
18167 Expansion : Elist_Id;
18168 Discriminant : Entity_Id;
18169
18170 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
18171 -- Find the nearest type that actually specifies discriminants
18172
18173 ---------------------------------
18174 -- Type_With_Explicit_Discrims --
18175 ---------------------------------
18176
18177 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
18178 Typ : constant E := Base_Type (Id);
18179
18180 begin
18181 if Ekind (Typ) in Incomplete_Or_Private_Kind then
18182 if Present (Full_View (Typ)) then
18183 return Type_With_Explicit_Discrims (Full_View (Typ));
18184 end if;
18185
18186 else
18187 if Has_Discriminants (Typ) then
18188 return Typ;
18189 end if;
18190 end if;
18191
18192 if Etype (Typ) = Typ then
18193 return Empty;
18194 elsif Has_Discriminants (Typ) then
18195 return Typ;
18196 else
18197 return Type_With_Explicit_Discrims (Etype (Typ));
18198 end if;
18199
18200 end Type_With_Explicit_Discrims;
18201
18202 -- Start of processing for Expand_To_Stored_Constraint
18203
18204 begin
18205 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
18206 return No_Elist;
18207 end if;
18208
18209 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
18210
18211 if No (Explicitly_Discriminated_Type) then
18212 return No_Elist;
18213 end if;
18214
18215 Expansion := New_Elmt_List;
18216
18217 Discriminant :=
18218 First_Stored_Discriminant (Explicitly_Discriminated_Type);
18219 while Present (Discriminant) loop
18220 Append_Elmt
18221 (Get_Discriminant_Value
18222 (Discriminant, Explicitly_Discriminated_Type, Constraint),
18223 To => Expansion);
18224 Next_Stored_Discriminant (Discriminant);
18225 end loop;
18226
18227 return Expansion;
18228 end Expand_To_Stored_Constraint;
18229
18230 ---------------------------
18231 -- Find_Hidden_Interface --
18232 ---------------------------
18233
18234 function Find_Hidden_Interface
18235 (Src : Elist_Id;
18236 Dest : Elist_Id) return Entity_Id
18237 is
18238 Iface : Entity_Id;
18239 Iface_Elmt : Elmt_Id;
18240
18241 begin
18242 if Present (Src) and then Present (Dest) then
18243 Iface_Elmt := First_Elmt (Src);
18244 while Present (Iface_Elmt) loop
18245 Iface := Node (Iface_Elmt);
18246
18247 if Is_Interface (Iface)
18248 and then not Contain_Interface (Iface, Dest)
18249 then
18250 return Iface;
18251 end if;
18252
18253 Next_Elmt (Iface_Elmt);
18254 end loop;
18255 end if;
18256
18257 return Empty;
18258 end Find_Hidden_Interface;
18259
18260 --------------------
18261 -- Find_Type_Name --
18262 --------------------
18263
18264 function Find_Type_Name (N : Node_Id) return Entity_Id is
18265 Id : constant Entity_Id := Defining_Identifier (N);
18266 New_Id : Entity_Id;
18267 Prev : Entity_Id;
18268 Prev_Par : Node_Id;
18269
18270 procedure Check_Duplicate_Aspects;
18271 -- Check that aspects specified in a completion have not been specified
18272 -- already in the partial view.
18273
18274 procedure Tag_Mismatch;
18275 -- Diagnose a tagged partial view whose full view is untagged. We post
18276 -- the message on the full view, with a reference to the previous
18277 -- partial view. The partial view can be private or incomplete, and
18278 -- these are handled in a different manner, so we determine the position
18279 -- of the error message from the respective slocs of both.
18280
18281 -----------------------------
18282 -- Check_Duplicate_Aspects --
18283 -----------------------------
18284
18285 procedure Check_Duplicate_Aspects is
18286 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
18287 -- Return the corresponding aspect of the partial view which matches
18288 -- the aspect id of Asp. Return Empty is no such aspect exists.
18289
18290 -----------------------------
18291 -- Get_Partial_View_Aspect --
18292 -----------------------------
18293
18294 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
18295 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
18296 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
18297 Prev_Asp : Node_Id;
18298
18299 begin
18300 Prev_Asp := First (Prev_Asps);
18301 while Present (Prev_Asp) loop
18302 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
18303 return Prev_Asp;
18304 end if;
18305
18306 Next (Prev_Asp);
18307 end loop;
18308
18309 return Empty;
18310 end Get_Partial_View_Aspect;
18311
18312 -- Local variables
18313
18314 Full_Asps : constant List_Id := Aspect_Specifications (N);
18315 Full_Asp : Node_Id;
18316 Part_Asp : Node_Id;
18317
18318 -- Start of processing for Check_Duplicate_Aspects
18319
18320 begin
18321 Full_Asp := First (Full_Asps);
18322 while Present (Full_Asp) loop
18323 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
18324
18325 -- An aspect and its class-wide counterpart are two distinct
18326 -- aspects and may apply to both views of an entity.
18327
18328 if Present (Part_Asp)
18329 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
18330 then
18331 Error_Msg_N
18332 ("aspect already specified in private declaration", Full_Asp);
18333
18334 Remove (Full_Asp);
18335 return;
18336 end if;
18337
18338 if Has_Discriminants (Prev)
18339 and then not Has_Unknown_Discriminants (Prev)
18340 and then Get_Aspect_Id (Full_Asp) =
18341 Aspect_Implicit_Dereference
18342 then
18343 Error_Msg_N
18344 ("cannot specify aspect if partial view has known "
18345 & "discriminants", Full_Asp);
18346 end if;
18347
18348 Next (Full_Asp);
18349 end loop;
18350 end Check_Duplicate_Aspects;
18351
18352 ------------------
18353 -- Tag_Mismatch --
18354 ------------------
18355
18356 procedure Tag_Mismatch is
18357 begin
18358 if Sloc (Prev) < Sloc (Id) then
18359 if Ada_Version >= Ada_2012
18360 and then Nkind (N) = N_Private_Type_Declaration
18361 then
18362 Error_Msg_NE
18363 ("declaration of private } must be a tagged type", Id, Prev);
18364 else
18365 Error_Msg_NE
18366 ("full declaration of } must be a tagged type", Id, Prev);
18367 end if;
18368
18369 else
18370 if Ada_Version >= Ada_2012
18371 and then Nkind (N) = N_Private_Type_Declaration
18372 then
18373 Error_Msg_NE
18374 ("declaration of private } must be a tagged type", Prev, Id);
18375 else
18376 Error_Msg_NE
18377 ("full declaration of } must be a tagged type", Prev, Id);
18378 end if;
18379 end if;
18380 end Tag_Mismatch;
18381
18382 -- Start of processing for Find_Type_Name
18383
18384 begin
18385 -- Find incomplete declaration, if one was given
18386
18387 Prev := Current_Entity_In_Scope (Id);
18388
18389 -- New type declaration
18390
18391 if No (Prev) then
18392 Enter_Name (Id);
18393 return Id;
18394
18395 -- Previous declaration exists
18396
18397 else
18398 Prev_Par := Parent (Prev);
18399
18400 -- Error if not incomplete/private case except if previous
18401 -- declaration is implicit, etc. Enter_Name will emit error if
18402 -- appropriate.
18403
18404 if not Is_Incomplete_Or_Private_Type (Prev) then
18405 Enter_Name (Id);
18406 New_Id := Id;
18407
18408 -- Check invalid completion of private or incomplete type
18409
18410 elsif Nkind (N) not in N_Full_Type_Declaration
18411 | N_Task_Type_Declaration
18412 | N_Protected_Type_Declaration
18413 and then
18414 (Ada_Version < Ada_2012
18415 or else not Is_Incomplete_Type (Prev)
18416 or else Nkind (N) not in N_Private_Type_Declaration
18417 | N_Private_Extension_Declaration)
18418 then
18419 -- Completion must be a full type declarations (RM 7.3(4))
18420
18421 Error_Msg_Sloc := Sloc (Prev);
18422 Error_Msg_NE ("invalid completion of }", Id, Prev);
18423
18424 -- Set scope of Id to avoid cascaded errors. Entity is never
18425 -- examined again, except when saving globals in generics.
18426
18427 Set_Scope (Id, Current_Scope);
18428 New_Id := Id;
18429
18430 -- If this is a repeated incomplete declaration, no further
18431 -- checks are possible.
18432
18433 if Nkind (N) = N_Incomplete_Type_Declaration then
18434 return Prev;
18435 end if;
18436
18437 -- Case of full declaration of incomplete type
18438
18439 elsif Ekind (Prev) = E_Incomplete_Type
18440 and then (Ada_Version < Ada_2012
18441 or else No (Full_View (Prev))
18442 or else not Is_Private_Type (Full_View (Prev)))
18443 then
18444 -- Indicate that the incomplete declaration has a matching full
18445 -- declaration. The defining occurrence of the incomplete
18446 -- declaration remains the visible one, and the procedure
18447 -- Get_Full_View dereferences it whenever the type is used.
18448
18449 if Present (Full_View (Prev)) then
18450 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18451 end if;
18452
18453 Set_Full_View (Prev, Id);
18454 Append_Entity (Id, Current_Scope);
18455 Set_Is_Public (Id, Is_Public (Prev));
18456 Set_Is_Internal (Id);
18457 New_Id := Prev;
18458
18459 -- If the incomplete view is tagged, a class_wide type has been
18460 -- created already. Use it for the private type as well, in order
18461 -- to prevent multiple incompatible class-wide types that may be
18462 -- created for self-referential anonymous access components.
18463
18464 if Is_Tagged_Type (Prev)
18465 and then Present (Class_Wide_Type (Prev))
18466 then
18467 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18468 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18469
18470 -- Type of the class-wide type is the current Id. Previously
18471 -- this was not done for private declarations because of order-
18472 -- of-elaboration issues in the back end, but gigi now handles
18473 -- this properly.
18474
18475 Set_Etype (Class_Wide_Type (Id), Id);
18476 end if;
18477
18478 -- Case of full declaration of private type
18479
18480 else
18481 -- If the private type was a completion of an incomplete type then
18482 -- update Prev to reference the private type
18483
18484 if Ada_Version >= Ada_2012
18485 and then Ekind (Prev) = E_Incomplete_Type
18486 and then Present (Full_View (Prev))
18487 and then Is_Private_Type (Full_View (Prev))
18488 then
18489 Prev := Full_View (Prev);
18490 Prev_Par := Parent (Prev);
18491 end if;
18492
18493 if Nkind (N) = N_Full_Type_Declaration
18494 and then Nkind (Type_Definition (N)) in
18495 N_Record_Definition | N_Derived_Type_Definition
18496 and then Interface_Present (Type_Definition (N))
18497 then
18498 Error_Msg_N
18499 ("completion of private type cannot be an interface", N);
18500 end if;
18501
18502 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18503 if Etype (Prev) /= Prev then
18504
18505 -- Prev is a private subtype or a derived type, and needs
18506 -- no completion.
18507
18508 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18509 New_Id := Id;
18510
18511 elsif Ekind (Prev) = E_Private_Type
18512 and then Nkind (N) in N_Task_Type_Declaration
18513 | N_Protected_Type_Declaration
18514 then
18515 Error_Msg_N
18516 ("completion of nonlimited type cannot be limited", N);
18517
18518 elsif Ekind (Prev) = E_Record_Type_With_Private
18519 and then Nkind (N) in N_Task_Type_Declaration
18520 | N_Protected_Type_Declaration
18521 then
18522 if not Is_Limited_Record (Prev) then
18523 Error_Msg_N
18524 ("completion of nonlimited type cannot be limited", N);
18525
18526 elsif No (Interface_List (N)) then
18527 Error_Msg_N
18528 ("completion of tagged private type must be tagged",
18529 N);
18530 end if;
18531 end if;
18532
18533 -- Ada 2005 (AI-251): Private extension declaration of a task
18534 -- type or a protected type. This case arises when covering
18535 -- interface types.
18536
18537 elsif Nkind (N) in N_Task_Type_Declaration
18538 | N_Protected_Type_Declaration
18539 then
18540 null;
18541
18542 elsif Nkind (N) /= N_Full_Type_Declaration
18543 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18544 then
18545 Error_Msg_N
18546 ("full view of private extension must be an extension", N);
18547
18548 elsif not Abstract_Present (Parent (Prev))
18549 and then Abstract_Present (Type_Definition (N))
18550 then
18551 Error_Msg_N
18552 ("full view of non-abstract extension cannot be abstract", N);
18553 end if;
18554
18555 if not In_Private_Part (Current_Scope) then
18556 Error_Msg_N
18557 ("declaration of full view must appear in private part", N);
18558 end if;
18559
18560 if Ada_Version >= Ada_2012 then
18561 Check_Duplicate_Aspects;
18562 end if;
18563
18564 Copy_And_Swap (Prev, Id);
18565 Set_Has_Private_Declaration (Prev);
18566 Set_Has_Private_Declaration (Id);
18567
18568 -- AI12-0133: Indicate whether we have a partial view with
18569 -- unknown discriminants, in which case initialization of objects
18570 -- of the type do not receive an invariant check.
18571
18572 Set_Partial_View_Has_Unknown_Discr
18573 (Prev, Has_Unknown_Discriminants (Id));
18574
18575 -- Preserve aspect and iterator flags that may have been set on
18576 -- the partial view.
18577
18578 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18579 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18580
18581 -- If no error, propagate freeze_node from private to full view.
18582 -- It may have been generated for an early operational item.
18583
18584 if Present (Freeze_Node (Id))
18585 and then Serious_Errors_Detected = 0
18586 and then No (Full_View (Id))
18587 then
18588 Set_Freeze_Node (Prev, Freeze_Node (Id));
18589 Set_Freeze_Node (Id, Empty);
18590 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18591 end if;
18592
18593 Set_Full_View (Id, Prev);
18594 New_Id := Prev;
18595 end if;
18596
18597 -- Verify that full declaration conforms to partial one
18598
18599 if Is_Incomplete_Or_Private_Type (Prev)
18600 and then Present (Discriminant_Specifications (Prev_Par))
18601 then
18602 if Present (Discriminant_Specifications (N)) then
18603 if Ekind (Prev) = E_Incomplete_Type then
18604 Check_Discriminant_Conformance (N, Prev, Prev);
18605 else
18606 Check_Discriminant_Conformance (N, Prev, Id);
18607 end if;
18608
18609 else
18610 Error_Msg_N
18611 ("missing discriminants in full type declaration", N);
18612
18613 -- To avoid cascaded errors on subsequent use, share the
18614 -- discriminants of the partial view.
18615
18616 Set_Discriminant_Specifications (N,
18617 Discriminant_Specifications (Prev_Par));
18618 end if;
18619 end if;
18620
18621 -- A prior untagged partial view can have an associated class-wide
18622 -- type due to use of the class attribute, and in this case the full
18623 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18624 -- of incomplete tagged declarations, but we check for it.
18625
18626 if Is_Type (Prev)
18627 and then (Is_Tagged_Type (Prev)
18628 or else Present (Class_Wide_Type (Prev)))
18629 then
18630 -- Ada 2012 (AI05-0162): A private type may be the completion of
18631 -- an incomplete type.
18632
18633 if Ada_Version >= Ada_2012
18634 and then Is_Incomplete_Type (Prev)
18635 and then Nkind (N) in N_Private_Type_Declaration
18636 | N_Private_Extension_Declaration
18637 then
18638 -- No need to check private extensions since they are tagged
18639
18640 if Nkind (N) = N_Private_Type_Declaration
18641 and then not Tagged_Present (N)
18642 then
18643 Tag_Mismatch;
18644 end if;
18645
18646 -- The full declaration is either a tagged type (including
18647 -- a synchronized type that implements interfaces) or a
18648 -- type extension, otherwise this is an error.
18649
18650 elsif Nkind (N) in N_Task_Type_Declaration
18651 | N_Protected_Type_Declaration
18652 then
18653 if No (Interface_List (N)) and then not Error_Posted (N) then
18654 Tag_Mismatch;
18655 end if;
18656
18657 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18658
18659 -- Indicate that the previous declaration (tagged incomplete
18660 -- or private declaration) requires the same on the full one.
18661
18662 if not Tagged_Present (Type_Definition (N)) then
18663 Tag_Mismatch;
18664 Set_Is_Tagged_Type (Id);
18665 end if;
18666
18667 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18668 if No (Record_Extension_Part (Type_Definition (N))) then
18669 Error_Msg_NE
18670 ("full declaration of } must be a record extension",
18671 Prev, Id);
18672
18673 -- Set some attributes to produce a usable full view
18674
18675 Set_Is_Tagged_Type (Id);
18676 end if;
18677
18678 else
18679 Tag_Mismatch;
18680 end if;
18681 end if;
18682
18683 if Present (Prev)
18684 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18685 and then Present (Premature_Use (Parent (Prev)))
18686 then
18687 Error_Msg_Sloc := Sloc (N);
18688 Error_Msg_N
18689 ("\full declaration #", Premature_Use (Parent (Prev)));
18690 end if;
18691
18692 return New_Id;
18693 end if;
18694 end Find_Type_Name;
18695
18696 -------------------------
18697 -- Find_Type_Of_Object --
18698 -------------------------
18699
18700 function Find_Type_Of_Object
18701 (Obj_Def : Node_Id;
18702 Related_Nod : Node_Id) return Entity_Id
18703 is
18704 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18705 P : Node_Id := Parent (Obj_Def);
18706 T : Entity_Id;
18707 Nam : Name_Id;
18708
18709 begin
18710 -- If the parent is a component_definition node we climb to the
18711 -- component_declaration node.
18712
18713 if Nkind (P) = N_Component_Definition then
18714 P := Parent (P);
18715 end if;
18716
18717 -- Case of an anonymous array subtype
18718
18719 if Def_Kind in N_Array_Type_Definition then
18720 T := Empty;
18721 Array_Type_Declaration (T, Obj_Def);
18722
18723 -- Create an explicit subtype whenever possible
18724
18725 elsif Nkind (P) /= N_Component_Declaration
18726 and then Def_Kind = N_Subtype_Indication
18727 then
18728 -- Base name of subtype on object name, which will be unique in
18729 -- the current scope.
18730
18731 -- If this is a duplicate declaration, return base type, to avoid
18732 -- generating duplicate anonymous types.
18733
18734 if Error_Posted (P) then
18735 Analyze (Subtype_Mark (Obj_Def));
18736 return Entity (Subtype_Mark (Obj_Def));
18737 end if;
18738
18739 Nam :=
18740 New_External_Name
18741 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18742
18743 T := Make_Defining_Identifier (Sloc (P), Nam);
18744
18745 -- If In_Spec_Expression, for example within a pre/postcondition,
18746 -- provide enough information for use of the subtype without
18747 -- depending on full analysis and freezing, which will happen when
18748 -- building the corresponding subprogram.
18749
18750 if In_Spec_Expression then
18751 Analyze (Subtype_Mark (Obj_Def));
18752
18753 declare
18754 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18755 New_Def : constant Node_Id := New_Copy_Tree (Obj_Def);
18756 Decl : constant Node_Id :=
18757 Make_Subtype_Declaration (Sloc (P),
18758 Defining_Identifier => T,
18759 Subtype_Indication => New_Def);
18760
18761 begin
18762 Set_Etype (T, Base_T);
18763 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18764 Set_Parent (T, Decl);
18765 Set_Scope (T, Current_Scope);
18766
18767 if Ekind (T) = E_Array_Subtype then
18768 Constrain_Array (T, New_Def, Related_Nod, T, 'P');
18769
18770 elsif Ekind (T) = E_Record_Subtype then
18771 Set_First_Entity (T, First_Entity (Base_T));
18772 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18773 Set_Is_Constrained (T);
18774 end if;
18775
18776 Insert_Before (Related_Nod, Decl);
18777 end;
18778
18779 return T;
18780 end if;
18781
18782 -- When generating code, insert subtype declaration ahead of
18783 -- declaration that generated it. Similar behavior required under
18784 -- preanalysis (including strict preanalysis) to perform the
18785 -- minimum decoration, and avoid reporting spurious errors.
18786
18787 Insert_Action (Obj_Def,
18788 Make_Subtype_Declaration (Sloc (P),
18789 Defining_Identifier => T,
18790 Subtype_Indication => Relocate_Node (Obj_Def)));
18791
18792 -- This subtype may need freezing, and this will not be done
18793 -- automatically if the object declaration is not in declarative
18794 -- part. Since this is an object declaration, the type cannot always
18795 -- be frozen here. Deferred constants do not freeze their type
18796 -- (which often enough will be private).
18797
18798 if Nkind (P) = N_Object_Declaration
18799 and then Constant_Present (P)
18800 and then No (Expression (P))
18801 then
18802 null;
18803
18804 -- Here we freeze the base type of object type to catch premature use
18805 -- of discriminated private type without a full view.
18806
18807 else
18808 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18809 end if;
18810
18811 -- Ada 2005 AI-406: the object definition in an object declaration
18812 -- can be an access definition.
18813
18814 elsif Def_Kind = N_Access_Definition then
18815 T := Access_Definition (Related_Nod, Obj_Def);
18816
18817 Set_Is_Local_Anonymous_Access
18818 (T, Ada_Version < Ada_2012
18819 or else Nkind (P) /= N_Object_Declaration
18820 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18821
18822 -- Otherwise, the object definition is just a subtype_mark
18823
18824 else
18825 T := Process_Subtype (Obj_Def, Related_Nod);
18826 end if;
18827
18828 return T;
18829 end Find_Type_Of_Object;
18830
18831 --------------------------------
18832 -- Find_Type_Of_Subtype_Indic --
18833 --------------------------------
18834
18835 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18836 Typ : Entity_Id;
18837
18838 begin
18839 -- Case of subtype mark with a constraint
18840
18841 if Nkind (S) = N_Subtype_Indication then
18842 Find_Type (Subtype_Mark (S));
18843 Typ := Entity (Subtype_Mark (S));
18844
18845 if not
18846 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18847 then
18848 Error_Msg_N
18849 ("incorrect constraint for this kind of type", Constraint (S));
18850 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18851 end if;
18852
18853 -- Otherwise we have a subtype mark without a constraint
18854
18855 elsif Error_Posted (S) then
18856 -- Don't rewrite if S is Empty or Error
18857 if S > Empty_Or_Error then
18858 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18859 end if;
18860 return Any_Type;
18861
18862 else
18863 Find_Type (S);
18864 Typ := Entity (S);
18865 end if;
18866
18867 return Typ;
18868 end Find_Type_Of_Subtype_Indic;
18869
18870 -------------------------------------
18871 -- Floating_Point_Type_Declaration --
18872 -------------------------------------
18873
18874 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18875 Digs : constant Node_Id := Digits_Expression (Def);
18876 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18877 Digs_Val : Uint;
18878 Base_Typ : Entity_Id;
18879 Implicit_Base : Entity_Id;
18880
18881 function Can_Derive_From (E : Entity_Id) return Boolean;
18882 -- Find if given digits value, and possibly a specified range, allows
18883 -- derivation from specified type
18884
18885 procedure Convert_Bound (B : Node_Id);
18886 -- If specified, the bounds must be static but may be of different
18887 -- types. They must be converted into machine numbers of the base type,
18888 -- in accordance with RM 4.9(38).
18889
18890 function Find_Base_Type return Entity_Id;
18891 -- Find a predefined base type that Def can derive from, or generate
18892 -- an error and substitute Long_Long_Float if none exists.
18893
18894 ---------------------
18895 -- Can_Derive_From --
18896 ---------------------
18897
18898 function Can_Derive_From (E : Entity_Id) return Boolean is
18899 Spec : constant Entity_Id := Real_Range_Specification (Def);
18900
18901 begin
18902 -- Check specified "digits" constraint
18903
18904 if Digs_Val > Digits_Value (E) then
18905 return False;
18906 end if;
18907
18908 -- Check for matching range, if specified
18909
18910 if Present (Spec) then
18911 if Expr_Value_R (Type_Low_Bound (E)) >
18912 Expr_Value_R (Low_Bound (Spec))
18913 then
18914 return False;
18915 end if;
18916
18917 if Expr_Value_R (Type_High_Bound (E)) <
18918 Expr_Value_R (High_Bound (Spec))
18919 then
18920 return False;
18921 end if;
18922 end if;
18923
18924 return True;
18925 end Can_Derive_From;
18926
18927 -------------------
18928 -- Convert_Bound --
18929 --------------------
18930
18931 procedure Convert_Bound (B : Node_Id) is
18932 begin
18933 -- If the bound is not a literal it can only be static if it is
18934 -- a static constant, possibly of a specified type.
18935
18936 if Is_Entity_Name (B)
18937 and then Ekind (Entity (B)) = E_Constant
18938 then
18939 Rewrite (B, Constant_Value (Entity (B)));
18940 end if;
18941
18942 if Nkind (B) = N_Real_Literal then
18943 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18944 Set_Is_Machine_Number (B);
18945 Set_Etype (B, Base_Typ);
18946 end if;
18947 end Convert_Bound;
18948
18949 --------------------
18950 -- Find_Base_Type --
18951 --------------------
18952
18953 function Find_Base_Type return Entity_Id is
18954 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18955
18956 begin
18957 -- Iterate over the predefined types in order, returning the first
18958 -- one that Def can derive from.
18959
18960 while Present (Choice) loop
18961 if Can_Derive_From (Node (Choice)) then
18962 return Node (Choice);
18963 end if;
18964
18965 Next_Elmt (Choice);
18966 end loop;
18967
18968 -- If we can't derive from any existing type, use Long_Long_Float
18969 -- and give appropriate message explaining the problem.
18970
18971 if Digs_Val > Max_Digs_Val then
18972 -- It might be the case that there is a type with the requested
18973 -- range, just not the combination of digits and range.
18974
18975 Error_Msg_N
18976 ("no predefined type has requested range and precision",
18977 Real_Range_Specification (Def));
18978
18979 else
18980 Error_Msg_N
18981 ("range too large for any predefined type",
18982 Real_Range_Specification (Def));
18983 end if;
18984
18985 return Standard_Long_Long_Float;
18986 end Find_Base_Type;
18987
18988 -- Start of processing for Floating_Point_Type_Declaration
18989
18990 begin
18991 Check_Restriction (No_Floating_Point, Def);
18992
18993 -- Create an implicit base type
18994
18995 Implicit_Base :=
18996 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18997
18998 -- Analyze and verify digits value
18999
19000 Analyze_And_Resolve (Digs, Any_Integer);
19001 Check_Digits_Expression (Digs);
19002 Digs_Val := Expr_Value (Digs);
19003
19004 -- Process possible range spec and find correct type to derive from
19005
19006 Process_Real_Range_Specification (Def);
19007
19008 -- Check that requested number of digits is not too high.
19009
19010 if Digs_Val > Max_Digs_Val then
19011
19012 -- The check for Max_Base_Digits may be somewhat expensive, as it
19013 -- requires reading System, so only do it when necessary.
19014
19015 declare
19016 Max_Base_Digits : constant Uint :=
19017 Expr_Value
19018 (Expression
19019 (Parent (RTE (RE_Max_Base_Digits))));
19020
19021 begin
19022 if Digs_Val > Max_Base_Digits then
19023 Error_Msg_Uint_1 := Max_Base_Digits;
19024 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
19025
19026 elsif No (Real_Range_Specification (Def)) then
19027 Error_Msg_Uint_1 := Max_Digs_Val;
19028 Error_Msg_N ("types with more than ^ digits need range spec "
19029 & "(RM 3.5.7(6))", Digs);
19030 end if;
19031 end;
19032 end if;
19033
19034 -- Find a suitable type to derive from or complain and use a substitute
19035
19036 Base_Typ := Find_Base_Type;
19037
19038 -- If there are bounds given in the declaration use them as the bounds
19039 -- of the type, otherwise use the bounds of the predefined base type
19040 -- that was chosen based on the Digits value.
19041
19042 if Present (Real_Range_Specification (Def)) then
19043 Set_Scalar_Range (T, Real_Range_Specification (Def));
19044 Set_Is_Constrained (T);
19045
19046 Convert_Bound (Type_Low_Bound (T));
19047 Convert_Bound (Type_High_Bound (T));
19048
19049 else
19050 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
19051 end if;
19052
19053 -- Complete definition of implicit base and declared first subtype. The
19054 -- inheritance of the rep item chain ensures that SPARK-related pragmas
19055 -- are not clobbered when the floating point type acts as a full view of
19056 -- a private type.
19057
19058 Set_Etype (Implicit_Base, Base_Typ);
19059 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
19060 Set_Size_Info (Implicit_Base, Base_Typ);
19061 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19062 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19063 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
19064 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
19065
19066 Mutate_Ekind (T, E_Floating_Point_Subtype);
19067 Set_Etype (T, Implicit_Base);
19068 Set_Size_Info (T, Implicit_Base);
19069 Set_RM_Size (T, RM_Size (Implicit_Base));
19070 Inherit_Rep_Item_Chain (T, Implicit_Base);
19071
19072 if Digs_Val >= Uint_1 then
19073 Set_Digits_Value (T, Digs_Val);
19074 else
19075 pragma Assert (Serious_Errors_Detected > 0); null;
19076 end if;
19077 end Floating_Point_Type_Declaration;
19078
19079 ----------------------------
19080 -- Get_Discriminant_Value --
19081 ----------------------------
19082
19083 -- This is the situation:
19084
19085 -- There is a non-derived type
19086
19087 -- type T0 (Dx, Dy, Dz...)
19088
19089 -- There are zero or more levels of derivation, with each derivation
19090 -- either purely inheriting the discriminants, or defining its own.
19091
19092 -- type Ti is new Ti-1
19093 -- or
19094 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
19095 -- or
19096 -- subtype Ti is ...
19097
19098 -- The subtype issue is avoided by the use of Original_Record_Component,
19099 -- and the fact that derived subtypes also derive the constraints.
19100
19101 -- This chain leads back from
19102
19103 -- Typ_For_Constraint
19104
19105 -- Typ_For_Constraint has discriminants, and the value for each
19106 -- discriminant is given by its corresponding Elmt of Constraints.
19107
19108 -- Discriminant is some discriminant in this hierarchy
19109
19110 -- We need to return its value
19111
19112 -- We do this by recursively searching each level, and looking for
19113 -- Discriminant. Once we get to the bottom, we start backing up
19114 -- returning the value for it which may in turn be a discriminant
19115 -- further up, so on the backup we continue the substitution.
19116
19117 function Get_Discriminant_Value
19118 (Discriminant : Entity_Id;
19119 Typ_For_Constraint : Entity_Id;
19120 Constraint : Elist_Id) return Node_Id
19121 is
19122 function Root_Corresponding_Discriminant
19123 (Discr : Entity_Id) return Entity_Id;
19124 -- Given a discriminant, traverse the chain of inherited discriminants
19125 -- and return the topmost discriminant.
19126
19127 function Search_Derivation_Levels
19128 (Ti : Entity_Id;
19129 Discrim_Values : Elist_Id;
19130 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
19131 -- This is the routine that performs the recursive search of levels
19132 -- as described above.
19133
19134 -------------------------------------
19135 -- Root_Corresponding_Discriminant --
19136 -------------------------------------
19137
19138 function Root_Corresponding_Discriminant
19139 (Discr : Entity_Id) return Entity_Id
19140 is
19141 D : Entity_Id;
19142
19143 begin
19144 D := Discr;
19145 while Present (Corresponding_Discriminant (D)) loop
19146 D := Corresponding_Discriminant (D);
19147 end loop;
19148
19149 return D;
19150 end Root_Corresponding_Discriminant;
19151
19152 ------------------------------
19153 -- Search_Derivation_Levels --
19154 ------------------------------
19155
19156 function Search_Derivation_Levels
19157 (Ti : Entity_Id;
19158 Discrim_Values : Elist_Id;
19159 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
19160 is
19161 Assoc : Elmt_Id;
19162 Disc : Entity_Id;
19163 Result : Node_Or_Entity_Id;
19164 Result_Entity : Node_Id;
19165
19166 begin
19167 -- If inappropriate type, return Error, this happens only in
19168 -- cascaded error situations, and we want to avoid a blow up.
19169
19170 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
19171 return Error;
19172 end if;
19173
19174 -- Look deeper if possible. Use Stored_Constraints only for
19175 -- untagged types. For tagged types use the given constraint.
19176 -- This asymmetry needs explanation???
19177
19178 if not Stored_Discrim_Values
19179 and then Present (Stored_Constraint (Ti))
19180 and then not Is_Tagged_Type (Ti)
19181 then
19182 Result :=
19183 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
19184
19185 else
19186 declare
19187 Td : Entity_Id := Etype (Ti);
19188
19189 begin
19190 -- If the parent type is private, the full view may include
19191 -- renamed discriminants, and it is those stored values that
19192 -- may be needed (the partial view never has more information
19193 -- than the full view).
19194
19195 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
19196 Td := Full_View (Td);
19197 end if;
19198
19199 if Td = Ti then
19200 Result := Discriminant;
19201
19202 else
19203 if Present (Stored_Constraint (Ti)) then
19204 Result :=
19205 Search_Derivation_Levels
19206 (Td, Stored_Constraint (Ti), True);
19207 else
19208 Result :=
19209 Search_Derivation_Levels
19210 (Td, Discrim_Values, Stored_Discrim_Values);
19211 end if;
19212 end if;
19213 end;
19214 end if;
19215
19216 -- Extra underlying places to search, if not found above. For
19217 -- concurrent types, the relevant discriminant appears in the
19218 -- corresponding record. For a type derived from a private type
19219 -- without discriminant, the full view inherits the discriminants
19220 -- of the full view of the parent.
19221
19222 if Result = Discriminant then
19223 if Is_Concurrent_Type (Ti)
19224 and then Present (Corresponding_Record_Type (Ti))
19225 then
19226 Result :=
19227 Search_Derivation_Levels (
19228 Corresponding_Record_Type (Ti),
19229 Discrim_Values,
19230 Stored_Discrim_Values);
19231
19232 elsif Is_Private_Type (Ti)
19233 and then not Has_Discriminants (Ti)
19234 and then Present (Full_View (Ti))
19235 and then Etype (Full_View (Ti)) /= Ti
19236 then
19237 Result :=
19238 Search_Derivation_Levels (
19239 Full_View (Ti),
19240 Discrim_Values,
19241 Stored_Discrim_Values);
19242 end if;
19243 end if;
19244
19245 -- If Result is not a (reference to a) discriminant, return it,
19246 -- otherwise set Result_Entity to the discriminant.
19247
19248 if Nkind (Result) = N_Defining_Identifier then
19249 pragma Assert (Result = Discriminant);
19250 Result_Entity := Result;
19251
19252 else
19253 if not Denotes_Discriminant (Result) then
19254 return Result;
19255 end if;
19256
19257 Result_Entity := Entity (Result);
19258 end if;
19259
19260 -- See if this level of derivation actually has discriminants because
19261 -- tagged derivations can add them, hence the lower levels need not
19262 -- have any.
19263
19264 if not Has_Discriminants (Ti) then
19265 return Result;
19266 end if;
19267
19268 -- Scan Ti's discriminants for Result_Entity, and return its
19269 -- corresponding value, if any.
19270
19271 Result_Entity := Original_Record_Component (Result_Entity);
19272
19273 Assoc := First_Elmt (Discrim_Values);
19274
19275 if Stored_Discrim_Values then
19276 Disc := First_Stored_Discriminant (Ti);
19277 else
19278 Disc := First_Discriminant (Ti);
19279 end if;
19280
19281 while Present (Disc) loop
19282
19283 -- If no further associations return the discriminant, value will
19284 -- be found on the second pass.
19285
19286 if No (Assoc) then
19287 return Result;
19288 end if;
19289
19290 if Original_Record_Component (Disc) = Result_Entity then
19291 return Node (Assoc);
19292 end if;
19293
19294 Next_Elmt (Assoc);
19295
19296 if Stored_Discrim_Values then
19297 Next_Stored_Discriminant (Disc);
19298 else
19299 Next_Discriminant (Disc);
19300 end if;
19301 end loop;
19302
19303 -- Could not find it
19304
19305 return Result;
19306 end Search_Derivation_Levels;
19307
19308 -- Local Variables
19309
19310 Result : Node_Or_Entity_Id;
19311
19312 -- Start of processing for Get_Discriminant_Value
19313
19314 begin
19315 -- ??? This routine is a gigantic mess and will be deleted. For the
19316 -- time being just test for the trivial case before calling recurse.
19317
19318 -- We are now celebrating the 20th anniversary of this comment!
19319
19320 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
19321 declare
19322 D : Entity_Id;
19323 E : Elmt_Id;
19324
19325 begin
19326 D := First_Discriminant (Typ_For_Constraint);
19327 E := First_Elmt (Constraint);
19328 while Present (D) loop
19329 if Chars (D) = Chars (Discriminant) then
19330 return Node (E);
19331 end if;
19332
19333 Next_Discriminant (D);
19334 Next_Elmt (E);
19335 end loop;
19336 end;
19337 end if;
19338
19339 Result := Search_Derivation_Levels
19340 (Typ_For_Constraint, Constraint, False);
19341
19342 -- ??? hack to disappear when this routine is gone
19343
19344 if Nkind (Result) = N_Defining_Identifier then
19345 declare
19346 D : Entity_Id;
19347 E : Elmt_Id;
19348
19349 begin
19350 D := First_Discriminant (Typ_For_Constraint);
19351 E := First_Elmt (Constraint);
19352 while Present (D) loop
19353 if Root_Corresponding_Discriminant (D) = Discriminant then
19354 return Node (E);
19355 end if;
19356
19357 Next_Discriminant (D);
19358 Next_Elmt (E);
19359 end loop;
19360 end;
19361 end if;
19362
19363 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19364 return Result;
19365 end Get_Discriminant_Value;
19366
19367 --------------------------
19368 -- Has_Range_Constraint --
19369 --------------------------
19370
19371 function Has_Range_Constraint (N : Node_Id) return Boolean is
19372 C : constant Node_Id := Constraint (N);
19373
19374 begin
19375 if Nkind (C) = N_Range_Constraint then
19376 return True;
19377
19378 elsif Nkind (C) = N_Digits_Constraint then
19379 return
19380 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19381 or else Present (Range_Constraint (C));
19382
19383 elsif Nkind (C) = N_Delta_Constraint then
19384 return Present (Range_Constraint (C));
19385
19386 else
19387 return False;
19388 end if;
19389 end Has_Range_Constraint;
19390
19391 ------------------------
19392 -- Inherit_Components --
19393 ------------------------
19394
19395 function Inherit_Components
19396 (N : Node_Id;
19397 Parent_Base : Entity_Id;
19398 Derived_Base : Entity_Id;
19399 Is_Tagged : Boolean;
19400 Inherit_Discr : Boolean;
19401 Discs : Elist_Id) return Elist_Id
19402 is
19403 Assoc_List : constant Elist_Id := New_Elmt_List;
19404
19405 procedure Inherit_Component
19406 (Old_C : Entity_Id;
19407 Plain_Discrim : Boolean := False;
19408 Stored_Discrim : Boolean := False);
19409 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19410 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19411 -- True, Old_C is a stored discriminant. If they are both false then
19412 -- Old_C is a regular component.
19413
19414 -----------------------
19415 -- Inherit_Component --
19416 -----------------------
19417
19418 procedure Inherit_Component
19419 (Old_C : Entity_Id;
19420 Plain_Discrim : Boolean := False;
19421 Stored_Discrim : Boolean := False)
19422 is
19423 procedure Set_Anonymous_Type (Id : Entity_Id);
19424 -- Id denotes the entity of an access discriminant or anonymous
19425 -- access component. Set the type of Id to either the same type of
19426 -- Old_C or create a new one depending on whether the parent and
19427 -- the child types are in the same scope.
19428
19429 ------------------------
19430 -- Set_Anonymous_Type --
19431 ------------------------
19432
19433 procedure Set_Anonymous_Type (Id : Entity_Id) is
19434 Old_Typ : constant Entity_Id := Etype (Old_C);
19435
19436 begin
19437 if Scope (Parent_Base) = Scope (Derived_Base) then
19438 Set_Etype (Id, Old_Typ);
19439
19440 -- The parent and the derived type are in two different scopes.
19441 -- Reuse the type of the original discriminant / component by
19442 -- copying it in order to preserve all attributes.
19443
19444 else
19445 declare
19446 Typ : constant Entity_Id := New_Copy (Old_Typ);
19447
19448 begin
19449 Set_Etype (Id, Typ);
19450
19451 -- Since we do not generate component declarations for
19452 -- inherited components, associate the itype with the
19453 -- derived type.
19454
19455 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19456 Set_Scope (Typ, Derived_Base);
19457 end;
19458 end if;
19459 end Set_Anonymous_Type;
19460
19461 -- Local variables and constants
19462
19463 New_C : constant Entity_Id := New_Copy (Old_C);
19464
19465 Corr_Discrim : Entity_Id;
19466 Discrim : Entity_Id;
19467
19468 -- Start of processing for Inherit_Component
19469
19470 begin
19471 pragma Assert (not Is_Tagged or not Stored_Discrim);
19472
19473 Set_Parent (New_C, Parent (Old_C));
19474
19475 -- Regular discriminants and components must be inserted in the scope
19476 -- of the Derived_Base. Do it here.
19477
19478 if not Stored_Discrim then
19479 Enter_Name (New_C);
19480 end if;
19481
19482 -- For tagged types the Original_Record_Component must point to
19483 -- whatever this field was pointing to in the parent type. This has
19484 -- already been achieved by the call to New_Copy above.
19485
19486 if not Is_Tagged then
19487 Set_Original_Record_Component (New_C, New_C);
19488 Set_Corresponding_Record_Component (New_C, Old_C);
19489 end if;
19490
19491 -- Set the proper type of an access discriminant
19492
19493 if Ekind (New_C) = E_Discriminant
19494 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19495 then
19496 Set_Anonymous_Type (New_C);
19497 end if;
19498
19499 -- If we have inherited a component then see if its Etype contains
19500 -- references to Parent_Base discriminants. In this case, replace
19501 -- these references with the constraints given in Discs. We do not
19502 -- do this for the partial view of private types because this is
19503 -- not needed (only the components of the full view will be used
19504 -- for code generation) and cause problem. We also avoid this
19505 -- transformation in some error situations.
19506
19507 if Ekind (New_C) = E_Component then
19508
19509 -- Set the proper type of an anonymous access component
19510
19511 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19512 Set_Anonymous_Type (New_C);
19513
19514 elsif (Is_Private_Type (Derived_Base)
19515 and then not Is_Generic_Type (Derived_Base))
19516 or else (Is_Empty_Elmt_List (Discs)
19517 and then not Expander_Active)
19518 then
19519 Set_Etype (New_C, Etype (Old_C));
19520
19521 else
19522 -- The current component introduces a circularity of the
19523 -- following kind:
19524
19525 -- limited with Pack_2;
19526 -- package Pack_1 is
19527 -- type T_1 is tagged record
19528 -- Comp : access Pack_2.T_2;
19529 -- ...
19530 -- end record;
19531 -- end Pack_1;
19532
19533 -- with Pack_1;
19534 -- package Pack_2 is
19535 -- type T_2 is new Pack_1.T_1 with ...;
19536 -- end Pack_2;
19537
19538 Set_Etype
19539 (New_C,
19540 Constrain_Component_Type
19541 (Old_C, Derived_Base, N, Parent_Base, Discs));
19542 end if;
19543 end if;
19544
19545 if Plain_Discrim then
19546 Set_Corresponding_Discriminant (New_C, Old_C);
19547 Build_Discriminal (New_C);
19548
19549 -- If we are explicitly inheriting a stored discriminant it will be
19550 -- completely hidden.
19551
19552 elsif Stored_Discrim then
19553 Set_Corresponding_Discriminant (New_C, Empty);
19554 Set_Discriminal (New_C, Empty);
19555 Set_Is_Completely_Hidden (New_C);
19556
19557 -- Set the Original_Record_Component of each discriminant in the
19558 -- derived base to point to the corresponding stored that we just
19559 -- created.
19560
19561 Discrim := First_Discriminant (Derived_Base);
19562 while Present (Discrim) loop
19563 Corr_Discrim := Corresponding_Discriminant (Discrim);
19564
19565 -- Corr_Discrim could be missing in an error situation
19566
19567 if Present (Corr_Discrim)
19568 and then Original_Record_Component (Corr_Discrim) = Old_C
19569 then
19570 Set_Original_Record_Component (Discrim, New_C);
19571 Set_Corresponding_Record_Component (Discrim, Empty);
19572 end if;
19573
19574 Next_Discriminant (Discrim);
19575 end loop;
19576
19577 Append_Entity (New_C, Derived_Base);
19578 end if;
19579
19580 if not Is_Tagged then
19581 Append_Elmt (Old_C, Assoc_List);
19582 Append_Elmt (New_C, Assoc_List);
19583 end if;
19584 end Inherit_Component;
19585
19586 -- Variables local to Inherit_Component
19587
19588 Loc : constant Source_Ptr := Sloc (N);
19589
19590 Parent_Discrim : Entity_Id;
19591 Stored_Discrim : Entity_Id;
19592 D : Entity_Id;
19593 Component : Entity_Id;
19594
19595 -- Start of processing for Inherit_Components
19596
19597 begin
19598 if not Is_Tagged then
19599 Append_Elmt (Parent_Base, Assoc_List);
19600 Append_Elmt (Derived_Base, Assoc_List);
19601 end if;
19602
19603 -- Inherit parent discriminants if needed
19604
19605 if Inherit_Discr then
19606 Parent_Discrim := First_Discriminant (Parent_Base);
19607 while Present (Parent_Discrim) loop
19608 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19609 Next_Discriminant (Parent_Discrim);
19610 end loop;
19611 end if;
19612
19613 -- Create explicit stored discrims for untagged types when necessary
19614
19615 if not Has_Unknown_Discriminants (Derived_Base)
19616 and then Has_Discriminants (Parent_Base)
19617 and then not Is_Tagged
19618 and then
19619 (not Inherit_Discr
19620 or else First_Discriminant (Parent_Base) /=
19621 First_Stored_Discriminant (Parent_Base))
19622 then
19623 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19624 while Present (Stored_Discrim) loop
19625 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19626 Next_Stored_Discriminant (Stored_Discrim);
19627 end loop;
19628 end if;
19629
19630 -- See if we can apply the second transformation for derived types, as
19631 -- explained in point 6. in the comments above Build_Derived_Record_Type
19632 -- This is achieved by appending Derived_Base discriminants into Discs,
19633 -- which has the side effect of returning a non empty Discs list to the
19634 -- caller of Inherit_Components, which is what we want. This must be
19635 -- done for private derived types if there are explicit stored
19636 -- discriminants, to ensure that we can retrieve the values of the
19637 -- constraints provided in the ancestors.
19638
19639 if Inherit_Discr
19640 and then Is_Empty_Elmt_List (Discs)
19641 and then Present (First_Discriminant (Derived_Base))
19642 and then
19643 (not Is_Private_Type (Derived_Base)
19644 or else Is_Completely_Hidden
19645 (First_Stored_Discriminant (Derived_Base))
19646 or else Is_Generic_Type (Derived_Base))
19647 then
19648 D := First_Discriminant (Derived_Base);
19649 while Present (D) loop
19650 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19651 Next_Discriminant (D);
19652 end loop;
19653 end if;
19654
19655 -- Finally, inherit non-discriminant components unless they are not
19656 -- visible because defined or inherited from the full view of the
19657 -- parent. Don't inherit the _parent field of the parent type.
19658
19659 Component := First_Entity (Parent_Base);
19660 while Present (Component) loop
19661
19662 -- Ada 2005 (AI-251): Do not inherit components associated with
19663 -- secondary tags of the parent.
19664
19665 if Ekind (Component) = E_Component
19666 and then Present (Related_Type (Component))
19667 then
19668 null;
19669
19670 elsif Ekind (Component) /= E_Component
19671 or else Chars (Component) = Name_uParent
19672 then
19673 null;
19674
19675 -- If the derived type is within the parent type's declarative
19676 -- region, then the components can still be inherited even though
19677 -- they aren't visible at this point. This can occur for cases
19678 -- such as within public child units where the components must
19679 -- become visible upon entering the child unit's private part.
19680
19681 elsif not Is_Visible_Component (Component)
19682 and then not In_Open_Scopes (Scope (Parent_Base))
19683 then
19684 null;
19685
19686 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19687 then
19688 null;
19689
19690 else
19691 Inherit_Component (Component);
19692 end if;
19693
19694 Next_Entity (Component);
19695 end loop;
19696
19697 -- For tagged derived types, inherited discriminants cannot be used in
19698 -- component declarations of the record extension part. To achieve this
19699 -- we mark the inherited discriminants as not visible.
19700
19701 if Is_Tagged and then Inherit_Discr then
19702 D := First_Discriminant (Derived_Base);
19703 while Present (D) loop
19704 Set_Is_Immediately_Visible (D, False);
19705 Next_Discriminant (D);
19706 end loop;
19707 end if;
19708
19709 return Assoc_List;
19710 end Inherit_Components;
19711
19712 ----------------------
19713 -- Is_EVF_Procedure --
19714 ----------------------
19715
19716 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19717 Formal : Entity_Id;
19718
19719 begin
19720 -- Examine the formals of an Extensions_Visible False procedure looking
19721 -- for a controlling OUT parameter.
19722
19723 if Ekind (Subp) = E_Procedure
19724 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19725 then
19726 Formal := First_Formal (Subp);
19727 while Present (Formal) loop
19728 if Ekind (Formal) = E_Out_Parameter
19729 and then Is_Controlling_Formal (Formal)
19730 then
19731 return True;
19732 end if;
19733
19734 Next_Formal (Formal);
19735 end loop;
19736 end if;
19737
19738 return False;
19739 end Is_EVF_Procedure;
19740
19741 --------------------------
19742 -- Is_Private_Primitive --
19743 --------------------------
19744
19745 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19746 Prim_Scope : constant Entity_Id := Scope (Prim);
19747 Priv_Entity : Entity_Id;
19748 begin
19749 if Is_Package_Or_Generic_Package (Prim_Scope) then
19750 Priv_Entity := First_Private_Entity (Prim_Scope);
19751
19752 while Present (Priv_Entity) loop
19753 if Priv_Entity = Prim then
19754 return True;
19755 end if;
19756
19757 Next_Entity (Priv_Entity);
19758 end loop;
19759 end if;
19760
19761 return False;
19762 end Is_Private_Primitive;
19763
19764 ------------------------------
19765 -- Is_Valid_Constraint_Kind --
19766 ------------------------------
19767
19768 function Is_Valid_Constraint_Kind
19769 (T_Kind : Type_Kind;
19770 Constraint_Kind : Node_Kind) return Boolean
19771 is
19772 begin
19773 case T_Kind is
19774 when Enumeration_Kind
19775 | Integer_Kind
19776 =>
19777 return Constraint_Kind = N_Range_Constraint;
19778
19779 when Decimal_Fixed_Point_Kind =>
19780 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19781
19782 when Ordinary_Fixed_Point_Kind =>
19783 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19784
19785 when Float_Kind =>
19786 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19787
19788 when Access_Kind
19789 | Array_Kind
19790 | Class_Wide_Kind
19791 | Concurrent_Kind
19792 | Private_Kind
19793 | E_Incomplete_Type
19794 | E_Record_Subtype
19795 | E_Record_Type
19796 =>
19797 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19798
19799 when others =>
19800 return True; -- Error will be detected later
19801 end case;
19802 end Is_Valid_Constraint_Kind;
19803
19804 --------------------------
19805 -- Is_Visible_Component --
19806 --------------------------
19807
19808 function Is_Visible_Component
19809 (C : Entity_Id;
19810 N : Node_Id := Empty) return Boolean
19811 is
19812 Original_Comp : Entity_Id := Empty;
19813 Original_Type : Entity_Id;
19814 Type_Scope : Entity_Id;
19815
19816 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19817 -- Check whether parent type of inherited component is declared locally,
19818 -- possibly within a nested package or instance. The current scope is
19819 -- the derived record itself.
19820
19821 -------------------
19822 -- Is_Local_Type --
19823 -------------------
19824
19825 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19826 begin
19827 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19828 end Is_Local_Type;
19829
19830 -- Start of processing for Is_Visible_Component
19831
19832 begin
19833 if Ekind (C) in E_Component | E_Discriminant then
19834 Original_Comp := Original_Record_Component (C);
19835 end if;
19836
19837 if No (Original_Comp) then
19838
19839 -- Premature usage, or previous error
19840
19841 return False;
19842
19843 else
19844 Original_Type := Scope (Original_Comp);
19845 Type_Scope := Scope (Base_Type (Scope (C)));
19846 end if;
19847
19848 -- This test only concerns tagged types
19849
19850 if not Is_Tagged_Type (Original_Type) then
19851
19852 -- Check if this is a renamed discriminant (hidden either by the
19853 -- derived type or by some ancestor), unless we are analyzing code
19854 -- generated by the expander since it may reference such components
19855 -- (for example see the expansion of Deep_Adjust).
19856
19857 if Ekind (C) = E_Discriminant and then Present (N) then
19858 return
19859 not Comes_From_Source (N)
19860 or else not Is_Completely_Hidden (C);
19861 else
19862 return True;
19863 end if;
19864
19865 -- If it is _Parent or _Tag, there is no visibility issue
19866
19867 elsif not Comes_From_Source (Original_Comp) then
19868 return True;
19869
19870 -- Discriminants are visible unless the (private) type has unknown
19871 -- discriminants. If the discriminant reference is inserted for a
19872 -- discriminant check on a full view it is also visible.
19873
19874 elsif Ekind (Original_Comp) = E_Discriminant
19875 and then
19876 (not Has_Unknown_Discriminants (Original_Type)
19877 or else (Present (N)
19878 and then Nkind (N) = N_Selected_Component
19879 and then Nkind (Prefix (N)) = N_Type_Conversion
19880 and then not Comes_From_Source (Prefix (N))))
19881 then
19882 return True;
19883
19884 -- If the component has been declared in an ancestor which is currently
19885 -- a private type, then it is not visible. The same applies if the
19886 -- component's containing type is not in an open scope and the original
19887 -- component's enclosing type is a visible full view of a private type
19888 -- (which can occur in cases where an attempt is being made to reference
19889 -- a component in a sibling package that is inherited from a visible
19890 -- component of a type in an ancestor package; the component in the
19891 -- sibling package should not be visible even though the component it
19892 -- inherited from is visible), but instance bodies are not subject to
19893 -- this second case since they have the Has_Private_View mechanism to
19894 -- ensure proper visibility. This does not apply however in the case
19895 -- where the scope of the type is a private child unit, or when the
19896 -- parent comes from a local package in which the ancestor is currently
19897 -- visible. The latter suppression of visibility is needed for cases
19898 -- that are tested in B730006.
19899
19900 elsif Is_Private_Type (Original_Type)
19901 or else
19902 (not Is_Private_Descendant (Type_Scope)
19903 and then not In_Open_Scopes (Type_Scope)
19904 and then Has_Private_Declaration (Original_Type)
19905 and then not In_Instance_Body)
19906 then
19907 -- If the type derives from an entity in a formal package, there
19908 -- are no additional visible components.
19909
19910 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19911 N_Formal_Package_Declaration
19912 then
19913 return False;
19914
19915 -- if we are not in the private part of the current package, there
19916 -- are no additional visible components.
19917
19918 elsif Ekind (Scope (Current_Scope)) = E_Package
19919 and then not In_Private_Part (Scope (Current_Scope))
19920 then
19921 return False;
19922 else
19923 return
19924 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19925 and then In_Open_Scopes (Scope (Original_Type))
19926 and then Is_Local_Type (Type_Scope);
19927 end if;
19928
19929 -- There is another weird way in which a component may be invisible when
19930 -- the private and the full view are not derived from the same ancestor.
19931 -- Here is an example :
19932
19933 -- type A1 is tagged record F1 : integer; end record;
19934 -- type A2 is new A1 with record F2 : integer; end record;
19935 -- type T is new A1 with private;
19936 -- private
19937 -- type T is new A2 with null record;
19938
19939 -- In this case, the full view of T inherits F1 and F2 but the private
19940 -- view inherits only F1
19941
19942 else
19943 declare
19944 Ancestor : Entity_Id := Scope (C);
19945
19946 begin
19947 loop
19948 if Ancestor = Original_Type then
19949 return True;
19950
19951 -- The ancestor may have a partial view of the original type,
19952 -- but if the full view is in scope, as in a child body, the
19953 -- component is visible.
19954
19955 elsif In_Private_Part (Scope (Original_Type))
19956 and then Full_View (Ancestor) = Original_Type
19957 then
19958 return True;
19959
19960 elsif Ancestor = Etype (Ancestor) then
19961
19962 -- No further ancestors to examine
19963
19964 return False;
19965 end if;
19966
19967 Ancestor := Etype (Ancestor);
19968 end loop;
19969 end;
19970 end if;
19971 end Is_Visible_Component;
19972
19973 --------------------------
19974 -- Make_Class_Wide_Type --
19975 --------------------------
19976
19977 procedure Make_Class_Wide_Type (T : Entity_Id) is
19978 CW_Type : Entity_Id;
19979 CW_Name : Name_Id;
19980 Next_E : Entity_Id;
19981 Prev_E : Entity_Id;
19982
19983 begin
19984 if Present (Class_Wide_Type (T)) then
19985
19986 -- The class-wide type is a partially decorated entity created for a
19987 -- unanalyzed tagged type referenced through a limited with clause.
19988 -- When the tagged type is analyzed, its class-wide type needs to be
19989 -- redecorated. Note that we reuse the entity created by Decorate_
19990 -- Tagged_Type in order to preserve all links.
19991
19992 if Materialize_Entity (Class_Wide_Type (T)) then
19993 CW_Type := Class_Wide_Type (T);
19994 Set_Materialize_Entity (CW_Type, False);
19995
19996 -- The class wide type can have been defined by the partial view, in
19997 -- which case everything is already done.
19998
19999 else
20000 return;
20001 end if;
20002
20003 -- Default case, we need to create a new class-wide type
20004
20005 else
20006 CW_Type :=
20007 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
20008 end if;
20009
20010 -- Inherit root type characteristics
20011
20012 CW_Name := Chars (CW_Type);
20013 Next_E := Next_Entity (CW_Type);
20014 Prev_E := Prev_Entity (CW_Type);
20015 Copy_Node (T, CW_Type);
20016 Set_Comes_From_Source (CW_Type, False);
20017 Set_Chars (CW_Type, CW_Name);
20018 Set_Parent (CW_Type, Parent (T));
20019 Set_Prev_Entity (CW_Type, Prev_E);
20020 Set_Next_Entity (CW_Type, Next_E);
20021
20022 -- Ensure we have a new freeze node for the class-wide type. The partial
20023 -- view may have freeze action of its own, requiring a proper freeze
20024 -- node, and the same freeze node cannot be shared between the two
20025 -- types.
20026
20027 Set_Has_Delayed_Freeze (CW_Type);
20028 Set_Freeze_Node (CW_Type, Empty);
20029
20030 -- Customize the class-wide type: It has no prim. op., it cannot be
20031 -- abstract, its Etype points back to the specific root type, and it
20032 -- cannot have any invariants.
20033
20034 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
20035 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
20036
20037 elsif Ekind (CW_Type) in Concurrent_Kind then
20038 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
20039 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
20040
20041 if Ekind (CW_Type) in Task_Kind then
20042 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
20043 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
20044 end if;
20045
20046 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
20047 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
20048 end if;
20049
20050 elsif Ekind (CW_Type) = E_Record_Type then
20051 Reinit_Field_To_Zero (CW_Type, F_Corresponding_Concurrent_Type);
20052 end if;
20053
20054 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
20055 Set_Is_Tagged_Type (CW_Type, True);
20056 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
20057 Set_Is_Abstract_Type (CW_Type, False);
20058 Set_Is_Constrained (CW_Type, False);
20059 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
20060 Set_Default_SSO (CW_Type);
20061 Set_Has_Inheritable_Invariants (CW_Type, False);
20062 Set_Has_Inherited_Invariants (CW_Type, False);
20063 Set_Has_Own_Invariants (CW_Type, False);
20064
20065 if Ekind (T) = E_Class_Wide_Subtype then
20066 Set_Etype (CW_Type, Etype (Base_Type (T)));
20067 else
20068 Set_Etype (CW_Type, T);
20069 end if;
20070
20071 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
20072
20073 -- If this is the class_wide type of a constrained subtype, it does
20074 -- not have discriminants.
20075
20076 Set_Has_Discriminants (CW_Type,
20077 Has_Discriminants (T) and then not Is_Constrained (T));
20078
20079 Set_Has_Unknown_Discriminants (CW_Type, True);
20080 Set_Class_Wide_Type (T, CW_Type);
20081 Set_Equivalent_Type (CW_Type, Empty);
20082
20083 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
20084
20085 Set_Class_Wide_Type (CW_Type, CW_Type);
20086 end Make_Class_Wide_Type;
20087
20088 ----------------
20089 -- Make_Index --
20090 ----------------
20091
20092 procedure Make_Index
20093 (N : Node_Id;
20094 Related_Nod : Node_Id;
20095 Related_Id : Entity_Id := Empty;
20096 Suffix_Index : Pos := 1)
20097 is
20098 R : Node_Id;
20099 T : Entity_Id;
20100 Def_Id : Entity_Id := Empty;
20101 Found : Boolean := False;
20102
20103 begin
20104 -- For a discrete range used in a constrained array definition and
20105 -- defined by a range, an implicit conversion to the predefined type
20106 -- INTEGER is assumed if each bound is either a numeric literal, a named
20107 -- number, or an attribute, and the type of both bounds (prior to the
20108 -- implicit conversion) is the type universal_integer. Otherwise, both
20109 -- bounds must be of the same discrete type, other than universal
20110 -- integer; this type must be determinable independently of the
20111 -- context, but using the fact that the type must be discrete and that
20112 -- both bounds must have the same type.
20113
20114 -- Character literals also have a universal type in the absence of
20115 -- of additional context, and are resolved to Standard_Character.
20116
20117 if Nkind (N) = N_Range then
20118
20119 -- The index is given by a range constraint. The bounds are known
20120 -- to be of a consistent type.
20121
20122 if not Is_Overloaded (N) then
20123 T := Etype (N);
20124
20125 -- For universal bounds, choose the specific predefined type
20126
20127 if T = Universal_Integer then
20128 T := Standard_Integer;
20129
20130 elsif T = Any_Character then
20131 Ambiguous_Character (Low_Bound (N));
20132
20133 T := Standard_Character;
20134 end if;
20135
20136 -- The node may be overloaded because some user-defined operators
20137 -- are available, but if a universal interpretation exists it is
20138 -- also the selected one.
20139
20140 elsif Universal_Interpretation (N) = Universal_Integer then
20141 T := Standard_Integer;
20142
20143 else
20144 T := Any_Type;
20145
20146 declare
20147 Ind : Interp_Index;
20148 It : Interp;
20149
20150 begin
20151 Get_First_Interp (N, Ind, It);
20152 while Present (It.Typ) loop
20153 if Is_Discrete_Type (It.Typ) then
20154
20155 if Found
20156 and then not Covers (It.Typ, T)
20157 and then not Covers (T, It.Typ)
20158 then
20159 Error_Msg_N ("ambiguous bounds in discrete range", N);
20160 exit;
20161 else
20162 T := It.Typ;
20163 Found := True;
20164 end if;
20165 end if;
20166
20167 Get_Next_Interp (Ind, It);
20168 end loop;
20169
20170 if T = Any_Type then
20171 Error_Msg_N ("discrete type required for range", N);
20172 Set_Etype (N, Any_Type);
20173 return;
20174
20175 elsif T = Universal_Integer then
20176 T := Standard_Integer;
20177 end if;
20178 end;
20179 end if;
20180
20181 if not Is_Discrete_Type (T) then
20182 Error_Msg_N ("discrete type required for range", N);
20183 Set_Etype (N, Any_Type);
20184 return;
20185 end if;
20186
20187 -- If the range bounds are "T'First .. T'Last" where T is a name of a
20188 -- discrete type, then use T as the type of the index.
20189
20190 if Nkind (Low_Bound (N)) = N_Attribute_Reference
20191 and then Attribute_Name (Low_Bound (N)) = Name_First
20192 and then Is_Entity_Name (Prefix (Low_Bound (N)))
20193 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
20194
20195 and then Nkind (High_Bound (N)) = N_Attribute_Reference
20196 and then Attribute_Name (High_Bound (N)) = Name_Last
20197 and then Is_Entity_Name (Prefix (High_Bound (N)))
20198 and then Entity (Prefix (High_Bound (N))) = Def_Id
20199 then
20200 Def_Id := Entity (Prefix (Low_Bound (N)));
20201 end if;
20202
20203 R := N;
20204 Process_Range_Expr_In_Decl (R, T);
20205
20206 elsif Nkind (N) = N_Subtype_Indication then
20207
20208 -- The index is given by a subtype with a range constraint
20209
20210 T := Base_Type (Entity (Subtype_Mark (N)));
20211
20212 if not Is_Discrete_Type (T) then
20213 Error_Msg_N ("discrete type required for range", N);
20214 Set_Etype (N, Any_Type);
20215 return;
20216 end if;
20217
20218 R := Range_Expression (Constraint (N));
20219
20220 Resolve (R, T);
20221 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
20222
20223 elsif Nkind (N) = N_Attribute_Reference then
20224
20225 -- Catch beginner's error (use of attribute other than 'Range)
20226
20227 if Attribute_Name (N) /= Name_Range then
20228 Error_Msg_N ("expect attribute ''Range", N);
20229 Set_Etype (N, Any_Type);
20230 return;
20231 end if;
20232
20233 -- If the node denotes the range of a type mark, that is also the
20234 -- resulting type, and we do not need to create an Itype for it.
20235
20236 if Is_Entity_Name (Prefix (N))
20237 and then Comes_From_Source (N)
20238 and then Is_Discrete_Type (Entity (Prefix (N)))
20239 then
20240 Def_Id := Entity (Prefix (N));
20241 end if;
20242
20243 Analyze_And_Resolve (N);
20244 T := Etype (N);
20245 R := N;
20246
20247 -- If none of the above, must be a subtype. We convert this to a
20248 -- range attribute reference because in the case of declared first
20249 -- named subtypes, the types in the range reference can be different
20250 -- from the type of the entity. A range attribute normalizes the
20251 -- reference and obtains the correct types for the bounds.
20252
20253 -- This transformation is in the nature of an expansion, is only
20254 -- done if expansion is active. In particular, it is not done on
20255 -- formal generic types, because we need to retain the name of the
20256 -- original index for instantiation purposes.
20257
20258 else
20259 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
20260 Error_Msg_N ("invalid subtype mark in discrete range", N);
20261 Set_Etype (N, Any_Integer);
20262 return;
20263
20264 else
20265 -- The type mark may be that of an incomplete type. It is only
20266 -- now that we can get the full view, previous analysis does
20267 -- not look specifically for a type mark.
20268
20269 Set_Entity (N, Get_Full_View (Entity (N)));
20270 Set_Etype (N, Entity (N));
20271 Def_Id := Entity (N);
20272
20273 if not Is_Discrete_Type (Def_Id) then
20274 Error_Msg_N ("discrete type required for index", N);
20275 Set_Etype (N, Any_Type);
20276 return;
20277 end if;
20278 end if;
20279
20280 if Expander_Active then
20281 Rewrite (N,
20282 Make_Attribute_Reference (Sloc (N),
20283 Attribute_Name => Name_Range,
20284 Prefix => Relocate_Node (N)));
20285
20286 -- The original was a subtype mark that does not freeze. This
20287 -- means that the rewritten version must not freeze either.
20288
20289 Set_Must_Not_Freeze (N);
20290 Set_Must_Not_Freeze (Prefix (N));
20291 Analyze_And_Resolve (N);
20292 T := Etype (N);
20293 R := N;
20294
20295 -- If expander is inactive, type is legal, nothing else to construct
20296
20297 else
20298 return;
20299 end if;
20300 end if;
20301
20302 if not Is_Discrete_Type (T) then
20303 Error_Msg_N ("discrete type required for range", N);
20304 Set_Etype (N, Any_Type);
20305 return;
20306
20307 elsif T = Any_Type then
20308 Set_Etype (N, Any_Type);
20309 return;
20310 end if;
20311
20312 -- We will now create the appropriate Itype to describe the range, but
20313 -- first a check. If we originally had a subtype, then we just label
20314 -- the range with this subtype. Not only is there no need to construct
20315 -- a new subtype, but it is wrong to do so for two reasons:
20316
20317 -- 1. A legality concern, if we have a subtype, it must not freeze,
20318 -- and the Itype would cause freezing incorrectly
20319
20320 -- 2. An efficiency concern, if we created an Itype, it would not be
20321 -- recognized as the same type for the purposes of eliminating
20322 -- checks in some circumstances.
20323
20324 -- We signal this case by setting the subtype entity in Def_Id
20325
20326 if No (Def_Id) then
20327 Def_Id :=
20328 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
20329 Set_Etype (Def_Id, Base_Type (T));
20330
20331 if Is_Signed_Integer_Type (T) then
20332 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20333
20334 elsif Is_Modular_Integer_Type (T) then
20335 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20336
20337 else
20338 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20339 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20340 Set_First_Literal (Def_Id, First_Literal (T));
20341 end if;
20342
20343 Set_Size_Info (Def_Id, (T));
20344 Set_RM_Size (Def_Id, RM_Size (T));
20345 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20346
20347 Set_Scalar_Range (Def_Id, R);
20348 Conditional_Delay (Def_Id, T);
20349
20350 -- In the subtype indication case inherit properties of the parent
20351
20352 if Nkind (N) = N_Subtype_Indication then
20353
20354 -- It is enough to inherit predicate flags and not the predicate
20355 -- functions, because predicates on an index type are illegal
20356 -- anyway and the flags are enough to detect them.
20357
20358 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20359
20360 -- If the immediate parent of the new subtype is nonstatic, then
20361 -- the subtype we create is nonstatic as well, even if its bounds
20362 -- are static.
20363
20364 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20365 Set_Is_Non_Static_Subtype (Def_Id);
20366 end if;
20367 end if;
20368
20369 Set_Parent (Def_Id, N);
20370 end if;
20371
20372 -- Final step is to label the index with this constructed type
20373
20374 Set_Etype (N, Def_Id);
20375 end Make_Index;
20376
20377 ------------------------------
20378 -- Modular_Type_Declaration --
20379 ------------------------------
20380
20381 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20382 Mod_Expr : constant Node_Id := Expression (Def);
20383 M_Val : Uint;
20384
20385 procedure Set_Modular_Size (Bits : Int);
20386 -- Sets RM_Size to Bits, and Esize to normal word size above this
20387
20388 ----------------------
20389 -- Set_Modular_Size --
20390 ----------------------
20391
20392 procedure Set_Modular_Size (Bits : Int) is
20393 Siz : Int;
20394
20395 begin
20396 Set_RM_Size (T, UI_From_Int (Bits));
20397
20398 if Bits < System_Max_Binary_Modulus_Power then
20399 Siz := 8;
20400
20401 while Siz < 128 loop
20402 exit when Bits <= Siz;
20403 Siz := Siz * 2;
20404 end loop;
20405
20406 Set_Esize (T, UI_From_Int (Siz));
20407
20408 else
20409 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20410 end if;
20411
20412 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20413 Set_Is_Known_Valid (T);
20414 end if;
20415 end Set_Modular_Size;
20416
20417 -- Start of processing for Modular_Type_Declaration
20418
20419 begin
20420 -- If the mod expression is (exactly) 2 * literal, where literal is
20421 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20422
20423 if Warn_On_Suspicious_Modulus_Value
20424 and then Nkind (Mod_Expr) = N_Op_Multiply
20425 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20426 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20427 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20428 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20429 then
20430 Error_Msg_N
20431 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20432 end if;
20433
20434 -- Proceed with analysis of mod expression
20435
20436 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20437
20438 Set_Etype (T, T);
20439 Mutate_Ekind (T, E_Modular_Integer_Type);
20440 Reinit_Alignment (T);
20441 Set_Is_Constrained (T);
20442
20443 if not Is_OK_Static_Expression (Mod_Expr) then
20444 Flag_Non_Static_Expr
20445 ("non-static expression used for modular type bound!", Mod_Expr);
20446 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20447 else
20448 M_Val := Expr_Value (Mod_Expr);
20449 end if;
20450
20451 if M_Val < 1 then
20452 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20453 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20454 end if;
20455
20456 if M_Val > 2 ** Standard_Long_Integer_Size then
20457 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20458 end if;
20459
20460 Set_Modulus (T, M_Val);
20461
20462 -- Create bounds for the modular type based on the modulus given in
20463 -- the type declaration and then analyze and resolve those bounds.
20464
20465 Set_Scalar_Range (T,
20466 Make_Range (Sloc (Mod_Expr),
20467 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20468 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20469
20470 -- Properly analyze the literals for the range. We do this manually
20471 -- because we can't go calling Resolve, since we are resolving these
20472 -- bounds with the type, and this type is certainly not complete yet.
20473
20474 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20475 Set_Etype (High_Bound (Scalar_Range (T)), T);
20476 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20477 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20478
20479 -- Loop through powers of two to find number of bits required
20480
20481 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20482
20483 -- Binary case
20484
20485 if M_Val = 2 ** Bits then
20486 Set_Modular_Size (Bits);
20487 return;
20488
20489 -- Nonbinary case
20490
20491 elsif M_Val < 2 ** Bits then
20492 Set_Non_Binary_Modulus (T);
20493
20494 if Bits > System_Max_Nonbinary_Modulus_Power then
20495 Error_Msg_Uint_1 :=
20496 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20497 Error_Msg_F
20498 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20499 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20500 return;
20501
20502 else
20503 -- In the nonbinary case, set size as per RM 13.3(55)
20504
20505 Set_Modular_Size (Bits);
20506 return;
20507 end if;
20508 end if;
20509
20510 end loop;
20511
20512 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20513 -- so we just signal an error and set the maximum size.
20514
20515 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20516 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20517
20518 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20519 Reinit_Alignment (T);
20520
20521 end Modular_Type_Declaration;
20522
20523 --------------------------
20524 -- New_Concatenation_Op --
20525 --------------------------
20526
20527 procedure New_Concatenation_Op (Typ : Entity_Id) is
20528 Loc : constant Source_Ptr := Sloc (Typ);
20529 Op : Entity_Id;
20530
20531 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20532 -- Create abbreviated declaration for the formal of a predefined
20533 -- Operator 'Op' of type 'Typ'
20534
20535 --------------------
20536 -- Make_Op_Formal --
20537 --------------------
20538
20539 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20540 Formal : Entity_Id;
20541 begin
20542 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20543 Set_Etype (Formal, Typ);
20544 Set_Mechanism (Formal, Default_Mechanism);
20545 return Formal;
20546 end Make_Op_Formal;
20547
20548 -- Start of processing for New_Concatenation_Op
20549
20550 begin
20551 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20552
20553 Mutate_Ekind (Op, E_Operator);
20554 Set_Is_Not_Self_Hidden (Op);
20555 Set_Scope (Op, Current_Scope);
20556 Set_Etype (Op, Typ);
20557 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20558 Set_Is_Immediately_Visible (Op);
20559 Set_Is_Intrinsic_Subprogram (Op);
20560 Set_Has_Completion (Op);
20561 Append_Entity (Op, Current_Scope);
20562
20563 Set_Name_Entity_Id (Name_Op_Concat, Op);
20564
20565 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20566 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20567 end New_Concatenation_Op;
20568
20569 -------------------------
20570 -- OK_For_Limited_Init --
20571 -------------------------
20572
20573 -- ???Check all calls of this, and compare the conditions under which it's
20574 -- called.
20575
20576 function OK_For_Limited_Init
20577 (Typ : Entity_Id;
20578 Exp : Node_Id) return Boolean
20579 is
20580 begin
20581 return Is_CPP_Constructor_Call (Exp)
20582 or else (Ada_Version >= Ada_2005
20583 and then not Debug_Flag_Dot_L
20584 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20585 end OK_For_Limited_Init;
20586
20587 -------------------------------
20588 -- OK_For_Limited_Init_In_05 --
20589 -------------------------------
20590
20591 function OK_For_Limited_Init_In_05
20592 (Typ : Entity_Id;
20593 Exp : Node_Id) return Boolean
20594 is
20595 begin
20596 -- An object of a limited interface type can be initialized with any
20597 -- expression of a nonlimited descendant type. However this does not
20598 -- apply if this is a view conversion of some other expression. This
20599 -- is checked below.
20600
20601 if Is_Class_Wide_Type (Typ)
20602 and then Is_Limited_Interface (Typ)
20603 and then not Is_Limited_Type (Etype (Exp))
20604 and then Nkind (Exp) /= N_Type_Conversion
20605 then
20606 return True;
20607 end if;
20608
20609 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20610 -- case of limited aggregates (including extension aggregates), and
20611 -- function calls. The function call may have been given in prefixed
20612 -- notation, in which case the original node is an indexed component.
20613 -- If the function is parameterless, the original node was an explicit
20614 -- dereference. The function may also be parameterless, in which case
20615 -- the source node is just an identifier.
20616
20617 -- A branch of a conditional expression may have been removed if the
20618 -- condition is statically known. This happens during expansion, and
20619 -- thus will not happen if previous errors were encountered. The check
20620 -- will have been performed on the chosen branch, which replaces the
20621 -- original conditional expression.
20622
20623 if No (Exp) then
20624 return True;
20625 end if;
20626
20627 case Nkind (Original_Node (Exp)) is
20628 when N_Aggregate
20629 | N_Delta_Aggregate
20630 | N_Extension_Aggregate
20631 | N_Function_Call
20632 | N_Op
20633 =>
20634 return True;
20635
20636 when N_Identifier =>
20637 return Present (Entity (Original_Node (Exp)))
20638 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20639
20640 when N_Qualified_Expression =>
20641 return
20642 OK_For_Limited_Init_In_05
20643 (Typ, Expression (Original_Node (Exp)));
20644
20645 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20646 -- with a function call, the expander has rewritten the call into an
20647 -- N_Type_Conversion node to force displacement of the pointer to
20648 -- reference the component containing the secondary dispatch table.
20649 -- Otherwise a type conversion is not a legal context.
20650 -- A return statement for a build-in-place function returning a
20651 -- synchronized type also introduces an unchecked conversion.
20652
20653 when N_Type_Conversion
20654 | N_Unchecked_Type_Conversion
20655 =>
20656 return not Comes_From_Source (Exp)
20657 and then
20658 -- If the conversion has been rewritten, check Original_Node;
20659 -- otherwise, check the expression of the compiler-generated
20660 -- conversion (which is a conversion that we want to ignore
20661 -- for purposes of the limited-initialization restrictions).
20662
20663 (if Is_Rewrite_Substitution (Exp)
20664 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20665 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20666
20667 when N_Explicit_Dereference
20668 | N_Indexed_Component
20669 | N_Selected_Component
20670 =>
20671 return Nkind (Exp) = N_Function_Call;
20672
20673 -- A use of 'Input is a function call, hence allowed. Normally the
20674 -- attribute will be changed to a call, but the attribute by itself
20675 -- can occur with -gnatc.
20676
20677 when N_Attribute_Reference =>
20678 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20679
20680 -- "return raise ..." is OK
20681
20682 when N_Raise_Expression =>
20683 return True;
20684
20685 -- For a case expression, all dependent expressions must be legal
20686
20687 when N_Case_Expression =>
20688 declare
20689 Alt : Node_Id;
20690
20691 begin
20692 Alt := First (Alternatives (Original_Node (Exp)));
20693 while Present (Alt) loop
20694 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20695 return False;
20696 end if;
20697
20698 Next (Alt);
20699 end loop;
20700
20701 return True;
20702 end;
20703
20704 -- For an if expression, all dependent expressions must be legal
20705
20706 when N_If_Expression =>
20707 declare
20708 Then_Expr : constant Node_Id :=
20709 Next (First (Expressions (Original_Node (Exp))));
20710 Else_Expr : constant Node_Id := Next (Then_Expr);
20711 begin
20712 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20713 and then
20714 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20715 end;
20716
20717 when others =>
20718 return False;
20719 end case;
20720 end OK_For_Limited_Init_In_05;
20721
20722 -------------------------------------------
20723 -- Ordinary_Fixed_Point_Type_Declaration --
20724 -------------------------------------------
20725
20726 procedure Ordinary_Fixed_Point_Type_Declaration
20727 (T : Entity_Id;
20728 Def : Node_Id)
20729 is
20730 Loc : constant Source_Ptr := Sloc (Def);
20731 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20732 RRS : constant Node_Id := Real_Range_Specification (Def);
20733 Implicit_Base : Entity_Id;
20734 Delta_Val : Ureal;
20735 Small_Val : Ureal;
20736 Low_Val : Ureal;
20737 High_Val : Ureal;
20738
20739 begin
20740 Check_Restriction (No_Fixed_Point, Def);
20741
20742 -- Create implicit base type
20743
20744 Implicit_Base :=
20745 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20746 Set_Etype (Implicit_Base, Implicit_Base);
20747
20748 -- Analyze and process delta expression
20749
20750 Analyze_And_Resolve (Delta_Expr, Any_Real);
20751
20752 Check_Delta_Expression (Delta_Expr);
20753 Delta_Val := Expr_Value_R (Delta_Expr);
20754
20755 Set_Delta_Value (Implicit_Base, Delta_Val);
20756
20757 -- Compute default small from given delta, which is the largest power
20758 -- of two that does not exceed the given delta value.
20759
20760 declare
20761 Tmp : Ureal;
20762 Scale : Int;
20763
20764 begin
20765 Tmp := Ureal_1;
20766 Scale := 0;
20767
20768 if Delta_Val < Ureal_1 then
20769 while Delta_Val < Tmp loop
20770 Tmp := Tmp / Ureal_2;
20771 Scale := Scale + 1;
20772 end loop;
20773
20774 else
20775 loop
20776 Tmp := Tmp * Ureal_2;
20777 exit when Tmp > Delta_Val;
20778 Scale := Scale - 1;
20779 end loop;
20780 end if;
20781
20782 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20783 end;
20784
20785 Set_Small_Value (Implicit_Base, Small_Val);
20786
20787 -- If no range was given, set a dummy range
20788
20789 if RRS <= Empty_Or_Error then
20790 Low_Val := -Small_Val;
20791 High_Val := Small_Val;
20792
20793 -- Otherwise analyze and process given range
20794
20795 else
20796 declare
20797 Low : constant Node_Id := Low_Bound (RRS);
20798 High : constant Node_Id := High_Bound (RRS);
20799
20800 begin
20801 Analyze_And_Resolve (Low, Any_Real);
20802 Analyze_And_Resolve (High, Any_Real);
20803 Check_Real_Bound (Low);
20804 Check_Real_Bound (High);
20805
20806 -- Obtain and set the range
20807
20808 Low_Val := Expr_Value_R (Low);
20809 High_Val := Expr_Value_R (High);
20810
20811 if Low_Val > High_Val then
20812 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20813 end if;
20814 end;
20815 end if;
20816
20817 -- The range for both the implicit base and the declared first subtype
20818 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20819 -- set a temporary range in place. Note that the bounds of the base
20820 -- type will be widened to be symmetrical and to fill the available
20821 -- bits when the type is frozen.
20822
20823 -- We could do this with all discrete types, and probably should, but
20824 -- we absolutely have to do it for fixed-point, since the end-points
20825 -- of the range and the size are determined by the small value, which
20826 -- could be reset before the freeze point.
20827
20828 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20829 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20830
20831 -- Complete definition of first subtype. The inheritance of the rep item
20832 -- chain ensures that SPARK-related pragmas are not clobbered when the
20833 -- ordinary fixed point type acts as a full view of a private type.
20834
20835 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20836 Set_Etype (T, Implicit_Base);
20837 Reinit_Size_Align (T);
20838 Inherit_Rep_Item_Chain (T, Implicit_Base);
20839 Set_Small_Value (T, Small_Val);
20840 Set_Delta_Value (T, Delta_Val);
20841 Set_Is_Constrained (T);
20842 end Ordinary_Fixed_Point_Type_Declaration;
20843
20844 ----------------------------------
20845 -- Preanalyze_Assert_Expression --
20846 ----------------------------------
20847
20848 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20849 begin
20850 In_Assertion_Expr := In_Assertion_Expr + 1;
20851 Preanalyze_Spec_Expression (N, T);
20852 In_Assertion_Expr := In_Assertion_Expr - 1;
20853 end Preanalyze_Assert_Expression;
20854
20855 -- ??? The variant below explicitly saves and restores all the flags,
20856 -- because it is impossible to compose the existing variety of
20857 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20858 -- to achieve the desired semantics.
20859
20860 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20861 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20862 Save_Full_Analysis : constant Boolean := Full_Analysis;
20863
20864 begin
20865 In_Assertion_Expr := In_Assertion_Expr + 1;
20866 In_Spec_Expression := True;
20867 Full_Analysis := False;
20868 Expander_Mode_Save_And_Set (False);
20869
20870 if GNATprove_Mode then
20871 Analyze_And_Resolve (N);
20872 else
20873 Analyze_And_Resolve (N, Suppress => All_Checks);
20874 end if;
20875
20876 Expander_Mode_Restore;
20877 Full_Analysis := Save_Full_Analysis;
20878 In_Spec_Expression := Save_In_Spec_Expression;
20879 In_Assertion_Expr := In_Assertion_Expr - 1;
20880 end Preanalyze_Assert_Expression;
20881
20882 -----------------------------------
20883 -- Preanalyze_Default_Expression --
20884 -----------------------------------
20885
20886 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20887 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20888 begin
20889 In_Default_Expr := True;
20890 Preanalyze_Spec_Expression (N, T);
20891 In_Default_Expr := Save_In_Default_Expr;
20892 end Preanalyze_Default_Expression;
20893
20894 --------------------------------
20895 -- Preanalyze_Spec_Expression --
20896 --------------------------------
20897
20898 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20899 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20900 begin
20901 In_Spec_Expression := True;
20902 Preanalyze_And_Resolve (N, T);
20903 In_Spec_Expression := Save_In_Spec_Expression;
20904 end Preanalyze_Spec_Expression;
20905
20906 ----------------------------------------
20907 -- Prepare_Private_Subtype_Completion --
20908 ----------------------------------------
20909
20910 procedure Prepare_Private_Subtype_Completion
20911 (Id : Entity_Id;
20912 Related_Nod : Node_Id)
20913 is
20914 Id_B : constant Entity_Id := Base_Type (Id);
20915 Full_B : constant Entity_Id := Full_View (Id_B);
20916 Full : Entity_Id;
20917
20918 begin
20919 if Present (Full_B) then
20920
20921 -- The Base_Type is already completed, we can complete the subtype
20922 -- now. We have to create a new entity with the same name, Thus we
20923 -- can't use Create_Itype.
20924
20925 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20926 Set_Is_Itype (Full);
20927 Set_Associated_Node_For_Itype (Full, Related_Nod);
20928 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20929 Set_Full_View (Id, Full);
20930 end if;
20931
20932 -- The parent subtype may be private, but the base might not, in some
20933 -- nested instances. In that case, the subtype does not need to be
20934 -- exchanged. It would still be nice to make private subtypes and their
20935 -- bases consistent at all times ???
20936
20937 if Is_Private_Type (Id_B) then
20938 Append_Elmt (Id, Private_Dependents (Id_B));
20939 end if;
20940 end Prepare_Private_Subtype_Completion;
20941
20942 ---------------------------
20943 -- Process_Discriminants --
20944 ---------------------------
20945
20946 procedure Process_Discriminants
20947 (N : Node_Id;
20948 Prev : Entity_Id := Empty)
20949 is
20950 Elist : constant Elist_Id := New_Elmt_List;
20951 Id : Node_Id;
20952 Discr : Node_Id;
20953 Discr_Number : Uint;
20954 Discr_Type : Entity_Id;
20955 Default_Present : Boolean := False;
20956 Default_Not_Present : Boolean := False;
20957
20958 begin
20959 -- A composite type other than an array type can have discriminants.
20960 -- On entry, the current scope is the composite type.
20961
20962 -- The discriminants are initially entered into the scope of the type
20963 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20964 -- use, as explained at the end of this procedure.
20965
20966 Discr := First (Discriminant_Specifications (N));
20967 while Present (Discr) loop
20968 Enter_Name (Defining_Identifier (Discr));
20969
20970 -- For navigation purposes we add a reference to the discriminant
20971 -- in the entity for the type. If the current declaration is a
20972 -- completion, place references on the partial view. Otherwise the
20973 -- type is the current scope.
20974
20975 if Present (Prev) then
20976
20977 -- The references go on the partial view, if present. If the
20978 -- partial view has discriminants, the references have been
20979 -- generated already.
20980
20981 if not Has_Discriminants (Prev) then
20982 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20983 end if;
20984 else
20985 Generate_Reference
20986 (Current_Scope, Defining_Identifier (Discr), 'd');
20987 end if;
20988
20989 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20990 Check_Anonymous_Access_Component
20991 (Typ_Decl => N,
20992 Typ => Defining_Identifier (N),
20993 Prev => Prev,
20994 Comp_Def => Discr,
20995 Access_Def => Discriminant_Type (Discr));
20996
20997 -- if Check_Anonymous_Access_Component replaced Discr then
20998 -- its Original_Node points to the old Discr and the access type
20999 -- for Discr_Type has already been created.
21000
21001 if Is_Rewrite_Substitution (Discr) then
21002 Discr_Type := Etype (Discriminant_Type (Discr));
21003 else
21004 Discr_Type :=
21005 Access_Definition (Discr, Discriminant_Type (Discr));
21006
21007 -- Ada 2005 (AI-254)
21008
21009 if Present (Access_To_Subprogram_Definition
21010 (Discriminant_Type (Discr)))
21011 and then Protected_Present (Access_To_Subprogram_Definition
21012 (Discriminant_Type (Discr)))
21013 then
21014 Discr_Type :=
21015 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
21016 end if;
21017 end if;
21018 else
21019 Find_Type (Discriminant_Type (Discr));
21020 Discr_Type := Etype (Discriminant_Type (Discr));
21021
21022 if Error_Posted (Discriminant_Type (Discr)) then
21023 Discr_Type := Any_Type;
21024 end if;
21025 end if;
21026
21027 -- Handling of discriminants that are access types
21028
21029 if Is_Access_Type (Discr_Type) then
21030
21031 -- Ada 2005 (AI-230): Access discriminant allowed in non-
21032 -- limited record types
21033
21034 if Ada_Version < Ada_2005 then
21035 Check_Access_Discriminant_Requires_Limited
21036 (Discr, Discriminant_Type (Discr));
21037 end if;
21038
21039 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
21040 Error_Msg_N
21041 ("(Ada 83) access discriminant not allowed", Discr);
21042 end if;
21043
21044 -- If not access type, must be a discrete type
21045
21046 elsif not Is_Discrete_Type (Discr_Type) then
21047 Error_Msg_N
21048 ("discriminants must have a discrete or access type",
21049 Discriminant_Type (Discr));
21050 end if;
21051
21052 Set_Etype (Defining_Identifier (Discr), Discr_Type);
21053
21054 -- If a discriminant specification includes the assignment compound
21055 -- delimiter followed by an expression, the expression is the default
21056 -- expression of the discriminant; the default expression must be of
21057 -- the type of the discriminant. (RM 3.7.1) Since this expression is
21058 -- a default expression, we do the special preanalysis, since this
21059 -- expression does not freeze (see section "Handling of Default and
21060 -- Per-Object Expressions" in spec of package Sem).
21061
21062 if Present (Expression (Discr)) then
21063 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
21064
21065 -- Legaity checks
21066
21067 if Nkind (N) = N_Formal_Type_Declaration then
21068 Error_Msg_N
21069 ("discriminant defaults not allowed for formal type",
21070 Expression (Discr));
21071
21072 -- Flag an error for a tagged type with defaulted discriminants,
21073 -- excluding limited tagged types when compiling for Ada 2012
21074 -- (see AI05-0214).
21075
21076 elsif Is_Tagged_Type (Current_Scope)
21077 and then (not Is_Limited_Type (Current_Scope)
21078 or else Ada_Version < Ada_2012)
21079 and then Comes_From_Source (N)
21080 then
21081 -- Note: see similar test in Check_Or_Process_Discriminants, to
21082 -- handle the (illegal) case of the completion of an untagged
21083 -- view with discriminants with defaults by a tagged full view.
21084 -- We skip the check if Discr does not come from source, to
21085 -- account for the case of an untagged derived type providing
21086 -- defaults for a renamed discriminant from a private untagged
21087 -- ancestor with a tagged full view (ACATS B460006).
21088
21089 if Ada_Version >= Ada_2012 then
21090 Error_Msg_N
21091 ("discriminants of nonlimited tagged type cannot have"
21092 & " defaults",
21093 Expression (Discr));
21094 else
21095 Error_Msg_N
21096 ("discriminants of tagged type cannot have defaults",
21097 Expression (Discr));
21098 end if;
21099
21100 else
21101 Default_Present := True;
21102 Append_Elmt (Expression (Discr), Elist);
21103
21104 -- Tag the defining identifiers for the discriminants with
21105 -- their corresponding default expressions from the tree.
21106
21107 Set_Discriminant_Default_Value
21108 (Defining_Identifier (Discr), Expression (Discr));
21109 end if;
21110
21111 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
21112 -- gets set unless we can be sure that no range check is required.
21113
21114 if not Expander_Active
21115 and then not
21116 Is_In_Range
21117 (Expression (Discr), Discr_Type, Assume_Valid => True)
21118 then
21119 Set_Do_Range_Check (Expression (Discr));
21120 end if;
21121
21122 -- No default discriminant value given
21123
21124 else
21125 Default_Not_Present := True;
21126 end if;
21127
21128 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
21129 -- Discr_Type but with the null-exclusion attribute
21130
21131 if Ada_Version >= Ada_2005 then
21132
21133 -- Ada 2005 (AI-231): Static checks
21134
21135 if Can_Never_Be_Null (Discr_Type) then
21136 Null_Exclusion_Static_Checks (Discr);
21137
21138 elsif Is_Access_Type (Discr_Type)
21139 and then Null_Exclusion_Present (Discr)
21140
21141 -- No need to check itypes because in their case this check
21142 -- was done at their point of creation
21143
21144 and then not Is_Itype (Discr_Type)
21145 then
21146 if Can_Never_Be_Null (Discr_Type) then
21147 Error_Msg_NE
21148 ("`NOT NULL` not allowed (& already excludes null)",
21149 Discr,
21150 Discr_Type);
21151 end if;
21152
21153 Set_Etype (Defining_Identifier (Discr),
21154 Create_Null_Excluding_Itype
21155 (T => Discr_Type,
21156 Related_Nod => Discr));
21157
21158 -- Check for improper null exclusion if the type is otherwise
21159 -- legal for a discriminant.
21160
21161 elsif Null_Exclusion_Present (Discr)
21162 and then Is_Discrete_Type (Discr_Type)
21163 then
21164 Error_Msg_N
21165 ("null exclusion can only apply to an access type", Discr);
21166 end if;
21167
21168 -- Ada 2005 (AI-402): access discriminants of nonlimited types
21169 -- can't have defaults. Synchronized types, or types that are
21170 -- explicitly limited are fine, but special tests apply to derived
21171 -- types in generics: in a generic body we have to assume the
21172 -- worst, and therefore defaults are not allowed if the parent is
21173 -- a generic formal private type (see ACATS B370001).
21174
21175 if Is_Access_Type (Discr_Type) and then Default_Present then
21176 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
21177 or else Is_Limited_Record (Current_Scope)
21178 or else Is_Concurrent_Type (Current_Scope)
21179 or else Is_Concurrent_Record_Type (Current_Scope)
21180 or else Ekind (Current_Scope) = E_Limited_Private_Type
21181 then
21182 if not Is_Derived_Type (Current_Scope)
21183 or else not Is_Generic_Type (Etype (Current_Scope))
21184 or else not In_Package_Body (Scope (Etype (Current_Scope)))
21185 or else Limited_Present
21186 (Type_Definition (Parent (Current_Scope)))
21187 then
21188 null;
21189
21190 else
21191 Error_Msg_N
21192 ("access discriminants of nonlimited types cannot "
21193 & "have defaults", Expression (Discr));
21194 end if;
21195
21196 elsif Present (Expression (Discr)) then
21197 Error_Msg_N
21198 ("(Ada 2005) access discriminants of nonlimited types "
21199 & "cannot have defaults", Expression (Discr));
21200 end if;
21201 end if;
21202 end if;
21203
21204 Next (Discr);
21205 end loop;
21206
21207 -- An element list consisting of the default expressions of the
21208 -- discriminants is constructed in the above loop and used to set
21209 -- the Discriminant_Constraint attribute for the type. If an object
21210 -- is declared of this (record or task) type without any explicit
21211 -- discriminant constraint given, this element list will form the
21212 -- actual parameters for the corresponding initialization procedure
21213 -- for the type.
21214
21215 Set_Discriminant_Constraint (Current_Scope, Elist);
21216 Set_Stored_Constraint (Current_Scope, No_Elist);
21217
21218 -- Default expressions must be provided either for all or for none
21219 -- of the discriminants of a discriminant part. (RM 3.7.1)
21220
21221 if Default_Present and then Default_Not_Present then
21222 Error_Msg_N
21223 ("incomplete specification of defaults for discriminants", N);
21224 end if;
21225
21226 -- The use of the name of a discriminant is not allowed in default
21227 -- expressions of a discriminant part if the specification of the
21228 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
21229
21230 -- To detect this, the discriminant names are entered initially with an
21231 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
21232 -- attempt to use a void entity (for example in an expression that is
21233 -- type-checked) produces the error message: premature usage. Now after
21234 -- completing the semantic analysis of the discriminant part, we can set
21235 -- the Ekind of all the discriminants appropriately.
21236
21237 Discr := First (Discriminant_Specifications (N));
21238 Discr_Number := Uint_1;
21239 while Present (Discr) loop
21240 Id := Defining_Identifier (Discr);
21241
21242 if Ekind (Id) = E_In_Parameter then
21243 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
21244 end if;
21245
21246 Mutate_Ekind (Id, E_Discriminant);
21247 Set_Is_Not_Self_Hidden (Id);
21248 Reinit_Component_Location (Id);
21249 Reinit_Esize (Id);
21250 Set_Discriminant_Number (Id, Discr_Number);
21251
21252 -- Make sure this is always set, even in illegal programs
21253
21254 Set_Corresponding_Discriminant (Id, Empty);
21255
21256 -- Initialize the Original_Record_Component to the entity itself.
21257 -- Inherit_Components will propagate the right value to
21258 -- discriminants in derived record types.
21259
21260 Set_Original_Record_Component (Id, Id);
21261
21262 -- Create the discriminal for the discriminant
21263
21264 Build_Discriminal (Id);
21265
21266 Next (Discr);
21267 Discr_Number := Discr_Number + 1;
21268 end loop;
21269
21270 Set_Has_Discriminants (Current_Scope);
21271 end Process_Discriminants;
21272
21273 -----------------------
21274 -- Process_Full_View --
21275 -----------------------
21276
21277 -- WARNING: This routine manages Ghost regions. Return statements must be
21278 -- replaced by gotos which jump to the end of the routine and restore the
21279 -- Ghost mode.
21280
21281 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
21282 procedure Collect_Implemented_Interfaces
21283 (Typ : Entity_Id;
21284 Ifaces : Elist_Id);
21285 -- Ada 2005: Gather all the interfaces that Typ directly or
21286 -- inherently implements. Duplicate entries are not added to
21287 -- the list Ifaces.
21288
21289 ------------------------------------
21290 -- Collect_Implemented_Interfaces --
21291 ------------------------------------
21292
21293 procedure Collect_Implemented_Interfaces
21294 (Typ : Entity_Id;
21295 Ifaces : Elist_Id)
21296 is
21297 Iface : Entity_Id;
21298 Iface_Elmt : Elmt_Id;
21299
21300 begin
21301 -- Abstract interfaces are only associated with tagged record types
21302
21303 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
21304 return;
21305 end if;
21306
21307 -- Recursively climb to the ancestors
21308
21309 if Etype (Typ) /= Typ
21310
21311 -- Protect the frontend against wrong cyclic declarations like:
21312
21313 -- type B is new A with private;
21314 -- type C is new A with private;
21315 -- private
21316 -- type B is new C with null record;
21317 -- type C is new B with null record;
21318
21319 and then Etype (Typ) /= Priv_T
21320 and then Etype (Typ) /= Full_T
21321 then
21322 -- Keep separate the management of private type declarations
21323
21324 if Ekind (Typ) = E_Record_Type_With_Private then
21325
21326 -- Handle the following illegal usage:
21327 -- type Private_Type is tagged private;
21328 -- private
21329 -- type Private_Type is new Type_Implementing_Iface;
21330
21331 if Present (Full_View (Typ))
21332 and then Etype (Typ) /= Full_View (Typ)
21333 then
21334 if Is_Interface (Etype (Typ)) then
21335 Append_Unique_Elmt (Etype (Typ), Ifaces);
21336 end if;
21337
21338 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21339 end if;
21340
21341 -- Non-private types
21342
21343 else
21344 if Is_Interface (Etype (Typ)) then
21345 Append_Unique_Elmt (Etype (Typ), Ifaces);
21346 end if;
21347
21348 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21349 end if;
21350 end if;
21351
21352 -- Handle entities in the list of abstract interfaces
21353
21354 if Present (Interfaces (Typ)) then
21355 Iface_Elmt := First_Elmt (Interfaces (Typ));
21356 while Present (Iface_Elmt) loop
21357 Iface := Node (Iface_Elmt);
21358
21359 pragma Assert (Is_Interface (Iface));
21360
21361 if not Contain_Interface (Iface, Ifaces) then
21362 Append_Elmt (Iface, Ifaces);
21363 Collect_Implemented_Interfaces (Iface, Ifaces);
21364 end if;
21365
21366 Next_Elmt (Iface_Elmt);
21367 end loop;
21368 end if;
21369 end Collect_Implemented_Interfaces;
21370
21371 -- Local variables
21372
21373 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21374 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21375 -- Save the Ghost-related attributes to restore on exit
21376
21377 Full_Indic : Node_Id;
21378 Full_Parent : Entity_Id;
21379 Priv_Parent : Entity_Id;
21380
21381 -- Start of processing for Process_Full_View
21382
21383 begin
21384 Mark_And_Set_Ghost_Completion (N, Priv_T);
21385
21386 -- First some sanity checks that must be done after semantic
21387 -- decoration of the full view and thus cannot be placed with other
21388 -- similar checks in Find_Type_Name
21389
21390 if not Is_Limited_Type (Priv_T)
21391 and then (Is_Limited_Type (Full_T)
21392 or else Is_Limited_Composite (Full_T))
21393 then
21394 if In_Instance then
21395 null;
21396 else
21397 Error_Msg_N
21398 ("completion of nonlimited type cannot be limited", Full_T);
21399 Explain_Limited_Type (Full_T, Full_T);
21400 end if;
21401
21402 elsif Is_Abstract_Type (Full_T)
21403 and then not Is_Abstract_Type (Priv_T)
21404 then
21405 Error_Msg_N
21406 ("completion of nonabstract type cannot be abstract", Full_T);
21407
21408 elsif Is_Tagged_Type (Priv_T)
21409 and then Is_Limited_Type (Priv_T)
21410 and then not Is_Limited_Type (Full_T)
21411 then
21412 -- If pragma CPP_Class was applied to the private declaration
21413 -- propagate the limitedness to the full-view
21414
21415 if Is_CPP_Class (Priv_T) then
21416 Set_Is_Limited_Record (Full_T);
21417
21418 -- GNAT allow its own definition of Limited_Controlled to disobey
21419 -- this rule in order in ease the implementation. This test is safe
21420 -- because Root_Controlled is defined in a child of System that
21421 -- normal programs are not supposed to use.
21422
21423 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21424 Set_Is_Limited_Composite (Full_T);
21425 else
21426 Error_Msg_N
21427 ("completion of limited tagged type must be limited", Full_T);
21428 end if;
21429
21430 elsif Is_Generic_Type (Priv_T) then
21431 Error_Msg_N ("generic type cannot have a completion", Full_T);
21432 end if;
21433
21434 -- Check that ancestor interfaces of private and full views are
21435 -- consistent. We omit this check for synchronized types because
21436 -- they are performed on the corresponding record type when frozen.
21437
21438 if Ada_Version >= Ada_2005
21439 and then Is_Tagged_Type (Priv_T)
21440 and then Is_Tagged_Type (Full_T)
21441 and then not Is_Concurrent_Type (Full_T)
21442 then
21443 declare
21444 Iface : Entity_Id;
21445 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21446 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21447
21448 begin
21449 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21450 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21451
21452 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21453 -- an interface type if and only if the full type is descendant
21454 -- of the interface type (AARM 7.3 (7.3/2)).
21455
21456 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21457
21458 if Present (Iface) then
21459 Error_Msg_NE
21460 ("interface in partial view& not implemented by full type "
21461 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21462 end if;
21463
21464 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21465
21466 if Present (Iface) then
21467 Error_Msg_NE
21468 ("interface & not implemented by partial view "
21469 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21470 end if;
21471 end;
21472 end if;
21473
21474 if Is_Tagged_Type (Priv_T)
21475 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21476 and then Is_Derived_Type (Full_T)
21477 then
21478 Priv_Parent := Etype (Priv_T);
21479
21480 -- The full view of a private extension may have been transformed
21481 -- into an unconstrained derived type declaration and a subtype
21482 -- declaration (see build_derived_record_type for details).
21483
21484 if Nkind (N) = N_Subtype_Declaration then
21485 Full_Indic := Subtype_Indication (N);
21486 Full_Parent := Etype (Base_Type (Full_T));
21487 else
21488 Full_Indic := Subtype_Indication (Type_Definition (N));
21489 Full_Parent := Etype (Full_T);
21490 end if;
21491
21492 -- Check that the parent type of the full type is a descendant of
21493 -- the ancestor subtype given in the private extension. If either
21494 -- entity has an Etype equal to Any_Type then we had some previous
21495 -- error situation [7.3(8)].
21496
21497 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21498 goto Leave;
21499
21500 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21501 -- any order. Therefore we don't have to check that its parent must
21502 -- be a descendant of the parent of the private type declaration.
21503
21504 elsif Is_Interface (Priv_Parent)
21505 and then Is_Interface (Full_Parent)
21506 then
21507 null;
21508
21509 -- Ada 2005 (AI-251): If the parent of the private type declaration
21510 -- is an interface there is no need to check that it is an ancestor
21511 -- of the associated full type declaration. The required tests for
21512 -- this case are performed by Build_Derived_Record_Type.
21513
21514 elsif not Is_Interface (Base_Type (Priv_Parent))
21515 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21516 then
21517 Error_Msg_N
21518 ("parent of full type must descend from parent of private "
21519 & "extension", Full_Indic);
21520
21521 -- First check a formal restriction, and then proceed with checking
21522 -- Ada rules. Since the formal restriction is not a serious error, we
21523 -- don't prevent further error detection for this check, hence the
21524 -- ELSE.
21525
21526 else
21527 -- Check the rules of 7.3(10): if the private extension inherits
21528 -- known discriminants, then the full type must also inherit those
21529 -- discriminants from the same (ancestor) type, and the parent
21530 -- subtype of the full type must be constrained if and only if
21531 -- the ancestor subtype of the private extension is constrained.
21532
21533 if No (Discriminant_Specifications (Parent (Priv_T)))
21534 and then not Has_Unknown_Discriminants (Priv_T)
21535 and then Has_Discriminants (Base_Type (Priv_Parent))
21536 then
21537 declare
21538 Priv_Indic : constant Node_Id :=
21539 Subtype_Indication (Parent (Priv_T));
21540
21541 Priv_Constr : constant Boolean :=
21542 Is_Constrained (Priv_Parent)
21543 or else
21544 Nkind (Priv_Indic) = N_Subtype_Indication
21545 or else
21546 Is_Constrained (Entity (Priv_Indic));
21547
21548 Full_Constr : constant Boolean :=
21549 Is_Constrained (Full_Parent)
21550 or else
21551 Nkind (Full_Indic) = N_Subtype_Indication
21552 or else
21553 Is_Constrained (Entity (Full_Indic));
21554
21555 Priv_Discr : Entity_Id;
21556 Full_Discr : Entity_Id;
21557
21558 begin
21559 Priv_Discr := First_Discriminant (Priv_Parent);
21560 Full_Discr := First_Discriminant (Full_Parent);
21561 while Present (Priv_Discr) and then Present (Full_Discr) loop
21562 if Original_Record_Component (Priv_Discr) =
21563 Original_Record_Component (Full_Discr)
21564 or else
21565 Corresponding_Discriminant (Priv_Discr) =
21566 Corresponding_Discriminant (Full_Discr)
21567 then
21568 null;
21569 else
21570 exit;
21571 end if;
21572
21573 Next_Discriminant (Priv_Discr);
21574 Next_Discriminant (Full_Discr);
21575 end loop;
21576
21577 if Present (Priv_Discr) or else Present (Full_Discr) then
21578 Error_Msg_N
21579 ("full view must inherit discriminants of the parent "
21580 & "type used in the private extension", Full_Indic);
21581
21582 elsif Priv_Constr and then not Full_Constr then
21583 Error_Msg_N
21584 ("parent subtype of full type must be constrained",
21585 Full_Indic);
21586
21587 elsif Full_Constr and then not Priv_Constr then
21588 Error_Msg_N
21589 ("parent subtype of full type must be unconstrained",
21590 Full_Indic);
21591 end if;
21592 end;
21593
21594 -- Check the rules of 7.3(12): if a partial view has neither
21595 -- known or unknown discriminants, then the full type
21596 -- declaration shall define a definite subtype.
21597
21598 elsif not Has_Unknown_Discriminants (Priv_T)
21599 and then not Has_Discriminants (Priv_T)
21600 and then not Is_Constrained (Full_T)
21601 then
21602 Error_Msg_N
21603 ("full view must define a constrained type if partial view "
21604 & "has no discriminants", Full_T);
21605 end if;
21606
21607 -- Do we implement the following properly???
21608 -- If the ancestor subtype of a private extension has constrained
21609 -- discriminants, then the parent subtype of the full view shall
21610 -- impose a statically matching constraint on those discriminants
21611 -- [7.3(13)].
21612 end if;
21613
21614 else
21615 -- For untagged types, verify that a type without discriminants is
21616 -- not completed with an unconstrained type. A separate error message
21617 -- is produced if the full type has defaulted discriminants.
21618
21619 if Is_Definite_Subtype (Priv_T)
21620 and then not Is_Definite_Subtype (Full_T)
21621 then
21622 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21623 Error_Msg_NE
21624 ("full view of& not compatible with declaration#",
21625 Full_T, Priv_T);
21626
21627 if not Is_Tagged_Type (Full_T) then
21628 Error_Msg_N
21629 ("\one is constrained, the other unconstrained", Full_T);
21630 end if;
21631 end if;
21632 end if;
21633
21634 -- AI-419: verify that the use of "limited" is consistent
21635
21636 declare
21637 Orig_Decl : constant Node_Id := Original_Node (N);
21638
21639 begin
21640 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21641 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21642 and then Nkind
21643 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21644 then
21645 if not Limited_Present (Parent (Priv_T))
21646 and then not Synchronized_Present (Parent (Priv_T))
21647 and then Limited_Present (Type_Definition (Orig_Decl))
21648 then
21649 Error_Msg_N
21650 ("full view of non-limited extension cannot be limited", N);
21651
21652 -- Conversely, if the partial view carries the limited keyword,
21653 -- the full view must as well, even if it may be redundant.
21654
21655 elsif Limited_Present (Parent (Priv_T))
21656 and then not Limited_Present (Type_Definition (Orig_Decl))
21657 then
21658 Error_Msg_N
21659 ("full view of limited extension must be explicitly limited",
21660 N);
21661 end if;
21662 end if;
21663 end;
21664
21665 -- Ada 2005 (AI-443): A synchronized private extension must be
21666 -- completed by a task or protected type.
21667
21668 if Ada_Version >= Ada_2005
21669 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21670 and then Synchronized_Present (Parent (Priv_T))
21671 and then not Is_Concurrent_Type (Full_T)
21672 then
21673 Error_Msg_N ("full view of synchronized extension must " &
21674 "be synchronized type", N);
21675 end if;
21676
21677 -- Ada 2005 AI-363: if the full view has discriminants with
21678 -- defaults, it is illegal to declare constrained access subtypes
21679 -- whose designated type is the current type. This allows objects
21680 -- of the type that are declared in the heap to be unconstrained.
21681
21682 if not Has_Unknown_Discriminants (Priv_T)
21683 and then not Has_Discriminants (Priv_T)
21684 and then Has_Defaulted_Discriminants (Full_T)
21685 then
21686 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21687 Set_Has_Constrained_Partial_View (Priv_T);
21688 end if;
21689
21690 -- Create a full declaration for all its subtypes recorded in
21691 -- Private_Dependents and swap them similarly to the base type. These
21692 -- are subtypes that have been define before the full declaration of
21693 -- the private type. We also swap the entry in Private_Dependents list
21694 -- so we can properly restore the private view on exit from the scope.
21695
21696 declare
21697 Priv_Elmt : Elmt_Id;
21698 Priv_Scop : Entity_Id;
21699 Priv : Entity_Id;
21700 Full : Entity_Id;
21701
21702 begin
21703 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21704 while Present (Priv_Elmt) loop
21705 Priv := Node (Priv_Elmt);
21706 Priv_Scop := Scope (Priv);
21707
21708 if Ekind (Priv) in E_Private_Subtype
21709 | E_Limited_Private_Subtype
21710 | E_Record_Subtype_With_Private
21711 then
21712 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21713 Set_Is_Itype (Full);
21714 Set_Parent (Full, Parent (Priv));
21715 Set_Associated_Node_For_Itype (Full, N);
21716
21717 -- Now we need to complete the private subtype, but since the
21718 -- base type has already been swapped, we must also swap the
21719 -- subtypes (and thus, reverse the arguments in the call to
21720 -- Complete_Private_Subtype). Also note that we may need to
21721 -- re-establish the scope of the private subtype.
21722
21723 Copy_And_Swap (Priv, Full);
21724
21725 if not In_Open_Scopes (Priv_Scop) then
21726 Push_Scope (Priv_Scop);
21727
21728 else
21729 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21730
21731 Priv_Scop := Empty;
21732 end if;
21733
21734 Complete_Private_Subtype (Full, Priv, Full_T, N);
21735 Set_Full_View (Full, Priv);
21736
21737 if Present (Priv_Scop) then
21738 Pop_Scope;
21739 end if;
21740
21741 Replace_Elmt (Priv_Elmt, Full);
21742 end if;
21743
21744 Next_Elmt (Priv_Elmt);
21745 end loop;
21746 end;
21747
21748 declare
21749 Disp_Typ : Entity_Id;
21750 Full_List : Elist_Id;
21751 Prim : Entity_Id;
21752 Prim_Elmt : Elmt_Id;
21753 Priv_List : Elist_Id;
21754
21755 function Contains
21756 (E : Entity_Id;
21757 L : Elist_Id) return Boolean;
21758 -- Determine whether list L contains element E
21759
21760 --------------
21761 -- Contains --
21762 --------------
21763
21764 function Contains
21765 (E : Entity_Id;
21766 L : Elist_Id) return Boolean
21767 is
21768 List_Elmt : Elmt_Id;
21769
21770 begin
21771 List_Elmt := First_Elmt (L);
21772 while Present (List_Elmt) loop
21773 if Node (List_Elmt) = E then
21774 return True;
21775 end if;
21776
21777 Next_Elmt (List_Elmt);
21778 end loop;
21779
21780 return False;
21781 end Contains;
21782
21783 -- Start of processing
21784
21785 begin
21786 -- If the private view was tagged, copy the new primitive operations
21787 -- from the private view to the full view.
21788
21789 if Is_Tagged_Type (Full_T) then
21790 if Is_Tagged_Type (Priv_T) then
21791 Priv_List := Primitive_Operations (Priv_T);
21792 Prim_Elmt := First_Elmt (Priv_List);
21793
21794 -- In the case of a concurrent type completing a private tagged
21795 -- type, primitives may have been declared in between the two
21796 -- views. These subprograms need to be wrapped the same way
21797 -- entries and protected procedures are handled because they
21798 -- cannot be directly shared by the two views.
21799
21800 if Is_Concurrent_Type (Full_T) then
21801 declare
21802 Conc_Typ : constant Entity_Id :=
21803 Corresponding_Record_Type (Full_T);
21804 Curr_Nod : Node_Id := Parent (Conc_Typ);
21805 Wrap_Spec : Node_Id;
21806
21807 begin
21808 while Present (Prim_Elmt) loop
21809 Prim := Node (Prim_Elmt);
21810
21811 if Comes_From_Source (Prim)
21812 and then not Is_Abstract_Subprogram (Prim)
21813 then
21814 Wrap_Spec :=
21815 Make_Subprogram_Declaration (Sloc (Prim),
21816 Specification =>
21817 Build_Wrapper_Spec
21818 (Subp_Id => Prim,
21819 Obj_Typ => Conc_Typ,
21820 Formals =>
21821 Parameter_Specifications
21822 (Parent (Prim))));
21823
21824 Insert_After (Curr_Nod, Wrap_Spec);
21825 Curr_Nod := Wrap_Spec;
21826
21827 Analyze (Wrap_Spec);
21828
21829 -- Remove the wrapper from visibility to avoid
21830 -- spurious conflict with the wrapped entity.
21831
21832 Set_Is_Immediately_Visible
21833 (Defining_Entity (Specification (Wrap_Spec)),
21834 False);
21835 end if;
21836
21837 Next_Elmt (Prim_Elmt);
21838 end loop;
21839
21840 goto Leave;
21841 end;
21842
21843 -- For nonconcurrent types, transfer explicit primitives, but
21844 -- omit those inherited from the parent of the private view
21845 -- since they will be re-inherited later on.
21846
21847 else
21848 Full_List := Primitive_Operations (Full_T);
21849 while Present (Prim_Elmt) loop
21850 Prim := Node (Prim_Elmt);
21851
21852 if Comes_From_Source (Prim)
21853 and then not Contains (Prim, Full_List)
21854 then
21855 Append_Elmt (Prim, Full_List);
21856 end if;
21857
21858 Next_Elmt (Prim_Elmt);
21859 end loop;
21860 end if;
21861
21862 -- Untagged private view
21863
21864 else
21865 Full_List := Primitive_Operations (Full_T);
21866
21867 -- In this case the partial view is untagged, so here we locate
21868 -- all of the earlier primitives that need to be treated as
21869 -- dispatching (those that appear between the two views). Note
21870 -- that these additional operations must all be new operations
21871 -- (any earlier operations that override inherited operations
21872 -- of the full view will already have been inserted in the
21873 -- primitives list, marked by Check_Operation_From_Private_View
21874 -- as dispatching. Note that implicit "/=" operators are
21875 -- excluded from being added to the primitives list since they
21876 -- shouldn't be treated as dispatching (tagged "/=" is handled
21877 -- specially).
21878
21879 Prim := Next_Entity (Full_T);
21880 while Present (Prim) and then Prim /= Priv_T loop
21881 if Ekind (Prim) in E_Procedure | E_Function then
21882 Disp_Typ := Find_Dispatching_Type (Prim);
21883
21884 if Disp_Typ = Full_T
21885 and then (Chars (Prim) /= Name_Op_Ne
21886 or else Comes_From_Source (Prim))
21887 then
21888 Check_Controlling_Formals (Full_T, Prim);
21889
21890 if Is_Suitable_Primitive (Prim)
21891 and then not Is_Dispatching_Operation (Prim)
21892 then
21893 Append_Elmt (Prim, Full_List);
21894 Set_Is_Dispatching_Operation (Prim);
21895 Set_DT_Position_Value (Prim, No_Uint);
21896 end if;
21897
21898 elsif Is_Dispatching_Operation (Prim)
21899 and then Disp_Typ /= Full_T
21900 then
21901 -- Verify that it is not otherwise controlled by a
21902 -- formal or a return value of type T.
21903
21904 Check_Controlling_Formals (Disp_Typ, Prim);
21905 end if;
21906 end if;
21907
21908 Next_Entity (Prim);
21909 end loop;
21910 end if;
21911
21912 -- For the tagged case, the two views can share the same primitive
21913 -- operations list and the same class-wide type. Update attributes
21914 -- of the class-wide type which depend on the full declaration.
21915
21916 if Is_Tagged_Type (Priv_T) then
21917 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21918 Set_Class_Wide_Type
21919 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21920
21921 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21922 end if;
21923
21924 -- For untagged types, copy the primitives across from the private
21925 -- view to the full view, for support of prefixed calls when
21926 -- extensions are enabled, and better error messages otherwise.
21927
21928 else
21929 Priv_List := Primitive_Operations (Priv_T);
21930 Prim_Elmt := First_Elmt (Priv_List);
21931
21932 Full_List := Primitive_Operations (Full_T);
21933 while Present (Prim_Elmt) loop
21934 Prim := Node (Prim_Elmt);
21935 Append_Elmt (Prim, Full_List);
21936 Next_Elmt (Prim_Elmt);
21937 end loop;
21938 end if;
21939 end;
21940
21941 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21942
21943 if Known_To_Have_Preelab_Init (Priv_T) then
21944
21945 -- Case where there is a pragma Preelaborable_Initialization. We
21946 -- always allow this in predefined units, which is cheating a bit,
21947 -- but it means we don't have to struggle to meet the requirements in
21948 -- the RM for having Preelaborable Initialization. Otherwise we
21949 -- require that the type meets the RM rules. But we can't check that
21950 -- yet, because of the rule about overriding Initialize, so we simply
21951 -- set a flag that will be checked at freeze time.
21952
21953 if not In_Predefined_Unit (Full_T) then
21954 Set_Must_Have_Preelab_Init (Full_T);
21955 end if;
21956 end if;
21957
21958 -- If pragma CPP_Class was applied to the private type declaration,
21959 -- propagate it now to the full type declaration.
21960
21961 if Is_CPP_Class (Priv_T) then
21962 Set_Is_CPP_Class (Full_T);
21963 Set_Convention (Full_T, Convention_CPP);
21964
21965 -- Check that components of imported CPP types do not have default
21966 -- expressions.
21967
21968 Check_CPP_Type_Has_No_Defaults (Full_T);
21969 end if;
21970
21971 -- If the private view has user specified stream attributes, then so has
21972 -- the full view.
21973
21974 -- Why the test, how could these flags be already set in Full_T ???
21975
21976 if Has_Specified_Stream_Read (Priv_T) then
21977 Set_Has_Specified_Stream_Read (Full_T);
21978 end if;
21979
21980 if Has_Specified_Stream_Write (Priv_T) then
21981 Set_Has_Specified_Stream_Write (Full_T);
21982 end if;
21983
21984 if Has_Specified_Stream_Input (Priv_T) then
21985 Set_Has_Specified_Stream_Input (Full_T);
21986 end if;
21987
21988 if Has_Specified_Stream_Output (Priv_T) then
21989 Set_Has_Specified_Stream_Output (Full_T);
21990 end if;
21991
21992 -- Propagate Default_Initial_Condition-related attributes from the
21993 -- partial view to the full view.
21994
21995 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21996
21997 -- And to the underlying full view, if any
21998
21999 if Is_Private_Type (Full_T)
22000 and then Present (Underlying_Full_View (Full_T))
22001 then
22002 Propagate_DIC_Attributes
22003 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
22004 end if;
22005
22006 -- Propagate invariant-related attributes from the partial view to the
22007 -- full view.
22008
22009 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
22010
22011 -- And to the underlying full view, if any
22012
22013 if Is_Private_Type (Full_T)
22014 and then Present (Underlying_Full_View (Full_T))
22015 then
22016 Propagate_Invariant_Attributes
22017 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
22018 end if;
22019
22020 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
22021 -- in the full view without advertising the inheritance in the partial
22022 -- view. This can only occur when the partial view has no parent type
22023 -- and the full view has an interface as a parent. Any other scenarios
22024 -- are illegal because implemented interfaces must match between the
22025 -- two views.
22026
22027 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
22028 declare
22029 Full_Par : constant Entity_Id := Etype (Full_T);
22030 Priv_Par : constant Entity_Id := Etype (Priv_T);
22031
22032 begin
22033 if not Is_Interface (Priv_Par)
22034 and then Is_Interface (Full_Par)
22035 and then Has_Inheritable_Invariants (Full_Par)
22036 then
22037 Error_Msg_N
22038 ("hidden inheritance of class-wide type invariants not "
22039 & "allowed", N);
22040 end if;
22041 end;
22042 end if;
22043
22044 -- Propagate First_Controlling_Parameter aspect to the full type
22045
22046 if Is_Tagged_Type (Priv_T)
22047 and then Has_First_Controlling_Parameter_Aspect (Priv_T)
22048 then
22049 Set_Has_First_Controlling_Parameter_Aspect (Full_T);
22050 end if;
22051
22052 -- Propagate predicates to full type, and predicate function if already
22053 -- defined. It is not clear that this can actually happen? the partial
22054 -- view cannot be frozen yet, and the predicate function has not been
22055 -- built. Still it is a cheap check and seems safer to make it.
22056
22057 Propagate_Predicate_Attributes (Full_T, Priv_T);
22058
22059 if Is_Private_Type (Full_T)
22060 and then Present (Underlying_Full_View (Full_T))
22061 then
22062 Propagate_Predicate_Attributes
22063 (Underlying_Full_View (Full_T), Priv_T);
22064 end if;
22065
22066 <<Leave>>
22067 Restore_Ghost_Region (Saved_GM, Saved_IGR);
22068 end Process_Full_View;
22069
22070 -----------------------------------
22071 -- Process_Incomplete_Dependents --
22072 -----------------------------------
22073
22074 procedure Process_Incomplete_Dependents
22075 (N : Node_Id;
22076 Full_T : Entity_Id;
22077 Inc_T : Entity_Id)
22078 is
22079 Inc_Elmt : Elmt_Id;
22080 Priv_Dep : Entity_Id;
22081 New_Subt : Entity_Id;
22082
22083 Disc_Constraint : Elist_Id;
22084
22085 begin
22086 if No (Private_Dependents (Inc_T)) then
22087 return;
22088 end if;
22089
22090 -- Itypes that may be generated by the completion of an incomplete
22091 -- subtype are not used by the back-end and not attached to the tree.
22092 -- They are created only for constraint-checking purposes.
22093
22094 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
22095 while Present (Inc_Elmt) loop
22096 Priv_Dep := Node (Inc_Elmt);
22097
22098 if Ekind (Priv_Dep) = E_Subprogram_Type then
22099
22100 -- An Access_To_Subprogram type may have a return type or a
22101 -- parameter type that is incomplete. Replace with the full view.
22102
22103 if Etype (Priv_Dep) = Inc_T then
22104 Set_Etype (Priv_Dep, Full_T);
22105 end if;
22106
22107 declare
22108 Formal : Entity_Id;
22109
22110 begin
22111 Formal := First_Formal (Priv_Dep);
22112 while Present (Formal) loop
22113 if Etype (Formal) = Inc_T then
22114 Set_Etype (Formal, Full_T);
22115 end if;
22116
22117 Next_Formal (Formal);
22118 end loop;
22119 end;
22120
22121 elsif Is_Overloadable (Priv_Dep) then
22122
22123 -- If a subprogram in the incomplete dependents list is primitive
22124 -- for a tagged full type then mark it as a dispatching operation,
22125 -- check whether it overrides an inherited subprogram, and check
22126 -- restrictions on its controlling formals. Note that a protected
22127 -- operation is never dispatching: only its wrapper operation
22128 -- (which has convention Ada) is.
22129
22130 if Is_Tagged_Type (Full_T)
22131 and then Is_Primitive (Priv_Dep)
22132 and then Convention (Priv_Dep) /= Convention_Protected
22133 then
22134 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
22135 Set_Is_Dispatching_Operation (Priv_Dep);
22136 Check_Controlling_Formals (Full_T, Priv_Dep);
22137 end if;
22138
22139 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
22140
22141 -- Can happen during processing of a body before the completion
22142 -- of a TA type. Ignore, because spec is also on dependent list.
22143
22144 return;
22145
22146 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
22147 -- corresponding subtype of the full view.
22148
22149 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
22150 and then Comes_From_Source (Priv_Dep)
22151 then
22152 Set_Subtype_Indication
22153 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
22154 Reinit_Field_To_Zero
22155 (Priv_Dep, F_Private_Dependents,
22156 Old_Ekind => E_Incomplete_Subtype);
22157 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
22158 Set_Etype (Priv_Dep, Full_T);
22159 Set_Analyzed (Parent (Priv_Dep), False);
22160
22161 -- Reanalyze the declaration, suppressing the call to Enter_Name
22162 -- to avoid duplicate names.
22163
22164 Analyze_Subtype_Declaration
22165 (N => Parent (Priv_Dep),
22166 Skip => True);
22167
22168 -- Dependent is a subtype
22169
22170 else
22171 -- We build a new subtype indication using the full view of the
22172 -- incomplete parent. The discriminant constraints have been
22173 -- elaborated already at the point of the subtype declaration.
22174
22175 New_Subt := Create_Itype (E_Void, N);
22176
22177 if Has_Discriminants (Full_T) then
22178 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
22179 else
22180 Disc_Constraint := No_Elist;
22181 end if;
22182
22183 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
22184 Set_Full_View (Priv_Dep, New_Subt);
22185 end if;
22186
22187 Next_Elmt (Inc_Elmt);
22188 end loop;
22189 end Process_Incomplete_Dependents;
22190
22191 --------------------------------
22192 -- Process_Range_Expr_In_Decl --
22193 --------------------------------
22194
22195 procedure Process_Range_Expr_In_Decl
22196 (R : Node_Id;
22197 T : Entity_Id;
22198 Subtyp : Entity_Id := Empty;
22199 Check_List : List_Id := No_List)
22200 is
22201 Lo, Hi : Node_Id;
22202 R_Checks : Check_Result;
22203 Insert_Node : Node_Id;
22204 Def_Id : Entity_Id;
22205
22206 begin
22207 Analyze_And_Resolve (R, Base_Type (T));
22208
22209 if Nkind (R) = N_Range then
22210 Lo := Low_Bound (R);
22211 Hi := High_Bound (R);
22212
22213 -- Validity checks on the range of a quantified expression are
22214 -- delayed until the construct is transformed into a loop.
22215
22216 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
22217 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
22218 then
22219 null;
22220
22221 -- We need to ensure validity of the bounds here, because if we
22222 -- go ahead and do the expansion, then the expanded code will get
22223 -- analyzed with range checks suppressed and we miss the check.
22224
22225 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
22226 -- the temporaries generated by routine Remove_Side_Effects by means
22227 -- of validity checks must use the same names. When a range appears
22228 -- in the parent of a generic, the range is processed with checks
22229 -- disabled as part of the generic context and with checks enabled
22230 -- for code generation purposes. This leads to link issues as the
22231 -- generic contains references to xxx_FIRST/_LAST, but the inlined
22232 -- template sees the temporaries generated by Remove_Side_Effects.
22233
22234 else
22235 Validity_Check_Range (R, Subtyp);
22236 end if;
22237
22238 -- If there were errors in the declaration, try and patch up some
22239 -- common mistakes in the bounds. The cases handled are literals
22240 -- which are Integer where the expected type is Real and vice versa.
22241 -- These corrections allow the compilation process to proceed further
22242 -- along since some basic assumptions of the format of the bounds
22243 -- are guaranteed.
22244
22245 if Etype (R) = Any_Type then
22246 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
22247 Rewrite (Lo,
22248 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
22249
22250 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
22251 Rewrite (Hi,
22252 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
22253
22254 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
22255 Rewrite (Lo,
22256 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
22257
22258 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
22259 Rewrite (Hi,
22260 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
22261 end if;
22262
22263 Set_Etype (Lo, T);
22264 Set_Etype (Hi, T);
22265 end if;
22266
22267 -- If the bounds of the range have been mistakenly given as string
22268 -- literals (perhaps in place of character literals), then an error
22269 -- has already been reported, but we rewrite the string literal as a
22270 -- bound of the range's type to avoid blowups in later processing
22271 -- that looks at static values.
22272
22273 if Nkind (Lo) = N_String_Literal then
22274 Rewrite (Lo,
22275 Make_Attribute_Reference (Sloc (Lo),
22276 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
22277 Attribute_Name => Name_First));
22278 Analyze_And_Resolve (Lo);
22279 end if;
22280
22281 if Nkind (Hi) = N_String_Literal then
22282 Rewrite (Hi,
22283 Make_Attribute_Reference (Sloc (Hi),
22284 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
22285 Attribute_Name => Name_First));
22286 Analyze_And_Resolve (Hi);
22287 end if;
22288
22289 -- If bounds aren't scalar at this point then exit, avoiding
22290 -- problems with further processing of the range in this procedure.
22291
22292 if not Is_Scalar_Type (Etype (Lo)) then
22293 return;
22294 end if;
22295
22296 -- Resolve (actually Sem_Eval) has checked that the bounds are in
22297 -- then range of the base type. Here we check whether the bounds
22298 -- are in the range of the subtype itself. Note that if the bounds
22299 -- represent the null range the Constraint_Error exception should
22300 -- not be raised.
22301
22302 -- Capture values of bounds and generate temporaries for them
22303 -- if needed, before applying checks, since checks may cause
22304 -- duplication of the expression without forcing evaluation.
22305
22306 -- The forced evaluation removes side effects from expressions,
22307 -- which should occur also in GNATprove mode. Otherwise, we end up
22308 -- with unexpected insertions of actions at places where this is
22309 -- not supposed to occur, e.g. on default parameters of a call.
22310
22311 if Expander_Active or GNATprove_Mode then
22312
22313 -- Call Force_Evaluation to create declarations as needed
22314 -- to deal with side effects, and also create typ_FIRST/LAST
22315 -- entities for bounds if we have a subtype name.
22316
22317 -- Note: we do this transformation even if expansion is not
22318 -- active if we are in GNATprove_Mode since the transformation
22319 -- is in general required to ensure that the resulting tree has
22320 -- proper Ada semantics.
22321
22322 Force_Evaluation
22323 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22324 Force_Evaluation
22325 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22326 end if;
22327
22328 -- We use a flag here instead of suppressing checks on the type
22329 -- because the type we check against isn't necessarily the place
22330 -- where we put the check.
22331
22332 R_Checks := Get_Range_Checks (R, T);
22333
22334 -- Look up tree to find an appropriate insertion point. We can't
22335 -- just use insert_actions because later processing depends on
22336 -- the insertion node. Prior to Ada 2012 the insertion point could
22337 -- only be a declaration or a loop, but quantified expressions can
22338 -- appear within any context in an expression, and the insertion
22339 -- point can be any statement, pragma, or declaration.
22340
22341 Insert_Node := Parent (R);
22342 while Present (Insert_Node) loop
22343 exit when
22344 Nkind (Insert_Node) in N_Declaration
22345 and then
22346 Nkind (Insert_Node) not in N_Component_Declaration
22347 | N_Loop_Parameter_Specification
22348 | N_Function_Specification
22349 | N_Procedure_Specification;
22350
22351 exit when Nkind (Insert_Node) in
22352 N_Later_Decl_Item |
22353 N_Statement_Other_Than_Procedure_Call |
22354 N_Procedure_Call_Statement |
22355 N_Pragma;
22356
22357 Insert_Node := Parent (Insert_Node);
22358 end loop;
22359
22360 if Present (Insert_Node) then
22361
22362 -- Case of loop statement. Verify that the range is part of the
22363 -- subtype indication of the iteration scheme.
22364
22365 if Nkind (Insert_Node) = N_Loop_Statement then
22366 declare
22367 Indic : Node_Id;
22368
22369 begin
22370 Indic := Parent (R);
22371 while Present (Indic)
22372 and then Nkind (Indic) /= N_Subtype_Indication
22373 loop
22374 Indic := Parent (Indic);
22375 end loop;
22376
22377 if Present (Indic) then
22378 Def_Id := Etype (Subtype_Mark (Indic));
22379
22380 Insert_Range_Checks
22381 (R_Checks,
22382 Insert_Node,
22383 Def_Id,
22384 Sloc (Insert_Node),
22385 Do_Before => True);
22386 end if;
22387 end;
22388
22389 -- Case of declarations. If the declaration is for a type and
22390 -- involves discriminants, the checks are premature at the
22391 -- declaration point and need to wait for the expansion of the
22392 -- initialization procedure, which will pass in the list to put
22393 -- them on; otherwise, the checks are done at the declaration
22394 -- point and there is no need to do them again in the
22395 -- initialization procedure.
22396
22397 elsif Nkind (Insert_Node) in N_Declaration then
22398 Def_Id := Defining_Identifier (Insert_Node);
22399
22400 if (Ekind (Def_Id) = E_Record_Type
22401 and then Depends_On_Discriminant (R))
22402 or else
22403 (Ekind (Def_Id) = E_Protected_Type
22404 and then Has_Discriminants (Def_Id))
22405 then
22406 if Present (Check_List) then
22407 Append_Range_Checks
22408 (R_Checks,
22409 Check_List, Def_Id, Sloc (Insert_Node));
22410 end if;
22411
22412 else
22413 if No (Check_List) then
22414 Insert_Range_Checks
22415 (R_Checks,
22416 Insert_Node, Def_Id, Sloc (Insert_Node));
22417 end if;
22418 end if;
22419
22420 -- Case of statements. Drop the checks, as the range appears in
22421 -- the context of a quantified expression. Insertion will take
22422 -- place when expression is expanded.
22423
22424 else
22425 null;
22426 end if;
22427 end if;
22428
22429 -- Case of other than an explicit N_Range node
22430
22431 -- The forced evaluation removes side effects from expressions, which
22432 -- should occur also in GNATprove mode. Otherwise, we end up with
22433 -- unexpected insertions of actions at places where this is not
22434 -- supposed to occur, e.g. on default parameters of a call.
22435
22436 elsif Expander_Active or GNATprove_Mode then
22437 Get_Index_Bounds (R, Lo, Hi);
22438 Force_Evaluation (Lo);
22439 Force_Evaluation (Hi);
22440 end if;
22441 end Process_Range_Expr_In_Decl;
22442
22443 --------------------------------------
22444 -- Process_Real_Range_Specification --
22445 --------------------------------------
22446
22447 procedure Process_Real_Range_Specification (Def : Node_Id) is
22448 Spec : constant Node_Id := Real_Range_Specification (Def);
22449 Lo : Node_Id;
22450 Hi : Node_Id;
22451 Err : Boolean := False;
22452
22453 procedure Analyze_Bound (N : Node_Id);
22454 -- Analyze and check one bound
22455
22456 -------------------
22457 -- Analyze_Bound --
22458 -------------------
22459
22460 procedure Analyze_Bound (N : Node_Id) is
22461 begin
22462 Analyze_And_Resolve (N, Any_Real);
22463
22464 if not Is_OK_Static_Expression (N) then
22465 Flag_Non_Static_Expr
22466 ("bound in real type definition is not static!", N);
22467 Err := True;
22468 end if;
22469 end Analyze_Bound;
22470
22471 -- Start of processing for Process_Real_Range_Specification
22472
22473 begin
22474 if Present (Spec) then
22475 Lo := Low_Bound (Spec);
22476 Hi := High_Bound (Spec);
22477 Analyze_Bound (Lo);
22478 Analyze_Bound (Hi);
22479
22480 -- If error, clear away junk range specification
22481
22482 if Err then
22483 Set_Real_Range_Specification (Def, Empty);
22484 end if;
22485 end if;
22486 end Process_Real_Range_Specification;
22487
22488 ---------------------
22489 -- Process_Subtype --
22490 ---------------------
22491
22492 function Process_Subtype
22493 (S : Node_Id;
22494 Related_Nod : Node_Id;
22495 Related_Id : Entity_Id := Empty;
22496 Suffix : Character := ' ') return Entity_Id
22497 is
22498 procedure Check_Incomplete (T : Node_Id);
22499 -- Called to verify that an incomplete type is not used prematurely
22500
22501 ----------------------
22502 -- Check_Incomplete --
22503 ----------------------
22504
22505 procedure Check_Incomplete (T : Node_Id) is
22506 begin
22507 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22508
22509 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22510 and then
22511 not (Ada_Version >= Ada_2005
22512 and then
22513 (Nkind (Parent (T)) = N_Subtype_Declaration
22514 or else (Nkind (Parent (T)) = N_Subtype_Indication
22515 and then Nkind (Parent (Parent (T))) =
22516 N_Subtype_Declaration)))
22517 then
22518 Error_Msg_N ("invalid use of type before its full declaration", T);
22519 end if;
22520 end Check_Incomplete;
22521
22522 -- Local variables
22523
22524 P : Node_Id;
22525 Def_Id : Entity_Id;
22526 Error_Node : Node_Id;
22527 Full_View_Id : Entity_Id;
22528 Subtype_Mark_Id : Entity_Id;
22529
22530 May_Have_Null_Exclusion : Boolean;
22531
22532 -- Start of processing for Process_Subtype
22533
22534 begin
22535 -- Case of no constraints present
22536
22537 if Nkind (S) /= N_Subtype_Indication then
22538 Find_Type (S);
22539
22540 -- No way to proceed if the subtype indication is malformed. This
22541 -- will happen for example when the subtype indication in an object
22542 -- declaration is missing altogether and the expression is analyzed
22543 -- as if it were that indication.
22544
22545 if not Is_Entity_Name (S) then
22546 return Any_Type;
22547 end if;
22548
22549 Check_Incomplete (S);
22550 P := Parent (S);
22551
22552 -- The following mirroring of assertion in Null_Exclusion_Present is
22553 -- ugly, can't we have a range, a static predicate or even a flag???
22554
22555 May_Have_Null_Exclusion :=
22556 Present (P)
22557 and then
22558 Nkind (P) in N_Access_Definition
22559 | N_Access_Function_Definition
22560 | N_Access_Procedure_Definition
22561 | N_Access_To_Object_Definition
22562 | N_Allocator
22563 | N_Component_Definition
22564 | N_Derived_Type_Definition
22565 | N_Discriminant_Specification
22566 | N_Formal_Object_Declaration
22567 | N_Function_Specification
22568 | N_Object_Declaration
22569 | N_Object_Renaming_Declaration
22570 | N_Parameter_Specification
22571 | N_Subtype_Declaration;
22572
22573 -- Ada 2005 (AI-231): Static check
22574
22575 if Ada_Version >= Ada_2005
22576 and then May_Have_Null_Exclusion
22577 and then Null_Exclusion_Present (P)
22578 and then Nkind (P) /= N_Access_To_Object_Definition
22579 and then not Is_Access_Type (Entity (S))
22580 then
22581 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22582 end if;
22583
22584 -- Create an Itype that is a duplicate of Entity (S) but with the
22585 -- null-exclusion attribute.
22586
22587 if May_Have_Null_Exclusion
22588 and then Is_Access_Type (Entity (S))
22589 and then Null_Exclusion_Present (P)
22590
22591 -- No need to check the case of an access to object definition.
22592 -- It is correct to define double not-null pointers.
22593
22594 -- Example:
22595 -- type Not_Null_Int_Ptr is not null access Integer;
22596 -- type Acc is not null access Not_Null_Int_Ptr;
22597
22598 and then Nkind (P) /= N_Access_To_Object_Definition
22599 then
22600 if Can_Never_Be_Null (Entity (S)) then
22601 case Nkind (Related_Nod) is
22602 when N_Full_Type_Declaration =>
22603 if Nkind (Type_Definition (Related_Nod))
22604 in N_Array_Type_Definition
22605 then
22606 Error_Node :=
22607 Subtype_Indication
22608 (Component_Definition
22609 (Type_Definition (Related_Nod)));
22610 else
22611 Error_Node :=
22612 Subtype_Indication (Type_Definition (Related_Nod));
22613 end if;
22614
22615 when N_Subtype_Declaration =>
22616 Error_Node := Subtype_Indication (Related_Nod);
22617
22618 when N_Object_Declaration =>
22619 Error_Node := Object_Definition (Related_Nod);
22620
22621 when N_Component_Declaration =>
22622 Error_Node :=
22623 Subtype_Indication (Component_Definition (Related_Nod));
22624
22625 when N_Allocator =>
22626 Error_Node := Expression (Related_Nod);
22627
22628 when others =>
22629 pragma Assert (False);
22630 Error_Node := Related_Nod;
22631 end case;
22632
22633 Error_Msg_NE
22634 ("`NOT NULL` not allowed (& already excludes null)",
22635 Error_Node,
22636 Entity (S));
22637 end if;
22638
22639 Set_Etype (S,
22640 Create_Null_Excluding_Itype
22641 (T => Entity (S),
22642 Related_Nod => P));
22643 Set_Entity (S, Etype (S));
22644 end if;
22645
22646 return Entity (S);
22647
22648 -- Case of constraint present, so that we have an N_Subtype_Indication
22649 -- node (this node is created only if constraints are present).
22650
22651 else
22652 Find_Type (Subtype_Mark (S));
22653
22654 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22655 and then not
22656 (Nkind (Parent (S)) = N_Subtype_Declaration
22657 and then Is_Itype (Defining_Identifier (Parent (S))))
22658 then
22659 Check_Incomplete (Subtype_Mark (S));
22660 end if;
22661
22662 P := Parent (S);
22663 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22664
22665 -- Explicit subtype declaration case
22666
22667 if Nkind (P) = N_Subtype_Declaration then
22668 Def_Id := Defining_Identifier (P);
22669
22670 -- Explicit derived type definition case
22671
22672 elsif Nkind (P) = N_Derived_Type_Definition then
22673 Def_Id := Defining_Identifier (Parent (P));
22674
22675 -- Implicit case, the Def_Id must be created as an implicit type.
22676 -- The one exception arises in the case of concurrent types, array
22677 -- and access types, where other subsidiary implicit types may be
22678 -- created and must appear before the main implicit type. In these
22679 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22680 -- has not yet been called to create Def_Id.
22681
22682 else
22683 if Is_Array_Type (Subtype_Mark_Id)
22684 or else Is_Concurrent_Type (Subtype_Mark_Id)
22685 or else Is_Access_Type (Subtype_Mark_Id)
22686 then
22687 Def_Id := Empty;
22688
22689 -- For the other cases, we create a new unattached Itype,
22690 -- and set the indication to ensure it gets attached later.
22691
22692 else
22693 Def_Id :=
22694 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22695 end if;
22696 end if;
22697
22698 -- If the kind of constraint is invalid for this kind of type,
22699 -- then give an error, and then pretend no constraint was given.
22700
22701 if not Is_Valid_Constraint_Kind
22702 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22703 then
22704 Error_Msg_N
22705 ("incorrect constraint for this kind of type", Constraint (S));
22706
22707 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22708
22709 -- Set Ekind of orphan itype, to prevent cascaded errors
22710
22711 if Present (Def_Id) then
22712 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22713 end if;
22714
22715 -- Make recursive call, having got rid of the bogus constraint
22716
22717 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22718 end if;
22719
22720 -- Remaining processing depends on type. Select on Base_Type kind to
22721 -- ensure getting to the concrete type kind in the case of a private
22722 -- subtype (needed when only doing semantic analysis).
22723
22724 case Ekind (Base_Type (Subtype_Mark_Id)) is
22725 when Access_Kind =>
22726
22727 -- If this is a constraint on a class-wide type, discard it.
22728 -- There is currently no way to express a partial discriminant
22729 -- constraint on a type with unknown discriminants. This is
22730 -- a pathology that the ACATS wisely decides not to test.
22731
22732 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22733 if Comes_From_Source (S) then
22734 Error_Msg_N
22735 ("constraint on class-wide type ignored??",
22736 Constraint (S));
22737 end if;
22738
22739 if Nkind (P) = N_Subtype_Declaration then
22740 Set_Subtype_Indication (P,
22741 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22742 end if;
22743
22744 return Subtype_Mark_Id;
22745 end if;
22746
22747 Constrain_Access (Def_Id, S, Related_Nod);
22748
22749 if Expander_Active
22750 and then Is_Itype (Designated_Type (Def_Id))
22751 and then Nkind (Related_Nod) = N_Subtype_Declaration
22752 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22753 then
22754 Build_Itype_Reference
22755 (Designated_Type (Def_Id), Related_Nod);
22756 end if;
22757
22758 when Array_Kind =>
22759 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22760
22761 when Decimal_Fixed_Point_Kind =>
22762 Constrain_Decimal (Def_Id, S);
22763
22764 when Enumeration_Kind =>
22765 Constrain_Enumeration (Def_Id, S);
22766
22767 when Ordinary_Fixed_Point_Kind =>
22768 Constrain_Ordinary_Fixed (Def_Id, S);
22769
22770 when Float_Kind =>
22771 Constrain_Float (Def_Id, S);
22772
22773 when Integer_Kind =>
22774 Constrain_Integer (Def_Id, S);
22775
22776 when Class_Wide_Kind
22777 | E_Incomplete_Type
22778 | E_Record_Subtype
22779 | E_Record_Type
22780 =>
22781 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22782
22783 if Ekind (Def_Id) = E_Incomplete_Type then
22784 Set_Private_Dependents (Def_Id, New_Elmt_List);
22785 end if;
22786
22787 when Private_Kind =>
22788
22789 -- A private type with unknown discriminants may be completed
22790 -- by an unconstrained array type.
22791
22792 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22793 and then Present (Full_View (Subtype_Mark_Id))
22794 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22795 then
22796 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22797
22798 -- ... but more commonly is completed by a discriminated record
22799 -- type.
22800
22801 else
22802 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22803 end if;
22804
22805 -- The base type may be private but Def_Id may be a full view
22806 -- in an instance.
22807
22808 if Is_Private_Type (Def_Id) then
22809 Set_Private_Dependents (Def_Id, New_Elmt_List);
22810 end if;
22811
22812 -- In case of an invalid constraint prevent further processing
22813 -- since the type constructed is missing expected fields.
22814
22815 if Etype (Def_Id) = Any_Type then
22816 return Def_Id;
22817 end if;
22818
22819 -- If the full view is that of a task with discriminants,
22820 -- we must constrain both the concurrent type and its
22821 -- corresponding record type. Otherwise we will just propagate
22822 -- the constraint to the full view, if available.
22823
22824 if Present (Full_View (Subtype_Mark_Id))
22825 and then Has_Discriminants (Subtype_Mark_Id)
22826 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22827 then
22828 Full_View_Id :=
22829 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22830
22831 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22832 Constrain_Concurrent (Full_View_Id, S,
22833 Related_Nod, Related_Id, Suffix);
22834 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22835 Set_Full_View (Def_Id, Full_View_Id);
22836
22837 -- Introduce an explicit reference to the private subtype,
22838 -- to prevent scope anomalies in gigi if first use appears
22839 -- in a nested context, e.g. a later function body.
22840 -- Should this be generated in other contexts than a full
22841 -- type declaration?
22842
22843 if Is_Itype (Def_Id)
22844 and then
22845 Nkind (Parent (P)) = N_Full_Type_Declaration
22846 then
22847 Build_Itype_Reference (Def_Id, Parent (P));
22848 end if;
22849
22850 else
22851 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22852 end if;
22853
22854 when Concurrent_Kind =>
22855 Constrain_Concurrent (Def_Id, S,
22856 Related_Nod, Related_Id, Suffix);
22857
22858 when others =>
22859 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22860 end case;
22861
22862 -- Size, Alignment, Representation aspects and Convention are always
22863 -- inherited from the base type.
22864
22865 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22866 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22867 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22868
22869 -- The anonymous subtype created for the subtype indication
22870 -- inherits the predicates of the parent.
22871
22872 if Has_Predicates (Subtype_Mark_Id) then
22873 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22874
22875 -- Indicate where the predicate function may be found
22876
22877 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22878 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22879 end if;
22880 end if;
22881
22882 return Def_Id;
22883 end if;
22884 end Process_Subtype;
22885
22886 -----------------------------
22887 -- Record_Type_Declaration --
22888 -----------------------------
22889
22890 procedure Record_Type_Declaration
22891 (T : Entity_Id;
22892 N : Node_Id;
22893 Prev : Entity_Id)
22894 is
22895 Def : constant Node_Id := Type_Definition (N);
22896 Is_Tagged : Boolean;
22897 Tag_Comp : Entity_Id;
22898
22899 begin
22900 -- These flags must be initialized before calling Process_Discriminants
22901 -- because this routine makes use of them.
22902
22903 Mutate_Ekind (T, E_Record_Type);
22904 Set_Etype (T, T);
22905 Reinit_Size_Align (T);
22906 Set_Interfaces (T, No_Elist);
22907 Set_Stored_Constraint (T, No_Elist);
22908 Set_Default_SSO (T);
22909 Set_No_Reordering (T, No_Component_Reordering);
22910
22911 -- Normal case
22912
22913 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22914 -- The flag Is_Tagged_Type might have already been set by
22915 -- Find_Type_Name if it detected an error for declaration T. This
22916 -- arises in the case of private tagged types where the full view
22917 -- omits the word tagged.
22918
22919 Is_Tagged :=
22920 Tagged_Present (Def)
22921 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22922
22923 Set_Is_Limited_Record (T, Limited_Present (Def));
22924
22925 if Is_Tagged then
22926 Set_Is_Tagged_Type (T, True);
22927 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22928 end if;
22929
22930 -- Type is abstract if full declaration carries keyword, or if
22931 -- previous partial view did.
22932
22933 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22934 or else Abstract_Present (Def));
22935
22936 else
22937 Is_Tagged := True;
22938 Analyze_Interface_Declaration (T, Def);
22939
22940 if Present (Discriminant_Specifications (N)) then
22941 Error_Msg_N
22942 ("interface types cannot have discriminants",
22943 Defining_Identifier
22944 (First (Discriminant_Specifications (N))));
22945 end if;
22946 end if;
22947
22948 -- First pass: if there are self-referential access components,
22949 -- create the required anonymous access type declarations, and if
22950 -- need be an incomplete type declaration for T itself.
22951
22952 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22953
22954 if Ada_Version >= Ada_2005
22955 and then Present (Interface_List (Def))
22956 then
22957 Check_Interfaces (N, Def);
22958
22959 declare
22960 Ifaces_List : Elist_Id;
22961
22962 begin
22963 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22964 -- already in the parents.
22965
22966 Collect_Interfaces
22967 (T => T,
22968 Ifaces_List => Ifaces_List,
22969 Exclude_Parents => True);
22970
22971 Set_Interfaces (T, Ifaces_List);
22972 end;
22973 end if;
22974
22975 -- Records constitute a scope for the component declarations within.
22976 -- The scope is created prior to the processing of these declarations.
22977 -- Discriminants are processed first, so that they are visible when
22978 -- processing the other components. The Ekind of the record type itself
22979 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22980
22981 -- Enter record scope
22982
22983 Push_Scope (T);
22984
22985 -- If an incomplete or private type declaration was already given for
22986 -- the type, then this scope already exists, and the discriminants have
22987 -- been declared within. We must verify that the full declaration
22988 -- matches the incomplete one.
22989
22990 Check_Or_Process_Discriminants (N, T, Prev);
22991
22992 Set_Is_Constrained (T, not Has_Discriminants (T));
22993 Set_Has_Delayed_Freeze (T, True);
22994
22995 -- For tagged types add a manually analyzed component corresponding
22996 -- to the component _tag, the corresponding piece of tree will be
22997 -- expanded as part of the freezing actions if it is not a CPP_Class.
22998
22999 if Is_Tagged then
23000
23001 -- Do not add the tag unless we are in expansion mode
23002
23003 if Expander_Active then
23004 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
23005 Enter_Name (Tag_Comp);
23006
23007 Mutate_Ekind (Tag_Comp, E_Component);
23008 Set_Is_Tag (Tag_Comp);
23009 Set_Is_Aliased (Tag_Comp);
23010 Set_Is_Independent (Tag_Comp);
23011 Set_Etype (Tag_Comp, RTE (RE_Tag));
23012 Set_DT_Entry_Count (Tag_Comp, No_Uint);
23013 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
23014 Reinit_Component_Location (Tag_Comp);
23015
23016 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
23017 -- implemented interfaces.
23018
23019 if Has_Interfaces (T) then
23020 Add_Interface_Tag_Components (N, T);
23021 end if;
23022 end if;
23023
23024 Make_Class_Wide_Type (T);
23025 Set_Direct_Primitive_Operations (T, New_Elmt_List);
23026 end if;
23027
23028 Record_Type_Definition (Def, Prev);
23029
23030 -- Exit from record scope
23031
23032 End_Scope;
23033
23034 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
23035 -- the implemented interfaces and associate them an aliased entity.
23036
23037 if Is_Tagged
23038 and then not Is_Empty_List (Interface_List (Def))
23039 then
23040 Derive_Progenitor_Subprograms (T, T);
23041 end if;
23042
23043 Warn_On_Inherently_Limited_Type (T);
23044
23045 Check_Function_Writable_Actuals (N);
23046 end Record_Type_Declaration;
23047
23048 ----------------------------
23049 -- Record_Type_Definition --
23050 ----------------------------
23051
23052 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
23053 Component : Entity_Id;
23054 Final_Storage_Only : Boolean := True;
23055 Relaxed_Finalization : Boolean := True;
23056 T : Entity_Id;
23057
23058 begin
23059 if Ekind (Prev_T) = E_Incomplete_Type then
23060 T := Full_View (Prev_T);
23061 else
23062 T := Prev_T;
23063 end if;
23064
23065 Set_Is_Not_Self_Hidden (T);
23066
23067 -- Ada 2005: Check whether an explicit "limited" is present in a derived
23068 -- type declaration.
23069
23070 if Parent_Kind (Def) = N_Derived_Type_Definition
23071 and then Limited_Present (Parent (Def))
23072 then
23073 Set_Is_Limited_Record (T);
23074 end if;
23075
23076 -- If the component list of a record type is defined by the reserved
23077 -- word null and there is no discriminant part, then the record type has
23078 -- no components and all records of the type are null records (RM 3.7)
23079 -- This procedure is also called to process the extension part of a
23080 -- record extension, in which case the current scope may have inherited
23081 -- components.
23082
23083 if Present (Def)
23084 and then Present (Component_List (Def))
23085 and then not Null_Present (Component_List (Def))
23086 then
23087 Analyze_Declarations (Component_Items (Component_List (Def)));
23088
23089 if Present (Variant_Part (Component_List (Def))) then
23090 Analyze (Variant_Part (Component_List (Def)));
23091 end if;
23092
23093 -- For tagged types, when compiling in Generate_Code mode, the _Tag
23094 -- component is added before the creation of the class-wide entity
23095 -- and it is shared by that entity once it is built.
23096
23097 -- However, when compiling in Check_Syntax or Check_Semantics modes,
23098 -- since the _Tag component is not added to the tagged type entity,
23099 -- the First_Entity of its class-wide type remains empty, and must
23100 -- be explicitly set to prevent spurious errors from being reported.
23101
23102 if Operating_Mode <= Check_Semantics
23103 and then Is_Tagged_Type (T)
23104 and then Present (First_Entity (T))
23105 and then No (First_Entity (Class_Wide_Type (T)))
23106 then
23107 Set_First_Entity (Class_Wide_Type (T), First_Entity (T));
23108 end if;
23109 end if;
23110
23111 -- After completing the semantic analysis of the record definition,
23112 -- record components, both new and inherited, are accessible. Set their
23113 -- kind accordingly. Exclude malformed itypes from illegal declarations,
23114 -- whose Ekind may be void.
23115
23116 Component := First_Entity (Current_Scope);
23117 while Present (Component) loop
23118 if Ekind (Component) = E_Void
23119 and then not Is_Itype (Component)
23120 then
23121 Mutate_Ekind (Component, E_Component);
23122 Reinit_Component_Location (Component);
23123 Set_Is_Not_Self_Hidden (Component);
23124 end if;
23125
23126 Propagate_Concurrent_Flags (T, Etype (Component));
23127
23128 if Ekind (Component) /= E_Component then
23129 null;
23130
23131 -- Do not set Has_Controlled_Component on a class-wide equivalent
23132 -- type. See Make_CW_Equivalent_Type.
23133
23134 elsif not Is_Class_Wide_Equivalent_Type (T)
23135 and then (Has_Controlled_Component (Etype (Component))
23136 or else (Chars (Component) /= Name_uParent
23137 and then Is_Controlled (Etype (Component))))
23138 then
23139 Set_Has_Controlled_Component (T);
23140 Final_Storage_Only :=
23141 Final_Storage_Only
23142 and then Finalize_Storage_Only (Etype (Component));
23143 Relaxed_Finalization :=
23144 Relaxed_Finalization
23145 and then Has_Relaxed_Finalization (Etype (Component));
23146 end if;
23147
23148 Next_Entity (Component);
23149 end loop;
23150
23151 -- For a type that is not directly controlled but has controlled
23152 -- components, Finalize_Storage_Only is set if all the controlled
23153 -- components are Finalize_Storage_Only. The same processing is
23154 -- appled to Has_Relaxed_Finalization.
23155
23156 if not Is_Controlled (T) and then Has_Controlled_Component (T) then
23157 Set_Finalize_Storage_Only (T, Final_Storage_Only);
23158 Set_Has_Relaxed_Finalization (T, Relaxed_Finalization);
23159 end if;
23160
23161 -- Place reference to end record on the proper entity, which may
23162 -- be a partial view.
23163
23164 if Present (Def) then
23165 Process_End_Label (Def, 'e', Prev_T);
23166 end if;
23167 end Record_Type_Definition;
23168
23169 ---------------------------
23170 -- Replace_Discriminants --
23171 ---------------------------
23172
23173 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
23174 function Process (N : Node_Id) return Traverse_Result;
23175
23176 -------------
23177 -- Process --
23178 -------------
23179
23180 function Process (N : Node_Id) return Traverse_Result is
23181 Comp : Entity_Id;
23182
23183 begin
23184 if Nkind (N) = N_Discriminant_Specification then
23185 Comp := First_Discriminant (Typ);
23186 while Present (Comp) loop
23187 if Original_Record_Component (Comp) = Defining_Identifier (N)
23188 or else Chars (Comp) = Chars (Defining_Identifier (N))
23189 then
23190 Set_Defining_Identifier (N, Comp);
23191 exit;
23192 end if;
23193
23194 Next_Discriminant (Comp);
23195 end loop;
23196
23197 elsif Nkind (N) = N_Variant_Part then
23198 Comp := First_Discriminant (Typ);
23199 while Present (Comp) loop
23200 if Original_Record_Component (Comp) = Entity (Name (N))
23201 or else Chars (Comp) = Chars (Name (N))
23202 then
23203 -- Make sure to preserve the type coming from the parent on
23204 -- the Name, even if the subtype of the discriminant can be
23205 -- constrained, so that discrete choices inherited from the
23206 -- parent in the variant part are not flagged as violating
23207 -- the constraints of the subtype.
23208
23209 declare
23210 Typ : constant Entity_Id := Etype (Name (N));
23211 begin
23212 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
23213 Set_Etype (Name (N), Typ);
23214 end;
23215 exit;
23216 end if;
23217
23218 Next_Discriminant (Comp);
23219 end loop;
23220 end if;
23221
23222 return OK;
23223 end Process;
23224
23225 procedure Replace is new Traverse_Proc (Process);
23226
23227 -- Start of processing for Replace_Discriminants
23228
23229 begin
23230 Replace (Decl);
23231 end Replace_Discriminants;
23232
23233 -------------------------------
23234 -- Set_Completion_Referenced --
23235 -------------------------------
23236
23237 procedure Set_Completion_Referenced (E : Entity_Id) is
23238 begin
23239 -- If in main unit, mark entity that is a completion as referenced,
23240 -- warnings go on the partial view when needed.
23241
23242 if In_Extended_Main_Source_Unit (E) then
23243 Set_Referenced (E);
23244 end if;
23245 end Set_Completion_Referenced;
23246
23247 ---------------------
23248 -- Set_Default_SSO --
23249 ---------------------
23250
23251 procedure Set_Default_SSO (T : Entity_Id) is
23252 begin
23253 case Opt.Default_SSO is
23254 when ' ' =>
23255 null;
23256 when 'L' =>
23257 Set_SSO_Set_Low_By_Default (T, True);
23258 when 'H' =>
23259 Set_SSO_Set_High_By_Default (T, True);
23260 when others =>
23261 raise Program_Error;
23262 end case;
23263 end Set_Default_SSO;
23264
23265 ---------------------
23266 -- Set_Fixed_Range --
23267 ---------------------
23268
23269 -- The range for fixed-point types is complicated by the fact that we
23270 -- do not know the exact end points at the time of the declaration. This
23271 -- is true for three reasons:
23272
23273 -- A size clause may affect the fudging of the end-points.
23274 -- A small clause may affect the values of the end-points.
23275 -- We try to include the end-points if it does not affect the size.
23276
23277 -- This means that the actual end-points must be established at the
23278 -- point when the type is frozen. Meanwhile, we first narrow the range
23279 -- as permitted (so that it will fit if necessary in a small specified
23280 -- size), and then build a range subtree with these narrowed bounds.
23281 -- Set_Fixed_Range constructs the range from real literal values, and
23282 -- sets the range as the Scalar_Range of the given fixed-point type entity.
23283
23284 -- The parent of this range is set to point to the entity so that it is
23285 -- properly hooked into the tree (unlike normal Scalar_Range entries for
23286 -- other scalar types, which are just pointers to the range in the
23287 -- original tree, this would otherwise be an orphan).
23288
23289 -- The tree is left unanalyzed. When the type is frozen, the processing
23290 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
23291 -- analyzed, and uses this as an indication that it should complete
23292 -- work on the range (it will know the final small and size values).
23293
23294 procedure Set_Fixed_Range
23295 (E : Entity_Id;
23296 Loc : Source_Ptr;
23297 Lo : Ureal;
23298 Hi : Ureal)
23299 is
23300 S : constant Node_Id :=
23301 Make_Range (Loc,
23302 Low_Bound => Make_Real_Literal (Loc, Lo),
23303 High_Bound => Make_Real_Literal (Loc, Hi));
23304 begin
23305 Set_Scalar_Range (E, S);
23306 Set_Parent (S, E);
23307
23308 -- Before the freeze point, the bounds of a fixed point are universal
23309 -- and carry the corresponding type.
23310
23311 Set_Etype (Low_Bound (S), Universal_Real);
23312 Set_Etype (High_Bound (S), Universal_Real);
23313 end Set_Fixed_Range;
23314
23315 ----------------------------------
23316 -- Set_Scalar_Range_For_Subtype --
23317 ----------------------------------
23318
23319 procedure Set_Scalar_Range_For_Subtype
23320 (Def_Id : Entity_Id;
23321 R : Node_Id;
23322 Subt : Entity_Id)
23323 is
23324 Kind : constant Entity_Kind := Ekind (Def_Id);
23325
23326 begin
23327 -- Defend against previous error
23328
23329 if Nkind (R) = N_Error then
23330 return;
23331 end if;
23332
23333 Set_Scalar_Range (Def_Id, R);
23334
23335 -- We need to link the range into the tree before resolving it so
23336 -- that types that are referenced, including importantly the subtype
23337 -- itself, are properly frozen (Freeze_Expression requires that the
23338 -- expression be properly linked into the tree). Of course if it is
23339 -- already linked in, then we do not disturb the current link.
23340
23341 if No (Parent (R)) then
23342 Set_Parent (R, Def_Id);
23343 end if;
23344
23345 -- Reset the kind of the subtype during analysis of the range, to
23346 -- catch possible premature use in the bounds themselves.
23347
23348 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23349 pragma Assert (Ekind (Def_Id) = Kind);
23350 end Set_Scalar_Range_For_Subtype;
23351
23352 --------------------------------------------------------
23353 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23354 --------------------------------------------------------
23355
23356 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23357 (E : Entity_Id)
23358 is
23359 begin
23360 -- Make sure set if encountered during Expand_To_Stored_Constraint
23361
23362 Set_Stored_Constraint (E, No_Elist);
23363
23364 -- Give it the right value
23365
23366 if Is_Constrained (E) and then Has_Discriminants (E) then
23367 Set_Stored_Constraint (E,
23368 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23369 end if;
23370 end Set_Stored_Constraint_From_Discriminant_Constraint;
23371
23372 -------------------------------------
23373 -- Signed_Integer_Type_Declaration --
23374 -------------------------------------
23375
23376 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23377 Implicit_Base : Entity_Id;
23378 Base_Typ : Entity_Id;
23379 Lo_Val : Uint;
23380 Hi_Val : Uint;
23381 Errs : Boolean := False;
23382 Lo : Node_Id;
23383 Hi : Node_Id;
23384
23385 function Can_Derive_From (E : Entity_Id) return Boolean;
23386 -- Determine whether given bounds allow derivation from specified type
23387
23388 procedure Check_Bound (Expr : Node_Id);
23389 -- Check bound to make sure it is integral and static. If not, post
23390 -- appropriate error message and set Errs flag
23391
23392 ---------------------
23393 -- Can_Derive_From --
23394 ---------------------
23395
23396 -- Note we check both bounds against both end values, to deal with
23397 -- strange types like ones with a range of 0 .. -12341234.
23398
23399 function Can_Derive_From (E : Entity_Id) return Boolean is
23400 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23401 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23402 begin
23403 return Lo <= Lo_Val and then Lo_Val <= Hi
23404 and then
23405 Lo <= Hi_Val and then Hi_Val <= Hi;
23406 end Can_Derive_From;
23407
23408 -----------------
23409 -- Check_Bound --
23410 -----------------
23411
23412 procedure Check_Bound (Expr : Node_Id) is
23413 begin
23414 -- If a range constraint is used as an integer type definition, each
23415 -- bound of the range must be defined by a static expression of some
23416 -- integer type, but the two bounds need not have the same integer
23417 -- type (Negative bounds are allowed.) (RM 3.5.4)
23418
23419 if not Is_Integer_Type (Etype (Expr)) then
23420 Error_Msg_N
23421 ("integer type definition bounds must be of integer type", Expr);
23422 Errs := True;
23423
23424 elsif not Is_OK_Static_Expression (Expr) then
23425 Flag_Non_Static_Expr
23426 ("non-static expression used for integer type bound!", Expr);
23427 Errs := True;
23428
23429 -- Otherwise the bounds are folded into literals
23430
23431 elsif Is_Entity_Name (Expr) then
23432 Fold_Uint (Expr, Expr_Value (Expr), True);
23433 end if;
23434 end Check_Bound;
23435
23436 -- Start of processing for Signed_Integer_Type_Declaration
23437
23438 begin
23439 -- Create an anonymous base type
23440
23441 Implicit_Base :=
23442 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23443
23444 -- Analyze and check the bounds, they can be of any integer type
23445
23446 Lo := Low_Bound (Def);
23447 Hi := High_Bound (Def);
23448
23449 -- Arbitrarily use Integer as the type if either bound had an error
23450
23451 if Hi = Error or else Lo = Error then
23452 Base_Typ := Any_Integer;
23453 Set_Error_Posted (T, True);
23454 Errs := True;
23455
23456 -- Here both bounds are OK expressions
23457
23458 else
23459 Analyze_And_Resolve (Lo, Any_Integer);
23460 Analyze_And_Resolve (Hi, Any_Integer);
23461
23462 Check_Bound (Lo);
23463 Check_Bound (Hi);
23464
23465 if Errs then
23466 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23467 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23468 end if;
23469
23470 -- Find type to derive from
23471
23472 Lo_Val := Expr_Value (Lo);
23473 Hi_Val := Expr_Value (Hi);
23474
23475 if Can_Derive_From (Standard_Short_Short_Integer) then
23476 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23477
23478 elsif Can_Derive_From (Standard_Short_Integer) then
23479 Base_Typ := Base_Type (Standard_Short_Integer);
23480
23481 elsif Can_Derive_From (Standard_Integer) then
23482 Base_Typ := Base_Type (Standard_Integer);
23483
23484 elsif Can_Derive_From (Standard_Long_Integer) then
23485 Base_Typ := Base_Type (Standard_Long_Integer);
23486
23487 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23488 Check_Restriction (No_Long_Long_Integers, Def);
23489 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23490
23491 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23492 Check_Restriction (No_Long_Long_Integers, Def);
23493 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23494
23495 else
23496 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23497 Error_Msg_N ("integer type definition bounds out of range", Def);
23498 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23499 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23500 end if;
23501 end if;
23502
23503 -- Set the type of the bounds to the implicit base: we cannot set it to
23504 -- the new type, because this would be a forward reference for the code
23505 -- generator and, if the original type is user-defined, this could even
23506 -- lead to spurious semantic errors. Furthermore we do not set it to be
23507 -- universal, because this could make it much larger than needed here.
23508
23509 if not Errs then
23510 Set_Etype (Lo, Implicit_Base);
23511 Set_Etype (Hi, Implicit_Base);
23512 end if;
23513
23514 -- Complete both implicit base and declared first subtype entities. The
23515 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23516 -- are not clobbered when the signed integer type acts as a full view of
23517 -- a private type.
23518
23519 Set_Etype (Implicit_Base, Base_Typ);
23520 Set_Size_Info (Implicit_Base, Base_Typ);
23521 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23522 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23523 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23524
23525 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23526 Set_Etype (T, Implicit_Base);
23527 Set_Size_Info (T, Implicit_Base);
23528 Inherit_Rep_Item_Chain (T, Implicit_Base);
23529 Set_Scalar_Range (T, Def);
23530 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23531 Set_Is_Constrained (T);
23532 end Signed_Integer_Type_Declaration;
23533
23534 -------------------------------------
23535 -- Warn_On_Inherently_Limited_Type --
23536 -------------------------------------
23537
23538 procedure Warn_On_Inherently_Limited_Type (E : Entity_Id) is
23539 C : Entity_Id;
23540 begin
23541 if Warnsw.Warn_On_Inherently_Limited_Type
23542 and then not Is_Limited_Record (E)
23543 then
23544 C := First_Component (Base_Type (E));
23545 while Present (C) loop
23546 if Is_Inherently_Limited_Type (Etype (C)) then
23547 Error_Msg_Node_2 := E;
23548 Error_Msg_NE
23549 ("?_l?limited component & makes & limited", E, C);
23550 Error_Msg_N
23551 ("\\?_l?consider annotating the record type "
23552 & "with a LIMITED keyword", E);
23553 exit;
23554 end if;
23555
23556 Next_Component (C);
23557 end loop;
23558 end if;
23559 end Warn_On_Inherently_Limited_Type;
23560
23561 end Sem_Ch3;