]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch3.adb
[Ada] Clean up uses of Esize and RM_Size
[gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2021, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Errout; use Errout;
36 with Eval_Fat; use Eval_Fat;
37 with Exp_Ch3; use Exp_Ch3;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Itypes; use Itypes;
46 with Layout; use Layout;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Case; use Sem_Case;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch7; use Sem_Ch7;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Elab; use Sem_Elab;
68 with Sem_Elim; use Sem_Elim;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_Smem; use Sem_Smem;
73 with Sem_Type; use Sem_Type;
74 with Sem_Util; use Sem_Util;
75 with Sem_Warn; use Sem_Warn;
76 with Stand; use Stand;
77 with Sinfo; use Sinfo;
78 with Sinfo.Nodes; use Sinfo.Nodes;
79 with Sinfo.Utils; use Sinfo.Utils;
80 with Sinput; use Sinput;
81 with Snames; use Snames;
82 with Targparm; use Targparm;
83 with Tbuild; use Tbuild;
84 with Ttypes; use Ttypes;
85 with Uintp; use Uintp;
86 with Urealp; use Urealp;
87
88 package body Sem_Ch3 is
89
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
93
94 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
95 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
96 -- abstract interface types implemented by a record type or a derived
97 -- record type.
98
99 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
100 -- When an access-to-subprogram type has pre/postconditions, we build a
101 -- subprogram that includes these contracts and is invoked by an indirect
102 -- call through the corresponding access type.
103
104 procedure Build_Derived_Type
105 (N : Node_Id;
106 Parent_Type : Entity_Id;
107 Derived_Type : Entity_Id;
108 Is_Completion : Boolean;
109 Derive_Subps : Boolean := True);
110 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
111 -- the N_Full_Type_Declaration node containing the derived type definition.
112 -- Parent_Type is the entity for the parent type in the derived type
113 -- definition and Derived_Type the actual derived type. Is_Completion must
114 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
115 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
116 -- completion of a private type declaration. If Is_Completion is set to
117 -- True, N is the completion of a private type declaration and Derived_Type
118 -- is different from the defining identifier inside N (i.e. Derived_Type /=
119 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
120 -- subprograms should be derived. The only case where this parameter is
121 -- False is when Build_Derived_Type is recursively called to process an
122 -- implicit derived full type for a type derived from a private type (in
123 -- that case the subprograms must only be derived for the private view of
124 -- the type).
125 --
126 -- ??? These flags need a bit of re-examination and re-documentation:
127 -- ??? are they both necessary (both seem related to the recursion)?
128
129 procedure Build_Derived_Access_Type
130 (N : Node_Id;
131 Parent_Type : Entity_Id;
132 Derived_Type : Entity_Id);
133 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
134 -- create an implicit base if the parent type is constrained or if the
135 -- subtype indication has a constraint.
136
137 procedure Build_Derived_Array_Type
138 (N : Node_Id;
139 Parent_Type : Entity_Id;
140 Derived_Type : Entity_Id);
141 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
142 -- create an implicit base if the parent type is constrained or if the
143 -- subtype indication has a constraint.
144
145 procedure Build_Derived_Concurrent_Type
146 (N : Node_Id;
147 Parent_Type : Entity_Id;
148 Derived_Type : Entity_Id);
149 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
150 -- protected type, inherit entries and protected subprograms, check
151 -- legality of discriminant constraints if any.
152
153 procedure Build_Derived_Enumeration_Type
154 (N : Node_Id;
155 Parent_Type : Entity_Id;
156 Derived_Type : Entity_Id);
157 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
158 -- type, we must create a new list of literals. Types derived from
159 -- Character and [Wide_]Wide_Character are special-cased.
160
161 procedure Build_Derived_Numeric_Type
162 (N : Node_Id;
163 Parent_Type : Entity_Id;
164 Derived_Type : Entity_Id);
165 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
166 -- an anonymous base type, and propagate constraint to subtype if needed.
167
168 procedure Build_Derived_Private_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Is_Completion : Boolean;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
175 -- because the parent may or may not have a completion, and the derivation
176 -- may itself be a completion.
177
178 procedure Build_Derived_Record_Type
179 (N : Node_Id;
180 Parent_Type : Entity_Id;
181 Derived_Type : Entity_Id;
182 Derive_Subps : Boolean := True);
183 -- Subsidiary procedure used for tagged and untagged record types
184 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
185 -- All parameters are as in Build_Derived_Type except that N, in
186 -- addition to being an N_Full_Type_Declaration node, can also be an
187 -- N_Private_Extension_Declaration node. See the definition of this routine
188 -- for much more info. Derive_Subps indicates whether subprograms should be
189 -- derived from the parent type. The only case where Derive_Subps is False
190 -- is for an implicit derived full type for a type derived from a private
191 -- type (see Build_Derived_Type).
192
193 procedure Build_Discriminal (Discrim : Entity_Id);
194 -- Create the discriminal corresponding to discriminant Discrim, that is
195 -- the parameter corresponding to Discrim to be used in initialization
196 -- procedures for the type where Discrim is a discriminant. Discriminals
197 -- are not used during semantic analysis, and are not fully defined
198 -- entities until expansion. Thus they are not given a scope until
199 -- initialization procedures are built.
200
201 function Build_Discriminant_Constraints
202 (T : Entity_Id;
203 Def : Node_Id;
204 Derived_Def : Boolean := False) return Elist_Id;
205 -- Validate discriminant constraints and return the list of the constraints
206 -- in order of discriminant declarations, where T is the discriminated
207 -- unconstrained type. Def is the N_Subtype_Indication node where the
208 -- discriminants constraints for T are specified. Derived_Def is True
209 -- when building the discriminant constraints in a derived type definition
210 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
211 -- type and Def is the constraint "(xxx)" on T and this routine sets the
212 -- Corresponding_Discriminant field of the discriminants in the derived
213 -- type D to point to the corresponding discriminants in the parent type T.
214
215 procedure Build_Discriminated_Subtype
216 (T : Entity_Id;
217 Def_Id : Entity_Id;
218 Elist : Elist_Id;
219 Related_Nod : Node_Id;
220 For_Access : Boolean := False);
221 -- Subsidiary procedure to Constrain_Discriminated_Type and to
222 -- Process_Incomplete_Dependents. Given
223 --
224 -- T (a possibly discriminated base type)
225 -- Def_Id (a very partially built subtype for T),
226 --
227 -- the call completes Def_Id to be the appropriate E_*_Subtype.
228 --
229 -- The Elist is the list of discriminant constraints if any (it is set
230 -- to No_Elist if T is not a discriminated type, and to an empty list if
231 -- T has discriminants but there are no discriminant constraints). The
232 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
233 -- The For_Access says whether or not this subtype is really constraining
234 -- an access type.
235
236 function Build_Scalar_Bound
237 (Bound : Node_Id;
238 Par_T : Entity_Id;
239 Der_T : Entity_Id) return Node_Id;
240 -- The bounds of a derived scalar type are conversions of the bounds of
241 -- the parent type. Optimize the representation if the bounds are literals.
242 -- Needs a more complete spec--what are the parameters exactly, and what
243 -- exactly is the returned value, and how is Bound affected???
244
245 procedure Check_Access_Discriminant_Requires_Limited
246 (D : Node_Id;
247 Loc : Node_Id);
248 -- Check the restriction that the type to which an access discriminant
249 -- belongs must be a concurrent type or a descendant of a type with
250 -- the reserved word 'limited' in its declaration.
251
252 procedure Check_Anonymous_Access_Component
253 (Typ_Decl : Node_Id;
254 Typ : Entity_Id;
255 Prev : Entity_Id;
256 Comp_Def : Node_Id;
257 Access_Def : Node_Id);
258 -- Ada 2005 AI-382: an access component in a record definition can refer to
259 -- the enclosing record, in which case it denotes the type itself, and not
260 -- the current instance of the type. We create an anonymous access type for
261 -- the component, and flag it as an access to a component, so accessibility
262 -- checks are properly performed on it. The declaration of the access type
263 -- is placed ahead of that of the record to prevent order-of-elaboration
264 -- circularity issues in Gigi. We create an incomplete type for the record
265 -- declaration, which is the designated type of the anonymous access.
266
267 procedure Check_Anonymous_Access_Components
268 (Typ_Decl : Node_Id;
269 Typ : Entity_Id;
270 Prev : Entity_Id;
271 Comp_List : Node_Id);
272 -- Call Check_Anonymous_Access_Component on Comp_List
273
274 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
275 -- Check that, if a new discriminant is used in a constraint defining the
276 -- parent subtype of a derivation, its subtype is statically compatible
277 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
278
279 procedure Check_Delta_Expression (E : Node_Id);
280 -- Check that the expression represented by E is suitable for use as a
281 -- delta expression, i.e. it is of real type and is static.
282
283 procedure Check_Digits_Expression (E : Node_Id);
284 -- Check that the expression represented by E is suitable for use as a
285 -- digits expression, i.e. it is of integer type, positive and static.
286
287 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
288 -- Validate the initialization of an object declaration. T is the required
289 -- type, and Exp is the initialization expression.
290
291 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
292 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
293
294 procedure Check_Or_Process_Discriminants
295 (N : Node_Id;
296 T : Entity_Id;
297 Prev : Entity_Id := Empty);
298 -- If N is the full declaration of the completion T of an incomplete or
299 -- private type, check its discriminants (which are already known to be
300 -- conformant with those of the partial view, see Find_Type_Name),
301 -- otherwise process them. Prev is the entity of the partial declaration,
302 -- if any.
303
304 procedure Check_Real_Bound (Bound : Node_Id);
305 -- Check given bound for being of real type and static. If not, post an
306 -- appropriate message, and rewrite the bound with the real literal zero.
307
308 procedure Constant_Redeclaration
309 (Id : Entity_Id;
310 N : Node_Id;
311 T : out Entity_Id);
312 -- Various checks on legality of full declaration of deferred constant.
313 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
314 -- node. The caller has not yet set any attributes of this entity.
315
316 function Contain_Interface
317 (Iface : Entity_Id;
318 Ifaces : Elist_Id) return Boolean;
319 -- Ada 2005: Determine whether Iface is present in the list Ifaces
320
321 procedure Convert_Scalar_Bounds
322 (N : Node_Id;
323 Parent_Type : Entity_Id;
324 Derived_Type : Entity_Id;
325 Loc : Source_Ptr);
326 -- For derived scalar types, convert the bounds in the type definition to
327 -- the derived type, and complete their analysis. Given a constraint of the
328 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
329 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
330 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
331 -- subtype are conversions of those bounds to the derived_type, so that
332 -- their typing is consistent.
333
334 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
335 -- Copies attributes from array base type T2 to array base type T1. Copies
336 -- only attributes that apply to base types, but not subtypes.
337
338 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
339 -- Copies attributes from array subtype T2 to array subtype T1. Copies
340 -- attributes that apply to both subtypes and base types.
341
342 procedure Create_Constrained_Components
343 (Subt : Entity_Id;
344 Decl_Node : Node_Id;
345 Typ : Entity_Id;
346 Constraints : Elist_Id);
347 -- Build the list of entities for a constrained discriminated record
348 -- subtype. If a component depends on a discriminant, replace its subtype
349 -- using the discriminant values in the discriminant constraint. Subt
350 -- is the defining identifier for the subtype whose list of constrained
351 -- entities we will create. Decl_Node is the type declaration node where
352 -- we will attach all the itypes created. Typ is the base discriminated
353 -- type for the subtype Subt. Constraints is the list of discriminant
354 -- constraints for Typ.
355
356 function Constrain_Component_Type
357 (Comp : Entity_Id;
358 Constrained_Typ : Entity_Id;
359 Related_Node : Node_Id;
360 Typ : Entity_Id;
361 Constraints : Elist_Id) return Entity_Id;
362 -- Given a discriminated base type Typ, a list of discriminant constraints,
363 -- Constraints, for Typ and a component Comp of Typ, create and return the
364 -- type corresponding to Etype (Comp) where all discriminant references
365 -- are replaced with the corresponding constraint. If Etype (Comp) contains
366 -- no discriminant references then it is returned as-is. Constrained_Typ
367 -- is the final constrained subtype to which the constrained component
368 -- belongs. Related_Node is the node where we attach all created itypes.
369
370 procedure Constrain_Access
371 (Def_Id : in out Entity_Id;
372 S : Node_Id;
373 Related_Nod : Node_Id);
374 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
375 -- an anonymous type created for a subtype indication. In that case it is
376 -- created in the procedure and attached to Related_Nod.
377
378 procedure Constrain_Array
379 (Def_Id : in out Entity_Id;
380 SI : Node_Id;
381 Related_Nod : Node_Id;
382 Related_Id : Entity_Id;
383 Suffix : Character);
384 -- Apply a list of index constraints to an unconstrained array type. The
385 -- first parameter is the entity for the resulting subtype. A value of
386 -- Empty for Def_Id indicates that an implicit type must be created, but
387 -- creation is delayed (and must be done by this procedure) because other
388 -- subsidiary implicit types must be created first (which is why Def_Id
389 -- is an in/out parameter). The second parameter is a subtype indication
390 -- node for the constrained array to be created (e.g. something of the
391 -- form string (1 .. 10)). Related_Nod gives the place where this type
392 -- has to be inserted in the tree. The Related_Id and Suffix parameters
393 -- are used to build the associated Implicit type name.
394
395 procedure Constrain_Concurrent
396 (Def_Id : in out Entity_Id;
397 SI : Node_Id;
398 Related_Nod : Node_Id;
399 Related_Id : Entity_Id;
400 Suffix : Character);
401 -- Apply list of discriminant constraints to an unconstrained concurrent
402 -- type.
403 --
404 -- SI is the N_Subtype_Indication node containing the constraint and
405 -- the unconstrained type to constrain.
406 --
407 -- Def_Id is the entity for the resulting constrained subtype. A value
408 -- of Empty for Def_Id indicates that an implicit type must be created,
409 -- but creation is delayed (and must be done by this procedure) because
410 -- other subsidiary implicit types must be created first (which is why
411 -- Def_Id is an in/out parameter).
412 --
413 -- Related_Nod gives the place where this type has to be inserted
414 -- in the tree.
415 --
416 -- The last two arguments are used to create its external name if needed.
417
418 function Constrain_Corresponding_Record
419 (Prot_Subt : Entity_Id;
420 Corr_Rec : Entity_Id;
421 Related_Nod : Node_Id) return Entity_Id;
422 -- When constraining a protected type or task type with discriminants,
423 -- constrain the corresponding record with the same discriminant values.
424
425 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id);
426 -- Constrain a decimal fixed point type with a digits constraint and/or a
427 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
428
429 procedure Constrain_Discriminated_Type
430 (Def_Id : Entity_Id;
431 S : Node_Id;
432 Related_Nod : Node_Id;
433 For_Access : Boolean := False);
434 -- Process discriminant constraints of composite type. Verify that values
435 -- have been provided for all discriminants, that the original type is
436 -- unconstrained, and that the types of the supplied expressions match
437 -- the discriminant types. The first three parameters are like in routine
438 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
439 -- of For_Access.
440
441 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id);
442 -- Constrain an enumeration type with a range constraint. This is identical
443 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
444
445 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id);
446 -- Constrain a floating point type with either a digits constraint
447 -- and/or a range constraint, building a E_Floating_Point_Subtype.
448
449 procedure Constrain_Index
450 (Index : Node_Id;
451 S : Node_Id;
452 Related_Nod : Node_Id;
453 Related_Id : Entity_Id;
454 Suffix : Character;
455 Suffix_Index : Pos);
456 -- Process an index constraint S in a constrained array declaration. The
457 -- constraint can be a subtype name, or a range with or without an explicit
458 -- subtype mark. The index is the corresponding index of the unconstrained
459 -- array. The Related_Id and Suffix parameters are used to build the
460 -- associated Implicit type name.
461
462 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id);
463 -- Build subtype of a signed or modular integer type
464
465 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id);
466 -- Constrain an ordinary fixed point type with a range constraint, and
467 -- build an E_Ordinary_Fixed_Point_Subtype entity.
468
469 procedure Copy_And_Swap (Priv, Full : Entity_Id);
470 -- Copy the Priv entity into the entity of its full declaration then swap
471 -- the two entities in such a manner that the former private type is now
472 -- seen as a full type.
473
474 procedure Decimal_Fixed_Point_Type_Declaration
475 (T : Entity_Id;
476 Def : Node_Id);
477 -- Create a new decimal fixed point type, and apply the constraint to
478 -- obtain a subtype of this new type.
479
480 procedure Complete_Private_Subtype
481 (Priv : Entity_Id;
482 Full : Entity_Id;
483 Full_Base : Entity_Id;
484 Related_Nod : Node_Id);
485 -- Complete the implicit full view of a private subtype by setting the
486 -- appropriate semantic fields. If the full view of the parent is a record
487 -- type, build constrained components of subtype.
488
489 procedure Derive_Progenitor_Subprograms
490 (Parent_Type : Entity_Id;
491 Tagged_Type : Entity_Id);
492 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
493 -- operations of progenitors of Tagged_Type, and replace the subsidiary
494 -- subtypes with Tagged_Type, to build the specs of the inherited interface
495 -- primitives. The derived primitives are aliased to those of the
496 -- interface. This routine takes care also of transferring to the full view
497 -- subprograms associated with the partial view of Tagged_Type that cover
498 -- interface primitives.
499
500 procedure Derived_Standard_Character
501 (N : Node_Id;
502 Parent_Type : Entity_Id;
503 Derived_Type : Entity_Id);
504 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
505 -- derivations from types Standard.Character and Standard.Wide_Character.
506
507 procedure Derived_Type_Declaration
508 (T : Entity_Id;
509 N : Node_Id;
510 Is_Completion : Boolean);
511 -- Process a derived type declaration. Build_Derived_Type is invoked
512 -- to process the actual derived type definition. Parameters N and
513 -- Is_Completion have the same meaning as in Build_Derived_Type.
514 -- T is the N_Defining_Identifier for the entity defined in the
515 -- N_Full_Type_Declaration node N, that is T is the derived type.
516
517 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
518 -- Insert each literal in symbol table, as an overloadable identifier. Each
519 -- enumeration type is mapped into a sequence of integers, and each literal
520 -- is defined as a constant with integer value. If any of the literals are
521 -- character literals, the type is a character type, which means that
522 -- strings are legal aggregates for arrays of components of the type.
523
524 function Expand_To_Stored_Constraint
525 (Typ : Entity_Id;
526 Constraint : Elist_Id) return Elist_Id;
527 -- Given a constraint (i.e. a list of expressions) on the discriminants of
528 -- Typ, expand it into a constraint on the stored discriminants and return
529 -- the new list of expressions constraining the stored discriminants.
530
531 function Find_Type_Of_Object
532 (Obj_Def : Node_Id;
533 Related_Nod : Node_Id) return Entity_Id;
534 -- Get type entity for object referenced by Obj_Def, attaching the implicit
535 -- types generated to Related_Nod.
536
537 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
538 -- Create a new float and apply the constraint to obtain subtype of it
539
540 function Has_Range_Constraint (N : Node_Id) return Boolean;
541 -- Given an N_Subtype_Indication node N, return True if a range constraint
542 -- is present, either directly, or as part of a digits or delta constraint.
543 -- In addition, a digits constraint in the decimal case returns True, since
544 -- it establishes a default range if no explicit range is present.
545
546 function Inherit_Components
547 (N : Node_Id;
548 Parent_Base : Entity_Id;
549 Derived_Base : Entity_Id;
550 Is_Tagged : Boolean;
551 Inherit_Discr : Boolean;
552 Discs : Elist_Id) return Elist_Id;
553 -- Called from Build_Derived_Record_Type to inherit the components of
554 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
555 -- For more information on derived types and component inheritance please
556 -- consult the comment above the body of Build_Derived_Record_Type.
557 --
558 -- N is the original derived type declaration
559 --
560 -- Is_Tagged is set if we are dealing with tagged types
561 --
562 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
563 -- Parent_Base, otherwise no discriminants are inherited.
564 --
565 -- Discs gives the list of constraints that apply to Parent_Base in the
566 -- derived type declaration. If Discs is set to No_Elist, then we have
567 -- the following situation:
568 --
569 -- type Parent (D1..Dn : ..) is [tagged] record ...;
570 -- type Derived is new Parent [with ...];
571 --
572 -- which gets treated as
573 --
574 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
575 --
576 -- For untagged types the returned value is an association list. The list
577 -- starts from the association (Parent_Base => Derived_Base), and then it
578 -- contains a sequence of the associations of the form
579 --
580 -- (Old_Component => New_Component),
581 --
582 -- where Old_Component is the Entity_Id of a component in Parent_Base and
583 -- New_Component is the Entity_Id of the corresponding component in
584 -- Derived_Base. For untagged records, this association list is needed when
585 -- copying the record declaration for the derived base. In the tagged case
586 -- the value returned is irrelevant.
587
588 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
589 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
590 -- Determine whether subprogram Subp is a procedure subject to pragma
591 -- Extensions_Visible with value False and has at least one controlling
592 -- parameter of mode OUT.
593
594 function Is_Private_Primitive (Prim : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- When applied to a primitive subprogram Prim, returns True if Prim is
597 -- declared as a private operation within a package or generic package,
598 -- and returns False otherwise.
599
600 function Is_Valid_Constraint_Kind
601 (T_Kind : Type_Kind;
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
605
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
608
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
612
613 procedure Ordinary_Fixed_Point_Type_Declaration
614 (T : Entity_Id;
615 Def : Node_Id);
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
618
619 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
620 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
621 -- In_Default_Expr can be properly adjusted.
622
623 procedure Prepare_Private_Subtype_Completion
624 (Id : Entity_Id;
625 Related_Nod : Node_Id);
626 -- Id is a subtype of some private type. Creates the full declaration
627 -- associated with Id whenever possible, i.e. when the full declaration
628 -- of the base type is already known. Records each subtype into
629 -- Private_Dependents of the base type.
630
631 procedure Process_Incomplete_Dependents
632 (N : Node_Id;
633 Full_T : Entity_Id;
634 Inc_T : Entity_Id);
635 -- Process all entities that depend on an incomplete type. There include
636 -- subtypes, subprogram types that mention the incomplete type in their
637 -- profiles, and subprogram with access parameters that designate the
638 -- incomplete type.
639
640 -- Inc_T is the defining identifier of an incomplete type declaration, its
641 -- Ekind is E_Incomplete_Type.
642 --
643 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
644 --
645 -- Full_T is N's defining identifier.
646 --
647 -- Subtypes of incomplete types with discriminants are completed when the
648 -- parent type is. This is simpler than private subtypes, because they can
649 -- only appear in the same scope, and there is no need to exchange views.
650 -- Similarly, access_to_subprogram types may have a parameter or a return
651 -- type that is an incomplete type, and that must be replaced with the
652 -- full type.
653 --
654 -- If the full type is tagged, subprogram with access parameters that
655 -- designated the incomplete may be primitive operations of the full type,
656 -- and have to be processed accordingly.
657
658 procedure Process_Real_Range_Specification (Def : Node_Id);
659 -- Given the type definition for a real type, this procedure processes and
660 -- checks the real range specification of this type definition if one is
661 -- present. If errors are found, error messages are posted, and the
662 -- Real_Range_Specification of Def is reset to Empty.
663
664 procedure Record_Type_Declaration
665 (T : Entity_Id;
666 N : Node_Id;
667 Prev : Entity_Id);
668 -- Process a record type declaration (for both untagged and tagged
669 -- records). Parameters T and N are exactly like in procedure
670 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
671 -- for this routine. If this is the completion of an incomplete type
672 -- declaration, Prev is the entity of the incomplete declaration, used for
673 -- cross-referencing. Otherwise Prev = T.
674
675 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
676 -- This routine is used to process the actual record type definition (both
677 -- for untagged and tagged records). Def is a record type definition node.
678 -- This procedure analyzes the components in this record type definition.
679 -- Prev_T is the entity for the enclosing record type. It is provided so
680 -- that its Has_Task flag can be set if any of the component have Has_Task
681 -- set. If the declaration is the completion of an incomplete type
682 -- declaration, Prev_T is the original incomplete type, whose full view is
683 -- the record type.
684
685 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
686 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
687 -- first create the list of components for the derived type from that of
688 -- the parent by means of Inherit_Components and then build a copy of the
689 -- declaration tree of the parent with the help of the mapping returned by
690 -- Inherit_Components, which will for example be used to validate record
691 -- representation clauses given for the derived type. If the parent type
692 -- is private and has discriminants, the ancestor discriminants used in the
693 -- inheritance are that of the private declaration, whereas the ancestor
694 -- discriminants present in the declaration tree of the parent are that of
695 -- the full declaration; as a consequence, the remapping done during the
696 -- copy will leave the references to the ancestor discriminants unchanged
697 -- in the declaration tree and they need to be fixed up. If the derived
698 -- type has a known discriminant part, then the remapping done during the
699 -- copy will only create references to the girder discriminants and they
700 -- need to be replaced with references to the non-girder discriminants.
701
702 procedure Set_Fixed_Range
703 (E : Entity_Id;
704 Loc : Source_Ptr;
705 Lo : Ureal;
706 Hi : Ureal);
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
710
711 procedure Set_Scalar_Range_For_Subtype
712 (Def_Id : Entity_Id;
713 R : Node_Id;
714 Subt : Entity_Id);
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
719
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
724
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
728
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
730 (E : Entity_Id);
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
733
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
737
738 -----------------------
739 -- Access_Definition --
740 -----------------------
741
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
745 is
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
750
751 begin
752 if Is_Entry (Current_Scope)
753 and then Is_Task_Type (Etype (Scope (Current_Scope)))
754 then
755 Error_Msg_N ("task entries cannot have access parameters", N);
756 return Empty;
757 end if;
758
759 -- Ada 2005: For an object declaration the corresponding anonymous
760 -- type is declared in the current scope.
761
762 -- If the access definition is the return type of another access to
763 -- function, scope is the current one, because it is the one of the
764 -- current type declaration, except for the pathological case below.
765
766 if Nkind (Related_Nod) in
767 N_Object_Declaration | N_Access_Function_Definition
768 then
769 Anon_Scope := Current_Scope;
770
771 -- A pathological case: function returning access functions that
772 -- return access functions, etc. Each anonymous access type created
773 -- is in the enclosing scope of the outermost function.
774
775 declare
776 Par : Node_Id;
777
778 begin
779 Par := Related_Nod;
780 while Nkind (Par) in
781 N_Access_Function_Definition | N_Access_Definition
782 loop
783 Par := Parent (Par);
784 end loop;
785
786 if Nkind (Par) = N_Function_Specification then
787 Anon_Scope := Scope (Defining_Entity (Par));
788 end if;
789 end;
790
791 -- For the anonymous function result case, retrieve the scope of the
792 -- function specification's associated entity rather than using the
793 -- current scope. The current scope will be the function itself if the
794 -- formal part is currently being analyzed, but will be the parent scope
795 -- in the case of a parameterless function, and we always want to use
796 -- the function's parent scope. Finally, if the function is a child
797 -- unit, we must traverse the tree to retrieve the proper entity.
798
799 elsif Nkind (Related_Nod) = N_Function_Specification
800 and then Nkind (Parent (N)) /= N_Parameter_Specification
801 then
802 -- If the current scope is a protected type, the anonymous access
803 -- is associated with one of the protected operations, and must
804 -- be available in the scope that encloses the protected declaration.
805 -- Otherwise the type is in the scope enclosing the subprogram.
806
807 -- If the function has formals, the return type of a subprogram
808 -- declaration is analyzed in the scope of the subprogram (see
809 -- Process_Formals) and thus the protected type, if present, is
810 -- the scope of the current function scope.
811
812 if Ekind (Current_Scope) = E_Protected_Type then
813 Enclosing_Prot_Type := Current_Scope;
814
815 elsif Ekind (Current_Scope) = E_Function
816 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
817 then
818 Enclosing_Prot_Type := Scope (Current_Scope);
819 end if;
820
821 if Present (Enclosing_Prot_Type) then
822 Anon_Scope := Scope (Enclosing_Prot_Type);
823
824 else
825 Anon_Scope := Scope (Defining_Entity (Related_Nod));
826 end if;
827
828 -- For an access type definition, if the current scope is a child
829 -- unit it is the scope of the type.
830
831 elsif Is_Compilation_Unit (Current_Scope) then
832 Anon_Scope := Current_Scope;
833
834 -- For access formals, access components, and access discriminants, the
835 -- scope is that of the enclosing declaration,
836
837 else
838 Anon_Scope := Scope (Current_Scope);
839 end if;
840
841 Anon_Type :=
842 Create_Itype
843 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
844
845 if All_Present (N)
846 and then Ada_Version >= Ada_2005
847 then
848 Error_Msg_N ("ALL not permitted for anonymous access types", N);
849 end if;
850
851 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
852 -- the corresponding semantic routine
853
854 if Present (Access_To_Subprogram_Definition (N)) then
855 Access_Subprogram_Declaration
856 (T_Name => Anon_Type,
857 T_Def => Access_To_Subprogram_Definition (N));
858
859 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
860 Mutate_Ekind
861 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
862 else
863 Mutate_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
864 end if;
865
866 Set_Can_Use_Internal_Rep
867 (Anon_Type, not Always_Compatible_Rep_On_Target);
868
869 -- If the anonymous access is associated with a protected operation,
870 -- create a reference to it after the enclosing protected definition
871 -- because the itype will be used in the subsequent bodies.
872
873 -- If the anonymous access itself is protected, a full type
874 -- declaratiton will be created for it, so that the equivalent
875 -- record type can be constructed. For further details, see
876 -- Replace_Anonymous_Access_To_Protected-Subprogram.
877
878 if Ekind (Current_Scope) = E_Protected_Type
879 and then not Protected_Present (Access_To_Subprogram_Definition (N))
880 then
881 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
882 end if;
883
884 return Anon_Type;
885 end if;
886
887 Find_Type (Subtype_Mark (N));
888 Desig_Type := Entity (Subtype_Mark (N));
889
890 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
891 Set_Etype (Anon_Type, Anon_Type);
892
893 -- Make sure the anonymous access type has size and alignment fields
894 -- set, as required by gigi. This is necessary in the case of the
895 -- Task_Body_Procedure.
896
897 if not Has_Private_Component (Desig_Type) then
898 Layout_Type (Anon_Type);
899 end if;
900
901 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
902 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
903 -- the null value is allowed. In Ada 95 the null value is never allowed.
904
905 if Ada_Version >= Ada_2005 then
906 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
907 else
908 Set_Can_Never_Be_Null (Anon_Type, True);
909 end if;
910
911 -- The anonymous access type is as public as the discriminated type or
912 -- subprogram that defines it. It is imported (for back-end purposes)
913 -- if the designated type is.
914
915 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
916
917 -- Ada 2005 (AI-231): Propagate the access-constant attribute
918
919 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
920
921 -- The context is either a subprogram declaration, object declaration,
922 -- or an access discriminant, in a private or a full type declaration.
923 -- In the case of a subprogram, if the designated type is incomplete,
924 -- the operation will be a primitive operation of the full type, to be
925 -- updated subsequently. If the type is imported through a limited_with
926 -- clause, the subprogram is not a primitive operation of the type
927 -- (which is declared elsewhere in some other scope).
928
929 if Ekind (Desig_Type) = E_Incomplete_Type
930 and then not From_Limited_With (Desig_Type)
931 and then Is_Overloadable (Current_Scope)
932 then
933 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
934 Set_Has_Delayed_Freeze (Current_Scope);
935 end if;
936
937 -- If the designated type is limited and class-wide, the object might
938 -- contain tasks, so we create a Master entity for the declaration. This
939 -- must be done before expansion of the full declaration, because the
940 -- declaration may include an expression that is an allocator, whose
941 -- expansion needs the proper Master for the created tasks.
942
943 if Expander_Active
944 and then Nkind (Related_Nod) = N_Object_Declaration
945 then
946 if Is_Limited_Record (Desig_Type)
947 and then Is_Class_Wide_Type (Desig_Type)
948 then
949 Build_Class_Wide_Master (Anon_Type);
950
951 -- Similarly, if the type is an anonymous access that designates
952 -- tasks, create a master entity for it in the current context.
953
954 elsif Has_Task (Desig_Type)
955 and then Comes_From_Source (Related_Nod)
956 then
957 Build_Master_Entity (Defining_Identifier (Related_Nod));
958 Build_Master_Renaming (Anon_Type);
959 end if;
960 end if;
961
962 -- For a private component of a protected type, it is imperative that
963 -- the back-end elaborate the type immediately after the protected
964 -- declaration, because this type will be used in the declarations
965 -- created for the component within each protected body, so we must
966 -- create an itype reference for it now.
967
968 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
969 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
970
971 -- Similarly, if the access definition is the return result of a
972 -- function, create an itype reference for it because it will be used
973 -- within the function body. For a regular function that is not a
974 -- compilation unit, insert reference after the declaration. For a
975 -- protected operation, insert it after the enclosing protected type
976 -- declaration. In either case, do not create a reference for a type
977 -- obtained through a limited_with clause, because this would introduce
978 -- semantic dependencies.
979
980 -- Similarly, do not create a reference if the designated type is a
981 -- generic formal, because no use of it will reach the backend.
982
983 elsif Nkind (Related_Nod) = N_Function_Specification
984 and then not From_Limited_With (Desig_Type)
985 and then not Is_Generic_Type (Desig_Type)
986 then
987 if Present (Enclosing_Prot_Type) then
988 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
989
990 elsif Is_List_Member (Parent (Related_Nod))
991 and then Nkind (Parent (N)) /= N_Parameter_Specification
992 then
993 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
994 end if;
995
996 -- Finally, create an itype reference for an object declaration of an
997 -- anonymous access type. This is strictly necessary only for deferred
998 -- constants, but in any case will avoid out-of-scope problems in the
999 -- back-end.
1000
1001 elsif Nkind (Related_Nod) = N_Object_Declaration then
1002 Build_Itype_Reference (Anon_Type, Related_Nod);
1003 end if;
1004
1005 return Anon_Type;
1006 end Access_Definition;
1007
1008 -----------------------------------
1009 -- Access_Subprogram_Declaration --
1010 -----------------------------------
1011
1012 procedure Access_Subprogram_Declaration
1013 (T_Name : Entity_Id;
1014 T_Def : Node_Id)
1015 is
1016 procedure Check_For_Premature_Usage (Def : Node_Id);
1017 -- Check that type T_Name is not used, directly or recursively, as a
1018 -- parameter or a return type in Def. Def is either a subtype, an
1019 -- access_definition, or an access_to_subprogram_definition.
1020
1021 -------------------------------
1022 -- Check_For_Premature_Usage --
1023 -------------------------------
1024
1025 procedure Check_For_Premature_Usage (Def : Node_Id) is
1026 Param : Node_Id;
1027
1028 begin
1029 -- Check for a subtype mark
1030
1031 if Nkind (Def) in N_Has_Etype then
1032 if Etype (Def) = T_Name then
1033 Error_Msg_N
1034 ("type& cannot be used before the end of its declaration",
1035 Def);
1036 end if;
1037
1038 -- If this is not a subtype, then this is an access_definition
1039
1040 elsif Nkind (Def) = N_Access_Definition then
1041 if Present (Access_To_Subprogram_Definition (Def)) then
1042 Check_For_Premature_Usage
1043 (Access_To_Subprogram_Definition (Def));
1044 else
1045 Check_For_Premature_Usage (Subtype_Mark (Def));
1046 end if;
1047
1048 -- The only cases left are N_Access_Function_Definition and
1049 -- N_Access_Procedure_Definition.
1050
1051 else
1052 if Present (Parameter_Specifications (Def)) then
1053 Param := First (Parameter_Specifications (Def));
1054 while Present (Param) loop
1055 Check_For_Premature_Usage (Parameter_Type (Param));
1056 Next (Param);
1057 end loop;
1058 end if;
1059
1060 if Nkind (Def) = N_Access_Function_Definition then
1061 Check_For_Premature_Usage (Result_Definition (Def));
1062 end if;
1063 end if;
1064 end Check_For_Premature_Usage;
1065
1066 -- Local variables
1067
1068 Formals : constant List_Id := Parameter_Specifications (T_Def);
1069 Formal : Entity_Id;
1070 D_Ityp : Node_Id;
1071 Desig_Type : constant Entity_Id :=
1072 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1073
1074 -- Start of processing for Access_Subprogram_Declaration
1075
1076 begin
1077 -- Associate the Itype node with the inner full-type declaration or
1078 -- subprogram spec or entry body. This is required to handle nested
1079 -- anonymous declarations. For example:
1080
1081 -- procedure P
1082 -- (X : access procedure
1083 -- (Y : access procedure
1084 -- (Z : access T)))
1085
1086 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1087 while Nkind (D_Ityp) not in N_Full_Type_Declaration
1088 | N_Private_Type_Declaration
1089 | N_Private_Extension_Declaration
1090 | N_Procedure_Specification
1091 | N_Function_Specification
1092 | N_Entry_Body
1093 | N_Object_Declaration
1094 | N_Object_Renaming_Declaration
1095 | N_Formal_Object_Declaration
1096 | N_Formal_Type_Declaration
1097 | N_Task_Type_Declaration
1098 | N_Protected_Type_Declaration
1099 loop
1100 D_Ityp := Parent (D_Ityp);
1101 pragma Assert (D_Ityp /= Empty);
1102 end loop;
1103
1104 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1105
1106 if Nkind (D_Ityp) in N_Procedure_Specification | N_Function_Specification
1107 then
1108 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1109
1110 elsif Nkind (D_Ityp) in N_Full_Type_Declaration
1111 | N_Object_Declaration
1112 | N_Object_Renaming_Declaration
1113 | N_Formal_Type_Declaration
1114 then
1115 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1116 end if;
1117
1118 if Nkind (T_Def) = N_Access_Function_Definition then
1119 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1120 declare
1121 Acc : constant Node_Id := Result_Definition (T_Def);
1122
1123 begin
1124 if Present (Access_To_Subprogram_Definition (Acc))
1125 and then
1126 Protected_Present (Access_To_Subprogram_Definition (Acc))
1127 then
1128 Set_Etype
1129 (Desig_Type,
1130 Replace_Anonymous_Access_To_Protected_Subprogram
1131 (T_Def));
1132
1133 else
1134 Set_Etype
1135 (Desig_Type,
1136 Access_Definition (T_Def, Result_Definition (T_Def)));
1137 end if;
1138 end;
1139
1140 else
1141 Analyze (Result_Definition (T_Def));
1142
1143 declare
1144 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1145
1146 begin
1147 -- If a null exclusion is imposed on the result type, then
1148 -- create a null-excluding itype (an access subtype) and use
1149 -- it as the function's Etype.
1150
1151 if Is_Access_Type (Typ)
1152 and then Null_Exclusion_In_Return_Present (T_Def)
1153 then
1154 Set_Etype (Desig_Type,
1155 Create_Null_Excluding_Itype
1156 (T => Typ,
1157 Related_Nod => T_Def,
1158 Scope_Id => Current_Scope));
1159
1160 else
1161 if From_Limited_With (Typ) then
1162
1163 -- AI05-151: Incomplete types are allowed in all basic
1164 -- declarations, including access to subprograms.
1165
1166 if Ada_Version >= Ada_2012 then
1167 null;
1168
1169 else
1170 Error_Msg_NE
1171 ("illegal use of incomplete type&",
1172 Result_Definition (T_Def), Typ);
1173 end if;
1174
1175 elsif Ekind (Current_Scope) = E_Package
1176 and then In_Private_Part (Current_Scope)
1177 then
1178 if Ekind (Typ) = E_Incomplete_Type then
1179 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1180
1181 elsif Is_Class_Wide_Type (Typ)
1182 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1183 then
1184 Append_Elmt
1185 (Desig_Type, Private_Dependents (Etype (Typ)));
1186 end if;
1187 end if;
1188
1189 Set_Etype (Desig_Type, Typ);
1190 end if;
1191 end;
1192 end if;
1193
1194 if not Is_Type (Etype (Desig_Type)) then
1195 Error_Msg_N
1196 ("expect type in function specification",
1197 Result_Definition (T_Def));
1198 end if;
1199
1200 else
1201 Set_Etype (Desig_Type, Standard_Void_Type);
1202 end if;
1203
1204 if Present (Formals) then
1205 Push_Scope (Desig_Type);
1206
1207 -- Some special tests here. These special tests can be removed
1208 -- if and when Itypes always have proper parent pointers to their
1209 -- declarations???
1210
1211 -- Special test 1) Link defining_identifier of formals. Required by
1212 -- First_Formal to provide its functionality.
1213
1214 declare
1215 F : Node_Id;
1216
1217 begin
1218 F := First (Formals);
1219
1220 while Present (F) loop
1221 if No (Parent (Defining_Identifier (F))) then
1222 Set_Parent (Defining_Identifier (F), F);
1223 end if;
1224
1225 Next (F);
1226 end loop;
1227 end;
1228
1229 Process_Formals (Formals, Parent (T_Def));
1230
1231 -- Special test 2) End_Scope requires that the parent pointer be set
1232 -- to something reasonable, but Itypes don't have parent pointers. So
1233 -- we set it and then unset it ???
1234
1235 Set_Parent (Desig_Type, T_Name);
1236 End_Scope;
1237 Set_Parent (Desig_Type, Empty);
1238 end if;
1239
1240 -- Check for premature usage of the type being defined
1241
1242 Check_For_Premature_Usage (T_Def);
1243
1244 -- The return type and/or any parameter type may be incomplete. Mark the
1245 -- subprogram_type as depending on the incomplete type, so that it can
1246 -- be updated when the full type declaration is seen. This only applies
1247 -- to incomplete types declared in some enclosing scope, not to limited
1248 -- views from other packages.
1249
1250 -- Prior to Ada 2012, access to functions can only have in_parameters.
1251
1252 if Present (Formals) then
1253 Formal := First_Formal (Desig_Type);
1254 while Present (Formal) loop
1255 if Ekind (Formal) /= E_In_Parameter
1256 and then Nkind (T_Def) = N_Access_Function_Definition
1257 and then Ada_Version < Ada_2012
1258 then
1259 Error_Msg_N ("functions can only have IN parameters", Formal);
1260 end if;
1261
1262 if Ekind (Etype (Formal)) = E_Incomplete_Type
1263 and then In_Open_Scopes (Scope (Etype (Formal)))
1264 then
1265 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1266 Set_Has_Delayed_Freeze (Desig_Type);
1267 end if;
1268
1269 Next_Formal (Formal);
1270 end loop;
1271 end if;
1272
1273 -- Check whether an indirect call without actuals may be possible. This
1274 -- is used when resolving calls whose result is then indexed.
1275
1276 May_Need_Actuals (Desig_Type);
1277
1278 -- If the return type is incomplete, this is legal as long as the type
1279 -- is declared in the current scope and will be completed in it (rather
1280 -- than being part of limited view).
1281
1282 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1283 and then not Has_Delayed_Freeze (Desig_Type)
1284 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1285 then
1286 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1287 Set_Has_Delayed_Freeze (Desig_Type);
1288 end if;
1289
1290 Check_Delayed_Subprogram (Desig_Type);
1291
1292 if Protected_Present (T_Def) then
1293 Mutate_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1294 Set_Convention (Desig_Type, Convention_Protected);
1295 else
1296 Mutate_Ekind (T_Name, E_Access_Subprogram_Type);
1297 end if;
1298
1299 Set_Can_Use_Internal_Rep (T_Name,
1300 not Always_Compatible_Rep_On_Target);
1301 Set_Etype (T_Name, T_Name);
1302 Reinit_Size_Align (T_Name);
1303 Set_Directly_Designated_Type (T_Name, Desig_Type);
1304
1305 -- If the access_to_subprogram is not declared at the library level,
1306 -- it can only point to subprograms that are at the same or deeper
1307 -- accessibility level. The corresponding subprogram type might
1308 -- require an activation record when compiling for C.
1309
1310 Set_Needs_Activation_Record (Desig_Type,
1311 not Is_Library_Level_Entity (T_Name));
1312
1313 Generate_Reference_To_Formals (T_Name);
1314
1315 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1316
1317 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1318
1319 Check_Restriction (No_Access_Subprograms, T_Def);
1320
1321 Create_Extra_Formals (Desig_Type);
1322 end Access_Subprogram_Declaration;
1323
1324 ----------------------------
1325 -- Access_Type_Declaration --
1326 ----------------------------
1327
1328 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1329
1330 procedure Setup_Access_Type (Desig_Typ : Entity_Id);
1331 -- After type declaration is analysed with T being an incomplete type,
1332 -- this routine will mutate the kind of T to the appropriate access type
1333 -- and set its directly designated type to Desig_Typ.
1334
1335 -----------------------
1336 -- Setup_Access_Type --
1337 -----------------------
1338
1339 procedure Setup_Access_Type (Desig_Typ : Entity_Id) is
1340 begin
1341 if All_Present (Def) or else Constant_Present (Def) then
1342 Mutate_Ekind (T, E_General_Access_Type);
1343 else
1344 Mutate_Ekind (T, E_Access_Type);
1345 end if;
1346
1347 Set_Directly_Designated_Type (T, Desig_Typ);
1348 end Setup_Access_Type;
1349
1350 -- Local variables
1351
1352 P : constant Node_Id := Parent (Def);
1353 S : constant Node_Id := Subtype_Indication (Def);
1354
1355 Full_Desig : Entity_Id;
1356
1357 -- Start of processing for Access_Type_Declaration
1358
1359 begin
1360 -- Check for permissible use of incomplete type
1361
1362 if Nkind (S) /= N_Subtype_Indication then
1363
1364 Analyze (S);
1365
1366 if Nkind (S) in N_Has_Entity
1367 and then Present (Entity (S))
1368 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1369 then
1370 Setup_Access_Type (Desig_Typ => Entity (S));
1371
1372 -- If the designated type is a limited view, we cannot tell if
1373 -- the full view contains tasks, and there is no way to handle
1374 -- that full view in a client. We create a master entity for the
1375 -- scope, which will be used when a client determines that one
1376 -- is needed.
1377
1378 if From_Limited_With (Entity (S))
1379 and then not Is_Class_Wide_Type (Entity (S))
1380 then
1381 Build_Master_Entity (T);
1382 Build_Master_Renaming (T);
1383 end if;
1384
1385 else
1386 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1387 end if;
1388
1389 -- If the access definition is of the form: ACCESS NOT NULL ..
1390 -- the subtype indication must be of an access type. Create
1391 -- a null-excluding subtype of it.
1392
1393 if Null_Excluding_Subtype (Def) then
1394 if not Is_Access_Type (Entity (S)) then
1395 Error_Msg_N ("null exclusion must apply to access type", Def);
1396
1397 else
1398 declare
1399 Loc : constant Source_Ptr := Sloc (S);
1400 Decl : Node_Id;
1401 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1402
1403 begin
1404 Decl :=
1405 Make_Subtype_Declaration (Loc,
1406 Defining_Identifier => Nam,
1407 Subtype_Indication =>
1408 New_Occurrence_Of (Entity (S), Loc));
1409 Set_Null_Exclusion_Present (Decl);
1410 Insert_Before (Parent (Def), Decl);
1411 Analyze (Decl);
1412 Set_Entity (S, Nam);
1413 end;
1414 end if;
1415 end if;
1416
1417 else
1418 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1419 end if;
1420
1421 if not Error_Posted (T) then
1422 Full_Desig := Designated_Type (T);
1423
1424 if Base_Type (Full_Desig) = T then
1425 Error_Msg_N ("access type cannot designate itself", S);
1426
1427 -- In Ada 2005, the type may have a limited view through some unit in
1428 -- its own context, allowing the following circularity that cannot be
1429 -- detected earlier.
1430
1431 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1432 then
1433 Error_Msg_N
1434 ("access type cannot designate its own class-wide type", S);
1435
1436 -- Clean up indication of tagged status to prevent cascaded errors
1437
1438 Set_Is_Tagged_Type (T, False);
1439 end if;
1440
1441 Set_Etype (T, T);
1442
1443 -- For SPARK, check that the designated type is compatible with
1444 -- respect to volatility with the access type.
1445
1446 if SPARK_Mode /= Off
1447 and then Comes_From_Source (T)
1448 then
1449 -- ??? UNIMPLEMENTED
1450 -- In the case where the designated type is incomplete at this
1451 -- point, performing this check here is harmless but the check
1452 -- will need to be repeated when the designated type is complete.
1453
1454 -- The preceding call to Comes_From_Source is needed because the
1455 -- FE sometimes introduces implicitly declared access types. See,
1456 -- for example, the expansion of nested_po.ads in OA28-015.
1457
1458 Check_Volatility_Compatibility
1459 (Full_Desig, T, "designated type", "access type",
1460 Srcpos_Bearer => T);
1461 end if;
1462 end if;
1463
1464 -- If the type has appeared already in a with_type clause, it is frozen
1465 -- and the pointer size is already set. Else, initialize.
1466
1467 if not From_Limited_With (T) then
1468 Reinit_Size_Align (T);
1469 end if;
1470
1471 -- Note that Has_Task is always false, since the access type itself
1472 -- is not a task type. See Einfo for more description on this point.
1473 -- Exactly the same consideration applies to Has_Controlled_Component
1474 -- and to Has_Protected.
1475
1476 Set_Has_Task (T, False);
1477 Set_Has_Protected (T, False);
1478 Set_Has_Timing_Event (T, False);
1479 Set_Has_Controlled_Component (T, False);
1480
1481 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1482 -- problems where an incomplete view of this entity has been previously
1483 -- established by a limited with and an overlaid version of this field
1484 -- (Stored_Constraint) was initialized for the incomplete view.
1485
1486 -- This reset is performed in most cases except where the access type
1487 -- has been created for the purposes of allocating or deallocating a
1488 -- build-in-place object. Such access types have explicitly set pools
1489 -- and finalization masters.
1490
1491 if No (Associated_Storage_Pool (T)) then
1492 Set_Finalization_Master (T, Empty);
1493 end if;
1494
1495 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1496 -- attributes
1497
1498 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1499 Set_Is_Access_Constant (T, Constant_Present (Def));
1500 end Access_Type_Declaration;
1501
1502 ----------------------------------
1503 -- Add_Interface_Tag_Components --
1504 ----------------------------------
1505
1506 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1507 Loc : constant Source_Ptr := Sloc (N);
1508 L : List_Id;
1509 Last_Tag : Node_Id;
1510
1511 procedure Add_Tag (Iface : Entity_Id);
1512 -- Add tag for one of the progenitor interfaces
1513
1514 -------------
1515 -- Add_Tag --
1516 -------------
1517
1518 procedure Add_Tag (Iface : Entity_Id) is
1519 Decl : Node_Id;
1520 Def : Node_Id;
1521 Tag : Entity_Id;
1522 Offset : Entity_Id;
1523
1524 begin
1525 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1526
1527 -- This is a reasonable place to propagate predicates
1528
1529 if Has_Predicates (Iface) then
1530 Set_Has_Predicates (Typ);
1531 end if;
1532
1533 Def :=
1534 Make_Component_Definition (Loc,
1535 Aliased_Present => True,
1536 Subtype_Indication =>
1537 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1538
1539 Tag := Make_Temporary (Loc, 'V');
1540
1541 Decl :=
1542 Make_Component_Declaration (Loc,
1543 Defining_Identifier => Tag,
1544 Component_Definition => Def);
1545
1546 Analyze_Component_Declaration (Decl);
1547
1548 Set_Analyzed (Decl);
1549 Mutate_Ekind (Tag, E_Component);
1550 Set_Is_Tag (Tag);
1551 Set_Is_Aliased (Tag);
1552 Set_Is_Independent (Tag);
1553 Set_Related_Type (Tag, Iface);
1554 Reinit_Component_Location (Tag);
1555
1556 pragma Assert (Is_Frozen (Iface));
1557
1558 Set_DT_Entry_Count (Tag,
1559 DT_Entry_Count (First_Entity (Iface)));
1560
1561 if No (Last_Tag) then
1562 Prepend (Decl, L);
1563 else
1564 Insert_After (Last_Tag, Decl);
1565 end if;
1566
1567 Last_Tag := Decl;
1568
1569 -- If the ancestor has discriminants we need to give special support
1570 -- to store the offset_to_top value of the secondary dispatch tables.
1571 -- For this purpose we add a supplementary component just after the
1572 -- field that contains the tag associated with each secondary DT.
1573
1574 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1575 Def :=
1576 Make_Component_Definition (Loc,
1577 Subtype_Indication =>
1578 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1579
1580 Offset := Make_Temporary (Loc, 'V');
1581
1582 Decl :=
1583 Make_Component_Declaration (Loc,
1584 Defining_Identifier => Offset,
1585 Component_Definition => Def);
1586
1587 Analyze_Component_Declaration (Decl);
1588
1589 Set_Analyzed (Decl);
1590 Mutate_Ekind (Offset, E_Component);
1591 Set_Is_Aliased (Offset);
1592 Set_Is_Independent (Offset);
1593 Set_Related_Type (Offset, Iface);
1594 Reinit_Component_Location (Offset);
1595 Insert_After (Last_Tag, Decl);
1596 Last_Tag := Decl;
1597 end if;
1598 end Add_Tag;
1599
1600 -- Local variables
1601
1602 Elmt : Elmt_Id;
1603 Ext : Node_Id;
1604 Comp : Node_Id;
1605
1606 -- Start of processing for Add_Interface_Tag_Components
1607
1608 begin
1609 if not RTE_Available (RE_Interface_Tag) then
1610 Error_Msg_N
1611 ("(Ada 2005) interface types not supported by this run-time!", N);
1612 return;
1613 end if;
1614
1615 if Ekind (Typ) /= E_Record_Type
1616 or else (Is_Concurrent_Record_Type (Typ)
1617 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1618 or else (not Is_Concurrent_Record_Type (Typ)
1619 and then No (Interfaces (Typ))
1620 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1621 then
1622 return;
1623 end if;
1624
1625 -- Find the current last tag
1626
1627 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1628 Ext := Record_Extension_Part (Type_Definition (N));
1629 else
1630 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1631 Ext := Type_Definition (N);
1632 end if;
1633
1634 Last_Tag := Empty;
1635
1636 if not (Present (Component_List (Ext))) then
1637 Set_Null_Present (Ext, False);
1638 L := New_List;
1639 Set_Component_List (Ext,
1640 Make_Component_List (Loc,
1641 Component_Items => L,
1642 Null_Present => False));
1643 else
1644 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1645 L := Component_Items
1646 (Component_List
1647 (Record_Extension_Part
1648 (Type_Definition (N))));
1649 else
1650 L := Component_Items
1651 (Component_List
1652 (Type_Definition (N)));
1653 end if;
1654
1655 -- Find the last tag component
1656
1657 Comp := First (L);
1658 while Present (Comp) loop
1659 if Nkind (Comp) = N_Component_Declaration
1660 and then Is_Tag (Defining_Identifier (Comp))
1661 then
1662 Last_Tag := Comp;
1663 end if;
1664
1665 Next (Comp);
1666 end loop;
1667 end if;
1668
1669 -- At this point L references the list of components and Last_Tag
1670 -- references the current last tag (if any). Now we add the tag
1671 -- corresponding with all the interfaces that are not implemented
1672 -- by the parent.
1673
1674 if Present (Interfaces (Typ)) then
1675 Elmt := First_Elmt (Interfaces (Typ));
1676 while Present (Elmt) loop
1677 Add_Tag (Node (Elmt));
1678 Next_Elmt (Elmt);
1679 end loop;
1680 end if;
1681 end Add_Interface_Tag_Components;
1682
1683 -------------------------------------
1684 -- Add_Internal_Interface_Entities --
1685 -------------------------------------
1686
1687 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1688 Elmt : Elmt_Id;
1689 Iface : Entity_Id;
1690 Iface_Elmt : Elmt_Id;
1691 Iface_Prim : Entity_Id;
1692 Ifaces_List : Elist_Id;
1693 New_Subp : Entity_Id := Empty;
1694 Prim : Entity_Id;
1695 Restore_Scope : Boolean := False;
1696
1697 begin
1698 pragma Assert (Ada_Version >= Ada_2005
1699 and then Is_Record_Type (Tagged_Type)
1700 and then Is_Tagged_Type (Tagged_Type)
1701 and then Has_Interfaces (Tagged_Type)
1702 and then not Is_Interface (Tagged_Type));
1703
1704 -- Ensure that the internal entities are added to the scope of the type
1705
1706 if Scope (Tagged_Type) /= Current_Scope then
1707 Push_Scope (Scope (Tagged_Type));
1708 Restore_Scope := True;
1709 end if;
1710
1711 Collect_Interfaces (Tagged_Type, Ifaces_List);
1712
1713 Iface_Elmt := First_Elmt (Ifaces_List);
1714 while Present (Iface_Elmt) loop
1715 Iface := Node (Iface_Elmt);
1716
1717 -- Originally we excluded here from this processing interfaces that
1718 -- are parents of Tagged_Type because their primitives are located
1719 -- in the primary dispatch table (and hence no auxiliary internal
1720 -- entities are required to handle secondary dispatch tables in such
1721 -- case). However, these auxiliary entities are also required to
1722 -- handle derivations of interfaces in formals of generics (see
1723 -- Derive_Subprograms).
1724
1725 Elmt := First_Elmt (Primitive_Operations (Iface));
1726 while Present (Elmt) loop
1727 Iface_Prim := Node (Elmt);
1728
1729 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1730 Prim :=
1731 Find_Primitive_Covering_Interface
1732 (Tagged_Type => Tagged_Type,
1733 Iface_Prim => Iface_Prim);
1734
1735 if No (Prim) and then Serious_Errors_Detected > 0 then
1736 goto Continue;
1737 end if;
1738
1739 pragma Assert (Present (Prim));
1740
1741 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1742 -- differs from the name of the interface primitive then it is
1743 -- a private primitive inherited from a parent type. In such
1744 -- case, given that Tagged_Type covers the interface, the
1745 -- inherited private primitive becomes visible. For such
1746 -- purpose we add a new entity that renames the inherited
1747 -- private primitive.
1748
1749 if Chars (Prim) /= Chars (Iface_Prim) then
1750 pragma Assert (Has_Suffix (Prim, 'P'));
1751 Derive_Subprogram
1752 (New_Subp => New_Subp,
1753 Parent_Subp => Iface_Prim,
1754 Derived_Type => Tagged_Type,
1755 Parent_Type => Iface);
1756 Set_Alias (New_Subp, Prim);
1757 Set_Is_Abstract_Subprogram
1758 (New_Subp, Is_Abstract_Subprogram (Prim));
1759 end if;
1760
1761 Derive_Subprogram
1762 (New_Subp => New_Subp,
1763 Parent_Subp => Iface_Prim,
1764 Derived_Type => Tagged_Type,
1765 Parent_Type => Iface);
1766
1767 declare
1768 Anc : Entity_Id;
1769 begin
1770 if Is_Inherited_Operation (Prim)
1771 and then Present (Alias (Prim))
1772 then
1773 Anc := Alias (Prim);
1774 else
1775 Anc := Overridden_Operation (Prim);
1776 end if;
1777
1778 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1779 -- nonconforming preconditions in both an ancestor and
1780 -- a progenitor operation.
1781
1782 -- If the operation is a primitive wrapper it is an explicit
1783 -- (overriding) operqtion and all is fine.
1784
1785 if Present (Anc)
1786 and then Has_Non_Trivial_Precondition (Anc)
1787 and then Has_Non_Trivial_Precondition (Iface_Prim)
1788 then
1789 if Is_Abstract_Subprogram (Prim)
1790 or else
1791 (Ekind (Prim) = E_Procedure
1792 and then Nkind (Parent (Prim)) =
1793 N_Procedure_Specification
1794 and then Null_Present (Parent (Prim)))
1795 or else Is_Primitive_Wrapper (Prim)
1796 then
1797 null;
1798
1799 -- The operation is inherited and must be overridden
1800
1801 elsif not Comes_From_Source (Prim) then
1802 Error_Msg_NE
1803 ("&inherits non-conforming preconditions and must "
1804 & "be overridden (RM 6.1.1 (10-16))",
1805 Parent (Tagged_Type), Prim);
1806 end if;
1807 end if;
1808 end;
1809
1810 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1811 -- associated with interface types. These entities are
1812 -- only registered in the list of primitives of its
1813 -- corresponding tagged type because they are only used
1814 -- to fill the contents of the secondary dispatch tables.
1815 -- Therefore they are removed from the homonym chains.
1816
1817 Set_Is_Hidden (New_Subp);
1818 Set_Is_Internal (New_Subp);
1819 Set_Alias (New_Subp, Prim);
1820 Set_Is_Abstract_Subprogram
1821 (New_Subp, Is_Abstract_Subprogram (Prim));
1822 Set_Interface_Alias (New_Subp, Iface_Prim);
1823
1824 -- If the returned type is an interface then propagate it to
1825 -- the returned type. Needed by the thunk to generate the code
1826 -- which displaces "this" to reference the corresponding
1827 -- secondary dispatch table in the returned object.
1828
1829 if Is_Interface (Etype (Iface_Prim)) then
1830 Set_Etype (New_Subp, Etype (Iface_Prim));
1831 end if;
1832
1833 -- Internal entities associated with interface types are only
1834 -- registered in the list of primitives of the tagged type.
1835 -- They are only used to fill the contents of the secondary
1836 -- dispatch tables. Therefore they are not needed in the
1837 -- homonym chains.
1838
1839 Remove_Homonym (New_Subp);
1840
1841 -- Hidden entities associated with interfaces must have set
1842 -- the Has_Delay_Freeze attribute to ensure that, in case
1843 -- of locally defined tagged types (or compiling with static
1844 -- dispatch tables generation disabled) the corresponding
1845 -- entry of the secondary dispatch table is filled when such
1846 -- an entity is frozen.
1847
1848 Set_Has_Delayed_Freeze (New_Subp);
1849 end if;
1850
1851 <<Continue>>
1852 Next_Elmt (Elmt);
1853 end loop;
1854
1855 Next_Elmt (Iface_Elmt);
1856 end loop;
1857
1858 if Restore_Scope then
1859 Pop_Scope;
1860 end if;
1861 end Add_Internal_Interface_Entities;
1862
1863 -----------------------------------
1864 -- Analyze_Component_Declaration --
1865 -----------------------------------
1866
1867 procedure Analyze_Component_Declaration (N : Node_Id) is
1868 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1869 Id : constant Entity_Id := Defining_Identifier (N);
1870 E : constant Node_Id := Expression (N);
1871 Typ : constant Node_Id :=
1872 Subtype_Indication (Component_Definition (N));
1873 T : Entity_Id;
1874 P : Entity_Id;
1875
1876 function Contains_POC (Constr : Node_Id) return Boolean;
1877 -- Determines whether a constraint uses the discriminant of a record
1878 -- type thus becoming a per-object constraint (POC).
1879
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1881 -- Typ is the type of the current component, check whether this type is
1882 -- a limited type. Used to validate declaration against that of
1883 -- enclosing record.
1884
1885 ------------------
1886 -- Contains_POC --
1887 ------------------
1888
1889 function Contains_POC (Constr : Node_Id) return Boolean is
1890 begin
1891 -- Prevent cascaded errors
1892
1893 if Error_Posted (Constr) then
1894 return False;
1895 end if;
1896
1897 case Nkind (Constr) is
1898 when N_Attribute_Reference =>
1899 return Attribute_Name (Constr) = Name_Access
1900 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1901
1902 when N_Discriminant_Association =>
1903 return Denotes_Discriminant (Expression (Constr));
1904
1905 when N_Identifier =>
1906 return Denotes_Discriminant (Constr);
1907
1908 when N_Index_Or_Discriminant_Constraint =>
1909 declare
1910 IDC : Node_Id;
1911
1912 begin
1913 IDC := First (Constraints (Constr));
1914 while Present (IDC) loop
1915
1916 -- One per-object constraint is sufficient
1917
1918 if Contains_POC (IDC) then
1919 return True;
1920 end if;
1921
1922 Next (IDC);
1923 end loop;
1924
1925 return False;
1926 end;
1927
1928 when N_Range =>
1929 return Denotes_Discriminant (Low_Bound (Constr))
1930 or else
1931 Denotes_Discriminant (High_Bound (Constr));
1932
1933 when N_Range_Constraint =>
1934 return Denotes_Discriminant (Range_Expression (Constr));
1935
1936 when others =>
1937 return False;
1938 end case;
1939 end Contains_POC;
1940
1941 ----------------------
1942 -- Is_Known_Limited --
1943 ----------------------
1944
1945 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1946 P : constant Entity_Id := Etype (Typ);
1947 R : constant Entity_Id := Root_Type (Typ);
1948
1949 begin
1950 if Is_Limited_Record (Typ) then
1951 return True;
1952
1953 -- If the root type is limited (and not a limited interface) so is
1954 -- the current type.
1955
1956 elsif Is_Limited_Record (R)
1957 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1958 then
1959 return True;
1960
1961 -- Else the type may have a limited interface progenitor, but a
1962 -- limited record parent that is not an interface.
1963
1964 elsif R /= P
1965 and then Is_Limited_Record (P)
1966 and then not Is_Interface (P)
1967 then
1968 return True;
1969
1970 else
1971 return False;
1972 end if;
1973 end Is_Known_Limited;
1974
1975 -- Start of processing for Analyze_Component_Declaration
1976
1977 begin
1978 Generate_Definition (Id);
1979 Enter_Name (Id);
1980
1981 if Present (Typ) then
1982 T := Find_Type_Of_Object
1983 (Subtype_Indication (Component_Definition (N)), N);
1984
1985 -- Ada 2005 (AI-230): Access Definition case
1986
1987 else
1988 pragma Assert (Present
1989 (Access_Definition (Component_Definition (N))));
1990
1991 T := Access_Definition
1992 (Related_Nod => N,
1993 N => Access_Definition (Component_Definition (N)));
1994 Set_Is_Local_Anonymous_Access (T);
1995
1996 -- Ada 2005 (AI-254)
1997
1998 if Present (Access_To_Subprogram_Definition
1999 (Access_Definition (Component_Definition (N))))
2000 and then Protected_Present (Access_To_Subprogram_Definition
2001 (Access_Definition
2002 (Component_Definition (N))))
2003 then
2004 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2005 end if;
2006 end if;
2007
2008 -- If the subtype is a constrained subtype of the enclosing record,
2009 -- (which must have a partial view) the back-end does not properly
2010 -- handle the recursion. Rewrite the component declaration with an
2011 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2012 -- the tree directly because side effects have already been removed from
2013 -- discriminant constraints.
2014
2015 if Ekind (T) = E_Access_Subtype
2016 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2017 and then Comes_From_Source (T)
2018 and then Nkind (Parent (T)) = N_Subtype_Declaration
2019 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2020 then
2021 Rewrite
2022 (Subtype_Indication (Component_Definition (N)),
2023 New_Copy_Tree (Subtype_Indication (Parent (T))));
2024 T := Find_Type_Of_Object
2025 (Subtype_Indication (Component_Definition (N)), N);
2026 end if;
2027
2028 -- If the component declaration includes a default expression, then we
2029 -- check that the component is not of a limited type (RM 3.7(5)),
2030 -- and do the special preanalysis of the expression (see section on
2031 -- "Handling of Default and Per-Object Expressions" in the spec of
2032 -- package Sem).
2033
2034 if Present (E) then
2035 Preanalyze_Default_Expression (E, T);
2036 Check_Initialization (T, E);
2037
2038 if Ada_Version >= Ada_2005
2039 and then Ekind (T) = E_Anonymous_Access_Type
2040 and then Etype (E) /= Any_Type
2041 then
2042 -- Check RM 3.9.2(9): "if the expected type for an expression is
2043 -- an anonymous access-to-specific tagged type, then the object
2044 -- designated by the expression shall not be dynamically tagged
2045 -- unless it is a controlling operand in a call on a dispatching
2046 -- operation"
2047
2048 if Is_Tagged_Type (Directly_Designated_Type (T))
2049 and then
2050 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2051 and then
2052 Ekind (Directly_Designated_Type (Etype (E))) =
2053 E_Class_Wide_Type
2054 then
2055 Error_Msg_N
2056 ("access to specific tagged type required (RM 3.9.2(9))", E);
2057 end if;
2058
2059 -- (Ada 2005: AI-230): Accessibility check for anonymous
2060 -- components
2061
2062 if Type_Access_Level (Etype (E)) >
2063 Deepest_Type_Access_Level (T)
2064 then
2065 Error_Msg_N
2066 ("expression has deeper access level than component " &
2067 "(RM 3.10.2 (12.2))", E);
2068 end if;
2069
2070 -- The initialization expression is a reference to an access
2071 -- discriminant. The type of the discriminant is always deeper
2072 -- than any access type.
2073
2074 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2075 and then Is_Entity_Name (E)
2076 and then Ekind (Entity (E)) = E_In_Parameter
2077 and then Present (Discriminal_Link (Entity (E)))
2078 then
2079 Error_Msg_N
2080 ("discriminant has deeper accessibility level than target",
2081 E);
2082 end if;
2083 end if;
2084 end if;
2085
2086 -- The parent type may be a private view with unknown discriminants,
2087 -- and thus unconstrained. Regular components must be constrained.
2088
2089 if not Is_Definite_Subtype (T)
2090 and then Chars (Id) /= Name_uParent
2091 then
2092 if Is_Class_Wide_Type (T) then
2093 Error_Msg_N
2094 ("class-wide subtype with unknown discriminants" &
2095 " in component declaration",
2096 Subtype_Indication (Component_Definition (N)));
2097 else
2098 Error_Msg_N
2099 ("unconstrained subtype in component declaration",
2100 Subtype_Indication (Component_Definition (N)));
2101 end if;
2102
2103 -- Components cannot be abstract, except for the special case of
2104 -- the _Parent field (case of extending an abstract tagged type)
2105
2106 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2107 Error_Msg_N ("type of a component cannot be abstract", N);
2108 end if;
2109
2110 Set_Etype (Id, T);
2111
2112 if Aliased_Present (Component_Definition (N)) then
2113 Set_Is_Aliased (Id);
2114
2115 -- AI12-001: All aliased objects are considered to be specified as
2116 -- independently addressable (RM C.6(8.1/4)).
2117
2118 Set_Is_Independent (Id);
2119 end if;
2120
2121 -- The component declaration may have a per-object constraint, set
2122 -- the appropriate flag in the defining identifier of the subtype.
2123
2124 if Present (Subtype_Indication (Component_Definition (N))) then
2125 declare
2126 Sindic : constant Node_Id :=
2127 Subtype_Indication (Component_Definition (N));
2128 begin
2129 if Nkind (Sindic) = N_Subtype_Indication
2130 and then Present (Constraint (Sindic))
2131 and then Contains_POC (Constraint (Sindic))
2132 then
2133 Set_Has_Per_Object_Constraint (Id);
2134 end if;
2135 end;
2136 end if;
2137
2138 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2139 -- out some static checks.
2140
2141 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2142 Null_Exclusion_Static_Checks (N);
2143 end if;
2144
2145 -- If this component is private (or depends on a private type), flag the
2146 -- record type to indicate that some operations are not available.
2147
2148 P := Private_Component (T);
2149
2150 if Present (P) then
2151
2152 -- Check for circular definitions
2153
2154 if P = Any_Type then
2155 Set_Etype (Id, Any_Type);
2156
2157 -- There is a gap in the visibility of operations only if the
2158 -- component type is not defined in the scope of the record type.
2159
2160 elsif Scope (P) = Scope (Current_Scope) then
2161 null;
2162
2163 elsif Is_Limited_Type (P) then
2164 Set_Is_Limited_Composite (Current_Scope);
2165
2166 else
2167 Set_Is_Private_Composite (Current_Scope);
2168 end if;
2169 end if;
2170
2171 if P /= Any_Type
2172 and then Is_Limited_Type (T)
2173 and then Chars (Id) /= Name_uParent
2174 and then Is_Tagged_Type (Current_Scope)
2175 then
2176 if Is_Derived_Type (Current_Scope)
2177 and then not Is_Known_Limited (Current_Scope)
2178 then
2179 Error_Msg_N
2180 ("extension of nonlimited type cannot have limited components",
2181 N);
2182
2183 if Is_Interface (Root_Type (Current_Scope)) then
2184 Error_Msg_N
2185 ("\limitedness is not inherited from limited interface", N);
2186 Error_Msg_N ("\add LIMITED to type indication", N);
2187 end if;
2188
2189 Explain_Limited_Type (T, N);
2190 Set_Etype (Id, Any_Type);
2191 Set_Is_Limited_Composite (Current_Scope, False);
2192
2193 elsif not Is_Derived_Type (Current_Scope)
2194 and then not Is_Limited_Record (Current_Scope)
2195 and then not Is_Concurrent_Type (Current_Scope)
2196 then
2197 Error_Msg_N
2198 ("nonlimited tagged type cannot have limited components", N);
2199 Explain_Limited_Type (T, N);
2200 Set_Etype (Id, Any_Type);
2201 Set_Is_Limited_Composite (Current_Scope, False);
2202 end if;
2203 end if;
2204
2205 -- If the component is an unconstrained task or protected type with
2206 -- discriminants, the component and the enclosing record are limited
2207 -- and the component is constrained by its default values. Compute
2208 -- its actual subtype, else it may be allocated the maximum size by
2209 -- the backend, and possibly overflow.
2210
2211 if Is_Concurrent_Type (T)
2212 and then not Is_Constrained (T)
2213 and then Has_Discriminants (T)
2214 and then not Has_Discriminants (Current_Scope)
2215 then
2216 declare
2217 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2218
2219 begin
2220 Set_Etype (Id, Act_T);
2221
2222 -- Rewrite component definition to use the constrained subtype
2223
2224 Rewrite (Component_Definition (N),
2225 Make_Component_Definition (Loc,
2226 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2227 end;
2228 end if;
2229
2230 Set_Original_Record_Component (Id, Id);
2231
2232 if Has_Aspects (N) then
2233 Analyze_Aspect_Specifications (N, Id);
2234 end if;
2235
2236 Analyze_Dimension (N);
2237 end Analyze_Component_Declaration;
2238
2239 --------------------------
2240 -- Analyze_Declarations --
2241 --------------------------
2242
2243 procedure Analyze_Declarations (L : List_Id) is
2244 Decl : Node_Id;
2245
2246 procedure Adjust_Decl;
2247 -- Adjust Decl not to include implicit label declarations, since these
2248 -- have strange Sloc values that result in elaboration check problems.
2249 -- (They have the sloc of the label as found in the source, and that
2250 -- is ahead of the current declarative part).
2251
2252 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2253 -- Create the subprogram bodies which verify the run-time semantics of
2254 -- the pragmas listed below for each elibigle type found in declarative
2255 -- list Decls. The pragmas are:
2256 --
2257 -- Default_Initial_Condition
2258 -- Invariant
2259 -- Type_Invariant
2260 --
2261 -- Context denotes the owner of the declarative list.
2262
2263 procedure Check_Entry_Contracts;
2264 -- Perform a preanalysis of the pre- and postconditions of an entry
2265 -- declaration. This must be done before full resolution and creation
2266 -- of the parameter block, etc. to catch illegal uses within the
2267 -- contract expression. Full analysis of the expression is done when
2268 -- the contract is processed.
2269
2270 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2271 -- Check if a nested package has entities within it that rely on library
2272 -- level private types where the full view has not been completed for
2273 -- the purposes of checking if it is acceptable to freeze an expression
2274 -- function at the point of declaration.
2275
2276 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2277 -- Determine whether Body_Decl denotes the body of a late controlled
2278 -- primitive (either Initialize, Adjust or Finalize). If this is the
2279 -- case, add a proper spec if the body lacks one. The spec is inserted
2280 -- before Body_Decl and immediately analyzed.
2281
2282 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2283 -- Spec_Id is the entity of a package that may define abstract states,
2284 -- and in the case of a child unit, whose ancestors may define abstract
2285 -- states. If the states have partial visible refinement, remove the
2286 -- partial visibility of each constituent at the end of the package
2287 -- spec and body declarations.
2288
2289 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2290 -- Spec_Id is the entity of a package that may define abstract states.
2291 -- If the states have visible refinement, remove the visibility of each
2292 -- constituent at the end of the package body declaration.
2293
2294 procedure Resolve_Aspects;
2295 -- Utility to resolve the expressions of aspects at the end of a list of
2296 -- declarations, or before a declaration that freezes previous entities,
2297 -- such as in a subprogram body.
2298
2299 -----------------
2300 -- Adjust_Decl --
2301 -----------------
2302
2303 procedure Adjust_Decl is
2304 begin
2305 while Present (Prev (Decl))
2306 and then Nkind (Decl) = N_Implicit_Label_Declaration
2307 loop
2308 Prev (Decl);
2309 end loop;
2310 end Adjust_Decl;
2311
2312 ----------------------------
2313 -- Build_Assertion_Bodies --
2314 ----------------------------
2315
2316 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2317 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2318 -- Create the subprogram bodies which verify the run-time semantics
2319 -- of the pragmas listed below for type Typ. The pragmas are:
2320 --
2321 -- Default_Initial_Condition
2322 -- Invariant
2323 -- Type_Invariant
2324
2325 -------------------------------------
2326 -- Build_Assertion_Bodies_For_Type --
2327 -------------------------------------
2328
2329 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2330 begin
2331 if Nkind (Context) = N_Package_Specification then
2332
2333 -- Preanalyze and resolve the class-wide invariants of an
2334 -- interface at the end of whichever declarative part has the
2335 -- interface type. Note that an interface may be declared in
2336 -- any non-package declarative part, but reaching the end of
2337 -- such a declarative part will always freeze the type and
2338 -- generate the invariant procedure (see Freeze_Type).
2339
2340 if Is_Interface (Typ) then
2341
2342 -- Interfaces are treated as the partial view of a private
2343 -- type, in order to achieve uniformity with the general
2344 -- case. As a result, an interface receives only a "partial"
2345 -- invariant procedure, which is never called.
2346
2347 if Has_Own_Invariants (Typ) then
2348 Build_Invariant_Procedure_Body
2349 (Typ => Typ,
2350 Partial_Invariant => True);
2351 end if;
2352
2353 elsif Decls = Visible_Declarations (Context) then
2354 -- Preanalyze and resolve the invariants of a private type
2355 -- at the end of the visible declarations to catch potential
2356 -- errors. Inherited class-wide invariants are not included
2357 -- because they have already been resolved.
2358
2359 if Ekind (Typ) in E_Limited_Private_Type
2360 | E_Private_Type
2361 | E_Record_Type_With_Private
2362 and then Has_Own_Invariants (Typ)
2363 then
2364 Build_Invariant_Procedure_Body
2365 (Typ => Typ,
2366 Partial_Invariant => True);
2367 end if;
2368
2369 -- Preanalyze and resolve the Default_Initial_Condition
2370 -- assertion expression at the end of the declarations to
2371 -- catch any errors.
2372
2373 if Ekind (Typ) in E_Limited_Private_Type
2374 | E_Private_Type
2375 | E_Record_Type_With_Private
2376 and then Has_Own_DIC (Typ)
2377 then
2378 Build_DIC_Procedure_Body
2379 (Typ => Typ,
2380 Partial_DIC => True);
2381 end if;
2382
2383 elsif Decls = Private_Declarations (Context) then
2384
2385 -- Preanalyze and resolve the invariants of a private type's
2386 -- full view at the end of the private declarations to catch
2387 -- potential errors.
2388
2389 if (not Is_Private_Type (Typ)
2390 or else Present (Underlying_Full_View (Typ)))
2391 and then Has_Private_Declaration (Typ)
2392 and then Has_Invariants (Typ)
2393 then
2394 Build_Invariant_Procedure_Body (Typ);
2395 end if;
2396
2397 if (not Is_Private_Type (Typ)
2398 or else Present (Underlying_Full_View (Typ)))
2399 and then Has_Private_Declaration (Typ)
2400 and then Has_DIC (Typ)
2401 then
2402 Build_DIC_Procedure_Body (Typ);
2403 end if;
2404 end if;
2405 end if;
2406 end Build_Assertion_Bodies_For_Type;
2407
2408 -- Local variables
2409
2410 Decl : Node_Id;
2411 Decl_Id : Entity_Id;
2412
2413 -- Start of processing for Build_Assertion_Bodies
2414
2415 begin
2416 Decl := First (Decls);
2417 while Present (Decl) loop
2418 if Is_Declaration (Decl) then
2419 Decl_Id := Defining_Entity (Decl);
2420
2421 if Is_Type (Decl_Id) then
2422 Build_Assertion_Bodies_For_Type (Decl_Id);
2423 end if;
2424 end if;
2425
2426 Next (Decl);
2427 end loop;
2428 end Build_Assertion_Bodies;
2429
2430 ---------------------------
2431 -- Check_Entry_Contracts --
2432 ---------------------------
2433
2434 procedure Check_Entry_Contracts is
2435 ASN : Node_Id;
2436 Ent : Entity_Id;
2437 Exp : Node_Id;
2438
2439 begin
2440 Ent := First_Entity (Current_Scope);
2441 while Present (Ent) loop
2442
2443 -- This only concerns entries with pre/postconditions
2444
2445 if Ekind (Ent) = E_Entry
2446 and then Present (Contract (Ent))
2447 and then Present (Pre_Post_Conditions (Contract (Ent)))
2448 then
2449 ASN := Pre_Post_Conditions (Contract (Ent));
2450 Push_Scope (Ent);
2451 Install_Formals (Ent);
2452
2453 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2454 -- is performed on a copy of the pragma expression, to prevent
2455 -- modifying the original expression.
2456
2457 while Present (ASN) loop
2458 if Nkind (ASN) = N_Pragma then
2459 Exp :=
2460 New_Copy_Tree
2461 (Expression
2462 (First (Pragma_Argument_Associations (ASN))));
2463 Set_Parent (Exp, ASN);
2464
2465 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2466 end if;
2467
2468 ASN := Next_Pragma (ASN);
2469 end loop;
2470
2471 End_Scope;
2472 end if;
2473
2474 Next_Entity (Ent);
2475 end loop;
2476 end Check_Entry_Contracts;
2477
2478 ----------------------------------
2479 -- Contains_Lib_Incomplete_Type --
2480 ----------------------------------
2481
2482 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2483 Curr : Entity_Id;
2484
2485 begin
2486 -- Avoid looking through scopes that do not meet the precondition of
2487 -- Pkg not being within a library unit spec.
2488
2489 if not Is_Compilation_Unit (Pkg)
2490 and then not Is_Generic_Instance (Pkg)
2491 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2492 then
2493 -- Loop through all entities in the current scope to identify
2494 -- an entity that depends on a private type.
2495
2496 Curr := First_Entity (Pkg);
2497 loop
2498 if Nkind (Curr) in N_Entity
2499 and then Depends_On_Private (Curr)
2500 then
2501 return True;
2502 end if;
2503
2504 exit when Last_Entity (Current_Scope) = Curr;
2505 Next_Entity (Curr);
2506 end loop;
2507 end if;
2508
2509 return False;
2510 end Contains_Lib_Incomplete_Type;
2511
2512 --------------------------------------
2513 -- Handle_Late_Controlled_Primitive --
2514 --------------------------------------
2515
2516 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2517 Body_Spec : constant Node_Id := Specification (Body_Decl);
2518 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2519 Loc : constant Source_Ptr := Sloc (Body_Id);
2520 Params : constant List_Id :=
2521 Parameter_Specifications (Body_Spec);
2522 Spec : Node_Id;
2523 Spec_Id : Entity_Id;
2524 Typ : Node_Id;
2525
2526 begin
2527 -- Consider only procedure bodies whose name matches one of the three
2528 -- controlled primitives.
2529
2530 if Nkind (Body_Spec) /= N_Procedure_Specification
2531 or else Chars (Body_Id) not in Name_Adjust
2532 | Name_Finalize
2533 | Name_Initialize
2534 then
2535 return;
2536
2537 -- A controlled primitive must have exactly one formal which is not
2538 -- an anonymous access type.
2539
2540 elsif List_Length (Params) /= 1 then
2541 return;
2542 end if;
2543
2544 Typ := Parameter_Type (First (Params));
2545
2546 if Nkind (Typ) = N_Access_Definition then
2547 return;
2548 end if;
2549
2550 Find_Type (Typ);
2551
2552 -- The type of the formal must be derived from [Limited_]Controlled
2553
2554 if not Is_Controlled (Entity (Typ)) then
2555 return;
2556 end if;
2557
2558 -- Check whether a specification exists for this body. We do not
2559 -- analyze the spec of the body in full, because it will be analyzed
2560 -- again when the body is properly analyzed, and we cannot create
2561 -- duplicate entries in the formals chain. We look for an explicit
2562 -- specification because the body may be an overriding operation and
2563 -- an inherited spec may be present.
2564
2565 Spec_Id := Current_Entity (Body_Id);
2566
2567 while Present (Spec_Id) loop
2568 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2569 and then Scope (Spec_Id) = Current_Scope
2570 and then Present (First_Formal (Spec_Id))
2571 and then No (Next_Formal (First_Formal (Spec_Id)))
2572 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2573 and then Comes_From_Source (Spec_Id)
2574 then
2575 return;
2576 end if;
2577
2578 Spec_Id := Homonym (Spec_Id);
2579 end loop;
2580
2581 -- At this point the body is known to be a late controlled primitive.
2582 -- Generate a matching spec and insert it before the body. Note the
2583 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2584 -- tree in this case.
2585
2586 Spec := Copy_Separate_Tree (Body_Spec);
2587
2588 -- Ensure that the subprogram declaration does not inherit the null
2589 -- indicator from the body as we now have a proper spec/body pair.
2590
2591 Set_Null_Present (Spec, False);
2592
2593 -- Ensure that the freeze node is inserted after the declaration of
2594 -- the primitive since its expansion will freeze the primitive.
2595
2596 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2597
2598 Insert_Before_And_Analyze (Body_Decl, Decl);
2599 end Handle_Late_Controlled_Primitive;
2600
2601 ----------------------------------------
2602 -- Remove_Partial_Visible_Refinements --
2603 ----------------------------------------
2604
2605 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2606 State_Elmt : Elmt_Id;
2607 begin
2608 if Present (Abstract_States (Spec_Id)) then
2609 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2610 while Present (State_Elmt) loop
2611 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2612 Next_Elmt (State_Elmt);
2613 end loop;
2614 end if;
2615
2616 -- For a child unit, also hide the partial state refinement from
2617 -- ancestor packages.
2618
2619 if Is_Child_Unit (Spec_Id) then
2620 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2621 end if;
2622 end Remove_Partial_Visible_Refinements;
2623
2624 --------------------------------
2625 -- Remove_Visible_Refinements --
2626 --------------------------------
2627
2628 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2629 State_Elmt : Elmt_Id;
2630 begin
2631 if Present (Abstract_States (Spec_Id)) then
2632 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2633 while Present (State_Elmt) loop
2634 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2635 Next_Elmt (State_Elmt);
2636 end loop;
2637 end if;
2638 end Remove_Visible_Refinements;
2639
2640 ---------------------
2641 -- Resolve_Aspects --
2642 ---------------------
2643
2644 procedure Resolve_Aspects is
2645 E : Entity_Id;
2646
2647 begin
2648 E := First_Entity (Current_Scope);
2649 while Present (E) loop
2650 Resolve_Aspect_Expressions (E);
2651
2652 -- Now that the aspect expressions have been resolved, if this is
2653 -- at the end of the visible declarations, we can set the flag
2654 -- Known_To_Have_Preelab_Init properly on types declared in the
2655 -- visible part, which is needed for checking whether full types
2656 -- in the private part satisfy the Preelaborable_Initialization
2657 -- aspect of the partial view. We can't wait for the creation of
2658 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2659 -- freeze point may occur after the end of the package declaration
2660 -- (in the case of nested packages).
2661
2662 if Is_Type (E)
2663 and then L = Visible_Declarations (Parent (L))
2664 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2665 then
2666 declare
2667 ASN : constant Node_Id :=
2668 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2669 Expr : constant Node_Id := Expression (ASN);
2670 begin
2671 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2672 -- expression, or if the expression is True (or was folded
2673 -- to True), or if the expression is a conjunction of one or
2674 -- more Preelaborable_Initialization attributes applied to
2675 -- formal types and wasn't folded to False. (Note that
2676 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2677 -- Original_Node if needed, hence test for Standard_False.)
2678
2679 if not Present (Expr)
2680 or else (Is_Entity_Name (Expr)
2681 and then Entity (Expr) = Standard_True)
2682 or else
2683 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2684 and then
2685 not (Is_Entity_Name (Expr)
2686 and then Entity (Expr) = Standard_False))
2687 then
2688 Set_Known_To_Have_Preelab_Init (E);
2689 end if;
2690 end;
2691 end if;
2692
2693 Next_Entity (E);
2694 end loop;
2695 end Resolve_Aspects;
2696
2697 -- Local variables
2698
2699 Context : Node_Id := Empty;
2700 Ctrl_Typ : Entity_Id := Empty;
2701 Freeze_From : Entity_Id := Empty;
2702 Next_Decl : Node_Id;
2703
2704 -- Start of processing for Analyze_Declarations
2705
2706 begin
2707 Decl := First (L);
2708 while Present (Decl) loop
2709
2710 -- Complete analysis of declaration
2711
2712 Analyze (Decl);
2713 Next_Decl := Next (Decl);
2714
2715 if No (Freeze_From) then
2716 Freeze_From := First_Entity (Current_Scope);
2717 end if;
2718
2719 -- Remember if the declaration we just processed is the full type
2720 -- declaration of a controlled type (to handle late overriding of
2721 -- initialize, adjust or finalize).
2722
2723 if Nkind (Decl) = N_Full_Type_Declaration
2724 and then Is_Controlled (Defining_Identifier (Decl))
2725 then
2726 Ctrl_Typ := Defining_Identifier (Decl);
2727 end if;
2728
2729 -- At the end of a declarative part, freeze remaining entities
2730 -- declared in it. The end of the visible declarations of package
2731 -- specification is not the end of a declarative part if private
2732 -- declarations are present. The end of a package declaration is a
2733 -- freezing point only if it a library package. A task definition or
2734 -- protected type definition is not a freeze point either. Finally,
2735 -- we do not freeze entities in generic scopes, because there is no
2736 -- code generated for them and freeze nodes will be generated for
2737 -- the instance.
2738
2739 -- The end of a package instantiation is not a freeze point, but
2740 -- for now we make it one, because the generic body is inserted
2741 -- (currently) immediately after. Generic instantiations will not
2742 -- be a freeze point once delayed freezing of bodies is implemented.
2743 -- (This is needed in any case for early instantiations ???).
2744
2745 if No (Next_Decl) then
2746 if Nkind (Parent (L)) = N_Component_List then
2747 null;
2748
2749 elsif Nkind (Parent (L)) in
2750 N_Protected_Definition | N_Task_Definition
2751 then
2752 Check_Entry_Contracts;
2753
2754 elsif Nkind (Parent (L)) /= N_Package_Specification then
2755 if Nkind (Parent (L)) = N_Package_Body then
2756 Freeze_From := First_Entity (Current_Scope);
2757 end if;
2758
2759 -- There may have been several freezing points previously,
2760 -- for example object declarations or subprogram bodies, but
2761 -- at the end of a declarative part we check freezing from
2762 -- the beginning, even though entities may already be frozen,
2763 -- in order to perform visibility checks on delayed aspects.
2764
2765 Adjust_Decl;
2766
2767 -- If the current scope is a generic subprogram body. Skip the
2768 -- generic formal parameters that are not frozen here.
2769
2770 if Is_Subprogram (Current_Scope)
2771 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2772 N_Generic_Subprogram_Declaration
2773 and then Present (First_Entity (Current_Scope))
2774 then
2775 while Is_Generic_Formal (Freeze_From) loop
2776 Next_Entity (Freeze_From);
2777 end loop;
2778
2779 Freeze_All (Freeze_From, Decl);
2780 Freeze_From := Last_Entity (Current_Scope);
2781
2782 else
2783 -- For declarations in a subprogram body there is no issue
2784 -- with name resolution in aspect specifications.
2785
2786 Freeze_All (First_Entity (Current_Scope), Decl);
2787 Freeze_From := Last_Entity (Current_Scope);
2788 end if;
2789
2790 -- Current scope is a package specification
2791
2792 elsif Scope (Current_Scope) /= Standard_Standard
2793 and then not Is_Child_Unit (Current_Scope)
2794 and then No (Generic_Parent (Parent (L)))
2795 then
2796 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2797 -- resolved at the end of the immediately enclosing declaration
2798 -- list (AI05-0183-1).
2799
2800 Resolve_Aspects;
2801
2802 elsif L /= Visible_Declarations (Parent (L))
2803 or else Is_Empty_List (Private_Declarations (Parent (L)))
2804 then
2805 Adjust_Decl;
2806
2807 -- End of a package declaration
2808
2809 -- This is a freeze point because it is the end of a
2810 -- compilation unit.
2811
2812 Freeze_All (First_Entity (Current_Scope), Decl);
2813 Freeze_From := Last_Entity (Current_Scope);
2814
2815 -- At the end of the visible declarations the expressions in
2816 -- aspects of all entities declared so far must be resolved.
2817 -- The entities themselves might be frozen later, and the
2818 -- generated pragmas and attribute definition clauses analyzed
2819 -- in full at that point, but name resolution must take place
2820 -- now.
2821 -- In addition to being the proper semantics, this is mandatory
2822 -- within generic units, because global name capture requires
2823 -- those expressions to be analyzed, given that the generated
2824 -- pragmas do not appear in the original generic tree.
2825
2826 elsif Serious_Errors_Detected = 0 then
2827 Resolve_Aspects;
2828 end if;
2829
2830 -- If next node is a body then freeze all types before the body.
2831 -- An exception occurs for some expander-generated bodies. If these
2832 -- are generated at places where in general language rules would not
2833 -- allow a freeze point, then we assume that the expander has
2834 -- explicitly checked that all required types are properly frozen,
2835 -- and we do not cause general freezing here. This special circuit
2836 -- is used when the encountered body is marked as having already
2837 -- been analyzed.
2838
2839 -- In all other cases (bodies that come from source, and expander
2840 -- generated bodies that have not been analyzed yet), freeze all
2841 -- types now. Note that in the latter case, the expander must take
2842 -- care to attach the bodies at a proper place in the tree so as to
2843 -- not cause unwanted freezing at that point.
2844
2845 -- It is also necessary to check for a case where both an expression
2846 -- function is used and the current scope depends on an incomplete
2847 -- private type from a library unit, otherwise premature freezing of
2848 -- the private type will occur.
2849
2850 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2851 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2852 or else not Was_Expression_Function (Next_Decl))
2853 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2854 and then not Contains_Lib_Incomplete_Type
2855 (Current_Scope)))
2856 then
2857 -- When a controlled type is frozen, the expander generates stream
2858 -- and controlled-type support routines. If the freeze is caused
2859 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2860 -- expander will end up using the wrong version of these routines,
2861 -- as the body has not been processed yet. To remedy this, detect
2862 -- a late controlled primitive and create a proper spec for it.
2863 -- This ensures that the primitive will override its inherited
2864 -- counterpart before the freeze takes place.
2865
2866 -- If the declaration we just processed is a body, do not attempt
2867 -- to examine Next_Decl as the late primitive idiom can only apply
2868 -- to the first encountered body.
2869
2870 -- ??? A cleaner approach may be possible and/or this solution
2871 -- could be extended to general-purpose late primitives.
2872
2873 if Present (Ctrl_Typ) then
2874
2875 -- No need to continue searching for late body overriding if
2876 -- the controlled type is already frozen.
2877
2878 if Is_Frozen (Ctrl_Typ) then
2879 Ctrl_Typ := Empty;
2880
2881 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2882 Handle_Late_Controlled_Primitive (Next_Decl);
2883 end if;
2884 end if;
2885
2886 Adjust_Decl;
2887
2888 -- The generated body of an expression function does not freeze,
2889 -- unless it is a completion, in which case only the expression
2890 -- itself freezes. This is handled when the body itself is
2891 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2892
2893 Freeze_All (Freeze_From, Decl);
2894 Freeze_From := Last_Entity (Current_Scope);
2895 end if;
2896
2897 Decl := Next_Decl;
2898 end loop;
2899
2900 -- Post-freezing actions
2901
2902 if Present (L) then
2903 Context := Parent (L);
2904
2905 -- Certain contract annotations have forward visibility semantics and
2906 -- must be analyzed after all declarative items have been processed.
2907 -- This timing ensures that entities referenced by such contracts are
2908 -- visible.
2909
2910 -- Analyze the contract of an immediately enclosing package spec or
2911 -- body first because other contracts may depend on its information.
2912
2913 if Nkind (Context) = N_Package_Body then
2914 Analyze_Package_Body_Contract (Defining_Entity (Context));
2915
2916 elsif Nkind (Context) = N_Package_Specification then
2917 Analyze_Package_Contract (Defining_Entity (Context));
2918 end if;
2919
2920 -- Analyze the contracts of various constructs in the declarative
2921 -- list.
2922
2923 Analyze_Contracts (L);
2924
2925 if Nkind (Context) = N_Package_Body then
2926
2927 -- Ensure that all abstract states and objects declared in the
2928 -- state space of a package body are utilized as constituents.
2929
2930 Check_Unused_Body_States (Defining_Entity (Context));
2931
2932 -- State refinements are visible up to the end of the package body
2933 -- declarations. Hide the state refinements from visibility to
2934 -- restore the original state conditions.
2935
2936 Remove_Visible_Refinements (Corresponding_Spec (Context));
2937 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2938
2939 elsif Nkind (Context) = N_Package_Specification then
2940
2941 -- Partial state refinements are visible up to the end of the
2942 -- package spec declarations. Hide the partial state refinements
2943 -- from visibility to restore the original state conditions.
2944
2945 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2946 end if;
2947
2948 -- Verify that all abstract states found in any package declared in
2949 -- the input declarative list have proper refinements. The check is
2950 -- performed only when the context denotes a block, entry, package,
2951 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2952
2953 Check_State_Refinements (Context);
2954
2955 -- Create the subprogram bodies which verify the run-time semantics
2956 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2957 -- types within the current declarative list. This ensures that all
2958 -- assertion expressions are preanalyzed and resolved at the end of
2959 -- the declarative part. Note that the resolution happens even when
2960 -- freezing does not take place.
2961
2962 Build_Assertion_Bodies (L, Context);
2963 end if;
2964 end Analyze_Declarations;
2965
2966 -----------------------------------
2967 -- Analyze_Full_Type_Declaration --
2968 -----------------------------------
2969
2970 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2971 Def : constant Node_Id := Type_Definition (N);
2972 Def_Id : constant Entity_Id := Defining_Identifier (N);
2973 T : Entity_Id;
2974 Prev : Entity_Id;
2975
2976 Is_Remote : constant Boolean :=
2977 (Is_Remote_Types (Current_Scope)
2978 or else Is_Remote_Call_Interface (Current_Scope))
2979 and then not (In_Private_Part (Current_Scope)
2980 or else In_Package_Body (Current_Scope));
2981
2982 procedure Check_Nonoverridable_Aspects;
2983 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2984 -- be overridden, and can only be confirmed on derivation.
2985
2986 procedure Check_Ops_From_Incomplete_Type;
2987 -- If there is a tagged incomplete partial view of the type, traverse
2988 -- the primitives of the incomplete view and change the type of any
2989 -- controlling formals and result to indicate the full view. The
2990 -- primitives will be added to the full type's primitive operations
2991 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2992 -- is called from Process_Incomplete_Dependents).
2993
2994 ----------------------------------
2995 -- Check_Nonoverridable_Aspects --
2996 ----------------------------------
2997
2998 procedure Check_Nonoverridable_Aspects is
2999 function Get_Aspect_Spec
3000 (Specs : List_Id;
3001 Aspect_Name : Name_Id) return Node_Id;
3002 -- Check whether a list of aspect specifications includes an entry
3003 -- for a specific aspect. The list is either that of a partial or
3004 -- a full view.
3005
3006 ---------------------
3007 -- Get_Aspect_Spec --
3008 ---------------------
3009
3010 function Get_Aspect_Spec
3011 (Specs : List_Id;
3012 Aspect_Name : Name_Id) return Node_Id
3013 is
3014 Spec : Node_Id;
3015
3016 begin
3017 Spec := First (Specs);
3018 while Present (Spec) loop
3019 if Chars (Identifier (Spec)) = Aspect_Name then
3020 return Spec;
3021 end if;
3022 Next (Spec);
3023 end loop;
3024
3025 return Empty;
3026 end Get_Aspect_Spec;
3027
3028 -- Local variables
3029
3030 Prev_Aspects : constant List_Id :=
3031 Aspect_Specifications (Parent (Def_Id));
3032 Par_Type : Entity_Id;
3033 Prev_Aspect : Node_Id;
3034
3035 -- Start of processing for Check_Nonoverridable_Aspects
3036
3037 begin
3038 -- Get parent type of derived type. Note that Prev is the entity in
3039 -- the partial declaration, but its contents are now those of full
3040 -- view, while Def_Id reflects the partial view.
3041
3042 if Is_Private_Type (Def_Id) then
3043 Par_Type := Etype (Full_View (Def_Id));
3044 else
3045 Par_Type := Etype (Def_Id);
3046 end if;
3047
3048 -- If there is an inherited Implicit_Dereference, verify that it is
3049 -- made explicit in the partial view.
3050
3051 if Has_Discriminants (Base_Type (Par_Type))
3052 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3053 and then Present (Discriminant_Specifications (Parent (Prev)))
3054 and then Present (Get_Reference_Discriminant (Par_Type))
3055 then
3056 Prev_Aspect :=
3057 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3058
3059 if No (Prev_Aspect)
3060 and then Present
3061 (Discriminant_Specifications
3062 (Original_Node (Parent (Prev))))
3063 then
3064 Error_Msg_N
3065 ("type does not inherit implicit dereference", Prev);
3066
3067 else
3068 -- If one of the views has the aspect specified, verify that it
3069 -- is consistent with that of the parent.
3070
3071 declare
3072 Cur_Discr : constant Entity_Id :=
3073 Get_Reference_Discriminant (Prev);
3074 Par_Discr : constant Entity_Id :=
3075 Get_Reference_Discriminant (Par_Type);
3076
3077 begin
3078 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3079 Error_Msg_N
3080 ("aspect inconsistent with that of parent", N);
3081 end if;
3082
3083 -- Check that specification in partial view matches the
3084 -- inherited aspect. Compare names directly because aspect
3085 -- expression may not be analyzed.
3086
3087 if Present (Prev_Aspect)
3088 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3089 and then Chars (Expression (Prev_Aspect)) /=
3090 Chars (Cur_Discr)
3091 then
3092 Error_Msg_N
3093 ("aspect inconsistent with that of parent", N);
3094 end if;
3095 end;
3096 end if;
3097 end if;
3098
3099 -- What about other nonoverridable aspects???
3100 end Check_Nonoverridable_Aspects;
3101
3102 ------------------------------------
3103 -- Check_Ops_From_Incomplete_Type --
3104 ------------------------------------
3105
3106 procedure Check_Ops_From_Incomplete_Type is
3107 Elmt : Elmt_Id;
3108 Formal : Entity_Id;
3109 Op : Entity_Id;
3110
3111 begin
3112 if Prev /= T
3113 and then Ekind (Prev) = E_Incomplete_Type
3114 and then Is_Tagged_Type (Prev)
3115 and then Is_Tagged_Type (T)
3116 and then Present (Primitive_Operations (Prev))
3117 then
3118 Elmt := First_Elmt (Primitive_Operations (Prev));
3119 while Present (Elmt) loop
3120 Op := Node (Elmt);
3121
3122 Formal := First_Formal (Op);
3123 while Present (Formal) loop
3124 if Etype (Formal) = Prev then
3125 Set_Etype (Formal, T);
3126 end if;
3127
3128 Next_Formal (Formal);
3129 end loop;
3130
3131 if Etype (Op) = Prev then
3132 Set_Etype (Op, T);
3133 end if;
3134
3135 Next_Elmt (Elmt);
3136 end loop;
3137 end if;
3138 end Check_Ops_From_Incomplete_Type;
3139
3140 -- Start of processing for Analyze_Full_Type_Declaration
3141
3142 begin
3143 Prev := Find_Type_Name (N);
3144
3145 -- The full view, if present, now points to the current type. If there
3146 -- is an incomplete partial view, set a link to it, to simplify the
3147 -- retrieval of primitive operations of the type.
3148
3149 -- Ada 2005 (AI-50217): If the type was previously decorated when
3150 -- imported through a LIMITED WITH clause, it appears as incomplete
3151 -- but has no full view.
3152
3153 if Ekind (Prev) = E_Incomplete_Type
3154 and then Present (Full_View (Prev))
3155 then
3156 T := Full_View (Prev);
3157 Set_Incomplete_View (N, Parent (Prev));
3158 else
3159 T := Prev;
3160 end if;
3161
3162 Set_Is_Pure (T, Is_Pure (Current_Scope));
3163
3164 -- We set the flag Is_First_Subtype here. It is needed to set the
3165 -- corresponding flag for the Implicit class-wide-type created
3166 -- during tagged types processing.
3167
3168 Set_Is_First_Subtype (T, True);
3169
3170 -- Only composite types other than array types are allowed to have
3171 -- discriminants.
3172
3173 case Nkind (Def) is
3174
3175 -- For derived types, the rule will be checked once we've figured
3176 -- out the parent type.
3177
3178 when N_Derived_Type_Definition =>
3179 null;
3180
3181 -- For record types, discriminants are allowed.
3182
3183 when N_Record_Definition =>
3184 null;
3185
3186 when others =>
3187 if Present (Discriminant_Specifications (N)) then
3188 Error_Msg_N
3189 ("elementary or array type cannot have discriminants",
3190 Defining_Identifier
3191 (First (Discriminant_Specifications (N))));
3192 end if;
3193 end case;
3194
3195 -- Elaborate the type definition according to kind, and generate
3196 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3197 -- already done (this happens during the reanalysis that follows a call
3198 -- to the high level optimizer).
3199
3200 if not Analyzed (T) then
3201 Set_Analyzed (T);
3202
3203 -- Set the SPARK mode from the current context
3204
3205 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3206 Set_SPARK_Pragma_Inherited (T);
3207
3208 case Nkind (Def) is
3209 when N_Access_To_Subprogram_Definition =>
3210 Access_Subprogram_Declaration (T, Def);
3211
3212 -- If this is a remote access to subprogram, we must create the
3213 -- equivalent fat pointer type, and related subprograms.
3214
3215 if Is_Remote then
3216 Process_Remote_AST_Declaration (N);
3217 end if;
3218
3219 -- Validate categorization rule against access type declaration
3220 -- usually a violation in Pure unit, Shared_Passive unit.
3221
3222 Validate_Access_Type_Declaration (T, N);
3223
3224 -- If the type has contracts, we create the corresponding
3225 -- wrapper at once, before analyzing the aspect specifications,
3226 -- so that pre/postconditions can be handled directly on the
3227 -- generated wrapper.
3228
3229 if Ada_Version >= Ada_2022
3230 and then Present (Aspect_Specifications (N))
3231 then
3232 Build_Access_Subprogram_Wrapper (N);
3233 end if;
3234
3235 when N_Access_To_Object_Definition =>
3236 Access_Type_Declaration (T, Def);
3237
3238 -- Validate categorization rule against access type declaration
3239 -- usually a violation in Pure unit, Shared_Passive unit.
3240
3241 Validate_Access_Type_Declaration (T, N);
3242
3243 -- If we are in a Remote_Call_Interface package and define a
3244 -- RACW, then calling stubs and specific stream attributes
3245 -- must be added.
3246
3247 if Is_Remote
3248 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3249 then
3250 Add_RACW_Features (Def_Id);
3251 end if;
3252
3253 when N_Array_Type_Definition =>
3254 Array_Type_Declaration (T, Def);
3255
3256 when N_Derived_Type_Definition =>
3257 Derived_Type_Declaration (T, N, T /= Def_Id);
3258
3259 -- Inherit predicates from parent, and protect against illegal
3260 -- derivations.
3261
3262 if Is_Type (T) and then Has_Predicates (T) then
3263 Set_Has_Predicates (Def_Id);
3264 end if;
3265
3266 -- Save the scenario for examination by the ABE Processing
3267 -- phase.
3268
3269 Record_Elaboration_Scenario (N);
3270
3271 when N_Enumeration_Type_Definition =>
3272 Enumeration_Type_Declaration (T, Def);
3273
3274 when N_Floating_Point_Definition =>
3275 Floating_Point_Type_Declaration (T, Def);
3276
3277 when N_Decimal_Fixed_Point_Definition =>
3278 Decimal_Fixed_Point_Type_Declaration (T, Def);
3279
3280 when N_Ordinary_Fixed_Point_Definition =>
3281 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3282
3283 when N_Signed_Integer_Type_Definition =>
3284 Signed_Integer_Type_Declaration (T, Def);
3285
3286 when N_Modular_Type_Definition =>
3287 Modular_Type_Declaration (T, Def);
3288
3289 when N_Record_Definition =>
3290 Record_Type_Declaration (T, N, Prev);
3291
3292 -- If declaration has a parse error, nothing to elaborate.
3293
3294 when N_Error =>
3295 null;
3296
3297 when others =>
3298 raise Program_Error;
3299 end case;
3300 end if;
3301
3302 if Etype (T) = Any_Type then
3303 return;
3304 end if;
3305
3306 -- Set the primitives list of the full type and its base type when
3307 -- needed. T may be E_Void in cases of earlier errors, and in that
3308 -- case we bypass this.
3309
3310 if Ekind (T) /= E_Void
3311 and then not Present (Direct_Primitive_Operations (T))
3312 then
3313 if Etype (T) = T then
3314 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3315
3316 -- If Etype of T is the base type (as opposed to a parent type) and
3317 -- already has an associated list of primitive operations, then set
3318 -- T's primitive list to the base type's list. Otherwise, create a
3319 -- new empty primitives list and share the list between T and its
3320 -- base type. The lists need to be shared in common between the two.
3321
3322 elsif Etype (T) = Base_Type (T) then
3323
3324 if not Present (Direct_Primitive_Operations (Base_Type (T))) then
3325 Set_Direct_Primitive_Operations
3326 (Base_Type (T), New_Elmt_List);
3327 end if;
3328
3329 Set_Direct_Primitive_Operations
3330 (T, Direct_Primitive_Operations (Base_Type (T)));
3331
3332 -- Case where the Etype is a parent type, so we need a new primitives
3333 -- list for T.
3334
3335 else
3336 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3337 end if;
3338 end if;
3339
3340 -- Some common processing for all types
3341
3342 Set_Depends_On_Private (T, Has_Private_Component (T));
3343 Check_Ops_From_Incomplete_Type;
3344
3345 -- Both the declared entity, and its anonymous base type if one was
3346 -- created, need freeze nodes allocated.
3347
3348 declare
3349 B : constant Entity_Id := Base_Type (T);
3350
3351 begin
3352 -- In the case where the base type differs from the first subtype, we
3353 -- pre-allocate a freeze node, and set the proper link to the first
3354 -- subtype. Freeze_Entity will use this preallocated freeze node when
3355 -- it freezes the entity.
3356
3357 -- This does not apply if the base type is a generic type, whose
3358 -- declaration is independent of the current derived definition.
3359
3360 if B /= T and then not Is_Generic_Type (B) then
3361 Ensure_Freeze_Node (B);
3362 Set_First_Subtype_Link (Freeze_Node (B), T);
3363 end if;
3364
3365 -- A type that is imported through a limited_with clause cannot
3366 -- generate any code, and thus need not be frozen. However, an access
3367 -- type with an imported designated type needs a finalization list,
3368 -- which may be referenced in some other package that has non-limited
3369 -- visibility on the designated type. Thus we must create the
3370 -- finalization list at the point the access type is frozen, to
3371 -- prevent unsatisfied references at link time.
3372
3373 if not From_Limited_With (T) or else Is_Access_Type (T) then
3374 Set_Has_Delayed_Freeze (T);
3375 end if;
3376 end;
3377
3378 -- Case where T is the full declaration of some private type which has
3379 -- been swapped in Defining_Identifier (N).
3380
3381 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3382 Process_Full_View (N, T, Def_Id);
3383
3384 -- Record the reference. The form of this is a little strange, since
3385 -- the full declaration has been swapped in. So the first parameter
3386 -- here represents the entity to which a reference is made which is
3387 -- the "real" entity, i.e. the one swapped in, and the second
3388 -- parameter provides the reference location.
3389
3390 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3391 -- since we don't want a complaint about the full type being an
3392 -- unwanted reference to the private type
3393
3394 declare
3395 B : constant Boolean := Has_Pragma_Unreferenced (T);
3396 begin
3397 Set_Has_Pragma_Unreferenced (T, False);
3398 Generate_Reference (T, T, 'c');
3399 Set_Has_Pragma_Unreferenced (T, B);
3400 end;
3401
3402 Set_Completion_Referenced (Def_Id);
3403
3404 -- For completion of incomplete type, process incomplete dependents
3405 -- and always mark the full type as referenced (it is the incomplete
3406 -- type that we get for any real reference).
3407
3408 elsif Ekind (Prev) = E_Incomplete_Type then
3409 Process_Incomplete_Dependents (N, T, Prev);
3410 Generate_Reference (Prev, Def_Id, 'c');
3411 Set_Completion_Referenced (Def_Id);
3412
3413 -- If not private type or incomplete type completion, this is a real
3414 -- definition of a new entity, so record it.
3415
3416 else
3417 Generate_Definition (Def_Id);
3418 end if;
3419
3420 -- Propagate any pending access types whose finalization masters need to
3421 -- be fully initialized from the partial to the full view. Guard against
3422 -- an illegal full view that remains unanalyzed.
3423
3424 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3425 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3426 end if;
3427
3428 if Chars (Scope (Def_Id)) = Name_System
3429 and then Chars (Def_Id) = Name_Address
3430 and then In_Predefined_Unit (N)
3431 then
3432 Set_Is_Descendant_Of_Address (Def_Id);
3433 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3434 Set_Is_Descendant_Of_Address (Prev);
3435 end if;
3436
3437 Set_Optimize_Alignment_Flags (Def_Id);
3438 Check_Eliminated (Def_Id);
3439
3440 -- If the declaration is a completion and aspects are present, apply
3441 -- them to the entity for the type which is currently the partial
3442 -- view, but which is the one that will be frozen.
3443
3444 if Has_Aspects (N) then
3445
3446 -- In most cases the partial view is a private type, and both views
3447 -- appear in different declarative parts. In the unusual case where
3448 -- the partial view is incomplete, perform the analysis on the
3449 -- full view, to prevent freezing anomalies with the corresponding
3450 -- class-wide type, which otherwise might be frozen before the
3451 -- dispatch table is built.
3452
3453 if Prev /= Def_Id
3454 and then Ekind (Prev) /= E_Incomplete_Type
3455 then
3456 Analyze_Aspect_Specifications (N, Prev);
3457
3458 -- Normal case
3459
3460 else
3461 Analyze_Aspect_Specifications (N, Def_Id);
3462 end if;
3463 end if;
3464
3465 if Is_Derived_Type (Prev)
3466 and then Def_Id /= Prev
3467 then
3468 Check_Nonoverridable_Aspects;
3469 end if;
3470 end Analyze_Full_Type_Declaration;
3471
3472 ----------------------------------
3473 -- Analyze_Incomplete_Type_Decl --
3474 ----------------------------------
3475
3476 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3477 F : constant Boolean := Is_Pure (Current_Scope);
3478 T : Entity_Id;
3479
3480 begin
3481 Generate_Definition (Defining_Identifier (N));
3482
3483 -- Process an incomplete declaration. The identifier must not have been
3484 -- declared already in the scope. However, an incomplete declaration may
3485 -- appear in the private part of a package, for a private type that has
3486 -- already been declared.
3487
3488 -- In this case, the discriminants (if any) must match
3489
3490 T := Find_Type_Name (N);
3491
3492 Mutate_Ekind (T, E_Incomplete_Type);
3493 Set_Etype (T, T);
3494 Set_Is_First_Subtype (T);
3495 Reinit_Size_Align (T);
3496
3497 -- Set the SPARK mode from the current context
3498
3499 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3500 Set_SPARK_Pragma_Inherited (T);
3501
3502 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3503 -- incomplete types.
3504
3505 if Tagged_Present (N) then
3506 Set_Is_Tagged_Type (T, True);
3507 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3508 Make_Class_Wide_Type (T);
3509 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3510 end if;
3511
3512 Set_Stored_Constraint (T, No_Elist);
3513
3514 if Present (Discriminant_Specifications (N)) then
3515 Push_Scope (T);
3516 Process_Discriminants (N);
3517 End_Scope;
3518 end if;
3519
3520 -- If the type has discriminants, nontrivial subtypes may be declared
3521 -- before the full view of the type. The full views of those subtypes
3522 -- will be built after the full view of the type.
3523
3524 Set_Private_Dependents (T, New_Elmt_List);
3525 Set_Is_Pure (T, F);
3526 end Analyze_Incomplete_Type_Decl;
3527
3528 -----------------------------------
3529 -- Analyze_Interface_Declaration --
3530 -----------------------------------
3531
3532 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3533 CW : constant Entity_Id := Class_Wide_Type (T);
3534
3535 begin
3536 Set_Is_Tagged_Type (T);
3537 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3538
3539 Set_Is_Limited_Record (T, Limited_Present (Def)
3540 or else Task_Present (Def)
3541 or else Protected_Present (Def)
3542 or else Synchronized_Present (Def));
3543
3544 -- Type is abstract if full declaration carries keyword, or if previous
3545 -- partial view did.
3546
3547 Set_Is_Abstract_Type (T);
3548 Set_Is_Interface (T);
3549
3550 -- Type is a limited interface if it includes the keyword limited, task,
3551 -- protected, or synchronized.
3552
3553 Set_Is_Limited_Interface
3554 (T, Limited_Present (Def)
3555 or else Protected_Present (Def)
3556 or else Synchronized_Present (Def)
3557 or else Task_Present (Def));
3558
3559 Set_Interfaces (T, New_Elmt_List);
3560 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3561
3562 -- Complete the decoration of the class-wide entity if it was already
3563 -- built (i.e. during the creation of the limited view)
3564
3565 if Present (CW) then
3566 Set_Is_Interface (CW);
3567 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3568 end if;
3569
3570 -- Check runtime support for synchronized interfaces
3571
3572 if Is_Concurrent_Interface (T)
3573 and then not RTE_Available (RE_Select_Specific_Data)
3574 then
3575 Error_Msg_CRT ("synchronized interfaces", T);
3576 end if;
3577 end Analyze_Interface_Declaration;
3578
3579 -----------------------------
3580 -- Analyze_Itype_Reference --
3581 -----------------------------
3582
3583 -- Nothing to do. This node is placed in the tree only for the benefit of
3584 -- back end processing, and has no effect on the semantic processing.
3585
3586 procedure Analyze_Itype_Reference (N : Node_Id) is
3587 begin
3588 pragma Assert (Is_Itype (Itype (N)));
3589 null;
3590 end Analyze_Itype_Reference;
3591
3592 --------------------------------
3593 -- Analyze_Number_Declaration --
3594 --------------------------------
3595
3596 procedure Analyze_Number_Declaration (N : Node_Id) is
3597 E : constant Node_Id := Expression (N);
3598 Id : constant Entity_Id := Defining_Identifier (N);
3599 Index : Interp_Index;
3600 It : Interp;
3601 T : Entity_Id;
3602
3603 begin
3604 Generate_Definition (Id);
3605 Enter_Name (Id);
3606
3607 -- This is an optimization of a common case of an integer literal
3608
3609 if Nkind (E) = N_Integer_Literal then
3610 Set_Is_Static_Expression (E, True);
3611 Set_Etype (E, Universal_Integer);
3612
3613 Set_Etype (Id, Universal_Integer);
3614 Mutate_Ekind (Id, E_Named_Integer);
3615 Set_Is_Frozen (Id, True);
3616
3617 Set_Debug_Info_Needed (Id);
3618 return;
3619 end if;
3620
3621 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3622
3623 -- Process expression, replacing error by integer zero, to avoid
3624 -- cascaded errors or aborts further along in the processing
3625
3626 -- Replace Error by integer zero, which seems least likely to cause
3627 -- cascaded errors.
3628
3629 if E = Error then
3630 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3631 Set_Error_Posted (E);
3632 end if;
3633
3634 Analyze (E);
3635
3636 -- Verify that the expression is static and numeric. If
3637 -- the expression is overloaded, we apply the preference
3638 -- rule that favors root numeric types.
3639
3640 if not Is_Overloaded (E) then
3641 T := Etype (E);
3642 if Has_Dynamic_Predicate_Aspect (T) then
3643 Error_Msg_N
3644 ("subtype has dynamic predicate, "
3645 & "not allowed in number declaration", N);
3646 end if;
3647
3648 else
3649 T := Any_Type;
3650
3651 Get_First_Interp (E, Index, It);
3652 while Present (It.Typ) loop
3653 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3654 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3655 then
3656 if T = Any_Type then
3657 T := It.Typ;
3658
3659 elsif Is_Universal_Numeric_Type (It.Typ) then
3660 -- Choose universal interpretation over any other
3661
3662 T := It.Typ;
3663 exit;
3664 end if;
3665 end if;
3666
3667 Get_Next_Interp (Index, It);
3668 end loop;
3669 end if;
3670
3671 if Is_Integer_Type (T) then
3672 Resolve (E, T);
3673 Set_Etype (Id, Universal_Integer);
3674 Mutate_Ekind (Id, E_Named_Integer);
3675
3676 elsif Is_Real_Type (T) then
3677
3678 -- Because the real value is converted to universal_real, this is a
3679 -- legal context for a universal fixed expression.
3680
3681 if T = Universal_Fixed then
3682 declare
3683 Loc : constant Source_Ptr := Sloc (N);
3684 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3685 Subtype_Mark =>
3686 New_Occurrence_Of (Universal_Real, Loc),
3687 Expression => Relocate_Node (E));
3688
3689 begin
3690 Rewrite (E, Conv);
3691 Analyze (E);
3692 end;
3693
3694 elsif T = Any_Fixed then
3695 Error_Msg_N ("illegal context for mixed mode operation", E);
3696
3697 -- Expression is of the form : universal_fixed * integer. Try to
3698 -- resolve as universal_real.
3699
3700 T := Universal_Real;
3701 Set_Etype (E, T);
3702 end if;
3703
3704 Resolve (E, T);
3705 Set_Etype (Id, Universal_Real);
3706 Mutate_Ekind (Id, E_Named_Real);
3707
3708 else
3709 Wrong_Type (E, Any_Numeric);
3710 Resolve (E, T);
3711
3712 Set_Etype (Id, T);
3713 Mutate_Ekind (Id, E_Constant);
3714 Set_Never_Set_In_Source (Id, True);
3715 Set_Is_True_Constant (Id, True);
3716 return;
3717 end if;
3718
3719 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3720 Set_Etype (E, Etype (Id));
3721 end if;
3722
3723 if not Is_OK_Static_Expression (E) then
3724 Flag_Non_Static_Expr
3725 ("non-static expression used in number declaration!", E);
3726 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3727 Set_Etype (E, Any_Type);
3728 end if;
3729
3730 Analyze_Dimension (N);
3731 end Analyze_Number_Declaration;
3732
3733 --------------------------------
3734 -- Analyze_Object_Declaration --
3735 --------------------------------
3736
3737 -- WARNING: This routine manages Ghost regions. Return statements must be
3738 -- replaced by gotos which jump to the end of the routine and restore the
3739 -- Ghost mode.
3740
3741 procedure Analyze_Object_Declaration (N : Node_Id) is
3742 Loc : constant Source_Ptr := Sloc (N);
3743 Id : constant Entity_Id := Defining_Identifier (N);
3744 Next_Decl : constant Node_Id := Next (N);
3745
3746 Act_T : Entity_Id;
3747 T : Entity_Id;
3748
3749 E : Node_Id := Expression (N);
3750 -- E is set to Expression (N) throughout this routine. When Expression
3751 -- (N) is modified, E is changed accordingly.
3752
3753 procedure Check_Dynamic_Object (Typ : Entity_Id);
3754 -- A library-level object with nonstatic discriminant constraints may
3755 -- require dynamic allocation. The declaration is illegal if the
3756 -- profile includes the restriction No_Implicit_Heap_Allocations.
3757
3758 procedure Check_For_Null_Excluding_Components
3759 (Obj_Typ : Entity_Id;
3760 Obj_Decl : Node_Id);
3761 -- Verify that each null-excluding component of object declaration
3762 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3763 -- a compile-time warning if this is not the case.
3764
3765 function Count_Tasks (T : Entity_Id) return Uint;
3766 -- This function is called when a non-generic library level object of a
3767 -- task type is declared. Its function is to count the static number of
3768 -- tasks declared within the type (it is only called if Has_Task is set
3769 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3770 -- or a variant record type is encountered, Check_Restriction is called
3771 -- indicating the count is unknown.
3772
3773 function Delayed_Aspect_Present return Boolean;
3774 -- If the declaration has an expression that is an aggregate, and it
3775 -- has aspects that require delayed analysis, the resolution of the
3776 -- aggregate must be deferred to the freeze point of the object. This
3777 -- special processing was created for address clauses, but it must
3778 -- also apply to address aspects. This must be done before the aspect
3779 -- specifications are analyzed because we must handle the aggregate
3780 -- before the analysis of the object declaration is complete.
3781
3782 -- Any other relevant delayed aspects on object declarations ???
3783
3784 --------------------------
3785 -- Check_Dynamic_Object --
3786 --------------------------
3787
3788 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3789 Comp : Entity_Id;
3790 Obj_Type : Entity_Id;
3791
3792 begin
3793 Obj_Type := Typ;
3794
3795 if Is_Private_Type (Obj_Type)
3796 and then Present (Full_View (Obj_Type))
3797 then
3798 Obj_Type := Full_View (Obj_Type);
3799 end if;
3800
3801 if Known_Static_Esize (Obj_Type) then
3802 return;
3803 end if;
3804
3805 if Restriction_Active (No_Implicit_Heap_Allocations)
3806 and then Expander_Active
3807 and then Has_Discriminants (Obj_Type)
3808 then
3809 Comp := First_Component (Obj_Type);
3810 while Present (Comp) loop
3811 if Known_Static_Esize (Etype (Comp))
3812 or else Size_Known_At_Compile_Time (Etype (Comp))
3813 then
3814 null;
3815
3816 elsif not Discriminated_Size (Comp)
3817 and then Comes_From_Source (Comp)
3818 then
3819 Error_Msg_NE
3820 ("component& of non-static size will violate restriction "
3821 & "No_Implicit_Heap_Allocation?", N, Comp);
3822
3823 elsif Is_Record_Type (Etype (Comp)) then
3824 Check_Dynamic_Object (Etype (Comp));
3825 end if;
3826
3827 Next_Component (Comp);
3828 end loop;
3829 end if;
3830 end Check_Dynamic_Object;
3831
3832 -----------------------------------------
3833 -- Check_For_Null_Excluding_Components --
3834 -----------------------------------------
3835
3836 procedure Check_For_Null_Excluding_Components
3837 (Obj_Typ : Entity_Id;
3838 Obj_Decl : Node_Id)
3839 is
3840 procedure Check_Component
3841 (Comp_Typ : Entity_Id;
3842 Comp_Decl : Node_Id := Empty;
3843 Array_Comp : Boolean := False);
3844 -- Apply a compile-time null-exclusion check on a component denoted
3845 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3846 -- subcomponents (if any).
3847
3848 ---------------------
3849 -- Check_Component --
3850 ---------------------
3851
3852 procedure Check_Component
3853 (Comp_Typ : Entity_Id;
3854 Comp_Decl : Node_Id := Empty;
3855 Array_Comp : Boolean := False)
3856 is
3857 Comp : Entity_Id;
3858 T : Entity_Id;
3859
3860 begin
3861 -- Do not consider internally-generated components or those that
3862 -- are already initialized.
3863
3864 if Present (Comp_Decl)
3865 and then (not Comes_From_Source (Comp_Decl)
3866 or else Present (Expression (Comp_Decl)))
3867 then
3868 return;
3869 end if;
3870
3871 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3872 and then Present (Full_View (Comp_Typ))
3873 then
3874 T := Full_View (Comp_Typ);
3875 else
3876 T := Comp_Typ;
3877 end if;
3878
3879 -- Verify a component of a null-excluding access type
3880
3881 if Is_Access_Type (T)
3882 and then Can_Never_Be_Null (T)
3883 then
3884 if Comp_Decl = Obj_Decl then
3885 Null_Exclusion_Static_Checks
3886 (N => Obj_Decl,
3887 Comp => Empty,
3888 Array_Comp => Array_Comp);
3889
3890 else
3891 Null_Exclusion_Static_Checks
3892 (N => Obj_Decl,
3893 Comp => Comp_Decl,
3894 Array_Comp => Array_Comp);
3895 end if;
3896
3897 -- Check array components
3898
3899 elsif Is_Array_Type (T) then
3900
3901 -- There is no suitable component when the object is of an
3902 -- array type. However, a namable component may appear at some
3903 -- point during the recursive inspection, but not at the top
3904 -- level. At the top level just indicate array component case.
3905
3906 if Comp_Decl = Obj_Decl then
3907 Check_Component (Component_Type (T), Array_Comp => True);
3908 else
3909 Check_Component (Component_Type (T), Comp_Decl);
3910 end if;
3911
3912 -- Verify all components of type T
3913
3914 -- Note: No checks are performed on types with discriminants due
3915 -- to complexities involving variants. ???
3916
3917 elsif (Is_Concurrent_Type (T)
3918 or else Is_Incomplete_Or_Private_Type (T)
3919 or else Is_Record_Type (T))
3920 and then not Has_Discriminants (T)
3921 then
3922 Comp := First_Component (T);
3923 while Present (Comp) loop
3924 Check_Component (Etype (Comp), Parent (Comp));
3925
3926 Next_Component (Comp);
3927 end loop;
3928 end if;
3929 end Check_Component;
3930
3931 -- Start processing for Check_For_Null_Excluding_Components
3932
3933 begin
3934 Check_Component (Obj_Typ, Obj_Decl);
3935 end Check_For_Null_Excluding_Components;
3936
3937 -----------------
3938 -- Count_Tasks --
3939 -----------------
3940
3941 function Count_Tasks (T : Entity_Id) return Uint is
3942 C : Entity_Id;
3943 X : Node_Id;
3944 V : Uint;
3945
3946 begin
3947 if Is_Task_Type (T) then
3948 return Uint_1;
3949
3950 elsif Is_Record_Type (T) then
3951 if Has_Discriminants (T) then
3952 Check_Restriction (Max_Tasks, N);
3953 return Uint_0;
3954
3955 else
3956 V := Uint_0;
3957 C := First_Component (T);
3958 while Present (C) loop
3959 V := V + Count_Tasks (Etype (C));
3960 Next_Component (C);
3961 end loop;
3962
3963 return V;
3964 end if;
3965
3966 elsif Is_Array_Type (T) then
3967 X := First_Index (T);
3968 V := Count_Tasks (Component_Type (T));
3969 while Present (X) loop
3970 C := Etype (X);
3971
3972 if not Is_OK_Static_Subtype (C) then
3973 Check_Restriction (Max_Tasks, N);
3974 return Uint_0;
3975 else
3976 V := V * (UI_Max (Uint_0,
3977 Expr_Value (Type_High_Bound (C)) -
3978 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3979 end if;
3980
3981 Next_Index (X);
3982 end loop;
3983
3984 return V;
3985
3986 else
3987 return Uint_0;
3988 end if;
3989 end Count_Tasks;
3990
3991 ----------------------------
3992 -- Delayed_Aspect_Present --
3993 ----------------------------
3994
3995 function Delayed_Aspect_Present return Boolean is
3996 A : Node_Id;
3997 A_Id : Aspect_Id;
3998
3999 begin
4000 if Present (Aspect_Specifications (N)) then
4001 A := First (Aspect_Specifications (N));
4002
4003 while Present (A) loop
4004 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4005
4006 if A_Id = Aspect_Address then
4007
4008 -- Set flag on object entity, for later processing at
4009 -- the freeze point.
4010
4011 Set_Has_Delayed_Aspects (Id);
4012 return True;
4013 end if;
4014
4015 Next (A);
4016 end loop;
4017 end if;
4018
4019 return False;
4020 end Delayed_Aspect_Present;
4021
4022 -- Local variables
4023
4024 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4025 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4026 -- Save the Ghost-related attributes to restore on exit
4027
4028 Prev_Entity : Entity_Id := Empty;
4029 Related_Id : Entity_Id;
4030 Full_View_Present : Boolean := False;
4031
4032 -- Start of processing for Analyze_Object_Declaration
4033
4034 begin
4035 -- There are three kinds of implicit types generated by an
4036 -- object declaration:
4037
4038 -- 1. Those generated by the original Object Definition
4039
4040 -- 2. Those generated by the Expression
4041
4042 -- 3. Those used to constrain the Object Definition with the
4043 -- expression constraints when the definition is unconstrained.
4044
4045 -- They must be generated in this order to avoid order of elaboration
4046 -- issues. Thus the first step (after entering the name) is to analyze
4047 -- the object definition.
4048
4049 if Constant_Present (N) then
4050 Prev_Entity := Current_Entity_In_Scope (Id);
4051
4052 if Present (Prev_Entity)
4053 and then
4054 -- If the homograph is an implicit subprogram, it is overridden
4055 -- by the current declaration.
4056
4057 ((Is_Overloadable (Prev_Entity)
4058 and then Is_Inherited_Operation (Prev_Entity))
4059
4060 -- The current object is a discriminal generated for an entry
4061 -- family index. Even though the index is a constant, in this
4062 -- particular context there is no true constant redeclaration.
4063 -- Enter_Name will handle the visibility.
4064
4065 or else
4066 (Is_Discriminal (Id)
4067 and then Ekind (Discriminal_Link (Id)) =
4068 E_Entry_Index_Parameter)
4069
4070 -- The current object is the renaming for a generic declared
4071 -- within the instance.
4072
4073 or else
4074 (Ekind (Prev_Entity) = E_Package
4075 and then Nkind (Parent (Prev_Entity)) =
4076 N_Package_Renaming_Declaration
4077 and then not Comes_From_Source (Prev_Entity)
4078 and then
4079 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4080
4081 -- The entity may be a homonym of a private component of the
4082 -- enclosing protected object, for which we create a local
4083 -- renaming declaration. The declaration is legal, even if
4084 -- useless when it just captures that component.
4085
4086 or else
4087 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4088 and then Nkind (Parent (Prev_Entity)) =
4089 N_Object_Renaming_Declaration))
4090 then
4091 Prev_Entity := Empty;
4092 end if;
4093 end if;
4094
4095 if Present (Prev_Entity) then
4096
4097 -- The object declaration is Ghost when it completes a deferred Ghost
4098 -- constant.
4099
4100 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4101
4102 Constant_Redeclaration (Id, N, T);
4103
4104 Generate_Reference (Prev_Entity, Id, 'c');
4105 Set_Completion_Referenced (Id);
4106
4107 if Error_Posted (N) then
4108
4109 -- Type mismatch or illegal redeclaration; do not analyze
4110 -- expression to avoid cascaded errors.
4111
4112 T := Find_Type_Of_Object (Object_Definition (N), N);
4113 Set_Etype (Id, T);
4114 Mutate_Ekind (Id, E_Variable);
4115 goto Leave;
4116 end if;
4117
4118 -- In the normal case, enter identifier at the start to catch premature
4119 -- usage in the initialization expression.
4120
4121 else
4122 Generate_Definition (Id);
4123 Enter_Name (Id);
4124
4125 Mark_Coextensions (N, Object_Definition (N));
4126
4127 T := Find_Type_Of_Object (Object_Definition (N), N);
4128
4129 if Nkind (Object_Definition (N)) = N_Access_Definition
4130 and then Present
4131 (Access_To_Subprogram_Definition (Object_Definition (N)))
4132 and then Protected_Present
4133 (Access_To_Subprogram_Definition (Object_Definition (N)))
4134 then
4135 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4136 end if;
4137
4138 if Error_Posted (Id) then
4139 Set_Etype (Id, T);
4140 Mutate_Ekind (Id, E_Variable);
4141 goto Leave;
4142 end if;
4143 end if;
4144
4145 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4146 -- out some static checks.
4147
4148 if Ada_Version >= Ada_2005 then
4149
4150 -- In case of aggregates we must also take care of the correct
4151 -- initialization of nested aggregates bug this is done at the
4152 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4153
4154 if Can_Never_Be_Null (T) then
4155 if Present (Expression (N))
4156 and then Nkind (Expression (N)) = N_Aggregate
4157 then
4158 null;
4159
4160 elsif Comes_From_Source (Id) then
4161 declare
4162 Save_Typ : constant Entity_Id := Etype (Id);
4163 begin
4164 Set_Etype (Id, T); -- Temp. decoration for static checks
4165 Null_Exclusion_Static_Checks (N);
4166 Set_Etype (Id, Save_Typ);
4167 end;
4168 end if;
4169
4170 -- We might be dealing with an object of a composite type containing
4171 -- null-excluding components without an aggregate, so we must verify
4172 -- that such components have default initialization.
4173
4174 else
4175 Check_For_Null_Excluding_Components (T, N);
4176 end if;
4177 end if;
4178
4179 -- Object is marked pure if it is in a pure scope
4180
4181 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4182
4183 -- If deferred constant, make sure context is appropriate. We detect
4184 -- a deferred constant as a constant declaration with no expression.
4185 -- A deferred constant can appear in a package body if its completion
4186 -- is by means of an interface pragma.
4187
4188 if Constant_Present (N) and then No (E) then
4189
4190 -- A deferred constant may appear in the declarative part of the
4191 -- following constructs:
4192
4193 -- blocks
4194 -- entry bodies
4195 -- extended return statements
4196 -- package specs
4197 -- package bodies
4198 -- subprogram bodies
4199 -- task bodies
4200
4201 -- When declared inside a package spec, a deferred constant must be
4202 -- completed by a full constant declaration or pragma Import. In all
4203 -- other cases, the only proper completion is pragma Import. Extended
4204 -- return statements are flagged as invalid contexts because they do
4205 -- not have a declarative part and so cannot accommodate the pragma.
4206
4207 if Ekind (Current_Scope) = E_Return_Statement then
4208 Error_Msg_N
4209 ("invalid context for deferred constant declaration (RM 7.4)",
4210 N);
4211 Error_Msg_N
4212 ("\declaration requires an initialization expression",
4213 N);
4214 Set_Constant_Present (N, False);
4215
4216 -- In Ada 83, deferred constant must be of private type
4217
4218 elsif not Is_Private_Type (T) then
4219 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4220 Error_Msg_N
4221 ("(Ada 83) deferred constant must be private type", N);
4222 end if;
4223 end if;
4224
4225 -- If not a deferred constant, then the object declaration freezes
4226 -- its type, unless the object is of an anonymous type and has delayed
4227 -- aspects. In that case the type is frozen when the object itself is.
4228
4229 else
4230 Check_Fully_Declared (T, N);
4231
4232 if Has_Delayed_Aspects (Id)
4233 and then Is_Array_Type (T)
4234 and then Is_Itype (T)
4235 then
4236 Set_Has_Delayed_Freeze (T);
4237 else
4238 Freeze_Before (N, T);
4239 end if;
4240 end if;
4241
4242 -- If the object was created by a constrained array definition, then
4243 -- set the link in both the anonymous base type and anonymous subtype
4244 -- that are built to represent the array type to point to the object.
4245
4246 if Nkind (Object_Definition (Declaration_Node (Id))) =
4247 N_Constrained_Array_Definition
4248 then
4249 Set_Related_Array_Object (T, Id);
4250 Set_Related_Array_Object (Base_Type (T), Id);
4251 end if;
4252
4253 -- Check for protected objects not at library level
4254
4255 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4256 Check_Restriction (No_Local_Protected_Objects, Id);
4257 end if;
4258
4259 -- Check for violation of No_Local_Timing_Events
4260
4261 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4262 Check_Restriction (No_Local_Timing_Events, Id);
4263 end if;
4264
4265 -- The actual subtype of the object is the nominal subtype, unless
4266 -- the nominal one is unconstrained and obtained from the expression.
4267
4268 Act_T := T;
4269
4270 if Is_Library_Level_Entity (Id) then
4271 Check_Dynamic_Object (T);
4272 end if;
4273
4274 -- Process initialization expression if present and not in error
4275
4276 if Present (E) and then E /= Error then
4277
4278 -- Generate an error in case of CPP class-wide object initialization.
4279 -- Required because otherwise the expansion of the class-wide
4280 -- assignment would try to use 'size to initialize the object
4281 -- (primitive that is not available in CPP tagged types).
4282
4283 if Is_Class_Wide_Type (Act_T)
4284 and then
4285 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4286 or else
4287 (Present (Full_View (Root_Type (Etype (Act_T))))
4288 and then
4289 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4290 then
4291 Error_Msg_N
4292 ("predefined assignment not available for 'C'P'P tagged types",
4293 E);
4294 end if;
4295
4296 Mark_Coextensions (N, E);
4297 Analyze (E);
4298
4299 -- In case of errors detected in the analysis of the expression,
4300 -- decorate it with the expected type to avoid cascaded errors.
4301
4302 if No (Etype (E)) then
4303 Set_Etype (E, T);
4304 end if;
4305
4306 -- If an initialization expression is present, then we set the
4307 -- Is_True_Constant flag. It will be reset if this is a variable
4308 -- and it is indeed modified.
4309
4310 Set_Is_True_Constant (Id, True);
4311
4312 -- If we are analyzing a constant declaration, set its completion
4313 -- flag after analyzing and resolving the expression.
4314
4315 if Constant_Present (N) then
4316 Set_Has_Completion (Id);
4317 end if;
4318
4319 -- Set type and resolve (type may be overridden later on). Note:
4320 -- Ekind (Id) must still be E_Void at this point so that incorrect
4321 -- early usage within E is properly diagnosed.
4322
4323 Set_Etype (Id, T);
4324
4325 -- If the expression is an aggregate we must look ahead to detect
4326 -- the possible presence of an address clause, and defer resolution
4327 -- and expansion of the aggregate to the freeze point of the entity.
4328
4329 -- This is not always legal because the aggregate may contain other
4330 -- references that need freezing, e.g. references to other entities
4331 -- with address clauses. In any case, when compiling with -gnatI the
4332 -- presence of the address clause must be ignored.
4333
4334 if Comes_From_Source (N)
4335 and then Expander_Active
4336 and then Nkind (E) = N_Aggregate
4337 and then
4338 ((Present (Following_Address_Clause (N))
4339 and then not Ignore_Rep_Clauses)
4340 or else Delayed_Aspect_Present)
4341 then
4342 Set_Etype (E, T);
4343
4344 -- If the aggregate is limited it will be built in place, and its
4345 -- expansion is deferred until the object declaration is expanded.
4346
4347 -- This is also required when generating C code to ensure that an
4348 -- object with an alignment or address clause can be initialized
4349 -- by means of component by component assignments.
4350
4351 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4352 Set_Expansion_Delayed (E);
4353 end if;
4354
4355 else
4356 -- If the expression is a formal that is a "subprogram pointer"
4357 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4358 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4359 -- the corresponding check, as is done for assignments.
4360
4361 if Is_Entity_Name (E)
4362 and then Present (Entity (E))
4363 and then Is_Formal (Entity (E))
4364 and then
4365 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4366 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4367 then
4368 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4369 end if;
4370
4371 Resolve (E, T);
4372 end if;
4373
4374 -- No further action needed if E is a call to an inlined function
4375 -- which returns an unconstrained type and it has been expanded into
4376 -- a procedure call. In that case N has been replaced by an object
4377 -- declaration without initializing expression and it has been
4378 -- analyzed (see Expand_Inlined_Call).
4379
4380 if Back_End_Inlining
4381 and then Expander_Active
4382 and then Nkind (E) = N_Function_Call
4383 and then Nkind (Name (E)) in N_Has_Entity
4384 and then Is_Inlined (Entity (Name (E)))
4385 and then not Is_Constrained (Etype (E))
4386 and then Analyzed (N)
4387 and then No (Expression (N))
4388 then
4389 goto Leave;
4390 end if;
4391
4392 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4393 -- node (which was marked already-analyzed), we need to set the type
4394 -- to something other than Any_Access in order to keep gigi happy.
4395
4396 if Etype (E) = Any_Access then
4397 Set_Etype (E, T);
4398 end if;
4399
4400 -- If the object is an access to variable, the initialization
4401 -- expression cannot be an access to constant.
4402
4403 if Is_Access_Type (T)
4404 and then not Is_Access_Constant (T)
4405 and then Is_Access_Type (Etype (E))
4406 and then Is_Access_Constant (Etype (E))
4407 then
4408 Error_Msg_N
4409 ("access to variable cannot be initialized with an "
4410 & "access-to-constant expression", E);
4411 end if;
4412
4413 if not Assignment_OK (N) then
4414 Check_Initialization (T, E);
4415 end if;
4416
4417 Check_Unset_Reference (E);
4418
4419 -- If this is a variable, then set current value. If this is a
4420 -- declared constant of a scalar type with a static expression,
4421 -- indicate that it is always valid.
4422
4423 if not Constant_Present (N) then
4424 if Compile_Time_Known_Value (E) then
4425 Set_Current_Value (Id, E);
4426 end if;
4427
4428 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4429 Set_Is_Known_Valid (Id);
4430
4431 -- If it is a constant initialized with a valid nonstatic entity,
4432 -- the constant is known valid as well, and can inherit the subtype
4433 -- of the entity if it is a subtype of the given type. This info
4434 -- is preserved on the actual subtype of the constant.
4435
4436 elsif Is_Scalar_Type (T)
4437 and then Is_Entity_Name (E)
4438 and then Is_Known_Valid (Entity (E))
4439 and then In_Subrange_Of (Etype (Entity (E)), T)
4440 then
4441 Set_Is_Known_Valid (Id);
4442 Mutate_Ekind (Id, E_Constant);
4443 Set_Actual_Subtype (Id, Etype (Entity (E)));
4444 end if;
4445
4446 -- Deal with setting of null flags
4447
4448 if Is_Access_Type (T) then
4449 if Known_Non_Null (E) then
4450 Set_Is_Known_Non_Null (Id, True);
4451 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4452 Set_Is_Known_Null (Id, True);
4453 end if;
4454 end if;
4455
4456 -- Check incorrect use of dynamically tagged expressions
4457
4458 if Is_Tagged_Type (T) then
4459 Check_Dynamically_Tagged_Expression
4460 (Expr => E,
4461 Typ => T,
4462 Related_Nod => N);
4463 end if;
4464
4465 Apply_Scalar_Range_Check (E, T);
4466 Apply_Static_Length_Check (E, T);
4467
4468 -- A formal parameter of a specific tagged type whose related
4469 -- subprogram is subject to pragma Extensions_Visible with value
4470 -- "False" cannot be implicitly converted to a class-wide type by
4471 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4472 -- not consider internally generated expressions.
4473
4474 if Is_Class_Wide_Type (T)
4475 and then Comes_From_Source (E)
4476 and then Is_EVF_Expression (E)
4477 then
4478 Error_Msg_N
4479 ("formal parameter cannot be implicitly converted to "
4480 & "class-wide type when Extensions_Visible is False", E);
4481 end if;
4482 end if;
4483
4484 -- If the No_Streams restriction is set, check that the type of the
4485 -- object is not, and does not contain, any subtype derived from
4486 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4487 -- Has_Stream just for efficiency reasons. There is no point in
4488 -- spending time on a Has_Stream check if the restriction is not set.
4489
4490 if Restriction_Check_Required (No_Streams) then
4491 if Has_Stream (T) then
4492 Check_Restriction (No_Streams, N);
4493 end if;
4494 end if;
4495
4496 -- Deal with predicate check before we start to do major rewriting. It
4497 -- is OK to initialize and then check the initialized value, since the
4498 -- object goes out of scope if we get a predicate failure. Note that we
4499 -- do this in the analyzer and not the expander because the analyzer
4500 -- does some substantial rewriting in some cases.
4501
4502 -- We need a predicate check if the type has predicates that are not
4503 -- ignored, and if either there is an initializing expression, or for
4504 -- default initialization when we have at least one case of an explicit
4505 -- default initial value (including via a Default_Value or
4506 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4507 -- an internal declaration whose initialization comes later (as for an
4508 -- aggregate expansion).
4509 -- If expression is an aggregate it may be expanded into assignments
4510 -- and the declaration itself is marked with No_Initialization, but
4511 -- the predicate still applies.
4512
4513 if not Suppress_Assignment_Checks (N)
4514 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4515 and then
4516 (not No_Initialization (N)
4517 or else (Present (E) and then Nkind (E) = N_Aggregate))
4518 and then
4519 (Present (E)
4520 or else
4521 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4522 then
4523 -- If the type has a static predicate and the expression is known at
4524 -- compile time, see if the expression satisfies the predicate.
4525 -- In the case of a static expression, this must be done even if
4526 -- the predicate is not enabled (as per static expression rules).
4527
4528 if Present (E) then
4529 Check_Expression_Against_Static_Predicate (E, T);
4530 end if;
4531
4532 -- Do not perform further predicate-related checks unless
4533 -- predicates are enabled for the subtype.
4534
4535 if not Predicate_Enabled (T) then
4536 null;
4537
4538 -- If the type is a null record and there is no explicit initial
4539 -- expression, no predicate check applies.
4540
4541 elsif No (E) and then Is_Null_Record_Type (T) then
4542 null;
4543
4544 -- Do not generate a predicate check if the initialization expression
4545 -- is a type conversion because the conversion has been subjected to
4546 -- the same check. This is a small optimization which avoid redundant
4547 -- checks.
4548
4549 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4550 null;
4551
4552 else
4553 -- The check must be inserted after the expanded aggregate
4554 -- expansion code, if any.
4555
4556 declare
4557 Check : constant Node_Id :=
4558 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4559
4560 begin
4561 if No (Next_Decl) then
4562 Append_To (List_Containing (N), Check);
4563 else
4564 Insert_Before (Next_Decl, Check);
4565 end if;
4566 end;
4567 end if;
4568 end if;
4569
4570 -- Case of unconstrained type
4571
4572 if not Is_Definite_Subtype (T) then
4573
4574 -- Nothing to do in deferred constant case
4575
4576 if Constant_Present (N) and then No (E) then
4577 null;
4578
4579 -- Case of no initialization present
4580
4581 elsif No (E) then
4582 if No_Initialization (N) then
4583 null;
4584
4585 elsif Is_Class_Wide_Type (T) then
4586 Error_Msg_N
4587 ("initialization required in class-wide declaration", N);
4588
4589 else
4590 Error_Msg_N
4591 ("unconstrained subtype not allowed (need initialization)",
4592 Object_Definition (N));
4593
4594 if Is_Record_Type (T) and then Has_Discriminants (T) then
4595 Error_Msg_N
4596 ("\provide initial value or explicit discriminant values",
4597 Object_Definition (N));
4598
4599 Error_Msg_NE
4600 ("\or give default discriminant values for type&",
4601 Object_Definition (N), T);
4602
4603 elsif Is_Array_Type (T) then
4604 Error_Msg_N
4605 ("\provide initial value or explicit array bounds",
4606 Object_Definition (N));
4607 end if;
4608 end if;
4609
4610 -- Case of initialization present but in error. Set initial
4611 -- expression as absent (but do not make above complaints).
4612
4613 elsif E = Error then
4614 Set_Expression (N, Empty);
4615 E := Empty;
4616
4617 -- Case of initialization present
4618
4619 else
4620 -- Unconstrained variables not allowed in Ada 83
4621
4622 if Ada_Version = Ada_83
4623 and then not Constant_Present (N)
4624 and then Comes_From_Source (Object_Definition (N))
4625 then
4626 Error_Msg_N
4627 ("(Ada 83) unconstrained variable not allowed",
4628 Object_Definition (N));
4629 end if;
4630
4631 -- Now we constrain the variable from the initializing expression
4632
4633 -- If the expression is an aggregate, it has been expanded into
4634 -- individual assignments. Retrieve the actual type from the
4635 -- expanded construct.
4636
4637 if Is_Array_Type (T)
4638 and then No_Initialization (N)
4639 and then Nkind (Original_Node (E)) = N_Aggregate
4640 then
4641 Act_T := Etype (E);
4642
4643 -- In case of class-wide interface object declarations we delay
4644 -- the generation of the equivalent record type declarations until
4645 -- its expansion because there are cases in they are not required.
4646
4647 elsif Is_Interface (T) then
4648 null;
4649
4650 -- If the type is an unchecked union, no subtype can be built from
4651 -- the expression. Rewrite declaration as a renaming, which the
4652 -- back-end can handle properly. This is a rather unusual case,
4653 -- because most unchecked_union declarations have default values
4654 -- for discriminants and are thus not indefinite.
4655
4656 elsif Is_Unchecked_Union (T) then
4657 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4658 Mutate_Ekind (Id, E_Constant);
4659 else
4660 Mutate_Ekind (Id, E_Variable);
4661 end if;
4662
4663 -- If the expression is an aggregate it contains the required
4664 -- discriminant values but it has not been resolved yet, so do
4665 -- it now, and treat it as the initial expression of an object
4666 -- declaration, rather than a renaming.
4667
4668 if Nkind (E) = N_Aggregate then
4669 Analyze_And_Resolve (E, T);
4670
4671 else
4672 Rewrite (N,
4673 Make_Object_Renaming_Declaration (Loc,
4674 Defining_Identifier => Id,
4675 Subtype_Mark => New_Occurrence_Of (T, Loc),
4676 Name => E));
4677
4678 Set_Renamed_Object (Id, E);
4679 Freeze_Before (N, T);
4680 Set_Is_Frozen (Id);
4681 goto Leave;
4682 end if;
4683
4684 else
4685 -- Ensure that the generated subtype has a unique external name
4686 -- when the related object is public. This guarantees that the
4687 -- subtype and its bounds will not be affected by switches or
4688 -- pragmas that may offset the internal counter due to extra
4689 -- generated code.
4690
4691 if Is_Public (Id) then
4692 Related_Id := Id;
4693 else
4694 Related_Id := Empty;
4695 end if;
4696
4697 -- If the object has an unconstrained array subtype with fixed
4698 -- lower bound, then sliding to that bound may be needed.
4699
4700 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4701 Expand_Sliding_Conversion (E, T);
4702 end if;
4703
4704 Expand_Subtype_From_Expr
4705 (N => N,
4706 Unc_Type => T,
4707 Subtype_Indic => Object_Definition (N),
4708 Exp => E,
4709 Related_Id => Related_Id);
4710
4711 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4712 end if;
4713
4714 -- Propagate attributes to full view when needed
4715
4716 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4717
4718 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4719 then
4720 Full_View_Present := True;
4721 end if;
4722
4723 if Full_View_Present then
4724 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4725 end if;
4726
4727 if Aliased_Present (N) then
4728 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4729
4730 if Full_View_Present then
4731 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4732 end if;
4733 end if;
4734
4735 Freeze_Before (N, Act_T);
4736 Freeze_Before (N, T);
4737 end if;
4738
4739 elsif Is_Array_Type (T)
4740 and then No_Initialization (N)
4741 and then (Nkind (Original_Node (E)) = N_Aggregate
4742 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4743 and then Nkind (Original_Node (Expression
4744 (Original_Node (E)))) = N_Aggregate))
4745 then
4746 if not Is_Entity_Name (Object_Definition (N)) then
4747 Act_T := Etype (E);
4748 Check_Compile_Time_Size (Act_T);
4749
4750 if Aliased_Present (N) then
4751 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4752 end if;
4753 end if;
4754
4755 -- When the given object definition and the aggregate are specified
4756 -- independently, and their lengths might differ do a length check.
4757 -- This cannot happen if the aggregate is of the form (others =>...)
4758
4759 if not Is_Constrained (T) then
4760 null;
4761
4762 elsif Nkind (E) = N_Raise_Constraint_Error then
4763
4764 -- Aggregate is statically illegal. Place back in declaration
4765
4766 Set_Expression (N, E);
4767 Set_No_Initialization (N, False);
4768
4769 elsif T = Etype (E) then
4770 null;
4771
4772 elsif Nkind (E) = N_Aggregate
4773 and then Present (Component_Associations (E))
4774 and then Present (Choice_List (First (Component_Associations (E))))
4775 and then
4776 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4777 N_Others_Choice
4778 then
4779 null;
4780
4781 else
4782 Apply_Length_Check (E, T);
4783 end if;
4784
4785 -- If the type is limited unconstrained with defaulted discriminants and
4786 -- there is no expression, then the object is constrained by the
4787 -- defaults, so it is worthwhile building the corresponding subtype.
4788
4789 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4790 and then not Is_Constrained (T)
4791 and then Has_Discriminants (T)
4792 then
4793 if No (E) then
4794 Act_T := Build_Default_Subtype (T, N);
4795 else
4796 -- Ada 2005: A limited object may be initialized by means of an
4797 -- aggregate. If the type has default discriminants it has an
4798 -- unconstrained nominal type, Its actual subtype will be obtained
4799 -- from the aggregate, and not from the default discriminants.
4800
4801 Act_T := Etype (E);
4802 end if;
4803
4804 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4805
4806 elsif Nkind (E) = N_Function_Call
4807 and then Constant_Present (N)
4808 and then Has_Unconstrained_Elements (Etype (E))
4809 then
4810 -- The back-end has problems with constants of a discriminated type
4811 -- with defaults, if the initial value is a function call. We
4812 -- generate an intermediate temporary that will receive a reference
4813 -- to the result of the call. The initialization expression then
4814 -- becomes a dereference of that temporary.
4815
4816 Remove_Side_Effects (E);
4817
4818 -- If this is a constant declaration of an unconstrained type and
4819 -- the initialization is an aggregate, we can use the subtype of the
4820 -- aggregate for the declared entity because it is immutable.
4821
4822 elsif not Is_Constrained (T)
4823 and then Has_Discriminants (T)
4824 and then Constant_Present (N)
4825 and then not Has_Unchecked_Union (T)
4826 and then Nkind (E) = N_Aggregate
4827 then
4828 Act_T := Etype (E);
4829 end if;
4830
4831 -- Check No_Wide_Characters restriction
4832
4833 Check_Wide_Character_Restriction (T, Object_Definition (N));
4834
4835 -- Indicate this is not set in source. Certainly true for constants, and
4836 -- true for variables so far (will be reset for a variable if and when
4837 -- we encounter a modification in the source).
4838
4839 Set_Never_Set_In_Source (Id);
4840
4841 -- Now establish the proper kind and type of the object
4842
4843 if Ekind (Id) = E_Void then
4844 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
4845 end if;
4846
4847 if Constant_Present (N) then
4848 Mutate_Ekind (Id, E_Constant);
4849 Set_Is_True_Constant (Id);
4850
4851 else
4852 Mutate_Ekind (Id, E_Variable);
4853
4854 -- A variable is set as shared passive if it appears in a shared
4855 -- passive package, and is at the outer level. This is not done for
4856 -- entities generated during expansion, because those are always
4857 -- manipulated locally.
4858
4859 if Is_Shared_Passive (Current_Scope)
4860 and then Is_Library_Level_Entity (Id)
4861 and then Comes_From_Source (Id)
4862 then
4863 Set_Is_Shared_Passive (Id);
4864 Check_Shared_Var (Id, T, N);
4865 end if;
4866
4867 -- Set Has_Initial_Value if initializing expression present. Note
4868 -- that if there is no initializing expression, we leave the state
4869 -- of this flag unchanged (usually it will be False, but notably in
4870 -- the case of exception choice variables, it will already be true).
4871
4872 if Present (E) then
4873 Set_Has_Initial_Value (Id);
4874 end if;
4875 end if;
4876
4877 -- Set the SPARK mode from the current context (may be overwritten later
4878 -- with explicit pragma).
4879
4880 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4881 Set_SPARK_Pragma_Inherited (Id);
4882
4883 -- Preserve relevant elaboration-related attributes of the context which
4884 -- are no longer available or very expensive to recompute once analysis,
4885 -- resolution, and expansion are over.
4886
4887 Mark_Elaboration_Attributes
4888 (N_Id => Id,
4889 Checks => True,
4890 Warnings => True);
4891
4892 -- Initialize alignment and size and capture alignment setting
4893
4894 Reinit_Alignment (Id);
4895 Reinit_Esize (Id);
4896 Set_Optimize_Alignment_Flags (Id);
4897
4898 -- Deal with aliased case
4899
4900 if Aliased_Present (N) then
4901 Set_Is_Aliased (Id);
4902
4903 -- AI12-001: All aliased objects are considered to be specified as
4904 -- independently addressable (RM C.6(8.1/4)).
4905
4906 Set_Is_Independent (Id);
4907
4908 -- If the object is aliased and the type is unconstrained with
4909 -- defaulted discriminants and there is no expression, then the
4910 -- object is constrained by the defaults, so it is worthwhile
4911 -- building the corresponding subtype.
4912
4913 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4914 -- unconstrained, then only establish an actual subtype if the
4915 -- nominal subtype is indefinite. In definite cases the object is
4916 -- unconstrained in Ada 2005.
4917
4918 if No (E)
4919 and then Is_Record_Type (T)
4920 and then not Is_Constrained (T)
4921 and then Has_Discriminants (T)
4922 and then (Ada_Version < Ada_2005
4923 or else not Is_Definite_Subtype (T))
4924 then
4925 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4926 end if;
4927 end if;
4928
4929 -- Now we can set the type of the object
4930
4931 Set_Etype (Id, Act_T);
4932
4933 -- Non-constant object is marked to be treated as volatile if type is
4934 -- volatile and we clear the Current_Value setting that may have been
4935 -- set above. Doing so for constants isn't required and might interfere
4936 -- with possible uses of the object as a static expression in contexts
4937 -- incompatible with volatility (e.g. as a case-statement alternative).
4938
4939 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4940 Set_Treat_As_Volatile (Id);
4941 Set_Current_Value (Id, Empty);
4942 end if;
4943
4944 -- Deal with controlled types
4945
4946 if Has_Controlled_Component (Etype (Id))
4947 or else Is_Controlled (Etype (Id))
4948 then
4949 if not Is_Library_Level_Entity (Id) then
4950 Check_Restriction (No_Nested_Finalization, N);
4951 else
4952 Validate_Controlled_Object (Id);
4953 end if;
4954 end if;
4955
4956 if Has_Task (Etype (Id)) then
4957 Check_Restriction (No_Tasking, N);
4958
4959 -- Deal with counting max tasks
4960
4961 -- Nothing to do if inside a generic
4962
4963 if Inside_A_Generic then
4964 null;
4965
4966 -- If library level entity, then count tasks
4967
4968 elsif Is_Library_Level_Entity (Id) then
4969 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4970
4971 -- If not library level entity, then indicate we don't know max
4972 -- tasks and also check task hierarchy restriction and blocking
4973 -- operation (since starting a task is definitely blocking).
4974
4975 else
4976 Check_Restriction (Max_Tasks, N);
4977 Check_Restriction (No_Task_Hierarchy, N);
4978 Check_Potentially_Blocking_Operation (N);
4979 end if;
4980
4981 -- A rather specialized test. If we see two tasks being declared
4982 -- of the same type in the same object declaration, and the task
4983 -- has an entry with an address clause, we know that program error
4984 -- will be raised at run time since we can't have two tasks with
4985 -- entries at the same address.
4986
4987 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4988 declare
4989 E : Entity_Id;
4990
4991 begin
4992 E := First_Entity (Etype (Id));
4993 while Present (E) loop
4994 if Ekind (E) = E_Entry
4995 and then Present (Get_Attribute_Definition_Clause
4996 (E, Attribute_Address))
4997 then
4998 Error_Msg_Warn := SPARK_Mode /= On;
4999 Error_Msg_N
5000 ("more than one task with same entry address<<", N);
5001 Error_Msg_N ("\Program_Error [<<", N);
5002 Insert_Action (N,
5003 Make_Raise_Program_Error (Loc,
5004 Reason => PE_Duplicated_Entry_Address));
5005 exit;
5006 end if;
5007
5008 Next_Entity (E);
5009 end loop;
5010 end;
5011 end if;
5012 end if;
5013
5014 -- Some simple constant-propagation: if the expression is a constant
5015 -- string initialized with a literal, share the literal. This avoids
5016 -- a run-time copy.
5017
5018 if Present (E)
5019 and then Is_Entity_Name (E)
5020 and then Ekind (Entity (E)) = E_Constant
5021 and then Base_Type (Etype (E)) = Standard_String
5022 then
5023 declare
5024 Val : constant Node_Id := Constant_Value (Entity (E));
5025 begin
5026 if Present (Val) and then Nkind (Val) = N_String_Literal then
5027 Rewrite (E, New_Copy (Val));
5028 end if;
5029 end;
5030 end if;
5031
5032 -- Another optimization: if the nominal subtype is unconstrained and
5033 -- the expression is a function call that returns an unconstrained
5034 -- type, rewrite the declaration as a renaming of the result of the
5035 -- call. The exceptions below are cases where the copy is expected,
5036 -- either by the back end (Aliased case) or by the semantics, as for
5037 -- initializing controlled types or copying tags for class-wide types.
5038
5039 if Present (E)
5040 and then Nkind (E) = N_Explicit_Dereference
5041 and then Nkind (Original_Node (E)) = N_Function_Call
5042 and then not Is_Library_Level_Entity (Id)
5043 and then not Is_Constrained (Underlying_Type (T))
5044 and then not Is_Aliased (Id)
5045 and then not Is_Class_Wide_Type (T)
5046 and then not Is_Controlled (T)
5047 and then not Has_Controlled_Component (Base_Type (T))
5048 and then Expander_Active
5049 then
5050 Rewrite (N,
5051 Make_Object_Renaming_Declaration (Loc,
5052 Defining_Identifier => Id,
5053 Access_Definition => Empty,
5054 Subtype_Mark => New_Occurrence_Of
5055 (Base_Type (Etype (Id)), Loc),
5056 Name => E));
5057
5058 Set_Renamed_Object (Id, E);
5059
5060 -- Force generation of debugging information for the constant and for
5061 -- the renamed function call.
5062
5063 Set_Debug_Info_Needed (Id);
5064 Set_Debug_Info_Needed (Entity (Prefix (E)));
5065 end if;
5066
5067 if Present (Prev_Entity)
5068 and then Is_Frozen (Prev_Entity)
5069 and then not Error_Posted (Id)
5070 then
5071 Error_Msg_N ("full constant declaration appears too late", N);
5072 end if;
5073
5074 Check_Eliminated (Id);
5075
5076 -- Deal with setting In_Private_Part flag if in private part
5077
5078 if Ekind (Scope (Id)) = E_Package
5079 and then In_Private_Part (Scope (Id))
5080 then
5081 Set_In_Private_Part (Id);
5082 end if;
5083
5084 <<Leave>>
5085 -- Initialize the refined state of a variable here because this is a
5086 -- common destination for legal and illegal object declarations.
5087
5088 if Ekind (Id) = E_Variable then
5089 Set_Encapsulating_State (Id, Empty);
5090 end if;
5091
5092 if Has_Aspects (N) then
5093 Analyze_Aspect_Specifications (N, Id);
5094 end if;
5095
5096 Analyze_Dimension (N);
5097
5098 -- Verify whether the object declaration introduces an illegal hidden
5099 -- state within a package subject to a null abstract state.
5100
5101 if Ekind (Id) = E_Variable then
5102 Check_No_Hidden_State (Id);
5103 end if;
5104
5105 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5106 end Analyze_Object_Declaration;
5107
5108 ---------------------------
5109 -- Analyze_Others_Choice --
5110 ---------------------------
5111
5112 -- Nothing to do for the others choice node itself, the semantic analysis
5113 -- of the others choice will occur as part of the processing of the parent
5114
5115 procedure Analyze_Others_Choice (N : Node_Id) is
5116 pragma Warnings (Off, N);
5117 begin
5118 null;
5119 end Analyze_Others_Choice;
5120
5121 -------------------------------------------
5122 -- Analyze_Private_Extension_Declaration --
5123 -------------------------------------------
5124
5125 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5126 Indic : constant Node_Id := Subtype_Indication (N);
5127 T : constant Entity_Id := Defining_Identifier (N);
5128 Iface : Entity_Id;
5129 Iface_Elmt : Elmt_Id;
5130 Parent_Base : Entity_Id;
5131 Parent_Type : Entity_Id;
5132
5133 begin
5134 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5135
5136 if Is_Non_Empty_List (Interface_List (N)) then
5137 declare
5138 Intf : Node_Id;
5139 T : Entity_Id;
5140
5141 begin
5142 Intf := First (Interface_List (N));
5143 while Present (Intf) loop
5144 T := Find_Type_Of_Subtype_Indic (Intf);
5145
5146 Diagnose_Interface (Intf, T);
5147 Next (Intf);
5148 end loop;
5149 end;
5150 end if;
5151
5152 Generate_Definition (T);
5153
5154 -- For other than Ada 2012, just enter the name in the current scope
5155
5156 if Ada_Version < Ada_2012 then
5157 Enter_Name (T);
5158
5159 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5160 -- case of private type that completes an incomplete type.
5161
5162 else
5163 declare
5164 Prev : Entity_Id;
5165
5166 begin
5167 Prev := Find_Type_Name (N);
5168
5169 pragma Assert (Prev = T
5170 or else (Ekind (Prev) = E_Incomplete_Type
5171 and then Present (Full_View (Prev))
5172 and then Full_View (Prev) = T));
5173 end;
5174 end if;
5175
5176 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5177 Parent_Base := Base_Type (Parent_Type);
5178
5179 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5180 Mutate_Ekind (T, Ekind (Parent_Type));
5181 Set_Etype (T, Any_Type);
5182 goto Leave;
5183
5184 elsif not Is_Tagged_Type (Parent_Type) then
5185 Error_Msg_N
5186 ("parent of type extension must be a tagged type", Indic);
5187 goto Leave;
5188
5189 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5190 Error_Msg_N ("premature derivation of incomplete type", Indic);
5191 goto Leave;
5192
5193 elsif Is_Concurrent_Type (Parent_Type) then
5194 Error_Msg_N
5195 ("parent type of a private extension cannot be a synchronized "
5196 & "tagged type (RM 3.9.1 (3/1))", N);
5197
5198 Set_Etype (T, Any_Type);
5199 Mutate_Ekind (T, E_Limited_Private_Type);
5200 Set_Private_Dependents (T, New_Elmt_List);
5201 Set_Error_Posted (T);
5202 goto Leave;
5203 end if;
5204
5205 Check_Wide_Character_Restriction (Parent_Type, Indic);
5206
5207 -- Perhaps the parent type should be changed to the class-wide type's
5208 -- specific type in this case to prevent cascading errors ???
5209
5210 if Is_Class_Wide_Type (Parent_Type) then
5211 Error_Msg_N
5212 ("parent of type extension must not be a class-wide type", Indic);
5213 goto Leave;
5214 end if;
5215
5216 if (not Is_Package_Or_Generic_Package (Current_Scope)
5217 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5218 or else In_Private_Part (Current_Scope)
5219 then
5220 Error_Msg_N ("invalid context for private extension", N);
5221 end if;
5222
5223 -- Set common attributes
5224
5225 Set_Is_Pure (T, Is_Pure (Current_Scope));
5226 Set_Scope (T, Current_Scope);
5227 Mutate_Ekind (T, E_Record_Type_With_Private);
5228 Reinit_Size_Align (T);
5229 Set_Default_SSO (T);
5230 Set_No_Reordering (T, No_Component_Reordering);
5231
5232 Set_Etype (T, Parent_Base);
5233 Propagate_Concurrent_Flags (T, Parent_Base);
5234
5235 Set_Convention (T, Convention (Parent_Type));
5236 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5237 Set_Is_First_Subtype (T);
5238 Make_Class_Wide_Type (T);
5239
5240 -- Set the SPARK mode from the current context
5241
5242 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5243 Set_SPARK_Pragma_Inherited (T);
5244
5245 if Unknown_Discriminants_Present (N) then
5246 Set_Discriminant_Constraint (T, No_Elist);
5247 end if;
5248
5249 Build_Derived_Record_Type (N, Parent_Type, T);
5250
5251 -- A private extension inherits the Default_Initial_Condition pragma
5252 -- coming from any parent type within the derivation chain.
5253
5254 if Has_DIC (Parent_Type) then
5255 Set_Has_Inherited_DIC (T);
5256 end if;
5257
5258 -- A private extension inherits any class-wide invariants coming from a
5259 -- parent type or an interface. Note that the invariant procedure of the
5260 -- parent type should not be inherited because the private extension may
5261 -- define invariants of its own.
5262
5263 if Has_Inherited_Invariants (Parent_Type)
5264 or else Has_Inheritable_Invariants (Parent_Type)
5265 then
5266 Set_Has_Inherited_Invariants (T);
5267
5268 elsif Present (Interfaces (T)) then
5269 Iface_Elmt := First_Elmt (Interfaces (T));
5270 while Present (Iface_Elmt) loop
5271 Iface := Node (Iface_Elmt);
5272
5273 if Has_Inheritable_Invariants (Iface) then
5274 Set_Has_Inherited_Invariants (T);
5275 exit;
5276 end if;
5277
5278 Next_Elmt (Iface_Elmt);
5279 end loop;
5280 end if;
5281
5282 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5283 -- synchronized formal derived type.
5284
5285 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5286 Set_Is_Limited_Record (T);
5287
5288 -- Formal derived type case
5289
5290 if Is_Generic_Type (T) then
5291
5292 -- The parent must be a tagged limited type or a synchronized
5293 -- interface.
5294
5295 if (not Is_Tagged_Type (Parent_Type)
5296 or else not Is_Limited_Type (Parent_Type))
5297 and then
5298 (not Is_Interface (Parent_Type)
5299 or else not Is_Synchronized_Interface (Parent_Type))
5300 then
5301 Error_Msg_NE
5302 ("parent type of & must be tagged limited or synchronized",
5303 N, T);
5304 end if;
5305
5306 -- The progenitors (if any) must be limited or synchronized
5307 -- interfaces.
5308
5309 if Present (Interfaces (T)) then
5310 Iface_Elmt := First_Elmt (Interfaces (T));
5311 while Present (Iface_Elmt) loop
5312 Iface := Node (Iface_Elmt);
5313
5314 if not Is_Limited_Interface (Iface)
5315 and then not Is_Synchronized_Interface (Iface)
5316 then
5317 Error_Msg_NE
5318 ("progenitor & must be limited or synchronized",
5319 N, Iface);
5320 end if;
5321
5322 Next_Elmt (Iface_Elmt);
5323 end loop;
5324 end if;
5325
5326 -- Regular derived extension, the parent must be a limited or
5327 -- synchronized interface.
5328
5329 else
5330 if not Is_Interface (Parent_Type)
5331 or else (not Is_Limited_Interface (Parent_Type)
5332 and then not Is_Synchronized_Interface (Parent_Type))
5333 then
5334 Error_Msg_NE
5335 ("parent type of & must be limited interface", N, T);
5336 end if;
5337 end if;
5338
5339 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5340 -- extension with a synchronized parent must be explicitly declared
5341 -- synchronized, because the full view will be a synchronized type.
5342 -- This must be checked before the check for limited types below,
5343 -- to ensure that types declared limited are not allowed to extend
5344 -- synchronized interfaces.
5345
5346 elsif Is_Interface (Parent_Type)
5347 and then Is_Synchronized_Interface (Parent_Type)
5348 and then not Synchronized_Present (N)
5349 then
5350 Error_Msg_NE
5351 ("private extension of& must be explicitly synchronized",
5352 N, Parent_Type);
5353
5354 elsif Limited_Present (N) then
5355 Set_Is_Limited_Record (T);
5356
5357 if not Is_Limited_Type (Parent_Type)
5358 and then
5359 (not Is_Interface (Parent_Type)
5360 or else not Is_Limited_Interface (Parent_Type))
5361 then
5362 Error_Msg_NE ("parent type& of limited extension must be limited",
5363 N, Parent_Type);
5364 end if;
5365 end if;
5366
5367 -- Remember that its parent type has a private extension. Used to warn
5368 -- on public primitives of the parent type defined after its private
5369 -- extensions (see Check_Dispatching_Operation).
5370
5371 Set_Has_Private_Extension (Parent_Type);
5372
5373 <<Leave>>
5374 if Has_Aspects (N) then
5375 Analyze_Aspect_Specifications (N, T);
5376 end if;
5377 end Analyze_Private_Extension_Declaration;
5378
5379 ---------------------------------
5380 -- Analyze_Subtype_Declaration --
5381 ---------------------------------
5382
5383 procedure Analyze_Subtype_Declaration
5384 (N : Node_Id;
5385 Skip : Boolean := False)
5386 is
5387 Id : constant Entity_Id := Defining_Identifier (N);
5388 T : Entity_Id;
5389
5390 begin
5391 Generate_Definition (Id);
5392 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5393 Reinit_Size_Align (Id);
5394
5395 -- The following guard condition on Enter_Name is to handle cases where
5396 -- the defining identifier has already been entered into the scope but
5397 -- the declaration as a whole needs to be analyzed.
5398
5399 -- This case in particular happens for derived enumeration types. The
5400 -- derived enumeration type is processed as an inserted enumeration type
5401 -- declaration followed by a rewritten subtype declaration. The defining
5402 -- identifier, however, is entered into the name scope very early in the
5403 -- processing of the original type declaration and therefore needs to be
5404 -- avoided here, when the created subtype declaration is analyzed. (See
5405 -- Build_Derived_Types)
5406
5407 -- This also happens when the full view of a private type is derived
5408 -- type with constraints. In this case the entity has been introduced
5409 -- in the private declaration.
5410
5411 -- Finally this happens in some complex cases when validity checks are
5412 -- enabled, where the same subtype declaration may be analyzed twice.
5413 -- This can happen if the subtype is created by the preanalysis of
5414 -- an attribute tht gives the range of a loop statement, and the loop
5415 -- itself appears within an if_statement that will be rewritten during
5416 -- expansion.
5417
5418 if Skip
5419 or else (Present (Etype (Id))
5420 and then (Is_Private_Type (Etype (Id))
5421 or else Is_Task_Type (Etype (Id))
5422 or else Is_Rewrite_Substitution (N)))
5423 then
5424 null;
5425
5426 elsif Current_Entity (Id) = Id then
5427 null;
5428
5429 else
5430 Enter_Name (Id);
5431 end if;
5432
5433 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5434
5435 -- Class-wide equivalent types of records with unknown discriminants
5436 -- involve the generation of an itype which serves as the private view
5437 -- of a constrained record subtype. In such cases the base type of the
5438 -- current subtype we are processing is the private itype. Use the full
5439 -- of the private itype when decorating various attributes.
5440
5441 if Is_Itype (T)
5442 and then Is_Private_Type (T)
5443 and then Present (Full_View (T))
5444 then
5445 T := Full_View (T);
5446 end if;
5447
5448 -- Inherit common attributes
5449
5450 Set_Is_Volatile (Id, Is_Volatile (T));
5451 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5452 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5453 Set_Convention (Id, Convention (T));
5454
5455 -- If ancestor has predicates then so does the subtype, and in addition
5456 -- we must delay the freeze to properly arrange predicate inheritance.
5457
5458 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5459 -- in which T = ID, so the above tests and assignments do nothing???
5460
5461 if Has_Predicates (T)
5462 or else (Present (Ancestor_Subtype (T))
5463 and then Has_Predicates (Ancestor_Subtype (T)))
5464 then
5465 Set_Has_Predicates (Id);
5466 Set_Has_Delayed_Freeze (Id);
5467
5468 -- Generated subtypes inherit the predicate function from the parent
5469 -- (no aspects to examine on the generated declaration).
5470
5471 if not Comes_From_Source (N) then
5472 Mutate_Ekind (Id, Ekind (T));
5473
5474 if Present (Predicate_Function (Id)) then
5475 null;
5476
5477 elsif Present (Predicate_Function (T)) then
5478 Set_Predicate_Function (Id, Predicate_Function (T));
5479
5480 elsif Present (Ancestor_Subtype (T))
5481 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5482 then
5483 Set_Predicate_Function (Id,
5484 Predicate_Function (Ancestor_Subtype (T)));
5485 end if;
5486 end if;
5487 end if;
5488
5489 -- In the case where there is no constraint given in the subtype
5490 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5491 -- semantic attributes must be established here.
5492
5493 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5494 Set_Etype (Id, Base_Type (T));
5495
5496 case Ekind (T) is
5497 when Array_Kind =>
5498 Mutate_Ekind (Id, E_Array_Subtype);
5499 Copy_Array_Subtype_Attributes (Id, T);
5500
5501 when Decimal_Fixed_Point_Kind =>
5502 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5503 Set_Digits_Value (Id, Digits_Value (T));
5504 Set_Delta_Value (Id, Delta_Value (T));
5505 Set_Scale_Value (Id, Scale_Value (T));
5506 Set_Small_Value (Id, Small_Value (T));
5507 Set_Scalar_Range (Id, Scalar_Range (T));
5508 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5509 Set_Is_Constrained (Id, Is_Constrained (T));
5510 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5511 Copy_RM_Size (To => Id, From => T);
5512
5513 when Enumeration_Kind =>
5514 Mutate_Ekind (Id, E_Enumeration_Subtype);
5515 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5516 Set_Scalar_Range (Id, Scalar_Range (T));
5517 Set_Is_Character_Type (Id, Is_Character_Type (T));
5518 Set_Is_Constrained (Id, Is_Constrained (T));
5519 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5520 Copy_RM_Size (To => Id, From => T);
5521
5522 when Ordinary_Fixed_Point_Kind =>
5523 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5524 Set_Scalar_Range (Id, Scalar_Range (T));
5525 Set_Small_Value (Id, Small_Value (T));
5526 Set_Delta_Value (Id, Delta_Value (T));
5527 Set_Is_Constrained (Id, Is_Constrained (T));
5528 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5529 Copy_RM_Size (To => Id, From => T);
5530
5531 when Float_Kind =>
5532 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5533 Set_Scalar_Range (Id, Scalar_Range (T));
5534 Set_Digits_Value (Id, Digits_Value (T));
5535 Set_Is_Constrained (Id, Is_Constrained (T));
5536
5537 -- If the floating point type has dimensions, these will be
5538 -- inherited subsequently when Analyze_Dimensions is called.
5539
5540 when Signed_Integer_Kind =>
5541 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5542 Set_Scalar_Range (Id, Scalar_Range (T));
5543 Set_Is_Constrained (Id, Is_Constrained (T));
5544 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5545 Copy_RM_Size (To => Id, From => T);
5546
5547 when Modular_Integer_Kind =>
5548 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5549 Set_Scalar_Range (Id, Scalar_Range (T));
5550 Set_Is_Constrained (Id, Is_Constrained (T));
5551 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5552 Copy_RM_Size (To => Id, From => T);
5553
5554 when Class_Wide_Kind =>
5555 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5556 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5557 Set_Cloned_Subtype (Id, T);
5558 Set_Is_Tagged_Type (Id, True);
5559 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5560 Set_Has_Unknown_Discriminants
5561 (Id, True);
5562 Set_No_Tagged_Streams_Pragma
5563 (Id, No_Tagged_Streams_Pragma (T));
5564
5565 if Ekind (T) = E_Class_Wide_Subtype then
5566 Set_Equivalent_Type (Id, Equivalent_Type (T));
5567 end if;
5568
5569 when E_Record_Subtype
5570 | E_Record_Type
5571 =>
5572 Mutate_Ekind (Id, E_Record_Subtype);
5573
5574 -- Subtype declarations introduced for formal type parameters
5575 -- in generic instantiations should inherit the Size value of
5576 -- the type they rename.
5577
5578 if Present (Generic_Parent_Type (N)) then
5579 Copy_RM_Size (To => Id, From => T);
5580 end if;
5581
5582 if Ekind (T) = E_Record_Subtype
5583 and then Present (Cloned_Subtype (T))
5584 then
5585 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5586 else
5587 Set_Cloned_Subtype (Id, T);
5588 end if;
5589
5590 Set_First_Entity (Id, First_Entity (T));
5591 Set_Last_Entity (Id, Last_Entity (T));
5592 Set_Has_Discriminants (Id, Has_Discriminants (T));
5593 Set_Is_Constrained (Id, Is_Constrained (T));
5594 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5595 Set_Has_Implicit_Dereference
5596 (Id, Has_Implicit_Dereference (T));
5597 Set_Has_Unknown_Discriminants
5598 (Id, Has_Unknown_Discriminants (T));
5599
5600 if Has_Discriminants (T) then
5601 Set_Discriminant_Constraint
5602 (Id, Discriminant_Constraint (T));
5603 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5604
5605 elsif Has_Unknown_Discriminants (Id) then
5606 Set_Discriminant_Constraint (Id, No_Elist);
5607 end if;
5608
5609 if Is_Tagged_Type (T) then
5610 Set_Is_Tagged_Type (Id, True);
5611 Set_No_Tagged_Streams_Pragma
5612 (Id, No_Tagged_Streams_Pragma (T));
5613 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5614 Set_Direct_Primitive_Operations
5615 (Id, Direct_Primitive_Operations (T));
5616 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5617
5618 if Is_Interface (T) then
5619 Set_Is_Interface (Id);
5620 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5621 end if;
5622 end if;
5623
5624 when Private_Kind =>
5625 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5626 Set_Has_Discriminants (Id, Has_Discriminants (T));
5627 Set_Is_Constrained (Id, Is_Constrained (T));
5628 Set_First_Entity (Id, First_Entity (T));
5629 Set_Last_Entity (Id, Last_Entity (T));
5630 Set_Private_Dependents (Id, New_Elmt_List);
5631 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5632 Set_Has_Implicit_Dereference
5633 (Id, Has_Implicit_Dereference (T));
5634 Set_Has_Unknown_Discriminants
5635 (Id, Has_Unknown_Discriminants (T));
5636 Set_Known_To_Have_Preelab_Init
5637 (Id, Known_To_Have_Preelab_Init (T));
5638
5639 if Is_Tagged_Type (T) then
5640 Set_Is_Tagged_Type (Id);
5641 Set_No_Tagged_Streams_Pragma (Id,
5642 No_Tagged_Streams_Pragma (T));
5643 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5644 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5645 Set_Direct_Primitive_Operations (Id,
5646 Direct_Primitive_Operations (T));
5647 end if;
5648
5649 -- In general the attributes of the subtype of a private type
5650 -- are the attributes of the partial view of parent. However,
5651 -- the full view may be a discriminated type, and the subtype
5652 -- must share the discriminant constraint to generate correct
5653 -- calls to initialization procedures.
5654
5655 if Has_Discriminants (T) then
5656 Set_Discriminant_Constraint
5657 (Id, Discriminant_Constraint (T));
5658 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5659
5660 elsif Present (Full_View (T))
5661 and then Has_Discriminants (Full_View (T))
5662 then
5663 Set_Discriminant_Constraint
5664 (Id, Discriminant_Constraint (Full_View (T)));
5665 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5666
5667 -- This would seem semantically correct, but apparently
5668 -- generates spurious errors about missing components ???
5669
5670 -- Set_Has_Discriminants (Id);
5671 end if;
5672
5673 Prepare_Private_Subtype_Completion (Id, N);
5674
5675 -- If this is the subtype of a constrained private type with
5676 -- discriminants that has got a full view and we also have
5677 -- built a completion just above, show that the completion
5678 -- is a clone of the full view to the back-end.
5679
5680 if Has_Discriminants (T)
5681 and then not Has_Unknown_Discriminants (T)
5682 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5683 and then Present (Full_View (T))
5684 and then Present (Full_View (Id))
5685 then
5686 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5687 end if;
5688
5689 when Access_Kind =>
5690 Mutate_Ekind (Id, E_Access_Subtype);
5691 Set_Is_Constrained (Id, Is_Constrained (T));
5692 Set_Is_Access_Constant
5693 (Id, Is_Access_Constant (T));
5694 Set_Directly_Designated_Type
5695 (Id, Designated_Type (T));
5696 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5697
5698 -- A Pure library_item must not contain the declaration of a
5699 -- named access type, except within a subprogram, generic
5700 -- subprogram, task unit, or protected unit, or if it has
5701 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5702
5703 if Comes_From_Source (Id)
5704 and then In_Pure_Unit
5705 and then not In_Subprogram_Task_Protected_Unit
5706 and then not No_Pool_Assigned (Id)
5707 then
5708 Error_Msg_N
5709 ("named access types not allowed in pure unit", N);
5710 end if;
5711
5712 when Concurrent_Kind =>
5713 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5714 Set_Corresponding_Record_Type (Id,
5715 Corresponding_Record_Type (T));
5716 Set_First_Entity (Id, First_Entity (T));
5717 Set_First_Private_Entity (Id, First_Private_Entity (T));
5718 Set_Has_Discriminants (Id, Has_Discriminants (T));
5719 Set_Is_Constrained (Id, Is_Constrained (T));
5720 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5721 Set_Last_Entity (Id, Last_Entity (T));
5722
5723 if Is_Tagged_Type (T) then
5724 Set_No_Tagged_Streams_Pragma
5725 (Id, No_Tagged_Streams_Pragma (T));
5726 end if;
5727
5728 if Has_Discriminants (T) then
5729 Set_Discriminant_Constraint
5730 (Id, Discriminant_Constraint (T));
5731 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5732 end if;
5733
5734 when Incomplete_Kind =>
5735 if Ada_Version >= Ada_2005 then
5736
5737 -- In Ada 2005 an incomplete type can be explicitly tagged:
5738 -- propagate indication. Note that we also have to include
5739 -- subtypes for Ada 2012 extended use of incomplete types.
5740
5741 Mutate_Ekind (Id, E_Incomplete_Subtype);
5742 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5743 Set_Private_Dependents (Id, New_Elmt_List);
5744
5745 if Is_Tagged_Type (Id) then
5746 Set_No_Tagged_Streams_Pragma
5747 (Id, No_Tagged_Streams_Pragma (T));
5748 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5749 end if;
5750
5751 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5752 -- incomplete type visible through a limited with clause.
5753
5754 if From_Limited_With (T)
5755 and then Present (Non_Limited_View (T))
5756 then
5757 Set_From_Limited_With (Id);
5758 Set_Non_Limited_View (Id, Non_Limited_View (T));
5759
5760 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5761 -- to the private dependents of the original incomplete
5762 -- type for future transformation.
5763
5764 else
5765 Append_Elmt (Id, Private_Dependents (T));
5766 end if;
5767
5768 -- If the subtype name denotes an incomplete type an error
5769 -- was already reported by Process_Subtype.
5770
5771 else
5772 Set_Etype (Id, Any_Type);
5773 end if;
5774
5775 when others =>
5776 raise Program_Error;
5777 end case;
5778
5779 -- If there is no constraint in the subtype indication, the
5780 -- declared entity inherits predicates from the parent.
5781
5782 Inherit_Predicate_Flags (Id, T);
5783 end if;
5784
5785 -- When prefixed calls are enabled for untagged types, the subtype
5786 -- shares the primitive operations of its base type.
5787
5788 if Extensions_Allowed then
5789 Set_Direct_Primitive_Operations
5790 (Id, Direct_Primitive_Operations (Base_Type (T)));
5791 end if;
5792
5793 if Etype (Id) = Any_Type then
5794 goto Leave;
5795 end if;
5796
5797 -- Some common processing on all types
5798
5799 Set_Size_Info (Id, T);
5800 Set_First_Rep_Item (Id, First_Rep_Item (T));
5801
5802 -- If the parent type is a generic actual, so is the subtype. This may
5803 -- happen in a nested instance. Why Comes_From_Source test???
5804
5805 if not Comes_From_Source (N) then
5806 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5807 end if;
5808
5809 -- If this is a subtype declaration for an actual in an instance,
5810 -- inherit static and dynamic predicates if any.
5811
5812 -- If declaration has no aspect specifications, inherit predicate
5813 -- info as well. Unclear how to handle the case of both specified
5814 -- and inherited predicates ??? Other inherited aspects, such as
5815 -- invariants, should be OK, but the combination with later pragmas
5816 -- may also require special merging.
5817
5818 if Has_Predicates (T)
5819 and then Present (Predicate_Function (T))
5820 and then
5821 ((In_Instance and then not Comes_From_Source (N))
5822 or else No (Aspect_Specifications (N)))
5823 then
5824 -- Inherit Subprograms_For_Type from the full view, if present
5825
5826 if Present (Full_View (T))
5827 and then Subprograms_For_Type (Full_View (T)) /= No_Elist
5828 then
5829 Set_Subprograms_For_Type
5830 (Id, Subprograms_For_Type (Full_View (T)));
5831 else
5832 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5833 end if;
5834
5835 -- If the current declaration created both a private and a full view,
5836 -- then propagate Predicate_Function to the latter as well.
5837
5838 if Present (Full_View (Id))
5839 and then No (Predicate_Function (Full_View (Id)))
5840 then
5841 Set_Subprograms_For_Type
5842 (Full_View (Id), Subprograms_For_Type (Id));
5843 end if;
5844
5845 if Has_Static_Predicate (T) then
5846 Set_Has_Static_Predicate (Id);
5847 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5848 end if;
5849 end if;
5850
5851 -- If the base type is a scalar type, or else if there is no
5852 -- constraint, the atomic flag is inherited by the subtype.
5853 -- Ditto for the Independent aspect.
5854
5855 if Is_Scalar_Type (Id)
5856 or else Is_Entity_Name (Subtype_Indication (N))
5857 then
5858 Set_Is_Atomic (Id, Is_Atomic (T));
5859 Set_Is_Independent (Id, Is_Independent (T));
5860 end if;
5861
5862 -- Remaining processing depends on characteristics of base type
5863
5864 T := Etype (Id);
5865
5866 Set_Is_Immediately_Visible (Id, True);
5867 Set_Depends_On_Private (Id, Has_Private_Component (T));
5868 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5869
5870 if Is_Interface (T) then
5871 Set_Is_Interface (Id);
5872 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5873 end if;
5874
5875 if Present (Generic_Parent_Type (N))
5876 and then
5877 (Nkind (Parent (Generic_Parent_Type (N))) /=
5878 N_Formal_Type_Declaration
5879 or else Nkind (Formal_Type_Definition
5880 (Parent (Generic_Parent_Type (N)))) /=
5881 N_Formal_Private_Type_Definition)
5882 then
5883 if Is_Tagged_Type (Id) then
5884
5885 -- If this is a generic actual subtype for a synchronized type,
5886 -- the primitive operations are those of the corresponding record
5887 -- for which there is a separate subtype declaration.
5888
5889 if Is_Concurrent_Type (Id) then
5890 null;
5891 elsif Is_Class_Wide_Type (Id) then
5892 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5893 else
5894 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5895 end if;
5896
5897 elsif Scope (Etype (Id)) /= Standard_Standard then
5898 Derive_Subprograms (Generic_Parent_Type (N), Id);
5899 end if;
5900 end if;
5901
5902 if Is_Private_Type (T) and then Present (Full_View (T)) then
5903 Conditional_Delay (Id, Full_View (T));
5904
5905 -- The subtypes of components or subcomponents of protected types
5906 -- do not need freeze nodes, which would otherwise appear in the
5907 -- wrong scope (before the freeze node for the protected type). The
5908 -- proper subtypes are those of the subcomponents of the corresponding
5909 -- record.
5910
5911 elsif Ekind (Scope (Id)) /= E_Protected_Type
5912 and then Present (Scope (Scope (Id))) -- error defense
5913 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5914 then
5915 Conditional_Delay (Id, T);
5916 end if;
5917
5918 -- If we have a subtype of an incomplete type whose full type is a
5919 -- derived numeric type, we need to have a freeze node for the subtype.
5920 -- Otherwise gigi will complain while computing the (static) bounds of
5921 -- the subtype.
5922
5923 if Is_Itype (T)
5924 and then Is_Elementary_Type (Id)
5925 and then Etype (Id) /= Id
5926 then
5927 declare
5928 Partial : constant Entity_Id :=
5929 Incomplete_Or_Partial_View (First_Subtype (Id));
5930 begin
5931 if Present (Partial)
5932 and then Ekind (Partial) = E_Incomplete_Type
5933 then
5934 Set_Has_Delayed_Freeze (Id);
5935 end if;
5936 end;
5937 end if;
5938
5939 -- Check that Constraint_Error is raised for a scalar subtype indication
5940 -- when the lower or upper bound of a non-null range lies outside the
5941 -- range of the type mark. Likewise for an array subtype, but check the
5942 -- compatibility for each index.
5943
5944 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5945 declare
5946 Indic_Typ : constant Entity_Id :=
5947 Etype (Subtype_Mark (Subtype_Indication (N)));
5948 Subt_Index : Node_Id;
5949 Target_Index : Node_Id;
5950
5951 begin
5952 if Is_Scalar_Type (Etype (Id))
5953 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
5954 then
5955 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
5956
5957 elsif Is_Array_Type (Etype (Id))
5958 and then Present (First_Index (Id))
5959 then
5960 Subt_Index := First_Index (Id);
5961 Target_Index := First_Index (Indic_Typ);
5962
5963 while Present (Subt_Index) loop
5964 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
5965 and then Is_Scalar_Type (Entity (Subt_Index)))
5966 or else Nkind (Subt_Index) = N_Subtype_Indication)
5967 and then
5968 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5969 then
5970 Apply_Range_Check
5971 (Scalar_Range (Etype (Subt_Index)),
5972 Etype (Target_Index),
5973 Insert_Node => N);
5974 end if;
5975
5976 Next_Index (Subt_Index);
5977 Next_Index (Target_Index);
5978 end loop;
5979 end if;
5980 end;
5981 end if;
5982
5983 Set_Optimize_Alignment_Flags (Id);
5984 Check_Eliminated (Id);
5985
5986 <<Leave>>
5987 if Has_Aspects (N) then
5988 Analyze_Aspect_Specifications (N, Id);
5989 end if;
5990
5991 Analyze_Dimension (N);
5992
5993 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5994 -- indications on composite types where the constraints are dynamic.
5995 -- Note that object declarations and aggregates generate implicit
5996 -- subtype declarations, which this covers. One special case is that the
5997 -- implicitly generated "=" for discriminated types includes an
5998 -- offending subtype declaration, which is harmless, so we ignore it
5999 -- here.
6000
6001 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6002 declare
6003 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6004 begin
6005 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6006 and then not (Is_Internal (Id)
6007 and then Is_TSS (Scope (Id),
6008 TSS_Composite_Equality))
6009 and then not Within_Init_Proc
6010 and then not All_Composite_Constraints_Static (Cstr)
6011 then
6012 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6013 end if;
6014 end;
6015 end if;
6016 end Analyze_Subtype_Declaration;
6017
6018 --------------------------------
6019 -- Analyze_Subtype_Indication --
6020 --------------------------------
6021
6022 procedure Analyze_Subtype_Indication (N : Node_Id) is
6023 T : constant Entity_Id := Subtype_Mark (N);
6024 R : constant Node_Id := Range_Expression (Constraint (N));
6025
6026 begin
6027 Analyze (T);
6028
6029 if R /= Error then
6030 Analyze (R);
6031 Set_Etype (N, Etype (R));
6032 Resolve (R, Entity (T));
6033 else
6034 Set_Error_Posted (R);
6035 Set_Error_Posted (T);
6036 end if;
6037 end Analyze_Subtype_Indication;
6038
6039 --------------------------
6040 -- Analyze_Variant_Part --
6041 --------------------------
6042
6043 procedure Analyze_Variant_Part (N : Node_Id) is
6044 Discr_Name : Node_Id;
6045 Discr_Type : Entity_Id;
6046
6047 procedure Process_Variant (A : Node_Id);
6048 -- Analyze declarations for a single variant
6049
6050 package Analyze_Variant_Choices is
6051 new Generic_Analyze_Choices (Process_Variant);
6052 use Analyze_Variant_Choices;
6053
6054 ---------------------
6055 -- Process_Variant --
6056 ---------------------
6057
6058 procedure Process_Variant (A : Node_Id) is
6059 CL : constant Node_Id := Component_List (A);
6060 begin
6061 if not Null_Present (CL) then
6062 Analyze_Declarations (Component_Items (CL));
6063
6064 if Present (Variant_Part (CL)) then
6065 Analyze (Variant_Part (CL));
6066 end if;
6067 end if;
6068 end Process_Variant;
6069
6070 -- Start of processing for Analyze_Variant_Part
6071
6072 begin
6073 Discr_Name := Name (N);
6074 Analyze (Discr_Name);
6075
6076 -- If Discr_Name bad, get out (prevent cascaded errors)
6077
6078 if Etype (Discr_Name) = Any_Type then
6079 return;
6080 end if;
6081
6082 -- Check invalid discriminant in variant part
6083
6084 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6085 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6086 end if;
6087
6088 Discr_Type := Etype (Entity (Discr_Name));
6089
6090 if not Is_Discrete_Type (Discr_Type) then
6091 Error_Msg_N
6092 ("discriminant in a variant part must be of a discrete type",
6093 Name (N));
6094 return;
6095 end if;
6096
6097 -- Now analyze the choices, which also analyzes the declarations that
6098 -- are associated with each choice.
6099
6100 Analyze_Choices (Variants (N), Discr_Type);
6101
6102 -- Note: we used to instantiate and call Check_Choices here to check
6103 -- that the choices covered the discriminant, but it's too early to do
6104 -- that because of statically predicated subtypes, whose analysis may
6105 -- be deferred to their freeze point which may be as late as the freeze
6106 -- point of the containing record. So this call is now to be found in
6107 -- Freeze_Record_Declaration.
6108
6109 end Analyze_Variant_Part;
6110
6111 ----------------------------
6112 -- Array_Type_Declaration --
6113 ----------------------------
6114
6115 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6116 Component_Def : constant Node_Id := Component_Definition (Def);
6117 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6118 P : constant Node_Id := Parent (Def);
6119 Element_Type : Entity_Id;
6120 Implicit_Base : Entity_Id;
6121 Index : Node_Id;
6122 Nb_Index : Pos;
6123 Priv : Entity_Id;
6124 Related_Id : Entity_Id;
6125 Has_FLB_Index : Boolean := False;
6126
6127 begin
6128 if Nkind (Def) = N_Constrained_Array_Definition then
6129 Index := First (Discrete_Subtype_Definitions (Def));
6130 else
6131 Index := First (Subtype_Marks (Def));
6132 end if;
6133
6134 -- Find proper names for the implicit types which may be public. In case
6135 -- of anonymous arrays we use the name of the first object of that type
6136 -- as prefix.
6137
6138 if No (T) then
6139 Related_Id := Defining_Identifier (P);
6140 else
6141 Related_Id := T;
6142 end if;
6143
6144 Nb_Index := 1;
6145 while Present (Index) loop
6146 Analyze (Index);
6147
6148 -- Test for odd case of trying to index a type by the type itself
6149
6150 if Is_Entity_Name (Index) and then Entity (Index) = T then
6151 Error_Msg_N ("type& cannot be indexed by itself", Index);
6152 Set_Entity (Index, Standard_Boolean);
6153 Set_Etype (Index, Standard_Boolean);
6154 end if;
6155
6156 -- Add a subtype declaration for each index of private array type
6157 -- declaration whose type is also private. For example:
6158
6159 -- package Pkg is
6160 -- type Index is private;
6161 -- private
6162 -- type Table is array (Index) of ...
6163 -- end;
6164
6165 -- This is currently required by the expander for the internally
6166 -- generated equality subprogram of records with variant parts in
6167 -- which the type of some component is such a private type. And it
6168 -- also helps semantic analysis in peculiar cases where the array
6169 -- type is referenced from an instance but not the index directly.
6170
6171 if Is_Package_Or_Generic_Package (Current_Scope)
6172 and then In_Private_Part (Current_Scope)
6173 and then Has_Private_Declaration (Etype (Index))
6174 and then Scope (Etype (Index)) = Current_Scope
6175 then
6176 declare
6177 Loc : constant Source_Ptr := Sloc (Def);
6178 Decl : Node_Id;
6179 New_E : Entity_Id;
6180
6181 begin
6182 New_E := Make_Temporary (Loc, 'T');
6183 Set_Is_Internal (New_E);
6184
6185 Decl :=
6186 Make_Subtype_Declaration (Loc,
6187 Defining_Identifier => New_E,
6188 Subtype_Indication =>
6189 New_Occurrence_Of (Etype (Index), Loc));
6190
6191 Insert_Before (Parent (Def), Decl);
6192 Analyze (Decl);
6193 Set_Etype (Index, New_E);
6194
6195 -- If the index is a range or a subtype indication it carries
6196 -- no entity. Example:
6197
6198 -- package Pkg is
6199 -- type T is private;
6200 -- private
6201 -- type T is new Natural;
6202 -- Table : array (T(1) .. T(10)) of Boolean;
6203 -- end Pkg;
6204
6205 -- Otherwise the type of the reference is its entity.
6206
6207 if Is_Entity_Name (Index) then
6208 Set_Entity (Index, New_E);
6209 end if;
6210 end;
6211 end if;
6212
6213 Make_Index (Index, P, Related_Id, Nb_Index);
6214
6215 -- In the case where we have an unconstrained array with an index
6216 -- given by a subtype_indication, this is necessarily a "fixed lower
6217 -- bound" index. We change the upper bound of that index to the upper
6218 -- bound of the index's subtype (denoted by the subtype_mark), since
6219 -- that upper bound was originally set by the parser to be the same
6220 -- as the lower bound. In truth, that upper bound corresponds to
6221 -- a box ("<>"), and could be set to Empty, but it's convenient to
6222 -- set it to the upper bound to avoid needing to add special tests
6223 -- in various places for an Empty upper bound, and in any case that
6224 -- accurately characterizes the index's range of values.
6225
6226 if Nkind (Def) = N_Unconstrained_Array_Definition
6227 and then Nkind (Index) = N_Subtype_Indication
6228 then
6229 declare
6230 Index_Subtype_High_Bound : constant Entity_Id :=
6231 Type_High_Bound (Entity (Subtype_Mark (Index)));
6232 begin
6233 Set_High_Bound (Range_Expression (Constraint (Index)),
6234 Index_Subtype_High_Bound);
6235
6236 -- Record that the array type has one or more indexes with
6237 -- a fixed lower bound.
6238
6239 Has_FLB_Index := True;
6240
6241 -- Mark the index as belonging to an array type with a fixed
6242 -- lower bound.
6243
6244 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6245 end;
6246 end if;
6247
6248 -- Check error of subtype with predicate for index type
6249
6250 Bad_Predicated_Subtype_Use
6251 ("subtype& has predicate, not allowed as index subtype",
6252 Index, Etype (Index));
6253
6254 -- Move to next index
6255
6256 Next_Index (Index);
6257 Nb_Index := Nb_Index + 1;
6258 end loop;
6259
6260 -- Process subtype indication if one is present
6261
6262 if Present (Component_Typ) then
6263 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6264 Set_Etype (Component_Typ, Element_Type);
6265
6266 -- Ada 2005 (AI-230): Access Definition case
6267
6268 else pragma Assert (Present (Access_Definition (Component_Def)));
6269
6270 -- Indicate that the anonymous access type is created by the
6271 -- array type declaration.
6272
6273 Element_Type := Access_Definition
6274 (Related_Nod => P,
6275 N => Access_Definition (Component_Def));
6276 Set_Is_Local_Anonymous_Access (Element_Type);
6277
6278 -- Propagate the parent. This field is needed if we have to generate
6279 -- the master_id associated with an anonymous access to task type
6280 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6281
6282 Copy_Parent (To => Element_Type, From => T);
6283
6284 -- Ada 2005 (AI-230): In case of components that are anonymous access
6285 -- types the level of accessibility depends on the enclosing type
6286 -- declaration
6287
6288 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6289
6290 -- Ada 2005 (AI-254)
6291
6292 declare
6293 CD : constant Node_Id :=
6294 Access_To_Subprogram_Definition
6295 (Access_Definition (Component_Def));
6296 begin
6297 if Present (CD) and then Protected_Present (CD) then
6298 Element_Type :=
6299 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6300 end if;
6301 end;
6302 end if;
6303
6304 -- Constrained array case
6305
6306 if No (T) then
6307 -- We might be creating more than one itype with the same Related_Id,
6308 -- e.g. for an array object definition and its initial value. Give
6309 -- them unique suffixes, because GNATprove require distinct types to
6310 -- have different names.
6311
6312 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6313 end if;
6314
6315 if Nkind (Def) = N_Constrained_Array_Definition then
6316
6317 if Ekind (T) in Incomplete_Or_Private_Kind then
6318 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6319 else
6320 pragma Assert (Ekind (T) = E_Void);
6321 end if;
6322
6323 -- Establish Implicit_Base as unconstrained base type
6324
6325 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6326
6327 Set_Etype (Implicit_Base, Implicit_Base);
6328 Set_Scope (Implicit_Base, Current_Scope);
6329 Set_Has_Delayed_Freeze (Implicit_Base);
6330 Set_Default_SSO (Implicit_Base);
6331
6332 -- The constrained array type is a subtype of the unconstrained one
6333
6334 Mutate_Ekind (T, E_Array_Subtype);
6335 Reinit_Size_Align (T);
6336 Set_Etype (T, Implicit_Base);
6337 Set_Scope (T, Current_Scope);
6338 Set_Is_Constrained (T);
6339 Set_First_Index (T,
6340 First (Discrete_Subtype_Definitions (Def)));
6341 Set_Has_Delayed_Freeze (T);
6342
6343 -- Complete setup of implicit base type
6344
6345 Set_Component_Size (Implicit_Base, Uint_0);
6346 Set_Component_Type (Implicit_Base, Element_Type);
6347 Set_Finalize_Storage_Only
6348 (Implicit_Base,
6349 Finalize_Storage_Only (Element_Type));
6350 Set_First_Index (Implicit_Base, First_Index (T));
6351 Set_Has_Controlled_Component
6352 (Implicit_Base,
6353 Has_Controlled_Component (Element_Type)
6354 or else Is_Controlled (Element_Type));
6355 Set_Packed_Array_Impl_Type
6356 (Implicit_Base, Empty);
6357
6358 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6359
6360 -- Unconstrained array case
6361
6362 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6363
6364 if Ekind (T) in Incomplete_Or_Private_Kind then
6365 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6366 else
6367 pragma Assert (Ekind (T) = E_Void);
6368 end if;
6369
6370 Mutate_Ekind (T, E_Array_Type);
6371 Reinit_Size_Align (T);
6372 Set_Etype (T, T);
6373 Set_Scope (T, Current_Scope);
6374 Set_Component_Size (T, Uint_0);
6375 Set_Is_Constrained (T, False);
6376 Set_Is_Fixed_Lower_Bound_Array_Subtype
6377 (T, Has_FLB_Index);
6378 Set_First_Index (T, First (Subtype_Marks (Def)));
6379 Set_Has_Delayed_Freeze (T, True);
6380 Propagate_Concurrent_Flags (T, Element_Type);
6381 Set_Has_Controlled_Component (T, Has_Controlled_Component
6382 (Element_Type)
6383 or else
6384 Is_Controlled (Element_Type));
6385 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6386 (Element_Type));
6387 Set_Default_SSO (T);
6388 end if;
6389
6390 -- Common attributes for both cases
6391
6392 Set_Component_Type (Base_Type (T), Element_Type);
6393 Set_Packed_Array_Impl_Type (T, Empty);
6394
6395 if Aliased_Present (Component_Definition (Def)) then
6396 Set_Has_Aliased_Components (Etype (T));
6397
6398 -- AI12-001: All aliased objects are considered to be specified as
6399 -- independently addressable (RM C.6(8.1/4)).
6400
6401 Set_Has_Independent_Components (Etype (T));
6402 end if;
6403
6404 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6405 -- array type to ensure that objects of this type are initialized.
6406
6407 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6408 Set_Can_Never_Be_Null (T);
6409
6410 if Null_Exclusion_Present (Component_Definition (Def))
6411
6412 -- No need to check itypes because in their case this check was
6413 -- done at their point of creation
6414
6415 and then not Is_Itype (Element_Type)
6416 then
6417 Error_Msg_N
6418 ("`NOT NULL` not allowed (null already excluded)",
6419 Subtype_Indication (Component_Definition (Def)));
6420 end if;
6421 end if;
6422
6423 Priv := Private_Component (Element_Type);
6424
6425 if Present (Priv) then
6426
6427 -- Check for circular definitions
6428
6429 if Priv = Any_Type then
6430 Set_Component_Type (Etype (T), Any_Type);
6431
6432 -- There is a gap in the visibility of operations on the composite
6433 -- type only if the component type is defined in a different scope.
6434
6435 elsif Scope (Priv) = Current_Scope then
6436 null;
6437
6438 elsif Is_Limited_Type (Priv) then
6439 Set_Is_Limited_Composite (Etype (T));
6440 Set_Is_Limited_Composite (T);
6441 else
6442 Set_Is_Private_Composite (Etype (T));
6443 Set_Is_Private_Composite (T);
6444 end if;
6445 end if;
6446
6447 -- A syntax error in the declaration itself may lead to an empty index
6448 -- list, in which case do a minimal patch.
6449
6450 if No (First_Index (T)) then
6451 Error_Msg_N ("missing index definition in array type declaration", T);
6452
6453 declare
6454 Indexes : constant List_Id :=
6455 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6456 begin
6457 Set_Discrete_Subtype_Definitions (Def, Indexes);
6458 Set_First_Index (T, First (Indexes));
6459 return;
6460 end;
6461 end if;
6462
6463 -- Create a concatenation operator for the new type. Internal array
6464 -- types created for packed entities do not need such, they are
6465 -- compatible with the user-defined type.
6466
6467 if Number_Dimensions (T) = 1
6468 and then not Is_Packed_Array_Impl_Type (T)
6469 then
6470 New_Concatenation_Op (T);
6471 end if;
6472
6473 -- In the case of an unconstrained array the parser has already verified
6474 -- that all the indexes are unconstrained but we still need to make sure
6475 -- that the element type is constrained.
6476
6477 if not Is_Definite_Subtype (Element_Type) then
6478 Error_Msg_N
6479 ("unconstrained element type in array declaration",
6480 Subtype_Indication (Component_Def));
6481
6482 elsif Is_Abstract_Type (Element_Type) then
6483 Error_Msg_N
6484 ("the type of a component cannot be abstract",
6485 Subtype_Indication (Component_Def));
6486 end if;
6487
6488 -- There may be an invariant declared for the component type, but
6489 -- the construction of the component invariant checking procedure
6490 -- takes place during expansion.
6491 end Array_Type_Declaration;
6492
6493 ------------------------------------------------------
6494 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6495 ------------------------------------------------------
6496
6497 function Replace_Anonymous_Access_To_Protected_Subprogram
6498 (N : Node_Id) return Entity_Id
6499 is
6500 Loc : constant Source_Ptr := Sloc (N);
6501
6502 Curr_Scope : constant Scope_Stack_Entry :=
6503 Scope_Stack.Table (Scope_Stack.Last);
6504
6505 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6506
6507 Acc : Node_Id;
6508 -- Access definition in declaration
6509
6510 Comp : Node_Id;
6511 -- Object definition or formal definition with an access definition
6512
6513 Decl : Node_Id;
6514 -- Declaration of anonymous access to subprogram type
6515
6516 Spec : Node_Id;
6517 -- Original specification in access to subprogram
6518
6519 P : Node_Id;
6520
6521 begin
6522 Set_Is_Internal (Anon);
6523
6524 case Nkind (N) is
6525 when N_Constrained_Array_Definition
6526 | N_Component_Declaration
6527 | N_Unconstrained_Array_Definition
6528 =>
6529 Comp := Component_Definition (N);
6530 Acc := Access_Definition (Comp);
6531
6532 when N_Discriminant_Specification =>
6533 Comp := Discriminant_Type (N);
6534 Acc := Comp;
6535
6536 when N_Parameter_Specification =>
6537 Comp := Parameter_Type (N);
6538 Acc := Comp;
6539
6540 when N_Access_Function_Definition =>
6541 Comp := Result_Definition (N);
6542 Acc := Comp;
6543
6544 when N_Object_Declaration =>
6545 Comp := Object_Definition (N);
6546 Acc := Comp;
6547
6548 when N_Function_Specification =>
6549 Comp := Result_Definition (N);
6550 Acc := Comp;
6551
6552 when others =>
6553 raise Program_Error;
6554 end case;
6555
6556 Spec := Access_To_Subprogram_Definition (Acc);
6557
6558 Decl :=
6559 Make_Full_Type_Declaration (Loc,
6560 Defining_Identifier => Anon,
6561 Type_Definition => Copy_Separate_Tree (Spec));
6562
6563 Mark_Rewrite_Insertion (Decl);
6564
6565 -- Insert the new declaration in the nearest enclosing scope. If the
6566 -- parent is a body and N is its return type, the declaration belongs
6567 -- in the enclosing scope. Likewise if N is the type of a parameter.
6568
6569 P := Parent (N);
6570
6571 if Nkind (N) = N_Function_Specification
6572 and then Nkind (P) = N_Subprogram_Body
6573 then
6574 P := Parent (P);
6575 elsif Nkind (N) = N_Parameter_Specification
6576 and then Nkind (P) in N_Subprogram_Specification
6577 and then Nkind (Parent (P)) = N_Subprogram_Body
6578 then
6579 P := Parent (Parent (P));
6580 end if;
6581
6582 while Present (P) and then not Has_Declarations (P) loop
6583 P := Parent (P);
6584 end loop;
6585
6586 pragma Assert (Present (P));
6587
6588 if Nkind (P) = N_Package_Specification then
6589 Prepend (Decl, Visible_Declarations (P));
6590 else
6591 Prepend (Decl, Declarations (P));
6592 end if;
6593
6594 -- Replace the anonymous type with an occurrence of the new declaration.
6595 -- In all cases the rewritten node does not have the null-exclusion
6596 -- attribute because (if present) it was already inherited by the
6597 -- anonymous entity (Anon). Thus, in case of components we do not
6598 -- inherit this attribute.
6599
6600 if Nkind (N) = N_Parameter_Specification then
6601 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6602 Set_Etype (Defining_Identifier (N), Anon);
6603 Set_Null_Exclusion_Present (N, False);
6604
6605 elsif Nkind (N) = N_Object_Declaration then
6606 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6607 Set_Etype (Defining_Identifier (N), Anon);
6608
6609 elsif Nkind (N) = N_Access_Function_Definition then
6610 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6611
6612 elsif Nkind (N) = N_Function_Specification then
6613 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6614 Set_Etype (Defining_Unit_Name (N), Anon);
6615
6616 else
6617 Rewrite (Comp,
6618 Make_Component_Definition (Loc,
6619 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6620 end if;
6621
6622 Mark_Rewrite_Insertion (Comp);
6623
6624 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6625 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6626 and then not Is_Type (Current_Scope))
6627 then
6628
6629 -- Declaration can be analyzed in the current scope.
6630
6631 Analyze (Decl);
6632
6633 else
6634 -- Temporarily remove the current scope (record or subprogram) from
6635 -- the stack to add the new declarations to the enclosing scope.
6636 -- The anonymous entity is an Itype with the proper attributes.
6637
6638 Scope_Stack.Decrement_Last;
6639 Analyze (Decl);
6640 Set_Is_Itype (Anon);
6641 Set_Associated_Node_For_Itype (Anon, N);
6642 Scope_Stack.Append (Curr_Scope);
6643 end if;
6644
6645 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6646 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6647 return Anon;
6648 end Replace_Anonymous_Access_To_Protected_Subprogram;
6649
6650 -------------------------------------
6651 -- Build_Access_Subprogram_Wrapper --
6652 -------------------------------------
6653
6654 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6655 Loc : constant Source_Ptr := Sloc (Decl);
6656 Id : constant Entity_Id := Defining_Identifier (Decl);
6657 Type_Def : constant Node_Id := Type_Definition (Decl);
6658 Specs : constant List_Id :=
6659 Parameter_Specifications (Type_Def);
6660 Profile : constant List_Id := New_List;
6661 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6662
6663 Contracts : constant List_Id := New_List;
6664 Form_P : Node_Id;
6665 New_P : Node_Id;
6666 New_Decl : Node_Id;
6667 Spec : Node_Id;
6668
6669 procedure Replace_Type_Name (Expr : Node_Id);
6670 -- In the expressions for contract aspects, replace occurrences of the
6671 -- access type with the name of the subprogram entity, as needed, e.g.
6672 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6673 -- remain on the original access type declaration. What about expanded
6674 -- names denoting formals, whose prefix in source is the type name ???
6675
6676 -----------------------
6677 -- Replace_Type_Name --
6678 -----------------------
6679
6680 procedure Replace_Type_Name (Expr : Node_Id) is
6681 function Process (N : Node_Id) return Traverse_Result;
6682 function Process (N : Node_Id) return Traverse_Result is
6683 begin
6684 if Nkind (N) = N_Attribute_Reference
6685 and then Is_Entity_Name (Prefix (N))
6686 and then Chars (Prefix (N)) = Chars (Id)
6687 then
6688 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6689 end if;
6690
6691 return OK;
6692 end Process;
6693
6694 procedure Traverse is new Traverse_Proc (Process);
6695 begin
6696 Traverse (Expr);
6697 end Replace_Type_Name;
6698
6699 begin
6700 if Ekind (Id) in E_Access_Subprogram_Type
6701 | E_Access_Protected_Subprogram_Type
6702 | E_Anonymous_Access_Protected_Subprogram_Type
6703 | E_Anonymous_Access_Subprogram_Type
6704 then
6705 null;
6706
6707 else
6708 Error_Msg_N
6709 ("illegal pre/postcondition on access type", Decl);
6710 return;
6711 end if;
6712
6713 declare
6714 Asp : Node_Id;
6715 A_Id : Aspect_Id;
6716 Cond : Node_Id;
6717 Expr : Node_Id;
6718
6719 begin
6720 Asp := First (Aspect_Specifications (Decl));
6721 while Present (Asp) loop
6722 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6723 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6724 Cond := Asp;
6725 Expr := Expression (Cond);
6726 Replace_Type_Name (Expr);
6727 Next (Asp);
6728
6729 Remove (Cond);
6730 Append (Cond, Contracts);
6731
6732 else
6733 Next (Asp);
6734 end if;
6735 end loop;
6736 end;
6737
6738 -- If there are no contract aspects, no need for a wrapper.
6739
6740 if Is_Empty_List (Contracts) then
6741 return;
6742 end if;
6743
6744 Form_P := First (Specs);
6745
6746 while Present (Form_P) loop
6747 New_P := New_Copy_Tree (Form_P);
6748 Set_Defining_Identifier (New_P,
6749 Make_Defining_Identifier
6750 (Loc, Chars (Defining_Identifier (Form_P))));
6751 Append (New_P, Profile);
6752 Next (Form_P);
6753 end loop;
6754
6755 -- Add to parameter specifications the access parameter that is passed
6756 -- in from an indirect call.
6757
6758 Append (
6759 Make_Parameter_Specification (Loc,
6760 Defining_Identifier => Make_Temporary (Loc, 'P'),
6761 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6762 Profile);
6763
6764 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6765 Spec :=
6766 Make_Procedure_Specification (Loc,
6767 Defining_Unit_Name => Subp,
6768 Parameter_Specifications => Profile);
6769 else
6770 Spec :=
6771 Make_Function_Specification (Loc,
6772 Defining_Unit_Name => Subp,
6773 Parameter_Specifications => Profile,
6774 Result_Definition =>
6775 New_Copy_Tree
6776 (Result_Definition (Type_Definition (Decl))));
6777 end if;
6778
6779 New_Decl :=
6780 Make_Subprogram_Declaration (Loc, Specification => Spec);
6781 Set_Aspect_Specifications (New_Decl, Contracts);
6782
6783 Insert_After (Decl, New_Decl);
6784 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6785 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6786 end Build_Access_Subprogram_Wrapper;
6787
6788 -------------------------------
6789 -- Build_Derived_Access_Type --
6790 -------------------------------
6791
6792 procedure Build_Derived_Access_Type
6793 (N : Node_Id;
6794 Parent_Type : Entity_Id;
6795 Derived_Type : Entity_Id)
6796 is
6797 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6798
6799 Desig_Type : Entity_Id;
6800 Discr : Entity_Id;
6801 Discr_Con_Elist : Elist_Id;
6802 Discr_Con_El : Elmt_Id;
6803 Subt : Entity_Id;
6804
6805 begin
6806 -- Set the designated type so it is available in case this is an access
6807 -- to a self-referential type, e.g. a standard list type with a next
6808 -- pointer. Will be reset after subtype is built.
6809
6810 Set_Directly_Designated_Type
6811 (Derived_Type, Designated_Type (Parent_Type));
6812
6813 Subt := Process_Subtype (S, N);
6814
6815 if Nkind (S) /= N_Subtype_Indication
6816 and then Subt /= Base_Type (Subt)
6817 then
6818 Mutate_Ekind (Derived_Type, E_Access_Subtype);
6819 end if;
6820
6821 if Ekind (Derived_Type) = E_Access_Subtype then
6822 declare
6823 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6824 Ibase : constant Entity_Id :=
6825 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6826 Svg_Chars : constant Name_Id := Chars (Ibase);
6827 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6828 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6829
6830 begin
6831 Copy_Node (Pbase, Ibase);
6832
6833 -- Restore Itype status after Copy_Node
6834
6835 Set_Is_Itype (Ibase);
6836 Set_Associated_Node_For_Itype (Ibase, N);
6837
6838 Set_Chars (Ibase, Svg_Chars);
6839 Set_Prev_Entity (Ibase, Svg_Prev_E);
6840 Set_Next_Entity (Ibase, Svg_Next_E);
6841 Set_Sloc (Ibase, Sloc (Derived_Type));
6842 Set_Scope (Ibase, Scope (Derived_Type));
6843 Set_Freeze_Node (Ibase, Empty);
6844 Set_Is_Frozen (Ibase, False);
6845 Set_Comes_From_Source (Ibase, False);
6846 Set_Is_First_Subtype (Ibase, False);
6847
6848 Set_Etype (Ibase, Pbase);
6849 Set_Etype (Derived_Type, Ibase);
6850 end;
6851 end if;
6852
6853 Set_Directly_Designated_Type
6854 (Derived_Type, Designated_Type (Subt));
6855
6856 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6857 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6858 Set_Size_Info (Derived_Type, Parent_Type);
6859 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
6860 Set_Depends_On_Private (Derived_Type,
6861 Has_Private_Component (Derived_Type));
6862 Conditional_Delay (Derived_Type, Subt);
6863
6864 if Is_Access_Subprogram_Type (Derived_Type)
6865 and then Is_Base_Type (Derived_Type)
6866 then
6867 Set_Can_Use_Internal_Rep
6868 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6869 end if;
6870
6871 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6872 -- that it is not redundant.
6873
6874 if Null_Exclusion_Present (Type_Definition (N)) then
6875 Set_Can_Never_Be_Null (Derived_Type);
6876
6877 elsif Can_Never_Be_Null (Parent_Type) then
6878 Set_Can_Never_Be_Null (Derived_Type);
6879 end if;
6880
6881 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6882 -- the root type for this information.
6883
6884 -- Apply range checks to discriminants for derived record case
6885 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6886
6887 Desig_Type := Designated_Type (Derived_Type);
6888
6889 if Is_Composite_Type (Desig_Type)
6890 and then (not Is_Array_Type (Desig_Type))
6891 and then Has_Discriminants (Desig_Type)
6892 and then Base_Type (Desig_Type) /= Desig_Type
6893 then
6894 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6895 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6896
6897 Discr := First_Discriminant (Base_Type (Desig_Type));
6898 while Present (Discr_Con_El) loop
6899 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6900 Next_Elmt (Discr_Con_El);
6901 Next_Discriminant (Discr);
6902 end loop;
6903 end if;
6904 end Build_Derived_Access_Type;
6905
6906 ------------------------------
6907 -- Build_Derived_Array_Type --
6908 ------------------------------
6909
6910 procedure Build_Derived_Array_Type
6911 (N : Node_Id;
6912 Parent_Type : Entity_Id;
6913 Derived_Type : Entity_Id)
6914 is
6915 Loc : constant Source_Ptr := Sloc (N);
6916 Tdef : constant Node_Id := Type_Definition (N);
6917 Indic : constant Node_Id := Subtype_Indication (Tdef);
6918 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6919 Implicit_Base : Entity_Id := Empty;
6920 New_Indic : Node_Id;
6921
6922 procedure Make_Implicit_Base;
6923 -- If the parent subtype is constrained, the derived type is a subtype
6924 -- of an implicit base type derived from the parent base.
6925
6926 ------------------------
6927 -- Make_Implicit_Base --
6928 ------------------------
6929
6930 procedure Make_Implicit_Base is
6931 begin
6932 Implicit_Base :=
6933 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6934
6935 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
6936 Set_Etype (Implicit_Base, Parent_Base);
6937
6938 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6939 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6940
6941 Set_Has_Delayed_Freeze (Implicit_Base, True);
6942 end Make_Implicit_Base;
6943
6944 -- Start of processing for Build_Derived_Array_Type
6945
6946 begin
6947 if not Is_Constrained (Parent_Type) then
6948 if Nkind (Indic) /= N_Subtype_Indication then
6949 Mutate_Ekind (Derived_Type, E_Array_Type);
6950
6951 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6952 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6953
6954 Set_Has_Delayed_Freeze (Derived_Type, True);
6955
6956 else
6957 Make_Implicit_Base;
6958 Set_Etype (Derived_Type, Implicit_Base);
6959
6960 New_Indic :=
6961 Make_Subtype_Declaration (Loc,
6962 Defining_Identifier => Derived_Type,
6963 Subtype_Indication =>
6964 Make_Subtype_Indication (Loc,
6965 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6966 Constraint => Constraint (Indic)));
6967
6968 Rewrite (N, New_Indic);
6969 Analyze (N);
6970 end if;
6971
6972 else
6973 if Nkind (Indic) /= N_Subtype_Indication then
6974 Make_Implicit_Base;
6975
6976 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
6977 Set_Etype (Derived_Type, Implicit_Base);
6978 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6979
6980 else
6981 Error_Msg_N ("illegal constraint on constrained type", Indic);
6982 end if;
6983 end if;
6984
6985 -- If parent type is not a derived type itself, and is declared in
6986 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6987 -- the new type's concatenation operator since Derive_Subprograms
6988 -- will not inherit the parent's operator. If the parent type is
6989 -- unconstrained, the operator is of the unconstrained base type.
6990
6991 if Number_Dimensions (Parent_Type) = 1
6992 and then not Is_Limited_Type (Parent_Type)
6993 and then not Is_Derived_Type (Parent_Type)
6994 and then not Is_Package_Or_Generic_Package
6995 (Scope (Base_Type (Parent_Type)))
6996 then
6997 if not Is_Constrained (Parent_Type)
6998 and then Is_Constrained (Derived_Type)
6999 then
7000 New_Concatenation_Op (Implicit_Base);
7001 else
7002 New_Concatenation_Op (Derived_Type);
7003 end if;
7004 end if;
7005 end Build_Derived_Array_Type;
7006
7007 -----------------------------------
7008 -- Build_Derived_Concurrent_Type --
7009 -----------------------------------
7010
7011 procedure Build_Derived_Concurrent_Type
7012 (N : Node_Id;
7013 Parent_Type : Entity_Id;
7014 Derived_Type : Entity_Id)
7015 is
7016 Loc : constant Source_Ptr := Sloc (N);
7017 Def : constant Node_Id := Type_Definition (N);
7018 Indic : constant Node_Id := Subtype_Indication (Def);
7019
7020 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7021 Corr_Decl : Node_Id;
7022 Corr_Decl_Needed : Boolean;
7023 -- If the derived type has fewer discriminants than its parent, the
7024 -- corresponding record is also a derived type, in order to account for
7025 -- the bound discriminants. We create a full type declaration for it in
7026 -- this case.
7027
7028 Constraint_Present : constant Boolean :=
7029 Nkind (Indic) = N_Subtype_Indication;
7030
7031 D_Constraint : Node_Id;
7032 New_Constraint : Elist_Id := No_Elist;
7033 Old_Disc : Entity_Id;
7034 New_Disc : Entity_Id;
7035 New_N : Node_Id;
7036
7037 begin
7038 Set_Stored_Constraint (Derived_Type, No_Elist);
7039 Corr_Decl_Needed := False;
7040 Old_Disc := Empty;
7041
7042 if Present (Discriminant_Specifications (N))
7043 and then Constraint_Present
7044 then
7045 Old_Disc := First_Discriminant (Parent_Type);
7046 New_Disc := First (Discriminant_Specifications (N));
7047 while Present (New_Disc) and then Present (Old_Disc) loop
7048 Next_Discriminant (Old_Disc);
7049 Next (New_Disc);
7050 end loop;
7051 end if;
7052
7053 if Present (Old_Disc) and then Expander_Active then
7054
7055 -- The new type has fewer discriminants, so we need to create a new
7056 -- corresponding record, which is derived from the corresponding
7057 -- record of the parent, and has a stored constraint that captures
7058 -- the values of the discriminant constraints. The corresponding
7059 -- record is needed only if expander is active and code generation is
7060 -- enabled.
7061
7062 -- The type declaration for the derived corresponding record has the
7063 -- same discriminant part and constraints as the current declaration.
7064 -- Copy the unanalyzed tree to build declaration.
7065
7066 Corr_Decl_Needed := True;
7067 New_N := Copy_Separate_Tree (N);
7068
7069 Corr_Decl :=
7070 Make_Full_Type_Declaration (Loc,
7071 Defining_Identifier => Corr_Record,
7072 Discriminant_Specifications =>
7073 Discriminant_Specifications (New_N),
7074 Type_Definition =>
7075 Make_Derived_Type_Definition (Loc,
7076 Subtype_Indication =>
7077 Make_Subtype_Indication (Loc,
7078 Subtype_Mark =>
7079 New_Occurrence_Of
7080 (Corresponding_Record_Type (Parent_Type), Loc),
7081 Constraint =>
7082 Constraint
7083 (Subtype_Indication (Type_Definition (New_N))))));
7084 end if;
7085
7086 -- Copy Storage_Size and Relative_Deadline variables if task case
7087
7088 if Is_Task_Type (Parent_Type) then
7089 Set_Storage_Size_Variable (Derived_Type,
7090 Storage_Size_Variable (Parent_Type));
7091 Set_Relative_Deadline_Variable (Derived_Type,
7092 Relative_Deadline_Variable (Parent_Type));
7093 end if;
7094
7095 if Present (Discriminant_Specifications (N)) then
7096 Push_Scope (Derived_Type);
7097 Check_Or_Process_Discriminants (N, Derived_Type);
7098
7099 if Constraint_Present then
7100 New_Constraint :=
7101 Expand_To_Stored_Constraint
7102 (Parent_Type,
7103 Build_Discriminant_Constraints
7104 (Parent_Type, Indic, True));
7105 end if;
7106
7107 End_Scope;
7108
7109 elsif Constraint_Present then
7110
7111 -- Build an unconstrained derived type and rewrite the derived type
7112 -- as a subtype of this new base type.
7113
7114 declare
7115 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7116 New_Base : Entity_Id;
7117 New_Decl : Node_Id;
7118 New_Indic : Node_Id;
7119
7120 begin
7121 New_Base :=
7122 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7123
7124 New_Decl :=
7125 Make_Full_Type_Declaration (Loc,
7126 Defining_Identifier => New_Base,
7127 Type_Definition =>
7128 Make_Derived_Type_Definition (Loc,
7129 Abstract_Present => Abstract_Present (Def),
7130 Limited_Present => Limited_Present (Def),
7131 Subtype_Indication =>
7132 New_Occurrence_Of (Parent_Base, Loc)));
7133
7134 Mark_Rewrite_Insertion (New_Decl);
7135 Insert_Before (N, New_Decl);
7136 Analyze (New_Decl);
7137
7138 New_Indic :=
7139 Make_Subtype_Indication (Loc,
7140 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7141 Constraint => Relocate_Node (Constraint (Indic)));
7142
7143 Rewrite (N,
7144 Make_Subtype_Declaration (Loc,
7145 Defining_Identifier => Derived_Type,
7146 Subtype_Indication => New_Indic));
7147
7148 Analyze (N);
7149 return;
7150 end;
7151 end if;
7152
7153 -- By default, operations and private data are inherited from parent.
7154 -- However, in the presence of bound discriminants, a new corresponding
7155 -- record will be created, see below.
7156
7157 Set_Has_Discriminants
7158 (Derived_Type, Has_Discriminants (Parent_Type));
7159 Set_Corresponding_Record_Type
7160 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7161
7162 -- Is_Constrained is set according the parent subtype, but is set to
7163 -- False if the derived type is declared with new discriminants.
7164
7165 Set_Is_Constrained
7166 (Derived_Type,
7167 (Is_Constrained (Parent_Type) or else Constraint_Present)
7168 and then not Present (Discriminant_Specifications (N)));
7169
7170 if Constraint_Present then
7171 if not Has_Discriminants (Parent_Type) then
7172 Error_Msg_N ("untagged parent must have discriminants", N);
7173
7174 elsif Present (Discriminant_Specifications (N)) then
7175
7176 -- Verify that new discriminants are used to constrain old ones
7177
7178 D_Constraint := First (Constraints (Constraint (Indic)));
7179
7180 Old_Disc := First_Discriminant (Parent_Type);
7181
7182 while Present (D_Constraint) loop
7183 if Nkind (D_Constraint) /= N_Discriminant_Association then
7184
7185 -- Positional constraint. If it is a reference to a new
7186 -- discriminant, it constrains the corresponding old one.
7187
7188 if Nkind (D_Constraint) = N_Identifier then
7189 New_Disc := First_Discriminant (Derived_Type);
7190 while Present (New_Disc) loop
7191 exit when Chars (New_Disc) = Chars (D_Constraint);
7192 Next_Discriminant (New_Disc);
7193 end loop;
7194
7195 if Present (New_Disc) then
7196 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7197 end if;
7198 end if;
7199
7200 Next_Discriminant (Old_Disc);
7201
7202 -- if this is a named constraint, search by name for the old
7203 -- discriminants constrained by the new one.
7204
7205 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7206
7207 -- Find new discriminant with that name
7208
7209 New_Disc := First_Discriminant (Derived_Type);
7210 while Present (New_Disc) loop
7211 exit when
7212 Chars (New_Disc) = Chars (Expression (D_Constraint));
7213 Next_Discriminant (New_Disc);
7214 end loop;
7215
7216 if Present (New_Disc) then
7217
7218 -- Verify that new discriminant renames some discriminant
7219 -- of the parent type, and associate the new discriminant
7220 -- with one or more old ones that it renames.
7221
7222 declare
7223 Selector : Node_Id;
7224
7225 begin
7226 Selector := First (Selector_Names (D_Constraint));
7227 while Present (Selector) loop
7228 Old_Disc := First_Discriminant (Parent_Type);
7229 while Present (Old_Disc) loop
7230 exit when Chars (Old_Disc) = Chars (Selector);
7231 Next_Discriminant (Old_Disc);
7232 end loop;
7233
7234 if Present (Old_Disc) then
7235 Set_Corresponding_Discriminant
7236 (New_Disc, Old_Disc);
7237 end if;
7238
7239 Next (Selector);
7240 end loop;
7241 end;
7242 end if;
7243 end if;
7244
7245 Next (D_Constraint);
7246 end loop;
7247
7248 New_Disc := First_Discriminant (Derived_Type);
7249 while Present (New_Disc) loop
7250 if No (Corresponding_Discriminant (New_Disc)) then
7251 Error_Msg_NE
7252 ("new discriminant& must constrain old one", N, New_Disc);
7253
7254 -- If a new discriminant is used in the constraint, then its
7255 -- subtype must be statically compatible with the subtype of
7256 -- the parent discriminant (RM 3.7(15)).
7257
7258 else
7259 Check_Constraining_Discriminant
7260 (New_Disc, Corresponding_Discriminant (New_Disc));
7261 end if;
7262
7263 Next_Discriminant (New_Disc);
7264 end loop;
7265 end if;
7266
7267 elsif Present (Discriminant_Specifications (N)) then
7268 Error_Msg_N
7269 ("missing discriminant constraint in untagged derivation", N);
7270 end if;
7271
7272 -- The entity chain of the derived type includes the new discriminants
7273 -- but shares operations with the parent.
7274
7275 if Present (Discriminant_Specifications (N)) then
7276 Old_Disc := First_Discriminant (Parent_Type);
7277 while Present (Old_Disc) loop
7278 if No (Next_Entity (Old_Disc))
7279 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7280 then
7281 Link_Entities
7282 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7283 exit;
7284 end if;
7285
7286 Next_Discriminant (Old_Disc);
7287 end loop;
7288
7289 else
7290 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7291 if Has_Discriminants (Parent_Type) then
7292 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7293 Set_Discriminant_Constraint (
7294 Derived_Type, Discriminant_Constraint (Parent_Type));
7295 end if;
7296 end if;
7297
7298 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7299
7300 Set_Has_Completion (Derived_Type);
7301
7302 if Corr_Decl_Needed then
7303 Set_Stored_Constraint (Derived_Type, New_Constraint);
7304 Insert_After (N, Corr_Decl);
7305 Analyze (Corr_Decl);
7306 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7307 end if;
7308 end Build_Derived_Concurrent_Type;
7309
7310 ------------------------------------
7311 -- Build_Derived_Enumeration_Type --
7312 ------------------------------------
7313
7314 procedure Build_Derived_Enumeration_Type
7315 (N : Node_Id;
7316 Parent_Type : Entity_Id;
7317 Derived_Type : Entity_Id)
7318 is
7319 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7320 -- When the type declaration includes a constraint, we generate
7321 -- a subtype declaration of an anonymous base type, with the constraint
7322 -- given in the original type declaration. Conceptually, the bounds
7323 -- are converted to the new base type, and this conversion freezes
7324 -- (prematurely) that base type, when the bounds are simply literals.
7325 -- As a result, a representation clause for the derived type is then
7326 -- rejected or ignored. This procedure recognizes the simple case of
7327 -- literal bounds, which allows us to indicate that the conversions
7328 -- are not freeze points, and the subsequent representation clause
7329 -- can be accepted.
7330 -- A similar approach might be used to resolve the long-standing
7331 -- problem of premature freezing of derived numeric types ???
7332
7333 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7334 begin
7335 return Nkind (B) = N_Type_Conversion
7336 and then Is_Entity_Name (Expression (B))
7337 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7338 end Bound_Belongs_To_Type;
7339
7340 Loc : constant Source_Ptr := Sloc (N);
7341 Def : constant Node_Id := Type_Definition (N);
7342 Indic : constant Node_Id := Subtype_Indication (Def);
7343 Implicit_Base : Entity_Id;
7344 Literal : Entity_Id;
7345 New_Lit : Entity_Id;
7346 Literals_List : List_Id;
7347 Type_Decl : Node_Id;
7348 Hi, Lo : Node_Id;
7349 Rang_Expr : Node_Id;
7350
7351 begin
7352 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7353 -- not have explicit literals lists we need to process types derived
7354 -- from them specially. This is handled by Derived_Standard_Character.
7355 -- If the parent type is a generic type, there are no literals either,
7356 -- and we construct the same skeletal representation as for the generic
7357 -- parent type.
7358
7359 if Is_Standard_Character_Type (Parent_Type) then
7360 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7361
7362 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7363 declare
7364 Lo : Node_Id;
7365 Hi : Node_Id;
7366
7367 begin
7368 if Nkind (Indic) /= N_Subtype_Indication then
7369 Lo :=
7370 Make_Attribute_Reference (Loc,
7371 Attribute_Name => Name_First,
7372 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7373 Set_Etype (Lo, Derived_Type);
7374
7375 Hi :=
7376 Make_Attribute_Reference (Loc,
7377 Attribute_Name => Name_Last,
7378 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7379 Set_Etype (Hi, Derived_Type);
7380
7381 Set_Scalar_Range (Derived_Type,
7382 Make_Range (Loc,
7383 Low_Bound => Lo,
7384 High_Bound => Hi));
7385 else
7386
7387 -- Analyze subtype indication and verify compatibility
7388 -- with parent type.
7389
7390 if Base_Type (Process_Subtype (Indic, N)) /=
7391 Base_Type (Parent_Type)
7392 then
7393 Error_Msg_N
7394 ("illegal constraint for formal discrete type", N);
7395 end if;
7396 end if;
7397 end;
7398
7399 else
7400 -- If a constraint is present, analyze the bounds to catch
7401 -- premature usage of the derived literals.
7402
7403 if Nkind (Indic) = N_Subtype_Indication
7404 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7405 then
7406 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7407 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7408 end if;
7409
7410 -- Introduce an implicit base type for the derived type even if there
7411 -- is no constraint attached to it, since this seems closer to the
7412 -- Ada semantics. Build a full type declaration tree for the derived
7413 -- type using the implicit base type as the defining identifier. Then
7414 -- build a subtype declaration tree which applies the constraint (if
7415 -- any) have it replace the derived type declaration.
7416
7417 Literal := First_Literal (Parent_Type);
7418 Literals_List := New_List;
7419 while Present (Literal)
7420 and then Ekind (Literal) = E_Enumeration_Literal
7421 loop
7422 -- Literals of the derived type have the same representation as
7423 -- those of the parent type, but this representation can be
7424 -- overridden by an explicit representation clause. Indicate
7425 -- that there is no explicit representation given yet. These
7426 -- derived literals are implicit operations of the new type,
7427 -- and can be overridden by explicit ones.
7428
7429 if Nkind (Literal) = N_Defining_Character_Literal then
7430 New_Lit :=
7431 Make_Defining_Character_Literal (Loc, Chars (Literal));
7432 else
7433 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7434 end if;
7435
7436 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7437 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7438 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7439 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7440 Set_Alias (New_Lit, Literal);
7441 Set_Is_Known_Valid (New_Lit, True);
7442
7443 Append (New_Lit, Literals_List);
7444 Next_Literal (Literal);
7445 end loop;
7446
7447 Implicit_Base :=
7448 Make_Defining_Identifier (Sloc (Derived_Type),
7449 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7450
7451 -- Indicate the proper nature of the derived type. This must be done
7452 -- before analysis of the literals, to recognize cases when a literal
7453 -- may be hidden by a previous explicit function definition (cf.
7454 -- c83031a).
7455
7456 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7457 Set_Etype (Derived_Type, Implicit_Base);
7458
7459 Type_Decl :=
7460 Make_Full_Type_Declaration (Loc,
7461 Defining_Identifier => Implicit_Base,
7462 Discriminant_Specifications => No_List,
7463 Type_Definition =>
7464 Make_Enumeration_Type_Definition (Loc, Literals_List));
7465
7466 Mark_Rewrite_Insertion (Type_Decl);
7467 Insert_Before (N, Type_Decl);
7468 Analyze (Type_Decl);
7469
7470 -- The anonymous base now has a full declaration, but this base
7471 -- is not a first subtype.
7472
7473 Set_Is_First_Subtype (Implicit_Base, False);
7474
7475 -- After the implicit base is analyzed its Etype needs to be changed
7476 -- to reflect the fact that it is derived from the parent type which
7477 -- was ignored during analysis. We also set the size at this point.
7478
7479 Set_Etype (Implicit_Base, Parent_Type);
7480
7481 Set_Size_Info (Implicit_Base, Parent_Type);
7482 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7483 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7484
7485 -- Copy other flags from parent type
7486
7487 Set_Has_Non_Standard_Rep
7488 (Implicit_Base, Has_Non_Standard_Rep
7489 (Parent_Type));
7490 Set_Has_Pragma_Ordered
7491 (Implicit_Base, Has_Pragma_Ordered
7492 (Parent_Type));
7493 Set_Has_Delayed_Freeze (Implicit_Base);
7494
7495 -- Process the subtype indication including a validation check on the
7496 -- constraint, if any. If a constraint is given, its bounds must be
7497 -- implicitly converted to the new type.
7498
7499 if Nkind (Indic) = N_Subtype_Indication then
7500 declare
7501 R : constant Node_Id :=
7502 Range_Expression (Constraint (Indic));
7503
7504 begin
7505 if Nkind (R) = N_Range then
7506 Hi := Build_Scalar_Bound
7507 (High_Bound (R), Parent_Type, Implicit_Base);
7508 Lo := Build_Scalar_Bound
7509 (Low_Bound (R), Parent_Type, Implicit_Base);
7510
7511 else
7512 -- Constraint is a Range attribute. Replace with explicit
7513 -- mention of the bounds of the prefix, which must be a
7514 -- subtype.
7515
7516 Analyze (Prefix (R));
7517 Hi :=
7518 Convert_To (Implicit_Base,
7519 Make_Attribute_Reference (Loc,
7520 Attribute_Name => Name_Last,
7521 Prefix =>
7522 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7523
7524 Lo :=
7525 Convert_To (Implicit_Base,
7526 Make_Attribute_Reference (Loc,
7527 Attribute_Name => Name_First,
7528 Prefix =>
7529 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7530 end if;
7531 end;
7532
7533 else
7534 Hi :=
7535 Build_Scalar_Bound
7536 (Type_High_Bound (Parent_Type),
7537 Parent_Type, Implicit_Base);
7538 Lo :=
7539 Build_Scalar_Bound
7540 (Type_Low_Bound (Parent_Type),
7541 Parent_Type, Implicit_Base);
7542 end if;
7543
7544 Rang_Expr :=
7545 Make_Range (Loc,
7546 Low_Bound => Lo,
7547 High_Bound => Hi);
7548
7549 -- If we constructed a default range for the case where no range
7550 -- was given, then the expressions in the range must not freeze
7551 -- since they do not correspond to expressions in the source.
7552 -- However, if the type inherits predicates the expressions will
7553 -- be elaborated earlier and must freeze.
7554
7555 if (Nkind (Indic) /= N_Subtype_Indication
7556 or else
7557 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7558 and then not Has_Predicates (Derived_Type)
7559 then
7560 Set_Must_Not_Freeze (Lo);
7561 Set_Must_Not_Freeze (Hi);
7562 Set_Must_Not_Freeze (Rang_Expr);
7563 end if;
7564
7565 Rewrite (N,
7566 Make_Subtype_Declaration (Loc,
7567 Defining_Identifier => Derived_Type,
7568 Subtype_Indication =>
7569 Make_Subtype_Indication (Loc,
7570 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7571 Constraint =>
7572 Make_Range_Constraint (Loc,
7573 Range_Expression => Rang_Expr))));
7574
7575 Analyze (N);
7576
7577 -- Propagate the aspects from the original type declaration to the
7578 -- declaration of the implicit base.
7579
7580 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7581
7582 -- Apply a range check. Since this range expression doesn't have an
7583 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7584 -- this right???
7585
7586 if Nkind (Indic) = N_Subtype_Indication then
7587 Apply_Range_Check
7588 (Range_Expression (Constraint (Indic)), Parent_Type,
7589 Source_Typ => Entity (Subtype_Mark (Indic)));
7590 end if;
7591 end if;
7592 end Build_Derived_Enumeration_Type;
7593
7594 --------------------------------
7595 -- Build_Derived_Numeric_Type --
7596 --------------------------------
7597
7598 procedure Build_Derived_Numeric_Type
7599 (N : Node_Id;
7600 Parent_Type : Entity_Id;
7601 Derived_Type : Entity_Id)
7602 is
7603 Loc : constant Source_Ptr := Sloc (N);
7604 Tdef : constant Node_Id := Type_Definition (N);
7605 Indic : constant Node_Id := Subtype_Indication (Tdef);
7606 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7607 No_Constraint : constant Boolean := Nkind (Indic) /=
7608 N_Subtype_Indication;
7609 Implicit_Base : Entity_Id;
7610
7611 Lo : Node_Id;
7612 Hi : Node_Id;
7613
7614 begin
7615 -- Process the subtype indication including a validation check on
7616 -- the constraint if any.
7617
7618 Discard_Node (Process_Subtype (Indic, N));
7619
7620 -- Introduce an implicit base type for the derived type even if there
7621 -- is no constraint attached to it, since this seems closer to the Ada
7622 -- semantics.
7623
7624 Implicit_Base :=
7625 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7626
7627 Set_Etype (Implicit_Base, Parent_Base);
7628 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7629 Set_Size_Info (Implicit_Base, Parent_Base);
7630 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7631 Set_Parent (Implicit_Base, Parent (Derived_Type));
7632 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7633 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7634
7635 -- Set RM Size for discrete type or decimal fixed-point type
7636 -- Ordinary fixed-point is excluded, why???
7637
7638 if Is_Discrete_Type (Parent_Base)
7639 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7640 then
7641 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7642 end if;
7643
7644 Set_Has_Delayed_Freeze (Implicit_Base);
7645
7646 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7647 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7648
7649 Set_Scalar_Range (Implicit_Base,
7650 Make_Range (Loc,
7651 Low_Bound => Lo,
7652 High_Bound => Hi));
7653
7654 if Has_Infinities (Parent_Base) then
7655 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7656 end if;
7657
7658 -- The Derived_Type, which is the entity of the declaration, is a
7659 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7660 -- absence of an explicit constraint.
7661
7662 Set_Etype (Derived_Type, Implicit_Base);
7663
7664 -- If we did not have a constraint, then the Ekind is set from the
7665 -- parent type (otherwise Process_Subtype has set the bounds)
7666
7667 if No_Constraint then
7668 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7669 end if;
7670
7671 -- If we did not have a range constraint, then set the range from the
7672 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7673
7674 if No_Constraint or else not Has_Range_Constraint (Indic) then
7675 Set_Scalar_Range (Derived_Type,
7676 Make_Range (Loc,
7677 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7678 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7679 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7680
7681 if Has_Infinities (Parent_Type) then
7682 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7683 end if;
7684
7685 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7686 end if;
7687
7688 Set_Is_Descendant_Of_Address (Derived_Type,
7689 Is_Descendant_Of_Address (Parent_Type));
7690 Set_Is_Descendant_Of_Address (Implicit_Base,
7691 Is_Descendant_Of_Address (Parent_Type));
7692
7693 -- Set remaining type-specific fields, depending on numeric type
7694
7695 if Is_Modular_Integer_Type (Parent_Type) then
7696 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7697
7698 Set_Non_Binary_Modulus
7699 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7700
7701 Set_Is_Known_Valid
7702 (Implicit_Base, Is_Known_Valid (Parent_Base));
7703
7704 elsif Is_Floating_Point_Type (Parent_Type) then
7705
7706 -- Digits of base type is always copied from the digits value of
7707 -- the parent base type, but the digits of the derived type will
7708 -- already have been set if there was a constraint present.
7709
7710 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7711 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7712
7713 if No_Constraint then
7714 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7715 end if;
7716
7717 elsif Is_Fixed_Point_Type (Parent_Type) then
7718
7719 -- Small of base type and derived type are always copied from the
7720 -- parent base type, since smalls never change. The delta of the
7721 -- base type is also copied from the parent base type. However the
7722 -- delta of the derived type will have been set already if a
7723 -- constraint was present.
7724
7725 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7726 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7727 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7728
7729 if No_Constraint then
7730 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7731 end if;
7732
7733 -- The scale and machine radix in the decimal case are always
7734 -- copied from the parent base type.
7735
7736 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7737 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7738 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7739
7740 Set_Machine_Radix_10
7741 (Derived_Type, Machine_Radix_10 (Parent_Base));
7742 Set_Machine_Radix_10
7743 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7744
7745 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7746
7747 if No_Constraint then
7748 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7749
7750 else
7751 -- the analysis of the subtype_indication sets the
7752 -- digits value of the derived type.
7753
7754 null;
7755 end if;
7756 end if;
7757 end if;
7758
7759 if Is_Integer_Type (Parent_Type) then
7760 Set_Has_Shift_Operator
7761 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7762 end if;
7763
7764 -- The type of the bounds is that of the parent type, and they
7765 -- must be converted to the derived type.
7766
7767 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7768
7769 -- The implicit_base should be frozen when the derived type is frozen,
7770 -- but note that it is used in the conversions of the bounds. For fixed
7771 -- types we delay the determination of the bounds until the proper
7772 -- freezing point. For other numeric types this is rejected by GCC, for
7773 -- reasons that are currently unclear (???), so we choose to freeze the
7774 -- implicit base now. In the case of integers and floating point types
7775 -- this is harmless because subsequent representation clauses cannot
7776 -- affect anything, but it is still baffling that we cannot use the
7777 -- same mechanism for all derived numeric types.
7778
7779 -- There is a further complication: actually some representation
7780 -- clauses can affect the implicit base type. For example, attribute
7781 -- definition clauses for stream-oriented attributes need to set the
7782 -- corresponding TSS entries on the base type, and this normally
7783 -- cannot be done after the base type is frozen, so the circuitry in
7784 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7785 -- and not use Set_TSS in this case.
7786
7787 -- There are also consequences for the case of delayed representation
7788 -- aspects for some cases. For example, a Size aspect is delayed and
7789 -- should not be evaluated to the freeze point. This early freezing
7790 -- means that the size attribute evaluation happens too early???
7791
7792 if Is_Fixed_Point_Type (Parent_Type) then
7793 Conditional_Delay (Implicit_Base, Parent_Type);
7794 else
7795 Freeze_Before (N, Implicit_Base);
7796 end if;
7797 end Build_Derived_Numeric_Type;
7798
7799 --------------------------------
7800 -- Build_Derived_Private_Type --
7801 --------------------------------
7802
7803 procedure Build_Derived_Private_Type
7804 (N : Node_Id;
7805 Parent_Type : Entity_Id;
7806 Derived_Type : Entity_Id;
7807 Is_Completion : Boolean;
7808 Derive_Subps : Boolean := True)
7809 is
7810 Loc : constant Source_Ptr := Sloc (N);
7811 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7812 Par_Scope : constant Entity_Id := Scope (Par_Base);
7813 Full_N : constant Node_Id := New_Copy_Tree (N);
7814 Full_Der : Entity_Id := New_Copy (Derived_Type);
7815 Full_P : Entity_Id;
7816
7817 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7818 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7819 -- present (they cannot be both present for the same type), or Empty.
7820
7821 procedure Build_Full_Derivation;
7822 -- Build full derivation, i.e. derive from the full view
7823
7824 procedure Copy_And_Build;
7825 -- Copy derived type declaration, replace parent with its full view,
7826 -- and build derivation
7827
7828 -------------------------
7829 -- Available_Full_View --
7830 -------------------------
7831
7832 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7833 begin
7834 if Present (Full_View (Typ)) then
7835 return Full_View (Typ);
7836
7837 elsif Present (Underlying_Full_View (Typ)) then
7838
7839 -- We should be called on a type with an underlying full view
7840 -- only by means of the recursive call made in Copy_And_Build
7841 -- through the first call to Build_Derived_Type, or else if
7842 -- the parent scope is being analyzed because we are deriving
7843 -- a completion.
7844
7845 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7846
7847 return Underlying_Full_View (Typ);
7848
7849 else
7850 return Empty;
7851 end if;
7852 end Available_Full_View;
7853
7854 ---------------------------
7855 -- Build_Full_Derivation --
7856 ---------------------------
7857
7858 procedure Build_Full_Derivation is
7859 begin
7860 -- If parent scope is not open, install the declarations
7861
7862 if not In_Open_Scopes (Par_Scope) then
7863 Install_Private_Declarations (Par_Scope);
7864 Install_Visible_Declarations (Par_Scope);
7865 Copy_And_Build;
7866 Uninstall_Declarations (Par_Scope);
7867
7868 -- If parent scope is open and in another unit, and parent has a
7869 -- completion, then the derivation is taking place in the visible
7870 -- part of a child unit. In that case retrieve the full view of
7871 -- the parent momentarily.
7872
7873 elsif not In_Same_Source_Unit (N, Parent_Type)
7874 and then Present (Full_View (Parent_Type))
7875 then
7876 Full_P := Full_View (Parent_Type);
7877 Exchange_Declarations (Parent_Type);
7878 Copy_And_Build;
7879 Exchange_Declarations (Full_P);
7880
7881 -- Otherwise it is a local derivation
7882
7883 else
7884 Copy_And_Build;
7885 end if;
7886 end Build_Full_Derivation;
7887
7888 --------------------
7889 -- Copy_And_Build --
7890 --------------------
7891
7892 procedure Copy_And_Build is
7893 Full_Parent : Entity_Id := Parent_Type;
7894
7895 begin
7896 -- If the parent is itself derived from another private type,
7897 -- installing the private declarations has not affected its
7898 -- privacy status, so use its own full view explicitly.
7899
7900 if Is_Private_Type (Full_Parent)
7901 and then Present (Full_View (Full_Parent))
7902 then
7903 Full_Parent := Full_View (Full_Parent);
7904 end if;
7905
7906 -- If the full view is itself derived from another private type
7907 -- and has got an underlying full view, and this is done for a
7908 -- completion, i.e. to build the underlying full view of the type,
7909 -- then use this underlying full view. We cannot do that if this
7910 -- is not a completion, i.e. to build the full view of the type,
7911 -- because this would break the privacy of the parent type, except
7912 -- if the parent scope is being analyzed because we are deriving a
7913 -- completion.
7914
7915 if Is_Private_Type (Full_Parent)
7916 and then Present (Underlying_Full_View (Full_Parent))
7917 and then (Is_Completion or else In_Private_Part (Par_Scope))
7918 then
7919 Full_Parent := Underlying_Full_View (Full_Parent);
7920 end if;
7921
7922 -- For private, record, concurrent, access and almost all enumeration
7923 -- types, the derivation from the full view requires a fully-fledged
7924 -- declaration. In the other cases, just use an itype.
7925
7926 if Is_Private_Type (Full_Parent)
7927 or else Is_Record_Type (Full_Parent)
7928 or else Is_Concurrent_Type (Full_Parent)
7929 or else Is_Access_Type (Full_Parent)
7930 or else
7931 (Is_Enumeration_Type (Full_Parent)
7932 and then not Is_Standard_Character_Type (Full_Parent)
7933 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7934 then
7935 -- Copy and adjust declaration to provide a completion for what
7936 -- is originally a private declaration. Indicate that full view
7937 -- is internally generated.
7938
7939 Set_Comes_From_Source (Full_N, False);
7940 Set_Comes_From_Source (Full_Der, False);
7941 Set_Parent (Full_Der, Full_N);
7942 Set_Defining_Identifier (Full_N, Full_Der);
7943
7944 -- If there are no constraints, adjust the subtype mark
7945
7946 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7947 N_Subtype_Indication
7948 then
7949 Set_Subtype_Indication
7950 (Type_Definition (Full_N),
7951 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7952 end if;
7953
7954 Insert_After (N, Full_N);
7955
7956 -- Build full view of derived type from full view of parent which
7957 -- is now installed. Subprograms have been derived on the partial
7958 -- view, the completion does not derive them anew.
7959
7960 if Is_Record_Type (Full_Parent) then
7961
7962 -- If parent type is tagged, the completion inherits the proper
7963 -- primitive operations.
7964
7965 if Is_Tagged_Type (Parent_Type) then
7966 Build_Derived_Record_Type
7967 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7968 else
7969 Build_Derived_Record_Type
7970 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7971 end if;
7972
7973 else
7974 -- If the parent type is private, this is not a completion and
7975 -- we build the full derivation recursively as a completion.
7976
7977 Build_Derived_Type
7978 (Full_N, Full_Parent, Full_Der,
7979 Is_Completion => Is_Private_Type (Full_Parent),
7980 Derive_Subps => False);
7981 end if;
7982
7983 -- The full declaration has been introduced into the tree and
7984 -- processed in the step above. It should not be analyzed again
7985 -- (when encountered later in the current list of declarations)
7986 -- to prevent spurious name conflicts. The full entity remains
7987 -- invisible.
7988
7989 Set_Analyzed (Full_N);
7990
7991 else
7992 Full_Der :=
7993 Make_Defining_Identifier (Sloc (Derived_Type),
7994 Chars => Chars (Derived_Type));
7995 Set_Is_Itype (Full_Der);
7996 Set_Associated_Node_For_Itype (Full_Der, N);
7997 Set_Parent (Full_Der, N);
7998 Build_Derived_Type
7999 (N, Full_Parent, Full_Der,
8000 Is_Completion => False, Derive_Subps => False);
8001 end if;
8002
8003 Set_Has_Private_Declaration (Full_Der);
8004 Set_Has_Private_Declaration (Derived_Type);
8005
8006 Set_Scope (Full_Der, Scope (Derived_Type));
8007 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8008 Set_Has_Size_Clause (Full_Der, False);
8009 Set_Has_Alignment_Clause (Full_Der, False);
8010 Set_Has_Delayed_Freeze (Full_Der);
8011 Set_Is_Frozen (Full_Der, False);
8012 Set_Freeze_Node (Full_Der, Empty);
8013 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8014 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8015
8016 -- The convention on the base type may be set in the private part
8017 -- and not propagated to the subtype until later, so we obtain the
8018 -- convention from the base type of the parent.
8019
8020 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8021 end Copy_And_Build;
8022
8023 -- Start of processing for Build_Derived_Private_Type
8024
8025 begin
8026 if Is_Tagged_Type (Parent_Type) then
8027 Full_P := Full_View (Parent_Type);
8028
8029 -- A type extension of a type with unknown discriminants is an
8030 -- indefinite type that the back-end cannot handle directly.
8031 -- We treat it as a private type, and build a completion that is
8032 -- derived from the full view of the parent, and hopefully has
8033 -- known discriminants.
8034
8035 -- If the full view of the parent type has an underlying record view,
8036 -- use it to generate the underlying record view of this derived type
8037 -- (required for chains of derivations with unknown discriminants).
8038
8039 -- Minor optimization: we avoid the generation of useless underlying
8040 -- record view entities if the private type declaration has unknown
8041 -- discriminants but its corresponding full view has no
8042 -- discriminants.
8043
8044 if Has_Unknown_Discriminants (Parent_Type)
8045 and then Present (Full_P)
8046 and then (Has_Discriminants (Full_P)
8047 or else Present (Underlying_Record_View (Full_P)))
8048 and then not In_Open_Scopes (Par_Scope)
8049 and then Expander_Active
8050 then
8051 declare
8052 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8053 New_Ext : constant Node_Id :=
8054 Copy_Separate_Tree
8055 (Record_Extension_Part (Type_Definition (N)));
8056 Decl : Node_Id;
8057
8058 begin
8059 Build_Derived_Record_Type
8060 (N, Parent_Type, Derived_Type, Derive_Subps);
8061
8062 -- Build anonymous completion, as a derivation from the full
8063 -- view of the parent. This is not a completion in the usual
8064 -- sense, because the current type is not private.
8065
8066 Decl :=
8067 Make_Full_Type_Declaration (Loc,
8068 Defining_Identifier => Full_Der,
8069 Type_Definition =>
8070 Make_Derived_Type_Definition (Loc,
8071 Subtype_Indication =>
8072 New_Copy_Tree
8073 (Subtype_Indication (Type_Definition (N))),
8074 Record_Extension_Part => New_Ext));
8075
8076 -- If the parent type has an underlying record view, use it
8077 -- here to build the new underlying record view.
8078
8079 if Present (Underlying_Record_View (Full_P)) then
8080 pragma Assert
8081 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8082 = N_Identifier);
8083 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8084 Underlying_Record_View (Full_P));
8085 end if;
8086
8087 Install_Private_Declarations (Par_Scope);
8088 Install_Visible_Declarations (Par_Scope);
8089 Insert_Before (N, Decl);
8090
8091 -- Mark entity as an underlying record view before analysis,
8092 -- to avoid generating the list of its primitive operations
8093 -- (which is not really required for this entity) and thus
8094 -- prevent spurious errors associated with missing overriding
8095 -- of abstract primitives (overridden only for Derived_Type).
8096
8097 Mutate_Ekind (Full_Der, E_Record_Type);
8098 Set_Is_Underlying_Record_View (Full_Der);
8099 Set_Default_SSO (Full_Der);
8100 Set_No_Reordering (Full_Der, No_Component_Reordering);
8101
8102 Analyze (Decl);
8103
8104 pragma Assert (Has_Discriminants (Full_Der)
8105 and then not Has_Unknown_Discriminants (Full_Der));
8106
8107 Uninstall_Declarations (Par_Scope);
8108
8109 -- Freeze the underlying record view, to prevent generation of
8110 -- useless dispatching information, which is simply shared with
8111 -- the real derived type.
8112
8113 Set_Is_Frozen (Full_Der);
8114
8115 -- If the derived type has access discriminants, create
8116 -- references to their anonymous types now, to prevent
8117 -- back-end problems when their first use is in generated
8118 -- bodies of primitives.
8119
8120 declare
8121 E : Entity_Id;
8122
8123 begin
8124 E := First_Entity (Full_Der);
8125
8126 while Present (E) loop
8127 if Ekind (E) = E_Discriminant
8128 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8129 then
8130 Build_Itype_Reference (Etype (E), Decl);
8131 end if;
8132
8133 Next_Entity (E);
8134 end loop;
8135 end;
8136
8137 -- Set up links between real entity and underlying record view
8138
8139 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8140 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8141 end;
8142
8143 -- If discriminants are known, build derived record
8144
8145 else
8146 Build_Derived_Record_Type
8147 (N, Parent_Type, Derived_Type, Derive_Subps);
8148 end if;
8149
8150 return;
8151
8152 elsif Has_Discriminants (Parent_Type) then
8153
8154 -- Build partial view of derived type from partial view of parent.
8155 -- This must be done before building the full derivation because the
8156 -- second derivation will modify the discriminants of the first and
8157 -- the discriminants are chained with the rest of the components in
8158 -- the full derivation.
8159
8160 Build_Derived_Record_Type
8161 (N, Parent_Type, Derived_Type, Derive_Subps);
8162
8163 -- Build the full derivation if this is not the anonymous derived
8164 -- base type created by Build_Derived_Record_Type in the constrained
8165 -- case (see point 5. of its head comment) since we build it for the
8166 -- derived subtype.
8167
8168 if Present (Available_Full_View (Parent_Type))
8169 and then not Is_Itype (Derived_Type)
8170 then
8171 declare
8172 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8173 Discr : Entity_Id;
8174 Last_Discr : Entity_Id;
8175
8176 begin
8177 -- If this is not a completion, construct the implicit full
8178 -- view by deriving from the full view of the parent type.
8179 -- But if this is a completion, the derived private type
8180 -- being built is a full view and the full derivation can
8181 -- only be its underlying full view.
8182
8183 Build_Full_Derivation;
8184
8185 if not Is_Completion then
8186 Set_Full_View (Derived_Type, Full_Der);
8187 else
8188 Set_Underlying_Full_View (Derived_Type, Full_Der);
8189 Set_Is_Underlying_Full_View (Full_Der);
8190 end if;
8191
8192 if not Is_Base_Type (Derived_Type) then
8193 Set_Full_View (Der_Base, Base_Type (Full_Der));
8194 end if;
8195
8196 -- Copy the discriminant list from full view to the partial
8197 -- view (base type and its subtype). Gigi requires that the
8198 -- partial and full views have the same discriminants.
8199
8200 -- Note that since the partial view points to discriminants
8201 -- in the full view, their scope will be that of the full
8202 -- view. This might cause some front end problems and need
8203 -- adjustment???
8204
8205 Discr := First_Discriminant (Base_Type (Full_Der));
8206 Set_First_Entity (Der_Base, Discr);
8207
8208 loop
8209 Last_Discr := Discr;
8210 Next_Discriminant (Discr);
8211 exit when No (Discr);
8212 end loop;
8213
8214 Set_Last_Entity (Der_Base, Last_Discr);
8215 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8216 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8217 end;
8218 end if;
8219
8220 elsif Present (Available_Full_View (Parent_Type))
8221 and then Has_Discriminants (Available_Full_View (Parent_Type))
8222 then
8223 if Has_Unknown_Discriminants (Parent_Type)
8224 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8225 N_Subtype_Indication
8226 then
8227 Error_Msg_N
8228 ("cannot constrain type with unknown discriminants",
8229 Subtype_Indication (Type_Definition (N)));
8230 return;
8231 end if;
8232
8233 -- If this is not a completion, construct the implicit full view by
8234 -- deriving from the full view of the parent type. But if this is a
8235 -- completion, the derived private type being built is a full view
8236 -- and the full derivation can only be its underlying full view.
8237
8238 Build_Full_Derivation;
8239
8240 if not Is_Completion then
8241 Set_Full_View (Derived_Type, Full_Der);
8242 else
8243 Set_Underlying_Full_View (Derived_Type, Full_Der);
8244 Set_Is_Underlying_Full_View (Full_Der);
8245 end if;
8246
8247 -- In any case, the primitive operations are inherited from the
8248 -- parent type, not from the internal full view.
8249
8250 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8251
8252 if Derive_Subps then
8253 Derive_Subprograms (Parent_Type, Derived_Type);
8254 end if;
8255
8256 Set_Stored_Constraint (Derived_Type, No_Elist);
8257 Set_Is_Constrained
8258 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8259
8260 else
8261 -- Untagged type, No discriminants on either view
8262
8263 if Nkind (Subtype_Indication (Type_Definition (N))) =
8264 N_Subtype_Indication
8265 then
8266 Error_Msg_N
8267 ("illegal constraint on type without discriminants", N);
8268 end if;
8269
8270 if Present (Discriminant_Specifications (N))
8271 and then Present (Available_Full_View (Parent_Type))
8272 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8273 then
8274 Error_Msg_N ("cannot add discriminants to untagged type", N);
8275 end if;
8276
8277 Set_Stored_Constraint (Derived_Type, No_Elist);
8278 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8279
8280 Set_Is_Controlled_Active
8281 (Derived_Type, Is_Controlled_Active (Parent_Type));
8282
8283 Set_Disable_Controlled
8284 (Derived_Type, Disable_Controlled (Parent_Type));
8285
8286 Set_Has_Controlled_Component
8287 (Derived_Type, Has_Controlled_Component (Parent_Type));
8288
8289 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8290
8291 if not Is_Controlled (Parent_Type) then
8292 Set_Finalize_Storage_Only
8293 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8294 end if;
8295
8296 -- If this is not a completion, construct the implicit full view by
8297 -- deriving from the full view of the parent type. But if this is a
8298 -- completion, the derived private type being built is a full view
8299 -- and the full derivation can only be its underlying full view.
8300
8301 -- ??? If the parent type is untagged private and its completion is
8302 -- tagged, this mechanism will not work because we cannot derive from
8303 -- the tagged full view unless we have an extension.
8304
8305 if Present (Available_Full_View (Parent_Type))
8306 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8307 and then not Error_Posted (N)
8308 then
8309 Build_Full_Derivation;
8310
8311 if not Is_Completion then
8312 Set_Full_View (Derived_Type, Full_Der);
8313 else
8314 Set_Underlying_Full_View (Derived_Type, Full_Der);
8315 Set_Is_Underlying_Full_View (Full_Der);
8316 end if;
8317 end if;
8318 end if;
8319
8320 Set_Has_Unknown_Discriminants (Derived_Type,
8321 Has_Unknown_Discriminants (Parent_Type));
8322
8323 if Is_Private_Type (Derived_Type) then
8324 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8325 end if;
8326
8327 -- If the parent base type is in scope, add the derived type to its
8328 -- list of private dependents, because its full view may become
8329 -- visible subsequently (in a nested private part, a body, or in a
8330 -- further child unit).
8331
8332 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8333 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8334
8335 -- Check for unusual case where a type completed by a private
8336 -- derivation occurs within a package nested in a child unit, and
8337 -- the parent is declared in an ancestor.
8338
8339 if Is_Child_Unit (Scope (Current_Scope))
8340 and then Is_Completion
8341 and then In_Private_Part (Current_Scope)
8342 and then Scope (Parent_Type) /= Current_Scope
8343
8344 -- Note that if the parent has a completion in the private part,
8345 -- (which is itself a derivation from some other private type)
8346 -- it is that completion that is visible, there is no full view
8347 -- available, and no special processing is needed.
8348
8349 and then Present (Full_View (Parent_Type))
8350 then
8351 -- In this case, the full view of the parent type will become
8352 -- visible in the body of the enclosing child, and only then will
8353 -- the current type be possibly non-private. Build an underlying
8354 -- full view that will be installed when the enclosing child body
8355 -- is compiled.
8356
8357 if Present (Underlying_Full_View (Derived_Type)) then
8358 Full_Der := Underlying_Full_View (Derived_Type);
8359 else
8360 Build_Full_Derivation;
8361 Set_Underlying_Full_View (Derived_Type, Full_Der);
8362 Set_Is_Underlying_Full_View (Full_Der);
8363 end if;
8364
8365 -- The full view will be used to swap entities on entry/exit to
8366 -- the body, and must appear in the entity list for the package.
8367
8368 Append_Entity (Full_Der, Scope (Derived_Type));
8369 end if;
8370 end if;
8371 end Build_Derived_Private_Type;
8372
8373 -------------------------------
8374 -- Build_Derived_Record_Type --
8375 -------------------------------
8376
8377 -- 1. INTRODUCTION
8378
8379 -- Ideally we would like to use the same model of type derivation for
8380 -- tagged and untagged record types. Unfortunately this is not quite
8381 -- possible because the semantics of representation clauses is different
8382 -- for tagged and untagged records under inheritance. Consider the
8383 -- following:
8384
8385 -- type R (...) is [tagged] record ... end record;
8386 -- type T (...) is new R (...) [with ...];
8387
8388 -- The representation clauses for T can specify a completely different
8389 -- record layout from R's. Hence the same component can be placed in two
8390 -- very different positions in objects of type T and R. If R and T are
8391 -- tagged types, representation clauses for T can only specify the layout
8392 -- of non inherited components, thus components that are common in R and T
8393 -- have the same position in objects of type R and T.
8394
8395 -- This has two implications. The first is that the entire tree for R's
8396 -- declaration needs to be copied for T in the untagged case, so that T
8397 -- can be viewed as a record type of its own with its own representation
8398 -- clauses. The second implication is the way we handle discriminants.
8399 -- Specifically, in the untagged case we need a way to communicate to Gigi
8400 -- what are the real discriminants in the record, while for the semantics
8401 -- we need to consider those introduced by the user to rename the
8402 -- discriminants in the parent type. This is handled by introducing the
8403 -- notion of stored discriminants. See below for more.
8404
8405 -- Fortunately the way regular components are inherited can be handled in
8406 -- the same way in tagged and untagged types.
8407
8408 -- To complicate things a bit more the private view of a private extension
8409 -- cannot be handled in the same way as the full view (for one thing the
8410 -- semantic rules are somewhat different). We will explain what differs
8411 -- below.
8412
8413 -- 2. DISCRIMINANTS UNDER INHERITANCE
8414
8415 -- The semantic rules governing the discriminants of derived types are
8416 -- quite subtle.
8417
8418 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8419 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8420
8421 -- If parent type has discriminants, then the discriminants that are
8422 -- declared in the derived type are [3.4 (11)]:
8423
8424 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8425 -- there is one;
8426
8427 -- o Otherwise, each discriminant of the parent type (implicitly declared
8428 -- in the same order with the same specifications). In this case, the
8429 -- discriminants are said to be "inherited", or if unknown in the parent
8430 -- are also unknown in the derived type.
8431
8432 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8433
8434 -- o The parent subtype must be constrained;
8435
8436 -- o If the parent type is not a tagged type, then each discriminant of
8437 -- the derived type must be used in the constraint defining a parent
8438 -- subtype. [Implementation note: This ensures that the new discriminant
8439 -- can share storage with an existing discriminant.]
8440
8441 -- For the derived type each discriminant of the parent type is either
8442 -- inherited, constrained to equal some new discriminant of the derived
8443 -- type, or constrained to the value of an expression.
8444
8445 -- When inherited or constrained to equal some new discriminant, the
8446 -- parent discriminant and the discriminant of the derived type are said
8447 -- to "correspond".
8448
8449 -- If a discriminant of the parent type is constrained to a specific value
8450 -- in the derived type definition, then the discriminant is said to be
8451 -- "specified" by that derived type definition.
8452
8453 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8454
8455 -- We have spoken about stored discriminants in point 1 (introduction)
8456 -- above. There are two sorts of stored discriminants: implicit and
8457 -- explicit. As long as the derived type inherits the same discriminants as
8458 -- the root record type, stored discriminants are the same as regular
8459 -- discriminants, and are said to be implicit. However, if any discriminant
8460 -- in the root type was renamed in the derived type, then the derived
8461 -- type will contain explicit stored discriminants. Explicit stored
8462 -- discriminants are discriminants in addition to the semantically visible
8463 -- discriminants defined for the derived type. Stored discriminants are
8464 -- used by Gigi to figure out what are the physical discriminants in
8465 -- objects of the derived type (see precise definition in einfo.ads).
8466 -- As an example, consider the following:
8467
8468 -- type R (D1, D2, D3 : Int) is record ... end record;
8469 -- type T1 is new R;
8470 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8471 -- type T3 is new T2;
8472 -- type T4 (Y : Int) is new T3 (Y, 99);
8473
8474 -- The following table summarizes the discriminants and stored
8475 -- discriminants in R and T1 through T4:
8476
8477 -- Type Discrim Stored Discrim Comment
8478 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8479 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8480 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8481 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8482 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8483
8484 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8485 -- find the corresponding discriminant in the parent type, while
8486 -- Original_Record_Component (abbreviated ORC below) the actual physical
8487 -- component that is renamed. Finally the field Is_Completely_Hidden
8488 -- (abbreviated ICH below) is set for all explicit stored discriminants
8489 -- (see einfo.ads for more info). For the above example this gives:
8490
8491 -- Discrim CD ORC ICH
8492 -- ^^^^^^^ ^^ ^^^ ^^^
8493 -- D1 in R empty itself no
8494 -- D2 in R empty itself no
8495 -- D3 in R empty itself no
8496
8497 -- D1 in T1 D1 in R itself no
8498 -- D2 in T1 D2 in R itself no
8499 -- D3 in T1 D3 in R itself no
8500
8501 -- X1 in T2 D3 in T1 D3 in T2 no
8502 -- X2 in T2 D1 in T1 D1 in T2 no
8503 -- D1 in T2 empty itself yes
8504 -- D2 in T2 empty itself yes
8505 -- D3 in T2 empty itself yes
8506
8507 -- X1 in T3 X1 in T2 D3 in T3 no
8508 -- X2 in T3 X2 in T2 D1 in T3 no
8509 -- D1 in T3 empty itself yes
8510 -- D2 in T3 empty itself yes
8511 -- D3 in T3 empty itself yes
8512
8513 -- Y in T4 X1 in T3 D3 in T4 no
8514 -- D1 in T4 empty itself yes
8515 -- D2 in T4 empty itself yes
8516 -- D3 in T4 empty itself yes
8517
8518 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8519
8520 -- Type derivation for tagged types is fairly straightforward. If no
8521 -- discriminants are specified by the derived type, these are inherited
8522 -- from the parent. No explicit stored discriminants are ever necessary.
8523 -- The only manipulation that is done to the tree is that of adding a
8524 -- _parent field with parent type and constrained to the same constraint
8525 -- specified for the parent in the derived type definition. For instance:
8526
8527 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8528 -- type T1 is new R with null record;
8529 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8530
8531 -- are changed into:
8532
8533 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8534 -- _parent : R (D1, D2, D3);
8535 -- end record;
8536
8537 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8538 -- _parent : T1 (X2, 88, X1);
8539 -- end record;
8540
8541 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8542 -- ORC and ICH fields are:
8543
8544 -- Discrim CD ORC ICH
8545 -- ^^^^^^^ ^^ ^^^ ^^^
8546 -- D1 in R empty itself no
8547 -- D2 in R empty itself no
8548 -- D3 in R empty itself no
8549
8550 -- D1 in T1 D1 in R D1 in R no
8551 -- D2 in T1 D2 in R D2 in R no
8552 -- D3 in T1 D3 in R D3 in R no
8553
8554 -- X1 in T2 D3 in T1 D3 in R no
8555 -- X2 in T2 D1 in T1 D1 in R no
8556
8557 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8558 --
8559 -- Regardless of whether we dealing with a tagged or untagged type
8560 -- we will transform all derived type declarations of the form
8561 --
8562 -- type T is new R (...) [with ...];
8563 -- or
8564 -- subtype S is R (...);
8565 -- type T is new S [with ...];
8566 -- into
8567 -- type BT is new R [with ...];
8568 -- subtype T is BT (...);
8569 --
8570 -- That is, the base derived type is constrained only if it has no
8571 -- discriminants. The reason for doing this is that GNAT's semantic model
8572 -- assumes that a base type with discriminants is unconstrained.
8573 --
8574 -- Note that, strictly speaking, the above transformation is not always
8575 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8576 --
8577 -- procedure B34011A is
8578 -- type REC (D : integer := 0) is record
8579 -- I : Integer;
8580 -- end record;
8581
8582 -- package P is
8583 -- type T6 is new Rec;
8584 -- function F return T6;
8585 -- end P;
8586
8587 -- use P;
8588 -- package Q6 is
8589 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8590 -- end Q6;
8591 --
8592 -- The definition of Q6.U is illegal. However transforming Q6.U into
8593
8594 -- type BaseU is new T6;
8595 -- subtype U is BaseU (Q6.F.I)
8596
8597 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8598 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8599 -- the transformation described above.
8600
8601 -- There is another instance where the above transformation is incorrect.
8602 -- Consider:
8603
8604 -- package Pack is
8605 -- type Base (D : Integer) is tagged null record;
8606 -- procedure P (X : Base);
8607
8608 -- type Der is new Base (2) with null record;
8609 -- procedure P (X : Der);
8610 -- end Pack;
8611
8612 -- Then the above transformation turns this into
8613
8614 -- type Der_Base is new Base with null record;
8615 -- -- procedure P (X : Base) is implicitly inherited here
8616 -- -- as procedure P (X : Der_Base).
8617
8618 -- subtype Der is Der_Base (2);
8619 -- procedure P (X : Der);
8620 -- -- The overriding of P (X : Der_Base) is illegal since we
8621 -- -- have a parameter conformance problem.
8622
8623 -- To get around this problem, after having semantically processed Der_Base
8624 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8625 -- Discriminant_Constraint from Der so that when parameter conformance is
8626 -- checked when P is overridden, no semantic errors are flagged.
8627
8628 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8629
8630 -- Regardless of whether we are dealing with a tagged or untagged type
8631 -- we will transform all derived type declarations of the form
8632
8633 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8634 -- type T is new R [with ...];
8635 -- into
8636 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8637
8638 -- The reason for such transformation is that it allows us to implement a
8639 -- very clean form of component inheritance as explained below.
8640
8641 -- Note that this transformation is not achieved by direct tree rewriting
8642 -- and manipulation, but rather by redoing the semantic actions that the
8643 -- above transformation will entail. This is done directly in routine
8644 -- Inherit_Components.
8645
8646 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8647
8648 -- In both tagged and untagged derived types, regular non discriminant
8649 -- components are inherited in the derived type from the parent type. In
8650 -- the absence of discriminants component, inheritance is straightforward
8651 -- as components can simply be copied from the parent.
8652
8653 -- If the parent has discriminants, inheriting components constrained with
8654 -- these discriminants requires caution. Consider the following example:
8655
8656 -- type R (D1, D2 : Positive) is [tagged] record
8657 -- S : String (D1 .. D2);
8658 -- end record;
8659
8660 -- type T1 is new R [with null record];
8661 -- type T2 (X : positive) is new R (1, X) [with null record];
8662
8663 -- As explained in 6. above, T1 is rewritten as
8664 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8665 -- which makes the treatment for T1 and T2 identical.
8666
8667 -- What we want when inheriting S, is that references to D1 and D2 in R are
8668 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8669 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8670 -- with either discriminant references in the derived type or expressions.
8671 -- This replacement is achieved as follows: before inheriting R's
8672 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8673 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8674 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8675 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8676 -- by String (1 .. X).
8677
8678 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8679
8680 -- We explain here the rules governing private type extensions relevant to
8681 -- type derivation. These rules are explained on the following example:
8682
8683 -- type D [(...)] is new A [(...)] with private; <-- partial view
8684 -- type D [(...)] is new P [(...)] with null record; <-- full view
8685
8686 -- Type A is called the ancestor subtype of the private extension.
8687 -- Type P is the parent type of the full view of the private extension. It
8688 -- must be A or a type derived from A.
8689
8690 -- The rules concerning the discriminants of private type extensions are
8691 -- [7.3(10-13)]:
8692
8693 -- o If a private extension inherits known discriminants from the ancestor
8694 -- subtype, then the full view must also inherit its discriminants from
8695 -- the ancestor subtype and the parent subtype of the full view must be
8696 -- constrained if and only if the ancestor subtype is constrained.
8697
8698 -- o If a partial view has unknown discriminants, then the full view may
8699 -- define a definite or an indefinite subtype, with or without
8700 -- discriminants.
8701
8702 -- o If a partial view has neither known nor unknown discriminants, then
8703 -- the full view must define a definite subtype.
8704
8705 -- o If the ancestor subtype of a private extension has constrained
8706 -- discriminants, then the parent subtype of the full view must impose a
8707 -- statically matching constraint on those discriminants.
8708
8709 -- This means that only the following forms of private extensions are
8710 -- allowed:
8711
8712 -- type D is new A with private; <-- partial view
8713 -- type D is new P with null record; <-- full view
8714
8715 -- If A has no discriminants than P has no discriminants, otherwise P must
8716 -- inherit A's discriminants.
8717
8718 -- type D is new A (...) with private; <-- partial view
8719 -- type D is new P (:::) with null record; <-- full view
8720
8721 -- P must inherit A's discriminants and (...) and (:::) must statically
8722 -- match.
8723
8724 -- subtype A is R (...);
8725 -- type D is new A with private; <-- partial view
8726 -- type D is new P with null record; <-- full view
8727
8728 -- P must have inherited R's discriminants and must be derived from A or
8729 -- any of its subtypes.
8730
8731 -- type D (..) is new A with private; <-- partial view
8732 -- type D (..) is new P [(:::)] with null record; <-- full view
8733
8734 -- No specific constraints on P's discriminants or constraint (:::).
8735 -- Note that A can be unconstrained, but the parent subtype P must either
8736 -- be constrained or (:::) must be present.
8737
8738 -- type D (..) is new A [(...)] with private; <-- partial view
8739 -- type D (..) is new P [(:::)] with null record; <-- full view
8740
8741 -- P's constraints on A's discriminants must statically match those
8742 -- imposed by (...).
8743
8744 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8745
8746 -- The full view of a private extension is handled exactly as described
8747 -- above. The model chose for the private view of a private extension is
8748 -- the same for what concerns discriminants (i.e. they receive the same
8749 -- treatment as in the tagged case). However, the private view of the
8750 -- private extension always inherits the components of the parent base,
8751 -- without replacing any discriminant reference. Strictly speaking this is
8752 -- incorrect. However, Gigi never uses this view to generate code so this
8753 -- is a purely semantic issue. In theory, a set of transformations similar
8754 -- to those given in 5. and 6. above could be applied to private views of
8755 -- private extensions to have the same model of component inheritance as
8756 -- for non private extensions. However, this is not done because it would
8757 -- further complicate private type processing. Semantically speaking, this
8758 -- leaves us in an uncomfortable situation. As an example consider:
8759
8760 -- package Pack is
8761 -- type R (D : integer) is tagged record
8762 -- S : String (1 .. D);
8763 -- end record;
8764 -- procedure P (X : R);
8765 -- type T is new R (1) with private;
8766 -- private
8767 -- type T is new R (1) with null record;
8768 -- end;
8769
8770 -- This is transformed into:
8771
8772 -- package Pack is
8773 -- type R (D : integer) is tagged record
8774 -- S : String (1 .. D);
8775 -- end record;
8776 -- procedure P (X : R);
8777 -- type T is new R (1) with private;
8778 -- private
8779 -- type BaseT is new R with null record;
8780 -- subtype T is BaseT (1);
8781 -- end;
8782
8783 -- (strictly speaking the above is incorrect Ada)
8784
8785 -- From the semantic standpoint the private view of private extension T
8786 -- should be flagged as constrained since one can clearly have
8787 --
8788 -- Obj : T;
8789 --
8790 -- in a unit withing Pack. However, when deriving subprograms for the
8791 -- private view of private extension T, T must be seen as unconstrained
8792 -- since T has discriminants (this is a constraint of the current
8793 -- subprogram derivation model). Thus, when processing the private view of
8794 -- a private extension such as T, we first mark T as unconstrained, we
8795 -- process it, we perform program derivation and just before returning from
8796 -- Build_Derived_Record_Type we mark T as constrained.
8797
8798 -- ??? Are there are other uncomfortable cases that we will have to
8799 -- deal with.
8800
8801 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8802
8803 -- Types that are derived from a visible record type and have a private
8804 -- extension present other peculiarities. They behave mostly like private
8805 -- types, but if they have primitive operations defined, these will not
8806 -- have the proper signatures for further inheritance, because other
8807 -- primitive operations will use the implicit base that we define for
8808 -- private derivations below. This affect subprogram inheritance (see
8809 -- Derive_Subprograms for details). We also derive the implicit base from
8810 -- the base type of the full view, so that the implicit base is a record
8811 -- type and not another private type, This avoids infinite loops.
8812
8813 procedure Build_Derived_Record_Type
8814 (N : Node_Id;
8815 Parent_Type : Entity_Id;
8816 Derived_Type : Entity_Id;
8817 Derive_Subps : Boolean := True)
8818 is
8819 Discriminant_Specs : constant Boolean :=
8820 Present (Discriminant_Specifications (N));
8821 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8822 Loc : constant Source_Ptr := Sloc (N);
8823 Private_Extension : constant Boolean :=
8824 Nkind (N) = N_Private_Extension_Declaration;
8825 Assoc_List : Elist_Id;
8826 Constraint_Present : Boolean;
8827 Constrs : Elist_Id;
8828 Discrim : Entity_Id;
8829 Indic : Node_Id;
8830 Inherit_Discrims : Boolean := False;
8831 Last_Discrim : Entity_Id;
8832 New_Base : Entity_Id;
8833 New_Decl : Node_Id;
8834 New_Discrs : Elist_Id;
8835 New_Indic : Node_Id;
8836 Parent_Base : Entity_Id;
8837 Save_Etype : Entity_Id;
8838 Save_Discr_Constr : Elist_Id;
8839 Save_Next_Entity : Entity_Id;
8840 Type_Def : Node_Id;
8841
8842 Discs : Elist_Id := New_Elmt_List;
8843 -- An empty Discs list means that there were no constraints in the
8844 -- subtype indication or that there was an error processing it.
8845
8846 procedure Check_Generic_Ancestors;
8847 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8848 -- cannot be declared at a deeper level than its parent type is
8849 -- removed. The check on derivation within a generic body is also
8850 -- relaxed, but there's a restriction that a derived tagged type
8851 -- cannot be declared in a generic body if it's derived directly
8852 -- or indirectly from a formal type of that generic. This applies
8853 -- to progenitors as well.
8854
8855 -----------------------------
8856 -- Check_Generic_Ancestors --
8857 -----------------------------
8858
8859 procedure Check_Generic_Ancestors is
8860 Ancestor_Type : Entity_Id;
8861 Intf_List : List_Id;
8862 Intf_Name : Node_Id;
8863
8864 procedure Check_Ancestor;
8865 -- For parent and progenitors.
8866
8867 --------------------
8868 -- Check_Ancestor --
8869 --------------------
8870
8871 procedure Check_Ancestor is
8872 begin
8873 -- If the derived type does have a formal type as an ancestor
8874 -- then it's an error if the derived type is declared within
8875 -- the body of the generic unit that declares the formal type
8876 -- in its generic formal part. It's sufficient to check whether
8877 -- the ancestor type is declared inside the same generic body
8878 -- as the derived type (such as within a nested generic spec),
8879 -- in which case the derivation is legal. If the formal type is
8880 -- declared outside of that generic body, then it's certain
8881 -- that the derived type is declared within the generic body
8882 -- of the generic unit declaring the formal type.
8883
8884 if Is_Generic_Type (Ancestor_Type)
8885 and then Enclosing_Generic_Body (Ancestor_Type) /=
8886 Enclosing_Generic_Body (Derived_Type)
8887 then
8888 Error_Msg_NE
8889 ("ancestor type& is formal type of enclosing"
8890 & " generic unit (RM 3.9.1 (4/2))",
8891 Indic, Ancestor_Type);
8892 end if;
8893 end Check_Ancestor;
8894
8895 begin
8896 if Nkind (N) = N_Private_Extension_Declaration then
8897 Intf_List := Interface_List (N);
8898 else
8899 Intf_List := Interface_List (Type_Definition (N));
8900 end if;
8901
8902 if Present (Enclosing_Generic_Body (Derived_Type)) then
8903 Ancestor_Type := Parent_Type;
8904
8905 while not Is_Generic_Type (Ancestor_Type)
8906 and then Etype (Ancestor_Type) /= Ancestor_Type
8907 loop
8908 Ancestor_Type := Etype (Ancestor_Type);
8909 end loop;
8910
8911 Check_Ancestor;
8912
8913 if Present (Intf_List) then
8914 Intf_Name := First (Intf_List);
8915 while Present (Intf_Name) loop
8916 Ancestor_Type := Entity (Intf_Name);
8917 Check_Ancestor;
8918 Next (Intf_Name);
8919 end loop;
8920 end if;
8921 end if;
8922 end Check_Generic_Ancestors;
8923
8924 -- Start of processing for Build_Derived_Record_Type
8925
8926 begin
8927 if Ekind (Parent_Type) = E_Record_Type_With_Private
8928 and then Present (Full_View (Parent_Type))
8929 and then Has_Discriminants (Parent_Type)
8930 then
8931 Parent_Base := Base_Type (Full_View (Parent_Type));
8932 else
8933 Parent_Base := Base_Type (Parent_Type);
8934 end if;
8935
8936 -- If the parent type is declared as a subtype of another private
8937 -- type with inherited discriminants, its generated base type is
8938 -- itself a record subtype. To further inherit the constraint we
8939 -- need to use its own base to have an unconstrained type on which
8940 -- to apply the inherited constraint.
8941
8942 if Ekind (Parent_Base) = E_Record_Subtype then
8943 Parent_Base := Base_Type (Parent_Base);
8944 end if;
8945
8946 -- AI05-0115: if this is a derivation from a private type in some
8947 -- other scope that may lead to invisible components for the derived
8948 -- type, mark it accordingly.
8949
8950 if Is_Private_Type (Parent_Type) then
8951 if Scope (Parent_Base) = Scope (Derived_Type) then
8952 null;
8953
8954 elsif In_Open_Scopes (Scope (Parent_Base))
8955 and then In_Private_Part (Scope (Parent_Base))
8956 then
8957 null;
8958
8959 else
8960 Set_Has_Private_Ancestor (Derived_Type);
8961 end if;
8962
8963 else
8964 Set_Has_Private_Ancestor
8965 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8966 end if;
8967
8968 -- Before we start the previously documented transformations, here is
8969 -- little fix for size and alignment of tagged types. Normally when we
8970 -- derive type D from type P, we copy the size and alignment of P as the
8971 -- default for D, and in the absence of explicit representation clauses
8972 -- for D, the size and alignment are indeed the same as the parent.
8973
8974 -- But this is wrong for tagged types, since fields may be added, and
8975 -- the default size may need to be larger, and the default alignment may
8976 -- need to be larger.
8977
8978 -- We therefore reset the size and alignment fields in the tagged case.
8979 -- Note that the size and alignment will in any case be at least as
8980 -- large as the parent type (since the derived type has a copy of the
8981 -- parent type in the _parent field)
8982
8983 -- The type is also marked as being tagged here, which is needed when
8984 -- processing components with a self-referential anonymous access type
8985 -- in the call to Check_Anonymous_Access_Components below. Note that
8986 -- this flag is also set later on for completeness.
8987
8988 if Is_Tagged then
8989 Set_Is_Tagged_Type (Derived_Type);
8990 Reinit_Size_Align (Derived_Type);
8991 end if;
8992
8993 -- STEP 0a: figure out what kind of derived type declaration we have
8994
8995 if Private_Extension then
8996 Type_Def := N;
8997 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
8998 Set_Default_SSO (Derived_Type);
8999 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9000
9001 else
9002 Type_Def := Type_Definition (N);
9003
9004 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9005 -- Parent_Base can be a private type or private extension. However,
9006 -- for tagged types with an extension the newly added fields are
9007 -- visible and hence the Derived_Type is always an E_Record_Type.
9008 -- (except that the parent may have its own private fields).
9009 -- For untagged types we preserve the Ekind of the Parent_Base.
9010
9011 if Present (Record_Extension_Part (Type_Def)) then
9012 Mutate_Ekind (Derived_Type, E_Record_Type);
9013 Set_Default_SSO (Derived_Type);
9014 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9015
9016 -- Create internal access types for components with anonymous
9017 -- access types.
9018
9019 if Ada_Version >= Ada_2005 then
9020 Check_Anonymous_Access_Components
9021 (N, Derived_Type, Derived_Type,
9022 Component_List (Record_Extension_Part (Type_Def)));
9023 end if;
9024
9025 else
9026 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9027 end if;
9028 end if;
9029
9030 -- Indic can either be an N_Identifier if the subtype indication
9031 -- contains no constraint or an N_Subtype_Indication if the subtype
9032 -- indication has a constraint. In either case it can include an
9033 -- interface list.
9034
9035 Indic := Subtype_Indication (Type_Def);
9036 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9037
9038 -- Check that the type has visible discriminants. The type may be
9039 -- a private type with unknown discriminants whose full view has
9040 -- discriminants which are invisible.
9041
9042 if Constraint_Present then
9043 if not Has_Discriminants (Parent_Base)
9044 or else
9045 (Has_Unknown_Discriminants (Parent_Base)
9046 and then Is_Private_Type (Parent_Base))
9047 then
9048 Error_Msg_N
9049 ("invalid constraint: type has no discriminant",
9050 Constraint (Indic));
9051
9052 Constraint_Present := False;
9053 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9054
9055 elsif Is_Constrained (Parent_Type) then
9056 Error_Msg_N
9057 ("invalid constraint: parent type is already constrained",
9058 Constraint (Indic));
9059
9060 Constraint_Present := False;
9061 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9062 end if;
9063 end if;
9064
9065 -- STEP 0b: If needed, apply transformation given in point 5. above
9066
9067 if not Private_Extension
9068 and then Has_Discriminants (Parent_Type)
9069 and then not Discriminant_Specs
9070 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9071 then
9072 -- First, we must analyze the constraint (see comment in point 5.)
9073 -- The constraint may come from the subtype indication of the full
9074 -- declaration.
9075
9076 if Constraint_Present then
9077 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9078
9079 -- If there is no explicit constraint, there might be one that is
9080 -- inherited from a constrained parent type. In that case verify that
9081 -- it conforms to the constraint in the partial view. In perverse
9082 -- cases the parent subtypes of the partial and full view can have
9083 -- different constraints.
9084
9085 elsif Present (Stored_Constraint (Parent_Type)) then
9086 New_Discrs := Stored_Constraint (Parent_Type);
9087
9088 else
9089 New_Discrs := No_Elist;
9090 end if;
9091
9092 if Has_Discriminants (Derived_Type)
9093 and then Has_Private_Declaration (Derived_Type)
9094 and then Present (Discriminant_Constraint (Derived_Type))
9095 and then Present (New_Discrs)
9096 then
9097 -- Verify that constraints of the full view statically match
9098 -- those given in the partial view.
9099
9100 declare
9101 C1, C2 : Elmt_Id;
9102
9103 begin
9104 C1 := First_Elmt (New_Discrs);
9105 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9106 while Present (C1) and then Present (C2) loop
9107 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9108 or else
9109 (Is_OK_Static_Expression (Node (C1))
9110 and then Is_OK_Static_Expression (Node (C2))
9111 and then
9112 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9113 then
9114 null;
9115
9116 else
9117 if Constraint_Present then
9118 Error_Msg_N
9119 ("constraint not conformant to previous declaration",
9120 Node (C1));
9121 else
9122 Error_Msg_N
9123 ("constraint of full view is incompatible "
9124 & "with partial view", N);
9125 end if;
9126 end if;
9127
9128 Next_Elmt (C1);
9129 Next_Elmt (C2);
9130 end loop;
9131 end;
9132 end if;
9133
9134 -- Insert and analyze the declaration for the unconstrained base type
9135
9136 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9137
9138 New_Decl :=
9139 Make_Full_Type_Declaration (Loc,
9140 Defining_Identifier => New_Base,
9141 Type_Definition =>
9142 Make_Derived_Type_Definition (Loc,
9143 Abstract_Present => Abstract_Present (Type_Def),
9144 Limited_Present => Limited_Present (Type_Def),
9145 Subtype_Indication =>
9146 New_Occurrence_Of (Parent_Base, Loc),
9147 Record_Extension_Part =>
9148 Relocate_Node (Record_Extension_Part (Type_Def)),
9149 Interface_List => Interface_List (Type_Def)));
9150
9151 Set_Parent (New_Decl, Parent (N));
9152 Mark_Rewrite_Insertion (New_Decl);
9153 Insert_Before (N, New_Decl);
9154
9155 -- In the extension case, make sure ancestor is frozen appropriately
9156 -- (see also non-discriminated case below).
9157
9158 if Present (Record_Extension_Part (Type_Def))
9159 or else Is_Interface (Parent_Base)
9160 then
9161 Freeze_Before (New_Decl, Parent_Type);
9162 end if;
9163
9164 -- Note that this call passes False for the Derive_Subps parameter
9165 -- because subprogram derivation is deferred until after creating
9166 -- the subtype (see below).
9167
9168 Build_Derived_Type
9169 (New_Decl, Parent_Base, New_Base,
9170 Is_Completion => False, Derive_Subps => False);
9171
9172 -- ??? This needs re-examination to determine whether the
9173 -- above call can simply be replaced by a call to Analyze.
9174
9175 Set_Analyzed (New_Decl);
9176
9177 -- Insert and analyze the declaration for the constrained subtype
9178
9179 if Constraint_Present then
9180 New_Indic :=
9181 Make_Subtype_Indication (Loc,
9182 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9183 Constraint => Relocate_Node (Constraint (Indic)));
9184
9185 else
9186 declare
9187 Constr_List : constant List_Id := New_List;
9188 C : Elmt_Id;
9189 Expr : Node_Id;
9190
9191 begin
9192 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9193 while Present (C) loop
9194 Expr := Node (C);
9195
9196 -- It is safe here to call New_Copy_Tree since we called
9197 -- Force_Evaluation on each constraint previously
9198 -- in Build_Discriminant_Constraints.
9199
9200 Append (New_Copy_Tree (Expr), To => Constr_List);
9201
9202 Next_Elmt (C);
9203 end loop;
9204
9205 New_Indic :=
9206 Make_Subtype_Indication (Loc,
9207 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9208 Constraint =>
9209 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9210 end;
9211 end if;
9212
9213 Rewrite (N,
9214 Make_Subtype_Declaration (Loc,
9215 Defining_Identifier => Derived_Type,
9216 Subtype_Indication => New_Indic));
9217
9218 Analyze (N);
9219
9220 -- Derivation of subprograms must be delayed until the full subtype
9221 -- has been established, to ensure proper overriding of subprograms
9222 -- inherited by full types. If the derivations occurred as part of
9223 -- the call to Build_Derived_Type above, then the check for type
9224 -- conformance would fail because earlier primitive subprograms
9225 -- could still refer to the full type prior the change to the new
9226 -- subtype and hence would not match the new base type created here.
9227 -- Subprograms are not derived, however, when Derive_Subps is False
9228 -- (since otherwise there could be redundant derivations).
9229
9230 if Derive_Subps then
9231 Derive_Subprograms (Parent_Type, Derived_Type);
9232 end if;
9233
9234 -- For tagged types the Discriminant_Constraint of the new base itype
9235 -- is inherited from the first subtype so that no subtype conformance
9236 -- problem arise when the first subtype overrides primitive
9237 -- operations inherited by the implicit base type.
9238
9239 if Is_Tagged then
9240 Set_Discriminant_Constraint
9241 (New_Base, Discriminant_Constraint (Derived_Type));
9242 end if;
9243
9244 return;
9245 end if;
9246
9247 -- If we get here Derived_Type will have no discriminants or it will be
9248 -- a discriminated unconstrained base type.
9249
9250 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9251
9252 if Is_Tagged then
9253
9254 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9255 -- The declaration of a specific descendant of an interface type
9256 -- freezes the interface type (RM 13.14).
9257
9258 if not Private_Extension or else Is_Interface (Parent_Base) then
9259 Freeze_Before (N, Parent_Type);
9260 end if;
9261
9262 if Ada_Version >= Ada_2005 then
9263 Check_Generic_Ancestors;
9264
9265 elsif Type_Access_Level (Derived_Type) /=
9266 Type_Access_Level (Parent_Type)
9267 and then not Is_Generic_Type (Derived_Type)
9268 then
9269 if Is_Controlled (Parent_Type) then
9270 Error_Msg_N
9271 ("controlled type must be declared at the library level",
9272 Indic);
9273 else
9274 Error_Msg_N
9275 ("type extension at deeper accessibility level than parent",
9276 Indic);
9277 end if;
9278
9279 else
9280 declare
9281 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9282 begin
9283 if Present (GB)
9284 and then GB /= Enclosing_Generic_Body (Parent_Base)
9285 then
9286 Error_Msg_NE
9287 ("parent type of& must not be outside generic body"
9288 & " (RM 3.9.1(4))",
9289 Indic, Derived_Type);
9290 end if;
9291 end;
9292 end if;
9293 end if;
9294
9295 -- Ada 2005 (AI-251)
9296
9297 if Ada_Version >= Ada_2005 and then Is_Tagged then
9298
9299 -- "The declaration of a specific descendant of an interface type
9300 -- freezes the interface type" (RM 13.14).
9301
9302 declare
9303 Iface : Node_Id;
9304 begin
9305 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9306 Iface := First (Interface_List (Type_Def));
9307 while Present (Iface) loop
9308 Freeze_Before (N, Etype (Iface));
9309 Next (Iface);
9310 end loop;
9311 end if;
9312 end;
9313 end if;
9314
9315 -- STEP 1b : preliminary cleanup of the full view of private types
9316
9317 -- If the type is already marked as having discriminants, then it's the
9318 -- completion of a private type or private extension and we need to
9319 -- retain the discriminants from the partial view if the current
9320 -- declaration has Discriminant_Specifications so that we can verify
9321 -- conformance. However, we must remove any existing components that
9322 -- were inherited from the parent (and attached in Copy_And_Swap)
9323 -- because the full type inherits all appropriate components anyway, and
9324 -- we do not want the partial view's components interfering.
9325
9326 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9327 Discrim := First_Discriminant (Derived_Type);
9328 loop
9329 Last_Discrim := Discrim;
9330 Next_Discriminant (Discrim);
9331 exit when No (Discrim);
9332 end loop;
9333
9334 Set_Last_Entity (Derived_Type, Last_Discrim);
9335
9336 -- In all other cases wipe out the list of inherited components (even
9337 -- inherited discriminants), it will be properly rebuilt here.
9338
9339 else
9340 Set_First_Entity (Derived_Type, Empty);
9341 Set_Last_Entity (Derived_Type, Empty);
9342 end if;
9343
9344 -- STEP 1c: Initialize some flags for the Derived_Type
9345
9346 -- The following flags must be initialized here so that
9347 -- Process_Discriminants can check that discriminants of tagged types do
9348 -- not have a default initial value and that access discriminants are
9349 -- only specified for limited records. For completeness, these flags are
9350 -- also initialized along with all the other flags below.
9351
9352 -- AI-419: Limitedness is not inherited from an interface parent, so to
9353 -- be limited in that case the type must be explicitly declared as
9354 -- limited. However, task and protected interfaces are always limited.
9355
9356 if Limited_Present (Type_Def) then
9357 Set_Is_Limited_Record (Derived_Type);
9358
9359 elsif Is_Limited_Record (Parent_Type)
9360 or else (Present (Full_View (Parent_Type))
9361 and then Is_Limited_Record (Full_View (Parent_Type)))
9362 then
9363 if not Is_Interface (Parent_Type)
9364 or else Is_Concurrent_Interface (Parent_Type)
9365 then
9366 Set_Is_Limited_Record (Derived_Type);
9367 end if;
9368 end if;
9369
9370 -- STEP 2a: process discriminants of derived type if any
9371
9372 Push_Scope (Derived_Type);
9373
9374 if Discriminant_Specs then
9375 Set_Has_Unknown_Discriminants (Derived_Type, False);
9376
9377 -- The following call initializes fields Has_Discriminants and
9378 -- Discriminant_Constraint, unless we are processing the completion
9379 -- of a private type declaration.
9380
9381 Check_Or_Process_Discriminants (N, Derived_Type);
9382
9383 -- For untagged types, the constraint on the Parent_Type must be
9384 -- present and is used to rename the discriminants.
9385
9386 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9387 Error_Msg_N ("untagged parent must have discriminants", Indic);
9388
9389 elsif not Is_Tagged and then not Constraint_Present then
9390 Error_Msg_N
9391 ("discriminant constraint needed for derived untagged records",
9392 Indic);
9393
9394 -- Otherwise the parent subtype must be constrained unless we have a
9395 -- private extension.
9396
9397 elsif not Constraint_Present
9398 and then not Private_Extension
9399 and then not Is_Constrained (Parent_Type)
9400 then
9401 Error_Msg_N
9402 ("unconstrained type not allowed in this context", Indic);
9403
9404 elsif Constraint_Present then
9405 -- The following call sets the field Corresponding_Discriminant
9406 -- for the discriminants in the Derived_Type.
9407
9408 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9409
9410 -- For untagged types all new discriminants must rename
9411 -- discriminants in the parent. For private extensions new
9412 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9413
9414 Discrim := First_Discriminant (Derived_Type);
9415 while Present (Discrim) loop
9416 if not Is_Tagged
9417 and then No (Corresponding_Discriminant (Discrim))
9418 then
9419 Error_Msg_N
9420 ("new discriminants must constrain old ones", Discrim);
9421
9422 elsif Private_Extension
9423 and then Present (Corresponding_Discriminant (Discrim))
9424 then
9425 Error_Msg_N
9426 ("only static constraints allowed for parent"
9427 & " discriminants in the partial view", Indic);
9428 exit;
9429 end if;
9430
9431 -- If a new discriminant is used in the constraint, then its
9432 -- subtype must be statically compatible with the subtype of
9433 -- the parent discriminant (RM 3.7(15)).
9434
9435 if Present (Corresponding_Discriminant (Discrim)) then
9436 Check_Constraining_Discriminant
9437 (Discrim, Corresponding_Discriminant (Discrim));
9438 end if;
9439
9440 Next_Discriminant (Discrim);
9441 end loop;
9442
9443 -- Check whether the constraints of the full view statically
9444 -- match those imposed by the parent subtype [7.3(13)].
9445
9446 if Present (Stored_Constraint (Derived_Type)) then
9447 declare
9448 C1, C2 : Elmt_Id;
9449
9450 begin
9451 C1 := First_Elmt (Discs);
9452 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9453 while Present (C1) and then Present (C2) loop
9454 if not
9455 Fully_Conformant_Expressions (Node (C1), Node (C2))
9456 then
9457 Error_Msg_N
9458 ("not conformant with previous declaration",
9459 Node (C1));
9460 end if;
9461
9462 Next_Elmt (C1);
9463 Next_Elmt (C2);
9464 end loop;
9465 end;
9466 end if;
9467 end if;
9468
9469 -- STEP 2b: No new discriminants, inherit discriminants if any
9470
9471 else
9472 if Private_Extension then
9473 Set_Has_Unknown_Discriminants
9474 (Derived_Type,
9475 Has_Unknown_Discriminants (Parent_Type)
9476 or else Unknown_Discriminants_Present (N));
9477
9478 -- The partial view of the parent may have unknown discriminants,
9479 -- but if the full view has discriminants and the parent type is
9480 -- in scope they must be inherited.
9481
9482 elsif Has_Unknown_Discriminants (Parent_Type)
9483 and then
9484 (not Has_Discriminants (Parent_Type)
9485 or else not In_Open_Scopes (Scope (Parent_Base)))
9486 then
9487 Set_Has_Unknown_Discriminants (Derived_Type);
9488 end if;
9489
9490 if not Has_Unknown_Discriminants (Derived_Type)
9491 and then not Has_Unknown_Discriminants (Parent_Base)
9492 and then Has_Discriminants (Parent_Type)
9493 then
9494 Inherit_Discrims := True;
9495 Set_Has_Discriminants
9496 (Derived_Type, True);
9497 Set_Discriminant_Constraint
9498 (Derived_Type, Discriminant_Constraint (Parent_Base));
9499 end if;
9500
9501 -- The following test is true for private types (remember
9502 -- transformation 5. is not applied to those) and in an error
9503 -- situation.
9504
9505 if Constraint_Present then
9506 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9507 end if;
9508
9509 -- For now mark a new derived type as constrained only if it has no
9510 -- discriminants. At the end of Build_Derived_Record_Type we properly
9511 -- set this flag in the case of private extensions. See comments in
9512 -- point 9. just before body of Build_Derived_Record_Type.
9513
9514 Set_Is_Constrained
9515 (Derived_Type,
9516 not (Inherit_Discrims
9517 or else Has_Unknown_Discriminants (Derived_Type)));
9518 end if;
9519
9520 -- STEP 3: initialize fields of derived type
9521
9522 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9523 Set_Stored_Constraint (Derived_Type, No_Elist);
9524
9525 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9526 -- but cannot be interfaces
9527
9528 if not Private_Extension
9529 and then Ekind (Derived_Type) /= E_Private_Type
9530 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9531 then
9532 if Interface_Present (Type_Def) then
9533 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9534 end if;
9535
9536 Set_Interfaces (Derived_Type, No_Elist);
9537 end if;
9538
9539 -- Fields inherited from the Parent_Type
9540
9541 Set_Has_Specified_Layout
9542 (Derived_Type, Has_Specified_Layout (Parent_Type));
9543 Set_Is_Limited_Composite
9544 (Derived_Type, Is_Limited_Composite (Parent_Type));
9545 Set_Is_Private_Composite
9546 (Derived_Type, Is_Private_Composite (Parent_Type));
9547
9548 if Is_Tagged_Type (Parent_Type) then
9549 Set_No_Tagged_Streams_Pragma
9550 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9551 end if;
9552
9553 -- Fields inherited from the Parent_Base
9554
9555 Set_Has_Controlled_Component
9556 (Derived_Type, Has_Controlled_Component (Parent_Base));
9557 Set_Has_Non_Standard_Rep
9558 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9559 Set_Has_Primitive_Operations
9560 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9561
9562 -- Set fields for private derived types
9563
9564 if Is_Private_Type (Derived_Type) then
9565 Set_Depends_On_Private (Derived_Type, True);
9566 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9567 end if;
9568
9569 -- Inherit fields for non-private types. If this is the completion of a
9570 -- derivation from a private type, the parent itself is private and the
9571 -- attributes come from its full view, which must be present.
9572
9573 if Is_Record_Type (Derived_Type) then
9574 declare
9575 Parent_Full : Entity_Id;
9576
9577 begin
9578 if Is_Private_Type (Parent_Base)
9579 and then not Is_Record_Type (Parent_Base)
9580 then
9581 Parent_Full := Full_View (Parent_Base);
9582 else
9583 Parent_Full := Parent_Base;
9584 end if;
9585
9586 Set_Component_Alignment
9587 (Derived_Type, Component_Alignment (Parent_Full));
9588 Set_C_Pass_By_Copy
9589 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9590 Set_Has_Complex_Representation
9591 (Derived_Type, Has_Complex_Representation (Parent_Full));
9592
9593 -- For untagged types, inherit the layout by default to avoid
9594 -- costly changes of representation for type conversions.
9595
9596 if not Is_Tagged then
9597 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9598 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9599 end if;
9600 end;
9601 end if;
9602
9603 -- When prefixed-call syntax is allowed for untagged types, initialize
9604 -- the list of primitive operations to an empty list.
9605
9606 if Extensions_Allowed and then not Is_Tagged then
9607 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9608 end if;
9609
9610 -- Set fields for tagged types
9611
9612 if Is_Tagged then
9613 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9614
9615 -- All tagged types defined in Ada.Finalization are controlled
9616
9617 if Chars (Scope (Derived_Type)) = Name_Finalization
9618 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9619 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9620 then
9621 Set_Is_Controlled_Active (Derived_Type);
9622 else
9623 Set_Is_Controlled_Active
9624 (Derived_Type, Is_Controlled_Active (Parent_Base));
9625 end if;
9626
9627 -- Minor optimization: there is no need to generate the class-wide
9628 -- entity associated with an underlying record view.
9629
9630 if not Is_Underlying_Record_View (Derived_Type) then
9631 Make_Class_Wide_Type (Derived_Type);
9632 end if;
9633
9634 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9635
9636 if Has_Discriminants (Derived_Type)
9637 and then Constraint_Present
9638 then
9639 Set_Stored_Constraint
9640 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9641 end if;
9642
9643 if Ada_Version >= Ada_2005 then
9644 declare
9645 Ifaces_List : Elist_Id;
9646
9647 begin
9648 -- Checks rules 3.9.4 (13/2 and 14/2)
9649
9650 if Comes_From_Source (Derived_Type)
9651 and then not Is_Private_Type (Derived_Type)
9652 and then Is_Interface (Parent_Type)
9653 and then not Is_Interface (Derived_Type)
9654 then
9655 if Is_Task_Interface (Parent_Type) then
9656 Error_Msg_N
9657 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9658 Derived_Type);
9659
9660 elsif Is_Protected_Interface (Parent_Type) then
9661 Error_Msg_N
9662 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9663 Derived_Type);
9664 end if;
9665 end if;
9666
9667 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9668
9669 Check_Interfaces (N, Type_Def);
9670
9671 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9672 -- not already in the parents.
9673
9674 Collect_Interfaces
9675 (T => Derived_Type,
9676 Ifaces_List => Ifaces_List,
9677 Exclude_Parents => True);
9678
9679 Set_Interfaces (Derived_Type, Ifaces_List);
9680
9681 -- If the derived type is the anonymous type created for
9682 -- a declaration whose parent has a constraint, propagate
9683 -- the interface list to the source type. This must be done
9684 -- prior to the completion of the analysis of the source type
9685 -- because the components in the extension may contain current
9686 -- instances whose legality depends on some ancestor.
9687
9688 if Is_Itype (Derived_Type) then
9689 declare
9690 Def : constant Node_Id :=
9691 Associated_Node_For_Itype (Derived_Type);
9692 begin
9693 if Present (Def)
9694 and then Nkind (Def) = N_Full_Type_Declaration
9695 then
9696 Set_Interfaces
9697 (Defining_Identifier (Def), Ifaces_List);
9698 end if;
9699 end;
9700 end if;
9701
9702 -- A type extension is automatically Ghost when one of its
9703 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9704 -- also inherited when the parent type is Ghost, but this is
9705 -- done in Build_Derived_Type as the mechanism also handles
9706 -- untagged derivations.
9707
9708 if Implements_Ghost_Interface (Derived_Type) then
9709 Set_Is_Ghost_Entity (Derived_Type);
9710 end if;
9711 end;
9712 end if;
9713 end if;
9714
9715 -- STEP 4: Inherit components from the parent base and constrain them.
9716 -- Apply the second transformation described in point 6. above.
9717
9718 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9719 or else not Has_Discriminants (Parent_Type)
9720 or else not Is_Constrained (Parent_Type)
9721 then
9722 Constrs := Discs;
9723 else
9724 Constrs := Discriminant_Constraint (Parent_Type);
9725 end if;
9726
9727 Assoc_List :=
9728 Inherit_Components
9729 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9730
9731 -- STEP 5a: Copy the parent record declaration for untagged types
9732
9733 Set_Has_Implicit_Dereference
9734 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9735
9736 if not Is_Tagged then
9737
9738 -- Discriminant_Constraint (Derived_Type) has been properly
9739 -- constructed. Save it and temporarily set it to Empty because we
9740 -- do not want the call to New_Copy_Tree below to mess this list.
9741
9742 if Has_Discriminants (Derived_Type) then
9743 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9744 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9745 else
9746 Save_Discr_Constr := No_Elist;
9747 end if;
9748
9749 -- Save the Etype field of Derived_Type. It is correctly set now,
9750 -- but the call to New_Copy tree may remap it to point to itself,
9751 -- which is not what we want. Ditto for the Next_Entity field.
9752
9753 Save_Etype := Etype (Derived_Type);
9754 Save_Next_Entity := Next_Entity (Derived_Type);
9755
9756 -- Assoc_List maps all stored discriminants in the Parent_Base to
9757 -- stored discriminants in the Derived_Type. It is fundamental that
9758 -- no types or itypes with discriminants other than the stored
9759 -- discriminants appear in the entities declared inside
9760 -- Derived_Type, since the back end cannot deal with it.
9761
9762 New_Decl :=
9763 New_Copy_Tree
9764 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9765 Copy_Dimensions_Of_Components (Derived_Type);
9766
9767 -- Restore the fields saved prior to the New_Copy_Tree call
9768 -- and compute the stored constraint.
9769
9770 Set_Etype (Derived_Type, Save_Etype);
9771 Link_Entities (Derived_Type, Save_Next_Entity);
9772
9773 if Has_Discriminants (Derived_Type) then
9774 Set_Discriminant_Constraint
9775 (Derived_Type, Save_Discr_Constr);
9776 Set_Stored_Constraint
9777 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9778
9779 Replace_Discriminants (Derived_Type, New_Decl);
9780 end if;
9781
9782 -- Insert the new derived type declaration
9783
9784 Rewrite (N, New_Decl);
9785
9786 -- STEP 5b: Complete the processing for record extensions in generics
9787
9788 -- There is no completion for record extensions declared in the
9789 -- parameter part of a generic, so we need to complete processing for
9790 -- these generic record extensions here. The Record_Type_Definition call
9791 -- will change the Ekind of the components from E_Void to E_Component.
9792
9793 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9794 Record_Type_Definition (Empty, Derived_Type);
9795
9796 -- STEP 5c: Process the record extension for non private tagged types
9797
9798 elsif not Private_Extension then
9799 Expand_Record_Extension (Derived_Type, Type_Def);
9800
9801 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9802 -- implemented interfaces if we are in expansion mode
9803
9804 if Expander_Active
9805 and then Has_Interfaces (Derived_Type)
9806 then
9807 Add_Interface_Tag_Components (N, Derived_Type);
9808 end if;
9809
9810 -- Analyze the record extension
9811
9812 Record_Type_Definition
9813 (Record_Extension_Part (Type_Def), Derived_Type);
9814 end if;
9815
9816 End_Scope;
9817
9818 -- Nothing else to do if there is an error in the derivation.
9819 -- An unusual case: the full view may be derived from a type in an
9820 -- instance, when the partial view was used illegally as an actual
9821 -- in that instance, leading to a circular definition.
9822
9823 if Etype (Derived_Type) = Any_Type
9824 or else Etype (Parent_Type) = Derived_Type
9825 then
9826 return;
9827 end if;
9828
9829 -- Set delayed freeze and then derive subprograms, we need to do
9830 -- this in this order so that derived subprograms inherit the
9831 -- derived freeze if necessary.
9832
9833 Set_Has_Delayed_Freeze (Derived_Type);
9834
9835 if Derive_Subps then
9836 Derive_Subprograms (Parent_Type, Derived_Type);
9837 end if;
9838
9839 -- If we have a private extension which defines a constrained derived
9840 -- type mark as constrained here after we have derived subprograms. See
9841 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9842
9843 if Private_Extension and then Inherit_Discrims then
9844 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9845 Set_Is_Constrained (Derived_Type, True);
9846 Set_Discriminant_Constraint (Derived_Type, Discs);
9847
9848 elsif Is_Constrained (Parent_Type) then
9849 Set_Is_Constrained
9850 (Derived_Type, True);
9851 Set_Discriminant_Constraint
9852 (Derived_Type, Discriminant_Constraint (Parent_Type));
9853 end if;
9854 end if;
9855
9856 -- Update the class-wide type, which shares the now-completed entity
9857 -- list with its specific type. In case of underlying record views,
9858 -- we do not generate the corresponding class wide entity.
9859
9860 if Is_Tagged
9861 and then not Is_Underlying_Record_View (Derived_Type)
9862 then
9863 Set_First_Entity
9864 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9865 Set_Last_Entity
9866 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9867 end if;
9868
9869 Check_Function_Writable_Actuals (N);
9870 end Build_Derived_Record_Type;
9871
9872 ------------------------
9873 -- Build_Derived_Type --
9874 ------------------------
9875
9876 procedure Build_Derived_Type
9877 (N : Node_Id;
9878 Parent_Type : Entity_Id;
9879 Derived_Type : Entity_Id;
9880 Is_Completion : Boolean;
9881 Derive_Subps : Boolean := True)
9882 is
9883 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9884
9885 begin
9886 -- Set common attributes
9887
9888 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
9889 and then Ekind (Parent_Base) in Modular_Integer_Kind | Array_Kind
9890 then
9891 Reinit_Field_To_Zero (Derived_Type, F_Stored_Constraint);
9892 end if;
9893
9894 Set_Scope (Derived_Type, Current_Scope);
9895 Set_Etype (Derived_Type, Parent_Base);
9896 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9897 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9898
9899 Set_Size_Info (Derived_Type, Parent_Type);
9900 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
9901
9902 Set_Is_Controlled_Active
9903 (Derived_Type, Is_Controlled_Active (Parent_Type));
9904
9905 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9906 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9907 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9908
9909 if Is_Tagged_Type (Derived_Type) then
9910 Set_No_Tagged_Streams_Pragma
9911 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9912 end if;
9913
9914 -- If the parent has primitive routines and may have not-seen-yet aspect
9915 -- specifications (e.g., a Pack pragma), then set the derived type link
9916 -- in order to later diagnose "early derivation" issues. If in different
9917 -- compilation units, then "early derivation" cannot be an issue (and we
9918 -- don't like interunit references that go in the opposite direction of
9919 -- semantic dependencies).
9920
9921 if Has_Primitive_Operations (Parent_Type)
9922 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9923 Enclosing_Comp_Unit_Node (Derived_Type)
9924 then
9925 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9926 end if;
9927
9928 -- If the parent type is a private subtype, the convention on the base
9929 -- type may be set in the private part, and not propagated to the
9930 -- subtype until later, so we obtain the convention from the base type.
9931
9932 Set_Convention (Derived_Type, Convention (Parent_Base));
9933
9934 if Is_Tagged_Type (Derived_Type)
9935 and then Present (Class_Wide_Type (Derived_Type))
9936 then
9937 Set_Convention (Class_Wide_Type (Derived_Type),
9938 Convention (Class_Wide_Type (Parent_Base)));
9939 end if;
9940
9941 -- Set SSO default for record or array type
9942
9943 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9944 and then Is_Base_Type (Derived_Type)
9945 then
9946 Set_Default_SSO (Derived_Type);
9947 end if;
9948
9949 -- A derived type inherits the Default_Initial_Condition pragma coming
9950 -- from any parent type within the derivation chain.
9951
9952 if Has_DIC (Parent_Type) then
9953 Set_Has_Inherited_DIC (Derived_Type);
9954 end if;
9955
9956 -- A derived type inherits any class-wide invariants coming from a
9957 -- parent type or an interface. Note that the invariant procedure of
9958 -- the parent type should not be inherited because the derived type may
9959 -- define invariants of its own.
9960
9961 if not Is_Interface (Derived_Type) then
9962 if Has_Inherited_Invariants (Parent_Type)
9963 or else Has_Inheritable_Invariants (Parent_Type)
9964 then
9965 Set_Has_Inherited_Invariants (Derived_Type);
9966
9967 elsif Is_Concurrent_Type (Derived_Type)
9968 or else Is_Tagged_Type (Derived_Type)
9969 then
9970 declare
9971 Iface : Entity_Id;
9972 Ifaces : Elist_Id;
9973 Iface_Elmt : Elmt_Id;
9974
9975 begin
9976 Collect_Interfaces
9977 (T => Derived_Type,
9978 Ifaces_List => Ifaces,
9979 Exclude_Parents => True);
9980
9981 if Present (Ifaces) then
9982 Iface_Elmt := First_Elmt (Ifaces);
9983 while Present (Iface_Elmt) loop
9984 Iface := Node (Iface_Elmt);
9985
9986 if Has_Inheritable_Invariants (Iface) then
9987 Set_Has_Inherited_Invariants (Derived_Type);
9988 exit;
9989 end if;
9990
9991 Next_Elmt (Iface_Elmt);
9992 end loop;
9993 end if;
9994 end;
9995 end if;
9996 end if;
9997
9998 -- We similarly inherit predicates. Note that for scalar derived types
9999 -- the predicate is inherited from the first subtype, and not from its
10000 -- (anonymous) base type.
10001
10002 if Has_Predicates (Parent_Type)
10003 or else Has_Predicates (First_Subtype (Parent_Type))
10004 then
10005 Set_Has_Predicates (Derived_Type);
10006 end if;
10007
10008 -- The derived type inherits representation clauses from the parent
10009 -- type, and from any interfaces.
10010
10011 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10012
10013 declare
10014 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10015 begin
10016 while Present (Iface) loop
10017 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10018 Next (Iface);
10019 end loop;
10020 end;
10021
10022 -- If the parent type has delayed rep aspects, then mark the derived
10023 -- type as possibly inheriting a delayed rep aspect.
10024
10025 if Has_Delayed_Rep_Aspects (Parent_Type) then
10026 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10027 end if;
10028
10029 -- A derived type becomes Ghost when its parent type is also Ghost
10030 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10031 -- directly inherited because the Ghost policy in effect may differ.
10032
10033 if Is_Ghost_Entity (Parent_Type) then
10034 Set_Is_Ghost_Entity (Derived_Type);
10035 end if;
10036
10037 -- Type dependent processing
10038
10039 case Ekind (Parent_Type) is
10040 when Numeric_Kind =>
10041 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10042
10043 when Array_Kind =>
10044 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10045
10046 when Class_Wide_Kind
10047 | E_Record_Subtype
10048 | E_Record_Type
10049 =>
10050 Build_Derived_Record_Type
10051 (N, Parent_Type, Derived_Type, Derive_Subps);
10052 return;
10053
10054 when Enumeration_Kind =>
10055 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10056
10057 when Access_Kind =>
10058 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10059
10060 when Incomplete_Or_Private_Kind =>
10061 Build_Derived_Private_Type
10062 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10063
10064 -- For discriminated types, the derivation includes deriving
10065 -- primitive operations. For others it is done below.
10066
10067 if Is_Tagged_Type (Parent_Type)
10068 or else Has_Discriminants (Parent_Type)
10069 or else (Present (Full_View (Parent_Type))
10070 and then Has_Discriminants (Full_View (Parent_Type)))
10071 then
10072 return;
10073 end if;
10074
10075 when Concurrent_Kind =>
10076 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10077
10078 when others =>
10079 raise Program_Error;
10080 end case;
10081
10082 -- Nothing more to do if some error occurred
10083
10084 if Etype (Derived_Type) = Any_Type then
10085 return;
10086 end if;
10087
10088 -- If not already set, initialize the derived type's list of primitive
10089 -- operations to an empty element list.
10090
10091 if not Present (Direct_Primitive_Operations (Derived_Type)) then
10092 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10093
10094 -- If Etype of the derived type is the base type (as opposed to
10095 -- a parent type) and doesn't have an associated list of primitive
10096 -- operations, then set the base type's primitive list to the
10097 -- derived type's list. The lists need to be shared in common
10098 -- between the two.
10099
10100 if Etype (Derived_Type) = Base_Type (Derived_Type)
10101 and then
10102 not Present (Direct_Primitive_Operations (Etype (Derived_Type)))
10103 then
10104 Set_Direct_Primitive_Operations
10105 (Etype (Derived_Type),
10106 Direct_Primitive_Operations (Derived_Type));
10107 end if;
10108 end if;
10109
10110 -- Set delayed freeze and then derive subprograms, we need to do this
10111 -- in this order so that derived subprograms inherit the derived freeze
10112 -- if necessary.
10113
10114 Set_Has_Delayed_Freeze (Derived_Type);
10115
10116 if Derive_Subps then
10117 Derive_Subprograms (Parent_Type, Derived_Type);
10118 end if;
10119
10120 Set_Has_Primitive_Operations
10121 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10122 end Build_Derived_Type;
10123
10124 -----------------------
10125 -- Build_Discriminal --
10126 -----------------------
10127
10128 procedure Build_Discriminal (Discrim : Entity_Id) is
10129 D_Minal : Entity_Id;
10130 CR_Disc : Entity_Id;
10131
10132 begin
10133 -- A discriminal has the same name as the discriminant
10134
10135 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10136
10137 Mutate_Ekind (D_Minal, E_In_Parameter);
10138 Set_Mechanism (D_Minal, Default_Mechanism);
10139 Set_Etype (D_Minal, Etype (Discrim));
10140 Set_Scope (D_Minal, Current_Scope);
10141 Set_Parent (D_Minal, Parent (Discrim));
10142
10143 Set_Discriminal (Discrim, D_Minal);
10144 Set_Discriminal_Link (D_Minal, Discrim);
10145
10146 -- For task types, build at once the discriminants of the corresponding
10147 -- record, which are needed if discriminants are used in entry defaults
10148 -- and in family bounds.
10149
10150 if Is_Concurrent_Type (Current_Scope)
10151 or else
10152 Is_Limited_Type (Current_Scope)
10153 then
10154 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10155
10156 Mutate_Ekind (CR_Disc, E_In_Parameter);
10157 Set_Mechanism (CR_Disc, Default_Mechanism);
10158 Set_Etype (CR_Disc, Etype (Discrim));
10159 Set_Scope (CR_Disc, Current_Scope);
10160 Set_Discriminal_Link (CR_Disc, Discrim);
10161 Set_CR_Discriminant (Discrim, CR_Disc);
10162 end if;
10163 end Build_Discriminal;
10164
10165 ------------------------------------
10166 -- Build_Discriminant_Constraints --
10167 ------------------------------------
10168
10169 function Build_Discriminant_Constraints
10170 (T : Entity_Id;
10171 Def : Node_Id;
10172 Derived_Def : Boolean := False) return Elist_Id
10173 is
10174 C : constant Node_Id := Constraint (Def);
10175 Nb_Discr : constant Nat := Number_Discriminants (T);
10176
10177 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10178 -- Saves the expression corresponding to a given discriminant in T
10179
10180 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10181 -- Return the Position number within array Discr_Expr of a discriminant
10182 -- D within the discriminant list of the discriminated type T.
10183
10184 procedure Process_Discriminant_Expression
10185 (Expr : Node_Id;
10186 D : Entity_Id);
10187 -- If this is a discriminant constraint on a partial view, do not
10188 -- generate an overflow check on the discriminant expression. The check
10189 -- will be generated when constraining the full view. Otherwise the
10190 -- backend creates duplicate symbols for the temporaries corresponding
10191 -- to the expressions to be checked, causing spurious assembler errors.
10192
10193 ------------------
10194 -- Pos_Of_Discr --
10195 ------------------
10196
10197 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10198 Disc : Entity_Id;
10199
10200 begin
10201 Disc := First_Discriminant (T);
10202 for J in Discr_Expr'Range loop
10203 if Disc = D then
10204 return J;
10205 end if;
10206
10207 Next_Discriminant (Disc);
10208 end loop;
10209
10210 -- Note: Since this function is called on discriminants that are
10211 -- known to belong to the discriminated type, falling through the
10212 -- loop with no match signals an internal compiler error.
10213
10214 raise Program_Error;
10215 end Pos_Of_Discr;
10216
10217 -------------------------------------
10218 -- Process_Discriminant_Expression --
10219 -------------------------------------
10220
10221 procedure Process_Discriminant_Expression
10222 (Expr : Node_Id;
10223 D : Entity_Id)
10224 is
10225 BDT : constant Entity_Id := Base_Type (Etype (D));
10226
10227 begin
10228 -- If this is a discriminant constraint on a partial view, do
10229 -- not generate an overflow on the discriminant expression. The
10230 -- check will be generated when constraining the full view.
10231
10232 if Is_Private_Type (T)
10233 and then Present (Full_View (T))
10234 then
10235 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10236 else
10237 Analyze_And_Resolve (Expr, BDT);
10238 end if;
10239 end Process_Discriminant_Expression;
10240
10241 -- Declarations local to Build_Discriminant_Constraints
10242
10243 Discr : Entity_Id;
10244 E : Entity_Id;
10245 Elist : constant Elist_Id := New_Elmt_List;
10246
10247 Constr : Node_Id;
10248 Expr : Node_Id;
10249 Id : Node_Id;
10250 Position : Nat;
10251 Found : Boolean;
10252
10253 Discrim_Present : Boolean := False;
10254
10255 -- Start of processing for Build_Discriminant_Constraints
10256
10257 begin
10258 -- The following loop will process positional associations only.
10259 -- For a positional association, the (single) discriminant is
10260 -- implicitly specified by position, in textual order (RM 3.7.2).
10261
10262 Discr := First_Discriminant (T);
10263 Constr := First (Constraints (C));
10264 for D in Discr_Expr'Range loop
10265 exit when Nkind (Constr) = N_Discriminant_Association;
10266
10267 if No (Constr) then
10268 Error_Msg_N ("too few discriminants given in constraint", C);
10269 return New_Elmt_List;
10270
10271 elsif Nkind (Constr) = N_Range
10272 or else (Nkind (Constr) = N_Attribute_Reference
10273 and then Attribute_Name (Constr) = Name_Range)
10274 then
10275 Error_Msg_N
10276 ("a range is not a valid discriminant constraint", Constr);
10277 Discr_Expr (D) := Error;
10278
10279 elsif Nkind (Constr) = N_Subtype_Indication then
10280 Error_Msg_N
10281 ("a subtype indication is not a valid discriminant constraint",
10282 Constr);
10283 Discr_Expr (D) := Error;
10284
10285 else
10286 Process_Discriminant_Expression (Constr, Discr);
10287 Discr_Expr (D) := Constr;
10288 end if;
10289
10290 Next_Discriminant (Discr);
10291 Next (Constr);
10292 end loop;
10293
10294 if No (Discr) and then Present (Constr) then
10295 Error_Msg_N ("too many discriminants given in constraint", Constr);
10296 return New_Elmt_List;
10297 end if;
10298
10299 -- Named associations can be given in any order, but if both positional
10300 -- and named associations are used in the same discriminant constraint,
10301 -- then positional associations must occur first, at their normal
10302 -- position. Hence once a named association is used, the rest of the
10303 -- discriminant constraint must use only named associations.
10304
10305 while Present (Constr) loop
10306
10307 -- Positional association forbidden after a named association
10308
10309 if Nkind (Constr) /= N_Discriminant_Association then
10310 Error_Msg_N ("positional association follows named one", Constr);
10311 return New_Elmt_List;
10312
10313 -- Otherwise it is a named association
10314
10315 else
10316 -- E records the type of the discriminants in the named
10317 -- association. All the discriminants specified in the same name
10318 -- association must have the same type.
10319
10320 E := Empty;
10321
10322 -- Search the list of discriminants in T to see if the simple name
10323 -- given in the constraint matches any of them.
10324
10325 Id := First (Selector_Names (Constr));
10326 while Present (Id) loop
10327 Found := False;
10328
10329 -- If Original_Discriminant is present, we are processing a
10330 -- generic instantiation and this is an instance node. We need
10331 -- to find the name of the corresponding discriminant in the
10332 -- actual record type T and not the name of the discriminant in
10333 -- the generic formal. Example:
10334
10335 -- generic
10336 -- type G (D : int) is private;
10337 -- package P is
10338 -- subtype W is G (D => 1);
10339 -- end package;
10340 -- type Rec (X : int) is record ... end record;
10341 -- package Q is new P (G => Rec);
10342
10343 -- At the point of the instantiation, formal type G is Rec
10344 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10345 -- which really looks like "subtype W is Rec (D => 1);" at
10346 -- the point of instantiation, we want to find the discriminant
10347 -- that corresponds to D in Rec, i.e. X.
10348
10349 if Present (Original_Discriminant (Id))
10350 and then In_Instance
10351 then
10352 Discr := Find_Corresponding_Discriminant (Id, T);
10353 Found := True;
10354
10355 else
10356 Discr := First_Discriminant (T);
10357 while Present (Discr) loop
10358 if Chars (Discr) = Chars (Id) then
10359 Found := True;
10360 exit;
10361 end if;
10362
10363 Next_Discriminant (Discr);
10364 end loop;
10365
10366 if not Found then
10367 Error_Msg_N ("& does not match any discriminant", Id);
10368 return New_Elmt_List;
10369
10370 -- If the parent type is a generic formal, preserve the
10371 -- name of the discriminant for subsequent instances.
10372 -- see comment at the beginning of this if statement.
10373
10374 elsif Is_Generic_Type (Root_Type (T)) then
10375 Set_Original_Discriminant (Id, Discr);
10376 end if;
10377 end if;
10378
10379 Position := Pos_Of_Discr (T, Discr);
10380
10381 if Present (Discr_Expr (Position)) then
10382 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10383
10384 else
10385 -- Each discriminant specified in the same named association
10386 -- must be associated with a separate copy of the
10387 -- corresponding expression.
10388
10389 if Present (Next (Id)) then
10390 Expr := New_Copy_Tree (Expression (Constr));
10391 Set_Parent (Expr, Parent (Expression (Constr)));
10392 else
10393 Expr := Expression (Constr);
10394 end if;
10395
10396 Discr_Expr (Position) := Expr;
10397 Process_Discriminant_Expression (Expr, Discr);
10398 end if;
10399
10400 -- A discriminant association with more than one discriminant
10401 -- name is only allowed if the named discriminants are all of
10402 -- the same type (RM 3.7.1(8)).
10403
10404 if E = Empty then
10405 E := Base_Type (Etype (Discr));
10406
10407 elsif Base_Type (Etype (Discr)) /= E then
10408 Error_Msg_N
10409 ("all discriminants in an association " &
10410 "must have the same type", Id);
10411 end if;
10412
10413 Next (Id);
10414 end loop;
10415 end if;
10416
10417 Next (Constr);
10418 end loop;
10419
10420 -- A discriminant constraint must provide exactly one value for each
10421 -- discriminant of the type (RM 3.7.1(8)).
10422
10423 for J in Discr_Expr'Range loop
10424 if No (Discr_Expr (J)) then
10425 Error_Msg_N ("too few discriminants given in constraint", C);
10426 return New_Elmt_List;
10427 end if;
10428 end loop;
10429
10430 -- Determine if there are discriminant expressions in the constraint
10431
10432 for J in Discr_Expr'Range loop
10433 if Denotes_Discriminant
10434 (Discr_Expr (J), Check_Concurrent => True)
10435 then
10436 Discrim_Present := True;
10437 exit;
10438 end if;
10439 end loop;
10440
10441 -- Build an element list consisting of the expressions given in the
10442 -- discriminant constraint and apply the appropriate checks. The list
10443 -- is constructed after resolving any named discriminant associations
10444 -- and therefore the expressions appear in the textual order of the
10445 -- discriminants.
10446
10447 Discr := First_Discriminant (T);
10448 for J in Discr_Expr'Range loop
10449 if Discr_Expr (J) /= Error then
10450 Append_Elmt (Discr_Expr (J), Elist);
10451
10452 -- If any of the discriminant constraints is given by a
10453 -- discriminant and we are in a derived type declaration we
10454 -- have a discriminant renaming. Establish link between new
10455 -- and old discriminant. The new discriminant has an implicit
10456 -- dereference if the old one does.
10457
10458 if Denotes_Discriminant (Discr_Expr (J)) then
10459 if Derived_Def then
10460 declare
10461 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10462
10463 begin
10464 Set_Corresponding_Discriminant (New_Discr, Discr);
10465 Set_Has_Implicit_Dereference (New_Discr,
10466 Has_Implicit_Dereference (Discr));
10467 end;
10468 end if;
10469
10470 -- Force the evaluation of non-discriminant expressions.
10471 -- If we have found a discriminant in the constraint 3.4(26)
10472 -- and 3.8(18) demand that no range checks are performed are
10473 -- after evaluation. If the constraint is for a component
10474 -- definition that has a per-object constraint, expressions are
10475 -- evaluated but not checked either. In all other cases perform
10476 -- a range check.
10477
10478 else
10479 if Discrim_Present then
10480 null;
10481
10482 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10483 and then Has_Per_Object_Constraint
10484 (Defining_Identifier (Parent (Parent (Def))))
10485 then
10486 null;
10487
10488 elsif Is_Access_Type (Etype (Discr)) then
10489 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10490
10491 else
10492 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10493 end if;
10494
10495 -- If the value of the discriminant may be visible in
10496 -- another unit or child unit, create an external name
10497 -- for it. We use the name of the object or component
10498 -- that carries the discriminated subtype. The code
10499 -- below may generate external symbols for the discriminant
10500 -- expression when not strictly needed, which is harmless.
10501
10502 if Expander_Active
10503 and then Comes_From_Source (Def)
10504 and then not Is_Subprogram (Current_Scope)
10505 then
10506 declare
10507 Id : Entity_Id := Empty;
10508 begin
10509 if Nkind (Parent (Def)) = N_Object_Declaration then
10510 Id := Defining_Identifier (Parent (Def));
10511
10512 elsif Nkind (Parent (Def)) = N_Component_Definition
10513 and then
10514 Nkind (Parent (Parent (Def)))
10515 = N_Component_Declaration
10516 then
10517 Id := Defining_Identifier (Parent (Parent (Def)));
10518 end if;
10519
10520 if Present (Id) then
10521 Force_Evaluation (
10522 Discr_Expr (J),
10523 Related_Id => Id,
10524 Discr_Number => J);
10525 else
10526 Force_Evaluation (Discr_Expr (J));
10527 end if;
10528 end;
10529 else
10530 Force_Evaluation (Discr_Expr (J));
10531 end if;
10532 end if;
10533
10534 -- Check that the designated type of an access discriminant's
10535 -- expression is not a class-wide type unless the discriminant's
10536 -- designated type is also class-wide.
10537
10538 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10539 and then not Is_Class_Wide_Type
10540 (Designated_Type (Etype (Discr)))
10541 and then Etype (Discr_Expr (J)) /= Any_Type
10542 and then Is_Class_Wide_Type
10543 (Designated_Type (Etype (Discr_Expr (J))))
10544 then
10545 Wrong_Type (Discr_Expr (J), Etype (Discr));
10546
10547 elsif Is_Access_Type (Etype (Discr))
10548 and then not Is_Access_Constant (Etype (Discr))
10549 and then Is_Access_Type (Etype (Discr_Expr (J)))
10550 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10551 then
10552 Error_Msg_NE
10553 ("constraint for discriminant& must be access to variable",
10554 Def, Discr);
10555 end if;
10556 end if;
10557
10558 Next_Discriminant (Discr);
10559 end loop;
10560
10561 return Elist;
10562 end Build_Discriminant_Constraints;
10563
10564 ---------------------------------
10565 -- Build_Discriminated_Subtype --
10566 ---------------------------------
10567
10568 procedure Build_Discriminated_Subtype
10569 (T : Entity_Id;
10570 Def_Id : Entity_Id;
10571 Elist : Elist_Id;
10572 Related_Nod : Node_Id;
10573 For_Access : Boolean := False)
10574 is
10575 Has_Discrs : constant Boolean := Has_Discriminants (T);
10576 Constrained : constant Boolean :=
10577 (Has_Discrs
10578 and then not Is_Empty_Elmt_List (Elist)
10579 and then not Is_Class_Wide_Type (T))
10580 or else Is_Constrained (T);
10581
10582 begin
10583 if Ekind (T) = E_Record_Type then
10584 Mutate_Ekind (Def_Id, E_Record_Subtype);
10585
10586 -- Inherit preelaboration flag from base, for types for which it
10587 -- may have been set: records, private types, protected types.
10588
10589 Set_Known_To_Have_Preelab_Init
10590 (Def_Id, Known_To_Have_Preelab_Init (T));
10591
10592 elsif Ekind (T) = E_Task_Type then
10593 Mutate_Ekind (Def_Id, E_Task_Subtype);
10594
10595 elsif Ekind (T) = E_Protected_Type then
10596 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10597 Set_Known_To_Have_Preelab_Init
10598 (Def_Id, Known_To_Have_Preelab_Init (T));
10599
10600 elsif Is_Private_Type (T) then
10601 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10602 Set_Known_To_Have_Preelab_Init
10603 (Def_Id, Known_To_Have_Preelab_Init (T));
10604
10605 -- Private subtypes may have private dependents
10606
10607 Set_Private_Dependents (Def_Id, New_Elmt_List);
10608
10609 elsif Is_Class_Wide_Type (T) then
10610 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10611
10612 else
10613 -- Incomplete type. Attach subtype to list of dependents, to be
10614 -- completed with full view of parent type, unless is it the
10615 -- designated subtype of a record component within an init_proc.
10616 -- This last case arises for a component of an access type whose
10617 -- designated type is incomplete (e.g. a Taft Amendment type).
10618 -- The designated subtype is within an inner scope, and needs no
10619 -- elaboration, because only the access type is needed in the
10620 -- initialization procedure.
10621
10622 if Ekind (T) = E_Incomplete_Type then
10623 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10624 else
10625 Mutate_Ekind (Def_Id, Ekind (T));
10626 end if;
10627
10628 if For_Access and then Within_Init_Proc then
10629 null;
10630 else
10631 Append_Elmt (Def_Id, Private_Dependents (T));
10632 end if;
10633 end if;
10634
10635 Set_Etype (Def_Id, T);
10636 Reinit_Size_Align (Def_Id);
10637 Set_Has_Discriminants (Def_Id, Has_Discrs);
10638 Set_Is_Constrained (Def_Id, Constrained);
10639
10640 Set_First_Entity (Def_Id, First_Entity (T));
10641 Set_Last_Entity (Def_Id, Last_Entity (T));
10642 Set_Has_Implicit_Dereference
10643 (Def_Id, Has_Implicit_Dereference (T));
10644 Set_Has_Pragma_Unreferenced_Objects
10645 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10646
10647 -- If the subtype is the completion of a private declaration, there may
10648 -- have been representation clauses for the partial view, and they must
10649 -- be preserved. Build_Derived_Type chains the inherited clauses with
10650 -- the ones appearing on the extension. If this comes from a subtype
10651 -- declaration, all clauses are inherited.
10652
10653 if No (First_Rep_Item (Def_Id)) then
10654 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10655 end if;
10656
10657 if Is_Tagged_Type (T) then
10658 Set_Is_Tagged_Type (Def_Id);
10659 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10660 Make_Class_Wide_Type (Def_Id);
10661 end if;
10662
10663 Set_Stored_Constraint (Def_Id, No_Elist);
10664
10665 if Has_Discrs then
10666 Set_Discriminant_Constraint (Def_Id, Elist);
10667 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10668 end if;
10669
10670 if Is_Tagged_Type (T) then
10671
10672 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10673 -- concurrent record type (which has the list of primitive
10674 -- operations).
10675
10676 if Ada_Version >= Ada_2005
10677 and then Is_Concurrent_Type (T)
10678 then
10679 Set_Corresponding_Record_Type (Def_Id,
10680 Corresponding_Record_Type (T));
10681 else
10682 Set_Direct_Primitive_Operations (Def_Id,
10683 Direct_Primitive_Operations (T));
10684 end if;
10685
10686 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10687 end if;
10688
10689 -- Subtypes introduced by component declarations do not need to be
10690 -- marked as delayed, and do not get freeze nodes, because the semantics
10691 -- verifies that the parents of the subtypes are frozen before the
10692 -- enclosing record is frozen.
10693
10694 if not Is_Type (Scope (Def_Id)) then
10695 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10696
10697 if Is_Private_Type (T)
10698 and then Present (Full_View (T))
10699 then
10700 Conditional_Delay (Def_Id, Full_View (T));
10701 else
10702 Conditional_Delay (Def_Id, T);
10703 end if;
10704 end if;
10705
10706 if Is_Record_Type (T) then
10707 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10708
10709 if Has_Discrs
10710 and then not Is_Empty_Elmt_List (Elist)
10711 and then not For_Access
10712 then
10713 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10714
10715 elsif not Is_Private_Type (T) then
10716 Set_Cloned_Subtype (Def_Id, T);
10717 end if;
10718 end if;
10719 end Build_Discriminated_Subtype;
10720
10721 ---------------------------
10722 -- Build_Itype_Reference --
10723 ---------------------------
10724
10725 procedure Build_Itype_Reference
10726 (Ityp : Entity_Id;
10727 Nod : Node_Id)
10728 is
10729 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10730 begin
10731
10732 -- Itype references are only created for use by the back-end
10733
10734 if Inside_A_Generic then
10735 return;
10736 else
10737 Set_Itype (IR, Ityp);
10738
10739 -- If Nod is a library unit entity, then Insert_After won't work,
10740 -- because Nod is not a member of any list. Therefore, we use
10741 -- Add_Global_Declaration in this case. This can happen if we have a
10742 -- build-in-place library function, child unit or not.
10743
10744 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10745 or else (Nkind (Nod) in
10746 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
10747 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10748 then
10749 Add_Global_Declaration (IR);
10750 else
10751 Insert_After (Nod, IR);
10752 end if;
10753 end if;
10754 end Build_Itype_Reference;
10755
10756 ------------------------
10757 -- Build_Scalar_Bound --
10758 ------------------------
10759
10760 function Build_Scalar_Bound
10761 (Bound : Node_Id;
10762 Par_T : Entity_Id;
10763 Der_T : Entity_Id) return Node_Id
10764 is
10765 New_Bound : Entity_Id;
10766
10767 begin
10768 -- Note: not clear why this is needed, how can the original bound
10769 -- be unanalyzed at this point? and if it is, what business do we
10770 -- have messing around with it? and why is the base type of the
10771 -- parent type the right type for the resolution. It probably is
10772 -- not. It is OK for the new bound we are creating, but not for
10773 -- the old one??? Still if it never happens, no problem.
10774
10775 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10776
10777 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
10778 New_Bound := New_Copy (Bound);
10779 Set_Etype (New_Bound, Der_T);
10780 Set_Analyzed (New_Bound);
10781
10782 elsif Is_Entity_Name (Bound) then
10783 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10784
10785 -- The following is almost certainly wrong. What business do we have
10786 -- relocating a node (Bound) that is presumably still attached to
10787 -- the tree elsewhere???
10788
10789 else
10790 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10791 end if;
10792
10793 Set_Etype (New_Bound, Der_T);
10794 return New_Bound;
10795 end Build_Scalar_Bound;
10796
10797 -------------------------------
10798 -- Check_Abstract_Overriding --
10799 -------------------------------
10800
10801 procedure Check_Abstract_Overriding (T : Entity_Id) is
10802 Alias_Subp : Entity_Id;
10803 Elmt : Elmt_Id;
10804 Op_List : Elist_Id;
10805 Subp : Entity_Id;
10806 Type_Def : Node_Id;
10807
10808 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10809 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10810 -- which has pragma Implemented already set. Check whether Subp's entity
10811 -- kind conforms to the implementation kind of the overridden routine.
10812
10813 procedure Check_Pragma_Implemented
10814 (Subp : Entity_Id;
10815 Iface_Subp : Entity_Id);
10816 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10817 -- Iface_Subp and both entities have pragma Implemented already set on
10818 -- them. Check whether the two implementation kinds are conforming.
10819
10820 procedure Inherit_Pragma_Implemented
10821 (Subp : Entity_Id;
10822 Iface_Subp : Entity_Id);
10823 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10824 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10825 -- Propagate the implementation kind of Iface_Subp to Subp.
10826
10827 ------------------------------
10828 -- Check_Pragma_Implemented --
10829 ------------------------------
10830
10831 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10832 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10833 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10834 Subp_Alias : constant Entity_Id := Alias (Subp);
10835 Contr_Typ : Entity_Id;
10836 Impl_Subp : Entity_Id;
10837
10838 begin
10839 -- Subp must have an alias since it is a hidden entity used to link
10840 -- an interface subprogram to its overriding counterpart.
10841
10842 pragma Assert (Present (Subp_Alias));
10843
10844 -- Handle aliases to synchronized wrappers
10845
10846 Impl_Subp := Subp_Alias;
10847
10848 if Is_Primitive_Wrapper (Impl_Subp) then
10849 Impl_Subp := Wrapped_Entity (Impl_Subp);
10850 end if;
10851
10852 -- Extract the type of the controlling formal
10853
10854 Contr_Typ := Etype (First_Formal (Subp_Alias));
10855
10856 if Is_Concurrent_Record_Type (Contr_Typ) then
10857 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10858 end if;
10859
10860 -- An interface subprogram whose implementation kind is By_Entry must
10861 -- be implemented by an entry.
10862
10863 if Impl_Kind = Name_By_Entry
10864 and then Ekind (Impl_Subp) /= E_Entry
10865 then
10866 Error_Msg_Node_2 := Iface_Alias;
10867 Error_Msg_NE
10868 ("type & must implement abstract subprogram & with an entry",
10869 Subp_Alias, Contr_Typ);
10870
10871 elsif Impl_Kind = Name_By_Protected_Procedure then
10872
10873 -- An interface subprogram whose implementation kind is By_
10874 -- Protected_Procedure cannot be implemented by a primitive
10875 -- procedure of a task type.
10876
10877 if Ekind (Contr_Typ) /= E_Protected_Type then
10878 Error_Msg_Node_2 := Contr_Typ;
10879 Error_Msg_NE
10880 ("interface subprogram & cannot be implemented by a "
10881 & "primitive procedure of task type &",
10882 Subp_Alias, Iface_Alias);
10883
10884 -- An interface subprogram whose implementation kind is By_
10885 -- Protected_Procedure must be implemented by a procedure.
10886
10887 elsif Ekind (Impl_Subp) /= E_Procedure then
10888 Error_Msg_Node_2 := Iface_Alias;
10889 Error_Msg_NE
10890 ("type & must implement abstract subprogram & with a "
10891 & "procedure", Subp_Alias, Contr_Typ);
10892
10893 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10894 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10895 then
10896 Error_Msg_Name_1 := Impl_Kind;
10897 Error_Msg_N
10898 ("overriding operation& must have synchronization%",
10899 Subp_Alias);
10900 end if;
10901
10902 -- If primitive has Optional synchronization, overriding operation
10903 -- must match if it has an explicit synchronization.
10904
10905 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10906 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10907 then
10908 Error_Msg_Name_1 := Impl_Kind;
10909 Error_Msg_N
10910 ("overriding operation& must have synchronization%", Subp_Alias);
10911 end if;
10912 end Check_Pragma_Implemented;
10913
10914 ------------------------------
10915 -- Check_Pragma_Implemented --
10916 ------------------------------
10917
10918 procedure Check_Pragma_Implemented
10919 (Subp : Entity_Id;
10920 Iface_Subp : Entity_Id)
10921 is
10922 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10923 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10924
10925 begin
10926 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10927 -- and overriding subprogram are different. In general this is an
10928 -- error except when the implementation kind of the overridden
10929 -- subprograms is By_Any or Optional.
10930
10931 if Iface_Kind /= Subp_Kind
10932 and then Iface_Kind /= Name_By_Any
10933 and then Iface_Kind /= Name_Optional
10934 then
10935 if Iface_Kind = Name_By_Entry then
10936 Error_Msg_N
10937 ("incompatible implementation kind, overridden subprogram " &
10938 "is marked By_Entry", Subp);
10939 else
10940 Error_Msg_N
10941 ("incompatible implementation kind, overridden subprogram " &
10942 "is marked By_Protected_Procedure", Subp);
10943 end if;
10944 end if;
10945 end Check_Pragma_Implemented;
10946
10947 --------------------------------
10948 -- Inherit_Pragma_Implemented --
10949 --------------------------------
10950
10951 procedure Inherit_Pragma_Implemented
10952 (Subp : Entity_Id;
10953 Iface_Subp : Entity_Id)
10954 is
10955 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10956 Loc : constant Source_Ptr := Sloc (Subp);
10957 Impl_Prag : Node_Id;
10958
10959 begin
10960 -- Since the implementation kind is stored as a representation item
10961 -- rather than a flag, create a pragma node.
10962
10963 Impl_Prag :=
10964 Make_Pragma (Loc,
10965 Chars => Name_Implemented,
10966 Pragma_Argument_Associations => New_List (
10967 Make_Pragma_Argument_Association (Loc,
10968 Expression => New_Occurrence_Of (Subp, Loc)),
10969
10970 Make_Pragma_Argument_Association (Loc,
10971 Expression => Make_Identifier (Loc, Iface_Kind))));
10972
10973 -- The pragma doesn't need to be analyzed because it is internally
10974 -- built. It is safe to directly register it as a rep item since we
10975 -- are only interested in the characters of the implementation kind.
10976
10977 Record_Rep_Item (Subp, Impl_Prag);
10978 end Inherit_Pragma_Implemented;
10979
10980 -- Start of processing for Check_Abstract_Overriding
10981
10982 begin
10983 Op_List := Primitive_Operations (T);
10984
10985 -- Loop to check primitive operations
10986
10987 Elmt := First_Elmt (Op_List);
10988 while Present (Elmt) loop
10989 Subp := Node (Elmt);
10990 Alias_Subp := Alias (Subp);
10991
10992 -- Inherited subprograms are identified by the fact that they do not
10993 -- come from source, and the associated source location is the
10994 -- location of the first subtype of the derived type.
10995
10996 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10997 -- subprograms that "require overriding".
10998
10999 -- Special exception, do not complain about failure to override the
11000 -- stream routines _Input and _Output, as well as the primitive
11001 -- operations used in dispatching selects since we always provide
11002 -- automatic overridings for these subprograms.
11003
11004 -- The partial view of T may have been a private extension, for
11005 -- which inherited functions dispatching on result are abstract.
11006 -- If the full view is a null extension, there is no need for
11007 -- overriding in Ada 2005, but wrappers need to be built for them
11008 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11009
11010 if Is_Null_Extension (T)
11011 and then Has_Controlling_Result (Subp)
11012 and then Ada_Version >= Ada_2005
11013 and then Present (Alias_Subp)
11014 and then not Comes_From_Source (Subp)
11015 and then not Is_Abstract_Subprogram (Alias_Subp)
11016 and then not Is_Access_Type (Etype (Subp))
11017 then
11018 null;
11019
11020 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11021 -- processing because this check is done with the aliased
11022 -- entity
11023
11024 elsif Present (Interface_Alias (Subp)) then
11025 null;
11026
11027 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11028 -- of a visible private primitive inherited from an ancestor with
11029 -- the aspect Type_Invariant'Class, unless the inherited primitive
11030 -- is abstract.
11031
11032 elsif not Is_Abstract_Subprogram (Subp)
11033 and then not Comes_From_Source (Subp) -- An inherited subprogram
11034 and then Requires_Overriding (Subp)
11035 and then Present (Alias_Subp)
11036 and then Has_Invariants (Etype (T))
11037 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11038 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11039 and then Is_Private_Primitive (Alias_Subp)
11040 then
11041 Error_Msg_NE
11042 ("inherited private primitive & must be overridden", T, Subp);
11043 Error_Msg_N
11044 ("\because ancestor type has 'Type_'Invariant''Class " &
11045 "(RM 7.3.2(6.1))", T);
11046
11047 elsif (Is_Abstract_Subprogram (Subp)
11048 or else Requires_Overriding (Subp)
11049 or else
11050 (Has_Controlling_Result (Subp)
11051 and then Present (Alias_Subp)
11052 and then not Comes_From_Source (Subp)
11053 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11054 and then not Is_TSS (Subp, TSS_Stream_Input)
11055 and then not Is_TSS (Subp, TSS_Stream_Output)
11056 and then not Is_Abstract_Type (T)
11057 and then not Is_Predefined_Interface_Primitive (Subp)
11058
11059 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11060 -- with abstract interface types because the check will be done
11061 -- with the aliased entity (otherwise we generate a duplicated
11062 -- error message).
11063
11064 and then not Present (Interface_Alias (Subp))
11065 then
11066 if Present (Alias_Subp) then
11067
11068 -- Only perform the check for a derived subprogram when the
11069 -- type has an explicit record extension. This avoids incorrect
11070 -- flagging of abstract subprograms for the case of a type
11071 -- without an extension that is derived from a formal type
11072 -- with a tagged actual (can occur within a private part).
11073
11074 -- Ada 2005 (AI-391): In the case of an inherited function with
11075 -- a controlling result of the type, the rule does not apply if
11076 -- the type is a null extension (unless the parent function
11077 -- itself is abstract, in which case the function must still be
11078 -- be overridden). The expander will generate an overriding
11079 -- wrapper function calling the parent subprogram (see
11080 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11081
11082 Type_Def := Type_Definition (Parent (T));
11083
11084 if Nkind (Type_Def) = N_Derived_Type_Definition
11085 and then Present (Record_Extension_Part (Type_Def))
11086 and then
11087 (Ada_Version < Ada_2005
11088 or else not Is_Null_Extension (T)
11089 or else Ekind (Subp) = E_Procedure
11090 or else not Has_Controlling_Result (Subp)
11091 or else Is_Abstract_Subprogram (Alias_Subp)
11092 or else Requires_Overriding (Subp)
11093 or else Is_Access_Type (Etype (Subp)))
11094 then
11095 -- Avoid reporting error in case of abstract predefined
11096 -- primitive inherited from interface type because the
11097 -- body of internally generated predefined primitives
11098 -- of tagged types are generated later by Freeze_Type
11099
11100 if Is_Interface (Root_Type (T))
11101 and then Is_Abstract_Subprogram (Subp)
11102 and then Is_Predefined_Dispatching_Operation (Subp)
11103 and then not Comes_From_Source (Ultimate_Alias (Subp))
11104 then
11105 null;
11106
11107 -- A null extension is not obliged to override an inherited
11108 -- procedure subject to pragma Extensions_Visible with value
11109 -- False and at least one controlling OUT parameter
11110 -- (SPARK RM 6.1.7(6)).
11111
11112 elsif Is_Null_Extension (T)
11113 and then Is_EVF_Procedure (Subp)
11114 then
11115 null;
11116
11117 -- Subprogram renamings cannot be overridden
11118
11119 elsif Comes_From_Source (Subp)
11120 and then Present (Alias (Subp))
11121 then
11122 null;
11123
11124 -- Skip reporting the error on Ada 2022 only subprograms
11125 -- that require overriding if we are not in Ada 2022 mode.
11126
11127 elsif Ada_Version < Ada_2022
11128 and then Requires_Overriding (Subp)
11129 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11130 then
11131 null;
11132
11133 else
11134 Error_Msg_NE
11135 ("type must be declared abstract or & overridden",
11136 T, Subp);
11137
11138 -- Traverse the whole chain of aliased subprograms to
11139 -- complete the error notification. This is especially
11140 -- useful for traceability of the chain of entities when
11141 -- the subprogram corresponds with an interface
11142 -- subprogram (which may be defined in another package).
11143
11144 if Present (Alias_Subp) then
11145 declare
11146 E : Entity_Id;
11147
11148 begin
11149 E := Subp;
11150 while Present (Alias (E)) loop
11151
11152 -- Avoid reporting redundant errors on entities
11153 -- inherited from interfaces
11154
11155 if Sloc (E) /= Sloc (T) then
11156 Error_Msg_Sloc := Sloc (E);
11157 Error_Msg_NE
11158 ("\& has been inherited #", T, Subp);
11159 end if;
11160
11161 E := Alias (E);
11162 end loop;
11163
11164 Error_Msg_Sloc := Sloc (E);
11165
11166 -- AI05-0068: report if there is an overriding
11167 -- non-abstract subprogram that is invisible.
11168
11169 if Is_Hidden (E)
11170 and then not Is_Abstract_Subprogram (E)
11171 then
11172 Error_Msg_NE
11173 ("\& subprogram# is not visible",
11174 T, Subp);
11175
11176 -- Clarify the case where a non-null extension must
11177 -- override inherited procedure subject to pragma
11178 -- Extensions_Visible with value False and at least
11179 -- one controlling OUT param.
11180
11181 elsif Is_EVF_Procedure (E) then
11182 Error_Msg_NE
11183 ("\& # is subject to Extensions_Visible False",
11184 T, Subp);
11185
11186 else
11187 Error_Msg_NE
11188 ("\& has been inherited from subprogram #",
11189 T, Subp);
11190 end if;
11191 end;
11192 end if;
11193 end if;
11194
11195 -- Ada 2005 (AI-345): Protected or task type implementing
11196 -- abstract interfaces.
11197
11198 elsif Is_Concurrent_Record_Type (T)
11199 and then Present (Interfaces (T))
11200 then
11201 -- There is no need to check here RM 9.4(11.9/3) since we
11202 -- are processing the corresponding record type and the
11203 -- mode of the overriding subprograms was verified by
11204 -- Check_Conformance when the corresponding concurrent
11205 -- type declaration was analyzed.
11206
11207 Error_Msg_NE
11208 ("interface subprogram & must be overridden", T, Subp);
11209
11210 -- Examine primitive operations of synchronized type to find
11211 -- homonyms that have the wrong profile.
11212
11213 declare
11214 Prim : Entity_Id;
11215
11216 begin
11217 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11218 while Present (Prim) loop
11219 if Chars (Prim) = Chars (Subp) then
11220 Error_Msg_NE
11221 ("profile is not type conformant with prefixed "
11222 & "view profile of inherited operation&",
11223 Prim, Subp);
11224 end if;
11225
11226 Next_Entity (Prim);
11227 end loop;
11228 end;
11229 end if;
11230
11231 else
11232 Error_Msg_Node_2 := T;
11233 Error_Msg_N
11234 ("abstract subprogram& not allowed for type&", Subp);
11235
11236 -- Also post unconditional warning on the type (unconditional
11237 -- so that if there are more than one of these cases, we get
11238 -- them all, and not just the first one).
11239
11240 Error_Msg_Node_2 := Subp;
11241 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11242 end if;
11243
11244 -- A subprogram subject to pragma Extensions_Visible with value
11245 -- "True" cannot override a subprogram subject to the same pragma
11246 -- with value "False" (SPARK RM 6.1.7(5)).
11247
11248 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11249 and then Present (Overridden_Operation (Subp))
11250 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11251 Extensions_Visible_False
11252 then
11253 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11254 Error_Msg_N
11255 ("subprogram & with Extensions_Visible True cannot override "
11256 & "subprogram # with Extensions_Visible False", Subp);
11257 end if;
11258
11259 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11260
11261 -- Subp is an expander-generated procedure which maps an interface
11262 -- alias to a protected wrapper. The interface alias is flagged by
11263 -- pragma Implemented. Ensure that Subp is a procedure when the
11264 -- implementation kind is By_Protected_Procedure or an entry when
11265 -- By_Entry.
11266
11267 if Ada_Version >= Ada_2012
11268 and then Is_Hidden (Subp)
11269 and then Present (Interface_Alias (Subp))
11270 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11271 then
11272 Check_Pragma_Implemented (Subp);
11273 end if;
11274
11275 -- Subp is an interface primitive which overrides another interface
11276 -- primitive marked with pragma Implemented.
11277
11278 if Ada_Version >= Ada_2012
11279 and then Present (Overridden_Operation (Subp))
11280 and then Has_Rep_Pragma
11281 (Overridden_Operation (Subp), Name_Implemented)
11282 then
11283 -- If the overriding routine is also marked by Implemented, check
11284 -- that the two implementation kinds are conforming.
11285
11286 if Has_Rep_Pragma (Subp, Name_Implemented) then
11287 Check_Pragma_Implemented
11288 (Subp => Subp,
11289 Iface_Subp => Overridden_Operation (Subp));
11290
11291 -- Otherwise the overriding routine inherits the implementation
11292 -- kind from the overridden subprogram.
11293
11294 else
11295 Inherit_Pragma_Implemented
11296 (Subp => Subp,
11297 Iface_Subp => Overridden_Operation (Subp));
11298 end if;
11299 end if;
11300
11301 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11302 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11303 -- for procedures, since this is our pragma.
11304
11305 if Present (Overridden_Operation (Subp))
11306 and then No_Return (Overridden_Operation (Subp))
11307 then
11308
11309 -- If the subprogram is a renaming, check that the renamed
11310 -- subprogram is No_Return.
11311
11312 if Present (Renamed_Or_Alias (Subp)) then
11313 if not No_Return (Renamed_Or_Alias (Subp)) then
11314 Error_Msg_NE ("subprogram & must be No_Return",
11315 Subp,
11316 Renamed_Or_Alias (Subp));
11317 Error_Msg_N ("\since renaming & overrides No_Return "
11318 & "subprogram (RM 6.5.1(6/2))",
11319 Subp);
11320 end if;
11321
11322 -- Make sure that the subprogram itself is No_Return.
11323
11324 elsif not No_Return (Subp) then
11325 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11326 Error_Msg_N
11327 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11328 Subp);
11329 end if;
11330 end if;
11331
11332 -- If the operation is a wrapper for a synchronized primitive, it
11333 -- may be called indirectly through a dispatching select. We assume
11334 -- that it will be referenced elsewhere indirectly, and suppress
11335 -- warnings about an unused entity.
11336
11337 if Is_Primitive_Wrapper (Subp)
11338 and then Present (Wrapped_Entity (Subp))
11339 then
11340 Set_Referenced (Wrapped_Entity (Subp));
11341 end if;
11342
11343 Next_Elmt (Elmt);
11344 end loop;
11345 end Check_Abstract_Overriding;
11346
11347 ------------------------------------------------
11348 -- Check_Access_Discriminant_Requires_Limited --
11349 ------------------------------------------------
11350
11351 procedure Check_Access_Discriminant_Requires_Limited
11352 (D : Node_Id;
11353 Loc : Node_Id)
11354 is
11355 begin
11356 -- A discriminant_specification for an access discriminant shall appear
11357 -- only in the declaration for a task or protected type, or for a type
11358 -- with the reserved word 'limited' in its definition or in one of its
11359 -- ancestors (RM 3.7(10)).
11360
11361 -- AI-0063: The proper condition is that type must be immutably limited,
11362 -- or else be a partial view.
11363
11364 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11365 if Is_Limited_View (Current_Scope)
11366 or else
11367 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11368 and then Limited_Present (Parent (Current_Scope)))
11369 then
11370 null;
11371
11372 else
11373 Error_Msg_N
11374 ("access discriminants allowed only for limited types", Loc);
11375 end if;
11376 end if;
11377 end Check_Access_Discriminant_Requires_Limited;
11378
11379 -----------------------------------
11380 -- Check_Aliased_Component_Types --
11381 -----------------------------------
11382
11383 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11384 C : Entity_Id;
11385
11386 begin
11387 -- ??? Also need to check components of record extensions, but not
11388 -- components of protected types (which are always limited).
11389
11390 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11391 -- types to be unconstrained. This is safe because it is illegal to
11392 -- create access subtypes to such types with explicit discriminant
11393 -- constraints.
11394
11395 if not Is_Limited_Type (T) then
11396 if Ekind (T) = E_Record_Type then
11397 C := First_Component (T);
11398 while Present (C) loop
11399 if Is_Aliased (C)
11400 and then Has_Discriminants (Etype (C))
11401 and then not Is_Constrained (Etype (C))
11402 and then not In_Instance_Body
11403 and then Ada_Version < Ada_2005
11404 then
11405 Error_Msg_N
11406 ("aliased component must be constrained (RM 3.6(11))",
11407 C);
11408 end if;
11409
11410 Next_Component (C);
11411 end loop;
11412
11413 elsif Ekind (T) = E_Array_Type then
11414 if Has_Aliased_Components (T)
11415 and then Has_Discriminants (Component_Type (T))
11416 and then not Is_Constrained (Component_Type (T))
11417 and then not In_Instance_Body
11418 and then Ada_Version < Ada_2005
11419 then
11420 Error_Msg_N
11421 ("aliased component type must be constrained (RM 3.6(11))",
11422 T);
11423 end if;
11424 end if;
11425 end if;
11426 end Check_Aliased_Component_Types;
11427
11428 --------------------------------------
11429 -- Check_Anonymous_Access_Component --
11430 --------------------------------------
11431
11432 procedure Check_Anonymous_Access_Component
11433 (Typ_Decl : Node_Id;
11434 Typ : Entity_Id;
11435 Prev : Entity_Id;
11436 Comp_Def : Node_Id;
11437 Access_Def : Node_Id)
11438 is
11439 Loc : constant Source_Ptr := Sloc (Comp_Def);
11440 Anon_Access : Entity_Id;
11441 Acc_Def : Node_Id;
11442 Decl : Node_Id;
11443 Type_Def : Node_Id;
11444
11445 procedure Build_Incomplete_Type_Declaration;
11446 -- If the record type contains components that include an access to the
11447 -- current record, then create an incomplete type declaration for the
11448 -- record, to be used as the designated type of the anonymous access.
11449 -- This is done only once, and only if there is no previous partial
11450 -- view of the type.
11451
11452 function Designates_T (Subt : Node_Id) return Boolean;
11453 -- Check whether a node designates the enclosing record type, or 'Class
11454 -- of that type
11455
11456 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11457 -- Check whether an access definition includes a reference to
11458 -- the enclosing record type. The reference can be a subtype mark
11459 -- in the access definition itself, a 'Class attribute reference, or
11460 -- recursively a reference appearing in a parameter specification
11461 -- or result definition of an access_to_subprogram definition.
11462
11463 --------------------------------------
11464 -- Build_Incomplete_Type_Declaration --
11465 --------------------------------------
11466
11467 procedure Build_Incomplete_Type_Declaration is
11468 Decl : Node_Id;
11469 Inc_T : Entity_Id;
11470 H : Entity_Id;
11471
11472 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11473 -- it's "is new ... with record" or else "is tagged record ...".
11474
11475 Typ_Def : constant Node_Id :=
11476 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11477 then Type_Definition (Typ_Decl) else Empty);
11478 Is_Tagged : constant Boolean :=
11479 Present (Typ_Def)
11480 and then
11481 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11482 and then
11483 Present (Record_Extension_Part (Typ_Def)))
11484 or else
11485 (Nkind (Typ_Def) = N_Record_Definition
11486 and then Tagged_Present (Typ_Def)));
11487
11488 begin
11489 -- If there is a previous partial view, no need to create a new one
11490 -- If the partial view, given by Prev, is incomplete, If Prev is
11491 -- a private declaration, full declaration is flagged accordingly.
11492
11493 if Prev /= Typ then
11494 if Is_Tagged then
11495 Make_Class_Wide_Type (Prev);
11496 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11497 Set_Etype (Class_Wide_Type (Typ), Typ);
11498 end if;
11499
11500 return;
11501
11502 elsif Has_Private_Declaration (Typ) then
11503
11504 -- If we refer to T'Class inside T, and T is the completion of a
11505 -- private type, then make sure the class-wide type exists.
11506
11507 if Is_Tagged then
11508 Make_Class_Wide_Type (Typ);
11509 end if;
11510
11511 return;
11512
11513 -- If there was a previous anonymous access type, the incomplete
11514 -- type declaration will have been created already.
11515
11516 elsif Present (Current_Entity (Typ))
11517 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11518 and then Full_View (Current_Entity (Typ)) = Typ
11519 then
11520 if Is_Tagged
11521 and then Comes_From_Source (Current_Entity (Typ))
11522 and then not Is_Tagged_Type (Current_Entity (Typ))
11523 then
11524 Make_Class_Wide_Type (Typ);
11525 Error_Msg_N
11526 ("incomplete view of tagged type should be declared tagged??",
11527 Parent (Current_Entity (Typ)));
11528 end if;
11529 return;
11530
11531 else
11532 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11533 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11534
11535 -- Type has already been inserted into the current scope. Remove
11536 -- it, and add incomplete declaration for type, so that subsequent
11537 -- anonymous access types can use it. The entity is unchained from
11538 -- the homonym list and from immediate visibility. After analysis,
11539 -- the entity in the incomplete declaration becomes immediately
11540 -- visible in the record declaration that follows.
11541
11542 H := Current_Entity (Typ);
11543
11544 if H = Typ then
11545 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11546 else
11547 while Present (H)
11548 and then Homonym (H) /= Typ
11549 loop
11550 H := Homonym (Typ);
11551 end loop;
11552
11553 Set_Homonym (H, Homonym (Typ));
11554 end if;
11555
11556 Insert_Before (Typ_Decl, Decl);
11557 Analyze (Decl);
11558 Set_Full_View (Inc_T, Typ);
11559
11560 if Is_Tagged then
11561
11562 -- Create a common class-wide type for both views, and set the
11563 -- Etype of the class-wide type to the full view.
11564
11565 Make_Class_Wide_Type (Inc_T);
11566 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11567 Set_Etype (Class_Wide_Type (Typ), Typ);
11568 end if;
11569 end if;
11570 end Build_Incomplete_Type_Declaration;
11571
11572 ------------------
11573 -- Designates_T --
11574 ------------------
11575
11576 function Designates_T (Subt : Node_Id) return Boolean is
11577 Type_Id : constant Name_Id := Chars (Typ);
11578
11579 function Names_T (Nam : Node_Id) return Boolean;
11580 -- The record type has not been introduced in the current scope
11581 -- yet, so we must examine the name of the type itself, either
11582 -- an identifier T, or an expanded name of the form P.T, where
11583 -- P denotes the current scope.
11584
11585 -------------
11586 -- Names_T --
11587 -------------
11588
11589 function Names_T (Nam : Node_Id) return Boolean is
11590 begin
11591 if Nkind (Nam) = N_Identifier then
11592 return Chars (Nam) = Type_Id;
11593
11594 elsif Nkind (Nam) = N_Selected_Component then
11595 if Chars (Selector_Name (Nam)) = Type_Id then
11596 if Nkind (Prefix (Nam)) = N_Identifier then
11597 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11598
11599 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11600 return Chars (Selector_Name (Prefix (Nam))) =
11601 Chars (Current_Scope);
11602 else
11603 return False;
11604 end if;
11605
11606 else
11607 return False;
11608 end if;
11609
11610 else
11611 return False;
11612 end if;
11613 end Names_T;
11614
11615 -- Start of processing for Designates_T
11616
11617 begin
11618 if Nkind (Subt) = N_Identifier then
11619 return Chars (Subt) = Type_Id;
11620
11621 -- Reference can be through an expanded name which has not been
11622 -- analyzed yet, and which designates enclosing scopes.
11623
11624 elsif Nkind (Subt) = N_Selected_Component then
11625 if Names_T (Subt) then
11626 return True;
11627
11628 -- Otherwise it must denote an entity that is already visible.
11629 -- The access definition may name a subtype of the enclosing
11630 -- type, if there is a previous incomplete declaration for it.
11631
11632 else
11633 Find_Selected_Component (Subt);
11634 return
11635 Is_Entity_Name (Subt)
11636 and then Scope (Entity (Subt)) = Current_Scope
11637 and then
11638 (Chars (Base_Type (Entity (Subt))) = Type_Id
11639 or else
11640 (Is_Class_Wide_Type (Entity (Subt))
11641 and then
11642 Chars (Etype (Base_Type (Entity (Subt)))) =
11643 Type_Id));
11644 end if;
11645
11646 -- A reference to the current type may appear as the prefix of
11647 -- a 'Class attribute.
11648
11649 elsif Nkind (Subt) = N_Attribute_Reference
11650 and then Attribute_Name (Subt) = Name_Class
11651 then
11652 return Names_T (Prefix (Subt));
11653
11654 else
11655 return False;
11656 end if;
11657 end Designates_T;
11658
11659 ----------------
11660 -- Mentions_T --
11661 ----------------
11662
11663 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11664 Param_Spec : Node_Id;
11665
11666 Acc_Subprg : constant Node_Id :=
11667 Access_To_Subprogram_Definition (Acc_Def);
11668
11669 begin
11670 if No (Acc_Subprg) then
11671 return Designates_T (Subtype_Mark (Acc_Def));
11672 end if;
11673
11674 -- Component is an access_to_subprogram: examine its formals,
11675 -- and result definition in the case of an access_to_function.
11676
11677 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11678 while Present (Param_Spec) loop
11679 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11680 and then Mentions_T (Parameter_Type (Param_Spec))
11681 then
11682 return True;
11683
11684 elsif Designates_T (Parameter_Type (Param_Spec)) then
11685 return True;
11686 end if;
11687
11688 Next (Param_Spec);
11689 end loop;
11690
11691 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11692 if Nkind (Result_Definition (Acc_Subprg)) =
11693 N_Access_Definition
11694 then
11695 return Mentions_T (Result_Definition (Acc_Subprg));
11696 else
11697 return Designates_T (Result_Definition (Acc_Subprg));
11698 end if;
11699 end if;
11700
11701 return False;
11702 end Mentions_T;
11703
11704 -- Start of processing for Check_Anonymous_Access_Component
11705
11706 begin
11707 if Present (Access_Def) and then Mentions_T (Access_Def) then
11708 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
11709
11710 Build_Incomplete_Type_Declaration;
11711 Anon_Access := Make_Temporary (Loc, 'S');
11712
11713 -- Create a declaration for the anonymous access type: either
11714 -- an access_to_object or an access_to_subprogram.
11715
11716 if Present (Acc_Def) then
11717 if Nkind (Acc_Def) = N_Access_Function_Definition then
11718 Type_Def :=
11719 Make_Access_Function_Definition (Loc,
11720 Parameter_Specifications =>
11721 Parameter_Specifications (Acc_Def),
11722 Result_Definition => Result_Definition (Acc_Def));
11723 else
11724 Type_Def :=
11725 Make_Access_Procedure_Definition (Loc,
11726 Parameter_Specifications =>
11727 Parameter_Specifications (Acc_Def));
11728 end if;
11729
11730 else
11731 Type_Def :=
11732 Make_Access_To_Object_Definition (Loc,
11733 Subtype_Indication =>
11734 Relocate_Node (Subtype_Mark (Access_Def)));
11735
11736 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
11737 Set_All_Present (Type_Def, All_Present (Access_Def));
11738 end if;
11739
11740 Set_Null_Exclusion_Present
11741 (Type_Def, Null_Exclusion_Present (Access_Def));
11742
11743 Decl :=
11744 Make_Full_Type_Declaration (Loc,
11745 Defining_Identifier => Anon_Access,
11746 Type_Definition => Type_Def);
11747
11748 Insert_Before (Typ_Decl, Decl);
11749 Analyze (Decl);
11750
11751 -- If an access to subprogram, create the extra formals
11752
11753 if Present (Acc_Def) then
11754 Create_Extra_Formals (Designated_Type (Anon_Access));
11755 end if;
11756
11757 if Nkind (Comp_Def) = N_Component_Definition then
11758 Rewrite (Comp_Def,
11759 Make_Component_Definition (Loc,
11760 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
11761 else
11762 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
11763 Rewrite (Comp_Def,
11764 Make_Discriminant_Specification (Loc,
11765 Defining_Identifier => Defining_Identifier (Comp_Def),
11766 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
11767 end if;
11768
11769 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11770 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11771 else
11772 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
11773 end if;
11774
11775 Set_Is_Local_Anonymous_Access (Anon_Access);
11776 end if;
11777 end Check_Anonymous_Access_Component;
11778
11779 ---------------------------------------
11780 -- Check_Anonymous_Access_Components --
11781 ---------------------------------------
11782
11783 procedure Check_Anonymous_Access_Components
11784 (Typ_Decl : Node_Id;
11785 Typ : Entity_Id;
11786 Prev : Entity_Id;
11787 Comp_List : Node_Id)
11788 is
11789 Comp : Node_Id;
11790 begin
11791 if No (Comp_List) then
11792 return;
11793 end if;
11794
11795 Comp := First (Component_Items (Comp_List));
11796 while Present (Comp) loop
11797 if Nkind (Comp) = N_Component_Declaration then
11798 Check_Anonymous_Access_Component
11799 (Typ_Decl, Typ, Prev,
11800 Component_Definition (Comp),
11801 Access_Definition (Component_Definition (Comp)));
11802 end if;
11803
11804 Next (Comp);
11805 end loop;
11806
11807 if Present (Variant_Part (Comp_List)) then
11808 declare
11809 V : Node_Id;
11810 begin
11811 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11812 while Present (V) loop
11813 Check_Anonymous_Access_Components
11814 (Typ_Decl, Typ, Prev, Component_List (V));
11815 Next_Non_Pragma (V);
11816 end loop;
11817 end;
11818 end if;
11819 end Check_Anonymous_Access_Components;
11820
11821 ----------------------
11822 -- Check_Completion --
11823 ----------------------
11824
11825 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11826 E : Entity_Id;
11827
11828 procedure Post_Error;
11829 -- Post error message for lack of completion for entity E
11830
11831 ----------------
11832 -- Post_Error --
11833 ----------------
11834
11835 procedure Post_Error is
11836 procedure Missing_Body;
11837 -- Output missing body message
11838
11839 ------------------
11840 -- Missing_Body --
11841 ------------------
11842
11843 procedure Missing_Body is
11844 begin
11845 -- Spec is in same unit, so we can post on spec
11846
11847 if In_Same_Source_Unit (Body_Id, E) then
11848 Error_Msg_N ("missing body for &", E);
11849
11850 -- Spec is in a separate unit, so we have to post on the body
11851
11852 else
11853 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11854 end if;
11855 end Missing_Body;
11856
11857 -- Start of processing for Post_Error
11858
11859 begin
11860 if not Comes_From_Source (E) then
11861 if Ekind (E) in E_Task_Type | E_Protected_Type then
11862
11863 -- It may be an anonymous protected type created for a
11864 -- single variable. Post error on variable, if present.
11865
11866 declare
11867 Var : Entity_Id;
11868
11869 begin
11870 Var := First_Entity (Current_Scope);
11871 while Present (Var) loop
11872 exit when Etype (Var) = E
11873 and then Comes_From_Source (Var);
11874
11875 Next_Entity (Var);
11876 end loop;
11877
11878 if Present (Var) then
11879 E := Var;
11880 end if;
11881 end;
11882 end if;
11883 end if;
11884
11885 -- If a generated entity has no completion, then either previous
11886 -- semantic errors have disabled the expansion phase, or else we had
11887 -- missing subunits, or else we are compiling without expansion,
11888 -- or else something is very wrong.
11889
11890 if not Comes_From_Source (E) then
11891 pragma Assert
11892 (Serious_Errors_Detected > 0
11893 or else Configurable_Run_Time_Violations > 0
11894 or else Subunits_Missing
11895 or else not Expander_Active);
11896 return;
11897
11898 -- Here for source entity
11899
11900 else
11901 -- Here if no body to post the error message, so we post the error
11902 -- on the declaration that has no completion. This is not really
11903 -- the right place to post it, think about this later ???
11904
11905 if No (Body_Id) then
11906 if Is_Type (E) then
11907 Error_Msg_NE
11908 ("missing full declaration for }", Parent (E), E);
11909 else
11910 Error_Msg_NE ("missing body for &", Parent (E), E);
11911 end if;
11912
11913 -- Package body has no completion for a declaration that appears
11914 -- in the corresponding spec. Post error on the body, with a
11915 -- reference to the non-completed declaration.
11916
11917 else
11918 Error_Msg_Sloc := Sloc (E);
11919
11920 if Is_Type (E) then
11921 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11922
11923 elsif Is_Overloadable (E)
11924 and then Current_Entity_In_Scope (E) /= E
11925 then
11926 -- It may be that the completion is mistyped and appears as
11927 -- a distinct overloading of the entity.
11928
11929 declare
11930 Candidate : constant Entity_Id :=
11931 Current_Entity_In_Scope (E);
11932 Decl : constant Node_Id :=
11933 Unit_Declaration_Node (Candidate);
11934
11935 begin
11936 if Is_Overloadable (Candidate)
11937 and then Ekind (Candidate) = Ekind (E)
11938 and then Nkind (Decl) = N_Subprogram_Body
11939 and then Acts_As_Spec (Decl)
11940 then
11941 Check_Type_Conformant (Candidate, E);
11942
11943 else
11944 Missing_Body;
11945 end if;
11946 end;
11947
11948 else
11949 Missing_Body;
11950 end if;
11951 end if;
11952 end if;
11953 end Post_Error;
11954
11955 -- Local variables
11956
11957 Pack_Id : constant Entity_Id := Current_Scope;
11958
11959 -- Start of processing for Check_Completion
11960
11961 begin
11962 E := First_Entity (Pack_Id);
11963 while Present (E) loop
11964 if Is_Intrinsic_Subprogram (E) then
11965 null;
11966
11967 -- The following situation requires special handling: a child unit
11968 -- that appears in the context clause of the body of its parent:
11969
11970 -- procedure Parent.Child (...);
11971
11972 -- with Parent.Child;
11973 -- package body Parent is
11974
11975 -- Here Parent.Child appears as a local entity, but should not be
11976 -- flagged as requiring completion, because it is a compilation
11977 -- unit.
11978
11979 -- Ignore missing completion for a subprogram that does not come from
11980 -- source (including the _Call primitive operation of RAS types,
11981 -- which has to have the flag Comes_From_Source for other purposes):
11982 -- we assume that the expander will provide the missing completion.
11983 -- In case of previous errors, other expansion actions that provide
11984 -- bodies for null procedures with not be invoked, so inhibit message
11985 -- in those cases.
11986
11987 -- Note that E_Operator is not in the list that follows, because
11988 -- this kind is reserved for predefined operators, that are
11989 -- intrinsic and do not need completion.
11990
11991 elsif Ekind (E) in E_Function
11992 | E_Procedure
11993 | E_Generic_Function
11994 | E_Generic_Procedure
11995 then
11996 if Has_Completion (E) then
11997 null;
11998
11999 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12000 null;
12001
12002 elsif Is_Subprogram (E)
12003 and then (not Comes_From_Source (E)
12004 or else Chars (E) = Name_uCall)
12005 then
12006 null;
12007
12008 elsif
12009 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12010 then
12011 null;
12012
12013 elsif Nkind (Parent (E)) = N_Procedure_Specification
12014 and then Null_Present (Parent (E))
12015 and then Serious_Errors_Detected > 0
12016 then
12017 null;
12018
12019 else
12020 Post_Error;
12021 end if;
12022
12023 elsif Is_Entry (E) then
12024 if not Has_Completion (E)
12025 and then Ekind (Scope (E)) = E_Protected_Type
12026 then
12027 Post_Error;
12028 end if;
12029
12030 elsif Is_Package_Or_Generic_Package (E) then
12031 if Unit_Requires_Body (E) then
12032 if not Has_Completion (E)
12033 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12034 N_Compilation_Unit
12035 then
12036 Post_Error;
12037 end if;
12038
12039 elsif not Is_Child_Unit (E) then
12040 May_Need_Implicit_Body (E);
12041 end if;
12042
12043 -- A formal incomplete type (Ada 2012) does not require a completion;
12044 -- other incomplete type declarations do.
12045
12046 elsif Ekind (E) = E_Incomplete_Type then
12047 if No (Underlying_Type (E))
12048 and then not Is_Generic_Type (E)
12049 then
12050 Post_Error;
12051 end if;
12052
12053 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12054 if not Has_Completion (E) then
12055 Post_Error;
12056 end if;
12057
12058 -- A single task declared in the current scope is a constant, verify
12059 -- that the body of its anonymous type is in the same scope. If the
12060 -- task is defined elsewhere, this may be a renaming declaration for
12061 -- which no completion is needed.
12062
12063 elsif Ekind (E) = E_Constant then
12064 if Ekind (Etype (E)) = E_Task_Type
12065 and then not Has_Completion (Etype (E))
12066 and then Scope (Etype (E)) = Current_Scope
12067 then
12068 Post_Error;
12069 end if;
12070
12071 elsif Ekind (E) = E_Record_Type then
12072 if Is_Tagged_Type (E) then
12073 Check_Abstract_Overriding (E);
12074 Check_Conventions (E);
12075 end if;
12076
12077 Check_Aliased_Component_Types (E);
12078
12079 elsif Ekind (E) = E_Array_Type then
12080 Check_Aliased_Component_Types (E);
12081
12082 end if;
12083
12084 Next_Entity (E);
12085 end loop;
12086 end Check_Completion;
12087
12088 -------------------------------------
12089 -- Check_Constraining_Discriminant --
12090 -------------------------------------
12091
12092 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12093 is
12094 New_Type : constant Entity_Id := Etype (New_Disc);
12095 Old_Type : Entity_Id;
12096
12097 begin
12098 -- If the record type contains an array constrained by the discriminant
12099 -- but with some different bound, the compiler tries to create a smaller
12100 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12101 -- In this case, where the discriminant type is a scalar type, the check
12102 -- must use the original discriminant type in the parent declaration.
12103
12104 if Is_Scalar_Type (New_Type) then
12105 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12106 else
12107 Old_Type := Etype (Old_Disc);
12108 end if;
12109
12110 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12111 Error_Msg_N
12112 ("subtype must be statically compatible with parent discriminant",
12113 New_Disc);
12114
12115 if not Predicates_Compatible (New_Type, Old_Type) then
12116 Error_Msg_N
12117 ("\subtype predicate is not compatible with parent discriminant",
12118 New_Disc);
12119 end if;
12120 end if;
12121 end Check_Constraining_Discriminant;
12122
12123 ------------------------------------
12124 -- Check_CPP_Type_Has_No_Defaults --
12125 ------------------------------------
12126
12127 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12128 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12129 Clist : Node_Id;
12130 Comp : Node_Id;
12131
12132 begin
12133 -- Obtain the component list
12134
12135 if Nkind (Tdef) = N_Record_Definition then
12136 Clist := Component_List (Tdef);
12137 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12138 Clist := Component_List (Record_Extension_Part (Tdef));
12139 end if;
12140
12141 -- Check all components to ensure no default expressions
12142
12143 if Present (Clist) then
12144 Comp := First (Component_Items (Clist));
12145 while Present (Comp) loop
12146 if Present (Expression (Comp)) then
12147 Error_Msg_N
12148 ("component of imported 'C'P'P type cannot have "
12149 & "default expression", Expression (Comp));
12150 end if;
12151
12152 Next (Comp);
12153 end loop;
12154 end if;
12155 end Check_CPP_Type_Has_No_Defaults;
12156
12157 ----------------------------
12158 -- Check_Delta_Expression --
12159 ----------------------------
12160
12161 procedure Check_Delta_Expression (E : Node_Id) is
12162 begin
12163 if not (Is_Real_Type (Etype (E))) then
12164 Wrong_Type (E, Any_Real);
12165
12166 elsif not Is_OK_Static_Expression (E) then
12167 Flag_Non_Static_Expr
12168 ("non-static expression used for delta value!", E);
12169
12170 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12171 Error_Msg_N ("delta expression must be positive", E);
12172
12173 else
12174 return;
12175 end if;
12176
12177 -- If any of above errors occurred, then replace the incorrect
12178 -- expression by the real 0.1, which should prevent further errors.
12179
12180 Rewrite (E,
12181 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12182 Analyze_And_Resolve (E, Standard_Float);
12183 end Check_Delta_Expression;
12184
12185 -----------------------------
12186 -- Check_Digits_Expression --
12187 -----------------------------
12188
12189 procedure Check_Digits_Expression (E : Node_Id) is
12190 begin
12191 if not (Is_Integer_Type (Etype (E))) then
12192 Wrong_Type (E, Any_Integer);
12193
12194 elsif not Is_OK_Static_Expression (E) then
12195 Flag_Non_Static_Expr
12196 ("non-static expression used for digits value!", E);
12197
12198 elsif Expr_Value (E) <= 0 then
12199 Error_Msg_N ("digits value must be greater than zero", E);
12200
12201 else
12202 return;
12203 end if;
12204
12205 -- If any of above errors occurred, then replace the incorrect
12206 -- expression by the integer 1, which should prevent further errors.
12207
12208 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12209 Analyze_And_Resolve (E, Standard_Integer);
12210
12211 end Check_Digits_Expression;
12212
12213 --------------------------
12214 -- Check_Initialization --
12215 --------------------------
12216
12217 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12218 begin
12219 -- Special processing for limited types
12220
12221 if Is_Limited_Type (T)
12222 and then not In_Instance
12223 and then not In_Inlined_Body
12224 then
12225 if not OK_For_Limited_Init (T, Exp) then
12226
12227 -- In GNAT mode, this is just a warning, to allow it to be evilly
12228 -- turned off. Otherwise it is a real error.
12229
12230 if GNAT_Mode then
12231 Error_Msg_N
12232 ("??cannot initialize entities of limited type!", Exp);
12233
12234 elsif Ada_Version < Ada_2005 then
12235
12236 -- The side effect removal machinery may generate illegal Ada
12237 -- code to avoid the usage of access types and 'reference in
12238 -- SPARK mode. Since this is legal code with respect to theorem
12239 -- proving, do not emit the error.
12240
12241 if GNATprove_Mode
12242 and then Nkind (Exp) = N_Function_Call
12243 and then Nkind (Parent (Exp)) = N_Object_Declaration
12244 and then not Comes_From_Source
12245 (Defining_Identifier (Parent (Exp)))
12246 then
12247 null;
12248
12249 else
12250 Error_Msg_N
12251 ("cannot initialize entities of limited type", Exp);
12252 Explain_Limited_Type (T, Exp);
12253 end if;
12254
12255 else
12256 -- Specialize error message according to kind of illegal
12257 -- initial expression. We check the Original_Node to cover
12258 -- cases where the initialization expression of an object
12259 -- declaration generated by the compiler has been rewritten
12260 -- (such as for dispatching calls).
12261
12262 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12263 and then
12264 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12265 then
12266 -- No error for internally-generated object declarations,
12267 -- which can come from build-in-place assignment statements.
12268
12269 if Nkind (Parent (Exp)) = N_Object_Declaration
12270 and then not Comes_From_Source
12271 (Defining_Identifier (Parent (Exp)))
12272 then
12273 null;
12274
12275 else
12276 Error_Msg_N
12277 ("illegal context for call to function with limited "
12278 & "result", Exp);
12279 end if;
12280
12281 else
12282 Error_Msg_N
12283 ("initialization of limited object requires aggregate or "
12284 & "function call", Exp);
12285 end if;
12286 end if;
12287 end if;
12288 end if;
12289
12290 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12291 -- set unless we can be sure that no range check is required.
12292
12293 if not Expander_Active
12294 and then Is_Scalar_Type (T)
12295 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12296 then
12297 Set_Do_Range_Check (Exp);
12298 end if;
12299 end Check_Initialization;
12300
12301 ----------------------
12302 -- Check_Interfaces --
12303 ----------------------
12304
12305 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12306 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12307
12308 Iface : Node_Id;
12309 Iface_Def : Node_Id;
12310 Iface_Typ : Entity_Id;
12311 Parent_Node : Node_Id;
12312
12313 Is_Task : Boolean := False;
12314 -- Set True if parent type or any progenitor is a task interface
12315
12316 Is_Protected : Boolean := False;
12317 -- Set True if parent type or any progenitor is a protected interface
12318
12319 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12320 -- Check that a progenitor is compatible with declaration. If an error
12321 -- message is output, it is posted on Error_Node.
12322
12323 ------------------
12324 -- Check_Ifaces --
12325 ------------------
12326
12327 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12328 Iface_Id : constant Entity_Id :=
12329 Defining_Identifier (Parent (Iface_Def));
12330 Type_Def : Node_Id;
12331
12332 begin
12333 if Nkind (N) = N_Private_Extension_Declaration then
12334 Type_Def := N;
12335 else
12336 Type_Def := Type_Definition (N);
12337 end if;
12338
12339 if Is_Task_Interface (Iface_Id) then
12340 Is_Task := True;
12341
12342 elsif Is_Protected_Interface (Iface_Id) then
12343 Is_Protected := True;
12344 end if;
12345
12346 if Is_Synchronized_Interface (Iface_Id) then
12347
12348 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12349 -- extension derived from a synchronized interface must explicitly
12350 -- be declared synchronized, because the full view will be a
12351 -- synchronized type.
12352
12353 if Nkind (N) = N_Private_Extension_Declaration then
12354 if not Synchronized_Present (N) then
12355 Error_Msg_NE
12356 ("private extension of& must be explicitly synchronized",
12357 N, Iface_Id);
12358 end if;
12359
12360 -- However, by 3.9.4(16/2), a full type that is a record extension
12361 -- is never allowed to derive from a synchronized interface (note
12362 -- that interfaces must be excluded from this check, because those
12363 -- are represented by derived type definitions in some cases).
12364
12365 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12366 and then not Interface_Present (Type_Definition (N))
12367 then
12368 Error_Msg_N ("record extension cannot derive from synchronized "
12369 & "interface", Error_Node);
12370 end if;
12371 end if;
12372
12373 -- Check that the characteristics of the progenitor are compatible
12374 -- with the explicit qualifier in the declaration.
12375 -- The check only applies to qualifiers that come from source.
12376 -- Limited_Present also appears in the declaration of corresponding
12377 -- records, and the check does not apply to them.
12378
12379 if Limited_Present (Type_Def)
12380 and then not
12381 Is_Concurrent_Record_Type (Defining_Identifier (N))
12382 then
12383 if Is_Limited_Interface (Parent_Type)
12384 and then not Is_Limited_Interface (Iface_Id)
12385 then
12386 Error_Msg_NE
12387 ("progenitor & must be limited interface",
12388 Error_Node, Iface_Id);
12389
12390 elsif
12391 (Task_Present (Iface_Def)
12392 or else Protected_Present (Iface_Def)
12393 or else Synchronized_Present (Iface_Def))
12394 and then Nkind (N) /= N_Private_Extension_Declaration
12395 and then not Error_Posted (N)
12396 then
12397 Error_Msg_NE
12398 ("progenitor & must be limited interface",
12399 Error_Node, Iface_Id);
12400 end if;
12401
12402 -- Protected interfaces can only inherit from limited, synchronized
12403 -- or protected interfaces.
12404
12405 elsif Nkind (N) = N_Full_Type_Declaration
12406 and then Protected_Present (Type_Def)
12407 then
12408 if Limited_Present (Iface_Def)
12409 or else Synchronized_Present (Iface_Def)
12410 or else Protected_Present (Iface_Def)
12411 then
12412 null;
12413
12414 elsif Task_Present (Iface_Def) then
12415 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12416 & "from task interface", Error_Node);
12417
12418 else
12419 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12420 & "from non-limited interface", Error_Node);
12421 end if;
12422
12423 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12424 -- limited and synchronized.
12425
12426 elsif Synchronized_Present (Type_Def) then
12427 if Limited_Present (Iface_Def)
12428 or else Synchronized_Present (Iface_Def)
12429 then
12430 null;
12431
12432 elsif Protected_Present (Iface_Def)
12433 and then Nkind (N) /= N_Private_Extension_Declaration
12434 then
12435 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12436 & "from protected interface", Error_Node);
12437
12438 elsif Task_Present (Iface_Def)
12439 and then Nkind (N) /= N_Private_Extension_Declaration
12440 then
12441 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12442 & "from task interface", Error_Node);
12443
12444 elsif not Is_Limited_Interface (Iface_Id) then
12445 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12446 & "from non-limited interface", Error_Node);
12447 end if;
12448
12449 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12450 -- synchronized or task interfaces.
12451
12452 elsif Nkind (N) = N_Full_Type_Declaration
12453 and then Task_Present (Type_Def)
12454 then
12455 if Limited_Present (Iface_Def)
12456 or else Synchronized_Present (Iface_Def)
12457 or else Task_Present (Iface_Def)
12458 then
12459 null;
12460
12461 elsif Protected_Present (Iface_Def) then
12462 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12463 & "protected interface", Error_Node);
12464
12465 else
12466 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12467 & "non-limited interface", Error_Node);
12468 end if;
12469 end if;
12470 end Check_Ifaces;
12471
12472 -- Start of processing for Check_Interfaces
12473
12474 begin
12475 if Is_Interface (Parent_Type) then
12476 if Is_Task_Interface (Parent_Type) then
12477 Is_Task := True;
12478
12479 elsif Is_Protected_Interface (Parent_Type) then
12480 Is_Protected := True;
12481 end if;
12482 end if;
12483
12484 if Nkind (N) = N_Private_Extension_Declaration then
12485
12486 -- Check that progenitors are compatible with declaration
12487
12488 Iface := First (Interface_List (Def));
12489 while Present (Iface) loop
12490 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12491
12492 Parent_Node := Parent (Base_Type (Iface_Typ));
12493 Iface_Def := Type_Definition (Parent_Node);
12494
12495 if not Is_Interface (Iface_Typ) then
12496 Diagnose_Interface (Iface, Iface_Typ);
12497 else
12498 Check_Ifaces (Iface_Def, Iface);
12499 end if;
12500
12501 Next (Iface);
12502 end loop;
12503
12504 if Is_Task and Is_Protected then
12505 Error_Msg_N
12506 ("type cannot derive from task and protected interface", N);
12507 end if;
12508
12509 return;
12510 end if;
12511
12512 -- Full type declaration of derived type.
12513 -- Check compatibility with parent if it is interface type
12514
12515 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12516 and then Is_Interface (Parent_Type)
12517 then
12518 Parent_Node := Parent (Parent_Type);
12519
12520 -- More detailed checks for interface varieties
12521
12522 Check_Ifaces
12523 (Iface_Def => Type_Definition (Parent_Node),
12524 Error_Node => Subtype_Indication (Type_Definition (N)));
12525 end if;
12526
12527 Iface := First (Interface_List (Def));
12528 while Present (Iface) loop
12529 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12530
12531 Parent_Node := Parent (Base_Type (Iface_Typ));
12532 Iface_Def := Type_Definition (Parent_Node);
12533
12534 if not Is_Interface (Iface_Typ) then
12535 Diagnose_Interface (Iface, Iface_Typ);
12536
12537 else
12538 -- "The declaration of a specific descendant of an interface
12539 -- type freezes the interface type" RM 13.14
12540
12541 Freeze_Before (N, Iface_Typ);
12542 Check_Ifaces (Iface_Def, Error_Node => Iface);
12543 end if;
12544
12545 Next (Iface);
12546 end loop;
12547
12548 if Is_Task and Is_Protected then
12549 Error_Msg_N
12550 ("type cannot derive from task and protected interface", N);
12551 end if;
12552 end Check_Interfaces;
12553
12554 ------------------------------------
12555 -- Check_Or_Process_Discriminants --
12556 ------------------------------------
12557
12558 -- If an incomplete or private type declaration was already given for the
12559 -- type, the discriminants may have already been processed if they were
12560 -- present on the incomplete declaration. In this case a full conformance
12561 -- check has been performed in Find_Type_Name, and we then recheck here
12562 -- some properties that can't be checked on the partial view alone.
12563 -- Otherwise we call Process_Discriminants.
12564
12565 procedure Check_Or_Process_Discriminants
12566 (N : Node_Id;
12567 T : Entity_Id;
12568 Prev : Entity_Id := Empty)
12569 is
12570 begin
12571 if Has_Discriminants (T) then
12572
12573 -- Discriminants are already set on T if they were already present
12574 -- on the partial view. Make them visible to component declarations.
12575
12576 declare
12577 D : Entity_Id;
12578 -- Discriminant on T (full view) referencing expr on partial view
12579
12580 Prev_D : Entity_Id;
12581 -- Entity of corresponding discriminant on partial view
12582
12583 New_D : Node_Id;
12584 -- Discriminant specification for full view, expression is
12585 -- the syntactic copy on full view (which has been checked for
12586 -- conformance with partial view), only used here to post error
12587 -- message.
12588
12589 begin
12590 D := First_Discriminant (T);
12591 New_D := First (Discriminant_Specifications (N));
12592 while Present (D) loop
12593 Prev_D := Current_Entity (D);
12594 Set_Current_Entity (D);
12595 Set_Is_Immediately_Visible (D);
12596 Set_Homonym (D, Prev_D);
12597
12598 -- Handle the case where there is an untagged partial view and
12599 -- the full view is tagged: must disallow discriminants with
12600 -- defaults, unless compiling for Ada 2012, which allows a
12601 -- limited tagged type to have defaulted discriminants (see
12602 -- AI05-0214). However, suppress error here if it was already
12603 -- reported on the default expression of the partial view.
12604
12605 if Is_Tagged_Type (T)
12606 and then Present (Expression (Parent (D)))
12607 and then (not Is_Limited_Type (Current_Scope)
12608 or else Ada_Version < Ada_2012)
12609 and then not Error_Posted (Expression (Parent (D)))
12610 then
12611 if Ada_Version >= Ada_2012 then
12612 Error_Msg_N
12613 ("discriminants of nonlimited tagged type cannot have "
12614 & "defaults",
12615 Expression (New_D));
12616 else
12617 Error_Msg_N
12618 ("discriminants of tagged type cannot have defaults",
12619 Expression (New_D));
12620 end if;
12621 end if;
12622
12623 -- Ada 2005 (AI-230): Access discriminant allowed in
12624 -- non-limited record types.
12625
12626 if Ada_Version < Ada_2005 then
12627
12628 -- This restriction gets applied to the full type here. It
12629 -- has already been applied earlier to the partial view.
12630
12631 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12632 end if;
12633
12634 Next_Discriminant (D);
12635 Next (New_D);
12636 end loop;
12637 end;
12638
12639 elsif Present (Discriminant_Specifications (N)) then
12640 Process_Discriminants (N, Prev);
12641 end if;
12642 end Check_Or_Process_Discriminants;
12643
12644 ----------------------
12645 -- Check_Real_Bound --
12646 ----------------------
12647
12648 procedure Check_Real_Bound (Bound : Node_Id) is
12649 begin
12650 if not Is_Real_Type (Etype (Bound)) then
12651 Error_Msg_N
12652 ("bound in real type definition must be of real type", Bound);
12653
12654 elsif not Is_OK_Static_Expression (Bound) then
12655 Flag_Non_Static_Expr
12656 ("non-static expression used for real type bound!", Bound);
12657
12658 else
12659 return;
12660 end if;
12661
12662 Rewrite
12663 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12664 Analyze (Bound);
12665 Resolve (Bound, Standard_Float);
12666 end Check_Real_Bound;
12667
12668 ------------------------------
12669 -- Complete_Private_Subtype --
12670 ------------------------------
12671
12672 procedure Complete_Private_Subtype
12673 (Priv : Entity_Id;
12674 Full : Entity_Id;
12675 Full_Base : Entity_Id;
12676 Related_Nod : Node_Id)
12677 is
12678 Save_Next_Entity : Entity_Id;
12679 Save_Homonym : Entity_Id;
12680
12681 begin
12682 -- Set semantic attributes for (implicit) private subtype completion.
12683 -- If the full type has no discriminants, then it is a copy of the
12684 -- full view of the base. Otherwise, it is a subtype of the base with
12685 -- a possible discriminant constraint. Save and restore the original
12686 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12687 -- not corrupt the entity chain.
12688
12689 Save_Next_Entity := Next_Entity (Full);
12690 Save_Homonym := Homonym (Priv);
12691
12692 if Is_Private_Type (Full_Base)
12693 or else Is_Record_Type (Full_Base)
12694 or else Is_Concurrent_Type (Full_Base)
12695 then
12696 Copy_Node (Priv, Full);
12697
12698 -- Note that the Etype of the full view is the same as the Etype of
12699 -- the partial view. In this fashion, the subtype has access to the
12700 -- correct view of the parent.
12701
12702 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12703 Set_Has_Unknown_Discriminants
12704 (Full, Has_Unknown_Discriminants (Full_Base));
12705 Set_First_Entity (Full, First_Entity (Full_Base));
12706 Set_Last_Entity (Full, Last_Entity (Full_Base));
12707
12708 -- If the underlying base type is constrained, we know that the
12709 -- full view of the subtype is constrained as well (the converse
12710 -- is not necessarily true).
12711
12712 if Is_Constrained (Full_Base) then
12713 Set_Is_Constrained (Full);
12714 end if;
12715
12716 else
12717 Copy_Node (Full_Base, Full);
12718
12719 -- The following subtlety with the Etype of the full view needs to be
12720 -- taken into account here. One could think that it must naturally be
12721 -- set to the base type of the full base:
12722
12723 -- Set_Etype (Full, Base_Type (Full_Base));
12724
12725 -- so that the full view becomes a subtype of the full base when the
12726 -- latter is a base type, which must for example happen when the full
12727 -- base is declared as derived type. That's also correct if the full
12728 -- base is declared as an array type, or a floating-point type, or a
12729 -- fixed-point type, or a signed integer type, as these declarations
12730 -- create an implicit base type and a first subtype so the Etype of
12731 -- the full views must be the implicit base type. But that's wrong
12732 -- if the full base is declared as an access type, or an enumeration
12733 -- type, or a modular integer type, as these declarations directly
12734 -- create a base type, i.e. with Etype pointing to itself. Moreover
12735 -- the full base being declared in the private part, i.e. when the
12736 -- views are swapped, the end result is that the Etype of the full
12737 -- base is set to its private view in this case and that we need to
12738 -- propagate this setting to the full view in order for the subtype
12739 -- to be compatible with the base type.
12740
12741 if Is_Base_Type (Full_Base)
12742 and then (Is_Derived_Type (Full_Base)
12743 or else Ekind (Full_Base) in Array_Kind
12744 or else Ekind (Full_Base) in Fixed_Point_Kind
12745 or else Ekind (Full_Base) in Float_Kind
12746 or else Ekind (Full_Base) in Signed_Integer_Kind)
12747 then
12748 Set_Etype (Full, Full_Base);
12749 end if;
12750
12751 Set_Chars (Full, Chars (Priv));
12752 Set_Sloc (Full, Sloc (Priv));
12753 Conditional_Delay (Full, Priv);
12754 end if;
12755
12756 Link_Entities (Full, Save_Next_Entity);
12757 Set_Homonym (Full, Save_Homonym);
12758 Set_Associated_Node_For_Itype (Full, Related_Nod);
12759
12760 if Ekind (Full) in Incomplete_Or_Private_Kind then
12761 Reinit_Field_To_Zero (Full, F_Private_Dependents);
12762 end if;
12763
12764 -- Set common attributes for all subtypes: kind, convention, etc.
12765
12766 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12767 Set_Convention (Full, Convention (Full_Base));
12768 Set_Is_First_Subtype (Full, False);
12769 Set_Scope (Full, Scope (Priv));
12770 Set_Size_Info (Full, Full_Base);
12771 Copy_RM_Size (To => Full, From => Full_Base);
12772 Set_Is_Itype (Full);
12773
12774 -- A subtype of a private-type-without-discriminants, whose full-view
12775 -- has discriminants with default expressions, is not constrained.
12776
12777 if not Has_Discriminants (Priv) then
12778 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12779
12780 if Has_Discriminants (Full_Base) then
12781 Set_Discriminant_Constraint
12782 (Full, Discriminant_Constraint (Full_Base));
12783
12784 -- The partial view may have been indefinite, the full view
12785 -- might not be.
12786
12787 Set_Has_Unknown_Discriminants
12788 (Full, Has_Unknown_Discriminants (Full_Base));
12789 end if;
12790 end if;
12791
12792 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12793 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12794
12795 -- Freeze the private subtype entity if its parent is delayed, and not
12796 -- already frozen. We skip this processing if the type is an anonymous
12797 -- subtype of a record component, or is the corresponding record of a
12798 -- protected type, since these are processed when the enclosing type
12799 -- is frozen. If the parent type is declared in a nested package then
12800 -- the freezing of the private and full views also happens later.
12801
12802 if not Is_Type (Scope (Full)) then
12803 if Is_Itype (Priv)
12804 and then In_Same_Source_Unit (Full, Full_Base)
12805 and then Scope (Full_Base) /= Scope (Full)
12806 then
12807 Set_Has_Delayed_Freeze (Full);
12808 Set_Has_Delayed_Freeze (Priv);
12809
12810 else
12811 Set_Has_Delayed_Freeze (Full,
12812 Has_Delayed_Freeze (Full_Base)
12813 and then not Is_Frozen (Full_Base));
12814 end if;
12815 end if;
12816
12817 Set_Freeze_Node (Full, Empty);
12818 Set_Is_Frozen (Full, False);
12819
12820 if Has_Discriminants (Full) then
12821 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12822 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12823
12824 if Has_Unknown_Discriminants (Full) then
12825 Set_Discriminant_Constraint (Full, No_Elist);
12826 end if;
12827 end if;
12828
12829 if Ekind (Full_Base) = E_Record_Type
12830 and then Has_Discriminants (Full_Base)
12831 and then Has_Discriminants (Priv) -- might not, if errors
12832 and then not Has_Unknown_Discriminants (Priv)
12833 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12834 then
12835 Create_Constrained_Components
12836 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12837
12838 -- If the full base is itself derived from private, build a congruent
12839 -- subtype of its underlying full view, for use by the back end.
12840
12841 elsif Is_Private_Type (Full_Base)
12842 and then Present (Underlying_Full_View (Full_Base))
12843 then
12844 declare
12845 Underlying_Full_Base : constant Entity_Id
12846 := Underlying_Full_View (Full_Base);
12847 Underlying_Full : constant Entity_Id
12848 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12849 begin
12850 Set_Is_Itype (Underlying_Full);
12851 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12852 Complete_Private_Subtype
12853 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12854 Set_Underlying_Full_View (Full, Underlying_Full);
12855 Set_Is_Underlying_Full_View (Underlying_Full);
12856 end;
12857
12858 elsif Is_Record_Type (Full_Base) then
12859
12860 -- Show Full is simply a renaming of Full_Base
12861
12862 Set_Cloned_Subtype (Full, Full_Base);
12863 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12864
12865 -- Propagate predicates
12866
12867 Propagate_Predicate_Attributes (Full, Full_Base);
12868 end if;
12869
12870 -- It is unsafe to share the bounds of a scalar type, because the Itype
12871 -- is elaborated on demand, and if a bound is nonstatic, then different
12872 -- orders of elaboration in different units will lead to different
12873 -- external symbols.
12874
12875 if Is_Scalar_Type (Full_Base) then
12876 Set_Scalar_Range (Full,
12877 Make_Range (Sloc (Related_Nod),
12878 Low_Bound =>
12879 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12880 High_Bound =>
12881 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12882
12883 -- This completion inherits the bounds of the full parent, but if
12884 -- the parent is an unconstrained floating point type, so is the
12885 -- completion.
12886
12887 if Is_Floating_Point_Type (Full_Base) then
12888 Set_Includes_Infinities
12889 (Scalar_Range (Full), Has_Infinities (Full_Base));
12890 end if;
12891 end if;
12892
12893 -- ??? It seems that a lot of fields are missing that should be copied
12894 -- from Full_Base to Full. Here are some that are introduced in a
12895 -- non-disruptive way but a cleanup is necessary.
12896
12897 if Is_Tagged_Type (Full_Base) then
12898 Set_Is_Tagged_Type (Full);
12899 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12900
12901 Set_Direct_Primitive_Operations
12902 (Full, Direct_Primitive_Operations (Full_Base));
12903 Set_No_Tagged_Streams_Pragma
12904 (Full, No_Tagged_Streams_Pragma (Full_Base));
12905
12906 if Is_Interface (Full_Base) then
12907 Set_Is_Interface (Full);
12908 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
12909 end if;
12910
12911 -- Inherit class_wide type of full_base in case the partial view was
12912 -- not tagged. Otherwise it has already been created when the private
12913 -- subtype was analyzed.
12914
12915 if No (Class_Wide_Type (Full)) then
12916 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12917 end if;
12918
12919 -- If this is a subtype of a protected or task type, constrain its
12920 -- corresponding record, unless this is a subtype without constraints,
12921 -- i.e. a simple renaming as with an actual subtype in an instance.
12922
12923 elsif Is_Concurrent_Type (Full_Base) then
12924 if Has_Discriminants (Full)
12925 and then Present (Corresponding_Record_Type (Full_Base))
12926 and then
12927 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12928 then
12929 Set_Corresponding_Record_Type (Full,
12930 Constrain_Corresponding_Record
12931 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12932
12933 else
12934 Set_Corresponding_Record_Type (Full,
12935 Corresponding_Record_Type (Full_Base));
12936 end if;
12937 end if;
12938
12939 -- Link rep item chain, and also setting of Has_Predicates from private
12940 -- subtype to full subtype, since we will need these on the full subtype
12941 -- to create the predicate function. Note that the full subtype may
12942 -- already have rep items, inherited from the full view of the base
12943 -- type, so we must be sure not to overwrite these entries.
12944
12945 declare
12946 Append : Boolean;
12947 Item : Node_Id;
12948 Next_Item : Node_Id;
12949 Priv_Item : Node_Id;
12950
12951 begin
12952 Item := First_Rep_Item (Full);
12953 Priv_Item := First_Rep_Item (Priv);
12954
12955 -- If no existing rep items on full type, we can just link directly
12956 -- to the list of items on the private type, if any exist.. Same if
12957 -- the rep items are only those inherited from the base
12958
12959 if (No (Item)
12960 or else Nkind (Item) /= N_Aspect_Specification
12961 or else Entity (Item) = Full_Base)
12962 and then Present (First_Rep_Item (Priv))
12963 then
12964 Set_First_Rep_Item (Full, Priv_Item);
12965
12966 -- Otherwise, search to the end of items currently linked to the full
12967 -- subtype and append the private items to the end. However, if Priv
12968 -- and Full already have the same list of rep items, then the append
12969 -- is not done, as that would create a circularity.
12970 --
12971 -- The partial view may have a predicate and the rep item lists of
12972 -- both views agree when inherited from the same ancestor. In that
12973 -- case, simply propagate the list from one view to the other.
12974 -- A more complex analysis needed here ???
12975
12976 elsif Present (Priv_Item)
12977 and then Item = Next_Rep_Item (Priv_Item)
12978 then
12979 Set_First_Rep_Item (Full, Priv_Item);
12980
12981 elsif Item /= Priv_Item then
12982 Append := True;
12983 loop
12984 Next_Item := Next_Rep_Item (Item);
12985 exit when No (Next_Item);
12986 Item := Next_Item;
12987
12988 -- If the private view has aspect specifications, the full view
12989 -- inherits them. Since these aspects may already have been
12990 -- attached to the full view during derivation, do not append
12991 -- them if already present.
12992
12993 if Item = First_Rep_Item (Priv) then
12994 Append := False;
12995 exit;
12996 end if;
12997 end loop;
12998
12999 -- And link the private type items at the end of the chain
13000
13001 if Append then
13002 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13003 end if;
13004 end if;
13005 end;
13006
13007 -- Make sure Has_Predicates is set on full type if it is set on the
13008 -- private type. Note that it may already be set on the full type and
13009 -- if so, we don't want to unset it. Similarly, propagate information
13010 -- about delayed aspects, because the corresponding pragmas must be
13011 -- analyzed when one of the views is frozen. This last step is needed
13012 -- in particular when the full type is a scalar type for which an
13013 -- anonymous base type is constructed.
13014
13015 -- The predicate functions are generated either at the freeze point
13016 -- of the type or at the end of the visible part, and we must avoid
13017 -- generating them twice.
13018
13019 Propagate_Predicate_Attributes (Full, Priv);
13020
13021 if Has_Delayed_Aspects (Priv) then
13022 Set_Has_Delayed_Aspects (Full);
13023 end if;
13024 end Complete_Private_Subtype;
13025
13026 ----------------------------
13027 -- Constant_Redeclaration --
13028 ----------------------------
13029
13030 procedure Constant_Redeclaration
13031 (Id : Entity_Id;
13032 N : Node_Id;
13033 T : out Entity_Id)
13034 is
13035 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13036 Obj_Def : constant Node_Id := Object_Definition (N);
13037 New_T : Entity_Id;
13038
13039 procedure Check_Possible_Deferred_Completion
13040 (Prev_Id : Entity_Id;
13041 Prev_Obj_Def : Node_Id;
13042 Curr_Obj_Def : Node_Id);
13043 -- Determine whether the two object definitions describe the partial
13044 -- and the full view of a constrained deferred constant. Generate
13045 -- a subtype for the full view and verify that it statically matches
13046 -- the subtype of the partial view.
13047
13048 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13049 -- If deferred constant is an access type initialized with an allocator,
13050 -- check whether there is an illegal recursion in the definition,
13051 -- through a default value of some record subcomponent. This is normally
13052 -- detected when generating init procs, but requires this additional
13053 -- mechanism when expansion is disabled.
13054
13055 ----------------------------------------
13056 -- Check_Possible_Deferred_Completion --
13057 ----------------------------------------
13058
13059 procedure Check_Possible_Deferred_Completion
13060 (Prev_Id : Entity_Id;
13061 Prev_Obj_Def : Node_Id;
13062 Curr_Obj_Def : Node_Id)
13063 is
13064 begin
13065 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
13066 and then Present (Constraint (Prev_Obj_Def))
13067 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
13068 and then Present (Constraint (Curr_Obj_Def))
13069 then
13070 declare
13071 Loc : constant Source_Ptr := Sloc (N);
13072 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13073 Decl : constant Node_Id :=
13074 Make_Subtype_Declaration (Loc,
13075 Defining_Identifier => Def_Id,
13076 Subtype_Indication =>
13077 Relocate_Node (Curr_Obj_Def));
13078
13079 begin
13080 Insert_Before_And_Analyze (N, Decl);
13081 Set_Etype (Id, Def_Id);
13082
13083 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
13084 Error_Msg_Sloc := Sloc (Prev_Id);
13085 Error_Msg_N ("subtype does not statically match deferred "
13086 & "declaration #", N);
13087 end if;
13088 end;
13089 end if;
13090 end Check_Possible_Deferred_Completion;
13091
13092 ---------------------------------
13093 -- Check_Recursive_Declaration --
13094 ---------------------------------
13095
13096 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13097 Comp : Entity_Id;
13098
13099 begin
13100 if Is_Record_Type (Typ) then
13101 Comp := First_Component (Typ);
13102 while Present (Comp) loop
13103 if Comes_From_Source (Comp) then
13104 if Present (Expression (Parent (Comp)))
13105 and then Is_Entity_Name (Expression (Parent (Comp)))
13106 and then Entity (Expression (Parent (Comp))) = Prev
13107 then
13108 Error_Msg_Sloc := Sloc (Parent (Comp));
13109 Error_Msg_NE
13110 ("illegal circularity with declaration for & #",
13111 N, Comp);
13112 return;
13113
13114 elsif Is_Record_Type (Etype (Comp)) then
13115 Check_Recursive_Declaration (Etype (Comp));
13116 end if;
13117 end if;
13118
13119 Next_Component (Comp);
13120 end loop;
13121 end if;
13122 end Check_Recursive_Declaration;
13123
13124 -- Start of processing for Constant_Redeclaration
13125
13126 begin
13127 if Nkind (Parent (Prev)) = N_Object_Declaration then
13128 if Nkind (Object_Definition
13129 (Parent (Prev))) = N_Subtype_Indication
13130 then
13131 -- Find type of new declaration. The constraints of the two
13132 -- views must match statically, but there is no point in
13133 -- creating an itype for the full view.
13134
13135 if Nkind (Obj_Def) = N_Subtype_Indication then
13136 Find_Type (Subtype_Mark (Obj_Def));
13137 New_T := Entity (Subtype_Mark (Obj_Def));
13138
13139 else
13140 Find_Type (Obj_Def);
13141 New_T := Entity (Obj_Def);
13142 end if;
13143
13144 T := Etype (Prev);
13145
13146 else
13147 -- The full view may impose a constraint, even if the partial
13148 -- view does not, so construct the subtype.
13149
13150 New_T := Find_Type_Of_Object (Obj_Def, N);
13151 T := New_T;
13152 end if;
13153
13154 else
13155 -- Current declaration is illegal, diagnosed below in Enter_Name
13156
13157 T := Empty;
13158 New_T := Any_Type;
13159 end if;
13160
13161 -- If previous full declaration or a renaming declaration exists, or if
13162 -- a homograph is present, let Enter_Name handle it, either with an
13163 -- error or with the removal of an overridden implicit subprogram.
13164 -- The previous one is a full declaration if it has an expression
13165 -- (which in the case of an aggregate is indicated by the Init flag).
13166
13167 if Ekind (Prev) /= E_Constant
13168 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13169 or else Present (Expression (Parent (Prev)))
13170 or else Has_Init_Expression (Parent (Prev))
13171 or else Present (Full_View (Prev))
13172 then
13173 Enter_Name (Id);
13174
13175 -- Verify that types of both declarations match, or else that both types
13176 -- are anonymous access types whose designated subtypes statically match
13177 -- (as allowed in Ada 2005 by AI-385).
13178
13179 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13180 and then
13181 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13182 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13183 or else Is_Access_Constant (Etype (New_T)) /=
13184 Is_Access_Constant (Etype (Prev))
13185 or else Can_Never_Be_Null (Etype (New_T)) /=
13186 Can_Never_Be_Null (Etype (Prev))
13187 or else Null_Exclusion_Present (Parent (Prev)) /=
13188 Null_Exclusion_Present (Parent (Id))
13189 or else not Subtypes_Statically_Match
13190 (Designated_Type (Etype (Prev)),
13191 Designated_Type (Etype (New_T))))
13192 then
13193 Error_Msg_Sloc := Sloc (Prev);
13194 Error_Msg_N ("type does not match declaration#", N);
13195 Set_Full_View (Prev, Id);
13196 Set_Etype (Id, Any_Type);
13197
13198 -- A deferred constant whose type is an anonymous array is always
13199 -- illegal (unless imported). A detailed error message might be
13200 -- helpful for Ada beginners.
13201
13202 if Nkind (Object_Definition (Parent (Prev)))
13203 = N_Constrained_Array_Definition
13204 and then Nkind (Object_Definition (N))
13205 = N_Constrained_Array_Definition
13206 then
13207 Error_Msg_N ("\each anonymous array is a distinct type", N);
13208 Error_Msg_N ("a deferred constant must have a named type",
13209 Object_Definition (Parent (Prev)));
13210 end if;
13211
13212 elsif
13213 Null_Exclusion_Present (Parent (Prev))
13214 and then not Null_Exclusion_Present (N)
13215 then
13216 Error_Msg_Sloc := Sloc (Prev);
13217 Error_Msg_N ("null-exclusion does not match declaration#", N);
13218 Set_Full_View (Prev, Id);
13219 Set_Etype (Id, Any_Type);
13220
13221 -- If so, process the full constant declaration
13222
13223 else
13224 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13225 -- the deferred declaration is constrained, then the subtype defined
13226 -- by the subtype_indication in the full declaration shall match it
13227 -- statically.
13228
13229 Check_Possible_Deferred_Completion
13230 (Prev_Id => Prev,
13231 Prev_Obj_Def => Object_Definition (Parent (Prev)),
13232 Curr_Obj_Def => Obj_Def);
13233
13234 Set_Full_View (Prev, Id);
13235 Set_Is_Public (Id, Is_Public (Prev));
13236 Set_Is_Internal (Id);
13237 Append_Entity (Id, Current_Scope);
13238
13239 -- Check ALIASED present if present before (RM 7.4(7))
13240
13241 if Is_Aliased (Prev)
13242 and then not Aliased_Present (N)
13243 then
13244 Error_Msg_Sloc := Sloc (Prev);
13245 Error_Msg_N ("ALIASED required (see declaration #)", N);
13246 end if;
13247
13248 -- Check that placement is in private part and that the incomplete
13249 -- declaration appeared in the visible part.
13250
13251 if Ekind (Current_Scope) = E_Package
13252 and then not In_Private_Part (Current_Scope)
13253 then
13254 Error_Msg_Sloc := Sloc (Prev);
13255 Error_Msg_N
13256 ("full constant for declaration # must be in private part", N);
13257
13258 elsif Ekind (Current_Scope) = E_Package
13259 and then
13260 List_Containing (Parent (Prev)) /=
13261 Visible_Declarations (Package_Specification (Current_Scope))
13262 then
13263 Error_Msg_N
13264 ("deferred constant must be declared in visible part",
13265 Parent (Prev));
13266 end if;
13267
13268 if Is_Access_Type (T)
13269 and then Nkind (Expression (N)) = N_Allocator
13270 then
13271 Check_Recursive_Declaration (Designated_Type (T));
13272 end if;
13273
13274 -- A deferred constant is a visible entity. If type has invariants,
13275 -- verify that the initial value satisfies them. This is not done in
13276 -- GNATprove mode, as GNATprove handles invariant checks itself.
13277
13278 if Has_Invariants (T)
13279 and then Present (Invariant_Procedure (T))
13280 and then not GNATprove_Mode
13281 then
13282 Insert_After (N,
13283 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13284 end if;
13285 end if;
13286 end Constant_Redeclaration;
13287
13288 ----------------------
13289 -- Constrain_Access --
13290 ----------------------
13291
13292 procedure Constrain_Access
13293 (Def_Id : in out Entity_Id;
13294 S : Node_Id;
13295 Related_Nod : Node_Id)
13296 is
13297 T : constant Entity_Id := Entity (Subtype_Mark (S));
13298 Desig_Type : constant Entity_Id := Designated_Type (T);
13299 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
13300 Constraint_OK : Boolean := True;
13301
13302 begin
13303 if Is_Array_Type (Desig_Type) then
13304 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13305
13306 elsif (Is_Record_Type (Desig_Type)
13307 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13308 and then not Is_Constrained (Desig_Type)
13309 then
13310 -- If this is a constrained access definition for a record
13311 -- component, we leave the type as an unconstrained access,
13312 -- and mark the component so that its actual type is built
13313 -- at a point of use (e.g., an assignment statement). This
13314 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13315
13316 if Desig_Type = Current_Scope
13317 and then No (Def_Id)
13318 then
13319 Desig_Subtype :=
13320 Create_Itype
13321 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13322 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13323 Def_Id := Entity (Subtype_Mark (S));
13324
13325 -- We indicate that the component has a per-object constraint
13326 -- for treatment at a point of use, even though the constraint
13327 -- may be independent of discriminants of the enclosing type.
13328
13329 if Nkind (Related_Nod) = N_Component_Declaration then
13330 Set_Has_Per_Object_Constraint
13331 (Defining_Identifier (Related_Nod));
13332 end if;
13333
13334 -- This call added to ensure that the constraint is analyzed
13335 -- (needed for a B test). Note that we still return early from
13336 -- this procedure to avoid recursive processing.
13337
13338 Constrain_Discriminated_Type
13339 (Desig_Subtype, S, Related_Nod, For_Access => True);
13340 return;
13341 end if;
13342
13343 -- Enforce rule that the constraint is illegal if there is an
13344 -- unconstrained view of the designated type. This means that the
13345 -- partial view (either a private type declaration or a derivation
13346 -- from a private type) has no discriminants. (Defect Report
13347 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13348
13349 -- Rule updated for Ada 2005: The private type is said to have
13350 -- a constrained partial view, given that objects of the type
13351 -- can be declared. Furthermore, the rule applies to all access
13352 -- types, unlike the rule concerning default discriminants (see
13353 -- RM 3.7.1(7/3))
13354
13355 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13356 and then Has_Private_Declaration (Desig_Type)
13357 and then In_Open_Scopes (Scope (Desig_Type))
13358 and then Has_Discriminants (Desig_Type)
13359 then
13360 declare
13361 Pack : constant Node_Id :=
13362 Unit_Declaration_Node (Scope (Desig_Type));
13363 Decls : List_Id;
13364 Decl : Node_Id;
13365
13366 begin
13367 if Nkind (Pack) = N_Package_Declaration then
13368 Decls := Visible_Declarations (Specification (Pack));
13369 Decl := First (Decls);
13370 while Present (Decl) loop
13371 if (Nkind (Decl) = N_Private_Type_Declaration
13372 and then Chars (Defining_Identifier (Decl)) =
13373 Chars (Desig_Type))
13374
13375 or else
13376 (Nkind (Decl) = N_Full_Type_Declaration
13377 and then
13378 Chars (Defining_Identifier (Decl)) =
13379 Chars (Desig_Type)
13380 and then Is_Derived_Type (Desig_Type)
13381 and then
13382 Has_Private_Declaration (Etype (Desig_Type)))
13383 then
13384 if No (Discriminant_Specifications (Decl)) then
13385 Error_Msg_N
13386 ("cannot constrain access type if designated "
13387 & "type has constrained partial view", S);
13388 end if;
13389
13390 exit;
13391 end if;
13392
13393 Next (Decl);
13394 end loop;
13395 end if;
13396 end;
13397 end if;
13398
13399 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13400 For_Access => True);
13401
13402 elsif Is_Concurrent_Type (Desig_Type)
13403 and then not Is_Constrained (Desig_Type)
13404 then
13405 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13406
13407 else
13408 Error_Msg_N ("invalid constraint on access type", S);
13409
13410 -- We simply ignore an invalid constraint
13411
13412 Desig_Subtype := Desig_Type;
13413 Constraint_OK := False;
13414 end if;
13415
13416 if No (Def_Id) then
13417 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13418 else
13419 Mutate_Ekind (Def_Id, E_Access_Subtype);
13420 end if;
13421
13422 if Constraint_OK then
13423 Set_Etype (Def_Id, Base_Type (T));
13424
13425 if Is_Private_Type (Desig_Type) then
13426 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13427 end if;
13428 else
13429 Set_Etype (Def_Id, Any_Type);
13430 end if;
13431
13432 Set_Size_Info (Def_Id, T);
13433 Set_Is_Constrained (Def_Id, Constraint_OK);
13434 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13435 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13436 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13437
13438 Conditional_Delay (Def_Id, T);
13439
13440 -- AI-363 : Subtypes of general access types whose designated types have
13441 -- default discriminants are disallowed. In instances, the rule has to
13442 -- be checked against the actual, of which T is the subtype. In a
13443 -- generic body, the rule is checked assuming that the actual type has
13444 -- defaulted discriminants.
13445
13446 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13447 if Ekind (Base_Type (T)) = E_General_Access_Type
13448 and then Has_Defaulted_Discriminants (Desig_Type)
13449 then
13450 if Ada_Version < Ada_2005 then
13451 Error_Msg_N
13452 ("access subtype of general access type would not " &
13453 "be allowed in Ada 2005?y?", S);
13454 else
13455 Error_Msg_N
13456 ("access subtype of general access type not allowed", S);
13457 end if;
13458
13459 Error_Msg_N ("\discriminants have defaults", S);
13460
13461 elsif Is_Access_Type (T)
13462 and then Is_Generic_Type (Desig_Type)
13463 and then Has_Discriminants (Desig_Type)
13464 and then In_Package_Body (Current_Scope)
13465 then
13466 if Ada_Version < Ada_2005 then
13467 Error_Msg_N
13468 ("access subtype would not be allowed in generic body "
13469 & "in Ada 2005?y?", S);
13470 else
13471 Error_Msg_N
13472 ("access subtype not allowed in generic body", S);
13473 end if;
13474
13475 Error_Msg_N
13476 ("\designated type is a discriminated formal", S);
13477 end if;
13478 end if;
13479 end Constrain_Access;
13480
13481 ---------------------
13482 -- Constrain_Array --
13483 ---------------------
13484
13485 procedure Constrain_Array
13486 (Def_Id : in out Entity_Id;
13487 SI : Node_Id;
13488 Related_Nod : Node_Id;
13489 Related_Id : Entity_Id;
13490 Suffix : Character)
13491 is
13492 C : constant Node_Id := Constraint (SI);
13493 Number_Of_Constraints : Nat := 0;
13494 Index : Node_Id;
13495 S, T : Entity_Id;
13496 Constraint_OK : Boolean := True;
13497 Is_FLB_Array_Subtype : Boolean := False;
13498
13499 begin
13500 T := Entity (Subtype_Mark (SI));
13501
13502 if Is_Access_Type (T) then
13503 T := Designated_Type (T);
13504 end if;
13505
13506 -- If an index constraint follows a subtype mark in a subtype indication
13507 -- then the type or subtype denoted by the subtype mark must not already
13508 -- impose an index constraint. The subtype mark must denote either an
13509 -- unconstrained array type or an access type whose designated type
13510 -- is such an array type... (RM 3.6.1)
13511
13512 if Is_Constrained (T) then
13513 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13514 Constraint_OK := False;
13515
13516 else
13517 S := First (Constraints (C));
13518 while Present (S) loop
13519 Number_Of_Constraints := Number_Of_Constraints + 1;
13520 Next (S);
13521 end loop;
13522
13523 -- In either case, the index constraint must provide a discrete
13524 -- range for each index of the array type and the type of each
13525 -- discrete range must be the same as that of the corresponding
13526 -- index. (RM 3.6.1)
13527
13528 if Number_Of_Constraints /= Number_Dimensions (T) then
13529 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13530 Constraint_OK := False;
13531
13532 else
13533 S := First (Constraints (C));
13534 Index := First_Index (T);
13535 Analyze (Index);
13536
13537 -- Apply constraints to each index type
13538
13539 for J in 1 .. Number_Of_Constraints loop
13540 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13541
13542 -- If the subtype of the index has been set to indicate that
13543 -- it has a fixed lower bound, then record that the subtype's
13544 -- entity will need to be marked as being a fixed-lower-bound
13545 -- array subtype.
13546
13547 if S = First (Constraints (C)) then
13548 Is_FLB_Array_Subtype :=
13549 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13550
13551 -- If the parent subtype (or should this be Etype of that?)
13552 -- is an FLB array subtype, we flag an error, because we
13553 -- don't currently allow subtypes of such subtypes to
13554 -- specify a fixed lower bound for any of their indexes,
13555 -- even if the index of the parent subtype is a "range <>"
13556 -- index.
13557
13558 if Is_FLB_Array_Subtype
13559 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13560 then
13561 Error_Msg_NE
13562 ("index with fixed lower bound not allowed for subtype "
13563 & "of fixed-lower-bound }", S, T);
13564
13565 Is_FLB_Array_Subtype := False;
13566 end if;
13567
13568 elsif Is_FLB_Array_Subtype
13569 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13570 then
13571 Error_Msg_NE
13572 ("constrained index not allowed for fixed-lower-bound "
13573 & "subtype of}", S, T);
13574
13575 elsif not Is_FLB_Array_Subtype
13576 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13577 then
13578 Error_Msg_NE
13579 ("index with fixed lower bound not allowed for "
13580 & "constrained subtype of}", S, T);
13581 end if;
13582
13583 Next (Index);
13584 Next (S);
13585 end loop;
13586
13587 end if;
13588 end if;
13589
13590 if No (Def_Id) then
13591 Def_Id :=
13592 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13593 Set_Parent (Def_Id, Related_Nod);
13594
13595 else
13596 Mutate_Ekind (Def_Id, E_Array_Subtype);
13597 end if;
13598
13599 Set_Size_Info (Def_Id, (T));
13600 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13601 Set_Etype (Def_Id, Base_Type (T));
13602
13603 if Constraint_OK then
13604 Set_First_Index (Def_Id, First (Constraints (C)));
13605 else
13606 Set_First_Index (Def_Id, First_Index (T));
13607 end if;
13608
13609 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13610 Set_Is_Fixed_Lower_Bound_Array_Subtype
13611 (Def_Id, Is_FLB_Array_Subtype);
13612 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13613 Set_Is_Independent (Def_Id, Is_Independent (T));
13614 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13615
13616 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13617 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13618
13619 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13620 -- We need to initialize the attribute because if Def_Id is previously
13621 -- analyzed through a limited_with clause, it will have the attributes
13622 -- of an incomplete type, one of which is an Elist that overlaps the
13623 -- Packed_Array_Impl_Type field.
13624
13625 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13626
13627 -- Build a freeze node if parent still needs one. Also make sure that
13628 -- the Depends_On_Private status is set because the subtype will need
13629 -- reprocessing at the time the base type does, and also we must set a
13630 -- conditional delay.
13631
13632 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13633 Conditional_Delay (Def_Id, T);
13634 end Constrain_Array;
13635
13636 ------------------------------
13637 -- Constrain_Component_Type --
13638 ------------------------------
13639
13640 function Constrain_Component_Type
13641 (Comp : Entity_Id;
13642 Constrained_Typ : Entity_Id;
13643 Related_Node : Node_Id;
13644 Typ : Entity_Id;
13645 Constraints : Elist_Id) return Entity_Id
13646 is
13647 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13648 Compon_Type : constant Entity_Id := Etype (Comp);
13649
13650 function Build_Constrained_Array_Type
13651 (Old_Type : Entity_Id) return Entity_Id;
13652 -- If Old_Type is an array type, one of whose indexes is constrained
13653 -- by a discriminant, build an Itype whose constraint replaces the
13654 -- discriminant with its value in the constraint.
13655
13656 function Build_Constrained_Discriminated_Type
13657 (Old_Type : Entity_Id) return Entity_Id;
13658 -- Ditto for record components. Handle the case where the constraint
13659 -- is a conversion of the discriminant value, introduced during
13660 -- expansion.
13661
13662 function Build_Constrained_Access_Type
13663 (Old_Type : Entity_Id) return Entity_Id;
13664 -- Ditto for access types. Makes use of previous two functions, to
13665 -- constrain designated type.
13666
13667 function Is_Discriminant (Expr : Node_Id) return Boolean;
13668 -- Returns True if Expr is a discriminant
13669
13670 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13671 -- Find the value of a discriminant named by Discr_Expr in Constraints
13672
13673 -----------------------------------
13674 -- Build_Constrained_Access_Type --
13675 -----------------------------------
13676
13677 function Build_Constrained_Access_Type
13678 (Old_Type : Entity_Id) return Entity_Id
13679 is
13680 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13681 Itype : Entity_Id;
13682 Desig_Subtype : Entity_Id;
13683 Scop : Entity_Id;
13684
13685 begin
13686 -- If the original access type was not embedded in the enclosing
13687 -- type definition, there is no need to produce a new access
13688 -- subtype. In fact every access type with an explicit constraint
13689 -- generates an itype whose scope is the enclosing record.
13690
13691 if not Is_Type (Scope (Old_Type)) then
13692 return Old_Type;
13693
13694 elsif Is_Array_Type (Desig_Type) then
13695 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13696
13697 elsif Has_Discriminants (Desig_Type) then
13698
13699 -- This may be an access type to an enclosing record type for
13700 -- which we are constructing the constrained components. Return
13701 -- the enclosing record subtype. This is not always correct,
13702 -- but avoids infinite recursion. ???
13703
13704 Desig_Subtype := Any_Type;
13705
13706 for J in reverse 0 .. Scope_Stack.Last loop
13707 Scop := Scope_Stack.Table (J).Entity;
13708
13709 if Is_Type (Scop)
13710 and then Base_Type (Scop) = Base_Type (Desig_Type)
13711 then
13712 Desig_Subtype := Scop;
13713 end if;
13714
13715 exit when not Is_Type (Scop);
13716 end loop;
13717
13718 if Desig_Subtype = Any_Type then
13719 Desig_Subtype :=
13720 Build_Constrained_Discriminated_Type (Desig_Type);
13721 end if;
13722
13723 else
13724 return Old_Type;
13725 end if;
13726
13727 if Desig_Subtype /= Desig_Type then
13728
13729 -- The Related_Node better be here or else we won't be able
13730 -- to attach new itypes to a node in the tree.
13731
13732 pragma Assert (Present (Related_Node));
13733
13734 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13735
13736 Set_Etype (Itype, Base_Type (Old_Type));
13737 Set_Size_Info (Itype, (Old_Type));
13738 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13739 Set_Depends_On_Private (Itype, Has_Private_Component
13740 (Old_Type));
13741 Set_Is_Access_Constant (Itype, Is_Access_Constant
13742 (Old_Type));
13743
13744 -- The new itype needs freezing when it depends on a not frozen
13745 -- type and the enclosing subtype needs freezing.
13746
13747 if Has_Delayed_Freeze (Constrained_Typ)
13748 and then not Is_Frozen (Constrained_Typ)
13749 then
13750 Conditional_Delay (Itype, Base_Type (Old_Type));
13751 end if;
13752
13753 return Itype;
13754
13755 else
13756 return Old_Type;
13757 end if;
13758 end Build_Constrained_Access_Type;
13759
13760 ----------------------------------
13761 -- Build_Constrained_Array_Type --
13762 ----------------------------------
13763
13764 function Build_Constrained_Array_Type
13765 (Old_Type : Entity_Id) return Entity_Id
13766 is
13767 Lo_Expr : Node_Id;
13768 Hi_Expr : Node_Id;
13769 Old_Index : Node_Id;
13770 Range_Node : Node_Id;
13771 Constr_List : List_Id;
13772
13773 Need_To_Create_Itype : Boolean := False;
13774
13775 begin
13776 Old_Index := First_Index (Old_Type);
13777 while Present (Old_Index) loop
13778 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13779
13780 if Is_Discriminant (Lo_Expr)
13781 or else
13782 Is_Discriminant (Hi_Expr)
13783 then
13784 Need_To_Create_Itype := True;
13785 exit;
13786 end if;
13787
13788 Next_Index (Old_Index);
13789 end loop;
13790
13791 if Need_To_Create_Itype then
13792 Constr_List := New_List;
13793
13794 Old_Index := First_Index (Old_Type);
13795 while Present (Old_Index) loop
13796 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13797
13798 if Is_Discriminant (Lo_Expr) then
13799 Lo_Expr := Get_Discr_Value (Lo_Expr);
13800 end if;
13801
13802 if Is_Discriminant (Hi_Expr) then
13803 Hi_Expr := Get_Discr_Value (Hi_Expr);
13804 end if;
13805
13806 Range_Node :=
13807 Make_Range
13808 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13809
13810 Append (Range_Node, To => Constr_List);
13811
13812 Next_Index (Old_Index);
13813 end loop;
13814
13815 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13816
13817 else
13818 return Old_Type;
13819 end if;
13820 end Build_Constrained_Array_Type;
13821
13822 ------------------------------------------
13823 -- Build_Constrained_Discriminated_Type --
13824 ------------------------------------------
13825
13826 function Build_Constrained_Discriminated_Type
13827 (Old_Type : Entity_Id) return Entity_Id
13828 is
13829 Expr : Node_Id;
13830 Constr_List : List_Id;
13831 Old_Constraint : Elmt_Id;
13832
13833 Need_To_Create_Itype : Boolean := False;
13834
13835 begin
13836 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13837 while Present (Old_Constraint) loop
13838 Expr := Node (Old_Constraint);
13839
13840 if Is_Discriminant (Expr) then
13841 Need_To_Create_Itype := True;
13842 exit;
13843
13844 -- After expansion of discriminated task types, the value
13845 -- of the discriminant may be converted to a run-time type
13846 -- for restricted run-times. Propagate the value of the
13847 -- discriminant as well, so that e.g. the secondary stack
13848 -- component has a static constraint. Necessary for LLVM.
13849
13850 elsif Nkind (Expr) = N_Type_Conversion
13851 and then Is_Discriminant (Expression (Expr))
13852 then
13853 Need_To_Create_Itype := True;
13854 exit;
13855 end if;
13856
13857 Next_Elmt (Old_Constraint);
13858 end loop;
13859
13860 if Need_To_Create_Itype then
13861 Constr_List := New_List;
13862
13863 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13864 while Present (Old_Constraint) loop
13865 Expr := Node (Old_Constraint);
13866
13867 if Is_Discriminant (Expr) then
13868 Expr := Get_Discr_Value (Expr);
13869
13870 elsif Nkind (Expr) = N_Type_Conversion
13871 and then Is_Discriminant (Expression (Expr))
13872 then
13873 Expr := New_Copy_Tree (Expr);
13874 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13875 end if;
13876
13877 Append (New_Copy_Tree (Expr), To => Constr_List);
13878
13879 Next_Elmt (Old_Constraint);
13880 end loop;
13881
13882 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13883
13884 else
13885 return Old_Type;
13886 end if;
13887 end Build_Constrained_Discriminated_Type;
13888
13889 ---------------------
13890 -- Get_Discr_Value --
13891 ---------------------
13892
13893 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
13894 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
13895 -- Entity of a discriminant that appear as a standalone expression in
13896 -- the constraint of a component.
13897
13898 D : Entity_Id;
13899 E : Elmt_Id;
13900
13901 begin
13902 -- The discriminant may be declared for the type, in which case we
13903 -- find it by iterating over the list of discriminants. If the
13904 -- discriminant is inherited from a parent type, it appears as the
13905 -- corresponding discriminant of the current type. This will be the
13906 -- case when constraining an inherited component whose constraint is
13907 -- given by a discriminant of the parent.
13908
13909 D := First_Discriminant (Typ);
13910 E := First_Elmt (Constraints);
13911
13912 while Present (D) loop
13913 if D = Discr_Id
13914 or else D = CR_Discriminant (Discr_Id)
13915 or else Corresponding_Discriminant (D) = Discr_Id
13916 then
13917 return Node (E);
13918 end if;
13919
13920 Next_Discriminant (D);
13921 Next_Elmt (E);
13922 end loop;
13923
13924 -- The Corresponding_Discriminant mechanism is incomplete, because
13925 -- the correspondence between new and old discriminants is not one
13926 -- to one: one new discriminant can constrain several old ones. In
13927 -- that case, scan sequentially the stored_constraint, the list of
13928 -- discriminants of the parents, and the constraints.
13929
13930 -- Previous code checked for the present of the Stored_Constraint
13931 -- list for the derived type, but did not use it at all. Should it
13932 -- be present when the component is a discriminated task type?
13933
13934 if Is_Derived_Type (Typ)
13935 and then Scope (Discr_Id) = Etype (Typ)
13936 then
13937 D := First_Discriminant (Etype (Typ));
13938 E := First_Elmt (Constraints);
13939 while Present (D) loop
13940 if D = Discr_Id then
13941 return Node (E);
13942 end if;
13943
13944 Next_Discriminant (D);
13945 Next_Elmt (E);
13946 end loop;
13947 end if;
13948
13949 -- Something is wrong if we did not find the value
13950
13951 raise Program_Error;
13952 end Get_Discr_Value;
13953
13954 ---------------------
13955 -- Is_Discriminant --
13956 ---------------------
13957
13958 function Is_Discriminant (Expr : Node_Id) return Boolean is
13959 Discrim_Scope : Entity_Id;
13960
13961 begin
13962 if Denotes_Discriminant (Expr) then
13963 Discrim_Scope := Scope (Entity (Expr));
13964
13965 -- Either we have a reference to one of Typ's discriminants,
13966
13967 pragma Assert (Discrim_Scope = Typ
13968
13969 -- or to the discriminants of the parent type, in the case
13970 -- of a derivation of a tagged type with variants.
13971
13972 or else Discrim_Scope = Etype (Typ)
13973 or else Full_View (Discrim_Scope) = Etype (Typ)
13974
13975 -- or same as above for the case where the discriminants
13976 -- were declared in Typ's private view.
13977
13978 or else (Is_Private_Type (Discrim_Scope)
13979 and then Chars (Discrim_Scope) = Chars (Typ))
13980
13981 -- or else we are deriving from the full view and the
13982 -- discriminant is declared in the private entity.
13983
13984 or else (Is_Private_Type (Typ)
13985 and then Chars (Discrim_Scope) = Chars (Typ))
13986
13987 -- Or we are constrained the corresponding record of a
13988 -- synchronized type that completes a private declaration.
13989
13990 or else (Is_Concurrent_Record_Type (Typ)
13991 and then
13992 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13993
13994 -- or we have a class-wide type, in which case make sure the
13995 -- discriminant found belongs to the root type.
13996
13997 or else (Is_Class_Wide_Type (Typ)
13998 and then Etype (Typ) = Discrim_Scope));
13999
14000 return True;
14001 end if;
14002
14003 -- In all other cases we have something wrong
14004
14005 return False;
14006 end Is_Discriminant;
14007
14008 -- Start of processing for Constrain_Component_Type
14009
14010 begin
14011 if Nkind (Parent (Comp)) = N_Component_Declaration
14012 and then Comes_From_Source (Parent (Comp))
14013 and then Comes_From_Source
14014 (Subtype_Indication (Component_Definition (Parent (Comp))))
14015 and then
14016 Is_Entity_Name
14017 (Subtype_Indication (Component_Definition (Parent (Comp))))
14018 then
14019 return Compon_Type;
14020
14021 elsif Is_Array_Type (Compon_Type) then
14022 return Build_Constrained_Array_Type (Compon_Type);
14023
14024 elsif Has_Discriminants (Compon_Type) then
14025 return Build_Constrained_Discriminated_Type (Compon_Type);
14026
14027 elsif Is_Access_Type (Compon_Type) then
14028 return Build_Constrained_Access_Type (Compon_Type);
14029
14030 else
14031 return Compon_Type;
14032 end if;
14033 end Constrain_Component_Type;
14034
14035 --------------------------
14036 -- Constrain_Concurrent --
14037 --------------------------
14038
14039 -- For concurrent types, the associated record value type carries the same
14040 -- discriminants, so when we constrain a concurrent type, we must constrain
14041 -- the corresponding record type as well.
14042
14043 procedure Constrain_Concurrent
14044 (Def_Id : in out Entity_Id;
14045 SI : Node_Id;
14046 Related_Nod : Node_Id;
14047 Related_Id : Entity_Id;
14048 Suffix : Character)
14049 is
14050 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14051 -- case of a private subtype (needed when only doing semantic analysis).
14052
14053 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14054 T_Val : Entity_Id;
14055
14056 begin
14057 if Is_Access_Type (T_Ent) then
14058 T_Ent := Designated_Type (T_Ent);
14059 end if;
14060
14061 T_Val := Corresponding_Record_Type (T_Ent);
14062
14063 if Present (T_Val) then
14064
14065 if No (Def_Id) then
14066 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14067
14068 -- Elaborate itype now, as it may be used in a subsequent
14069 -- synchronized operation in another scope.
14070
14071 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14072 Build_Itype_Reference (Def_Id, Related_Nod);
14073 end if;
14074 end if;
14075
14076 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14077 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14078
14079 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14080 Set_Corresponding_Record_Type (Def_Id,
14081 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14082
14083 else
14084 -- If there is no associated record, expansion is disabled and this
14085 -- is a generic context. Create a subtype in any case, so that
14086 -- semantic analysis can proceed.
14087
14088 if No (Def_Id) then
14089 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14090 end if;
14091
14092 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14093 end if;
14094 end Constrain_Concurrent;
14095
14096 ------------------------------------
14097 -- Constrain_Corresponding_Record --
14098 ------------------------------------
14099
14100 function Constrain_Corresponding_Record
14101 (Prot_Subt : Entity_Id;
14102 Corr_Rec : Entity_Id;
14103 Related_Nod : Node_Id) return Entity_Id
14104 is
14105 T_Sub : constant Entity_Id :=
14106 Create_Itype
14107 (Ekind => E_Record_Subtype,
14108 Related_Nod => Related_Nod,
14109 Related_Id => Corr_Rec,
14110 Suffix => 'C',
14111 Suffix_Index => -1);
14112
14113 begin
14114 Set_Etype (T_Sub, Corr_Rec);
14115 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14116 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14117 Set_Is_Constrained (T_Sub, True);
14118 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14119 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14120
14121 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14122 Set_Discriminant_Constraint
14123 (T_Sub, Discriminant_Constraint (Prot_Subt));
14124 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14125 Create_Constrained_Components
14126 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14127 end if;
14128
14129 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14130
14131 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14132 Conditional_Delay (T_Sub, Corr_Rec);
14133
14134 else
14135 -- This is a component subtype: it will be frozen in the context of
14136 -- the enclosing record's init_proc, so that discriminant references
14137 -- are resolved to discriminals. (Note: we used to skip freezing
14138 -- altogether in that case, which caused errors downstream for
14139 -- components of a bit packed array type).
14140
14141 Set_Has_Delayed_Freeze (T_Sub);
14142 end if;
14143
14144 return T_Sub;
14145 end Constrain_Corresponding_Record;
14146
14147 -----------------------
14148 -- Constrain_Decimal --
14149 -----------------------
14150
14151 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14152 T : constant Entity_Id := Entity (Subtype_Mark (S));
14153 C : constant Node_Id := Constraint (S);
14154 Loc : constant Source_Ptr := Sloc (C);
14155 Range_Expr : Node_Id;
14156 Digits_Expr : Node_Id;
14157 Digits_Val : Uint;
14158 Bound_Val : Ureal;
14159
14160 begin
14161 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14162
14163 if Nkind (C) = N_Range_Constraint then
14164 Range_Expr := Range_Expression (C);
14165 Digits_Val := Digits_Value (T);
14166
14167 else
14168 pragma Assert (Nkind (C) = N_Digits_Constraint);
14169
14170 Digits_Expr := Digits_Expression (C);
14171 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14172
14173 Check_Digits_Expression (Digits_Expr);
14174 Digits_Val := Expr_Value (Digits_Expr);
14175
14176 if Digits_Val > Digits_Value (T) then
14177 Error_Msg_N
14178 ("digits expression is incompatible with subtype", C);
14179 Digits_Val := Digits_Value (T);
14180 end if;
14181
14182 if Present (Range_Constraint (C)) then
14183 Range_Expr := Range_Expression (Range_Constraint (C));
14184 else
14185 Range_Expr := Empty;
14186 end if;
14187 end if;
14188
14189 Set_Etype (Def_Id, Base_Type (T));
14190 Set_Size_Info (Def_Id, (T));
14191 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14192 Set_Delta_Value (Def_Id, Delta_Value (T));
14193 Set_Scale_Value (Def_Id, Scale_Value (T));
14194 Set_Small_Value (Def_Id, Small_Value (T));
14195 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14196 Set_Digits_Value (Def_Id, Digits_Val);
14197
14198 -- Manufacture range from given digits value if no range present
14199
14200 if No (Range_Expr) then
14201 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14202 Range_Expr :=
14203 Make_Range (Loc,
14204 Low_Bound =>
14205 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14206 High_Bound =>
14207 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14208 end if;
14209
14210 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14211 Set_Discrete_RM_Size (Def_Id);
14212
14213 -- Unconditionally delay the freeze, since we cannot set size
14214 -- information in all cases correctly until the freeze point.
14215
14216 Set_Has_Delayed_Freeze (Def_Id);
14217 end Constrain_Decimal;
14218
14219 ----------------------------------
14220 -- Constrain_Discriminated_Type --
14221 ----------------------------------
14222
14223 procedure Constrain_Discriminated_Type
14224 (Def_Id : Entity_Id;
14225 S : Node_Id;
14226 Related_Nod : Node_Id;
14227 For_Access : Boolean := False)
14228 is
14229 E : Entity_Id := Entity (Subtype_Mark (S));
14230 T : Entity_Id;
14231
14232 procedure Fixup_Bad_Constraint;
14233 -- Called after finding a bad constraint, and after having posted an
14234 -- appropriate error message. The goal is to leave type Def_Id in as
14235 -- reasonable state as possible.
14236
14237 --------------------------
14238 -- Fixup_Bad_Constraint --
14239 --------------------------
14240
14241 procedure Fixup_Bad_Constraint is
14242 begin
14243 -- Set a reasonable Ekind for the entity, including incomplete types.
14244
14245 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14246
14247 -- Set Etype to the known type, to reduce chances of cascaded errors
14248
14249 Set_Etype (Def_Id, E);
14250 Set_Error_Posted (Def_Id);
14251 end Fixup_Bad_Constraint;
14252
14253 -- Local variables
14254
14255 C : Node_Id;
14256 Constr : Elist_Id := New_Elmt_List;
14257
14258 -- Start of processing for Constrain_Discriminated_Type
14259
14260 begin
14261 C := Constraint (S);
14262
14263 -- A discriminant constraint is only allowed in a subtype indication,
14264 -- after a subtype mark. This subtype mark must denote either a type
14265 -- with discriminants, or an access type whose designated type is a
14266 -- type with discriminants. A discriminant constraint specifies the
14267 -- values of these discriminants (RM 3.7.2(5)).
14268
14269 T := Base_Type (Entity (Subtype_Mark (S)));
14270
14271 if Is_Access_Type (T) then
14272 T := Designated_Type (T);
14273 end if;
14274
14275 -- In an instance it may be necessary to retrieve the full view of a
14276 -- type with unknown discriminants, or a full view with defaulted
14277 -- discriminants. In other contexts the constraint is illegal.
14278
14279 if In_Instance
14280 and then Is_Private_Type (T)
14281 and then Present (Full_View (T))
14282 and then
14283 (Has_Unknown_Discriminants (T)
14284 or else
14285 (not Has_Discriminants (T)
14286 and then Has_Defaulted_Discriminants (Full_View (T))))
14287 then
14288 T := Full_View (T);
14289 E := Full_View (E);
14290 end if;
14291
14292 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14293 -- generating an error for access-to-incomplete subtypes.
14294
14295 if Ada_Version >= Ada_2005
14296 and then Ekind (T) = E_Incomplete_Type
14297 and then Nkind (Parent (S)) = N_Subtype_Declaration
14298 and then not Is_Itype (Def_Id)
14299 then
14300 -- A little sanity check: emit an error message if the type has
14301 -- discriminants to begin with. Type T may be a regular incomplete
14302 -- type or imported via a limited with clause.
14303
14304 if Has_Discriminants (T)
14305 or else (From_Limited_With (T)
14306 and then Present (Non_Limited_View (T))
14307 and then Nkind (Parent (Non_Limited_View (T))) =
14308 N_Full_Type_Declaration
14309 and then Present (Discriminant_Specifications
14310 (Parent (Non_Limited_View (T)))))
14311 then
14312 Error_Msg_N
14313 ("(Ada 2005) incomplete subtype may not be constrained", C);
14314 else
14315 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14316 end if;
14317
14318 Fixup_Bad_Constraint;
14319 return;
14320
14321 -- Check that the type has visible discriminants. The type may be
14322 -- a private type with unknown discriminants whose full view has
14323 -- discriminants which are invisible.
14324
14325 elsif not Has_Discriminants (T)
14326 or else
14327 (Has_Unknown_Discriminants (T)
14328 and then Is_Private_Type (T))
14329 then
14330 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14331 Fixup_Bad_Constraint;
14332 return;
14333
14334 elsif Is_Constrained (E)
14335 or else (Ekind (E) = E_Class_Wide_Subtype
14336 and then Present (Discriminant_Constraint (E)))
14337 then
14338 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14339 Fixup_Bad_Constraint;
14340 return;
14341 end if;
14342
14343 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14344 -- applies to the base type.
14345
14346 T := Base_Type (T);
14347
14348 Constr := Build_Discriminant_Constraints (T, S);
14349
14350 -- If the list returned was empty we had an error in building the
14351 -- discriminant constraint. We have also already signalled an error
14352 -- in the incomplete type case
14353
14354 if Is_Empty_Elmt_List (Constr) then
14355 Fixup_Bad_Constraint;
14356 return;
14357 end if;
14358
14359 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14360 end Constrain_Discriminated_Type;
14361
14362 ---------------------------
14363 -- Constrain_Enumeration --
14364 ---------------------------
14365
14366 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14367 T : constant Entity_Id := Entity (Subtype_Mark (S));
14368 C : constant Node_Id := Constraint (S);
14369
14370 begin
14371 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14372
14373 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14374
14375 Set_Etype (Def_Id, Base_Type (T));
14376 Set_Size_Info (Def_Id, (T));
14377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14378 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14379
14380 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14381
14382 Set_Discrete_RM_Size (Def_Id);
14383 end Constrain_Enumeration;
14384
14385 ----------------------
14386 -- Constrain_Float --
14387 ----------------------
14388
14389 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14390 T : constant Entity_Id := Entity (Subtype_Mark (S));
14391 C : Node_Id;
14392 D : Node_Id;
14393 Rais : Node_Id;
14394
14395 begin
14396 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14397
14398 Set_Etype (Def_Id, Base_Type (T));
14399 Set_Size_Info (Def_Id, (T));
14400 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14401
14402 -- Process the constraint
14403
14404 C := Constraint (S);
14405
14406 -- Digits constraint present
14407
14408 if Nkind (C) = N_Digits_Constraint then
14409 Check_Restriction (No_Obsolescent_Features, C);
14410
14411 if Warn_On_Obsolescent_Feature then
14412 Error_Msg_N
14413 ("subtype digits constraint is an " &
14414 "obsolescent feature (RM J.3(8))?j?", C);
14415 end if;
14416
14417 D := Digits_Expression (C);
14418 Analyze_And_Resolve (D, Any_Integer);
14419 Check_Digits_Expression (D);
14420 Set_Digits_Value (Def_Id, Expr_Value (D));
14421
14422 -- Check that digits value is in range. Obviously we can do this
14423 -- at compile time, but it is strictly a runtime check, and of
14424 -- course there is an ACVC test that checks this.
14425
14426 if Digits_Value (Def_Id) > Digits_Value (T) then
14427 Error_Msg_Uint_1 := Digits_Value (T);
14428 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14429 Rais :=
14430 Make_Raise_Constraint_Error (Sloc (D),
14431 Reason => CE_Range_Check_Failed);
14432 Insert_Action (Declaration_Node (Def_Id), Rais);
14433 end if;
14434
14435 C := Range_Constraint (C);
14436
14437 -- No digits constraint present
14438
14439 else
14440 Set_Digits_Value (Def_Id, Digits_Value (T));
14441 end if;
14442
14443 -- Range constraint present
14444
14445 if Nkind (C) = N_Range_Constraint then
14446 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14447
14448 -- No range constraint present
14449
14450 else
14451 pragma Assert (No (C));
14452 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14453 end if;
14454
14455 Set_Is_Constrained (Def_Id);
14456 end Constrain_Float;
14457
14458 ---------------------
14459 -- Constrain_Index --
14460 ---------------------
14461
14462 procedure Constrain_Index
14463 (Index : Node_Id;
14464 S : Node_Id;
14465 Related_Nod : Node_Id;
14466 Related_Id : Entity_Id;
14467 Suffix : Character;
14468 Suffix_Index : Pos)
14469 is
14470 Def_Id : Entity_Id;
14471 R : Node_Id := Empty;
14472 T : constant Entity_Id := Etype (Index);
14473 Is_FLB_Index : Boolean := False;
14474
14475 begin
14476 Def_Id :=
14477 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14478 Set_Etype (Def_Id, Base_Type (T));
14479
14480 if Nkind (S) = N_Range
14481 or else
14482 (Nkind (S) = N_Attribute_Reference
14483 and then Attribute_Name (S) = Name_Range)
14484 then
14485 -- A Range attribute will be transformed into N_Range by Resolve
14486
14487 -- If a range has an Empty upper bound, then remember that for later
14488 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14489 -- flag, and also set the upper bound of the range to the index
14490 -- subtype's upper bound rather than leaving it Empty. In truth,
14491 -- that upper bound corresponds to a box ("<>"), but it's convenient
14492 -- to set it to the upper bound to avoid needing to add special tests
14493 -- in various places for an Empty upper bound, and in any case it
14494 -- accurately characterizes the index's range of values.
14495
14496 if Nkind (S) = N_Range and then not Present (High_Bound (S)) then
14497 Is_FLB_Index := True;
14498 Set_High_Bound (S, Type_High_Bound (T));
14499 end if;
14500
14501 R := S;
14502
14503 Process_Range_Expr_In_Decl (R, T);
14504
14505 if not Error_Posted (S)
14506 and then
14507 (Nkind (S) /= N_Range
14508 or else not Covers (T, (Etype (Low_Bound (S))))
14509 or else not Covers (T, (Etype (High_Bound (S)))))
14510 then
14511 if Base_Type (T) /= Any_Type
14512 and then Etype (Low_Bound (S)) /= Any_Type
14513 and then Etype (High_Bound (S)) /= Any_Type
14514 then
14515 Error_Msg_N ("range expected", S);
14516 end if;
14517 end if;
14518
14519 elsif Nkind (S) = N_Subtype_Indication then
14520
14521 -- The parser has verified that this is a discrete indication
14522
14523 Resolve_Discrete_Subtype_Indication (S, T);
14524 Bad_Predicated_Subtype_Use
14525 ("subtype& has predicate, not allowed in index constraint",
14526 S, Entity (Subtype_Mark (S)));
14527
14528 R := Range_Expression (Constraint (S));
14529
14530 -- Capture values of bounds and generate temporaries for them if
14531 -- needed, since checks may cause duplication of the expressions
14532 -- which must not be reevaluated.
14533
14534 -- The forced evaluation removes side effects from expressions, which
14535 -- should occur also in GNATprove mode. Otherwise, we end up with
14536 -- unexpected insertions of actions at places where this is not
14537 -- supposed to occur, e.g. on default parameters of a call.
14538
14539 if Expander_Active or GNATprove_Mode then
14540 Force_Evaluation
14541 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14542 Force_Evaluation
14543 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14544 end if;
14545
14546 elsif Nkind (S) = N_Discriminant_Association then
14547
14548 -- Syntactically valid in subtype indication
14549
14550 Error_Msg_N ("invalid index constraint", S);
14551 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14552 return;
14553
14554 -- Subtype_Mark case, no anonymous subtypes to construct
14555
14556 else
14557 Analyze (S);
14558
14559 if Is_Entity_Name (S) then
14560 if not Is_Type (Entity (S)) then
14561 Error_Msg_N ("expect subtype mark for index constraint", S);
14562
14563 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14564 Wrong_Type (S, Base_Type (T));
14565
14566 -- Check error of subtype with predicate in index constraint
14567
14568 else
14569 Bad_Predicated_Subtype_Use
14570 ("subtype& has predicate, not allowed in index constraint",
14571 S, Entity (S));
14572 end if;
14573
14574 return;
14575
14576 else
14577 Error_Msg_N ("invalid index constraint", S);
14578 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14579 return;
14580 end if;
14581 end if;
14582
14583 -- Complete construction of the Itype
14584
14585 if Is_Modular_Integer_Type (T) then
14586 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14587
14588 elsif Is_Integer_Type (T) then
14589 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14590
14591 else
14592 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14593 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14594 Set_First_Literal (Def_Id, First_Literal (T));
14595 end if;
14596
14597 Set_Size_Info (Def_Id, (T));
14598 Copy_RM_Size (To => Def_Id, From => T);
14599 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14600
14601 -- If this is a range for a fixed-lower-bound subtype, then set the
14602 -- index itype's low bound to the FLB and the index itype's upper bound
14603 -- to the high bound of the parent array type's index subtype. Also,
14604 -- mark the itype as an FLB index subtype.
14605
14606 if Nkind (S) = N_Range and then Is_FLB_Index then
14607 Set_Scalar_Range
14608 (Def_Id,
14609 Make_Range (Sloc (S),
14610 Low_Bound => Low_Bound (S),
14611 High_Bound => Type_High_Bound (T)));
14612 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14613
14614 else
14615 Set_Scalar_Range (Def_Id, R);
14616 end if;
14617
14618 Set_Etype (S, Def_Id);
14619 Set_Discrete_RM_Size (Def_Id);
14620 end Constrain_Index;
14621
14622 -----------------------
14623 -- Constrain_Integer --
14624 -----------------------
14625
14626 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14627 T : constant Entity_Id := Entity (Subtype_Mark (S));
14628 C : constant Node_Id := Constraint (S);
14629
14630 begin
14631 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14632
14633 if Is_Modular_Integer_Type (T) then
14634 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14635 else
14636 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14637 end if;
14638
14639 Set_Etype (Def_Id, Base_Type (T));
14640 Set_Size_Info (Def_Id, (T));
14641 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14642 Set_Discrete_RM_Size (Def_Id);
14643 end Constrain_Integer;
14644
14645 ------------------------------
14646 -- Constrain_Ordinary_Fixed --
14647 ------------------------------
14648
14649 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14650 T : constant Entity_Id := Entity (Subtype_Mark (S));
14651 C : Node_Id;
14652 D : Node_Id;
14653 Rais : Node_Id;
14654
14655 begin
14656 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14657 Set_Etype (Def_Id, Base_Type (T));
14658 Set_Size_Info (Def_Id, (T));
14659 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14660 Set_Small_Value (Def_Id, Small_Value (T));
14661
14662 -- Process the constraint
14663
14664 C := Constraint (S);
14665
14666 -- Delta constraint present
14667
14668 if Nkind (C) = N_Delta_Constraint then
14669 Check_Restriction (No_Obsolescent_Features, C);
14670
14671 if Warn_On_Obsolescent_Feature then
14672 Error_Msg_S
14673 ("subtype delta constraint is an " &
14674 "obsolescent feature (RM J.3(7))?j?");
14675 end if;
14676
14677 D := Delta_Expression (C);
14678 Analyze_And_Resolve (D, Any_Real);
14679 Check_Delta_Expression (D);
14680 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14681
14682 -- Check that delta value is in range. Obviously we can do this
14683 -- at compile time, but it is strictly a runtime check, and of
14684 -- course there is an ACVC test that checks this.
14685
14686 if Delta_Value (Def_Id) < Delta_Value (T) then
14687 Error_Msg_N ("??delta value is too small", D);
14688 Rais :=
14689 Make_Raise_Constraint_Error (Sloc (D),
14690 Reason => CE_Range_Check_Failed);
14691 Insert_Action (Declaration_Node (Def_Id), Rais);
14692 end if;
14693
14694 C := Range_Constraint (C);
14695
14696 -- No delta constraint present
14697
14698 else
14699 Set_Delta_Value (Def_Id, Delta_Value (T));
14700 end if;
14701
14702 -- Range constraint present
14703
14704 if Nkind (C) = N_Range_Constraint then
14705 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14706
14707 -- No range constraint present
14708
14709 else
14710 pragma Assert (No (C));
14711 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14712 end if;
14713
14714 Set_Discrete_RM_Size (Def_Id);
14715
14716 -- Unconditionally delay the freeze, since we cannot set size
14717 -- information in all cases correctly until the freeze point.
14718
14719 Set_Has_Delayed_Freeze (Def_Id);
14720 end Constrain_Ordinary_Fixed;
14721
14722 -----------------------
14723 -- Contain_Interface --
14724 -----------------------
14725
14726 function Contain_Interface
14727 (Iface : Entity_Id;
14728 Ifaces : Elist_Id) return Boolean
14729 is
14730 Iface_Elmt : Elmt_Id;
14731
14732 begin
14733 if Present (Ifaces) then
14734 Iface_Elmt := First_Elmt (Ifaces);
14735 while Present (Iface_Elmt) loop
14736 if Node (Iface_Elmt) = Iface then
14737 return True;
14738 end if;
14739
14740 Next_Elmt (Iface_Elmt);
14741 end loop;
14742 end if;
14743
14744 return False;
14745 end Contain_Interface;
14746
14747 ---------------------------
14748 -- Convert_Scalar_Bounds --
14749 ---------------------------
14750
14751 procedure Convert_Scalar_Bounds
14752 (N : Node_Id;
14753 Parent_Type : Entity_Id;
14754 Derived_Type : Entity_Id;
14755 Loc : Source_Ptr)
14756 is
14757 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14758
14759 Lo : Node_Id;
14760 Hi : Node_Id;
14761 Rng : Node_Id;
14762
14763 begin
14764 -- Defend against previous errors
14765
14766 if No (Scalar_Range (Derived_Type)) then
14767 Check_Error_Detected;
14768 return;
14769 end if;
14770
14771 Lo := Build_Scalar_Bound
14772 (Type_Low_Bound (Derived_Type),
14773 Parent_Type, Implicit_Base);
14774
14775 Hi := Build_Scalar_Bound
14776 (Type_High_Bound (Derived_Type),
14777 Parent_Type, Implicit_Base);
14778
14779 Rng :=
14780 Make_Range (Loc,
14781 Low_Bound => Lo,
14782 High_Bound => Hi);
14783
14784 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14785
14786 Set_Parent (Rng, N);
14787 Set_Scalar_Range (Derived_Type, Rng);
14788
14789 -- Analyze the bounds
14790
14791 Analyze_And_Resolve (Lo, Implicit_Base);
14792 Analyze_And_Resolve (Hi, Implicit_Base);
14793
14794 -- Analyze the range itself, except that we do not analyze it if
14795 -- the bounds are real literals, and we have a fixed-point type.
14796 -- The reason for this is that we delay setting the bounds in this
14797 -- case till we know the final Small and Size values (see circuit
14798 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14799
14800 if Is_Fixed_Point_Type (Parent_Type)
14801 and then Nkind (Lo) = N_Real_Literal
14802 and then Nkind (Hi) = N_Real_Literal
14803 then
14804 return;
14805
14806 -- Here we do the analysis of the range
14807
14808 -- Note: we do this manually, since if we do a normal Analyze and
14809 -- Resolve call, there are problems with the conversions used for
14810 -- the derived type range.
14811
14812 else
14813 Set_Etype (Rng, Implicit_Base);
14814 Set_Analyzed (Rng, True);
14815 end if;
14816 end Convert_Scalar_Bounds;
14817
14818 -------------------
14819 -- Copy_And_Swap --
14820 -------------------
14821
14822 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14823 begin
14824 -- Initialize new full declaration entity by copying the pertinent
14825 -- fields of the corresponding private declaration entity.
14826
14827 -- We temporarily set Ekind to a value appropriate for a type to
14828 -- avoid assert failures in Einfo from checking for setting type
14829 -- attributes on something that is not a type. Ekind (Priv) is an
14830 -- appropriate choice, since it allowed the attributes to be set
14831 -- in the first place. This Ekind value will be modified later.
14832
14833 Mutate_Ekind (Full, Ekind (Priv));
14834
14835 -- Also set Etype temporarily to Any_Type, again, in the absence
14836 -- of errors, it will be properly reset, and if there are errors,
14837 -- then we want a value of Any_Type to remain.
14838
14839 Set_Etype (Full, Any_Type);
14840
14841 -- Now start copying attributes
14842
14843 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14844
14845 if Has_Discriminants (Full) then
14846 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14847 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14848 end if;
14849
14850 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14851 Set_Homonym (Full, Homonym (Priv));
14852 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14853 Set_Is_Public (Full, Is_Public (Priv));
14854 Set_Is_Pure (Full, Is_Pure (Priv));
14855 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14856 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14857 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14858 Set_Has_Pragma_Unreferenced_Objects
14859 (Full, Has_Pragma_Unreferenced_Objects
14860 (Priv));
14861
14862 Conditional_Delay (Full, Priv);
14863
14864 if Is_Tagged_Type (Full) then
14865 Set_Direct_Primitive_Operations
14866 (Full, Direct_Primitive_Operations (Priv));
14867 Set_No_Tagged_Streams_Pragma
14868 (Full, No_Tagged_Streams_Pragma (Priv));
14869
14870 if Is_Base_Type (Priv) then
14871 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14872 end if;
14873 end if;
14874
14875 Set_Is_Volatile (Full, Is_Volatile (Priv));
14876 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14877 Set_Scope (Full, Scope (Priv));
14878 Set_Prev_Entity (Full, Prev_Entity (Priv));
14879 Set_Next_Entity (Full, Next_Entity (Priv));
14880 Set_First_Entity (Full, First_Entity (Priv));
14881 Set_Last_Entity (Full, Last_Entity (Priv));
14882
14883 -- If access types have been recorded for later handling, keep them in
14884 -- the full view so that they get handled when the full view freeze
14885 -- node is expanded.
14886
14887 if Present (Freeze_Node (Priv))
14888 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14889 then
14890 Ensure_Freeze_Node (Full);
14891 Set_Access_Types_To_Process
14892 (Freeze_Node (Full),
14893 Access_Types_To_Process (Freeze_Node (Priv)));
14894 end if;
14895
14896 -- Swap the two entities. Now Private is the full type entity and Full
14897 -- is the private one. They will be swapped back at the end of the
14898 -- private part. This swapping ensures that the entity that is visible
14899 -- in the private part is the full declaration.
14900
14901 Exchange_Entities (Priv, Full);
14902 Append_Entity (Full, Scope (Full));
14903 end Copy_And_Swap;
14904
14905 -------------------------------------
14906 -- Copy_Array_Base_Type_Attributes --
14907 -------------------------------------
14908
14909 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14910 begin
14911 Set_Component_Alignment (T1, Component_Alignment (T2));
14912 Set_Component_Type (T1, Component_Type (T2));
14913 Set_Component_Size (T1, Component_Size (T2));
14914 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14915 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14916 Propagate_Concurrent_Flags (T1, T2);
14917 Set_Is_Packed (T1, Is_Packed (T2));
14918 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14919 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14920 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14921 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14922 end Copy_Array_Base_Type_Attributes;
14923
14924 -----------------------------------
14925 -- Copy_Array_Subtype_Attributes --
14926 -----------------------------------
14927
14928 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14929 begin
14930 Set_Size_Info (T1, T2);
14931
14932 Set_First_Index (T1, First_Index (T2));
14933 Set_Is_Aliased (T1, Is_Aliased (T2));
14934 Set_Is_Atomic (T1, Is_Atomic (T2));
14935 Set_Is_Independent (T1, Is_Independent (T2));
14936 Set_Is_Volatile (T1, Is_Volatile (T2));
14937 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14938 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14939 Set_Is_Constrained (T1, Is_Constrained (T2));
14940 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14941 Inherit_Rep_Item_Chain (T1, T2);
14942 Set_Convention (T1, Convention (T2));
14943 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14944 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14945 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14946 end Copy_Array_Subtype_Attributes;
14947
14948 -----------------------------------
14949 -- Create_Constrained_Components --
14950 -----------------------------------
14951
14952 procedure Create_Constrained_Components
14953 (Subt : Entity_Id;
14954 Decl_Node : Node_Id;
14955 Typ : Entity_Id;
14956 Constraints : Elist_Id)
14957 is
14958 Loc : constant Source_Ptr := Sloc (Subt);
14959 Comp_List : constant Elist_Id := New_Elmt_List;
14960 Parent_Type : constant Entity_Id := Etype (Typ);
14961 Assoc_List : constant List_Id := New_List;
14962
14963 Discr_Val : Elmt_Id;
14964 Errors : Boolean;
14965 New_C : Entity_Id;
14966 Old_C : Entity_Id;
14967 Is_Static : Boolean := True;
14968 Is_Compile_Time_Known : Boolean := True;
14969
14970 procedure Collect_Fixed_Components (Typ : Entity_Id);
14971 -- Collect parent type components that do not appear in a variant part
14972
14973 procedure Create_All_Components;
14974 -- Iterate over Comp_List to create the components of the subtype
14975
14976 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14977 -- Creates a new component from Old_Compon, copying all the fields from
14978 -- it, including its Etype, inserts the new component in the Subt entity
14979 -- chain and returns the new component.
14980
14981 function Is_Variant_Record (T : Entity_Id) return Boolean;
14982 -- If true, and discriminants are static, collect only components from
14983 -- variants selected by discriminant values.
14984
14985 ------------------------------
14986 -- Collect_Fixed_Components --
14987 ------------------------------
14988
14989 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14990 begin
14991 -- Build association list for discriminants, and find components of the
14992 -- variant part selected by the values of the discriminants.
14993
14994 Old_C := First_Discriminant (Typ);
14995 Discr_Val := First_Elmt (Constraints);
14996 while Present (Old_C) loop
14997 Append_To (Assoc_List,
14998 Make_Component_Association (Loc,
14999 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15000 Expression => New_Copy (Node (Discr_Val))));
15001
15002 Next_Elmt (Discr_Val);
15003 Next_Discriminant (Old_C);
15004 end loop;
15005
15006 -- The tag and the possible parent component are unconditionally in
15007 -- the subtype.
15008
15009 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15010 Old_C := First_Component (Typ);
15011 while Present (Old_C) loop
15012 if Chars (Old_C) in Name_uTag | Name_uParent then
15013 Append_Elmt (Old_C, Comp_List);
15014 end if;
15015
15016 Next_Component (Old_C);
15017 end loop;
15018 end if;
15019 end Collect_Fixed_Components;
15020
15021 ---------------------------
15022 -- Create_All_Components --
15023 ---------------------------
15024
15025 procedure Create_All_Components is
15026 Comp : Elmt_Id;
15027
15028 begin
15029 Comp := First_Elmt (Comp_List);
15030 while Present (Comp) loop
15031 Old_C := Node (Comp);
15032 New_C := Create_Component (Old_C);
15033
15034 Set_Etype
15035 (New_C,
15036 Constrain_Component_Type
15037 (Old_C, Subt, Decl_Node, Typ, Constraints));
15038 Set_Is_Public (New_C, Is_Public (Subt));
15039
15040 Next_Elmt (Comp);
15041 end loop;
15042 end Create_All_Components;
15043
15044 ----------------------
15045 -- Create_Component --
15046 ----------------------
15047
15048 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15049 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15050
15051 begin
15052 if Ekind (Old_Compon) = E_Discriminant
15053 and then Is_Completely_Hidden (Old_Compon)
15054 then
15055 -- This is a shadow discriminant created for a discriminant of
15056 -- the parent type, which needs to be present in the subtype.
15057 -- Give the shadow discriminant an internal name that cannot
15058 -- conflict with that of visible components.
15059
15060 Set_Chars (New_Compon, New_Internal_Name ('C'));
15061 end if;
15062
15063 -- Set the parent so we have a proper link for freezing etc. This is
15064 -- not a real parent pointer, since of course our parent does not own
15065 -- up to us and reference us, we are an illegitimate child of the
15066 -- original parent.
15067
15068 Set_Parent (New_Compon, Parent (Old_Compon));
15069
15070 -- We do not want this node marked as Comes_From_Source, since
15071 -- otherwise it would get first class status and a separate cross-
15072 -- reference line would be generated. Illegitimate children do not
15073 -- rate such recognition.
15074
15075 Set_Comes_From_Source (New_Compon, False);
15076
15077 -- But it is a real entity, and a birth certificate must be properly
15078 -- registered by entering it into the entity list, and setting its
15079 -- scope to the given subtype. This turns out to be useful for the
15080 -- LLVM code generator, but that scope is not used otherwise.
15081
15082 Enter_Name (New_Compon);
15083 Set_Scope (New_Compon, Subt);
15084
15085 return New_Compon;
15086 end Create_Component;
15087
15088 -----------------------
15089 -- Is_Variant_Record --
15090 -----------------------
15091
15092 function Is_Variant_Record (T : Entity_Id) return Boolean is
15093 begin
15094 return Nkind (Parent (T)) = N_Full_Type_Declaration
15095 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
15096 and then Present (Component_List (Type_Definition (Parent (T))))
15097 and then
15098 Present
15099 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
15100 end Is_Variant_Record;
15101
15102 -- Start of processing for Create_Constrained_Components
15103
15104 begin
15105 pragma Assert (Subt /= Base_Type (Subt));
15106 pragma Assert (Typ = Base_Type (Typ));
15107
15108 Set_First_Entity (Subt, Empty);
15109 Set_Last_Entity (Subt, Empty);
15110
15111 -- Check whether constraint is fully static, in which case we can
15112 -- optimize the list of components.
15113
15114 Discr_Val := First_Elmt (Constraints);
15115 while Present (Discr_Val) loop
15116 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15117 Is_Static := False;
15118
15119 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15120 Is_Compile_Time_Known := False;
15121 exit;
15122 end if;
15123 end if;
15124
15125 Next_Elmt (Discr_Val);
15126 end loop;
15127
15128 Set_Has_Static_Discriminants (Subt, Is_Static);
15129
15130 Push_Scope (Subt);
15131
15132 -- Inherit the discriminants of the parent type
15133
15134 Add_Discriminants : declare
15135 Num_Disc : Nat;
15136 Num_Gird : Nat;
15137
15138 begin
15139 Num_Disc := 0;
15140 Old_C := First_Discriminant (Typ);
15141
15142 while Present (Old_C) loop
15143 Num_Disc := Num_Disc + 1;
15144 New_C := Create_Component (Old_C);
15145 Set_Is_Public (New_C, Is_Public (Subt));
15146 Next_Discriminant (Old_C);
15147 end loop;
15148
15149 -- For an untagged derived subtype, the number of discriminants may
15150 -- be smaller than the number of inherited discriminants, because
15151 -- several of them may be renamed by a single new discriminant or
15152 -- constrained. In this case, add the hidden discriminants back into
15153 -- the subtype, because they need to be present if the optimizer of
15154 -- the GCC 4.x back-end decides to break apart assignments between
15155 -- objects using the parent view into member-wise assignments.
15156
15157 Num_Gird := 0;
15158
15159 if Is_Derived_Type (Typ)
15160 and then not Is_Tagged_Type (Typ)
15161 then
15162 Old_C := First_Stored_Discriminant (Typ);
15163
15164 while Present (Old_C) loop
15165 Num_Gird := Num_Gird + 1;
15166 Next_Stored_Discriminant (Old_C);
15167 end loop;
15168 end if;
15169
15170 if Num_Gird > Num_Disc then
15171
15172 -- Find out multiple uses of new discriminants, and add hidden
15173 -- components for the extra renamed discriminants. We recognize
15174 -- multiple uses through the Corresponding_Discriminant of a
15175 -- new discriminant: if it constrains several old discriminants,
15176 -- this field points to the last one in the parent type. The
15177 -- stored discriminants of the derived type have the same name
15178 -- as those of the parent.
15179
15180 declare
15181 Constr : Elmt_Id;
15182 New_Discr : Entity_Id;
15183 Old_Discr : Entity_Id;
15184
15185 begin
15186 Constr := First_Elmt (Stored_Constraint (Typ));
15187 Old_Discr := First_Stored_Discriminant (Typ);
15188 while Present (Constr) loop
15189 if Is_Entity_Name (Node (Constr))
15190 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15191 then
15192 New_Discr := Entity (Node (Constr));
15193
15194 if Chars (Corresponding_Discriminant (New_Discr)) /=
15195 Chars (Old_Discr)
15196 then
15197 -- The new discriminant has been used to rename a
15198 -- subsequent old discriminant. Introduce a shadow
15199 -- component for the current old discriminant.
15200
15201 New_C := Create_Component (Old_Discr);
15202 Set_Original_Record_Component (New_C, Old_Discr);
15203 end if;
15204
15205 else
15206 -- The constraint has eliminated the old discriminant.
15207 -- Introduce a shadow component.
15208
15209 New_C := Create_Component (Old_Discr);
15210 Set_Original_Record_Component (New_C, Old_Discr);
15211 end if;
15212
15213 Next_Elmt (Constr);
15214 Next_Stored_Discriminant (Old_Discr);
15215 end loop;
15216 end;
15217 end if;
15218 end Add_Discriminants;
15219
15220 if Is_Compile_Time_Known
15221 and then Is_Variant_Record (Typ)
15222 then
15223 Collect_Fixed_Components (Typ);
15224 Gather_Components
15225 (Typ,
15226 Component_List (Type_Definition (Parent (Typ))),
15227 Governed_By => Assoc_List,
15228 Into => Comp_List,
15229 Report_Errors => Errors,
15230 Allow_Compile_Time => True);
15231 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15232
15233 Create_All_Components;
15234
15235 -- If the subtype declaration is created for a tagged type derivation
15236 -- with constraints, we retrieve the record definition of the parent
15237 -- type to select the components of the proper variant.
15238
15239 elsif Is_Compile_Time_Known
15240 and then Is_Tagged_Type (Typ)
15241 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15242 and then
15243 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15244 and then Is_Variant_Record (Parent_Type)
15245 then
15246 Collect_Fixed_Components (Typ);
15247 Gather_Components
15248 (Typ,
15249 Component_List (Type_Definition (Parent (Parent_Type))),
15250 Governed_By => Assoc_List,
15251 Into => Comp_List,
15252 Report_Errors => Errors,
15253 Allow_Compile_Time => True);
15254
15255 -- Note: previously there was a check at this point that no errors
15256 -- were detected. As a consequence of AI05-220 there may be an error
15257 -- if an inherited discriminant that controls a variant has a non-
15258 -- static constraint.
15259
15260 -- If the tagged derivation has a type extension, collect all the
15261 -- new relevant components therein via Gather_Components.
15262
15263 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15264 then
15265 Gather_Components
15266 (Typ,
15267 Component_List
15268 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15269 Governed_By => Assoc_List,
15270 Into => Comp_List,
15271 Report_Errors => Errors,
15272 Allow_Compile_Time => True,
15273 Include_Interface_Tag => True);
15274 end if;
15275
15276 Create_All_Components;
15277
15278 else
15279 -- If discriminants are not static, or if this is a multi-level type
15280 -- extension, we have to include all components of the parent type.
15281
15282 Old_C := First_Component (Typ);
15283 while Present (Old_C) loop
15284 New_C := Create_Component (Old_C);
15285
15286 Set_Etype
15287 (New_C,
15288 Constrain_Component_Type
15289 (Old_C, Subt, Decl_Node, Typ, Constraints));
15290 Set_Is_Public (New_C, Is_Public (Subt));
15291
15292 Next_Component (Old_C);
15293 end loop;
15294 end if;
15295
15296 End_Scope;
15297 end Create_Constrained_Components;
15298
15299 ------------------------------------------
15300 -- Decimal_Fixed_Point_Type_Declaration --
15301 ------------------------------------------
15302
15303 procedure Decimal_Fixed_Point_Type_Declaration
15304 (T : Entity_Id;
15305 Def : Node_Id)
15306 is
15307 Loc : constant Source_Ptr := Sloc (Def);
15308 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15309 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15310 Max_Digits : constant Nat :=
15311 (if System_Max_Integer_Size = 128 then 38 else 18);
15312 -- Maximum number of digits that can be represented in an integer
15313
15314 Implicit_Base : Entity_Id;
15315 Digs_Val : Uint;
15316 Delta_Val : Ureal;
15317 Scale_Val : Uint;
15318 Bound_Val : Ureal;
15319
15320 begin
15321 Check_Restriction (No_Fixed_Point, Def);
15322
15323 -- Create implicit base type
15324
15325 Implicit_Base :=
15326 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15327 Set_Etype (Implicit_Base, Implicit_Base);
15328
15329 -- Analyze and process delta expression
15330
15331 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15332
15333 Check_Delta_Expression (Delta_Expr);
15334 Delta_Val := Expr_Value_R (Delta_Expr);
15335
15336 -- Check delta is power of 10, and determine scale value from it
15337
15338 declare
15339 Val : Ureal;
15340
15341 begin
15342 Scale_Val := Uint_0;
15343 Val := Delta_Val;
15344
15345 if Val < Ureal_1 then
15346 while Val < Ureal_1 loop
15347 Val := Val * Ureal_10;
15348 Scale_Val := Scale_Val + 1;
15349 end loop;
15350
15351 if Scale_Val > Max_Digits then
15352 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15353 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15354 Scale_Val := UI_From_Int (Max_Digits);
15355 end if;
15356
15357 else
15358 while Val > Ureal_1 loop
15359 Val := Val / Ureal_10;
15360 Scale_Val := Scale_Val - 1;
15361 end loop;
15362
15363 if Scale_Val < -Max_Digits then
15364 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15365 Error_Msg_N ("scale is less than minimum value of ^", Def);
15366 Scale_Val := UI_From_Int (-Max_Digits);
15367 end if;
15368 end if;
15369
15370 if Val /= Ureal_1 then
15371 Error_Msg_N ("delta expression must be a power of 10", Def);
15372 Delta_Val := Ureal_10 ** (-Scale_Val);
15373 end if;
15374 end;
15375
15376 -- Set delta, scale and small (small = delta for decimal type)
15377
15378 Set_Delta_Value (Implicit_Base, Delta_Val);
15379 Set_Scale_Value (Implicit_Base, Scale_Val);
15380 Set_Small_Value (Implicit_Base, Delta_Val);
15381
15382 -- Analyze and process digits expression
15383
15384 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15385 Check_Digits_Expression (Digs_Expr);
15386 Digs_Val := Expr_Value (Digs_Expr);
15387
15388 if Digs_Val > Max_Digits then
15389 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15390 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15391 Digs_Val := UI_From_Int (Max_Digits);
15392 end if;
15393
15394 Set_Digits_Value (Implicit_Base, Digs_Val);
15395 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15396
15397 -- Set range of base type from digits value for now. This will be
15398 -- expanded to represent the true underlying base range by Freeze.
15399
15400 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15401
15402 -- Note: We leave Esize unset for now, size will be set at freeze
15403 -- time. We have to do this for ordinary fixed-point, because the size
15404 -- depends on the specified small, and we might as well do the same for
15405 -- decimal fixed-point.
15406
15407 pragma Assert (not Known_Esize (Implicit_Base));
15408
15409 -- If there are bounds given in the declaration use them as the
15410 -- bounds of the first named subtype.
15411
15412 if Present (Real_Range_Specification (Def)) then
15413 declare
15414 RRS : constant Node_Id := Real_Range_Specification (Def);
15415 Low : constant Node_Id := Low_Bound (RRS);
15416 High : constant Node_Id := High_Bound (RRS);
15417 Low_Val : Ureal;
15418 High_Val : Ureal;
15419
15420 begin
15421 Analyze_And_Resolve (Low, Any_Real);
15422 Analyze_And_Resolve (High, Any_Real);
15423 Check_Real_Bound (Low);
15424 Check_Real_Bound (High);
15425 Low_Val := Expr_Value_R (Low);
15426 High_Val := Expr_Value_R (High);
15427
15428 if Low_Val < (-Bound_Val) then
15429 Error_Msg_N
15430 ("range low bound too small for digits value", Low);
15431 Low_Val := -Bound_Val;
15432 end if;
15433
15434 if High_Val > Bound_Val then
15435 Error_Msg_N
15436 ("range high bound too large for digits value", High);
15437 High_Val := Bound_Val;
15438 end if;
15439
15440 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15441 end;
15442
15443 -- If no explicit range, use range that corresponds to given
15444 -- digits value. This will end up as the final range for the
15445 -- first subtype.
15446
15447 else
15448 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15449 end if;
15450
15451 -- Complete entity for first subtype. The inheritance of the rep item
15452 -- chain ensures that SPARK-related pragmas are not clobbered when the
15453 -- decimal fixed point type acts as a full view of a private type.
15454
15455 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15456 Set_Etype (T, Implicit_Base);
15457 Set_Size_Info (T, Implicit_Base);
15458 Inherit_Rep_Item_Chain (T, Implicit_Base);
15459 Set_Digits_Value (T, Digs_Val);
15460 Set_Delta_Value (T, Delta_Val);
15461 Set_Small_Value (T, Delta_Val);
15462 Set_Scale_Value (T, Scale_Val);
15463 Set_Is_Constrained (T);
15464 end Decimal_Fixed_Point_Type_Declaration;
15465
15466 -----------------------------------
15467 -- Derive_Progenitor_Subprograms --
15468 -----------------------------------
15469
15470 procedure Derive_Progenitor_Subprograms
15471 (Parent_Type : Entity_Id;
15472 Tagged_Type : Entity_Id)
15473 is
15474 E : Entity_Id;
15475 Elmt : Elmt_Id;
15476 Iface : Entity_Id;
15477 Iface_Alias : Entity_Id;
15478 Iface_Elmt : Elmt_Id;
15479 Iface_Subp : Entity_Id;
15480 New_Subp : Entity_Id := Empty;
15481 Prim_Elmt : Elmt_Id;
15482 Subp : Entity_Id;
15483 Typ : Entity_Id;
15484
15485 begin
15486 pragma Assert (Ada_Version >= Ada_2005
15487 and then Is_Record_Type (Tagged_Type)
15488 and then Is_Tagged_Type (Tagged_Type)
15489 and then Has_Interfaces (Tagged_Type));
15490
15491 -- Step 1: Transfer to the full-view primitives associated with the
15492 -- partial-view that cover interface primitives. Conceptually this
15493 -- work should be done later by Process_Full_View; done here to
15494 -- simplify its implementation at later stages. It can be safely
15495 -- done here because interfaces must be visible in the partial and
15496 -- private view (RM 7.3(7.3/2)).
15497
15498 -- Small optimization: This work is only required if the parent may
15499 -- have entities whose Alias attribute reference an interface primitive.
15500 -- Such a situation may occur if the parent is an abstract type and the
15501 -- primitive has not been yet overridden or if the parent is a generic
15502 -- formal type covering interfaces.
15503
15504 -- If the tagged type is not abstract, it cannot have abstract
15505 -- primitives (the only entities in the list of primitives of
15506 -- non-abstract tagged types that can reference abstract primitives
15507 -- through its Alias attribute are the internal entities that have
15508 -- attribute Interface_Alias, and these entities are generated later
15509 -- by Add_Internal_Interface_Entities).
15510
15511 if In_Private_Part (Current_Scope)
15512 and then (Is_Abstract_Type (Parent_Type)
15513 or else
15514 Is_Generic_Type (Parent_Type))
15515 then
15516 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15517 while Present (Elmt) loop
15518 Subp := Node (Elmt);
15519
15520 -- At this stage it is not possible to have entities in the list
15521 -- of primitives that have attribute Interface_Alias.
15522
15523 pragma Assert (No (Interface_Alias (Subp)));
15524
15525 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15526
15527 if Is_Interface (Typ) then
15528 E := Find_Primitive_Covering_Interface
15529 (Tagged_Type => Tagged_Type,
15530 Iface_Prim => Subp);
15531
15532 if Present (E)
15533 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15534 then
15535 Replace_Elmt (Elmt, E);
15536 Remove_Homonym (Subp);
15537 end if;
15538 end if;
15539
15540 Next_Elmt (Elmt);
15541 end loop;
15542 end if;
15543
15544 -- Step 2: Add primitives of progenitors that are not implemented by
15545 -- parents of Tagged_Type.
15546
15547 if Present (Interfaces (Base_Type (Tagged_Type))) then
15548 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15549 while Present (Iface_Elmt) loop
15550 Iface := Node (Iface_Elmt);
15551
15552 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15553 while Present (Prim_Elmt) loop
15554 Iface_Subp := Node (Prim_Elmt);
15555 Iface_Alias := Ultimate_Alias (Iface_Subp);
15556
15557 -- Exclude derivation of predefined primitives except those
15558 -- that come from source, or are inherited from one that comes
15559 -- from source. Required to catch declarations of equality
15560 -- operators of interfaces. For example:
15561
15562 -- type Iface is interface;
15563 -- function "=" (Left, Right : Iface) return Boolean;
15564
15565 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15566 or else Comes_From_Source (Iface_Alias)
15567 then
15568 E :=
15569 Find_Primitive_Covering_Interface
15570 (Tagged_Type => Tagged_Type,
15571 Iface_Prim => Iface_Subp);
15572
15573 -- If not found we derive a new primitive leaving its alias
15574 -- attribute referencing the interface primitive.
15575
15576 if No (E) then
15577 Derive_Subprogram
15578 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15579
15580 -- Ada 2012 (AI05-0197): If the covering primitive's name
15581 -- differs from the name of the interface primitive then it
15582 -- is a private primitive inherited from a parent type. In
15583 -- such case, given that Tagged_Type covers the interface,
15584 -- the inherited private primitive becomes visible. For such
15585 -- purpose we add a new entity that renames the inherited
15586 -- private primitive.
15587
15588 elsif Chars (E) /= Chars (Iface_Subp) then
15589 pragma Assert (Has_Suffix (E, 'P'));
15590 Derive_Subprogram
15591 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15592 Set_Alias (New_Subp, E);
15593 Set_Is_Abstract_Subprogram (New_Subp,
15594 Is_Abstract_Subprogram (E));
15595
15596 -- Propagate to the full view interface entities associated
15597 -- with the partial view.
15598
15599 elsif In_Private_Part (Current_Scope)
15600 and then Present (Alias (E))
15601 and then Alias (E) = Iface_Subp
15602 and then
15603 List_Containing (Parent (E)) /=
15604 Private_Declarations
15605 (Specification
15606 (Unit_Declaration_Node (Current_Scope)))
15607 then
15608 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15609 end if;
15610 end if;
15611
15612 Next_Elmt (Prim_Elmt);
15613 end loop;
15614
15615 Next_Elmt (Iface_Elmt);
15616 end loop;
15617 end if;
15618 end Derive_Progenitor_Subprograms;
15619
15620 -----------------------
15621 -- Derive_Subprogram --
15622 -----------------------
15623
15624 procedure Derive_Subprogram
15625 (New_Subp : out Entity_Id;
15626 Parent_Subp : Entity_Id;
15627 Derived_Type : Entity_Id;
15628 Parent_Type : Entity_Id;
15629 Actual_Subp : Entity_Id := Empty)
15630 is
15631 Formal : Entity_Id;
15632 -- Formal parameter of parent primitive operation
15633
15634 Formal_Of_Actual : Entity_Id;
15635 -- Formal parameter of actual operation, when the derivation is to
15636 -- create a renaming for a primitive operation of an actual in an
15637 -- instantiation.
15638
15639 New_Formal : Entity_Id;
15640 -- Formal of inherited operation
15641
15642 Visible_Subp : Entity_Id := Parent_Subp;
15643
15644 function Is_Private_Overriding return Boolean;
15645 -- If Subp is a private overriding of a visible operation, the inherited
15646 -- operation derives from the overridden op (even though its body is the
15647 -- overriding one) and the inherited operation is visible now. See
15648 -- sem_disp to see the full details of the handling of the overridden
15649 -- subprogram, which is removed from the list of primitive operations of
15650 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15651 -- and used to diagnose abstract operations that need overriding in the
15652 -- derived type.
15653
15654 procedure Replace_Type (Id, New_Id : Entity_Id);
15655 -- When the type is an anonymous access type, create a new access type
15656 -- designating the derived type.
15657
15658 procedure Set_Derived_Name;
15659 -- This procedure sets the appropriate Chars name for New_Subp. This
15660 -- is normally just a copy of the parent name. An exception arises for
15661 -- type support subprograms, where the name is changed to reflect the
15662 -- name of the derived type, e.g. if type foo is derived from type bar,
15663 -- then a procedure barDA is derived with a name fooDA.
15664
15665 ---------------------------
15666 -- Is_Private_Overriding --
15667 ---------------------------
15668
15669 function Is_Private_Overriding return Boolean is
15670 Prev : Entity_Id;
15671
15672 begin
15673 -- If the parent is not a dispatching operation there is no
15674 -- need to investigate overridings
15675
15676 if not Is_Dispatching_Operation (Parent_Subp) then
15677 return False;
15678 end if;
15679
15680 -- The visible operation that is overridden is a homonym of the
15681 -- parent subprogram. We scan the homonym chain to find the one
15682 -- whose alias is the subprogram we are deriving.
15683
15684 Prev := Current_Entity (Parent_Subp);
15685 while Present (Prev) loop
15686 if Ekind (Prev) = Ekind (Parent_Subp)
15687 and then Alias (Prev) = Parent_Subp
15688 and then Scope (Parent_Subp) = Scope (Prev)
15689 and then not Is_Hidden (Prev)
15690 then
15691 Visible_Subp := Prev;
15692 return True;
15693 end if;
15694
15695 Prev := Homonym (Prev);
15696 end loop;
15697
15698 return False;
15699 end Is_Private_Overriding;
15700
15701 ------------------
15702 -- Replace_Type --
15703 ------------------
15704
15705 procedure Replace_Type (Id, New_Id : Entity_Id) is
15706 Id_Type : constant Entity_Id := Etype (Id);
15707 Acc_Type : Entity_Id;
15708 Par : constant Node_Id := Parent (Derived_Type);
15709
15710 begin
15711 -- When the type is an anonymous access type, create a new access
15712 -- type designating the derived type. This itype must be elaborated
15713 -- at the point of the derivation, not on subsequent calls that may
15714 -- be out of the proper scope for Gigi, so we insert a reference to
15715 -- it after the derivation.
15716
15717 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15718 declare
15719 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15720
15721 begin
15722 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15723 and then Present (Full_View (Desig_Typ))
15724 and then not Is_Private_Type (Parent_Type)
15725 then
15726 Desig_Typ := Full_View (Desig_Typ);
15727 end if;
15728
15729 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15730
15731 -- Ada 2005 (AI-251): Handle also derivations of abstract
15732 -- interface primitives.
15733
15734 or else (Is_Interface (Desig_Typ)
15735 and then not Is_Class_Wide_Type (Desig_Typ))
15736 then
15737 Acc_Type := New_Copy (Id_Type);
15738 Set_Etype (Acc_Type, Acc_Type);
15739 Set_Scope (Acc_Type, New_Subp);
15740
15741 -- Set size of anonymous access type. If we have an access
15742 -- to an unconstrained array, this is a fat pointer, so it
15743 -- is sizes at twice addtress size.
15744
15745 if Is_Array_Type (Desig_Typ)
15746 and then not Is_Constrained (Desig_Typ)
15747 then
15748 Init_Size (Acc_Type, 2 * System_Address_Size);
15749
15750 -- Other cases use a thin pointer
15751
15752 else
15753 Init_Size (Acc_Type, System_Address_Size);
15754 end if;
15755
15756 -- Set remaining characterstics of anonymous access type
15757
15758 Reinit_Alignment (Acc_Type);
15759 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15760
15761 Set_Etype (New_Id, Acc_Type);
15762 Set_Scope (New_Id, New_Subp);
15763
15764 -- Create a reference to it
15765
15766 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15767
15768 else
15769 Set_Etype (New_Id, Id_Type);
15770 end if;
15771 end;
15772
15773 -- In Ada2012, a formal may have an incomplete type but the type
15774 -- derivation that inherits the primitive follows the full view.
15775
15776 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15777 or else
15778 (Ekind (Id_Type) = E_Record_Type_With_Private
15779 and then Present (Full_View (Id_Type))
15780 and then
15781 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15782 or else
15783 (Ada_Version >= Ada_2012
15784 and then Ekind (Id_Type) = E_Incomplete_Type
15785 and then Full_View (Id_Type) = Parent_Type)
15786 then
15787 -- Constraint checks on formals are generated during expansion,
15788 -- based on the signature of the original subprogram. The bounds
15789 -- of the derived type are not relevant, and thus we can use
15790 -- the base type for the formals. However, the return type may be
15791 -- used in a context that requires that the proper static bounds
15792 -- be used (a case statement, for example) and for those cases
15793 -- we must use the derived type (first subtype), not its base.
15794
15795 -- If the derived_type_definition has no constraints, we know that
15796 -- the derived type has the same constraints as the first subtype
15797 -- of the parent, and we can also use it rather than its base,
15798 -- which can lead to more efficient code.
15799
15800 if Etype (Id) = Parent_Type then
15801 if Is_Scalar_Type (Parent_Type)
15802 and then
15803 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15804 then
15805 Set_Etype (New_Id, Derived_Type);
15806
15807 elsif Nkind (Par) = N_Full_Type_Declaration
15808 and then
15809 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15810 and then
15811 Is_Entity_Name
15812 (Subtype_Indication (Type_Definition (Par)))
15813 then
15814 Set_Etype (New_Id, Derived_Type);
15815
15816 else
15817 Set_Etype (New_Id, Base_Type (Derived_Type));
15818 end if;
15819
15820 else
15821 Set_Etype (New_Id, Base_Type (Derived_Type));
15822 end if;
15823
15824 else
15825 Set_Etype (New_Id, Etype (Id));
15826 end if;
15827 end Replace_Type;
15828
15829 ----------------------
15830 -- Set_Derived_Name --
15831 ----------------------
15832
15833 procedure Set_Derived_Name is
15834 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15835 begin
15836 if Nm = TSS_Null then
15837 Set_Chars (New_Subp, Chars (Parent_Subp));
15838 else
15839 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15840 end if;
15841 end Set_Derived_Name;
15842
15843 -- Start of processing for Derive_Subprogram
15844
15845 begin
15846 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15847 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
15848
15849 -- Check whether the inherited subprogram is a private operation that
15850 -- should be inherited but not yet made visible. Such subprograms can
15851 -- become visible at a later point (e.g., the private part of a public
15852 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15853 -- following predicate is true, then this is not such a private
15854 -- operation and the subprogram simply inherits the name of the parent
15855 -- subprogram. Note the special check for the names of controlled
15856 -- operations, which are currently exempted from being inherited with
15857 -- a hidden name because they must be findable for generation of
15858 -- implicit run-time calls.
15859
15860 if not Is_Hidden (Parent_Subp)
15861 or else Is_Internal (Parent_Subp)
15862 or else Is_Private_Overriding
15863 or else Is_Internal_Name (Chars (Parent_Subp))
15864 or else (Is_Controlled (Parent_Type)
15865 and then Chars (Parent_Subp) in Name_Adjust
15866 | Name_Finalize
15867 | Name_Initialize)
15868 then
15869 Set_Derived_Name;
15870
15871 -- An inherited dispatching equality will be overridden by an internally
15872 -- generated one, or by an explicit one, so preserve its name and thus
15873 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15874 -- private operation it may become invisible if the full view has
15875 -- progenitors, and the dispatch table will be malformed.
15876 -- We check that the type is limited to handle the anomalous declaration
15877 -- of Limited_Controlled, which is derived from a non-limited type, and
15878 -- which is handled specially elsewhere as well.
15879
15880 elsif Chars (Parent_Subp) = Name_Op_Eq
15881 and then Is_Dispatching_Operation (Parent_Subp)
15882 and then Etype (Parent_Subp) = Standard_Boolean
15883 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15884 and then
15885 Etype (First_Formal (Parent_Subp)) =
15886 Etype (Next_Formal (First_Formal (Parent_Subp)))
15887 then
15888 Set_Derived_Name;
15889
15890 -- If parent is hidden, this can be a regular derivation if the
15891 -- parent is immediately visible in a non-instantiating context,
15892 -- or if we are in the private part of an instance. This test
15893 -- should still be refined ???
15894
15895 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15896 -- operation as a non-visible operation in cases where the parent
15897 -- subprogram might not be visible now, but was visible within the
15898 -- original generic, so it would be wrong to make the inherited
15899 -- subprogram non-visible now. (Not clear if this test is fully
15900 -- correct; are there any cases where we should declare the inherited
15901 -- operation as not visible to avoid it being overridden, e.g., when
15902 -- the parent type is a generic actual with private primitives ???)
15903
15904 -- (they should be treated the same as other private inherited
15905 -- subprograms, but it's not clear how to do this cleanly). ???
15906
15907 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15908 and then Is_Immediately_Visible (Parent_Subp)
15909 and then not In_Instance)
15910 or else In_Instance_Not_Visible
15911 then
15912 Set_Derived_Name;
15913
15914 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15915 -- overrides an interface primitive because interface primitives
15916 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15917
15918 elsif Ada_Version >= Ada_2005
15919 and then Is_Dispatching_Operation (Parent_Subp)
15920 and then Present (Covered_Interface_Op (Parent_Subp))
15921 then
15922 Set_Derived_Name;
15923
15924 -- Otherwise, the type is inheriting a private operation, so enter it
15925 -- with a special name so it can't be overridden. See also below, where
15926 -- we check for this case, and if so avoid setting Requires_Overriding.
15927
15928 else
15929 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15930 end if;
15931
15932 Set_Parent (New_Subp, Parent (Derived_Type));
15933
15934 if Present (Actual_Subp) then
15935 Replace_Type (Actual_Subp, New_Subp);
15936 else
15937 Replace_Type (Parent_Subp, New_Subp);
15938 end if;
15939
15940 Conditional_Delay (New_Subp, Parent_Subp);
15941
15942 -- If we are creating a renaming for a primitive operation of an
15943 -- actual of a generic derived type, we must examine the signature
15944 -- of the actual primitive, not that of the generic formal, which for
15945 -- example may be an interface. However the name and initial value
15946 -- of the inherited operation are those of the formal primitive.
15947
15948 Formal := First_Formal (Parent_Subp);
15949
15950 if Present (Actual_Subp) then
15951 Formal_Of_Actual := First_Formal (Actual_Subp);
15952 else
15953 Formal_Of_Actual := Empty;
15954 end if;
15955
15956 while Present (Formal) loop
15957 New_Formal := New_Copy (Formal);
15958
15959 -- Extra formals are not inherited from a limited interface parent
15960 -- since limitedness is not inherited in such case (AI-419) and this
15961 -- affects the extra formals.
15962
15963 if Is_Limited_Interface (Parent_Type) then
15964 Set_Extra_Formal (New_Formal, Empty);
15965 Set_Extra_Accessibility (New_Formal, Empty);
15966 end if;
15967
15968 -- Normally we do not go copying parents, but in the case of
15969 -- formals, we need to link up to the declaration (which is the
15970 -- parameter specification), and it is fine to link up to the
15971 -- original formal's parameter specification in this case.
15972
15973 Set_Parent (New_Formal, Parent (Formal));
15974 Append_Entity (New_Formal, New_Subp);
15975
15976 if Present (Formal_Of_Actual) then
15977 Replace_Type (Formal_Of_Actual, New_Formal);
15978 Next_Formal (Formal_Of_Actual);
15979 else
15980 Replace_Type (Formal, New_Formal);
15981 end if;
15982
15983 Next_Formal (Formal);
15984 end loop;
15985
15986 -- Extra formals are shared between the parent subprogram and the
15987 -- derived subprogram (implicit in the above copy of formals), unless
15988 -- the parent type is a limited interface type; hence we must inherit
15989 -- also the reference to the first extra formal. When the parent type is
15990 -- an interface the extra formals will be added when the subprogram is
15991 -- frozen (see Freeze.Freeze_Subprogram).
15992
15993 if not Is_Limited_Interface (Parent_Type) then
15994 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
15995
15996 if Ekind (New_Subp) = E_Function then
15997 Set_Extra_Accessibility_Of_Result (New_Subp,
15998 Extra_Accessibility_Of_Result (Parent_Subp));
15999 end if;
16000 end if;
16001
16002 -- If this derivation corresponds to a tagged generic actual, then
16003 -- primitive operations rename those of the actual. Otherwise the
16004 -- primitive operations rename those of the parent type, If the parent
16005 -- renames an intrinsic operator, so does the new subprogram. We except
16006 -- concatenation, which is always properly typed, and does not get
16007 -- expanded as other intrinsic operations.
16008
16009 if No (Actual_Subp) then
16010 if Is_Intrinsic_Subprogram (Parent_Subp) then
16011 Set_Is_Intrinsic_Subprogram (New_Subp);
16012
16013 if Present (Alias (Parent_Subp))
16014 and then Chars (Parent_Subp) /= Name_Op_Concat
16015 then
16016 Set_Alias (New_Subp, Alias (Parent_Subp));
16017 else
16018 Set_Alias (New_Subp, Parent_Subp);
16019 end if;
16020
16021 else
16022 Set_Alias (New_Subp, Parent_Subp);
16023 end if;
16024
16025 else
16026 Set_Alias (New_Subp, Actual_Subp);
16027 end if;
16028
16029 -- Derived subprograms of a tagged type must inherit the convention
16030 -- of the parent subprogram (a requirement of AI-117). Derived
16031 -- subprograms of untagged types simply get convention Ada by default.
16032
16033 -- If the derived type is a tagged generic formal type with unknown
16034 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16035
16036 -- However, if the type is derived from a generic formal, the further
16037 -- inherited subprogram has the convention of the non-generic ancestor.
16038 -- Otherwise there would be no way to override the operation.
16039 -- (This is subject to forthcoming ARG discussions).
16040
16041 if Is_Tagged_Type (Derived_Type) then
16042 if Is_Generic_Type (Derived_Type)
16043 and then Has_Unknown_Discriminants (Derived_Type)
16044 then
16045 Set_Convention (New_Subp, Convention_Intrinsic);
16046
16047 else
16048 if Is_Generic_Type (Parent_Type)
16049 and then Has_Unknown_Discriminants (Parent_Type)
16050 then
16051 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16052 else
16053 Set_Convention (New_Subp, Convention (Parent_Subp));
16054 end if;
16055 end if;
16056 end if;
16057
16058 -- Predefined controlled operations retain their name even if the parent
16059 -- is hidden (see above), but they are not primitive operations if the
16060 -- ancestor is not visible, for example if the parent is a private
16061 -- extension completed with a controlled extension. Note that a full
16062 -- type that is controlled can break privacy: the flag Is_Controlled is
16063 -- set on both views of the type.
16064
16065 if Is_Controlled (Parent_Type)
16066 and then Chars (Parent_Subp) in Name_Initialize
16067 | Name_Adjust
16068 | Name_Finalize
16069 and then Is_Hidden (Parent_Subp)
16070 and then not Is_Visibly_Controlled (Parent_Type)
16071 then
16072 Set_Is_Hidden (New_Subp);
16073 end if;
16074
16075 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16076 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16077
16078 if Ekind (Parent_Subp) = E_Procedure then
16079 Set_Is_Valued_Procedure
16080 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16081 else
16082 Set_Has_Controlling_Result
16083 (New_Subp, Has_Controlling_Result (Parent_Subp));
16084 end if;
16085
16086 -- No_Return must be inherited properly. If this is overridden in the
16087 -- case of a dispatching operation, then the check is made later in
16088 -- Check_Abstract_Overriding that the overriding operation is also
16089 -- No_Return (no such check is required for the nondispatching case).
16090
16091 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16092
16093 -- A derived function with a controlling result is abstract. If the
16094 -- Derived_Type is a nonabstract formal generic derived type, then
16095 -- inherited operations are not abstract: the required check is done at
16096 -- instantiation time. If the derivation is for a generic actual, the
16097 -- function is not abstract unless the actual is.
16098
16099 if Is_Generic_Type (Derived_Type)
16100 and then not Is_Abstract_Type (Derived_Type)
16101 then
16102 null;
16103
16104 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16105 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16106 -- that functions with controlling access results of record extensions
16107 -- with a null extension part require overriding (AI95-00391/06).
16108
16109 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16110 -- implementing the rule of RM 7.3.2(6.1/4).
16111
16112 -- A subprogram subject to pragma Extensions_Visible with value False
16113 -- requires overriding if the subprogram has at least one controlling
16114 -- OUT parameter (SPARK RM 6.1.7(6)).
16115
16116 elsif Ada_Version >= Ada_2005
16117 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16118 or else (Is_Tagged_Type (Derived_Type)
16119 and then Etype (New_Subp) = Derived_Type
16120 and then not Is_Null_Extension (Derived_Type))
16121 or else (Is_Tagged_Type (Derived_Type)
16122 and then Ekind (Etype (New_Subp)) =
16123 E_Anonymous_Access_Type
16124 and then Designated_Type (Etype (New_Subp)) =
16125 Derived_Type)
16126 or else (Comes_From_Source (Alias (New_Subp))
16127 and then Is_EVF_Procedure (Alias (New_Subp)))
16128
16129 -- AI12-0042: Set Requires_Overriding when a type extension
16130 -- inherits a private operation that is visible at the
16131 -- point of extension (Has_Private_Ancestor is False) from
16132 -- an ancestor that has Type_Invariant'Class, and when the
16133 -- type extension is in a visible part (the latter as
16134 -- clarified by AI12-0382).
16135
16136 or else
16137 (not Has_Private_Ancestor (Derived_Type)
16138 and then Has_Invariants (Parent_Type)
16139 and then
16140 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16141 and then
16142 Class_Present
16143 (Get_Pragma (Parent_Type, Pragma_Invariant))
16144 and then Is_Private_Primitive (Parent_Subp)
16145 and then In_Visible_Part (Scope (Derived_Type))))
16146
16147 and then No (Actual_Subp)
16148 then
16149 if not Is_Tagged_Type (Derived_Type)
16150 or else Is_Abstract_Type (Derived_Type)
16151 or else Is_Abstract_Subprogram (Alias (New_Subp))
16152 then
16153 Set_Is_Abstract_Subprogram (New_Subp);
16154
16155 -- If the Chars of the new subprogram is different from that of the
16156 -- parent's one, it means that we entered it with a special name so
16157 -- it can't be overridden (see above). In that case we had better not
16158 -- *require* it to be overridden. This is the case where the parent
16159 -- type inherited the operation privately, so there's no danger of
16160 -- dangling dispatching.
16161
16162 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16163 Set_Requires_Overriding (New_Subp);
16164 end if;
16165
16166 elsif Ada_Version < Ada_2005
16167 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16168 or else (Is_Tagged_Type (Derived_Type)
16169 and then Etype (New_Subp) = Derived_Type
16170 and then No (Actual_Subp)))
16171 then
16172 Set_Is_Abstract_Subprogram (New_Subp);
16173
16174 -- AI05-0097 : an inherited operation that dispatches on result is
16175 -- abstract if the derived type is abstract, even if the parent type
16176 -- is concrete and the derived type is a null extension.
16177
16178 elsif Has_Controlling_Result (Alias (New_Subp))
16179 and then Is_Abstract_Type (Etype (New_Subp))
16180 then
16181 Set_Is_Abstract_Subprogram (New_Subp);
16182
16183 -- Finally, if the parent type is abstract we must verify that all
16184 -- inherited operations are either non-abstract or overridden, or that
16185 -- the derived type itself is abstract (this check is performed at the
16186 -- end of a package declaration, in Check_Abstract_Overriding). A
16187 -- private overriding in the parent type will not be visible in the
16188 -- derivation if we are not in an inner package or in a child unit of
16189 -- the parent type, in which case the abstractness of the inherited
16190 -- operation is carried to the new subprogram.
16191
16192 elsif Is_Abstract_Type (Parent_Type)
16193 and then not In_Open_Scopes (Scope (Parent_Type))
16194 and then Is_Private_Overriding
16195 and then Is_Abstract_Subprogram (Visible_Subp)
16196 then
16197 if No (Actual_Subp) then
16198 Set_Alias (New_Subp, Visible_Subp);
16199 Set_Is_Abstract_Subprogram (New_Subp, True);
16200
16201 else
16202 -- If this is a derivation for an instance of a formal derived
16203 -- type, abstractness comes from the primitive operation of the
16204 -- actual, not from the operation inherited from the ancestor.
16205
16206 Set_Is_Abstract_Subprogram
16207 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16208 end if;
16209 end if;
16210
16211 New_Overloaded_Entity (New_Subp, Derived_Type);
16212
16213 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16214 -- preconditions and the derived type is abstract, the derived operation
16215 -- is abstract as well if parent subprogram is not abstract or null.
16216
16217 if Is_Abstract_Type (Derived_Type)
16218 and then Has_Non_Trivial_Precondition (Parent_Subp)
16219 and then Present (Interfaces (Derived_Type))
16220 then
16221
16222 -- Add useful attributes of subprogram before the freeze point,
16223 -- in case freezing is delayed or there are previous errors.
16224
16225 Set_Is_Dispatching_Operation (New_Subp);
16226
16227 declare
16228 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16229
16230 begin
16231 if Present (Iface_Prim)
16232 and then Has_Non_Trivial_Precondition (Iface_Prim)
16233 then
16234 Set_Is_Abstract_Subprogram (New_Subp);
16235 end if;
16236 end;
16237 end if;
16238
16239 -- Check for case of a derived subprogram for the instantiation of a
16240 -- formal derived tagged type, if so mark the subprogram as dispatching
16241 -- and inherit the dispatching attributes of the actual subprogram. The
16242 -- derived subprogram is effectively renaming of the actual subprogram,
16243 -- so it needs to have the same attributes as the actual.
16244
16245 if Present (Actual_Subp)
16246 and then Is_Dispatching_Operation (Actual_Subp)
16247 then
16248 Set_Is_Dispatching_Operation (New_Subp);
16249
16250 if Present (DTC_Entity (Actual_Subp)) then
16251 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16252 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16253 end if;
16254 end if;
16255
16256 -- Indicate that a derived subprogram does not require a body and that
16257 -- it does not require processing of default expressions.
16258
16259 Set_Has_Completion (New_Subp);
16260 Set_Default_Expressions_Processed (New_Subp);
16261
16262 if Ekind (New_Subp) = E_Function then
16263 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16264 end if;
16265
16266 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16267 -- primitive subprogram S of a type T, then the aspect is inherited
16268 -- by the corresponding primitive subprogram of each descendant of T.
16269
16270 if Is_Tagged_Type (Derived_Type)
16271 and then Is_Dispatching_Operation (New_Subp)
16272 and then Has_Yield_Aspect (Alias (New_Subp))
16273 then
16274 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16275 end if;
16276
16277 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16278 end Derive_Subprogram;
16279
16280 ------------------------
16281 -- Derive_Subprograms --
16282 ------------------------
16283
16284 procedure Derive_Subprograms
16285 (Parent_Type : Entity_Id;
16286 Derived_Type : Entity_Id;
16287 Generic_Actual : Entity_Id := Empty)
16288 is
16289 Op_List : constant Elist_Id :=
16290 Collect_Primitive_Operations (Parent_Type);
16291
16292 function Check_Derived_Type return Boolean;
16293 -- Check that all the entities derived from Parent_Type are found in
16294 -- the list of primitives of Derived_Type exactly in the same order.
16295
16296 procedure Derive_Interface_Subprogram
16297 (New_Subp : out Entity_Id;
16298 Subp : Entity_Id;
16299 Actual_Subp : Entity_Id);
16300 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16301 -- (which is an interface primitive). If Generic_Actual is present then
16302 -- Actual_Subp is the actual subprogram corresponding with the generic
16303 -- subprogram Subp.
16304
16305 ------------------------
16306 -- Check_Derived_Type --
16307 ------------------------
16308
16309 function Check_Derived_Type return Boolean is
16310 E : Entity_Id;
16311 Elmt : Elmt_Id;
16312 List : Elist_Id;
16313 New_Subp : Entity_Id;
16314 Op_Elmt : Elmt_Id;
16315 Subp : Entity_Id;
16316
16317 begin
16318 -- Traverse list of entities in the current scope searching for
16319 -- an incomplete type whose full-view is derived type.
16320
16321 E := First_Entity (Scope (Derived_Type));
16322 while Present (E) and then E /= Derived_Type loop
16323 if Ekind (E) = E_Incomplete_Type
16324 and then Present (Full_View (E))
16325 and then Full_View (E) = Derived_Type
16326 then
16327 -- Disable this test if Derived_Type completes an incomplete
16328 -- type because in such case more primitives can be added
16329 -- later to the list of primitives of Derived_Type by routine
16330 -- Process_Incomplete_Dependents
16331
16332 return True;
16333 end if;
16334
16335 Next_Entity (E);
16336 end loop;
16337
16338 List := Collect_Primitive_Operations (Derived_Type);
16339 Elmt := First_Elmt (List);
16340
16341 Op_Elmt := First_Elmt (Op_List);
16342 while Present (Op_Elmt) loop
16343 Subp := Node (Op_Elmt);
16344 New_Subp := Node (Elmt);
16345
16346 -- At this early stage Derived_Type has no entities with attribute
16347 -- Interface_Alias. In addition, such primitives are always
16348 -- located at the end of the list of primitives of Parent_Type.
16349 -- Therefore, if found we can safely stop processing pending
16350 -- entities.
16351
16352 exit when Present (Interface_Alias (Subp));
16353
16354 -- Handle hidden entities
16355
16356 if not Is_Predefined_Dispatching_Operation (Subp)
16357 and then Is_Hidden (Subp)
16358 then
16359 if Present (New_Subp)
16360 and then Primitive_Names_Match (Subp, New_Subp)
16361 then
16362 Next_Elmt (Elmt);
16363 end if;
16364
16365 else
16366 if not Present (New_Subp)
16367 or else Ekind (Subp) /= Ekind (New_Subp)
16368 or else not Primitive_Names_Match (Subp, New_Subp)
16369 then
16370 return False;
16371 end if;
16372
16373 Next_Elmt (Elmt);
16374 end if;
16375
16376 Next_Elmt (Op_Elmt);
16377 end loop;
16378
16379 return True;
16380 end Check_Derived_Type;
16381
16382 ---------------------------------
16383 -- Derive_Interface_Subprogram --
16384 ---------------------------------
16385
16386 procedure Derive_Interface_Subprogram
16387 (New_Subp : out Entity_Id;
16388 Subp : Entity_Id;
16389 Actual_Subp : Entity_Id)
16390 is
16391 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16392 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16393
16394 begin
16395 pragma Assert (Is_Interface (Iface_Type));
16396
16397 Derive_Subprogram
16398 (New_Subp => New_Subp,
16399 Parent_Subp => Iface_Subp,
16400 Derived_Type => Derived_Type,
16401 Parent_Type => Iface_Type,
16402 Actual_Subp => Actual_Subp);
16403
16404 -- Given that this new interface entity corresponds with a primitive
16405 -- of the parent that was not overridden we must leave it associated
16406 -- with its parent primitive to ensure that it will share the same
16407 -- dispatch table slot when overridden. We must set the Alias to Subp
16408 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16409 -- (in case we inherited Subp from Iface_Type via a nonabstract
16410 -- generic formal type).
16411
16412 if No (Actual_Subp) then
16413 Set_Alias (New_Subp, Subp);
16414
16415 declare
16416 T : Entity_Id := Find_Dispatching_Type (Subp);
16417 begin
16418 while Etype (T) /= T loop
16419 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16420 Set_Is_Abstract_Subprogram (New_Subp, False);
16421 exit;
16422 end if;
16423
16424 T := Etype (T);
16425 end loop;
16426 end;
16427
16428 -- For instantiations this is not needed since the previous call to
16429 -- Derive_Subprogram leaves the entity well decorated.
16430
16431 else
16432 pragma Assert (Alias (New_Subp) = Actual_Subp);
16433 null;
16434 end if;
16435 end Derive_Interface_Subprogram;
16436
16437 -- Local variables
16438
16439 Alias_Subp : Entity_Id;
16440 Act_List : Elist_Id;
16441 Act_Elmt : Elmt_Id;
16442 Act_Subp : Entity_Id := Empty;
16443 Elmt : Elmt_Id;
16444 Need_Search : Boolean := False;
16445 New_Subp : Entity_Id := Empty;
16446 Parent_Base : Entity_Id;
16447 Subp : Entity_Id;
16448
16449 -- Start of processing for Derive_Subprograms
16450
16451 begin
16452 if Ekind (Parent_Type) = E_Record_Type_With_Private
16453 and then Has_Discriminants (Parent_Type)
16454 and then Present (Full_View (Parent_Type))
16455 then
16456 Parent_Base := Full_View (Parent_Type);
16457 else
16458 Parent_Base := Parent_Type;
16459 end if;
16460
16461 if Present (Generic_Actual) then
16462 Act_List := Collect_Primitive_Operations (Generic_Actual);
16463 Act_Elmt := First_Elmt (Act_List);
16464 else
16465 Act_List := No_Elist;
16466 Act_Elmt := No_Elmt;
16467 end if;
16468
16469 -- Derive primitives inherited from the parent. Note that if the generic
16470 -- actual is present, this is not really a type derivation, it is a
16471 -- completion within an instance.
16472
16473 -- Case 1: Derived_Type does not implement interfaces
16474
16475 if not Is_Tagged_Type (Derived_Type)
16476 or else (not Has_Interfaces (Derived_Type)
16477 and then not (Present (Generic_Actual)
16478 and then Has_Interfaces (Generic_Actual)))
16479 then
16480 Elmt := First_Elmt (Op_List);
16481 while Present (Elmt) loop
16482 Subp := Node (Elmt);
16483
16484 -- Literals are derived earlier in the process of building the
16485 -- derived type, and are skipped here.
16486
16487 if Ekind (Subp) = E_Enumeration_Literal then
16488 null;
16489
16490 -- The actual is a direct descendant and the common primitive
16491 -- operations appear in the same order.
16492
16493 -- If the generic parent type is present, the derived type is an
16494 -- instance of a formal derived type, and within the instance its
16495 -- operations are those of the actual. We derive from the formal
16496 -- type but make the inherited operations aliases of the
16497 -- corresponding operations of the actual.
16498
16499 else
16500 pragma Assert (No (Node (Act_Elmt))
16501 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16502 and then
16503 Type_Conformant
16504 (Subp, Node (Act_Elmt),
16505 Skip_Controlling_Formals => True)));
16506
16507 Derive_Subprogram
16508 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16509
16510 if Present (Act_Elmt) then
16511 Next_Elmt (Act_Elmt);
16512 end if;
16513 end if;
16514
16515 Next_Elmt (Elmt);
16516 end loop;
16517
16518 -- Case 2: Derived_Type implements interfaces
16519
16520 else
16521 -- If the parent type has no predefined primitives we remove
16522 -- predefined primitives from the list of primitives of generic
16523 -- actual to simplify the complexity of this algorithm.
16524
16525 if Present (Generic_Actual) then
16526 declare
16527 Has_Predefined_Primitives : Boolean := False;
16528
16529 begin
16530 -- Check if the parent type has predefined primitives
16531
16532 Elmt := First_Elmt (Op_List);
16533 while Present (Elmt) loop
16534 Subp := Node (Elmt);
16535
16536 if Is_Predefined_Dispatching_Operation (Subp)
16537 and then not Comes_From_Source (Ultimate_Alias (Subp))
16538 then
16539 Has_Predefined_Primitives := True;
16540 exit;
16541 end if;
16542
16543 Next_Elmt (Elmt);
16544 end loop;
16545
16546 -- Remove predefined primitives of Generic_Actual. We must use
16547 -- an auxiliary list because in case of tagged types the value
16548 -- returned by Collect_Primitive_Operations is the value stored
16549 -- in its Primitive_Operations attribute (and we don't want to
16550 -- modify its current contents).
16551
16552 if not Has_Predefined_Primitives then
16553 declare
16554 Aux_List : constant Elist_Id := New_Elmt_List;
16555
16556 begin
16557 Elmt := First_Elmt (Act_List);
16558 while Present (Elmt) loop
16559 Subp := Node (Elmt);
16560
16561 if not Is_Predefined_Dispatching_Operation (Subp)
16562 or else Comes_From_Source (Subp)
16563 then
16564 Append_Elmt (Subp, Aux_List);
16565 end if;
16566
16567 Next_Elmt (Elmt);
16568 end loop;
16569
16570 Act_List := Aux_List;
16571 end;
16572 end if;
16573
16574 Act_Elmt := First_Elmt (Act_List);
16575 Act_Subp := Node (Act_Elmt);
16576 end;
16577 end if;
16578
16579 -- Stage 1: If the generic actual is not present we derive the
16580 -- primitives inherited from the parent type. If the generic parent
16581 -- type is present, the derived type is an instance of a formal
16582 -- derived type, and within the instance its operations are those of
16583 -- the actual. We derive from the formal type but make the inherited
16584 -- operations aliases of the corresponding operations of the actual.
16585
16586 Elmt := First_Elmt (Op_List);
16587 while Present (Elmt) loop
16588 Subp := Node (Elmt);
16589 Alias_Subp := Ultimate_Alias (Subp);
16590
16591 -- Do not derive internal entities of the parent that link
16592 -- interface primitives with their covering primitive. These
16593 -- entities will be added to this type when frozen.
16594
16595 if Present (Interface_Alias (Subp)) then
16596 goto Continue;
16597 end if;
16598
16599 -- If the generic actual is present find the corresponding
16600 -- operation in the generic actual. If the parent type is a
16601 -- direct ancestor of the derived type then, even if it is an
16602 -- interface, the operations are inherited from the primary
16603 -- dispatch table and are in the proper order. If we detect here
16604 -- that primitives are not in the same order we traverse the list
16605 -- of primitive operations of the actual to find the one that
16606 -- implements the interface primitive.
16607
16608 if Need_Search
16609 or else
16610 (Present (Generic_Actual)
16611 and then Present (Act_Subp)
16612 and then not
16613 (Primitive_Names_Match (Subp, Act_Subp)
16614 and then
16615 Type_Conformant (Subp, Act_Subp,
16616 Skip_Controlling_Formals => True)))
16617 then
16618 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16619 Use_Full_View => True));
16620
16621 -- Remember that we need searching for all pending primitives
16622
16623 Need_Search := True;
16624
16625 -- Handle entities associated with interface primitives
16626
16627 if Present (Alias_Subp)
16628 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16629 and then not Is_Predefined_Dispatching_Operation (Subp)
16630 then
16631 -- Search for the primitive in the homonym chain
16632
16633 Act_Subp :=
16634 Find_Primitive_Covering_Interface
16635 (Tagged_Type => Generic_Actual,
16636 Iface_Prim => Alias_Subp);
16637
16638 -- Previous search may not locate primitives covering
16639 -- interfaces defined in generics units or instantiations.
16640 -- (it fails if the covering primitive has formals whose
16641 -- type is also defined in generics or instantiations).
16642 -- In such case we search in the list of primitives of the
16643 -- generic actual for the internal entity that links the
16644 -- interface primitive and the covering primitive.
16645
16646 if No (Act_Subp)
16647 and then Is_Generic_Type (Parent_Type)
16648 then
16649 -- This code has been designed to handle only generic
16650 -- formals that implement interfaces that are defined
16651 -- in a generic unit or instantiation. If this code is
16652 -- needed for other cases we must review it because
16653 -- (given that it relies on Original_Location to locate
16654 -- the primitive of Generic_Actual that covers the
16655 -- interface) it could leave linked through attribute
16656 -- Alias entities of unrelated instantiations).
16657
16658 pragma Assert
16659 (Is_Generic_Unit
16660 (Scope (Find_Dispatching_Type (Alias_Subp)))
16661 or else
16662 Instantiation_Depth
16663 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16664
16665 declare
16666 Iface_Prim_Loc : constant Source_Ptr :=
16667 Original_Location (Sloc (Alias_Subp));
16668
16669 Elmt : Elmt_Id;
16670 Prim : Entity_Id;
16671
16672 begin
16673 Elmt :=
16674 First_Elmt (Primitive_Operations (Generic_Actual));
16675
16676 Search : while Present (Elmt) loop
16677 Prim := Node (Elmt);
16678
16679 if Present (Interface_Alias (Prim))
16680 and then Original_Location
16681 (Sloc (Interface_Alias (Prim))) =
16682 Iface_Prim_Loc
16683 then
16684 Act_Subp := Alias (Prim);
16685 exit Search;
16686 end if;
16687
16688 Next_Elmt (Elmt);
16689 end loop Search;
16690 end;
16691 end if;
16692
16693 pragma Assert (Present (Act_Subp)
16694 or else Is_Abstract_Type (Generic_Actual)
16695 or else Serious_Errors_Detected > 0);
16696
16697 -- Handle predefined primitives plus the rest of user-defined
16698 -- primitives
16699
16700 else
16701 Act_Elmt := First_Elmt (Act_List);
16702 while Present (Act_Elmt) loop
16703 Act_Subp := Node (Act_Elmt);
16704
16705 exit when Primitive_Names_Match (Subp, Act_Subp)
16706 and then Type_Conformant
16707 (Subp, Act_Subp,
16708 Skip_Controlling_Formals => True)
16709 and then No (Interface_Alias (Act_Subp));
16710
16711 Next_Elmt (Act_Elmt);
16712 end loop;
16713
16714 if No (Act_Elmt) then
16715 Act_Subp := Empty;
16716 end if;
16717 end if;
16718 end if;
16719
16720 -- Case 1: If the parent is a limited interface then it has the
16721 -- predefined primitives of synchronized interfaces. However, the
16722 -- actual type may be a non-limited type and hence it does not
16723 -- have such primitives.
16724
16725 if Present (Generic_Actual)
16726 and then not Present (Act_Subp)
16727 and then Is_Limited_Interface (Parent_Base)
16728 and then Is_Predefined_Interface_Primitive (Subp)
16729 then
16730 null;
16731
16732 -- Case 2: Inherit entities associated with interfaces that were
16733 -- not covered by the parent type. We exclude here null interface
16734 -- primitives because they do not need special management.
16735
16736 -- We also exclude interface operations that are renamings. If the
16737 -- subprogram is an explicit renaming of an interface primitive,
16738 -- it is a regular primitive operation, and the presence of its
16739 -- alias is not relevant: it has to be derived like any other
16740 -- primitive.
16741
16742 elsif Present (Alias (Subp))
16743 and then Nkind (Unit_Declaration_Node (Subp)) /=
16744 N_Subprogram_Renaming_Declaration
16745 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16746 and then not
16747 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16748 and then Null_Present (Parent (Alias_Subp)))
16749 then
16750 -- If this is an abstract private type then we transfer the
16751 -- derivation of the interface primitive from the partial view
16752 -- to the full view. This is safe because all the interfaces
16753 -- must be visible in the partial view. Done to avoid adding
16754 -- a new interface derivation to the private part of the
16755 -- enclosing package; otherwise this new derivation would be
16756 -- decorated as hidden when the analysis of the enclosing
16757 -- package completes.
16758
16759 if Is_Abstract_Type (Derived_Type)
16760 and then In_Private_Part (Current_Scope)
16761 and then Has_Private_Declaration (Derived_Type)
16762 then
16763 declare
16764 Partial_View : Entity_Id;
16765 Elmt : Elmt_Id;
16766 Ent : Entity_Id;
16767
16768 begin
16769 Partial_View := First_Entity (Current_Scope);
16770 loop
16771 exit when No (Partial_View)
16772 or else (Has_Private_Declaration (Partial_View)
16773 and then
16774 Full_View (Partial_View) = Derived_Type);
16775
16776 Next_Entity (Partial_View);
16777 end loop;
16778
16779 -- If the partial view was not found then the source code
16780 -- has errors and the derivation is not needed.
16781
16782 if Present (Partial_View) then
16783 Elmt :=
16784 First_Elmt (Primitive_Operations (Partial_View));
16785 while Present (Elmt) loop
16786 Ent := Node (Elmt);
16787
16788 if Present (Alias (Ent))
16789 and then Ultimate_Alias (Ent) = Alias (Subp)
16790 then
16791 Append_Elmt
16792 (Ent, Primitive_Operations (Derived_Type));
16793 exit;
16794 end if;
16795
16796 Next_Elmt (Elmt);
16797 end loop;
16798
16799 -- If the interface primitive was not found in the
16800 -- partial view then this interface primitive was
16801 -- overridden. We add a derivation to activate in
16802 -- Derive_Progenitor_Subprograms the machinery to
16803 -- search for it.
16804
16805 if No (Elmt) then
16806 Derive_Interface_Subprogram
16807 (New_Subp => New_Subp,
16808 Subp => Subp,
16809 Actual_Subp => Act_Subp);
16810 end if;
16811 end if;
16812 end;
16813 else
16814 Derive_Interface_Subprogram
16815 (New_Subp => New_Subp,
16816 Subp => Subp,
16817 Actual_Subp => Act_Subp);
16818 end if;
16819
16820 -- Case 3: Common derivation
16821
16822 else
16823 Derive_Subprogram
16824 (New_Subp => New_Subp,
16825 Parent_Subp => Subp,
16826 Derived_Type => Derived_Type,
16827 Parent_Type => Parent_Base,
16828 Actual_Subp => Act_Subp);
16829 end if;
16830
16831 -- No need to update Act_Elm if we must search for the
16832 -- corresponding operation in the generic actual
16833
16834 if not Need_Search
16835 and then Present (Act_Elmt)
16836 then
16837 Next_Elmt (Act_Elmt);
16838 Act_Subp := Node (Act_Elmt);
16839 end if;
16840
16841 <<Continue>>
16842 Next_Elmt (Elmt);
16843 end loop;
16844
16845 -- Inherit additional operations from progenitors. If the derived
16846 -- type is a generic actual, there are not new primitive operations
16847 -- for the type because it has those of the actual, and therefore
16848 -- nothing needs to be done. The renamings generated above are not
16849 -- primitive operations, and their purpose is simply to make the
16850 -- proper operations visible within an instantiation.
16851
16852 if No (Generic_Actual) then
16853 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16854 end if;
16855 end if;
16856
16857 -- Final check: Direct descendants must have their primitives in the
16858 -- same order. We exclude from this test untagged types and instances
16859 -- of formal derived types. We skip this test if we have already
16860 -- reported serious errors in the sources.
16861
16862 pragma Assert (not Is_Tagged_Type (Derived_Type)
16863 or else Present (Generic_Actual)
16864 or else Serious_Errors_Detected > 0
16865 or else Check_Derived_Type);
16866 end Derive_Subprograms;
16867
16868 --------------------------------
16869 -- Derived_Standard_Character --
16870 --------------------------------
16871
16872 procedure Derived_Standard_Character
16873 (N : Node_Id;
16874 Parent_Type : Entity_Id;
16875 Derived_Type : Entity_Id)
16876 is
16877 Loc : constant Source_Ptr := Sloc (N);
16878 Def : constant Node_Id := Type_Definition (N);
16879 Indic : constant Node_Id := Subtype_Indication (Def);
16880 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16881 Implicit_Base : constant Entity_Id :=
16882 Create_Itype
16883 (E_Enumeration_Type, N, Derived_Type, 'B');
16884
16885 Lo : Node_Id;
16886 Hi : Node_Id;
16887
16888 begin
16889 Discard_Node (Process_Subtype (Indic, N));
16890
16891 Set_Etype (Implicit_Base, Parent_Base);
16892 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16893 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16894
16895 Set_Is_Character_Type (Implicit_Base, True);
16896 Set_Has_Delayed_Freeze (Implicit_Base);
16897
16898 -- The bounds of the implicit base are the bounds of the parent base.
16899 -- Note that their type is the parent base.
16900
16901 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16902 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16903
16904 Set_Scalar_Range (Implicit_Base,
16905 Make_Range (Loc,
16906 Low_Bound => Lo,
16907 High_Bound => Hi));
16908
16909 Conditional_Delay (Derived_Type, Parent_Type);
16910
16911 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
16912 Set_Etype (Derived_Type, Implicit_Base);
16913 Set_Size_Info (Derived_Type, Parent_Type);
16914
16915 if not Known_RM_Size (Derived_Type) then
16916 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16917 end if;
16918
16919 Set_Is_Character_Type (Derived_Type, True);
16920
16921 if Nkind (Indic) /= N_Subtype_Indication then
16922
16923 -- If no explicit constraint, the bounds are those
16924 -- of the parent type.
16925
16926 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16927 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16928 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16929 end if;
16930
16931 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16932
16933 -- Because the implicit base is used in the conversion of the bounds, we
16934 -- have to freeze it now. This is similar to what is done for numeric
16935 -- types, and it equally suspicious, but otherwise a nonstatic bound
16936 -- will have a reference to an unfrozen type, which is rejected by Gigi
16937 -- (???). This requires specific care for definition of stream
16938 -- attributes. For details, see comments at the end of
16939 -- Build_Derived_Numeric_Type.
16940
16941 Freeze_Before (N, Implicit_Base);
16942 end Derived_Standard_Character;
16943
16944 ------------------------------
16945 -- Derived_Type_Declaration --
16946 ------------------------------
16947
16948 procedure Derived_Type_Declaration
16949 (T : Entity_Id;
16950 N : Node_Id;
16951 Is_Completion : Boolean)
16952 is
16953 Parent_Type : Entity_Id;
16954
16955 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16956 -- Check whether the parent type is a generic formal, or derives
16957 -- directly or indirectly from one.
16958
16959 ------------------------
16960 -- Comes_From_Generic --
16961 ------------------------
16962
16963 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16964 begin
16965 if Is_Generic_Type (Typ) then
16966 return True;
16967
16968 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16969 return True;
16970
16971 elsif Is_Private_Type (Typ)
16972 and then Present (Full_View (Typ))
16973 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16974 then
16975 return True;
16976
16977 elsif Is_Generic_Actual_Type (Typ) then
16978 return True;
16979
16980 else
16981 return False;
16982 end if;
16983 end Comes_From_Generic;
16984
16985 -- Local variables
16986
16987 Def : constant Node_Id := Type_Definition (N);
16988 Iface_Def : Node_Id;
16989 Indic : constant Node_Id := Subtype_Indication (Def);
16990 Extension : constant Node_Id := Record_Extension_Part (Def);
16991 Parent_Node : Node_Id;
16992 Taggd : Boolean;
16993
16994 -- Start of processing for Derived_Type_Declaration
16995
16996 begin
16997 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16998
16999 if SPARK_Mode = On
17000 and then Is_Tagged_Type (Parent_Type)
17001 then
17002 declare
17003 Partial_View : constant Entity_Id :=
17004 Incomplete_Or_Partial_View (Parent_Type);
17005
17006 begin
17007 -- If the partial view was not found then the parent type is not
17008 -- a private type. Otherwise check if the partial view is a tagged
17009 -- private type.
17010
17011 if Present (Partial_View)
17012 and then Is_Private_Type (Partial_View)
17013 and then not Is_Tagged_Type (Partial_View)
17014 then
17015 Error_Msg_NE
17016 ("cannot derive from & declared as untagged private "
17017 & "(SPARK RM 3.4(1))", N, Partial_View);
17018 end if;
17019 end;
17020 end if;
17021
17022 -- Ada 2005 (AI-251): In case of interface derivation check that the
17023 -- parent is also an interface.
17024
17025 if Interface_Present (Def) then
17026 if not Is_Interface (Parent_Type) then
17027 Diagnose_Interface (Indic, Parent_Type);
17028
17029 else
17030 Parent_Node := Parent (Base_Type (Parent_Type));
17031 Iface_Def := Type_Definition (Parent_Node);
17032
17033 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17034 -- other limited interfaces.
17035
17036 if Limited_Present (Def) then
17037 if Limited_Present (Iface_Def) then
17038 null;
17039
17040 elsif Protected_Present (Iface_Def) then
17041 Error_Msg_NE
17042 ("descendant of & must be declared as a protected "
17043 & "interface", N, Parent_Type);
17044
17045 elsif Synchronized_Present (Iface_Def) then
17046 Error_Msg_NE
17047 ("descendant of & must be declared as a synchronized "
17048 & "interface", N, Parent_Type);
17049
17050 elsif Task_Present (Iface_Def) then
17051 Error_Msg_NE
17052 ("descendant of & must be declared as a task interface",
17053 N, Parent_Type);
17054
17055 else
17056 Error_Msg_N
17057 ("(Ada 2005) limited interface cannot inherit from "
17058 & "non-limited interface", Indic);
17059 end if;
17060
17061 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17062 -- from non-limited or limited interfaces.
17063
17064 elsif not Protected_Present (Def)
17065 and then not Synchronized_Present (Def)
17066 and then not Task_Present (Def)
17067 then
17068 if Limited_Present (Iface_Def) then
17069 null;
17070
17071 elsif Protected_Present (Iface_Def) then
17072 Error_Msg_NE
17073 ("descendant of & must be declared as a protected "
17074 & "interface", N, Parent_Type);
17075
17076 elsif Synchronized_Present (Iface_Def) then
17077 Error_Msg_NE
17078 ("descendant of & must be declared as a synchronized "
17079 & "interface", N, Parent_Type);
17080
17081 elsif Task_Present (Iface_Def) then
17082 Error_Msg_NE
17083 ("descendant of & must be declared as a task interface",
17084 N, Parent_Type);
17085 else
17086 null;
17087 end if;
17088 end if;
17089 end if;
17090 end if;
17091
17092 if Is_Tagged_Type (Parent_Type)
17093 and then Is_Concurrent_Type (Parent_Type)
17094 and then not Is_Interface (Parent_Type)
17095 then
17096 Error_Msg_N
17097 ("parent type of a record extension cannot be a synchronized "
17098 & "tagged type (RM 3.9.1 (3/1))", N);
17099 Set_Etype (T, Any_Type);
17100 return;
17101 end if;
17102
17103 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17104 -- interfaces
17105
17106 if Is_Tagged_Type (Parent_Type)
17107 and then Is_Non_Empty_List (Interface_List (Def))
17108 then
17109 declare
17110 Intf : Node_Id;
17111 T : Entity_Id;
17112
17113 begin
17114 Intf := First (Interface_List (Def));
17115 while Present (Intf) loop
17116 T := Find_Type_Of_Subtype_Indic (Intf);
17117
17118 if not Is_Interface (T) then
17119 Diagnose_Interface (Intf, T);
17120
17121 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17122 -- a limited type from having a nonlimited progenitor.
17123
17124 elsif (Limited_Present (Def)
17125 or else (not Is_Interface (Parent_Type)
17126 and then Is_Limited_Type (Parent_Type)))
17127 and then not Is_Limited_Interface (T)
17128 then
17129 Error_Msg_NE
17130 ("progenitor interface& of limited type must be limited",
17131 N, T);
17132 end if;
17133
17134 Next (Intf);
17135 end loop;
17136 end;
17137
17138 -- Check consistency of any nonoverridable aspects that are
17139 -- inherited from multiple sources.
17140
17141 Check_Inherited_Nonoverridable_Aspects
17142 (Inheritor => T,
17143 Interface_List => Interface_List (Def),
17144 Parent_Type => Parent_Type);
17145 end if;
17146
17147 if Parent_Type = Any_Type
17148 or else Etype (Parent_Type) = Any_Type
17149 or else (Is_Class_Wide_Type (Parent_Type)
17150 and then Etype (Parent_Type) = T)
17151 then
17152 -- If Parent_Type is undefined or illegal, make new type into a
17153 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17154 -- errors. If this is a self-definition, emit error now.
17155
17156 if T = Parent_Type or else T = Etype (Parent_Type) then
17157 Error_Msg_N ("type cannot be used in its own definition", Indic);
17158 end if;
17159
17160 Mutate_Ekind (T, Ekind (Parent_Type));
17161 Set_Etype (T, Any_Type);
17162 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17163
17164 if Is_Tagged_Type (T)
17165 and then Is_Record_Type (T)
17166 then
17167 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17168 end if;
17169
17170 return;
17171 end if;
17172
17173 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17174 -- an interface is special because the list of interfaces in the full
17175 -- view can be given in any order. For example:
17176
17177 -- type A is interface;
17178 -- type B is interface and A;
17179 -- type D is new B with private;
17180 -- private
17181 -- type D is new A and B with null record; -- 1 --
17182
17183 -- In this case we perform the following transformation of -1-:
17184
17185 -- type D is new B and A with null record;
17186
17187 -- If the parent of the full-view covers the parent of the partial-view
17188 -- we have two possible cases:
17189
17190 -- 1) They have the same parent
17191 -- 2) The parent of the full-view implements some further interfaces
17192
17193 -- In both cases we do not need to perform the transformation. In the
17194 -- first case the source program is correct and the transformation is
17195 -- not needed; in the second case the source program does not fulfill
17196 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17197 -- later.
17198
17199 -- This transformation not only simplifies the rest of the analysis of
17200 -- this type declaration but also simplifies the correct generation of
17201 -- the object layout to the expander.
17202
17203 if In_Private_Part (Current_Scope)
17204 and then Is_Interface (Parent_Type)
17205 then
17206 declare
17207 Iface : Node_Id;
17208 Partial_View : Entity_Id;
17209 Partial_View_Parent : Entity_Id;
17210 New_Iface : Node_Id;
17211
17212 begin
17213 -- Look for the associated private type declaration
17214
17215 Partial_View := Incomplete_Or_Partial_View (T);
17216
17217 -- If the partial view was not found then the source code has
17218 -- errors and the transformation is not needed.
17219
17220 if Present (Partial_View) then
17221 Partial_View_Parent := Etype (Partial_View);
17222
17223 -- If the parent of the full-view covers the parent of the
17224 -- partial-view we have nothing else to do.
17225
17226 if Interface_Present_In_Ancestor
17227 (Parent_Type, Partial_View_Parent)
17228 then
17229 null;
17230
17231 -- Traverse the list of interfaces of the full-view to look
17232 -- for the parent of the partial-view and perform the tree
17233 -- transformation.
17234
17235 else
17236 Iface := First (Interface_List (Def));
17237 while Present (Iface) loop
17238 if Etype (Iface) = Etype (Partial_View) then
17239 Rewrite (Subtype_Indication (Def),
17240 New_Copy (Subtype_Indication
17241 (Parent (Partial_View))));
17242
17243 New_Iface :=
17244 Make_Identifier (Sloc (N), Chars (Parent_Type));
17245 Append (New_Iface, Interface_List (Def));
17246
17247 -- Analyze the transformed code
17248
17249 Derived_Type_Declaration (T, N, Is_Completion);
17250 return;
17251 end if;
17252
17253 Next (Iface);
17254 end loop;
17255 end if;
17256 end if;
17257 end;
17258 end if;
17259
17260 -- Only composite types other than array types are allowed to have
17261 -- discriminants.
17262
17263 if Present (Discriminant_Specifications (N)) then
17264 if (Is_Elementary_Type (Parent_Type)
17265 or else
17266 Is_Array_Type (Parent_Type))
17267 and then not Error_Posted (N)
17268 then
17269 Error_Msg_N
17270 ("elementary or array type cannot have discriminants",
17271 Defining_Identifier (First (Discriminant_Specifications (N))));
17272
17273 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17274 -- only if we are not already processing a malformed syntax tree.
17275
17276 if Is_Type (T) then
17277 Set_Has_Discriminants (T, False);
17278 end if;
17279 end if;
17280 end if;
17281
17282 -- In Ada 83, a derived type defined in a package specification cannot
17283 -- be used for further derivation until the end of its visible part.
17284 -- Note that derivation in the private part of the package is allowed.
17285
17286 if Ada_Version = Ada_83
17287 and then Is_Derived_Type (Parent_Type)
17288 and then In_Visible_Part (Scope (Parent_Type))
17289 then
17290 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17291 Error_Msg_N
17292 ("(Ada 83) premature use of type for derivation", Indic);
17293 end if;
17294 end if;
17295
17296 -- Check for early use of incomplete or private type
17297
17298 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17299 Error_Msg_N ("premature derivation of incomplete type", Indic);
17300 return;
17301
17302 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17303 and then not Comes_From_Generic (Parent_Type))
17304 or else Has_Private_Component (Parent_Type)
17305 then
17306 -- The ancestor type of a formal type can be incomplete, in which
17307 -- case only the operations of the partial view are available in the
17308 -- generic. Subsequent checks may be required when the full view is
17309 -- analyzed to verify that a derivation from a tagged type has an
17310 -- extension.
17311
17312 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17313 null;
17314
17315 elsif No (Underlying_Type (Parent_Type))
17316 or else Has_Private_Component (Parent_Type)
17317 then
17318 Error_Msg_N
17319 ("premature derivation of derived or private type", Indic);
17320
17321 -- Flag the type itself as being in error, this prevents some
17322 -- nasty problems with subsequent uses of the malformed type.
17323
17324 Set_Error_Posted (T);
17325
17326 -- Check that within the immediate scope of an untagged partial
17327 -- view it's illegal to derive from the partial view if the
17328 -- full view is tagged. (7.3(7))
17329
17330 -- We verify that the Parent_Type is a partial view by checking
17331 -- that it is not a Full_Type_Declaration (i.e. a private type or
17332 -- private extension declaration), to distinguish a partial view
17333 -- from a derivation from a private type which also appears as
17334 -- E_Private_Type. If the parent base type is not declared in an
17335 -- enclosing scope there is no need to check.
17336
17337 elsif Present (Full_View (Parent_Type))
17338 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17339 and then not Is_Tagged_Type (Parent_Type)
17340 and then Is_Tagged_Type (Full_View (Parent_Type))
17341 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17342 then
17343 Error_Msg_N
17344 ("premature derivation from type with tagged full view",
17345 Indic);
17346 end if;
17347 end if;
17348
17349 -- Check that form of derivation is appropriate
17350
17351 Taggd := Is_Tagged_Type (Parent_Type);
17352
17353 -- Set the parent type to the class-wide type's specific type in this
17354 -- case to prevent cascading errors
17355
17356 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17357 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17358 Set_Etype (T, Etype (Parent_Type));
17359 return;
17360 end if;
17361
17362 if Present (Extension) and then not Taggd then
17363 Error_Msg_N
17364 ("type derived from untagged type cannot have extension", Indic);
17365
17366 elsif No (Extension) and then Taggd then
17367
17368 -- If this declaration is within a private part (or body) of a
17369 -- generic instantiation then the derivation is allowed (the parent
17370 -- type can only appear tagged in this case if it's a generic actual
17371 -- type, since it would otherwise have been rejected in the analysis
17372 -- of the generic template).
17373
17374 if not Is_Generic_Actual_Type (Parent_Type)
17375 or else In_Visible_Part (Scope (Parent_Type))
17376 then
17377 if Is_Class_Wide_Type (Parent_Type) then
17378 Error_Msg_N
17379 ("parent type must not be a class-wide type", Indic);
17380
17381 -- Use specific type to prevent cascaded errors.
17382
17383 Parent_Type := Etype (Parent_Type);
17384
17385 else
17386 Error_Msg_N
17387 ("type derived from tagged type must have extension", Indic);
17388 end if;
17389 end if;
17390 end if;
17391
17392 -- AI-443: Synchronized formal derived types require a private
17393 -- extension. There is no point in checking the ancestor type or
17394 -- the progenitors since the construct is wrong to begin with.
17395
17396 if Ada_Version >= Ada_2005
17397 and then Is_Generic_Type (T)
17398 and then Present (Original_Node (N))
17399 then
17400 declare
17401 Decl : constant Node_Id := Original_Node (N);
17402
17403 begin
17404 if Nkind (Decl) = N_Formal_Type_Declaration
17405 and then Nkind (Formal_Type_Definition (Decl)) =
17406 N_Formal_Derived_Type_Definition
17407 and then Synchronized_Present (Formal_Type_Definition (Decl))
17408 and then No (Extension)
17409
17410 -- Avoid emitting a duplicate error message
17411
17412 and then not Error_Posted (Indic)
17413 then
17414 Error_Msg_N
17415 ("synchronized derived type must have extension", N);
17416 end if;
17417 end;
17418 end if;
17419
17420 if Null_Exclusion_Present (Def)
17421 and then not Is_Access_Type (Parent_Type)
17422 then
17423 Error_Msg_N ("null exclusion can only apply to an access type", N);
17424 end if;
17425
17426 Check_Wide_Character_Restriction (Parent_Type, Indic);
17427
17428 -- Avoid deriving parent primitives of underlying record views
17429
17430 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17431 Derive_Subps => not Is_Underlying_Record_View (T));
17432
17433 -- AI-419: The parent type of an explicitly limited derived type must
17434 -- be a limited type or a limited interface.
17435
17436 if Limited_Present (Def) then
17437 Set_Is_Limited_Record (T);
17438
17439 if Is_Interface (T) then
17440 Set_Is_Limited_Interface (T);
17441 end if;
17442
17443 if not Is_Limited_Type (Parent_Type)
17444 and then
17445 (not Is_Interface (Parent_Type)
17446 or else not Is_Limited_Interface (Parent_Type))
17447 then
17448 -- AI05-0096: a derivation in the private part of an instance is
17449 -- legal if the generic formal is untagged limited, and the actual
17450 -- is non-limited.
17451
17452 if Is_Generic_Actual_Type (Parent_Type)
17453 and then In_Private_Part (Current_Scope)
17454 and then
17455 not Is_Tagged_Type
17456 (Generic_Parent_Type (Parent (Parent_Type)))
17457 then
17458 null;
17459
17460 else
17461 Error_Msg_NE
17462 ("parent type& of limited type must be limited",
17463 N, Parent_Type);
17464 end if;
17465 end if;
17466 end if;
17467 end Derived_Type_Declaration;
17468
17469 ------------------------
17470 -- Diagnose_Interface --
17471 ------------------------
17472
17473 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17474 begin
17475 if not Is_Interface (E) and then E /= Any_Type then
17476 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17477 end if;
17478 end Diagnose_Interface;
17479
17480 ----------------------------------
17481 -- Enumeration_Type_Declaration --
17482 ----------------------------------
17483
17484 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17485 Ev : Uint;
17486 L : Node_Id;
17487 R_Node : Node_Id;
17488 B_Node : Node_Id;
17489
17490 begin
17491 -- Create identifier node representing lower bound
17492
17493 B_Node := New_Node (N_Identifier, Sloc (Def));
17494 L := First (Literals (Def));
17495 Set_Chars (B_Node, Chars (L));
17496 Set_Entity (B_Node, L);
17497 Set_Etype (B_Node, T);
17498 Set_Is_Static_Expression (B_Node, True);
17499
17500 R_Node := New_Node (N_Range, Sloc (Def));
17501 Set_Low_Bound (R_Node, B_Node);
17502
17503 Mutate_Ekind (T, E_Enumeration_Type);
17504 Set_First_Literal (T, L);
17505 Set_Etype (T, T);
17506 Set_Is_Constrained (T);
17507
17508 Ev := Uint_0;
17509
17510 -- Loop through literals of enumeration type setting pos and rep values
17511 -- except that if the Ekind is already set, then it means the literal
17512 -- was already constructed (case of a derived type declaration and we
17513 -- should not disturb the Pos and Rep values.
17514
17515 while Present (L) loop
17516 if Ekind (L) /= E_Enumeration_Literal then
17517 Mutate_Ekind (L, E_Enumeration_Literal);
17518 Set_Enumeration_Pos (L, Ev);
17519 Set_Enumeration_Rep (L, Ev);
17520 Set_Is_Known_Valid (L, True);
17521 end if;
17522
17523 Set_Etype (L, T);
17524 New_Overloaded_Entity (L);
17525 Generate_Definition (L);
17526 Set_Convention (L, Convention_Intrinsic);
17527
17528 -- Case of character literal
17529
17530 if Nkind (L) = N_Defining_Character_Literal then
17531 Set_Is_Character_Type (T, True);
17532
17533 -- Check violation of No_Wide_Characters
17534
17535 if Restriction_Check_Required (No_Wide_Characters) then
17536 Get_Name_String (Chars (L));
17537
17538 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17539 Check_Restriction (No_Wide_Characters, L);
17540 end if;
17541 end if;
17542 end if;
17543
17544 Ev := Ev + 1;
17545 Next (L);
17546 end loop;
17547
17548 -- Now create a node representing upper bound
17549
17550 B_Node := New_Node (N_Identifier, Sloc (Def));
17551 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17552 Set_Entity (B_Node, Last (Literals (Def)));
17553 Set_Etype (B_Node, T);
17554 Set_Is_Static_Expression (B_Node, True);
17555
17556 Set_High_Bound (R_Node, B_Node);
17557
17558 -- Initialize various fields of the type. Some of this information
17559 -- may be overwritten later through rep.clauses.
17560
17561 Set_Scalar_Range (T, R_Node);
17562 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17563 Set_Enum_Esize (T);
17564 Set_Enum_Pos_To_Rep (T, Empty);
17565
17566 -- Set Discard_Names if configuration pragma set, or if there is
17567 -- a parameterless pragma in the current declarative region
17568
17569 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17570 Set_Discard_Names (T);
17571 end if;
17572
17573 -- Process end label if there is one
17574
17575 if Present (Def) then
17576 Process_End_Label (Def, 'e', T);
17577 end if;
17578 end Enumeration_Type_Declaration;
17579
17580 ---------------------------------
17581 -- Expand_To_Stored_Constraint --
17582 ---------------------------------
17583
17584 function Expand_To_Stored_Constraint
17585 (Typ : Entity_Id;
17586 Constraint : Elist_Id) return Elist_Id
17587 is
17588 Explicitly_Discriminated_Type : Entity_Id;
17589 Expansion : Elist_Id;
17590 Discriminant : Entity_Id;
17591
17592 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17593 -- Find the nearest type that actually specifies discriminants
17594
17595 ---------------------------------
17596 -- Type_With_Explicit_Discrims --
17597 ---------------------------------
17598
17599 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17600 Typ : constant E := Base_Type (Id);
17601
17602 begin
17603 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17604 if Present (Full_View (Typ)) then
17605 return Type_With_Explicit_Discrims (Full_View (Typ));
17606 end if;
17607
17608 else
17609 if Has_Discriminants (Typ) then
17610 return Typ;
17611 end if;
17612 end if;
17613
17614 if Etype (Typ) = Typ then
17615 return Empty;
17616 elsif Has_Discriminants (Typ) then
17617 return Typ;
17618 else
17619 return Type_With_Explicit_Discrims (Etype (Typ));
17620 end if;
17621
17622 end Type_With_Explicit_Discrims;
17623
17624 -- Start of processing for Expand_To_Stored_Constraint
17625
17626 begin
17627 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17628 return No_Elist;
17629 end if;
17630
17631 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17632
17633 if No (Explicitly_Discriminated_Type) then
17634 return No_Elist;
17635 end if;
17636
17637 Expansion := New_Elmt_List;
17638
17639 Discriminant :=
17640 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17641 while Present (Discriminant) loop
17642 Append_Elmt
17643 (Get_Discriminant_Value
17644 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17645 To => Expansion);
17646 Next_Stored_Discriminant (Discriminant);
17647 end loop;
17648
17649 return Expansion;
17650 end Expand_To_Stored_Constraint;
17651
17652 ---------------------------
17653 -- Find_Hidden_Interface --
17654 ---------------------------
17655
17656 function Find_Hidden_Interface
17657 (Src : Elist_Id;
17658 Dest : Elist_Id) return Entity_Id
17659 is
17660 Iface : Entity_Id;
17661 Iface_Elmt : Elmt_Id;
17662
17663 begin
17664 if Present (Src) and then Present (Dest) then
17665 Iface_Elmt := First_Elmt (Src);
17666 while Present (Iface_Elmt) loop
17667 Iface := Node (Iface_Elmt);
17668
17669 if Is_Interface (Iface)
17670 and then not Contain_Interface (Iface, Dest)
17671 then
17672 return Iface;
17673 end if;
17674
17675 Next_Elmt (Iface_Elmt);
17676 end loop;
17677 end if;
17678
17679 return Empty;
17680 end Find_Hidden_Interface;
17681
17682 --------------------
17683 -- Find_Type_Name --
17684 --------------------
17685
17686 function Find_Type_Name (N : Node_Id) return Entity_Id is
17687 Id : constant Entity_Id := Defining_Identifier (N);
17688 New_Id : Entity_Id;
17689 Prev : Entity_Id;
17690 Prev_Par : Node_Id;
17691
17692 procedure Check_Duplicate_Aspects;
17693 -- Check that aspects specified in a completion have not been specified
17694 -- already in the partial view.
17695
17696 procedure Tag_Mismatch;
17697 -- Diagnose a tagged partial view whose full view is untagged. We post
17698 -- the message on the full view, with a reference to the previous
17699 -- partial view. The partial view can be private or incomplete, and
17700 -- these are handled in a different manner, so we determine the position
17701 -- of the error message from the respective slocs of both.
17702
17703 -----------------------------
17704 -- Check_Duplicate_Aspects --
17705 -----------------------------
17706
17707 procedure Check_Duplicate_Aspects is
17708 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17709 -- Return the corresponding aspect of the partial view which matches
17710 -- the aspect id of Asp. Return Empty is no such aspect exists.
17711
17712 -----------------------------
17713 -- Get_Partial_View_Aspect --
17714 -----------------------------
17715
17716 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17717 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17718 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17719 Prev_Asp : Node_Id;
17720
17721 begin
17722 if Present (Prev_Asps) then
17723 Prev_Asp := First (Prev_Asps);
17724 while Present (Prev_Asp) loop
17725 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17726 return Prev_Asp;
17727 end if;
17728
17729 Next (Prev_Asp);
17730 end loop;
17731 end if;
17732
17733 return Empty;
17734 end Get_Partial_View_Aspect;
17735
17736 -- Local variables
17737
17738 Full_Asps : constant List_Id := Aspect_Specifications (N);
17739 Full_Asp : Node_Id;
17740 Part_Asp : Node_Id;
17741
17742 -- Start of processing for Check_Duplicate_Aspects
17743
17744 begin
17745 if Present (Full_Asps) then
17746 Full_Asp := First (Full_Asps);
17747 while Present (Full_Asp) loop
17748 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17749
17750 -- An aspect and its class-wide counterpart are two distinct
17751 -- aspects and may apply to both views of an entity.
17752
17753 if Present (Part_Asp)
17754 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17755 then
17756 Error_Msg_N
17757 ("aspect already specified in private declaration",
17758 Full_Asp);
17759
17760 Remove (Full_Asp);
17761 return;
17762 end if;
17763
17764 if Has_Discriminants (Prev)
17765 and then not Has_Unknown_Discriminants (Prev)
17766 and then Get_Aspect_Id (Full_Asp) =
17767 Aspect_Implicit_Dereference
17768 then
17769 Error_Msg_N
17770 ("cannot specify aspect if partial view has known "
17771 & "discriminants", Full_Asp);
17772 end if;
17773
17774 Next (Full_Asp);
17775 end loop;
17776 end if;
17777 end Check_Duplicate_Aspects;
17778
17779 ------------------
17780 -- Tag_Mismatch --
17781 ------------------
17782
17783 procedure Tag_Mismatch is
17784 begin
17785 if Sloc (Prev) < Sloc (Id) then
17786 if Ada_Version >= Ada_2012
17787 and then Nkind (N) = N_Private_Type_Declaration
17788 then
17789 Error_Msg_NE
17790 ("declaration of private } must be a tagged type", Id, Prev);
17791 else
17792 Error_Msg_NE
17793 ("full declaration of } must be a tagged type", Id, Prev);
17794 end if;
17795
17796 else
17797 if Ada_Version >= Ada_2012
17798 and then Nkind (N) = N_Private_Type_Declaration
17799 then
17800 Error_Msg_NE
17801 ("declaration of private } must be a tagged type", Prev, Id);
17802 else
17803 Error_Msg_NE
17804 ("full declaration of } must be a tagged type", Prev, Id);
17805 end if;
17806 end if;
17807 end Tag_Mismatch;
17808
17809 -- Start of processing for Find_Type_Name
17810
17811 begin
17812 -- Find incomplete declaration, if one was given
17813
17814 Prev := Current_Entity_In_Scope (Id);
17815
17816 -- New type declaration
17817
17818 if No (Prev) then
17819 Enter_Name (Id);
17820 return Id;
17821
17822 -- Previous declaration exists
17823
17824 else
17825 Prev_Par := Parent (Prev);
17826
17827 -- Error if not incomplete/private case except if previous
17828 -- declaration is implicit, etc. Enter_Name will emit error if
17829 -- appropriate.
17830
17831 if not Is_Incomplete_Or_Private_Type (Prev) then
17832 Enter_Name (Id);
17833 New_Id := Id;
17834
17835 -- Check invalid completion of private or incomplete type
17836
17837 elsif Nkind (N) not in N_Full_Type_Declaration
17838 | N_Task_Type_Declaration
17839 | N_Protected_Type_Declaration
17840 and then
17841 (Ada_Version < Ada_2012
17842 or else not Is_Incomplete_Type (Prev)
17843 or else Nkind (N) not in N_Private_Type_Declaration
17844 | N_Private_Extension_Declaration)
17845 then
17846 -- Completion must be a full type declarations (RM 7.3(4))
17847
17848 Error_Msg_Sloc := Sloc (Prev);
17849 Error_Msg_NE ("invalid completion of }", Id, Prev);
17850
17851 -- Set scope of Id to avoid cascaded errors. Entity is never
17852 -- examined again, except when saving globals in generics.
17853
17854 Set_Scope (Id, Current_Scope);
17855 New_Id := Id;
17856
17857 -- If this is a repeated incomplete declaration, no further
17858 -- checks are possible.
17859
17860 if Nkind (N) = N_Incomplete_Type_Declaration then
17861 return Prev;
17862 end if;
17863
17864 -- Case of full declaration of incomplete type
17865
17866 elsif Ekind (Prev) = E_Incomplete_Type
17867 and then (Ada_Version < Ada_2012
17868 or else No (Full_View (Prev))
17869 or else not Is_Private_Type (Full_View (Prev)))
17870 then
17871 -- Indicate that the incomplete declaration has a matching full
17872 -- declaration. The defining occurrence of the incomplete
17873 -- declaration remains the visible one, and the procedure
17874 -- Get_Full_View dereferences it whenever the type is used.
17875
17876 if Present (Full_View (Prev)) then
17877 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17878 end if;
17879
17880 Set_Full_View (Prev, Id);
17881 Append_Entity (Id, Current_Scope);
17882 Set_Is_Public (Id, Is_Public (Prev));
17883 Set_Is_Internal (Id);
17884 New_Id := Prev;
17885
17886 -- If the incomplete view is tagged, a class_wide type has been
17887 -- created already. Use it for the private type as well, in order
17888 -- to prevent multiple incompatible class-wide types that may be
17889 -- created for self-referential anonymous access components.
17890
17891 if Is_Tagged_Type (Prev)
17892 and then Present (Class_Wide_Type (Prev))
17893 then
17894 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
17895 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17896
17897 -- Type of the class-wide type is the current Id. Previously
17898 -- this was not done for private declarations because of order-
17899 -- of-elaboration issues in the back end, but gigi now handles
17900 -- this properly.
17901
17902 Set_Etype (Class_Wide_Type (Id), Id);
17903 end if;
17904
17905 -- Case of full declaration of private type
17906
17907 else
17908 -- If the private type was a completion of an incomplete type then
17909 -- update Prev to reference the private type
17910
17911 if Ada_Version >= Ada_2012
17912 and then Ekind (Prev) = E_Incomplete_Type
17913 and then Present (Full_View (Prev))
17914 and then Is_Private_Type (Full_View (Prev))
17915 then
17916 Prev := Full_View (Prev);
17917 Prev_Par := Parent (Prev);
17918 end if;
17919
17920 if Nkind (N) = N_Full_Type_Declaration
17921 and then Nkind (Type_Definition (N)) in
17922 N_Record_Definition | N_Derived_Type_Definition
17923 and then Interface_Present (Type_Definition (N))
17924 then
17925 Error_Msg_N
17926 ("completion of private type cannot be an interface", N);
17927 end if;
17928
17929 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17930 if Etype (Prev) /= Prev then
17931
17932 -- Prev is a private subtype or a derived type, and needs
17933 -- no completion.
17934
17935 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17936 New_Id := Id;
17937
17938 elsif Ekind (Prev) = E_Private_Type
17939 and then Nkind (N) in N_Task_Type_Declaration
17940 | N_Protected_Type_Declaration
17941 then
17942 Error_Msg_N
17943 ("completion of nonlimited type cannot be limited", N);
17944
17945 elsif Ekind (Prev) = E_Record_Type_With_Private
17946 and then Nkind (N) in N_Task_Type_Declaration
17947 | N_Protected_Type_Declaration
17948 then
17949 if not Is_Limited_Record (Prev) then
17950 Error_Msg_N
17951 ("completion of nonlimited type cannot be limited", N);
17952
17953 elsif No (Interface_List (N)) then
17954 Error_Msg_N
17955 ("completion of tagged private type must be tagged",
17956 N);
17957 end if;
17958 end if;
17959
17960 -- Ada 2005 (AI-251): Private extension declaration of a task
17961 -- type or a protected type. This case arises when covering
17962 -- interface types.
17963
17964 elsif Nkind (N) in N_Task_Type_Declaration
17965 | N_Protected_Type_Declaration
17966 then
17967 null;
17968
17969 elsif Nkind (N) /= N_Full_Type_Declaration
17970 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17971 then
17972 Error_Msg_N
17973 ("full view of private extension must be an extension", N);
17974
17975 elsif not (Abstract_Present (Parent (Prev)))
17976 and then Abstract_Present (Type_Definition (N))
17977 then
17978 Error_Msg_N
17979 ("full view of non-abstract extension cannot be abstract", N);
17980 end if;
17981
17982 if not In_Private_Part (Current_Scope) then
17983 Error_Msg_N
17984 ("declaration of full view must appear in private part", N);
17985 end if;
17986
17987 if Ada_Version >= Ada_2012 then
17988 Check_Duplicate_Aspects;
17989 end if;
17990
17991 Copy_And_Swap (Prev, Id);
17992 Set_Has_Private_Declaration (Prev);
17993 Set_Has_Private_Declaration (Id);
17994
17995 -- AI12-0133: Indicate whether we have a partial view with
17996 -- unknown discriminants, in which case initialization of objects
17997 -- of the type do not receive an invariant check.
17998
17999 Set_Partial_View_Has_Unknown_Discr
18000 (Prev, Has_Unknown_Discriminants (Id));
18001
18002 -- Preserve aspect and iterator flags that may have been set on
18003 -- the partial view.
18004
18005 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18006 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18007
18008 -- If no error, propagate freeze_node from private to full view.
18009 -- It may have been generated for an early operational item.
18010
18011 if Present (Freeze_Node (Id))
18012 and then Serious_Errors_Detected = 0
18013 and then No (Full_View (Id))
18014 then
18015 Set_Freeze_Node (Prev, Freeze_Node (Id));
18016 Set_Freeze_Node (Id, Empty);
18017 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18018 end if;
18019
18020 Set_Full_View (Id, Prev);
18021 New_Id := Prev;
18022 end if;
18023
18024 -- Verify that full declaration conforms to partial one
18025
18026 if Is_Incomplete_Or_Private_Type (Prev)
18027 and then Present (Discriminant_Specifications (Prev_Par))
18028 then
18029 if Present (Discriminant_Specifications (N)) then
18030 if Ekind (Prev) = E_Incomplete_Type then
18031 Check_Discriminant_Conformance (N, Prev, Prev);
18032 else
18033 Check_Discriminant_Conformance (N, Prev, Id);
18034 end if;
18035
18036 else
18037 Error_Msg_N
18038 ("missing discriminants in full type declaration", N);
18039
18040 -- To avoid cascaded errors on subsequent use, share the
18041 -- discriminants of the partial view.
18042
18043 Set_Discriminant_Specifications (N,
18044 Discriminant_Specifications (Prev_Par));
18045 end if;
18046 end if;
18047
18048 -- A prior untagged partial view can have an associated class-wide
18049 -- type due to use of the class attribute, and in this case the full
18050 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18051 -- of incomplete tagged declarations, but we check for it.
18052
18053 if Is_Type (Prev)
18054 and then (Is_Tagged_Type (Prev)
18055 or else Present (Class_Wide_Type (Prev)))
18056 then
18057 -- Ada 2012 (AI05-0162): A private type may be the completion of
18058 -- an incomplete type.
18059
18060 if Ada_Version >= Ada_2012
18061 and then Is_Incomplete_Type (Prev)
18062 and then Nkind (N) in N_Private_Type_Declaration
18063 | N_Private_Extension_Declaration
18064 then
18065 -- No need to check private extensions since they are tagged
18066
18067 if Nkind (N) = N_Private_Type_Declaration
18068 and then not Tagged_Present (N)
18069 then
18070 Tag_Mismatch;
18071 end if;
18072
18073 -- The full declaration is either a tagged type (including
18074 -- a synchronized type that implements interfaces) or a
18075 -- type extension, otherwise this is an error.
18076
18077 elsif Nkind (N) in N_Task_Type_Declaration
18078 | N_Protected_Type_Declaration
18079 then
18080 if No (Interface_List (N)) and then not Error_Posted (N) then
18081 Tag_Mismatch;
18082 end if;
18083
18084 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18085
18086 -- Indicate that the previous declaration (tagged incomplete
18087 -- or private declaration) requires the same on the full one.
18088
18089 if not Tagged_Present (Type_Definition (N)) then
18090 Tag_Mismatch;
18091 Set_Is_Tagged_Type (Id);
18092 end if;
18093
18094 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18095 if No (Record_Extension_Part (Type_Definition (N))) then
18096 Error_Msg_NE
18097 ("full declaration of } must be a record extension",
18098 Prev, Id);
18099
18100 -- Set some attributes to produce a usable full view
18101
18102 Set_Is_Tagged_Type (Id);
18103 end if;
18104
18105 else
18106 Tag_Mismatch;
18107 end if;
18108 end if;
18109
18110 if Present (Prev)
18111 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18112 and then Present (Premature_Use (Parent (Prev)))
18113 then
18114 Error_Msg_Sloc := Sloc (N);
18115 Error_Msg_N
18116 ("\full declaration #", Premature_Use (Parent (Prev)));
18117 end if;
18118
18119 return New_Id;
18120 end if;
18121 end Find_Type_Name;
18122
18123 -------------------------
18124 -- Find_Type_Of_Object --
18125 -------------------------
18126
18127 function Find_Type_Of_Object
18128 (Obj_Def : Node_Id;
18129 Related_Nod : Node_Id) return Entity_Id
18130 is
18131 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18132 P : Node_Id := Parent (Obj_Def);
18133 T : Entity_Id;
18134 Nam : Name_Id;
18135
18136 begin
18137 -- If the parent is a component_definition node we climb to the
18138 -- component_declaration node
18139
18140 if Nkind (P) = N_Component_Definition then
18141 P := Parent (P);
18142 end if;
18143
18144 -- Case of an anonymous array subtype
18145
18146 if Def_Kind in N_Array_Type_Definition then
18147 T := Empty;
18148 Array_Type_Declaration (T, Obj_Def);
18149
18150 -- Create an explicit subtype whenever possible
18151
18152 elsif Nkind (P) /= N_Component_Declaration
18153 and then Def_Kind = N_Subtype_Indication
18154 then
18155 -- Base name of subtype on object name, which will be unique in
18156 -- the current scope.
18157
18158 -- If this is a duplicate declaration, return base type, to avoid
18159 -- generating duplicate anonymous types.
18160
18161 if Error_Posted (P) then
18162 Analyze (Subtype_Mark (Obj_Def));
18163 return Entity (Subtype_Mark (Obj_Def));
18164 end if;
18165
18166 Nam :=
18167 New_External_Name
18168 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18169
18170 T := Make_Defining_Identifier (Sloc (P), Nam);
18171
18172 -- If In_Spec_Expression, for example within a pre/postcondition,
18173 -- provide enough information for use of the subtype without
18174 -- depending on full analysis and freezing, which will happen when
18175 -- building the correspondiing subprogram.
18176
18177 if In_Spec_Expression then
18178 Analyze (Subtype_Mark (Obj_Def));
18179
18180 declare
18181 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18182 Decl : constant Node_Id :=
18183 Make_Subtype_Declaration (Sloc (P),
18184 Defining_Identifier => T,
18185 Subtype_Indication => Relocate_Node (Obj_Def));
18186 begin
18187 Set_Etype (T, Base_T);
18188 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18189 Set_Parent (T, Obj_Def);
18190
18191 if Ekind (T) = E_Array_Subtype then
18192 Set_First_Index (T, First_Index (Base_T));
18193 Set_Is_Constrained (T);
18194
18195 elsif Ekind (T) = E_Record_Subtype then
18196 Set_First_Entity (T, First_Entity (Base_T));
18197 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18198 Set_Is_Constrained (T);
18199 end if;
18200
18201 Insert_Before (Related_Nod, Decl);
18202 end;
18203
18204 return T;
18205 end if;
18206
18207 -- When generating code, insert subtype declaration ahead of
18208 -- declaration that generated it.
18209
18210 Insert_Action (Obj_Def,
18211 Make_Subtype_Declaration (Sloc (P),
18212 Defining_Identifier => T,
18213 Subtype_Indication => Relocate_Node (Obj_Def)));
18214
18215 -- This subtype may need freezing, and this will not be done
18216 -- automatically if the object declaration is not in declarative
18217 -- part. Since this is an object declaration, the type cannot always
18218 -- be frozen here. Deferred constants do not freeze their type
18219 -- (which often enough will be private).
18220
18221 if Nkind (P) = N_Object_Declaration
18222 and then Constant_Present (P)
18223 and then No (Expression (P))
18224 then
18225 null;
18226
18227 -- Here we freeze the base type of object type to catch premature use
18228 -- of discriminated private type without a full view.
18229
18230 else
18231 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18232 end if;
18233
18234 -- Ada 2005 AI-406: the object definition in an object declaration
18235 -- can be an access definition.
18236
18237 elsif Def_Kind = N_Access_Definition then
18238 T := Access_Definition (Related_Nod, Obj_Def);
18239
18240 Set_Is_Local_Anonymous_Access
18241 (T, Ada_Version < Ada_2012
18242 or else Nkind (P) /= N_Object_Declaration
18243 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18244
18245 -- Otherwise, the object definition is just a subtype_mark
18246
18247 else
18248 T := Process_Subtype (Obj_Def, Related_Nod);
18249 end if;
18250
18251 return T;
18252 end Find_Type_Of_Object;
18253
18254 --------------------------------
18255 -- Find_Type_Of_Subtype_Indic --
18256 --------------------------------
18257
18258 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18259 Typ : Entity_Id;
18260
18261 begin
18262 -- Case of subtype mark with a constraint
18263
18264 if Nkind (S) = N_Subtype_Indication then
18265 Find_Type (Subtype_Mark (S));
18266 Typ := Entity (Subtype_Mark (S));
18267
18268 if not
18269 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18270 then
18271 Error_Msg_N
18272 ("incorrect constraint for this kind of type", Constraint (S));
18273 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18274 end if;
18275
18276 -- Otherwise we have a subtype mark without a constraint
18277
18278 elsif Error_Posted (S) then
18279 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18280 return Any_Type;
18281
18282 else
18283 Find_Type (S);
18284 Typ := Entity (S);
18285 end if;
18286
18287 return Typ;
18288 end Find_Type_Of_Subtype_Indic;
18289
18290 -------------------------------------
18291 -- Floating_Point_Type_Declaration --
18292 -------------------------------------
18293
18294 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18295 Digs : constant Node_Id := Digits_Expression (Def);
18296 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18297 Digs_Val : Uint;
18298 Base_Typ : Entity_Id;
18299 Implicit_Base : Entity_Id;
18300
18301 function Can_Derive_From (E : Entity_Id) return Boolean;
18302 -- Find if given digits value, and possibly a specified range, allows
18303 -- derivation from specified type
18304
18305 procedure Convert_Bound (B : Node_Id);
18306 -- If specified, the bounds must be static but may be of different
18307 -- types. They must be converted into machine numbers of the base type,
18308 -- in accordance with RM 4.9(38).
18309
18310 function Find_Base_Type return Entity_Id;
18311 -- Find a predefined base type that Def can derive from, or generate
18312 -- an error and substitute Long_Long_Float if none exists.
18313
18314 ---------------------
18315 -- Can_Derive_From --
18316 ---------------------
18317
18318 function Can_Derive_From (E : Entity_Id) return Boolean is
18319 Spec : constant Entity_Id := Real_Range_Specification (Def);
18320
18321 begin
18322 -- Check specified "digits" constraint
18323
18324 if Digs_Val > Digits_Value (E) then
18325 return False;
18326 end if;
18327
18328 -- Check for matching range, if specified
18329
18330 if Present (Spec) then
18331 if Expr_Value_R (Type_Low_Bound (E)) >
18332 Expr_Value_R (Low_Bound (Spec))
18333 then
18334 return False;
18335 end if;
18336
18337 if Expr_Value_R (Type_High_Bound (E)) <
18338 Expr_Value_R (High_Bound (Spec))
18339 then
18340 return False;
18341 end if;
18342 end if;
18343
18344 return True;
18345 end Can_Derive_From;
18346
18347 -------------------
18348 -- Convert_Bound --
18349 --------------------
18350
18351 procedure Convert_Bound (B : Node_Id) is
18352 begin
18353 -- If the bound is not a literal it can only be static if it is
18354 -- a static constant, possibly of a specified type.
18355
18356 if Is_Entity_Name (B)
18357 and then Ekind (Entity (B)) = E_Constant
18358 then
18359 Rewrite (B, Constant_Value (Entity (B)));
18360 end if;
18361
18362 if Nkind (B) = N_Real_Literal then
18363 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18364 Set_Is_Machine_Number (B);
18365 Set_Etype (B, Base_Typ);
18366 end if;
18367 end Convert_Bound;
18368
18369 --------------------
18370 -- Find_Base_Type --
18371 --------------------
18372
18373 function Find_Base_Type return Entity_Id is
18374 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18375
18376 begin
18377 -- Iterate over the predefined types in order, returning the first
18378 -- one that Def can derive from.
18379
18380 while Present (Choice) loop
18381 if Can_Derive_From (Node (Choice)) then
18382 return Node (Choice);
18383 end if;
18384
18385 Next_Elmt (Choice);
18386 end loop;
18387
18388 -- If we can't derive from any existing type, use Long_Long_Float
18389 -- and give appropriate message explaining the problem.
18390
18391 if Digs_Val > Max_Digs_Val then
18392 -- It might be the case that there is a type with the requested
18393 -- range, just not the combination of digits and range.
18394
18395 Error_Msg_N
18396 ("no predefined type has requested range and precision",
18397 Real_Range_Specification (Def));
18398
18399 else
18400 Error_Msg_N
18401 ("range too large for any predefined type",
18402 Real_Range_Specification (Def));
18403 end if;
18404
18405 return Standard_Long_Long_Float;
18406 end Find_Base_Type;
18407
18408 -- Start of processing for Floating_Point_Type_Declaration
18409
18410 begin
18411 Check_Restriction (No_Floating_Point, Def);
18412
18413 -- Create an implicit base type
18414
18415 Implicit_Base :=
18416 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18417
18418 -- Analyze and verify digits value
18419
18420 Analyze_And_Resolve (Digs, Any_Integer);
18421 Check_Digits_Expression (Digs);
18422 Digs_Val := Expr_Value (Digs);
18423
18424 -- Process possible range spec and find correct type to derive from
18425
18426 Process_Real_Range_Specification (Def);
18427
18428 -- Check that requested number of digits is not too high.
18429
18430 if Digs_Val > Max_Digs_Val then
18431
18432 -- The check for Max_Base_Digits may be somewhat expensive, as it
18433 -- requires reading System, so only do it when necessary.
18434
18435 declare
18436 Max_Base_Digits : constant Uint :=
18437 Expr_Value
18438 (Expression
18439 (Parent (RTE (RE_Max_Base_Digits))));
18440
18441 begin
18442 if Digs_Val > Max_Base_Digits then
18443 Error_Msg_Uint_1 := Max_Base_Digits;
18444 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18445
18446 elsif No (Real_Range_Specification (Def)) then
18447 Error_Msg_Uint_1 := Max_Digs_Val;
18448 Error_Msg_N ("types with more than ^ digits need range spec "
18449 & "(RM 3.5.7(6))", Digs);
18450 end if;
18451 end;
18452 end if;
18453
18454 -- Find a suitable type to derive from or complain and use a substitute
18455
18456 Base_Typ := Find_Base_Type;
18457
18458 -- If there are bounds given in the declaration use them as the bounds
18459 -- of the type, otherwise use the bounds of the predefined base type
18460 -- that was chosen based on the Digits value.
18461
18462 if Present (Real_Range_Specification (Def)) then
18463 Set_Scalar_Range (T, Real_Range_Specification (Def));
18464 Set_Is_Constrained (T);
18465
18466 Convert_Bound (Type_Low_Bound (T));
18467 Convert_Bound (Type_High_Bound (T));
18468
18469 else
18470 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18471 end if;
18472
18473 -- Complete definition of implicit base and declared first subtype. The
18474 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18475 -- are not clobbered when the floating point type acts as a full view of
18476 -- a private type.
18477
18478 Set_Etype (Implicit_Base, Base_Typ);
18479 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18480 Set_Size_Info (Implicit_Base, Base_Typ);
18481 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18482 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18483 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18484 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18485
18486 Mutate_Ekind (T, E_Floating_Point_Subtype);
18487 Set_Etype (T, Implicit_Base);
18488 Set_Size_Info (T, Implicit_Base);
18489 Set_RM_Size (T, RM_Size (Implicit_Base));
18490 Inherit_Rep_Item_Chain (T, Implicit_Base);
18491 Set_Digits_Value (T, Digs_Val);
18492 end Floating_Point_Type_Declaration;
18493
18494 ----------------------------
18495 -- Get_Discriminant_Value --
18496 ----------------------------
18497
18498 -- This is the situation:
18499
18500 -- There is a non-derived type
18501
18502 -- type T0 (Dx, Dy, Dz...)
18503
18504 -- There are zero or more levels of derivation, with each derivation
18505 -- either purely inheriting the discriminants, or defining its own.
18506
18507 -- type Ti is new Ti-1
18508 -- or
18509 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18510 -- or
18511 -- subtype Ti is ...
18512
18513 -- The subtype issue is avoided by the use of Original_Record_Component,
18514 -- and the fact that derived subtypes also derive the constraints.
18515
18516 -- This chain leads back from
18517
18518 -- Typ_For_Constraint
18519
18520 -- Typ_For_Constraint has discriminants, and the value for each
18521 -- discriminant is given by its corresponding Elmt of Constraints.
18522
18523 -- Discriminant is some discriminant in this hierarchy
18524
18525 -- We need to return its value
18526
18527 -- We do this by recursively searching each level, and looking for
18528 -- Discriminant. Once we get to the bottom, we start backing up
18529 -- returning the value for it which may in turn be a discriminant
18530 -- further up, so on the backup we continue the substitution.
18531
18532 function Get_Discriminant_Value
18533 (Discriminant : Entity_Id;
18534 Typ_For_Constraint : Entity_Id;
18535 Constraint : Elist_Id) return Node_Id
18536 is
18537 function Root_Corresponding_Discriminant
18538 (Discr : Entity_Id) return Entity_Id;
18539 -- Given a discriminant, traverse the chain of inherited discriminants
18540 -- and return the topmost discriminant.
18541
18542 function Search_Derivation_Levels
18543 (Ti : Entity_Id;
18544 Discrim_Values : Elist_Id;
18545 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18546 -- This is the routine that performs the recursive search of levels
18547 -- as described above.
18548
18549 -------------------------------------
18550 -- Root_Corresponding_Discriminant --
18551 -------------------------------------
18552
18553 function Root_Corresponding_Discriminant
18554 (Discr : Entity_Id) return Entity_Id
18555 is
18556 D : Entity_Id;
18557
18558 begin
18559 D := Discr;
18560 while Present (Corresponding_Discriminant (D)) loop
18561 D := Corresponding_Discriminant (D);
18562 end loop;
18563
18564 return D;
18565 end Root_Corresponding_Discriminant;
18566
18567 ------------------------------
18568 -- Search_Derivation_Levels --
18569 ------------------------------
18570
18571 function Search_Derivation_Levels
18572 (Ti : Entity_Id;
18573 Discrim_Values : Elist_Id;
18574 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18575 is
18576 Assoc : Elmt_Id;
18577 Disc : Entity_Id;
18578 Result : Node_Or_Entity_Id;
18579 Result_Entity : Node_Id;
18580
18581 begin
18582 -- If inappropriate type, return Error, this happens only in
18583 -- cascaded error situations, and we want to avoid a blow up.
18584
18585 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18586 return Error;
18587 end if;
18588
18589 -- Look deeper if possible. Use Stored_Constraints only for
18590 -- untagged types. For tagged types use the given constraint.
18591 -- This asymmetry needs explanation???
18592
18593 if not Stored_Discrim_Values
18594 and then Present (Stored_Constraint (Ti))
18595 and then not Is_Tagged_Type (Ti)
18596 then
18597 Result :=
18598 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18599
18600 else
18601 declare
18602 Td : Entity_Id := Etype (Ti);
18603
18604 begin
18605 -- If the parent type is private, the full view may include
18606 -- renamed discriminants, and it is those stored values that
18607 -- may be needed (the partial view never has more information
18608 -- than the full view).
18609
18610 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18611 Td := Full_View (Td);
18612 end if;
18613
18614 if Td = Ti then
18615 Result := Discriminant;
18616
18617 else
18618 if Present (Stored_Constraint (Ti)) then
18619 Result :=
18620 Search_Derivation_Levels
18621 (Td, Stored_Constraint (Ti), True);
18622 else
18623 Result :=
18624 Search_Derivation_Levels
18625 (Td, Discrim_Values, Stored_Discrim_Values);
18626 end if;
18627 end if;
18628 end;
18629 end if;
18630
18631 -- Extra underlying places to search, if not found above. For
18632 -- concurrent types, the relevant discriminant appears in the
18633 -- corresponding record. For a type derived from a private type
18634 -- without discriminant, the full view inherits the discriminants
18635 -- of the full view of the parent.
18636
18637 if Result = Discriminant then
18638 if Is_Concurrent_Type (Ti)
18639 and then Present (Corresponding_Record_Type (Ti))
18640 then
18641 Result :=
18642 Search_Derivation_Levels (
18643 Corresponding_Record_Type (Ti),
18644 Discrim_Values,
18645 Stored_Discrim_Values);
18646
18647 elsif Is_Private_Type (Ti)
18648 and then not Has_Discriminants (Ti)
18649 and then Present (Full_View (Ti))
18650 and then Etype (Full_View (Ti)) /= Ti
18651 then
18652 Result :=
18653 Search_Derivation_Levels (
18654 Full_View (Ti),
18655 Discrim_Values,
18656 Stored_Discrim_Values);
18657 end if;
18658 end if;
18659
18660 -- If Result is not a (reference to a) discriminant, return it,
18661 -- otherwise set Result_Entity to the discriminant.
18662
18663 if Nkind (Result) = N_Defining_Identifier then
18664 pragma Assert (Result = Discriminant);
18665 Result_Entity := Result;
18666
18667 else
18668 if not Denotes_Discriminant (Result) then
18669 return Result;
18670 end if;
18671
18672 Result_Entity := Entity (Result);
18673 end if;
18674
18675 -- See if this level of derivation actually has discriminants because
18676 -- tagged derivations can add them, hence the lower levels need not
18677 -- have any.
18678
18679 if not Has_Discriminants (Ti) then
18680 return Result;
18681 end if;
18682
18683 -- Scan Ti's discriminants for Result_Entity, and return its
18684 -- corresponding value, if any.
18685
18686 Result_Entity := Original_Record_Component (Result_Entity);
18687
18688 Assoc := First_Elmt (Discrim_Values);
18689
18690 if Stored_Discrim_Values then
18691 Disc := First_Stored_Discriminant (Ti);
18692 else
18693 Disc := First_Discriminant (Ti);
18694 end if;
18695
18696 while Present (Disc) loop
18697
18698 -- If no further associations return the discriminant, value will
18699 -- be found on the second pass.
18700
18701 if No (Assoc) then
18702 return Result;
18703 end if;
18704
18705 if Original_Record_Component (Disc) = Result_Entity then
18706 return Node (Assoc);
18707 end if;
18708
18709 Next_Elmt (Assoc);
18710
18711 if Stored_Discrim_Values then
18712 Next_Stored_Discriminant (Disc);
18713 else
18714 Next_Discriminant (Disc);
18715 end if;
18716 end loop;
18717
18718 -- Could not find it
18719
18720 return Result;
18721 end Search_Derivation_Levels;
18722
18723 -- Local Variables
18724
18725 Result : Node_Or_Entity_Id;
18726
18727 -- Start of processing for Get_Discriminant_Value
18728
18729 begin
18730 -- ??? This routine is a gigantic mess and will be deleted. For the
18731 -- time being just test for the trivial case before calling recurse.
18732
18733 -- We are now celebrating the 20th anniversary of this comment!
18734
18735 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18736 declare
18737 D : Entity_Id;
18738 E : Elmt_Id;
18739
18740 begin
18741 D := First_Discriminant (Typ_For_Constraint);
18742 E := First_Elmt (Constraint);
18743 while Present (D) loop
18744 if Chars (D) = Chars (Discriminant) then
18745 return Node (E);
18746 end if;
18747
18748 Next_Discriminant (D);
18749 Next_Elmt (E);
18750 end loop;
18751 end;
18752 end if;
18753
18754 Result := Search_Derivation_Levels
18755 (Typ_For_Constraint, Constraint, False);
18756
18757 -- ??? hack to disappear when this routine is gone
18758
18759 if Nkind (Result) = N_Defining_Identifier then
18760 declare
18761 D : Entity_Id;
18762 E : Elmt_Id;
18763
18764 begin
18765 D := First_Discriminant (Typ_For_Constraint);
18766 E := First_Elmt (Constraint);
18767 while Present (D) loop
18768 if Root_Corresponding_Discriminant (D) = Discriminant then
18769 return Node (E);
18770 end if;
18771
18772 Next_Discriminant (D);
18773 Next_Elmt (E);
18774 end loop;
18775 end;
18776 end if;
18777
18778 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18779 return Result;
18780 end Get_Discriminant_Value;
18781
18782 --------------------------
18783 -- Has_Range_Constraint --
18784 --------------------------
18785
18786 function Has_Range_Constraint (N : Node_Id) return Boolean is
18787 C : constant Node_Id := Constraint (N);
18788
18789 begin
18790 if Nkind (C) = N_Range_Constraint then
18791 return True;
18792
18793 elsif Nkind (C) = N_Digits_Constraint then
18794 return
18795 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18796 or else Present (Range_Constraint (C));
18797
18798 elsif Nkind (C) = N_Delta_Constraint then
18799 return Present (Range_Constraint (C));
18800
18801 else
18802 return False;
18803 end if;
18804 end Has_Range_Constraint;
18805
18806 ------------------------
18807 -- Inherit_Components --
18808 ------------------------
18809
18810 function Inherit_Components
18811 (N : Node_Id;
18812 Parent_Base : Entity_Id;
18813 Derived_Base : Entity_Id;
18814 Is_Tagged : Boolean;
18815 Inherit_Discr : Boolean;
18816 Discs : Elist_Id) return Elist_Id
18817 is
18818 Assoc_List : constant Elist_Id := New_Elmt_List;
18819
18820 procedure Inherit_Component
18821 (Old_C : Entity_Id;
18822 Plain_Discrim : Boolean := False;
18823 Stored_Discrim : Boolean := False);
18824 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18825 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18826 -- True, Old_C is a stored discriminant. If they are both false then
18827 -- Old_C is a regular component.
18828
18829 -----------------------
18830 -- Inherit_Component --
18831 -----------------------
18832
18833 procedure Inherit_Component
18834 (Old_C : Entity_Id;
18835 Plain_Discrim : Boolean := False;
18836 Stored_Discrim : Boolean := False)
18837 is
18838 procedure Set_Anonymous_Type (Id : Entity_Id);
18839 -- Id denotes the entity of an access discriminant or anonymous
18840 -- access component. Set the type of Id to either the same type of
18841 -- Old_C or create a new one depending on whether the parent and
18842 -- the child types are in the same scope.
18843
18844 ------------------------
18845 -- Set_Anonymous_Type --
18846 ------------------------
18847
18848 procedure Set_Anonymous_Type (Id : Entity_Id) is
18849 Old_Typ : constant Entity_Id := Etype (Old_C);
18850
18851 begin
18852 if Scope (Parent_Base) = Scope (Derived_Base) then
18853 Set_Etype (Id, Old_Typ);
18854
18855 -- The parent and the derived type are in two different scopes.
18856 -- Reuse the type of the original discriminant / component by
18857 -- copying it in order to preserve all attributes.
18858
18859 else
18860 declare
18861 Typ : constant Entity_Id := New_Copy (Old_Typ);
18862
18863 begin
18864 Set_Etype (Id, Typ);
18865
18866 -- Since we do not generate component declarations for
18867 -- inherited components, associate the itype with the
18868 -- derived type.
18869
18870 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18871 Set_Scope (Typ, Derived_Base);
18872 end;
18873 end if;
18874 end Set_Anonymous_Type;
18875
18876 -- Local variables and constants
18877
18878 New_C : constant Entity_Id := New_Copy (Old_C);
18879
18880 Corr_Discrim : Entity_Id;
18881 Discrim : Entity_Id;
18882
18883 -- Start of processing for Inherit_Component
18884
18885 begin
18886 pragma Assert (not Is_Tagged or not Stored_Discrim);
18887
18888 Set_Parent (New_C, Parent (Old_C));
18889
18890 -- Regular discriminants and components must be inserted in the scope
18891 -- of the Derived_Base. Do it here.
18892
18893 if not Stored_Discrim then
18894 Enter_Name (New_C);
18895 end if;
18896
18897 -- For tagged types the Original_Record_Component must point to
18898 -- whatever this field was pointing to in the parent type. This has
18899 -- already been achieved by the call to New_Copy above.
18900
18901 if not Is_Tagged then
18902 Set_Original_Record_Component (New_C, New_C);
18903 Set_Corresponding_Record_Component (New_C, Old_C);
18904 end if;
18905
18906 -- Set the proper type of an access discriminant
18907
18908 if Ekind (New_C) = E_Discriminant
18909 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18910 then
18911 Set_Anonymous_Type (New_C);
18912 end if;
18913
18914 -- If we have inherited a component then see if its Etype contains
18915 -- references to Parent_Base discriminants. In this case, replace
18916 -- these references with the constraints given in Discs. We do not
18917 -- do this for the partial view of private types because this is
18918 -- not needed (only the components of the full view will be used
18919 -- for code generation) and cause problem. We also avoid this
18920 -- transformation in some error situations.
18921
18922 if Ekind (New_C) = E_Component then
18923
18924 -- Set the proper type of an anonymous access component
18925
18926 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18927 Set_Anonymous_Type (New_C);
18928
18929 elsif (Is_Private_Type (Derived_Base)
18930 and then not Is_Generic_Type (Derived_Base))
18931 or else (Is_Empty_Elmt_List (Discs)
18932 and then not Expander_Active)
18933 then
18934 Set_Etype (New_C, Etype (Old_C));
18935
18936 else
18937 -- The current component introduces a circularity of the
18938 -- following kind:
18939
18940 -- limited with Pack_2;
18941 -- package Pack_1 is
18942 -- type T_1 is tagged record
18943 -- Comp : access Pack_2.T_2;
18944 -- ...
18945 -- end record;
18946 -- end Pack_1;
18947
18948 -- with Pack_1;
18949 -- package Pack_2 is
18950 -- type T_2 is new Pack_1.T_1 with ...;
18951 -- end Pack_2;
18952
18953 Set_Etype
18954 (New_C,
18955 Constrain_Component_Type
18956 (Old_C, Derived_Base, N, Parent_Base, Discs));
18957 end if;
18958 end if;
18959
18960 -- In derived tagged types it is illegal to reference a non
18961 -- discriminant component in the parent type. To catch this, mark
18962 -- these components with an Ekind of E_Void. This will be reset in
18963 -- Record_Type_Definition after processing the record extension of
18964 -- the derived type.
18965
18966 -- If the declaration is a private extension, there is no further
18967 -- record extension to process, and the components retain their
18968 -- current kind, because they are visible at this point.
18969
18970 if Is_Tagged and then Ekind (New_C) = E_Component
18971 and then Nkind (N) /= N_Private_Extension_Declaration
18972 then
18973 Mutate_Ekind (New_C, E_Void);
18974 end if;
18975
18976 if Plain_Discrim then
18977 Set_Corresponding_Discriminant (New_C, Old_C);
18978 Build_Discriminal (New_C);
18979
18980 -- If we are explicitly inheriting a stored discriminant it will be
18981 -- completely hidden.
18982
18983 elsif Stored_Discrim then
18984 Set_Corresponding_Discriminant (New_C, Empty);
18985 Set_Discriminal (New_C, Empty);
18986 Set_Is_Completely_Hidden (New_C);
18987
18988 -- Set the Original_Record_Component of each discriminant in the
18989 -- derived base to point to the corresponding stored that we just
18990 -- created.
18991
18992 Discrim := First_Discriminant (Derived_Base);
18993 while Present (Discrim) loop
18994 Corr_Discrim := Corresponding_Discriminant (Discrim);
18995
18996 -- Corr_Discrim could be missing in an error situation
18997
18998 if Present (Corr_Discrim)
18999 and then Original_Record_Component (Corr_Discrim) = Old_C
19000 then
19001 Set_Original_Record_Component (Discrim, New_C);
19002 Set_Corresponding_Record_Component (Discrim, Empty);
19003 end if;
19004
19005 Next_Discriminant (Discrim);
19006 end loop;
19007
19008 Append_Entity (New_C, Derived_Base);
19009 end if;
19010
19011 if not Is_Tagged then
19012 Append_Elmt (Old_C, Assoc_List);
19013 Append_Elmt (New_C, Assoc_List);
19014 end if;
19015 end Inherit_Component;
19016
19017 -- Variables local to Inherit_Component
19018
19019 Loc : constant Source_Ptr := Sloc (N);
19020
19021 Parent_Discrim : Entity_Id;
19022 Stored_Discrim : Entity_Id;
19023 D : Entity_Id;
19024 Component : Entity_Id;
19025
19026 -- Start of processing for Inherit_Components
19027
19028 begin
19029 if not Is_Tagged then
19030 Append_Elmt (Parent_Base, Assoc_List);
19031 Append_Elmt (Derived_Base, Assoc_List);
19032 end if;
19033
19034 -- Inherit parent discriminants if needed
19035
19036 if Inherit_Discr then
19037 Parent_Discrim := First_Discriminant (Parent_Base);
19038 while Present (Parent_Discrim) loop
19039 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19040 Next_Discriminant (Parent_Discrim);
19041 end loop;
19042 end if;
19043
19044 -- Create explicit stored discrims for untagged types when necessary
19045
19046 if not Has_Unknown_Discriminants (Derived_Base)
19047 and then Has_Discriminants (Parent_Base)
19048 and then not Is_Tagged
19049 and then
19050 (not Inherit_Discr
19051 or else First_Discriminant (Parent_Base) /=
19052 First_Stored_Discriminant (Parent_Base))
19053 then
19054 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19055 while Present (Stored_Discrim) loop
19056 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19057 Next_Stored_Discriminant (Stored_Discrim);
19058 end loop;
19059 end if;
19060
19061 -- See if we can apply the second transformation for derived types, as
19062 -- explained in point 6. in the comments above Build_Derived_Record_Type
19063 -- This is achieved by appending Derived_Base discriminants into Discs,
19064 -- which has the side effect of returning a non empty Discs list to the
19065 -- caller of Inherit_Components, which is what we want. This must be
19066 -- done for private derived types if there are explicit stored
19067 -- discriminants, to ensure that we can retrieve the values of the
19068 -- constraints provided in the ancestors.
19069
19070 if Inherit_Discr
19071 and then Is_Empty_Elmt_List (Discs)
19072 and then Present (First_Discriminant (Derived_Base))
19073 and then
19074 (not Is_Private_Type (Derived_Base)
19075 or else Is_Completely_Hidden
19076 (First_Stored_Discriminant (Derived_Base))
19077 or else Is_Generic_Type (Derived_Base))
19078 then
19079 D := First_Discriminant (Derived_Base);
19080 while Present (D) loop
19081 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19082 Next_Discriminant (D);
19083 end loop;
19084 end if;
19085
19086 -- Finally, inherit non-discriminant components unless they are not
19087 -- visible because defined or inherited from the full view of the
19088 -- parent. Don't inherit the _parent field of the parent type.
19089
19090 Component := First_Entity (Parent_Base);
19091 while Present (Component) loop
19092
19093 -- Ada 2005 (AI-251): Do not inherit components associated with
19094 -- secondary tags of the parent.
19095
19096 if Ekind (Component) = E_Component
19097 and then Present (Related_Type (Component))
19098 then
19099 null;
19100
19101 elsif Ekind (Component) /= E_Component
19102 or else Chars (Component) = Name_uParent
19103 then
19104 null;
19105
19106 -- If the derived type is within the parent type's declarative
19107 -- region, then the components can still be inherited even though
19108 -- they aren't visible at this point. This can occur for cases
19109 -- such as within public child units where the components must
19110 -- become visible upon entering the child unit's private part.
19111
19112 elsif not Is_Visible_Component (Component)
19113 and then not In_Open_Scopes (Scope (Parent_Base))
19114 then
19115 null;
19116
19117 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19118 then
19119 null;
19120
19121 else
19122 Inherit_Component (Component);
19123 end if;
19124
19125 Next_Entity (Component);
19126 end loop;
19127
19128 -- For tagged derived types, inherited discriminants cannot be used in
19129 -- component declarations of the record extension part. To achieve this
19130 -- we mark the inherited discriminants as not visible.
19131
19132 if Is_Tagged and then Inherit_Discr then
19133 D := First_Discriminant (Derived_Base);
19134 while Present (D) loop
19135 Set_Is_Immediately_Visible (D, False);
19136 Next_Discriminant (D);
19137 end loop;
19138 end if;
19139
19140 return Assoc_List;
19141 end Inherit_Components;
19142
19143 ----------------------
19144 -- Is_EVF_Procedure --
19145 ----------------------
19146
19147 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19148 Formal : Entity_Id;
19149
19150 begin
19151 -- Examine the formals of an Extensions_Visible False procedure looking
19152 -- for a controlling OUT parameter.
19153
19154 if Ekind (Subp) = E_Procedure
19155 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19156 then
19157 Formal := First_Formal (Subp);
19158 while Present (Formal) loop
19159 if Ekind (Formal) = E_Out_Parameter
19160 and then Is_Controlling_Formal (Formal)
19161 then
19162 return True;
19163 end if;
19164
19165 Next_Formal (Formal);
19166 end loop;
19167 end if;
19168
19169 return False;
19170 end Is_EVF_Procedure;
19171
19172 --------------------------
19173 -- Is_Private_Primitive --
19174 --------------------------
19175
19176 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19177 Prim_Scope : constant Entity_Id := Scope (Prim);
19178 Priv_Entity : Entity_Id;
19179 begin
19180 if Is_Package_Or_Generic_Package (Prim_Scope) then
19181 Priv_Entity := First_Private_Entity (Prim_Scope);
19182
19183 while Present (Priv_Entity) loop
19184 if Priv_Entity = Prim then
19185 return True;
19186 end if;
19187
19188 Next_Entity (Priv_Entity);
19189 end loop;
19190 end if;
19191
19192 return False;
19193 end Is_Private_Primitive;
19194
19195 ------------------------------
19196 -- Is_Valid_Constraint_Kind --
19197 ------------------------------
19198
19199 function Is_Valid_Constraint_Kind
19200 (T_Kind : Type_Kind;
19201 Constraint_Kind : Node_Kind) return Boolean
19202 is
19203 begin
19204 case T_Kind is
19205 when Enumeration_Kind
19206 | Integer_Kind
19207 =>
19208 return Constraint_Kind = N_Range_Constraint;
19209
19210 when Decimal_Fixed_Point_Kind =>
19211 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19212
19213 when Ordinary_Fixed_Point_Kind =>
19214 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19215
19216 when Float_Kind =>
19217 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19218
19219 when Access_Kind
19220 | Array_Kind
19221 | Class_Wide_Kind
19222 | Concurrent_Kind
19223 | Private_Kind
19224 | E_Incomplete_Type
19225 | E_Record_Subtype
19226 | E_Record_Type
19227 =>
19228 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19229
19230 when others =>
19231 return True; -- Error will be detected later
19232 end case;
19233 end Is_Valid_Constraint_Kind;
19234
19235 --------------------------
19236 -- Is_Visible_Component --
19237 --------------------------
19238
19239 function Is_Visible_Component
19240 (C : Entity_Id;
19241 N : Node_Id := Empty) return Boolean
19242 is
19243 Original_Comp : Entity_Id := Empty;
19244 Original_Type : Entity_Id;
19245 Type_Scope : Entity_Id;
19246
19247 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19248 -- Check whether parent type of inherited component is declared locally,
19249 -- possibly within a nested package or instance. The current scope is
19250 -- the derived record itself.
19251
19252 -------------------
19253 -- Is_Local_Type --
19254 -------------------
19255
19256 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19257 begin
19258 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19259 end Is_Local_Type;
19260
19261 -- Start of processing for Is_Visible_Component
19262
19263 begin
19264 if Ekind (C) in E_Component | E_Discriminant then
19265 Original_Comp := Original_Record_Component (C);
19266 end if;
19267
19268 if No (Original_Comp) then
19269
19270 -- Premature usage, or previous error
19271
19272 return False;
19273
19274 else
19275 Original_Type := Scope (Original_Comp);
19276 Type_Scope := Scope (Base_Type (Scope (C)));
19277 end if;
19278
19279 -- This test only concerns tagged types
19280
19281 if not Is_Tagged_Type (Original_Type) then
19282
19283 -- Check if this is a renamed discriminant (hidden either by the
19284 -- derived type or by some ancestor), unless we are analyzing code
19285 -- generated by the expander since it may reference such components
19286 -- (for example see the expansion of Deep_Adjust).
19287
19288 if Ekind (C) = E_Discriminant and then Present (N) then
19289 return
19290 not Comes_From_Source (N)
19291 or else not Is_Completely_Hidden (C);
19292 else
19293 return True;
19294 end if;
19295
19296 -- If it is _Parent or _Tag, there is no visibility issue
19297
19298 elsif not Comes_From_Source (Original_Comp) then
19299 return True;
19300
19301 -- Discriminants are visible unless the (private) type has unknown
19302 -- discriminants. If the discriminant reference is inserted for a
19303 -- discriminant check on a full view it is also visible.
19304
19305 elsif Ekind (Original_Comp) = E_Discriminant
19306 and then
19307 (not Has_Unknown_Discriminants (Original_Type)
19308 or else (Present (N)
19309 and then Nkind (N) = N_Selected_Component
19310 and then Nkind (Prefix (N)) = N_Type_Conversion
19311 and then not Comes_From_Source (Prefix (N))))
19312 then
19313 return True;
19314
19315 -- If the component has been declared in an ancestor which is currently
19316 -- a private type, then it is not visible. The same applies if the
19317 -- component's containing type is not in an open scope and the original
19318 -- component's enclosing type is a visible full view of a private type
19319 -- (which can occur in cases where an attempt is being made to reference
19320 -- a component in a sibling package that is inherited from a visible
19321 -- component of a type in an ancestor package; the component in the
19322 -- sibling package should not be visible even though the component it
19323 -- inherited from is visible), but instance bodies are not subject to
19324 -- this second case since they have the Has_Private_View mechanism to
19325 -- ensure proper visibility. This does not apply however in the case
19326 -- where the scope of the type is a private child unit, or when the
19327 -- parent comes from a local package in which the ancestor is currently
19328 -- visible. The latter suppression of visibility is needed for cases
19329 -- that are tested in B730006.
19330
19331 elsif Is_Private_Type (Original_Type)
19332 or else
19333 (not Is_Private_Descendant (Type_Scope)
19334 and then not In_Open_Scopes (Type_Scope)
19335 and then Has_Private_Declaration (Original_Type)
19336 and then not In_Instance_Body)
19337 then
19338 -- If the type derives from an entity in a formal package, there
19339 -- are no additional visible components.
19340
19341 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19342 N_Formal_Package_Declaration
19343 then
19344 return False;
19345
19346 -- if we are not in the private part of the current package, there
19347 -- are no additional visible components.
19348
19349 elsif Ekind (Scope (Current_Scope)) = E_Package
19350 and then not In_Private_Part (Scope (Current_Scope))
19351 then
19352 return False;
19353 else
19354 return
19355 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19356 and then In_Open_Scopes (Scope (Original_Type))
19357 and then Is_Local_Type (Type_Scope);
19358 end if;
19359
19360 -- There is another weird way in which a component may be invisible when
19361 -- the private and the full view are not derived from the same ancestor.
19362 -- Here is an example :
19363
19364 -- type A1 is tagged record F1 : integer; end record;
19365 -- type A2 is new A1 with record F2 : integer; end record;
19366 -- type T is new A1 with private;
19367 -- private
19368 -- type T is new A2 with null record;
19369
19370 -- In this case, the full view of T inherits F1 and F2 but the private
19371 -- view inherits only F1
19372
19373 else
19374 declare
19375 Ancestor : Entity_Id := Scope (C);
19376
19377 begin
19378 loop
19379 if Ancestor = Original_Type then
19380 return True;
19381
19382 -- The ancestor may have a partial view of the original type,
19383 -- but if the full view is in scope, as in a child body, the
19384 -- component is visible.
19385
19386 elsif In_Private_Part (Scope (Original_Type))
19387 and then Full_View (Ancestor) = Original_Type
19388 then
19389 return True;
19390
19391 elsif Ancestor = Etype (Ancestor) then
19392
19393 -- No further ancestors to examine
19394
19395 return False;
19396 end if;
19397
19398 Ancestor := Etype (Ancestor);
19399 end loop;
19400 end;
19401 end if;
19402 end Is_Visible_Component;
19403
19404 --------------------------
19405 -- Make_Class_Wide_Type --
19406 --------------------------
19407
19408 procedure Make_Class_Wide_Type (T : Entity_Id) is
19409 CW_Type : Entity_Id;
19410 CW_Name : Name_Id;
19411 Next_E : Entity_Id;
19412 Prev_E : Entity_Id;
19413
19414 begin
19415 if Present (Class_Wide_Type (T)) then
19416
19417 -- The class-wide type is a partially decorated entity created for a
19418 -- unanalyzed tagged type referenced through a limited with clause.
19419 -- When the tagged type is analyzed, its class-wide type needs to be
19420 -- redecorated. Note that we reuse the entity created by Decorate_
19421 -- Tagged_Type in order to preserve all links.
19422
19423 if Materialize_Entity (Class_Wide_Type (T)) then
19424 CW_Type := Class_Wide_Type (T);
19425 Set_Materialize_Entity (CW_Type, False);
19426
19427 -- The class wide type can have been defined by the partial view, in
19428 -- which case everything is already done.
19429
19430 else
19431 return;
19432 end if;
19433
19434 -- Default case, we need to create a new class-wide type
19435
19436 else
19437 CW_Type :=
19438 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19439 end if;
19440
19441 -- Inherit root type characteristics
19442
19443 CW_Name := Chars (CW_Type);
19444 Next_E := Next_Entity (CW_Type);
19445 Prev_E := Prev_Entity (CW_Type);
19446 Copy_Node (T, CW_Type);
19447 Set_Comes_From_Source (CW_Type, False);
19448 Set_Chars (CW_Type, CW_Name);
19449 Set_Parent (CW_Type, Parent (T));
19450 Set_Prev_Entity (CW_Type, Prev_E);
19451 Set_Next_Entity (CW_Type, Next_E);
19452
19453 -- Ensure we have a new freeze node for the class-wide type. The partial
19454 -- view may have freeze action of its own, requiring a proper freeze
19455 -- node, and the same freeze node cannot be shared between the two
19456 -- types.
19457
19458 Set_Has_Delayed_Freeze (CW_Type);
19459 Set_Freeze_Node (CW_Type, Empty);
19460
19461 -- Customize the class-wide type: It has no prim. op., it cannot be
19462 -- abstract, its Etype points back to the specific root type, and it
19463 -- cannot have any invariants.
19464
19465 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19466 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19467
19468 elsif Ekind (CW_Type) in Concurrent_Kind then
19469 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19470 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19471
19472 if Ekind (CW_Type) in Task_Kind then
19473 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19474 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19475 end if;
19476
19477 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19478 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19479 end if;
19480 end if;
19481
19482 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19483 Set_Is_Tagged_Type (CW_Type, True);
19484 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19485 Set_Is_Abstract_Type (CW_Type, False);
19486 Set_Is_Constrained (CW_Type, False);
19487 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19488 Set_Default_SSO (CW_Type);
19489 Set_Has_Inheritable_Invariants (CW_Type, False);
19490 Set_Has_Inherited_Invariants (CW_Type, False);
19491 Set_Has_Own_Invariants (CW_Type, False);
19492
19493 if Ekind (T) = E_Class_Wide_Subtype then
19494 Set_Etype (CW_Type, Etype (Base_Type (T)));
19495 else
19496 Set_Etype (CW_Type, T);
19497 end if;
19498
19499 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19500
19501 -- If this is the class_wide type of a constrained subtype, it does
19502 -- not have discriminants.
19503
19504 Set_Has_Discriminants (CW_Type,
19505 Has_Discriminants (T) and then not Is_Constrained (T));
19506
19507 Set_Has_Unknown_Discriminants (CW_Type, True);
19508 Set_Class_Wide_Type (T, CW_Type);
19509 Set_Equivalent_Type (CW_Type, Empty);
19510
19511 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19512
19513 Set_Class_Wide_Type (CW_Type, CW_Type);
19514 end Make_Class_Wide_Type;
19515
19516 ----------------
19517 -- Make_Index --
19518 ----------------
19519
19520 procedure Make_Index
19521 (N : Node_Id;
19522 Related_Nod : Node_Id;
19523 Related_Id : Entity_Id := Empty;
19524 Suffix_Index : Pos := 1)
19525 is
19526 R : Node_Id;
19527 T : Entity_Id;
19528 Def_Id : Entity_Id := Empty;
19529 Found : Boolean := False;
19530
19531 begin
19532 -- For a discrete range used in a constrained array definition and
19533 -- defined by a range, an implicit conversion to the predefined type
19534 -- INTEGER is assumed if each bound is either a numeric literal, a named
19535 -- number, or an attribute, and the type of both bounds (prior to the
19536 -- implicit conversion) is the type universal_integer. Otherwise, both
19537 -- bounds must be of the same discrete type, other than universal
19538 -- integer; this type must be determinable independently of the
19539 -- context, but using the fact that the type must be discrete and that
19540 -- both bounds must have the same type.
19541
19542 -- Character literals also have a universal type in the absence of
19543 -- of additional context, and are resolved to Standard_Character.
19544
19545 if Nkind (N) = N_Range then
19546
19547 -- The index is given by a range constraint. The bounds are known
19548 -- to be of a consistent type.
19549
19550 if not Is_Overloaded (N) then
19551 T := Etype (N);
19552
19553 -- For universal bounds, choose the specific predefined type
19554
19555 if T = Universal_Integer then
19556 T := Standard_Integer;
19557
19558 elsif T = Any_Character then
19559 Ambiguous_Character (Low_Bound (N));
19560
19561 T := Standard_Character;
19562 end if;
19563
19564 -- The node may be overloaded because some user-defined operators
19565 -- are available, but if a universal interpretation exists it is
19566 -- also the selected one.
19567
19568 elsif Universal_Interpretation (N) = Universal_Integer then
19569 T := Standard_Integer;
19570
19571 else
19572 T := Any_Type;
19573
19574 declare
19575 Ind : Interp_Index;
19576 It : Interp;
19577
19578 begin
19579 Get_First_Interp (N, Ind, It);
19580 while Present (It.Typ) loop
19581 if Is_Discrete_Type (It.Typ) then
19582
19583 if Found
19584 and then not Covers (It.Typ, T)
19585 and then not Covers (T, It.Typ)
19586 then
19587 Error_Msg_N ("ambiguous bounds in discrete range", N);
19588 exit;
19589 else
19590 T := It.Typ;
19591 Found := True;
19592 end if;
19593 end if;
19594
19595 Get_Next_Interp (Ind, It);
19596 end loop;
19597
19598 if T = Any_Type then
19599 Error_Msg_N ("discrete type required for range", N);
19600 Set_Etype (N, Any_Type);
19601 return;
19602
19603 elsif T = Universal_Integer then
19604 T := Standard_Integer;
19605 end if;
19606 end;
19607 end if;
19608
19609 if not Is_Discrete_Type (T) then
19610 Error_Msg_N ("discrete type required for range", N);
19611 Set_Etype (N, Any_Type);
19612 return;
19613 end if;
19614
19615 -- If the range bounds are "T'Low .. T'High" where T is a name of
19616 -- a discrete type, then use T as the type of the index.
19617
19618 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19619 and then Attribute_Name (Low_Bound (N)) = Name_First
19620 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19621 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19622
19623 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19624 and then Attribute_Name (High_Bound (N)) = Name_Last
19625 and then Is_Entity_Name (Prefix (High_Bound (N)))
19626 and then Entity (Prefix (High_Bound (N))) = Def_Id
19627 then
19628 Def_Id := Entity (Prefix (Low_Bound (N)));
19629 end if;
19630
19631 R := N;
19632 Process_Range_Expr_In_Decl (R, T);
19633
19634 elsif Nkind (N) = N_Subtype_Indication then
19635
19636 -- The index is given by a subtype with a range constraint
19637
19638 T := Base_Type (Entity (Subtype_Mark (N)));
19639
19640 if not Is_Discrete_Type (T) then
19641 Error_Msg_N ("discrete type required for range", N);
19642 Set_Etype (N, Any_Type);
19643 return;
19644 end if;
19645
19646 R := Range_Expression (Constraint (N));
19647
19648 Resolve (R, T);
19649 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19650
19651 elsif Nkind (N) = N_Attribute_Reference then
19652
19653 -- Catch beginner's error (use of attribute other than 'Range)
19654
19655 if Attribute_Name (N) /= Name_Range then
19656 Error_Msg_N ("expect attribute ''Range", N);
19657 Set_Etype (N, Any_Type);
19658 return;
19659 end if;
19660
19661 -- If the node denotes the range of a type mark, that is also the
19662 -- resulting type, and we do not need to create an Itype for it.
19663
19664 if Is_Entity_Name (Prefix (N))
19665 and then Comes_From_Source (N)
19666 and then Is_Discrete_Type (Entity (Prefix (N)))
19667 then
19668 Def_Id := Entity (Prefix (N));
19669 end if;
19670
19671 Analyze_And_Resolve (N);
19672 T := Etype (N);
19673 R := N;
19674
19675 -- If none of the above, must be a subtype. We convert this to a
19676 -- range attribute reference because in the case of declared first
19677 -- named subtypes, the types in the range reference can be different
19678 -- from the type of the entity. A range attribute normalizes the
19679 -- reference and obtains the correct types for the bounds.
19680
19681 -- This transformation is in the nature of an expansion, is only
19682 -- done if expansion is active. In particular, it is not done on
19683 -- formal generic types, because we need to retain the name of the
19684 -- original index for instantiation purposes.
19685
19686 else
19687 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19688 Error_Msg_N ("invalid subtype mark in discrete range", N);
19689 Set_Etype (N, Any_Integer);
19690 return;
19691
19692 else
19693 -- The type mark may be that of an incomplete type. It is only
19694 -- now that we can get the full view, previous analysis does
19695 -- not look specifically for a type mark.
19696
19697 Set_Entity (N, Get_Full_View (Entity (N)));
19698 Set_Etype (N, Entity (N));
19699 Def_Id := Entity (N);
19700
19701 if not Is_Discrete_Type (Def_Id) then
19702 Error_Msg_N ("discrete type required for index", N);
19703 Set_Etype (N, Any_Type);
19704 return;
19705 end if;
19706 end if;
19707
19708 if Expander_Active then
19709 Rewrite (N,
19710 Make_Attribute_Reference (Sloc (N),
19711 Attribute_Name => Name_Range,
19712 Prefix => Relocate_Node (N)));
19713
19714 -- The original was a subtype mark that does not freeze. This
19715 -- means that the rewritten version must not freeze either.
19716
19717 Set_Must_Not_Freeze (N);
19718 Set_Must_Not_Freeze (Prefix (N));
19719 Analyze_And_Resolve (N);
19720 T := Etype (N);
19721 R := N;
19722
19723 -- If expander is inactive, type is legal, nothing else to construct
19724
19725 else
19726 return;
19727 end if;
19728 end if;
19729
19730 if not Is_Discrete_Type (T) then
19731 Error_Msg_N ("discrete type required for range", N);
19732 Set_Etype (N, Any_Type);
19733 return;
19734
19735 elsif T = Any_Type then
19736 Set_Etype (N, Any_Type);
19737 return;
19738 end if;
19739
19740 -- We will now create the appropriate Itype to describe the range, but
19741 -- first a check. If we originally had a subtype, then we just label
19742 -- the range with this subtype. Not only is there no need to construct
19743 -- a new subtype, but it is wrong to do so for two reasons:
19744
19745 -- 1. A legality concern, if we have a subtype, it must not freeze,
19746 -- and the Itype would cause freezing incorrectly
19747
19748 -- 2. An efficiency concern, if we created an Itype, it would not be
19749 -- recognized as the same type for the purposes of eliminating
19750 -- checks in some circumstances.
19751
19752 -- We signal this case by setting the subtype entity in Def_Id
19753
19754 if No (Def_Id) then
19755 Def_Id :=
19756 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19757 Set_Etype (Def_Id, Base_Type (T));
19758
19759 if Is_Signed_Integer_Type (T) then
19760 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
19761
19762 elsif Is_Modular_Integer_Type (T) then
19763 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
19764
19765 else
19766 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
19767 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19768 Set_First_Literal (Def_Id, First_Literal (T));
19769 end if;
19770
19771 Set_Size_Info (Def_Id, (T));
19772 Set_RM_Size (Def_Id, RM_Size (T));
19773 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19774
19775 Set_Scalar_Range (Def_Id, R);
19776 Conditional_Delay (Def_Id, T);
19777
19778 -- In the subtype indication case inherit properties of the parent
19779
19780 if Nkind (N) = N_Subtype_Indication then
19781
19782 -- It is enough to inherit predicate flags and not the predicate
19783 -- functions, because predicates on an index type are illegal
19784 -- anyway and the flags are enough to detect them.
19785
19786 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19787
19788 -- If the immediate parent of the new subtype is nonstatic, then
19789 -- the subtype we create is nonstatic as well, even if its bounds
19790 -- are static.
19791
19792 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
19793 Set_Is_Non_Static_Subtype (Def_Id);
19794 end if;
19795 end if;
19796 end if;
19797
19798 -- Final step is to label the index with this constructed type
19799
19800 Set_Etype (N, Def_Id);
19801 end Make_Index;
19802
19803 ------------------------------
19804 -- Modular_Type_Declaration --
19805 ------------------------------
19806
19807 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19808 Mod_Expr : constant Node_Id := Expression (Def);
19809 M_Val : Uint;
19810
19811 procedure Set_Modular_Size (Bits : Int);
19812 -- Sets RM_Size to Bits, and Esize to normal word size above this
19813
19814 ----------------------
19815 -- Set_Modular_Size --
19816 ----------------------
19817
19818 procedure Set_Modular_Size (Bits : Int) is
19819 Siz : Int;
19820
19821 begin
19822 Set_RM_Size (T, UI_From_Int (Bits));
19823
19824 if Bits < System_Max_Binary_Modulus_Power then
19825 Siz := 8;
19826
19827 while Siz < 128 loop
19828 exit when Bits <= Siz;
19829 Siz := Siz * 2;
19830 end loop;
19831
19832 Set_Esize (T, UI_From_Int (Siz));
19833
19834 else
19835 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
19836 end if;
19837
19838 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19839 Set_Is_Known_Valid (T);
19840 end if;
19841 end Set_Modular_Size;
19842
19843 -- Start of processing for Modular_Type_Declaration
19844
19845 begin
19846 -- If the mod expression is (exactly) 2 * literal, where literal is
19847 -- 128 or less, then almost certainly the * was meant to be **. Warn.
19848
19849 if Warn_On_Suspicious_Modulus_Value
19850 and then Nkind (Mod_Expr) = N_Op_Multiply
19851 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19852 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19853 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19854 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
19855 then
19856 Error_Msg_N
19857 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19858 end if;
19859
19860 -- Proceed with analysis of mod expression
19861
19862 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19863
19864 if Ekind (T) in Incomplete_Or_Private_Kind then
19865 Reinit_Field_To_Zero (T, F_Stored_Constraint);
19866 end if;
19867
19868 Set_Etype (T, T);
19869 Mutate_Ekind (T, E_Modular_Integer_Type);
19870 Reinit_Alignment (T);
19871 Set_Is_Constrained (T);
19872
19873 if not Is_OK_Static_Expression (Mod_Expr) then
19874 Flag_Non_Static_Expr
19875 ("non-static expression used for modular type bound!", Mod_Expr);
19876 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19877 else
19878 M_Val := Expr_Value (Mod_Expr);
19879 end if;
19880
19881 if M_Val < 1 then
19882 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19883 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19884 end if;
19885
19886 if M_Val > 2 ** Standard_Long_Integer_Size then
19887 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19888 end if;
19889
19890 Set_Modulus (T, M_Val);
19891
19892 -- Create bounds for the modular type based on the modulus given in
19893 -- the type declaration and then analyze and resolve those bounds.
19894
19895 Set_Scalar_Range (T,
19896 Make_Range (Sloc (Mod_Expr),
19897 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19898 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19899
19900 -- Properly analyze the literals for the range. We do this manually
19901 -- because we can't go calling Resolve, since we are resolving these
19902 -- bounds with the type, and this type is certainly not complete yet.
19903
19904 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19905 Set_Etype (High_Bound (Scalar_Range (T)), T);
19906 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19907 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19908
19909 -- Loop through powers of two to find number of bits required
19910
19911 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19912
19913 -- Binary case
19914
19915 if M_Val = 2 ** Bits then
19916 Set_Modular_Size (Bits);
19917 return;
19918
19919 -- Nonbinary case
19920
19921 elsif M_Val < 2 ** Bits then
19922 Set_Non_Binary_Modulus (T);
19923
19924 if Bits > System_Max_Nonbinary_Modulus_Power then
19925 Error_Msg_Uint_1 :=
19926 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19927 Error_Msg_F
19928 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19929 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19930 return;
19931
19932 else
19933 -- In the nonbinary case, set size as per RM 13.3(55)
19934
19935 Set_Modular_Size (Bits);
19936 return;
19937 end if;
19938 end if;
19939
19940 end loop;
19941
19942 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19943 -- so we just signal an error and set the maximum size.
19944
19945 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19946 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19947
19948 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19949 Reinit_Alignment (T);
19950
19951 end Modular_Type_Declaration;
19952
19953 --------------------------
19954 -- New_Concatenation_Op --
19955 --------------------------
19956
19957 procedure New_Concatenation_Op (Typ : Entity_Id) is
19958 Loc : constant Source_Ptr := Sloc (Typ);
19959 Op : Entity_Id;
19960
19961 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19962 -- Create abbreviated declaration for the formal of a predefined
19963 -- Operator 'Op' of type 'Typ'
19964
19965 --------------------
19966 -- Make_Op_Formal --
19967 --------------------
19968
19969 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19970 Formal : Entity_Id;
19971 begin
19972 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19973 Set_Etype (Formal, Typ);
19974 Set_Mechanism (Formal, Default_Mechanism);
19975 return Formal;
19976 end Make_Op_Formal;
19977
19978 -- Start of processing for New_Concatenation_Op
19979
19980 begin
19981 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19982
19983 Mutate_Ekind (Op, E_Operator);
19984 Set_Scope (Op, Current_Scope);
19985 Set_Etype (Op, Typ);
19986 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19987 Set_Is_Immediately_Visible (Op);
19988 Set_Is_Intrinsic_Subprogram (Op);
19989 Set_Has_Completion (Op);
19990 Append_Entity (Op, Current_Scope);
19991
19992 Set_Name_Entity_Id (Name_Op_Concat, Op);
19993
19994 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19995 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19996 end New_Concatenation_Op;
19997
19998 -------------------------
19999 -- OK_For_Limited_Init --
20000 -------------------------
20001
20002 -- ???Check all calls of this, and compare the conditions under which it's
20003 -- called.
20004
20005 function OK_For_Limited_Init
20006 (Typ : Entity_Id;
20007 Exp : Node_Id) return Boolean
20008 is
20009 begin
20010 return Is_CPP_Constructor_Call (Exp)
20011 or else (Ada_Version >= Ada_2005
20012 and then not Debug_Flag_Dot_L
20013 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20014 end OK_For_Limited_Init;
20015
20016 -------------------------------
20017 -- OK_For_Limited_Init_In_05 --
20018 -------------------------------
20019
20020 function OK_For_Limited_Init_In_05
20021 (Typ : Entity_Id;
20022 Exp : Node_Id) return Boolean
20023 is
20024 begin
20025 -- An object of a limited interface type can be initialized with any
20026 -- expression of a nonlimited descendant type. However this does not
20027 -- apply if this is a view conversion of some other expression. This
20028 -- is checked below.
20029
20030 if Is_Class_Wide_Type (Typ)
20031 and then Is_Limited_Interface (Typ)
20032 and then not Is_Limited_Type (Etype (Exp))
20033 and then Nkind (Exp) /= N_Type_Conversion
20034 then
20035 return True;
20036 end if;
20037
20038 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20039 -- case of limited aggregates (including extension aggregates), and
20040 -- function calls. The function call may have been given in prefixed
20041 -- notation, in which case the original node is an indexed component.
20042 -- If the function is parameterless, the original node was an explicit
20043 -- dereference. The function may also be parameterless, in which case
20044 -- the source node is just an identifier.
20045
20046 -- A branch of a conditional expression may have been removed if the
20047 -- condition is statically known. This happens during expansion, and
20048 -- thus will not happen if previous errors were encountered. The check
20049 -- will have been performed on the chosen branch, which replaces the
20050 -- original conditional expression.
20051
20052 if No (Exp) then
20053 return True;
20054 end if;
20055
20056 case Nkind (Original_Node (Exp)) is
20057 when N_Aggregate
20058 | N_Extension_Aggregate
20059 | N_Function_Call
20060 | N_Op
20061 =>
20062 return True;
20063
20064 when N_Identifier =>
20065 return Present (Entity (Original_Node (Exp)))
20066 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20067
20068 when N_Qualified_Expression =>
20069 return
20070 OK_For_Limited_Init_In_05
20071 (Typ, Expression (Original_Node (Exp)));
20072
20073 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20074 -- with a function call, the expander has rewritten the call into an
20075 -- N_Type_Conversion node to force displacement of the pointer to
20076 -- reference the component containing the secondary dispatch table.
20077 -- Otherwise a type conversion is not a legal context.
20078 -- A return statement for a build-in-place function returning a
20079 -- synchronized type also introduces an unchecked conversion.
20080
20081 when N_Type_Conversion
20082 | N_Unchecked_Type_Conversion
20083 =>
20084 return not Comes_From_Source (Exp)
20085 and then
20086 -- If the conversion has been rewritten, check Original_Node
20087
20088 ((Original_Node (Exp) /= Exp
20089 and then
20090 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
20091
20092 -- Otherwise, check the expression of the compiler-generated
20093 -- conversion (which is a conversion that we want to ignore
20094 -- for purposes of the limited-initialization restrictions).
20095
20096 or else
20097 (Original_Node (Exp) = Exp
20098 and then
20099 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
20100
20101 when N_Explicit_Dereference
20102 | N_Indexed_Component
20103 | N_Selected_Component
20104 =>
20105 return Nkind (Exp) = N_Function_Call;
20106
20107 -- A use of 'Input is a function call, hence allowed. Normally the
20108 -- attribute will be changed to a call, but the attribute by itself
20109 -- can occur with -gnatc.
20110
20111 when N_Attribute_Reference =>
20112 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20113
20114 -- "return raise ..." is OK
20115
20116 when N_Raise_Expression =>
20117 return True;
20118
20119 -- For a case expression, all dependent expressions must be legal
20120
20121 when N_Case_Expression =>
20122 declare
20123 Alt : Node_Id;
20124
20125 begin
20126 Alt := First (Alternatives (Original_Node (Exp)));
20127 while Present (Alt) loop
20128 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20129 return False;
20130 end if;
20131
20132 Next (Alt);
20133 end loop;
20134
20135 return True;
20136 end;
20137
20138 -- For an if expression, all dependent expressions must be legal
20139
20140 when N_If_Expression =>
20141 declare
20142 Then_Expr : constant Node_Id :=
20143 Next (First (Expressions (Original_Node (Exp))));
20144 Else_Expr : constant Node_Id := Next (Then_Expr);
20145 begin
20146 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20147 and then
20148 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20149 end;
20150
20151 when others =>
20152 return False;
20153 end case;
20154 end OK_For_Limited_Init_In_05;
20155
20156 -------------------------------------------
20157 -- Ordinary_Fixed_Point_Type_Declaration --
20158 -------------------------------------------
20159
20160 procedure Ordinary_Fixed_Point_Type_Declaration
20161 (T : Entity_Id;
20162 Def : Node_Id)
20163 is
20164 Loc : constant Source_Ptr := Sloc (Def);
20165 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20166 RRS : constant Node_Id := Real_Range_Specification (Def);
20167 Implicit_Base : Entity_Id;
20168 Delta_Val : Ureal;
20169 Small_Val : Ureal;
20170 Low_Val : Ureal;
20171 High_Val : Ureal;
20172
20173 begin
20174 Check_Restriction (No_Fixed_Point, Def);
20175
20176 -- Create implicit base type
20177
20178 Implicit_Base :=
20179 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20180 Set_Etype (Implicit_Base, Implicit_Base);
20181
20182 -- Analyze and process delta expression
20183
20184 Analyze_And_Resolve (Delta_Expr, Any_Real);
20185
20186 Check_Delta_Expression (Delta_Expr);
20187 Delta_Val := Expr_Value_R (Delta_Expr);
20188
20189 Set_Delta_Value (Implicit_Base, Delta_Val);
20190
20191 -- Compute default small from given delta, which is the largest power
20192 -- of two that does not exceed the given delta value.
20193
20194 declare
20195 Tmp : Ureal;
20196 Scale : Int;
20197
20198 begin
20199 Tmp := Ureal_1;
20200 Scale := 0;
20201
20202 if Delta_Val < Ureal_1 then
20203 while Delta_Val < Tmp loop
20204 Tmp := Tmp / Ureal_2;
20205 Scale := Scale + 1;
20206 end loop;
20207
20208 else
20209 loop
20210 Tmp := Tmp * Ureal_2;
20211 exit when Tmp > Delta_Val;
20212 Scale := Scale - 1;
20213 end loop;
20214 end if;
20215
20216 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20217 end;
20218
20219 Set_Small_Value (Implicit_Base, Small_Val);
20220
20221 -- If no range was given, set a dummy range
20222
20223 if RRS <= Empty_Or_Error then
20224 Low_Val := -Small_Val;
20225 High_Val := Small_Val;
20226
20227 -- Otherwise analyze and process given range
20228
20229 else
20230 declare
20231 Low : constant Node_Id := Low_Bound (RRS);
20232 High : constant Node_Id := High_Bound (RRS);
20233
20234 begin
20235 Analyze_And_Resolve (Low, Any_Real);
20236 Analyze_And_Resolve (High, Any_Real);
20237 Check_Real_Bound (Low);
20238 Check_Real_Bound (High);
20239
20240 -- Obtain and set the range
20241
20242 Low_Val := Expr_Value_R (Low);
20243 High_Val := Expr_Value_R (High);
20244
20245 if Low_Val > High_Val then
20246 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20247 end if;
20248 end;
20249 end if;
20250
20251 -- The range for both the implicit base and the declared first subtype
20252 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20253 -- set a temporary range in place. Note that the bounds of the base
20254 -- type will be widened to be symmetrical and to fill the available
20255 -- bits when the type is frozen.
20256
20257 -- We could do this with all discrete types, and probably should, but
20258 -- we absolutely have to do it for fixed-point, since the end-points
20259 -- of the range and the size are determined by the small value, which
20260 -- could be reset before the freeze point.
20261
20262 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20263 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20264
20265 -- Complete definition of first subtype. The inheritance of the rep item
20266 -- chain ensures that SPARK-related pragmas are not clobbered when the
20267 -- ordinary fixed point type acts as a full view of a private type.
20268
20269 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20270 Set_Etype (T, Implicit_Base);
20271 Reinit_Size_Align (T);
20272 Inherit_Rep_Item_Chain (T, Implicit_Base);
20273 Set_Small_Value (T, Small_Val);
20274 Set_Delta_Value (T, Delta_Val);
20275 Set_Is_Constrained (T);
20276 end Ordinary_Fixed_Point_Type_Declaration;
20277
20278 ----------------------------------
20279 -- Preanalyze_Assert_Expression --
20280 ----------------------------------
20281
20282 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20283 begin
20284 In_Assertion_Expr := In_Assertion_Expr + 1;
20285 Preanalyze_Spec_Expression (N, T);
20286 In_Assertion_Expr := In_Assertion_Expr - 1;
20287 end Preanalyze_Assert_Expression;
20288
20289 -----------------------------------
20290 -- Preanalyze_Default_Expression --
20291 -----------------------------------
20292
20293 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20294 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20295 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20296
20297 begin
20298 In_Default_Expr := True;
20299 In_Spec_Expression := True;
20300
20301 Preanalyze_With_Freezing_And_Resolve (N, T);
20302
20303 In_Default_Expr := Save_In_Default_Expr;
20304 In_Spec_Expression := Save_In_Spec_Expression;
20305 end Preanalyze_Default_Expression;
20306
20307 --------------------------------
20308 -- Preanalyze_Spec_Expression --
20309 --------------------------------
20310
20311 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20312 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20313 begin
20314 In_Spec_Expression := True;
20315 Preanalyze_And_Resolve (N, T);
20316 In_Spec_Expression := Save_In_Spec_Expression;
20317 end Preanalyze_Spec_Expression;
20318
20319 ----------------------------------------
20320 -- Prepare_Private_Subtype_Completion --
20321 ----------------------------------------
20322
20323 procedure Prepare_Private_Subtype_Completion
20324 (Id : Entity_Id;
20325 Related_Nod : Node_Id)
20326 is
20327 Id_B : constant Entity_Id := Base_Type (Id);
20328 Full_B : constant Entity_Id := Full_View (Id_B);
20329 Full : Entity_Id;
20330
20331 begin
20332 if Present (Full_B) then
20333
20334 -- The Base_Type is already completed, we can complete the subtype
20335 -- now. We have to create a new entity with the same name, Thus we
20336 -- can't use Create_Itype.
20337
20338 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20339 Set_Is_Itype (Full);
20340 Set_Associated_Node_For_Itype (Full, Related_Nod);
20341 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20342 Set_Full_View (Id, Full);
20343 end if;
20344
20345 -- The parent subtype may be private, but the base might not, in some
20346 -- nested instances. In that case, the subtype does not need to be
20347 -- exchanged. It would still be nice to make private subtypes and their
20348 -- bases consistent at all times ???
20349
20350 if Is_Private_Type (Id_B) then
20351 Append_Elmt (Id, Private_Dependents (Id_B));
20352 end if;
20353 end Prepare_Private_Subtype_Completion;
20354
20355 ---------------------------
20356 -- Process_Discriminants --
20357 ---------------------------
20358
20359 procedure Process_Discriminants
20360 (N : Node_Id;
20361 Prev : Entity_Id := Empty)
20362 is
20363 Elist : constant Elist_Id := New_Elmt_List;
20364 Id : Node_Id;
20365 Discr : Node_Id;
20366 Discr_Number : Uint;
20367 Discr_Type : Entity_Id;
20368 Default_Present : Boolean := False;
20369 Default_Not_Present : Boolean := False;
20370
20371 begin
20372 -- A composite type other than an array type can have discriminants.
20373 -- On entry, the current scope is the composite type.
20374
20375 -- The discriminants are initially entered into the scope of the type
20376 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20377 -- use, as explained at the end of this procedure.
20378
20379 Discr := First (Discriminant_Specifications (N));
20380 while Present (Discr) loop
20381 Enter_Name (Defining_Identifier (Discr));
20382
20383 -- For navigation purposes we add a reference to the discriminant
20384 -- in the entity for the type. If the current declaration is a
20385 -- completion, place references on the partial view. Otherwise the
20386 -- type is the current scope.
20387
20388 if Present (Prev) then
20389
20390 -- The references go on the partial view, if present. If the
20391 -- partial view has discriminants, the references have been
20392 -- generated already.
20393
20394 if not Has_Discriminants (Prev) then
20395 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20396 end if;
20397 else
20398 Generate_Reference
20399 (Current_Scope, Defining_Identifier (Discr), 'd');
20400 end if;
20401
20402 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20403 Check_Anonymous_Access_Component
20404 (Typ_Decl => N,
20405 Typ => Defining_Identifier (N),
20406 Prev => Prev,
20407 Comp_Def => Discr,
20408 Access_Def => Discriminant_Type (Discr));
20409
20410 -- if Check_Anonymous_Access_Component replaced Discr then
20411 -- its Original_Node points to the old Discr and the access type
20412 -- for Discr_Type has already been created.
20413
20414 if Original_Node (Discr) /= Discr then
20415 Discr_Type := Etype (Discriminant_Type (Discr));
20416 else
20417 Discr_Type :=
20418 Access_Definition (Discr, Discriminant_Type (Discr));
20419
20420 -- Ada 2005 (AI-254)
20421
20422 if Present (Access_To_Subprogram_Definition
20423 (Discriminant_Type (Discr)))
20424 and then Protected_Present (Access_To_Subprogram_Definition
20425 (Discriminant_Type (Discr)))
20426 then
20427 Discr_Type :=
20428 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20429 end if;
20430 end if;
20431 else
20432 Find_Type (Discriminant_Type (Discr));
20433 Discr_Type := Etype (Discriminant_Type (Discr));
20434
20435 if Error_Posted (Discriminant_Type (Discr)) then
20436 Discr_Type := Any_Type;
20437 end if;
20438 end if;
20439
20440 -- Handling of discriminants that are access types
20441
20442 if Is_Access_Type (Discr_Type) then
20443
20444 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20445 -- limited record types
20446
20447 if Ada_Version < Ada_2005 then
20448 Check_Access_Discriminant_Requires_Limited
20449 (Discr, Discriminant_Type (Discr));
20450 end if;
20451
20452 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20453 Error_Msg_N
20454 ("(Ada 83) access discriminant not allowed", Discr);
20455 end if;
20456
20457 -- If not access type, must be a discrete type
20458
20459 elsif not Is_Discrete_Type (Discr_Type) then
20460 Error_Msg_N
20461 ("discriminants must have a discrete or access type",
20462 Discriminant_Type (Discr));
20463 end if;
20464
20465 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20466
20467 -- If a discriminant specification includes the assignment compound
20468 -- delimiter followed by an expression, the expression is the default
20469 -- expression of the discriminant; the default expression must be of
20470 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20471 -- a default expression, we do the special preanalysis, since this
20472 -- expression does not freeze (see section "Handling of Default and
20473 -- Per-Object Expressions" in spec of package Sem).
20474
20475 if Present (Expression (Discr)) then
20476 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20477
20478 -- Legaity checks
20479
20480 if Nkind (N) = N_Formal_Type_Declaration then
20481 Error_Msg_N
20482 ("discriminant defaults not allowed for formal type",
20483 Expression (Discr));
20484
20485 -- Flag an error for a tagged type with defaulted discriminants,
20486 -- excluding limited tagged types when compiling for Ada 2012
20487 -- (see AI05-0214).
20488
20489 elsif Is_Tagged_Type (Current_Scope)
20490 and then (not Is_Limited_Type (Current_Scope)
20491 or else Ada_Version < Ada_2012)
20492 and then Comes_From_Source (N)
20493 then
20494 -- Note: see similar test in Check_Or_Process_Discriminants, to
20495 -- handle the (illegal) case of the completion of an untagged
20496 -- view with discriminants with defaults by a tagged full view.
20497 -- We skip the check if Discr does not come from source, to
20498 -- account for the case of an untagged derived type providing
20499 -- defaults for a renamed discriminant from a private untagged
20500 -- ancestor with a tagged full view (ACATS B460006).
20501
20502 if Ada_Version >= Ada_2012 then
20503 Error_Msg_N
20504 ("discriminants of nonlimited tagged type cannot have"
20505 & " defaults",
20506 Expression (Discr));
20507 else
20508 Error_Msg_N
20509 ("discriminants of tagged type cannot have defaults",
20510 Expression (Discr));
20511 end if;
20512
20513 else
20514 Default_Present := True;
20515 Append_Elmt (Expression (Discr), Elist);
20516
20517 -- Tag the defining identifiers for the discriminants with
20518 -- their corresponding default expressions from the tree.
20519
20520 Set_Discriminant_Default_Value
20521 (Defining_Identifier (Discr), Expression (Discr));
20522 end if;
20523
20524 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20525 -- gets set unless we can be sure that no range check is required.
20526
20527 if not Expander_Active
20528 and then not
20529 Is_In_Range
20530 (Expression (Discr), Discr_Type, Assume_Valid => True)
20531 then
20532 Set_Do_Range_Check (Expression (Discr));
20533 end if;
20534
20535 -- No default discriminant value given
20536
20537 else
20538 Default_Not_Present := True;
20539 end if;
20540
20541 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20542 -- Discr_Type but with the null-exclusion attribute
20543
20544 if Ada_Version >= Ada_2005 then
20545
20546 -- Ada 2005 (AI-231): Static checks
20547
20548 if Can_Never_Be_Null (Discr_Type) then
20549 Null_Exclusion_Static_Checks (Discr);
20550
20551 elsif Is_Access_Type (Discr_Type)
20552 and then Null_Exclusion_Present (Discr)
20553
20554 -- No need to check itypes because in their case this check
20555 -- was done at their point of creation
20556
20557 and then not Is_Itype (Discr_Type)
20558 then
20559 if Can_Never_Be_Null (Discr_Type) then
20560 Error_Msg_NE
20561 ("`NOT NULL` not allowed (& already excludes null)",
20562 Discr,
20563 Discr_Type);
20564 end if;
20565
20566 Set_Etype (Defining_Identifier (Discr),
20567 Create_Null_Excluding_Itype
20568 (T => Discr_Type,
20569 Related_Nod => Discr));
20570
20571 -- Check for improper null exclusion if the type is otherwise
20572 -- legal for a discriminant.
20573
20574 elsif Null_Exclusion_Present (Discr)
20575 and then Is_Discrete_Type (Discr_Type)
20576 then
20577 Error_Msg_N
20578 ("null exclusion can only apply to an access type", Discr);
20579 end if;
20580
20581 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20582 -- can't have defaults. Synchronized types, or types that are
20583 -- explicitly limited are fine, but special tests apply to derived
20584 -- types in generics: in a generic body we have to assume the
20585 -- worst, and therefore defaults are not allowed if the parent is
20586 -- a generic formal private type (see ACATS B370001).
20587
20588 if Is_Access_Type (Discr_Type) and then Default_Present then
20589 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20590 or else Is_Limited_Record (Current_Scope)
20591 or else Is_Concurrent_Type (Current_Scope)
20592 or else Is_Concurrent_Record_Type (Current_Scope)
20593 or else Ekind (Current_Scope) = E_Limited_Private_Type
20594 then
20595 if not Is_Derived_Type (Current_Scope)
20596 or else not Is_Generic_Type (Etype (Current_Scope))
20597 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20598 or else Limited_Present
20599 (Type_Definition (Parent (Current_Scope)))
20600 then
20601 null;
20602
20603 else
20604 Error_Msg_N
20605 ("access discriminants of nonlimited types cannot "
20606 & "have defaults", Expression (Discr));
20607 end if;
20608
20609 elsif Present (Expression (Discr)) then
20610 Error_Msg_N
20611 ("(Ada 2005) access discriminants of nonlimited types "
20612 & "cannot have defaults", Expression (Discr));
20613 end if;
20614 end if;
20615 end if;
20616
20617 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20618 -- This check is relevant only when SPARK_Mode is on as it is not a
20619 -- standard Ada legality rule. The only way for a discriminant to be
20620 -- effectively volatile is to have an effectively volatile type, so
20621 -- we check this directly, because the Ekind of Discr might not be
20622 -- set yet (to help preventing cascaded errors on derived types).
20623
20624 if SPARK_Mode = On
20625 and then Is_Effectively_Volatile (Discr_Type)
20626 then
20627 Error_Msg_N ("discriminant cannot be volatile", Discr);
20628 end if;
20629
20630 Next (Discr);
20631 end loop;
20632
20633 -- An element list consisting of the default expressions of the
20634 -- discriminants is constructed in the above loop and used to set
20635 -- the Discriminant_Constraint attribute for the type. If an object
20636 -- is declared of this (record or task) type without any explicit
20637 -- discriminant constraint given, this element list will form the
20638 -- actual parameters for the corresponding initialization procedure
20639 -- for the type.
20640
20641 Set_Discriminant_Constraint (Current_Scope, Elist);
20642 Set_Stored_Constraint (Current_Scope, No_Elist);
20643
20644 -- Default expressions must be provided either for all or for none
20645 -- of the discriminants of a discriminant part. (RM 3.7.1)
20646
20647 if Default_Present and then Default_Not_Present then
20648 Error_Msg_N
20649 ("incomplete specification of defaults for discriminants", N);
20650 end if;
20651
20652 -- The use of the name of a discriminant is not allowed in default
20653 -- expressions of a discriminant part if the specification of the
20654 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20655
20656 -- To detect this, the discriminant names are entered initially with an
20657 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20658 -- attempt to use a void entity (for example in an expression that is
20659 -- type-checked) produces the error message: premature usage. Now after
20660 -- completing the semantic analysis of the discriminant part, we can set
20661 -- the Ekind of all the discriminants appropriately.
20662
20663 Discr := First (Discriminant_Specifications (N));
20664 Discr_Number := Uint_1;
20665 while Present (Discr) loop
20666 Id := Defining_Identifier (Discr);
20667
20668 if Ekind (Id) = E_In_Parameter then
20669 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
20670 end if;
20671
20672 Mutate_Ekind (Id, E_Discriminant);
20673 Reinit_Component_Location (Id);
20674 Reinit_Esize (Id);
20675 Set_Discriminant_Number (Id, Discr_Number);
20676
20677 -- Make sure this is always set, even in illegal programs
20678
20679 Set_Corresponding_Discriminant (Id, Empty);
20680
20681 -- Initialize the Original_Record_Component to the entity itself.
20682 -- Inherit_Components will propagate the right value to
20683 -- discriminants in derived record types.
20684
20685 Set_Original_Record_Component (Id, Id);
20686
20687 -- Create the discriminal for the discriminant
20688
20689 Build_Discriminal (Id);
20690
20691 Next (Discr);
20692 Discr_Number := Discr_Number + 1;
20693 end loop;
20694
20695 Set_Has_Discriminants (Current_Scope);
20696 end Process_Discriminants;
20697
20698 -----------------------
20699 -- Process_Full_View --
20700 -----------------------
20701
20702 -- WARNING: This routine manages Ghost regions. Return statements must be
20703 -- replaced by gotos which jump to the end of the routine and restore the
20704 -- Ghost mode.
20705
20706 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20707 procedure Collect_Implemented_Interfaces
20708 (Typ : Entity_Id;
20709 Ifaces : Elist_Id);
20710 -- Ada 2005: Gather all the interfaces that Typ directly or
20711 -- inherently implements. Duplicate entries are not added to
20712 -- the list Ifaces.
20713
20714 ------------------------------------
20715 -- Collect_Implemented_Interfaces --
20716 ------------------------------------
20717
20718 procedure Collect_Implemented_Interfaces
20719 (Typ : Entity_Id;
20720 Ifaces : Elist_Id)
20721 is
20722 Iface : Entity_Id;
20723 Iface_Elmt : Elmt_Id;
20724
20725 begin
20726 -- Abstract interfaces are only associated with tagged record types
20727
20728 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20729 return;
20730 end if;
20731
20732 -- Recursively climb to the ancestors
20733
20734 if Etype (Typ) /= Typ
20735
20736 -- Protect the frontend against wrong cyclic declarations like:
20737
20738 -- type B is new A with private;
20739 -- type C is new A with private;
20740 -- private
20741 -- type B is new C with null record;
20742 -- type C is new B with null record;
20743
20744 and then Etype (Typ) /= Priv_T
20745 and then Etype (Typ) /= Full_T
20746 then
20747 -- Keep separate the management of private type declarations
20748
20749 if Ekind (Typ) = E_Record_Type_With_Private then
20750
20751 -- Handle the following illegal usage:
20752 -- type Private_Type is tagged private;
20753 -- private
20754 -- type Private_Type is new Type_Implementing_Iface;
20755
20756 if Present (Full_View (Typ))
20757 and then Etype (Typ) /= Full_View (Typ)
20758 then
20759 if Is_Interface (Etype (Typ)) then
20760 Append_Unique_Elmt (Etype (Typ), Ifaces);
20761 end if;
20762
20763 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20764 end if;
20765
20766 -- Non-private types
20767
20768 else
20769 if Is_Interface (Etype (Typ)) then
20770 Append_Unique_Elmt (Etype (Typ), Ifaces);
20771 end if;
20772
20773 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20774 end if;
20775 end if;
20776
20777 -- Handle entities in the list of abstract interfaces
20778
20779 if Present (Interfaces (Typ)) then
20780 Iface_Elmt := First_Elmt (Interfaces (Typ));
20781 while Present (Iface_Elmt) loop
20782 Iface := Node (Iface_Elmt);
20783
20784 pragma Assert (Is_Interface (Iface));
20785
20786 if not Contain_Interface (Iface, Ifaces) then
20787 Append_Elmt (Iface, Ifaces);
20788 Collect_Implemented_Interfaces (Iface, Ifaces);
20789 end if;
20790
20791 Next_Elmt (Iface_Elmt);
20792 end loop;
20793 end if;
20794 end Collect_Implemented_Interfaces;
20795
20796 -- Local variables
20797
20798 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20799 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20800 -- Save the Ghost-related attributes to restore on exit
20801
20802 Full_Indic : Node_Id;
20803 Full_Parent : Entity_Id;
20804 Priv_Parent : Entity_Id;
20805
20806 -- Start of processing for Process_Full_View
20807
20808 begin
20809 Mark_And_Set_Ghost_Completion (N, Priv_T);
20810
20811 -- First some sanity checks that must be done after semantic
20812 -- decoration of the full view and thus cannot be placed with other
20813 -- similar checks in Find_Type_Name
20814
20815 if not Is_Limited_Type (Priv_T)
20816 and then (Is_Limited_Type (Full_T)
20817 or else Is_Limited_Composite (Full_T))
20818 then
20819 if In_Instance then
20820 null;
20821 else
20822 Error_Msg_N
20823 ("completion of nonlimited type cannot be limited", Full_T);
20824 Explain_Limited_Type (Full_T, Full_T);
20825 end if;
20826
20827 elsif Is_Abstract_Type (Full_T)
20828 and then not Is_Abstract_Type (Priv_T)
20829 then
20830 Error_Msg_N
20831 ("completion of nonabstract type cannot be abstract", Full_T);
20832
20833 elsif Is_Tagged_Type (Priv_T)
20834 and then Is_Limited_Type (Priv_T)
20835 and then not Is_Limited_Type (Full_T)
20836 then
20837 -- If pragma CPP_Class was applied to the private declaration
20838 -- propagate the limitedness to the full-view
20839
20840 if Is_CPP_Class (Priv_T) then
20841 Set_Is_Limited_Record (Full_T);
20842
20843 -- GNAT allow its own definition of Limited_Controlled to disobey
20844 -- this rule in order in ease the implementation. This test is safe
20845 -- because Root_Controlled is defined in a child of System that
20846 -- normal programs are not supposed to use.
20847
20848 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20849 Set_Is_Limited_Composite (Full_T);
20850 else
20851 Error_Msg_N
20852 ("completion of limited tagged type must be limited", Full_T);
20853 end if;
20854
20855 elsif Is_Generic_Type (Priv_T) then
20856 Error_Msg_N ("generic type cannot have a completion", Full_T);
20857 end if;
20858
20859 -- Check that ancestor interfaces of private and full views are
20860 -- consistent. We omit this check for synchronized types because
20861 -- they are performed on the corresponding record type when frozen.
20862
20863 if Ada_Version >= Ada_2005
20864 and then Is_Tagged_Type (Priv_T)
20865 and then Is_Tagged_Type (Full_T)
20866 and then not Is_Concurrent_Type (Full_T)
20867 then
20868 declare
20869 Iface : Entity_Id;
20870 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20871 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20872
20873 begin
20874 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20875 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20876
20877 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20878 -- an interface type if and only if the full type is descendant
20879 -- of the interface type (AARM 7.3 (7.3/2)).
20880
20881 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20882
20883 if Present (Iface) then
20884 Error_Msg_NE
20885 ("interface in partial view& not implemented by full type "
20886 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20887 end if;
20888
20889 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20890
20891 if Present (Iface) then
20892 Error_Msg_NE
20893 ("interface & not implemented by partial view "
20894 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20895 end if;
20896 end;
20897 end if;
20898
20899 if Is_Tagged_Type (Priv_T)
20900 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20901 and then Is_Derived_Type (Full_T)
20902 then
20903 Priv_Parent := Etype (Priv_T);
20904
20905 -- The full view of a private extension may have been transformed
20906 -- into an unconstrained derived type declaration and a subtype
20907 -- declaration (see build_derived_record_type for details).
20908
20909 if Nkind (N) = N_Subtype_Declaration then
20910 Full_Indic := Subtype_Indication (N);
20911 Full_Parent := Etype (Base_Type (Full_T));
20912 else
20913 Full_Indic := Subtype_Indication (Type_Definition (N));
20914 Full_Parent := Etype (Full_T);
20915 end if;
20916
20917 -- Check that the parent type of the full type is a descendant of
20918 -- the ancestor subtype given in the private extension. If either
20919 -- entity has an Etype equal to Any_Type then we had some previous
20920 -- error situation [7.3(8)].
20921
20922 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20923 goto Leave;
20924
20925 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20926 -- any order. Therefore we don't have to check that its parent must
20927 -- be a descendant of the parent of the private type declaration.
20928
20929 elsif Is_Interface (Priv_Parent)
20930 and then Is_Interface (Full_Parent)
20931 then
20932 null;
20933
20934 -- Ada 2005 (AI-251): If the parent of the private type declaration
20935 -- is an interface there is no need to check that it is an ancestor
20936 -- of the associated full type declaration. The required tests for
20937 -- this case are performed by Build_Derived_Record_Type.
20938
20939 elsif not Is_Interface (Base_Type (Priv_Parent))
20940 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20941 then
20942 Error_Msg_N
20943 ("parent of full type must descend from parent of private "
20944 & "extension", Full_Indic);
20945
20946 -- First check a formal restriction, and then proceed with checking
20947 -- Ada rules. Since the formal restriction is not a serious error, we
20948 -- don't prevent further error detection for this check, hence the
20949 -- ELSE.
20950
20951 else
20952 -- Check the rules of 7.3(10): if the private extension inherits
20953 -- known discriminants, then the full type must also inherit those
20954 -- discriminants from the same (ancestor) type, and the parent
20955 -- subtype of the full type must be constrained if and only if
20956 -- the ancestor subtype of the private extension is constrained.
20957
20958 if No (Discriminant_Specifications (Parent (Priv_T)))
20959 and then not Has_Unknown_Discriminants (Priv_T)
20960 and then Has_Discriminants (Base_Type (Priv_Parent))
20961 then
20962 declare
20963 Priv_Indic : constant Node_Id :=
20964 Subtype_Indication (Parent (Priv_T));
20965
20966 Priv_Constr : constant Boolean :=
20967 Is_Constrained (Priv_Parent)
20968 or else
20969 Nkind (Priv_Indic) = N_Subtype_Indication
20970 or else
20971 Is_Constrained (Entity (Priv_Indic));
20972
20973 Full_Constr : constant Boolean :=
20974 Is_Constrained (Full_Parent)
20975 or else
20976 Nkind (Full_Indic) = N_Subtype_Indication
20977 or else
20978 Is_Constrained (Entity (Full_Indic));
20979
20980 Priv_Discr : Entity_Id;
20981 Full_Discr : Entity_Id;
20982
20983 begin
20984 Priv_Discr := First_Discriminant (Priv_Parent);
20985 Full_Discr := First_Discriminant (Full_Parent);
20986 while Present (Priv_Discr) and then Present (Full_Discr) loop
20987 if Original_Record_Component (Priv_Discr) =
20988 Original_Record_Component (Full_Discr)
20989 or else
20990 Corresponding_Discriminant (Priv_Discr) =
20991 Corresponding_Discriminant (Full_Discr)
20992 then
20993 null;
20994 else
20995 exit;
20996 end if;
20997
20998 Next_Discriminant (Priv_Discr);
20999 Next_Discriminant (Full_Discr);
21000 end loop;
21001
21002 if Present (Priv_Discr) or else Present (Full_Discr) then
21003 Error_Msg_N
21004 ("full view must inherit discriminants of the parent "
21005 & "type used in the private extension", Full_Indic);
21006
21007 elsif Priv_Constr and then not Full_Constr then
21008 Error_Msg_N
21009 ("parent subtype of full type must be constrained",
21010 Full_Indic);
21011
21012 elsif Full_Constr and then not Priv_Constr then
21013 Error_Msg_N
21014 ("parent subtype of full type must be unconstrained",
21015 Full_Indic);
21016 end if;
21017 end;
21018
21019 -- Check the rules of 7.3(12): if a partial view has neither
21020 -- known or unknown discriminants, then the full type
21021 -- declaration shall define a definite subtype.
21022
21023 elsif not Has_Unknown_Discriminants (Priv_T)
21024 and then not Has_Discriminants (Priv_T)
21025 and then not Is_Constrained (Full_T)
21026 then
21027 Error_Msg_N
21028 ("full view must define a constrained type if partial view "
21029 & "has no discriminants", Full_T);
21030 end if;
21031
21032 -- Do we implement the following properly???
21033 -- If the ancestor subtype of a private extension has constrained
21034 -- discriminants, then the parent subtype of the full view shall
21035 -- impose a statically matching constraint on those discriminants
21036 -- [7.3(13)].
21037 end if;
21038
21039 else
21040 -- For untagged types, verify that a type without discriminants is
21041 -- not completed with an unconstrained type. A separate error message
21042 -- is produced if the full type has defaulted discriminants.
21043
21044 if Is_Definite_Subtype (Priv_T)
21045 and then not Is_Definite_Subtype (Full_T)
21046 then
21047 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21048 Error_Msg_NE
21049 ("full view of& not compatible with declaration#",
21050 Full_T, Priv_T);
21051
21052 if not Is_Tagged_Type (Full_T) then
21053 Error_Msg_N
21054 ("\one is constrained, the other unconstrained", Full_T);
21055 end if;
21056 end if;
21057 end if;
21058
21059 -- AI-419: verify that the use of "limited" is consistent
21060
21061 declare
21062 Orig_Decl : constant Node_Id := Original_Node (N);
21063
21064 begin
21065 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21066 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21067 and then Nkind
21068 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21069 then
21070 if not Limited_Present (Parent (Priv_T))
21071 and then not Synchronized_Present (Parent (Priv_T))
21072 and then Limited_Present (Type_Definition (Orig_Decl))
21073 then
21074 Error_Msg_N
21075 ("full view of non-limited extension cannot be limited", N);
21076
21077 -- Conversely, if the partial view carries the limited keyword,
21078 -- the full view must as well, even if it may be redundant.
21079
21080 elsif Limited_Present (Parent (Priv_T))
21081 and then not Limited_Present (Type_Definition (Orig_Decl))
21082 then
21083 Error_Msg_N
21084 ("full view of limited extension must be explicitly limited",
21085 N);
21086 end if;
21087 end if;
21088 end;
21089
21090 -- Ada 2005 (AI-443): A synchronized private extension must be
21091 -- completed by a task or protected type.
21092
21093 if Ada_Version >= Ada_2005
21094 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21095 and then Synchronized_Present (Parent (Priv_T))
21096 and then not Is_Concurrent_Type (Full_T)
21097 then
21098 Error_Msg_N ("full view of synchronized extension must " &
21099 "be synchronized type", N);
21100 end if;
21101
21102 -- Ada 2005 AI-363: if the full view has discriminants with
21103 -- defaults, it is illegal to declare constrained access subtypes
21104 -- whose designated type is the current type. This allows objects
21105 -- of the type that are declared in the heap to be unconstrained.
21106
21107 if not Has_Unknown_Discriminants (Priv_T)
21108 and then not Has_Discriminants (Priv_T)
21109 and then Has_Defaulted_Discriminants (Full_T)
21110 then
21111 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21112 Set_Has_Constrained_Partial_View (Priv_T);
21113 end if;
21114
21115 -- Create a full declaration for all its subtypes recorded in
21116 -- Private_Dependents and swap them similarly to the base type. These
21117 -- are subtypes that have been define before the full declaration of
21118 -- the private type. We also swap the entry in Private_Dependents list
21119 -- so we can properly restore the private view on exit from the scope.
21120
21121 declare
21122 Priv_Elmt : Elmt_Id;
21123 Priv_Scop : Entity_Id;
21124 Priv : Entity_Id;
21125 Full : Entity_Id;
21126
21127 begin
21128 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21129 while Present (Priv_Elmt) loop
21130 Priv := Node (Priv_Elmt);
21131 Priv_Scop := Scope (Priv);
21132
21133 if Ekind (Priv) in E_Private_Subtype
21134 | E_Limited_Private_Subtype
21135 | E_Record_Subtype_With_Private
21136 then
21137 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21138 Set_Is_Itype (Full);
21139 Set_Parent (Full, Parent (Priv));
21140 Set_Associated_Node_For_Itype (Full, N);
21141
21142 -- Now we need to complete the private subtype, but since the
21143 -- base type has already been swapped, we must also swap the
21144 -- subtypes (and thus, reverse the arguments in the call to
21145 -- Complete_Private_Subtype). Also note that we may need to
21146 -- re-establish the scope of the private subtype.
21147
21148 Copy_And_Swap (Priv, Full);
21149
21150 if not In_Open_Scopes (Priv_Scop) then
21151 Push_Scope (Priv_Scop);
21152
21153 else
21154 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21155
21156 Priv_Scop := Empty;
21157 end if;
21158
21159 Complete_Private_Subtype (Full, Priv, Full_T, N);
21160 Set_Full_View (Full, Priv);
21161
21162 if Present (Priv_Scop) then
21163 Pop_Scope;
21164 end if;
21165
21166 Replace_Elmt (Priv_Elmt, Full);
21167 end if;
21168
21169 Next_Elmt (Priv_Elmt);
21170 end loop;
21171 end;
21172
21173 declare
21174 Disp_Typ : Entity_Id;
21175 Full_List : Elist_Id;
21176 Prim : Entity_Id;
21177 Prim_Elmt : Elmt_Id;
21178 Priv_List : Elist_Id;
21179
21180 function Contains
21181 (E : Entity_Id;
21182 L : Elist_Id) return Boolean;
21183 -- Determine whether list L contains element E
21184
21185 --------------
21186 -- Contains --
21187 --------------
21188
21189 function Contains
21190 (E : Entity_Id;
21191 L : Elist_Id) return Boolean
21192 is
21193 List_Elmt : Elmt_Id;
21194
21195 begin
21196 List_Elmt := First_Elmt (L);
21197 while Present (List_Elmt) loop
21198 if Node (List_Elmt) = E then
21199 return True;
21200 end if;
21201
21202 Next_Elmt (List_Elmt);
21203 end loop;
21204
21205 return False;
21206 end Contains;
21207
21208 -- Start of processing
21209
21210 begin
21211 -- If the private view was tagged, copy the new primitive operations
21212 -- from the private view to the full view.
21213
21214 if Is_Tagged_Type (Full_T) then
21215 if Is_Tagged_Type (Priv_T) then
21216 Priv_List := Primitive_Operations (Priv_T);
21217 Prim_Elmt := First_Elmt (Priv_List);
21218
21219 -- In the case of a concurrent type completing a private tagged
21220 -- type, primitives may have been declared in between the two
21221 -- views. These subprograms need to be wrapped the same way
21222 -- entries and protected procedures are handled because they
21223 -- cannot be directly shared by the two views.
21224
21225 if Is_Concurrent_Type (Full_T) then
21226 declare
21227 Conc_Typ : constant Entity_Id :=
21228 Corresponding_Record_Type (Full_T);
21229 Curr_Nod : Node_Id := Parent (Conc_Typ);
21230 Wrap_Spec : Node_Id;
21231
21232 begin
21233 while Present (Prim_Elmt) loop
21234 Prim := Node (Prim_Elmt);
21235
21236 if Comes_From_Source (Prim)
21237 and then not Is_Abstract_Subprogram (Prim)
21238 then
21239 Wrap_Spec :=
21240 Make_Subprogram_Declaration (Sloc (Prim),
21241 Specification =>
21242 Build_Wrapper_Spec
21243 (Subp_Id => Prim,
21244 Obj_Typ => Conc_Typ,
21245 Formals =>
21246 Parameter_Specifications
21247 (Parent (Prim))));
21248
21249 Insert_After (Curr_Nod, Wrap_Spec);
21250 Curr_Nod := Wrap_Spec;
21251
21252 Analyze (Wrap_Spec);
21253
21254 -- Remove the wrapper from visibility to avoid
21255 -- spurious conflict with the wrapped entity.
21256
21257 Set_Is_Immediately_Visible
21258 (Defining_Entity (Specification (Wrap_Spec)),
21259 False);
21260 end if;
21261
21262 Next_Elmt (Prim_Elmt);
21263 end loop;
21264
21265 goto Leave;
21266 end;
21267
21268 -- For non-concurrent types, transfer explicit primitives, but
21269 -- omit those inherited from the parent of the private view
21270 -- since they will be re-inherited later on.
21271
21272 else
21273 Full_List := Primitive_Operations (Full_T);
21274 while Present (Prim_Elmt) loop
21275 Prim := Node (Prim_Elmt);
21276
21277 if Comes_From_Source (Prim)
21278 and then not Contains (Prim, Full_List)
21279 then
21280 Append_Elmt (Prim, Full_List);
21281 end if;
21282
21283 Next_Elmt (Prim_Elmt);
21284 end loop;
21285 end if;
21286
21287 -- Untagged private view
21288
21289 else
21290 Full_List := Primitive_Operations (Full_T);
21291
21292 -- In this case the partial view is untagged, so here we locate
21293 -- all of the earlier primitives that need to be treated as
21294 -- dispatching (those that appear between the two views). Note
21295 -- that these additional operations must all be new operations
21296 -- (any earlier operations that override inherited operations
21297 -- of the full view will already have been inserted in the
21298 -- primitives list, marked by Check_Operation_From_Private_View
21299 -- as dispatching. Note that implicit "/=" operators are
21300 -- excluded from being added to the primitives list since they
21301 -- shouldn't be treated as dispatching (tagged "/=" is handled
21302 -- specially).
21303
21304 Prim := Next_Entity (Full_T);
21305 while Present (Prim) and then Prim /= Priv_T loop
21306 if Ekind (Prim) in E_Procedure | E_Function then
21307 Disp_Typ := Find_Dispatching_Type (Prim);
21308
21309 if Disp_Typ = Full_T
21310 and then (Chars (Prim) /= Name_Op_Ne
21311 or else Comes_From_Source (Prim))
21312 then
21313 Check_Controlling_Formals (Full_T, Prim);
21314
21315 if Is_Suitable_Primitive (Prim)
21316 and then not Is_Dispatching_Operation (Prim)
21317 then
21318 Append_Elmt (Prim, Full_List);
21319 Set_Is_Dispatching_Operation (Prim);
21320 Set_DT_Position_Value (Prim, No_Uint);
21321 end if;
21322
21323 elsif Is_Dispatching_Operation (Prim)
21324 and then Disp_Typ /= Full_T
21325 then
21326 -- Verify that it is not otherwise controlled by a
21327 -- formal or a return value of type T.
21328
21329 Check_Controlling_Formals (Disp_Typ, Prim);
21330 end if;
21331 end if;
21332
21333 Next_Entity (Prim);
21334 end loop;
21335 end if;
21336
21337 -- For the tagged case, the two views can share the same primitive
21338 -- operations list and the same class-wide type. Update attributes
21339 -- of the class-wide type which depend on the full declaration.
21340
21341 if Is_Tagged_Type (Priv_T) then
21342 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21343 Set_Class_Wide_Type
21344 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21345
21346 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21347 end if;
21348
21349 -- For untagged types, copy the primitives across from the private
21350 -- view to the full view (when extensions are allowed), for support
21351 -- of prefixed calls (when extensions are enabled).
21352
21353 elsif Extensions_Allowed then
21354 Priv_List := Primitive_Operations (Priv_T);
21355 Prim_Elmt := First_Elmt (Priv_List);
21356
21357 Full_List := Primitive_Operations (Full_T);
21358 while Present (Prim_Elmt) loop
21359 Prim := Node (Prim_Elmt);
21360 Append_Elmt (Prim, Full_List);
21361 Next_Elmt (Prim_Elmt);
21362 end loop;
21363 end if;
21364 end;
21365
21366 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21367
21368 if Known_To_Have_Preelab_Init (Priv_T) then
21369
21370 -- Case where there is a pragma Preelaborable_Initialization. We
21371 -- always allow this in predefined units, which is cheating a bit,
21372 -- but it means we don't have to struggle to meet the requirements in
21373 -- the RM for having Preelaborable Initialization. Otherwise we
21374 -- require that the type meets the RM rules. But we can't check that
21375 -- yet, because of the rule about overriding Initialize, so we simply
21376 -- set a flag that will be checked at freeze time.
21377
21378 if not In_Predefined_Unit (Full_T) then
21379 Set_Must_Have_Preelab_Init (Full_T);
21380 end if;
21381 end if;
21382
21383 -- If pragma CPP_Class was applied to the private type declaration,
21384 -- propagate it now to the full type declaration.
21385
21386 if Is_CPP_Class (Priv_T) then
21387 Set_Is_CPP_Class (Full_T);
21388 Set_Convention (Full_T, Convention_CPP);
21389
21390 -- Check that components of imported CPP types do not have default
21391 -- expressions.
21392
21393 Check_CPP_Type_Has_No_Defaults (Full_T);
21394 end if;
21395
21396 -- If the private view has user specified stream attributes, then so has
21397 -- the full view.
21398
21399 -- Why the test, how could these flags be already set in Full_T ???
21400
21401 if Has_Specified_Stream_Read (Priv_T) then
21402 Set_Has_Specified_Stream_Read (Full_T);
21403 end if;
21404
21405 if Has_Specified_Stream_Write (Priv_T) then
21406 Set_Has_Specified_Stream_Write (Full_T);
21407 end if;
21408
21409 if Has_Specified_Stream_Input (Priv_T) then
21410 Set_Has_Specified_Stream_Input (Full_T);
21411 end if;
21412
21413 if Has_Specified_Stream_Output (Priv_T) then
21414 Set_Has_Specified_Stream_Output (Full_T);
21415 end if;
21416
21417 -- Propagate Default_Initial_Condition-related attributes from the
21418 -- partial view to the full view.
21419
21420 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21421
21422 -- And to the underlying full view, if any
21423
21424 if Is_Private_Type (Full_T)
21425 and then Present (Underlying_Full_View (Full_T))
21426 then
21427 Propagate_DIC_Attributes
21428 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21429 end if;
21430
21431 -- Propagate invariant-related attributes from the partial view to the
21432 -- full view.
21433
21434 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21435
21436 -- And to the underlying full view, if any
21437
21438 if Is_Private_Type (Full_T)
21439 and then Present (Underlying_Full_View (Full_T))
21440 then
21441 Propagate_Invariant_Attributes
21442 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21443 end if;
21444
21445 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21446 -- in the full view without advertising the inheritance in the partial
21447 -- view. This can only occur when the partial view has no parent type
21448 -- and the full view has an interface as a parent. Any other scenarios
21449 -- are illegal because implemented interfaces must match between the
21450 -- two views.
21451
21452 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21453 declare
21454 Full_Par : constant Entity_Id := Etype (Full_T);
21455 Priv_Par : constant Entity_Id := Etype (Priv_T);
21456
21457 begin
21458 if not Is_Interface (Priv_Par)
21459 and then Is_Interface (Full_Par)
21460 and then Has_Inheritable_Invariants (Full_Par)
21461 then
21462 Error_Msg_N
21463 ("hidden inheritance of class-wide type invariants not "
21464 & "allowed", N);
21465 end if;
21466 end;
21467 end if;
21468
21469 -- Propagate predicates to full type, and predicate function if already
21470 -- defined. It is not clear that this can actually happen? the partial
21471 -- view cannot be frozen yet, and the predicate function has not been
21472 -- built. Still it is a cheap check and seems safer to make it.
21473
21474 Propagate_Predicate_Attributes (Full_T, Priv_T);
21475
21476 if Is_Private_Type (Full_T)
21477 and then Present (Underlying_Full_View (Full_T))
21478 then
21479 Propagate_Predicate_Attributes
21480 (Underlying_Full_View (Full_T), Priv_T);
21481 end if;
21482
21483 <<Leave>>
21484 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21485 end Process_Full_View;
21486
21487 -----------------------------------
21488 -- Process_Incomplete_Dependents --
21489 -----------------------------------
21490
21491 procedure Process_Incomplete_Dependents
21492 (N : Node_Id;
21493 Full_T : Entity_Id;
21494 Inc_T : Entity_Id)
21495 is
21496 Inc_Elmt : Elmt_Id;
21497 Priv_Dep : Entity_Id;
21498 New_Subt : Entity_Id;
21499
21500 Disc_Constraint : Elist_Id;
21501
21502 begin
21503 if No (Private_Dependents (Inc_T)) then
21504 return;
21505 end if;
21506
21507 -- Itypes that may be generated by the completion of an incomplete
21508 -- subtype are not used by the back-end and not attached to the tree.
21509 -- They are created only for constraint-checking purposes.
21510
21511 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21512 while Present (Inc_Elmt) loop
21513 Priv_Dep := Node (Inc_Elmt);
21514
21515 if Ekind (Priv_Dep) = E_Subprogram_Type then
21516
21517 -- An Access_To_Subprogram type may have a return type or a
21518 -- parameter type that is incomplete. Replace with the full view.
21519
21520 if Etype (Priv_Dep) = Inc_T then
21521 Set_Etype (Priv_Dep, Full_T);
21522 end if;
21523
21524 declare
21525 Formal : Entity_Id;
21526
21527 begin
21528 Formal := First_Formal (Priv_Dep);
21529 while Present (Formal) loop
21530 if Etype (Formal) = Inc_T then
21531 Set_Etype (Formal, Full_T);
21532 end if;
21533
21534 Next_Formal (Formal);
21535 end loop;
21536 end;
21537
21538 elsif Is_Overloadable (Priv_Dep) then
21539
21540 -- If a subprogram in the incomplete dependents list is primitive
21541 -- for a tagged full type then mark it as a dispatching operation,
21542 -- check whether it overrides an inherited subprogram, and check
21543 -- restrictions on its controlling formals. Note that a protected
21544 -- operation is never dispatching: only its wrapper operation
21545 -- (which has convention Ada) is.
21546
21547 if Is_Tagged_Type (Full_T)
21548 and then Is_Primitive (Priv_Dep)
21549 and then Convention (Priv_Dep) /= Convention_Protected
21550 then
21551 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21552 Set_Is_Dispatching_Operation (Priv_Dep);
21553 Check_Controlling_Formals (Full_T, Priv_Dep);
21554 end if;
21555
21556 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21557
21558 -- Can happen during processing of a body before the completion
21559 -- of a TA type. Ignore, because spec is also on dependent list.
21560
21561 return;
21562
21563 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21564 -- corresponding subtype of the full view.
21565
21566 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21567 and then Comes_From_Source (Priv_Dep)
21568 then
21569 Set_Subtype_Indication
21570 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21571 Reinit_Field_To_Zero
21572 (Priv_Dep, F_Private_Dependents,
21573 Old_Ekind => E_Incomplete_Subtype);
21574 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21575 Set_Etype (Priv_Dep, Full_T);
21576 Set_Analyzed (Parent (Priv_Dep), False);
21577
21578 -- Reanalyze the declaration, suppressing the call to Enter_Name
21579 -- to avoid duplicate names.
21580
21581 Analyze_Subtype_Declaration
21582 (N => Parent (Priv_Dep),
21583 Skip => True);
21584
21585 -- Dependent is a subtype
21586
21587 else
21588 -- We build a new subtype indication using the full view of the
21589 -- incomplete parent. The discriminant constraints have been
21590 -- elaborated already at the point of the subtype declaration.
21591
21592 New_Subt := Create_Itype (E_Void, N);
21593
21594 if Has_Discriminants (Full_T) then
21595 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21596 else
21597 Disc_Constraint := No_Elist;
21598 end if;
21599
21600 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21601 Set_Full_View (Priv_Dep, New_Subt);
21602 end if;
21603
21604 Next_Elmt (Inc_Elmt);
21605 end loop;
21606 end Process_Incomplete_Dependents;
21607
21608 --------------------------------
21609 -- Process_Range_Expr_In_Decl --
21610 --------------------------------
21611
21612 procedure Process_Range_Expr_In_Decl
21613 (R : Node_Id;
21614 T : Entity_Id;
21615 Subtyp : Entity_Id := Empty;
21616 Check_List : List_Id := No_List;
21617 R_Check_Off : Boolean := False)
21618 is
21619 Lo, Hi : Node_Id;
21620 R_Checks : Check_Result;
21621 Insert_Node : Node_Id;
21622 Def_Id : Entity_Id;
21623
21624 begin
21625 Analyze_And_Resolve (R, Base_Type (T));
21626
21627 if Nkind (R) = N_Range then
21628 Lo := Low_Bound (R);
21629 Hi := High_Bound (R);
21630
21631 -- Validity checks on the range of a quantified expression are
21632 -- delayed until the construct is transformed into a loop.
21633
21634 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21635 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21636 then
21637 null;
21638
21639 -- We need to ensure validity of the bounds here, because if we
21640 -- go ahead and do the expansion, then the expanded code will get
21641 -- analyzed with range checks suppressed and we miss the check.
21642
21643 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21644 -- the temporaries generated by routine Remove_Side_Effects by means
21645 -- of validity checks must use the same names. When a range appears
21646 -- in the parent of a generic, the range is processed with checks
21647 -- disabled as part of the generic context and with checks enabled
21648 -- for code generation purposes. This leads to link issues as the
21649 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21650 -- template sees the temporaries generated by Remove_Side_Effects.
21651
21652 else
21653 Validity_Check_Range (R, Subtyp);
21654 end if;
21655
21656 -- If there were errors in the declaration, try and patch up some
21657 -- common mistakes in the bounds. The cases handled are literals
21658 -- which are Integer where the expected type is Real and vice versa.
21659 -- These corrections allow the compilation process to proceed further
21660 -- along since some basic assumptions of the format of the bounds
21661 -- are guaranteed.
21662
21663 if Etype (R) = Any_Type then
21664 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21665 Rewrite (Lo,
21666 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21667
21668 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21669 Rewrite (Hi,
21670 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21671
21672 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21673 Rewrite (Lo,
21674 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21675
21676 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21677 Rewrite (Hi,
21678 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21679 end if;
21680
21681 Set_Etype (Lo, T);
21682 Set_Etype (Hi, T);
21683 end if;
21684
21685 -- If the bounds of the range have been mistakenly given as string
21686 -- literals (perhaps in place of character literals), then an error
21687 -- has already been reported, but we rewrite the string literal as a
21688 -- bound of the range's type to avoid blowups in later processing
21689 -- that looks at static values.
21690
21691 if Nkind (Lo) = N_String_Literal then
21692 Rewrite (Lo,
21693 Make_Attribute_Reference (Sloc (Lo),
21694 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21695 Attribute_Name => Name_First));
21696 Analyze_And_Resolve (Lo);
21697 end if;
21698
21699 if Nkind (Hi) = N_String_Literal then
21700 Rewrite (Hi,
21701 Make_Attribute_Reference (Sloc (Hi),
21702 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21703 Attribute_Name => Name_First));
21704 Analyze_And_Resolve (Hi);
21705 end if;
21706
21707 -- If bounds aren't scalar at this point then exit, avoiding
21708 -- problems with further processing of the range in this procedure.
21709
21710 if not Is_Scalar_Type (Etype (Lo)) then
21711 return;
21712 end if;
21713
21714 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21715 -- then range of the base type. Here we check whether the bounds
21716 -- are in the range of the subtype itself. Note that if the bounds
21717 -- represent the null range the Constraint_Error exception should
21718 -- not be raised.
21719
21720 -- ??? The following code should be cleaned up as follows
21721
21722 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21723 -- is done in the call to Range_Check (R, T); below
21724
21725 -- 2. The use of R_Check_Off should be investigated and possibly
21726 -- removed, this would clean up things a bit.
21727
21728 if Is_Null_Range (Lo, Hi) then
21729 null;
21730
21731 else
21732 -- Capture values of bounds and generate temporaries for them
21733 -- if needed, before applying checks, since checks may cause
21734 -- duplication of the expression without forcing evaluation.
21735
21736 -- The forced evaluation removes side effects from expressions,
21737 -- which should occur also in GNATprove mode. Otherwise, we end up
21738 -- with unexpected insertions of actions at places where this is
21739 -- not supposed to occur, e.g. on default parameters of a call.
21740
21741 if Expander_Active or GNATprove_Mode then
21742
21743 -- Call Force_Evaluation to create declarations as needed to
21744 -- deal with side effects, and also create typ_FIRST/LAST
21745 -- entities for bounds if we have a subtype name.
21746
21747 -- Note: we do this transformation even if expansion is not
21748 -- active if we are in GNATprove_Mode since the transformation
21749 -- is in general required to ensure that the resulting tree has
21750 -- proper Ada semantics.
21751
21752 Force_Evaluation
21753 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21754 Force_Evaluation
21755 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21756 end if;
21757
21758 -- We use a flag here instead of suppressing checks on the type
21759 -- because the type we check against isn't necessarily the place
21760 -- where we put the check.
21761
21762 if not R_Check_Off then
21763 R_Checks := Get_Range_Checks (R, T);
21764
21765 -- Look up tree to find an appropriate insertion point. We
21766 -- can't just use insert_actions because later processing
21767 -- depends on the insertion node. Prior to Ada 2012 the
21768 -- insertion point could only be a declaration or a loop, but
21769 -- quantified expressions can appear within any context in an
21770 -- expression, and the insertion point can be any statement,
21771 -- pragma, or declaration.
21772
21773 Insert_Node := Parent (R);
21774 while Present (Insert_Node) loop
21775 exit when
21776 Nkind (Insert_Node) in N_Declaration
21777 and then
21778 Nkind (Insert_Node) not in N_Component_Declaration
21779 | N_Loop_Parameter_Specification
21780 | N_Function_Specification
21781 | N_Procedure_Specification;
21782
21783 exit when Nkind (Insert_Node) in
21784 N_Later_Decl_Item |
21785 N_Statement_Other_Than_Procedure_Call |
21786 N_Procedure_Call_Statement |
21787 N_Pragma;
21788
21789 Insert_Node := Parent (Insert_Node);
21790 end loop;
21791
21792 -- Why would Type_Decl not be present??? Without this test,
21793 -- short regression tests fail.
21794
21795 if Present (Insert_Node) then
21796
21797 -- Case of loop statement. Verify that the range is part
21798 -- of the subtype indication of the iteration scheme.
21799
21800 if Nkind (Insert_Node) = N_Loop_Statement then
21801 declare
21802 Indic : Node_Id;
21803
21804 begin
21805 Indic := Parent (R);
21806 while Present (Indic)
21807 and then Nkind (Indic) /= N_Subtype_Indication
21808 loop
21809 Indic := Parent (Indic);
21810 end loop;
21811
21812 if Present (Indic) then
21813 Def_Id := Etype (Subtype_Mark (Indic));
21814
21815 Insert_Range_Checks
21816 (R_Checks,
21817 Insert_Node,
21818 Def_Id,
21819 Sloc (Insert_Node),
21820 Do_Before => True);
21821 end if;
21822 end;
21823
21824 -- Case of declarations. If the declaration is for a type
21825 -- and involves discriminants, the checks are premature at
21826 -- the declaration point and need to wait for the expansion
21827 -- of the initialization procedure, which will pass in the
21828 -- list to put them on; otherwise, the checks are done at
21829 -- the declaration point and there is no need to do them
21830 -- again in the initialization procedure.
21831
21832 elsif Nkind (Insert_Node) in N_Declaration then
21833 Def_Id := Defining_Identifier (Insert_Node);
21834
21835 if (Ekind (Def_Id) = E_Record_Type
21836 and then Depends_On_Discriminant (R))
21837 or else
21838 (Ekind (Def_Id) = E_Protected_Type
21839 and then Has_Discriminants (Def_Id))
21840 then
21841 if Present (Check_List) then
21842 Append_Range_Checks
21843 (R_Checks,
21844 Check_List, Def_Id, Sloc (Insert_Node));
21845 end if;
21846
21847 else
21848 if No (Check_List) then
21849 Insert_Range_Checks
21850 (R_Checks,
21851 Insert_Node, Def_Id, Sloc (Insert_Node));
21852 end if;
21853 end if;
21854
21855 -- Case of statements. Drop the checks, as the range appears
21856 -- in the context of a quantified expression. Insertion will
21857 -- take place when expression is expanded.
21858
21859 else
21860 null;
21861 end if;
21862 end if;
21863 end if;
21864 end if;
21865
21866 -- Case of other than an explicit N_Range node
21867
21868 -- The forced evaluation removes side effects from expressions, which
21869 -- should occur also in GNATprove mode. Otherwise, we end up with
21870 -- unexpected insertions of actions at places where this is not
21871 -- supposed to occur, e.g. on default parameters of a call.
21872
21873 elsif Expander_Active or GNATprove_Mode then
21874 Get_Index_Bounds (R, Lo, Hi);
21875 Force_Evaluation (Lo);
21876 Force_Evaluation (Hi);
21877 end if;
21878 end Process_Range_Expr_In_Decl;
21879
21880 --------------------------------------
21881 -- Process_Real_Range_Specification --
21882 --------------------------------------
21883
21884 procedure Process_Real_Range_Specification (Def : Node_Id) is
21885 Spec : constant Node_Id := Real_Range_Specification (Def);
21886 Lo : Node_Id;
21887 Hi : Node_Id;
21888 Err : Boolean := False;
21889
21890 procedure Analyze_Bound (N : Node_Id);
21891 -- Analyze and check one bound
21892
21893 -------------------
21894 -- Analyze_Bound --
21895 -------------------
21896
21897 procedure Analyze_Bound (N : Node_Id) is
21898 begin
21899 Analyze_And_Resolve (N, Any_Real);
21900
21901 if not Is_OK_Static_Expression (N) then
21902 Flag_Non_Static_Expr
21903 ("bound in real type definition is not static!", N);
21904 Err := True;
21905 end if;
21906 end Analyze_Bound;
21907
21908 -- Start of processing for Process_Real_Range_Specification
21909
21910 begin
21911 if Present (Spec) then
21912 Lo := Low_Bound (Spec);
21913 Hi := High_Bound (Spec);
21914 Analyze_Bound (Lo);
21915 Analyze_Bound (Hi);
21916
21917 -- If error, clear away junk range specification
21918
21919 if Err then
21920 Set_Real_Range_Specification (Def, Empty);
21921 end if;
21922 end if;
21923 end Process_Real_Range_Specification;
21924
21925 ---------------------
21926 -- Process_Subtype --
21927 ---------------------
21928
21929 function Process_Subtype
21930 (S : Node_Id;
21931 Related_Nod : Node_Id;
21932 Related_Id : Entity_Id := Empty;
21933 Suffix : Character := ' ') return Entity_Id
21934 is
21935 procedure Check_Incomplete (T : Node_Id);
21936 -- Called to verify that an incomplete type is not used prematurely
21937
21938 ----------------------
21939 -- Check_Incomplete --
21940 ----------------------
21941
21942 procedure Check_Incomplete (T : Node_Id) is
21943 begin
21944 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21945
21946 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21947 and then
21948 not (Ada_Version >= Ada_2005
21949 and then
21950 (Nkind (Parent (T)) = N_Subtype_Declaration
21951 or else (Nkind (Parent (T)) = N_Subtype_Indication
21952 and then Nkind (Parent (Parent (T))) =
21953 N_Subtype_Declaration)))
21954 then
21955 Error_Msg_N ("invalid use of type before its full declaration", T);
21956 end if;
21957 end Check_Incomplete;
21958
21959 -- Local variables
21960
21961 P : Node_Id;
21962 Def_Id : Entity_Id;
21963 Error_Node : Node_Id;
21964 Full_View_Id : Entity_Id;
21965 Subtype_Mark_Id : Entity_Id;
21966
21967 May_Have_Null_Exclusion : Boolean;
21968
21969 -- Start of processing for Process_Subtype
21970
21971 begin
21972 -- Case of no constraints present
21973
21974 if Nkind (S) /= N_Subtype_Indication then
21975 Find_Type (S);
21976
21977 -- No way to proceed if the subtype indication is malformed. This
21978 -- will happen for example when the subtype indication in an object
21979 -- declaration is missing altogether and the expression is analyzed
21980 -- as if it were that indication.
21981
21982 if not Is_Entity_Name (S) then
21983 return Any_Type;
21984 end if;
21985
21986 Check_Incomplete (S);
21987 P := Parent (S);
21988
21989 -- The following mirroring of assertion in Null_Exclusion_Present is
21990 -- ugly, can't we have a range, a static predicate or even a flag???
21991
21992 May_Have_Null_Exclusion :=
21993 Present (P)
21994 and then
21995 Nkind (P) in N_Access_Definition
21996 | N_Access_Function_Definition
21997 | N_Access_Procedure_Definition
21998 | N_Access_To_Object_Definition
21999 | N_Allocator
22000 | N_Component_Definition
22001 | N_Derived_Type_Definition
22002 | N_Discriminant_Specification
22003 | N_Formal_Object_Declaration
22004 | N_Function_Specification
22005 | N_Object_Declaration
22006 | N_Object_Renaming_Declaration
22007 | N_Parameter_Specification
22008 | N_Subtype_Declaration;
22009
22010 -- Ada 2005 (AI-231): Static check
22011
22012 if Ada_Version >= Ada_2005
22013 and then May_Have_Null_Exclusion
22014 and then Null_Exclusion_Present (P)
22015 and then Nkind (P) /= N_Access_To_Object_Definition
22016 and then not Is_Access_Type (Entity (S))
22017 then
22018 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22019 end if;
22020
22021 -- Create an Itype that is a duplicate of Entity (S) but with the
22022 -- null-exclusion attribute.
22023
22024 if May_Have_Null_Exclusion
22025 and then Is_Access_Type (Entity (S))
22026 and then Null_Exclusion_Present (P)
22027
22028 -- No need to check the case of an access to object definition.
22029 -- It is correct to define double not-null pointers.
22030
22031 -- Example:
22032 -- type Not_Null_Int_Ptr is not null access Integer;
22033 -- type Acc is not null access Not_Null_Int_Ptr;
22034
22035 and then Nkind (P) /= N_Access_To_Object_Definition
22036 then
22037 if Can_Never_Be_Null (Entity (S)) then
22038 case Nkind (Related_Nod) is
22039 when N_Full_Type_Declaration =>
22040 if Nkind (Type_Definition (Related_Nod))
22041 in N_Array_Type_Definition
22042 then
22043 Error_Node :=
22044 Subtype_Indication
22045 (Component_Definition
22046 (Type_Definition (Related_Nod)));
22047 else
22048 Error_Node :=
22049 Subtype_Indication (Type_Definition (Related_Nod));
22050 end if;
22051
22052 when N_Subtype_Declaration =>
22053 Error_Node := Subtype_Indication (Related_Nod);
22054
22055 when N_Object_Declaration =>
22056 Error_Node := Object_Definition (Related_Nod);
22057
22058 when N_Component_Declaration =>
22059 Error_Node :=
22060 Subtype_Indication (Component_Definition (Related_Nod));
22061
22062 when N_Allocator =>
22063 Error_Node := Expression (Related_Nod);
22064
22065 when others =>
22066 pragma Assert (False);
22067 Error_Node := Related_Nod;
22068 end case;
22069
22070 Error_Msg_NE
22071 ("`NOT NULL` not allowed (& already excludes null)",
22072 Error_Node,
22073 Entity (S));
22074 end if;
22075
22076 Set_Etype (S,
22077 Create_Null_Excluding_Itype
22078 (T => Entity (S),
22079 Related_Nod => P));
22080 Set_Entity (S, Etype (S));
22081 end if;
22082
22083 return Entity (S);
22084
22085 -- Case of constraint present, so that we have an N_Subtype_Indication
22086 -- node (this node is created only if constraints are present).
22087
22088 else
22089 Find_Type (Subtype_Mark (S));
22090
22091 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22092 and then not
22093 (Nkind (Parent (S)) = N_Subtype_Declaration
22094 and then Is_Itype (Defining_Identifier (Parent (S))))
22095 then
22096 Check_Incomplete (Subtype_Mark (S));
22097 end if;
22098
22099 P := Parent (S);
22100 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22101
22102 -- Explicit subtype declaration case
22103
22104 if Nkind (P) = N_Subtype_Declaration then
22105 Def_Id := Defining_Identifier (P);
22106
22107 -- Explicit derived type definition case
22108
22109 elsif Nkind (P) = N_Derived_Type_Definition then
22110 Def_Id := Defining_Identifier (Parent (P));
22111
22112 -- Implicit case, the Def_Id must be created as an implicit type.
22113 -- The one exception arises in the case of concurrent types, array
22114 -- and access types, where other subsidiary implicit types may be
22115 -- created and must appear before the main implicit type. In these
22116 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22117 -- has not yet been called to create Def_Id.
22118
22119 else
22120 if Is_Array_Type (Subtype_Mark_Id)
22121 or else Is_Concurrent_Type (Subtype_Mark_Id)
22122 or else Is_Access_Type (Subtype_Mark_Id)
22123 then
22124 Def_Id := Empty;
22125
22126 -- For the other cases, we create a new unattached Itype,
22127 -- and set the indication to ensure it gets attached later.
22128
22129 else
22130 Def_Id :=
22131 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22132 end if;
22133 end if;
22134
22135 -- If the kind of constraint is invalid for this kind of type,
22136 -- then give an error, and then pretend no constraint was given.
22137
22138 if not Is_Valid_Constraint_Kind
22139 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22140 then
22141 Error_Msg_N
22142 ("incorrect constraint for this kind of type", Constraint (S));
22143
22144 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22145
22146 -- Set Ekind of orphan itype, to prevent cascaded errors
22147
22148 if Present (Def_Id) then
22149 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22150 end if;
22151
22152 -- Make recursive call, having got rid of the bogus constraint
22153
22154 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22155 end if;
22156
22157 -- Remaining processing depends on type. Select on Base_Type kind to
22158 -- ensure getting to the concrete type kind in the case of a private
22159 -- subtype (needed when only doing semantic analysis).
22160
22161 case Ekind (Base_Type (Subtype_Mark_Id)) is
22162 when Access_Kind =>
22163
22164 -- If this is a constraint on a class-wide type, discard it.
22165 -- There is currently no way to express a partial discriminant
22166 -- constraint on a type with unknown discriminants. This is
22167 -- a pathology that the ACATS wisely decides not to test.
22168
22169 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22170 if Comes_From_Source (S) then
22171 Error_Msg_N
22172 ("constraint on class-wide type ignored??",
22173 Constraint (S));
22174 end if;
22175
22176 if Nkind (P) = N_Subtype_Declaration then
22177 Set_Subtype_Indication (P,
22178 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22179 end if;
22180
22181 return Subtype_Mark_Id;
22182 end if;
22183
22184 Constrain_Access (Def_Id, S, Related_Nod);
22185
22186 if Expander_Active
22187 and then Is_Itype (Designated_Type (Def_Id))
22188 and then Nkind (Related_Nod) = N_Subtype_Declaration
22189 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22190 then
22191 Build_Itype_Reference
22192 (Designated_Type (Def_Id), Related_Nod);
22193 end if;
22194
22195 when Array_Kind =>
22196 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22197
22198 when Decimal_Fixed_Point_Kind =>
22199 Constrain_Decimal (Def_Id, S);
22200
22201 when Enumeration_Kind =>
22202 Constrain_Enumeration (Def_Id, S);
22203
22204 when Ordinary_Fixed_Point_Kind =>
22205 Constrain_Ordinary_Fixed (Def_Id, S);
22206
22207 when Float_Kind =>
22208 Constrain_Float (Def_Id, S);
22209
22210 when Integer_Kind =>
22211 Constrain_Integer (Def_Id, S);
22212
22213 when Class_Wide_Kind
22214 | E_Incomplete_Type
22215 | E_Record_Subtype
22216 | E_Record_Type
22217 =>
22218 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22219
22220 if Ekind (Def_Id) = E_Incomplete_Type then
22221 Set_Private_Dependents (Def_Id, New_Elmt_List);
22222 end if;
22223
22224 when Private_Kind =>
22225
22226 -- A private type with unknown discriminants may be completed
22227 -- by an unconstrained array type.
22228
22229 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22230 and then Present (Full_View (Subtype_Mark_Id))
22231 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22232 then
22233 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22234
22235 -- ... but more commonly is completed by a discriminated record
22236 -- type.
22237
22238 else
22239 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22240 end if;
22241
22242 -- The base type may be private but Def_Id may be a full view
22243 -- in an instance.
22244
22245 if Is_Private_Type (Def_Id) then
22246 Set_Private_Dependents (Def_Id, New_Elmt_List);
22247 end if;
22248
22249 -- In case of an invalid constraint prevent further processing
22250 -- since the type constructed is missing expected fields.
22251
22252 if Etype (Def_Id) = Any_Type then
22253 return Def_Id;
22254 end if;
22255
22256 -- If the full view is that of a task with discriminants,
22257 -- we must constrain both the concurrent type and its
22258 -- corresponding record type. Otherwise we will just propagate
22259 -- the constraint to the full view, if available.
22260
22261 if Present (Full_View (Subtype_Mark_Id))
22262 and then Has_Discriminants (Subtype_Mark_Id)
22263 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22264 then
22265 Full_View_Id :=
22266 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22267
22268 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22269 Constrain_Concurrent (Full_View_Id, S,
22270 Related_Nod, Related_Id, Suffix);
22271 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22272 Set_Full_View (Def_Id, Full_View_Id);
22273
22274 -- Introduce an explicit reference to the private subtype,
22275 -- to prevent scope anomalies in gigi if first use appears
22276 -- in a nested context, e.g. a later function body.
22277 -- Should this be generated in other contexts than a full
22278 -- type declaration?
22279
22280 if Is_Itype (Def_Id)
22281 and then
22282 Nkind (Parent (P)) = N_Full_Type_Declaration
22283 then
22284 Build_Itype_Reference (Def_Id, Parent (P));
22285 end if;
22286
22287 else
22288 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22289 end if;
22290
22291 when Concurrent_Kind =>
22292 Constrain_Concurrent (Def_Id, S,
22293 Related_Nod, Related_Id, Suffix);
22294
22295 when others =>
22296 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22297 end case;
22298
22299 -- Size, Alignment, Representation aspects and Convention are always
22300 -- inherited from the base type.
22301
22302 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22303 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22304 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22305
22306 -- The anonymous subtype created for the subtype indication
22307 -- inherits the predicates of the parent.
22308
22309 if Has_Predicates (Subtype_Mark_Id) then
22310 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22311
22312 -- Indicate where the predicate function may be found
22313
22314 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22315 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22316 end if;
22317 end if;
22318
22319 return Def_Id;
22320 end if;
22321 end Process_Subtype;
22322
22323 -----------------------------
22324 -- Record_Type_Declaration --
22325 -----------------------------
22326
22327 procedure Record_Type_Declaration
22328 (T : Entity_Id;
22329 N : Node_Id;
22330 Prev : Entity_Id)
22331 is
22332 Def : constant Node_Id := Type_Definition (N);
22333 Is_Tagged : Boolean;
22334 Tag_Comp : Entity_Id;
22335
22336 begin
22337 -- These flags must be initialized before calling Process_Discriminants
22338 -- because this routine makes use of them.
22339
22340 Mutate_Ekind (T, E_Record_Type);
22341 Set_Etype (T, T);
22342 Reinit_Size_Align (T);
22343 Set_Interfaces (T, No_Elist);
22344 Set_Stored_Constraint (T, No_Elist);
22345 Set_Default_SSO (T);
22346 Set_No_Reordering (T, No_Component_Reordering);
22347
22348 -- Normal case
22349
22350 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22351 -- The flag Is_Tagged_Type might have already been set by
22352 -- Find_Type_Name if it detected an error for declaration T. This
22353 -- arises in the case of private tagged types where the full view
22354 -- omits the word tagged.
22355
22356 Is_Tagged :=
22357 Tagged_Present (Def)
22358 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22359
22360 Set_Is_Limited_Record (T, Limited_Present (Def));
22361
22362 if Is_Tagged then
22363 Set_Is_Tagged_Type (T, True);
22364 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22365 end if;
22366
22367 -- Type is abstract if full declaration carries keyword, or if
22368 -- previous partial view did.
22369
22370 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22371 or else Abstract_Present (Def));
22372
22373 else
22374 Is_Tagged := True;
22375 Analyze_Interface_Declaration (T, Def);
22376
22377 if Present (Discriminant_Specifications (N)) then
22378 Error_Msg_N
22379 ("interface types cannot have discriminants",
22380 Defining_Identifier
22381 (First (Discriminant_Specifications (N))));
22382 end if;
22383 end if;
22384
22385 -- First pass: if there are self-referential access components,
22386 -- create the required anonymous access type declarations, and if
22387 -- need be an incomplete type declaration for T itself.
22388
22389 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22390
22391 if Ada_Version >= Ada_2005
22392 and then Present (Interface_List (Def))
22393 then
22394 Check_Interfaces (N, Def);
22395
22396 declare
22397 Ifaces_List : Elist_Id;
22398
22399 begin
22400 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22401 -- already in the parents.
22402
22403 Collect_Interfaces
22404 (T => T,
22405 Ifaces_List => Ifaces_List,
22406 Exclude_Parents => True);
22407
22408 Set_Interfaces (T, Ifaces_List);
22409 end;
22410 end if;
22411
22412 -- Records constitute a scope for the component declarations within.
22413 -- The scope is created prior to the processing of these declarations.
22414 -- Discriminants are processed first, so that they are visible when
22415 -- processing the other components. The Ekind of the record type itself
22416 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22417
22418 -- Enter record scope
22419
22420 Push_Scope (T);
22421
22422 -- If an incomplete or private type declaration was already given for
22423 -- the type, then this scope already exists, and the discriminants have
22424 -- been declared within. We must verify that the full declaration
22425 -- matches the incomplete one.
22426
22427 Check_Or_Process_Discriminants (N, T, Prev);
22428
22429 Set_Is_Constrained (T, not Has_Discriminants (T));
22430 Set_Has_Delayed_Freeze (T, True);
22431
22432 -- For tagged types add a manually analyzed component corresponding
22433 -- to the component _tag, the corresponding piece of tree will be
22434 -- expanded as part of the freezing actions if it is not a CPP_Class.
22435
22436 if Is_Tagged then
22437
22438 -- Do not add the tag unless we are in expansion mode
22439
22440 if Expander_Active then
22441 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22442 Enter_Name (Tag_Comp);
22443
22444 Mutate_Ekind (Tag_Comp, E_Component);
22445 Set_Is_Tag (Tag_Comp);
22446 Set_Is_Aliased (Tag_Comp);
22447 Set_Is_Independent (Tag_Comp);
22448 Set_Etype (Tag_Comp, RTE (RE_Tag));
22449 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22450 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22451 Reinit_Component_Location (Tag_Comp);
22452
22453 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22454 -- implemented interfaces.
22455
22456 if Has_Interfaces (T) then
22457 Add_Interface_Tag_Components (N, T);
22458 end if;
22459 end if;
22460
22461 Make_Class_Wide_Type (T);
22462 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22463 end if;
22464
22465 -- We must suppress range checks when processing record components in
22466 -- the presence of discriminants, since we don't want spurious checks to
22467 -- be generated during their analysis, but Suppress_Range_Checks flags
22468 -- must be reset the after processing the record definition.
22469
22470 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22471 -- couldn't we just use the normal range check suppression method here.
22472 -- That would seem cleaner ???
22473
22474 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22475 Set_Kill_Range_Checks (T, True);
22476 Record_Type_Definition (Def, Prev);
22477 Set_Kill_Range_Checks (T, False);
22478 else
22479 Record_Type_Definition (Def, Prev);
22480 end if;
22481
22482 -- Exit from record scope
22483
22484 End_Scope;
22485
22486 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22487 -- the implemented interfaces and associate them an aliased entity.
22488
22489 if Is_Tagged
22490 and then not Is_Empty_List (Interface_List (Def))
22491 then
22492 Derive_Progenitor_Subprograms (T, T);
22493 end if;
22494
22495 Check_Function_Writable_Actuals (N);
22496 end Record_Type_Declaration;
22497
22498 ----------------------------
22499 -- Record_Type_Definition --
22500 ----------------------------
22501
22502 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22503 Component : Entity_Id;
22504 Ctrl_Components : Boolean := False;
22505 Final_Storage_Only : Boolean;
22506 T : Entity_Id;
22507
22508 begin
22509 if Ekind (Prev_T) = E_Incomplete_Type then
22510 T := Full_View (Prev_T);
22511 else
22512 T := Prev_T;
22513 end if;
22514
22515 Final_Storage_Only := not Is_Controlled (T);
22516
22517 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22518 -- type declaration.
22519
22520 if Parent_Kind (Def) = N_Derived_Type_Definition
22521 and then Limited_Present (Parent (Def))
22522 then
22523 Set_Is_Limited_Record (T);
22524 end if;
22525
22526 -- If the component list of a record type is defined by the reserved
22527 -- word null and there is no discriminant part, then the record type has
22528 -- no components and all records of the type are null records (RM 3.7)
22529 -- This procedure is also called to process the extension part of a
22530 -- record extension, in which case the current scope may have inherited
22531 -- components.
22532
22533 if Present (Def)
22534 and then Present (Component_List (Def))
22535 and then not Null_Present (Component_List (Def))
22536 then
22537 Analyze_Declarations (Component_Items (Component_List (Def)));
22538
22539 if Present (Variant_Part (Component_List (Def))) then
22540 Analyze (Variant_Part (Component_List (Def)));
22541 end if;
22542 end if;
22543
22544 -- After completing the semantic analysis of the record definition,
22545 -- record components, both new and inherited, are accessible. Set their
22546 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22547 -- whose Ekind may be void.
22548
22549 Component := First_Entity (Current_Scope);
22550 while Present (Component) loop
22551 if Ekind (Component) = E_Void
22552 and then not Is_Itype (Component)
22553 then
22554 Mutate_Ekind (Component, E_Component);
22555 Reinit_Component_Location (Component);
22556 end if;
22557
22558 Propagate_Concurrent_Flags (T, Etype (Component));
22559
22560 if Ekind (Component) /= E_Component then
22561 null;
22562
22563 -- Do not set Has_Controlled_Component on a class-wide equivalent
22564 -- type. See Make_CW_Equivalent_Type.
22565
22566 elsif not Is_Class_Wide_Equivalent_Type (T)
22567 and then (Has_Controlled_Component (Etype (Component))
22568 or else (Chars (Component) /= Name_uParent
22569 and then Is_Controlled (Etype (Component))))
22570 then
22571 Set_Has_Controlled_Component (T, True);
22572 Final_Storage_Only :=
22573 Final_Storage_Only
22574 and then Finalize_Storage_Only (Etype (Component));
22575 Ctrl_Components := True;
22576 end if;
22577
22578 Next_Entity (Component);
22579 end loop;
22580
22581 -- A Type is Finalize_Storage_Only only if all its controlled components
22582 -- are also.
22583
22584 if Ctrl_Components then
22585 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22586 end if;
22587
22588 -- Place reference to end record on the proper entity, which may
22589 -- be a partial view.
22590
22591 if Present (Def) then
22592 Process_End_Label (Def, 'e', Prev_T);
22593 end if;
22594 end Record_Type_Definition;
22595
22596 ---------------------------
22597 -- Replace_Discriminants --
22598 ---------------------------
22599
22600 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22601 function Process (N : Node_Id) return Traverse_Result;
22602
22603 -------------
22604 -- Process --
22605 -------------
22606
22607 function Process (N : Node_Id) return Traverse_Result is
22608 Comp : Entity_Id;
22609
22610 begin
22611 if Nkind (N) = N_Discriminant_Specification then
22612 Comp := First_Discriminant (Typ);
22613 while Present (Comp) loop
22614 if Original_Record_Component (Comp) = Defining_Identifier (N)
22615 or else Chars (Comp) = Chars (Defining_Identifier (N))
22616 then
22617 Set_Defining_Identifier (N, Comp);
22618 exit;
22619 end if;
22620
22621 Next_Discriminant (Comp);
22622 end loop;
22623
22624 elsif Nkind (N) = N_Variant_Part then
22625 Comp := First_Discriminant (Typ);
22626 while Present (Comp) loop
22627 if Original_Record_Component (Comp) = Entity (Name (N))
22628 or else Chars (Comp) = Chars (Name (N))
22629 then
22630 -- Make sure to preserve the type coming from the parent on
22631 -- the Name, even if the subtype of the discriminant can be
22632 -- constrained, so that discrete choices inherited from the
22633 -- parent in the variant part are not flagged as violating
22634 -- the constraints of the subtype.
22635
22636 declare
22637 Typ : constant Entity_Id := Etype (Name (N));
22638 begin
22639 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22640 Set_Etype (Name (N), Typ);
22641 end;
22642 exit;
22643 end if;
22644
22645 Next_Discriminant (Comp);
22646 end loop;
22647 end if;
22648
22649 return OK;
22650 end Process;
22651
22652 procedure Replace is new Traverse_Proc (Process);
22653
22654 -- Start of processing for Replace_Discriminants
22655
22656 begin
22657 Replace (Decl);
22658 end Replace_Discriminants;
22659
22660 -------------------------------
22661 -- Set_Completion_Referenced --
22662 -------------------------------
22663
22664 procedure Set_Completion_Referenced (E : Entity_Id) is
22665 begin
22666 -- If in main unit, mark entity that is a completion as referenced,
22667 -- warnings go on the partial view when needed.
22668
22669 if In_Extended_Main_Source_Unit (E) then
22670 Set_Referenced (E);
22671 end if;
22672 end Set_Completion_Referenced;
22673
22674 ---------------------
22675 -- Set_Default_SSO --
22676 ---------------------
22677
22678 procedure Set_Default_SSO (T : Entity_Id) is
22679 begin
22680 case Opt.Default_SSO is
22681 when ' ' =>
22682 null;
22683 when 'L' =>
22684 Set_SSO_Set_Low_By_Default (T, True);
22685 when 'H' =>
22686 Set_SSO_Set_High_By_Default (T, True);
22687 when others =>
22688 raise Program_Error;
22689 end case;
22690 end Set_Default_SSO;
22691
22692 ---------------------
22693 -- Set_Fixed_Range --
22694 ---------------------
22695
22696 -- The range for fixed-point types is complicated by the fact that we
22697 -- do not know the exact end points at the time of the declaration. This
22698 -- is true for three reasons:
22699
22700 -- A size clause may affect the fudging of the end-points.
22701 -- A small clause may affect the values of the end-points.
22702 -- We try to include the end-points if it does not affect the size.
22703
22704 -- This means that the actual end-points must be established at the
22705 -- point when the type is frozen. Meanwhile, we first narrow the range
22706 -- as permitted (so that it will fit if necessary in a small specified
22707 -- size), and then build a range subtree with these narrowed bounds.
22708 -- Set_Fixed_Range constructs the range from real literal values, and
22709 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22710
22711 -- The parent of this range is set to point to the entity so that it is
22712 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22713 -- other scalar types, which are just pointers to the range in the
22714 -- original tree, this would otherwise be an orphan).
22715
22716 -- The tree is left unanalyzed. When the type is frozen, the processing
22717 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22718 -- analyzed, and uses this as an indication that it should complete
22719 -- work on the range (it will know the final small and size values).
22720
22721 procedure Set_Fixed_Range
22722 (E : Entity_Id;
22723 Loc : Source_Ptr;
22724 Lo : Ureal;
22725 Hi : Ureal)
22726 is
22727 S : constant Node_Id :=
22728 Make_Range (Loc,
22729 Low_Bound => Make_Real_Literal (Loc, Lo),
22730 High_Bound => Make_Real_Literal (Loc, Hi));
22731 begin
22732 Set_Scalar_Range (E, S);
22733 Set_Parent (S, E);
22734
22735 -- Before the freeze point, the bounds of a fixed point are universal
22736 -- and carry the corresponding type.
22737
22738 Set_Etype (Low_Bound (S), Universal_Real);
22739 Set_Etype (High_Bound (S), Universal_Real);
22740 end Set_Fixed_Range;
22741
22742 ----------------------------------
22743 -- Set_Scalar_Range_For_Subtype --
22744 ----------------------------------
22745
22746 procedure Set_Scalar_Range_For_Subtype
22747 (Def_Id : Entity_Id;
22748 R : Node_Id;
22749 Subt : Entity_Id)
22750 is
22751 Kind : constant Entity_Kind := Ekind (Def_Id);
22752
22753 begin
22754 -- Defend against previous error
22755
22756 if Nkind (R) = N_Error then
22757 return;
22758 end if;
22759
22760 Set_Scalar_Range (Def_Id, R);
22761
22762 -- We need to link the range into the tree before resolving it so
22763 -- that types that are referenced, including importantly the subtype
22764 -- itself, are properly frozen (Freeze_Expression requires that the
22765 -- expression be properly linked into the tree). Of course if it is
22766 -- already linked in, then we do not disturb the current link.
22767
22768 if No (Parent (R)) then
22769 Set_Parent (R, Def_Id);
22770 end if;
22771
22772 -- Reset the kind of the subtype during analysis of the range, to
22773 -- catch possible premature use in the bounds themselves.
22774
22775 Mutate_Ekind (Def_Id, E_Void);
22776 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22777 Mutate_Ekind (Def_Id, Kind);
22778 end Set_Scalar_Range_For_Subtype;
22779
22780 --------------------------------------------------------
22781 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22782 --------------------------------------------------------
22783
22784 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22785 (E : Entity_Id)
22786 is
22787 begin
22788 -- Make sure set if encountered during Expand_To_Stored_Constraint
22789
22790 Set_Stored_Constraint (E, No_Elist);
22791
22792 -- Give it the right value
22793
22794 if Is_Constrained (E) and then Has_Discriminants (E) then
22795 Set_Stored_Constraint (E,
22796 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22797 end if;
22798 end Set_Stored_Constraint_From_Discriminant_Constraint;
22799
22800 -------------------------------------
22801 -- Signed_Integer_Type_Declaration --
22802 -------------------------------------
22803
22804 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22805 Implicit_Base : Entity_Id;
22806 Base_Typ : Entity_Id;
22807 Lo_Val : Uint;
22808 Hi_Val : Uint;
22809 Errs : Boolean := False;
22810 Lo : Node_Id;
22811 Hi : Node_Id;
22812
22813 function Can_Derive_From (E : Entity_Id) return Boolean;
22814 -- Determine whether given bounds allow derivation from specified type
22815
22816 procedure Check_Bound (Expr : Node_Id);
22817 -- Check bound to make sure it is integral and static. If not, post
22818 -- appropriate error message and set Errs flag
22819
22820 ---------------------
22821 -- Can_Derive_From --
22822 ---------------------
22823
22824 -- Note we check both bounds against both end values, to deal with
22825 -- strange types like ones with a range of 0 .. -12341234.
22826
22827 function Can_Derive_From (E : Entity_Id) return Boolean is
22828 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22829 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22830 begin
22831 return Lo <= Lo_Val and then Lo_Val <= Hi
22832 and then
22833 Lo <= Hi_Val and then Hi_Val <= Hi;
22834 end Can_Derive_From;
22835
22836 -----------------
22837 -- Check_Bound --
22838 -----------------
22839
22840 procedure Check_Bound (Expr : Node_Id) is
22841 begin
22842 -- If a range constraint is used as an integer type definition, each
22843 -- bound of the range must be defined by a static expression of some
22844 -- integer type, but the two bounds need not have the same integer
22845 -- type (Negative bounds are allowed.) (RM 3.5.4)
22846
22847 if not Is_Integer_Type (Etype (Expr)) then
22848 Error_Msg_N
22849 ("integer type definition bounds must be of integer type", Expr);
22850 Errs := True;
22851
22852 elsif not Is_OK_Static_Expression (Expr) then
22853 Flag_Non_Static_Expr
22854 ("non-static expression used for integer type bound!", Expr);
22855 Errs := True;
22856
22857 -- Otherwise the bounds are folded into literals
22858
22859 elsif Is_Entity_Name (Expr) then
22860 Fold_Uint (Expr, Expr_Value (Expr), True);
22861 end if;
22862 end Check_Bound;
22863
22864 -- Start of processing for Signed_Integer_Type_Declaration
22865
22866 begin
22867 -- Create an anonymous base type
22868
22869 Implicit_Base :=
22870 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22871
22872 -- Analyze and check the bounds, they can be of any integer type
22873
22874 Lo := Low_Bound (Def);
22875 Hi := High_Bound (Def);
22876
22877 -- Arbitrarily use Integer as the type if either bound had an error
22878
22879 if Hi = Error or else Lo = Error then
22880 Base_Typ := Any_Integer;
22881 Set_Error_Posted (T, True);
22882 Errs := True;
22883
22884 -- Here both bounds are OK expressions
22885
22886 else
22887 Analyze_And_Resolve (Lo, Any_Integer);
22888 Analyze_And_Resolve (Hi, Any_Integer);
22889
22890 Check_Bound (Lo);
22891 Check_Bound (Hi);
22892
22893 if Errs then
22894 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
22895 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
22896 end if;
22897
22898 -- Find type to derive from
22899
22900 Lo_Val := Expr_Value (Lo);
22901 Hi_Val := Expr_Value (Hi);
22902
22903 if Can_Derive_From (Standard_Short_Short_Integer) then
22904 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22905
22906 elsif Can_Derive_From (Standard_Short_Integer) then
22907 Base_Typ := Base_Type (Standard_Short_Integer);
22908
22909 elsif Can_Derive_From (Standard_Integer) then
22910 Base_Typ := Base_Type (Standard_Integer);
22911
22912 elsif Can_Derive_From (Standard_Long_Integer) then
22913 Base_Typ := Base_Type (Standard_Long_Integer);
22914
22915 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22916 Check_Restriction (No_Long_Long_Integers, Def);
22917 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22918
22919 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
22920 Check_Restriction (No_Long_Long_Integers, Def);
22921 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
22922
22923 else
22924 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
22925 Error_Msg_N ("integer type definition bounds out of range", Def);
22926 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
22927 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
22928 end if;
22929 end if;
22930
22931 -- Set the type of the bounds to the implicit base: we cannot set it to
22932 -- the new type, because this would be a forward reference for the code
22933 -- generator and, if the original type is user-defined, this could even
22934 -- lead to spurious semantic errors. Furthermore we do not set it to be
22935 -- universal, because this could make it much larger than needed here.
22936
22937 if not Errs then
22938 Set_Etype (Lo, Implicit_Base);
22939 Set_Etype (Hi, Implicit_Base);
22940 end if;
22941
22942 -- Complete both implicit base and declared first subtype entities. The
22943 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22944 -- are not clobbered when the signed integer type acts as a full view of
22945 -- a private type.
22946
22947 Set_Etype (Implicit_Base, Base_Typ);
22948 Set_Size_Info (Implicit_Base, Base_Typ);
22949 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22950 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22951 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22952
22953 Mutate_Ekind (T, E_Signed_Integer_Subtype);
22954 Set_Etype (T, Implicit_Base);
22955 Set_Size_Info (T, Implicit_Base);
22956 Inherit_Rep_Item_Chain (T, Implicit_Base);
22957 Set_Scalar_Range (T, Def);
22958 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22959 Set_Is_Constrained (T);
22960 end Signed_Integer_Type_Declaration;
22961
22962 end Sem_Ch3;
This page took 1.103417 seconds and 5 git commands to generate.