]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch3.adb
4c3212d3dee0f9c24fab560bcedd65046c2313d3
[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-2020, 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 Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elab; use Sem_Elab;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83
84 package body Sem_Ch3 is
85
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
89
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
93 -- record type.
94
95 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
96 -- When an access-to-subprogram type has pre/postconditions, we build a
97 -- subprogram that includes these contracts and is invoked by an indirect
98 -- call through the corresponding access type.
99
100 procedure Build_Derived_Type
101 (N : Node_Id;
102 Parent_Type : Entity_Id;
103 Derived_Type : Entity_Id;
104 Is_Completion : Boolean;
105 Derive_Subps : Boolean := True);
106 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
107 -- the N_Full_Type_Declaration node containing the derived type definition.
108 -- Parent_Type is the entity for the parent type in the derived type
109 -- definition and Derived_Type the actual derived type. Is_Completion must
110 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
111 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
112 -- completion of a private type declaration. If Is_Completion is set to
113 -- True, N is the completion of a private type declaration and Derived_Type
114 -- is different from the defining identifier inside N (i.e. Derived_Type /=
115 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
116 -- subprograms should be derived. The only case where this parameter is
117 -- False is when Build_Derived_Type is recursively called to process an
118 -- implicit derived full type for a type derived from a private type (in
119 -- that case the subprograms must only be derived for the private view of
120 -- the type).
121 --
122 -- ??? These flags need a bit of re-examination and re-documentation:
123 -- ??? are they both necessary (both seem related to the recursion)?
124
125 procedure Build_Derived_Access_Type
126 (N : Node_Id;
127 Parent_Type : Entity_Id;
128 Derived_Type : Entity_Id);
129 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
130 -- create an implicit base if the parent type is constrained or if the
131 -- subtype indication has a constraint.
132
133 procedure Build_Derived_Array_Type
134 (N : Node_Id;
135 Parent_Type : Entity_Id;
136 Derived_Type : Entity_Id);
137 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
138 -- create an implicit base if the parent type is constrained or if the
139 -- subtype indication has a constraint.
140
141 procedure Build_Derived_Concurrent_Type
142 (N : Node_Id;
143 Parent_Type : Entity_Id;
144 Derived_Type : Entity_Id);
145 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
146 -- protected type, inherit entries and protected subprograms, check
147 -- legality of discriminant constraints if any.
148
149 procedure Build_Derived_Enumeration_Type
150 (N : Node_Id;
151 Parent_Type : Entity_Id;
152 Derived_Type : Entity_Id);
153 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
154 -- type, we must create a new list of literals. Types derived from
155 -- Character and [Wide_]Wide_Character are special-cased.
156
157 procedure Build_Derived_Numeric_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id);
161 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
162 -- an anonymous base type, and propagate constraint to subtype if needed.
163
164 procedure Build_Derived_Private_Type
165 (N : Node_Id;
166 Parent_Type : Entity_Id;
167 Derived_Type : Entity_Id;
168 Is_Completion : Boolean;
169 Derive_Subps : Boolean := True);
170 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
171 -- because the parent may or may not have a completion, and the derivation
172 -- may itself be a completion.
173
174 procedure Build_Derived_Record_Type
175 (N : Node_Id;
176 Parent_Type : Entity_Id;
177 Derived_Type : Entity_Id;
178 Derive_Subps : Boolean := True);
179 -- Subsidiary procedure used for tagged and untagged record types
180 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
181 -- All parameters are as in Build_Derived_Type except that N, in
182 -- addition to being an N_Full_Type_Declaration node, can also be an
183 -- N_Private_Extension_Declaration node. See the definition of this routine
184 -- for much more info. Derive_Subps indicates whether subprograms should be
185 -- derived from the parent type. The only case where Derive_Subps is False
186 -- is for an implicit derived full type for a type derived from a private
187 -- type (see Build_Derived_Type).
188
189 procedure Build_Discriminal (Discrim : Entity_Id);
190 -- Create the discriminal corresponding to discriminant Discrim, that is
191 -- the parameter corresponding to Discrim to be used in initialization
192 -- procedures for the type where Discrim is a discriminant. Discriminals
193 -- are not used during semantic analysis, and are not fully defined
194 -- entities until expansion. Thus they are not given a scope until
195 -- initialization procedures are built.
196
197 function Build_Discriminant_Constraints
198 (T : Entity_Id;
199 Def : Node_Id;
200 Derived_Def : Boolean := False) return Elist_Id;
201 -- Validate discriminant constraints and return the list of the constraints
202 -- in order of discriminant declarations, where T is the discriminated
203 -- unconstrained type. Def is the N_Subtype_Indication node where the
204 -- discriminants constraints for T are specified. Derived_Def is True
205 -- when building the discriminant constraints in a derived type definition
206 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
207 -- type and Def is the constraint "(xxx)" on T and this routine sets the
208 -- Corresponding_Discriminant field of the discriminants in the derived
209 -- type D to point to the corresponding discriminants in the parent type T.
210
211 procedure Build_Discriminated_Subtype
212 (T : Entity_Id;
213 Def_Id : Entity_Id;
214 Elist : Elist_Id;
215 Related_Nod : Node_Id;
216 For_Access : Boolean := False);
217 -- Subsidiary procedure to Constrain_Discriminated_Type and to
218 -- Process_Incomplete_Dependents. Given
219 --
220 -- T (a possibly discriminated base type)
221 -- Def_Id (a very partially built subtype for T),
222 --
223 -- the call completes Def_Id to be the appropriate E_*_Subtype.
224 --
225 -- The Elist is the list of discriminant constraints if any (it is set
226 -- to No_Elist if T is not a discriminated type, and to an empty list if
227 -- T has discriminants but there are no discriminant constraints). The
228 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
229 -- The For_Access says whether or not this subtype is really constraining
230 -- an access type.
231
232 function Build_Scalar_Bound
233 (Bound : Node_Id;
234 Par_T : Entity_Id;
235 Der_T : Entity_Id) return Node_Id;
236 -- The bounds of a derived scalar type are conversions of the bounds of
237 -- the parent type. Optimize the representation if the bounds are literals.
238 -- Needs a more complete spec--what are the parameters exactly, and what
239 -- exactly is the returned value, and how is Bound affected???
240
241 procedure Check_Access_Discriminant_Requires_Limited
242 (D : Node_Id;
243 Loc : Node_Id);
244 -- Check the restriction that the type to which an access discriminant
245 -- belongs must be a concurrent type or a descendant of a type with
246 -- the reserved word 'limited' in its declaration.
247
248 procedure Check_Anonymous_Access_Components
249 (Typ_Decl : Node_Id;
250 Typ : Entity_Id;
251 Prev : Entity_Id;
252 Comp_List : Node_Id);
253 -- Ada 2005 AI-382: an access component in a record definition can refer to
254 -- the enclosing record, in which case it denotes the type itself, and not
255 -- the current instance of the type. We create an anonymous access type for
256 -- the component, and flag it as an access to a component, so accessibility
257 -- checks are properly performed on it. The declaration of the access type
258 -- is placed ahead of that of the record to prevent order-of-elaboration
259 -- circularity issues in Gigi. We create an incomplete type for the record
260 -- declaration, which is the designated type of the anonymous access.
261
262 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
263 -- Check that, if a new discriminant is used in a constraint defining the
264 -- parent subtype of a derivation, its subtype is statically compatible
265 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
266
267 procedure Check_Delta_Expression (E : Node_Id);
268 -- Check that the expression represented by E is suitable for use as a
269 -- delta expression, i.e. it is of real type and is static.
270
271 procedure Check_Digits_Expression (E : Node_Id);
272 -- Check that the expression represented by E is suitable for use as a
273 -- digits expression, i.e. it is of integer type, positive and static.
274
275 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
276 -- Validate the initialization of an object declaration. T is the required
277 -- type, and Exp is the initialization expression.
278
279 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
280 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
281
282 procedure Check_Or_Process_Discriminants
283 (N : Node_Id;
284 T : Entity_Id;
285 Prev : Entity_Id := Empty);
286 -- If N is the full declaration of the completion T of an incomplete or
287 -- private type, check its discriminants (which are already known to be
288 -- conformant with those of the partial view, see Find_Type_Name),
289 -- otherwise process them. Prev is the entity of the partial declaration,
290 -- if any.
291
292 procedure Check_Real_Bound (Bound : Node_Id);
293 -- Check given bound for being of real type and static. If not, post an
294 -- appropriate message, and rewrite the bound with the real literal zero.
295
296 procedure Constant_Redeclaration
297 (Id : Entity_Id;
298 N : Node_Id;
299 T : out Entity_Id);
300 -- Various checks on legality of full declaration of deferred constant.
301 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
302 -- node. The caller has not yet set any attributes of this entity.
303
304 function Contain_Interface
305 (Iface : Entity_Id;
306 Ifaces : Elist_Id) return Boolean;
307 -- Ada 2005: Determine whether Iface is present in the list Ifaces
308
309 procedure Convert_Scalar_Bounds
310 (N : Node_Id;
311 Parent_Type : Entity_Id;
312 Derived_Type : Entity_Id;
313 Loc : Source_Ptr);
314 -- For derived scalar types, convert the bounds in the type definition to
315 -- the derived type, and complete their analysis. Given a constraint of the
316 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
317 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
318 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
319 -- subtype are conversions of those bounds to the derived_type, so that
320 -- their typing is consistent.
321
322 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
323 -- Copies attributes from array base type T2 to array base type T1. Copies
324 -- only attributes that apply to base types, but not subtypes.
325
326 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
327 -- Copies attributes from array subtype T2 to array subtype T1. Copies
328 -- attributes that apply to both subtypes and base types.
329
330 procedure Create_Constrained_Components
331 (Subt : Entity_Id;
332 Decl_Node : Node_Id;
333 Typ : Entity_Id;
334 Constraints : Elist_Id);
335 -- Build the list of entities for a constrained discriminated record
336 -- subtype. If a component depends on a discriminant, replace its subtype
337 -- using the discriminant values in the discriminant constraint. Subt
338 -- is the defining identifier for the subtype whose list of constrained
339 -- entities we will create. Decl_Node is the type declaration node where
340 -- we will attach all the itypes created. Typ is the base discriminated
341 -- type for the subtype Subt. Constraints is the list of discriminant
342 -- constraints for Typ.
343
344 function Constrain_Component_Type
345 (Comp : Entity_Id;
346 Constrained_Typ : Entity_Id;
347 Related_Node : Node_Id;
348 Typ : Entity_Id;
349 Constraints : Elist_Id) return Entity_Id;
350 -- Given a discriminated base type Typ, a list of discriminant constraints,
351 -- Constraints, for Typ and a component Comp of Typ, create and return the
352 -- type corresponding to Etype (Comp) where all discriminant references
353 -- are replaced with the corresponding constraint. If Etype (Comp) contains
354 -- no discriminant references then it is returned as-is. Constrained_Typ
355 -- is the final constrained subtype to which the constrained component
356 -- belongs. Related_Node is the node where we attach all created itypes.
357
358 procedure Constrain_Access
359 (Def_Id : in out Entity_Id;
360 S : Node_Id;
361 Related_Nod : Node_Id);
362 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
363 -- an anonymous type created for a subtype indication. In that case it is
364 -- created in the procedure and attached to Related_Nod.
365
366 procedure Constrain_Array
367 (Def_Id : in out Entity_Id;
368 SI : Node_Id;
369 Related_Nod : Node_Id;
370 Related_Id : Entity_Id;
371 Suffix : Character);
372 -- Apply a list of index constraints to an unconstrained array type. The
373 -- first parameter is the entity for the resulting subtype. A value of
374 -- Empty for Def_Id indicates that an implicit type must be created, but
375 -- creation is delayed (and must be done by this procedure) because other
376 -- subsidiary implicit types must be created first (which is why Def_Id
377 -- is an in/out parameter). The second parameter is a subtype indication
378 -- node for the constrained array to be created (e.g. something of the
379 -- form string (1 .. 10)). Related_Nod gives the place where this type
380 -- has to be inserted in the tree. The Related_Id and Suffix parameters
381 -- are used to build the associated Implicit type name.
382
383 procedure Constrain_Concurrent
384 (Def_Id : in out Entity_Id;
385 SI : Node_Id;
386 Related_Nod : Node_Id;
387 Related_Id : Entity_Id;
388 Suffix : Character);
389 -- Apply list of discriminant constraints to an unconstrained concurrent
390 -- type.
391 --
392 -- SI is the N_Subtype_Indication node containing the constraint and
393 -- the unconstrained type to constrain.
394 --
395 -- Def_Id is the entity for the resulting constrained subtype. A value
396 -- of Empty for Def_Id indicates that an implicit type must be created,
397 -- but creation is delayed (and must be done by this procedure) because
398 -- other subsidiary implicit types must be created first (which is why
399 -- Def_Id is an in/out parameter).
400 --
401 -- Related_Nod gives the place where this type has to be inserted
402 -- in the tree.
403 --
404 -- The last two arguments are used to create its external name if needed.
405
406 function Constrain_Corresponding_Record
407 (Prot_Subt : Entity_Id;
408 Corr_Rec : Entity_Id;
409 Related_Nod : Node_Id) return Entity_Id;
410 -- When constraining a protected type or task type with discriminants,
411 -- constrain the corresponding record with the same discriminant values.
412
413 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
414 -- Constrain a decimal fixed point type with a digits constraint and/or a
415 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
416
417 procedure Constrain_Discriminated_Type
418 (Def_Id : Entity_Id;
419 S : Node_Id;
420 Related_Nod : Node_Id;
421 For_Access : Boolean := False);
422 -- Process discriminant constraints of composite type. Verify that values
423 -- have been provided for all discriminants, that the original type is
424 -- unconstrained, and that the types of the supplied expressions match
425 -- the discriminant types. The first three parameters are like in routine
426 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
427 -- of For_Access.
428
429 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
430 -- Constrain an enumeration type with a range constraint. This is identical
431 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
432
433 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
434 -- Constrain a floating point type with either a digits constraint
435 -- and/or a range constraint, building a E_Floating_Point_Subtype.
436
437 procedure Constrain_Index
438 (Index : Node_Id;
439 S : Node_Id;
440 Related_Nod : Node_Id;
441 Related_Id : Entity_Id;
442 Suffix : Character;
443 Suffix_Index : Nat);
444 -- Process an index constraint S in a constrained array declaration. The
445 -- constraint can be a subtype name, or a range with or without an explicit
446 -- subtype mark. The index is the corresponding index of the unconstrained
447 -- array. The Related_Id and Suffix parameters are used to build the
448 -- associated Implicit type name.
449
450 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
451 -- Build subtype of a signed or modular integer type
452
453 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
454 -- Constrain an ordinary fixed point type with a range constraint, and
455 -- build an E_Ordinary_Fixed_Point_Subtype entity.
456
457 procedure Copy_And_Swap (Priv, Full : Entity_Id);
458 -- Copy the Priv entity into the entity of its full declaration then swap
459 -- the two entities in such a manner that the former private type is now
460 -- seen as a full type.
461
462 procedure Decimal_Fixed_Point_Type_Declaration
463 (T : Entity_Id;
464 Def : Node_Id);
465 -- Create a new decimal fixed point type, and apply the constraint to
466 -- obtain a subtype of this new type.
467
468 procedure Complete_Private_Subtype
469 (Priv : Entity_Id;
470 Full : Entity_Id;
471 Full_Base : Entity_Id;
472 Related_Nod : Node_Id);
473 -- Complete the implicit full view of a private subtype by setting the
474 -- appropriate semantic fields. If the full view of the parent is a record
475 -- type, build constrained components of subtype.
476
477 procedure Derive_Progenitor_Subprograms
478 (Parent_Type : Entity_Id;
479 Tagged_Type : Entity_Id);
480 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
481 -- operations of progenitors of Tagged_Type, and replace the subsidiary
482 -- subtypes with Tagged_Type, to build the specs of the inherited interface
483 -- primitives. The derived primitives are aliased to those of the
484 -- interface. This routine takes care also of transferring to the full view
485 -- subprograms associated with the partial view of Tagged_Type that cover
486 -- interface primitives.
487
488 procedure Derived_Standard_Character
489 (N : Node_Id;
490 Parent_Type : Entity_Id;
491 Derived_Type : Entity_Id);
492 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
493 -- derivations from types Standard.Character and Standard.Wide_Character.
494
495 procedure Derived_Type_Declaration
496 (T : Entity_Id;
497 N : Node_Id;
498 Is_Completion : Boolean);
499 -- Process a derived type declaration. Build_Derived_Type is invoked
500 -- to process the actual derived type definition. Parameters N and
501 -- Is_Completion have the same meaning as in Build_Derived_Type.
502 -- T is the N_Defining_Identifier for the entity defined in the
503 -- N_Full_Type_Declaration node N, that is T is the derived type.
504
505 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
506 -- Insert each literal in symbol table, as an overloadable identifier. Each
507 -- enumeration type is mapped into a sequence of integers, and each literal
508 -- is defined as a constant with integer value. If any of the literals are
509 -- character literals, the type is a character type, which means that
510 -- strings are legal aggregates for arrays of components of the type.
511
512 function Expand_To_Stored_Constraint
513 (Typ : Entity_Id;
514 Constraint : Elist_Id) return Elist_Id;
515 -- Given a constraint (i.e. a list of expressions) on the discriminants of
516 -- Typ, expand it into a constraint on the stored discriminants and return
517 -- the new list of expressions constraining the stored discriminants.
518
519 function Find_Type_Of_Object
520 (Obj_Def : Node_Id;
521 Related_Nod : Node_Id) return Entity_Id;
522 -- Get type entity for object referenced by Obj_Def, attaching the implicit
523 -- types generated to Related_Nod.
524
525 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
526 -- Create a new float and apply the constraint to obtain subtype of it
527
528 function Has_Range_Constraint (N : Node_Id) return Boolean;
529 -- Given an N_Subtype_Indication node N, return True if a range constraint
530 -- is present, either directly, or as part of a digits or delta constraint.
531 -- In addition, a digits constraint in the decimal case returns True, since
532 -- it establishes a default range if no explicit range is present.
533
534 function Inherit_Components
535 (N : Node_Id;
536 Parent_Base : Entity_Id;
537 Derived_Base : Entity_Id;
538 Is_Tagged : Boolean;
539 Inherit_Discr : Boolean;
540 Discs : Elist_Id) return Elist_Id;
541 -- Called from Build_Derived_Record_Type to inherit the components of
542 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
543 -- For more information on derived types and component inheritance please
544 -- consult the comment above the body of Build_Derived_Record_Type.
545 --
546 -- N is the original derived type declaration
547 --
548 -- Is_Tagged is set if we are dealing with tagged types
549 --
550 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
551 -- Parent_Base, otherwise no discriminants are inherited.
552 --
553 -- Discs gives the list of constraints that apply to Parent_Base in the
554 -- derived type declaration. If Discs is set to No_Elist, then we have
555 -- the following situation:
556 --
557 -- type Parent (D1..Dn : ..) is [tagged] record ...;
558 -- type Derived is new Parent [with ...];
559 --
560 -- which gets treated as
561 --
562 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
563 --
564 -- For untagged types the returned value is an association list. The list
565 -- starts from the association (Parent_Base => Derived_Base), and then it
566 -- contains a sequence of the associations of the form
567 --
568 -- (Old_Component => New_Component),
569 --
570 -- where Old_Component is the Entity_Id of a component in Parent_Base and
571 -- New_Component is the Entity_Id of the corresponding component in
572 -- Derived_Base. For untagged records, this association list is needed when
573 -- copying the record declaration for the derived base. In the tagged case
574 -- the value returned is irrelevant.
575
576 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
577 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
578 -- Determine whether subprogram Subp is a procedure subject to pragma
579 -- Extensions_Visible with value False and has at least one controlling
580 -- parameter of mode OUT.
581
582 function Is_Valid_Constraint_Kind
583 (T_Kind : Type_Kind;
584 Constraint_Kind : Node_Kind) return Boolean;
585 -- Returns True if it is legal to apply the given kind of constraint to the
586 -- given kind of type (index constraint to an array type, for example).
587
588 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
589 -- Create new modular type. Verify that modulus is in bounds
590
591 procedure New_Concatenation_Op (Typ : Entity_Id);
592 -- Create an abbreviated declaration for an operator in order to
593 -- materialize concatenation on array types.
594
595 procedure Ordinary_Fixed_Point_Type_Declaration
596 (T : Entity_Id;
597 Def : Node_Id);
598 -- Create a new ordinary fixed point type, and apply the constraint to
599 -- obtain subtype of it.
600
601 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
602 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
603 -- In_Default_Expr can be properly adjusted.
604
605 procedure Prepare_Private_Subtype_Completion
606 (Id : Entity_Id;
607 Related_Nod : Node_Id);
608 -- Id is a subtype of some private type. Creates the full declaration
609 -- associated with Id whenever possible, i.e. when the full declaration
610 -- of the base type is already known. Records each subtype into
611 -- Private_Dependents of the base type.
612
613 procedure Process_Incomplete_Dependents
614 (N : Node_Id;
615 Full_T : Entity_Id;
616 Inc_T : Entity_Id);
617 -- Process all entities that depend on an incomplete type. There include
618 -- subtypes, subprogram types that mention the incomplete type in their
619 -- profiles, and subprogram with access parameters that designate the
620 -- incomplete type.
621
622 -- Inc_T is the defining identifier of an incomplete type declaration, its
623 -- Ekind is E_Incomplete_Type.
624 --
625 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
626 --
627 -- Full_T is N's defining identifier.
628 --
629 -- Subtypes of incomplete types with discriminants are completed when the
630 -- parent type is. This is simpler than private subtypes, because they can
631 -- only appear in the same scope, and there is no need to exchange views.
632 -- Similarly, access_to_subprogram types may have a parameter or a return
633 -- type that is an incomplete type, and that must be replaced with the
634 -- full type.
635 --
636 -- If the full type is tagged, subprogram with access parameters that
637 -- designated the incomplete may be primitive operations of the full type,
638 -- and have to be processed accordingly.
639
640 procedure Process_Real_Range_Specification (Def : Node_Id);
641 -- Given the type definition for a real type, this procedure processes and
642 -- checks the real range specification of this type definition if one is
643 -- present. If errors are found, error messages are posted, and the
644 -- Real_Range_Specification of Def is reset to Empty.
645
646 procedure Record_Type_Declaration
647 (T : Entity_Id;
648 N : Node_Id;
649 Prev : Entity_Id);
650 -- Process a record type declaration (for both untagged and tagged
651 -- records). Parameters T and N are exactly like in procedure
652 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
653 -- for this routine. If this is the completion of an incomplete type
654 -- declaration, Prev is the entity of the incomplete declaration, used for
655 -- cross-referencing. Otherwise Prev = T.
656
657 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
658 -- This routine is used to process the actual record type definition (both
659 -- for untagged and tagged records). Def is a record type definition node.
660 -- This procedure analyzes the components in this record type definition.
661 -- Prev_T is the entity for the enclosing record type. It is provided so
662 -- that its Has_Task flag can be set if any of the component have Has_Task
663 -- set. If the declaration is the completion of an incomplete type
664 -- declaration, Prev_T is the original incomplete type, whose full view is
665 -- the record type.
666
667 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
668 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
669 -- first create the list of components for the derived type from that of
670 -- the parent by means of Inherit_Components and then build a copy of the
671 -- declaration tree of the parent with the help of the mapping returned by
672 -- Inherit_Components, which will for example be used to validate record
673 -- representation clauses given for the derived type. If the parent type
674 -- is private and has discriminants, the ancestor discriminants used in the
675 -- inheritance are that of the private declaration, whereas the ancestor
676 -- discriminants present in the declaration tree of the parent are that of
677 -- the full declaration; as a consequence, the remapping done during the
678 -- copy will leave the references to the ancestor discriminants unchanged
679 -- in the declaration tree and they need to be fixed up. If the derived
680 -- type has a known discriminant part, then the remapping done during the
681 -- copy will only create references to the girder discriminants and they
682 -- need to be replaced with references to the non-girder discriminants.
683
684 procedure Set_Fixed_Range
685 (E : Entity_Id;
686 Loc : Source_Ptr;
687 Lo : Ureal;
688 Hi : Ureal);
689 -- Build a range node with the given bounds and set it as the Scalar_Range
690 -- of the given fixed-point type entity. Loc is the source location used
691 -- for the constructed range. See body for further details.
692
693 procedure Set_Scalar_Range_For_Subtype
694 (Def_Id : Entity_Id;
695 R : Node_Id;
696 Subt : Entity_Id);
697 -- This routine is used to set the scalar range field for a subtype given
698 -- Def_Id, the entity for the subtype, and R, the range expression for the
699 -- scalar range. Subt provides the parent subtype to be used to analyze,
700 -- resolve, and check the given range.
701
702 procedure Set_Default_SSO (T : Entity_Id);
703 -- T is the entity for an array or record being declared. This procedure
704 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
705 -- to the setting of Opt.Default_SSO.
706
707 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
708 -- Create a new signed integer entity, and apply the constraint to obtain
709 -- the required first named subtype of this type.
710
711 procedure Set_Stored_Constraint_From_Discriminant_Constraint
712 (E : Entity_Id);
713 -- E is some record type. This routine computes E's Stored_Constraint
714 -- from its Discriminant_Constraint.
715
716 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
717 -- Check that an entity in a list of progenitors is an interface,
718 -- emit error otherwise.
719
720 -----------------------
721 -- Access_Definition --
722 -----------------------
723
724 function Access_Definition
725 (Related_Nod : Node_Id;
726 N : Node_Id) return Entity_Id
727 is
728 Anon_Type : Entity_Id;
729 Anon_Scope : Entity_Id;
730 Desig_Type : Entity_Id;
731 Enclosing_Prot_Type : Entity_Id := Empty;
732
733 begin
734 if Is_Entry (Current_Scope)
735 and then Is_Task_Type (Etype (Scope (Current_Scope)))
736 then
737 Error_Msg_N ("task entries cannot have access parameters", N);
738 return Empty;
739 end if;
740
741 -- Ada 2005: For an object declaration the corresponding anonymous
742 -- type is declared in the current scope.
743
744 -- If the access definition is the return type of another access to
745 -- function, scope is the current one, because it is the one of the
746 -- current type declaration, except for the pathological case below.
747
748 if Nkind_In (Related_Nod, N_Object_Declaration,
749 N_Access_Function_Definition)
750 then
751 Anon_Scope := Current_Scope;
752
753 -- A pathological case: function returning access functions that
754 -- return access functions, etc. Each anonymous access type created
755 -- is in the enclosing scope of the outermost function.
756
757 declare
758 Par : Node_Id;
759
760 begin
761 Par := Related_Nod;
762 while Nkind_In (Par, N_Access_Function_Definition,
763 N_Access_Definition)
764 loop
765 Par := Parent (Par);
766 end loop;
767
768 if Nkind (Par) = N_Function_Specification then
769 Anon_Scope := Scope (Defining_Entity (Par));
770 end if;
771 end;
772
773 -- For the anonymous function result case, retrieve the scope of the
774 -- function specification's associated entity rather than using the
775 -- current scope. The current scope will be the function itself if the
776 -- formal part is currently being analyzed, but will be the parent scope
777 -- in the case of a parameterless function, and we always want to use
778 -- the function's parent scope. Finally, if the function is a child
779 -- unit, we must traverse the tree to retrieve the proper entity.
780
781 elsif Nkind (Related_Nod) = N_Function_Specification
782 and then Nkind (Parent (N)) /= N_Parameter_Specification
783 then
784 -- If the current scope is a protected type, the anonymous access
785 -- is associated with one of the protected operations, and must
786 -- be available in the scope that encloses the protected declaration.
787 -- Otherwise the type is in the scope enclosing the subprogram.
788
789 -- If the function has formals, the return type of a subprogram
790 -- declaration is analyzed in the scope of the subprogram (see
791 -- Process_Formals) and thus the protected type, if present, is
792 -- the scope of the current function scope.
793
794 if Ekind (Current_Scope) = E_Protected_Type then
795 Enclosing_Prot_Type := Current_Scope;
796
797 elsif Ekind (Current_Scope) = E_Function
798 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
799 then
800 Enclosing_Prot_Type := Scope (Current_Scope);
801 end if;
802
803 if Present (Enclosing_Prot_Type) then
804 Anon_Scope := Scope (Enclosing_Prot_Type);
805
806 else
807 Anon_Scope := Scope (Defining_Entity (Related_Nod));
808 end if;
809
810 -- For an access type definition, if the current scope is a child
811 -- unit it is the scope of the type.
812
813 elsif Is_Compilation_Unit (Current_Scope) then
814 Anon_Scope := Current_Scope;
815
816 -- For access formals, access components, and access discriminants, the
817 -- scope is that of the enclosing declaration,
818
819 else
820 Anon_Scope := Scope (Current_Scope);
821 end if;
822
823 Anon_Type :=
824 Create_Itype
825 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
826
827 if All_Present (N)
828 and then Ada_Version >= Ada_2005
829 then
830 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
831 end if;
832
833 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
834 -- the corresponding semantic routine
835
836 if Present (Access_To_Subprogram_Definition (N)) then
837
838 -- Compiler runtime units are compiled in Ada 2005 mode when building
839 -- the runtime library but must also be compilable in Ada 95 mode
840 -- (when bootstrapping the compiler).
841
842 Check_Compiler_Unit ("anonymous access to subprogram", N);
843
844 Access_Subprogram_Declaration
845 (T_Name => Anon_Type,
846 T_Def => Access_To_Subprogram_Definition (N));
847
848 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
849 Set_Ekind
850 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
851 else
852 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
853 end if;
854
855 Set_Can_Use_Internal_Rep
856 (Anon_Type, not Always_Compatible_Rep_On_Target);
857
858 -- If the anonymous access is associated with a protected operation,
859 -- create a reference to it after the enclosing protected definition
860 -- because the itype will be used in the subsequent bodies.
861
862 -- If the anonymous access itself is protected, a full type
863 -- declaratiton will be created for it, so that the equivalent
864 -- record type can be constructed. For further details, see
865 -- Replace_Anonymous_Access_To_Protected-Subprogram.
866
867 if Ekind (Current_Scope) = E_Protected_Type
868 and then not Protected_Present (Access_To_Subprogram_Definition (N))
869 then
870 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
871 end if;
872
873 return Anon_Type;
874 end if;
875
876 Find_Type (Subtype_Mark (N));
877 Desig_Type := Entity (Subtype_Mark (N));
878
879 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
880 Set_Etype (Anon_Type, Anon_Type);
881
882 -- Make sure the anonymous access type has size and alignment fields
883 -- set, as required by gigi. This is necessary in the case of the
884 -- Task_Body_Procedure.
885
886 if not Has_Private_Component (Desig_Type) then
887 Layout_Type (Anon_Type);
888 end if;
889
890 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
891 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
892 -- the null value is allowed. In Ada 95 the null value is never allowed.
893
894 if Ada_Version >= Ada_2005 then
895 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
896 else
897 Set_Can_Never_Be_Null (Anon_Type, True);
898 end if;
899
900 -- The anonymous access type is as public as the discriminated type or
901 -- subprogram that defines it. It is imported (for back-end purposes)
902 -- if the designated type is.
903
904 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
905
906 -- Ada 2005 (AI-231): Propagate the access-constant attribute
907
908 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
909
910 -- The context is either a subprogram declaration, object declaration,
911 -- or an access discriminant, in a private or a full type declaration.
912 -- In the case of a subprogram, if the designated type is incomplete,
913 -- the operation will be a primitive operation of the full type, to be
914 -- updated subsequently. If the type is imported through a limited_with
915 -- clause, the subprogram is not a primitive operation of the type
916 -- (which is declared elsewhere in some other scope).
917
918 if Ekind (Desig_Type) = E_Incomplete_Type
919 and then not From_Limited_With (Desig_Type)
920 and then Is_Overloadable (Current_Scope)
921 then
922 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
923 Set_Has_Delayed_Freeze (Current_Scope);
924 end if;
925
926 -- If the designated type is limited and class-wide, the object might
927 -- contain tasks, so we create a Master entity for the declaration. This
928 -- must be done before expansion of the full declaration, because the
929 -- declaration may include an expression that is an allocator, whose
930 -- expansion needs the proper Master for the created tasks.
931
932 if Expander_Active
933 and then Nkind (Related_Nod) = N_Object_Declaration
934 then
935 if Is_Limited_Record (Desig_Type)
936 and then Is_Class_Wide_Type (Desig_Type)
937 then
938 Build_Class_Wide_Master (Anon_Type);
939
940 -- Similarly, if the type is an anonymous access that designates
941 -- tasks, create a master entity for it in the current context.
942
943 elsif Has_Task (Desig_Type)
944 and then Comes_From_Source (Related_Nod)
945 then
946 Build_Master_Entity (Defining_Identifier (Related_Nod));
947 Build_Master_Renaming (Anon_Type);
948 end if;
949 end if;
950
951 -- For a private component of a protected type, it is imperative that
952 -- the back-end elaborate the type immediately after the protected
953 -- declaration, because this type will be used in the declarations
954 -- created for the component within each protected body, so we must
955 -- create an itype reference for it now.
956
957 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
958 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
959
960 -- Similarly, if the access definition is the return result of a
961 -- function, create an itype reference for it because it will be used
962 -- within the function body. For a regular function that is not a
963 -- compilation unit, insert reference after the declaration. For a
964 -- protected operation, insert it after the enclosing protected type
965 -- declaration. In either case, do not create a reference for a type
966 -- obtained through a limited_with clause, because this would introduce
967 -- semantic dependencies.
968
969 -- Similarly, do not create a reference if the designated type is a
970 -- generic formal, because no use of it will reach the backend.
971
972 elsif Nkind (Related_Nod) = N_Function_Specification
973 and then not From_Limited_With (Desig_Type)
974 and then not Is_Generic_Type (Desig_Type)
975 then
976 if Present (Enclosing_Prot_Type) then
977 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
978
979 elsif Is_List_Member (Parent (Related_Nod))
980 and then Nkind (Parent (N)) /= N_Parameter_Specification
981 then
982 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
983 end if;
984
985 -- Finally, create an itype reference for an object declaration of an
986 -- anonymous access type. This is strictly necessary only for deferred
987 -- constants, but in any case will avoid out-of-scope problems in the
988 -- back-end.
989
990 elsif Nkind (Related_Nod) = N_Object_Declaration then
991 Build_Itype_Reference (Anon_Type, Related_Nod);
992 end if;
993
994 return Anon_Type;
995 end Access_Definition;
996
997 -----------------------------------
998 -- Access_Subprogram_Declaration --
999 -----------------------------------
1000
1001 procedure Access_Subprogram_Declaration
1002 (T_Name : Entity_Id;
1003 T_Def : Node_Id)
1004 is
1005 procedure Check_For_Premature_Usage (Def : Node_Id);
1006 -- Check that type T_Name is not used, directly or recursively, as a
1007 -- parameter or a return type in Def. Def is either a subtype, an
1008 -- access_definition, or an access_to_subprogram_definition.
1009
1010 -------------------------------
1011 -- Check_For_Premature_Usage --
1012 -------------------------------
1013
1014 procedure Check_For_Premature_Usage (Def : Node_Id) is
1015 Param : Node_Id;
1016
1017 begin
1018 -- Check for a subtype mark
1019
1020 if Nkind (Def) in N_Has_Etype then
1021 if Etype (Def) = T_Name then
1022 Error_Msg_N
1023 ("type& cannot be used before end of its declaration", Def);
1024 end if;
1025
1026 -- If this is not a subtype, then this is an access_definition
1027
1028 elsif Nkind (Def) = N_Access_Definition then
1029 if Present (Access_To_Subprogram_Definition (Def)) then
1030 Check_For_Premature_Usage
1031 (Access_To_Subprogram_Definition (Def));
1032 else
1033 Check_For_Premature_Usage (Subtype_Mark (Def));
1034 end if;
1035
1036 -- The only cases left are N_Access_Function_Definition and
1037 -- N_Access_Procedure_Definition.
1038
1039 else
1040 if Present (Parameter_Specifications (Def)) then
1041 Param := First (Parameter_Specifications (Def));
1042 while Present (Param) loop
1043 Check_For_Premature_Usage (Parameter_Type (Param));
1044 Next (Param);
1045 end loop;
1046 end if;
1047
1048 if Nkind (Def) = N_Access_Function_Definition then
1049 Check_For_Premature_Usage (Result_Definition (Def));
1050 end if;
1051 end if;
1052 end Check_For_Premature_Usage;
1053
1054 -- Local variables
1055
1056 Formals : constant List_Id := Parameter_Specifications (T_Def);
1057 Formal : Entity_Id;
1058 D_Ityp : Node_Id;
1059 Desig_Type : constant Entity_Id :=
1060 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1061
1062 -- Start of processing for Access_Subprogram_Declaration
1063
1064 begin
1065 -- Associate the Itype node with the inner full-type declaration or
1066 -- subprogram spec or entry body. This is required to handle nested
1067 -- anonymous declarations. For example:
1068
1069 -- procedure P
1070 -- (X : access procedure
1071 -- (Y : access procedure
1072 -- (Z : access T)))
1073
1074 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1075 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1076 N_Private_Type_Declaration,
1077 N_Private_Extension_Declaration,
1078 N_Procedure_Specification,
1079 N_Function_Specification,
1080 N_Entry_Body)
1081
1082 or else
1083 Nkind_In (D_Ityp, N_Object_Declaration,
1084 N_Object_Renaming_Declaration,
1085 N_Formal_Object_Declaration,
1086 N_Formal_Type_Declaration,
1087 N_Task_Type_Declaration,
1088 N_Protected_Type_Declaration))
1089 loop
1090 D_Ityp := Parent (D_Ityp);
1091 pragma Assert (D_Ityp /= Empty);
1092 end loop;
1093
1094 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1095
1096 if Nkind_In (D_Ityp, N_Procedure_Specification,
1097 N_Function_Specification)
1098 then
1099 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1100
1101 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1102 N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Type_Declaration)
1105 then
1106 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1107 end if;
1108
1109 if Nkind (T_Def) = N_Access_Function_Definition then
1110 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1111 declare
1112 Acc : constant Node_Id := Result_Definition (T_Def);
1113
1114 begin
1115 if Present (Access_To_Subprogram_Definition (Acc))
1116 and then
1117 Protected_Present (Access_To_Subprogram_Definition (Acc))
1118 then
1119 Set_Etype
1120 (Desig_Type,
1121 Replace_Anonymous_Access_To_Protected_Subprogram
1122 (T_Def));
1123
1124 else
1125 Set_Etype
1126 (Desig_Type,
1127 Access_Definition (T_Def, Result_Definition (T_Def)));
1128 end if;
1129 end;
1130
1131 else
1132 Analyze (Result_Definition (T_Def));
1133
1134 declare
1135 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1136
1137 begin
1138 -- If a null exclusion is imposed on the result type, then
1139 -- create a null-excluding itype (an access subtype) and use
1140 -- it as the function's Etype.
1141
1142 if Is_Access_Type (Typ)
1143 and then Null_Exclusion_In_Return_Present (T_Def)
1144 then
1145 Set_Etype (Desig_Type,
1146 Create_Null_Excluding_Itype
1147 (T => Typ,
1148 Related_Nod => T_Def,
1149 Scope_Id => Current_Scope));
1150
1151 else
1152 if From_Limited_With (Typ) then
1153
1154 -- AI05-151: Incomplete types are allowed in all basic
1155 -- declarations, including access to subprograms.
1156
1157 if Ada_Version >= Ada_2012 then
1158 null;
1159
1160 else
1161 Error_Msg_NE
1162 ("illegal use of incomplete type&",
1163 Result_Definition (T_Def), Typ);
1164 end if;
1165
1166 elsif Ekind (Current_Scope) = E_Package
1167 and then In_Private_Part (Current_Scope)
1168 then
1169 if Ekind (Typ) = E_Incomplete_Type then
1170 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1171
1172 elsif Is_Class_Wide_Type (Typ)
1173 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1174 then
1175 Append_Elmt
1176 (Desig_Type, Private_Dependents (Etype (Typ)));
1177 end if;
1178 end if;
1179
1180 Set_Etype (Desig_Type, Typ);
1181 end if;
1182 end;
1183 end if;
1184
1185 if not (Is_Type (Etype (Desig_Type))) then
1186 Error_Msg_N
1187 ("expect type in function specification",
1188 Result_Definition (T_Def));
1189 end if;
1190
1191 else
1192 Set_Etype (Desig_Type, Standard_Void_Type);
1193 end if;
1194
1195 if Present (Formals) then
1196 Push_Scope (Desig_Type);
1197
1198 -- Some special tests here. These special tests can be removed
1199 -- if and when Itypes always have proper parent pointers to their
1200 -- declarations???
1201
1202 -- Special test 1) Link defining_identifier of formals. Required by
1203 -- First_Formal to provide its functionality.
1204
1205 declare
1206 F : Node_Id;
1207
1208 begin
1209 F := First (Formals);
1210
1211 while Present (F) loop
1212 if No (Parent (Defining_Identifier (F))) then
1213 Set_Parent (Defining_Identifier (F), F);
1214 end if;
1215
1216 Next (F);
1217 end loop;
1218 end;
1219
1220 Process_Formals (Formals, Parent (T_Def));
1221
1222 -- Special test 2) End_Scope requires that the parent pointer be set
1223 -- to something reasonable, but Itypes don't have parent pointers. So
1224 -- we set it and then unset it ???
1225
1226 Set_Parent (Desig_Type, T_Name);
1227 End_Scope;
1228 Set_Parent (Desig_Type, Empty);
1229 end if;
1230
1231 -- Check for premature usage of the type being defined
1232
1233 Check_For_Premature_Usage (T_Def);
1234
1235 -- The return type and/or any parameter type may be incomplete. Mark the
1236 -- subprogram_type as depending on the incomplete type, so that it can
1237 -- be updated when the full type declaration is seen. This only applies
1238 -- to incomplete types declared in some enclosing scope, not to limited
1239 -- views from other packages.
1240
1241 -- Prior to Ada 2012, access to functions can only have in_parameters.
1242
1243 if Present (Formals) then
1244 Formal := First_Formal (Desig_Type);
1245 while Present (Formal) loop
1246 if Ekind (Formal) /= E_In_Parameter
1247 and then Nkind (T_Def) = N_Access_Function_Definition
1248 and then Ada_Version < Ada_2012
1249 then
1250 Error_Msg_N ("functions can only have IN parameters", Formal);
1251 end if;
1252
1253 if Ekind (Etype (Formal)) = E_Incomplete_Type
1254 and then In_Open_Scopes (Scope (Etype (Formal)))
1255 then
1256 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1257 Set_Has_Delayed_Freeze (Desig_Type);
1258 end if;
1259
1260 Next_Formal (Formal);
1261 end loop;
1262 end if;
1263
1264 -- Check whether an indirect call without actuals may be possible. This
1265 -- is used when resolving calls whose result is then indexed.
1266
1267 May_Need_Actuals (Desig_Type);
1268
1269 -- If the return type is incomplete, this is legal as long as the type
1270 -- is declared in the current scope and will be completed in it (rather
1271 -- than being part of limited view).
1272
1273 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1274 and then not Has_Delayed_Freeze (Desig_Type)
1275 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1276 then
1277 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1278 Set_Has_Delayed_Freeze (Desig_Type);
1279 end if;
1280
1281 Check_Delayed_Subprogram (Desig_Type);
1282
1283 if Protected_Present (T_Def) then
1284 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1285 Set_Convention (Desig_Type, Convention_Protected);
1286 else
1287 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1288 end if;
1289
1290 Set_Can_Use_Internal_Rep (T_Name,
1291 not Always_Compatible_Rep_On_Target);
1292 Set_Etype (T_Name, T_Name);
1293 Init_Size_Align (T_Name);
1294 Set_Directly_Designated_Type (T_Name, Desig_Type);
1295
1296 -- If the access_to_subprogram is not declared at the library level,
1297 -- it can only point to subprograms that are at the same or deeper
1298 -- accessibility level. The corresponding subprogram type might
1299 -- require an activation record when compiling for C.
1300
1301 Set_Needs_Activation_Record (Desig_Type,
1302 not Is_Library_Level_Entity (T_Name));
1303
1304 Generate_Reference_To_Formals (T_Name);
1305
1306 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1307
1308 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1309
1310 Check_Restriction (No_Access_Subprograms, T_Def);
1311 end Access_Subprogram_Declaration;
1312
1313 ----------------------------
1314 -- Access_Type_Declaration --
1315 ----------------------------
1316
1317 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1318 P : constant Node_Id := Parent (Def);
1319 S : constant Node_Id := Subtype_Indication (Def);
1320
1321 Full_Desig : Entity_Id;
1322
1323 begin
1324 -- Check for permissible use of incomplete type
1325
1326 if Nkind (S) /= N_Subtype_Indication then
1327 Analyze (S);
1328
1329 if Present (Entity (S))
1330 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1331 then
1332 Set_Directly_Designated_Type (T, Entity (S));
1333
1334 -- If the designated type is a limited view, we cannot tell if
1335 -- the full view contains tasks, and there is no way to handle
1336 -- that full view in a client. We create a master entity for the
1337 -- scope, which will be used when a client determines that one
1338 -- is needed.
1339
1340 if From_Limited_With (Entity (S))
1341 and then not Is_Class_Wide_Type (Entity (S))
1342 then
1343 Set_Ekind (T, E_Access_Type);
1344 Build_Master_Entity (T);
1345 Build_Master_Renaming (T);
1346 end if;
1347
1348 else
1349 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1350 end if;
1351
1352 -- If the access definition is of the form: ACCESS NOT NULL ..
1353 -- the subtype indication must be of an access type. Create
1354 -- a null-excluding subtype of it.
1355
1356 if Null_Excluding_Subtype (Def) then
1357 if not Is_Access_Type (Entity (S)) then
1358 Error_Msg_N ("null exclusion must apply to access type", Def);
1359
1360 else
1361 declare
1362 Loc : constant Source_Ptr := Sloc (S);
1363 Decl : Node_Id;
1364 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1365
1366 begin
1367 Decl :=
1368 Make_Subtype_Declaration (Loc,
1369 Defining_Identifier => Nam,
1370 Subtype_Indication =>
1371 New_Occurrence_Of (Entity (S), Loc));
1372 Set_Null_Exclusion_Present (Decl);
1373 Insert_Before (Parent (Def), Decl);
1374 Analyze (Decl);
1375 Set_Entity (S, Nam);
1376 end;
1377 end if;
1378 end if;
1379
1380 else
1381 Set_Directly_Designated_Type (T,
1382 Process_Subtype (S, P, T, 'P'));
1383 end if;
1384
1385 if All_Present (Def) or Constant_Present (Def) then
1386 Set_Ekind (T, E_General_Access_Type);
1387 else
1388 Set_Ekind (T, E_Access_Type);
1389 end if;
1390
1391 Full_Desig := Designated_Type (T);
1392
1393 if Base_Type (Full_Desig) = T then
1394 Error_Msg_N ("access type cannot designate itself", S);
1395
1396 -- In Ada 2005, the type may have a limited view through some unit in
1397 -- its own context, allowing the following circularity that cannot be
1398 -- detected earlier.
1399
1400 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1401 then
1402 Error_Msg_N
1403 ("access type cannot designate its own class-wide type", S);
1404
1405 -- Clean up indication of tagged status to prevent cascaded errors
1406
1407 Set_Is_Tagged_Type (T, False);
1408 end if;
1409
1410 -- For SPARK, check that the designated type is compatible with
1411 -- respect to volatility with the access type.
1412
1413 if SPARK_Mode /= Off
1414 and then Comes_From_Source (T)
1415 then
1416 -- ??? UNIMPLEMENTED
1417 -- In the case where the designated type is incomplete at this point,
1418 -- performing this check here is harmless but the check will need to
1419 -- be repeated when the designated type is complete.
1420
1421 -- The preceding call to Comes_From_Source is needed because the
1422 -- FE sometimes introduces implicitly declared access types. See,
1423 -- for example, the expansion of nested_po.ads in OA28-015.
1424
1425 Check_Volatility_Compatibility
1426 (Full_Desig, T, "designated type", "access type",
1427 Srcpos_Bearer => T);
1428 end if;
1429
1430 Set_Etype (T, T);
1431
1432 -- If the type has appeared already in a with_type clause, it is frozen
1433 -- and the pointer size is already set. Else, initialize.
1434
1435 if not From_Limited_With (T) then
1436 Init_Size_Align (T);
1437 end if;
1438
1439 -- Note that Has_Task is always false, since the access type itself
1440 -- is not a task type. See Einfo for more description on this point.
1441 -- Exactly the same consideration applies to Has_Controlled_Component
1442 -- and to Has_Protected.
1443
1444 Set_Has_Task (T, False);
1445 Set_Has_Protected (T, False);
1446 Set_Has_Timing_Event (T, False);
1447 Set_Has_Controlled_Component (T, False);
1448
1449 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1450 -- problems where an incomplete view of this entity has been previously
1451 -- established by a limited with and an overlaid version of this field
1452 -- (Stored_Constraint) was initialized for the incomplete view.
1453
1454 -- This reset is performed in most cases except where the access type
1455 -- has been created for the purposes of allocating or deallocating a
1456 -- build-in-place object. Such access types have explicitly set pools
1457 -- and finalization masters.
1458
1459 if No (Associated_Storage_Pool (T)) then
1460 Set_Finalization_Master (T, Empty);
1461 end if;
1462
1463 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1464 -- attributes
1465
1466 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1467 Set_Is_Access_Constant (T, Constant_Present (Def));
1468 end Access_Type_Declaration;
1469
1470 ----------------------------------
1471 -- Add_Interface_Tag_Components --
1472 ----------------------------------
1473
1474 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1475 Loc : constant Source_Ptr := Sloc (N);
1476 L : List_Id;
1477 Last_Tag : Node_Id;
1478
1479 procedure Add_Tag (Iface : Entity_Id);
1480 -- Add tag for one of the progenitor interfaces
1481
1482 -------------
1483 -- Add_Tag --
1484 -------------
1485
1486 procedure Add_Tag (Iface : Entity_Id) is
1487 Decl : Node_Id;
1488 Def : Node_Id;
1489 Tag : Entity_Id;
1490 Offset : Entity_Id;
1491
1492 begin
1493 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1494
1495 -- This is a reasonable place to propagate predicates
1496
1497 if Has_Predicates (Iface) then
1498 Set_Has_Predicates (Typ);
1499 end if;
1500
1501 Def :=
1502 Make_Component_Definition (Loc,
1503 Aliased_Present => True,
1504 Subtype_Indication =>
1505 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1506
1507 Tag := Make_Temporary (Loc, 'V');
1508
1509 Decl :=
1510 Make_Component_Declaration (Loc,
1511 Defining_Identifier => Tag,
1512 Component_Definition => Def);
1513
1514 Analyze_Component_Declaration (Decl);
1515
1516 Set_Analyzed (Decl);
1517 Set_Ekind (Tag, E_Component);
1518 Set_Is_Tag (Tag);
1519 Set_Is_Aliased (Tag);
1520 Set_Is_Independent (Tag);
1521 Set_Related_Type (Tag, Iface);
1522 Init_Component_Location (Tag);
1523
1524 pragma Assert (Is_Frozen (Iface));
1525
1526 Set_DT_Entry_Count (Tag,
1527 DT_Entry_Count (First_Entity (Iface)));
1528
1529 if No (Last_Tag) then
1530 Prepend (Decl, L);
1531 else
1532 Insert_After (Last_Tag, Decl);
1533 end if;
1534
1535 Last_Tag := Decl;
1536
1537 -- If the ancestor has discriminants we need to give special support
1538 -- to store the offset_to_top value of the secondary dispatch tables.
1539 -- For this purpose we add a supplementary component just after the
1540 -- field that contains the tag associated with each secondary DT.
1541
1542 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1543 Def :=
1544 Make_Component_Definition (Loc,
1545 Subtype_Indication =>
1546 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1547
1548 Offset := Make_Temporary (Loc, 'V');
1549
1550 Decl :=
1551 Make_Component_Declaration (Loc,
1552 Defining_Identifier => Offset,
1553 Component_Definition => Def);
1554
1555 Analyze_Component_Declaration (Decl);
1556
1557 Set_Analyzed (Decl);
1558 Set_Ekind (Offset, E_Component);
1559 Set_Is_Aliased (Offset);
1560 Set_Is_Independent (Offset);
1561 Set_Related_Type (Offset, Iface);
1562 Init_Component_Location (Offset);
1563 Insert_After (Last_Tag, Decl);
1564 Last_Tag := Decl;
1565 end if;
1566 end Add_Tag;
1567
1568 -- Local variables
1569
1570 Elmt : Elmt_Id;
1571 Ext : Node_Id;
1572 Comp : Node_Id;
1573
1574 -- Start of processing for Add_Interface_Tag_Components
1575
1576 begin
1577 if not RTE_Available (RE_Interface_Tag) then
1578 Error_Msg
1579 ("(Ada 2005) interface types not supported by this run-time!",
1580 Sloc (N));
1581 return;
1582 end if;
1583
1584 if Ekind (Typ) /= E_Record_Type
1585 or else (Is_Concurrent_Record_Type (Typ)
1586 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1587 or else (not Is_Concurrent_Record_Type (Typ)
1588 and then No (Interfaces (Typ))
1589 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1590 then
1591 return;
1592 end if;
1593
1594 -- Find the current last tag
1595
1596 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1597 Ext := Record_Extension_Part (Type_Definition (N));
1598 else
1599 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1600 Ext := Type_Definition (N);
1601 end if;
1602
1603 Last_Tag := Empty;
1604
1605 if not (Present (Component_List (Ext))) then
1606 Set_Null_Present (Ext, False);
1607 L := New_List;
1608 Set_Component_List (Ext,
1609 Make_Component_List (Loc,
1610 Component_Items => L,
1611 Null_Present => False));
1612 else
1613 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1614 L := Component_Items
1615 (Component_List
1616 (Record_Extension_Part
1617 (Type_Definition (N))));
1618 else
1619 L := Component_Items
1620 (Component_List
1621 (Type_Definition (N)));
1622 end if;
1623
1624 -- Find the last tag component
1625
1626 Comp := First (L);
1627 while Present (Comp) loop
1628 if Nkind (Comp) = N_Component_Declaration
1629 and then Is_Tag (Defining_Identifier (Comp))
1630 then
1631 Last_Tag := Comp;
1632 end if;
1633
1634 Next (Comp);
1635 end loop;
1636 end if;
1637
1638 -- At this point L references the list of components and Last_Tag
1639 -- references the current last tag (if any). Now we add the tag
1640 -- corresponding with all the interfaces that are not implemented
1641 -- by the parent.
1642
1643 if Present (Interfaces (Typ)) then
1644 Elmt := First_Elmt (Interfaces (Typ));
1645 while Present (Elmt) loop
1646 Add_Tag (Node (Elmt));
1647 Next_Elmt (Elmt);
1648 end loop;
1649 end if;
1650 end Add_Interface_Tag_Components;
1651
1652 -------------------------------------
1653 -- Add_Internal_Interface_Entities --
1654 -------------------------------------
1655
1656 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1657 Elmt : Elmt_Id;
1658 Iface : Entity_Id;
1659 Iface_Elmt : Elmt_Id;
1660 Iface_Prim : Entity_Id;
1661 Ifaces_List : Elist_Id;
1662 New_Subp : Entity_Id := Empty;
1663 Prim : Entity_Id;
1664 Restore_Scope : Boolean := False;
1665
1666 begin
1667 pragma Assert (Ada_Version >= Ada_2005
1668 and then Is_Record_Type (Tagged_Type)
1669 and then Is_Tagged_Type (Tagged_Type)
1670 and then Has_Interfaces (Tagged_Type)
1671 and then not Is_Interface (Tagged_Type));
1672
1673 -- Ensure that the internal entities are added to the scope of the type
1674
1675 if Scope (Tagged_Type) /= Current_Scope then
1676 Push_Scope (Scope (Tagged_Type));
1677 Restore_Scope := True;
1678 end if;
1679
1680 Collect_Interfaces (Tagged_Type, Ifaces_List);
1681
1682 Iface_Elmt := First_Elmt (Ifaces_List);
1683 while Present (Iface_Elmt) loop
1684 Iface := Node (Iface_Elmt);
1685
1686 -- Originally we excluded here from this processing interfaces that
1687 -- are parents of Tagged_Type because their primitives are located
1688 -- in the primary dispatch table (and hence no auxiliary internal
1689 -- entities are required to handle secondary dispatch tables in such
1690 -- case). However, these auxiliary entities are also required to
1691 -- handle derivations of interfaces in formals of generics (see
1692 -- Derive_Subprograms).
1693
1694 Elmt := First_Elmt (Primitive_Operations (Iface));
1695 while Present (Elmt) loop
1696 Iface_Prim := Node (Elmt);
1697
1698 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1699 Prim :=
1700 Find_Primitive_Covering_Interface
1701 (Tagged_Type => Tagged_Type,
1702 Iface_Prim => Iface_Prim);
1703
1704 if No (Prim) and then Serious_Errors_Detected > 0 then
1705 goto Continue;
1706 end if;
1707
1708 pragma Assert (Present (Prim));
1709
1710 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1711 -- differs from the name of the interface primitive then it is
1712 -- a private primitive inherited from a parent type. In such
1713 -- case, given that Tagged_Type covers the interface, the
1714 -- inherited private primitive becomes visible. For such
1715 -- purpose we add a new entity that renames the inherited
1716 -- private primitive.
1717
1718 if Chars (Prim) /= Chars (Iface_Prim) then
1719 pragma Assert (Has_Suffix (Prim, 'P'));
1720 Derive_Subprogram
1721 (New_Subp => New_Subp,
1722 Parent_Subp => Iface_Prim,
1723 Derived_Type => Tagged_Type,
1724 Parent_Type => Iface);
1725 Set_Alias (New_Subp, Prim);
1726 Set_Is_Abstract_Subprogram
1727 (New_Subp, Is_Abstract_Subprogram (Prim));
1728 end if;
1729
1730 Derive_Subprogram
1731 (New_Subp => New_Subp,
1732 Parent_Subp => Iface_Prim,
1733 Derived_Type => Tagged_Type,
1734 Parent_Type => Iface);
1735
1736 declare
1737 Anc : Entity_Id;
1738 begin
1739 if Is_Inherited_Operation (Prim)
1740 and then Present (Alias (Prim))
1741 then
1742 Anc := Alias (Prim);
1743 else
1744 Anc := Overridden_Operation (Prim);
1745 end if;
1746
1747 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1748 -- nonconforming preconditions in both an ancestor and
1749 -- a progenitor operation.
1750
1751 -- If the operation is a primitive wrapper it is an explicit
1752 -- (overriding) operqtion and all is fine.
1753
1754 if Present (Anc)
1755 and then Has_Non_Trivial_Precondition (Anc)
1756 and then Has_Non_Trivial_Precondition (Iface_Prim)
1757 then
1758 if Is_Abstract_Subprogram (Prim)
1759 or else
1760 (Ekind (Prim) = E_Procedure
1761 and then Nkind (Parent (Prim)) =
1762 N_Procedure_Specification
1763 and then Null_Present (Parent (Prim)))
1764 or else Is_Primitive_Wrapper (Prim)
1765 then
1766 null;
1767
1768 -- The operation is inherited and must be overridden
1769
1770 elsif not Comes_From_Source (Prim) then
1771 Error_Msg_NE
1772 ("&inherits non-conforming preconditions and must "
1773 & "be overridden (RM 6.1.1 (10-16)",
1774 Parent (Tagged_Type), Prim);
1775 end if;
1776 end if;
1777 end;
1778
1779 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1780 -- associated with interface types. These entities are
1781 -- only registered in the list of primitives of its
1782 -- corresponding tagged type because they are only used
1783 -- to fill the contents of the secondary dispatch tables.
1784 -- Therefore they are removed from the homonym chains.
1785
1786 Set_Is_Hidden (New_Subp);
1787 Set_Is_Internal (New_Subp);
1788 Set_Alias (New_Subp, Prim);
1789 Set_Is_Abstract_Subprogram
1790 (New_Subp, Is_Abstract_Subprogram (Prim));
1791 Set_Interface_Alias (New_Subp, Iface_Prim);
1792
1793 -- If the returned type is an interface then propagate it to
1794 -- the returned type. Needed by the thunk to generate the code
1795 -- which displaces "this" to reference the corresponding
1796 -- secondary dispatch table in the returned object.
1797
1798 if Is_Interface (Etype (Iface_Prim)) then
1799 Set_Etype (New_Subp, Etype (Iface_Prim));
1800 end if;
1801
1802 -- Internal entities associated with interface types are only
1803 -- registered in the list of primitives of the tagged type.
1804 -- They are only used to fill the contents of the secondary
1805 -- dispatch tables. Therefore they are not needed in the
1806 -- homonym chains.
1807
1808 Remove_Homonym (New_Subp);
1809
1810 -- Hidden entities associated with interfaces must have set
1811 -- the Has_Delay_Freeze attribute to ensure that, in case
1812 -- of locally defined tagged types (or compiling with static
1813 -- dispatch tables generation disabled) the corresponding
1814 -- entry of the secondary dispatch table is filled when such
1815 -- an entity is frozen.
1816
1817 Set_Has_Delayed_Freeze (New_Subp);
1818 end if;
1819
1820 <<Continue>>
1821 Next_Elmt (Elmt);
1822 end loop;
1823
1824 Next_Elmt (Iface_Elmt);
1825 end loop;
1826
1827 if Restore_Scope then
1828 Pop_Scope;
1829 end if;
1830 end Add_Internal_Interface_Entities;
1831
1832 -----------------------------------
1833 -- Analyze_Component_Declaration --
1834 -----------------------------------
1835
1836 procedure Analyze_Component_Declaration (N : Node_Id) is
1837 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1838 Id : constant Entity_Id := Defining_Identifier (N);
1839 E : constant Node_Id := Expression (N);
1840 Typ : constant Node_Id :=
1841 Subtype_Indication (Component_Definition (N));
1842 T : Entity_Id;
1843 P : Entity_Id;
1844
1845 function Contains_POC (Constr : Node_Id) return Boolean;
1846 -- Determines whether a constraint uses the discriminant of a record
1847 -- type thus becoming a per-object constraint (POC).
1848
1849 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1850 -- Typ is the type of the current component, check whether this type is
1851 -- a limited type. Used to validate declaration against that of
1852 -- enclosing record.
1853
1854 ------------------
1855 -- Contains_POC --
1856 ------------------
1857
1858 function Contains_POC (Constr : Node_Id) return Boolean is
1859 begin
1860 -- Prevent cascaded errors
1861
1862 if Error_Posted (Constr) then
1863 return False;
1864 end if;
1865
1866 case Nkind (Constr) is
1867 when N_Attribute_Reference =>
1868 return Attribute_Name (Constr) = Name_Access
1869 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1870
1871 when N_Discriminant_Association =>
1872 return Denotes_Discriminant (Expression (Constr));
1873
1874 when N_Identifier =>
1875 return Denotes_Discriminant (Constr);
1876
1877 when N_Index_Or_Discriminant_Constraint =>
1878 declare
1879 IDC : Node_Id;
1880
1881 begin
1882 IDC := First (Constraints (Constr));
1883 while Present (IDC) loop
1884
1885 -- One per-object constraint is sufficient
1886
1887 if Contains_POC (IDC) then
1888 return True;
1889 end if;
1890
1891 Next (IDC);
1892 end loop;
1893
1894 return False;
1895 end;
1896
1897 when N_Range =>
1898 return Denotes_Discriminant (Low_Bound (Constr))
1899 or else
1900 Denotes_Discriminant (High_Bound (Constr));
1901
1902 when N_Range_Constraint =>
1903 return Denotes_Discriminant (Range_Expression (Constr));
1904
1905 when others =>
1906 return False;
1907 end case;
1908 end Contains_POC;
1909
1910 ----------------------
1911 -- Is_Known_Limited --
1912 ----------------------
1913
1914 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1915 P : constant Entity_Id := Etype (Typ);
1916 R : constant Entity_Id := Root_Type (Typ);
1917
1918 begin
1919 if Is_Limited_Record (Typ) then
1920 return True;
1921
1922 -- If the root type is limited (and not a limited interface) so is
1923 -- the current type.
1924
1925 elsif Is_Limited_Record (R)
1926 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1927 then
1928 return True;
1929
1930 -- Else the type may have a limited interface progenitor, but a
1931 -- limited record parent that is not an interface.
1932
1933 elsif R /= P
1934 and then Is_Limited_Record (P)
1935 and then not Is_Interface (P)
1936 then
1937 return True;
1938
1939 else
1940 return False;
1941 end if;
1942 end Is_Known_Limited;
1943
1944 -- Start of processing for Analyze_Component_Declaration
1945
1946 begin
1947 Generate_Definition (Id);
1948 Enter_Name (Id);
1949
1950 if Present (Typ) then
1951 T := Find_Type_Of_Object
1952 (Subtype_Indication (Component_Definition (N)), N);
1953
1954 -- Ada 2005 (AI-230): Access Definition case
1955
1956 else
1957 pragma Assert (Present
1958 (Access_Definition (Component_Definition (N))));
1959
1960 T := Access_Definition
1961 (Related_Nod => N,
1962 N => Access_Definition (Component_Definition (N)));
1963 Set_Is_Local_Anonymous_Access (T);
1964
1965 -- Ada 2005 (AI-254)
1966
1967 if Present (Access_To_Subprogram_Definition
1968 (Access_Definition (Component_Definition (N))))
1969 and then Protected_Present (Access_To_Subprogram_Definition
1970 (Access_Definition
1971 (Component_Definition (N))))
1972 then
1973 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1974 end if;
1975 end if;
1976
1977 -- If the subtype is a constrained subtype of the enclosing record,
1978 -- (which must have a partial view) the back-end does not properly
1979 -- handle the recursion. Rewrite the component declaration with an
1980 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1981 -- the tree directly because side effects have already been removed from
1982 -- discriminant constraints.
1983
1984 if Ekind (T) = E_Access_Subtype
1985 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1986 and then Comes_From_Source (T)
1987 and then Nkind (Parent (T)) = N_Subtype_Declaration
1988 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1989 then
1990 Rewrite
1991 (Subtype_Indication (Component_Definition (N)),
1992 New_Copy_Tree (Subtype_Indication (Parent (T))));
1993 T := Find_Type_Of_Object
1994 (Subtype_Indication (Component_Definition (N)), N);
1995 end if;
1996
1997 -- If the component declaration includes a default expression, then we
1998 -- check that the component is not of a limited type (RM 3.7(5)),
1999 -- and do the special preanalysis of the expression (see section on
2000 -- "Handling of Default and Per-Object Expressions" in the spec of
2001 -- package Sem).
2002
2003 if Present (E) then
2004 Preanalyze_Default_Expression (E, T);
2005 Check_Initialization (T, E);
2006
2007 if Ada_Version >= Ada_2005
2008 and then Ekind (T) = E_Anonymous_Access_Type
2009 and then Etype (E) /= Any_Type
2010 then
2011 -- Check RM 3.9.2(9): "if the expected type for an expression is
2012 -- an anonymous access-to-specific tagged type, then the object
2013 -- designated by the expression shall not be dynamically tagged
2014 -- unless it is a controlling operand in a call on a dispatching
2015 -- operation"
2016
2017 if Is_Tagged_Type (Directly_Designated_Type (T))
2018 and then
2019 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2020 and then
2021 Ekind (Directly_Designated_Type (Etype (E))) =
2022 E_Class_Wide_Type
2023 then
2024 Error_Msg_N
2025 ("access to specific tagged type required (RM 3.9.2(9))", E);
2026 end if;
2027
2028 -- (Ada 2005: AI-230): Accessibility check for anonymous
2029 -- components
2030
2031 if Type_Access_Level (Etype (E)) >
2032 Deepest_Type_Access_Level (T)
2033 then
2034 Error_Msg_N
2035 ("expression has deeper access level than component " &
2036 "(RM 3.10.2 (12.2))", E);
2037 end if;
2038
2039 -- The initialization expression is a reference to an access
2040 -- discriminant. The type of the discriminant is always deeper
2041 -- than any access type.
2042
2043 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2044 and then Is_Entity_Name (E)
2045 and then Ekind (Entity (E)) = E_In_Parameter
2046 and then Present (Discriminal_Link (Entity (E)))
2047 then
2048 Error_Msg_N
2049 ("discriminant has deeper accessibility level than target",
2050 E);
2051 end if;
2052 end if;
2053 end if;
2054
2055 -- Avoid reporting spurious errors if the component is initialized with
2056 -- a raise expression (which is legal in any expression context)
2057
2058 if Present (E)
2059 and then
2060 (Nkind (E) = N_Raise_Expression
2061 or else (Nkind (E) = N_Qualified_Expression
2062 and then Nkind (Expression (E)) = N_Raise_Expression))
2063 then
2064 null;
2065
2066 -- The parent type may be a private view with unknown discriminants,
2067 -- and thus unconstrained. Regular components must be constrained.
2068
2069 elsif not Is_Definite_Subtype (T)
2070 and then Chars (Id) /= Name_uParent
2071 then
2072 if Is_Class_Wide_Type (T) then
2073 Error_Msg_N
2074 ("class-wide subtype with unknown discriminants" &
2075 " in component declaration",
2076 Subtype_Indication (Component_Definition (N)));
2077 else
2078 Error_Msg_N
2079 ("unconstrained subtype in component declaration",
2080 Subtype_Indication (Component_Definition (N)));
2081 end if;
2082
2083 -- Components cannot be abstract, except for the special case of
2084 -- the _Parent field (case of extending an abstract tagged type)
2085
2086 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2087 Error_Msg_N ("type of a component cannot be abstract", N);
2088 end if;
2089
2090 Set_Etype (Id, T);
2091
2092 if Aliased_Present (Component_Definition (N)) then
2093 Set_Is_Aliased (Id);
2094
2095 -- AI12-001: All aliased objects are considered to be specified as
2096 -- independently addressable (RM C.6(8.1/4)).
2097
2098 Set_Is_Independent (Id);
2099 end if;
2100
2101 -- The component declaration may have a per-object constraint, set
2102 -- the appropriate flag in the defining identifier of the subtype.
2103
2104 if Present (Subtype_Indication (Component_Definition (N))) then
2105 declare
2106 Sindic : constant Node_Id :=
2107 Subtype_Indication (Component_Definition (N));
2108 begin
2109 if Nkind (Sindic) = N_Subtype_Indication
2110 and then Present (Constraint (Sindic))
2111 and then Contains_POC (Constraint (Sindic))
2112 then
2113 Set_Has_Per_Object_Constraint (Id);
2114 end if;
2115 end;
2116 end if;
2117
2118 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2119 -- out some static checks.
2120
2121 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2122 Null_Exclusion_Static_Checks (N);
2123 end if;
2124
2125 -- If this component is private (or depends on a private type), flag the
2126 -- record type to indicate that some operations are not available.
2127
2128 P := Private_Component (T);
2129
2130 if Present (P) then
2131
2132 -- Check for circular definitions
2133
2134 if P = Any_Type then
2135 Set_Etype (Id, Any_Type);
2136
2137 -- There is a gap in the visibility of operations only if the
2138 -- component type is not defined in the scope of the record type.
2139
2140 elsif Scope (P) = Scope (Current_Scope) then
2141 null;
2142
2143 elsif Is_Limited_Type (P) then
2144 Set_Is_Limited_Composite (Current_Scope);
2145
2146 else
2147 Set_Is_Private_Composite (Current_Scope);
2148 end if;
2149 end if;
2150
2151 if P /= Any_Type
2152 and then Is_Limited_Type (T)
2153 and then Chars (Id) /= Name_uParent
2154 and then Is_Tagged_Type (Current_Scope)
2155 then
2156 if Is_Derived_Type (Current_Scope)
2157 and then not Is_Known_Limited (Current_Scope)
2158 then
2159 Error_Msg_N
2160 ("extension of nonlimited type cannot have limited components",
2161 N);
2162
2163 if Is_Interface (Root_Type (Current_Scope)) then
2164 Error_Msg_N
2165 ("\limitedness is not inherited from limited interface", N);
2166 Error_Msg_N ("\add LIMITED to type indication", N);
2167 end if;
2168
2169 Explain_Limited_Type (T, N);
2170 Set_Etype (Id, Any_Type);
2171 Set_Is_Limited_Composite (Current_Scope, False);
2172
2173 elsif not Is_Derived_Type (Current_Scope)
2174 and then not Is_Limited_Record (Current_Scope)
2175 and then not Is_Concurrent_Type (Current_Scope)
2176 then
2177 Error_Msg_N
2178 ("nonlimited tagged type cannot have limited components", N);
2179 Explain_Limited_Type (T, N);
2180 Set_Etype (Id, Any_Type);
2181 Set_Is_Limited_Composite (Current_Scope, False);
2182 end if;
2183 end if;
2184
2185 -- If the component is an unconstrained task or protected type with
2186 -- discriminants, the component and the enclosing record are limited
2187 -- and the component is constrained by its default values. Compute
2188 -- its actual subtype, else it may be allocated the maximum size by
2189 -- the backend, and possibly overflow.
2190
2191 if Is_Concurrent_Type (T)
2192 and then not Is_Constrained (T)
2193 and then Has_Discriminants (T)
2194 and then not Has_Discriminants (Current_Scope)
2195 then
2196 declare
2197 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2198
2199 begin
2200 Set_Etype (Id, Act_T);
2201
2202 -- Rewrite component definition to use the constrained subtype
2203
2204 Rewrite (Component_Definition (N),
2205 Make_Component_Definition (Loc,
2206 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2207 end;
2208 end if;
2209
2210 Set_Original_Record_Component (Id, Id);
2211
2212 if Has_Aspects (N) then
2213 Analyze_Aspect_Specifications (N, Id);
2214 end if;
2215
2216 Analyze_Dimension (N);
2217 end Analyze_Component_Declaration;
2218
2219 --------------------------
2220 -- Analyze_Declarations --
2221 --------------------------
2222
2223 procedure Analyze_Declarations (L : List_Id) is
2224 Decl : Node_Id;
2225
2226 procedure Adjust_Decl;
2227 -- Adjust Decl not to include implicit label declarations, since these
2228 -- have strange Sloc values that result in elaboration check problems.
2229 -- (They have the sloc of the label as found in the source, and that
2230 -- is ahead of the current declarative part).
2231
2232 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2233 -- Create the subprogram bodies which verify the run-time semantics of
2234 -- the pragmas listed below for each elibigle type found in declarative
2235 -- list Decls. The pragmas are:
2236 --
2237 -- Default_Initial_Condition
2238 -- Invariant
2239 -- Type_Invariant
2240 --
2241 -- Context denotes the owner of the declarative list.
2242
2243 procedure Check_Entry_Contracts;
2244 -- Perform a preanalysis of the pre- and postconditions of an entry
2245 -- declaration. This must be done before full resolution and creation
2246 -- of the parameter block, etc. to catch illegal uses within the
2247 -- contract expression. Full analysis of the expression is done when
2248 -- the contract is processed.
2249
2250 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2251 -- Check if a nested package has entities within it that rely on library
2252 -- level private types where the full view has not been completed for
2253 -- the purposes of checking if it is acceptable to freeze an expression
2254 -- function at the point of declaration.
2255
2256 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2257 -- Determine whether Body_Decl denotes the body of a late controlled
2258 -- primitive (either Initialize, Adjust or Finalize). If this is the
2259 -- case, add a proper spec if the body lacks one. The spec is inserted
2260 -- before Body_Decl and immediately analyzed.
2261
2262 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2263 -- Spec_Id is the entity of a package that may define abstract states,
2264 -- and in the case of a child unit, whose ancestors may define abstract
2265 -- states. If the states have partial visible refinement, remove the
2266 -- partial visibility of each constituent at the end of the package
2267 -- spec and body declarations.
2268
2269 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2270 -- Spec_Id is the entity of a package that may define abstract states.
2271 -- If the states have visible refinement, remove the visibility of each
2272 -- constituent at the end of the package body declaration.
2273
2274 procedure Resolve_Aspects;
2275 -- Utility to resolve the expressions of aspects at the end of a list of
2276 -- declarations, or before a declaration that freezes previous entities,
2277 -- such as in a subprogram body.
2278
2279 -----------------
2280 -- Adjust_Decl --
2281 -----------------
2282
2283 procedure Adjust_Decl is
2284 begin
2285 while Present (Prev (Decl))
2286 and then Nkind (Decl) = N_Implicit_Label_Declaration
2287 loop
2288 Prev (Decl);
2289 end loop;
2290 end Adjust_Decl;
2291
2292 ----------------------------
2293 -- Build_Assertion_Bodies --
2294 ----------------------------
2295
2296 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2297 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2298 -- Create the subprogram bodies which verify the run-time semantics
2299 -- of the pragmas listed below for type Typ. The pragmas are:
2300 --
2301 -- Default_Initial_Condition
2302 -- Invariant
2303 -- Type_Invariant
2304
2305 -------------------------------------
2306 -- Build_Assertion_Bodies_For_Type --
2307 -------------------------------------
2308
2309 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2310 begin
2311 -- Preanalyze and resolve the Default_Initial_Condition assertion
2312 -- expression at the end of the declarations to catch any errors.
2313
2314 if Has_DIC (Typ) then
2315 Build_DIC_Procedure_Body (Typ);
2316 end if;
2317
2318 if Nkind (Context) = N_Package_Specification then
2319
2320 -- Preanalyze and resolve the class-wide invariants of an
2321 -- interface at the end of whichever declarative part has the
2322 -- interface type. Note that an interface may be declared in
2323 -- any non-package declarative part, but reaching the end of
2324 -- such a declarative part will always freeze the type and
2325 -- generate the invariant procedure (see Freeze_Type).
2326
2327 if Is_Interface (Typ) then
2328
2329 -- Interfaces are treated as the partial view of a private
2330 -- type, in order to achieve uniformity with the general
2331 -- case. As a result, an interface receives only a "partial"
2332 -- invariant procedure, which is never called.
2333
2334 if Has_Own_Invariants (Typ) then
2335 Build_Invariant_Procedure_Body
2336 (Typ => Typ,
2337 Partial_Invariant => True);
2338 end if;
2339
2340 -- Preanalyze and resolve the invariants of a private type
2341 -- at the end of the visible declarations to catch potential
2342 -- errors. Inherited class-wide invariants are not included
2343 -- because they have already been resolved.
2344
2345 elsif Decls = Visible_Declarations (Context)
2346 and then Ekind_In (Typ, E_Limited_Private_Type,
2347 E_Private_Type,
2348 E_Record_Type_With_Private)
2349 and then Has_Own_Invariants (Typ)
2350 then
2351 Build_Invariant_Procedure_Body
2352 (Typ => Typ,
2353 Partial_Invariant => True);
2354
2355 -- Preanalyze and resolve the invariants of a private type's
2356 -- full view at the end of the private declarations to catch
2357 -- potential errors.
2358
2359 elsif Decls = Private_Declarations (Context)
2360 and then (not Is_Private_Type (Typ)
2361 or else Present (Underlying_Full_View (Typ)))
2362 and then Has_Private_Declaration (Typ)
2363 and then Has_Invariants (Typ)
2364 then
2365 Build_Invariant_Procedure_Body (Typ);
2366 end if;
2367 end if;
2368 end Build_Assertion_Bodies_For_Type;
2369
2370 -- Local variables
2371
2372 Decl : Node_Id;
2373 Decl_Id : Entity_Id;
2374
2375 -- Start of processing for Build_Assertion_Bodies
2376
2377 begin
2378 Decl := First (Decls);
2379 while Present (Decl) loop
2380 if Is_Declaration (Decl) then
2381 Decl_Id := Defining_Entity (Decl);
2382
2383 if Is_Type (Decl_Id) then
2384 Build_Assertion_Bodies_For_Type (Decl_Id);
2385 end if;
2386 end if;
2387
2388 Next (Decl);
2389 end loop;
2390 end Build_Assertion_Bodies;
2391
2392 ---------------------------
2393 -- Check_Entry_Contracts --
2394 ---------------------------
2395
2396 procedure Check_Entry_Contracts is
2397 ASN : Node_Id;
2398 Ent : Entity_Id;
2399 Exp : Node_Id;
2400
2401 begin
2402 Ent := First_Entity (Current_Scope);
2403 while Present (Ent) loop
2404
2405 -- This only concerns entries with pre/postconditions
2406
2407 if Ekind (Ent) = E_Entry
2408 and then Present (Contract (Ent))
2409 and then Present (Pre_Post_Conditions (Contract (Ent)))
2410 then
2411 ASN := Pre_Post_Conditions (Contract (Ent));
2412 Push_Scope (Ent);
2413 Install_Formals (Ent);
2414
2415 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2416 -- is performed on a copy of the pragma expression, to prevent
2417 -- modifying the original expression.
2418
2419 while Present (ASN) loop
2420 if Nkind (ASN) = N_Pragma then
2421 Exp :=
2422 New_Copy_Tree
2423 (Expression
2424 (First (Pragma_Argument_Associations (ASN))));
2425 Set_Parent (Exp, ASN);
2426
2427 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2428 end if;
2429
2430 ASN := Next_Pragma (ASN);
2431 end loop;
2432
2433 End_Scope;
2434 end if;
2435
2436 Next_Entity (Ent);
2437 end loop;
2438 end Check_Entry_Contracts;
2439
2440 ----------------------------------
2441 -- Contains_Lib_Incomplete_Type --
2442 ----------------------------------
2443
2444 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2445 Curr : Entity_Id;
2446
2447 begin
2448 -- Avoid looking through scopes that do not meet the precondition of
2449 -- Pkg not being within a library unit spec.
2450
2451 if not Is_Compilation_Unit (Pkg)
2452 and then not Is_Generic_Instance (Pkg)
2453 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2454 then
2455 -- Loop through all entities in the current scope to identify
2456 -- an entity that depends on a private type.
2457
2458 Curr := First_Entity (Pkg);
2459 loop
2460 if Nkind (Curr) in N_Entity
2461 and then Depends_On_Private (Curr)
2462 then
2463 return True;
2464 end if;
2465
2466 exit when Last_Entity (Current_Scope) = Curr;
2467 Next_Entity (Curr);
2468 end loop;
2469 end if;
2470
2471 return False;
2472 end Contains_Lib_Incomplete_Type;
2473
2474 --------------------------------------
2475 -- Handle_Late_Controlled_Primitive --
2476 --------------------------------------
2477
2478 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2479 Body_Spec : constant Node_Id := Specification (Body_Decl);
2480 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2481 Loc : constant Source_Ptr := Sloc (Body_Id);
2482 Params : constant List_Id :=
2483 Parameter_Specifications (Body_Spec);
2484 Spec : Node_Id;
2485 Spec_Id : Entity_Id;
2486 Typ : Node_Id;
2487
2488 begin
2489 -- Consider only procedure bodies whose name matches one of the three
2490 -- controlled primitives.
2491
2492 if Nkind (Body_Spec) /= N_Procedure_Specification
2493 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2494 Name_Finalize,
2495 Name_Initialize)
2496 then
2497 return;
2498
2499 -- A controlled primitive must have exactly one formal which is not
2500 -- an anonymous access type.
2501
2502 elsif List_Length (Params) /= 1 then
2503 return;
2504 end if;
2505
2506 Typ := Parameter_Type (First (Params));
2507
2508 if Nkind (Typ) = N_Access_Definition then
2509 return;
2510 end if;
2511
2512 Find_Type (Typ);
2513
2514 -- The type of the formal must be derived from [Limited_]Controlled
2515
2516 if not Is_Controlled (Entity (Typ)) then
2517 return;
2518 end if;
2519
2520 -- Check whether a specification exists for this body. We do not
2521 -- analyze the spec of the body in full, because it will be analyzed
2522 -- again when the body is properly analyzed, and we cannot create
2523 -- duplicate entries in the formals chain. We look for an explicit
2524 -- specification because the body may be an overriding operation and
2525 -- an inherited spec may be present.
2526
2527 Spec_Id := Current_Entity (Body_Id);
2528
2529 while Present (Spec_Id) loop
2530 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2531 and then Scope (Spec_Id) = Current_Scope
2532 and then Present (First_Formal (Spec_Id))
2533 and then No (Next_Formal (First_Formal (Spec_Id)))
2534 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2535 and then Comes_From_Source (Spec_Id)
2536 then
2537 return;
2538 end if;
2539
2540 Spec_Id := Homonym (Spec_Id);
2541 end loop;
2542
2543 -- At this point the body is known to be a late controlled primitive.
2544 -- Generate a matching spec and insert it before the body. Note the
2545 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2546 -- tree in this case.
2547
2548 Spec := Copy_Separate_Tree (Body_Spec);
2549
2550 -- Ensure that the subprogram declaration does not inherit the null
2551 -- indicator from the body as we now have a proper spec/body pair.
2552
2553 Set_Null_Present (Spec, False);
2554
2555 -- Ensure that the freeze node is inserted after the declaration of
2556 -- the primitive since its expansion will freeze the primitive.
2557
2558 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2559
2560 Insert_Before_And_Analyze (Body_Decl, Decl);
2561 end Handle_Late_Controlled_Primitive;
2562
2563 ----------------------------------------
2564 -- Remove_Partial_Visible_Refinements --
2565 ----------------------------------------
2566
2567 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2568 State_Elmt : Elmt_Id;
2569 begin
2570 if Present (Abstract_States (Spec_Id)) then
2571 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2572 while Present (State_Elmt) loop
2573 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2574 Next_Elmt (State_Elmt);
2575 end loop;
2576 end if;
2577
2578 -- For a child unit, also hide the partial state refinement from
2579 -- ancestor packages.
2580
2581 if Is_Child_Unit (Spec_Id) then
2582 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2583 end if;
2584 end Remove_Partial_Visible_Refinements;
2585
2586 --------------------------------
2587 -- Remove_Visible_Refinements --
2588 --------------------------------
2589
2590 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2591 State_Elmt : Elmt_Id;
2592 begin
2593 if Present (Abstract_States (Spec_Id)) then
2594 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2595 while Present (State_Elmt) loop
2596 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2597 Next_Elmt (State_Elmt);
2598 end loop;
2599 end if;
2600 end Remove_Visible_Refinements;
2601
2602 ---------------------
2603 -- Resolve_Aspects --
2604 ---------------------
2605
2606 procedure Resolve_Aspects is
2607 E : Entity_Id;
2608
2609 begin
2610 E := First_Entity (Current_Scope);
2611 while Present (E) loop
2612 Resolve_Aspect_Expressions (E);
2613 Next_Entity (E);
2614 end loop;
2615 end Resolve_Aspects;
2616
2617 -- Local variables
2618
2619 Context : Node_Id := Empty;
2620 Ctrl_Typ : Entity_Id := Empty;
2621 Freeze_From : Entity_Id := Empty;
2622 Next_Decl : Node_Id;
2623
2624 -- Start of processing for Analyze_Declarations
2625
2626 begin
2627 Decl := First (L);
2628 while Present (Decl) loop
2629
2630 -- Complete analysis of declaration
2631
2632 Analyze (Decl);
2633 Next_Decl := Next (Decl);
2634
2635 if No (Freeze_From) then
2636 Freeze_From := First_Entity (Current_Scope);
2637 end if;
2638
2639 -- Remember if the declaration we just processed is the full type
2640 -- declaration of a controlled type (to handle late overriding of
2641 -- initialize, adjust or finalize).
2642
2643 if Nkind (Decl) = N_Full_Type_Declaration
2644 and then Is_Controlled (Defining_Identifier (Decl))
2645 then
2646 Ctrl_Typ := Defining_Identifier (Decl);
2647 end if;
2648
2649 -- At the end of a declarative part, freeze remaining entities
2650 -- declared in it. The end of the visible declarations of package
2651 -- specification is not the end of a declarative part if private
2652 -- declarations are present. The end of a package declaration is a
2653 -- freezing point only if it a library package. A task definition or
2654 -- protected type definition is not a freeze point either. Finally,
2655 -- we do not freeze entities in generic scopes, because there is no
2656 -- code generated for them and freeze nodes will be generated for
2657 -- the instance.
2658
2659 -- The end of a package instantiation is not a freeze point, but
2660 -- for now we make it one, because the generic body is inserted
2661 -- (currently) immediately after. Generic instantiations will not
2662 -- be a freeze point once delayed freezing of bodies is implemented.
2663 -- (This is needed in any case for early instantiations ???).
2664
2665 if No (Next_Decl) then
2666 if Nkind (Parent (L)) = N_Component_List then
2667 null;
2668
2669 elsif Nkind_In (Parent (L), N_Protected_Definition,
2670 N_Task_Definition)
2671 then
2672 Check_Entry_Contracts;
2673
2674 elsif Nkind (Parent (L)) /= N_Package_Specification then
2675 if Nkind (Parent (L)) = N_Package_Body then
2676 Freeze_From := First_Entity (Current_Scope);
2677 end if;
2678
2679 -- There may have been several freezing points previously,
2680 -- for example object declarations or subprogram bodies, but
2681 -- at the end of a declarative part we check freezing from
2682 -- the beginning, even though entities may already be frozen,
2683 -- in order to perform visibility checks on delayed aspects.
2684
2685 Adjust_Decl;
2686
2687 -- If the current scope is a generic subprogram body. Skip the
2688 -- generic formal parameters that are not frozen here.
2689
2690 if Is_Subprogram (Current_Scope)
2691 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2692 N_Generic_Subprogram_Declaration
2693 and then Present (First_Entity (Current_Scope))
2694 then
2695 while Is_Generic_Formal (Freeze_From) loop
2696 Next_Entity (Freeze_From);
2697 end loop;
2698
2699 Freeze_All (Freeze_From, Decl);
2700 Freeze_From := Last_Entity (Current_Scope);
2701
2702 else
2703 -- For declarations in a subprogram body there is no issue
2704 -- with name resolution in aspect specifications.
2705
2706 Freeze_All (First_Entity (Current_Scope), Decl);
2707 Freeze_From := Last_Entity (Current_Scope);
2708 end if;
2709
2710 -- Current scope is a package specification
2711
2712 elsif Scope (Current_Scope) /= Standard_Standard
2713 and then not Is_Child_Unit (Current_Scope)
2714 and then No (Generic_Parent (Parent (L)))
2715 then
2716 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2717 -- resolved at the end of the immediately enclosing declaration
2718 -- list (AI05-0183-1).
2719
2720 Resolve_Aspects;
2721
2722 elsif L /= Visible_Declarations (Parent (L))
2723 or else No (Private_Declarations (Parent (L)))
2724 or else Is_Empty_List (Private_Declarations (Parent (L)))
2725 then
2726 Adjust_Decl;
2727
2728 -- End of a package declaration
2729
2730 -- This is a freeze point because it is the end of a
2731 -- compilation unit.
2732
2733 Freeze_All (First_Entity (Current_Scope), Decl);
2734 Freeze_From := Last_Entity (Current_Scope);
2735
2736 -- At the end of the visible declarations the expressions in
2737 -- aspects of all entities declared so far must be resolved.
2738 -- The entities themselves might be frozen later, and the
2739 -- generated pragmas and attribute definition clauses analyzed
2740 -- in full at that point, but name resolution must take place
2741 -- now.
2742 -- In addition to being the proper semantics, this is mandatory
2743 -- within generic units, because global name capture requires
2744 -- those expressions to be analyzed, given that the generated
2745 -- pragmas do not appear in the original generic tree.
2746
2747 elsif Serious_Errors_Detected = 0 then
2748 Resolve_Aspects;
2749 end if;
2750
2751 -- If next node is a body then freeze all types before the body.
2752 -- An exception occurs for some expander-generated bodies. If these
2753 -- are generated at places where in general language rules would not
2754 -- allow a freeze point, then we assume that the expander has
2755 -- explicitly checked that all required types are properly frozen,
2756 -- and we do not cause general freezing here. This special circuit
2757 -- is used when the encountered body is marked as having already
2758 -- been analyzed.
2759
2760 -- In all other cases (bodies that come from source, and expander
2761 -- generated bodies that have not been analyzed yet), freeze all
2762 -- types now. Note that in the latter case, the expander must take
2763 -- care to attach the bodies at a proper place in the tree so as to
2764 -- not cause unwanted freezing at that point.
2765
2766 -- It is also necessary to check for a case where both an expression
2767 -- function is used and the current scope depends on an incomplete
2768 -- private type from a library unit, otherwise premature freezing of
2769 -- the private type will occur.
2770
2771 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2772 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2773 or else not Was_Expression_Function (Next_Decl))
2774 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2775 and then not Contains_Lib_Incomplete_Type
2776 (Current_Scope)))
2777 then
2778 -- When a controlled type is frozen, the expander generates stream
2779 -- and controlled-type support routines. If the freeze is caused
2780 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2781 -- expander will end up using the wrong version of these routines,
2782 -- as the body has not been processed yet. To remedy this, detect
2783 -- a late controlled primitive and create a proper spec for it.
2784 -- This ensures that the primitive will override its inherited
2785 -- counterpart before the freeze takes place.
2786
2787 -- If the declaration we just processed is a body, do not attempt
2788 -- to examine Next_Decl as the late primitive idiom can only apply
2789 -- to the first encountered body.
2790
2791 -- ??? A cleaner approach may be possible and/or this solution
2792 -- could be extended to general-purpose late primitives, TBD.
2793
2794 if Present (Ctrl_Typ) then
2795
2796 -- No need to continue searching for late body overriding if
2797 -- the controlled type is already frozen.
2798
2799 if Is_Frozen (Ctrl_Typ) then
2800 Ctrl_Typ := Empty;
2801
2802 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2803 Handle_Late_Controlled_Primitive (Next_Decl);
2804 end if;
2805 end if;
2806
2807 Adjust_Decl;
2808
2809 -- The generated body of an expression function does not freeze,
2810 -- unless it is a completion, in which case only the expression
2811 -- itself freezes. This is handled when the body itself is
2812 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2813
2814 Freeze_All (Freeze_From, Decl);
2815 Freeze_From := Last_Entity (Current_Scope);
2816 end if;
2817
2818 Decl := Next_Decl;
2819 end loop;
2820
2821 -- Post-freezing actions
2822
2823 if Present (L) then
2824 Context := Parent (L);
2825
2826 -- Certain contract annotations have forward visibility semantics and
2827 -- must be analyzed after all declarative items have been processed.
2828 -- This timing ensures that entities referenced by such contracts are
2829 -- visible.
2830
2831 -- Analyze the contract of an immediately enclosing package spec or
2832 -- body first because other contracts may depend on its information.
2833
2834 if Nkind (Context) = N_Package_Body then
2835 Analyze_Package_Body_Contract (Defining_Entity (Context));
2836
2837 elsif Nkind (Context) = N_Package_Specification then
2838 Analyze_Package_Contract (Defining_Entity (Context));
2839 end if;
2840
2841 -- Analyze the contracts of various constructs in the declarative
2842 -- list.
2843
2844 Analyze_Contracts (L);
2845
2846 if Nkind (Context) = N_Package_Body then
2847
2848 -- Ensure that all abstract states and objects declared in the
2849 -- state space of a package body are utilized as constituents.
2850
2851 Check_Unused_Body_States (Defining_Entity (Context));
2852
2853 -- State refinements are visible up to the end of the package body
2854 -- declarations. Hide the state refinements from visibility to
2855 -- restore the original state conditions.
2856
2857 Remove_Visible_Refinements (Corresponding_Spec (Context));
2858 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2859
2860 elsif Nkind (Context) = N_Package_Specification then
2861
2862 -- Partial state refinements are visible up to the end of the
2863 -- package spec declarations. Hide the partial state refinements
2864 -- from visibility to restore the original state conditions.
2865
2866 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2867 end if;
2868
2869 -- Verify that all abstract states found in any package declared in
2870 -- the input declarative list have proper refinements. The check is
2871 -- performed only when the context denotes a block, entry, package,
2872 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2873
2874 Check_State_Refinements (Context);
2875
2876 -- Create the subprogram bodies which verify the run-time semantics
2877 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2878 -- types within the current declarative list. This ensures that all
2879 -- assertion expressions are preanalyzed and resolved at the end of
2880 -- the declarative part. Note that the resolution happens even when
2881 -- freezing does not take place.
2882
2883 Build_Assertion_Bodies (L, Context);
2884 end if;
2885 end Analyze_Declarations;
2886
2887 -----------------------------------
2888 -- Analyze_Full_Type_Declaration --
2889 -----------------------------------
2890
2891 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2892 Def : constant Node_Id := Type_Definition (N);
2893 Def_Id : constant Entity_Id := Defining_Identifier (N);
2894 T : Entity_Id;
2895 Prev : Entity_Id;
2896
2897 Is_Remote : constant Boolean :=
2898 (Is_Remote_Types (Current_Scope)
2899 or else Is_Remote_Call_Interface (Current_Scope))
2900 and then not (In_Private_Part (Current_Scope)
2901 or else In_Package_Body (Current_Scope));
2902
2903 procedure Check_Nonoverridable_Aspects;
2904 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2905 -- be overridden, and can only be confirmed on derivation.
2906
2907 procedure Check_Ops_From_Incomplete_Type;
2908 -- If there is a tagged incomplete partial view of the type, traverse
2909 -- the primitives of the incomplete view and change the type of any
2910 -- controlling formals and result to indicate the full view. The
2911 -- primitives will be added to the full type's primitive operations
2912 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2913 -- is called from Process_Incomplete_Dependents).
2914
2915 ----------------------------------
2916 -- Check_Nonoverridable_Aspects --
2917 ----------------------------------
2918
2919 procedure Check_Nonoverridable_Aspects is
2920 function Get_Aspect_Spec
2921 (Specs : List_Id;
2922 Aspect_Name : Name_Id) return Node_Id;
2923 -- Check whether a list of aspect specifications includes an entry
2924 -- for a specific aspect. The list is either that of a partial or
2925 -- a full view.
2926
2927 ---------------------
2928 -- Get_Aspect_Spec --
2929 ---------------------
2930
2931 function Get_Aspect_Spec
2932 (Specs : List_Id;
2933 Aspect_Name : Name_Id) return Node_Id
2934 is
2935 Spec : Node_Id;
2936
2937 begin
2938 Spec := First (Specs);
2939 while Present (Spec) loop
2940 if Chars (Identifier (Spec)) = Aspect_Name then
2941 return Spec;
2942 end if;
2943 Next (Spec);
2944 end loop;
2945
2946 return Empty;
2947 end Get_Aspect_Spec;
2948
2949 -- Local variables
2950
2951 Prev_Aspects : constant List_Id :=
2952 Aspect_Specifications (Parent (Def_Id));
2953 Par_Type : Entity_Id;
2954 Prev_Aspect : Node_Id;
2955
2956 -- Start of processing for Check_Nonoverridable_Aspects
2957
2958 begin
2959 -- Get parent type of derived type. Note that Prev is the entity in
2960 -- the partial declaration, but its contents are now those of full
2961 -- view, while Def_Id reflects the partial view.
2962
2963 if Is_Private_Type (Def_Id) then
2964 Par_Type := Etype (Full_View (Def_Id));
2965 else
2966 Par_Type := Etype (Def_Id);
2967 end if;
2968
2969 -- If there is an inherited Implicit_Dereference, verify that it is
2970 -- made explicit in the partial view.
2971
2972 if Has_Discriminants (Base_Type (Par_Type))
2973 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2974 and then Present (Discriminant_Specifications (Parent (Prev)))
2975 and then Present (Get_Reference_Discriminant (Par_Type))
2976 then
2977 Prev_Aspect :=
2978 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2979
2980 if No (Prev_Aspect)
2981 and then Present
2982 (Discriminant_Specifications
2983 (Original_Node (Parent (Prev))))
2984 then
2985 Error_Msg_N
2986 ("type does not inherit implicit dereference", Prev);
2987
2988 else
2989 -- If one of the views has the aspect specified, verify that it
2990 -- is consistent with that of the parent.
2991
2992 declare
2993 Cur_Discr : constant Entity_Id :=
2994 Get_Reference_Discriminant (Prev);
2995 Par_Discr : constant Entity_Id :=
2996 Get_Reference_Discriminant (Par_Type);
2997
2998 begin
2999 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3000 Error_Msg_N
3001 ("aspect inconsistent with that of parent", N);
3002 end if;
3003
3004 -- Check that specification in partial view matches the
3005 -- inherited aspect. Compare names directly because aspect
3006 -- expression may not be analyzed.
3007
3008 if Present (Prev_Aspect)
3009 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3010 and then Chars (Expression (Prev_Aspect)) /=
3011 Chars (Cur_Discr)
3012 then
3013 Error_Msg_N
3014 ("aspect inconsistent with that of parent", N);
3015 end if;
3016 end;
3017 end if;
3018 end if;
3019
3020 -- TBD : other nonoverridable aspects.
3021 end Check_Nonoverridable_Aspects;
3022
3023 ------------------------------------
3024 -- Check_Ops_From_Incomplete_Type --
3025 ------------------------------------
3026
3027 procedure Check_Ops_From_Incomplete_Type is
3028 Elmt : Elmt_Id;
3029 Formal : Entity_Id;
3030 Op : Entity_Id;
3031
3032 begin
3033 if Prev /= T
3034 and then Ekind (Prev) = E_Incomplete_Type
3035 and then Is_Tagged_Type (Prev)
3036 and then Is_Tagged_Type (T)
3037 then
3038 Elmt := First_Elmt (Primitive_Operations (Prev));
3039 while Present (Elmt) loop
3040 Op := Node (Elmt);
3041
3042 Formal := First_Formal (Op);
3043 while Present (Formal) loop
3044 if Etype (Formal) = Prev then
3045 Set_Etype (Formal, T);
3046 end if;
3047
3048 Next_Formal (Formal);
3049 end loop;
3050
3051 if Etype (Op) = Prev then
3052 Set_Etype (Op, T);
3053 end if;
3054
3055 Next_Elmt (Elmt);
3056 end loop;
3057 end if;
3058 end Check_Ops_From_Incomplete_Type;
3059
3060 -- Start of processing for Analyze_Full_Type_Declaration
3061
3062 begin
3063 Prev := Find_Type_Name (N);
3064
3065 -- The full view, if present, now points to the current type. If there
3066 -- is an incomplete partial view, set a link to it, to simplify the
3067 -- retrieval of primitive operations of the type.
3068
3069 -- Ada 2005 (AI-50217): If the type was previously decorated when
3070 -- imported through a LIMITED WITH clause, it appears as incomplete
3071 -- but has no full view.
3072
3073 if Ekind (Prev) = E_Incomplete_Type
3074 and then Present (Full_View (Prev))
3075 then
3076 T := Full_View (Prev);
3077 Set_Incomplete_View (N, Parent (Prev));
3078 else
3079 T := Prev;
3080 end if;
3081
3082 Set_Is_Pure (T, Is_Pure (Current_Scope));
3083
3084 -- We set the flag Is_First_Subtype here. It is needed to set the
3085 -- corresponding flag for the Implicit class-wide-type created
3086 -- during tagged types processing.
3087
3088 Set_Is_First_Subtype (T, True);
3089
3090 -- Only composite types other than array types are allowed to have
3091 -- discriminants.
3092
3093 case Nkind (Def) is
3094
3095 -- For derived types, the rule will be checked once we've figured
3096 -- out the parent type.
3097
3098 when N_Derived_Type_Definition =>
3099 null;
3100
3101 -- For record types, discriminants are allowed.
3102
3103 when N_Record_Definition =>
3104 null;
3105
3106 when others =>
3107 if Present (Discriminant_Specifications (N)) then
3108 Error_Msg_N
3109 ("elementary or array type cannot have discriminants",
3110 Defining_Identifier
3111 (First (Discriminant_Specifications (N))));
3112 end if;
3113 end case;
3114
3115 -- Elaborate the type definition according to kind, and generate
3116 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3117 -- already done (this happens during the reanalysis that follows a call
3118 -- to the high level optimizer).
3119
3120 if not Analyzed (T) then
3121 Set_Analyzed (T);
3122
3123 -- Set the SPARK mode from the current context
3124
3125 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3126 Set_SPARK_Pragma_Inherited (T);
3127
3128 case Nkind (Def) is
3129 when N_Access_To_Subprogram_Definition =>
3130 Access_Subprogram_Declaration (T, Def);
3131
3132 -- If this is a remote access to subprogram, we must create the
3133 -- equivalent fat pointer type, and related subprograms.
3134
3135 if Is_Remote then
3136 Process_Remote_AST_Declaration (N);
3137 end if;
3138
3139 -- Validate categorization rule against access type declaration
3140 -- usually a violation in Pure unit, Shared_Passive unit.
3141
3142 Validate_Access_Type_Declaration (T, N);
3143
3144 -- If the type has contracts, we create the corresponding
3145 -- wrapper at once, before analyzing the aspect specifications,
3146 -- so that pre/postconditions can be handled directly on the
3147 -- generated wrapper.
3148
3149 if Ada_Version >= Ada_2020
3150 and then Present (Aspect_Specifications (N))
3151 then
3152 Build_Access_Subprogram_Wrapper (N);
3153 end if;
3154
3155 when N_Access_To_Object_Definition =>
3156 Access_Type_Declaration (T, Def);
3157
3158 -- Validate categorization rule against access type declaration
3159 -- usually a violation in Pure unit, Shared_Passive unit.
3160
3161 Validate_Access_Type_Declaration (T, N);
3162
3163 -- If we are in a Remote_Call_Interface package and define a
3164 -- RACW, then calling stubs and specific stream attributes
3165 -- must be added.
3166
3167 if Is_Remote
3168 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3169 then
3170 Add_RACW_Features (Def_Id);
3171 end if;
3172
3173 when N_Array_Type_Definition =>
3174 Array_Type_Declaration (T, Def);
3175
3176 when N_Derived_Type_Definition =>
3177 Derived_Type_Declaration (T, N, T /= Def_Id);
3178
3179 -- Inherit predicates from parent, and protect against illegal
3180 -- derivations.
3181
3182 if Is_Type (T) and then Has_Predicates (T) then
3183 Set_Has_Predicates (Def_Id);
3184 end if;
3185
3186 -- Save the scenario for examination by the ABE Processing
3187 -- phase.
3188
3189 Record_Elaboration_Scenario (N);
3190
3191 when N_Enumeration_Type_Definition =>
3192 Enumeration_Type_Declaration (T, Def);
3193
3194 when N_Floating_Point_Definition =>
3195 Floating_Point_Type_Declaration (T, Def);
3196
3197 when N_Decimal_Fixed_Point_Definition =>
3198 Decimal_Fixed_Point_Type_Declaration (T, Def);
3199
3200 when N_Ordinary_Fixed_Point_Definition =>
3201 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3202
3203 when N_Signed_Integer_Type_Definition =>
3204 Signed_Integer_Type_Declaration (T, Def);
3205
3206 when N_Modular_Type_Definition =>
3207 Modular_Type_Declaration (T, Def);
3208
3209 when N_Record_Definition =>
3210 Record_Type_Declaration (T, N, Prev);
3211
3212 -- If declaration has a parse error, nothing to elaborate.
3213
3214 when N_Error =>
3215 null;
3216
3217 when others =>
3218 raise Program_Error;
3219 end case;
3220 end if;
3221
3222 if Etype (T) = Any_Type then
3223 return;
3224 end if;
3225
3226 -- Some common processing for all types
3227
3228 Set_Depends_On_Private (T, Has_Private_Component (T));
3229 Check_Ops_From_Incomplete_Type;
3230
3231 -- Both the declared entity, and its anonymous base type if one was
3232 -- created, need freeze nodes allocated.
3233
3234 declare
3235 B : constant Entity_Id := Base_Type (T);
3236
3237 begin
3238 -- In the case where the base type differs from the first subtype, we
3239 -- pre-allocate a freeze node, and set the proper link to the first
3240 -- subtype. Freeze_Entity will use this preallocated freeze node when
3241 -- it freezes the entity.
3242
3243 -- This does not apply if the base type is a generic type, whose
3244 -- declaration is independent of the current derived definition.
3245
3246 if B /= T and then not Is_Generic_Type (B) then
3247 Ensure_Freeze_Node (B);
3248 Set_First_Subtype_Link (Freeze_Node (B), T);
3249 end if;
3250
3251 -- A type that is imported through a limited_with clause cannot
3252 -- generate any code, and thus need not be frozen. However, an access
3253 -- type with an imported designated type needs a finalization list,
3254 -- which may be referenced in some other package that has non-limited
3255 -- visibility on the designated type. Thus we must create the
3256 -- finalization list at the point the access type is frozen, to
3257 -- prevent unsatisfied references at link time.
3258
3259 if not From_Limited_With (T) or else Is_Access_Type (T) then
3260 Set_Has_Delayed_Freeze (T);
3261 end if;
3262 end;
3263
3264 -- Case where T is the full declaration of some private type which has
3265 -- been swapped in Defining_Identifier (N).
3266
3267 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3268 Process_Full_View (N, T, Def_Id);
3269
3270 -- Record the reference. The form of this is a little strange, since
3271 -- the full declaration has been swapped in. So the first parameter
3272 -- here represents the entity to which a reference is made which is
3273 -- the "real" entity, i.e. the one swapped in, and the second
3274 -- parameter provides the reference location.
3275
3276 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3277 -- since we don't want a complaint about the full type being an
3278 -- unwanted reference to the private type
3279
3280 declare
3281 B : constant Boolean := Has_Pragma_Unreferenced (T);
3282 begin
3283 Set_Has_Pragma_Unreferenced (T, False);
3284 Generate_Reference (T, T, 'c');
3285 Set_Has_Pragma_Unreferenced (T, B);
3286 end;
3287
3288 Set_Completion_Referenced (Def_Id);
3289
3290 -- For completion of incomplete type, process incomplete dependents
3291 -- and always mark the full type as referenced (it is the incomplete
3292 -- type that we get for any real reference).
3293
3294 elsif Ekind (Prev) = E_Incomplete_Type then
3295 Process_Incomplete_Dependents (N, T, Prev);
3296 Generate_Reference (Prev, Def_Id, 'c');
3297 Set_Completion_Referenced (Def_Id);
3298
3299 -- If not private type or incomplete type completion, this is a real
3300 -- definition of a new entity, so record it.
3301
3302 else
3303 Generate_Definition (Def_Id);
3304 end if;
3305
3306 -- Propagate any pending access types whose finalization masters need to
3307 -- be fully initialized from the partial to the full view. Guard against
3308 -- an illegal full view that remains unanalyzed.
3309
3310 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3311 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3312 end if;
3313
3314 if Chars (Scope (Def_Id)) = Name_System
3315 and then Chars (Def_Id) = Name_Address
3316 and then In_Predefined_Unit (N)
3317 then
3318 Set_Is_Descendant_Of_Address (Def_Id);
3319 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3320 Set_Is_Descendant_Of_Address (Prev);
3321 end if;
3322
3323 Set_Optimize_Alignment_Flags (Def_Id);
3324 Check_Eliminated (Def_Id);
3325
3326 -- If the declaration is a completion and aspects are present, apply
3327 -- them to the entity for the type which is currently the partial
3328 -- view, but which is the one that will be frozen.
3329
3330 if Has_Aspects (N) then
3331
3332 -- In most cases the partial view is a private type, and both views
3333 -- appear in different declarative parts. In the unusual case where
3334 -- the partial view is incomplete, perform the analysis on the
3335 -- full view, to prevent freezing anomalies with the corresponding
3336 -- class-wide type, which otherwise might be frozen before the
3337 -- dispatch table is built.
3338
3339 if Prev /= Def_Id
3340 and then Ekind (Prev) /= E_Incomplete_Type
3341 then
3342 Analyze_Aspect_Specifications (N, Prev);
3343
3344 -- Normal case
3345
3346 else
3347 Analyze_Aspect_Specifications (N, Def_Id);
3348 end if;
3349 end if;
3350
3351 if Is_Derived_Type (Prev)
3352 and then Def_Id /= Prev
3353 then
3354 Check_Nonoverridable_Aspects;
3355 end if;
3356 end Analyze_Full_Type_Declaration;
3357
3358 ----------------------------------
3359 -- Analyze_Incomplete_Type_Decl --
3360 ----------------------------------
3361
3362 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3363 F : constant Boolean := Is_Pure (Current_Scope);
3364 T : Entity_Id;
3365
3366 begin
3367 Generate_Definition (Defining_Identifier (N));
3368
3369 -- Process an incomplete declaration. The identifier must not have been
3370 -- declared already in the scope. However, an incomplete declaration may
3371 -- appear in the private part of a package, for a private type that has
3372 -- already been declared.
3373
3374 -- In this case, the discriminants (if any) must match
3375
3376 T := Find_Type_Name (N);
3377
3378 Set_Ekind (T, E_Incomplete_Type);
3379 Set_Etype (T, T);
3380 Set_Is_First_Subtype (T);
3381 Init_Size_Align (T);
3382
3383 -- Set the SPARK mode from the current context
3384
3385 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3386 Set_SPARK_Pragma_Inherited (T);
3387
3388 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3389 -- incomplete types.
3390
3391 if Tagged_Present (N) then
3392 Set_Is_Tagged_Type (T, True);
3393 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3394 Make_Class_Wide_Type (T);
3395 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3396 end if;
3397
3398 Set_Stored_Constraint (T, No_Elist);
3399
3400 if Present (Discriminant_Specifications (N)) then
3401 Push_Scope (T);
3402 Process_Discriminants (N);
3403 End_Scope;
3404 end if;
3405
3406 -- If the type has discriminants, nontrivial subtypes may be declared
3407 -- before the full view of the type. The full views of those subtypes
3408 -- will be built after the full view of the type.
3409
3410 Set_Private_Dependents (T, New_Elmt_List);
3411 Set_Is_Pure (T, F);
3412 end Analyze_Incomplete_Type_Decl;
3413
3414 -----------------------------------
3415 -- Analyze_Interface_Declaration --
3416 -----------------------------------
3417
3418 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3419 CW : constant Entity_Id := Class_Wide_Type (T);
3420
3421 begin
3422 Set_Is_Tagged_Type (T);
3423 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3424
3425 Set_Is_Limited_Record (T, Limited_Present (Def)
3426 or else Task_Present (Def)
3427 or else Protected_Present (Def)
3428 or else Synchronized_Present (Def));
3429
3430 -- Type is abstract if full declaration carries keyword, or if previous
3431 -- partial view did.
3432
3433 Set_Is_Abstract_Type (T);
3434 Set_Is_Interface (T);
3435
3436 -- Type is a limited interface if it includes the keyword limited, task,
3437 -- protected, or synchronized.
3438
3439 Set_Is_Limited_Interface
3440 (T, Limited_Present (Def)
3441 or else Protected_Present (Def)
3442 or else Synchronized_Present (Def)
3443 or else Task_Present (Def));
3444
3445 Set_Interfaces (T, New_Elmt_List);
3446 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3447
3448 -- Complete the decoration of the class-wide entity if it was already
3449 -- built (i.e. during the creation of the limited view)
3450
3451 if Present (CW) then
3452 Set_Is_Interface (CW);
3453 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3454 end if;
3455
3456 -- Check runtime support for synchronized interfaces
3457
3458 if (Is_Task_Interface (T)
3459 or else Is_Protected_Interface (T)
3460 or else Is_Synchronized_Interface (T))
3461 and then not RTE_Available (RE_Select_Specific_Data)
3462 then
3463 Error_Msg_CRT ("synchronized interfaces", T);
3464 end if;
3465 end Analyze_Interface_Declaration;
3466
3467 -----------------------------
3468 -- Analyze_Itype_Reference --
3469 -----------------------------
3470
3471 -- Nothing to do. This node is placed in the tree only for the benefit of
3472 -- back end processing, and has no effect on the semantic processing.
3473
3474 procedure Analyze_Itype_Reference (N : Node_Id) is
3475 begin
3476 pragma Assert (Is_Itype (Itype (N)));
3477 null;
3478 end Analyze_Itype_Reference;
3479
3480 --------------------------------
3481 -- Analyze_Number_Declaration --
3482 --------------------------------
3483
3484 procedure Analyze_Number_Declaration (N : Node_Id) is
3485 E : constant Node_Id := Expression (N);
3486 Id : constant Entity_Id := Defining_Identifier (N);
3487 Index : Interp_Index;
3488 It : Interp;
3489 T : Entity_Id;
3490
3491 begin
3492 Generate_Definition (Id);
3493 Enter_Name (Id);
3494
3495 -- This is an optimization of a common case of an integer literal
3496
3497 if Nkind (E) = N_Integer_Literal then
3498 Set_Is_Static_Expression (E, True);
3499 Set_Etype (E, Universal_Integer);
3500
3501 Set_Etype (Id, Universal_Integer);
3502 Set_Ekind (Id, E_Named_Integer);
3503 Set_Is_Frozen (Id, True);
3504
3505 Set_Debug_Info_Needed (Id);
3506 return;
3507 end if;
3508
3509 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3510
3511 -- Process expression, replacing error by integer zero, to avoid
3512 -- cascaded errors or aborts further along in the processing
3513
3514 -- Replace Error by integer zero, which seems least likely to cause
3515 -- cascaded errors.
3516
3517 if E = Error then
3518 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3519 Set_Error_Posted (E);
3520 end if;
3521
3522 Analyze (E);
3523
3524 -- Verify that the expression is static and numeric. If
3525 -- the expression is overloaded, we apply the preference
3526 -- rule that favors root numeric types.
3527
3528 if not Is_Overloaded (E) then
3529 T := Etype (E);
3530 if Has_Dynamic_Predicate_Aspect (T) then
3531 Error_Msg_N
3532 ("subtype has dynamic predicate, "
3533 & "not allowed in number declaration", N);
3534 end if;
3535
3536 else
3537 T := Any_Type;
3538
3539 Get_First_Interp (E, Index, It);
3540 while Present (It.Typ) loop
3541 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3542 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3543 then
3544 if T = Any_Type then
3545 T := It.Typ;
3546
3547 elsif It.Typ = Universal_Real
3548 or else
3549 It.Typ = Universal_Integer
3550 then
3551 -- Choose universal interpretation over any other
3552
3553 T := It.Typ;
3554 exit;
3555 end if;
3556 end if;
3557
3558 Get_Next_Interp (Index, It);
3559 end loop;
3560 end if;
3561
3562 if Is_Integer_Type (T) then
3563 Resolve (E, T);
3564 Set_Etype (Id, Universal_Integer);
3565 Set_Ekind (Id, E_Named_Integer);
3566
3567 elsif Is_Real_Type (T) then
3568
3569 -- Because the real value is converted to universal_real, this is a
3570 -- legal context for a universal fixed expression.
3571
3572 if T = Universal_Fixed then
3573 declare
3574 Loc : constant Source_Ptr := Sloc (N);
3575 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3576 Subtype_Mark =>
3577 New_Occurrence_Of (Universal_Real, Loc),
3578 Expression => Relocate_Node (E));
3579
3580 begin
3581 Rewrite (E, Conv);
3582 Analyze (E);
3583 end;
3584
3585 elsif T = Any_Fixed then
3586 Error_Msg_N ("illegal context for mixed mode operation", E);
3587
3588 -- Expression is of the form : universal_fixed * integer. Try to
3589 -- resolve as universal_real.
3590
3591 T := Universal_Real;
3592 Set_Etype (E, T);
3593 end if;
3594
3595 Resolve (E, T);
3596 Set_Etype (Id, Universal_Real);
3597 Set_Ekind (Id, E_Named_Real);
3598
3599 else
3600 Wrong_Type (E, Any_Numeric);
3601 Resolve (E, T);
3602
3603 Set_Etype (Id, T);
3604 Set_Ekind (Id, E_Constant);
3605 Set_Never_Set_In_Source (Id, True);
3606 Set_Is_True_Constant (Id, True);
3607 return;
3608 end if;
3609
3610 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3611 Set_Etype (E, Etype (Id));
3612 end if;
3613
3614 if not Is_OK_Static_Expression (E) then
3615 Flag_Non_Static_Expr
3616 ("non-static expression used in number declaration!", E);
3617 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3618 Set_Etype (E, Any_Type);
3619 end if;
3620
3621 Analyze_Dimension (N);
3622 end Analyze_Number_Declaration;
3623
3624 --------------------------------
3625 -- Analyze_Object_Declaration --
3626 --------------------------------
3627
3628 -- WARNING: This routine manages Ghost regions. Return statements must be
3629 -- replaced by gotos which jump to the end of the routine and restore the
3630 -- Ghost mode.
3631
3632 procedure Analyze_Object_Declaration (N : Node_Id) is
3633 Loc : constant Source_Ptr := Sloc (N);
3634 Id : constant Entity_Id := Defining_Identifier (N);
3635 Next_Decl : constant Node_Id := Next (N);
3636
3637 Act_T : Entity_Id;
3638 T : Entity_Id;
3639
3640 E : Node_Id := Expression (N);
3641 -- E is set to Expression (N) throughout this routine. When Expression
3642 -- (N) is modified, E is changed accordingly.
3643
3644 Prev_Entity : Entity_Id := Empty;
3645
3646 procedure Check_Dynamic_Object (Typ : Entity_Id);
3647 -- A library-level object with nonstatic discriminant constraints may
3648 -- require dynamic allocation. The declaration is illegal if the
3649 -- profile includes the restriction No_Implicit_Heap_Allocations.
3650
3651 procedure Check_For_Null_Excluding_Components
3652 (Obj_Typ : Entity_Id;
3653 Obj_Decl : Node_Id);
3654 -- Verify that each null-excluding component of object declaration
3655 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3656 -- a compile-time warning if this is not the case.
3657
3658 function Count_Tasks (T : Entity_Id) return Uint;
3659 -- This function is called when a non-generic library level object of a
3660 -- task type is declared. Its function is to count the static number of
3661 -- tasks declared within the type (it is only called if Has_Task is set
3662 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3663 -- or a variant record type is encountered, Check_Restriction is called
3664 -- indicating the count is unknown.
3665
3666 function Delayed_Aspect_Present return Boolean;
3667 -- If the declaration has an expression that is an aggregate, and it
3668 -- has aspects that require delayed analysis, the resolution of the
3669 -- aggregate must be deferred to the freeze point of the object. This
3670 -- special processing was created for address clauses, but it must
3671 -- also apply to Alignment. This must be done before the aspect
3672 -- specifications are analyzed because we must handle the aggregate
3673 -- before the analysis of the object declaration is complete.
3674
3675 -- Any other relevant delayed aspects on object declarations ???
3676
3677 --------------------------
3678 -- Check_Dynamic_Object --
3679 --------------------------
3680
3681 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3682 Comp : Entity_Id;
3683 Obj_Type : Entity_Id;
3684
3685 begin
3686 Obj_Type := Typ;
3687
3688 if Is_Private_Type (Obj_Type)
3689 and then Present (Full_View (Obj_Type))
3690 then
3691 Obj_Type := Full_View (Obj_Type);
3692 end if;
3693
3694 if Known_Static_Esize (Obj_Type) then
3695 return;
3696 end if;
3697
3698 if Restriction_Active (No_Implicit_Heap_Allocations)
3699 and then Expander_Active
3700 and then Has_Discriminants (Obj_Type)
3701 then
3702 Comp := First_Component (Obj_Type);
3703 while Present (Comp) loop
3704 if Known_Static_Esize (Etype (Comp))
3705 or else Size_Known_At_Compile_Time (Etype (Comp))
3706 then
3707 null;
3708
3709 elsif not Discriminated_Size (Comp)
3710 and then Comes_From_Source (Comp)
3711 then
3712 Error_Msg_NE
3713 ("component& of non-static size will violate restriction "
3714 & "No_Implicit_Heap_Allocation?", N, Comp);
3715
3716 elsif Is_Record_Type (Etype (Comp)) then
3717 Check_Dynamic_Object (Etype (Comp));
3718 end if;
3719
3720 Next_Component (Comp);
3721 end loop;
3722 end if;
3723 end Check_Dynamic_Object;
3724
3725 -----------------------------------------
3726 -- Check_For_Null_Excluding_Components --
3727 -----------------------------------------
3728
3729 procedure Check_For_Null_Excluding_Components
3730 (Obj_Typ : Entity_Id;
3731 Obj_Decl : Node_Id)
3732 is
3733 procedure Check_Component
3734 (Comp_Typ : Entity_Id;
3735 Comp_Decl : Node_Id := Empty;
3736 Array_Comp : Boolean := False);
3737 -- Apply a compile-time null-exclusion check on a component denoted
3738 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3739 -- subcomponents (if any).
3740
3741 ---------------------
3742 -- Check_Component --
3743 ---------------------
3744
3745 procedure Check_Component
3746 (Comp_Typ : Entity_Id;
3747 Comp_Decl : Node_Id := Empty;
3748 Array_Comp : Boolean := False)
3749 is
3750 Comp : Entity_Id;
3751 T : Entity_Id;
3752
3753 begin
3754 -- Do not consider internally-generated components or those that
3755 -- are already initialized.
3756
3757 if Present (Comp_Decl)
3758 and then (not Comes_From_Source (Comp_Decl)
3759 or else Present (Expression (Comp_Decl)))
3760 then
3761 return;
3762 end if;
3763
3764 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3765 and then Present (Full_View (Comp_Typ))
3766 then
3767 T := Full_View (Comp_Typ);
3768 else
3769 T := Comp_Typ;
3770 end if;
3771
3772 -- Verify a component of a null-excluding access type
3773
3774 if Is_Access_Type (T)
3775 and then Can_Never_Be_Null (T)
3776 then
3777 if Comp_Decl = Obj_Decl then
3778 Null_Exclusion_Static_Checks
3779 (N => Obj_Decl,
3780 Comp => Empty,
3781 Array_Comp => Array_Comp);
3782
3783 else
3784 Null_Exclusion_Static_Checks
3785 (N => Obj_Decl,
3786 Comp => Comp_Decl,
3787 Array_Comp => Array_Comp);
3788 end if;
3789
3790 -- Check array components
3791
3792 elsif Is_Array_Type (T) then
3793
3794 -- There is no suitable component when the object is of an
3795 -- array type. However, a namable component may appear at some
3796 -- point during the recursive inspection, but not at the top
3797 -- level. At the top level just indicate array component case.
3798
3799 if Comp_Decl = Obj_Decl then
3800 Check_Component (Component_Type (T), Array_Comp => True);
3801 else
3802 Check_Component (Component_Type (T), Comp_Decl);
3803 end if;
3804
3805 -- Verify all components of type T
3806
3807 -- Note: No checks are performed on types with discriminants due
3808 -- to complexities involving variants. ???
3809
3810 elsif (Is_Concurrent_Type (T)
3811 or else Is_Incomplete_Or_Private_Type (T)
3812 or else Is_Record_Type (T))
3813 and then not Has_Discriminants (T)
3814 then
3815 Comp := First_Component (T);
3816 while Present (Comp) loop
3817 Check_Component (Etype (Comp), Parent (Comp));
3818
3819 Next_Component (Comp);
3820 end loop;
3821 end if;
3822 end Check_Component;
3823
3824 -- Start processing for Check_For_Null_Excluding_Components
3825
3826 begin
3827 Check_Component (Obj_Typ, Obj_Decl);
3828 end Check_For_Null_Excluding_Components;
3829
3830 -----------------
3831 -- Count_Tasks --
3832 -----------------
3833
3834 function Count_Tasks (T : Entity_Id) return Uint is
3835 C : Entity_Id;
3836 X : Node_Id;
3837 V : Uint;
3838
3839 begin
3840 if Is_Task_Type (T) then
3841 return Uint_1;
3842
3843 elsif Is_Record_Type (T) then
3844 if Has_Discriminants (T) then
3845 Check_Restriction (Max_Tasks, N);
3846 return Uint_0;
3847
3848 else
3849 V := Uint_0;
3850 C := First_Component (T);
3851 while Present (C) loop
3852 V := V + Count_Tasks (Etype (C));
3853 Next_Component (C);
3854 end loop;
3855
3856 return V;
3857 end if;
3858
3859 elsif Is_Array_Type (T) then
3860 X := First_Index (T);
3861 V := Count_Tasks (Component_Type (T));
3862 while Present (X) loop
3863 C := Etype (X);
3864
3865 if not Is_OK_Static_Subtype (C) then
3866 Check_Restriction (Max_Tasks, N);
3867 return Uint_0;
3868 else
3869 V := V * (UI_Max (Uint_0,
3870 Expr_Value (Type_High_Bound (C)) -
3871 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3872 end if;
3873
3874 Next_Index (X);
3875 end loop;
3876
3877 return V;
3878
3879 else
3880 return Uint_0;
3881 end if;
3882 end Count_Tasks;
3883
3884 ----------------------------
3885 -- Delayed_Aspect_Present --
3886 ----------------------------
3887
3888 function Delayed_Aspect_Present return Boolean is
3889 A : Node_Id;
3890 A_Id : Aspect_Id;
3891
3892 begin
3893 if Present (Aspect_Specifications (N)) then
3894 A := First (Aspect_Specifications (N));
3895 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3896 while Present (A) loop
3897 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3898
3899 -- Set flag on object entity, for later processing at
3900 -- the freeze point.
3901
3902 Set_Has_Delayed_Aspects (Id);
3903 return True;
3904 end if;
3905
3906 Next (A);
3907 end loop;
3908 end if;
3909
3910 return False;
3911 end Delayed_Aspect_Present;
3912
3913 -- Local variables
3914
3915 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3916 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3917 -- Save the Ghost-related attributes to restore on exit
3918
3919 Related_Id : Entity_Id;
3920 Full_View_Present : Boolean := False;
3921
3922 -- Start of processing for Analyze_Object_Declaration
3923
3924 begin
3925 -- There are three kinds of implicit types generated by an
3926 -- object declaration:
3927
3928 -- 1. Those generated by the original Object Definition
3929
3930 -- 2. Those generated by the Expression
3931
3932 -- 3. Those used to constrain the Object Definition with the
3933 -- expression constraints when the definition is unconstrained.
3934
3935 -- They must be generated in this order to avoid order of elaboration
3936 -- issues. Thus the first step (after entering the name) is to analyze
3937 -- the object definition.
3938
3939 if Constant_Present (N) then
3940 Prev_Entity := Current_Entity_In_Scope (Id);
3941
3942 if Present (Prev_Entity)
3943 and then
3944 -- If the homograph is an implicit subprogram, it is overridden
3945 -- by the current declaration.
3946
3947 ((Is_Overloadable (Prev_Entity)
3948 and then Is_Inherited_Operation (Prev_Entity))
3949
3950 -- The current object is a discriminal generated for an entry
3951 -- family index. Even though the index is a constant, in this
3952 -- particular context there is no true constant redeclaration.
3953 -- Enter_Name will handle the visibility.
3954
3955 or else
3956 (Is_Discriminal (Id)
3957 and then Ekind (Discriminal_Link (Id)) =
3958 E_Entry_Index_Parameter)
3959
3960 -- The current object is the renaming for a generic declared
3961 -- within the instance.
3962
3963 or else
3964 (Ekind (Prev_Entity) = E_Package
3965 and then Nkind (Parent (Prev_Entity)) =
3966 N_Package_Renaming_Declaration
3967 and then not Comes_From_Source (Prev_Entity)
3968 and then
3969 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3970
3971 -- The entity may be a homonym of a private component of the
3972 -- enclosing protected object, for which we create a local
3973 -- renaming declaration. The declaration is legal, even if
3974 -- useless when it just captures that component.
3975
3976 or else
3977 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3978 and then Nkind (Parent (Prev_Entity)) =
3979 N_Object_Renaming_Declaration))
3980 then
3981 Prev_Entity := Empty;
3982 end if;
3983 end if;
3984
3985 if Present (Prev_Entity) then
3986
3987 -- The object declaration is Ghost when it completes a deferred Ghost
3988 -- constant.
3989
3990 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3991
3992 Constant_Redeclaration (Id, N, T);
3993
3994 Generate_Reference (Prev_Entity, Id, 'c');
3995 Set_Completion_Referenced (Id);
3996
3997 if Error_Posted (N) then
3998
3999 -- Type mismatch or illegal redeclaration; do not analyze
4000 -- expression to avoid cascaded errors.
4001
4002 T := Find_Type_Of_Object (Object_Definition (N), N);
4003 Set_Etype (Id, T);
4004 Set_Ekind (Id, E_Variable);
4005 goto Leave;
4006 end if;
4007
4008 -- In the normal case, enter identifier at the start to catch premature
4009 -- usage in the initialization expression.
4010
4011 else
4012 Generate_Definition (Id);
4013 Enter_Name (Id);
4014
4015 Mark_Coextensions (N, Object_Definition (N));
4016
4017 T := Find_Type_Of_Object (Object_Definition (N), N);
4018
4019 if Nkind (Object_Definition (N)) = N_Access_Definition
4020 and then Present
4021 (Access_To_Subprogram_Definition (Object_Definition (N)))
4022 and then Protected_Present
4023 (Access_To_Subprogram_Definition (Object_Definition (N)))
4024 then
4025 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4026 end if;
4027
4028 if Error_Posted (Id) then
4029 Set_Etype (Id, T);
4030 Set_Ekind (Id, E_Variable);
4031 goto Leave;
4032 end if;
4033 end if;
4034
4035 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4036 -- out some static checks.
4037
4038 if Ada_Version >= Ada_2005 then
4039
4040 -- In case of aggregates we must also take care of the correct
4041 -- initialization of nested aggregates bug this is done at the
4042 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4043
4044 if Can_Never_Be_Null (T) then
4045 if Present (Expression (N))
4046 and then Nkind (Expression (N)) = N_Aggregate
4047 then
4048 null;
4049
4050 else
4051 declare
4052 Save_Typ : constant Entity_Id := Etype (Id);
4053 begin
4054 Set_Etype (Id, T); -- Temp. decoration for static checks
4055 Null_Exclusion_Static_Checks (N);
4056 Set_Etype (Id, Save_Typ);
4057 end;
4058 end if;
4059
4060 -- We might be dealing with an object of a composite type containing
4061 -- null-excluding components without an aggregate, so we must verify
4062 -- that such components have default initialization.
4063
4064 else
4065 Check_For_Null_Excluding_Components (T, N);
4066 end if;
4067 end if;
4068
4069 -- Object is marked pure if it is in a pure scope
4070
4071 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4072
4073 -- If deferred constant, make sure context is appropriate. We detect
4074 -- a deferred constant as a constant declaration with no expression.
4075 -- A deferred constant can appear in a package body if its completion
4076 -- is by means of an interface pragma.
4077
4078 if Constant_Present (N) and then No (E) then
4079
4080 -- A deferred constant may appear in the declarative part of the
4081 -- following constructs:
4082
4083 -- blocks
4084 -- entry bodies
4085 -- extended return statements
4086 -- package specs
4087 -- package bodies
4088 -- subprogram bodies
4089 -- task bodies
4090
4091 -- When declared inside a package spec, a deferred constant must be
4092 -- completed by a full constant declaration or pragma Import. In all
4093 -- other cases, the only proper completion is pragma Import. Extended
4094 -- return statements are flagged as invalid contexts because they do
4095 -- not have a declarative part and so cannot accommodate the pragma.
4096
4097 if Ekind (Current_Scope) = E_Return_Statement then
4098 Error_Msg_N
4099 ("invalid context for deferred constant declaration (RM 7.4)",
4100 N);
4101 Error_Msg_N
4102 ("\declaration requires an initialization expression",
4103 N);
4104 Set_Constant_Present (N, False);
4105
4106 -- In Ada 83, deferred constant must be of private type
4107
4108 elsif not Is_Private_Type (T) then
4109 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4110 Error_Msg_N
4111 ("(Ada 83) deferred constant must be private type", N);
4112 end if;
4113 end if;
4114
4115 -- If not a deferred constant, then the object declaration freezes
4116 -- its type, unless the object is of an anonymous type and has delayed
4117 -- aspects. In that case the type is frozen when the object itself is.
4118
4119 else
4120 Check_Fully_Declared (T, N);
4121
4122 if Has_Delayed_Aspects (Id)
4123 and then Is_Array_Type (T)
4124 and then Is_Itype (T)
4125 then
4126 Set_Has_Delayed_Freeze (T);
4127 else
4128 Freeze_Before (N, T);
4129 end if;
4130 end if;
4131
4132 -- If the object was created by a constrained array definition, then
4133 -- set the link in both the anonymous base type and anonymous subtype
4134 -- that are built to represent the array type to point to the object.
4135
4136 if Nkind (Object_Definition (Declaration_Node (Id))) =
4137 N_Constrained_Array_Definition
4138 then
4139 Set_Related_Array_Object (T, Id);
4140 Set_Related_Array_Object (Base_Type (T), Id);
4141 end if;
4142
4143 -- Special checks for protected objects not at library level
4144
4145 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4146 Check_Restriction (No_Local_Protected_Objects, Id);
4147
4148 -- Protected objects with interrupt handlers must be at library level
4149
4150 -- Ada 2005: This test is not needed (and the corresponding clause
4151 -- in the RM is removed) because accessibility checks are sufficient
4152 -- to make handlers not at the library level illegal.
4153
4154 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4155 -- applies to the '95 version of the language as well.
4156
4157 if Is_Protected_Type (T)
4158 and then Has_Interrupt_Handler (T)
4159 and then Ada_Version < Ada_95
4160 then
4161 Error_Msg_N
4162 ("interrupt object can only be declared at library level", Id);
4163 end if;
4164 end if;
4165
4166 -- Check for violation of No_Local_Timing_Events
4167
4168 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4169 Check_Restriction (No_Local_Timing_Events, Id);
4170 end if;
4171
4172 -- The actual subtype of the object is the nominal subtype, unless
4173 -- the nominal one is unconstrained and obtained from the expression.
4174
4175 Act_T := T;
4176
4177 if Is_Library_Level_Entity (Id) then
4178 Check_Dynamic_Object (T);
4179 end if;
4180
4181 -- Process initialization expression if present and not in error
4182
4183 if Present (E) and then E /= Error then
4184
4185 -- Generate an error in case of CPP class-wide object initialization.
4186 -- Required because otherwise the expansion of the class-wide
4187 -- assignment would try to use 'size to initialize the object
4188 -- (primitive that is not available in CPP tagged types).
4189
4190 if Is_Class_Wide_Type (Act_T)
4191 and then
4192 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4193 or else
4194 (Present (Full_View (Root_Type (Etype (Act_T))))
4195 and then
4196 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4197 then
4198 Error_Msg_N
4199 ("predefined assignment not available for 'C'P'P tagged types",
4200 E);
4201 end if;
4202
4203 Mark_Coextensions (N, E);
4204 Analyze (E);
4205
4206 -- In case of errors detected in the analysis of the expression,
4207 -- decorate it with the expected type to avoid cascaded errors
4208
4209 if No (Etype (E)) then
4210 Set_Etype (E, T);
4211 end if;
4212
4213 -- If an initialization expression is present, then we set the
4214 -- Is_True_Constant flag. It will be reset if this is a variable
4215 -- and it is indeed modified.
4216
4217 Set_Is_True_Constant (Id, True);
4218
4219 -- If we are analyzing a constant declaration, set its completion
4220 -- flag after analyzing and resolving the expression.
4221
4222 if Constant_Present (N) then
4223 Set_Has_Completion (Id);
4224 end if;
4225
4226 -- Set type and resolve (type may be overridden later on). Note:
4227 -- Ekind (Id) must still be E_Void at this point so that incorrect
4228 -- early usage within E is properly diagnosed.
4229
4230 Set_Etype (Id, T);
4231
4232 -- If the expression is an aggregate we must look ahead to detect
4233 -- the possible presence of an address clause, and defer resolution
4234 -- and expansion of the aggregate to the freeze point of the entity.
4235
4236 -- This is not always legal because the aggregate may contain other
4237 -- references that need freezing, e.g. references to other entities
4238 -- with address clauses. In any case, when compiling with -gnatI the
4239 -- presence of the address clause must be ignored.
4240
4241 if Comes_From_Source (N)
4242 and then Expander_Active
4243 and then Nkind (E) = N_Aggregate
4244 and then
4245 ((Present (Following_Address_Clause (N))
4246 and then not Ignore_Rep_Clauses)
4247 or else Delayed_Aspect_Present)
4248 then
4249 Set_Etype (E, T);
4250
4251 -- If the aggregate is limited it will be built in place, and its
4252 -- expansion is deferred until the object declaration is expanded.
4253
4254 -- This is also required when generating C code to ensure that an
4255 -- object with an alignment or address clause can be initialized
4256 -- by means of component by component assignments.
4257
4258 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4259 Set_Expansion_Delayed (E);
4260 end if;
4261
4262 else
4263 -- If the expression is a formal that is a "subprogram pointer"
4264 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4265 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4266 -- the corresponding check, as is done for assignments.
4267
4268 if Is_Entity_Name (E)
4269 and then Present (Entity (E))
4270 and then Is_Formal (Entity (E))
4271 and then
4272 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4273 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4274 then
4275 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4276 end if;
4277
4278 Resolve (E, T);
4279 end if;
4280
4281 -- No further action needed if E is a call to an inlined function
4282 -- which returns an unconstrained type and it has been expanded into
4283 -- a procedure call. In that case N has been replaced by an object
4284 -- declaration without initializing expression and it has been
4285 -- analyzed (see Expand_Inlined_Call).
4286
4287 if Back_End_Inlining
4288 and then Expander_Active
4289 and then Nkind (E) = N_Function_Call
4290 and then Nkind (Name (E)) in N_Has_Entity
4291 and then Is_Inlined (Entity (Name (E)))
4292 and then not Is_Constrained (Etype (E))
4293 and then Analyzed (N)
4294 and then No (Expression (N))
4295 then
4296 goto Leave;
4297 end if;
4298
4299 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4300 -- node (which was marked already-analyzed), we need to set the type
4301 -- to something other than Any_Access in order to keep gigi happy.
4302
4303 if Etype (E) = Any_Access then
4304 Set_Etype (E, T);
4305 end if;
4306
4307 -- If the object is an access to variable, the initialization
4308 -- expression cannot be an access to constant.
4309
4310 if Is_Access_Type (T)
4311 and then not Is_Access_Constant (T)
4312 and then Is_Access_Type (Etype (E))
4313 and then Is_Access_Constant (Etype (E))
4314 then
4315 Error_Msg_N
4316 ("access to variable cannot be initialized with an "
4317 & "access-to-constant expression", E);
4318 end if;
4319
4320 if not Assignment_OK (N) then
4321 Check_Initialization (T, E);
4322 end if;
4323
4324 Check_Unset_Reference (E);
4325
4326 -- If this is a variable, then set current value. If this is a
4327 -- declared constant of a scalar type with a static expression,
4328 -- indicate that it is always valid.
4329
4330 if not Constant_Present (N) then
4331 if Compile_Time_Known_Value (E) then
4332 Set_Current_Value (Id, E);
4333 end if;
4334
4335 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4336 Set_Is_Known_Valid (Id);
4337
4338 -- If it is a constant initialized with a valid nonstatic entity,
4339 -- the constant is known valid as well, and can inherit the subtype
4340 -- of the entity if it is a subtype of the given type. This info
4341 -- is preserved on the actual subtype of the constant.
4342
4343 elsif Is_Scalar_Type (T)
4344 and then Is_Entity_Name (E)
4345 and then Is_Known_Valid (Entity (E))
4346 and then In_Subrange_Of (Etype (Entity (E)), T)
4347 then
4348 Set_Is_Known_Valid (Id);
4349 Set_Ekind (Id, E_Constant);
4350 Set_Actual_Subtype (Id, Etype (Entity (E)));
4351 end if;
4352
4353 -- Deal with setting of null flags
4354
4355 if Is_Access_Type (T) then
4356 if Known_Non_Null (E) then
4357 Set_Is_Known_Non_Null (Id, True);
4358 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4359 Set_Is_Known_Null (Id, True);
4360 end if;
4361 end if;
4362
4363 -- Check incorrect use of dynamically tagged expressions
4364
4365 if Is_Tagged_Type (T) then
4366 Check_Dynamically_Tagged_Expression
4367 (Expr => E,
4368 Typ => T,
4369 Related_Nod => N);
4370 end if;
4371
4372 Apply_Scalar_Range_Check (E, T);
4373 Apply_Static_Length_Check (E, T);
4374
4375 -- A formal parameter of a specific tagged type whose related
4376 -- subprogram is subject to pragma Extensions_Visible with value
4377 -- "False" cannot be implicitly converted to a class-wide type by
4378 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4379 -- not consider internally generated expressions.
4380
4381 if Is_Class_Wide_Type (T)
4382 and then Comes_From_Source (E)
4383 and then Is_EVF_Expression (E)
4384 then
4385 Error_Msg_N
4386 ("formal parameter cannot be implicitly converted to "
4387 & "class-wide type when Extensions_Visible is False", E);
4388 end if;
4389 end if;
4390
4391 -- If the No_Streams restriction is set, check that the type of the
4392 -- object is not, and does not contain, any subtype derived from
4393 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4394 -- Has_Stream just for efficiency reasons. There is no point in
4395 -- spending time on a Has_Stream check if the restriction is not set.
4396
4397 if Restriction_Check_Required (No_Streams) then
4398 if Has_Stream (T) then
4399 Check_Restriction (No_Streams, N);
4400 end if;
4401 end if;
4402
4403 -- Deal with predicate check before we start to do major rewriting. It
4404 -- is OK to initialize and then check the initialized value, since the
4405 -- object goes out of scope if we get a predicate failure. Note that we
4406 -- do this in the analyzer and not the expander because the analyzer
4407 -- does some substantial rewriting in some cases.
4408
4409 -- We need a predicate check if the type has predicates that are not
4410 -- ignored, and if either there is an initializing expression, or for
4411 -- default initialization when we have at least one case of an explicit
4412 -- default initial value (including via a Default_Value or
4413 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4414 -- an internal declaration whose initialization comes later (as for an
4415 -- aggregate expansion).
4416 -- If expression is an aggregate it may be expanded into assignments
4417 -- and the declaration itself is marked with No_Initialization, but
4418 -- the predicate still applies.
4419
4420 if not Suppress_Assignment_Checks (N)
4421 and then Present (Predicate_Function (T))
4422 and then not Predicates_Ignored (T)
4423 and then
4424 (not No_Initialization (N)
4425 or else (Present (E) and then Nkind (E) = N_Aggregate))
4426 and then
4427 (Present (E)
4428 or else
4429 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4430 then
4431 -- If the type has a static predicate and the expression is known at
4432 -- compile time, see if the expression satisfies the predicate.
4433
4434 if Present (E) then
4435 Check_Expression_Against_Static_Predicate (E, T);
4436 end if;
4437
4438 -- If the type is a null record and there is no explicit initial
4439 -- expression, no predicate check applies.
4440
4441 if No (E) and then Is_Null_Record_Type (T) then
4442 null;
4443
4444 -- Do not generate a predicate check if the initialization expression
4445 -- is a type conversion because the conversion has been subjected to
4446 -- the same check. This is a small optimization which avoid redundant
4447 -- checks.
4448
4449 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4450 null;
4451
4452 else
4453 -- The check must be inserted after the expanded aggregate
4454 -- expansion code, if any.
4455
4456 declare
4457 Check : constant Node_Id :=
4458 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4459
4460 begin
4461 if No (Next_Decl) then
4462 Append_To (List_Containing (N), Check);
4463 else
4464 Insert_Before (Next_Decl, Check);
4465 end if;
4466 end;
4467 end if;
4468 end if;
4469
4470 -- Case of unconstrained type
4471
4472 if not Is_Definite_Subtype (T) then
4473
4474 -- Nothing to do in deferred constant case
4475
4476 if Constant_Present (N) and then No (E) then
4477 null;
4478
4479 -- Case of no initialization present
4480
4481 elsif No (E) then
4482 if No_Initialization (N) then
4483 null;
4484
4485 elsif Is_Class_Wide_Type (T) then
4486 Error_Msg_N
4487 ("initialization required in class-wide declaration ", N);
4488
4489 else
4490 Error_Msg_N
4491 ("unconstrained subtype not allowed (need initialization)",
4492 Object_Definition (N));
4493
4494 if Is_Record_Type (T) and then Has_Discriminants (T) then
4495 Error_Msg_N
4496 ("\provide initial value or explicit discriminant values",
4497 Object_Definition (N));
4498
4499 Error_Msg_NE
4500 ("\or give default discriminant values for type&",
4501 Object_Definition (N), T);
4502
4503 elsif Is_Array_Type (T) then
4504 Error_Msg_N
4505 ("\provide initial value or explicit array bounds",
4506 Object_Definition (N));
4507 end if;
4508 end if;
4509
4510 -- Case of initialization present but in error. Set initial
4511 -- expression as absent (but do not make above complaints)
4512
4513 elsif E = Error then
4514 Set_Expression (N, Empty);
4515 E := Empty;
4516
4517 -- Case of initialization present
4518
4519 else
4520 -- Check restrictions in Ada 83
4521
4522 if not Constant_Present (N) then
4523
4524 -- Unconstrained variables not allowed in Ada 83 mode
4525
4526 if Ada_Version = Ada_83
4527 and then Comes_From_Source (Object_Definition (N))
4528 then
4529 Error_Msg_N
4530 ("(Ada 83) unconstrained variable not allowed",
4531 Object_Definition (N));
4532 end if;
4533 end if;
4534
4535 -- Now we constrain the variable from the initializing expression
4536
4537 -- If the expression is an aggregate, it has been expanded into
4538 -- individual assignments. Retrieve the actual type from the
4539 -- expanded construct.
4540
4541 if Is_Array_Type (T)
4542 and then No_Initialization (N)
4543 and then Nkind (Original_Node (E)) = N_Aggregate
4544 then
4545 Act_T := Etype (E);
4546
4547 -- In case of class-wide interface object declarations we delay
4548 -- the generation of the equivalent record type declarations until
4549 -- its expansion because there are cases in they are not required.
4550
4551 elsif Is_Interface (T) then
4552 null;
4553
4554 -- If the type is an unchecked union, no subtype can be built from
4555 -- the expression. Rewrite declaration as a renaming, which the
4556 -- back-end can handle properly. This is a rather unusual case,
4557 -- because most unchecked_union declarations have default values
4558 -- for discriminants and are thus not indefinite.
4559
4560 elsif Is_Unchecked_Union (T) then
4561 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4562 Set_Ekind (Id, E_Constant);
4563 else
4564 Set_Ekind (Id, E_Variable);
4565 end if;
4566
4567 Rewrite (N,
4568 Make_Object_Renaming_Declaration (Loc,
4569 Defining_Identifier => Id,
4570 Subtype_Mark => New_Occurrence_Of (T, Loc),
4571 Name => E));
4572
4573 Set_Renamed_Object (Id, E);
4574 Freeze_Before (N, T);
4575 Set_Is_Frozen (Id);
4576 goto Leave;
4577
4578 else
4579 -- Ensure that the generated subtype has a unique external name
4580 -- when the related object is public. This guarantees that the
4581 -- subtype and its bounds will not be affected by switches or
4582 -- pragmas that may offset the internal counter due to extra
4583 -- generated code.
4584
4585 if Is_Public (Id) then
4586 Related_Id := Id;
4587 else
4588 Related_Id := Empty;
4589 end if;
4590
4591 Expand_Subtype_From_Expr
4592 (N => N,
4593 Unc_Type => T,
4594 Subtype_Indic => Object_Definition (N),
4595 Exp => E,
4596 Related_Id => Related_Id);
4597
4598 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4599 end if;
4600
4601 -- Propagate attributes to full view when needed.
4602
4603 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4604
4605 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4606 then
4607 Full_View_Present := True;
4608 end if;
4609
4610 if Full_View_Present then
4611 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4612 end if;
4613
4614 if Aliased_Present (N) then
4615 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4616
4617 if Full_View_Present then
4618 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4619 end if;
4620 end if;
4621
4622 Freeze_Before (N, Act_T);
4623 Freeze_Before (N, T);
4624 end if;
4625
4626 elsif Is_Array_Type (T)
4627 and then No_Initialization (N)
4628 and then (Nkind (Original_Node (E)) = N_Aggregate
4629 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4630 and then Nkind (Original_Node (Expression
4631 (Original_Node (E)))) = N_Aggregate))
4632 then
4633 if not Is_Entity_Name (Object_Definition (N)) then
4634 Act_T := Etype (E);
4635 Check_Compile_Time_Size (Act_T);
4636
4637 if Aliased_Present (N) then
4638 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4639 end if;
4640 end if;
4641
4642 -- When the given object definition and the aggregate are specified
4643 -- independently, and their lengths might differ do a length check.
4644 -- This cannot happen if the aggregate is of the form (others =>...)
4645
4646 if not Is_Constrained (T) then
4647 null;
4648
4649 elsif Nkind (E) = N_Raise_Constraint_Error then
4650
4651 -- Aggregate is statically illegal. Place back in declaration
4652
4653 Set_Expression (N, E);
4654 Set_No_Initialization (N, False);
4655
4656 elsif T = Etype (E) then
4657 null;
4658
4659 elsif Nkind (E) = N_Aggregate
4660 and then Present (Component_Associations (E))
4661 and then Present (Choice_List (First (Component_Associations (E))))
4662 and then
4663 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4664 N_Others_Choice
4665 then
4666 null;
4667
4668 else
4669 Apply_Length_Check (E, T);
4670 end if;
4671
4672 -- If the type is limited unconstrained with defaulted discriminants and
4673 -- there is no expression, then the object is constrained by the
4674 -- defaults, so it is worthwhile building the corresponding subtype.
4675
4676 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4677 and then not Is_Constrained (T)
4678 and then Has_Discriminants (T)
4679 then
4680 if No (E) then
4681 Act_T := Build_Default_Subtype (T, N);
4682 else
4683 -- Ada 2005: A limited object may be initialized by means of an
4684 -- aggregate. If the type has default discriminants it has an
4685 -- unconstrained nominal type, Its actual subtype will be obtained
4686 -- from the aggregate, and not from the default discriminants.
4687
4688 Act_T := Etype (E);
4689 end if;
4690
4691 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4692
4693 elsif Nkind (E) = N_Function_Call
4694 and then Constant_Present (N)
4695 and then Has_Unconstrained_Elements (Etype (E))
4696 then
4697 -- The back-end has problems with constants of a discriminated type
4698 -- with defaults, if the initial value is a function call. We
4699 -- generate an intermediate temporary that will receive a reference
4700 -- to the result of the call. The initialization expression then
4701 -- becomes a dereference of that temporary.
4702
4703 Remove_Side_Effects (E);
4704
4705 -- If this is a constant declaration of an unconstrained type and
4706 -- the initialization is an aggregate, we can use the subtype of the
4707 -- aggregate for the declared entity because it is immutable.
4708
4709 elsif not Is_Constrained (T)
4710 and then Has_Discriminants (T)
4711 and then Constant_Present (N)
4712 and then not Has_Unchecked_Union (T)
4713 and then Nkind (E) = N_Aggregate
4714 then
4715 Act_T := Etype (E);
4716 end if;
4717
4718 -- Check No_Wide_Characters restriction
4719
4720 Check_Wide_Character_Restriction (T, Object_Definition (N));
4721
4722 -- Indicate this is not set in source. Certainly true for constants, and
4723 -- true for variables so far (will be reset for a variable if and when
4724 -- we encounter a modification in the source).
4725
4726 Set_Never_Set_In_Source (Id);
4727
4728 -- Now establish the proper kind and type of the object
4729
4730 if Constant_Present (N) then
4731 Set_Ekind (Id, E_Constant);
4732 Set_Is_True_Constant (Id);
4733
4734 else
4735 Set_Ekind (Id, E_Variable);
4736
4737 -- A variable is set as shared passive if it appears in a shared
4738 -- passive package, and is at the outer level. This is not done for
4739 -- entities generated during expansion, because those are always
4740 -- manipulated locally.
4741
4742 if Is_Shared_Passive (Current_Scope)
4743 and then Is_Library_Level_Entity (Id)
4744 and then Comes_From_Source (Id)
4745 then
4746 Set_Is_Shared_Passive (Id);
4747 Check_Shared_Var (Id, T, N);
4748 end if;
4749
4750 -- Set Has_Initial_Value if initializing expression present. Note
4751 -- that if there is no initializing expression, we leave the state
4752 -- of this flag unchanged (usually it will be False, but notably in
4753 -- the case of exception choice variables, it will already be true).
4754
4755 if Present (E) then
4756 Set_Has_Initial_Value (Id);
4757 end if;
4758 end if;
4759
4760 -- Set the SPARK mode from the current context (may be overwritten later
4761 -- with explicit pragma).
4762
4763 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4764 Set_SPARK_Pragma_Inherited (Id);
4765
4766 -- Preserve relevant elaboration-related attributes of the context which
4767 -- are no longer available or very expensive to recompute once analysis,
4768 -- resolution, and expansion are over.
4769
4770 Mark_Elaboration_Attributes
4771 (N_Id => Id,
4772 Checks => True,
4773 Warnings => True);
4774
4775 -- Initialize alignment and size and capture alignment setting
4776
4777 Init_Alignment (Id);
4778 Init_Esize (Id);
4779 Set_Optimize_Alignment_Flags (Id);
4780
4781 -- Deal with aliased case
4782
4783 if Aliased_Present (N) then
4784 Set_Is_Aliased (Id);
4785
4786 -- AI12-001: All aliased objects are considered to be specified as
4787 -- independently addressable (RM C.6(8.1/4)).
4788
4789 Set_Is_Independent (Id);
4790
4791 -- If the object is aliased and the type is unconstrained with
4792 -- defaulted discriminants and there is no expression, then the
4793 -- object is constrained by the defaults, so it is worthwhile
4794 -- building the corresponding subtype.
4795
4796 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4797 -- unconstrained, then only establish an actual subtype if the
4798 -- nominal subtype is indefinite. In definite cases the object is
4799 -- unconstrained in Ada 2005.
4800
4801 if No (E)
4802 and then Is_Record_Type (T)
4803 and then not Is_Constrained (T)
4804 and then Has_Discriminants (T)
4805 and then (Ada_Version < Ada_2005
4806 or else not Is_Definite_Subtype (T))
4807 then
4808 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4809 end if;
4810 end if;
4811
4812 -- Now we can set the type of the object
4813
4814 Set_Etype (Id, Act_T);
4815
4816 -- Non-constant object is marked to be treated as volatile if type is
4817 -- volatile and we clear the Current_Value setting that may have been
4818 -- set above. Doing so for constants isn't required and might interfere
4819 -- with possible uses of the object as a static expression in contexts
4820 -- incompatible with volatility (e.g. as a case-statement alternative).
4821
4822 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4823 Set_Treat_As_Volatile (Id);
4824 Set_Current_Value (Id, Empty);
4825 end if;
4826
4827 -- Deal with controlled types
4828
4829 if Has_Controlled_Component (Etype (Id))
4830 or else Is_Controlled (Etype (Id))
4831 then
4832 if not Is_Library_Level_Entity (Id) then
4833 Check_Restriction (No_Nested_Finalization, N);
4834 else
4835 Validate_Controlled_Object (Id);
4836 end if;
4837 end if;
4838
4839 if Has_Task (Etype (Id)) then
4840 Check_Restriction (No_Tasking, N);
4841
4842 -- Deal with counting max tasks
4843
4844 -- Nothing to do if inside a generic
4845
4846 if Inside_A_Generic then
4847 null;
4848
4849 -- If library level entity, then count tasks
4850
4851 elsif Is_Library_Level_Entity (Id) then
4852 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4853
4854 -- If not library level entity, then indicate we don't know max
4855 -- tasks and also check task hierarchy restriction and blocking
4856 -- operation (since starting a task is definitely blocking).
4857
4858 else
4859 Check_Restriction (Max_Tasks, N);
4860 Check_Restriction (No_Task_Hierarchy, N);
4861 Check_Potentially_Blocking_Operation (N);
4862 end if;
4863
4864 -- A rather specialized test. If we see two tasks being declared
4865 -- of the same type in the same object declaration, and the task
4866 -- has an entry with an address clause, we know that program error
4867 -- will be raised at run time since we can't have two tasks with
4868 -- entries at the same address.
4869
4870 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4871 declare
4872 E : Entity_Id;
4873
4874 begin
4875 E := First_Entity (Etype (Id));
4876 while Present (E) loop
4877 if Ekind (E) = E_Entry
4878 and then Present (Get_Attribute_Definition_Clause
4879 (E, Attribute_Address))
4880 then
4881 Error_Msg_Warn := SPARK_Mode /= On;
4882 Error_Msg_N
4883 ("more than one task with same entry address<<", N);
4884 Error_Msg_N ("\Program_Error [<<", N);
4885 Insert_Action (N,
4886 Make_Raise_Program_Error (Loc,
4887 Reason => PE_Duplicated_Entry_Address));
4888 exit;
4889 end if;
4890
4891 Next_Entity (E);
4892 end loop;
4893 end;
4894 end if;
4895 end if;
4896
4897 -- Some simple constant-propagation: if the expression is a constant
4898 -- string initialized with a literal, share the literal. This avoids
4899 -- a run-time copy.
4900
4901 if Present (E)
4902 and then Is_Entity_Name (E)
4903 and then Ekind (Entity (E)) = E_Constant
4904 and then Base_Type (Etype (E)) = Standard_String
4905 then
4906 declare
4907 Val : constant Node_Id := Constant_Value (Entity (E));
4908 begin
4909 if Present (Val) and then Nkind (Val) = N_String_Literal then
4910 Rewrite (E, New_Copy (Val));
4911 end if;
4912 end;
4913 end if;
4914
4915 -- Another optimization: if the nominal subtype is unconstrained and
4916 -- the expression is a function call that returns an unconstrained
4917 -- type, rewrite the declaration as a renaming of the result of the
4918 -- call. The exceptions below are cases where the copy is expected,
4919 -- either by the back end (Aliased case) or by the semantics, as for
4920 -- initializing controlled types or copying tags for class-wide types.
4921
4922 if Present (E)
4923 and then Nkind (E) = N_Explicit_Dereference
4924 and then Nkind (Original_Node (E)) = N_Function_Call
4925 and then not Is_Library_Level_Entity (Id)
4926 and then not Is_Constrained (Underlying_Type (T))
4927 and then not Is_Aliased (Id)
4928 and then not Is_Class_Wide_Type (T)
4929 and then not Is_Controlled (T)
4930 and then not Has_Controlled_Component (Base_Type (T))
4931 and then Expander_Active
4932 then
4933 Rewrite (N,
4934 Make_Object_Renaming_Declaration (Loc,
4935 Defining_Identifier => Id,
4936 Access_Definition => Empty,
4937 Subtype_Mark => New_Occurrence_Of
4938 (Base_Type (Etype (Id)), Loc),
4939 Name => E));
4940
4941 Set_Renamed_Object (Id, E);
4942
4943 -- Force generation of debugging information for the constant and for
4944 -- the renamed function call.
4945
4946 Set_Debug_Info_Needed (Id);
4947 Set_Debug_Info_Needed (Entity (Prefix (E)));
4948 end if;
4949
4950 if Present (Prev_Entity)
4951 and then Is_Frozen (Prev_Entity)
4952 and then not Error_Posted (Id)
4953 then
4954 Error_Msg_N ("full constant declaration appears too late", N);
4955 end if;
4956
4957 Check_Eliminated (Id);
4958
4959 -- Deal with setting In_Private_Part flag if in private part
4960
4961 if Ekind (Scope (Id)) = E_Package
4962 and then In_Private_Part (Scope (Id))
4963 then
4964 Set_In_Private_Part (Id);
4965 end if;
4966
4967 <<Leave>>
4968 -- Initialize the refined state of a variable here because this is a
4969 -- common destination for legal and illegal object declarations.
4970
4971 if Ekind (Id) = E_Variable then
4972 Set_Encapsulating_State (Id, Empty);
4973 end if;
4974
4975 if Has_Aspects (N) then
4976 Analyze_Aspect_Specifications (N, Id);
4977 end if;
4978
4979 Analyze_Dimension (N);
4980
4981 -- Verify whether the object declaration introduces an illegal hidden
4982 -- state within a package subject to a null abstract state.
4983
4984 if Ekind (Id) = E_Variable then
4985 Check_No_Hidden_State (Id);
4986 end if;
4987
4988 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4989 end Analyze_Object_Declaration;
4990
4991 ---------------------------
4992 -- Analyze_Others_Choice --
4993 ---------------------------
4994
4995 -- Nothing to do for the others choice node itself, the semantic analysis
4996 -- of the others choice will occur as part of the processing of the parent
4997
4998 procedure Analyze_Others_Choice (N : Node_Id) is
4999 pragma Warnings (Off, N);
5000 begin
5001 null;
5002 end Analyze_Others_Choice;
5003
5004 -------------------------------------------
5005 -- Analyze_Private_Extension_Declaration --
5006 -------------------------------------------
5007
5008 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5009 Indic : constant Node_Id := Subtype_Indication (N);
5010 T : constant Entity_Id := Defining_Identifier (N);
5011 Iface : Entity_Id;
5012 Iface_Elmt : Elmt_Id;
5013 Parent_Base : Entity_Id;
5014 Parent_Type : Entity_Id;
5015
5016 begin
5017 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5018
5019 if Is_Non_Empty_List (Interface_List (N)) then
5020 declare
5021 Intf : Node_Id;
5022 T : Entity_Id;
5023
5024 begin
5025 Intf := First (Interface_List (N));
5026 while Present (Intf) loop
5027 T := Find_Type_Of_Subtype_Indic (Intf);
5028
5029 Diagnose_Interface (Intf, T);
5030 Next (Intf);
5031 end loop;
5032 end;
5033 end if;
5034
5035 Generate_Definition (T);
5036
5037 -- For other than Ada 2012, just enter the name in the current scope
5038
5039 if Ada_Version < Ada_2012 then
5040 Enter_Name (T);
5041
5042 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5043 -- case of private type that completes an incomplete type.
5044
5045 else
5046 declare
5047 Prev : Entity_Id;
5048
5049 begin
5050 Prev := Find_Type_Name (N);
5051
5052 pragma Assert (Prev = T
5053 or else (Ekind (Prev) = E_Incomplete_Type
5054 and then Present (Full_View (Prev))
5055 and then Full_View (Prev) = T));
5056 end;
5057 end if;
5058
5059 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5060 Parent_Base := Base_Type (Parent_Type);
5061
5062 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5063 Set_Ekind (T, Ekind (Parent_Type));
5064 Set_Etype (T, Any_Type);
5065 goto Leave;
5066
5067 elsif not Is_Tagged_Type (Parent_Type) then
5068 Error_Msg_N
5069 ("parent of type extension must be a tagged type ", Indic);
5070 goto Leave;
5071
5072 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5073 Error_Msg_N ("premature derivation of incomplete type", Indic);
5074 goto Leave;
5075
5076 elsif Is_Concurrent_Type (Parent_Type) then
5077 Error_Msg_N
5078 ("parent type of a private extension cannot be a synchronized "
5079 & "tagged type (RM 3.9.1 (3/1))", N);
5080
5081 Set_Etype (T, Any_Type);
5082 Set_Ekind (T, E_Limited_Private_Type);
5083 Set_Private_Dependents (T, New_Elmt_List);
5084 Set_Error_Posted (T);
5085 goto Leave;
5086 end if;
5087
5088 -- Perhaps the parent type should be changed to the class-wide type's
5089 -- specific type in this case to prevent cascading errors ???
5090
5091 if Is_Class_Wide_Type (Parent_Type) then
5092 Error_Msg_N
5093 ("parent of type extension must not be a class-wide type", Indic);
5094 goto Leave;
5095 end if;
5096
5097 if (not Is_Package_Or_Generic_Package (Current_Scope)
5098 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5099 or else In_Private_Part (Current_Scope)
5100 then
5101 Error_Msg_N ("invalid context for private extension", N);
5102 end if;
5103
5104 -- Set common attributes
5105
5106 Set_Is_Pure (T, Is_Pure (Current_Scope));
5107 Set_Scope (T, Current_Scope);
5108 Set_Ekind (T, E_Record_Type_With_Private);
5109 Init_Size_Align (T);
5110 Set_Default_SSO (T);
5111 Set_No_Reordering (T, No_Component_Reordering);
5112
5113 Set_Etype (T, Parent_Base);
5114 Propagate_Concurrent_Flags (T, Parent_Base);
5115
5116 Set_Convention (T, Convention (Parent_Type));
5117 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5118 Set_Is_First_Subtype (T);
5119 Make_Class_Wide_Type (T);
5120
5121 -- Set the SPARK mode from the current context
5122
5123 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5124 Set_SPARK_Pragma_Inherited (T);
5125
5126 if Unknown_Discriminants_Present (N) then
5127 Set_Discriminant_Constraint (T, No_Elist);
5128 end if;
5129
5130 Build_Derived_Record_Type (N, Parent_Type, T);
5131
5132 -- A private extension inherits the Default_Initial_Condition pragma
5133 -- coming from any parent type within the derivation chain.
5134
5135 if Has_DIC (Parent_Type) then
5136 Set_Has_Inherited_DIC (T);
5137 end if;
5138
5139 -- A private extension inherits any class-wide invariants coming from a
5140 -- parent type or an interface. Note that the invariant procedure of the
5141 -- parent type should not be inherited because the private extension may
5142 -- define invariants of its own.
5143
5144 if Has_Inherited_Invariants (Parent_Type)
5145 or else Has_Inheritable_Invariants (Parent_Type)
5146 then
5147 Set_Has_Inherited_Invariants (T);
5148
5149 elsif Present (Interfaces (T)) then
5150 Iface_Elmt := First_Elmt (Interfaces (T));
5151 while Present (Iface_Elmt) loop
5152 Iface := Node (Iface_Elmt);
5153
5154 if Has_Inheritable_Invariants (Iface) then
5155 Set_Has_Inherited_Invariants (T);
5156 exit;
5157 end if;
5158
5159 Next_Elmt (Iface_Elmt);
5160 end loop;
5161 end if;
5162
5163 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5164 -- synchronized formal derived type.
5165
5166 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5167 Set_Is_Limited_Record (T);
5168
5169 -- Formal derived type case
5170
5171 if Is_Generic_Type (T) then
5172
5173 -- The parent must be a tagged limited type or a synchronized
5174 -- interface.
5175
5176 if (not Is_Tagged_Type (Parent_Type)
5177 or else not Is_Limited_Type (Parent_Type))
5178 and then
5179 (not Is_Interface (Parent_Type)
5180 or else not Is_Synchronized_Interface (Parent_Type))
5181 then
5182 Error_Msg_NE
5183 ("parent type of & must be tagged limited or synchronized",
5184 N, T);
5185 end if;
5186
5187 -- The progenitors (if any) must be limited or synchronized
5188 -- interfaces.
5189
5190 if Present (Interfaces (T)) then
5191 Iface_Elmt := First_Elmt (Interfaces (T));
5192 while Present (Iface_Elmt) loop
5193 Iface := Node (Iface_Elmt);
5194
5195 if not Is_Limited_Interface (Iface)
5196 and then not Is_Synchronized_Interface (Iface)
5197 then
5198 Error_Msg_NE
5199 ("progenitor & must be limited or synchronized",
5200 N, Iface);
5201 end if;
5202
5203 Next_Elmt (Iface_Elmt);
5204 end loop;
5205 end if;
5206
5207 -- Regular derived extension, the parent must be a limited or
5208 -- synchronized interface.
5209
5210 else
5211 if not Is_Interface (Parent_Type)
5212 or else (not Is_Limited_Interface (Parent_Type)
5213 and then not Is_Synchronized_Interface (Parent_Type))
5214 then
5215 Error_Msg_NE
5216 ("parent type of & must be limited interface", N, T);
5217 end if;
5218 end if;
5219
5220 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5221 -- extension with a synchronized parent must be explicitly declared
5222 -- synchronized, because the full view will be a synchronized type.
5223 -- This must be checked before the check for limited types below,
5224 -- to ensure that types declared limited are not allowed to extend
5225 -- synchronized interfaces.
5226
5227 elsif Is_Interface (Parent_Type)
5228 and then Is_Synchronized_Interface (Parent_Type)
5229 and then not Synchronized_Present (N)
5230 then
5231 Error_Msg_NE
5232 ("private extension of& must be explicitly synchronized",
5233 N, Parent_Type);
5234
5235 elsif Limited_Present (N) then
5236 Set_Is_Limited_Record (T);
5237
5238 if not Is_Limited_Type (Parent_Type)
5239 and then
5240 (not Is_Interface (Parent_Type)
5241 or else not Is_Limited_Interface (Parent_Type))
5242 then
5243 Error_Msg_NE ("parent type& of limited extension must be limited",
5244 N, Parent_Type);
5245 end if;
5246 end if;
5247
5248 -- Remember that its parent type has a private extension. Used to warn
5249 -- on public primitives of the parent type defined after its private
5250 -- extensions (see Check_Dispatching_Operation).
5251
5252 Set_Has_Private_Extension (Parent_Type);
5253
5254 <<Leave>>
5255 if Has_Aspects (N) then
5256 Analyze_Aspect_Specifications (N, T);
5257 end if;
5258 end Analyze_Private_Extension_Declaration;
5259
5260 ---------------------------------
5261 -- Analyze_Subtype_Declaration --
5262 ---------------------------------
5263
5264 procedure Analyze_Subtype_Declaration
5265 (N : Node_Id;
5266 Skip : Boolean := False)
5267 is
5268 Id : constant Entity_Id := Defining_Identifier (N);
5269 R_Checks : Check_Result;
5270 T : Entity_Id;
5271
5272 begin
5273 Generate_Definition (Id);
5274 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5275 Init_Size_Align (Id);
5276
5277 -- The following guard condition on Enter_Name is to handle cases where
5278 -- the defining identifier has already been entered into the scope but
5279 -- the declaration as a whole needs to be analyzed.
5280
5281 -- This case in particular happens for derived enumeration types. The
5282 -- derived enumeration type is processed as an inserted enumeration type
5283 -- declaration followed by a rewritten subtype declaration. The defining
5284 -- identifier, however, is entered into the name scope very early in the
5285 -- processing of the original type declaration and therefore needs to be
5286 -- avoided here, when the created subtype declaration is analyzed. (See
5287 -- Build_Derived_Types)
5288
5289 -- This also happens when the full view of a private type is derived
5290 -- type with constraints. In this case the entity has been introduced
5291 -- in the private declaration.
5292
5293 -- Finally this happens in some complex cases when validity checks are
5294 -- enabled, where the same subtype declaration may be analyzed twice.
5295 -- This can happen if the subtype is created by the preanalysis of
5296 -- an attribute tht gives the range of a loop statement, and the loop
5297 -- itself appears within an if_statement that will be rewritten during
5298 -- expansion.
5299
5300 if Skip
5301 or else (Present (Etype (Id))
5302 and then (Is_Private_Type (Etype (Id))
5303 or else Is_Task_Type (Etype (Id))
5304 or else Is_Rewrite_Substitution (N)))
5305 then
5306 null;
5307
5308 elsif Current_Entity (Id) = Id then
5309 null;
5310
5311 else
5312 Enter_Name (Id);
5313 end if;
5314
5315 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5316
5317 -- Class-wide equivalent types of records with unknown discriminants
5318 -- involve the generation of an itype which serves as the private view
5319 -- of a constrained record subtype. In such cases the base type of the
5320 -- current subtype we are processing is the private itype. Use the full
5321 -- of the private itype when decorating various attributes.
5322
5323 if Is_Itype (T)
5324 and then Is_Private_Type (T)
5325 and then Present (Full_View (T))
5326 then
5327 T := Full_View (T);
5328 end if;
5329
5330 -- Inherit common attributes
5331
5332 Set_Is_Volatile (Id, Is_Volatile (T));
5333 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5334 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5335 Set_Convention (Id, Convention (T));
5336
5337 -- If ancestor has predicates then so does the subtype, and in addition
5338 -- we must delay the freeze to properly arrange predicate inheritance.
5339
5340 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5341 -- in which T = ID, so the above tests and assignments do nothing???
5342
5343 if Has_Predicates (T)
5344 or else (Present (Ancestor_Subtype (T))
5345 and then Has_Predicates (Ancestor_Subtype (T)))
5346 then
5347 Set_Has_Predicates (Id);
5348 Set_Has_Delayed_Freeze (Id);
5349
5350 -- Generated subtypes inherit the predicate function from the parent
5351 -- (no aspects to examine on the generated declaration).
5352
5353 if not Comes_From_Source (N) then
5354 Set_Ekind (Id, Ekind (T));
5355
5356 if Present (Predicate_Function (Id)) then
5357 null;
5358
5359 elsif Present (Predicate_Function (T)) then
5360 Set_Predicate_Function (Id, Predicate_Function (T));
5361
5362 elsif Present (Ancestor_Subtype (T))
5363 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5364 then
5365 Set_Predicate_Function (Id,
5366 Predicate_Function (Ancestor_Subtype (T)));
5367 end if;
5368 end if;
5369 end if;
5370
5371 -- In the case where there is no constraint given in the subtype
5372 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5373 -- semantic attributes must be established here.
5374
5375 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5376 Set_Etype (Id, Base_Type (T));
5377
5378 case Ekind (T) is
5379 when Array_Kind =>
5380 Set_Ekind (Id, E_Array_Subtype);
5381 Copy_Array_Subtype_Attributes (Id, T);
5382
5383 when Decimal_Fixed_Point_Kind =>
5384 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5385 Set_Digits_Value (Id, Digits_Value (T));
5386 Set_Delta_Value (Id, Delta_Value (T));
5387 Set_Scale_Value (Id, Scale_Value (T));
5388 Set_Small_Value (Id, Small_Value (T));
5389 Set_Scalar_Range (Id, Scalar_Range (T));
5390 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5391 Set_Is_Constrained (Id, Is_Constrained (T));
5392 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5393 Set_RM_Size (Id, RM_Size (T));
5394
5395 when Enumeration_Kind =>
5396 Set_Ekind (Id, E_Enumeration_Subtype);
5397 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5398 Set_Scalar_Range (Id, Scalar_Range (T));
5399 Set_Is_Character_Type (Id, Is_Character_Type (T));
5400 Set_Is_Constrained (Id, Is_Constrained (T));
5401 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5402 Set_RM_Size (Id, RM_Size (T));
5403
5404 when Ordinary_Fixed_Point_Kind =>
5405 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5406 Set_Scalar_Range (Id, Scalar_Range (T));
5407 Set_Small_Value (Id, Small_Value (T));
5408 Set_Delta_Value (Id, Delta_Value (T));
5409 Set_Is_Constrained (Id, Is_Constrained (T));
5410 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5411 Set_RM_Size (Id, RM_Size (T));
5412
5413 when Float_Kind =>
5414 Set_Ekind (Id, E_Floating_Point_Subtype);
5415 Set_Scalar_Range (Id, Scalar_Range (T));
5416 Set_Digits_Value (Id, Digits_Value (T));
5417 Set_Is_Constrained (Id, Is_Constrained (T));
5418
5419 -- If the floating point type has dimensions, these will be
5420 -- inherited subsequently when Analyze_Dimensions is called.
5421
5422 when Signed_Integer_Kind =>
5423 Set_Ekind (Id, E_Signed_Integer_Subtype);
5424 Set_Scalar_Range (Id, Scalar_Range (T));
5425 Set_Is_Constrained (Id, Is_Constrained (T));
5426 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5427 Set_RM_Size (Id, RM_Size (T));
5428
5429 when Modular_Integer_Kind =>
5430 Set_Ekind (Id, E_Modular_Integer_Subtype);
5431 Set_Scalar_Range (Id, Scalar_Range (T));
5432 Set_Is_Constrained (Id, Is_Constrained (T));
5433 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5434 Set_RM_Size (Id, RM_Size (T));
5435
5436 when Class_Wide_Kind =>
5437 Set_Ekind (Id, E_Class_Wide_Subtype);
5438 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5439 Set_Cloned_Subtype (Id, T);
5440 Set_Is_Tagged_Type (Id, True);
5441 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5442 Set_Has_Unknown_Discriminants
5443 (Id, True);
5444 Set_No_Tagged_Streams_Pragma
5445 (Id, No_Tagged_Streams_Pragma (T));
5446
5447 if Ekind (T) = E_Class_Wide_Subtype then
5448 Set_Equivalent_Type (Id, Equivalent_Type (T));
5449 end if;
5450
5451 when E_Record_Subtype
5452 | E_Record_Type
5453 =>
5454 Set_Ekind (Id, E_Record_Subtype);
5455
5456 -- Subtype declarations introduced for formal type parameters
5457 -- in generic instantiations should inherit the Size value of
5458 -- the type they rename.
5459
5460 if Present (Generic_Parent_Type (N)) then
5461 Set_RM_Size (Id, RM_Size (T));
5462 end if;
5463
5464 if Ekind (T) = E_Record_Subtype
5465 and then Present (Cloned_Subtype (T))
5466 then
5467 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5468 else
5469 Set_Cloned_Subtype (Id, T);
5470 end if;
5471
5472 Set_First_Entity (Id, First_Entity (T));
5473 Set_Last_Entity (Id, Last_Entity (T));
5474 Set_Has_Discriminants (Id, Has_Discriminants (T));
5475 Set_Is_Constrained (Id, Is_Constrained (T));
5476 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5477 Set_Has_Implicit_Dereference
5478 (Id, Has_Implicit_Dereference (T));
5479 Set_Has_Unknown_Discriminants
5480 (Id, Has_Unknown_Discriminants (T));
5481
5482 if Has_Discriminants (T) then
5483 Set_Discriminant_Constraint
5484 (Id, Discriminant_Constraint (T));
5485 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5486
5487 elsif Has_Unknown_Discriminants (Id) then
5488 Set_Discriminant_Constraint (Id, No_Elist);
5489 end if;
5490
5491 if Is_Tagged_Type (T) then
5492 Set_Is_Tagged_Type (Id, True);
5493 Set_No_Tagged_Streams_Pragma
5494 (Id, No_Tagged_Streams_Pragma (T));
5495 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5496 Set_Direct_Primitive_Operations
5497 (Id, Direct_Primitive_Operations (T));
5498 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5499
5500 if Is_Interface (T) then
5501 Set_Is_Interface (Id);
5502 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5503 end if;
5504 end if;
5505
5506 when Private_Kind =>
5507 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5508 Set_Has_Discriminants (Id, Has_Discriminants (T));
5509 Set_Is_Constrained (Id, Is_Constrained (T));
5510 Set_First_Entity (Id, First_Entity (T));
5511 Set_Last_Entity (Id, Last_Entity (T));
5512 Set_Private_Dependents (Id, New_Elmt_List);
5513 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5514 Set_Has_Implicit_Dereference
5515 (Id, Has_Implicit_Dereference (T));
5516 Set_Has_Unknown_Discriminants
5517 (Id, Has_Unknown_Discriminants (T));
5518 Set_Known_To_Have_Preelab_Init
5519 (Id, Known_To_Have_Preelab_Init (T));
5520
5521 if Is_Tagged_Type (T) then
5522 Set_Is_Tagged_Type (Id);
5523 Set_No_Tagged_Streams_Pragma (Id,
5524 No_Tagged_Streams_Pragma (T));
5525 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5526 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5527 Set_Direct_Primitive_Operations (Id,
5528 Direct_Primitive_Operations (T));
5529 end if;
5530
5531 -- In general the attributes of the subtype of a private type
5532 -- are the attributes of the partial view of parent. However,
5533 -- the full view may be a discriminated type, and the subtype
5534 -- must share the discriminant constraint to generate correct
5535 -- calls to initialization procedures.
5536
5537 if Has_Discriminants (T) then
5538 Set_Discriminant_Constraint
5539 (Id, Discriminant_Constraint (T));
5540 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5541
5542 elsif Present (Full_View (T))
5543 and then Has_Discriminants (Full_View (T))
5544 then
5545 Set_Discriminant_Constraint
5546 (Id, Discriminant_Constraint (Full_View (T)));
5547 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5548
5549 -- This would seem semantically correct, but apparently
5550 -- generates spurious errors about missing components ???
5551
5552 -- Set_Has_Discriminants (Id);
5553 end if;
5554
5555 Prepare_Private_Subtype_Completion (Id, N);
5556
5557 -- If this is the subtype of a constrained private type with
5558 -- discriminants that has got a full view and we also have
5559 -- built a completion just above, show that the completion
5560 -- is a clone of the full view to the back-end.
5561
5562 if Has_Discriminants (T)
5563 and then not Has_Unknown_Discriminants (T)
5564 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5565 and then Present (Full_View (T))
5566 and then Present (Full_View (Id))
5567 then
5568 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5569 end if;
5570
5571 when Access_Kind =>
5572 Set_Ekind (Id, E_Access_Subtype);
5573 Set_Is_Constrained (Id, Is_Constrained (T));
5574 Set_Is_Access_Constant
5575 (Id, Is_Access_Constant (T));
5576 Set_Directly_Designated_Type
5577 (Id, Designated_Type (T));
5578 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5579
5580 -- A Pure library_item must not contain the declaration of a
5581 -- named access type, except within a subprogram, generic
5582 -- subprogram, task unit, or protected unit, or if it has
5583 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5584
5585 if Comes_From_Source (Id)
5586 and then In_Pure_Unit
5587 and then not In_Subprogram_Task_Protected_Unit
5588 and then not No_Pool_Assigned (Id)
5589 then
5590 Error_Msg_N
5591 ("named access types not allowed in pure unit", N);
5592 end if;
5593
5594 when Concurrent_Kind =>
5595 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5596 Set_Corresponding_Record_Type (Id,
5597 Corresponding_Record_Type (T));
5598 Set_First_Entity (Id, First_Entity (T));
5599 Set_First_Private_Entity (Id, First_Private_Entity (T));
5600 Set_Has_Discriminants (Id, Has_Discriminants (T));
5601 Set_Is_Constrained (Id, Is_Constrained (T));
5602 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5603 Set_Last_Entity (Id, Last_Entity (T));
5604
5605 if Is_Tagged_Type (T) then
5606 Set_No_Tagged_Streams_Pragma
5607 (Id, No_Tagged_Streams_Pragma (T));
5608 end if;
5609
5610 if Has_Discriminants (T) then
5611 Set_Discriminant_Constraint
5612 (Id, Discriminant_Constraint (T));
5613 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5614 end if;
5615
5616 when Incomplete_Kind =>
5617 if Ada_Version >= Ada_2005 then
5618
5619 -- In Ada 2005 an incomplete type can be explicitly tagged:
5620 -- propagate indication. Note that we also have to include
5621 -- subtypes for Ada 2012 extended use of incomplete types.
5622
5623 Set_Ekind (Id, E_Incomplete_Subtype);
5624 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5625 Set_Private_Dependents (Id, New_Elmt_List);
5626
5627 if Is_Tagged_Type (Id) then
5628 Set_No_Tagged_Streams_Pragma
5629 (Id, No_Tagged_Streams_Pragma (T));
5630 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5631 end if;
5632
5633 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5634 -- incomplete type visible through a limited with clause.
5635
5636 if From_Limited_With (T)
5637 and then Present (Non_Limited_View (T))
5638 then
5639 Set_From_Limited_With (Id);
5640 Set_Non_Limited_View (Id, Non_Limited_View (T));
5641
5642 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5643 -- to the private dependents of the original incomplete
5644 -- type for future transformation.
5645
5646 else
5647 Append_Elmt (Id, Private_Dependents (T));
5648 end if;
5649
5650 -- If the subtype name denotes an incomplete type an error
5651 -- was already reported by Process_Subtype.
5652
5653 else
5654 Set_Etype (Id, Any_Type);
5655 end if;
5656
5657 when others =>
5658 raise Program_Error;
5659 end case;
5660
5661 -- If there is no constraint in the subtype indication, the
5662 -- declared entity inherits predicates from the parent.
5663
5664 Inherit_Predicate_Flags (Id, T);
5665 end if;
5666
5667 if Etype (Id) = Any_Type then
5668 goto Leave;
5669 end if;
5670
5671 -- Some common processing on all types
5672
5673 Set_Size_Info (Id, T);
5674 Set_First_Rep_Item (Id, First_Rep_Item (T));
5675
5676 -- If the parent type is a generic actual, so is the subtype. This may
5677 -- happen in a nested instance. Why Comes_From_Source test???
5678
5679 if not Comes_From_Source (N) then
5680 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5681 end if;
5682
5683 -- If this is a subtype declaration for an actual in an instance,
5684 -- inherit static and dynamic predicates if any.
5685
5686 -- If declaration has no aspect specifications, inherit predicate
5687 -- info as well. Unclear how to handle the case of both specified
5688 -- and inherited predicates ??? Other inherited aspects, such as
5689 -- invariants, should be OK, but the combination with later pragmas
5690 -- may also require special merging.
5691
5692 if Has_Predicates (T)
5693 and then Present (Predicate_Function (T))
5694 and then
5695 ((In_Instance and then not Comes_From_Source (N))
5696 or else No (Aspect_Specifications (N)))
5697 then
5698 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5699
5700 if Has_Static_Predicate (T) then
5701 Set_Has_Static_Predicate (Id);
5702 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5703 end if;
5704 end if;
5705
5706 -- If the base type is a scalar type, or else if there is no
5707 -- constraint, the atomic flag is inherited by the subtype.
5708
5709 if Is_Scalar_Type (Id)
5710 or else Is_Entity_Name (Subtype_Indication (N))
5711 then
5712 Set_Is_Atomic (Id, Is_Atomic (T));
5713 end if;
5714
5715 -- Remaining processing depends on characteristics of base type
5716
5717 T := Etype (Id);
5718
5719 Set_Is_Immediately_Visible (Id, True);
5720 Set_Depends_On_Private (Id, Has_Private_Component (T));
5721 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5722
5723 if Is_Interface (T) then
5724 Set_Is_Interface (Id);
5725 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5726 end if;
5727
5728 if Present (Generic_Parent_Type (N))
5729 and then
5730 (Nkind (Parent (Generic_Parent_Type (N))) /=
5731 N_Formal_Type_Declaration
5732 or else Nkind (Formal_Type_Definition
5733 (Parent (Generic_Parent_Type (N)))) /=
5734 N_Formal_Private_Type_Definition)
5735 then
5736 if Is_Tagged_Type (Id) then
5737
5738 -- If this is a generic actual subtype for a synchronized type,
5739 -- the primitive operations are those of the corresponding record
5740 -- for which there is a separate subtype declaration.
5741
5742 if Is_Concurrent_Type (Id) then
5743 null;
5744 elsif Is_Class_Wide_Type (Id) then
5745 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5746 else
5747 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5748 end if;
5749
5750 elsif Scope (Etype (Id)) /= Standard_Standard then
5751 Derive_Subprograms (Generic_Parent_Type (N), Id);
5752 end if;
5753 end if;
5754
5755 if Is_Private_Type (T) and then Present (Full_View (T)) then
5756 Conditional_Delay (Id, Full_View (T));
5757
5758 -- The subtypes of components or subcomponents of protected types
5759 -- do not need freeze nodes, which would otherwise appear in the
5760 -- wrong scope (before the freeze node for the protected type). The
5761 -- proper subtypes are those of the subcomponents of the corresponding
5762 -- record.
5763
5764 elsif Ekind (Scope (Id)) /= E_Protected_Type
5765 and then Present (Scope (Scope (Id))) -- error defense
5766 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5767 then
5768 Conditional_Delay (Id, T);
5769 end if;
5770
5771 -- If we have a subtype of an incomplete type whose full type is a
5772 -- derived numeric type, we need to have a freeze node for the subtype.
5773 -- Otherwise gigi will complain while computing the (static) bounds of
5774 -- the subtype.
5775
5776 if Is_Itype (T)
5777 and then Is_Elementary_Type (Id)
5778 and then Etype (Id) /= Id
5779 then
5780 declare
5781 Partial : constant Entity_Id :=
5782 Incomplete_Or_Partial_View (First_Subtype (Id));
5783 begin
5784 if Present (Partial)
5785 and then Ekind (Partial) = E_Incomplete_Type
5786 then
5787 Set_Has_Delayed_Freeze (Id);
5788 end if;
5789 end;
5790 end if;
5791
5792 -- Check that Constraint_Error is raised for a scalar subtype indication
5793 -- when the lower or upper bound of a non-null range lies outside the
5794 -- range of the type mark.
5795
5796 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5797 if Is_Scalar_Type (Etype (Id))
5798 and then Scalar_Range (Id) /=
5799 Scalar_Range
5800 (Etype (Subtype_Mark (Subtype_Indication (N))))
5801 then
5802 Apply_Range_Check
5803 (Scalar_Range (Id),
5804 Etype (Subtype_Mark (Subtype_Indication (N))));
5805
5806 -- In the array case, check compatibility for each index
5807
5808 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5809 then
5810 -- This really should be a subprogram that finds the indications
5811 -- to check???
5812
5813 declare
5814 Subt_Index : Node_Id := First_Index (Id);
5815 Target_Index : Node_Id :=
5816 First_Index (Etype
5817 (Subtype_Mark (Subtype_Indication (N))));
5818
5819 begin
5820 while Present (Subt_Index) loop
5821 if ((Nkind (Subt_Index) = N_Identifier
5822 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5823 or else Nkind (Subt_Index) = N_Subtype_Indication)
5824 and then
5825 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5826 then
5827 declare
5828 Target_Typ : constant Entity_Id :=
5829 Etype (Target_Index);
5830 begin
5831 R_Checks :=
5832 Get_Range_Checks
5833 (Scalar_Range (Etype (Subt_Index)),
5834 Target_Typ,
5835 Etype (Subt_Index),
5836 Defining_Identifier (N));
5837
5838 Insert_Range_Checks
5839 (R_Checks,
5840 N,
5841 Target_Typ,
5842 Sloc (Defining_Identifier (N)));
5843 end;
5844 end if;
5845
5846 Next_Index (Subt_Index);
5847 Next_Index (Target_Index);
5848 end loop;
5849 end;
5850 end if;
5851 end if;
5852
5853 Set_Optimize_Alignment_Flags (Id);
5854 Check_Eliminated (Id);
5855
5856 <<Leave>>
5857 if Has_Aspects (N) then
5858 Analyze_Aspect_Specifications (N, Id);
5859 end if;
5860
5861 Analyze_Dimension (N);
5862
5863 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5864 -- indications on composite types where the constraints are dynamic.
5865 -- Note that object declarations and aggregates generate implicit
5866 -- subtype declarations, which this covers. One special case is that the
5867 -- implicitly generated "=" for discriminated types includes an
5868 -- offending subtype declaration, which is harmless, so we ignore it
5869 -- here.
5870
5871 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5872 declare
5873 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5874 begin
5875 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5876 and then not (Is_Internal (Id)
5877 and then Is_TSS (Scope (Id),
5878 TSS_Composite_Equality))
5879 and then not Within_Init_Proc
5880 and then not All_Composite_Constraints_Static (Cstr)
5881 then
5882 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5883 end if;
5884 end;
5885 end if;
5886 end Analyze_Subtype_Declaration;
5887
5888 --------------------------------
5889 -- Analyze_Subtype_Indication --
5890 --------------------------------
5891
5892 procedure Analyze_Subtype_Indication (N : Node_Id) is
5893 T : constant Entity_Id := Subtype_Mark (N);
5894 R : constant Node_Id := Range_Expression (Constraint (N));
5895
5896 begin
5897 Analyze (T);
5898
5899 if R /= Error then
5900 Analyze (R);
5901 Set_Etype (N, Etype (R));
5902 Resolve (R, Entity (T));
5903 else
5904 Set_Error_Posted (R);
5905 Set_Error_Posted (T);
5906 end if;
5907 end Analyze_Subtype_Indication;
5908
5909 --------------------------
5910 -- Analyze_Variant_Part --
5911 --------------------------
5912
5913 procedure Analyze_Variant_Part (N : Node_Id) is
5914 Discr_Name : Node_Id;
5915 Discr_Type : Entity_Id;
5916
5917 procedure Process_Variant (A : Node_Id);
5918 -- Analyze declarations for a single variant
5919
5920 package Analyze_Variant_Choices is
5921 new Generic_Analyze_Choices (Process_Variant);
5922 use Analyze_Variant_Choices;
5923
5924 ---------------------
5925 -- Process_Variant --
5926 ---------------------
5927
5928 procedure Process_Variant (A : Node_Id) is
5929 CL : constant Node_Id := Component_List (A);
5930 begin
5931 if not Null_Present (CL) then
5932 Analyze_Declarations (Component_Items (CL));
5933
5934 if Present (Variant_Part (CL)) then
5935 Analyze (Variant_Part (CL));
5936 end if;
5937 end if;
5938 end Process_Variant;
5939
5940 -- Start of processing for Analyze_Variant_Part
5941
5942 begin
5943 Discr_Name := Name (N);
5944 Analyze (Discr_Name);
5945
5946 -- If Discr_Name bad, get out (prevent cascaded errors)
5947
5948 if Etype (Discr_Name) = Any_Type then
5949 return;
5950 end if;
5951
5952 -- Check invalid discriminant in variant part
5953
5954 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5955 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5956 end if;
5957
5958 Discr_Type := Etype (Entity (Discr_Name));
5959
5960 if not Is_Discrete_Type (Discr_Type) then
5961 Error_Msg_N
5962 ("discriminant in a variant part must be of a discrete type",
5963 Name (N));
5964 return;
5965 end if;
5966
5967 -- Now analyze the choices, which also analyzes the declarations that
5968 -- are associated with each choice.
5969
5970 Analyze_Choices (Variants (N), Discr_Type);
5971
5972 -- Note: we used to instantiate and call Check_Choices here to check
5973 -- that the choices covered the discriminant, but it's too early to do
5974 -- that because of statically predicated subtypes, whose analysis may
5975 -- be deferred to their freeze point which may be as late as the freeze
5976 -- point of the containing record. So this call is now to be found in
5977 -- Freeze_Record_Declaration.
5978
5979 end Analyze_Variant_Part;
5980
5981 ----------------------------
5982 -- Array_Type_Declaration --
5983 ----------------------------
5984
5985 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5986 Component_Def : constant Node_Id := Component_Definition (Def);
5987 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5988 P : constant Node_Id := Parent (Def);
5989 Element_Type : Entity_Id;
5990 Implicit_Base : Entity_Id;
5991 Index : Node_Id;
5992 Nb_Index : Nat;
5993 Priv : Entity_Id;
5994 Related_Id : Entity_Id := Empty;
5995
5996 begin
5997 if Nkind (Def) = N_Constrained_Array_Definition then
5998 Index := First (Discrete_Subtype_Definitions (Def));
5999 else
6000 Index := First (Subtype_Marks (Def));
6001 end if;
6002
6003 -- Find proper names for the implicit types which may be public. In case
6004 -- of anonymous arrays we use the name of the first object of that type
6005 -- as prefix.
6006
6007 if No (T) then
6008 Related_Id := Defining_Identifier (P);
6009 else
6010 Related_Id := T;
6011 end if;
6012
6013 Nb_Index := 1;
6014 while Present (Index) loop
6015 Analyze (Index);
6016
6017 -- Test for odd case of trying to index a type by the type itself
6018
6019 if Is_Entity_Name (Index) and then Entity (Index) = T then
6020 Error_Msg_N ("type& cannot be indexed by itself", Index);
6021 Set_Entity (Index, Standard_Boolean);
6022 Set_Etype (Index, Standard_Boolean);
6023 end if;
6024
6025 -- Add a subtype declaration for each index of private array type
6026 -- declaration whose type is also private. For example:
6027
6028 -- package Pkg is
6029 -- type Index is private;
6030 -- private
6031 -- type Table is array (Index) of ...
6032 -- end;
6033
6034 -- This is currently required by the expander for the internally
6035 -- generated equality subprogram of records with variant parts in
6036 -- which the type of some component is such a private type. And it
6037 -- also helps semantic analysis in peculiar cases where the array
6038 -- type is referenced from an instance but not the index directly.
6039
6040 if Is_Package_Or_Generic_Package (Current_Scope)
6041 and then In_Private_Part (Current_Scope)
6042 and then Has_Private_Declaration (Etype (Index))
6043 and then Scope (Etype (Index)) = Current_Scope
6044 then
6045 declare
6046 Loc : constant Source_Ptr := Sloc (Def);
6047 Decl : Entity_Id;
6048 New_E : Entity_Id;
6049
6050 begin
6051 New_E := Make_Temporary (Loc, 'T');
6052 Set_Is_Internal (New_E);
6053
6054 Decl :=
6055 Make_Subtype_Declaration (Loc,
6056 Defining_Identifier => New_E,
6057 Subtype_Indication =>
6058 New_Occurrence_Of (Etype (Index), Loc));
6059
6060 Insert_Before (Parent (Def), Decl);
6061 Analyze (Decl);
6062 Set_Etype (Index, New_E);
6063
6064 -- If the index is a range or a subtype indication it carries
6065 -- no entity. Example:
6066
6067 -- package Pkg is
6068 -- type T is private;
6069 -- private
6070 -- type T is new Natural;
6071 -- Table : array (T(1) .. T(10)) of Boolean;
6072 -- end Pkg;
6073
6074 -- Otherwise the type of the reference is its entity.
6075
6076 if Is_Entity_Name (Index) then
6077 Set_Entity (Index, New_E);
6078 end if;
6079 end;
6080 end if;
6081
6082 Make_Index (Index, P, Related_Id, Nb_Index);
6083
6084 -- Check error of subtype with predicate for index type
6085
6086 Bad_Predicated_Subtype_Use
6087 ("subtype& has predicate, not allowed as index subtype",
6088 Index, Etype (Index));
6089
6090 -- Move to next index
6091
6092 Next_Index (Index);
6093 Nb_Index := Nb_Index + 1;
6094 end loop;
6095
6096 -- Process subtype indication if one is present
6097
6098 if Present (Component_Typ) then
6099 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6100 Set_Etype (Component_Typ, Element_Type);
6101
6102 -- Ada 2005 (AI-230): Access Definition case
6103
6104 else pragma Assert (Present (Access_Definition (Component_Def)));
6105
6106 -- Indicate that the anonymous access type is created by the
6107 -- array type declaration.
6108
6109 Element_Type := Access_Definition
6110 (Related_Nod => P,
6111 N => Access_Definition (Component_Def));
6112 Set_Is_Local_Anonymous_Access (Element_Type);
6113
6114 -- Propagate the parent. This field is needed if we have to generate
6115 -- the master_id associated with an anonymous access to task type
6116 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6117
6118 Set_Parent (Element_Type, Parent (T));
6119
6120 -- Ada 2005 (AI-230): In case of components that are anonymous access
6121 -- types the level of accessibility depends on the enclosing type
6122 -- declaration
6123
6124 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6125
6126 -- Ada 2005 (AI-254)
6127
6128 declare
6129 CD : constant Node_Id :=
6130 Access_To_Subprogram_Definition
6131 (Access_Definition (Component_Def));
6132 begin
6133 if Present (CD) and then Protected_Present (CD) then
6134 Element_Type :=
6135 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6136 end if;
6137 end;
6138 end if;
6139
6140 -- Constrained array case
6141
6142 if No (T) then
6143 T := Create_Itype (E_Void, P, Related_Id, 'T');
6144 end if;
6145
6146 if Nkind (Def) = N_Constrained_Array_Definition then
6147
6148 -- Establish Implicit_Base as unconstrained base type
6149
6150 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6151
6152 Set_Etype (Implicit_Base, Implicit_Base);
6153 Set_Scope (Implicit_Base, Current_Scope);
6154 Set_Has_Delayed_Freeze (Implicit_Base);
6155 Set_Default_SSO (Implicit_Base);
6156
6157 -- The constrained array type is a subtype of the unconstrained one
6158
6159 Set_Ekind (T, E_Array_Subtype);
6160 Init_Size_Align (T);
6161 Set_Etype (T, Implicit_Base);
6162 Set_Scope (T, Current_Scope);
6163 Set_Is_Constrained (T);
6164 Set_First_Index (T,
6165 First (Discrete_Subtype_Definitions (Def)));
6166 Set_Has_Delayed_Freeze (T);
6167
6168 -- Complete setup of implicit base type
6169
6170 Set_Component_Size (Implicit_Base, Uint_0);
6171 Set_Component_Type (Implicit_Base, Element_Type);
6172 Set_Finalize_Storage_Only
6173 (Implicit_Base,
6174 Finalize_Storage_Only (Element_Type));
6175 Set_First_Index (Implicit_Base, First_Index (T));
6176 Set_Has_Controlled_Component
6177 (Implicit_Base,
6178 Has_Controlled_Component (Element_Type)
6179 or else Is_Controlled (Element_Type));
6180 Set_Packed_Array_Impl_Type
6181 (Implicit_Base, Empty);
6182
6183 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6184
6185 -- Unconstrained array case
6186
6187 else
6188 Set_Ekind (T, E_Array_Type);
6189 Init_Size_Align (T);
6190 Set_Etype (T, T);
6191 Set_Scope (T, Current_Scope);
6192 Set_Component_Size (T, Uint_0);
6193 Set_Is_Constrained (T, False);
6194 Set_First_Index (T, First (Subtype_Marks (Def)));
6195 Set_Has_Delayed_Freeze (T, True);
6196 Propagate_Concurrent_Flags (T, Element_Type);
6197 Set_Has_Controlled_Component (T, Has_Controlled_Component
6198 (Element_Type)
6199 or else
6200 Is_Controlled (Element_Type));
6201 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6202 (Element_Type));
6203 Set_Default_SSO (T);
6204 end if;
6205
6206 -- Common attributes for both cases
6207
6208 Set_Component_Type (Base_Type (T), Element_Type);
6209 Set_Packed_Array_Impl_Type (T, Empty);
6210
6211 if Aliased_Present (Component_Definition (Def)) then
6212 Set_Has_Aliased_Components (Etype (T));
6213
6214 -- AI12-001: All aliased objects are considered to be specified as
6215 -- independently addressable (RM C.6(8.1/4)).
6216
6217 Set_Has_Independent_Components (Etype (T));
6218 end if;
6219
6220 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6221 -- array type to ensure that objects of this type are initialized.
6222
6223 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6224 Set_Can_Never_Be_Null (T);
6225
6226 if Null_Exclusion_Present (Component_Definition (Def))
6227
6228 -- No need to check itypes because in their case this check was
6229 -- done at their point of creation
6230
6231 and then not Is_Itype (Element_Type)
6232 then
6233 Error_Msg_N
6234 ("`NOT NULL` not allowed (null already excluded)",
6235 Subtype_Indication (Component_Definition (Def)));
6236 end if;
6237 end if;
6238
6239 Priv := Private_Component (Element_Type);
6240
6241 if Present (Priv) then
6242
6243 -- Check for circular definitions
6244
6245 if Priv = Any_Type then
6246 Set_Component_Type (Etype (T), Any_Type);
6247
6248 -- There is a gap in the visibility of operations on the composite
6249 -- type only if the component type is defined in a different scope.
6250
6251 elsif Scope (Priv) = Current_Scope then
6252 null;
6253
6254 elsif Is_Limited_Type (Priv) then
6255 Set_Is_Limited_Composite (Etype (T));
6256 Set_Is_Limited_Composite (T);
6257 else
6258 Set_Is_Private_Composite (Etype (T));
6259 Set_Is_Private_Composite (T);
6260 end if;
6261 end if;
6262
6263 -- A syntax error in the declaration itself may lead to an empty index
6264 -- list, in which case do a minimal patch.
6265
6266 if No (First_Index (T)) then
6267 Error_Msg_N ("missing index definition in array type declaration", T);
6268
6269 declare
6270 Indexes : constant List_Id :=
6271 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6272 begin
6273 Set_Discrete_Subtype_Definitions (Def, Indexes);
6274 Set_First_Index (T, First (Indexes));
6275 return;
6276 end;
6277 end if;
6278
6279 -- Create a concatenation operator for the new type. Internal array
6280 -- types created for packed entities do not need such, they are
6281 -- compatible with the user-defined type.
6282
6283 if Number_Dimensions (T) = 1
6284 and then not Is_Packed_Array_Impl_Type (T)
6285 then
6286 New_Concatenation_Op (T);
6287 end if;
6288
6289 -- In the case of an unconstrained array the parser has already verified
6290 -- that all the indexes are unconstrained but we still need to make sure
6291 -- that the element type is constrained.
6292
6293 if not Is_Definite_Subtype (Element_Type) then
6294 Error_Msg_N
6295 ("unconstrained element type in array declaration",
6296 Subtype_Indication (Component_Def));
6297
6298 elsif Is_Abstract_Type (Element_Type) then
6299 Error_Msg_N
6300 ("the type of a component cannot be abstract",
6301 Subtype_Indication (Component_Def));
6302 end if;
6303
6304 -- There may be an invariant declared for the component type, but
6305 -- the construction of the component invariant checking procedure
6306 -- takes place during expansion.
6307 end Array_Type_Declaration;
6308
6309 ------------------------------------------------------
6310 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6311 ------------------------------------------------------
6312
6313 function Replace_Anonymous_Access_To_Protected_Subprogram
6314 (N : Node_Id) return Entity_Id
6315 is
6316 Loc : constant Source_Ptr := Sloc (N);
6317
6318 Curr_Scope : constant Scope_Stack_Entry :=
6319 Scope_Stack.Table (Scope_Stack.Last);
6320
6321 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6322
6323 Acc : Node_Id;
6324 -- Access definition in declaration
6325
6326 Comp : Node_Id;
6327 -- Object definition or formal definition with an access definition
6328
6329 Decl : Node_Id;
6330 -- Declaration of anonymous access to subprogram type
6331
6332 Spec : Node_Id;
6333 -- Original specification in access to subprogram
6334
6335 P : Node_Id;
6336
6337 begin
6338 Set_Is_Internal (Anon);
6339
6340 case Nkind (N) is
6341 when N_Constrained_Array_Definition
6342 | N_Component_Declaration
6343 | N_Unconstrained_Array_Definition
6344 =>
6345 Comp := Component_Definition (N);
6346 Acc := Access_Definition (Comp);
6347
6348 when N_Discriminant_Specification =>
6349 Comp := Discriminant_Type (N);
6350 Acc := Comp;
6351
6352 when N_Parameter_Specification =>
6353 Comp := Parameter_Type (N);
6354 Acc := Comp;
6355
6356 when N_Access_Function_Definition =>
6357 Comp := Result_Definition (N);
6358 Acc := Comp;
6359
6360 when N_Object_Declaration =>
6361 Comp := Object_Definition (N);
6362 Acc := Comp;
6363
6364 when N_Function_Specification =>
6365 Comp := Result_Definition (N);
6366 Acc := Comp;
6367
6368 when others =>
6369 raise Program_Error;
6370 end case;
6371
6372 Spec := Access_To_Subprogram_Definition (Acc);
6373
6374 Decl :=
6375 Make_Full_Type_Declaration (Loc,
6376 Defining_Identifier => Anon,
6377 Type_Definition => Copy_Separate_Tree (Spec));
6378
6379 Mark_Rewrite_Insertion (Decl);
6380
6381 -- Insert the new declaration in the nearest enclosing scope. If the
6382 -- parent is a body and N is its return type, the declaration belongs
6383 -- in the enclosing scope. Likewise if N is the type of a parameter.
6384
6385 P := Parent (N);
6386
6387 if Nkind (N) = N_Function_Specification
6388 and then Nkind (P) = N_Subprogram_Body
6389 then
6390 P := Parent (P);
6391 elsif Nkind (N) = N_Parameter_Specification
6392 and then Nkind (P) in N_Subprogram_Specification
6393 and then Nkind (Parent (P)) = N_Subprogram_Body
6394 then
6395 P := Parent (Parent (P));
6396 end if;
6397
6398 while Present (P) and then not Has_Declarations (P) loop
6399 P := Parent (P);
6400 end loop;
6401
6402 pragma Assert (Present (P));
6403
6404 if Nkind (P) = N_Package_Specification then
6405 Prepend (Decl, Visible_Declarations (P));
6406 else
6407 Prepend (Decl, Declarations (P));
6408 end if;
6409
6410 -- Replace the anonymous type with an occurrence of the new declaration.
6411 -- In all cases the rewritten node does not have the null-exclusion
6412 -- attribute because (if present) it was already inherited by the
6413 -- anonymous entity (Anon). Thus, in case of components we do not
6414 -- inherit this attribute.
6415
6416 if Nkind (N) = N_Parameter_Specification then
6417 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6418 Set_Etype (Defining_Identifier (N), Anon);
6419 Set_Null_Exclusion_Present (N, False);
6420
6421 elsif Nkind (N) = N_Object_Declaration then
6422 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6423 Set_Etype (Defining_Identifier (N), Anon);
6424
6425 elsif Nkind (N) = N_Access_Function_Definition then
6426 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6427
6428 elsif Nkind (N) = N_Function_Specification then
6429 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6430 Set_Etype (Defining_Unit_Name (N), Anon);
6431
6432 else
6433 Rewrite (Comp,
6434 Make_Component_Definition (Loc,
6435 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6436 end if;
6437
6438 Mark_Rewrite_Insertion (Comp);
6439
6440 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6441 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6442 and then not Is_Type (Current_Scope))
6443 then
6444
6445 -- Declaration can be analyzed in the current scope.
6446
6447 Analyze (Decl);
6448
6449 else
6450 -- Temporarily remove the current scope (record or subprogram) from
6451 -- the stack to add the new declarations to the enclosing scope.
6452 -- The anonymous entity is an Itype with the proper attributes.
6453
6454 Scope_Stack.Decrement_Last;
6455 Analyze (Decl);
6456 Set_Is_Itype (Anon);
6457 Set_Associated_Node_For_Itype (Anon, N);
6458 Scope_Stack.Append (Curr_Scope);
6459 end if;
6460
6461 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6462 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6463 return Anon;
6464 end Replace_Anonymous_Access_To_Protected_Subprogram;
6465
6466 -------------------------------------
6467 -- Build_Access_Subprogram_Wrapper --
6468 -------------------------------------
6469
6470 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6471 Loc : constant Source_Ptr := Sloc (Decl);
6472 Id : constant Entity_Id := Defining_Identifier (Decl);
6473 Type_Def : constant Node_Id := Type_Definition (Decl);
6474 Specs : constant List_Id :=
6475 Parameter_Specifications (Type_Def);
6476 Profile : constant List_Id := New_List;
6477 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6478
6479 Contracts : constant List_Id := New_List;
6480 Form_P : Node_Id;
6481 New_P : Node_Id;
6482 New_Decl : Node_Id;
6483 Spec : Node_Id;
6484
6485 procedure Replace_Type_Name (Expr : Node_Id);
6486 -- In the expressions for contract aspects, replace occurrences of the
6487 -- access type with the name of the subprogram entity, as needed, e.g.
6488 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6489 -- remain on the original access type declaration. What about expanded
6490 -- names denoting formals, whose prefix in source is the type name ???
6491
6492 -----------------------
6493 -- Replace_Type_Name --
6494 -----------------------
6495
6496 procedure Replace_Type_Name (Expr : Node_Id) is
6497 function Process (N : Node_Id) return Traverse_Result;
6498 function Process (N : Node_Id) return Traverse_Result is
6499 begin
6500 if Nkind (N) = N_Attribute_Reference
6501 and then Is_Entity_Name (Prefix (N))
6502 and then Chars (Prefix (N)) = Chars (Id)
6503 then
6504 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6505 end if;
6506
6507 return OK;
6508 end Process;
6509
6510 procedure Traverse is new Traverse_Proc (Process);
6511 begin
6512 Traverse (Expr);
6513 end Replace_Type_Name;
6514
6515 begin
6516 if Ekind_In (Id, E_Access_Subprogram_Type,
6517 E_Access_Protected_Subprogram_Type,
6518 E_Anonymous_Access_Protected_Subprogram_Type,
6519 E_Anonymous_Access_Subprogram_Type)
6520 then
6521 null;
6522
6523 else
6524 Error_Msg_N
6525 ("illegal pre/postcondition on access type", Decl);
6526 return;
6527 end if;
6528
6529 declare
6530 Asp : Node_Id;
6531 A_Id : Aspect_Id;
6532 Cond : Node_Id;
6533 Expr : Node_Id;
6534
6535 begin
6536 Asp := First (Aspect_Specifications (Decl));
6537 while Present (Asp) loop
6538 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6539 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6540 Cond := Asp;
6541 Expr := Expression (Cond);
6542 Replace_Type_Name (Expr);
6543 Next (Asp);
6544
6545 Remove (Cond);
6546 Append (Cond, Contracts);
6547
6548 else
6549 Next (Asp);
6550 end if;
6551 end loop;
6552 end;
6553
6554 -- If there are no contract aspects, no need for a wrapper.
6555
6556 if Is_Empty_List (Contracts) then
6557 return;
6558 end if;
6559
6560 Form_P := First (Specs);
6561
6562 while Present (Form_P) loop
6563 New_P := New_Copy_Tree (Form_P);
6564 Set_Defining_Identifier (New_P,
6565 Make_Defining_Identifier
6566 (Loc, Chars (Defining_Identifier (Form_P))));
6567 Append (New_P, Profile);
6568 Next (Form_P);
6569 end loop;
6570
6571 -- Add to parameter specifications the access parameter that is passed
6572 -- in from an indirect call.
6573
6574 Append (
6575 Make_Parameter_Specification (Loc,
6576 Defining_Identifier => Make_Temporary (Loc, 'P'),
6577 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6578 Profile);
6579
6580 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6581 Spec :=
6582 Make_Procedure_Specification (Loc,
6583 Defining_Unit_Name => Subp,
6584 Parameter_Specifications => Profile);
6585 else
6586 Spec :=
6587 Make_Function_Specification (Loc,
6588 Defining_Unit_Name => Subp,
6589 Parameter_Specifications => Profile,
6590 Result_Definition =>
6591 New_Copy_Tree
6592 (Result_Definition (Type_Definition (Decl))));
6593 end if;
6594
6595 New_Decl :=
6596 Make_Subprogram_Declaration (Loc, Specification => Spec);
6597 Set_Aspect_Specifications (New_Decl, Contracts);
6598
6599 Insert_After (Decl, New_Decl);
6600 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6601 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6602 end Build_Access_Subprogram_Wrapper;
6603
6604 -------------------------------
6605 -- Build_Derived_Access_Type --
6606 -------------------------------
6607
6608 procedure Build_Derived_Access_Type
6609 (N : Node_Id;
6610 Parent_Type : Entity_Id;
6611 Derived_Type : Entity_Id)
6612 is
6613 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6614
6615 Desig_Type : Entity_Id;
6616 Discr : Entity_Id;
6617 Discr_Con_Elist : Elist_Id;
6618 Discr_Con_El : Elmt_Id;
6619 Subt : Entity_Id;
6620
6621 begin
6622 -- Set the designated type so it is available in case this is an access
6623 -- to a self-referential type, e.g. a standard list type with a next
6624 -- pointer. Will be reset after subtype is built.
6625
6626 Set_Directly_Designated_Type
6627 (Derived_Type, Designated_Type (Parent_Type));
6628
6629 Subt := Process_Subtype (S, N);
6630
6631 if Nkind (S) /= N_Subtype_Indication
6632 and then Subt /= Base_Type (Subt)
6633 then
6634 Set_Ekind (Derived_Type, E_Access_Subtype);
6635 end if;
6636
6637 if Ekind (Derived_Type) = E_Access_Subtype then
6638 declare
6639 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6640 Ibase : constant Entity_Id :=
6641 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6642 Svg_Chars : constant Name_Id := Chars (Ibase);
6643 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6644 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6645
6646 begin
6647 Copy_Node (Pbase, Ibase);
6648
6649 -- Restore Itype status after Copy_Node
6650
6651 Set_Is_Itype (Ibase);
6652 Set_Associated_Node_For_Itype (Ibase, N);
6653
6654 Set_Chars (Ibase, Svg_Chars);
6655 Set_Prev_Entity (Ibase, Svg_Prev_E);
6656 Set_Next_Entity (Ibase, Svg_Next_E);
6657 Set_Sloc (Ibase, Sloc (Derived_Type));
6658 Set_Scope (Ibase, Scope (Derived_Type));
6659 Set_Freeze_Node (Ibase, Empty);
6660 Set_Is_Frozen (Ibase, False);
6661 Set_Comes_From_Source (Ibase, False);
6662 Set_Is_First_Subtype (Ibase, False);
6663
6664 Set_Etype (Ibase, Pbase);
6665 Set_Etype (Derived_Type, Ibase);
6666 end;
6667 end if;
6668
6669 Set_Directly_Designated_Type
6670 (Derived_Type, Designated_Type (Subt));
6671
6672 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6673 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6674 Set_Size_Info (Derived_Type, Parent_Type);
6675 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6676 Set_Depends_On_Private (Derived_Type,
6677 Has_Private_Component (Derived_Type));
6678 Conditional_Delay (Derived_Type, Subt);
6679
6680 if Is_Access_Subprogram_Type (Derived_Type) then
6681 Set_Can_Use_Internal_Rep
6682 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6683 end if;
6684
6685 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6686 -- that it is not redundant.
6687
6688 if Null_Exclusion_Present (Type_Definition (N)) then
6689 Set_Can_Never_Be_Null (Derived_Type);
6690
6691 elsif Can_Never_Be_Null (Parent_Type) then
6692 Set_Can_Never_Be_Null (Derived_Type);
6693 end if;
6694
6695 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6696 -- the root type for this information.
6697
6698 -- Apply range checks to discriminants for derived record case
6699 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6700
6701 Desig_Type := Designated_Type (Derived_Type);
6702
6703 if Is_Composite_Type (Desig_Type)
6704 and then (not Is_Array_Type (Desig_Type))
6705 and then Has_Discriminants (Desig_Type)
6706 and then Base_Type (Desig_Type) /= Desig_Type
6707 then
6708 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6709 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6710
6711 Discr := First_Discriminant (Base_Type (Desig_Type));
6712 while Present (Discr_Con_El) loop
6713 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6714 Next_Elmt (Discr_Con_El);
6715 Next_Discriminant (Discr);
6716 end loop;
6717 end if;
6718 end Build_Derived_Access_Type;
6719
6720 ------------------------------
6721 -- Build_Derived_Array_Type --
6722 ------------------------------
6723
6724 procedure Build_Derived_Array_Type
6725 (N : Node_Id;
6726 Parent_Type : Entity_Id;
6727 Derived_Type : Entity_Id)
6728 is
6729 Loc : constant Source_Ptr := Sloc (N);
6730 Tdef : constant Node_Id := Type_Definition (N);
6731 Indic : constant Node_Id := Subtype_Indication (Tdef);
6732 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6733 Implicit_Base : Entity_Id := Empty;
6734 New_Indic : Node_Id;
6735
6736 procedure Make_Implicit_Base;
6737 -- If the parent subtype is constrained, the derived type is a subtype
6738 -- of an implicit base type derived from the parent base.
6739
6740 ------------------------
6741 -- Make_Implicit_Base --
6742 ------------------------
6743
6744 procedure Make_Implicit_Base is
6745 begin
6746 Implicit_Base :=
6747 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6748
6749 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6750 Set_Etype (Implicit_Base, Parent_Base);
6751
6752 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6753 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6754
6755 Set_Has_Delayed_Freeze (Implicit_Base, True);
6756 end Make_Implicit_Base;
6757
6758 -- Start of processing for Build_Derived_Array_Type
6759
6760 begin
6761 if not Is_Constrained (Parent_Type) then
6762 if Nkind (Indic) /= N_Subtype_Indication then
6763 Set_Ekind (Derived_Type, E_Array_Type);
6764
6765 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6766 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6767
6768 Set_Has_Delayed_Freeze (Derived_Type, True);
6769
6770 else
6771 Make_Implicit_Base;
6772 Set_Etype (Derived_Type, Implicit_Base);
6773
6774 New_Indic :=
6775 Make_Subtype_Declaration (Loc,
6776 Defining_Identifier => Derived_Type,
6777 Subtype_Indication =>
6778 Make_Subtype_Indication (Loc,
6779 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6780 Constraint => Constraint (Indic)));
6781
6782 Rewrite (N, New_Indic);
6783 Analyze (N);
6784 end if;
6785
6786 else
6787 if Nkind (Indic) /= N_Subtype_Indication then
6788 Make_Implicit_Base;
6789
6790 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6791 Set_Etype (Derived_Type, Implicit_Base);
6792 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6793
6794 else
6795 Error_Msg_N ("illegal constraint on constrained type", Indic);
6796 end if;
6797 end if;
6798
6799 -- If parent type is not a derived type itself, and is declared in
6800 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6801 -- the new type's concatenation operator since Derive_Subprograms
6802 -- will not inherit the parent's operator. If the parent type is
6803 -- unconstrained, the operator is of the unconstrained base type.
6804
6805 if Number_Dimensions (Parent_Type) = 1
6806 and then not Is_Limited_Type (Parent_Type)
6807 and then not Is_Derived_Type (Parent_Type)
6808 and then not Is_Package_Or_Generic_Package
6809 (Scope (Base_Type (Parent_Type)))
6810 then
6811 if not Is_Constrained (Parent_Type)
6812 and then Is_Constrained (Derived_Type)
6813 then
6814 New_Concatenation_Op (Implicit_Base);
6815 else
6816 New_Concatenation_Op (Derived_Type);
6817 end if;
6818 end if;
6819 end Build_Derived_Array_Type;
6820
6821 -----------------------------------
6822 -- Build_Derived_Concurrent_Type --
6823 -----------------------------------
6824
6825 procedure Build_Derived_Concurrent_Type
6826 (N : Node_Id;
6827 Parent_Type : Entity_Id;
6828 Derived_Type : Entity_Id)
6829 is
6830 Loc : constant Source_Ptr := Sloc (N);
6831 Def : constant Node_Id := Type_Definition (N);
6832 Indic : constant Node_Id := Subtype_Indication (Def);
6833
6834 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6835 Corr_Decl : Node_Id;
6836 Corr_Decl_Needed : Boolean;
6837 -- If the derived type has fewer discriminants than its parent, the
6838 -- corresponding record is also a derived type, in order to account for
6839 -- the bound discriminants. We create a full type declaration for it in
6840 -- this case.
6841
6842 Constraint_Present : constant Boolean :=
6843 Nkind (Indic) = N_Subtype_Indication;
6844
6845 D_Constraint : Node_Id;
6846 New_Constraint : Elist_Id := No_Elist;
6847 Old_Disc : Entity_Id;
6848 New_Disc : Entity_Id;
6849 New_N : Node_Id;
6850
6851 begin
6852 Set_Stored_Constraint (Derived_Type, No_Elist);
6853 Corr_Decl_Needed := False;
6854 Old_Disc := Empty;
6855
6856 if Present (Discriminant_Specifications (N))
6857 and then Constraint_Present
6858 then
6859 Old_Disc := First_Discriminant (Parent_Type);
6860 New_Disc := First (Discriminant_Specifications (N));
6861 while Present (New_Disc) and then Present (Old_Disc) loop
6862 Next_Discriminant (Old_Disc);
6863 Next (New_Disc);
6864 end loop;
6865 end if;
6866
6867 if Present (Old_Disc) and then Expander_Active then
6868
6869 -- The new type has fewer discriminants, so we need to create a new
6870 -- corresponding record, which is derived from the corresponding
6871 -- record of the parent, and has a stored constraint that captures
6872 -- the values of the discriminant constraints. The corresponding
6873 -- record is needed only if expander is active and code generation is
6874 -- enabled.
6875
6876 -- The type declaration for the derived corresponding record has the
6877 -- same discriminant part and constraints as the current declaration.
6878 -- Copy the unanalyzed tree to build declaration.
6879
6880 Corr_Decl_Needed := True;
6881 New_N := Copy_Separate_Tree (N);
6882
6883 Corr_Decl :=
6884 Make_Full_Type_Declaration (Loc,
6885 Defining_Identifier => Corr_Record,
6886 Discriminant_Specifications =>
6887 Discriminant_Specifications (New_N),
6888 Type_Definition =>
6889 Make_Derived_Type_Definition (Loc,
6890 Subtype_Indication =>
6891 Make_Subtype_Indication (Loc,
6892 Subtype_Mark =>
6893 New_Occurrence_Of
6894 (Corresponding_Record_Type (Parent_Type), Loc),
6895 Constraint =>
6896 Constraint
6897 (Subtype_Indication (Type_Definition (New_N))))));
6898 end if;
6899
6900 -- Copy Storage_Size and Relative_Deadline variables if task case
6901
6902 if Is_Task_Type (Parent_Type) then
6903 Set_Storage_Size_Variable (Derived_Type,
6904 Storage_Size_Variable (Parent_Type));
6905 Set_Relative_Deadline_Variable (Derived_Type,
6906 Relative_Deadline_Variable (Parent_Type));
6907 end if;
6908
6909 if Present (Discriminant_Specifications (N)) then
6910 Push_Scope (Derived_Type);
6911 Check_Or_Process_Discriminants (N, Derived_Type);
6912
6913 if Constraint_Present then
6914 New_Constraint :=
6915 Expand_To_Stored_Constraint
6916 (Parent_Type,
6917 Build_Discriminant_Constraints
6918 (Parent_Type, Indic, True));
6919 end if;
6920
6921 End_Scope;
6922
6923 elsif Constraint_Present then
6924
6925 -- Build an unconstrained derived type and rewrite the derived type
6926 -- as a subtype of this new base type.
6927
6928 declare
6929 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6930 New_Base : Entity_Id;
6931 New_Decl : Node_Id;
6932 New_Indic : Node_Id;
6933
6934 begin
6935 New_Base :=
6936 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6937
6938 New_Decl :=
6939 Make_Full_Type_Declaration (Loc,
6940 Defining_Identifier => New_Base,
6941 Type_Definition =>
6942 Make_Derived_Type_Definition (Loc,
6943 Abstract_Present => Abstract_Present (Def),
6944 Limited_Present => Limited_Present (Def),
6945 Subtype_Indication =>
6946 New_Occurrence_Of (Parent_Base, Loc)));
6947
6948 Mark_Rewrite_Insertion (New_Decl);
6949 Insert_Before (N, New_Decl);
6950 Analyze (New_Decl);
6951
6952 New_Indic :=
6953 Make_Subtype_Indication (Loc,
6954 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6955 Constraint => Relocate_Node (Constraint (Indic)));
6956
6957 Rewrite (N,
6958 Make_Subtype_Declaration (Loc,
6959 Defining_Identifier => Derived_Type,
6960 Subtype_Indication => New_Indic));
6961
6962 Analyze (N);
6963 return;
6964 end;
6965 end if;
6966
6967 -- By default, operations and private data are inherited from parent.
6968 -- However, in the presence of bound discriminants, a new corresponding
6969 -- record will be created, see below.
6970
6971 Set_Has_Discriminants
6972 (Derived_Type, Has_Discriminants (Parent_Type));
6973 Set_Corresponding_Record_Type
6974 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6975
6976 -- Is_Constrained is set according the parent subtype, but is set to
6977 -- False if the derived type is declared with new discriminants.
6978
6979 Set_Is_Constrained
6980 (Derived_Type,
6981 (Is_Constrained (Parent_Type) or else Constraint_Present)
6982 and then not Present (Discriminant_Specifications (N)));
6983
6984 if Constraint_Present then
6985 if not Has_Discriminants (Parent_Type) then
6986 Error_Msg_N ("untagged parent must have discriminants", N);
6987
6988 elsif Present (Discriminant_Specifications (N)) then
6989
6990 -- Verify that new discriminants are used to constrain old ones
6991
6992 D_Constraint := First (Constraints (Constraint (Indic)));
6993
6994 Old_Disc := First_Discriminant (Parent_Type);
6995
6996 while Present (D_Constraint) loop
6997 if Nkind (D_Constraint) /= N_Discriminant_Association then
6998
6999 -- Positional constraint. If it is a reference to a new
7000 -- discriminant, it constrains the corresponding old one.
7001
7002 if Nkind (D_Constraint) = N_Identifier then
7003 New_Disc := First_Discriminant (Derived_Type);
7004 while Present (New_Disc) loop
7005 exit when Chars (New_Disc) = Chars (D_Constraint);
7006 Next_Discriminant (New_Disc);
7007 end loop;
7008
7009 if Present (New_Disc) then
7010 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7011 end if;
7012 end if;
7013
7014 Next_Discriminant (Old_Disc);
7015
7016 -- if this is a named constraint, search by name for the old
7017 -- discriminants constrained by the new one.
7018
7019 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7020
7021 -- Find new discriminant with that name
7022
7023 New_Disc := First_Discriminant (Derived_Type);
7024 while Present (New_Disc) loop
7025 exit when
7026 Chars (New_Disc) = Chars (Expression (D_Constraint));
7027 Next_Discriminant (New_Disc);
7028 end loop;
7029
7030 if Present (New_Disc) then
7031
7032 -- Verify that new discriminant renames some discriminant
7033 -- of the parent type, and associate the new discriminant
7034 -- with one or more old ones that it renames.
7035
7036 declare
7037 Selector : Node_Id;
7038
7039 begin
7040 Selector := First (Selector_Names (D_Constraint));
7041 while Present (Selector) loop
7042 Old_Disc := First_Discriminant (Parent_Type);
7043 while Present (Old_Disc) loop
7044 exit when Chars (Old_Disc) = Chars (Selector);
7045 Next_Discriminant (Old_Disc);
7046 end loop;
7047
7048 if Present (Old_Disc) then
7049 Set_Corresponding_Discriminant
7050 (New_Disc, Old_Disc);
7051 end if;
7052
7053 Next (Selector);
7054 end loop;
7055 end;
7056 end if;
7057 end if;
7058
7059 Next (D_Constraint);
7060 end loop;
7061
7062 New_Disc := First_Discriminant (Derived_Type);
7063 while Present (New_Disc) loop
7064 if No (Corresponding_Discriminant (New_Disc)) then
7065 Error_Msg_NE
7066 ("new discriminant& must constrain old one", N, New_Disc);
7067
7068 -- If a new discriminant is used in the constraint, then its
7069 -- subtype must be statically compatible with the subtype of
7070 -- the parent discriminant (RM 3.7(15)).
7071
7072 else
7073 Check_Constraining_Discriminant
7074 (New_Disc, Corresponding_Discriminant (New_Disc));
7075 end if;
7076
7077 Next_Discriminant (New_Disc);
7078 end loop;
7079 end if;
7080
7081 elsif Present (Discriminant_Specifications (N)) then
7082 Error_Msg_N
7083 ("missing discriminant constraint in untagged derivation", N);
7084 end if;
7085
7086 -- The entity chain of the derived type includes the new discriminants
7087 -- but shares operations with the parent.
7088
7089 if Present (Discriminant_Specifications (N)) then
7090 Old_Disc := First_Discriminant (Parent_Type);
7091 while Present (Old_Disc) loop
7092 if No (Next_Entity (Old_Disc))
7093 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7094 then
7095 Link_Entities
7096 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7097 exit;
7098 end if;
7099
7100 Next_Discriminant (Old_Disc);
7101 end loop;
7102
7103 else
7104 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7105 if Has_Discriminants (Parent_Type) then
7106 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7107 Set_Discriminant_Constraint (
7108 Derived_Type, Discriminant_Constraint (Parent_Type));
7109 end if;
7110 end if;
7111
7112 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7113
7114 Set_Has_Completion (Derived_Type);
7115
7116 if Corr_Decl_Needed then
7117 Set_Stored_Constraint (Derived_Type, New_Constraint);
7118 Insert_After (N, Corr_Decl);
7119 Analyze (Corr_Decl);
7120 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7121 end if;
7122 end Build_Derived_Concurrent_Type;
7123
7124 ------------------------------------
7125 -- Build_Derived_Enumeration_Type --
7126 ------------------------------------
7127
7128 procedure Build_Derived_Enumeration_Type
7129 (N : Node_Id;
7130 Parent_Type : Entity_Id;
7131 Derived_Type : Entity_Id)
7132 is
7133 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7134 -- When the type declaration includes a constraint, we generate
7135 -- a subtype declaration of an anonymous base type, with the constraint
7136 -- given in the original type declaration. Conceptually, the bounds
7137 -- are converted to the new base type, and this conversion freezes
7138 -- (prematurely) that base type, when the bounds are simply literals.
7139 -- As a result, a representation clause for the derived type is then
7140 -- rejected or ignored. This procedure recognizes the simple case of
7141 -- literal bounds, which allows us to indicate that the conversions
7142 -- are not freeze points, and the subsequent representation clause
7143 -- can be accepted.
7144 -- A similar approach might be used to resolve the long-standing
7145 -- problem of premature freezing of derived numeric types ???
7146
7147 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7148 begin
7149 return Nkind (B) = N_Type_Conversion
7150 and then Is_Entity_Name (Expression (B))
7151 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7152 end Bound_Belongs_To_Type;
7153
7154 Loc : constant Source_Ptr := Sloc (N);
7155 Def : constant Node_Id := Type_Definition (N);
7156 Indic : constant Node_Id := Subtype_Indication (Def);
7157 Implicit_Base : Entity_Id;
7158 Literal : Entity_Id;
7159 New_Lit : Entity_Id;
7160 Literals_List : List_Id;
7161 Type_Decl : Node_Id;
7162 Hi, Lo : Node_Id;
7163 Rang_Expr : Node_Id;
7164
7165 begin
7166 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7167 -- not have explicit literals lists we need to process types derived
7168 -- from them specially. This is handled by Derived_Standard_Character.
7169 -- If the parent type is a generic type, there are no literals either,
7170 -- and we construct the same skeletal representation as for the generic
7171 -- parent type.
7172
7173 if Is_Standard_Character_Type (Parent_Type) then
7174 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7175
7176 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7177 declare
7178 Lo : Node_Id;
7179 Hi : Node_Id;
7180
7181 begin
7182 if Nkind (Indic) /= N_Subtype_Indication then
7183 Lo :=
7184 Make_Attribute_Reference (Loc,
7185 Attribute_Name => Name_First,
7186 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7187 Set_Etype (Lo, Derived_Type);
7188
7189 Hi :=
7190 Make_Attribute_Reference (Loc,
7191 Attribute_Name => Name_Last,
7192 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7193 Set_Etype (Hi, Derived_Type);
7194
7195 Set_Scalar_Range (Derived_Type,
7196 Make_Range (Loc,
7197 Low_Bound => Lo,
7198 High_Bound => Hi));
7199 else
7200
7201 -- Analyze subtype indication and verify compatibility
7202 -- with parent type.
7203
7204 if Base_Type (Process_Subtype (Indic, N)) /=
7205 Base_Type (Parent_Type)
7206 then
7207 Error_Msg_N
7208 ("illegal constraint for formal discrete type", N);
7209 end if;
7210 end if;
7211 end;
7212
7213 else
7214 -- If a constraint is present, analyze the bounds to catch
7215 -- premature usage of the derived literals.
7216
7217 if Nkind (Indic) = N_Subtype_Indication
7218 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7219 then
7220 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7221 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7222 end if;
7223
7224 -- Introduce an implicit base type for the derived type even if there
7225 -- is no constraint attached to it, since this seems closer to the
7226 -- Ada semantics. Build a full type declaration tree for the derived
7227 -- type using the implicit base type as the defining identifier. The
7228 -- build a subtype declaration tree which applies the constraint (if
7229 -- any) have it replace the derived type declaration.
7230
7231 Literal := First_Literal (Parent_Type);
7232 Literals_List := New_List;
7233 while Present (Literal)
7234 and then Ekind (Literal) = E_Enumeration_Literal
7235 loop
7236 -- Literals of the derived type have the same representation as
7237 -- those of the parent type, but this representation can be
7238 -- overridden by an explicit representation clause. Indicate
7239 -- that there is no explicit representation given yet. These
7240 -- derived literals are implicit operations of the new type,
7241 -- and can be overridden by explicit ones.
7242
7243 if Nkind (Literal) = N_Defining_Character_Literal then
7244 New_Lit :=
7245 Make_Defining_Character_Literal (Loc, Chars (Literal));
7246 else
7247 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7248 end if;
7249
7250 Set_Ekind (New_Lit, E_Enumeration_Literal);
7251 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7252 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7253 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7254 Set_Alias (New_Lit, Literal);
7255 Set_Is_Known_Valid (New_Lit, True);
7256
7257 Append (New_Lit, Literals_List);
7258 Next_Literal (Literal);
7259 end loop;
7260
7261 Implicit_Base :=
7262 Make_Defining_Identifier (Sloc (Derived_Type),
7263 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7264
7265 -- Indicate the proper nature of the derived type. This must be done
7266 -- before analysis of the literals, to recognize cases when a literal
7267 -- may be hidden by a previous explicit function definition (cf.
7268 -- c83031a).
7269
7270 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7271 Set_Etype (Derived_Type, Implicit_Base);
7272
7273 Type_Decl :=
7274 Make_Full_Type_Declaration (Loc,
7275 Defining_Identifier => Implicit_Base,
7276 Discriminant_Specifications => No_List,
7277 Type_Definition =>
7278 Make_Enumeration_Type_Definition (Loc, Literals_List));
7279
7280 Mark_Rewrite_Insertion (Type_Decl);
7281 Insert_Before (N, Type_Decl);
7282 Analyze (Type_Decl);
7283
7284 -- The anonymous base now has a full declaration, but this base
7285 -- is not a first subtype.
7286
7287 Set_Is_First_Subtype (Implicit_Base, False);
7288
7289 -- After the implicit base is analyzed its Etype needs to be changed
7290 -- to reflect the fact that it is derived from the parent type which
7291 -- was ignored during analysis. We also set the size at this point.
7292
7293 Set_Etype (Implicit_Base, Parent_Type);
7294
7295 Set_Size_Info (Implicit_Base, Parent_Type);
7296 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7297 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7298
7299 -- Copy other flags from parent type
7300
7301 Set_Has_Non_Standard_Rep
7302 (Implicit_Base, Has_Non_Standard_Rep
7303 (Parent_Type));
7304 Set_Has_Pragma_Ordered
7305 (Implicit_Base, Has_Pragma_Ordered
7306 (Parent_Type));
7307 Set_Has_Delayed_Freeze (Implicit_Base);
7308
7309 -- Process the subtype indication including a validation check on the
7310 -- constraint, if any. If a constraint is given, its bounds must be
7311 -- implicitly converted to the new type.
7312
7313 if Nkind (Indic) = N_Subtype_Indication then
7314 declare
7315 R : constant Node_Id :=
7316 Range_Expression (Constraint (Indic));
7317
7318 begin
7319 if Nkind (R) = N_Range then
7320 Hi := Build_Scalar_Bound
7321 (High_Bound (R), Parent_Type, Implicit_Base);
7322 Lo := Build_Scalar_Bound
7323 (Low_Bound (R), Parent_Type, Implicit_Base);
7324
7325 else
7326 -- Constraint is a Range attribute. Replace with explicit
7327 -- mention of the bounds of the prefix, which must be a
7328 -- subtype.
7329
7330 Analyze (Prefix (R));
7331 Hi :=
7332 Convert_To (Implicit_Base,
7333 Make_Attribute_Reference (Loc,
7334 Attribute_Name => Name_Last,
7335 Prefix =>
7336 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7337
7338 Lo :=
7339 Convert_To (Implicit_Base,
7340 Make_Attribute_Reference (Loc,
7341 Attribute_Name => Name_First,
7342 Prefix =>
7343 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7344 end if;
7345 end;
7346
7347 else
7348 Hi :=
7349 Build_Scalar_Bound
7350 (Type_High_Bound (Parent_Type),
7351 Parent_Type, Implicit_Base);
7352 Lo :=
7353 Build_Scalar_Bound
7354 (Type_Low_Bound (Parent_Type),
7355 Parent_Type, Implicit_Base);
7356 end if;
7357
7358 Rang_Expr :=
7359 Make_Range (Loc,
7360 Low_Bound => Lo,
7361 High_Bound => Hi);
7362
7363 -- If we constructed a default range for the case where no range
7364 -- was given, then the expressions in the range must not freeze
7365 -- since they do not correspond to expressions in the source.
7366 -- However, if the type inherits predicates the expressions will
7367 -- be elaborated earlier and must freeze.
7368
7369 if (Nkind (Indic) /= N_Subtype_Indication
7370 or else
7371 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7372 and then not Has_Predicates (Derived_Type)
7373 then
7374 Set_Must_Not_Freeze (Lo);
7375 Set_Must_Not_Freeze (Hi);
7376 Set_Must_Not_Freeze (Rang_Expr);
7377 end if;
7378
7379 Rewrite (N,
7380 Make_Subtype_Declaration (Loc,
7381 Defining_Identifier => Derived_Type,
7382 Subtype_Indication =>
7383 Make_Subtype_Indication (Loc,
7384 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7385 Constraint =>
7386 Make_Range_Constraint (Loc,
7387 Range_Expression => Rang_Expr))));
7388
7389 Analyze (N);
7390
7391 -- Propagate the aspects from the original type declaration to the
7392 -- declaration of the implicit base.
7393
7394 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7395
7396 -- Apply a range check. Since this range expression doesn't have an
7397 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7398 -- this right???
7399
7400 if Nkind (Indic) = N_Subtype_Indication then
7401 Apply_Range_Check
7402 (Range_Expression (Constraint (Indic)), Parent_Type,
7403 Source_Typ => Entity (Subtype_Mark (Indic)));
7404 end if;
7405 end if;
7406 end Build_Derived_Enumeration_Type;
7407
7408 --------------------------------
7409 -- Build_Derived_Numeric_Type --
7410 --------------------------------
7411
7412 procedure Build_Derived_Numeric_Type
7413 (N : Node_Id;
7414 Parent_Type : Entity_Id;
7415 Derived_Type : Entity_Id)
7416 is
7417 Loc : constant Source_Ptr := Sloc (N);
7418 Tdef : constant Node_Id := Type_Definition (N);
7419 Indic : constant Node_Id := Subtype_Indication (Tdef);
7420 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7421 No_Constraint : constant Boolean := Nkind (Indic) /=
7422 N_Subtype_Indication;
7423 Implicit_Base : Entity_Id;
7424
7425 Lo : Node_Id;
7426 Hi : Node_Id;
7427
7428 begin
7429 -- Process the subtype indication including a validation check on
7430 -- the constraint if any.
7431
7432 Discard_Node (Process_Subtype (Indic, N));
7433
7434 -- Introduce an implicit base type for the derived type even if there
7435 -- is no constraint attached to it, since this seems closer to the Ada
7436 -- semantics.
7437
7438 Implicit_Base :=
7439 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7440
7441 Set_Etype (Implicit_Base, Parent_Base);
7442 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7443 Set_Size_Info (Implicit_Base, Parent_Base);
7444 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7445 Set_Parent (Implicit_Base, Parent (Derived_Type));
7446 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7447 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7448
7449 -- Set RM Size for discrete type or decimal fixed-point type
7450 -- Ordinary fixed-point is excluded, why???
7451
7452 if Is_Discrete_Type (Parent_Base)
7453 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7454 then
7455 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7456 end if;
7457
7458 Set_Has_Delayed_Freeze (Implicit_Base);
7459
7460 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7461 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7462
7463 Set_Scalar_Range (Implicit_Base,
7464 Make_Range (Loc,
7465 Low_Bound => Lo,
7466 High_Bound => Hi));
7467
7468 if Has_Infinities (Parent_Base) then
7469 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7470 end if;
7471
7472 -- The Derived_Type, which is the entity of the declaration, is a
7473 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7474 -- absence of an explicit constraint.
7475
7476 Set_Etype (Derived_Type, Implicit_Base);
7477
7478 -- If we did not have a constraint, then the Ekind is set from the
7479 -- parent type (otherwise Process_Subtype has set the bounds)
7480
7481 if No_Constraint then
7482 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7483 end if;
7484
7485 -- If we did not have a range constraint, then set the range from the
7486 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7487
7488 if No_Constraint or else not Has_Range_Constraint (Indic) then
7489 Set_Scalar_Range (Derived_Type,
7490 Make_Range (Loc,
7491 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7492 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7493 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7494
7495 if Has_Infinities (Parent_Type) then
7496 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7497 end if;
7498
7499 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7500 end if;
7501
7502 Set_Is_Descendant_Of_Address (Derived_Type,
7503 Is_Descendant_Of_Address (Parent_Type));
7504 Set_Is_Descendant_Of_Address (Implicit_Base,
7505 Is_Descendant_Of_Address (Parent_Type));
7506
7507 -- Set remaining type-specific fields, depending on numeric type
7508
7509 if Is_Modular_Integer_Type (Parent_Type) then
7510 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7511
7512 Set_Non_Binary_Modulus
7513 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7514
7515 Set_Is_Known_Valid
7516 (Implicit_Base, Is_Known_Valid (Parent_Base));
7517
7518 elsif Is_Floating_Point_Type (Parent_Type) then
7519
7520 -- Digits of base type is always copied from the digits value of
7521 -- the parent base type, but the digits of the derived type will
7522 -- already have been set if there was a constraint present.
7523
7524 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7525 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7526
7527 if No_Constraint then
7528 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7529 end if;
7530
7531 elsif Is_Fixed_Point_Type (Parent_Type) then
7532
7533 -- Small of base type and derived type are always copied from the
7534 -- parent base type, since smalls never change. The delta of the
7535 -- base type is also copied from the parent base type. However the
7536 -- delta of the derived type will have been set already if a
7537 -- constraint was present.
7538
7539 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7540 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7541 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7542
7543 if No_Constraint then
7544 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7545 end if;
7546
7547 -- The scale and machine radix in the decimal case are always
7548 -- copied from the parent base type.
7549
7550 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7551 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7552 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7553
7554 Set_Machine_Radix_10
7555 (Derived_Type, Machine_Radix_10 (Parent_Base));
7556 Set_Machine_Radix_10
7557 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7558
7559 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7560
7561 if No_Constraint then
7562 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7563
7564 else
7565 -- the analysis of the subtype_indication sets the
7566 -- digits value of the derived type.
7567
7568 null;
7569 end if;
7570 end if;
7571 end if;
7572
7573 if Is_Integer_Type (Parent_Type) then
7574 Set_Has_Shift_Operator
7575 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7576 end if;
7577
7578 -- The type of the bounds is that of the parent type, and they
7579 -- must be converted to the derived type.
7580
7581 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7582
7583 -- The implicit_base should be frozen when the derived type is frozen,
7584 -- but note that it is used in the conversions of the bounds. For fixed
7585 -- types we delay the determination of the bounds until the proper
7586 -- freezing point. For other numeric types this is rejected by GCC, for
7587 -- reasons that are currently unclear (???), so we choose to freeze the
7588 -- implicit base now. In the case of integers and floating point types
7589 -- this is harmless because subsequent representation clauses cannot
7590 -- affect anything, but it is still baffling that we cannot use the
7591 -- same mechanism for all derived numeric types.
7592
7593 -- There is a further complication: actually some representation
7594 -- clauses can affect the implicit base type. For example, attribute
7595 -- definition clauses for stream-oriented attributes need to set the
7596 -- corresponding TSS entries on the base type, and this normally
7597 -- cannot be done after the base type is frozen, so the circuitry in
7598 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7599 -- and not use Set_TSS in this case.
7600
7601 -- There are also consequences for the case of delayed representation
7602 -- aspects for some cases. For example, a Size aspect is delayed and
7603 -- should not be evaluated to the freeze point. This early freezing
7604 -- means that the size attribute evaluation happens too early???
7605
7606 if Is_Fixed_Point_Type (Parent_Type) then
7607 Conditional_Delay (Implicit_Base, Parent_Type);
7608 else
7609 Freeze_Before (N, Implicit_Base);
7610 end if;
7611 end Build_Derived_Numeric_Type;
7612
7613 --------------------------------
7614 -- Build_Derived_Private_Type --
7615 --------------------------------
7616
7617 procedure Build_Derived_Private_Type
7618 (N : Node_Id;
7619 Parent_Type : Entity_Id;
7620 Derived_Type : Entity_Id;
7621 Is_Completion : Boolean;
7622 Derive_Subps : Boolean := True)
7623 is
7624 Loc : constant Source_Ptr := Sloc (N);
7625 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7626 Par_Scope : constant Entity_Id := Scope (Par_Base);
7627 Full_N : constant Node_Id := New_Copy_Tree (N);
7628 Full_Der : Entity_Id := New_Copy (Derived_Type);
7629 Full_P : Entity_Id;
7630
7631 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7632 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7633 -- present (they cannot be both present for the same type), or Empty.
7634
7635 procedure Build_Full_Derivation;
7636 -- Build full derivation, i.e. derive from the full view
7637
7638 procedure Copy_And_Build;
7639 -- Copy derived type declaration, replace parent with its full view,
7640 -- and build derivation
7641
7642 -------------------------
7643 -- Available_Full_View --
7644 -------------------------
7645
7646 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7647 begin
7648 if Present (Full_View (Typ)) then
7649 return Full_View (Typ);
7650
7651 elsif Present (Underlying_Full_View (Typ)) then
7652
7653 -- We should be called on a type with an underlying full view
7654 -- only by means of the recursive call made in Copy_And_Build
7655 -- through the first call to Build_Derived_Type, or else if
7656 -- the parent scope is being analyzed because we are deriving
7657 -- a completion.
7658
7659 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7660
7661 return Underlying_Full_View (Typ);
7662
7663 else
7664 return Empty;
7665 end if;
7666 end Available_Full_View;
7667
7668 ---------------------------
7669 -- Build_Full_Derivation --
7670 ---------------------------
7671
7672 procedure Build_Full_Derivation is
7673 begin
7674 -- If parent scope is not open, install the declarations
7675
7676 if not In_Open_Scopes (Par_Scope) then
7677 Install_Private_Declarations (Par_Scope);
7678 Install_Visible_Declarations (Par_Scope);
7679 Copy_And_Build;
7680 Uninstall_Declarations (Par_Scope);
7681
7682 -- If parent scope is open and in another unit, and parent has a
7683 -- completion, then the derivation is taking place in the visible
7684 -- part of a child unit. In that case retrieve the full view of
7685 -- the parent momentarily.
7686
7687 elsif not In_Same_Source_Unit (N, Parent_Type)
7688 and then Present (Full_View (Parent_Type))
7689 then
7690 Full_P := Full_View (Parent_Type);
7691 Exchange_Declarations (Parent_Type);
7692 Copy_And_Build;
7693 Exchange_Declarations (Full_P);
7694
7695 -- Otherwise it is a local derivation
7696
7697 else
7698 Copy_And_Build;
7699 end if;
7700 end Build_Full_Derivation;
7701
7702 --------------------
7703 -- Copy_And_Build --
7704 --------------------
7705
7706 procedure Copy_And_Build is
7707 Full_Parent : Entity_Id := Parent_Type;
7708
7709 begin
7710 -- If the parent is itself derived from another private type,
7711 -- installing the private declarations has not affected its
7712 -- privacy status, so use its own full view explicitly.
7713
7714 if Is_Private_Type (Full_Parent)
7715 and then Present (Full_View (Full_Parent))
7716 then
7717 Full_Parent := Full_View (Full_Parent);
7718 end if;
7719
7720 -- If the full view is itself derived from another private type
7721 -- and has got an underlying full view, and this is done for a
7722 -- completion, i.e. to build the underlying full view of the type,
7723 -- then use this underlying full view. We cannot do that if this
7724 -- is not a completion, i.e. to build the full view of the type,
7725 -- because this would break the privacy of the parent type, except
7726 -- if the parent scope is being analyzed because we are deriving a
7727 -- completion.
7728
7729 if Is_Private_Type (Full_Parent)
7730 and then Present (Underlying_Full_View (Full_Parent))
7731 and then (Is_Completion or else In_Private_Part (Par_Scope))
7732 then
7733 Full_Parent := Underlying_Full_View (Full_Parent);
7734 end if;
7735
7736 -- For private, record, concurrent, access and almost all enumeration
7737 -- types, the derivation from the full view requires a fully-fledged
7738 -- declaration. In the other cases, just use an itype.
7739
7740 if Is_Private_Type (Full_Parent)
7741 or else Is_Record_Type (Full_Parent)
7742 or else Is_Concurrent_Type (Full_Parent)
7743 or else Is_Access_Type (Full_Parent)
7744 or else
7745 (Is_Enumeration_Type (Full_Parent)
7746 and then not Is_Standard_Character_Type (Full_Parent)
7747 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7748 then
7749 -- Copy and adjust declaration to provide a completion for what
7750 -- is originally a private declaration. Indicate that full view
7751 -- is internally generated.
7752
7753 Set_Comes_From_Source (Full_N, False);
7754 Set_Comes_From_Source (Full_Der, False);
7755 Set_Parent (Full_Der, Full_N);
7756 Set_Defining_Identifier (Full_N, Full_Der);
7757
7758 -- If there are no constraints, adjust the subtype mark
7759
7760 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7761 N_Subtype_Indication
7762 then
7763 Set_Subtype_Indication
7764 (Type_Definition (Full_N),
7765 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7766 end if;
7767
7768 Insert_After (N, Full_N);
7769
7770 -- Build full view of derived type from full view of parent which
7771 -- is now installed. Subprograms have been derived on the partial
7772 -- view, the completion does not derive them anew.
7773
7774 if Is_Record_Type (Full_Parent) then
7775
7776 -- If parent type is tagged, the completion inherits the proper
7777 -- primitive operations.
7778
7779 if Is_Tagged_Type (Parent_Type) then
7780 Build_Derived_Record_Type
7781 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7782 else
7783 Build_Derived_Record_Type
7784 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7785 end if;
7786
7787 else
7788 -- If the parent type is private, this is not a completion and
7789 -- we build the full derivation recursively as a completion.
7790
7791 Build_Derived_Type
7792 (Full_N, Full_Parent, Full_Der,
7793 Is_Completion => Is_Private_Type (Full_Parent),
7794 Derive_Subps => False);
7795 end if;
7796
7797 -- The full declaration has been introduced into the tree and
7798 -- processed in the step above. It should not be analyzed again
7799 -- (when encountered later in the current list of declarations)
7800 -- to prevent spurious name conflicts. The full entity remains
7801 -- invisible.
7802
7803 Set_Analyzed (Full_N);
7804
7805 else
7806 Full_Der :=
7807 Make_Defining_Identifier (Sloc (Derived_Type),
7808 Chars => Chars (Derived_Type));
7809 Set_Is_Itype (Full_Der);
7810 Set_Associated_Node_For_Itype (Full_Der, N);
7811 Set_Parent (Full_Der, N);
7812 Build_Derived_Type
7813 (N, Full_Parent, Full_Der,
7814 Is_Completion => False, Derive_Subps => False);
7815 end if;
7816
7817 Set_Has_Private_Declaration (Full_Der);
7818 Set_Has_Private_Declaration (Derived_Type);
7819
7820 Set_Scope (Full_Der, Scope (Derived_Type));
7821 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7822 Set_Has_Size_Clause (Full_Der, False);
7823 Set_Has_Alignment_Clause (Full_Der, False);
7824 Set_Has_Delayed_Freeze (Full_Der);
7825 Set_Is_Frozen (Full_Der, False);
7826 Set_Freeze_Node (Full_Der, Empty);
7827 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7828 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7829
7830 -- The convention on the base type may be set in the private part
7831 -- and not propagated to the subtype until later, so we obtain the
7832 -- convention from the base type of the parent.
7833
7834 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7835 end Copy_And_Build;
7836
7837 -- Start of processing for Build_Derived_Private_Type
7838
7839 begin
7840 if Is_Tagged_Type (Parent_Type) then
7841 Full_P := Full_View (Parent_Type);
7842
7843 -- A type extension of a type with unknown discriminants is an
7844 -- indefinite type that the back-end cannot handle directly.
7845 -- We treat it as a private type, and build a completion that is
7846 -- derived from the full view of the parent, and hopefully has
7847 -- known discriminants.
7848
7849 -- If the full view of the parent type has an underlying record view,
7850 -- use it to generate the underlying record view of this derived type
7851 -- (required for chains of derivations with unknown discriminants).
7852
7853 -- Minor optimization: we avoid the generation of useless underlying
7854 -- record view entities if the private type declaration has unknown
7855 -- discriminants but its corresponding full view has no
7856 -- discriminants.
7857
7858 if Has_Unknown_Discriminants (Parent_Type)
7859 and then Present (Full_P)
7860 and then (Has_Discriminants (Full_P)
7861 or else Present (Underlying_Record_View (Full_P)))
7862 and then not In_Open_Scopes (Par_Scope)
7863 and then Expander_Active
7864 then
7865 declare
7866 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7867 New_Ext : constant Node_Id :=
7868 Copy_Separate_Tree
7869 (Record_Extension_Part (Type_Definition (N)));
7870 Decl : Node_Id;
7871
7872 begin
7873 Build_Derived_Record_Type
7874 (N, Parent_Type, Derived_Type, Derive_Subps);
7875
7876 -- Build anonymous completion, as a derivation from the full
7877 -- view of the parent. This is not a completion in the usual
7878 -- sense, because the current type is not private.
7879
7880 Decl :=
7881 Make_Full_Type_Declaration (Loc,
7882 Defining_Identifier => Full_Der,
7883 Type_Definition =>
7884 Make_Derived_Type_Definition (Loc,
7885 Subtype_Indication =>
7886 New_Copy_Tree
7887 (Subtype_Indication (Type_Definition (N))),
7888 Record_Extension_Part => New_Ext));
7889
7890 -- If the parent type has an underlying record view, use it
7891 -- here to build the new underlying record view.
7892
7893 if Present (Underlying_Record_View (Full_P)) then
7894 pragma Assert
7895 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7896 = N_Identifier);
7897 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7898 Underlying_Record_View (Full_P));
7899 end if;
7900
7901 Install_Private_Declarations (Par_Scope);
7902 Install_Visible_Declarations (Par_Scope);
7903 Insert_Before (N, Decl);
7904
7905 -- Mark entity as an underlying record view before analysis,
7906 -- to avoid generating the list of its primitive operations
7907 -- (which is not really required for this entity) and thus
7908 -- prevent spurious errors associated with missing overriding
7909 -- of abstract primitives (overridden only for Derived_Type).
7910
7911 Set_Ekind (Full_Der, E_Record_Type);
7912 Set_Is_Underlying_Record_View (Full_Der);
7913 Set_Default_SSO (Full_Der);
7914 Set_No_Reordering (Full_Der, No_Component_Reordering);
7915
7916 Analyze (Decl);
7917
7918 pragma Assert (Has_Discriminants (Full_Der)
7919 and then not Has_Unknown_Discriminants (Full_Der));
7920
7921 Uninstall_Declarations (Par_Scope);
7922
7923 -- Freeze the underlying record view, to prevent generation of
7924 -- useless dispatching information, which is simply shared with
7925 -- the real derived type.
7926
7927 Set_Is_Frozen (Full_Der);
7928
7929 -- If the derived type has access discriminants, create
7930 -- references to their anonymous types now, to prevent
7931 -- back-end problems when their first use is in generated
7932 -- bodies of primitives.
7933
7934 declare
7935 E : Entity_Id;
7936
7937 begin
7938 E := First_Entity (Full_Der);
7939
7940 while Present (E) loop
7941 if Ekind (E) = E_Discriminant
7942 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7943 then
7944 Build_Itype_Reference (Etype (E), Decl);
7945 end if;
7946
7947 Next_Entity (E);
7948 end loop;
7949 end;
7950
7951 -- Set up links between real entity and underlying record view
7952
7953 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7954 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7955 end;
7956
7957 -- If discriminants are known, build derived record
7958
7959 else
7960 Build_Derived_Record_Type
7961 (N, Parent_Type, Derived_Type, Derive_Subps);
7962 end if;
7963
7964 return;
7965
7966 elsif Has_Discriminants (Parent_Type) then
7967
7968 -- Build partial view of derived type from partial view of parent.
7969 -- This must be done before building the full derivation because the
7970 -- second derivation will modify the discriminants of the first and
7971 -- the discriminants are chained with the rest of the components in
7972 -- the full derivation.
7973
7974 Build_Derived_Record_Type
7975 (N, Parent_Type, Derived_Type, Derive_Subps);
7976
7977 -- Build the full derivation if this is not the anonymous derived
7978 -- base type created by Build_Derived_Record_Type in the constrained
7979 -- case (see point 5. of its head comment) since we build it for the
7980 -- derived subtype.
7981
7982 if Present (Available_Full_View (Parent_Type))
7983 and then not Is_Itype (Derived_Type)
7984 then
7985 declare
7986 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7987 Discr : Entity_Id;
7988 Last_Discr : Entity_Id;
7989
7990 begin
7991 -- If this is not a completion, construct the implicit full
7992 -- view by deriving from the full view of the parent type.
7993 -- But if this is a completion, the derived private type
7994 -- being built is a full view and the full derivation can
7995 -- only be its underlying full view.
7996
7997 Build_Full_Derivation;
7998
7999 if not Is_Completion then
8000 Set_Full_View (Derived_Type, Full_Der);
8001 else
8002 Set_Underlying_Full_View (Derived_Type, Full_Der);
8003 Set_Is_Underlying_Full_View (Full_Der);
8004 end if;
8005
8006 if not Is_Base_Type (Derived_Type) then
8007 Set_Full_View (Der_Base, Base_Type (Full_Der));
8008 end if;
8009
8010 -- Copy the discriminant list from full view to the partial
8011 -- view (base type and its subtype). Gigi requires that the
8012 -- partial and full views have the same discriminants.
8013
8014 -- Note that since the partial view points to discriminants
8015 -- in the full view, their scope will be that of the full
8016 -- view. This might cause some front end problems and need
8017 -- adjustment???
8018
8019 Discr := First_Discriminant (Base_Type (Full_Der));
8020 Set_First_Entity (Der_Base, Discr);
8021
8022 loop
8023 Last_Discr := Discr;
8024 Next_Discriminant (Discr);
8025 exit when No (Discr);
8026 end loop;
8027
8028 Set_Last_Entity (Der_Base, Last_Discr);
8029 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8030 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8031 end;
8032 end if;
8033
8034 elsif Present (Available_Full_View (Parent_Type))
8035 and then Has_Discriminants (Available_Full_View (Parent_Type))
8036 then
8037 if Has_Unknown_Discriminants (Parent_Type)
8038 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8039 N_Subtype_Indication
8040 then
8041 Error_Msg_N
8042 ("cannot constrain type with unknown discriminants",
8043 Subtype_Indication (Type_Definition (N)));
8044 return;
8045 end if;
8046
8047 -- If this is not a completion, construct the implicit full view by
8048 -- deriving from the full view of the parent type. But if this is a
8049 -- completion, the derived private type being built is a full view
8050 -- and the full derivation can only be its underlying full view.
8051
8052 Build_Full_Derivation;
8053
8054 if not Is_Completion then
8055 Set_Full_View (Derived_Type, Full_Der);
8056 else
8057 Set_Underlying_Full_View (Derived_Type, Full_Der);
8058 Set_Is_Underlying_Full_View (Full_Der);
8059 end if;
8060
8061 -- In any case, the primitive operations are inherited from the
8062 -- parent type, not from the internal full view.
8063
8064 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8065
8066 if Derive_Subps then
8067 Derive_Subprograms (Parent_Type, Derived_Type);
8068 end if;
8069
8070 Set_Stored_Constraint (Derived_Type, No_Elist);
8071 Set_Is_Constrained
8072 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8073
8074 else
8075 -- Untagged type, No discriminants on either view
8076
8077 if Nkind (Subtype_Indication (Type_Definition (N))) =
8078 N_Subtype_Indication
8079 then
8080 Error_Msg_N
8081 ("illegal constraint on type without discriminants", N);
8082 end if;
8083
8084 if Present (Discriminant_Specifications (N))
8085 and then Present (Available_Full_View (Parent_Type))
8086 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8087 then
8088 Error_Msg_N ("cannot add discriminants to untagged type", N);
8089 end if;
8090
8091 Set_Stored_Constraint (Derived_Type, No_Elist);
8092 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8093
8094 Set_Is_Controlled_Active
8095 (Derived_Type, Is_Controlled_Active (Parent_Type));
8096
8097 Set_Disable_Controlled
8098 (Derived_Type, Disable_Controlled (Parent_Type));
8099
8100 Set_Has_Controlled_Component
8101 (Derived_Type, Has_Controlled_Component (Parent_Type));
8102
8103 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8104
8105 if not Is_Controlled (Parent_Type) then
8106 Set_Finalize_Storage_Only
8107 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8108 end if;
8109
8110 -- If this is not a completion, construct the implicit full view by
8111 -- deriving from the full view of the parent type. But if this is a
8112 -- completion, the derived private type being built is a full view
8113 -- and the full derivation can only be its underlying full view.
8114
8115 -- ??? If the parent type is untagged private and its completion is
8116 -- tagged, this mechanism will not work because we cannot derive from
8117 -- the tagged full view unless we have an extension.
8118
8119 if Present (Available_Full_View (Parent_Type))
8120 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8121 and then not Error_Posted (N)
8122 then
8123 Build_Full_Derivation;
8124
8125 if not Is_Completion then
8126 Set_Full_View (Derived_Type, Full_Der);
8127 else
8128 Set_Underlying_Full_View (Derived_Type, Full_Der);
8129 Set_Is_Underlying_Full_View (Full_Der);
8130 end if;
8131 end if;
8132 end if;
8133
8134 Set_Has_Unknown_Discriminants (Derived_Type,
8135 Has_Unknown_Discriminants (Parent_Type));
8136
8137 if Is_Private_Type (Derived_Type) then
8138 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8139 end if;
8140
8141 -- If the parent base type is in scope, add the derived type to its
8142 -- list of private dependents, because its full view may become
8143 -- visible subsequently (in a nested private part, a body, or in a
8144 -- further child unit).
8145
8146 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8147 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8148
8149 -- Check for unusual case where a type completed by a private
8150 -- derivation occurs within a package nested in a child unit, and
8151 -- the parent is declared in an ancestor.
8152
8153 if Is_Child_Unit (Scope (Current_Scope))
8154 and then Is_Completion
8155 and then In_Private_Part (Current_Scope)
8156 and then Scope (Parent_Type) /= Current_Scope
8157
8158 -- Note that if the parent has a completion in the private part,
8159 -- (which is itself a derivation from some other private type)
8160 -- it is that completion that is visible, there is no full view
8161 -- available, and no special processing is needed.
8162
8163 and then Present (Full_View (Parent_Type))
8164 then
8165 -- In this case, the full view of the parent type will become
8166 -- visible in the body of the enclosing child, and only then will
8167 -- the current type be possibly non-private. Build an underlying
8168 -- full view that will be installed when the enclosing child body
8169 -- is compiled.
8170
8171 if Present (Underlying_Full_View (Derived_Type)) then
8172 Full_Der := Underlying_Full_View (Derived_Type);
8173 else
8174 Build_Full_Derivation;
8175 Set_Underlying_Full_View (Derived_Type, Full_Der);
8176 Set_Is_Underlying_Full_View (Full_Der);
8177 end if;
8178
8179 -- The full view will be used to swap entities on entry/exit to
8180 -- the body, and must appear in the entity list for the package.
8181
8182 Append_Entity (Full_Der, Scope (Derived_Type));
8183 end if;
8184 end if;
8185 end Build_Derived_Private_Type;
8186
8187 -------------------------------
8188 -- Build_Derived_Record_Type --
8189 -------------------------------
8190
8191 -- 1. INTRODUCTION
8192
8193 -- Ideally we would like to use the same model of type derivation for
8194 -- tagged and untagged record types. Unfortunately this is not quite
8195 -- possible because the semantics of representation clauses is different
8196 -- for tagged and untagged records under inheritance. Consider the
8197 -- following:
8198
8199 -- type R (...) is [tagged] record ... end record;
8200 -- type T (...) is new R (...) [with ...];
8201
8202 -- The representation clauses for T can specify a completely different
8203 -- record layout from R's. Hence the same component can be placed in two
8204 -- very different positions in objects of type T and R. If R and T are
8205 -- tagged types, representation clauses for T can only specify the layout
8206 -- of non inherited components, thus components that are common in R and T
8207 -- have the same position in objects of type R and T.
8208
8209 -- This has two implications. The first is that the entire tree for R's
8210 -- declaration needs to be copied for T in the untagged case, so that T
8211 -- can be viewed as a record type of its own with its own representation
8212 -- clauses. The second implication is the way we handle discriminants.
8213 -- Specifically, in the untagged case we need a way to communicate to Gigi
8214 -- what are the real discriminants in the record, while for the semantics
8215 -- we need to consider those introduced by the user to rename the
8216 -- discriminants in the parent type. This is handled by introducing the
8217 -- notion of stored discriminants. See below for more.
8218
8219 -- Fortunately the way regular components are inherited can be handled in
8220 -- the same way in tagged and untagged types.
8221
8222 -- To complicate things a bit more the private view of a private extension
8223 -- cannot be handled in the same way as the full view (for one thing the
8224 -- semantic rules are somewhat different). We will explain what differs
8225 -- below.
8226
8227 -- 2. DISCRIMINANTS UNDER INHERITANCE
8228
8229 -- The semantic rules governing the discriminants of derived types are
8230 -- quite subtle.
8231
8232 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8233 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8234
8235 -- If parent type has discriminants, then the discriminants that are
8236 -- declared in the derived type are [3.4 (11)]:
8237
8238 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8239 -- there is one;
8240
8241 -- o Otherwise, each discriminant of the parent type (implicitly declared
8242 -- in the same order with the same specifications). In this case, the
8243 -- discriminants are said to be "inherited", or if unknown in the parent
8244 -- are also unknown in the derived type.
8245
8246 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8247
8248 -- o The parent subtype must be constrained;
8249
8250 -- o If the parent type is not a tagged type, then each discriminant of
8251 -- the derived type must be used in the constraint defining a parent
8252 -- subtype. [Implementation note: This ensures that the new discriminant
8253 -- can share storage with an existing discriminant.]
8254
8255 -- For the derived type each discriminant of the parent type is either
8256 -- inherited, constrained to equal some new discriminant of the derived
8257 -- type, or constrained to the value of an expression.
8258
8259 -- When inherited or constrained to equal some new discriminant, the
8260 -- parent discriminant and the discriminant of the derived type are said
8261 -- to "correspond".
8262
8263 -- If a discriminant of the parent type is constrained to a specific value
8264 -- in the derived type definition, then the discriminant is said to be
8265 -- "specified" by that derived type definition.
8266
8267 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8268
8269 -- We have spoken about stored discriminants in point 1 (introduction)
8270 -- above. There are two sorts of stored discriminants: implicit and
8271 -- explicit. As long as the derived type inherits the same discriminants as
8272 -- the root record type, stored discriminants are the same as regular
8273 -- discriminants, and are said to be implicit. However, if any discriminant
8274 -- in the root type was renamed in the derived type, then the derived
8275 -- type will contain explicit stored discriminants. Explicit stored
8276 -- discriminants are discriminants in addition to the semantically visible
8277 -- discriminants defined for the derived type. Stored discriminants are
8278 -- used by Gigi to figure out what are the physical discriminants in
8279 -- objects of the derived type (see precise definition in einfo.ads).
8280 -- As an example, consider the following:
8281
8282 -- type R (D1, D2, D3 : Int) is record ... end record;
8283 -- type T1 is new R;
8284 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8285 -- type T3 is new T2;
8286 -- type T4 (Y : Int) is new T3 (Y, 99);
8287
8288 -- The following table summarizes the discriminants and stored
8289 -- discriminants in R and T1 through T4:
8290
8291 -- Type Discrim Stored Discrim Comment
8292 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8293 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8294 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8295 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8296 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8297
8298 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8299 -- find the corresponding discriminant in the parent type, while
8300 -- Original_Record_Component (abbreviated ORC below) the actual physical
8301 -- component that is renamed. Finally the field Is_Completely_Hidden
8302 -- (abbreviated ICH below) is set for all explicit stored discriminants
8303 -- (see einfo.ads for more info). For the above example this gives:
8304
8305 -- Discrim CD ORC ICH
8306 -- ^^^^^^^ ^^ ^^^ ^^^
8307 -- D1 in R empty itself no
8308 -- D2 in R empty itself no
8309 -- D3 in R empty itself no
8310
8311 -- D1 in T1 D1 in R itself no
8312 -- D2 in T1 D2 in R itself no
8313 -- D3 in T1 D3 in R itself no
8314
8315 -- X1 in T2 D3 in T1 D3 in T2 no
8316 -- X2 in T2 D1 in T1 D1 in T2 no
8317 -- D1 in T2 empty itself yes
8318 -- D2 in T2 empty itself yes
8319 -- D3 in T2 empty itself yes
8320
8321 -- X1 in T3 X1 in T2 D3 in T3 no
8322 -- X2 in T3 X2 in T2 D1 in T3 no
8323 -- D1 in T3 empty itself yes
8324 -- D2 in T3 empty itself yes
8325 -- D3 in T3 empty itself yes
8326
8327 -- Y in T4 X1 in T3 D3 in T4 no
8328 -- D1 in T4 empty itself yes
8329 -- D2 in T4 empty itself yes
8330 -- D3 in T4 empty itself yes
8331
8332 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8333
8334 -- Type derivation for tagged types is fairly straightforward. If no
8335 -- discriminants are specified by the derived type, these are inherited
8336 -- from the parent. No explicit stored discriminants are ever necessary.
8337 -- The only manipulation that is done to the tree is that of adding a
8338 -- _parent field with parent type and constrained to the same constraint
8339 -- specified for the parent in the derived type definition. For instance:
8340
8341 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8342 -- type T1 is new R with null record;
8343 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8344
8345 -- are changed into:
8346
8347 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8348 -- _parent : R (D1, D2, D3);
8349 -- end record;
8350
8351 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8352 -- _parent : T1 (X2, 88, X1);
8353 -- end record;
8354
8355 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8356 -- ORC and ICH fields are:
8357
8358 -- Discrim CD ORC ICH
8359 -- ^^^^^^^ ^^ ^^^ ^^^
8360 -- D1 in R empty itself no
8361 -- D2 in R empty itself no
8362 -- D3 in R empty itself no
8363
8364 -- D1 in T1 D1 in R D1 in R no
8365 -- D2 in T1 D2 in R D2 in R no
8366 -- D3 in T1 D3 in R D3 in R no
8367
8368 -- X1 in T2 D3 in T1 D3 in R no
8369 -- X2 in T2 D1 in T1 D1 in R no
8370
8371 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8372 --
8373 -- Regardless of whether we dealing with a tagged or untagged type
8374 -- we will transform all derived type declarations of the form
8375 --
8376 -- type T is new R (...) [with ...];
8377 -- or
8378 -- subtype S is R (...);
8379 -- type T is new S [with ...];
8380 -- into
8381 -- type BT is new R [with ...];
8382 -- subtype T is BT (...);
8383 --
8384 -- That is, the base derived type is constrained only if it has no
8385 -- discriminants. The reason for doing this is that GNAT's semantic model
8386 -- assumes that a base type with discriminants is unconstrained.
8387 --
8388 -- Note that, strictly speaking, the above transformation is not always
8389 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8390 --
8391 -- procedure B34011A is
8392 -- type REC (D : integer := 0) is record
8393 -- I : Integer;
8394 -- end record;
8395
8396 -- package P is
8397 -- type T6 is new Rec;
8398 -- function F return T6;
8399 -- end P;
8400
8401 -- use P;
8402 -- package Q6 is
8403 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8404 -- end Q6;
8405 --
8406 -- The definition of Q6.U is illegal. However transforming Q6.U into
8407
8408 -- type BaseU is new T6;
8409 -- subtype U is BaseU (Q6.F.I)
8410
8411 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8412 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8413 -- the transformation described above.
8414
8415 -- There is another instance where the above transformation is incorrect.
8416 -- Consider:
8417
8418 -- package Pack is
8419 -- type Base (D : Integer) is tagged null record;
8420 -- procedure P (X : Base);
8421
8422 -- type Der is new Base (2) with null record;
8423 -- procedure P (X : Der);
8424 -- end Pack;
8425
8426 -- Then the above transformation turns this into
8427
8428 -- type Der_Base is new Base with null record;
8429 -- -- procedure P (X : Base) is implicitly inherited here
8430 -- -- as procedure P (X : Der_Base).
8431
8432 -- subtype Der is Der_Base (2);
8433 -- procedure P (X : Der);
8434 -- -- The overriding of P (X : Der_Base) is illegal since we
8435 -- -- have a parameter conformance problem.
8436
8437 -- To get around this problem, after having semantically processed Der_Base
8438 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8439 -- Discriminant_Constraint from Der so that when parameter conformance is
8440 -- checked when P is overridden, no semantic errors are flagged.
8441
8442 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8443
8444 -- Regardless of whether we are dealing with a tagged or untagged type
8445 -- we will transform all derived type declarations of the form
8446
8447 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8448 -- type T is new R [with ...];
8449 -- into
8450 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8451
8452 -- The reason for such transformation is that it allows us to implement a
8453 -- very clean form of component inheritance as explained below.
8454
8455 -- Note that this transformation is not achieved by direct tree rewriting
8456 -- and manipulation, but rather by redoing the semantic actions that the
8457 -- above transformation will entail. This is done directly in routine
8458 -- Inherit_Components.
8459
8460 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8461
8462 -- In both tagged and untagged derived types, regular non discriminant
8463 -- components are inherited in the derived type from the parent type. In
8464 -- the absence of discriminants component, inheritance is straightforward
8465 -- as components can simply be copied from the parent.
8466
8467 -- If the parent has discriminants, inheriting components constrained with
8468 -- these discriminants requires caution. Consider the following example:
8469
8470 -- type R (D1, D2 : Positive) is [tagged] record
8471 -- S : String (D1 .. D2);
8472 -- end record;
8473
8474 -- type T1 is new R [with null record];
8475 -- type T2 (X : positive) is new R (1, X) [with null record];
8476
8477 -- As explained in 6. above, T1 is rewritten as
8478 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8479 -- which makes the treatment for T1 and T2 identical.
8480
8481 -- What we want when inheriting S, is that references to D1 and D2 in R are
8482 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8483 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8484 -- with either discriminant references in the derived type or expressions.
8485 -- This replacement is achieved as follows: before inheriting R's
8486 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8487 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8488 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8489 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8490 -- by String (1 .. X).
8491
8492 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8493
8494 -- We explain here the rules governing private type extensions relevant to
8495 -- type derivation. These rules are explained on the following example:
8496
8497 -- type D [(...)] is new A [(...)] with private; <-- partial view
8498 -- type D [(...)] is new P [(...)] with null record; <-- full view
8499
8500 -- Type A is called the ancestor subtype of the private extension.
8501 -- Type P is the parent type of the full view of the private extension. It
8502 -- must be A or a type derived from A.
8503
8504 -- The rules concerning the discriminants of private type extensions are
8505 -- [7.3(10-13)]:
8506
8507 -- o If a private extension inherits known discriminants from the ancestor
8508 -- subtype, then the full view must also inherit its discriminants from
8509 -- the ancestor subtype and the parent subtype of the full view must be
8510 -- constrained if and only if the ancestor subtype is constrained.
8511
8512 -- o If a partial view has unknown discriminants, then the full view may
8513 -- define a definite or an indefinite subtype, with or without
8514 -- discriminants.
8515
8516 -- o If a partial view has neither known nor unknown discriminants, then
8517 -- the full view must define a definite subtype.
8518
8519 -- o If the ancestor subtype of a private extension has constrained
8520 -- discriminants, then the parent subtype of the full view must impose a
8521 -- statically matching constraint on those discriminants.
8522
8523 -- This means that only the following forms of private extensions are
8524 -- allowed:
8525
8526 -- type D is new A with private; <-- partial view
8527 -- type D is new P with null record; <-- full view
8528
8529 -- If A has no discriminants than P has no discriminants, otherwise P must
8530 -- inherit A's discriminants.
8531
8532 -- type D is new A (...) with private; <-- partial view
8533 -- type D is new P (:::) with null record; <-- full view
8534
8535 -- P must inherit A's discriminants and (...) and (:::) must statically
8536 -- match.
8537
8538 -- subtype A is R (...);
8539 -- type D is new A with private; <-- partial view
8540 -- type D is new P with null record; <-- full view
8541
8542 -- P must have inherited R's discriminants and must be derived from A or
8543 -- any of its subtypes.
8544
8545 -- type D (..) is new A with private; <-- partial view
8546 -- type D (..) is new P [(:::)] with null record; <-- full view
8547
8548 -- No specific constraints on P's discriminants or constraint (:::).
8549 -- Note that A can be unconstrained, but the parent subtype P must either
8550 -- be constrained or (:::) must be present.
8551
8552 -- type D (..) is new A [(...)] with private; <-- partial view
8553 -- type D (..) is new P [(:::)] with null record; <-- full view
8554
8555 -- P's constraints on A's discriminants must statically match those
8556 -- imposed by (...).
8557
8558 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8559
8560 -- The full view of a private extension is handled exactly as described
8561 -- above. The model chose for the private view of a private extension is
8562 -- the same for what concerns discriminants (i.e. they receive the same
8563 -- treatment as in the tagged case). However, the private view of the
8564 -- private extension always inherits the components of the parent base,
8565 -- without replacing any discriminant reference. Strictly speaking this is
8566 -- incorrect. However, Gigi never uses this view to generate code so this
8567 -- is a purely semantic issue. In theory, a set of transformations similar
8568 -- to those given in 5. and 6. above could be applied to private views of
8569 -- private extensions to have the same model of component inheritance as
8570 -- for non private extensions. However, this is not done because it would
8571 -- further complicate private type processing. Semantically speaking, this
8572 -- leaves us in an uncomfortable situation. As an example consider:
8573
8574 -- package Pack is
8575 -- type R (D : integer) is tagged record
8576 -- S : String (1 .. D);
8577 -- end record;
8578 -- procedure P (X : R);
8579 -- type T is new R (1) with private;
8580 -- private
8581 -- type T is new R (1) with null record;
8582 -- end;
8583
8584 -- This is transformed into:
8585
8586 -- package Pack is
8587 -- type R (D : integer) is tagged record
8588 -- S : String (1 .. D);
8589 -- end record;
8590 -- procedure P (X : R);
8591 -- type T is new R (1) with private;
8592 -- private
8593 -- type BaseT is new R with null record;
8594 -- subtype T is BaseT (1);
8595 -- end;
8596
8597 -- (strictly speaking the above is incorrect Ada)
8598
8599 -- From the semantic standpoint the private view of private extension T
8600 -- should be flagged as constrained since one can clearly have
8601 --
8602 -- Obj : T;
8603 --
8604 -- in a unit withing Pack. However, when deriving subprograms for the
8605 -- private view of private extension T, T must be seen as unconstrained
8606 -- since T has discriminants (this is a constraint of the current
8607 -- subprogram derivation model). Thus, when processing the private view of
8608 -- a private extension such as T, we first mark T as unconstrained, we
8609 -- process it, we perform program derivation and just before returning from
8610 -- Build_Derived_Record_Type we mark T as constrained.
8611
8612 -- ??? Are there are other uncomfortable cases that we will have to
8613 -- deal with.
8614
8615 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8616
8617 -- Types that are derived from a visible record type and have a private
8618 -- extension present other peculiarities. They behave mostly like private
8619 -- types, but if they have primitive operations defined, these will not
8620 -- have the proper signatures for further inheritance, because other
8621 -- primitive operations will use the implicit base that we define for
8622 -- private derivations below. This affect subprogram inheritance (see
8623 -- Derive_Subprograms for details). We also derive the implicit base from
8624 -- the base type of the full view, so that the implicit base is a record
8625 -- type and not another private type, This avoids infinite loops.
8626
8627 procedure Build_Derived_Record_Type
8628 (N : Node_Id;
8629 Parent_Type : Entity_Id;
8630 Derived_Type : Entity_Id;
8631 Derive_Subps : Boolean := True)
8632 is
8633 Discriminant_Specs : constant Boolean :=
8634 Present (Discriminant_Specifications (N));
8635 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8636 Loc : constant Source_Ptr := Sloc (N);
8637 Private_Extension : constant Boolean :=
8638 Nkind (N) = N_Private_Extension_Declaration;
8639 Assoc_List : Elist_Id;
8640 Constraint_Present : Boolean;
8641 Constrs : Elist_Id;
8642 Discrim : Entity_Id;
8643 Indic : Node_Id;
8644 Inherit_Discrims : Boolean := False;
8645 Last_Discrim : Entity_Id;
8646 New_Base : Entity_Id;
8647 New_Decl : Node_Id;
8648 New_Discrs : Elist_Id;
8649 New_Indic : Node_Id;
8650 Parent_Base : Entity_Id;
8651 Save_Etype : Entity_Id;
8652 Save_Discr_Constr : Elist_Id;
8653 Save_Next_Entity : Entity_Id;
8654 Type_Def : Node_Id;
8655
8656 Discs : Elist_Id := New_Elmt_List;
8657 -- An empty Discs list means that there were no constraints in the
8658 -- subtype indication or that there was an error processing it.
8659
8660 procedure Check_Generic_Ancestors;
8661 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8662 -- cannot be declared at a deeper level than its parent type is
8663 -- removed. The check on derivation within a generic body is also
8664 -- relaxed, but there's a restriction that a derived tagged type
8665 -- cannot be declared in a generic body if it's derived directly
8666 -- or indirectly from a formal type of that generic. This applies
8667 -- to progenitors as well.
8668
8669 -----------------------------
8670 -- Check_Generic_Ancestors --
8671 -----------------------------
8672
8673 procedure Check_Generic_Ancestors is
8674 Ancestor_Type : Entity_Id;
8675 Intf_List : List_Id;
8676 Intf_Name : Node_Id;
8677
8678 procedure Check_Ancestor;
8679 -- For parent and progenitors.
8680
8681 --------------------
8682 -- Check_Ancestor --
8683 --------------------
8684
8685 procedure Check_Ancestor is
8686 begin
8687 -- If the derived type does have a formal type as an ancestor
8688 -- then it's an error if the derived type is declared within
8689 -- the body of the generic unit that declares the formal type
8690 -- in its generic formal part. It's sufficient to check whether
8691 -- the ancestor type is declared inside the same generic body
8692 -- as the derived type (such as within a nested generic spec),
8693 -- in which case the derivation is legal. If the formal type is
8694 -- declared outside of that generic body, then it's certain
8695 -- that the derived type is declared within the generic body
8696 -- of the generic unit declaring the formal type.
8697
8698 if Is_Generic_Type (Ancestor_Type)
8699 and then Enclosing_Generic_Body (Ancestor_Type) /=
8700 Enclosing_Generic_Body (Derived_Type)
8701 then
8702 Error_Msg_NE
8703 ("ancestor type& is formal type of enclosing"
8704 & " generic unit (RM 3.9.1 (4/2))",
8705 Indic, Ancestor_Type);
8706 end if;
8707 end Check_Ancestor;
8708
8709 begin
8710 if Nkind (N) = N_Private_Extension_Declaration then
8711 Intf_List := Interface_List (N);
8712 else
8713 Intf_List := Interface_List (Type_Definition (N));
8714 end if;
8715
8716 if Present (Enclosing_Generic_Body (Derived_Type)) then
8717 Ancestor_Type := Parent_Type;
8718
8719 while not Is_Generic_Type (Ancestor_Type)
8720 and then Etype (Ancestor_Type) /= Ancestor_Type
8721 loop
8722 Ancestor_Type := Etype (Ancestor_Type);
8723 end loop;
8724
8725 Check_Ancestor;
8726
8727 if Present (Intf_List) then
8728 Intf_Name := First (Intf_List);
8729 while Present (Intf_Name) loop
8730 Ancestor_Type := Entity (Intf_Name);
8731 Check_Ancestor;
8732 Next (Intf_Name);
8733 end loop;
8734 end if;
8735 end if;
8736 end Check_Generic_Ancestors;
8737
8738 -- Start of processing for Build_Derived_Record_Type
8739
8740 begin
8741 if Ekind (Parent_Type) = E_Record_Type_With_Private
8742 and then Present (Full_View (Parent_Type))
8743 and then Has_Discriminants (Parent_Type)
8744 then
8745 Parent_Base := Base_Type (Full_View (Parent_Type));
8746 else
8747 Parent_Base := Base_Type (Parent_Type);
8748 end if;
8749
8750 -- If the parent type is declared as a subtype of another private
8751 -- type with inherited discriminants, its generated base type is
8752 -- itself a record subtype. To further inherit the constraint we
8753 -- need to use its own base to have an unconstrained type on which
8754 -- to apply the inherited constraint.
8755
8756 if Ekind (Parent_Base) = E_Record_Subtype then
8757 Parent_Base := Base_Type (Parent_Base);
8758 end if;
8759
8760 -- AI05-0115: if this is a derivation from a private type in some
8761 -- other scope that may lead to invisible components for the derived
8762 -- type, mark it accordingly.
8763
8764 if Is_Private_Type (Parent_Type) then
8765 if Scope (Parent_Base) = Scope (Derived_Type) then
8766 null;
8767
8768 elsif In_Open_Scopes (Scope (Parent_Base))
8769 and then In_Private_Part (Scope (Parent_Base))
8770 then
8771 null;
8772
8773 else
8774 Set_Has_Private_Ancestor (Derived_Type);
8775 end if;
8776
8777 else
8778 Set_Has_Private_Ancestor
8779 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8780 end if;
8781
8782 -- Before we start the previously documented transformations, here is
8783 -- little fix for size and alignment of tagged types. Normally when we
8784 -- derive type D from type P, we copy the size and alignment of P as the
8785 -- default for D, and in the absence of explicit representation clauses
8786 -- for D, the size and alignment are indeed the same as the parent.
8787
8788 -- But this is wrong for tagged types, since fields may be added, and
8789 -- the default size may need to be larger, and the default alignment may
8790 -- need to be larger.
8791
8792 -- We therefore reset the size and alignment fields in the tagged case.
8793 -- Note that the size and alignment will in any case be at least as
8794 -- large as the parent type (since the derived type has a copy of the
8795 -- parent type in the _parent field)
8796
8797 -- The type is also marked as being tagged here, which is needed when
8798 -- processing components with a self-referential anonymous access type
8799 -- in the call to Check_Anonymous_Access_Components below. Note that
8800 -- this flag is also set later on for completeness.
8801
8802 if Is_Tagged then
8803 Set_Is_Tagged_Type (Derived_Type);
8804 Init_Size_Align (Derived_Type);
8805 end if;
8806
8807 -- STEP 0a: figure out what kind of derived type declaration we have
8808
8809 if Private_Extension then
8810 Type_Def := N;
8811 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8812 Set_Default_SSO (Derived_Type);
8813 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8814
8815 else
8816 Type_Def := Type_Definition (N);
8817
8818 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8819 -- Parent_Base can be a private type or private extension. However,
8820 -- for tagged types with an extension the newly added fields are
8821 -- visible and hence the Derived_Type is always an E_Record_Type.
8822 -- (except that the parent may have its own private fields).
8823 -- For untagged types we preserve the Ekind of the Parent_Base.
8824
8825 if Present (Record_Extension_Part (Type_Def)) then
8826 Set_Ekind (Derived_Type, E_Record_Type);
8827 Set_Default_SSO (Derived_Type);
8828 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8829
8830 -- Create internal access types for components with anonymous
8831 -- access types.
8832
8833 if Ada_Version >= Ada_2005 then
8834 Check_Anonymous_Access_Components
8835 (N, Derived_Type, Derived_Type,
8836 Component_List (Record_Extension_Part (Type_Def)));
8837 end if;
8838
8839 else
8840 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8841 end if;
8842 end if;
8843
8844 -- Indic can either be an N_Identifier if the subtype indication
8845 -- contains no constraint or an N_Subtype_Indication if the subtype
8846 -- indication has a constraint. In either case it can include an
8847 -- interface list.
8848
8849 Indic := Subtype_Indication (Type_Def);
8850 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8851
8852 -- Check that the type has visible discriminants. The type may be
8853 -- a private type with unknown discriminants whose full view has
8854 -- discriminants which are invisible.
8855
8856 if Constraint_Present then
8857 if not Has_Discriminants (Parent_Base)
8858 or else
8859 (Has_Unknown_Discriminants (Parent_Base)
8860 and then Is_Private_Type (Parent_Base))
8861 then
8862 Error_Msg_N
8863 ("invalid constraint: type has no discriminant",
8864 Constraint (Indic));
8865
8866 Constraint_Present := False;
8867 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8868
8869 elsif Is_Constrained (Parent_Type) then
8870 Error_Msg_N
8871 ("invalid constraint: parent type is already constrained",
8872 Constraint (Indic));
8873
8874 Constraint_Present := False;
8875 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8876 end if;
8877 end if;
8878
8879 -- STEP 0b: If needed, apply transformation given in point 5. above
8880
8881 if not Private_Extension
8882 and then Has_Discriminants (Parent_Type)
8883 and then not Discriminant_Specs
8884 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8885 then
8886 -- First, we must analyze the constraint (see comment in point 5.)
8887 -- The constraint may come from the subtype indication of the full
8888 -- declaration.
8889
8890 if Constraint_Present then
8891 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8892
8893 -- If there is no explicit constraint, there might be one that is
8894 -- inherited from a constrained parent type. In that case verify that
8895 -- it conforms to the constraint in the partial view. In perverse
8896 -- cases the parent subtypes of the partial and full view can have
8897 -- different constraints.
8898
8899 elsif Present (Stored_Constraint (Parent_Type)) then
8900 New_Discrs := Stored_Constraint (Parent_Type);
8901
8902 else
8903 New_Discrs := No_Elist;
8904 end if;
8905
8906 if Has_Discriminants (Derived_Type)
8907 and then Has_Private_Declaration (Derived_Type)
8908 and then Present (Discriminant_Constraint (Derived_Type))
8909 and then Present (New_Discrs)
8910 then
8911 -- Verify that constraints of the full view statically match
8912 -- those given in the partial view.
8913
8914 declare
8915 C1, C2 : Elmt_Id;
8916
8917 begin
8918 C1 := First_Elmt (New_Discrs);
8919 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8920 while Present (C1) and then Present (C2) loop
8921 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8922 or else
8923 (Is_OK_Static_Expression (Node (C1))
8924 and then Is_OK_Static_Expression (Node (C2))
8925 and then
8926 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8927 then
8928 null;
8929
8930 else
8931 if Constraint_Present then
8932 Error_Msg_N
8933 ("constraint not conformant to previous declaration",
8934 Node (C1));
8935 else
8936 Error_Msg_N
8937 ("constraint of full view is incompatible "
8938 & "with partial view", N);
8939 end if;
8940 end if;
8941
8942 Next_Elmt (C1);
8943 Next_Elmt (C2);
8944 end loop;
8945 end;
8946 end if;
8947
8948 -- Insert and analyze the declaration for the unconstrained base type
8949
8950 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8951
8952 New_Decl :=
8953 Make_Full_Type_Declaration (Loc,
8954 Defining_Identifier => New_Base,
8955 Type_Definition =>
8956 Make_Derived_Type_Definition (Loc,
8957 Abstract_Present => Abstract_Present (Type_Def),
8958 Limited_Present => Limited_Present (Type_Def),
8959 Subtype_Indication =>
8960 New_Occurrence_Of (Parent_Base, Loc),
8961 Record_Extension_Part =>
8962 Relocate_Node (Record_Extension_Part (Type_Def)),
8963 Interface_List => Interface_List (Type_Def)));
8964
8965 Set_Parent (New_Decl, Parent (N));
8966 Mark_Rewrite_Insertion (New_Decl);
8967 Insert_Before (N, New_Decl);
8968
8969 -- In the extension case, make sure ancestor is frozen appropriately
8970 -- (see also non-discriminated case below).
8971
8972 if Present (Record_Extension_Part (Type_Def))
8973 or else Is_Interface (Parent_Base)
8974 then
8975 Freeze_Before (New_Decl, Parent_Type);
8976 end if;
8977
8978 -- Note that this call passes False for the Derive_Subps parameter
8979 -- because subprogram derivation is deferred until after creating
8980 -- the subtype (see below).
8981
8982 Build_Derived_Type
8983 (New_Decl, Parent_Base, New_Base,
8984 Is_Completion => False, Derive_Subps => False);
8985
8986 -- ??? This needs re-examination to determine whether the
8987 -- above call can simply be replaced by a call to Analyze.
8988
8989 Set_Analyzed (New_Decl);
8990
8991 -- Insert and analyze the declaration for the constrained subtype
8992
8993 if Constraint_Present then
8994 New_Indic :=
8995 Make_Subtype_Indication (Loc,
8996 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8997 Constraint => Relocate_Node (Constraint (Indic)));
8998
8999 else
9000 declare
9001 Constr_List : constant List_Id := New_List;
9002 C : Elmt_Id;
9003 Expr : Node_Id;
9004
9005 begin
9006 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9007 while Present (C) loop
9008 Expr := Node (C);
9009
9010 -- It is safe here to call New_Copy_Tree since we called
9011 -- Force_Evaluation on each constraint previously
9012 -- in Build_Discriminant_Constraints.
9013
9014 Append (New_Copy_Tree (Expr), To => Constr_List);
9015
9016 Next_Elmt (C);
9017 end loop;
9018
9019 New_Indic :=
9020 Make_Subtype_Indication (Loc,
9021 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9022 Constraint =>
9023 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9024 end;
9025 end if;
9026
9027 Rewrite (N,
9028 Make_Subtype_Declaration (Loc,
9029 Defining_Identifier => Derived_Type,
9030 Subtype_Indication => New_Indic));
9031
9032 Analyze (N);
9033
9034 -- Derivation of subprograms must be delayed until the full subtype
9035 -- has been established, to ensure proper overriding of subprograms
9036 -- inherited by full types. If the derivations occurred as part of
9037 -- the call to Build_Derived_Type above, then the check for type
9038 -- conformance would fail because earlier primitive subprograms
9039 -- could still refer to the full type prior the change to the new
9040 -- subtype and hence would not match the new base type created here.
9041 -- Subprograms are not derived, however, when Derive_Subps is False
9042 -- (since otherwise there could be redundant derivations).
9043
9044 if Derive_Subps then
9045 Derive_Subprograms (Parent_Type, Derived_Type);
9046 end if;
9047
9048 -- For tagged types the Discriminant_Constraint of the new base itype
9049 -- is inherited from the first subtype so that no subtype conformance
9050 -- problem arise when the first subtype overrides primitive
9051 -- operations inherited by the implicit base type.
9052
9053 if Is_Tagged then
9054 Set_Discriminant_Constraint
9055 (New_Base, Discriminant_Constraint (Derived_Type));
9056 end if;
9057
9058 return;
9059 end if;
9060
9061 -- If we get here Derived_Type will have no discriminants or it will be
9062 -- a discriminated unconstrained base type.
9063
9064 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9065
9066 if Is_Tagged then
9067
9068 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9069 -- The declaration of a specific descendant of an interface type
9070 -- freezes the interface type (RM 13.14).
9071
9072 if not Private_Extension or else Is_Interface (Parent_Base) then
9073 Freeze_Before (N, Parent_Type);
9074 end if;
9075
9076 if Ada_Version >= Ada_2005 then
9077 Check_Generic_Ancestors;
9078
9079 elsif Type_Access_Level (Derived_Type) /=
9080 Type_Access_Level (Parent_Type)
9081 and then not Is_Generic_Type (Derived_Type)
9082 then
9083 if Is_Controlled (Parent_Type) then
9084 Error_Msg_N
9085 ("controlled type must be declared at the library level",
9086 Indic);
9087 else
9088 Error_Msg_N
9089 ("type extension at deeper accessibility level than parent",
9090 Indic);
9091 end if;
9092
9093 else
9094 declare
9095 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9096 begin
9097 if Present (GB)
9098 and then GB /= Enclosing_Generic_Body (Parent_Base)
9099 then
9100 Error_Msg_NE
9101 ("parent type of& must not be outside generic body"
9102 & " (RM 3.9.1(4))",
9103 Indic, Derived_Type);
9104 end if;
9105 end;
9106 end if;
9107 end if;
9108
9109 -- Ada 2005 (AI-251)
9110
9111 if Ada_Version >= Ada_2005 and then Is_Tagged then
9112
9113 -- "The declaration of a specific descendant of an interface type
9114 -- freezes the interface type" (RM 13.14).
9115
9116 declare
9117 Iface : Node_Id;
9118 begin
9119 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9120 Iface := First (Interface_List (Type_Def));
9121 while Present (Iface) loop
9122 Freeze_Before (N, Etype (Iface));
9123 Next (Iface);
9124 end loop;
9125 end if;
9126 end;
9127 end if;
9128
9129 -- STEP 1b : preliminary cleanup of the full view of private types
9130
9131 -- If the type is already marked as having discriminants, then it's the
9132 -- completion of a private type or private extension and we need to
9133 -- retain the discriminants from the partial view if the current
9134 -- declaration has Discriminant_Specifications so that we can verify
9135 -- conformance. However, we must remove any existing components that
9136 -- were inherited from the parent (and attached in Copy_And_Swap)
9137 -- because the full type inherits all appropriate components anyway, and
9138 -- we do not want the partial view's components interfering.
9139
9140 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9141 Discrim := First_Discriminant (Derived_Type);
9142 loop
9143 Last_Discrim := Discrim;
9144 Next_Discriminant (Discrim);
9145 exit when No (Discrim);
9146 end loop;
9147
9148 Set_Last_Entity (Derived_Type, Last_Discrim);
9149
9150 -- In all other cases wipe out the list of inherited components (even
9151 -- inherited discriminants), it will be properly rebuilt here.
9152
9153 else
9154 Set_First_Entity (Derived_Type, Empty);
9155 Set_Last_Entity (Derived_Type, Empty);
9156 end if;
9157
9158 -- STEP 1c: Initialize some flags for the Derived_Type
9159
9160 -- The following flags must be initialized here so that
9161 -- Process_Discriminants can check that discriminants of tagged types do
9162 -- not have a default initial value and that access discriminants are
9163 -- only specified for limited records. For completeness, these flags are
9164 -- also initialized along with all the other flags below.
9165
9166 -- AI-419: Limitedness is not inherited from an interface parent, so to
9167 -- be limited in that case the type must be explicitly declared as
9168 -- limited. However, task and protected interfaces are always limited.
9169
9170 if Limited_Present (Type_Def) then
9171 Set_Is_Limited_Record (Derived_Type);
9172
9173 elsif Is_Limited_Record (Parent_Type)
9174 or else (Present (Full_View (Parent_Type))
9175 and then Is_Limited_Record (Full_View (Parent_Type)))
9176 then
9177 if not Is_Interface (Parent_Type)
9178 or else Is_Synchronized_Interface (Parent_Type)
9179 or else Is_Protected_Interface (Parent_Type)
9180 or else Is_Task_Interface (Parent_Type)
9181 then
9182 Set_Is_Limited_Record (Derived_Type);
9183 end if;
9184 end if;
9185
9186 -- STEP 2a: process discriminants of derived type if any
9187
9188 Push_Scope (Derived_Type);
9189
9190 if Discriminant_Specs then
9191 Set_Has_Unknown_Discriminants (Derived_Type, False);
9192
9193 -- The following call initializes fields Has_Discriminants and
9194 -- Discriminant_Constraint, unless we are processing the completion
9195 -- of a private type declaration.
9196
9197 Check_Or_Process_Discriminants (N, Derived_Type);
9198
9199 -- For untagged types, the constraint on the Parent_Type must be
9200 -- present and is used to rename the discriminants.
9201
9202 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9203 Error_Msg_N ("untagged parent must have discriminants", Indic);
9204
9205 elsif not Is_Tagged and then not Constraint_Present then
9206 Error_Msg_N
9207 ("discriminant constraint needed for derived untagged records",
9208 Indic);
9209
9210 -- Otherwise the parent subtype must be constrained unless we have a
9211 -- private extension.
9212
9213 elsif not Constraint_Present
9214 and then not Private_Extension
9215 and then not Is_Constrained (Parent_Type)
9216 then
9217 Error_Msg_N
9218 ("unconstrained type not allowed in this context", Indic);
9219
9220 elsif Constraint_Present then
9221 -- The following call sets the field Corresponding_Discriminant
9222 -- for the discriminants in the Derived_Type.
9223
9224 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9225
9226 -- For untagged types all new discriminants must rename
9227 -- discriminants in the parent. For private extensions new
9228 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9229
9230 Discrim := First_Discriminant (Derived_Type);
9231 while Present (Discrim) loop
9232 if not Is_Tagged
9233 and then No (Corresponding_Discriminant (Discrim))
9234 then
9235 Error_Msg_N
9236 ("new discriminants must constrain old ones", Discrim);
9237
9238 elsif Private_Extension
9239 and then Present (Corresponding_Discriminant (Discrim))
9240 then
9241 Error_Msg_N
9242 ("only static constraints allowed for parent"
9243 & " discriminants in the partial view", Indic);
9244 exit;
9245 end if;
9246
9247 -- If a new discriminant is used in the constraint, then its
9248 -- subtype must be statically compatible with the subtype of
9249 -- the parent discriminant (RM 3.7(15)).
9250
9251 if Present (Corresponding_Discriminant (Discrim)) then
9252 Check_Constraining_Discriminant
9253 (Discrim, Corresponding_Discriminant (Discrim));
9254 end if;
9255
9256 Next_Discriminant (Discrim);
9257 end loop;
9258
9259 -- Check whether the constraints of the full view statically
9260 -- match those imposed by the parent subtype [7.3(13)].
9261
9262 if Present (Stored_Constraint (Derived_Type)) then
9263 declare
9264 C1, C2 : Elmt_Id;
9265
9266 begin
9267 C1 := First_Elmt (Discs);
9268 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9269 while Present (C1) and then Present (C2) loop
9270 if not
9271 Fully_Conformant_Expressions (Node (C1), Node (C2))
9272 then
9273 Error_Msg_N
9274 ("not conformant with previous declaration",
9275 Node (C1));
9276 end if;
9277
9278 Next_Elmt (C1);
9279 Next_Elmt (C2);
9280 end loop;
9281 end;
9282 end if;
9283 end if;
9284
9285 -- STEP 2b: No new discriminants, inherit discriminants if any
9286
9287 else
9288 if Private_Extension then
9289 Set_Has_Unknown_Discriminants
9290 (Derived_Type,
9291 Has_Unknown_Discriminants (Parent_Type)
9292 or else Unknown_Discriminants_Present (N));
9293
9294 -- The partial view of the parent may have unknown discriminants,
9295 -- but if the full view has discriminants and the parent type is
9296 -- in scope they must be inherited.
9297
9298 elsif Has_Unknown_Discriminants (Parent_Type)
9299 and then
9300 (not Has_Discriminants (Parent_Type)
9301 or else not In_Open_Scopes (Scope (Parent_Base)))
9302 then
9303 Set_Has_Unknown_Discriminants (Derived_Type);
9304 end if;
9305
9306 if not Has_Unknown_Discriminants (Derived_Type)
9307 and then not Has_Unknown_Discriminants (Parent_Base)
9308 and then Has_Discriminants (Parent_Type)
9309 then
9310 Inherit_Discrims := True;
9311 Set_Has_Discriminants
9312 (Derived_Type, True);
9313 Set_Discriminant_Constraint
9314 (Derived_Type, Discriminant_Constraint (Parent_Base));
9315 end if;
9316
9317 -- The following test is true for private types (remember
9318 -- transformation 5. is not applied to those) and in an error
9319 -- situation.
9320
9321 if Constraint_Present then
9322 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9323 end if;
9324
9325 -- For now mark a new derived type as constrained only if it has no
9326 -- discriminants. At the end of Build_Derived_Record_Type we properly
9327 -- set this flag in the case of private extensions. See comments in
9328 -- point 9. just before body of Build_Derived_Record_Type.
9329
9330 Set_Is_Constrained
9331 (Derived_Type,
9332 not (Inherit_Discrims
9333 or else Has_Unknown_Discriminants (Derived_Type)));
9334 end if;
9335
9336 -- STEP 3: initialize fields of derived type
9337
9338 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9339 Set_Stored_Constraint (Derived_Type, No_Elist);
9340
9341 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9342 -- but cannot be interfaces
9343
9344 if not Private_Extension
9345 and then Ekind (Derived_Type) /= E_Private_Type
9346 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9347 then
9348 if Interface_Present (Type_Def) then
9349 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9350 end if;
9351
9352 Set_Interfaces (Derived_Type, No_Elist);
9353 end if;
9354
9355 -- Fields inherited from the Parent_Type
9356
9357 Set_Has_Specified_Layout
9358 (Derived_Type, Has_Specified_Layout (Parent_Type));
9359 Set_Is_Limited_Composite
9360 (Derived_Type, Is_Limited_Composite (Parent_Type));
9361 Set_Is_Private_Composite
9362 (Derived_Type, Is_Private_Composite (Parent_Type));
9363
9364 if Is_Tagged_Type (Parent_Type) then
9365 Set_No_Tagged_Streams_Pragma
9366 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9367 end if;
9368
9369 -- Fields inherited from the Parent_Base
9370
9371 Set_Has_Controlled_Component
9372 (Derived_Type, Has_Controlled_Component (Parent_Base));
9373 Set_Has_Non_Standard_Rep
9374 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9375 Set_Has_Primitive_Operations
9376 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9377
9378 -- Set fields for private derived types
9379
9380 if Is_Private_Type (Derived_Type) then
9381 Set_Depends_On_Private (Derived_Type, True);
9382 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9383 end if;
9384
9385 -- Inherit fields for non-private types. If this is the completion of a
9386 -- derivation from a private type, the parent itself is private and the
9387 -- attributes come from its full view, which must be present.
9388
9389 if Is_Record_Type (Derived_Type) then
9390 declare
9391 Parent_Full : Entity_Id;
9392
9393 begin
9394 if Is_Private_Type (Parent_Base)
9395 and then not Is_Record_Type (Parent_Base)
9396 then
9397 Parent_Full := Full_View (Parent_Base);
9398 else
9399 Parent_Full := Parent_Base;
9400 end if;
9401
9402 Set_Component_Alignment
9403 (Derived_Type, Component_Alignment (Parent_Full));
9404 Set_C_Pass_By_Copy
9405 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9406 Set_Has_Complex_Representation
9407 (Derived_Type, Has_Complex_Representation (Parent_Full));
9408
9409 -- For untagged types, inherit the layout by default to avoid
9410 -- costly changes of representation for type conversions.
9411
9412 if not Is_Tagged then
9413 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9414 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9415 end if;
9416 end;
9417 end if;
9418
9419 -- Set fields for tagged types
9420
9421 if Is_Tagged then
9422 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9423
9424 -- All tagged types defined in Ada.Finalization are controlled
9425
9426 if Chars (Scope (Derived_Type)) = Name_Finalization
9427 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9428 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9429 then
9430 Set_Is_Controlled_Active (Derived_Type);
9431 else
9432 Set_Is_Controlled_Active
9433 (Derived_Type, Is_Controlled_Active (Parent_Base));
9434 end if;
9435
9436 -- Minor optimization: there is no need to generate the class-wide
9437 -- entity associated with an underlying record view.
9438
9439 if not Is_Underlying_Record_View (Derived_Type) then
9440 Make_Class_Wide_Type (Derived_Type);
9441 end if;
9442
9443 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9444
9445 if Has_Discriminants (Derived_Type)
9446 and then Constraint_Present
9447 then
9448 Set_Stored_Constraint
9449 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9450 end if;
9451
9452 if Ada_Version >= Ada_2005 then
9453 declare
9454 Ifaces_List : Elist_Id;
9455
9456 begin
9457 -- Checks rules 3.9.4 (13/2 and 14/2)
9458
9459 if Comes_From_Source (Derived_Type)
9460 and then not Is_Private_Type (Derived_Type)
9461 and then Is_Interface (Parent_Type)
9462 and then not Is_Interface (Derived_Type)
9463 then
9464 if Is_Task_Interface (Parent_Type) then
9465 Error_Msg_N
9466 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9467 Derived_Type);
9468
9469 elsif Is_Protected_Interface (Parent_Type) then
9470 Error_Msg_N
9471 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9472 Derived_Type);
9473 end if;
9474 end if;
9475
9476 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9477
9478 Check_Interfaces (N, Type_Def);
9479
9480 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9481 -- not already in the parents.
9482
9483 Collect_Interfaces
9484 (T => Derived_Type,
9485 Ifaces_List => Ifaces_List,
9486 Exclude_Parents => True);
9487
9488 Set_Interfaces (Derived_Type, Ifaces_List);
9489
9490 -- If the derived type is the anonymous type created for
9491 -- a declaration whose parent has a constraint, propagate
9492 -- the interface list to the source type. This must be done
9493 -- prior to the completion of the analysis of the source type
9494 -- because the components in the extension may contain current
9495 -- instances whose legality depends on some ancestor.
9496
9497 if Is_Itype (Derived_Type) then
9498 declare
9499 Def : constant Node_Id :=
9500 Associated_Node_For_Itype (Derived_Type);
9501 begin
9502 if Present (Def)
9503 and then Nkind (Def) = N_Full_Type_Declaration
9504 then
9505 Set_Interfaces
9506 (Defining_Identifier (Def), Ifaces_List);
9507 end if;
9508 end;
9509 end if;
9510
9511 -- A type extension is automatically Ghost when one of its
9512 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9513 -- also inherited when the parent type is Ghost, but this is
9514 -- done in Build_Derived_Type as the mechanism also handles
9515 -- untagged derivations.
9516
9517 if Implements_Ghost_Interface (Derived_Type) then
9518 Set_Is_Ghost_Entity (Derived_Type);
9519 end if;
9520 end;
9521 end if;
9522 end if;
9523
9524 -- STEP 4: Inherit components from the parent base and constrain them.
9525 -- Apply the second transformation described in point 6. above.
9526
9527 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9528 or else not Has_Discriminants (Parent_Type)
9529 or else not Is_Constrained (Parent_Type)
9530 then
9531 Constrs := Discs;
9532 else
9533 Constrs := Discriminant_Constraint (Parent_Type);
9534 end if;
9535
9536 Assoc_List :=
9537 Inherit_Components
9538 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9539
9540 -- STEP 5a: Copy the parent record declaration for untagged types
9541
9542 Set_Has_Implicit_Dereference
9543 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9544
9545 if not Is_Tagged then
9546
9547 -- Discriminant_Constraint (Derived_Type) has been properly
9548 -- constructed. Save it and temporarily set it to Empty because we
9549 -- do not want the call to New_Copy_Tree below to mess this list.
9550
9551 if Has_Discriminants (Derived_Type) then
9552 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9553 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9554 else
9555 Save_Discr_Constr := No_Elist;
9556 end if;
9557
9558 -- Save the Etype field of Derived_Type. It is correctly set now,
9559 -- but the call to New_Copy tree may remap it to point to itself,
9560 -- which is not what we want. Ditto for the Next_Entity field.
9561
9562 Save_Etype := Etype (Derived_Type);
9563 Save_Next_Entity := Next_Entity (Derived_Type);
9564
9565 -- Assoc_List maps all stored discriminants in the Parent_Base to
9566 -- stored discriminants in the Derived_Type. It is fundamental that
9567 -- no types or itypes with discriminants other than the stored
9568 -- discriminants appear in the entities declared inside
9569 -- Derived_Type, since the back end cannot deal with it.
9570
9571 New_Decl :=
9572 New_Copy_Tree
9573 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9574 Copy_Dimensions_Of_Components (Derived_Type);
9575
9576 -- Restore the fields saved prior to the New_Copy_Tree call
9577 -- and compute the stored constraint.
9578
9579 Set_Etype (Derived_Type, Save_Etype);
9580 Link_Entities (Derived_Type, Save_Next_Entity);
9581
9582 if Has_Discriminants (Derived_Type) then
9583 Set_Discriminant_Constraint
9584 (Derived_Type, Save_Discr_Constr);
9585 Set_Stored_Constraint
9586 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9587
9588 Replace_Discriminants (Derived_Type, New_Decl);
9589 end if;
9590
9591 -- Insert the new derived type declaration
9592
9593 Rewrite (N, New_Decl);
9594
9595 -- STEP 5b: Complete the processing for record extensions in generics
9596
9597 -- There is no completion for record extensions declared in the
9598 -- parameter part of a generic, so we need to complete processing for
9599 -- these generic record extensions here. The Record_Type_Definition call
9600 -- will change the Ekind of the components from E_Void to E_Component.
9601
9602 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9603 Record_Type_Definition (Empty, Derived_Type);
9604
9605 -- STEP 5c: Process the record extension for non private tagged types
9606
9607 elsif not Private_Extension then
9608 Expand_Record_Extension (Derived_Type, Type_Def);
9609
9610 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9611 -- implemented interfaces if we are in expansion mode
9612
9613 if Expander_Active
9614 and then Has_Interfaces (Derived_Type)
9615 then
9616 Add_Interface_Tag_Components (N, Derived_Type);
9617 end if;
9618
9619 -- Analyze the record extension
9620
9621 Record_Type_Definition
9622 (Record_Extension_Part (Type_Def), Derived_Type);
9623 end if;
9624
9625 End_Scope;
9626
9627 -- Nothing else to do if there is an error in the derivation.
9628 -- An unusual case: the full view may be derived from a type in an
9629 -- instance, when the partial view was used illegally as an actual
9630 -- in that instance, leading to a circular definition.
9631
9632 if Etype (Derived_Type) = Any_Type
9633 or else Etype (Parent_Type) = Derived_Type
9634 then
9635 return;
9636 end if;
9637
9638 -- Set delayed freeze and then derive subprograms, we need to do
9639 -- this in this order so that derived subprograms inherit the
9640 -- derived freeze if necessary.
9641
9642 Set_Has_Delayed_Freeze (Derived_Type);
9643
9644 if Derive_Subps then
9645 Derive_Subprograms (Parent_Type, Derived_Type);
9646 end if;
9647
9648 -- If we have a private extension which defines a constrained derived
9649 -- type mark as constrained here after we have derived subprograms. See
9650 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9651
9652 if Private_Extension and then Inherit_Discrims then
9653 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9654 Set_Is_Constrained (Derived_Type, True);
9655 Set_Discriminant_Constraint (Derived_Type, Discs);
9656
9657 elsif Is_Constrained (Parent_Type) then
9658 Set_Is_Constrained
9659 (Derived_Type, True);
9660 Set_Discriminant_Constraint
9661 (Derived_Type, Discriminant_Constraint (Parent_Type));
9662 end if;
9663 end if;
9664
9665 -- Update the class-wide type, which shares the now-completed entity
9666 -- list with its specific type. In case of underlying record views,
9667 -- we do not generate the corresponding class wide entity.
9668
9669 if Is_Tagged
9670 and then not Is_Underlying_Record_View (Derived_Type)
9671 then
9672 Set_First_Entity
9673 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9674 Set_Last_Entity
9675 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9676 end if;
9677
9678 Check_Function_Writable_Actuals (N);
9679 end Build_Derived_Record_Type;
9680
9681 ------------------------
9682 -- Build_Derived_Type --
9683 ------------------------
9684
9685 procedure Build_Derived_Type
9686 (N : Node_Id;
9687 Parent_Type : Entity_Id;
9688 Derived_Type : Entity_Id;
9689 Is_Completion : Boolean;
9690 Derive_Subps : Boolean := True)
9691 is
9692 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9693
9694 begin
9695 -- Set common attributes
9696
9697 Set_Scope (Derived_Type, Current_Scope);
9698 Set_Etype (Derived_Type, Parent_Base);
9699 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9700 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9701
9702 Set_Size_Info (Derived_Type, Parent_Type);
9703 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9704
9705 Set_Is_Controlled_Active
9706 (Derived_Type, Is_Controlled_Active (Parent_Type));
9707
9708 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9709 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9710 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9711
9712 if Is_Tagged_Type (Derived_Type) then
9713 Set_No_Tagged_Streams_Pragma
9714 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9715 end if;
9716
9717 -- If the parent has primitive routines and may have not-seen-yet aspect
9718 -- specifications (e.g., a Pack pragma), then set the derived type link
9719 -- in order to later diagnose "early derivation" issues. If in different
9720 -- compilation units, then "early derivation" cannot be an issue (and we
9721 -- don't like interunit references that go in the opposite direction of
9722 -- semantic dependencies).
9723
9724 if Has_Primitive_Operations (Parent_Type)
9725 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9726 Enclosing_Comp_Unit_Node (Derived_Type)
9727 then
9728 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9729 end if;
9730
9731 -- If the parent type is a private subtype, the convention on the base
9732 -- type may be set in the private part, and not propagated to the
9733 -- subtype until later, so we obtain the convention from the base type.
9734
9735 Set_Convention (Derived_Type, Convention (Parent_Base));
9736
9737 -- Set SSO default for record or array type
9738
9739 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9740 and then Is_Base_Type (Derived_Type)
9741 then
9742 Set_Default_SSO (Derived_Type);
9743 end if;
9744
9745 -- A derived type inherits the Default_Initial_Condition pragma coming
9746 -- from any parent type within the derivation chain.
9747
9748 if Has_DIC (Parent_Type) then
9749 Set_Has_Inherited_DIC (Derived_Type);
9750 end if;
9751
9752 -- A derived type inherits any class-wide invariants coming from a
9753 -- parent type or an interface. Note that the invariant procedure of
9754 -- the parent type should not be inherited because the derived type may
9755 -- define invariants of its own.
9756
9757 if not Is_Interface (Derived_Type) then
9758 if Has_Inherited_Invariants (Parent_Type)
9759 or else Has_Inheritable_Invariants (Parent_Type)
9760 then
9761 Set_Has_Inherited_Invariants (Derived_Type);
9762
9763 elsif Is_Concurrent_Type (Derived_Type)
9764 or else Is_Tagged_Type (Derived_Type)
9765 then
9766 declare
9767 Iface : Entity_Id;
9768 Ifaces : Elist_Id;
9769 Iface_Elmt : Elmt_Id;
9770
9771 begin
9772 Collect_Interfaces
9773 (T => Derived_Type,
9774 Ifaces_List => Ifaces,
9775 Exclude_Parents => True);
9776
9777 if Present (Ifaces) then
9778 Iface_Elmt := First_Elmt (Ifaces);
9779 while Present (Iface_Elmt) loop
9780 Iface := Node (Iface_Elmt);
9781
9782 if Has_Inheritable_Invariants (Iface) then
9783 Set_Has_Inherited_Invariants (Derived_Type);
9784 exit;
9785 end if;
9786
9787 Next_Elmt (Iface_Elmt);
9788 end loop;
9789 end if;
9790 end;
9791 end if;
9792 end if;
9793
9794 -- We similarly inherit predicates. Note that for scalar derived types
9795 -- the predicate is inherited from the first subtype, and not from its
9796 -- (anonymous) base type.
9797
9798 if Has_Predicates (Parent_Type)
9799 or else Has_Predicates (First_Subtype (Parent_Type))
9800 then
9801 Set_Has_Predicates (Derived_Type);
9802 end if;
9803
9804 -- The derived type inherits representation clauses from the parent
9805 -- type, and from any interfaces.
9806
9807 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9808
9809 declare
9810 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9811 begin
9812 while Present (Iface) loop
9813 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9814 Next (Iface);
9815 end loop;
9816 end;
9817
9818 -- If the parent type has delayed rep aspects, then mark the derived
9819 -- type as possibly inheriting a delayed rep aspect.
9820
9821 if Has_Delayed_Rep_Aspects (Parent_Type) then
9822 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9823 end if;
9824
9825 -- A derived type becomes Ghost when its parent type is also Ghost
9826 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9827 -- directly inherited because the Ghost policy in effect may differ.
9828
9829 if Is_Ghost_Entity (Parent_Type) then
9830 Set_Is_Ghost_Entity (Derived_Type);
9831 end if;
9832
9833 -- Type dependent processing
9834
9835 case Ekind (Parent_Type) is
9836 when Numeric_Kind =>
9837 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9838
9839 when Array_Kind =>
9840 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9841
9842 when Class_Wide_Kind
9843 | E_Record_Subtype
9844 | E_Record_Type
9845 =>
9846 Build_Derived_Record_Type
9847 (N, Parent_Type, Derived_Type, Derive_Subps);
9848 return;
9849
9850 when Enumeration_Kind =>
9851 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9852
9853 when Access_Kind =>
9854 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9855
9856 when Incomplete_Or_Private_Kind =>
9857 Build_Derived_Private_Type
9858 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9859
9860 -- For discriminated types, the derivation includes deriving
9861 -- primitive operations. For others it is done below.
9862
9863 if Is_Tagged_Type (Parent_Type)
9864 or else Has_Discriminants (Parent_Type)
9865 or else (Present (Full_View (Parent_Type))
9866 and then Has_Discriminants (Full_View (Parent_Type)))
9867 then
9868 return;
9869 end if;
9870
9871 when Concurrent_Kind =>
9872 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9873
9874 when others =>
9875 raise Program_Error;
9876 end case;
9877
9878 -- Nothing more to do if some error occurred
9879
9880 if Etype (Derived_Type) = Any_Type then
9881 return;
9882 end if;
9883
9884 -- Set delayed freeze and then derive subprograms, we need to do this
9885 -- in this order so that derived subprograms inherit the derived freeze
9886 -- if necessary.
9887
9888 Set_Has_Delayed_Freeze (Derived_Type);
9889
9890 if Derive_Subps then
9891 Derive_Subprograms (Parent_Type, Derived_Type);
9892 end if;
9893
9894 Set_Has_Primitive_Operations
9895 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9896 end Build_Derived_Type;
9897
9898 -----------------------
9899 -- Build_Discriminal --
9900 -----------------------
9901
9902 procedure Build_Discriminal (Discrim : Entity_Id) is
9903 D_Minal : Entity_Id;
9904 CR_Disc : Entity_Id;
9905
9906 begin
9907 -- A discriminal has the same name as the discriminant
9908
9909 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9910
9911 Set_Ekind (D_Minal, E_In_Parameter);
9912 Set_Mechanism (D_Minal, Default_Mechanism);
9913 Set_Etype (D_Minal, Etype (Discrim));
9914 Set_Scope (D_Minal, Current_Scope);
9915 Set_Parent (D_Minal, Parent (Discrim));
9916
9917 Set_Discriminal (Discrim, D_Minal);
9918 Set_Discriminal_Link (D_Minal, Discrim);
9919
9920 -- For task types, build at once the discriminants of the corresponding
9921 -- record, which are needed if discriminants are used in entry defaults
9922 -- and in family bounds.
9923
9924 if Is_Concurrent_Type (Current_Scope)
9925 or else
9926 Is_Limited_Type (Current_Scope)
9927 then
9928 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9929
9930 Set_Ekind (CR_Disc, E_In_Parameter);
9931 Set_Mechanism (CR_Disc, Default_Mechanism);
9932 Set_Etype (CR_Disc, Etype (Discrim));
9933 Set_Scope (CR_Disc, Current_Scope);
9934 Set_Discriminal_Link (CR_Disc, Discrim);
9935 Set_CR_Discriminant (Discrim, CR_Disc);
9936 end if;
9937 end Build_Discriminal;
9938
9939 ------------------------------------
9940 -- Build_Discriminant_Constraints --
9941 ------------------------------------
9942
9943 function Build_Discriminant_Constraints
9944 (T : Entity_Id;
9945 Def : Node_Id;
9946 Derived_Def : Boolean := False) return Elist_Id
9947 is
9948 C : constant Node_Id := Constraint (Def);
9949 Nb_Discr : constant Nat := Number_Discriminants (T);
9950
9951 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9952 -- Saves the expression corresponding to a given discriminant in T
9953
9954 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9955 -- Return the Position number within array Discr_Expr of a discriminant
9956 -- D within the discriminant list of the discriminated type T.
9957
9958 procedure Process_Discriminant_Expression
9959 (Expr : Node_Id;
9960 D : Entity_Id);
9961 -- If this is a discriminant constraint on a partial view, do not
9962 -- generate an overflow check on the discriminant expression. The check
9963 -- will be generated when constraining the full view. Otherwise the
9964 -- backend creates duplicate symbols for the temporaries corresponding
9965 -- to the expressions to be checked, causing spurious assembler errors.
9966
9967 ------------------
9968 -- Pos_Of_Discr --
9969 ------------------
9970
9971 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9972 Disc : Entity_Id;
9973
9974 begin
9975 Disc := First_Discriminant (T);
9976 for J in Discr_Expr'Range loop
9977 if Disc = D then
9978 return J;
9979 end if;
9980
9981 Next_Discriminant (Disc);
9982 end loop;
9983
9984 -- Note: Since this function is called on discriminants that are
9985 -- known to belong to the discriminated type, falling through the
9986 -- loop with no match signals an internal compiler error.
9987
9988 raise Program_Error;
9989 end Pos_Of_Discr;
9990
9991 -------------------------------------
9992 -- Process_Discriminant_Expression --
9993 -------------------------------------
9994
9995 procedure Process_Discriminant_Expression
9996 (Expr : Node_Id;
9997 D : Entity_Id)
9998 is
9999 BDT : constant Entity_Id := Base_Type (Etype (D));
10000
10001 begin
10002 -- If this is a discriminant constraint on a partial view, do
10003 -- not generate an overflow on the discriminant expression. The
10004 -- check will be generated when constraining the full view.
10005
10006 if Is_Private_Type (T)
10007 and then Present (Full_View (T))
10008 then
10009 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10010 else
10011 Analyze_And_Resolve (Expr, BDT);
10012 end if;
10013 end Process_Discriminant_Expression;
10014
10015 -- Declarations local to Build_Discriminant_Constraints
10016
10017 Discr : Entity_Id;
10018 E : Entity_Id;
10019 Elist : constant Elist_Id := New_Elmt_List;
10020
10021 Constr : Node_Id;
10022 Expr : Node_Id;
10023 Id : Node_Id;
10024 Position : Nat;
10025 Found : Boolean;
10026
10027 Discrim_Present : Boolean := False;
10028
10029 -- Start of processing for Build_Discriminant_Constraints
10030
10031 begin
10032 -- The following loop will process positional associations only.
10033 -- For a positional association, the (single) discriminant is
10034 -- implicitly specified by position, in textual order (RM 3.7.2).
10035
10036 Discr := First_Discriminant (T);
10037 Constr := First (Constraints (C));
10038 for D in Discr_Expr'Range loop
10039 exit when Nkind (Constr) = N_Discriminant_Association;
10040
10041 if No (Constr) then
10042 Error_Msg_N ("too few discriminants given in constraint", C);
10043 return New_Elmt_List;
10044
10045 elsif Nkind (Constr) = N_Range
10046 or else (Nkind (Constr) = N_Attribute_Reference
10047 and then Attribute_Name (Constr) = Name_Range)
10048 then
10049 Error_Msg_N
10050 ("a range is not a valid discriminant constraint", Constr);
10051 Discr_Expr (D) := Error;
10052
10053 elsif Nkind (Constr) = N_Subtype_Indication then
10054 Error_Msg_N
10055 ("a subtype indication is not a valid discriminant constraint",
10056 Constr);
10057 Discr_Expr (D) := Error;
10058
10059 else
10060 Process_Discriminant_Expression (Constr, Discr);
10061 Discr_Expr (D) := Constr;
10062 end if;
10063
10064 Next_Discriminant (Discr);
10065 Next (Constr);
10066 end loop;
10067
10068 if No (Discr) and then Present (Constr) then
10069 Error_Msg_N ("too many discriminants given in constraint", Constr);
10070 return New_Elmt_List;
10071 end if;
10072
10073 -- Named associations can be given in any order, but if both positional
10074 -- and named associations are used in the same discriminant constraint,
10075 -- then positional associations must occur first, at their normal
10076 -- position. Hence once a named association is used, the rest of the
10077 -- discriminant constraint must use only named associations.
10078
10079 while Present (Constr) loop
10080
10081 -- Positional association forbidden after a named association
10082
10083 if Nkind (Constr) /= N_Discriminant_Association then
10084 Error_Msg_N ("positional association follows named one", Constr);
10085 return New_Elmt_List;
10086
10087 -- Otherwise it is a named association
10088
10089 else
10090 -- E records the type of the discriminants in the named
10091 -- association. All the discriminants specified in the same name
10092 -- association must have the same type.
10093
10094 E := Empty;
10095
10096 -- Search the list of discriminants in T to see if the simple name
10097 -- given in the constraint matches any of them.
10098
10099 Id := First (Selector_Names (Constr));
10100 while Present (Id) loop
10101 Found := False;
10102
10103 -- If Original_Discriminant is present, we are processing a
10104 -- generic instantiation and this is an instance node. We need
10105 -- to find the name of the corresponding discriminant in the
10106 -- actual record type T and not the name of the discriminant in
10107 -- the generic formal. Example:
10108
10109 -- generic
10110 -- type G (D : int) is private;
10111 -- package P is
10112 -- subtype W is G (D => 1);
10113 -- end package;
10114 -- type Rec (X : int) is record ... end record;
10115 -- package Q is new P (G => Rec);
10116
10117 -- At the point of the instantiation, formal type G is Rec
10118 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10119 -- which really looks like "subtype W is Rec (D => 1);" at
10120 -- the point of instantiation, we want to find the discriminant
10121 -- that corresponds to D in Rec, i.e. X.
10122
10123 if Present (Original_Discriminant (Id))
10124 and then In_Instance
10125 then
10126 Discr := Find_Corresponding_Discriminant (Id, T);
10127 Found := True;
10128
10129 else
10130 Discr := First_Discriminant (T);
10131 while Present (Discr) loop
10132 if Chars (Discr) = Chars (Id) then
10133 Found := True;
10134 exit;
10135 end if;
10136
10137 Next_Discriminant (Discr);
10138 end loop;
10139
10140 if not Found then
10141 Error_Msg_N ("& does not match any discriminant", Id);
10142 return New_Elmt_List;
10143
10144 -- If the parent type is a generic formal, preserve the
10145 -- name of the discriminant for subsequent instances.
10146 -- see comment at the beginning of this if statement.
10147
10148 elsif Is_Generic_Type (Root_Type (T)) then
10149 Set_Original_Discriminant (Id, Discr);
10150 end if;
10151 end if;
10152
10153 Position := Pos_Of_Discr (T, Discr);
10154
10155 if Present (Discr_Expr (Position)) then
10156 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10157
10158 else
10159 -- Each discriminant specified in the same named association
10160 -- must be associated with a separate copy of the
10161 -- corresponding expression.
10162
10163 if Present (Next (Id)) then
10164 Expr := New_Copy_Tree (Expression (Constr));
10165 Set_Parent (Expr, Parent (Expression (Constr)));
10166 else
10167 Expr := Expression (Constr);
10168 end if;
10169
10170 Discr_Expr (Position) := Expr;
10171 Process_Discriminant_Expression (Expr, Discr);
10172 end if;
10173
10174 -- A discriminant association with more than one discriminant
10175 -- name is only allowed if the named discriminants are all of
10176 -- the same type (RM 3.7.1(8)).
10177
10178 if E = Empty then
10179 E := Base_Type (Etype (Discr));
10180
10181 elsif Base_Type (Etype (Discr)) /= E then
10182 Error_Msg_N
10183 ("all discriminants in an association " &
10184 "must have the same type", Id);
10185 end if;
10186
10187 Next (Id);
10188 end loop;
10189 end if;
10190
10191 Next (Constr);
10192 end loop;
10193
10194 -- A discriminant constraint must provide exactly one value for each
10195 -- discriminant of the type (RM 3.7.1(8)).
10196
10197 for J in Discr_Expr'Range loop
10198 if No (Discr_Expr (J)) then
10199 Error_Msg_N ("too few discriminants given in constraint", C);
10200 return New_Elmt_List;
10201 end if;
10202 end loop;
10203
10204 -- Determine if there are discriminant expressions in the constraint
10205
10206 for J in Discr_Expr'Range loop
10207 if Denotes_Discriminant
10208 (Discr_Expr (J), Check_Concurrent => True)
10209 then
10210 Discrim_Present := True;
10211 end if;
10212 end loop;
10213
10214 -- Build an element list consisting of the expressions given in the
10215 -- discriminant constraint and apply the appropriate checks. The list
10216 -- is constructed after resolving any named discriminant associations
10217 -- and therefore the expressions appear in the textual order of the
10218 -- discriminants.
10219
10220 Discr := First_Discriminant (T);
10221 for J in Discr_Expr'Range loop
10222 if Discr_Expr (J) /= Error then
10223 Append_Elmt (Discr_Expr (J), Elist);
10224
10225 -- If any of the discriminant constraints is given by a
10226 -- discriminant and we are in a derived type declaration we
10227 -- have a discriminant renaming. Establish link between new
10228 -- and old discriminant. The new discriminant has an implicit
10229 -- dereference if the old one does.
10230
10231 if Denotes_Discriminant (Discr_Expr (J)) then
10232 if Derived_Def then
10233 declare
10234 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10235
10236 begin
10237 Set_Corresponding_Discriminant (New_Discr, Discr);
10238 Set_Has_Implicit_Dereference (New_Discr,
10239 Has_Implicit_Dereference (Discr));
10240 end;
10241 end if;
10242
10243 -- Force the evaluation of non-discriminant expressions.
10244 -- If we have found a discriminant in the constraint 3.4(26)
10245 -- and 3.8(18) demand that no range checks are performed are
10246 -- after evaluation. If the constraint is for a component
10247 -- definition that has a per-object constraint, expressions are
10248 -- evaluated but not checked either. In all other cases perform
10249 -- a range check.
10250
10251 else
10252 if Discrim_Present then
10253 null;
10254
10255 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10256 and then Has_Per_Object_Constraint
10257 (Defining_Identifier (Parent (Parent (Def))))
10258 then
10259 null;
10260
10261 elsif Is_Access_Type (Etype (Discr)) then
10262 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10263
10264 else
10265 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10266 end if;
10267
10268 Force_Evaluation (Discr_Expr (J));
10269 end if;
10270
10271 -- Check that the designated type of an access discriminant's
10272 -- expression is not a class-wide type unless the discriminant's
10273 -- designated type is also class-wide.
10274
10275 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10276 and then not Is_Class_Wide_Type
10277 (Designated_Type (Etype (Discr)))
10278 and then Etype (Discr_Expr (J)) /= Any_Type
10279 and then Is_Class_Wide_Type
10280 (Designated_Type (Etype (Discr_Expr (J))))
10281 then
10282 Wrong_Type (Discr_Expr (J), Etype (Discr));
10283
10284 elsif Is_Access_Type (Etype (Discr))
10285 and then not Is_Access_Constant (Etype (Discr))
10286 and then Is_Access_Type (Etype (Discr_Expr (J)))
10287 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10288 then
10289 Error_Msg_NE
10290 ("constraint for discriminant& must be access to variable",
10291 Def, Discr);
10292 end if;
10293 end if;
10294
10295 Next_Discriminant (Discr);
10296 end loop;
10297
10298 return Elist;
10299 end Build_Discriminant_Constraints;
10300
10301 ---------------------------------
10302 -- Build_Discriminated_Subtype --
10303 ---------------------------------
10304
10305 procedure Build_Discriminated_Subtype
10306 (T : Entity_Id;
10307 Def_Id : Entity_Id;
10308 Elist : Elist_Id;
10309 Related_Nod : Node_Id;
10310 For_Access : Boolean := False)
10311 is
10312 Has_Discrs : constant Boolean := Has_Discriminants (T);
10313 Constrained : constant Boolean :=
10314 (Has_Discrs
10315 and then not Is_Empty_Elmt_List (Elist)
10316 and then not Is_Class_Wide_Type (T))
10317 or else Is_Constrained (T);
10318
10319 begin
10320 if Ekind (T) = E_Record_Type then
10321 Set_Ekind (Def_Id, E_Record_Subtype);
10322
10323 -- Inherit preelaboration flag from base, for types for which it
10324 -- may have been set: records, private types, protected types.
10325
10326 Set_Known_To_Have_Preelab_Init
10327 (Def_Id, Known_To_Have_Preelab_Init (T));
10328
10329 elsif Ekind (T) = E_Task_Type then
10330 Set_Ekind (Def_Id, E_Task_Subtype);
10331
10332 elsif Ekind (T) = E_Protected_Type then
10333 Set_Ekind (Def_Id, E_Protected_Subtype);
10334 Set_Known_To_Have_Preelab_Init
10335 (Def_Id, Known_To_Have_Preelab_Init (T));
10336
10337 elsif Is_Private_Type (T) then
10338 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10339 Set_Known_To_Have_Preelab_Init
10340 (Def_Id, Known_To_Have_Preelab_Init (T));
10341
10342 -- Private subtypes may have private dependents
10343
10344 Set_Private_Dependents (Def_Id, New_Elmt_List);
10345
10346 elsif Is_Class_Wide_Type (T) then
10347 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10348
10349 else
10350 -- Incomplete type. Attach subtype to list of dependents, to be
10351 -- completed with full view of parent type, unless is it the
10352 -- designated subtype of a record component within an init_proc.
10353 -- This last case arises for a component of an access type whose
10354 -- designated type is incomplete (e.g. a Taft Amendment type).
10355 -- The designated subtype is within an inner scope, and needs no
10356 -- elaboration, because only the access type is needed in the
10357 -- initialization procedure.
10358
10359 if Ekind (T) = E_Incomplete_Type then
10360 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10361 else
10362 Set_Ekind (Def_Id, Ekind (T));
10363 end if;
10364
10365 if For_Access and then Within_Init_Proc then
10366 null;
10367 else
10368 Append_Elmt (Def_Id, Private_Dependents (T));
10369 end if;
10370 end if;
10371
10372 Set_Etype (Def_Id, T);
10373 Init_Size_Align (Def_Id);
10374 Set_Has_Discriminants (Def_Id, Has_Discrs);
10375 Set_Is_Constrained (Def_Id, Constrained);
10376
10377 Set_First_Entity (Def_Id, First_Entity (T));
10378 Set_Last_Entity (Def_Id, Last_Entity (T));
10379 Set_Has_Implicit_Dereference
10380 (Def_Id, Has_Implicit_Dereference (T));
10381 Set_Has_Pragma_Unreferenced_Objects
10382 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10383
10384 -- If the subtype is the completion of a private declaration, there may
10385 -- have been representation clauses for the partial view, and they must
10386 -- be preserved. Build_Derived_Type chains the inherited clauses with
10387 -- the ones appearing on the extension. If this comes from a subtype
10388 -- declaration, all clauses are inherited.
10389
10390 if No (First_Rep_Item (Def_Id)) then
10391 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10392 end if;
10393
10394 if Is_Tagged_Type (T) then
10395 Set_Is_Tagged_Type (Def_Id);
10396 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10397 Make_Class_Wide_Type (Def_Id);
10398 end if;
10399
10400 Set_Stored_Constraint (Def_Id, No_Elist);
10401
10402 if Has_Discrs then
10403 Set_Discriminant_Constraint (Def_Id, Elist);
10404 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10405 end if;
10406
10407 if Is_Tagged_Type (T) then
10408
10409 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10410 -- concurrent record type (which has the list of primitive
10411 -- operations).
10412
10413 if Ada_Version >= Ada_2005
10414 and then Is_Concurrent_Type (T)
10415 then
10416 Set_Corresponding_Record_Type (Def_Id,
10417 Corresponding_Record_Type (T));
10418 else
10419 Set_Direct_Primitive_Operations (Def_Id,
10420 Direct_Primitive_Operations (T));
10421 end if;
10422
10423 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10424 end if;
10425
10426 -- Subtypes introduced by component declarations do not need to be
10427 -- marked as delayed, and do not get freeze nodes, because the semantics
10428 -- verifies that the parents of the subtypes are frozen before the
10429 -- enclosing record is frozen.
10430
10431 if not Is_Type (Scope (Def_Id)) then
10432 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10433
10434 if Is_Private_Type (T)
10435 and then Present (Full_View (T))
10436 then
10437 Conditional_Delay (Def_Id, Full_View (T));
10438 else
10439 Conditional_Delay (Def_Id, T);
10440 end if;
10441 end if;
10442
10443 if Is_Record_Type (T) then
10444 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10445
10446 if Has_Discrs
10447 and then not Is_Empty_Elmt_List (Elist)
10448 and then not For_Access
10449 then
10450 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10451
10452 else
10453 Set_Cloned_Subtype (Def_Id, T);
10454 end if;
10455 end if;
10456 end Build_Discriminated_Subtype;
10457
10458 ---------------------------
10459 -- Build_Itype_Reference --
10460 ---------------------------
10461
10462 procedure Build_Itype_Reference
10463 (Ityp : Entity_Id;
10464 Nod : Node_Id)
10465 is
10466 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10467 begin
10468
10469 -- Itype references are only created for use by the back-end
10470
10471 if Inside_A_Generic then
10472 return;
10473 else
10474 Set_Itype (IR, Ityp);
10475
10476 -- If Nod is a library unit entity, then Insert_After won't work,
10477 -- because Nod is not a member of any list. Therefore, we use
10478 -- Add_Global_Declaration in this case. This can happen if we have a
10479 -- build-in-place library function, child unit or not.
10480
10481 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10482 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10483 N_Subprogram_Declaration)
10484 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10485 then
10486 Add_Global_Declaration (IR);
10487 else
10488 Insert_After (Nod, IR);
10489 end if;
10490 end if;
10491 end Build_Itype_Reference;
10492
10493 ------------------------
10494 -- Build_Scalar_Bound --
10495 ------------------------
10496
10497 function Build_Scalar_Bound
10498 (Bound : Node_Id;
10499 Par_T : Entity_Id;
10500 Der_T : Entity_Id) return Node_Id
10501 is
10502 New_Bound : Entity_Id;
10503
10504 begin
10505 -- Note: not clear why this is needed, how can the original bound
10506 -- be unanalyzed at this point? and if it is, what business do we
10507 -- have messing around with it? and why is the base type of the
10508 -- parent type the right type for the resolution. It probably is
10509 -- not. It is OK for the new bound we are creating, but not for
10510 -- the old one??? Still if it never happens, no problem.
10511
10512 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10513
10514 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10515 New_Bound := New_Copy (Bound);
10516 Set_Etype (New_Bound, Der_T);
10517 Set_Analyzed (New_Bound);
10518
10519 elsif Is_Entity_Name (Bound) then
10520 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10521
10522 -- The following is almost certainly wrong. What business do we have
10523 -- relocating a node (Bound) that is presumably still attached to
10524 -- the tree elsewhere???
10525
10526 else
10527 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10528 end if;
10529
10530 Set_Etype (New_Bound, Der_T);
10531 return New_Bound;
10532 end Build_Scalar_Bound;
10533
10534 -------------------------------
10535 -- Check_Abstract_Overriding --
10536 -------------------------------
10537
10538 procedure Check_Abstract_Overriding (T : Entity_Id) is
10539 Alias_Subp : Entity_Id;
10540 Elmt : Elmt_Id;
10541 Op_List : Elist_Id;
10542 Subp : Entity_Id;
10543 Type_Def : Node_Id;
10544
10545 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10546 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10547 -- which has pragma Implemented already set. Check whether Subp's entity
10548 -- kind conforms to the implementation kind of the overridden routine.
10549
10550 procedure Check_Pragma_Implemented
10551 (Subp : Entity_Id;
10552 Iface_Subp : Entity_Id);
10553 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10554 -- Iface_Subp and both entities have pragma Implemented already set on
10555 -- them. Check whether the two implementation kinds are conforming.
10556
10557 procedure Inherit_Pragma_Implemented
10558 (Subp : Entity_Id;
10559 Iface_Subp : Entity_Id);
10560 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10561 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10562 -- Propagate the implementation kind of Iface_Subp to Subp.
10563
10564 ------------------------------
10565 -- Check_Pragma_Implemented --
10566 ------------------------------
10567
10568 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10569 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10570 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10571 Subp_Alias : constant Entity_Id := Alias (Subp);
10572 Contr_Typ : Entity_Id;
10573 Impl_Subp : Entity_Id;
10574
10575 begin
10576 -- Subp must have an alias since it is a hidden entity used to link
10577 -- an interface subprogram to its overriding counterpart.
10578
10579 pragma Assert (Present (Subp_Alias));
10580
10581 -- Handle aliases to synchronized wrappers
10582
10583 Impl_Subp := Subp_Alias;
10584
10585 if Is_Primitive_Wrapper (Impl_Subp) then
10586 Impl_Subp := Wrapped_Entity (Impl_Subp);
10587 end if;
10588
10589 -- Extract the type of the controlling formal
10590
10591 Contr_Typ := Etype (First_Formal (Subp_Alias));
10592
10593 if Is_Concurrent_Record_Type (Contr_Typ) then
10594 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10595 end if;
10596
10597 -- An interface subprogram whose implementation kind is By_Entry must
10598 -- be implemented by an entry.
10599
10600 if Impl_Kind = Name_By_Entry
10601 and then Ekind (Impl_Subp) /= E_Entry
10602 then
10603 Error_Msg_Node_2 := Iface_Alias;
10604 Error_Msg_NE
10605 ("type & must implement abstract subprogram & with an entry",
10606 Subp_Alias, Contr_Typ);
10607
10608 elsif Impl_Kind = Name_By_Protected_Procedure then
10609
10610 -- An interface subprogram whose implementation kind is By_
10611 -- Protected_Procedure cannot be implemented by a primitive
10612 -- procedure of a task type.
10613
10614 if Ekind (Contr_Typ) /= E_Protected_Type then
10615 Error_Msg_Node_2 := Contr_Typ;
10616 Error_Msg_NE
10617 ("interface subprogram & cannot be implemented by a "
10618 & "primitive procedure of task type &",
10619 Subp_Alias, Iface_Alias);
10620
10621 -- An interface subprogram whose implementation kind is By_
10622 -- Protected_Procedure must be implemented by a procedure.
10623
10624 elsif Ekind (Impl_Subp) /= E_Procedure then
10625 Error_Msg_Node_2 := Iface_Alias;
10626 Error_Msg_NE
10627 ("type & must implement abstract subprogram & with a "
10628 & "procedure", Subp_Alias, Contr_Typ);
10629
10630 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10631 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10632 then
10633 Error_Msg_Name_1 := Impl_Kind;
10634 Error_Msg_N
10635 ("overriding operation& must have synchronization%",
10636 Subp_Alias);
10637 end if;
10638
10639 -- If primitive has Optional synchronization, overriding operation
10640 -- must match if it has an explicit synchronization.
10641
10642 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10643 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10644 then
10645 Error_Msg_Name_1 := Impl_Kind;
10646 Error_Msg_N
10647 ("overriding operation& must have synchronization%", Subp_Alias);
10648 end if;
10649 end Check_Pragma_Implemented;
10650
10651 ------------------------------
10652 -- Check_Pragma_Implemented --
10653 ------------------------------
10654
10655 procedure Check_Pragma_Implemented
10656 (Subp : Entity_Id;
10657 Iface_Subp : Entity_Id)
10658 is
10659 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10660 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10661
10662 begin
10663 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10664 -- and overriding subprogram are different. In general this is an
10665 -- error except when the implementation kind of the overridden
10666 -- subprograms is By_Any or Optional.
10667
10668 if Iface_Kind /= Subp_Kind
10669 and then Iface_Kind /= Name_By_Any
10670 and then Iface_Kind /= Name_Optional
10671 then
10672 if Iface_Kind = Name_By_Entry then
10673 Error_Msg_N
10674 ("incompatible implementation kind, overridden subprogram " &
10675 "is marked By_Entry", Subp);
10676 else
10677 Error_Msg_N
10678 ("incompatible implementation kind, overridden subprogram " &
10679 "is marked By_Protected_Procedure", Subp);
10680 end if;
10681 end if;
10682 end Check_Pragma_Implemented;
10683
10684 --------------------------------
10685 -- Inherit_Pragma_Implemented --
10686 --------------------------------
10687
10688 procedure Inherit_Pragma_Implemented
10689 (Subp : Entity_Id;
10690 Iface_Subp : Entity_Id)
10691 is
10692 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10693 Loc : constant Source_Ptr := Sloc (Subp);
10694 Impl_Prag : Node_Id;
10695
10696 begin
10697 -- Since the implementation kind is stored as a representation item
10698 -- rather than a flag, create a pragma node.
10699
10700 Impl_Prag :=
10701 Make_Pragma (Loc,
10702 Chars => Name_Implemented,
10703 Pragma_Argument_Associations => New_List (
10704 Make_Pragma_Argument_Association (Loc,
10705 Expression => New_Occurrence_Of (Subp, Loc)),
10706
10707 Make_Pragma_Argument_Association (Loc,
10708 Expression => Make_Identifier (Loc, Iface_Kind))));
10709
10710 -- The pragma doesn't need to be analyzed because it is internally
10711 -- built. It is safe to directly register it as a rep item since we
10712 -- are only interested in the characters of the implementation kind.
10713
10714 Record_Rep_Item (Subp, Impl_Prag);
10715 end Inherit_Pragma_Implemented;
10716
10717 -- Start of processing for Check_Abstract_Overriding
10718
10719 begin
10720 Op_List := Primitive_Operations (T);
10721
10722 -- Loop to check primitive operations
10723
10724 Elmt := First_Elmt (Op_List);
10725 while Present (Elmt) loop
10726 Subp := Node (Elmt);
10727 Alias_Subp := Alias (Subp);
10728
10729 -- Inherited subprograms are identified by the fact that they do not
10730 -- come from source, and the associated source location is the
10731 -- location of the first subtype of the derived type.
10732
10733 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10734 -- subprograms that "require overriding".
10735
10736 -- Special exception, do not complain about failure to override the
10737 -- stream routines _Input and _Output, as well as the primitive
10738 -- operations used in dispatching selects since we always provide
10739 -- automatic overridings for these subprograms.
10740
10741 -- The partial view of T may have been a private extension, for
10742 -- which inherited functions dispatching on result are abstract.
10743 -- If the full view is a null extension, there is no need for
10744 -- overriding in Ada 2005, but wrappers need to be built for them
10745 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10746
10747 if Is_Null_Extension (T)
10748 and then Has_Controlling_Result (Subp)
10749 and then Ada_Version >= Ada_2005
10750 and then Present (Alias_Subp)
10751 and then not Comes_From_Source (Subp)
10752 and then not Is_Abstract_Subprogram (Alias_Subp)
10753 and then not Is_Access_Type (Etype (Subp))
10754 then
10755 null;
10756
10757 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10758 -- processing because this check is done with the aliased
10759 -- entity
10760
10761 elsif Present (Interface_Alias (Subp)) then
10762 null;
10763
10764 elsif (Is_Abstract_Subprogram (Subp)
10765 or else Requires_Overriding (Subp)
10766 or else
10767 (Has_Controlling_Result (Subp)
10768 and then Present (Alias_Subp)
10769 and then not Comes_From_Source (Subp)
10770 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10771 and then not Is_TSS (Subp, TSS_Stream_Input)
10772 and then not Is_TSS (Subp, TSS_Stream_Output)
10773 and then not Is_Abstract_Type (T)
10774 and then not Is_Predefined_Interface_Primitive (Subp)
10775
10776 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10777 -- with abstract interface types because the check will be done
10778 -- with the aliased entity (otherwise we generate a duplicated
10779 -- error message).
10780
10781 and then not Present (Interface_Alias (Subp))
10782 then
10783 if Present (Alias_Subp) then
10784
10785 -- Only perform the check for a derived subprogram when the
10786 -- type has an explicit record extension. This avoids incorrect
10787 -- flagging of abstract subprograms for the case of a type
10788 -- without an extension that is derived from a formal type
10789 -- with a tagged actual (can occur within a private part).
10790
10791 -- Ada 2005 (AI-391): In the case of an inherited function with
10792 -- a controlling result of the type, the rule does not apply if
10793 -- the type is a null extension (unless the parent function
10794 -- itself is abstract, in which case the function must still be
10795 -- be overridden). The expander will generate an overriding
10796 -- wrapper function calling the parent subprogram (see
10797 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10798
10799 Type_Def := Type_Definition (Parent (T));
10800
10801 if Nkind (Type_Def) = N_Derived_Type_Definition
10802 and then Present (Record_Extension_Part (Type_Def))
10803 and then
10804 (Ada_Version < Ada_2005
10805 or else not Is_Null_Extension (T)
10806 or else Ekind (Subp) = E_Procedure
10807 or else not Has_Controlling_Result (Subp)
10808 or else Is_Abstract_Subprogram (Alias_Subp)
10809 or else Requires_Overriding (Subp)
10810 or else Is_Access_Type (Etype (Subp)))
10811 then
10812 -- Avoid reporting error in case of abstract predefined
10813 -- primitive inherited from interface type because the
10814 -- body of internally generated predefined primitives
10815 -- of tagged types are generated later by Freeze_Type
10816
10817 if Is_Interface (Root_Type (T))
10818 and then Is_Abstract_Subprogram (Subp)
10819 and then Is_Predefined_Dispatching_Operation (Subp)
10820 and then not Comes_From_Source (Ultimate_Alias (Subp))
10821 then
10822 null;
10823
10824 -- A null extension is not obliged to override an inherited
10825 -- procedure subject to pragma Extensions_Visible with value
10826 -- False and at least one controlling OUT parameter
10827 -- (SPARK RM 6.1.7(6)).
10828
10829 elsif Is_Null_Extension (T)
10830 and then Is_EVF_Procedure (Subp)
10831 then
10832 null;
10833
10834 else
10835 Error_Msg_NE
10836 ("type must be declared abstract or & overridden",
10837 T, Subp);
10838
10839 -- Traverse the whole chain of aliased subprograms to
10840 -- complete the error notification. This is especially
10841 -- useful for traceability of the chain of entities when
10842 -- the subprogram corresponds with an interface
10843 -- subprogram (which may be defined in another package).
10844
10845 if Present (Alias_Subp) then
10846 declare
10847 E : Entity_Id;
10848
10849 begin
10850 E := Subp;
10851 while Present (Alias (E)) loop
10852
10853 -- Avoid reporting redundant errors on entities
10854 -- inherited from interfaces
10855
10856 if Sloc (E) /= Sloc (T) then
10857 Error_Msg_Sloc := Sloc (E);
10858 Error_Msg_NE
10859 ("\& has been inherited #", T, Subp);
10860 end if;
10861
10862 E := Alias (E);
10863 end loop;
10864
10865 Error_Msg_Sloc := Sloc (E);
10866
10867 -- AI05-0068: report if there is an overriding
10868 -- non-abstract subprogram that is invisible.
10869
10870 if Is_Hidden (E)
10871 and then not Is_Abstract_Subprogram (E)
10872 then
10873 Error_Msg_NE
10874 ("\& subprogram# is not visible",
10875 T, Subp);
10876
10877 -- Clarify the case where a non-null extension must
10878 -- override inherited procedure subject to pragma
10879 -- Extensions_Visible with value False and at least
10880 -- one controlling OUT param.
10881
10882 elsif Is_EVF_Procedure (E) then
10883 Error_Msg_NE
10884 ("\& # is subject to Extensions_Visible False",
10885 T, Subp);
10886
10887 else
10888 Error_Msg_NE
10889 ("\& has been inherited from subprogram #",
10890 T, Subp);
10891 end if;
10892 end;
10893 end if;
10894 end if;
10895
10896 -- Ada 2005 (AI-345): Protected or task type implementing
10897 -- abstract interfaces.
10898
10899 elsif Is_Concurrent_Record_Type (T)
10900 and then Present (Interfaces (T))
10901 then
10902 -- There is no need to check here RM 9.4(11.9/3) since we
10903 -- are processing the corresponding record type and the
10904 -- mode of the overriding subprograms was verified by
10905 -- Check_Conformance when the corresponding concurrent
10906 -- type declaration was analyzed.
10907
10908 Error_Msg_NE
10909 ("interface subprogram & must be overridden", T, Subp);
10910
10911 -- Examine primitive operations of synchronized type to find
10912 -- homonyms that have the wrong profile.
10913
10914 declare
10915 Prim : Entity_Id;
10916
10917 begin
10918 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10919 while Present (Prim) loop
10920 if Chars (Prim) = Chars (Subp) then
10921 Error_Msg_NE
10922 ("profile is not type conformant with prefixed "
10923 & "view profile of inherited operation&",
10924 Prim, Subp);
10925 end if;
10926
10927 Next_Entity (Prim);
10928 end loop;
10929 end;
10930 end if;
10931
10932 else
10933 Error_Msg_Node_2 := T;
10934 Error_Msg_N
10935 ("abstract subprogram& not allowed for type&", Subp);
10936
10937 -- Also post unconditional warning on the type (unconditional
10938 -- so that if there are more than one of these cases, we get
10939 -- them all, and not just the first one).
10940
10941 Error_Msg_Node_2 := Subp;
10942 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10943 end if;
10944
10945 -- A subprogram subject to pragma Extensions_Visible with value
10946 -- "True" cannot override a subprogram subject to the same pragma
10947 -- with value "False" (SPARK RM 6.1.7(5)).
10948
10949 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10950 and then Present (Overridden_Operation (Subp))
10951 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10952 Extensions_Visible_False
10953 then
10954 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10955 Error_Msg_N
10956 ("subprogram & with Extensions_Visible True cannot override "
10957 & "subprogram # with Extensions_Visible False", Subp);
10958 end if;
10959
10960 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10961
10962 -- Subp is an expander-generated procedure which maps an interface
10963 -- alias to a protected wrapper. The interface alias is flagged by
10964 -- pragma Implemented. Ensure that Subp is a procedure when the
10965 -- implementation kind is By_Protected_Procedure or an entry when
10966 -- By_Entry.
10967
10968 if Ada_Version >= Ada_2012
10969 and then Is_Hidden (Subp)
10970 and then Present (Interface_Alias (Subp))
10971 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10972 then
10973 Check_Pragma_Implemented (Subp);
10974 end if;
10975
10976 -- Subp is an interface primitive which overrides another interface
10977 -- primitive marked with pragma Implemented.
10978
10979 if Ada_Version >= Ada_2012
10980 and then Present (Overridden_Operation (Subp))
10981 and then Has_Rep_Pragma
10982 (Overridden_Operation (Subp), Name_Implemented)
10983 then
10984 -- If the overriding routine is also marked by Implemented, check
10985 -- that the two implementation kinds are conforming.
10986
10987 if Has_Rep_Pragma (Subp, Name_Implemented) then
10988 Check_Pragma_Implemented
10989 (Subp => Subp,
10990 Iface_Subp => Overridden_Operation (Subp));
10991
10992 -- Otherwise the overriding routine inherits the implementation
10993 -- kind from the overridden subprogram.
10994
10995 else
10996 Inherit_Pragma_Implemented
10997 (Subp => Subp,
10998 Iface_Subp => Overridden_Operation (Subp));
10999 end if;
11000 end if;
11001
11002 -- Ada 2005 (AI95-0414) and Ada 2020 (AI12-0269): Diagnose failure to
11003 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11004 -- for procedures, since this is our pragma.
11005
11006 if Present (Overridden_Operation (Subp))
11007 and then No_Return (Overridden_Operation (Subp))
11008 and then not No_Return (Subp)
11009 then
11010 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11011 Error_Msg_N
11012 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11013 Subp);
11014 end if;
11015
11016 -- If the operation is a wrapper for a synchronized primitive, it
11017 -- may be called indirectly through a dispatching select. We assume
11018 -- that it will be referenced elsewhere indirectly, and suppress
11019 -- warnings about an unused entity.
11020
11021 if Is_Primitive_Wrapper (Subp)
11022 and then Present (Wrapped_Entity (Subp))
11023 then
11024 Set_Referenced (Wrapped_Entity (Subp));
11025 end if;
11026
11027 Next_Elmt (Elmt);
11028 end loop;
11029 end Check_Abstract_Overriding;
11030
11031 ------------------------------------------------
11032 -- Check_Access_Discriminant_Requires_Limited --
11033 ------------------------------------------------
11034
11035 procedure Check_Access_Discriminant_Requires_Limited
11036 (D : Node_Id;
11037 Loc : Node_Id)
11038 is
11039 begin
11040 -- A discriminant_specification for an access discriminant shall appear
11041 -- only in the declaration for a task or protected type, or for a type
11042 -- with the reserved word 'limited' in its definition or in one of its
11043 -- ancestors (RM 3.7(10)).
11044
11045 -- AI-0063: The proper condition is that type must be immutably limited,
11046 -- or else be a partial view.
11047
11048 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11049 if Is_Limited_View (Current_Scope)
11050 or else
11051 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11052 and then Limited_Present (Parent (Current_Scope)))
11053 then
11054 null;
11055
11056 else
11057 Error_Msg_N
11058 ("access discriminants allowed only for limited types", Loc);
11059 end if;
11060 end if;
11061 end Check_Access_Discriminant_Requires_Limited;
11062
11063 -----------------------------------
11064 -- Check_Aliased_Component_Types --
11065 -----------------------------------
11066
11067 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11068 C : Entity_Id;
11069
11070 begin
11071 -- ??? Also need to check components of record extensions, but not
11072 -- components of protected types (which are always limited).
11073
11074 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11075 -- types to be unconstrained. This is safe because it is illegal to
11076 -- create access subtypes to such types with explicit discriminant
11077 -- constraints.
11078
11079 if not Is_Limited_Type (T) then
11080 if Ekind (T) = E_Record_Type then
11081 C := First_Component (T);
11082 while Present (C) loop
11083 if Is_Aliased (C)
11084 and then Has_Discriminants (Etype (C))
11085 and then not Is_Constrained (Etype (C))
11086 and then not In_Instance_Body
11087 and then Ada_Version < Ada_2005
11088 then
11089 Error_Msg_N
11090 ("aliased component must be constrained (RM 3.6(11))",
11091 C);
11092 end if;
11093
11094 Next_Component (C);
11095 end loop;
11096
11097 elsif Ekind (T) = E_Array_Type then
11098 if Has_Aliased_Components (T)
11099 and then Has_Discriminants (Component_Type (T))
11100 and then not Is_Constrained (Component_Type (T))
11101 and then not In_Instance_Body
11102 and then Ada_Version < Ada_2005
11103 then
11104 Error_Msg_N
11105 ("aliased component type must be constrained (RM 3.6(11))",
11106 T);
11107 end if;
11108 end if;
11109 end if;
11110 end Check_Aliased_Component_Types;
11111
11112 ---------------------------------------
11113 -- Check_Anonymous_Access_Components --
11114 ---------------------------------------
11115
11116 procedure Check_Anonymous_Access_Components
11117 (Typ_Decl : Node_Id;
11118 Typ : Entity_Id;
11119 Prev : Entity_Id;
11120 Comp_List : Node_Id)
11121 is
11122 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11123 Anon_Access : Entity_Id;
11124 Acc_Def : Node_Id;
11125 Comp : Node_Id;
11126 Comp_Def : Node_Id;
11127 Decl : Node_Id;
11128 Type_Def : Node_Id;
11129
11130 procedure Build_Incomplete_Type_Declaration;
11131 -- If the record type contains components that include an access to the
11132 -- current record, then create an incomplete type declaration for the
11133 -- record, to be used as the designated type of the anonymous access.
11134 -- This is done only once, and only if there is no previous partial
11135 -- view of the type.
11136
11137 function Designates_T (Subt : Node_Id) return Boolean;
11138 -- Check whether a node designates the enclosing record type, or 'Class
11139 -- of that type
11140
11141 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11142 -- Check whether an access definition includes a reference to
11143 -- the enclosing record type. The reference can be a subtype mark
11144 -- in the access definition itself, a 'Class attribute reference, or
11145 -- recursively a reference appearing in a parameter specification
11146 -- or result definition of an access_to_subprogram definition.
11147
11148 --------------------------------------
11149 -- Build_Incomplete_Type_Declaration --
11150 --------------------------------------
11151
11152 procedure Build_Incomplete_Type_Declaration is
11153 Decl : Node_Id;
11154 Inc_T : Entity_Id;
11155 H : Entity_Id;
11156
11157 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11158 -- it's "is new ... with record" or else "is tagged record ...".
11159
11160 Is_Tagged : constant Boolean :=
11161 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11162 and then
11163 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11164 or else
11165 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11166 and then Tagged_Present (Type_Definition (Typ_Decl)));
11167
11168 begin
11169 -- If there is a previous partial view, no need to create a new one
11170 -- If the partial view, given by Prev, is incomplete, If Prev is
11171 -- a private declaration, full declaration is flagged accordingly.
11172
11173 if Prev /= Typ then
11174 if Is_Tagged then
11175 Make_Class_Wide_Type (Prev);
11176 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11177 Set_Etype (Class_Wide_Type (Typ), Typ);
11178 end if;
11179
11180 return;
11181
11182 elsif Has_Private_Declaration (Typ) then
11183
11184 -- If we refer to T'Class inside T, and T is the completion of a
11185 -- private type, then make sure the class-wide type exists.
11186
11187 if Is_Tagged then
11188 Make_Class_Wide_Type (Typ);
11189 end if;
11190
11191 return;
11192
11193 -- If there was a previous anonymous access type, the incomplete
11194 -- type declaration will have been created already.
11195
11196 elsif Present (Current_Entity (Typ))
11197 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11198 and then Full_View (Current_Entity (Typ)) = Typ
11199 then
11200 if Is_Tagged
11201 and then Comes_From_Source (Current_Entity (Typ))
11202 and then not Is_Tagged_Type (Current_Entity (Typ))
11203 then
11204 Make_Class_Wide_Type (Typ);
11205 Error_Msg_N
11206 ("incomplete view of tagged type should be declared tagged??",
11207 Parent (Current_Entity (Typ)));
11208 end if;
11209 return;
11210
11211 else
11212 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11213 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11214
11215 -- Type has already been inserted into the current scope. Remove
11216 -- it, and add incomplete declaration for type, so that subsequent
11217 -- anonymous access types can use it. The entity is unchained from
11218 -- the homonym list and from immediate visibility. After analysis,
11219 -- the entity in the incomplete declaration becomes immediately
11220 -- visible in the record declaration that follows.
11221
11222 H := Current_Entity (Typ);
11223
11224 if H = Typ then
11225 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11226 else
11227 while Present (H)
11228 and then Homonym (H) /= Typ
11229 loop
11230 H := Homonym (Typ);
11231 end loop;
11232
11233 Set_Homonym (H, Homonym (Typ));
11234 end if;
11235
11236 Insert_Before (Typ_Decl, Decl);
11237 Analyze (Decl);
11238 Set_Full_View (Inc_T, Typ);
11239
11240 if Is_Tagged then
11241
11242 -- Create a common class-wide type for both views, and set the
11243 -- Etype of the class-wide type to the full view.
11244
11245 Make_Class_Wide_Type (Inc_T);
11246 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11247 Set_Etype (Class_Wide_Type (Typ), Typ);
11248 end if;
11249 end if;
11250 end Build_Incomplete_Type_Declaration;
11251
11252 ------------------
11253 -- Designates_T --
11254 ------------------
11255
11256 function Designates_T (Subt : Node_Id) return Boolean is
11257 Type_Id : constant Name_Id := Chars (Typ);
11258
11259 function Names_T (Nam : Node_Id) return Boolean;
11260 -- The record type has not been introduced in the current scope
11261 -- yet, so we must examine the name of the type itself, either
11262 -- an identifier T, or an expanded name of the form P.T, where
11263 -- P denotes the current scope.
11264
11265 -------------
11266 -- Names_T --
11267 -------------
11268
11269 function Names_T (Nam : Node_Id) return Boolean is
11270 begin
11271 if Nkind (Nam) = N_Identifier then
11272 return Chars (Nam) = Type_Id;
11273
11274 elsif Nkind (Nam) = N_Selected_Component then
11275 if Chars (Selector_Name (Nam)) = Type_Id then
11276 if Nkind (Prefix (Nam)) = N_Identifier then
11277 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11278
11279 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11280 return Chars (Selector_Name (Prefix (Nam))) =
11281 Chars (Current_Scope);
11282 else
11283 return False;
11284 end if;
11285
11286 else
11287 return False;
11288 end if;
11289
11290 else
11291 return False;
11292 end if;
11293 end Names_T;
11294
11295 -- Start of processing for Designates_T
11296
11297 begin
11298 if Nkind (Subt) = N_Identifier then
11299 return Chars (Subt) = Type_Id;
11300
11301 -- Reference can be through an expanded name which has not been
11302 -- analyzed yet, and which designates enclosing scopes.
11303
11304 elsif Nkind (Subt) = N_Selected_Component then
11305 if Names_T (Subt) then
11306 return True;
11307
11308 -- Otherwise it must denote an entity that is already visible.
11309 -- The access definition may name a subtype of the enclosing
11310 -- type, if there is a previous incomplete declaration for it.
11311
11312 else
11313 Find_Selected_Component (Subt);
11314 return
11315 Is_Entity_Name (Subt)
11316 and then Scope (Entity (Subt)) = Current_Scope
11317 and then
11318 (Chars (Base_Type (Entity (Subt))) = Type_Id
11319 or else
11320 (Is_Class_Wide_Type (Entity (Subt))
11321 and then
11322 Chars (Etype (Base_Type (Entity (Subt)))) =
11323 Type_Id));
11324 end if;
11325
11326 -- A reference to the current type may appear as the prefix of
11327 -- a 'Class attribute.
11328
11329 elsif Nkind (Subt) = N_Attribute_Reference
11330 and then Attribute_Name (Subt) = Name_Class
11331 then
11332 return Names_T (Prefix (Subt));
11333
11334 else
11335 return False;
11336 end if;
11337 end Designates_T;
11338
11339 ----------------
11340 -- Mentions_T --
11341 ----------------
11342
11343 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11344 Param_Spec : Node_Id;
11345
11346 Acc_Subprg : constant Node_Id :=
11347 Access_To_Subprogram_Definition (Acc_Def);
11348
11349 begin
11350 if No (Acc_Subprg) then
11351 return Designates_T (Subtype_Mark (Acc_Def));
11352 end if;
11353
11354 -- Component is an access_to_subprogram: examine its formals,
11355 -- and result definition in the case of an access_to_function.
11356
11357 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11358 while Present (Param_Spec) loop
11359 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11360 and then Mentions_T (Parameter_Type (Param_Spec))
11361 then
11362 return True;
11363
11364 elsif Designates_T (Parameter_Type (Param_Spec)) then
11365 return True;
11366 end if;
11367
11368 Next (Param_Spec);
11369 end loop;
11370
11371 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11372 if Nkind (Result_Definition (Acc_Subprg)) =
11373 N_Access_Definition
11374 then
11375 return Mentions_T (Result_Definition (Acc_Subprg));
11376 else
11377 return Designates_T (Result_Definition (Acc_Subprg));
11378 end if;
11379 end if;
11380
11381 return False;
11382 end Mentions_T;
11383
11384 -- Start of processing for Check_Anonymous_Access_Components
11385
11386 begin
11387 if No (Comp_List) then
11388 return;
11389 end if;
11390
11391 Comp := First (Component_Items (Comp_List));
11392 while Present (Comp) loop
11393 if Nkind (Comp) = N_Component_Declaration
11394 and then Present
11395 (Access_Definition (Component_Definition (Comp)))
11396 and then
11397 Mentions_T (Access_Definition (Component_Definition (Comp)))
11398 then
11399 Comp_Def := Component_Definition (Comp);
11400 Acc_Def :=
11401 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11402
11403 Build_Incomplete_Type_Declaration;
11404 Anon_Access := Make_Temporary (Loc, 'S');
11405
11406 -- Create a declaration for the anonymous access type: either
11407 -- an access_to_object or an access_to_subprogram.
11408
11409 if Present (Acc_Def) then
11410 if Nkind (Acc_Def) = N_Access_Function_Definition then
11411 Type_Def :=
11412 Make_Access_Function_Definition (Loc,
11413 Parameter_Specifications =>
11414 Parameter_Specifications (Acc_Def),
11415 Result_Definition => Result_Definition (Acc_Def));
11416 else
11417 Type_Def :=
11418 Make_Access_Procedure_Definition (Loc,
11419 Parameter_Specifications =>
11420 Parameter_Specifications (Acc_Def));
11421 end if;
11422
11423 else
11424 Type_Def :=
11425 Make_Access_To_Object_Definition (Loc,
11426 Subtype_Indication =>
11427 Relocate_Node
11428 (Subtype_Mark (Access_Definition (Comp_Def))));
11429
11430 Set_Constant_Present
11431 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11432 Set_All_Present
11433 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11434 end if;
11435
11436 Set_Null_Exclusion_Present
11437 (Type_Def,
11438 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11439
11440 Decl :=
11441 Make_Full_Type_Declaration (Loc,
11442 Defining_Identifier => Anon_Access,
11443 Type_Definition => Type_Def);
11444
11445 Insert_Before (Typ_Decl, Decl);
11446 Analyze (Decl);
11447
11448 -- If an access to subprogram, create the extra formals
11449
11450 if Present (Acc_Def) then
11451 Create_Extra_Formals (Designated_Type (Anon_Access));
11452 end if;
11453
11454 Rewrite (Comp_Def,
11455 Make_Component_Definition (Loc,
11456 Subtype_Indication =>
11457 New_Occurrence_Of (Anon_Access, Loc)));
11458
11459 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11460 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11461 else
11462 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11463 end if;
11464
11465 Set_Is_Local_Anonymous_Access (Anon_Access);
11466 end if;
11467
11468 Next (Comp);
11469 end loop;
11470
11471 if Present (Variant_Part (Comp_List)) then
11472 declare
11473 V : Node_Id;
11474 begin
11475 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11476 while Present (V) loop
11477 Check_Anonymous_Access_Components
11478 (Typ_Decl, Typ, Prev, Component_List (V));
11479 Next_Non_Pragma (V);
11480 end loop;
11481 end;
11482 end if;
11483 end Check_Anonymous_Access_Components;
11484
11485 ----------------------
11486 -- Check_Completion --
11487 ----------------------
11488
11489 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11490 E : Entity_Id;
11491
11492 procedure Post_Error;
11493 -- Post error message for lack of completion for entity E
11494
11495 ----------------
11496 -- Post_Error --
11497 ----------------
11498
11499 procedure Post_Error is
11500 procedure Missing_Body;
11501 -- Output missing body message
11502
11503 ------------------
11504 -- Missing_Body --
11505 ------------------
11506
11507 procedure Missing_Body is
11508 begin
11509 -- Spec is in same unit, so we can post on spec
11510
11511 if In_Same_Source_Unit (Body_Id, E) then
11512 Error_Msg_N ("missing body for &", E);
11513
11514 -- Spec is in a separate unit, so we have to post on the body
11515
11516 else
11517 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11518 end if;
11519 end Missing_Body;
11520
11521 -- Start of processing for Post_Error
11522
11523 begin
11524 if not Comes_From_Source (E) then
11525 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11526
11527 -- It may be an anonymous protected type created for a
11528 -- single variable. Post error on variable, if present.
11529
11530 declare
11531 Var : Entity_Id;
11532
11533 begin
11534 Var := First_Entity (Current_Scope);
11535 while Present (Var) loop
11536 exit when Etype (Var) = E
11537 and then Comes_From_Source (Var);
11538
11539 Next_Entity (Var);
11540 end loop;
11541
11542 if Present (Var) then
11543 E := Var;
11544 end if;
11545 end;
11546 end if;
11547 end if;
11548
11549 -- If a generated entity has no completion, then either previous
11550 -- semantic errors have disabled the expansion phase, or else we had
11551 -- missing subunits, or else we are compiling without expansion,
11552 -- or else something is very wrong.
11553
11554 if not Comes_From_Source (E) then
11555 pragma Assert
11556 (Serious_Errors_Detected > 0
11557 or else Configurable_Run_Time_Violations > 0
11558 or else Subunits_Missing
11559 or else not Expander_Active);
11560 return;
11561
11562 -- Here for source entity
11563
11564 else
11565 -- Here if no body to post the error message, so we post the error
11566 -- on the declaration that has no completion. This is not really
11567 -- the right place to post it, think about this later ???
11568
11569 if No (Body_Id) then
11570 if Is_Type (E) then
11571 Error_Msg_NE
11572 ("missing full declaration for }", Parent (E), E);
11573 else
11574 Error_Msg_NE ("missing body for &", Parent (E), E);
11575 end if;
11576
11577 -- Package body has no completion for a declaration that appears
11578 -- in the corresponding spec. Post error on the body, with a
11579 -- reference to the non-completed declaration.
11580
11581 else
11582 Error_Msg_Sloc := Sloc (E);
11583
11584 if Is_Type (E) then
11585 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11586
11587 elsif Is_Overloadable (E)
11588 and then Current_Entity_In_Scope (E) /= E
11589 then
11590 -- It may be that the completion is mistyped and appears as
11591 -- a distinct overloading of the entity.
11592
11593 declare
11594 Candidate : constant Entity_Id :=
11595 Current_Entity_In_Scope (E);
11596 Decl : constant Node_Id :=
11597 Unit_Declaration_Node (Candidate);
11598
11599 begin
11600 if Is_Overloadable (Candidate)
11601 and then Ekind (Candidate) = Ekind (E)
11602 and then Nkind (Decl) = N_Subprogram_Body
11603 and then Acts_As_Spec (Decl)
11604 then
11605 Check_Type_Conformant (Candidate, E);
11606
11607 else
11608 Missing_Body;
11609 end if;
11610 end;
11611
11612 else
11613 Missing_Body;
11614 end if;
11615 end if;
11616 end if;
11617 end Post_Error;
11618
11619 -- Local variables
11620
11621 Pack_Id : constant Entity_Id := Current_Scope;
11622
11623 -- Start of processing for Check_Completion
11624
11625 begin
11626 E := First_Entity (Pack_Id);
11627 while Present (E) loop
11628 if Is_Intrinsic_Subprogram (E) then
11629 null;
11630
11631 -- The following situation requires special handling: a child unit
11632 -- that appears in the context clause of the body of its parent:
11633
11634 -- procedure Parent.Child (...);
11635
11636 -- with Parent.Child;
11637 -- package body Parent is
11638
11639 -- Here Parent.Child appears as a local entity, but should not be
11640 -- flagged as requiring completion, because it is a compilation
11641 -- unit.
11642
11643 -- Ignore missing completion for a subprogram that does not come from
11644 -- source (including the _Call primitive operation of RAS types,
11645 -- which has to have the flag Comes_From_Source for other purposes):
11646 -- we assume that the expander will provide the missing completion.
11647 -- In case of previous errors, other expansion actions that provide
11648 -- bodies for null procedures with not be invoked, so inhibit message
11649 -- in those cases.
11650
11651 -- Note that E_Operator is not in the list that follows, because
11652 -- this kind is reserved for predefined operators, that are
11653 -- intrinsic and do not need completion.
11654
11655 elsif Ekind_In (E, E_Function,
11656 E_Procedure,
11657 E_Generic_Function,
11658 E_Generic_Procedure)
11659 then
11660 if Has_Completion (E) then
11661 null;
11662
11663 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11664 null;
11665
11666 elsif Is_Subprogram (E)
11667 and then (not Comes_From_Source (E)
11668 or else Chars (E) = Name_uCall)
11669 then
11670 null;
11671
11672 elsif
11673 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11674 then
11675 null;
11676
11677 elsif Nkind (Parent (E)) = N_Procedure_Specification
11678 and then Null_Present (Parent (E))
11679 and then Serious_Errors_Detected > 0
11680 then
11681 null;
11682
11683 else
11684 Post_Error;
11685 end if;
11686
11687 elsif Is_Entry (E) then
11688 if not Has_Completion (E) and then
11689 (Ekind (Scope (E)) = E_Protected_Object
11690 or else Ekind (Scope (E)) = E_Protected_Type)
11691 then
11692 Post_Error;
11693 end if;
11694
11695 elsif Is_Package_Or_Generic_Package (E) then
11696 if Unit_Requires_Body (E) then
11697 if not Has_Completion (E)
11698 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11699 N_Compilation_Unit
11700 then
11701 Post_Error;
11702 end if;
11703
11704 elsif not Is_Child_Unit (E) then
11705 May_Need_Implicit_Body (E);
11706 end if;
11707
11708 -- A formal incomplete type (Ada 2012) does not require a completion;
11709 -- other incomplete type declarations do.
11710
11711 elsif Ekind (E) = E_Incomplete_Type
11712 and then No (Underlying_Type (E))
11713 and then not Is_Generic_Type (E)
11714 then
11715 Post_Error;
11716
11717 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11718 and then not Has_Completion (E)
11719 then
11720 Post_Error;
11721
11722 -- A single task declared in the current scope is a constant, verify
11723 -- that the body of its anonymous type is in the same scope. If the
11724 -- task is defined elsewhere, this may be a renaming declaration for
11725 -- which no completion is needed.
11726
11727 elsif Ekind (E) = E_Constant
11728 and then Ekind (Etype (E)) = E_Task_Type
11729 and then not Has_Completion (Etype (E))
11730 and then Scope (Etype (E)) = Current_Scope
11731 then
11732 Post_Error;
11733
11734 elsif Ekind (E) = E_Protected_Object
11735 and then not Has_Completion (Etype (E))
11736 then
11737 Post_Error;
11738
11739 elsif Ekind (E) = E_Record_Type then
11740 if Is_Tagged_Type (E) then
11741 Check_Abstract_Overriding (E);
11742 Check_Conventions (E);
11743 end if;
11744
11745 Check_Aliased_Component_Types (E);
11746
11747 elsif Ekind (E) = E_Array_Type then
11748 Check_Aliased_Component_Types (E);
11749
11750 end if;
11751
11752 Next_Entity (E);
11753 end loop;
11754 end Check_Completion;
11755
11756 -------------------------------------
11757 -- Check_Constraining_Discriminant --
11758 -------------------------------------
11759
11760 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
11761 is
11762 New_Type : constant Entity_Id := Etype (New_Disc);
11763 Old_Type : Entity_Id;
11764
11765 begin
11766 -- If the record type contains an array constrained by the discriminant
11767 -- but with some different bound, the compiler tries to create a smaller
11768 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
11769 -- In this case, where the discriminant type is a scalar type, the check
11770 -- must use the original discriminant type in the parent declaration.
11771
11772 if Is_Scalar_Type (New_Type) then
11773 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
11774 else
11775 Old_Type := Etype (Old_Disc);
11776 end if;
11777
11778 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
11779 Error_Msg_N
11780 ("subtype must be statically compatible with parent discriminant",
11781 New_Disc);
11782
11783 if not Predicates_Compatible (New_Type, Old_Type) then
11784 Error_Msg_N
11785 ("\subtype predicate is not compatible with parent discriminant",
11786 New_Disc);
11787 end if;
11788 end if;
11789 end Check_Constraining_Discriminant;
11790
11791 ------------------------------------
11792 -- Check_CPP_Type_Has_No_Defaults --
11793 ------------------------------------
11794
11795 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11796 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11797 Clist : Node_Id;
11798 Comp : Node_Id;
11799
11800 begin
11801 -- Obtain the component list
11802
11803 if Nkind (Tdef) = N_Record_Definition then
11804 Clist := Component_List (Tdef);
11805 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11806 Clist := Component_List (Record_Extension_Part (Tdef));
11807 end if;
11808
11809 -- Check all components to ensure no default expressions
11810
11811 if Present (Clist) then
11812 Comp := First (Component_Items (Clist));
11813 while Present (Comp) loop
11814 if Present (Expression (Comp)) then
11815 Error_Msg_N
11816 ("component of imported 'C'P'P type cannot have "
11817 & "default expression", Expression (Comp));
11818 end if;
11819
11820 Next (Comp);
11821 end loop;
11822 end if;
11823 end Check_CPP_Type_Has_No_Defaults;
11824
11825 ----------------------------
11826 -- Check_Delta_Expression --
11827 ----------------------------
11828
11829 procedure Check_Delta_Expression (E : Node_Id) is
11830 begin
11831 if not (Is_Real_Type (Etype (E))) then
11832 Wrong_Type (E, Any_Real);
11833
11834 elsif not Is_OK_Static_Expression (E) then
11835 Flag_Non_Static_Expr
11836 ("non-static expression used for delta value!", E);
11837
11838 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11839 Error_Msg_N ("delta expression must be positive", E);
11840
11841 else
11842 return;
11843 end if;
11844
11845 -- If any of above errors occurred, then replace the incorrect
11846 -- expression by the real 0.1, which should prevent further errors.
11847
11848 Rewrite (E,
11849 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11850 Analyze_And_Resolve (E, Standard_Float);
11851 end Check_Delta_Expression;
11852
11853 -----------------------------
11854 -- Check_Digits_Expression --
11855 -----------------------------
11856
11857 procedure Check_Digits_Expression (E : Node_Id) is
11858 begin
11859 if not (Is_Integer_Type (Etype (E))) then
11860 Wrong_Type (E, Any_Integer);
11861
11862 elsif not Is_OK_Static_Expression (E) then
11863 Flag_Non_Static_Expr
11864 ("non-static expression used for digits value!", E);
11865
11866 elsif Expr_Value (E) <= 0 then
11867 Error_Msg_N ("digits value must be greater than zero", E);
11868
11869 else
11870 return;
11871 end if;
11872
11873 -- If any of above errors occurred, then replace the incorrect
11874 -- expression by the integer 1, which should prevent further errors.
11875
11876 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11877 Analyze_And_Resolve (E, Standard_Integer);
11878
11879 end Check_Digits_Expression;
11880
11881 --------------------------
11882 -- Check_Initialization --
11883 --------------------------
11884
11885 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11886 begin
11887 -- Special processing for limited types
11888
11889 if Is_Limited_Type (T)
11890 and then not In_Instance
11891 and then not In_Inlined_Body
11892 then
11893 if not OK_For_Limited_Init (T, Exp) then
11894
11895 -- In GNAT mode, this is just a warning, to allow it to be evilly
11896 -- turned off. Otherwise it is a real error.
11897
11898 if GNAT_Mode then
11899 Error_Msg_N
11900 ("??cannot initialize entities of limited type!", Exp);
11901
11902 elsif Ada_Version < Ada_2005 then
11903
11904 -- The side effect removal machinery may generate illegal Ada
11905 -- code to avoid the usage of access types and 'reference in
11906 -- SPARK mode. Since this is legal code with respect to theorem
11907 -- proving, do not emit the error.
11908
11909 if GNATprove_Mode
11910 and then Nkind (Exp) = N_Function_Call
11911 and then Nkind (Parent (Exp)) = N_Object_Declaration
11912 and then not Comes_From_Source
11913 (Defining_Identifier (Parent (Exp)))
11914 then
11915 null;
11916
11917 else
11918 Error_Msg_N
11919 ("cannot initialize entities of limited type", Exp);
11920 Explain_Limited_Type (T, Exp);
11921 end if;
11922
11923 else
11924 -- Specialize error message according to kind of illegal
11925 -- initial expression. We check the Original_Node to cover
11926 -- cases where the initialization expression of an object
11927 -- declaration generated by the compiler has been rewritten
11928 -- (such as for dispatching calls).
11929
11930 if Nkind (Original_Node (Exp)) = N_Type_Conversion
11931 and then
11932 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
11933 then
11934 -- No error for internally-generated object declarations,
11935 -- which can come from build-in-place assignment statements.
11936
11937 if Nkind (Parent (Exp)) = N_Object_Declaration
11938 and then not Comes_From_Source
11939 (Defining_Identifier (Parent (Exp)))
11940 then
11941 null;
11942
11943 else
11944 Error_Msg_N
11945 ("illegal context for call to function with limited "
11946 & "result", Exp);
11947 end if;
11948
11949 else
11950 Error_Msg_N
11951 ("initialization of limited object requires aggregate or "
11952 & "function call", Exp);
11953 end if;
11954 end if;
11955 end if;
11956 end if;
11957
11958 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11959 -- set unless we can be sure that no range check is required.
11960
11961 if not Expander_Active
11962 and then Is_Scalar_Type (T)
11963 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11964 then
11965 Set_Do_Range_Check (Exp);
11966 end if;
11967 end Check_Initialization;
11968
11969 ----------------------
11970 -- Check_Interfaces --
11971 ----------------------
11972
11973 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11974 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11975
11976 Iface : Node_Id;
11977 Iface_Def : Node_Id;
11978 Iface_Typ : Entity_Id;
11979 Parent_Node : Node_Id;
11980
11981 Is_Task : Boolean := False;
11982 -- Set True if parent type or any progenitor is a task interface
11983
11984 Is_Protected : Boolean := False;
11985 -- Set True if parent type or any progenitor is a protected interface
11986
11987 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11988 -- Check that a progenitor is compatible with declaration. If an error
11989 -- message is output, it is posted on Error_Node.
11990
11991 ------------------
11992 -- Check_Ifaces --
11993 ------------------
11994
11995 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11996 Iface_Id : constant Entity_Id :=
11997 Defining_Identifier (Parent (Iface_Def));
11998 Type_Def : Node_Id;
11999
12000 begin
12001 if Nkind (N) = N_Private_Extension_Declaration then
12002 Type_Def := N;
12003 else
12004 Type_Def := Type_Definition (N);
12005 end if;
12006
12007 if Is_Task_Interface (Iface_Id) then
12008 Is_Task := True;
12009
12010 elsif Is_Protected_Interface (Iface_Id) then
12011 Is_Protected := True;
12012 end if;
12013
12014 if Is_Synchronized_Interface (Iface_Id) then
12015
12016 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12017 -- extension derived from a synchronized interface must explicitly
12018 -- be declared synchronized, because the full view will be a
12019 -- synchronized type.
12020
12021 if Nkind (N) = N_Private_Extension_Declaration then
12022 if not Synchronized_Present (N) then
12023 Error_Msg_NE
12024 ("private extension of& must be explicitly synchronized",
12025 N, Iface_Id);
12026 end if;
12027
12028 -- However, by 3.9.4(16/2), a full type that is a record extension
12029 -- is never allowed to derive from a synchronized interface (note
12030 -- that interfaces must be excluded from this check, because those
12031 -- are represented by derived type definitions in some cases).
12032
12033 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12034 and then not Interface_Present (Type_Definition (N))
12035 then
12036 Error_Msg_N ("record extension cannot derive from synchronized "
12037 & "interface", Error_Node);
12038 end if;
12039 end if;
12040
12041 -- Check that the characteristics of the progenitor are compatible
12042 -- with the explicit qualifier in the declaration.
12043 -- The check only applies to qualifiers that come from source.
12044 -- Limited_Present also appears in the declaration of corresponding
12045 -- records, and the check does not apply to them.
12046
12047 if Limited_Present (Type_Def)
12048 and then not
12049 Is_Concurrent_Record_Type (Defining_Identifier (N))
12050 then
12051 if Is_Limited_Interface (Parent_Type)
12052 and then not Is_Limited_Interface (Iface_Id)
12053 then
12054 Error_Msg_NE
12055 ("progenitor & must be limited interface",
12056 Error_Node, Iface_Id);
12057
12058 elsif
12059 (Task_Present (Iface_Def)
12060 or else Protected_Present (Iface_Def)
12061 or else Synchronized_Present (Iface_Def))
12062 and then Nkind (N) /= N_Private_Extension_Declaration
12063 and then not Error_Posted (N)
12064 then
12065 Error_Msg_NE
12066 ("progenitor & must be limited interface",
12067 Error_Node, Iface_Id);
12068 end if;
12069
12070 -- Protected interfaces can only inherit from limited, synchronized
12071 -- or protected interfaces.
12072
12073 elsif Nkind (N) = N_Full_Type_Declaration
12074 and then Protected_Present (Type_Def)
12075 then
12076 if Limited_Present (Iface_Def)
12077 or else Synchronized_Present (Iface_Def)
12078 or else Protected_Present (Iface_Def)
12079 then
12080 null;
12081
12082 elsif Task_Present (Iface_Def) then
12083 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12084 & "from task interface", Error_Node);
12085
12086 else
12087 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12088 & "from non-limited interface", Error_Node);
12089 end if;
12090
12091 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12092 -- limited and synchronized.
12093
12094 elsif Synchronized_Present (Type_Def) then
12095 if Limited_Present (Iface_Def)
12096 or else Synchronized_Present (Iface_Def)
12097 then
12098 null;
12099
12100 elsif Protected_Present (Iface_Def)
12101 and then Nkind (N) /= N_Private_Extension_Declaration
12102 then
12103 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12104 & "from protected interface", Error_Node);
12105
12106 elsif Task_Present (Iface_Def)
12107 and then Nkind (N) /= N_Private_Extension_Declaration
12108 then
12109 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12110 & "from task interface", Error_Node);
12111
12112 elsif not Is_Limited_Interface (Iface_Id) then
12113 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12114 & "from non-limited interface", Error_Node);
12115 end if;
12116
12117 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12118 -- synchronized or task interfaces.
12119
12120 elsif Nkind (N) = N_Full_Type_Declaration
12121 and then Task_Present (Type_Def)
12122 then
12123 if Limited_Present (Iface_Def)
12124 or else Synchronized_Present (Iface_Def)
12125 or else Task_Present (Iface_Def)
12126 then
12127 null;
12128
12129 elsif Protected_Present (Iface_Def) then
12130 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12131 & "protected interface", Error_Node);
12132
12133 else
12134 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12135 & "non-limited interface", Error_Node);
12136 end if;
12137 end if;
12138 end Check_Ifaces;
12139
12140 -- Start of processing for Check_Interfaces
12141
12142 begin
12143 if Is_Interface (Parent_Type) then
12144 if Is_Task_Interface (Parent_Type) then
12145 Is_Task := True;
12146
12147 elsif Is_Protected_Interface (Parent_Type) then
12148 Is_Protected := True;
12149 end if;
12150 end if;
12151
12152 if Nkind (N) = N_Private_Extension_Declaration then
12153
12154 -- Check that progenitors are compatible with declaration
12155
12156 Iface := First (Interface_List (Def));
12157 while Present (Iface) loop
12158 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12159
12160 Parent_Node := Parent (Base_Type (Iface_Typ));
12161 Iface_Def := Type_Definition (Parent_Node);
12162
12163 if not Is_Interface (Iface_Typ) then
12164 Diagnose_Interface (Iface, Iface_Typ);
12165 else
12166 Check_Ifaces (Iface_Def, Iface);
12167 end if;
12168
12169 Next (Iface);
12170 end loop;
12171
12172 if Is_Task and Is_Protected then
12173 Error_Msg_N
12174 ("type cannot derive from task and protected interface", N);
12175 end if;
12176
12177 return;
12178 end if;
12179
12180 -- Full type declaration of derived type.
12181 -- Check compatibility with parent if it is interface type
12182
12183 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12184 and then Is_Interface (Parent_Type)
12185 then
12186 Parent_Node := Parent (Parent_Type);
12187
12188 -- More detailed checks for interface varieties
12189
12190 Check_Ifaces
12191 (Iface_Def => Type_Definition (Parent_Node),
12192 Error_Node => Subtype_Indication (Type_Definition (N)));
12193 end if;
12194
12195 Iface := First (Interface_List (Def));
12196 while Present (Iface) loop
12197 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12198
12199 Parent_Node := Parent (Base_Type (Iface_Typ));
12200 Iface_Def := Type_Definition (Parent_Node);
12201
12202 if not Is_Interface (Iface_Typ) then
12203 Diagnose_Interface (Iface, Iface_Typ);
12204
12205 else
12206 -- "The declaration of a specific descendant of an interface
12207 -- type freezes the interface type" RM 13.14
12208
12209 Freeze_Before (N, Iface_Typ);
12210 Check_Ifaces (Iface_Def, Error_Node => Iface);
12211 end if;
12212
12213 Next (Iface);
12214 end loop;
12215
12216 if Is_Task and Is_Protected then
12217 Error_Msg_N
12218 ("type cannot derive from task and protected interface", N);
12219 end if;
12220 end Check_Interfaces;
12221
12222 ------------------------------------
12223 -- Check_Or_Process_Discriminants --
12224 ------------------------------------
12225
12226 -- If an incomplete or private type declaration was already given for the
12227 -- type, the discriminants may have already been processed if they were
12228 -- present on the incomplete declaration. In this case a full conformance
12229 -- check has been performed in Find_Type_Name, and we then recheck here
12230 -- some properties that can't be checked on the partial view alone.
12231 -- Otherwise we call Process_Discriminants.
12232
12233 procedure Check_Or_Process_Discriminants
12234 (N : Node_Id;
12235 T : Entity_Id;
12236 Prev : Entity_Id := Empty)
12237 is
12238 begin
12239 if Has_Discriminants (T) then
12240
12241 -- Discriminants are already set on T if they were already present
12242 -- on the partial view. Make them visible to component declarations.
12243
12244 declare
12245 D : Entity_Id;
12246 -- Discriminant on T (full view) referencing expr on partial view
12247
12248 Prev_D : Entity_Id;
12249 -- Entity of corresponding discriminant on partial view
12250
12251 New_D : Node_Id;
12252 -- Discriminant specification for full view, expression is
12253 -- the syntactic copy on full view (which has been checked for
12254 -- conformance with partial view), only used here to post error
12255 -- message.
12256
12257 begin
12258 D := First_Discriminant (T);
12259 New_D := First (Discriminant_Specifications (N));
12260 while Present (D) loop
12261 Prev_D := Current_Entity (D);
12262 Set_Current_Entity (D);
12263 Set_Is_Immediately_Visible (D);
12264 Set_Homonym (D, Prev_D);
12265
12266 -- Handle the case where there is an untagged partial view and
12267 -- the full view is tagged: must disallow discriminants with
12268 -- defaults, unless compiling for Ada 2012, which allows a
12269 -- limited tagged type to have defaulted discriminants (see
12270 -- AI05-0214). However, suppress error here if it was already
12271 -- reported on the default expression of the partial view.
12272
12273 if Is_Tagged_Type (T)
12274 and then Present (Expression (Parent (D)))
12275 and then (not Is_Limited_Type (Current_Scope)
12276 or else Ada_Version < Ada_2012)
12277 and then not Error_Posted (Expression (Parent (D)))
12278 then
12279 if Ada_Version >= Ada_2012 then
12280 Error_Msg_N
12281 ("discriminants of nonlimited tagged type cannot have "
12282 & "defaults",
12283 Expression (New_D));
12284 else
12285 Error_Msg_N
12286 ("discriminants of tagged type cannot have defaults",
12287 Expression (New_D));
12288 end if;
12289 end if;
12290
12291 -- Ada 2005 (AI-230): Access discriminant allowed in
12292 -- non-limited record types.
12293
12294 if Ada_Version < Ada_2005 then
12295
12296 -- This restriction gets applied to the full type here. It
12297 -- has already been applied earlier to the partial view.
12298
12299 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12300 end if;
12301
12302 Next_Discriminant (D);
12303 Next (New_D);
12304 end loop;
12305 end;
12306
12307 elsif Present (Discriminant_Specifications (N)) then
12308 Process_Discriminants (N, Prev);
12309 end if;
12310 end Check_Or_Process_Discriminants;
12311
12312 ----------------------
12313 -- Check_Real_Bound --
12314 ----------------------
12315
12316 procedure Check_Real_Bound (Bound : Node_Id) is
12317 begin
12318 if not Is_Real_Type (Etype (Bound)) then
12319 Error_Msg_N
12320 ("bound in real type definition must be of real type", Bound);
12321
12322 elsif not Is_OK_Static_Expression (Bound) then
12323 Flag_Non_Static_Expr
12324 ("non-static expression used for real type bound!", Bound);
12325
12326 else
12327 return;
12328 end if;
12329
12330 Rewrite
12331 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12332 Analyze (Bound);
12333 Resolve (Bound, Standard_Float);
12334 end Check_Real_Bound;
12335
12336 ------------------------------
12337 -- Complete_Private_Subtype --
12338 ------------------------------
12339
12340 procedure Complete_Private_Subtype
12341 (Priv : Entity_Id;
12342 Full : Entity_Id;
12343 Full_Base : Entity_Id;
12344 Related_Nod : Node_Id)
12345 is
12346 Save_Next_Entity : Entity_Id;
12347 Save_Homonym : Entity_Id;
12348
12349 begin
12350 -- Set semantic attributes for (implicit) private subtype completion.
12351 -- If the full type has no discriminants, then it is a copy of the
12352 -- full view of the base. Otherwise, it is a subtype of the base with
12353 -- a possible discriminant constraint. Save and restore the original
12354 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12355 -- not corrupt the entity chain.
12356
12357 Save_Next_Entity := Next_Entity (Full);
12358 Save_Homonym := Homonym (Priv);
12359
12360 if Is_Private_Type (Full_Base)
12361 or else Is_Record_Type (Full_Base)
12362 or else Is_Concurrent_Type (Full_Base)
12363 then
12364 Copy_Node (Priv, Full);
12365
12366 -- Note that the Etype of the full view is the same as the Etype of
12367 -- the partial view. In this fashion, the subtype has access to the
12368 -- correct view of the parent.
12369
12370 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12371 Set_Has_Unknown_Discriminants
12372 (Full, Has_Unknown_Discriminants (Full_Base));
12373 Set_First_Entity (Full, First_Entity (Full_Base));
12374 Set_Last_Entity (Full, Last_Entity (Full_Base));
12375
12376 -- If the underlying base type is constrained, we know that the
12377 -- full view of the subtype is constrained as well (the converse
12378 -- is not necessarily true).
12379
12380 if Is_Constrained (Full_Base) then
12381 Set_Is_Constrained (Full);
12382 end if;
12383
12384 else
12385 Copy_Node (Full_Base, Full);
12386
12387 -- The following subtlety with the Etype of the full view needs to be
12388 -- taken into account here. One could think that it must naturally be
12389 -- set to the base type of the full base:
12390
12391 -- Set_Etype (Full, Base_Type (Full_Base));
12392
12393 -- so that the full view becomes a subtype of the full base when the
12394 -- latter is a base type, which must for example happen when the full
12395 -- base is declared as derived type. That's also correct if the full
12396 -- base is declared as an array type, or a floating-point type, or a
12397 -- fixed-point type, or a signed integer type, as these declarations
12398 -- create an implicit base type and a first subtype so the Etype of
12399 -- the full views must be the implicit base type. But that's wrong
12400 -- if the full base is declared as an access type, or an enumeration
12401 -- type, or a modular integer type, as these declarations directly
12402 -- create a base type, i.e. with Etype pointing to itself. Moreover
12403 -- the full base being declared in the private part, i.e. when the
12404 -- views are swapped, the end result is that the Etype of the full
12405 -- base is set to its private view in this case and that we need to
12406 -- propagate this setting to the full view in order for the subtype
12407 -- to be compatible with the base type.
12408
12409 if Is_Base_Type (Full_Base)
12410 and then (Is_Derived_Type (Full_Base)
12411 or else Ekind (Full_Base) in Array_Kind
12412 or else Ekind (Full_Base) in Fixed_Point_Kind
12413 or else Ekind (Full_Base) in Float_Kind
12414 or else Ekind (Full_Base) in Signed_Integer_Kind)
12415 then
12416 Set_Etype (Full, Full_Base);
12417 end if;
12418
12419 Set_Chars (Full, Chars (Priv));
12420 Set_Sloc (Full, Sloc (Priv));
12421 Conditional_Delay (Full, Priv);
12422 end if;
12423
12424 Link_Entities (Full, Save_Next_Entity);
12425 Set_Homonym (Full, Save_Homonym);
12426 Set_Associated_Node_For_Itype (Full, Related_Nod);
12427
12428 -- Set common attributes for all subtypes: kind, convention, etc.
12429
12430 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12431 Set_Convention (Full, Convention (Full_Base));
12432 Set_Is_First_Subtype (Full, False);
12433 Set_Scope (Full, Scope (Priv));
12434 Set_Size_Info (Full, Full_Base);
12435 Set_RM_Size (Full, RM_Size (Full_Base));
12436 Set_Is_Itype (Full);
12437
12438 -- A subtype of a private-type-without-discriminants, whose full-view
12439 -- has discriminants with default expressions, is not constrained.
12440
12441 if not Has_Discriminants (Priv) then
12442 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12443
12444 if Has_Discriminants (Full_Base) then
12445 Set_Discriminant_Constraint
12446 (Full, Discriminant_Constraint (Full_Base));
12447
12448 -- The partial view may have been indefinite, the full view
12449 -- might not be.
12450
12451 Set_Has_Unknown_Discriminants
12452 (Full, Has_Unknown_Discriminants (Full_Base));
12453 end if;
12454 end if;
12455
12456 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12457 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12458
12459 -- Freeze the private subtype entity if its parent is delayed, and not
12460 -- already frozen. We skip this processing if the type is an anonymous
12461 -- subtype of a record component, or is the corresponding record of a
12462 -- protected type, since these are processed when the enclosing type
12463 -- is frozen. If the parent type is declared in a nested package then
12464 -- the freezing of the private and full views also happens later.
12465
12466 if not Is_Type (Scope (Full)) then
12467 if Is_Itype (Priv)
12468 and then In_Same_Source_Unit (Full, Full_Base)
12469 and then Scope (Full_Base) /= Scope (Full)
12470 then
12471 Set_Has_Delayed_Freeze (Full);
12472 Set_Has_Delayed_Freeze (Priv);
12473
12474 else
12475 Set_Has_Delayed_Freeze (Full,
12476 Has_Delayed_Freeze (Full_Base)
12477 and then not Is_Frozen (Full_Base));
12478 end if;
12479 end if;
12480
12481 Set_Freeze_Node (Full, Empty);
12482 Set_Is_Frozen (Full, False);
12483
12484 if Has_Discriminants (Full) then
12485 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12486 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12487
12488 if Has_Unknown_Discriminants (Full) then
12489 Set_Discriminant_Constraint (Full, No_Elist);
12490 end if;
12491 end if;
12492
12493 if Ekind (Full_Base) = E_Record_Type
12494 and then Has_Discriminants (Full_Base)
12495 and then Has_Discriminants (Priv) -- might not, if errors
12496 and then not Has_Unknown_Discriminants (Priv)
12497 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12498 then
12499 Create_Constrained_Components
12500 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12501
12502 -- If the full base is itself derived from private, build a congruent
12503 -- subtype of its underlying full view, for use by the back end.
12504
12505 elsif Is_Private_Type (Full_Base)
12506 and then Present (Underlying_Full_View (Full_Base))
12507 then
12508 declare
12509 Underlying_Full_Base : constant Entity_Id
12510 := Underlying_Full_View (Full_Base);
12511 Underlying_Full : constant Entity_Id
12512 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12513 begin
12514 Set_Is_Itype (Underlying_Full);
12515 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12516 Complete_Private_Subtype
12517 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12518 Set_Underlying_Full_View (Full, Underlying_Full);
12519 Set_Is_Underlying_Full_View (Underlying_Full);
12520 end;
12521
12522 elsif Is_Record_Type (Full_Base) then
12523
12524 -- Show Full is simply a renaming of Full_Base
12525
12526 Set_Cloned_Subtype (Full, Full_Base);
12527 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12528
12529 -- Propagate predicates
12530
12531 Propagate_Predicate_Attributes (Full, Full_Base);
12532 end if;
12533
12534 -- It is unsafe to share the bounds of a scalar type, because the Itype
12535 -- is elaborated on demand, and if a bound is nonstatic, then different
12536 -- orders of elaboration in different units will lead to different
12537 -- external symbols.
12538
12539 if Is_Scalar_Type (Full_Base) then
12540 Set_Scalar_Range (Full,
12541 Make_Range (Sloc (Related_Nod),
12542 Low_Bound =>
12543 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12544 High_Bound =>
12545 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12546
12547 -- This completion inherits the bounds of the full parent, but if
12548 -- the parent is an unconstrained floating point type, so is the
12549 -- completion.
12550
12551 if Is_Floating_Point_Type (Full_Base) then
12552 Set_Includes_Infinities
12553 (Scalar_Range (Full), Has_Infinities (Full_Base));
12554 end if;
12555 end if;
12556
12557 -- ??? It seems that a lot of fields are missing that should be copied
12558 -- from Full_Base to Full. Here are some that are introduced in a
12559 -- non-disruptive way but a cleanup is necessary.
12560
12561 if Is_Tagged_Type (Full_Base) then
12562 Set_Is_Tagged_Type (Full);
12563 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12564
12565 Set_Direct_Primitive_Operations
12566 (Full, Direct_Primitive_Operations (Full_Base));
12567 Set_No_Tagged_Streams_Pragma
12568 (Full, No_Tagged_Streams_Pragma (Full_Base));
12569
12570 if Is_Interface (Full_Base) then
12571 Set_Is_Interface (Full);
12572 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
12573 end if;
12574
12575 -- Inherit class_wide type of full_base in case the partial view was
12576 -- not tagged. Otherwise it has already been created when the private
12577 -- subtype was analyzed.
12578
12579 if No (Class_Wide_Type (Full)) then
12580 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12581 end if;
12582
12583 -- If this is a subtype of a protected or task type, constrain its
12584 -- corresponding record, unless this is a subtype without constraints,
12585 -- i.e. a simple renaming as with an actual subtype in an instance.
12586
12587 elsif Is_Concurrent_Type (Full_Base) then
12588 if Has_Discriminants (Full)
12589 and then Present (Corresponding_Record_Type (Full_Base))
12590 and then
12591 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12592 then
12593 Set_Corresponding_Record_Type (Full,
12594 Constrain_Corresponding_Record
12595 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12596
12597 else
12598 Set_Corresponding_Record_Type (Full,
12599 Corresponding_Record_Type (Full_Base));
12600 end if;
12601 end if;
12602
12603 -- Link rep item chain, and also setting of Has_Predicates from private
12604 -- subtype to full subtype, since we will need these on the full subtype
12605 -- to create the predicate function. Note that the full subtype may
12606 -- already have rep items, inherited from the full view of the base
12607 -- type, so we must be sure not to overwrite these entries.
12608
12609 declare
12610 Append : Boolean;
12611 Item : Node_Id;
12612 Next_Item : Node_Id;
12613 Priv_Item : Node_Id;
12614
12615 begin
12616 Item := First_Rep_Item (Full);
12617 Priv_Item := First_Rep_Item (Priv);
12618
12619 -- If no existing rep items on full type, we can just link directly
12620 -- to the list of items on the private type, if any exist.. Same if
12621 -- the rep items are only those inherited from the base
12622
12623 if (No (Item)
12624 or else Nkind (Item) /= N_Aspect_Specification
12625 or else Entity (Item) = Full_Base)
12626 and then Present (First_Rep_Item (Priv))
12627 then
12628 Set_First_Rep_Item (Full, Priv_Item);
12629
12630 -- Otherwise, search to the end of items currently linked to the full
12631 -- subtype and append the private items to the end. However, if Priv
12632 -- and Full already have the same list of rep items, then the append
12633 -- is not done, as that would create a circularity.
12634 --
12635 -- The partial view may have a predicate and the rep item lists of
12636 -- both views agree when inherited from the same ancestor. In that
12637 -- case, simply propagate the list from one view to the other.
12638 -- A more complex analysis needed here ???
12639
12640 elsif Present (Priv_Item)
12641 and then Item = Next_Rep_Item (Priv_Item)
12642 then
12643 Set_First_Rep_Item (Full, Priv_Item);
12644
12645 elsif Item /= Priv_Item then
12646 Append := True;
12647 loop
12648 Next_Item := Next_Rep_Item (Item);
12649 exit when No (Next_Item);
12650 Item := Next_Item;
12651
12652 -- If the private view has aspect specifications, the full view
12653 -- inherits them. Since these aspects may already have been
12654 -- attached to the full view during derivation, do not append
12655 -- them if already present.
12656
12657 if Item = First_Rep_Item (Priv) then
12658 Append := False;
12659 exit;
12660 end if;
12661 end loop;
12662
12663 -- And link the private type items at the end of the chain
12664
12665 if Append then
12666 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12667 end if;
12668 end if;
12669 end;
12670
12671 -- Make sure Has_Predicates is set on full type if it is set on the
12672 -- private type. Note that it may already be set on the full type and
12673 -- if so, we don't want to unset it. Similarly, propagate information
12674 -- about delayed aspects, because the corresponding pragmas must be
12675 -- analyzed when one of the views is frozen. This last step is needed
12676 -- in particular when the full type is a scalar type for which an
12677 -- anonymous base type is constructed.
12678
12679 -- The predicate functions are generated either at the freeze point
12680 -- of the type or at the end of the visible part, and we must avoid
12681 -- generating them twice.
12682
12683 Propagate_Predicate_Attributes (Full, Priv);
12684
12685 if Has_Delayed_Aspects (Priv) then
12686 Set_Has_Delayed_Aspects (Full);
12687 end if;
12688 end Complete_Private_Subtype;
12689
12690 ----------------------------
12691 -- Constant_Redeclaration --
12692 ----------------------------
12693
12694 procedure Constant_Redeclaration
12695 (Id : Entity_Id;
12696 N : Node_Id;
12697 T : out Entity_Id)
12698 is
12699 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12700 Obj_Def : constant Node_Id := Object_Definition (N);
12701 New_T : Entity_Id;
12702
12703 procedure Check_Possible_Deferred_Completion
12704 (Prev_Id : Entity_Id;
12705 Prev_Obj_Def : Node_Id;
12706 Curr_Obj_Def : Node_Id);
12707 -- Determine whether the two object definitions describe the partial
12708 -- and the full view of a constrained deferred constant. Generate
12709 -- a subtype for the full view and verify that it statically matches
12710 -- the subtype of the partial view.
12711
12712 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12713 -- If deferred constant is an access type initialized with an allocator,
12714 -- check whether there is an illegal recursion in the definition,
12715 -- through a default value of some record subcomponent. This is normally
12716 -- detected when generating init procs, but requires this additional
12717 -- mechanism when expansion is disabled.
12718
12719 ----------------------------------------
12720 -- Check_Possible_Deferred_Completion --
12721 ----------------------------------------
12722
12723 procedure Check_Possible_Deferred_Completion
12724 (Prev_Id : Entity_Id;
12725 Prev_Obj_Def : Node_Id;
12726 Curr_Obj_Def : Node_Id)
12727 is
12728 begin
12729 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12730 and then Present (Constraint (Prev_Obj_Def))
12731 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12732 and then Present (Constraint (Curr_Obj_Def))
12733 then
12734 declare
12735 Loc : constant Source_Ptr := Sloc (N);
12736 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12737 Decl : constant Node_Id :=
12738 Make_Subtype_Declaration (Loc,
12739 Defining_Identifier => Def_Id,
12740 Subtype_Indication =>
12741 Relocate_Node (Curr_Obj_Def));
12742
12743 begin
12744 Insert_Before_And_Analyze (N, Decl);
12745 Set_Etype (Id, Def_Id);
12746
12747 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12748 Error_Msg_Sloc := Sloc (Prev_Id);
12749 Error_Msg_N ("subtype does not statically match deferred "
12750 & "declaration #", N);
12751 end if;
12752 end;
12753 end if;
12754 end Check_Possible_Deferred_Completion;
12755
12756 ---------------------------------
12757 -- Check_Recursive_Declaration --
12758 ---------------------------------
12759
12760 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12761 Comp : Entity_Id;
12762
12763 begin
12764 if Is_Record_Type (Typ) then
12765 Comp := First_Component (Typ);
12766 while Present (Comp) loop
12767 if Comes_From_Source (Comp) then
12768 if Present (Expression (Parent (Comp)))
12769 and then Is_Entity_Name (Expression (Parent (Comp)))
12770 and then Entity (Expression (Parent (Comp))) = Prev
12771 then
12772 Error_Msg_Sloc := Sloc (Parent (Comp));
12773 Error_Msg_NE
12774 ("illegal circularity with declaration for & #",
12775 N, Comp);
12776 return;
12777
12778 elsif Is_Record_Type (Etype (Comp)) then
12779 Check_Recursive_Declaration (Etype (Comp));
12780 end if;
12781 end if;
12782
12783 Next_Component (Comp);
12784 end loop;
12785 end if;
12786 end Check_Recursive_Declaration;
12787
12788 -- Start of processing for Constant_Redeclaration
12789
12790 begin
12791 if Nkind (Parent (Prev)) = N_Object_Declaration then
12792 if Nkind (Object_Definition
12793 (Parent (Prev))) = N_Subtype_Indication
12794 then
12795 -- Find type of new declaration. The constraints of the two
12796 -- views must match statically, but there is no point in
12797 -- creating an itype for the full view.
12798
12799 if Nkind (Obj_Def) = N_Subtype_Indication then
12800 Find_Type (Subtype_Mark (Obj_Def));
12801 New_T := Entity (Subtype_Mark (Obj_Def));
12802
12803 else
12804 Find_Type (Obj_Def);
12805 New_T := Entity (Obj_Def);
12806 end if;
12807
12808 T := Etype (Prev);
12809
12810 else
12811 -- The full view may impose a constraint, even if the partial
12812 -- view does not, so construct the subtype.
12813
12814 New_T := Find_Type_Of_Object (Obj_Def, N);
12815 T := New_T;
12816 end if;
12817
12818 else
12819 -- Current declaration is illegal, diagnosed below in Enter_Name
12820
12821 T := Empty;
12822 New_T := Any_Type;
12823 end if;
12824
12825 -- If previous full declaration or a renaming declaration exists, or if
12826 -- a homograph is present, let Enter_Name handle it, either with an
12827 -- error or with the removal of an overridden implicit subprogram.
12828 -- The previous one is a full declaration if it has an expression
12829 -- (which in the case of an aggregate is indicated by the Init flag).
12830
12831 if Ekind (Prev) /= E_Constant
12832 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12833 or else Present (Expression (Parent (Prev)))
12834 or else Has_Init_Expression (Parent (Prev))
12835 or else Present (Full_View (Prev))
12836 then
12837 Enter_Name (Id);
12838
12839 -- Verify that types of both declarations match, or else that both types
12840 -- are anonymous access types whose designated subtypes statically match
12841 -- (as allowed in Ada 2005 by AI-385).
12842
12843 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12844 and then
12845 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12846 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12847 or else Is_Access_Constant (Etype (New_T)) /=
12848 Is_Access_Constant (Etype (Prev))
12849 or else Can_Never_Be_Null (Etype (New_T)) /=
12850 Can_Never_Be_Null (Etype (Prev))
12851 or else Null_Exclusion_Present (Parent (Prev)) /=
12852 Null_Exclusion_Present (Parent (Id))
12853 or else not Subtypes_Statically_Match
12854 (Designated_Type (Etype (Prev)),
12855 Designated_Type (Etype (New_T))))
12856 then
12857 Error_Msg_Sloc := Sloc (Prev);
12858 Error_Msg_N ("type does not match declaration#", N);
12859 Set_Full_View (Prev, Id);
12860 Set_Etype (Id, Any_Type);
12861
12862 -- A deferred constant whose type is an anonymous array is always
12863 -- illegal (unless imported). A detailed error message might be
12864 -- helpful for Ada beginners.
12865
12866 if Nkind (Object_Definition (Parent (Prev)))
12867 = N_Constrained_Array_Definition
12868 and then Nkind (Object_Definition (N))
12869 = N_Constrained_Array_Definition
12870 then
12871 Error_Msg_N ("\each anonymous array is a distinct type", N);
12872 Error_Msg_N ("a deferred constant must have a named type",
12873 Object_Definition (Parent (Prev)));
12874 end if;
12875
12876 elsif
12877 Null_Exclusion_Present (Parent (Prev))
12878 and then not Null_Exclusion_Present (N)
12879 then
12880 Error_Msg_Sloc := Sloc (Prev);
12881 Error_Msg_N ("null-exclusion does not match declaration#", N);
12882 Set_Full_View (Prev, Id);
12883 Set_Etype (Id, Any_Type);
12884
12885 -- If so, process the full constant declaration
12886
12887 else
12888 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12889 -- the deferred declaration is constrained, then the subtype defined
12890 -- by the subtype_indication in the full declaration shall match it
12891 -- statically.
12892
12893 Check_Possible_Deferred_Completion
12894 (Prev_Id => Prev,
12895 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12896 Curr_Obj_Def => Obj_Def);
12897
12898 Set_Full_View (Prev, Id);
12899 Set_Is_Public (Id, Is_Public (Prev));
12900 Set_Is_Internal (Id);
12901 Append_Entity (Id, Current_Scope);
12902
12903 -- Check ALIASED present if present before (RM 7.4(7))
12904
12905 if Is_Aliased (Prev)
12906 and then not Aliased_Present (N)
12907 then
12908 Error_Msg_Sloc := Sloc (Prev);
12909 Error_Msg_N ("ALIASED required (see declaration #)", N);
12910 end if;
12911
12912 -- Check that placement is in private part and that the incomplete
12913 -- declaration appeared in the visible part.
12914
12915 if Ekind (Current_Scope) = E_Package
12916 and then not In_Private_Part (Current_Scope)
12917 then
12918 Error_Msg_Sloc := Sloc (Prev);
12919 Error_Msg_N
12920 ("full constant for declaration # must be in private part", N);
12921
12922 elsif Ekind (Current_Scope) = E_Package
12923 and then
12924 List_Containing (Parent (Prev)) /=
12925 Visible_Declarations (Package_Specification (Current_Scope))
12926 then
12927 Error_Msg_N
12928 ("deferred constant must be declared in visible part",
12929 Parent (Prev));
12930 end if;
12931
12932 if Is_Access_Type (T)
12933 and then Nkind (Expression (N)) = N_Allocator
12934 then
12935 Check_Recursive_Declaration (Designated_Type (T));
12936 end if;
12937
12938 -- A deferred constant is a visible entity. If type has invariants,
12939 -- verify that the initial value satisfies them. This is not done in
12940 -- GNATprove mode, as GNATprove handles invariant checks itself.
12941
12942 if Has_Invariants (T)
12943 and then Present (Invariant_Procedure (T))
12944 and then not GNATprove_Mode
12945 then
12946 Insert_After (N,
12947 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12948 end if;
12949 end if;
12950 end Constant_Redeclaration;
12951
12952 ----------------------
12953 -- Constrain_Access --
12954 ----------------------
12955
12956 procedure Constrain_Access
12957 (Def_Id : in out Entity_Id;
12958 S : Node_Id;
12959 Related_Nod : Node_Id)
12960 is
12961 T : constant Entity_Id := Entity (Subtype_Mark (S));
12962 Desig_Type : constant Entity_Id := Designated_Type (T);
12963 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12964 Constraint_OK : Boolean := True;
12965
12966 begin
12967 if Is_Array_Type (Desig_Type) then
12968 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12969
12970 elsif (Is_Record_Type (Desig_Type)
12971 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12972 and then not Is_Constrained (Desig_Type)
12973 then
12974 -- If this is a constrained access definition for a record
12975 -- component, we leave the type as an unconstrained access,
12976 -- and mark the component so that its actual type is built
12977 -- at a point of use (e.g., an assignment statement). This
12978 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
12979
12980 if Desig_Type = Current_Scope
12981 and then No (Def_Id)
12982 then
12983 Desig_Subtype :=
12984 Create_Itype
12985 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
12986 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12987 Def_Id := Entity (Subtype_Mark (S));
12988
12989 -- We indicate that the component has a per-object constraint
12990 -- for treatment at a point of use, even though the constraint
12991 -- may be independent of discriminants of the enclosing type.
12992
12993 if Nkind (Related_Nod) = N_Component_Declaration then
12994 Set_Has_Per_Object_Constraint
12995 (Defining_Identifier (Related_Nod));
12996 end if;
12997
12998 -- This call added to ensure that the constraint is analyzed
12999 -- (needed for a B test). Note that we still return early from
13000 -- this procedure to avoid recursive processing.
13001
13002 Constrain_Discriminated_Type
13003 (Desig_Subtype, S, Related_Nod, For_Access => True);
13004 return;
13005 end if;
13006
13007 -- Enforce rule that the constraint is illegal if there is an
13008 -- unconstrained view of the designated type. This means that the
13009 -- partial view (either a private type declaration or a derivation
13010 -- from a private type) has no discriminants. (Defect Report
13011 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13012
13013 -- Rule updated for Ada 2005: The private type is said to have
13014 -- a constrained partial view, given that objects of the type
13015 -- can be declared. Furthermore, the rule applies to all access
13016 -- types, unlike the rule concerning default discriminants (see
13017 -- RM 3.7.1(7/3))
13018
13019 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13020 and then Has_Private_Declaration (Desig_Type)
13021 and then In_Open_Scopes (Scope (Desig_Type))
13022 and then Has_Discriminants (Desig_Type)
13023 then
13024 declare
13025 Pack : constant Node_Id :=
13026 Unit_Declaration_Node (Scope (Desig_Type));
13027 Decls : List_Id;
13028 Decl : Node_Id;
13029
13030 begin
13031 if Nkind (Pack) = N_Package_Declaration then
13032 Decls := Visible_Declarations (Specification (Pack));
13033 Decl := First (Decls);
13034 while Present (Decl) loop
13035 if (Nkind (Decl) = N_Private_Type_Declaration
13036 and then Chars (Defining_Identifier (Decl)) =
13037 Chars (Desig_Type))
13038
13039 or else
13040 (Nkind (Decl) = N_Full_Type_Declaration
13041 and then
13042 Chars (Defining_Identifier (Decl)) =
13043 Chars (Desig_Type)
13044 and then Is_Derived_Type (Desig_Type)
13045 and then
13046 Has_Private_Declaration (Etype (Desig_Type)))
13047 then
13048 if No (Discriminant_Specifications (Decl)) then
13049 Error_Msg_N
13050 ("cannot constrain access type if designated "
13051 & "type has constrained partial view", S);
13052 end if;
13053
13054 exit;
13055 end if;
13056
13057 Next (Decl);
13058 end loop;
13059 end if;
13060 end;
13061 end if;
13062
13063 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13064 For_Access => True);
13065
13066 elsif Is_Concurrent_Type (Desig_Type)
13067 and then not Is_Constrained (Desig_Type)
13068 then
13069 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13070
13071 else
13072 Error_Msg_N ("invalid constraint on access type", S);
13073
13074 -- We simply ignore an invalid constraint
13075
13076 Desig_Subtype := Desig_Type;
13077 Constraint_OK := False;
13078 end if;
13079
13080 if No (Def_Id) then
13081 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13082 else
13083 Set_Ekind (Def_Id, E_Access_Subtype);
13084 end if;
13085
13086 if Constraint_OK then
13087 Set_Etype (Def_Id, Base_Type (T));
13088
13089 if Is_Private_Type (Desig_Type) then
13090 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13091 end if;
13092 else
13093 Set_Etype (Def_Id, Any_Type);
13094 end if;
13095
13096 Set_Size_Info (Def_Id, T);
13097 Set_Is_Constrained (Def_Id, Constraint_OK);
13098 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13099 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13100 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13101
13102 Conditional_Delay (Def_Id, T);
13103
13104 -- AI-363 : Subtypes of general access types whose designated types have
13105 -- default discriminants are disallowed. In instances, the rule has to
13106 -- be checked against the actual, of which T is the subtype. In a
13107 -- generic body, the rule is checked assuming that the actual type has
13108 -- defaulted discriminants.
13109
13110 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13111 if Ekind (Base_Type (T)) = E_General_Access_Type
13112 and then Has_Defaulted_Discriminants (Desig_Type)
13113 then
13114 if Ada_Version < Ada_2005 then
13115 Error_Msg_N
13116 ("access subtype of general access type would not " &
13117 "be allowed in Ada 2005?y?", S);
13118 else
13119 Error_Msg_N
13120 ("access subtype of general access type not allowed", S);
13121 end if;
13122
13123 Error_Msg_N ("\discriminants have defaults", S);
13124
13125 elsif Is_Access_Type (T)
13126 and then Is_Generic_Type (Desig_Type)
13127 and then Has_Discriminants (Desig_Type)
13128 and then In_Package_Body (Current_Scope)
13129 then
13130 if Ada_Version < Ada_2005 then
13131 Error_Msg_N
13132 ("access subtype would not be allowed in generic body "
13133 & "in Ada 2005?y?", S);
13134 else
13135 Error_Msg_N
13136 ("access subtype not allowed in generic body", S);
13137 end if;
13138
13139 Error_Msg_N
13140 ("\designated type is a discriminated formal", S);
13141 end if;
13142 end if;
13143 end Constrain_Access;
13144
13145 ---------------------
13146 -- Constrain_Array --
13147 ---------------------
13148
13149 procedure Constrain_Array
13150 (Def_Id : in out Entity_Id;
13151 SI : Node_Id;
13152 Related_Nod : Node_Id;
13153 Related_Id : Entity_Id;
13154 Suffix : Character)
13155 is
13156 C : constant Node_Id := Constraint (SI);
13157 Number_Of_Constraints : Nat := 0;
13158 Index : Node_Id;
13159 S, T : Entity_Id;
13160 Constraint_OK : Boolean := True;
13161
13162 begin
13163 T := Entity (Subtype_Mark (SI));
13164
13165 if Is_Access_Type (T) then
13166 T := Designated_Type (T);
13167 end if;
13168
13169 -- If an index constraint follows a subtype mark in a subtype indication
13170 -- then the type or subtype denoted by the subtype mark must not already
13171 -- impose an index constraint. The subtype mark must denote either an
13172 -- unconstrained array type or an access type whose designated type
13173 -- is such an array type... (RM 3.6.1)
13174
13175 if Is_Constrained (T) then
13176 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13177 Constraint_OK := False;
13178
13179 else
13180 S := First (Constraints (C));
13181 while Present (S) loop
13182 Number_Of_Constraints := Number_Of_Constraints + 1;
13183 Next (S);
13184 end loop;
13185
13186 -- In either case, the index constraint must provide a discrete
13187 -- range for each index of the array type and the type of each
13188 -- discrete range must be the same as that of the corresponding
13189 -- index. (RM 3.6.1)
13190
13191 if Number_Of_Constraints /= Number_Dimensions (T) then
13192 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13193 Constraint_OK := False;
13194
13195 else
13196 S := First (Constraints (C));
13197 Index := First_Index (T);
13198 Analyze (Index);
13199
13200 -- Apply constraints to each index type
13201
13202 for J in 1 .. Number_Of_Constraints loop
13203 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13204 Next (Index);
13205 Next (S);
13206 end loop;
13207
13208 end if;
13209 end if;
13210
13211 if No (Def_Id) then
13212 Def_Id :=
13213 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13214 Set_Parent (Def_Id, Related_Nod);
13215
13216 else
13217 Set_Ekind (Def_Id, E_Array_Subtype);
13218 end if;
13219
13220 Set_Size_Info (Def_Id, (T));
13221 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13222 Set_Etype (Def_Id, Base_Type (T));
13223
13224 if Constraint_OK then
13225 Set_First_Index (Def_Id, First (Constraints (C)));
13226 else
13227 Set_First_Index (Def_Id, First_Index (T));
13228 end if;
13229
13230 Set_Is_Constrained (Def_Id, True);
13231 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13232 Set_Is_Independent (Def_Id, Is_Independent (T));
13233 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13234
13235 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13236 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13237
13238 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13239 -- We need to initialize the attribute because if Def_Id is previously
13240 -- analyzed through a limited_with clause, it will have the attributes
13241 -- of an incomplete type, one of which is an Elist that overlaps the
13242 -- Packed_Array_Impl_Type field.
13243
13244 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13245
13246 -- Build a freeze node if parent still needs one. Also make sure that
13247 -- the Depends_On_Private status is set because the subtype will need
13248 -- reprocessing at the time the base type does, and also we must set a
13249 -- conditional delay.
13250
13251 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13252 Conditional_Delay (Def_Id, T);
13253 end Constrain_Array;
13254
13255 ------------------------------
13256 -- Constrain_Component_Type --
13257 ------------------------------
13258
13259 function Constrain_Component_Type
13260 (Comp : Entity_Id;
13261 Constrained_Typ : Entity_Id;
13262 Related_Node : Node_Id;
13263 Typ : Entity_Id;
13264 Constraints : Elist_Id) return Entity_Id
13265 is
13266 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13267 Compon_Type : constant Entity_Id := Etype (Comp);
13268
13269 function Build_Constrained_Array_Type
13270 (Old_Type : Entity_Id) return Entity_Id;
13271 -- If Old_Type is an array type, one of whose indexes is constrained
13272 -- by a discriminant, build an Itype whose constraint replaces the
13273 -- discriminant with its value in the constraint.
13274
13275 function Build_Constrained_Discriminated_Type
13276 (Old_Type : Entity_Id) return Entity_Id;
13277 -- Ditto for record components. Handle the case where the constraint
13278 -- is a conversion of the discriminant value, introduced during
13279 -- expansion.
13280
13281 function Build_Constrained_Access_Type
13282 (Old_Type : Entity_Id) return Entity_Id;
13283 -- Ditto for access types. Makes use of previous two functions, to
13284 -- constrain designated type.
13285
13286 function Is_Discriminant (Expr : Node_Id) return Boolean;
13287 -- Returns True if Expr is a discriminant
13288
13289 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13290 -- Find the value of a discriminant named by Discr_Expr in Constraints
13291
13292 -----------------------------------
13293 -- Build_Constrained_Access_Type --
13294 -----------------------------------
13295
13296 function Build_Constrained_Access_Type
13297 (Old_Type : Entity_Id) return Entity_Id
13298 is
13299 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13300 Itype : Entity_Id;
13301 Desig_Subtype : Entity_Id;
13302 Scop : Entity_Id;
13303
13304 begin
13305 -- If the original access type was not embedded in the enclosing
13306 -- type definition, there is no need to produce a new access
13307 -- subtype. In fact every access type with an explicit constraint
13308 -- generates an itype whose scope is the enclosing record.
13309
13310 if not Is_Type (Scope (Old_Type)) then
13311 return Old_Type;
13312
13313 elsif Is_Array_Type (Desig_Type) then
13314 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13315
13316 elsif Has_Discriminants (Desig_Type) then
13317
13318 -- This may be an access type to an enclosing record type for
13319 -- which we are constructing the constrained components. Return
13320 -- the enclosing record subtype. This is not always correct,
13321 -- but avoids infinite recursion. ???
13322
13323 Desig_Subtype := Any_Type;
13324
13325 for J in reverse 0 .. Scope_Stack.Last loop
13326 Scop := Scope_Stack.Table (J).Entity;
13327
13328 if Is_Type (Scop)
13329 and then Base_Type (Scop) = Base_Type (Desig_Type)
13330 then
13331 Desig_Subtype := Scop;
13332 end if;
13333
13334 exit when not Is_Type (Scop);
13335 end loop;
13336
13337 if Desig_Subtype = Any_Type then
13338 Desig_Subtype :=
13339 Build_Constrained_Discriminated_Type (Desig_Type);
13340 end if;
13341
13342 else
13343 return Old_Type;
13344 end if;
13345
13346 if Desig_Subtype /= Desig_Type then
13347
13348 -- The Related_Node better be here or else we won't be able
13349 -- to attach new itypes to a node in the tree.
13350
13351 pragma Assert (Present (Related_Node));
13352
13353 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13354
13355 Set_Etype (Itype, Base_Type (Old_Type));
13356 Set_Size_Info (Itype, (Old_Type));
13357 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13358 Set_Depends_On_Private (Itype, Has_Private_Component
13359 (Old_Type));
13360 Set_Is_Access_Constant (Itype, Is_Access_Constant
13361 (Old_Type));
13362
13363 -- The new itype needs freezing when it depends on a not frozen
13364 -- type and the enclosing subtype needs freezing.
13365
13366 if Has_Delayed_Freeze (Constrained_Typ)
13367 and then not Is_Frozen (Constrained_Typ)
13368 then
13369 Conditional_Delay (Itype, Base_Type (Old_Type));
13370 end if;
13371
13372 return Itype;
13373
13374 else
13375 return Old_Type;
13376 end if;
13377 end Build_Constrained_Access_Type;
13378
13379 ----------------------------------
13380 -- Build_Constrained_Array_Type --
13381 ----------------------------------
13382
13383 function Build_Constrained_Array_Type
13384 (Old_Type : Entity_Id) return Entity_Id
13385 is
13386 Lo_Expr : Node_Id;
13387 Hi_Expr : Node_Id;
13388 Old_Index : Node_Id;
13389 Range_Node : Node_Id;
13390 Constr_List : List_Id;
13391
13392 Need_To_Create_Itype : Boolean := False;
13393
13394 begin
13395 Old_Index := First_Index (Old_Type);
13396 while Present (Old_Index) loop
13397 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13398
13399 if Is_Discriminant (Lo_Expr)
13400 or else
13401 Is_Discriminant (Hi_Expr)
13402 then
13403 Need_To_Create_Itype := True;
13404 exit;
13405 end if;
13406
13407 Next_Index (Old_Index);
13408 end loop;
13409
13410 if Need_To_Create_Itype then
13411 Constr_List := New_List;
13412
13413 Old_Index := First_Index (Old_Type);
13414 while Present (Old_Index) loop
13415 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13416
13417 if Is_Discriminant (Lo_Expr) then
13418 Lo_Expr := Get_Discr_Value (Lo_Expr);
13419 end if;
13420
13421 if Is_Discriminant (Hi_Expr) then
13422 Hi_Expr := Get_Discr_Value (Hi_Expr);
13423 end if;
13424
13425 Range_Node :=
13426 Make_Range
13427 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13428
13429 Append (Range_Node, To => Constr_List);
13430
13431 Next_Index (Old_Index);
13432 end loop;
13433
13434 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13435
13436 else
13437 return Old_Type;
13438 end if;
13439 end Build_Constrained_Array_Type;
13440
13441 ------------------------------------------
13442 -- Build_Constrained_Discriminated_Type --
13443 ------------------------------------------
13444
13445 function Build_Constrained_Discriminated_Type
13446 (Old_Type : Entity_Id) return Entity_Id
13447 is
13448 Expr : Node_Id;
13449 Constr_List : List_Id;
13450 Old_Constraint : Elmt_Id;
13451
13452 Need_To_Create_Itype : Boolean := False;
13453
13454 begin
13455 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13456 while Present (Old_Constraint) loop
13457 Expr := Node (Old_Constraint);
13458
13459 if Is_Discriminant (Expr) then
13460 Need_To_Create_Itype := True;
13461 exit;
13462
13463 -- After expansion of discriminated task types, the value
13464 -- of the discriminant may be converted to a run-time type
13465 -- for restricted run-times. Propagate the value of the
13466 -- discriminant as well, so that e.g. the secondary stack
13467 -- component has a static constraint. Necessary for LLVM.
13468
13469 elsif Nkind (Expr) = N_Type_Conversion
13470 and then Is_Discriminant (Expression (Expr))
13471 then
13472 Need_To_Create_Itype := True;
13473 exit;
13474 end if;
13475
13476 Next_Elmt (Old_Constraint);
13477 end loop;
13478
13479 if Need_To_Create_Itype then
13480 Constr_List := New_List;
13481
13482 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13483 while Present (Old_Constraint) loop
13484 Expr := Node (Old_Constraint);
13485
13486 if Is_Discriminant (Expr) then
13487 Expr := Get_Discr_Value (Expr);
13488
13489 elsif Nkind (Expr) = N_Type_Conversion
13490 and then Is_Discriminant (Expression (Expr))
13491 then
13492 Expr := New_Copy_Tree (Expr);
13493 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13494 end if;
13495
13496 Append (New_Copy_Tree (Expr), To => Constr_List);
13497
13498 Next_Elmt (Old_Constraint);
13499 end loop;
13500
13501 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13502
13503 else
13504 return Old_Type;
13505 end if;
13506 end Build_Constrained_Discriminated_Type;
13507
13508 ---------------------
13509 -- Get_Discr_Value --
13510 ---------------------
13511
13512 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
13513 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
13514 -- Entity of a discriminant that appear as a standalone expression in
13515 -- the constraint of a component.
13516
13517 D : Entity_Id;
13518 E : Elmt_Id;
13519
13520 begin
13521 -- The discriminant may be declared for the type, in which case we
13522 -- find it by iterating over the list of discriminants. If the
13523 -- discriminant is inherited from a parent type, it appears as the
13524 -- corresponding discriminant of the current type. This will be the
13525 -- case when constraining an inherited component whose constraint is
13526 -- given by a discriminant of the parent.
13527
13528 D := First_Discriminant (Typ);
13529 E := First_Elmt (Constraints);
13530
13531 while Present (D) loop
13532 if D = Discr_Id
13533 or else D = CR_Discriminant (Discr_Id)
13534 or else Corresponding_Discriminant (D) = Discr_Id
13535 then
13536 return Node (E);
13537 end if;
13538
13539 Next_Discriminant (D);
13540 Next_Elmt (E);
13541 end loop;
13542
13543 -- The Corresponding_Discriminant mechanism is incomplete, because
13544 -- the correspondence between new and old discriminants is not one
13545 -- to one: one new discriminant can constrain several old ones. In
13546 -- that case, scan sequentially the stored_constraint, the list of
13547 -- discriminants of the parents, and the constraints.
13548
13549 -- Previous code checked for the present of the Stored_Constraint
13550 -- list for the derived type, but did not use it at all. Should it
13551 -- be present when the component is a discriminated task type?
13552
13553 if Is_Derived_Type (Typ)
13554 and then Scope (Discr_Id) = Etype (Typ)
13555 then
13556 D := First_Discriminant (Etype (Typ));
13557 E := First_Elmt (Constraints);
13558 while Present (D) loop
13559 if D = Discr_Id then
13560 return Node (E);
13561 end if;
13562
13563 Next_Discriminant (D);
13564 Next_Elmt (E);
13565 end loop;
13566 end if;
13567
13568 -- Something is wrong if we did not find the value
13569
13570 raise Program_Error;
13571 end Get_Discr_Value;
13572
13573 ---------------------
13574 -- Is_Discriminant --
13575 ---------------------
13576
13577 function Is_Discriminant (Expr : Node_Id) return Boolean is
13578 Discrim_Scope : Entity_Id;
13579
13580 begin
13581 if Denotes_Discriminant (Expr) then
13582 Discrim_Scope := Scope (Entity (Expr));
13583
13584 -- Either we have a reference to one of Typ's discriminants,
13585
13586 pragma Assert (Discrim_Scope = Typ
13587
13588 -- or to the discriminants of the parent type, in the case
13589 -- of a derivation of a tagged type with variants.
13590
13591 or else Discrim_Scope = Etype (Typ)
13592 or else Full_View (Discrim_Scope) = Etype (Typ)
13593
13594 -- or same as above for the case where the discriminants
13595 -- were declared in Typ's private view.
13596
13597 or else (Is_Private_Type (Discrim_Scope)
13598 and then Chars (Discrim_Scope) = Chars (Typ))
13599
13600 -- or else we are deriving from the full view and the
13601 -- discriminant is declared in the private entity.
13602
13603 or else (Is_Private_Type (Typ)
13604 and then Chars (Discrim_Scope) = Chars (Typ))
13605
13606 -- Or we are constrained the corresponding record of a
13607 -- synchronized type that completes a private declaration.
13608
13609 or else (Is_Concurrent_Record_Type (Typ)
13610 and then
13611 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13612
13613 -- or we have a class-wide type, in which case make sure the
13614 -- discriminant found belongs to the root type.
13615
13616 or else (Is_Class_Wide_Type (Typ)
13617 and then Etype (Typ) = Discrim_Scope));
13618
13619 return True;
13620 end if;
13621
13622 -- In all other cases we have something wrong
13623
13624 return False;
13625 end Is_Discriminant;
13626
13627 -- Start of processing for Constrain_Component_Type
13628
13629 begin
13630 if Nkind (Parent (Comp)) = N_Component_Declaration
13631 and then Comes_From_Source (Parent (Comp))
13632 and then Comes_From_Source
13633 (Subtype_Indication (Component_Definition (Parent (Comp))))
13634 and then
13635 Is_Entity_Name
13636 (Subtype_Indication (Component_Definition (Parent (Comp))))
13637 then
13638 return Compon_Type;
13639
13640 elsif Is_Array_Type (Compon_Type) then
13641 return Build_Constrained_Array_Type (Compon_Type);
13642
13643 elsif Has_Discriminants (Compon_Type) then
13644 return Build_Constrained_Discriminated_Type (Compon_Type);
13645
13646 elsif Is_Access_Type (Compon_Type) then
13647 return Build_Constrained_Access_Type (Compon_Type);
13648
13649 else
13650 return Compon_Type;
13651 end if;
13652 end Constrain_Component_Type;
13653
13654 --------------------------
13655 -- Constrain_Concurrent --
13656 --------------------------
13657
13658 -- For concurrent types, the associated record value type carries the same
13659 -- discriminants, so when we constrain a concurrent type, we must constrain
13660 -- the corresponding record type as well.
13661
13662 procedure Constrain_Concurrent
13663 (Def_Id : in out Entity_Id;
13664 SI : Node_Id;
13665 Related_Nod : Node_Id;
13666 Related_Id : Entity_Id;
13667 Suffix : Character)
13668 is
13669 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13670 -- case of a private subtype (needed when only doing semantic analysis).
13671
13672 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13673 T_Val : Entity_Id;
13674
13675 begin
13676 if Is_Access_Type (T_Ent) then
13677 T_Ent := Designated_Type (T_Ent);
13678 end if;
13679
13680 T_Val := Corresponding_Record_Type (T_Ent);
13681
13682 if Present (T_Val) then
13683
13684 if No (Def_Id) then
13685 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13686
13687 -- Elaborate itype now, as it may be used in a subsequent
13688 -- synchronized operation in another scope.
13689
13690 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13691 Build_Itype_Reference (Def_Id, Related_Nod);
13692 end if;
13693 end if;
13694
13695 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13696 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13697
13698 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13699 Set_Corresponding_Record_Type (Def_Id,
13700 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13701
13702 else
13703 -- If there is no associated record, expansion is disabled and this
13704 -- is a generic context. Create a subtype in any case, so that
13705 -- semantic analysis can proceed.
13706
13707 if No (Def_Id) then
13708 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13709 end if;
13710
13711 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13712 end if;
13713 end Constrain_Concurrent;
13714
13715 ------------------------------------
13716 -- Constrain_Corresponding_Record --
13717 ------------------------------------
13718
13719 function Constrain_Corresponding_Record
13720 (Prot_Subt : Entity_Id;
13721 Corr_Rec : Entity_Id;
13722 Related_Nod : Node_Id) return Entity_Id
13723 is
13724 T_Sub : constant Entity_Id :=
13725 Create_Itype
13726 (Ekind => E_Record_Subtype,
13727 Related_Nod => Related_Nod,
13728 Related_Id => Corr_Rec,
13729 Suffix => 'C',
13730 Suffix_Index => -1);
13731
13732 begin
13733 Set_Etype (T_Sub, Corr_Rec);
13734 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13735 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
13736 Set_Is_Constrained (T_Sub, True);
13737 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13738 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13739
13740 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13741 Set_Discriminant_Constraint
13742 (T_Sub, Discriminant_Constraint (Prot_Subt));
13743 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13744 Create_Constrained_Components
13745 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13746 end if;
13747
13748 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13749
13750 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13751 Conditional_Delay (T_Sub, Corr_Rec);
13752
13753 else
13754 -- This is a component subtype: it will be frozen in the context of
13755 -- the enclosing record's init_proc, so that discriminant references
13756 -- are resolved to discriminals. (Note: we used to skip freezing
13757 -- altogether in that case, which caused errors downstream for
13758 -- components of a bit packed array type).
13759
13760 Set_Has_Delayed_Freeze (T_Sub);
13761 end if;
13762
13763 return T_Sub;
13764 end Constrain_Corresponding_Record;
13765
13766 -----------------------
13767 -- Constrain_Decimal --
13768 -----------------------
13769
13770 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13771 T : constant Entity_Id := Entity (Subtype_Mark (S));
13772 C : constant Node_Id := Constraint (S);
13773 Loc : constant Source_Ptr := Sloc (C);
13774 Range_Expr : Node_Id;
13775 Digits_Expr : Node_Id;
13776 Digits_Val : Uint;
13777 Bound_Val : Ureal;
13778
13779 begin
13780 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13781
13782 if Nkind (C) = N_Range_Constraint then
13783 Range_Expr := Range_Expression (C);
13784 Digits_Val := Digits_Value (T);
13785
13786 else
13787 pragma Assert (Nkind (C) = N_Digits_Constraint);
13788
13789 Digits_Expr := Digits_Expression (C);
13790 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13791
13792 Check_Digits_Expression (Digits_Expr);
13793 Digits_Val := Expr_Value (Digits_Expr);
13794
13795 if Digits_Val > Digits_Value (T) then
13796 Error_Msg_N
13797 ("digits expression is incompatible with subtype", C);
13798 Digits_Val := Digits_Value (T);
13799 end if;
13800
13801 if Present (Range_Constraint (C)) then
13802 Range_Expr := Range_Expression (Range_Constraint (C));
13803 else
13804 Range_Expr := Empty;
13805 end if;
13806 end if;
13807
13808 Set_Etype (Def_Id, Base_Type (T));
13809 Set_Size_Info (Def_Id, (T));
13810 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13811 Set_Delta_Value (Def_Id, Delta_Value (T));
13812 Set_Scale_Value (Def_Id, Scale_Value (T));
13813 Set_Small_Value (Def_Id, Small_Value (T));
13814 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13815 Set_Digits_Value (Def_Id, Digits_Val);
13816
13817 -- Manufacture range from given digits value if no range present
13818
13819 if No (Range_Expr) then
13820 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13821 Range_Expr :=
13822 Make_Range (Loc,
13823 Low_Bound =>
13824 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13825 High_Bound =>
13826 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13827 end if;
13828
13829 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13830 Set_Discrete_RM_Size (Def_Id);
13831
13832 -- Unconditionally delay the freeze, since we cannot set size
13833 -- information in all cases correctly until the freeze point.
13834
13835 Set_Has_Delayed_Freeze (Def_Id);
13836 end Constrain_Decimal;
13837
13838 ----------------------------------
13839 -- Constrain_Discriminated_Type --
13840 ----------------------------------
13841
13842 procedure Constrain_Discriminated_Type
13843 (Def_Id : Entity_Id;
13844 S : Node_Id;
13845 Related_Nod : Node_Id;
13846 For_Access : Boolean := False)
13847 is
13848 E : Entity_Id := Entity (Subtype_Mark (S));
13849 T : Entity_Id;
13850
13851 procedure Fixup_Bad_Constraint;
13852 -- Called after finding a bad constraint, and after having posted an
13853 -- appropriate error message. The goal is to leave type Def_Id in as
13854 -- reasonable state as possible.
13855
13856 --------------------------
13857 -- Fixup_Bad_Constraint --
13858 --------------------------
13859
13860 procedure Fixup_Bad_Constraint is
13861 begin
13862 -- Set a reasonable Ekind for the entity, including incomplete types.
13863
13864 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13865
13866 -- Set Etype to the known type, to reduce chances of cascaded errors
13867
13868 Set_Etype (Def_Id, E);
13869 Set_Error_Posted (Def_Id);
13870 end Fixup_Bad_Constraint;
13871
13872 -- Local variables
13873
13874 C : Node_Id;
13875 Constr : Elist_Id := New_Elmt_List;
13876
13877 -- Start of processing for Constrain_Discriminated_Type
13878
13879 begin
13880 C := Constraint (S);
13881
13882 -- A discriminant constraint is only allowed in a subtype indication,
13883 -- after a subtype mark. This subtype mark must denote either a type
13884 -- with discriminants, or an access type whose designated type is a
13885 -- type with discriminants. A discriminant constraint specifies the
13886 -- values of these discriminants (RM 3.7.2(5)).
13887
13888 T := Base_Type (Entity (Subtype_Mark (S)));
13889
13890 if Is_Access_Type (T) then
13891 T := Designated_Type (T);
13892 end if;
13893
13894 -- In an instance it may be necessary to retrieve the full view of a
13895 -- type with unknown discriminants, or a full view with defaulted
13896 -- discriminants. In other contexts the constraint is illegal.
13897
13898 if In_Instance
13899 and then Is_Private_Type (T)
13900 and then Present (Full_View (T))
13901 and then
13902 (Has_Unknown_Discriminants (T)
13903 or else
13904 (not Has_Discriminants (T)
13905 and then Has_Discriminants (Full_View (T))
13906 and then Present (Discriminant_Default_Value
13907 (First_Discriminant (Full_View (T))))))
13908 then
13909 T := Full_View (T);
13910 E := Full_View (E);
13911 end if;
13912
13913 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13914 -- generating an error for access-to-incomplete subtypes.
13915
13916 if Ada_Version >= Ada_2005
13917 and then Ekind (T) = E_Incomplete_Type
13918 and then Nkind (Parent (S)) = N_Subtype_Declaration
13919 and then not Is_Itype (Def_Id)
13920 then
13921 -- A little sanity check: emit an error message if the type has
13922 -- discriminants to begin with. Type T may be a regular incomplete
13923 -- type or imported via a limited with clause.
13924
13925 if Has_Discriminants (T)
13926 or else (From_Limited_With (T)
13927 and then Present (Non_Limited_View (T))
13928 and then Nkind (Parent (Non_Limited_View (T))) =
13929 N_Full_Type_Declaration
13930 and then Present (Discriminant_Specifications
13931 (Parent (Non_Limited_View (T)))))
13932 then
13933 Error_Msg_N
13934 ("(Ada 2005) incomplete subtype may not be constrained", C);
13935 else
13936 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13937 end if;
13938
13939 Fixup_Bad_Constraint;
13940 return;
13941
13942 -- Check that the type has visible discriminants. The type may be
13943 -- a private type with unknown discriminants whose full view has
13944 -- discriminants which are invisible.
13945
13946 elsif not Has_Discriminants (T)
13947 or else
13948 (Has_Unknown_Discriminants (T)
13949 and then Is_Private_Type (T))
13950 then
13951 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13952 Fixup_Bad_Constraint;
13953 return;
13954
13955 elsif Is_Constrained (E)
13956 or else (Ekind (E) = E_Class_Wide_Subtype
13957 and then Present (Discriminant_Constraint (E)))
13958 then
13959 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13960 Fixup_Bad_Constraint;
13961 return;
13962 end if;
13963
13964 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13965 -- applies to the base type.
13966
13967 T := Base_Type (T);
13968
13969 Constr := Build_Discriminant_Constraints (T, S);
13970
13971 -- If the list returned was empty we had an error in building the
13972 -- discriminant constraint. We have also already signalled an error
13973 -- in the incomplete type case
13974
13975 if Is_Empty_Elmt_List (Constr) then
13976 Fixup_Bad_Constraint;
13977 return;
13978 end if;
13979
13980 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13981 end Constrain_Discriminated_Type;
13982
13983 ---------------------------
13984 -- Constrain_Enumeration --
13985 ---------------------------
13986
13987 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13988 T : constant Entity_Id := Entity (Subtype_Mark (S));
13989 C : constant Node_Id := Constraint (S);
13990
13991 begin
13992 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13993
13994 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13995
13996 Set_Etype (Def_Id, Base_Type (T));
13997 Set_Size_Info (Def_Id, (T));
13998 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13999 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14000
14001 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14002
14003 Set_Discrete_RM_Size (Def_Id);
14004 end Constrain_Enumeration;
14005
14006 ----------------------
14007 -- Constrain_Float --
14008 ----------------------
14009
14010 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14011 T : constant Entity_Id := Entity (Subtype_Mark (S));
14012 C : Node_Id;
14013 D : Node_Id;
14014 Rais : Node_Id;
14015
14016 begin
14017 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14018
14019 Set_Etype (Def_Id, Base_Type (T));
14020 Set_Size_Info (Def_Id, (T));
14021 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14022
14023 -- Process the constraint
14024
14025 C := Constraint (S);
14026
14027 -- Digits constraint present
14028
14029 if Nkind (C) = N_Digits_Constraint then
14030 Check_Restriction (No_Obsolescent_Features, C);
14031
14032 if Warn_On_Obsolescent_Feature then
14033 Error_Msg_N
14034 ("subtype digits constraint is an " &
14035 "obsolescent feature (RM J.3(8))?j?", C);
14036 end if;
14037
14038 D := Digits_Expression (C);
14039 Analyze_And_Resolve (D, Any_Integer);
14040 Check_Digits_Expression (D);
14041 Set_Digits_Value (Def_Id, Expr_Value (D));
14042
14043 -- Check that digits value is in range. Obviously we can do this
14044 -- at compile time, but it is strictly a runtime check, and of
14045 -- course there is an ACVC test that checks this.
14046
14047 if Digits_Value (Def_Id) > Digits_Value (T) then
14048 Error_Msg_Uint_1 := Digits_Value (T);
14049 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14050 Rais :=
14051 Make_Raise_Constraint_Error (Sloc (D),
14052 Reason => CE_Range_Check_Failed);
14053 Insert_Action (Declaration_Node (Def_Id), Rais);
14054 end if;
14055
14056 C := Range_Constraint (C);
14057
14058 -- No digits constraint present
14059
14060 else
14061 Set_Digits_Value (Def_Id, Digits_Value (T));
14062 end if;
14063
14064 -- Range constraint present
14065
14066 if Nkind (C) = N_Range_Constraint then
14067 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14068
14069 -- No range constraint present
14070
14071 else
14072 pragma Assert (No (C));
14073 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14074 end if;
14075
14076 Set_Is_Constrained (Def_Id);
14077 end Constrain_Float;
14078
14079 ---------------------
14080 -- Constrain_Index --
14081 ---------------------
14082
14083 procedure Constrain_Index
14084 (Index : Node_Id;
14085 S : Node_Id;
14086 Related_Nod : Node_Id;
14087 Related_Id : Entity_Id;
14088 Suffix : Character;
14089 Suffix_Index : Nat)
14090 is
14091 Def_Id : Entity_Id;
14092 R : Node_Id := Empty;
14093 T : constant Entity_Id := Etype (Index);
14094
14095 begin
14096 Def_Id :=
14097 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14098 Set_Etype (Def_Id, Base_Type (T));
14099
14100 if Nkind (S) = N_Range
14101 or else
14102 (Nkind (S) = N_Attribute_Reference
14103 and then Attribute_Name (S) = Name_Range)
14104 then
14105 -- A Range attribute will be transformed into N_Range by Resolve
14106
14107 Analyze (S);
14108 Set_Etype (S, T);
14109 R := S;
14110
14111 Process_Range_Expr_In_Decl (R, T);
14112
14113 if not Error_Posted (S)
14114 and then
14115 (Nkind (S) /= N_Range
14116 or else not Covers (T, (Etype (Low_Bound (S))))
14117 or else not Covers (T, (Etype (High_Bound (S)))))
14118 then
14119 if Base_Type (T) /= Any_Type
14120 and then Etype (Low_Bound (S)) /= Any_Type
14121 and then Etype (High_Bound (S)) /= Any_Type
14122 then
14123 Error_Msg_N ("range expected", S);
14124 end if;
14125 end if;
14126
14127 elsif Nkind (S) = N_Subtype_Indication then
14128
14129 -- The parser has verified that this is a discrete indication
14130
14131 Resolve_Discrete_Subtype_Indication (S, T);
14132 Bad_Predicated_Subtype_Use
14133 ("subtype& has predicate, not allowed in index constraint",
14134 S, Entity (Subtype_Mark (S)));
14135
14136 R := Range_Expression (Constraint (S));
14137
14138 -- Capture values of bounds and generate temporaries for them if
14139 -- needed, since checks may cause duplication of the expressions
14140 -- which must not be reevaluated.
14141
14142 -- The forced evaluation removes side effects from expressions, which
14143 -- should occur also in GNATprove mode. Otherwise, we end up with
14144 -- unexpected insertions of actions at places where this is not
14145 -- supposed to occur, e.g. on default parameters of a call.
14146
14147 if Expander_Active or GNATprove_Mode then
14148 Force_Evaluation
14149 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14150 Force_Evaluation
14151 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14152 end if;
14153
14154 elsif Nkind (S) = N_Discriminant_Association then
14155
14156 -- Syntactically valid in subtype indication
14157
14158 Error_Msg_N ("invalid index constraint", S);
14159 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14160 return;
14161
14162 -- Subtype_Mark case, no anonymous subtypes to construct
14163
14164 else
14165 Analyze (S);
14166
14167 if Is_Entity_Name (S) then
14168 if not Is_Type (Entity (S)) then
14169 Error_Msg_N ("expect subtype mark for index constraint", S);
14170
14171 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14172 Wrong_Type (S, Base_Type (T));
14173
14174 -- Check error of subtype with predicate in index constraint
14175
14176 else
14177 Bad_Predicated_Subtype_Use
14178 ("subtype& has predicate, not allowed in index constraint",
14179 S, Entity (S));
14180 end if;
14181
14182 return;
14183
14184 else
14185 Error_Msg_N ("invalid index constraint", S);
14186 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14187 return;
14188 end if;
14189 end if;
14190
14191 -- Complete construction of the Itype
14192
14193 if Is_Modular_Integer_Type (T) then
14194 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14195
14196 elsif Is_Integer_Type (T) then
14197 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14198
14199 else
14200 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14201 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14202 Set_First_Literal (Def_Id, First_Literal (T));
14203 end if;
14204
14205 Set_Size_Info (Def_Id, (T));
14206 Set_RM_Size (Def_Id, RM_Size (T));
14207 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14208
14209 Set_Scalar_Range (Def_Id, R);
14210
14211 Set_Etype (S, Def_Id);
14212 Set_Discrete_RM_Size (Def_Id);
14213 end Constrain_Index;
14214
14215 -----------------------
14216 -- Constrain_Integer --
14217 -----------------------
14218
14219 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14220 T : constant Entity_Id := Entity (Subtype_Mark (S));
14221 C : constant Node_Id := Constraint (S);
14222
14223 begin
14224 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14225
14226 if Is_Modular_Integer_Type (T) then
14227 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14228 else
14229 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14230 end if;
14231
14232 Set_Etype (Def_Id, Base_Type (T));
14233 Set_Size_Info (Def_Id, (T));
14234 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14235 Set_Discrete_RM_Size (Def_Id);
14236 end Constrain_Integer;
14237
14238 ------------------------------
14239 -- Constrain_Ordinary_Fixed --
14240 ------------------------------
14241
14242 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14243 T : constant Entity_Id := Entity (Subtype_Mark (S));
14244 C : Node_Id;
14245 D : Node_Id;
14246 Rais : Node_Id;
14247
14248 begin
14249 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14250 Set_Etype (Def_Id, Base_Type (T));
14251 Set_Size_Info (Def_Id, (T));
14252 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14253 Set_Small_Value (Def_Id, Small_Value (T));
14254
14255 -- Process the constraint
14256
14257 C := Constraint (S);
14258
14259 -- Delta constraint present
14260
14261 if Nkind (C) = N_Delta_Constraint then
14262 Check_Restriction (No_Obsolescent_Features, C);
14263
14264 if Warn_On_Obsolescent_Feature then
14265 Error_Msg_S
14266 ("subtype delta constraint is an " &
14267 "obsolescent feature (RM J.3(7))?j?");
14268 end if;
14269
14270 D := Delta_Expression (C);
14271 Analyze_And_Resolve (D, Any_Real);
14272 Check_Delta_Expression (D);
14273 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14274
14275 -- Check that delta value is in range. Obviously we can do this
14276 -- at compile time, but it is strictly a runtime check, and of
14277 -- course there is an ACVC test that checks this.
14278
14279 if Delta_Value (Def_Id) < Delta_Value (T) then
14280 Error_Msg_N ("??delta value is too small", D);
14281 Rais :=
14282 Make_Raise_Constraint_Error (Sloc (D),
14283 Reason => CE_Range_Check_Failed);
14284 Insert_Action (Declaration_Node (Def_Id), Rais);
14285 end if;
14286
14287 C := Range_Constraint (C);
14288
14289 -- No delta constraint present
14290
14291 else
14292 Set_Delta_Value (Def_Id, Delta_Value (T));
14293 end if;
14294
14295 -- Range constraint present
14296
14297 if Nkind (C) = N_Range_Constraint then
14298 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14299
14300 -- No range constraint present
14301
14302 else
14303 pragma Assert (No (C));
14304 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14305 end if;
14306
14307 Set_Discrete_RM_Size (Def_Id);
14308
14309 -- Unconditionally delay the freeze, since we cannot set size
14310 -- information in all cases correctly until the freeze point.
14311
14312 Set_Has_Delayed_Freeze (Def_Id);
14313 end Constrain_Ordinary_Fixed;
14314
14315 -----------------------
14316 -- Contain_Interface --
14317 -----------------------
14318
14319 function Contain_Interface
14320 (Iface : Entity_Id;
14321 Ifaces : Elist_Id) return Boolean
14322 is
14323 Iface_Elmt : Elmt_Id;
14324
14325 begin
14326 if Present (Ifaces) then
14327 Iface_Elmt := First_Elmt (Ifaces);
14328 while Present (Iface_Elmt) loop
14329 if Node (Iface_Elmt) = Iface then
14330 return True;
14331 end if;
14332
14333 Next_Elmt (Iface_Elmt);
14334 end loop;
14335 end if;
14336
14337 return False;
14338 end Contain_Interface;
14339
14340 ---------------------------
14341 -- Convert_Scalar_Bounds --
14342 ---------------------------
14343
14344 procedure Convert_Scalar_Bounds
14345 (N : Node_Id;
14346 Parent_Type : Entity_Id;
14347 Derived_Type : Entity_Id;
14348 Loc : Source_Ptr)
14349 is
14350 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14351
14352 Lo : Node_Id;
14353 Hi : Node_Id;
14354 Rng : Node_Id;
14355
14356 begin
14357 -- Defend against previous errors
14358
14359 if No (Scalar_Range (Derived_Type)) then
14360 Check_Error_Detected;
14361 return;
14362 end if;
14363
14364 Lo := Build_Scalar_Bound
14365 (Type_Low_Bound (Derived_Type),
14366 Parent_Type, Implicit_Base);
14367
14368 Hi := Build_Scalar_Bound
14369 (Type_High_Bound (Derived_Type),
14370 Parent_Type, Implicit_Base);
14371
14372 Rng :=
14373 Make_Range (Loc,
14374 Low_Bound => Lo,
14375 High_Bound => Hi);
14376
14377 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14378
14379 Set_Parent (Rng, N);
14380 Set_Scalar_Range (Derived_Type, Rng);
14381
14382 -- Analyze the bounds
14383
14384 Analyze_And_Resolve (Lo, Implicit_Base);
14385 Analyze_And_Resolve (Hi, Implicit_Base);
14386
14387 -- Analyze the range itself, except that we do not analyze it if
14388 -- the bounds are real literals, and we have a fixed-point type.
14389 -- The reason for this is that we delay setting the bounds in this
14390 -- case till we know the final Small and Size values (see circuit
14391 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14392
14393 if Is_Fixed_Point_Type (Parent_Type)
14394 and then Nkind (Lo) = N_Real_Literal
14395 and then Nkind (Hi) = N_Real_Literal
14396 then
14397 return;
14398
14399 -- Here we do the analysis of the range
14400
14401 -- Note: we do this manually, since if we do a normal Analyze and
14402 -- Resolve call, there are problems with the conversions used for
14403 -- the derived type range.
14404
14405 else
14406 Set_Etype (Rng, Implicit_Base);
14407 Set_Analyzed (Rng, True);
14408 end if;
14409 end Convert_Scalar_Bounds;
14410
14411 -------------------
14412 -- Copy_And_Swap --
14413 -------------------
14414
14415 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14416 begin
14417 -- Initialize new full declaration entity by copying the pertinent
14418 -- fields of the corresponding private declaration entity.
14419
14420 -- We temporarily set Ekind to a value appropriate for a type to
14421 -- avoid assert failures in Einfo from checking for setting type
14422 -- attributes on something that is not a type. Ekind (Priv) is an
14423 -- appropriate choice, since it allowed the attributes to be set
14424 -- in the first place. This Ekind value will be modified later.
14425
14426 Set_Ekind (Full, Ekind (Priv));
14427
14428 -- Also set Etype temporarily to Any_Type, again, in the absence
14429 -- of errors, it will be properly reset, and if there are errors,
14430 -- then we want a value of Any_Type to remain.
14431
14432 Set_Etype (Full, Any_Type);
14433
14434 -- Now start copying attributes
14435
14436 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14437
14438 if Has_Discriminants (Full) then
14439 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14440 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14441 end if;
14442
14443 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14444 Set_Homonym (Full, Homonym (Priv));
14445 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14446 Set_Is_Public (Full, Is_Public (Priv));
14447 Set_Is_Pure (Full, Is_Pure (Priv));
14448 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14449 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14450 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14451 Set_Has_Pragma_Unreferenced_Objects
14452 (Full, Has_Pragma_Unreferenced_Objects
14453 (Priv));
14454
14455 Conditional_Delay (Full, Priv);
14456
14457 if Is_Tagged_Type (Full) then
14458 Set_Direct_Primitive_Operations
14459 (Full, Direct_Primitive_Operations (Priv));
14460 Set_No_Tagged_Streams_Pragma
14461 (Full, No_Tagged_Streams_Pragma (Priv));
14462
14463 if Is_Base_Type (Priv) then
14464 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14465 end if;
14466 end if;
14467
14468 Set_Is_Volatile (Full, Is_Volatile (Priv));
14469 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14470 Set_Scope (Full, Scope (Priv));
14471 Set_Prev_Entity (Full, Prev_Entity (Priv));
14472 Set_Next_Entity (Full, Next_Entity (Priv));
14473 Set_First_Entity (Full, First_Entity (Priv));
14474 Set_Last_Entity (Full, Last_Entity (Priv));
14475
14476 -- If access types have been recorded for later handling, keep them in
14477 -- the full view so that they get handled when the full view freeze
14478 -- node is expanded.
14479
14480 if Present (Freeze_Node (Priv))
14481 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14482 then
14483 Ensure_Freeze_Node (Full);
14484 Set_Access_Types_To_Process
14485 (Freeze_Node (Full),
14486 Access_Types_To_Process (Freeze_Node (Priv)));
14487 end if;
14488
14489 -- Swap the two entities. Now Private is the full type entity and Full
14490 -- is the private one. They will be swapped back at the end of the
14491 -- private part. This swapping ensures that the entity that is visible
14492 -- in the private part is the full declaration.
14493
14494 Exchange_Entities (Priv, Full);
14495 Append_Entity (Full, Scope (Full));
14496 end Copy_And_Swap;
14497
14498 -------------------------------------
14499 -- Copy_Array_Base_Type_Attributes --
14500 -------------------------------------
14501
14502 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14503 begin
14504 Set_Component_Alignment (T1, Component_Alignment (T2));
14505 Set_Component_Type (T1, Component_Type (T2));
14506 Set_Component_Size (T1, Component_Size (T2));
14507 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14508 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14509 Propagate_Concurrent_Flags (T1, T2);
14510 Set_Is_Packed (T1, Is_Packed (T2));
14511 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14512 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14513 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14514 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14515 end Copy_Array_Base_Type_Attributes;
14516
14517 -----------------------------------
14518 -- Copy_Array_Subtype_Attributes --
14519 -----------------------------------
14520
14521 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14522 begin
14523 Set_Size_Info (T1, T2);
14524
14525 Set_First_Index (T1, First_Index (T2));
14526 Set_Is_Aliased (T1, Is_Aliased (T2));
14527 Set_Is_Atomic (T1, Is_Atomic (T2));
14528 Set_Is_Independent (T1, Is_Independent (T2));
14529 Set_Is_Volatile (T1, Is_Volatile (T2));
14530 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14531 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14532 Set_Is_Constrained (T1, Is_Constrained (T2));
14533 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14534 Inherit_Rep_Item_Chain (T1, T2);
14535 Set_Convention (T1, Convention (T2));
14536 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14537 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14538 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14539 end Copy_Array_Subtype_Attributes;
14540
14541 -----------------------------------
14542 -- Create_Constrained_Components --
14543 -----------------------------------
14544
14545 procedure Create_Constrained_Components
14546 (Subt : Entity_Id;
14547 Decl_Node : Node_Id;
14548 Typ : Entity_Id;
14549 Constraints : Elist_Id)
14550 is
14551 Loc : constant Source_Ptr := Sloc (Subt);
14552 Comp_List : constant Elist_Id := New_Elmt_List;
14553 Parent_Type : constant Entity_Id := Etype (Typ);
14554 Assoc_List : constant List_Id := New_List;
14555 Discr_Val : Elmt_Id;
14556 Errors : Boolean;
14557 New_C : Entity_Id;
14558 Old_C : Entity_Id;
14559 Is_Static : Boolean := True;
14560
14561 procedure Collect_Fixed_Components (Typ : Entity_Id);
14562 -- Collect parent type components that do not appear in a variant part
14563
14564 procedure Create_All_Components;
14565 -- Iterate over Comp_List to create the components of the subtype
14566
14567 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14568 -- Creates a new component from Old_Compon, copying all the fields from
14569 -- it, including its Etype, inserts the new component in the Subt entity
14570 -- chain and returns the new component.
14571
14572 function Is_Variant_Record (T : Entity_Id) return Boolean;
14573 -- If true, and discriminants are static, collect only components from
14574 -- variants selected by discriminant values.
14575
14576 ------------------------------
14577 -- Collect_Fixed_Components --
14578 ------------------------------
14579
14580 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14581 begin
14582 -- Build association list for discriminants, and find components of the
14583 -- variant part selected by the values of the discriminants.
14584
14585 Old_C := First_Discriminant (Typ);
14586 Discr_Val := First_Elmt (Constraints);
14587 while Present (Old_C) loop
14588 Append_To (Assoc_List,
14589 Make_Component_Association (Loc,
14590 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14591 Expression => New_Copy (Node (Discr_Val))));
14592
14593 Next_Elmt (Discr_Val);
14594 Next_Discriminant (Old_C);
14595 end loop;
14596
14597 -- The tag and the possible parent component are unconditionally in
14598 -- the subtype.
14599
14600 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14601 Old_C := First_Component (Typ);
14602 while Present (Old_C) loop
14603 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14604 Append_Elmt (Old_C, Comp_List);
14605 end if;
14606
14607 Next_Component (Old_C);
14608 end loop;
14609 end if;
14610 end Collect_Fixed_Components;
14611
14612 ---------------------------
14613 -- Create_All_Components --
14614 ---------------------------
14615
14616 procedure Create_All_Components is
14617 Comp : Elmt_Id;
14618
14619 begin
14620 Comp := First_Elmt (Comp_List);
14621 while Present (Comp) loop
14622 Old_C := Node (Comp);
14623 New_C := Create_Component (Old_C);
14624
14625 Set_Etype
14626 (New_C,
14627 Constrain_Component_Type
14628 (Old_C, Subt, Decl_Node, Typ, Constraints));
14629 Set_Is_Public (New_C, Is_Public (Subt));
14630
14631 Next_Elmt (Comp);
14632 end loop;
14633 end Create_All_Components;
14634
14635 ----------------------
14636 -- Create_Component --
14637 ----------------------
14638
14639 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14640 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14641
14642 begin
14643 if Ekind (Old_Compon) = E_Discriminant
14644 and then Is_Completely_Hidden (Old_Compon)
14645 then
14646 -- This is a shadow discriminant created for a discriminant of
14647 -- the parent type, which needs to be present in the subtype.
14648 -- Give the shadow discriminant an internal name that cannot
14649 -- conflict with that of visible components.
14650
14651 Set_Chars (New_Compon, New_Internal_Name ('C'));
14652 end if;
14653
14654 -- Set the parent so we have a proper link for freezing etc. This is
14655 -- not a real parent pointer, since of course our parent does not own
14656 -- up to us and reference us, we are an illegitimate child of the
14657 -- original parent.
14658
14659 Set_Parent (New_Compon, Parent (Old_Compon));
14660
14661 -- We do not want this node marked as Comes_From_Source, since
14662 -- otherwise it would get first class status and a separate cross-
14663 -- reference line would be generated. Illegitimate children do not
14664 -- rate such recognition.
14665
14666 Set_Comes_From_Source (New_Compon, False);
14667
14668 -- But it is a real entity, and a birth certificate must be properly
14669 -- registered by entering it into the entity list, and setting its
14670 -- scope to the given subtype. This turns out to be useful for the
14671 -- LLVM code generator, but that scope is not used otherwise.
14672
14673 Enter_Name (New_Compon);
14674 Set_Scope (New_Compon, Subt);
14675
14676 return New_Compon;
14677 end Create_Component;
14678
14679 -----------------------
14680 -- Is_Variant_Record --
14681 -----------------------
14682
14683 function Is_Variant_Record (T : Entity_Id) return Boolean is
14684 begin
14685 return Nkind (Parent (T)) = N_Full_Type_Declaration
14686 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14687 and then Present (Component_List (Type_Definition (Parent (T))))
14688 and then
14689 Present
14690 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14691 end Is_Variant_Record;
14692
14693 -- Start of processing for Create_Constrained_Components
14694
14695 begin
14696 pragma Assert (Subt /= Base_Type (Subt));
14697 pragma Assert (Typ = Base_Type (Typ));
14698
14699 Set_First_Entity (Subt, Empty);
14700 Set_Last_Entity (Subt, Empty);
14701
14702 -- Check whether constraint is fully static, in which case we can
14703 -- optimize the list of components.
14704
14705 Discr_Val := First_Elmt (Constraints);
14706 while Present (Discr_Val) loop
14707 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14708 Is_Static := False;
14709 exit;
14710 end if;
14711
14712 Next_Elmt (Discr_Val);
14713 end loop;
14714
14715 Set_Has_Static_Discriminants (Subt, Is_Static);
14716
14717 Push_Scope (Subt);
14718
14719 -- Inherit the discriminants of the parent type
14720
14721 Add_Discriminants : declare
14722 Num_Disc : Nat;
14723 Num_Gird : Nat;
14724
14725 begin
14726 Num_Disc := 0;
14727 Old_C := First_Discriminant (Typ);
14728
14729 while Present (Old_C) loop
14730 Num_Disc := Num_Disc + 1;
14731 New_C := Create_Component (Old_C);
14732 Set_Is_Public (New_C, Is_Public (Subt));
14733 Next_Discriminant (Old_C);
14734 end loop;
14735
14736 -- For an untagged derived subtype, the number of discriminants may
14737 -- be smaller than the number of inherited discriminants, because
14738 -- several of them may be renamed by a single new discriminant or
14739 -- constrained. In this case, add the hidden discriminants back into
14740 -- the subtype, because they need to be present if the optimizer of
14741 -- the GCC 4.x back-end decides to break apart assignments between
14742 -- objects using the parent view into member-wise assignments.
14743
14744 Num_Gird := 0;
14745
14746 if Is_Derived_Type (Typ)
14747 and then not Is_Tagged_Type (Typ)
14748 then
14749 Old_C := First_Stored_Discriminant (Typ);
14750
14751 while Present (Old_C) loop
14752 Num_Gird := Num_Gird + 1;
14753 Next_Stored_Discriminant (Old_C);
14754 end loop;
14755 end if;
14756
14757 if Num_Gird > Num_Disc then
14758
14759 -- Find out multiple uses of new discriminants, and add hidden
14760 -- components for the extra renamed discriminants. We recognize
14761 -- multiple uses through the Corresponding_Discriminant of a
14762 -- new discriminant: if it constrains several old discriminants,
14763 -- this field points to the last one in the parent type. The
14764 -- stored discriminants of the derived type have the same name
14765 -- as those of the parent.
14766
14767 declare
14768 Constr : Elmt_Id;
14769 New_Discr : Entity_Id;
14770 Old_Discr : Entity_Id;
14771
14772 begin
14773 Constr := First_Elmt (Stored_Constraint (Typ));
14774 Old_Discr := First_Stored_Discriminant (Typ);
14775 while Present (Constr) loop
14776 if Is_Entity_Name (Node (Constr))
14777 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14778 then
14779 New_Discr := Entity (Node (Constr));
14780
14781 if Chars (Corresponding_Discriminant (New_Discr)) /=
14782 Chars (Old_Discr)
14783 then
14784 -- The new discriminant has been used to rename a
14785 -- subsequent old discriminant. Introduce a shadow
14786 -- component for the current old discriminant.
14787
14788 New_C := Create_Component (Old_Discr);
14789 Set_Original_Record_Component (New_C, Old_Discr);
14790 end if;
14791
14792 else
14793 -- The constraint has eliminated the old discriminant.
14794 -- Introduce a shadow component.
14795
14796 New_C := Create_Component (Old_Discr);
14797 Set_Original_Record_Component (New_C, Old_Discr);
14798 end if;
14799
14800 Next_Elmt (Constr);
14801 Next_Stored_Discriminant (Old_Discr);
14802 end loop;
14803 end;
14804 end if;
14805 end Add_Discriminants;
14806
14807 if Is_Static
14808 and then Is_Variant_Record (Typ)
14809 then
14810 Collect_Fixed_Components (Typ);
14811
14812 Gather_Components (
14813 Typ,
14814 Component_List (Type_Definition (Parent (Typ))),
14815 Governed_By => Assoc_List,
14816 Into => Comp_List,
14817 Report_Errors => Errors);
14818 pragma Assert (not Errors
14819 or else Serious_Errors_Detected > 0);
14820
14821 Create_All_Components;
14822
14823 -- If the subtype declaration is created for a tagged type derivation
14824 -- with constraints, we retrieve the record definition of the parent
14825 -- type to select the components of the proper variant.
14826
14827 elsif Is_Static
14828 and then Is_Tagged_Type (Typ)
14829 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14830 and then
14831 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14832 and then Is_Variant_Record (Parent_Type)
14833 then
14834 Collect_Fixed_Components (Typ);
14835
14836 Gather_Components
14837 (Typ,
14838 Component_List (Type_Definition (Parent (Parent_Type))),
14839 Governed_By => Assoc_List,
14840 Into => Comp_List,
14841 Report_Errors => Errors);
14842
14843 -- Note: previously there was a check at this point that no errors
14844 -- were detected. As a consequence of AI05-220 there may be an error
14845 -- if an inherited discriminant that controls a variant has a non-
14846 -- static constraint.
14847
14848 -- If the tagged derivation has a type extension, collect all the
14849 -- new components therein.
14850
14851 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14852 then
14853 Old_C := First_Component (Typ);
14854 while Present (Old_C) loop
14855 if Original_Record_Component (Old_C) = Old_C
14856 and then Chars (Old_C) /= Name_uTag
14857 and then Chars (Old_C) /= Name_uParent
14858 then
14859 Append_Elmt (Old_C, Comp_List);
14860 end if;
14861
14862 Next_Component (Old_C);
14863 end loop;
14864 end if;
14865
14866 Create_All_Components;
14867
14868 else
14869 -- If discriminants are not static, or if this is a multi-level type
14870 -- extension, we have to include all components of the parent type.
14871
14872 Old_C := First_Component (Typ);
14873 while Present (Old_C) loop
14874 New_C := Create_Component (Old_C);
14875
14876 Set_Etype
14877 (New_C,
14878 Constrain_Component_Type
14879 (Old_C, Subt, Decl_Node, Typ, Constraints));
14880 Set_Is_Public (New_C, Is_Public (Subt));
14881
14882 Next_Component (Old_C);
14883 end loop;
14884 end if;
14885
14886 End_Scope;
14887 end Create_Constrained_Components;
14888
14889 ------------------------------------------
14890 -- Decimal_Fixed_Point_Type_Declaration --
14891 ------------------------------------------
14892
14893 procedure Decimal_Fixed_Point_Type_Declaration
14894 (T : Entity_Id;
14895 Def : Node_Id)
14896 is
14897 Loc : constant Source_Ptr := Sloc (Def);
14898 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14899 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14900 Implicit_Base : Entity_Id;
14901 Digs_Val : Uint;
14902 Delta_Val : Ureal;
14903 Scale_Val : Uint;
14904 Bound_Val : Ureal;
14905
14906 begin
14907 Check_Restriction (No_Fixed_Point, Def);
14908
14909 -- Create implicit base type
14910
14911 Implicit_Base :=
14912 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14913 Set_Etype (Implicit_Base, Implicit_Base);
14914
14915 -- Analyze and process delta expression
14916
14917 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14918
14919 Check_Delta_Expression (Delta_Expr);
14920 Delta_Val := Expr_Value_R (Delta_Expr);
14921
14922 -- Check delta is power of 10, and determine scale value from it
14923
14924 declare
14925 Val : Ureal;
14926
14927 begin
14928 Scale_Val := Uint_0;
14929 Val := Delta_Val;
14930
14931 if Val < Ureal_1 then
14932 while Val < Ureal_1 loop
14933 Val := Val * Ureal_10;
14934 Scale_Val := Scale_Val + 1;
14935 end loop;
14936
14937 if Scale_Val > 18 then
14938 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14939 Scale_Val := UI_From_Int (+18);
14940 end if;
14941
14942 else
14943 while Val > Ureal_1 loop
14944 Val := Val / Ureal_10;
14945 Scale_Val := Scale_Val - 1;
14946 end loop;
14947
14948 if Scale_Val < -18 then
14949 Error_Msg_N ("scale is less than minimum value of -18", Def);
14950 Scale_Val := UI_From_Int (-18);
14951 end if;
14952 end if;
14953
14954 if Val /= Ureal_1 then
14955 Error_Msg_N ("delta expression must be a power of 10", Def);
14956 Delta_Val := Ureal_10 ** (-Scale_Val);
14957 end if;
14958 end;
14959
14960 -- Set delta, scale and small (small = delta for decimal type)
14961
14962 Set_Delta_Value (Implicit_Base, Delta_Val);
14963 Set_Scale_Value (Implicit_Base, Scale_Val);
14964 Set_Small_Value (Implicit_Base, Delta_Val);
14965
14966 -- Analyze and process digits expression
14967
14968 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14969 Check_Digits_Expression (Digs_Expr);
14970 Digs_Val := Expr_Value (Digs_Expr);
14971
14972 if Digs_Val > 18 then
14973 Digs_Val := UI_From_Int (+18);
14974 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14975 end if;
14976
14977 Set_Digits_Value (Implicit_Base, Digs_Val);
14978 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14979
14980 -- Set range of base type from digits value for now. This will be
14981 -- expanded to represent the true underlying base range by Freeze.
14982
14983 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14984
14985 -- Note: We leave size as zero for now, size will be set at freeze
14986 -- time. We have to do this for ordinary fixed-point, because the size
14987 -- depends on the specified small, and we might as well do the same for
14988 -- decimal fixed-point.
14989
14990 pragma Assert (Esize (Implicit_Base) = Uint_0);
14991
14992 -- If there are bounds given in the declaration use them as the
14993 -- bounds of the first named subtype.
14994
14995 if Present (Real_Range_Specification (Def)) then
14996 declare
14997 RRS : constant Node_Id := Real_Range_Specification (Def);
14998 Low : constant Node_Id := Low_Bound (RRS);
14999 High : constant Node_Id := High_Bound (RRS);
15000 Low_Val : Ureal;
15001 High_Val : Ureal;
15002
15003 begin
15004 Analyze_And_Resolve (Low, Any_Real);
15005 Analyze_And_Resolve (High, Any_Real);
15006 Check_Real_Bound (Low);
15007 Check_Real_Bound (High);
15008 Low_Val := Expr_Value_R (Low);
15009 High_Val := Expr_Value_R (High);
15010
15011 if Low_Val < (-Bound_Val) then
15012 Error_Msg_N
15013 ("range low bound too small for digits value", Low);
15014 Low_Val := -Bound_Val;
15015 end if;
15016
15017 if High_Val > Bound_Val then
15018 Error_Msg_N
15019 ("range high bound too large for digits value", High);
15020 High_Val := Bound_Val;
15021 end if;
15022
15023 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15024 end;
15025
15026 -- If no explicit range, use range that corresponds to given
15027 -- digits value. This will end up as the final range for the
15028 -- first subtype.
15029
15030 else
15031 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15032 end if;
15033
15034 -- Complete entity for first subtype. The inheritance of the rep item
15035 -- chain ensures that SPARK-related pragmas are not clobbered when the
15036 -- decimal fixed point type acts as a full view of a private type.
15037
15038 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15039 Set_Etype (T, Implicit_Base);
15040 Set_Size_Info (T, Implicit_Base);
15041 Inherit_Rep_Item_Chain (T, Implicit_Base);
15042 Set_Digits_Value (T, Digs_Val);
15043 Set_Delta_Value (T, Delta_Val);
15044 Set_Small_Value (T, Delta_Val);
15045 Set_Scale_Value (T, Scale_Val);
15046 Set_Is_Constrained (T);
15047 end Decimal_Fixed_Point_Type_Declaration;
15048
15049 -----------------------------------
15050 -- Derive_Progenitor_Subprograms --
15051 -----------------------------------
15052
15053 procedure Derive_Progenitor_Subprograms
15054 (Parent_Type : Entity_Id;
15055 Tagged_Type : Entity_Id)
15056 is
15057 E : Entity_Id;
15058 Elmt : Elmt_Id;
15059 Iface : Entity_Id;
15060 Iface_Alias : Entity_Id;
15061 Iface_Elmt : Elmt_Id;
15062 Iface_Subp : Entity_Id;
15063 New_Subp : Entity_Id := Empty;
15064 Prim_Elmt : Elmt_Id;
15065 Subp : Entity_Id;
15066 Typ : Entity_Id;
15067
15068 begin
15069 pragma Assert (Ada_Version >= Ada_2005
15070 and then Is_Record_Type (Tagged_Type)
15071 and then Is_Tagged_Type (Tagged_Type)
15072 and then Has_Interfaces (Tagged_Type));
15073
15074 -- Step 1: Transfer to the full-view primitives associated with the
15075 -- partial-view that cover interface primitives. Conceptually this
15076 -- work should be done later by Process_Full_View; done here to
15077 -- simplify its implementation at later stages. It can be safely
15078 -- done here because interfaces must be visible in the partial and
15079 -- private view (RM 7.3(7.3/2)).
15080
15081 -- Small optimization: This work is only required if the parent may
15082 -- have entities whose Alias attribute reference an interface primitive.
15083 -- Such a situation may occur if the parent is an abstract type and the
15084 -- primitive has not been yet overridden or if the parent is a generic
15085 -- formal type covering interfaces.
15086
15087 -- If the tagged type is not abstract, it cannot have abstract
15088 -- primitives (the only entities in the list of primitives of
15089 -- non-abstract tagged types that can reference abstract primitives
15090 -- through its Alias attribute are the internal entities that have
15091 -- attribute Interface_Alias, and these entities are generated later
15092 -- by Add_Internal_Interface_Entities).
15093
15094 if In_Private_Part (Current_Scope)
15095 and then (Is_Abstract_Type (Parent_Type)
15096 or else
15097 Is_Generic_Type (Parent_Type))
15098 then
15099 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15100 while Present (Elmt) loop
15101 Subp := Node (Elmt);
15102
15103 -- At this stage it is not possible to have entities in the list
15104 -- of primitives that have attribute Interface_Alias.
15105
15106 pragma Assert (No (Interface_Alias (Subp)));
15107
15108 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15109
15110 if Is_Interface (Typ) then
15111 E := Find_Primitive_Covering_Interface
15112 (Tagged_Type => Tagged_Type,
15113 Iface_Prim => Subp);
15114
15115 if Present (E)
15116 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15117 then
15118 Replace_Elmt (Elmt, E);
15119 Remove_Homonym (Subp);
15120 end if;
15121 end if;
15122
15123 Next_Elmt (Elmt);
15124 end loop;
15125 end if;
15126
15127 -- Step 2: Add primitives of progenitors that are not implemented by
15128 -- parents of Tagged_Type.
15129
15130 if Present (Interfaces (Base_Type (Tagged_Type))) then
15131 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15132 while Present (Iface_Elmt) loop
15133 Iface := Node (Iface_Elmt);
15134
15135 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15136 while Present (Prim_Elmt) loop
15137 Iface_Subp := Node (Prim_Elmt);
15138 Iface_Alias := Ultimate_Alias (Iface_Subp);
15139
15140 -- Exclude derivation of predefined primitives except those
15141 -- that come from source, or are inherited from one that comes
15142 -- from source. Required to catch declarations of equality
15143 -- operators of interfaces. For example:
15144
15145 -- type Iface is interface;
15146 -- function "=" (Left, Right : Iface) return Boolean;
15147
15148 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15149 or else Comes_From_Source (Iface_Alias)
15150 then
15151 E :=
15152 Find_Primitive_Covering_Interface
15153 (Tagged_Type => Tagged_Type,
15154 Iface_Prim => Iface_Subp);
15155
15156 -- If not found we derive a new primitive leaving its alias
15157 -- attribute referencing the interface primitive.
15158
15159 if No (E) then
15160 Derive_Subprogram
15161 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15162
15163 -- Ada 2012 (AI05-0197): If the covering primitive's name
15164 -- differs from the name of the interface primitive then it
15165 -- is a private primitive inherited from a parent type. In
15166 -- such case, given that Tagged_Type covers the interface,
15167 -- the inherited private primitive becomes visible. For such
15168 -- purpose we add a new entity that renames the inherited
15169 -- private primitive.
15170
15171 elsif Chars (E) /= Chars (Iface_Subp) then
15172 pragma Assert (Has_Suffix (E, 'P'));
15173 Derive_Subprogram
15174 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15175 Set_Alias (New_Subp, E);
15176 Set_Is_Abstract_Subprogram (New_Subp,
15177 Is_Abstract_Subprogram (E));
15178
15179 -- Propagate to the full view interface entities associated
15180 -- with the partial view.
15181
15182 elsif In_Private_Part (Current_Scope)
15183 and then Present (Alias (E))
15184 and then Alias (E) = Iface_Subp
15185 and then
15186 List_Containing (Parent (E)) /=
15187 Private_Declarations
15188 (Specification
15189 (Unit_Declaration_Node (Current_Scope)))
15190 then
15191 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15192 end if;
15193 end if;
15194
15195 Next_Elmt (Prim_Elmt);
15196 end loop;
15197
15198 Next_Elmt (Iface_Elmt);
15199 end loop;
15200 end if;
15201 end Derive_Progenitor_Subprograms;
15202
15203 -----------------------
15204 -- Derive_Subprogram --
15205 -----------------------
15206
15207 procedure Derive_Subprogram
15208 (New_Subp : out Entity_Id;
15209 Parent_Subp : Entity_Id;
15210 Derived_Type : Entity_Id;
15211 Parent_Type : Entity_Id;
15212 Actual_Subp : Entity_Id := Empty)
15213 is
15214 Formal : Entity_Id;
15215 -- Formal parameter of parent primitive operation
15216
15217 Formal_Of_Actual : Entity_Id;
15218 -- Formal parameter of actual operation, when the derivation is to
15219 -- create a renaming for a primitive operation of an actual in an
15220 -- instantiation.
15221
15222 New_Formal : Entity_Id;
15223 -- Formal of inherited operation
15224
15225 Visible_Subp : Entity_Id := Parent_Subp;
15226
15227 function Is_Private_Overriding return Boolean;
15228 -- If Subp is a private overriding of a visible operation, the inherited
15229 -- operation derives from the overridden op (even though its body is the
15230 -- overriding one) and the inherited operation is visible now. See
15231 -- sem_disp to see the full details of the handling of the overridden
15232 -- subprogram, which is removed from the list of primitive operations of
15233 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15234 -- and used to diagnose abstract operations that need overriding in the
15235 -- derived type.
15236
15237 procedure Replace_Type (Id, New_Id : Entity_Id);
15238 -- When the type is an anonymous access type, create a new access type
15239 -- designating the derived type.
15240
15241 procedure Set_Derived_Name;
15242 -- This procedure sets the appropriate Chars name for New_Subp. This
15243 -- is normally just a copy of the parent name. An exception arises for
15244 -- type support subprograms, where the name is changed to reflect the
15245 -- name of the derived type, e.g. if type foo is derived from type bar,
15246 -- then a procedure barDA is derived with a name fooDA.
15247
15248 ---------------------------
15249 -- Is_Private_Overriding --
15250 ---------------------------
15251
15252 function Is_Private_Overriding return Boolean is
15253 Prev : Entity_Id;
15254
15255 begin
15256 -- If the parent is not a dispatching operation there is no
15257 -- need to investigate overridings
15258
15259 if not Is_Dispatching_Operation (Parent_Subp) then
15260 return False;
15261 end if;
15262
15263 -- The visible operation that is overridden is a homonym of the
15264 -- parent subprogram. We scan the homonym chain to find the one
15265 -- whose alias is the subprogram we are deriving.
15266
15267 Prev := Current_Entity (Parent_Subp);
15268 while Present (Prev) loop
15269 if Ekind (Prev) = Ekind (Parent_Subp)
15270 and then Alias (Prev) = Parent_Subp
15271 and then Scope (Parent_Subp) = Scope (Prev)
15272 and then not Is_Hidden (Prev)
15273 then
15274 Visible_Subp := Prev;
15275 return True;
15276 end if;
15277
15278 Prev := Homonym (Prev);
15279 end loop;
15280
15281 return False;
15282 end Is_Private_Overriding;
15283
15284 ------------------
15285 -- Replace_Type --
15286 ------------------
15287
15288 procedure Replace_Type (Id, New_Id : Entity_Id) is
15289 Id_Type : constant Entity_Id := Etype (Id);
15290 Acc_Type : Entity_Id;
15291 Par : constant Node_Id := Parent (Derived_Type);
15292
15293 begin
15294 -- When the type is an anonymous access type, create a new access
15295 -- type designating the derived type. This itype must be elaborated
15296 -- at the point of the derivation, not on subsequent calls that may
15297 -- be out of the proper scope for Gigi, so we insert a reference to
15298 -- it after the derivation.
15299
15300 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15301 declare
15302 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15303
15304 begin
15305 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15306 and then Present (Full_View (Desig_Typ))
15307 and then not Is_Private_Type (Parent_Type)
15308 then
15309 Desig_Typ := Full_View (Desig_Typ);
15310 end if;
15311
15312 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15313
15314 -- Ada 2005 (AI-251): Handle also derivations of abstract
15315 -- interface primitives.
15316
15317 or else (Is_Interface (Desig_Typ)
15318 and then not Is_Class_Wide_Type (Desig_Typ))
15319 then
15320 Acc_Type := New_Copy (Id_Type);
15321 Set_Etype (Acc_Type, Acc_Type);
15322 Set_Scope (Acc_Type, New_Subp);
15323
15324 -- Set size of anonymous access type. If we have an access
15325 -- to an unconstrained array, this is a fat pointer, so it
15326 -- is sizes at twice addtress size.
15327
15328 if Is_Array_Type (Desig_Typ)
15329 and then not Is_Constrained (Desig_Typ)
15330 then
15331 Init_Size (Acc_Type, 2 * System_Address_Size);
15332
15333 -- Other cases use a thin pointer
15334
15335 else
15336 Init_Size (Acc_Type, System_Address_Size);
15337 end if;
15338
15339 -- Set remaining characterstics of anonymous access type
15340
15341 Init_Alignment (Acc_Type);
15342 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15343
15344 Set_Etype (New_Id, Acc_Type);
15345 Set_Scope (New_Id, New_Subp);
15346
15347 -- Create a reference to it
15348
15349 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15350
15351 else
15352 Set_Etype (New_Id, Id_Type);
15353 end if;
15354 end;
15355
15356 -- In Ada2012, a formal may have an incomplete type but the type
15357 -- derivation that inherits the primitive follows the full view.
15358
15359 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15360 or else
15361 (Ekind (Id_Type) = E_Record_Type_With_Private
15362 and then Present (Full_View (Id_Type))
15363 and then
15364 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15365 or else
15366 (Ada_Version >= Ada_2012
15367 and then Ekind (Id_Type) = E_Incomplete_Type
15368 and then Full_View (Id_Type) = Parent_Type)
15369 then
15370 -- Constraint checks on formals are generated during expansion,
15371 -- based on the signature of the original subprogram. The bounds
15372 -- of the derived type are not relevant, and thus we can use
15373 -- the base type for the formals. However, the return type may be
15374 -- used in a context that requires that the proper static bounds
15375 -- be used (a case statement, for example) and for those cases
15376 -- we must use the derived type (first subtype), not its base.
15377
15378 -- If the derived_type_definition has no constraints, we know that
15379 -- the derived type has the same constraints as the first subtype
15380 -- of the parent, and we can also use it rather than its base,
15381 -- which can lead to more efficient code.
15382
15383 if Etype (Id) = Parent_Type then
15384 if Is_Scalar_Type (Parent_Type)
15385 and then
15386 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15387 then
15388 Set_Etype (New_Id, Derived_Type);
15389
15390 elsif Nkind (Par) = N_Full_Type_Declaration
15391 and then
15392 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15393 and then
15394 Is_Entity_Name
15395 (Subtype_Indication (Type_Definition (Par)))
15396 then
15397 Set_Etype (New_Id, Derived_Type);
15398
15399 else
15400 Set_Etype (New_Id, Base_Type (Derived_Type));
15401 end if;
15402
15403 else
15404 Set_Etype (New_Id, Base_Type (Derived_Type));
15405 end if;
15406
15407 else
15408 Set_Etype (New_Id, Etype (Id));
15409 end if;
15410 end Replace_Type;
15411
15412 ----------------------
15413 -- Set_Derived_Name --
15414 ----------------------
15415
15416 procedure Set_Derived_Name is
15417 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15418 begin
15419 if Nm = TSS_Null then
15420 Set_Chars (New_Subp, Chars (Parent_Subp));
15421 else
15422 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15423 end if;
15424 end Set_Derived_Name;
15425
15426 -- Start of processing for Derive_Subprogram
15427
15428 begin
15429 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15430 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15431
15432 -- Check whether the inherited subprogram is a private operation that
15433 -- should be inherited but not yet made visible. Such subprograms can
15434 -- become visible at a later point (e.g., the private part of a public
15435 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15436 -- following predicate is true, then this is not such a private
15437 -- operation and the subprogram simply inherits the name of the parent
15438 -- subprogram. Note the special check for the names of controlled
15439 -- operations, which are currently exempted from being inherited with
15440 -- a hidden name because they must be findable for generation of
15441 -- implicit run-time calls.
15442
15443 if not Is_Hidden (Parent_Subp)
15444 or else Is_Internal (Parent_Subp)
15445 or else Is_Private_Overriding
15446 or else Is_Internal_Name (Chars (Parent_Subp))
15447 or else (Is_Controlled (Parent_Type)
15448 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15449 Name_Finalize,
15450 Name_Initialize))
15451 then
15452 Set_Derived_Name;
15453
15454 -- An inherited dispatching equality will be overridden by an internally
15455 -- generated one, or by an explicit one, so preserve its name and thus
15456 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15457 -- private operation it may become invisible if the full view has
15458 -- progenitors, and the dispatch table will be malformed.
15459 -- We check that the type is limited to handle the anomalous declaration
15460 -- of Limited_Controlled, which is derived from a non-limited type, and
15461 -- which is handled specially elsewhere as well.
15462
15463 elsif Chars (Parent_Subp) = Name_Op_Eq
15464 and then Is_Dispatching_Operation (Parent_Subp)
15465 and then Etype (Parent_Subp) = Standard_Boolean
15466 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15467 and then
15468 Etype (First_Formal (Parent_Subp)) =
15469 Etype (Next_Formal (First_Formal (Parent_Subp)))
15470 then
15471 Set_Derived_Name;
15472
15473 -- If parent is hidden, this can be a regular derivation if the
15474 -- parent is immediately visible in a non-instantiating context,
15475 -- or if we are in the private part of an instance. This test
15476 -- should still be refined ???
15477
15478 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15479 -- operation as a non-visible operation in cases where the parent
15480 -- subprogram might not be visible now, but was visible within the
15481 -- original generic, so it would be wrong to make the inherited
15482 -- subprogram non-visible now. (Not clear if this test is fully
15483 -- correct; are there any cases where we should declare the inherited
15484 -- operation as not visible to avoid it being overridden, e.g., when
15485 -- the parent type is a generic actual with private primitives ???)
15486
15487 -- (they should be treated the same as other private inherited
15488 -- subprograms, but it's not clear how to do this cleanly). ???
15489
15490 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15491 and then Is_Immediately_Visible (Parent_Subp)
15492 and then not In_Instance)
15493 or else In_Instance_Not_Visible
15494 then
15495 Set_Derived_Name;
15496
15497 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15498 -- overrides an interface primitive because interface primitives
15499 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15500
15501 elsif Ada_Version >= Ada_2005
15502 and then Is_Dispatching_Operation (Parent_Subp)
15503 and then Present (Covered_Interface_Op (Parent_Subp))
15504 then
15505 Set_Derived_Name;
15506
15507 -- Otherwise, the type is inheriting a private operation, so enter it
15508 -- with a special name so it can't be overridden. See also below, where
15509 -- we check for this case, and if so avoid setting Requires_Overriding.
15510
15511 else
15512 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15513 end if;
15514
15515 Set_Parent (New_Subp, Parent (Derived_Type));
15516
15517 if Present (Actual_Subp) then
15518 Replace_Type (Actual_Subp, New_Subp);
15519 else
15520 Replace_Type (Parent_Subp, New_Subp);
15521 end if;
15522
15523 Conditional_Delay (New_Subp, Parent_Subp);
15524
15525 -- If we are creating a renaming for a primitive operation of an
15526 -- actual of a generic derived type, we must examine the signature
15527 -- of the actual primitive, not that of the generic formal, which for
15528 -- example may be an interface. However the name and initial value
15529 -- of the inherited operation are those of the formal primitive.
15530
15531 Formal := First_Formal (Parent_Subp);
15532
15533 if Present (Actual_Subp) then
15534 Formal_Of_Actual := First_Formal (Actual_Subp);
15535 else
15536 Formal_Of_Actual := Empty;
15537 end if;
15538
15539 while Present (Formal) loop
15540 New_Formal := New_Copy (Formal);
15541
15542 -- Normally we do not go copying parents, but in the case of
15543 -- formals, we need to link up to the declaration (which is the
15544 -- parameter specification), and it is fine to link up to the
15545 -- original formal's parameter specification in this case.
15546
15547 Set_Parent (New_Formal, Parent (Formal));
15548 Append_Entity (New_Formal, New_Subp);
15549
15550 if Present (Formal_Of_Actual) then
15551 Replace_Type (Formal_Of_Actual, New_Formal);
15552 Next_Formal (Formal_Of_Actual);
15553 else
15554 Replace_Type (Formal, New_Formal);
15555 end if;
15556
15557 Next_Formal (Formal);
15558 end loop;
15559
15560 -- Extra formals are shared between the parent subprogram and the
15561 -- derived subprogram (implicit in the above copy of formals), and
15562 -- hence we must inherit also the reference to the first extra formal.
15563
15564 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
15565
15566 -- If this derivation corresponds to a tagged generic actual, then
15567 -- primitive operations rename those of the actual. Otherwise the
15568 -- primitive operations rename those of the parent type, If the parent
15569 -- renames an intrinsic operator, so does the new subprogram. We except
15570 -- concatenation, which is always properly typed, and does not get
15571 -- expanded as other intrinsic operations.
15572
15573 if No (Actual_Subp) then
15574 if Is_Intrinsic_Subprogram (Parent_Subp) then
15575 Set_Is_Intrinsic_Subprogram (New_Subp);
15576
15577 if Present (Alias (Parent_Subp))
15578 and then Chars (Parent_Subp) /= Name_Op_Concat
15579 then
15580 Set_Alias (New_Subp, Alias (Parent_Subp));
15581 else
15582 Set_Alias (New_Subp, Parent_Subp);
15583 end if;
15584
15585 else
15586 Set_Alias (New_Subp, Parent_Subp);
15587 end if;
15588
15589 else
15590 Set_Alias (New_Subp, Actual_Subp);
15591 end if;
15592
15593 -- Derived subprograms of a tagged type must inherit the convention
15594 -- of the parent subprogram (a requirement of AI-117). Derived
15595 -- subprograms of untagged types simply get convention Ada by default.
15596
15597 -- If the derived type is a tagged generic formal type with unknown
15598 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15599
15600 -- However, if the type is derived from a generic formal, the further
15601 -- inherited subprogram has the convention of the non-generic ancestor.
15602 -- Otherwise there would be no way to override the operation.
15603 -- (This is subject to forthcoming ARG discussions).
15604
15605 if Is_Tagged_Type (Derived_Type) then
15606 if Is_Generic_Type (Derived_Type)
15607 and then Has_Unknown_Discriminants (Derived_Type)
15608 then
15609 Set_Convention (New_Subp, Convention_Intrinsic);
15610
15611 else
15612 if Is_Generic_Type (Parent_Type)
15613 and then Has_Unknown_Discriminants (Parent_Type)
15614 then
15615 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15616 else
15617 Set_Convention (New_Subp, Convention (Parent_Subp));
15618 end if;
15619 end if;
15620 end if;
15621
15622 -- Predefined controlled operations retain their name even if the parent
15623 -- is hidden (see above), but they are not primitive operations if the
15624 -- ancestor is not visible, for example if the parent is a private
15625 -- extension completed with a controlled extension. Note that a full
15626 -- type that is controlled can break privacy: the flag Is_Controlled is
15627 -- set on both views of the type.
15628
15629 if Is_Controlled (Parent_Type)
15630 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15631 Name_Adjust,
15632 Name_Finalize)
15633 and then Is_Hidden (Parent_Subp)
15634 and then not Is_Visibly_Controlled (Parent_Type)
15635 then
15636 Set_Is_Hidden (New_Subp);
15637 end if;
15638
15639 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15640 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15641
15642 if Ekind (Parent_Subp) = E_Procedure then
15643 Set_Is_Valued_Procedure
15644 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15645 else
15646 Set_Has_Controlling_Result
15647 (New_Subp, Has_Controlling_Result (Parent_Subp));
15648 end if;
15649
15650 -- No_Return must be inherited properly. If this is overridden in the
15651 -- case of a dispatching operation, then the check is made later in
15652 -- Check_Abstract_Overriding that the overriding operation is also
15653 -- No_Return (no such check is required for the nondispatching case).
15654
15655 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15656
15657 -- A derived function with a controlling result is abstract. If the
15658 -- Derived_Type is a nonabstract formal generic derived type, then
15659 -- inherited operations are not abstract: the required check is done at
15660 -- instantiation time. If the derivation is for a generic actual, the
15661 -- function is not abstract unless the actual is.
15662
15663 if Is_Generic_Type (Derived_Type)
15664 and then not Is_Abstract_Type (Derived_Type)
15665 then
15666 null;
15667
15668 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15669 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15670
15671 -- A subprogram subject to pragma Extensions_Visible with value False
15672 -- requires overriding if the subprogram has at least one controlling
15673 -- OUT parameter (SPARK RM 6.1.7(6)).
15674
15675 elsif Ada_Version >= Ada_2005
15676 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15677 or else (Is_Tagged_Type (Derived_Type)
15678 and then Etype (New_Subp) = Derived_Type
15679 and then not Is_Null_Extension (Derived_Type))
15680 or else (Is_Tagged_Type (Derived_Type)
15681 and then Ekind (Etype (New_Subp)) =
15682 E_Anonymous_Access_Type
15683 and then Designated_Type (Etype (New_Subp)) =
15684 Derived_Type
15685 and then not Is_Null_Extension (Derived_Type))
15686 or else (Comes_From_Source (Alias (New_Subp))
15687 and then Is_EVF_Procedure (Alias (New_Subp))))
15688 and then No (Actual_Subp)
15689 then
15690 if not Is_Tagged_Type (Derived_Type)
15691 or else Is_Abstract_Type (Derived_Type)
15692 or else Is_Abstract_Subprogram (Alias (New_Subp))
15693 then
15694 Set_Is_Abstract_Subprogram (New_Subp);
15695
15696 -- If the Chars of the new subprogram is different from that of the
15697 -- parent's one, it means that we entered it with a special name so
15698 -- it can't be overridden (see above). In that case we had better not
15699 -- *require* it to be overridden. This is the case where the parent
15700 -- type inherited the operation privately, so there's no danger of
15701 -- dangling dispatching.
15702
15703 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
15704 Set_Requires_Overriding (New_Subp);
15705 end if;
15706
15707 elsif Ada_Version < Ada_2005
15708 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15709 or else (Is_Tagged_Type (Derived_Type)
15710 and then Etype (New_Subp) = Derived_Type
15711 and then No (Actual_Subp)))
15712 then
15713 Set_Is_Abstract_Subprogram (New_Subp);
15714
15715 -- AI05-0097 : an inherited operation that dispatches on result is
15716 -- abstract if the derived type is abstract, even if the parent type
15717 -- is concrete and the derived type is a null extension.
15718
15719 elsif Has_Controlling_Result (Alias (New_Subp))
15720 and then Is_Abstract_Type (Etype (New_Subp))
15721 then
15722 Set_Is_Abstract_Subprogram (New_Subp);
15723
15724 -- Finally, if the parent type is abstract we must verify that all
15725 -- inherited operations are either non-abstract or overridden, or that
15726 -- the derived type itself is abstract (this check is performed at the
15727 -- end of a package declaration, in Check_Abstract_Overriding). A
15728 -- private overriding in the parent type will not be visible in the
15729 -- derivation if we are not in an inner package or in a child unit of
15730 -- the parent type, in which case the abstractness of the inherited
15731 -- operation is carried to the new subprogram.
15732
15733 elsif Is_Abstract_Type (Parent_Type)
15734 and then not In_Open_Scopes (Scope (Parent_Type))
15735 and then Is_Private_Overriding
15736 and then Is_Abstract_Subprogram (Visible_Subp)
15737 then
15738 if No (Actual_Subp) then
15739 Set_Alias (New_Subp, Visible_Subp);
15740 Set_Is_Abstract_Subprogram (New_Subp, True);
15741
15742 else
15743 -- If this is a derivation for an instance of a formal derived
15744 -- type, abstractness comes from the primitive operation of the
15745 -- actual, not from the operation inherited from the ancestor.
15746
15747 Set_Is_Abstract_Subprogram
15748 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15749 end if;
15750 end if;
15751
15752 New_Overloaded_Entity (New_Subp, Derived_Type);
15753
15754 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15755 -- preconditions and the derived type is abstract, the derived operation
15756 -- is abstract as well if parent subprogram is not abstract or null.
15757
15758 if Is_Abstract_Type (Derived_Type)
15759 and then Has_Non_Trivial_Precondition (Parent_Subp)
15760 and then Present (Interfaces (Derived_Type))
15761 then
15762
15763 -- Add useful attributes of subprogram before the freeze point,
15764 -- in case freezing is delayed or there are previous errors.
15765
15766 Set_Is_Dispatching_Operation (New_Subp);
15767
15768 declare
15769 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15770
15771 begin
15772 if Present (Iface_Prim)
15773 and then Has_Non_Trivial_Precondition (Iface_Prim)
15774 then
15775 Set_Is_Abstract_Subprogram (New_Subp);
15776 end if;
15777 end;
15778 end if;
15779
15780 -- Check for case of a derived subprogram for the instantiation of a
15781 -- formal derived tagged type, if so mark the subprogram as dispatching
15782 -- and inherit the dispatching attributes of the actual subprogram. The
15783 -- derived subprogram is effectively renaming of the actual subprogram,
15784 -- so it needs to have the same attributes as the actual.
15785
15786 if Present (Actual_Subp)
15787 and then Is_Dispatching_Operation (Actual_Subp)
15788 then
15789 Set_Is_Dispatching_Operation (New_Subp);
15790
15791 if Present (DTC_Entity (Actual_Subp)) then
15792 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15793 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15794 end if;
15795 end if;
15796
15797 -- Indicate that a derived subprogram does not require a body and that
15798 -- it does not require processing of default expressions.
15799
15800 Set_Has_Completion (New_Subp);
15801 Set_Default_Expressions_Processed (New_Subp);
15802
15803 if Ekind (New_Subp) = E_Function then
15804 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15805 end if;
15806 end Derive_Subprogram;
15807
15808 ------------------------
15809 -- Derive_Subprograms --
15810 ------------------------
15811
15812 procedure Derive_Subprograms
15813 (Parent_Type : Entity_Id;
15814 Derived_Type : Entity_Id;
15815 Generic_Actual : Entity_Id := Empty)
15816 is
15817 Op_List : constant Elist_Id :=
15818 Collect_Primitive_Operations (Parent_Type);
15819
15820 function Check_Derived_Type return Boolean;
15821 -- Check that all the entities derived from Parent_Type are found in
15822 -- the list of primitives of Derived_Type exactly in the same order.
15823
15824 procedure Derive_Interface_Subprogram
15825 (New_Subp : out Entity_Id;
15826 Subp : Entity_Id;
15827 Actual_Subp : Entity_Id);
15828 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15829 -- (which is an interface primitive). If Generic_Actual is present then
15830 -- Actual_Subp is the actual subprogram corresponding with the generic
15831 -- subprogram Subp.
15832
15833 ------------------------
15834 -- Check_Derived_Type --
15835 ------------------------
15836
15837 function Check_Derived_Type return Boolean is
15838 E : Entity_Id;
15839 Elmt : Elmt_Id;
15840 List : Elist_Id;
15841 New_Subp : Entity_Id;
15842 Op_Elmt : Elmt_Id;
15843 Subp : Entity_Id;
15844
15845 begin
15846 -- Traverse list of entities in the current scope searching for
15847 -- an incomplete type whose full-view is derived type.
15848
15849 E := First_Entity (Scope (Derived_Type));
15850 while Present (E) and then E /= Derived_Type loop
15851 if Ekind (E) = E_Incomplete_Type
15852 and then Present (Full_View (E))
15853 and then Full_View (E) = Derived_Type
15854 then
15855 -- Disable this test if Derived_Type completes an incomplete
15856 -- type because in such case more primitives can be added
15857 -- later to the list of primitives of Derived_Type by routine
15858 -- Process_Incomplete_Dependents
15859
15860 return True;
15861 end if;
15862
15863 Next_Entity (E);
15864 end loop;
15865
15866 List := Collect_Primitive_Operations (Derived_Type);
15867 Elmt := First_Elmt (List);
15868
15869 Op_Elmt := First_Elmt (Op_List);
15870 while Present (Op_Elmt) loop
15871 Subp := Node (Op_Elmt);
15872 New_Subp := Node (Elmt);
15873
15874 -- At this early stage Derived_Type has no entities with attribute
15875 -- Interface_Alias. In addition, such primitives are always
15876 -- located at the end of the list of primitives of Parent_Type.
15877 -- Therefore, if found we can safely stop processing pending
15878 -- entities.
15879
15880 exit when Present (Interface_Alias (Subp));
15881
15882 -- Handle hidden entities
15883
15884 if not Is_Predefined_Dispatching_Operation (Subp)
15885 and then Is_Hidden (Subp)
15886 then
15887 if Present (New_Subp)
15888 and then Primitive_Names_Match (Subp, New_Subp)
15889 then
15890 Next_Elmt (Elmt);
15891 end if;
15892
15893 else
15894 if not Present (New_Subp)
15895 or else Ekind (Subp) /= Ekind (New_Subp)
15896 or else not Primitive_Names_Match (Subp, New_Subp)
15897 then
15898 return False;
15899 end if;
15900
15901 Next_Elmt (Elmt);
15902 end if;
15903
15904 Next_Elmt (Op_Elmt);
15905 end loop;
15906
15907 return True;
15908 end Check_Derived_Type;
15909
15910 ---------------------------------
15911 -- Derive_Interface_Subprogram --
15912 ---------------------------------
15913
15914 procedure Derive_Interface_Subprogram
15915 (New_Subp : out Entity_Id;
15916 Subp : Entity_Id;
15917 Actual_Subp : Entity_Id)
15918 is
15919 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15920 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15921
15922 begin
15923 pragma Assert (Is_Interface (Iface_Type));
15924
15925 Derive_Subprogram
15926 (New_Subp => New_Subp,
15927 Parent_Subp => Iface_Subp,
15928 Derived_Type => Derived_Type,
15929 Parent_Type => Iface_Type,
15930 Actual_Subp => Actual_Subp);
15931
15932 -- Given that this new interface entity corresponds with a primitive
15933 -- of the parent that was not overridden we must leave it associated
15934 -- with its parent primitive to ensure that it will share the same
15935 -- dispatch table slot when overridden. We must set the Alias to Subp
15936 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15937 -- (in case we inherited Subp from Iface_Type via a nonabstract
15938 -- generic formal type).
15939
15940 if No (Actual_Subp) then
15941 Set_Alias (New_Subp, Subp);
15942
15943 declare
15944 T : Entity_Id := Find_Dispatching_Type (Subp);
15945 begin
15946 while Etype (T) /= T loop
15947 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15948 Set_Is_Abstract_Subprogram (New_Subp, False);
15949 exit;
15950 end if;
15951
15952 T := Etype (T);
15953 end loop;
15954 end;
15955
15956 -- For instantiations this is not needed since the previous call to
15957 -- Derive_Subprogram leaves the entity well decorated.
15958
15959 else
15960 pragma Assert (Alias (New_Subp) = Actual_Subp);
15961 null;
15962 end if;
15963 end Derive_Interface_Subprogram;
15964
15965 -- Local variables
15966
15967 Alias_Subp : Entity_Id;
15968 Act_List : Elist_Id;
15969 Act_Elmt : Elmt_Id;
15970 Act_Subp : Entity_Id := Empty;
15971 Elmt : Elmt_Id;
15972 Need_Search : Boolean := False;
15973 New_Subp : Entity_Id := Empty;
15974 Parent_Base : Entity_Id;
15975 Subp : Entity_Id;
15976
15977 -- Start of processing for Derive_Subprograms
15978
15979 begin
15980 if Ekind (Parent_Type) = E_Record_Type_With_Private
15981 and then Has_Discriminants (Parent_Type)
15982 and then Present (Full_View (Parent_Type))
15983 then
15984 Parent_Base := Full_View (Parent_Type);
15985 else
15986 Parent_Base := Parent_Type;
15987 end if;
15988
15989 if Present (Generic_Actual) then
15990 Act_List := Collect_Primitive_Operations (Generic_Actual);
15991 Act_Elmt := First_Elmt (Act_List);
15992 else
15993 Act_List := No_Elist;
15994 Act_Elmt := No_Elmt;
15995 end if;
15996
15997 -- Derive primitives inherited from the parent. Note that if the generic
15998 -- actual is present, this is not really a type derivation, it is a
15999 -- completion within an instance.
16000
16001 -- Case 1: Derived_Type does not implement interfaces
16002
16003 if not Is_Tagged_Type (Derived_Type)
16004 or else (not Has_Interfaces (Derived_Type)
16005 and then not (Present (Generic_Actual)
16006 and then Has_Interfaces (Generic_Actual)))
16007 then
16008 Elmt := First_Elmt (Op_List);
16009 while Present (Elmt) loop
16010 Subp := Node (Elmt);
16011
16012 -- Literals are derived earlier in the process of building the
16013 -- derived type, and are skipped here.
16014
16015 if Ekind (Subp) = E_Enumeration_Literal then
16016 null;
16017
16018 -- The actual is a direct descendant and the common primitive
16019 -- operations appear in the same order.
16020
16021 -- If the generic parent type is present, the derived type is an
16022 -- instance of a formal derived type, and within the instance its
16023 -- operations are those of the actual. We derive from the formal
16024 -- type but make the inherited operations aliases of the
16025 -- corresponding operations of the actual.
16026
16027 else
16028 pragma Assert (No (Node (Act_Elmt))
16029 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16030 and then
16031 Type_Conformant
16032 (Subp, Node (Act_Elmt),
16033 Skip_Controlling_Formals => True)));
16034
16035 Derive_Subprogram
16036 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16037
16038 if Present (Act_Elmt) then
16039 Next_Elmt (Act_Elmt);
16040 end if;
16041 end if;
16042
16043 Next_Elmt (Elmt);
16044 end loop;
16045
16046 -- Case 2: Derived_Type implements interfaces
16047
16048 else
16049 -- If the parent type has no predefined primitives we remove
16050 -- predefined primitives from the list of primitives of generic
16051 -- actual to simplify the complexity of this algorithm.
16052
16053 if Present (Generic_Actual) then
16054 declare
16055 Has_Predefined_Primitives : Boolean := False;
16056
16057 begin
16058 -- Check if the parent type has predefined primitives
16059
16060 Elmt := First_Elmt (Op_List);
16061 while Present (Elmt) loop
16062 Subp := Node (Elmt);
16063
16064 if Is_Predefined_Dispatching_Operation (Subp)
16065 and then not Comes_From_Source (Ultimate_Alias (Subp))
16066 then
16067 Has_Predefined_Primitives := True;
16068 exit;
16069 end if;
16070
16071 Next_Elmt (Elmt);
16072 end loop;
16073
16074 -- Remove predefined primitives of Generic_Actual. We must use
16075 -- an auxiliary list because in case of tagged types the value
16076 -- returned by Collect_Primitive_Operations is the value stored
16077 -- in its Primitive_Operations attribute (and we don't want to
16078 -- modify its current contents).
16079
16080 if not Has_Predefined_Primitives then
16081 declare
16082 Aux_List : constant Elist_Id := New_Elmt_List;
16083
16084 begin
16085 Elmt := First_Elmt (Act_List);
16086 while Present (Elmt) loop
16087 Subp := Node (Elmt);
16088
16089 if not Is_Predefined_Dispatching_Operation (Subp)
16090 or else Comes_From_Source (Subp)
16091 then
16092 Append_Elmt (Subp, Aux_List);
16093 end if;
16094
16095 Next_Elmt (Elmt);
16096 end loop;
16097
16098 Act_List := Aux_List;
16099 end;
16100 end if;
16101
16102 Act_Elmt := First_Elmt (Act_List);
16103 Act_Subp := Node (Act_Elmt);
16104 end;
16105 end if;
16106
16107 -- Stage 1: If the generic actual is not present we derive the
16108 -- primitives inherited from the parent type. If the generic parent
16109 -- type is present, the derived type is an instance of a formal
16110 -- derived type, and within the instance its operations are those of
16111 -- the actual. We derive from the formal type but make the inherited
16112 -- operations aliases of the corresponding operations of the actual.
16113
16114 Elmt := First_Elmt (Op_List);
16115 while Present (Elmt) loop
16116 Subp := Node (Elmt);
16117 Alias_Subp := Ultimate_Alias (Subp);
16118
16119 -- Do not derive internal entities of the parent that link
16120 -- interface primitives with their covering primitive. These
16121 -- entities will be added to this type when frozen.
16122
16123 if Present (Interface_Alias (Subp)) then
16124 goto Continue;
16125 end if;
16126
16127 -- If the generic actual is present find the corresponding
16128 -- operation in the generic actual. If the parent type is a
16129 -- direct ancestor of the derived type then, even if it is an
16130 -- interface, the operations are inherited from the primary
16131 -- dispatch table and are in the proper order. If we detect here
16132 -- that primitives are not in the same order we traverse the list
16133 -- of primitive operations of the actual to find the one that
16134 -- implements the interface primitive.
16135
16136 if Need_Search
16137 or else
16138 (Present (Generic_Actual)
16139 and then Present (Act_Subp)
16140 and then not
16141 (Primitive_Names_Match (Subp, Act_Subp)
16142 and then
16143 Type_Conformant (Subp, Act_Subp,
16144 Skip_Controlling_Formals => True)))
16145 then
16146 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16147 Use_Full_View => True));
16148
16149 -- Remember that we need searching for all pending primitives
16150
16151 Need_Search := True;
16152
16153 -- Handle entities associated with interface primitives
16154
16155 if Present (Alias_Subp)
16156 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16157 and then not Is_Predefined_Dispatching_Operation (Subp)
16158 then
16159 -- Search for the primitive in the homonym chain
16160
16161 Act_Subp :=
16162 Find_Primitive_Covering_Interface
16163 (Tagged_Type => Generic_Actual,
16164 Iface_Prim => Alias_Subp);
16165
16166 -- Previous search may not locate primitives covering
16167 -- interfaces defined in generics units or instantiations.
16168 -- (it fails if the covering primitive has formals whose
16169 -- type is also defined in generics or instantiations).
16170 -- In such case we search in the list of primitives of the
16171 -- generic actual for the internal entity that links the
16172 -- interface primitive and the covering primitive.
16173
16174 if No (Act_Subp)
16175 and then Is_Generic_Type (Parent_Type)
16176 then
16177 -- This code has been designed to handle only generic
16178 -- formals that implement interfaces that are defined
16179 -- in a generic unit or instantiation. If this code is
16180 -- needed for other cases we must review it because
16181 -- (given that it relies on Original_Location to locate
16182 -- the primitive of Generic_Actual that covers the
16183 -- interface) it could leave linked through attribute
16184 -- Alias entities of unrelated instantiations).
16185
16186 pragma Assert
16187 (Is_Generic_Unit
16188 (Scope (Find_Dispatching_Type (Alias_Subp)))
16189 or else
16190 Instantiation_Depth
16191 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16192
16193 declare
16194 Iface_Prim_Loc : constant Source_Ptr :=
16195 Original_Location (Sloc (Alias_Subp));
16196
16197 Elmt : Elmt_Id;
16198 Prim : Entity_Id;
16199
16200 begin
16201 Elmt :=
16202 First_Elmt (Primitive_Operations (Generic_Actual));
16203
16204 Search : while Present (Elmt) loop
16205 Prim := Node (Elmt);
16206
16207 if Present (Interface_Alias (Prim))
16208 and then Original_Location
16209 (Sloc (Interface_Alias (Prim))) =
16210 Iface_Prim_Loc
16211 then
16212 Act_Subp := Alias (Prim);
16213 exit Search;
16214 end if;
16215
16216 Next_Elmt (Elmt);
16217 end loop Search;
16218 end;
16219 end if;
16220
16221 pragma Assert (Present (Act_Subp)
16222 or else Is_Abstract_Type (Generic_Actual)
16223 or else Serious_Errors_Detected > 0);
16224
16225 -- Handle predefined primitives plus the rest of user-defined
16226 -- primitives
16227
16228 else
16229 Act_Elmt := First_Elmt (Act_List);
16230 while Present (Act_Elmt) loop
16231 Act_Subp := Node (Act_Elmt);
16232
16233 exit when Primitive_Names_Match (Subp, Act_Subp)
16234 and then Type_Conformant
16235 (Subp, Act_Subp,
16236 Skip_Controlling_Formals => True)
16237 and then No (Interface_Alias (Act_Subp));
16238
16239 Next_Elmt (Act_Elmt);
16240 end loop;
16241
16242 if No (Act_Elmt) then
16243 Act_Subp := Empty;
16244 end if;
16245 end if;
16246 end if;
16247
16248 -- Case 1: If the parent is a limited interface then it has the
16249 -- predefined primitives of synchronized interfaces. However, the
16250 -- actual type may be a non-limited type and hence it does not
16251 -- have such primitives.
16252
16253 if Present (Generic_Actual)
16254 and then not Present (Act_Subp)
16255 and then Is_Limited_Interface (Parent_Base)
16256 and then Is_Predefined_Interface_Primitive (Subp)
16257 then
16258 null;
16259
16260 -- Case 2: Inherit entities associated with interfaces that were
16261 -- not covered by the parent type. We exclude here null interface
16262 -- primitives because they do not need special management.
16263
16264 -- We also exclude interface operations that are renamings. If the
16265 -- subprogram is an explicit renaming of an interface primitive,
16266 -- it is a regular primitive operation, and the presence of its
16267 -- alias is not relevant: it has to be derived like any other
16268 -- primitive.
16269
16270 elsif Present (Alias (Subp))
16271 and then Nkind (Unit_Declaration_Node (Subp)) /=
16272 N_Subprogram_Renaming_Declaration
16273 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16274 and then not
16275 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16276 and then Null_Present (Parent (Alias_Subp)))
16277 then
16278 -- If this is an abstract private type then we transfer the
16279 -- derivation of the interface primitive from the partial view
16280 -- to the full view. This is safe because all the interfaces
16281 -- must be visible in the partial view. Done to avoid adding
16282 -- a new interface derivation to the private part of the
16283 -- enclosing package; otherwise this new derivation would be
16284 -- decorated as hidden when the analysis of the enclosing
16285 -- package completes.
16286
16287 if Is_Abstract_Type (Derived_Type)
16288 and then In_Private_Part (Current_Scope)
16289 and then Has_Private_Declaration (Derived_Type)
16290 then
16291 declare
16292 Partial_View : Entity_Id;
16293 Elmt : Elmt_Id;
16294 Ent : Entity_Id;
16295
16296 begin
16297 Partial_View := First_Entity (Current_Scope);
16298 loop
16299 exit when No (Partial_View)
16300 or else (Has_Private_Declaration (Partial_View)
16301 and then
16302 Full_View (Partial_View) = Derived_Type);
16303
16304 Next_Entity (Partial_View);
16305 end loop;
16306
16307 -- If the partial view was not found then the source code
16308 -- has errors and the derivation is not needed.
16309
16310 if Present (Partial_View) then
16311 Elmt :=
16312 First_Elmt (Primitive_Operations (Partial_View));
16313 while Present (Elmt) loop
16314 Ent := Node (Elmt);
16315
16316 if Present (Alias (Ent))
16317 and then Ultimate_Alias (Ent) = Alias (Subp)
16318 then
16319 Append_Elmt
16320 (Ent, Primitive_Operations (Derived_Type));
16321 exit;
16322 end if;
16323
16324 Next_Elmt (Elmt);
16325 end loop;
16326
16327 -- If the interface primitive was not found in the
16328 -- partial view then this interface primitive was
16329 -- overridden. We add a derivation to activate in
16330 -- Derive_Progenitor_Subprograms the machinery to
16331 -- search for it.
16332
16333 if No (Elmt) then
16334 Derive_Interface_Subprogram
16335 (New_Subp => New_Subp,
16336 Subp => Subp,
16337 Actual_Subp => Act_Subp);
16338 end if;
16339 end if;
16340 end;
16341 else
16342 Derive_Interface_Subprogram
16343 (New_Subp => New_Subp,
16344 Subp => Subp,
16345 Actual_Subp => Act_Subp);
16346 end if;
16347
16348 -- Case 3: Common derivation
16349
16350 else
16351 Derive_Subprogram
16352 (New_Subp => New_Subp,
16353 Parent_Subp => Subp,
16354 Derived_Type => Derived_Type,
16355 Parent_Type => Parent_Base,
16356 Actual_Subp => Act_Subp);
16357 end if;
16358
16359 -- No need to update Act_Elm if we must search for the
16360 -- corresponding operation in the generic actual
16361
16362 if not Need_Search
16363 and then Present (Act_Elmt)
16364 then
16365 Next_Elmt (Act_Elmt);
16366 Act_Subp := Node (Act_Elmt);
16367 end if;
16368
16369 <<Continue>>
16370 Next_Elmt (Elmt);
16371 end loop;
16372
16373 -- Inherit additional operations from progenitors. If the derived
16374 -- type is a generic actual, there are not new primitive operations
16375 -- for the type because it has those of the actual, and therefore
16376 -- nothing needs to be done. The renamings generated above are not
16377 -- primitive operations, and their purpose is simply to make the
16378 -- proper operations visible within an instantiation.
16379
16380 if No (Generic_Actual) then
16381 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16382 end if;
16383 end if;
16384
16385 -- Final check: Direct descendants must have their primitives in the
16386 -- same order. We exclude from this test untagged types and instances
16387 -- of formal derived types. We skip this test if we have already
16388 -- reported serious errors in the sources.
16389
16390 pragma Assert (not Is_Tagged_Type (Derived_Type)
16391 or else Present (Generic_Actual)
16392 or else Serious_Errors_Detected > 0
16393 or else Check_Derived_Type);
16394 end Derive_Subprograms;
16395
16396 --------------------------------
16397 -- Derived_Standard_Character --
16398 --------------------------------
16399
16400 procedure Derived_Standard_Character
16401 (N : Node_Id;
16402 Parent_Type : Entity_Id;
16403 Derived_Type : Entity_Id)
16404 is
16405 Loc : constant Source_Ptr := Sloc (N);
16406 Def : constant Node_Id := Type_Definition (N);
16407 Indic : constant Node_Id := Subtype_Indication (Def);
16408 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16409 Implicit_Base : constant Entity_Id :=
16410 Create_Itype
16411 (E_Enumeration_Type, N, Derived_Type, 'B');
16412
16413 Lo : Node_Id;
16414 Hi : Node_Id;
16415
16416 begin
16417 Discard_Node (Process_Subtype (Indic, N));
16418
16419 Set_Etype (Implicit_Base, Parent_Base);
16420 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16421 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16422
16423 Set_Is_Character_Type (Implicit_Base, True);
16424 Set_Has_Delayed_Freeze (Implicit_Base);
16425
16426 -- The bounds of the implicit base are the bounds of the parent base.
16427 -- Note that their type is the parent base.
16428
16429 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16430 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16431
16432 Set_Scalar_Range (Implicit_Base,
16433 Make_Range (Loc,
16434 Low_Bound => Lo,
16435 High_Bound => Hi));
16436
16437 Conditional_Delay (Derived_Type, Parent_Type);
16438
16439 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16440 Set_Etype (Derived_Type, Implicit_Base);
16441 Set_Size_Info (Derived_Type, Parent_Type);
16442
16443 if Unknown_RM_Size (Derived_Type) then
16444 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16445 end if;
16446
16447 Set_Is_Character_Type (Derived_Type, True);
16448
16449 if Nkind (Indic) /= N_Subtype_Indication then
16450
16451 -- If no explicit constraint, the bounds are those
16452 -- of the parent type.
16453
16454 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16455 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16456 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16457 end if;
16458
16459 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16460
16461 -- Because the implicit base is used in the conversion of the bounds, we
16462 -- have to freeze it now. This is similar to what is done for numeric
16463 -- types, and it equally suspicious, but otherwise a nonstatic bound
16464 -- will have a reference to an unfrozen type, which is rejected by Gigi
16465 -- (???). This requires specific care for definition of stream
16466 -- attributes. For details, see comments at the end of
16467 -- Build_Derived_Numeric_Type.
16468
16469 Freeze_Before (N, Implicit_Base);
16470 end Derived_Standard_Character;
16471
16472 ------------------------------
16473 -- Derived_Type_Declaration --
16474 ------------------------------
16475
16476 procedure Derived_Type_Declaration
16477 (T : Entity_Id;
16478 N : Node_Id;
16479 Is_Completion : Boolean)
16480 is
16481 Parent_Type : Entity_Id;
16482
16483 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16484 -- Check whether the parent type is a generic formal, or derives
16485 -- directly or indirectly from one.
16486
16487 ------------------------
16488 -- Comes_From_Generic --
16489 ------------------------
16490
16491 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16492 begin
16493 if Is_Generic_Type (Typ) then
16494 return True;
16495
16496 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16497 return True;
16498
16499 elsif Is_Private_Type (Typ)
16500 and then Present (Full_View (Typ))
16501 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16502 then
16503 return True;
16504
16505 elsif Is_Generic_Actual_Type (Typ) then
16506 return True;
16507
16508 else
16509 return False;
16510 end if;
16511 end Comes_From_Generic;
16512
16513 -- Local variables
16514
16515 Def : constant Node_Id := Type_Definition (N);
16516 Iface_Def : Node_Id;
16517 Indic : constant Node_Id := Subtype_Indication (Def);
16518 Extension : constant Node_Id := Record_Extension_Part (Def);
16519 Parent_Node : Node_Id;
16520 Taggd : Boolean;
16521
16522 -- Start of processing for Derived_Type_Declaration
16523
16524 begin
16525 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16526
16527 if SPARK_Mode = On
16528 and then Is_Tagged_Type (Parent_Type)
16529 then
16530 declare
16531 Partial_View : constant Entity_Id :=
16532 Incomplete_Or_Partial_View (Parent_Type);
16533
16534 begin
16535 -- If the partial view was not found then the parent type is not
16536 -- a private type. Otherwise check if the partial view is a tagged
16537 -- private type.
16538
16539 if Present (Partial_View)
16540 and then Is_Private_Type (Partial_View)
16541 and then not Is_Tagged_Type (Partial_View)
16542 then
16543 Error_Msg_NE
16544 ("cannot derive from & declared as untagged private "
16545 & "(SPARK RM 3.4(1))", N, Partial_View);
16546 end if;
16547 end;
16548 end if;
16549
16550 -- Ada 2005 (AI-251): In case of interface derivation check that the
16551 -- parent is also an interface.
16552
16553 if Interface_Present (Def) then
16554 if not Is_Interface (Parent_Type) then
16555 Diagnose_Interface (Indic, Parent_Type);
16556
16557 else
16558 Parent_Node := Parent (Base_Type (Parent_Type));
16559 Iface_Def := Type_Definition (Parent_Node);
16560
16561 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16562 -- other limited interfaces.
16563
16564 if Limited_Present (Def) then
16565 if Limited_Present (Iface_Def) then
16566 null;
16567
16568 elsif Protected_Present (Iface_Def) then
16569 Error_Msg_NE
16570 ("descendant of & must be declared as a protected "
16571 & "interface", N, Parent_Type);
16572
16573 elsif Synchronized_Present (Iface_Def) then
16574 Error_Msg_NE
16575 ("descendant of & must be declared as a synchronized "
16576 & "interface", N, Parent_Type);
16577
16578 elsif Task_Present (Iface_Def) then
16579 Error_Msg_NE
16580 ("descendant of & must be declared as a task interface",
16581 N, Parent_Type);
16582
16583 else
16584 Error_Msg_N
16585 ("(Ada 2005) limited interface cannot inherit from "
16586 & "non-limited interface", Indic);
16587 end if;
16588
16589 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16590 -- from non-limited or limited interfaces.
16591
16592 elsif not Protected_Present (Def)
16593 and then not Synchronized_Present (Def)
16594 and then not Task_Present (Def)
16595 then
16596 if Limited_Present (Iface_Def) then
16597 null;
16598
16599 elsif Protected_Present (Iface_Def) then
16600 Error_Msg_NE
16601 ("descendant of & must be declared as a protected "
16602 & "interface", N, Parent_Type);
16603
16604 elsif Synchronized_Present (Iface_Def) then
16605 Error_Msg_NE
16606 ("descendant of & must be declared as a synchronized "
16607 & "interface", N, Parent_Type);
16608
16609 elsif Task_Present (Iface_Def) then
16610 Error_Msg_NE
16611 ("descendant of & must be declared as a task interface",
16612 N, Parent_Type);
16613 else
16614 null;
16615 end if;
16616 end if;
16617 end if;
16618 end if;
16619
16620 if Is_Tagged_Type (Parent_Type)
16621 and then Is_Concurrent_Type (Parent_Type)
16622 and then not Is_Interface (Parent_Type)
16623 then
16624 Error_Msg_N
16625 ("parent type of a record extension cannot be a synchronized "
16626 & "tagged type (RM 3.9.1 (3/1))", N);
16627 Set_Etype (T, Any_Type);
16628 return;
16629 end if;
16630
16631 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16632 -- interfaces
16633
16634 if Is_Tagged_Type (Parent_Type)
16635 and then Is_Non_Empty_List (Interface_List (Def))
16636 then
16637 declare
16638 Intf : Node_Id;
16639 T : Entity_Id;
16640
16641 begin
16642 Intf := First (Interface_List (Def));
16643 while Present (Intf) loop
16644 T := Find_Type_Of_Subtype_Indic (Intf);
16645
16646 if not Is_Interface (T) then
16647 Diagnose_Interface (Intf, T);
16648
16649 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16650 -- a limited type from having a nonlimited progenitor.
16651
16652 elsif (Limited_Present (Def)
16653 or else (not Is_Interface (Parent_Type)
16654 and then Is_Limited_Type (Parent_Type)))
16655 and then not Is_Limited_Interface (T)
16656 then
16657 Error_Msg_NE
16658 ("progenitor interface& of limited type must be limited",
16659 N, T);
16660 end if;
16661
16662 Next (Intf);
16663 end loop;
16664 end;
16665 end if;
16666
16667 if Parent_Type = Any_Type
16668 or else Etype (Parent_Type) = Any_Type
16669 or else (Is_Class_Wide_Type (Parent_Type)
16670 and then Etype (Parent_Type) = T)
16671 then
16672 -- If Parent_Type is undefined or illegal, make new type into a
16673 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16674 -- errors. If this is a self-definition, emit error now.
16675
16676 if T = Parent_Type or else T = Etype (Parent_Type) then
16677 Error_Msg_N ("type cannot be used in its own definition", Indic);
16678 end if;
16679
16680 Set_Ekind (T, Ekind (Parent_Type));
16681 Set_Etype (T, Any_Type);
16682 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16683
16684 if Is_Tagged_Type (T)
16685 and then Is_Record_Type (T)
16686 then
16687 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16688 end if;
16689
16690 return;
16691 end if;
16692
16693 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16694 -- an interface is special because the list of interfaces in the full
16695 -- view can be given in any order. For example:
16696
16697 -- type A is interface;
16698 -- type B is interface and A;
16699 -- type D is new B with private;
16700 -- private
16701 -- type D is new A and B with null record; -- 1 --
16702
16703 -- In this case we perform the following transformation of -1-:
16704
16705 -- type D is new B and A with null record;
16706
16707 -- If the parent of the full-view covers the parent of the partial-view
16708 -- we have two possible cases:
16709
16710 -- 1) They have the same parent
16711 -- 2) The parent of the full-view implements some further interfaces
16712
16713 -- In both cases we do not need to perform the transformation. In the
16714 -- first case the source program is correct and the transformation is
16715 -- not needed; in the second case the source program does not fulfill
16716 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16717 -- later.
16718
16719 -- This transformation not only simplifies the rest of the analysis of
16720 -- this type declaration but also simplifies the correct generation of
16721 -- the object layout to the expander.
16722
16723 if In_Private_Part (Current_Scope)
16724 and then Is_Interface (Parent_Type)
16725 then
16726 declare
16727 Iface : Node_Id;
16728 Partial_View : Entity_Id;
16729 Partial_View_Parent : Entity_Id;
16730 New_Iface : Node_Id;
16731
16732 begin
16733 -- Look for the associated private type declaration
16734
16735 Partial_View := Incomplete_Or_Partial_View (T);
16736
16737 -- If the partial view was not found then the source code has
16738 -- errors and the transformation is not needed.
16739
16740 if Present (Partial_View) then
16741 Partial_View_Parent := Etype (Partial_View);
16742
16743 -- If the parent of the full-view covers the parent of the
16744 -- partial-view we have nothing else to do.
16745
16746 if Interface_Present_In_Ancestor
16747 (Parent_Type, Partial_View_Parent)
16748 then
16749 null;
16750
16751 -- Traverse the list of interfaces of the full-view to look
16752 -- for the parent of the partial-view and perform the tree
16753 -- transformation.
16754
16755 else
16756 Iface := First (Interface_List (Def));
16757 while Present (Iface) loop
16758 if Etype (Iface) = Etype (Partial_View) then
16759 Rewrite (Subtype_Indication (Def),
16760 New_Copy (Subtype_Indication
16761 (Parent (Partial_View))));
16762
16763 New_Iface :=
16764 Make_Identifier (Sloc (N), Chars (Parent_Type));
16765 Append (New_Iface, Interface_List (Def));
16766
16767 -- Analyze the transformed code
16768
16769 Derived_Type_Declaration (T, N, Is_Completion);
16770 return;
16771 end if;
16772
16773 Next (Iface);
16774 end loop;
16775 end if;
16776 end if;
16777 end;
16778 end if;
16779
16780 -- Only composite types other than array types are allowed to have
16781 -- discriminants.
16782
16783 if Present (Discriminant_Specifications (N)) then
16784 if (Is_Elementary_Type (Parent_Type)
16785 or else
16786 Is_Array_Type (Parent_Type))
16787 and then not Error_Posted (N)
16788 then
16789 Error_Msg_N
16790 ("elementary or array type cannot have discriminants",
16791 Defining_Identifier (First (Discriminant_Specifications (N))));
16792
16793 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16794 -- only if we are not already processing a malformed syntax tree.
16795
16796 if Is_Type (T) then
16797 Set_Has_Discriminants (T, False);
16798 end if;
16799 end if;
16800 end if;
16801
16802 -- In Ada 83, a derived type defined in a package specification cannot
16803 -- be used for further derivation until the end of its visible part.
16804 -- Note that derivation in the private part of the package is allowed.
16805
16806 if Ada_Version = Ada_83
16807 and then Is_Derived_Type (Parent_Type)
16808 and then In_Visible_Part (Scope (Parent_Type))
16809 then
16810 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16811 Error_Msg_N
16812 ("(Ada 83): premature use of type for derivation", Indic);
16813 end if;
16814 end if;
16815
16816 -- Check for early use of incomplete or private type
16817
16818 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16819 Error_Msg_N ("premature derivation of incomplete type", Indic);
16820 return;
16821
16822 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16823 and then not Comes_From_Generic (Parent_Type))
16824 or else Has_Private_Component (Parent_Type)
16825 then
16826 -- The ancestor type of a formal type can be incomplete, in which
16827 -- case only the operations of the partial view are available in the
16828 -- generic. Subsequent checks may be required when the full view is
16829 -- analyzed to verify that a derivation from a tagged type has an
16830 -- extension.
16831
16832 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16833 null;
16834
16835 elsif No (Underlying_Type (Parent_Type))
16836 or else Has_Private_Component (Parent_Type)
16837 then
16838 Error_Msg_N
16839 ("premature derivation of derived or private type", Indic);
16840
16841 -- Flag the type itself as being in error, this prevents some
16842 -- nasty problems with subsequent uses of the malformed type.
16843
16844 Set_Error_Posted (T);
16845
16846 -- Check that within the immediate scope of an untagged partial
16847 -- view it's illegal to derive from the partial view if the
16848 -- full view is tagged. (7.3(7))
16849
16850 -- We verify that the Parent_Type is a partial view by checking
16851 -- that it is not a Full_Type_Declaration (i.e. a private type or
16852 -- private extension declaration), to distinguish a partial view
16853 -- from a derivation from a private type which also appears as
16854 -- E_Private_Type. If the parent base type is not declared in an
16855 -- enclosing scope there is no need to check.
16856
16857 elsif Present (Full_View (Parent_Type))
16858 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16859 and then not Is_Tagged_Type (Parent_Type)
16860 and then Is_Tagged_Type (Full_View (Parent_Type))
16861 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16862 then
16863 Error_Msg_N
16864 ("premature derivation from type with tagged full view",
16865 Indic);
16866 end if;
16867 end if;
16868
16869 -- Check that form of derivation is appropriate
16870
16871 Taggd := Is_Tagged_Type (Parent_Type);
16872
16873 -- Set the parent type to the class-wide type's specific type in this
16874 -- case to prevent cascading errors
16875
16876 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16877 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16878 Set_Etype (T, Etype (Parent_Type));
16879 return;
16880 end if;
16881
16882 if Present (Extension) and then not Taggd then
16883 Error_Msg_N
16884 ("type derived from untagged type cannot have extension", Indic);
16885
16886 elsif No (Extension) and then Taggd then
16887
16888 -- If this declaration is within a private part (or body) of a
16889 -- generic instantiation then the derivation is allowed (the parent
16890 -- type can only appear tagged in this case if it's a generic actual
16891 -- type, since it would otherwise have been rejected in the analysis
16892 -- of the generic template).
16893
16894 if not Is_Generic_Actual_Type (Parent_Type)
16895 or else In_Visible_Part (Scope (Parent_Type))
16896 then
16897 if Is_Class_Wide_Type (Parent_Type) then
16898 Error_Msg_N
16899 ("parent type must not be a class-wide type", Indic);
16900
16901 -- Use specific type to prevent cascaded errors.
16902
16903 Parent_Type := Etype (Parent_Type);
16904
16905 else
16906 Error_Msg_N
16907 ("type derived from tagged type must have extension", Indic);
16908 end if;
16909 end if;
16910 end if;
16911
16912 -- AI-443: Synchronized formal derived types require a private
16913 -- extension. There is no point in checking the ancestor type or
16914 -- the progenitors since the construct is wrong to begin with.
16915
16916 if Ada_Version >= Ada_2005
16917 and then Is_Generic_Type (T)
16918 and then Present (Original_Node (N))
16919 then
16920 declare
16921 Decl : constant Node_Id := Original_Node (N);
16922
16923 begin
16924 if Nkind (Decl) = N_Formal_Type_Declaration
16925 and then Nkind (Formal_Type_Definition (Decl)) =
16926 N_Formal_Derived_Type_Definition
16927 and then Synchronized_Present (Formal_Type_Definition (Decl))
16928 and then No (Extension)
16929
16930 -- Avoid emitting a duplicate error message
16931
16932 and then not Error_Posted (Indic)
16933 then
16934 Error_Msg_N
16935 ("synchronized derived type must have extension", N);
16936 end if;
16937 end;
16938 end if;
16939
16940 if Null_Exclusion_Present (Def)
16941 and then not Is_Access_Type (Parent_Type)
16942 then
16943 Error_Msg_N ("null exclusion can only apply to an access type", N);
16944 end if;
16945
16946 -- Avoid deriving parent primitives of underlying record views
16947
16948 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16949 Derive_Subps => not Is_Underlying_Record_View (T));
16950
16951 -- AI-419: The parent type of an explicitly limited derived type must
16952 -- be a limited type or a limited interface.
16953
16954 if Limited_Present (Def) then
16955 Set_Is_Limited_Record (T);
16956
16957 if Is_Interface (T) then
16958 Set_Is_Limited_Interface (T);
16959 end if;
16960
16961 if not Is_Limited_Type (Parent_Type)
16962 and then
16963 (not Is_Interface (Parent_Type)
16964 or else not Is_Limited_Interface (Parent_Type))
16965 then
16966 -- AI05-0096: a derivation in the private part of an instance is
16967 -- legal if the generic formal is untagged limited, and the actual
16968 -- is non-limited.
16969
16970 if Is_Generic_Actual_Type (Parent_Type)
16971 and then In_Private_Part (Current_Scope)
16972 and then
16973 not Is_Tagged_Type
16974 (Generic_Parent_Type (Parent (Parent_Type)))
16975 then
16976 null;
16977
16978 else
16979 Error_Msg_NE
16980 ("parent type& of limited type must be limited",
16981 N, Parent_Type);
16982 end if;
16983 end if;
16984 end if;
16985 end Derived_Type_Declaration;
16986
16987 ------------------------
16988 -- Diagnose_Interface --
16989 ------------------------
16990
16991 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16992 begin
16993 if not Is_Interface (E) and then E /= Any_Type then
16994 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16995 end if;
16996 end Diagnose_Interface;
16997
16998 ----------------------------------
16999 -- Enumeration_Type_Declaration --
17000 ----------------------------------
17001
17002 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17003 Ev : Uint;
17004 L : Node_Id;
17005 R_Node : Node_Id;
17006 B_Node : Node_Id;
17007
17008 begin
17009 -- Create identifier node representing lower bound
17010
17011 B_Node := New_Node (N_Identifier, Sloc (Def));
17012 L := First (Literals (Def));
17013 Set_Chars (B_Node, Chars (L));
17014 Set_Entity (B_Node, L);
17015 Set_Etype (B_Node, T);
17016 Set_Is_Static_Expression (B_Node, True);
17017
17018 R_Node := New_Node (N_Range, Sloc (Def));
17019 Set_Low_Bound (R_Node, B_Node);
17020
17021 Set_Ekind (T, E_Enumeration_Type);
17022 Set_First_Literal (T, L);
17023 Set_Etype (T, T);
17024 Set_Is_Constrained (T);
17025
17026 Ev := Uint_0;
17027
17028 -- Loop through literals of enumeration type setting pos and rep values
17029 -- except that if the Ekind is already set, then it means the literal
17030 -- was already constructed (case of a derived type declaration and we
17031 -- should not disturb the Pos and Rep values.
17032
17033 while Present (L) loop
17034 if Ekind (L) /= E_Enumeration_Literal then
17035 Set_Ekind (L, E_Enumeration_Literal);
17036 Set_Enumeration_Pos (L, Ev);
17037 Set_Enumeration_Rep (L, Ev);
17038 Set_Is_Known_Valid (L, True);
17039 end if;
17040
17041 Set_Etype (L, T);
17042 New_Overloaded_Entity (L);
17043 Generate_Definition (L);
17044 Set_Convention (L, Convention_Intrinsic);
17045
17046 -- Case of character literal
17047
17048 if Nkind (L) = N_Defining_Character_Literal then
17049 Set_Is_Character_Type (T, True);
17050
17051 -- Check violation of No_Wide_Characters
17052
17053 if Restriction_Check_Required (No_Wide_Characters) then
17054 Get_Name_String (Chars (L));
17055
17056 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17057 Check_Restriction (No_Wide_Characters, L);
17058 end if;
17059 end if;
17060 end if;
17061
17062 Ev := Ev + 1;
17063 Next (L);
17064 end loop;
17065
17066 -- Now create a node representing upper bound
17067
17068 B_Node := New_Node (N_Identifier, Sloc (Def));
17069 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17070 Set_Entity (B_Node, Last (Literals (Def)));
17071 Set_Etype (B_Node, T);
17072 Set_Is_Static_Expression (B_Node, True);
17073
17074 Set_High_Bound (R_Node, B_Node);
17075
17076 -- Initialize various fields of the type. Some of this information
17077 -- may be overwritten later through rep.clauses.
17078
17079 Set_Scalar_Range (T, R_Node);
17080 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17081 Set_Enum_Esize (T);
17082 Set_Enum_Pos_To_Rep (T, Empty);
17083
17084 -- Set Discard_Names if configuration pragma set, or if there is
17085 -- a parameterless pragma in the current declarative region
17086
17087 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17088 Set_Discard_Names (T);
17089 end if;
17090
17091 -- Process end label if there is one
17092
17093 if Present (Def) then
17094 Process_End_Label (Def, 'e', T);
17095 end if;
17096 end Enumeration_Type_Declaration;
17097
17098 ---------------------------------
17099 -- Expand_To_Stored_Constraint --
17100 ---------------------------------
17101
17102 function Expand_To_Stored_Constraint
17103 (Typ : Entity_Id;
17104 Constraint : Elist_Id) return Elist_Id
17105 is
17106 Explicitly_Discriminated_Type : Entity_Id;
17107 Expansion : Elist_Id;
17108 Discriminant : Entity_Id;
17109
17110 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17111 -- Find the nearest type that actually specifies discriminants
17112
17113 ---------------------------------
17114 -- Type_With_Explicit_Discrims --
17115 ---------------------------------
17116
17117 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17118 Typ : constant E := Base_Type (Id);
17119
17120 begin
17121 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17122 if Present (Full_View (Typ)) then
17123 return Type_With_Explicit_Discrims (Full_View (Typ));
17124 end if;
17125
17126 else
17127 if Has_Discriminants (Typ) then
17128 return Typ;
17129 end if;
17130 end if;
17131
17132 if Etype (Typ) = Typ then
17133 return Empty;
17134 elsif Has_Discriminants (Typ) then
17135 return Typ;
17136 else
17137 return Type_With_Explicit_Discrims (Etype (Typ));
17138 end if;
17139
17140 end Type_With_Explicit_Discrims;
17141
17142 -- Start of processing for Expand_To_Stored_Constraint
17143
17144 begin
17145 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17146 return No_Elist;
17147 end if;
17148
17149 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17150
17151 if No (Explicitly_Discriminated_Type) then
17152 return No_Elist;
17153 end if;
17154
17155 Expansion := New_Elmt_List;
17156
17157 Discriminant :=
17158 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17159 while Present (Discriminant) loop
17160 Append_Elmt
17161 (Get_Discriminant_Value
17162 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17163 To => Expansion);
17164 Next_Stored_Discriminant (Discriminant);
17165 end loop;
17166
17167 return Expansion;
17168 end Expand_To_Stored_Constraint;
17169
17170 ---------------------------
17171 -- Find_Hidden_Interface --
17172 ---------------------------
17173
17174 function Find_Hidden_Interface
17175 (Src : Elist_Id;
17176 Dest : Elist_Id) return Entity_Id
17177 is
17178 Iface : Entity_Id;
17179 Iface_Elmt : Elmt_Id;
17180
17181 begin
17182 if Present (Src) and then Present (Dest) then
17183 Iface_Elmt := First_Elmt (Src);
17184 while Present (Iface_Elmt) loop
17185 Iface := Node (Iface_Elmt);
17186
17187 if Is_Interface (Iface)
17188 and then not Contain_Interface (Iface, Dest)
17189 then
17190 return Iface;
17191 end if;
17192
17193 Next_Elmt (Iface_Elmt);
17194 end loop;
17195 end if;
17196
17197 return Empty;
17198 end Find_Hidden_Interface;
17199
17200 --------------------
17201 -- Find_Type_Name --
17202 --------------------
17203
17204 function Find_Type_Name (N : Node_Id) return Entity_Id is
17205 Id : constant Entity_Id := Defining_Identifier (N);
17206 New_Id : Entity_Id;
17207 Prev : Entity_Id;
17208 Prev_Par : Node_Id;
17209
17210 procedure Check_Duplicate_Aspects;
17211 -- Check that aspects specified in a completion have not been specified
17212 -- already in the partial view.
17213
17214 procedure Tag_Mismatch;
17215 -- Diagnose a tagged partial view whose full view is untagged. We post
17216 -- the message on the full view, with a reference to the previous
17217 -- partial view. The partial view can be private or incomplete, and
17218 -- these are handled in a different manner, so we determine the position
17219 -- of the error message from the respective slocs of both.
17220
17221 -----------------------------
17222 -- Check_Duplicate_Aspects --
17223 -----------------------------
17224
17225 procedure Check_Duplicate_Aspects is
17226 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17227 -- Return the corresponding aspect of the partial view which matches
17228 -- the aspect id of Asp. Return Empty is no such aspect exists.
17229
17230 -----------------------------
17231 -- Get_Partial_View_Aspect --
17232 -----------------------------
17233
17234 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17235 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17236 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17237 Prev_Asp : Node_Id;
17238
17239 begin
17240 if Present (Prev_Asps) then
17241 Prev_Asp := First (Prev_Asps);
17242 while Present (Prev_Asp) loop
17243 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17244 return Prev_Asp;
17245 end if;
17246
17247 Next (Prev_Asp);
17248 end loop;
17249 end if;
17250
17251 return Empty;
17252 end Get_Partial_View_Aspect;
17253
17254 -- Local variables
17255
17256 Full_Asps : constant List_Id := Aspect_Specifications (N);
17257 Full_Asp : Node_Id;
17258 Part_Asp : Node_Id;
17259
17260 -- Start of processing for Check_Duplicate_Aspects
17261
17262 begin
17263 if Present (Full_Asps) then
17264 Full_Asp := First (Full_Asps);
17265 while Present (Full_Asp) loop
17266 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17267
17268 -- An aspect and its class-wide counterpart are two distinct
17269 -- aspects and may apply to both views of an entity.
17270
17271 if Present (Part_Asp)
17272 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17273 then
17274 Error_Msg_N
17275 ("aspect already specified in private declaration",
17276 Full_Asp);
17277
17278 Remove (Full_Asp);
17279 return;
17280 end if;
17281
17282 if Has_Discriminants (Prev)
17283 and then not Has_Unknown_Discriminants (Prev)
17284 and then Get_Aspect_Id (Full_Asp) =
17285 Aspect_Implicit_Dereference
17286 then
17287 Error_Msg_N
17288 ("cannot specify aspect if partial view has known "
17289 & "discriminants", Full_Asp);
17290 end if;
17291
17292 Next (Full_Asp);
17293 end loop;
17294 end if;
17295 end Check_Duplicate_Aspects;
17296
17297 ------------------
17298 -- Tag_Mismatch --
17299 ------------------
17300
17301 procedure Tag_Mismatch is
17302 begin
17303 if Sloc (Prev) < Sloc (Id) then
17304 if Ada_Version >= Ada_2012
17305 and then Nkind (N) = N_Private_Type_Declaration
17306 then
17307 Error_Msg_NE
17308 ("declaration of private } must be a tagged type ", Id, Prev);
17309 else
17310 Error_Msg_NE
17311 ("full declaration of } must be a tagged type ", Id, Prev);
17312 end if;
17313
17314 else
17315 if Ada_Version >= Ada_2012
17316 and then Nkind (N) = N_Private_Type_Declaration
17317 then
17318 Error_Msg_NE
17319 ("declaration of private } must be a tagged type ", Prev, Id);
17320 else
17321 Error_Msg_NE
17322 ("full declaration of } must be a tagged type ", Prev, Id);
17323 end if;
17324 end if;
17325 end Tag_Mismatch;
17326
17327 -- Start of processing for Find_Type_Name
17328
17329 begin
17330 -- Find incomplete declaration, if one was given
17331
17332 Prev := Current_Entity_In_Scope (Id);
17333
17334 -- New type declaration
17335
17336 if No (Prev) then
17337 Enter_Name (Id);
17338 return Id;
17339
17340 -- Previous declaration exists
17341
17342 else
17343 Prev_Par := Parent (Prev);
17344
17345 -- Error if not incomplete/private case except if previous
17346 -- declaration is implicit, etc. Enter_Name will emit error if
17347 -- appropriate.
17348
17349 if not Is_Incomplete_Or_Private_Type (Prev) then
17350 Enter_Name (Id);
17351 New_Id := Id;
17352
17353 -- Check invalid completion of private or incomplete type
17354
17355 elsif not Nkind_In (N, N_Full_Type_Declaration,
17356 N_Task_Type_Declaration,
17357 N_Protected_Type_Declaration)
17358 and then
17359 (Ada_Version < Ada_2012
17360 or else not Is_Incomplete_Type (Prev)
17361 or else not Nkind_In (N, N_Private_Type_Declaration,
17362 N_Private_Extension_Declaration))
17363 then
17364 -- Completion must be a full type declarations (RM 7.3(4))
17365
17366 Error_Msg_Sloc := Sloc (Prev);
17367 Error_Msg_NE ("invalid completion of }", Id, Prev);
17368
17369 -- Set scope of Id to avoid cascaded errors. Entity is never
17370 -- examined again, except when saving globals in generics.
17371
17372 Set_Scope (Id, Current_Scope);
17373 New_Id := Id;
17374
17375 -- If this is a repeated incomplete declaration, no further
17376 -- checks are possible.
17377
17378 if Nkind (N) = N_Incomplete_Type_Declaration then
17379 return Prev;
17380 end if;
17381
17382 -- Case of full declaration of incomplete type
17383
17384 elsif Ekind (Prev) = E_Incomplete_Type
17385 and then (Ada_Version < Ada_2012
17386 or else No (Full_View (Prev))
17387 or else not Is_Private_Type (Full_View (Prev)))
17388 then
17389 -- Indicate that the incomplete declaration has a matching full
17390 -- declaration. The defining occurrence of the incomplete
17391 -- declaration remains the visible one, and the procedure
17392 -- Get_Full_View dereferences it whenever the type is used.
17393
17394 if Present (Full_View (Prev)) then
17395 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17396 end if;
17397
17398 Set_Full_View (Prev, Id);
17399 Append_Entity (Id, Current_Scope);
17400 Set_Is_Public (Id, Is_Public (Prev));
17401 Set_Is_Internal (Id);
17402 New_Id := Prev;
17403
17404 -- If the incomplete view is tagged, a class_wide type has been
17405 -- created already. Use it for the private type as well, in order
17406 -- to prevent multiple incompatible class-wide types that may be
17407 -- created for self-referential anonymous access components.
17408
17409 if Is_Tagged_Type (Prev)
17410 and then Present (Class_Wide_Type (Prev))
17411 then
17412 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17413 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17414
17415 -- Type of the class-wide type is the current Id. Previously
17416 -- this was not done for private declarations because of order-
17417 -- of-elaboration issues in the back end, but gigi now handles
17418 -- this properly.
17419
17420 Set_Etype (Class_Wide_Type (Id), Id);
17421 end if;
17422
17423 -- Case of full declaration of private type
17424
17425 else
17426 -- If the private type was a completion of an incomplete type then
17427 -- update Prev to reference the private type
17428
17429 if Ada_Version >= Ada_2012
17430 and then Ekind (Prev) = E_Incomplete_Type
17431 and then Present (Full_View (Prev))
17432 and then Is_Private_Type (Full_View (Prev))
17433 then
17434 Prev := Full_View (Prev);
17435 Prev_Par := Parent (Prev);
17436 end if;
17437
17438 if Nkind (N) = N_Full_Type_Declaration
17439 and then Nkind_In
17440 (Type_Definition (N), N_Record_Definition,
17441 N_Derived_Type_Definition)
17442 and then Interface_Present (Type_Definition (N))
17443 then
17444 Error_Msg_N
17445 ("completion of private type cannot be an interface", N);
17446 end if;
17447
17448 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17449 if Etype (Prev) /= Prev then
17450
17451 -- Prev is a private subtype or a derived type, and needs
17452 -- no completion.
17453
17454 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17455 New_Id := Id;
17456
17457 elsif Ekind (Prev) = E_Private_Type
17458 and then Nkind_In (N, N_Task_Type_Declaration,
17459 N_Protected_Type_Declaration)
17460 then
17461 Error_Msg_N
17462 ("completion of nonlimited type cannot be limited", N);
17463
17464 elsif Ekind (Prev) = E_Record_Type_With_Private
17465 and then Nkind_In (N, N_Task_Type_Declaration,
17466 N_Protected_Type_Declaration)
17467 then
17468 if not Is_Limited_Record (Prev) then
17469 Error_Msg_N
17470 ("completion of nonlimited type cannot be limited", N);
17471
17472 elsif No (Interface_List (N)) then
17473 Error_Msg_N
17474 ("completion of tagged private type must be tagged",
17475 N);
17476 end if;
17477 end if;
17478
17479 -- Ada 2005 (AI-251): Private extension declaration of a task
17480 -- type or a protected type. This case arises when covering
17481 -- interface types.
17482
17483 elsif Nkind_In (N, N_Task_Type_Declaration,
17484 N_Protected_Type_Declaration)
17485 then
17486 null;
17487
17488 elsif Nkind (N) /= N_Full_Type_Declaration
17489 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17490 then
17491 Error_Msg_N
17492 ("full view of private extension must be an extension", N);
17493
17494 elsif not (Abstract_Present (Parent (Prev)))
17495 and then Abstract_Present (Type_Definition (N))
17496 then
17497 Error_Msg_N
17498 ("full view of non-abstract extension cannot be abstract", N);
17499 end if;
17500
17501 if not In_Private_Part (Current_Scope) then
17502 Error_Msg_N
17503 ("declaration of full view must appear in private part", N);
17504 end if;
17505
17506 if Ada_Version >= Ada_2012 then
17507 Check_Duplicate_Aspects;
17508 end if;
17509
17510 Copy_And_Swap (Prev, Id);
17511 Set_Has_Private_Declaration (Prev);
17512 Set_Has_Private_Declaration (Id);
17513
17514 -- AI12-0133: Indicate whether we have a partial view with
17515 -- unknown discriminants, in which case initialization of objects
17516 -- of the type do not receive an invariant check.
17517
17518 Set_Partial_View_Has_Unknown_Discr
17519 (Prev, Has_Unknown_Discriminants (Id));
17520
17521 -- Preserve aspect and iterator flags that may have been set on
17522 -- the partial view.
17523
17524 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17525 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17526
17527 -- If no error, propagate freeze_node from private to full view.
17528 -- It may have been generated for an early operational item.
17529
17530 if Present (Freeze_Node (Id))
17531 and then Serious_Errors_Detected = 0
17532 and then No (Full_View (Id))
17533 then
17534 Set_Freeze_Node (Prev, Freeze_Node (Id));
17535 Set_Freeze_Node (Id, Empty);
17536 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17537 end if;
17538
17539 Set_Full_View (Id, Prev);
17540 New_Id := Prev;
17541 end if;
17542
17543 -- Verify that full declaration conforms to partial one
17544
17545 if Is_Incomplete_Or_Private_Type (Prev)
17546 and then Present (Discriminant_Specifications (Prev_Par))
17547 then
17548 if Present (Discriminant_Specifications (N)) then
17549 if Ekind (Prev) = E_Incomplete_Type then
17550 Check_Discriminant_Conformance (N, Prev, Prev);
17551 else
17552 Check_Discriminant_Conformance (N, Prev, Id);
17553 end if;
17554
17555 else
17556 Error_Msg_N
17557 ("missing discriminants in full type declaration", N);
17558
17559 -- To avoid cascaded errors on subsequent use, share the
17560 -- discriminants of the partial view.
17561
17562 Set_Discriminant_Specifications (N,
17563 Discriminant_Specifications (Prev_Par));
17564 end if;
17565 end if;
17566
17567 -- A prior untagged partial view can have an associated class-wide
17568 -- type due to use of the class attribute, and in this case the full
17569 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17570 -- of incomplete tagged declarations, but we check for it.
17571
17572 if Is_Type (Prev)
17573 and then (Is_Tagged_Type (Prev)
17574 or else Present (Class_Wide_Type (Prev)))
17575 then
17576 -- Ada 2012 (AI05-0162): A private type may be the completion of
17577 -- an incomplete type.
17578
17579 if Ada_Version >= Ada_2012
17580 and then Is_Incomplete_Type (Prev)
17581 and then Nkind_In (N, N_Private_Type_Declaration,
17582 N_Private_Extension_Declaration)
17583 then
17584 -- No need to check private extensions since they are tagged
17585
17586 if Nkind (N) = N_Private_Type_Declaration
17587 and then not Tagged_Present (N)
17588 then
17589 Tag_Mismatch;
17590 end if;
17591
17592 -- The full declaration is either a tagged type (including
17593 -- a synchronized type that implements interfaces) or a
17594 -- type extension, otherwise this is an error.
17595
17596 elsif Nkind_In (N, N_Task_Type_Declaration,
17597 N_Protected_Type_Declaration)
17598 then
17599 if No (Interface_List (N)) and then not Error_Posted (N) then
17600 Tag_Mismatch;
17601 end if;
17602
17603 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17604
17605 -- Indicate that the previous declaration (tagged incomplete
17606 -- or private declaration) requires the same on the full one.
17607
17608 if not Tagged_Present (Type_Definition (N)) then
17609 Tag_Mismatch;
17610 Set_Is_Tagged_Type (Id);
17611 end if;
17612
17613 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17614 if No (Record_Extension_Part (Type_Definition (N))) then
17615 Error_Msg_NE
17616 ("full declaration of } must be a record extension",
17617 Prev, Id);
17618
17619 -- Set some attributes to produce a usable full view
17620
17621 Set_Is_Tagged_Type (Id);
17622 end if;
17623
17624 else
17625 Tag_Mismatch;
17626 end if;
17627 end if;
17628
17629 if Present (Prev)
17630 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17631 and then Present (Premature_Use (Parent (Prev)))
17632 then
17633 Error_Msg_Sloc := Sloc (N);
17634 Error_Msg_N
17635 ("\full declaration #", Premature_Use (Parent (Prev)));
17636 end if;
17637
17638 return New_Id;
17639 end if;
17640 end Find_Type_Name;
17641
17642 -------------------------
17643 -- Find_Type_Of_Object --
17644 -------------------------
17645
17646 function Find_Type_Of_Object
17647 (Obj_Def : Node_Id;
17648 Related_Nod : Node_Id) return Entity_Id
17649 is
17650 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17651 P : Node_Id := Parent (Obj_Def);
17652 T : Entity_Id;
17653 Nam : Name_Id;
17654
17655 begin
17656 -- If the parent is a component_definition node we climb to the
17657 -- component_declaration node
17658
17659 if Nkind (P) = N_Component_Definition then
17660 P := Parent (P);
17661 end if;
17662
17663 -- Case of an anonymous array subtype
17664
17665 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17666 N_Unconstrained_Array_Definition)
17667 then
17668 T := Empty;
17669 Array_Type_Declaration (T, Obj_Def);
17670
17671 -- Create an explicit subtype whenever possible
17672
17673 elsif Nkind (P) /= N_Component_Declaration
17674 and then Def_Kind = N_Subtype_Indication
17675 then
17676 -- Base name of subtype on object name, which will be unique in
17677 -- the current scope.
17678
17679 -- If this is a duplicate declaration, return base type, to avoid
17680 -- generating duplicate anonymous types.
17681
17682 if Error_Posted (P) then
17683 Analyze (Subtype_Mark (Obj_Def));
17684 return Entity (Subtype_Mark (Obj_Def));
17685 end if;
17686
17687 Nam :=
17688 New_External_Name
17689 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17690
17691 T := Make_Defining_Identifier (Sloc (P), Nam);
17692
17693 Insert_Action (Obj_Def,
17694 Make_Subtype_Declaration (Sloc (P),
17695 Defining_Identifier => T,
17696 Subtype_Indication => Relocate_Node (Obj_Def)));
17697
17698 -- This subtype may need freezing, and this will not be done
17699 -- automatically if the object declaration is not in declarative
17700 -- part. Since this is an object declaration, the type cannot always
17701 -- be frozen here. Deferred constants do not freeze their type
17702 -- (which often enough will be private).
17703
17704 if Nkind (P) = N_Object_Declaration
17705 and then Constant_Present (P)
17706 and then No (Expression (P))
17707 then
17708 null;
17709
17710 -- Here we freeze the base type of object type to catch premature use
17711 -- of discriminated private type without a full view.
17712
17713 else
17714 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17715 end if;
17716
17717 -- Ada 2005 AI-406: the object definition in an object declaration
17718 -- can be an access definition.
17719
17720 elsif Def_Kind = N_Access_Definition then
17721 T := Access_Definition (Related_Nod, Obj_Def);
17722
17723 Set_Is_Local_Anonymous_Access
17724 (T,
17725 V => (Ada_Version < Ada_2012)
17726 or else (Nkind (P) /= N_Object_Declaration)
17727 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17728
17729 -- Otherwise, the object definition is just a subtype_mark
17730
17731 else
17732 T := Process_Subtype (Obj_Def, Related_Nod);
17733 end if;
17734
17735 return T;
17736 end Find_Type_Of_Object;
17737
17738 --------------------------------
17739 -- Find_Type_Of_Subtype_Indic --
17740 --------------------------------
17741
17742 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17743 Typ : Entity_Id;
17744
17745 begin
17746 -- Case of subtype mark with a constraint
17747
17748 if Nkind (S) = N_Subtype_Indication then
17749 Find_Type (Subtype_Mark (S));
17750 Typ := Entity (Subtype_Mark (S));
17751
17752 if not
17753 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17754 then
17755 Error_Msg_N
17756 ("incorrect constraint for this kind of type", Constraint (S));
17757 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17758 end if;
17759
17760 -- Otherwise we have a subtype mark without a constraint
17761
17762 elsif Error_Posted (S) then
17763 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17764 return Any_Type;
17765
17766 else
17767 Find_Type (S);
17768 Typ := Entity (S);
17769 end if;
17770
17771 -- Check No_Wide_Characters restriction
17772
17773 Check_Wide_Character_Restriction (Typ, S);
17774
17775 return Typ;
17776 end Find_Type_Of_Subtype_Indic;
17777
17778 -------------------------------------
17779 -- Floating_Point_Type_Declaration --
17780 -------------------------------------
17781
17782 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17783 Digs : constant Node_Id := Digits_Expression (Def);
17784 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17785 Digs_Val : Uint;
17786 Base_Typ : Entity_Id;
17787 Implicit_Base : Entity_Id;
17788
17789 function Can_Derive_From (E : Entity_Id) return Boolean;
17790 -- Find if given digits value, and possibly a specified range, allows
17791 -- derivation from specified type
17792
17793 procedure Convert_Bound (B : Node_Id);
17794 -- If specified, the bounds must be static but may be of different
17795 -- types. They must be converted into machine numbers of the base type,
17796 -- in accordance with RM 4.9(38).
17797
17798 function Find_Base_Type return Entity_Id;
17799 -- Find a predefined base type that Def can derive from, or generate
17800 -- an error and substitute Long_Long_Float if none exists.
17801
17802 ---------------------
17803 -- Can_Derive_From --
17804 ---------------------
17805
17806 function Can_Derive_From (E : Entity_Id) return Boolean is
17807 Spec : constant Entity_Id := Real_Range_Specification (Def);
17808
17809 begin
17810 -- Check specified "digits" constraint
17811
17812 if Digs_Val > Digits_Value (E) then
17813 return False;
17814 end if;
17815
17816 -- Check for matching range, if specified
17817
17818 if Present (Spec) then
17819 if Expr_Value_R (Type_Low_Bound (E)) >
17820 Expr_Value_R (Low_Bound (Spec))
17821 then
17822 return False;
17823 end if;
17824
17825 if Expr_Value_R (Type_High_Bound (E)) <
17826 Expr_Value_R (High_Bound (Spec))
17827 then
17828 return False;
17829 end if;
17830 end if;
17831
17832 return True;
17833 end Can_Derive_From;
17834
17835 -------------------
17836 -- Convert_Bound --
17837 --------------------
17838
17839 procedure Convert_Bound (B : Node_Id) is
17840 begin
17841 -- If the bound is not a literal it can only be static if it is
17842 -- a static constant, possibly of a specified type.
17843
17844 if Is_Entity_Name (B)
17845 and then Ekind (Entity (B)) = E_Constant
17846 then
17847 Rewrite (B, Constant_Value (Entity (B)));
17848 end if;
17849
17850 if Nkind (B) = N_Real_Literal then
17851 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17852 Set_Is_Machine_Number (B);
17853 Set_Etype (B, Base_Typ);
17854 end if;
17855 end Convert_Bound;
17856
17857 --------------------
17858 -- Find_Base_Type --
17859 --------------------
17860
17861 function Find_Base_Type return Entity_Id is
17862 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17863
17864 begin
17865 -- Iterate over the predefined types in order, returning the first
17866 -- one that Def can derive from.
17867
17868 while Present (Choice) loop
17869 if Can_Derive_From (Node (Choice)) then
17870 return Node (Choice);
17871 end if;
17872
17873 Next_Elmt (Choice);
17874 end loop;
17875
17876 -- If we can't derive from any existing type, use Long_Long_Float
17877 -- and give appropriate message explaining the problem.
17878
17879 if Digs_Val > Max_Digs_Val then
17880 -- It might be the case that there is a type with the requested
17881 -- range, just not the combination of digits and range.
17882
17883 Error_Msg_N
17884 ("no predefined type has requested range and precision",
17885 Real_Range_Specification (Def));
17886
17887 else
17888 Error_Msg_N
17889 ("range too large for any predefined type",
17890 Real_Range_Specification (Def));
17891 end if;
17892
17893 return Standard_Long_Long_Float;
17894 end Find_Base_Type;
17895
17896 -- Start of processing for Floating_Point_Type_Declaration
17897
17898 begin
17899 Check_Restriction (No_Floating_Point, Def);
17900
17901 -- Create an implicit base type
17902
17903 Implicit_Base :=
17904 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17905
17906 -- Analyze and verify digits value
17907
17908 Analyze_And_Resolve (Digs, Any_Integer);
17909 Check_Digits_Expression (Digs);
17910 Digs_Val := Expr_Value (Digs);
17911
17912 -- Process possible range spec and find correct type to derive from
17913
17914 Process_Real_Range_Specification (Def);
17915
17916 -- Check that requested number of digits is not too high.
17917
17918 if Digs_Val > Max_Digs_Val then
17919
17920 -- The check for Max_Base_Digits may be somewhat expensive, as it
17921 -- requires reading System, so only do it when necessary.
17922
17923 declare
17924 Max_Base_Digits : constant Uint :=
17925 Expr_Value
17926 (Expression
17927 (Parent (RTE (RE_Max_Base_Digits))));
17928
17929 begin
17930 if Digs_Val > Max_Base_Digits then
17931 Error_Msg_Uint_1 := Max_Base_Digits;
17932 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17933
17934 elsif No (Real_Range_Specification (Def)) then
17935 Error_Msg_Uint_1 := Max_Digs_Val;
17936 Error_Msg_N ("types with more than ^ digits need range spec "
17937 & "(RM 3.5.7(6))", Digs);
17938 end if;
17939 end;
17940 end if;
17941
17942 -- Find a suitable type to derive from or complain and use a substitute
17943
17944 Base_Typ := Find_Base_Type;
17945
17946 -- If there are bounds given in the declaration use them as the bounds
17947 -- of the type, otherwise use the bounds of the predefined base type
17948 -- that was chosen based on the Digits value.
17949
17950 if Present (Real_Range_Specification (Def)) then
17951 Set_Scalar_Range (T, Real_Range_Specification (Def));
17952 Set_Is_Constrained (T);
17953
17954 Convert_Bound (Type_Low_Bound (T));
17955 Convert_Bound (Type_High_Bound (T));
17956
17957 else
17958 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17959 end if;
17960
17961 -- Complete definition of implicit base and declared first subtype. The
17962 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17963 -- are not clobbered when the floating point type acts as a full view of
17964 -- a private type.
17965
17966 Set_Etype (Implicit_Base, Base_Typ);
17967 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17968 Set_Size_Info (Implicit_Base, Base_Typ);
17969 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17970 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17971 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17972 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17973
17974 Set_Ekind (T, E_Floating_Point_Subtype);
17975 Set_Etype (T, Implicit_Base);
17976 Set_Size_Info (T, Implicit_Base);
17977 Set_RM_Size (T, RM_Size (Implicit_Base));
17978 Inherit_Rep_Item_Chain (T, Implicit_Base);
17979 Set_Digits_Value (T, Digs_Val);
17980 end Floating_Point_Type_Declaration;
17981
17982 ----------------------------
17983 -- Get_Discriminant_Value --
17984 ----------------------------
17985
17986 -- This is the situation:
17987
17988 -- There is a non-derived type
17989
17990 -- type T0 (Dx, Dy, Dz...)
17991
17992 -- There are zero or more levels of derivation, with each derivation
17993 -- either purely inheriting the discriminants, or defining its own.
17994
17995 -- type Ti is new Ti-1
17996 -- or
17997 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17998 -- or
17999 -- subtype Ti is ...
18000
18001 -- The subtype issue is avoided by the use of Original_Record_Component,
18002 -- and the fact that derived subtypes also derive the constraints.
18003
18004 -- This chain leads back from
18005
18006 -- Typ_For_Constraint
18007
18008 -- Typ_For_Constraint has discriminants, and the value for each
18009 -- discriminant is given by its corresponding Elmt of Constraints.
18010
18011 -- Discriminant is some discriminant in this hierarchy
18012
18013 -- We need to return its value
18014
18015 -- We do this by recursively searching each level, and looking for
18016 -- Discriminant. Once we get to the bottom, we start backing up
18017 -- returning the value for it which may in turn be a discriminant
18018 -- further up, so on the backup we continue the substitution.
18019
18020 function Get_Discriminant_Value
18021 (Discriminant : Entity_Id;
18022 Typ_For_Constraint : Entity_Id;
18023 Constraint : Elist_Id) return Node_Id
18024 is
18025 function Root_Corresponding_Discriminant
18026 (Discr : Entity_Id) return Entity_Id;
18027 -- Given a discriminant, traverse the chain of inherited discriminants
18028 -- and return the topmost discriminant.
18029
18030 function Search_Derivation_Levels
18031 (Ti : Entity_Id;
18032 Discrim_Values : Elist_Id;
18033 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18034 -- This is the routine that performs the recursive search of levels
18035 -- as described above.
18036
18037 -------------------------------------
18038 -- Root_Corresponding_Discriminant --
18039 -------------------------------------
18040
18041 function Root_Corresponding_Discriminant
18042 (Discr : Entity_Id) return Entity_Id
18043 is
18044 D : Entity_Id;
18045
18046 begin
18047 D := Discr;
18048 while Present (Corresponding_Discriminant (D)) loop
18049 D := Corresponding_Discriminant (D);
18050 end loop;
18051
18052 return D;
18053 end Root_Corresponding_Discriminant;
18054
18055 ------------------------------
18056 -- Search_Derivation_Levels --
18057 ------------------------------
18058
18059 function Search_Derivation_Levels
18060 (Ti : Entity_Id;
18061 Discrim_Values : Elist_Id;
18062 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18063 is
18064 Assoc : Elmt_Id;
18065 Disc : Entity_Id;
18066 Result : Node_Or_Entity_Id;
18067 Result_Entity : Node_Id;
18068
18069 begin
18070 -- If inappropriate type, return Error, this happens only in
18071 -- cascaded error situations, and we want to avoid a blow up.
18072
18073 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18074 return Error;
18075 end if;
18076
18077 -- Look deeper if possible. Use Stored_Constraints only for
18078 -- untagged types. For tagged types use the given constraint.
18079 -- This asymmetry needs explanation???
18080
18081 if not Stored_Discrim_Values
18082 and then Present (Stored_Constraint (Ti))
18083 and then not Is_Tagged_Type (Ti)
18084 then
18085 Result :=
18086 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18087
18088 else
18089 declare
18090 Td : Entity_Id := Etype (Ti);
18091
18092 begin
18093 -- If the parent type is private, the full view may include
18094 -- renamed discriminants, and it is those stored values that
18095 -- may be needed (the partial view never has more information
18096 -- than the full view).
18097
18098 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18099 Td := Full_View (Td);
18100 end if;
18101
18102 if Td = Ti then
18103 Result := Discriminant;
18104
18105 else
18106 if Present (Stored_Constraint (Ti)) then
18107 Result :=
18108 Search_Derivation_Levels
18109 (Td, Stored_Constraint (Ti), True);
18110 else
18111 Result :=
18112 Search_Derivation_Levels
18113 (Td, Discrim_Values, Stored_Discrim_Values);
18114 end if;
18115 end if;
18116 end;
18117 end if;
18118
18119 -- Extra underlying places to search, if not found above. For
18120 -- concurrent types, the relevant discriminant appears in the
18121 -- corresponding record. For a type derived from a private type
18122 -- without discriminant, the full view inherits the discriminants
18123 -- of the full view of the parent.
18124
18125 if Result = Discriminant then
18126 if Is_Concurrent_Type (Ti)
18127 and then Present (Corresponding_Record_Type (Ti))
18128 then
18129 Result :=
18130 Search_Derivation_Levels (
18131 Corresponding_Record_Type (Ti),
18132 Discrim_Values,
18133 Stored_Discrim_Values);
18134
18135 elsif Is_Private_Type (Ti)
18136 and then not Has_Discriminants (Ti)
18137 and then Present (Full_View (Ti))
18138 and then Etype (Full_View (Ti)) /= Ti
18139 then
18140 Result :=
18141 Search_Derivation_Levels (
18142 Full_View (Ti),
18143 Discrim_Values,
18144 Stored_Discrim_Values);
18145 end if;
18146 end if;
18147
18148 -- If Result is not a (reference to a) discriminant, return it,
18149 -- otherwise set Result_Entity to the discriminant.
18150
18151 if Nkind (Result) = N_Defining_Identifier then
18152 pragma Assert (Result = Discriminant);
18153 Result_Entity := Result;
18154
18155 else
18156 if not Denotes_Discriminant (Result) then
18157 return Result;
18158 end if;
18159
18160 Result_Entity := Entity (Result);
18161 end if;
18162
18163 -- See if this level of derivation actually has discriminants because
18164 -- tagged derivations can add them, hence the lower levels need not
18165 -- have any.
18166
18167 if not Has_Discriminants (Ti) then
18168 return Result;
18169 end if;
18170
18171 -- Scan Ti's discriminants for Result_Entity, and return its
18172 -- corresponding value, if any.
18173
18174 Result_Entity := Original_Record_Component (Result_Entity);
18175
18176 Assoc := First_Elmt (Discrim_Values);
18177
18178 if Stored_Discrim_Values then
18179 Disc := First_Stored_Discriminant (Ti);
18180 else
18181 Disc := First_Discriminant (Ti);
18182 end if;
18183
18184 while Present (Disc) loop
18185
18186 -- If no further associations return the discriminant, value will
18187 -- be found on the second pass.
18188
18189 if No (Assoc) then
18190 return Result;
18191 end if;
18192
18193 if Original_Record_Component (Disc) = Result_Entity then
18194 return Node (Assoc);
18195 end if;
18196
18197 Next_Elmt (Assoc);
18198
18199 if Stored_Discrim_Values then
18200 Next_Stored_Discriminant (Disc);
18201 else
18202 Next_Discriminant (Disc);
18203 end if;
18204 end loop;
18205
18206 -- Could not find it
18207
18208 return Result;
18209 end Search_Derivation_Levels;
18210
18211 -- Local Variables
18212
18213 Result : Node_Or_Entity_Id;
18214
18215 -- Start of processing for Get_Discriminant_Value
18216
18217 begin
18218 -- ??? This routine is a gigantic mess and will be deleted. For the
18219 -- time being just test for the trivial case before calling recurse.
18220
18221 -- We are now celebrating the 20th anniversary of this comment!
18222
18223 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18224 declare
18225 D : Entity_Id;
18226 E : Elmt_Id;
18227
18228 begin
18229 D := First_Discriminant (Typ_For_Constraint);
18230 E := First_Elmt (Constraint);
18231 while Present (D) loop
18232 if Chars (D) = Chars (Discriminant) then
18233 return Node (E);
18234 end if;
18235
18236 Next_Discriminant (D);
18237 Next_Elmt (E);
18238 end loop;
18239 end;
18240 end if;
18241
18242 Result := Search_Derivation_Levels
18243 (Typ_For_Constraint, Constraint, False);
18244
18245 -- ??? hack to disappear when this routine is gone
18246
18247 if Nkind (Result) = N_Defining_Identifier then
18248 declare
18249 D : Entity_Id;
18250 E : Elmt_Id;
18251
18252 begin
18253 D := First_Discriminant (Typ_For_Constraint);
18254 E := First_Elmt (Constraint);
18255 while Present (D) loop
18256 if Root_Corresponding_Discriminant (D) = Discriminant then
18257 return Node (E);
18258 end if;
18259
18260 Next_Discriminant (D);
18261 Next_Elmt (E);
18262 end loop;
18263 end;
18264 end if;
18265
18266 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18267 return Result;
18268 end Get_Discriminant_Value;
18269
18270 --------------------------
18271 -- Has_Range_Constraint --
18272 --------------------------
18273
18274 function Has_Range_Constraint (N : Node_Id) return Boolean is
18275 C : constant Node_Id := Constraint (N);
18276
18277 begin
18278 if Nkind (C) = N_Range_Constraint then
18279 return True;
18280
18281 elsif Nkind (C) = N_Digits_Constraint then
18282 return
18283 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18284 or else Present (Range_Constraint (C));
18285
18286 elsif Nkind (C) = N_Delta_Constraint then
18287 return Present (Range_Constraint (C));
18288
18289 else
18290 return False;
18291 end if;
18292 end Has_Range_Constraint;
18293
18294 ------------------------
18295 -- Inherit_Components --
18296 ------------------------
18297
18298 function Inherit_Components
18299 (N : Node_Id;
18300 Parent_Base : Entity_Id;
18301 Derived_Base : Entity_Id;
18302 Is_Tagged : Boolean;
18303 Inherit_Discr : Boolean;
18304 Discs : Elist_Id) return Elist_Id
18305 is
18306 Assoc_List : constant Elist_Id := New_Elmt_List;
18307
18308 procedure Inherit_Component
18309 (Old_C : Entity_Id;
18310 Plain_Discrim : Boolean := False;
18311 Stored_Discrim : Boolean := False);
18312 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18313 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18314 -- True, Old_C is a stored discriminant. If they are both false then
18315 -- Old_C is a regular component.
18316
18317 -----------------------
18318 -- Inherit_Component --
18319 -----------------------
18320
18321 procedure Inherit_Component
18322 (Old_C : Entity_Id;
18323 Plain_Discrim : Boolean := False;
18324 Stored_Discrim : Boolean := False)
18325 is
18326 procedure Set_Anonymous_Type (Id : Entity_Id);
18327 -- Id denotes the entity of an access discriminant or anonymous
18328 -- access component. Set the type of Id to either the same type of
18329 -- Old_C or create a new one depending on whether the parent and
18330 -- the child types are in the same scope.
18331
18332 ------------------------
18333 -- Set_Anonymous_Type --
18334 ------------------------
18335
18336 procedure Set_Anonymous_Type (Id : Entity_Id) is
18337 Old_Typ : constant Entity_Id := Etype (Old_C);
18338
18339 begin
18340 if Scope (Parent_Base) = Scope (Derived_Base) then
18341 Set_Etype (Id, Old_Typ);
18342
18343 -- The parent and the derived type are in two different scopes.
18344 -- Reuse the type of the original discriminant / component by
18345 -- copying it in order to preserve all attributes.
18346
18347 else
18348 declare
18349 Typ : constant Entity_Id := New_Copy (Old_Typ);
18350
18351 begin
18352 Set_Etype (Id, Typ);
18353
18354 -- Since we do not generate component declarations for
18355 -- inherited components, associate the itype with the
18356 -- derived type.
18357
18358 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18359 Set_Scope (Typ, Derived_Base);
18360 end;
18361 end if;
18362 end Set_Anonymous_Type;
18363
18364 -- Local variables and constants
18365
18366 New_C : constant Entity_Id := New_Copy (Old_C);
18367
18368 Corr_Discrim : Entity_Id;
18369 Discrim : Entity_Id;
18370
18371 -- Start of processing for Inherit_Component
18372
18373 begin
18374 pragma Assert (not Is_Tagged or not Stored_Discrim);
18375
18376 Set_Parent (New_C, Parent (Old_C));
18377
18378 -- Regular discriminants and components must be inserted in the scope
18379 -- of the Derived_Base. Do it here.
18380
18381 if not Stored_Discrim then
18382 Enter_Name (New_C);
18383 end if;
18384
18385 -- For tagged types the Original_Record_Component must point to
18386 -- whatever this field was pointing to in the parent type. This has
18387 -- already been achieved by the call to New_Copy above.
18388
18389 if not Is_Tagged then
18390 Set_Original_Record_Component (New_C, New_C);
18391 Set_Corresponding_Record_Component (New_C, Old_C);
18392 end if;
18393
18394 -- Set the proper type of an access discriminant
18395
18396 if Ekind (New_C) = E_Discriminant
18397 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18398 then
18399 Set_Anonymous_Type (New_C);
18400 end if;
18401
18402 -- If we have inherited a component then see if its Etype contains
18403 -- references to Parent_Base discriminants. In this case, replace
18404 -- these references with the constraints given in Discs. We do not
18405 -- do this for the partial view of private types because this is
18406 -- not needed (only the components of the full view will be used
18407 -- for code generation) and cause problem. We also avoid this
18408 -- transformation in some error situations.
18409
18410 if Ekind (New_C) = E_Component then
18411
18412 -- Set the proper type of an anonymous access component
18413
18414 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18415 Set_Anonymous_Type (New_C);
18416
18417 elsif (Is_Private_Type (Derived_Base)
18418 and then not Is_Generic_Type (Derived_Base))
18419 or else (Is_Empty_Elmt_List (Discs)
18420 and then not Expander_Active)
18421 then
18422 Set_Etype (New_C, Etype (Old_C));
18423
18424 else
18425 -- The current component introduces a circularity of the
18426 -- following kind:
18427
18428 -- limited with Pack_2;
18429 -- package Pack_1 is
18430 -- type T_1 is tagged record
18431 -- Comp : access Pack_2.T_2;
18432 -- ...
18433 -- end record;
18434 -- end Pack_1;
18435
18436 -- with Pack_1;
18437 -- package Pack_2 is
18438 -- type T_2 is new Pack_1.T_1 with ...;
18439 -- end Pack_2;
18440
18441 Set_Etype
18442 (New_C,
18443 Constrain_Component_Type
18444 (Old_C, Derived_Base, N, Parent_Base, Discs));
18445 end if;
18446 end if;
18447
18448 -- In derived tagged types it is illegal to reference a non
18449 -- discriminant component in the parent type. To catch this, mark
18450 -- these components with an Ekind of E_Void. This will be reset in
18451 -- Record_Type_Definition after processing the record extension of
18452 -- the derived type.
18453
18454 -- If the declaration is a private extension, there is no further
18455 -- record extension to process, and the components retain their
18456 -- current kind, because they are visible at this point.
18457
18458 if Is_Tagged and then Ekind (New_C) = E_Component
18459 and then Nkind (N) /= N_Private_Extension_Declaration
18460 then
18461 Set_Ekind (New_C, E_Void);
18462 end if;
18463
18464 if Plain_Discrim then
18465 Set_Corresponding_Discriminant (New_C, Old_C);
18466 Build_Discriminal (New_C);
18467
18468 -- If we are explicitly inheriting a stored discriminant it will be
18469 -- completely hidden.
18470
18471 elsif Stored_Discrim then
18472 Set_Corresponding_Discriminant (New_C, Empty);
18473 Set_Discriminal (New_C, Empty);
18474 Set_Is_Completely_Hidden (New_C);
18475
18476 -- Set the Original_Record_Component of each discriminant in the
18477 -- derived base to point to the corresponding stored that we just
18478 -- created.
18479
18480 Discrim := First_Discriminant (Derived_Base);
18481 while Present (Discrim) loop
18482 Corr_Discrim := Corresponding_Discriminant (Discrim);
18483
18484 -- Corr_Discrim could be missing in an error situation
18485
18486 if Present (Corr_Discrim)
18487 and then Original_Record_Component (Corr_Discrim) = Old_C
18488 then
18489 Set_Original_Record_Component (Discrim, New_C);
18490 Set_Corresponding_Record_Component (Discrim, Empty);
18491 end if;
18492
18493 Next_Discriminant (Discrim);
18494 end loop;
18495
18496 Append_Entity (New_C, Derived_Base);
18497 end if;
18498
18499 if not Is_Tagged then
18500 Append_Elmt (Old_C, Assoc_List);
18501 Append_Elmt (New_C, Assoc_List);
18502 end if;
18503 end Inherit_Component;
18504
18505 -- Variables local to Inherit_Component
18506
18507 Loc : constant Source_Ptr := Sloc (N);
18508
18509 Parent_Discrim : Entity_Id;
18510 Stored_Discrim : Entity_Id;
18511 D : Entity_Id;
18512 Component : Entity_Id;
18513
18514 -- Start of processing for Inherit_Components
18515
18516 begin
18517 if not Is_Tagged then
18518 Append_Elmt (Parent_Base, Assoc_List);
18519 Append_Elmt (Derived_Base, Assoc_List);
18520 end if;
18521
18522 -- Inherit parent discriminants if needed
18523
18524 if Inherit_Discr then
18525 Parent_Discrim := First_Discriminant (Parent_Base);
18526 while Present (Parent_Discrim) loop
18527 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18528 Next_Discriminant (Parent_Discrim);
18529 end loop;
18530 end if;
18531
18532 -- Create explicit stored discrims for untagged types when necessary
18533
18534 if not Has_Unknown_Discriminants (Derived_Base)
18535 and then Has_Discriminants (Parent_Base)
18536 and then not Is_Tagged
18537 and then
18538 (not Inherit_Discr
18539 or else First_Discriminant (Parent_Base) /=
18540 First_Stored_Discriminant (Parent_Base))
18541 then
18542 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18543 while Present (Stored_Discrim) loop
18544 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18545 Next_Stored_Discriminant (Stored_Discrim);
18546 end loop;
18547 end if;
18548
18549 -- See if we can apply the second transformation for derived types, as
18550 -- explained in point 6. in the comments above Build_Derived_Record_Type
18551 -- This is achieved by appending Derived_Base discriminants into Discs,
18552 -- which has the side effect of returning a non empty Discs list to the
18553 -- caller of Inherit_Components, which is what we want. This must be
18554 -- done for private derived types if there are explicit stored
18555 -- discriminants, to ensure that we can retrieve the values of the
18556 -- constraints provided in the ancestors.
18557
18558 if Inherit_Discr
18559 and then Is_Empty_Elmt_List (Discs)
18560 and then Present (First_Discriminant (Derived_Base))
18561 and then
18562 (not Is_Private_Type (Derived_Base)
18563 or else Is_Completely_Hidden
18564 (First_Stored_Discriminant (Derived_Base))
18565 or else Is_Generic_Type (Derived_Base))
18566 then
18567 D := First_Discriminant (Derived_Base);
18568 while Present (D) loop
18569 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18570 Next_Discriminant (D);
18571 end loop;
18572 end if;
18573
18574 -- Finally, inherit non-discriminant components unless they are not
18575 -- visible because defined or inherited from the full view of the
18576 -- parent. Don't inherit the _parent field of the parent type.
18577
18578 Component := First_Entity (Parent_Base);
18579 while Present (Component) loop
18580
18581 -- Ada 2005 (AI-251): Do not inherit components associated with
18582 -- secondary tags of the parent.
18583
18584 if Ekind (Component) = E_Component
18585 and then Present (Related_Type (Component))
18586 then
18587 null;
18588
18589 elsif Ekind (Component) /= E_Component
18590 or else Chars (Component) = Name_uParent
18591 then
18592 null;
18593
18594 -- If the derived type is within the parent type's declarative
18595 -- region, then the components can still be inherited even though
18596 -- they aren't visible at this point. This can occur for cases
18597 -- such as within public child units where the components must
18598 -- become visible upon entering the child unit's private part.
18599
18600 elsif not Is_Visible_Component (Component)
18601 and then not In_Open_Scopes (Scope (Parent_Base))
18602 then
18603 null;
18604
18605 elsif Ekind_In (Derived_Base, E_Private_Type,
18606 E_Limited_Private_Type)
18607 then
18608 null;
18609
18610 else
18611 Inherit_Component (Component);
18612 end if;
18613
18614 Next_Entity (Component);
18615 end loop;
18616
18617 -- For tagged derived types, inherited discriminants cannot be used in
18618 -- component declarations of the record extension part. To achieve this
18619 -- we mark the inherited discriminants as not visible.
18620
18621 if Is_Tagged and then Inherit_Discr then
18622 D := First_Discriminant (Derived_Base);
18623 while Present (D) loop
18624 Set_Is_Immediately_Visible (D, False);
18625 Next_Discriminant (D);
18626 end loop;
18627 end if;
18628
18629 return Assoc_List;
18630 end Inherit_Components;
18631
18632 ----------------------
18633 -- Is_EVF_Procedure --
18634 ----------------------
18635
18636 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18637 Formal : Entity_Id;
18638
18639 begin
18640 -- Examine the formals of an Extensions_Visible False procedure looking
18641 -- for a controlling OUT parameter.
18642
18643 if Ekind (Subp) = E_Procedure
18644 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18645 then
18646 Formal := First_Formal (Subp);
18647 while Present (Formal) loop
18648 if Ekind (Formal) = E_Out_Parameter
18649 and then Is_Controlling_Formal (Formal)
18650 then
18651 return True;
18652 end if;
18653
18654 Next_Formal (Formal);
18655 end loop;
18656 end if;
18657
18658 return False;
18659 end Is_EVF_Procedure;
18660
18661 -----------------------
18662 -- Is_Null_Extension --
18663 -----------------------
18664
18665 function Is_Null_Extension (T : Entity_Id) return Boolean is
18666 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18667 Comp_List : Node_Id;
18668 Comp : Node_Id;
18669
18670 begin
18671 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18672 or else not Is_Tagged_Type (T)
18673 or else Nkind (Type_Definition (Type_Decl)) /=
18674 N_Derived_Type_Definition
18675 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18676 then
18677 return False;
18678 end if;
18679
18680 Comp_List :=
18681 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18682
18683 if Present (Discriminant_Specifications (Type_Decl)) then
18684 return False;
18685
18686 elsif Present (Comp_List)
18687 and then Is_Non_Empty_List (Component_Items (Comp_List))
18688 then
18689 Comp := First (Component_Items (Comp_List));
18690
18691 -- Only user-defined components are relevant. The component list
18692 -- may also contain a parent component and internal components
18693 -- corresponding to secondary tags, but these do not determine
18694 -- whether this is a null extension.
18695
18696 while Present (Comp) loop
18697 if Comes_From_Source (Comp) then
18698 return False;
18699 end if;
18700
18701 Next (Comp);
18702 end loop;
18703
18704 return True;
18705
18706 else
18707 return True;
18708 end if;
18709 end Is_Null_Extension;
18710
18711 ------------------------------
18712 -- Is_Valid_Constraint_Kind --
18713 ------------------------------
18714
18715 function Is_Valid_Constraint_Kind
18716 (T_Kind : Type_Kind;
18717 Constraint_Kind : Node_Kind) return Boolean
18718 is
18719 begin
18720 case T_Kind is
18721 when Enumeration_Kind
18722 | Integer_Kind
18723 =>
18724 return Constraint_Kind = N_Range_Constraint;
18725
18726 when Decimal_Fixed_Point_Kind =>
18727 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18728 N_Range_Constraint);
18729
18730 when Ordinary_Fixed_Point_Kind =>
18731 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18732 N_Range_Constraint);
18733
18734 when Float_Kind =>
18735 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18736 N_Range_Constraint);
18737
18738 when Access_Kind
18739 | Array_Kind
18740 | Class_Wide_Kind
18741 | Concurrent_Kind
18742 | Private_Kind
18743 | E_Incomplete_Type
18744 | E_Record_Subtype
18745 | E_Record_Type
18746 =>
18747 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18748
18749 when others =>
18750 return True; -- Error will be detected later
18751 end case;
18752 end Is_Valid_Constraint_Kind;
18753
18754 --------------------------
18755 -- Is_Visible_Component --
18756 --------------------------
18757
18758 function Is_Visible_Component
18759 (C : Entity_Id;
18760 N : Node_Id := Empty) return Boolean
18761 is
18762 Original_Comp : Entity_Id := Empty;
18763 Original_Type : Entity_Id;
18764 Type_Scope : Entity_Id;
18765
18766 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18767 -- Check whether parent type of inherited component is declared locally,
18768 -- possibly within a nested package or instance. The current scope is
18769 -- the derived record itself.
18770
18771 -------------------
18772 -- Is_Local_Type --
18773 -------------------
18774
18775 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18776 Scop : Entity_Id;
18777
18778 begin
18779 Scop := Scope (Typ);
18780 while Present (Scop)
18781 and then Scop /= Standard_Standard
18782 loop
18783 if Scop = Scope (Current_Scope) then
18784 return True;
18785 end if;
18786
18787 Scop := Scope (Scop);
18788 end loop;
18789
18790 return False;
18791 end Is_Local_Type;
18792
18793 -- Start of processing for Is_Visible_Component
18794
18795 begin
18796 if Ekind_In (C, E_Component, E_Discriminant) then
18797 Original_Comp := Original_Record_Component (C);
18798 end if;
18799
18800 if No (Original_Comp) then
18801
18802 -- Premature usage, or previous error
18803
18804 return False;
18805
18806 else
18807 Original_Type := Scope (Original_Comp);
18808 Type_Scope := Scope (Base_Type (Scope (C)));
18809 end if;
18810
18811 -- This test only concerns tagged types
18812
18813 if not Is_Tagged_Type (Original_Type) then
18814
18815 -- Check if this is a renamed discriminant (hidden either by the
18816 -- derived type or by some ancestor), unless we are analyzing code
18817 -- generated by the expander since it may reference such components
18818 -- (for example see the expansion of Deep_Adjust).
18819
18820 if Ekind (C) = E_Discriminant and then Present (N) then
18821 return
18822 not Comes_From_Source (N)
18823 or else not Is_Completely_Hidden (C);
18824 else
18825 return True;
18826 end if;
18827
18828 -- If it is _Parent or _Tag, there is no visibility issue
18829
18830 elsif not Comes_From_Source (Original_Comp) then
18831 return True;
18832
18833 -- Discriminants are visible unless the (private) type has unknown
18834 -- discriminants. If the discriminant reference is inserted for a
18835 -- discriminant check on a full view it is also visible.
18836
18837 elsif Ekind (Original_Comp) = E_Discriminant
18838 and then
18839 (not Has_Unknown_Discriminants (Original_Type)
18840 or else (Present (N)
18841 and then Nkind (N) = N_Selected_Component
18842 and then Nkind (Prefix (N)) = N_Type_Conversion
18843 and then not Comes_From_Source (Prefix (N))))
18844 then
18845 return True;
18846
18847 -- If the component has been declared in an ancestor which is currently
18848 -- a private type, then it is not visible. The same applies if the
18849 -- component's containing type is not in an open scope and the original
18850 -- component's enclosing type is a visible full view of a private type
18851 -- (which can occur in cases where an attempt is being made to reference
18852 -- a component in a sibling package that is inherited from a visible
18853 -- component of a type in an ancestor package; the component in the
18854 -- sibling package should not be visible even though the component it
18855 -- inherited from is visible). This does not apply however in the case
18856 -- where the scope of the type is a private child unit, or when the
18857 -- parent comes from a local package in which the ancestor is currently
18858 -- visible. The latter suppression of visibility is needed for cases
18859 -- that are tested in B730006.
18860
18861 elsif Is_Private_Type (Original_Type)
18862 or else
18863 (not Is_Private_Descendant (Type_Scope)
18864 and then not In_Open_Scopes (Type_Scope)
18865 and then Has_Private_Declaration (Original_Type))
18866 then
18867 -- If the type derives from an entity in a formal package, there
18868 -- are no additional visible components.
18869
18870 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18871 N_Formal_Package_Declaration
18872 then
18873 return False;
18874
18875 -- if we are not in the private part of the current package, there
18876 -- are no additional visible components.
18877
18878 elsif Ekind (Scope (Current_Scope)) = E_Package
18879 and then not In_Private_Part (Scope (Current_Scope))
18880 then
18881 return False;
18882 else
18883 return
18884 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18885 and then In_Open_Scopes (Scope (Original_Type))
18886 and then Is_Local_Type (Type_Scope);
18887 end if;
18888
18889 -- There is another weird way in which a component may be invisible when
18890 -- the private and the full view are not derived from the same ancestor.
18891 -- Here is an example :
18892
18893 -- type A1 is tagged record F1 : integer; end record;
18894 -- type A2 is new A1 with record F2 : integer; end record;
18895 -- type T is new A1 with private;
18896 -- private
18897 -- type T is new A2 with null record;
18898
18899 -- In this case, the full view of T inherits F1 and F2 but the private
18900 -- view inherits only F1
18901
18902 else
18903 declare
18904 Ancestor : Entity_Id := Scope (C);
18905
18906 begin
18907 loop
18908 if Ancestor = Original_Type then
18909 return True;
18910
18911 -- The ancestor may have a partial view of the original type,
18912 -- but if the full view is in scope, as in a child body, the
18913 -- component is visible.
18914
18915 elsif In_Private_Part (Scope (Original_Type))
18916 and then Full_View (Ancestor) = Original_Type
18917 then
18918 return True;
18919
18920 elsif Ancestor = Etype (Ancestor) then
18921
18922 -- No further ancestors to examine
18923
18924 return False;
18925 end if;
18926
18927 Ancestor := Etype (Ancestor);
18928 end loop;
18929 end;
18930 end if;
18931 end Is_Visible_Component;
18932
18933 --------------------------
18934 -- Make_Class_Wide_Type --
18935 --------------------------
18936
18937 procedure Make_Class_Wide_Type (T : Entity_Id) is
18938 CW_Type : Entity_Id;
18939 CW_Name : Name_Id;
18940 Next_E : Entity_Id;
18941 Prev_E : Entity_Id;
18942
18943 begin
18944 if Present (Class_Wide_Type (T)) then
18945
18946 -- The class-wide type is a partially decorated entity created for a
18947 -- unanalyzed tagged type referenced through a limited with clause.
18948 -- When the tagged type is analyzed, its class-wide type needs to be
18949 -- redecorated. Note that we reuse the entity created by Decorate_
18950 -- Tagged_Type in order to preserve all links.
18951
18952 if Materialize_Entity (Class_Wide_Type (T)) then
18953 CW_Type := Class_Wide_Type (T);
18954 Set_Materialize_Entity (CW_Type, False);
18955
18956 -- The class wide type can have been defined by the partial view, in
18957 -- which case everything is already done.
18958
18959 else
18960 return;
18961 end if;
18962
18963 -- Default case, we need to create a new class-wide type
18964
18965 else
18966 CW_Type :=
18967 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18968 end if;
18969
18970 -- Inherit root type characteristics
18971
18972 CW_Name := Chars (CW_Type);
18973 Next_E := Next_Entity (CW_Type);
18974 Prev_E := Prev_Entity (CW_Type);
18975 Copy_Node (T, CW_Type);
18976 Set_Comes_From_Source (CW_Type, False);
18977 Set_Chars (CW_Type, CW_Name);
18978 Set_Parent (CW_Type, Parent (T));
18979 Set_Prev_Entity (CW_Type, Prev_E);
18980 Set_Next_Entity (CW_Type, Next_E);
18981
18982 -- Ensure we have a new freeze node for the class-wide type. The partial
18983 -- view may have freeze action of its own, requiring a proper freeze
18984 -- node, and the same freeze node cannot be shared between the two
18985 -- types.
18986
18987 Set_Has_Delayed_Freeze (CW_Type);
18988 Set_Freeze_Node (CW_Type, Empty);
18989
18990 -- Customize the class-wide type: It has no prim. op., it cannot be
18991 -- abstract, its Etype points back to the specific root type, and it
18992 -- cannot have any invariants.
18993
18994 Set_Ekind (CW_Type, E_Class_Wide_Type);
18995 Set_Is_Tagged_Type (CW_Type, True);
18996 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18997 Set_Is_Abstract_Type (CW_Type, False);
18998 Set_Is_Constrained (CW_Type, False);
18999 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19000 Set_Default_SSO (CW_Type);
19001 Set_Has_Inheritable_Invariants (CW_Type, False);
19002 Set_Has_Inherited_Invariants (CW_Type, False);
19003 Set_Has_Own_Invariants (CW_Type, False);
19004
19005 if Ekind (T) = E_Class_Wide_Subtype then
19006 Set_Etype (CW_Type, Etype (Base_Type (T)));
19007 else
19008 Set_Etype (CW_Type, T);
19009 end if;
19010
19011 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19012
19013 -- If this is the class_wide type of a constrained subtype, it does
19014 -- not have discriminants.
19015
19016 Set_Has_Discriminants (CW_Type,
19017 Has_Discriminants (T) and then not Is_Constrained (T));
19018
19019 Set_Has_Unknown_Discriminants (CW_Type, True);
19020 Set_Class_Wide_Type (T, CW_Type);
19021 Set_Equivalent_Type (CW_Type, Empty);
19022
19023 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19024
19025 Set_Class_Wide_Type (CW_Type, CW_Type);
19026 end Make_Class_Wide_Type;
19027
19028 ----------------
19029 -- Make_Index --
19030 ----------------
19031
19032 procedure Make_Index
19033 (N : Node_Id;
19034 Related_Nod : Node_Id;
19035 Related_Id : Entity_Id := Empty;
19036 Suffix_Index : Nat := 1)
19037 is
19038 R : Node_Id;
19039 T : Entity_Id;
19040 Def_Id : Entity_Id := Empty;
19041 Found : Boolean := False;
19042
19043 begin
19044 -- For a discrete range used in a constrained array definition and
19045 -- defined by a range, an implicit conversion to the predefined type
19046 -- INTEGER is assumed if each bound is either a numeric literal, a named
19047 -- number, or an attribute, and the type of both bounds (prior to the
19048 -- implicit conversion) is the type universal_integer. Otherwise, both
19049 -- bounds must be of the same discrete type, other than universal
19050 -- integer; this type must be determinable independently of the
19051 -- context, but using the fact that the type must be discrete and that
19052 -- both bounds must have the same type.
19053
19054 -- Character literals also have a universal type in the absence of
19055 -- of additional context, and are resolved to Standard_Character.
19056
19057 if Nkind (N) = N_Range then
19058
19059 -- The index is given by a range constraint. The bounds are known
19060 -- to be of a consistent type.
19061
19062 if not Is_Overloaded (N) then
19063 T := Etype (N);
19064
19065 -- For universal bounds, choose the specific predefined type
19066
19067 if T = Universal_Integer then
19068 T := Standard_Integer;
19069
19070 elsif T = Any_Character then
19071 Ambiguous_Character (Low_Bound (N));
19072
19073 T := Standard_Character;
19074 end if;
19075
19076 -- The node may be overloaded because some user-defined operators
19077 -- are available, but if a universal interpretation exists it is
19078 -- also the selected one.
19079
19080 elsif Universal_Interpretation (N) = Universal_Integer then
19081 T := Standard_Integer;
19082
19083 else
19084 T := Any_Type;
19085
19086 declare
19087 Ind : Interp_Index;
19088 It : Interp;
19089
19090 begin
19091 Get_First_Interp (N, Ind, It);
19092 while Present (It.Typ) loop
19093 if Is_Discrete_Type (It.Typ) then
19094
19095 if Found
19096 and then not Covers (It.Typ, T)
19097 and then not Covers (T, It.Typ)
19098 then
19099 Error_Msg_N ("ambiguous bounds in discrete range", N);
19100 exit;
19101 else
19102 T := It.Typ;
19103 Found := True;
19104 end if;
19105 end if;
19106
19107 Get_Next_Interp (Ind, It);
19108 end loop;
19109
19110 if T = Any_Type then
19111 Error_Msg_N ("discrete type required for range", N);
19112 Set_Etype (N, Any_Type);
19113 return;
19114
19115 elsif T = Universal_Integer then
19116 T := Standard_Integer;
19117 end if;
19118 end;
19119 end if;
19120
19121 if not Is_Discrete_Type (T) then
19122 Error_Msg_N ("discrete type required for range", N);
19123 Set_Etype (N, Any_Type);
19124 return;
19125 end if;
19126
19127 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19128 and then Attribute_Name (Low_Bound (N)) = Name_First
19129 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19130 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19131 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19132 then
19133 -- The type of the index will be the type of the prefix, as long
19134 -- as the upper bound is 'Last of the same type.
19135
19136 Def_Id := Entity (Prefix (Low_Bound (N)));
19137
19138 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19139 or else Attribute_Name (High_Bound (N)) /= Name_Last
19140 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19141 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19142 then
19143 Def_Id := Empty;
19144 end if;
19145 end if;
19146
19147 R := N;
19148 Process_Range_Expr_In_Decl (R, T);
19149
19150 elsif Nkind (N) = N_Subtype_Indication then
19151
19152 -- The index is given by a subtype with a range constraint
19153
19154 T := Base_Type (Entity (Subtype_Mark (N)));
19155
19156 if not Is_Discrete_Type (T) then
19157 Error_Msg_N ("discrete type required for range", N);
19158 Set_Etype (N, Any_Type);
19159 return;
19160 end if;
19161
19162 R := Range_Expression (Constraint (N));
19163
19164 Resolve (R, T);
19165 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19166
19167 elsif Nkind (N) = N_Attribute_Reference then
19168
19169 -- Catch beginner's error (use of attribute other than 'Range)
19170
19171 if Attribute_Name (N) /= Name_Range then
19172 Error_Msg_N ("expect attribute ''Range", N);
19173 Set_Etype (N, Any_Type);
19174 return;
19175 end if;
19176
19177 -- If the node denotes the range of a type mark, that is also the
19178 -- resulting type, and we do not need to create an Itype for it.
19179
19180 if Is_Entity_Name (Prefix (N))
19181 and then Comes_From_Source (N)
19182 and then Is_Type (Entity (Prefix (N)))
19183 and then Is_Discrete_Type (Entity (Prefix (N)))
19184 then
19185 Def_Id := Entity (Prefix (N));
19186 end if;
19187
19188 Analyze_And_Resolve (N);
19189 T := Etype (N);
19190 R := N;
19191
19192 -- If none of the above, must be a subtype. We convert this to a
19193 -- range attribute reference because in the case of declared first
19194 -- named subtypes, the types in the range reference can be different
19195 -- from the type of the entity. A range attribute normalizes the
19196 -- reference and obtains the correct types for the bounds.
19197
19198 -- This transformation is in the nature of an expansion, is only
19199 -- done if expansion is active. In particular, it is not done on
19200 -- formal generic types, because we need to retain the name of the
19201 -- original index for instantiation purposes.
19202
19203 else
19204 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19205 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19206 Set_Etype (N, Any_Integer);
19207 return;
19208
19209 else
19210 -- The type mark may be that of an incomplete type. It is only
19211 -- now that we can get the full view, previous analysis does
19212 -- not look specifically for a type mark.
19213
19214 Set_Entity (N, Get_Full_View (Entity (N)));
19215 Set_Etype (N, Entity (N));
19216 Def_Id := Entity (N);
19217
19218 if not Is_Discrete_Type (Def_Id) then
19219 Error_Msg_N ("discrete type required for index", N);
19220 Set_Etype (N, Any_Type);
19221 return;
19222 end if;
19223 end if;
19224
19225 if Expander_Active then
19226 Rewrite (N,
19227 Make_Attribute_Reference (Sloc (N),
19228 Attribute_Name => Name_Range,
19229 Prefix => Relocate_Node (N)));
19230
19231 -- The original was a subtype mark that does not freeze. This
19232 -- means that the rewritten version must not freeze either.
19233
19234 Set_Must_Not_Freeze (N);
19235 Set_Must_Not_Freeze (Prefix (N));
19236 Analyze_And_Resolve (N);
19237 T := Etype (N);
19238 R := N;
19239
19240 -- If expander is inactive, type is legal, nothing else to construct
19241
19242 else
19243 return;
19244 end if;
19245 end if;
19246
19247 if not Is_Discrete_Type (T) then
19248 Error_Msg_N ("discrete type required for range", N);
19249 Set_Etype (N, Any_Type);
19250 return;
19251
19252 elsif T = Any_Type then
19253 Set_Etype (N, Any_Type);
19254 return;
19255 end if;
19256
19257 -- We will now create the appropriate Itype to describe the range, but
19258 -- first a check. If we originally had a subtype, then we just label
19259 -- the range with this subtype. Not only is there no need to construct
19260 -- a new subtype, but it is wrong to do so for two reasons:
19261
19262 -- 1. A legality concern, if we have a subtype, it must not freeze,
19263 -- and the Itype would cause freezing incorrectly
19264
19265 -- 2. An efficiency concern, if we created an Itype, it would not be
19266 -- recognized as the same type for the purposes of eliminating
19267 -- checks in some circumstances.
19268
19269 -- We signal this case by setting the subtype entity in Def_Id
19270
19271 if No (Def_Id) then
19272 Def_Id :=
19273 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19274 Set_Etype (Def_Id, Base_Type (T));
19275
19276 if Is_Signed_Integer_Type (T) then
19277 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19278
19279 elsif Is_Modular_Integer_Type (T) then
19280 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19281
19282 else
19283 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19284 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19285 Set_First_Literal (Def_Id, First_Literal (T));
19286 end if;
19287
19288 Set_Size_Info (Def_Id, (T));
19289 Set_RM_Size (Def_Id, RM_Size (T));
19290 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19291
19292 Set_Scalar_Range (Def_Id, R);
19293 Conditional_Delay (Def_Id, T);
19294
19295 if Nkind (N) = N_Subtype_Indication then
19296 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19297 end if;
19298
19299 -- In the subtype indication case, if the immediate parent of the
19300 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19301 -- even if its bounds are static.
19302
19303 if Nkind (N) = N_Subtype_Indication
19304 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19305 then
19306 Set_Is_Non_Static_Subtype (Def_Id);
19307 end if;
19308 end if;
19309
19310 -- Final step is to label the index with this constructed type
19311
19312 Set_Etype (N, Def_Id);
19313 end Make_Index;
19314
19315 ------------------------------
19316 -- Modular_Type_Declaration --
19317 ------------------------------
19318
19319 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19320 Mod_Expr : constant Node_Id := Expression (Def);
19321 M_Val : Uint;
19322
19323 procedure Set_Modular_Size (Bits : Int);
19324 -- Sets RM_Size to Bits, and Esize to normal word size above this
19325
19326 ----------------------
19327 -- Set_Modular_Size --
19328 ----------------------
19329
19330 procedure Set_Modular_Size (Bits : Int) is
19331 begin
19332 Set_RM_Size (T, UI_From_Int (Bits));
19333
19334 if Bits <= 8 then
19335 Init_Esize (T, 8);
19336
19337 elsif Bits <= 16 then
19338 Init_Esize (T, 16);
19339
19340 elsif Bits <= 32 then
19341 Init_Esize (T, 32);
19342
19343 else
19344 Init_Esize (T, System_Max_Binary_Modulus_Power);
19345 end if;
19346
19347 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19348 Set_Is_Known_Valid (T);
19349 end if;
19350 end Set_Modular_Size;
19351
19352 -- Start of processing for Modular_Type_Declaration
19353
19354 begin
19355 -- If the mod expression is (exactly) 2 * literal, where literal is
19356 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19357
19358 if Warn_On_Suspicious_Modulus_Value
19359 and then Nkind (Mod_Expr) = N_Op_Multiply
19360 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19361 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19362 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19363 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19364 then
19365 Error_Msg_N
19366 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19367 end if;
19368
19369 -- Proceed with analysis of mod expression
19370
19371 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19372 Set_Etype (T, T);
19373 Set_Ekind (T, E_Modular_Integer_Type);
19374 Init_Alignment (T);
19375 Set_Is_Constrained (T);
19376
19377 if not Is_OK_Static_Expression (Mod_Expr) then
19378 Flag_Non_Static_Expr
19379 ("non-static expression used for modular type bound!", Mod_Expr);
19380 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19381 else
19382 M_Val := Expr_Value (Mod_Expr);
19383 end if;
19384
19385 if M_Val < 1 then
19386 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19387 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19388 end if;
19389
19390 if M_Val > 2 ** Standard_Long_Integer_Size then
19391 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19392 end if;
19393
19394 Set_Modulus (T, M_Val);
19395
19396 -- Create bounds for the modular type based on the modulus given in
19397 -- the type declaration and then analyze and resolve those bounds.
19398
19399 Set_Scalar_Range (T,
19400 Make_Range (Sloc (Mod_Expr),
19401 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19402 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19403
19404 -- Properly analyze the literals for the range. We do this manually
19405 -- because we can't go calling Resolve, since we are resolving these
19406 -- bounds with the type, and this type is certainly not complete yet.
19407
19408 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19409 Set_Etype (High_Bound (Scalar_Range (T)), T);
19410 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19411 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19412
19413 -- Loop through powers of two to find number of bits required
19414
19415 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19416
19417 -- Binary case
19418
19419 if M_Val = 2 ** Bits then
19420 Set_Modular_Size (Bits);
19421 return;
19422
19423 -- Nonbinary case
19424
19425 elsif M_Val < 2 ** Bits then
19426 Set_Non_Binary_Modulus (T);
19427
19428 if Bits > System_Max_Nonbinary_Modulus_Power then
19429 Error_Msg_Uint_1 :=
19430 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19431 Error_Msg_F
19432 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19433 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19434 return;
19435
19436 else
19437 -- In the nonbinary case, set size as per RM 13.3(55)
19438
19439 Set_Modular_Size (Bits);
19440 return;
19441 end if;
19442 end if;
19443
19444 end loop;
19445
19446 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19447 -- so we just signal an error and set the maximum size.
19448
19449 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19450 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19451
19452 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19453 Init_Alignment (T);
19454
19455 end Modular_Type_Declaration;
19456
19457 --------------------------
19458 -- New_Concatenation_Op --
19459 --------------------------
19460
19461 procedure New_Concatenation_Op (Typ : Entity_Id) is
19462 Loc : constant Source_Ptr := Sloc (Typ);
19463 Op : Entity_Id;
19464
19465 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19466 -- Create abbreviated declaration for the formal of a predefined
19467 -- Operator 'Op' of type 'Typ'
19468
19469 --------------------
19470 -- Make_Op_Formal --
19471 --------------------
19472
19473 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19474 Formal : Entity_Id;
19475 begin
19476 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19477 Set_Etype (Formal, Typ);
19478 Set_Mechanism (Formal, Default_Mechanism);
19479 return Formal;
19480 end Make_Op_Formal;
19481
19482 -- Start of processing for New_Concatenation_Op
19483
19484 begin
19485 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19486
19487 Set_Ekind (Op, E_Operator);
19488 Set_Scope (Op, Current_Scope);
19489 Set_Etype (Op, Typ);
19490 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19491 Set_Is_Immediately_Visible (Op);
19492 Set_Is_Intrinsic_Subprogram (Op);
19493 Set_Has_Completion (Op);
19494 Append_Entity (Op, Current_Scope);
19495
19496 Set_Name_Entity_Id (Name_Op_Concat, Op);
19497
19498 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19499 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19500 end New_Concatenation_Op;
19501
19502 -------------------------
19503 -- OK_For_Limited_Init --
19504 -------------------------
19505
19506 -- ???Check all calls of this, and compare the conditions under which it's
19507 -- called.
19508
19509 function OK_For_Limited_Init
19510 (Typ : Entity_Id;
19511 Exp : Node_Id) return Boolean
19512 is
19513 begin
19514 return Is_CPP_Constructor_Call (Exp)
19515 or else (Ada_Version >= Ada_2005
19516 and then not Debug_Flag_Dot_L
19517 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19518 end OK_For_Limited_Init;
19519
19520 -------------------------------
19521 -- OK_For_Limited_Init_In_05 --
19522 -------------------------------
19523
19524 function OK_For_Limited_Init_In_05
19525 (Typ : Entity_Id;
19526 Exp : Node_Id) return Boolean
19527 is
19528 begin
19529 -- An object of a limited interface type can be initialized with any
19530 -- expression of a nonlimited descendant type. However this does not
19531 -- apply if this is a view conversion of some other expression. This
19532 -- is checked below.
19533
19534 if Is_Class_Wide_Type (Typ)
19535 and then Is_Limited_Interface (Typ)
19536 and then not Is_Limited_Type (Etype (Exp))
19537 and then Nkind (Exp) /= N_Type_Conversion
19538 then
19539 return True;
19540 end if;
19541
19542 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19543 -- case of limited aggregates (including extension aggregates), and
19544 -- function calls. The function call may have been given in prefixed
19545 -- notation, in which case the original node is an indexed component.
19546 -- If the function is parameterless, the original node was an explicit
19547 -- dereference. The function may also be parameterless, in which case
19548 -- the source node is just an identifier.
19549
19550 -- A branch of a conditional expression may have been removed if the
19551 -- condition is statically known. This happens during expansion, and
19552 -- thus will not happen if previous errors were encountered. The check
19553 -- will have been performed on the chosen branch, which replaces the
19554 -- original conditional expression.
19555
19556 if No (Exp) then
19557 return True;
19558 end if;
19559
19560 case Nkind (Original_Node (Exp)) is
19561 when N_Aggregate
19562 | N_Extension_Aggregate
19563 | N_Function_Call
19564 | N_Op
19565 =>
19566 return True;
19567
19568 when N_Identifier =>
19569 return Present (Entity (Original_Node (Exp)))
19570 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19571
19572 when N_Qualified_Expression =>
19573 return
19574 OK_For_Limited_Init_In_05
19575 (Typ, Expression (Original_Node (Exp)));
19576
19577 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19578 -- with a function call, the expander has rewritten the call into an
19579 -- N_Type_Conversion node to force displacement of the pointer to
19580 -- reference the component containing the secondary dispatch table.
19581 -- Otherwise a type conversion is not a legal context.
19582 -- A return statement for a build-in-place function returning a
19583 -- synchronized type also introduces an unchecked conversion.
19584
19585 when N_Type_Conversion
19586 | N_Unchecked_Type_Conversion
19587 =>
19588 return not Comes_From_Source (Exp)
19589 and then
19590 -- If the conversion has been rewritten, check Original_Node
19591
19592 ((Original_Node (Exp) /= Exp
19593 and then
19594 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
19595
19596 -- Otherwise, check the expression of the compiler-generated
19597 -- conversion (which is a conversion that we want to ignore
19598 -- for purposes of the limited-initialization restrictions).
19599
19600 or else
19601 (Original_Node (Exp) = Exp
19602 and then
19603 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
19604
19605 when N_Explicit_Dereference
19606 | N_Indexed_Component
19607 | N_Selected_Component
19608 =>
19609 return Nkind (Exp) = N_Function_Call;
19610
19611 -- A use of 'Input is a function call, hence allowed. Normally the
19612 -- attribute will be changed to a call, but the attribute by itself
19613 -- can occur with -gnatc.
19614
19615 when N_Attribute_Reference =>
19616 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19617
19618 -- "return raise ..." is OK
19619
19620 when N_Raise_Expression =>
19621 return True;
19622
19623 -- For a case expression, all dependent expressions must be legal
19624
19625 when N_Case_Expression =>
19626 declare
19627 Alt : Node_Id;
19628
19629 begin
19630 Alt := First (Alternatives (Original_Node (Exp)));
19631 while Present (Alt) loop
19632 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19633 return False;
19634 end if;
19635
19636 Next (Alt);
19637 end loop;
19638
19639 return True;
19640 end;
19641
19642 -- For an if expression, all dependent expressions must be legal
19643
19644 when N_If_Expression =>
19645 declare
19646 Then_Expr : constant Node_Id :=
19647 Next (First (Expressions (Original_Node (Exp))));
19648 Else_Expr : constant Node_Id := Next (Then_Expr);
19649 begin
19650 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19651 and then
19652 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19653 end;
19654
19655 when others =>
19656 return False;
19657 end case;
19658 end OK_For_Limited_Init_In_05;
19659
19660 -------------------------------------------
19661 -- Ordinary_Fixed_Point_Type_Declaration --
19662 -------------------------------------------
19663
19664 procedure Ordinary_Fixed_Point_Type_Declaration
19665 (T : Entity_Id;
19666 Def : Node_Id)
19667 is
19668 Loc : constant Source_Ptr := Sloc (Def);
19669 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19670 RRS : constant Node_Id := Real_Range_Specification (Def);
19671 Implicit_Base : Entity_Id;
19672 Delta_Val : Ureal;
19673 Small_Val : Ureal;
19674 Low_Val : Ureal;
19675 High_Val : Ureal;
19676
19677 begin
19678 Check_Restriction (No_Fixed_Point, Def);
19679
19680 -- Create implicit base type
19681
19682 Implicit_Base :=
19683 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19684 Set_Etype (Implicit_Base, Implicit_Base);
19685
19686 -- Analyze and process delta expression
19687
19688 Analyze_And_Resolve (Delta_Expr, Any_Real);
19689
19690 Check_Delta_Expression (Delta_Expr);
19691 Delta_Val := Expr_Value_R (Delta_Expr);
19692
19693 Set_Delta_Value (Implicit_Base, Delta_Val);
19694
19695 -- Compute default small from given delta, which is the largest power
19696 -- of two that does not exceed the given delta value.
19697
19698 declare
19699 Tmp : Ureal;
19700 Scale : Int;
19701
19702 begin
19703 Tmp := Ureal_1;
19704 Scale := 0;
19705
19706 if Delta_Val < Ureal_1 then
19707 while Delta_Val < Tmp loop
19708 Tmp := Tmp / Ureal_2;
19709 Scale := Scale + 1;
19710 end loop;
19711
19712 else
19713 loop
19714 Tmp := Tmp * Ureal_2;
19715 exit when Tmp > Delta_Val;
19716 Scale := Scale - 1;
19717 end loop;
19718 end if;
19719
19720 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19721 end;
19722
19723 Set_Small_Value (Implicit_Base, Small_Val);
19724
19725 -- If no range was given, set a dummy range
19726
19727 if RRS <= Empty_Or_Error then
19728 Low_Val := -Small_Val;
19729 High_Val := Small_Val;
19730
19731 -- Otherwise analyze and process given range
19732
19733 else
19734 declare
19735 Low : constant Node_Id := Low_Bound (RRS);
19736 High : constant Node_Id := High_Bound (RRS);
19737
19738 begin
19739 Analyze_And_Resolve (Low, Any_Real);
19740 Analyze_And_Resolve (High, Any_Real);
19741 Check_Real_Bound (Low);
19742 Check_Real_Bound (High);
19743
19744 -- Obtain and set the range
19745
19746 Low_Val := Expr_Value_R (Low);
19747 High_Val := Expr_Value_R (High);
19748
19749 if Low_Val > High_Val then
19750 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19751 end if;
19752 end;
19753 end if;
19754
19755 -- The range for both the implicit base and the declared first subtype
19756 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19757 -- set a temporary range in place. Note that the bounds of the base
19758 -- type will be widened to be symmetrical and to fill the available
19759 -- bits when the type is frozen.
19760
19761 -- We could do this with all discrete types, and probably should, but
19762 -- we absolutely have to do it for fixed-point, since the end-points
19763 -- of the range and the size are determined by the small value, which
19764 -- could be reset before the freeze point.
19765
19766 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19767 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19768
19769 -- Complete definition of first subtype. The inheritance of the rep item
19770 -- chain ensures that SPARK-related pragmas are not clobbered when the
19771 -- ordinary fixed point type acts as a full view of a private type.
19772
19773 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19774 Set_Etype (T, Implicit_Base);
19775 Init_Size_Align (T);
19776 Inherit_Rep_Item_Chain (T, Implicit_Base);
19777 Set_Small_Value (T, Small_Val);
19778 Set_Delta_Value (T, Delta_Val);
19779 Set_Is_Constrained (T);
19780 end Ordinary_Fixed_Point_Type_Declaration;
19781
19782 ----------------------------------
19783 -- Preanalyze_Assert_Expression --
19784 ----------------------------------
19785
19786 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19787 begin
19788 In_Assertion_Expr := In_Assertion_Expr + 1;
19789 Preanalyze_Spec_Expression (N, T);
19790 In_Assertion_Expr := In_Assertion_Expr - 1;
19791 end Preanalyze_Assert_Expression;
19792
19793 -----------------------------------
19794 -- Preanalyze_Default_Expression --
19795 -----------------------------------
19796
19797 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19798 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19799 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19800
19801 begin
19802 In_Default_Expr := True;
19803 In_Spec_Expression := True;
19804
19805 Preanalyze_With_Freezing_And_Resolve (N, T);
19806
19807 In_Default_Expr := Save_In_Default_Expr;
19808 In_Spec_Expression := Save_In_Spec_Expression;
19809 end Preanalyze_Default_Expression;
19810
19811 --------------------------------
19812 -- Preanalyze_Spec_Expression --
19813 --------------------------------
19814
19815 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19816 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19817 begin
19818 In_Spec_Expression := True;
19819 Preanalyze_And_Resolve (N, T);
19820 In_Spec_Expression := Save_In_Spec_Expression;
19821 end Preanalyze_Spec_Expression;
19822
19823 ----------------------------------------
19824 -- Prepare_Private_Subtype_Completion --
19825 ----------------------------------------
19826
19827 procedure Prepare_Private_Subtype_Completion
19828 (Id : Entity_Id;
19829 Related_Nod : Node_Id)
19830 is
19831 Id_B : constant Entity_Id := Base_Type (Id);
19832 Full_B : constant Entity_Id := Full_View (Id_B);
19833 Full : Entity_Id;
19834
19835 begin
19836 if Present (Full_B) then
19837
19838 -- The Base_Type is already completed, we can complete the subtype
19839 -- now. We have to create a new entity with the same name, Thus we
19840 -- can't use Create_Itype.
19841
19842 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19843 Set_Is_Itype (Full);
19844 Set_Associated_Node_For_Itype (Full, Related_Nod);
19845 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19846 Set_Full_View (Id, Full);
19847 end if;
19848
19849 -- The parent subtype may be private, but the base might not, in some
19850 -- nested instances. In that case, the subtype does not need to be
19851 -- exchanged. It would still be nice to make private subtypes and their
19852 -- bases consistent at all times ???
19853
19854 if Is_Private_Type (Id_B) then
19855 Append_Elmt (Id, Private_Dependents (Id_B));
19856 end if;
19857 end Prepare_Private_Subtype_Completion;
19858
19859 ---------------------------
19860 -- Process_Discriminants --
19861 ---------------------------
19862
19863 procedure Process_Discriminants
19864 (N : Node_Id;
19865 Prev : Entity_Id := Empty)
19866 is
19867 Elist : constant Elist_Id := New_Elmt_List;
19868 Id : Node_Id;
19869 Discr : Node_Id;
19870 Discr_Number : Uint;
19871 Discr_Type : Entity_Id;
19872 Default_Present : Boolean := False;
19873 Default_Not_Present : Boolean := False;
19874
19875 begin
19876 -- A composite type other than an array type can have discriminants.
19877 -- On entry, the current scope is the composite type.
19878
19879 -- The discriminants are initially entered into the scope of the type
19880 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19881 -- use, as explained at the end of this procedure.
19882
19883 Discr := First (Discriminant_Specifications (N));
19884 while Present (Discr) loop
19885 Enter_Name (Defining_Identifier (Discr));
19886
19887 -- For navigation purposes we add a reference to the discriminant
19888 -- in the entity for the type. If the current declaration is a
19889 -- completion, place references on the partial view. Otherwise the
19890 -- type is the current scope.
19891
19892 if Present (Prev) then
19893
19894 -- The references go on the partial view, if present. If the
19895 -- partial view has discriminants, the references have been
19896 -- generated already.
19897
19898 if not Has_Discriminants (Prev) then
19899 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19900 end if;
19901 else
19902 Generate_Reference
19903 (Current_Scope, Defining_Identifier (Discr), 'd');
19904 end if;
19905
19906 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19907 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19908
19909 -- Ada 2005 (AI-254)
19910
19911 if Present (Access_To_Subprogram_Definition
19912 (Discriminant_Type (Discr)))
19913 and then Protected_Present (Access_To_Subprogram_Definition
19914 (Discriminant_Type (Discr)))
19915 then
19916 Discr_Type :=
19917 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19918 end if;
19919
19920 else
19921 Find_Type (Discriminant_Type (Discr));
19922 Discr_Type := Etype (Discriminant_Type (Discr));
19923
19924 if Error_Posted (Discriminant_Type (Discr)) then
19925 Discr_Type := Any_Type;
19926 end if;
19927 end if;
19928
19929 -- Handling of discriminants that are access types
19930
19931 if Is_Access_Type (Discr_Type) then
19932
19933 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19934 -- limited record types
19935
19936 if Ada_Version < Ada_2005 then
19937 Check_Access_Discriminant_Requires_Limited
19938 (Discr, Discriminant_Type (Discr));
19939 end if;
19940
19941 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19942 Error_Msg_N
19943 ("(Ada 83) access discriminant not allowed", Discr);
19944 end if;
19945
19946 -- If not access type, must be a discrete type
19947
19948 elsif not Is_Discrete_Type (Discr_Type) then
19949 Error_Msg_N
19950 ("discriminants must have a discrete or access type",
19951 Discriminant_Type (Discr));
19952 end if;
19953
19954 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19955
19956 -- If a discriminant specification includes the assignment compound
19957 -- delimiter followed by an expression, the expression is the default
19958 -- expression of the discriminant; the default expression must be of
19959 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19960 -- a default expression, we do the special preanalysis, since this
19961 -- expression does not freeze (see section "Handling of Default and
19962 -- Per-Object Expressions" in spec of package Sem).
19963
19964 if Present (Expression (Discr)) then
19965 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19966
19967 -- Legaity checks
19968
19969 if Nkind (N) = N_Formal_Type_Declaration then
19970 Error_Msg_N
19971 ("discriminant defaults not allowed for formal type",
19972 Expression (Discr));
19973
19974 -- Flag an error for a tagged type with defaulted discriminants,
19975 -- excluding limited tagged types when compiling for Ada 2012
19976 -- (see AI05-0214).
19977
19978 elsif Is_Tagged_Type (Current_Scope)
19979 and then (not Is_Limited_Type (Current_Scope)
19980 or else Ada_Version < Ada_2012)
19981 and then Comes_From_Source (N)
19982 then
19983 -- Note: see similar test in Check_Or_Process_Discriminants, to
19984 -- handle the (illegal) case of the completion of an untagged
19985 -- view with discriminants with defaults by a tagged full view.
19986 -- We skip the check if Discr does not come from source, to
19987 -- account for the case of an untagged derived type providing
19988 -- defaults for a renamed discriminant from a private untagged
19989 -- ancestor with a tagged full view (ACATS B460006).
19990
19991 if Ada_Version >= Ada_2012 then
19992 Error_Msg_N
19993 ("discriminants of nonlimited tagged type cannot have"
19994 & " defaults",
19995 Expression (Discr));
19996 else
19997 Error_Msg_N
19998 ("discriminants of tagged type cannot have defaults",
19999 Expression (Discr));
20000 end if;
20001
20002 else
20003 Default_Present := True;
20004 Append_Elmt (Expression (Discr), Elist);
20005
20006 -- Tag the defining identifiers for the discriminants with
20007 -- their corresponding default expressions from the tree.
20008
20009 Set_Discriminant_Default_Value
20010 (Defining_Identifier (Discr), Expression (Discr));
20011 end if;
20012
20013 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20014 -- gets set unless we can be sure that no range check is required.
20015
20016 if not Expander_Active
20017 and then not
20018 Is_In_Range
20019 (Expression (Discr), Discr_Type, Assume_Valid => True)
20020 then
20021 Set_Do_Range_Check (Expression (Discr));
20022 end if;
20023
20024 -- No default discriminant value given
20025
20026 else
20027 Default_Not_Present := True;
20028 end if;
20029
20030 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20031 -- Discr_Type but with the null-exclusion attribute
20032
20033 if Ada_Version >= Ada_2005 then
20034
20035 -- Ada 2005 (AI-231): Static checks
20036
20037 if Can_Never_Be_Null (Discr_Type) then
20038 Null_Exclusion_Static_Checks (Discr);
20039
20040 elsif Is_Access_Type (Discr_Type)
20041 and then Null_Exclusion_Present (Discr)
20042
20043 -- No need to check itypes because in their case this check
20044 -- was done at their point of creation
20045
20046 and then not Is_Itype (Discr_Type)
20047 then
20048 if Can_Never_Be_Null (Discr_Type) then
20049 Error_Msg_NE
20050 ("`NOT NULL` not allowed (& already excludes null)",
20051 Discr,
20052 Discr_Type);
20053 end if;
20054
20055 Set_Etype (Defining_Identifier (Discr),
20056 Create_Null_Excluding_Itype
20057 (T => Discr_Type,
20058 Related_Nod => Discr));
20059
20060 -- Check for improper null exclusion if the type is otherwise
20061 -- legal for a discriminant.
20062
20063 elsif Null_Exclusion_Present (Discr)
20064 and then Is_Discrete_Type (Discr_Type)
20065 then
20066 Error_Msg_N
20067 ("null exclusion can only apply to an access type", Discr);
20068 end if;
20069
20070 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20071 -- can't have defaults. Synchronized types, or types that are
20072 -- explicitly limited are fine, but special tests apply to derived
20073 -- types in generics: in a generic body we have to assume the
20074 -- worst, and therefore defaults are not allowed if the parent is
20075 -- a generic formal private type (see ACATS B370001).
20076
20077 if Is_Access_Type (Discr_Type) and then Default_Present then
20078 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20079 or else Is_Limited_Record (Current_Scope)
20080 or else Is_Concurrent_Type (Current_Scope)
20081 or else Is_Concurrent_Record_Type (Current_Scope)
20082 or else Ekind (Current_Scope) = E_Limited_Private_Type
20083 then
20084 if not Is_Derived_Type (Current_Scope)
20085 or else not Is_Generic_Type (Etype (Current_Scope))
20086 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20087 or else Limited_Present
20088 (Type_Definition (Parent (Current_Scope)))
20089 then
20090 null;
20091
20092 else
20093 Error_Msg_N
20094 ("access discriminants of nonlimited types cannot "
20095 & "have defaults", Expression (Discr));
20096 end if;
20097
20098 elsif Present (Expression (Discr)) then
20099 Error_Msg_N
20100 ("(Ada 2005) access discriminants of nonlimited types "
20101 & "cannot have defaults", Expression (Discr));
20102 end if;
20103 end if;
20104 end if;
20105
20106 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20107 -- This check is relevant only when SPARK_Mode is on as it is not a
20108 -- standard Ada legality rule.
20109
20110 if SPARK_Mode = On
20111 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20112 then
20113 Error_Msg_N ("discriminant cannot be volatile", Discr);
20114 end if;
20115
20116 Next (Discr);
20117 end loop;
20118
20119 -- An element list consisting of the default expressions of the
20120 -- discriminants is constructed in the above loop and used to set
20121 -- the Discriminant_Constraint attribute for the type. If an object
20122 -- is declared of this (record or task) type without any explicit
20123 -- discriminant constraint given, this element list will form the
20124 -- actual parameters for the corresponding initialization procedure
20125 -- for the type.
20126
20127 Set_Discriminant_Constraint (Current_Scope, Elist);
20128 Set_Stored_Constraint (Current_Scope, No_Elist);
20129
20130 -- Default expressions must be provided either for all or for none
20131 -- of the discriminants of a discriminant part. (RM 3.7.1)
20132
20133 if Default_Present and then Default_Not_Present then
20134 Error_Msg_N
20135 ("incomplete specification of defaults for discriminants", N);
20136 end if;
20137
20138 -- The use of the name of a discriminant is not allowed in default
20139 -- expressions of a discriminant part if the specification of the
20140 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20141
20142 -- To detect this, the discriminant names are entered initially with an
20143 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20144 -- attempt to use a void entity (for example in an expression that is
20145 -- type-checked) produces the error message: premature usage. Now after
20146 -- completing the semantic analysis of the discriminant part, we can set
20147 -- the Ekind of all the discriminants appropriately.
20148
20149 Discr := First (Discriminant_Specifications (N));
20150 Discr_Number := Uint_1;
20151 while Present (Discr) loop
20152 Id := Defining_Identifier (Discr);
20153 Set_Ekind (Id, E_Discriminant);
20154 Init_Component_Location (Id);
20155 Init_Esize (Id);
20156 Set_Discriminant_Number (Id, Discr_Number);
20157
20158 -- Make sure this is always set, even in illegal programs
20159
20160 Set_Corresponding_Discriminant (Id, Empty);
20161
20162 -- Initialize the Original_Record_Component to the entity itself.
20163 -- Inherit_Components will propagate the right value to
20164 -- discriminants in derived record types.
20165
20166 Set_Original_Record_Component (Id, Id);
20167
20168 -- Create the discriminal for the discriminant
20169
20170 Build_Discriminal (Id);
20171
20172 Next (Discr);
20173 Discr_Number := Discr_Number + 1;
20174 end loop;
20175
20176 Set_Has_Discriminants (Current_Scope);
20177 end Process_Discriminants;
20178
20179 -----------------------
20180 -- Process_Full_View --
20181 -----------------------
20182
20183 -- WARNING: This routine manages Ghost regions. Return statements must be
20184 -- replaced by gotos which jump to the end of the routine and restore the
20185 -- Ghost mode.
20186
20187 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20188 procedure Collect_Implemented_Interfaces
20189 (Typ : Entity_Id;
20190 Ifaces : Elist_Id);
20191 -- Ada 2005: Gather all the interfaces that Typ directly or
20192 -- inherently implements. Duplicate entries are not added to
20193 -- the list Ifaces.
20194
20195 ------------------------------------
20196 -- Collect_Implemented_Interfaces --
20197 ------------------------------------
20198
20199 procedure Collect_Implemented_Interfaces
20200 (Typ : Entity_Id;
20201 Ifaces : Elist_Id)
20202 is
20203 Iface : Entity_Id;
20204 Iface_Elmt : Elmt_Id;
20205
20206 begin
20207 -- Abstract interfaces are only associated with tagged record types
20208
20209 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20210 return;
20211 end if;
20212
20213 -- Recursively climb to the ancestors
20214
20215 if Etype (Typ) /= Typ
20216
20217 -- Protect the frontend against wrong cyclic declarations like:
20218
20219 -- type B is new A with private;
20220 -- type C is new A with private;
20221 -- private
20222 -- type B is new C with null record;
20223 -- type C is new B with null record;
20224
20225 and then Etype (Typ) /= Priv_T
20226 and then Etype (Typ) /= Full_T
20227 then
20228 -- Keep separate the management of private type declarations
20229
20230 if Ekind (Typ) = E_Record_Type_With_Private then
20231
20232 -- Handle the following illegal usage:
20233 -- type Private_Type is tagged private;
20234 -- private
20235 -- type Private_Type is new Type_Implementing_Iface;
20236
20237 if Present (Full_View (Typ))
20238 and then Etype (Typ) /= Full_View (Typ)
20239 then
20240 if Is_Interface (Etype (Typ)) then
20241 Append_Unique_Elmt (Etype (Typ), Ifaces);
20242 end if;
20243
20244 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20245 end if;
20246
20247 -- Non-private types
20248
20249 else
20250 if Is_Interface (Etype (Typ)) then
20251 Append_Unique_Elmt (Etype (Typ), Ifaces);
20252 end if;
20253
20254 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20255 end if;
20256 end if;
20257
20258 -- Handle entities in the list of abstract interfaces
20259
20260 if Present (Interfaces (Typ)) then
20261 Iface_Elmt := First_Elmt (Interfaces (Typ));
20262 while Present (Iface_Elmt) loop
20263 Iface := Node (Iface_Elmt);
20264
20265 pragma Assert (Is_Interface (Iface));
20266
20267 if not Contain_Interface (Iface, Ifaces) then
20268 Append_Elmt (Iface, Ifaces);
20269 Collect_Implemented_Interfaces (Iface, Ifaces);
20270 end if;
20271
20272 Next_Elmt (Iface_Elmt);
20273 end loop;
20274 end if;
20275 end Collect_Implemented_Interfaces;
20276
20277 -- Local variables
20278
20279 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20280 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20281 -- Save the Ghost-related attributes to restore on exit
20282
20283 Full_Indic : Node_Id;
20284 Full_Parent : Entity_Id;
20285 Priv_Parent : Entity_Id;
20286
20287 -- Start of processing for Process_Full_View
20288
20289 begin
20290 Mark_And_Set_Ghost_Completion (N, Priv_T);
20291
20292 -- First some sanity checks that must be done after semantic
20293 -- decoration of the full view and thus cannot be placed with other
20294 -- similar checks in Find_Type_Name
20295
20296 if not Is_Limited_Type (Priv_T)
20297 and then (Is_Limited_Type (Full_T)
20298 or else Is_Limited_Composite (Full_T))
20299 then
20300 if In_Instance then
20301 null;
20302 else
20303 Error_Msg_N
20304 ("completion of nonlimited type cannot be limited", Full_T);
20305 Explain_Limited_Type (Full_T, Full_T);
20306 end if;
20307
20308 elsif Is_Abstract_Type (Full_T)
20309 and then not Is_Abstract_Type (Priv_T)
20310 then
20311 Error_Msg_N
20312 ("completion of nonabstract type cannot be abstract", Full_T);
20313
20314 elsif Is_Tagged_Type (Priv_T)
20315 and then Is_Limited_Type (Priv_T)
20316 and then not Is_Limited_Type (Full_T)
20317 then
20318 -- If pragma CPP_Class was applied to the private declaration
20319 -- propagate the limitedness to the full-view
20320
20321 if Is_CPP_Class (Priv_T) then
20322 Set_Is_Limited_Record (Full_T);
20323
20324 -- GNAT allow its own definition of Limited_Controlled to disobey
20325 -- this rule in order in ease the implementation. This test is safe
20326 -- because Root_Controlled is defined in a child of System that
20327 -- normal programs are not supposed to use.
20328
20329 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20330 Set_Is_Limited_Composite (Full_T);
20331 else
20332 Error_Msg_N
20333 ("completion of limited tagged type must be limited", Full_T);
20334 end if;
20335
20336 elsif Is_Generic_Type (Priv_T) then
20337 Error_Msg_N ("generic type cannot have a completion", Full_T);
20338 end if;
20339
20340 -- Check that ancestor interfaces of private and full views are
20341 -- consistent. We omit this check for synchronized types because
20342 -- they are performed on the corresponding record type when frozen.
20343
20344 if Ada_Version >= Ada_2005
20345 and then Is_Tagged_Type (Priv_T)
20346 and then Is_Tagged_Type (Full_T)
20347 and then not Is_Concurrent_Type (Full_T)
20348 then
20349 declare
20350 Iface : Entity_Id;
20351 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20352 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20353
20354 begin
20355 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20356 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20357
20358 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20359 -- an interface type if and only if the full type is descendant
20360 -- of the interface type (AARM 7.3 (7.3/2)).
20361
20362 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20363
20364 if Present (Iface) then
20365 Error_Msg_NE
20366 ("interface in partial view& not implemented by full type "
20367 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20368 end if;
20369
20370 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20371
20372 if Present (Iface) then
20373 Error_Msg_NE
20374 ("interface & not implemented by partial view "
20375 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20376 end if;
20377 end;
20378 end if;
20379
20380 if Is_Tagged_Type (Priv_T)
20381 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20382 and then Is_Derived_Type (Full_T)
20383 then
20384 Priv_Parent := Etype (Priv_T);
20385
20386 -- The full view of a private extension may have been transformed
20387 -- into an unconstrained derived type declaration and a subtype
20388 -- declaration (see build_derived_record_type for details).
20389
20390 if Nkind (N) = N_Subtype_Declaration then
20391 Full_Indic := Subtype_Indication (N);
20392 Full_Parent := Etype (Base_Type (Full_T));
20393 else
20394 Full_Indic := Subtype_Indication (Type_Definition (N));
20395 Full_Parent := Etype (Full_T);
20396 end if;
20397
20398 -- Check that the parent type of the full type is a descendant of
20399 -- the ancestor subtype given in the private extension. If either
20400 -- entity has an Etype equal to Any_Type then we had some previous
20401 -- error situation [7.3(8)].
20402
20403 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20404 goto Leave;
20405
20406 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20407 -- any order. Therefore we don't have to check that its parent must
20408 -- be a descendant of the parent of the private type declaration.
20409
20410 elsif Is_Interface (Priv_Parent)
20411 and then Is_Interface (Full_Parent)
20412 then
20413 null;
20414
20415 -- Ada 2005 (AI-251): If the parent of the private type declaration
20416 -- is an interface there is no need to check that it is an ancestor
20417 -- of the associated full type declaration. The required tests for
20418 -- this case are performed by Build_Derived_Record_Type.
20419
20420 elsif not Is_Interface (Base_Type (Priv_Parent))
20421 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20422 then
20423 Error_Msg_N
20424 ("parent of full type must descend from parent of private "
20425 & "extension", Full_Indic);
20426
20427 -- First check a formal restriction, and then proceed with checking
20428 -- Ada rules. Since the formal restriction is not a serious error, we
20429 -- don't prevent further error detection for this check, hence the
20430 -- ELSE.
20431
20432 else
20433 -- Check the rules of 7.3(10): if the private extension inherits
20434 -- known discriminants, then the full type must also inherit those
20435 -- discriminants from the same (ancestor) type, and the parent
20436 -- subtype of the full type must be constrained if and only if
20437 -- the ancestor subtype of the private extension is constrained.
20438
20439 if No (Discriminant_Specifications (Parent (Priv_T)))
20440 and then not Has_Unknown_Discriminants (Priv_T)
20441 and then Has_Discriminants (Base_Type (Priv_Parent))
20442 then
20443 declare
20444 Priv_Indic : constant Node_Id :=
20445 Subtype_Indication (Parent (Priv_T));
20446
20447 Priv_Constr : constant Boolean :=
20448 Is_Constrained (Priv_Parent)
20449 or else
20450 Nkind (Priv_Indic) = N_Subtype_Indication
20451 or else
20452 Is_Constrained (Entity (Priv_Indic));
20453
20454 Full_Constr : constant Boolean :=
20455 Is_Constrained (Full_Parent)
20456 or else
20457 Nkind (Full_Indic) = N_Subtype_Indication
20458 or else
20459 Is_Constrained (Entity (Full_Indic));
20460
20461 Priv_Discr : Entity_Id;
20462 Full_Discr : Entity_Id;
20463
20464 begin
20465 Priv_Discr := First_Discriminant (Priv_Parent);
20466 Full_Discr := First_Discriminant (Full_Parent);
20467 while Present (Priv_Discr) and then Present (Full_Discr) loop
20468 if Original_Record_Component (Priv_Discr) =
20469 Original_Record_Component (Full_Discr)
20470 or else
20471 Corresponding_Discriminant (Priv_Discr) =
20472 Corresponding_Discriminant (Full_Discr)
20473 then
20474 null;
20475 else
20476 exit;
20477 end if;
20478
20479 Next_Discriminant (Priv_Discr);
20480 Next_Discriminant (Full_Discr);
20481 end loop;
20482
20483 if Present (Priv_Discr) or else Present (Full_Discr) then
20484 Error_Msg_N
20485 ("full view must inherit discriminants of the parent "
20486 & "type used in the private extension", Full_Indic);
20487
20488 elsif Priv_Constr and then not Full_Constr then
20489 Error_Msg_N
20490 ("parent subtype of full type must be constrained",
20491 Full_Indic);
20492
20493 elsif Full_Constr and then not Priv_Constr then
20494 Error_Msg_N
20495 ("parent subtype of full type must be unconstrained",
20496 Full_Indic);
20497 end if;
20498 end;
20499
20500 -- Check the rules of 7.3(12): if a partial view has neither
20501 -- known or unknown discriminants, then the full type
20502 -- declaration shall define a definite subtype.
20503
20504 elsif not Has_Unknown_Discriminants (Priv_T)
20505 and then not Has_Discriminants (Priv_T)
20506 and then not Is_Constrained (Full_T)
20507 then
20508 Error_Msg_N
20509 ("full view must define a constrained type if partial view "
20510 & "has no discriminants", Full_T);
20511 end if;
20512
20513 -- ??????? Do we implement the following properly ?????
20514 -- If the ancestor subtype of a private extension has constrained
20515 -- discriminants, then the parent subtype of the full view shall
20516 -- impose a statically matching constraint on those discriminants
20517 -- [7.3(13)].
20518 end if;
20519
20520 else
20521 -- For untagged types, verify that a type without discriminants is
20522 -- not completed with an unconstrained type. A separate error message
20523 -- is produced if the full type has defaulted discriminants.
20524
20525 if Is_Definite_Subtype (Priv_T)
20526 and then not Is_Definite_Subtype (Full_T)
20527 then
20528 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20529 Error_Msg_NE
20530 ("full view of& not compatible with declaration#",
20531 Full_T, Priv_T);
20532
20533 if not Is_Tagged_Type (Full_T) then
20534 Error_Msg_N
20535 ("\one is constrained, the other unconstrained", Full_T);
20536 end if;
20537 end if;
20538 end if;
20539
20540 -- AI-419: verify that the use of "limited" is consistent
20541
20542 declare
20543 Orig_Decl : constant Node_Id := Original_Node (N);
20544
20545 begin
20546 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20547 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20548 and then Nkind
20549 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20550 then
20551 if not Limited_Present (Parent (Priv_T))
20552 and then not Synchronized_Present (Parent (Priv_T))
20553 and then Limited_Present (Type_Definition (Orig_Decl))
20554 then
20555 Error_Msg_N
20556 ("full view of non-limited extension cannot be limited", N);
20557
20558 -- Conversely, if the partial view carries the limited keyword,
20559 -- the full view must as well, even if it may be redundant.
20560
20561 elsif Limited_Present (Parent (Priv_T))
20562 and then not Limited_Present (Type_Definition (Orig_Decl))
20563 then
20564 Error_Msg_N
20565 ("full view of limited extension must be explicitly limited",
20566 N);
20567 end if;
20568 end if;
20569 end;
20570
20571 -- Ada 2005 (AI-443): A synchronized private extension must be
20572 -- completed by a task or protected type.
20573
20574 if Ada_Version >= Ada_2005
20575 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20576 and then Synchronized_Present (Parent (Priv_T))
20577 and then not Is_Concurrent_Type (Full_T)
20578 then
20579 Error_Msg_N ("full view of synchronized extension must " &
20580 "be synchronized type", N);
20581 end if;
20582
20583 -- Ada 2005 AI-363: if the full view has discriminants with
20584 -- defaults, it is illegal to declare constrained access subtypes
20585 -- whose designated type is the current type. This allows objects
20586 -- of the type that are declared in the heap to be unconstrained.
20587
20588 if not Has_Unknown_Discriminants (Priv_T)
20589 and then not Has_Discriminants (Priv_T)
20590 and then Has_Discriminants (Full_T)
20591 and then
20592 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20593 then
20594 Set_Has_Constrained_Partial_View (Full_T);
20595 Set_Has_Constrained_Partial_View (Priv_T);
20596 end if;
20597
20598 -- Create a full declaration for all its subtypes recorded in
20599 -- Private_Dependents and swap them similarly to the base type. These
20600 -- are subtypes that have been define before the full declaration of
20601 -- the private type. We also swap the entry in Private_Dependents list
20602 -- so we can properly restore the private view on exit from the scope.
20603
20604 declare
20605 Priv_Elmt : Elmt_Id;
20606 Priv_Scop : Entity_Id;
20607 Priv : Entity_Id;
20608 Full : Entity_Id;
20609
20610 begin
20611 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20612 while Present (Priv_Elmt) loop
20613 Priv := Node (Priv_Elmt);
20614 Priv_Scop := Scope (Priv);
20615
20616 if Ekind_In (Priv, E_Private_Subtype,
20617 E_Limited_Private_Subtype,
20618 E_Record_Subtype_With_Private)
20619 then
20620 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20621 Set_Is_Itype (Full);
20622 Set_Parent (Full, Parent (Priv));
20623 Set_Associated_Node_For_Itype (Full, N);
20624
20625 -- Now we need to complete the private subtype, but since the
20626 -- base type has already been swapped, we must also swap the
20627 -- subtypes (and thus, reverse the arguments in the call to
20628 -- Complete_Private_Subtype). Also note that we may need to
20629 -- re-establish the scope of the private subtype.
20630
20631 Copy_And_Swap (Priv, Full);
20632
20633 if not In_Open_Scopes (Priv_Scop) then
20634 Push_Scope (Priv_Scop);
20635
20636 else
20637 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20638
20639 Priv_Scop := Empty;
20640 end if;
20641
20642 Complete_Private_Subtype (Full, Priv, Full_T, N);
20643 Set_Full_View (Full, Priv);
20644
20645 if Present (Priv_Scop) then
20646 Pop_Scope;
20647 end if;
20648
20649 Replace_Elmt (Priv_Elmt, Full);
20650 end if;
20651
20652 Next_Elmt (Priv_Elmt);
20653 end loop;
20654 end;
20655
20656 -- If the private view was tagged, copy the new primitive operations
20657 -- from the private view to the full view.
20658
20659 if Is_Tagged_Type (Full_T) then
20660 declare
20661 Disp_Typ : Entity_Id;
20662 Full_List : Elist_Id;
20663 Prim : Entity_Id;
20664 Prim_Elmt : Elmt_Id;
20665 Priv_List : Elist_Id;
20666
20667 function Contains
20668 (E : Entity_Id;
20669 L : Elist_Id) return Boolean;
20670 -- Determine whether list L contains element E
20671
20672 --------------
20673 -- Contains --
20674 --------------
20675
20676 function Contains
20677 (E : Entity_Id;
20678 L : Elist_Id) return Boolean
20679 is
20680 List_Elmt : Elmt_Id;
20681
20682 begin
20683 List_Elmt := First_Elmt (L);
20684 while Present (List_Elmt) loop
20685 if Node (List_Elmt) = E then
20686 return True;
20687 end if;
20688
20689 Next_Elmt (List_Elmt);
20690 end loop;
20691
20692 return False;
20693 end Contains;
20694
20695 -- Start of processing
20696
20697 begin
20698 if Is_Tagged_Type (Priv_T) then
20699 Priv_List := Primitive_Operations (Priv_T);
20700 Prim_Elmt := First_Elmt (Priv_List);
20701
20702 -- In the case of a concurrent type completing a private tagged
20703 -- type, primitives may have been declared in between the two
20704 -- views. These subprograms need to be wrapped the same way
20705 -- entries and protected procedures are handled because they
20706 -- cannot be directly shared by the two views.
20707
20708 if Is_Concurrent_Type (Full_T) then
20709 declare
20710 Conc_Typ : constant Entity_Id :=
20711 Corresponding_Record_Type (Full_T);
20712 Curr_Nod : Node_Id := Parent (Conc_Typ);
20713 Wrap_Spec : Node_Id;
20714
20715 begin
20716 while Present (Prim_Elmt) loop
20717 Prim := Node (Prim_Elmt);
20718
20719 if Comes_From_Source (Prim)
20720 and then not Is_Abstract_Subprogram (Prim)
20721 then
20722 Wrap_Spec :=
20723 Make_Subprogram_Declaration (Sloc (Prim),
20724 Specification =>
20725 Build_Wrapper_Spec
20726 (Subp_Id => Prim,
20727 Obj_Typ => Conc_Typ,
20728 Formals =>
20729 Parameter_Specifications
20730 (Parent (Prim))));
20731
20732 Insert_After (Curr_Nod, Wrap_Spec);
20733 Curr_Nod := Wrap_Spec;
20734
20735 Analyze (Wrap_Spec);
20736
20737 -- Remove the wrapper from visibility to avoid
20738 -- spurious conflict with the wrapped entity.
20739
20740 Set_Is_Immediately_Visible
20741 (Defining_Entity (Specification (Wrap_Spec)),
20742 False);
20743 end if;
20744
20745 Next_Elmt (Prim_Elmt);
20746 end loop;
20747
20748 goto Leave;
20749 end;
20750
20751 -- For non-concurrent types, transfer explicit primitives, but
20752 -- omit those inherited from the parent of the private view
20753 -- since they will be re-inherited later on.
20754
20755 else
20756 Full_List := Primitive_Operations (Full_T);
20757 while Present (Prim_Elmt) loop
20758 Prim := Node (Prim_Elmt);
20759
20760 if Comes_From_Source (Prim)
20761 and then not Contains (Prim, Full_List)
20762 then
20763 Append_Elmt (Prim, Full_List);
20764 end if;
20765
20766 Next_Elmt (Prim_Elmt);
20767 end loop;
20768 end if;
20769
20770 -- Untagged private view
20771
20772 else
20773 Full_List := Primitive_Operations (Full_T);
20774
20775 -- In this case the partial view is untagged, so here we locate
20776 -- all of the earlier primitives that need to be treated as
20777 -- dispatching (those that appear between the two views). Note
20778 -- that these additional operations must all be new operations
20779 -- (any earlier operations that override inherited operations
20780 -- of the full view will already have been inserted in the
20781 -- primitives list, marked by Check_Operation_From_Private_View
20782 -- as dispatching. Note that implicit "/=" operators are
20783 -- excluded from being added to the primitives list since they
20784 -- shouldn't be treated as dispatching (tagged "/=" is handled
20785 -- specially).
20786
20787 Prim := Next_Entity (Full_T);
20788 while Present (Prim) and then Prim /= Priv_T loop
20789 if Ekind_In (Prim, E_Procedure, E_Function) then
20790 Disp_Typ := Find_Dispatching_Type (Prim);
20791
20792 if Disp_Typ = Full_T
20793 and then (Chars (Prim) /= Name_Op_Ne
20794 or else Comes_From_Source (Prim))
20795 then
20796 Check_Controlling_Formals (Full_T, Prim);
20797
20798 if Is_Suitable_Primitive (Prim)
20799 and then not Is_Dispatching_Operation (Prim)
20800 then
20801 Append_Elmt (Prim, Full_List);
20802 Set_Is_Dispatching_Operation (Prim);
20803 Set_DT_Position_Value (Prim, No_Uint);
20804 end if;
20805
20806 elsif Is_Dispatching_Operation (Prim)
20807 and then Disp_Typ /= Full_T
20808 then
20809 -- Verify that it is not otherwise controlled by a
20810 -- formal or a return value of type T.
20811
20812 Check_Controlling_Formals (Disp_Typ, Prim);
20813 end if;
20814 end if;
20815
20816 Next_Entity (Prim);
20817 end loop;
20818 end if;
20819
20820 -- For the tagged case, the two views can share the same primitive
20821 -- operations list and the same class-wide type. Update attributes
20822 -- of the class-wide type which depend on the full declaration.
20823
20824 if Is_Tagged_Type (Priv_T) then
20825 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20826 Set_Class_Wide_Type
20827 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20828
20829 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20830 end if;
20831 end;
20832 end if;
20833
20834 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20835
20836 if Known_To_Have_Preelab_Init (Priv_T) then
20837
20838 -- Case where there is a pragma Preelaborable_Initialization. We
20839 -- always allow this in predefined units, which is cheating a bit,
20840 -- but it means we don't have to struggle to meet the requirements in
20841 -- the RM for having Preelaborable Initialization. Otherwise we
20842 -- require that the type meets the RM rules. But we can't check that
20843 -- yet, because of the rule about overriding Initialize, so we simply
20844 -- set a flag that will be checked at freeze time.
20845
20846 if not In_Predefined_Unit (Full_T) then
20847 Set_Must_Have_Preelab_Init (Full_T);
20848 end if;
20849 end if;
20850
20851 -- If pragma CPP_Class was applied to the private type declaration,
20852 -- propagate it now to the full type declaration.
20853
20854 if Is_CPP_Class (Priv_T) then
20855 Set_Is_CPP_Class (Full_T);
20856 Set_Convention (Full_T, Convention_CPP);
20857
20858 -- Check that components of imported CPP types do not have default
20859 -- expressions.
20860
20861 Check_CPP_Type_Has_No_Defaults (Full_T);
20862 end if;
20863
20864 -- If the private view has user specified stream attributes, then so has
20865 -- the full view.
20866
20867 -- Why the test, how could these flags be already set in Full_T ???
20868
20869 if Has_Specified_Stream_Read (Priv_T) then
20870 Set_Has_Specified_Stream_Read (Full_T);
20871 end if;
20872
20873 if Has_Specified_Stream_Write (Priv_T) then
20874 Set_Has_Specified_Stream_Write (Full_T);
20875 end if;
20876
20877 if Has_Specified_Stream_Input (Priv_T) then
20878 Set_Has_Specified_Stream_Input (Full_T);
20879 end if;
20880
20881 if Has_Specified_Stream_Output (Priv_T) then
20882 Set_Has_Specified_Stream_Output (Full_T);
20883 end if;
20884
20885 -- Propagate Default_Initial_Condition-related attributes from the
20886 -- partial view to the full view.
20887
20888 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20889
20890 -- And to the underlying full view, if any
20891
20892 if Is_Private_Type (Full_T)
20893 and then Present (Underlying_Full_View (Full_T))
20894 then
20895 Propagate_DIC_Attributes
20896 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
20897 end if;
20898
20899 -- Propagate invariant-related attributes from the partial view to the
20900 -- full view.
20901
20902 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20903
20904 -- And to the underlying full view, if any
20905
20906 if Is_Private_Type (Full_T)
20907 and then Present (Underlying_Full_View (Full_T))
20908 then
20909 Propagate_Invariant_Attributes
20910 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
20911 end if;
20912
20913 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20914 -- in the full view without advertising the inheritance in the partial
20915 -- view. This can only occur when the partial view has no parent type
20916 -- and the full view has an interface as a parent. Any other scenarios
20917 -- are illegal because implemented interfaces must match between the
20918 -- two views.
20919
20920 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20921 declare
20922 Full_Par : constant Entity_Id := Etype (Full_T);
20923 Priv_Par : constant Entity_Id := Etype (Priv_T);
20924
20925 begin
20926 if not Is_Interface (Priv_Par)
20927 and then Is_Interface (Full_Par)
20928 and then Has_Inheritable_Invariants (Full_Par)
20929 then
20930 Error_Msg_N
20931 ("hidden inheritance of class-wide type invariants not "
20932 & "allowed", N);
20933 end if;
20934 end;
20935 end if;
20936
20937 -- Propagate predicates to full type, and predicate function if already
20938 -- defined. It is not clear that this can actually happen? the partial
20939 -- view cannot be frozen yet, and the predicate function has not been
20940 -- built. Still it is a cheap check and seems safer to make it.
20941
20942 Propagate_Predicate_Attributes (Full_T, Priv_T);
20943
20944 if Is_Private_Type (Full_T)
20945 and then Present (Underlying_Full_View (Full_T))
20946 then
20947 Propagate_Predicate_Attributes
20948 (Underlying_Full_View (Full_T), Priv_T);
20949 end if;
20950
20951 <<Leave>>
20952 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20953 end Process_Full_View;
20954
20955 -----------------------------------
20956 -- Process_Incomplete_Dependents --
20957 -----------------------------------
20958
20959 procedure Process_Incomplete_Dependents
20960 (N : Node_Id;
20961 Full_T : Entity_Id;
20962 Inc_T : Entity_Id)
20963 is
20964 Inc_Elmt : Elmt_Id;
20965 Priv_Dep : Entity_Id;
20966 New_Subt : Entity_Id;
20967
20968 Disc_Constraint : Elist_Id;
20969
20970 begin
20971 if No (Private_Dependents (Inc_T)) then
20972 return;
20973 end if;
20974
20975 -- Itypes that may be generated by the completion of an incomplete
20976 -- subtype are not used by the back-end and not attached to the tree.
20977 -- They are created only for constraint-checking purposes.
20978
20979 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20980 while Present (Inc_Elmt) loop
20981 Priv_Dep := Node (Inc_Elmt);
20982
20983 if Ekind (Priv_Dep) = E_Subprogram_Type then
20984
20985 -- An Access_To_Subprogram type may have a return type or a
20986 -- parameter type that is incomplete. Replace with the full view.
20987
20988 if Etype (Priv_Dep) = Inc_T then
20989 Set_Etype (Priv_Dep, Full_T);
20990 end if;
20991
20992 declare
20993 Formal : Entity_Id;
20994
20995 begin
20996 Formal := First_Formal (Priv_Dep);
20997 while Present (Formal) loop
20998 if Etype (Formal) = Inc_T then
20999 Set_Etype (Formal, Full_T);
21000 end if;
21001
21002 Next_Formal (Formal);
21003 end loop;
21004 end;
21005
21006 elsif Is_Overloadable (Priv_Dep) then
21007
21008 -- If a subprogram in the incomplete dependents list is primitive
21009 -- for a tagged full type then mark it as a dispatching operation,
21010 -- check whether it overrides an inherited subprogram, and check
21011 -- restrictions on its controlling formals. Note that a protected
21012 -- operation is never dispatching: only its wrapper operation
21013 -- (which has convention Ada) is.
21014
21015 if Is_Tagged_Type (Full_T)
21016 and then Is_Primitive (Priv_Dep)
21017 and then Convention (Priv_Dep) /= Convention_Protected
21018 then
21019 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21020 Set_Is_Dispatching_Operation (Priv_Dep);
21021 Check_Controlling_Formals (Full_T, Priv_Dep);
21022 end if;
21023
21024 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21025
21026 -- Can happen during processing of a body before the completion
21027 -- of a TA type. Ignore, because spec is also on dependent list.
21028
21029 return;
21030
21031 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21032 -- corresponding subtype of the full view.
21033
21034 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21035 and then Comes_From_Source (Priv_Dep)
21036 then
21037 Set_Subtype_Indication
21038 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21039 Set_Etype (Priv_Dep, Full_T);
21040 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21041 Set_Analyzed (Parent (Priv_Dep), False);
21042
21043 -- Reanalyze the declaration, suppressing the call to Enter_Name
21044 -- to avoid duplicate names.
21045
21046 Analyze_Subtype_Declaration
21047 (N => Parent (Priv_Dep),
21048 Skip => True);
21049
21050 -- Dependent is a subtype
21051
21052 else
21053 -- We build a new subtype indication using the full view of the
21054 -- incomplete parent. The discriminant constraints have been
21055 -- elaborated already at the point of the subtype declaration.
21056
21057 New_Subt := Create_Itype (E_Void, N);
21058
21059 if Has_Discriminants (Full_T) then
21060 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21061 else
21062 Disc_Constraint := No_Elist;
21063 end if;
21064
21065 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21066 Set_Full_View (Priv_Dep, New_Subt);
21067 end if;
21068
21069 Next_Elmt (Inc_Elmt);
21070 end loop;
21071 end Process_Incomplete_Dependents;
21072
21073 --------------------------------
21074 -- Process_Range_Expr_In_Decl --
21075 --------------------------------
21076
21077 procedure Process_Range_Expr_In_Decl
21078 (R : Node_Id;
21079 T : Entity_Id;
21080 Subtyp : Entity_Id := Empty;
21081 Check_List : List_Id := No_List;
21082 R_Check_Off : Boolean := False)
21083 is
21084 Lo, Hi : Node_Id;
21085 R_Checks : Check_Result;
21086 Insert_Node : Node_Id;
21087 Def_Id : Entity_Id;
21088
21089 begin
21090 Analyze_And_Resolve (R, Base_Type (T));
21091
21092 if Nkind (R) = N_Range then
21093 Lo := Low_Bound (R);
21094 Hi := High_Bound (R);
21095
21096 -- Validity checks on the range of a quantified expression are
21097 -- delayed until the construct is transformed into a loop.
21098
21099 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21100 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21101 then
21102 null;
21103
21104 -- We need to ensure validity of the bounds here, because if we
21105 -- go ahead and do the expansion, then the expanded code will get
21106 -- analyzed with range checks suppressed and we miss the check.
21107
21108 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21109 -- the temporaries generated by routine Remove_Side_Effects by means
21110 -- of validity checks must use the same names. When a range appears
21111 -- in the parent of a generic, the range is processed with checks
21112 -- disabled as part of the generic context and with checks enabled
21113 -- for code generation purposes. This leads to link issues as the
21114 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21115 -- template sees the temporaries generated by Remove_Side_Effects.
21116
21117 else
21118 Validity_Check_Range (R, Subtyp);
21119 end if;
21120
21121 -- If there were errors in the declaration, try and patch up some
21122 -- common mistakes in the bounds. The cases handled are literals
21123 -- which are Integer where the expected type is Real and vice versa.
21124 -- These corrections allow the compilation process to proceed further
21125 -- along since some basic assumptions of the format of the bounds
21126 -- are guaranteed.
21127
21128 if Etype (R) = Any_Type then
21129 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21130 Rewrite (Lo,
21131 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21132
21133 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21134 Rewrite (Hi,
21135 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21136
21137 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21138 Rewrite (Lo,
21139 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21140
21141 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21142 Rewrite (Hi,
21143 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21144 end if;
21145
21146 Set_Etype (Lo, T);
21147 Set_Etype (Hi, T);
21148 end if;
21149
21150 -- If the bounds of the range have been mistakenly given as string
21151 -- literals (perhaps in place of character literals), then an error
21152 -- has already been reported, but we rewrite the string literal as a
21153 -- bound of the range's type to avoid blowups in later processing
21154 -- that looks at static values.
21155
21156 if Nkind (Lo) = N_String_Literal then
21157 Rewrite (Lo,
21158 Make_Attribute_Reference (Sloc (Lo),
21159 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21160 Attribute_Name => Name_First));
21161 Analyze_And_Resolve (Lo);
21162 end if;
21163
21164 if Nkind (Hi) = N_String_Literal then
21165 Rewrite (Hi,
21166 Make_Attribute_Reference (Sloc (Hi),
21167 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21168 Attribute_Name => Name_First));
21169 Analyze_And_Resolve (Hi);
21170 end if;
21171
21172 -- If bounds aren't scalar at this point then exit, avoiding
21173 -- problems with further processing of the range in this procedure.
21174
21175 if not Is_Scalar_Type (Etype (Lo)) then
21176 return;
21177 end if;
21178
21179 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21180 -- then range of the base type. Here we check whether the bounds
21181 -- are in the range of the subtype itself. Note that if the bounds
21182 -- represent the null range the Constraint_Error exception should
21183 -- not be raised.
21184
21185 -- ??? The following code should be cleaned up as follows
21186
21187 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21188 -- is done in the call to Range_Check (R, T); below
21189
21190 -- 2. The use of R_Check_Off should be investigated and possibly
21191 -- removed, this would clean up things a bit.
21192
21193 if Is_Null_Range (Lo, Hi) then
21194 null;
21195
21196 else
21197 -- Capture values of bounds and generate temporaries for them
21198 -- if needed, before applying checks, since checks may cause
21199 -- duplication of the expression without forcing evaluation.
21200
21201 -- The forced evaluation removes side effects from expressions,
21202 -- which should occur also in GNATprove mode. Otherwise, we end up
21203 -- with unexpected insertions of actions at places where this is
21204 -- not supposed to occur, e.g. on default parameters of a call.
21205
21206 if Expander_Active or GNATprove_Mode then
21207
21208 -- Call Force_Evaluation to create declarations as needed to
21209 -- deal with side effects, and also create typ_FIRST/LAST
21210 -- entities for bounds if we have a subtype name.
21211
21212 -- Note: we do this transformation even if expansion is not
21213 -- active if we are in GNATprove_Mode since the transformation
21214 -- is in general required to ensure that the resulting tree has
21215 -- proper Ada semantics.
21216
21217 Force_Evaluation
21218 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21219 Force_Evaluation
21220 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21221 end if;
21222
21223 -- We use a flag here instead of suppressing checks on the type
21224 -- because the type we check against isn't necessarily the place
21225 -- where we put the check.
21226
21227 if not R_Check_Off then
21228 R_Checks := Get_Range_Checks (R, T);
21229
21230 -- Look up tree to find an appropriate insertion point. We
21231 -- can't just use insert_actions because later processing
21232 -- depends on the insertion node. Prior to Ada 2012 the
21233 -- insertion point could only be a declaration or a loop, but
21234 -- quantified expressions can appear within any context in an
21235 -- expression, and the insertion point can be any statement,
21236 -- pragma, or declaration.
21237
21238 Insert_Node := Parent (R);
21239 while Present (Insert_Node) loop
21240 exit when
21241 Nkind (Insert_Node) in N_Declaration
21242 and then
21243 not Nkind_In
21244 (Insert_Node, N_Component_Declaration,
21245 N_Loop_Parameter_Specification,
21246 N_Function_Specification,
21247 N_Procedure_Specification);
21248
21249 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21250 or else Nkind (Insert_Node) in
21251 N_Statement_Other_Than_Procedure_Call
21252 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21253 N_Pragma);
21254
21255 Insert_Node := Parent (Insert_Node);
21256 end loop;
21257
21258 -- Why would Type_Decl not be present??? Without this test,
21259 -- short regression tests fail.
21260
21261 if Present (Insert_Node) then
21262
21263 -- Case of loop statement. Verify that the range is part
21264 -- of the subtype indication of the iteration scheme.
21265
21266 if Nkind (Insert_Node) = N_Loop_Statement then
21267 declare
21268 Indic : Node_Id;
21269
21270 begin
21271 Indic := Parent (R);
21272 while Present (Indic)
21273 and then Nkind (Indic) /= N_Subtype_Indication
21274 loop
21275 Indic := Parent (Indic);
21276 end loop;
21277
21278 if Present (Indic) then
21279 Def_Id := Etype (Subtype_Mark (Indic));
21280
21281 Insert_Range_Checks
21282 (R_Checks,
21283 Insert_Node,
21284 Def_Id,
21285 Sloc (Insert_Node),
21286 Do_Before => True);
21287 end if;
21288 end;
21289
21290 -- Case of declarations. If the declaration is for a type
21291 -- and involves discriminants, the checks are premature at
21292 -- the declaration point and need to wait for the expansion
21293 -- of the initialization procedure, which will pass in the
21294 -- list to put them on; otherwise, the checks are done at
21295 -- the declaration point and there is no need to do them
21296 -- again in the initialization procedure.
21297
21298 elsif Nkind (Insert_Node) in N_Declaration then
21299 Def_Id := Defining_Identifier (Insert_Node);
21300
21301 if (Ekind (Def_Id) = E_Record_Type
21302 and then Depends_On_Discriminant (R))
21303 or else
21304 (Ekind (Def_Id) = E_Protected_Type
21305 and then Has_Discriminants (Def_Id))
21306 then
21307 if Present (Check_List) then
21308 Append_Range_Checks
21309 (R_Checks,
21310 Check_List, Def_Id, Sloc (Insert_Node));
21311 end if;
21312
21313 else
21314 if No (Check_List) then
21315 Insert_Range_Checks
21316 (R_Checks,
21317 Insert_Node, Def_Id, Sloc (Insert_Node));
21318 end if;
21319 end if;
21320
21321 -- Case of statements. Drop the checks, as the range appears
21322 -- in the context of a quantified expression. Insertion will
21323 -- take place when expression is expanded.
21324
21325 else
21326 null;
21327 end if;
21328 end if;
21329 end if;
21330 end if;
21331
21332 -- Case of other than an explicit N_Range node
21333
21334 -- The forced evaluation removes side effects from expressions, which
21335 -- should occur also in GNATprove mode. Otherwise, we end up with
21336 -- unexpected insertions of actions at places where this is not
21337 -- supposed to occur, e.g. on default parameters of a call.
21338
21339 elsif Expander_Active or GNATprove_Mode then
21340 Get_Index_Bounds (R, Lo, Hi);
21341 Force_Evaluation (Lo);
21342 Force_Evaluation (Hi);
21343 end if;
21344 end Process_Range_Expr_In_Decl;
21345
21346 --------------------------------------
21347 -- Process_Real_Range_Specification --
21348 --------------------------------------
21349
21350 procedure Process_Real_Range_Specification (Def : Node_Id) is
21351 Spec : constant Node_Id := Real_Range_Specification (Def);
21352 Lo : Node_Id;
21353 Hi : Node_Id;
21354 Err : Boolean := False;
21355
21356 procedure Analyze_Bound (N : Node_Id);
21357 -- Analyze and check one bound
21358
21359 -------------------
21360 -- Analyze_Bound --
21361 -------------------
21362
21363 procedure Analyze_Bound (N : Node_Id) is
21364 begin
21365 Analyze_And_Resolve (N, Any_Real);
21366
21367 if not Is_OK_Static_Expression (N) then
21368 Flag_Non_Static_Expr
21369 ("bound in real type definition is not static!", N);
21370 Err := True;
21371 end if;
21372 end Analyze_Bound;
21373
21374 -- Start of processing for Process_Real_Range_Specification
21375
21376 begin
21377 if Present (Spec) then
21378 Lo := Low_Bound (Spec);
21379 Hi := High_Bound (Spec);
21380 Analyze_Bound (Lo);
21381 Analyze_Bound (Hi);
21382
21383 -- If error, clear away junk range specification
21384
21385 if Err then
21386 Set_Real_Range_Specification (Def, Empty);
21387 end if;
21388 end if;
21389 end Process_Real_Range_Specification;
21390
21391 ---------------------
21392 -- Process_Subtype --
21393 ---------------------
21394
21395 function Process_Subtype
21396 (S : Node_Id;
21397 Related_Nod : Node_Id;
21398 Related_Id : Entity_Id := Empty;
21399 Suffix : Character := ' ') return Entity_Id
21400 is
21401 P : Node_Id;
21402 Def_Id : Entity_Id;
21403 Error_Node : Node_Id;
21404 Full_View_Id : Entity_Id;
21405 Subtype_Mark_Id : Entity_Id;
21406
21407 May_Have_Null_Exclusion : Boolean;
21408
21409 procedure Check_Incomplete (T : Node_Id);
21410 -- Called to verify that an incomplete type is not used prematurely
21411
21412 ----------------------
21413 -- Check_Incomplete --
21414 ----------------------
21415
21416 procedure Check_Incomplete (T : Node_Id) is
21417 begin
21418 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21419
21420 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21421 and then
21422 not (Ada_Version >= Ada_2005
21423 and then
21424 (Nkind (Parent (T)) = N_Subtype_Declaration
21425 or else (Nkind (Parent (T)) = N_Subtype_Indication
21426 and then Nkind (Parent (Parent (T))) =
21427 N_Subtype_Declaration)))
21428 then
21429 Error_Msg_N ("invalid use of type before its full declaration", T);
21430 end if;
21431 end Check_Incomplete;
21432
21433 -- Start of processing for Process_Subtype
21434
21435 begin
21436 -- Case of no constraints present
21437
21438 if Nkind (S) /= N_Subtype_Indication then
21439 Find_Type (S);
21440
21441 -- No way to proceed if the subtype indication is malformed. This
21442 -- will happen for example when the subtype indication in an object
21443 -- declaration is missing altogether and the expression is analyzed
21444 -- as if it were that indication.
21445
21446 if not Is_Entity_Name (S) then
21447 return Any_Type;
21448 end if;
21449
21450 Check_Incomplete (S);
21451 P := Parent (S);
21452
21453 -- Ada 2005 (AI-231): Static check
21454
21455 if Ada_Version >= Ada_2005
21456 and then Present (P)
21457 and then Null_Exclusion_Present (P)
21458 and then Nkind (P) /= N_Access_To_Object_Definition
21459 and then not Is_Access_Type (Entity (S))
21460 then
21461 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21462 end if;
21463
21464 -- The following is ugly, can't we have a range or even a flag???
21465
21466 May_Have_Null_Exclusion :=
21467 Nkind_In (P, N_Access_Definition,
21468 N_Access_Function_Definition,
21469 N_Access_Procedure_Definition,
21470 N_Access_To_Object_Definition,
21471 N_Allocator,
21472 N_Component_Definition)
21473 or else
21474 Nkind_In (P, N_Derived_Type_Definition,
21475 N_Discriminant_Specification,
21476 N_Formal_Object_Declaration,
21477 N_Object_Declaration,
21478 N_Object_Renaming_Declaration,
21479 N_Parameter_Specification,
21480 N_Subtype_Declaration);
21481
21482 -- Create an Itype that is a duplicate of Entity (S) but with the
21483 -- null-exclusion attribute.
21484
21485 if May_Have_Null_Exclusion
21486 and then Is_Access_Type (Entity (S))
21487 and then Null_Exclusion_Present (P)
21488
21489 -- No need to check the case of an access to object definition.
21490 -- It is correct to define double not-null pointers.
21491
21492 -- Example:
21493 -- type Not_Null_Int_Ptr is not null access Integer;
21494 -- type Acc is not null access Not_Null_Int_Ptr;
21495
21496 and then Nkind (P) /= N_Access_To_Object_Definition
21497 then
21498 if Can_Never_Be_Null (Entity (S)) then
21499 case Nkind (Related_Nod) is
21500 when N_Full_Type_Declaration =>
21501 if Nkind (Type_Definition (Related_Nod))
21502 in N_Array_Type_Definition
21503 then
21504 Error_Node :=
21505 Subtype_Indication
21506 (Component_Definition
21507 (Type_Definition (Related_Nod)));
21508 else
21509 Error_Node :=
21510 Subtype_Indication (Type_Definition (Related_Nod));
21511 end if;
21512
21513 when N_Subtype_Declaration =>
21514 Error_Node := Subtype_Indication (Related_Nod);
21515
21516 when N_Object_Declaration =>
21517 Error_Node := Object_Definition (Related_Nod);
21518
21519 when N_Component_Declaration =>
21520 Error_Node :=
21521 Subtype_Indication (Component_Definition (Related_Nod));
21522
21523 when N_Allocator =>
21524 Error_Node := Expression (Related_Nod);
21525
21526 when others =>
21527 pragma Assert (False);
21528 Error_Node := Related_Nod;
21529 end case;
21530
21531 Error_Msg_NE
21532 ("`NOT NULL` not allowed (& already excludes null)",
21533 Error_Node,
21534 Entity (S));
21535 end if;
21536
21537 Set_Etype (S,
21538 Create_Null_Excluding_Itype
21539 (T => Entity (S),
21540 Related_Nod => P));
21541 Set_Entity (S, Etype (S));
21542 end if;
21543
21544 return Entity (S);
21545
21546 -- Case of constraint present, so that we have an N_Subtype_Indication
21547 -- node (this node is created only if constraints are present).
21548
21549 else
21550 Find_Type (Subtype_Mark (S));
21551
21552 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21553 and then not
21554 (Nkind (Parent (S)) = N_Subtype_Declaration
21555 and then Is_Itype (Defining_Identifier (Parent (S))))
21556 then
21557 Check_Incomplete (Subtype_Mark (S));
21558 end if;
21559
21560 P := Parent (S);
21561 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21562
21563 -- Explicit subtype declaration case
21564
21565 if Nkind (P) = N_Subtype_Declaration then
21566 Def_Id := Defining_Identifier (P);
21567
21568 -- Explicit derived type definition case
21569
21570 elsif Nkind (P) = N_Derived_Type_Definition then
21571 Def_Id := Defining_Identifier (Parent (P));
21572
21573 -- Implicit case, the Def_Id must be created as an implicit type.
21574 -- The one exception arises in the case of concurrent types, array
21575 -- and access types, where other subsidiary implicit types may be
21576 -- created and must appear before the main implicit type. In these
21577 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21578 -- has not yet been called to create Def_Id.
21579
21580 else
21581 if Is_Array_Type (Subtype_Mark_Id)
21582 or else Is_Concurrent_Type (Subtype_Mark_Id)
21583 or else Is_Access_Type (Subtype_Mark_Id)
21584 then
21585 Def_Id := Empty;
21586
21587 -- For the other cases, we create a new unattached Itype,
21588 -- and set the indication to ensure it gets attached later.
21589
21590 else
21591 Def_Id :=
21592 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21593 end if;
21594 end if;
21595
21596 -- If the kind of constraint is invalid for this kind of type,
21597 -- then give an error, and then pretend no constraint was given.
21598
21599 if not Is_Valid_Constraint_Kind
21600 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21601 then
21602 Error_Msg_N
21603 ("incorrect constraint for this kind of type", Constraint (S));
21604
21605 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21606
21607 -- Set Ekind of orphan itype, to prevent cascaded errors
21608
21609 if Present (Def_Id) then
21610 Set_Ekind (Def_Id, Ekind (Any_Type));
21611 end if;
21612
21613 -- Make recursive call, having got rid of the bogus constraint
21614
21615 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21616 end if;
21617
21618 -- Remaining processing depends on type. Select on Base_Type kind to
21619 -- ensure getting to the concrete type kind in the case of a private
21620 -- subtype (needed when only doing semantic analysis).
21621
21622 case Ekind (Base_Type (Subtype_Mark_Id)) is
21623 when Access_Kind =>
21624
21625 -- If this is a constraint on a class-wide type, discard it.
21626 -- There is currently no way to express a partial discriminant
21627 -- constraint on a type with unknown discriminants. This is
21628 -- a pathology that the ACATS wisely decides not to test.
21629
21630 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21631 if Comes_From_Source (S) then
21632 Error_Msg_N
21633 ("constraint on class-wide type ignored??",
21634 Constraint (S));
21635 end if;
21636
21637 if Nkind (P) = N_Subtype_Declaration then
21638 Set_Subtype_Indication (P,
21639 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21640 end if;
21641
21642 return Subtype_Mark_Id;
21643 end if;
21644
21645 Constrain_Access (Def_Id, S, Related_Nod);
21646
21647 if Expander_Active
21648 and then Is_Itype (Designated_Type (Def_Id))
21649 and then Nkind (Related_Nod) = N_Subtype_Declaration
21650 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21651 then
21652 Build_Itype_Reference
21653 (Designated_Type (Def_Id), Related_Nod);
21654 end if;
21655
21656 when Array_Kind =>
21657 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21658
21659 when Decimal_Fixed_Point_Kind =>
21660 Constrain_Decimal (Def_Id, S);
21661
21662 when Enumeration_Kind =>
21663 Constrain_Enumeration (Def_Id, S);
21664
21665 when Ordinary_Fixed_Point_Kind =>
21666 Constrain_Ordinary_Fixed (Def_Id, S);
21667
21668 when Float_Kind =>
21669 Constrain_Float (Def_Id, S);
21670
21671 when Integer_Kind =>
21672 Constrain_Integer (Def_Id, S);
21673
21674 when Class_Wide_Kind
21675 | E_Incomplete_Type
21676 | E_Record_Subtype
21677 | E_Record_Type
21678 =>
21679 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21680
21681 if Ekind (Def_Id) = E_Incomplete_Type then
21682 Set_Private_Dependents (Def_Id, New_Elmt_List);
21683 end if;
21684
21685 when Private_Kind =>
21686
21687 -- A private type with unknown discriminants may be completed
21688 -- by an unconstrained array type.
21689
21690 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21691 and then Present (Full_View (Subtype_Mark_Id))
21692 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21693 then
21694 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21695
21696 -- ... but more commonly is completed by a discriminated record
21697 -- type.
21698
21699 else
21700 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21701 end if;
21702
21703 -- The base type may be private but Def_Id may be a full view
21704 -- in an instance.
21705
21706 if Is_Private_Type (Def_Id) then
21707 Set_Private_Dependents (Def_Id, New_Elmt_List);
21708 end if;
21709
21710 -- In case of an invalid constraint prevent further processing
21711 -- since the type constructed is missing expected fields.
21712
21713 if Etype (Def_Id) = Any_Type then
21714 return Def_Id;
21715 end if;
21716
21717 -- If the full view is that of a task with discriminants,
21718 -- we must constrain both the concurrent type and its
21719 -- corresponding record type. Otherwise we will just propagate
21720 -- the constraint to the full view, if available.
21721
21722 if Present (Full_View (Subtype_Mark_Id))
21723 and then Has_Discriminants (Subtype_Mark_Id)
21724 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21725 then
21726 Full_View_Id :=
21727 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21728
21729 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21730 Constrain_Concurrent (Full_View_Id, S,
21731 Related_Nod, Related_Id, Suffix);
21732 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21733 Set_Full_View (Def_Id, Full_View_Id);
21734
21735 -- Introduce an explicit reference to the private subtype,
21736 -- to prevent scope anomalies in gigi if first use appears
21737 -- in a nested context, e.g. a later function body.
21738 -- Should this be generated in other contexts than a full
21739 -- type declaration?
21740
21741 if Is_Itype (Def_Id)
21742 and then
21743 Nkind (Parent (P)) = N_Full_Type_Declaration
21744 then
21745 Build_Itype_Reference (Def_Id, Parent (P));
21746 end if;
21747
21748 else
21749 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21750 end if;
21751
21752 when Concurrent_Kind =>
21753 Constrain_Concurrent (Def_Id, S,
21754 Related_Nod, Related_Id, Suffix);
21755
21756 when others =>
21757 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21758 end case;
21759
21760 -- Size, Alignment, Representation aspects and Convention are always
21761 -- inherited from the base type.
21762
21763 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21764 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21765 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21766
21767 -- The anonymous subtype created for the subtype indication
21768 -- inherits the predicates of the parent.
21769
21770 if Has_Predicates (Subtype_Mark_Id) then
21771 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21772
21773 -- Indicate where the predicate function may be found
21774
21775 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21776 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21777 end if;
21778 end if;
21779
21780 return Def_Id;
21781 end if;
21782 end Process_Subtype;
21783
21784 -----------------------------
21785 -- Record_Type_Declaration --
21786 -----------------------------
21787
21788 procedure Record_Type_Declaration
21789 (T : Entity_Id;
21790 N : Node_Id;
21791 Prev : Entity_Id)
21792 is
21793 Def : constant Node_Id := Type_Definition (N);
21794 Is_Tagged : Boolean;
21795 Tag_Comp : Entity_Id;
21796
21797 begin
21798 -- These flags must be initialized before calling Process_Discriminants
21799 -- because this routine makes use of them.
21800
21801 Set_Ekind (T, E_Record_Type);
21802 Set_Etype (T, T);
21803 Init_Size_Align (T);
21804 Set_Interfaces (T, No_Elist);
21805 Set_Stored_Constraint (T, No_Elist);
21806 Set_Default_SSO (T);
21807 Set_No_Reordering (T, No_Component_Reordering);
21808
21809 -- Normal case
21810
21811 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21812 -- The flag Is_Tagged_Type might have already been set by
21813 -- Find_Type_Name if it detected an error for declaration T. This
21814 -- arises in the case of private tagged types where the full view
21815 -- omits the word tagged.
21816
21817 Is_Tagged :=
21818 Tagged_Present (Def)
21819 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21820
21821 Set_Is_Limited_Record (T, Limited_Present (Def));
21822
21823 if Is_Tagged then
21824 Set_Is_Tagged_Type (T, True);
21825 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21826 end if;
21827
21828 -- Type is abstract if full declaration carries keyword, or if
21829 -- previous partial view did.
21830
21831 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21832 or else Abstract_Present (Def));
21833
21834 else
21835 Is_Tagged := True;
21836 Analyze_Interface_Declaration (T, Def);
21837
21838 if Present (Discriminant_Specifications (N)) then
21839 Error_Msg_N
21840 ("interface types cannot have discriminants",
21841 Defining_Identifier
21842 (First (Discriminant_Specifications (N))));
21843 end if;
21844 end if;
21845
21846 -- First pass: if there are self-referential access components,
21847 -- create the required anonymous access type declarations, and if
21848 -- need be an incomplete type declaration for T itself.
21849
21850 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21851
21852 if Ada_Version >= Ada_2005
21853 and then Present (Interface_List (Def))
21854 then
21855 Check_Interfaces (N, Def);
21856
21857 declare
21858 Ifaces_List : Elist_Id;
21859
21860 begin
21861 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21862 -- already in the parents.
21863
21864 Collect_Interfaces
21865 (T => T,
21866 Ifaces_List => Ifaces_List,
21867 Exclude_Parents => True);
21868
21869 Set_Interfaces (T, Ifaces_List);
21870 end;
21871 end if;
21872
21873 -- Records constitute a scope for the component declarations within.
21874 -- The scope is created prior to the processing of these declarations.
21875 -- Discriminants are processed first, so that they are visible when
21876 -- processing the other components. The Ekind of the record type itself
21877 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21878
21879 -- Enter record scope
21880
21881 Push_Scope (T);
21882
21883 -- If an incomplete or private type declaration was already given for
21884 -- the type, then this scope already exists, and the discriminants have
21885 -- been declared within. We must verify that the full declaration
21886 -- matches the incomplete one.
21887
21888 Check_Or_Process_Discriminants (N, T, Prev);
21889
21890 Set_Is_Constrained (T, not Has_Discriminants (T));
21891 Set_Has_Delayed_Freeze (T, True);
21892
21893 -- For tagged types add a manually analyzed component corresponding
21894 -- to the component _tag, the corresponding piece of tree will be
21895 -- expanded as part of the freezing actions if it is not a CPP_Class.
21896
21897 if Is_Tagged then
21898
21899 -- Do not add the tag unless we are in expansion mode
21900
21901 if Expander_Active then
21902 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21903 Enter_Name (Tag_Comp);
21904
21905 Set_Ekind (Tag_Comp, E_Component);
21906 Set_Is_Tag (Tag_Comp);
21907 Set_Is_Aliased (Tag_Comp);
21908 Set_Is_Independent (Tag_Comp);
21909 Set_Etype (Tag_Comp, RTE (RE_Tag));
21910 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21911 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21912 Init_Component_Location (Tag_Comp);
21913
21914 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21915 -- implemented interfaces.
21916
21917 if Has_Interfaces (T) then
21918 Add_Interface_Tag_Components (N, T);
21919 end if;
21920 end if;
21921
21922 Make_Class_Wide_Type (T);
21923 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21924 end if;
21925
21926 -- We must suppress range checks when processing record components in
21927 -- the presence of discriminants, since we don't want spurious checks to
21928 -- be generated during their analysis, but Suppress_Range_Checks flags
21929 -- must be reset the after processing the record definition.
21930
21931 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21932 -- couldn't we just use the normal range check suppression method here.
21933 -- That would seem cleaner ???
21934
21935 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21936 Set_Kill_Range_Checks (T, True);
21937 Record_Type_Definition (Def, Prev);
21938 Set_Kill_Range_Checks (T, False);
21939 else
21940 Record_Type_Definition (Def, Prev);
21941 end if;
21942
21943 -- Exit from record scope
21944
21945 End_Scope;
21946
21947 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21948 -- the implemented interfaces and associate them an aliased entity.
21949
21950 if Is_Tagged
21951 and then not Is_Empty_List (Interface_List (Def))
21952 then
21953 Derive_Progenitor_Subprograms (T, T);
21954 end if;
21955
21956 Check_Function_Writable_Actuals (N);
21957 end Record_Type_Declaration;
21958
21959 ----------------------------
21960 -- Record_Type_Definition --
21961 ----------------------------
21962
21963 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21964 Component : Entity_Id;
21965 Ctrl_Components : Boolean := False;
21966 Final_Storage_Only : Boolean;
21967 T : Entity_Id;
21968
21969 begin
21970 if Ekind (Prev_T) = E_Incomplete_Type then
21971 T := Full_View (Prev_T);
21972 else
21973 T := Prev_T;
21974 end if;
21975
21976 Final_Storage_Only := not Is_Controlled (T);
21977
21978 -- Ada 2005: Check whether an explicit Limited is present in a derived
21979 -- type declaration.
21980
21981 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21982 and then Limited_Present (Parent (Def))
21983 then
21984 Set_Is_Limited_Record (T);
21985 end if;
21986
21987 -- If the component list of a record type is defined by the reserved
21988 -- word null and there is no discriminant part, then the record type has
21989 -- no components and all records of the type are null records (RM 3.7)
21990 -- This procedure is also called to process the extension part of a
21991 -- record extension, in which case the current scope may have inherited
21992 -- components.
21993
21994 if Present (Def)
21995 and then Present (Component_List (Def))
21996 and then not Null_Present (Component_List (Def))
21997 then
21998 Analyze_Declarations (Component_Items (Component_List (Def)));
21999
22000 if Present (Variant_Part (Component_List (Def))) then
22001 Analyze (Variant_Part (Component_List (Def)));
22002 end if;
22003 end if;
22004
22005 -- After completing the semantic analysis of the record definition,
22006 -- record components, both new and inherited, are accessible. Set their
22007 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22008 -- whose Ekind may be void.
22009
22010 Component := First_Entity (Current_Scope);
22011 while Present (Component) loop
22012 if Ekind (Component) = E_Void
22013 and then not Is_Itype (Component)
22014 then
22015 Set_Ekind (Component, E_Component);
22016 Init_Component_Location (Component);
22017 end if;
22018
22019 Propagate_Concurrent_Flags (T, Etype (Component));
22020
22021 if Ekind (Component) /= E_Component then
22022 null;
22023
22024 -- Do not set Has_Controlled_Component on a class-wide equivalent
22025 -- type. See Make_CW_Equivalent_Type.
22026
22027 elsif not Is_Class_Wide_Equivalent_Type (T)
22028 and then (Has_Controlled_Component (Etype (Component))
22029 or else (Chars (Component) /= Name_uParent
22030 and then Is_Controlled (Etype (Component))))
22031 then
22032 Set_Has_Controlled_Component (T, True);
22033 Final_Storage_Only :=
22034 Final_Storage_Only
22035 and then Finalize_Storage_Only (Etype (Component));
22036 Ctrl_Components := True;
22037 end if;
22038
22039 Next_Entity (Component);
22040 end loop;
22041
22042 -- A Type is Finalize_Storage_Only only if all its controlled components
22043 -- are also.
22044
22045 if Ctrl_Components then
22046 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22047 end if;
22048
22049 -- Place reference to end record on the proper entity, which may
22050 -- be a partial view.
22051
22052 if Present (Def) then
22053 Process_End_Label (Def, 'e', Prev_T);
22054 end if;
22055 end Record_Type_Definition;
22056
22057 ---------------------------
22058 -- Replace_Discriminants --
22059 ---------------------------
22060
22061 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22062 function Process (N : Node_Id) return Traverse_Result;
22063
22064 -------------
22065 -- Process --
22066 -------------
22067
22068 function Process (N : Node_Id) return Traverse_Result is
22069 Comp : Entity_Id;
22070
22071 begin
22072 if Nkind (N) = N_Discriminant_Specification then
22073 Comp := First_Discriminant (Typ);
22074 while Present (Comp) loop
22075 if Original_Record_Component (Comp) = Defining_Identifier (N)
22076 or else Chars (Comp) = Chars (Defining_Identifier (N))
22077 then
22078 Set_Defining_Identifier (N, Comp);
22079 exit;
22080 end if;
22081
22082 Next_Discriminant (Comp);
22083 end loop;
22084
22085 elsif Nkind (N) = N_Variant_Part then
22086 Comp := First_Discriminant (Typ);
22087 while Present (Comp) loop
22088 if Original_Record_Component (Comp) = Entity (Name (N))
22089 or else Chars (Comp) = Chars (Name (N))
22090 then
22091 -- Make sure to preserve the type coming from the parent on
22092 -- the Name, even if the subtype of the discriminant can be
22093 -- constrained, so that discrete choices inherited from the
22094 -- parent in the variant part are not flagged as violating
22095 -- the constraints of the subtype.
22096
22097 declare
22098 Typ : constant Entity_Id := Etype (Name (N));
22099 begin
22100 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22101 Set_Etype (Name (N), Typ);
22102 end;
22103 exit;
22104 end if;
22105
22106 Next_Discriminant (Comp);
22107 end loop;
22108 end if;
22109
22110 return OK;
22111 end Process;
22112
22113 procedure Replace is new Traverse_Proc (Process);
22114
22115 -- Start of processing for Replace_Discriminants
22116
22117 begin
22118 Replace (Decl);
22119 end Replace_Discriminants;
22120
22121 -------------------------------
22122 -- Set_Completion_Referenced --
22123 -------------------------------
22124
22125 procedure Set_Completion_Referenced (E : Entity_Id) is
22126 begin
22127 -- If in main unit, mark entity that is a completion as referenced,
22128 -- warnings go on the partial view when needed.
22129
22130 if In_Extended_Main_Source_Unit (E) then
22131 Set_Referenced (E);
22132 end if;
22133 end Set_Completion_Referenced;
22134
22135 ---------------------
22136 -- Set_Default_SSO --
22137 ---------------------
22138
22139 procedure Set_Default_SSO (T : Entity_Id) is
22140 begin
22141 case Opt.Default_SSO is
22142 when ' ' =>
22143 null;
22144 when 'L' =>
22145 Set_SSO_Set_Low_By_Default (T, True);
22146 when 'H' =>
22147 Set_SSO_Set_High_By_Default (T, True);
22148 when others =>
22149 raise Program_Error;
22150 end case;
22151 end Set_Default_SSO;
22152
22153 ---------------------
22154 -- Set_Fixed_Range --
22155 ---------------------
22156
22157 -- The range for fixed-point types is complicated by the fact that we
22158 -- do not know the exact end points at the time of the declaration. This
22159 -- is true for three reasons:
22160
22161 -- A size clause may affect the fudging of the end-points.
22162 -- A small clause may affect the values of the end-points.
22163 -- We try to include the end-points if it does not affect the size.
22164
22165 -- This means that the actual end-points must be established at the
22166 -- point when the type is frozen. Meanwhile, we first narrow the range
22167 -- as permitted (so that it will fit if necessary in a small specified
22168 -- size), and then build a range subtree with these narrowed bounds.
22169 -- Set_Fixed_Range constructs the range from real literal values, and
22170 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22171
22172 -- The parent of this range is set to point to the entity so that it is
22173 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22174 -- other scalar types, which are just pointers to the range in the
22175 -- original tree, this would otherwise be an orphan).
22176
22177 -- The tree is left unanalyzed. When the type is frozen, the processing
22178 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22179 -- analyzed, and uses this as an indication that it should complete
22180 -- work on the range (it will know the final small and size values).
22181
22182 procedure Set_Fixed_Range
22183 (E : Entity_Id;
22184 Loc : Source_Ptr;
22185 Lo : Ureal;
22186 Hi : Ureal)
22187 is
22188 S : constant Node_Id :=
22189 Make_Range (Loc,
22190 Low_Bound => Make_Real_Literal (Loc, Lo),
22191 High_Bound => Make_Real_Literal (Loc, Hi));
22192 begin
22193 Set_Scalar_Range (E, S);
22194 Set_Parent (S, E);
22195
22196 -- Before the freeze point, the bounds of a fixed point are universal
22197 -- and carry the corresponding type.
22198
22199 Set_Etype (Low_Bound (S), Universal_Real);
22200 Set_Etype (High_Bound (S), Universal_Real);
22201 end Set_Fixed_Range;
22202
22203 ----------------------------------
22204 -- Set_Scalar_Range_For_Subtype --
22205 ----------------------------------
22206
22207 procedure Set_Scalar_Range_For_Subtype
22208 (Def_Id : Entity_Id;
22209 R : Node_Id;
22210 Subt : Entity_Id)
22211 is
22212 Kind : constant Entity_Kind := Ekind (Def_Id);
22213
22214 begin
22215 -- Defend against previous error
22216
22217 if Nkind (R) = N_Error then
22218 return;
22219 end if;
22220
22221 Set_Scalar_Range (Def_Id, R);
22222
22223 -- We need to link the range into the tree before resolving it so
22224 -- that types that are referenced, including importantly the subtype
22225 -- itself, are properly frozen (Freeze_Expression requires that the
22226 -- expression be properly linked into the tree). Of course if it is
22227 -- already linked in, then we do not disturb the current link.
22228
22229 if No (Parent (R)) then
22230 Set_Parent (R, Def_Id);
22231 end if;
22232
22233 -- Reset the kind of the subtype during analysis of the range, to
22234 -- catch possible premature use in the bounds themselves.
22235
22236 Set_Ekind (Def_Id, E_Void);
22237 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22238 Set_Ekind (Def_Id, Kind);
22239 end Set_Scalar_Range_For_Subtype;
22240
22241 --------------------------------------------------------
22242 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22243 --------------------------------------------------------
22244
22245 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22246 (E : Entity_Id)
22247 is
22248 begin
22249 -- Make sure set if encountered during Expand_To_Stored_Constraint
22250
22251 Set_Stored_Constraint (E, No_Elist);
22252
22253 -- Give it the right value
22254
22255 if Is_Constrained (E) and then Has_Discriminants (E) then
22256 Set_Stored_Constraint (E,
22257 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22258 end if;
22259 end Set_Stored_Constraint_From_Discriminant_Constraint;
22260
22261 -------------------------------------
22262 -- Signed_Integer_Type_Declaration --
22263 -------------------------------------
22264
22265 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22266 Implicit_Base : Entity_Id;
22267 Base_Typ : Entity_Id;
22268 Lo_Val : Uint;
22269 Hi_Val : Uint;
22270 Errs : Boolean := False;
22271 Lo : Node_Id;
22272 Hi : Node_Id;
22273
22274 function Can_Derive_From (E : Entity_Id) return Boolean;
22275 -- Determine whether given bounds allow derivation from specified type
22276
22277 procedure Check_Bound (Expr : Node_Id);
22278 -- Check bound to make sure it is integral and static. If not, post
22279 -- appropriate error message and set Errs flag
22280
22281 ---------------------
22282 -- Can_Derive_From --
22283 ---------------------
22284
22285 -- Note we check both bounds against both end values, to deal with
22286 -- strange types like ones with a range of 0 .. -12341234.
22287
22288 function Can_Derive_From (E : Entity_Id) return Boolean is
22289 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22290 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22291 begin
22292 return Lo <= Lo_Val and then Lo_Val <= Hi
22293 and then
22294 Lo <= Hi_Val and then Hi_Val <= Hi;
22295 end Can_Derive_From;
22296
22297 -----------------
22298 -- Check_Bound --
22299 -----------------
22300
22301 procedure Check_Bound (Expr : Node_Id) is
22302 begin
22303 -- If a range constraint is used as an integer type definition, each
22304 -- bound of the range must be defined by a static expression of some
22305 -- integer type, but the two bounds need not have the same integer
22306 -- type (Negative bounds are allowed.) (RM 3.5.4)
22307
22308 if not Is_Integer_Type (Etype (Expr)) then
22309 Error_Msg_N
22310 ("integer type definition bounds must be of integer type", Expr);
22311 Errs := True;
22312
22313 elsif not Is_OK_Static_Expression (Expr) then
22314 Flag_Non_Static_Expr
22315 ("non-static expression used for integer type bound!", Expr);
22316 Errs := True;
22317
22318 -- Otherwise the bounds are folded into literals
22319
22320 elsif Is_Entity_Name (Expr) then
22321 Fold_Uint (Expr, Expr_Value (Expr), True);
22322 end if;
22323 end Check_Bound;
22324
22325 -- Start of processing for Signed_Integer_Type_Declaration
22326
22327 begin
22328 -- Create an anonymous base type
22329
22330 Implicit_Base :=
22331 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22332
22333 -- Analyze and check the bounds, they can be of any integer type
22334
22335 Lo := Low_Bound (Def);
22336 Hi := High_Bound (Def);
22337
22338 -- Arbitrarily use Integer as the type if either bound had an error
22339
22340 if Hi = Error or else Lo = Error then
22341 Base_Typ := Any_Integer;
22342 Set_Error_Posted (T, True);
22343 Errs := True;
22344
22345 -- Here both bounds are OK expressions
22346
22347 else
22348 Analyze_And_Resolve (Lo, Any_Integer);
22349 Analyze_And_Resolve (Hi, Any_Integer);
22350
22351 Check_Bound (Lo);
22352 Check_Bound (Hi);
22353
22354 if Errs then
22355 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22356 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22357 end if;
22358
22359 -- Find type to derive from
22360
22361 Lo_Val := Expr_Value (Lo);
22362 Hi_Val := Expr_Value (Hi);
22363
22364 if Can_Derive_From (Standard_Short_Short_Integer) then
22365 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22366
22367 elsif Can_Derive_From (Standard_Short_Integer) then
22368 Base_Typ := Base_Type (Standard_Short_Integer);
22369
22370 elsif Can_Derive_From (Standard_Integer) then
22371 Base_Typ := Base_Type (Standard_Integer);
22372
22373 elsif Can_Derive_From (Standard_Long_Integer) then
22374 Base_Typ := Base_Type (Standard_Long_Integer);
22375
22376 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22377 Check_Restriction (No_Long_Long_Integers, Def);
22378 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22379
22380 else
22381 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22382 Error_Msg_N ("integer type definition bounds out of range", Def);
22383 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22384 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22385 end if;
22386 end if;
22387
22388 -- Set the type of the bounds to the implicit base: we cannot set it to
22389 -- the new type, because this would be a forward reference for the code
22390 -- generator and, if the original type is user-defined, this could even
22391 -- lead to spurious semantic errors. Furthermore we do not set it to be
22392 -- universal, because this could make it much larger than needed here.
22393
22394 if not Errs then
22395 Set_Etype (Lo, Implicit_Base);
22396 Set_Etype (Hi, Implicit_Base);
22397 end if;
22398
22399 -- Complete both implicit base and declared first subtype entities. The
22400 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22401 -- are not clobbered when the signed integer type acts as a full view of
22402 -- a private type.
22403
22404 Set_Etype (Implicit_Base, Base_Typ);
22405 Set_Size_Info (Implicit_Base, Base_Typ);
22406 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22407 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22408 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22409
22410 Set_Ekind (T, E_Signed_Integer_Subtype);
22411 Set_Etype (T, Implicit_Base);
22412 Set_Size_Info (T, Implicit_Base);
22413 Inherit_Rep_Item_Chain (T, Implicit_Base);
22414 Set_Scalar_Range (T, Def);
22415 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22416 Set_Is_Constrained (T);
22417 end Signed_Integer_Type_Declaration;
22418
22419 end Sem_Ch3;
This page took 0.956337 seconds and 4 git commands to generate.