]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch3.adb
[Ada] Skip unnecessary iterations over constraint expressions
[thirdparty/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_Derived_Type
96 (N : Node_Id;
97 Parent_Type : Entity_Id;
98 Derived_Type : Entity_Id;
99 Is_Completion : Boolean;
100 Derive_Subps : Boolean := True);
101 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
102 -- the N_Full_Type_Declaration node containing the derived type definition.
103 -- Parent_Type is the entity for the parent type in the derived type
104 -- definition and Derived_Type the actual derived type. Is_Completion must
105 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
106 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
107 -- completion of a private type declaration. If Is_Completion is set to
108 -- True, N is the completion of a private type declaration and Derived_Type
109 -- is different from the defining identifier inside N (i.e. Derived_Type /=
110 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
111 -- subprograms should be derived. The only case where this parameter is
112 -- False is when Build_Derived_Type is recursively called to process an
113 -- implicit derived full type for a type derived from a private type (in
114 -- that case the subprograms must only be derived for the private view of
115 -- the type).
116 --
117 -- ??? These flags need a bit of re-examination and re-documentation:
118 -- ??? are they both necessary (both seem related to the recursion)?
119
120 procedure Build_Derived_Access_Type
121 (N : Node_Id;
122 Parent_Type : Entity_Id;
123 Derived_Type : Entity_Id);
124 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
125 -- create an implicit base if the parent type is constrained or if the
126 -- subtype indication has a constraint.
127
128 procedure Build_Derived_Array_Type
129 (N : Node_Id;
130 Parent_Type : Entity_Id;
131 Derived_Type : Entity_Id);
132 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
133 -- create an implicit base if the parent type is constrained or if the
134 -- subtype indication has a constraint.
135
136 procedure Build_Derived_Concurrent_Type
137 (N : Node_Id;
138 Parent_Type : Entity_Id;
139 Derived_Type : Entity_Id);
140 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
141 -- protected type, inherit entries and protected subprograms, check
142 -- legality of discriminant constraints if any.
143
144 procedure Build_Derived_Enumeration_Type
145 (N : Node_Id;
146 Parent_Type : Entity_Id;
147 Derived_Type : Entity_Id);
148 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
149 -- type, we must create a new list of literals. Types derived from
150 -- Character and [Wide_]Wide_Character are special-cased.
151
152 procedure Build_Derived_Numeric_Type
153 (N : Node_Id;
154 Parent_Type : Entity_Id;
155 Derived_Type : Entity_Id);
156 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
157 -- an anonymous base type, and propagate constraint to subtype if needed.
158
159 procedure Build_Derived_Private_Type
160 (N : Node_Id;
161 Parent_Type : Entity_Id;
162 Derived_Type : Entity_Id;
163 Is_Completion : Boolean;
164 Derive_Subps : Boolean := True);
165 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
166 -- because the parent may or may not have a completion, and the derivation
167 -- may itself be a completion.
168
169 procedure Build_Derived_Record_Type
170 (N : Node_Id;
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure used for tagged and untagged record types
175 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
176 -- All parameters are as in Build_Derived_Type except that N, in
177 -- addition to being an N_Full_Type_Declaration node, can also be an
178 -- N_Private_Extension_Declaration node. See the definition of this routine
179 -- for much more info. Derive_Subps indicates whether subprograms should be
180 -- derived from the parent type. The only case where Derive_Subps is False
181 -- is for an implicit derived full type for a type derived from a private
182 -- type (see Build_Derived_Type).
183
184 procedure Build_Discriminal (Discrim : Entity_Id);
185 -- Create the discriminal corresponding to discriminant Discrim, that is
186 -- the parameter corresponding to Discrim to be used in initialization
187 -- procedures for the type where Discrim is a discriminant. Discriminals
188 -- are not used during semantic analysis, and are not fully defined
189 -- entities until expansion. Thus they are not given a scope until
190 -- initialization procedures are built.
191
192 function Build_Discriminant_Constraints
193 (T : Entity_Id;
194 Def : Node_Id;
195 Derived_Def : Boolean := False) return Elist_Id;
196 -- Validate discriminant constraints and return the list of the constraints
197 -- in order of discriminant declarations, where T is the discriminated
198 -- unconstrained type. Def is the N_Subtype_Indication node where the
199 -- discriminants constraints for T are specified. Derived_Def is True
200 -- when building the discriminant constraints in a derived type definition
201 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
202 -- type and Def is the constraint "(xxx)" on T and this routine sets the
203 -- Corresponding_Discriminant field of the discriminants in the derived
204 -- type D to point to the corresponding discriminants in the parent type T.
205
206 procedure Build_Discriminated_Subtype
207 (T : Entity_Id;
208 Def_Id : Entity_Id;
209 Elist : Elist_Id;
210 Related_Nod : Node_Id;
211 For_Access : Boolean := False);
212 -- Subsidiary procedure to Constrain_Discriminated_Type and to
213 -- Process_Incomplete_Dependents. Given
214 --
215 -- T (a possibly discriminated base type)
216 -- Def_Id (a very partially built subtype for T),
217 --
218 -- the call completes Def_Id to be the appropriate E_*_Subtype.
219 --
220 -- The Elist is the list of discriminant constraints if any (it is set
221 -- to No_Elist if T is not a discriminated type, and to an empty list if
222 -- T has discriminants but there are no discriminant constraints). The
223 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
224 -- The For_Access says whether or not this subtype is really constraining
225 -- an access type.
226
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
235
236 procedure Check_Access_Discriminant_Requires_Limited
237 (D : Node_Id;
238 Loc : Node_Id);
239 -- Check the restriction that the type to which an access discriminant
240 -- belongs must be a concurrent type or a descendant of a type with
241 -- the reserved word 'limited' in its declaration.
242
243 procedure Check_Anonymous_Access_Components
244 (Typ_Decl : Node_Id;
245 Typ : Entity_Id;
246 Prev : Entity_Id;
247 Comp_List : Node_Id);
248 -- Ada 2005 AI-382: an access component in a record definition can refer to
249 -- the enclosing record, in which case it denotes the type itself, and not
250 -- the current instance of the type. We create an anonymous access type for
251 -- the component, and flag it as an access to a component, so accessibility
252 -- checks are properly performed on it. The declaration of the access type
253 -- is placed ahead of that of the record to prevent order-of-elaboration
254 -- circularity issues in Gigi. We create an incomplete type for the record
255 -- declaration, which is the designated type of the anonymous access.
256
257 procedure Check_Delta_Expression (E : Node_Id);
258 -- Check that the expression represented by E is suitable for use as a
259 -- delta expression, i.e. it is of real type and is static.
260
261 procedure Check_Digits_Expression (E : Node_Id);
262 -- Check that the expression represented by E is suitable for use as a
263 -- digits expression, i.e. it is of integer type, positive and static.
264
265 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
266 -- Validate the initialization of an object declaration. T is the required
267 -- type, and Exp is the initialization expression.
268
269 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
270 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
271
272 procedure Check_Or_Process_Discriminants
273 (N : Node_Id;
274 T : Entity_Id;
275 Prev : Entity_Id := Empty);
276 -- If N is the full declaration of the completion T of an incomplete or
277 -- private type, check its discriminants (which are already known to be
278 -- conformant with those of the partial view, see Find_Type_Name),
279 -- otherwise process them. Prev is the entity of the partial declaration,
280 -- if any.
281
282 procedure Check_Real_Bound (Bound : Node_Id);
283 -- Check given bound for being of real type and static. If not, post an
284 -- appropriate message, and rewrite the bound with the real literal zero.
285
286 procedure Constant_Redeclaration
287 (Id : Entity_Id;
288 N : Node_Id;
289 T : out Entity_Id);
290 -- Various checks on legality of full declaration of deferred constant.
291 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
292 -- node. The caller has not yet set any attributes of this entity.
293
294 function Contain_Interface
295 (Iface : Entity_Id;
296 Ifaces : Elist_Id) return Boolean;
297 -- Ada 2005: Determine whether Iface is present in the list Ifaces
298
299 procedure Convert_Scalar_Bounds
300 (N : Node_Id;
301 Parent_Type : Entity_Id;
302 Derived_Type : Entity_Id;
303 Loc : Source_Ptr);
304 -- For derived scalar types, convert the bounds in the type definition to
305 -- the derived type, and complete their analysis. Given a constraint of the
306 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
307 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
308 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
309 -- subtype are conversions of those bounds to the derived_type, so that
310 -- their typing is consistent.
311
312 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
313 -- Copies attributes from array base type T2 to array base type T1. Copies
314 -- only attributes that apply to base types, but not subtypes.
315
316 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
317 -- Copies attributes from array subtype T2 to array subtype T1. Copies
318 -- attributes that apply to both subtypes and base types.
319
320 procedure Create_Constrained_Components
321 (Subt : Entity_Id;
322 Decl_Node : Node_Id;
323 Typ : Entity_Id;
324 Constraints : Elist_Id);
325 -- Build the list of entities for a constrained discriminated record
326 -- subtype. If a component depends on a discriminant, replace its subtype
327 -- using the discriminant values in the discriminant constraint. Subt
328 -- is the defining identifier for the subtype whose list of constrained
329 -- entities we will create. Decl_Node is the type declaration node where
330 -- we will attach all the itypes created. Typ is the base discriminated
331 -- type for the subtype Subt. Constraints is the list of discriminant
332 -- constraints for Typ.
333
334 function Constrain_Component_Type
335 (Comp : Entity_Id;
336 Constrained_Typ : Entity_Id;
337 Related_Node : Node_Id;
338 Typ : Entity_Id;
339 Constraints : Elist_Id) return Entity_Id;
340 -- Given a discriminated base type Typ, a list of discriminant constraints,
341 -- Constraints, for Typ and a component Comp of Typ, create and return the
342 -- type corresponding to Etype (Comp) where all discriminant references
343 -- are replaced with the corresponding constraint. If Etype (Comp) contains
344 -- no discriminant references then it is returned as-is. Constrained_Typ
345 -- is the final constrained subtype to which the constrained component
346 -- belongs. Related_Node is the node where we attach all created itypes.
347
348 procedure Constrain_Access
349 (Def_Id : in out Entity_Id;
350 S : Node_Id;
351 Related_Nod : Node_Id);
352 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
353 -- an anonymous type created for a subtype indication. In that case it is
354 -- created in the procedure and attached to Related_Nod.
355
356 procedure Constrain_Array
357 (Def_Id : in out Entity_Id;
358 SI : Node_Id;
359 Related_Nod : Node_Id;
360 Related_Id : Entity_Id;
361 Suffix : Character);
362 -- Apply a list of index constraints to an unconstrained array type. The
363 -- first parameter is the entity for the resulting subtype. A value of
364 -- Empty for Def_Id indicates that an implicit type must be created, but
365 -- creation is delayed (and must be done by this procedure) because other
366 -- subsidiary implicit types must be created first (which is why Def_Id
367 -- is an in/out parameter). The second parameter is a subtype indication
368 -- node for the constrained array to be created (e.g. something of the
369 -- form string (1 .. 10)). Related_Nod gives the place where this type
370 -- has to be inserted in the tree. The Related_Id and Suffix parameters
371 -- are used to build the associated Implicit type name.
372
373 procedure Constrain_Concurrent
374 (Def_Id : in out Entity_Id;
375 SI : Node_Id;
376 Related_Nod : Node_Id;
377 Related_Id : Entity_Id;
378 Suffix : Character);
379 -- Apply list of discriminant constraints to an unconstrained concurrent
380 -- type.
381 --
382 -- SI is the N_Subtype_Indication node containing the constraint and
383 -- the unconstrained type to constrain.
384 --
385 -- Def_Id is the entity for the resulting constrained subtype. A value
386 -- of Empty for Def_Id indicates that an implicit type must be created,
387 -- but creation is delayed (and must be done by this procedure) because
388 -- other subsidiary implicit types must be created first (which is why
389 -- Def_Id is an in/out parameter).
390 --
391 -- Related_Nod gives the place where this type has to be inserted
392 -- in the tree.
393 --
394 -- The last two arguments are used to create its external name if needed.
395
396 function Constrain_Corresponding_Record
397 (Prot_Subt : Entity_Id;
398 Corr_Rec : Entity_Id;
399 Related_Nod : Node_Id) return Entity_Id;
400 -- When constraining a protected type or task type with discriminants,
401 -- constrain the corresponding record with the same discriminant values.
402
403 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
404 -- Constrain a decimal fixed point type with a digits constraint and/or a
405 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
406
407 procedure Constrain_Discriminated_Type
408 (Def_Id : Entity_Id;
409 S : Node_Id;
410 Related_Nod : Node_Id;
411 For_Access : Boolean := False);
412 -- Process discriminant constraints of composite type. Verify that values
413 -- have been provided for all discriminants, that the original type is
414 -- unconstrained, and that the types of the supplied expressions match
415 -- the discriminant types. The first three parameters are like in routine
416 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
417 -- of For_Access.
418
419 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
420 -- Constrain an enumeration type with a range constraint. This is identical
421 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
422
423 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
424 -- Constrain a floating point type with either a digits constraint
425 -- and/or a range constraint, building a E_Floating_Point_Subtype.
426
427 procedure Constrain_Index
428 (Index : Node_Id;
429 S : Node_Id;
430 Related_Nod : Node_Id;
431 Related_Id : Entity_Id;
432 Suffix : Character;
433 Suffix_Index : Nat);
434 -- Process an index constraint S in a constrained array declaration. The
435 -- constraint can be a subtype name, or a range with or without an explicit
436 -- subtype mark. The index is the corresponding index of the unconstrained
437 -- array. The Related_Id and Suffix parameters are used to build the
438 -- associated Implicit type name.
439
440 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
441 -- Build subtype of a signed or modular integer type
442
443 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
444 -- Constrain an ordinary fixed point type with a range constraint, and
445 -- build an E_Ordinary_Fixed_Point_Subtype entity.
446
447 procedure Copy_And_Swap (Priv, Full : Entity_Id);
448 -- Copy the Priv entity into the entity of its full declaration then swap
449 -- the two entities in such a manner that the former private type is now
450 -- seen as a full type.
451
452 procedure Decimal_Fixed_Point_Type_Declaration
453 (T : Entity_Id;
454 Def : Node_Id);
455 -- Create a new decimal fixed point type, and apply the constraint to
456 -- obtain a subtype of this new type.
457
458 procedure Complete_Private_Subtype
459 (Priv : Entity_Id;
460 Full : Entity_Id;
461 Full_Base : Entity_Id;
462 Related_Nod : Node_Id);
463 -- Complete the implicit full view of a private subtype by setting the
464 -- appropriate semantic fields. If the full view of the parent is a record
465 -- type, build constrained components of subtype.
466
467 procedure Derive_Progenitor_Subprograms
468 (Parent_Type : Entity_Id;
469 Tagged_Type : Entity_Id);
470 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
471 -- operations of progenitors of Tagged_Type, and replace the subsidiary
472 -- subtypes with Tagged_Type, to build the specs of the inherited interface
473 -- primitives. The derived primitives are aliased to those of the
474 -- interface. This routine takes care also of transferring to the full view
475 -- subprograms associated with the partial view of Tagged_Type that cover
476 -- interface primitives.
477
478 procedure Derived_Standard_Character
479 (N : Node_Id;
480 Parent_Type : Entity_Id;
481 Derived_Type : Entity_Id);
482 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
483 -- derivations from types Standard.Character and Standard.Wide_Character.
484
485 procedure Derived_Type_Declaration
486 (T : Entity_Id;
487 N : Node_Id;
488 Is_Completion : Boolean);
489 -- Process a derived type declaration. Build_Derived_Type is invoked
490 -- to process the actual derived type definition. Parameters N and
491 -- Is_Completion have the same meaning as in Build_Derived_Type.
492 -- T is the N_Defining_Identifier for the entity defined in the
493 -- N_Full_Type_Declaration node N, that is T is the derived type.
494
495 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
496 -- Insert each literal in symbol table, as an overloadable identifier. Each
497 -- enumeration type is mapped into a sequence of integers, and each literal
498 -- is defined as a constant with integer value. If any of the literals are
499 -- character literals, the type is a character type, which means that
500 -- strings are legal aggregates for arrays of components of the type.
501
502 function Expand_To_Stored_Constraint
503 (Typ : Entity_Id;
504 Constraint : Elist_Id) return Elist_Id;
505 -- Given a constraint (i.e. a list of expressions) on the discriminants of
506 -- Typ, expand it into a constraint on the stored discriminants and return
507 -- the new list of expressions constraining the stored discriminants.
508
509 function Find_Type_Of_Object
510 (Obj_Def : Node_Id;
511 Related_Nod : Node_Id) return Entity_Id;
512 -- Get type entity for object referenced by Obj_Def, attaching the implicit
513 -- types generated to Related_Nod.
514
515 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
516 -- Create a new float and apply the constraint to obtain subtype of it
517
518 function Has_Range_Constraint (N : Node_Id) return Boolean;
519 -- Given an N_Subtype_Indication node N, return True if a range constraint
520 -- is present, either directly, or as part of a digits or delta constraint.
521 -- In addition, a digits constraint in the decimal case returns True, since
522 -- it establishes a default range if no explicit range is present.
523
524 function Inherit_Components
525 (N : Node_Id;
526 Parent_Base : Entity_Id;
527 Derived_Base : Entity_Id;
528 Is_Tagged : Boolean;
529 Inherit_Discr : Boolean;
530 Discs : Elist_Id) return Elist_Id;
531 -- Called from Build_Derived_Record_Type to inherit the components of
532 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
533 -- For more information on derived types and component inheritance please
534 -- consult the comment above the body of Build_Derived_Record_Type.
535 --
536 -- N is the original derived type declaration
537 --
538 -- Is_Tagged is set if we are dealing with tagged types
539 --
540 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
541 -- Parent_Base, otherwise no discriminants are inherited.
542 --
543 -- Discs gives the list of constraints that apply to Parent_Base in the
544 -- derived type declaration. If Discs is set to No_Elist, then we have
545 -- the following situation:
546 --
547 -- type Parent (D1..Dn : ..) is [tagged] record ...;
548 -- type Derived is new Parent [with ...];
549 --
550 -- which gets treated as
551 --
552 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
553 --
554 -- For untagged types the returned value is an association list. The list
555 -- starts from the association (Parent_Base => Derived_Base), and then it
556 -- contains a sequence of the associations of the form
557 --
558 -- (Old_Component => New_Component),
559 --
560 -- where Old_Component is the Entity_Id of a component in Parent_Base and
561 -- New_Component is the Entity_Id of the corresponding component in
562 -- Derived_Base. For untagged records, this association list is needed when
563 -- copying the record declaration for the derived base. In the tagged case
564 -- the value returned is irrelevant.
565
566 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
567 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
568 -- Determine whether subprogram Subp is a procedure subject to pragma
569 -- Extensions_Visible with value False and has at least one controlling
570 -- parameter of mode OUT.
571
572 function Is_Valid_Constraint_Kind
573 (T_Kind : Type_Kind;
574 Constraint_Kind : Node_Kind) return Boolean;
575 -- Returns True if it is legal to apply the given kind of constraint to the
576 -- given kind of type (index constraint to an array type, for example).
577
578 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
579 -- Create new modular type. Verify that modulus is in bounds
580
581 procedure New_Concatenation_Op (Typ : Entity_Id);
582 -- Create an abbreviated declaration for an operator in order to
583 -- materialize concatenation on array types.
584
585 procedure Ordinary_Fixed_Point_Type_Declaration
586 (T : Entity_Id;
587 Def : Node_Id);
588 -- Create a new ordinary fixed point type, and apply the constraint to
589 -- obtain subtype of it.
590
591 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
592 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
593 -- In_Default_Expr can be properly adjusted.
594
595 procedure Prepare_Private_Subtype_Completion
596 (Id : Entity_Id;
597 Related_Nod : Node_Id);
598 -- Id is a subtype of some private type. Creates the full declaration
599 -- associated with Id whenever possible, i.e. when the full declaration
600 -- of the base type is already known. Records each subtype into
601 -- Private_Dependents of the base type.
602
603 procedure Process_Incomplete_Dependents
604 (N : Node_Id;
605 Full_T : Entity_Id;
606 Inc_T : Entity_Id);
607 -- Process all entities that depend on an incomplete type. There include
608 -- subtypes, subprogram types that mention the incomplete type in their
609 -- profiles, and subprogram with access parameters that designate the
610 -- incomplete type.
611
612 -- Inc_T is the defining identifier of an incomplete type declaration, its
613 -- Ekind is E_Incomplete_Type.
614 --
615 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
616 --
617 -- Full_T is N's defining identifier.
618 --
619 -- Subtypes of incomplete types with discriminants are completed when the
620 -- parent type is. This is simpler than private subtypes, because they can
621 -- only appear in the same scope, and there is no need to exchange views.
622 -- Similarly, access_to_subprogram types may have a parameter or a return
623 -- type that is an incomplete type, and that must be replaced with the
624 -- full type.
625 --
626 -- If the full type is tagged, subprogram with access parameters that
627 -- designated the incomplete may be primitive operations of the full type,
628 -- and have to be processed accordingly.
629
630 procedure Process_Real_Range_Specification (Def : Node_Id);
631 -- Given the type definition for a real type, this procedure processes and
632 -- checks the real range specification of this type definition if one is
633 -- present. If errors are found, error messages are posted, and the
634 -- Real_Range_Specification of Def is reset to Empty.
635
636 procedure Record_Type_Declaration
637 (T : Entity_Id;
638 N : Node_Id;
639 Prev : Entity_Id);
640 -- Process a record type declaration (for both untagged and tagged
641 -- records). Parameters T and N are exactly like in procedure
642 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
643 -- for this routine. If this is the completion of an incomplete type
644 -- declaration, Prev is the entity of the incomplete declaration, used for
645 -- cross-referencing. Otherwise Prev = T.
646
647 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
648 -- This routine is used to process the actual record type definition (both
649 -- for untagged and tagged records). Def is a record type definition node.
650 -- This procedure analyzes the components in this record type definition.
651 -- Prev_T is the entity for the enclosing record type. It is provided so
652 -- that its Has_Task flag can be set if any of the component have Has_Task
653 -- set. If the declaration is the completion of an incomplete type
654 -- declaration, Prev_T is the original incomplete type, whose full view is
655 -- the record type.
656
657 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
658 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
659 -- first create the list of components for the derived type from that of
660 -- the parent by means of Inherit_Components and then build a copy of the
661 -- declaration tree of the parent with the help of the mapping returned by
662 -- Inherit_Components, which will for example be used to validate record
663 -- representation clauses given for the derived type. If the parent type
664 -- is private and has discriminants, the ancestor discriminants used in the
665 -- inheritance are that of the private declaration, whereas the ancestor
666 -- discriminants present in the declaration tree of the parent are that of
667 -- the full declaration; as a consequence, the remapping done during the
668 -- copy will leave the references to the ancestor discriminants unchanged
669 -- in the declaration tree and they need to be fixed up. If the derived
670 -- type has a known discriminant part, then the remapping done during the
671 -- copy will only create references to the girder discriminants and they
672 -- need to be replaced with references to the non-girder discriminants.
673
674 procedure Set_Fixed_Range
675 (E : Entity_Id;
676 Loc : Source_Ptr;
677 Lo : Ureal;
678 Hi : Ureal);
679 -- Build a range node with the given bounds and set it as the Scalar_Range
680 -- of the given fixed-point type entity. Loc is the source location used
681 -- for the constructed range. See body for further details.
682
683 procedure Set_Scalar_Range_For_Subtype
684 (Def_Id : Entity_Id;
685 R : Node_Id;
686 Subt : Entity_Id);
687 -- This routine is used to set the scalar range field for a subtype given
688 -- Def_Id, the entity for the subtype, and R, the range expression for the
689 -- scalar range. Subt provides the parent subtype to be used to analyze,
690 -- resolve, and check the given range.
691
692 procedure Set_Default_SSO (T : Entity_Id);
693 -- T is the entity for an array or record being declared. This procedure
694 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
695 -- to the setting of Opt.Default_SSO.
696
697 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
698 -- Create a new signed integer entity, and apply the constraint to obtain
699 -- the required first named subtype of this type.
700
701 procedure Set_Stored_Constraint_From_Discriminant_Constraint
702 (E : Entity_Id);
703 -- E is some record type. This routine computes E's Stored_Constraint
704 -- from its Discriminant_Constraint.
705
706 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
707 -- Check that an entity in a list of progenitors is an interface,
708 -- emit error otherwise.
709
710 -----------------------
711 -- Access_Definition --
712 -----------------------
713
714 function Access_Definition
715 (Related_Nod : Node_Id;
716 N : Node_Id) return Entity_Id
717 is
718 Anon_Type : Entity_Id;
719 Anon_Scope : Entity_Id;
720 Desig_Type : Entity_Id;
721 Enclosing_Prot_Type : Entity_Id := Empty;
722
723 begin
724 if Is_Entry (Current_Scope)
725 and then Is_Task_Type (Etype (Scope (Current_Scope)))
726 then
727 Error_Msg_N ("task entries cannot have access parameters", N);
728 return Empty;
729 end if;
730
731 -- Ada 2005: For an object declaration the corresponding anonymous
732 -- type is declared in the current scope.
733
734 -- If the access definition is the return type of another access to
735 -- function, scope is the current one, because it is the one of the
736 -- current type declaration, except for the pathological case below.
737
738 if Nkind_In (Related_Nod, N_Object_Declaration,
739 N_Access_Function_Definition)
740 then
741 Anon_Scope := Current_Scope;
742
743 -- A pathological case: function returning access functions that
744 -- return access functions, etc. Each anonymous access type created
745 -- is in the enclosing scope of the outermost function.
746
747 declare
748 Par : Node_Id;
749
750 begin
751 Par := Related_Nod;
752 while Nkind_In (Par, N_Access_Function_Definition,
753 N_Access_Definition)
754 loop
755 Par := Parent (Par);
756 end loop;
757
758 if Nkind (Par) = N_Function_Specification then
759 Anon_Scope := Scope (Defining_Entity (Par));
760 end if;
761 end;
762
763 -- For the anonymous function result case, retrieve the scope of the
764 -- function specification's associated entity rather than using the
765 -- current scope. The current scope will be the function itself if the
766 -- formal part is currently being analyzed, but will be the parent scope
767 -- in the case of a parameterless function, and we always want to use
768 -- the function's parent scope. Finally, if the function is a child
769 -- unit, we must traverse the tree to retrieve the proper entity.
770
771 elsif Nkind (Related_Nod) = N_Function_Specification
772 and then Nkind (Parent (N)) /= N_Parameter_Specification
773 then
774 -- If the current scope is a protected type, the anonymous access
775 -- is associated with one of the protected operations, and must
776 -- be available in the scope that encloses the protected declaration.
777 -- Otherwise the type is in the scope enclosing the subprogram.
778
779 -- If the function has formals, The return type of a subprogram
780 -- declaration is analyzed in the scope of the subprogram (see
781 -- Process_Formals) and thus the protected type, if present, is
782 -- the scope of the current function scope.
783
784 if Ekind (Current_Scope) = E_Protected_Type then
785 Enclosing_Prot_Type := Current_Scope;
786
787 elsif Ekind (Current_Scope) = E_Function
788 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
789 then
790 Enclosing_Prot_Type := Scope (Current_Scope);
791 end if;
792
793 if Present (Enclosing_Prot_Type) then
794 Anon_Scope := Scope (Enclosing_Prot_Type);
795
796 else
797 Anon_Scope := Scope (Defining_Entity (Related_Nod));
798 end if;
799
800 -- For an access type definition, if the current scope is a child
801 -- unit it is the scope of the type.
802
803 elsif Is_Compilation_Unit (Current_Scope) then
804 Anon_Scope := Current_Scope;
805
806 -- For access formals, access components, and access discriminants, the
807 -- scope is that of the enclosing declaration,
808
809 else
810 Anon_Scope := Scope (Current_Scope);
811 end if;
812
813 Anon_Type :=
814 Create_Itype
815 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
816
817 if All_Present (N)
818 and then Ada_Version >= Ada_2005
819 then
820 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
821 end if;
822
823 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
824 -- the corresponding semantic routine
825
826 if Present (Access_To_Subprogram_Definition (N)) then
827
828 -- Compiler runtime units are compiled in Ada 2005 mode when building
829 -- the runtime library but must also be compilable in Ada 95 mode
830 -- (when bootstrapping the compiler).
831
832 Check_Compiler_Unit ("anonymous access to subprogram", N);
833
834 Access_Subprogram_Declaration
835 (T_Name => Anon_Type,
836 T_Def => Access_To_Subprogram_Definition (N));
837
838 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
839 Set_Ekind
840 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
841 else
842 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
843 end if;
844
845 Set_Can_Use_Internal_Rep
846 (Anon_Type, not Always_Compatible_Rep_On_Target);
847
848 -- If the anonymous access is associated with a protected operation,
849 -- create a reference to it after the enclosing protected definition
850 -- because the itype will be used in the subsequent bodies.
851
852 -- If the anonymous access itself is protected, a full type
853 -- declaratiton will be created for it, so that the equivalent
854 -- record type can be constructed. For further details, see
855 -- Replace_Anonymous_Access_To_Protected-Subprogram.
856
857 if Ekind (Current_Scope) = E_Protected_Type
858 and then not Protected_Present (Access_To_Subprogram_Definition (N))
859 then
860 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
861 end if;
862
863 return Anon_Type;
864 end if;
865
866 Find_Type (Subtype_Mark (N));
867 Desig_Type := Entity (Subtype_Mark (N));
868
869 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
870 Set_Etype (Anon_Type, Anon_Type);
871
872 -- Make sure the anonymous access type has size and alignment fields
873 -- set, as required by gigi. This is necessary in the case of the
874 -- Task_Body_Procedure.
875
876 if not Has_Private_Component (Desig_Type) then
877 Layout_Type (Anon_Type);
878 end if;
879
880 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
881 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
882 -- the null value is allowed. In Ada 95 the null value is never allowed.
883
884 if Ada_Version >= Ada_2005 then
885 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
886 else
887 Set_Can_Never_Be_Null (Anon_Type, True);
888 end if;
889
890 -- The anonymous access type is as public as the discriminated type or
891 -- subprogram that defines it. It is imported (for back-end purposes)
892 -- if the designated type is.
893
894 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
895
896 -- Ada 2005 (AI-231): Propagate the access-constant attribute
897
898 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
899
900 -- The context is either a subprogram declaration, object declaration,
901 -- or an access discriminant, in a private or a full type declaration.
902 -- In the case of a subprogram, if the designated type is incomplete,
903 -- the operation will be a primitive operation of the full type, to be
904 -- updated subsequently. If the type is imported through a limited_with
905 -- clause, the subprogram is not a primitive operation of the type
906 -- (which is declared elsewhere in some other scope).
907
908 if Ekind (Desig_Type) = E_Incomplete_Type
909 and then not From_Limited_With (Desig_Type)
910 and then Is_Overloadable (Current_Scope)
911 then
912 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
913 Set_Has_Delayed_Freeze (Current_Scope);
914 end if;
915
916 -- If the designated type is limited and class-wide, the object might
917 -- contain tasks, so we create a Master entity for the declaration. This
918 -- must be done before expansion of the full declaration, because the
919 -- declaration may include an expression that is an allocator, whose
920 -- expansion needs the proper Master for the created tasks.
921
922 if Expander_Active
923 and then Nkind (Related_Nod) = N_Object_Declaration
924 then
925 if Is_Limited_Record (Desig_Type)
926 and then Is_Class_Wide_Type (Desig_Type)
927 and then Tasking_Allowed
928 then
929 Build_Class_Wide_Master (Anon_Type);
930
931 -- Similarly, if the type is an anonymous access that designates
932 -- tasks, create a master entity for it in the current context.
933
934 elsif Has_Task (Desig_Type)
935 and then Comes_From_Source (Related_Nod)
936 then
937 Build_Master_Entity (Defining_Identifier (Related_Nod));
938 Build_Master_Renaming (Anon_Type);
939 end if;
940 end if;
941
942 -- For a private component of a protected type, it is imperative that
943 -- the back-end elaborate the type immediately after the protected
944 -- declaration, because this type will be used in the declarations
945 -- created for the component within each protected body, so we must
946 -- create an itype reference for it now.
947
948 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
949 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
950
951 -- Similarly, if the access definition is the return result of a
952 -- function, create an itype reference for it because it will be used
953 -- within the function body. For a regular function that is not a
954 -- compilation unit, insert reference after the declaration. For a
955 -- protected operation, insert it after the enclosing protected type
956 -- declaration. In either case, do not create a reference for a type
957 -- obtained through a limited_with clause, because this would introduce
958 -- semantic dependencies.
959
960 -- Similarly, do not create a reference if the designated type is a
961 -- generic formal, because no use of it will reach the backend.
962
963 elsif Nkind (Related_Nod) = N_Function_Specification
964 and then not From_Limited_With (Desig_Type)
965 and then not Is_Generic_Type (Desig_Type)
966 then
967 if Present (Enclosing_Prot_Type) then
968 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
969
970 elsif Is_List_Member (Parent (Related_Nod))
971 and then Nkind (Parent (N)) /= N_Parameter_Specification
972 then
973 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
974 end if;
975
976 -- Finally, create an itype reference for an object declaration of an
977 -- anonymous access type. This is strictly necessary only for deferred
978 -- constants, but in any case will avoid out-of-scope problems in the
979 -- back-end.
980
981 elsif Nkind (Related_Nod) = N_Object_Declaration then
982 Build_Itype_Reference (Anon_Type, Related_Nod);
983 end if;
984
985 return Anon_Type;
986 end Access_Definition;
987
988 -----------------------------------
989 -- Access_Subprogram_Declaration --
990 -----------------------------------
991
992 procedure Access_Subprogram_Declaration
993 (T_Name : Entity_Id;
994 T_Def : Node_Id)
995 is
996 procedure Check_For_Premature_Usage (Def : Node_Id);
997 -- Check that type T_Name is not used, directly or recursively, as a
998 -- parameter or a return type in Def. Def is either a subtype, an
999 -- access_definition, or an access_to_subprogram_definition.
1000
1001 -------------------------------
1002 -- Check_For_Premature_Usage --
1003 -------------------------------
1004
1005 procedure Check_For_Premature_Usage (Def : Node_Id) is
1006 Param : Node_Id;
1007
1008 begin
1009 -- Check for a subtype mark
1010
1011 if Nkind (Def) in N_Has_Etype then
1012 if Etype (Def) = T_Name then
1013 Error_Msg_N
1014 ("type& cannot be used before end of its declaration", Def);
1015 end if;
1016
1017 -- If this is not a subtype, then this is an access_definition
1018
1019 elsif Nkind (Def) = N_Access_Definition then
1020 if Present (Access_To_Subprogram_Definition (Def)) then
1021 Check_For_Premature_Usage
1022 (Access_To_Subprogram_Definition (Def));
1023 else
1024 Check_For_Premature_Usage (Subtype_Mark (Def));
1025 end if;
1026
1027 -- The only cases left are N_Access_Function_Definition and
1028 -- N_Access_Procedure_Definition.
1029
1030 else
1031 if Present (Parameter_Specifications (Def)) then
1032 Param := First (Parameter_Specifications (Def));
1033 while Present (Param) loop
1034 Check_For_Premature_Usage (Parameter_Type (Param));
1035 Param := Next (Param);
1036 end loop;
1037 end if;
1038
1039 if Nkind (Def) = N_Access_Function_Definition then
1040 Check_For_Premature_Usage (Result_Definition (Def));
1041 end if;
1042 end if;
1043 end Check_For_Premature_Usage;
1044
1045 -- Local variables
1046
1047 Formals : constant List_Id := Parameter_Specifications (T_Def);
1048 Formal : Entity_Id;
1049 D_Ityp : Node_Id;
1050 Desig_Type : constant Entity_Id :=
1051 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1052
1053 -- Start of processing for Access_Subprogram_Declaration
1054
1055 begin
1056 -- Associate the Itype node with the inner full-type declaration or
1057 -- subprogram spec or entry body. This is required to handle nested
1058 -- anonymous declarations. For example:
1059
1060 -- procedure P
1061 -- (X : access procedure
1062 -- (Y : access procedure
1063 -- (Z : access T)))
1064
1065 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1066 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1067 N_Private_Type_Declaration,
1068 N_Private_Extension_Declaration,
1069 N_Procedure_Specification,
1070 N_Function_Specification,
1071 N_Entry_Body)
1072
1073 or else
1074 Nkind_In (D_Ityp, N_Object_Declaration,
1075 N_Object_Renaming_Declaration,
1076 N_Formal_Object_Declaration,
1077 N_Formal_Type_Declaration,
1078 N_Task_Type_Declaration,
1079 N_Protected_Type_Declaration))
1080 loop
1081 D_Ityp := Parent (D_Ityp);
1082 pragma Assert (D_Ityp /= Empty);
1083 end loop;
1084
1085 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1086
1087 if Nkind_In (D_Ityp, N_Procedure_Specification,
1088 N_Function_Specification)
1089 then
1090 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1091
1092 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1093 N_Object_Declaration,
1094 N_Object_Renaming_Declaration,
1095 N_Formal_Type_Declaration)
1096 then
1097 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1098 end if;
1099
1100 if Nkind (T_Def) = N_Access_Function_Definition then
1101 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1102 declare
1103 Acc : constant Node_Id := Result_Definition (T_Def);
1104
1105 begin
1106 if Present (Access_To_Subprogram_Definition (Acc))
1107 and then
1108 Protected_Present (Access_To_Subprogram_Definition (Acc))
1109 then
1110 Set_Etype
1111 (Desig_Type,
1112 Replace_Anonymous_Access_To_Protected_Subprogram
1113 (T_Def));
1114
1115 else
1116 Set_Etype
1117 (Desig_Type,
1118 Access_Definition (T_Def, Result_Definition (T_Def)));
1119 end if;
1120 end;
1121
1122 else
1123 Analyze (Result_Definition (T_Def));
1124
1125 declare
1126 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1127
1128 begin
1129 -- If a null exclusion is imposed on the result type, then
1130 -- create a null-excluding itype (an access subtype) and use
1131 -- it as the function's Etype.
1132
1133 if Is_Access_Type (Typ)
1134 and then Null_Exclusion_In_Return_Present (T_Def)
1135 then
1136 Set_Etype (Desig_Type,
1137 Create_Null_Excluding_Itype
1138 (T => Typ,
1139 Related_Nod => T_Def,
1140 Scope_Id => Current_Scope));
1141
1142 else
1143 if From_Limited_With (Typ) then
1144
1145 -- AI05-151: Incomplete types are allowed in all basic
1146 -- declarations, including access to subprograms.
1147
1148 if Ada_Version >= Ada_2012 then
1149 null;
1150
1151 else
1152 Error_Msg_NE
1153 ("illegal use of incomplete type&",
1154 Result_Definition (T_Def), Typ);
1155 end if;
1156
1157 elsif Ekind (Current_Scope) = E_Package
1158 and then In_Private_Part (Current_Scope)
1159 then
1160 if Ekind (Typ) = E_Incomplete_Type then
1161 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1162
1163 elsif Is_Class_Wide_Type (Typ)
1164 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1165 then
1166 Append_Elmt
1167 (Desig_Type, Private_Dependents (Etype (Typ)));
1168 end if;
1169 end if;
1170
1171 Set_Etype (Desig_Type, Typ);
1172 end if;
1173 end;
1174 end if;
1175
1176 if not (Is_Type (Etype (Desig_Type))) then
1177 Error_Msg_N
1178 ("expect type in function specification",
1179 Result_Definition (T_Def));
1180 end if;
1181
1182 else
1183 Set_Etype (Desig_Type, Standard_Void_Type);
1184 end if;
1185
1186 if Present (Formals) then
1187 Push_Scope (Desig_Type);
1188
1189 -- Some special tests here. These special tests can be removed
1190 -- if and when Itypes always have proper parent pointers to their
1191 -- declarations???
1192
1193 -- Special test 1) Link defining_identifier of formals. Required by
1194 -- First_Formal to provide its functionality.
1195
1196 declare
1197 F : Node_Id;
1198
1199 begin
1200 F := First (Formals);
1201
1202 while Present (F) loop
1203 if No (Parent (Defining_Identifier (F))) then
1204 Set_Parent (Defining_Identifier (F), F);
1205 end if;
1206
1207 Next (F);
1208 end loop;
1209 end;
1210
1211 Process_Formals (Formals, Parent (T_Def));
1212
1213 -- Special test 2) End_Scope requires that the parent pointer be set
1214 -- to something reasonable, but Itypes don't have parent pointers. So
1215 -- we set it and then unset it ???
1216
1217 Set_Parent (Desig_Type, T_Name);
1218 End_Scope;
1219 Set_Parent (Desig_Type, Empty);
1220 end if;
1221
1222 -- Check for premature usage of the type being defined
1223
1224 Check_For_Premature_Usage (T_Def);
1225
1226 -- The return type and/or any parameter type may be incomplete. Mark the
1227 -- subprogram_type as depending on the incomplete type, so that it can
1228 -- be updated when the full type declaration is seen. This only applies
1229 -- to incomplete types declared in some enclosing scope, not to limited
1230 -- views from other packages.
1231
1232 -- Prior to Ada 2012, access to functions can only have in_parameters.
1233
1234 if Present (Formals) then
1235 Formal := First_Formal (Desig_Type);
1236 while Present (Formal) loop
1237 if Ekind (Formal) /= E_In_Parameter
1238 and then Nkind (T_Def) = N_Access_Function_Definition
1239 and then Ada_Version < Ada_2012
1240 then
1241 Error_Msg_N ("functions can only have IN parameters", Formal);
1242 end if;
1243
1244 if Ekind (Etype (Formal)) = E_Incomplete_Type
1245 and then In_Open_Scopes (Scope (Etype (Formal)))
1246 then
1247 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1248 Set_Has_Delayed_Freeze (Desig_Type);
1249 end if;
1250
1251 Next_Formal (Formal);
1252 end loop;
1253 end if;
1254
1255 -- Check whether an indirect call without actuals may be possible. This
1256 -- is used when resolving calls whose result is then indexed.
1257
1258 May_Need_Actuals (Desig_Type);
1259
1260 -- If the return type is incomplete, this is legal as long as the type
1261 -- is declared in the current scope and will be completed in it (rather
1262 -- than being part of limited view).
1263
1264 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1265 and then not Has_Delayed_Freeze (Desig_Type)
1266 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1267 then
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1270 end if;
1271
1272 Check_Delayed_Subprogram (Desig_Type);
1273
1274 if Protected_Present (T_Def) then
1275 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1276 Set_Convention (Desig_Type, Convention_Protected);
1277 else
1278 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1279 end if;
1280
1281 Set_Can_Use_Internal_Rep (T_Name,
1282 not Always_Compatible_Rep_On_Target);
1283 Set_Etype (T_Name, T_Name);
1284 Init_Size_Align (T_Name);
1285 Set_Directly_Designated_Type (T_Name, Desig_Type);
1286
1287 -- If the access_to_subprogram is not declared at the library level,
1288 -- it can only point to subprograms that are at the same or deeper
1289 -- accessibility level. The corresponding subprogram type might
1290 -- require an activation record when compiling for C.
1291
1292 Set_Needs_Activation_Record (Desig_Type,
1293 not Is_Library_Level_Entity (T_Name));
1294
1295 Generate_Reference_To_Formals (T_Name);
1296
1297 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1298
1299 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1300
1301 Check_Restriction (No_Access_Subprograms, T_Def);
1302 end Access_Subprogram_Declaration;
1303
1304 ----------------------------
1305 -- Access_Type_Declaration --
1306 ----------------------------
1307
1308 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1309 P : constant Node_Id := Parent (Def);
1310 S : constant Node_Id := Subtype_Indication (Def);
1311
1312 Full_Desig : Entity_Id;
1313
1314 begin
1315 -- Check for permissible use of incomplete type
1316
1317 if Nkind (S) /= N_Subtype_Indication then
1318 Analyze (S);
1319
1320 if Present (Entity (S))
1321 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1322 then
1323 Set_Directly_Designated_Type (T, Entity (S));
1324
1325 -- If the designated type is a limited view, we cannot tell if
1326 -- the full view contains tasks, and there is no way to handle
1327 -- that full view in a client. We create a master entity for the
1328 -- scope, which will be used when a client determines that one
1329 -- is needed.
1330
1331 if From_Limited_With (Entity (S))
1332 and then not Is_Class_Wide_Type (Entity (S))
1333 then
1334 Set_Ekind (T, E_Access_Type);
1335 Build_Master_Entity (T);
1336 Build_Master_Renaming (T);
1337 end if;
1338
1339 else
1340 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1341 end if;
1342
1343 -- If the access definition is of the form: ACCESS NOT NULL ..
1344 -- the subtype indication must be of an access type. Create
1345 -- a null-excluding subtype of it.
1346
1347 if Null_Excluding_Subtype (Def) then
1348 if not Is_Access_Type (Entity (S)) then
1349 Error_Msg_N ("null exclusion must apply to access type", Def);
1350
1351 else
1352 declare
1353 Loc : constant Source_Ptr := Sloc (S);
1354 Decl : Node_Id;
1355 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1356
1357 begin
1358 Decl :=
1359 Make_Subtype_Declaration (Loc,
1360 Defining_Identifier => Nam,
1361 Subtype_Indication =>
1362 New_Occurrence_Of (Entity (S), Loc));
1363 Set_Null_Exclusion_Present (Decl);
1364 Insert_Before (Parent (Def), Decl);
1365 Analyze (Decl);
1366 Set_Entity (S, Nam);
1367 end;
1368 end if;
1369 end if;
1370
1371 else
1372 Set_Directly_Designated_Type (T,
1373 Process_Subtype (S, P, T, 'P'));
1374 end if;
1375
1376 if All_Present (Def) or Constant_Present (Def) then
1377 Set_Ekind (T, E_General_Access_Type);
1378 else
1379 Set_Ekind (T, E_Access_Type);
1380 end if;
1381
1382 Full_Desig := Designated_Type (T);
1383
1384 if Base_Type (Full_Desig) = T then
1385 Error_Msg_N ("access type cannot designate itself", S);
1386
1387 -- In Ada 2005, the type may have a limited view through some unit in
1388 -- its own context, allowing the following circularity that cannot be
1389 -- detected earlier.
1390
1391 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1392 then
1393 Error_Msg_N
1394 ("access type cannot designate its own class-wide type", S);
1395
1396 -- Clean up indication of tagged status to prevent cascaded errors
1397
1398 Set_Is_Tagged_Type (T, False);
1399 end if;
1400
1401 Set_Etype (T, T);
1402
1403 -- If the type has appeared already in a with_type clause, it is frozen
1404 -- and the pointer size is already set. Else, initialize.
1405
1406 if not From_Limited_With (T) then
1407 Init_Size_Align (T);
1408 end if;
1409
1410 -- Note that Has_Task is always false, since the access type itself
1411 -- is not a task type. See Einfo for more description on this point.
1412 -- Exactly the same consideration applies to Has_Controlled_Component
1413 -- and to Has_Protected.
1414
1415 Set_Has_Task (T, False);
1416 Set_Has_Protected (T, False);
1417 Set_Has_Timing_Event (T, False);
1418 Set_Has_Controlled_Component (T, False);
1419
1420 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1421 -- problems where an incomplete view of this entity has been previously
1422 -- established by a limited with and an overlaid version of this field
1423 -- (Stored_Constraint) was initialized for the incomplete view.
1424
1425 -- This reset is performed in most cases except where the access type
1426 -- has been created for the purposes of allocating or deallocating a
1427 -- build-in-place object. Such access types have explicitly set pools
1428 -- and finalization masters.
1429
1430 if No (Associated_Storage_Pool (T)) then
1431 Set_Finalization_Master (T, Empty);
1432 end if;
1433
1434 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1435 -- attributes
1436
1437 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1438 Set_Is_Access_Constant (T, Constant_Present (Def));
1439 end Access_Type_Declaration;
1440
1441 ----------------------------------
1442 -- Add_Interface_Tag_Components --
1443 ----------------------------------
1444
1445 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1446 Loc : constant Source_Ptr := Sloc (N);
1447 L : List_Id;
1448 Last_Tag : Node_Id;
1449
1450 procedure Add_Tag (Iface : Entity_Id);
1451 -- Add tag for one of the progenitor interfaces
1452
1453 -------------
1454 -- Add_Tag --
1455 -------------
1456
1457 procedure Add_Tag (Iface : Entity_Id) is
1458 Decl : Node_Id;
1459 Def : Node_Id;
1460 Tag : Entity_Id;
1461 Offset : Entity_Id;
1462
1463 begin
1464 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1465
1466 -- This is a reasonable place to propagate predicates
1467
1468 if Has_Predicates (Iface) then
1469 Set_Has_Predicates (Typ);
1470 end if;
1471
1472 Def :=
1473 Make_Component_Definition (Loc,
1474 Aliased_Present => True,
1475 Subtype_Indication =>
1476 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1477
1478 Tag := Make_Temporary (Loc, 'V');
1479
1480 Decl :=
1481 Make_Component_Declaration (Loc,
1482 Defining_Identifier => Tag,
1483 Component_Definition => Def);
1484
1485 Analyze_Component_Declaration (Decl);
1486
1487 Set_Analyzed (Decl);
1488 Set_Ekind (Tag, E_Component);
1489 Set_Is_Tag (Tag);
1490 Set_Is_Aliased (Tag);
1491 Set_Is_Independent (Tag);
1492 Set_Related_Type (Tag, Iface);
1493 Init_Component_Location (Tag);
1494
1495 pragma Assert (Is_Frozen (Iface));
1496
1497 Set_DT_Entry_Count (Tag,
1498 DT_Entry_Count (First_Entity (Iface)));
1499
1500 if No (Last_Tag) then
1501 Prepend (Decl, L);
1502 else
1503 Insert_After (Last_Tag, Decl);
1504 end if;
1505
1506 Last_Tag := Decl;
1507
1508 -- If the ancestor has discriminants we need to give special support
1509 -- to store the offset_to_top value of the secondary dispatch tables.
1510 -- For this purpose we add a supplementary component just after the
1511 -- field that contains the tag associated with each secondary DT.
1512
1513 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1514 Def :=
1515 Make_Component_Definition (Loc,
1516 Subtype_Indication =>
1517 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1518
1519 Offset := Make_Temporary (Loc, 'V');
1520
1521 Decl :=
1522 Make_Component_Declaration (Loc,
1523 Defining_Identifier => Offset,
1524 Component_Definition => Def);
1525
1526 Analyze_Component_Declaration (Decl);
1527
1528 Set_Analyzed (Decl);
1529 Set_Ekind (Offset, E_Component);
1530 Set_Is_Aliased (Offset);
1531 Set_Is_Independent (Offset);
1532 Set_Related_Type (Offset, Iface);
1533 Init_Component_Location (Offset);
1534 Insert_After (Last_Tag, Decl);
1535 Last_Tag := Decl;
1536 end if;
1537 end Add_Tag;
1538
1539 -- Local variables
1540
1541 Elmt : Elmt_Id;
1542 Ext : Node_Id;
1543 Comp : Node_Id;
1544
1545 -- Start of processing for Add_Interface_Tag_Components
1546
1547 begin
1548 if not RTE_Available (RE_Interface_Tag) then
1549 Error_Msg
1550 ("(Ada 2005) interface types not supported by this run-time!",
1551 Sloc (N));
1552 return;
1553 end if;
1554
1555 if Ekind (Typ) /= E_Record_Type
1556 or else (Is_Concurrent_Record_Type (Typ)
1557 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1558 or else (not Is_Concurrent_Record_Type (Typ)
1559 and then No (Interfaces (Typ))
1560 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1561 then
1562 return;
1563 end if;
1564
1565 -- Find the current last tag
1566
1567 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1568 Ext := Record_Extension_Part (Type_Definition (N));
1569 else
1570 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1571 Ext := Type_Definition (N);
1572 end if;
1573
1574 Last_Tag := Empty;
1575
1576 if not (Present (Component_List (Ext))) then
1577 Set_Null_Present (Ext, False);
1578 L := New_List;
1579 Set_Component_List (Ext,
1580 Make_Component_List (Loc,
1581 Component_Items => L,
1582 Null_Present => False));
1583 else
1584 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1585 L := Component_Items
1586 (Component_List
1587 (Record_Extension_Part
1588 (Type_Definition (N))));
1589 else
1590 L := Component_Items
1591 (Component_List
1592 (Type_Definition (N)));
1593 end if;
1594
1595 -- Find the last tag component
1596
1597 Comp := First (L);
1598 while Present (Comp) loop
1599 if Nkind (Comp) = N_Component_Declaration
1600 and then Is_Tag (Defining_Identifier (Comp))
1601 then
1602 Last_Tag := Comp;
1603 end if;
1604
1605 Next (Comp);
1606 end loop;
1607 end if;
1608
1609 -- At this point L references the list of components and Last_Tag
1610 -- references the current last tag (if any). Now we add the tag
1611 -- corresponding with all the interfaces that are not implemented
1612 -- by the parent.
1613
1614 if Present (Interfaces (Typ)) then
1615 Elmt := First_Elmt (Interfaces (Typ));
1616 while Present (Elmt) loop
1617 Add_Tag (Node (Elmt));
1618 Next_Elmt (Elmt);
1619 end loop;
1620 end if;
1621 end Add_Interface_Tag_Components;
1622
1623 -------------------------------------
1624 -- Add_Internal_Interface_Entities --
1625 -------------------------------------
1626
1627 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1628 Elmt : Elmt_Id;
1629 Iface : Entity_Id;
1630 Iface_Elmt : Elmt_Id;
1631 Iface_Prim : Entity_Id;
1632 Ifaces_List : Elist_Id;
1633 New_Subp : Entity_Id := Empty;
1634 Prim : Entity_Id;
1635 Restore_Scope : Boolean := False;
1636
1637 begin
1638 pragma Assert (Ada_Version >= Ada_2005
1639 and then Is_Record_Type (Tagged_Type)
1640 and then Is_Tagged_Type (Tagged_Type)
1641 and then Has_Interfaces (Tagged_Type)
1642 and then not Is_Interface (Tagged_Type));
1643
1644 -- Ensure that the internal entities are added to the scope of the type
1645
1646 if Scope (Tagged_Type) /= Current_Scope then
1647 Push_Scope (Scope (Tagged_Type));
1648 Restore_Scope := True;
1649 end if;
1650
1651 Collect_Interfaces (Tagged_Type, Ifaces_List);
1652
1653 Iface_Elmt := First_Elmt (Ifaces_List);
1654 while Present (Iface_Elmt) loop
1655 Iface := Node (Iface_Elmt);
1656
1657 -- Originally we excluded here from this processing interfaces that
1658 -- are parents of Tagged_Type because their primitives are located
1659 -- in the primary dispatch table (and hence no auxiliary internal
1660 -- entities are required to handle secondary dispatch tables in such
1661 -- case). However, these auxiliary entities are also required to
1662 -- handle derivations of interfaces in formals of generics (see
1663 -- Derive_Subprograms).
1664
1665 Elmt := First_Elmt (Primitive_Operations (Iface));
1666 while Present (Elmt) loop
1667 Iface_Prim := Node (Elmt);
1668
1669 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1670 Prim :=
1671 Find_Primitive_Covering_Interface
1672 (Tagged_Type => Tagged_Type,
1673 Iface_Prim => Iface_Prim);
1674
1675 if No (Prim) and then Serious_Errors_Detected > 0 then
1676 goto Continue;
1677 end if;
1678
1679 pragma Assert (Present (Prim));
1680
1681 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1682 -- differs from the name of the interface primitive then it is
1683 -- a private primitive inherited from a parent type. In such
1684 -- case, given that Tagged_Type covers the interface, the
1685 -- inherited private primitive becomes visible. For such
1686 -- purpose we add a new entity that renames the inherited
1687 -- private primitive.
1688
1689 if Chars (Prim) /= Chars (Iface_Prim) then
1690 pragma Assert (Has_Suffix (Prim, 'P'));
1691 Derive_Subprogram
1692 (New_Subp => New_Subp,
1693 Parent_Subp => Iface_Prim,
1694 Derived_Type => Tagged_Type,
1695 Parent_Type => Iface);
1696 Set_Alias (New_Subp, Prim);
1697 Set_Is_Abstract_Subprogram
1698 (New_Subp, Is_Abstract_Subprogram (Prim));
1699 end if;
1700
1701 Derive_Subprogram
1702 (New_Subp => New_Subp,
1703 Parent_Subp => Iface_Prim,
1704 Derived_Type => Tagged_Type,
1705 Parent_Type => Iface);
1706
1707 declare
1708 Anc : Entity_Id;
1709 begin
1710 if Is_Inherited_Operation (Prim)
1711 and then Present (Alias (Prim))
1712 then
1713 Anc := Alias (Prim);
1714 else
1715 Anc := Overridden_Operation (Prim);
1716 end if;
1717
1718 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1719 -- nonconforming preconditions in both an ancestor and
1720 -- a progenitor operation.
1721
1722 -- If the operation is a primitive wrapper it is an explicit
1723 -- (overriding) operqtion and all is fine.
1724
1725 if Present (Anc)
1726 and then Has_Non_Trivial_Precondition (Anc)
1727 and then Has_Non_Trivial_Precondition (Iface_Prim)
1728 then
1729 if Is_Abstract_Subprogram (Prim)
1730 or else
1731 (Ekind (Prim) = E_Procedure
1732 and then Nkind (Parent (Prim)) =
1733 N_Procedure_Specification
1734 and then Null_Present (Parent (Prim)))
1735 or else Is_Primitive_Wrapper (Prim)
1736 then
1737 null;
1738
1739 -- The operation is inherited and must be overridden
1740
1741 elsif not Comes_From_Source (Prim) then
1742 Error_Msg_NE
1743 ("&inherits non-conforming preconditions and must "
1744 & "be overridden (RM 6.1.1 (10-16)",
1745 Parent (Tagged_Type), Prim);
1746 end if;
1747 end if;
1748 end;
1749
1750 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1751 -- associated with interface types. These entities are
1752 -- only registered in the list of primitives of its
1753 -- corresponding tagged type because they are only used
1754 -- to fill the contents of the secondary dispatch tables.
1755 -- Therefore they are removed from the homonym chains.
1756
1757 Set_Is_Hidden (New_Subp);
1758 Set_Is_Internal (New_Subp);
1759 Set_Alias (New_Subp, Prim);
1760 Set_Is_Abstract_Subprogram
1761 (New_Subp, Is_Abstract_Subprogram (Prim));
1762 Set_Interface_Alias (New_Subp, Iface_Prim);
1763
1764 -- If the returned type is an interface then propagate it to
1765 -- the returned type. Needed by the thunk to generate the code
1766 -- which displaces "this" to reference the corresponding
1767 -- secondary dispatch table in the returned object.
1768
1769 if Is_Interface (Etype (Iface_Prim)) then
1770 Set_Etype (New_Subp, Etype (Iface_Prim));
1771 end if;
1772
1773 -- Internal entities associated with interface types are only
1774 -- registered in the list of primitives of the tagged type.
1775 -- They are only used to fill the contents of the secondary
1776 -- dispatch tables. Therefore they are not needed in the
1777 -- homonym chains.
1778
1779 Remove_Homonym (New_Subp);
1780
1781 -- Hidden entities associated with interfaces must have set
1782 -- the Has_Delay_Freeze attribute to ensure that, in case
1783 -- of locally defined tagged types (or compiling with static
1784 -- dispatch tables generation disabled) the corresponding
1785 -- entry of the secondary dispatch table is filled when such
1786 -- an entity is frozen.
1787
1788 Set_Has_Delayed_Freeze (New_Subp);
1789 end if;
1790
1791 <<Continue>>
1792 Next_Elmt (Elmt);
1793 end loop;
1794
1795 Next_Elmt (Iface_Elmt);
1796 end loop;
1797
1798 if Restore_Scope then
1799 Pop_Scope;
1800 end if;
1801 end Add_Internal_Interface_Entities;
1802
1803 -----------------------------------
1804 -- Analyze_Component_Declaration --
1805 -----------------------------------
1806
1807 procedure Analyze_Component_Declaration (N : Node_Id) is
1808 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1809 Id : constant Entity_Id := Defining_Identifier (N);
1810 E : constant Node_Id := Expression (N);
1811 Typ : constant Node_Id :=
1812 Subtype_Indication (Component_Definition (N));
1813 T : Entity_Id;
1814 P : Entity_Id;
1815
1816 function Contains_POC (Constr : Node_Id) return Boolean;
1817 -- Determines whether a constraint uses the discriminant of a record
1818 -- type thus becoming a per-object constraint (POC).
1819
1820 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1821 -- Typ is the type of the current component, check whether this type is
1822 -- a limited type. Used to validate declaration against that of
1823 -- enclosing record.
1824
1825 ------------------
1826 -- Contains_POC --
1827 ------------------
1828
1829 function Contains_POC (Constr : Node_Id) return Boolean is
1830 begin
1831 -- Prevent cascaded errors
1832
1833 if Error_Posted (Constr) then
1834 return False;
1835 end if;
1836
1837 case Nkind (Constr) is
1838 when N_Attribute_Reference =>
1839 return Attribute_Name (Constr) = Name_Access
1840 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1841
1842 when N_Discriminant_Association =>
1843 return Denotes_Discriminant (Expression (Constr));
1844
1845 when N_Identifier =>
1846 return Denotes_Discriminant (Constr);
1847
1848 when N_Index_Or_Discriminant_Constraint =>
1849 declare
1850 IDC : Node_Id;
1851
1852 begin
1853 IDC := First (Constraints (Constr));
1854 while Present (IDC) loop
1855
1856 -- One per-object constraint is sufficient
1857
1858 if Contains_POC (IDC) then
1859 return True;
1860 end if;
1861
1862 Next (IDC);
1863 end loop;
1864
1865 return False;
1866 end;
1867
1868 when N_Range =>
1869 return Denotes_Discriminant (Low_Bound (Constr))
1870 or else
1871 Denotes_Discriminant (High_Bound (Constr));
1872
1873 when N_Range_Constraint =>
1874 return Denotes_Discriminant (Range_Expression (Constr));
1875
1876 when others =>
1877 return False;
1878 end case;
1879 end Contains_POC;
1880
1881 ----------------------
1882 -- Is_Known_Limited --
1883 ----------------------
1884
1885 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1886 P : constant Entity_Id := Etype (Typ);
1887 R : constant Entity_Id := Root_Type (Typ);
1888
1889 begin
1890 if Is_Limited_Record (Typ) then
1891 return True;
1892
1893 -- If the root type is limited (and not a limited interface) so is
1894 -- the current type.
1895
1896 elsif Is_Limited_Record (R)
1897 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1898 then
1899 return True;
1900
1901 -- Else the type may have a limited interface progenitor, but a
1902 -- limited record parent that is not an interface.
1903
1904 elsif R /= P
1905 and then Is_Limited_Record (P)
1906 and then not Is_Interface (P)
1907 then
1908 return True;
1909
1910 else
1911 return False;
1912 end if;
1913 end Is_Known_Limited;
1914
1915 -- Start of processing for Analyze_Component_Declaration
1916
1917 begin
1918 Generate_Definition (Id);
1919 Enter_Name (Id);
1920
1921 if Present (Typ) then
1922 T := Find_Type_Of_Object
1923 (Subtype_Indication (Component_Definition (N)), N);
1924
1925 -- Ada 2005 (AI-230): Access Definition case
1926
1927 else
1928 pragma Assert (Present
1929 (Access_Definition (Component_Definition (N))));
1930
1931 T := Access_Definition
1932 (Related_Nod => N,
1933 N => Access_Definition (Component_Definition (N)));
1934 Set_Is_Local_Anonymous_Access (T);
1935
1936 -- Ada 2005 (AI-254)
1937
1938 if Present (Access_To_Subprogram_Definition
1939 (Access_Definition (Component_Definition (N))))
1940 and then Protected_Present (Access_To_Subprogram_Definition
1941 (Access_Definition
1942 (Component_Definition (N))))
1943 then
1944 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1945 end if;
1946 end if;
1947
1948 -- If the subtype is a constrained subtype of the enclosing record,
1949 -- (which must have a partial view) the back-end does not properly
1950 -- handle the recursion. Rewrite the component declaration with an
1951 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1952 -- the tree directly because side effects have already been removed from
1953 -- discriminant constraints.
1954
1955 if Ekind (T) = E_Access_Subtype
1956 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1957 and then Comes_From_Source (T)
1958 and then Nkind (Parent (T)) = N_Subtype_Declaration
1959 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1960 then
1961 Rewrite
1962 (Subtype_Indication (Component_Definition (N)),
1963 New_Copy_Tree (Subtype_Indication (Parent (T))));
1964 T := Find_Type_Of_Object
1965 (Subtype_Indication (Component_Definition (N)), N);
1966 end if;
1967
1968 -- If the component declaration includes a default expression, then we
1969 -- check that the component is not of a limited type (RM 3.7(5)),
1970 -- and do the special preanalysis of the expression (see section on
1971 -- "Handling of Default and Per-Object Expressions" in the spec of
1972 -- package Sem).
1973
1974 if Present (E) then
1975 Preanalyze_Default_Expression (E, T);
1976 Check_Initialization (T, E);
1977
1978 if Ada_Version >= Ada_2005
1979 and then Ekind (T) = E_Anonymous_Access_Type
1980 and then Etype (E) /= Any_Type
1981 then
1982 -- Check RM 3.9.2(9): "if the expected type for an expression is
1983 -- an anonymous access-to-specific tagged type, then the object
1984 -- designated by the expression shall not be dynamically tagged
1985 -- unless it is a controlling operand in a call on a dispatching
1986 -- operation"
1987
1988 if Is_Tagged_Type (Directly_Designated_Type (T))
1989 and then
1990 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1991 and then
1992 Ekind (Directly_Designated_Type (Etype (E))) =
1993 E_Class_Wide_Type
1994 then
1995 Error_Msg_N
1996 ("access to specific tagged type required (RM 3.9.2(9))", E);
1997 end if;
1998
1999 -- (Ada 2005: AI-230): Accessibility check for anonymous
2000 -- components
2001
2002 if Type_Access_Level (Etype (E)) >
2003 Deepest_Type_Access_Level (T)
2004 then
2005 Error_Msg_N
2006 ("expression has deeper access level than component " &
2007 "(RM 3.10.2 (12.2))", E);
2008 end if;
2009
2010 -- The initialization expression is a reference to an access
2011 -- discriminant. The type of the discriminant is always deeper
2012 -- than any access type.
2013
2014 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2015 and then Is_Entity_Name (E)
2016 and then Ekind (Entity (E)) = E_In_Parameter
2017 and then Present (Discriminal_Link (Entity (E)))
2018 then
2019 Error_Msg_N
2020 ("discriminant has deeper accessibility level than target",
2021 E);
2022 end if;
2023 end if;
2024 end if;
2025
2026 -- Avoid reporting spurious errors if the component is initialized with
2027 -- a raise expression (which is legal in any expression context)
2028
2029 if Present (E)
2030 and then
2031 (Nkind (E) = N_Raise_Expression
2032 or else (Nkind (E) = N_Qualified_Expression
2033 and then Nkind (Expression (E)) = N_Raise_Expression))
2034 then
2035 null;
2036
2037 -- The parent type may be a private view with unknown discriminants,
2038 -- and thus unconstrained. Regular components must be constrained.
2039
2040 elsif not Is_Definite_Subtype (T)
2041 and then Chars (Id) /= Name_uParent
2042 then
2043 if Is_Class_Wide_Type (T) then
2044 Error_Msg_N
2045 ("class-wide subtype with unknown discriminants" &
2046 " in component declaration",
2047 Subtype_Indication (Component_Definition (N)));
2048 else
2049 Error_Msg_N
2050 ("unconstrained subtype in component declaration",
2051 Subtype_Indication (Component_Definition (N)));
2052 end if;
2053
2054 -- Components cannot be abstract, except for the special case of
2055 -- the _Parent field (case of extending an abstract tagged type)
2056
2057 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2058 Error_Msg_N ("type of a component cannot be abstract", N);
2059 end if;
2060
2061 Set_Etype (Id, T);
2062
2063 if Aliased_Present (Component_Definition (N)) then
2064 Set_Is_Aliased (Id);
2065
2066 -- AI12-001: All aliased objects are considered to be specified as
2067 -- independently addressable (RM C.6(8.1/4)).
2068
2069 Set_Is_Independent (Id);
2070 end if;
2071
2072 -- The component declaration may have a per-object constraint, set
2073 -- the appropriate flag in the defining identifier of the subtype.
2074
2075 if Present (Subtype_Indication (Component_Definition (N))) then
2076 declare
2077 Sindic : constant Node_Id :=
2078 Subtype_Indication (Component_Definition (N));
2079 begin
2080 if Nkind (Sindic) = N_Subtype_Indication
2081 and then Present (Constraint (Sindic))
2082 and then Contains_POC (Constraint (Sindic))
2083 then
2084 Set_Has_Per_Object_Constraint (Id);
2085 end if;
2086 end;
2087 end if;
2088
2089 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2090 -- out some static checks.
2091
2092 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2093 Null_Exclusion_Static_Checks (N);
2094 end if;
2095
2096 -- If this component is private (or depends on a private type), flag the
2097 -- record type to indicate that some operations are not available.
2098
2099 P := Private_Component (T);
2100
2101 if Present (P) then
2102
2103 -- Check for circular definitions
2104
2105 if P = Any_Type then
2106 Set_Etype (Id, Any_Type);
2107
2108 -- There is a gap in the visibility of operations only if the
2109 -- component type is not defined in the scope of the record type.
2110
2111 elsif Scope (P) = Scope (Current_Scope) then
2112 null;
2113
2114 elsif Is_Limited_Type (P) then
2115 Set_Is_Limited_Composite (Current_Scope);
2116
2117 else
2118 Set_Is_Private_Composite (Current_Scope);
2119 end if;
2120 end if;
2121
2122 if P /= Any_Type
2123 and then Is_Limited_Type (T)
2124 and then Chars (Id) /= Name_uParent
2125 and then Is_Tagged_Type (Current_Scope)
2126 then
2127 if Is_Derived_Type (Current_Scope)
2128 and then not Is_Known_Limited (Current_Scope)
2129 then
2130 Error_Msg_N
2131 ("extension of nonlimited type cannot have limited components",
2132 N);
2133
2134 if Is_Interface (Root_Type (Current_Scope)) then
2135 Error_Msg_N
2136 ("\limitedness is not inherited from limited interface", N);
2137 Error_Msg_N ("\add LIMITED to type indication", N);
2138 end if;
2139
2140 Explain_Limited_Type (T, N);
2141 Set_Etype (Id, Any_Type);
2142 Set_Is_Limited_Composite (Current_Scope, False);
2143
2144 elsif not Is_Derived_Type (Current_Scope)
2145 and then not Is_Limited_Record (Current_Scope)
2146 and then not Is_Concurrent_Type (Current_Scope)
2147 then
2148 Error_Msg_N
2149 ("nonlimited tagged type cannot have limited components", N);
2150 Explain_Limited_Type (T, N);
2151 Set_Etype (Id, Any_Type);
2152 Set_Is_Limited_Composite (Current_Scope, False);
2153 end if;
2154 end if;
2155
2156 -- If the component is an unconstrained task or protected type with
2157 -- discriminants, the component and the enclosing record are limited
2158 -- and the component is constrained by its default values. Compute
2159 -- its actual subtype, else it may be allocated the maximum size by
2160 -- the backend, and possibly overflow.
2161
2162 if Is_Concurrent_Type (T)
2163 and then not Is_Constrained (T)
2164 and then Has_Discriminants (T)
2165 and then not Has_Discriminants (Current_Scope)
2166 then
2167 declare
2168 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2169
2170 begin
2171 Set_Etype (Id, Act_T);
2172
2173 -- Rewrite component definition to use the constrained subtype
2174
2175 Rewrite (Component_Definition (N),
2176 Make_Component_Definition (Loc,
2177 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2178 end;
2179 end if;
2180
2181 Set_Original_Record_Component (Id, Id);
2182
2183 if Has_Aspects (N) then
2184 Analyze_Aspect_Specifications (N, Id);
2185 end if;
2186
2187 Analyze_Dimension (N);
2188 end Analyze_Component_Declaration;
2189
2190 --------------------------
2191 -- Analyze_Declarations --
2192 --------------------------
2193
2194 procedure Analyze_Declarations (L : List_Id) is
2195 Decl : Node_Id;
2196
2197 procedure Adjust_Decl;
2198 -- Adjust Decl not to include implicit label declarations, since these
2199 -- have strange Sloc values that result in elaboration check problems.
2200 -- (They have the sloc of the label as found in the source, and that
2201 -- is ahead of the current declarative part).
2202
2203 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2204 -- Create the subprogram bodies which verify the run-time semantics of
2205 -- the pragmas listed below for each elibigle type found in declarative
2206 -- list Decls. The pragmas are:
2207 --
2208 -- Default_Initial_Condition
2209 -- Invariant
2210 -- Type_Invariant
2211 --
2212 -- Context denotes the owner of the declarative list.
2213
2214 procedure Check_Entry_Contracts;
2215 -- Perform a preanalysis of the pre- and postconditions of an entry
2216 -- declaration. This must be done before full resolution and creation
2217 -- of the parameter block, etc. to catch illegal uses within the
2218 -- contract expression. Full analysis of the expression is done when
2219 -- the contract is processed.
2220
2221 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2222 -- Check if a nested package has entities within it that rely on library
2223 -- level private types where the full view has not been completed for
2224 -- the purposes of checking if it is acceptable to freeze an expression
2225 -- function at the point of declaration.
2226
2227 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2228 -- Determine whether Body_Decl denotes the body of a late controlled
2229 -- primitive (either Initialize, Adjust or Finalize). If this is the
2230 -- case, add a proper spec if the body lacks one. The spec is inserted
2231 -- before Body_Decl and immediately analyzed.
2232
2233 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2234 -- Spec_Id is the entity of a package that may define abstract states,
2235 -- and in the case of a child unit, whose ancestors may define abstract
2236 -- states. If the states have partial visible refinement, remove the
2237 -- partial visibility of each constituent at the end of the package
2238 -- spec and body declarations.
2239
2240 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2241 -- Spec_Id is the entity of a package that may define abstract states.
2242 -- If the states have visible refinement, remove the visibility of each
2243 -- constituent at the end of the package body declaration.
2244
2245 procedure Resolve_Aspects;
2246 -- Utility to resolve the expressions of aspects at the end of a list of
2247 -- declarations, or before a declaration that freezes previous entities,
2248 -- such as in a subprogram body.
2249
2250 -----------------
2251 -- Adjust_Decl --
2252 -----------------
2253
2254 procedure Adjust_Decl is
2255 begin
2256 while Present (Prev (Decl))
2257 and then Nkind (Decl) = N_Implicit_Label_Declaration
2258 loop
2259 Prev (Decl);
2260 end loop;
2261 end Adjust_Decl;
2262
2263 ----------------------------
2264 -- Build_Assertion_Bodies --
2265 ----------------------------
2266
2267 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2268 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2269 -- Create the subprogram bodies which verify the run-time semantics
2270 -- of the pragmas listed below for type Typ. The pragmas are:
2271 --
2272 -- Default_Initial_Condition
2273 -- Invariant
2274 -- Type_Invariant
2275
2276 -------------------------------------
2277 -- Build_Assertion_Bodies_For_Type --
2278 -------------------------------------
2279
2280 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2281 begin
2282 -- Preanalyze and resolve the Default_Initial_Condition assertion
2283 -- expression at the end of the declarations to catch any errors.
2284
2285 if Has_DIC (Typ) then
2286 Build_DIC_Procedure_Body (Typ);
2287 end if;
2288
2289 if Nkind (Context) = N_Package_Specification then
2290
2291 -- Preanalyze and resolve the class-wide invariants of an
2292 -- interface at the end of whichever declarative part has the
2293 -- interface type. Note that an interface may be declared in
2294 -- any non-package declarative part, but reaching the end of
2295 -- such a declarative part will always freeze the type and
2296 -- generate the invariant procedure (see Freeze_Type).
2297
2298 if Is_Interface (Typ) then
2299
2300 -- Interfaces are treated as the partial view of a private
2301 -- type, in order to achieve uniformity with the general
2302 -- case. As a result, an interface receives only a "partial"
2303 -- invariant procedure, which is never called.
2304
2305 if Has_Own_Invariants (Typ) then
2306 Build_Invariant_Procedure_Body
2307 (Typ => Typ,
2308 Partial_Invariant => True);
2309 end if;
2310
2311 -- Preanalyze and resolve the invariants of a private type
2312 -- at the end of the visible declarations to catch potential
2313 -- errors. Inherited class-wide invariants are not included
2314 -- because they have already been resolved.
2315
2316 elsif Decls = Visible_Declarations (Context)
2317 and then Ekind_In (Typ, E_Limited_Private_Type,
2318 E_Private_Type,
2319 E_Record_Type_With_Private)
2320 and then Has_Own_Invariants (Typ)
2321 then
2322 Build_Invariant_Procedure_Body
2323 (Typ => Typ,
2324 Partial_Invariant => True);
2325
2326 -- Preanalyze and resolve the invariants of a private type's
2327 -- full view at the end of the private declarations to catch
2328 -- potential errors.
2329
2330 elsif Decls = Private_Declarations (Context)
2331 and then (not Is_Private_Type (Typ)
2332 or else Present (Underlying_Full_View (Typ)))
2333 and then Has_Private_Declaration (Typ)
2334 and then Has_Invariants (Typ)
2335 then
2336 Build_Invariant_Procedure_Body (Typ);
2337 end if;
2338 end if;
2339 end Build_Assertion_Bodies_For_Type;
2340
2341 -- Local variables
2342
2343 Decl : Node_Id;
2344 Decl_Id : Entity_Id;
2345
2346 -- Start of processing for Build_Assertion_Bodies
2347
2348 begin
2349 Decl := First (Decls);
2350 while Present (Decl) loop
2351 if Is_Declaration (Decl) then
2352 Decl_Id := Defining_Entity (Decl);
2353
2354 if Is_Type (Decl_Id) then
2355 Build_Assertion_Bodies_For_Type (Decl_Id);
2356 end if;
2357 end if;
2358
2359 Next (Decl);
2360 end loop;
2361 end Build_Assertion_Bodies;
2362
2363 ---------------------------
2364 -- Check_Entry_Contracts --
2365 ---------------------------
2366
2367 procedure Check_Entry_Contracts is
2368 ASN : Node_Id;
2369 Ent : Entity_Id;
2370 Exp : Node_Id;
2371
2372 begin
2373 Ent := First_Entity (Current_Scope);
2374 while Present (Ent) loop
2375
2376 -- This only concerns entries with pre/postconditions
2377
2378 if Ekind (Ent) = E_Entry
2379 and then Present (Contract (Ent))
2380 and then Present (Pre_Post_Conditions (Contract (Ent)))
2381 then
2382 ASN := Pre_Post_Conditions (Contract (Ent));
2383 Push_Scope (Ent);
2384 Install_Formals (Ent);
2385
2386 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2387 -- is performed on a copy of the pragma expression, to prevent
2388 -- modifying the original expression.
2389
2390 while Present (ASN) loop
2391 if Nkind (ASN) = N_Pragma then
2392 Exp :=
2393 New_Copy_Tree
2394 (Expression
2395 (First (Pragma_Argument_Associations (ASN))));
2396 Set_Parent (Exp, ASN);
2397
2398 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2399 end if;
2400
2401 ASN := Next_Pragma (ASN);
2402 end loop;
2403
2404 End_Scope;
2405 end if;
2406
2407 Next_Entity (Ent);
2408 end loop;
2409 end Check_Entry_Contracts;
2410
2411 ----------------------------------
2412 -- Contains_Lib_Incomplete_Type --
2413 ----------------------------------
2414
2415 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2416 Curr : Entity_Id;
2417
2418 begin
2419 -- Avoid looking through scopes that do not meet the precondition of
2420 -- Pkg not being within a library unit spec.
2421
2422 if not Is_Compilation_Unit (Pkg)
2423 and then not Is_Generic_Instance (Pkg)
2424 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2425 then
2426 -- Loop through all entities in the current scope to identify
2427 -- an entity that depends on a private type.
2428
2429 Curr := First_Entity (Pkg);
2430 loop
2431 if Nkind (Curr) in N_Entity
2432 and then Depends_On_Private (Curr)
2433 then
2434 return True;
2435 end if;
2436
2437 exit when Last_Entity (Current_Scope) = Curr;
2438 Next_Entity (Curr);
2439 end loop;
2440 end if;
2441
2442 return False;
2443 end Contains_Lib_Incomplete_Type;
2444
2445 --------------------------------------
2446 -- Handle_Late_Controlled_Primitive --
2447 --------------------------------------
2448
2449 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2450 Body_Spec : constant Node_Id := Specification (Body_Decl);
2451 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2452 Loc : constant Source_Ptr := Sloc (Body_Id);
2453 Params : constant List_Id :=
2454 Parameter_Specifications (Body_Spec);
2455 Spec : Node_Id;
2456 Spec_Id : Entity_Id;
2457 Typ : Node_Id;
2458
2459 begin
2460 -- Consider only procedure bodies whose name matches one of the three
2461 -- controlled primitives.
2462
2463 if Nkind (Body_Spec) /= N_Procedure_Specification
2464 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2465 Name_Finalize,
2466 Name_Initialize)
2467 then
2468 return;
2469
2470 -- A controlled primitive must have exactly one formal which is not
2471 -- an anonymous access type.
2472
2473 elsif List_Length (Params) /= 1 then
2474 return;
2475 end if;
2476
2477 Typ := Parameter_Type (First (Params));
2478
2479 if Nkind (Typ) = N_Access_Definition then
2480 return;
2481 end if;
2482
2483 Find_Type (Typ);
2484
2485 -- The type of the formal must be derived from [Limited_]Controlled
2486
2487 if not Is_Controlled (Entity (Typ)) then
2488 return;
2489 end if;
2490
2491 -- Check whether a specification exists for this body. We do not
2492 -- analyze the spec of the body in full, because it will be analyzed
2493 -- again when the body is properly analyzed, and we cannot create
2494 -- duplicate entries in the formals chain. We look for an explicit
2495 -- specification because the body may be an overriding operation and
2496 -- an inherited spec may be present.
2497
2498 Spec_Id := Current_Entity (Body_Id);
2499
2500 while Present (Spec_Id) loop
2501 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2502 and then Scope (Spec_Id) = Current_Scope
2503 and then Present (First_Formal (Spec_Id))
2504 and then No (Next_Formal (First_Formal (Spec_Id)))
2505 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2506 and then Comes_From_Source (Spec_Id)
2507 then
2508 return;
2509 end if;
2510
2511 Spec_Id := Homonym (Spec_Id);
2512 end loop;
2513
2514 -- At this point the body is known to be a late controlled primitive.
2515 -- Generate a matching spec and insert it before the body. Note the
2516 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2517 -- tree in this case.
2518
2519 Spec := Copy_Separate_Tree (Body_Spec);
2520
2521 -- Ensure that the subprogram declaration does not inherit the null
2522 -- indicator from the body as we now have a proper spec/body pair.
2523
2524 Set_Null_Present (Spec, False);
2525
2526 -- Ensure that the freeze node is inserted after the declaration of
2527 -- the primitive since its expansion will freeze the primitive.
2528
2529 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2530
2531 Insert_Before_And_Analyze (Body_Decl, Decl);
2532 end Handle_Late_Controlled_Primitive;
2533
2534 ----------------------------------------
2535 -- Remove_Partial_Visible_Refinements --
2536 ----------------------------------------
2537
2538 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2539 State_Elmt : Elmt_Id;
2540 begin
2541 if Present (Abstract_States (Spec_Id)) then
2542 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2543 while Present (State_Elmt) loop
2544 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2545 Next_Elmt (State_Elmt);
2546 end loop;
2547 end if;
2548
2549 -- For a child unit, also hide the partial state refinement from
2550 -- ancestor packages.
2551
2552 if Is_Child_Unit (Spec_Id) then
2553 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2554 end if;
2555 end Remove_Partial_Visible_Refinements;
2556
2557 --------------------------------
2558 -- Remove_Visible_Refinements --
2559 --------------------------------
2560
2561 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2562 State_Elmt : Elmt_Id;
2563 begin
2564 if Present (Abstract_States (Spec_Id)) then
2565 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2566 while Present (State_Elmt) loop
2567 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2568 Next_Elmt (State_Elmt);
2569 end loop;
2570 end if;
2571 end Remove_Visible_Refinements;
2572
2573 ---------------------
2574 -- Resolve_Aspects --
2575 ---------------------
2576
2577 procedure Resolve_Aspects is
2578 E : Entity_Id;
2579
2580 begin
2581 E := First_Entity (Current_Scope);
2582 while Present (E) loop
2583 Resolve_Aspect_Expressions (E);
2584 Next_Entity (E);
2585 end loop;
2586 end Resolve_Aspects;
2587
2588 -- Local variables
2589
2590 Context : Node_Id := Empty;
2591 Ctrl_Typ : Entity_Id := Empty;
2592 Freeze_From : Entity_Id := Empty;
2593 Next_Decl : Node_Id;
2594
2595 -- Start of processing for Analyze_Declarations
2596
2597 begin
2598 Decl := First (L);
2599 while Present (Decl) loop
2600
2601 -- Complete analysis of declaration
2602
2603 Analyze (Decl);
2604 Next_Decl := Next (Decl);
2605
2606 if No (Freeze_From) then
2607 Freeze_From := First_Entity (Current_Scope);
2608 end if;
2609
2610 -- Remember if the declaration we just processed is the full type
2611 -- declaration of a controlled type (to handle late overriding of
2612 -- initialize, adjust or finalize).
2613
2614 if Nkind (Decl) = N_Full_Type_Declaration
2615 and then Is_Controlled (Defining_Identifier (Decl))
2616 then
2617 Ctrl_Typ := Defining_Identifier (Decl);
2618 end if;
2619
2620 -- At the end of a declarative part, freeze remaining entities
2621 -- declared in it. The end of the visible declarations of package
2622 -- specification is not the end of a declarative part if private
2623 -- declarations are present. The end of a package declaration is a
2624 -- freezing point only if it a library package. A task definition or
2625 -- protected type definition is not a freeze point either. Finally,
2626 -- we do not freeze entities in generic scopes, because there is no
2627 -- code generated for them and freeze nodes will be generated for
2628 -- the instance.
2629
2630 -- The end of a package instantiation is not a freeze point, but
2631 -- for now we make it one, because the generic body is inserted
2632 -- (currently) immediately after. Generic instantiations will not
2633 -- be a freeze point once delayed freezing of bodies is implemented.
2634 -- (This is needed in any case for early instantiations ???).
2635
2636 if No (Next_Decl) then
2637 if Nkind (Parent (L)) = N_Component_List then
2638 null;
2639
2640 elsif Nkind_In (Parent (L), N_Protected_Definition,
2641 N_Task_Definition)
2642 then
2643 Check_Entry_Contracts;
2644
2645 elsif Nkind (Parent (L)) /= N_Package_Specification then
2646 if Nkind (Parent (L)) = N_Package_Body then
2647 Freeze_From := First_Entity (Current_Scope);
2648 end if;
2649
2650 -- There may have been several freezing points previously,
2651 -- for example object declarations or subprogram bodies, but
2652 -- at the end of a declarative part we check freezing from
2653 -- the beginning, even though entities may already be frozen,
2654 -- in order to perform visibility checks on delayed aspects.
2655
2656 Adjust_Decl;
2657
2658 -- If the current scope is a generic subprogram body. Skip the
2659 -- generic formal parameters that are not frozen here.
2660
2661 if Is_Subprogram (Current_Scope)
2662 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2663 N_Generic_Subprogram_Declaration
2664 and then Present (First_Entity (Current_Scope))
2665 then
2666 while Is_Generic_Formal (Freeze_From) loop
2667 Next_Entity (Freeze_From);
2668 end loop;
2669
2670 Freeze_All (Freeze_From, Decl);
2671 Freeze_From := Last_Entity (Current_Scope);
2672
2673 else
2674 -- For declarations in a subprogram body there is no issue
2675 -- with name resolution in aspect specifications.
2676
2677 Freeze_All (First_Entity (Current_Scope), Decl);
2678 Freeze_From := Last_Entity (Current_Scope);
2679 end if;
2680
2681 -- Current scope is a package specification
2682
2683 elsif Scope (Current_Scope) /= Standard_Standard
2684 and then not Is_Child_Unit (Current_Scope)
2685 and then No (Generic_Parent (Parent (L)))
2686 then
2687 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2688 -- resolved at the end of the immediately enclosing declaration
2689 -- list (AI05-0183-1).
2690
2691 Resolve_Aspects;
2692
2693 elsif L /= Visible_Declarations (Parent (L))
2694 or else No (Private_Declarations (Parent (L)))
2695 or else Is_Empty_List (Private_Declarations (Parent (L)))
2696 then
2697 Adjust_Decl;
2698
2699 -- End of a package declaration
2700
2701 -- This is a freeze point because it is the end of a
2702 -- compilation unit.
2703
2704 Freeze_All (First_Entity (Current_Scope), Decl);
2705 Freeze_From := Last_Entity (Current_Scope);
2706
2707 -- At the end of the visible declarations the expressions in
2708 -- aspects of all entities declared so far must be resolved.
2709 -- The entities themselves might be frozen later, and the
2710 -- generated pragmas and attribute definition clauses analyzed
2711 -- in full at that point, but name resolution must take place
2712 -- now.
2713 -- In addition to being the proper semantics, this is mandatory
2714 -- within generic units, because global name capture requires
2715 -- those expressions to be analyzed, given that the generated
2716 -- pragmas do not appear in the original generic tree.
2717
2718 elsif Serious_Errors_Detected = 0 then
2719 Resolve_Aspects;
2720 end if;
2721
2722 -- If next node is a body then freeze all types before the body.
2723 -- An exception occurs for some expander-generated bodies. If these
2724 -- are generated at places where in general language rules would not
2725 -- allow a freeze point, then we assume that the expander has
2726 -- explicitly checked that all required types are properly frozen,
2727 -- and we do not cause general freezing here. This special circuit
2728 -- is used when the encountered body is marked as having already
2729 -- been analyzed.
2730
2731 -- In all other cases (bodies that come from source, and expander
2732 -- generated bodies that have not been analyzed yet), freeze all
2733 -- types now. Note that in the latter case, the expander must take
2734 -- care to attach the bodies at a proper place in the tree so as to
2735 -- not cause unwanted freezing at that point.
2736
2737 -- It is also necessary to check for a case where both an expression
2738 -- function is used and the current scope depends on an incomplete
2739 -- private type from a library unit, otherwise premature freezing of
2740 -- the private type will occur.
2741
2742 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2743 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2744 or else not Was_Expression_Function (Next_Decl))
2745 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2746 and then not Contains_Lib_Incomplete_Type
2747 (Current_Scope)))
2748 then
2749 -- When a controlled type is frozen, the expander generates stream
2750 -- and controlled-type support routines. If the freeze is caused
2751 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2752 -- expander will end up using the wrong version of these routines,
2753 -- as the body has not been processed yet. To remedy this, detect
2754 -- a late controlled primitive and create a proper spec for it.
2755 -- This ensures that the primitive will override its inherited
2756 -- counterpart before the freeze takes place.
2757
2758 -- If the declaration we just processed is a body, do not attempt
2759 -- to examine Next_Decl as the late primitive idiom can only apply
2760 -- to the first encountered body.
2761
2762 -- ??? A cleaner approach may be possible and/or this solution
2763 -- could be extended to general-purpose late primitives, TBD.
2764
2765 if Present (Ctrl_Typ) then
2766
2767 -- No need to continue searching for late body overriding if
2768 -- the controlled type is already frozen.
2769
2770 if Is_Frozen (Ctrl_Typ) then
2771 Ctrl_Typ := Empty;
2772
2773 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2774 Handle_Late_Controlled_Primitive (Next_Decl);
2775 end if;
2776 end if;
2777
2778 Adjust_Decl;
2779
2780 -- The generated body of an expression function does not freeze,
2781 -- unless it is a completion, in which case only the expression
2782 -- itself freezes. This is handled when the body itself is
2783 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2784
2785 Freeze_All (Freeze_From, Decl);
2786 Freeze_From := Last_Entity (Current_Scope);
2787 end if;
2788
2789 Decl := Next_Decl;
2790 end loop;
2791
2792 -- Post-freezing actions
2793
2794 if Present (L) then
2795 Context := Parent (L);
2796
2797 -- Certain contract annocations have forward visibility semantics and
2798 -- must be analyzed after all declarative items have been processed.
2799 -- This timing ensures that entities referenced by such contracts are
2800 -- visible.
2801
2802 -- Analyze the contract of an immediately enclosing package spec or
2803 -- body first because other contracts may depend on its information.
2804
2805 if Nkind (Context) = N_Package_Body then
2806 Analyze_Package_Body_Contract (Defining_Entity (Context));
2807
2808 elsif Nkind (Context) = N_Package_Specification then
2809 Analyze_Package_Contract (Defining_Entity (Context));
2810 end if;
2811
2812 -- Analyze the contracts of various constructs in the declarative
2813 -- list.
2814
2815 Analyze_Contracts (L);
2816
2817 if Nkind (Context) = N_Package_Body then
2818
2819 -- Ensure that all abstract states and objects declared in the
2820 -- state space of a package body are utilized as constituents.
2821
2822 Check_Unused_Body_States (Defining_Entity (Context));
2823
2824 -- State refinements are visible up to the end of the package body
2825 -- declarations. Hide the state refinements from visibility to
2826 -- restore the original state conditions.
2827
2828 Remove_Visible_Refinements (Corresponding_Spec (Context));
2829 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2830
2831 elsif Nkind (Context) = N_Package_Specification then
2832
2833 -- Partial state refinements are visible up to the end of the
2834 -- package spec declarations. Hide the partial state refinements
2835 -- from visibility to restore the original state conditions.
2836
2837 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2838 end if;
2839
2840 -- Verify that all abstract states found in any package declared in
2841 -- the input declarative list have proper refinements. The check is
2842 -- performed only when the context denotes a block, entry, package,
2843 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2844
2845 Check_State_Refinements (Context);
2846
2847 -- Create the subprogram bodies which verify the run-time semantics
2848 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2849 -- types within the current declarative list. This ensures that all
2850 -- assertion expressions are preanalyzed and resolved at the end of
2851 -- the declarative part. Note that the resolution happens even when
2852 -- freezing does not take place.
2853
2854 Build_Assertion_Bodies (L, Context);
2855 end if;
2856 end Analyze_Declarations;
2857
2858 -----------------------------------
2859 -- Analyze_Full_Type_Declaration --
2860 -----------------------------------
2861
2862 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2863 Def : constant Node_Id := Type_Definition (N);
2864 Def_Id : constant Entity_Id := Defining_Identifier (N);
2865 T : Entity_Id;
2866 Prev : Entity_Id;
2867
2868 Is_Remote : constant Boolean :=
2869 (Is_Remote_Types (Current_Scope)
2870 or else Is_Remote_Call_Interface (Current_Scope))
2871 and then not (In_Private_Part (Current_Scope)
2872 or else In_Package_Body (Current_Scope));
2873
2874 procedure Check_Nonoverridable_Aspects;
2875 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2876 -- be overridden, and can only be confirmed on derivation.
2877
2878 procedure Check_Ops_From_Incomplete_Type;
2879 -- If there is a tagged incomplete partial view of the type, traverse
2880 -- the primitives of the incomplete view and change the type of any
2881 -- controlling formals and result to indicate the full view. The
2882 -- primitives will be added to the full type's primitive operations
2883 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2884 -- is called from Process_Incomplete_Dependents).
2885
2886 ----------------------------------
2887 -- Check_Nonoverridable_Aspects --
2888 ----------------------------------
2889
2890 procedure Check_Nonoverridable_Aspects is
2891 function Get_Aspect_Spec
2892 (Specs : List_Id;
2893 Aspect_Name : Name_Id) return Node_Id;
2894 -- Check whether a list of aspect specifications includes an entry
2895 -- for a specific aspect. The list is either that of a partial or
2896 -- a full view.
2897
2898 ---------------------
2899 -- Get_Aspect_Spec --
2900 ---------------------
2901
2902 function Get_Aspect_Spec
2903 (Specs : List_Id;
2904 Aspect_Name : Name_Id) return Node_Id
2905 is
2906 Spec : Node_Id;
2907
2908 begin
2909 Spec := First (Specs);
2910 while Present (Spec) loop
2911 if Chars (Identifier (Spec)) = Aspect_Name then
2912 return Spec;
2913 end if;
2914 Next (Spec);
2915 end loop;
2916
2917 return Empty;
2918 end Get_Aspect_Spec;
2919
2920 -- Local variables
2921
2922 Prev_Aspects : constant List_Id :=
2923 Aspect_Specifications (Parent (Def_Id));
2924 Par_Type : Entity_Id;
2925 Prev_Aspect : Node_Id;
2926
2927 -- Start of processing for Check_Nonoverridable_Aspects
2928
2929 begin
2930 -- Get parent type of derived type. Note that Prev is the entity in
2931 -- the partial declaration, but its contents are now those of full
2932 -- view, while Def_Id reflects the partial view.
2933
2934 if Is_Private_Type (Def_Id) then
2935 Par_Type := Etype (Full_View (Def_Id));
2936 else
2937 Par_Type := Etype (Def_Id);
2938 end if;
2939
2940 -- If there is an inherited Implicit_Dereference, verify that it is
2941 -- made explicit in the partial view.
2942
2943 if Has_Discriminants (Base_Type (Par_Type))
2944 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2945 and then Present (Discriminant_Specifications (Parent (Prev)))
2946 and then Present (Get_Reference_Discriminant (Par_Type))
2947 then
2948 Prev_Aspect :=
2949 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2950
2951 if No (Prev_Aspect)
2952 and then Present
2953 (Discriminant_Specifications
2954 (Original_Node (Parent (Prev))))
2955 then
2956 Error_Msg_N
2957 ("type does not inherit implicit dereference", Prev);
2958
2959 else
2960 -- If one of the views has the aspect specified, verify that it
2961 -- is consistent with that of the parent.
2962
2963 declare
2964 Cur_Discr : constant Entity_Id :=
2965 Get_Reference_Discriminant (Prev);
2966 Par_Discr : constant Entity_Id :=
2967 Get_Reference_Discriminant (Par_Type);
2968
2969 begin
2970 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2971 Error_Msg_N
2972 ("aspect inconsistent with that of parent", N);
2973 end if;
2974
2975 -- Check that specification in partial view matches the
2976 -- inherited aspect. Compare names directly because aspect
2977 -- expression may not be analyzed.
2978
2979 if Present (Prev_Aspect)
2980 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2981 and then Chars (Expression (Prev_Aspect)) /=
2982 Chars (Cur_Discr)
2983 then
2984 Error_Msg_N
2985 ("aspect inconsistent with that of parent", N);
2986 end if;
2987 end;
2988 end if;
2989 end if;
2990
2991 -- TBD : other nonoverridable aspects.
2992 end Check_Nonoverridable_Aspects;
2993
2994 ------------------------------------
2995 -- Check_Ops_From_Incomplete_Type --
2996 ------------------------------------
2997
2998 procedure Check_Ops_From_Incomplete_Type is
2999 Elmt : Elmt_Id;
3000 Formal : Entity_Id;
3001 Op : Entity_Id;
3002
3003 begin
3004 if Prev /= T
3005 and then Ekind (Prev) = E_Incomplete_Type
3006 and then Is_Tagged_Type (Prev)
3007 and then Is_Tagged_Type (T)
3008 then
3009 Elmt := First_Elmt (Primitive_Operations (Prev));
3010 while Present (Elmt) loop
3011 Op := Node (Elmt);
3012
3013 Formal := First_Formal (Op);
3014 while Present (Formal) loop
3015 if Etype (Formal) = Prev then
3016 Set_Etype (Formal, T);
3017 end if;
3018
3019 Next_Formal (Formal);
3020 end loop;
3021
3022 if Etype (Op) = Prev then
3023 Set_Etype (Op, T);
3024 end if;
3025
3026 Next_Elmt (Elmt);
3027 end loop;
3028 end if;
3029 end Check_Ops_From_Incomplete_Type;
3030
3031 -- Start of processing for Analyze_Full_Type_Declaration
3032
3033 begin
3034 Prev := Find_Type_Name (N);
3035
3036 -- The full view, if present, now points to the current type. If there
3037 -- is an incomplete partial view, set a link to it, to simplify the
3038 -- retrieval of primitive operations of the type.
3039
3040 -- Ada 2005 (AI-50217): If the type was previously decorated when
3041 -- imported through a LIMITED WITH clause, it appears as incomplete
3042 -- but has no full view.
3043
3044 if Ekind (Prev) = E_Incomplete_Type
3045 and then Present (Full_View (Prev))
3046 then
3047 T := Full_View (Prev);
3048 Set_Incomplete_View (N, Parent (Prev));
3049 else
3050 T := Prev;
3051 end if;
3052
3053 Set_Is_Pure (T, Is_Pure (Current_Scope));
3054
3055 -- We set the flag Is_First_Subtype here. It is needed to set the
3056 -- corresponding flag for the Implicit class-wide-type created
3057 -- during tagged types processing.
3058
3059 Set_Is_First_Subtype (T, True);
3060
3061 -- Only composite types other than array types are allowed to have
3062 -- discriminants.
3063
3064 case Nkind (Def) is
3065
3066 -- For derived types, the rule will be checked once we've figured
3067 -- out the parent type.
3068
3069 when N_Derived_Type_Definition =>
3070 null;
3071
3072 -- For record types, discriminants are allowed.
3073
3074 when N_Record_Definition =>
3075 null;
3076
3077 when others =>
3078 if Present (Discriminant_Specifications (N)) then
3079 Error_Msg_N
3080 ("elementary or array type cannot have discriminants",
3081 Defining_Identifier
3082 (First (Discriminant_Specifications (N))));
3083 end if;
3084 end case;
3085
3086 -- Elaborate the type definition according to kind, and generate
3087 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3088 -- already done (this happens during the reanalysis that follows a call
3089 -- to the high level optimizer).
3090
3091 if not Analyzed (T) then
3092 Set_Analyzed (T);
3093
3094 -- Set the SPARK mode from the current context
3095
3096 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3097 Set_SPARK_Pragma_Inherited (T);
3098
3099 case Nkind (Def) is
3100 when N_Access_To_Subprogram_Definition =>
3101 Access_Subprogram_Declaration (T, Def);
3102
3103 -- If this is a remote access to subprogram, we must create the
3104 -- equivalent fat pointer type, and related subprograms.
3105
3106 if Is_Remote then
3107 Process_Remote_AST_Declaration (N);
3108 end if;
3109
3110 -- Validate categorization rule against access type declaration
3111 -- usually a violation in Pure unit, Shared_Passive unit.
3112
3113 Validate_Access_Type_Declaration (T, N);
3114
3115 when N_Access_To_Object_Definition =>
3116 Access_Type_Declaration (T, Def);
3117
3118 -- Validate categorization rule against access type declaration
3119 -- usually a violation in Pure unit, Shared_Passive unit.
3120
3121 Validate_Access_Type_Declaration (T, N);
3122
3123 -- If we are in a Remote_Call_Interface package and define a
3124 -- RACW, then calling stubs and specific stream attributes
3125 -- must be added.
3126
3127 if Is_Remote
3128 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3129 then
3130 Add_RACW_Features (Def_Id);
3131 end if;
3132
3133 when N_Array_Type_Definition =>
3134 Array_Type_Declaration (T, Def);
3135
3136 when N_Derived_Type_Definition =>
3137 Derived_Type_Declaration (T, N, T /= Def_Id);
3138
3139 -- Inherit predicates from parent, and protect against illegal
3140 -- derivations.
3141
3142 if Is_Type (T) and then Has_Predicates (T) then
3143 Set_Has_Predicates (Def_Id);
3144 end if;
3145
3146 -- Save the scenario for examination by the ABE Processing
3147 -- phase.
3148
3149 Record_Elaboration_Scenario (N);
3150
3151 when N_Enumeration_Type_Definition =>
3152 Enumeration_Type_Declaration (T, Def);
3153
3154 when N_Floating_Point_Definition =>
3155 Floating_Point_Type_Declaration (T, Def);
3156
3157 when N_Decimal_Fixed_Point_Definition =>
3158 Decimal_Fixed_Point_Type_Declaration (T, Def);
3159
3160 when N_Ordinary_Fixed_Point_Definition =>
3161 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3162
3163 when N_Signed_Integer_Type_Definition =>
3164 Signed_Integer_Type_Declaration (T, Def);
3165
3166 when N_Modular_Type_Definition =>
3167 Modular_Type_Declaration (T, Def);
3168
3169 when N_Record_Definition =>
3170 Record_Type_Declaration (T, N, Prev);
3171
3172 -- If declaration has a parse error, nothing to elaborate.
3173
3174 when N_Error =>
3175 null;
3176
3177 when others =>
3178 raise Program_Error;
3179 end case;
3180 end if;
3181
3182 if Etype (T) = Any_Type then
3183 return;
3184 end if;
3185
3186 -- Some common processing for all types
3187
3188 Set_Depends_On_Private (T, Has_Private_Component (T));
3189 Check_Ops_From_Incomplete_Type;
3190
3191 -- Both the declared entity, and its anonymous base type if one was
3192 -- created, need freeze nodes allocated.
3193
3194 declare
3195 B : constant Entity_Id := Base_Type (T);
3196
3197 begin
3198 -- In the case where the base type differs from the first subtype, we
3199 -- pre-allocate a freeze node, and set the proper link to the first
3200 -- subtype. Freeze_Entity will use this preallocated freeze node when
3201 -- it freezes the entity.
3202
3203 -- This does not apply if the base type is a generic type, whose
3204 -- declaration is independent of the current derived definition.
3205
3206 if B /= T and then not Is_Generic_Type (B) then
3207 Ensure_Freeze_Node (B);
3208 Set_First_Subtype_Link (Freeze_Node (B), T);
3209 end if;
3210
3211 -- A type that is imported through a limited_with clause cannot
3212 -- generate any code, and thus need not be frozen. However, an access
3213 -- type with an imported designated type needs a finalization list,
3214 -- which may be referenced in some other package that has non-limited
3215 -- visibility on the designated type. Thus we must create the
3216 -- finalization list at the point the access type is frozen, to
3217 -- prevent unsatisfied references at link time.
3218
3219 if not From_Limited_With (T) or else Is_Access_Type (T) then
3220 Set_Has_Delayed_Freeze (T);
3221 end if;
3222 end;
3223
3224 -- Case where T is the full declaration of some private type which has
3225 -- been swapped in Defining_Identifier (N).
3226
3227 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3228 Process_Full_View (N, T, Def_Id);
3229
3230 -- Record the reference. The form of this is a little strange, since
3231 -- the full declaration has been swapped in. So the first parameter
3232 -- here represents the entity to which a reference is made which is
3233 -- the "real" entity, i.e. the one swapped in, and the second
3234 -- parameter provides the reference location.
3235
3236 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3237 -- since we don't want a complaint about the full type being an
3238 -- unwanted reference to the private type
3239
3240 declare
3241 B : constant Boolean := Has_Pragma_Unreferenced (T);
3242 begin
3243 Set_Has_Pragma_Unreferenced (T, False);
3244 Generate_Reference (T, T, 'c');
3245 Set_Has_Pragma_Unreferenced (T, B);
3246 end;
3247
3248 Set_Completion_Referenced (Def_Id);
3249
3250 -- For completion of incomplete type, process incomplete dependents
3251 -- and always mark the full type as referenced (it is the incomplete
3252 -- type that we get for any real reference).
3253
3254 elsif Ekind (Prev) = E_Incomplete_Type then
3255 Process_Incomplete_Dependents (N, T, Prev);
3256 Generate_Reference (Prev, Def_Id, 'c');
3257 Set_Completion_Referenced (Def_Id);
3258
3259 -- If not private type or incomplete type completion, this is a real
3260 -- definition of a new entity, so record it.
3261
3262 else
3263 Generate_Definition (Def_Id);
3264 end if;
3265
3266 -- Propagate any pending access types whose finalization masters need to
3267 -- be fully initialized from the partial to the full view. Guard against
3268 -- an illegal full view that remains unanalyzed.
3269
3270 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3271 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3272 end if;
3273
3274 if Chars (Scope (Def_Id)) = Name_System
3275 and then Chars (Def_Id) = Name_Address
3276 and then In_Predefined_Unit (N)
3277 then
3278 Set_Is_Descendant_Of_Address (Def_Id);
3279 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3280 Set_Is_Descendant_Of_Address (Prev);
3281 end if;
3282
3283 Set_Optimize_Alignment_Flags (Def_Id);
3284 Check_Eliminated (Def_Id);
3285
3286 -- If the declaration is a completion and aspects are present, apply
3287 -- them to the entity for the type which is currently the partial
3288 -- view, but which is the one that will be frozen.
3289
3290 if Has_Aspects (N) then
3291
3292 -- In most cases the partial view is a private type, and both views
3293 -- appear in different declarative parts. In the unusual case where
3294 -- the partial view is incomplete, perform the analysis on the
3295 -- full view, to prevent freezing anomalies with the corresponding
3296 -- class-wide type, which otherwise might be frozen before the
3297 -- dispatch table is built.
3298
3299 if Prev /= Def_Id
3300 and then Ekind (Prev) /= E_Incomplete_Type
3301 then
3302 Analyze_Aspect_Specifications (N, Prev);
3303
3304 -- Normal case
3305
3306 else
3307 Analyze_Aspect_Specifications (N, Def_Id);
3308 end if;
3309 end if;
3310
3311 if Is_Derived_Type (Prev)
3312 and then Def_Id /= Prev
3313 then
3314 Check_Nonoverridable_Aspects;
3315 end if;
3316 end Analyze_Full_Type_Declaration;
3317
3318 ----------------------------------
3319 -- Analyze_Incomplete_Type_Decl --
3320 ----------------------------------
3321
3322 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3323 F : constant Boolean := Is_Pure (Current_Scope);
3324 T : Entity_Id;
3325
3326 begin
3327 Generate_Definition (Defining_Identifier (N));
3328
3329 -- Process an incomplete declaration. The identifier must not have been
3330 -- declared already in the scope. However, an incomplete declaration may
3331 -- appear in the private part of a package, for a private type that has
3332 -- already been declared.
3333
3334 -- In this case, the discriminants (if any) must match
3335
3336 T := Find_Type_Name (N);
3337
3338 Set_Ekind (T, E_Incomplete_Type);
3339 Set_Etype (T, T);
3340 Set_Is_First_Subtype (T);
3341 Init_Size_Align (T);
3342
3343 -- Set the SPARK mode from the current context
3344
3345 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3346 Set_SPARK_Pragma_Inherited (T);
3347
3348 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3349 -- incomplete types.
3350
3351 if Tagged_Present (N) then
3352 Set_Is_Tagged_Type (T, True);
3353 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3354 Make_Class_Wide_Type (T);
3355 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3356 end if;
3357
3358 Set_Stored_Constraint (T, No_Elist);
3359
3360 if Present (Discriminant_Specifications (N)) then
3361 Push_Scope (T);
3362 Process_Discriminants (N);
3363 End_Scope;
3364 end if;
3365
3366 -- If the type has discriminants, nontrivial subtypes may be declared
3367 -- before the full view of the type. The full views of those subtypes
3368 -- will be built after the full view of the type.
3369
3370 Set_Private_Dependents (T, New_Elmt_List);
3371 Set_Is_Pure (T, F);
3372 end Analyze_Incomplete_Type_Decl;
3373
3374 -----------------------------------
3375 -- Analyze_Interface_Declaration --
3376 -----------------------------------
3377
3378 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3379 CW : constant Entity_Id := Class_Wide_Type (T);
3380
3381 begin
3382 Set_Is_Tagged_Type (T);
3383 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3384
3385 Set_Is_Limited_Record (T, Limited_Present (Def)
3386 or else Task_Present (Def)
3387 or else Protected_Present (Def)
3388 or else Synchronized_Present (Def));
3389
3390 -- Type is abstract if full declaration carries keyword, or if previous
3391 -- partial view did.
3392
3393 Set_Is_Abstract_Type (T);
3394 Set_Is_Interface (T);
3395
3396 -- Type is a limited interface if it includes the keyword limited, task,
3397 -- protected, or synchronized.
3398
3399 Set_Is_Limited_Interface
3400 (T, Limited_Present (Def)
3401 or else Protected_Present (Def)
3402 or else Synchronized_Present (Def)
3403 or else Task_Present (Def));
3404
3405 Set_Interfaces (T, New_Elmt_List);
3406 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3407
3408 -- Complete the decoration of the class-wide entity if it was already
3409 -- built (i.e. during the creation of the limited view)
3410
3411 if Present (CW) then
3412 Set_Is_Interface (CW);
3413 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3414 end if;
3415
3416 -- Check runtime support for synchronized interfaces
3417
3418 if (Is_Task_Interface (T)
3419 or else Is_Protected_Interface (T)
3420 or else Is_Synchronized_Interface (T))
3421 and then not RTE_Available (RE_Select_Specific_Data)
3422 then
3423 Error_Msg_CRT ("synchronized interfaces", T);
3424 end if;
3425 end Analyze_Interface_Declaration;
3426
3427 -----------------------------
3428 -- Analyze_Itype_Reference --
3429 -----------------------------
3430
3431 -- Nothing to do. This node is placed in the tree only for the benefit of
3432 -- back end processing, and has no effect on the semantic processing.
3433
3434 procedure Analyze_Itype_Reference (N : Node_Id) is
3435 begin
3436 pragma Assert (Is_Itype (Itype (N)));
3437 null;
3438 end Analyze_Itype_Reference;
3439
3440 --------------------------------
3441 -- Analyze_Number_Declaration --
3442 --------------------------------
3443
3444 procedure Analyze_Number_Declaration (N : Node_Id) is
3445 E : constant Node_Id := Expression (N);
3446 Id : constant Entity_Id := Defining_Identifier (N);
3447 Index : Interp_Index;
3448 It : Interp;
3449 T : Entity_Id;
3450
3451 begin
3452 Generate_Definition (Id);
3453 Enter_Name (Id);
3454
3455 -- This is an optimization of a common case of an integer literal
3456
3457 if Nkind (E) = N_Integer_Literal then
3458 Set_Is_Static_Expression (E, True);
3459 Set_Etype (E, Universal_Integer);
3460
3461 Set_Etype (Id, Universal_Integer);
3462 Set_Ekind (Id, E_Named_Integer);
3463 Set_Is_Frozen (Id, True);
3464
3465 Set_Debug_Info_Needed (Id);
3466 return;
3467 end if;
3468
3469 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3470
3471 -- Process expression, replacing error by integer zero, to avoid
3472 -- cascaded errors or aborts further along in the processing
3473
3474 -- Replace Error by integer zero, which seems least likely to cause
3475 -- cascaded errors.
3476
3477 if E = Error then
3478 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3479 Set_Error_Posted (E);
3480 end if;
3481
3482 Analyze (E);
3483
3484 -- Verify that the expression is static and numeric. If
3485 -- the expression is overloaded, we apply the preference
3486 -- rule that favors root numeric types.
3487
3488 if not Is_Overloaded (E) then
3489 T := Etype (E);
3490 if Has_Dynamic_Predicate_Aspect (T) then
3491 Error_Msg_N
3492 ("subtype has dynamic predicate, "
3493 & "not allowed in number declaration", N);
3494 end if;
3495
3496 else
3497 T := Any_Type;
3498
3499 Get_First_Interp (E, Index, It);
3500 while Present (It.Typ) loop
3501 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3502 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3503 then
3504 if T = Any_Type then
3505 T := It.Typ;
3506
3507 elsif It.Typ = Universal_Real
3508 or else
3509 It.Typ = Universal_Integer
3510 then
3511 -- Choose universal interpretation over any other
3512
3513 T := It.Typ;
3514 exit;
3515 end if;
3516 end if;
3517
3518 Get_Next_Interp (Index, It);
3519 end loop;
3520 end if;
3521
3522 if Is_Integer_Type (T) then
3523 Resolve (E, T);
3524 Set_Etype (Id, Universal_Integer);
3525 Set_Ekind (Id, E_Named_Integer);
3526
3527 elsif Is_Real_Type (T) then
3528
3529 -- Because the real value is converted to universal_real, this is a
3530 -- legal context for a universal fixed expression.
3531
3532 if T = Universal_Fixed then
3533 declare
3534 Loc : constant Source_Ptr := Sloc (N);
3535 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3536 Subtype_Mark =>
3537 New_Occurrence_Of (Universal_Real, Loc),
3538 Expression => Relocate_Node (E));
3539
3540 begin
3541 Rewrite (E, Conv);
3542 Analyze (E);
3543 end;
3544
3545 elsif T = Any_Fixed then
3546 Error_Msg_N ("illegal context for mixed mode operation", E);
3547
3548 -- Expression is of the form : universal_fixed * integer. Try to
3549 -- resolve as universal_real.
3550
3551 T := Universal_Real;
3552 Set_Etype (E, T);
3553 end if;
3554
3555 Resolve (E, T);
3556 Set_Etype (Id, Universal_Real);
3557 Set_Ekind (Id, E_Named_Real);
3558
3559 else
3560 Wrong_Type (E, Any_Numeric);
3561 Resolve (E, T);
3562
3563 Set_Etype (Id, T);
3564 Set_Ekind (Id, E_Constant);
3565 Set_Never_Set_In_Source (Id, True);
3566 Set_Is_True_Constant (Id, True);
3567 return;
3568 end if;
3569
3570 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3571 Set_Etype (E, Etype (Id));
3572 end if;
3573
3574 if not Is_OK_Static_Expression (E) then
3575 Flag_Non_Static_Expr
3576 ("non-static expression used in number declaration!", E);
3577 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3578 Set_Etype (E, Any_Type);
3579 end if;
3580
3581 Analyze_Dimension (N);
3582 end Analyze_Number_Declaration;
3583
3584 --------------------------------
3585 -- Analyze_Object_Declaration --
3586 --------------------------------
3587
3588 -- WARNING: This routine manages Ghost regions. Return statements must be
3589 -- replaced by gotos which jump to the end of the routine and restore the
3590 -- Ghost mode.
3591
3592 procedure Analyze_Object_Declaration (N : Node_Id) is
3593 Loc : constant Source_Ptr := Sloc (N);
3594 Id : constant Entity_Id := Defining_Identifier (N);
3595 Next_Decl : constant Node_Id := Next (N);
3596
3597 Act_T : Entity_Id;
3598 T : Entity_Id;
3599
3600 E : Node_Id := Expression (N);
3601 -- E is set to Expression (N) throughout this routine. When Expression
3602 -- (N) is modified, E is changed accordingly.
3603
3604 Prev_Entity : Entity_Id := Empty;
3605
3606 procedure Check_Dynamic_Object (Typ : Entity_Id);
3607 -- A library-level object with nonstatic discriminant constraints may
3608 -- require dynamic allocation. The declaration is illegal if the
3609 -- profile includes the restriction No_Implicit_Heap_Allocations.
3610
3611 procedure Check_For_Null_Excluding_Components
3612 (Obj_Typ : Entity_Id;
3613 Obj_Decl : Node_Id);
3614 -- Verify that each null-excluding component of object declaration
3615 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3616 -- a compile-time warning if this is not the case.
3617
3618 function Count_Tasks (T : Entity_Id) return Uint;
3619 -- This function is called when a non-generic library level object of a
3620 -- task type is declared. Its function is to count the static number of
3621 -- tasks declared within the type (it is only called if Has_Task is set
3622 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3623 -- or a variant record type is encountered, Check_Restriction is called
3624 -- indicating the count is unknown.
3625
3626 function Delayed_Aspect_Present return Boolean;
3627 -- If the declaration has an expression that is an aggregate, and it
3628 -- has aspects that require delayed analysis, the resolution of the
3629 -- aggregate must be deferred to the freeze point of the object. This
3630 -- special processing was created for address clauses, but it must
3631 -- also apply to Alignment. This must be done before the aspect
3632 -- specifications are analyzed because we must handle the aggregate
3633 -- before the analysis of the object declaration is complete.
3634
3635 -- Any other relevant delayed aspects on object declarations ???
3636
3637 --------------------------
3638 -- Check_Dynamic_Object --
3639 --------------------------
3640
3641 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3642 Comp : Entity_Id;
3643 Obj_Type : Entity_Id;
3644
3645 begin
3646 Obj_Type := Typ;
3647
3648 if Is_Private_Type (Obj_Type)
3649 and then Present (Full_View (Obj_Type))
3650 then
3651 Obj_Type := Full_View (Obj_Type);
3652 end if;
3653
3654 if Known_Static_Esize (Obj_Type) then
3655 return;
3656 end if;
3657
3658 if Restriction_Active (No_Implicit_Heap_Allocations)
3659 and then Expander_Active
3660 and then Has_Discriminants (Obj_Type)
3661 then
3662 Comp := First_Component (Obj_Type);
3663 while Present (Comp) loop
3664 if Known_Static_Esize (Etype (Comp))
3665 or else Size_Known_At_Compile_Time (Etype (Comp))
3666 then
3667 null;
3668
3669 elsif not Discriminated_Size (Comp)
3670 and then Comes_From_Source (Comp)
3671 then
3672 Error_Msg_NE
3673 ("component& of non-static size will violate restriction "
3674 & "No_Implicit_Heap_Allocation?", N, Comp);
3675
3676 elsif Is_Record_Type (Etype (Comp)) then
3677 Check_Dynamic_Object (Etype (Comp));
3678 end if;
3679
3680 Next_Component (Comp);
3681 end loop;
3682 end if;
3683 end Check_Dynamic_Object;
3684
3685 -----------------------------------------
3686 -- Check_For_Null_Excluding_Components --
3687 -----------------------------------------
3688
3689 procedure Check_For_Null_Excluding_Components
3690 (Obj_Typ : Entity_Id;
3691 Obj_Decl : Node_Id)
3692 is
3693 procedure Check_Component
3694 (Comp_Typ : Entity_Id;
3695 Comp_Decl : Node_Id := Empty;
3696 Array_Comp : Boolean := False);
3697 -- Apply a compile-time null-exclusion check on a component denoted
3698 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3699 -- subcomponents (if any).
3700
3701 ---------------------
3702 -- Check_Component --
3703 ---------------------
3704
3705 procedure Check_Component
3706 (Comp_Typ : Entity_Id;
3707 Comp_Decl : Node_Id := Empty;
3708 Array_Comp : Boolean := False)
3709 is
3710 Comp : Entity_Id;
3711 T : Entity_Id;
3712
3713 begin
3714 -- Do not consider internally-generated components or those that
3715 -- are already initialized.
3716
3717 if Present (Comp_Decl)
3718 and then (not Comes_From_Source (Comp_Decl)
3719 or else Present (Expression (Comp_Decl)))
3720 then
3721 return;
3722 end if;
3723
3724 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3725 and then Present (Full_View (Comp_Typ))
3726 then
3727 T := Full_View (Comp_Typ);
3728 else
3729 T := Comp_Typ;
3730 end if;
3731
3732 -- Verify a component of a null-excluding access type
3733
3734 if Is_Access_Type (T)
3735 and then Can_Never_Be_Null (T)
3736 then
3737 if Comp_Decl = Obj_Decl then
3738 Null_Exclusion_Static_Checks
3739 (N => Obj_Decl,
3740 Comp => Empty,
3741 Array_Comp => Array_Comp);
3742
3743 else
3744 Null_Exclusion_Static_Checks
3745 (N => Obj_Decl,
3746 Comp => Comp_Decl,
3747 Array_Comp => Array_Comp);
3748 end if;
3749
3750 -- Check array components
3751
3752 elsif Is_Array_Type (T) then
3753
3754 -- There is no suitable component when the object is of an
3755 -- array type. However, a namable component may appear at some
3756 -- point during the recursive inspection, but not at the top
3757 -- level. At the top level just indicate array component case.
3758
3759 if Comp_Decl = Obj_Decl then
3760 Check_Component (Component_Type (T), Array_Comp => True);
3761 else
3762 Check_Component (Component_Type (T), Comp_Decl);
3763 end if;
3764
3765 -- Verify all components of type T
3766
3767 -- Note: No checks are performed on types with discriminants due
3768 -- to complexities involving variants. ???
3769
3770 elsif (Is_Concurrent_Type (T)
3771 or else Is_Incomplete_Or_Private_Type (T)
3772 or else Is_Record_Type (T))
3773 and then not Has_Discriminants (T)
3774 then
3775 Comp := First_Component (T);
3776 while Present (Comp) loop
3777 Check_Component (Etype (Comp), Parent (Comp));
3778
3779 Next_Component (Comp);
3780 end loop;
3781 end if;
3782 end Check_Component;
3783
3784 -- Start processing for Check_For_Null_Excluding_Components
3785
3786 begin
3787 Check_Component (Obj_Typ, Obj_Decl);
3788 end Check_For_Null_Excluding_Components;
3789
3790 -----------------
3791 -- Count_Tasks --
3792 -----------------
3793
3794 function Count_Tasks (T : Entity_Id) return Uint is
3795 C : Entity_Id;
3796 X : Node_Id;
3797 V : Uint;
3798
3799 begin
3800 if Is_Task_Type (T) then
3801 return Uint_1;
3802
3803 elsif Is_Record_Type (T) then
3804 if Has_Discriminants (T) then
3805 Check_Restriction (Max_Tasks, N);
3806 return Uint_0;
3807
3808 else
3809 V := Uint_0;
3810 C := First_Component (T);
3811 while Present (C) loop
3812 V := V + Count_Tasks (Etype (C));
3813 Next_Component (C);
3814 end loop;
3815
3816 return V;
3817 end if;
3818
3819 elsif Is_Array_Type (T) then
3820 X := First_Index (T);
3821 V := Count_Tasks (Component_Type (T));
3822 while Present (X) loop
3823 C := Etype (X);
3824
3825 if not Is_OK_Static_Subtype (C) then
3826 Check_Restriction (Max_Tasks, N);
3827 return Uint_0;
3828 else
3829 V := V * (UI_Max (Uint_0,
3830 Expr_Value (Type_High_Bound (C)) -
3831 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3832 end if;
3833
3834 Next_Index (X);
3835 end loop;
3836
3837 return V;
3838
3839 else
3840 return Uint_0;
3841 end if;
3842 end Count_Tasks;
3843
3844 ----------------------------
3845 -- Delayed_Aspect_Present --
3846 ----------------------------
3847
3848 function Delayed_Aspect_Present return Boolean is
3849 A : Node_Id;
3850 A_Id : Aspect_Id;
3851
3852 begin
3853 if Present (Aspect_Specifications (N)) then
3854 A := First (Aspect_Specifications (N));
3855 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3856 while Present (A) loop
3857 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3858
3859 -- Set flag on object entity, for later processing at
3860 -- the freeze point.
3861
3862 Set_Has_Delayed_Aspects (Id);
3863 return True;
3864 end if;
3865
3866 Next (A);
3867 end loop;
3868 end if;
3869
3870 return False;
3871 end Delayed_Aspect_Present;
3872
3873 -- Local variables
3874
3875 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3876 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3877 -- Save the Ghost-related attributes to restore on exit
3878
3879 Related_Id : Entity_Id;
3880 Full_View_Present : Boolean := False;
3881
3882 -- Start of processing for Analyze_Object_Declaration
3883
3884 begin
3885 -- There are three kinds of implicit types generated by an
3886 -- object declaration:
3887
3888 -- 1. Those generated by the original Object Definition
3889
3890 -- 2. Those generated by the Expression
3891
3892 -- 3. Those used to constrain the Object Definition with the
3893 -- expression constraints when the definition is unconstrained.
3894
3895 -- They must be generated in this order to avoid order of elaboration
3896 -- issues. Thus the first step (after entering the name) is to analyze
3897 -- the object definition.
3898
3899 if Constant_Present (N) then
3900 Prev_Entity := Current_Entity_In_Scope (Id);
3901
3902 if Present (Prev_Entity)
3903 and then
3904 -- If the homograph is an implicit subprogram, it is overridden
3905 -- by the current declaration.
3906
3907 ((Is_Overloadable (Prev_Entity)
3908 and then Is_Inherited_Operation (Prev_Entity))
3909
3910 -- The current object is a discriminal generated for an entry
3911 -- family index. Even though the index is a constant, in this
3912 -- particular context there is no true constant redeclaration.
3913 -- Enter_Name will handle the visibility.
3914
3915 or else
3916 (Is_Discriminal (Id)
3917 and then Ekind (Discriminal_Link (Id)) =
3918 E_Entry_Index_Parameter)
3919
3920 -- The current object is the renaming for a generic declared
3921 -- within the instance.
3922
3923 or else
3924 (Ekind (Prev_Entity) = E_Package
3925 and then Nkind (Parent (Prev_Entity)) =
3926 N_Package_Renaming_Declaration
3927 and then not Comes_From_Source (Prev_Entity)
3928 and then
3929 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3930
3931 -- The entity may be a homonym of a private component of the
3932 -- enclosing protected object, for which we create a local
3933 -- renaming declaration. The declaration is legal, even if
3934 -- useless when it just captures that component.
3935
3936 or else
3937 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3938 and then Nkind (Parent (Prev_Entity)) =
3939 N_Object_Renaming_Declaration))
3940 then
3941 Prev_Entity := Empty;
3942 end if;
3943 end if;
3944
3945 if Present (Prev_Entity) then
3946
3947 -- The object declaration is Ghost when it completes a deferred Ghost
3948 -- constant.
3949
3950 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3951
3952 Constant_Redeclaration (Id, N, T);
3953
3954 Generate_Reference (Prev_Entity, Id, 'c');
3955 Set_Completion_Referenced (Id);
3956
3957 if Error_Posted (N) then
3958
3959 -- Type mismatch or illegal redeclaration; do not analyze
3960 -- expression to avoid cascaded errors.
3961
3962 T := Find_Type_Of_Object (Object_Definition (N), N);
3963 Set_Etype (Id, T);
3964 Set_Ekind (Id, E_Variable);
3965 goto Leave;
3966 end if;
3967
3968 -- In the normal case, enter identifier at the start to catch premature
3969 -- usage in the initialization expression.
3970
3971 else
3972 Generate_Definition (Id);
3973 Enter_Name (Id);
3974
3975 Mark_Coextensions (N, Object_Definition (N));
3976
3977 T := Find_Type_Of_Object (Object_Definition (N), N);
3978
3979 if Nkind (Object_Definition (N)) = N_Access_Definition
3980 and then Present
3981 (Access_To_Subprogram_Definition (Object_Definition (N)))
3982 and then Protected_Present
3983 (Access_To_Subprogram_Definition (Object_Definition (N)))
3984 then
3985 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3986 end if;
3987
3988 if Error_Posted (Id) then
3989 Set_Etype (Id, T);
3990 Set_Ekind (Id, E_Variable);
3991 goto Leave;
3992 end if;
3993 end if;
3994
3995 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3996 -- out some static checks.
3997
3998 if Ada_Version >= Ada_2005 then
3999
4000 -- In case of aggregates we must also take care of the correct
4001 -- initialization of nested aggregates bug this is done at the
4002 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4003
4004 if Can_Never_Be_Null (T) then
4005 if Present (Expression (N))
4006 and then Nkind (Expression (N)) = N_Aggregate
4007 then
4008 null;
4009
4010 else
4011 declare
4012 Save_Typ : constant Entity_Id := Etype (Id);
4013 begin
4014 Set_Etype (Id, T); -- Temp. decoration for static checks
4015 Null_Exclusion_Static_Checks (N);
4016 Set_Etype (Id, Save_Typ);
4017 end;
4018 end if;
4019
4020 -- We might be dealing with an object of a composite type containing
4021 -- null-excluding components without an aggregate, so we must verify
4022 -- that such components have default initialization.
4023
4024 else
4025 Check_For_Null_Excluding_Components (T, N);
4026 end if;
4027 end if;
4028
4029 -- Object is marked pure if it is in a pure scope
4030
4031 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4032
4033 -- If deferred constant, make sure context is appropriate. We detect
4034 -- a deferred constant as a constant declaration with no expression.
4035 -- A deferred constant can appear in a package body if its completion
4036 -- is by means of an interface pragma.
4037
4038 if Constant_Present (N) and then No (E) then
4039
4040 -- A deferred constant may appear in the declarative part of the
4041 -- following constructs:
4042
4043 -- blocks
4044 -- entry bodies
4045 -- extended return statements
4046 -- package specs
4047 -- package bodies
4048 -- subprogram bodies
4049 -- task bodies
4050
4051 -- When declared inside a package spec, a deferred constant must be
4052 -- completed by a full constant declaration or pragma Import. In all
4053 -- other cases, the only proper completion is pragma Import. Extended
4054 -- return statements are flagged as invalid contexts because they do
4055 -- not have a declarative part and so cannot accommodate the pragma.
4056
4057 if Ekind (Current_Scope) = E_Return_Statement then
4058 Error_Msg_N
4059 ("invalid context for deferred constant declaration (RM 7.4)",
4060 N);
4061 Error_Msg_N
4062 ("\declaration requires an initialization expression",
4063 N);
4064 Set_Constant_Present (N, False);
4065
4066 -- In Ada 83, deferred constant must be of private type
4067
4068 elsif not Is_Private_Type (T) then
4069 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4070 Error_Msg_N
4071 ("(Ada 83) deferred constant must be private type", N);
4072 end if;
4073 end if;
4074
4075 -- If not a deferred constant, then the object declaration freezes
4076 -- its type, unless the object is of an anonymous type and has delayed
4077 -- aspects. In that case the type is frozen when the object itself is.
4078
4079 else
4080 Check_Fully_Declared (T, N);
4081
4082 if Has_Delayed_Aspects (Id)
4083 and then Is_Array_Type (T)
4084 and then Is_Itype (T)
4085 then
4086 Set_Has_Delayed_Freeze (T);
4087 else
4088 Freeze_Before (N, T);
4089 end if;
4090 end if;
4091
4092 -- If the object was created by a constrained array definition, then
4093 -- set the link in both the anonymous base type and anonymous subtype
4094 -- that are built to represent the array type to point to the object.
4095
4096 if Nkind (Object_Definition (Declaration_Node (Id))) =
4097 N_Constrained_Array_Definition
4098 then
4099 Set_Related_Array_Object (T, Id);
4100 Set_Related_Array_Object (Base_Type (T), Id);
4101 end if;
4102
4103 -- Special checks for protected objects not at library level
4104
4105 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4106 Check_Restriction (No_Local_Protected_Objects, Id);
4107
4108 -- Protected objects with interrupt handlers must be at library level
4109
4110 -- Ada 2005: This test is not needed (and the corresponding clause
4111 -- in the RM is removed) because accessibility checks are sufficient
4112 -- to make handlers not at the library level illegal.
4113
4114 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4115 -- applies to the '95 version of the language as well.
4116
4117 if Is_Protected_Type (T)
4118 and then Has_Interrupt_Handler (T)
4119 and then Ada_Version < Ada_95
4120 then
4121 Error_Msg_N
4122 ("interrupt object can only be declared at library level", Id);
4123 end if;
4124 end if;
4125
4126 -- Check for violation of No_Local_Timing_Events
4127
4128 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4129 Check_Restriction (No_Local_Timing_Events, Id);
4130 end if;
4131
4132 -- The actual subtype of the object is the nominal subtype, unless
4133 -- the nominal one is unconstrained and obtained from the expression.
4134
4135 Act_T := T;
4136
4137 if Is_Library_Level_Entity (Id) then
4138 Check_Dynamic_Object (T);
4139 end if;
4140
4141 -- Process initialization expression if present and not in error
4142
4143 if Present (E) and then E /= Error then
4144
4145 -- Generate an error in case of CPP class-wide object initialization.
4146 -- Required because otherwise the expansion of the class-wide
4147 -- assignment would try to use 'size to initialize the object
4148 -- (primitive that is not available in CPP tagged types).
4149
4150 if Is_Class_Wide_Type (Act_T)
4151 and then
4152 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4153 or else
4154 (Present (Full_View (Root_Type (Etype (Act_T))))
4155 and then
4156 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4157 then
4158 Error_Msg_N
4159 ("predefined assignment not available for 'C'P'P tagged types",
4160 E);
4161 end if;
4162
4163 Mark_Coextensions (N, E);
4164 Analyze (E);
4165
4166 -- In case of errors detected in the analysis of the expression,
4167 -- decorate it with the expected type to avoid cascaded errors
4168
4169 if No (Etype (E)) then
4170 Set_Etype (E, T);
4171 end if;
4172
4173 -- If an initialization expression is present, then we set the
4174 -- Is_True_Constant flag. It will be reset if this is a variable
4175 -- and it is indeed modified.
4176
4177 Set_Is_True_Constant (Id, True);
4178
4179 -- If we are analyzing a constant declaration, set its completion
4180 -- flag after analyzing and resolving the expression.
4181
4182 if Constant_Present (N) then
4183 Set_Has_Completion (Id);
4184 end if;
4185
4186 -- Set type and resolve (type may be overridden later on). Note:
4187 -- Ekind (Id) must still be E_Void at this point so that incorrect
4188 -- early usage within E is properly diagnosed.
4189
4190 Set_Etype (Id, T);
4191
4192 -- If the expression is an aggregate we must look ahead to detect
4193 -- the possible presence of an address clause, and defer resolution
4194 -- and expansion of the aggregate to the freeze point of the entity.
4195
4196 -- This is not always legal because the aggregate may contain other
4197 -- references that need freezing, e.g. references to other entities
4198 -- with address clauses. In any case, when compiling with -gnatI the
4199 -- presence of the address clause must be ignored.
4200
4201 if Comes_From_Source (N)
4202 and then Expander_Active
4203 and then Nkind (E) = N_Aggregate
4204 and then
4205 ((Present (Following_Address_Clause (N))
4206 and then not Ignore_Rep_Clauses)
4207 or else Delayed_Aspect_Present)
4208 then
4209 Set_Etype (E, T);
4210
4211 -- If the aggregate is limited it will be built in place, and its
4212 -- expansion is deferred until the object declaration is expanded.
4213
4214 -- This is also required when generating C code to ensure that an
4215 -- object with an alignment or address clause can be initialized
4216 -- by means of component by component assignments.
4217
4218 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4219 Set_Expansion_Delayed (E);
4220 end if;
4221
4222 else
4223 -- If the expression is a formal that is a "subprogram pointer"
4224 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4225 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4226 -- the corresponding check, as is done for assignments.
4227
4228 if Is_Entity_Name (E)
4229 and then Present (Entity (E))
4230 and then Is_Formal (Entity (E))
4231 and then
4232 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4233 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4234 then
4235 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4236 end if;
4237
4238 Resolve (E, T);
4239 end if;
4240
4241 -- No further action needed if E is a call to an inlined function
4242 -- which returns an unconstrained type and it has been expanded into
4243 -- a procedure call. In that case N has been replaced by an object
4244 -- declaration without initializing expression and it has been
4245 -- analyzed (see Expand_Inlined_Call).
4246
4247 if Back_End_Inlining
4248 and then Expander_Active
4249 and then Nkind (E) = N_Function_Call
4250 and then Nkind (Name (E)) in N_Has_Entity
4251 and then Is_Inlined (Entity (Name (E)))
4252 and then not Is_Constrained (Etype (E))
4253 and then Analyzed (N)
4254 and then No (Expression (N))
4255 then
4256 goto Leave;
4257 end if;
4258
4259 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4260 -- node (which was marked already-analyzed), we need to set the type
4261 -- to something other than Any_Access in order to keep gigi happy.
4262
4263 if Etype (E) = Any_Access then
4264 Set_Etype (E, T);
4265 end if;
4266
4267 -- If the object is an access to variable, the initialization
4268 -- expression cannot be an access to constant.
4269
4270 if Is_Access_Type (T)
4271 and then not Is_Access_Constant (T)
4272 and then Is_Access_Type (Etype (E))
4273 and then Is_Access_Constant (Etype (E))
4274 then
4275 Error_Msg_N
4276 ("access to variable cannot be initialized with an "
4277 & "access-to-constant expression", E);
4278 end if;
4279
4280 if not Assignment_OK (N) then
4281 Check_Initialization (T, E);
4282 end if;
4283
4284 Check_Unset_Reference (E);
4285
4286 -- If this is a variable, then set current value. If this is a
4287 -- declared constant of a scalar type with a static expression,
4288 -- indicate that it is always valid.
4289
4290 if not Constant_Present (N) then
4291 if Compile_Time_Known_Value (E) then
4292 Set_Current_Value (Id, E);
4293 end if;
4294
4295 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4296 Set_Is_Known_Valid (Id);
4297
4298 -- If it is a constant initialized with a valid nonstatic entity,
4299 -- the constant is known valid as well, and can inherit the subtype
4300 -- of the entity if it is a subtype of the given type. This info
4301 -- is preserved on the actual subtype of the constant.
4302
4303 elsif Is_Scalar_Type (T)
4304 and then Is_Entity_Name (E)
4305 and then Is_Known_Valid (Entity (E))
4306 and then In_Subrange_Of (Etype (Entity (E)), T)
4307 then
4308 Set_Is_Known_Valid (Id);
4309 Set_Ekind (Id, E_Constant);
4310 Set_Actual_Subtype (Id, Etype (Entity (E)));
4311 end if;
4312
4313 -- Deal with setting of null flags
4314
4315 if Is_Access_Type (T) then
4316 if Known_Non_Null (E) then
4317 Set_Is_Known_Non_Null (Id, True);
4318 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4319 Set_Is_Known_Null (Id, True);
4320 end if;
4321 end if;
4322
4323 -- Check incorrect use of dynamically tagged expressions
4324
4325 if Is_Tagged_Type (T) then
4326 Check_Dynamically_Tagged_Expression
4327 (Expr => E,
4328 Typ => T,
4329 Related_Nod => N);
4330 end if;
4331
4332 Apply_Scalar_Range_Check (E, T);
4333 Apply_Static_Length_Check (E, T);
4334
4335 -- A formal parameter of a specific tagged type whose related
4336 -- subprogram is subject to pragma Extensions_Visible with value
4337 -- "False" cannot be implicitly converted to a class-wide type by
4338 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4339 -- not consider internally generated expressions.
4340
4341 if Is_Class_Wide_Type (T)
4342 and then Comes_From_Source (E)
4343 and then Is_EVF_Expression (E)
4344 then
4345 Error_Msg_N
4346 ("formal parameter cannot be implicitly converted to "
4347 & "class-wide type when Extensions_Visible is False", E);
4348 end if;
4349 end if;
4350
4351 -- If the No_Streams restriction is set, check that the type of the
4352 -- object is not, and does not contain, any subtype derived from
4353 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4354 -- Has_Stream just for efficiency reasons. There is no point in
4355 -- spending time on a Has_Stream check if the restriction is not set.
4356
4357 if Restriction_Check_Required (No_Streams) then
4358 if Has_Stream (T) then
4359 Check_Restriction (No_Streams, N);
4360 end if;
4361 end if;
4362
4363 -- Deal with predicate check before we start to do major rewriting. It
4364 -- is OK to initialize and then check the initialized value, since the
4365 -- object goes out of scope if we get a predicate failure. Note that we
4366 -- do this in the analyzer and not the expander because the analyzer
4367 -- does some substantial rewriting in some cases.
4368
4369 -- We need a predicate check if the type has predicates that are not
4370 -- ignored, and if either there is an initializing expression, or for
4371 -- default initialization when we have at least one case of an explicit
4372 -- default initial value (including via a Default_Value or
4373 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4374 -- an internal declaration whose initialization comes later (as for an
4375 -- aggregate expansion).
4376 -- If expression is an aggregate it may be expanded into assignments
4377 -- and the declaration itself is marked with No_Initialization, but
4378 -- the predicate still applies.
4379
4380 if not Suppress_Assignment_Checks (N)
4381 and then Present (Predicate_Function (T))
4382 and then not Predicates_Ignored (T)
4383 and then
4384 (not No_Initialization (N)
4385 or else (Present (E) and then Nkind (E) = N_Aggregate))
4386 and then
4387 (Present (E)
4388 or else
4389 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4390 then
4391 -- If the type has a static predicate and the expression is known at
4392 -- compile time, see if the expression satisfies the predicate.
4393
4394 if Present (E) then
4395 Check_Expression_Against_Static_Predicate (E, T);
4396 end if;
4397
4398 -- If the type is a null record and there is no explicit initial
4399 -- expression, no predicate check applies.
4400
4401 if No (E) and then Is_Null_Record_Type (T) then
4402 null;
4403
4404 -- Do not generate a predicate check if the initialization expression
4405 -- is a type conversion because the conversion has been subjected to
4406 -- the same check. This is a small optimization which avoid redundant
4407 -- checks.
4408
4409 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4410 null;
4411
4412 else
4413 -- The check must be inserted after the expanded aggregate
4414 -- expansion code, if any.
4415
4416 declare
4417 Check : constant Node_Id :=
4418 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4419
4420 begin
4421 if No (Next_Decl) then
4422 Append_To (List_Containing (N), Check);
4423 else
4424 Insert_Before (Next_Decl, Check);
4425 end if;
4426 end;
4427 end if;
4428 end if;
4429
4430 -- Case of unconstrained type
4431
4432 if not Is_Definite_Subtype (T) then
4433
4434 -- Nothing to do in deferred constant case
4435
4436 if Constant_Present (N) and then No (E) then
4437 null;
4438
4439 -- Case of no initialization present
4440
4441 elsif No (E) then
4442 if No_Initialization (N) then
4443 null;
4444
4445 elsif Is_Class_Wide_Type (T) then
4446 Error_Msg_N
4447 ("initialization required in class-wide declaration ", N);
4448
4449 else
4450 Error_Msg_N
4451 ("unconstrained subtype not allowed (need initialization)",
4452 Object_Definition (N));
4453
4454 if Is_Record_Type (T) and then Has_Discriminants (T) then
4455 Error_Msg_N
4456 ("\provide initial value or explicit discriminant values",
4457 Object_Definition (N));
4458
4459 Error_Msg_NE
4460 ("\or give default discriminant values for type&",
4461 Object_Definition (N), T);
4462
4463 elsif Is_Array_Type (T) then
4464 Error_Msg_N
4465 ("\provide initial value or explicit array bounds",
4466 Object_Definition (N));
4467 end if;
4468 end if;
4469
4470 -- Case of initialization present but in error. Set initial
4471 -- expression as absent (but do not make above complaints)
4472
4473 elsif E = Error then
4474 Set_Expression (N, Empty);
4475 E := Empty;
4476
4477 -- Case of initialization present
4478
4479 else
4480 -- Check restrictions in Ada 83
4481
4482 if not Constant_Present (N) then
4483
4484 -- Unconstrained variables not allowed in Ada 83 mode
4485
4486 if Ada_Version = Ada_83
4487 and then Comes_From_Source (Object_Definition (N))
4488 then
4489 Error_Msg_N
4490 ("(Ada 83) unconstrained variable not allowed",
4491 Object_Definition (N));
4492 end if;
4493 end if;
4494
4495 -- Now we constrain the variable from the initializing expression
4496
4497 -- If the expression is an aggregate, it has been expanded into
4498 -- individual assignments. Retrieve the actual type from the
4499 -- expanded construct.
4500
4501 if Is_Array_Type (T)
4502 and then No_Initialization (N)
4503 and then Nkind (Original_Node (E)) = N_Aggregate
4504 then
4505 Act_T := Etype (E);
4506
4507 -- In case of class-wide interface object declarations we delay
4508 -- the generation of the equivalent record type declarations until
4509 -- its expansion because there are cases in they are not required.
4510
4511 elsif Is_Interface (T) then
4512 null;
4513
4514 -- If the type is an unchecked union, no subtype can be built from
4515 -- the expression. Rewrite declaration as a renaming, which the
4516 -- back-end can handle properly. This is a rather unusual case,
4517 -- because most unchecked_union declarations have default values
4518 -- for discriminants and are thus not indefinite.
4519
4520 elsif Is_Unchecked_Union (T) then
4521 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4522 Set_Ekind (Id, E_Constant);
4523 else
4524 Set_Ekind (Id, E_Variable);
4525 end if;
4526
4527 Rewrite (N,
4528 Make_Object_Renaming_Declaration (Loc,
4529 Defining_Identifier => Id,
4530 Subtype_Mark => New_Occurrence_Of (T, Loc),
4531 Name => E));
4532
4533 Set_Renamed_Object (Id, E);
4534 Freeze_Before (N, T);
4535 Set_Is_Frozen (Id);
4536 goto Leave;
4537
4538 else
4539 -- Ensure that the generated subtype has a unique external name
4540 -- when the related object is public. This guarantees that the
4541 -- subtype and its bounds will not be affected by switches or
4542 -- pragmas that may offset the internal counter due to extra
4543 -- generated code.
4544
4545 if Is_Public (Id) then
4546 Related_Id := Id;
4547 else
4548 Related_Id := Empty;
4549 end if;
4550
4551 Expand_Subtype_From_Expr
4552 (N => N,
4553 Unc_Type => T,
4554 Subtype_Indic => Object_Definition (N),
4555 Exp => E,
4556 Related_Id => Related_Id);
4557
4558 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4559 end if;
4560
4561 -- Propagate attributes to full view when needed.
4562
4563 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4564
4565 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4566 then
4567 Full_View_Present := True;
4568 end if;
4569
4570 if Full_View_Present then
4571 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4572 end if;
4573
4574 if Aliased_Present (N) then
4575 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4576
4577 if Full_View_Present then
4578 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4579 end if;
4580 end if;
4581
4582 Freeze_Before (N, Act_T);
4583 Freeze_Before (N, T);
4584 end if;
4585
4586 elsif Is_Array_Type (T)
4587 and then No_Initialization (N)
4588 and then (Nkind (Original_Node (E)) = N_Aggregate
4589 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4590 and then Nkind (Original_Node (Expression
4591 (Original_Node (E)))) = N_Aggregate))
4592 then
4593 if not Is_Entity_Name (Object_Definition (N)) then
4594 Act_T := Etype (E);
4595 Check_Compile_Time_Size (Act_T);
4596
4597 if Aliased_Present (N) then
4598 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4599 end if;
4600 end if;
4601
4602 -- When the given object definition and the aggregate are specified
4603 -- independently, and their lengths might differ do a length check.
4604 -- This cannot happen if the aggregate is of the form (others =>...)
4605
4606 if not Is_Constrained (T) then
4607 null;
4608
4609 elsif Nkind (E) = N_Raise_Constraint_Error then
4610
4611 -- Aggregate is statically illegal. Place back in declaration
4612
4613 Set_Expression (N, E);
4614 Set_No_Initialization (N, False);
4615
4616 elsif T = Etype (E) then
4617 null;
4618
4619 elsif Nkind (E) = N_Aggregate
4620 and then Present (Component_Associations (E))
4621 and then Present (Choice_List (First (Component_Associations (E))))
4622 and then
4623 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4624 N_Others_Choice
4625 then
4626 null;
4627
4628 else
4629 Apply_Length_Check (E, T);
4630 end if;
4631
4632 -- If the type is limited unconstrained with defaulted discriminants and
4633 -- there is no expression, then the object is constrained by the
4634 -- defaults, so it is worthwhile building the corresponding subtype.
4635
4636 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4637 and then not Is_Constrained (T)
4638 and then Has_Discriminants (T)
4639 then
4640 if No (E) then
4641 Act_T := Build_Default_Subtype (T, N);
4642 else
4643 -- Ada 2005: A limited object may be initialized by means of an
4644 -- aggregate. If the type has default discriminants it has an
4645 -- unconstrained nominal type, Its actual subtype will be obtained
4646 -- from the aggregate, and not from the default discriminants.
4647
4648 Act_T := Etype (E);
4649 end if;
4650
4651 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4652
4653 elsif Nkind (E) = N_Function_Call
4654 and then Constant_Present (N)
4655 and then Has_Unconstrained_Elements (Etype (E))
4656 then
4657 -- The back-end has problems with constants of a discriminated type
4658 -- with defaults, if the initial value is a function call. We
4659 -- generate an intermediate temporary that will receive a reference
4660 -- to the result of the call. The initialization expression then
4661 -- becomes a dereference of that temporary.
4662
4663 Remove_Side_Effects (E);
4664
4665 -- If this is a constant declaration of an unconstrained type and
4666 -- the initialization is an aggregate, we can use the subtype of the
4667 -- aggregate for the declared entity because it is immutable.
4668
4669 elsif not Is_Constrained (T)
4670 and then Has_Discriminants (T)
4671 and then Constant_Present (N)
4672 and then not Has_Unchecked_Union (T)
4673 and then Nkind (E) = N_Aggregate
4674 then
4675 Act_T := Etype (E);
4676 end if;
4677
4678 -- Check No_Wide_Characters restriction
4679
4680 Check_Wide_Character_Restriction (T, Object_Definition (N));
4681
4682 -- Indicate this is not set in source. Certainly true for constants, and
4683 -- true for variables so far (will be reset for a variable if and when
4684 -- we encounter a modification in the source).
4685
4686 Set_Never_Set_In_Source (Id);
4687
4688 -- Now establish the proper kind and type of the object
4689
4690 if Constant_Present (N) then
4691 Set_Ekind (Id, E_Constant);
4692 Set_Is_True_Constant (Id);
4693
4694 else
4695 Set_Ekind (Id, E_Variable);
4696
4697 -- A variable is set as shared passive if it appears in a shared
4698 -- passive package, and is at the outer level. This is not done for
4699 -- entities generated during expansion, because those are always
4700 -- manipulated locally.
4701
4702 if Is_Shared_Passive (Current_Scope)
4703 and then Is_Library_Level_Entity (Id)
4704 and then Comes_From_Source (Id)
4705 then
4706 Set_Is_Shared_Passive (Id);
4707 Check_Shared_Var (Id, T, N);
4708 end if;
4709
4710 -- Set Has_Initial_Value if initializing expression present. Note
4711 -- that if there is no initializing expression, we leave the state
4712 -- of this flag unchanged (usually it will be False, but notably in
4713 -- the case of exception choice variables, it will already be true).
4714
4715 if Present (E) then
4716 Set_Has_Initial_Value (Id);
4717 end if;
4718 end if;
4719
4720 -- Set the SPARK mode from the current context (may be overwritten later
4721 -- with explicit pragma).
4722
4723 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4724 Set_SPARK_Pragma_Inherited (Id);
4725
4726 -- Preserve relevant elaboration-related attributes of the context which
4727 -- are no longer available or very expensive to recompute once analysis,
4728 -- resolution, and expansion are over.
4729
4730 Mark_Elaboration_Attributes
4731 (N_Id => Id,
4732 Checks => True,
4733 Warnings => True);
4734
4735 -- Initialize alignment and size and capture alignment setting
4736
4737 Init_Alignment (Id);
4738 Init_Esize (Id);
4739 Set_Optimize_Alignment_Flags (Id);
4740
4741 -- Deal with aliased case
4742
4743 if Aliased_Present (N) then
4744 Set_Is_Aliased (Id);
4745
4746 -- AI12-001: All aliased objects are considered to be specified as
4747 -- independently addressable (RM C.6(8.1/4)).
4748
4749 Set_Is_Independent (Id);
4750
4751 -- If the object is aliased and the type is unconstrained with
4752 -- defaulted discriminants and there is no expression, then the
4753 -- object is constrained by the defaults, so it is worthwhile
4754 -- building the corresponding subtype.
4755
4756 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4757 -- unconstrained, then only establish an actual subtype if the
4758 -- nominal subtype is indefinite. In definite cases the object is
4759 -- unconstrained in Ada 2005.
4760
4761 if No (E)
4762 and then Is_Record_Type (T)
4763 and then not Is_Constrained (T)
4764 and then Has_Discriminants (T)
4765 and then (Ada_Version < Ada_2005
4766 or else not Is_Definite_Subtype (T))
4767 then
4768 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4769 end if;
4770 end if;
4771
4772 -- Now we can set the type of the object
4773
4774 Set_Etype (Id, Act_T);
4775
4776 -- Non-constant object is marked to be treated as volatile if type is
4777 -- volatile and we clear the Current_Value setting that may have been
4778 -- set above. Doing so for constants isn't required and might interfere
4779 -- with possible uses of the object as a static expression in contexts
4780 -- incompatible with volatility (e.g. as a case-statement alternative).
4781
4782 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4783 Set_Treat_As_Volatile (Id);
4784 Set_Current_Value (Id, Empty);
4785 end if;
4786
4787 -- Deal with controlled types
4788
4789 if Has_Controlled_Component (Etype (Id))
4790 or else Is_Controlled (Etype (Id))
4791 then
4792 if not Is_Library_Level_Entity (Id) then
4793 Check_Restriction (No_Nested_Finalization, N);
4794 else
4795 Validate_Controlled_Object (Id);
4796 end if;
4797 end if;
4798
4799 if Has_Task (Etype (Id)) then
4800 Check_Restriction (No_Tasking, N);
4801
4802 -- Deal with counting max tasks
4803
4804 -- Nothing to do if inside a generic
4805
4806 if Inside_A_Generic then
4807 null;
4808
4809 -- If library level entity, then count tasks
4810
4811 elsif Is_Library_Level_Entity (Id) then
4812 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4813
4814 -- If not library level entity, then indicate we don't know max
4815 -- tasks and also check task hierarchy restriction and blocking
4816 -- operation (since starting a task is definitely blocking).
4817
4818 else
4819 Check_Restriction (Max_Tasks, N);
4820 Check_Restriction (No_Task_Hierarchy, N);
4821 Check_Potentially_Blocking_Operation (N);
4822 end if;
4823
4824 -- A rather specialized test. If we see two tasks being declared
4825 -- of the same type in the same object declaration, and the task
4826 -- has an entry with an address clause, we know that program error
4827 -- will be raised at run time since we can't have two tasks with
4828 -- entries at the same address.
4829
4830 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4831 declare
4832 E : Entity_Id;
4833
4834 begin
4835 E := First_Entity (Etype (Id));
4836 while Present (E) loop
4837 if Ekind (E) = E_Entry
4838 and then Present (Get_Attribute_Definition_Clause
4839 (E, Attribute_Address))
4840 then
4841 Error_Msg_Warn := SPARK_Mode /= On;
4842 Error_Msg_N
4843 ("more than one task with same entry address<<", N);
4844 Error_Msg_N ("\Program_Error [<<", N);
4845 Insert_Action (N,
4846 Make_Raise_Program_Error (Loc,
4847 Reason => PE_Duplicated_Entry_Address));
4848 exit;
4849 end if;
4850
4851 Next_Entity (E);
4852 end loop;
4853 end;
4854 end if;
4855 end if;
4856
4857 -- Some simple constant-propagation: if the expression is a constant
4858 -- string initialized with a literal, share the literal. This avoids
4859 -- a run-time copy.
4860
4861 if Present (E)
4862 and then Is_Entity_Name (E)
4863 and then Ekind (Entity (E)) = E_Constant
4864 and then Base_Type (Etype (E)) = Standard_String
4865 then
4866 declare
4867 Val : constant Node_Id := Constant_Value (Entity (E));
4868 begin
4869 if Present (Val) and then Nkind (Val) = N_String_Literal then
4870 Rewrite (E, New_Copy (Val));
4871 end if;
4872 end;
4873 end if;
4874
4875 -- Another optimization: if the nominal subtype is unconstrained and
4876 -- the expression is a function call that returns an unconstrained
4877 -- type, rewrite the declaration as a renaming of the result of the
4878 -- call. The exceptions below are cases where the copy is expected,
4879 -- either by the back end (Aliased case) or by the semantics, as for
4880 -- initializing controlled types or copying tags for class-wide types.
4881
4882 if Present (E)
4883 and then Nkind (E) = N_Explicit_Dereference
4884 and then Nkind (Original_Node (E)) = N_Function_Call
4885 and then not Is_Library_Level_Entity (Id)
4886 and then not Is_Constrained (Underlying_Type (T))
4887 and then not Is_Aliased (Id)
4888 and then not Is_Class_Wide_Type (T)
4889 and then not Is_Controlled (T)
4890 and then not Has_Controlled_Component (Base_Type (T))
4891 and then Expander_Active
4892 then
4893 Rewrite (N,
4894 Make_Object_Renaming_Declaration (Loc,
4895 Defining_Identifier => Id,
4896 Access_Definition => Empty,
4897 Subtype_Mark => New_Occurrence_Of
4898 (Base_Type (Etype (Id)), Loc),
4899 Name => E));
4900
4901 Set_Renamed_Object (Id, E);
4902
4903 -- Force generation of debugging information for the constant and for
4904 -- the renamed function call.
4905
4906 Set_Debug_Info_Needed (Id);
4907 Set_Debug_Info_Needed (Entity (Prefix (E)));
4908 end if;
4909
4910 if Present (Prev_Entity)
4911 and then Is_Frozen (Prev_Entity)
4912 and then not Error_Posted (Id)
4913 then
4914 Error_Msg_N ("full constant declaration appears too late", N);
4915 end if;
4916
4917 Check_Eliminated (Id);
4918
4919 -- Deal with setting In_Private_Part flag if in private part
4920
4921 if Ekind (Scope (Id)) = E_Package
4922 and then In_Private_Part (Scope (Id))
4923 then
4924 Set_In_Private_Part (Id);
4925 end if;
4926
4927 <<Leave>>
4928 -- Initialize the refined state of a variable here because this is a
4929 -- common destination for legal and illegal object declarations.
4930
4931 if Ekind (Id) = E_Variable then
4932 Set_Encapsulating_State (Id, Empty);
4933 end if;
4934
4935 if Has_Aspects (N) then
4936 Analyze_Aspect_Specifications (N, Id);
4937 end if;
4938
4939 Analyze_Dimension (N);
4940
4941 -- Verify whether the object declaration introduces an illegal hidden
4942 -- state within a package subject to a null abstract state.
4943
4944 if Ekind (Id) = E_Variable then
4945 Check_No_Hidden_State (Id);
4946 end if;
4947
4948 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4949 end Analyze_Object_Declaration;
4950
4951 ---------------------------
4952 -- Analyze_Others_Choice --
4953 ---------------------------
4954
4955 -- Nothing to do for the others choice node itself, the semantic analysis
4956 -- of the others choice will occur as part of the processing of the parent
4957
4958 procedure Analyze_Others_Choice (N : Node_Id) is
4959 pragma Warnings (Off, N);
4960 begin
4961 null;
4962 end Analyze_Others_Choice;
4963
4964 -------------------------------------------
4965 -- Analyze_Private_Extension_Declaration --
4966 -------------------------------------------
4967
4968 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4969 Indic : constant Node_Id := Subtype_Indication (N);
4970 T : constant Entity_Id := Defining_Identifier (N);
4971 Iface : Entity_Id;
4972 Iface_Elmt : Elmt_Id;
4973 Parent_Base : Entity_Id;
4974 Parent_Type : Entity_Id;
4975
4976 begin
4977 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4978
4979 if Is_Non_Empty_List (Interface_List (N)) then
4980 declare
4981 Intf : Node_Id;
4982 T : Entity_Id;
4983
4984 begin
4985 Intf := First (Interface_List (N));
4986 while Present (Intf) loop
4987 T := Find_Type_Of_Subtype_Indic (Intf);
4988
4989 Diagnose_Interface (Intf, T);
4990 Next (Intf);
4991 end loop;
4992 end;
4993 end if;
4994
4995 Generate_Definition (T);
4996
4997 -- For other than Ada 2012, just enter the name in the current scope
4998
4999 if Ada_Version < Ada_2012 then
5000 Enter_Name (T);
5001
5002 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5003 -- case of private type that completes an incomplete type.
5004
5005 else
5006 declare
5007 Prev : Entity_Id;
5008
5009 begin
5010 Prev := Find_Type_Name (N);
5011
5012 pragma Assert (Prev = T
5013 or else (Ekind (Prev) = E_Incomplete_Type
5014 and then Present (Full_View (Prev))
5015 and then Full_View (Prev) = T));
5016 end;
5017 end if;
5018
5019 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5020 Parent_Base := Base_Type (Parent_Type);
5021
5022 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5023 Set_Ekind (T, Ekind (Parent_Type));
5024 Set_Etype (T, Any_Type);
5025 goto Leave;
5026
5027 elsif not Is_Tagged_Type (Parent_Type) then
5028 Error_Msg_N
5029 ("parent of type extension must be a tagged type ", Indic);
5030 goto Leave;
5031
5032 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5033 Error_Msg_N ("premature derivation of incomplete type", Indic);
5034 goto Leave;
5035
5036 elsif Is_Concurrent_Type (Parent_Type) then
5037 Error_Msg_N
5038 ("parent type of a private extension cannot be a synchronized "
5039 & "tagged type (RM 3.9.1 (3/1))", N);
5040
5041 Set_Etype (T, Any_Type);
5042 Set_Ekind (T, E_Limited_Private_Type);
5043 Set_Private_Dependents (T, New_Elmt_List);
5044 Set_Error_Posted (T);
5045 goto Leave;
5046 end if;
5047
5048 -- Perhaps the parent type should be changed to the class-wide type's
5049 -- specific type in this case to prevent cascading errors ???
5050
5051 if Is_Class_Wide_Type (Parent_Type) then
5052 Error_Msg_N
5053 ("parent of type extension must not be a class-wide type", Indic);
5054 goto Leave;
5055 end if;
5056
5057 if (not Is_Package_Or_Generic_Package (Current_Scope)
5058 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5059 or else In_Private_Part (Current_Scope)
5060 then
5061 Error_Msg_N ("invalid context for private extension", N);
5062 end if;
5063
5064 -- Set common attributes
5065
5066 Set_Is_Pure (T, Is_Pure (Current_Scope));
5067 Set_Scope (T, Current_Scope);
5068 Set_Ekind (T, E_Record_Type_With_Private);
5069 Init_Size_Align (T);
5070 Set_Default_SSO (T);
5071 Set_No_Reordering (T, No_Component_Reordering);
5072
5073 Set_Etype (T, Parent_Base);
5074 Propagate_Concurrent_Flags (T, Parent_Base);
5075
5076 Set_Convention (T, Convention (Parent_Type));
5077 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5078 Set_Is_First_Subtype (T);
5079 Make_Class_Wide_Type (T);
5080
5081 -- Set the SPARK mode from the current context
5082
5083 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5084 Set_SPARK_Pragma_Inherited (T);
5085
5086 if Unknown_Discriminants_Present (N) then
5087 Set_Discriminant_Constraint (T, No_Elist);
5088 end if;
5089
5090 Build_Derived_Record_Type (N, Parent_Type, T);
5091
5092 -- A private extension inherits the Default_Initial_Condition pragma
5093 -- coming from any parent type within the derivation chain.
5094
5095 if Has_DIC (Parent_Type) then
5096 Set_Has_Inherited_DIC (T);
5097 end if;
5098
5099 -- A private extension inherits any class-wide invariants coming from a
5100 -- parent type or an interface. Note that the invariant procedure of the
5101 -- parent type should not be inherited because the private extension may
5102 -- define invariants of its own.
5103
5104 if Has_Inherited_Invariants (Parent_Type)
5105 or else Has_Inheritable_Invariants (Parent_Type)
5106 then
5107 Set_Has_Inherited_Invariants (T);
5108
5109 elsif Present (Interfaces (T)) then
5110 Iface_Elmt := First_Elmt (Interfaces (T));
5111 while Present (Iface_Elmt) loop
5112 Iface := Node (Iface_Elmt);
5113
5114 if Has_Inheritable_Invariants (Iface) then
5115 Set_Has_Inherited_Invariants (T);
5116 exit;
5117 end if;
5118
5119 Next_Elmt (Iface_Elmt);
5120 end loop;
5121 end if;
5122
5123 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5124 -- synchronized formal derived type.
5125
5126 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5127 Set_Is_Limited_Record (T);
5128
5129 -- Formal derived type case
5130
5131 if Is_Generic_Type (T) then
5132
5133 -- The parent must be a tagged limited type or a synchronized
5134 -- interface.
5135
5136 if (not Is_Tagged_Type (Parent_Type)
5137 or else not Is_Limited_Type (Parent_Type))
5138 and then
5139 (not Is_Interface (Parent_Type)
5140 or else not Is_Synchronized_Interface (Parent_Type))
5141 then
5142 Error_Msg_NE
5143 ("parent type of & must be tagged limited or synchronized",
5144 N, T);
5145 end if;
5146
5147 -- The progenitors (if any) must be limited or synchronized
5148 -- interfaces.
5149
5150 if Present (Interfaces (T)) then
5151 Iface_Elmt := First_Elmt (Interfaces (T));
5152 while Present (Iface_Elmt) loop
5153 Iface := Node (Iface_Elmt);
5154
5155 if not Is_Limited_Interface (Iface)
5156 and then not Is_Synchronized_Interface (Iface)
5157 then
5158 Error_Msg_NE
5159 ("progenitor & must be limited or synchronized",
5160 N, Iface);
5161 end if;
5162
5163 Next_Elmt (Iface_Elmt);
5164 end loop;
5165 end if;
5166
5167 -- Regular derived extension, the parent must be a limited or
5168 -- synchronized interface.
5169
5170 else
5171 if not Is_Interface (Parent_Type)
5172 or else (not Is_Limited_Interface (Parent_Type)
5173 and then not Is_Synchronized_Interface (Parent_Type))
5174 then
5175 Error_Msg_NE
5176 ("parent type of & must be limited interface", N, T);
5177 end if;
5178 end if;
5179
5180 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5181 -- extension with a synchronized parent must be explicitly declared
5182 -- synchronized, because the full view will be a synchronized type.
5183 -- This must be checked before the check for limited types below,
5184 -- to ensure that types declared limited are not allowed to extend
5185 -- synchronized interfaces.
5186
5187 elsif Is_Interface (Parent_Type)
5188 and then Is_Synchronized_Interface (Parent_Type)
5189 and then not Synchronized_Present (N)
5190 then
5191 Error_Msg_NE
5192 ("private extension of& must be explicitly synchronized",
5193 N, Parent_Type);
5194
5195 elsif Limited_Present (N) then
5196 Set_Is_Limited_Record (T);
5197
5198 if not Is_Limited_Type (Parent_Type)
5199 and then
5200 (not Is_Interface (Parent_Type)
5201 or else not Is_Limited_Interface (Parent_Type))
5202 then
5203 Error_Msg_NE ("parent type& of limited extension must be limited",
5204 N, Parent_Type);
5205 end if;
5206 end if;
5207
5208 -- Remember that its parent type has a private extension. Used to warn
5209 -- on public primitives of the parent type defined after its private
5210 -- extensions (see Check_Dispatching_Operation).
5211
5212 Set_Has_Private_Extension (Parent_Type);
5213
5214 <<Leave>>
5215 if Has_Aspects (N) then
5216 Analyze_Aspect_Specifications (N, T);
5217 end if;
5218 end Analyze_Private_Extension_Declaration;
5219
5220 ---------------------------------
5221 -- Analyze_Subtype_Declaration --
5222 ---------------------------------
5223
5224 procedure Analyze_Subtype_Declaration
5225 (N : Node_Id;
5226 Skip : Boolean := False)
5227 is
5228 Id : constant Entity_Id := Defining_Identifier (N);
5229 R_Checks : Check_Result;
5230 T : Entity_Id;
5231
5232 begin
5233 Generate_Definition (Id);
5234 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5235 Init_Size_Align (Id);
5236
5237 -- The following guard condition on Enter_Name is to handle cases where
5238 -- the defining identifier has already been entered into the scope but
5239 -- the declaration as a whole needs to be analyzed.
5240
5241 -- This case in particular happens for derived enumeration types. The
5242 -- derived enumeration type is processed as an inserted enumeration type
5243 -- declaration followed by a rewritten subtype declaration. The defining
5244 -- identifier, however, is entered into the name scope very early in the
5245 -- processing of the original type declaration and therefore needs to be
5246 -- avoided here, when the created subtype declaration is analyzed. (See
5247 -- Build_Derived_Types)
5248
5249 -- This also happens when the full view of a private type is derived
5250 -- type with constraints. In this case the entity has been introduced
5251 -- in the private declaration.
5252
5253 -- Finally this happens in some complex cases when validity checks are
5254 -- enabled, where the same subtype declaration may be analyzed twice.
5255 -- This can happen if the subtype is created by the preanalysis of
5256 -- an attribute tht gives the range of a loop statement, and the loop
5257 -- itself appears within an if_statement that will be rewritten during
5258 -- expansion.
5259
5260 if Skip
5261 or else (Present (Etype (Id))
5262 and then (Is_Private_Type (Etype (Id))
5263 or else Is_Task_Type (Etype (Id))
5264 or else Is_Rewrite_Substitution (N)))
5265 then
5266 null;
5267
5268 elsif Current_Entity (Id) = Id then
5269 null;
5270
5271 else
5272 Enter_Name (Id);
5273 end if;
5274
5275 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5276
5277 -- Class-wide equivalent types of records with unknown discriminants
5278 -- involve the generation of an itype which serves as the private view
5279 -- of a constrained record subtype. In such cases the base type of the
5280 -- current subtype we are processing is the private itype. Use the full
5281 -- of the private itype when decorating various attributes.
5282
5283 if Is_Itype (T)
5284 and then Is_Private_Type (T)
5285 and then Present (Full_View (T))
5286 then
5287 T := Full_View (T);
5288 end if;
5289
5290 -- Inherit common attributes
5291
5292 Set_Is_Volatile (Id, Is_Volatile (T));
5293 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5294 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5295 Set_Convention (Id, Convention (T));
5296
5297 -- If ancestor has predicates then so does the subtype, and in addition
5298 -- we must delay the freeze to properly arrange predicate inheritance.
5299
5300 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5301 -- in which T = ID, so the above tests and assignments do nothing???
5302
5303 if Has_Predicates (T)
5304 or else (Present (Ancestor_Subtype (T))
5305 and then Has_Predicates (Ancestor_Subtype (T)))
5306 then
5307 Set_Has_Predicates (Id);
5308 Set_Has_Delayed_Freeze (Id);
5309
5310 -- Generated subtypes inherit the predicate function from the parent
5311 -- (no aspects to examine on the generated declaration).
5312
5313 if not Comes_From_Source (N) then
5314 Set_Ekind (Id, Ekind (T));
5315
5316 if Present (Predicate_Function (Id)) then
5317 null;
5318
5319 elsif Present (Predicate_Function (T)) then
5320 Set_Predicate_Function (Id, Predicate_Function (T));
5321
5322 elsif Present (Ancestor_Subtype (T))
5323 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5324 then
5325 Set_Predicate_Function (Id,
5326 Predicate_Function (Ancestor_Subtype (T)));
5327 end if;
5328 end if;
5329 end if;
5330
5331 -- In the case where there is no constraint given in the subtype
5332 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5333 -- semantic attributes must be established here.
5334
5335 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5336 Set_Etype (Id, Base_Type (T));
5337
5338 case Ekind (T) is
5339 when Array_Kind =>
5340 Set_Ekind (Id, E_Array_Subtype);
5341 Copy_Array_Subtype_Attributes (Id, T);
5342
5343 when Decimal_Fixed_Point_Kind =>
5344 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5345 Set_Digits_Value (Id, Digits_Value (T));
5346 Set_Delta_Value (Id, Delta_Value (T));
5347 Set_Scale_Value (Id, Scale_Value (T));
5348 Set_Small_Value (Id, Small_Value (T));
5349 Set_Scalar_Range (Id, Scalar_Range (T));
5350 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5351 Set_Is_Constrained (Id, Is_Constrained (T));
5352 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5353 Set_RM_Size (Id, RM_Size (T));
5354
5355 when Enumeration_Kind =>
5356 Set_Ekind (Id, E_Enumeration_Subtype);
5357 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5358 Set_Scalar_Range (Id, Scalar_Range (T));
5359 Set_Is_Character_Type (Id, Is_Character_Type (T));
5360 Set_Is_Constrained (Id, Is_Constrained (T));
5361 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5362 Set_RM_Size (Id, RM_Size (T));
5363
5364 when Ordinary_Fixed_Point_Kind =>
5365 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5366 Set_Scalar_Range (Id, Scalar_Range (T));
5367 Set_Small_Value (Id, Small_Value (T));
5368 Set_Delta_Value (Id, Delta_Value (T));
5369 Set_Is_Constrained (Id, Is_Constrained (T));
5370 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5371 Set_RM_Size (Id, RM_Size (T));
5372
5373 when Float_Kind =>
5374 Set_Ekind (Id, E_Floating_Point_Subtype);
5375 Set_Scalar_Range (Id, Scalar_Range (T));
5376 Set_Digits_Value (Id, Digits_Value (T));
5377 Set_Is_Constrained (Id, Is_Constrained (T));
5378
5379 -- If the floating point type has dimensions, these will be
5380 -- inherited subsequently when Analyze_Dimensions is called.
5381
5382 when Signed_Integer_Kind =>
5383 Set_Ekind (Id, E_Signed_Integer_Subtype);
5384 Set_Scalar_Range (Id, Scalar_Range (T));
5385 Set_Is_Constrained (Id, Is_Constrained (T));
5386 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5387 Set_RM_Size (Id, RM_Size (T));
5388
5389 when Modular_Integer_Kind =>
5390 Set_Ekind (Id, E_Modular_Integer_Subtype);
5391 Set_Scalar_Range (Id, Scalar_Range (T));
5392 Set_Is_Constrained (Id, Is_Constrained (T));
5393 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5394 Set_RM_Size (Id, RM_Size (T));
5395
5396 when Class_Wide_Kind =>
5397 Set_Ekind (Id, E_Class_Wide_Subtype);
5398 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5399 Set_Cloned_Subtype (Id, T);
5400 Set_Is_Tagged_Type (Id, True);
5401 Set_Has_Unknown_Discriminants
5402 (Id, True);
5403 Set_No_Tagged_Streams_Pragma
5404 (Id, No_Tagged_Streams_Pragma (T));
5405
5406 if Ekind (T) = E_Class_Wide_Subtype then
5407 Set_Equivalent_Type (Id, Equivalent_Type (T));
5408 end if;
5409
5410 when E_Record_Subtype
5411 | E_Record_Type
5412 =>
5413 Set_Ekind (Id, E_Record_Subtype);
5414
5415 -- Subtype declarations introduced for formal type parameters
5416 -- in generic instantiations should inherit the Size value of
5417 -- the type they rename.
5418
5419 if Present (Generic_Parent_Type (N)) then
5420 Set_RM_Size (Id, RM_Size (T));
5421 end if;
5422
5423 if Ekind (T) = E_Record_Subtype
5424 and then Present (Cloned_Subtype (T))
5425 then
5426 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5427 else
5428 Set_Cloned_Subtype (Id, T);
5429 end if;
5430
5431 Set_First_Entity (Id, First_Entity (T));
5432 Set_Last_Entity (Id, Last_Entity (T));
5433 Set_Has_Discriminants (Id, Has_Discriminants (T));
5434 Set_Is_Constrained (Id, Is_Constrained (T));
5435 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5436 Set_Has_Implicit_Dereference
5437 (Id, Has_Implicit_Dereference (T));
5438 Set_Has_Unknown_Discriminants
5439 (Id, Has_Unknown_Discriminants (T));
5440
5441 if Has_Discriminants (T) then
5442 Set_Discriminant_Constraint
5443 (Id, Discriminant_Constraint (T));
5444 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5445
5446 elsif Has_Unknown_Discriminants (Id) then
5447 Set_Discriminant_Constraint (Id, No_Elist);
5448 end if;
5449
5450 if Is_Tagged_Type (T) then
5451 Set_Is_Tagged_Type (Id, True);
5452 Set_No_Tagged_Streams_Pragma
5453 (Id, No_Tagged_Streams_Pragma (T));
5454 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5455 Set_Direct_Primitive_Operations
5456 (Id, Direct_Primitive_Operations (T));
5457 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5458
5459 if Is_Interface (T) then
5460 Set_Is_Interface (Id);
5461 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5462 end if;
5463 end if;
5464
5465 when Private_Kind =>
5466 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5467 Set_Has_Discriminants (Id, Has_Discriminants (T));
5468 Set_Is_Constrained (Id, Is_Constrained (T));
5469 Set_First_Entity (Id, First_Entity (T));
5470 Set_Last_Entity (Id, Last_Entity (T));
5471 Set_Private_Dependents (Id, New_Elmt_List);
5472 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5473 Set_Has_Implicit_Dereference
5474 (Id, Has_Implicit_Dereference (T));
5475 Set_Has_Unknown_Discriminants
5476 (Id, Has_Unknown_Discriminants (T));
5477 Set_Known_To_Have_Preelab_Init
5478 (Id, Known_To_Have_Preelab_Init (T));
5479
5480 if Is_Tagged_Type (T) then
5481 Set_Is_Tagged_Type (Id);
5482 Set_No_Tagged_Streams_Pragma (Id,
5483 No_Tagged_Streams_Pragma (T));
5484 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5485 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5486 Set_Direct_Primitive_Operations (Id,
5487 Direct_Primitive_Operations (T));
5488 end if;
5489
5490 -- In general the attributes of the subtype of a private type
5491 -- are the attributes of the partial view of parent. However,
5492 -- the full view may be a discriminated type, and the subtype
5493 -- must share the discriminant constraint to generate correct
5494 -- calls to initialization procedures.
5495
5496 if Has_Discriminants (T) then
5497 Set_Discriminant_Constraint
5498 (Id, Discriminant_Constraint (T));
5499 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5500
5501 elsif Present (Full_View (T))
5502 and then Has_Discriminants (Full_View (T))
5503 then
5504 Set_Discriminant_Constraint
5505 (Id, Discriminant_Constraint (Full_View (T)));
5506 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5507
5508 -- This would seem semantically correct, but apparently
5509 -- generates spurious errors about missing components ???
5510
5511 -- Set_Has_Discriminants (Id);
5512 end if;
5513
5514 Prepare_Private_Subtype_Completion (Id, N);
5515
5516 -- If this is the subtype of a constrained private type with
5517 -- discriminants that has got a full view and we also have
5518 -- built a completion just above, show that the completion
5519 -- is a clone of the full view to the back-end.
5520
5521 if Has_Discriminants (T)
5522 and then not Has_Unknown_Discriminants (T)
5523 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5524 and then Present (Full_View (T))
5525 and then Present (Full_View (Id))
5526 then
5527 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5528 end if;
5529
5530 when Access_Kind =>
5531 Set_Ekind (Id, E_Access_Subtype);
5532 Set_Is_Constrained (Id, Is_Constrained (T));
5533 Set_Is_Access_Constant
5534 (Id, Is_Access_Constant (T));
5535 Set_Directly_Designated_Type
5536 (Id, Designated_Type (T));
5537 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5538
5539 -- A Pure library_item must not contain the declaration of a
5540 -- named access type, except within a subprogram, generic
5541 -- subprogram, task unit, or protected unit, or if it has
5542 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5543
5544 if Comes_From_Source (Id)
5545 and then In_Pure_Unit
5546 and then not In_Subprogram_Task_Protected_Unit
5547 and then not No_Pool_Assigned (Id)
5548 then
5549 Error_Msg_N
5550 ("named access types not allowed in pure unit", N);
5551 end if;
5552
5553 when Concurrent_Kind =>
5554 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5555 Set_Corresponding_Record_Type (Id,
5556 Corresponding_Record_Type (T));
5557 Set_First_Entity (Id, First_Entity (T));
5558 Set_First_Private_Entity (Id, First_Private_Entity (T));
5559 Set_Has_Discriminants (Id, Has_Discriminants (T));
5560 Set_Is_Constrained (Id, Is_Constrained (T));
5561 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5562 Set_Last_Entity (Id, Last_Entity (T));
5563
5564 if Is_Tagged_Type (T) then
5565 Set_No_Tagged_Streams_Pragma
5566 (Id, No_Tagged_Streams_Pragma (T));
5567 end if;
5568
5569 if Has_Discriminants (T) then
5570 Set_Discriminant_Constraint
5571 (Id, Discriminant_Constraint (T));
5572 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5573 end if;
5574
5575 when Incomplete_Kind =>
5576 if Ada_Version >= Ada_2005 then
5577
5578 -- In Ada 2005 an incomplete type can be explicitly tagged:
5579 -- propagate indication. Note that we also have to include
5580 -- subtypes for Ada 2012 extended use of incomplete types.
5581
5582 Set_Ekind (Id, E_Incomplete_Subtype);
5583 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5584 Set_Private_Dependents (Id, New_Elmt_List);
5585
5586 if Is_Tagged_Type (Id) then
5587 Set_No_Tagged_Streams_Pragma
5588 (Id, No_Tagged_Streams_Pragma (T));
5589 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5590 end if;
5591
5592 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5593 -- incomplete type visible through a limited with clause.
5594
5595 if From_Limited_With (T)
5596 and then Present (Non_Limited_View (T))
5597 then
5598 Set_From_Limited_With (Id);
5599 Set_Non_Limited_View (Id, Non_Limited_View (T));
5600
5601 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5602 -- to the private dependents of the original incomplete
5603 -- type for future transformation.
5604
5605 else
5606 Append_Elmt (Id, Private_Dependents (T));
5607 end if;
5608
5609 -- If the subtype name denotes an incomplete type an error
5610 -- was already reported by Process_Subtype.
5611
5612 else
5613 Set_Etype (Id, Any_Type);
5614 end if;
5615
5616 when others =>
5617 raise Program_Error;
5618 end case;
5619
5620 -- If there is no constraint in the subtype indication, the
5621 -- declared entity inherits predicates from the parent.
5622
5623 Inherit_Predicate_Flags (Id, T);
5624 end if;
5625
5626 if Etype (Id) = Any_Type then
5627 goto Leave;
5628 end if;
5629
5630 -- Some common processing on all types
5631
5632 Set_Size_Info (Id, T);
5633 Set_First_Rep_Item (Id, First_Rep_Item (T));
5634
5635 -- If the parent type is a generic actual, so is the subtype. This may
5636 -- happen in a nested instance. Why Comes_From_Source test???
5637
5638 if not Comes_From_Source (N) then
5639 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5640 end if;
5641
5642 -- If this is a subtype declaration for an actual in an instance,
5643 -- inherit static and dynamic predicates if any.
5644
5645 -- If declaration has no aspect specifications, inherit predicate
5646 -- info as well. Unclear how to handle the case of both specified
5647 -- and inherited predicates ??? Other inherited aspects, such as
5648 -- invariants, should be OK, but the combination with later pragmas
5649 -- may also require special merging.
5650
5651 if Has_Predicates (T)
5652 and then Present (Predicate_Function (T))
5653 and then
5654 ((In_Instance and then not Comes_From_Source (N))
5655 or else No (Aspect_Specifications (N)))
5656 then
5657 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5658
5659 if Has_Static_Predicate (T) then
5660 Set_Has_Static_Predicate (Id);
5661 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5662 end if;
5663 end if;
5664
5665 -- If the base type is a scalar type, or else if there is no
5666 -- constraint, the atomic flag is inherited by the subtype.
5667
5668 if Is_Scalar_Type (Id)
5669 or else Is_Entity_Name (Subtype_Indication (N))
5670 then
5671 Set_Is_Atomic (Id, Is_Atomic (T));
5672 end if;
5673
5674 -- Remaining processing depends on characteristics of base type
5675
5676 T := Etype (Id);
5677
5678 Set_Is_Immediately_Visible (Id, True);
5679 Set_Depends_On_Private (Id, Has_Private_Component (T));
5680 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5681
5682 if Is_Interface (T) then
5683 Set_Is_Interface (Id);
5684 end if;
5685
5686 if Present (Generic_Parent_Type (N))
5687 and then
5688 (Nkind (Parent (Generic_Parent_Type (N))) /=
5689 N_Formal_Type_Declaration
5690 or else Nkind (Formal_Type_Definition
5691 (Parent (Generic_Parent_Type (N)))) /=
5692 N_Formal_Private_Type_Definition)
5693 then
5694 if Is_Tagged_Type (Id) then
5695
5696 -- If this is a generic actual subtype for a synchronized type,
5697 -- the primitive operations are those of the corresponding record
5698 -- for which there is a separate subtype declaration.
5699
5700 if Is_Concurrent_Type (Id) then
5701 null;
5702 elsif Is_Class_Wide_Type (Id) then
5703 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5704 else
5705 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5706 end if;
5707
5708 elsif Scope (Etype (Id)) /= Standard_Standard then
5709 Derive_Subprograms (Generic_Parent_Type (N), Id);
5710 end if;
5711 end if;
5712
5713 if Is_Private_Type (T) and then Present (Full_View (T)) then
5714 Conditional_Delay (Id, Full_View (T));
5715
5716 -- The subtypes of components or subcomponents of protected types
5717 -- do not need freeze nodes, which would otherwise appear in the
5718 -- wrong scope (before the freeze node for the protected type). The
5719 -- proper subtypes are those of the subcomponents of the corresponding
5720 -- record.
5721
5722 elsif Ekind (Scope (Id)) /= E_Protected_Type
5723 and then Present (Scope (Scope (Id))) -- error defense
5724 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5725 then
5726 Conditional_Delay (Id, T);
5727 end if;
5728
5729 -- If we have a subtype of an incomplete type whose full type is a
5730 -- derived numeric type, we need to have a freeze node for the subtype.
5731 -- Otherwise gigi will complain while computing the (static) bounds of
5732 -- the subtype.
5733
5734 if Is_Itype (T)
5735 and then Is_Elementary_Type (Id)
5736 and then Etype (Id) /= Id
5737 then
5738 declare
5739 Partial : constant Entity_Id :=
5740 Incomplete_Or_Partial_View (First_Subtype (Id));
5741 begin
5742 if Present (Partial)
5743 and then Ekind (Partial) = E_Incomplete_Type
5744 then
5745 Set_Has_Delayed_Freeze (Id);
5746 end if;
5747 end;
5748 end if;
5749
5750 -- Check that Constraint_Error is raised for a scalar subtype indication
5751 -- when the lower or upper bound of a non-null range lies outside the
5752 -- range of the type mark.
5753
5754 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5755 if Is_Scalar_Type (Etype (Id))
5756 and then Scalar_Range (Id) /=
5757 Scalar_Range
5758 (Etype (Subtype_Mark (Subtype_Indication (N))))
5759 then
5760 Apply_Range_Check
5761 (Scalar_Range (Id),
5762 Etype (Subtype_Mark (Subtype_Indication (N))));
5763
5764 -- In the array case, check compatibility for each index
5765
5766 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5767 then
5768 -- This really should be a subprogram that finds the indications
5769 -- to check???
5770
5771 declare
5772 Subt_Index : Node_Id := First_Index (Id);
5773 Target_Index : Node_Id :=
5774 First_Index (Etype
5775 (Subtype_Mark (Subtype_Indication (N))));
5776
5777 begin
5778 while Present (Subt_Index) loop
5779 if ((Nkind (Subt_Index) = N_Identifier
5780 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5781 or else Nkind (Subt_Index) = N_Subtype_Indication)
5782 and then
5783 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5784 then
5785 declare
5786 Target_Typ : constant Entity_Id :=
5787 Etype (Target_Index);
5788 begin
5789 R_Checks :=
5790 Get_Range_Checks
5791 (Scalar_Range (Etype (Subt_Index)),
5792 Target_Typ,
5793 Etype (Subt_Index),
5794 Defining_Identifier (N));
5795
5796 Insert_Range_Checks
5797 (R_Checks,
5798 N,
5799 Target_Typ,
5800 Sloc (Defining_Identifier (N)));
5801 end;
5802 end if;
5803
5804 Next_Index (Subt_Index);
5805 Next_Index (Target_Index);
5806 end loop;
5807 end;
5808 end if;
5809 end if;
5810
5811 Set_Optimize_Alignment_Flags (Id);
5812 Check_Eliminated (Id);
5813
5814 <<Leave>>
5815 if Has_Aspects (N) then
5816 Analyze_Aspect_Specifications (N, Id);
5817 end if;
5818
5819 Analyze_Dimension (N);
5820
5821 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5822 -- indications on composite types where the constraints are dynamic.
5823 -- Note that object declarations and aggregates generate implicit
5824 -- subtype declarations, which this covers. One special case is that the
5825 -- implicitly generated "=" for discriminated types includes an
5826 -- offending subtype declaration, which is harmless, so we ignore it
5827 -- here.
5828
5829 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5830 declare
5831 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5832 begin
5833 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5834 and then not (Is_Internal (Id)
5835 and then Is_TSS (Scope (Id),
5836 TSS_Composite_Equality))
5837 and then not Within_Init_Proc
5838 and then not All_Composite_Constraints_Static (Cstr)
5839 then
5840 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5841 end if;
5842 end;
5843 end if;
5844 end Analyze_Subtype_Declaration;
5845
5846 --------------------------------
5847 -- Analyze_Subtype_Indication --
5848 --------------------------------
5849
5850 procedure Analyze_Subtype_Indication (N : Node_Id) is
5851 T : constant Entity_Id := Subtype_Mark (N);
5852 R : constant Node_Id := Range_Expression (Constraint (N));
5853
5854 begin
5855 Analyze (T);
5856
5857 if R /= Error then
5858 Analyze (R);
5859 Set_Etype (N, Etype (R));
5860 Resolve (R, Entity (T));
5861 else
5862 Set_Error_Posted (R);
5863 Set_Error_Posted (T);
5864 end if;
5865 end Analyze_Subtype_Indication;
5866
5867 --------------------------
5868 -- Analyze_Variant_Part --
5869 --------------------------
5870
5871 procedure Analyze_Variant_Part (N : Node_Id) is
5872 Discr_Name : Node_Id;
5873 Discr_Type : Entity_Id;
5874
5875 procedure Process_Variant (A : Node_Id);
5876 -- Analyze declarations for a single variant
5877
5878 package Analyze_Variant_Choices is
5879 new Generic_Analyze_Choices (Process_Variant);
5880 use Analyze_Variant_Choices;
5881
5882 ---------------------
5883 -- Process_Variant --
5884 ---------------------
5885
5886 procedure Process_Variant (A : Node_Id) is
5887 CL : constant Node_Id := Component_List (A);
5888 begin
5889 if not Null_Present (CL) then
5890 Analyze_Declarations (Component_Items (CL));
5891
5892 if Present (Variant_Part (CL)) then
5893 Analyze (Variant_Part (CL));
5894 end if;
5895 end if;
5896 end Process_Variant;
5897
5898 -- Start of processing for Analyze_Variant_Part
5899
5900 begin
5901 Discr_Name := Name (N);
5902 Analyze (Discr_Name);
5903
5904 -- If Discr_Name bad, get out (prevent cascaded errors)
5905
5906 if Etype (Discr_Name) = Any_Type then
5907 return;
5908 end if;
5909
5910 -- Check invalid discriminant in variant part
5911
5912 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5913 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5914 end if;
5915
5916 Discr_Type := Etype (Entity (Discr_Name));
5917
5918 if not Is_Discrete_Type (Discr_Type) then
5919 Error_Msg_N
5920 ("discriminant in a variant part must be of a discrete type",
5921 Name (N));
5922 return;
5923 end if;
5924
5925 -- Now analyze the choices, which also analyzes the declarations that
5926 -- are associated with each choice.
5927
5928 Analyze_Choices (Variants (N), Discr_Type);
5929
5930 -- Note: we used to instantiate and call Check_Choices here to check
5931 -- that the choices covered the discriminant, but it's too early to do
5932 -- that because of statically predicated subtypes, whose analysis may
5933 -- be deferred to their freeze point which may be as late as the freeze
5934 -- point of the containing record. So this call is now to be found in
5935 -- Freeze_Record_Declaration.
5936
5937 end Analyze_Variant_Part;
5938
5939 ----------------------------
5940 -- Array_Type_Declaration --
5941 ----------------------------
5942
5943 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5944 Component_Def : constant Node_Id := Component_Definition (Def);
5945 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5946 P : constant Node_Id := Parent (Def);
5947 Element_Type : Entity_Id;
5948 Implicit_Base : Entity_Id;
5949 Index : Node_Id;
5950 Nb_Index : Nat;
5951 Priv : Entity_Id;
5952 Related_Id : Entity_Id := Empty;
5953
5954 begin
5955 if Nkind (Def) = N_Constrained_Array_Definition then
5956 Index := First (Discrete_Subtype_Definitions (Def));
5957 else
5958 Index := First (Subtype_Marks (Def));
5959 end if;
5960
5961 -- Find proper names for the implicit types which may be public. In case
5962 -- of anonymous arrays we use the name of the first object of that type
5963 -- as prefix.
5964
5965 if No (T) then
5966 Related_Id := Defining_Identifier (P);
5967 else
5968 Related_Id := T;
5969 end if;
5970
5971 Nb_Index := 1;
5972 while Present (Index) loop
5973 Analyze (Index);
5974
5975 -- Test for odd case of trying to index a type by the type itself
5976
5977 if Is_Entity_Name (Index) and then Entity (Index) = T then
5978 Error_Msg_N ("type& cannot be indexed by itself", Index);
5979 Set_Entity (Index, Standard_Boolean);
5980 Set_Etype (Index, Standard_Boolean);
5981 end if;
5982
5983 -- Add a subtype declaration for each index of private array type
5984 -- declaration whose type is also private. For example:
5985
5986 -- package Pkg is
5987 -- type Index is private;
5988 -- private
5989 -- type Table is array (Index) of ...
5990 -- end;
5991
5992 -- This is currently required by the expander for the internally
5993 -- generated equality subprogram of records with variant parts in
5994 -- which the type of some component is such a private type. And it
5995 -- also helps semantic analysis in peculiar cases where the array
5996 -- type is referenced from an instance but not the index directly.
5997
5998 if Is_Package_Or_Generic_Package (Current_Scope)
5999 and then In_Private_Part (Current_Scope)
6000 and then Has_Private_Declaration (Etype (Index))
6001 and then Scope (Etype (Index)) = Current_Scope
6002 then
6003 declare
6004 Loc : constant Source_Ptr := Sloc (Def);
6005 Decl : Entity_Id;
6006 New_E : Entity_Id;
6007
6008 begin
6009 New_E := Make_Temporary (Loc, 'T');
6010 Set_Is_Internal (New_E);
6011
6012 Decl :=
6013 Make_Subtype_Declaration (Loc,
6014 Defining_Identifier => New_E,
6015 Subtype_Indication =>
6016 New_Occurrence_Of (Etype (Index), Loc));
6017
6018 Insert_Before (Parent (Def), Decl);
6019 Analyze (Decl);
6020 Set_Etype (Index, New_E);
6021
6022 -- If the index is a range or a subtype indication it carries
6023 -- no entity. Example:
6024
6025 -- package Pkg is
6026 -- type T is private;
6027 -- private
6028 -- type T is new Natural;
6029 -- Table : array (T(1) .. T(10)) of Boolean;
6030 -- end Pkg;
6031
6032 -- Otherwise the type of the reference is its entity.
6033
6034 if Is_Entity_Name (Index) then
6035 Set_Entity (Index, New_E);
6036 end if;
6037 end;
6038 end if;
6039
6040 Make_Index (Index, P, Related_Id, Nb_Index);
6041
6042 -- Check error of subtype with predicate for index type
6043
6044 Bad_Predicated_Subtype_Use
6045 ("subtype& has predicate, not allowed as index subtype",
6046 Index, Etype (Index));
6047
6048 -- Move to next index
6049
6050 Next_Index (Index);
6051 Nb_Index := Nb_Index + 1;
6052 end loop;
6053
6054 -- Process subtype indication if one is present
6055
6056 if Present (Component_Typ) then
6057 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6058 Set_Etype (Component_Typ, Element_Type);
6059
6060 -- Ada 2005 (AI-230): Access Definition case
6061
6062 else pragma Assert (Present (Access_Definition (Component_Def)));
6063
6064 -- Indicate that the anonymous access type is created by the
6065 -- array type declaration.
6066
6067 Element_Type := Access_Definition
6068 (Related_Nod => P,
6069 N => Access_Definition (Component_Def));
6070 Set_Is_Local_Anonymous_Access (Element_Type);
6071
6072 -- Propagate the parent. This field is needed if we have to generate
6073 -- the master_id associated with an anonymous access to task type
6074 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6075
6076 Set_Parent (Element_Type, Parent (T));
6077
6078 -- Ada 2005 (AI-230): In case of components that are anonymous access
6079 -- types the level of accessibility depends on the enclosing type
6080 -- declaration
6081
6082 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6083
6084 -- Ada 2005 (AI-254)
6085
6086 declare
6087 CD : constant Node_Id :=
6088 Access_To_Subprogram_Definition
6089 (Access_Definition (Component_Def));
6090 begin
6091 if Present (CD) and then Protected_Present (CD) then
6092 Element_Type :=
6093 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6094 end if;
6095 end;
6096 end if;
6097
6098 -- Constrained array case
6099
6100 if No (T) then
6101 T := Create_Itype (E_Void, P, Related_Id, 'T');
6102 end if;
6103
6104 if Nkind (Def) = N_Constrained_Array_Definition then
6105
6106 -- Establish Implicit_Base as unconstrained base type
6107
6108 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6109
6110 Set_Etype (Implicit_Base, Implicit_Base);
6111 Set_Scope (Implicit_Base, Current_Scope);
6112 Set_Has_Delayed_Freeze (Implicit_Base);
6113 Set_Default_SSO (Implicit_Base);
6114
6115 -- The constrained array type is a subtype of the unconstrained one
6116
6117 Set_Ekind (T, E_Array_Subtype);
6118 Init_Size_Align (T);
6119 Set_Etype (T, Implicit_Base);
6120 Set_Scope (T, Current_Scope);
6121 Set_Is_Constrained (T);
6122 Set_First_Index (T,
6123 First (Discrete_Subtype_Definitions (Def)));
6124 Set_Has_Delayed_Freeze (T);
6125
6126 -- Complete setup of implicit base type
6127
6128 Set_Component_Size (Implicit_Base, Uint_0);
6129 Set_Component_Type (Implicit_Base, Element_Type);
6130 Set_Finalize_Storage_Only
6131 (Implicit_Base,
6132 Finalize_Storage_Only (Element_Type));
6133 Set_First_Index (Implicit_Base, First_Index (T));
6134 Set_Has_Controlled_Component
6135 (Implicit_Base,
6136 Has_Controlled_Component (Element_Type)
6137 or else Is_Controlled (Element_Type));
6138 Set_Packed_Array_Impl_Type
6139 (Implicit_Base, Empty);
6140
6141 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6142
6143 -- Unconstrained array case
6144
6145 else
6146 Set_Ekind (T, E_Array_Type);
6147 Init_Size_Align (T);
6148 Set_Etype (T, T);
6149 Set_Scope (T, Current_Scope);
6150 Set_Component_Size (T, Uint_0);
6151 Set_Is_Constrained (T, False);
6152 Set_First_Index (T, First (Subtype_Marks (Def)));
6153 Set_Has_Delayed_Freeze (T, True);
6154 Propagate_Concurrent_Flags (T, Element_Type);
6155 Set_Has_Controlled_Component (T, Has_Controlled_Component
6156 (Element_Type)
6157 or else
6158 Is_Controlled (Element_Type));
6159 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6160 (Element_Type));
6161 Set_Default_SSO (T);
6162 end if;
6163
6164 -- Common attributes for both cases
6165
6166 Set_Component_Type (Base_Type (T), Element_Type);
6167 Set_Packed_Array_Impl_Type (T, Empty);
6168
6169 if Aliased_Present (Component_Definition (Def)) then
6170 Set_Has_Aliased_Components (Etype (T));
6171
6172 -- AI12-001: All aliased objects are considered to be specified as
6173 -- independently addressable (RM C.6(8.1/4)).
6174
6175 Set_Has_Independent_Components (Etype (T));
6176 end if;
6177
6178 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6179 -- array type to ensure that objects of this type are initialized.
6180
6181 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6182 Set_Can_Never_Be_Null (T);
6183
6184 if Null_Exclusion_Present (Component_Definition (Def))
6185
6186 -- No need to check itypes because in their case this check was
6187 -- done at their point of creation
6188
6189 and then not Is_Itype (Element_Type)
6190 then
6191 Error_Msg_N
6192 ("`NOT NULL` not allowed (null already excluded)",
6193 Subtype_Indication (Component_Definition (Def)));
6194 end if;
6195 end if;
6196
6197 Priv := Private_Component (Element_Type);
6198
6199 if Present (Priv) then
6200
6201 -- Check for circular definitions
6202
6203 if Priv = Any_Type then
6204 Set_Component_Type (Etype (T), Any_Type);
6205
6206 -- There is a gap in the visibility of operations on the composite
6207 -- type only if the component type is defined in a different scope.
6208
6209 elsif Scope (Priv) = Current_Scope then
6210 null;
6211
6212 elsif Is_Limited_Type (Priv) then
6213 Set_Is_Limited_Composite (Etype (T));
6214 Set_Is_Limited_Composite (T);
6215 else
6216 Set_Is_Private_Composite (Etype (T));
6217 Set_Is_Private_Composite (T);
6218 end if;
6219 end if;
6220
6221 -- A syntax error in the declaration itself may lead to an empty index
6222 -- list, in which case do a minimal patch.
6223
6224 if No (First_Index (T)) then
6225 Error_Msg_N ("missing index definition in array type declaration", T);
6226
6227 declare
6228 Indexes : constant List_Id :=
6229 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6230 begin
6231 Set_Discrete_Subtype_Definitions (Def, Indexes);
6232 Set_First_Index (T, First (Indexes));
6233 return;
6234 end;
6235 end if;
6236
6237 -- Create a concatenation operator for the new type. Internal array
6238 -- types created for packed entities do not need such, they are
6239 -- compatible with the user-defined type.
6240
6241 if Number_Dimensions (T) = 1
6242 and then not Is_Packed_Array_Impl_Type (T)
6243 then
6244 New_Concatenation_Op (T);
6245 end if;
6246
6247 -- In the case of an unconstrained array the parser has already verified
6248 -- that all the indexes are unconstrained but we still need to make sure
6249 -- that the element type is constrained.
6250
6251 if not Is_Definite_Subtype (Element_Type) then
6252 Error_Msg_N
6253 ("unconstrained element type in array declaration",
6254 Subtype_Indication (Component_Def));
6255
6256 elsif Is_Abstract_Type (Element_Type) then
6257 Error_Msg_N
6258 ("the type of a component cannot be abstract",
6259 Subtype_Indication (Component_Def));
6260 end if;
6261
6262 -- There may be an invariant declared for the component type, but
6263 -- the construction of the component invariant checking procedure
6264 -- takes place during expansion.
6265 end Array_Type_Declaration;
6266
6267 ------------------------------------------------------
6268 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6269 ------------------------------------------------------
6270
6271 function Replace_Anonymous_Access_To_Protected_Subprogram
6272 (N : Node_Id) return Entity_Id
6273 is
6274 Loc : constant Source_Ptr := Sloc (N);
6275
6276 Curr_Scope : constant Scope_Stack_Entry :=
6277 Scope_Stack.Table (Scope_Stack.Last);
6278
6279 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6280
6281 Acc : Node_Id;
6282 -- Access definition in declaration
6283
6284 Comp : Node_Id;
6285 -- Object definition or formal definition with an access definition
6286
6287 Decl : Node_Id;
6288 -- Declaration of anonymous access to subprogram type
6289
6290 Spec : Node_Id;
6291 -- Original specification in access to subprogram
6292
6293 P : Node_Id;
6294
6295 begin
6296 Set_Is_Internal (Anon);
6297
6298 case Nkind (N) is
6299 when N_Constrained_Array_Definition
6300 | N_Component_Declaration
6301 | N_Unconstrained_Array_Definition
6302 =>
6303 Comp := Component_Definition (N);
6304 Acc := Access_Definition (Comp);
6305
6306 when N_Discriminant_Specification =>
6307 Comp := Discriminant_Type (N);
6308 Acc := Comp;
6309
6310 when N_Parameter_Specification =>
6311 Comp := Parameter_Type (N);
6312 Acc := Comp;
6313
6314 when N_Access_Function_Definition =>
6315 Comp := Result_Definition (N);
6316 Acc := Comp;
6317
6318 when N_Object_Declaration =>
6319 Comp := Object_Definition (N);
6320 Acc := Comp;
6321
6322 when N_Function_Specification =>
6323 Comp := Result_Definition (N);
6324 Acc := Comp;
6325
6326 when others =>
6327 raise Program_Error;
6328 end case;
6329
6330 Spec := Access_To_Subprogram_Definition (Acc);
6331
6332 Decl :=
6333 Make_Full_Type_Declaration (Loc,
6334 Defining_Identifier => Anon,
6335 Type_Definition => Copy_Separate_Tree (Spec));
6336
6337 Mark_Rewrite_Insertion (Decl);
6338
6339 -- Insert the new declaration in the nearest enclosing scope. If the
6340 -- parent is a body and N is its return type, the declaration belongs
6341 -- in the enclosing scope. Likewise if N is the type of a parameter.
6342
6343 P := Parent (N);
6344
6345 if Nkind (N) = N_Function_Specification
6346 and then Nkind (P) = N_Subprogram_Body
6347 then
6348 P := Parent (P);
6349 elsif Nkind (N) = N_Parameter_Specification
6350 and then Nkind (P) in N_Subprogram_Specification
6351 and then Nkind (Parent (P)) = N_Subprogram_Body
6352 then
6353 P := Parent (Parent (P));
6354 end if;
6355
6356 while Present (P) and then not Has_Declarations (P) loop
6357 P := Parent (P);
6358 end loop;
6359
6360 pragma Assert (Present (P));
6361
6362 if Nkind (P) = N_Package_Specification then
6363 Prepend (Decl, Visible_Declarations (P));
6364 else
6365 Prepend (Decl, Declarations (P));
6366 end if;
6367
6368 -- Replace the anonymous type with an occurrence of the new declaration.
6369 -- In all cases the rewritten node does not have the null-exclusion
6370 -- attribute because (if present) it was already inherited by the
6371 -- anonymous entity (Anon). Thus, in case of components we do not
6372 -- inherit this attribute.
6373
6374 if Nkind (N) = N_Parameter_Specification then
6375 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6376 Set_Etype (Defining_Identifier (N), Anon);
6377 Set_Null_Exclusion_Present (N, False);
6378
6379 elsif Nkind (N) = N_Object_Declaration then
6380 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6381 Set_Etype (Defining_Identifier (N), Anon);
6382
6383 elsif Nkind (N) = N_Access_Function_Definition then
6384 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6385
6386 elsif Nkind (N) = N_Function_Specification then
6387 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6388 Set_Etype (Defining_Unit_Name (N), Anon);
6389
6390 else
6391 Rewrite (Comp,
6392 Make_Component_Definition (Loc,
6393 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6394 end if;
6395
6396 Mark_Rewrite_Insertion (Comp);
6397
6398 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6399 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6400 and then not Is_Type (Current_Scope))
6401 then
6402
6403 -- Declaration can be analyzed in the current scope.
6404
6405 Analyze (Decl);
6406
6407 else
6408 -- Temporarily remove the current scope (record or subprogram) from
6409 -- the stack to add the new declarations to the enclosing scope.
6410 -- The anonymous entity is an Itype with the proper attributes.
6411
6412 Scope_Stack.Decrement_Last;
6413 Analyze (Decl);
6414 Set_Is_Itype (Anon);
6415 Set_Associated_Node_For_Itype (Anon, N);
6416 Scope_Stack.Append (Curr_Scope);
6417 end if;
6418
6419 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6420 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6421 return Anon;
6422 end Replace_Anonymous_Access_To_Protected_Subprogram;
6423
6424 -------------------------------
6425 -- Build_Derived_Access_Type --
6426 -------------------------------
6427
6428 procedure Build_Derived_Access_Type
6429 (N : Node_Id;
6430 Parent_Type : Entity_Id;
6431 Derived_Type : Entity_Id)
6432 is
6433 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6434
6435 Desig_Type : Entity_Id;
6436 Discr : Entity_Id;
6437 Discr_Con_Elist : Elist_Id;
6438 Discr_Con_El : Elmt_Id;
6439 Subt : Entity_Id;
6440
6441 begin
6442 -- Set the designated type so it is available in case this is an access
6443 -- to a self-referential type, e.g. a standard list type with a next
6444 -- pointer. Will be reset after subtype is built.
6445
6446 Set_Directly_Designated_Type
6447 (Derived_Type, Designated_Type (Parent_Type));
6448
6449 Subt := Process_Subtype (S, N);
6450
6451 if Nkind (S) /= N_Subtype_Indication
6452 and then Subt /= Base_Type (Subt)
6453 then
6454 Set_Ekind (Derived_Type, E_Access_Subtype);
6455 end if;
6456
6457 if Ekind (Derived_Type) = E_Access_Subtype then
6458 declare
6459 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6460 Ibase : constant Entity_Id :=
6461 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6462 Svg_Chars : constant Name_Id := Chars (Ibase);
6463 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6464 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6465
6466 begin
6467 Copy_Node (Pbase, Ibase);
6468
6469 -- Restore Itype status after Copy_Node
6470
6471 Set_Is_Itype (Ibase);
6472 Set_Associated_Node_For_Itype (Ibase, N);
6473
6474 Set_Chars (Ibase, Svg_Chars);
6475 Set_Prev_Entity (Ibase, Svg_Prev_E);
6476 Set_Next_Entity (Ibase, Svg_Next_E);
6477 Set_Sloc (Ibase, Sloc (Derived_Type));
6478 Set_Scope (Ibase, Scope (Derived_Type));
6479 Set_Freeze_Node (Ibase, Empty);
6480 Set_Is_Frozen (Ibase, False);
6481 Set_Comes_From_Source (Ibase, False);
6482 Set_Is_First_Subtype (Ibase, False);
6483
6484 Set_Etype (Ibase, Pbase);
6485 Set_Etype (Derived_Type, Ibase);
6486 end;
6487 end if;
6488
6489 Set_Directly_Designated_Type
6490 (Derived_Type, Designated_Type (Subt));
6491
6492 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6493 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6494 Set_Size_Info (Derived_Type, Parent_Type);
6495 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6496 Set_Depends_On_Private (Derived_Type,
6497 Has_Private_Component (Derived_Type));
6498 Conditional_Delay (Derived_Type, Subt);
6499
6500 if Is_Access_Subprogram_Type (Derived_Type) then
6501 Set_Can_Use_Internal_Rep
6502 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6503 end if;
6504
6505 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6506 -- that it is not redundant.
6507
6508 if Null_Exclusion_Present (Type_Definition (N)) then
6509 Set_Can_Never_Be_Null (Derived_Type);
6510
6511 elsif Can_Never_Be_Null (Parent_Type) then
6512 Set_Can_Never_Be_Null (Derived_Type);
6513 end if;
6514
6515 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6516 -- the root type for this information.
6517
6518 -- Apply range checks to discriminants for derived record case
6519 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6520
6521 Desig_Type := Designated_Type (Derived_Type);
6522
6523 if Is_Composite_Type (Desig_Type)
6524 and then (not Is_Array_Type (Desig_Type))
6525 and then Has_Discriminants (Desig_Type)
6526 and then Base_Type (Desig_Type) /= Desig_Type
6527 then
6528 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6529 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6530
6531 Discr := First_Discriminant (Base_Type (Desig_Type));
6532 while Present (Discr_Con_El) loop
6533 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6534 Next_Elmt (Discr_Con_El);
6535 Next_Discriminant (Discr);
6536 end loop;
6537 end if;
6538 end Build_Derived_Access_Type;
6539
6540 ------------------------------
6541 -- Build_Derived_Array_Type --
6542 ------------------------------
6543
6544 procedure Build_Derived_Array_Type
6545 (N : Node_Id;
6546 Parent_Type : Entity_Id;
6547 Derived_Type : Entity_Id)
6548 is
6549 Loc : constant Source_Ptr := Sloc (N);
6550 Tdef : constant Node_Id := Type_Definition (N);
6551 Indic : constant Node_Id := Subtype_Indication (Tdef);
6552 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6553 Implicit_Base : Entity_Id := Empty;
6554 New_Indic : Node_Id;
6555
6556 procedure Make_Implicit_Base;
6557 -- If the parent subtype is constrained, the derived type is a subtype
6558 -- of an implicit base type derived from the parent base.
6559
6560 ------------------------
6561 -- Make_Implicit_Base --
6562 ------------------------
6563
6564 procedure Make_Implicit_Base is
6565 begin
6566 Implicit_Base :=
6567 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6568
6569 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6570 Set_Etype (Implicit_Base, Parent_Base);
6571
6572 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6573 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6574
6575 Set_Has_Delayed_Freeze (Implicit_Base, True);
6576 end Make_Implicit_Base;
6577
6578 -- Start of processing for Build_Derived_Array_Type
6579
6580 begin
6581 if not Is_Constrained (Parent_Type) then
6582 if Nkind (Indic) /= N_Subtype_Indication then
6583 Set_Ekind (Derived_Type, E_Array_Type);
6584
6585 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6586 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6587
6588 Set_Has_Delayed_Freeze (Derived_Type, True);
6589
6590 else
6591 Make_Implicit_Base;
6592 Set_Etype (Derived_Type, Implicit_Base);
6593
6594 New_Indic :=
6595 Make_Subtype_Declaration (Loc,
6596 Defining_Identifier => Derived_Type,
6597 Subtype_Indication =>
6598 Make_Subtype_Indication (Loc,
6599 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6600 Constraint => Constraint (Indic)));
6601
6602 Rewrite (N, New_Indic);
6603 Analyze (N);
6604 end if;
6605
6606 else
6607 if Nkind (Indic) /= N_Subtype_Indication then
6608 Make_Implicit_Base;
6609
6610 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6611 Set_Etype (Derived_Type, Implicit_Base);
6612 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6613
6614 else
6615 Error_Msg_N ("illegal constraint on constrained type", Indic);
6616 end if;
6617 end if;
6618
6619 -- If parent type is not a derived type itself, and is declared in
6620 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6621 -- the new type's concatenation operator since Derive_Subprograms
6622 -- will not inherit the parent's operator. If the parent type is
6623 -- unconstrained, the operator is of the unconstrained base type.
6624
6625 if Number_Dimensions (Parent_Type) = 1
6626 and then not Is_Limited_Type (Parent_Type)
6627 and then not Is_Derived_Type (Parent_Type)
6628 and then not Is_Package_Or_Generic_Package
6629 (Scope (Base_Type (Parent_Type)))
6630 then
6631 if not Is_Constrained (Parent_Type)
6632 and then Is_Constrained (Derived_Type)
6633 then
6634 New_Concatenation_Op (Implicit_Base);
6635 else
6636 New_Concatenation_Op (Derived_Type);
6637 end if;
6638 end if;
6639 end Build_Derived_Array_Type;
6640
6641 -----------------------------------
6642 -- Build_Derived_Concurrent_Type --
6643 -----------------------------------
6644
6645 procedure Build_Derived_Concurrent_Type
6646 (N : Node_Id;
6647 Parent_Type : Entity_Id;
6648 Derived_Type : Entity_Id)
6649 is
6650 Loc : constant Source_Ptr := Sloc (N);
6651 Def : constant Node_Id := Type_Definition (N);
6652 Indic : constant Node_Id := Subtype_Indication (Def);
6653
6654 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6655 Corr_Decl : Node_Id;
6656 Corr_Decl_Needed : Boolean;
6657 -- If the derived type has fewer discriminants than its parent, the
6658 -- corresponding record is also a derived type, in order to account for
6659 -- the bound discriminants. We create a full type declaration for it in
6660 -- this case.
6661
6662 Constraint_Present : constant Boolean :=
6663 Nkind (Indic) = N_Subtype_Indication;
6664
6665 D_Constraint : Node_Id;
6666 New_Constraint : Elist_Id := No_Elist;
6667 Old_Disc : Entity_Id;
6668 New_Disc : Entity_Id;
6669 New_N : Node_Id;
6670
6671 begin
6672 Set_Stored_Constraint (Derived_Type, No_Elist);
6673 Corr_Decl_Needed := False;
6674 Old_Disc := Empty;
6675
6676 if Present (Discriminant_Specifications (N))
6677 and then Constraint_Present
6678 then
6679 Old_Disc := First_Discriminant (Parent_Type);
6680 New_Disc := First (Discriminant_Specifications (N));
6681 while Present (New_Disc) and then Present (Old_Disc) loop
6682 Next_Discriminant (Old_Disc);
6683 Next (New_Disc);
6684 end loop;
6685 end if;
6686
6687 if Present (Old_Disc) and then Expander_Active then
6688
6689 -- The new type has fewer discriminants, so we need to create a new
6690 -- corresponding record, which is derived from the corresponding
6691 -- record of the parent, and has a stored constraint that captures
6692 -- the values of the discriminant constraints. The corresponding
6693 -- record is needed only if expander is active and code generation is
6694 -- enabled.
6695
6696 -- The type declaration for the derived corresponding record has the
6697 -- same discriminant part and constraints as the current declaration.
6698 -- Copy the unanalyzed tree to build declaration.
6699
6700 Corr_Decl_Needed := True;
6701 New_N := Copy_Separate_Tree (N);
6702
6703 Corr_Decl :=
6704 Make_Full_Type_Declaration (Loc,
6705 Defining_Identifier => Corr_Record,
6706 Discriminant_Specifications =>
6707 Discriminant_Specifications (New_N),
6708 Type_Definition =>
6709 Make_Derived_Type_Definition (Loc,
6710 Subtype_Indication =>
6711 Make_Subtype_Indication (Loc,
6712 Subtype_Mark =>
6713 New_Occurrence_Of
6714 (Corresponding_Record_Type (Parent_Type), Loc),
6715 Constraint =>
6716 Constraint
6717 (Subtype_Indication (Type_Definition (New_N))))));
6718 end if;
6719
6720 -- Copy Storage_Size and Relative_Deadline variables if task case
6721
6722 if Is_Task_Type (Parent_Type) then
6723 Set_Storage_Size_Variable (Derived_Type,
6724 Storage_Size_Variable (Parent_Type));
6725 Set_Relative_Deadline_Variable (Derived_Type,
6726 Relative_Deadline_Variable (Parent_Type));
6727 end if;
6728
6729 if Present (Discriminant_Specifications (N)) then
6730 Push_Scope (Derived_Type);
6731 Check_Or_Process_Discriminants (N, Derived_Type);
6732
6733 if Constraint_Present then
6734 New_Constraint :=
6735 Expand_To_Stored_Constraint
6736 (Parent_Type,
6737 Build_Discriminant_Constraints
6738 (Parent_Type, Indic, True));
6739 end if;
6740
6741 End_Scope;
6742
6743 elsif Constraint_Present then
6744
6745 -- Build an unconstrained derived type and rewrite the derived type
6746 -- as a subtype of this new base type.
6747
6748 declare
6749 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6750 New_Base : Entity_Id;
6751 New_Decl : Node_Id;
6752 New_Indic : Node_Id;
6753
6754 begin
6755 New_Base :=
6756 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6757
6758 New_Decl :=
6759 Make_Full_Type_Declaration (Loc,
6760 Defining_Identifier => New_Base,
6761 Type_Definition =>
6762 Make_Derived_Type_Definition (Loc,
6763 Abstract_Present => Abstract_Present (Def),
6764 Limited_Present => Limited_Present (Def),
6765 Subtype_Indication =>
6766 New_Occurrence_Of (Parent_Base, Loc)));
6767
6768 Mark_Rewrite_Insertion (New_Decl);
6769 Insert_Before (N, New_Decl);
6770 Analyze (New_Decl);
6771
6772 New_Indic :=
6773 Make_Subtype_Indication (Loc,
6774 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6775 Constraint => Relocate_Node (Constraint (Indic)));
6776
6777 Rewrite (N,
6778 Make_Subtype_Declaration (Loc,
6779 Defining_Identifier => Derived_Type,
6780 Subtype_Indication => New_Indic));
6781
6782 Analyze (N);
6783 return;
6784 end;
6785 end if;
6786
6787 -- By default, operations and private data are inherited from parent.
6788 -- However, in the presence of bound discriminants, a new corresponding
6789 -- record will be created, see below.
6790
6791 Set_Has_Discriminants
6792 (Derived_Type, Has_Discriminants (Parent_Type));
6793 Set_Corresponding_Record_Type
6794 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6795
6796 -- Is_Constrained is set according the parent subtype, but is set to
6797 -- False if the derived type is declared with new discriminants.
6798
6799 Set_Is_Constrained
6800 (Derived_Type,
6801 (Is_Constrained (Parent_Type) or else Constraint_Present)
6802 and then not Present (Discriminant_Specifications (N)));
6803
6804 if Constraint_Present then
6805 if not Has_Discriminants (Parent_Type) then
6806 Error_Msg_N ("untagged parent must have discriminants", N);
6807
6808 elsif Present (Discriminant_Specifications (N)) then
6809
6810 -- Verify that new discriminants are used to constrain old ones
6811
6812 D_Constraint := First (Constraints (Constraint (Indic)));
6813
6814 Old_Disc := First_Discriminant (Parent_Type);
6815
6816 while Present (D_Constraint) loop
6817 if Nkind (D_Constraint) /= N_Discriminant_Association then
6818
6819 -- Positional constraint. If it is a reference to a new
6820 -- discriminant, it constrains the corresponding old one.
6821
6822 if Nkind (D_Constraint) = N_Identifier then
6823 New_Disc := First_Discriminant (Derived_Type);
6824 while Present (New_Disc) loop
6825 exit when Chars (New_Disc) = Chars (D_Constraint);
6826 Next_Discriminant (New_Disc);
6827 end loop;
6828
6829 if Present (New_Disc) then
6830 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6831 end if;
6832 end if;
6833
6834 Next_Discriminant (Old_Disc);
6835
6836 -- if this is a named constraint, search by name for the old
6837 -- discriminants constrained by the new one.
6838
6839 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6840
6841 -- Find new discriminant with that name
6842
6843 New_Disc := First_Discriminant (Derived_Type);
6844 while Present (New_Disc) loop
6845 exit when
6846 Chars (New_Disc) = Chars (Expression (D_Constraint));
6847 Next_Discriminant (New_Disc);
6848 end loop;
6849
6850 if Present (New_Disc) then
6851
6852 -- Verify that new discriminant renames some discriminant
6853 -- of the parent type, and associate the new discriminant
6854 -- with one or more old ones that it renames.
6855
6856 declare
6857 Selector : Node_Id;
6858
6859 begin
6860 Selector := First (Selector_Names (D_Constraint));
6861 while Present (Selector) loop
6862 Old_Disc := First_Discriminant (Parent_Type);
6863 while Present (Old_Disc) loop
6864 exit when Chars (Old_Disc) = Chars (Selector);
6865 Next_Discriminant (Old_Disc);
6866 end loop;
6867
6868 if Present (Old_Disc) then
6869 Set_Corresponding_Discriminant
6870 (New_Disc, Old_Disc);
6871 end if;
6872
6873 Next (Selector);
6874 end loop;
6875 end;
6876 end if;
6877 end if;
6878
6879 Next (D_Constraint);
6880 end loop;
6881
6882 New_Disc := First_Discriminant (Derived_Type);
6883 while Present (New_Disc) loop
6884 if No (Corresponding_Discriminant (New_Disc)) then
6885 Error_Msg_NE
6886 ("new discriminant& must constrain old one", N, New_Disc);
6887
6888 elsif not
6889 Subtypes_Statically_Compatible
6890 (Etype (New_Disc),
6891 Etype (Corresponding_Discriminant (New_Disc)))
6892 then
6893 Error_Msg_NE
6894 ("& not statically compatible with parent discriminant",
6895 N, New_Disc);
6896 end if;
6897
6898 Next_Discriminant (New_Disc);
6899 end loop;
6900 end if;
6901
6902 elsif Present (Discriminant_Specifications (N)) then
6903 Error_Msg_N
6904 ("missing discriminant constraint in untagged derivation", N);
6905 end if;
6906
6907 -- The entity chain of the derived type includes the new discriminants
6908 -- but shares operations with the parent.
6909
6910 if Present (Discriminant_Specifications (N)) then
6911 Old_Disc := First_Discriminant (Parent_Type);
6912 while Present (Old_Disc) loop
6913 if No (Next_Entity (Old_Disc))
6914 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6915 then
6916 Link_Entities
6917 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6918 exit;
6919 end if;
6920
6921 Next_Discriminant (Old_Disc);
6922 end loop;
6923
6924 else
6925 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6926 if Has_Discriminants (Parent_Type) then
6927 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6928 Set_Discriminant_Constraint (
6929 Derived_Type, Discriminant_Constraint (Parent_Type));
6930 end if;
6931 end if;
6932
6933 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6934
6935 Set_Has_Completion (Derived_Type);
6936
6937 if Corr_Decl_Needed then
6938 Set_Stored_Constraint (Derived_Type, New_Constraint);
6939 Insert_After (N, Corr_Decl);
6940 Analyze (Corr_Decl);
6941 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6942 end if;
6943 end Build_Derived_Concurrent_Type;
6944
6945 ------------------------------------
6946 -- Build_Derived_Enumeration_Type --
6947 ------------------------------------
6948
6949 procedure Build_Derived_Enumeration_Type
6950 (N : Node_Id;
6951 Parent_Type : Entity_Id;
6952 Derived_Type : Entity_Id)
6953 is
6954 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
6955 -- When the type declaration includes a constraint, we generate
6956 -- a subtype declaration of an anonymous base type, with the constraint
6957 -- given in the original type declaration. Conceptually, the bounds
6958 -- are converted to the new base type, and this conversion freezes
6959 -- (prematurely) that base type, when the bounds are simply literals.
6960 -- As a result, a representation clause for the derived type is then
6961 -- rejected or ignored. This procedure recognizes the simple case of
6962 -- literal bounds, which allows us to indicate that the conversions
6963 -- are not freeze points, and the subsequent representation clause
6964 -- can be accepted.
6965 -- A similar approach might be used to resolve the long-standing
6966 -- problem of premature freezing of derived numeric types ???
6967
6968 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
6969 begin
6970 return Nkind (B) = N_Type_Conversion
6971 and then Is_Entity_Name (Expression (B))
6972 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
6973 end Bound_Belongs_To_Type;
6974
6975 Loc : constant Source_Ptr := Sloc (N);
6976 Def : constant Node_Id := Type_Definition (N);
6977 Indic : constant Node_Id := Subtype_Indication (Def);
6978 Implicit_Base : Entity_Id;
6979 Literal : Entity_Id;
6980 New_Lit : Entity_Id;
6981 Literals_List : List_Id;
6982 Type_Decl : Node_Id;
6983 Hi, Lo : Node_Id;
6984 Rang_Expr : Node_Id;
6985
6986 begin
6987 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6988 -- not have explicit literals lists we need to process types derived
6989 -- from them specially. This is handled by Derived_Standard_Character.
6990 -- If the parent type is a generic type, there are no literals either,
6991 -- and we construct the same skeletal representation as for the generic
6992 -- parent type.
6993
6994 if Is_Standard_Character_Type (Parent_Type) then
6995 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6996
6997 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6998 declare
6999 Lo : Node_Id;
7000 Hi : Node_Id;
7001
7002 begin
7003 if Nkind (Indic) /= N_Subtype_Indication then
7004 Lo :=
7005 Make_Attribute_Reference (Loc,
7006 Attribute_Name => Name_First,
7007 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7008 Set_Etype (Lo, Derived_Type);
7009
7010 Hi :=
7011 Make_Attribute_Reference (Loc,
7012 Attribute_Name => Name_Last,
7013 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7014 Set_Etype (Hi, Derived_Type);
7015
7016 Set_Scalar_Range (Derived_Type,
7017 Make_Range (Loc,
7018 Low_Bound => Lo,
7019 High_Bound => Hi));
7020 else
7021
7022 -- Analyze subtype indication and verify compatibility
7023 -- with parent type.
7024
7025 if Base_Type (Process_Subtype (Indic, N)) /=
7026 Base_Type (Parent_Type)
7027 then
7028 Error_Msg_N
7029 ("illegal constraint for formal discrete type", N);
7030 end if;
7031 end if;
7032 end;
7033
7034 else
7035 -- If a constraint is present, analyze the bounds to catch
7036 -- premature usage of the derived literals.
7037
7038 if Nkind (Indic) = N_Subtype_Indication
7039 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7040 then
7041 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7042 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7043 end if;
7044
7045 -- Introduce an implicit base type for the derived type even if there
7046 -- is no constraint attached to it, since this seems closer to the
7047 -- Ada semantics. Build a full type declaration tree for the derived
7048 -- type using the implicit base type as the defining identifier. The
7049 -- build a subtype declaration tree which applies the constraint (if
7050 -- any) have it replace the derived type declaration.
7051
7052 Literal := First_Literal (Parent_Type);
7053 Literals_List := New_List;
7054 while Present (Literal)
7055 and then Ekind (Literal) = E_Enumeration_Literal
7056 loop
7057 -- Literals of the derived type have the same representation as
7058 -- those of the parent type, but this representation can be
7059 -- overridden by an explicit representation clause. Indicate
7060 -- that there is no explicit representation given yet. These
7061 -- derived literals are implicit operations of the new type,
7062 -- and can be overridden by explicit ones.
7063
7064 if Nkind (Literal) = N_Defining_Character_Literal then
7065 New_Lit :=
7066 Make_Defining_Character_Literal (Loc, Chars (Literal));
7067 else
7068 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7069 end if;
7070
7071 Set_Ekind (New_Lit, E_Enumeration_Literal);
7072 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7073 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7074 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7075 Set_Alias (New_Lit, Literal);
7076 Set_Is_Known_Valid (New_Lit, True);
7077
7078 Append (New_Lit, Literals_List);
7079 Next_Literal (Literal);
7080 end loop;
7081
7082 Implicit_Base :=
7083 Make_Defining_Identifier (Sloc (Derived_Type),
7084 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7085
7086 -- Indicate the proper nature of the derived type. This must be done
7087 -- before analysis of the literals, to recognize cases when a literal
7088 -- may be hidden by a previous explicit function definition (cf.
7089 -- c83031a).
7090
7091 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7092 Set_Etype (Derived_Type, Implicit_Base);
7093
7094 Type_Decl :=
7095 Make_Full_Type_Declaration (Loc,
7096 Defining_Identifier => Implicit_Base,
7097 Discriminant_Specifications => No_List,
7098 Type_Definition =>
7099 Make_Enumeration_Type_Definition (Loc, Literals_List));
7100
7101 Mark_Rewrite_Insertion (Type_Decl);
7102 Insert_Before (N, Type_Decl);
7103 Analyze (Type_Decl);
7104
7105 -- The anonymous base now has a full declaration, but this base
7106 -- is not a first subtype.
7107
7108 Set_Is_First_Subtype (Implicit_Base, False);
7109
7110 -- After the implicit base is analyzed its Etype needs to be changed
7111 -- to reflect the fact that it is derived from the parent type which
7112 -- was ignored during analysis. We also set the size at this point.
7113
7114 Set_Etype (Implicit_Base, Parent_Type);
7115
7116 Set_Size_Info (Implicit_Base, Parent_Type);
7117 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7118 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7119
7120 -- Copy other flags from parent type
7121
7122 Set_Has_Non_Standard_Rep
7123 (Implicit_Base, Has_Non_Standard_Rep
7124 (Parent_Type));
7125 Set_Has_Pragma_Ordered
7126 (Implicit_Base, Has_Pragma_Ordered
7127 (Parent_Type));
7128 Set_Has_Delayed_Freeze (Implicit_Base);
7129
7130 -- Process the subtype indication including a validation check on the
7131 -- constraint, if any. If a constraint is given, its bounds must be
7132 -- implicitly converted to the new type.
7133
7134 if Nkind (Indic) = N_Subtype_Indication then
7135 declare
7136 R : constant Node_Id :=
7137 Range_Expression (Constraint (Indic));
7138
7139 begin
7140 if Nkind (R) = N_Range then
7141 Hi := Build_Scalar_Bound
7142 (High_Bound (R), Parent_Type, Implicit_Base);
7143 Lo := Build_Scalar_Bound
7144 (Low_Bound (R), Parent_Type, Implicit_Base);
7145
7146 else
7147 -- Constraint is a Range attribute. Replace with explicit
7148 -- mention of the bounds of the prefix, which must be a
7149 -- subtype.
7150
7151 Analyze (Prefix (R));
7152 Hi :=
7153 Convert_To (Implicit_Base,
7154 Make_Attribute_Reference (Loc,
7155 Attribute_Name => Name_Last,
7156 Prefix =>
7157 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7158
7159 Lo :=
7160 Convert_To (Implicit_Base,
7161 Make_Attribute_Reference (Loc,
7162 Attribute_Name => Name_First,
7163 Prefix =>
7164 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7165 end if;
7166 end;
7167
7168 else
7169 Hi :=
7170 Build_Scalar_Bound
7171 (Type_High_Bound (Parent_Type),
7172 Parent_Type, Implicit_Base);
7173 Lo :=
7174 Build_Scalar_Bound
7175 (Type_Low_Bound (Parent_Type),
7176 Parent_Type, Implicit_Base);
7177 end if;
7178
7179 Rang_Expr :=
7180 Make_Range (Loc,
7181 Low_Bound => Lo,
7182 High_Bound => Hi);
7183
7184 -- If we constructed a default range for the case where no range
7185 -- was given, then the expressions in the range must not freeze
7186 -- since they do not correspond to expressions in the source.
7187 -- However, if the type inherits predicates the expressions will
7188 -- be elaborated earlier and must freeze.
7189
7190 if (Nkind (Indic) /= N_Subtype_Indication
7191 or else
7192 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7193 and then not Has_Predicates (Derived_Type)
7194 then
7195 Set_Must_Not_Freeze (Lo);
7196 Set_Must_Not_Freeze (Hi);
7197 Set_Must_Not_Freeze (Rang_Expr);
7198 end if;
7199
7200 Rewrite (N,
7201 Make_Subtype_Declaration (Loc,
7202 Defining_Identifier => Derived_Type,
7203 Subtype_Indication =>
7204 Make_Subtype_Indication (Loc,
7205 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7206 Constraint =>
7207 Make_Range_Constraint (Loc,
7208 Range_Expression => Rang_Expr))));
7209
7210 Analyze (N);
7211
7212 -- Propagate the aspects from the original type declaration to the
7213 -- declaration of the implicit base.
7214
7215 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7216
7217 -- Apply a range check. Since this range expression doesn't have an
7218 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7219 -- this right???
7220
7221 if Nkind (Indic) = N_Subtype_Indication then
7222 Apply_Range_Check
7223 (Range_Expression (Constraint (Indic)), Parent_Type,
7224 Source_Typ => Entity (Subtype_Mark (Indic)));
7225 end if;
7226 end if;
7227 end Build_Derived_Enumeration_Type;
7228
7229 --------------------------------
7230 -- Build_Derived_Numeric_Type --
7231 --------------------------------
7232
7233 procedure Build_Derived_Numeric_Type
7234 (N : Node_Id;
7235 Parent_Type : Entity_Id;
7236 Derived_Type : Entity_Id)
7237 is
7238 Loc : constant Source_Ptr := Sloc (N);
7239 Tdef : constant Node_Id := Type_Definition (N);
7240 Indic : constant Node_Id := Subtype_Indication (Tdef);
7241 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7242 No_Constraint : constant Boolean := Nkind (Indic) /=
7243 N_Subtype_Indication;
7244 Implicit_Base : Entity_Id;
7245
7246 Lo : Node_Id;
7247 Hi : Node_Id;
7248
7249 begin
7250 -- Process the subtype indication including a validation check on
7251 -- the constraint if any.
7252
7253 Discard_Node (Process_Subtype (Indic, N));
7254
7255 -- Introduce an implicit base type for the derived type even if there
7256 -- is no constraint attached to it, since this seems closer to the Ada
7257 -- semantics.
7258
7259 Implicit_Base :=
7260 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7261
7262 Set_Etype (Implicit_Base, Parent_Base);
7263 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7264 Set_Size_Info (Implicit_Base, Parent_Base);
7265 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7266 Set_Parent (Implicit_Base, Parent (Derived_Type));
7267 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7268
7269 -- Set RM Size for discrete type or decimal fixed-point type
7270 -- Ordinary fixed-point is excluded, why???
7271
7272 if Is_Discrete_Type (Parent_Base)
7273 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7274 then
7275 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7276 end if;
7277
7278 Set_Has_Delayed_Freeze (Implicit_Base);
7279
7280 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7281 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7282
7283 Set_Scalar_Range (Implicit_Base,
7284 Make_Range (Loc,
7285 Low_Bound => Lo,
7286 High_Bound => Hi));
7287
7288 if Has_Infinities (Parent_Base) then
7289 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7290 end if;
7291
7292 -- The Derived_Type, which is the entity of the declaration, is a
7293 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7294 -- absence of an explicit constraint.
7295
7296 Set_Etype (Derived_Type, Implicit_Base);
7297
7298 -- If we did not have a constraint, then the Ekind is set from the
7299 -- parent type (otherwise Process_Subtype has set the bounds)
7300
7301 if No_Constraint then
7302 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7303 end if;
7304
7305 -- If we did not have a range constraint, then set the range from the
7306 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7307
7308 if No_Constraint or else not Has_Range_Constraint (Indic) then
7309 Set_Scalar_Range (Derived_Type,
7310 Make_Range (Loc,
7311 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7312 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7313 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7314
7315 if Has_Infinities (Parent_Type) then
7316 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7317 end if;
7318
7319 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7320 end if;
7321
7322 Set_Is_Descendant_Of_Address (Derived_Type,
7323 Is_Descendant_Of_Address (Parent_Type));
7324 Set_Is_Descendant_Of_Address (Implicit_Base,
7325 Is_Descendant_Of_Address (Parent_Type));
7326
7327 -- Set remaining type-specific fields, depending on numeric type
7328
7329 if Is_Modular_Integer_Type (Parent_Type) then
7330 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7331
7332 Set_Non_Binary_Modulus
7333 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7334
7335 Set_Is_Known_Valid
7336 (Implicit_Base, Is_Known_Valid (Parent_Base));
7337
7338 elsif Is_Floating_Point_Type (Parent_Type) then
7339
7340 -- Digits of base type is always copied from the digits value of
7341 -- the parent base type, but the digits of the derived type will
7342 -- already have been set if there was a constraint present.
7343
7344 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7345 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7346
7347 if No_Constraint then
7348 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7349 end if;
7350
7351 elsif Is_Fixed_Point_Type (Parent_Type) then
7352
7353 -- Small of base type and derived type are always copied from the
7354 -- parent base type, since smalls never change. The delta of the
7355 -- base type is also copied from the parent base type. However the
7356 -- delta of the derived type will have been set already if a
7357 -- constraint was present.
7358
7359 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7360 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7361 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7362
7363 if No_Constraint then
7364 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7365 end if;
7366
7367 -- The scale and machine radix in the decimal case are always
7368 -- copied from the parent base type.
7369
7370 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7371 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7372 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7373
7374 Set_Machine_Radix_10
7375 (Derived_Type, Machine_Radix_10 (Parent_Base));
7376 Set_Machine_Radix_10
7377 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7378
7379 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7380
7381 if No_Constraint then
7382 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7383
7384 else
7385 -- the analysis of the subtype_indication sets the
7386 -- digits value of the derived type.
7387
7388 null;
7389 end if;
7390 end if;
7391 end if;
7392
7393 if Is_Integer_Type (Parent_Type) then
7394 Set_Has_Shift_Operator
7395 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7396 end if;
7397
7398 -- The type of the bounds is that of the parent type, and they
7399 -- must be converted to the derived type.
7400
7401 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7402
7403 -- The implicit_base should be frozen when the derived type is frozen,
7404 -- but note that it is used in the conversions of the bounds. For fixed
7405 -- types we delay the determination of the bounds until the proper
7406 -- freezing point. For other numeric types this is rejected by GCC, for
7407 -- reasons that are currently unclear (???), so we choose to freeze the
7408 -- implicit base now. In the case of integers and floating point types
7409 -- this is harmless because subsequent representation clauses cannot
7410 -- affect anything, but it is still baffling that we cannot use the
7411 -- same mechanism for all derived numeric types.
7412
7413 -- There is a further complication: actually some representation
7414 -- clauses can affect the implicit base type. For example, attribute
7415 -- definition clauses for stream-oriented attributes need to set the
7416 -- corresponding TSS entries on the base type, and this normally
7417 -- cannot be done after the base type is frozen, so the circuitry in
7418 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7419 -- and not use Set_TSS in this case.
7420
7421 -- There are also consequences for the case of delayed representation
7422 -- aspects for some cases. For example, a Size aspect is delayed and
7423 -- should not be evaluated to the freeze point. This early freezing
7424 -- means that the size attribute evaluation happens too early???
7425
7426 if Is_Fixed_Point_Type (Parent_Type) then
7427 Conditional_Delay (Implicit_Base, Parent_Type);
7428 else
7429 Freeze_Before (N, Implicit_Base);
7430 end if;
7431 end Build_Derived_Numeric_Type;
7432
7433 --------------------------------
7434 -- Build_Derived_Private_Type --
7435 --------------------------------
7436
7437 procedure Build_Derived_Private_Type
7438 (N : Node_Id;
7439 Parent_Type : Entity_Id;
7440 Derived_Type : Entity_Id;
7441 Is_Completion : Boolean;
7442 Derive_Subps : Boolean := True)
7443 is
7444 Loc : constant Source_Ptr := Sloc (N);
7445 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7446 Par_Scope : constant Entity_Id := Scope (Par_Base);
7447 Full_N : constant Node_Id := New_Copy_Tree (N);
7448 Full_Der : Entity_Id := New_Copy (Derived_Type);
7449 Full_P : Entity_Id;
7450
7451 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7452 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7453 -- present (they cannot be both present for the same type), or Empty.
7454
7455 procedure Build_Full_Derivation;
7456 -- Build full derivation, i.e. derive from the full view
7457
7458 procedure Copy_And_Build;
7459 -- Copy derived type declaration, replace parent with its full view,
7460 -- and build derivation
7461
7462 -------------------------
7463 -- Available_Full_View --
7464 -------------------------
7465
7466 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7467 begin
7468 if Present (Full_View (Typ)) then
7469 return Full_View (Typ);
7470
7471 elsif Present (Underlying_Full_View (Typ)) then
7472
7473 -- We should be called on a type with an underlying full view
7474 -- only by means of the recursive call made in Copy_And_Build
7475 -- through the first call to Build_Derived_Type, or else if
7476 -- the parent scope is being analyzed because we are deriving
7477 -- a completion.
7478
7479 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7480
7481 return Underlying_Full_View (Typ);
7482
7483 else
7484 return Empty;
7485 end if;
7486 end Available_Full_View;
7487
7488 ---------------------------
7489 -- Build_Full_Derivation --
7490 ---------------------------
7491
7492 procedure Build_Full_Derivation is
7493 begin
7494 -- If parent scope is not open, install the declarations
7495
7496 if not In_Open_Scopes (Par_Scope) then
7497 Install_Private_Declarations (Par_Scope);
7498 Install_Visible_Declarations (Par_Scope);
7499 Copy_And_Build;
7500 Uninstall_Declarations (Par_Scope);
7501
7502 -- If parent scope is open and in another unit, and parent has a
7503 -- completion, then the derivation is taking place in the visible
7504 -- part of a child unit. In that case retrieve the full view of
7505 -- the parent momentarily.
7506
7507 elsif not In_Same_Source_Unit (N, Parent_Type)
7508 and then Present (Full_View (Parent_Type))
7509 then
7510 Full_P := Full_View (Parent_Type);
7511 Exchange_Declarations (Parent_Type);
7512 Copy_And_Build;
7513 Exchange_Declarations (Full_P);
7514
7515 -- Otherwise it is a local derivation
7516
7517 else
7518 Copy_And_Build;
7519 end if;
7520 end Build_Full_Derivation;
7521
7522 --------------------
7523 -- Copy_And_Build --
7524 --------------------
7525
7526 procedure Copy_And_Build is
7527 Full_Parent : Entity_Id := Parent_Type;
7528
7529 begin
7530 -- If the parent is itself derived from another private type,
7531 -- installing the private declarations has not affected its
7532 -- privacy status, so use its own full view explicitly.
7533
7534 if Is_Private_Type (Full_Parent)
7535 and then Present (Full_View (Full_Parent))
7536 then
7537 Full_Parent := Full_View (Full_Parent);
7538 end if;
7539
7540 -- If the full view is itself derived from another private type
7541 -- and has got an underlying full view, and this is done for a
7542 -- completion, i.e. to build the underlying full view of the type,
7543 -- then use this underlying full view. We cannot do that if this
7544 -- is not a completion, i.e. to build the full view of the type,
7545 -- because this would break the privacy of the parent type, except
7546 -- if the parent scope is being analyzed because we are deriving a
7547 -- completion.
7548
7549 if Is_Private_Type (Full_Parent)
7550 and then Present (Underlying_Full_View (Full_Parent))
7551 and then (Is_Completion or else In_Private_Part (Par_Scope))
7552 then
7553 Full_Parent := Underlying_Full_View (Full_Parent);
7554 end if;
7555
7556 -- For private, record, concurrent, access and almost all enumeration
7557 -- types, the derivation from the full view requires a fully-fledged
7558 -- declaration. In the other cases, just use an itype.
7559
7560 if Is_Private_Type (Full_Parent)
7561 or else Is_Record_Type (Full_Parent)
7562 or else Is_Concurrent_Type (Full_Parent)
7563 or else Is_Access_Type (Full_Parent)
7564 or else
7565 (Is_Enumeration_Type (Full_Parent)
7566 and then not Is_Standard_Character_Type (Full_Parent)
7567 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7568 then
7569 -- Copy and adjust declaration to provide a completion for what
7570 -- is originally a private declaration. Indicate that full view
7571 -- is internally generated.
7572
7573 Set_Comes_From_Source (Full_N, False);
7574 Set_Comes_From_Source (Full_Der, False);
7575 Set_Parent (Full_Der, Full_N);
7576 Set_Defining_Identifier (Full_N, Full_Der);
7577
7578 -- If there are no constraints, adjust the subtype mark
7579
7580 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7581 N_Subtype_Indication
7582 then
7583 Set_Subtype_Indication
7584 (Type_Definition (Full_N),
7585 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7586 end if;
7587
7588 Insert_After (N, Full_N);
7589
7590 -- Build full view of derived type from full view of parent which
7591 -- is now installed. Subprograms have been derived on the partial
7592 -- view, the completion does not derive them anew.
7593
7594 if Is_Record_Type (Full_Parent) then
7595
7596 -- If parent type is tagged, the completion inherits the proper
7597 -- primitive operations.
7598
7599 if Is_Tagged_Type (Parent_Type) then
7600 Build_Derived_Record_Type
7601 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7602 else
7603 Build_Derived_Record_Type
7604 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7605 end if;
7606
7607 else
7608 -- If the parent type is private, this is not a completion and
7609 -- we build the full derivation recursively as a completion.
7610
7611 Build_Derived_Type
7612 (Full_N, Full_Parent, Full_Der,
7613 Is_Completion => Is_Private_Type (Full_Parent),
7614 Derive_Subps => False);
7615 end if;
7616
7617 -- The full declaration has been introduced into the tree and
7618 -- processed in the step above. It should not be analyzed again
7619 -- (when encountered later in the current list of declarations)
7620 -- to prevent spurious name conflicts. The full entity remains
7621 -- invisible.
7622
7623 Set_Analyzed (Full_N);
7624
7625 else
7626 Full_Der :=
7627 Make_Defining_Identifier (Sloc (Derived_Type),
7628 Chars => Chars (Derived_Type));
7629 Set_Is_Itype (Full_Der);
7630 Set_Associated_Node_For_Itype (Full_Der, N);
7631 Set_Parent (Full_Der, N);
7632 Build_Derived_Type
7633 (N, Full_Parent, Full_Der,
7634 Is_Completion => False, Derive_Subps => False);
7635 end if;
7636
7637 Set_Has_Private_Declaration (Full_Der);
7638 Set_Has_Private_Declaration (Derived_Type);
7639
7640 Set_Scope (Full_Der, Scope (Derived_Type));
7641 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7642 Set_Has_Size_Clause (Full_Der, False);
7643 Set_Has_Alignment_Clause (Full_Der, False);
7644 Set_Has_Delayed_Freeze (Full_Der);
7645 Set_Is_Frozen (Full_Der, False);
7646 Set_Freeze_Node (Full_Der, Empty);
7647 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7648 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7649
7650 -- The convention on the base type may be set in the private part
7651 -- and not propagated to the subtype until later, so we obtain the
7652 -- convention from the base type of the parent.
7653
7654 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7655 end Copy_And_Build;
7656
7657 -- Start of processing for Build_Derived_Private_Type
7658
7659 begin
7660 if Is_Tagged_Type (Parent_Type) then
7661 Full_P := Full_View (Parent_Type);
7662
7663 -- A type extension of a type with unknown discriminants is an
7664 -- indefinite type that the back-end cannot handle directly.
7665 -- We treat it as a private type, and build a completion that is
7666 -- derived from the full view of the parent, and hopefully has
7667 -- known discriminants.
7668
7669 -- If the full view of the parent type has an underlying record view,
7670 -- use it to generate the underlying record view of this derived type
7671 -- (required for chains of derivations with unknown discriminants).
7672
7673 -- Minor optimization: we avoid the generation of useless underlying
7674 -- record view entities if the private type declaration has unknown
7675 -- discriminants but its corresponding full view has no
7676 -- discriminants.
7677
7678 if Has_Unknown_Discriminants (Parent_Type)
7679 and then Present (Full_P)
7680 and then (Has_Discriminants (Full_P)
7681 or else Present (Underlying_Record_View (Full_P)))
7682 and then not In_Open_Scopes (Par_Scope)
7683 and then Expander_Active
7684 then
7685 declare
7686 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7687 New_Ext : constant Node_Id :=
7688 Copy_Separate_Tree
7689 (Record_Extension_Part (Type_Definition (N)));
7690 Decl : Node_Id;
7691
7692 begin
7693 Build_Derived_Record_Type
7694 (N, Parent_Type, Derived_Type, Derive_Subps);
7695
7696 -- Build anonymous completion, as a derivation from the full
7697 -- view of the parent. This is not a completion in the usual
7698 -- sense, because the current type is not private.
7699
7700 Decl :=
7701 Make_Full_Type_Declaration (Loc,
7702 Defining_Identifier => Full_Der,
7703 Type_Definition =>
7704 Make_Derived_Type_Definition (Loc,
7705 Subtype_Indication =>
7706 New_Copy_Tree
7707 (Subtype_Indication (Type_Definition (N))),
7708 Record_Extension_Part => New_Ext));
7709
7710 -- If the parent type has an underlying record view, use it
7711 -- here to build the new underlying record view.
7712
7713 if Present (Underlying_Record_View (Full_P)) then
7714 pragma Assert
7715 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7716 = N_Identifier);
7717 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7718 Underlying_Record_View (Full_P));
7719 end if;
7720
7721 Install_Private_Declarations (Par_Scope);
7722 Install_Visible_Declarations (Par_Scope);
7723 Insert_Before (N, Decl);
7724
7725 -- Mark entity as an underlying record view before analysis,
7726 -- to avoid generating the list of its primitive operations
7727 -- (which is not really required for this entity) and thus
7728 -- prevent spurious errors associated with missing overriding
7729 -- of abstract primitives (overridden only for Derived_Type).
7730
7731 Set_Ekind (Full_Der, E_Record_Type);
7732 Set_Is_Underlying_Record_View (Full_Der);
7733 Set_Default_SSO (Full_Der);
7734 Set_No_Reordering (Full_Der, No_Component_Reordering);
7735
7736 Analyze (Decl);
7737
7738 pragma Assert (Has_Discriminants (Full_Der)
7739 and then not Has_Unknown_Discriminants (Full_Der));
7740
7741 Uninstall_Declarations (Par_Scope);
7742
7743 -- Freeze the underlying record view, to prevent generation of
7744 -- useless dispatching information, which is simply shared with
7745 -- the real derived type.
7746
7747 Set_Is_Frozen (Full_Der);
7748
7749 -- If the derived type has access discriminants, create
7750 -- references to their anonymous types now, to prevent
7751 -- back-end problems when their first use is in generated
7752 -- bodies of primitives.
7753
7754 declare
7755 E : Entity_Id;
7756
7757 begin
7758 E := First_Entity (Full_Der);
7759
7760 while Present (E) loop
7761 if Ekind (E) = E_Discriminant
7762 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7763 then
7764 Build_Itype_Reference (Etype (E), Decl);
7765 end if;
7766
7767 Next_Entity (E);
7768 end loop;
7769 end;
7770
7771 -- Set up links between real entity and underlying record view
7772
7773 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7774 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7775 end;
7776
7777 -- If discriminants are known, build derived record
7778
7779 else
7780 Build_Derived_Record_Type
7781 (N, Parent_Type, Derived_Type, Derive_Subps);
7782 end if;
7783
7784 return;
7785
7786 elsif Has_Discriminants (Parent_Type) then
7787
7788 -- Build partial view of derived type from partial view of parent.
7789 -- This must be done before building the full derivation because the
7790 -- second derivation will modify the discriminants of the first and
7791 -- the discriminants are chained with the rest of the components in
7792 -- the full derivation.
7793
7794 Build_Derived_Record_Type
7795 (N, Parent_Type, Derived_Type, Derive_Subps);
7796
7797 -- Build the full derivation if this is not the anonymous derived
7798 -- base type created by Build_Derived_Record_Type in the constrained
7799 -- case (see point 5. of its head comment) since we build it for the
7800 -- derived subtype.
7801
7802 if Present (Available_Full_View (Parent_Type))
7803 and then not Is_Itype (Derived_Type)
7804 then
7805 declare
7806 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7807 Discr : Entity_Id;
7808 Last_Discr : Entity_Id;
7809
7810 begin
7811 -- If this is not a completion, construct the implicit full
7812 -- view by deriving from the full view of the parent type.
7813 -- But if this is a completion, the derived private type
7814 -- being built is a full view and the full derivation can
7815 -- only be its underlying full view.
7816
7817 Build_Full_Derivation;
7818
7819 if not Is_Completion then
7820 Set_Full_View (Derived_Type, Full_Der);
7821 else
7822 Set_Underlying_Full_View (Derived_Type, Full_Der);
7823 Set_Is_Underlying_Full_View (Full_Der);
7824 end if;
7825
7826 if not Is_Base_Type (Derived_Type) then
7827 Set_Full_View (Der_Base, Base_Type (Full_Der));
7828 end if;
7829
7830 -- Copy the discriminant list from full view to the partial
7831 -- view (base type and its subtype). Gigi requires that the
7832 -- partial and full views have the same discriminants.
7833
7834 -- Note that since the partial view points to discriminants
7835 -- in the full view, their scope will be that of the full
7836 -- view. This might cause some front end problems and need
7837 -- adjustment???
7838
7839 Discr := First_Discriminant (Base_Type (Full_Der));
7840 Set_First_Entity (Der_Base, Discr);
7841
7842 loop
7843 Last_Discr := Discr;
7844 Next_Discriminant (Discr);
7845 exit when No (Discr);
7846 end loop;
7847
7848 Set_Last_Entity (Der_Base, Last_Discr);
7849 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7850 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7851 end;
7852 end if;
7853
7854 elsif Present (Available_Full_View (Parent_Type))
7855 and then Has_Discriminants (Available_Full_View (Parent_Type))
7856 then
7857 if Has_Unknown_Discriminants (Parent_Type)
7858 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7859 N_Subtype_Indication
7860 then
7861 Error_Msg_N
7862 ("cannot constrain type with unknown discriminants",
7863 Subtype_Indication (Type_Definition (N)));
7864 return;
7865 end if;
7866
7867 -- If this is not a completion, construct the implicit full view by
7868 -- deriving from the full view of the parent type. But if this is a
7869 -- completion, the derived private type being built is a full view
7870 -- and the full derivation can only be its underlying full view.
7871
7872 Build_Full_Derivation;
7873
7874 if not Is_Completion then
7875 Set_Full_View (Derived_Type, Full_Der);
7876 else
7877 Set_Underlying_Full_View (Derived_Type, Full_Der);
7878 Set_Is_Underlying_Full_View (Full_Der);
7879 end if;
7880
7881 -- In any case, the primitive operations are inherited from the
7882 -- parent type, not from the internal full view.
7883
7884 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7885
7886 if Derive_Subps then
7887 Derive_Subprograms (Parent_Type, Derived_Type);
7888 end if;
7889
7890 Set_Stored_Constraint (Derived_Type, No_Elist);
7891 Set_Is_Constrained
7892 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
7893
7894 else
7895 -- Untagged type, No discriminants on either view
7896
7897 if Nkind (Subtype_Indication (Type_Definition (N))) =
7898 N_Subtype_Indication
7899 then
7900 Error_Msg_N
7901 ("illegal constraint on type without discriminants", N);
7902 end if;
7903
7904 if Present (Discriminant_Specifications (N))
7905 and then Present (Available_Full_View (Parent_Type))
7906 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
7907 then
7908 Error_Msg_N ("cannot add discriminants to untagged type", N);
7909 end if;
7910
7911 Set_Stored_Constraint (Derived_Type, No_Elist);
7912 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7913
7914 Set_Is_Controlled_Active
7915 (Derived_Type, Is_Controlled_Active (Parent_Type));
7916
7917 Set_Disable_Controlled
7918 (Derived_Type, Disable_Controlled (Parent_Type));
7919
7920 Set_Has_Controlled_Component
7921 (Derived_Type, Has_Controlled_Component (Parent_Type));
7922
7923 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7924
7925 if not Is_Controlled (Parent_Type) then
7926 Set_Finalize_Storage_Only
7927 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7928 end if;
7929
7930 -- If this is not a completion, construct the implicit full view by
7931 -- deriving from the full view of the parent type. But if this is a
7932 -- completion, the derived private type being built is a full view
7933 -- and the full derivation can only be its underlying full view.
7934
7935 -- ??? If the parent type is untagged private and its completion is
7936 -- tagged, this mechanism will not work because we cannot derive from
7937 -- the tagged full view unless we have an extension.
7938
7939 if Present (Available_Full_View (Parent_Type))
7940 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
7941 and then not Error_Posted (N)
7942 then
7943 Build_Full_Derivation;
7944
7945 if not Is_Completion then
7946 Set_Full_View (Derived_Type, Full_Der);
7947 else
7948 Set_Underlying_Full_View (Derived_Type, Full_Der);
7949 Set_Is_Underlying_Full_View (Full_Der);
7950 end if;
7951 end if;
7952 end if;
7953
7954 Set_Has_Unknown_Discriminants (Derived_Type,
7955 Has_Unknown_Discriminants (Parent_Type));
7956
7957 if Is_Private_Type (Derived_Type) then
7958 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7959 end if;
7960
7961 -- If the parent base type is in scope, add the derived type to its
7962 -- list of private dependents, because its full view may become
7963 -- visible subsequently (in a nested private part, a body, or in a
7964 -- further child unit).
7965
7966 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7967 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7968
7969 -- Check for unusual case where a type completed by a private
7970 -- derivation occurs within a package nested in a child unit, and
7971 -- the parent is declared in an ancestor.
7972
7973 if Is_Child_Unit (Scope (Current_Scope))
7974 and then Is_Completion
7975 and then In_Private_Part (Current_Scope)
7976 and then Scope (Parent_Type) /= Current_Scope
7977
7978 -- Note that if the parent has a completion in the private part,
7979 -- (which is itself a derivation from some other private type)
7980 -- it is that completion that is visible, there is no full view
7981 -- available, and no special processing is needed.
7982
7983 and then Present (Full_View (Parent_Type))
7984 then
7985 -- In this case, the full view of the parent type will become
7986 -- visible in the body of the enclosing child, and only then will
7987 -- the current type be possibly non-private. Build an underlying
7988 -- full view that will be installed when the enclosing child body
7989 -- is compiled.
7990
7991 if Present (Underlying_Full_View (Derived_Type)) then
7992 Full_Der := Underlying_Full_View (Derived_Type);
7993 else
7994 Build_Full_Derivation;
7995 Set_Underlying_Full_View (Derived_Type, Full_Der);
7996 Set_Is_Underlying_Full_View (Full_Der);
7997 end if;
7998
7999 -- The full view will be used to swap entities on entry/exit to
8000 -- the body, and must appear in the entity list for the package.
8001
8002 Append_Entity (Full_Der, Scope (Derived_Type));
8003 end if;
8004 end if;
8005 end Build_Derived_Private_Type;
8006
8007 -------------------------------
8008 -- Build_Derived_Record_Type --
8009 -------------------------------
8010
8011 -- 1. INTRODUCTION
8012
8013 -- Ideally we would like to use the same model of type derivation for
8014 -- tagged and untagged record types. Unfortunately this is not quite
8015 -- possible because the semantics of representation clauses is different
8016 -- for tagged and untagged records under inheritance. Consider the
8017 -- following:
8018
8019 -- type R (...) is [tagged] record ... end record;
8020 -- type T (...) is new R (...) [with ...];
8021
8022 -- The representation clauses for T can specify a completely different
8023 -- record layout from R's. Hence the same component can be placed in two
8024 -- very different positions in objects of type T and R. If R and T are
8025 -- tagged types, representation clauses for T can only specify the layout
8026 -- of non inherited components, thus components that are common in R and T
8027 -- have the same position in objects of type R and T.
8028
8029 -- This has two implications. The first is that the entire tree for R's
8030 -- declaration needs to be copied for T in the untagged case, so that T
8031 -- can be viewed as a record type of its own with its own representation
8032 -- clauses. The second implication is the way we handle discriminants.
8033 -- Specifically, in the untagged case we need a way to communicate to Gigi
8034 -- what are the real discriminants in the record, while for the semantics
8035 -- we need to consider those introduced by the user to rename the
8036 -- discriminants in the parent type. This is handled by introducing the
8037 -- notion of stored discriminants. See below for more.
8038
8039 -- Fortunately the way regular components are inherited can be handled in
8040 -- the same way in tagged and untagged types.
8041
8042 -- To complicate things a bit more the private view of a private extension
8043 -- cannot be handled in the same way as the full view (for one thing the
8044 -- semantic rules are somewhat different). We will explain what differs
8045 -- below.
8046
8047 -- 2. DISCRIMINANTS UNDER INHERITANCE
8048
8049 -- The semantic rules governing the discriminants of derived types are
8050 -- quite subtle.
8051
8052 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8053 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8054
8055 -- If parent type has discriminants, then the discriminants that are
8056 -- declared in the derived type are [3.4 (11)]:
8057
8058 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8059 -- there is one;
8060
8061 -- o Otherwise, each discriminant of the parent type (implicitly declared
8062 -- in the same order with the same specifications). In this case, the
8063 -- discriminants are said to be "inherited", or if unknown in the parent
8064 -- are also unknown in the derived type.
8065
8066 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8067
8068 -- o The parent subtype must be constrained;
8069
8070 -- o If the parent type is not a tagged type, then each discriminant of
8071 -- the derived type must be used in the constraint defining a parent
8072 -- subtype. [Implementation note: This ensures that the new discriminant
8073 -- can share storage with an existing discriminant.]
8074
8075 -- For the derived type each discriminant of the parent type is either
8076 -- inherited, constrained to equal some new discriminant of the derived
8077 -- type, or constrained to the value of an expression.
8078
8079 -- When inherited or constrained to equal some new discriminant, the
8080 -- parent discriminant and the discriminant of the derived type are said
8081 -- to "correspond".
8082
8083 -- If a discriminant of the parent type is constrained to a specific value
8084 -- in the derived type definition, then the discriminant is said to be
8085 -- "specified" by that derived type definition.
8086
8087 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8088
8089 -- We have spoken about stored discriminants in point 1 (introduction)
8090 -- above. There are two sorts of stored discriminants: implicit and
8091 -- explicit. As long as the derived type inherits the same discriminants as
8092 -- the root record type, stored discriminants are the same as regular
8093 -- discriminants, and are said to be implicit. However, if any discriminant
8094 -- in the root type was renamed in the derived type, then the derived
8095 -- type will contain explicit stored discriminants. Explicit stored
8096 -- discriminants are discriminants in addition to the semantically visible
8097 -- discriminants defined for the derived type. Stored discriminants are
8098 -- used by Gigi to figure out what are the physical discriminants in
8099 -- objects of the derived type (see precise definition in einfo.ads).
8100 -- As an example, consider the following:
8101
8102 -- type R (D1, D2, D3 : Int) is record ... end record;
8103 -- type T1 is new R;
8104 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8105 -- type T3 is new T2;
8106 -- type T4 (Y : Int) is new T3 (Y, 99);
8107
8108 -- The following table summarizes the discriminants and stored
8109 -- discriminants in R and T1 through T4:
8110
8111 -- Type Discrim Stored Discrim Comment
8112 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8113 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8114 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8115 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8116 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8117
8118 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8119 -- find the corresponding discriminant in the parent type, while
8120 -- Original_Record_Component (abbreviated ORC below) the actual physical
8121 -- component that is renamed. Finally the field Is_Completely_Hidden
8122 -- (abbreviated ICH below) is set for all explicit stored discriminants
8123 -- (see einfo.ads for more info). For the above example this gives:
8124
8125 -- Discrim CD ORC ICH
8126 -- ^^^^^^^ ^^ ^^^ ^^^
8127 -- D1 in R empty itself no
8128 -- D2 in R empty itself no
8129 -- D3 in R empty itself no
8130
8131 -- D1 in T1 D1 in R itself no
8132 -- D2 in T1 D2 in R itself no
8133 -- D3 in T1 D3 in R itself no
8134
8135 -- X1 in T2 D3 in T1 D3 in T2 no
8136 -- X2 in T2 D1 in T1 D1 in T2 no
8137 -- D1 in T2 empty itself yes
8138 -- D2 in T2 empty itself yes
8139 -- D3 in T2 empty itself yes
8140
8141 -- X1 in T3 X1 in T2 D3 in T3 no
8142 -- X2 in T3 X2 in T2 D1 in T3 no
8143 -- D1 in T3 empty itself yes
8144 -- D2 in T3 empty itself yes
8145 -- D3 in T3 empty itself yes
8146
8147 -- Y in T4 X1 in T3 D3 in T4 no
8148 -- D1 in T4 empty itself yes
8149 -- D2 in T4 empty itself yes
8150 -- D3 in T4 empty itself yes
8151
8152 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8153
8154 -- Type derivation for tagged types is fairly straightforward. If no
8155 -- discriminants are specified by the derived type, these are inherited
8156 -- from the parent. No explicit stored discriminants are ever necessary.
8157 -- The only manipulation that is done to the tree is that of adding a
8158 -- _parent field with parent type and constrained to the same constraint
8159 -- specified for the parent in the derived type definition. For instance:
8160
8161 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8162 -- type T1 is new R with null record;
8163 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8164
8165 -- are changed into:
8166
8167 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8168 -- _parent : R (D1, D2, D3);
8169 -- end record;
8170
8171 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8172 -- _parent : T1 (X2, 88, X1);
8173 -- end record;
8174
8175 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8176 -- ORC and ICH fields are:
8177
8178 -- Discrim CD ORC ICH
8179 -- ^^^^^^^ ^^ ^^^ ^^^
8180 -- D1 in R empty itself no
8181 -- D2 in R empty itself no
8182 -- D3 in R empty itself no
8183
8184 -- D1 in T1 D1 in R D1 in R no
8185 -- D2 in T1 D2 in R D2 in R no
8186 -- D3 in T1 D3 in R D3 in R no
8187
8188 -- X1 in T2 D3 in T1 D3 in R no
8189 -- X2 in T2 D1 in T1 D1 in R no
8190
8191 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8192 --
8193 -- Regardless of whether we dealing with a tagged or untagged type
8194 -- we will transform all derived type declarations of the form
8195 --
8196 -- type T is new R (...) [with ...];
8197 -- or
8198 -- subtype S is R (...);
8199 -- type T is new S [with ...];
8200 -- into
8201 -- type BT is new R [with ...];
8202 -- subtype T is BT (...);
8203 --
8204 -- That is, the base derived type is constrained only if it has no
8205 -- discriminants. The reason for doing this is that GNAT's semantic model
8206 -- assumes that a base type with discriminants is unconstrained.
8207 --
8208 -- Note that, strictly speaking, the above transformation is not always
8209 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8210 --
8211 -- procedure B34011A is
8212 -- type REC (D : integer := 0) is record
8213 -- I : Integer;
8214 -- end record;
8215
8216 -- package P is
8217 -- type T6 is new Rec;
8218 -- function F return T6;
8219 -- end P;
8220
8221 -- use P;
8222 -- package Q6 is
8223 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8224 -- end Q6;
8225 --
8226 -- The definition of Q6.U is illegal. However transforming Q6.U into
8227
8228 -- type BaseU is new T6;
8229 -- subtype U is BaseU (Q6.F.I)
8230
8231 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8232 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8233 -- the transformation described above.
8234
8235 -- There is another instance where the above transformation is incorrect.
8236 -- Consider:
8237
8238 -- package Pack is
8239 -- type Base (D : Integer) is tagged null record;
8240 -- procedure P (X : Base);
8241
8242 -- type Der is new Base (2) with null record;
8243 -- procedure P (X : Der);
8244 -- end Pack;
8245
8246 -- Then the above transformation turns this into
8247
8248 -- type Der_Base is new Base with null record;
8249 -- -- procedure P (X : Base) is implicitly inherited here
8250 -- -- as procedure P (X : Der_Base).
8251
8252 -- subtype Der is Der_Base (2);
8253 -- procedure P (X : Der);
8254 -- -- The overriding of P (X : Der_Base) is illegal since we
8255 -- -- have a parameter conformance problem.
8256
8257 -- To get around this problem, after having semantically processed Der_Base
8258 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8259 -- Discriminant_Constraint from Der so that when parameter conformance is
8260 -- checked when P is overridden, no semantic errors are flagged.
8261
8262 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8263
8264 -- Regardless of whether we are dealing with a tagged or untagged type
8265 -- we will transform all derived type declarations of the form
8266
8267 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8268 -- type T is new R [with ...];
8269 -- into
8270 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8271
8272 -- The reason for such transformation is that it allows us to implement a
8273 -- very clean form of component inheritance as explained below.
8274
8275 -- Note that this transformation is not achieved by direct tree rewriting
8276 -- and manipulation, but rather by redoing the semantic actions that the
8277 -- above transformation will entail. This is done directly in routine
8278 -- Inherit_Components.
8279
8280 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8281
8282 -- In both tagged and untagged derived types, regular non discriminant
8283 -- components are inherited in the derived type from the parent type. In
8284 -- the absence of discriminants component, inheritance is straightforward
8285 -- as components can simply be copied from the parent.
8286
8287 -- If the parent has discriminants, inheriting components constrained with
8288 -- these discriminants requires caution. Consider the following example:
8289
8290 -- type R (D1, D2 : Positive) is [tagged] record
8291 -- S : String (D1 .. D2);
8292 -- end record;
8293
8294 -- type T1 is new R [with null record];
8295 -- type T2 (X : positive) is new R (1, X) [with null record];
8296
8297 -- As explained in 6. above, T1 is rewritten as
8298 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8299 -- which makes the treatment for T1 and T2 identical.
8300
8301 -- What we want when inheriting S, is that references to D1 and D2 in R are
8302 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8303 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8304 -- with either discriminant references in the derived type or expressions.
8305 -- This replacement is achieved as follows: before inheriting R's
8306 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8307 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8308 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8309 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8310 -- by String (1 .. X).
8311
8312 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8313
8314 -- We explain here the rules governing private type extensions relevant to
8315 -- type derivation. These rules are explained on the following example:
8316
8317 -- type D [(...)] is new A [(...)] with private; <-- partial view
8318 -- type D [(...)] is new P [(...)] with null record; <-- full view
8319
8320 -- Type A is called the ancestor subtype of the private extension.
8321 -- Type P is the parent type of the full view of the private extension. It
8322 -- must be A or a type derived from A.
8323
8324 -- The rules concerning the discriminants of private type extensions are
8325 -- [7.3(10-13)]:
8326
8327 -- o If a private extension inherits known discriminants from the ancestor
8328 -- subtype, then the full view must also inherit its discriminants from
8329 -- the ancestor subtype and the parent subtype of the full view must be
8330 -- constrained if and only if the ancestor subtype is constrained.
8331
8332 -- o If a partial view has unknown discriminants, then the full view may
8333 -- define a definite or an indefinite subtype, with or without
8334 -- discriminants.
8335
8336 -- o If a partial view has neither known nor unknown discriminants, then
8337 -- the full view must define a definite subtype.
8338
8339 -- o If the ancestor subtype of a private extension has constrained
8340 -- discriminants, then the parent subtype of the full view must impose a
8341 -- statically matching constraint on those discriminants.
8342
8343 -- This means that only the following forms of private extensions are
8344 -- allowed:
8345
8346 -- type D is new A with private; <-- partial view
8347 -- type D is new P with null record; <-- full view
8348
8349 -- If A has no discriminants than P has no discriminants, otherwise P must
8350 -- inherit A's discriminants.
8351
8352 -- type D is new A (...) with private; <-- partial view
8353 -- type D is new P (:::) with null record; <-- full view
8354
8355 -- P must inherit A's discriminants and (...) and (:::) must statically
8356 -- match.
8357
8358 -- subtype A is R (...);
8359 -- type D is new A with private; <-- partial view
8360 -- type D is new P with null record; <-- full view
8361
8362 -- P must have inherited R's discriminants and must be derived from A or
8363 -- any of its subtypes.
8364
8365 -- type D (..) is new A with private; <-- partial view
8366 -- type D (..) is new P [(:::)] with null record; <-- full view
8367
8368 -- No specific constraints on P's discriminants or constraint (:::).
8369 -- Note that A can be unconstrained, but the parent subtype P must either
8370 -- be constrained or (:::) must be present.
8371
8372 -- type D (..) is new A [(...)] with private; <-- partial view
8373 -- type D (..) is new P [(:::)] with null record; <-- full view
8374
8375 -- P's constraints on A's discriminants must statically match those
8376 -- imposed by (...).
8377
8378 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8379
8380 -- The full view of a private extension is handled exactly as described
8381 -- above. The model chose for the private view of a private extension is
8382 -- the same for what concerns discriminants (i.e. they receive the same
8383 -- treatment as in the tagged case). However, the private view of the
8384 -- private extension always inherits the components of the parent base,
8385 -- without replacing any discriminant reference. Strictly speaking this is
8386 -- incorrect. However, Gigi never uses this view to generate code so this
8387 -- is a purely semantic issue. In theory, a set of transformations similar
8388 -- to those given in 5. and 6. above could be applied to private views of
8389 -- private extensions to have the same model of component inheritance as
8390 -- for non private extensions. However, this is not done because it would
8391 -- further complicate private type processing. Semantically speaking, this
8392 -- leaves us in an uncomfortable situation. As an example consider:
8393
8394 -- package Pack is
8395 -- type R (D : integer) is tagged record
8396 -- S : String (1 .. D);
8397 -- end record;
8398 -- procedure P (X : R);
8399 -- type T is new R (1) with private;
8400 -- private
8401 -- type T is new R (1) with null record;
8402 -- end;
8403
8404 -- This is transformed into:
8405
8406 -- package Pack is
8407 -- type R (D : integer) is tagged record
8408 -- S : String (1 .. D);
8409 -- end record;
8410 -- procedure P (X : R);
8411 -- type T is new R (1) with private;
8412 -- private
8413 -- type BaseT is new R with null record;
8414 -- subtype T is BaseT (1);
8415 -- end;
8416
8417 -- (strictly speaking the above is incorrect Ada)
8418
8419 -- From the semantic standpoint the private view of private extension T
8420 -- should be flagged as constrained since one can clearly have
8421 --
8422 -- Obj : T;
8423 --
8424 -- in a unit withing Pack. However, when deriving subprograms for the
8425 -- private view of private extension T, T must be seen as unconstrained
8426 -- since T has discriminants (this is a constraint of the current
8427 -- subprogram derivation model). Thus, when processing the private view of
8428 -- a private extension such as T, we first mark T as unconstrained, we
8429 -- process it, we perform program derivation and just before returning from
8430 -- Build_Derived_Record_Type we mark T as constrained.
8431
8432 -- ??? Are there are other uncomfortable cases that we will have to
8433 -- deal with.
8434
8435 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8436
8437 -- Types that are derived from a visible record type and have a private
8438 -- extension present other peculiarities. They behave mostly like private
8439 -- types, but if they have primitive operations defined, these will not
8440 -- have the proper signatures for further inheritance, because other
8441 -- primitive operations will use the implicit base that we define for
8442 -- private derivations below. This affect subprogram inheritance (see
8443 -- Derive_Subprograms for details). We also derive the implicit base from
8444 -- the base type of the full view, so that the implicit base is a record
8445 -- type and not another private type, This avoids infinite loops.
8446
8447 procedure Build_Derived_Record_Type
8448 (N : Node_Id;
8449 Parent_Type : Entity_Id;
8450 Derived_Type : Entity_Id;
8451 Derive_Subps : Boolean := True)
8452 is
8453 Discriminant_Specs : constant Boolean :=
8454 Present (Discriminant_Specifications (N));
8455 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8456 Loc : constant Source_Ptr := Sloc (N);
8457 Private_Extension : constant Boolean :=
8458 Nkind (N) = N_Private_Extension_Declaration;
8459 Assoc_List : Elist_Id;
8460 Constraint_Present : Boolean;
8461 Constrs : Elist_Id;
8462 Discrim : Entity_Id;
8463 Indic : Node_Id;
8464 Inherit_Discrims : Boolean := False;
8465 Last_Discrim : Entity_Id;
8466 New_Base : Entity_Id;
8467 New_Decl : Node_Id;
8468 New_Discrs : Elist_Id;
8469 New_Indic : Node_Id;
8470 Parent_Base : Entity_Id;
8471 Save_Etype : Entity_Id;
8472 Save_Discr_Constr : Elist_Id;
8473 Save_Next_Entity : Entity_Id;
8474 Type_Def : Node_Id;
8475
8476 Discs : Elist_Id := New_Elmt_List;
8477 -- An empty Discs list means that there were no constraints in the
8478 -- subtype indication or that there was an error processing it.
8479
8480 procedure Check_Generic_Ancestors;
8481 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8482 -- cannot be declared at a deeper level than its parent type is
8483 -- removed. The check on derivation within a generic body is also
8484 -- relaxed, but there's a restriction that a derived tagged type
8485 -- cannot be declared in a generic body if it's derived directly
8486 -- or indirectly from a formal type of that generic. This applies
8487 -- to progenitors as well.
8488
8489 -----------------------------
8490 -- Check_Generic_Ancestors --
8491 -----------------------------
8492
8493 procedure Check_Generic_Ancestors is
8494 Ancestor_Type : Entity_Id;
8495 Intf_List : List_Id;
8496 Intf_Name : Node_Id;
8497
8498 procedure Check_Ancestor;
8499 -- For parent and progenitors.
8500
8501 --------------------
8502 -- Check_Ancestor --
8503 --------------------
8504
8505 procedure Check_Ancestor is
8506 begin
8507 -- If the derived type does have a formal type as an ancestor
8508 -- then it's an error if the derived type is declared within
8509 -- the body of the generic unit that declares the formal type
8510 -- in its generic formal part. It's sufficient to check whether
8511 -- the ancestor type is declared inside the same generic body
8512 -- as the derived type (such as within a nested generic spec),
8513 -- in which case the derivation is legal. If the formal type is
8514 -- declared outside of that generic body, then it's certain
8515 -- that the derived type is declared within the generic body
8516 -- of the generic unit declaring the formal type.
8517
8518 if Is_Generic_Type (Ancestor_Type)
8519 and then Enclosing_Generic_Body (Ancestor_Type) /=
8520 Enclosing_Generic_Body (Derived_Type)
8521 then
8522 Error_Msg_NE
8523 ("ancestor type& is formal type of enclosing"
8524 & " generic unit (RM 3.9.1 (4/2))",
8525 Indic, Ancestor_Type);
8526 end if;
8527 end Check_Ancestor;
8528
8529 begin
8530 if Nkind (N) = N_Private_Extension_Declaration then
8531 Intf_List := Interface_List (N);
8532 else
8533 Intf_List := Interface_List (Type_Definition (N));
8534 end if;
8535
8536 if Present (Enclosing_Generic_Body (Derived_Type)) then
8537 Ancestor_Type := Parent_Type;
8538
8539 while not Is_Generic_Type (Ancestor_Type)
8540 and then Etype (Ancestor_Type) /= Ancestor_Type
8541 loop
8542 Ancestor_Type := Etype (Ancestor_Type);
8543 end loop;
8544
8545 Check_Ancestor;
8546
8547 if Present (Intf_List) then
8548 Intf_Name := First (Intf_List);
8549 while Present (Intf_Name) loop
8550 Ancestor_Type := Entity (Intf_Name);
8551 Check_Ancestor;
8552 Next (Intf_Name);
8553 end loop;
8554 end if;
8555 end if;
8556 end Check_Generic_Ancestors;
8557
8558 -- Start of processing for Build_Derived_Record_Type
8559
8560 begin
8561 if Ekind (Parent_Type) = E_Record_Type_With_Private
8562 and then Present (Full_View (Parent_Type))
8563 and then Has_Discriminants (Parent_Type)
8564 then
8565 Parent_Base := Base_Type (Full_View (Parent_Type));
8566 else
8567 Parent_Base := Base_Type (Parent_Type);
8568 end if;
8569
8570 -- If the parent type is declared as a subtype of another private
8571 -- type with inherited discriminants, its generated base type is
8572 -- itself a record subtype. To further inherit the constraint we
8573 -- need to use its own base to have an unconstrained type on which
8574 -- to apply the inherited constraint.
8575
8576 if Ekind (Parent_Base) = E_Record_Subtype then
8577 Parent_Base := Base_Type (Parent_Base);
8578 end if;
8579
8580 -- AI05-0115: if this is a derivation from a private type in some
8581 -- other scope that may lead to invisible components for the derived
8582 -- type, mark it accordingly.
8583
8584 if Is_Private_Type (Parent_Type) then
8585 if Scope (Parent_Base) = Scope (Derived_Type) then
8586 null;
8587
8588 elsif In_Open_Scopes (Scope (Parent_Base))
8589 and then In_Private_Part (Scope (Parent_Base))
8590 then
8591 null;
8592
8593 else
8594 Set_Has_Private_Ancestor (Derived_Type);
8595 end if;
8596
8597 else
8598 Set_Has_Private_Ancestor
8599 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8600 end if;
8601
8602 -- Before we start the previously documented transformations, here is
8603 -- little fix for size and alignment of tagged types. Normally when we
8604 -- derive type D from type P, we copy the size and alignment of P as the
8605 -- default for D, and in the absence of explicit representation clauses
8606 -- for D, the size and alignment are indeed the same as the parent.
8607
8608 -- But this is wrong for tagged types, since fields may be added, and
8609 -- the default size may need to be larger, and the default alignment may
8610 -- need to be larger.
8611
8612 -- We therefore reset the size and alignment fields in the tagged case.
8613 -- Note that the size and alignment will in any case be at least as
8614 -- large as the parent type (since the derived type has a copy of the
8615 -- parent type in the _parent field)
8616
8617 -- The type is also marked as being tagged here, which is needed when
8618 -- processing components with a self-referential anonymous access type
8619 -- in the call to Check_Anonymous_Access_Components below. Note that
8620 -- this flag is also set later on for completeness.
8621
8622 if Is_Tagged then
8623 Set_Is_Tagged_Type (Derived_Type);
8624 Init_Size_Align (Derived_Type);
8625 end if;
8626
8627 -- STEP 0a: figure out what kind of derived type declaration we have
8628
8629 if Private_Extension then
8630 Type_Def := N;
8631 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8632 Set_Default_SSO (Derived_Type);
8633 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8634
8635 else
8636 Type_Def := Type_Definition (N);
8637
8638 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8639 -- Parent_Base can be a private type or private extension. However,
8640 -- for tagged types with an extension the newly added fields are
8641 -- visible and hence the Derived_Type is always an E_Record_Type.
8642 -- (except that the parent may have its own private fields).
8643 -- For untagged types we preserve the Ekind of the Parent_Base.
8644
8645 if Present (Record_Extension_Part (Type_Def)) then
8646 Set_Ekind (Derived_Type, E_Record_Type);
8647 Set_Default_SSO (Derived_Type);
8648 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8649
8650 -- Create internal access types for components with anonymous
8651 -- access types.
8652
8653 if Ada_Version >= Ada_2005 then
8654 Check_Anonymous_Access_Components
8655 (N, Derived_Type, Derived_Type,
8656 Component_List (Record_Extension_Part (Type_Def)));
8657 end if;
8658
8659 else
8660 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8661 end if;
8662 end if;
8663
8664 -- Indic can either be an N_Identifier if the subtype indication
8665 -- contains no constraint or an N_Subtype_Indication if the subtype
8666 -- indication has a constraint. In either case it can include an
8667 -- interface list.
8668
8669 Indic := Subtype_Indication (Type_Def);
8670 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8671
8672 -- Check that the type has visible discriminants. The type may be
8673 -- a private type with unknown discriminants whose full view has
8674 -- discriminants which are invisible.
8675
8676 if Constraint_Present then
8677 if not Has_Discriminants (Parent_Base)
8678 or else
8679 (Has_Unknown_Discriminants (Parent_Base)
8680 and then Is_Private_Type (Parent_Base))
8681 then
8682 Error_Msg_N
8683 ("invalid constraint: type has no discriminant",
8684 Constraint (Indic));
8685
8686 Constraint_Present := False;
8687 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8688
8689 elsif Is_Constrained (Parent_Type) then
8690 Error_Msg_N
8691 ("invalid constraint: parent type is already constrained",
8692 Constraint (Indic));
8693
8694 Constraint_Present := False;
8695 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8696 end if;
8697 end if;
8698
8699 -- STEP 0b: If needed, apply transformation given in point 5. above
8700
8701 if not Private_Extension
8702 and then Has_Discriminants (Parent_Type)
8703 and then not Discriminant_Specs
8704 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8705 then
8706 -- First, we must analyze the constraint (see comment in point 5.)
8707 -- The constraint may come from the subtype indication of the full
8708 -- declaration.
8709
8710 if Constraint_Present then
8711 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8712
8713 -- If there is no explicit constraint, there might be one that is
8714 -- inherited from a constrained parent type. In that case verify that
8715 -- it conforms to the constraint in the partial view. In perverse
8716 -- cases the parent subtypes of the partial and full view can have
8717 -- different constraints.
8718
8719 elsif Present (Stored_Constraint (Parent_Type)) then
8720 New_Discrs := Stored_Constraint (Parent_Type);
8721
8722 else
8723 New_Discrs := No_Elist;
8724 end if;
8725
8726 if Has_Discriminants (Derived_Type)
8727 and then Has_Private_Declaration (Derived_Type)
8728 and then Present (Discriminant_Constraint (Derived_Type))
8729 and then Present (New_Discrs)
8730 then
8731 -- Verify that constraints of the full view statically match
8732 -- those given in the partial view.
8733
8734 declare
8735 C1, C2 : Elmt_Id;
8736
8737 begin
8738 C1 := First_Elmt (New_Discrs);
8739 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8740 while Present (C1) and then Present (C2) loop
8741 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8742 or else
8743 (Is_OK_Static_Expression (Node (C1))
8744 and then Is_OK_Static_Expression (Node (C2))
8745 and then
8746 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8747 then
8748 null;
8749
8750 else
8751 if Constraint_Present then
8752 Error_Msg_N
8753 ("constraint not conformant to previous declaration",
8754 Node (C1));
8755 else
8756 Error_Msg_N
8757 ("constraint of full view is incompatible "
8758 & "with partial view", N);
8759 end if;
8760 end if;
8761
8762 Next_Elmt (C1);
8763 Next_Elmt (C2);
8764 end loop;
8765 end;
8766 end if;
8767
8768 -- Insert and analyze the declaration for the unconstrained base type
8769
8770 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8771
8772 New_Decl :=
8773 Make_Full_Type_Declaration (Loc,
8774 Defining_Identifier => New_Base,
8775 Type_Definition =>
8776 Make_Derived_Type_Definition (Loc,
8777 Abstract_Present => Abstract_Present (Type_Def),
8778 Limited_Present => Limited_Present (Type_Def),
8779 Subtype_Indication =>
8780 New_Occurrence_Of (Parent_Base, Loc),
8781 Record_Extension_Part =>
8782 Relocate_Node (Record_Extension_Part (Type_Def)),
8783 Interface_List => Interface_List (Type_Def)));
8784
8785 Set_Parent (New_Decl, Parent (N));
8786 Mark_Rewrite_Insertion (New_Decl);
8787 Insert_Before (N, New_Decl);
8788
8789 -- In the extension case, make sure ancestor is frozen appropriately
8790 -- (see also non-discriminated case below).
8791
8792 if Present (Record_Extension_Part (Type_Def))
8793 or else Is_Interface (Parent_Base)
8794 then
8795 Freeze_Before (New_Decl, Parent_Type);
8796 end if;
8797
8798 -- Note that this call passes False for the Derive_Subps parameter
8799 -- because subprogram derivation is deferred until after creating
8800 -- the subtype (see below).
8801
8802 Build_Derived_Type
8803 (New_Decl, Parent_Base, New_Base,
8804 Is_Completion => False, Derive_Subps => False);
8805
8806 -- ??? This needs re-examination to determine whether the
8807 -- above call can simply be replaced by a call to Analyze.
8808
8809 Set_Analyzed (New_Decl);
8810
8811 -- Insert and analyze the declaration for the constrained subtype
8812
8813 if Constraint_Present then
8814 New_Indic :=
8815 Make_Subtype_Indication (Loc,
8816 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8817 Constraint => Relocate_Node (Constraint (Indic)));
8818
8819 else
8820 declare
8821 Constr_List : constant List_Id := New_List;
8822 C : Elmt_Id;
8823 Expr : Node_Id;
8824
8825 begin
8826 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8827 while Present (C) loop
8828 Expr := Node (C);
8829
8830 -- It is safe here to call New_Copy_Tree since we called
8831 -- Force_Evaluation on each constraint previously
8832 -- in Build_Discriminant_Constraints.
8833
8834 Append (New_Copy_Tree (Expr), To => Constr_List);
8835
8836 Next_Elmt (C);
8837 end loop;
8838
8839 New_Indic :=
8840 Make_Subtype_Indication (Loc,
8841 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8842 Constraint =>
8843 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8844 end;
8845 end if;
8846
8847 Rewrite (N,
8848 Make_Subtype_Declaration (Loc,
8849 Defining_Identifier => Derived_Type,
8850 Subtype_Indication => New_Indic));
8851
8852 Analyze (N);
8853
8854 -- Derivation of subprograms must be delayed until the full subtype
8855 -- has been established, to ensure proper overriding of subprograms
8856 -- inherited by full types. If the derivations occurred as part of
8857 -- the call to Build_Derived_Type above, then the check for type
8858 -- conformance would fail because earlier primitive subprograms
8859 -- could still refer to the full type prior the change to the new
8860 -- subtype and hence would not match the new base type created here.
8861 -- Subprograms are not derived, however, when Derive_Subps is False
8862 -- (since otherwise there could be redundant derivations).
8863
8864 if Derive_Subps then
8865 Derive_Subprograms (Parent_Type, Derived_Type);
8866 end if;
8867
8868 -- For tagged types the Discriminant_Constraint of the new base itype
8869 -- is inherited from the first subtype so that no subtype conformance
8870 -- problem arise when the first subtype overrides primitive
8871 -- operations inherited by the implicit base type.
8872
8873 if Is_Tagged then
8874 Set_Discriminant_Constraint
8875 (New_Base, Discriminant_Constraint (Derived_Type));
8876 end if;
8877
8878 return;
8879 end if;
8880
8881 -- If we get here Derived_Type will have no discriminants or it will be
8882 -- a discriminated unconstrained base type.
8883
8884 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8885
8886 if Is_Tagged then
8887
8888 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8889 -- The declaration of a specific descendant of an interface type
8890 -- freezes the interface type (RM 13.14).
8891
8892 if not Private_Extension or else Is_Interface (Parent_Base) then
8893 Freeze_Before (N, Parent_Type);
8894 end if;
8895
8896 if Ada_Version >= Ada_2005 then
8897 Check_Generic_Ancestors;
8898
8899 elsif Type_Access_Level (Derived_Type) /=
8900 Type_Access_Level (Parent_Type)
8901 and then not Is_Generic_Type (Derived_Type)
8902 then
8903 if Is_Controlled (Parent_Type) then
8904 Error_Msg_N
8905 ("controlled type must be declared at the library level",
8906 Indic);
8907 else
8908 Error_Msg_N
8909 ("type extension at deeper accessibility level than parent",
8910 Indic);
8911 end if;
8912
8913 else
8914 declare
8915 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8916 begin
8917 if Present (GB)
8918 and then GB /= Enclosing_Generic_Body (Parent_Base)
8919 then
8920 Error_Msg_NE
8921 ("parent type of& must not be outside generic body"
8922 & " (RM 3.9.1(4))",
8923 Indic, Derived_Type);
8924 end if;
8925 end;
8926 end if;
8927 end if;
8928
8929 -- Ada 2005 (AI-251)
8930
8931 if Ada_Version >= Ada_2005 and then Is_Tagged then
8932
8933 -- "The declaration of a specific descendant of an interface type
8934 -- freezes the interface type" (RM 13.14).
8935
8936 declare
8937 Iface : Node_Id;
8938 begin
8939 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8940 Iface := First (Interface_List (Type_Def));
8941 while Present (Iface) loop
8942 Freeze_Before (N, Etype (Iface));
8943 Next (Iface);
8944 end loop;
8945 end if;
8946 end;
8947 end if;
8948
8949 -- STEP 1b : preliminary cleanup of the full view of private types
8950
8951 -- If the type is already marked as having discriminants, then it's the
8952 -- completion of a private type or private extension and we need to
8953 -- retain the discriminants from the partial view if the current
8954 -- declaration has Discriminant_Specifications so that we can verify
8955 -- conformance. However, we must remove any existing components that
8956 -- were inherited from the parent (and attached in Copy_And_Swap)
8957 -- because the full type inherits all appropriate components anyway, and
8958 -- we do not want the partial view's components interfering.
8959
8960 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8961 Discrim := First_Discriminant (Derived_Type);
8962 loop
8963 Last_Discrim := Discrim;
8964 Next_Discriminant (Discrim);
8965 exit when No (Discrim);
8966 end loop;
8967
8968 Set_Last_Entity (Derived_Type, Last_Discrim);
8969
8970 -- In all other cases wipe out the list of inherited components (even
8971 -- inherited discriminants), it will be properly rebuilt here.
8972
8973 else
8974 Set_First_Entity (Derived_Type, Empty);
8975 Set_Last_Entity (Derived_Type, Empty);
8976 end if;
8977
8978 -- STEP 1c: Initialize some flags for the Derived_Type
8979
8980 -- The following flags must be initialized here so that
8981 -- Process_Discriminants can check that discriminants of tagged types do
8982 -- not have a default initial value and that access discriminants are
8983 -- only specified for limited records. For completeness, these flags are
8984 -- also initialized along with all the other flags below.
8985
8986 -- AI-419: Limitedness is not inherited from an interface parent, so to
8987 -- be limited in that case the type must be explicitly declared as
8988 -- limited. However, task and protected interfaces are always limited.
8989
8990 if Limited_Present (Type_Def) then
8991 Set_Is_Limited_Record (Derived_Type);
8992
8993 elsif Is_Limited_Record (Parent_Type)
8994 or else (Present (Full_View (Parent_Type))
8995 and then Is_Limited_Record (Full_View (Parent_Type)))
8996 then
8997 if not Is_Interface (Parent_Type)
8998 or else Is_Synchronized_Interface (Parent_Type)
8999 or else Is_Protected_Interface (Parent_Type)
9000 or else Is_Task_Interface (Parent_Type)
9001 then
9002 Set_Is_Limited_Record (Derived_Type);
9003 end if;
9004 end if;
9005
9006 -- STEP 2a: process discriminants of derived type if any
9007
9008 Push_Scope (Derived_Type);
9009
9010 if Discriminant_Specs then
9011 Set_Has_Unknown_Discriminants (Derived_Type, False);
9012
9013 -- The following call initializes fields Has_Discriminants and
9014 -- Discriminant_Constraint, unless we are processing the completion
9015 -- of a private type declaration.
9016
9017 Check_Or_Process_Discriminants (N, Derived_Type);
9018
9019 -- For untagged types, the constraint on the Parent_Type must be
9020 -- present and is used to rename the discriminants.
9021
9022 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9023 Error_Msg_N ("untagged parent must have discriminants", Indic);
9024
9025 elsif not Is_Tagged and then not Constraint_Present then
9026 Error_Msg_N
9027 ("discriminant constraint needed for derived untagged records",
9028 Indic);
9029
9030 -- Otherwise the parent subtype must be constrained unless we have a
9031 -- private extension.
9032
9033 elsif not Constraint_Present
9034 and then not Private_Extension
9035 and then not Is_Constrained (Parent_Type)
9036 then
9037 Error_Msg_N
9038 ("unconstrained type not allowed in this context", Indic);
9039
9040 elsif Constraint_Present then
9041 -- The following call sets the field Corresponding_Discriminant
9042 -- for the discriminants in the Derived_Type.
9043
9044 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9045
9046 -- For untagged types all new discriminants must rename
9047 -- discriminants in the parent. For private extensions new
9048 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9049
9050 Discrim := First_Discriminant (Derived_Type);
9051 while Present (Discrim) loop
9052 if not Is_Tagged
9053 and then No (Corresponding_Discriminant (Discrim))
9054 then
9055 Error_Msg_N
9056 ("new discriminants must constrain old ones", Discrim);
9057
9058 elsif Private_Extension
9059 and then Present (Corresponding_Discriminant (Discrim))
9060 then
9061 Error_Msg_N
9062 ("only static constraints allowed for parent"
9063 & " discriminants in the partial view", Indic);
9064 exit;
9065 end if;
9066
9067 -- If a new discriminant is used in the constraint, then its
9068 -- subtype must be statically compatible with the parent
9069 -- discriminant's subtype (3.7(15)).
9070
9071 -- However, if the record contains an array constrained by
9072 -- the discriminant but with some different bound, the compiler
9073 -- tries to create a smaller range for the discriminant type.
9074 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9075 -- the discriminant type is a scalar type, the check must use
9076 -- the original discriminant type in the parent declaration.
9077
9078 declare
9079 Corr_Disc : constant Entity_Id :=
9080 Corresponding_Discriminant (Discrim);
9081 Disc_Type : constant Entity_Id := Etype (Discrim);
9082 Corr_Type : Entity_Id;
9083
9084 begin
9085 if Present (Corr_Disc) then
9086 if Is_Scalar_Type (Disc_Type) then
9087 Corr_Type :=
9088 Entity (Discriminant_Type (Parent (Corr_Disc)));
9089 else
9090 Corr_Type := Etype (Corr_Disc);
9091 end if;
9092
9093 if not
9094 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9095 then
9096 Error_Msg_N
9097 ("subtype must be compatible "
9098 & "with parent discriminant",
9099 Discrim);
9100 end if;
9101 end if;
9102 end;
9103
9104 Next_Discriminant (Discrim);
9105 end loop;
9106
9107 -- Check whether the constraints of the full view statically
9108 -- match those imposed by the parent subtype [7.3(13)].
9109
9110 if Present (Stored_Constraint (Derived_Type)) then
9111 declare
9112 C1, C2 : Elmt_Id;
9113
9114 begin
9115 C1 := First_Elmt (Discs);
9116 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9117 while Present (C1) and then Present (C2) loop
9118 if not
9119 Fully_Conformant_Expressions (Node (C1), Node (C2))
9120 then
9121 Error_Msg_N
9122 ("not conformant with previous declaration",
9123 Node (C1));
9124 end if;
9125
9126 Next_Elmt (C1);
9127 Next_Elmt (C2);
9128 end loop;
9129 end;
9130 end if;
9131 end if;
9132
9133 -- STEP 2b: No new discriminants, inherit discriminants if any
9134
9135 else
9136 if Private_Extension then
9137 Set_Has_Unknown_Discriminants
9138 (Derived_Type,
9139 Has_Unknown_Discriminants (Parent_Type)
9140 or else Unknown_Discriminants_Present (N));
9141
9142 -- The partial view of the parent may have unknown discriminants,
9143 -- but if the full view has discriminants and the parent type is
9144 -- in scope they must be inherited.
9145
9146 elsif Has_Unknown_Discriminants (Parent_Type)
9147 and then
9148 (not Has_Discriminants (Parent_Type)
9149 or else not In_Open_Scopes (Scope (Parent_Base)))
9150 then
9151 Set_Has_Unknown_Discriminants (Derived_Type);
9152 end if;
9153
9154 if not Has_Unknown_Discriminants (Derived_Type)
9155 and then not Has_Unknown_Discriminants (Parent_Base)
9156 and then Has_Discriminants (Parent_Type)
9157 then
9158 Inherit_Discrims := True;
9159 Set_Has_Discriminants
9160 (Derived_Type, True);
9161 Set_Discriminant_Constraint
9162 (Derived_Type, Discriminant_Constraint (Parent_Base));
9163 end if;
9164
9165 -- The following test is true for private types (remember
9166 -- transformation 5. is not applied to those) and in an error
9167 -- situation.
9168
9169 if Constraint_Present then
9170 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9171 end if;
9172
9173 -- For now mark a new derived type as constrained only if it has no
9174 -- discriminants. At the end of Build_Derived_Record_Type we properly
9175 -- set this flag in the case of private extensions. See comments in
9176 -- point 9. just before body of Build_Derived_Record_Type.
9177
9178 Set_Is_Constrained
9179 (Derived_Type,
9180 not (Inherit_Discrims
9181 or else Has_Unknown_Discriminants (Derived_Type)));
9182 end if;
9183
9184 -- STEP 3: initialize fields of derived type
9185
9186 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9187 Set_Stored_Constraint (Derived_Type, No_Elist);
9188
9189 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9190 -- but cannot be interfaces
9191
9192 if not Private_Extension
9193 and then Ekind (Derived_Type) /= E_Private_Type
9194 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9195 then
9196 if Interface_Present (Type_Def) then
9197 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9198 end if;
9199
9200 Set_Interfaces (Derived_Type, No_Elist);
9201 end if;
9202
9203 -- Fields inherited from the Parent_Type
9204
9205 Set_Has_Specified_Layout
9206 (Derived_Type, Has_Specified_Layout (Parent_Type));
9207 Set_Is_Limited_Composite
9208 (Derived_Type, Is_Limited_Composite (Parent_Type));
9209 Set_Is_Private_Composite
9210 (Derived_Type, Is_Private_Composite (Parent_Type));
9211
9212 if Is_Tagged_Type (Parent_Type) then
9213 Set_No_Tagged_Streams_Pragma
9214 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9215 end if;
9216
9217 -- Fields inherited from the Parent_Base
9218
9219 Set_Has_Controlled_Component
9220 (Derived_Type, Has_Controlled_Component (Parent_Base));
9221 Set_Has_Non_Standard_Rep
9222 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9223 Set_Has_Primitive_Operations
9224 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9225
9226 -- Set fields for private derived types
9227
9228 if Is_Private_Type (Derived_Type) then
9229 Set_Depends_On_Private (Derived_Type, True);
9230 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9231 end if;
9232
9233 -- Inherit fields for non-private types. If this is the completion of a
9234 -- derivation from a private type, the parent itself is private and the
9235 -- attributes come from its full view, which must be present.
9236
9237 if Is_Record_Type (Derived_Type) then
9238 declare
9239 Parent_Full : Entity_Id;
9240
9241 begin
9242 if Is_Private_Type (Parent_Base)
9243 and then not Is_Record_Type (Parent_Base)
9244 then
9245 Parent_Full := Full_View (Parent_Base);
9246 else
9247 Parent_Full := Parent_Base;
9248 end if;
9249
9250 Set_Component_Alignment
9251 (Derived_Type, Component_Alignment (Parent_Full));
9252 Set_C_Pass_By_Copy
9253 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9254 Set_Has_Complex_Representation
9255 (Derived_Type, Has_Complex_Representation (Parent_Full));
9256
9257 -- For untagged types, inherit the layout by default to avoid
9258 -- costly changes of representation for type conversions.
9259
9260 if not Is_Tagged then
9261 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9262 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9263 end if;
9264 end;
9265 end if;
9266
9267 -- Set fields for tagged types
9268
9269 if Is_Tagged then
9270 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9271
9272 -- All tagged types defined in Ada.Finalization are controlled
9273
9274 if Chars (Scope (Derived_Type)) = Name_Finalization
9275 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9276 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9277 then
9278 Set_Is_Controlled_Active (Derived_Type);
9279 else
9280 Set_Is_Controlled_Active
9281 (Derived_Type, Is_Controlled_Active (Parent_Base));
9282 end if;
9283
9284 -- Minor optimization: there is no need to generate the class-wide
9285 -- entity associated with an underlying record view.
9286
9287 if not Is_Underlying_Record_View (Derived_Type) then
9288 Make_Class_Wide_Type (Derived_Type);
9289 end if;
9290
9291 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9292
9293 if Has_Discriminants (Derived_Type)
9294 and then Constraint_Present
9295 then
9296 Set_Stored_Constraint
9297 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9298 end if;
9299
9300 if Ada_Version >= Ada_2005 then
9301 declare
9302 Ifaces_List : Elist_Id;
9303
9304 begin
9305 -- Checks rules 3.9.4 (13/2 and 14/2)
9306
9307 if Comes_From_Source (Derived_Type)
9308 and then not Is_Private_Type (Derived_Type)
9309 and then Is_Interface (Parent_Type)
9310 and then not Is_Interface (Derived_Type)
9311 then
9312 if Is_Task_Interface (Parent_Type) then
9313 Error_Msg_N
9314 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9315 Derived_Type);
9316
9317 elsif Is_Protected_Interface (Parent_Type) then
9318 Error_Msg_N
9319 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9320 Derived_Type);
9321 end if;
9322 end if;
9323
9324 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9325
9326 Check_Interfaces (N, Type_Def);
9327
9328 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9329 -- not already in the parents.
9330
9331 Collect_Interfaces
9332 (T => Derived_Type,
9333 Ifaces_List => Ifaces_List,
9334 Exclude_Parents => True);
9335
9336 Set_Interfaces (Derived_Type, Ifaces_List);
9337
9338 -- If the derived type is the anonymous type created for
9339 -- a declaration whose parent has a constraint, propagate
9340 -- the interface list to the source type. This must be done
9341 -- prior to the completion of the analysis of the source type
9342 -- because the components in the extension may contain current
9343 -- instances whose legality depends on some ancestor.
9344
9345 if Is_Itype (Derived_Type) then
9346 declare
9347 Def : constant Node_Id :=
9348 Associated_Node_For_Itype (Derived_Type);
9349 begin
9350 if Present (Def)
9351 and then Nkind (Def) = N_Full_Type_Declaration
9352 then
9353 Set_Interfaces
9354 (Defining_Identifier (Def), Ifaces_List);
9355 end if;
9356 end;
9357 end if;
9358
9359 -- A type extension is automatically Ghost when one of its
9360 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9361 -- also inherited when the parent type is Ghost, but this is
9362 -- done in Build_Derived_Type as the mechanism also handles
9363 -- untagged derivations.
9364
9365 if Implements_Ghost_Interface (Derived_Type) then
9366 Set_Is_Ghost_Entity (Derived_Type);
9367 end if;
9368 end;
9369 end if;
9370 end if;
9371
9372 -- STEP 4: Inherit components from the parent base and constrain them.
9373 -- Apply the second transformation described in point 6. above.
9374
9375 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9376 or else not Has_Discriminants (Parent_Type)
9377 or else not Is_Constrained (Parent_Type)
9378 then
9379 Constrs := Discs;
9380 else
9381 Constrs := Discriminant_Constraint (Parent_Type);
9382 end if;
9383
9384 Assoc_List :=
9385 Inherit_Components
9386 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9387
9388 -- STEP 5a: Copy the parent record declaration for untagged types
9389
9390 Set_Has_Implicit_Dereference
9391 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9392
9393 if not Is_Tagged then
9394
9395 -- Discriminant_Constraint (Derived_Type) has been properly
9396 -- constructed. Save it and temporarily set it to Empty because we
9397 -- do not want the call to New_Copy_Tree below to mess this list.
9398
9399 if Has_Discriminants (Derived_Type) then
9400 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9401 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9402 else
9403 Save_Discr_Constr := No_Elist;
9404 end if;
9405
9406 -- Save the Etype field of Derived_Type. It is correctly set now,
9407 -- but the call to New_Copy tree may remap it to point to itself,
9408 -- which is not what we want. Ditto for the Next_Entity field.
9409
9410 Save_Etype := Etype (Derived_Type);
9411 Save_Next_Entity := Next_Entity (Derived_Type);
9412
9413 -- Assoc_List maps all stored discriminants in the Parent_Base to
9414 -- stored discriminants in the Derived_Type. It is fundamental that
9415 -- no types or itypes with discriminants other than the stored
9416 -- discriminants appear in the entities declared inside
9417 -- Derived_Type, since the back end cannot deal with it.
9418
9419 New_Decl :=
9420 New_Copy_Tree
9421 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9422 Copy_Dimensions_Of_Components (Derived_Type);
9423
9424 -- Restore the fields saved prior to the New_Copy_Tree call
9425 -- and compute the stored constraint.
9426
9427 Set_Etype (Derived_Type, Save_Etype);
9428 Link_Entities (Derived_Type, Save_Next_Entity);
9429
9430 if Has_Discriminants (Derived_Type) then
9431 Set_Discriminant_Constraint
9432 (Derived_Type, Save_Discr_Constr);
9433 Set_Stored_Constraint
9434 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9435
9436 Replace_Discriminants (Derived_Type, New_Decl);
9437 end if;
9438
9439 -- Insert the new derived type declaration
9440
9441 Rewrite (N, New_Decl);
9442
9443 -- STEP 5b: Complete the processing for record extensions in generics
9444
9445 -- There is no completion for record extensions declared in the
9446 -- parameter part of a generic, so we need to complete processing for
9447 -- these generic record extensions here. The Record_Type_Definition call
9448 -- will change the Ekind of the components from E_Void to E_Component.
9449
9450 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9451 Record_Type_Definition (Empty, Derived_Type);
9452
9453 -- STEP 5c: Process the record extension for non private tagged types
9454
9455 elsif not Private_Extension then
9456 Expand_Record_Extension (Derived_Type, Type_Def);
9457
9458 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9459 -- implemented interfaces if we are in expansion mode
9460
9461 if Expander_Active
9462 and then Has_Interfaces (Derived_Type)
9463 then
9464 Add_Interface_Tag_Components (N, Derived_Type);
9465 end if;
9466
9467 -- Analyze the record extension
9468
9469 Record_Type_Definition
9470 (Record_Extension_Part (Type_Def), Derived_Type);
9471 end if;
9472
9473 End_Scope;
9474
9475 -- Nothing else to do if there is an error in the derivation.
9476 -- An unusual case: the full view may be derived from a type in an
9477 -- instance, when the partial view was used illegally as an actual
9478 -- in that instance, leading to a circular definition.
9479
9480 if Etype (Derived_Type) = Any_Type
9481 or else Etype (Parent_Type) = Derived_Type
9482 then
9483 return;
9484 end if;
9485
9486 -- Set delayed freeze and then derive subprograms, we need to do
9487 -- this in this order so that derived subprograms inherit the
9488 -- derived freeze if necessary.
9489
9490 Set_Has_Delayed_Freeze (Derived_Type);
9491
9492 if Derive_Subps then
9493 Derive_Subprograms (Parent_Type, Derived_Type);
9494 end if;
9495
9496 -- If we have a private extension which defines a constrained derived
9497 -- type mark as constrained here after we have derived subprograms. See
9498 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9499
9500 if Private_Extension and then Inherit_Discrims then
9501 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9502 Set_Is_Constrained (Derived_Type, True);
9503 Set_Discriminant_Constraint (Derived_Type, Discs);
9504
9505 elsif Is_Constrained (Parent_Type) then
9506 Set_Is_Constrained
9507 (Derived_Type, True);
9508 Set_Discriminant_Constraint
9509 (Derived_Type, Discriminant_Constraint (Parent_Type));
9510 end if;
9511 end if;
9512
9513 -- Update the class-wide type, which shares the now-completed entity
9514 -- list with its specific type. In case of underlying record views,
9515 -- we do not generate the corresponding class wide entity.
9516
9517 if Is_Tagged
9518 and then not Is_Underlying_Record_View (Derived_Type)
9519 then
9520 Set_First_Entity
9521 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9522 Set_Last_Entity
9523 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9524 end if;
9525
9526 Check_Function_Writable_Actuals (N);
9527 end Build_Derived_Record_Type;
9528
9529 ------------------------
9530 -- Build_Derived_Type --
9531 ------------------------
9532
9533 procedure Build_Derived_Type
9534 (N : Node_Id;
9535 Parent_Type : Entity_Id;
9536 Derived_Type : Entity_Id;
9537 Is_Completion : Boolean;
9538 Derive_Subps : Boolean := True)
9539 is
9540 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9541
9542 begin
9543 -- Set common attributes
9544
9545 Set_Scope (Derived_Type, Current_Scope);
9546 Set_Etype (Derived_Type, Parent_Base);
9547 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9548 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9549
9550 Set_Size_Info (Derived_Type, Parent_Type);
9551 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9552
9553 Set_Is_Controlled_Active
9554 (Derived_Type, Is_Controlled_Active (Parent_Type));
9555
9556 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9557 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9558 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9559
9560 if Is_Tagged_Type (Derived_Type) then
9561 Set_No_Tagged_Streams_Pragma
9562 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9563 end if;
9564
9565 -- If the parent has primitive routines and may have not-seen-yet aspect
9566 -- specifications (e.g., a Pack pragma), then set the derived type link
9567 -- in order to later diagnose "early derivation" issues. If in different
9568 -- compilation units, then "early derivation" cannot be an issue (and we
9569 -- don't like interunit references that go in the opposite direction of
9570 -- semantic dependencies).
9571
9572 if Has_Primitive_Operations (Parent_Type)
9573 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9574 Enclosing_Comp_Unit_Node (Derived_Type)
9575 then
9576 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9577 end if;
9578
9579 -- If the parent type is a private subtype, the convention on the base
9580 -- type may be set in the private part, and not propagated to the
9581 -- subtype until later, so we obtain the convention from the base type.
9582
9583 Set_Convention (Derived_Type, Convention (Parent_Base));
9584
9585 -- Set SSO default for record or array type
9586
9587 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9588 and then Is_Base_Type (Derived_Type)
9589 then
9590 Set_Default_SSO (Derived_Type);
9591 end if;
9592
9593 -- A derived type inherits the Default_Initial_Condition pragma coming
9594 -- from any parent type within the derivation chain.
9595
9596 if Has_DIC (Parent_Type) then
9597 Set_Has_Inherited_DIC (Derived_Type);
9598 end if;
9599
9600 -- A derived type inherits any class-wide invariants coming from a
9601 -- parent type or an interface. Note that the invariant procedure of
9602 -- the parent type should not be inherited because the derived type may
9603 -- define invariants of its own.
9604
9605 if not Is_Interface (Derived_Type) then
9606 if Has_Inherited_Invariants (Parent_Type)
9607 or else Has_Inheritable_Invariants (Parent_Type)
9608 then
9609 Set_Has_Inherited_Invariants (Derived_Type);
9610
9611 elsif Is_Concurrent_Type (Derived_Type)
9612 or else Is_Tagged_Type (Derived_Type)
9613 then
9614 declare
9615 Iface : Entity_Id;
9616 Ifaces : Elist_Id;
9617 Iface_Elmt : Elmt_Id;
9618
9619 begin
9620 Collect_Interfaces
9621 (T => Derived_Type,
9622 Ifaces_List => Ifaces,
9623 Exclude_Parents => True);
9624
9625 if Present (Ifaces) then
9626 Iface_Elmt := First_Elmt (Ifaces);
9627 while Present (Iface_Elmt) loop
9628 Iface := Node (Iface_Elmt);
9629
9630 if Has_Inheritable_Invariants (Iface) then
9631 Set_Has_Inherited_Invariants (Derived_Type);
9632 exit;
9633 end if;
9634
9635 Next_Elmt (Iface_Elmt);
9636 end loop;
9637 end if;
9638 end;
9639 end if;
9640 end if;
9641
9642 -- We similarly inherit predicates. Note that for scalar derived types
9643 -- the predicate is inherited from the first subtype, and not from its
9644 -- (anonymous) base type.
9645
9646 if Has_Predicates (Parent_Type)
9647 or else Has_Predicates (First_Subtype (Parent_Type))
9648 then
9649 Set_Has_Predicates (Derived_Type);
9650 end if;
9651
9652 -- The derived type inherits representation clauses from the parent
9653 -- type, and from any interfaces.
9654
9655 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9656
9657 declare
9658 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9659 begin
9660 while Present (Iface) loop
9661 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9662 Next (Iface);
9663 end loop;
9664 end;
9665
9666 -- If the parent type has delayed rep aspects, then mark the derived
9667 -- type as possibly inheriting a delayed rep aspect.
9668
9669 if Has_Delayed_Rep_Aspects (Parent_Type) then
9670 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9671 end if;
9672
9673 -- A derived type becomes Ghost when its parent type is also Ghost
9674 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9675 -- directly inherited because the Ghost policy in effect may differ.
9676
9677 if Is_Ghost_Entity (Parent_Type) then
9678 Set_Is_Ghost_Entity (Derived_Type);
9679 end if;
9680
9681 -- Type dependent processing
9682
9683 case Ekind (Parent_Type) is
9684 when Numeric_Kind =>
9685 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9686
9687 when Array_Kind =>
9688 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9689
9690 when Class_Wide_Kind
9691 | E_Record_Subtype
9692 | E_Record_Type
9693 =>
9694 Build_Derived_Record_Type
9695 (N, Parent_Type, Derived_Type, Derive_Subps);
9696 return;
9697
9698 when Enumeration_Kind =>
9699 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9700
9701 when Access_Kind =>
9702 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9703
9704 when Incomplete_Or_Private_Kind =>
9705 Build_Derived_Private_Type
9706 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9707
9708 -- For discriminated types, the derivation includes deriving
9709 -- primitive operations. For others it is done below.
9710
9711 if Is_Tagged_Type (Parent_Type)
9712 or else Has_Discriminants (Parent_Type)
9713 or else (Present (Full_View (Parent_Type))
9714 and then Has_Discriminants (Full_View (Parent_Type)))
9715 then
9716 return;
9717 end if;
9718
9719 when Concurrent_Kind =>
9720 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9721
9722 when others =>
9723 raise Program_Error;
9724 end case;
9725
9726 -- Nothing more to do if some error occurred
9727
9728 if Etype (Derived_Type) = Any_Type then
9729 return;
9730 end if;
9731
9732 -- Set delayed freeze and then derive subprograms, we need to do this
9733 -- in this order so that derived subprograms inherit the derived freeze
9734 -- if necessary.
9735
9736 Set_Has_Delayed_Freeze (Derived_Type);
9737
9738 if Derive_Subps then
9739 Derive_Subprograms (Parent_Type, Derived_Type);
9740 end if;
9741
9742 Set_Has_Primitive_Operations
9743 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9744 end Build_Derived_Type;
9745
9746 -----------------------
9747 -- Build_Discriminal --
9748 -----------------------
9749
9750 procedure Build_Discriminal (Discrim : Entity_Id) is
9751 D_Minal : Entity_Id;
9752 CR_Disc : Entity_Id;
9753
9754 begin
9755 -- A discriminal has the same name as the discriminant
9756
9757 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9758
9759 Set_Ekind (D_Minal, E_In_Parameter);
9760 Set_Mechanism (D_Minal, Default_Mechanism);
9761 Set_Etype (D_Minal, Etype (Discrim));
9762 Set_Scope (D_Minal, Current_Scope);
9763 Set_Parent (D_Minal, Parent (Discrim));
9764
9765 Set_Discriminal (Discrim, D_Minal);
9766 Set_Discriminal_Link (D_Minal, Discrim);
9767
9768 -- For task types, build at once the discriminants of the corresponding
9769 -- record, which are needed if discriminants are used in entry defaults
9770 -- and in family bounds.
9771
9772 if Is_Concurrent_Type (Current_Scope)
9773 or else
9774 Is_Limited_Type (Current_Scope)
9775 then
9776 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9777
9778 Set_Ekind (CR_Disc, E_In_Parameter);
9779 Set_Mechanism (CR_Disc, Default_Mechanism);
9780 Set_Etype (CR_Disc, Etype (Discrim));
9781 Set_Scope (CR_Disc, Current_Scope);
9782 Set_Discriminal_Link (CR_Disc, Discrim);
9783 Set_CR_Discriminant (Discrim, CR_Disc);
9784 end if;
9785 end Build_Discriminal;
9786
9787 ------------------------------------
9788 -- Build_Discriminant_Constraints --
9789 ------------------------------------
9790
9791 function Build_Discriminant_Constraints
9792 (T : Entity_Id;
9793 Def : Node_Id;
9794 Derived_Def : Boolean := False) return Elist_Id
9795 is
9796 C : constant Node_Id := Constraint (Def);
9797 Nb_Discr : constant Nat := Number_Discriminants (T);
9798
9799 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9800 -- Saves the expression corresponding to a given discriminant in T
9801
9802 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9803 -- Return the Position number within array Discr_Expr of a discriminant
9804 -- D within the discriminant list of the discriminated type T.
9805
9806 procedure Process_Discriminant_Expression
9807 (Expr : Node_Id;
9808 D : Entity_Id);
9809 -- If this is a discriminant constraint on a partial view, do not
9810 -- generate an overflow check on the discriminant expression. The check
9811 -- will be generated when constraining the full view. Otherwise the
9812 -- backend creates duplicate symbols for the temporaries corresponding
9813 -- to the expressions to be checked, causing spurious assembler errors.
9814
9815 ------------------
9816 -- Pos_Of_Discr --
9817 ------------------
9818
9819 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9820 Disc : Entity_Id;
9821
9822 begin
9823 Disc := First_Discriminant (T);
9824 for J in Discr_Expr'Range loop
9825 if Disc = D then
9826 return J;
9827 end if;
9828
9829 Next_Discriminant (Disc);
9830 end loop;
9831
9832 -- Note: Since this function is called on discriminants that are
9833 -- known to belong to the discriminated type, falling through the
9834 -- loop with no match signals an internal compiler error.
9835
9836 raise Program_Error;
9837 end Pos_Of_Discr;
9838
9839 -------------------------------------
9840 -- Process_Discriminant_Expression --
9841 -------------------------------------
9842
9843 procedure Process_Discriminant_Expression
9844 (Expr : Node_Id;
9845 D : Entity_Id)
9846 is
9847 BDT : constant Entity_Id := Base_Type (Etype (D));
9848
9849 begin
9850 -- If this is a discriminant constraint on a partial view, do
9851 -- not generate an overflow on the discriminant expression. The
9852 -- check will be generated when constraining the full view.
9853
9854 if Is_Private_Type (T)
9855 and then Present (Full_View (T))
9856 then
9857 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9858 else
9859 Analyze_And_Resolve (Expr, BDT);
9860 end if;
9861 end Process_Discriminant_Expression;
9862
9863 -- Declarations local to Build_Discriminant_Constraints
9864
9865 Discr : Entity_Id;
9866 E : Entity_Id;
9867 Elist : constant Elist_Id := New_Elmt_List;
9868
9869 Constr : Node_Id;
9870 Expr : Node_Id;
9871 Id : Node_Id;
9872 Position : Nat;
9873 Found : Boolean;
9874
9875 Discrim_Present : Boolean := False;
9876
9877 -- Start of processing for Build_Discriminant_Constraints
9878
9879 begin
9880 -- The following loop will process positional associations only.
9881 -- For a positional association, the (single) discriminant is
9882 -- implicitly specified by position, in textual order (RM 3.7.2).
9883
9884 Discr := First_Discriminant (T);
9885 Constr := First (Constraints (C));
9886 for D in Discr_Expr'Range loop
9887 exit when Nkind (Constr) = N_Discriminant_Association;
9888
9889 if No (Constr) then
9890 Error_Msg_N ("too few discriminants given in constraint", C);
9891 return New_Elmt_List;
9892
9893 elsif Nkind (Constr) = N_Range
9894 or else (Nkind (Constr) = N_Attribute_Reference
9895 and then Attribute_Name (Constr) = Name_Range)
9896 then
9897 Error_Msg_N
9898 ("a range is not a valid discriminant constraint", Constr);
9899 Discr_Expr (D) := Error;
9900
9901 elsif Nkind (Constr) = N_Subtype_Indication then
9902 Error_Msg_N
9903 ("a subtype indication is not a valid discriminant constraint",
9904 Constr);
9905 Discr_Expr (D) := Error;
9906
9907 else
9908 Process_Discriminant_Expression (Constr, Discr);
9909 Discr_Expr (D) := Constr;
9910 end if;
9911
9912 Next_Discriminant (Discr);
9913 Next (Constr);
9914 end loop;
9915
9916 if No (Discr) and then Present (Constr) then
9917 Error_Msg_N ("too many discriminants given in constraint", Constr);
9918 return New_Elmt_List;
9919 end if;
9920
9921 -- Named associations can be given in any order, but if both positional
9922 -- and named associations are used in the same discriminant constraint,
9923 -- then positional associations must occur first, at their normal
9924 -- position. Hence once a named association is used, the rest of the
9925 -- discriminant constraint must use only named associations.
9926
9927 while Present (Constr) loop
9928
9929 -- Positional association forbidden after a named association
9930
9931 if Nkind (Constr) /= N_Discriminant_Association then
9932 Error_Msg_N ("positional association follows named one", Constr);
9933 return New_Elmt_List;
9934
9935 -- Otherwise it is a named association
9936
9937 else
9938 -- E records the type of the discriminants in the named
9939 -- association. All the discriminants specified in the same name
9940 -- association must have the same type.
9941
9942 E := Empty;
9943
9944 -- Search the list of discriminants in T to see if the simple name
9945 -- given in the constraint matches any of them.
9946
9947 Id := First (Selector_Names (Constr));
9948 while Present (Id) loop
9949 Found := False;
9950
9951 -- If Original_Discriminant is present, we are processing a
9952 -- generic instantiation and this is an instance node. We need
9953 -- to find the name of the corresponding discriminant in the
9954 -- actual record type T and not the name of the discriminant in
9955 -- the generic formal. Example:
9956
9957 -- generic
9958 -- type G (D : int) is private;
9959 -- package P is
9960 -- subtype W is G (D => 1);
9961 -- end package;
9962 -- type Rec (X : int) is record ... end record;
9963 -- package Q is new P (G => Rec);
9964
9965 -- At the point of the instantiation, formal type G is Rec
9966 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9967 -- which really looks like "subtype W is Rec (D => 1);" at
9968 -- the point of instantiation, we want to find the discriminant
9969 -- that corresponds to D in Rec, i.e. X.
9970
9971 if Present (Original_Discriminant (Id))
9972 and then In_Instance
9973 then
9974 Discr := Find_Corresponding_Discriminant (Id, T);
9975 Found := True;
9976
9977 else
9978 Discr := First_Discriminant (T);
9979 while Present (Discr) loop
9980 if Chars (Discr) = Chars (Id) then
9981 Found := True;
9982 exit;
9983 end if;
9984
9985 Next_Discriminant (Discr);
9986 end loop;
9987
9988 if not Found then
9989 Error_Msg_N ("& does not match any discriminant", Id);
9990 return New_Elmt_List;
9991
9992 -- If the parent type is a generic formal, preserve the
9993 -- name of the discriminant for subsequent instances.
9994 -- see comment at the beginning of this if statement.
9995
9996 elsif Is_Generic_Type (Root_Type (T)) then
9997 Set_Original_Discriminant (Id, Discr);
9998 end if;
9999 end if;
10000
10001 Position := Pos_Of_Discr (T, Discr);
10002
10003 if Present (Discr_Expr (Position)) then
10004 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10005
10006 else
10007 -- Each discriminant specified in the same named association
10008 -- must be associated with a separate copy of the
10009 -- corresponding expression.
10010
10011 if Present (Next (Id)) then
10012 Expr := New_Copy_Tree (Expression (Constr));
10013 Set_Parent (Expr, Parent (Expression (Constr)));
10014 else
10015 Expr := Expression (Constr);
10016 end if;
10017
10018 Discr_Expr (Position) := Expr;
10019 Process_Discriminant_Expression (Expr, Discr);
10020 end if;
10021
10022 -- A discriminant association with more than one discriminant
10023 -- name is only allowed if the named discriminants are all of
10024 -- the same type (RM 3.7.1(8)).
10025
10026 if E = Empty then
10027 E := Base_Type (Etype (Discr));
10028
10029 elsif Base_Type (Etype (Discr)) /= E then
10030 Error_Msg_N
10031 ("all discriminants in an association " &
10032 "must have the same type", Id);
10033 end if;
10034
10035 Next (Id);
10036 end loop;
10037 end if;
10038
10039 Next (Constr);
10040 end loop;
10041
10042 -- A discriminant constraint must provide exactly one value for each
10043 -- discriminant of the type (RM 3.7.1(8)).
10044
10045 for J in Discr_Expr'Range loop
10046 if No (Discr_Expr (J)) then
10047 Error_Msg_N ("too few discriminants given in constraint", C);
10048 return New_Elmt_List;
10049 end if;
10050 end loop;
10051
10052 -- Determine if there are discriminant expressions in the constraint
10053
10054 for J in Discr_Expr'Range loop
10055 if Denotes_Discriminant
10056 (Discr_Expr (J), Check_Concurrent => True)
10057 then
10058 Discrim_Present := True;
10059 end if;
10060 end loop;
10061
10062 -- Build an element list consisting of the expressions given in the
10063 -- discriminant constraint and apply the appropriate checks. The list
10064 -- is constructed after resolving any named discriminant associations
10065 -- and therefore the expressions appear in the textual order of the
10066 -- discriminants.
10067
10068 Discr := First_Discriminant (T);
10069 for J in Discr_Expr'Range loop
10070 if Discr_Expr (J) /= Error then
10071 Append_Elmt (Discr_Expr (J), Elist);
10072
10073 -- If any of the discriminant constraints is given by a
10074 -- discriminant and we are in a derived type declaration we
10075 -- have a discriminant renaming. Establish link between new
10076 -- and old discriminant. The new discriminant has an implicit
10077 -- dereference if the old one does.
10078
10079 if Denotes_Discriminant (Discr_Expr (J)) then
10080 if Derived_Def then
10081 declare
10082 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10083
10084 begin
10085 Set_Corresponding_Discriminant (New_Discr, Discr);
10086 Set_Has_Implicit_Dereference (New_Discr,
10087 Has_Implicit_Dereference (Discr));
10088 end;
10089 end if;
10090
10091 -- Force the evaluation of non-discriminant expressions.
10092 -- If we have found a discriminant in the constraint 3.4(26)
10093 -- and 3.8(18) demand that no range checks are performed are
10094 -- after evaluation. If the constraint is for a component
10095 -- definition that has a per-object constraint, expressions are
10096 -- evaluated but not checked either. In all other cases perform
10097 -- a range check.
10098
10099 else
10100 if Discrim_Present then
10101 null;
10102
10103 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10104 and then Has_Per_Object_Constraint
10105 (Defining_Identifier (Parent (Parent (Def))))
10106 then
10107 null;
10108
10109 elsif Is_Access_Type (Etype (Discr)) then
10110 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10111
10112 else
10113 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10114 end if;
10115
10116 Force_Evaluation (Discr_Expr (J));
10117 end if;
10118
10119 -- Check that the designated type of an access discriminant's
10120 -- expression is not a class-wide type unless the discriminant's
10121 -- designated type is also class-wide.
10122
10123 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10124 and then not Is_Class_Wide_Type
10125 (Designated_Type (Etype (Discr)))
10126 and then Etype (Discr_Expr (J)) /= Any_Type
10127 and then Is_Class_Wide_Type
10128 (Designated_Type (Etype (Discr_Expr (J))))
10129 then
10130 Wrong_Type (Discr_Expr (J), Etype (Discr));
10131
10132 elsif Is_Access_Type (Etype (Discr))
10133 and then not Is_Access_Constant (Etype (Discr))
10134 and then Is_Access_Type (Etype (Discr_Expr (J)))
10135 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10136 then
10137 Error_Msg_NE
10138 ("constraint for discriminant& must be access to variable",
10139 Def, Discr);
10140 end if;
10141 end if;
10142
10143 Next_Discriminant (Discr);
10144 end loop;
10145
10146 return Elist;
10147 end Build_Discriminant_Constraints;
10148
10149 ---------------------------------
10150 -- Build_Discriminated_Subtype --
10151 ---------------------------------
10152
10153 procedure Build_Discriminated_Subtype
10154 (T : Entity_Id;
10155 Def_Id : Entity_Id;
10156 Elist : Elist_Id;
10157 Related_Nod : Node_Id;
10158 For_Access : Boolean := False)
10159 is
10160 Has_Discrs : constant Boolean := Has_Discriminants (T);
10161 Constrained : constant Boolean :=
10162 (Has_Discrs
10163 and then not Is_Empty_Elmt_List (Elist)
10164 and then not Is_Class_Wide_Type (T))
10165 or else Is_Constrained (T);
10166
10167 begin
10168 if Ekind (T) = E_Record_Type then
10169 Set_Ekind (Def_Id, E_Record_Subtype);
10170
10171 -- Inherit preelaboration flag from base, for types for which it
10172 -- may have been set: records, private types, protected types.
10173
10174 Set_Known_To_Have_Preelab_Init
10175 (Def_Id, Known_To_Have_Preelab_Init (T));
10176
10177 elsif Ekind (T) = E_Task_Type then
10178 Set_Ekind (Def_Id, E_Task_Subtype);
10179
10180 elsif Ekind (T) = E_Protected_Type then
10181 Set_Ekind (Def_Id, E_Protected_Subtype);
10182 Set_Known_To_Have_Preelab_Init
10183 (Def_Id, Known_To_Have_Preelab_Init (T));
10184
10185 elsif Is_Private_Type (T) then
10186 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10187 Set_Known_To_Have_Preelab_Init
10188 (Def_Id, Known_To_Have_Preelab_Init (T));
10189
10190 -- Private subtypes may have private dependents
10191
10192 Set_Private_Dependents (Def_Id, New_Elmt_List);
10193
10194 elsif Is_Class_Wide_Type (T) then
10195 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10196
10197 else
10198 -- Incomplete type. Attach subtype to list of dependents, to be
10199 -- completed with full view of parent type, unless is it the
10200 -- designated subtype of a record component within an init_proc.
10201 -- This last case arises for a component of an access type whose
10202 -- designated type is incomplete (e.g. a Taft Amendment type).
10203 -- The designated subtype is within an inner scope, and needs no
10204 -- elaboration, because only the access type is needed in the
10205 -- initialization procedure.
10206
10207 if Ekind (T) = E_Incomplete_Type then
10208 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10209 else
10210 Set_Ekind (Def_Id, Ekind (T));
10211 end if;
10212
10213 if For_Access and then Within_Init_Proc then
10214 null;
10215 else
10216 Append_Elmt (Def_Id, Private_Dependents (T));
10217 end if;
10218 end if;
10219
10220 Set_Etype (Def_Id, T);
10221 Init_Size_Align (Def_Id);
10222 Set_Has_Discriminants (Def_Id, Has_Discrs);
10223 Set_Is_Constrained (Def_Id, Constrained);
10224
10225 Set_First_Entity (Def_Id, First_Entity (T));
10226 Set_Last_Entity (Def_Id, Last_Entity (T));
10227 Set_Has_Implicit_Dereference
10228 (Def_Id, Has_Implicit_Dereference (T));
10229 Set_Has_Pragma_Unreferenced_Objects
10230 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10231
10232 -- If the subtype is the completion of a private declaration, there may
10233 -- have been representation clauses for the partial view, and they must
10234 -- be preserved. Build_Derived_Type chains the inherited clauses with
10235 -- the ones appearing on the extension. If this comes from a subtype
10236 -- declaration, all clauses are inherited.
10237
10238 if No (First_Rep_Item (Def_Id)) then
10239 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10240 end if;
10241
10242 if Is_Tagged_Type (T) then
10243 Set_Is_Tagged_Type (Def_Id);
10244 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10245 Make_Class_Wide_Type (Def_Id);
10246 end if;
10247
10248 Set_Stored_Constraint (Def_Id, No_Elist);
10249
10250 if Has_Discrs then
10251 Set_Discriminant_Constraint (Def_Id, Elist);
10252 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10253 end if;
10254
10255 if Is_Tagged_Type (T) then
10256
10257 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10258 -- concurrent record type (which has the list of primitive
10259 -- operations).
10260
10261 if Ada_Version >= Ada_2005
10262 and then Is_Concurrent_Type (T)
10263 then
10264 Set_Corresponding_Record_Type (Def_Id,
10265 Corresponding_Record_Type (T));
10266 else
10267 Set_Direct_Primitive_Operations (Def_Id,
10268 Direct_Primitive_Operations (T));
10269 end if;
10270
10271 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10272 end if;
10273
10274 -- Subtypes introduced by component declarations do not need to be
10275 -- marked as delayed, and do not get freeze nodes, because the semantics
10276 -- verifies that the parents of the subtypes are frozen before the
10277 -- enclosing record is frozen.
10278
10279 if not Is_Type (Scope (Def_Id)) then
10280 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10281
10282 if Is_Private_Type (T)
10283 and then Present (Full_View (T))
10284 then
10285 Conditional_Delay (Def_Id, Full_View (T));
10286 else
10287 Conditional_Delay (Def_Id, T);
10288 end if;
10289 end if;
10290
10291 if Is_Record_Type (T) then
10292 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10293
10294 if Has_Discrs
10295 and then not Is_Empty_Elmt_List (Elist)
10296 and then not For_Access
10297 then
10298 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10299
10300 else
10301 Set_Cloned_Subtype (Def_Id, T);
10302 end if;
10303 end if;
10304 end Build_Discriminated_Subtype;
10305
10306 ---------------------------
10307 -- Build_Itype_Reference --
10308 ---------------------------
10309
10310 procedure Build_Itype_Reference
10311 (Ityp : Entity_Id;
10312 Nod : Node_Id)
10313 is
10314 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10315 begin
10316
10317 -- Itype references are only created for use by the back-end
10318
10319 if Inside_A_Generic then
10320 return;
10321 else
10322 Set_Itype (IR, Ityp);
10323
10324 -- If Nod is a library unit entity, then Insert_After won't work,
10325 -- because Nod is not a member of any list. Therefore, we use
10326 -- Add_Global_Declaration in this case. This can happen if we have a
10327 -- build-in-place library function, child unit or not.
10328
10329 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10330 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10331 N_Subprogram_Declaration)
10332 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10333 then
10334 Add_Global_Declaration (IR);
10335 else
10336 Insert_After (Nod, IR);
10337 end if;
10338 end if;
10339 end Build_Itype_Reference;
10340
10341 ------------------------
10342 -- Build_Scalar_Bound --
10343 ------------------------
10344
10345 function Build_Scalar_Bound
10346 (Bound : Node_Id;
10347 Par_T : Entity_Id;
10348 Der_T : Entity_Id) return Node_Id
10349 is
10350 New_Bound : Entity_Id;
10351
10352 begin
10353 -- Note: not clear why this is needed, how can the original bound
10354 -- be unanalyzed at this point? and if it is, what business do we
10355 -- have messing around with it? and why is the base type of the
10356 -- parent type the right type for the resolution. It probably is
10357 -- not. It is OK for the new bound we are creating, but not for
10358 -- the old one??? Still if it never happens, no problem.
10359
10360 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10361
10362 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10363 New_Bound := New_Copy (Bound);
10364 Set_Etype (New_Bound, Der_T);
10365 Set_Analyzed (New_Bound);
10366
10367 elsif Is_Entity_Name (Bound) then
10368 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10369
10370 -- The following is almost certainly wrong. What business do we have
10371 -- relocating a node (Bound) that is presumably still attached to
10372 -- the tree elsewhere???
10373
10374 else
10375 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10376 end if;
10377
10378 Set_Etype (New_Bound, Der_T);
10379 return New_Bound;
10380 end Build_Scalar_Bound;
10381
10382 -------------------------------
10383 -- Check_Abstract_Overriding --
10384 -------------------------------
10385
10386 procedure Check_Abstract_Overriding (T : Entity_Id) is
10387 Alias_Subp : Entity_Id;
10388 Elmt : Elmt_Id;
10389 Op_List : Elist_Id;
10390 Subp : Entity_Id;
10391 Type_Def : Node_Id;
10392
10393 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10394 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10395 -- which has pragma Implemented already set. Check whether Subp's entity
10396 -- kind conforms to the implementation kind of the overridden routine.
10397
10398 procedure Check_Pragma_Implemented
10399 (Subp : Entity_Id;
10400 Iface_Subp : Entity_Id);
10401 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10402 -- Iface_Subp and both entities have pragma Implemented already set on
10403 -- them. Check whether the two implementation kinds are conforming.
10404
10405 procedure Inherit_Pragma_Implemented
10406 (Subp : Entity_Id;
10407 Iface_Subp : Entity_Id);
10408 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10409 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10410 -- Propagate the implementation kind of Iface_Subp to Subp.
10411
10412 ------------------------------
10413 -- Check_Pragma_Implemented --
10414 ------------------------------
10415
10416 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10417 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10418 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10419 Subp_Alias : constant Entity_Id := Alias (Subp);
10420 Contr_Typ : Entity_Id;
10421 Impl_Subp : Entity_Id;
10422
10423 begin
10424 -- Subp must have an alias since it is a hidden entity used to link
10425 -- an interface subprogram to its overriding counterpart.
10426
10427 pragma Assert (Present (Subp_Alias));
10428
10429 -- Handle aliases to synchronized wrappers
10430
10431 Impl_Subp := Subp_Alias;
10432
10433 if Is_Primitive_Wrapper (Impl_Subp) then
10434 Impl_Subp := Wrapped_Entity (Impl_Subp);
10435 end if;
10436
10437 -- Extract the type of the controlling formal
10438
10439 Contr_Typ := Etype (First_Formal (Subp_Alias));
10440
10441 if Is_Concurrent_Record_Type (Contr_Typ) then
10442 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10443 end if;
10444
10445 -- An interface subprogram whose implementation kind is By_Entry must
10446 -- be implemented by an entry.
10447
10448 if Impl_Kind = Name_By_Entry
10449 and then Ekind (Impl_Subp) /= E_Entry
10450 then
10451 Error_Msg_Node_2 := Iface_Alias;
10452 Error_Msg_NE
10453 ("type & must implement abstract subprogram & with an entry",
10454 Subp_Alias, Contr_Typ);
10455
10456 elsif Impl_Kind = Name_By_Protected_Procedure then
10457
10458 -- An interface subprogram whose implementation kind is By_
10459 -- Protected_Procedure cannot be implemented by a primitive
10460 -- procedure of a task type.
10461
10462 if Ekind (Contr_Typ) /= E_Protected_Type then
10463 Error_Msg_Node_2 := Contr_Typ;
10464 Error_Msg_NE
10465 ("interface subprogram & cannot be implemented by a "
10466 & "primitive procedure of task type &",
10467 Subp_Alias, Iface_Alias);
10468
10469 -- An interface subprogram whose implementation kind is By_
10470 -- Protected_Procedure must be implemented by a procedure.
10471
10472 elsif Ekind (Impl_Subp) /= E_Procedure then
10473 Error_Msg_Node_2 := Iface_Alias;
10474 Error_Msg_NE
10475 ("type & must implement abstract subprogram & with a "
10476 & "procedure", Subp_Alias, Contr_Typ);
10477
10478 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10479 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10480 then
10481 Error_Msg_Name_1 := Impl_Kind;
10482 Error_Msg_N
10483 ("overriding operation& must have synchronization%",
10484 Subp_Alias);
10485 end if;
10486
10487 -- If primitive has Optional synchronization, overriding operation
10488 -- must match if it has an explicit synchronization.
10489
10490 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10491 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10492 then
10493 Error_Msg_Name_1 := Impl_Kind;
10494 Error_Msg_N
10495 ("overriding operation& must have synchronization%", Subp_Alias);
10496 end if;
10497 end Check_Pragma_Implemented;
10498
10499 ------------------------------
10500 -- Check_Pragma_Implemented --
10501 ------------------------------
10502
10503 procedure Check_Pragma_Implemented
10504 (Subp : Entity_Id;
10505 Iface_Subp : Entity_Id)
10506 is
10507 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10508 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10509
10510 begin
10511 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10512 -- and overriding subprogram are different. In general this is an
10513 -- error except when the implementation kind of the overridden
10514 -- subprograms is By_Any or Optional.
10515
10516 if Iface_Kind /= Subp_Kind
10517 and then Iface_Kind /= Name_By_Any
10518 and then Iface_Kind /= Name_Optional
10519 then
10520 if Iface_Kind = Name_By_Entry then
10521 Error_Msg_N
10522 ("incompatible implementation kind, overridden subprogram " &
10523 "is marked By_Entry", Subp);
10524 else
10525 Error_Msg_N
10526 ("incompatible implementation kind, overridden subprogram " &
10527 "is marked By_Protected_Procedure", Subp);
10528 end if;
10529 end if;
10530 end Check_Pragma_Implemented;
10531
10532 --------------------------------
10533 -- Inherit_Pragma_Implemented --
10534 --------------------------------
10535
10536 procedure Inherit_Pragma_Implemented
10537 (Subp : Entity_Id;
10538 Iface_Subp : Entity_Id)
10539 is
10540 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10541 Loc : constant Source_Ptr := Sloc (Subp);
10542 Impl_Prag : Node_Id;
10543
10544 begin
10545 -- Since the implementation kind is stored as a representation item
10546 -- rather than a flag, create a pragma node.
10547
10548 Impl_Prag :=
10549 Make_Pragma (Loc,
10550 Chars => Name_Implemented,
10551 Pragma_Argument_Associations => New_List (
10552 Make_Pragma_Argument_Association (Loc,
10553 Expression => New_Occurrence_Of (Subp, Loc)),
10554
10555 Make_Pragma_Argument_Association (Loc,
10556 Expression => Make_Identifier (Loc, Iface_Kind))));
10557
10558 -- The pragma doesn't need to be analyzed because it is internally
10559 -- built. It is safe to directly register it as a rep item since we
10560 -- are only interested in the characters of the implementation kind.
10561
10562 Record_Rep_Item (Subp, Impl_Prag);
10563 end Inherit_Pragma_Implemented;
10564
10565 -- Start of processing for Check_Abstract_Overriding
10566
10567 begin
10568 Op_List := Primitive_Operations (T);
10569
10570 -- Loop to check primitive operations
10571
10572 Elmt := First_Elmt (Op_List);
10573 while Present (Elmt) loop
10574 Subp := Node (Elmt);
10575 Alias_Subp := Alias (Subp);
10576
10577 -- Inherited subprograms are identified by the fact that they do not
10578 -- come from source, and the associated source location is the
10579 -- location of the first subtype of the derived type.
10580
10581 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10582 -- subprograms that "require overriding".
10583
10584 -- Special exception, do not complain about failure to override the
10585 -- stream routines _Input and _Output, as well as the primitive
10586 -- operations used in dispatching selects since we always provide
10587 -- automatic overridings for these subprograms.
10588
10589 -- The partial view of T may have been a private extension, for
10590 -- which inherited functions dispatching on result are abstract.
10591 -- If the full view is a null extension, there is no need for
10592 -- overriding in Ada 2005, but wrappers need to be built for them
10593 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10594
10595 if Is_Null_Extension (T)
10596 and then Has_Controlling_Result (Subp)
10597 and then Ada_Version >= Ada_2005
10598 and then Present (Alias_Subp)
10599 and then not Comes_From_Source (Subp)
10600 and then not Is_Abstract_Subprogram (Alias_Subp)
10601 and then not Is_Access_Type (Etype (Subp))
10602 then
10603 null;
10604
10605 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10606 -- processing because this check is done with the aliased
10607 -- entity
10608
10609 elsif Present (Interface_Alias (Subp)) then
10610 null;
10611
10612 elsif (Is_Abstract_Subprogram (Subp)
10613 or else Requires_Overriding (Subp)
10614 or else
10615 (Has_Controlling_Result (Subp)
10616 and then Present (Alias_Subp)
10617 and then not Comes_From_Source (Subp)
10618 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10619 and then not Is_TSS (Subp, TSS_Stream_Input)
10620 and then not Is_TSS (Subp, TSS_Stream_Output)
10621 and then not Is_Abstract_Type (T)
10622 and then not Is_Predefined_Interface_Primitive (Subp)
10623
10624 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10625 -- with abstract interface types because the check will be done
10626 -- with the aliased entity (otherwise we generate a duplicated
10627 -- error message).
10628
10629 and then not Present (Interface_Alias (Subp))
10630 then
10631 if Present (Alias_Subp) then
10632
10633 -- Only perform the check for a derived subprogram when the
10634 -- type has an explicit record extension. This avoids incorrect
10635 -- flagging of abstract subprograms for the case of a type
10636 -- without an extension that is derived from a formal type
10637 -- with a tagged actual (can occur within a private part).
10638
10639 -- Ada 2005 (AI-391): In the case of an inherited function with
10640 -- a controlling result of the type, the rule does not apply if
10641 -- the type is a null extension (unless the parent function
10642 -- itself is abstract, in which case the function must still be
10643 -- be overridden). The expander will generate an overriding
10644 -- wrapper function calling the parent subprogram (see
10645 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10646
10647 Type_Def := Type_Definition (Parent (T));
10648
10649 if Nkind (Type_Def) = N_Derived_Type_Definition
10650 and then Present (Record_Extension_Part (Type_Def))
10651 and then
10652 (Ada_Version < Ada_2005
10653 or else not Is_Null_Extension (T)
10654 or else Ekind (Subp) = E_Procedure
10655 or else not Has_Controlling_Result (Subp)
10656 or else Is_Abstract_Subprogram (Alias_Subp)
10657 or else Requires_Overriding (Subp)
10658 or else Is_Access_Type (Etype (Subp)))
10659 then
10660 -- Avoid reporting error in case of abstract predefined
10661 -- primitive inherited from interface type because the
10662 -- body of internally generated predefined primitives
10663 -- of tagged types are generated later by Freeze_Type
10664
10665 if Is_Interface (Root_Type (T))
10666 and then Is_Abstract_Subprogram (Subp)
10667 and then Is_Predefined_Dispatching_Operation (Subp)
10668 and then not Comes_From_Source (Ultimate_Alias (Subp))
10669 then
10670 null;
10671
10672 -- A null extension is not obliged to override an inherited
10673 -- procedure subject to pragma Extensions_Visible with value
10674 -- False and at least one controlling OUT parameter
10675 -- (SPARK RM 6.1.7(6)).
10676
10677 elsif Is_Null_Extension (T)
10678 and then Is_EVF_Procedure (Subp)
10679 then
10680 null;
10681
10682 else
10683 Error_Msg_NE
10684 ("type must be declared abstract or & overridden",
10685 T, Subp);
10686
10687 -- Traverse the whole chain of aliased subprograms to
10688 -- complete the error notification. This is especially
10689 -- useful for traceability of the chain of entities when
10690 -- the subprogram corresponds with an interface
10691 -- subprogram (which may be defined in another package).
10692
10693 if Present (Alias_Subp) then
10694 declare
10695 E : Entity_Id;
10696
10697 begin
10698 E := Subp;
10699 while Present (Alias (E)) loop
10700
10701 -- Avoid reporting redundant errors on entities
10702 -- inherited from interfaces
10703
10704 if Sloc (E) /= Sloc (T) then
10705 Error_Msg_Sloc := Sloc (E);
10706 Error_Msg_NE
10707 ("\& has been inherited #", T, Subp);
10708 end if;
10709
10710 E := Alias (E);
10711 end loop;
10712
10713 Error_Msg_Sloc := Sloc (E);
10714
10715 -- AI05-0068: report if there is an overriding
10716 -- non-abstract subprogram that is invisible.
10717
10718 if Is_Hidden (E)
10719 and then not Is_Abstract_Subprogram (E)
10720 then
10721 Error_Msg_NE
10722 ("\& subprogram# is not visible",
10723 T, Subp);
10724
10725 -- Clarify the case where a non-null extension must
10726 -- override inherited procedure subject to pragma
10727 -- Extensions_Visible with value False and at least
10728 -- one controlling OUT param.
10729
10730 elsif Is_EVF_Procedure (E) then
10731 Error_Msg_NE
10732 ("\& # is subject to Extensions_Visible False",
10733 T, Subp);
10734
10735 else
10736 Error_Msg_NE
10737 ("\& has been inherited from subprogram #",
10738 T, Subp);
10739 end if;
10740 end;
10741 end if;
10742 end if;
10743
10744 -- Ada 2005 (AI-345): Protected or task type implementing
10745 -- abstract interfaces.
10746
10747 elsif Is_Concurrent_Record_Type (T)
10748 and then Present (Interfaces (T))
10749 then
10750 -- There is no need to check here RM 9.4(11.9/3) since we
10751 -- are processing the corresponding record type and the
10752 -- mode of the overriding subprograms was verified by
10753 -- Check_Conformance when the corresponding concurrent
10754 -- type declaration was analyzed.
10755
10756 Error_Msg_NE
10757 ("interface subprogram & must be overridden", T, Subp);
10758
10759 -- Examine primitive operations of synchronized type to find
10760 -- homonyms that have the wrong profile.
10761
10762 declare
10763 Prim : Entity_Id;
10764
10765 begin
10766 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10767 while Present (Prim) loop
10768 if Chars (Prim) = Chars (Subp) then
10769 Error_Msg_NE
10770 ("profile is not type conformant with prefixed "
10771 & "view profile of inherited operation&",
10772 Prim, Subp);
10773 end if;
10774
10775 Next_Entity (Prim);
10776 end loop;
10777 end;
10778 end if;
10779
10780 else
10781 Error_Msg_Node_2 := T;
10782 Error_Msg_N
10783 ("abstract subprogram& not allowed for type&", Subp);
10784
10785 -- Also post unconditional warning on the type (unconditional
10786 -- so that if there are more than one of these cases, we get
10787 -- them all, and not just the first one).
10788
10789 Error_Msg_Node_2 := Subp;
10790 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10791 end if;
10792
10793 -- A subprogram subject to pragma Extensions_Visible with value
10794 -- "True" cannot override a subprogram subject to the same pragma
10795 -- with value "False" (SPARK RM 6.1.7(5)).
10796
10797 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10798 and then Present (Overridden_Operation (Subp))
10799 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10800 Extensions_Visible_False
10801 then
10802 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10803 Error_Msg_N
10804 ("subprogram & with Extensions_Visible True cannot override "
10805 & "subprogram # with Extensions_Visible False", Subp);
10806 end if;
10807
10808 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10809
10810 -- Subp is an expander-generated procedure which maps an interface
10811 -- alias to a protected wrapper. The interface alias is flagged by
10812 -- pragma Implemented. Ensure that Subp is a procedure when the
10813 -- implementation kind is By_Protected_Procedure or an entry when
10814 -- By_Entry.
10815
10816 if Ada_Version >= Ada_2012
10817 and then Is_Hidden (Subp)
10818 and then Present (Interface_Alias (Subp))
10819 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10820 then
10821 Check_Pragma_Implemented (Subp);
10822 end if;
10823
10824 -- Subp is an interface primitive which overrides another interface
10825 -- primitive marked with pragma Implemented.
10826
10827 if Ada_Version >= Ada_2012
10828 and then Present (Overridden_Operation (Subp))
10829 and then Has_Rep_Pragma
10830 (Overridden_Operation (Subp), Name_Implemented)
10831 then
10832 -- If the overriding routine is also marked by Implemented, check
10833 -- that the two implementation kinds are conforming.
10834
10835 if Has_Rep_Pragma (Subp, Name_Implemented) then
10836 Check_Pragma_Implemented
10837 (Subp => Subp,
10838 Iface_Subp => Overridden_Operation (Subp));
10839
10840 -- Otherwise the overriding routine inherits the implementation
10841 -- kind from the overridden subprogram.
10842
10843 else
10844 Inherit_Pragma_Implemented
10845 (Subp => Subp,
10846 Iface_Subp => Overridden_Operation (Subp));
10847 end if;
10848 end if;
10849
10850 -- If the operation is a wrapper for a synchronized primitive, it
10851 -- may be called indirectly through a dispatching select. We assume
10852 -- that it will be referenced elsewhere indirectly, and suppress
10853 -- warnings about an unused entity.
10854
10855 if Is_Primitive_Wrapper (Subp)
10856 and then Present (Wrapped_Entity (Subp))
10857 then
10858 Set_Referenced (Wrapped_Entity (Subp));
10859 end if;
10860
10861 Next_Elmt (Elmt);
10862 end loop;
10863 end Check_Abstract_Overriding;
10864
10865 ------------------------------------------------
10866 -- Check_Access_Discriminant_Requires_Limited --
10867 ------------------------------------------------
10868
10869 procedure Check_Access_Discriminant_Requires_Limited
10870 (D : Node_Id;
10871 Loc : Node_Id)
10872 is
10873 begin
10874 -- A discriminant_specification for an access discriminant shall appear
10875 -- only in the declaration for a task or protected type, or for a type
10876 -- with the reserved word 'limited' in its definition or in one of its
10877 -- ancestors (RM 3.7(10)).
10878
10879 -- AI-0063: The proper condition is that type must be immutably limited,
10880 -- or else be a partial view.
10881
10882 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10883 if Is_Limited_View (Current_Scope)
10884 or else
10885 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10886 and then Limited_Present (Parent (Current_Scope)))
10887 then
10888 null;
10889
10890 else
10891 Error_Msg_N
10892 ("access discriminants allowed only for limited types", Loc);
10893 end if;
10894 end if;
10895 end Check_Access_Discriminant_Requires_Limited;
10896
10897 -----------------------------------
10898 -- Check_Aliased_Component_Types --
10899 -----------------------------------
10900
10901 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10902 C : Entity_Id;
10903
10904 begin
10905 -- ??? Also need to check components of record extensions, but not
10906 -- components of protected types (which are always limited).
10907
10908 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10909 -- types to be unconstrained. This is safe because it is illegal to
10910 -- create access subtypes to such types with explicit discriminant
10911 -- constraints.
10912
10913 if not Is_Limited_Type (T) then
10914 if Ekind (T) = E_Record_Type then
10915 C := First_Component (T);
10916 while Present (C) loop
10917 if Is_Aliased (C)
10918 and then Has_Discriminants (Etype (C))
10919 and then not Is_Constrained (Etype (C))
10920 and then not In_Instance_Body
10921 and then Ada_Version < Ada_2005
10922 then
10923 Error_Msg_N
10924 ("aliased component must be constrained (RM 3.6(11))",
10925 C);
10926 end if;
10927
10928 Next_Component (C);
10929 end loop;
10930
10931 elsif Ekind (T) = E_Array_Type then
10932 if Has_Aliased_Components (T)
10933 and then Has_Discriminants (Component_Type (T))
10934 and then not Is_Constrained (Component_Type (T))
10935 and then not In_Instance_Body
10936 and then Ada_Version < Ada_2005
10937 then
10938 Error_Msg_N
10939 ("aliased component type must be constrained (RM 3.6(11))",
10940 T);
10941 end if;
10942 end if;
10943 end if;
10944 end Check_Aliased_Component_Types;
10945
10946 ---------------------------------------
10947 -- Check_Anonymous_Access_Components --
10948 ---------------------------------------
10949
10950 procedure Check_Anonymous_Access_Components
10951 (Typ_Decl : Node_Id;
10952 Typ : Entity_Id;
10953 Prev : Entity_Id;
10954 Comp_List : Node_Id)
10955 is
10956 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10957 Anon_Access : Entity_Id;
10958 Acc_Def : Node_Id;
10959 Comp : Node_Id;
10960 Comp_Def : Node_Id;
10961 Decl : Node_Id;
10962 Type_Def : Node_Id;
10963
10964 procedure Build_Incomplete_Type_Declaration;
10965 -- If the record type contains components that include an access to the
10966 -- current record, then create an incomplete type declaration for the
10967 -- record, to be used as the designated type of the anonymous access.
10968 -- This is done only once, and only if there is no previous partial
10969 -- view of the type.
10970
10971 function Designates_T (Subt : Node_Id) return Boolean;
10972 -- Check whether a node designates the enclosing record type, or 'Class
10973 -- of that type
10974
10975 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10976 -- Check whether an access definition includes a reference to
10977 -- the enclosing record type. The reference can be a subtype mark
10978 -- in the access definition itself, a 'Class attribute reference, or
10979 -- recursively a reference appearing in a parameter specification
10980 -- or result definition of an access_to_subprogram definition.
10981
10982 --------------------------------------
10983 -- Build_Incomplete_Type_Declaration --
10984 --------------------------------------
10985
10986 procedure Build_Incomplete_Type_Declaration is
10987 Decl : Node_Id;
10988 Inc_T : Entity_Id;
10989 H : Entity_Id;
10990
10991 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10992 -- it's "is new ... with record" or else "is tagged record ...".
10993
10994 Is_Tagged : constant Boolean :=
10995 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10996 and then
10997 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10998 or else
10999 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11000 and then Tagged_Present (Type_Definition (Typ_Decl)));
11001
11002 begin
11003 -- If there is a previous partial view, no need to create a new one
11004 -- If the partial view, given by Prev, is incomplete, If Prev is
11005 -- a private declaration, full declaration is flagged accordingly.
11006
11007 if Prev /= Typ then
11008 if Is_Tagged then
11009 Make_Class_Wide_Type (Prev);
11010 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11011 Set_Etype (Class_Wide_Type (Typ), Typ);
11012 end if;
11013
11014 return;
11015
11016 elsif Has_Private_Declaration (Typ) then
11017
11018 -- If we refer to T'Class inside T, and T is the completion of a
11019 -- private type, then make sure the class-wide type exists.
11020
11021 if Is_Tagged then
11022 Make_Class_Wide_Type (Typ);
11023 end if;
11024
11025 return;
11026
11027 -- If there was a previous anonymous access type, the incomplete
11028 -- type declaration will have been created already.
11029
11030 elsif Present (Current_Entity (Typ))
11031 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11032 and then Full_View (Current_Entity (Typ)) = Typ
11033 then
11034 if Is_Tagged
11035 and then Comes_From_Source (Current_Entity (Typ))
11036 and then not Is_Tagged_Type (Current_Entity (Typ))
11037 then
11038 Make_Class_Wide_Type (Typ);
11039 Error_Msg_N
11040 ("incomplete view of tagged type should be declared tagged??",
11041 Parent (Current_Entity (Typ)));
11042 end if;
11043 return;
11044
11045 else
11046 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11047 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11048
11049 -- Type has already been inserted into the current scope. Remove
11050 -- it, and add incomplete declaration for type, so that subsequent
11051 -- anonymous access types can use it. The entity is unchained from
11052 -- the homonym list and from immediate visibility. After analysis,
11053 -- the entity in the incomplete declaration becomes immediately
11054 -- visible in the record declaration that follows.
11055
11056 H := Current_Entity (Typ);
11057
11058 if H = Typ then
11059 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11060 else
11061 while Present (H)
11062 and then Homonym (H) /= Typ
11063 loop
11064 H := Homonym (Typ);
11065 end loop;
11066
11067 Set_Homonym (H, Homonym (Typ));
11068 end if;
11069
11070 Insert_Before (Typ_Decl, Decl);
11071 Analyze (Decl);
11072 Set_Full_View (Inc_T, Typ);
11073
11074 if Is_Tagged then
11075
11076 -- Create a common class-wide type for both views, and set the
11077 -- Etype of the class-wide type to the full view.
11078
11079 Make_Class_Wide_Type (Inc_T);
11080 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11081 Set_Etype (Class_Wide_Type (Typ), Typ);
11082 end if;
11083 end if;
11084 end Build_Incomplete_Type_Declaration;
11085
11086 ------------------
11087 -- Designates_T --
11088 ------------------
11089
11090 function Designates_T (Subt : Node_Id) return Boolean is
11091 Type_Id : constant Name_Id := Chars (Typ);
11092
11093 function Names_T (Nam : Node_Id) return Boolean;
11094 -- The record type has not been introduced in the current scope
11095 -- yet, so we must examine the name of the type itself, either
11096 -- an identifier T, or an expanded name of the form P.T, where
11097 -- P denotes the current scope.
11098
11099 -------------
11100 -- Names_T --
11101 -------------
11102
11103 function Names_T (Nam : Node_Id) return Boolean is
11104 begin
11105 if Nkind (Nam) = N_Identifier then
11106 return Chars (Nam) = Type_Id;
11107
11108 elsif Nkind (Nam) = N_Selected_Component then
11109 if Chars (Selector_Name (Nam)) = Type_Id then
11110 if Nkind (Prefix (Nam)) = N_Identifier then
11111 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11112
11113 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11114 return Chars (Selector_Name (Prefix (Nam))) =
11115 Chars (Current_Scope);
11116 else
11117 return False;
11118 end if;
11119
11120 else
11121 return False;
11122 end if;
11123
11124 else
11125 return False;
11126 end if;
11127 end Names_T;
11128
11129 -- Start of processing for Designates_T
11130
11131 begin
11132 if Nkind (Subt) = N_Identifier then
11133 return Chars (Subt) = Type_Id;
11134
11135 -- Reference can be through an expanded name which has not been
11136 -- analyzed yet, and which designates enclosing scopes.
11137
11138 elsif Nkind (Subt) = N_Selected_Component then
11139 if Names_T (Subt) then
11140 return True;
11141
11142 -- Otherwise it must denote an entity that is already visible.
11143 -- The access definition may name a subtype of the enclosing
11144 -- type, if there is a previous incomplete declaration for it.
11145
11146 else
11147 Find_Selected_Component (Subt);
11148 return
11149 Is_Entity_Name (Subt)
11150 and then Scope (Entity (Subt)) = Current_Scope
11151 and then
11152 (Chars (Base_Type (Entity (Subt))) = Type_Id
11153 or else
11154 (Is_Class_Wide_Type (Entity (Subt))
11155 and then
11156 Chars (Etype (Base_Type (Entity (Subt)))) =
11157 Type_Id));
11158 end if;
11159
11160 -- A reference to the current type may appear as the prefix of
11161 -- a 'Class attribute.
11162
11163 elsif Nkind (Subt) = N_Attribute_Reference
11164 and then Attribute_Name (Subt) = Name_Class
11165 then
11166 return Names_T (Prefix (Subt));
11167
11168 else
11169 return False;
11170 end if;
11171 end Designates_T;
11172
11173 ----------------
11174 -- Mentions_T --
11175 ----------------
11176
11177 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11178 Param_Spec : Node_Id;
11179
11180 Acc_Subprg : constant Node_Id :=
11181 Access_To_Subprogram_Definition (Acc_Def);
11182
11183 begin
11184 if No (Acc_Subprg) then
11185 return Designates_T (Subtype_Mark (Acc_Def));
11186 end if;
11187
11188 -- Component is an access_to_subprogram: examine its formals,
11189 -- and result definition in the case of an access_to_function.
11190
11191 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11192 while Present (Param_Spec) loop
11193 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11194 and then Mentions_T (Parameter_Type (Param_Spec))
11195 then
11196 return True;
11197
11198 elsif Designates_T (Parameter_Type (Param_Spec)) then
11199 return True;
11200 end if;
11201
11202 Next (Param_Spec);
11203 end loop;
11204
11205 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11206 if Nkind (Result_Definition (Acc_Subprg)) =
11207 N_Access_Definition
11208 then
11209 return Mentions_T (Result_Definition (Acc_Subprg));
11210 else
11211 return Designates_T (Result_Definition (Acc_Subprg));
11212 end if;
11213 end if;
11214
11215 return False;
11216 end Mentions_T;
11217
11218 -- Start of processing for Check_Anonymous_Access_Components
11219
11220 begin
11221 if No (Comp_List) then
11222 return;
11223 end if;
11224
11225 Comp := First (Component_Items (Comp_List));
11226 while Present (Comp) loop
11227 if Nkind (Comp) = N_Component_Declaration
11228 and then Present
11229 (Access_Definition (Component_Definition (Comp)))
11230 and then
11231 Mentions_T (Access_Definition (Component_Definition (Comp)))
11232 then
11233 Comp_Def := Component_Definition (Comp);
11234 Acc_Def :=
11235 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11236
11237 Build_Incomplete_Type_Declaration;
11238 Anon_Access := Make_Temporary (Loc, 'S');
11239
11240 -- Create a declaration for the anonymous access type: either
11241 -- an access_to_object or an access_to_subprogram.
11242
11243 if Present (Acc_Def) then
11244 if Nkind (Acc_Def) = N_Access_Function_Definition then
11245 Type_Def :=
11246 Make_Access_Function_Definition (Loc,
11247 Parameter_Specifications =>
11248 Parameter_Specifications (Acc_Def),
11249 Result_Definition => Result_Definition (Acc_Def));
11250 else
11251 Type_Def :=
11252 Make_Access_Procedure_Definition (Loc,
11253 Parameter_Specifications =>
11254 Parameter_Specifications (Acc_Def));
11255 end if;
11256
11257 else
11258 Type_Def :=
11259 Make_Access_To_Object_Definition (Loc,
11260 Subtype_Indication =>
11261 Relocate_Node
11262 (Subtype_Mark (Access_Definition (Comp_Def))));
11263
11264 Set_Constant_Present
11265 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11266 Set_All_Present
11267 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11268 end if;
11269
11270 Set_Null_Exclusion_Present
11271 (Type_Def,
11272 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11273
11274 Decl :=
11275 Make_Full_Type_Declaration (Loc,
11276 Defining_Identifier => Anon_Access,
11277 Type_Definition => Type_Def);
11278
11279 Insert_Before (Typ_Decl, Decl);
11280 Analyze (Decl);
11281
11282 -- If an access to subprogram, create the extra formals
11283
11284 if Present (Acc_Def) then
11285 Create_Extra_Formals (Designated_Type (Anon_Access));
11286 end if;
11287
11288 Rewrite (Comp_Def,
11289 Make_Component_Definition (Loc,
11290 Subtype_Indication =>
11291 New_Occurrence_Of (Anon_Access, Loc)));
11292
11293 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11294 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11295 else
11296 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11297 end if;
11298
11299 Set_Is_Local_Anonymous_Access (Anon_Access);
11300 end if;
11301
11302 Next (Comp);
11303 end loop;
11304
11305 if Present (Variant_Part (Comp_List)) then
11306 declare
11307 V : Node_Id;
11308 begin
11309 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11310 while Present (V) loop
11311 Check_Anonymous_Access_Components
11312 (Typ_Decl, Typ, Prev, Component_List (V));
11313 Next_Non_Pragma (V);
11314 end loop;
11315 end;
11316 end if;
11317 end Check_Anonymous_Access_Components;
11318
11319 ----------------------
11320 -- Check_Completion --
11321 ----------------------
11322
11323 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11324 E : Entity_Id;
11325
11326 procedure Post_Error;
11327 -- Post error message for lack of completion for entity E
11328
11329 ----------------
11330 -- Post_Error --
11331 ----------------
11332
11333 procedure Post_Error is
11334 procedure Missing_Body;
11335 -- Output missing body message
11336
11337 ------------------
11338 -- Missing_Body --
11339 ------------------
11340
11341 procedure Missing_Body is
11342 begin
11343 -- Spec is in same unit, so we can post on spec
11344
11345 if In_Same_Source_Unit (Body_Id, E) then
11346 Error_Msg_N ("missing body for &", E);
11347
11348 -- Spec is in a separate unit, so we have to post on the body
11349
11350 else
11351 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11352 end if;
11353 end Missing_Body;
11354
11355 -- Start of processing for Post_Error
11356
11357 begin
11358 if not Comes_From_Source (E) then
11359 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11360
11361 -- It may be an anonymous protected type created for a
11362 -- single variable. Post error on variable, if present.
11363
11364 declare
11365 Var : Entity_Id;
11366
11367 begin
11368 Var := First_Entity (Current_Scope);
11369 while Present (Var) loop
11370 exit when Etype (Var) = E
11371 and then Comes_From_Source (Var);
11372
11373 Next_Entity (Var);
11374 end loop;
11375
11376 if Present (Var) then
11377 E := Var;
11378 end if;
11379 end;
11380 end if;
11381 end if;
11382
11383 -- If a generated entity has no completion, then either previous
11384 -- semantic errors have disabled the expansion phase, or else we had
11385 -- missing subunits, or else we are compiling without expansion,
11386 -- or else something is very wrong.
11387
11388 if not Comes_From_Source (E) then
11389 pragma Assert
11390 (Serious_Errors_Detected > 0
11391 or else Configurable_Run_Time_Violations > 0
11392 or else Subunits_Missing
11393 or else not Expander_Active);
11394 return;
11395
11396 -- Here for source entity
11397
11398 else
11399 -- Here if no body to post the error message, so we post the error
11400 -- on the declaration that has no completion. This is not really
11401 -- the right place to post it, think about this later ???
11402
11403 if No (Body_Id) then
11404 if Is_Type (E) then
11405 Error_Msg_NE
11406 ("missing full declaration for }", Parent (E), E);
11407 else
11408 Error_Msg_NE ("missing body for &", Parent (E), E);
11409 end if;
11410
11411 -- Package body has no completion for a declaration that appears
11412 -- in the corresponding spec. Post error on the body, with a
11413 -- reference to the non-completed declaration.
11414
11415 else
11416 Error_Msg_Sloc := Sloc (E);
11417
11418 if Is_Type (E) then
11419 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11420
11421 elsif Is_Overloadable (E)
11422 and then Current_Entity_In_Scope (E) /= E
11423 then
11424 -- It may be that the completion is mistyped and appears as
11425 -- a distinct overloading of the entity.
11426
11427 declare
11428 Candidate : constant Entity_Id :=
11429 Current_Entity_In_Scope (E);
11430 Decl : constant Node_Id :=
11431 Unit_Declaration_Node (Candidate);
11432
11433 begin
11434 if Is_Overloadable (Candidate)
11435 and then Ekind (Candidate) = Ekind (E)
11436 and then Nkind (Decl) = N_Subprogram_Body
11437 and then Acts_As_Spec (Decl)
11438 then
11439 Check_Type_Conformant (Candidate, E);
11440
11441 else
11442 Missing_Body;
11443 end if;
11444 end;
11445
11446 else
11447 Missing_Body;
11448 end if;
11449 end if;
11450 end if;
11451 end Post_Error;
11452
11453 -- Local variables
11454
11455 Pack_Id : constant Entity_Id := Current_Scope;
11456
11457 -- Start of processing for Check_Completion
11458
11459 begin
11460 E := First_Entity (Pack_Id);
11461 while Present (E) loop
11462 if Is_Intrinsic_Subprogram (E) then
11463 null;
11464
11465 -- The following situation requires special handling: a child unit
11466 -- that appears in the context clause of the body of its parent:
11467
11468 -- procedure Parent.Child (...);
11469
11470 -- with Parent.Child;
11471 -- package body Parent is
11472
11473 -- Here Parent.Child appears as a local entity, but should not be
11474 -- flagged as requiring completion, because it is a compilation
11475 -- unit.
11476
11477 -- Ignore missing completion for a subprogram that does not come from
11478 -- source (including the _Call primitive operation of RAS types,
11479 -- which has to have the flag Comes_From_Source for other purposes):
11480 -- we assume that the expander will provide the missing completion.
11481 -- In case of previous errors, other expansion actions that provide
11482 -- bodies for null procedures with not be invoked, so inhibit message
11483 -- in those cases.
11484
11485 -- Note that E_Operator is not in the list that follows, because
11486 -- this kind is reserved for predefined operators, that are
11487 -- intrinsic and do not need completion.
11488
11489 elsif Ekind_In (E, E_Function,
11490 E_Procedure,
11491 E_Generic_Function,
11492 E_Generic_Procedure)
11493 then
11494 if Has_Completion (E) then
11495 null;
11496
11497 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11498 null;
11499
11500 elsif Is_Subprogram (E)
11501 and then (not Comes_From_Source (E)
11502 or else Chars (E) = Name_uCall)
11503 then
11504 null;
11505
11506 elsif
11507 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11508 then
11509 null;
11510
11511 elsif Nkind (Parent (E)) = N_Procedure_Specification
11512 and then Null_Present (Parent (E))
11513 and then Serious_Errors_Detected > 0
11514 then
11515 null;
11516
11517 else
11518 Post_Error;
11519 end if;
11520
11521 elsif Is_Entry (E) then
11522 if not Has_Completion (E) and then
11523 (Ekind (Scope (E)) = E_Protected_Object
11524 or else Ekind (Scope (E)) = E_Protected_Type)
11525 then
11526 Post_Error;
11527 end if;
11528
11529 elsif Is_Package_Or_Generic_Package (E) then
11530 if Unit_Requires_Body (E) then
11531 if not Has_Completion (E)
11532 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11533 N_Compilation_Unit
11534 then
11535 Post_Error;
11536 end if;
11537
11538 elsif not Is_Child_Unit (E) then
11539 May_Need_Implicit_Body (E);
11540 end if;
11541
11542 -- A formal incomplete type (Ada 2012) does not require a completion;
11543 -- other incomplete type declarations do.
11544
11545 elsif Ekind (E) = E_Incomplete_Type
11546 and then No (Underlying_Type (E))
11547 and then not Is_Generic_Type (E)
11548 then
11549 Post_Error;
11550
11551 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11552 and then not Has_Completion (E)
11553 then
11554 Post_Error;
11555
11556 -- A single task declared in the current scope is a constant, verify
11557 -- that the body of its anonymous type is in the same scope. If the
11558 -- task is defined elsewhere, this may be a renaming declaration for
11559 -- which no completion is needed.
11560
11561 elsif Ekind (E) = E_Constant
11562 and then Ekind (Etype (E)) = E_Task_Type
11563 and then not Has_Completion (Etype (E))
11564 and then Scope (Etype (E)) = Current_Scope
11565 then
11566 Post_Error;
11567
11568 elsif Ekind (E) = E_Protected_Object
11569 and then not Has_Completion (Etype (E))
11570 then
11571 Post_Error;
11572
11573 elsif Ekind (E) = E_Record_Type then
11574 if Is_Tagged_Type (E) then
11575 Check_Abstract_Overriding (E);
11576 Check_Conventions (E);
11577 end if;
11578
11579 Check_Aliased_Component_Types (E);
11580
11581 elsif Ekind (E) = E_Array_Type then
11582 Check_Aliased_Component_Types (E);
11583
11584 end if;
11585
11586 Next_Entity (E);
11587 end loop;
11588 end Check_Completion;
11589
11590 ------------------------------------
11591 -- Check_CPP_Type_Has_No_Defaults --
11592 ------------------------------------
11593
11594 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11595 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11596 Clist : Node_Id;
11597 Comp : Node_Id;
11598
11599 begin
11600 -- Obtain the component list
11601
11602 if Nkind (Tdef) = N_Record_Definition then
11603 Clist := Component_List (Tdef);
11604 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11605 Clist := Component_List (Record_Extension_Part (Tdef));
11606 end if;
11607
11608 -- Check all components to ensure no default expressions
11609
11610 if Present (Clist) then
11611 Comp := First (Component_Items (Clist));
11612 while Present (Comp) loop
11613 if Present (Expression (Comp)) then
11614 Error_Msg_N
11615 ("component of imported 'C'P'P type cannot have "
11616 & "default expression", Expression (Comp));
11617 end if;
11618
11619 Next (Comp);
11620 end loop;
11621 end if;
11622 end Check_CPP_Type_Has_No_Defaults;
11623
11624 ----------------------------
11625 -- Check_Delta_Expression --
11626 ----------------------------
11627
11628 procedure Check_Delta_Expression (E : Node_Id) is
11629 begin
11630 if not (Is_Real_Type (Etype (E))) then
11631 Wrong_Type (E, Any_Real);
11632
11633 elsif not Is_OK_Static_Expression (E) then
11634 Flag_Non_Static_Expr
11635 ("non-static expression used for delta value!", E);
11636
11637 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11638 Error_Msg_N ("delta expression must be positive", E);
11639
11640 else
11641 return;
11642 end if;
11643
11644 -- If any of above errors occurred, then replace the incorrect
11645 -- expression by the real 0.1, which should prevent further errors.
11646
11647 Rewrite (E,
11648 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11649 Analyze_And_Resolve (E, Standard_Float);
11650 end Check_Delta_Expression;
11651
11652 -----------------------------
11653 -- Check_Digits_Expression --
11654 -----------------------------
11655
11656 procedure Check_Digits_Expression (E : Node_Id) is
11657 begin
11658 if not (Is_Integer_Type (Etype (E))) then
11659 Wrong_Type (E, Any_Integer);
11660
11661 elsif not Is_OK_Static_Expression (E) then
11662 Flag_Non_Static_Expr
11663 ("non-static expression used for digits value!", E);
11664
11665 elsif Expr_Value (E) <= 0 then
11666 Error_Msg_N ("digits value must be greater than zero", E);
11667
11668 else
11669 return;
11670 end if;
11671
11672 -- If any of above errors occurred, then replace the incorrect
11673 -- expression by the integer 1, which should prevent further errors.
11674
11675 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11676 Analyze_And_Resolve (E, Standard_Integer);
11677
11678 end Check_Digits_Expression;
11679
11680 --------------------------
11681 -- Check_Initialization --
11682 --------------------------
11683
11684 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11685 begin
11686 -- Special processing for limited types
11687
11688 if Is_Limited_Type (T)
11689 and then not In_Instance
11690 and then not In_Inlined_Body
11691 then
11692 if not OK_For_Limited_Init (T, Exp) then
11693
11694 -- In GNAT mode, this is just a warning, to allow it to be evilly
11695 -- turned off. Otherwise it is a real error.
11696
11697 if GNAT_Mode then
11698 Error_Msg_N
11699 ("??cannot initialize entities of limited type!", Exp);
11700
11701 elsif Ada_Version < Ada_2005 then
11702
11703 -- The side effect removal machinery may generate illegal Ada
11704 -- code to avoid the usage of access types and 'reference in
11705 -- SPARK mode. Since this is legal code with respect to theorem
11706 -- proving, do not emit the error.
11707
11708 if GNATprove_Mode
11709 and then Nkind (Exp) = N_Function_Call
11710 and then Nkind (Parent (Exp)) = N_Object_Declaration
11711 and then not Comes_From_Source
11712 (Defining_Identifier (Parent (Exp)))
11713 then
11714 null;
11715
11716 else
11717 Error_Msg_N
11718 ("cannot initialize entities of limited type", Exp);
11719 Explain_Limited_Type (T, Exp);
11720 end if;
11721
11722 else
11723 -- Specialize error message according to kind of illegal
11724 -- initial expression. We check the Original_Node to cover
11725 -- cases where the initialization expression of an object
11726 -- declaration generated by the compiler has been rewritten
11727 -- (such as for dispatching calls).
11728
11729 if Nkind (Original_Node (Exp)) = N_Type_Conversion
11730 and then
11731 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
11732 then
11733 -- No error for internally-generated object declarations,
11734 -- which can come from build-in-place assignment statements.
11735
11736 if Nkind (Parent (Exp)) = N_Object_Declaration
11737 and then not Comes_From_Source
11738 (Defining_Identifier (Parent (Exp)))
11739 then
11740 null;
11741
11742 else
11743 Error_Msg_N
11744 ("illegal context for call to function with limited "
11745 & "result", Exp);
11746 end if;
11747
11748 else
11749 Error_Msg_N
11750 ("initialization of limited object requires aggregate or "
11751 & "function call", Exp);
11752 end if;
11753 end if;
11754 end if;
11755 end if;
11756
11757 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11758 -- set unless we can be sure that no range check is required.
11759
11760 if (GNATprove_Mode or not Expander_Active)
11761 and then Is_Scalar_Type (T)
11762 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11763 then
11764 Set_Do_Range_Check (Exp);
11765 end if;
11766 end Check_Initialization;
11767
11768 ----------------------
11769 -- Check_Interfaces --
11770 ----------------------
11771
11772 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11773 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11774
11775 Iface : Node_Id;
11776 Iface_Def : Node_Id;
11777 Iface_Typ : Entity_Id;
11778 Parent_Node : Node_Id;
11779
11780 Is_Task : Boolean := False;
11781 -- Set True if parent type or any progenitor is a task interface
11782
11783 Is_Protected : Boolean := False;
11784 -- Set True if parent type or any progenitor is a protected interface
11785
11786 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11787 -- Check that a progenitor is compatible with declaration. If an error
11788 -- message is output, it is posted on Error_Node.
11789
11790 ------------------
11791 -- Check_Ifaces --
11792 ------------------
11793
11794 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11795 Iface_Id : constant Entity_Id :=
11796 Defining_Identifier (Parent (Iface_Def));
11797 Type_Def : Node_Id;
11798
11799 begin
11800 if Nkind (N) = N_Private_Extension_Declaration then
11801 Type_Def := N;
11802 else
11803 Type_Def := Type_Definition (N);
11804 end if;
11805
11806 if Is_Task_Interface (Iface_Id) then
11807 Is_Task := True;
11808
11809 elsif Is_Protected_Interface (Iface_Id) then
11810 Is_Protected := True;
11811 end if;
11812
11813 if Is_Synchronized_Interface (Iface_Id) then
11814
11815 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11816 -- extension derived from a synchronized interface must explicitly
11817 -- be declared synchronized, because the full view will be a
11818 -- synchronized type.
11819
11820 if Nkind (N) = N_Private_Extension_Declaration then
11821 if not Synchronized_Present (N) then
11822 Error_Msg_NE
11823 ("private extension of& must be explicitly synchronized",
11824 N, Iface_Id);
11825 end if;
11826
11827 -- However, by 3.9.4(16/2), a full type that is a record extension
11828 -- is never allowed to derive from a synchronized interface (note
11829 -- that interfaces must be excluded from this check, because those
11830 -- are represented by derived type definitions in some cases).
11831
11832 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11833 and then not Interface_Present (Type_Definition (N))
11834 then
11835 Error_Msg_N ("record extension cannot derive from synchronized "
11836 & "interface", Error_Node);
11837 end if;
11838 end if;
11839
11840 -- Check that the characteristics of the progenitor are compatible
11841 -- with the explicit qualifier in the declaration.
11842 -- The check only applies to qualifiers that come from source.
11843 -- Limited_Present also appears in the declaration of corresponding
11844 -- records, and the check does not apply to them.
11845
11846 if Limited_Present (Type_Def)
11847 and then not
11848 Is_Concurrent_Record_Type (Defining_Identifier (N))
11849 then
11850 if Is_Limited_Interface (Parent_Type)
11851 and then not Is_Limited_Interface (Iface_Id)
11852 then
11853 Error_Msg_NE
11854 ("progenitor & must be limited interface",
11855 Error_Node, Iface_Id);
11856
11857 elsif
11858 (Task_Present (Iface_Def)
11859 or else Protected_Present (Iface_Def)
11860 or else Synchronized_Present (Iface_Def))
11861 and then Nkind (N) /= N_Private_Extension_Declaration
11862 and then not Error_Posted (N)
11863 then
11864 Error_Msg_NE
11865 ("progenitor & must be limited interface",
11866 Error_Node, Iface_Id);
11867 end if;
11868
11869 -- Protected interfaces can only inherit from limited, synchronized
11870 -- or protected interfaces.
11871
11872 elsif Nkind (N) = N_Full_Type_Declaration
11873 and then Protected_Present (Type_Def)
11874 then
11875 if Limited_Present (Iface_Def)
11876 or else Synchronized_Present (Iface_Def)
11877 or else Protected_Present (Iface_Def)
11878 then
11879 null;
11880
11881 elsif Task_Present (Iface_Def) then
11882 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11883 & "from task interface", Error_Node);
11884
11885 else
11886 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11887 & "from non-limited interface", Error_Node);
11888 end if;
11889
11890 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11891 -- limited and synchronized.
11892
11893 elsif Synchronized_Present (Type_Def) then
11894 if Limited_Present (Iface_Def)
11895 or else Synchronized_Present (Iface_Def)
11896 then
11897 null;
11898
11899 elsif Protected_Present (Iface_Def)
11900 and then Nkind (N) /= N_Private_Extension_Declaration
11901 then
11902 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11903 & "from protected interface", Error_Node);
11904
11905 elsif Task_Present (Iface_Def)
11906 and then Nkind (N) /= N_Private_Extension_Declaration
11907 then
11908 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11909 & "from task interface", Error_Node);
11910
11911 elsif not Is_Limited_Interface (Iface_Id) then
11912 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11913 & "from non-limited interface", Error_Node);
11914 end if;
11915
11916 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11917 -- synchronized or task interfaces.
11918
11919 elsif Nkind (N) = N_Full_Type_Declaration
11920 and then Task_Present (Type_Def)
11921 then
11922 if Limited_Present (Iface_Def)
11923 or else Synchronized_Present (Iface_Def)
11924 or else Task_Present (Iface_Def)
11925 then
11926 null;
11927
11928 elsif Protected_Present (Iface_Def) then
11929 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11930 & "protected interface", Error_Node);
11931
11932 else
11933 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11934 & "non-limited interface", Error_Node);
11935 end if;
11936 end if;
11937 end Check_Ifaces;
11938
11939 -- Start of processing for Check_Interfaces
11940
11941 begin
11942 if Is_Interface (Parent_Type) then
11943 if Is_Task_Interface (Parent_Type) then
11944 Is_Task := True;
11945
11946 elsif Is_Protected_Interface (Parent_Type) then
11947 Is_Protected := True;
11948 end if;
11949 end if;
11950
11951 if Nkind (N) = N_Private_Extension_Declaration then
11952
11953 -- Check that progenitors are compatible with declaration
11954
11955 Iface := First (Interface_List (Def));
11956 while Present (Iface) loop
11957 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11958
11959 Parent_Node := Parent (Base_Type (Iface_Typ));
11960 Iface_Def := Type_Definition (Parent_Node);
11961
11962 if not Is_Interface (Iface_Typ) then
11963 Diagnose_Interface (Iface, Iface_Typ);
11964 else
11965 Check_Ifaces (Iface_Def, Iface);
11966 end if;
11967
11968 Next (Iface);
11969 end loop;
11970
11971 if Is_Task and Is_Protected then
11972 Error_Msg_N
11973 ("type cannot derive from task and protected interface", N);
11974 end if;
11975
11976 return;
11977 end if;
11978
11979 -- Full type declaration of derived type.
11980 -- Check compatibility with parent if it is interface type
11981
11982 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11983 and then Is_Interface (Parent_Type)
11984 then
11985 Parent_Node := Parent (Parent_Type);
11986
11987 -- More detailed checks for interface varieties
11988
11989 Check_Ifaces
11990 (Iface_Def => Type_Definition (Parent_Node),
11991 Error_Node => Subtype_Indication (Type_Definition (N)));
11992 end if;
11993
11994 Iface := First (Interface_List (Def));
11995 while Present (Iface) loop
11996 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11997
11998 Parent_Node := Parent (Base_Type (Iface_Typ));
11999 Iface_Def := Type_Definition (Parent_Node);
12000
12001 if not Is_Interface (Iface_Typ) then
12002 Diagnose_Interface (Iface, Iface_Typ);
12003
12004 else
12005 -- "The declaration of a specific descendant of an interface
12006 -- type freezes the interface type" RM 13.14
12007
12008 Freeze_Before (N, Iface_Typ);
12009 Check_Ifaces (Iface_Def, Error_Node => Iface);
12010 end if;
12011
12012 Next (Iface);
12013 end loop;
12014
12015 if Is_Task and Is_Protected then
12016 Error_Msg_N
12017 ("type cannot derive from task and protected interface", N);
12018 end if;
12019 end Check_Interfaces;
12020
12021 ------------------------------------
12022 -- Check_Or_Process_Discriminants --
12023 ------------------------------------
12024
12025 -- If an incomplete or private type declaration was already given for the
12026 -- type, the discriminants may have already been processed if they were
12027 -- present on the incomplete declaration. In this case a full conformance
12028 -- check has been performed in Find_Type_Name, and we then recheck here
12029 -- some properties that can't be checked on the partial view alone.
12030 -- Otherwise we call Process_Discriminants.
12031
12032 procedure Check_Or_Process_Discriminants
12033 (N : Node_Id;
12034 T : Entity_Id;
12035 Prev : Entity_Id := Empty)
12036 is
12037 begin
12038 if Has_Discriminants (T) then
12039
12040 -- Discriminants are already set on T if they were already present
12041 -- on the partial view. Make them visible to component declarations.
12042
12043 declare
12044 D : Entity_Id;
12045 -- Discriminant on T (full view) referencing expr on partial view
12046
12047 Prev_D : Entity_Id;
12048 -- Entity of corresponding discriminant on partial view
12049
12050 New_D : Node_Id;
12051 -- Discriminant specification for full view, expression is
12052 -- the syntactic copy on full view (which has been checked for
12053 -- conformance with partial view), only used here to post error
12054 -- message.
12055
12056 begin
12057 D := First_Discriminant (T);
12058 New_D := First (Discriminant_Specifications (N));
12059 while Present (D) loop
12060 Prev_D := Current_Entity (D);
12061 Set_Current_Entity (D);
12062 Set_Is_Immediately_Visible (D);
12063 Set_Homonym (D, Prev_D);
12064
12065 -- Handle the case where there is an untagged partial view and
12066 -- the full view is tagged: must disallow discriminants with
12067 -- defaults, unless compiling for Ada 2012, which allows a
12068 -- limited tagged type to have defaulted discriminants (see
12069 -- AI05-0214). However, suppress error here if it was already
12070 -- reported on the default expression of the partial view.
12071
12072 if Is_Tagged_Type (T)
12073 and then Present (Expression (Parent (D)))
12074 and then (not Is_Limited_Type (Current_Scope)
12075 or else Ada_Version < Ada_2012)
12076 and then not Error_Posted (Expression (Parent (D)))
12077 then
12078 if Ada_Version >= Ada_2012 then
12079 Error_Msg_N
12080 ("discriminants of nonlimited tagged type cannot have "
12081 & "defaults",
12082 Expression (New_D));
12083 else
12084 Error_Msg_N
12085 ("discriminants of tagged type cannot have defaults",
12086 Expression (New_D));
12087 end if;
12088 end if;
12089
12090 -- Ada 2005 (AI-230): Access discriminant allowed in
12091 -- non-limited record types.
12092
12093 if Ada_Version < Ada_2005 then
12094
12095 -- This restriction gets applied to the full type here. It
12096 -- has already been applied earlier to the partial view.
12097
12098 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12099 end if;
12100
12101 Next_Discriminant (D);
12102 Next (New_D);
12103 end loop;
12104 end;
12105
12106 elsif Present (Discriminant_Specifications (N)) then
12107 Process_Discriminants (N, Prev);
12108 end if;
12109 end Check_Or_Process_Discriminants;
12110
12111 ----------------------
12112 -- Check_Real_Bound --
12113 ----------------------
12114
12115 procedure Check_Real_Bound (Bound : Node_Id) is
12116 begin
12117 if not Is_Real_Type (Etype (Bound)) then
12118 Error_Msg_N
12119 ("bound in real type definition must be of real type", Bound);
12120
12121 elsif not Is_OK_Static_Expression (Bound) then
12122 Flag_Non_Static_Expr
12123 ("non-static expression used for real type bound!", Bound);
12124
12125 else
12126 return;
12127 end if;
12128
12129 Rewrite
12130 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12131 Analyze (Bound);
12132 Resolve (Bound, Standard_Float);
12133 end Check_Real_Bound;
12134
12135 ------------------------------
12136 -- Complete_Private_Subtype --
12137 ------------------------------
12138
12139 procedure Complete_Private_Subtype
12140 (Priv : Entity_Id;
12141 Full : Entity_Id;
12142 Full_Base : Entity_Id;
12143 Related_Nod : Node_Id)
12144 is
12145 Save_Next_Entity : Entity_Id;
12146 Save_Homonym : Entity_Id;
12147
12148 begin
12149 -- Set semantic attributes for (implicit) private subtype completion.
12150 -- If the full type has no discriminants, then it is a copy of the
12151 -- full view of the base. Otherwise, it is a subtype of the base with
12152 -- a possible discriminant constraint. Save and restore the original
12153 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12154 -- not corrupt the entity chain.
12155
12156 Save_Next_Entity := Next_Entity (Full);
12157 Save_Homonym := Homonym (Priv);
12158
12159 if Is_Private_Type (Full_Base)
12160 or else Is_Record_Type (Full_Base)
12161 or else Is_Concurrent_Type (Full_Base)
12162 then
12163 Copy_Node (Priv, Full);
12164
12165 -- Note that the Etype of the full view is the same as the Etype of
12166 -- the partial view. In this fashion, the subtype has access to the
12167 -- correct view of the parent.
12168
12169 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12170 Set_Has_Unknown_Discriminants
12171 (Full, Has_Unknown_Discriminants (Full_Base));
12172 Set_First_Entity (Full, First_Entity (Full_Base));
12173 Set_Last_Entity (Full, Last_Entity (Full_Base));
12174
12175 -- If the underlying base type is constrained, we know that the
12176 -- full view of the subtype is constrained as well (the converse
12177 -- is not necessarily true).
12178
12179 if Is_Constrained (Full_Base) then
12180 Set_Is_Constrained (Full);
12181 end if;
12182
12183 else
12184 Copy_Node (Full_Base, Full);
12185
12186 -- The following subtlety with the Etype of the full view needs to be
12187 -- taken into account here. One could think that it must naturally be
12188 -- set to the base type of the full base:
12189
12190 -- Set_Etype (Full, Base_Type (Full_Base));
12191
12192 -- so that the full view becomes a subtype of the full base when the
12193 -- latter is a base type, which must for example happen when the full
12194 -- base is declared as derived type. That's also correct if the full
12195 -- base is declared as an array type, or a floating-point type, or a
12196 -- fixed-point type, or a signed integer type, as these declarations
12197 -- create an implicit base type and a first subtype so the Etype of
12198 -- the full views must be the implicit base type. But that's wrong
12199 -- if the full base is declared as an access type, or an enumeration
12200 -- type, or a modular integer type, as these declarations directly
12201 -- create a base type, i.e. with Etype pointing to itself. Moreover
12202 -- the full base being declared in the private part, i.e. when the
12203 -- views are swapped, the end result is that the Etype of the full
12204 -- base is set to its private view in this case and that we need to
12205 -- propagate this setting to the full view in order for the subtype
12206 -- to be compatible with the base type.
12207
12208 if Is_Base_Type (Full_Base)
12209 and then (Is_Derived_Type (Full_Base)
12210 or else Ekind (Full_Base) in Array_Kind
12211 or else Ekind (Full_Base) in Fixed_Point_Kind
12212 or else Ekind (Full_Base) in Float_Kind
12213 or else Ekind (Full_Base) in Signed_Integer_Kind)
12214 then
12215 Set_Etype (Full, Full_Base);
12216 end if;
12217
12218 Set_Chars (Full, Chars (Priv));
12219 Set_Sloc (Full, Sloc (Priv));
12220 Conditional_Delay (Full, Priv);
12221 end if;
12222
12223 Link_Entities (Full, Save_Next_Entity);
12224 Set_Homonym (Full, Save_Homonym);
12225 Set_Associated_Node_For_Itype (Full, Related_Nod);
12226
12227 -- Set common attributes for all subtypes: kind, convention, etc.
12228
12229 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12230 Set_Convention (Full, Convention (Full_Base));
12231 Set_Is_First_Subtype (Full, False);
12232 Set_Scope (Full, Scope (Priv));
12233 Set_Size_Info (Full, Full_Base);
12234 Set_RM_Size (Full, RM_Size (Full_Base));
12235 Set_Is_Itype (Full);
12236
12237 -- A subtype of a private-type-without-discriminants, whose full-view
12238 -- has discriminants with default expressions, is not constrained.
12239
12240 if not Has_Discriminants (Priv) then
12241 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12242
12243 if Has_Discriminants (Full_Base) then
12244 Set_Discriminant_Constraint
12245 (Full, Discriminant_Constraint (Full_Base));
12246
12247 -- The partial view may have been indefinite, the full view
12248 -- might not be.
12249
12250 Set_Has_Unknown_Discriminants
12251 (Full, Has_Unknown_Discriminants (Full_Base));
12252 end if;
12253 end if;
12254
12255 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12256 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12257
12258 -- Freeze the private subtype entity if its parent is delayed, and not
12259 -- already frozen. We skip this processing if the type is an anonymous
12260 -- subtype of a record component, or is the corresponding record of a
12261 -- protected type, since these are processed when the enclosing type
12262 -- is frozen. If the parent type is declared in a nested package then
12263 -- the freezing of the private and full views also happens later.
12264
12265 if not Is_Type (Scope (Full)) then
12266 if Is_Itype (Priv)
12267 and then In_Same_Source_Unit (Full, Full_Base)
12268 and then Scope (Full_Base) /= Scope (Full)
12269 then
12270 Set_Has_Delayed_Freeze (Full);
12271 Set_Has_Delayed_Freeze (Priv);
12272
12273 else
12274 Set_Has_Delayed_Freeze (Full,
12275 Has_Delayed_Freeze (Full_Base)
12276 and then not Is_Frozen (Full_Base));
12277 end if;
12278 end if;
12279
12280 Set_Freeze_Node (Full, Empty);
12281 Set_Is_Frozen (Full, False);
12282
12283 if Has_Discriminants (Full) then
12284 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12285 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12286
12287 if Has_Unknown_Discriminants (Full) then
12288 Set_Discriminant_Constraint (Full, No_Elist);
12289 end if;
12290 end if;
12291
12292 if Ekind (Full_Base) = E_Record_Type
12293 and then Has_Discriminants (Full_Base)
12294 and then Has_Discriminants (Priv) -- might not, if errors
12295 and then not Has_Unknown_Discriminants (Priv)
12296 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12297 then
12298 Create_Constrained_Components
12299 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12300
12301 -- If the full base is itself derived from private, build a congruent
12302 -- subtype of its underlying full view, for use by the back end.
12303
12304 elsif Is_Private_Type (Full_Base)
12305 and then Present (Underlying_Full_View (Full_Base))
12306 then
12307 declare
12308 Underlying_Full_Base : constant Entity_Id
12309 := Underlying_Full_View (Full_Base);
12310 Underlying_Full : constant Entity_Id
12311 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12312 begin
12313 Set_Is_Itype (Underlying_Full);
12314 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12315 Complete_Private_Subtype
12316 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12317 Set_Underlying_Full_View (Full, Underlying_Full);
12318 Set_Is_Underlying_Full_View (Underlying_Full);
12319 end;
12320
12321 elsif Is_Record_Type (Full_Base) then
12322
12323 -- Show Full is simply a renaming of Full_Base
12324
12325 Set_Cloned_Subtype (Full, Full_Base);
12326
12327 -- Propagate predicates
12328
12329 Propagate_Predicate_Attributes (Full, Full_Base);
12330 end if;
12331
12332 -- It is unsafe to share the bounds of a scalar type, because the Itype
12333 -- is elaborated on demand, and if a bound is nonstatic, then different
12334 -- orders of elaboration in different units will lead to different
12335 -- external symbols.
12336
12337 if Is_Scalar_Type (Full_Base) then
12338 Set_Scalar_Range (Full,
12339 Make_Range (Sloc (Related_Nod),
12340 Low_Bound =>
12341 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12342 High_Bound =>
12343 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12344
12345 -- This completion inherits the bounds of the full parent, but if
12346 -- the parent is an unconstrained floating point type, so is the
12347 -- completion.
12348
12349 if Is_Floating_Point_Type (Full_Base) then
12350 Set_Includes_Infinities
12351 (Scalar_Range (Full), Has_Infinities (Full_Base));
12352 end if;
12353 end if;
12354
12355 -- ??? It seems that a lot of fields are missing that should be copied
12356 -- from Full_Base to Full. Here are some that are introduced in a
12357 -- non-disruptive way but a cleanup is necessary.
12358
12359 if Is_Tagged_Type (Full_Base) then
12360 Set_Is_Tagged_Type (Full);
12361 Set_Direct_Primitive_Operations
12362 (Full, Direct_Primitive_Operations (Full_Base));
12363 Set_No_Tagged_Streams_Pragma
12364 (Full, No_Tagged_Streams_Pragma (Full_Base));
12365
12366 -- Inherit class_wide type of full_base in case the partial view was
12367 -- not tagged. Otherwise it has already been created when the private
12368 -- subtype was analyzed.
12369
12370 if No (Class_Wide_Type (Full)) then
12371 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12372 end if;
12373
12374 -- If this is a subtype of a protected or task type, constrain its
12375 -- corresponding record, unless this is a subtype without constraints,
12376 -- i.e. a simple renaming as with an actual subtype in an instance.
12377
12378 elsif Is_Concurrent_Type (Full_Base) then
12379 if Has_Discriminants (Full)
12380 and then Present (Corresponding_Record_Type (Full_Base))
12381 and then
12382 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12383 then
12384 Set_Corresponding_Record_Type (Full,
12385 Constrain_Corresponding_Record
12386 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12387
12388 else
12389 Set_Corresponding_Record_Type (Full,
12390 Corresponding_Record_Type (Full_Base));
12391 end if;
12392 end if;
12393
12394 -- Link rep item chain, and also setting of Has_Predicates from private
12395 -- subtype to full subtype, since we will need these on the full subtype
12396 -- to create the predicate function. Note that the full subtype may
12397 -- already have rep items, inherited from the full view of the base
12398 -- type, so we must be sure not to overwrite these entries.
12399
12400 declare
12401 Append : Boolean;
12402 Item : Node_Id;
12403 Next_Item : Node_Id;
12404 Priv_Item : Node_Id;
12405
12406 begin
12407 Item := First_Rep_Item (Full);
12408 Priv_Item := First_Rep_Item (Priv);
12409
12410 -- If no existing rep items on full type, we can just link directly
12411 -- to the list of items on the private type, if any exist.. Same if
12412 -- the rep items are only those inherited from the base
12413
12414 if (No (Item)
12415 or else Nkind (Item) /= N_Aspect_Specification
12416 or else Entity (Item) = Full_Base)
12417 and then Present (First_Rep_Item (Priv))
12418 then
12419 Set_First_Rep_Item (Full, Priv_Item);
12420
12421 -- Otherwise, search to the end of items currently linked to the full
12422 -- subtype and append the private items to the end. However, if Priv
12423 -- and Full already have the same list of rep items, then the append
12424 -- is not done, as that would create a circularity.
12425 --
12426 -- The partial view may have a predicate and the rep item lists of
12427 -- both views agree when inherited from the same ancestor. In that
12428 -- case, simply propagate the list from one view to the other.
12429 -- A more complex analysis needed here ???
12430
12431 elsif Present (Priv_Item)
12432 and then Item = Next_Rep_Item (Priv_Item)
12433 then
12434 Set_First_Rep_Item (Full, Priv_Item);
12435
12436 elsif Item /= Priv_Item then
12437 Append := True;
12438 loop
12439 Next_Item := Next_Rep_Item (Item);
12440 exit when No (Next_Item);
12441 Item := Next_Item;
12442
12443 -- If the private view has aspect specifications, the full view
12444 -- inherits them. Since these aspects may already have been
12445 -- attached to the full view during derivation, do not append
12446 -- them if already present.
12447
12448 if Item = First_Rep_Item (Priv) then
12449 Append := False;
12450 exit;
12451 end if;
12452 end loop;
12453
12454 -- And link the private type items at the end of the chain
12455
12456 if Append then
12457 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12458 end if;
12459 end if;
12460 end;
12461
12462 -- Make sure Has_Predicates is set on full type if it is set on the
12463 -- private type. Note that it may already be set on the full type and
12464 -- if so, we don't want to unset it. Similarly, propagate information
12465 -- about delayed aspects, because the corresponding pragmas must be
12466 -- analyzed when one of the views is frozen. This last step is needed
12467 -- in particular when the full type is a scalar type for which an
12468 -- anonymous base type is constructed.
12469
12470 -- The predicate functions are generated either at the freeze point
12471 -- of the type or at the end of the visible part, and we must avoid
12472 -- generating them twice.
12473
12474 Propagate_Predicate_Attributes (Full, Priv);
12475
12476 if Has_Delayed_Aspects (Priv) then
12477 Set_Has_Delayed_Aspects (Full);
12478 end if;
12479 end Complete_Private_Subtype;
12480
12481 ----------------------------
12482 -- Constant_Redeclaration --
12483 ----------------------------
12484
12485 procedure Constant_Redeclaration
12486 (Id : Entity_Id;
12487 N : Node_Id;
12488 T : out Entity_Id)
12489 is
12490 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12491 Obj_Def : constant Node_Id := Object_Definition (N);
12492 New_T : Entity_Id;
12493
12494 procedure Check_Possible_Deferred_Completion
12495 (Prev_Id : Entity_Id;
12496 Prev_Obj_Def : Node_Id;
12497 Curr_Obj_Def : Node_Id);
12498 -- Determine whether the two object definitions describe the partial
12499 -- and the full view of a constrained deferred constant. Generate
12500 -- a subtype for the full view and verify that it statically matches
12501 -- the subtype of the partial view.
12502
12503 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12504 -- If deferred constant is an access type initialized with an allocator,
12505 -- check whether there is an illegal recursion in the definition,
12506 -- through a default value of some record subcomponent. This is normally
12507 -- detected when generating init procs, but requires this additional
12508 -- mechanism when expansion is disabled.
12509
12510 ----------------------------------------
12511 -- Check_Possible_Deferred_Completion --
12512 ----------------------------------------
12513
12514 procedure Check_Possible_Deferred_Completion
12515 (Prev_Id : Entity_Id;
12516 Prev_Obj_Def : Node_Id;
12517 Curr_Obj_Def : Node_Id)
12518 is
12519 begin
12520 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12521 and then Present (Constraint (Prev_Obj_Def))
12522 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12523 and then Present (Constraint (Curr_Obj_Def))
12524 then
12525 declare
12526 Loc : constant Source_Ptr := Sloc (N);
12527 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12528 Decl : constant Node_Id :=
12529 Make_Subtype_Declaration (Loc,
12530 Defining_Identifier => Def_Id,
12531 Subtype_Indication =>
12532 Relocate_Node (Curr_Obj_Def));
12533
12534 begin
12535 Insert_Before_And_Analyze (N, Decl);
12536 Set_Etype (Id, Def_Id);
12537
12538 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12539 Error_Msg_Sloc := Sloc (Prev_Id);
12540 Error_Msg_N ("subtype does not statically match deferred "
12541 & "declaration #", N);
12542 end if;
12543 end;
12544 end if;
12545 end Check_Possible_Deferred_Completion;
12546
12547 ---------------------------------
12548 -- Check_Recursive_Declaration --
12549 ---------------------------------
12550
12551 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12552 Comp : Entity_Id;
12553
12554 begin
12555 if Is_Record_Type (Typ) then
12556 Comp := First_Component (Typ);
12557 while Present (Comp) loop
12558 if Comes_From_Source (Comp) then
12559 if Present (Expression (Parent (Comp)))
12560 and then Is_Entity_Name (Expression (Parent (Comp)))
12561 and then Entity (Expression (Parent (Comp))) = Prev
12562 then
12563 Error_Msg_Sloc := Sloc (Parent (Comp));
12564 Error_Msg_NE
12565 ("illegal circularity with declaration for & #",
12566 N, Comp);
12567 return;
12568
12569 elsif Is_Record_Type (Etype (Comp)) then
12570 Check_Recursive_Declaration (Etype (Comp));
12571 end if;
12572 end if;
12573
12574 Next_Component (Comp);
12575 end loop;
12576 end if;
12577 end Check_Recursive_Declaration;
12578
12579 -- Start of processing for Constant_Redeclaration
12580
12581 begin
12582 if Nkind (Parent (Prev)) = N_Object_Declaration then
12583 if Nkind (Object_Definition
12584 (Parent (Prev))) = N_Subtype_Indication
12585 then
12586 -- Find type of new declaration. The constraints of the two
12587 -- views must match statically, but there is no point in
12588 -- creating an itype for the full view.
12589
12590 if Nkind (Obj_Def) = N_Subtype_Indication then
12591 Find_Type (Subtype_Mark (Obj_Def));
12592 New_T := Entity (Subtype_Mark (Obj_Def));
12593
12594 else
12595 Find_Type (Obj_Def);
12596 New_T := Entity (Obj_Def);
12597 end if;
12598
12599 T := Etype (Prev);
12600
12601 else
12602 -- The full view may impose a constraint, even if the partial
12603 -- view does not, so construct the subtype.
12604
12605 New_T := Find_Type_Of_Object (Obj_Def, N);
12606 T := New_T;
12607 end if;
12608
12609 else
12610 -- Current declaration is illegal, diagnosed below in Enter_Name
12611
12612 T := Empty;
12613 New_T := Any_Type;
12614 end if;
12615
12616 -- If previous full declaration or a renaming declaration exists, or if
12617 -- a homograph is present, let Enter_Name handle it, either with an
12618 -- error or with the removal of an overridden implicit subprogram.
12619 -- The previous one is a full declaration if it has an expression
12620 -- (which in the case of an aggregate is indicated by the Init flag).
12621
12622 if Ekind (Prev) /= E_Constant
12623 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12624 or else Present (Expression (Parent (Prev)))
12625 or else Has_Init_Expression (Parent (Prev))
12626 or else Present (Full_View (Prev))
12627 then
12628 Enter_Name (Id);
12629
12630 -- Verify that types of both declarations match, or else that both types
12631 -- are anonymous access types whose designated subtypes statically match
12632 -- (as allowed in Ada 2005 by AI-385).
12633
12634 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12635 and then
12636 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12637 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12638 or else Is_Access_Constant (Etype (New_T)) /=
12639 Is_Access_Constant (Etype (Prev))
12640 or else Can_Never_Be_Null (Etype (New_T)) /=
12641 Can_Never_Be_Null (Etype (Prev))
12642 or else Null_Exclusion_Present (Parent (Prev)) /=
12643 Null_Exclusion_Present (Parent (Id))
12644 or else not Subtypes_Statically_Match
12645 (Designated_Type (Etype (Prev)),
12646 Designated_Type (Etype (New_T))))
12647 then
12648 Error_Msg_Sloc := Sloc (Prev);
12649 Error_Msg_N ("type does not match declaration#", N);
12650 Set_Full_View (Prev, Id);
12651 Set_Etype (Id, Any_Type);
12652
12653 -- A deferred constant whose type is an anonymous array is always
12654 -- illegal (unless imported). A detailed error message might be
12655 -- helpful for Ada beginners.
12656
12657 if Nkind (Object_Definition (Parent (Prev)))
12658 = N_Constrained_Array_Definition
12659 and then Nkind (Object_Definition (N))
12660 = N_Constrained_Array_Definition
12661 then
12662 Error_Msg_N ("\each anonymous array is a distinct type", N);
12663 Error_Msg_N ("a deferred constant must have a named type",
12664 Object_Definition (Parent (Prev)));
12665 end if;
12666
12667 elsif
12668 Null_Exclusion_Present (Parent (Prev))
12669 and then not Null_Exclusion_Present (N)
12670 then
12671 Error_Msg_Sloc := Sloc (Prev);
12672 Error_Msg_N ("null-exclusion does not match declaration#", N);
12673 Set_Full_View (Prev, Id);
12674 Set_Etype (Id, Any_Type);
12675
12676 -- If so, process the full constant declaration
12677
12678 else
12679 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12680 -- the deferred declaration is constrained, then the subtype defined
12681 -- by the subtype_indication in the full declaration shall match it
12682 -- statically.
12683
12684 Check_Possible_Deferred_Completion
12685 (Prev_Id => Prev,
12686 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12687 Curr_Obj_Def => Obj_Def);
12688
12689 Set_Full_View (Prev, Id);
12690 Set_Is_Public (Id, Is_Public (Prev));
12691 Set_Is_Internal (Id);
12692 Append_Entity (Id, Current_Scope);
12693
12694 -- Check ALIASED present if present before (RM 7.4(7))
12695
12696 if Is_Aliased (Prev)
12697 and then not Aliased_Present (N)
12698 then
12699 Error_Msg_Sloc := Sloc (Prev);
12700 Error_Msg_N ("ALIASED required (see declaration #)", N);
12701 end if;
12702
12703 -- Check that placement is in private part and that the incomplete
12704 -- declaration appeared in the visible part.
12705
12706 if Ekind (Current_Scope) = E_Package
12707 and then not In_Private_Part (Current_Scope)
12708 then
12709 Error_Msg_Sloc := Sloc (Prev);
12710 Error_Msg_N
12711 ("full constant for declaration # must be in private part", N);
12712
12713 elsif Ekind (Current_Scope) = E_Package
12714 and then
12715 List_Containing (Parent (Prev)) /=
12716 Visible_Declarations (Package_Specification (Current_Scope))
12717 then
12718 Error_Msg_N
12719 ("deferred constant must be declared in visible part",
12720 Parent (Prev));
12721 end if;
12722
12723 if Is_Access_Type (T)
12724 and then Nkind (Expression (N)) = N_Allocator
12725 then
12726 Check_Recursive_Declaration (Designated_Type (T));
12727 end if;
12728
12729 -- A deferred constant is a visible entity. If type has invariants,
12730 -- verify that the initial value satisfies them. This is not done in
12731 -- GNATprove mode, as GNATprove handles invariant checks itself.
12732
12733 if Has_Invariants (T)
12734 and then Present (Invariant_Procedure (T))
12735 and then not GNATprove_Mode
12736 then
12737 Insert_After (N,
12738 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12739 end if;
12740 end if;
12741 end Constant_Redeclaration;
12742
12743 ----------------------
12744 -- Constrain_Access --
12745 ----------------------
12746
12747 procedure Constrain_Access
12748 (Def_Id : in out Entity_Id;
12749 S : Node_Id;
12750 Related_Nod : Node_Id)
12751 is
12752 T : constant Entity_Id := Entity (Subtype_Mark (S));
12753 Desig_Type : constant Entity_Id := Designated_Type (T);
12754 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12755 Constraint_OK : Boolean := True;
12756
12757 begin
12758 if Is_Array_Type (Desig_Type) then
12759 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12760
12761 elsif (Is_Record_Type (Desig_Type)
12762 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12763 and then not Is_Constrained (Desig_Type)
12764 then
12765 -- If this is a constrained access definition for a record
12766 -- component, we leave the type as an unconstrained access,
12767 -- and mark the component so that its actual type is built
12768 -- at a point of use (e.g., an assignment statement). This
12769 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
12770
12771 if Desig_Type = Current_Scope
12772 and then No (Def_Id)
12773 then
12774 Desig_Subtype :=
12775 Create_Itype
12776 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
12777 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12778 Def_Id := Entity (Subtype_Mark (S));
12779
12780 -- We indicate that the component has a per-object constraint
12781 -- for treatment at a point of use, even though the constraint
12782 -- may be independent of discriminants of the enclosing type.
12783
12784 if Nkind (Related_Nod) = N_Component_Declaration then
12785 Set_Has_Per_Object_Constraint
12786 (Defining_Identifier (Related_Nod));
12787 end if;
12788
12789 -- This call added to ensure that the constraint is analyzed
12790 -- (needed for a B test). Note that we still return early from
12791 -- this procedure to avoid recursive processing.
12792
12793 Constrain_Discriminated_Type
12794 (Desig_Subtype, S, Related_Nod, For_Access => True);
12795 return;
12796 end if;
12797
12798 -- Enforce rule that the constraint is illegal if there is an
12799 -- unconstrained view of the designated type. This means that the
12800 -- partial view (either a private type declaration or a derivation
12801 -- from a private type) has no discriminants. (Defect Report
12802 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12803
12804 -- Rule updated for Ada 2005: The private type is said to have
12805 -- a constrained partial view, given that objects of the type
12806 -- can be declared. Furthermore, the rule applies to all access
12807 -- types, unlike the rule concerning default discriminants (see
12808 -- RM 3.7.1(7/3))
12809
12810 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12811 and then Has_Private_Declaration (Desig_Type)
12812 and then In_Open_Scopes (Scope (Desig_Type))
12813 and then Has_Discriminants (Desig_Type)
12814 then
12815 declare
12816 Pack : constant Node_Id :=
12817 Unit_Declaration_Node (Scope (Desig_Type));
12818 Decls : List_Id;
12819 Decl : Node_Id;
12820
12821 begin
12822 if Nkind (Pack) = N_Package_Declaration then
12823 Decls := Visible_Declarations (Specification (Pack));
12824 Decl := First (Decls);
12825 while Present (Decl) loop
12826 if (Nkind (Decl) = N_Private_Type_Declaration
12827 and then Chars (Defining_Identifier (Decl)) =
12828 Chars (Desig_Type))
12829
12830 or else
12831 (Nkind (Decl) = N_Full_Type_Declaration
12832 and then
12833 Chars (Defining_Identifier (Decl)) =
12834 Chars (Desig_Type)
12835 and then Is_Derived_Type (Desig_Type)
12836 and then
12837 Has_Private_Declaration (Etype (Desig_Type)))
12838 then
12839 if No (Discriminant_Specifications (Decl)) then
12840 Error_Msg_N
12841 ("cannot constrain access type if designated "
12842 & "type has constrained partial view", S);
12843 end if;
12844
12845 exit;
12846 end if;
12847
12848 Next (Decl);
12849 end loop;
12850 end if;
12851 end;
12852 end if;
12853
12854 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12855 For_Access => True);
12856
12857 elsif Is_Concurrent_Type (Desig_Type)
12858 and then not Is_Constrained (Desig_Type)
12859 then
12860 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12861
12862 else
12863 Error_Msg_N ("invalid constraint on access type", S);
12864
12865 -- We simply ignore an invalid constraint
12866
12867 Desig_Subtype := Desig_Type;
12868 Constraint_OK := False;
12869 end if;
12870
12871 if No (Def_Id) then
12872 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12873 else
12874 Set_Ekind (Def_Id, E_Access_Subtype);
12875 end if;
12876
12877 if Constraint_OK then
12878 Set_Etype (Def_Id, Base_Type (T));
12879
12880 if Is_Private_Type (Desig_Type) then
12881 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12882 end if;
12883 else
12884 Set_Etype (Def_Id, Any_Type);
12885 end if;
12886
12887 Set_Size_Info (Def_Id, T);
12888 Set_Is_Constrained (Def_Id, Constraint_OK);
12889 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12890 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12891 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12892
12893 Conditional_Delay (Def_Id, T);
12894
12895 -- AI-363 : Subtypes of general access types whose designated types have
12896 -- default discriminants are disallowed. In instances, the rule has to
12897 -- be checked against the actual, of which T is the subtype. In a
12898 -- generic body, the rule is checked assuming that the actual type has
12899 -- defaulted discriminants.
12900
12901 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12902 if Ekind (Base_Type (T)) = E_General_Access_Type
12903 and then Has_Defaulted_Discriminants (Desig_Type)
12904 then
12905 if Ada_Version < Ada_2005 then
12906 Error_Msg_N
12907 ("access subtype of general access type would not " &
12908 "be allowed in Ada 2005?y?", S);
12909 else
12910 Error_Msg_N
12911 ("access subtype of general access type not allowed", S);
12912 end if;
12913
12914 Error_Msg_N ("\discriminants have defaults", S);
12915
12916 elsif Is_Access_Type (T)
12917 and then Is_Generic_Type (Desig_Type)
12918 and then Has_Discriminants (Desig_Type)
12919 and then In_Package_Body (Current_Scope)
12920 then
12921 if Ada_Version < Ada_2005 then
12922 Error_Msg_N
12923 ("access subtype would not be allowed in generic body "
12924 & "in Ada 2005?y?", S);
12925 else
12926 Error_Msg_N
12927 ("access subtype not allowed in generic body", S);
12928 end if;
12929
12930 Error_Msg_N
12931 ("\designated type is a discriminated formal", S);
12932 end if;
12933 end if;
12934 end Constrain_Access;
12935
12936 ---------------------
12937 -- Constrain_Array --
12938 ---------------------
12939
12940 procedure Constrain_Array
12941 (Def_Id : in out Entity_Id;
12942 SI : Node_Id;
12943 Related_Nod : Node_Id;
12944 Related_Id : Entity_Id;
12945 Suffix : Character)
12946 is
12947 C : constant Node_Id := Constraint (SI);
12948 Number_Of_Constraints : Nat := 0;
12949 Index : Node_Id;
12950 S, T : Entity_Id;
12951 Constraint_OK : Boolean := True;
12952
12953 begin
12954 T := Entity (Subtype_Mark (SI));
12955
12956 if Is_Access_Type (T) then
12957 T := Designated_Type (T);
12958 end if;
12959
12960 -- If an index constraint follows a subtype mark in a subtype indication
12961 -- then the type or subtype denoted by the subtype mark must not already
12962 -- impose an index constraint. The subtype mark must denote either an
12963 -- unconstrained array type or an access type whose designated type
12964 -- is such an array type... (RM 3.6.1)
12965
12966 if Is_Constrained (T) then
12967 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12968 Constraint_OK := False;
12969
12970 else
12971 S := First (Constraints (C));
12972 while Present (S) loop
12973 Number_Of_Constraints := Number_Of_Constraints + 1;
12974 Next (S);
12975 end loop;
12976
12977 -- In either case, the index constraint must provide a discrete
12978 -- range for each index of the array type and the type of each
12979 -- discrete range must be the same as that of the corresponding
12980 -- index. (RM 3.6.1)
12981
12982 if Number_Of_Constraints /= Number_Dimensions (T) then
12983 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12984 Constraint_OK := False;
12985
12986 else
12987 S := First (Constraints (C));
12988 Index := First_Index (T);
12989 Analyze (Index);
12990
12991 -- Apply constraints to each index type
12992
12993 for J in 1 .. Number_Of_Constraints loop
12994 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12995 Next (Index);
12996 Next (S);
12997 end loop;
12998
12999 end if;
13000 end if;
13001
13002 if No (Def_Id) then
13003 Def_Id :=
13004 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13005 Set_Parent (Def_Id, Related_Nod);
13006
13007 else
13008 Set_Ekind (Def_Id, E_Array_Subtype);
13009 end if;
13010
13011 Set_Size_Info (Def_Id, (T));
13012 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13013 Set_Etype (Def_Id, Base_Type (T));
13014
13015 if Constraint_OK then
13016 Set_First_Index (Def_Id, First (Constraints (C)));
13017 else
13018 Set_First_Index (Def_Id, First_Index (T));
13019 end if;
13020
13021 Set_Is_Constrained (Def_Id, True);
13022 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13023 Set_Is_Independent (Def_Id, Is_Independent (T));
13024 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13025
13026 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13027 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13028
13029 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13030 -- We need to initialize the attribute because if Def_Id is previously
13031 -- analyzed through a limited_with clause, it will have the attributes
13032 -- of an incomplete type, one of which is an Elist that overlaps the
13033 -- Packed_Array_Impl_Type field.
13034
13035 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13036
13037 -- Build a freeze node if parent still needs one. Also make sure that
13038 -- the Depends_On_Private status is set because the subtype will need
13039 -- reprocessing at the time the base type does, and also we must set a
13040 -- conditional delay.
13041
13042 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13043 Conditional_Delay (Def_Id, T);
13044 end Constrain_Array;
13045
13046 ------------------------------
13047 -- Constrain_Component_Type --
13048 ------------------------------
13049
13050 function Constrain_Component_Type
13051 (Comp : Entity_Id;
13052 Constrained_Typ : Entity_Id;
13053 Related_Node : Node_Id;
13054 Typ : Entity_Id;
13055 Constraints : Elist_Id) return Entity_Id
13056 is
13057 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13058 Compon_Type : constant Entity_Id := Etype (Comp);
13059
13060 function Build_Constrained_Array_Type
13061 (Old_Type : Entity_Id) return Entity_Id;
13062 -- If Old_Type is an array type, one of whose indexes is constrained
13063 -- by a discriminant, build an Itype whose constraint replaces the
13064 -- discriminant with its value in the constraint.
13065
13066 function Build_Constrained_Discriminated_Type
13067 (Old_Type : Entity_Id) return Entity_Id;
13068 -- Ditto for record components. Handle the case where the constraint
13069 -- is a conversion of the discriminant value, introduced during
13070 -- expansion.
13071
13072 function Build_Constrained_Access_Type
13073 (Old_Type : Entity_Id) return Entity_Id;
13074 -- Ditto for access types. Makes use of previous two functions, to
13075 -- constrain designated type.
13076
13077 function Is_Discriminant (Expr : Node_Id) return Boolean;
13078 -- Returns True if Expr is a discriminant
13079
13080 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13081 -- Find the value of discriminant Discrim in Constraint
13082
13083 -----------------------------------
13084 -- Build_Constrained_Access_Type --
13085 -----------------------------------
13086
13087 function Build_Constrained_Access_Type
13088 (Old_Type : Entity_Id) return Entity_Id
13089 is
13090 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13091 Itype : Entity_Id;
13092 Desig_Subtype : Entity_Id;
13093 Scop : Entity_Id;
13094
13095 begin
13096 -- If the original access type was not embedded in the enclosing
13097 -- type definition, there is no need to produce a new access
13098 -- subtype. In fact every access type with an explicit constraint
13099 -- generates an itype whose scope is the enclosing record.
13100
13101 if not Is_Type (Scope (Old_Type)) then
13102 return Old_Type;
13103
13104 elsif Is_Array_Type (Desig_Type) then
13105 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13106
13107 elsif Has_Discriminants (Desig_Type) then
13108
13109 -- This may be an access type to an enclosing record type for
13110 -- which we are constructing the constrained components. Return
13111 -- the enclosing record subtype. This is not always correct,
13112 -- but avoids infinite recursion. ???
13113
13114 Desig_Subtype := Any_Type;
13115
13116 for J in reverse 0 .. Scope_Stack.Last loop
13117 Scop := Scope_Stack.Table (J).Entity;
13118
13119 if Is_Type (Scop)
13120 and then Base_Type (Scop) = Base_Type (Desig_Type)
13121 then
13122 Desig_Subtype := Scop;
13123 end if;
13124
13125 exit when not Is_Type (Scop);
13126 end loop;
13127
13128 if Desig_Subtype = Any_Type then
13129 Desig_Subtype :=
13130 Build_Constrained_Discriminated_Type (Desig_Type);
13131 end if;
13132
13133 else
13134 return Old_Type;
13135 end if;
13136
13137 if Desig_Subtype /= Desig_Type then
13138
13139 -- The Related_Node better be here or else we won't be able
13140 -- to attach new itypes to a node in the tree.
13141
13142 pragma Assert (Present (Related_Node));
13143
13144 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13145
13146 Set_Etype (Itype, Base_Type (Old_Type));
13147 Set_Size_Info (Itype, (Old_Type));
13148 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13149 Set_Depends_On_Private (Itype, Has_Private_Component
13150 (Old_Type));
13151 Set_Is_Access_Constant (Itype, Is_Access_Constant
13152 (Old_Type));
13153
13154 -- The new itype needs freezing when it depends on a not frozen
13155 -- type and the enclosing subtype needs freezing.
13156
13157 if Has_Delayed_Freeze (Constrained_Typ)
13158 and then not Is_Frozen (Constrained_Typ)
13159 then
13160 Conditional_Delay (Itype, Base_Type (Old_Type));
13161 end if;
13162
13163 return Itype;
13164
13165 else
13166 return Old_Type;
13167 end if;
13168 end Build_Constrained_Access_Type;
13169
13170 ----------------------------------
13171 -- Build_Constrained_Array_Type --
13172 ----------------------------------
13173
13174 function Build_Constrained_Array_Type
13175 (Old_Type : Entity_Id) return Entity_Id
13176 is
13177 Lo_Expr : Node_Id;
13178 Hi_Expr : Node_Id;
13179 Old_Index : Node_Id;
13180 Range_Node : Node_Id;
13181 Constr_List : List_Id;
13182
13183 Need_To_Create_Itype : Boolean := False;
13184
13185 begin
13186 Old_Index := First_Index (Old_Type);
13187 while Present (Old_Index) loop
13188 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13189
13190 if Is_Discriminant (Lo_Expr)
13191 or else
13192 Is_Discriminant (Hi_Expr)
13193 then
13194 Need_To_Create_Itype := True;
13195 exit;
13196 end if;
13197
13198 Next_Index (Old_Index);
13199 end loop;
13200
13201 if Need_To_Create_Itype then
13202 Constr_List := New_List;
13203
13204 Old_Index := First_Index (Old_Type);
13205 while Present (Old_Index) loop
13206 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13207
13208 if Is_Discriminant (Lo_Expr) then
13209 Lo_Expr := Get_Discr_Value (Lo_Expr);
13210 end if;
13211
13212 if Is_Discriminant (Hi_Expr) then
13213 Hi_Expr := Get_Discr_Value (Hi_Expr);
13214 end if;
13215
13216 Range_Node :=
13217 Make_Range
13218 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13219
13220 Append (Range_Node, To => Constr_List);
13221
13222 Next_Index (Old_Index);
13223 end loop;
13224
13225 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13226
13227 else
13228 return Old_Type;
13229 end if;
13230 end Build_Constrained_Array_Type;
13231
13232 ------------------------------------------
13233 -- Build_Constrained_Discriminated_Type --
13234 ------------------------------------------
13235
13236 function Build_Constrained_Discriminated_Type
13237 (Old_Type : Entity_Id) return Entity_Id
13238 is
13239 Expr : Node_Id;
13240 Constr_List : List_Id;
13241 Old_Constraint : Elmt_Id;
13242
13243 Need_To_Create_Itype : Boolean := False;
13244
13245 begin
13246 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13247 while Present (Old_Constraint) loop
13248 Expr := Node (Old_Constraint);
13249
13250 if Is_Discriminant (Expr) then
13251 Need_To_Create_Itype := True;
13252 exit;
13253
13254 -- After expansion of discriminated task types, the value
13255 -- of the discriminant may be converted to a run-time type
13256 -- for restricted run-times. Propagate the value of the
13257 -- discriminant as well, so that e.g. the secondary stack
13258 -- component has a static constraint. Necessary for LLVM.
13259
13260 elsif Nkind (Expr) = N_Type_Conversion
13261 and then Is_Discriminant (Expression (Expr))
13262 then
13263 Need_To_Create_Itype := True;
13264 exit;
13265 end if;
13266
13267 Next_Elmt (Old_Constraint);
13268 end loop;
13269
13270 if Need_To_Create_Itype then
13271 Constr_List := New_List;
13272
13273 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13274 while Present (Old_Constraint) loop
13275 Expr := Node (Old_Constraint);
13276
13277 if Is_Discriminant (Expr) then
13278 Expr := Get_Discr_Value (Expr);
13279
13280 elsif Nkind (Expr) = N_Type_Conversion
13281 and then Is_Discriminant (Expression (Expr))
13282 then
13283 Expr := New_Copy_Tree (Expr);
13284 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13285 end if;
13286
13287 Append (New_Copy_Tree (Expr), To => Constr_List);
13288
13289 Next_Elmt (Old_Constraint);
13290 end loop;
13291
13292 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13293
13294 else
13295 return Old_Type;
13296 end if;
13297 end Build_Constrained_Discriminated_Type;
13298
13299 ---------------------
13300 -- Get_Discr_Value --
13301 ---------------------
13302
13303 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13304 D : Entity_Id;
13305 E : Elmt_Id;
13306
13307 begin
13308 -- The discriminant may be declared for the type, in which case we
13309 -- find it by iterating over the list of discriminants. If the
13310 -- discriminant is inherited from a parent type, it appears as the
13311 -- corresponding discriminant of the current type. This will be the
13312 -- case when constraining an inherited component whose constraint is
13313 -- given by a discriminant of the parent.
13314
13315 D := First_Discriminant (Typ);
13316 E := First_Elmt (Constraints);
13317
13318 while Present (D) loop
13319 if D = Entity (Discrim)
13320 or else D = CR_Discriminant (Entity (Discrim))
13321 or else Corresponding_Discriminant (D) = Entity (Discrim)
13322 then
13323 return Node (E);
13324 end if;
13325
13326 Next_Discriminant (D);
13327 Next_Elmt (E);
13328 end loop;
13329
13330 -- The Corresponding_Discriminant mechanism is incomplete, because
13331 -- the correspondence between new and old discriminants is not one
13332 -- to one: one new discriminant can constrain several old ones. In
13333 -- that case, scan sequentially the stored_constraint, the list of
13334 -- discriminants of the parents, and the constraints.
13335
13336 -- Previous code checked for the present of the Stored_Constraint
13337 -- list for the derived type, but did not use it at all. Should it
13338 -- be present when the component is a discriminated task type?
13339
13340 if Is_Derived_Type (Typ)
13341 and then Scope (Entity (Discrim)) = Etype (Typ)
13342 then
13343 D := First_Discriminant (Etype (Typ));
13344 E := First_Elmt (Constraints);
13345 while Present (D) loop
13346 if D = Entity (Discrim) then
13347 return Node (E);
13348 end if;
13349
13350 Next_Discriminant (D);
13351 Next_Elmt (E);
13352 end loop;
13353 end if;
13354
13355 -- Something is wrong if we did not find the value
13356
13357 raise Program_Error;
13358 end Get_Discr_Value;
13359
13360 ---------------------
13361 -- Is_Discriminant --
13362 ---------------------
13363
13364 function Is_Discriminant (Expr : Node_Id) return Boolean is
13365 Discrim_Scope : Entity_Id;
13366
13367 begin
13368 if Denotes_Discriminant (Expr) then
13369 Discrim_Scope := Scope (Entity (Expr));
13370
13371 -- Either we have a reference to one of Typ's discriminants,
13372
13373 pragma Assert (Discrim_Scope = Typ
13374
13375 -- or to the discriminants of the parent type, in the case
13376 -- of a derivation of a tagged type with variants.
13377
13378 or else Discrim_Scope = Etype (Typ)
13379 or else Full_View (Discrim_Scope) = Etype (Typ)
13380
13381 -- or same as above for the case where the discriminants
13382 -- were declared in Typ's private view.
13383
13384 or else (Is_Private_Type (Discrim_Scope)
13385 and then Chars (Discrim_Scope) = Chars (Typ))
13386
13387 -- or else we are deriving from the full view and the
13388 -- discriminant is declared in the private entity.
13389
13390 or else (Is_Private_Type (Typ)
13391 and then Chars (Discrim_Scope) = Chars (Typ))
13392
13393 -- Or we are constrained the corresponding record of a
13394 -- synchronized type that completes a private declaration.
13395
13396 or else (Is_Concurrent_Record_Type (Typ)
13397 and then
13398 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13399
13400 -- or we have a class-wide type, in which case make sure the
13401 -- discriminant found belongs to the root type.
13402
13403 or else (Is_Class_Wide_Type (Typ)
13404 and then Etype (Typ) = Discrim_Scope));
13405
13406 return True;
13407 end if;
13408
13409 -- In all other cases we have something wrong
13410
13411 return False;
13412 end Is_Discriminant;
13413
13414 -- Start of processing for Constrain_Component_Type
13415
13416 begin
13417 if Nkind (Parent (Comp)) = N_Component_Declaration
13418 and then Comes_From_Source (Parent (Comp))
13419 and then Comes_From_Source
13420 (Subtype_Indication (Component_Definition (Parent (Comp))))
13421 and then
13422 Is_Entity_Name
13423 (Subtype_Indication (Component_Definition (Parent (Comp))))
13424 then
13425 return Compon_Type;
13426
13427 elsif Is_Array_Type (Compon_Type) then
13428 return Build_Constrained_Array_Type (Compon_Type);
13429
13430 elsif Has_Discriminants (Compon_Type) then
13431 return Build_Constrained_Discriminated_Type (Compon_Type);
13432
13433 elsif Is_Access_Type (Compon_Type) then
13434 return Build_Constrained_Access_Type (Compon_Type);
13435
13436 else
13437 return Compon_Type;
13438 end if;
13439 end Constrain_Component_Type;
13440
13441 --------------------------
13442 -- Constrain_Concurrent --
13443 --------------------------
13444
13445 -- For concurrent types, the associated record value type carries the same
13446 -- discriminants, so when we constrain a concurrent type, we must constrain
13447 -- the corresponding record type as well.
13448
13449 procedure Constrain_Concurrent
13450 (Def_Id : in out Entity_Id;
13451 SI : Node_Id;
13452 Related_Nod : Node_Id;
13453 Related_Id : Entity_Id;
13454 Suffix : Character)
13455 is
13456 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13457 -- case of a private subtype (needed when only doing semantic analysis).
13458
13459 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13460 T_Val : Entity_Id;
13461
13462 begin
13463 if Is_Access_Type (T_Ent) then
13464 T_Ent := Designated_Type (T_Ent);
13465 end if;
13466
13467 T_Val := Corresponding_Record_Type (T_Ent);
13468
13469 if Present (T_Val) then
13470
13471 if No (Def_Id) then
13472 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13473
13474 -- Elaborate itype now, as it may be used in a subsequent
13475 -- synchronized operation in another scope.
13476
13477 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13478 Build_Itype_Reference (Def_Id, Related_Nod);
13479 end if;
13480 end if;
13481
13482 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13483 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13484
13485 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13486 Set_Corresponding_Record_Type (Def_Id,
13487 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13488
13489 else
13490 -- If there is no associated record, expansion is disabled and this
13491 -- is a generic context. Create a subtype in any case, so that
13492 -- semantic analysis can proceed.
13493
13494 if No (Def_Id) then
13495 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13496 end if;
13497
13498 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13499 end if;
13500 end Constrain_Concurrent;
13501
13502 ------------------------------------
13503 -- Constrain_Corresponding_Record --
13504 ------------------------------------
13505
13506 function Constrain_Corresponding_Record
13507 (Prot_Subt : Entity_Id;
13508 Corr_Rec : Entity_Id;
13509 Related_Nod : Node_Id) return Entity_Id
13510 is
13511 T_Sub : constant Entity_Id :=
13512 Create_Itype
13513 (Ekind => E_Record_Subtype,
13514 Related_Nod => Related_Nod,
13515 Related_Id => Corr_Rec,
13516 Suffix => 'C',
13517 Suffix_Index => -1);
13518
13519 begin
13520 Set_Etype (T_Sub, Corr_Rec);
13521 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13522 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
13523 Set_Is_Constrained (T_Sub, True);
13524 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13525 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13526
13527 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13528 Set_Discriminant_Constraint
13529 (T_Sub, Discriminant_Constraint (Prot_Subt));
13530 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13531 Create_Constrained_Components
13532 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13533 end if;
13534
13535 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13536
13537 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13538 Conditional_Delay (T_Sub, Corr_Rec);
13539
13540 else
13541 -- This is a component subtype: it will be frozen in the context of
13542 -- the enclosing record's init_proc, so that discriminant references
13543 -- are resolved to discriminals. (Note: we used to skip freezing
13544 -- altogether in that case, which caused errors downstream for
13545 -- components of a bit packed array type).
13546
13547 Set_Has_Delayed_Freeze (T_Sub);
13548 end if;
13549
13550 return T_Sub;
13551 end Constrain_Corresponding_Record;
13552
13553 -----------------------
13554 -- Constrain_Decimal --
13555 -----------------------
13556
13557 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13558 T : constant Entity_Id := Entity (Subtype_Mark (S));
13559 C : constant Node_Id := Constraint (S);
13560 Loc : constant Source_Ptr := Sloc (C);
13561 Range_Expr : Node_Id;
13562 Digits_Expr : Node_Id;
13563 Digits_Val : Uint;
13564 Bound_Val : Ureal;
13565
13566 begin
13567 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13568
13569 if Nkind (C) = N_Range_Constraint then
13570 Range_Expr := Range_Expression (C);
13571 Digits_Val := Digits_Value (T);
13572
13573 else
13574 pragma Assert (Nkind (C) = N_Digits_Constraint);
13575
13576 Digits_Expr := Digits_Expression (C);
13577 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13578
13579 Check_Digits_Expression (Digits_Expr);
13580 Digits_Val := Expr_Value (Digits_Expr);
13581
13582 if Digits_Val > Digits_Value (T) then
13583 Error_Msg_N
13584 ("digits expression is incompatible with subtype", C);
13585 Digits_Val := Digits_Value (T);
13586 end if;
13587
13588 if Present (Range_Constraint (C)) then
13589 Range_Expr := Range_Expression (Range_Constraint (C));
13590 else
13591 Range_Expr := Empty;
13592 end if;
13593 end if;
13594
13595 Set_Etype (Def_Id, Base_Type (T));
13596 Set_Size_Info (Def_Id, (T));
13597 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13598 Set_Delta_Value (Def_Id, Delta_Value (T));
13599 Set_Scale_Value (Def_Id, Scale_Value (T));
13600 Set_Small_Value (Def_Id, Small_Value (T));
13601 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13602 Set_Digits_Value (Def_Id, Digits_Val);
13603
13604 -- Manufacture range from given digits value if no range present
13605
13606 if No (Range_Expr) then
13607 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13608 Range_Expr :=
13609 Make_Range (Loc,
13610 Low_Bound =>
13611 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13612 High_Bound =>
13613 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13614 end if;
13615
13616 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13617 Set_Discrete_RM_Size (Def_Id);
13618
13619 -- Unconditionally delay the freeze, since we cannot set size
13620 -- information in all cases correctly until the freeze point.
13621
13622 Set_Has_Delayed_Freeze (Def_Id);
13623 end Constrain_Decimal;
13624
13625 ----------------------------------
13626 -- Constrain_Discriminated_Type --
13627 ----------------------------------
13628
13629 procedure Constrain_Discriminated_Type
13630 (Def_Id : Entity_Id;
13631 S : Node_Id;
13632 Related_Nod : Node_Id;
13633 For_Access : Boolean := False)
13634 is
13635 E : Entity_Id := Entity (Subtype_Mark (S));
13636 T : Entity_Id;
13637
13638 procedure Fixup_Bad_Constraint;
13639 -- Called after finding a bad constraint, and after having posted an
13640 -- appropriate error message. The goal is to leave type Def_Id in as
13641 -- reasonable state as possible.
13642
13643 --------------------------
13644 -- Fixup_Bad_Constraint --
13645 --------------------------
13646
13647 procedure Fixup_Bad_Constraint is
13648 begin
13649 -- Set a reasonable Ekind for the entity, including incomplete types.
13650
13651 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13652
13653 -- Set Etype to the known type, to reduce chances of cascaded errors
13654
13655 Set_Etype (Def_Id, E);
13656 Set_Error_Posted (Def_Id);
13657 end Fixup_Bad_Constraint;
13658
13659 -- Local variables
13660
13661 C : Node_Id;
13662 Constr : Elist_Id := New_Elmt_List;
13663
13664 -- Start of processing for Constrain_Discriminated_Type
13665
13666 begin
13667 C := Constraint (S);
13668
13669 -- A discriminant constraint is only allowed in a subtype indication,
13670 -- after a subtype mark. This subtype mark must denote either a type
13671 -- with discriminants, or an access type whose designated type is a
13672 -- type with discriminants. A discriminant constraint specifies the
13673 -- values of these discriminants (RM 3.7.2(5)).
13674
13675 T := Base_Type (Entity (Subtype_Mark (S)));
13676
13677 if Is_Access_Type (T) then
13678 T := Designated_Type (T);
13679 end if;
13680
13681 -- In an instance it may be necessary to retrieve the full view of a
13682 -- type with unknown discriminants, or a full view with defaulted
13683 -- discriminants. In other contexts the constraint is illegal.
13684
13685 if In_Instance
13686 and then Is_Private_Type (T)
13687 and then Present (Full_View (T))
13688 and then
13689 (Has_Unknown_Discriminants (T)
13690 or else
13691 (not Has_Discriminants (T)
13692 and then Has_Discriminants (Full_View (T))
13693 and then Present (Discriminant_Default_Value
13694 (First_Discriminant (Full_View (T))))))
13695 then
13696 T := Full_View (T);
13697 E := Full_View (E);
13698 end if;
13699
13700 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13701 -- generating an error for access-to-incomplete subtypes.
13702
13703 if Ada_Version >= Ada_2005
13704 and then Ekind (T) = E_Incomplete_Type
13705 and then Nkind (Parent (S)) = N_Subtype_Declaration
13706 and then not Is_Itype (Def_Id)
13707 then
13708 -- A little sanity check: emit an error message if the type has
13709 -- discriminants to begin with. Type T may be a regular incomplete
13710 -- type or imported via a limited with clause.
13711
13712 if Has_Discriminants (T)
13713 or else (From_Limited_With (T)
13714 and then Present (Non_Limited_View (T))
13715 and then Nkind (Parent (Non_Limited_View (T))) =
13716 N_Full_Type_Declaration
13717 and then Present (Discriminant_Specifications
13718 (Parent (Non_Limited_View (T)))))
13719 then
13720 Error_Msg_N
13721 ("(Ada 2005) incomplete subtype may not be constrained", C);
13722 else
13723 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13724 end if;
13725
13726 Fixup_Bad_Constraint;
13727 return;
13728
13729 -- Check that the type has visible discriminants. The type may be
13730 -- a private type with unknown discriminants whose full view has
13731 -- discriminants which are invisible.
13732
13733 elsif not Has_Discriminants (T)
13734 or else
13735 (Has_Unknown_Discriminants (T)
13736 and then Is_Private_Type (T))
13737 then
13738 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13739 Fixup_Bad_Constraint;
13740 return;
13741
13742 elsif Is_Constrained (E)
13743 or else (Ekind (E) = E_Class_Wide_Subtype
13744 and then Present (Discriminant_Constraint (E)))
13745 then
13746 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13747 Fixup_Bad_Constraint;
13748 return;
13749 end if;
13750
13751 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13752 -- applies to the base type.
13753
13754 T := Base_Type (T);
13755
13756 Constr := Build_Discriminant_Constraints (T, S);
13757
13758 -- If the list returned was empty we had an error in building the
13759 -- discriminant constraint. We have also already signalled an error
13760 -- in the incomplete type case
13761
13762 if Is_Empty_Elmt_List (Constr) then
13763 Fixup_Bad_Constraint;
13764 return;
13765 end if;
13766
13767 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13768 end Constrain_Discriminated_Type;
13769
13770 ---------------------------
13771 -- Constrain_Enumeration --
13772 ---------------------------
13773
13774 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13775 T : constant Entity_Id := Entity (Subtype_Mark (S));
13776 C : constant Node_Id := Constraint (S);
13777
13778 begin
13779 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13780
13781 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13782
13783 Set_Etype (Def_Id, Base_Type (T));
13784 Set_Size_Info (Def_Id, (T));
13785 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13786 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13787
13788 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13789
13790 Set_Discrete_RM_Size (Def_Id);
13791 end Constrain_Enumeration;
13792
13793 ----------------------
13794 -- Constrain_Float --
13795 ----------------------
13796
13797 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13798 T : constant Entity_Id := Entity (Subtype_Mark (S));
13799 C : Node_Id;
13800 D : Node_Id;
13801 Rais : Node_Id;
13802
13803 begin
13804 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13805
13806 Set_Etype (Def_Id, Base_Type (T));
13807 Set_Size_Info (Def_Id, (T));
13808 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13809
13810 -- Process the constraint
13811
13812 C := Constraint (S);
13813
13814 -- Digits constraint present
13815
13816 if Nkind (C) = N_Digits_Constraint then
13817 Check_Restriction (No_Obsolescent_Features, C);
13818
13819 if Warn_On_Obsolescent_Feature then
13820 Error_Msg_N
13821 ("subtype digits constraint is an " &
13822 "obsolescent feature (RM J.3(8))?j?", C);
13823 end if;
13824
13825 D := Digits_Expression (C);
13826 Analyze_And_Resolve (D, Any_Integer);
13827 Check_Digits_Expression (D);
13828 Set_Digits_Value (Def_Id, Expr_Value (D));
13829
13830 -- Check that digits value is in range. Obviously we can do this
13831 -- at compile time, but it is strictly a runtime check, and of
13832 -- course there is an ACVC test that checks this.
13833
13834 if Digits_Value (Def_Id) > Digits_Value (T) then
13835 Error_Msg_Uint_1 := Digits_Value (T);
13836 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13837 Rais :=
13838 Make_Raise_Constraint_Error (Sloc (D),
13839 Reason => CE_Range_Check_Failed);
13840 Insert_Action (Declaration_Node (Def_Id), Rais);
13841 end if;
13842
13843 C := Range_Constraint (C);
13844
13845 -- No digits constraint present
13846
13847 else
13848 Set_Digits_Value (Def_Id, Digits_Value (T));
13849 end if;
13850
13851 -- Range constraint present
13852
13853 if Nkind (C) = N_Range_Constraint then
13854 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13855
13856 -- No range constraint present
13857
13858 else
13859 pragma Assert (No (C));
13860 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13861 end if;
13862
13863 Set_Is_Constrained (Def_Id);
13864 end Constrain_Float;
13865
13866 ---------------------
13867 -- Constrain_Index --
13868 ---------------------
13869
13870 procedure Constrain_Index
13871 (Index : Node_Id;
13872 S : Node_Id;
13873 Related_Nod : Node_Id;
13874 Related_Id : Entity_Id;
13875 Suffix : Character;
13876 Suffix_Index : Nat)
13877 is
13878 Def_Id : Entity_Id;
13879 R : Node_Id := Empty;
13880 T : constant Entity_Id := Etype (Index);
13881
13882 begin
13883 Def_Id :=
13884 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13885 Set_Etype (Def_Id, Base_Type (T));
13886
13887 if Nkind (S) = N_Range
13888 or else
13889 (Nkind (S) = N_Attribute_Reference
13890 and then Attribute_Name (S) = Name_Range)
13891 then
13892 -- A Range attribute will be transformed into N_Range by Resolve
13893
13894 Analyze (S);
13895 Set_Etype (S, T);
13896 R := S;
13897
13898 Process_Range_Expr_In_Decl (R, T);
13899
13900 if not Error_Posted (S)
13901 and then
13902 (Nkind (S) /= N_Range
13903 or else not Covers (T, (Etype (Low_Bound (S))))
13904 or else not Covers (T, (Etype (High_Bound (S)))))
13905 then
13906 if Base_Type (T) /= Any_Type
13907 and then Etype (Low_Bound (S)) /= Any_Type
13908 and then Etype (High_Bound (S)) /= Any_Type
13909 then
13910 Error_Msg_N ("range expected", S);
13911 end if;
13912 end if;
13913
13914 elsif Nkind (S) = N_Subtype_Indication then
13915
13916 -- The parser has verified that this is a discrete indication
13917
13918 Resolve_Discrete_Subtype_Indication (S, T);
13919 Bad_Predicated_Subtype_Use
13920 ("subtype& has predicate, not allowed in index constraint",
13921 S, Entity (Subtype_Mark (S)));
13922
13923 R := Range_Expression (Constraint (S));
13924
13925 -- Capture values of bounds and generate temporaries for them if
13926 -- needed, since checks may cause duplication of the expressions
13927 -- which must not be reevaluated.
13928
13929 -- The forced evaluation removes side effects from expressions, which
13930 -- should occur also in GNATprove mode. Otherwise, we end up with
13931 -- unexpected insertions of actions at places where this is not
13932 -- supposed to occur, e.g. on default parameters of a call.
13933
13934 if Expander_Active or GNATprove_Mode then
13935 Force_Evaluation
13936 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13937 Force_Evaluation
13938 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13939 end if;
13940
13941 elsif Nkind (S) = N_Discriminant_Association then
13942
13943 -- Syntactically valid in subtype indication
13944
13945 Error_Msg_N ("invalid index constraint", S);
13946 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13947 return;
13948
13949 -- Subtype_Mark case, no anonymous subtypes to construct
13950
13951 else
13952 Analyze (S);
13953
13954 if Is_Entity_Name (S) then
13955 if not Is_Type (Entity (S)) then
13956 Error_Msg_N ("expect subtype mark for index constraint", S);
13957
13958 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13959 Wrong_Type (S, Base_Type (T));
13960
13961 -- Check error of subtype with predicate in index constraint
13962
13963 else
13964 Bad_Predicated_Subtype_Use
13965 ("subtype& has predicate, not allowed in index constraint",
13966 S, Entity (S));
13967 end if;
13968
13969 return;
13970
13971 else
13972 Error_Msg_N ("invalid index constraint", S);
13973 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13974 return;
13975 end if;
13976 end if;
13977
13978 -- Complete construction of the Itype
13979
13980 if Is_Modular_Integer_Type (T) then
13981 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13982
13983 elsif Is_Integer_Type (T) then
13984 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13985
13986 else
13987 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13988 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13989 Set_First_Literal (Def_Id, First_Literal (T));
13990 end if;
13991
13992 Set_Size_Info (Def_Id, (T));
13993 Set_RM_Size (Def_Id, RM_Size (T));
13994 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13995
13996 Set_Scalar_Range (Def_Id, R);
13997
13998 Set_Etype (S, Def_Id);
13999 Set_Discrete_RM_Size (Def_Id);
14000 end Constrain_Index;
14001
14002 -----------------------
14003 -- Constrain_Integer --
14004 -----------------------
14005
14006 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14007 T : constant Entity_Id := Entity (Subtype_Mark (S));
14008 C : constant Node_Id := Constraint (S);
14009
14010 begin
14011 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14012
14013 if Is_Modular_Integer_Type (T) then
14014 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14015 else
14016 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14017 end if;
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 Set_Discrete_RM_Size (Def_Id);
14023 end Constrain_Integer;
14024
14025 ------------------------------
14026 -- Constrain_Ordinary_Fixed --
14027 ------------------------------
14028
14029 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14030 T : constant Entity_Id := Entity (Subtype_Mark (S));
14031 C : Node_Id;
14032 D : Node_Id;
14033 Rais : Node_Id;
14034
14035 begin
14036 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14037 Set_Etype (Def_Id, Base_Type (T));
14038 Set_Size_Info (Def_Id, (T));
14039 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14040 Set_Small_Value (Def_Id, Small_Value (T));
14041
14042 -- Process the constraint
14043
14044 C := Constraint (S);
14045
14046 -- Delta constraint present
14047
14048 if Nkind (C) = N_Delta_Constraint then
14049 Check_Restriction (No_Obsolescent_Features, C);
14050
14051 if Warn_On_Obsolescent_Feature then
14052 Error_Msg_S
14053 ("subtype delta constraint is an " &
14054 "obsolescent feature (RM J.3(7))?j?");
14055 end if;
14056
14057 D := Delta_Expression (C);
14058 Analyze_And_Resolve (D, Any_Real);
14059 Check_Delta_Expression (D);
14060 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14061
14062 -- Check that delta value is in range. Obviously we can do this
14063 -- at compile time, but it is strictly a runtime check, and of
14064 -- course there is an ACVC test that checks this.
14065
14066 if Delta_Value (Def_Id) < Delta_Value (T) then
14067 Error_Msg_N ("??delta value is too small", D);
14068 Rais :=
14069 Make_Raise_Constraint_Error (Sloc (D),
14070 Reason => CE_Range_Check_Failed);
14071 Insert_Action (Declaration_Node (Def_Id), Rais);
14072 end if;
14073
14074 C := Range_Constraint (C);
14075
14076 -- No delta constraint present
14077
14078 else
14079 Set_Delta_Value (Def_Id, Delta_Value (T));
14080 end if;
14081
14082 -- Range constraint present
14083
14084 if Nkind (C) = N_Range_Constraint then
14085 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14086
14087 -- No range constraint present
14088
14089 else
14090 pragma Assert (No (C));
14091 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14092 end if;
14093
14094 Set_Discrete_RM_Size (Def_Id);
14095
14096 -- Unconditionally delay the freeze, since we cannot set size
14097 -- information in all cases correctly until the freeze point.
14098
14099 Set_Has_Delayed_Freeze (Def_Id);
14100 end Constrain_Ordinary_Fixed;
14101
14102 -----------------------
14103 -- Contain_Interface --
14104 -----------------------
14105
14106 function Contain_Interface
14107 (Iface : Entity_Id;
14108 Ifaces : Elist_Id) return Boolean
14109 is
14110 Iface_Elmt : Elmt_Id;
14111
14112 begin
14113 if Present (Ifaces) then
14114 Iface_Elmt := First_Elmt (Ifaces);
14115 while Present (Iface_Elmt) loop
14116 if Node (Iface_Elmt) = Iface then
14117 return True;
14118 end if;
14119
14120 Next_Elmt (Iface_Elmt);
14121 end loop;
14122 end if;
14123
14124 return False;
14125 end Contain_Interface;
14126
14127 ---------------------------
14128 -- Convert_Scalar_Bounds --
14129 ---------------------------
14130
14131 procedure Convert_Scalar_Bounds
14132 (N : Node_Id;
14133 Parent_Type : Entity_Id;
14134 Derived_Type : Entity_Id;
14135 Loc : Source_Ptr)
14136 is
14137 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14138
14139 Lo : Node_Id;
14140 Hi : Node_Id;
14141 Rng : Node_Id;
14142
14143 begin
14144 -- Defend against previous errors
14145
14146 if No (Scalar_Range (Derived_Type)) then
14147 Check_Error_Detected;
14148 return;
14149 end if;
14150
14151 Lo := Build_Scalar_Bound
14152 (Type_Low_Bound (Derived_Type),
14153 Parent_Type, Implicit_Base);
14154
14155 Hi := Build_Scalar_Bound
14156 (Type_High_Bound (Derived_Type),
14157 Parent_Type, Implicit_Base);
14158
14159 Rng :=
14160 Make_Range (Loc,
14161 Low_Bound => Lo,
14162 High_Bound => Hi);
14163
14164 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14165
14166 Set_Parent (Rng, N);
14167 Set_Scalar_Range (Derived_Type, Rng);
14168
14169 -- Analyze the bounds
14170
14171 Analyze_And_Resolve (Lo, Implicit_Base);
14172 Analyze_And_Resolve (Hi, Implicit_Base);
14173
14174 -- Analyze the range itself, except that we do not analyze it if
14175 -- the bounds are real literals, and we have a fixed-point type.
14176 -- The reason for this is that we delay setting the bounds in this
14177 -- case till we know the final Small and Size values (see circuit
14178 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14179
14180 if Is_Fixed_Point_Type (Parent_Type)
14181 and then Nkind (Lo) = N_Real_Literal
14182 and then Nkind (Hi) = N_Real_Literal
14183 then
14184 return;
14185
14186 -- Here we do the analysis of the range
14187
14188 -- Note: we do this manually, since if we do a normal Analyze and
14189 -- Resolve call, there are problems with the conversions used for
14190 -- the derived type range.
14191
14192 else
14193 Set_Etype (Rng, Implicit_Base);
14194 Set_Analyzed (Rng, True);
14195 end if;
14196 end Convert_Scalar_Bounds;
14197
14198 -------------------
14199 -- Copy_And_Swap --
14200 -------------------
14201
14202 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14203 begin
14204 -- Initialize new full declaration entity by copying the pertinent
14205 -- fields of the corresponding private declaration entity.
14206
14207 -- We temporarily set Ekind to a value appropriate for a type to
14208 -- avoid assert failures in Einfo from checking for setting type
14209 -- attributes on something that is not a type. Ekind (Priv) is an
14210 -- appropriate choice, since it allowed the attributes to be set
14211 -- in the first place. This Ekind value will be modified later.
14212
14213 Set_Ekind (Full, Ekind (Priv));
14214
14215 -- Also set Etype temporarily to Any_Type, again, in the absence
14216 -- of errors, it will be properly reset, and if there are errors,
14217 -- then we want a value of Any_Type to remain.
14218
14219 Set_Etype (Full, Any_Type);
14220
14221 -- Now start copying attributes
14222
14223 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14224
14225 if Has_Discriminants (Full) then
14226 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14227 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14228 end if;
14229
14230 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14231 Set_Homonym (Full, Homonym (Priv));
14232 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14233 Set_Is_Public (Full, Is_Public (Priv));
14234 Set_Is_Pure (Full, Is_Pure (Priv));
14235 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14236 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14237 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14238 Set_Has_Pragma_Unreferenced_Objects
14239 (Full, Has_Pragma_Unreferenced_Objects
14240 (Priv));
14241
14242 Conditional_Delay (Full, Priv);
14243
14244 if Is_Tagged_Type (Full) then
14245 Set_Direct_Primitive_Operations
14246 (Full, Direct_Primitive_Operations (Priv));
14247 Set_No_Tagged_Streams_Pragma
14248 (Full, No_Tagged_Streams_Pragma (Priv));
14249
14250 if Is_Base_Type (Priv) then
14251 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14252 end if;
14253 end if;
14254
14255 Set_Is_Volatile (Full, Is_Volatile (Priv));
14256 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14257 Set_Scope (Full, Scope (Priv));
14258 Set_Prev_Entity (Full, Prev_Entity (Priv));
14259 Set_Next_Entity (Full, Next_Entity (Priv));
14260 Set_First_Entity (Full, First_Entity (Priv));
14261 Set_Last_Entity (Full, Last_Entity (Priv));
14262
14263 -- If access types have been recorded for later handling, keep them in
14264 -- the full view so that they get handled when the full view freeze
14265 -- node is expanded.
14266
14267 if Present (Freeze_Node (Priv))
14268 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14269 then
14270 Ensure_Freeze_Node (Full);
14271 Set_Access_Types_To_Process
14272 (Freeze_Node (Full),
14273 Access_Types_To_Process (Freeze_Node (Priv)));
14274 end if;
14275
14276 -- Swap the two entities. Now Private is the full type entity and Full
14277 -- is the private one. They will be swapped back at the end of the
14278 -- private part. This swapping ensures that the entity that is visible
14279 -- in the private part is the full declaration.
14280
14281 Exchange_Entities (Priv, Full);
14282 Append_Entity (Full, Scope (Full));
14283 end Copy_And_Swap;
14284
14285 -------------------------------------
14286 -- Copy_Array_Base_Type_Attributes --
14287 -------------------------------------
14288
14289 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14290 begin
14291 Set_Component_Alignment (T1, Component_Alignment (T2));
14292 Set_Component_Type (T1, Component_Type (T2));
14293 Set_Component_Size (T1, Component_Size (T2));
14294 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14295 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14296 Propagate_Concurrent_Flags (T1, T2);
14297 Set_Is_Packed (T1, Is_Packed (T2));
14298 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14299 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14300 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14301 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14302 end Copy_Array_Base_Type_Attributes;
14303
14304 -----------------------------------
14305 -- Copy_Array_Subtype_Attributes --
14306 -----------------------------------
14307
14308 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14309 begin
14310 Set_Size_Info (T1, T2);
14311
14312 Set_First_Index (T1, First_Index (T2));
14313 Set_Is_Aliased (T1, Is_Aliased (T2));
14314 Set_Is_Atomic (T1, Is_Atomic (T2));
14315 Set_Is_Independent (T1, Is_Independent (T2));
14316 Set_Is_Volatile (T1, Is_Volatile (T2));
14317 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14318 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14319 Set_Is_Constrained (T1, Is_Constrained (T2));
14320 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14321 Inherit_Rep_Item_Chain (T1, T2);
14322 Set_Convention (T1, Convention (T2));
14323 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14324 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14325 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14326 end Copy_Array_Subtype_Attributes;
14327
14328 -----------------------------------
14329 -- Create_Constrained_Components --
14330 -----------------------------------
14331
14332 procedure Create_Constrained_Components
14333 (Subt : Entity_Id;
14334 Decl_Node : Node_Id;
14335 Typ : Entity_Id;
14336 Constraints : Elist_Id)
14337 is
14338 Loc : constant Source_Ptr := Sloc (Subt);
14339 Comp_List : constant Elist_Id := New_Elmt_List;
14340 Parent_Type : constant Entity_Id := Etype (Typ);
14341 Assoc_List : constant List_Id := New_List;
14342 Discr_Val : Elmt_Id;
14343 Errors : Boolean;
14344 New_C : Entity_Id;
14345 Old_C : Entity_Id;
14346 Is_Static : Boolean := True;
14347
14348 procedure Collect_Fixed_Components (Typ : Entity_Id);
14349 -- Collect parent type components that do not appear in a variant part
14350
14351 procedure Create_All_Components;
14352 -- Iterate over Comp_List to create the components of the subtype
14353
14354 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14355 -- Creates a new component from Old_Compon, copying all the fields from
14356 -- it, including its Etype, inserts the new component in the Subt entity
14357 -- chain and returns the new component.
14358
14359 function Is_Variant_Record (T : Entity_Id) return Boolean;
14360 -- If true, and discriminants are static, collect only components from
14361 -- variants selected by discriminant values.
14362
14363 ------------------------------
14364 -- Collect_Fixed_Components --
14365 ------------------------------
14366
14367 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14368 begin
14369 -- Build association list for discriminants, and find components of the
14370 -- variant part selected by the values of the discriminants.
14371
14372 Old_C := First_Discriminant (Typ);
14373 Discr_Val := First_Elmt (Constraints);
14374 while Present (Old_C) loop
14375 Append_To (Assoc_List,
14376 Make_Component_Association (Loc,
14377 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14378 Expression => New_Copy (Node (Discr_Val))));
14379
14380 Next_Elmt (Discr_Val);
14381 Next_Discriminant (Old_C);
14382 end loop;
14383
14384 -- The tag and the possible parent component are unconditionally in
14385 -- the subtype.
14386
14387 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14388 Old_C := First_Component (Typ);
14389 while Present (Old_C) loop
14390 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14391 Append_Elmt (Old_C, Comp_List);
14392 end if;
14393
14394 Next_Component (Old_C);
14395 end loop;
14396 end if;
14397 end Collect_Fixed_Components;
14398
14399 ---------------------------
14400 -- Create_All_Components --
14401 ---------------------------
14402
14403 procedure Create_All_Components is
14404 Comp : Elmt_Id;
14405
14406 begin
14407 Comp := First_Elmt (Comp_List);
14408 while Present (Comp) loop
14409 Old_C := Node (Comp);
14410 New_C := Create_Component (Old_C);
14411
14412 Set_Etype
14413 (New_C,
14414 Constrain_Component_Type
14415 (Old_C, Subt, Decl_Node, Typ, Constraints));
14416 Set_Is_Public (New_C, Is_Public (Subt));
14417
14418 Next_Elmt (Comp);
14419 end loop;
14420 end Create_All_Components;
14421
14422 ----------------------
14423 -- Create_Component --
14424 ----------------------
14425
14426 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14427 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14428
14429 begin
14430 if Ekind (Old_Compon) = E_Discriminant
14431 and then Is_Completely_Hidden (Old_Compon)
14432 then
14433 -- This is a shadow discriminant created for a discriminant of
14434 -- the parent type, which needs to be present in the subtype.
14435 -- Give the shadow discriminant an internal name that cannot
14436 -- conflict with that of visible components.
14437
14438 Set_Chars (New_Compon, New_Internal_Name ('C'));
14439 end if;
14440
14441 -- Set the parent so we have a proper link for freezing etc. This is
14442 -- not a real parent pointer, since of course our parent does not own
14443 -- up to us and reference us, we are an illegitimate child of the
14444 -- original parent.
14445
14446 Set_Parent (New_Compon, Parent (Old_Compon));
14447
14448 -- We do not want this node marked as Comes_From_Source, since
14449 -- otherwise it would get first class status and a separate cross-
14450 -- reference line would be generated. Illegitimate children do not
14451 -- rate such recognition.
14452
14453 Set_Comes_From_Source (New_Compon, False);
14454
14455 -- But it is a real entity, and a birth certificate must be properly
14456 -- registered by entering it into the entity list, and setting its
14457 -- scope to the given subtype. This turns out to be useful for the
14458 -- LLVM code generator, but that scope is not used otherwise.
14459
14460 Enter_Name (New_Compon);
14461 Set_Scope (New_Compon, Subt);
14462
14463 return New_Compon;
14464 end Create_Component;
14465
14466 -----------------------
14467 -- Is_Variant_Record --
14468 -----------------------
14469
14470 function Is_Variant_Record (T : Entity_Id) return Boolean is
14471 begin
14472 return Nkind (Parent (T)) = N_Full_Type_Declaration
14473 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14474 and then Present (Component_List (Type_Definition (Parent (T))))
14475 and then
14476 Present
14477 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14478 end Is_Variant_Record;
14479
14480 -- Start of processing for Create_Constrained_Components
14481
14482 begin
14483 pragma Assert (Subt /= Base_Type (Subt));
14484 pragma Assert (Typ = Base_Type (Typ));
14485
14486 Set_First_Entity (Subt, Empty);
14487 Set_Last_Entity (Subt, Empty);
14488
14489 -- Check whether constraint is fully static, in which case we can
14490 -- optimize the list of components.
14491
14492 Discr_Val := First_Elmt (Constraints);
14493 while Present (Discr_Val) loop
14494 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14495 Is_Static := False;
14496 exit;
14497 end if;
14498
14499 Next_Elmt (Discr_Val);
14500 end loop;
14501
14502 Set_Has_Static_Discriminants (Subt, Is_Static);
14503
14504 Push_Scope (Subt);
14505
14506 -- Inherit the discriminants of the parent type
14507
14508 Add_Discriminants : declare
14509 Num_Disc : Nat;
14510 Num_Gird : Nat;
14511
14512 begin
14513 Num_Disc := 0;
14514 Old_C := First_Discriminant (Typ);
14515
14516 while Present (Old_C) loop
14517 Num_Disc := Num_Disc + 1;
14518 New_C := Create_Component (Old_C);
14519 Set_Is_Public (New_C, Is_Public (Subt));
14520 Next_Discriminant (Old_C);
14521 end loop;
14522
14523 -- For an untagged derived subtype, the number of discriminants may
14524 -- be smaller than the number of inherited discriminants, because
14525 -- several of them may be renamed by a single new discriminant or
14526 -- constrained. In this case, add the hidden discriminants back into
14527 -- the subtype, because they need to be present if the optimizer of
14528 -- the GCC 4.x back-end decides to break apart assignments between
14529 -- objects using the parent view into member-wise assignments.
14530
14531 Num_Gird := 0;
14532
14533 if Is_Derived_Type (Typ)
14534 and then not Is_Tagged_Type (Typ)
14535 then
14536 Old_C := First_Stored_Discriminant (Typ);
14537
14538 while Present (Old_C) loop
14539 Num_Gird := Num_Gird + 1;
14540 Next_Stored_Discriminant (Old_C);
14541 end loop;
14542 end if;
14543
14544 if Num_Gird > Num_Disc then
14545
14546 -- Find out multiple uses of new discriminants, and add hidden
14547 -- components for the extra renamed discriminants. We recognize
14548 -- multiple uses through the Corresponding_Discriminant of a
14549 -- new discriminant: if it constrains several old discriminants,
14550 -- this field points to the last one in the parent type. The
14551 -- stored discriminants of the derived type have the same name
14552 -- as those of the parent.
14553
14554 declare
14555 Constr : Elmt_Id;
14556 New_Discr : Entity_Id;
14557 Old_Discr : Entity_Id;
14558
14559 begin
14560 Constr := First_Elmt (Stored_Constraint (Typ));
14561 Old_Discr := First_Stored_Discriminant (Typ);
14562 while Present (Constr) loop
14563 if Is_Entity_Name (Node (Constr))
14564 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14565 then
14566 New_Discr := Entity (Node (Constr));
14567
14568 if Chars (Corresponding_Discriminant (New_Discr)) /=
14569 Chars (Old_Discr)
14570 then
14571 -- The new discriminant has been used to rename a
14572 -- subsequent old discriminant. Introduce a shadow
14573 -- component for the current old discriminant.
14574
14575 New_C := Create_Component (Old_Discr);
14576 Set_Original_Record_Component (New_C, Old_Discr);
14577 end if;
14578
14579 else
14580 -- The constraint has eliminated the old discriminant.
14581 -- Introduce a shadow component.
14582
14583 New_C := Create_Component (Old_Discr);
14584 Set_Original_Record_Component (New_C, Old_Discr);
14585 end if;
14586
14587 Next_Elmt (Constr);
14588 Next_Stored_Discriminant (Old_Discr);
14589 end loop;
14590 end;
14591 end if;
14592 end Add_Discriminants;
14593
14594 if Is_Static
14595 and then Is_Variant_Record (Typ)
14596 then
14597 Collect_Fixed_Components (Typ);
14598
14599 Gather_Components (
14600 Typ,
14601 Component_List (Type_Definition (Parent (Typ))),
14602 Governed_By => Assoc_List,
14603 Into => Comp_List,
14604 Report_Errors => Errors);
14605 pragma Assert (not Errors
14606 or else Serious_Errors_Detected > 0);
14607
14608 Create_All_Components;
14609
14610 -- If the subtype declaration is created for a tagged type derivation
14611 -- with constraints, we retrieve the record definition of the parent
14612 -- type to select the components of the proper variant.
14613
14614 elsif Is_Static
14615 and then Is_Tagged_Type (Typ)
14616 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14617 and then
14618 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14619 and then Is_Variant_Record (Parent_Type)
14620 then
14621 Collect_Fixed_Components (Typ);
14622
14623 Gather_Components
14624 (Typ,
14625 Component_List (Type_Definition (Parent (Parent_Type))),
14626 Governed_By => Assoc_List,
14627 Into => Comp_List,
14628 Report_Errors => Errors);
14629
14630 -- Note: previously there was a check at this point that no errors
14631 -- were detected. As a consequence of AI05-220 there may be an error
14632 -- if an inherited discriminant that controls a variant has a non-
14633 -- static constraint.
14634
14635 -- If the tagged derivation has a type extension, collect all the
14636 -- new components therein.
14637
14638 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14639 then
14640 Old_C := First_Component (Typ);
14641 while Present (Old_C) loop
14642 if Original_Record_Component (Old_C) = Old_C
14643 and then Chars (Old_C) /= Name_uTag
14644 and then Chars (Old_C) /= Name_uParent
14645 then
14646 Append_Elmt (Old_C, Comp_List);
14647 end if;
14648
14649 Next_Component (Old_C);
14650 end loop;
14651 end if;
14652
14653 Create_All_Components;
14654
14655 else
14656 -- If discriminants are not static, or if this is a multi-level type
14657 -- extension, we have to include all components of the parent type.
14658
14659 Old_C := First_Component (Typ);
14660 while Present (Old_C) loop
14661 New_C := Create_Component (Old_C);
14662
14663 Set_Etype
14664 (New_C,
14665 Constrain_Component_Type
14666 (Old_C, Subt, Decl_Node, Typ, Constraints));
14667 Set_Is_Public (New_C, Is_Public (Subt));
14668
14669 Next_Component (Old_C);
14670 end loop;
14671 end if;
14672
14673 End_Scope;
14674 end Create_Constrained_Components;
14675
14676 ------------------------------------------
14677 -- Decimal_Fixed_Point_Type_Declaration --
14678 ------------------------------------------
14679
14680 procedure Decimal_Fixed_Point_Type_Declaration
14681 (T : Entity_Id;
14682 Def : Node_Id)
14683 is
14684 Loc : constant Source_Ptr := Sloc (Def);
14685 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14686 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14687 Implicit_Base : Entity_Id;
14688 Digs_Val : Uint;
14689 Delta_Val : Ureal;
14690 Scale_Val : Uint;
14691 Bound_Val : Ureal;
14692
14693 begin
14694 Check_Restriction (No_Fixed_Point, Def);
14695
14696 -- Create implicit base type
14697
14698 Implicit_Base :=
14699 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14700 Set_Etype (Implicit_Base, Implicit_Base);
14701
14702 -- Analyze and process delta expression
14703
14704 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14705
14706 Check_Delta_Expression (Delta_Expr);
14707 Delta_Val := Expr_Value_R (Delta_Expr);
14708
14709 -- Check delta is power of 10, and determine scale value from it
14710
14711 declare
14712 Val : Ureal;
14713
14714 begin
14715 Scale_Val := Uint_0;
14716 Val := Delta_Val;
14717
14718 if Val < Ureal_1 then
14719 while Val < Ureal_1 loop
14720 Val := Val * Ureal_10;
14721 Scale_Val := Scale_Val + 1;
14722 end loop;
14723
14724 if Scale_Val > 18 then
14725 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14726 Scale_Val := UI_From_Int (+18);
14727 end if;
14728
14729 else
14730 while Val > Ureal_1 loop
14731 Val := Val / Ureal_10;
14732 Scale_Val := Scale_Val - 1;
14733 end loop;
14734
14735 if Scale_Val < -18 then
14736 Error_Msg_N ("scale is less than minimum value of -18", Def);
14737 Scale_Val := UI_From_Int (-18);
14738 end if;
14739 end if;
14740
14741 if Val /= Ureal_1 then
14742 Error_Msg_N ("delta expression must be a power of 10", Def);
14743 Delta_Val := Ureal_10 ** (-Scale_Val);
14744 end if;
14745 end;
14746
14747 -- Set delta, scale and small (small = delta for decimal type)
14748
14749 Set_Delta_Value (Implicit_Base, Delta_Val);
14750 Set_Scale_Value (Implicit_Base, Scale_Val);
14751 Set_Small_Value (Implicit_Base, Delta_Val);
14752
14753 -- Analyze and process digits expression
14754
14755 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14756 Check_Digits_Expression (Digs_Expr);
14757 Digs_Val := Expr_Value (Digs_Expr);
14758
14759 if Digs_Val > 18 then
14760 Digs_Val := UI_From_Int (+18);
14761 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14762 end if;
14763
14764 Set_Digits_Value (Implicit_Base, Digs_Val);
14765 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14766
14767 -- Set range of base type from digits value for now. This will be
14768 -- expanded to represent the true underlying base range by Freeze.
14769
14770 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14771
14772 -- Note: We leave size as zero for now, size will be set at freeze
14773 -- time. We have to do this for ordinary fixed-point, because the size
14774 -- depends on the specified small, and we might as well do the same for
14775 -- decimal fixed-point.
14776
14777 pragma Assert (Esize (Implicit_Base) = Uint_0);
14778
14779 -- If there are bounds given in the declaration use them as the
14780 -- bounds of the first named subtype.
14781
14782 if Present (Real_Range_Specification (Def)) then
14783 declare
14784 RRS : constant Node_Id := Real_Range_Specification (Def);
14785 Low : constant Node_Id := Low_Bound (RRS);
14786 High : constant Node_Id := High_Bound (RRS);
14787 Low_Val : Ureal;
14788 High_Val : Ureal;
14789
14790 begin
14791 Analyze_And_Resolve (Low, Any_Real);
14792 Analyze_And_Resolve (High, Any_Real);
14793 Check_Real_Bound (Low);
14794 Check_Real_Bound (High);
14795 Low_Val := Expr_Value_R (Low);
14796 High_Val := Expr_Value_R (High);
14797
14798 if Low_Val < (-Bound_Val) then
14799 Error_Msg_N
14800 ("range low bound too small for digits value", Low);
14801 Low_Val := -Bound_Val;
14802 end if;
14803
14804 if High_Val > Bound_Val then
14805 Error_Msg_N
14806 ("range high bound too large for digits value", High);
14807 High_Val := Bound_Val;
14808 end if;
14809
14810 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14811 end;
14812
14813 -- If no explicit range, use range that corresponds to given
14814 -- digits value. This will end up as the final range for the
14815 -- first subtype.
14816
14817 else
14818 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14819 end if;
14820
14821 -- Complete entity for first subtype. The inheritance of the rep item
14822 -- chain ensures that SPARK-related pragmas are not clobbered when the
14823 -- decimal fixed point type acts as a full view of a private type.
14824
14825 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14826 Set_Etype (T, Implicit_Base);
14827 Set_Size_Info (T, Implicit_Base);
14828 Inherit_Rep_Item_Chain (T, Implicit_Base);
14829 Set_Digits_Value (T, Digs_Val);
14830 Set_Delta_Value (T, Delta_Val);
14831 Set_Small_Value (T, Delta_Val);
14832 Set_Scale_Value (T, Scale_Val);
14833 Set_Is_Constrained (T);
14834 end Decimal_Fixed_Point_Type_Declaration;
14835
14836 -----------------------------------
14837 -- Derive_Progenitor_Subprograms --
14838 -----------------------------------
14839
14840 procedure Derive_Progenitor_Subprograms
14841 (Parent_Type : Entity_Id;
14842 Tagged_Type : Entity_Id)
14843 is
14844 E : Entity_Id;
14845 Elmt : Elmt_Id;
14846 Iface : Entity_Id;
14847 Iface_Alias : Entity_Id;
14848 Iface_Elmt : Elmt_Id;
14849 Iface_Subp : Entity_Id;
14850 New_Subp : Entity_Id := Empty;
14851 Prim_Elmt : Elmt_Id;
14852 Subp : Entity_Id;
14853 Typ : Entity_Id;
14854
14855 begin
14856 pragma Assert (Ada_Version >= Ada_2005
14857 and then Is_Record_Type (Tagged_Type)
14858 and then Is_Tagged_Type (Tagged_Type)
14859 and then Has_Interfaces (Tagged_Type));
14860
14861 -- Step 1: Transfer to the full-view primitives associated with the
14862 -- partial-view that cover interface primitives. Conceptually this
14863 -- work should be done later by Process_Full_View; done here to
14864 -- simplify its implementation at later stages. It can be safely
14865 -- done here because interfaces must be visible in the partial and
14866 -- private view (RM 7.3(7.3/2)).
14867
14868 -- Small optimization: This work is only required if the parent may
14869 -- have entities whose Alias attribute reference an interface primitive.
14870 -- Such a situation may occur if the parent is an abstract type and the
14871 -- primitive has not been yet overridden or if the parent is a generic
14872 -- formal type covering interfaces.
14873
14874 -- If the tagged type is not abstract, it cannot have abstract
14875 -- primitives (the only entities in the list of primitives of
14876 -- non-abstract tagged types that can reference abstract primitives
14877 -- through its Alias attribute are the internal entities that have
14878 -- attribute Interface_Alias, and these entities are generated later
14879 -- by Add_Internal_Interface_Entities).
14880
14881 if In_Private_Part (Current_Scope)
14882 and then (Is_Abstract_Type (Parent_Type)
14883 or else
14884 Is_Generic_Type (Parent_Type))
14885 then
14886 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14887 while Present (Elmt) loop
14888 Subp := Node (Elmt);
14889
14890 -- At this stage it is not possible to have entities in the list
14891 -- of primitives that have attribute Interface_Alias.
14892
14893 pragma Assert (No (Interface_Alias (Subp)));
14894
14895 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14896
14897 if Is_Interface (Typ) then
14898 E := Find_Primitive_Covering_Interface
14899 (Tagged_Type => Tagged_Type,
14900 Iface_Prim => Subp);
14901
14902 if Present (E)
14903 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14904 then
14905 Replace_Elmt (Elmt, E);
14906 Remove_Homonym (Subp);
14907 end if;
14908 end if;
14909
14910 Next_Elmt (Elmt);
14911 end loop;
14912 end if;
14913
14914 -- Step 2: Add primitives of progenitors that are not implemented by
14915 -- parents of Tagged_Type.
14916
14917 if Present (Interfaces (Base_Type (Tagged_Type))) then
14918 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14919 while Present (Iface_Elmt) loop
14920 Iface := Node (Iface_Elmt);
14921
14922 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14923 while Present (Prim_Elmt) loop
14924 Iface_Subp := Node (Prim_Elmt);
14925 Iface_Alias := Ultimate_Alias (Iface_Subp);
14926
14927 -- Exclude derivation of predefined primitives except those
14928 -- that come from source, or are inherited from one that comes
14929 -- from source. Required to catch declarations of equality
14930 -- operators of interfaces. For example:
14931
14932 -- type Iface is interface;
14933 -- function "=" (Left, Right : Iface) return Boolean;
14934
14935 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14936 or else Comes_From_Source (Iface_Alias)
14937 then
14938 E :=
14939 Find_Primitive_Covering_Interface
14940 (Tagged_Type => Tagged_Type,
14941 Iface_Prim => Iface_Subp);
14942
14943 -- If not found we derive a new primitive leaving its alias
14944 -- attribute referencing the interface primitive.
14945
14946 if No (E) then
14947 Derive_Subprogram
14948 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14949
14950 -- Ada 2012 (AI05-0197): If the covering primitive's name
14951 -- differs from the name of the interface primitive then it
14952 -- is a private primitive inherited from a parent type. In
14953 -- such case, given that Tagged_Type covers the interface,
14954 -- the inherited private primitive becomes visible. For such
14955 -- purpose we add a new entity that renames the inherited
14956 -- private primitive.
14957
14958 elsif Chars (E) /= Chars (Iface_Subp) then
14959 pragma Assert (Has_Suffix (E, 'P'));
14960 Derive_Subprogram
14961 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14962 Set_Alias (New_Subp, E);
14963 Set_Is_Abstract_Subprogram (New_Subp,
14964 Is_Abstract_Subprogram (E));
14965
14966 -- Propagate to the full view interface entities associated
14967 -- with the partial view.
14968
14969 elsif In_Private_Part (Current_Scope)
14970 and then Present (Alias (E))
14971 and then Alias (E) = Iface_Subp
14972 and then
14973 List_Containing (Parent (E)) /=
14974 Private_Declarations
14975 (Specification
14976 (Unit_Declaration_Node (Current_Scope)))
14977 then
14978 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14979 end if;
14980 end if;
14981
14982 Next_Elmt (Prim_Elmt);
14983 end loop;
14984
14985 Next_Elmt (Iface_Elmt);
14986 end loop;
14987 end if;
14988 end Derive_Progenitor_Subprograms;
14989
14990 -----------------------
14991 -- Derive_Subprogram --
14992 -----------------------
14993
14994 procedure Derive_Subprogram
14995 (New_Subp : out Entity_Id;
14996 Parent_Subp : Entity_Id;
14997 Derived_Type : Entity_Id;
14998 Parent_Type : Entity_Id;
14999 Actual_Subp : Entity_Id := Empty)
15000 is
15001 Formal : Entity_Id;
15002 -- Formal parameter of parent primitive operation
15003
15004 Formal_Of_Actual : Entity_Id;
15005 -- Formal parameter of actual operation, when the derivation is to
15006 -- create a renaming for a primitive operation of an actual in an
15007 -- instantiation.
15008
15009 New_Formal : Entity_Id;
15010 -- Formal of inherited operation
15011
15012 Visible_Subp : Entity_Id := Parent_Subp;
15013
15014 function Is_Private_Overriding return Boolean;
15015 -- If Subp is a private overriding of a visible operation, the inherited
15016 -- operation derives from the overridden op (even though its body is the
15017 -- overriding one) and the inherited operation is visible now. See
15018 -- sem_disp to see the full details of the handling of the overridden
15019 -- subprogram, which is removed from the list of primitive operations of
15020 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15021 -- and used to diagnose abstract operations that need overriding in the
15022 -- derived type.
15023
15024 procedure Replace_Type (Id, New_Id : Entity_Id);
15025 -- When the type is an anonymous access type, create a new access type
15026 -- designating the derived type.
15027
15028 procedure Set_Derived_Name;
15029 -- This procedure sets the appropriate Chars name for New_Subp. This
15030 -- is normally just a copy of the parent name. An exception arises for
15031 -- type support subprograms, where the name is changed to reflect the
15032 -- name of the derived type, e.g. if type foo is derived from type bar,
15033 -- then a procedure barDA is derived with a name fooDA.
15034
15035 ---------------------------
15036 -- Is_Private_Overriding --
15037 ---------------------------
15038
15039 function Is_Private_Overriding return Boolean is
15040 Prev : Entity_Id;
15041
15042 begin
15043 -- If the parent is not a dispatching operation there is no
15044 -- need to investigate overridings
15045
15046 if not Is_Dispatching_Operation (Parent_Subp) then
15047 return False;
15048 end if;
15049
15050 -- The visible operation that is overridden is a homonym of the
15051 -- parent subprogram. We scan the homonym chain to find the one
15052 -- whose alias is the subprogram we are deriving.
15053
15054 Prev := Current_Entity (Parent_Subp);
15055 while Present (Prev) loop
15056 if Ekind (Prev) = Ekind (Parent_Subp)
15057 and then Alias (Prev) = Parent_Subp
15058 and then Scope (Parent_Subp) = Scope (Prev)
15059 and then not Is_Hidden (Prev)
15060 then
15061 Visible_Subp := Prev;
15062 return True;
15063 end if;
15064
15065 Prev := Homonym (Prev);
15066 end loop;
15067
15068 return False;
15069 end Is_Private_Overriding;
15070
15071 ------------------
15072 -- Replace_Type --
15073 ------------------
15074
15075 procedure Replace_Type (Id, New_Id : Entity_Id) is
15076 Id_Type : constant Entity_Id := Etype (Id);
15077 Acc_Type : Entity_Id;
15078 Par : constant Node_Id := Parent (Derived_Type);
15079
15080 begin
15081 -- When the type is an anonymous access type, create a new access
15082 -- type designating the derived type. This itype must be elaborated
15083 -- at the point of the derivation, not on subsequent calls that may
15084 -- be out of the proper scope for Gigi, so we insert a reference to
15085 -- it after the derivation.
15086
15087 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15088 declare
15089 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15090
15091 begin
15092 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15093 and then Present (Full_View (Desig_Typ))
15094 and then not Is_Private_Type (Parent_Type)
15095 then
15096 Desig_Typ := Full_View (Desig_Typ);
15097 end if;
15098
15099 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15100
15101 -- Ada 2005 (AI-251): Handle also derivations of abstract
15102 -- interface primitives.
15103
15104 or else (Is_Interface (Desig_Typ)
15105 and then not Is_Class_Wide_Type (Desig_Typ))
15106 then
15107 Acc_Type := New_Copy (Id_Type);
15108 Set_Etype (Acc_Type, Acc_Type);
15109 Set_Scope (Acc_Type, New_Subp);
15110
15111 -- Set size of anonymous access type. If we have an access
15112 -- to an unconstrained array, this is a fat pointer, so it
15113 -- is sizes at twice addtress size.
15114
15115 if Is_Array_Type (Desig_Typ)
15116 and then not Is_Constrained (Desig_Typ)
15117 then
15118 Init_Size (Acc_Type, 2 * System_Address_Size);
15119
15120 -- Other cases use a thin pointer
15121
15122 else
15123 Init_Size (Acc_Type, System_Address_Size);
15124 end if;
15125
15126 -- Set remaining characterstics of anonymous access type
15127
15128 Init_Alignment (Acc_Type);
15129 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15130
15131 Set_Etype (New_Id, Acc_Type);
15132 Set_Scope (New_Id, New_Subp);
15133
15134 -- Create a reference to it
15135
15136 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15137
15138 else
15139 Set_Etype (New_Id, Id_Type);
15140 end if;
15141 end;
15142
15143 -- In Ada2012, a formal may have an incomplete type but the type
15144 -- derivation that inherits the primitive follows the full view.
15145
15146 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15147 or else
15148 (Ekind (Id_Type) = E_Record_Type_With_Private
15149 and then Present (Full_View (Id_Type))
15150 and then
15151 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15152 or else
15153 (Ada_Version >= Ada_2012
15154 and then Ekind (Id_Type) = E_Incomplete_Type
15155 and then Full_View (Id_Type) = Parent_Type)
15156 then
15157 -- Constraint checks on formals are generated during expansion,
15158 -- based on the signature of the original subprogram. The bounds
15159 -- of the derived type are not relevant, and thus we can use
15160 -- the base type for the formals. However, the return type may be
15161 -- used in a context that requires that the proper static bounds
15162 -- be used (a case statement, for example) and for those cases
15163 -- we must use the derived type (first subtype), not its base.
15164
15165 -- If the derived_type_definition has no constraints, we know that
15166 -- the derived type has the same constraints as the first subtype
15167 -- of the parent, and we can also use it rather than its base,
15168 -- which can lead to more efficient code.
15169
15170 if Etype (Id) = Parent_Type then
15171 if Is_Scalar_Type (Parent_Type)
15172 and then
15173 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15174 then
15175 Set_Etype (New_Id, Derived_Type);
15176
15177 elsif Nkind (Par) = N_Full_Type_Declaration
15178 and then
15179 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15180 and then
15181 Is_Entity_Name
15182 (Subtype_Indication (Type_Definition (Par)))
15183 then
15184 Set_Etype (New_Id, Derived_Type);
15185
15186 else
15187 Set_Etype (New_Id, Base_Type (Derived_Type));
15188 end if;
15189
15190 else
15191 Set_Etype (New_Id, Base_Type (Derived_Type));
15192 end if;
15193
15194 else
15195 Set_Etype (New_Id, Etype (Id));
15196 end if;
15197 end Replace_Type;
15198
15199 ----------------------
15200 -- Set_Derived_Name --
15201 ----------------------
15202
15203 procedure Set_Derived_Name is
15204 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15205 begin
15206 if Nm = TSS_Null then
15207 Set_Chars (New_Subp, Chars (Parent_Subp));
15208 else
15209 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15210 end if;
15211 end Set_Derived_Name;
15212
15213 -- Start of processing for Derive_Subprogram
15214
15215 begin
15216 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15217 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15218
15219 -- Check whether the inherited subprogram is a private operation that
15220 -- should be inherited but not yet made visible. Such subprograms can
15221 -- become visible at a later point (e.g., the private part of a public
15222 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15223 -- following predicate is true, then this is not such a private
15224 -- operation and the subprogram simply inherits the name of the parent
15225 -- subprogram. Note the special check for the names of controlled
15226 -- operations, which are currently exempted from being inherited with
15227 -- a hidden name because they must be findable for generation of
15228 -- implicit run-time calls.
15229
15230 if not Is_Hidden (Parent_Subp)
15231 or else Is_Internal (Parent_Subp)
15232 or else Is_Private_Overriding
15233 or else Is_Internal_Name (Chars (Parent_Subp))
15234 or else (Is_Controlled (Parent_Type)
15235 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15236 Name_Finalize,
15237 Name_Initialize))
15238 then
15239 Set_Derived_Name;
15240
15241 -- An inherited dispatching equality will be overridden by an internally
15242 -- generated one, or by an explicit one, so preserve its name and thus
15243 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15244 -- private operation it may become invisible if the full view has
15245 -- progenitors, and the dispatch table will be malformed.
15246 -- We check that the type is limited to handle the anomalous declaration
15247 -- of Limited_Controlled, which is derived from a non-limited type, and
15248 -- which is handled specially elsewhere as well.
15249
15250 elsif Chars (Parent_Subp) = Name_Op_Eq
15251 and then Is_Dispatching_Operation (Parent_Subp)
15252 and then Etype (Parent_Subp) = Standard_Boolean
15253 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15254 and then
15255 Etype (First_Formal (Parent_Subp)) =
15256 Etype (Next_Formal (First_Formal (Parent_Subp)))
15257 then
15258 Set_Derived_Name;
15259
15260 -- If parent is hidden, this can be a regular derivation if the
15261 -- parent is immediately visible in a non-instantiating context,
15262 -- or if we are in the private part of an instance. This test
15263 -- should still be refined ???
15264
15265 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15266 -- operation as a non-visible operation in cases where the parent
15267 -- subprogram might not be visible now, but was visible within the
15268 -- original generic, so it would be wrong to make the inherited
15269 -- subprogram non-visible now. (Not clear if this test is fully
15270 -- correct; are there any cases where we should declare the inherited
15271 -- operation as not visible to avoid it being overridden, e.g., when
15272 -- the parent type is a generic actual with private primitives ???)
15273
15274 -- (they should be treated the same as other private inherited
15275 -- subprograms, but it's not clear how to do this cleanly). ???
15276
15277 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15278 and then Is_Immediately_Visible (Parent_Subp)
15279 and then not In_Instance)
15280 or else In_Instance_Not_Visible
15281 then
15282 Set_Derived_Name;
15283
15284 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15285 -- overrides an interface primitive because interface primitives
15286 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15287
15288 elsif Ada_Version >= Ada_2005
15289 and then Is_Dispatching_Operation (Parent_Subp)
15290 and then Present (Covered_Interface_Op (Parent_Subp))
15291 then
15292 Set_Derived_Name;
15293
15294 -- Otherwise, the type is inheriting a private operation, so enter it
15295 -- with a special name so it can't be overridden. See also below, where
15296 -- we check for this case, and if so avoid setting Requires_Overriding.
15297
15298 else
15299 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15300 end if;
15301
15302 Set_Parent (New_Subp, Parent (Derived_Type));
15303
15304 if Present (Actual_Subp) then
15305 Replace_Type (Actual_Subp, New_Subp);
15306 else
15307 Replace_Type (Parent_Subp, New_Subp);
15308 end if;
15309
15310 Conditional_Delay (New_Subp, Parent_Subp);
15311
15312 -- If we are creating a renaming for a primitive operation of an
15313 -- actual of a generic derived type, we must examine the signature
15314 -- of the actual primitive, not that of the generic formal, which for
15315 -- example may be an interface. However the name and initial value
15316 -- of the inherited operation are those of the formal primitive.
15317
15318 Formal := First_Formal (Parent_Subp);
15319
15320 if Present (Actual_Subp) then
15321 Formal_Of_Actual := First_Formal (Actual_Subp);
15322 else
15323 Formal_Of_Actual := Empty;
15324 end if;
15325
15326 while Present (Formal) loop
15327 New_Formal := New_Copy (Formal);
15328
15329 -- Normally we do not go copying parents, but in the case of
15330 -- formals, we need to link up to the declaration (which is the
15331 -- parameter specification), and it is fine to link up to the
15332 -- original formal's parameter specification in this case.
15333
15334 Set_Parent (New_Formal, Parent (Formal));
15335 Append_Entity (New_Formal, New_Subp);
15336
15337 if Present (Formal_Of_Actual) then
15338 Replace_Type (Formal_Of_Actual, New_Formal);
15339 Next_Formal (Formal_Of_Actual);
15340 else
15341 Replace_Type (Formal, New_Formal);
15342 end if;
15343
15344 Next_Formal (Formal);
15345 end loop;
15346
15347 -- If this derivation corresponds to a tagged generic actual, then
15348 -- primitive operations rename those of the actual. Otherwise the
15349 -- primitive operations rename those of the parent type, If the parent
15350 -- renames an intrinsic operator, so does the new subprogram. We except
15351 -- concatenation, which is always properly typed, and does not get
15352 -- expanded as other intrinsic operations.
15353
15354 if No (Actual_Subp) then
15355 if Is_Intrinsic_Subprogram (Parent_Subp) then
15356 Set_Is_Intrinsic_Subprogram (New_Subp);
15357
15358 if Present (Alias (Parent_Subp))
15359 and then Chars (Parent_Subp) /= Name_Op_Concat
15360 then
15361 Set_Alias (New_Subp, Alias (Parent_Subp));
15362 else
15363 Set_Alias (New_Subp, Parent_Subp);
15364 end if;
15365
15366 else
15367 Set_Alias (New_Subp, Parent_Subp);
15368 end if;
15369
15370 else
15371 Set_Alias (New_Subp, Actual_Subp);
15372 end if;
15373
15374 -- Derived subprograms of a tagged type must inherit the convention
15375 -- of the parent subprogram (a requirement of AI-117). Derived
15376 -- subprograms of untagged types simply get convention Ada by default.
15377
15378 -- If the derived type is a tagged generic formal type with unknown
15379 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15380
15381 -- However, if the type is derived from a generic formal, the further
15382 -- inherited subprogram has the convention of the non-generic ancestor.
15383 -- Otherwise there would be no way to override the operation.
15384 -- (This is subject to forthcoming ARG discussions).
15385
15386 if Is_Tagged_Type (Derived_Type) then
15387 if Is_Generic_Type (Derived_Type)
15388 and then Has_Unknown_Discriminants (Derived_Type)
15389 then
15390 Set_Convention (New_Subp, Convention_Intrinsic);
15391
15392 else
15393 if Is_Generic_Type (Parent_Type)
15394 and then Has_Unknown_Discriminants (Parent_Type)
15395 then
15396 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15397 else
15398 Set_Convention (New_Subp, Convention (Parent_Subp));
15399 end if;
15400 end if;
15401 end if;
15402
15403 -- Predefined controlled operations retain their name even if the parent
15404 -- is hidden (see above), but they are not primitive operations if the
15405 -- ancestor is not visible, for example if the parent is a private
15406 -- extension completed with a controlled extension. Note that a full
15407 -- type that is controlled can break privacy: the flag Is_Controlled is
15408 -- set on both views of the type.
15409
15410 if Is_Controlled (Parent_Type)
15411 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15412 Name_Adjust,
15413 Name_Finalize)
15414 and then Is_Hidden (Parent_Subp)
15415 and then not Is_Visibly_Controlled (Parent_Type)
15416 then
15417 Set_Is_Hidden (New_Subp);
15418 end if;
15419
15420 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15421 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15422
15423 if Ekind (Parent_Subp) = E_Procedure then
15424 Set_Is_Valued_Procedure
15425 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15426 else
15427 Set_Has_Controlling_Result
15428 (New_Subp, Has_Controlling_Result (Parent_Subp));
15429 end if;
15430
15431 -- No_Return must be inherited properly. If this is overridden in the
15432 -- case of a dispatching operation, then a check is made in Sem_Disp
15433 -- that the overriding operation is also No_Return (no such check is
15434 -- required for the case of non-dispatching operation.
15435
15436 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15437
15438 -- A derived function with a controlling result is abstract. If the
15439 -- Derived_Type is a nonabstract formal generic derived type, then
15440 -- inherited operations are not abstract: the required check is done at
15441 -- instantiation time. If the derivation is for a generic actual, the
15442 -- function is not abstract unless the actual is.
15443
15444 if Is_Generic_Type (Derived_Type)
15445 and then not Is_Abstract_Type (Derived_Type)
15446 then
15447 null;
15448
15449 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15450 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15451
15452 -- A subprogram subject to pragma Extensions_Visible with value False
15453 -- requires overriding if the subprogram has at least one controlling
15454 -- OUT parameter (SPARK RM 6.1.7(6)).
15455
15456 elsif Ada_Version >= Ada_2005
15457 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15458 or else (Is_Tagged_Type (Derived_Type)
15459 and then Etype (New_Subp) = Derived_Type
15460 and then not Is_Null_Extension (Derived_Type))
15461 or else (Is_Tagged_Type (Derived_Type)
15462 and then Ekind (Etype (New_Subp)) =
15463 E_Anonymous_Access_Type
15464 and then Designated_Type (Etype (New_Subp)) =
15465 Derived_Type
15466 and then not Is_Null_Extension (Derived_Type))
15467 or else (Comes_From_Source (Alias (New_Subp))
15468 and then Is_EVF_Procedure (Alias (New_Subp))))
15469 and then No (Actual_Subp)
15470 then
15471 if not Is_Tagged_Type (Derived_Type)
15472 or else Is_Abstract_Type (Derived_Type)
15473 or else Is_Abstract_Subprogram (Alias (New_Subp))
15474 then
15475 Set_Is_Abstract_Subprogram (New_Subp);
15476
15477 -- If the Chars of the new subprogram is different from that of the
15478 -- parent's one, it means that we entered it with a special name so
15479 -- it can't be overridden (see above). In that case we had better not
15480 -- *require* it to be overridden. This is the case where the parent
15481 -- type inherited the operation privately, so there's no danger of
15482 -- dangling dispatching.
15483
15484 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
15485 Set_Requires_Overriding (New_Subp);
15486 end if;
15487
15488 elsif Ada_Version < Ada_2005
15489 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15490 or else (Is_Tagged_Type (Derived_Type)
15491 and then Etype (New_Subp) = Derived_Type
15492 and then No (Actual_Subp)))
15493 then
15494 Set_Is_Abstract_Subprogram (New_Subp);
15495
15496 -- AI05-0097 : an inherited operation that dispatches on result is
15497 -- abstract if the derived type is abstract, even if the parent type
15498 -- is concrete and the derived type is a null extension.
15499
15500 elsif Has_Controlling_Result (Alias (New_Subp))
15501 and then Is_Abstract_Type (Etype (New_Subp))
15502 then
15503 Set_Is_Abstract_Subprogram (New_Subp);
15504
15505 -- Finally, if the parent type is abstract we must verify that all
15506 -- inherited operations are either non-abstract or overridden, or that
15507 -- the derived type itself is abstract (this check is performed at the
15508 -- end of a package declaration, in Check_Abstract_Overriding). A
15509 -- private overriding in the parent type will not be visible in the
15510 -- derivation if we are not in an inner package or in a child unit of
15511 -- the parent type, in which case the abstractness of the inherited
15512 -- operation is carried to the new subprogram.
15513
15514 elsif Is_Abstract_Type (Parent_Type)
15515 and then not In_Open_Scopes (Scope (Parent_Type))
15516 and then Is_Private_Overriding
15517 and then Is_Abstract_Subprogram (Visible_Subp)
15518 then
15519 if No (Actual_Subp) then
15520 Set_Alias (New_Subp, Visible_Subp);
15521 Set_Is_Abstract_Subprogram (New_Subp, True);
15522
15523 else
15524 -- If this is a derivation for an instance of a formal derived
15525 -- type, abstractness comes from the primitive operation of the
15526 -- actual, not from the operation inherited from the ancestor.
15527
15528 Set_Is_Abstract_Subprogram
15529 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15530 end if;
15531 end if;
15532
15533 New_Overloaded_Entity (New_Subp, Derived_Type);
15534
15535 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15536 -- preconditions and the derived type is abstract, the derived operation
15537 -- is abstract as well if parent subprogram is not abstract or null.
15538
15539 if Is_Abstract_Type (Derived_Type)
15540 and then Has_Non_Trivial_Precondition (Parent_Subp)
15541 and then Present (Interfaces (Derived_Type))
15542 then
15543
15544 -- Add useful attributes of subprogram before the freeze point,
15545 -- in case freezing is delayed or there are previous errors.
15546
15547 Set_Is_Dispatching_Operation (New_Subp);
15548
15549 declare
15550 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15551
15552 begin
15553 if Present (Iface_Prim)
15554 and then Has_Non_Trivial_Precondition (Iface_Prim)
15555 then
15556 Set_Is_Abstract_Subprogram (New_Subp);
15557 end if;
15558 end;
15559 end if;
15560
15561 -- Check for case of a derived subprogram for the instantiation of a
15562 -- formal derived tagged type, if so mark the subprogram as dispatching
15563 -- and inherit the dispatching attributes of the actual subprogram. The
15564 -- derived subprogram is effectively renaming of the actual subprogram,
15565 -- so it needs to have the same attributes as the actual.
15566
15567 if Present (Actual_Subp)
15568 and then Is_Dispatching_Operation (Actual_Subp)
15569 then
15570 Set_Is_Dispatching_Operation (New_Subp);
15571
15572 if Present (DTC_Entity (Actual_Subp)) then
15573 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15574 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15575 end if;
15576 end if;
15577
15578 -- Indicate that a derived subprogram does not require a body and that
15579 -- it does not require processing of default expressions.
15580
15581 Set_Has_Completion (New_Subp);
15582 Set_Default_Expressions_Processed (New_Subp);
15583
15584 if Ekind (New_Subp) = E_Function then
15585 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15586 end if;
15587 end Derive_Subprogram;
15588
15589 ------------------------
15590 -- Derive_Subprograms --
15591 ------------------------
15592
15593 procedure Derive_Subprograms
15594 (Parent_Type : Entity_Id;
15595 Derived_Type : Entity_Id;
15596 Generic_Actual : Entity_Id := Empty)
15597 is
15598 Op_List : constant Elist_Id :=
15599 Collect_Primitive_Operations (Parent_Type);
15600
15601 function Check_Derived_Type return Boolean;
15602 -- Check that all the entities derived from Parent_Type are found in
15603 -- the list of primitives of Derived_Type exactly in the same order.
15604
15605 procedure Derive_Interface_Subprogram
15606 (New_Subp : out Entity_Id;
15607 Subp : Entity_Id;
15608 Actual_Subp : Entity_Id);
15609 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15610 -- (which is an interface primitive). If Generic_Actual is present then
15611 -- Actual_Subp is the actual subprogram corresponding with the generic
15612 -- subprogram Subp.
15613
15614 ------------------------
15615 -- Check_Derived_Type --
15616 ------------------------
15617
15618 function Check_Derived_Type return Boolean is
15619 E : Entity_Id;
15620 Elmt : Elmt_Id;
15621 List : Elist_Id;
15622 New_Subp : Entity_Id;
15623 Op_Elmt : Elmt_Id;
15624 Subp : Entity_Id;
15625
15626 begin
15627 -- Traverse list of entities in the current scope searching for
15628 -- an incomplete type whose full-view is derived type.
15629
15630 E := First_Entity (Scope (Derived_Type));
15631 while Present (E) and then E /= Derived_Type loop
15632 if Ekind (E) = E_Incomplete_Type
15633 and then Present (Full_View (E))
15634 and then Full_View (E) = Derived_Type
15635 then
15636 -- Disable this test if Derived_Type completes an incomplete
15637 -- type because in such case more primitives can be added
15638 -- later to the list of primitives of Derived_Type by routine
15639 -- Process_Incomplete_Dependents
15640
15641 return True;
15642 end if;
15643
15644 Next_Entity (E);
15645 end loop;
15646
15647 List := Collect_Primitive_Operations (Derived_Type);
15648 Elmt := First_Elmt (List);
15649
15650 Op_Elmt := First_Elmt (Op_List);
15651 while Present (Op_Elmt) loop
15652 Subp := Node (Op_Elmt);
15653 New_Subp := Node (Elmt);
15654
15655 -- At this early stage Derived_Type has no entities with attribute
15656 -- Interface_Alias. In addition, such primitives are always
15657 -- located at the end of the list of primitives of Parent_Type.
15658 -- Therefore, if found we can safely stop processing pending
15659 -- entities.
15660
15661 exit when Present (Interface_Alias (Subp));
15662
15663 -- Handle hidden entities
15664
15665 if not Is_Predefined_Dispatching_Operation (Subp)
15666 and then Is_Hidden (Subp)
15667 then
15668 if Present (New_Subp)
15669 and then Primitive_Names_Match (Subp, New_Subp)
15670 then
15671 Next_Elmt (Elmt);
15672 end if;
15673
15674 else
15675 if not Present (New_Subp)
15676 or else Ekind (Subp) /= Ekind (New_Subp)
15677 or else not Primitive_Names_Match (Subp, New_Subp)
15678 then
15679 return False;
15680 end if;
15681
15682 Next_Elmt (Elmt);
15683 end if;
15684
15685 Next_Elmt (Op_Elmt);
15686 end loop;
15687
15688 return True;
15689 end Check_Derived_Type;
15690
15691 ---------------------------------
15692 -- Derive_Interface_Subprogram --
15693 ---------------------------------
15694
15695 procedure Derive_Interface_Subprogram
15696 (New_Subp : out Entity_Id;
15697 Subp : Entity_Id;
15698 Actual_Subp : Entity_Id)
15699 is
15700 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15701 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15702
15703 begin
15704 pragma Assert (Is_Interface (Iface_Type));
15705
15706 Derive_Subprogram
15707 (New_Subp => New_Subp,
15708 Parent_Subp => Iface_Subp,
15709 Derived_Type => Derived_Type,
15710 Parent_Type => Iface_Type,
15711 Actual_Subp => Actual_Subp);
15712
15713 -- Given that this new interface entity corresponds with a primitive
15714 -- of the parent that was not overridden we must leave it associated
15715 -- with its parent primitive to ensure that it will share the same
15716 -- dispatch table slot when overridden. We must set the Alias to Subp
15717 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15718 -- (in case we inherited Subp from Iface_Type via a nonabstract
15719 -- generic formal type).
15720
15721 if No (Actual_Subp) then
15722 Set_Alias (New_Subp, Subp);
15723
15724 declare
15725 T : Entity_Id := Find_Dispatching_Type (Subp);
15726 begin
15727 while Etype (T) /= T loop
15728 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15729 Set_Is_Abstract_Subprogram (New_Subp, False);
15730 exit;
15731 end if;
15732
15733 T := Etype (T);
15734 end loop;
15735 end;
15736
15737 -- For instantiations this is not needed since the previous call to
15738 -- Derive_Subprogram leaves the entity well decorated.
15739
15740 else
15741 pragma Assert (Alias (New_Subp) = Actual_Subp);
15742 null;
15743 end if;
15744 end Derive_Interface_Subprogram;
15745
15746 -- Local variables
15747
15748 Alias_Subp : Entity_Id;
15749 Act_List : Elist_Id;
15750 Act_Elmt : Elmt_Id;
15751 Act_Subp : Entity_Id := Empty;
15752 Elmt : Elmt_Id;
15753 Need_Search : Boolean := False;
15754 New_Subp : Entity_Id := Empty;
15755 Parent_Base : Entity_Id;
15756 Subp : Entity_Id;
15757
15758 -- Start of processing for Derive_Subprograms
15759
15760 begin
15761 if Ekind (Parent_Type) = E_Record_Type_With_Private
15762 and then Has_Discriminants (Parent_Type)
15763 and then Present (Full_View (Parent_Type))
15764 then
15765 Parent_Base := Full_View (Parent_Type);
15766 else
15767 Parent_Base := Parent_Type;
15768 end if;
15769
15770 if Present (Generic_Actual) then
15771 Act_List := Collect_Primitive_Operations (Generic_Actual);
15772 Act_Elmt := First_Elmt (Act_List);
15773 else
15774 Act_List := No_Elist;
15775 Act_Elmt := No_Elmt;
15776 end if;
15777
15778 -- Derive primitives inherited from the parent. Note that if the generic
15779 -- actual is present, this is not really a type derivation, it is a
15780 -- completion within an instance.
15781
15782 -- Case 1: Derived_Type does not implement interfaces
15783
15784 if not Is_Tagged_Type (Derived_Type)
15785 or else (not Has_Interfaces (Derived_Type)
15786 and then not (Present (Generic_Actual)
15787 and then Has_Interfaces (Generic_Actual)))
15788 then
15789 Elmt := First_Elmt (Op_List);
15790 while Present (Elmt) loop
15791 Subp := Node (Elmt);
15792
15793 -- Literals are derived earlier in the process of building the
15794 -- derived type, and are skipped here.
15795
15796 if Ekind (Subp) = E_Enumeration_Literal then
15797 null;
15798
15799 -- The actual is a direct descendant and the common primitive
15800 -- operations appear in the same order.
15801
15802 -- If the generic parent type is present, the derived type is an
15803 -- instance of a formal derived type, and within the instance its
15804 -- operations are those of the actual. We derive from the formal
15805 -- type but make the inherited operations aliases of the
15806 -- corresponding operations of the actual.
15807
15808 else
15809 pragma Assert (No (Node (Act_Elmt))
15810 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15811 and then
15812 Type_Conformant
15813 (Subp, Node (Act_Elmt),
15814 Skip_Controlling_Formals => True)));
15815
15816 Derive_Subprogram
15817 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15818
15819 if Present (Act_Elmt) then
15820 Next_Elmt (Act_Elmt);
15821 end if;
15822 end if;
15823
15824 Next_Elmt (Elmt);
15825 end loop;
15826
15827 -- Case 2: Derived_Type implements interfaces
15828
15829 else
15830 -- If the parent type has no predefined primitives we remove
15831 -- predefined primitives from the list of primitives of generic
15832 -- actual to simplify the complexity of this algorithm.
15833
15834 if Present (Generic_Actual) then
15835 declare
15836 Has_Predefined_Primitives : Boolean := False;
15837
15838 begin
15839 -- Check if the parent type has predefined primitives
15840
15841 Elmt := First_Elmt (Op_List);
15842 while Present (Elmt) loop
15843 Subp := Node (Elmt);
15844
15845 if Is_Predefined_Dispatching_Operation (Subp)
15846 and then not Comes_From_Source (Ultimate_Alias (Subp))
15847 then
15848 Has_Predefined_Primitives := True;
15849 exit;
15850 end if;
15851
15852 Next_Elmt (Elmt);
15853 end loop;
15854
15855 -- Remove predefined primitives of Generic_Actual. We must use
15856 -- an auxiliary list because in case of tagged types the value
15857 -- returned by Collect_Primitive_Operations is the value stored
15858 -- in its Primitive_Operations attribute (and we don't want to
15859 -- modify its current contents).
15860
15861 if not Has_Predefined_Primitives then
15862 declare
15863 Aux_List : constant Elist_Id := New_Elmt_List;
15864
15865 begin
15866 Elmt := First_Elmt (Act_List);
15867 while Present (Elmt) loop
15868 Subp := Node (Elmt);
15869
15870 if not Is_Predefined_Dispatching_Operation (Subp)
15871 or else Comes_From_Source (Subp)
15872 then
15873 Append_Elmt (Subp, Aux_List);
15874 end if;
15875
15876 Next_Elmt (Elmt);
15877 end loop;
15878
15879 Act_List := Aux_List;
15880 end;
15881 end if;
15882
15883 Act_Elmt := First_Elmt (Act_List);
15884 Act_Subp := Node (Act_Elmt);
15885 end;
15886 end if;
15887
15888 -- Stage 1: If the generic actual is not present we derive the
15889 -- primitives inherited from the parent type. If the generic parent
15890 -- type is present, the derived type is an instance of a formal
15891 -- derived type, and within the instance its operations are those of
15892 -- the actual. We derive from the formal type but make the inherited
15893 -- operations aliases of the corresponding operations of the actual.
15894
15895 Elmt := First_Elmt (Op_List);
15896 while Present (Elmt) loop
15897 Subp := Node (Elmt);
15898 Alias_Subp := Ultimate_Alias (Subp);
15899
15900 -- Do not derive internal entities of the parent that link
15901 -- interface primitives with their covering primitive. These
15902 -- entities will be added to this type when frozen.
15903
15904 if Present (Interface_Alias (Subp)) then
15905 goto Continue;
15906 end if;
15907
15908 -- If the generic actual is present find the corresponding
15909 -- operation in the generic actual. If the parent type is a
15910 -- direct ancestor of the derived type then, even if it is an
15911 -- interface, the operations are inherited from the primary
15912 -- dispatch table and are in the proper order. If we detect here
15913 -- that primitives are not in the same order we traverse the list
15914 -- of primitive operations of the actual to find the one that
15915 -- implements the interface primitive.
15916
15917 if Need_Search
15918 or else
15919 (Present (Generic_Actual)
15920 and then Present (Act_Subp)
15921 and then not
15922 (Primitive_Names_Match (Subp, Act_Subp)
15923 and then
15924 Type_Conformant (Subp, Act_Subp,
15925 Skip_Controlling_Formals => True)))
15926 then
15927 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15928 Use_Full_View => True));
15929
15930 -- Remember that we need searching for all pending primitives
15931
15932 Need_Search := True;
15933
15934 -- Handle entities associated with interface primitives
15935
15936 if Present (Alias_Subp)
15937 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15938 and then not Is_Predefined_Dispatching_Operation (Subp)
15939 then
15940 -- Search for the primitive in the homonym chain
15941
15942 Act_Subp :=
15943 Find_Primitive_Covering_Interface
15944 (Tagged_Type => Generic_Actual,
15945 Iface_Prim => Alias_Subp);
15946
15947 -- Previous search may not locate primitives covering
15948 -- interfaces defined in generics units or instantiations.
15949 -- (it fails if the covering primitive has formals whose
15950 -- type is also defined in generics or instantiations).
15951 -- In such case we search in the list of primitives of the
15952 -- generic actual for the internal entity that links the
15953 -- interface primitive and the covering primitive.
15954
15955 if No (Act_Subp)
15956 and then Is_Generic_Type (Parent_Type)
15957 then
15958 -- This code has been designed to handle only generic
15959 -- formals that implement interfaces that are defined
15960 -- in a generic unit or instantiation. If this code is
15961 -- needed for other cases we must review it because
15962 -- (given that it relies on Original_Location to locate
15963 -- the primitive of Generic_Actual that covers the
15964 -- interface) it could leave linked through attribute
15965 -- Alias entities of unrelated instantiations).
15966
15967 pragma Assert
15968 (Is_Generic_Unit
15969 (Scope (Find_Dispatching_Type (Alias_Subp)))
15970 or else
15971 Instantiation_Depth
15972 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15973
15974 declare
15975 Iface_Prim_Loc : constant Source_Ptr :=
15976 Original_Location (Sloc (Alias_Subp));
15977
15978 Elmt : Elmt_Id;
15979 Prim : Entity_Id;
15980
15981 begin
15982 Elmt :=
15983 First_Elmt (Primitive_Operations (Generic_Actual));
15984
15985 Search : while Present (Elmt) loop
15986 Prim := Node (Elmt);
15987
15988 if Present (Interface_Alias (Prim))
15989 and then Original_Location
15990 (Sloc (Interface_Alias (Prim))) =
15991 Iface_Prim_Loc
15992 then
15993 Act_Subp := Alias (Prim);
15994 exit Search;
15995 end if;
15996
15997 Next_Elmt (Elmt);
15998 end loop Search;
15999 end;
16000 end if;
16001
16002 pragma Assert (Present (Act_Subp)
16003 or else Is_Abstract_Type (Generic_Actual)
16004 or else Serious_Errors_Detected > 0);
16005
16006 -- Handle predefined primitives plus the rest of user-defined
16007 -- primitives
16008
16009 else
16010 Act_Elmt := First_Elmt (Act_List);
16011 while Present (Act_Elmt) loop
16012 Act_Subp := Node (Act_Elmt);
16013
16014 exit when Primitive_Names_Match (Subp, Act_Subp)
16015 and then Type_Conformant
16016 (Subp, Act_Subp,
16017 Skip_Controlling_Formals => True)
16018 and then No (Interface_Alias (Act_Subp));
16019
16020 Next_Elmt (Act_Elmt);
16021 end loop;
16022
16023 if No (Act_Elmt) then
16024 Act_Subp := Empty;
16025 end if;
16026 end if;
16027 end if;
16028
16029 -- Case 1: If the parent is a limited interface then it has the
16030 -- predefined primitives of synchronized interfaces. However, the
16031 -- actual type may be a non-limited type and hence it does not
16032 -- have such primitives.
16033
16034 if Present (Generic_Actual)
16035 and then not Present (Act_Subp)
16036 and then Is_Limited_Interface (Parent_Base)
16037 and then Is_Predefined_Interface_Primitive (Subp)
16038 then
16039 null;
16040
16041 -- Case 2: Inherit entities associated with interfaces that were
16042 -- not covered by the parent type. We exclude here null interface
16043 -- primitives because they do not need special management.
16044
16045 -- We also exclude interface operations that are renamings. If the
16046 -- subprogram is an explicit renaming of an interface primitive,
16047 -- it is a regular primitive operation, and the presence of its
16048 -- alias is not relevant: it has to be derived like any other
16049 -- primitive.
16050
16051 elsif Present (Alias (Subp))
16052 and then Nkind (Unit_Declaration_Node (Subp)) /=
16053 N_Subprogram_Renaming_Declaration
16054 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16055 and then not
16056 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16057 and then Null_Present (Parent (Alias_Subp)))
16058 then
16059 -- If this is an abstract private type then we transfer the
16060 -- derivation of the interface primitive from the partial view
16061 -- to the full view. This is safe because all the interfaces
16062 -- must be visible in the partial view. Done to avoid adding
16063 -- a new interface derivation to the private part of the
16064 -- enclosing package; otherwise this new derivation would be
16065 -- decorated as hidden when the analysis of the enclosing
16066 -- package completes.
16067
16068 if Is_Abstract_Type (Derived_Type)
16069 and then In_Private_Part (Current_Scope)
16070 and then Has_Private_Declaration (Derived_Type)
16071 then
16072 declare
16073 Partial_View : Entity_Id;
16074 Elmt : Elmt_Id;
16075 Ent : Entity_Id;
16076
16077 begin
16078 Partial_View := First_Entity (Current_Scope);
16079 loop
16080 exit when No (Partial_View)
16081 or else (Has_Private_Declaration (Partial_View)
16082 and then
16083 Full_View (Partial_View) = Derived_Type);
16084
16085 Next_Entity (Partial_View);
16086 end loop;
16087
16088 -- If the partial view was not found then the source code
16089 -- has errors and the derivation is not needed.
16090
16091 if Present (Partial_View) then
16092 Elmt :=
16093 First_Elmt (Primitive_Operations (Partial_View));
16094 while Present (Elmt) loop
16095 Ent := Node (Elmt);
16096
16097 if Present (Alias (Ent))
16098 and then Ultimate_Alias (Ent) = Alias (Subp)
16099 then
16100 Append_Elmt
16101 (Ent, Primitive_Operations (Derived_Type));
16102 exit;
16103 end if;
16104
16105 Next_Elmt (Elmt);
16106 end loop;
16107
16108 -- If the interface primitive was not found in the
16109 -- partial view then this interface primitive was
16110 -- overridden. We add a derivation to activate in
16111 -- Derive_Progenitor_Subprograms the machinery to
16112 -- search for it.
16113
16114 if No (Elmt) then
16115 Derive_Interface_Subprogram
16116 (New_Subp => New_Subp,
16117 Subp => Subp,
16118 Actual_Subp => Act_Subp);
16119 end if;
16120 end if;
16121 end;
16122 else
16123 Derive_Interface_Subprogram
16124 (New_Subp => New_Subp,
16125 Subp => Subp,
16126 Actual_Subp => Act_Subp);
16127 end if;
16128
16129 -- Case 3: Common derivation
16130
16131 else
16132 Derive_Subprogram
16133 (New_Subp => New_Subp,
16134 Parent_Subp => Subp,
16135 Derived_Type => Derived_Type,
16136 Parent_Type => Parent_Base,
16137 Actual_Subp => Act_Subp);
16138 end if;
16139
16140 -- No need to update Act_Elm if we must search for the
16141 -- corresponding operation in the generic actual
16142
16143 if not Need_Search
16144 and then Present (Act_Elmt)
16145 then
16146 Next_Elmt (Act_Elmt);
16147 Act_Subp := Node (Act_Elmt);
16148 end if;
16149
16150 <<Continue>>
16151 Next_Elmt (Elmt);
16152 end loop;
16153
16154 -- Inherit additional operations from progenitors. If the derived
16155 -- type is a generic actual, there are not new primitive operations
16156 -- for the type because it has those of the actual, and therefore
16157 -- nothing needs to be done. The renamings generated above are not
16158 -- primitive operations, and their purpose is simply to make the
16159 -- proper operations visible within an instantiation.
16160
16161 if No (Generic_Actual) then
16162 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16163 end if;
16164 end if;
16165
16166 -- Final check: Direct descendants must have their primitives in the
16167 -- same order. We exclude from this test untagged types and instances
16168 -- of formal derived types. We skip this test if we have already
16169 -- reported serious errors in the sources.
16170
16171 pragma Assert (not Is_Tagged_Type (Derived_Type)
16172 or else Present (Generic_Actual)
16173 or else Serious_Errors_Detected > 0
16174 or else Check_Derived_Type);
16175 end Derive_Subprograms;
16176
16177 --------------------------------
16178 -- Derived_Standard_Character --
16179 --------------------------------
16180
16181 procedure Derived_Standard_Character
16182 (N : Node_Id;
16183 Parent_Type : Entity_Id;
16184 Derived_Type : Entity_Id)
16185 is
16186 Loc : constant Source_Ptr := Sloc (N);
16187 Def : constant Node_Id := Type_Definition (N);
16188 Indic : constant Node_Id := Subtype_Indication (Def);
16189 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16190 Implicit_Base : constant Entity_Id :=
16191 Create_Itype
16192 (E_Enumeration_Type, N, Derived_Type, 'B');
16193
16194 Lo : Node_Id;
16195 Hi : Node_Id;
16196
16197 begin
16198 Discard_Node (Process_Subtype (Indic, N));
16199
16200 Set_Etype (Implicit_Base, Parent_Base);
16201 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16202 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16203
16204 Set_Is_Character_Type (Implicit_Base, True);
16205 Set_Has_Delayed_Freeze (Implicit_Base);
16206
16207 -- The bounds of the implicit base are the bounds of the parent base.
16208 -- Note that their type is the parent base.
16209
16210 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16211 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16212
16213 Set_Scalar_Range (Implicit_Base,
16214 Make_Range (Loc,
16215 Low_Bound => Lo,
16216 High_Bound => Hi));
16217
16218 Conditional_Delay (Derived_Type, Parent_Type);
16219
16220 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16221 Set_Etype (Derived_Type, Implicit_Base);
16222 Set_Size_Info (Derived_Type, Parent_Type);
16223
16224 if Unknown_RM_Size (Derived_Type) then
16225 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16226 end if;
16227
16228 Set_Is_Character_Type (Derived_Type, True);
16229
16230 if Nkind (Indic) /= N_Subtype_Indication then
16231
16232 -- If no explicit constraint, the bounds are those
16233 -- of the parent type.
16234
16235 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16236 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16237 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16238 end if;
16239
16240 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16241
16242 -- Because the implicit base is used in the conversion of the bounds, we
16243 -- have to freeze it now. This is similar to what is done for numeric
16244 -- types, and it equally suspicious, but otherwise a nonstatic bound
16245 -- will have a reference to an unfrozen type, which is rejected by Gigi
16246 -- (???). This requires specific care for definition of stream
16247 -- attributes. For details, see comments at the end of
16248 -- Build_Derived_Numeric_Type.
16249
16250 Freeze_Before (N, Implicit_Base);
16251 end Derived_Standard_Character;
16252
16253 ------------------------------
16254 -- Derived_Type_Declaration --
16255 ------------------------------
16256
16257 procedure Derived_Type_Declaration
16258 (T : Entity_Id;
16259 N : Node_Id;
16260 Is_Completion : Boolean)
16261 is
16262 Parent_Type : Entity_Id;
16263
16264 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16265 -- Check whether the parent type is a generic formal, or derives
16266 -- directly or indirectly from one.
16267
16268 ------------------------
16269 -- Comes_From_Generic --
16270 ------------------------
16271
16272 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16273 begin
16274 if Is_Generic_Type (Typ) then
16275 return True;
16276
16277 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16278 return True;
16279
16280 elsif Is_Private_Type (Typ)
16281 and then Present (Full_View (Typ))
16282 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16283 then
16284 return True;
16285
16286 elsif Is_Generic_Actual_Type (Typ) then
16287 return True;
16288
16289 else
16290 return False;
16291 end if;
16292 end Comes_From_Generic;
16293
16294 -- Local variables
16295
16296 Def : constant Node_Id := Type_Definition (N);
16297 Iface_Def : Node_Id;
16298 Indic : constant Node_Id := Subtype_Indication (Def);
16299 Extension : constant Node_Id := Record_Extension_Part (Def);
16300 Parent_Node : Node_Id;
16301 Taggd : Boolean;
16302
16303 -- Start of processing for Derived_Type_Declaration
16304
16305 begin
16306 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16307
16308 if SPARK_Mode = On
16309 and then Is_Tagged_Type (Parent_Type)
16310 then
16311 declare
16312 Partial_View : constant Entity_Id :=
16313 Incomplete_Or_Partial_View (Parent_Type);
16314
16315 begin
16316 -- If the partial view was not found then the parent type is not
16317 -- a private type. Otherwise check if the partial view is a tagged
16318 -- private type.
16319
16320 if Present (Partial_View)
16321 and then Is_Private_Type (Partial_View)
16322 and then not Is_Tagged_Type (Partial_View)
16323 then
16324 Error_Msg_NE
16325 ("cannot derive from & declared as untagged private "
16326 & "(SPARK RM 3.4(1))", N, Partial_View);
16327 end if;
16328 end;
16329 end if;
16330
16331 -- Ada 2005 (AI-251): In case of interface derivation check that the
16332 -- parent is also an interface.
16333
16334 if Interface_Present (Def) then
16335 if not Is_Interface (Parent_Type) then
16336 Diagnose_Interface (Indic, Parent_Type);
16337
16338 else
16339 Parent_Node := Parent (Base_Type (Parent_Type));
16340 Iface_Def := Type_Definition (Parent_Node);
16341
16342 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16343 -- other limited interfaces.
16344
16345 if Limited_Present (Def) then
16346 if Limited_Present (Iface_Def) then
16347 null;
16348
16349 elsif Protected_Present (Iface_Def) then
16350 Error_Msg_NE
16351 ("descendant of & must be declared as a protected "
16352 & "interface", N, Parent_Type);
16353
16354 elsif Synchronized_Present (Iface_Def) then
16355 Error_Msg_NE
16356 ("descendant of & must be declared as a synchronized "
16357 & "interface", N, Parent_Type);
16358
16359 elsif Task_Present (Iface_Def) then
16360 Error_Msg_NE
16361 ("descendant of & must be declared as a task interface",
16362 N, Parent_Type);
16363
16364 else
16365 Error_Msg_N
16366 ("(Ada 2005) limited interface cannot inherit from "
16367 & "non-limited interface", Indic);
16368 end if;
16369
16370 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16371 -- from non-limited or limited interfaces.
16372
16373 elsif not Protected_Present (Def)
16374 and then not Synchronized_Present (Def)
16375 and then not Task_Present (Def)
16376 then
16377 if Limited_Present (Iface_Def) then
16378 null;
16379
16380 elsif Protected_Present (Iface_Def) then
16381 Error_Msg_NE
16382 ("descendant of & must be declared as a protected "
16383 & "interface", N, Parent_Type);
16384
16385 elsif Synchronized_Present (Iface_Def) then
16386 Error_Msg_NE
16387 ("descendant of & must be declared as a synchronized "
16388 & "interface", N, Parent_Type);
16389
16390 elsif Task_Present (Iface_Def) then
16391 Error_Msg_NE
16392 ("descendant of & must be declared as a task interface",
16393 N, Parent_Type);
16394 else
16395 null;
16396 end if;
16397 end if;
16398 end if;
16399 end if;
16400
16401 if Is_Tagged_Type (Parent_Type)
16402 and then Is_Concurrent_Type (Parent_Type)
16403 and then not Is_Interface (Parent_Type)
16404 then
16405 Error_Msg_N
16406 ("parent type of a record extension cannot be a synchronized "
16407 & "tagged type (RM 3.9.1 (3/1))", N);
16408 Set_Etype (T, Any_Type);
16409 return;
16410 end if;
16411
16412 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16413 -- interfaces
16414
16415 if Is_Tagged_Type (Parent_Type)
16416 and then Is_Non_Empty_List (Interface_List (Def))
16417 then
16418 declare
16419 Intf : Node_Id;
16420 T : Entity_Id;
16421
16422 begin
16423 Intf := First (Interface_List (Def));
16424 while Present (Intf) loop
16425 T := Find_Type_Of_Subtype_Indic (Intf);
16426
16427 if not Is_Interface (T) then
16428 Diagnose_Interface (Intf, T);
16429
16430 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16431 -- a limited type from having a nonlimited progenitor.
16432
16433 elsif (Limited_Present (Def)
16434 or else (not Is_Interface (Parent_Type)
16435 and then Is_Limited_Type (Parent_Type)))
16436 and then not Is_Limited_Interface (T)
16437 then
16438 Error_Msg_NE
16439 ("progenitor interface& of limited type must be limited",
16440 N, T);
16441 end if;
16442
16443 Next (Intf);
16444 end loop;
16445 end;
16446 end if;
16447
16448 if Parent_Type = Any_Type
16449 or else Etype (Parent_Type) = Any_Type
16450 or else (Is_Class_Wide_Type (Parent_Type)
16451 and then Etype (Parent_Type) = T)
16452 then
16453 -- If Parent_Type is undefined or illegal, make new type into a
16454 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16455 -- errors. If this is a self-definition, emit error now.
16456
16457 if T = Parent_Type or else T = Etype (Parent_Type) then
16458 Error_Msg_N ("type cannot be used in its own definition", Indic);
16459 end if;
16460
16461 Set_Ekind (T, Ekind (Parent_Type));
16462 Set_Etype (T, Any_Type);
16463 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16464
16465 if Is_Tagged_Type (T)
16466 and then Is_Record_Type (T)
16467 then
16468 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16469 end if;
16470
16471 return;
16472 end if;
16473
16474 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16475 -- an interface is special because the list of interfaces in the full
16476 -- view can be given in any order. For example:
16477
16478 -- type A is interface;
16479 -- type B is interface and A;
16480 -- type D is new B with private;
16481 -- private
16482 -- type D is new A and B with null record; -- 1 --
16483
16484 -- In this case we perform the following transformation of -1-:
16485
16486 -- type D is new B and A with null record;
16487
16488 -- If the parent of the full-view covers the parent of the partial-view
16489 -- we have two possible cases:
16490
16491 -- 1) They have the same parent
16492 -- 2) The parent of the full-view implements some further interfaces
16493
16494 -- In both cases we do not need to perform the transformation. In the
16495 -- first case the source program is correct and the transformation is
16496 -- not needed; in the second case the source program does not fulfill
16497 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16498 -- later.
16499
16500 -- This transformation not only simplifies the rest of the analysis of
16501 -- this type declaration but also simplifies the correct generation of
16502 -- the object layout to the expander.
16503
16504 if In_Private_Part (Current_Scope)
16505 and then Is_Interface (Parent_Type)
16506 then
16507 declare
16508 Iface : Node_Id;
16509 Partial_View : Entity_Id;
16510 Partial_View_Parent : Entity_Id;
16511 New_Iface : Node_Id;
16512
16513 begin
16514 -- Look for the associated private type declaration
16515
16516 Partial_View := Incomplete_Or_Partial_View (T);
16517
16518 -- If the partial view was not found then the source code has
16519 -- errors and the transformation is not needed.
16520
16521 if Present (Partial_View) then
16522 Partial_View_Parent := Etype (Partial_View);
16523
16524 -- If the parent of the full-view covers the parent of the
16525 -- partial-view we have nothing else to do.
16526
16527 if Interface_Present_In_Ancestor
16528 (Parent_Type, Partial_View_Parent)
16529 then
16530 null;
16531
16532 -- Traverse the list of interfaces of the full-view to look
16533 -- for the parent of the partial-view and perform the tree
16534 -- transformation.
16535
16536 else
16537 Iface := First (Interface_List (Def));
16538 while Present (Iface) loop
16539 if Etype (Iface) = Etype (Partial_View) then
16540 Rewrite (Subtype_Indication (Def),
16541 New_Copy (Subtype_Indication
16542 (Parent (Partial_View))));
16543
16544 New_Iface :=
16545 Make_Identifier (Sloc (N), Chars (Parent_Type));
16546 Append (New_Iface, Interface_List (Def));
16547
16548 -- Analyze the transformed code
16549
16550 Derived_Type_Declaration (T, N, Is_Completion);
16551 return;
16552 end if;
16553
16554 Next (Iface);
16555 end loop;
16556 end if;
16557 end if;
16558 end;
16559 end if;
16560
16561 -- Only composite types other than array types are allowed to have
16562 -- discriminants.
16563
16564 if Present (Discriminant_Specifications (N)) then
16565 if (Is_Elementary_Type (Parent_Type)
16566 or else
16567 Is_Array_Type (Parent_Type))
16568 and then not Error_Posted (N)
16569 then
16570 Error_Msg_N
16571 ("elementary or array type cannot have discriminants",
16572 Defining_Identifier (First (Discriminant_Specifications (N))));
16573
16574 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16575 -- only if we are not already processing a malformed syntax tree.
16576
16577 if Is_Type (T) then
16578 Set_Has_Discriminants (T, False);
16579 end if;
16580 end if;
16581 end if;
16582
16583 -- In Ada 83, a derived type defined in a package specification cannot
16584 -- be used for further derivation until the end of its visible part.
16585 -- Note that derivation in the private part of the package is allowed.
16586
16587 if Ada_Version = Ada_83
16588 and then Is_Derived_Type (Parent_Type)
16589 and then In_Visible_Part (Scope (Parent_Type))
16590 then
16591 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16592 Error_Msg_N
16593 ("(Ada 83): premature use of type for derivation", Indic);
16594 end if;
16595 end if;
16596
16597 -- Check for early use of incomplete or private type
16598
16599 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16600 Error_Msg_N ("premature derivation of incomplete type", Indic);
16601 return;
16602
16603 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16604 and then not Comes_From_Generic (Parent_Type))
16605 or else Has_Private_Component (Parent_Type)
16606 then
16607 -- The ancestor type of a formal type can be incomplete, in which
16608 -- case only the operations of the partial view are available in the
16609 -- generic. Subsequent checks may be required when the full view is
16610 -- analyzed to verify that a derivation from a tagged type has an
16611 -- extension.
16612
16613 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16614 null;
16615
16616 elsif No (Underlying_Type (Parent_Type))
16617 or else Has_Private_Component (Parent_Type)
16618 then
16619 Error_Msg_N
16620 ("premature derivation of derived or private type", Indic);
16621
16622 -- Flag the type itself as being in error, this prevents some
16623 -- nasty problems with subsequent uses of the malformed type.
16624
16625 Set_Error_Posted (T);
16626
16627 -- Check that within the immediate scope of an untagged partial
16628 -- view it's illegal to derive from the partial view if the
16629 -- full view is tagged. (7.3(7))
16630
16631 -- We verify that the Parent_Type is a partial view by checking
16632 -- that it is not a Full_Type_Declaration (i.e. a private type or
16633 -- private extension declaration), to distinguish a partial view
16634 -- from a derivation from a private type which also appears as
16635 -- E_Private_Type. If the parent base type is not declared in an
16636 -- enclosing scope there is no need to check.
16637
16638 elsif Present (Full_View (Parent_Type))
16639 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16640 and then not Is_Tagged_Type (Parent_Type)
16641 and then Is_Tagged_Type (Full_View (Parent_Type))
16642 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16643 then
16644 Error_Msg_N
16645 ("premature derivation from type with tagged full view",
16646 Indic);
16647 end if;
16648 end if;
16649
16650 -- Check that form of derivation is appropriate
16651
16652 Taggd := Is_Tagged_Type (Parent_Type);
16653
16654 -- Set the parent type to the class-wide type's specific type in this
16655 -- case to prevent cascading errors
16656
16657 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16658 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16659 Set_Etype (T, Etype (Parent_Type));
16660 return;
16661 end if;
16662
16663 if Present (Extension) and then not Taggd then
16664 Error_Msg_N
16665 ("type derived from untagged type cannot have extension", Indic);
16666
16667 elsif No (Extension) and then Taggd then
16668
16669 -- If this declaration is within a private part (or body) of a
16670 -- generic instantiation then the derivation is allowed (the parent
16671 -- type can only appear tagged in this case if it's a generic actual
16672 -- type, since it would otherwise have been rejected in the analysis
16673 -- of the generic template).
16674
16675 if not Is_Generic_Actual_Type (Parent_Type)
16676 or else In_Visible_Part (Scope (Parent_Type))
16677 then
16678 if Is_Class_Wide_Type (Parent_Type) then
16679 Error_Msg_N
16680 ("parent type must not be a class-wide type", Indic);
16681
16682 -- Use specific type to prevent cascaded errors.
16683
16684 Parent_Type := Etype (Parent_Type);
16685
16686 else
16687 Error_Msg_N
16688 ("type derived from tagged type must have extension", Indic);
16689 end if;
16690 end if;
16691 end if;
16692
16693 -- AI-443: Synchronized formal derived types require a private
16694 -- extension. There is no point in checking the ancestor type or
16695 -- the progenitors since the construct is wrong to begin with.
16696
16697 if Ada_Version >= Ada_2005
16698 and then Is_Generic_Type (T)
16699 and then Present (Original_Node (N))
16700 then
16701 declare
16702 Decl : constant Node_Id := Original_Node (N);
16703
16704 begin
16705 if Nkind (Decl) = N_Formal_Type_Declaration
16706 and then Nkind (Formal_Type_Definition (Decl)) =
16707 N_Formal_Derived_Type_Definition
16708 and then Synchronized_Present (Formal_Type_Definition (Decl))
16709 and then No (Extension)
16710
16711 -- Avoid emitting a duplicate error message
16712
16713 and then not Error_Posted (Indic)
16714 then
16715 Error_Msg_N
16716 ("synchronized derived type must have extension", N);
16717 end if;
16718 end;
16719 end if;
16720
16721 if Null_Exclusion_Present (Def)
16722 and then not Is_Access_Type (Parent_Type)
16723 then
16724 Error_Msg_N ("null exclusion can only apply to an access type", N);
16725 end if;
16726
16727 -- Avoid deriving parent primitives of underlying record views
16728
16729 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16730 Derive_Subps => not Is_Underlying_Record_View (T));
16731
16732 -- AI-419: The parent type of an explicitly limited derived type must
16733 -- be a limited type or a limited interface.
16734
16735 if Limited_Present (Def) then
16736 Set_Is_Limited_Record (T);
16737
16738 if Is_Interface (T) then
16739 Set_Is_Limited_Interface (T);
16740 end if;
16741
16742 if not Is_Limited_Type (Parent_Type)
16743 and then
16744 (not Is_Interface (Parent_Type)
16745 or else not Is_Limited_Interface (Parent_Type))
16746 then
16747 -- AI05-0096: a derivation in the private part of an instance is
16748 -- legal if the generic formal is untagged limited, and the actual
16749 -- is non-limited.
16750
16751 if Is_Generic_Actual_Type (Parent_Type)
16752 and then In_Private_Part (Current_Scope)
16753 and then
16754 not Is_Tagged_Type
16755 (Generic_Parent_Type (Parent (Parent_Type)))
16756 then
16757 null;
16758
16759 else
16760 Error_Msg_NE
16761 ("parent type& of limited type must be limited",
16762 N, Parent_Type);
16763 end if;
16764 end if;
16765 end if;
16766 end Derived_Type_Declaration;
16767
16768 ------------------------
16769 -- Diagnose_Interface --
16770 ------------------------
16771
16772 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16773 begin
16774 if not Is_Interface (E) and then E /= Any_Type then
16775 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16776 end if;
16777 end Diagnose_Interface;
16778
16779 ----------------------------------
16780 -- Enumeration_Type_Declaration --
16781 ----------------------------------
16782
16783 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16784 Ev : Uint;
16785 L : Node_Id;
16786 R_Node : Node_Id;
16787 B_Node : Node_Id;
16788
16789 begin
16790 -- Create identifier node representing lower bound
16791
16792 B_Node := New_Node (N_Identifier, Sloc (Def));
16793 L := First (Literals (Def));
16794 Set_Chars (B_Node, Chars (L));
16795 Set_Entity (B_Node, L);
16796 Set_Etype (B_Node, T);
16797 Set_Is_Static_Expression (B_Node, True);
16798
16799 R_Node := New_Node (N_Range, Sloc (Def));
16800 Set_Low_Bound (R_Node, B_Node);
16801
16802 Set_Ekind (T, E_Enumeration_Type);
16803 Set_First_Literal (T, L);
16804 Set_Etype (T, T);
16805 Set_Is_Constrained (T);
16806
16807 Ev := Uint_0;
16808
16809 -- Loop through literals of enumeration type setting pos and rep values
16810 -- except that if the Ekind is already set, then it means the literal
16811 -- was already constructed (case of a derived type declaration and we
16812 -- should not disturb the Pos and Rep values.
16813
16814 while Present (L) loop
16815 if Ekind (L) /= E_Enumeration_Literal then
16816 Set_Ekind (L, E_Enumeration_Literal);
16817 Set_Enumeration_Pos (L, Ev);
16818 Set_Enumeration_Rep (L, Ev);
16819 Set_Is_Known_Valid (L, True);
16820 end if;
16821
16822 Set_Etype (L, T);
16823 New_Overloaded_Entity (L);
16824 Generate_Definition (L);
16825 Set_Convention (L, Convention_Intrinsic);
16826
16827 -- Case of character literal
16828
16829 if Nkind (L) = N_Defining_Character_Literal then
16830 Set_Is_Character_Type (T, True);
16831
16832 -- Check violation of No_Wide_Characters
16833
16834 if Restriction_Check_Required (No_Wide_Characters) then
16835 Get_Name_String (Chars (L));
16836
16837 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16838 Check_Restriction (No_Wide_Characters, L);
16839 end if;
16840 end if;
16841 end if;
16842
16843 Ev := Ev + 1;
16844 Next (L);
16845 end loop;
16846
16847 -- Now create a node representing upper bound
16848
16849 B_Node := New_Node (N_Identifier, Sloc (Def));
16850 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16851 Set_Entity (B_Node, Last (Literals (Def)));
16852 Set_Etype (B_Node, T);
16853 Set_Is_Static_Expression (B_Node, True);
16854
16855 Set_High_Bound (R_Node, B_Node);
16856
16857 -- Initialize various fields of the type. Some of this information
16858 -- may be overwritten later through rep.clauses.
16859
16860 Set_Scalar_Range (T, R_Node);
16861 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16862 Set_Enum_Esize (T);
16863 Set_Enum_Pos_To_Rep (T, Empty);
16864
16865 -- Set Discard_Names if configuration pragma set, or if there is
16866 -- a parameterless pragma in the current declarative region
16867
16868 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16869 Set_Discard_Names (T);
16870 end if;
16871
16872 -- Process end label if there is one
16873
16874 if Present (Def) then
16875 Process_End_Label (Def, 'e', T);
16876 end if;
16877 end Enumeration_Type_Declaration;
16878
16879 ---------------------------------
16880 -- Expand_To_Stored_Constraint --
16881 ---------------------------------
16882
16883 function Expand_To_Stored_Constraint
16884 (Typ : Entity_Id;
16885 Constraint : Elist_Id) return Elist_Id
16886 is
16887 Explicitly_Discriminated_Type : Entity_Id;
16888 Expansion : Elist_Id;
16889 Discriminant : Entity_Id;
16890
16891 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16892 -- Find the nearest type that actually specifies discriminants
16893
16894 ---------------------------------
16895 -- Type_With_Explicit_Discrims --
16896 ---------------------------------
16897
16898 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16899 Typ : constant E := Base_Type (Id);
16900
16901 begin
16902 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16903 if Present (Full_View (Typ)) then
16904 return Type_With_Explicit_Discrims (Full_View (Typ));
16905 end if;
16906
16907 else
16908 if Has_Discriminants (Typ) then
16909 return Typ;
16910 end if;
16911 end if;
16912
16913 if Etype (Typ) = Typ then
16914 return Empty;
16915 elsif Has_Discriminants (Typ) then
16916 return Typ;
16917 else
16918 return Type_With_Explicit_Discrims (Etype (Typ));
16919 end if;
16920
16921 end Type_With_Explicit_Discrims;
16922
16923 -- Start of processing for Expand_To_Stored_Constraint
16924
16925 begin
16926 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16927 return No_Elist;
16928 end if;
16929
16930 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16931
16932 if No (Explicitly_Discriminated_Type) then
16933 return No_Elist;
16934 end if;
16935
16936 Expansion := New_Elmt_List;
16937
16938 Discriminant :=
16939 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16940 while Present (Discriminant) loop
16941 Append_Elmt
16942 (Get_Discriminant_Value
16943 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16944 To => Expansion);
16945 Next_Stored_Discriminant (Discriminant);
16946 end loop;
16947
16948 return Expansion;
16949 end Expand_To_Stored_Constraint;
16950
16951 ---------------------------
16952 -- Find_Hidden_Interface --
16953 ---------------------------
16954
16955 function Find_Hidden_Interface
16956 (Src : Elist_Id;
16957 Dest : Elist_Id) return Entity_Id
16958 is
16959 Iface : Entity_Id;
16960 Iface_Elmt : Elmt_Id;
16961
16962 begin
16963 if Present (Src) and then Present (Dest) then
16964 Iface_Elmt := First_Elmt (Src);
16965 while Present (Iface_Elmt) loop
16966 Iface := Node (Iface_Elmt);
16967
16968 if Is_Interface (Iface)
16969 and then not Contain_Interface (Iface, Dest)
16970 then
16971 return Iface;
16972 end if;
16973
16974 Next_Elmt (Iface_Elmt);
16975 end loop;
16976 end if;
16977
16978 return Empty;
16979 end Find_Hidden_Interface;
16980
16981 --------------------
16982 -- Find_Type_Name --
16983 --------------------
16984
16985 function Find_Type_Name (N : Node_Id) return Entity_Id is
16986 Id : constant Entity_Id := Defining_Identifier (N);
16987 New_Id : Entity_Id;
16988 Prev : Entity_Id;
16989 Prev_Par : Node_Id;
16990
16991 procedure Check_Duplicate_Aspects;
16992 -- Check that aspects specified in a completion have not been specified
16993 -- already in the partial view.
16994
16995 procedure Tag_Mismatch;
16996 -- Diagnose a tagged partial view whose full view is untagged. We post
16997 -- the message on the full view, with a reference to the previous
16998 -- partial view. The partial view can be private or incomplete, and
16999 -- these are handled in a different manner, so we determine the position
17000 -- of the error message from the respective slocs of both.
17001
17002 -----------------------------
17003 -- Check_Duplicate_Aspects --
17004 -----------------------------
17005
17006 procedure Check_Duplicate_Aspects is
17007 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17008 -- Return the corresponding aspect of the partial view which matches
17009 -- the aspect id of Asp. Return Empty is no such aspect exists.
17010
17011 -----------------------------
17012 -- Get_Partial_View_Aspect --
17013 -----------------------------
17014
17015 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17016 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17017 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17018 Prev_Asp : Node_Id;
17019
17020 begin
17021 if Present (Prev_Asps) then
17022 Prev_Asp := First (Prev_Asps);
17023 while Present (Prev_Asp) loop
17024 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17025 return Prev_Asp;
17026 end if;
17027
17028 Next (Prev_Asp);
17029 end loop;
17030 end if;
17031
17032 return Empty;
17033 end Get_Partial_View_Aspect;
17034
17035 -- Local variables
17036
17037 Full_Asps : constant List_Id := Aspect_Specifications (N);
17038 Full_Asp : Node_Id;
17039 Part_Asp : Node_Id;
17040
17041 -- Start of processing for Check_Duplicate_Aspects
17042
17043 begin
17044 if Present (Full_Asps) then
17045 Full_Asp := First (Full_Asps);
17046 while Present (Full_Asp) loop
17047 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17048
17049 -- An aspect and its class-wide counterpart are two distinct
17050 -- aspects and may apply to both views of an entity.
17051
17052 if Present (Part_Asp)
17053 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17054 then
17055 Error_Msg_N
17056 ("aspect already specified in private declaration",
17057 Full_Asp);
17058
17059 Remove (Full_Asp);
17060 return;
17061 end if;
17062
17063 if Has_Discriminants (Prev)
17064 and then not Has_Unknown_Discriminants (Prev)
17065 and then Get_Aspect_Id (Full_Asp) =
17066 Aspect_Implicit_Dereference
17067 then
17068 Error_Msg_N
17069 ("cannot specify aspect if partial view has known "
17070 & "discriminants", Full_Asp);
17071 end if;
17072
17073 Next (Full_Asp);
17074 end loop;
17075 end if;
17076 end Check_Duplicate_Aspects;
17077
17078 ------------------
17079 -- Tag_Mismatch --
17080 ------------------
17081
17082 procedure Tag_Mismatch is
17083 begin
17084 if Sloc (Prev) < Sloc (Id) then
17085 if Ada_Version >= Ada_2012
17086 and then Nkind (N) = N_Private_Type_Declaration
17087 then
17088 Error_Msg_NE
17089 ("declaration of private } must be a tagged type ", Id, Prev);
17090 else
17091 Error_Msg_NE
17092 ("full declaration of } must be a tagged type ", Id, Prev);
17093 end if;
17094
17095 else
17096 if Ada_Version >= Ada_2012
17097 and then Nkind (N) = N_Private_Type_Declaration
17098 then
17099 Error_Msg_NE
17100 ("declaration of private } must be a tagged type ", Prev, Id);
17101 else
17102 Error_Msg_NE
17103 ("full declaration of } must be a tagged type ", Prev, Id);
17104 end if;
17105 end if;
17106 end Tag_Mismatch;
17107
17108 -- Start of processing for Find_Type_Name
17109
17110 begin
17111 -- Find incomplete declaration, if one was given
17112
17113 Prev := Current_Entity_In_Scope (Id);
17114
17115 -- New type declaration
17116
17117 if No (Prev) then
17118 Enter_Name (Id);
17119 return Id;
17120
17121 -- Previous declaration exists
17122
17123 else
17124 Prev_Par := Parent (Prev);
17125
17126 -- Error if not incomplete/private case except if previous
17127 -- declaration is implicit, etc. Enter_Name will emit error if
17128 -- appropriate.
17129
17130 if not Is_Incomplete_Or_Private_Type (Prev) then
17131 Enter_Name (Id);
17132 New_Id := Id;
17133
17134 -- Check invalid completion of private or incomplete type
17135
17136 elsif not Nkind_In (N, N_Full_Type_Declaration,
17137 N_Task_Type_Declaration,
17138 N_Protected_Type_Declaration)
17139 and then
17140 (Ada_Version < Ada_2012
17141 or else not Is_Incomplete_Type (Prev)
17142 or else not Nkind_In (N, N_Private_Type_Declaration,
17143 N_Private_Extension_Declaration))
17144 then
17145 -- Completion must be a full type declarations (RM 7.3(4))
17146
17147 Error_Msg_Sloc := Sloc (Prev);
17148 Error_Msg_NE ("invalid completion of }", Id, Prev);
17149
17150 -- Set scope of Id to avoid cascaded errors. Entity is never
17151 -- examined again, except when saving globals in generics.
17152
17153 Set_Scope (Id, Current_Scope);
17154 New_Id := Id;
17155
17156 -- If this is a repeated incomplete declaration, no further
17157 -- checks are possible.
17158
17159 if Nkind (N) = N_Incomplete_Type_Declaration then
17160 return Prev;
17161 end if;
17162
17163 -- Case of full declaration of incomplete type
17164
17165 elsif Ekind (Prev) = E_Incomplete_Type
17166 and then (Ada_Version < Ada_2012
17167 or else No (Full_View (Prev))
17168 or else not Is_Private_Type (Full_View (Prev)))
17169 then
17170 -- Indicate that the incomplete declaration has a matching full
17171 -- declaration. The defining occurrence of the incomplete
17172 -- declaration remains the visible one, and the procedure
17173 -- Get_Full_View dereferences it whenever the type is used.
17174
17175 if Present (Full_View (Prev)) then
17176 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17177 end if;
17178
17179 Set_Full_View (Prev, Id);
17180 Append_Entity (Id, Current_Scope);
17181 Set_Is_Public (Id, Is_Public (Prev));
17182 Set_Is_Internal (Id);
17183 New_Id := Prev;
17184
17185 -- If the incomplete view is tagged, a class_wide type has been
17186 -- created already. Use it for the private type as well, in order
17187 -- to prevent multiple incompatible class-wide types that may be
17188 -- created for self-referential anonymous access components.
17189
17190 if Is_Tagged_Type (Prev)
17191 and then Present (Class_Wide_Type (Prev))
17192 then
17193 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17194 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17195
17196 -- Type of the class-wide type is the current Id. Previously
17197 -- this was not done for private declarations because of order-
17198 -- of-elaboration issues in the back end, but gigi now handles
17199 -- this properly.
17200
17201 Set_Etype (Class_Wide_Type (Id), Id);
17202 end if;
17203
17204 -- Case of full declaration of private type
17205
17206 else
17207 -- If the private type was a completion of an incomplete type then
17208 -- update Prev to reference the private type
17209
17210 if Ada_Version >= Ada_2012
17211 and then Ekind (Prev) = E_Incomplete_Type
17212 and then Present (Full_View (Prev))
17213 and then Is_Private_Type (Full_View (Prev))
17214 then
17215 Prev := Full_View (Prev);
17216 Prev_Par := Parent (Prev);
17217 end if;
17218
17219 if Nkind (N) = N_Full_Type_Declaration
17220 and then Nkind_In
17221 (Type_Definition (N), N_Record_Definition,
17222 N_Derived_Type_Definition)
17223 and then Interface_Present (Type_Definition (N))
17224 then
17225 Error_Msg_N
17226 ("completion of private type cannot be an interface", N);
17227 end if;
17228
17229 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17230 if Etype (Prev) /= Prev then
17231
17232 -- Prev is a private subtype or a derived type, and needs
17233 -- no completion.
17234
17235 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17236 New_Id := Id;
17237
17238 elsif Ekind (Prev) = E_Private_Type
17239 and then Nkind_In (N, N_Task_Type_Declaration,
17240 N_Protected_Type_Declaration)
17241 then
17242 Error_Msg_N
17243 ("completion of nonlimited type cannot be limited", N);
17244
17245 elsif Ekind (Prev) = E_Record_Type_With_Private
17246 and then Nkind_In (N, N_Task_Type_Declaration,
17247 N_Protected_Type_Declaration)
17248 then
17249 if not Is_Limited_Record (Prev) then
17250 Error_Msg_N
17251 ("completion of nonlimited type cannot be limited", N);
17252
17253 elsif No (Interface_List (N)) then
17254 Error_Msg_N
17255 ("completion of tagged private type must be tagged",
17256 N);
17257 end if;
17258 end if;
17259
17260 -- Ada 2005 (AI-251): Private extension declaration of a task
17261 -- type or a protected type. This case arises when covering
17262 -- interface types.
17263
17264 elsif Nkind_In (N, N_Task_Type_Declaration,
17265 N_Protected_Type_Declaration)
17266 then
17267 null;
17268
17269 elsif Nkind (N) /= N_Full_Type_Declaration
17270 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17271 then
17272 Error_Msg_N
17273 ("full view of private extension must be an extension", N);
17274
17275 elsif not (Abstract_Present (Parent (Prev)))
17276 and then Abstract_Present (Type_Definition (N))
17277 then
17278 Error_Msg_N
17279 ("full view of non-abstract extension cannot be abstract", N);
17280 end if;
17281
17282 if not In_Private_Part (Current_Scope) then
17283 Error_Msg_N
17284 ("declaration of full view must appear in private part", N);
17285 end if;
17286
17287 if Ada_Version >= Ada_2012 then
17288 Check_Duplicate_Aspects;
17289 end if;
17290
17291 Copy_And_Swap (Prev, Id);
17292 Set_Has_Private_Declaration (Prev);
17293 Set_Has_Private_Declaration (Id);
17294
17295 -- AI12-0133: Indicate whether we have a partial view with
17296 -- unknown discriminants, in which case initialization of objects
17297 -- of the type do not receive an invariant check.
17298
17299 Set_Partial_View_Has_Unknown_Discr
17300 (Prev, Has_Unknown_Discriminants (Id));
17301
17302 -- Preserve aspect and iterator flags that may have been set on
17303 -- the partial view.
17304
17305 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17306 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17307
17308 -- If no error, propagate freeze_node from private to full view.
17309 -- It may have been generated for an early operational item.
17310
17311 if Present (Freeze_Node (Id))
17312 and then Serious_Errors_Detected = 0
17313 and then No (Full_View (Id))
17314 then
17315 Set_Freeze_Node (Prev, Freeze_Node (Id));
17316 Set_Freeze_Node (Id, Empty);
17317 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17318 end if;
17319
17320 Set_Full_View (Id, Prev);
17321 New_Id := Prev;
17322 end if;
17323
17324 -- Verify that full declaration conforms to partial one
17325
17326 if Is_Incomplete_Or_Private_Type (Prev)
17327 and then Present (Discriminant_Specifications (Prev_Par))
17328 then
17329 if Present (Discriminant_Specifications (N)) then
17330 if Ekind (Prev) = E_Incomplete_Type then
17331 Check_Discriminant_Conformance (N, Prev, Prev);
17332 else
17333 Check_Discriminant_Conformance (N, Prev, Id);
17334 end if;
17335
17336 else
17337 Error_Msg_N
17338 ("missing discriminants in full type declaration", N);
17339
17340 -- To avoid cascaded errors on subsequent use, share the
17341 -- discriminants of the partial view.
17342
17343 Set_Discriminant_Specifications (N,
17344 Discriminant_Specifications (Prev_Par));
17345 end if;
17346 end if;
17347
17348 -- A prior untagged partial view can have an associated class-wide
17349 -- type due to use of the class attribute, and in this case the full
17350 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17351 -- of incomplete tagged declarations, but we check for it.
17352
17353 if Is_Type (Prev)
17354 and then (Is_Tagged_Type (Prev)
17355 or else Present (Class_Wide_Type (Prev)))
17356 then
17357 -- Ada 2012 (AI05-0162): A private type may be the completion of
17358 -- an incomplete type.
17359
17360 if Ada_Version >= Ada_2012
17361 and then Is_Incomplete_Type (Prev)
17362 and then Nkind_In (N, N_Private_Type_Declaration,
17363 N_Private_Extension_Declaration)
17364 then
17365 -- No need to check private extensions since they are tagged
17366
17367 if Nkind (N) = N_Private_Type_Declaration
17368 and then not Tagged_Present (N)
17369 then
17370 Tag_Mismatch;
17371 end if;
17372
17373 -- The full declaration is either a tagged type (including
17374 -- a synchronized type that implements interfaces) or a
17375 -- type extension, otherwise this is an error.
17376
17377 elsif Nkind_In (N, N_Task_Type_Declaration,
17378 N_Protected_Type_Declaration)
17379 then
17380 if No (Interface_List (N)) and then not Error_Posted (N) then
17381 Tag_Mismatch;
17382 end if;
17383
17384 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17385
17386 -- Indicate that the previous declaration (tagged incomplete
17387 -- or private declaration) requires the same on the full one.
17388
17389 if not Tagged_Present (Type_Definition (N)) then
17390 Tag_Mismatch;
17391 Set_Is_Tagged_Type (Id);
17392 end if;
17393
17394 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17395 if No (Record_Extension_Part (Type_Definition (N))) then
17396 Error_Msg_NE
17397 ("full declaration of } must be a record extension",
17398 Prev, Id);
17399
17400 -- Set some attributes to produce a usable full view
17401
17402 Set_Is_Tagged_Type (Id);
17403 end if;
17404
17405 else
17406 Tag_Mismatch;
17407 end if;
17408 end if;
17409
17410 if Present (Prev)
17411 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17412 and then Present (Premature_Use (Parent (Prev)))
17413 then
17414 Error_Msg_Sloc := Sloc (N);
17415 Error_Msg_N
17416 ("\full declaration #", Premature_Use (Parent (Prev)));
17417 end if;
17418
17419 return New_Id;
17420 end if;
17421 end Find_Type_Name;
17422
17423 -------------------------
17424 -- Find_Type_Of_Object --
17425 -------------------------
17426
17427 function Find_Type_Of_Object
17428 (Obj_Def : Node_Id;
17429 Related_Nod : Node_Id) return Entity_Id
17430 is
17431 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17432 P : Node_Id := Parent (Obj_Def);
17433 T : Entity_Id;
17434 Nam : Name_Id;
17435
17436 begin
17437 -- If the parent is a component_definition node we climb to the
17438 -- component_declaration node
17439
17440 if Nkind (P) = N_Component_Definition then
17441 P := Parent (P);
17442 end if;
17443
17444 -- Case of an anonymous array subtype
17445
17446 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17447 N_Unconstrained_Array_Definition)
17448 then
17449 T := Empty;
17450 Array_Type_Declaration (T, Obj_Def);
17451
17452 -- Create an explicit subtype whenever possible
17453
17454 elsif Nkind (P) /= N_Component_Declaration
17455 and then Def_Kind = N_Subtype_Indication
17456 then
17457 -- Base name of subtype on object name, which will be unique in
17458 -- the current scope.
17459
17460 -- If this is a duplicate declaration, return base type, to avoid
17461 -- generating duplicate anonymous types.
17462
17463 if Error_Posted (P) then
17464 Analyze (Subtype_Mark (Obj_Def));
17465 return Entity (Subtype_Mark (Obj_Def));
17466 end if;
17467
17468 Nam :=
17469 New_External_Name
17470 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17471
17472 T := Make_Defining_Identifier (Sloc (P), Nam);
17473
17474 Insert_Action (Obj_Def,
17475 Make_Subtype_Declaration (Sloc (P),
17476 Defining_Identifier => T,
17477 Subtype_Indication => Relocate_Node (Obj_Def)));
17478
17479 -- This subtype may need freezing, and this will not be done
17480 -- automatically if the object declaration is not in declarative
17481 -- part. Since this is an object declaration, the type cannot always
17482 -- be frozen here. Deferred constants do not freeze their type
17483 -- (which often enough will be private).
17484
17485 if Nkind (P) = N_Object_Declaration
17486 and then Constant_Present (P)
17487 and then No (Expression (P))
17488 then
17489 null;
17490
17491 -- Here we freeze the base type of object type to catch premature use
17492 -- of discriminated private type without a full view.
17493
17494 else
17495 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17496 end if;
17497
17498 -- Ada 2005 AI-406: the object definition in an object declaration
17499 -- can be an access definition.
17500
17501 elsif Def_Kind = N_Access_Definition then
17502 T := Access_Definition (Related_Nod, Obj_Def);
17503
17504 Set_Is_Local_Anonymous_Access
17505 (T,
17506 V => (Ada_Version < Ada_2012)
17507 or else (Nkind (P) /= N_Object_Declaration)
17508 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17509
17510 -- Otherwise, the object definition is just a subtype_mark
17511
17512 else
17513 T := Process_Subtype (Obj_Def, Related_Nod);
17514 end if;
17515
17516 return T;
17517 end Find_Type_Of_Object;
17518
17519 --------------------------------
17520 -- Find_Type_Of_Subtype_Indic --
17521 --------------------------------
17522
17523 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17524 Typ : Entity_Id;
17525
17526 begin
17527 -- Case of subtype mark with a constraint
17528
17529 if Nkind (S) = N_Subtype_Indication then
17530 Find_Type (Subtype_Mark (S));
17531 Typ := Entity (Subtype_Mark (S));
17532
17533 if not
17534 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17535 then
17536 Error_Msg_N
17537 ("incorrect constraint for this kind of type", Constraint (S));
17538 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17539 end if;
17540
17541 -- Otherwise we have a subtype mark without a constraint
17542
17543 elsif Error_Posted (S) then
17544 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17545 return Any_Type;
17546
17547 else
17548 Find_Type (S);
17549 Typ := Entity (S);
17550 end if;
17551
17552 -- Check No_Wide_Characters restriction
17553
17554 Check_Wide_Character_Restriction (Typ, S);
17555
17556 return Typ;
17557 end Find_Type_Of_Subtype_Indic;
17558
17559 -------------------------------------
17560 -- Floating_Point_Type_Declaration --
17561 -------------------------------------
17562
17563 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17564 Digs : constant Node_Id := Digits_Expression (Def);
17565 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17566 Digs_Val : Uint;
17567 Base_Typ : Entity_Id;
17568 Implicit_Base : Entity_Id;
17569
17570 function Can_Derive_From (E : Entity_Id) return Boolean;
17571 -- Find if given digits value, and possibly a specified range, allows
17572 -- derivation from specified type
17573
17574 procedure Convert_Bound (B : Node_Id);
17575 -- If specified, the bounds must be static but may be of different
17576 -- types. They must be converted into machine numbers of the base type,
17577 -- in accordance with RM 4.9(38).
17578
17579 function Find_Base_Type return Entity_Id;
17580 -- Find a predefined base type that Def can derive from, or generate
17581 -- an error and substitute Long_Long_Float if none exists.
17582
17583 ---------------------
17584 -- Can_Derive_From --
17585 ---------------------
17586
17587 function Can_Derive_From (E : Entity_Id) return Boolean is
17588 Spec : constant Entity_Id := Real_Range_Specification (Def);
17589
17590 begin
17591 -- Check specified "digits" constraint
17592
17593 if Digs_Val > Digits_Value (E) then
17594 return False;
17595 end if;
17596
17597 -- Check for matching range, if specified
17598
17599 if Present (Spec) then
17600 if Expr_Value_R (Type_Low_Bound (E)) >
17601 Expr_Value_R (Low_Bound (Spec))
17602 then
17603 return False;
17604 end if;
17605
17606 if Expr_Value_R (Type_High_Bound (E)) <
17607 Expr_Value_R (High_Bound (Spec))
17608 then
17609 return False;
17610 end if;
17611 end if;
17612
17613 return True;
17614 end Can_Derive_From;
17615
17616 -------------------
17617 -- Convert_Bound --
17618 --------------------
17619
17620 procedure Convert_Bound (B : Node_Id) is
17621 begin
17622 -- If the bound is not a literal it can only be static if it is
17623 -- a static constant, possibly of a specified type.
17624
17625 if Is_Entity_Name (B)
17626 and then Ekind (Entity (B)) = E_Constant
17627 then
17628 Rewrite (B, Constant_Value (Entity (B)));
17629 end if;
17630
17631 if Nkind (B) = N_Real_Literal then
17632 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17633 Set_Is_Machine_Number (B);
17634 Set_Etype (B, Base_Typ);
17635 end if;
17636 end Convert_Bound;
17637
17638 --------------------
17639 -- Find_Base_Type --
17640 --------------------
17641
17642 function Find_Base_Type return Entity_Id is
17643 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17644
17645 begin
17646 -- Iterate over the predefined types in order, returning the first
17647 -- one that Def can derive from.
17648
17649 while Present (Choice) loop
17650 if Can_Derive_From (Node (Choice)) then
17651 return Node (Choice);
17652 end if;
17653
17654 Next_Elmt (Choice);
17655 end loop;
17656
17657 -- If we can't derive from any existing type, use Long_Long_Float
17658 -- and give appropriate message explaining the problem.
17659
17660 if Digs_Val > Max_Digs_Val then
17661 -- It might be the case that there is a type with the requested
17662 -- range, just not the combination of digits and range.
17663
17664 Error_Msg_N
17665 ("no predefined type has requested range and precision",
17666 Real_Range_Specification (Def));
17667
17668 else
17669 Error_Msg_N
17670 ("range too large for any predefined type",
17671 Real_Range_Specification (Def));
17672 end if;
17673
17674 return Standard_Long_Long_Float;
17675 end Find_Base_Type;
17676
17677 -- Start of processing for Floating_Point_Type_Declaration
17678
17679 begin
17680 Check_Restriction (No_Floating_Point, Def);
17681
17682 -- Create an implicit base type
17683
17684 Implicit_Base :=
17685 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17686
17687 -- Analyze and verify digits value
17688
17689 Analyze_And_Resolve (Digs, Any_Integer);
17690 Check_Digits_Expression (Digs);
17691 Digs_Val := Expr_Value (Digs);
17692
17693 -- Process possible range spec and find correct type to derive from
17694
17695 Process_Real_Range_Specification (Def);
17696
17697 -- Check that requested number of digits is not too high.
17698
17699 if Digs_Val > Max_Digs_Val then
17700
17701 -- The check for Max_Base_Digits may be somewhat expensive, as it
17702 -- requires reading System, so only do it when necessary.
17703
17704 declare
17705 Max_Base_Digits : constant Uint :=
17706 Expr_Value
17707 (Expression
17708 (Parent (RTE (RE_Max_Base_Digits))));
17709
17710 begin
17711 if Digs_Val > Max_Base_Digits then
17712 Error_Msg_Uint_1 := Max_Base_Digits;
17713 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17714
17715 elsif No (Real_Range_Specification (Def)) then
17716 Error_Msg_Uint_1 := Max_Digs_Val;
17717 Error_Msg_N ("types with more than ^ digits need range spec "
17718 & "(RM 3.5.7(6))", Digs);
17719 end if;
17720 end;
17721 end if;
17722
17723 -- Find a suitable type to derive from or complain and use a substitute
17724
17725 Base_Typ := Find_Base_Type;
17726
17727 -- If there are bounds given in the declaration use them as the bounds
17728 -- of the type, otherwise use the bounds of the predefined base type
17729 -- that was chosen based on the Digits value.
17730
17731 if Present (Real_Range_Specification (Def)) then
17732 Set_Scalar_Range (T, Real_Range_Specification (Def));
17733 Set_Is_Constrained (T);
17734
17735 Convert_Bound (Type_Low_Bound (T));
17736 Convert_Bound (Type_High_Bound (T));
17737
17738 else
17739 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17740 end if;
17741
17742 -- Complete definition of implicit base and declared first subtype. The
17743 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17744 -- are not clobbered when the floating point type acts as a full view of
17745 -- a private type.
17746
17747 Set_Etype (Implicit_Base, Base_Typ);
17748 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17749 Set_Size_Info (Implicit_Base, Base_Typ);
17750 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17751 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17752 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17753 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17754
17755 Set_Ekind (T, E_Floating_Point_Subtype);
17756 Set_Etype (T, Implicit_Base);
17757 Set_Size_Info (T, Implicit_Base);
17758 Set_RM_Size (T, RM_Size (Implicit_Base));
17759 Inherit_Rep_Item_Chain (T, Implicit_Base);
17760 Set_Digits_Value (T, Digs_Val);
17761 end Floating_Point_Type_Declaration;
17762
17763 ----------------------------
17764 -- Get_Discriminant_Value --
17765 ----------------------------
17766
17767 -- This is the situation:
17768
17769 -- There is a non-derived type
17770
17771 -- type T0 (Dx, Dy, Dz...)
17772
17773 -- There are zero or more levels of derivation, with each derivation
17774 -- either purely inheriting the discriminants, or defining its own.
17775
17776 -- type Ti is new Ti-1
17777 -- or
17778 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17779 -- or
17780 -- subtype Ti is ...
17781
17782 -- The subtype issue is avoided by the use of Original_Record_Component,
17783 -- and the fact that derived subtypes also derive the constraints.
17784
17785 -- This chain leads back from
17786
17787 -- Typ_For_Constraint
17788
17789 -- Typ_For_Constraint has discriminants, and the value for each
17790 -- discriminant is given by its corresponding Elmt of Constraints.
17791
17792 -- Discriminant is some discriminant in this hierarchy
17793
17794 -- We need to return its value
17795
17796 -- We do this by recursively searching each level, and looking for
17797 -- Discriminant. Once we get to the bottom, we start backing up
17798 -- returning the value for it which may in turn be a discriminant
17799 -- further up, so on the backup we continue the substitution.
17800
17801 function Get_Discriminant_Value
17802 (Discriminant : Entity_Id;
17803 Typ_For_Constraint : Entity_Id;
17804 Constraint : Elist_Id) return Node_Id
17805 is
17806 function Root_Corresponding_Discriminant
17807 (Discr : Entity_Id) return Entity_Id;
17808 -- Given a discriminant, traverse the chain of inherited discriminants
17809 -- and return the topmost discriminant.
17810
17811 function Search_Derivation_Levels
17812 (Ti : Entity_Id;
17813 Discrim_Values : Elist_Id;
17814 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17815 -- This is the routine that performs the recursive search of levels
17816 -- as described above.
17817
17818 -------------------------------------
17819 -- Root_Corresponding_Discriminant --
17820 -------------------------------------
17821
17822 function Root_Corresponding_Discriminant
17823 (Discr : Entity_Id) return Entity_Id
17824 is
17825 D : Entity_Id;
17826
17827 begin
17828 D := Discr;
17829 while Present (Corresponding_Discriminant (D)) loop
17830 D := Corresponding_Discriminant (D);
17831 end loop;
17832
17833 return D;
17834 end Root_Corresponding_Discriminant;
17835
17836 ------------------------------
17837 -- Search_Derivation_Levels --
17838 ------------------------------
17839
17840 function Search_Derivation_Levels
17841 (Ti : Entity_Id;
17842 Discrim_Values : Elist_Id;
17843 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17844 is
17845 Assoc : Elmt_Id;
17846 Disc : Entity_Id;
17847 Result : Node_Or_Entity_Id;
17848 Result_Entity : Node_Id;
17849
17850 begin
17851 -- If inappropriate type, return Error, this happens only in
17852 -- cascaded error situations, and we want to avoid a blow up.
17853
17854 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17855 return Error;
17856 end if;
17857
17858 -- Look deeper if possible. Use Stored_Constraints only for
17859 -- untagged types. For tagged types use the given constraint.
17860 -- This asymmetry needs explanation???
17861
17862 if not Stored_Discrim_Values
17863 and then Present (Stored_Constraint (Ti))
17864 and then not Is_Tagged_Type (Ti)
17865 then
17866 Result :=
17867 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17868
17869 else
17870 declare
17871 Td : Entity_Id := Etype (Ti);
17872
17873 begin
17874 -- If the parent type is private, the full view may include
17875 -- renamed discriminants, and it is those stored values that
17876 -- may be needed (the partial view never has more information
17877 -- than the full view).
17878
17879 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
17880 Td := Full_View (Td);
17881 end if;
17882
17883 if Td = Ti then
17884 Result := Discriminant;
17885
17886 else
17887 if Present (Stored_Constraint (Ti)) then
17888 Result :=
17889 Search_Derivation_Levels
17890 (Td, Stored_Constraint (Ti), True);
17891 else
17892 Result :=
17893 Search_Derivation_Levels
17894 (Td, Discrim_Values, Stored_Discrim_Values);
17895 end if;
17896 end if;
17897 end;
17898 end if;
17899
17900 -- Extra underlying places to search, if not found above. For
17901 -- concurrent types, the relevant discriminant appears in the
17902 -- corresponding record. For a type derived from a private type
17903 -- without discriminant, the full view inherits the discriminants
17904 -- of the full view of the parent.
17905
17906 if Result = Discriminant then
17907 if Is_Concurrent_Type (Ti)
17908 and then Present (Corresponding_Record_Type (Ti))
17909 then
17910 Result :=
17911 Search_Derivation_Levels (
17912 Corresponding_Record_Type (Ti),
17913 Discrim_Values,
17914 Stored_Discrim_Values);
17915
17916 elsif Is_Private_Type (Ti)
17917 and then not Has_Discriminants (Ti)
17918 and then Present (Full_View (Ti))
17919 and then Etype (Full_View (Ti)) /= Ti
17920 then
17921 Result :=
17922 Search_Derivation_Levels (
17923 Full_View (Ti),
17924 Discrim_Values,
17925 Stored_Discrim_Values);
17926 end if;
17927 end if;
17928
17929 -- If Result is not a (reference to a) discriminant, return it,
17930 -- otherwise set Result_Entity to the discriminant.
17931
17932 if Nkind (Result) = N_Defining_Identifier then
17933 pragma Assert (Result = Discriminant);
17934 Result_Entity := Result;
17935
17936 else
17937 if not Denotes_Discriminant (Result) then
17938 return Result;
17939 end if;
17940
17941 Result_Entity := Entity (Result);
17942 end if;
17943
17944 -- See if this level of derivation actually has discriminants because
17945 -- tagged derivations can add them, hence the lower levels need not
17946 -- have any.
17947
17948 if not Has_Discriminants (Ti) then
17949 return Result;
17950 end if;
17951
17952 -- Scan Ti's discriminants for Result_Entity, and return its
17953 -- corresponding value, if any.
17954
17955 Result_Entity := Original_Record_Component (Result_Entity);
17956
17957 Assoc := First_Elmt (Discrim_Values);
17958
17959 if Stored_Discrim_Values then
17960 Disc := First_Stored_Discriminant (Ti);
17961 else
17962 Disc := First_Discriminant (Ti);
17963 end if;
17964
17965 while Present (Disc) loop
17966
17967 -- If no further associations return the discriminant, value will
17968 -- be found on the second pass.
17969
17970 if No (Assoc) then
17971 return Result;
17972 end if;
17973
17974 if Original_Record_Component (Disc) = Result_Entity then
17975 return Node (Assoc);
17976 end if;
17977
17978 Next_Elmt (Assoc);
17979
17980 if Stored_Discrim_Values then
17981 Next_Stored_Discriminant (Disc);
17982 else
17983 Next_Discriminant (Disc);
17984 end if;
17985 end loop;
17986
17987 -- Could not find it
17988
17989 return Result;
17990 end Search_Derivation_Levels;
17991
17992 -- Local Variables
17993
17994 Result : Node_Or_Entity_Id;
17995
17996 -- Start of processing for Get_Discriminant_Value
17997
17998 begin
17999 -- ??? This routine is a gigantic mess and will be deleted. For the
18000 -- time being just test for the trivial case before calling recurse.
18001
18002 -- We are now celebrating the 20th anniversary of this comment!
18003
18004 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18005 declare
18006 D : Entity_Id;
18007 E : Elmt_Id;
18008
18009 begin
18010 D := First_Discriminant (Typ_For_Constraint);
18011 E := First_Elmt (Constraint);
18012 while Present (D) loop
18013 if Chars (D) = Chars (Discriminant) then
18014 return Node (E);
18015 end if;
18016
18017 Next_Discriminant (D);
18018 Next_Elmt (E);
18019 end loop;
18020 end;
18021 end if;
18022
18023 Result := Search_Derivation_Levels
18024 (Typ_For_Constraint, Constraint, False);
18025
18026 -- ??? hack to disappear when this routine is gone
18027
18028 if Nkind (Result) = N_Defining_Identifier then
18029 declare
18030 D : Entity_Id;
18031 E : Elmt_Id;
18032
18033 begin
18034 D := First_Discriminant (Typ_For_Constraint);
18035 E := First_Elmt (Constraint);
18036 while Present (D) loop
18037 if Root_Corresponding_Discriminant (D) = Discriminant then
18038 return Node (E);
18039 end if;
18040
18041 Next_Discriminant (D);
18042 Next_Elmt (E);
18043 end loop;
18044 end;
18045 end if;
18046
18047 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18048 return Result;
18049 end Get_Discriminant_Value;
18050
18051 --------------------------
18052 -- Has_Range_Constraint --
18053 --------------------------
18054
18055 function Has_Range_Constraint (N : Node_Id) return Boolean is
18056 C : constant Node_Id := Constraint (N);
18057
18058 begin
18059 if Nkind (C) = N_Range_Constraint then
18060 return True;
18061
18062 elsif Nkind (C) = N_Digits_Constraint then
18063 return
18064 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18065 or else Present (Range_Constraint (C));
18066
18067 elsif Nkind (C) = N_Delta_Constraint then
18068 return Present (Range_Constraint (C));
18069
18070 else
18071 return False;
18072 end if;
18073 end Has_Range_Constraint;
18074
18075 ------------------------
18076 -- Inherit_Components --
18077 ------------------------
18078
18079 function Inherit_Components
18080 (N : Node_Id;
18081 Parent_Base : Entity_Id;
18082 Derived_Base : Entity_Id;
18083 Is_Tagged : Boolean;
18084 Inherit_Discr : Boolean;
18085 Discs : Elist_Id) return Elist_Id
18086 is
18087 Assoc_List : constant Elist_Id := New_Elmt_List;
18088
18089 procedure Inherit_Component
18090 (Old_C : Entity_Id;
18091 Plain_Discrim : Boolean := False;
18092 Stored_Discrim : Boolean := False);
18093 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18094 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18095 -- True, Old_C is a stored discriminant. If they are both false then
18096 -- Old_C is a regular component.
18097
18098 -----------------------
18099 -- Inherit_Component --
18100 -----------------------
18101
18102 procedure Inherit_Component
18103 (Old_C : Entity_Id;
18104 Plain_Discrim : Boolean := False;
18105 Stored_Discrim : Boolean := False)
18106 is
18107 procedure Set_Anonymous_Type (Id : Entity_Id);
18108 -- Id denotes the entity of an access discriminant or anonymous
18109 -- access component. Set the type of Id to either the same type of
18110 -- Old_C or create a new one depending on whether the parent and
18111 -- the child types are in the same scope.
18112
18113 ------------------------
18114 -- Set_Anonymous_Type --
18115 ------------------------
18116
18117 procedure Set_Anonymous_Type (Id : Entity_Id) is
18118 Old_Typ : constant Entity_Id := Etype (Old_C);
18119
18120 begin
18121 if Scope (Parent_Base) = Scope (Derived_Base) then
18122 Set_Etype (Id, Old_Typ);
18123
18124 -- The parent and the derived type are in two different scopes.
18125 -- Reuse the type of the original discriminant / component by
18126 -- copying it in order to preserve all attributes.
18127
18128 else
18129 declare
18130 Typ : constant Entity_Id := New_Copy (Old_Typ);
18131
18132 begin
18133 Set_Etype (Id, Typ);
18134
18135 -- Since we do not generate component declarations for
18136 -- inherited components, associate the itype with the
18137 -- derived type.
18138
18139 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18140 Set_Scope (Typ, Derived_Base);
18141 end;
18142 end if;
18143 end Set_Anonymous_Type;
18144
18145 -- Local variables and constants
18146
18147 New_C : constant Entity_Id := New_Copy (Old_C);
18148
18149 Corr_Discrim : Entity_Id;
18150 Discrim : Entity_Id;
18151
18152 -- Start of processing for Inherit_Component
18153
18154 begin
18155 pragma Assert (not Is_Tagged or not Stored_Discrim);
18156
18157 Set_Parent (New_C, Parent (Old_C));
18158
18159 -- Regular discriminants and components must be inserted in the scope
18160 -- of the Derived_Base. Do it here.
18161
18162 if not Stored_Discrim then
18163 Enter_Name (New_C);
18164 end if;
18165
18166 -- For tagged types the Original_Record_Component must point to
18167 -- whatever this field was pointing to in the parent type. This has
18168 -- already been achieved by the call to New_Copy above.
18169
18170 if not Is_Tagged then
18171 Set_Original_Record_Component (New_C, New_C);
18172 Set_Corresponding_Record_Component (New_C, Old_C);
18173 end if;
18174
18175 -- Set the proper type of an access discriminant
18176
18177 if Ekind (New_C) = E_Discriminant
18178 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18179 then
18180 Set_Anonymous_Type (New_C);
18181 end if;
18182
18183 -- If we have inherited a component then see if its Etype contains
18184 -- references to Parent_Base discriminants. In this case, replace
18185 -- these references with the constraints given in Discs. We do not
18186 -- do this for the partial view of private types because this is
18187 -- not needed (only the components of the full view will be used
18188 -- for code generation) and cause problem. We also avoid this
18189 -- transformation in some error situations.
18190
18191 if Ekind (New_C) = E_Component then
18192
18193 -- Set the proper type of an anonymous access component
18194
18195 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18196 Set_Anonymous_Type (New_C);
18197
18198 elsif (Is_Private_Type (Derived_Base)
18199 and then not Is_Generic_Type (Derived_Base))
18200 or else (Is_Empty_Elmt_List (Discs)
18201 and then not Expander_Active)
18202 then
18203 Set_Etype (New_C, Etype (Old_C));
18204
18205 else
18206 -- The current component introduces a circularity of the
18207 -- following kind:
18208
18209 -- limited with Pack_2;
18210 -- package Pack_1 is
18211 -- type T_1 is tagged record
18212 -- Comp : access Pack_2.T_2;
18213 -- ...
18214 -- end record;
18215 -- end Pack_1;
18216
18217 -- with Pack_1;
18218 -- package Pack_2 is
18219 -- type T_2 is new Pack_1.T_1 with ...;
18220 -- end Pack_2;
18221
18222 Set_Etype
18223 (New_C,
18224 Constrain_Component_Type
18225 (Old_C, Derived_Base, N, Parent_Base, Discs));
18226 end if;
18227 end if;
18228
18229 -- In derived tagged types it is illegal to reference a non
18230 -- discriminant component in the parent type. To catch this, mark
18231 -- these components with an Ekind of E_Void. This will be reset in
18232 -- Record_Type_Definition after processing the record extension of
18233 -- the derived type.
18234
18235 -- If the declaration is a private extension, there is no further
18236 -- record extension to process, and the components retain their
18237 -- current kind, because they are visible at this point.
18238
18239 if Is_Tagged and then Ekind (New_C) = E_Component
18240 and then Nkind (N) /= N_Private_Extension_Declaration
18241 then
18242 Set_Ekind (New_C, E_Void);
18243 end if;
18244
18245 if Plain_Discrim then
18246 Set_Corresponding_Discriminant (New_C, Old_C);
18247 Build_Discriminal (New_C);
18248
18249 -- If we are explicitly inheriting a stored discriminant it will be
18250 -- completely hidden.
18251
18252 elsif Stored_Discrim then
18253 Set_Corresponding_Discriminant (New_C, Empty);
18254 Set_Discriminal (New_C, Empty);
18255 Set_Is_Completely_Hidden (New_C);
18256
18257 -- Set the Original_Record_Component of each discriminant in the
18258 -- derived base to point to the corresponding stored that we just
18259 -- created.
18260
18261 Discrim := First_Discriminant (Derived_Base);
18262 while Present (Discrim) loop
18263 Corr_Discrim := Corresponding_Discriminant (Discrim);
18264
18265 -- Corr_Discrim could be missing in an error situation
18266
18267 if Present (Corr_Discrim)
18268 and then Original_Record_Component (Corr_Discrim) = Old_C
18269 then
18270 Set_Original_Record_Component (Discrim, New_C);
18271 Set_Corresponding_Record_Component (Discrim, Empty);
18272 end if;
18273
18274 Next_Discriminant (Discrim);
18275 end loop;
18276
18277 Append_Entity (New_C, Derived_Base);
18278 end if;
18279
18280 if not Is_Tagged then
18281 Append_Elmt (Old_C, Assoc_List);
18282 Append_Elmt (New_C, Assoc_List);
18283 end if;
18284 end Inherit_Component;
18285
18286 -- Variables local to Inherit_Component
18287
18288 Loc : constant Source_Ptr := Sloc (N);
18289
18290 Parent_Discrim : Entity_Id;
18291 Stored_Discrim : Entity_Id;
18292 D : Entity_Id;
18293 Component : Entity_Id;
18294
18295 -- Start of processing for Inherit_Components
18296
18297 begin
18298 if not Is_Tagged then
18299 Append_Elmt (Parent_Base, Assoc_List);
18300 Append_Elmt (Derived_Base, Assoc_List);
18301 end if;
18302
18303 -- Inherit parent discriminants if needed
18304
18305 if Inherit_Discr then
18306 Parent_Discrim := First_Discriminant (Parent_Base);
18307 while Present (Parent_Discrim) loop
18308 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18309 Next_Discriminant (Parent_Discrim);
18310 end loop;
18311 end if;
18312
18313 -- Create explicit stored discrims for untagged types when necessary
18314
18315 if not Has_Unknown_Discriminants (Derived_Base)
18316 and then Has_Discriminants (Parent_Base)
18317 and then not Is_Tagged
18318 and then
18319 (not Inherit_Discr
18320 or else First_Discriminant (Parent_Base) /=
18321 First_Stored_Discriminant (Parent_Base))
18322 then
18323 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18324 while Present (Stored_Discrim) loop
18325 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18326 Next_Stored_Discriminant (Stored_Discrim);
18327 end loop;
18328 end if;
18329
18330 -- See if we can apply the second transformation for derived types, as
18331 -- explained in point 6. in the comments above Build_Derived_Record_Type
18332 -- This is achieved by appending Derived_Base discriminants into Discs,
18333 -- which has the side effect of returning a non empty Discs list to the
18334 -- caller of Inherit_Components, which is what we want. This must be
18335 -- done for private derived types if there are explicit stored
18336 -- discriminants, to ensure that we can retrieve the values of the
18337 -- constraints provided in the ancestors.
18338
18339 if Inherit_Discr
18340 and then Is_Empty_Elmt_List (Discs)
18341 and then Present (First_Discriminant (Derived_Base))
18342 and then
18343 (not Is_Private_Type (Derived_Base)
18344 or else Is_Completely_Hidden
18345 (First_Stored_Discriminant (Derived_Base))
18346 or else Is_Generic_Type (Derived_Base))
18347 then
18348 D := First_Discriminant (Derived_Base);
18349 while Present (D) loop
18350 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18351 Next_Discriminant (D);
18352 end loop;
18353 end if;
18354
18355 -- Finally, inherit non-discriminant components unless they are not
18356 -- visible because defined or inherited from the full view of the
18357 -- parent. Don't inherit the _parent field of the parent type.
18358
18359 Component := First_Entity (Parent_Base);
18360 while Present (Component) loop
18361
18362 -- Ada 2005 (AI-251): Do not inherit components associated with
18363 -- secondary tags of the parent.
18364
18365 if Ekind (Component) = E_Component
18366 and then Present (Related_Type (Component))
18367 then
18368 null;
18369
18370 elsif Ekind (Component) /= E_Component
18371 or else Chars (Component) = Name_uParent
18372 then
18373 null;
18374
18375 -- If the derived type is within the parent type's declarative
18376 -- region, then the components can still be inherited even though
18377 -- they aren't visible at this point. This can occur for cases
18378 -- such as within public child units where the components must
18379 -- become visible upon entering the child unit's private part.
18380
18381 elsif not Is_Visible_Component (Component)
18382 and then not In_Open_Scopes (Scope (Parent_Base))
18383 then
18384 null;
18385
18386 elsif Ekind_In (Derived_Base, E_Private_Type,
18387 E_Limited_Private_Type)
18388 then
18389 null;
18390
18391 else
18392 Inherit_Component (Component);
18393 end if;
18394
18395 Next_Entity (Component);
18396 end loop;
18397
18398 -- For tagged derived types, inherited discriminants cannot be used in
18399 -- component declarations of the record extension part. To achieve this
18400 -- we mark the inherited discriminants as not visible.
18401
18402 if Is_Tagged and then Inherit_Discr then
18403 D := First_Discriminant (Derived_Base);
18404 while Present (D) loop
18405 Set_Is_Immediately_Visible (D, False);
18406 Next_Discriminant (D);
18407 end loop;
18408 end if;
18409
18410 return Assoc_List;
18411 end Inherit_Components;
18412
18413 ----------------------
18414 -- Is_EVF_Procedure --
18415 ----------------------
18416
18417 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18418 Formal : Entity_Id;
18419
18420 begin
18421 -- Examine the formals of an Extensions_Visible False procedure looking
18422 -- for a controlling OUT parameter.
18423
18424 if Ekind (Subp) = E_Procedure
18425 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18426 then
18427 Formal := First_Formal (Subp);
18428 while Present (Formal) loop
18429 if Ekind (Formal) = E_Out_Parameter
18430 and then Is_Controlling_Formal (Formal)
18431 then
18432 return True;
18433 end if;
18434
18435 Next_Formal (Formal);
18436 end loop;
18437 end if;
18438
18439 return False;
18440 end Is_EVF_Procedure;
18441
18442 -----------------------
18443 -- Is_Null_Extension --
18444 -----------------------
18445
18446 function Is_Null_Extension (T : Entity_Id) return Boolean is
18447 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18448 Comp_List : Node_Id;
18449 Comp : Node_Id;
18450
18451 begin
18452 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18453 or else not Is_Tagged_Type (T)
18454 or else Nkind (Type_Definition (Type_Decl)) /=
18455 N_Derived_Type_Definition
18456 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18457 then
18458 return False;
18459 end if;
18460
18461 Comp_List :=
18462 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18463
18464 if Present (Discriminant_Specifications (Type_Decl)) then
18465 return False;
18466
18467 elsif Present (Comp_List)
18468 and then Is_Non_Empty_List (Component_Items (Comp_List))
18469 then
18470 Comp := First (Component_Items (Comp_List));
18471
18472 -- Only user-defined components are relevant. The component list
18473 -- may also contain a parent component and internal components
18474 -- corresponding to secondary tags, but these do not determine
18475 -- whether this is a null extension.
18476
18477 while Present (Comp) loop
18478 if Comes_From_Source (Comp) then
18479 return False;
18480 end if;
18481
18482 Next (Comp);
18483 end loop;
18484
18485 return True;
18486
18487 else
18488 return True;
18489 end if;
18490 end Is_Null_Extension;
18491
18492 ------------------------------
18493 -- Is_Valid_Constraint_Kind --
18494 ------------------------------
18495
18496 function Is_Valid_Constraint_Kind
18497 (T_Kind : Type_Kind;
18498 Constraint_Kind : Node_Kind) return Boolean
18499 is
18500 begin
18501 case T_Kind is
18502 when Enumeration_Kind
18503 | Integer_Kind
18504 =>
18505 return Constraint_Kind = N_Range_Constraint;
18506
18507 when Decimal_Fixed_Point_Kind =>
18508 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18509 N_Range_Constraint);
18510
18511 when Ordinary_Fixed_Point_Kind =>
18512 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18513 N_Range_Constraint);
18514
18515 when Float_Kind =>
18516 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18517 N_Range_Constraint);
18518
18519 when Access_Kind
18520 | Array_Kind
18521 | Class_Wide_Kind
18522 | Concurrent_Kind
18523 | Private_Kind
18524 | E_Incomplete_Type
18525 | E_Record_Subtype
18526 | E_Record_Type
18527 =>
18528 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18529
18530 when others =>
18531 return True; -- Error will be detected later
18532 end case;
18533 end Is_Valid_Constraint_Kind;
18534
18535 --------------------------
18536 -- Is_Visible_Component --
18537 --------------------------
18538
18539 function Is_Visible_Component
18540 (C : Entity_Id;
18541 N : Node_Id := Empty) return Boolean
18542 is
18543 Original_Comp : Entity_Id := Empty;
18544 Original_Type : Entity_Id;
18545 Type_Scope : Entity_Id;
18546
18547 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18548 -- Check whether parent type of inherited component is declared locally,
18549 -- possibly within a nested package or instance. The current scope is
18550 -- the derived record itself.
18551
18552 -------------------
18553 -- Is_Local_Type --
18554 -------------------
18555
18556 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18557 Scop : Entity_Id;
18558
18559 begin
18560 Scop := Scope (Typ);
18561 while Present (Scop)
18562 and then Scop /= Standard_Standard
18563 loop
18564 if Scop = Scope (Current_Scope) then
18565 return True;
18566 end if;
18567
18568 Scop := Scope (Scop);
18569 end loop;
18570
18571 return False;
18572 end Is_Local_Type;
18573
18574 -- Start of processing for Is_Visible_Component
18575
18576 begin
18577 if Ekind_In (C, E_Component, E_Discriminant) then
18578 Original_Comp := Original_Record_Component (C);
18579 end if;
18580
18581 if No (Original_Comp) then
18582
18583 -- Premature usage, or previous error
18584
18585 return False;
18586
18587 else
18588 Original_Type := Scope (Original_Comp);
18589 Type_Scope := Scope (Base_Type (Scope (C)));
18590 end if;
18591
18592 -- This test only concerns tagged types
18593
18594 if not Is_Tagged_Type (Original_Type) then
18595
18596 -- Check if this is a renamed discriminant (hidden either by the
18597 -- derived type or by some ancestor), unless we are analyzing code
18598 -- generated by the expander since it may reference such components
18599 -- (for example see the expansion of Deep_Adjust).
18600
18601 if Ekind (C) = E_Discriminant and then Present (N) then
18602 return
18603 not Comes_From_Source (N)
18604 or else not Is_Completely_Hidden (C);
18605 else
18606 return True;
18607 end if;
18608
18609 -- If it is _Parent or _Tag, there is no visibility issue
18610
18611 elsif not Comes_From_Source (Original_Comp) then
18612 return True;
18613
18614 -- Discriminants are visible unless the (private) type has unknown
18615 -- discriminants. If the discriminant reference is inserted for a
18616 -- discriminant check on a full view it is also visible.
18617
18618 elsif Ekind (Original_Comp) = E_Discriminant
18619 and then
18620 (not Has_Unknown_Discriminants (Original_Type)
18621 or else (Present (N)
18622 and then Nkind (N) = N_Selected_Component
18623 and then Nkind (Prefix (N)) = N_Type_Conversion
18624 and then not Comes_From_Source (Prefix (N))))
18625 then
18626 return True;
18627
18628 -- If the component has been declared in an ancestor which is currently
18629 -- a private type, then it is not visible. The same applies if the
18630 -- component's containing type is not in an open scope and the original
18631 -- component's enclosing type is a visible full view of a private type
18632 -- (which can occur in cases where an attempt is being made to reference
18633 -- a component in a sibling package that is inherited from a visible
18634 -- component of a type in an ancestor package; the component in the
18635 -- sibling package should not be visible even though the component it
18636 -- inherited from is visible). This does not apply however in the case
18637 -- where the scope of the type is a private child unit, or when the
18638 -- parent comes from a local package in which the ancestor is currently
18639 -- visible. The latter suppression of visibility is needed for cases
18640 -- that are tested in B730006.
18641
18642 elsif Is_Private_Type (Original_Type)
18643 or else
18644 (not Is_Private_Descendant (Type_Scope)
18645 and then not In_Open_Scopes (Type_Scope)
18646 and then Has_Private_Declaration (Original_Type))
18647 then
18648 -- If the type derives from an entity in a formal package, there
18649 -- are no additional visible components.
18650
18651 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18652 N_Formal_Package_Declaration
18653 then
18654 return False;
18655
18656 -- if we are not in the private part of the current package, there
18657 -- are no additional visible components.
18658
18659 elsif Ekind (Scope (Current_Scope)) = E_Package
18660 and then not In_Private_Part (Scope (Current_Scope))
18661 then
18662 return False;
18663 else
18664 return
18665 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18666 and then In_Open_Scopes (Scope (Original_Type))
18667 and then Is_Local_Type (Type_Scope);
18668 end if;
18669
18670 -- There is another weird way in which a component may be invisible when
18671 -- the private and the full view are not derived from the same ancestor.
18672 -- Here is an example :
18673
18674 -- type A1 is tagged record F1 : integer; end record;
18675 -- type A2 is new A1 with record F2 : integer; end record;
18676 -- type T is new A1 with private;
18677 -- private
18678 -- type T is new A2 with null record;
18679
18680 -- In this case, the full view of T inherits F1 and F2 but the private
18681 -- view inherits only F1
18682
18683 else
18684 declare
18685 Ancestor : Entity_Id := Scope (C);
18686
18687 begin
18688 loop
18689 if Ancestor = Original_Type then
18690 return True;
18691
18692 -- The ancestor may have a partial view of the original type,
18693 -- but if the full view is in scope, as in a child body, the
18694 -- component is visible.
18695
18696 elsif In_Private_Part (Scope (Original_Type))
18697 and then Full_View (Ancestor) = Original_Type
18698 then
18699 return True;
18700
18701 elsif Ancestor = Etype (Ancestor) then
18702
18703 -- No further ancestors to examine
18704
18705 return False;
18706 end if;
18707
18708 Ancestor := Etype (Ancestor);
18709 end loop;
18710 end;
18711 end if;
18712 end Is_Visible_Component;
18713
18714 --------------------------
18715 -- Make_Class_Wide_Type --
18716 --------------------------
18717
18718 procedure Make_Class_Wide_Type (T : Entity_Id) is
18719 CW_Type : Entity_Id;
18720 CW_Name : Name_Id;
18721 Next_E : Entity_Id;
18722 Prev_E : Entity_Id;
18723
18724 begin
18725 if Present (Class_Wide_Type (T)) then
18726
18727 -- The class-wide type is a partially decorated entity created for a
18728 -- unanalyzed tagged type referenced through a limited with clause.
18729 -- When the tagged type is analyzed, its class-wide type needs to be
18730 -- redecorated. Note that we reuse the entity created by Decorate_
18731 -- Tagged_Type in order to preserve all links.
18732
18733 if Materialize_Entity (Class_Wide_Type (T)) then
18734 CW_Type := Class_Wide_Type (T);
18735 Set_Materialize_Entity (CW_Type, False);
18736
18737 -- The class wide type can have been defined by the partial view, in
18738 -- which case everything is already done.
18739
18740 else
18741 return;
18742 end if;
18743
18744 -- Default case, we need to create a new class-wide type
18745
18746 else
18747 CW_Type :=
18748 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18749 end if;
18750
18751 -- Inherit root type characteristics
18752
18753 CW_Name := Chars (CW_Type);
18754 Next_E := Next_Entity (CW_Type);
18755 Prev_E := Prev_Entity (CW_Type);
18756 Copy_Node (T, CW_Type);
18757 Set_Comes_From_Source (CW_Type, False);
18758 Set_Chars (CW_Type, CW_Name);
18759 Set_Parent (CW_Type, Parent (T));
18760 Set_Prev_Entity (CW_Type, Prev_E);
18761 Set_Next_Entity (CW_Type, Next_E);
18762
18763 -- Ensure we have a new freeze node for the class-wide type. The partial
18764 -- view may have freeze action of its own, requiring a proper freeze
18765 -- node, and the same freeze node cannot be shared between the two
18766 -- types.
18767
18768 Set_Has_Delayed_Freeze (CW_Type);
18769 Set_Freeze_Node (CW_Type, Empty);
18770
18771 -- Customize the class-wide type: It has no prim. op., it cannot be
18772 -- abstract, its Etype points back to the specific root type, and it
18773 -- cannot have any invariants.
18774
18775 Set_Ekind (CW_Type, E_Class_Wide_Type);
18776 Set_Is_Tagged_Type (CW_Type, True);
18777 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18778 Set_Is_Abstract_Type (CW_Type, False);
18779 Set_Is_Constrained (CW_Type, False);
18780 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18781 Set_Default_SSO (CW_Type);
18782 Set_Has_Inheritable_Invariants (CW_Type, False);
18783 Set_Has_Inherited_Invariants (CW_Type, False);
18784 Set_Has_Own_Invariants (CW_Type, False);
18785
18786 if Ekind (T) = E_Class_Wide_Subtype then
18787 Set_Etype (CW_Type, Etype (Base_Type (T)));
18788 else
18789 Set_Etype (CW_Type, T);
18790 end if;
18791
18792 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18793
18794 -- If this is the class_wide type of a constrained subtype, it does
18795 -- not have discriminants.
18796
18797 Set_Has_Discriminants (CW_Type,
18798 Has_Discriminants (T) and then not Is_Constrained (T));
18799
18800 Set_Has_Unknown_Discriminants (CW_Type, True);
18801 Set_Class_Wide_Type (T, CW_Type);
18802 Set_Equivalent_Type (CW_Type, Empty);
18803
18804 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18805
18806 Set_Class_Wide_Type (CW_Type, CW_Type);
18807 end Make_Class_Wide_Type;
18808
18809 ----------------
18810 -- Make_Index --
18811 ----------------
18812
18813 procedure Make_Index
18814 (N : Node_Id;
18815 Related_Nod : Node_Id;
18816 Related_Id : Entity_Id := Empty;
18817 Suffix_Index : Nat := 1)
18818 is
18819 R : Node_Id;
18820 T : Entity_Id;
18821 Def_Id : Entity_Id := Empty;
18822 Found : Boolean := False;
18823
18824 begin
18825 -- For a discrete range used in a constrained array definition and
18826 -- defined by a range, an implicit conversion to the predefined type
18827 -- INTEGER is assumed if each bound is either a numeric literal, a named
18828 -- number, or an attribute, and the type of both bounds (prior to the
18829 -- implicit conversion) is the type universal_integer. Otherwise, both
18830 -- bounds must be of the same discrete type, other than universal
18831 -- integer; this type must be determinable independently of the
18832 -- context, but using the fact that the type must be discrete and that
18833 -- both bounds must have the same type.
18834
18835 -- Character literals also have a universal type in the absence of
18836 -- of additional context, and are resolved to Standard_Character.
18837
18838 if Nkind (N) = N_Range then
18839
18840 -- The index is given by a range constraint. The bounds are known
18841 -- to be of a consistent type.
18842
18843 if not Is_Overloaded (N) then
18844 T := Etype (N);
18845
18846 -- For universal bounds, choose the specific predefined type
18847
18848 if T = Universal_Integer then
18849 T := Standard_Integer;
18850
18851 elsif T = Any_Character then
18852 Ambiguous_Character (Low_Bound (N));
18853
18854 T := Standard_Character;
18855 end if;
18856
18857 -- The node may be overloaded because some user-defined operators
18858 -- are available, but if a universal interpretation exists it is
18859 -- also the selected one.
18860
18861 elsif Universal_Interpretation (N) = Universal_Integer then
18862 T := Standard_Integer;
18863
18864 else
18865 T := Any_Type;
18866
18867 declare
18868 Ind : Interp_Index;
18869 It : Interp;
18870
18871 begin
18872 Get_First_Interp (N, Ind, It);
18873 while Present (It.Typ) loop
18874 if Is_Discrete_Type (It.Typ) then
18875
18876 if Found
18877 and then not Covers (It.Typ, T)
18878 and then not Covers (T, It.Typ)
18879 then
18880 Error_Msg_N ("ambiguous bounds in discrete range", N);
18881 exit;
18882 else
18883 T := It.Typ;
18884 Found := True;
18885 end if;
18886 end if;
18887
18888 Get_Next_Interp (Ind, It);
18889 end loop;
18890
18891 if T = Any_Type then
18892 Error_Msg_N ("discrete type required for range", N);
18893 Set_Etype (N, Any_Type);
18894 return;
18895
18896 elsif T = Universal_Integer then
18897 T := Standard_Integer;
18898 end if;
18899 end;
18900 end if;
18901
18902 if not Is_Discrete_Type (T) then
18903 Error_Msg_N ("discrete type required for range", N);
18904 Set_Etype (N, Any_Type);
18905 return;
18906 end if;
18907
18908 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18909 and then Attribute_Name (Low_Bound (N)) = Name_First
18910 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18911 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18912 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18913 then
18914 -- The type of the index will be the type of the prefix, as long
18915 -- as the upper bound is 'Last of the same type.
18916
18917 Def_Id := Entity (Prefix (Low_Bound (N)));
18918
18919 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18920 or else Attribute_Name (High_Bound (N)) /= Name_Last
18921 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18922 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18923 then
18924 Def_Id := Empty;
18925 end if;
18926 end if;
18927
18928 R := N;
18929 Process_Range_Expr_In_Decl (R, T);
18930
18931 elsif Nkind (N) = N_Subtype_Indication then
18932
18933 -- The index is given by a subtype with a range constraint
18934
18935 T := Base_Type (Entity (Subtype_Mark (N)));
18936
18937 if not Is_Discrete_Type (T) then
18938 Error_Msg_N ("discrete type required for range", N);
18939 Set_Etype (N, Any_Type);
18940 return;
18941 end if;
18942
18943 R := Range_Expression (Constraint (N));
18944
18945 Resolve (R, T);
18946 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
18947
18948 elsif Nkind (N) = N_Attribute_Reference then
18949
18950 -- Catch beginner's error (use of attribute other than 'Range)
18951
18952 if Attribute_Name (N) /= Name_Range then
18953 Error_Msg_N ("expect attribute ''Range", N);
18954 Set_Etype (N, Any_Type);
18955 return;
18956 end if;
18957
18958 -- If the node denotes the range of a type mark, that is also the
18959 -- resulting type, and we do not need to create an Itype for it.
18960
18961 if Is_Entity_Name (Prefix (N))
18962 and then Comes_From_Source (N)
18963 and then Is_Type (Entity (Prefix (N)))
18964 and then Is_Discrete_Type (Entity (Prefix (N)))
18965 then
18966 Def_Id := Entity (Prefix (N));
18967 end if;
18968
18969 Analyze_And_Resolve (N);
18970 T := Etype (N);
18971 R := N;
18972
18973 -- If none of the above, must be a subtype. We convert this to a
18974 -- range attribute reference because in the case of declared first
18975 -- named subtypes, the types in the range reference can be different
18976 -- from the type of the entity. A range attribute normalizes the
18977 -- reference and obtains the correct types for the bounds.
18978
18979 -- This transformation is in the nature of an expansion, is only
18980 -- done if expansion is active. In particular, it is not done on
18981 -- formal generic types, because we need to retain the name of the
18982 -- original index for instantiation purposes.
18983
18984 else
18985 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18986 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18987 Set_Etype (N, Any_Integer);
18988 return;
18989
18990 else
18991 -- The type mark may be that of an incomplete type. It is only
18992 -- now that we can get the full view, previous analysis does
18993 -- not look specifically for a type mark.
18994
18995 Set_Entity (N, Get_Full_View (Entity (N)));
18996 Set_Etype (N, Entity (N));
18997 Def_Id := Entity (N);
18998
18999 if not Is_Discrete_Type (Def_Id) then
19000 Error_Msg_N ("discrete type required for index", N);
19001 Set_Etype (N, Any_Type);
19002 return;
19003 end if;
19004 end if;
19005
19006 if Expander_Active then
19007 Rewrite (N,
19008 Make_Attribute_Reference (Sloc (N),
19009 Attribute_Name => Name_Range,
19010 Prefix => Relocate_Node (N)));
19011
19012 -- The original was a subtype mark that does not freeze. This
19013 -- means that the rewritten version must not freeze either.
19014
19015 Set_Must_Not_Freeze (N);
19016 Set_Must_Not_Freeze (Prefix (N));
19017 Analyze_And_Resolve (N);
19018 T := Etype (N);
19019 R := N;
19020
19021 -- If expander is inactive, type is legal, nothing else to construct
19022
19023 else
19024 return;
19025 end if;
19026 end if;
19027
19028 if not Is_Discrete_Type (T) then
19029 Error_Msg_N ("discrete type required for range", N);
19030 Set_Etype (N, Any_Type);
19031 return;
19032
19033 elsif T = Any_Type then
19034 Set_Etype (N, Any_Type);
19035 return;
19036 end if;
19037
19038 -- We will now create the appropriate Itype to describe the range, but
19039 -- first a check. If we originally had a subtype, then we just label
19040 -- the range with this subtype. Not only is there no need to construct
19041 -- a new subtype, but it is wrong to do so for two reasons:
19042
19043 -- 1. A legality concern, if we have a subtype, it must not freeze,
19044 -- and the Itype would cause freezing incorrectly
19045
19046 -- 2. An efficiency concern, if we created an Itype, it would not be
19047 -- recognized as the same type for the purposes of eliminating
19048 -- checks in some circumstances.
19049
19050 -- We signal this case by setting the subtype entity in Def_Id
19051
19052 if No (Def_Id) then
19053 Def_Id :=
19054 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19055 Set_Etype (Def_Id, Base_Type (T));
19056
19057 if Is_Signed_Integer_Type (T) then
19058 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19059
19060 elsif Is_Modular_Integer_Type (T) then
19061 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19062
19063 else
19064 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19065 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19066 Set_First_Literal (Def_Id, First_Literal (T));
19067 end if;
19068
19069 Set_Size_Info (Def_Id, (T));
19070 Set_RM_Size (Def_Id, RM_Size (T));
19071 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19072
19073 Set_Scalar_Range (Def_Id, R);
19074 Conditional_Delay (Def_Id, T);
19075
19076 if Nkind (N) = N_Subtype_Indication then
19077 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19078 end if;
19079
19080 -- In the subtype indication case, if the immediate parent of the
19081 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19082 -- even if its bounds are static.
19083
19084 if Nkind (N) = N_Subtype_Indication
19085 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19086 then
19087 Set_Is_Non_Static_Subtype (Def_Id);
19088 end if;
19089 end if;
19090
19091 -- Final step is to label the index with this constructed type
19092
19093 Set_Etype (N, Def_Id);
19094 end Make_Index;
19095
19096 ------------------------------
19097 -- Modular_Type_Declaration --
19098 ------------------------------
19099
19100 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19101 Mod_Expr : constant Node_Id := Expression (Def);
19102 M_Val : Uint;
19103
19104 procedure Set_Modular_Size (Bits : Int);
19105 -- Sets RM_Size to Bits, and Esize to normal word size above this
19106
19107 ----------------------
19108 -- Set_Modular_Size --
19109 ----------------------
19110
19111 procedure Set_Modular_Size (Bits : Int) is
19112 begin
19113 Set_RM_Size (T, UI_From_Int (Bits));
19114
19115 if Bits <= 8 then
19116 Init_Esize (T, 8);
19117
19118 elsif Bits <= 16 then
19119 Init_Esize (T, 16);
19120
19121 elsif Bits <= 32 then
19122 Init_Esize (T, 32);
19123
19124 else
19125 Init_Esize (T, System_Max_Binary_Modulus_Power);
19126 end if;
19127
19128 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19129 Set_Is_Known_Valid (T);
19130 end if;
19131 end Set_Modular_Size;
19132
19133 -- Start of processing for Modular_Type_Declaration
19134
19135 begin
19136 -- If the mod expression is (exactly) 2 * literal, where literal is
19137 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19138
19139 if Warn_On_Suspicious_Modulus_Value
19140 and then Nkind (Mod_Expr) = N_Op_Multiply
19141 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19142 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19143 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19144 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19145 then
19146 Error_Msg_N
19147 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19148 end if;
19149
19150 -- Proceed with analysis of mod expression
19151
19152 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19153 Set_Etype (T, T);
19154 Set_Ekind (T, E_Modular_Integer_Type);
19155 Init_Alignment (T);
19156 Set_Is_Constrained (T);
19157
19158 if not Is_OK_Static_Expression (Mod_Expr) then
19159 Flag_Non_Static_Expr
19160 ("non-static expression used for modular type bound!", Mod_Expr);
19161 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19162 else
19163 M_Val := Expr_Value (Mod_Expr);
19164 end if;
19165
19166 if M_Val < 1 then
19167 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19168 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19169 end if;
19170
19171 if M_Val > 2 ** Standard_Long_Integer_Size then
19172 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19173 end if;
19174
19175 Set_Modulus (T, M_Val);
19176
19177 -- Create bounds for the modular type based on the modulus given in
19178 -- the type declaration and then analyze and resolve those bounds.
19179
19180 Set_Scalar_Range (T,
19181 Make_Range (Sloc (Mod_Expr),
19182 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19183 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19184
19185 -- Properly analyze the literals for the range. We do this manually
19186 -- because we can't go calling Resolve, since we are resolving these
19187 -- bounds with the type, and this type is certainly not complete yet.
19188
19189 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19190 Set_Etype (High_Bound (Scalar_Range (T)), T);
19191 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19192 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19193
19194 -- Loop through powers of two to find number of bits required
19195
19196 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19197
19198 -- Binary case
19199
19200 if M_Val = 2 ** Bits then
19201 Set_Modular_Size (Bits);
19202 return;
19203
19204 -- Nonbinary case
19205
19206 elsif M_Val < 2 ** Bits then
19207 Set_Non_Binary_Modulus (T);
19208
19209 if Bits > System_Max_Nonbinary_Modulus_Power then
19210 Error_Msg_Uint_1 :=
19211 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19212 Error_Msg_F
19213 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19214 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19215 return;
19216
19217 else
19218 -- In the nonbinary case, set size as per RM 13.3(55)
19219
19220 Set_Modular_Size (Bits);
19221 return;
19222 end if;
19223 end if;
19224
19225 end loop;
19226
19227 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19228 -- so we just signal an error and set the maximum size.
19229
19230 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19231 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19232
19233 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19234 Init_Alignment (T);
19235
19236 end Modular_Type_Declaration;
19237
19238 --------------------------
19239 -- New_Concatenation_Op --
19240 --------------------------
19241
19242 procedure New_Concatenation_Op (Typ : Entity_Id) is
19243 Loc : constant Source_Ptr := Sloc (Typ);
19244 Op : Entity_Id;
19245
19246 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19247 -- Create abbreviated declaration for the formal of a predefined
19248 -- Operator 'Op' of type 'Typ'
19249
19250 --------------------
19251 -- Make_Op_Formal --
19252 --------------------
19253
19254 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19255 Formal : Entity_Id;
19256 begin
19257 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19258 Set_Etype (Formal, Typ);
19259 Set_Mechanism (Formal, Default_Mechanism);
19260 return Formal;
19261 end Make_Op_Formal;
19262
19263 -- Start of processing for New_Concatenation_Op
19264
19265 begin
19266 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19267
19268 Set_Ekind (Op, E_Operator);
19269 Set_Scope (Op, Current_Scope);
19270 Set_Etype (Op, Typ);
19271 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19272 Set_Is_Immediately_Visible (Op);
19273 Set_Is_Intrinsic_Subprogram (Op);
19274 Set_Has_Completion (Op);
19275 Append_Entity (Op, Current_Scope);
19276
19277 Set_Name_Entity_Id (Name_Op_Concat, Op);
19278
19279 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19280 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19281 end New_Concatenation_Op;
19282
19283 -------------------------
19284 -- OK_For_Limited_Init --
19285 -------------------------
19286
19287 -- ???Check all calls of this, and compare the conditions under which it's
19288 -- called.
19289
19290 function OK_For_Limited_Init
19291 (Typ : Entity_Id;
19292 Exp : Node_Id) return Boolean
19293 is
19294 begin
19295 return Is_CPP_Constructor_Call (Exp)
19296 or else (Ada_Version >= Ada_2005
19297 and then not Debug_Flag_Dot_L
19298 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19299 end OK_For_Limited_Init;
19300
19301 -------------------------------
19302 -- OK_For_Limited_Init_In_05 --
19303 -------------------------------
19304
19305 function OK_For_Limited_Init_In_05
19306 (Typ : Entity_Id;
19307 Exp : Node_Id) return Boolean
19308 is
19309 begin
19310 -- An object of a limited interface type can be initialized with any
19311 -- expression of a nonlimited descendant type. However this does not
19312 -- apply if this is a view conversion of some other expression. This
19313 -- is checked below.
19314
19315 if Is_Class_Wide_Type (Typ)
19316 and then Is_Limited_Interface (Typ)
19317 and then not Is_Limited_Type (Etype (Exp))
19318 and then Nkind (Exp) /= N_Type_Conversion
19319 then
19320 return True;
19321 end if;
19322
19323 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19324 -- case of limited aggregates (including extension aggregates), and
19325 -- function calls. The function call may have been given in prefixed
19326 -- notation, in which case the original node is an indexed component.
19327 -- If the function is parameterless, the original node was an explicit
19328 -- dereference. The function may also be parameterless, in which case
19329 -- the source node is just an identifier.
19330
19331 -- A branch of a conditional expression may have been removed if the
19332 -- condition is statically known. This happens during expansion, and
19333 -- thus will not happen if previous errors were encountered. The check
19334 -- will have been performed on the chosen branch, which replaces the
19335 -- original conditional expression.
19336
19337 if No (Exp) then
19338 return True;
19339 end if;
19340
19341 case Nkind (Original_Node (Exp)) is
19342 when N_Aggregate
19343 | N_Extension_Aggregate
19344 | N_Function_Call
19345 | N_Op
19346 =>
19347 return True;
19348
19349 when N_Identifier =>
19350 return Present (Entity (Original_Node (Exp)))
19351 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19352
19353 when N_Qualified_Expression =>
19354 return
19355 OK_For_Limited_Init_In_05
19356 (Typ, Expression (Original_Node (Exp)));
19357
19358 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19359 -- with a function call, the expander has rewritten the call into an
19360 -- N_Type_Conversion node to force displacement of the pointer to
19361 -- reference the component containing the secondary dispatch table.
19362 -- Otherwise a type conversion is not a legal context.
19363 -- A return statement for a build-in-place function returning a
19364 -- synchronized type also introduces an unchecked conversion.
19365
19366 when N_Type_Conversion
19367 | N_Unchecked_Type_Conversion
19368 =>
19369 return not Comes_From_Source (Exp)
19370 and then
19371 -- If the conversion has been rewritten, check Original_Node
19372
19373 ((Original_Node (Exp) /= Exp
19374 and then
19375 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
19376
19377 -- Otherwise, check the expression of the compiler-generated
19378 -- conversion (which is a conversion that we want to ignore
19379 -- for purposes of the limited-initialization restrictions).
19380
19381 or else
19382 (Original_Node (Exp) = Exp
19383 and then
19384 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
19385
19386 when N_Explicit_Dereference
19387 | N_Indexed_Component
19388 | N_Selected_Component
19389 =>
19390 return Nkind (Exp) = N_Function_Call;
19391
19392 -- A use of 'Input is a function call, hence allowed. Normally the
19393 -- attribute will be changed to a call, but the attribute by itself
19394 -- can occur with -gnatc.
19395
19396 when N_Attribute_Reference =>
19397 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19398
19399 -- "return raise ..." is OK
19400
19401 when N_Raise_Expression =>
19402 return True;
19403
19404 -- For a case expression, all dependent expressions must be legal
19405
19406 when N_Case_Expression =>
19407 declare
19408 Alt : Node_Id;
19409
19410 begin
19411 Alt := First (Alternatives (Original_Node (Exp)));
19412 while Present (Alt) loop
19413 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19414 return False;
19415 end if;
19416
19417 Next (Alt);
19418 end loop;
19419
19420 return True;
19421 end;
19422
19423 -- For an if expression, all dependent expressions must be legal
19424
19425 when N_If_Expression =>
19426 declare
19427 Then_Expr : constant Node_Id :=
19428 Next (First (Expressions (Original_Node (Exp))));
19429 Else_Expr : constant Node_Id := Next (Then_Expr);
19430 begin
19431 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19432 and then
19433 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19434 end;
19435
19436 when others =>
19437 return False;
19438 end case;
19439 end OK_For_Limited_Init_In_05;
19440
19441 -------------------------------------------
19442 -- Ordinary_Fixed_Point_Type_Declaration --
19443 -------------------------------------------
19444
19445 procedure Ordinary_Fixed_Point_Type_Declaration
19446 (T : Entity_Id;
19447 Def : Node_Id)
19448 is
19449 Loc : constant Source_Ptr := Sloc (Def);
19450 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19451 RRS : constant Node_Id := Real_Range_Specification (Def);
19452 Implicit_Base : Entity_Id;
19453 Delta_Val : Ureal;
19454 Small_Val : Ureal;
19455 Low_Val : Ureal;
19456 High_Val : Ureal;
19457
19458 begin
19459 Check_Restriction (No_Fixed_Point, Def);
19460
19461 -- Create implicit base type
19462
19463 Implicit_Base :=
19464 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19465 Set_Etype (Implicit_Base, Implicit_Base);
19466
19467 -- Analyze and process delta expression
19468
19469 Analyze_And_Resolve (Delta_Expr, Any_Real);
19470
19471 Check_Delta_Expression (Delta_Expr);
19472 Delta_Val := Expr_Value_R (Delta_Expr);
19473
19474 Set_Delta_Value (Implicit_Base, Delta_Val);
19475
19476 -- Compute default small from given delta, which is the largest power
19477 -- of two that does not exceed the given delta value.
19478
19479 declare
19480 Tmp : Ureal;
19481 Scale : Int;
19482
19483 begin
19484 Tmp := Ureal_1;
19485 Scale := 0;
19486
19487 if Delta_Val < Ureal_1 then
19488 while Delta_Val < Tmp loop
19489 Tmp := Tmp / Ureal_2;
19490 Scale := Scale + 1;
19491 end loop;
19492
19493 else
19494 loop
19495 Tmp := Tmp * Ureal_2;
19496 exit when Tmp > Delta_Val;
19497 Scale := Scale - 1;
19498 end loop;
19499 end if;
19500
19501 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19502 end;
19503
19504 Set_Small_Value (Implicit_Base, Small_Val);
19505
19506 -- If no range was given, set a dummy range
19507
19508 if RRS <= Empty_Or_Error then
19509 Low_Val := -Small_Val;
19510 High_Val := Small_Val;
19511
19512 -- Otherwise analyze and process given range
19513
19514 else
19515 declare
19516 Low : constant Node_Id := Low_Bound (RRS);
19517 High : constant Node_Id := High_Bound (RRS);
19518
19519 begin
19520 Analyze_And_Resolve (Low, Any_Real);
19521 Analyze_And_Resolve (High, Any_Real);
19522 Check_Real_Bound (Low);
19523 Check_Real_Bound (High);
19524
19525 -- Obtain and set the range
19526
19527 Low_Val := Expr_Value_R (Low);
19528 High_Val := Expr_Value_R (High);
19529
19530 if Low_Val > High_Val then
19531 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19532 end if;
19533 end;
19534 end if;
19535
19536 -- The range for both the implicit base and the declared first subtype
19537 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19538 -- set a temporary range in place. Note that the bounds of the base
19539 -- type will be widened to be symmetrical and to fill the available
19540 -- bits when the type is frozen.
19541
19542 -- We could do this with all discrete types, and probably should, but
19543 -- we absolutely have to do it for fixed-point, since the end-points
19544 -- of the range and the size are determined by the small value, which
19545 -- could be reset before the freeze point.
19546
19547 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19548 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19549
19550 -- Complete definition of first subtype. The inheritance of the rep item
19551 -- chain ensures that SPARK-related pragmas are not clobbered when the
19552 -- ordinary fixed point type acts as a full view of a private type.
19553
19554 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19555 Set_Etype (T, Implicit_Base);
19556 Init_Size_Align (T);
19557 Inherit_Rep_Item_Chain (T, Implicit_Base);
19558 Set_Small_Value (T, Small_Val);
19559 Set_Delta_Value (T, Delta_Val);
19560 Set_Is_Constrained (T);
19561 end Ordinary_Fixed_Point_Type_Declaration;
19562
19563 ----------------------------------
19564 -- Preanalyze_Assert_Expression --
19565 ----------------------------------
19566
19567 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19568 begin
19569 In_Assertion_Expr := In_Assertion_Expr + 1;
19570 Preanalyze_Spec_Expression (N, T);
19571 In_Assertion_Expr := In_Assertion_Expr - 1;
19572 end Preanalyze_Assert_Expression;
19573
19574 -----------------------------------
19575 -- Preanalyze_Default_Expression --
19576 -----------------------------------
19577
19578 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19579 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19580 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19581
19582 begin
19583 In_Default_Expr := True;
19584 In_Spec_Expression := True;
19585
19586 Preanalyze_With_Freezing_And_Resolve (N, T);
19587
19588 In_Default_Expr := Save_In_Default_Expr;
19589 In_Spec_Expression := Save_In_Spec_Expression;
19590 end Preanalyze_Default_Expression;
19591
19592 --------------------------------
19593 -- Preanalyze_Spec_Expression --
19594 --------------------------------
19595
19596 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19597 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19598 begin
19599 In_Spec_Expression := True;
19600 Preanalyze_And_Resolve (N, T);
19601 In_Spec_Expression := Save_In_Spec_Expression;
19602 end Preanalyze_Spec_Expression;
19603
19604 ----------------------------------------
19605 -- Prepare_Private_Subtype_Completion --
19606 ----------------------------------------
19607
19608 procedure Prepare_Private_Subtype_Completion
19609 (Id : Entity_Id;
19610 Related_Nod : Node_Id)
19611 is
19612 Id_B : constant Entity_Id := Base_Type (Id);
19613 Full_B : constant Entity_Id := Full_View (Id_B);
19614 Full : Entity_Id;
19615
19616 begin
19617 if Present (Full_B) then
19618
19619 -- The Base_Type is already completed, we can complete the subtype
19620 -- now. We have to create a new entity with the same name, Thus we
19621 -- can't use Create_Itype.
19622
19623 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19624 Set_Is_Itype (Full);
19625 Set_Associated_Node_For_Itype (Full, Related_Nod);
19626 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19627 Set_Full_View (Id, Full);
19628 end if;
19629
19630 -- The parent subtype may be private, but the base might not, in some
19631 -- nested instances. In that case, the subtype does not need to be
19632 -- exchanged. It would still be nice to make private subtypes and their
19633 -- bases consistent at all times ???
19634
19635 if Is_Private_Type (Id_B) then
19636 Append_Elmt (Id, Private_Dependents (Id_B));
19637 end if;
19638 end Prepare_Private_Subtype_Completion;
19639
19640 ---------------------------
19641 -- Process_Discriminants --
19642 ---------------------------
19643
19644 procedure Process_Discriminants
19645 (N : Node_Id;
19646 Prev : Entity_Id := Empty)
19647 is
19648 Elist : constant Elist_Id := New_Elmt_List;
19649 Id : Node_Id;
19650 Discr : Node_Id;
19651 Discr_Number : Uint;
19652 Discr_Type : Entity_Id;
19653 Default_Present : Boolean := False;
19654 Default_Not_Present : Boolean := False;
19655
19656 begin
19657 -- A composite type other than an array type can have discriminants.
19658 -- On entry, the current scope is the composite type.
19659
19660 -- The discriminants are initially entered into the scope of the type
19661 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19662 -- use, as explained at the end of this procedure.
19663
19664 Discr := First (Discriminant_Specifications (N));
19665 while Present (Discr) loop
19666 Enter_Name (Defining_Identifier (Discr));
19667
19668 -- For navigation purposes we add a reference to the discriminant
19669 -- in the entity for the type. If the current declaration is a
19670 -- completion, place references on the partial view. Otherwise the
19671 -- type is the current scope.
19672
19673 if Present (Prev) then
19674
19675 -- The references go on the partial view, if present. If the
19676 -- partial view has discriminants, the references have been
19677 -- generated already.
19678
19679 if not Has_Discriminants (Prev) then
19680 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19681 end if;
19682 else
19683 Generate_Reference
19684 (Current_Scope, Defining_Identifier (Discr), 'd');
19685 end if;
19686
19687 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19688 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19689
19690 -- Ada 2005 (AI-254)
19691
19692 if Present (Access_To_Subprogram_Definition
19693 (Discriminant_Type (Discr)))
19694 and then Protected_Present (Access_To_Subprogram_Definition
19695 (Discriminant_Type (Discr)))
19696 then
19697 Discr_Type :=
19698 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19699 end if;
19700
19701 else
19702 Find_Type (Discriminant_Type (Discr));
19703 Discr_Type := Etype (Discriminant_Type (Discr));
19704
19705 if Error_Posted (Discriminant_Type (Discr)) then
19706 Discr_Type := Any_Type;
19707 end if;
19708 end if;
19709
19710 -- Handling of discriminants that are access types
19711
19712 if Is_Access_Type (Discr_Type) then
19713
19714 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19715 -- limited record types
19716
19717 if Ada_Version < Ada_2005 then
19718 Check_Access_Discriminant_Requires_Limited
19719 (Discr, Discriminant_Type (Discr));
19720 end if;
19721
19722 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19723 Error_Msg_N
19724 ("(Ada 83) access discriminant not allowed", Discr);
19725 end if;
19726
19727 -- If not access type, must be a discrete type
19728
19729 elsif not Is_Discrete_Type (Discr_Type) then
19730 Error_Msg_N
19731 ("discriminants must have a discrete or access type",
19732 Discriminant_Type (Discr));
19733 end if;
19734
19735 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19736
19737 -- If a discriminant specification includes the assignment compound
19738 -- delimiter followed by an expression, the expression is the default
19739 -- expression of the discriminant; the default expression must be of
19740 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19741 -- a default expression, we do the special preanalysis, since this
19742 -- expression does not freeze (see section "Handling of Default and
19743 -- Per-Object Expressions" in spec of package Sem).
19744
19745 if Present (Expression (Discr)) then
19746 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19747
19748 -- Legaity checks
19749
19750 if Nkind (N) = N_Formal_Type_Declaration then
19751 Error_Msg_N
19752 ("discriminant defaults not allowed for formal type",
19753 Expression (Discr));
19754
19755 -- Flag an error for a tagged type with defaulted discriminants,
19756 -- excluding limited tagged types when compiling for Ada 2012
19757 -- (see AI05-0214).
19758
19759 elsif Is_Tagged_Type (Current_Scope)
19760 and then (not Is_Limited_Type (Current_Scope)
19761 or else Ada_Version < Ada_2012)
19762 and then Comes_From_Source (N)
19763 then
19764 -- Note: see similar test in Check_Or_Process_Discriminants, to
19765 -- handle the (illegal) case of the completion of an untagged
19766 -- view with discriminants with defaults by a tagged full view.
19767 -- We skip the check if Discr does not come from source, to
19768 -- account for the case of an untagged derived type providing
19769 -- defaults for a renamed discriminant from a private untagged
19770 -- ancestor with a tagged full view (ACATS B460006).
19771
19772 if Ada_Version >= Ada_2012 then
19773 Error_Msg_N
19774 ("discriminants of nonlimited tagged type cannot have"
19775 & " defaults",
19776 Expression (Discr));
19777 else
19778 Error_Msg_N
19779 ("discriminants of tagged type cannot have defaults",
19780 Expression (Discr));
19781 end if;
19782
19783 else
19784 Default_Present := True;
19785 Append_Elmt (Expression (Discr), Elist);
19786
19787 -- Tag the defining identifiers for the discriminants with
19788 -- their corresponding default expressions from the tree.
19789
19790 Set_Discriminant_Default_Value
19791 (Defining_Identifier (Discr), Expression (Discr));
19792 end if;
19793
19794 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19795 -- gets set unless we can be sure that no range check is required.
19796
19797 if (GNATprove_Mode or not Expander_Active)
19798 and then not
19799 Is_In_Range
19800 (Expression (Discr), Discr_Type, Assume_Valid => True)
19801 then
19802 Set_Do_Range_Check (Expression (Discr));
19803 end if;
19804
19805 -- No default discriminant value given
19806
19807 else
19808 Default_Not_Present := True;
19809 end if;
19810
19811 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19812 -- Discr_Type but with the null-exclusion attribute
19813
19814 if Ada_Version >= Ada_2005 then
19815
19816 -- Ada 2005 (AI-231): Static checks
19817
19818 if Can_Never_Be_Null (Discr_Type) then
19819 Null_Exclusion_Static_Checks (Discr);
19820
19821 elsif Is_Access_Type (Discr_Type)
19822 and then Null_Exclusion_Present (Discr)
19823
19824 -- No need to check itypes because in their case this check
19825 -- was done at their point of creation
19826
19827 and then not Is_Itype (Discr_Type)
19828 then
19829 if Can_Never_Be_Null (Discr_Type) then
19830 Error_Msg_NE
19831 ("`NOT NULL` not allowed (& already excludes null)",
19832 Discr,
19833 Discr_Type);
19834 end if;
19835
19836 Set_Etype (Defining_Identifier (Discr),
19837 Create_Null_Excluding_Itype
19838 (T => Discr_Type,
19839 Related_Nod => Discr));
19840
19841 -- Check for improper null exclusion if the type is otherwise
19842 -- legal for a discriminant.
19843
19844 elsif Null_Exclusion_Present (Discr)
19845 and then Is_Discrete_Type (Discr_Type)
19846 then
19847 Error_Msg_N
19848 ("null exclusion can only apply to an access type", Discr);
19849 end if;
19850
19851 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19852 -- can't have defaults. Synchronized types, or types that are
19853 -- explicitly limited are fine, but special tests apply to derived
19854 -- types in generics: in a generic body we have to assume the
19855 -- worst, and therefore defaults are not allowed if the parent is
19856 -- a generic formal private type (see ACATS B370001).
19857
19858 if Is_Access_Type (Discr_Type) and then Default_Present then
19859 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19860 or else Is_Limited_Record (Current_Scope)
19861 or else Is_Concurrent_Type (Current_Scope)
19862 or else Is_Concurrent_Record_Type (Current_Scope)
19863 or else Ekind (Current_Scope) = E_Limited_Private_Type
19864 then
19865 if not Is_Derived_Type (Current_Scope)
19866 or else not Is_Generic_Type (Etype (Current_Scope))
19867 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19868 or else Limited_Present
19869 (Type_Definition (Parent (Current_Scope)))
19870 then
19871 null;
19872
19873 else
19874 Error_Msg_N
19875 ("access discriminants of nonlimited types cannot "
19876 & "have defaults", Expression (Discr));
19877 end if;
19878
19879 elsif Present (Expression (Discr)) then
19880 Error_Msg_N
19881 ("(Ada 2005) access discriminants of nonlimited types "
19882 & "cannot have defaults", Expression (Discr));
19883 end if;
19884 end if;
19885 end if;
19886
19887 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
19888 -- This check is relevant only when SPARK_Mode is on as it is not a
19889 -- standard Ada legality rule.
19890
19891 if SPARK_Mode = On
19892 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19893 then
19894 Error_Msg_N ("discriminant cannot be volatile", Discr);
19895 end if;
19896
19897 Next (Discr);
19898 end loop;
19899
19900 -- An element list consisting of the default expressions of the
19901 -- discriminants is constructed in the above loop and used to set
19902 -- the Discriminant_Constraint attribute for the type. If an object
19903 -- is declared of this (record or task) type without any explicit
19904 -- discriminant constraint given, this element list will form the
19905 -- actual parameters for the corresponding initialization procedure
19906 -- for the type.
19907
19908 Set_Discriminant_Constraint (Current_Scope, Elist);
19909 Set_Stored_Constraint (Current_Scope, No_Elist);
19910
19911 -- Default expressions must be provided either for all or for none
19912 -- of the discriminants of a discriminant part. (RM 3.7.1)
19913
19914 if Default_Present and then Default_Not_Present then
19915 Error_Msg_N
19916 ("incomplete specification of defaults for discriminants", N);
19917 end if;
19918
19919 -- The use of the name of a discriminant is not allowed in default
19920 -- expressions of a discriminant part if the specification of the
19921 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19922
19923 -- To detect this, the discriminant names are entered initially with an
19924 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19925 -- attempt to use a void entity (for example in an expression that is
19926 -- type-checked) produces the error message: premature usage. Now after
19927 -- completing the semantic analysis of the discriminant part, we can set
19928 -- the Ekind of all the discriminants appropriately.
19929
19930 Discr := First (Discriminant_Specifications (N));
19931 Discr_Number := Uint_1;
19932 while Present (Discr) loop
19933 Id := Defining_Identifier (Discr);
19934 Set_Ekind (Id, E_Discriminant);
19935 Init_Component_Location (Id);
19936 Init_Esize (Id);
19937 Set_Discriminant_Number (Id, Discr_Number);
19938
19939 -- Make sure this is always set, even in illegal programs
19940
19941 Set_Corresponding_Discriminant (Id, Empty);
19942
19943 -- Initialize the Original_Record_Component to the entity itself.
19944 -- Inherit_Components will propagate the right value to
19945 -- discriminants in derived record types.
19946
19947 Set_Original_Record_Component (Id, Id);
19948
19949 -- Create the discriminal for the discriminant
19950
19951 Build_Discriminal (Id);
19952
19953 Next (Discr);
19954 Discr_Number := Discr_Number + 1;
19955 end loop;
19956
19957 Set_Has_Discriminants (Current_Scope);
19958 end Process_Discriminants;
19959
19960 -----------------------
19961 -- Process_Full_View --
19962 -----------------------
19963
19964 -- WARNING: This routine manages Ghost regions. Return statements must be
19965 -- replaced by gotos which jump to the end of the routine and restore the
19966 -- Ghost mode.
19967
19968 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19969 procedure Collect_Implemented_Interfaces
19970 (Typ : Entity_Id;
19971 Ifaces : Elist_Id);
19972 -- Ada 2005: Gather all the interfaces that Typ directly or
19973 -- inherently implements. Duplicate entries are not added to
19974 -- the list Ifaces.
19975
19976 ------------------------------------
19977 -- Collect_Implemented_Interfaces --
19978 ------------------------------------
19979
19980 procedure Collect_Implemented_Interfaces
19981 (Typ : Entity_Id;
19982 Ifaces : Elist_Id)
19983 is
19984 Iface : Entity_Id;
19985 Iface_Elmt : Elmt_Id;
19986
19987 begin
19988 -- Abstract interfaces are only associated with tagged record types
19989
19990 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19991 return;
19992 end if;
19993
19994 -- Recursively climb to the ancestors
19995
19996 if Etype (Typ) /= Typ
19997
19998 -- Protect the frontend against wrong cyclic declarations like:
19999
20000 -- type B is new A with private;
20001 -- type C is new A with private;
20002 -- private
20003 -- type B is new C with null record;
20004 -- type C is new B with null record;
20005
20006 and then Etype (Typ) /= Priv_T
20007 and then Etype (Typ) /= Full_T
20008 then
20009 -- Keep separate the management of private type declarations
20010
20011 if Ekind (Typ) = E_Record_Type_With_Private then
20012
20013 -- Handle the following illegal usage:
20014 -- type Private_Type is tagged private;
20015 -- private
20016 -- type Private_Type is new Type_Implementing_Iface;
20017
20018 if Present (Full_View (Typ))
20019 and then Etype (Typ) /= Full_View (Typ)
20020 then
20021 if Is_Interface (Etype (Typ)) then
20022 Append_Unique_Elmt (Etype (Typ), Ifaces);
20023 end if;
20024
20025 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20026 end if;
20027
20028 -- Non-private types
20029
20030 else
20031 if Is_Interface (Etype (Typ)) then
20032 Append_Unique_Elmt (Etype (Typ), Ifaces);
20033 end if;
20034
20035 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20036 end if;
20037 end if;
20038
20039 -- Handle entities in the list of abstract interfaces
20040
20041 if Present (Interfaces (Typ)) then
20042 Iface_Elmt := First_Elmt (Interfaces (Typ));
20043 while Present (Iface_Elmt) loop
20044 Iface := Node (Iface_Elmt);
20045
20046 pragma Assert (Is_Interface (Iface));
20047
20048 if not Contain_Interface (Iface, Ifaces) then
20049 Append_Elmt (Iface, Ifaces);
20050 Collect_Implemented_Interfaces (Iface, Ifaces);
20051 end if;
20052
20053 Next_Elmt (Iface_Elmt);
20054 end loop;
20055 end if;
20056 end Collect_Implemented_Interfaces;
20057
20058 -- Local variables
20059
20060 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20061 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20062 -- Save the Ghost-related attributes to restore on exit
20063
20064 Full_Indic : Node_Id;
20065 Full_Parent : Entity_Id;
20066 Priv_Parent : Entity_Id;
20067
20068 -- Start of processing for Process_Full_View
20069
20070 begin
20071 Mark_And_Set_Ghost_Completion (N, Priv_T);
20072
20073 -- First some sanity checks that must be done after semantic
20074 -- decoration of the full view and thus cannot be placed with other
20075 -- similar checks in Find_Type_Name
20076
20077 if not Is_Limited_Type (Priv_T)
20078 and then (Is_Limited_Type (Full_T)
20079 or else Is_Limited_Composite (Full_T))
20080 then
20081 if In_Instance then
20082 null;
20083 else
20084 Error_Msg_N
20085 ("completion of nonlimited type cannot be limited", Full_T);
20086 Explain_Limited_Type (Full_T, Full_T);
20087 end if;
20088
20089 elsif Is_Abstract_Type (Full_T)
20090 and then not Is_Abstract_Type (Priv_T)
20091 then
20092 Error_Msg_N
20093 ("completion of nonabstract type cannot be abstract", Full_T);
20094
20095 elsif Is_Tagged_Type (Priv_T)
20096 and then Is_Limited_Type (Priv_T)
20097 and then not Is_Limited_Type (Full_T)
20098 then
20099 -- If pragma CPP_Class was applied to the private declaration
20100 -- propagate the limitedness to the full-view
20101
20102 if Is_CPP_Class (Priv_T) then
20103 Set_Is_Limited_Record (Full_T);
20104
20105 -- GNAT allow its own definition of Limited_Controlled to disobey
20106 -- this rule in order in ease the implementation. This test is safe
20107 -- because Root_Controlled is defined in a child of System that
20108 -- normal programs are not supposed to use.
20109
20110 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20111 Set_Is_Limited_Composite (Full_T);
20112 else
20113 Error_Msg_N
20114 ("completion of limited tagged type must be limited", Full_T);
20115 end if;
20116
20117 elsif Is_Generic_Type (Priv_T) then
20118 Error_Msg_N ("generic type cannot have a completion", Full_T);
20119 end if;
20120
20121 -- Check that ancestor interfaces of private and full views are
20122 -- consistent. We omit this check for synchronized types because
20123 -- they are performed on the corresponding record type when frozen.
20124
20125 if Ada_Version >= Ada_2005
20126 and then Is_Tagged_Type (Priv_T)
20127 and then Is_Tagged_Type (Full_T)
20128 and then not Is_Concurrent_Type (Full_T)
20129 then
20130 declare
20131 Iface : Entity_Id;
20132 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20133 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20134
20135 begin
20136 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20137 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20138
20139 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20140 -- an interface type if and only if the full type is descendant
20141 -- of the interface type (AARM 7.3 (7.3/2)).
20142
20143 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20144
20145 if Present (Iface) then
20146 Error_Msg_NE
20147 ("interface in partial view& not implemented by full type "
20148 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20149 end if;
20150
20151 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20152
20153 if Present (Iface) then
20154 Error_Msg_NE
20155 ("interface & not implemented by partial view "
20156 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20157 end if;
20158 end;
20159 end if;
20160
20161 if Is_Tagged_Type (Priv_T)
20162 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20163 and then Is_Derived_Type (Full_T)
20164 then
20165 Priv_Parent := Etype (Priv_T);
20166
20167 -- The full view of a private extension may have been transformed
20168 -- into an unconstrained derived type declaration and a subtype
20169 -- declaration (see build_derived_record_type for details).
20170
20171 if Nkind (N) = N_Subtype_Declaration then
20172 Full_Indic := Subtype_Indication (N);
20173 Full_Parent := Etype (Base_Type (Full_T));
20174 else
20175 Full_Indic := Subtype_Indication (Type_Definition (N));
20176 Full_Parent := Etype (Full_T);
20177 end if;
20178
20179 -- Check that the parent type of the full type is a descendant of
20180 -- the ancestor subtype given in the private extension. If either
20181 -- entity has an Etype equal to Any_Type then we had some previous
20182 -- error situation [7.3(8)].
20183
20184 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20185 goto Leave;
20186
20187 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20188 -- any order. Therefore we don't have to check that its parent must
20189 -- be a descendant of the parent of the private type declaration.
20190
20191 elsif Is_Interface (Priv_Parent)
20192 and then Is_Interface (Full_Parent)
20193 then
20194 null;
20195
20196 -- Ada 2005 (AI-251): If the parent of the private type declaration
20197 -- is an interface there is no need to check that it is an ancestor
20198 -- of the associated full type declaration. The required tests for
20199 -- this case are performed by Build_Derived_Record_Type.
20200
20201 elsif not Is_Interface (Base_Type (Priv_Parent))
20202 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20203 then
20204 Error_Msg_N
20205 ("parent of full type must descend from parent of private "
20206 & "extension", Full_Indic);
20207
20208 -- First check a formal restriction, and then proceed with checking
20209 -- Ada rules. Since the formal restriction is not a serious error, we
20210 -- don't prevent further error detection for this check, hence the
20211 -- ELSE.
20212
20213 else
20214 -- Check the rules of 7.3(10): if the private extension inherits
20215 -- known discriminants, then the full type must also inherit those
20216 -- discriminants from the same (ancestor) type, and the parent
20217 -- subtype of the full type must be constrained if and only if
20218 -- the ancestor subtype of the private extension is constrained.
20219
20220 if No (Discriminant_Specifications (Parent (Priv_T)))
20221 and then not Has_Unknown_Discriminants (Priv_T)
20222 and then Has_Discriminants (Base_Type (Priv_Parent))
20223 then
20224 declare
20225 Priv_Indic : constant Node_Id :=
20226 Subtype_Indication (Parent (Priv_T));
20227
20228 Priv_Constr : constant Boolean :=
20229 Is_Constrained (Priv_Parent)
20230 or else
20231 Nkind (Priv_Indic) = N_Subtype_Indication
20232 or else
20233 Is_Constrained (Entity (Priv_Indic));
20234
20235 Full_Constr : constant Boolean :=
20236 Is_Constrained (Full_Parent)
20237 or else
20238 Nkind (Full_Indic) = N_Subtype_Indication
20239 or else
20240 Is_Constrained (Entity (Full_Indic));
20241
20242 Priv_Discr : Entity_Id;
20243 Full_Discr : Entity_Id;
20244
20245 begin
20246 Priv_Discr := First_Discriminant (Priv_Parent);
20247 Full_Discr := First_Discriminant (Full_Parent);
20248 while Present (Priv_Discr) and then Present (Full_Discr) loop
20249 if Original_Record_Component (Priv_Discr) =
20250 Original_Record_Component (Full_Discr)
20251 or else
20252 Corresponding_Discriminant (Priv_Discr) =
20253 Corresponding_Discriminant (Full_Discr)
20254 then
20255 null;
20256 else
20257 exit;
20258 end if;
20259
20260 Next_Discriminant (Priv_Discr);
20261 Next_Discriminant (Full_Discr);
20262 end loop;
20263
20264 if Present (Priv_Discr) or else Present (Full_Discr) then
20265 Error_Msg_N
20266 ("full view must inherit discriminants of the parent "
20267 & "type used in the private extension", Full_Indic);
20268
20269 elsif Priv_Constr and then not Full_Constr then
20270 Error_Msg_N
20271 ("parent subtype of full type must be constrained",
20272 Full_Indic);
20273
20274 elsif Full_Constr and then not Priv_Constr then
20275 Error_Msg_N
20276 ("parent subtype of full type must be unconstrained",
20277 Full_Indic);
20278 end if;
20279 end;
20280
20281 -- Check the rules of 7.3(12): if a partial view has neither
20282 -- known or unknown discriminants, then the full type
20283 -- declaration shall define a definite subtype.
20284
20285 elsif not Has_Unknown_Discriminants (Priv_T)
20286 and then not Has_Discriminants (Priv_T)
20287 and then not Is_Constrained (Full_T)
20288 then
20289 Error_Msg_N
20290 ("full view must define a constrained type if partial view "
20291 & "has no discriminants", Full_T);
20292 end if;
20293
20294 -- ??????? Do we implement the following properly ?????
20295 -- If the ancestor subtype of a private extension has constrained
20296 -- discriminants, then the parent subtype of the full view shall
20297 -- impose a statically matching constraint on those discriminants
20298 -- [7.3(13)].
20299 end if;
20300
20301 else
20302 -- For untagged types, verify that a type without discriminants is
20303 -- not completed with an unconstrained type. A separate error message
20304 -- is produced if the full type has defaulted discriminants.
20305
20306 if Is_Definite_Subtype (Priv_T)
20307 and then not Is_Definite_Subtype (Full_T)
20308 then
20309 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20310 Error_Msg_NE
20311 ("full view of& not compatible with declaration#",
20312 Full_T, Priv_T);
20313
20314 if not Is_Tagged_Type (Full_T) then
20315 Error_Msg_N
20316 ("\one is constrained, the other unconstrained", Full_T);
20317 end if;
20318 end if;
20319 end if;
20320
20321 -- AI-419: verify that the use of "limited" is consistent
20322
20323 declare
20324 Orig_Decl : constant Node_Id := Original_Node (N);
20325
20326 begin
20327 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20328 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20329 and then Nkind
20330 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20331 then
20332 if not Limited_Present (Parent (Priv_T))
20333 and then not Synchronized_Present (Parent (Priv_T))
20334 and then Limited_Present (Type_Definition (Orig_Decl))
20335 then
20336 Error_Msg_N
20337 ("full view of non-limited extension cannot be limited", N);
20338
20339 -- Conversely, if the partial view carries the limited keyword,
20340 -- the full view must as well, even if it may be redundant.
20341
20342 elsif Limited_Present (Parent (Priv_T))
20343 and then not Limited_Present (Type_Definition (Orig_Decl))
20344 then
20345 Error_Msg_N
20346 ("full view of limited extension must be explicitly limited",
20347 N);
20348 end if;
20349 end if;
20350 end;
20351
20352 -- Ada 2005 (AI-443): A synchronized private extension must be
20353 -- completed by a task or protected type.
20354
20355 if Ada_Version >= Ada_2005
20356 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20357 and then Synchronized_Present (Parent (Priv_T))
20358 and then not Is_Concurrent_Type (Full_T)
20359 then
20360 Error_Msg_N ("full view of synchronized extension must " &
20361 "be synchronized type", N);
20362 end if;
20363
20364 -- Ada 2005 AI-363: if the full view has discriminants with
20365 -- defaults, it is illegal to declare constrained access subtypes
20366 -- whose designated type is the current type. This allows objects
20367 -- of the type that are declared in the heap to be unconstrained.
20368
20369 if not Has_Unknown_Discriminants (Priv_T)
20370 and then not Has_Discriminants (Priv_T)
20371 and then Has_Discriminants (Full_T)
20372 and then
20373 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20374 then
20375 Set_Has_Constrained_Partial_View (Full_T);
20376 Set_Has_Constrained_Partial_View (Priv_T);
20377 end if;
20378
20379 -- Create a full declaration for all its subtypes recorded in
20380 -- Private_Dependents and swap them similarly to the base type. These
20381 -- are subtypes that have been define before the full declaration of
20382 -- the private type. We also swap the entry in Private_Dependents list
20383 -- so we can properly restore the private view on exit from the scope.
20384
20385 declare
20386 Priv_Elmt : Elmt_Id;
20387 Priv_Scop : Entity_Id;
20388 Priv : Entity_Id;
20389 Full : Entity_Id;
20390
20391 begin
20392 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20393 while Present (Priv_Elmt) loop
20394 Priv := Node (Priv_Elmt);
20395 Priv_Scop := Scope (Priv);
20396
20397 if Ekind_In (Priv, E_Private_Subtype,
20398 E_Limited_Private_Subtype,
20399 E_Record_Subtype_With_Private)
20400 then
20401 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20402 Set_Is_Itype (Full);
20403 Set_Parent (Full, Parent (Priv));
20404 Set_Associated_Node_For_Itype (Full, N);
20405
20406 -- Now we need to complete the private subtype, but since the
20407 -- base type has already been swapped, we must also swap the
20408 -- subtypes (and thus, reverse the arguments in the call to
20409 -- Complete_Private_Subtype). Also note that we may need to
20410 -- re-establish the scope of the private subtype.
20411
20412 Copy_And_Swap (Priv, Full);
20413
20414 if not In_Open_Scopes (Priv_Scop) then
20415 Push_Scope (Priv_Scop);
20416
20417 else
20418 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20419
20420 Priv_Scop := Empty;
20421 end if;
20422
20423 Complete_Private_Subtype (Full, Priv, Full_T, N);
20424 Set_Full_View (Full, Priv);
20425
20426 if Present (Priv_Scop) then
20427 Pop_Scope;
20428 end if;
20429
20430 Replace_Elmt (Priv_Elmt, Full);
20431 end if;
20432
20433 Next_Elmt (Priv_Elmt);
20434 end loop;
20435 end;
20436
20437 -- If the private view was tagged, copy the new primitive operations
20438 -- from the private view to the full view.
20439
20440 if Is_Tagged_Type (Full_T) then
20441 declare
20442 Disp_Typ : Entity_Id;
20443 Full_List : Elist_Id;
20444 Prim : Entity_Id;
20445 Prim_Elmt : Elmt_Id;
20446 Priv_List : Elist_Id;
20447
20448 function Contains
20449 (E : Entity_Id;
20450 L : Elist_Id) return Boolean;
20451 -- Determine whether list L contains element E
20452
20453 --------------
20454 -- Contains --
20455 --------------
20456
20457 function Contains
20458 (E : Entity_Id;
20459 L : Elist_Id) return Boolean
20460 is
20461 List_Elmt : Elmt_Id;
20462
20463 begin
20464 List_Elmt := First_Elmt (L);
20465 while Present (List_Elmt) loop
20466 if Node (List_Elmt) = E then
20467 return True;
20468 end if;
20469
20470 Next_Elmt (List_Elmt);
20471 end loop;
20472
20473 return False;
20474 end Contains;
20475
20476 -- Start of processing
20477
20478 begin
20479 if Is_Tagged_Type (Priv_T) then
20480 Priv_List := Primitive_Operations (Priv_T);
20481 Prim_Elmt := First_Elmt (Priv_List);
20482
20483 -- In the case of a concurrent type completing a private tagged
20484 -- type, primitives may have been declared in between the two
20485 -- views. These subprograms need to be wrapped the same way
20486 -- entries and protected procedures are handled because they
20487 -- cannot be directly shared by the two views.
20488
20489 if Is_Concurrent_Type (Full_T) then
20490 declare
20491 Conc_Typ : constant Entity_Id :=
20492 Corresponding_Record_Type (Full_T);
20493 Curr_Nod : Node_Id := Parent (Conc_Typ);
20494 Wrap_Spec : Node_Id;
20495
20496 begin
20497 while Present (Prim_Elmt) loop
20498 Prim := Node (Prim_Elmt);
20499
20500 if Comes_From_Source (Prim)
20501 and then not Is_Abstract_Subprogram (Prim)
20502 then
20503 Wrap_Spec :=
20504 Make_Subprogram_Declaration (Sloc (Prim),
20505 Specification =>
20506 Build_Wrapper_Spec
20507 (Subp_Id => Prim,
20508 Obj_Typ => Conc_Typ,
20509 Formals =>
20510 Parameter_Specifications
20511 (Parent (Prim))));
20512
20513 Insert_After (Curr_Nod, Wrap_Spec);
20514 Curr_Nod := Wrap_Spec;
20515
20516 Analyze (Wrap_Spec);
20517
20518 -- Remove the wrapper from visibility to avoid
20519 -- spurious conflict with the wrapped entity.
20520
20521 Set_Is_Immediately_Visible
20522 (Defining_Entity (Specification (Wrap_Spec)),
20523 False);
20524 end if;
20525
20526 Next_Elmt (Prim_Elmt);
20527 end loop;
20528
20529 goto Leave;
20530 end;
20531
20532 -- For non-concurrent types, transfer explicit primitives, but
20533 -- omit those inherited from the parent of the private view
20534 -- since they will be re-inherited later on.
20535
20536 else
20537 Full_List := Primitive_Operations (Full_T);
20538 while Present (Prim_Elmt) loop
20539 Prim := Node (Prim_Elmt);
20540
20541 if Comes_From_Source (Prim)
20542 and then not Contains (Prim, Full_List)
20543 then
20544 Append_Elmt (Prim, Full_List);
20545 end if;
20546
20547 Next_Elmt (Prim_Elmt);
20548 end loop;
20549 end if;
20550
20551 -- Untagged private view
20552
20553 else
20554 Full_List := Primitive_Operations (Full_T);
20555
20556 -- In this case the partial view is untagged, so here we locate
20557 -- all of the earlier primitives that need to be treated as
20558 -- dispatching (those that appear between the two views). Note
20559 -- that these additional operations must all be new operations
20560 -- (any earlier operations that override inherited operations
20561 -- of the full view will already have been inserted in the
20562 -- primitives list, marked by Check_Operation_From_Private_View
20563 -- as dispatching. Note that implicit "/=" operators are
20564 -- excluded from being added to the primitives list since they
20565 -- shouldn't be treated as dispatching (tagged "/=" is handled
20566 -- specially).
20567
20568 Prim := Next_Entity (Full_T);
20569 while Present (Prim) and then Prim /= Priv_T loop
20570 if Ekind_In (Prim, E_Procedure, E_Function) then
20571 Disp_Typ := Find_Dispatching_Type (Prim);
20572
20573 if Disp_Typ = Full_T
20574 and then (Chars (Prim) /= Name_Op_Ne
20575 or else Comes_From_Source (Prim))
20576 then
20577 Check_Controlling_Formals (Full_T, Prim);
20578
20579 if Is_Suitable_Primitive (Prim)
20580 and then not Is_Dispatching_Operation (Prim)
20581 then
20582 Append_Elmt (Prim, Full_List);
20583 Set_Is_Dispatching_Operation (Prim);
20584 Set_DT_Position_Value (Prim, No_Uint);
20585 end if;
20586
20587 elsif Is_Dispatching_Operation (Prim)
20588 and then Disp_Typ /= Full_T
20589 then
20590 -- Verify that it is not otherwise controlled by a
20591 -- formal or a return value of type T.
20592
20593 Check_Controlling_Formals (Disp_Typ, Prim);
20594 end if;
20595 end if;
20596
20597 Next_Entity (Prim);
20598 end loop;
20599 end if;
20600
20601 -- For the tagged case, the two views can share the same primitive
20602 -- operations list and the same class-wide type. Update attributes
20603 -- of the class-wide type which depend on the full declaration.
20604
20605 if Is_Tagged_Type (Priv_T) then
20606 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20607 Set_Class_Wide_Type
20608 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20609
20610 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20611 end if;
20612 end;
20613 end if;
20614
20615 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20616
20617 if Known_To_Have_Preelab_Init (Priv_T) then
20618
20619 -- Case where there is a pragma Preelaborable_Initialization. We
20620 -- always allow this in predefined units, which is cheating a bit,
20621 -- but it means we don't have to struggle to meet the requirements in
20622 -- the RM for having Preelaborable Initialization. Otherwise we
20623 -- require that the type meets the RM rules. But we can't check that
20624 -- yet, because of the rule about overriding Initialize, so we simply
20625 -- set a flag that will be checked at freeze time.
20626
20627 if not In_Predefined_Unit (Full_T) then
20628 Set_Must_Have_Preelab_Init (Full_T);
20629 end if;
20630 end if;
20631
20632 -- If pragma CPP_Class was applied to the private type declaration,
20633 -- propagate it now to the full type declaration.
20634
20635 if Is_CPP_Class (Priv_T) then
20636 Set_Is_CPP_Class (Full_T);
20637 Set_Convention (Full_T, Convention_CPP);
20638
20639 -- Check that components of imported CPP types do not have default
20640 -- expressions.
20641
20642 Check_CPP_Type_Has_No_Defaults (Full_T);
20643 end if;
20644
20645 -- If the private view has user specified stream attributes, then so has
20646 -- the full view.
20647
20648 -- Why the test, how could these flags be already set in Full_T ???
20649
20650 if Has_Specified_Stream_Read (Priv_T) then
20651 Set_Has_Specified_Stream_Read (Full_T);
20652 end if;
20653
20654 if Has_Specified_Stream_Write (Priv_T) then
20655 Set_Has_Specified_Stream_Write (Full_T);
20656 end if;
20657
20658 if Has_Specified_Stream_Input (Priv_T) then
20659 Set_Has_Specified_Stream_Input (Full_T);
20660 end if;
20661
20662 if Has_Specified_Stream_Output (Priv_T) then
20663 Set_Has_Specified_Stream_Output (Full_T);
20664 end if;
20665
20666 -- Propagate Default_Initial_Condition-related attributes from the
20667 -- partial view to the full view.
20668
20669 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20670
20671 -- And to the underlying full view, if any
20672
20673 if Is_Private_Type (Full_T)
20674 and then Present (Underlying_Full_View (Full_T))
20675 then
20676 Propagate_DIC_Attributes
20677 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
20678 end if;
20679
20680 -- Propagate invariant-related attributes from the partial view to the
20681 -- full view.
20682
20683 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20684
20685 -- And to the underlying full view, if any
20686
20687 if Is_Private_Type (Full_T)
20688 and then Present (Underlying_Full_View (Full_T))
20689 then
20690 Propagate_Invariant_Attributes
20691 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
20692 end if;
20693
20694 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20695 -- in the full view without advertising the inheritance in the partial
20696 -- view. This can only occur when the partial view has no parent type
20697 -- and the full view has an interface as a parent. Any other scenarios
20698 -- are illegal because implemented interfaces must match between the
20699 -- two views.
20700
20701 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20702 declare
20703 Full_Par : constant Entity_Id := Etype (Full_T);
20704 Priv_Par : constant Entity_Id := Etype (Priv_T);
20705
20706 begin
20707 if not Is_Interface (Priv_Par)
20708 and then Is_Interface (Full_Par)
20709 and then Has_Inheritable_Invariants (Full_Par)
20710 then
20711 Error_Msg_N
20712 ("hidden inheritance of class-wide type invariants not "
20713 & "allowed", N);
20714 end if;
20715 end;
20716 end if;
20717
20718 -- Propagate predicates to full type, and predicate function if already
20719 -- defined. It is not clear that this can actually happen? the partial
20720 -- view cannot be frozen yet, and the predicate function has not been
20721 -- built. Still it is a cheap check and seems safer to make it.
20722
20723 Propagate_Predicate_Attributes (Full_T, Priv_T);
20724
20725 if Is_Private_Type (Full_T)
20726 and then Present (Underlying_Full_View (Full_T))
20727 then
20728 Propagate_Predicate_Attributes
20729 (Underlying_Full_View (Full_T), Priv_T);
20730 end if;
20731
20732 <<Leave>>
20733 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20734 end Process_Full_View;
20735
20736 -----------------------------------
20737 -- Process_Incomplete_Dependents --
20738 -----------------------------------
20739
20740 procedure Process_Incomplete_Dependents
20741 (N : Node_Id;
20742 Full_T : Entity_Id;
20743 Inc_T : Entity_Id)
20744 is
20745 Inc_Elmt : Elmt_Id;
20746 Priv_Dep : Entity_Id;
20747 New_Subt : Entity_Id;
20748
20749 Disc_Constraint : Elist_Id;
20750
20751 begin
20752 if No (Private_Dependents (Inc_T)) then
20753 return;
20754 end if;
20755
20756 -- Itypes that may be generated by the completion of an incomplete
20757 -- subtype are not used by the back-end and not attached to the tree.
20758 -- They are created only for constraint-checking purposes.
20759
20760 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20761 while Present (Inc_Elmt) loop
20762 Priv_Dep := Node (Inc_Elmt);
20763
20764 if Ekind (Priv_Dep) = E_Subprogram_Type then
20765
20766 -- An Access_To_Subprogram type may have a return type or a
20767 -- parameter type that is incomplete. Replace with the full view.
20768
20769 if Etype (Priv_Dep) = Inc_T then
20770 Set_Etype (Priv_Dep, Full_T);
20771 end if;
20772
20773 declare
20774 Formal : Entity_Id;
20775
20776 begin
20777 Formal := First_Formal (Priv_Dep);
20778 while Present (Formal) loop
20779 if Etype (Formal) = Inc_T then
20780 Set_Etype (Formal, Full_T);
20781 end if;
20782
20783 Next_Formal (Formal);
20784 end loop;
20785 end;
20786
20787 elsif Is_Overloadable (Priv_Dep) then
20788
20789 -- If a subprogram in the incomplete dependents list is primitive
20790 -- for a tagged full type then mark it as a dispatching operation,
20791 -- check whether it overrides an inherited subprogram, and check
20792 -- restrictions on its controlling formals. Note that a protected
20793 -- operation is never dispatching: only its wrapper operation
20794 -- (which has convention Ada) is.
20795
20796 if Is_Tagged_Type (Full_T)
20797 and then Is_Primitive (Priv_Dep)
20798 and then Convention (Priv_Dep) /= Convention_Protected
20799 then
20800 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20801 Set_Is_Dispatching_Operation (Priv_Dep);
20802 Check_Controlling_Formals (Full_T, Priv_Dep);
20803 end if;
20804
20805 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20806
20807 -- Can happen during processing of a body before the completion
20808 -- of a TA type. Ignore, because spec is also on dependent list.
20809
20810 return;
20811
20812 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20813 -- corresponding subtype of the full view.
20814
20815 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
20816 and then Comes_From_Source (Priv_Dep)
20817 then
20818 Set_Subtype_Indication
20819 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20820 Set_Etype (Priv_Dep, Full_T);
20821 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20822 Set_Analyzed (Parent (Priv_Dep), False);
20823
20824 -- Reanalyze the declaration, suppressing the call to Enter_Name
20825 -- to avoid duplicate names.
20826
20827 Analyze_Subtype_Declaration
20828 (N => Parent (Priv_Dep),
20829 Skip => True);
20830
20831 -- Dependent is a subtype
20832
20833 else
20834 -- We build a new subtype indication using the full view of the
20835 -- incomplete parent. The discriminant constraints have been
20836 -- elaborated already at the point of the subtype declaration.
20837
20838 New_Subt := Create_Itype (E_Void, N);
20839
20840 if Has_Discriminants (Full_T) then
20841 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20842 else
20843 Disc_Constraint := No_Elist;
20844 end if;
20845
20846 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20847 Set_Full_View (Priv_Dep, New_Subt);
20848 end if;
20849
20850 Next_Elmt (Inc_Elmt);
20851 end loop;
20852 end Process_Incomplete_Dependents;
20853
20854 --------------------------------
20855 -- Process_Range_Expr_In_Decl --
20856 --------------------------------
20857
20858 procedure Process_Range_Expr_In_Decl
20859 (R : Node_Id;
20860 T : Entity_Id;
20861 Subtyp : Entity_Id := Empty;
20862 Check_List : List_Id := No_List;
20863 R_Check_Off : Boolean := False)
20864 is
20865 Lo, Hi : Node_Id;
20866 R_Checks : Check_Result;
20867 Insert_Node : Node_Id;
20868 Def_Id : Entity_Id;
20869
20870 begin
20871 Analyze_And_Resolve (R, Base_Type (T));
20872
20873 if Nkind (R) = N_Range then
20874 Lo := Low_Bound (R);
20875 Hi := High_Bound (R);
20876
20877 -- Validity checks on the range of a quantified expression are
20878 -- delayed until the construct is transformed into a loop.
20879
20880 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20881 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20882 then
20883 null;
20884
20885 -- We need to ensure validity of the bounds here, because if we
20886 -- go ahead and do the expansion, then the expanded code will get
20887 -- analyzed with range checks suppressed and we miss the check.
20888
20889 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20890 -- the temporaries generated by routine Remove_Side_Effects by means
20891 -- of validity checks must use the same names. When a range appears
20892 -- in the parent of a generic, the range is processed with checks
20893 -- disabled as part of the generic context and with checks enabled
20894 -- for code generation purposes. This leads to link issues as the
20895 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20896 -- template sees the temporaries generated by Remove_Side_Effects.
20897
20898 else
20899 Validity_Check_Range (R, Subtyp);
20900 end if;
20901
20902 -- If there were errors in the declaration, try and patch up some
20903 -- common mistakes in the bounds. The cases handled are literals
20904 -- which are Integer where the expected type is Real and vice versa.
20905 -- These corrections allow the compilation process to proceed further
20906 -- along since some basic assumptions of the format of the bounds
20907 -- are guaranteed.
20908
20909 if Etype (R) = Any_Type then
20910 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20911 Rewrite (Lo,
20912 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20913
20914 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20915 Rewrite (Hi,
20916 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20917
20918 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20919 Rewrite (Lo,
20920 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20921
20922 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20923 Rewrite (Hi,
20924 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20925 end if;
20926
20927 Set_Etype (Lo, T);
20928 Set_Etype (Hi, T);
20929 end if;
20930
20931 -- If the bounds of the range have been mistakenly given as string
20932 -- literals (perhaps in place of character literals), then an error
20933 -- has already been reported, but we rewrite the string literal as a
20934 -- bound of the range's type to avoid blowups in later processing
20935 -- that looks at static values.
20936
20937 if Nkind (Lo) = N_String_Literal then
20938 Rewrite (Lo,
20939 Make_Attribute_Reference (Sloc (Lo),
20940 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20941 Attribute_Name => Name_First));
20942 Analyze_And_Resolve (Lo);
20943 end if;
20944
20945 if Nkind (Hi) = N_String_Literal then
20946 Rewrite (Hi,
20947 Make_Attribute_Reference (Sloc (Hi),
20948 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20949 Attribute_Name => Name_First));
20950 Analyze_And_Resolve (Hi);
20951 end if;
20952
20953 -- If bounds aren't scalar at this point then exit, avoiding
20954 -- problems with further processing of the range in this procedure.
20955
20956 if not Is_Scalar_Type (Etype (Lo)) then
20957 return;
20958 end if;
20959
20960 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20961 -- then range of the base type. Here we check whether the bounds
20962 -- are in the range of the subtype itself. Note that if the bounds
20963 -- represent the null range the Constraint_Error exception should
20964 -- not be raised.
20965
20966 -- ??? The following code should be cleaned up as follows
20967
20968 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20969 -- is done in the call to Range_Check (R, T); below
20970
20971 -- 2. The use of R_Check_Off should be investigated and possibly
20972 -- removed, this would clean up things a bit.
20973
20974 if Is_Null_Range (Lo, Hi) then
20975 null;
20976
20977 else
20978 -- Capture values of bounds and generate temporaries for them
20979 -- if needed, before applying checks, since checks may cause
20980 -- duplication of the expression without forcing evaluation.
20981
20982 -- The forced evaluation removes side effects from expressions,
20983 -- which should occur also in GNATprove mode. Otherwise, we end up
20984 -- with unexpected insertions of actions at places where this is
20985 -- not supposed to occur, e.g. on default parameters of a call.
20986
20987 if Expander_Active or GNATprove_Mode then
20988
20989 -- Call Force_Evaluation to create declarations as needed to
20990 -- deal with side effects, and also create typ_FIRST/LAST
20991 -- entities for bounds if we have a subtype name.
20992
20993 -- Note: we do this transformation even if expansion is not
20994 -- active if we are in GNATprove_Mode since the transformation
20995 -- is in general required to ensure that the resulting tree has
20996 -- proper Ada semantics.
20997
20998 Force_Evaluation
20999 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21000 Force_Evaluation
21001 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21002 end if;
21003
21004 -- We use a flag here instead of suppressing checks on the type
21005 -- because the type we check against isn't necessarily the place
21006 -- where we put the check.
21007
21008 if not R_Check_Off then
21009 R_Checks := Get_Range_Checks (R, T);
21010
21011 -- Look up tree to find an appropriate insertion point. We
21012 -- can't just use insert_actions because later processing
21013 -- depends on the insertion node. Prior to Ada 2012 the
21014 -- insertion point could only be a declaration or a loop, but
21015 -- quantified expressions can appear within any context in an
21016 -- expression, and the insertion point can be any statement,
21017 -- pragma, or declaration.
21018
21019 Insert_Node := Parent (R);
21020 while Present (Insert_Node) loop
21021 exit when
21022 Nkind (Insert_Node) in N_Declaration
21023 and then
21024 not Nkind_In
21025 (Insert_Node, N_Component_Declaration,
21026 N_Loop_Parameter_Specification,
21027 N_Function_Specification,
21028 N_Procedure_Specification);
21029
21030 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21031 or else Nkind (Insert_Node) in
21032 N_Statement_Other_Than_Procedure_Call
21033 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21034 N_Pragma);
21035
21036 Insert_Node := Parent (Insert_Node);
21037 end loop;
21038
21039 -- Why would Type_Decl not be present??? Without this test,
21040 -- short regression tests fail.
21041
21042 if Present (Insert_Node) then
21043
21044 -- Case of loop statement. Verify that the range is part
21045 -- of the subtype indication of the iteration scheme.
21046
21047 if Nkind (Insert_Node) = N_Loop_Statement then
21048 declare
21049 Indic : Node_Id;
21050
21051 begin
21052 Indic := Parent (R);
21053 while Present (Indic)
21054 and then Nkind (Indic) /= N_Subtype_Indication
21055 loop
21056 Indic := Parent (Indic);
21057 end loop;
21058
21059 if Present (Indic) then
21060 Def_Id := Etype (Subtype_Mark (Indic));
21061
21062 Insert_Range_Checks
21063 (R_Checks,
21064 Insert_Node,
21065 Def_Id,
21066 Sloc (Insert_Node),
21067 Do_Before => True);
21068 end if;
21069 end;
21070
21071 -- Case of declarations. If the declaration is for a type
21072 -- and involves discriminants, the checks are premature at
21073 -- the declaration point and need to wait for the expansion
21074 -- of the initialization procedure, which will pass in the
21075 -- list to put them on; otherwise, the checks are done at
21076 -- the declaration point and there is no need to do them
21077 -- again in the initialization procedure.
21078
21079 elsif Nkind (Insert_Node) in N_Declaration then
21080 Def_Id := Defining_Identifier (Insert_Node);
21081
21082 if (Ekind (Def_Id) = E_Record_Type
21083 and then Depends_On_Discriminant (R))
21084 or else
21085 (Ekind (Def_Id) = E_Protected_Type
21086 and then Has_Discriminants (Def_Id))
21087 then
21088 if Present (Check_List) then
21089 Append_Range_Checks
21090 (R_Checks,
21091 Check_List, Def_Id, Sloc (Insert_Node));
21092 end if;
21093
21094 else
21095 if No (Check_List) then
21096 Insert_Range_Checks
21097 (R_Checks,
21098 Insert_Node, Def_Id, Sloc (Insert_Node));
21099 end if;
21100 end if;
21101
21102 -- Case of statements. Drop the checks, as the range appears
21103 -- in the context of a quantified expression. Insertion will
21104 -- take place when expression is expanded.
21105
21106 else
21107 null;
21108 end if;
21109 end if;
21110 end if;
21111 end if;
21112
21113 -- Case of other than an explicit N_Range node
21114
21115 -- The forced evaluation removes side effects from expressions, which
21116 -- should occur also in GNATprove mode. Otherwise, we end up with
21117 -- unexpected insertions of actions at places where this is not
21118 -- supposed to occur, e.g. on default parameters of a call.
21119
21120 elsif Expander_Active or GNATprove_Mode then
21121 Get_Index_Bounds (R, Lo, Hi);
21122 Force_Evaluation (Lo);
21123 Force_Evaluation (Hi);
21124 end if;
21125 end Process_Range_Expr_In_Decl;
21126
21127 --------------------------------------
21128 -- Process_Real_Range_Specification --
21129 --------------------------------------
21130
21131 procedure Process_Real_Range_Specification (Def : Node_Id) is
21132 Spec : constant Node_Id := Real_Range_Specification (Def);
21133 Lo : Node_Id;
21134 Hi : Node_Id;
21135 Err : Boolean := False;
21136
21137 procedure Analyze_Bound (N : Node_Id);
21138 -- Analyze and check one bound
21139
21140 -------------------
21141 -- Analyze_Bound --
21142 -------------------
21143
21144 procedure Analyze_Bound (N : Node_Id) is
21145 begin
21146 Analyze_And_Resolve (N, Any_Real);
21147
21148 if not Is_OK_Static_Expression (N) then
21149 Flag_Non_Static_Expr
21150 ("bound in real type definition is not static!", N);
21151 Err := True;
21152 end if;
21153 end Analyze_Bound;
21154
21155 -- Start of processing for Process_Real_Range_Specification
21156
21157 begin
21158 if Present (Spec) then
21159 Lo := Low_Bound (Spec);
21160 Hi := High_Bound (Spec);
21161 Analyze_Bound (Lo);
21162 Analyze_Bound (Hi);
21163
21164 -- If error, clear away junk range specification
21165
21166 if Err then
21167 Set_Real_Range_Specification (Def, Empty);
21168 end if;
21169 end if;
21170 end Process_Real_Range_Specification;
21171
21172 ---------------------
21173 -- Process_Subtype --
21174 ---------------------
21175
21176 function Process_Subtype
21177 (S : Node_Id;
21178 Related_Nod : Node_Id;
21179 Related_Id : Entity_Id := Empty;
21180 Suffix : Character := ' ') return Entity_Id
21181 is
21182 P : Node_Id;
21183 Def_Id : Entity_Id;
21184 Error_Node : Node_Id;
21185 Full_View_Id : Entity_Id;
21186 Subtype_Mark_Id : Entity_Id;
21187
21188 May_Have_Null_Exclusion : Boolean;
21189
21190 procedure Check_Incomplete (T : Node_Id);
21191 -- Called to verify that an incomplete type is not used prematurely
21192
21193 ----------------------
21194 -- Check_Incomplete --
21195 ----------------------
21196
21197 procedure Check_Incomplete (T : Node_Id) is
21198 begin
21199 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21200
21201 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21202 and then
21203 not (Ada_Version >= Ada_2005
21204 and then
21205 (Nkind (Parent (T)) = N_Subtype_Declaration
21206 or else (Nkind (Parent (T)) = N_Subtype_Indication
21207 and then Nkind (Parent (Parent (T))) =
21208 N_Subtype_Declaration)))
21209 then
21210 Error_Msg_N ("invalid use of type before its full declaration", T);
21211 end if;
21212 end Check_Incomplete;
21213
21214 -- Start of processing for Process_Subtype
21215
21216 begin
21217 -- Case of no constraints present
21218
21219 if Nkind (S) /= N_Subtype_Indication then
21220 Find_Type (S);
21221
21222 -- No way to proceed if the subtype indication is malformed. This
21223 -- will happen for example when the subtype indication in an object
21224 -- declaration is missing altogether and the expression is analyzed
21225 -- as if it were that indication.
21226
21227 if not Is_Entity_Name (S) then
21228 return Any_Type;
21229 end if;
21230
21231 Check_Incomplete (S);
21232 P := Parent (S);
21233
21234 -- Ada 2005 (AI-231): Static check
21235
21236 if Ada_Version >= Ada_2005
21237 and then Present (P)
21238 and then Null_Exclusion_Present (P)
21239 and then Nkind (P) /= N_Access_To_Object_Definition
21240 and then not Is_Access_Type (Entity (S))
21241 then
21242 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21243 end if;
21244
21245 -- The following is ugly, can't we have a range or even a flag???
21246
21247 May_Have_Null_Exclusion :=
21248 Nkind_In (P, N_Access_Definition,
21249 N_Access_Function_Definition,
21250 N_Access_Procedure_Definition,
21251 N_Access_To_Object_Definition,
21252 N_Allocator,
21253 N_Component_Definition)
21254 or else
21255 Nkind_In (P, N_Derived_Type_Definition,
21256 N_Discriminant_Specification,
21257 N_Formal_Object_Declaration,
21258 N_Object_Declaration,
21259 N_Object_Renaming_Declaration,
21260 N_Parameter_Specification,
21261 N_Subtype_Declaration);
21262
21263 -- Create an Itype that is a duplicate of Entity (S) but with the
21264 -- null-exclusion attribute.
21265
21266 if May_Have_Null_Exclusion
21267 and then Is_Access_Type (Entity (S))
21268 and then Null_Exclusion_Present (P)
21269
21270 -- No need to check the case of an access to object definition.
21271 -- It is correct to define double not-null pointers.
21272
21273 -- Example:
21274 -- type Not_Null_Int_Ptr is not null access Integer;
21275 -- type Acc is not null access Not_Null_Int_Ptr;
21276
21277 and then Nkind (P) /= N_Access_To_Object_Definition
21278 then
21279 if Can_Never_Be_Null (Entity (S)) then
21280 case Nkind (Related_Nod) is
21281 when N_Full_Type_Declaration =>
21282 if Nkind (Type_Definition (Related_Nod))
21283 in N_Array_Type_Definition
21284 then
21285 Error_Node :=
21286 Subtype_Indication
21287 (Component_Definition
21288 (Type_Definition (Related_Nod)));
21289 else
21290 Error_Node :=
21291 Subtype_Indication (Type_Definition (Related_Nod));
21292 end if;
21293
21294 when N_Subtype_Declaration =>
21295 Error_Node := Subtype_Indication (Related_Nod);
21296
21297 when N_Object_Declaration =>
21298 Error_Node := Object_Definition (Related_Nod);
21299
21300 when N_Component_Declaration =>
21301 Error_Node :=
21302 Subtype_Indication (Component_Definition (Related_Nod));
21303
21304 when N_Allocator =>
21305 Error_Node := Expression (Related_Nod);
21306
21307 when others =>
21308 pragma Assert (False);
21309 Error_Node := Related_Nod;
21310 end case;
21311
21312 Error_Msg_NE
21313 ("`NOT NULL` not allowed (& already excludes null)",
21314 Error_Node,
21315 Entity (S));
21316 end if;
21317
21318 Set_Etype (S,
21319 Create_Null_Excluding_Itype
21320 (T => Entity (S),
21321 Related_Nod => P));
21322 Set_Entity (S, Etype (S));
21323 end if;
21324
21325 return Entity (S);
21326
21327 -- Case of constraint present, so that we have an N_Subtype_Indication
21328 -- node (this node is created only if constraints are present).
21329
21330 else
21331 Find_Type (Subtype_Mark (S));
21332
21333 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21334 and then not
21335 (Nkind (Parent (S)) = N_Subtype_Declaration
21336 and then Is_Itype (Defining_Identifier (Parent (S))))
21337 then
21338 Check_Incomplete (Subtype_Mark (S));
21339 end if;
21340
21341 P := Parent (S);
21342 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21343
21344 -- Explicit subtype declaration case
21345
21346 if Nkind (P) = N_Subtype_Declaration then
21347 Def_Id := Defining_Identifier (P);
21348
21349 -- Explicit derived type definition case
21350
21351 elsif Nkind (P) = N_Derived_Type_Definition then
21352 Def_Id := Defining_Identifier (Parent (P));
21353
21354 -- Implicit case, the Def_Id must be created as an implicit type.
21355 -- The one exception arises in the case of concurrent types, array
21356 -- and access types, where other subsidiary implicit types may be
21357 -- created and must appear before the main implicit type. In these
21358 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21359 -- has not yet been called to create Def_Id.
21360
21361 else
21362 if Is_Array_Type (Subtype_Mark_Id)
21363 or else Is_Concurrent_Type (Subtype_Mark_Id)
21364 or else Is_Access_Type (Subtype_Mark_Id)
21365 then
21366 Def_Id := Empty;
21367
21368 -- For the other cases, we create a new unattached Itype,
21369 -- and set the indication to ensure it gets attached later.
21370
21371 else
21372 Def_Id :=
21373 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21374 end if;
21375 end if;
21376
21377 -- If the kind of constraint is invalid for this kind of type,
21378 -- then give an error, and then pretend no constraint was given.
21379
21380 if not Is_Valid_Constraint_Kind
21381 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21382 then
21383 Error_Msg_N
21384 ("incorrect constraint for this kind of type", Constraint (S));
21385
21386 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21387
21388 -- Set Ekind of orphan itype, to prevent cascaded errors
21389
21390 if Present (Def_Id) then
21391 Set_Ekind (Def_Id, Ekind (Any_Type));
21392 end if;
21393
21394 -- Make recursive call, having got rid of the bogus constraint
21395
21396 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21397 end if;
21398
21399 -- Remaining processing depends on type. Select on Base_Type kind to
21400 -- ensure getting to the concrete type kind in the case of a private
21401 -- subtype (needed when only doing semantic analysis).
21402
21403 case Ekind (Base_Type (Subtype_Mark_Id)) is
21404 when Access_Kind =>
21405
21406 -- If this is a constraint on a class-wide type, discard it.
21407 -- There is currently no way to express a partial discriminant
21408 -- constraint on a type with unknown discriminants. This is
21409 -- a pathology that the ACATS wisely decides not to test.
21410
21411 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21412 if Comes_From_Source (S) then
21413 Error_Msg_N
21414 ("constraint on class-wide type ignored??",
21415 Constraint (S));
21416 end if;
21417
21418 if Nkind (P) = N_Subtype_Declaration then
21419 Set_Subtype_Indication (P,
21420 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21421 end if;
21422
21423 return Subtype_Mark_Id;
21424 end if;
21425
21426 Constrain_Access (Def_Id, S, Related_Nod);
21427
21428 if Expander_Active
21429 and then Is_Itype (Designated_Type (Def_Id))
21430 and then Nkind (Related_Nod) = N_Subtype_Declaration
21431 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21432 then
21433 Build_Itype_Reference
21434 (Designated_Type (Def_Id), Related_Nod);
21435 end if;
21436
21437 when Array_Kind =>
21438 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21439
21440 when Decimal_Fixed_Point_Kind =>
21441 Constrain_Decimal (Def_Id, S);
21442
21443 when Enumeration_Kind =>
21444 Constrain_Enumeration (Def_Id, S);
21445
21446 when Ordinary_Fixed_Point_Kind =>
21447 Constrain_Ordinary_Fixed (Def_Id, S);
21448
21449 when Float_Kind =>
21450 Constrain_Float (Def_Id, S);
21451
21452 when Integer_Kind =>
21453 Constrain_Integer (Def_Id, S);
21454
21455 when Class_Wide_Kind
21456 | E_Incomplete_Type
21457 | E_Record_Subtype
21458 | E_Record_Type
21459 =>
21460 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21461
21462 if Ekind (Def_Id) = E_Incomplete_Type then
21463 Set_Private_Dependents (Def_Id, New_Elmt_List);
21464 end if;
21465
21466 when Private_Kind =>
21467
21468 -- A private type with unknown discriminants may be completed
21469 -- by an unconstrained array type.
21470
21471 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21472 and then Present (Full_View (Subtype_Mark_Id))
21473 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21474 then
21475 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21476
21477 -- ... but more commonly is completed by a discriminated record
21478 -- type.
21479
21480 else
21481 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21482 end if;
21483
21484 -- The base type may be private but Def_Id may be a full view
21485 -- in an instance.
21486
21487 if Is_Private_Type (Def_Id) then
21488 Set_Private_Dependents (Def_Id, New_Elmt_List);
21489 end if;
21490
21491 -- In case of an invalid constraint prevent further processing
21492 -- since the type constructed is missing expected fields.
21493
21494 if Etype (Def_Id) = Any_Type then
21495 return Def_Id;
21496 end if;
21497
21498 -- If the full view is that of a task with discriminants,
21499 -- we must constrain both the concurrent type and its
21500 -- corresponding record type. Otherwise we will just propagate
21501 -- the constraint to the full view, if available.
21502
21503 if Present (Full_View (Subtype_Mark_Id))
21504 and then Has_Discriminants (Subtype_Mark_Id)
21505 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21506 then
21507 Full_View_Id :=
21508 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21509
21510 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21511 Constrain_Concurrent (Full_View_Id, S,
21512 Related_Nod, Related_Id, Suffix);
21513 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21514 Set_Full_View (Def_Id, Full_View_Id);
21515
21516 -- Introduce an explicit reference to the private subtype,
21517 -- to prevent scope anomalies in gigi if first use appears
21518 -- in a nested context, e.g. a later function body.
21519 -- Should this be generated in other contexts than a full
21520 -- type declaration?
21521
21522 if Is_Itype (Def_Id)
21523 and then
21524 Nkind (Parent (P)) = N_Full_Type_Declaration
21525 then
21526 Build_Itype_Reference (Def_Id, Parent (P));
21527 end if;
21528
21529 else
21530 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21531 end if;
21532
21533 when Concurrent_Kind =>
21534 Constrain_Concurrent (Def_Id, S,
21535 Related_Nod, Related_Id, Suffix);
21536
21537 when others =>
21538 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21539 end case;
21540
21541 -- Size, Alignment, Representation aspects and Convention are always
21542 -- inherited from the base type.
21543
21544 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21545 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21546 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21547
21548 -- The anonymous subtype created for the subtype indication
21549 -- inherits the predicates of the parent.
21550
21551 if Has_Predicates (Subtype_Mark_Id) then
21552 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21553
21554 -- Indicate where the predicate function may be found
21555
21556 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21557 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21558 end if;
21559 end if;
21560
21561 return Def_Id;
21562 end if;
21563 end Process_Subtype;
21564
21565 -----------------------------
21566 -- Record_Type_Declaration --
21567 -----------------------------
21568
21569 procedure Record_Type_Declaration
21570 (T : Entity_Id;
21571 N : Node_Id;
21572 Prev : Entity_Id)
21573 is
21574 Def : constant Node_Id := Type_Definition (N);
21575 Is_Tagged : Boolean;
21576 Tag_Comp : Entity_Id;
21577
21578 begin
21579 -- These flags must be initialized before calling Process_Discriminants
21580 -- because this routine makes use of them.
21581
21582 Set_Ekind (T, E_Record_Type);
21583 Set_Etype (T, T);
21584 Init_Size_Align (T);
21585 Set_Interfaces (T, No_Elist);
21586 Set_Stored_Constraint (T, No_Elist);
21587 Set_Default_SSO (T);
21588 Set_No_Reordering (T, No_Component_Reordering);
21589
21590 -- Normal case
21591
21592 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21593 -- The flag Is_Tagged_Type might have already been set by
21594 -- Find_Type_Name if it detected an error for declaration T. This
21595 -- arises in the case of private tagged types where the full view
21596 -- omits the word tagged.
21597
21598 Is_Tagged :=
21599 Tagged_Present (Def)
21600 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21601
21602 Set_Is_Limited_Record (T, Limited_Present (Def));
21603
21604 if Is_Tagged then
21605 Set_Is_Tagged_Type (T, True);
21606 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21607 end if;
21608
21609 -- Type is abstract if full declaration carries keyword, or if
21610 -- previous partial view did.
21611
21612 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21613 or else Abstract_Present (Def));
21614
21615 else
21616 Is_Tagged := True;
21617 Analyze_Interface_Declaration (T, Def);
21618
21619 if Present (Discriminant_Specifications (N)) then
21620 Error_Msg_N
21621 ("interface types cannot have discriminants",
21622 Defining_Identifier
21623 (First (Discriminant_Specifications (N))));
21624 end if;
21625 end if;
21626
21627 -- First pass: if there are self-referential access components,
21628 -- create the required anonymous access type declarations, and if
21629 -- need be an incomplete type declaration for T itself.
21630
21631 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21632
21633 if Ada_Version >= Ada_2005
21634 and then Present (Interface_List (Def))
21635 then
21636 Check_Interfaces (N, Def);
21637
21638 declare
21639 Ifaces_List : Elist_Id;
21640
21641 begin
21642 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21643 -- already in the parents.
21644
21645 Collect_Interfaces
21646 (T => T,
21647 Ifaces_List => Ifaces_List,
21648 Exclude_Parents => True);
21649
21650 Set_Interfaces (T, Ifaces_List);
21651 end;
21652 end if;
21653
21654 -- Records constitute a scope for the component declarations within.
21655 -- The scope is created prior to the processing of these declarations.
21656 -- Discriminants are processed first, so that they are visible when
21657 -- processing the other components. The Ekind of the record type itself
21658 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21659
21660 -- Enter record scope
21661
21662 Push_Scope (T);
21663
21664 -- If an incomplete or private type declaration was already given for
21665 -- the type, then this scope already exists, and the discriminants have
21666 -- been declared within. We must verify that the full declaration
21667 -- matches the incomplete one.
21668
21669 Check_Or_Process_Discriminants (N, T, Prev);
21670
21671 Set_Is_Constrained (T, not Has_Discriminants (T));
21672 Set_Has_Delayed_Freeze (T, True);
21673
21674 -- For tagged types add a manually analyzed component corresponding
21675 -- to the component _tag, the corresponding piece of tree will be
21676 -- expanded as part of the freezing actions if it is not a CPP_Class.
21677
21678 if Is_Tagged then
21679
21680 -- Do not add the tag unless we are in expansion mode
21681
21682 if Expander_Active then
21683 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21684 Enter_Name (Tag_Comp);
21685
21686 Set_Ekind (Tag_Comp, E_Component);
21687 Set_Is_Tag (Tag_Comp);
21688 Set_Is_Aliased (Tag_Comp);
21689 Set_Is_Independent (Tag_Comp);
21690 Set_Etype (Tag_Comp, RTE (RE_Tag));
21691 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21692 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21693 Init_Component_Location (Tag_Comp);
21694
21695 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21696 -- implemented interfaces.
21697
21698 if Has_Interfaces (T) then
21699 Add_Interface_Tag_Components (N, T);
21700 end if;
21701 end if;
21702
21703 Make_Class_Wide_Type (T);
21704 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21705 end if;
21706
21707 -- We must suppress range checks when processing record components in
21708 -- the presence of discriminants, since we don't want spurious checks to
21709 -- be generated during their analysis, but Suppress_Range_Checks flags
21710 -- must be reset the after processing the record definition.
21711
21712 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21713 -- couldn't we just use the normal range check suppression method here.
21714 -- That would seem cleaner ???
21715
21716 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21717 Set_Kill_Range_Checks (T, True);
21718 Record_Type_Definition (Def, Prev);
21719 Set_Kill_Range_Checks (T, False);
21720 else
21721 Record_Type_Definition (Def, Prev);
21722 end if;
21723
21724 -- Exit from record scope
21725
21726 End_Scope;
21727
21728 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21729 -- the implemented interfaces and associate them an aliased entity.
21730
21731 if Is_Tagged
21732 and then not Is_Empty_List (Interface_List (Def))
21733 then
21734 Derive_Progenitor_Subprograms (T, T);
21735 end if;
21736
21737 Check_Function_Writable_Actuals (N);
21738 end Record_Type_Declaration;
21739
21740 ----------------------------
21741 -- Record_Type_Definition --
21742 ----------------------------
21743
21744 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21745 Component : Entity_Id;
21746 Ctrl_Components : Boolean := False;
21747 Final_Storage_Only : Boolean;
21748 T : Entity_Id;
21749
21750 begin
21751 if Ekind (Prev_T) = E_Incomplete_Type then
21752 T := Full_View (Prev_T);
21753 else
21754 T := Prev_T;
21755 end if;
21756
21757 Final_Storage_Only := not Is_Controlled (T);
21758
21759 -- Ada 2005: Check whether an explicit Limited is present in a derived
21760 -- type declaration.
21761
21762 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21763 and then Limited_Present (Parent (Def))
21764 then
21765 Set_Is_Limited_Record (T);
21766 end if;
21767
21768 -- If the component list of a record type is defined by the reserved
21769 -- word null and there is no discriminant part, then the record type has
21770 -- no components and all records of the type are null records (RM 3.7)
21771 -- This procedure is also called to process the extension part of a
21772 -- record extension, in which case the current scope may have inherited
21773 -- components.
21774
21775 if Present (Def)
21776 and then Present (Component_List (Def))
21777 and then not Null_Present (Component_List (Def))
21778 then
21779 Analyze_Declarations (Component_Items (Component_List (Def)));
21780
21781 if Present (Variant_Part (Component_List (Def))) then
21782 Analyze (Variant_Part (Component_List (Def)));
21783 end if;
21784 end if;
21785
21786 -- After completing the semantic analysis of the record definition,
21787 -- record components, both new and inherited, are accessible. Set their
21788 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21789 -- whose Ekind may be void.
21790
21791 Component := First_Entity (Current_Scope);
21792 while Present (Component) loop
21793 if Ekind (Component) = E_Void
21794 and then not Is_Itype (Component)
21795 then
21796 Set_Ekind (Component, E_Component);
21797 Init_Component_Location (Component);
21798 end if;
21799
21800 Propagate_Concurrent_Flags (T, Etype (Component));
21801
21802 if Ekind (Component) /= E_Component then
21803 null;
21804
21805 -- Do not set Has_Controlled_Component on a class-wide equivalent
21806 -- type. See Make_CW_Equivalent_Type.
21807
21808 elsif not Is_Class_Wide_Equivalent_Type (T)
21809 and then (Has_Controlled_Component (Etype (Component))
21810 or else (Chars (Component) /= Name_uParent
21811 and then Is_Controlled (Etype (Component))))
21812 then
21813 Set_Has_Controlled_Component (T, True);
21814 Final_Storage_Only :=
21815 Final_Storage_Only
21816 and then Finalize_Storage_Only (Etype (Component));
21817 Ctrl_Components := True;
21818 end if;
21819
21820 Next_Entity (Component);
21821 end loop;
21822
21823 -- A Type is Finalize_Storage_Only only if all its controlled components
21824 -- are also.
21825
21826 if Ctrl_Components then
21827 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21828 end if;
21829
21830 -- Place reference to end record on the proper entity, which may
21831 -- be a partial view.
21832
21833 if Present (Def) then
21834 Process_End_Label (Def, 'e', Prev_T);
21835 end if;
21836 end Record_Type_Definition;
21837
21838 ---------------------------
21839 -- Replace_Discriminants --
21840 ---------------------------
21841
21842 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
21843 function Process (N : Node_Id) return Traverse_Result;
21844
21845 -------------
21846 -- Process --
21847 -------------
21848
21849 function Process (N : Node_Id) return Traverse_Result is
21850 Comp : Entity_Id;
21851
21852 begin
21853 if Nkind (N) = N_Discriminant_Specification then
21854 Comp := First_Discriminant (Typ);
21855 while Present (Comp) loop
21856 if Original_Record_Component (Comp) = Defining_Identifier (N)
21857 or else Chars (Comp) = Chars (Defining_Identifier (N))
21858 then
21859 Set_Defining_Identifier (N, Comp);
21860 exit;
21861 end if;
21862
21863 Next_Discriminant (Comp);
21864 end loop;
21865
21866 elsif Nkind (N) = N_Variant_Part then
21867 Comp := First_Discriminant (Typ);
21868 while Present (Comp) loop
21869 if Original_Record_Component (Comp) = Entity (Name (N))
21870 or else Chars (Comp) = Chars (Name (N))
21871 then
21872 -- Make sure to preserve the type coming from the parent on
21873 -- the Name, even if the subtype of the discriminant can be
21874 -- constrained, so that discrete choices inherited from the
21875 -- parent in the variant part are not flagged as violating
21876 -- the constraints of the subtype.
21877
21878 declare
21879 Typ : constant Entity_Id := Etype (Name (N));
21880 begin
21881 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
21882 Set_Etype (Name (N), Typ);
21883 end;
21884 exit;
21885 end if;
21886
21887 Next_Discriminant (Comp);
21888 end loop;
21889 end if;
21890
21891 return OK;
21892 end Process;
21893
21894 procedure Replace is new Traverse_Proc (Process);
21895
21896 -- Start of processing for Replace_Discriminants
21897
21898 begin
21899 Replace (Decl);
21900 end Replace_Discriminants;
21901
21902 -------------------------------
21903 -- Set_Completion_Referenced --
21904 -------------------------------
21905
21906 procedure Set_Completion_Referenced (E : Entity_Id) is
21907 begin
21908 -- If in main unit, mark entity that is a completion as referenced,
21909 -- warnings go on the partial view when needed.
21910
21911 if In_Extended_Main_Source_Unit (E) then
21912 Set_Referenced (E);
21913 end if;
21914 end Set_Completion_Referenced;
21915
21916 ---------------------
21917 -- Set_Default_SSO --
21918 ---------------------
21919
21920 procedure Set_Default_SSO (T : Entity_Id) is
21921 begin
21922 case Opt.Default_SSO is
21923 when ' ' =>
21924 null;
21925 when 'L' =>
21926 Set_SSO_Set_Low_By_Default (T, True);
21927 when 'H' =>
21928 Set_SSO_Set_High_By_Default (T, True);
21929 when others =>
21930 raise Program_Error;
21931 end case;
21932 end Set_Default_SSO;
21933
21934 ---------------------
21935 -- Set_Fixed_Range --
21936 ---------------------
21937
21938 -- The range for fixed-point types is complicated by the fact that we
21939 -- do not know the exact end points at the time of the declaration. This
21940 -- is true for three reasons:
21941
21942 -- A size clause may affect the fudging of the end-points.
21943 -- A small clause may affect the values of the end-points.
21944 -- We try to include the end-points if it does not affect the size.
21945
21946 -- This means that the actual end-points must be established at the
21947 -- point when the type is frozen. Meanwhile, we first narrow the range
21948 -- as permitted (so that it will fit if necessary in a small specified
21949 -- size), and then build a range subtree with these narrowed bounds.
21950 -- Set_Fixed_Range constructs the range from real literal values, and
21951 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21952
21953 -- The parent of this range is set to point to the entity so that it is
21954 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21955 -- other scalar types, which are just pointers to the range in the
21956 -- original tree, this would otherwise be an orphan).
21957
21958 -- The tree is left unanalyzed. When the type is frozen, the processing
21959 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21960 -- analyzed, and uses this as an indication that it should complete
21961 -- work on the range (it will know the final small and size values).
21962
21963 procedure Set_Fixed_Range
21964 (E : Entity_Id;
21965 Loc : Source_Ptr;
21966 Lo : Ureal;
21967 Hi : Ureal)
21968 is
21969 S : constant Node_Id :=
21970 Make_Range (Loc,
21971 Low_Bound => Make_Real_Literal (Loc, Lo),
21972 High_Bound => Make_Real_Literal (Loc, Hi));
21973 begin
21974 Set_Scalar_Range (E, S);
21975 Set_Parent (S, E);
21976
21977 -- Before the freeze point, the bounds of a fixed point are universal
21978 -- and carry the corresponding type.
21979
21980 Set_Etype (Low_Bound (S), Universal_Real);
21981 Set_Etype (High_Bound (S), Universal_Real);
21982 end Set_Fixed_Range;
21983
21984 ----------------------------------
21985 -- Set_Scalar_Range_For_Subtype --
21986 ----------------------------------
21987
21988 procedure Set_Scalar_Range_For_Subtype
21989 (Def_Id : Entity_Id;
21990 R : Node_Id;
21991 Subt : Entity_Id)
21992 is
21993 Kind : constant Entity_Kind := Ekind (Def_Id);
21994
21995 begin
21996 -- Defend against previous error
21997
21998 if Nkind (R) = N_Error then
21999 return;
22000 end if;
22001
22002 Set_Scalar_Range (Def_Id, R);
22003
22004 -- We need to link the range into the tree before resolving it so
22005 -- that types that are referenced, including importantly the subtype
22006 -- itself, are properly frozen (Freeze_Expression requires that the
22007 -- expression be properly linked into the tree). Of course if it is
22008 -- already linked in, then we do not disturb the current link.
22009
22010 if No (Parent (R)) then
22011 Set_Parent (R, Def_Id);
22012 end if;
22013
22014 -- Reset the kind of the subtype during analysis of the range, to
22015 -- catch possible premature use in the bounds themselves.
22016
22017 Set_Ekind (Def_Id, E_Void);
22018 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22019 Set_Ekind (Def_Id, Kind);
22020 end Set_Scalar_Range_For_Subtype;
22021
22022 --------------------------------------------------------
22023 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22024 --------------------------------------------------------
22025
22026 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22027 (E : Entity_Id)
22028 is
22029 begin
22030 -- Make sure set if encountered during Expand_To_Stored_Constraint
22031
22032 Set_Stored_Constraint (E, No_Elist);
22033
22034 -- Give it the right value
22035
22036 if Is_Constrained (E) and then Has_Discriminants (E) then
22037 Set_Stored_Constraint (E,
22038 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22039 end if;
22040 end Set_Stored_Constraint_From_Discriminant_Constraint;
22041
22042 -------------------------------------
22043 -- Signed_Integer_Type_Declaration --
22044 -------------------------------------
22045
22046 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22047 Implicit_Base : Entity_Id;
22048 Base_Typ : Entity_Id;
22049 Lo_Val : Uint;
22050 Hi_Val : Uint;
22051 Errs : Boolean := False;
22052 Lo : Node_Id;
22053 Hi : Node_Id;
22054
22055 function Can_Derive_From (E : Entity_Id) return Boolean;
22056 -- Determine whether given bounds allow derivation from specified type
22057
22058 procedure Check_Bound (Expr : Node_Id);
22059 -- Check bound to make sure it is integral and static. If not, post
22060 -- appropriate error message and set Errs flag
22061
22062 ---------------------
22063 -- Can_Derive_From --
22064 ---------------------
22065
22066 -- Note we check both bounds against both end values, to deal with
22067 -- strange types like ones with a range of 0 .. -12341234.
22068
22069 function Can_Derive_From (E : Entity_Id) return Boolean is
22070 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22071 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22072 begin
22073 return Lo <= Lo_Val and then Lo_Val <= Hi
22074 and then
22075 Lo <= Hi_Val and then Hi_Val <= Hi;
22076 end Can_Derive_From;
22077
22078 -----------------
22079 -- Check_Bound --
22080 -----------------
22081
22082 procedure Check_Bound (Expr : Node_Id) is
22083 begin
22084 -- If a range constraint is used as an integer type definition, each
22085 -- bound of the range must be defined by a static expression of some
22086 -- integer type, but the two bounds need not have the same integer
22087 -- type (Negative bounds are allowed.) (RM 3.5.4)
22088
22089 if not Is_Integer_Type (Etype (Expr)) then
22090 Error_Msg_N
22091 ("integer type definition bounds must be of integer type", Expr);
22092 Errs := True;
22093
22094 elsif not Is_OK_Static_Expression (Expr) then
22095 Flag_Non_Static_Expr
22096 ("non-static expression used for integer type bound!", Expr);
22097 Errs := True;
22098
22099 -- Otherwise the bounds are folded into literals
22100
22101 elsif Is_Entity_Name (Expr) then
22102 Fold_Uint (Expr, Expr_Value (Expr), True);
22103 end if;
22104 end Check_Bound;
22105
22106 -- Start of processing for Signed_Integer_Type_Declaration
22107
22108 begin
22109 -- Create an anonymous base type
22110
22111 Implicit_Base :=
22112 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22113
22114 -- Analyze and check the bounds, they can be of any integer type
22115
22116 Lo := Low_Bound (Def);
22117 Hi := High_Bound (Def);
22118
22119 -- Arbitrarily use Integer as the type if either bound had an error
22120
22121 if Hi = Error or else Lo = Error then
22122 Base_Typ := Any_Integer;
22123 Set_Error_Posted (T, True);
22124 Errs := True;
22125
22126 -- Here both bounds are OK expressions
22127
22128 else
22129 Analyze_And_Resolve (Lo, Any_Integer);
22130 Analyze_And_Resolve (Hi, Any_Integer);
22131
22132 Check_Bound (Lo);
22133 Check_Bound (Hi);
22134
22135 if Errs then
22136 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22137 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22138 end if;
22139
22140 -- Find type to derive from
22141
22142 Lo_Val := Expr_Value (Lo);
22143 Hi_Val := Expr_Value (Hi);
22144
22145 if Can_Derive_From (Standard_Short_Short_Integer) then
22146 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22147
22148 elsif Can_Derive_From (Standard_Short_Integer) then
22149 Base_Typ := Base_Type (Standard_Short_Integer);
22150
22151 elsif Can_Derive_From (Standard_Integer) then
22152 Base_Typ := Base_Type (Standard_Integer);
22153
22154 elsif Can_Derive_From (Standard_Long_Integer) then
22155 Base_Typ := Base_Type (Standard_Long_Integer);
22156
22157 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22158 Check_Restriction (No_Long_Long_Integers, Def);
22159 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22160
22161 else
22162 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22163 Error_Msg_N ("integer type definition bounds out of range", Def);
22164 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22165 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22166 end if;
22167 end if;
22168
22169 -- Set the type of the bounds to the implicit base: we cannot set it to
22170 -- the new type, because this would be a forward reference for the code
22171 -- generator and, if the original type is user-defined, this could even
22172 -- lead to spurious semantic errors. Furthermore we do not set it to be
22173 -- universal, because this could make it much larger than needed here.
22174
22175 if not Errs then
22176 Set_Etype (Lo, Implicit_Base);
22177 Set_Etype (Hi, Implicit_Base);
22178 end if;
22179
22180 -- Complete both implicit base and declared first subtype entities. The
22181 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22182 -- are not clobbered when the signed integer type acts as a full view of
22183 -- a private type.
22184
22185 Set_Etype (Implicit_Base, Base_Typ);
22186 Set_Size_Info (Implicit_Base, Base_Typ);
22187 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22188 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22189 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22190
22191 Set_Ekind (T, E_Signed_Integer_Subtype);
22192 Set_Etype (T, Implicit_Base);
22193 Set_Size_Info (T, Implicit_Base);
22194 Inherit_Rep_Item_Chain (T, Implicit_Base);
22195 Set_Scalar_Range (T, Def);
22196 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22197 Set_Is_Constrained (T);
22198 end Signed_Integer_Type_Declaration;
22199
22200 end Sem_Ch3;