]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch3.adb
[Ada] Spurious "Duplicated symbol" error with discriminated tasks
[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-2018, 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 Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
82
83 package body Sem_Ch3 is
84
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
88
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
92 -- record type.
93
94 procedure Build_Derived_Type
95 (N : Node_Id;
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
114 -- the type).
115 --
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
118
119 procedure Build_Derived_Access_Type
120 (N : Node_Id;
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
126
127 procedure Build_Derived_Array_Type
128 (N : Node_Id;
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
134
135 procedure Build_Derived_Concurrent_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
142
143 procedure Build_Derived_Enumeration_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
150
151 procedure Build_Derived_Numeric_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
157
158 procedure Build_Derived_Private_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
167
168 procedure Build_Derived_Record_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
182
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
190
191 function Build_Discriminant_Constraints
192 (T : Entity_Id;
193 Def : Node_Id;
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
204
205 procedure Build_Discriminated_Subtype
206 (T : Entity_Id;
207 Def_Id : Entity_Id;
208 Elist : Elist_Id;
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
213 --
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
216 --
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 --
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
227
228 function Build_Scalar_Bound
229 (Bound : Node_Id;
230 Par_T : Entity_Id;
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
236
237 procedure Build_Underlying_Full_View
238 (N : Node_Id;
239 Typ : Entity_Id;
240 Par : Entity_Id);
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
247 -- view exchanges).
248
249 procedure Check_Access_Discriminant_Requires_Limited
250 (D : Node_Id;
251 Loc : Node_Id);
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
255
256 procedure Check_Anonymous_Access_Components
257 (Typ_Decl : Node_Id;
258 Typ : Entity_Id;
259 Prev : Entity_Id;
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
269
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
273
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
277
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
281
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284
285 procedure Check_Or_Process_Discriminants
286 (N : Node_Id;
287 T : Entity_Id;
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
293 -- if any.
294
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
298
299 procedure Constant_Redeclaration
300 (Id : Entity_Id;
301 N : Node_Id;
302 T : out Entity_Id);
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
306
307 function Contain_Interface
308 (Iface : Entity_Id;
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311
312 procedure Convert_Scalar_Bounds
313 (N : Node_Id;
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
316 Loc : Source_Ptr);
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
324
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
328
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
332
333 procedure Create_Constrained_Components
334 (Subt : Entity_Id;
335 Decl_Node : Node_Id;
336 Typ : Entity_Id;
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
346
347 function Constrain_Component_Type
348 (Comp : Entity_Id;
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
351 Typ : Entity_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
360
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
368
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
385
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
394 --
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
397 --
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
403 --
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree.
406 --
407 -- The last two arguments are used to create its external name if needed.
408
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
415
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419
420 procedure Constrain_Discriminated_Type
421 (Def_Id : Entity_Id;
422 S : Node_Id;
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 -- of For_Access.
431
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439
440 procedure Constrain_Index
441 (Index : Node_Id;
442 S : Node_Id;
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
445 Suffix : Character;
446 Suffix_Index : Nat);
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
452
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
455
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
464
465 procedure Decimal_Fixed_Point_Type_Declaration
466 (T : Entity_Id;
467 Def : Node_Id);
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
470
471 procedure Complete_Private_Subtype
472 (Priv : Entity_Id;
473 Full : Entity_Id;
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
479
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
490
491 procedure Derived_Standard_Character
492 (N : Node_Id;
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
497
498 procedure Derived_Type_Declaration
499 (T : Entity_Id;
500 N : Node_Id;
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
507
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
514
515 function Expand_To_Stored_Constraint
516 (Typ : Entity_Id;
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
521
522 function Find_Type_Of_Object
523 (Obj_Def : Node_Id;
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
527
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
530
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
536
537 function Inherit_Components
538 (N : Node_Id;
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
541 Is_Tagged : Boolean;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
548 --
549 -- N is the original derived type declaration
550 --
551 -- Is_Tagged is set if we are dealing with tagged types
552 --
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
555 --
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
559 --
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
562 --
563 -- which gets treated as
564 --
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 --
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
570 --
571 -- (Old_Component => New_Component),
572 --
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
578
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
582
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
588
589 function Is_Valid_Constraint_Kind
590 (T_Kind : Type_Kind;
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
594
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
597
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
601
602 procedure Ordinary_Fixed_Point_Type_Declaration
603 (T : Entity_Id;
604 Def : Node_Id);
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
607
608 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
609 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
610 -- In_Default_Expr can be properly adjusted.
611
612 procedure Prepare_Private_Subtype_Completion
613 (Id : Entity_Id;
614 Related_Nod : Node_Id);
615 -- Id is a subtype of some private type. Creates the full declaration
616 -- associated with Id whenever possible, i.e. when the full declaration
617 -- of the base type is already known. Records each subtype into
618 -- Private_Dependents of the base type.
619
620 procedure Process_Incomplete_Dependents
621 (N : Node_Id;
622 Full_T : Entity_Id;
623 Inc_T : Entity_Id);
624 -- Process all entities that depend on an incomplete type. There include
625 -- subtypes, subprogram types that mention the incomplete type in their
626 -- profiles, and subprogram with access parameters that designate the
627 -- incomplete type.
628
629 -- Inc_T is the defining identifier of an incomplete type declaration, its
630 -- Ekind is E_Incomplete_Type.
631 --
632 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
633 --
634 -- Full_T is N's defining identifier.
635 --
636 -- Subtypes of incomplete types with discriminants are completed when the
637 -- parent type is. This is simpler than private subtypes, because they can
638 -- only appear in the same scope, and there is no need to exchange views.
639 -- Similarly, access_to_subprogram types may have a parameter or a return
640 -- type that is an incomplete type, and that must be replaced with the
641 -- full type.
642 --
643 -- If the full type is tagged, subprogram with access parameters that
644 -- designated the incomplete may be primitive operations of the full type,
645 -- and have to be processed accordingly.
646
647 procedure Process_Real_Range_Specification (Def : Node_Id);
648 -- Given the type definition for a real type, this procedure processes and
649 -- checks the real range specification of this type definition if one is
650 -- present. If errors are found, error messages are posted, and the
651 -- Real_Range_Specification of Def is reset to Empty.
652
653 procedure Record_Type_Declaration
654 (T : Entity_Id;
655 N : Node_Id;
656 Prev : Entity_Id);
657 -- Process a record type declaration (for both untagged and tagged
658 -- records). Parameters T and N are exactly like in procedure
659 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
660 -- for this routine. If this is the completion of an incomplete type
661 -- declaration, Prev is the entity of the incomplete declaration, used for
662 -- cross-referencing. Otherwise Prev = T.
663
664 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
665 -- This routine is used to process the actual record type definition (both
666 -- for untagged and tagged records). Def is a record type definition node.
667 -- This procedure analyzes the components in this record type definition.
668 -- Prev_T is the entity for the enclosing record type. It is provided so
669 -- that its Has_Task flag can be set if any of the component have Has_Task
670 -- set. If the declaration is the completion of an incomplete type
671 -- declaration, Prev_T is the original incomplete type, whose full view is
672 -- the record type.
673
674 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
675 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
676 -- build a copy of the declaration tree of the parent, and we create
677 -- independently the list of components for the derived type. Semantic
678 -- information uses the component entities, but record representation
679 -- clauses are validated on the declaration tree. This procedure replaces
680 -- discriminants and components in the declaration with those that have
681 -- been created by Inherit_Components.
682
683 procedure Set_Fixed_Range
684 (E : Entity_Id;
685 Loc : Source_Ptr;
686 Lo : Ureal;
687 Hi : Ureal);
688 -- Build a range node with the given bounds and set it as the Scalar_Range
689 -- of the given fixed-point type entity. Loc is the source location used
690 -- for the constructed range. See body for further details.
691
692 procedure Set_Scalar_Range_For_Subtype
693 (Def_Id : Entity_Id;
694 R : Node_Id;
695 Subt : Entity_Id);
696 -- This routine is used to set the scalar range field for a subtype given
697 -- Def_Id, the entity for the subtype, and R, the range expression for the
698 -- scalar range. Subt provides the parent subtype to be used to analyze,
699 -- resolve, and check the given range.
700
701 procedure Set_Default_SSO (T : Entity_Id);
702 -- T is the entity for an array or record being declared. This procedure
703 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
704 -- to the setting of Opt.Default_SSO.
705
706 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
707 -- Create a new signed integer entity, and apply the constraint to obtain
708 -- the required first named subtype of this type.
709
710 procedure Set_Stored_Constraint_From_Discriminant_Constraint
711 (E : Entity_Id);
712 -- E is some record type. This routine computes E's Stored_Constraint
713 -- from its Discriminant_Constraint.
714
715 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
716 -- Check that an entity in a list of progenitors is an interface,
717 -- emit error otherwise.
718
719 -----------------------
720 -- Access_Definition --
721 -----------------------
722
723 function Access_Definition
724 (Related_Nod : Node_Id;
725 N : Node_Id) return Entity_Id
726 is
727 Anon_Type : Entity_Id;
728 Anon_Scope : Entity_Id;
729 Desig_Type : Entity_Id;
730 Enclosing_Prot_Type : Entity_Id := Empty;
731
732 begin
733 Check_SPARK_05_Restriction ("access type is not allowed", N);
734
735 if Is_Entry (Current_Scope)
736 and then Is_Task_Type (Etype (Scope (Current_Scope)))
737 then
738 Error_Msg_N ("task entries cannot have access parameters", N);
739 return Empty;
740 end if;
741
742 -- Ada 2005: For an object declaration the corresponding anonymous
743 -- type is declared in the current scope.
744
745 -- If the access definition is the return type of another access to
746 -- function, scope is the current one, because it is the one of the
747 -- current type declaration, except for the pathological case below.
748
749 if Nkind_In (Related_Nod, N_Object_Declaration,
750 N_Access_Function_Definition)
751 then
752 Anon_Scope := Current_Scope;
753
754 -- A pathological case: function returning access functions that
755 -- return access functions, etc. Each anonymous access type created
756 -- is in the enclosing scope of the outermost function.
757
758 declare
759 Par : Node_Id;
760
761 begin
762 Par := Related_Nod;
763 while Nkind_In (Par, N_Access_Function_Definition,
764 N_Access_Definition)
765 loop
766 Par := Parent (Par);
767 end loop;
768
769 if Nkind (Par) = N_Function_Specification then
770 Anon_Scope := Scope (Defining_Entity (Par));
771 end if;
772 end;
773
774 -- For the anonymous function result case, retrieve the scope of the
775 -- function specification's associated entity rather than using the
776 -- current scope. The current scope will be the function itself if the
777 -- formal part is currently being analyzed, but will be the parent scope
778 -- in the case of a parameterless function, and we always want to use
779 -- the function's parent scope. Finally, if the function is a child
780 -- unit, we must traverse the tree to retrieve the proper entity.
781
782 elsif Nkind (Related_Nod) = N_Function_Specification
783 and then Nkind (Parent (N)) /= N_Parameter_Specification
784 then
785 -- If the current scope is a protected type, the anonymous access
786 -- is associated with one of the protected operations, and must
787 -- be available in the scope that encloses the protected declaration.
788 -- Otherwise the type is in the scope enclosing the subprogram.
789
790 -- If the function has formals, The return type of a subprogram
791 -- declaration is analyzed in the scope of the subprogram (see
792 -- Process_Formals) and thus the protected type, if present, is
793 -- the scope of the current function scope.
794
795 if Ekind (Current_Scope) = E_Protected_Type then
796 Enclosing_Prot_Type := Current_Scope;
797
798 elsif Ekind (Current_Scope) = E_Function
799 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
800 then
801 Enclosing_Prot_Type := Scope (Current_Scope);
802 end if;
803
804 if Present (Enclosing_Prot_Type) then
805 Anon_Scope := Scope (Enclosing_Prot_Type);
806
807 else
808 Anon_Scope := Scope (Defining_Entity (Related_Nod));
809 end if;
810
811 -- For an access type definition, if the current scope is a child
812 -- unit it is the scope of the type.
813
814 elsif Is_Compilation_Unit (Current_Scope) then
815 Anon_Scope := Current_Scope;
816
817 -- For access formals, access components, and access discriminants, the
818 -- scope is that of the enclosing declaration,
819
820 else
821 Anon_Scope := Scope (Current_Scope);
822 end if;
823
824 Anon_Type :=
825 Create_Itype
826 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
827
828 if All_Present (N)
829 and then Ada_Version >= Ada_2005
830 then
831 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
832 end if;
833
834 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
835 -- the corresponding semantic routine
836
837 if Present (Access_To_Subprogram_Definition (N)) then
838
839 -- Compiler runtime units are compiled in Ada 2005 mode when building
840 -- the runtime library but must also be compilable in Ada 95 mode
841 -- (when bootstrapping the compiler).
842
843 Check_Compiler_Unit ("anonymous access to subprogram", N);
844
845 Access_Subprogram_Declaration
846 (T_Name => Anon_Type,
847 T_Def => Access_To_Subprogram_Definition (N));
848
849 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
850 Set_Ekind
851 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
852 else
853 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
854 end if;
855
856 Set_Can_Use_Internal_Rep
857 (Anon_Type, not Always_Compatible_Rep_On_Target);
858
859 -- If the anonymous access is associated with a protected operation,
860 -- create a reference to it after the enclosing protected definition
861 -- because the itype will be used in the subsequent bodies.
862
863 -- If the anonymous access itself is protected, a full type
864 -- declaratiton will be created for it, so that the equivalent
865 -- record type can be constructed. For further details, see
866 -- Replace_Anonymous_Access_To_Protected-Subprogram.
867
868 if Ekind (Current_Scope) = E_Protected_Type
869 and then not Protected_Present (Access_To_Subprogram_Definition (N))
870 then
871 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
872 end if;
873
874 return Anon_Type;
875 end if;
876
877 Find_Type (Subtype_Mark (N));
878 Desig_Type := Entity (Subtype_Mark (N));
879
880 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
881 Set_Etype (Anon_Type, Anon_Type);
882
883 -- Make sure the anonymous access type has size and alignment fields
884 -- set, as required by gigi. This is necessary in the case of the
885 -- Task_Body_Procedure.
886
887 if not Has_Private_Component (Desig_Type) then
888 Layout_Type (Anon_Type);
889 end if;
890
891 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
892 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
893 -- the null value is allowed. In Ada 95 the null value is never allowed.
894
895 if Ada_Version >= Ada_2005 then
896 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
897 else
898 Set_Can_Never_Be_Null (Anon_Type, True);
899 end if;
900
901 -- The anonymous access type is as public as the discriminated type or
902 -- subprogram that defines it. It is imported (for back-end purposes)
903 -- if the designated type is.
904
905 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
906
907 -- Ada 2005 (AI-231): Propagate the access-constant attribute
908
909 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
910
911 -- The context is either a subprogram declaration, object declaration,
912 -- or an access discriminant, in a private or a full type declaration.
913 -- In the case of a subprogram, if the designated type is incomplete,
914 -- the operation will be a primitive operation of the full type, to be
915 -- updated subsequently. If the type is imported through a limited_with
916 -- clause, the subprogram is not a primitive operation of the type
917 -- (which is declared elsewhere in some other scope).
918
919 if Ekind (Desig_Type) = E_Incomplete_Type
920 and then not From_Limited_With (Desig_Type)
921 and then Is_Overloadable (Current_Scope)
922 then
923 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
924 Set_Has_Delayed_Freeze (Current_Scope);
925 end if;
926
927 -- Ada 2005: If the designated type is an interface that may contain
928 -- tasks, create a Master entity for the declaration. This must be done
929 -- before expansion of the full declaration, because the declaration may
930 -- include an expression that is an allocator, whose expansion needs the
931 -- proper Master for the created tasks.
932
933 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
934 then
935 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
936 then
937 Build_Class_Wide_Master (Anon_Type);
938
939 -- Similarly, if the type is an anonymous access that designates
940 -- tasks, create a master entity for it in the current context.
941
942 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
943 then
944 Build_Master_Entity (Defining_Identifier (Related_Nod));
945 Build_Master_Renaming (Anon_Type);
946 end if;
947 end if;
948
949 -- For a private component of a protected type, it is imperative that
950 -- the back-end elaborate the type immediately after the protected
951 -- declaration, because this type will be used in the declarations
952 -- created for the component within each protected body, so we must
953 -- create an itype reference for it now.
954
955 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
956 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
957
958 -- Similarly, if the access definition is the return result of a
959 -- function, create an itype reference for it because it will be used
960 -- within the function body. For a regular function that is not a
961 -- compilation unit, insert reference after the declaration. For a
962 -- protected operation, insert it after the enclosing protected type
963 -- declaration. In either case, do not create a reference for a type
964 -- obtained through a limited_with clause, because this would introduce
965 -- semantic dependencies.
966
967 -- Similarly, do not create a reference if the designated type is a
968 -- generic formal, because no use of it will reach the backend.
969
970 elsif Nkind (Related_Nod) = N_Function_Specification
971 and then not From_Limited_With (Desig_Type)
972 and then not Is_Generic_Type (Desig_Type)
973 then
974 if Present (Enclosing_Prot_Type) then
975 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
976
977 elsif Is_List_Member (Parent (Related_Nod))
978 and then Nkind (Parent (N)) /= N_Parameter_Specification
979 then
980 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
981 end if;
982
983 -- Finally, create an itype reference for an object declaration of an
984 -- anonymous access type. This is strictly necessary only for deferred
985 -- constants, but in any case will avoid out-of-scope problems in the
986 -- back-end.
987
988 elsif Nkind (Related_Nod) = N_Object_Declaration then
989 Build_Itype_Reference (Anon_Type, Related_Nod);
990 end if;
991
992 return Anon_Type;
993 end Access_Definition;
994
995 -----------------------------------
996 -- Access_Subprogram_Declaration --
997 -----------------------------------
998
999 procedure Access_Subprogram_Declaration
1000 (T_Name : Entity_Id;
1001 T_Def : Node_Id)
1002 is
1003 procedure Check_For_Premature_Usage (Def : Node_Id);
1004 -- Check that type T_Name is not used, directly or recursively, as a
1005 -- parameter or a return type in Def. Def is either a subtype, an
1006 -- access_definition, or an access_to_subprogram_definition.
1007
1008 -------------------------------
1009 -- Check_For_Premature_Usage --
1010 -------------------------------
1011
1012 procedure Check_For_Premature_Usage (Def : Node_Id) is
1013 Param : Node_Id;
1014
1015 begin
1016 -- Check for a subtype mark
1017
1018 if Nkind (Def) in N_Has_Etype then
1019 if Etype (Def) = T_Name then
1020 Error_Msg_N
1021 ("type& cannot be used before end of its declaration", Def);
1022 end if;
1023
1024 -- If this is not a subtype, then this is an access_definition
1025
1026 elsif Nkind (Def) = N_Access_Definition then
1027 if Present (Access_To_Subprogram_Definition (Def)) then
1028 Check_For_Premature_Usage
1029 (Access_To_Subprogram_Definition (Def));
1030 else
1031 Check_For_Premature_Usage (Subtype_Mark (Def));
1032 end if;
1033
1034 -- The only cases left are N_Access_Function_Definition and
1035 -- N_Access_Procedure_Definition.
1036
1037 else
1038 if Present (Parameter_Specifications (Def)) then
1039 Param := First (Parameter_Specifications (Def));
1040 while Present (Param) loop
1041 Check_For_Premature_Usage (Parameter_Type (Param));
1042 Param := Next (Param);
1043 end loop;
1044 end if;
1045
1046 if Nkind (Def) = N_Access_Function_Definition then
1047 Check_For_Premature_Usage (Result_Definition (Def));
1048 end if;
1049 end if;
1050 end Check_For_Premature_Usage;
1051
1052 -- Local variables
1053
1054 Formals : constant List_Id := Parameter_Specifications (T_Def);
1055 Formal : Entity_Id;
1056 D_Ityp : Node_Id;
1057 Desig_Type : constant Entity_Id :=
1058 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1059
1060 -- Start of processing for Access_Subprogram_Declaration
1061
1062 begin
1063 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1064
1065 -- Associate the Itype node with the inner full-type declaration or
1066 -- subprogram spec or entry body. This is required to handle nested
1067 -- anonymous declarations. For example:
1068
1069 -- procedure P
1070 -- (X : access procedure
1071 -- (Y : access procedure
1072 -- (Z : access T)))
1073
1074 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1075 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1076 N_Private_Type_Declaration,
1077 N_Private_Extension_Declaration,
1078 N_Procedure_Specification,
1079 N_Function_Specification,
1080 N_Entry_Body)
1081
1082 or else
1083 Nkind_In (D_Ityp, N_Object_Declaration,
1084 N_Object_Renaming_Declaration,
1085 N_Formal_Object_Declaration,
1086 N_Formal_Type_Declaration,
1087 N_Task_Type_Declaration,
1088 N_Protected_Type_Declaration))
1089 loop
1090 D_Ityp := Parent (D_Ityp);
1091 pragma Assert (D_Ityp /= Empty);
1092 end loop;
1093
1094 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1095
1096 if Nkind_In (D_Ityp, N_Procedure_Specification,
1097 N_Function_Specification)
1098 then
1099 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1100
1101 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1102 N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Type_Declaration)
1105 then
1106 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1107 end if;
1108
1109 if Nkind (T_Def) = N_Access_Function_Definition then
1110 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1111 declare
1112 Acc : constant Node_Id := Result_Definition (T_Def);
1113
1114 begin
1115 if Present (Access_To_Subprogram_Definition (Acc))
1116 and then
1117 Protected_Present (Access_To_Subprogram_Definition (Acc))
1118 then
1119 Set_Etype
1120 (Desig_Type,
1121 Replace_Anonymous_Access_To_Protected_Subprogram
1122 (T_Def));
1123
1124 else
1125 Set_Etype
1126 (Desig_Type,
1127 Access_Definition (T_Def, Result_Definition (T_Def)));
1128 end if;
1129 end;
1130
1131 else
1132 Analyze (Result_Definition (T_Def));
1133
1134 declare
1135 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1136
1137 begin
1138 -- If a null exclusion is imposed on the result type, then
1139 -- create a null-excluding itype (an access subtype) and use
1140 -- it as the function's Etype.
1141
1142 if Is_Access_Type (Typ)
1143 and then Null_Exclusion_In_Return_Present (T_Def)
1144 then
1145 Set_Etype (Desig_Type,
1146 Create_Null_Excluding_Itype
1147 (T => Typ,
1148 Related_Nod => T_Def,
1149 Scope_Id => Current_Scope));
1150
1151 else
1152 if From_Limited_With (Typ) then
1153
1154 -- AI05-151: Incomplete types are allowed in all basic
1155 -- declarations, including access to subprograms.
1156
1157 if Ada_Version >= Ada_2012 then
1158 null;
1159
1160 else
1161 Error_Msg_NE
1162 ("illegal use of incomplete type&",
1163 Result_Definition (T_Def), Typ);
1164 end if;
1165
1166 elsif Ekind (Current_Scope) = E_Package
1167 and then In_Private_Part (Current_Scope)
1168 then
1169 if Ekind (Typ) = E_Incomplete_Type then
1170 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1171
1172 elsif Is_Class_Wide_Type (Typ)
1173 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1174 then
1175 Append_Elmt
1176 (Desig_Type, Private_Dependents (Etype (Typ)));
1177 end if;
1178 end if;
1179
1180 Set_Etype (Desig_Type, Typ);
1181 end if;
1182 end;
1183 end if;
1184
1185 if not (Is_Type (Etype (Desig_Type))) then
1186 Error_Msg_N
1187 ("expect type in function specification",
1188 Result_Definition (T_Def));
1189 end if;
1190
1191 else
1192 Set_Etype (Desig_Type, Standard_Void_Type);
1193 end if;
1194
1195 if Present (Formals) then
1196 Push_Scope (Desig_Type);
1197
1198 -- Some special tests here. These special tests can be removed
1199 -- if and when Itypes always have proper parent pointers to their
1200 -- declarations???
1201
1202 -- Special test 1) Link defining_identifier of formals. Required by
1203 -- First_Formal to provide its functionality.
1204
1205 declare
1206 F : Node_Id;
1207
1208 begin
1209 F := First (Formals);
1210
1211 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1212 -- when it is part of an unconstrained type and subtype expansion
1213 -- is disabled. To avoid back-end problems with shared profiles,
1214 -- use previous subprogram type as the designated type, and then
1215 -- remove scope added above.
1216
1217 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1218 then
1219 Set_Etype (T_Name, T_Name);
1220 Init_Size_Align (T_Name);
1221 Set_Directly_Designated_Type (T_Name,
1222 Scope (Defining_Identifier (F)));
1223 End_Scope;
1224 return;
1225 end if;
1226
1227 while Present (F) loop
1228 if No (Parent (Defining_Identifier (F))) then
1229 Set_Parent (Defining_Identifier (F), F);
1230 end if;
1231
1232 Next (F);
1233 end loop;
1234 end;
1235
1236 Process_Formals (Formals, Parent (T_Def));
1237
1238 -- Special test 2) End_Scope requires that the parent pointer be set
1239 -- to something reasonable, but Itypes don't have parent pointers. So
1240 -- we set it and then unset it ???
1241
1242 Set_Parent (Desig_Type, T_Name);
1243 End_Scope;
1244 Set_Parent (Desig_Type, Empty);
1245 end if;
1246
1247 -- Check for premature usage of the type being defined
1248
1249 Check_For_Premature_Usage (T_Def);
1250
1251 -- The return type and/or any parameter type may be incomplete. Mark the
1252 -- subprogram_type as depending on the incomplete type, so that it can
1253 -- be updated when the full type declaration is seen. This only applies
1254 -- to incomplete types declared in some enclosing scope, not to limited
1255 -- views from other packages.
1256
1257 -- Prior to Ada 2012, access to functions can only have in_parameters.
1258
1259 if Present (Formals) then
1260 Formal := First_Formal (Desig_Type);
1261 while Present (Formal) loop
1262 if Ekind (Formal) /= E_In_Parameter
1263 and then Nkind (T_Def) = N_Access_Function_Definition
1264 and then Ada_Version < Ada_2012
1265 then
1266 Error_Msg_N ("functions can only have IN parameters", Formal);
1267 end if;
1268
1269 if Ekind (Etype (Formal)) = E_Incomplete_Type
1270 and then In_Open_Scopes (Scope (Etype (Formal)))
1271 then
1272 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1273 Set_Has_Delayed_Freeze (Desig_Type);
1274 end if;
1275
1276 Next_Formal (Formal);
1277 end loop;
1278 end if;
1279
1280 -- Check whether an indirect call without actuals may be possible. This
1281 -- is used when resolving calls whose result is then indexed.
1282
1283 May_Need_Actuals (Desig_Type);
1284
1285 -- If the return type is incomplete, this is legal as long as the type
1286 -- is declared in the current scope and will be completed in it (rather
1287 -- than being part of limited view).
1288
1289 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1290 and then not Has_Delayed_Freeze (Desig_Type)
1291 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1292 then
1293 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1294 Set_Has_Delayed_Freeze (Desig_Type);
1295 end if;
1296
1297 Check_Delayed_Subprogram (Desig_Type);
1298
1299 if Protected_Present (T_Def) then
1300 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1301 Set_Convention (Desig_Type, Convention_Protected);
1302 else
1303 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1304 end if;
1305
1306 Set_Can_Use_Internal_Rep (T_Name,
1307 not Always_Compatible_Rep_On_Target);
1308 Set_Etype (T_Name, T_Name);
1309 Init_Size_Align (T_Name);
1310 Set_Directly_Designated_Type (T_Name, Desig_Type);
1311
1312 -- If the access_to_subprogram is not declared at the library level,
1313 -- it can only point to subprograms that are at the same or deeper
1314 -- accessibility level. The corresponding subprogram type might
1315 -- require an activation record when compiling for C.
1316
1317 Set_Needs_Activation_Record (Desig_Type,
1318 not Is_Library_Level_Entity (T_Name));
1319
1320 Generate_Reference_To_Formals (T_Name);
1321
1322 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1323
1324 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1325
1326 Check_Restriction (No_Access_Subprograms, T_Def);
1327 end Access_Subprogram_Declaration;
1328
1329 ----------------------------
1330 -- Access_Type_Declaration --
1331 ----------------------------
1332
1333 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1334 P : constant Node_Id := Parent (Def);
1335 S : constant Node_Id := Subtype_Indication (Def);
1336
1337 Full_Desig : Entity_Id;
1338
1339 begin
1340 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1341
1342 -- Check for permissible use of incomplete type
1343
1344 if Nkind (S) /= N_Subtype_Indication then
1345 Analyze (S);
1346
1347 if Present (Entity (S))
1348 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1349 then
1350 Set_Directly_Designated_Type (T, Entity (S));
1351
1352 -- If the designated type is a limited view, we cannot tell if
1353 -- the full view contains tasks, and there is no way to handle
1354 -- that full view in a client. We create a master entity for the
1355 -- scope, which will be used when a client determines that one
1356 -- is needed.
1357
1358 if From_Limited_With (Entity (S))
1359 and then not Is_Class_Wide_Type (Entity (S))
1360 then
1361 Set_Ekind (T, E_Access_Type);
1362 Build_Master_Entity (T);
1363 Build_Master_Renaming (T);
1364 end if;
1365
1366 else
1367 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1368 end if;
1369
1370 -- If the access definition is of the form: ACCESS NOT NULL ..
1371 -- the subtype indication must be of an access type. Create
1372 -- a null-excluding subtype of it.
1373
1374 if Null_Excluding_Subtype (Def) then
1375 if not Is_Access_Type (Entity (S)) then
1376 Error_Msg_N ("null exclusion must apply to access type", Def);
1377
1378 else
1379 declare
1380 Loc : constant Source_Ptr := Sloc (S);
1381 Decl : Node_Id;
1382 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1383
1384 begin
1385 Decl :=
1386 Make_Subtype_Declaration (Loc,
1387 Defining_Identifier => Nam,
1388 Subtype_Indication =>
1389 New_Occurrence_Of (Entity (S), Loc));
1390 Set_Null_Exclusion_Present (Decl);
1391 Insert_Before (Parent (Def), Decl);
1392 Analyze (Decl);
1393 Set_Entity (S, Nam);
1394 end;
1395 end if;
1396 end if;
1397
1398 else
1399 Set_Directly_Designated_Type (T,
1400 Process_Subtype (S, P, T, 'P'));
1401 end if;
1402
1403 if All_Present (Def) or Constant_Present (Def) then
1404 Set_Ekind (T, E_General_Access_Type);
1405 else
1406 Set_Ekind (T, E_Access_Type);
1407 end if;
1408
1409 Full_Desig := Designated_Type (T);
1410
1411 if Base_Type (Full_Desig) = T then
1412 Error_Msg_N ("access type cannot designate itself", S);
1413
1414 -- In Ada 2005, the type may have a limited view through some unit in
1415 -- its own context, allowing the following circularity that cannot be
1416 -- detected earlier.
1417
1418 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1419 then
1420 Error_Msg_N
1421 ("access type cannot designate its own class-wide type", S);
1422
1423 -- Clean up indication of tagged status to prevent cascaded errors
1424
1425 Set_Is_Tagged_Type (T, False);
1426 end if;
1427
1428 Set_Etype (T, T);
1429
1430 -- If the type has appeared already in a with_type clause, it is frozen
1431 -- and the pointer size is already set. Else, initialize.
1432
1433 if not From_Limited_With (T) then
1434 Init_Size_Align (T);
1435 end if;
1436
1437 -- Note that Has_Task is always false, since the access type itself
1438 -- is not a task type. See Einfo for more description on this point.
1439 -- Exactly the same consideration applies to Has_Controlled_Component
1440 -- and to Has_Protected.
1441
1442 Set_Has_Task (T, False);
1443 Set_Has_Protected (T, False);
1444 Set_Has_Timing_Event (T, False);
1445 Set_Has_Controlled_Component (T, False);
1446
1447 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1448 -- problems where an incomplete view of this entity has been previously
1449 -- established by a limited with and an overlaid version of this field
1450 -- (Stored_Constraint) was initialized for the incomplete view.
1451
1452 -- This reset is performed in most cases except where the access type
1453 -- has been created for the purposes of allocating or deallocating a
1454 -- build-in-place object. Such access types have explicitly set pools
1455 -- and finalization masters.
1456
1457 if No (Associated_Storage_Pool (T)) then
1458 Set_Finalization_Master (T, Empty);
1459 end if;
1460
1461 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1462 -- attributes
1463
1464 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1465 Set_Is_Access_Constant (T, Constant_Present (Def));
1466 end Access_Type_Declaration;
1467
1468 ----------------------------------
1469 -- Add_Interface_Tag_Components --
1470 ----------------------------------
1471
1472 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1473 Loc : constant Source_Ptr := Sloc (N);
1474 L : List_Id;
1475 Last_Tag : Node_Id;
1476
1477 procedure Add_Tag (Iface : Entity_Id);
1478 -- Add tag for one of the progenitor interfaces
1479
1480 -------------
1481 -- Add_Tag --
1482 -------------
1483
1484 procedure Add_Tag (Iface : Entity_Id) is
1485 Decl : Node_Id;
1486 Def : Node_Id;
1487 Tag : Entity_Id;
1488 Offset : Entity_Id;
1489
1490 begin
1491 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1492
1493 -- This is a reasonable place to propagate predicates
1494
1495 if Has_Predicates (Iface) then
1496 Set_Has_Predicates (Typ);
1497 end if;
1498
1499 Def :=
1500 Make_Component_Definition (Loc,
1501 Aliased_Present => True,
1502 Subtype_Indication =>
1503 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1504
1505 Tag := Make_Temporary (Loc, 'V');
1506
1507 Decl :=
1508 Make_Component_Declaration (Loc,
1509 Defining_Identifier => Tag,
1510 Component_Definition => Def);
1511
1512 Analyze_Component_Declaration (Decl);
1513
1514 Set_Analyzed (Decl);
1515 Set_Ekind (Tag, E_Component);
1516 Set_Is_Tag (Tag);
1517 Set_Is_Aliased (Tag);
1518 Set_Related_Type (Tag, Iface);
1519 Init_Component_Location (Tag);
1520
1521 pragma Assert (Is_Frozen (Iface));
1522
1523 Set_DT_Entry_Count (Tag,
1524 DT_Entry_Count (First_Entity (Iface)));
1525
1526 if No (Last_Tag) then
1527 Prepend (Decl, L);
1528 else
1529 Insert_After (Last_Tag, Decl);
1530 end if;
1531
1532 Last_Tag := Decl;
1533
1534 -- If the ancestor has discriminants we need to give special support
1535 -- to store the offset_to_top value of the secondary dispatch tables.
1536 -- For this purpose we add a supplementary component just after the
1537 -- field that contains the tag associated with each secondary DT.
1538
1539 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1540 Def :=
1541 Make_Component_Definition (Loc,
1542 Subtype_Indication =>
1543 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1544
1545 Offset := Make_Temporary (Loc, 'V');
1546
1547 Decl :=
1548 Make_Component_Declaration (Loc,
1549 Defining_Identifier => Offset,
1550 Component_Definition => Def);
1551
1552 Analyze_Component_Declaration (Decl);
1553
1554 Set_Analyzed (Decl);
1555 Set_Ekind (Offset, E_Component);
1556 Set_Is_Aliased (Offset);
1557 Set_Related_Type (Offset, Iface);
1558 Init_Component_Location (Offset);
1559 Insert_After (Last_Tag, Decl);
1560 Last_Tag := Decl;
1561 end if;
1562 end Add_Tag;
1563
1564 -- Local variables
1565
1566 Elmt : Elmt_Id;
1567 Ext : Node_Id;
1568 Comp : Node_Id;
1569
1570 -- Start of processing for Add_Interface_Tag_Components
1571
1572 begin
1573 if not RTE_Available (RE_Interface_Tag) then
1574 Error_Msg
1575 ("(Ada 2005) interface types not supported by this run-time!",
1576 Sloc (N));
1577 return;
1578 end if;
1579
1580 if Ekind (Typ) /= E_Record_Type
1581 or else (Is_Concurrent_Record_Type (Typ)
1582 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1583 or else (not Is_Concurrent_Record_Type (Typ)
1584 and then No (Interfaces (Typ))
1585 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1586 then
1587 return;
1588 end if;
1589
1590 -- Find the current last tag
1591
1592 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1593 Ext := Record_Extension_Part (Type_Definition (N));
1594 else
1595 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1596 Ext := Type_Definition (N);
1597 end if;
1598
1599 Last_Tag := Empty;
1600
1601 if not (Present (Component_List (Ext))) then
1602 Set_Null_Present (Ext, False);
1603 L := New_List;
1604 Set_Component_List (Ext,
1605 Make_Component_List (Loc,
1606 Component_Items => L,
1607 Null_Present => False));
1608 else
1609 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1610 L := Component_Items
1611 (Component_List
1612 (Record_Extension_Part
1613 (Type_Definition (N))));
1614 else
1615 L := Component_Items
1616 (Component_List
1617 (Type_Definition (N)));
1618 end if;
1619
1620 -- Find the last tag component
1621
1622 Comp := First (L);
1623 while Present (Comp) loop
1624 if Nkind (Comp) = N_Component_Declaration
1625 and then Is_Tag (Defining_Identifier (Comp))
1626 then
1627 Last_Tag := Comp;
1628 end if;
1629
1630 Next (Comp);
1631 end loop;
1632 end if;
1633
1634 -- At this point L references the list of components and Last_Tag
1635 -- references the current last tag (if any). Now we add the tag
1636 -- corresponding with all the interfaces that are not implemented
1637 -- by the parent.
1638
1639 if Present (Interfaces (Typ)) then
1640 Elmt := First_Elmt (Interfaces (Typ));
1641 while Present (Elmt) loop
1642 Add_Tag (Node (Elmt));
1643 Next_Elmt (Elmt);
1644 end loop;
1645 end if;
1646 end Add_Interface_Tag_Components;
1647
1648 -------------------------------------
1649 -- Add_Internal_Interface_Entities --
1650 -------------------------------------
1651
1652 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1653 Elmt : Elmt_Id;
1654 Iface : Entity_Id;
1655 Iface_Elmt : Elmt_Id;
1656 Iface_Prim : Entity_Id;
1657 Ifaces_List : Elist_Id;
1658 New_Subp : Entity_Id := Empty;
1659 Prim : Entity_Id;
1660 Restore_Scope : Boolean := False;
1661
1662 begin
1663 pragma Assert (Ada_Version >= Ada_2005
1664 and then Is_Record_Type (Tagged_Type)
1665 and then Is_Tagged_Type (Tagged_Type)
1666 and then Has_Interfaces (Tagged_Type)
1667 and then not Is_Interface (Tagged_Type));
1668
1669 -- Ensure that the internal entities are added to the scope of the type
1670
1671 if Scope (Tagged_Type) /= Current_Scope then
1672 Push_Scope (Scope (Tagged_Type));
1673 Restore_Scope := True;
1674 end if;
1675
1676 Collect_Interfaces (Tagged_Type, Ifaces_List);
1677
1678 Iface_Elmt := First_Elmt (Ifaces_List);
1679 while Present (Iface_Elmt) loop
1680 Iface := Node (Iface_Elmt);
1681
1682 -- Originally we excluded here from this processing interfaces that
1683 -- are parents of Tagged_Type because their primitives are located
1684 -- in the primary dispatch table (and hence no auxiliary internal
1685 -- entities are required to handle secondary dispatch tables in such
1686 -- case). However, these auxiliary entities are also required to
1687 -- handle derivations of interfaces in formals of generics (see
1688 -- Derive_Subprograms).
1689
1690 Elmt := First_Elmt (Primitive_Operations (Iface));
1691 while Present (Elmt) loop
1692 Iface_Prim := Node (Elmt);
1693
1694 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1695 Prim :=
1696 Find_Primitive_Covering_Interface
1697 (Tagged_Type => Tagged_Type,
1698 Iface_Prim => Iface_Prim);
1699
1700 if No (Prim) and then Serious_Errors_Detected > 0 then
1701 goto Continue;
1702 end if;
1703
1704 pragma Assert (Present (Prim));
1705
1706 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1707 -- differs from the name of the interface primitive then it is
1708 -- a private primitive inherited from a parent type. In such
1709 -- case, given that Tagged_Type covers the interface, the
1710 -- inherited private primitive becomes visible. For such
1711 -- purpose we add a new entity that renames the inherited
1712 -- private primitive.
1713
1714 if Chars (Prim) /= Chars (Iface_Prim) then
1715 pragma Assert (Has_Suffix (Prim, 'P'));
1716 Derive_Subprogram
1717 (New_Subp => New_Subp,
1718 Parent_Subp => Iface_Prim,
1719 Derived_Type => Tagged_Type,
1720 Parent_Type => Iface);
1721 Set_Alias (New_Subp, Prim);
1722 Set_Is_Abstract_Subprogram
1723 (New_Subp, Is_Abstract_Subprogram (Prim));
1724 end if;
1725
1726 Derive_Subprogram
1727 (New_Subp => New_Subp,
1728 Parent_Subp => Iface_Prim,
1729 Derived_Type => Tagged_Type,
1730 Parent_Type => Iface);
1731
1732 declare
1733 Anc : Entity_Id;
1734 begin
1735 if Is_Inherited_Operation (Prim)
1736 and then Present (Alias (Prim))
1737 then
1738 Anc := Alias (Prim);
1739 else
1740 Anc := Overridden_Operation (Prim);
1741 end if;
1742
1743 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1744 -- nonconforming preconditions in both an ancestor and
1745 -- a progenitor operation.
1746
1747 -- If the operation is a primitive wrapper it is an explicit
1748 -- (overriding) operqtion and all is fine.
1749
1750 if Present (Anc)
1751 and then Has_Non_Trivial_Precondition (Anc)
1752 and then Has_Non_Trivial_Precondition (Iface_Prim)
1753 then
1754 if Is_Abstract_Subprogram (Prim)
1755 or else
1756 (Ekind (Prim) = E_Procedure
1757 and then Nkind (Parent (Prim)) =
1758 N_Procedure_Specification
1759 and then Null_Present (Parent (Prim)))
1760 or else Is_Primitive_Wrapper (Prim)
1761 then
1762 null;
1763
1764 -- The operation is inherited and must be overridden
1765
1766 elsif not Comes_From_Source (Prim) then
1767 Error_Msg_NE
1768 ("&inherits non-conforming preconditions and must "
1769 & "be overridden (RM 6.1.1 (10-16)",
1770 Parent (Tagged_Type), Prim);
1771 end if;
1772 end if;
1773 end;
1774
1775 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1776 -- associated with interface types. These entities are
1777 -- only registered in the list of primitives of its
1778 -- corresponding tagged type because they are only used
1779 -- to fill the contents of the secondary dispatch tables.
1780 -- Therefore they are removed from the homonym chains.
1781
1782 Set_Is_Hidden (New_Subp);
1783 Set_Is_Internal (New_Subp);
1784 Set_Alias (New_Subp, Prim);
1785 Set_Is_Abstract_Subprogram
1786 (New_Subp, Is_Abstract_Subprogram (Prim));
1787 Set_Interface_Alias (New_Subp, Iface_Prim);
1788
1789 -- If the returned type is an interface then propagate it to
1790 -- the returned type. Needed by the thunk to generate the code
1791 -- which displaces "this" to reference the corresponding
1792 -- secondary dispatch table in the returned object.
1793
1794 if Is_Interface (Etype (Iface_Prim)) then
1795 Set_Etype (New_Subp, Etype (Iface_Prim));
1796 end if;
1797
1798 -- Internal entities associated with interface types are only
1799 -- registered in the list of primitives of the tagged type.
1800 -- They are only used to fill the contents of the secondary
1801 -- dispatch tables. Therefore they are not needed in the
1802 -- homonym chains.
1803
1804 Remove_Homonym (New_Subp);
1805
1806 -- Hidden entities associated with interfaces must have set
1807 -- the Has_Delay_Freeze attribute to ensure that, in case
1808 -- of locally defined tagged types (or compiling with static
1809 -- dispatch tables generation disabled) the corresponding
1810 -- entry of the secondary dispatch table is filled when such
1811 -- an entity is frozen. This is an expansion activity that must
1812 -- be suppressed for ASIS because it leads to gigi elaboration
1813 -- issues in annotate mode.
1814
1815 if not ASIS_Mode then
1816 Set_Has_Delayed_Freeze (New_Subp);
1817 end if;
1818 end if;
1819
1820 <<Continue>>
1821 Next_Elmt (Elmt);
1822 end loop;
1823
1824 Next_Elmt (Iface_Elmt);
1825 end loop;
1826
1827 if Restore_Scope then
1828 Pop_Scope;
1829 end if;
1830 end Add_Internal_Interface_Entities;
1831
1832 -----------------------------------
1833 -- Analyze_Component_Declaration --
1834 -----------------------------------
1835
1836 procedure Analyze_Component_Declaration (N : Node_Id) is
1837 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1838 Id : constant Entity_Id := Defining_Identifier (N);
1839 E : constant Node_Id := Expression (N);
1840 Typ : constant Node_Id :=
1841 Subtype_Indication (Component_Definition (N));
1842 T : Entity_Id;
1843 P : Entity_Id;
1844
1845 function Contains_POC (Constr : Node_Id) return Boolean;
1846 -- Determines whether a constraint uses the discriminant of a record
1847 -- type thus becoming a per-object constraint (POC).
1848
1849 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1850 -- Typ is the type of the current component, check whether this type is
1851 -- a limited type. Used to validate declaration against that of
1852 -- enclosing record.
1853
1854 ------------------
1855 -- Contains_POC --
1856 ------------------
1857
1858 function Contains_POC (Constr : Node_Id) return Boolean is
1859 begin
1860 -- Prevent cascaded errors
1861
1862 if Error_Posted (Constr) then
1863 return False;
1864 end if;
1865
1866 case Nkind (Constr) is
1867 when N_Attribute_Reference =>
1868 return Attribute_Name (Constr) = Name_Access
1869 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1870
1871 when N_Discriminant_Association =>
1872 return Denotes_Discriminant (Expression (Constr));
1873
1874 when N_Identifier =>
1875 return Denotes_Discriminant (Constr);
1876
1877 when N_Index_Or_Discriminant_Constraint =>
1878 declare
1879 IDC : Node_Id;
1880
1881 begin
1882 IDC := First (Constraints (Constr));
1883 while Present (IDC) loop
1884
1885 -- One per-object constraint is sufficient
1886
1887 if Contains_POC (IDC) then
1888 return True;
1889 end if;
1890
1891 Next (IDC);
1892 end loop;
1893
1894 return False;
1895 end;
1896
1897 when N_Range =>
1898 return Denotes_Discriminant (Low_Bound (Constr))
1899 or else
1900 Denotes_Discriminant (High_Bound (Constr));
1901
1902 when N_Range_Constraint =>
1903 return Denotes_Discriminant (Range_Expression (Constr));
1904
1905 when others =>
1906 return False;
1907 end case;
1908 end Contains_POC;
1909
1910 ----------------------
1911 -- Is_Known_Limited --
1912 ----------------------
1913
1914 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1915 P : constant Entity_Id := Etype (Typ);
1916 R : constant Entity_Id := Root_Type (Typ);
1917
1918 begin
1919 if Is_Limited_Record (Typ) then
1920 return True;
1921
1922 -- If the root type is limited (and not a limited interface)
1923 -- so is the current type
1924
1925 elsif Is_Limited_Record (R)
1926 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1927 then
1928 return True;
1929
1930 -- Else the type may have a limited interface progenitor, but a
1931 -- limited record parent.
1932
1933 elsif R /= P and then Is_Limited_Record (P) then
1934 return True;
1935
1936 else
1937 return False;
1938 end if;
1939 end Is_Known_Limited;
1940
1941 -- Start of processing for Analyze_Component_Declaration
1942
1943 begin
1944 Generate_Definition (Id);
1945 Enter_Name (Id);
1946
1947 if Present (Typ) then
1948 T := Find_Type_Of_Object
1949 (Subtype_Indication (Component_Definition (N)), N);
1950
1951 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1952 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1953 end if;
1954
1955 -- Ada 2005 (AI-230): Access Definition case
1956
1957 else
1958 pragma Assert (Present
1959 (Access_Definition (Component_Definition (N))));
1960
1961 T := Access_Definition
1962 (Related_Nod => N,
1963 N => Access_Definition (Component_Definition (N)));
1964 Set_Is_Local_Anonymous_Access (T);
1965
1966 -- Ada 2005 (AI-254)
1967
1968 if Present (Access_To_Subprogram_Definition
1969 (Access_Definition (Component_Definition (N))))
1970 and then Protected_Present (Access_To_Subprogram_Definition
1971 (Access_Definition
1972 (Component_Definition (N))))
1973 then
1974 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1975 end if;
1976 end if;
1977
1978 -- If the subtype is a constrained subtype of the enclosing record,
1979 -- (which must have a partial view) the back-end does not properly
1980 -- handle the recursion. Rewrite the component declaration with an
1981 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1982 -- the tree directly because side effects have already been removed from
1983 -- discriminant constraints.
1984
1985 if Ekind (T) = E_Access_Subtype
1986 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1987 and then Comes_From_Source (T)
1988 and then Nkind (Parent (T)) = N_Subtype_Declaration
1989 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1990 then
1991 Rewrite
1992 (Subtype_Indication (Component_Definition (N)),
1993 New_Copy_Tree (Subtype_Indication (Parent (T))));
1994 T := Find_Type_Of_Object
1995 (Subtype_Indication (Component_Definition (N)), N);
1996 end if;
1997
1998 -- If the component declaration includes a default expression, then we
1999 -- check that the component is not of a limited type (RM 3.7(5)),
2000 -- and do the special preanalysis of the expression (see section on
2001 -- "Handling of Default and Per-Object Expressions" in the spec of
2002 -- package Sem).
2003
2004 if Present (E) then
2005 Check_SPARK_05_Restriction ("default expression is not allowed", E);
2006 Preanalyze_Default_Expression (E, T);
2007 Check_Initialization (T, E);
2008
2009 if Ada_Version >= Ada_2005
2010 and then Ekind (T) = E_Anonymous_Access_Type
2011 and then Etype (E) /= Any_Type
2012 then
2013 -- Check RM 3.9.2(9): "if the expected type for an expression is
2014 -- an anonymous access-to-specific tagged type, then the object
2015 -- designated by the expression shall not be dynamically tagged
2016 -- unless it is a controlling operand in a call on a dispatching
2017 -- operation"
2018
2019 if Is_Tagged_Type (Directly_Designated_Type (T))
2020 and then
2021 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2022 and then
2023 Ekind (Directly_Designated_Type (Etype (E))) =
2024 E_Class_Wide_Type
2025 then
2026 Error_Msg_N
2027 ("access to specific tagged type required (RM 3.9.2(9))", E);
2028 end if;
2029
2030 -- (Ada 2005: AI-230): Accessibility check for anonymous
2031 -- components
2032
2033 if Type_Access_Level (Etype (E)) >
2034 Deepest_Type_Access_Level (T)
2035 then
2036 Error_Msg_N
2037 ("expression has deeper access level than component " &
2038 "(RM 3.10.2 (12.2))", E);
2039 end if;
2040
2041 -- The initialization expression is a reference to an access
2042 -- discriminant. The type of the discriminant is always deeper
2043 -- than any access type.
2044
2045 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2046 and then Is_Entity_Name (E)
2047 and then Ekind (Entity (E)) = E_In_Parameter
2048 and then Present (Discriminal_Link (Entity (E)))
2049 then
2050 Error_Msg_N
2051 ("discriminant has deeper accessibility level than target",
2052 E);
2053 end if;
2054 end if;
2055 end if;
2056
2057 -- The parent type may be a private view with unknown discriminants,
2058 -- and thus unconstrained. Regular components must be constrained.
2059
2060 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2061 if Is_Class_Wide_Type (T) then
2062 Error_Msg_N
2063 ("class-wide subtype with unknown discriminants" &
2064 " in component declaration",
2065 Subtype_Indication (Component_Definition (N)));
2066 else
2067 Error_Msg_N
2068 ("unconstrained subtype in component declaration",
2069 Subtype_Indication (Component_Definition (N)));
2070 end if;
2071
2072 -- Components cannot be abstract, except for the special case of
2073 -- the _Parent field (case of extending an abstract tagged type)
2074
2075 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2076 Error_Msg_N ("type of a component cannot be abstract", N);
2077 end if;
2078
2079 Set_Etype (Id, T);
2080 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2081
2082 -- The component declaration may have a per-object constraint, set
2083 -- the appropriate flag in the defining identifier of the subtype.
2084
2085 if Present (Subtype_Indication (Component_Definition (N))) then
2086 declare
2087 Sindic : constant Node_Id :=
2088 Subtype_Indication (Component_Definition (N));
2089 begin
2090 if Nkind (Sindic) = N_Subtype_Indication
2091 and then Present (Constraint (Sindic))
2092 and then Contains_POC (Constraint (Sindic))
2093 then
2094 Set_Has_Per_Object_Constraint (Id);
2095 end if;
2096 end;
2097 end if;
2098
2099 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2100 -- out some static checks.
2101
2102 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2103 Null_Exclusion_Static_Checks (N);
2104 end if;
2105
2106 -- If this component is private (or depends on a private type), flag the
2107 -- record type to indicate that some operations are not available.
2108
2109 P := Private_Component (T);
2110
2111 if Present (P) then
2112
2113 -- Check for circular definitions
2114
2115 if P = Any_Type then
2116 Set_Etype (Id, Any_Type);
2117
2118 -- There is a gap in the visibility of operations only if the
2119 -- component type is not defined in the scope of the record type.
2120
2121 elsif Scope (P) = Scope (Current_Scope) then
2122 null;
2123
2124 elsif Is_Limited_Type (P) then
2125 Set_Is_Limited_Composite (Current_Scope);
2126
2127 else
2128 Set_Is_Private_Composite (Current_Scope);
2129 end if;
2130 end if;
2131
2132 if P /= Any_Type
2133 and then Is_Limited_Type (T)
2134 and then Chars (Id) /= Name_uParent
2135 and then Is_Tagged_Type (Current_Scope)
2136 then
2137 if Is_Derived_Type (Current_Scope)
2138 and then not Is_Known_Limited (Current_Scope)
2139 then
2140 Error_Msg_N
2141 ("extension of nonlimited type cannot have limited components",
2142 N);
2143
2144 if Is_Interface (Root_Type (Current_Scope)) then
2145 Error_Msg_N
2146 ("\limitedness is not inherited from limited interface", N);
2147 Error_Msg_N ("\add LIMITED to type indication", N);
2148 end if;
2149
2150 Explain_Limited_Type (T, N);
2151 Set_Etype (Id, Any_Type);
2152 Set_Is_Limited_Composite (Current_Scope, False);
2153
2154 elsif not Is_Derived_Type (Current_Scope)
2155 and then not Is_Limited_Record (Current_Scope)
2156 and then not Is_Concurrent_Type (Current_Scope)
2157 then
2158 Error_Msg_N
2159 ("nonlimited tagged type cannot have limited components", N);
2160 Explain_Limited_Type (T, N);
2161 Set_Etype (Id, Any_Type);
2162 Set_Is_Limited_Composite (Current_Scope, False);
2163 end if;
2164 end if;
2165
2166 -- If the component is an unconstrained task or protected type with
2167 -- discriminants, the component and the enclosing record are limited
2168 -- and the component is constrained by its default values. Compute
2169 -- its actual subtype, else it may be allocated the maximum size by
2170 -- the backend, and possibly overflow.
2171
2172 if Is_Concurrent_Type (T)
2173 and then not Is_Constrained (T)
2174 and then Has_Discriminants (T)
2175 and then not Has_Discriminants (Current_Scope)
2176 then
2177 declare
2178 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2179
2180 begin
2181 Set_Etype (Id, Act_T);
2182
2183 -- Rewrite component definition to use the constrained subtype
2184
2185 Rewrite (Component_Definition (N),
2186 Make_Component_Definition (Loc,
2187 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2188 end;
2189 end if;
2190
2191 Set_Original_Record_Component (Id, Id);
2192
2193 if Has_Aspects (N) then
2194 Analyze_Aspect_Specifications (N, Id);
2195 end if;
2196
2197 Analyze_Dimension (N);
2198 end Analyze_Component_Declaration;
2199
2200 --------------------------
2201 -- Analyze_Declarations --
2202 --------------------------
2203
2204 procedure Analyze_Declarations (L : List_Id) is
2205 Decl : Node_Id;
2206
2207 procedure Adjust_Decl;
2208 -- Adjust Decl not to include implicit label declarations, since these
2209 -- have strange Sloc values that result in elaboration check problems.
2210 -- (They have the sloc of the label as found in the source, and that
2211 -- is ahead of the current declarative part).
2212
2213 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2214 -- Create the subprogram bodies which verify the run-time semantics of
2215 -- the pragmas listed below for each elibigle type found in declarative
2216 -- list Decls. The pragmas are:
2217 --
2218 -- Default_Initial_Condition
2219 -- Invariant
2220 -- Type_Invariant
2221 --
2222 -- Context denotes the owner of the declarative list.
2223
2224 procedure Check_Entry_Contracts;
2225 -- Perform a preanalysis of the pre- and postconditions of an entry
2226 -- declaration. This must be done before full resolution and creation
2227 -- of the parameter block, etc. to catch illegal uses within the
2228 -- contract expression. Full analysis of the expression is done when
2229 -- the contract is processed.
2230
2231 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2232 -- Check if a nested package has entities within it that rely on library
2233 -- level private types where the full view has not been completed for
2234 -- the purposes of checking if it is acceptable to freeze an expression
2235 -- function at the point of declaration.
2236
2237 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2238 -- Determine whether Body_Decl denotes the body of a late controlled
2239 -- primitive (either Initialize, Adjust or Finalize). If this is the
2240 -- case, add a proper spec if the body lacks one. The spec is inserted
2241 -- before Body_Decl and immediately analyzed.
2242
2243 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2244 -- Spec_Id is the entity of a package that may define abstract states,
2245 -- and in the case of a child unit, whose ancestors may define abstract
2246 -- states. If the states have partial visible refinement, remove the
2247 -- partial visibility of each constituent at the end of the package
2248 -- spec and body declarations.
2249
2250 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2251 -- Spec_Id is the entity of a package that may define abstract states.
2252 -- If the states have visible refinement, remove the visibility of each
2253 -- constituent at the end of the package body declaration.
2254
2255 procedure Resolve_Aspects;
2256 -- Utility to resolve the expressions of aspects at the end of a list of
2257 -- declarations, or before a declaration that freezes previous entities,
2258 -- such as in a subprogram body.
2259
2260 -----------------
2261 -- Adjust_Decl --
2262 -----------------
2263
2264 procedure Adjust_Decl is
2265 begin
2266 while Present (Prev (Decl))
2267 and then Nkind (Decl) = N_Implicit_Label_Declaration
2268 loop
2269 Prev (Decl);
2270 end loop;
2271 end Adjust_Decl;
2272
2273 ----------------------------
2274 -- Build_Assertion_Bodies --
2275 ----------------------------
2276
2277 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2278 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2279 -- Create the subprogram bodies which verify the run-time semantics
2280 -- of the pragmas listed below for type Typ. The pragmas are:
2281 --
2282 -- Default_Initial_Condition
2283 -- Invariant
2284 -- Type_Invariant
2285
2286 -------------------------------------
2287 -- Build_Assertion_Bodies_For_Type --
2288 -------------------------------------
2289
2290 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2291 begin
2292 -- Preanalyze and resolve the Default_Initial_Condition assertion
2293 -- expression at the end of the declarations to catch any errors.
2294
2295 if Has_DIC (Typ) then
2296 Build_DIC_Procedure_Body (Typ);
2297 end if;
2298
2299 if Nkind (Context) = N_Package_Specification then
2300
2301 -- Preanalyze and resolve the class-wide invariants of an
2302 -- interface at the end of whichever declarative part has the
2303 -- interface type. Note that an interface may be declared in
2304 -- any non-package declarative part, but reaching the end of
2305 -- such a declarative part will always freeze the type and
2306 -- generate the invariant procedure (see Freeze_Type).
2307
2308 if Is_Interface (Typ) then
2309
2310 -- Interfaces are treated as the partial view of a private
2311 -- type, in order to achieve uniformity with the general
2312 -- case. As a result, an interface receives only a "partial"
2313 -- invariant procedure, which is never called.
2314
2315 if Has_Own_Invariants (Typ) then
2316 Build_Invariant_Procedure_Body
2317 (Typ => Typ,
2318 Partial_Invariant => True);
2319 end if;
2320
2321 -- Preanalyze and resolve the invariants of a private type
2322 -- at the end of the visible declarations to catch potential
2323 -- errors. Inherited class-wide invariants are not included
2324 -- because they have already been resolved.
2325
2326 elsif Decls = Visible_Declarations (Context)
2327 and then Ekind_In (Typ, E_Limited_Private_Type,
2328 E_Private_Type,
2329 E_Record_Type_With_Private)
2330 and then Has_Own_Invariants (Typ)
2331 then
2332 Build_Invariant_Procedure_Body
2333 (Typ => Typ,
2334 Partial_Invariant => True);
2335
2336 -- Preanalyze and resolve the invariants of a private type's
2337 -- full view at the end of the private declarations to catch
2338 -- potential errors.
2339
2340 elsif Decls = Private_Declarations (Context)
2341 and then not Is_Private_Type (Typ)
2342 and then Has_Private_Declaration (Typ)
2343 and then Has_Invariants (Typ)
2344 then
2345 Build_Invariant_Procedure_Body (Typ);
2346 end if;
2347 end if;
2348 end Build_Assertion_Bodies_For_Type;
2349
2350 -- Local variables
2351
2352 Decl : Node_Id;
2353 Decl_Id : Entity_Id;
2354
2355 -- Start of processing for Build_Assertion_Bodies
2356
2357 begin
2358 Decl := First (Decls);
2359 while Present (Decl) loop
2360 if Is_Declaration (Decl) then
2361 Decl_Id := Defining_Entity (Decl);
2362
2363 if Is_Type (Decl_Id) then
2364 Build_Assertion_Bodies_For_Type (Decl_Id);
2365 end if;
2366 end if;
2367
2368 Next (Decl);
2369 end loop;
2370 end Build_Assertion_Bodies;
2371
2372 ---------------------------
2373 -- Check_Entry_Contracts --
2374 ---------------------------
2375
2376 procedure Check_Entry_Contracts is
2377 ASN : Node_Id;
2378 Ent : Entity_Id;
2379 Exp : Node_Id;
2380
2381 begin
2382 Ent := First_Entity (Current_Scope);
2383 while Present (Ent) loop
2384
2385 -- This only concerns entries with pre/postconditions
2386
2387 if Ekind (Ent) = E_Entry
2388 and then Present (Contract (Ent))
2389 and then Present (Pre_Post_Conditions (Contract (Ent)))
2390 then
2391 ASN := Pre_Post_Conditions (Contract (Ent));
2392 Push_Scope (Ent);
2393 Install_Formals (Ent);
2394
2395 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2396 -- is performed on a copy of the pragma expression, to prevent
2397 -- modifying the original expression.
2398
2399 while Present (ASN) loop
2400 if Nkind (ASN) = N_Pragma then
2401 Exp :=
2402 New_Copy_Tree
2403 (Expression
2404 (First (Pragma_Argument_Associations (ASN))));
2405 Set_Parent (Exp, ASN);
2406
2407 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2408 end if;
2409
2410 ASN := Next_Pragma (ASN);
2411 end loop;
2412
2413 End_Scope;
2414 end if;
2415
2416 Next_Entity (Ent);
2417 end loop;
2418 end Check_Entry_Contracts;
2419
2420 ----------------------------------
2421 -- Contains_Lib_Incomplete_Type --
2422 ----------------------------------
2423
2424 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2425 Curr : Entity_Id;
2426
2427 begin
2428 -- Avoid looking through scopes that do not meet the precondition of
2429 -- Pkg not being within a library unit spec.
2430
2431 if not Is_Compilation_Unit (Pkg)
2432 and then not Is_Generic_Instance (Pkg)
2433 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2434 then
2435 -- Loop through all entities in the current scope to identify
2436 -- an entity that depends on a private type.
2437
2438 Curr := First_Entity (Pkg);
2439 loop
2440 if Nkind (Curr) in N_Entity
2441 and then Depends_On_Private (Curr)
2442 then
2443 return True;
2444 end if;
2445
2446 exit when Last_Entity (Current_Scope) = Curr;
2447 Curr := Next_Entity (Curr);
2448 end loop;
2449 end if;
2450
2451 return False;
2452 end Contains_Lib_Incomplete_Type;
2453
2454 --------------------------------------
2455 -- Handle_Late_Controlled_Primitive --
2456 --------------------------------------
2457
2458 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2459 Body_Spec : constant Node_Id := Specification (Body_Decl);
2460 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2461 Loc : constant Source_Ptr := Sloc (Body_Id);
2462 Params : constant List_Id :=
2463 Parameter_Specifications (Body_Spec);
2464 Spec : Node_Id;
2465 Spec_Id : Entity_Id;
2466 Typ : Node_Id;
2467
2468 begin
2469 -- Consider only procedure bodies whose name matches one of the three
2470 -- controlled primitives.
2471
2472 if Nkind (Body_Spec) /= N_Procedure_Specification
2473 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2474 Name_Finalize,
2475 Name_Initialize)
2476 then
2477 return;
2478
2479 -- A controlled primitive must have exactly one formal which is not
2480 -- an anonymous access type.
2481
2482 elsif List_Length (Params) /= 1 then
2483 return;
2484 end if;
2485
2486 Typ := Parameter_Type (First (Params));
2487
2488 if Nkind (Typ) = N_Access_Definition then
2489 return;
2490 end if;
2491
2492 Find_Type (Typ);
2493
2494 -- The type of the formal must be derived from [Limited_]Controlled
2495
2496 if not Is_Controlled (Entity (Typ)) then
2497 return;
2498 end if;
2499
2500 -- Check whether a specification exists for this body. We do not
2501 -- analyze the spec of the body in full, because it will be analyzed
2502 -- again when the body is properly analyzed, and we cannot create
2503 -- duplicate entries in the formals chain. We look for an explicit
2504 -- specification because the body may be an overriding operation and
2505 -- an inherited spec may be present.
2506
2507 Spec_Id := Current_Entity (Body_Id);
2508
2509 while Present (Spec_Id) loop
2510 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2511 and then Scope (Spec_Id) = Current_Scope
2512 and then Present (First_Formal (Spec_Id))
2513 and then No (Next_Formal (First_Formal (Spec_Id)))
2514 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2515 and then Comes_From_Source (Spec_Id)
2516 then
2517 return;
2518 end if;
2519
2520 Spec_Id := Homonym (Spec_Id);
2521 end loop;
2522
2523 -- At this point the body is known to be a late controlled primitive.
2524 -- Generate a matching spec and insert it before the body. Note the
2525 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2526 -- tree in this case.
2527
2528 Spec := Copy_Separate_Tree (Body_Spec);
2529
2530 -- Ensure that the subprogram declaration does not inherit the null
2531 -- indicator from the body as we now have a proper spec/body pair.
2532
2533 Set_Null_Present (Spec, False);
2534
2535 -- Ensure that the freeze node is inserted after the declaration of
2536 -- the primitive since its expansion will freeze the primitive.
2537
2538 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2539
2540 Insert_Before_And_Analyze (Body_Decl, Decl);
2541 end Handle_Late_Controlled_Primitive;
2542
2543 ----------------------------------------
2544 -- Remove_Partial_Visible_Refinements --
2545 ----------------------------------------
2546
2547 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2548 State_Elmt : Elmt_Id;
2549 begin
2550 if Present (Abstract_States (Spec_Id)) then
2551 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2552 while Present (State_Elmt) loop
2553 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2554 Next_Elmt (State_Elmt);
2555 end loop;
2556 end if;
2557
2558 -- For a child unit, also hide the partial state refinement from
2559 -- ancestor packages.
2560
2561 if Is_Child_Unit (Spec_Id) then
2562 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2563 end if;
2564 end Remove_Partial_Visible_Refinements;
2565
2566 --------------------------------
2567 -- Remove_Visible_Refinements --
2568 --------------------------------
2569
2570 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2571 State_Elmt : Elmt_Id;
2572 begin
2573 if Present (Abstract_States (Spec_Id)) then
2574 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2575 while Present (State_Elmt) loop
2576 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2577 Next_Elmt (State_Elmt);
2578 end loop;
2579 end if;
2580 end Remove_Visible_Refinements;
2581
2582 ---------------------
2583 -- Resolve_Aspects --
2584 ---------------------
2585
2586 procedure Resolve_Aspects is
2587 E : Entity_Id;
2588
2589 begin
2590 E := First_Entity (Current_Scope);
2591 while Present (E) loop
2592 Resolve_Aspect_Expressions (E);
2593 Next_Entity (E);
2594 end loop;
2595 end Resolve_Aspects;
2596
2597 -- Local variables
2598
2599 Context : Node_Id := Empty;
2600 Freeze_From : Entity_Id := Empty;
2601 Next_Decl : Node_Id;
2602
2603 Body_Seen : Boolean := False;
2604 -- Flag set when the first body [stub] is encountered
2605
2606 -- Start of processing for Analyze_Declarations
2607
2608 begin
2609 if Restriction_Check_Required (SPARK_05) then
2610 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2611 end if;
2612
2613 Decl := First (L);
2614 while Present (Decl) loop
2615
2616 -- Package spec cannot contain a package declaration in SPARK
2617
2618 if Nkind (Decl) = N_Package_Declaration
2619 and then Nkind (Parent (L)) = N_Package_Specification
2620 then
2621 Check_SPARK_05_Restriction
2622 ("package specification cannot contain a package declaration",
2623 Decl);
2624 end if;
2625
2626 -- Complete analysis of declaration
2627
2628 Analyze (Decl);
2629 Next_Decl := Next (Decl);
2630
2631 if No (Freeze_From) then
2632 Freeze_From := First_Entity (Current_Scope);
2633 end if;
2634
2635 -- At the end of a declarative part, freeze remaining entities
2636 -- declared in it. The end of the visible declarations of package
2637 -- specification is not the end of a declarative part if private
2638 -- declarations are present. The end of a package declaration is a
2639 -- freezing point only if it a library package. A task definition or
2640 -- protected type definition is not a freeze point either. Finally,
2641 -- we do not freeze entities in generic scopes, because there is no
2642 -- code generated for them and freeze nodes will be generated for
2643 -- the instance.
2644
2645 -- The end of a package instantiation is not a freeze point, but
2646 -- for now we make it one, because the generic body is inserted
2647 -- (currently) immediately after. Generic instantiations will not
2648 -- be a freeze point once delayed freezing of bodies is implemented.
2649 -- (This is needed in any case for early instantiations ???).
2650
2651 if No (Next_Decl) then
2652 if Nkind (Parent (L)) = N_Component_List then
2653 null;
2654
2655 elsif Nkind_In (Parent (L), N_Protected_Definition,
2656 N_Task_Definition)
2657 then
2658 Check_Entry_Contracts;
2659
2660 elsif Nkind (Parent (L)) /= N_Package_Specification then
2661 if Nkind (Parent (L)) = N_Package_Body then
2662 Freeze_From := First_Entity (Current_Scope);
2663 end if;
2664
2665 -- There may have been several freezing points previously,
2666 -- for example object declarations or subprogram bodies, but
2667 -- at the end of a declarative part we check freezing from
2668 -- the beginning, even though entities may already be frozen,
2669 -- in order to perform visibility checks on delayed aspects.
2670
2671 Adjust_Decl;
2672
2673 -- If the current scope is a generic subprogram body. Skip the
2674 -- generic formal parameters that are not frozen here.
2675
2676 if Is_Subprogram (Current_Scope)
2677 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2678 N_Generic_Subprogram_Declaration
2679 and then Present (First_Entity (Current_Scope))
2680 then
2681 while Is_Generic_Formal (Freeze_From) loop
2682 Freeze_From := Next_Entity (Freeze_From);
2683 end loop;
2684
2685 Freeze_All (Freeze_From, Decl);
2686 Freeze_From := Last_Entity (Current_Scope);
2687
2688 else
2689 -- For declarations in a subprogram body there is no issue
2690 -- with name resolution in aspect specifications, but in
2691 -- ASIS mode we need to preanalyze aspect specifications
2692 -- that may otherwise only be analyzed during expansion
2693 -- (e.g. during generation of a related subprogram).
2694
2695 if ASIS_Mode then
2696 Resolve_Aspects;
2697 end if;
2698
2699 Freeze_All (First_Entity (Current_Scope), Decl);
2700 Freeze_From := Last_Entity (Current_Scope);
2701 end if;
2702
2703 -- Current scope is a package specification
2704
2705 elsif Scope (Current_Scope) /= Standard_Standard
2706 and then not Is_Child_Unit (Current_Scope)
2707 and then No (Generic_Parent (Parent (L)))
2708 then
2709 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2710 -- resolved at the end of the immediately enclosing declaration
2711 -- list (AI05-0183-1).
2712
2713 Resolve_Aspects;
2714
2715 elsif L /= Visible_Declarations (Parent (L))
2716 or else No (Private_Declarations (Parent (L)))
2717 or else Is_Empty_List (Private_Declarations (Parent (L)))
2718 then
2719 Adjust_Decl;
2720
2721 -- End of a package declaration
2722
2723 -- In compilation mode the expansion of freeze node takes care
2724 -- of resolving expressions of all aspects in the list. In ASIS
2725 -- mode this must be done explicitly.
2726
2727 if ASIS_Mode
2728 and then Scope (Current_Scope) = Standard_Standard
2729 then
2730 Resolve_Aspects;
2731 end if;
2732
2733 -- This is a freeze point because it is the end of a
2734 -- compilation unit.
2735
2736 Freeze_All (First_Entity (Current_Scope), Decl);
2737 Freeze_From := Last_Entity (Current_Scope);
2738
2739 -- At the end of the visible declarations the expressions in
2740 -- aspects of all entities declared so far must be resolved.
2741 -- The entities themselves might be frozen later, and the
2742 -- generated pragmas and attribute definition clauses analyzed
2743 -- in full at that point, but name resolution must take place
2744 -- now.
2745 -- In addition to being the proper semantics, this is mandatory
2746 -- within generic units, because global name capture requires
2747 -- those expressions to be analyzed, given that the generated
2748 -- pragmas do not appear in the original generic tree.
2749
2750 elsif Serious_Errors_Detected = 0 then
2751 Resolve_Aspects;
2752 end if;
2753
2754 -- If next node is a body then freeze all types before the body.
2755 -- An exception occurs for some expander-generated bodies. If these
2756 -- are generated at places where in general language rules would not
2757 -- allow a freeze point, then we assume that the expander has
2758 -- explicitly checked that all required types are properly frozen,
2759 -- and we do not cause general freezing here. This special circuit
2760 -- is used when the encountered body is marked as having already
2761 -- been analyzed.
2762
2763 -- In all other cases (bodies that come from source, and expander
2764 -- generated bodies that have not been analyzed yet), freeze all
2765 -- types now. Note that in the latter case, the expander must take
2766 -- care to attach the bodies at a proper place in the tree so as to
2767 -- not cause unwanted freezing at that point.
2768
2769 -- It is also necessary to check for a case where both an expression
2770 -- function is used and the current scope depends on an incomplete
2771 -- private type from a library unit, otherwise premature freezing of
2772 -- the private type will occur.
2773
2774 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2775 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2776 or else not Was_Expression_Function (Next_Decl))
2777 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2778 and then not Contains_Lib_Incomplete_Type
2779 (Current_Scope)))
2780 then
2781 -- When a controlled type is frozen, the expander generates stream
2782 -- and controlled-type support routines. If the freeze is caused
2783 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2784 -- expander will end up using the wrong version of these routines,
2785 -- as the body has not been processed yet. To remedy this, detect
2786 -- a late controlled primitive and create a proper spec for it.
2787 -- This ensures that the primitive will override its inherited
2788 -- counterpart before the freeze takes place.
2789
2790 -- If the declaration we just processed is a body, do not attempt
2791 -- to examine Next_Decl as the late primitive idiom can only apply
2792 -- to the first encountered body.
2793
2794 -- The spec of the late primitive is not generated in ASIS mode to
2795 -- ensure a consistent list of primitives that indicates the true
2796 -- semantic structure of the program (which is not relevant when
2797 -- generating executable code).
2798
2799 -- ??? A cleaner approach may be possible and/or this solution
2800 -- could be extended to general-purpose late primitives, TBD.
2801
2802 if not ASIS_Mode
2803 and then not Body_Seen
2804 and then not Is_Body (Decl)
2805 then
2806 Body_Seen := True;
2807
2808 if Nkind (Next_Decl) = N_Subprogram_Body then
2809 Handle_Late_Controlled_Primitive (Next_Decl);
2810 end if;
2811
2812 else
2813 -- In ASIS mode, if the next declaration is a body, complete
2814 -- the analysis of declarations so far.
2815
2816 Resolve_Aspects;
2817 end if;
2818
2819 Adjust_Decl;
2820
2821 -- The generated body of an expression function does not freeze,
2822 -- unless it is a completion, in which case only the expression
2823 -- itself freezes. This is handled when the body itself is
2824 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2825
2826 Freeze_All (Freeze_From, Decl);
2827 Freeze_From := Last_Entity (Current_Scope);
2828 end if;
2829
2830 Decl := Next_Decl;
2831 end loop;
2832
2833 -- Post-freezing actions
2834
2835 if Present (L) then
2836 Context := Parent (L);
2837
2838 -- Certain contract annocations have forward visibility semantics and
2839 -- must be analyzed after all declarative items have been processed.
2840 -- This timing ensures that entities referenced by such contracts are
2841 -- visible.
2842
2843 -- Analyze the contract of an immediately enclosing package spec or
2844 -- body first because other contracts may depend on its information.
2845
2846 if Nkind (Context) = N_Package_Body then
2847 Analyze_Package_Body_Contract (Defining_Entity (Context));
2848
2849 elsif Nkind (Context) = N_Package_Specification then
2850 Analyze_Package_Contract (Defining_Entity (Context));
2851 end if;
2852
2853 -- Analyze the contracts of various constructs in the declarative
2854 -- list.
2855
2856 Analyze_Contracts (L);
2857
2858 if Nkind (Context) = N_Package_Body then
2859
2860 -- Ensure that all abstract states and objects declared in the
2861 -- state space of a package body are utilized as constituents.
2862
2863 Check_Unused_Body_States (Defining_Entity (Context));
2864
2865 -- State refinements are visible up to the end of the package body
2866 -- declarations. Hide the state refinements from visibility to
2867 -- restore the original state conditions.
2868
2869 Remove_Visible_Refinements (Corresponding_Spec (Context));
2870 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2871
2872 elsif Nkind (Context) = N_Package_Specification then
2873
2874 -- Partial state refinements are visible up to the end of the
2875 -- package spec declarations. Hide the partial state refinements
2876 -- from visibility to restore the original state conditions.
2877
2878 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2879 end if;
2880
2881 -- Verify that all abstract states found in any package declared in
2882 -- the input declarative list have proper refinements. The check is
2883 -- performed only when the context denotes a block, entry, package,
2884 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2885
2886 Check_State_Refinements (Context);
2887
2888 -- Create the subprogram bodies which verify the run-time semantics
2889 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2890 -- types within the current declarative list. This ensures that all
2891 -- assertion expressions are preanalyzed and resolved at the end of
2892 -- the declarative part. Note that the resolution happens even when
2893 -- freezing does not take place.
2894
2895 Build_Assertion_Bodies (L, Context);
2896 end if;
2897 end Analyze_Declarations;
2898
2899 -----------------------------------
2900 -- Analyze_Full_Type_Declaration --
2901 -----------------------------------
2902
2903 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2904 Def : constant Node_Id := Type_Definition (N);
2905 Def_Id : constant Entity_Id := Defining_Identifier (N);
2906 T : Entity_Id;
2907 Prev : Entity_Id;
2908
2909 Is_Remote : constant Boolean :=
2910 (Is_Remote_Types (Current_Scope)
2911 or else Is_Remote_Call_Interface (Current_Scope))
2912 and then not (In_Private_Part (Current_Scope)
2913 or else In_Package_Body (Current_Scope));
2914
2915 procedure Check_Nonoverridable_Aspects;
2916 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2917 -- be overridden, and can only be confirmed on derivation.
2918
2919 procedure Check_Ops_From_Incomplete_Type;
2920 -- If there is a tagged incomplete partial view of the type, traverse
2921 -- the primitives of the incomplete view and change the type of any
2922 -- controlling formals and result to indicate the full view. The
2923 -- primitives will be added to the full type's primitive operations
2924 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2925 -- is called from Process_Incomplete_Dependents).
2926
2927 ----------------------------------
2928 -- Check_Nonoverridable_Aspects --
2929 ----------------------------------
2930
2931 procedure Check_Nonoverridable_Aspects is
2932 function Get_Aspect_Spec
2933 (Specs : List_Id;
2934 Aspect_Name : Name_Id) return Node_Id;
2935 -- Check whether a list of aspect specifications includes an entry
2936 -- for a specific aspect. The list is either that of a partial or
2937 -- a full view.
2938
2939 ---------------------
2940 -- Get_Aspect_Spec --
2941 ---------------------
2942
2943 function Get_Aspect_Spec
2944 (Specs : List_Id;
2945 Aspect_Name : Name_Id) return Node_Id
2946 is
2947 Spec : Node_Id;
2948
2949 begin
2950 Spec := First (Specs);
2951 while Present (Spec) loop
2952 if Chars (Identifier (Spec)) = Aspect_Name then
2953 return Spec;
2954 end if;
2955 Next (Spec);
2956 end loop;
2957
2958 return Empty;
2959 end Get_Aspect_Spec;
2960
2961 -- Local variables
2962
2963 Prev_Aspects : constant List_Id :=
2964 Aspect_Specifications (Parent (Def_Id));
2965 Par_Type : Entity_Id;
2966 Prev_Aspect : Node_Id;
2967
2968 -- Start of processing for Check_Nonoverridable_Aspects
2969
2970 begin
2971 -- Get parent type of derived type. Note that Prev is the entity in
2972 -- the partial declaration, but its contents are now those of full
2973 -- view, while Def_Id reflects the partial view.
2974
2975 if Is_Private_Type (Def_Id) then
2976 Par_Type := Etype (Full_View (Def_Id));
2977 else
2978 Par_Type := Etype (Def_Id);
2979 end if;
2980
2981 -- If there is an inherited Implicit_Dereference, verify that it is
2982 -- made explicit in the partial view.
2983
2984 if Has_Discriminants (Base_Type (Par_Type))
2985 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2986 and then Present (Discriminant_Specifications (Parent (Prev)))
2987 and then Present (Get_Reference_Discriminant (Par_Type))
2988 then
2989 Prev_Aspect :=
2990 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2991
2992 if No (Prev_Aspect)
2993 and then Present
2994 (Discriminant_Specifications
2995 (Original_Node (Parent (Prev))))
2996 then
2997 Error_Msg_N
2998 ("type does not inherit implicit dereference", Prev);
2999
3000 else
3001 -- If one of the views has the aspect specified, verify that it
3002 -- is consistent with that of the parent.
3003
3004 declare
3005 Par_Discr : constant Entity_Id :=
3006 Get_Reference_Discriminant (Par_Type);
3007 Cur_Discr : constant Entity_Id :=
3008 Get_Reference_Discriminant (Prev);
3009
3010 begin
3011 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3012 Error_Msg_N ("aspect incosistent with that of parent", N);
3013 end if;
3014
3015 -- Check that specification in partial view matches the
3016 -- inherited aspect. Compare names directly because aspect
3017 -- expression may not be analyzed.
3018
3019 if Present (Prev_Aspect)
3020 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3021 and then Chars (Expression (Prev_Aspect)) /=
3022 Chars (Cur_Discr)
3023 then
3024 Error_Msg_N
3025 ("aspect incosistent with that of parent", N);
3026 end if;
3027 end;
3028 end if;
3029 end if;
3030
3031 -- TBD : other nonoverridable aspects.
3032 end Check_Nonoverridable_Aspects;
3033
3034 ------------------------------------
3035 -- Check_Ops_From_Incomplete_Type --
3036 ------------------------------------
3037
3038 procedure Check_Ops_From_Incomplete_Type is
3039 Elmt : Elmt_Id;
3040 Formal : Entity_Id;
3041 Op : Entity_Id;
3042
3043 begin
3044 if Prev /= T
3045 and then Ekind (Prev) = E_Incomplete_Type
3046 and then Is_Tagged_Type (Prev)
3047 and then Is_Tagged_Type (T)
3048 then
3049 Elmt := First_Elmt (Primitive_Operations (Prev));
3050 while Present (Elmt) loop
3051 Op := Node (Elmt);
3052
3053 Formal := First_Formal (Op);
3054 while Present (Formal) loop
3055 if Etype (Formal) = Prev then
3056 Set_Etype (Formal, T);
3057 end if;
3058
3059 Next_Formal (Formal);
3060 end loop;
3061
3062 if Etype (Op) = Prev then
3063 Set_Etype (Op, T);
3064 end if;
3065
3066 Next_Elmt (Elmt);
3067 end loop;
3068 end if;
3069 end Check_Ops_From_Incomplete_Type;
3070
3071 -- Start of processing for Analyze_Full_Type_Declaration
3072
3073 begin
3074 Prev := Find_Type_Name (N);
3075
3076 -- The full view, if present, now points to the current type. If there
3077 -- is an incomplete partial view, set a link to it, to simplify the
3078 -- retrieval of primitive operations of the type.
3079
3080 -- Ada 2005 (AI-50217): If the type was previously decorated when
3081 -- imported through a LIMITED WITH clause, it appears as incomplete
3082 -- but has no full view.
3083
3084 if Ekind (Prev) = E_Incomplete_Type
3085 and then Present (Full_View (Prev))
3086 then
3087 T := Full_View (Prev);
3088 Set_Incomplete_View (N, Parent (Prev));
3089 else
3090 T := Prev;
3091 end if;
3092
3093 Set_Is_Pure (T, Is_Pure (Current_Scope));
3094
3095 -- We set the flag Is_First_Subtype here. It is needed to set the
3096 -- corresponding flag for the Implicit class-wide-type created
3097 -- during tagged types processing.
3098
3099 Set_Is_First_Subtype (T, True);
3100
3101 -- Only composite types other than array types are allowed to have
3102 -- discriminants.
3103
3104 case Nkind (Def) is
3105
3106 -- For derived types, the rule will be checked once we've figured
3107 -- out the parent type.
3108
3109 when N_Derived_Type_Definition =>
3110 null;
3111
3112 -- For record types, discriminants are allowed, unless we are in
3113 -- SPARK.
3114
3115 when N_Record_Definition =>
3116 if Present (Discriminant_Specifications (N)) then
3117 Check_SPARK_05_Restriction
3118 ("discriminant type is not allowed",
3119 Defining_Identifier
3120 (First (Discriminant_Specifications (N))));
3121 end if;
3122
3123 when others =>
3124 if Present (Discriminant_Specifications (N)) then
3125 Error_Msg_N
3126 ("elementary or array type cannot have discriminants",
3127 Defining_Identifier
3128 (First (Discriminant_Specifications (N))));
3129 end if;
3130 end case;
3131
3132 -- Elaborate the type definition according to kind, and generate
3133 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3134 -- already done (this happens during the reanalysis that follows a call
3135 -- to the high level optimizer).
3136
3137 if not Analyzed (T) then
3138 Set_Analyzed (T);
3139
3140 -- Set the SPARK mode from the current context
3141
3142 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3143 Set_SPARK_Pragma_Inherited (T);
3144
3145 case Nkind (Def) is
3146 when N_Access_To_Subprogram_Definition =>
3147 Access_Subprogram_Declaration (T, Def);
3148
3149 -- If this is a remote access to subprogram, we must create the
3150 -- equivalent fat pointer type, and related subprograms.
3151
3152 if Is_Remote then
3153 Process_Remote_AST_Declaration (N);
3154 end if;
3155
3156 -- Validate categorization rule against access type declaration
3157 -- usually a violation in Pure unit, Shared_Passive unit.
3158
3159 Validate_Access_Type_Declaration (T, N);
3160
3161 when N_Access_To_Object_Definition =>
3162 Access_Type_Declaration (T, Def);
3163
3164 -- Validate categorization rule against access type declaration
3165 -- usually a violation in Pure unit, Shared_Passive unit.
3166
3167 Validate_Access_Type_Declaration (T, N);
3168
3169 -- If we are in a Remote_Call_Interface package and define a
3170 -- RACW, then calling stubs and specific stream attributes
3171 -- must be added.
3172
3173 if Is_Remote
3174 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3175 then
3176 Add_RACW_Features (Def_Id);
3177 end if;
3178
3179 when N_Array_Type_Definition =>
3180 Array_Type_Declaration (T, Def);
3181
3182 when N_Derived_Type_Definition =>
3183 Derived_Type_Declaration (T, N, T /= Def_Id);
3184
3185 -- Inherit predicates from parent, and protect against illegal
3186 -- derivations.
3187
3188 if Is_Type (T) and then Has_Predicates (T) then
3189 Set_Has_Predicates (Def_Id);
3190 end if;
3191
3192 -- Save the scenario for examination by the ABE Processing
3193 -- phase.
3194
3195 Record_Elaboration_Scenario (N);
3196
3197 when N_Enumeration_Type_Definition =>
3198 Enumeration_Type_Declaration (T, Def);
3199
3200 when N_Floating_Point_Definition =>
3201 Floating_Point_Type_Declaration (T, Def);
3202
3203 when N_Decimal_Fixed_Point_Definition =>
3204 Decimal_Fixed_Point_Type_Declaration (T, Def);
3205
3206 when N_Ordinary_Fixed_Point_Definition =>
3207 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3208
3209 when N_Signed_Integer_Type_Definition =>
3210 Signed_Integer_Type_Declaration (T, Def);
3211
3212 when N_Modular_Type_Definition =>
3213 Modular_Type_Declaration (T, Def);
3214
3215 when N_Record_Definition =>
3216 Record_Type_Declaration (T, N, Prev);
3217
3218 -- If declaration has a parse error, nothing to elaborate.
3219
3220 when N_Error =>
3221 null;
3222
3223 when others =>
3224 raise Program_Error;
3225 end case;
3226 end if;
3227
3228 if Etype (T) = Any_Type then
3229 return;
3230 end if;
3231
3232 -- Controlled type is not allowed in SPARK
3233
3234 if Is_Visibly_Controlled (T) then
3235 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3236 end if;
3237
3238 -- Some common processing for all types
3239
3240 Set_Depends_On_Private (T, Has_Private_Component (T));
3241 Check_Ops_From_Incomplete_Type;
3242
3243 -- Both the declared entity, and its anonymous base type if one was
3244 -- created, need freeze nodes allocated.
3245
3246 declare
3247 B : constant Entity_Id := Base_Type (T);
3248
3249 begin
3250 -- In the case where the base type differs from the first subtype, we
3251 -- pre-allocate a freeze node, and set the proper link to the first
3252 -- subtype. Freeze_Entity will use this preallocated freeze node when
3253 -- it freezes the entity.
3254
3255 -- This does not apply if the base type is a generic type, whose
3256 -- declaration is independent of the current derived definition.
3257
3258 if B /= T and then not Is_Generic_Type (B) then
3259 Ensure_Freeze_Node (B);
3260 Set_First_Subtype_Link (Freeze_Node (B), T);
3261 end if;
3262
3263 -- A type that is imported through a limited_with clause cannot
3264 -- generate any code, and thus need not be frozen. However, an access
3265 -- type with an imported designated type needs a finalization list,
3266 -- which may be referenced in some other package that has non-limited
3267 -- visibility on the designated type. Thus we must create the
3268 -- finalization list at the point the access type is frozen, to
3269 -- prevent unsatisfied references at link time.
3270
3271 if not From_Limited_With (T) or else Is_Access_Type (T) then
3272 Set_Has_Delayed_Freeze (T);
3273 end if;
3274 end;
3275
3276 -- Case where T is the full declaration of some private type which has
3277 -- been swapped in Defining_Identifier (N).
3278
3279 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3280 Process_Full_View (N, T, Def_Id);
3281
3282 -- Record the reference. The form of this is a little strange, since
3283 -- the full declaration has been swapped in. So the first parameter
3284 -- here represents the entity to which a reference is made which is
3285 -- the "real" entity, i.e. the one swapped in, and the second
3286 -- parameter provides the reference location.
3287
3288 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3289 -- since we don't want a complaint about the full type being an
3290 -- unwanted reference to the private type
3291
3292 declare
3293 B : constant Boolean := Has_Pragma_Unreferenced (T);
3294 begin
3295 Set_Has_Pragma_Unreferenced (T, False);
3296 Generate_Reference (T, T, 'c');
3297 Set_Has_Pragma_Unreferenced (T, B);
3298 end;
3299
3300 Set_Completion_Referenced (Def_Id);
3301
3302 -- For completion of incomplete type, process incomplete dependents
3303 -- and always mark the full type as referenced (it is the incomplete
3304 -- type that we get for any real reference).
3305
3306 elsif Ekind (Prev) = E_Incomplete_Type then
3307 Process_Incomplete_Dependents (N, T, Prev);
3308 Generate_Reference (Prev, Def_Id, 'c');
3309 Set_Completion_Referenced (Def_Id);
3310
3311 -- If not private type or incomplete type completion, this is a real
3312 -- definition of a new entity, so record it.
3313
3314 else
3315 Generate_Definition (Def_Id);
3316 end if;
3317
3318 -- Propagate any pending access types whose finalization masters need to
3319 -- be fully initialized from the partial to the full view. Guard against
3320 -- an illegal full view that remains unanalyzed.
3321
3322 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3323 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3324 end if;
3325
3326 if Chars (Scope (Def_Id)) = Name_System
3327 and then Chars (Def_Id) = Name_Address
3328 and then In_Predefined_Unit (N)
3329 then
3330 Set_Is_Descendant_Of_Address (Def_Id);
3331 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3332 Set_Is_Descendant_Of_Address (Prev);
3333 end if;
3334
3335 Set_Optimize_Alignment_Flags (Def_Id);
3336 Check_Eliminated (Def_Id);
3337
3338 -- If the declaration is a completion and aspects are present, apply
3339 -- them to the entity for the type which is currently the partial
3340 -- view, but which is the one that will be frozen.
3341
3342 if Has_Aspects (N) then
3343
3344 -- In most cases the partial view is a private type, and both views
3345 -- appear in different declarative parts. In the unusual case where
3346 -- the partial view is incomplete, perform the analysis on the
3347 -- full view, to prevent freezing anomalies with the corresponding
3348 -- class-wide type, which otherwise might be frozen before the
3349 -- dispatch table is built.
3350
3351 if Prev /= Def_Id
3352 and then Ekind (Prev) /= E_Incomplete_Type
3353 then
3354 Analyze_Aspect_Specifications (N, Prev);
3355
3356 -- Normal case
3357
3358 else
3359 Analyze_Aspect_Specifications (N, Def_Id);
3360 end if;
3361 end if;
3362
3363 if Is_Derived_Type (Prev)
3364 and then Def_Id /= Prev
3365 then
3366 Check_Nonoverridable_Aspects;
3367 end if;
3368 end Analyze_Full_Type_Declaration;
3369
3370 ----------------------------------
3371 -- Analyze_Incomplete_Type_Decl --
3372 ----------------------------------
3373
3374 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3375 F : constant Boolean := Is_Pure (Current_Scope);
3376 T : Entity_Id;
3377
3378 begin
3379 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3380
3381 Generate_Definition (Defining_Identifier (N));
3382
3383 -- Process an incomplete declaration. The identifier must not have been
3384 -- declared already in the scope. However, an incomplete declaration may
3385 -- appear in the private part of a package, for a private type that has
3386 -- already been declared.
3387
3388 -- In this case, the discriminants (if any) must match
3389
3390 T := Find_Type_Name (N);
3391
3392 Set_Ekind (T, E_Incomplete_Type);
3393 Set_Etype (T, T);
3394 Set_Is_First_Subtype (T);
3395 Init_Size_Align (T);
3396
3397 -- Set the SPARK mode from the current context
3398
3399 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3400 Set_SPARK_Pragma_Inherited (T);
3401
3402 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3403 -- incomplete types.
3404
3405 if Tagged_Present (N) then
3406 Set_Is_Tagged_Type (T, True);
3407 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3408 Make_Class_Wide_Type (T);
3409 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3410 end if;
3411
3412 Set_Stored_Constraint (T, No_Elist);
3413
3414 if Present (Discriminant_Specifications (N)) then
3415 Push_Scope (T);
3416 Process_Discriminants (N);
3417 End_Scope;
3418 end if;
3419
3420 -- If the type has discriminants, nontrivial subtypes may be declared
3421 -- before the full view of the type. The full views of those subtypes
3422 -- will be built after the full view of the type.
3423
3424 Set_Private_Dependents (T, New_Elmt_List);
3425 Set_Is_Pure (T, F);
3426 end Analyze_Incomplete_Type_Decl;
3427
3428 -----------------------------------
3429 -- Analyze_Interface_Declaration --
3430 -----------------------------------
3431
3432 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3433 CW : constant Entity_Id := Class_Wide_Type (T);
3434
3435 begin
3436 Set_Is_Tagged_Type (T);
3437 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3438
3439 Set_Is_Limited_Record (T, Limited_Present (Def)
3440 or else Task_Present (Def)
3441 or else Protected_Present (Def)
3442 or else Synchronized_Present (Def));
3443
3444 -- Type is abstract if full declaration carries keyword, or if previous
3445 -- partial view did.
3446
3447 Set_Is_Abstract_Type (T);
3448 Set_Is_Interface (T);
3449
3450 -- Type is a limited interface if it includes the keyword limited, task,
3451 -- protected, or synchronized.
3452
3453 Set_Is_Limited_Interface
3454 (T, Limited_Present (Def)
3455 or else Protected_Present (Def)
3456 or else Synchronized_Present (Def)
3457 or else Task_Present (Def));
3458
3459 Set_Interfaces (T, New_Elmt_List);
3460 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3461
3462 -- Complete the decoration of the class-wide entity if it was already
3463 -- built (i.e. during the creation of the limited view)
3464
3465 if Present (CW) then
3466 Set_Is_Interface (CW);
3467 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3468 end if;
3469
3470 -- Check runtime support for synchronized interfaces
3471
3472 if (Is_Task_Interface (T)
3473 or else Is_Protected_Interface (T)
3474 or else Is_Synchronized_Interface (T))
3475 and then not RTE_Available (RE_Select_Specific_Data)
3476 then
3477 Error_Msg_CRT ("synchronized interfaces", T);
3478 end if;
3479 end Analyze_Interface_Declaration;
3480
3481 -----------------------------
3482 -- Analyze_Itype_Reference --
3483 -----------------------------
3484
3485 -- Nothing to do. This node is placed in the tree only for the benefit of
3486 -- back end processing, and has no effect on the semantic processing.
3487
3488 procedure Analyze_Itype_Reference (N : Node_Id) is
3489 begin
3490 pragma Assert (Is_Itype (Itype (N)));
3491 null;
3492 end Analyze_Itype_Reference;
3493
3494 --------------------------------
3495 -- Analyze_Number_Declaration --
3496 --------------------------------
3497
3498 procedure Analyze_Number_Declaration (N : Node_Id) is
3499 E : constant Node_Id := Expression (N);
3500 Id : constant Entity_Id := Defining_Identifier (N);
3501 Index : Interp_Index;
3502 It : Interp;
3503 T : Entity_Id;
3504
3505 begin
3506 Generate_Definition (Id);
3507 Enter_Name (Id);
3508
3509 -- This is an optimization of a common case of an integer literal
3510
3511 if Nkind (E) = N_Integer_Literal then
3512 Set_Is_Static_Expression (E, True);
3513 Set_Etype (E, Universal_Integer);
3514
3515 Set_Etype (Id, Universal_Integer);
3516 Set_Ekind (Id, E_Named_Integer);
3517 Set_Is_Frozen (Id, True);
3518 return;
3519 end if;
3520
3521 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3522
3523 -- Process expression, replacing error by integer zero, to avoid
3524 -- cascaded errors or aborts further along in the processing
3525
3526 -- Replace Error by integer zero, which seems least likely to cause
3527 -- cascaded errors.
3528
3529 if E = Error then
3530 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3531 Set_Error_Posted (E);
3532 end if;
3533
3534 Analyze (E);
3535
3536 -- Verify that the expression is static and numeric. If
3537 -- the expression is overloaded, we apply the preference
3538 -- rule that favors root numeric types.
3539
3540 if not Is_Overloaded (E) then
3541 T := Etype (E);
3542 if Has_Dynamic_Predicate_Aspect (T) then
3543 Error_Msg_N
3544 ("subtype has dynamic predicate, "
3545 & "not allowed in number declaration", N);
3546 end if;
3547
3548 else
3549 T := Any_Type;
3550
3551 Get_First_Interp (E, Index, It);
3552 while Present (It.Typ) loop
3553 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3554 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3555 then
3556 if T = Any_Type then
3557 T := It.Typ;
3558
3559 elsif It.Typ = Universal_Real
3560 or else
3561 It.Typ = Universal_Integer
3562 then
3563 -- Choose universal interpretation over any other
3564
3565 T := It.Typ;
3566 exit;
3567 end if;
3568 end if;
3569
3570 Get_Next_Interp (Index, It);
3571 end loop;
3572 end if;
3573
3574 if Is_Integer_Type (T) then
3575 Resolve (E, T);
3576 Set_Etype (Id, Universal_Integer);
3577 Set_Ekind (Id, E_Named_Integer);
3578
3579 elsif Is_Real_Type (T) then
3580
3581 -- Because the real value is converted to universal_real, this is a
3582 -- legal context for a universal fixed expression.
3583
3584 if T = Universal_Fixed then
3585 declare
3586 Loc : constant Source_Ptr := Sloc (N);
3587 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3588 Subtype_Mark =>
3589 New_Occurrence_Of (Universal_Real, Loc),
3590 Expression => Relocate_Node (E));
3591
3592 begin
3593 Rewrite (E, Conv);
3594 Analyze (E);
3595 end;
3596
3597 elsif T = Any_Fixed then
3598 Error_Msg_N ("illegal context for mixed mode operation", E);
3599
3600 -- Expression is of the form : universal_fixed * integer. Try to
3601 -- resolve as universal_real.
3602
3603 T := Universal_Real;
3604 Set_Etype (E, T);
3605 end if;
3606
3607 Resolve (E, T);
3608 Set_Etype (Id, Universal_Real);
3609 Set_Ekind (Id, E_Named_Real);
3610
3611 else
3612 Wrong_Type (E, Any_Numeric);
3613 Resolve (E, T);
3614
3615 Set_Etype (Id, T);
3616 Set_Ekind (Id, E_Constant);
3617 Set_Never_Set_In_Source (Id, True);
3618 Set_Is_True_Constant (Id, True);
3619 return;
3620 end if;
3621
3622 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3623 Set_Etype (E, Etype (Id));
3624 end if;
3625
3626 if not Is_OK_Static_Expression (E) then
3627 Flag_Non_Static_Expr
3628 ("non-static expression used in number declaration!", E);
3629 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3630 Set_Etype (E, Any_Type);
3631 end if;
3632
3633 Analyze_Dimension (N);
3634 end Analyze_Number_Declaration;
3635
3636 --------------------------------
3637 -- Analyze_Object_Declaration --
3638 --------------------------------
3639
3640 -- WARNING: This routine manages Ghost regions. Return statements must be
3641 -- replaced by gotos which jump to the end of the routine and restore the
3642 -- Ghost mode.
3643
3644 procedure Analyze_Object_Declaration (N : Node_Id) is
3645 Loc : constant Source_Ptr := Sloc (N);
3646 Id : constant Entity_Id := Defining_Identifier (N);
3647 Act_T : Entity_Id;
3648 T : Entity_Id;
3649
3650 E : Node_Id := Expression (N);
3651 -- E is set to Expression (N) throughout this routine. When Expression
3652 -- (N) is modified, E is changed accordingly.
3653
3654 Prev_Entity : Entity_Id := Empty;
3655
3656 procedure Check_Dynamic_Object (Typ : Entity_Id);
3657 -- A library-level object with non-static discriminant constraints may
3658 -- require dynamic allocation. The declaration is illegal if the
3659 -- profile includes the restriction No_Implicit_Heap_Allocations.
3660
3661 procedure Check_For_Null_Excluding_Components
3662 (Obj_Typ : Entity_Id;
3663 Obj_Decl : Node_Id);
3664 -- Verify that each null-excluding component of object declaration
3665 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3666 -- a compile-time warning if this is not the case.
3667
3668 function Count_Tasks (T : Entity_Id) return Uint;
3669 -- This function is called when a non-generic library level object of a
3670 -- task type is declared. Its function is to count the static number of
3671 -- tasks declared within the type (it is only called if Has_Task is set
3672 -- for T). As a side effect, if an array of tasks with non-static bounds
3673 -- or a variant record type is encountered, Check_Restriction is called
3674 -- indicating the count is unknown.
3675
3676 function Delayed_Aspect_Present return Boolean;
3677 -- If the declaration has an expression that is an aggregate, and it
3678 -- has aspects that require delayed analysis, the resolution of the
3679 -- aggregate must be deferred to the freeze point of the object. This
3680 -- special processing was created for address clauses, but it must
3681 -- also apply to Alignment. This must be done before the aspect
3682 -- specifications are analyzed because we must handle the aggregate
3683 -- before the analysis of the object declaration is complete.
3684
3685 -- Any other relevant delayed aspects on object declarations ???
3686
3687 --------------------------
3688 -- Check_Dynamic_Object --
3689 --------------------------
3690
3691 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3692 Comp : Entity_Id;
3693 Obj_Type : Entity_Id;
3694
3695 begin
3696 Obj_Type := Typ;
3697
3698 if Is_Private_Type (Obj_Type)
3699 and then Present (Full_View (Obj_Type))
3700 then
3701 Obj_Type := Full_View (Obj_Type);
3702 end if;
3703
3704 if Known_Static_Esize (Obj_Type) then
3705 return;
3706 end if;
3707
3708 if Restriction_Active (No_Implicit_Heap_Allocations)
3709 and then Expander_Active
3710 and then Has_Discriminants (Obj_Type)
3711 then
3712 Comp := First_Component (Obj_Type);
3713 while Present (Comp) loop
3714 if Known_Static_Esize (Etype (Comp))
3715 or else Size_Known_At_Compile_Time (Etype (Comp))
3716 then
3717 null;
3718
3719 elsif not Discriminated_Size (Comp)
3720 and then Comes_From_Source (Comp)
3721 then
3722 Error_Msg_NE
3723 ("component& of non-static size will violate restriction "
3724 & "No_Implicit_Heap_Allocation?", N, Comp);
3725
3726 elsif Is_Record_Type (Etype (Comp)) then
3727 Check_Dynamic_Object (Etype (Comp));
3728 end if;
3729
3730 Next_Component (Comp);
3731 end loop;
3732 end if;
3733 end Check_Dynamic_Object;
3734
3735 -----------------------------------------
3736 -- Check_For_Null_Excluding_Components --
3737 -----------------------------------------
3738
3739 procedure Check_For_Null_Excluding_Components
3740 (Obj_Typ : Entity_Id;
3741 Obj_Decl : Node_Id)
3742 is
3743 procedure Check_Component
3744 (Comp_Typ : Entity_Id;
3745 Comp_Decl : Node_Id := Empty;
3746 Array_Comp : Boolean := False);
3747 -- Apply a compile-time null-exclusion check on a component denoted
3748 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3749 -- subcomponents (if any).
3750
3751 ---------------------
3752 -- Check_Component --
3753 ---------------------
3754
3755 procedure Check_Component
3756 (Comp_Typ : Entity_Id;
3757 Comp_Decl : Node_Id := Empty;
3758 Array_Comp : Boolean := False)
3759 is
3760 Comp : Entity_Id;
3761 T : Entity_Id;
3762
3763 begin
3764 -- Do not consider internally-generated components or those that
3765 -- are already initialized.
3766
3767 if Present (Comp_Decl)
3768 and then (not Comes_From_Source (Comp_Decl)
3769 or else Present (Expression (Comp_Decl)))
3770 then
3771 return;
3772 end if;
3773
3774 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3775 and then Present (Full_View (Comp_Typ))
3776 then
3777 T := Full_View (Comp_Typ);
3778 else
3779 T := Comp_Typ;
3780 end if;
3781
3782 -- Verify a component of a null-excluding access type
3783
3784 if Is_Access_Type (T)
3785 and then Can_Never_Be_Null (T)
3786 then
3787 if Comp_Decl = Obj_Decl then
3788 Null_Exclusion_Static_Checks
3789 (N => Obj_Decl,
3790 Comp => Empty,
3791 Array_Comp => Array_Comp);
3792
3793 else
3794 Null_Exclusion_Static_Checks
3795 (N => Obj_Decl,
3796 Comp => Comp_Decl,
3797 Array_Comp => Array_Comp);
3798 end if;
3799
3800 -- Check array components
3801
3802 elsif Is_Array_Type (T) then
3803
3804 -- There is no suitable component when the object is of an
3805 -- array type. However, a namable component may appear at some
3806 -- point during the recursive inspection, but not at the top
3807 -- level. At the top level just indicate array component case.
3808
3809 if Comp_Decl = Obj_Decl then
3810 Check_Component (Component_Type (T), Array_Comp => True);
3811 else
3812 Check_Component (Component_Type (T), Comp_Decl);
3813 end if;
3814
3815 -- Verify all components of type T
3816
3817 -- Note: No checks are performed on types with discriminants due
3818 -- to complexities involving variants. ???
3819
3820 elsif (Is_Concurrent_Type (T)
3821 or else Is_Incomplete_Or_Private_Type (T)
3822 or else Is_Record_Type (T))
3823 and then not Has_Discriminants (T)
3824 then
3825 Comp := First_Component (T);
3826 while Present (Comp) loop
3827 Check_Component (Etype (Comp), Parent (Comp));
3828
3829 Comp := Next_Component (Comp);
3830 end loop;
3831 end if;
3832 end Check_Component;
3833
3834 -- Start processing for Check_For_Null_Excluding_Components
3835
3836 begin
3837 Check_Component (Obj_Typ, Obj_Decl);
3838 end Check_For_Null_Excluding_Components;
3839
3840 -----------------
3841 -- Count_Tasks --
3842 -----------------
3843
3844 function Count_Tasks (T : Entity_Id) return Uint is
3845 C : Entity_Id;
3846 X : Node_Id;
3847 V : Uint;
3848
3849 begin
3850 if Is_Task_Type (T) then
3851 return Uint_1;
3852
3853 elsif Is_Record_Type (T) then
3854 if Has_Discriminants (T) then
3855 Check_Restriction (Max_Tasks, N);
3856 return Uint_0;
3857
3858 else
3859 V := Uint_0;
3860 C := First_Component (T);
3861 while Present (C) loop
3862 V := V + Count_Tasks (Etype (C));
3863 Next_Component (C);
3864 end loop;
3865
3866 return V;
3867 end if;
3868
3869 elsif Is_Array_Type (T) then
3870 X := First_Index (T);
3871 V := Count_Tasks (Component_Type (T));
3872 while Present (X) loop
3873 C := Etype (X);
3874
3875 if not Is_OK_Static_Subtype (C) then
3876 Check_Restriction (Max_Tasks, N);
3877 return Uint_0;
3878 else
3879 V := V * (UI_Max (Uint_0,
3880 Expr_Value (Type_High_Bound (C)) -
3881 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3882 end if;
3883
3884 Next_Index (X);
3885 end loop;
3886
3887 return V;
3888
3889 else
3890 return Uint_0;
3891 end if;
3892 end Count_Tasks;
3893
3894 ----------------------------
3895 -- Delayed_Aspect_Present --
3896 ----------------------------
3897
3898 function Delayed_Aspect_Present return Boolean is
3899 A : Node_Id;
3900 A_Id : Aspect_Id;
3901
3902 begin
3903 if Present (Aspect_Specifications (N)) then
3904 A := First (Aspect_Specifications (N));
3905 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3906 while Present (A) loop
3907 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3908 return True;
3909 end if;
3910
3911 Next (A);
3912 end loop;
3913 end if;
3914
3915 return False;
3916 end Delayed_Aspect_Present;
3917
3918 -- Local variables
3919
3920 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3921 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3922 -- Save the Ghost-related attributes to restore on exit
3923
3924 Related_Id : Entity_Id;
3925
3926 -- Start of processing for Analyze_Object_Declaration
3927
3928 begin
3929 -- There are three kinds of implicit types generated by an
3930 -- object declaration:
3931
3932 -- 1. Those generated by the original Object Definition
3933
3934 -- 2. Those generated by the Expression
3935
3936 -- 3. Those used to constrain the Object Definition with the
3937 -- expression constraints when the definition is unconstrained.
3938
3939 -- They must be generated in this order to avoid order of elaboration
3940 -- issues. Thus the first step (after entering the name) is to analyze
3941 -- the object definition.
3942
3943 if Constant_Present (N) then
3944 Prev_Entity := Current_Entity_In_Scope (Id);
3945
3946 if Present (Prev_Entity)
3947 and then
3948 -- If the homograph is an implicit subprogram, it is overridden
3949 -- by the current declaration.
3950
3951 ((Is_Overloadable (Prev_Entity)
3952 and then Is_Inherited_Operation (Prev_Entity))
3953
3954 -- The current object is a discriminal generated for an entry
3955 -- family index. Even though the index is a constant, in this
3956 -- particular context there is no true constant redeclaration.
3957 -- Enter_Name will handle the visibility.
3958
3959 or else
3960 (Is_Discriminal (Id)
3961 and then Ekind (Discriminal_Link (Id)) =
3962 E_Entry_Index_Parameter)
3963
3964 -- The current object is the renaming for a generic declared
3965 -- within the instance.
3966
3967 or else
3968 (Ekind (Prev_Entity) = E_Package
3969 and then Nkind (Parent (Prev_Entity)) =
3970 N_Package_Renaming_Declaration
3971 and then not Comes_From_Source (Prev_Entity)
3972 and then
3973 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3974
3975 -- The entity may be a homonym of a private component of the
3976 -- enclosing protected object, for which we create a local
3977 -- renaming declaration. The declaration is legal, even if
3978 -- useless when it just captures that component.
3979
3980 or else
3981 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3982 and then Nkind (Parent (Prev_Entity)) =
3983 N_Object_Renaming_Declaration))
3984 then
3985 Prev_Entity := Empty;
3986 end if;
3987 end if;
3988
3989 if Present (Prev_Entity) then
3990
3991 -- The object declaration is Ghost when it completes a deferred Ghost
3992 -- constant.
3993
3994 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3995
3996 Constant_Redeclaration (Id, N, T);
3997
3998 Generate_Reference (Prev_Entity, Id, 'c');
3999 Set_Completion_Referenced (Id);
4000
4001 if Error_Posted (N) then
4002
4003 -- Type mismatch or illegal redeclaration; do not analyze
4004 -- expression to avoid cascaded errors.
4005
4006 T := Find_Type_Of_Object (Object_Definition (N), N);
4007 Set_Etype (Id, T);
4008 Set_Ekind (Id, E_Variable);
4009 goto Leave;
4010 end if;
4011
4012 -- In the normal case, enter identifier at the start to catch premature
4013 -- usage in the initialization expression.
4014
4015 else
4016 Generate_Definition (Id);
4017 Enter_Name (Id);
4018
4019 Mark_Coextensions (N, Object_Definition (N));
4020
4021 T := Find_Type_Of_Object (Object_Definition (N), N);
4022
4023 if Nkind (Object_Definition (N)) = N_Access_Definition
4024 and then Present
4025 (Access_To_Subprogram_Definition (Object_Definition (N)))
4026 and then Protected_Present
4027 (Access_To_Subprogram_Definition (Object_Definition (N)))
4028 then
4029 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4030 end if;
4031
4032 if Error_Posted (Id) then
4033 Set_Etype (Id, T);
4034 Set_Ekind (Id, E_Variable);
4035 goto Leave;
4036 end if;
4037 end if;
4038
4039 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4040 -- out some static checks.
4041
4042 if Ada_Version >= Ada_2005 then
4043
4044 -- In case of aggregates we must also take care of the correct
4045 -- initialization of nested aggregates bug this is done at the
4046 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4047
4048 if Can_Never_Be_Null (T) then
4049 if Present (Expression (N))
4050 and then Nkind (Expression (N)) = N_Aggregate
4051 then
4052 null;
4053
4054 else
4055 declare
4056 Save_Typ : constant Entity_Id := Etype (Id);
4057 begin
4058 Set_Etype (Id, T); -- Temp. decoration for static checks
4059 Null_Exclusion_Static_Checks (N);
4060 Set_Etype (Id, Save_Typ);
4061 end;
4062 end if;
4063
4064 -- We might be dealing with an object of a composite type containing
4065 -- null-excluding components without an aggregate, so we must verify
4066 -- that such components have default initialization.
4067
4068 else
4069 Check_For_Null_Excluding_Components (T, N);
4070 end if;
4071 end if;
4072
4073 -- Object is marked pure if it is in a pure scope
4074
4075 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4076
4077 -- If deferred constant, make sure context is appropriate. We detect
4078 -- a deferred constant as a constant declaration with no expression.
4079 -- A deferred constant can appear in a package body if its completion
4080 -- is by means of an interface pragma.
4081
4082 if Constant_Present (N) and then No (E) then
4083
4084 -- A deferred constant may appear in the declarative part of the
4085 -- following constructs:
4086
4087 -- blocks
4088 -- entry bodies
4089 -- extended return statements
4090 -- package specs
4091 -- package bodies
4092 -- subprogram bodies
4093 -- task bodies
4094
4095 -- When declared inside a package spec, a deferred constant must be
4096 -- completed by a full constant declaration or pragma Import. In all
4097 -- other cases, the only proper completion is pragma Import. Extended
4098 -- return statements are flagged as invalid contexts because they do
4099 -- not have a declarative part and so cannot accommodate the pragma.
4100
4101 if Ekind (Current_Scope) = E_Return_Statement then
4102 Error_Msg_N
4103 ("invalid context for deferred constant declaration (RM 7.4)",
4104 N);
4105 Error_Msg_N
4106 ("\declaration requires an initialization expression",
4107 N);
4108 Set_Constant_Present (N, False);
4109
4110 -- In Ada 83, deferred constant must be of private type
4111
4112 elsif not Is_Private_Type (T) then
4113 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4114 Error_Msg_N
4115 ("(Ada 83) deferred constant must be private type", N);
4116 end if;
4117 end if;
4118
4119 -- If not a deferred constant, then the object declaration freezes
4120 -- its type, unless the object is of an anonymous type and has delayed
4121 -- aspects. In that case the type is frozen when the object itself is.
4122
4123 else
4124 Check_Fully_Declared (T, N);
4125
4126 if Has_Delayed_Aspects (Id)
4127 and then Is_Array_Type (T)
4128 and then Is_Itype (T)
4129 then
4130 Set_Has_Delayed_Freeze (T);
4131 else
4132 Freeze_Before (N, T);
4133 end if;
4134 end if;
4135
4136 -- If the object was created by a constrained array definition, then
4137 -- set the link in both the anonymous base type and anonymous subtype
4138 -- that are built to represent the array type to point to the object.
4139
4140 if Nkind (Object_Definition (Declaration_Node (Id))) =
4141 N_Constrained_Array_Definition
4142 then
4143 Set_Related_Array_Object (T, Id);
4144 Set_Related_Array_Object (Base_Type (T), Id);
4145 end if;
4146
4147 -- Special checks for protected objects not at library level
4148
4149 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4150 Check_Restriction (No_Local_Protected_Objects, Id);
4151
4152 -- Protected objects with interrupt handlers must be at library level
4153
4154 -- Ada 2005: This test is not needed (and the corresponding clause
4155 -- in the RM is removed) because accessibility checks are sufficient
4156 -- to make handlers not at the library level illegal.
4157
4158 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4159 -- applies to the '95 version of the language as well.
4160
4161 if Is_Protected_Type (T)
4162 and then Has_Interrupt_Handler (T)
4163 and then Ada_Version < Ada_95
4164 then
4165 Error_Msg_N
4166 ("interrupt object can only be declared at library level", Id);
4167 end if;
4168 end if;
4169
4170 -- Check for violation of No_Local_Timing_Events
4171
4172 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4173 Check_Restriction (No_Local_Timing_Events, Id);
4174 end if;
4175
4176 -- The actual subtype of the object is the nominal subtype, unless
4177 -- the nominal one is unconstrained and obtained from the expression.
4178
4179 Act_T := T;
4180
4181 -- These checks should be performed before the initialization expression
4182 -- is considered, so that the Object_Definition node is still the same
4183 -- as in source code.
4184
4185 -- In SPARK, the nominal subtype is always given by a subtype mark
4186 -- and must not be unconstrained. (The only exception to this is the
4187 -- acceptance of declarations of constants of type String.)
4188
4189 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4190 then
4191 Check_SPARK_05_Restriction
4192 ("subtype mark required", Object_Definition (N));
4193
4194 elsif Is_Array_Type (T)
4195 and then not Is_Constrained (T)
4196 and then T /= Standard_String
4197 then
4198 Check_SPARK_05_Restriction
4199 ("subtype mark of constrained type expected",
4200 Object_Definition (N));
4201 end if;
4202
4203 if Is_Library_Level_Entity (Id) then
4204 Check_Dynamic_Object (T);
4205 end if;
4206
4207 -- There are no aliased objects in SPARK
4208
4209 if Aliased_Present (N) then
4210 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4211 end if;
4212
4213 -- Process initialization expression if present and not in error
4214
4215 if Present (E) and then E /= Error then
4216
4217 -- Generate an error in case of CPP class-wide object initialization.
4218 -- Required because otherwise the expansion of the class-wide
4219 -- assignment would try to use 'size to initialize the object
4220 -- (primitive that is not available in CPP tagged types).
4221
4222 if Is_Class_Wide_Type (Act_T)
4223 and then
4224 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4225 or else
4226 (Present (Full_View (Root_Type (Etype (Act_T))))
4227 and then
4228 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4229 then
4230 Error_Msg_N
4231 ("predefined assignment not available for 'C'P'P tagged types",
4232 E);
4233 end if;
4234
4235 Mark_Coextensions (N, E);
4236 Analyze (E);
4237
4238 -- In case of errors detected in the analysis of the expression,
4239 -- decorate it with the expected type to avoid cascaded errors
4240
4241 if No (Etype (E)) then
4242 Set_Etype (E, T);
4243 end if;
4244
4245 -- If an initialization expression is present, then we set the
4246 -- Is_True_Constant flag. It will be reset if this is a variable
4247 -- and it is indeed modified.
4248
4249 Set_Is_True_Constant (Id, True);
4250
4251 -- If we are analyzing a constant declaration, set its completion
4252 -- flag after analyzing and resolving the expression.
4253
4254 if Constant_Present (N) then
4255 Set_Has_Completion (Id);
4256 end if;
4257
4258 -- Set type and resolve (type may be overridden later on). Note:
4259 -- Ekind (Id) must still be E_Void at this point so that incorrect
4260 -- early usage within E is properly diagnosed.
4261
4262 Set_Etype (Id, T);
4263
4264 -- If the expression is an aggregate we must look ahead to detect
4265 -- the possible presence of an address clause, and defer resolution
4266 -- and expansion of the aggregate to the freeze point of the entity.
4267
4268 -- This is not always legal because the aggregate may contain other
4269 -- references that need freezing, e.g. references to other entities
4270 -- with address clauses. In any case, when compiling with -gnatI the
4271 -- presence of the address clause must be ignored.
4272
4273 if Comes_From_Source (N)
4274 and then Expander_Active
4275 and then Nkind (E) = N_Aggregate
4276 and then
4277 ((Present (Following_Address_Clause (N))
4278 and then not Ignore_Rep_Clauses)
4279 or else Delayed_Aspect_Present)
4280 then
4281 Set_Etype (E, T);
4282
4283 else
4284
4285 -- If the expression is a formal that is a "subprogram pointer"
4286 -- this is illegal in accessibility terms. Add an explicit
4287 -- conversion to force the corresponding check, as is done for
4288 -- assignments.
4289
4290 if Comes_From_Source (N)
4291 and then Is_Entity_Name (E)
4292 and then Present (Entity (E))
4293 and then Is_Formal (Entity (E))
4294 and then
4295 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4296 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4297 then
4298 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4299 end if;
4300
4301 Resolve (E, T);
4302 end if;
4303
4304 -- No further action needed if E is a call to an inlined function
4305 -- which returns an unconstrained type and it has been expanded into
4306 -- a procedure call. In that case N has been replaced by an object
4307 -- declaration without initializing expression and it has been
4308 -- analyzed (see Expand_Inlined_Call).
4309
4310 if Back_End_Inlining
4311 and then Expander_Active
4312 and then Nkind (E) = N_Function_Call
4313 and then Nkind (Name (E)) in N_Has_Entity
4314 and then Is_Inlined (Entity (Name (E)))
4315 and then not Is_Constrained (Etype (E))
4316 and then Analyzed (N)
4317 and then No (Expression (N))
4318 then
4319 goto Leave;
4320 end if;
4321
4322 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4323 -- node (which was marked already-analyzed), we need to set the type
4324 -- to something other than Any_Access in order to keep gigi happy.
4325
4326 if Etype (E) = Any_Access then
4327 Set_Etype (E, T);
4328 end if;
4329
4330 -- If the object is an access to variable, the initialization
4331 -- expression cannot be an access to constant.
4332
4333 if Is_Access_Type (T)
4334 and then not Is_Access_Constant (T)
4335 and then Is_Access_Type (Etype (E))
4336 and then Is_Access_Constant (Etype (E))
4337 then
4338 Error_Msg_N
4339 ("access to variable cannot be initialized with an "
4340 & "access-to-constant expression", E);
4341 end if;
4342
4343 if not Assignment_OK (N) then
4344 Check_Initialization (T, E);
4345 end if;
4346
4347 Check_Unset_Reference (E);
4348
4349 -- If this is a variable, then set current value. If this is a
4350 -- declared constant of a scalar type with a static expression,
4351 -- indicate that it is always valid.
4352
4353 if not Constant_Present (N) then
4354 if Compile_Time_Known_Value (E) then
4355 Set_Current_Value (Id, E);
4356 end if;
4357
4358 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4359 Set_Is_Known_Valid (Id);
4360 end if;
4361
4362 -- Deal with setting of null flags
4363
4364 if Is_Access_Type (T) then
4365 if Known_Non_Null (E) then
4366 Set_Is_Known_Non_Null (Id, True);
4367 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4368 Set_Is_Known_Null (Id, True);
4369 end if;
4370 end if;
4371
4372 -- Check incorrect use of dynamically tagged expressions
4373
4374 if Is_Tagged_Type (T) then
4375 Check_Dynamically_Tagged_Expression
4376 (Expr => E,
4377 Typ => T,
4378 Related_Nod => N);
4379 end if;
4380
4381 Apply_Scalar_Range_Check (E, T);
4382 Apply_Static_Length_Check (E, T);
4383
4384 if Nkind (Original_Node (N)) = N_Object_Declaration
4385 and then Comes_From_Source (Original_Node (N))
4386
4387 -- Only call test if needed
4388
4389 and then Restriction_Check_Required (SPARK_05)
4390 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4391 then
4392 Check_SPARK_05_Restriction
4393 ("initialization expression is not appropriate", E);
4394 end if;
4395
4396 -- A formal parameter of a specific tagged type whose related
4397 -- subprogram is subject to pragma Extensions_Visible with value
4398 -- "False" cannot be implicitly converted to a class-wide type by
4399 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4400 -- not consider internally generated expressions.
4401
4402 if Is_Class_Wide_Type (T)
4403 and then Comes_From_Source (E)
4404 and then Is_EVF_Expression (E)
4405 then
4406 Error_Msg_N
4407 ("formal parameter cannot be implicitly converted to "
4408 & "class-wide type when Extensions_Visible is False", E);
4409 end if;
4410 end if;
4411
4412 -- If the No_Streams restriction is set, check that the type of the
4413 -- object is not, and does not contain, any subtype derived from
4414 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4415 -- Has_Stream just for efficiency reasons. There is no point in
4416 -- spending time on a Has_Stream check if the restriction is not set.
4417
4418 if Restriction_Check_Required (No_Streams) then
4419 if Has_Stream (T) then
4420 Check_Restriction (No_Streams, N);
4421 end if;
4422 end if;
4423
4424 -- Deal with predicate check before we start to do major rewriting. It
4425 -- is OK to initialize and then check the initialized value, since the
4426 -- object goes out of scope if we get a predicate failure. Note that we
4427 -- do this in the analyzer and not the expander because the analyzer
4428 -- does some substantial rewriting in some cases.
4429
4430 -- We need a predicate check if the type has predicates that are not
4431 -- ignored, and if either there is an initializing expression, or for
4432 -- default initialization when we have at least one case of an explicit
4433 -- default initial value and then this is not an internal declaration
4434 -- whose initialization comes later (as for an aggregate expansion).
4435
4436 if not Suppress_Assignment_Checks (N)
4437 and then Present (Predicate_Function (T))
4438 and then not Predicates_Ignored (T)
4439 and then not No_Initialization (N)
4440 and then
4441 (Present (E)
4442 or else
4443 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4444 then
4445 -- If the type has a static predicate and the expression is known at
4446 -- compile time, see if the expression satisfies the predicate.
4447
4448 if Present (E) then
4449 Check_Expression_Against_Static_Predicate (E, T);
4450 end if;
4451
4452 -- If the type is a null record and there is no explicit initial
4453 -- expression, no predicate check applies.
4454
4455 if No (E) and then Is_Null_Record_Type (T) then
4456 null;
4457
4458 -- Do not generate a predicate check if the initialization expression
4459 -- is a type conversion because the conversion has been subjected to
4460 -- the same check. This is a small optimization which avoid redundant
4461 -- checks.
4462
4463 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4464 null;
4465
4466 else
4467 Insert_After (N,
4468 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4469 end if;
4470 end if;
4471
4472 -- Case of unconstrained type
4473
4474 if not Is_Definite_Subtype (T) then
4475
4476 -- In SPARK, a declaration of unconstrained type is allowed
4477 -- only for constants of type string.
4478
4479 if Is_String_Type (T) and then not Constant_Present (N) then
4480 Check_SPARK_05_Restriction
4481 ("declaration of object of unconstrained type not allowed", N);
4482 end if;
4483
4484 -- Nothing to do in deferred constant case
4485
4486 if Constant_Present (N) and then No (E) then
4487 null;
4488
4489 -- Case of no initialization present
4490
4491 elsif No (E) then
4492 if No_Initialization (N) then
4493 null;
4494
4495 elsif Is_Class_Wide_Type (T) then
4496 Error_Msg_N
4497 ("initialization required in class-wide declaration ", N);
4498
4499 else
4500 Error_Msg_N
4501 ("unconstrained subtype not allowed (need initialization)",
4502 Object_Definition (N));
4503
4504 if Is_Record_Type (T) and then Has_Discriminants (T) then
4505 Error_Msg_N
4506 ("\provide initial value or explicit discriminant values",
4507 Object_Definition (N));
4508
4509 Error_Msg_NE
4510 ("\or give default discriminant values for type&",
4511 Object_Definition (N), T);
4512
4513 elsif Is_Array_Type (T) then
4514 Error_Msg_N
4515 ("\provide initial value or explicit array bounds",
4516 Object_Definition (N));
4517 end if;
4518 end if;
4519
4520 -- Case of initialization present but in error. Set initial
4521 -- expression as absent (but do not make above complaints)
4522
4523 elsif E = Error then
4524 Set_Expression (N, Empty);
4525 E := Empty;
4526
4527 -- Case of initialization present
4528
4529 else
4530 -- Check restrictions in Ada 83
4531
4532 if not Constant_Present (N) then
4533
4534 -- Unconstrained variables not allowed in Ada 83 mode
4535
4536 if Ada_Version = Ada_83
4537 and then Comes_From_Source (Object_Definition (N))
4538 then
4539 Error_Msg_N
4540 ("(Ada 83) unconstrained variable not allowed",
4541 Object_Definition (N));
4542 end if;
4543 end if;
4544
4545 -- Now we constrain the variable from the initializing expression
4546
4547 -- If the expression is an aggregate, it has been expanded into
4548 -- individual assignments. Retrieve the actual type from the
4549 -- expanded construct.
4550
4551 if Is_Array_Type (T)
4552 and then No_Initialization (N)
4553 and then Nkind (Original_Node (E)) = N_Aggregate
4554 then
4555 Act_T := Etype (E);
4556
4557 -- In case of class-wide interface object declarations we delay
4558 -- the generation of the equivalent record type declarations until
4559 -- its expansion because there are cases in they are not required.
4560
4561 elsif Is_Interface (T) then
4562 null;
4563
4564 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4565 -- we should prevent the generation of another Itype with the
4566 -- same name as the one already generated, or we end up with
4567 -- two identical types in GNATprove.
4568
4569 elsif GNATprove_Mode then
4570 null;
4571
4572 -- If the type is an unchecked union, no subtype can be built from
4573 -- the expression. Rewrite declaration as a renaming, which the
4574 -- back-end can handle properly. This is a rather unusual case,
4575 -- because most unchecked_union declarations have default values
4576 -- for discriminants and are thus not indefinite.
4577
4578 elsif Is_Unchecked_Union (T) then
4579 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4580 Set_Ekind (Id, E_Constant);
4581 else
4582 Set_Ekind (Id, E_Variable);
4583 end if;
4584
4585 Rewrite (N,
4586 Make_Object_Renaming_Declaration (Loc,
4587 Defining_Identifier => Id,
4588 Subtype_Mark => New_Occurrence_Of (T, Loc),
4589 Name => E));
4590
4591 Set_Renamed_Object (Id, E);
4592 Freeze_Before (N, T);
4593 Set_Is_Frozen (Id);
4594 goto Leave;
4595
4596 else
4597 -- Ensure that the generated subtype has a unique external name
4598 -- when the related object is public. This guarantees that the
4599 -- subtype and its bounds will not be affected by switches or
4600 -- pragmas that may offset the internal counter due to extra
4601 -- generated code.
4602
4603 if Is_Public (Id) then
4604 Related_Id := Id;
4605 else
4606 Related_Id := Empty;
4607 end if;
4608
4609 Expand_Subtype_From_Expr
4610 (N => N,
4611 Unc_Type => T,
4612 Subtype_Indic => Object_Definition (N),
4613 Exp => E,
4614 Related_Id => Related_Id);
4615
4616 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4617 end if;
4618
4619 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4620
4621 if Aliased_Present (N) then
4622 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4623 end if;
4624
4625 Freeze_Before (N, Act_T);
4626 Freeze_Before (N, T);
4627 end if;
4628
4629 elsif Is_Array_Type (T)
4630 and then No_Initialization (N)
4631 and then (Nkind (Original_Node (E)) = N_Aggregate
4632 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4633 and then Nkind (Original_Node (Expression
4634 (Original_Node (E)))) = N_Aggregate))
4635 then
4636 if not Is_Entity_Name (Object_Definition (N)) then
4637 Act_T := Etype (E);
4638 Check_Compile_Time_Size (Act_T);
4639
4640 if Aliased_Present (N) then
4641 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4642 end if;
4643 end if;
4644
4645 -- When the given object definition and the aggregate are specified
4646 -- independently, and their lengths might differ do a length check.
4647 -- This cannot happen if the aggregate is of the form (others =>...)
4648
4649 if not Is_Constrained (T) then
4650 null;
4651
4652 elsif Nkind (E) = N_Raise_Constraint_Error then
4653
4654 -- Aggregate is statically illegal. Place back in declaration
4655
4656 Set_Expression (N, E);
4657 Set_No_Initialization (N, False);
4658
4659 elsif T = Etype (E) then
4660 null;
4661
4662 elsif Nkind (E) = N_Aggregate
4663 and then Present (Component_Associations (E))
4664 and then Present (Choice_List (First (Component_Associations (E))))
4665 and then
4666 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4667 N_Others_Choice
4668 then
4669 null;
4670
4671 else
4672 Apply_Length_Check (E, T);
4673 end if;
4674
4675 -- If the type is limited unconstrained with defaulted discriminants and
4676 -- there is no expression, then the object is constrained by the
4677 -- defaults, so it is worthwhile building the corresponding subtype.
4678
4679 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4680 and then not Is_Constrained (T)
4681 and then Has_Discriminants (T)
4682 then
4683 if No (E) then
4684 Act_T := Build_Default_Subtype (T, N);
4685 else
4686 -- Ada 2005: A limited object may be initialized by means of an
4687 -- aggregate. If the type has default discriminants it has an
4688 -- unconstrained nominal type, Its actual subtype will be obtained
4689 -- from the aggregate, and not from the default discriminants.
4690
4691 Act_T := Etype (E);
4692 end if;
4693
4694 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4695
4696 elsif Nkind (E) = N_Function_Call
4697 and then Constant_Present (N)
4698 and then Has_Unconstrained_Elements (Etype (E))
4699 then
4700 -- The back-end has problems with constants of a discriminated type
4701 -- with defaults, if the initial value is a function call. We
4702 -- generate an intermediate temporary that will receive a reference
4703 -- to the result of the call. The initialization expression then
4704 -- becomes a dereference of that temporary.
4705
4706 Remove_Side_Effects (E);
4707
4708 -- If this is a constant declaration of an unconstrained type and
4709 -- the initialization is an aggregate, we can use the subtype of the
4710 -- aggregate for the declared entity because it is immutable.
4711
4712 elsif not Is_Constrained (T)
4713 and then Has_Discriminants (T)
4714 and then Constant_Present (N)
4715 and then not Has_Unchecked_Union (T)
4716 and then Nkind (E) = N_Aggregate
4717 then
4718 Act_T := Etype (E);
4719 end if;
4720
4721 -- Check No_Wide_Characters restriction
4722
4723 Check_Wide_Character_Restriction (T, Object_Definition (N));
4724
4725 -- Indicate this is not set in source. Certainly true for constants, and
4726 -- true for variables so far (will be reset for a variable if and when
4727 -- we encounter a modification in the source).
4728
4729 Set_Never_Set_In_Source (Id);
4730
4731 -- Now establish the proper kind and type of the object
4732
4733 if Constant_Present (N) then
4734 Set_Ekind (Id, E_Constant);
4735 Set_Is_True_Constant (Id);
4736
4737 else
4738 Set_Ekind (Id, E_Variable);
4739
4740 -- A variable is set as shared passive if it appears in a shared
4741 -- passive package, and is at the outer level. This is not done for
4742 -- entities generated during expansion, because those are always
4743 -- manipulated locally.
4744
4745 if Is_Shared_Passive (Current_Scope)
4746 and then Is_Library_Level_Entity (Id)
4747 and then Comes_From_Source (Id)
4748 then
4749 Set_Is_Shared_Passive (Id);
4750 Check_Shared_Var (Id, T, N);
4751 end if;
4752
4753 -- Set Has_Initial_Value if initializing expression present. Note
4754 -- that if there is no initializing expression, we leave the state
4755 -- of this flag unchanged (usually it will be False, but notably in
4756 -- the case of exception choice variables, it will already be true).
4757
4758 if Present (E) then
4759 Set_Has_Initial_Value (Id);
4760 end if;
4761 end if;
4762
4763 -- Set the SPARK mode from the current context (may be overwritten later
4764 -- with explicit pragma).
4765
4766 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4767 Set_SPARK_Pragma_Inherited (Id);
4768
4769 -- Preserve relevant elaboration-related attributes of the context which
4770 -- are no longer available or very expensive to recompute once analysis,
4771 -- resolution, and expansion are over.
4772
4773 Mark_Elaboration_Attributes
4774 (N_Id => Id,
4775 Checks => True,
4776 Warnings => True);
4777
4778 -- Initialize alignment and size and capture alignment setting
4779
4780 Init_Alignment (Id);
4781 Init_Esize (Id);
4782 Set_Optimize_Alignment_Flags (Id);
4783
4784 -- Deal with aliased case
4785
4786 if Aliased_Present (N) then
4787 Set_Is_Aliased (Id);
4788
4789 -- If the object is aliased and the type is unconstrained with
4790 -- defaulted discriminants and there is no expression, then the
4791 -- object is constrained by the defaults, so it is worthwhile
4792 -- building the corresponding subtype.
4793
4794 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4795 -- unconstrained, then only establish an actual subtype if the
4796 -- nominal subtype is indefinite. In definite cases the object is
4797 -- unconstrained in Ada 2005.
4798
4799 if No (E)
4800 and then Is_Record_Type (T)
4801 and then not Is_Constrained (T)
4802 and then Has_Discriminants (T)
4803 and then (Ada_Version < Ada_2005
4804 or else not Is_Definite_Subtype (T))
4805 then
4806 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4807 end if;
4808 end if;
4809
4810 -- Now we can set the type of the object
4811
4812 Set_Etype (Id, Act_T);
4813
4814 -- Non-constant object is marked to be treated as volatile if type is
4815 -- volatile and we clear the Current_Value setting that may have been
4816 -- set above. Doing so for constants isn't required and might interfere
4817 -- with possible uses of the object as a static expression in contexts
4818 -- incompatible with volatility (e.g. as a case-statement alternative).
4819
4820 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4821 Set_Treat_As_Volatile (Id);
4822 Set_Current_Value (Id, Empty);
4823 end if;
4824
4825 -- Deal with controlled types
4826
4827 if Has_Controlled_Component (Etype (Id))
4828 or else Is_Controlled (Etype (Id))
4829 then
4830 if not Is_Library_Level_Entity (Id) then
4831 Check_Restriction (No_Nested_Finalization, N);
4832 else
4833 Validate_Controlled_Object (Id);
4834 end if;
4835 end if;
4836
4837 if Has_Task (Etype (Id)) then
4838 Check_Restriction (No_Tasking, N);
4839
4840 -- Deal with counting max tasks
4841
4842 -- Nothing to do if inside a generic
4843
4844 if Inside_A_Generic then
4845 null;
4846
4847 -- If library level entity, then count tasks
4848
4849 elsif Is_Library_Level_Entity (Id) then
4850 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4851
4852 -- If not library level entity, then indicate we don't know max
4853 -- tasks and also check task hierarchy restriction and blocking
4854 -- operation (since starting a task is definitely blocking).
4855
4856 else
4857 Check_Restriction (Max_Tasks, N);
4858 Check_Restriction (No_Task_Hierarchy, N);
4859 Check_Potentially_Blocking_Operation (N);
4860 end if;
4861
4862 -- A rather specialized test. If we see two tasks being declared
4863 -- of the same type in the same object declaration, and the task
4864 -- has an entry with an address clause, we know that program error
4865 -- will be raised at run time since we can't have two tasks with
4866 -- entries at the same address.
4867
4868 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4869 declare
4870 E : Entity_Id;
4871
4872 begin
4873 E := First_Entity (Etype (Id));
4874 while Present (E) loop
4875 if Ekind (E) = E_Entry
4876 and then Present (Get_Attribute_Definition_Clause
4877 (E, Attribute_Address))
4878 then
4879 Error_Msg_Warn := SPARK_Mode /= On;
4880 Error_Msg_N
4881 ("more than one task with same entry address<<", N);
4882 Error_Msg_N ("\Program_Error [<<", N);
4883 Insert_Action (N,
4884 Make_Raise_Program_Error (Loc,
4885 Reason => PE_Duplicated_Entry_Address));
4886 exit;
4887 end if;
4888
4889 Next_Entity (E);
4890 end loop;
4891 end;
4892 end if;
4893 end if;
4894
4895 -- Some simple constant-propagation: if the expression is a constant
4896 -- string initialized with a literal, share the literal. This avoids
4897 -- a run-time copy.
4898
4899 if Present (E)
4900 and then Is_Entity_Name (E)
4901 and then Ekind (Entity (E)) = E_Constant
4902 and then Base_Type (Etype (E)) = Standard_String
4903 then
4904 declare
4905 Val : constant Node_Id := Constant_Value (Entity (E));
4906 begin
4907 if Present (Val) and then Nkind (Val) = N_String_Literal then
4908 Rewrite (E, New_Copy (Val));
4909 end if;
4910 end;
4911 end if;
4912
4913 -- Another optimization: if the nominal subtype is unconstrained and
4914 -- the expression is a function call that returns an unconstrained
4915 -- type, rewrite the declaration as a renaming of the result of the
4916 -- call. The exceptions below are cases where the copy is expected,
4917 -- either by the back end (Aliased case) or by the semantics, as for
4918 -- initializing controlled types or copying tags for class-wide types.
4919
4920 if Present (E)
4921 and then Nkind (E) = N_Explicit_Dereference
4922 and then Nkind (Original_Node (E)) = N_Function_Call
4923 and then not Is_Library_Level_Entity (Id)
4924 and then not Is_Constrained (Underlying_Type (T))
4925 and then not Is_Aliased (Id)
4926 and then not Is_Class_Wide_Type (T)
4927 and then not Is_Controlled (T)
4928 and then not Has_Controlled_Component (Base_Type (T))
4929 and then Expander_Active
4930 then
4931 Rewrite (N,
4932 Make_Object_Renaming_Declaration (Loc,
4933 Defining_Identifier => Id,
4934 Access_Definition => Empty,
4935 Subtype_Mark => New_Occurrence_Of
4936 (Base_Type (Etype (Id)), Loc),
4937 Name => E));
4938
4939 Set_Renamed_Object (Id, E);
4940
4941 -- Force generation of debugging information for the constant and for
4942 -- the renamed function call.
4943
4944 Set_Debug_Info_Needed (Id);
4945 Set_Debug_Info_Needed (Entity (Prefix (E)));
4946 end if;
4947
4948 if Present (Prev_Entity)
4949 and then Is_Frozen (Prev_Entity)
4950 and then not Error_Posted (Id)
4951 then
4952 Error_Msg_N ("full constant declaration appears too late", N);
4953 end if;
4954
4955 Check_Eliminated (Id);
4956
4957 -- Deal with setting In_Private_Part flag if in private part
4958
4959 if Ekind (Scope (Id)) = E_Package
4960 and then In_Private_Part (Scope (Id))
4961 then
4962 Set_In_Private_Part (Id);
4963 end if;
4964
4965 <<Leave>>
4966 -- Initialize the refined state of a variable here because this is a
4967 -- common destination for legal and illegal object declarations.
4968
4969 if Ekind (Id) = E_Variable then
4970 Set_Encapsulating_State (Id, Empty);
4971 end if;
4972
4973 if Has_Aspects (N) then
4974 Analyze_Aspect_Specifications (N, Id);
4975 end if;
4976
4977 Analyze_Dimension (N);
4978
4979 -- Verify whether the object declaration introduces an illegal hidden
4980 -- state within a package subject to a null abstract state.
4981
4982 if Ekind (Id) = E_Variable then
4983 Check_No_Hidden_State (Id);
4984 end if;
4985
4986 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4987 end Analyze_Object_Declaration;
4988
4989 ---------------------------
4990 -- Analyze_Others_Choice --
4991 ---------------------------
4992
4993 -- Nothing to do for the others choice node itself, the semantic analysis
4994 -- of the others choice will occur as part of the processing of the parent
4995
4996 procedure Analyze_Others_Choice (N : Node_Id) is
4997 pragma Warnings (Off, N);
4998 begin
4999 null;
5000 end Analyze_Others_Choice;
5001
5002 -------------------------------------------
5003 -- Analyze_Private_Extension_Declaration --
5004 -------------------------------------------
5005
5006 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5007 Indic : constant Node_Id := Subtype_Indication (N);
5008 T : constant Entity_Id := Defining_Identifier (N);
5009 Iface : Entity_Id;
5010 Iface_Elmt : Elmt_Id;
5011 Parent_Base : Entity_Id;
5012 Parent_Type : Entity_Id;
5013
5014 begin
5015 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5016
5017 if Is_Non_Empty_List (Interface_List (N)) then
5018 declare
5019 Intf : Node_Id;
5020 T : Entity_Id;
5021
5022 begin
5023 Intf := First (Interface_List (N));
5024 while Present (Intf) loop
5025 T := Find_Type_Of_Subtype_Indic (Intf);
5026
5027 Diagnose_Interface (Intf, T);
5028 Next (Intf);
5029 end loop;
5030 end;
5031 end if;
5032
5033 Generate_Definition (T);
5034
5035 -- For other than Ada 2012, just enter the name in the current scope
5036
5037 if Ada_Version < Ada_2012 then
5038 Enter_Name (T);
5039
5040 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5041 -- case of private type that completes an incomplete type.
5042
5043 else
5044 declare
5045 Prev : Entity_Id;
5046
5047 begin
5048 Prev := Find_Type_Name (N);
5049
5050 pragma Assert (Prev = T
5051 or else (Ekind (Prev) = E_Incomplete_Type
5052 and then Present (Full_View (Prev))
5053 and then Full_View (Prev) = T));
5054 end;
5055 end if;
5056
5057 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5058 Parent_Base := Base_Type (Parent_Type);
5059
5060 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5061 Set_Ekind (T, Ekind (Parent_Type));
5062 Set_Etype (T, Any_Type);
5063 goto Leave;
5064
5065 elsif not Is_Tagged_Type (Parent_Type) then
5066 Error_Msg_N
5067 ("parent of type extension must be a tagged type ", Indic);
5068 goto Leave;
5069
5070 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5071 Error_Msg_N ("premature derivation of incomplete type", Indic);
5072 goto Leave;
5073
5074 elsif Is_Concurrent_Type (Parent_Type) then
5075 Error_Msg_N
5076 ("parent type of a private extension cannot be a synchronized "
5077 & "tagged type (RM 3.9.1 (3/1))", N);
5078
5079 Set_Etype (T, Any_Type);
5080 Set_Ekind (T, E_Limited_Private_Type);
5081 Set_Private_Dependents (T, New_Elmt_List);
5082 Set_Error_Posted (T);
5083 goto Leave;
5084 end if;
5085
5086 -- Perhaps the parent type should be changed to the class-wide type's
5087 -- specific type in this case to prevent cascading errors ???
5088
5089 if Is_Class_Wide_Type (Parent_Type) then
5090 Error_Msg_N
5091 ("parent of type extension must not be a class-wide type", Indic);
5092 goto Leave;
5093 end if;
5094
5095 if (not Is_Package_Or_Generic_Package (Current_Scope)
5096 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5097 or else In_Private_Part (Current_Scope)
5098 then
5099 Error_Msg_N ("invalid context for private extension", N);
5100 end if;
5101
5102 -- Set common attributes
5103
5104 Set_Is_Pure (T, Is_Pure (Current_Scope));
5105 Set_Scope (T, Current_Scope);
5106 Set_Ekind (T, E_Record_Type_With_Private);
5107 Init_Size_Align (T);
5108 Set_Default_SSO (T);
5109 Set_No_Reordering (T, No_Component_Reordering);
5110
5111 Set_Etype (T, Parent_Base);
5112 Propagate_Concurrent_Flags (T, Parent_Base);
5113
5114 Set_Convention (T, Convention (Parent_Type));
5115 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5116 Set_Is_First_Subtype (T);
5117 Make_Class_Wide_Type (T);
5118
5119 -- Set the SPARK mode from the current context
5120
5121 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5122 Set_SPARK_Pragma_Inherited (T);
5123
5124 if Unknown_Discriminants_Present (N) then
5125 Set_Discriminant_Constraint (T, No_Elist);
5126 end if;
5127
5128 Build_Derived_Record_Type (N, Parent_Type, T);
5129
5130 -- A private extension inherits the Default_Initial_Condition pragma
5131 -- coming from any parent type within the derivation chain.
5132
5133 if Has_DIC (Parent_Type) then
5134 Set_Has_Inherited_DIC (T);
5135 end if;
5136
5137 -- A private extension inherits any class-wide invariants coming from a
5138 -- parent type or an interface. Note that the invariant procedure of the
5139 -- parent type should not be inherited because the private extension may
5140 -- define invariants of its own.
5141
5142 if Has_Inherited_Invariants (Parent_Type)
5143 or else Has_Inheritable_Invariants (Parent_Type)
5144 then
5145 Set_Has_Inherited_Invariants (T);
5146
5147 elsif Present (Interfaces (T)) then
5148 Iface_Elmt := First_Elmt (Interfaces (T));
5149 while Present (Iface_Elmt) loop
5150 Iface := Node (Iface_Elmt);
5151
5152 if Has_Inheritable_Invariants (Iface) then
5153 Set_Has_Inherited_Invariants (T);
5154 exit;
5155 end if;
5156
5157 Next_Elmt (Iface_Elmt);
5158 end loop;
5159 end if;
5160
5161 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5162 -- synchronized formal derived type.
5163
5164 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5165 Set_Is_Limited_Record (T);
5166
5167 -- Formal derived type case
5168
5169 if Is_Generic_Type (T) then
5170
5171 -- The parent must be a tagged limited type or a synchronized
5172 -- interface.
5173
5174 if (not Is_Tagged_Type (Parent_Type)
5175 or else not Is_Limited_Type (Parent_Type))
5176 and then
5177 (not Is_Interface (Parent_Type)
5178 or else not Is_Synchronized_Interface (Parent_Type))
5179 then
5180 Error_Msg_NE
5181 ("parent type of & must be tagged limited or synchronized",
5182 N, T);
5183 end if;
5184
5185 -- The progenitors (if any) must be limited or synchronized
5186 -- interfaces.
5187
5188 if Present (Interfaces (T)) then
5189 Iface_Elmt := First_Elmt (Interfaces (T));
5190 while Present (Iface_Elmt) loop
5191 Iface := Node (Iface_Elmt);
5192
5193 if not Is_Limited_Interface (Iface)
5194 and then not Is_Synchronized_Interface (Iface)
5195 then
5196 Error_Msg_NE
5197 ("progenitor & must be limited or synchronized",
5198 N, Iface);
5199 end if;
5200
5201 Next_Elmt (Iface_Elmt);
5202 end loop;
5203 end if;
5204
5205 -- Regular derived extension, the parent must be a limited or
5206 -- synchronized interface.
5207
5208 else
5209 if not Is_Interface (Parent_Type)
5210 or else (not Is_Limited_Interface (Parent_Type)
5211 and then not Is_Synchronized_Interface (Parent_Type))
5212 then
5213 Error_Msg_NE
5214 ("parent type of & must be limited interface", N, T);
5215 end if;
5216 end if;
5217
5218 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5219 -- extension with a synchronized parent must be explicitly declared
5220 -- synchronized, because the full view will be a synchronized type.
5221 -- This must be checked before the check for limited types below,
5222 -- to ensure that types declared limited are not allowed to extend
5223 -- synchronized interfaces.
5224
5225 elsif Is_Interface (Parent_Type)
5226 and then Is_Synchronized_Interface (Parent_Type)
5227 and then not Synchronized_Present (N)
5228 then
5229 Error_Msg_NE
5230 ("private extension of& must be explicitly synchronized",
5231 N, Parent_Type);
5232
5233 elsif Limited_Present (N) then
5234 Set_Is_Limited_Record (T);
5235
5236 if not Is_Limited_Type (Parent_Type)
5237 and then
5238 (not Is_Interface (Parent_Type)
5239 or else not Is_Limited_Interface (Parent_Type))
5240 then
5241 Error_Msg_NE ("parent type& of limited extension must be limited",
5242 N, Parent_Type);
5243 end if;
5244 end if;
5245
5246 -- Remember that its parent type has a private extension. Used to warn
5247 -- on public primitives of the parent type defined after its private
5248 -- extensions (see Check_Dispatching_Operation).
5249
5250 Set_Has_Private_Extension (Parent_Type);
5251
5252 <<Leave>>
5253 if Has_Aspects (N) then
5254 Analyze_Aspect_Specifications (N, T);
5255 end if;
5256 end Analyze_Private_Extension_Declaration;
5257
5258 ---------------------------------
5259 -- Analyze_Subtype_Declaration --
5260 ---------------------------------
5261
5262 procedure Analyze_Subtype_Declaration
5263 (N : Node_Id;
5264 Skip : Boolean := False)
5265 is
5266 Id : constant Entity_Id := Defining_Identifier (N);
5267 R_Checks : Check_Result;
5268 T : Entity_Id;
5269
5270 begin
5271 Generate_Definition (Id);
5272 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5273 Init_Size_Align (Id);
5274
5275 -- The following guard condition on Enter_Name is to handle cases where
5276 -- the defining identifier has already been entered into the scope but
5277 -- the declaration as a whole needs to be analyzed.
5278
5279 -- This case in particular happens for derived enumeration types. The
5280 -- derived enumeration type is processed as an inserted enumeration type
5281 -- declaration followed by a rewritten subtype declaration. The defining
5282 -- identifier, however, is entered into the name scope very early in the
5283 -- processing of the original type declaration and therefore needs to be
5284 -- avoided here, when the created subtype declaration is analyzed. (See
5285 -- Build_Derived_Types)
5286
5287 -- This also happens when the full view of a private type is derived
5288 -- type with constraints. In this case the entity has been introduced
5289 -- in the private declaration.
5290
5291 -- Finally this happens in some complex cases when validity checks are
5292 -- enabled, where the same subtype declaration may be analyzed twice.
5293 -- This can happen if the subtype is created by the preanalysis of
5294 -- an attribute tht gives the range of a loop statement, and the loop
5295 -- itself appears within an if_statement that will be rewritten during
5296 -- expansion.
5297
5298 if Skip
5299 or else (Present (Etype (Id))
5300 and then (Is_Private_Type (Etype (Id))
5301 or else Is_Task_Type (Etype (Id))
5302 or else Is_Rewrite_Substitution (N)))
5303 then
5304 null;
5305
5306 elsif Current_Entity (Id) = Id then
5307 null;
5308
5309 else
5310 Enter_Name (Id);
5311 end if;
5312
5313 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5314
5315 -- Class-wide equivalent types of records with unknown discriminants
5316 -- involve the generation of an itype which serves as the private view
5317 -- of a constrained record subtype. In such cases the base type of the
5318 -- current subtype we are processing is the private itype. Use the full
5319 -- of the private itype when decorating various attributes.
5320
5321 if Is_Itype (T)
5322 and then Is_Private_Type (T)
5323 and then Present (Full_View (T))
5324 then
5325 T := Full_View (T);
5326 end if;
5327
5328 -- Inherit common attributes
5329
5330 Set_Is_Volatile (Id, Is_Volatile (T));
5331 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5332 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5333 Set_Convention (Id, Convention (T));
5334
5335 -- If ancestor has predicates then so does the subtype, and in addition
5336 -- we must delay the freeze to properly arrange predicate inheritance.
5337
5338 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5339 -- in which T = ID, so the above tests and assignments do nothing???
5340
5341 if Has_Predicates (T)
5342 or else (Present (Ancestor_Subtype (T))
5343 and then Has_Predicates (Ancestor_Subtype (T)))
5344 then
5345 Set_Has_Predicates (Id);
5346 Set_Has_Delayed_Freeze (Id);
5347
5348 -- Generated subtypes inherit the predicate function from the parent
5349 -- (no aspects to examine on the generated declaration).
5350
5351 if not Comes_From_Source (N) then
5352 Set_Ekind (Id, Ekind (T));
5353
5354 if Present (Predicate_Function (Id)) then
5355 null;
5356
5357 elsif Present (Predicate_Function (T)) then
5358 Set_Predicate_Function (Id, Predicate_Function (T));
5359
5360 elsif Present (Ancestor_Subtype (T))
5361 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5362 then
5363 Set_Predicate_Function (Id,
5364 Predicate_Function (Ancestor_Subtype (T)));
5365 end if;
5366 end if;
5367 end if;
5368
5369 -- Subtype of Boolean cannot have a constraint in SPARK
5370
5371 if Is_Boolean_Type (T)
5372 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5373 then
5374 Check_SPARK_05_Restriction
5375 ("subtype of Boolean cannot have constraint", N);
5376 end if;
5377
5378 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5379 declare
5380 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5381 One_Cstr : Node_Id;
5382 Low : Node_Id;
5383 High : Node_Id;
5384
5385 begin
5386 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5387 One_Cstr := First (Constraints (Cstr));
5388 while Present (One_Cstr) loop
5389
5390 -- Index or discriminant constraint in SPARK must be a
5391 -- subtype mark.
5392
5393 if not
5394 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5395 then
5396 Check_SPARK_05_Restriction
5397 ("subtype mark required", One_Cstr);
5398
5399 -- String subtype must have a lower bound of 1 in SPARK.
5400 -- Note that we do not need to test for the non-static case
5401 -- here, since that was already taken care of in
5402 -- Process_Range_Expr_In_Decl.
5403
5404 elsif Base_Type (T) = Standard_String then
5405 Get_Index_Bounds (One_Cstr, Low, High);
5406
5407 if Is_OK_Static_Expression (Low)
5408 and then Expr_Value (Low) /= 1
5409 then
5410 Check_SPARK_05_Restriction
5411 ("String subtype must have lower bound of 1", N);
5412 end if;
5413 end if;
5414
5415 Next (One_Cstr);
5416 end loop;
5417 end if;
5418 end;
5419 end if;
5420
5421 -- In the case where there is no constraint given in the subtype
5422 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5423 -- semantic attributes must be established here.
5424
5425 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5426 Set_Etype (Id, Base_Type (T));
5427
5428 -- Subtype of unconstrained array without constraint is not allowed
5429 -- in SPARK.
5430
5431 if Is_Array_Type (T) and then not Is_Constrained (T) then
5432 Check_SPARK_05_Restriction
5433 ("subtype of unconstrained array must have constraint", N);
5434 end if;
5435
5436 case Ekind (T) is
5437 when Array_Kind =>
5438 Set_Ekind (Id, E_Array_Subtype);
5439 Copy_Array_Subtype_Attributes (Id, T);
5440
5441 when Decimal_Fixed_Point_Kind =>
5442 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5443 Set_Digits_Value (Id, Digits_Value (T));
5444 Set_Delta_Value (Id, Delta_Value (T));
5445 Set_Scale_Value (Id, Scale_Value (T));
5446 Set_Small_Value (Id, Small_Value (T));
5447 Set_Scalar_Range (Id, Scalar_Range (T));
5448 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5449 Set_Is_Constrained (Id, Is_Constrained (T));
5450 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5451 Set_RM_Size (Id, RM_Size (T));
5452
5453 when Enumeration_Kind =>
5454 Set_Ekind (Id, E_Enumeration_Subtype);
5455 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5456 Set_Scalar_Range (Id, Scalar_Range (T));
5457 Set_Is_Character_Type (Id, Is_Character_Type (T));
5458 Set_Is_Constrained (Id, Is_Constrained (T));
5459 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5460 Set_RM_Size (Id, RM_Size (T));
5461
5462 when Ordinary_Fixed_Point_Kind =>
5463 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5464 Set_Scalar_Range (Id, Scalar_Range (T));
5465 Set_Small_Value (Id, Small_Value (T));
5466 Set_Delta_Value (Id, Delta_Value (T));
5467 Set_Is_Constrained (Id, Is_Constrained (T));
5468 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5469 Set_RM_Size (Id, RM_Size (T));
5470
5471 when Float_Kind =>
5472 Set_Ekind (Id, E_Floating_Point_Subtype);
5473 Set_Scalar_Range (Id, Scalar_Range (T));
5474 Set_Digits_Value (Id, Digits_Value (T));
5475 Set_Is_Constrained (Id, Is_Constrained (T));
5476
5477 -- If the floating point type has dimensions, these will be
5478 -- inherited subsequently when Analyze_Dimensions is called.
5479
5480 when Signed_Integer_Kind =>
5481 Set_Ekind (Id, E_Signed_Integer_Subtype);
5482 Set_Scalar_Range (Id, Scalar_Range (T));
5483 Set_Is_Constrained (Id, Is_Constrained (T));
5484 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5485 Set_RM_Size (Id, RM_Size (T));
5486
5487 when Modular_Integer_Kind =>
5488 Set_Ekind (Id, E_Modular_Integer_Subtype);
5489 Set_Scalar_Range (Id, Scalar_Range (T));
5490 Set_Is_Constrained (Id, Is_Constrained (T));
5491 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5492 Set_RM_Size (Id, RM_Size (T));
5493
5494 when Class_Wide_Kind =>
5495 Set_Ekind (Id, E_Class_Wide_Subtype);
5496 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5497 Set_Cloned_Subtype (Id, T);
5498 Set_Is_Tagged_Type (Id, True);
5499 Set_Has_Unknown_Discriminants
5500 (Id, True);
5501 Set_No_Tagged_Streams_Pragma
5502 (Id, No_Tagged_Streams_Pragma (T));
5503
5504 if Ekind (T) = E_Class_Wide_Subtype then
5505 Set_Equivalent_Type (Id, Equivalent_Type (T));
5506 end if;
5507
5508 when E_Record_Subtype
5509 | E_Record_Type
5510 =>
5511 Set_Ekind (Id, E_Record_Subtype);
5512
5513 if Ekind (T) = E_Record_Subtype
5514 and then Present (Cloned_Subtype (T))
5515 then
5516 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5517 else
5518 Set_Cloned_Subtype (Id, T);
5519 end if;
5520
5521 Set_First_Entity (Id, First_Entity (T));
5522 Set_Last_Entity (Id, Last_Entity (T));
5523 Set_Has_Discriminants (Id, Has_Discriminants (T));
5524 Set_Is_Constrained (Id, Is_Constrained (T));
5525 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5526 Set_Has_Implicit_Dereference
5527 (Id, Has_Implicit_Dereference (T));
5528 Set_Has_Unknown_Discriminants
5529 (Id, Has_Unknown_Discriminants (T));
5530
5531 if Has_Discriminants (T) then
5532 Set_Discriminant_Constraint
5533 (Id, Discriminant_Constraint (T));
5534 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5535
5536 elsif Has_Unknown_Discriminants (Id) then
5537 Set_Discriminant_Constraint (Id, No_Elist);
5538 end if;
5539
5540 if Is_Tagged_Type (T) then
5541 Set_Is_Tagged_Type (Id, True);
5542 Set_No_Tagged_Streams_Pragma
5543 (Id, No_Tagged_Streams_Pragma (T));
5544 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5545 Set_Direct_Primitive_Operations
5546 (Id, Direct_Primitive_Operations (T));
5547 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5548
5549 if Is_Interface (T) then
5550 Set_Is_Interface (Id);
5551 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5552 end if;
5553 end if;
5554
5555 when Private_Kind =>
5556 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5557 Set_Has_Discriminants (Id, Has_Discriminants (T));
5558 Set_Is_Constrained (Id, Is_Constrained (T));
5559 Set_First_Entity (Id, First_Entity (T));
5560 Set_Last_Entity (Id, Last_Entity (T));
5561 Set_Private_Dependents (Id, New_Elmt_List);
5562 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5563 Set_Has_Implicit_Dereference
5564 (Id, Has_Implicit_Dereference (T));
5565 Set_Has_Unknown_Discriminants
5566 (Id, Has_Unknown_Discriminants (T));
5567 Set_Known_To_Have_Preelab_Init
5568 (Id, Known_To_Have_Preelab_Init (T));
5569
5570 if Is_Tagged_Type (T) then
5571 Set_Is_Tagged_Type (Id);
5572 Set_No_Tagged_Streams_Pragma (Id,
5573 No_Tagged_Streams_Pragma (T));
5574 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5575 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5576 Set_Direct_Primitive_Operations (Id,
5577 Direct_Primitive_Operations (T));
5578 end if;
5579
5580 -- In general the attributes of the subtype of a private type
5581 -- are the attributes of the partial view of parent. However,
5582 -- the full view may be a discriminated type, and the subtype
5583 -- must share the discriminant constraint to generate correct
5584 -- calls to initialization procedures.
5585
5586 if Has_Discriminants (T) then
5587 Set_Discriminant_Constraint
5588 (Id, Discriminant_Constraint (T));
5589 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5590
5591 elsif Present (Full_View (T))
5592 and then Has_Discriminants (Full_View (T))
5593 then
5594 Set_Discriminant_Constraint
5595 (Id, Discriminant_Constraint (Full_View (T)));
5596 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5597
5598 -- This would seem semantically correct, but apparently
5599 -- generates spurious errors about missing components ???
5600
5601 -- Set_Has_Discriminants (Id);
5602 end if;
5603
5604 Prepare_Private_Subtype_Completion (Id, N);
5605
5606 -- If this is the subtype of a constrained private type with
5607 -- discriminants that has got a full view and we also have
5608 -- built a completion just above, show that the completion
5609 -- is a clone of the full view to the back-end.
5610
5611 if Has_Discriminants (T)
5612 and then not Has_Unknown_Discriminants (T)
5613 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5614 and then Present (Full_View (T))
5615 and then Present (Full_View (Id))
5616 then
5617 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5618 end if;
5619
5620 when Access_Kind =>
5621 Set_Ekind (Id, E_Access_Subtype);
5622 Set_Is_Constrained (Id, Is_Constrained (T));
5623 Set_Is_Access_Constant
5624 (Id, Is_Access_Constant (T));
5625 Set_Directly_Designated_Type
5626 (Id, Designated_Type (T));
5627 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5628
5629 -- A Pure library_item must not contain the declaration of a
5630 -- named access type, except within a subprogram, generic
5631 -- subprogram, task unit, or protected unit, or if it has
5632 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5633
5634 if Comes_From_Source (Id)
5635 and then In_Pure_Unit
5636 and then not In_Subprogram_Task_Protected_Unit
5637 and then not No_Pool_Assigned (Id)
5638 then
5639 Error_Msg_N
5640 ("named access types not allowed in pure unit", N);
5641 end if;
5642
5643 when Concurrent_Kind =>
5644 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5645 Set_Corresponding_Record_Type (Id,
5646 Corresponding_Record_Type (T));
5647 Set_First_Entity (Id, First_Entity (T));
5648 Set_First_Private_Entity (Id, First_Private_Entity (T));
5649 Set_Has_Discriminants (Id, Has_Discriminants (T));
5650 Set_Is_Constrained (Id, Is_Constrained (T));
5651 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5652 Set_Last_Entity (Id, Last_Entity (T));
5653
5654 if Is_Tagged_Type (T) then
5655 Set_No_Tagged_Streams_Pragma
5656 (Id, No_Tagged_Streams_Pragma (T));
5657 end if;
5658
5659 if Has_Discriminants (T) then
5660 Set_Discriminant_Constraint
5661 (Id, Discriminant_Constraint (T));
5662 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5663 end if;
5664
5665 when Incomplete_Kind =>
5666 if Ada_Version >= Ada_2005 then
5667
5668 -- In Ada 2005 an incomplete type can be explicitly tagged:
5669 -- propagate indication. Note that we also have to include
5670 -- subtypes for Ada 2012 extended use of incomplete types.
5671
5672 Set_Ekind (Id, E_Incomplete_Subtype);
5673 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5674 Set_Private_Dependents (Id, New_Elmt_List);
5675
5676 if Is_Tagged_Type (Id) then
5677 Set_No_Tagged_Streams_Pragma
5678 (Id, No_Tagged_Streams_Pragma (T));
5679 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5680 end if;
5681
5682 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5683 -- incomplete type visible through a limited with clause.
5684
5685 if From_Limited_With (T)
5686 and then Present (Non_Limited_View (T))
5687 then
5688 Set_From_Limited_With (Id);
5689 Set_Non_Limited_View (Id, Non_Limited_View (T));
5690
5691 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5692 -- to the private dependents of the original incomplete
5693 -- type for future transformation.
5694
5695 else
5696 Append_Elmt (Id, Private_Dependents (T));
5697 end if;
5698
5699 -- If the subtype name denotes an incomplete type an error
5700 -- was already reported by Process_Subtype.
5701
5702 else
5703 Set_Etype (Id, Any_Type);
5704 end if;
5705
5706 when others =>
5707 raise Program_Error;
5708 end case;
5709
5710 -- If there is no constraint in the subtype indication, the
5711 -- declared entity inherits predicates from the parent.
5712
5713 Inherit_Predicate_Flags (Id, T);
5714 end if;
5715
5716 if Etype (Id) = Any_Type then
5717 goto Leave;
5718 end if;
5719
5720 -- Some common processing on all types
5721
5722 Set_Size_Info (Id, T);
5723 Set_First_Rep_Item (Id, First_Rep_Item (T));
5724
5725 -- If the parent type is a generic actual, so is the subtype. This may
5726 -- happen in a nested instance. Why Comes_From_Source test???
5727
5728 if not Comes_From_Source (N) then
5729 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5730 end if;
5731
5732 -- If this is a subtype declaration for an actual in an instance,
5733 -- inherit static and dynamic predicates if any.
5734
5735 -- If declaration has no aspect specifications, inherit predicate
5736 -- info as well. Unclear how to handle the case of both specified
5737 -- and inherited predicates ??? Other inherited aspects, such as
5738 -- invariants, should be OK, but the combination with later pragmas
5739 -- may also require special merging.
5740
5741 if Has_Predicates (T)
5742 and then Present (Predicate_Function (T))
5743 and then
5744 ((In_Instance and then not Comes_From_Source (N))
5745 or else No (Aspect_Specifications (N)))
5746 then
5747 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5748
5749 if Has_Static_Predicate (T) then
5750 Set_Has_Static_Predicate (Id);
5751 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5752 end if;
5753 end if;
5754
5755 -- Remaining processing depends on characteristics of base type
5756
5757 T := Etype (Id);
5758
5759 Set_Is_Immediately_Visible (Id, True);
5760 Set_Depends_On_Private (Id, Has_Private_Component (T));
5761 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5762
5763 if Is_Interface (T) then
5764 Set_Is_Interface (Id);
5765 end if;
5766
5767 if Present (Generic_Parent_Type (N))
5768 and then
5769 (Nkind (Parent (Generic_Parent_Type (N))) /=
5770 N_Formal_Type_Declaration
5771 or else Nkind (Formal_Type_Definition
5772 (Parent (Generic_Parent_Type (N)))) /=
5773 N_Formal_Private_Type_Definition)
5774 then
5775 if Is_Tagged_Type (Id) then
5776
5777 -- If this is a generic actual subtype for a synchronized type,
5778 -- the primitive operations are those of the corresponding record
5779 -- for which there is a separate subtype declaration.
5780
5781 if Is_Concurrent_Type (Id) then
5782 null;
5783 elsif Is_Class_Wide_Type (Id) then
5784 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5785 else
5786 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5787 end if;
5788
5789 elsif Scope (Etype (Id)) /= Standard_Standard then
5790 Derive_Subprograms (Generic_Parent_Type (N), Id);
5791 end if;
5792 end if;
5793
5794 if Is_Private_Type (T) and then Present (Full_View (T)) then
5795 Conditional_Delay (Id, Full_View (T));
5796
5797 -- The subtypes of components or subcomponents of protected types
5798 -- do not need freeze nodes, which would otherwise appear in the
5799 -- wrong scope (before the freeze node for the protected type). The
5800 -- proper subtypes are those of the subcomponents of the corresponding
5801 -- record.
5802
5803 elsif Ekind (Scope (Id)) /= E_Protected_Type
5804 and then Present (Scope (Scope (Id))) -- error defense
5805 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5806 then
5807 Conditional_Delay (Id, T);
5808 end if;
5809
5810 -- If we have a subtype of an incomplete type whose full type is a
5811 -- derived numeric type, we need to have a freeze node for the subtype.
5812 -- Otherwise gigi will complain while computing the (static) bounds of
5813 -- the subtype.
5814
5815 if Is_Itype (T)
5816 and then Is_Elementary_Type (Id)
5817 and then Etype (Id) /= Id
5818 then
5819 declare
5820 Partial : constant Entity_Id :=
5821 Incomplete_Or_Partial_View (First_Subtype (Id));
5822 begin
5823 if Present (Partial)
5824 and then Ekind (Partial) = E_Incomplete_Type
5825 then
5826 Set_Has_Delayed_Freeze (Id);
5827 end if;
5828 end;
5829 end if;
5830
5831 -- Check that Constraint_Error is raised for a scalar subtype indication
5832 -- when the lower or upper bound of a non-null range lies outside the
5833 -- range of the type mark.
5834
5835 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5836 if Is_Scalar_Type (Etype (Id))
5837 and then Scalar_Range (Id) /=
5838 Scalar_Range
5839 (Etype (Subtype_Mark (Subtype_Indication (N))))
5840 then
5841 Apply_Range_Check
5842 (Scalar_Range (Id),
5843 Etype (Subtype_Mark (Subtype_Indication (N))));
5844
5845 -- In the array case, check compatibility for each index
5846
5847 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5848 then
5849 -- This really should be a subprogram that finds the indications
5850 -- to check???
5851
5852 declare
5853 Subt_Index : Node_Id := First_Index (Id);
5854 Target_Index : Node_Id :=
5855 First_Index (Etype
5856 (Subtype_Mark (Subtype_Indication (N))));
5857 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5858
5859 begin
5860 while Present (Subt_Index) loop
5861 if ((Nkind (Subt_Index) = N_Identifier
5862 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5863 or else Nkind (Subt_Index) = N_Subtype_Indication)
5864 and then
5865 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5866 then
5867 declare
5868 Target_Typ : constant Entity_Id :=
5869 Etype (Target_Index);
5870 begin
5871 R_Checks :=
5872 Get_Range_Checks
5873 (Scalar_Range (Etype (Subt_Index)),
5874 Target_Typ,
5875 Etype (Subt_Index),
5876 Defining_Identifier (N));
5877
5878 -- Reset Has_Dynamic_Range_Check on the subtype to
5879 -- prevent elision of the index check due to a dynamic
5880 -- check generated for a preceding index (needed since
5881 -- Insert_Range_Checks tries to avoid generating
5882 -- redundant checks on a given declaration).
5883
5884 Set_Has_Dynamic_Range_Check (N, False);
5885
5886 Insert_Range_Checks
5887 (R_Checks,
5888 N,
5889 Target_Typ,
5890 Sloc (Defining_Identifier (N)));
5891
5892 -- Record whether this index involved a dynamic check
5893
5894 Has_Dyn_Chk :=
5895 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5896 end;
5897 end if;
5898
5899 Next_Index (Subt_Index);
5900 Next_Index (Target_Index);
5901 end loop;
5902
5903 -- Finally, mark whether the subtype involves dynamic checks
5904
5905 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5906 end;
5907 end if;
5908 end if;
5909
5910 Set_Optimize_Alignment_Flags (Id);
5911 Check_Eliminated (Id);
5912
5913 <<Leave>>
5914 if Has_Aspects (N) then
5915 Analyze_Aspect_Specifications (N, Id);
5916 end if;
5917
5918 Analyze_Dimension (N);
5919
5920 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5921 -- indications on composite types where the constraints are dynamic.
5922 -- Note that object declarations and aggregates generate implicit
5923 -- subtype declarations, which this covers. One special case is that the
5924 -- implicitly generated "=" for discriminated types includes an
5925 -- offending subtype declaration, which is harmless, so we ignore it
5926 -- here.
5927
5928 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5929 declare
5930 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5931 begin
5932 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5933 and then not (Is_Internal (Id)
5934 and then Is_TSS (Scope (Id),
5935 TSS_Composite_Equality))
5936 and then not Within_Init_Proc
5937 and then not All_Composite_Constraints_Static (Cstr)
5938 then
5939 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5940 end if;
5941 end;
5942 end if;
5943 end Analyze_Subtype_Declaration;
5944
5945 --------------------------------
5946 -- Analyze_Subtype_Indication --
5947 --------------------------------
5948
5949 procedure Analyze_Subtype_Indication (N : Node_Id) is
5950 T : constant Entity_Id := Subtype_Mark (N);
5951 R : constant Node_Id := Range_Expression (Constraint (N));
5952
5953 begin
5954 Analyze (T);
5955
5956 if R /= Error then
5957 Analyze (R);
5958 Set_Etype (N, Etype (R));
5959 Resolve (R, Entity (T));
5960 else
5961 Set_Error_Posted (R);
5962 Set_Error_Posted (T);
5963 end if;
5964 end Analyze_Subtype_Indication;
5965
5966 --------------------------
5967 -- Analyze_Variant_Part --
5968 --------------------------
5969
5970 procedure Analyze_Variant_Part (N : Node_Id) is
5971 Discr_Name : Node_Id;
5972 Discr_Type : Entity_Id;
5973
5974 procedure Process_Variant (A : Node_Id);
5975 -- Analyze declarations for a single variant
5976
5977 package Analyze_Variant_Choices is
5978 new Generic_Analyze_Choices (Process_Variant);
5979 use Analyze_Variant_Choices;
5980
5981 ---------------------
5982 -- Process_Variant --
5983 ---------------------
5984
5985 procedure Process_Variant (A : Node_Id) is
5986 CL : constant Node_Id := Component_List (A);
5987 begin
5988 if not Null_Present (CL) then
5989 Analyze_Declarations (Component_Items (CL));
5990
5991 if Present (Variant_Part (CL)) then
5992 Analyze (Variant_Part (CL));
5993 end if;
5994 end if;
5995 end Process_Variant;
5996
5997 -- Start of processing for Analyze_Variant_Part
5998
5999 begin
6000 Discr_Name := Name (N);
6001 Analyze (Discr_Name);
6002
6003 -- If Discr_Name bad, get out (prevent cascaded errors)
6004
6005 if Etype (Discr_Name) = Any_Type then
6006 return;
6007 end if;
6008
6009 -- Check invalid discriminant in variant part
6010
6011 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6012 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6013 end if;
6014
6015 Discr_Type := Etype (Entity (Discr_Name));
6016
6017 if not Is_Discrete_Type (Discr_Type) then
6018 Error_Msg_N
6019 ("discriminant in a variant part must be of a discrete type",
6020 Name (N));
6021 return;
6022 end if;
6023
6024 -- Now analyze the choices, which also analyzes the declarations that
6025 -- are associated with each choice.
6026
6027 Analyze_Choices (Variants (N), Discr_Type);
6028
6029 -- Note: we used to instantiate and call Check_Choices here to check
6030 -- that the choices covered the discriminant, but it's too early to do
6031 -- that because of statically predicated subtypes, whose analysis may
6032 -- be deferred to their freeze point which may be as late as the freeze
6033 -- point of the containing record. So this call is now to be found in
6034 -- Freeze_Record_Declaration.
6035
6036 end Analyze_Variant_Part;
6037
6038 ----------------------------
6039 -- Array_Type_Declaration --
6040 ----------------------------
6041
6042 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6043 Component_Def : constant Node_Id := Component_Definition (Def);
6044 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6045 P : constant Node_Id := Parent (Def);
6046 Element_Type : Entity_Id;
6047 Implicit_Base : Entity_Id;
6048 Index : Node_Id;
6049 Nb_Index : Nat;
6050 Priv : Entity_Id;
6051 Related_Id : Entity_Id := Empty;
6052
6053 begin
6054 if Nkind (Def) = N_Constrained_Array_Definition then
6055 Index := First (Discrete_Subtype_Definitions (Def));
6056 else
6057 Index := First (Subtype_Marks (Def));
6058 end if;
6059
6060 -- Find proper names for the implicit types which may be public. In case
6061 -- of anonymous arrays we use the name of the first object of that type
6062 -- as prefix.
6063
6064 if No (T) then
6065 Related_Id := Defining_Identifier (P);
6066 else
6067 Related_Id := T;
6068 end if;
6069
6070 Nb_Index := 1;
6071 while Present (Index) loop
6072 Analyze (Index);
6073
6074 -- Test for odd case of trying to index a type by the type itself
6075
6076 if Is_Entity_Name (Index) and then Entity (Index) = T then
6077 Error_Msg_N ("type& cannot be indexed by itself", Index);
6078 Set_Entity (Index, Standard_Boolean);
6079 Set_Etype (Index, Standard_Boolean);
6080 end if;
6081
6082 -- Check SPARK restriction requiring a subtype mark
6083
6084 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6085 Check_SPARK_05_Restriction ("subtype mark required", Index);
6086 end if;
6087
6088 -- Add a subtype declaration for each index of private array type
6089 -- declaration whose etype is also private. For example:
6090
6091 -- package Pkg is
6092 -- type Index is private;
6093 -- private
6094 -- type Table is array (Index) of ...
6095 -- end;
6096
6097 -- This is currently required by the expander for the internally
6098 -- generated equality subprogram of records with variant parts in
6099 -- which the etype of some component is such private type.
6100
6101 if Ekind (Current_Scope) = E_Package
6102 and then In_Private_Part (Current_Scope)
6103 and then Has_Private_Declaration (Etype (Index))
6104 then
6105 declare
6106 Loc : constant Source_Ptr := Sloc (Def);
6107 Decl : Entity_Id;
6108 New_E : Entity_Id;
6109
6110 begin
6111 New_E := Make_Temporary (Loc, 'T');
6112 Set_Is_Internal (New_E);
6113
6114 Decl :=
6115 Make_Subtype_Declaration (Loc,
6116 Defining_Identifier => New_E,
6117 Subtype_Indication =>
6118 New_Occurrence_Of (Etype (Index), Loc));
6119
6120 Insert_Before (Parent (Def), Decl);
6121 Analyze (Decl);
6122 Set_Etype (Index, New_E);
6123
6124 -- If the index is a range or a subtype indication it carries
6125 -- no entity. Example:
6126
6127 -- package Pkg is
6128 -- type T is private;
6129 -- private
6130 -- type T is new Natural;
6131 -- Table : array (T(1) .. T(10)) of Boolean;
6132 -- end Pkg;
6133
6134 -- Otherwise the type of the reference is its entity.
6135
6136 if Is_Entity_Name (Index) then
6137 Set_Entity (Index, New_E);
6138 end if;
6139 end;
6140 end if;
6141
6142 Make_Index (Index, P, Related_Id, Nb_Index);
6143
6144 -- Check error of subtype with predicate for index type
6145
6146 Bad_Predicated_Subtype_Use
6147 ("subtype& has predicate, not allowed as index subtype",
6148 Index, Etype (Index));
6149
6150 -- Move to next index
6151
6152 Next_Index (Index);
6153 Nb_Index := Nb_Index + 1;
6154 end loop;
6155
6156 -- Process subtype indication if one is present
6157
6158 if Present (Component_Typ) then
6159 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6160
6161 Set_Etype (Component_Typ, Element_Type);
6162
6163 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6164 Check_SPARK_05_Restriction
6165 ("subtype mark required", Component_Typ);
6166 end if;
6167
6168 -- Ada 2005 (AI-230): Access Definition case
6169
6170 else pragma Assert (Present (Access_Definition (Component_Def)));
6171
6172 -- Indicate that the anonymous access type is created by the
6173 -- array type declaration.
6174
6175 Element_Type := Access_Definition
6176 (Related_Nod => P,
6177 N => Access_Definition (Component_Def));
6178 Set_Is_Local_Anonymous_Access (Element_Type);
6179
6180 -- Propagate the parent. This field is needed if we have to generate
6181 -- the master_id associated with an anonymous access to task type
6182 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6183
6184 Set_Parent (Element_Type, Parent (T));
6185
6186 -- Ada 2005 (AI-230): In case of components that are anonymous access
6187 -- types the level of accessibility depends on the enclosing type
6188 -- declaration
6189
6190 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6191
6192 -- Ada 2005 (AI-254)
6193
6194 declare
6195 CD : constant Node_Id :=
6196 Access_To_Subprogram_Definition
6197 (Access_Definition (Component_Def));
6198 begin
6199 if Present (CD) and then Protected_Present (CD) then
6200 Element_Type :=
6201 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6202 end if;
6203 end;
6204 end if;
6205
6206 -- Constrained array case
6207
6208 if No (T) then
6209 T := Create_Itype (E_Void, P, Related_Id, 'T');
6210 end if;
6211
6212 if Nkind (Def) = N_Constrained_Array_Definition then
6213
6214 -- Establish Implicit_Base as unconstrained base type
6215
6216 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6217
6218 Set_Etype (Implicit_Base, Implicit_Base);
6219 Set_Scope (Implicit_Base, Current_Scope);
6220 Set_Has_Delayed_Freeze (Implicit_Base);
6221 Set_Default_SSO (Implicit_Base);
6222
6223 -- The constrained array type is a subtype of the unconstrained one
6224
6225 Set_Ekind (T, E_Array_Subtype);
6226 Init_Size_Align (T);
6227 Set_Etype (T, Implicit_Base);
6228 Set_Scope (T, Current_Scope);
6229 Set_Is_Constrained (T);
6230 Set_First_Index (T,
6231 First (Discrete_Subtype_Definitions (Def)));
6232 Set_Has_Delayed_Freeze (T);
6233
6234 -- Complete setup of implicit base type
6235
6236 Set_Component_Size (Implicit_Base, Uint_0);
6237 Set_Component_Type (Implicit_Base, Element_Type);
6238 Set_Finalize_Storage_Only
6239 (Implicit_Base,
6240 Finalize_Storage_Only (Element_Type));
6241 Set_First_Index (Implicit_Base, First_Index (T));
6242 Set_Has_Controlled_Component
6243 (Implicit_Base,
6244 Has_Controlled_Component (Element_Type)
6245 or else Is_Controlled (Element_Type));
6246 Set_Packed_Array_Impl_Type
6247 (Implicit_Base, Empty);
6248
6249 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6250
6251 -- Unconstrained array case
6252
6253 else
6254 Set_Ekind (T, E_Array_Type);
6255 Init_Size_Align (T);
6256 Set_Etype (T, T);
6257 Set_Scope (T, Current_Scope);
6258 Set_Component_Size (T, Uint_0);
6259 Set_Is_Constrained (T, False);
6260 Set_First_Index (T, First (Subtype_Marks (Def)));
6261 Set_Has_Delayed_Freeze (T, True);
6262 Propagate_Concurrent_Flags (T, Element_Type);
6263 Set_Has_Controlled_Component (T, Has_Controlled_Component
6264 (Element_Type)
6265 or else
6266 Is_Controlled (Element_Type));
6267 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6268 (Element_Type));
6269 Set_Default_SSO (T);
6270 end if;
6271
6272 -- Common attributes for both cases
6273
6274 Set_Component_Type (Base_Type (T), Element_Type);
6275 Set_Packed_Array_Impl_Type (T, Empty);
6276
6277 if Aliased_Present (Component_Definition (Def)) then
6278 Check_SPARK_05_Restriction
6279 ("aliased is not allowed", Component_Definition (Def));
6280 Set_Has_Aliased_Components (Etype (T));
6281 end if;
6282
6283 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6284 -- array type to ensure that objects of this type are initialized.
6285
6286 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6287 Set_Can_Never_Be_Null (T);
6288
6289 if Null_Exclusion_Present (Component_Definition (Def))
6290
6291 -- No need to check itypes because in their case this check was
6292 -- done at their point of creation
6293
6294 and then not Is_Itype (Element_Type)
6295 then
6296 Error_Msg_N
6297 ("`NOT NULL` not allowed (null already excluded)",
6298 Subtype_Indication (Component_Definition (Def)));
6299 end if;
6300 end if;
6301
6302 Priv := Private_Component (Element_Type);
6303
6304 if Present (Priv) then
6305
6306 -- Check for circular definitions
6307
6308 if Priv = Any_Type then
6309 Set_Component_Type (Etype (T), Any_Type);
6310
6311 -- There is a gap in the visibility of operations on the composite
6312 -- type only if the component type is defined in a different scope.
6313
6314 elsif Scope (Priv) = Current_Scope then
6315 null;
6316
6317 elsif Is_Limited_Type (Priv) then
6318 Set_Is_Limited_Composite (Etype (T));
6319 Set_Is_Limited_Composite (T);
6320 else
6321 Set_Is_Private_Composite (Etype (T));
6322 Set_Is_Private_Composite (T);
6323 end if;
6324 end if;
6325
6326 -- A syntax error in the declaration itself may lead to an empty index
6327 -- list, in which case do a minimal patch.
6328
6329 if No (First_Index (T)) then
6330 Error_Msg_N ("missing index definition in array type declaration", T);
6331
6332 declare
6333 Indexes : constant List_Id :=
6334 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6335 begin
6336 Set_Discrete_Subtype_Definitions (Def, Indexes);
6337 Set_First_Index (T, First (Indexes));
6338 return;
6339 end;
6340 end if;
6341
6342 -- Create a concatenation operator for the new type. Internal array
6343 -- types created for packed entities do not need such, they are
6344 -- compatible with the user-defined type.
6345
6346 if Number_Dimensions (T) = 1
6347 and then not Is_Packed_Array_Impl_Type (T)
6348 then
6349 New_Concatenation_Op (T);
6350 end if;
6351
6352 -- In the case of an unconstrained array the parser has already verified
6353 -- that all the indexes are unconstrained but we still need to make sure
6354 -- that the element type is constrained.
6355
6356 if not Is_Definite_Subtype (Element_Type) then
6357 Error_Msg_N
6358 ("unconstrained element type in array declaration",
6359 Subtype_Indication (Component_Def));
6360
6361 elsif Is_Abstract_Type (Element_Type) then
6362 Error_Msg_N
6363 ("the type of a component cannot be abstract",
6364 Subtype_Indication (Component_Def));
6365 end if;
6366
6367 -- There may be an invariant declared for the component type, but
6368 -- the construction of the component invariant checking procedure
6369 -- takes place during expansion.
6370 end Array_Type_Declaration;
6371
6372 ------------------------------------------------------
6373 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6374 ------------------------------------------------------
6375
6376 function Replace_Anonymous_Access_To_Protected_Subprogram
6377 (N : Node_Id) return Entity_Id
6378 is
6379 Loc : constant Source_Ptr := Sloc (N);
6380
6381 Curr_Scope : constant Scope_Stack_Entry :=
6382 Scope_Stack.Table (Scope_Stack.Last);
6383
6384 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6385
6386 Acc : Node_Id;
6387 -- Access definition in declaration
6388
6389 Comp : Node_Id;
6390 -- Object definition or formal definition with an access definition
6391
6392 Decl : Node_Id;
6393 -- Declaration of anonymous access to subprogram type
6394
6395 Spec : Node_Id;
6396 -- Original specification in access to subprogram
6397
6398 P : Node_Id;
6399
6400 begin
6401 Set_Is_Internal (Anon);
6402
6403 case Nkind (N) is
6404 when N_Constrained_Array_Definition
6405 | N_Component_Declaration
6406 | N_Unconstrained_Array_Definition
6407 =>
6408 Comp := Component_Definition (N);
6409 Acc := Access_Definition (Comp);
6410
6411 when N_Discriminant_Specification =>
6412 Comp := Discriminant_Type (N);
6413 Acc := Comp;
6414
6415 when N_Parameter_Specification =>
6416 Comp := Parameter_Type (N);
6417 Acc := Comp;
6418
6419 when N_Access_Function_Definition =>
6420 Comp := Result_Definition (N);
6421 Acc := Comp;
6422
6423 when N_Object_Declaration =>
6424 Comp := Object_Definition (N);
6425 Acc := Comp;
6426
6427 when N_Function_Specification =>
6428 Comp := Result_Definition (N);
6429 Acc := Comp;
6430
6431 when others =>
6432 raise Program_Error;
6433 end case;
6434
6435 Spec := Access_To_Subprogram_Definition (Acc);
6436
6437 Decl :=
6438 Make_Full_Type_Declaration (Loc,
6439 Defining_Identifier => Anon,
6440 Type_Definition => Copy_Separate_Tree (Spec));
6441
6442 Mark_Rewrite_Insertion (Decl);
6443
6444 -- In ASIS mode, analyze the profile on the original node, because
6445 -- the separate copy does not provide enough links to recover the
6446 -- original tree. Analysis is limited to type annotations, within
6447 -- a temporary scope that serves as an anonymous subprogram to collect
6448 -- otherwise useless temporaries and itypes.
6449
6450 if ASIS_Mode then
6451 declare
6452 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6453
6454 begin
6455 if Nkind (Spec) = N_Access_Function_Definition then
6456 Set_Ekind (Typ, E_Function);
6457 else
6458 Set_Ekind (Typ, E_Procedure);
6459 end if;
6460
6461 Set_Parent (Typ, N);
6462 Set_Scope (Typ, Current_Scope);
6463 Push_Scope (Typ);
6464
6465 -- Nothing to do if procedure is parameterless
6466
6467 if Present (Parameter_Specifications (Spec)) then
6468 Process_Formals (Parameter_Specifications (Spec), Spec);
6469 end if;
6470
6471 if Nkind (Spec) = N_Access_Function_Definition then
6472 declare
6473 Def : constant Node_Id := Result_Definition (Spec);
6474
6475 begin
6476 -- The result might itself be an anonymous access type, so
6477 -- have to recurse.
6478
6479 if Nkind (Def) = N_Access_Definition then
6480 if Present (Access_To_Subprogram_Definition (Def)) then
6481 Set_Etype
6482 (Def,
6483 Replace_Anonymous_Access_To_Protected_Subprogram
6484 (Spec));
6485 else
6486 Find_Type (Subtype_Mark (Def));
6487 end if;
6488
6489 else
6490 Find_Type (Def);
6491 end if;
6492 end;
6493 end if;
6494
6495 End_Scope;
6496 end;
6497 end if;
6498
6499 -- Insert the new declaration in the nearest enclosing scope. If the
6500 -- parent is a body and N is its return type, the declaration belongs
6501 -- in the enclosing scope. Likewise if N is the type of a parameter.
6502
6503 P := Parent (N);
6504
6505 if Nkind (N) = N_Function_Specification
6506 and then Nkind (P) = N_Subprogram_Body
6507 then
6508 P := Parent (P);
6509 elsif Nkind (N) = N_Parameter_Specification
6510 and then Nkind (P) in N_Subprogram_Specification
6511 and then Nkind (Parent (P)) = N_Subprogram_Body
6512 then
6513 P := Parent (Parent (P));
6514 end if;
6515
6516 while Present (P) and then not Has_Declarations (P) loop
6517 P := Parent (P);
6518 end loop;
6519
6520 pragma Assert (Present (P));
6521
6522 if Nkind (P) = N_Package_Specification then
6523 Prepend (Decl, Visible_Declarations (P));
6524 else
6525 Prepend (Decl, Declarations (P));
6526 end if;
6527
6528 -- Replace the anonymous type with an occurrence of the new declaration.
6529 -- In all cases the rewritten node does not have the null-exclusion
6530 -- attribute because (if present) it was already inherited by the
6531 -- anonymous entity (Anon). Thus, in case of components we do not
6532 -- inherit this attribute.
6533
6534 if Nkind (N) = N_Parameter_Specification then
6535 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6536 Set_Etype (Defining_Identifier (N), Anon);
6537 Set_Null_Exclusion_Present (N, False);
6538
6539 elsif Nkind (N) = N_Object_Declaration then
6540 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6541 Set_Etype (Defining_Identifier (N), Anon);
6542
6543 elsif Nkind (N) = N_Access_Function_Definition then
6544 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6545
6546 elsif Nkind (N) = N_Function_Specification then
6547 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6548 Set_Etype (Defining_Unit_Name (N), Anon);
6549
6550 else
6551 Rewrite (Comp,
6552 Make_Component_Definition (Loc,
6553 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6554 end if;
6555
6556 Mark_Rewrite_Insertion (Comp);
6557
6558 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6559 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6560 and then not Is_Type (Current_Scope))
6561 then
6562
6563 -- Declaration can be analyzed in the current scope.
6564
6565 Analyze (Decl);
6566
6567 else
6568 -- Temporarily remove the current scope (record or subprogram) from
6569 -- the stack to add the new declarations to the enclosing scope.
6570 -- The anonymous entity is an Itype with the proper attributes.
6571
6572 Scope_Stack.Decrement_Last;
6573 Analyze (Decl);
6574 Set_Is_Itype (Anon);
6575 Set_Associated_Node_For_Itype (Anon, N);
6576 Scope_Stack.Append (Curr_Scope);
6577 end if;
6578
6579 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6580 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6581 return Anon;
6582 end Replace_Anonymous_Access_To_Protected_Subprogram;
6583
6584 -------------------------------
6585 -- Build_Derived_Access_Type --
6586 -------------------------------
6587
6588 procedure Build_Derived_Access_Type
6589 (N : Node_Id;
6590 Parent_Type : Entity_Id;
6591 Derived_Type : Entity_Id)
6592 is
6593 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6594
6595 Desig_Type : Entity_Id;
6596 Discr : Entity_Id;
6597 Discr_Con_Elist : Elist_Id;
6598 Discr_Con_El : Elmt_Id;
6599 Subt : Entity_Id;
6600
6601 begin
6602 -- Set the designated type so it is available in case this is an access
6603 -- to a self-referential type, e.g. a standard list type with a next
6604 -- pointer. Will be reset after subtype is built.
6605
6606 Set_Directly_Designated_Type
6607 (Derived_Type, Designated_Type (Parent_Type));
6608
6609 Subt := Process_Subtype (S, N);
6610
6611 if Nkind (S) /= N_Subtype_Indication
6612 and then Subt /= Base_Type (Subt)
6613 then
6614 Set_Ekind (Derived_Type, E_Access_Subtype);
6615 end if;
6616
6617 if Ekind (Derived_Type) = E_Access_Subtype then
6618 declare
6619 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6620 Ibase : constant Entity_Id :=
6621 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6622 Svg_Chars : constant Name_Id := Chars (Ibase);
6623 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6624 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6625
6626 begin
6627 Copy_Node (Pbase, Ibase);
6628
6629 -- Restore Itype status after Copy_Node
6630
6631 Set_Is_Itype (Ibase);
6632 Set_Associated_Node_For_Itype (Ibase, N);
6633
6634 Set_Chars (Ibase, Svg_Chars);
6635 Set_Prev_Entity (Ibase, Svg_Prev_E);
6636 Set_Next_Entity (Ibase, Svg_Next_E);
6637 Set_Sloc (Ibase, Sloc (Derived_Type));
6638 Set_Scope (Ibase, Scope (Derived_Type));
6639 Set_Freeze_Node (Ibase, Empty);
6640 Set_Is_Frozen (Ibase, False);
6641 Set_Comes_From_Source (Ibase, False);
6642 Set_Is_First_Subtype (Ibase, False);
6643
6644 Set_Etype (Ibase, Pbase);
6645 Set_Etype (Derived_Type, Ibase);
6646 end;
6647 end if;
6648
6649 Set_Directly_Designated_Type
6650 (Derived_Type, Designated_Type (Subt));
6651
6652 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6653 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6654 Set_Size_Info (Derived_Type, Parent_Type);
6655 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6656 Set_Depends_On_Private (Derived_Type,
6657 Has_Private_Component (Derived_Type));
6658 Conditional_Delay (Derived_Type, Subt);
6659
6660 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6661 -- that it is not redundant.
6662
6663 if Null_Exclusion_Present (Type_Definition (N)) then
6664 Set_Can_Never_Be_Null (Derived_Type);
6665
6666 elsif Can_Never_Be_Null (Parent_Type) then
6667 Set_Can_Never_Be_Null (Derived_Type);
6668 end if;
6669
6670 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6671 -- the root type for this information.
6672
6673 -- Apply range checks to discriminants for derived record case
6674 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6675
6676 Desig_Type := Designated_Type (Derived_Type);
6677
6678 if Is_Composite_Type (Desig_Type)
6679 and then (not Is_Array_Type (Desig_Type))
6680 and then Has_Discriminants (Desig_Type)
6681 and then Base_Type (Desig_Type) /= Desig_Type
6682 then
6683 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6684 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6685
6686 Discr := First_Discriminant (Base_Type (Desig_Type));
6687 while Present (Discr_Con_El) loop
6688 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6689 Next_Elmt (Discr_Con_El);
6690 Next_Discriminant (Discr);
6691 end loop;
6692 end if;
6693 end Build_Derived_Access_Type;
6694
6695 ------------------------------
6696 -- Build_Derived_Array_Type --
6697 ------------------------------
6698
6699 procedure Build_Derived_Array_Type
6700 (N : Node_Id;
6701 Parent_Type : Entity_Id;
6702 Derived_Type : Entity_Id)
6703 is
6704 Loc : constant Source_Ptr := Sloc (N);
6705 Tdef : constant Node_Id := Type_Definition (N);
6706 Indic : constant Node_Id := Subtype_Indication (Tdef);
6707 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6708 Implicit_Base : Entity_Id := Empty;
6709 New_Indic : Node_Id;
6710
6711 procedure Make_Implicit_Base;
6712 -- If the parent subtype is constrained, the derived type is a subtype
6713 -- of an implicit base type derived from the parent base.
6714
6715 ------------------------
6716 -- Make_Implicit_Base --
6717 ------------------------
6718
6719 procedure Make_Implicit_Base is
6720 begin
6721 Implicit_Base :=
6722 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6723
6724 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6725 Set_Etype (Implicit_Base, Parent_Base);
6726
6727 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6728 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6729
6730 Set_Has_Delayed_Freeze (Implicit_Base, True);
6731 end Make_Implicit_Base;
6732
6733 -- Start of processing for Build_Derived_Array_Type
6734
6735 begin
6736 if not Is_Constrained (Parent_Type) then
6737 if Nkind (Indic) /= N_Subtype_Indication then
6738 Set_Ekind (Derived_Type, E_Array_Type);
6739
6740 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6741 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6742
6743 Set_Has_Delayed_Freeze (Derived_Type, True);
6744
6745 else
6746 Make_Implicit_Base;
6747 Set_Etype (Derived_Type, Implicit_Base);
6748
6749 New_Indic :=
6750 Make_Subtype_Declaration (Loc,
6751 Defining_Identifier => Derived_Type,
6752 Subtype_Indication =>
6753 Make_Subtype_Indication (Loc,
6754 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6755 Constraint => Constraint (Indic)));
6756
6757 Rewrite (N, New_Indic);
6758 Analyze (N);
6759 end if;
6760
6761 else
6762 if Nkind (Indic) /= N_Subtype_Indication then
6763 Make_Implicit_Base;
6764
6765 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6766 Set_Etype (Derived_Type, Implicit_Base);
6767 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6768
6769 else
6770 Error_Msg_N ("illegal constraint on constrained type", Indic);
6771 end if;
6772 end if;
6773
6774 -- If parent type is not a derived type itself, and is declared in
6775 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6776 -- the new type's concatenation operator since Derive_Subprograms
6777 -- will not inherit the parent's operator. If the parent type is
6778 -- unconstrained, the operator is of the unconstrained base type.
6779
6780 if Number_Dimensions (Parent_Type) = 1
6781 and then not Is_Limited_Type (Parent_Type)
6782 and then not Is_Derived_Type (Parent_Type)
6783 and then not Is_Package_Or_Generic_Package
6784 (Scope (Base_Type (Parent_Type)))
6785 then
6786 if not Is_Constrained (Parent_Type)
6787 and then Is_Constrained (Derived_Type)
6788 then
6789 New_Concatenation_Op (Implicit_Base);
6790 else
6791 New_Concatenation_Op (Derived_Type);
6792 end if;
6793 end if;
6794 end Build_Derived_Array_Type;
6795
6796 -----------------------------------
6797 -- Build_Derived_Concurrent_Type --
6798 -----------------------------------
6799
6800 procedure Build_Derived_Concurrent_Type
6801 (N : Node_Id;
6802 Parent_Type : Entity_Id;
6803 Derived_Type : Entity_Id)
6804 is
6805 Loc : constant Source_Ptr := Sloc (N);
6806
6807 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6808 Corr_Decl : Node_Id;
6809 Corr_Decl_Needed : Boolean;
6810 -- If the derived type has fewer discriminants than its parent, the
6811 -- corresponding record is also a derived type, in order to account for
6812 -- the bound discriminants. We create a full type declaration for it in
6813 -- this case.
6814
6815 Constraint_Present : constant Boolean :=
6816 Nkind (Subtype_Indication (Type_Definition (N))) =
6817 N_Subtype_Indication;
6818
6819 D_Constraint : Node_Id;
6820 New_Constraint : Elist_Id := No_Elist;
6821 Old_Disc : Entity_Id;
6822 New_Disc : Entity_Id;
6823 New_N : Node_Id;
6824
6825 begin
6826 Set_Stored_Constraint (Derived_Type, No_Elist);
6827 Corr_Decl_Needed := False;
6828 Old_Disc := Empty;
6829
6830 if Present (Discriminant_Specifications (N))
6831 and then Constraint_Present
6832 then
6833 Old_Disc := First_Discriminant (Parent_Type);
6834 New_Disc := First (Discriminant_Specifications (N));
6835 while Present (New_Disc) and then Present (Old_Disc) loop
6836 Next_Discriminant (Old_Disc);
6837 Next (New_Disc);
6838 end loop;
6839 end if;
6840
6841 if Present (Old_Disc) and then Expander_Active then
6842
6843 -- The new type has fewer discriminants, so we need to create a new
6844 -- corresponding record, which is derived from the corresponding
6845 -- record of the parent, and has a stored constraint that captures
6846 -- the values of the discriminant constraints. The corresponding
6847 -- record is needed only if expander is active and code generation is
6848 -- enabled.
6849
6850 -- The type declaration for the derived corresponding record has the
6851 -- same discriminant part and constraints as the current declaration.
6852 -- Copy the unanalyzed tree to build declaration.
6853
6854 Corr_Decl_Needed := True;
6855 New_N := Copy_Separate_Tree (N);
6856
6857 Corr_Decl :=
6858 Make_Full_Type_Declaration (Loc,
6859 Defining_Identifier => Corr_Record,
6860 Discriminant_Specifications =>
6861 Discriminant_Specifications (New_N),
6862 Type_Definition =>
6863 Make_Derived_Type_Definition (Loc,
6864 Subtype_Indication =>
6865 Make_Subtype_Indication (Loc,
6866 Subtype_Mark =>
6867 New_Occurrence_Of
6868 (Corresponding_Record_Type (Parent_Type), Loc),
6869 Constraint =>
6870 Constraint
6871 (Subtype_Indication (Type_Definition (New_N))))));
6872 end if;
6873
6874 -- Copy Storage_Size and Relative_Deadline variables if task case
6875
6876 if Is_Task_Type (Parent_Type) then
6877 Set_Storage_Size_Variable (Derived_Type,
6878 Storage_Size_Variable (Parent_Type));
6879 Set_Relative_Deadline_Variable (Derived_Type,
6880 Relative_Deadline_Variable (Parent_Type));
6881 end if;
6882
6883 if Present (Discriminant_Specifications (N)) then
6884 Push_Scope (Derived_Type);
6885 Check_Or_Process_Discriminants (N, Derived_Type);
6886
6887 if Constraint_Present then
6888 New_Constraint :=
6889 Expand_To_Stored_Constraint
6890 (Parent_Type,
6891 Build_Discriminant_Constraints
6892 (Parent_Type,
6893 Subtype_Indication (Type_Definition (N)), True));
6894 end if;
6895
6896 End_Scope;
6897
6898 elsif Constraint_Present then
6899
6900 -- Build constrained subtype, copying the constraint, and derive
6901 -- from it to create a derived constrained type.
6902
6903 declare
6904 Loc : constant Source_Ptr := Sloc (N);
6905 Anon : constant Entity_Id :=
6906 Make_Defining_Identifier (Loc,
6907 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6908 Decl : Node_Id;
6909
6910 begin
6911 Decl :=
6912 Make_Subtype_Declaration (Loc,
6913 Defining_Identifier => Anon,
6914 Subtype_Indication =>
6915 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6916 Insert_Before (N, Decl);
6917 Analyze (Decl);
6918
6919 Rewrite (Subtype_Indication (Type_Definition (N)),
6920 New_Occurrence_Of (Anon, Loc));
6921 Set_Analyzed (Derived_Type, False);
6922 Analyze (N);
6923 return;
6924 end;
6925 end if;
6926
6927 -- By default, operations and private data are inherited from parent.
6928 -- However, in the presence of bound discriminants, a new corresponding
6929 -- record will be created, see below.
6930
6931 Set_Has_Discriminants
6932 (Derived_Type, Has_Discriminants (Parent_Type));
6933 Set_Corresponding_Record_Type
6934 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6935
6936 -- Is_Constrained is set according the parent subtype, but is set to
6937 -- False if the derived type is declared with new discriminants.
6938
6939 Set_Is_Constrained
6940 (Derived_Type,
6941 (Is_Constrained (Parent_Type) or else Constraint_Present)
6942 and then not Present (Discriminant_Specifications (N)));
6943
6944 if Constraint_Present then
6945 if not Has_Discriminants (Parent_Type) then
6946 Error_Msg_N ("untagged parent must have discriminants", N);
6947
6948 elsif Present (Discriminant_Specifications (N)) then
6949
6950 -- Verify that new discriminants are used to constrain old ones
6951
6952 D_Constraint :=
6953 First
6954 (Constraints
6955 (Constraint (Subtype_Indication (Type_Definition (N)))));
6956
6957 Old_Disc := First_Discriminant (Parent_Type);
6958
6959 while Present (D_Constraint) loop
6960 if Nkind (D_Constraint) /= N_Discriminant_Association then
6961
6962 -- Positional constraint. If it is a reference to a new
6963 -- discriminant, it constrains the corresponding old one.
6964
6965 if Nkind (D_Constraint) = N_Identifier then
6966 New_Disc := First_Discriminant (Derived_Type);
6967 while Present (New_Disc) loop
6968 exit when Chars (New_Disc) = Chars (D_Constraint);
6969 Next_Discriminant (New_Disc);
6970 end loop;
6971
6972 if Present (New_Disc) then
6973 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6974 end if;
6975 end if;
6976
6977 Next_Discriminant (Old_Disc);
6978
6979 -- if this is a named constraint, search by name for the old
6980 -- discriminants constrained by the new one.
6981
6982 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6983
6984 -- Find new discriminant with that name
6985
6986 New_Disc := First_Discriminant (Derived_Type);
6987 while Present (New_Disc) loop
6988 exit when
6989 Chars (New_Disc) = Chars (Expression (D_Constraint));
6990 Next_Discriminant (New_Disc);
6991 end loop;
6992
6993 if Present (New_Disc) then
6994
6995 -- Verify that new discriminant renames some discriminant
6996 -- of the parent type, and associate the new discriminant
6997 -- with one or more old ones that it renames.
6998
6999 declare
7000 Selector : Node_Id;
7001
7002 begin
7003 Selector := First (Selector_Names (D_Constraint));
7004 while Present (Selector) loop
7005 Old_Disc := First_Discriminant (Parent_Type);
7006 while Present (Old_Disc) loop
7007 exit when Chars (Old_Disc) = Chars (Selector);
7008 Next_Discriminant (Old_Disc);
7009 end loop;
7010
7011 if Present (Old_Disc) then
7012 Set_Corresponding_Discriminant
7013 (New_Disc, Old_Disc);
7014 end if;
7015
7016 Next (Selector);
7017 end loop;
7018 end;
7019 end if;
7020 end if;
7021
7022 Next (D_Constraint);
7023 end loop;
7024
7025 New_Disc := First_Discriminant (Derived_Type);
7026 while Present (New_Disc) loop
7027 if No (Corresponding_Discriminant (New_Disc)) then
7028 Error_Msg_NE
7029 ("new discriminant& must constrain old one", N, New_Disc);
7030
7031 elsif not
7032 Subtypes_Statically_Compatible
7033 (Etype (New_Disc),
7034 Etype (Corresponding_Discriminant (New_Disc)))
7035 then
7036 Error_Msg_NE
7037 ("& not statically compatible with parent discriminant",
7038 N, New_Disc);
7039 end if;
7040
7041 Next_Discriminant (New_Disc);
7042 end loop;
7043 end if;
7044
7045 elsif Present (Discriminant_Specifications (N)) then
7046 Error_Msg_N
7047 ("missing discriminant constraint in untagged derivation", N);
7048 end if;
7049
7050 -- The entity chain of the derived type includes the new discriminants
7051 -- but shares operations with the parent.
7052
7053 if Present (Discriminant_Specifications (N)) then
7054 Old_Disc := First_Discriminant (Parent_Type);
7055 while Present (Old_Disc) loop
7056 if No (Next_Entity (Old_Disc))
7057 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7058 then
7059 Link_Entities
7060 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7061 exit;
7062 end if;
7063
7064 Next_Discriminant (Old_Disc);
7065 end loop;
7066
7067 else
7068 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7069 if Has_Discriminants (Parent_Type) then
7070 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7071 Set_Discriminant_Constraint (
7072 Derived_Type, Discriminant_Constraint (Parent_Type));
7073 end if;
7074 end if;
7075
7076 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7077
7078 Set_Has_Completion (Derived_Type);
7079
7080 if Corr_Decl_Needed then
7081 Set_Stored_Constraint (Derived_Type, New_Constraint);
7082 Insert_After (N, Corr_Decl);
7083 Analyze (Corr_Decl);
7084 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7085 end if;
7086 end Build_Derived_Concurrent_Type;
7087
7088 ------------------------------------
7089 -- Build_Derived_Enumeration_Type --
7090 ------------------------------------
7091
7092 procedure Build_Derived_Enumeration_Type
7093 (N : Node_Id;
7094 Parent_Type : Entity_Id;
7095 Derived_Type : Entity_Id)
7096 is
7097 Loc : constant Source_Ptr := Sloc (N);
7098 Def : constant Node_Id := Type_Definition (N);
7099 Indic : constant Node_Id := Subtype_Indication (Def);
7100 Implicit_Base : Entity_Id;
7101 Literal : Entity_Id;
7102 New_Lit : Entity_Id;
7103 Literals_List : List_Id;
7104 Type_Decl : Node_Id;
7105 Hi, Lo : Node_Id;
7106 Rang_Expr : Node_Id;
7107
7108 begin
7109 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7110 -- not have explicit literals lists we need to process types derived
7111 -- from them specially. This is handled by Derived_Standard_Character.
7112 -- If the parent type is a generic type, there are no literals either,
7113 -- and we construct the same skeletal representation as for the generic
7114 -- parent type.
7115
7116 if Is_Standard_Character_Type (Parent_Type) then
7117 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7118
7119 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7120 declare
7121 Lo : Node_Id;
7122 Hi : Node_Id;
7123
7124 begin
7125 if Nkind (Indic) /= N_Subtype_Indication then
7126 Lo :=
7127 Make_Attribute_Reference (Loc,
7128 Attribute_Name => Name_First,
7129 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7130 Set_Etype (Lo, Derived_Type);
7131
7132 Hi :=
7133 Make_Attribute_Reference (Loc,
7134 Attribute_Name => Name_Last,
7135 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7136 Set_Etype (Hi, Derived_Type);
7137
7138 Set_Scalar_Range (Derived_Type,
7139 Make_Range (Loc,
7140 Low_Bound => Lo,
7141 High_Bound => Hi));
7142 else
7143
7144 -- Analyze subtype indication and verify compatibility
7145 -- with parent type.
7146
7147 if Base_Type (Process_Subtype (Indic, N)) /=
7148 Base_Type (Parent_Type)
7149 then
7150 Error_Msg_N
7151 ("illegal constraint for formal discrete type", N);
7152 end if;
7153 end if;
7154 end;
7155
7156 else
7157 -- If a constraint is present, analyze the bounds to catch
7158 -- premature usage of the derived literals.
7159
7160 if Nkind (Indic) = N_Subtype_Indication
7161 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7162 then
7163 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7164 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7165 end if;
7166
7167 -- Introduce an implicit base type for the derived type even if there
7168 -- is no constraint attached to it, since this seems closer to the
7169 -- Ada semantics. Build a full type declaration tree for the derived
7170 -- type using the implicit base type as the defining identifier. The
7171 -- build a subtype declaration tree which applies the constraint (if
7172 -- any) have it replace the derived type declaration.
7173
7174 Literal := First_Literal (Parent_Type);
7175 Literals_List := New_List;
7176 while Present (Literal)
7177 and then Ekind (Literal) = E_Enumeration_Literal
7178 loop
7179 -- Literals of the derived type have the same representation as
7180 -- those of the parent type, but this representation can be
7181 -- overridden by an explicit representation clause. Indicate
7182 -- that there is no explicit representation given yet. These
7183 -- derived literals are implicit operations of the new type,
7184 -- and can be overridden by explicit ones.
7185
7186 if Nkind (Literal) = N_Defining_Character_Literal then
7187 New_Lit :=
7188 Make_Defining_Character_Literal (Loc, Chars (Literal));
7189 else
7190 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7191 end if;
7192
7193 Set_Ekind (New_Lit, E_Enumeration_Literal);
7194 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7195 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7196 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7197 Set_Alias (New_Lit, Literal);
7198 Set_Is_Known_Valid (New_Lit, True);
7199
7200 Append (New_Lit, Literals_List);
7201 Next_Literal (Literal);
7202 end loop;
7203
7204 Implicit_Base :=
7205 Make_Defining_Identifier (Sloc (Derived_Type),
7206 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7207
7208 -- Indicate the proper nature of the derived type. This must be done
7209 -- before analysis of the literals, to recognize cases when a literal
7210 -- may be hidden by a previous explicit function definition (cf.
7211 -- c83031a).
7212
7213 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7214 Set_Etype (Derived_Type, Implicit_Base);
7215
7216 Type_Decl :=
7217 Make_Full_Type_Declaration (Loc,
7218 Defining_Identifier => Implicit_Base,
7219 Discriminant_Specifications => No_List,
7220 Type_Definition =>
7221 Make_Enumeration_Type_Definition (Loc, Literals_List));
7222
7223 Mark_Rewrite_Insertion (Type_Decl);
7224 Insert_Before (N, Type_Decl);
7225 Analyze (Type_Decl);
7226
7227 -- The anonymous base now has a full declaration, but this base
7228 -- is not a first subtype.
7229
7230 Set_Is_First_Subtype (Implicit_Base, False);
7231
7232 -- After the implicit base is analyzed its Etype needs to be changed
7233 -- to reflect the fact that it is derived from the parent type which
7234 -- was ignored during analysis. We also set the size at this point.
7235
7236 Set_Etype (Implicit_Base, Parent_Type);
7237
7238 Set_Size_Info (Implicit_Base, Parent_Type);
7239 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7240 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7241
7242 -- Copy other flags from parent type
7243
7244 Set_Has_Non_Standard_Rep
7245 (Implicit_Base, Has_Non_Standard_Rep
7246 (Parent_Type));
7247 Set_Has_Pragma_Ordered
7248 (Implicit_Base, Has_Pragma_Ordered
7249 (Parent_Type));
7250 Set_Has_Delayed_Freeze (Implicit_Base);
7251
7252 -- Process the subtype indication including a validation check on the
7253 -- constraint, if any. If a constraint is given, its bounds must be
7254 -- implicitly converted to the new type.
7255
7256 if Nkind (Indic) = N_Subtype_Indication then
7257 declare
7258 R : constant Node_Id :=
7259 Range_Expression (Constraint (Indic));
7260
7261 begin
7262 if Nkind (R) = N_Range then
7263 Hi := Build_Scalar_Bound
7264 (High_Bound (R), Parent_Type, Implicit_Base);
7265 Lo := Build_Scalar_Bound
7266 (Low_Bound (R), Parent_Type, Implicit_Base);
7267
7268 else
7269 -- Constraint is a Range attribute. Replace with explicit
7270 -- mention of the bounds of the prefix, which must be a
7271 -- subtype.
7272
7273 Analyze (Prefix (R));
7274 Hi :=
7275 Convert_To (Implicit_Base,
7276 Make_Attribute_Reference (Loc,
7277 Attribute_Name => Name_Last,
7278 Prefix =>
7279 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7280
7281 Lo :=
7282 Convert_To (Implicit_Base,
7283 Make_Attribute_Reference (Loc,
7284 Attribute_Name => Name_First,
7285 Prefix =>
7286 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7287 end if;
7288 end;
7289
7290 else
7291 Hi :=
7292 Build_Scalar_Bound
7293 (Type_High_Bound (Parent_Type),
7294 Parent_Type, Implicit_Base);
7295 Lo :=
7296 Build_Scalar_Bound
7297 (Type_Low_Bound (Parent_Type),
7298 Parent_Type, Implicit_Base);
7299 end if;
7300
7301 Rang_Expr :=
7302 Make_Range (Loc,
7303 Low_Bound => Lo,
7304 High_Bound => Hi);
7305
7306 -- If we constructed a default range for the case where no range
7307 -- was given, then the expressions in the range must not freeze
7308 -- since they do not correspond to expressions in the source.
7309 -- However, if the type inherits predicates the expressions will
7310 -- be elaborated earlier and must freeze.
7311
7312 if Nkind (Indic) /= N_Subtype_Indication
7313 and then not Has_Predicates (Derived_Type)
7314 then
7315 Set_Must_Not_Freeze (Lo);
7316 Set_Must_Not_Freeze (Hi);
7317 Set_Must_Not_Freeze (Rang_Expr);
7318 end if;
7319
7320 Rewrite (N,
7321 Make_Subtype_Declaration (Loc,
7322 Defining_Identifier => Derived_Type,
7323 Subtype_Indication =>
7324 Make_Subtype_Indication (Loc,
7325 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7326 Constraint =>
7327 Make_Range_Constraint (Loc,
7328 Range_Expression => Rang_Expr))));
7329
7330 Analyze (N);
7331
7332 -- Propagate the aspects from the original type declaration to the
7333 -- declaration of the implicit base.
7334
7335 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7336
7337 -- Apply a range check. Since this range expression doesn't have an
7338 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7339 -- this right???
7340
7341 if Nkind (Indic) = N_Subtype_Indication then
7342 Apply_Range_Check
7343 (Range_Expression (Constraint (Indic)), Parent_Type,
7344 Source_Typ => Entity (Subtype_Mark (Indic)));
7345 end if;
7346 end if;
7347 end Build_Derived_Enumeration_Type;
7348
7349 --------------------------------
7350 -- Build_Derived_Numeric_Type --
7351 --------------------------------
7352
7353 procedure Build_Derived_Numeric_Type
7354 (N : Node_Id;
7355 Parent_Type : Entity_Id;
7356 Derived_Type : Entity_Id)
7357 is
7358 Loc : constant Source_Ptr := Sloc (N);
7359 Tdef : constant Node_Id := Type_Definition (N);
7360 Indic : constant Node_Id := Subtype_Indication (Tdef);
7361 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7362 No_Constraint : constant Boolean := Nkind (Indic) /=
7363 N_Subtype_Indication;
7364 Implicit_Base : Entity_Id;
7365
7366 Lo : Node_Id;
7367 Hi : Node_Id;
7368
7369 begin
7370 -- Process the subtype indication including a validation check on
7371 -- the constraint if any.
7372
7373 Discard_Node (Process_Subtype (Indic, N));
7374
7375 -- Introduce an implicit base type for the derived type even if there
7376 -- is no constraint attached to it, since this seems closer to the Ada
7377 -- semantics.
7378
7379 Implicit_Base :=
7380 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7381
7382 Set_Etype (Implicit_Base, Parent_Base);
7383 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7384 Set_Size_Info (Implicit_Base, Parent_Base);
7385 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7386 Set_Parent (Implicit_Base, Parent (Derived_Type));
7387 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7388
7389 -- Set RM Size for discrete type or decimal fixed-point type
7390 -- Ordinary fixed-point is excluded, why???
7391
7392 if Is_Discrete_Type (Parent_Base)
7393 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7394 then
7395 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7396 end if;
7397
7398 Set_Has_Delayed_Freeze (Implicit_Base);
7399
7400 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7401 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7402
7403 Set_Scalar_Range (Implicit_Base,
7404 Make_Range (Loc,
7405 Low_Bound => Lo,
7406 High_Bound => Hi));
7407
7408 if Has_Infinities (Parent_Base) then
7409 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7410 end if;
7411
7412 -- The Derived_Type, which is the entity of the declaration, is a
7413 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7414 -- absence of an explicit constraint.
7415
7416 Set_Etype (Derived_Type, Implicit_Base);
7417
7418 -- If we did not have a constraint, then the Ekind is set from the
7419 -- parent type (otherwise Process_Subtype has set the bounds)
7420
7421 if No_Constraint then
7422 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7423 end if;
7424
7425 -- If we did not have a range constraint, then set the range from the
7426 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7427
7428 if No_Constraint or else not Has_Range_Constraint (Indic) then
7429 Set_Scalar_Range (Derived_Type,
7430 Make_Range (Loc,
7431 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7432 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7433 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7434
7435 if Has_Infinities (Parent_Type) then
7436 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7437 end if;
7438
7439 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7440 end if;
7441
7442 Set_Is_Descendant_Of_Address (Derived_Type,
7443 Is_Descendant_Of_Address (Parent_Type));
7444 Set_Is_Descendant_Of_Address (Implicit_Base,
7445 Is_Descendant_Of_Address (Parent_Type));
7446
7447 -- Set remaining type-specific fields, depending on numeric type
7448
7449 if Is_Modular_Integer_Type (Parent_Type) then
7450 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7451
7452 Set_Non_Binary_Modulus
7453 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7454
7455 Set_Is_Known_Valid
7456 (Implicit_Base, Is_Known_Valid (Parent_Base));
7457
7458 elsif Is_Floating_Point_Type (Parent_Type) then
7459
7460 -- Digits of base type is always copied from the digits value of
7461 -- the parent base type, but the digits of the derived type will
7462 -- already have been set if there was a constraint present.
7463
7464 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7465 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7466
7467 if No_Constraint then
7468 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7469 end if;
7470
7471 elsif Is_Fixed_Point_Type (Parent_Type) then
7472
7473 -- Small of base type and derived type are always copied from the
7474 -- parent base type, since smalls never change. The delta of the
7475 -- base type is also copied from the parent base type. However the
7476 -- delta of the derived type will have been set already if a
7477 -- constraint was present.
7478
7479 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7480 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7481 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7482
7483 if No_Constraint then
7484 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7485 end if;
7486
7487 -- The scale and machine radix in the decimal case are always
7488 -- copied from the parent base type.
7489
7490 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7491 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7492 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7493
7494 Set_Machine_Radix_10
7495 (Derived_Type, Machine_Radix_10 (Parent_Base));
7496 Set_Machine_Radix_10
7497 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7498
7499 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7500
7501 if No_Constraint then
7502 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7503
7504 else
7505 -- the analysis of the subtype_indication sets the
7506 -- digits value of the derived type.
7507
7508 null;
7509 end if;
7510 end if;
7511 end if;
7512
7513 if Is_Integer_Type (Parent_Type) then
7514 Set_Has_Shift_Operator
7515 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7516 end if;
7517
7518 -- The type of the bounds is that of the parent type, and they
7519 -- must be converted to the derived type.
7520
7521 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7522
7523 -- The implicit_base should be frozen when the derived type is frozen,
7524 -- but note that it is used in the conversions of the bounds. For fixed
7525 -- types we delay the determination of the bounds until the proper
7526 -- freezing point. For other numeric types this is rejected by GCC, for
7527 -- reasons that are currently unclear (???), so we choose to freeze the
7528 -- implicit base now. In the case of integers and floating point types
7529 -- this is harmless because subsequent representation clauses cannot
7530 -- affect anything, but it is still baffling that we cannot use the
7531 -- same mechanism for all derived numeric types.
7532
7533 -- There is a further complication: actually some representation
7534 -- clauses can affect the implicit base type. For example, attribute
7535 -- definition clauses for stream-oriented attributes need to set the
7536 -- corresponding TSS entries on the base type, and this normally
7537 -- cannot be done after the base type is frozen, so the circuitry in
7538 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7539 -- and not use Set_TSS in this case.
7540
7541 -- There are also consequences for the case of delayed representation
7542 -- aspects for some cases. For example, a Size aspect is delayed and
7543 -- should not be evaluated to the freeze point. This early freezing
7544 -- means that the size attribute evaluation happens too early???
7545
7546 if Is_Fixed_Point_Type (Parent_Type) then
7547 Conditional_Delay (Implicit_Base, Parent_Type);
7548 else
7549 Freeze_Before (N, Implicit_Base);
7550 end if;
7551 end Build_Derived_Numeric_Type;
7552
7553 --------------------------------
7554 -- Build_Derived_Private_Type --
7555 --------------------------------
7556
7557 procedure Build_Derived_Private_Type
7558 (N : Node_Id;
7559 Parent_Type : Entity_Id;
7560 Derived_Type : Entity_Id;
7561 Is_Completion : Boolean;
7562 Derive_Subps : Boolean := True)
7563 is
7564 Loc : constant Source_Ptr := Sloc (N);
7565 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7566 Par_Scope : constant Entity_Id := Scope (Par_Base);
7567 Full_N : constant Node_Id := New_Copy_Tree (N);
7568 Full_Der : Entity_Id := New_Copy (Derived_Type);
7569 Full_P : Entity_Id;
7570
7571 procedure Build_Full_Derivation;
7572 -- Build full derivation, i.e. derive from the full view
7573
7574 procedure Copy_And_Build;
7575 -- Copy derived type declaration, replace parent with its full view,
7576 -- and build derivation
7577
7578 ---------------------------
7579 -- Build_Full_Derivation --
7580 ---------------------------
7581
7582 procedure Build_Full_Derivation is
7583 begin
7584 -- If parent scope is not open, install the declarations
7585
7586 if not In_Open_Scopes (Par_Scope) then
7587 Install_Private_Declarations (Par_Scope);
7588 Install_Visible_Declarations (Par_Scope);
7589 Copy_And_Build;
7590 Uninstall_Declarations (Par_Scope);
7591
7592 -- If parent scope is open and in another unit, and parent has a
7593 -- completion, then the derivation is taking place in the visible
7594 -- part of a child unit. In that case retrieve the full view of
7595 -- the parent momentarily.
7596
7597 elsif not In_Same_Source_Unit (N, Parent_Type) then
7598 Full_P := Full_View (Parent_Type);
7599 Exchange_Declarations (Parent_Type);
7600 Copy_And_Build;
7601 Exchange_Declarations (Full_P);
7602
7603 -- Otherwise it is a local derivation
7604
7605 else
7606 Copy_And_Build;
7607 end if;
7608 end Build_Full_Derivation;
7609
7610 --------------------
7611 -- Copy_And_Build --
7612 --------------------
7613
7614 procedure Copy_And_Build is
7615 Full_Parent : Entity_Id := Parent_Type;
7616
7617 begin
7618 -- If the parent is itself derived from another private type,
7619 -- installing the private declarations has not affected its
7620 -- privacy status, so use its own full view explicitly.
7621
7622 if Is_Private_Type (Full_Parent)
7623 and then Present (Full_View (Full_Parent))
7624 then
7625 Full_Parent := Full_View (Full_Parent);
7626 end if;
7627
7628 -- And its underlying full view if necessary
7629
7630 if Is_Private_Type (Full_Parent)
7631 and then Present (Underlying_Full_View (Full_Parent))
7632 then
7633 Full_Parent := Underlying_Full_View (Full_Parent);
7634 end if;
7635
7636 -- For record, access and most enumeration types, derivation from
7637 -- the full view requires a fully-fledged declaration. In the other
7638 -- cases, just use an itype.
7639
7640 if Ekind (Full_Parent) in Record_Kind
7641 or else Ekind (Full_Parent) in Access_Kind
7642 or else
7643 (Ekind (Full_Parent) in Enumeration_Kind
7644 and then not Is_Standard_Character_Type (Full_Parent)
7645 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7646 then
7647 -- Copy and adjust declaration to provide a completion for what
7648 -- is originally a private declaration. Indicate that full view
7649 -- is internally generated.
7650
7651 Set_Comes_From_Source (Full_N, False);
7652 Set_Comes_From_Source (Full_Der, False);
7653 Set_Parent (Full_Der, Full_N);
7654 Set_Defining_Identifier (Full_N, Full_Der);
7655
7656 -- If there are no constraints, adjust the subtype mark
7657
7658 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7659 N_Subtype_Indication
7660 then
7661 Set_Subtype_Indication
7662 (Type_Definition (Full_N),
7663 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7664 end if;
7665
7666 Insert_After (N, Full_N);
7667
7668 -- Build full view of derived type from full view of parent which
7669 -- is now installed. Subprograms have been derived on the partial
7670 -- view, the completion does not derive them anew.
7671
7672 if Ekind (Full_Parent) in Record_Kind then
7673
7674 -- If parent type is tagged, the completion inherits the proper
7675 -- primitive operations.
7676
7677 if Is_Tagged_Type (Parent_Type) then
7678 Build_Derived_Record_Type
7679 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7680 else
7681 Build_Derived_Record_Type
7682 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7683 end if;
7684
7685 else
7686 Build_Derived_Type
7687 (Full_N, Full_Parent, Full_Der,
7688 Is_Completion => False, Derive_Subps => False);
7689 end if;
7690
7691 -- The full declaration has been introduced into the tree and
7692 -- processed in the step above. It should not be analyzed again
7693 -- (when encountered later in the current list of declarations)
7694 -- to prevent spurious name conflicts. The full entity remains
7695 -- invisible.
7696
7697 Set_Analyzed (Full_N);
7698
7699 else
7700 Full_Der :=
7701 Make_Defining_Identifier (Sloc (Derived_Type),
7702 Chars => Chars (Derived_Type));
7703 Set_Is_Itype (Full_Der);
7704 Set_Associated_Node_For_Itype (Full_Der, N);
7705 Set_Parent (Full_Der, N);
7706 Build_Derived_Type
7707 (N, Full_Parent, Full_Der,
7708 Is_Completion => False, Derive_Subps => False);
7709 end if;
7710
7711 Set_Has_Private_Declaration (Full_Der);
7712 Set_Has_Private_Declaration (Derived_Type);
7713
7714 Set_Scope (Full_Der, Scope (Derived_Type));
7715 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7716 Set_Has_Size_Clause (Full_Der, False);
7717 Set_Has_Alignment_Clause (Full_Der, False);
7718 Set_Has_Delayed_Freeze (Full_Der);
7719 Set_Is_Frozen (Full_Der, False);
7720 Set_Freeze_Node (Full_Der, Empty);
7721 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7722 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7723
7724 -- The convention on the base type may be set in the private part
7725 -- and not propagated to the subtype until later, so we obtain the
7726 -- convention from the base type of the parent.
7727
7728 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7729 end Copy_And_Build;
7730
7731 -- Start of processing for Build_Derived_Private_Type
7732
7733 begin
7734 if Is_Tagged_Type (Parent_Type) then
7735 Full_P := Full_View (Parent_Type);
7736
7737 -- A type extension of a type with unknown discriminants is an
7738 -- indefinite type that the back-end cannot handle directly.
7739 -- We treat it as a private type, and build a completion that is
7740 -- derived from the full view of the parent, and hopefully has
7741 -- known discriminants.
7742
7743 -- If the full view of the parent type has an underlying record view,
7744 -- use it to generate the underlying record view of this derived type
7745 -- (required for chains of derivations with unknown discriminants).
7746
7747 -- Minor optimization: we avoid the generation of useless underlying
7748 -- record view entities if the private type declaration has unknown
7749 -- discriminants but its corresponding full view has no
7750 -- discriminants.
7751
7752 if Has_Unknown_Discriminants (Parent_Type)
7753 and then Present (Full_P)
7754 and then (Has_Discriminants (Full_P)
7755 or else Present (Underlying_Record_View (Full_P)))
7756 and then not In_Open_Scopes (Par_Scope)
7757 and then Expander_Active
7758 then
7759 declare
7760 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7761 New_Ext : constant Node_Id :=
7762 Copy_Separate_Tree
7763 (Record_Extension_Part (Type_Definition (N)));
7764 Decl : Node_Id;
7765
7766 begin
7767 Build_Derived_Record_Type
7768 (N, Parent_Type, Derived_Type, Derive_Subps);
7769
7770 -- Build anonymous completion, as a derivation from the full
7771 -- view of the parent. This is not a completion in the usual
7772 -- sense, because the current type is not private.
7773
7774 Decl :=
7775 Make_Full_Type_Declaration (Loc,
7776 Defining_Identifier => Full_Der,
7777 Type_Definition =>
7778 Make_Derived_Type_Definition (Loc,
7779 Subtype_Indication =>
7780 New_Copy_Tree
7781 (Subtype_Indication (Type_Definition (N))),
7782 Record_Extension_Part => New_Ext));
7783
7784 -- If the parent type has an underlying record view, use it
7785 -- here to build the new underlying record view.
7786
7787 if Present (Underlying_Record_View (Full_P)) then
7788 pragma Assert
7789 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7790 = N_Identifier);
7791 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7792 Underlying_Record_View (Full_P));
7793 end if;
7794
7795 Install_Private_Declarations (Par_Scope);
7796 Install_Visible_Declarations (Par_Scope);
7797 Insert_Before (N, Decl);
7798
7799 -- Mark entity as an underlying record view before analysis,
7800 -- to avoid generating the list of its primitive operations
7801 -- (which is not really required for this entity) and thus
7802 -- prevent spurious errors associated with missing overriding
7803 -- of abstract primitives (overridden only for Derived_Type).
7804
7805 Set_Ekind (Full_Der, E_Record_Type);
7806 Set_Is_Underlying_Record_View (Full_Der);
7807 Set_Default_SSO (Full_Der);
7808 Set_No_Reordering (Full_Der, No_Component_Reordering);
7809
7810 Analyze (Decl);
7811
7812 pragma Assert (Has_Discriminants (Full_Der)
7813 and then not Has_Unknown_Discriminants (Full_Der));
7814
7815 Uninstall_Declarations (Par_Scope);
7816
7817 -- Freeze the underlying record view, to prevent generation of
7818 -- useless dispatching information, which is simply shared with
7819 -- the real derived type.
7820
7821 Set_Is_Frozen (Full_Der);
7822
7823 -- If the derived type has access discriminants, create
7824 -- references to their anonymous types now, to prevent
7825 -- back-end problems when their first use is in generated
7826 -- bodies of primitives.
7827
7828 declare
7829 E : Entity_Id;
7830
7831 begin
7832 E := First_Entity (Full_Der);
7833
7834 while Present (E) loop
7835 if Ekind (E) = E_Discriminant
7836 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7837 then
7838 Build_Itype_Reference (Etype (E), Decl);
7839 end if;
7840
7841 Next_Entity (E);
7842 end loop;
7843 end;
7844
7845 -- Set up links between real entity and underlying record view
7846
7847 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7848 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7849 end;
7850
7851 -- If discriminants are known, build derived record
7852
7853 else
7854 Build_Derived_Record_Type
7855 (N, Parent_Type, Derived_Type, Derive_Subps);
7856 end if;
7857
7858 return;
7859
7860 elsif Has_Discriminants (Parent_Type) then
7861
7862 -- Build partial view of derived type from partial view of parent.
7863 -- This must be done before building the full derivation because the
7864 -- second derivation will modify the discriminants of the first and
7865 -- the discriminants are chained with the rest of the components in
7866 -- the full derivation.
7867
7868 Build_Derived_Record_Type
7869 (N, Parent_Type, Derived_Type, Derive_Subps);
7870
7871 -- Build the full derivation if this is not the anonymous derived
7872 -- base type created by Build_Derived_Record_Type in the constrained
7873 -- case (see point 5. of its head comment) since we build it for the
7874 -- derived subtype. And skip it for synchronized types altogether, as
7875 -- gigi does not use these types directly.
7876
7877 if Present (Full_View (Parent_Type))
7878 and then not Is_Itype (Derived_Type)
7879 and then not Is_Concurrent_Type (Full_View (Parent_Type))
7880 then
7881 declare
7882 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7883 Discr : Entity_Id;
7884 Last_Discr : Entity_Id;
7885
7886 begin
7887 -- If this is not a completion, construct the implicit full
7888 -- view by deriving from the full view of the parent type.
7889 -- But if this is a completion, the derived private type
7890 -- being built is a full view and the full derivation can
7891 -- only be its underlying full view.
7892
7893 Build_Full_Derivation;
7894
7895 if not Is_Completion then
7896 Set_Full_View (Derived_Type, Full_Der);
7897 else
7898 Set_Underlying_Full_View (Derived_Type, Full_Der);
7899 Set_Is_Underlying_Full_View (Full_Der);
7900 end if;
7901
7902 if not Is_Base_Type (Derived_Type) then
7903 Set_Full_View (Der_Base, Base_Type (Full_Der));
7904 end if;
7905
7906 -- Copy the discriminant list from full view to the partial
7907 -- view (base type and its subtype). Gigi requires that the
7908 -- partial and full views have the same discriminants.
7909
7910 -- Note that since the partial view points to discriminants
7911 -- in the full view, their scope will be that of the full
7912 -- view. This might cause some front end problems and need
7913 -- adjustment???
7914
7915 Discr := First_Discriminant (Base_Type (Full_Der));
7916 Set_First_Entity (Der_Base, Discr);
7917
7918 loop
7919 Last_Discr := Discr;
7920 Next_Discriminant (Discr);
7921 exit when No (Discr);
7922 end loop;
7923
7924 Set_Last_Entity (Der_Base, Last_Discr);
7925 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7926 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7927 end;
7928 end if;
7929
7930 elsif Present (Full_View (Parent_Type))
7931 and then Has_Discriminants (Full_View (Parent_Type))
7932 then
7933 if Has_Unknown_Discriminants (Parent_Type)
7934 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7935 N_Subtype_Indication
7936 then
7937 Error_Msg_N
7938 ("cannot constrain type with unknown discriminants",
7939 Subtype_Indication (Type_Definition (N)));
7940 return;
7941 end if;
7942
7943 -- If this is not a completion, construct the implicit full view by
7944 -- deriving from the full view of the parent type. But if this is a
7945 -- completion, the derived private type being built is a full view
7946 -- and the full derivation can only be its underlying full view.
7947
7948 Build_Full_Derivation;
7949
7950 if not Is_Completion then
7951 Set_Full_View (Derived_Type, Full_Der);
7952 else
7953 Set_Underlying_Full_View (Derived_Type, Full_Der);
7954 Set_Is_Underlying_Full_View (Full_Der);
7955 end if;
7956
7957 -- In any case, the primitive operations are inherited from the
7958 -- parent type, not from the internal full view.
7959
7960 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7961
7962 if Derive_Subps then
7963 Derive_Subprograms (Parent_Type, Derived_Type);
7964 end if;
7965
7966 Set_Stored_Constraint (Derived_Type, No_Elist);
7967 Set_Is_Constrained
7968 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7969
7970 else
7971 -- Untagged type, No discriminants on either view
7972
7973 if Nkind (Subtype_Indication (Type_Definition (N))) =
7974 N_Subtype_Indication
7975 then
7976 Error_Msg_N
7977 ("illegal constraint on type without discriminants", N);
7978 end if;
7979
7980 if Present (Discriminant_Specifications (N))
7981 and then Present (Full_View (Parent_Type))
7982 and then not Is_Tagged_Type (Full_View (Parent_Type))
7983 then
7984 Error_Msg_N ("cannot add discriminants to untagged type", N);
7985 end if;
7986
7987 Set_Stored_Constraint (Derived_Type, No_Elist);
7988 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7989
7990 Set_Is_Controlled_Active
7991 (Derived_Type, Is_Controlled_Active (Parent_Type));
7992
7993 Set_Disable_Controlled
7994 (Derived_Type, Disable_Controlled (Parent_Type));
7995
7996 Set_Has_Controlled_Component
7997 (Derived_Type, Has_Controlled_Component (Parent_Type));
7998
7999 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8000
8001 if not Is_Controlled (Parent_Type) then
8002 Set_Finalize_Storage_Only
8003 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8004 end if;
8005
8006 -- If this is not a completion, construct the implicit full view by
8007 -- deriving from the full view of the parent type.
8008
8009 -- ??? If the parent is untagged private and its completion is
8010 -- tagged, this mechanism will not work because we cannot derive from
8011 -- the tagged full view unless we have an extension.
8012
8013 if Present (Full_View (Parent_Type))
8014 and then not Is_Tagged_Type (Full_View (Parent_Type))
8015 and then not Is_Completion
8016 then
8017 Build_Full_Derivation;
8018 Set_Full_View (Derived_Type, Full_Der);
8019 end if;
8020 end if;
8021
8022 Set_Has_Unknown_Discriminants (Derived_Type,
8023 Has_Unknown_Discriminants (Parent_Type));
8024
8025 if Is_Private_Type (Derived_Type) then
8026 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8027 end if;
8028
8029 -- If the parent base type is in scope, add the derived type to its
8030 -- list of private dependents, because its full view may become
8031 -- visible subsequently (in a nested private part, a body, or in a
8032 -- further child unit).
8033
8034 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8035 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8036
8037 -- Check for unusual case where a type completed by a private
8038 -- derivation occurs within a package nested in a child unit, and
8039 -- the parent is declared in an ancestor.
8040
8041 if Is_Child_Unit (Scope (Current_Scope))
8042 and then Is_Completion
8043 and then In_Private_Part (Current_Scope)
8044 and then Scope (Parent_Type) /= Current_Scope
8045
8046 -- Note that if the parent has a completion in the private part,
8047 -- (which is itself a derivation from some other private type)
8048 -- it is that completion that is visible, there is no full view
8049 -- available, and no special processing is needed.
8050
8051 and then Present (Full_View (Parent_Type))
8052 then
8053 -- In this case, the full view of the parent type will become
8054 -- visible in the body of the enclosing child, and only then will
8055 -- the current type be possibly non-private. Build an underlying
8056 -- full view that will be installed when the enclosing child body
8057 -- is compiled.
8058
8059 if Present (Underlying_Full_View (Derived_Type)) then
8060 Full_Der := Underlying_Full_View (Derived_Type);
8061 else
8062 Build_Full_Derivation;
8063 Set_Underlying_Full_View (Derived_Type, Full_Der);
8064 Set_Is_Underlying_Full_View (Full_Der);
8065 end if;
8066
8067 -- The full view will be used to swap entities on entry/exit to
8068 -- the body, and must appear in the entity list for the package.
8069
8070 Append_Entity (Full_Der, Scope (Derived_Type));
8071 end if;
8072 end if;
8073 end Build_Derived_Private_Type;
8074
8075 -------------------------------
8076 -- Build_Derived_Record_Type --
8077 -------------------------------
8078
8079 -- 1. INTRODUCTION
8080
8081 -- Ideally we would like to use the same model of type derivation for
8082 -- tagged and untagged record types. Unfortunately this is not quite
8083 -- possible because the semantics of representation clauses is different
8084 -- for tagged and untagged records under inheritance. Consider the
8085 -- following:
8086
8087 -- type R (...) is [tagged] record ... end record;
8088 -- type T (...) is new R (...) [with ...];
8089
8090 -- The representation clauses for T can specify a completely different
8091 -- record layout from R's. Hence the same component can be placed in two
8092 -- very different positions in objects of type T and R. If R and T are
8093 -- tagged types, representation clauses for T can only specify the layout
8094 -- of non inherited components, thus components that are common in R and T
8095 -- have the same position in objects of type R and T.
8096
8097 -- This has two implications. The first is that the entire tree for R's
8098 -- declaration needs to be copied for T in the untagged case, so that T
8099 -- can be viewed as a record type of its own with its own representation
8100 -- clauses. The second implication is the way we handle discriminants.
8101 -- Specifically, in the untagged case we need a way to communicate to Gigi
8102 -- what are the real discriminants in the record, while for the semantics
8103 -- we need to consider those introduced by the user to rename the
8104 -- discriminants in the parent type. This is handled by introducing the
8105 -- notion of stored discriminants. See below for more.
8106
8107 -- Fortunately the way regular components are inherited can be handled in
8108 -- the same way in tagged and untagged types.
8109
8110 -- To complicate things a bit more the private view of a private extension
8111 -- cannot be handled in the same way as the full view (for one thing the
8112 -- semantic rules are somewhat different). We will explain what differs
8113 -- below.
8114
8115 -- 2. DISCRIMINANTS UNDER INHERITANCE
8116
8117 -- The semantic rules governing the discriminants of derived types are
8118 -- quite subtle.
8119
8120 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8121 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8122
8123 -- If parent type has discriminants, then the discriminants that are
8124 -- declared in the derived type are [3.4 (11)]:
8125
8126 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8127 -- there is one;
8128
8129 -- o Otherwise, each discriminant of the parent type (implicitly declared
8130 -- in the same order with the same specifications). In this case, the
8131 -- discriminants are said to be "inherited", or if unknown in the parent
8132 -- are also unknown in the derived type.
8133
8134 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8135
8136 -- o The parent subtype must be constrained;
8137
8138 -- o If the parent type is not a tagged type, then each discriminant of
8139 -- the derived type must be used in the constraint defining a parent
8140 -- subtype. [Implementation note: This ensures that the new discriminant
8141 -- can share storage with an existing discriminant.]
8142
8143 -- For the derived type each discriminant of the parent type is either
8144 -- inherited, constrained to equal some new discriminant of the derived
8145 -- type, or constrained to the value of an expression.
8146
8147 -- When inherited or constrained to equal some new discriminant, the
8148 -- parent discriminant and the discriminant of the derived type are said
8149 -- to "correspond".
8150
8151 -- If a discriminant of the parent type is constrained to a specific value
8152 -- in the derived type definition, then the discriminant is said to be
8153 -- "specified" by that derived type definition.
8154
8155 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8156
8157 -- We have spoken about stored discriminants in point 1 (introduction)
8158 -- above. There are two sorts of stored discriminants: implicit and
8159 -- explicit. As long as the derived type inherits the same discriminants as
8160 -- the root record type, stored discriminants are the same as regular
8161 -- discriminants, and are said to be implicit. However, if any discriminant
8162 -- in the root type was renamed in the derived type, then the derived
8163 -- type will contain explicit stored discriminants. Explicit stored
8164 -- discriminants are discriminants in addition to the semantically visible
8165 -- discriminants defined for the derived type. Stored discriminants are
8166 -- used by Gigi to figure out what are the physical discriminants in
8167 -- objects of the derived type (see precise definition in einfo.ads).
8168 -- As an example, consider the following:
8169
8170 -- type R (D1, D2, D3 : Int) is record ... end record;
8171 -- type T1 is new R;
8172 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8173 -- type T3 is new T2;
8174 -- type T4 (Y : Int) is new T3 (Y, 99);
8175
8176 -- The following table summarizes the discriminants and stored
8177 -- discriminants in R and T1 through T4:
8178
8179 -- Type Discrim Stored Discrim Comment
8180 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8181 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8182 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8183 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8184 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8185
8186 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8187 -- find the corresponding discriminant in the parent type, while
8188 -- Original_Record_Component (abbreviated ORC below) the actual physical
8189 -- component that is renamed. Finally the field Is_Completely_Hidden
8190 -- (abbreviated ICH below) is set for all explicit stored discriminants
8191 -- (see einfo.ads for more info). For the above example this gives:
8192
8193 -- Discrim CD ORC ICH
8194 -- ^^^^^^^ ^^ ^^^ ^^^
8195 -- D1 in R empty itself no
8196 -- D2 in R empty itself no
8197 -- D3 in R empty itself no
8198
8199 -- D1 in T1 D1 in R itself no
8200 -- D2 in T1 D2 in R itself no
8201 -- D3 in T1 D3 in R itself no
8202
8203 -- X1 in T2 D3 in T1 D3 in T2 no
8204 -- X2 in T2 D1 in T1 D1 in T2 no
8205 -- D1 in T2 empty itself yes
8206 -- D2 in T2 empty itself yes
8207 -- D3 in T2 empty itself yes
8208
8209 -- X1 in T3 X1 in T2 D3 in T3 no
8210 -- X2 in T3 X2 in T2 D1 in T3 no
8211 -- D1 in T3 empty itself yes
8212 -- D2 in T3 empty itself yes
8213 -- D3 in T3 empty itself yes
8214
8215 -- Y in T4 X1 in T3 D3 in T4 no
8216 -- D1 in T4 empty itself yes
8217 -- D2 in T4 empty itself yes
8218 -- D3 in T4 empty itself yes
8219
8220 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8221
8222 -- Type derivation for tagged types is fairly straightforward. If no
8223 -- discriminants are specified by the derived type, these are inherited
8224 -- from the parent. No explicit stored discriminants are ever necessary.
8225 -- The only manipulation that is done to the tree is that of adding a
8226 -- _parent field with parent type and constrained to the same constraint
8227 -- specified for the parent in the derived type definition. For instance:
8228
8229 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8230 -- type T1 is new R with null record;
8231 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8232
8233 -- are changed into:
8234
8235 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8236 -- _parent : R (D1, D2, D3);
8237 -- end record;
8238
8239 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8240 -- _parent : T1 (X2, 88, X1);
8241 -- end record;
8242
8243 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8244 -- ORC and ICH fields are:
8245
8246 -- Discrim CD ORC ICH
8247 -- ^^^^^^^ ^^ ^^^ ^^^
8248 -- D1 in R empty itself no
8249 -- D2 in R empty itself no
8250 -- D3 in R empty itself no
8251
8252 -- D1 in T1 D1 in R D1 in R no
8253 -- D2 in T1 D2 in R D2 in R no
8254 -- D3 in T1 D3 in R D3 in R no
8255
8256 -- X1 in T2 D3 in T1 D3 in R no
8257 -- X2 in T2 D1 in T1 D1 in R no
8258
8259 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8260 --
8261 -- Regardless of whether we dealing with a tagged or untagged type
8262 -- we will transform all derived type declarations of the form
8263 --
8264 -- type T is new R (...) [with ...];
8265 -- or
8266 -- subtype S is R (...);
8267 -- type T is new S [with ...];
8268 -- into
8269 -- type BT is new R [with ...];
8270 -- subtype T is BT (...);
8271 --
8272 -- That is, the base derived type is constrained only if it has no
8273 -- discriminants. The reason for doing this is that GNAT's semantic model
8274 -- assumes that a base type with discriminants is unconstrained.
8275 --
8276 -- Note that, strictly speaking, the above transformation is not always
8277 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8278 --
8279 -- procedure B34011A is
8280 -- type REC (D : integer := 0) is record
8281 -- I : Integer;
8282 -- end record;
8283
8284 -- package P is
8285 -- type T6 is new Rec;
8286 -- function F return T6;
8287 -- end P;
8288
8289 -- use P;
8290 -- package Q6 is
8291 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8292 -- end Q6;
8293 --
8294 -- The definition of Q6.U is illegal. However transforming Q6.U into
8295
8296 -- type BaseU is new T6;
8297 -- subtype U is BaseU (Q6.F.I)
8298
8299 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8300 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8301 -- the transformation described above.
8302
8303 -- There is another instance where the above transformation is incorrect.
8304 -- Consider:
8305
8306 -- package Pack is
8307 -- type Base (D : Integer) is tagged null record;
8308 -- procedure P (X : Base);
8309
8310 -- type Der is new Base (2) with null record;
8311 -- procedure P (X : Der);
8312 -- end Pack;
8313
8314 -- Then the above transformation turns this into
8315
8316 -- type Der_Base is new Base with null record;
8317 -- -- procedure P (X : Base) is implicitly inherited here
8318 -- -- as procedure P (X : Der_Base).
8319
8320 -- subtype Der is Der_Base (2);
8321 -- procedure P (X : Der);
8322 -- -- The overriding of P (X : Der_Base) is illegal since we
8323 -- -- have a parameter conformance problem.
8324
8325 -- To get around this problem, after having semantically processed Der_Base
8326 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8327 -- Discriminant_Constraint from Der so that when parameter conformance is
8328 -- checked when P is overridden, no semantic errors are flagged.
8329
8330 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8331
8332 -- Regardless of whether we are dealing with a tagged or untagged type
8333 -- we will transform all derived type declarations of the form
8334
8335 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8336 -- type T is new R [with ...];
8337 -- into
8338 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8339
8340 -- The reason for such transformation is that it allows us to implement a
8341 -- very clean form of component inheritance as explained below.
8342
8343 -- Note that this transformation is not achieved by direct tree rewriting
8344 -- and manipulation, but rather by redoing the semantic actions that the
8345 -- above transformation will entail. This is done directly in routine
8346 -- Inherit_Components.
8347
8348 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8349
8350 -- In both tagged and untagged derived types, regular non discriminant
8351 -- components are inherited in the derived type from the parent type. In
8352 -- the absence of discriminants component, inheritance is straightforward
8353 -- as components can simply be copied from the parent.
8354
8355 -- If the parent has discriminants, inheriting components constrained with
8356 -- these discriminants requires caution. Consider the following example:
8357
8358 -- type R (D1, D2 : Positive) is [tagged] record
8359 -- S : String (D1 .. D2);
8360 -- end record;
8361
8362 -- type T1 is new R [with null record];
8363 -- type T2 (X : positive) is new R (1, X) [with null record];
8364
8365 -- As explained in 6. above, T1 is rewritten as
8366 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8367 -- which makes the treatment for T1 and T2 identical.
8368
8369 -- What we want when inheriting S, is that references to D1 and D2 in R are
8370 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8371 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8372 -- with either discriminant references in the derived type or expressions.
8373 -- This replacement is achieved as follows: before inheriting R's
8374 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8375 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8376 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8377 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8378 -- by String (1 .. X).
8379
8380 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8381
8382 -- We explain here the rules governing private type extensions relevant to
8383 -- type derivation. These rules are explained on the following example:
8384
8385 -- type D [(...)] is new A [(...)] with private; <-- partial view
8386 -- type D [(...)] is new P [(...)] with null record; <-- full view
8387
8388 -- Type A is called the ancestor subtype of the private extension.
8389 -- Type P is the parent type of the full view of the private extension. It
8390 -- must be A or a type derived from A.
8391
8392 -- The rules concerning the discriminants of private type extensions are
8393 -- [7.3(10-13)]:
8394
8395 -- o If a private extension inherits known discriminants from the ancestor
8396 -- subtype, then the full view must also inherit its discriminants from
8397 -- the ancestor subtype and the parent subtype of the full view must be
8398 -- constrained if and only if the ancestor subtype is constrained.
8399
8400 -- o If a partial view has unknown discriminants, then the full view may
8401 -- define a definite or an indefinite subtype, with or without
8402 -- discriminants.
8403
8404 -- o If a partial view has neither known nor unknown discriminants, then
8405 -- the full view must define a definite subtype.
8406
8407 -- o If the ancestor subtype of a private extension has constrained
8408 -- discriminants, then the parent subtype of the full view must impose a
8409 -- statically matching constraint on those discriminants.
8410
8411 -- This means that only the following forms of private extensions are
8412 -- allowed:
8413
8414 -- type D is new A with private; <-- partial view
8415 -- type D is new P with null record; <-- full view
8416
8417 -- If A has no discriminants than P has no discriminants, otherwise P must
8418 -- inherit A's discriminants.
8419
8420 -- type D is new A (...) with private; <-- partial view
8421 -- type D is new P (:::) with null record; <-- full view
8422
8423 -- P must inherit A's discriminants and (...) and (:::) must statically
8424 -- match.
8425
8426 -- subtype A is R (...);
8427 -- type D is new A with private; <-- partial view
8428 -- type D is new P with null record; <-- full view
8429
8430 -- P must have inherited R's discriminants and must be derived from A or
8431 -- any of its subtypes.
8432
8433 -- type D (..) is new A with private; <-- partial view
8434 -- type D (..) is new P [(:::)] with null record; <-- full view
8435
8436 -- No specific constraints on P's discriminants or constraint (:::).
8437 -- Note that A can be unconstrained, but the parent subtype P must either
8438 -- be constrained or (:::) must be present.
8439
8440 -- type D (..) is new A [(...)] with private; <-- partial view
8441 -- type D (..) is new P [(:::)] with null record; <-- full view
8442
8443 -- P's constraints on A's discriminants must statically match those
8444 -- imposed by (...).
8445
8446 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8447
8448 -- The full view of a private extension is handled exactly as described
8449 -- above. The model chose for the private view of a private extension is
8450 -- the same for what concerns discriminants (i.e. they receive the same
8451 -- treatment as in the tagged case). However, the private view of the
8452 -- private extension always inherits the components of the parent base,
8453 -- without replacing any discriminant reference. Strictly speaking this is
8454 -- incorrect. However, Gigi never uses this view to generate code so this
8455 -- is a purely semantic issue. In theory, a set of transformations similar
8456 -- to those given in 5. and 6. above could be applied to private views of
8457 -- private extensions to have the same model of component inheritance as
8458 -- for non private extensions. However, this is not done because it would
8459 -- further complicate private type processing. Semantically speaking, this
8460 -- leaves us in an uncomfortable situation. As an example consider:
8461
8462 -- package Pack is
8463 -- type R (D : integer) is tagged record
8464 -- S : String (1 .. D);
8465 -- end record;
8466 -- procedure P (X : R);
8467 -- type T is new R (1) with private;
8468 -- private
8469 -- type T is new R (1) with null record;
8470 -- end;
8471
8472 -- This is transformed into:
8473
8474 -- package Pack is
8475 -- type R (D : integer) is tagged record
8476 -- S : String (1 .. D);
8477 -- end record;
8478 -- procedure P (X : R);
8479 -- type T is new R (1) with private;
8480 -- private
8481 -- type BaseT is new R with null record;
8482 -- subtype T is BaseT (1);
8483 -- end;
8484
8485 -- (strictly speaking the above is incorrect Ada)
8486
8487 -- From the semantic standpoint the private view of private extension T
8488 -- should be flagged as constrained since one can clearly have
8489 --
8490 -- Obj : T;
8491 --
8492 -- in a unit withing Pack. However, when deriving subprograms for the
8493 -- private view of private extension T, T must be seen as unconstrained
8494 -- since T has discriminants (this is a constraint of the current
8495 -- subprogram derivation model). Thus, when processing the private view of
8496 -- a private extension such as T, we first mark T as unconstrained, we
8497 -- process it, we perform program derivation and just before returning from
8498 -- Build_Derived_Record_Type we mark T as constrained.
8499
8500 -- ??? Are there are other uncomfortable cases that we will have to
8501 -- deal with.
8502
8503 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8504
8505 -- Types that are derived from a visible record type and have a private
8506 -- extension present other peculiarities. They behave mostly like private
8507 -- types, but if they have primitive operations defined, these will not
8508 -- have the proper signatures for further inheritance, because other
8509 -- primitive operations will use the implicit base that we define for
8510 -- private derivations below. This affect subprogram inheritance (see
8511 -- Derive_Subprograms for details). We also derive the implicit base from
8512 -- the base type of the full view, so that the implicit base is a record
8513 -- type and not another private type, This avoids infinite loops.
8514
8515 procedure Build_Derived_Record_Type
8516 (N : Node_Id;
8517 Parent_Type : Entity_Id;
8518 Derived_Type : Entity_Id;
8519 Derive_Subps : Boolean := True)
8520 is
8521 Discriminant_Specs : constant Boolean :=
8522 Present (Discriminant_Specifications (N));
8523 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8524 Loc : constant Source_Ptr := Sloc (N);
8525 Private_Extension : constant Boolean :=
8526 Nkind (N) = N_Private_Extension_Declaration;
8527 Assoc_List : Elist_Id;
8528 Constraint_Present : Boolean;
8529 Constrs : Elist_Id;
8530 Discrim : Entity_Id;
8531 Indic : Node_Id;
8532 Inherit_Discrims : Boolean := False;
8533 Last_Discrim : Entity_Id;
8534 New_Base : Entity_Id;
8535 New_Decl : Node_Id;
8536 New_Discrs : Elist_Id;
8537 New_Indic : Node_Id;
8538 Parent_Base : Entity_Id;
8539 Save_Etype : Entity_Id;
8540 Save_Discr_Constr : Elist_Id;
8541 Save_Next_Entity : Entity_Id;
8542 Type_Def : Node_Id;
8543
8544 Discs : Elist_Id := New_Elmt_List;
8545 -- An empty Discs list means that there were no constraints in the
8546 -- subtype indication or that there was an error processing it.
8547
8548 begin
8549 if Ekind (Parent_Type) = E_Record_Type_With_Private
8550 and then Present (Full_View (Parent_Type))
8551 and then Has_Discriminants (Parent_Type)
8552 then
8553 Parent_Base := Base_Type (Full_View (Parent_Type));
8554 else
8555 Parent_Base := Base_Type (Parent_Type);
8556 end if;
8557
8558 -- AI05-0115: if this is a derivation from a private type in some
8559 -- other scope that may lead to invisible components for the derived
8560 -- type, mark it accordingly.
8561
8562 if Is_Private_Type (Parent_Type) then
8563 if Scope (Parent_Base) = Scope (Derived_Type) then
8564 null;
8565
8566 elsif In_Open_Scopes (Scope (Parent_Base))
8567 and then In_Private_Part (Scope (Parent_Base))
8568 then
8569 null;
8570
8571 else
8572 Set_Has_Private_Ancestor (Derived_Type);
8573 end if;
8574
8575 else
8576 Set_Has_Private_Ancestor
8577 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8578 end if;
8579
8580 -- Before we start the previously documented transformations, here is
8581 -- little fix for size and alignment of tagged types. Normally when we
8582 -- derive type D from type P, we copy the size and alignment of P as the
8583 -- default for D, and in the absence of explicit representation clauses
8584 -- for D, the size and alignment are indeed the same as the parent.
8585
8586 -- But this is wrong for tagged types, since fields may be added, and
8587 -- the default size may need to be larger, and the default alignment may
8588 -- need to be larger.
8589
8590 -- We therefore reset the size and alignment fields in the tagged case.
8591 -- Note that the size and alignment will in any case be at least as
8592 -- large as the parent type (since the derived type has a copy of the
8593 -- parent type in the _parent field)
8594
8595 -- The type is also marked as being tagged here, which is needed when
8596 -- processing components with a self-referential anonymous access type
8597 -- in the call to Check_Anonymous_Access_Components below. Note that
8598 -- this flag is also set later on for completeness.
8599
8600 if Is_Tagged then
8601 Set_Is_Tagged_Type (Derived_Type);
8602 Init_Size_Align (Derived_Type);
8603 end if;
8604
8605 -- STEP 0a: figure out what kind of derived type declaration we have
8606
8607 if Private_Extension then
8608 Type_Def := N;
8609 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8610 Set_Default_SSO (Derived_Type);
8611 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8612
8613 else
8614 Type_Def := Type_Definition (N);
8615
8616 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8617 -- Parent_Base can be a private type or private extension. However,
8618 -- for tagged types with an extension the newly added fields are
8619 -- visible and hence the Derived_Type is always an E_Record_Type.
8620 -- (except that the parent may have its own private fields).
8621 -- For untagged types we preserve the Ekind of the Parent_Base.
8622
8623 if Present (Record_Extension_Part (Type_Def)) then
8624 Set_Ekind (Derived_Type, E_Record_Type);
8625 Set_Default_SSO (Derived_Type);
8626 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8627
8628 -- Create internal access types for components with anonymous
8629 -- access types.
8630
8631 if Ada_Version >= Ada_2005 then
8632 Check_Anonymous_Access_Components
8633 (N, Derived_Type, Derived_Type,
8634 Component_List (Record_Extension_Part (Type_Def)));
8635 end if;
8636
8637 else
8638 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8639 end if;
8640 end if;
8641
8642 -- Indic can either be an N_Identifier if the subtype indication
8643 -- contains no constraint or an N_Subtype_Indication if the subtype
8644 -- indication has a constraint.
8645
8646 Indic := Subtype_Indication (Type_Def);
8647 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8648
8649 -- Check that the type has visible discriminants. The type may be
8650 -- a private type with unknown discriminants whose full view has
8651 -- discriminants which are invisible.
8652
8653 if Constraint_Present then
8654 if not Has_Discriminants (Parent_Base)
8655 or else
8656 (Has_Unknown_Discriminants (Parent_Base)
8657 and then Is_Private_Type (Parent_Base))
8658 then
8659 Error_Msg_N
8660 ("invalid constraint: type has no discriminant",
8661 Constraint (Indic));
8662
8663 Constraint_Present := False;
8664 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8665
8666 elsif Is_Constrained (Parent_Type) then
8667 Error_Msg_N
8668 ("invalid constraint: parent type is already constrained",
8669 Constraint (Indic));
8670
8671 Constraint_Present := False;
8672 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8673 end if;
8674 end if;
8675
8676 -- STEP 0b: If needed, apply transformation given in point 5. above
8677
8678 if not Private_Extension
8679 and then Has_Discriminants (Parent_Type)
8680 and then not Discriminant_Specs
8681 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8682 then
8683 -- First, we must analyze the constraint (see comment in point 5.)
8684 -- The constraint may come from the subtype indication of the full
8685 -- declaration.
8686
8687 if Constraint_Present then
8688 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8689
8690 -- If there is no explicit constraint, there might be one that is
8691 -- inherited from a constrained parent type. In that case verify that
8692 -- it conforms to the constraint in the partial view. In perverse
8693 -- cases the parent subtypes of the partial and full view can have
8694 -- different constraints.
8695
8696 elsif Present (Stored_Constraint (Parent_Type)) then
8697 New_Discrs := Stored_Constraint (Parent_Type);
8698
8699 else
8700 New_Discrs := No_Elist;
8701 end if;
8702
8703 if Has_Discriminants (Derived_Type)
8704 and then Has_Private_Declaration (Derived_Type)
8705 and then Present (Discriminant_Constraint (Derived_Type))
8706 and then Present (New_Discrs)
8707 then
8708 -- Verify that constraints of the full view statically match
8709 -- those given in the partial view.
8710
8711 declare
8712 C1, C2 : Elmt_Id;
8713
8714 begin
8715 C1 := First_Elmt (New_Discrs);
8716 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8717 while Present (C1) and then Present (C2) loop
8718 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8719 or else
8720 (Is_OK_Static_Expression (Node (C1))
8721 and then Is_OK_Static_Expression (Node (C2))
8722 and then
8723 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8724 then
8725 null;
8726
8727 else
8728 if Constraint_Present then
8729 Error_Msg_N
8730 ("constraint not conformant to previous declaration",
8731 Node (C1));
8732 else
8733 Error_Msg_N
8734 ("constraint of full view is incompatible "
8735 & "with partial view", N);
8736 end if;
8737 end if;
8738
8739 Next_Elmt (C1);
8740 Next_Elmt (C2);
8741 end loop;
8742 end;
8743 end if;
8744
8745 -- Insert and analyze the declaration for the unconstrained base type
8746
8747 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8748
8749 New_Decl :=
8750 Make_Full_Type_Declaration (Loc,
8751 Defining_Identifier => New_Base,
8752 Type_Definition =>
8753 Make_Derived_Type_Definition (Loc,
8754 Abstract_Present => Abstract_Present (Type_Def),
8755 Limited_Present => Limited_Present (Type_Def),
8756 Subtype_Indication =>
8757 New_Occurrence_Of (Parent_Base, Loc),
8758 Record_Extension_Part =>
8759 Relocate_Node (Record_Extension_Part (Type_Def)),
8760 Interface_List => Interface_List (Type_Def)));
8761
8762 Set_Parent (New_Decl, Parent (N));
8763 Mark_Rewrite_Insertion (New_Decl);
8764 Insert_Before (N, New_Decl);
8765
8766 -- In the extension case, make sure ancestor is frozen appropriately
8767 -- (see also non-discriminated case below).
8768
8769 if Present (Record_Extension_Part (Type_Def))
8770 or else Is_Interface (Parent_Base)
8771 then
8772 Freeze_Before (New_Decl, Parent_Type);
8773 end if;
8774
8775 -- Note that this call passes False for the Derive_Subps parameter
8776 -- because subprogram derivation is deferred until after creating
8777 -- the subtype (see below).
8778
8779 Build_Derived_Type
8780 (New_Decl, Parent_Base, New_Base,
8781 Is_Completion => False, Derive_Subps => False);
8782
8783 -- ??? This needs re-examination to determine whether the
8784 -- above call can simply be replaced by a call to Analyze.
8785
8786 Set_Analyzed (New_Decl);
8787
8788 -- Insert and analyze the declaration for the constrained subtype
8789
8790 if Constraint_Present then
8791 New_Indic :=
8792 Make_Subtype_Indication (Loc,
8793 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8794 Constraint => Relocate_Node (Constraint (Indic)));
8795
8796 else
8797 declare
8798 Constr_List : constant List_Id := New_List;
8799 C : Elmt_Id;
8800 Expr : Node_Id;
8801
8802 begin
8803 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8804 while Present (C) loop
8805 Expr := Node (C);
8806
8807 -- It is safe here to call New_Copy_Tree since we called
8808 -- Force_Evaluation on each constraint previously
8809 -- in Build_Discriminant_Constraints.
8810
8811 Append (New_Copy_Tree (Expr), To => Constr_List);
8812
8813 Next_Elmt (C);
8814 end loop;
8815
8816 New_Indic :=
8817 Make_Subtype_Indication (Loc,
8818 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8819 Constraint =>
8820 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8821 end;
8822 end if;
8823
8824 Rewrite (N,
8825 Make_Subtype_Declaration (Loc,
8826 Defining_Identifier => Derived_Type,
8827 Subtype_Indication => New_Indic));
8828
8829 Analyze (N);
8830
8831 -- Derivation of subprograms must be delayed until the full subtype
8832 -- has been established, to ensure proper overriding of subprograms
8833 -- inherited by full types. If the derivations occurred as part of
8834 -- the call to Build_Derived_Type above, then the check for type
8835 -- conformance would fail because earlier primitive subprograms
8836 -- could still refer to the full type prior the change to the new
8837 -- subtype and hence would not match the new base type created here.
8838 -- Subprograms are not derived, however, when Derive_Subps is False
8839 -- (since otherwise there could be redundant derivations).
8840
8841 if Derive_Subps then
8842 Derive_Subprograms (Parent_Type, Derived_Type);
8843 end if;
8844
8845 -- For tagged types the Discriminant_Constraint of the new base itype
8846 -- is inherited from the first subtype so that no subtype conformance
8847 -- problem arise when the first subtype overrides primitive
8848 -- operations inherited by the implicit base type.
8849
8850 if Is_Tagged then
8851 Set_Discriminant_Constraint
8852 (New_Base, Discriminant_Constraint (Derived_Type));
8853 end if;
8854
8855 return;
8856 end if;
8857
8858 -- If we get here Derived_Type will have no discriminants or it will be
8859 -- a discriminated unconstrained base type.
8860
8861 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8862
8863 if Is_Tagged then
8864
8865 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8866 -- The declaration of a specific descendant of an interface type
8867 -- freezes the interface type (RM 13.14).
8868
8869 if not Private_Extension or else Is_Interface (Parent_Base) then
8870 Freeze_Before (N, Parent_Type);
8871 end if;
8872
8873 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8874 -- cannot be declared at a deeper level than its parent type is
8875 -- removed. The check on derivation within a generic body is also
8876 -- relaxed, but there's a restriction that a derived tagged type
8877 -- cannot be declared in a generic body if it's derived directly
8878 -- or indirectly from a formal type of that generic.
8879
8880 if Ada_Version >= Ada_2005 then
8881 if Present (Enclosing_Generic_Body (Derived_Type)) then
8882 declare
8883 Ancestor_Type : Entity_Id;
8884
8885 begin
8886 -- Check to see if any ancestor of the derived type is a
8887 -- formal type.
8888
8889 Ancestor_Type := Parent_Type;
8890 while not Is_Generic_Type (Ancestor_Type)
8891 and then Etype (Ancestor_Type) /= Ancestor_Type
8892 loop
8893 Ancestor_Type := Etype (Ancestor_Type);
8894 end loop;
8895
8896 -- If the derived type does have a formal type as an
8897 -- ancestor, then it's an error if the derived type is
8898 -- declared within the body of the generic unit that
8899 -- declares the formal type in its generic formal part. It's
8900 -- sufficient to check whether the ancestor type is declared
8901 -- inside the same generic body as the derived type (such as
8902 -- within a nested generic spec), in which case the
8903 -- derivation is legal. If the formal type is declared
8904 -- outside of that generic body, then it's guaranteed that
8905 -- the derived type is declared within the generic body of
8906 -- the generic unit declaring the formal type.
8907
8908 if Is_Generic_Type (Ancestor_Type)
8909 and then Enclosing_Generic_Body (Ancestor_Type) /=
8910 Enclosing_Generic_Body (Derived_Type)
8911 then
8912 Error_Msg_NE
8913 ("parent type of& must not be descendant of formal type"
8914 & " of an enclosing generic body",
8915 Indic, Derived_Type);
8916 end if;
8917 end;
8918 end if;
8919
8920 elsif Type_Access_Level (Derived_Type) /=
8921 Type_Access_Level (Parent_Type)
8922 and then not Is_Generic_Type (Derived_Type)
8923 then
8924 if Is_Controlled (Parent_Type) then
8925 Error_Msg_N
8926 ("controlled type must be declared at the library level",
8927 Indic);
8928 else
8929 Error_Msg_N
8930 ("type extension at deeper accessibility level than parent",
8931 Indic);
8932 end if;
8933
8934 else
8935 declare
8936 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8937 begin
8938 if Present (GB)
8939 and then GB /= Enclosing_Generic_Body (Parent_Base)
8940 then
8941 Error_Msg_NE
8942 ("parent type of& must not be outside generic body"
8943 & " (RM 3.9.1(4))",
8944 Indic, Derived_Type);
8945 end if;
8946 end;
8947 end if;
8948 end if;
8949
8950 -- Ada 2005 (AI-251)
8951
8952 if Ada_Version >= Ada_2005 and then Is_Tagged then
8953
8954 -- "The declaration of a specific descendant of an interface type
8955 -- freezes the interface type" (RM 13.14).
8956
8957 declare
8958 Iface : Node_Id;
8959 begin
8960 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8961 Iface := First (Interface_List (Type_Def));
8962 while Present (Iface) loop
8963 Freeze_Before (N, Etype (Iface));
8964 Next (Iface);
8965 end loop;
8966 end if;
8967 end;
8968 end if;
8969
8970 -- STEP 1b : preliminary cleanup of the full view of private types
8971
8972 -- If the type is already marked as having discriminants, then it's the
8973 -- completion of a private type or private extension and we need to
8974 -- retain the discriminants from the partial view if the current
8975 -- declaration has Discriminant_Specifications so that we can verify
8976 -- conformance. However, we must remove any existing components that
8977 -- were inherited from the parent (and attached in Copy_And_Swap)
8978 -- because the full type inherits all appropriate components anyway, and
8979 -- we do not want the partial view's components interfering.
8980
8981 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8982 Discrim := First_Discriminant (Derived_Type);
8983 loop
8984 Last_Discrim := Discrim;
8985 Next_Discriminant (Discrim);
8986 exit when No (Discrim);
8987 end loop;
8988
8989 Set_Last_Entity (Derived_Type, Last_Discrim);
8990
8991 -- In all other cases wipe out the list of inherited components (even
8992 -- inherited discriminants), it will be properly rebuilt here.
8993
8994 else
8995 Set_First_Entity (Derived_Type, Empty);
8996 Set_Last_Entity (Derived_Type, Empty);
8997 end if;
8998
8999 -- STEP 1c: Initialize some flags for the Derived_Type
9000
9001 -- The following flags must be initialized here so that
9002 -- Process_Discriminants can check that discriminants of tagged types do
9003 -- not have a default initial value and that access discriminants are
9004 -- only specified for limited records. For completeness, these flags are
9005 -- also initialized along with all the other flags below.
9006
9007 -- AI-419: Limitedness is not inherited from an interface parent, so to
9008 -- be limited in that case the type must be explicitly declared as
9009 -- limited. However, task and protected interfaces are always limited.
9010
9011 if Limited_Present (Type_Def) then
9012 Set_Is_Limited_Record (Derived_Type);
9013
9014 elsif Is_Limited_Record (Parent_Type)
9015 or else (Present (Full_View (Parent_Type))
9016 and then Is_Limited_Record (Full_View (Parent_Type)))
9017 then
9018 if not Is_Interface (Parent_Type)
9019 or else Is_Synchronized_Interface (Parent_Type)
9020 or else Is_Protected_Interface (Parent_Type)
9021 or else Is_Task_Interface (Parent_Type)
9022 then
9023 Set_Is_Limited_Record (Derived_Type);
9024 end if;
9025 end if;
9026
9027 -- STEP 2a: process discriminants of derived type if any
9028
9029 Push_Scope (Derived_Type);
9030
9031 if Discriminant_Specs then
9032 Set_Has_Unknown_Discriminants (Derived_Type, False);
9033
9034 -- The following call initializes fields Has_Discriminants and
9035 -- Discriminant_Constraint, unless we are processing the completion
9036 -- of a private type declaration.
9037
9038 Check_Or_Process_Discriminants (N, Derived_Type);
9039
9040 -- For untagged types, the constraint on the Parent_Type must be
9041 -- present and is used to rename the discriminants.
9042
9043 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9044 Error_Msg_N ("untagged parent must have discriminants", Indic);
9045
9046 elsif not Is_Tagged and then not Constraint_Present then
9047 Error_Msg_N
9048 ("discriminant constraint needed for derived untagged records",
9049 Indic);
9050
9051 -- Otherwise the parent subtype must be constrained unless we have a
9052 -- private extension.
9053
9054 elsif not Constraint_Present
9055 and then not Private_Extension
9056 and then not Is_Constrained (Parent_Type)
9057 then
9058 Error_Msg_N
9059 ("unconstrained type not allowed in this context", Indic);
9060
9061 elsif Constraint_Present then
9062 -- The following call sets the field Corresponding_Discriminant
9063 -- for the discriminants in the Derived_Type.
9064
9065 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9066
9067 -- For untagged types all new discriminants must rename
9068 -- discriminants in the parent. For private extensions new
9069 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9070
9071 Discrim := First_Discriminant (Derived_Type);
9072 while Present (Discrim) loop
9073 if not Is_Tagged
9074 and then No (Corresponding_Discriminant (Discrim))
9075 then
9076 Error_Msg_N
9077 ("new discriminants must constrain old ones", Discrim);
9078
9079 elsif Private_Extension
9080 and then Present (Corresponding_Discriminant (Discrim))
9081 then
9082 Error_Msg_N
9083 ("only static constraints allowed for parent"
9084 & " discriminants in the partial view", Indic);
9085 exit;
9086 end if;
9087
9088 -- If a new discriminant is used in the constraint, then its
9089 -- subtype must be statically compatible with the parent
9090 -- discriminant's subtype (3.7(15)).
9091
9092 -- However, if the record contains an array constrained by
9093 -- the discriminant but with some different bound, the compiler
9094 -- tries to create a smaller range for the discriminant type.
9095 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9096 -- the discriminant type is a scalar type, the check must use
9097 -- the original discriminant type in the parent declaration.
9098
9099 declare
9100 Corr_Disc : constant Entity_Id :=
9101 Corresponding_Discriminant (Discrim);
9102 Disc_Type : constant Entity_Id := Etype (Discrim);
9103 Corr_Type : Entity_Id;
9104
9105 begin
9106 if Present (Corr_Disc) then
9107 if Is_Scalar_Type (Disc_Type) then
9108 Corr_Type :=
9109 Entity (Discriminant_Type (Parent (Corr_Disc)));
9110 else
9111 Corr_Type := Etype (Corr_Disc);
9112 end if;
9113
9114 if not
9115 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9116 then
9117 Error_Msg_N
9118 ("subtype must be compatible "
9119 & "with parent discriminant",
9120 Discrim);
9121 end if;
9122 end if;
9123 end;
9124
9125 Next_Discriminant (Discrim);
9126 end loop;
9127
9128 -- Check whether the constraints of the full view statically
9129 -- match those imposed by the parent subtype [7.3(13)].
9130
9131 if Present (Stored_Constraint (Derived_Type)) then
9132 declare
9133 C1, C2 : Elmt_Id;
9134
9135 begin
9136 C1 := First_Elmt (Discs);
9137 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9138 while Present (C1) and then Present (C2) loop
9139 if not
9140 Fully_Conformant_Expressions (Node (C1), Node (C2))
9141 then
9142 Error_Msg_N
9143 ("not conformant with previous declaration",
9144 Node (C1));
9145 end if;
9146
9147 Next_Elmt (C1);
9148 Next_Elmt (C2);
9149 end loop;
9150 end;
9151 end if;
9152 end if;
9153
9154 -- STEP 2b: No new discriminants, inherit discriminants if any
9155
9156 else
9157 if Private_Extension then
9158 Set_Has_Unknown_Discriminants
9159 (Derived_Type,
9160 Has_Unknown_Discriminants (Parent_Type)
9161 or else Unknown_Discriminants_Present (N));
9162
9163 -- The partial view of the parent may have unknown discriminants,
9164 -- but if the full view has discriminants and the parent type is
9165 -- in scope they must be inherited.
9166
9167 elsif Has_Unknown_Discriminants (Parent_Type)
9168 and then
9169 (not Has_Discriminants (Parent_Type)
9170 or else not In_Open_Scopes (Scope (Parent_Base)))
9171 then
9172 Set_Has_Unknown_Discriminants (Derived_Type);
9173 end if;
9174
9175 if not Has_Unknown_Discriminants (Derived_Type)
9176 and then not Has_Unknown_Discriminants (Parent_Base)
9177 and then Has_Discriminants (Parent_Type)
9178 then
9179 Inherit_Discrims := True;
9180 Set_Has_Discriminants
9181 (Derived_Type, True);
9182 Set_Discriminant_Constraint
9183 (Derived_Type, Discriminant_Constraint (Parent_Base));
9184 end if;
9185
9186 -- The following test is true for private types (remember
9187 -- transformation 5. is not applied to those) and in an error
9188 -- situation.
9189
9190 if Constraint_Present then
9191 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9192 end if;
9193
9194 -- For now mark a new derived type as constrained only if it has no
9195 -- discriminants. At the end of Build_Derived_Record_Type we properly
9196 -- set this flag in the case of private extensions. See comments in
9197 -- point 9. just before body of Build_Derived_Record_Type.
9198
9199 Set_Is_Constrained
9200 (Derived_Type,
9201 not (Inherit_Discrims
9202 or else Has_Unknown_Discriminants (Derived_Type)));
9203 end if;
9204
9205 -- STEP 3: initialize fields of derived type
9206
9207 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9208 Set_Stored_Constraint (Derived_Type, No_Elist);
9209
9210 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9211 -- but cannot be interfaces
9212
9213 if not Private_Extension
9214 and then Ekind (Derived_Type) /= E_Private_Type
9215 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9216 then
9217 if Interface_Present (Type_Def) then
9218 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9219 end if;
9220
9221 Set_Interfaces (Derived_Type, No_Elist);
9222 end if;
9223
9224 -- Fields inherited from the Parent_Type
9225
9226 Set_Has_Specified_Layout
9227 (Derived_Type, Has_Specified_Layout (Parent_Type));
9228 Set_Is_Limited_Composite
9229 (Derived_Type, Is_Limited_Composite (Parent_Type));
9230 Set_Is_Private_Composite
9231 (Derived_Type, Is_Private_Composite (Parent_Type));
9232
9233 if Is_Tagged_Type (Parent_Type) then
9234 Set_No_Tagged_Streams_Pragma
9235 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9236 end if;
9237
9238 -- Fields inherited from the Parent_Base
9239
9240 Set_Has_Controlled_Component
9241 (Derived_Type, Has_Controlled_Component (Parent_Base));
9242 Set_Has_Non_Standard_Rep
9243 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9244 Set_Has_Primitive_Operations
9245 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9246
9247 -- Set fields for private derived types
9248
9249 if Is_Private_Type (Derived_Type) then
9250 Set_Depends_On_Private (Derived_Type, True);
9251 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9252 end if;
9253
9254 -- Inherit fields for non-private types. If this is the completion of a
9255 -- derivation from a private type, the parent itself is private and the
9256 -- attributes come from its full view, which must be present.
9257
9258 if Is_Record_Type (Derived_Type) then
9259 declare
9260 Parent_Full : Entity_Id;
9261
9262 begin
9263 if Is_Private_Type (Parent_Base)
9264 and then not Is_Record_Type (Parent_Base)
9265 then
9266 Parent_Full := Full_View (Parent_Base);
9267 else
9268 Parent_Full := Parent_Base;
9269 end if;
9270
9271 Set_Component_Alignment
9272 (Derived_Type, Component_Alignment (Parent_Full));
9273 Set_C_Pass_By_Copy
9274 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9275 Set_Has_Complex_Representation
9276 (Derived_Type, Has_Complex_Representation (Parent_Full));
9277
9278 -- For untagged types, inherit the layout by default to avoid
9279 -- costly changes of representation for type conversions.
9280
9281 if not Is_Tagged then
9282 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9283 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9284 end if;
9285 end;
9286 end if;
9287
9288 -- Set fields for tagged types
9289
9290 if Is_Tagged then
9291 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9292
9293 -- All tagged types defined in Ada.Finalization are controlled
9294
9295 if Chars (Scope (Derived_Type)) = Name_Finalization
9296 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9297 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9298 then
9299 Set_Is_Controlled_Active (Derived_Type);
9300 else
9301 Set_Is_Controlled_Active
9302 (Derived_Type, Is_Controlled_Active (Parent_Base));
9303 end if;
9304
9305 -- Minor optimization: there is no need to generate the class-wide
9306 -- entity associated with an underlying record view.
9307
9308 if not Is_Underlying_Record_View (Derived_Type) then
9309 Make_Class_Wide_Type (Derived_Type);
9310 end if;
9311
9312 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9313
9314 if Has_Discriminants (Derived_Type)
9315 and then Constraint_Present
9316 then
9317 Set_Stored_Constraint
9318 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9319 end if;
9320
9321 if Ada_Version >= Ada_2005 then
9322 declare
9323 Ifaces_List : Elist_Id;
9324
9325 begin
9326 -- Checks rules 3.9.4 (13/2 and 14/2)
9327
9328 if Comes_From_Source (Derived_Type)
9329 and then not Is_Private_Type (Derived_Type)
9330 and then Is_Interface (Parent_Type)
9331 and then not Is_Interface (Derived_Type)
9332 then
9333 if Is_Task_Interface (Parent_Type) then
9334 Error_Msg_N
9335 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9336 Derived_Type);
9337
9338 elsif Is_Protected_Interface (Parent_Type) then
9339 Error_Msg_N
9340 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9341 Derived_Type);
9342 end if;
9343 end if;
9344
9345 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9346
9347 Check_Interfaces (N, Type_Def);
9348
9349 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9350 -- not already in the parents.
9351
9352 Collect_Interfaces
9353 (T => Derived_Type,
9354 Ifaces_List => Ifaces_List,
9355 Exclude_Parents => True);
9356
9357 Set_Interfaces (Derived_Type, Ifaces_List);
9358
9359 -- If the derived type is the anonymous type created for
9360 -- a declaration whose parent has a constraint, propagate
9361 -- the interface list to the source type. This must be done
9362 -- prior to the completion of the analysis of the source type
9363 -- because the components in the extension may contain current
9364 -- instances whose legality depends on some ancestor.
9365
9366 if Is_Itype (Derived_Type) then
9367 declare
9368 Def : constant Node_Id :=
9369 Associated_Node_For_Itype (Derived_Type);
9370 begin
9371 if Present (Def)
9372 and then Nkind (Def) = N_Full_Type_Declaration
9373 then
9374 Set_Interfaces
9375 (Defining_Identifier (Def), Ifaces_List);
9376 end if;
9377 end;
9378 end if;
9379
9380 -- A type extension is automatically Ghost when one of its
9381 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9382 -- also inherited when the parent type is Ghost, but this is
9383 -- done in Build_Derived_Type as the mechanism also handles
9384 -- untagged derivations.
9385
9386 if Implements_Ghost_Interface (Derived_Type) then
9387 Set_Is_Ghost_Entity (Derived_Type);
9388 end if;
9389 end;
9390 end if;
9391 end if;
9392
9393 -- STEP 4: Inherit components from the parent base and constrain them.
9394 -- Apply the second transformation described in point 6. above.
9395
9396 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9397 or else not Has_Discriminants (Parent_Type)
9398 or else not Is_Constrained (Parent_Type)
9399 then
9400 Constrs := Discs;
9401 else
9402 Constrs := Discriminant_Constraint (Parent_Type);
9403 end if;
9404
9405 Assoc_List :=
9406 Inherit_Components
9407 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9408
9409 -- STEP 5a: Copy the parent record declaration for untagged types
9410
9411 Set_Has_Implicit_Dereference
9412 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9413
9414 if not Is_Tagged then
9415
9416 -- Discriminant_Constraint (Derived_Type) has been properly
9417 -- constructed. Save it and temporarily set it to Empty because we
9418 -- do not want the call to New_Copy_Tree below to mess this list.
9419
9420 if Has_Discriminants (Derived_Type) then
9421 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9422 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9423 else
9424 Save_Discr_Constr := No_Elist;
9425 end if;
9426
9427 -- Save the Etype field of Derived_Type. It is correctly set now,
9428 -- but the call to New_Copy tree may remap it to point to itself,
9429 -- which is not what we want. Ditto for the Next_Entity field.
9430
9431 Save_Etype := Etype (Derived_Type);
9432 Save_Next_Entity := Next_Entity (Derived_Type);
9433
9434 -- Assoc_List maps all stored discriminants in the Parent_Base to
9435 -- stored discriminants in the Derived_Type. It is fundamental that
9436 -- no types or itypes with discriminants other than the stored
9437 -- discriminants appear in the entities declared inside
9438 -- Derived_Type, since the back end cannot deal with it.
9439
9440 New_Decl :=
9441 New_Copy_Tree
9442 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9443 Copy_Dimensions_Of_Components (Derived_Type);
9444
9445 -- Restore the fields saved prior to the New_Copy_Tree call
9446 -- and compute the stored constraint.
9447
9448 Set_Etype (Derived_Type, Save_Etype);
9449 Link_Entities (Derived_Type, Save_Next_Entity);
9450
9451 if Has_Discriminants (Derived_Type) then
9452 Set_Discriminant_Constraint
9453 (Derived_Type, Save_Discr_Constr);
9454 Set_Stored_Constraint
9455 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9456
9457 Replace_Components (Derived_Type, New_Decl);
9458 end if;
9459
9460 -- Insert the new derived type declaration
9461
9462 Rewrite (N, New_Decl);
9463
9464 -- STEP 5b: Complete the processing for record extensions in generics
9465
9466 -- There is no completion for record extensions declared in the
9467 -- parameter part of a generic, so we need to complete processing for
9468 -- these generic record extensions here. The Record_Type_Definition call
9469 -- will change the Ekind of the components from E_Void to E_Component.
9470
9471 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9472 Record_Type_Definition (Empty, Derived_Type);
9473
9474 -- STEP 5c: Process the record extension for non private tagged types
9475
9476 elsif not Private_Extension then
9477 Expand_Record_Extension (Derived_Type, Type_Def);
9478
9479 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9480 -- derived type to propagate some semantic information. This led
9481 -- to other ASIS failures and has been removed.
9482
9483 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9484 -- implemented interfaces if we are in expansion mode
9485
9486 if Expander_Active
9487 and then Has_Interfaces (Derived_Type)
9488 then
9489 Add_Interface_Tag_Components (N, Derived_Type);
9490 end if;
9491
9492 -- Analyze the record extension
9493
9494 Record_Type_Definition
9495 (Record_Extension_Part (Type_Def), Derived_Type);
9496 end if;
9497
9498 End_Scope;
9499
9500 -- Nothing else to do if there is an error in the derivation.
9501 -- An unusual case: the full view may be derived from a type in an
9502 -- instance, when the partial view was used illegally as an actual
9503 -- in that instance, leading to a circular definition.
9504
9505 if Etype (Derived_Type) = Any_Type
9506 or else Etype (Parent_Type) = Derived_Type
9507 then
9508 return;
9509 end if;
9510
9511 -- Set delayed freeze and then derive subprograms, we need to do
9512 -- this in this order so that derived subprograms inherit the
9513 -- derived freeze if necessary.
9514
9515 Set_Has_Delayed_Freeze (Derived_Type);
9516
9517 if Derive_Subps then
9518 Derive_Subprograms (Parent_Type, Derived_Type);
9519 end if;
9520
9521 -- If we have a private extension which defines a constrained derived
9522 -- type mark as constrained here after we have derived subprograms. See
9523 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9524
9525 if Private_Extension and then Inherit_Discrims then
9526 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9527 Set_Is_Constrained (Derived_Type, True);
9528 Set_Discriminant_Constraint (Derived_Type, Discs);
9529
9530 elsif Is_Constrained (Parent_Type) then
9531 Set_Is_Constrained
9532 (Derived_Type, True);
9533 Set_Discriminant_Constraint
9534 (Derived_Type, Discriminant_Constraint (Parent_Type));
9535 end if;
9536 end if;
9537
9538 -- Update the class-wide type, which shares the now-completed entity
9539 -- list with its specific type. In case of underlying record views,
9540 -- we do not generate the corresponding class wide entity.
9541
9542 if Is_Tagged
9543 and then not Is_Underlying_Record_View (Derived_Type)
9544 then
9545 Set_First_Entity
9546 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9547 Set_Last_Entity
9548 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9549 end if;
9550
9551 Check_Function_Writable_Actuals (N);
9552 end Build_Derived_Record_Type;
9553
9554 ------------------------
9555 -- Build_Derived_Type --
9556 ------------------------
9557
9558 procedure Build_Derived_Type
9559 (N : Node_Id;
9560 Parent_Type : Entity_Id;
9561 Derived_Type : Entity_Id;
9562 Is_Completion : Boolean;
9563 Derive_Subps : Boolean := True)
9564 is
9565 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9566
9567 begin
9568 -- Set common attributes
9569
9570 Set_Scope (Derived_Type, Current_Scope);
9571 Set_Etype (Derived_Type, Parent_Base);
9572 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9573 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9574
9575 Set_Size_Info (Derived_Type, Parent_Type);
9576 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9577
9578 Set_Is_Controlled_Active
9579 (Derived_Type, Is_Controlled_Active (Parent_Type));
9580
9581 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9582 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9583 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9584
9585 if Is_Tagged_Type (Derived_Type) then
9586 Set_No_Tagged_Streams_Pragma
9587 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9588 end if;
9589
9590 -- If the parent has primitive routines, set the derived type link
9591
9592 if Has_Primitive_Operations (Parent_Type) then
9593 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9594 end if;
9595
9596 -- If the parent type is a private subtype, the convention on the base
9597 -- type may be set in the private part, and not propagated to the
9598 -- subtype until later, so we obtain the convention from the base type.
9599
9600 Set_Convention (Derived_Type, Convention (Parent_Base));
9601
9602 -- Set SSO default for record or array type
9603
9604 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9605 and then Is_Base_Type (Derived_Type)
9606 then
9607 Set_Default_SSO (Derived_Type);
9608 end if;
9609
9610 -- A derived type inherits the Default_Initial_Condition pragma coming
9611 -- from any parent type within the derivation chain.
9612
9613 if Has_DIC (Parent_Type) then
9614 Set_Has_Inherited_DIC (Derived_Type);
9615 end if;
9616
9617 -- A derived type inherits any class-wide invariants coming from a
9618 -- parent type or an interface. Note that the invariant procedure of
9619 -- the parent type should not be inherited because the derived type may
9620 -- define invariants of its own.
9621
9622 if not Is_Interface (Derived_Type) then
9623 if Has_Inherited_Invariants (Parent_Type)
9624 or else Has_Inheritable_Invariants (Parent_Type)
9625 then
9626 Set_Has_Inherited_Invariants (Derived_Type);
9627
9628 elsif Is_Concurrent_Type (Derived_Type)
9629 or else Is_Tagged_Type (Derived_Type)
9630 then
9631 declare
9632 Iface : Entity_Id;
9633 Ifaces : Elist_Id;
9634 Iface_Elmt : Elmt_Id;
9635
9636 begin
9637 Collect_Interfaces
9638 (T => Derived_Type,
9639 Ifaces_List => Ifaces,
9640 Exclude_Parents => True);
9641
9642 if Present (Ifaces) then
9643 Iface_Elmt := First_Elmt (Ifaces);
9644 while Present (Iface_Elmt) loop
9645 Iface := Node (Iface_Elmt);
9646
9647 if Has_Inheritable_Invariants (Iface) then
9648 Set_Has_Inherited_Invariants (Derived_Type);
9649 exit;
9650 end if;
9651
9652 Next_Elmt (Iface_Elmt);
9653 end loop;
9654 end if;
9655 end;
9656 end if;
9657 end if;
9658
9659 -- We similarly inherit predicates. Note that for scalar derived types
9660 -- the predicate is inherited from the first subtype, and not from its
9661 -- (anonymous) base type.
9662
9663 if Has_Predicates (Parent_Type)
9664 or else Has_Predicates (First_Subtype (Parent_Type))
9665 then
9666 Set_Has_Predicates (Derived_Type);
9667 end if;
9668
9669 -- The derived type inherits representation clauses from the parent
9670 -- type, and from any interfaces.
9671
9672 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9673
9674 declare
9675 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9676 begin
9677 while Present (Iface) loop
9678 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9679 Next (Iface);
9680 end loop;
9681 end;
9682
9683 -- If the parent type has delayed rep aspects, then mark the derived
9684 -- type as possibly inheriting a delayed rep aspect.
9685
9686 if Has_Delayed_Rep_Aspects (Parent_Type) then
9687 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9688 end if;
9689
9690 -- A derived type becomes Ghost when its parent type is also Ghost
9691 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9692 -- directly inherited because the Ghost policy in effect may differ.
9693
9694 if Is_Ghost_Entity (Parent_Type) then
9695 Set_Is_Ghost_Entity (Derived_Type);
9696 end if;
9697
9698 -- Type dependent processing
9699
9700 case Ekind (Parent_Type) is
9701 when Numeric_Kind =>
9702 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9703
9704 when Array_Kind =>
9705 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9706
9707 when Class_Wide_Kind
9708 | E_Record_Subtype
9709 | E_Record_Type
9710 =>
9711 Build_Derived_Record_Type
9712 (N, Parent_Type, Derived_Type, Derive_Subps);
9713 return;
9714
9715 when Enumeration_Kind =>
9716 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9717
9718 when Access_Kind =>
9719 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9720
9721 when Incomplete_Or_Private_Kind =>
9722 Build_Derived_Private_Type
9723 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9724
9725 -- For discriminated types, the derivation includes deriving
9726 -- primitive operations. For others it is done below.
9727
9728 if Is_Tagged_Type (Parent_Type)
9729 or else Has_Discriminants (Parent_Type)
9730 or else (Present (Full_View (Parent_Type))
9731 and then Has_Discriminants (Full_View (Parent_Type)))
9732 then
9733 return;
9734 end if;
9735
9736 when Concurrent_Kind =>
9737 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9738
9739 when others =>
9740 raise Program_Error;
9741 end case;
9742
9743 -- Nothing more to do if some error occurred
9744
9745 if Etype (Derived_Type) = Any_Type then
9746 return;
9747 end if;
9748
9749 -- Set delayed freeze and then derive subprograms, we need to do this
9750 -- in this order so that derived subprograms inherit the derived freeze
9751 -- if necessary.
9752
9753 Set_Has_Delayed_Freeze (Derived_Type);
9754
9755 if Derive_Subps then
9756 Derive_Subprograms (Parent_Type, Derived_Type);
9757 end if;
9758
9759 Set_Has_Primitive_Operations
9760 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9761 end Build_Derived_Type;
9762
9763 -----------------------
9764 -- Build_Discriminal --
9765 -----------------------
9766
9767 procedure Build_Discriminal (Discrim : Entity_Id) is
9768 D_Minal : Entity_Id;
9769 CR_Disc : Entity_Id;
9770
9771 begin
9772 -- A discriminal has the same name as the discriminant
9773
9774 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9775
9776 Set_Ekind (D_Minal, E_In_Parameter);
9777 Set_Mechanism (D_Minal, Default_Mechanism);
9778 Set_Etype (D_Minal, Etype (Discrim));
9779 Set_Scope (D_Minal, Current_Scope);
9780 Set_Parent (D_Minal, Parent (Discrim));
9781
9782 Set_Discriminal (Discrim, D_Minal);
9783 Set_Discriminal_Link (D_Minal, Discrim);
9784
9785 -- For task types, build at once the discriminants of the corresponding
9786 -- record, which are needed if discriminants are used in entry defaults
9787 -- and in family bounds.
9788
9789 if Is_Concurrent_Type (Current_Scope)
9790 or else
9791 Is_Limited_Type (Current_Scope)
9792 then
9793 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9794
9795 Set_Ekind (CR_Disc, E_In_Parameter);
9796 Set_Mechanism (CR_Disc, Default_Mechanism);
9797 Set_Etype (CR_Disc, Etype (Discrim));
9798 Set_Scope (CR_Disc, Current_Scope);
9799 Set_Discriminal_Link (CR_Disc, Discrim);
9800 Set_CR_Discriminant (Discrim, CR_Disc);
9801 end if;
9802 end Build_Discriminal;
9803
9804 ------------------------------------
9805 -- Build_Discriminant_Constraints --
9806 ------------------------------------
9807
9808 function Build_Discriminant_Constraints
9809 (T : Entity_Id;
9810 Def : Node_Id;
9811 Derived_Def : Boolean := False) return Elist_Id
9812 is
9813 C : constant Node_Id := Constraint (Def);
9814 Nb_Discr : constant Nat := Number_Discriminants (T);
9815
9816 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9817 -- Saves the expression corresponding to a given discriminant in T
9818
9819 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9820 -- Return the Position number within array Discr_Expr of a discriminant
9821 -- D within the discriminant list of the discriminated type T.
9822
9823 procedure Process_Discriminant_Expression
9824 (Expr : Node_Id;
9825 D : Entity_Id);
9826 -- If this is a discriminant constraint on a partial view, do not
9827 -- generate an overflow check on the discriminant expression. The check
9828 -- will be generated when constraining the full view. Otherwise the
9829 -- backend creates duplicate symbols for the temporaries corresponding
9830 -- to the expressions to be checked, causing spurious assembler errors.
9831
9832 ------------------
9833 -- Pos_Of_Discr --
9834 ------------------
9835
9836 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9837 Disc : Entity_Id;
9838
9839 begin
9840 Disc := First_Discriminant (T);
9841 for J in Discr_Expr'Range loop
9842 if Disc = D then
9843 return J;
9844 end if;
9845
9846 Next_Discriminant (Disc);
9847 end loop;
9848
9849 -- Note: Since this function is called on discriminants that are
9850 -- known to belong to the discriminated type, falling through the
9851 -- loop with no match signals an internal compiler error.
9852
9853 raise Program_Error;
9854 end Pos_Of_Discr;
9855
9856 -------------------------------------
9857 -- Process_Discriminant_Expression --
9858 -------------------------------------
9859
9860 procedure Process_Discriminant_Expression
9861 (Expr : Node_Id;
9862 D : Entity_Id)
9863 is
9864 BDT : constant Entity_Id := Base_Type (Etype (D));
9865
9866 begin
9867 -- If this is a discriminant constraint on a partial view, do
9868 -- not generate an overflow on the discriminant expression. The
9869 -- check will be generated when constraining the full view.
9870
9871 if Is_Private_Type (T)
9872 and then Present (Full_View (T))
9873 then
9874 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9875 else
9876 Analyze_And_Resolve (Expr, BDT);
9877 end if;
9878 end Process_Discriminant_Expression;
9879
9880 -- Declarations local to Build_Discriminant_Constraints
9881
9882 Discr : Entity_Id;
9883 E : Entity_Id;
9884 Elist : constant Elist_Id := New_Elmt_List;
9885
9886 Constr : Node_Id;
9887 Expr : Node_Id;
9888 Id : Node_Id;
9889 Position : Nat;
9890 Found : Boolean;
9891
9892 Discrim_Present : Boolean := False;
9893
9894 -- Start of processing for Build_Discriminant_Constraints
9895
9896 begin
9897 -- The following loop will process positional associations only.
9898 -- For a positional association, the (single) discriminant is
9899 -- implicitly specified by position, in textual order (RM 3.7.2).
9900
9901 Discr := First_Discriminant (T);
9902 Constr := First (Constraints (C));
9903 for D in Discr_Expr'Range loop
9904 exit when Nkind (Constr) = N_Discriminant_Association;
9905
9906 if No (Constr) then
9907 Error_Msg_N ("too few discriminants given in constraint", C);
9908 return New_Elmt_List;
9909
9910 elsif Nkind (Constr) = N_Range
9911 or else (Nkind (Constr) = N_Attribute_Reference
9912 and then Attribute_Name (Constr) = Name_Range)
9913 then
9914 Error_Msg_N
9915 ("a range is not a valid discriminant constraint", Constr);
9916 Discr_Expr (D) := Error;
9917
9918 elsif Nkind (Constr) = N_Subtype_Indication then
9919 Error_Msg_N
9920 ("a subtype indication is not a valid discriminant constraint",
9921 Constr);
9922 Discr_Expr (D) := Error;
9923
9924 else
9925 Process_Discriminant_Expression (Constr, Discr);
9926 Discr_Expr (D) := Constr;
9927 end if;
9928
9929 Next_Discriminant (Discr);
9930 Next (Constr);
9931 end loop;
9932
9933 if No (Discr) and then Present (Constr) then
9934 Error_Msg_N ("too many discriminants given in constraint", Constr);
9935 return New_Elmt_List;
9936 end if;
9937
9938 -- Named associations can be given in any order, but if both positional
9939 -- and named associations are used in the same discriminant constraint,
9940 -- then positional associations must occur first, at their normal
9941 -- position. Hence once a named association is used, the rest of the
9942 -- discriminant constraint must use only named associations.
9943
9944 while Present (Constr) loop
9945
9946 -- Positional association forbidden after a named association
9947
9948 if Nkind (Constr) /= N_Discriminant_Association then
9949 Error_Msg_N ("positional association follows named one", Constr);
9950 return New_Elmt_List;
9951
9952 -- Otherwise it is a named association
9953
9954 else
9955 -- E records the type of the discriminants in the named
9956 -- association. All the discriminants specified in the same name
9957 -- association must have the same type.
9958
9959 E := Empty;
9960
9961 -- Search the list of discriminants in T to see if the simple name
9962 -- given in the constraint matches any of them.
9963
9964 Id := First (Selector_Names (Constr));
9965 while Present (Id) loop
9966 Found := False;
9967
9968 -- If Original_Discriminant is present, we are processing a
9969 -- generic instantiation and this is an instance node. We need
9970 -- to find the name of the corresponding discriminant in the
9971 -- actual record type T and not the name of the discriminant in
9972 -- the generic formal. Example:
9973
9974 -- generic
9975 -- type G (D : int) is private;
9976 -- package P is
9977 -- subtype W is G (D => 1);
9978 -- end package;
9979 -- type Rec (X : int) is record ... end record;
9980 -- package Q is new P (G => Rec);
9981
9982 -- At the point of the instantiation, formal type G is Rec
9983 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9984 -- which really looks like "subtype W is Rec (D => 1);" at
9985 -- the point of instantiation, we want to find the discriminant
9986 -- that corresponds to D in Rec, i.e. X.
9987
9988 if Present (Original_Discriminant (Id))
9989 and then In_Instance
9990 then
9991 Discr := Find_Corresponding_Discriminant (Id, T);
9992 Found := True;
9993
9994 else
9995 Discr := First_Discriminant (T);
9996 while Present (Discr) loop
9997 if Chars (Discr) = Chars (Id) then
9998 Found := True;
9999 exit;
10000 end if;
10001
10002 Next_Discriminant (Discr);
10003 end loop;
10004
10005 if not Found then
10006 Error_Msg_N ("& does not match any discriminant", Id);
10007 return New_Elmt_List;
10008
10009 -- If the parent type is a generic formal, preserve the
10010 -- name of the discriminant for subsequent instances.
10011 -- see comment at the beginning of this if statement.
10012
10013 elsif Is_Generic_Type (Root_Type (T)) then
10014 Set_Original_Discriminant (Id, Discr);
10015 end if;
10016 end if;
10017
10018 Position := Pos_Of_Discr (T, Discr);
10019
10020 if Present (Discr_Expr (Position)) then
10021 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10022
10023 else
10024 -- Each discriminant specified in the same named association
10025 -- must be associated with a separate copy of the
10026 -- corresponding expression.
10027
10028 if Present (Next (Id)) then
10029 Expr := New_Copy_Tree (Expression (Constr));
10030 Set_Parent (Expr, Parent (Expression (Constr)));
10031 else
10032 Expr := Expression (Constr);
10033 end if;
10034
10035 Discr_Expr (Position) := Expr;
10036 Process_Discriminant_Expression (Expr, Discr);
10037 end if;
10038
10039 -- A discriminant association with more than one discriminant
10040 -- name is only allowed if the named discriminants are all of
10041 -- the same type (RM 3.7.1(8)).
10042
10043 if E = Empty then
10044 E := Base_Type (Etype (Discr));
10045
10046 elsif Base_Type (Etype (Discr)) /= E then
10047 Error_Msg_N
10048 ("all discriminants in an association " &
10049 "must have the same type", Id);
10050 end if;
10051
10052 Next (Id);
10053 end loop;
10054 end if;
10055
10056 Next (Constr);
10057 end loop;
10058
10059 -- A discriminant constraint must provide exactly one value for each
10060 -- discriminant of the type (RM 3.7.1(8)).
10061
10062 for J in Discr_Expr'Range loop
10063 if No (Discr_Expr (J)) then
10064 Error_Msg_N ("too few discriminants given in constraint", C);
10065 return New_Elmt_List;
10066 end if;
10067 end loop;
10068
10069 -- Determine if there are discriminant expressions in the constraint
10070
10071 for J in Discr_Expr'Range loop
10072 if Denotes_Discriminant
10073 (Discr_Expr (J), Check_Concurrent => True)
10074 then
10075 Discrim_Present := True;
10076 end if;
10077 end loop;
10078
10079 -- Build an element list consisting of the expressions given in the
10080 -- discriminant constraint and apply the appropriate checks. The list
10081 -- is constructed after resolving any named discriminant associations
10082 -- and therefore the expressions appear in the textual order of the
10083 -- discriminants.
10084
10085 Discr := First_Discriminant (T);
10086 for J in Discr_Expr'Range loop
10087 if Discr_Expr (J) /= Error then
10088 Append_Elmt (Discr_Expr (J), Elist);
10089
10090 -- If any of the discriminant constraints is given by a
10091 -- discriminant and we are in a derived type declaration we
10092 -- have a discriminant renaming. Establish link between new
10093 -- and old discriminant. The new discriminant has an implicit
10094 -- dereference if the old one does.
10095
10096 if Denotes_Discriminant (Discr_Expr (J)) then
10097 if Derived_Def then
10098 declare
10099 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10100
10101 begin
10102 Set_Corresponding_Discriminant (New_Discr, Discr);
10103 Set_Has_Implicit_Dereference (New_Discr,
10104 Has_Implicit_Dereference (Discr));
10105 end;
10106 end if;
10107
10108 -- Force the evaluation of non-discriminant expressions.
10109 -- If we have found a discriminant in the constraint 3.4(26)
10110 -- and 3.8(18) demand that no range checks are performed are
10111 -- after evaluation. If the constraint is for a component
10112 -- definition that has a per-object constraint, expressions are
10113 -- evaluated but not checked either. In all other cases perform
10114 -- a range check.
10115
10116 else
10117 if Discrim_Present then
10118 null;
10119
10120 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10121 and then Has_Per_Object_Constraint
10122 (Defining_Identifier (Parent (Parent (Def))))
10123 then
10124 null;
10125
10126 elsif Is_Access_Type (Etype (Discr)) then
10127 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10128
10129 else
10130 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10131 end if;
10132
10133 Force_Evaluation (Discr_Expr (J));
10134 end if;
10135
10136 -- Check that the designated type of an access discriminant's
10137 -- expression is not a class-wide type unless the discriminant's
10138 -- designated type is also class-wide.
10139
10140 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10141 and then not Is_Class_Wide_Type
10142 (Designated_Type (Etype (Discr)))
10143 and then Etype (Discr_Expr (J)) /= Any_Type
10144 and then Is_Class_Wide_Type
10145 (Designated_Type (Etype (Discr_Expr (J))))
10146 then
10147 Wrong_Type (Discr_Expr (J), Etype (Discr));
10148
10149 elsif Is_Access_Type (Etype (Discr))
10150 and then not Is_Access_Constant (Etype (Discr))
10151 and then Is_Access_Type (Etype (Discr_Expr (J)))
10152 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10153 then
10154 Error_Msg_NE
10155 ("constraint for discriminant& must be access to variable",
10156 Def, Discr);
10157 end if;
10158 end if;
10159
10160 Next_Discriminant (Discr);
10161 end loop;
10162
10163 return Elist;
10164 end Build_Discriminant_Constraints;
10165
10166 ---------------------------------
10167 -- Build_Discriminated_Subtype --
10168 ---------------------------------
10169
10170 procedure Build_Discriminated_Subtype
10171 (T : Entity_Id;
10172 Def_Id : Entity_Id;
10173 Elist : Elist_Id;
10174 Related_Nod : Node_Id;
10175 For_Access : Boolean := False)
10176 is
10177 Has_Discrs : constant Boolean := Has_Discriminants (T);
10178 Constrained : constant Boolean :=
10179 (Has_Discrs
10180 and then not Is_Empty_Elmt_List (Elist)
10181 and then not Is_Class_Wide_Type (T))
10182 or else Is_Constrained (T);
10183
10184 begin
10185 if Ekind (T) = E_Record_Type then
10186 if For_Access then
10187 Set_Ekind (Def_Id, E_Private_Subtype);
10188 Set_Is_For_Access_Subtype (Def_Id, True);
10189 else
10190 Set_Ekind (Def_Id, E_Record_Subtype);
10191 end if;
10192
10193 -- Inherit preelaboration flag from base, for types for which it
10194 -- may have been set: records, private types, protected types.
10195
10196 Set_Known_To_Have_Preelab_Init
10197 (Def_Id, Known_To_Have_Preelab_Init (T));
10198
10199 elsif Ekind (T) = E_Task_Type then
10200 Set_Ekind (Def_Id, E_Task_Subtype);
10201
10202 elsif Ekind (T) = E_Protected_Type then
10203 Set_Ekind (Def_Id, E_Protected_Subtype);
10204 Set_Known_To_Have_Preelab_Init
10205 (Def_Id, Known_To_Have_Preelab_Init (T));
10206
10207 elsif Is_Private_Type (T) then
10208 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10209 Set_Known_To_Have_Preelab_Init
10210 (Def_Id, Known_To_Have_Preelab_Init (T));
10211
10212 -- Private subtypes may have private dependents
10213
10214 Set_Private_Dependents (Def_Id, New_Elmt_List);
10215
10216 elsif Is_Class_Wide_Type (T) then
10217 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10218
10219 else
10220 -- Incomplete type. Attach subtype to list of dependents, to be
10221 -- completed with full view of parent type, unless is it the
10222 -- designated subtype of a record component within an init_proc.
10223 -- This last case arises for a component of an access type whose
10224 -- designated type is incomplete (e.g. a Taft Amendment type).
10225 -- The designated subtype is within an inner scope, and needs no
10226 -- elaboration, because only the access type is needed in the
10227 -- initialization procedure.
10228
10229 if Ekind (T) = E_Incomplete_Type then
10230 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10231 else
10232 Set_Ekind (Def_Id, Ekind (T));
10233 end if;
10234
10235 if For_Access and then Within_Init_Proc then
10236 null;
10237 else
10238 Append_Elmt (Def_Id, Private_Dependents (T));
10239 end if;
10240 end if;
10241
10242 Set_Etype (Def_Id, T);
10243 Init_Size_Align (Def_Id);
10244 Set_Has_Discriminants (Def_Id, Has_Discrs);
10245 Set_Is_Constrained (Def_Id, Constrained);
10246
10247 Set_First_Entity (Def_Id, First_Entity (T));
10248 Set_Last_Entity (Def_Id, Last_Entity (T));
10249 Set_Has_Implicit_Dereference
10250 (Def_Id, Has_Implicit_Dereference (T));
10251 Set_Has_Pragma_Unreferenced_Objects
10252 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10253
10254 -- If the subtype is the completion of a private declaration, there may
10255 -- have been representation clauses for the partial view, and they must
10256 -- be preserved. Build_Derived_Type chains the inherited clauses with
10257 -- the ones appearing on the extension. If this comes from a subtype
10258 -- declaration, all clauses are inherited.
10259
10260 if No (First_Rep_Item (Def_Id)) then
10261 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10262 end if;
10263
10264 if Is_Tagged_Type (T) then
10265 Set_Is_Tagged_Type (Def_Id);
10266 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10267 Make_Class_Wide_Type (Def_Id);
10268 end if;
10269
10270 Set_Stored_Constraint (Def_Id, No_Elist);
10271
10272 if Has_Discrs then
10273 Set_Discriminant_Constraint (Def_Id, Elist);
10274 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10275 end if;
10276
10277 if Is_Tagged_Type (T) then
10278
10279 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10280 -- concurrent record type (which has the list of primitive
10281 -- operations).
10282
10283 if Ada_Version >= Ada_2005
10284 and then Is_Concurrent_Type (T)
10285 then
10286 Set_Corresponding_Record_Type (Def_Id,
10287 Corresponding_Record_Type (T));
10288 else
10289 Set_Direct_Primitive_Operations (Def_Id,
10290 Direct_Primitive_Operations (T));
10291 end if;
10292
10293 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10294 end if;
10295
10296 -- Subtypes introduced by component declarations do not need to be
10297 -- marked as delayed, and do not get freeze nodes, because the semantics
10298 -- verifies that the parents of the subtypes are frozen before the
10299 -- enclosing record is frozen.
10300
10301 if not Is_Type (Scope (Def_Id)) then
10302 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10303
10304 if Is_Private_Type (T)
10305 and then Present (Full_View (T))
10306 then
10307 Conditional_Delay (Def_Id, Full_View (T));
10308 else
10309 Conditional_Delay (Def_Id, T);
10310 end if;
10311 end if;
10312
10313 if Is_Record_Type (T) then
10314 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10315
10316 if Has_Discrs
10317 and then not Is_Empty_Elmt_List (Elist)
10318 and then not For_Access
10319 then
10320 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10321
10322 elsif not For_Access then
10323 Set_Cloned_Subtype (Def_Id, T);
10324 end if;
10325 end if;
10326 end Build_Discriminated_Subtype;
10327
10328 ---------------------------
10329 -- Build_Itype_Reference --
10330 ---------------------------
10331
10332 procedure Build_Itype_Reference
10333 (Ityp : Entity_Id;
10334 Nod : Node_Id)
10335 is
10336 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10337 begin
10338
10339 -- Itype references are only created for use by the back-end
10340
10341 if Inside_A_Generic then
10342 return;
10343 else
10344 Set_Itype (IR, Ityp);
10345
10346 -- If Nod is a library unit entity, then Insert_After won't work,
10347 -- because Nod is not a member of any list. Therefore, we use
10348 -- Add_Global_Declaration in this case. This can happen if we have a
10349 -- build-in-place library function.
10350
10351 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10352 or else
10353 (Nkind (Nod) = N_Defining_Program_Unit_Name
10354 and then Is_Compilation_Unit (Defining_Identifier (Nod)))
10355 then
10356 Add_Global_Declaration (IR);
10357 else
10358 Insert_After (Nod, IR);
10359 end if;
10360 end if;
10361 end Build_Itype_Reference;
10362
10363 ------------------------
10364 -- Build_Scalar_Bound --
10365 ------------------------
10366
10367 function Build_Scalar_Bound
10368 (Bound : Node_Id;
10369 Par_T : Entity_Id;
10370 Der_T : Entity_Id) return Node_Id
10371 is
10372 New_Bound : Entity_Id;
10373
10374 begin
10375 -- Note: not clear why this is needed, how can the original bound
10376 -- be unanalyzed at this point? and if it is, what business do we
10377 -- have messing around with it? and why is the base type of the
10378 -- parent type the right type for the resolution. It probably is
10379 -- not. It is OK for the new bound we are creating, but not for
10380 -- the old one??? Still if it never happens, no problem.
10381
10382 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10383
10384 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10385 New_Bound := New_Copy (Bound);
10386 Set_Etype (New_Bound, Der_T);
10387 Set_Analyzed (New_Bound);
10388
10389 elsif Is_Entity_Name (Bound) then
10390 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10391
10392 -- The following is almost certainly wrong. What business do we have
10393 -- relocating a node (Bound) that is presumably still attached to
10394 -- the tree elsewhere???
10395
10396 else
10397 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10398 end if;
10399
10400 Set_Etype (New_Bound, Der_T);
10401 return New_Bound;
10402 end Build_Scalar_Bound;
10403
10404 --------------------------------
10405 -- Build_Underlying_Full_View --
10406 --------------------------------
10407
10408 procedure Build_Underlying_Full_View
10409 (N : Node_Id;
10410 Typ : Entity_Id;
10411 Par : Entity_Id)
10412 is
10413 Loc : constant Source_Ptr := Sloc (N);
10414 Subt : constant Entity_Id :=
10415 Make_Defining_Identifier
10416 (Loc, New_External_Name (Chars (Typ), 'S'));
10417
10418 Constr : Node_Id;
10419 Indic : Node_Id;
10420 C : Node_Id;
10421 Id : Node_Id;
10422
10423 procedure Set_Discriminant_Name (Id : Node_Id);
10424 -- If the derived type has discriminants, they may rename discriminants
10425 -- of the parent. When building the full view of the parent, we need to
10426 -- recover the names of the original discriminants if the constraint is
10427 -- given by named associations.
10428
10429 ---------------------------
10430 -- Set_Discriminant_Name --
10431 ---------------------------
10432
10433 procedure Set_Discriminant_Name (Id : Node_Id) is
10434 Disc : Entity_Id;
10435
10436 begin
10437 Set_Original_Discriminant (Id, Empty);
10438
10439 if Has_Discriminants (Typ) then
10440 Disc := First_Discriminant (Typ);
10441 while Present (Disc) loop
10442 if Chars (Disc) = Chars (Id)
10443 and then Present (Corresponding_Discriminant (Disc))
10444 then
10445 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10446 end if;
10447 Next_Discriminant (Disc);
10448 end loop;
10449 end if;
10450 end Set_Discriminant_Name;
10451
10452 -- Start of processing for Build_Underlying_Full_View
10453
10454 begin
10455 if Nkind (N) = N_Full_Type_Declaration then
10456 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10457
10458 elsif Nkind (N) = N_Subtype_Declaration then
10459 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10460
10461 elsif Nkind (N) = N_Component_Declaration then
10462 Constr :=
10463 New_Copy_Tree
10464 (Constraint (Subtype_Indication (Component_Definition (N))));
10465
10466 else
10467 raise Program_Error;
10468 end if;
10469
10470 C := First (Constraints (Constr));
10471 while Present (C) loop
10472 if Nkind (C) = N_Discriminant_Association then
10473 Id := First (Selector_Names (C));
10474 while Present (Id) loop
10475 Set_Discriminant_Name (Id);
10476 Next (Id);
10477 end loop;
10478 end if;
10479
10480 Next (C);
10481 end loop;
10482
10483 Indic :=
10484 Make_Subtype_Declaration (Loc,
10485 Defining_Identifier => Subt,
10486 Subtype_Indication =>
10487 Make_Subtype_Indication (Loc,
10488 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10489 Constraint => New_Copy_Tree (Constr)));
10490
10491 -- If this is a component subtype for an outer itype, it is not
10492 -- a list member, so simply set the parent link for analysis: if
10493 -- the enclosing type does not need to be in a declarative list,
10494 -- neither do the components.
10495
10496 if Is_List_Member (N)
10497 and then Nkind (N) /= N_Component_Declaration
10498 then
10499 Insert_Before (N, Indic);
10500 else
10501 Set_Parent (Indic, Parent (N));
10502 end if;
10503
10504 Analyze (Indic);
10505 Set_Underlying_Full_View (Typ, Full_View (Subt));
10506 Set_Is_Underlying_Full_View (Full_View (Subt));
10507 end Build_Underlying_Full_View;
10508
10509 -------------------------------
10510 -- Check_Abstract_Overriding --
10511 -------------------------------
10512
10513 procedure Check_Abstract_Overriding (T : Entity_Id) is
10514 Alias_Subp : Entity_Id;
10515 Elmt : Elmt_Id;
10516 Op_List : Elist_Id;
10517 Subp : Entity_Id;
10518 Type_Def : Node_Id;
10519
10520 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10521 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10522 -- which has pragma Implemented already set. Check whether Subp's entity
10523 -- kind conforms to the implementation kind of the overridden routine.
10524
10525 procedure Check_Pragma_Implemented
10526 (Subp : Entity_Id;
10527 Iface_Subp : Entity_Id);
10528 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10529 -- Iface_Subp and both entities have pragma Implemented already set on
10530 -- them. Check whether the two implementation kinds are conforming.
10531
10532 procedure Inherit_Pragma_Implemented
10533 (Subp : Entity_Id;
10534 Iface_Subp : Entity_Id);
10535 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10536 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10537 -- Propagate the implementation kind of Iface_Subp to Subp.
10538
10539 ------------------------------
10540 -- Check_Pragma_Implemented --
10541 ------------------------------
10542
10543 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10544 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10545 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10546 Subp_Alias : constant Entity_Id := Alias (Subp);
10547 Contr_Typ : Entity_Id;
10548 Impl_Subp : Entity_Id;
10549
10550 begin
10551 -- Subp must have an alias since it is a hidden entity used to link
10552 -- an interface subprogram to its overriding counterpart.
10553
10554 pragma Assert (Present (Subp_Alias));
10555
10556 -- Handle aliases to synchronized wrappers
10557
10558 Impl_Subp := Subp_Alias;
10559
10560 if Is_Primitive_Wrapper (Impl_Subp) then
10561 Impl_Subp := Wrapped_Entity (Impl_Subp);
10562 end if;
10563
10564 -- Extract the type of the controlling formal
10565
10566 Contr_Typ := Etype (First_Formal (Subp_Alias));
10567
10568 if Is_Concurrent_Record_Type (Contr_Typ) then
10569 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10570 end if;
10571
10572 -- An interface subprogram whose implementation kind is By_Entry must
10573 -- be implemented by an entry.
10574
10575 if Impl_Kind = Name_By_Entry
10576 and then Ekind (Impl_Subp) /= E_Entry
10577 then
10578 Error_Msg_Node_2 := Iface_Alias;
10579 Error_Msg_NE
10580 ("type & must implement abstract subprogram & with an entry",
10581 Subp_Alias, Contr_Typ);
10582
10583 elsif Impl_Kind = Name_By_Protected_Procedure then
10584
10585 -- An interface subprogram whose implementation kind is By_
10586 -- Protected_Procedure cannot be implemented by a primitive
10587 -- procedure of a task type.
10588
10589 if Ekind (Contr_Typ) /= E_Protected_Type then
10590 Error_Msg_Node_2 := Contr_Typ;
10591 Error_Msg_NE
10592 ("interface subprogram & cannot be implemented by a " &
10593 "primitive procedure of task type &", Subp_Alias,
10594 Iface_Alias);
10595
10596 -- An interface subprogram whose implementation kind is By_
10597 -- Protected_Procedure must be implemented by a procedure.
10598
10599 elsif Ekind (Impl_Subp) /= E_Procedure then
10600 Error_Msg_Node_2 := Iface_Alias;
10601 Error_Msg_NE
10602 ("type & must implement abstract subprogram & with a " &
10603 "procedure", Subp_Alias, Contr_Typ);
10604
10605 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10606 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10607 then
10608 Error_Msg_Name_1 := Impl_Kind;
10609 Error_Msg_N
10610 ("overriding operation& must have synchronization%",
10611 Subp_Alias);
10612 end if;
10613
10614 -- If primitive has Optional synchronization, overriding operation
10615 -- must match if it has an explicit synchronization..
10616
10617 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10618 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10619 then
10620 Error_Msg_Name_1 := Impl_Kind;
10621 Error_Msg_N
10622 ("overriding operation& must have syncrhonization%",
10623 Subp_Alias);
10624 end if;
10625 end Check_Pragma_Implemented;
10626
10627 ------------------------------
10628 -- Check_Pragma_Implemented --
10629 ------------------------------
10630
10631 procedure Check_Pragma_Implemented
10632 (Subp : Entity_Id;
10633 Iface_Subp : Entity_Id)
10634 is
10635 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10636 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10637
10638 begin
10639 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10640 -- and overriding subprogram are different. In general this is an
10641 -- error except when the implementation kind of the overridden
10642 -- subprograms is By_Any or Optional.
10643
10644 if Iface_Kind /= Subp_Kind
10645 and then Iface_Kind /= Name_By_Any
10646 and then Iface_Kind /= Name_Optional
10647 then
10648 if Iface_Kind = Name_By_Entry then
10649 Error_Msg_N
10650 ("incompatible implementation kind, overridden subprogram " &
10651 "is marked By_Entry", Subp);
10652 else
10653 Error_Msg_N
10654 ("incompatible implementation kind, overridden subprogram " &
10655 "is marked By_Protected_Procedure", Subp);
10656 end if;
10657 end if;
10658 end Check_Pragma_Implemented;
10659
10660 --------------------------------
10661 -- Inherit_Pragma_Implemented --
10662 --------------------------------
10663
10664 procedure Inherit_Pragma_Implemented
10665 (Subp : Entity_Id;
10666 Iface_Subp : Entity_Id)
10667 is
10668 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10669 Loc : constant Source_Ptr := Sloc (Subp);
10670 Impl_Prag : Node_Id;
10671
10672 begin
10673 -- Since the implementation kind is stored as a representation item
10674 -- rather than a flag, create a pragma node.
10675
10676 Impl_Prag :=
10677 Make_Pragma (Loc,
10678 Chars => Name_Implemented,
10679 Pragma_Argument_Associations => New_List (
10680 Make_Pragma_Argument_Association (Loc,
10681 Expression => New_Occurrence_Of (Subp, Loc)),
10682
10683 Make_Pragma_Argument_Association (Loc,
10684 Expression => Make_Identifier (Loc, Iface_Kind))));
10685
10686 -- The pragma doesn't need to be analyzed because it is internally
10687 -- built. It is safe to directly register it as a rep item since we
10688 -- are only interested in the characters of the implementation kind.
10689
10690 Record_Rep_Item (Subp, Impl_Prag);
10691 end Inherit_Pragma_Implemented;
10692
10693 -- Start of processing for Check_Abstract_Overriding
10694
10695 begin
10696 Op_List := Primitive_Operations (T);
10697
10698 -- Loop to check primitive operations
10699
10700 Elmt := First_Elmt (Op_List);
10701 while Present (Elmt) loop
10702 Subp := Node (Elmt);
10703 Alias_Subp := Alias (Subp);
10704
10705 -- Inherited subprograms are identified by the fact that they do not
10706 -- come from source, and the associated source location is the
10707 -- location of the first subtype of the derived type.
10708
10709 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10710 -- subprograms that "require overriding".
10711
10712 -- Special exception, do not complain about failure to override the
10713 -- stream routines _Input and _Output, as well as the primitive
10714 -- operations used in dispatching selects since we always provide
10715 -- automatic overridings for these subprograms.
10716
10717 -- The partial view of T may have been a private extension, for
10718 -- which inherited functions dispatching on result are abstract.
10719 -- If the full view is a null extension, there is no need for
10720 -- overriding in Ada 2005, but wrappers need to be built for them
10721 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10722
10723 if Is_Null_Extension (T)
10724 and then Has_Controlling_Result (Subp)
10725 and then Ada_Version >= Ada_2005
10726 and then Present (Alias_Subp)
10727 and then not Comes_From_Source (Subp)
10728 and then not Is_Abstract_Subprogram (Alias_Subp)
10729 and then not Is_Access_Type (Etype (Subp))
10730 then
10731 null;
10732
10733 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10734 -- processing because this check is done with the aliased
10735 -- entity
10736
10737 elsif Present (Interface_Alias (Subp)) then
10738 null;
10739
10740 elsif (Is_Abstract_Subprogram (Subp)
10741 or else Requires_Overriding (Subp)
10742 or else
10743 (Has_Controlling_Result (Subp)
10744 and then Present (Alias_Subp)
10745 and then not Comes_From_Source (Subp)
10746 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10747 and then not Is_TSS (Subp, TSS_Stream_Input)
10748 and then not Is_TSS (Subp, TSS_Stream_Output)
10749 and then not Is_Abstract_Type (T)
10750 and then not Is_Predefined_Interface_Primitive (Subp)
10751
10752 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10753 -- with abstract interface types because the check will be done
10754 -- with the aliased entity (otherwise we generate a duplicated
10755 -- error message).
10756
10757 and then not Present (Interface_Alias (Subp))
10758 then
10759 if Present (Alias_Subp) then
10760
10761 -- Only perform the check for a derived subprogram when the
10762 -- type has an explicit record extension. This avoids incorrect
10763 -- flagging of abstract subprograms for the case of a type
10764 -- without an extension that is derived from a formal type
10765 -- with a tagged actual (can occur within a private part).
10766
10767 -- Ada 2005 (AI-391): In the case of an inherited function with
10768 -- a controlling result of the type, the rule does not apply if
10769 -- the type is a null extension (unless the parent function
10770 -- itself is abstract, in which case the function must still be
10771 -- be overridden). The expander will generate an overriding
10772 -- wrapper function calling the parent subprogram (see
10773 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10774
10775 Type_Def := Type_Definition (Parent (T));
10776
10777 if Nkind (Type_Def) = N_Derived_Type_Definition
10778 and then Present (Record_Extension_Part (Type_Def))
10779 and then
10780 (Ada_Version < Ada_2005
10781 or else not Is_Null_Extension (T)
10782 or else Ekind (Subp) = E_Procedure
10783 or else not Has_Controlling_Result (Subp)
10784 or else Is_Abstract_Subprogram (Alias_Subp)
10785 or else Requires_Overriding (Subp)
10786 or else Is_Access_Type (Etype (Subp)))
10787 then
10788 -- Avoid reporting error in case of abstract predefined
10789 -- primitive inherited from interface type because the
10790 -- body of internally generated predefined primitives
10791 -- of tagged types are generated later by Freeze_Type
10792
10793 if Is_Interface (Root_Type (T))
10794 and then Is_Abstract_Subprogram (Subp)
10795 and then Is_Predefined_Dispatching_Operation (Subp)
10796 and then not Comes_From_Source (Ultimate_Alias (Subp))
10797 then
10798 null;
10799
10800 -- A null extension is not obliged to override an inherited
10801 -- procedure subject to pragma Extensions_Visible with value
10802 -- False and at least one controlling OUT parameter
10803 -- (SPARK RM 6.1.7(6)).
10804
10805 elsif Is_Null_Extension (T)
10806 and then Is_EVF_Procedure (Subp)
10807 then
10808 null;
10809
10810 else
10811 Error_Msg_NE
10812 ("type must be declared abstract or & overridden",
10813 T, Subp);
10814
10815 -- Traverse the whole chain of aliased subprograms to
10816 -- complete the error notification. This is especially
10817 -- useful for traceability of the chain of entities when
10818 -- the subprogram corresponds with an interface
10819 -- subprogram (which may be defined in another package).
10820
10821 if Present (Alias_Subp) then
10822 declare
10823 E : Entity_Id;
10824
10825 begin
10826 E := Subp;
10827 while Present (Alias (E)) loop
10828
10829 -- Avoid reporting redundant errors on entities
10830 -- inherited from interfaces
10831
10832 if Sloc (E) /= Sloc (T) then
10833 Error_Msg_Sloc := Sloc (E);
10834 Error_Msg_NE
10835 ("\& has been inherited #", T, Subp);
10836 end if;
10837
10838 E := Alias (E);
10839 end loop;
10840
10841 Error_Msg_Sloc := Sloc (E);
10842
10843 -- AI05-0068: report if there is an overriding
10844 -- non-abstract subprogram that is invisible.
10845
10846 if Is_Hidden (E)
10847 and then not Is_Abstract_Subprogram (E)
10848 then
10849 Error_Msg_NE
10850 ("\& subprogram# is not visible",
10851 T, Subp);
10852
10853 -- Clarify the case where a non-null extension must
10854 -- override inherited procedure subject to pragma
10855 -- Extensions_Visible with value False and at least
10856 -- one controlling OUT param.
10857
10858 elsif Is_EVF_Procedure (E) then
10859 Error_Msg_NE
10860 ("\& # is subject to Extensions_Visible False",
10861 T, Subp);
10862
10863 else
10864 Error_Msg_NE
10865 ("\& has been inherited from subprogram #",
10866 T, Subp);
10867 end if;
10868 end;
10869 end if;
10870 end if;
10871
10872 -- Ada 2005 (AI-345): Protected or task type implementing
10873 -- abstract interfaces.
10874
10875 elsif Is_Concurrent_Record_Type (T)
10876 and then Present (Interfaces (T))
10877 then
10878 -- There is no need to check here RM 9.4(11.9/3) since we
10879 -- are processing the corresponding record type and the
10880 -- mode of the overriding subprograms was verified by
10881 -- Check_Conformance when the corresponding concurrent
10882 -- type declaration was analyzed.
10883
10884 Error_Msg_NE
10885 ("interface subprogram & must be overridden", T, Subp);
10886
10887 -- Examine primitive operations of synchronized type to find
10888 -- homonyms that have the wrong profile.
10889
10890 declare
10891 Prim : Entity_Id;
10892
10893 begin
10894 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10895 while Present (Prim) loop
10896 if Chars (Prim) = Chars (Subp) then
10897 Error_Msg_NE
10898 ("profile is not type conformant with prefixed "
10899 & "view profile of inherited operation&",
10900 Prim, Subp);
10901 end if;
10902
10903 Next_Entity (Prim);
10904 end loop;
10905 end;
10906 end if;
10907
10908 else
10909 Error_Msg_Node_2 := T;
10910 Error_Msg_N
10911 ("abstract subprogram& not allowed for type&", Subp);
10912
10913 -- Also post unconditional warning on the type (unconditional
10914 -- so that if there are more than one of these cases, we get
10915 -- them all, and not just the first one).
10916
10917 Error_Msg_Node_2 := Subp;
10918 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10919 end if;
10920
10921 -- A subprogram subject to pragma Extensions_Visible with value
10922 -- "True" cannot override a subprogram subject to the same pragma
10923 -- with value "False" (SPARK RM 6.1.7(5)).
10924
10925 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10926 and then Present (Overridden_Operation (Subp))
10927 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10928 Extensions_Visible_False
10929 then
10930 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10931 Error_Msg_N
10932 ("subprogram & with Extensions_Visible True cannot override "
10933 & "subprogram # with Extensions_Visible False", Subp);
10934 end if;
10935
10936 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10937
10938 -- Subp is an expander-generated procedure which maps an interface
10939 -- alias to a protected wrapper. The interface alias is flagged by
10940 -- pragma Implemented. Ensure that Subp is a procedure when the
10941 -- implementation kind is By_Protected_Procedure or an entry when
10942 -- By_Entry.
10943
10944 if Ada_Version >= Ada_2012
10945 and then Is_Hidden (Subp)
10946 and then Present (Interface_Alias (Subp))
10947 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10948 then
10949 Check_Pragma_Implemented (Subp);
10950 end if;
10951
10952 -- Subp is an interface primitive which overrides another interface
10953 -- primitive marked with pragma Implemented.
10954
10955 if Ada_Version >= Ada_2012
10956 and then Present (Overridden_Operation (Subp))
10957 and then Has_Rep_Pragma
10958 (Overridden_Operation (Subp), Name_Implemented)
10959 then
10960 -- If the overriding routine is also marked by Implemented, check
10961 -- that the two implementation kinds are conforming.
10962
10963 if Has_Rep_Pragma (Subp, Name_Implemented) then
10964 Check_Pragma_Implemented
10965 (Subp => Subp,
10966 Iface_Subp => Overridden_Operation (Subp));
10967
10968 -- Otherwise the overriding routine inherits the implementation
10969 -- kind from the overridden subprogram.
10970
10971 else
10972 Inherit_Pragma_Implemented
10973 (Subp => Subp,
10974 Iface_Subp => Overridden_Operation (Subp));
10975 end if;
10976 end if;
10977
10978 -- If the operation is a wrapper for a synchronized primitive, it
10979 -- may be called indirectly through a dispatching select. We assume
10980 -- that it will be referenced elsewhere indirectly, and suppress
10981 -- warnings about an unused entity.
10982
10983 if Is_Primitive_Wrapper (Subp)
10984 and then Present (Wrapped_Entity (Subp))
10985 then
10986 Set_Referenced (Wrapped_Entity (Subp));
10987 end if;
10988
10989 Next_Elmt (Elmt);
10990 end loop;
10991 end Check_Abstract_Overriding;
10992
10993 ------------------------------------------------
10994 -- Check_Access_Discriminant_Requires_Limited --
10995 ------------------------------------------------
10996
10997 procedure Check_Access_Discriminant_Requires_Limited
10998 (D : Node_Id;
10999 Loc : Node_Id)
11000 is
11001 begin
11002 -- A discriminant_specification for an access discriminant shall appear
11003 -- only in the declaration for a task or protected type, or for a type
11004 -- with the reserved word 'limited' in its definition or in one of its
11005 -- ancestors (RM 3.7(10)).
11006
11007 -- AI-0063: The proper condition is that type must be immutably limited,
11008 -- or else be a partial view.
11009
11010 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11011 if Is_Limited_View (Current_Scope)
11012 or else
11013 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11014 and then Limited_Present (Parent (Current_Scope)))
11015 then
11016 null;
11017
11018 else
11019 Error_Msg_N
11020 ("access discriminants allowed only for limited types", Loc);
11021 end if;
11022 end if;
11023 end Check_Access_Discriminant_Requires_Limited;
11024
11025 -----------------------------------
11026 -- Check_Aliased_Component_Types --
11027 -----------------------------------
11028
11029 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11030 C : Entity_Id;
11031
11032 begin
11033 -- ??? Also need to check components of record extensions, but not
11034 -- components of protected types (which are always limited).
11035
11036 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11037 -- types to be unconstrained. This is safe because it is illegal to
11038 -- create access subtypes to such types with explicit discriminant
11039 -- constraints.
11040
11041 if not Is_Limited_Type (T) then
11042 if Ekind (T) = E_Record_Type then
11043 C := First_Component (T);
11044 while Present (C) loop
11045 if Is_Aliased (C)
11046 and then Has_Discriminants (Etype (C))
11047 and then not Is_Constrained (Etype (C))
11048 and then not In_Instance_Body
11049 and then Ada_Version < Ada_2005
11050 then
11051 Error_Msg_N
11052 ("aliased component must be constrained (RM 3.6(11))",
11053 C);
11054 end if;
11055
11056 Next_Component (C);
11057 end loop;
11058
11059 elsif Ekind (T) = E_Array_Type then
11060 if Has_Aliased_Components (T)
11061 and then Has_Discriminants (Component_Type (T))
11062 and then not Is_Constrained (Component_Type (T))
11063 and then not In_Instance_Body
11064 and then Ada_Version < Ada_2005
11065 then
11066 Error_Msg_N
11067 ("aliased component type must be constrained (RM 3.6(11))",
11068 T);
11069 end if;
11070 end if;
11071 end if;
11072 end Check_Aliased_Component_Types;
11073
11074 ---------------------------------------
11075 -- Check_Anonymous_Access_Components --
11076 ---------------------------------------
11077
11078 procedure Check_Anonymous_Access_Components
11079 (Typ_Decl : Node_Id;
11080 Typ : Entity_Id;
11081 Prev : Entity_Id;
11082 Comp_List : Node_Id)
11083 is
11084 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11085 Anon_Access : Entity_Id;
11086 Acc_Def : Node_Id;
11087 Comp : Node_Id;
11088 Comp_Def : Node_Id;
11089 Decl : Node_Id;
11090 Type_Def : Node_Id;
11091
11092 procedure Build_Incomplete_Type_Declaration;
11093 -- If the record type contains components that include an access to the
11094 -- current record, then create an incomplete type declaration for the
11095 -- record, to be used as the designated type of the anonymous access.
11096 -- This is done only once, and only if there is no previous partial
11097 -- view of the type.
11098
11099 function Designates_T (Subt : Node_Id) return Boolean;
11100 -- Check whether a node designates the enclosing record type, or 'Class
11101 -- of that type
11102
11103 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11104 -- Check whether an access definition includes a reference to
11105 -- the enclosing record type. The reference can be a subtype mark
11106 -- in the access definition itself, a 'Class attribute reference, or
11107 -- recursively a reference appearing in a parameter specification
11108 -- or result definition of an access_to_subprogram definition.
11109
11110 --------------------------------------
11111 -- Build_Incomplete_Type_Declaration --
11112 --------------------------------------
11113
11114 procedure Build_Incomplete_Type_Declaration is
11115 Decl : Node_Id;
11116 Inc_T : Entity_Id;
11117 H : Entity_Id;
11118
11119 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11120 -- it's "is new ... with record" or else "is tagged record ...".
11121
11122 Is_Tagged : constant Boolean :=
11123 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11124 and then
11125 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11126 or else
11127 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11128 and then Tagged_Present (Type_Definition (Typ_Decl)));
11129
11130 begin
11131 -- If there is a previous partial view, no need to create a new one
11132 -- If the partial view, given by Prev, is incomplete, If Prev is
11133 -- a private declaration, full declaration is flagged accordingly.
11134
11135 if Prev /= Typ then
11136 if Is_Tagged then
11137 Make_Class_Wide_Type (Prev);
11138 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11139 Set_Etype (Class_Wide_Type (Typ), Typ);
11140 end if;
11141
11142 return;
11143
11144 elsif Has_Private_Declaration (Typ) then
11145
11146 -- If we refer to T'Class inside T, and T is the completion of a
11147 -- private type, then make sure the class-wide type exists.
11148
11149 if Is_Tagged then
11150 Make_Class_Wide_Type (Typ);
11151 end if;
11152
11153 return;
11154
11155 -- If there was a previous anonymous access type, the incomplete
11156 -- type declaration will have been created already.
11157
11158 elsif Present (Current_Entity (Typ))
11159 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11160 and then Full_View (Current_Entity (Typ)) = Typ
11161 then
11162 if Is_Tagged
11163 and then Comes_From_Source (Current_Entity (Typ))
11164 and then not Is_Tagged_Type (Current_Entity (Typ))
11165 then
11166 Make_Class_Wide_Type (Typ);
11167 Error_Msg_N
11168 ("incomplete view of tagged type should be declared tagged??",
11169 Parent (Current_Entity (Typ)));
11170 end if;
11171 return;
11172
11173 else
11174 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11175 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11176
11177 -- Type has already been inserted into the current scope. Remove
11178 -- it, and add incomplete declaration for type, so that subsequent
11179 -- anonymous access types can use it. The entity is unchained from
11180 -- the homonym list and from immediate visibility. After analysis,
11181 -- the entity in the incomplete declaration becomes immediately
11182 -- visible in the record declaration that follows.
11183
11184 H := Current_Entity (Typ);
11185
11186 if H = Typ then
11187 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11188 else
11189 while Present (H)
11190 and then Homonym (H) /= Typ
11191 loop
11192 H := Homonym (Typ);
11193 end loop;
11194
11195 Set_Homonym (H, Homonym (Typ));
11196 end if;
11197
11198 Insert_Before (Typ_Decl, Decl);
11199 Analyze (Decl);
11200 Set_Full_View (Inc_T, Typ);
11201
11202 if Is_Tagged then
11203
11204 -- Create a common class-wide type for both views, and set the
11205 -- Etype of the class-wide type to the full view.
11206
11207 Make_Class_Wide_Type (Inc_T);
11208 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11209 Set_Etype (Class_Wide_Type (Typ), Typ);
11210 end if;
11211 end if;
11212 end Build_Incomplete_Type_Declaration;
11213
11214 ------------------
11215 -- Designates_T --
11216 ------------------
11217
11218 function Designates_T (Subt : Node_Id) return Boolean is
11219 Type_Id : constant Name_Id := Chars (Typ);
11220
11221 function Names_T (Nam : Node_Id) return Boolean;
11222 -- The record type has not been introduced in the current scope
11223 -- yet, so we must examine the name of the type itself, either
11224 -- an identifier T, or an expanded name of the form P.T, where
11225 -- P denotes the current scope.
11226
11227 -------------
11228 -- Names_T --
11229 -------------
11230
11231 function Names_T (Nam : Node_Id) return Boolean is
11232 begin
11233 if Nkind (Nam) = N_Identifier then
11234 return Chars (Nam) = Type_Id;
11235
11236 elsif Nkind (Nam) = N_Selected_Component then
11237 if Chars (Selector_Name (Nam)) = Type_Id then
11238 if Nkind (Prefix (Nam)) = N_Identifier then
11239 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11240
11241 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11242 return Chars (Selector_Name (Prefix (Nam))) =
11243 Chars (Current_Scope);
11244 else
11245 return False;
11246 end if;
11247
11248 else
11249 return False;
11250 end if;
11251
11252 else
11253 return False;
11254 end if;
11255 end Names_T;
11256
11257 -- Start of processing for Designates_T
11258
11259 begin
11260 if Nkind (Subt) = N_Identifier then
11261 return Chars (Subt) = Type_Id;
11262
11263 -- Reference can be through an expanded name which has not been
11264 -- analyzed yet, and which designates enclosing scopes.
11265
11266 elsif Nkind (Subt) = N_Selected_Component then
11267 if Names_T (Subt) then
11268 return True;
11269
11270 -- Otherwise it must denote an entity that is already visible.
11271 -- The access definition may name a subtype of the enclosing
11272 -- type, if there is a previous incomplete declaration for it.
11273
11274 else
11275 Find_Selected_Component (Subt);
11276 return
11277 Is_Entity_Name (Subt)
11278 and then Scope (Entity (Subt)) = Current_Scope
11279 and then
11280 (Chars (Base_Type (Entity (Subt))) = Type_Id
11281 or else
11282 (Is_Class_Wide_Type (Entity (Subt))
11283 and then
11284 Chars (Etype (Base_Type (Entity (Subt)))) =
11285 Type_Id));
11286 end if;
11287
11288 -- A reference to the current type may appear as the prefix of
11289 -- a 'Class attribute.
11290
11291 elsif Nkind (Subt) = N_Attribute_Reference
11292 and then Attribute_Name (Subt) = Name_Class
11293 then
11294 return Names_T (Prefix (Subt));
11295
11296 else
11297 return False;
11298 end if;
11299 end Designates_T;
11300
11301 ----------------
11302 -- Mentions_T --
11303 ----------------
11304
11305 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11306 Param_Spec : Node_Id;
11307
11308 Acc_Subprg : constant Node_Id :=
11309 Access_To_Subprogram_Definition (Acc_Def);
11310
11311 begin
11312 if No (Acc_Subprg) then
11313 return Designates_T (Subtype_Mark (Acc_Def));
11314 end if;
11315
11316 -- Component is an access_to_subprogram: examine its formals,
11317 -- and result definition in the case of an access_to_function.
11318
11319 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11320 while Present (Param_Spec) loop
11321 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11322 and then Mentions_T (Parameter_Type (Param_Spec))
11323 then
11324 return True;
11325
11326 elsif Designates_T (Parameter_Type (Param_Spec)) then
11327 return True;
11328 end if;
11329
11330 Next (Param_Spec);
11331 end loop;
11332
11333 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11334 if Nkind (Result_Definition (Acc_Subprg)) =
11335 N_Access_Definition
11336 then
11337 return Mentions_T (Result_Definition (Acc_Subprg));
11338 else
11339 return Designates_T (Result_Definition (Acc_Subprg));
11340 end if;
11341 end if;
11342
11343 return False;
11344 end Mentions_T;
11345
11346 -- Start of processing for Check_Anonymous_Access_Components
11347
11348 begin
11349 if No (Comp_List) then
11350 return;
11351 end if;
11352
11353 Comp := First (Component_Items (Comp_List));
11354 while Present (Comp) loop
11355 if Nkind (Comp) = N_Component_Declaration
11356 and then Present
11357 (Access_Definition (Component_Definition (Comp)))
11358 and then
11359 Mentions_T (Access_Definition (Component_Definition (Comp)))
11360 then
11361 Comp_Def := Component_Definition (Comp);
11362 Acc_Def :=
11363 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11364
11365 Build_Incomplete_Type_Declaration;
11366 Anon_Access := Make_Temporary (Loc, 'S');
11367
11368 -- Create a declaration for the anonymous access type: either
11369 -- an access_to_object or an access_to_subprogram.
11370
11371 if Present (Acc_Def) then
11372 if Nkind (Acc_Def) = N_Access_Function_Definition then
11373 Type_Def :=
11374 Make_Access_Function_Definition (Loc,
11375 Parameter_Specifications =>
11376 Parameter_Specifications (Acc_Def),
11377 Result_Definition => Result_Definition (Acc_Def));
11378 else
11379 Type_Def :=
11380 Make_Access_Procedure_Definition (Loc,
11381 Parameter_Specifications =>
11382 Parameter_Specifications (Acc_Def));
11383 end if;
11384
11385 else
11386 Type_Def :=
11387 Make_Access_To_Object_Definition (Loc,
11388 Subtype_Indication =>
11389 Relocate_Node
11390 (Subtype_Mark (Access_Definition (Comp_Def))));
11391
11392 Set_Constant_Present
11393 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11394 Set_All_Present
11395 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11396 end if;
11397
11398 Set_Null_Exclusion_Present
11399 (Type_Def,
11400 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11401
11402 Decl :=
11403 Make_Full_Type_Declaration (Loc,
11404 Defining_Identifier => Anon_Access,
11405 Type_Definition => Type_Def);
11406
11407 Insert_Before (Typ_Decl, Decl);
11408 Analyze (Decl);
11409
11410 -- If an access to subprogram, create the extra formals
11411
11412 if Present (Acc_Def) then
11413 Create_Extra_Formals (Designated_Type (Anon_Access));
11414
11415 -- If an access to object, preserve entity of designated type,
11416 -- for ASIS use, before rewriting the component definition.
11417
11418 else
11419 declare
11420 Desig : Entity_Id;
11421
11422 begin
11423 Desig := Entity (Subtype_Indication (Type_Def));
11424
11425 -- If the access definition is to the current record,
11426 -- the visible entity at this point is an incomplete
11427 -- type. Retrieve the full view to simplify ASIS queries
11428
11429 if Ekind (Desig) = E_Incomplete_Type then
11430 Desig := Full_View (Desig);
11431 end if;
11432
11433 Set_Entity
11434 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11435 end;
11436 end if;
11437
11438 Rewrite (Comp_Def,
11439 Make_Component_Definition (Loc,
11440 Subtype_Indication =>
11441 New_Occurrence_Of (Anon_Access, Loc)));
11442
11443 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11444 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11445 else
11446 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11447 end if;
11448
11449 Set_Is_Local_Anonymous_Access (Anon_Access);
11450 end if;
11451
11452 Next (Comp);
11453 end loop;
11454
11455 if Present (Variant_Part (Comp_List)) then
11456 declare
11457 V : Node_Id;
11458 begin
11459 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11460 while Present (V) loop
11461 Check_Anonymous_Access_Components
11462 (Typ_Decl, Typ, Prev, Component_List (V));
11463 Next_Non_Pragma (V);
11464 end loop;
11465 end;
11466 end if;
11467 end Check_Anonymous_Access_Components;
11468
11469 ----------------------
11470 -- Check_Completion --
11471 ----------------------
11472
11473 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11474 E : Entity_Id;
11475
11476 procedure Post_Error;
11477 -- Post error message for lack of completion for entity E
11478
11479 ----------------
11480 -- Post_Error --
11481 ----------------
11482
11483 procedure Post_Error is
11484 procedure Missing_Body;
11485 -- Output missing body message
11486
11487 ------------------
11488 -- Missing_Body --
11489 ------------------
11490
11491 procedure Missing_Body is
11492 begin
11493 -- Spec is in same unit, so we can post on spec
11494
11495 if In_Same_Source_Unit (Body_Id, E) then
11496 Error_Msg_N ("missing body for &", E);
11497
11498 -- Spec is in a separate unit, so we have to post on the body
11499
11500 else
11501 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11502 end if;
11503 end Missing_Body;
11504
11505 -- Start of processing for Post_Error
11506
11507 begin
11508 if not Comes_From_Source (E) then
11509 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11510
11511 -- It may be an anonymous protected type created for a
11512 -- single variable. Post error on variable, if present.
11513
11514 declare
11515 Var : Entity_Id;
11516
11517 begin
11518 Var := First_Entity (Current_Scope);
11519 while Present (Var) loop
11520 exit when Etype (Var) = E
11521 and then Comes_From_Source (Var);
11522
11523 Next_Entity (Var);
11524 end loop;
11525
11526 if Present (Var) then
11527 E := Var;
11528 end if;
11529 end;
11530 end if;
11531 end if;
11532
11533 -- If a generated entity has no completion, then either previous
11534 -- semantic errors have disabled the expansion phase, or else we had
11535 -- missing subunits, or else we are compiling without expansion,
11536 -- or else something is very wrong.
11537
11538 if not Comes_From_Source (E) then
11539 pragma Assert
11540 (Serious_Errors_Detected > 0
11541 or else Configurable_Run_Time_Violations > 0
11542 or else Subunits_Missing
11543 or else not Expander_Active);
11544 return;
11545
11546 -- Here for source entity
11547
11548 else
11549 -- Here if no body to post the error message, so we post the error
11550 -- on the declaration that has no completion. This is not really
11551 -- the right place to post it, think about this later ???
11552
11553 if No (Body_Id) then
11554 if Is_Type (E) then
11555 Error_Msg_NE
11556 ("missing full declaration for }", Parent (E), E);
11557 else
11558 Error_Msg_NE ("missing body for &", Parent (E), E);
11559 end if;
11560
11561 -- Package body has no completion for a declaration that appears
11562 -- in the corresponding spec. Post error on the body, with a
11563 -- reference to the non-completed declaration.
11564
11565 else
11566 Error_Msg_Sloc := Sloc (E);
11567
11568 if Is_Type (E) then
11569 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11570
11571 elsif Is_Overloadable (E)
11572 and then Current_Entity_In_Scope (E) /= E
11573 then
11574 -- It may be that the completion is mistyped and appears as
11575 -- a distinct overloading of the entity.
11576
11577 declare
11578 Candidate : constant Entity_Id :=
11579 Current_Entity_In_Scope (E);
11580 Decl : constant Node_Id :=
11581 Unit_Declaration_Node (Candidate);
11582
11583 begin
11584 if Is_Overloadable (Candidate)
11585 and then Ekind (Candidate) = Ekind (E)
11586 and then Nkind (Decl) = N_Subprogram_Body
11587 and then Acts_As_Spec (Decl)
11588 then
11589 Check_Type_Conformant (Candidate, E);
11590
11591 else
11592 Missing_Body;
11593 end if;
11594 end;
11595
11596 else
11597 Missing_Body;
11598 end if;
11599 end if;
11600 end if;
11601 end Post_Error;
11602
11603 -- Local variables
11604
11605 Pack_Id : constant Entity_Id := Current_Scope;
11606
11607 -- Start of processing for Check_Completion
11608
11609 begin
11610 E := First_Entity (Pack_Id);
11611 while Present (E) loop
11612 if Is_Intrinsic_Subprogram (E) then
11613 null;
11614
11615 -- The following situation requires special handling: a child unit
11616 -- that appears in the context clause of the body of its parent:
11617
11618 -- procedure Parent.Child (...);
11619
11620 -- with Parent.Child;
11621 -- package body Parent is
11622
11623 -- Here Parent.Child appears as a local entity, but should not be
11624 -- flagged as requiring completion, because it is a compilation
11625 -- unit.
11626
11627 -- Ignore missing completion for a subprogram that does not come from
11628 -- source (including the _Call primitive operation of RAS types,
11629 -- which has to have the flag Comes_From_Source for other purposes):
11630 -- we assume that the expander will provide the missing completion.
11631 -- In case of previous errors, other expansion actions that provide
11632 -- bodies for null procedures with not be invoked, so inhibit message
11633 -- in those cases.
11634
11635 -- Note that E_Operator is not in the list that follows, because
11636 -- this kind is reserved for predefined operators, that are
11637 -- intrinsic and do not need completion.
11638
11639 elsif Ekind_In (E, E_Function,
11640 E_Procedure,
11641 E_Generic_Function,
11642 E_Generic_Procedure)
11643 then
11644 if Has_Completion (E) then
11645 null;
11646
11647 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11648 null;
11649
11650 elsif Is_Subprogram (E)
11651 and then (not Comes_From_Source (E)
11652 or else Chars (E) = Name_uCall)
11653 then
11654 null;
11655
11656 elsif
11657 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11658 then
11659 null;
11660
11661 elsif Nkind (Parent (E)) = N_Procedure_Specification
11662 and then Null_Present (Parent (E))
11663 and then Serious_Errors_Detected > 0
11664 then
11665 null;
11666
11667 else
11668 Post_Error;
11669 end if;
11670
11671 elsif Is_Entry (E) then
11672 if not Has_Completion (E) and then
11673 (Ekind (Scope (E)) = E_Protected_Object
11674 or else Ekind (Scope (E)) = E_Protected_Type)
11675 then
11676 Post_Error;
11677 end if;
11678
11679 elsif Is_Package_Or_Generic_Package (E) then
11680 if Unit_Requires_Body (E) then
11681 if not Has_Completion (E)
11682 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11683 N_Compilation_Unit
11684 then
11685 Post_Error;
11686 end if;
11687
11688 elsif not Is_Child_Unit (E) then
11689 May_Need_Implicit_Body (E);
11690 end if;
11691
11692 -- A formal incomplete type (Ada 2012) does not require a completion;
11693 -- other incomplete type declarations do.
11694
11695 elsif Ekind (E) = E_Incomplete_Type
11696 and then No (Underlying_Type (E))
11697 and then not Is_Generic_Type (E)
11698 then
11699 Post_Error;
11700
11701 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11702 and then not Has_Completion (E)
11703 then
11704 Post_Error;
11705
11706 -- A single task declared in the current scope is a constant, verify
11707 -- that the body of its anonymous type is in the same scope. If the
11708 -- task is defined elsewhere, this may be a renaming declaration for
11709 -- which no completion is needed.
11710
11711 elsif Ekind (E) = E_Constant
11712 and then Ekind (Etype (E)) = E_Task_Type
11713 and then not Has_Completion (Etype (E))
11714 and then Scope (Etype (E)) = Current_Scope
11715 then
11716 Post_Error;
11717
11718 elsif Ekind (E) = E_Protected_Object
11719 and then not Has_Completion (Etype (E))
11720 then
11721 Post_Error;
11722
11723 elsif Ekind (E) = E_Record_Type then
11724 if Is_Tagged_Type (E) then
11725 Check_Abstract_Overriding (E);
11726 Check_Conventions (E);
11727 end if;
11728
11729 Check_Aliased_Component_Types (E);
11730
11731 elsif Ekind (E) = E_Array_Type then
11732 Check_Aliased_Component_Types (E);
11733
11734 end if;
11735
11736 Next_Entity (E);
11737 end loop;
11738 end Check_Completion;
11739
11740 ------------------------------------
11741 -- Check_CPP_Type_Has_No_Defaults --
11742 ------------------------------------
11743
11744 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11745 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11746 Clist : Node_Id;
11747 Comp : Node_Id;
11748
11749 begin
11750 -- Obtain the component list
11751
11752 if Nkind (Tdef) = N_Record_Definition then
11753 Clist := Component_List (Tdef);
11754 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11755 Clist := Component_List (Record_Extension_Part (Tdef));
11756 end if;
11757
11758 -- Check all components to ensure no default expressions
11759
11760 if Present (Clist) then
11761 Comp := First (Component_Items (Clist));
11762 while Present (Comp) loop
11763 if Present (Expression (Comp)) then
11764 Error_Msg_N
11765 ("component of imported 'C'P'P type cannot have "
11766 & "default expression", Expression (Comp));
11767 end if;
11768
11769 Next (Comp);
11770 end loop;
11771 end if;
11772 end Check_CPP_Type_Has_No_Defaults;
11773
11774 ----------------------------
11775 -- Check_Delta_Expression --
11776 ----------------------------
11777
11778 procedure Check_Delta_Expression (E : Node_Id) is
11779 begin
11780 if not (Is_Real_Type (Etype (E))) then
11781 Wrong_Type (E, Any_Real);
11782
11783 elsif not Is_OK_Static_Expression (E) then
11784 Flag_Non_Static_Expr
11785 ("non-static expression used for delta value!", E);
11786
11787 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11788 Error_Msg_N ("delta expression must be positive", E);
11789
11790 else
11791 return;
11792 end if;
11793
11794 -- If any of above errors occurred, then replace the incorrect
11795 -- expression by the real 0.1, which should prevent further errors.
11796
11797 Rewrite (E,
11798 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11799 Analyze_And_Resolve (E, Standard_Float);
11800 end Check_Delta_Expression;
11801
11802 -----------------------------
11803 -- Check_Digits_Expression --
11804 -----------------------------
11805
11806 procedure Check_Digits_Expression (E : Node_Id) is
11807 begin
11808 if not (Is_Integer_Type (Etype (E))) then
11809 Wrong_Type (E, Any_Integer);
11810
11811 elsif not Is_OK_Static_Expression (E) then
11812 Flag_Non_Static_Expr
11813 ("non-static expression used for digits value!", E);
11814
11815 elsif Expr_Value (E) <= 0 then
11816 Error_Msg_N ("digits value must be greater than zero", E);
11817
11818 else
11819 return;
11820 end if;
11821
11822 -- If any of above errors occurred, then replace the incorrect
11823 -- expression by the integer 1, which should prevent further errors.
11824
11825 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11826 Analyze_And_Resolve (E, Standard_Integer);
11827
11828 end Check_Digits_Expression;
11829
11830 --------------------------
11831 -- Check_Initialization --
11832 --------------------------
11833
11834 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11835 begin
11836 -- Special processing for limited types
11837
11838 if Is_Limited_Type (T)
11839 and then not In_Instance
11840 and then not In_Inlined_Body
11841 then
11842 if not OK_For_Limited_Init (T, Exp) then
11843
11844 -- In GNAT mode, this is just a warning, to allow it to be evilly
11845 -- turned off. Otherwise it is a real error.
11846
11847 if GNAT_Mode then
11848 Error_Msg_N
11849 ("??cannot initialize entities of limited type!", Exp);
11850
11851 elsif Ada_Version < Ada_2005 then
11852
11853 -- The side effect removal machinery may generate illegal Ada
11854 -- code to avoid the usage of access types and 'reference in
11855 -- SPARK mode. Since this is legal code with respect to theorem
11856 -- proving, do not emit the error.
11857
11858 if GNATprove_Mode
11859 and then Nkind (Exp) = N_Function_Call
11860 and then Nkind (Parent (Exp)) = N_Object_Declaration
11861 and then not Comes_From_Source
11862 (Defining_Identifier (Parent (Exp)))
11863 then
11864 null;
11865
11866 else
11867 Error_Msg_N
11868 ("cannot initialize entities of limited type", Exp);
11869 Explain_Limited_Type (T, Exp);
11870 end if;
11871
11872 else
11873 -- Specialize error message according to kind of illegal
11874 -- initial expression.
11875
11876 if Nkind (Exp) = N_Type_Conversion
11877 and then Nkind (Expression (Exp)) = N_Function_Call
11878 then
11879 -- No error for internally-generated object declarations,
11880 -- which can come from build-in-place assignment statements.
11881
11882 if Nkind (Parent (Exp)) = N_Object_Declaration
11883 and then not Comes_From_Source
11884 (Defining_Identifier (Parent (Exp)))
11885 then
11886 null;
11887
11888 else
11889 Error_Msg_N
11890 ("illegal context for call to function with limited "
11891 & "result", Exp);
11892 end if;
11893
11894 else
11895 Error_Msg_N
11896 ("initialization of limited object requires aggregate or "
11897 & "function call", Exp);
11898 end if;
11899 end if;
11900 end if;
11901 end if;
11902
11903 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11904 -- set unless we can be sure that no range check is required.
11905
11906 if (GNATprove_Mode or not Expander_Active)
11907 and then Is_Scalar_Type (T)
11908 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11909 then
11910 Set_Do_Range_Check (Exp);
11911 end if;
11912 end Check_Initialization;
11913
11914 ----------------------
11915 -- Check_Interfaces --
11916 ----------------------
11917
11918 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11919 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11920
11921 Iface : Node_Id;
11922 Iface_Def : Node_Id;
11923 Iface_Typ : Entity_Id;
11924 Parent_Node : Node_Id;
11925
11926 Is_Task : Boolean := False;
11927 -- Set True if parent type or any progenitor is a task interface
11928
11929 Is_Protected : Boolean := False;
11930 -- Set True if parent type or any progenitor is a protected interface
11931
11932 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11933 -- Check that a progenitor is compatible with declaration. If an error
11934 -- message is output, it is posted on Error_Node.
11935
11936 ------------------
11937 -- Check_Ifaces --
11938 ------------------
11939
11940 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11941 Iface_Id : constant Entity_Id :=
11942 Defining_Identifier (Parent (Iface_Def));
11943 Type_Def : Node_Id;
11944
11945 begin
11946 if Nkind (N) = N_Private_Extension_Declaration then
11947 Type_Def := N;
11948 else
11949 Type_Def := Type_Definition (N);
11950 end if;
11951
11952 if Is_Task_Interface (Iface_Id) then
11953 Is_Task := True;
11954
11955 elsif Is_Protected_Interface (Iface_Id) then
11956 Is_Protected := True;
11957 end if;
11958
11959 if Is_Synchronized_Interface (Iface_Id) then
11960
11961 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11962 -- extension derived from a synchronized interface must explicitly
11963 -- be declared synchronized, because the full view will be a
11964 -- synchronized type.
11965
11966 if Nkind (N) = N_Private_Extension_Declaration then
11967 if not Synchronized_Present (N) then
11968 Error_Msg_NE
11969 ("private extension of& must be explicitly synchronized",
11970 N, Iface_Id);
11971 end if;
11972
11973 -- However, by 3.9.4(16/2), a full type that is a record extension
11974 -- is never allowed to derive from a synchronized interface (note
11975 -- that interfaces must be excluded from this check, because those
11976 -- are represented by derived type definitions in some cases).
11977
11978 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11979 and then not Interface_Present (Type_Definition (N))
11980 then
11981 Error_Msg_N ("record extension cannot derive from synchronized "
11982 & "interface", Error_Node);
11983 end if;
11984 end if;
11985
11986 -- Check that the characteristics of the progenitor are compatible
11987 -- with the explicit qualifier in the declaration.
11988 -- The check only applies to qualifiers that come from source.
11989 -- Limited_Present also appears in the declaration of corresponding
11990 -- records, and the check does not apply to them.
11991
11992 if Limited_Present (Type_Def)
11993 and then not
11994 Is_Concurrent_Record_Type (Defining_Identifier (N))
11995 then
11996 if Is_Limited_Interface (Parent_Type)
11997 and then not Is_Limited_Interface (Iface_Id)
11998 then
11999 Error_Msg_NE
12000 ("progenitor & must be limited interface",
12001 Error_Node, Iface_Id);
12002
12003 elsif
12004 (Task_Present (Iface_Def)
12005 or else Protected_Present (Iface_Def)
12006 or else Synchronized_Present (Iface_Def))
12007 and then Nkind (N) /= N_Private_Extension_Declaration
12008 and then not Error_Posted (N)
12009 then
12010 Error_Msg_NE
12011 ("progenitor & must be limited interface",
12012 Error_Node, Iface_Id);
12013 end if;
12014
12015 -- Protected interfaces can only inherit from limited, synchronized
12016 -- or protected interfaces.
12017
12018 elsif Nkind (N) = N_Full_Type_Declaration
12019 and then Protected_Present (Type_Def)
12020 then
12021 if Limited_Present (Iface_Def)
12022 or else Synchronized_Present (Iface_Def)
12023 or else Protected_Present (Iface_Def)
12024 then
12025 null;
12026
12027 elsif Task_Present (Iface_Def) then
12028 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12029 & "from task interface", Error_Node);
12030
12031 else
12032 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12033 & "from non-limited interface", Error_Node);
12034 end if;
12035
12036 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12037 -- limited and synchronized.
12038
12039 elsif Synchronized_Present (Type_Def) then
12040 if Limited_Present (Iface_Def)
12041 or else Synchronized_Present (Iface_Def)
12042 then
12043 null;
12044
12045 elsif Protected_Present (Iface_Def)
12046 and then Nkind (N) /= N_Private_Extension_Declaration
12047 then
12048 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12049 & "from protected interface", Error_Node);
12050
12051 elsif Task_Present (Iface_Def)
12052 and then Nkind (N) /= N_Private_Extension_Declaration
12053 then
12054 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12055 & "from task interface", Error_Node);
12056
12057 elsif not Is_Limited_Interface (Iface_Id) then
12058 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12059 & "from non-limited interface", Error_Node);
12060 end if;
12061
12062 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12063 -- synchronized or task interfaces.
12064
12065 elsif Nkind (N) = N_Full_Type_Declaration
12066 and then Task_Present (Type_Def)
12067 then
12068 if Limited_Present (Iface_Def)
12069 or else Synchronized_Present (Iface_Def)
12070 or else Task_Present (Iface_Def)
12071 then
12072 null;
12073
12074 elsif Protected_Present (Iface_Def) then
12075 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12076 & "protected interface", Error_Node);
12077
12078 else
12079 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12080 & "non-limited interface", Error_Node);
12081 end if;
12082 end if;
12083 end Check_Ifaces;
12084
12085 -- Start of processing for Check_Interfaces
12086
12087 begin
12088 if Is_Interface (Parent_Type) then
12089 if Is_Task_Interface (Parent_Type) then
12090 Is_Task := True;
12091
12092 elsif Is_Protected_Interface (Parent_Type) then
12093 Is_Protected := True;
12094 end if;
12095 end if;
12096
12097 if Nkind (N) = N_Private_Extension_Declaration then
12098
12099 -- Check that progenitors are compatible with declaration
12100
12101 Iface := First (Interface_List (Def));
12102 while Present (Iface) loop
12103 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12104
12105 Parent_Node := Parent (Base_Type (Iface_Typ));
12106 Iface_Def := Type_Definition (Parent_Node);
12107
12108 if not Is_Interface (Iface_Typ) then
12109 Diagnose_Interface (Iface, Iface_Typ);
12110 else
12111 Check_Ifaces (Iface_Def, Iface);
12112 end if;
12113
12114 Next (Iface);
12115 end loop;
12116
12117 if Is_Task and Is_Protected then
12118 Error_Msg_N
12119 ("type cannot derive from task and protected interface", N);
12120 end if;
12121
12122 return;
12123 end if;
12124
12125 -- Full type declaration of derived type.
12126 -- Check compatibility with parent if it is interface type
12127
12128 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12129 and then Is_Interface (Parent_Type)
12130 then
12131 Parent_Node := Parent (Parent_Type);
12132
12133 -- More detailed checks for interface varieties
12134
12135 Check_Ifaces
12136 (Iface_Def => Type_Definition (Parent_Node),
12137 Error_Node => Subtype_Indication (Type_Definition (N)));
12138 end if;
12139
12140 Iface := First (Interface_List (Def));
12141 while Present (Iface) loop
12142 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12143
12144 Parent_Node := Parent (Base_Type (Iface_Typ));
12145 Iface_Def := Type_Definition (Parent_Node);
12146
12147 if not Is_Interface (Iface_Typ) then
12148 Diagnose_Interface (Iface, Iface_Typ);
12149
12150 else
12151 -- "The declaration of a specific descendant of an interface
12152 -- type freezes the interface type" RM 13.14
12153
12154 Freeze_Before (N, Iface_Typ);
12155 Check_Ifaces (Iface_Def, Error_Node => Iface);
12156 end if;
12157
12158 Next (Iface);
12159 end loop;
12160
12161 if Is_Task and Is_Protected then
12162 Error_Msg_N
12163 ("type cannot derive from task and protected interface", N);
12164 end if;
12165 end Check_Interfaces;
12166
12167 ------------------------------------
12168 -- Check_Or_Process_Discriminants --
12169 ------------------------------------
12170
12171 -- If an incomplete or private type declaration was already given for the
12172 -- type, the discriminants may have already been processed if they were
12173 -- present on the incomplete declaration. In this case a full conformance
12174 -- check has been performed in Find_Type_Name, and we then recheck here
12175 -- some properties that can't be checked on the partial view alone.
12176 -- Otherwise we call Process_Discriminants.
12177
12178 procedure Check_Or_Process_Discriminants
12179 (N : Node_Id;
12180 T : Entity_Id;
12181 Prev : Entity_Id := Empty)
12182 is
12183 begin
12184 if Has_Discriminants (T) then
12185
12186 -- Discriminants are already set on T if they were already present
12187 -- on the partial view. Make them visible to component declarations.
12188
12189 declare
12190 D : Entity_Id;
12191 -- Discriminant on T (full view) referencing expr on partial view
12192
12193 Prev_D : Entity_Id;
12194 -- Entity of corresponding discriminant on partial view
12195
12196 New_D : Node_Id;
12197 -- Discriminant specification for full view, expression is
12198 -- the syntactic copy on full view (which has been checked for
12199 -- conformance with partial view), only used here to post error
12200 -- message.
12201
12202 begin
12203 D := First_Discriminant (T);
12204 New_D := First (Discriminant_Specifications (N));
12205 while Present (D) loop
12206 Prev_D := Current_Entity (D);
12207 Set_Current_Entity (D);
12208 Set_Is_Immediately_Visible (D);
12209 Set_Homonym (D, Prev_D);
12210
12211 -- Handle the case where there is an untagged partial view and
12212 -- the full view is tagged: must disallow discriminants with
12213 -- defaults, unless compiling for Ada 2012, which allows a
12214 -- limited tagged type to have defaulted discriminants (see
12215 -- AI05-0214). However, suppress error here if it was already
12216 -- reported on the default expression of the partial view.
12217
12218 if Is_Tagged_Type (T)
12219 and then Present (Expression (Parent (D)))
12220 and then (not Is_Limited_Type (Current_Scope)
12221 or else Ada_Version < Ada_2012)
12222 and then not Error_Posted (Expression (Parent (D)))
12223 then
12224 if Ada_Version >= Ada_2012 then
12225 Error_Msg_N
12226 ("discriminants of nonlimited tagged type cannot have "
12227 & "defaults",
12228 Expression (New_D));
12229 else
12230 Error_Msg_N
12231 ("discriminants of tagged type cannot have defaults",
12232 Expression (New_D));
12233 end if;
12234 end if;
12235
12236 -- Ada 2005 (AI-230): Access discriminant allowed in
12237 -- non-limited record types.
12238
12239 if Ada_Version < Ada_2005 then
12240
12241 -- This restriction gets applied to the full type here. It
12242 -- has already been applied earlier to the partial view.
12243
12244 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12245 end if;
12246
12247 Next_Discriminant (D);
12248 Next (New_D);
12249 end loop;
12250 end;
12251
12252 elsif Present (Discriminant_Specifications (N)) then
12253 Process_Discriminants (N, Prev);
12254 end if;
12255 end Check_Or_Process_Discriminants;
12256
12257 ----------------------
12258 -- Check_Real_Bound --
12259 ----------------------
12260
12261 procedure Check_Real_Bound (Bound : Node_Id) is
12262 begin
12263 if not Is_Real_Type (Etype (Bound)) then
12264 Error_Msg_N
12265 ("bound in real type definition must be of real type", Bound);
12266
12267 elsif not Is_OK_Static_Expression (Bound) then
12268 Flag_Non_Static_Expr
12269 ("non-static expression used for real type bound!", Bound);
12270
12271 else
12272 return;
12273 end if;
12274
12275 Rewrite
12276 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12277 Analyze (Bound);
12278 Resolve (Bound, Standard_Float);
12279 end Check_Real_Bound;
12280
12281 ------------------------------
12282 -- Complete_Private_Subtype --
12283 ------------------------------
12284
12285 procedure Complete_Private_Subtype
12286 (Priv : Entity_Id;
12287 Full : Entity_Id;
12288 Full_Base : Entity_Id;
12289 Related_Nod : Node_Id)
12290 is
12291 Save_Next_Entity : Entity_Id;
12292 Save_Homonym : Entity_Id;
12293
12294 begin
12295 -- Set semantic attributes for (implicit) private subtype completion.
12296 -- If the full type has no discriminants, then it is a copy of the
12297 -- full view of the base. Otherwise, it is a subtype of the base with
12298 -- a possible discriminant constraint. Save and restore the original
12299 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12300 -- not corrupt the entity chain.
12301
12302 -- Note that the type of the full view is the same entity as the type
12303 -- of the partial view. In this fashion, the subtype has access to the
12304 -- correct view of the parent.
12305
12306 Save_Next_Entity := Next_Entity (Full);
12307 Save_Homonym := Homonym (Priv);
12308
12309 case Ekind (Full_Base) is
12310 when Class_Wide_Kind
12311 | Private_Kind
12312 | Protected_Kind
12313 | Task_Kind
12314 | E_Record_Subtype
12315 | E_Record_Type
12316 =>
12317 Copy_Node (Priv, Full);
12318
12319 Set_Has_Discriminants
12320 (Full, Has_Discriminants (Full_Base));
12321 Set_Has_Unknown_Discriminants
12322 (Full, Has_Unknown_Discriminants (Full_Base));
12323 Set_First_Entity (Full, First_Entity (Full_Base));
12324 Set_Last_Entity (Full, Last_Entity (Full_Base));
12325
12326 -- If the underlying base type is constrained, we know that the
12327 -- full view of the subtype is constrained as well (the converse
12328 -- is not necessarily true).
12329
12330 if Is_Constrained (Full_Base) then
12331 Set_Is_Constrained (Full);
12332 end if;
12333
12334 when others =>
12335 Copy_Node (Full_Base, Full);
12336
12337 Set_Chars (Full, Chars (Priv));
12338 Conditional_Delay (Full, Priv);
12339 Set_Sloc (Full, Sloc (Priv));
12340 end case;
12341
12342 Link_Entities (Full, Save_Next_Entity);
12343 Set_Homonym (Full, Save_Homonym);
12344 Set_Associated_Node_For_Itype (Full, Related_Nod);
12345
12346 -- Set common attributes for all subtypes: kind, convention, etc.
12347
12348 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12349 Set_Convention (Full, Convention (Full_Base));
12350
12351 -- The Etype of the full view is inconsistent. Gigi needs to see the
12352 -- structural full view, which is what the current scheme gives: the
12353 -- Etype of the full view is the etype of the full base. However, if the
12354 -- full base is a derived type, the full view then looks like a subtype
12355 -- of the parent, not a subtype of the full base. If instead we write:
12356
12357 -- Set_Etype (Full, Full_Base);
12358
12359 -- then we get inconsistencies in the front-end (confusion between
12360 -- views). Several outstanding bugs are related to this ???
12361
12362 Set_Is_First_Subtype (Full, False);
12363 Set_Scope (Full, Scope (Priv));
12364 Set_Size_Info (Full, Full_Base);
12365 Set_RM_Size (Full, RM_Size (Full_Base));
12366 Set_Is_Itype (Full);
12367
12368 -- For the unusual case of a type with unknown discriminants whose
12369 -- completion is an array, use the proper full base.
12370
12371 if Is_Array_Type (Full_Base)
12372 and then Has_Unknown_Discriminants (Priv)
12373 then
12374 Set_Etype (Full, Full_Base);
12375 end if;
12376
12377 -- A subtype of a private-type-without-discriminants, whose full-view
12378 -- has discriminants with default expressions, is not constrained.
12379
12380 if not Has_Discriminants (Priv) then
12381 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12382
12383 if Has_Discriminants (Full_Base) then
12384 Set_Discriminant_Constraint
12385 (Full, Discriminant_Constraint (Full_Base));
12386
12387 -- The partial view may have been indefinite, the full view
12388 -- might not be.
12389
12390 Set_Has_Unknown_Discriminants
12391 (Full, Has_Unknown_Discriminants (Full_Base));
12392 end if;
12393 end if;
12394
12395 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12396 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12397
12398 -- Freeze the private subtype entity if its parent is delayed, and not
12399 -- already frozen. We skip this processing if the type is an anonymous
12400 -- subtype of a record component, or is the corresponding record of a
12401 -- protected type, since these are processed when the enclosing type
12402 -- is frozen. If the parent type is declared in a nested package then
12403 -- the freezing of the private and full views also happens later.
12404
12405 if not Is_Type (Scope (Full)) then
12406 if Is_Itype (Priv)
12407 and then In_Same_Source_Unit (Full, Full_Base)
12408 and then Scope (Full_Base) /= Scope (Full)
12409 then
12410 Set_Has_Delayed_Freeze (Full);
12411 Set_Has_Delayed_Freeze (Priv);
12412
12413 else
12414 Set_Has_Delayed_Freeze (Full,
12415 Has_Delayed_Freeze (Full_Base)
12416 and then not Is_Frozen (Full_Base));
12417 end if;
12418 end if;
12419
12420 Set_Freeze_Node (Full, Empty);
12421 Set_Is_Frozen (Full, False);
12422 Set_Full_View (Priv, Full);
12423
12424 if Has_Discriminants (Full) then
12425 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12426 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12427
12428 if Has_Unknown_Discriminants (Full) then
12429 Set_Discriminant_Constraint (Full, No_Elist);
12430 end if;
12431 end if;
12432
12433 if Ekind (Full_Base) = E_Record_Type
12434 and then Has_Discriminants (Full_Base)
12435 and then Has_Discriminants (Priv) -- might not, if errors
12436 and then not Has_Unknown_Discriminants (Priv)
12437 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12438 then
12439 Create_Constrained_Components
12440 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12441
12442 -- If the full base is itself derived from private, build a congruent
12443 -- subtype of its underlying type, for use by the back end. For a
12444 -- constrained record component, the declaration cannot be placed on
12445 -- the component list, but it must nevertheless be built an analyzed, to
12446 -- supply enough information for Gigi to compute the size of component.
12447
12448 elsif Ekind (Full_Base) in Private_Kind
12449 and then Is_Derived_Type (Full_Base)
12450 and then Has_Discriminants (Full_Base)
12451 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12452 then
12453 if not Is_Itype (Priv)
12454 and then
12455 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12456 then
12457 Build_Underlying_Full_View
12458 (Parent (Priv), Full, Etype (Full_Base));
12459
12460 elsif Nkind (Related_Nod) = N_Component_Declaration then
12461 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12462 end if;
12463
12464 elsif Is_Record_Type (Full_Base) then
12465
12466 -- Show Full is simply a renaming of Full_Base
12467
12468 Set_Cloned_Subtype (Full, Full_Base);
12469 end if;
12470
12471 -- It is unsafe to share the bounds of a scalar type, because the Itype
12472 -- is elaborated on demand, and if a bound is non-static then different
12473 -- orders of elaboration in different units will lead to different
12474 -- external symbols.
12475
12476 if Is_Scalar_Type (Full_Base) then
12477 Set_Scalar_Range (Full,
12478 Make_Range (Sloc (Related_Nod),
12479 Low_Bound =>
12480 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12481 High_Bound =>
12482 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12483
12484 -- This completion inherits the bounds of the full parent, but if
12485 -- the parent is an unconstrained floating point type, so is the
12486 -- completion.
12487
12488 if Is_Floating_Point_Type (Full_Base) then
12489 Set_Includes_Infinities
12490 (Scalar_Range (Full), Has_Infinities (Full_Base));
12491 end if;
12492 end if;
12493
12494 -- ??? It seems that a lot of fields are missing that should be copied
12495 -- from Full_Base to Full. Here are some that are introduced in a
12496 -- non-disruptive way but a cleanup is necessary.
12497
12498 if Is_Tagged_Type (Full_Base) then
12499 Set_Is_Tagged_Type (Full);
12500 Set_Direct_Primitive_Operations
12501 (Full, Direct_Primitive_Operations (Full_Base));
12502 Set_No_Tagged_Streams_Pragma
12503 (Full, No_Tagged_Streams_Pragma (Full_Base));
12504
12505 -- Inherit class_wide type of full_base in case the partial view was
12506 -- not tagged. Otherwise it has already been created when the private
12507 -- subtype was analyzed.
12508
12509 if No (Class_Wide_Type (Full)) then
12510 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12511 end if;
12512
12513 -- If this is a subtype of a protected or task type, constrain its
12514 -- corresponding record, unless this is a subtype without constraints,
12515 -- i.e. a simple renaming as with an actual subtype in an instance.
12516
12517 elsif Is_Concurrent_Type (Full_Base) then
12518 if Has_Discriminants (Full)
12519 and then Present (Corresponding_Record_Type (Full_Base))
12520 and then
12521 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12522 then
12523 Set_Corresponding_Record_Type (Full,
12524 Constrain_Corresponding_Record
12525 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12526
12527 else
12528 Set_Corresponding_Record_Type (Full,
12529 Corresponding_Record_Type (Full_Base));
12530 end if;
12531 end if;
12532
12533 -- Link rep item chain, and also setting of Has_Predicates from private
12534 -- subtype to full subtype, since we will need these on the full subtype
12535 -- to create the predicate function. Note that the full subtype may
12536 -- already have rep items, inherited from the full view of the base
12537 -- type, so we must be sure not to overwrite these entries.
12538
12539 declare
12540 Append : Boolean;
12541 Item : Node_Id;
12542 Next_Item : Node_Id;
12543 Priv_Item : Node_Id;
12544
12545 begin
12546 Item := First_Rep_Item (Full);
12547 Priv_Item := First_Rep_Item (Priv);
12548
12549 -- If no existing rep items on full type, we can just link directly
12550 -- to the list of items on the private type, if any exist.. Same if
12551 -- the rep items are only those inherited from the base
12552
12553 if (No (Item)
12554 or else Nkind (Item) /= N_Aspect_Specification
12555 or else Entity (Item) = Full_Base)
12556 and then Present (First_Rep_Item (Priv))
12557 then
12558 Set_First_Rep_Item (Full, Priv_Item);
12559
12560 -- Otherwise, search to the end of items currently linked to the full
12561 -- subtype and append the private items to the end. However, if Priv
12562 -- and Full already have the same list of rep items, then the append
12563 -- is not done, as that would create a circularity.
12564 --
12565 -- The partial view may have a predicate and the rep item lists of
12566 -- both views agree when inherited from the same ancestor. In that
12567 -- case, simply propagate the list from one view to the other.
12568 -- A more complex analysis needed here ???
12569
12570 elsif Present (Priv_Item)
12571 and then Item = Next_Rep_Item (Priv_Item)
12572 then
12573 Set_First_Rep_Item (Full, Priv_Item);
12574
12575 elsif Item /= Priv_Item then
12576 Append := True;
12577 loop
12578 Next_Item := Next_Rep_Item (Item);
12579 exit when No (Next_Item);
12580 Item := Next_Item;
12581
12582 -- If the private view has aspect specifications, the full view
12583 -- inherits them. Since these aspects may already have been
12584 -- attached to the full view during derivation, do not append
12585 -- them if already present.
12586
12587 if Item = First_Rep_Item (Priv) then
12588 Append := False;
12589 exit;
12590 end if;
12591 end loop;
12592
12593 -- And link the private type items at the end of the chain
12594
12595 if Append then
12596 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12597 end if;
12598 end if;
12599 end;
12600
12601 -- Make sure Has_Predicates is set on full type if it is set on the
12602 -- private type. Note that it may already be set on the full type and
12603 -- if so, we don't want to unset it. Similarly, propagate information
12604 -- about delayed aspects, because the corresponding pragmas must be
12605 -- analyzed when one of the views is frozen. This last step is needed
12606 -- in particular when the full type is a scalar type for which an
12607 -- anonymous base type is constructed.
12608
12609 -- The predicate functions are generated either at the freeze point
12610 -- of the type or at the end of the visible part, and we must avoid
12611 -- generating them twice.
12612
12613 if Has_Predicates (Priv) then
12614 Set_Has_Predicates (Full);
12615
12616 if Present (Predicate_Function (Priv))
12617 and then No (Predicate_Function (Full))
12618 then
12619 Set_Predicate_Function (Full, Predicate_Function (Priv));
12620 end if;
12621 end if;
12622
12623 if Has_Delayed_Aspects (Priv) then
12624 Set_Has_Delayed_Aspects (Full);
12625 end if;
12626 end Complete_Private_Subtype;
12627
12628 ----------------------------
12629 -- Constant_Redeclaration --
12630 ----------------------------
12631
12632 procedure Constant_Redeclaration
12633 (Id : Entity_Id;
12634 N : Node_Id;
12635 T : out Entity_Id)
12636 is
12637 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12638 Obj_Def : constant Node_Id := Object_Definition (N);
12639 New_T : Entity_Id;
12640
12641 procedure Check_Possible_Deferred_Completion
12642 (Prev_Id : Entity_Id;
12643 Prev_Obj_Def : Node_Id;
12644 Curr_Obj_Def : Node_Id);
12645 -- Determine whether the two object definitions describe the partial
12646 -- and the full view of a constrained deferred constant. Generate
12647 -- a subtype for the full view and verify that it statically matches
12648 -- the subtype of the partial view.
12649
12650 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12651 -- If deferred constant is an access type initialized with an allocator,
12652 -- check whether there is an illegal recursion in the definition,
12653 -- through a default value of some record subcomponent. This is normally
12654 -- detected when generating init procs, but requires this additional
12655 -- mechanism when expansion is disabled.
12656
12657 ----------------------------------------
12658 -- Check_Possible_Deferred_Completion --
12659 ----------------------------------------
12660
12661 procedure Check_Possible_Deferred_Completion
12662 (Prev_Id : Entity_Id;
12663 Prev_Obj_Def : Node_Id;
12664 Curr_Obj_Def : Node_Id)
12665 is
12666 begin
12667 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12668 and then Present (Constraint (Prev_Obj_Def))
12669 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12670 and then Present (Constraint (Curr_Obj_Def))
12671 then
12672 declare
12673 Loc : constant Source_Ptr := Sloc (N);
12674 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12675 Decl : constant Node_Id :=
12676 Make_Subtype_Declaration (Loc,
12677 Defining_Identifier => Def_Id,
12678 Subtype_Indication =>
12679 Relocate_Node (Curr_Obj_Def));
12680
12681 begin
12682 Insert_Before_And_Analyze (N, Decl);
12683 Set_Etype (Id, Def_Id);
12684
12685 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12686 Error_Msg_Sloc := Sloc (Prev_Id);
12687 Error_Msg_N ("subtype does not statically match deferred "
12688 & "declaration #", N);
12689 end if;
12690 end;
12691 end if;
12692 end Check_Possible_Deferred_Completion;
12693
12694 ---------------------------------
12695 -- Check_Recursive_Declaration --
12696 ---------------------------------
12697
12698 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12699 Comp : Entity_Id;
12700
12701 begin
12702 if Is_Record_Type (Typ) then
12703 Comp := First_Component (Typ);
12704 while Present (Comp) loop
12705 if Comes_From_Source (Comp) then
12706 if Present (Expression (Parent (Comp)))
12707 and then Is_Entity_Name (Expression (Parent (Comp)))
12708 and then Entity (Expression (Parent (Comp))) = Prev
12709 then
12710 Error_Msg_Sloc := Sloc (Parent (Comp));
12711 Error_Msg_NE
12712 ("illegal circularity with declaration for & #",
12713 N, Comp);
12714 return;
12715
12716 elsif Is_Record_Type (Etype (Comp)) then
12717 Check_Recursive_Declaration (Etype (Comp));
12718 end if;
12719 end if;
12720
12721 Next_Component (Comp);
12722 end loop;
12723 end if;
12724 end Check_Recursive_Declaration;
12725
12726 -- Start of processing for Constant_Redeclaration
12727
12728 begin
12729 if Nkind (Parent (Prev)) = N_Object_Declaration then
12730 if Nkind (Object_Definition
12731 (Parent (Prev))) = N_Subtype_Indication
12732 then
12733 -- Find type of new declaration. The constraints of the two
12734 -- views must match statically, but there is no point in
12735 -- creating an itype for the full view.
12736
12737 if Nkind (Obj_Def) = N_Subtype_Indication then
12738 Find_Type (Subtype_Mark (Obj_Def));
12739 New_T := Entity (Subtype_Mark (Obj_Def));
12740
12741 else
12742 Find_Type (Obj_Def);
12743 New_T := Entity (Obj_Def);
12744 end if;
12745
12746 T := Etype (Prev);
12747
12748 else
12749 -- The full view may impose a constraint, even if the partial
12750 -- view does not, so construct the subtype.
12751
12752 New_T := Find_Type_Of_Object (Obj_Def, N);
12753 T := New_T;
12754 end if;
12755
12756 else
12757 -- Current declaration is illegal, diagnosed below in Enter_Name
12758
12759 T := Empty;
12760 New_T := Any_Type;
12761 end if;
12762
12763 -- If previous full declaration or a renaming declaration exists, or if
12764 -- a homograph is present, let Enter_Name handle it, either with an
12765 -- error or with the removal of an overridden implicit subprogram.
12766 -- The previous one is a full declaration if it has an expression
12767 -- (which in the case of an aggregate is indicated by the Init flag).
12768
12769 if Ekind (Prev) /= E_Constant
12770 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12771 or else Present (Expression (Parent (Prev)))
12772 or else Has_Init_Expression (Parent (Prev))
12773 or else Present (Full_View (Prev))
12774 then
12775 Enter_Name (Id);
12776
12777 -- Verify that types of both declarations match, or else that both types
12778 -- are anonymous access types whose designated subtypes statically match
12779 -- (as allowed in Ada 2005 by AI-385).
12780
12781 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12782 and then
12783 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12784 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12785 or else Is_Access_Constant (Etype (New_T)) /=
12786 Is_Access_Constant (Etype (Prev))
12787 or else Can_Never_Be_Null (Etype (New_T)) /=
12788 Can_Never_Be_Null (Etype (Prev))
12789 or else Null_Exclusion_Present (Parent (Prev)) /=
12790 Null_Exclusion_Present (Parent (Id))
12791 or else not Subtypes_Statically_Match
12792 (Designated_Type (Etype (Prev)),
12793 Designated_Type (Etype (New_T))))
12794 then
12795 Error_Msg_Sloc := Sloc (Prev);
12796 Error_Msg_N ("type does not match declaration#", N);
12797 Set_Full_View (Prev, Id);
12798 Set_Etype (Id, Any_Type);
12799
12800 -- A deferred constant whose type is an anonymous array is always
12801 -- illegal (unless imported). A detailed error message might be
12802 -- helpful for Ada beginners.
12803
12804 if Nkind (Object_Definition (Parent (Prev)))
12805 = N_Constrained_Array_Definition
12806 and then Nkind (Object_Definition (N))
12807 = N_Constrained_Array_Definition
12808 then
12809 Error_Msg_N ("\each anonymous array is a distinct type", N);
12810 Error_Msg_N ("a deferred constant must have a named type",
12811 Object_Definition (Parent (Prev)));
12812 end if;
12813
12814 elsif
12815 Null_Exclusion_Present (Parent (Prev))
12816 and then not Null_Exclusion_Present (N)
12817 then
12818 Error_Msg_Sloc := Sloc (Prev);
12819 Error_Msg_N ("null-exclusion does not match declaration#", N);
12820 Set_Full_View (Prev, Id);
12821 Set_Etype (Id, Any_Type);
12822
12823 -- If so, process the full constant declaration
12824
12825 else
12826 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12827 -- the deferred declaration is constrained, then the subtype defined
12828 -- by the subtype_indication in the full declaration shall match it
12829 -- statically.
12830
12831 Check_Possible_Deferred_Completion
12832 (Prev_Id => Prev,
12833 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12834 Curr_Obj_Def => Obj_Def);
12835
12836 Set_Full_View (Prev, Id);
12837 Set_Is_Public (Id, Is_Public (Prev));
12838 Set_Is_Internal (Id);
12839 Append_Entity (Id, Current_Scope);
12840
12841 -- Check ALIASED present if present before (RM 7.4(7))
12842
12843 if Is_Aliased (Prev)
12844 and then not Aliased_Present (N)
12845 then
12846 Error_Msg_Sloc := Sloc (Prev);
12847 Error_Msg_N ("ALIASED required (see declaration #)", N);
12848 end if;
12849
12850 -- Check that placement is in private part and that the incomplete
12851 -- declaration appeared in the visible part.
12852
12853 if Ekind (Current_Scope) = E_Package
12854 and then not In_Private_Part (Current_Scope)
12855 then
12856 Error_Msg_Sloc := Sloc (Prev);
12857 Error_Msg_N
12858 ("full constant for declaration # must be in private part", N);
12859
12860 elsif Ekind (Current_Scope) = E_Package
12861 and then
12862 List_Containing (Parent (Prev)) /=
12863 Visible_Declarations (Package_Specification (Current_Scope))
12864 then
12865 Error_Msg_N
12866 ("deferred constant must be declared in visible part",
12867 Parent (Prev));
12868 end if;
12869
12870 if Is_Access_Type (T)
12871 and then Nkind (Expression (N)) = N_Allocator
12872 then
12873 Check_Recursive_Declaration (Designated_Type (T));
12874 end if;
12875
12876 -- A deferred constant is a visible entity. If type has invariants,
12877 -- verify that the initial value satisfies them. This is not done in
12878 -- GNATprove mode, as GNATprove handles invariant checks itself.
12879
12880 if Has_Invariants (T)
12881 and then Present (Invariant_Procedure (T))
12882 and then not GNATprove_Mode
12883 then
12884 Insert_After (N,
12885 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12886 end if;
12887 end if;
12888 end Constant_Redeclaration;
12889
12890 ----------------------
12891 -- Constrain_Access --
12892 ----------------------
12893
12894 procedure Constrain_Access
12895 (Def_Id : in out Entity_Id;
12896 S : Node_Id;
12897 Related_Nod : Node_Id)
12898 is
12899 T : constant Entity_Id := Entity (Subtype_Mark (S));
12900 Desig_Type : constant Entity_Id := Designated_Type (T);
12901 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12902 Constraint_OK : Boolean := True;
12903
12904 begin
12905 if Is_Array_Type (Desig_Type) then
12906 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12907
12908 elsif (Is_Record_Type (Desig_Type)
12909 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12910 and then not Is_Constrained (Desig_Type)
12911 then
12912 -- ??? The following code is a temporary bypass to ignore a
12913 -- discriminant constraint on access type if it is constraining
12914 -- the current record. Avoid creating the implicit subtype of the
12915 -- record we are currently compiling since right now, we cannot
12916 -- handle these. For now, just return the access type itself.
12917
12918 if Desig_Type = Current_Scope
12919 and then No (Def_Id)
12920 then
12921 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12922 Def_Id := Entity (Subtype_Mark (S));
12923
12924 -- This call added to ensure that the constraint is analyzed
12925 -- (needed for a B test). Note that we still return early from
12926 -- this procedure to avoid recursive processing. ???
12927
12928 Constrain_Discriminated_Type
12929 (Desig_Subtype, S, Related_Nod, For_Access => True);
12930 return;
12931 end if;
12932
12933 -- Enforce rule that the constraint is illegal if there is an
12934 -- unconstrained view of the designated type. This means that the
12935 -- partial view (either a private type declaration or a derivation
12936 -- from a private type) has no discriminants. (Defect Report
12937 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12938
12939 -- Rule updated for Ada 2005: The private type is said to have
12940 -- a constrained partial view, given that objects of the type
12941 -- can be declared. Furthermore, the rule applies to all access
12942 -- types, unlike the rule concerning default discriminants (see
12943 -- RM 3.7.1(7/3))
12944
12945 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12946 and then Has_Private_Declaration (Desig_Type)
12947 and then In_Open_Scopes (Scope (Desig_Type))
12948 and then Has_Discriminants (Desig_Type)
12949 then
12950 declare
12951 Pack : constant Node_Id :=
12952 Unit_Declaration_Node (Scope (Desig_Type));
12953 Decls : List_Id;
12954 Decl : Node_Id;
12955
12956 begin
12957 if Nkind (Pack) = N_Package_Declaration then
12958 Decls := Visible_Declarations (Specification (Pack));
12959 Decl := First (Decls);
12960 while Present (Decl) loop
12961 if (Nkind (Decl) = N_Private_Type_Declaration
12962 and then Chars (Defining_Identifier (Decl)) =
12963 Chars (Desig_Type))
12964
12965 or else
12966 (Nkind (Decl) = N_Full_Type_Declaration
12967 and then
12968 Chars (Defining_Identifier (Decl)) =
12969 Chars (Desig_Type)
12970 and then Is_Derived_Type (Desig_Type)
12971 and then
12972 Has_Private_Declaration (Etype (Desig_Type)))
12973 then
12974 if No (Discriminant_Specifications (Decl)) then
12975 Error_Msg_N
12976 ("cannot constrain access type if designated "
12977 & "type has constrained partial view", S);
12978 end if;
12979
12980 exit;
12981 end if;
12982
12983 Next (Decl);
12984 end loop;
12985 end if;
12986 end;
12987 end if;
12988
12989 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12990 For_Access => True);
12991
12992 elsif Is_Concurrent_Type (Desig_Type)
12993 and then not Is_Constrained (Desig_Type)
12994 then
12995 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12996
12997 else
12998 Error_Msg_N ("invalid constraint on access type", S);
12999
13000 -- We simply ignore an invalid constraint
13001
13002 Desig_Subtype := Desig_Type;
13003 Constraint_OK := False;
13004 end if;
13005
13006 if No (Def_Id) then
13007 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13008 else
13009 Set_Ekind (Def_Id, E_Access_Subtype);
13010 end if;
13011
13012 if Constraint_OK then
13013 Set_Etype (Def_Id, Base_Type (T));
13014
13015 if Is_Private_Type (Desig_Type) then
13016 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13017 end if;
13018 else
13019 Set_Etype (Def_Id, Any_Type);
13020 end if;
13021
13022 Set_Size_Info (Def_Id, T);
13023 Set_Is_Constrained (Def_Id, Constraint_OK);
13024 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13025 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13026 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13027
13028 Conditional_Delay (Def_Id, T);
13029
13030 -- AI-363 : Subtypes of general access types whose designated types have
13031 -- default discriminants are disallowed. In instances, the rule has to
13032 -- be checked against the actual, of which T is the subtype. In a
13033 -- generic body, the rule is checked assuming that the actual type has
13034 -- defaulted discriminants.
13035
13036 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13037 if Ekind (Base_Type (T)) = E_General_Access_Type
13038 and then Has_Defaulted_Discriminants (Desig_Type)
13039 then
13040 if Ada_Version < Ada_2005 then
13041 Error_Msg_N
13042 ("access subtype of general access type would not " &
13043 "be allowed in Ada 2005?y?", S);
13044 else
13045 Error_Msg_N
13046 ("access subtype of general access type not allowed", S);
13047 end if;
13048
13049 Error_Msg_N ("\discriminants have defaults", S);
13050
13051 elsif Is_Access_Type (T)
13052 and then Is_Generic_Type (Desig_Type)
13053 and then Has_Discriminants (Desig_Type)
13054 and then In_Package_Body (Current_Scope)
13055 then
13056 if Ada_Version < Ada_2005 then
13057 Error_Msg_N
13058 ("access subtype would not be allowed in generic body "
13059 & "in Ada 2005?y?", S);
13060 else
13061 Error_Msg_N
13062 ("access subtype not allowed in generic body", S);
13063 end if;
13064
13065 Error_Msg_N
13066 ("\designated type is a discriminated formal", S);
13067 end if;
13068 end if;
13069 end Constrain_Access;
13070
13071 ---------------------
13072 -- Constrain_Array --
13073 ---------------------
13074
13075 procedure Constrain_Array
13076 (Def_Id : in out Entity_Id;
13077 SI : Node_Id;
13078 Related_Nod : Node_Id;
13079 Related_Id : Entity_Id;
13080 Suffix : Character)
13081 is
13082 C : constant Node_Id := Constraint (SI);
13083 Number_Of_Constraints : Nat := 0;
13084 Index : Node_Id;
13085 S, T : Entity_Id;
13086 Constraint_OK : Boolean := True;
13087
13088 begin
13089 T := Entity (Subtype_Mark (SI));
13090
13091 if Is_Access_Type (T) then
13092 T := Designated_Type (T);
13093 end if;
13094
13095 -- If an index constraint follows a subtype mark in a subtype indication
13096 -- then the type or subtype denoted by the subtype mark must not already
13097 -- impose an index constraint. The subtype mark must denote either an
13098 -- unconstrained array type or an access type whose designated type
13099 -- is such an array type... (RM 3.6.1)
13100
13101 if Is_Constrained (T) then
13102 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13103 Constraint_OK := False;
13104
13105 else
13106 S := First (Constraints (C));
13107 while Present (S) loop
13108 Number_Of_Constraints := Number_Of_Constraints + 1;
13109 Next (S);
13110 end loop;
13111
13112 -- In either case, the index constraint must provide a discrete
13113 -- range for each index of the array type and the type of each
13114 -- discrete range must be the same as that of the corresponding
13115 -- index. (RM 3.6.1)
13116
13117 if Number_Of_Constraints /= Number_Dimensions (T) then
13118 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13119 Constraint_OK := False;
13120
13121 else
13122 S := First (Constraints (C));
13123 Index := First_Index (T);
13124 Analyze (Index);
13125
13126 -- Apply constraints to each index type
13127
13128 for J in 1 .. Number_Of_Constraints loop
13129 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13130 Next (Index);
13131 Next (S);
13132 end loop;
13133
13134 end if;
13135 end if;
13136
13137 if No (Def_Id) then
13138 Def_Id :=
13139 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13140 Set_Parent (Def_Id, Related_Nod);
13141
13142 else
13143 Set_Ekind (Def_Id, E_Array_Subtype);
13144 end if;
13145
13146 Set_Size_Info (Def_Id, (T));
13147 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13148 Set_Etype (Def_Id, Base_Type (T));
13149
13150 if Constraint_OK then
13151 Set_First_Index (Def_Id, First (Constraints (C)));
13152 else
13153 Set_First_Index (Def_Id, First_Index (T));
13154 end if;
13155
13156 Set_Is_Constrained (Def_Id, True);
13157 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13158 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13159
13160 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13161 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13162
13163 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13164 -- We need to initialize the attribute because if Def_Id is previously
13165 -- analyzed through a limited_with clause, it will have the attributes
13166 -- of an incomplete type, one of which is an Elist that overlaps the
13167 -- Packed_Array_Impl_Type field.
13168
13169 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13170
13171 -- Build a freeze node if parent still needs one. Also make sure that
13172 -- the Depends_On_Private status is set because the subtype will need
13173 -- reprocessing at the time the base type does, and also we must set a
13174 -- conditional delay.
13175
13176 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13177 Conditional_Delay (Def_Id, T);
13178 end Constrain_Array;
13179
13180 ------------------------------
13181 -- Constrain_Component_Type --
13182 ------------------------------
13183
13184 function Constrain_Component_Type
13185 (Comp : Entity_Id;
13186 Constrained_Typ : Entity_Id;
13187 Related_Node : Node_Id;
13188 Typ : Entity_Id;
13189 Constraints : Elist_Id) return Entity_Id
13190 is
13191 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13192 Compon_Type : constant Entity_Id := Etype (Comp);
13193
13194 function Build_Constrained_Array_Type
13195 (Old_Type : Entity_Id) return Entity_Id;
13196 -- If Old_Type is an array type, one of whose indexes is constrained
13197 -- by a discriminant, build an Itype whose constraint replaces the
13198 -- discriminant with its value in the constraint.
13199
13200 function Build_Constrained_Discriminated_Type
13201 (Old_Type : Entity_Id) return Entity_Id;
13202 -- Ditto for record components
13203
13204 function Build_Constrained_Access_Type
13205 (Old_Type : Entity_Id) return Entity_Id;
13206 -- Ditto for access types. Makes use of previous two functions, to
13207 -- constrain designated type.
13208
13209 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13210 -- T is an array or discriminated type, C is a list of constraints
13211 -- that apply to T. This routine builds the constrained subtype.
13212
13213 function Is_Discriminant (Expr : Node_Id) return Boolean;
13214 -- Returns True if Expr is a discriminant
13215
13216 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13217 -- Find the value of discriminant Discrim in Constraint
13218
13219 -----------------------------------
13220 -- Build_Constrained_Access_Type --
13221 -----------------------------------
13222
13223 function Build_Constrained_Access_Type
13224 (Old_Type : Entity_Id) return Entity_Id
13225 is
13226 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13227 Itype : Entity_Id;
13228 Desig_Subtype : Entity_Id;
13229 Scop : Entity_Id;
13230
13231 begin
13232 -- if the original access type was not embedded in the enclosing
13233 -- type definition, there is no need to produce a new access
13234 -- subtype. In fact every access type with an explicit constraint
13235 -- generates an itype whose scope is the enclosing record.
13236
13237 if not Is_Type (Scope (Old_Type)) then
13238 return Old_Type;
13239
13240 elsif Is_Array_Type (Desig_Type) then
13241 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13242
13243 elsif Has_Discriminants (Desig_Type) then
13244
13245 -- This may be an access type to an enclosing record type for
13246 -- which we are constructing the constrained components. Return
13247 -- the enclosing record subtype. This is not always correct,
13248 -- but avoids infinite recursion. ???
13249
13250 Desig_Subtype := Any_Type;
13251
13252 for J in reverse 0 .. Scope_Stack.Last loop
13253 Scop := Scope_Stack.Table (J).Entity;
13254
13255 if Is_Type (Scop)
13256 and then Base_Type (Scop) = Base_Type (Desig_Type)
13257 then
13258 Desig_Subtype := Scop;
13259 end if;
13260
13261 exit when not Is_Type (Scop);
13262 end loop;
13263
13264 if Desig_Subtype = Any_Type then
13265 Desig_Subtype :=
13266 Build_Constrained_Discriminated_Type (Desig_Type);
13267 end if;
13268
13269 else
13270 return Old_Type;
13271 end if;
13272
13273 if Desig_Subtype /= Desig_Type then
13274
13275 -- The Related_Node better be here or else we won't be able
13276 -- to attach new itypes to a node in the tree.
13277
13278 pragma Assert (Present (Related_Node));
13279
13280 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13281
13282 Set_Etype (Itype, Base_Type (Old_Type));
13283 Set_Size_Info (Itype, (Old_Type));
13284 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13285 Set_Depends_On_Private (Itype, Has_Private_Component
13286 (Old_Type));
13287 Set_Is_Access_Constant (Itype, Is_Access_Constant
13288 (Old_Type));
13289
13290 -- The new itype needs freezing when it depends on a not frozen
13291 -- type and the enclosing subtype needs freezing.
13292
13293 if Has_Delayed_Freeze (Constrained_Typ)
13294 and then not Is_Frozen (Constrained_Typ)
13295 then
13296 Conditional_Delay (Itype, Base_Type (Old_Type));
13297 end if;
13298
13299 return Itype;
13300
13301 else
13302 return Old_Type;
13303 end if;
13304 end Build_Constrained_Access_Type;
13305
13306 ----------------------------------
13307 -- Build_Constrained_Array_Type --
13308 ----------------------------------
13309
13310 function Build_Constrained_Array_Type
13311 (Old_Type : Entity_Id) return Entity_Id
13312 is
13313 Lo_Expr : Node_Id;
13314 Hi_Expr : Node_Id;
13315 Old_Index : Node_Id;
13316 Range_Node : Node_Id;
13317 Constr_List : List_Id;
13318
13319 Need_To_Create_Itype : Boolean := False;
13320
13321 begin
13322 Old_Index := First_Index (Old_Type);
13323 while Present (Old_Index) loop
13324 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13325
13326 if Is_Discriminant (Lo_Expr)
13327 or else
13328 Is_Discriminant (Hi_Expr)
13329 then
13330 Need_To_Create_Itype := True;
13331 end if;
13332
13333 Next_Index (Old_Index);
13334 end loop;
13335
13336 if Need_To_Create_Itype then
13337 Constr_List := New_List;
13338
13339 Old_Index := First_Index (Old_Type);
13340 while Present (Old_Index) loop
13341 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13342
13343 if Is_Discriminant (Lo_Expr) then
13344 Lo_Expr := Get_Discr_Value (Lo_Expr);
13345 end if;
13346
13347 if Is_Discriminant (Hi_Expr) then
13348 Hi_Expr := Get_Discr_Value (Hi_Expr);
13349 end if;
13350
13351 Range_Node :=
13352 Make_Range
13353 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13354
13355 Append (Range_Node, To => Constr_List);
13356
13357 Next_Index (Old_Index);
13358 end loop;
13359
13360 return Build_Subtype (Old_Type, Constr_List);
13361
13362 else
13363 return Old_Type;
13364 end if;
13365 end Build_Constrained_Array_Type;
13366
13367 ------------------------------------------
13368 -- Build_Constrained_Discriminated_Type --
13369 ------------------------------------------
13370
13371 function Build_Constrained_Discriminated_Type
13372 (Old_Type : Entity_Id) return Entity_Id
13373 is
13374 Expr : Node_Id;
13375 Constr_List : List_Id;
13376 Old_Constraint : Elmt_Id;
13377
13378 Need_To_Create_Itype : Boolean := False;
13379
13380 begin
13381 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13382 while Present (Old_Constraint) loop
13383 Expr := Node (Old_Constraint);
13384
13385 if Is_Discriminant (Expr) then
13386 Need_To_Create_Itype := True;
13387 end if;
13388
13389 Next_Elmt (Old_Constraint);
13390 end loop;
13391
13392 if Need_To_Create_Itype then
13393 Constr_List := New_List;
13394
13395 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13396 while Present (Old_Constraint) loop
13397 Expr := Node (Old_Constraint);
13398
13399 if Is_Discriminant (Expr) then
13400 Expr := Get_Discr_Value (Expr);
13401 end if;
13402
13403 Append (New_Copy_Tree (Expr), To => Constr_List);
13404
13405 Next_Elmt (Old_Constraint);
13406 end loop;
13407
13408 return Build_Subtype (Old_Type, Constr_List);
13409
13410 else
13411 return Old_Type;
13412 end if;
13413 end Build_Constrained_Discriminated_Type;
13414
13415 -------------------
13416 -- Build_Subtype --
13417 -------------------
13418
13419 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13420 Indic : Node_Id;
13421 Subtyp_Decl : Node_Id;
13422 Def_Id : Entity_Id;
13423 Btyp : Entity_Id := Base_Type (T);
13424
13425 begin
13426 -- The Related_Node better be here or else we won't be able to
13427 -- attach new itypes to a node in the tree.
13428
13429 pragma Assert (Present (Related_Node));
13430
13431 -- If the view of the component's type is incomplete or private
13432 -- with unknown discriminants, then the constraint must be applied
13433 -- to the full type.
13434
13435 if Has_Unknown_Discriminants (Btyp)
13436 and then Present (Underlying_Type (Btyp))
13437 then
13438 Btyp := Underlying_Type (Btyp);
13439 end if;
13440
13441 Indic :=
13442 Make_Subtype_Indication (Loc,
13443 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13444 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13445
13446 Def_Id := Create_Itype (Ekind (T), Related_Node);
13447
13448 Subtyp_Decl :=
13449 Make_Subtype_Declaration (Loc,
13450 Defining_Identifier => Def_Id,
13451 Subtype_Indication => Indic);
13452
13453 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13454
13455 -- Itypes must be analyzed with checks off (see package Itypes)
13456
13457 Analyze (Subtyp_Decl, Suppress => All_Checks);
13458
13459 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13460 Inherit_Predicate_Flags (Def_Id, T);
13461
13462 -- Indicate where the predicate function may be found
13463
13464 if Is_Itype (T) then
13465 if Present (Predicate_Function (Def_Id)) then
13466 null;
13467
13468 elsif Present (Predicate_Function (T)) then
13469 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13470
13471 else
13472 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13473 end if;
13474
13475 elsif No (Predicate_Function (Def_Id)) then
13476 Set_Predicated_Parent (Def_Id, T);
13477 end if;
13478 end if;
13479
13480 return Def_Id;
13481 end Build_Subtype;
13482
13483 ---------------------
13484 -- Get_Discr_Value --
13485 ---------------------
13486
13487 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13488 D : Entity_Id;
13489 E : Elmt_Id;
13490
13491 begin
13492 -- The discriminant may be declared for the type, in which case we
13493 -- find it by iterating over the list of discriminants. If the
13494 -- discriminant is inherited from a parent type, it appears as the
13495 -- corresponding discriminant of the current type. This will be the
13496 -- case when constraining an inherited component whose constraint is
13497 -- given by a discriminant of the parent.
13498
13499 D := First_Discriminant (Typ);
13500 E := First_Elmt (Constraints);
13501
13502 while Present (D) loop
13503 if D = Entity (Discrim)
13504 or else D = CR_Discriminant (Entity (Discrim))
13505 or else Corresponding_Discriminant (D) = Entity (Discrim)
13506 then
13507 return Node (E);
13508 end if;
13509
13510 Next_Discriminant (D);
13511 Next_Elmt (E);
13512 end loop;
13513
13514 -- The Corresponding_Discriminant mechanism is incomplete, because
13515 -- the correspondence between new and old discriminants is not one
13516 -- to one: one new discriminant can constrain several old ones. In
13517 -- that case, scan sequentially the stored_constraint, the list of
13518 -- discriminants of the parents, and the constraints.
13519
13520 -- Previous code checked for the present of the Stored_Constraint
13521 -- list for the derived type, but did not use it at all. Should it
13522 -- be present when the component is a discriminated task type?
13523
13524 if Is_Derived_Type (Typ)
13525 and then Scope (Entity (Discrim)) = Etype (Typ)
13526 then
13527 D := First_Discriminant (Etype (Typ));
13528 E := First_Elmt (Constraints);
13529 while Present (D) loop
13530 if D = Entity (Discrim) then
13531 return Node (E);
13532 end if;
13533
13534 Next_Discriminant (D);
13535 Next_Elmt (E);
13536 end loop;
13537 end if;
13538
13539 -- Something is wrong if we did not find the value
13540
13541 raise Program_Error;
13542 end Get_Discr_Value;
13543
13544 ---------------------
13545 -- Is_Discriminant --
13546 ---------------------
13547
13548 function Is_Discriminant (Expr : Node_Id) return Boolean is
13549 Discrim_Scope : Entity_Id;
13550
13551 begin
13552 if Denotes_Discriminant (Expr) then
13553 Discrim_Scope := Scope (Entity (Expr));
13554
13555 -- Either we have a reference to one of Typ's discriminants,
13556
13557 pragma Assert (Discrim_Scope = Typ
13558
13559 -- or to the discriminants of the parent type, in the case
13560 -- of a derivation of a tagged type with variants.
13561
13562 or else Discrim_Scope = Etype (Typ)
13563 or else Full_View (Discrim_Scope) = Etype (Typ)
13564
13565 -- or same as above for the case where the discriminants
13566 -- were declared in Typ's private view.
13567
13568 or else (Is_Private_Type (Discrim_Scope)
13569 and then Chars (Discrim_Scope) = Chars (Typ))
13570
13571 -- or else we are deriving from the full view and the
13572 -- discriminant is declared in the private entity.
13573
13574 or else (Is_Private_Type (Typ)
13575 and then Chars (Discrim_Scope) = Chars (Typ))
13576
13577 -- Or we are constrained the corresponding record of a
13578 -- synchronized type that completes a private declaration.
13579
13580 or else (Is_Concurrent_Record_Type (Typ)
13581 and then
13582 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13583
13584 -- or we have a class-wide type, in which case make sure the
13585 -- discriminant found belongs to the root type.
13586
13587 or else (Is_Class_Wide_Type (Typ)
13588 and then Etype (Typ) = Discrim_Scope));
13589
13590 return True;
13591 end if;
13592
13593 -- In all other cases we have something wrong
13594
13595 return False;
13596 end Is_Discriminant;
13597
13598 -- Start of processing for Constrain_Component_Type
13599
13600 begin
13601 if Nkind (Parent (Comp)) = N_Component_Declaration
13602 and then Comes_From_Source (Parent (Comp))
13603 and then Comes_From_Source
13604 (Subtype_Indication (Component_Definition (Parent (Comp))))
13605 and then
13606 Is_Entity_Name
13607 (Subtype_Indication (Component_Definition (Parent (Comp))))
13608 then
13609 return Compon_Type;
13610
13611 elsif Is_Array_Type (Compon_Type) then
13612 return Build_Constrained_Array_Type (Compon_Type);
13613
13614 elsif Has_Discriminants (Compon_Type) then
13615 return Build_Constrained_Discriminated_Type (Compon_Type);
13616
13617 elsif Is_Access_Type (Compon_Type) then
13618 return Build_Constrained_Access_Type (Compon_Type);
13619
13620 else
13621 return Compon_Type;
13622 end if;
13623 end Constrain_Component_Type;
13624
13625 --------------------------
13626 -- Constrain_Concurrent --
13627 --------------------------
13628
13629 -- For concurrent types, the associated record value type carries the same
13630 -- discriminants, so when we constrain a concurrent type, we must constrain
13631 -- the corresponding record type as well.
13632
13633 procedure Constrain_Concurrent
13634 (Def_Id : in out Entity_Id;
13635 SI : Node_Id;
13636 Related_Nod : Node_Id;
13637 Related_Id : Entity_Id;
13638 Suffix : Character)
13639 is
13640 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13641 -- case of a private subtype (needed when only doing semantic analysis).
13642
13643 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13644 T_Val : Entity_Id;
13645
13646 begin
13647 if Is_Access_Type (T_Ent) then
13648 T_Ent := Designated_Type (T_Ent);
13649 end if;
13650
13651 T_Val := Corresponding_Record_Type (T_Ent);
13652
13653 if Present (T_Val) then
13654
13655 if No (Def_Id) then
13656 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13657
13658 -- Elaborate itype now, as it may be used in a subsequent
13659 -- synchronized operation in another scope.
13660
13661 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13662 Build_Itype_Reference (Def_Id, Related_Nod);
13663 end if;
13664 end if;
13665
13666 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13667 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13668
13669 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13670 Set_Corresponding_Record_Type (Def_Id,
13671 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13672
13673 else
13674 -- If there is no associated record, expansion is disabled and this
13675 -- is a generic context. Create a subtype in any case, so that
13676 -- semantic analysis can proceed.
13677
13678 if No (Def_Id) then
13679 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13680 end if;
13681
13682 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13683 end if;
13684 end Constrain_Concurrent;
13685
13686 ------------------------------------
13687 -- Constrain_Corresponding_Record --
13688 ------------------------------------
13689
13690 function Constrain_Corresponding_Record
13691 (Prot_Subt : Entity_Id;
13692 Corr_Rec : Entity_Id;
13693 Related_Nod : Node_Id) return Entity_Id
13694 is
13695 T_Sub : constant Entity_Id :=
13696 Create_Itype (E_Record_Subtype,
13697 Related_Nod, Corr_Rec, 'C', Suffix_Index => -1);
13698
13699 begin
13700 Set_Etype (T_Sub, Corr_Rec);
13701 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13702 Set_Is_Constrained (T_Sub, True);
13703 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13704 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13705
13706 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13707 Set_Discriminant_Constraint
13708 (T_Sub, Discriminant_Constraint (Prot_Subt));
13709 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13710 Create_Constrained_Components
13711 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13712 end if;
13713
13714 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13715
13716 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13717 Conditional_Delay (T_Sub, Corr_Rec);
13718
13719 else
13720 -- This is a component subtype: it will be frozen in the context of
13721 -- the enclosing record's init_proc, so that discriminant references
13722 -- are resolved to discriminals. (Note: we used to skip freezing
13723 -- altogether in that case, which caused errors downstream for
13724 -- components of a bit packed array type).
13725
13726 Set_Has_Delayed_Freeze (T_Sub);
13727 end if;
13728
13729 return T_Sub;
13730 end Constrain_Corresponding_Record;
13731
13732 -----------------------
13733 -- Constrain_Decimal --
13734 -----------------------
13735
13736 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13737 T : constant Entity_Id := Entity (Subtype_Mark (S));
13738 C : constant Node_Id := Constraint (S);
13739 Loc : constant Source_Ptr := Sloc (C);
13740 Range_Expr : Node_Id;
13741 Digits_Expr : Node_Id;
13742 Digits_Val : Uint;
13743 Bound_Val : Ureal;
13744
13745 begin
13746 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13747
13748 if Nkind (C) = N_Range_Constraint then
13749 Range_Expr := Range_Expression (C);
13750 Digits_Val := Digits_Value (T);
13751
13752 else
13753 pragma Assert (Nkind (C) = N_Digits_Constraint);
13754
13755 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13756
13757 Digits_Expr := Digits_Expression (C);
13758 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13759
13760 Check_Digits_Expression (Digits_Expr);
13761 Digits_Val := Expr_Value (Digits_Expr);
13762
13763 if Digits_Val > Digits_Value (T) then
13764 Error_Msg_N
13765 ("digits expression is incompatible with subtype", C);
13766 Digits_Val := Digits_Value (T);
13767 end if;
13768
13769 if Present (Range_Constraint (C)) then
13770 Range_Expr := Range_Expression (Range_Constraint (C));
13771 else
13772 Range_Expr := Empty;
13773 end if;
13774 end if;
13775
13776 Set_Etype (Def_Id, Base_Type (T));
13777 Set_Size_Info (Def_Id, (T));
13778 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13779 Set_Delta_Value (Def_Id, Delta_Value (T));
13780 Set_Scale_Value (Def_Id, Scale_Value (T));
13781 Set_Small_Value (Def_Id, Small_Value (T));
13782 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13783 Set_Digits_Value (Def_Id, Digits_Val);
13784
13785 -- Manufacture range from given digits value if no range present
13786
13787 if No (Range_Expr) then
13788 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13789 Range_Expr :=
13790 Make_Range (Loc,
13791 Low_Bound =>
13792 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13793 High_Bound =>
13794 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13795 end if;
13796
13797 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13798 Set_Discrete_RM_Size (Def_Id);
13799
13800 -- Unconditionally delay the freeze, since we cannot set size
13801 -- information in all cases correctly until the freeze point.
13802
13803 Set_Has_Delayed_Freeze (Def_Id);
13804 end Constrain_Decimal;
13805
13806 ----------------------------------
13807 -- Constrain_Discriminated_Type --
13808 ----------------------------------
13809
13810 procedure Constrain_Discriminated_Type
13811 (Def_Id : Entity_Id;
13812 S : Node_Id;
13813 Related_Nod : Node_Id;
13814 For_Access : Boolean := False)
13815 is
13816 E : Entity_Id := Entity (Subtype_Mark (S));
13817 T : Entity_Id;
13818
13819 procedure Fixup_Bad_Constraint;
13820 -- Called after finding a bad constraint, and after having posted an
13821 -- appropriate error message. The goal is to leave type Def_Id in as
13822 -- reasonable state as possible.
13823
13824 --------------------------
13825 -- Fixup_Bad_Constraint --
13826 --------------------------
13827
13828 procedure Fixup_Bad_Constraint is
13829 begin
13830 -- Set a reasonable Ekind for the entity, including incomplete types.
13831
13832 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13833
13834 -- Set Etype to the known type, to reduce chances of cascaded errors
13835
13836 Set_Etype (Def_Id, E);
13837 Set_Error_Posted (Def_Id);
13838 end Fixup_Bad_Constraint;
13839
13840 -- Local variables
13841
13842 C : Node_Id;
13843 Constr : Elist_Id := New_Elmt_List;
13844
13845 -- Start of processing for Constrain_Discriminated_Type
13846
13847 begin
13848 C := Constraint (S);
13849
13850 -- A discriminant constraint is only allowed in a subtype indication,
13851 -- after a subtype mark. This subtype mark must denote either a type
13852 -- with discriminants, or an access type whose designated type is a
13853 -- type with discriminants. A discriminant constraint specifies the
13854 -- values of these discriminants (RM 3.7.2(5)).
13855
13856 T := Base_Type (Entity (Subtype_Mark (S)));
13857
13858 if Is_Access_Type (T) then
13859 T := Designated_Type (T);
13860 end if;
13861
13862 -- In an instance it may be necessary to retrieve the full view of a
13863 -- type with unknown discriminants, or a full view with defaulted
13864 -- discriminants. In other contexts the constraint is illegal.
13865
13866 if In_Instance
13867 and then Is_Private_Type (T)
13868 and then Present (Full_View (T))
13869 and then
13870 (Has_Unknown_Discriminants (T)
13871 or else
13872 (not Has_Discriminants (T)
13873 and then Has_Discriminants (Full_View (T))
13874 and then Present (Discriminant_Default_Value
13875 (First_Discriminant (Full_View (T))))))
13876 then
13877 T := Full_View (T);
13878 E := Full_View (E);
13879 end if;
13880
13881 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13882 -- generating an error for access-to-incomplete subtypes.
13883
13884 if Ada_Version >= Ada_2005
13885 and then Ekind (T) = E_Incomplete_Type
13886 and then Nkind (Parent (S)) = N_Subtype_Declaration
13887 and then not Is_Itype (Def_Id)
13888 then
13889 -- A little sanity check: emit an error message if the type has
13890 -- discriminants to begin with. Type T may be a regular incomplete
13891 -- type or imported via a limited with clause.
13892
13893 if Has_Discriminants (T)
13894 or else (From_Limited_With (T)
13895 and then Present (Non_Limited_View (T))
13896 and then Nkind (Parent (Non_Limited_View (T))) =
13897 N_Full_Type_Declaration
13898 and then Present (Discriminant_Specifications
13899 (Parent (Non_Limited_View (T)))))
13900 then
13901 Error_Msg_N
13902 ("(Ada 2005) incomplete subtype may not be constrained", C);
13903 else
13904 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13905 end if;
13906
13907 Fixup_Bad_Constraint;
13908 return;
13909
13910 -- Check that the type has visible discriminants. The type may be
13911 -- a private type with unknown discriminants whose full view has
13912 -- discriminants which are invisible.
13913
13914 elsif not Has_Discriminants (T)
13915 or else
13916 (Has_Unknown_Discriminants (T)
13917 and then Is_Private_Type (T))
13918 then
13919 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13920 Fixup_Bad_Constraint;
13921 return;
13922
13923 elsif Is_Constrained (E)
13924 or else (Ekind (E) = E_Class_Wide_Subtype
13925 and then Present (Discriminant_Constraint (E)))
13926 then
13927 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13928 Fixup_Bad_Constraint;
13929 return;
13930 end if;
13931
13932 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13933 -- applies to the base type.
13934
13935 T := Base_Type (T);
13936
13937 Constr := Build_Discriminant_Constraints (T, S);
13938
13939 -- If the list returned was empty we had an error in building the
13940 -- discriminant constraint. We have also already signalled an error
13941 -- in the incomplete type case
13942
13943 if Is_Empty_Elmt_List (Constr) then
13944 Fixup_Bad_Constraint;
13945 return;
13946 end if;
13947
13948 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13949 end Constrain_Discriminated_Type;
13950
13951 ---------------------------
13952 -- Constrain_Enumeration --
13953 ---------------------------
13954
13955 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13956 T : constant Entity_Id := Entity (Subtype_Mark (S));
13957 C : constant Node_Id := Constraint (S);
13958
13959 begin
13960 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13961
13962 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13963
13964 Set_Etype (Def_Id, Base_Type (T));
13965 Set_Size_Info (Def_Id, (T));
13966 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13967 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13968
13969 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13970
13971 Set_Discrete_RM_Size (Def_Id);
13972 end Constrain_Enumeration;
13973
13974 ----------------------
13975 -- Constrain_Float --
13976 ----------------------
13977
13978 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13979 T : constant Entity_Id := Entity (Subtype_Mark (S));
13980 C : Node_Id;
13981 D : Node_Id;
13982 Rais : Node_Id;
13983
13984 begin
13985 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13986
13987 Set_Etype (Def_Id, Base_Type (T));
13988 Set_Size_Info (Def_Id, (T));
13989 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13990
13991 -- Process the constraint
13992
13993 C := Constraint (S);
13994
13995 -- Digits constraint present
13996
13997 if Nkind (C) = N_Digits_Constraint then
13998
13999 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
14000 Check_Restriction (No_Obsolescent_Features, C);
14001
14002 if Warn_On_Obsolescent_Feature then
14003 Error_Msg_N
14004 ("subtype digits constraint is an " &
14005 "obsolescent feature (RM J.3(8))?j?", C);
14006 end if;
14007
14008 D := Digits_Expression (C);
14009 Analyze_And_Resolve (D, Any_Integer);
14010 Check_Digits_Expression (D);
14011 Set_Digits_Value (Def_Id, Expr_Value (D));
14012
14013 -- Check that digits value is in range. Obviously we can do this
14014 -- at compile time, but it is strictly a runtime check, and of
14015 -- course there is an ACVC test that checks this.
14016
14017 if Digits_Value (Def_Id) > Digits_Value (T) then
14018 Error_Msg_Uint_1 := Digits_Value (T);
14019 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14020 Rais :=
14021 Make_Raise_Constraint_Error (Sloc (D),
14022 Reason => CE_Range_Check_Failed);
14023 Insert_Action (Declaration_Node (Def_Id), Rais);
14024 end if;
14025
14026 C := Range_Constraint (C);
14027
14028 -- No digits constraint present
14029
14030 else
14031 Set_Digits_Value (Def_Id, Digits_Value (T));
14032 end if;
14033
14034 -- Range constraint present
14035
14036 if Nkind (C) = N_Range_Constraint then
14037 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14038
14039 -- No range constraint present
14040
14041 else
14042 pragma Assert (No (C));
14043 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14044 end if;
14045
14046 Set_Is_Constrained (Def_Id);
14047 end Constrain_Float;
14048
14049 ---------------------
14050 -- Constrain_Index --
14051 ---------------------
14052
14053 procedure Constrain_Index
14054 (Index : Node_Id;
14055 S : Node_Id;
14056 Related_Nod : Node_Id;
14057 Related_Id : Entity_Id;
14058 Suffix : Character;
14059 Suffix_Index : Nat)
14060 is
14061 Def_Id : Entity_Id;
14062 R : Node_Id := Empty;
14063 T : constant Entity_Id := Etype (Index);
14064
14065 begin
14066 Def_Id :=
14067 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14068 Set_Etype (Def_Id, Base_Type (T));
14069
14070 if Nkind (S) = N_Range
14071 or else
14072 (Nkind (S) = N_Attribute_Reference
14073 and then Attribute_Name (S) = Name_Range)
14074 then
14075 -- A Range attribute will be transformed into N_Range by Resolve
14076
14077 Analyze (S);
14078 Set_Etype (S, T);
14079 R := S;
14080
14081 Process_Range_Expr_In_Decl (R, T);
14082
14083 if not Error_Posted (S)
14084 and then
14085 (Nkind (S) /= N_Range
14086 or else not Covers (T, (Etype (Low_Bound (S))))
14087 or else not Covers (T, (Etype (High_Bound (S)))))
14088 then
14089 if Base_Type (T) /= Any_Type
14090 and then Etype (Low_Bound (S)) /= Any_Type
14091 and then Etype (High_Bound (S)) /= Any_Type
14092 then
14093 Error_Msg_N ("range expected", S);
14094 end if;
14095 end if;
14096
14097 elsif Nkind (S) = N_Subtype_Indication then
14098
14099 -- The parser has verified that this is a discrete indication
14100
14101 Resolve_Discrete_Subtype_Indication (S, T);
14102 Bad_Predicated_Subtype_Use
14103 ("subtype& has predicate, not allowed in index constraint",
14104 S, Entity (Subtype_Mark (S)));
14105
14106 R := Range_Expression (Constraint (S));
14107
14108 -- Capture values of bounds and generate temporaries for them if
14109 -- needed, since checks may cause duplication of the expressions
14110 -- which must not be reevaluated.
14111
14112 -- The forced evaluation removes side effects from expressions, which
14113 -- should occur also in GNATprove mode. Otherwise, we end up with
14114 -- unexpected insertions of actions at places where this is not
14115 -- supposed to occur, e.g. on default parameters of a call.
14116
14117 if Expander_Active or GNATprove_Mode then
14118 Force_Evaluation
14119 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14120 Force_Evaluation
14121 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14122 end if;
14123
14124 elsif Nkind (S) = N_Discriminant_Association then
14125
14126 -- Syntactically valid in subtype indication
14127
14128 Error_Msg_N ("invalid index constraint", S);
14129 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14130 return;
14131
14132 -- Subtype_Mark case, no anonymous subtypes to construct
14133
14134 else
14135 Analyze (S);
14136
14137 if Is_Entity_Name (S) then
14138 if not Is_Type (Entity (S)) then
14139 Error_Msg_N ("expect subtype mark for index constraint", S);
14140
14141 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14142 Wrong_Type (S, Base_Type (T));
14143
14144 -- Check error of subtype with predicate in index constraint
14145
14146 else
14147 Bad_Predicated_Subtype_Use
14148 ("subtype& has predicate, not allowed in index constraint",
14149 S, Entity (S));
14150 end if;
14151
14152 return;
14153
14154 else
14155 Error_Msg_N ("invalid index constraint", S);
14156 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14157 return;
14158 end if;
14159 end if;
14160
14161 -- Complete construction of the Itype
14162
14163 if Is_Modular_Integer_Type (T) then
14164 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14165
14166 elsif Is_Integer_Type (T) then
14167 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14168
14169 else
14170 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14171 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14172 Set_First_Literal (Def_Id, First_Literal (T));
14173 end if;
14174
14175 Set_Size_Info (Def_Id, (T));
14176 Set_RM_Size (Def_Id, RM_Size (T));
14177 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14178
14179 Set_Scalar_Range (Def_Id, R);
14180
14181 Set_Etype (S, Def_Id);
14182 Set_Discrete_RM_Size (Def_Id);
14183 end Constrain_Index;
14184
14185 -----------------------
14186 -- Constrain_Integer --
14187 -----------------------
14188
14189 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14190 T : constant Entity_Id := Entity (Subtype_Mark (S));
14191 C : constant Node_Id := Constraint (S);
14192
14193 begin
14194 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14195
14196 if Is_Modular_Integer_Type (T) then
14197 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14198 else
14199 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14200 end if;
14201
14202 Set_Etype (Def_Id, Base_Type (T));
14203 Set_Size_Info (Def_Id, (T));
14204 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14205 Set_Discrete_RM_Size (Def_Id);
14206 end Constrain_Integer;
14207
14208 ------------------------------
14209 -- Constrain_Ordinary_Fixed --
14210 ------------------------------
14211
14212 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14213 T : constant Entity_Id := Entity (Subtype_Mark (S));
14214 C : Node_Id;
14215 D : Node_Id;
14216 Rais : Node_Id;
14217
14218 begin
14219 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14220 Set_Etype (Def_Id, Base_Type (T));
14221 Set_Size_Info (Def_Id, (T));
14222 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14223 Set_Small_Value (Def_Id, Small_Value (T));
14224
14225 -- Process the constraint
14226
14227 C := Constraint (S);
14228
14229 -- Delta constraint present
14230
14231 if Nkind (C) = N_Delta_Constraint then
14232
14233 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14234 Check_Restriction (No_Obsolescent_Features, C);
14235
14236 if Warn_On_Obsolescent_Feature then
14237 Error_Msg_S
14238 ("subtype delta constraint is an " &
14239 "obsolescent feature (RM J.3(7))?j?");
14240 end if;
14241
14242 D := Delta_Expression (C);
14243 Analyze_And_Resolve (D, Any_Real);
14244 Check_Delta_Expression (D);
14245 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14246
14247 -- Check that delta value is in range. Obviously we can do this
14248 -- at compile time, but it is strictly a runtime check, and of
14249 -- course there is an ACVC test that checks this.
14250
14251 if Delta_Value (Def_Id) < Delta_Value (T) then
14252 Error_Msg_N ("??delta value is too small", D);
14253 Rais :=
14254 Make_Raise_Constraint_Error (Sloc (D),
14255 Reason => CE_Range_Check_Failed);
14256 Insert_Action (Declaration_Node (Def_Id), Rais);
14257 end if;
14258
14259 C := Range_Constraint (C);
14260
14261 -- No delta constraint present
14262
14263 else
14264 Set_Delta_Value (Def_Id, Delta_Value (T));
14265 end if;
14266
14267 -- Range constraint present
14268
14269 if Nkind (C) = N_Range_Constraint then
14270 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14271
14272 -- No range constraint present
14273
14274 else
14275 pragma Assert (No (C));
14276 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14277 end if;
14278
14279 Set_Discrete_RM_Size (Def_Id);
14280
14281 -- Unconditionally delay the freeze, since we cannot set size
14282 -- information in all cases correctly until the freeze point.
14283
14284 Set_Has_Delayed_Freeze (Def_Id);
14285 end Constrain_Ordinary_Fixed;
14286
14287 -----------------------
14288 -- Contain_Interface --
14289 -----------------------
14290
14291 function Contain_Interface
14292 (Iface : Entity_Id;
14293 Ifaces : Elist_Id) return Boolean
14294 is
14295 Iface_Elmt : Elmt_Id;
14296
14297 begin
14298 if Present (Ifaces) then
14299 Iface_Elmt := First_Elmt (Ifaces);
14300 while Present (Iface_Elmt) loop
14301 if Node (Iface_Elmt) = Iface then
14302 return True;
14303 end if;
14304
14305 Next_Elmt (Iface_Elmt);
14306 end loop;
14307 end if;
14308
14309 return False;
14310 end Contain_Interface;
14311
14312 ---------------------------
14313 -- Convert_Scalar_Bounds --
14314 ---------------------------
14315
14316 procedure Convert_Scalar_Bounds
14317 (N : Node_Id;
14318 Parent_Type : Entity_Id;
14319 Derived_Type : Entity_Id;
14320 Loc : Source_Ptr)
14321 is
14322 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14323
14324 Lo : Node_Id;
14325 Hi : Node_Id;
14326 Rng : Node_Id;
14327
14328 begin
14329 -- Defend against previous errors
14330
14331 if No (Scalar_Range (Derived_Type)) then
14332 Check_Error_Detected;
14333 return;
14334 end if;
14335
14336 Lo := Build_Scalar_Bound
14337 (Type_Low_Bound (Derived_Type),
14338 Parent_Type, Implicit_Base);
14339
14340 Hi := Build_Scalar_Bound
14341 (Type_High_Bound (Derived_Type),
14342 Parent_Type, Implicit_Base);
14343
14344 Rng :=
14345 Make_Range (Loc,
14346 Low_Bound => Lo,
14347 High_Bound => Hi);
14348
14349 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14350
14351 Set_Parent (Rng, N);
14352 Set_Scalar_Range (Derived_Type, Rng);
14353
14354 -- Analyze the bounds
14355
14356 Analyze_And_Resolve (Lo, Implicit_Base);
14357 Analyze_And_Resolve (Hi, Implicit_Base);
14358
14359 -- Analyze the range itself, except that we do not analyze it if
14360 -- the bounds are real literals, and we have a fixed-point type.
14361 -- The reason for this is that we delay setting the bounds in this
14362 -- case till we know the final Small and Size values (see circuit
14363 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14364
14365 if Is_Fixed_Point_Type (Parent_Type)
14366 and then Nkind (Lo) = N_Real_Literal
14367 and then Nkind (Hi) = N_Real_Literal
14368 then
14369 return;
14370
14371 -- Here we do the analysis of the range
14372
14373 -- Note: we do this manually, since if we do a normal Analyze and
14374 -- Resolve call, there are problems with the conversions used for
14375 -- the derived type range.
14376
14377 else
14378 Set_Etype (Rng, Implicit_Base);
14379 Set_Analyzed (Rng, True);
14380 end if;
14381 end Convert_Scalar_Bounds;
14382
14383 -------------------
14384 -- Copy_And_Swap --
14385 -------------------
14386
14387 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14388 begin
14389 -- Initialize new full declaration entity by copying the pertinent
14390 -- fields of the corresponding private declaration entity.
14391
14392 -- We temporarily set Ekind to a value appropriate for a type to
14393 -- avoid assert failures in Einfo from checking for setting type
14394 -- attributes on something that is not a type. Ekind (Priv) is an
14395 -- appropriate choice, since it allowed the attributes to be set
14396 -- in the first place. This Ekind value will be modified later.
14397
14398 Set_Ekind (Full, Ekind (Priv));
14399
14400 -- Also set Etype temporarily to Any_Type, again, in the absence
14401 -- of errors, it will be properly reset, and if there are errors,
14402 -- then we want a value of Any_Type to remain.
14403
14404 Set_Etype (Full, Any_Type);
14405
14406 -- Now start copying attributes
14407
14408 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14409
14410 if Has_Discriminants (Full) then
14411 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14412 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14413 end if;
14414
14415 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14416 Set_Homonym (Full, Homonym (Priv));
14417 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14418 Set_Is_Public (Full, Is_Public (Priv));
14419 Set_Is_Pure (Full, Is_Pure (Priv));
14420 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14421 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14422 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14423 Set_Has_Pragma_Unreferenced_Objects
14424 (Full, Has_Pragma_Unreferenced_Objects
14425 (Priv));
14426
14427 Conditional_Delay (Full, Priv);
14428
14429 if Is_Tagged_Type (Full) then
14430 Set_Direct_Primitive_Operations
14431 (Full, Direct_Primitive_Operations (Priv));
14432 Set_No_Tagged_Streams_Pragma
14433 (Full, No_Tagged_Streams_Pragma (Priv));
14434
14435 if Is_Base_Type (Priv) then
14436 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14437 end if;
14438 end if;
14439
14440 Set_Is_Volatile (Full, Is_Volatile (Priv));
14441 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14442 Set_Scope (Full, Scope (Priv));
14443 Set_Prev_Entity (Full, Prev_Entity (Priv));
14444 Set_Next_Entity (Full, Next_Entity (Priv));
14445 Set_First_Entity (Full, First_Entity (Priv));
14446 Set_Last_Entity (Full, Last_Entity (Priv));
14447
14448 -- If access types have been recorded for later handling, keep them in
14449 -- the full view so that they get handled when the full view freeze
14450 -- node is expanded.
14451
14452 if Present (Freeze_Node (Priv))
14453 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14454 then
14455 Ensure_Freeze_Node (Full);
14456 Set_Access_Types_To_Process
14457 (Freeze_Node (Full),
14458 Access_Types_To_Process (Freeze_Node (Priv)));
14459 end if;
14460
14461 -- Swap the two entities. Now Private is the full type entity and Full
14462 -- is the private one. They will be swapped back at the end of the
14463 -- private part. This swapping ensures that the entity that is visible
14464 -- in the private part is the full declaration.
14465
14466 Exchange_Entities (Priv, Full);
14467 Append_Entity (Full, Scope (Full));
14468 end Copy_And_Swap;
14469
14470 -------------------------------------
14471 -- Copy_Array_Base_Type_Attributes --
14472 -------------------------------------
14473
14474 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14475 begin
14476 Set_Component_Alignment (T1, Component_Alignment (T2));
14477 Set_Component_Type (T1, Component_Type (T2));
14478 Set_Component_Size (T1, Component_Size (T2));
14479 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14480 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14481 Propagate_Concurrent_Flags (T1, T2);
14482 Set_Is_Packed (T1, Is_Packed (T2));
14483 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14484 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14485 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14486 end Copy_Array_Base_Type_Attributes;
14487
14488 -----------------------------------
14489 -- Copy_Array_Subtype_Attributes --
14490 -----------------------------------
14491
14492 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14493 begin
14494 Set_Size_Info (T1, T2);
14495
14496 Set_First_Index (T1, First_Index (T2));
14497 Set_Is_Aliased (T1, Is_Aliased (T2));
14498 Set_Is_Volatile (T1, Is_Volatile (T2));
14499 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14500 Set_Is_Constrained (T1, Is_Constrained (T2));
14501 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14502 Inherit_Rep_Item_Chain (T1, T2);
14503 Set_Convention (T1, Convention (T2));
14504 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14505 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14506 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14507 end Copy_Array_Subtype_Attributes;
14508
14509 -----------------------------------
14510 -- Create_Constrained_Components --
14511 -----------------------------------
14512
14513 procedure Create_Constrained_Components
14514 (Subt : Entity_Id;
14515 Decl_Node : Node_Id;
14516 Typ : Entity_Id;
14517 Constraints : Elist_Id)
14518 is
14519 Loc : constant Source_Ptr := Sloc (Subt);
14520 Comp_List : constant Elist_Id := New_Elmt_List;
14521 Parent_Type : constant Entity_Id := Etype (Typ);
14522 Assoc_List : constant List_Id := New_List;
14523 Discr_Val : Elmt_Id;
14524 Errors : Boolean;
14525 New_C : Entity_Id;
14526 Old_C : Entity_Id;
14527 Is_Static : Boolean := True;
14528
14529 procedure Collect_Fixed_Components (Typ : Entity_Id);
14530 -- Collect parent type components that do not appear in a variant part
14531
14532 procedure Create_All_Components;
14533 -- Iterate over Comp_List to create the components of the subtype
14534
14535 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14536 -- Creates a new component from Old_Compon, copying all the fields from
14537 -- it, including its Etype, inserts the new component in the Subt entity
14538 -- chain and returns the new component.
14539
14540 function Is_Variant_Record (T : Entity_Id) return Boolean;
14541 -- If true, and discriminants are static, collect only components from
14542 -- variants selected by discriminant values.
14543
14544 ------------------------------
14545 -- Collect_Fixed_Components --
14546 ------------------------------
14547
14548 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14549 begin
14550 -- Build association list for discriminants, and find components of the
14551 -- variant part selected by the values of the discriminants.
14552
14553 Old_C := First_Discriminant (Typ);
14554 Discr_Val := First_Elmt (Constraints);
14555 while Present (Old_C) loop
14556 Append_To (Assoc_List,
14557 Make_Component_Association (Loc,
14558 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14559 Expression => New_Copy (Node (Discr_Val))));
14560
14561 Next_Elmt (Discr_Val);
14562 Next_Discriminant (Old_C);
14563 end loop;
14564
14565 -- The tag and the possible parent component are unconditionally in
14566 -- the subtype.
14567
14568 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14569 Old_C := First_Component (Typ);
14570 while Present (Old_C) loop
14571 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14572 Append_Elmt (Old_C, Comp_List);
14573 end if;
14574
14575 Next_Component (Old_C);
14576 end loop;
14577 end if;
14578 end Collect_Fixed_Components;
14579
14580 ---------------------------
14581 -- Create_All_Components --
14582 ---------------------------
14583
14584 procedure Create_All_Components is
14585 Comp : Elmt_Id;
14586
14587 begin
14588 Comp := First_Elmt (Comp_List);
14589 while Present (Comp) loop
14590 Old_C := Node (Comp);
14591 New_C := Create_Component (Old_C);
14592
14593 Set_Etype
14594 (New_C,
14595 Constrain_Component_Type
14596 (Old_C, Subt, Decl_Node, Typ, Constraints));
14597 Set_Is_Public (New_C, Is_Public (Subt));
14598
14599 Next_Elmt (Comp);
14600 end loop;
14601 end Create_All_Components;
14602
14603 ----------------------
14604 -- Create_Component --
14605 ----------------------
14606
14607 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14608 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14609
14610 begin
14611 if Ekind (Old_Compon) = E_Discriminant
14612 and then Is_Completely_Hidden (Old_Compon)
14613 then
14614 -- This is a shadow discriminant created for a discriminant of
14615 -- the parent type, which needs to be present in the subtype.
14616 -- Give the shadow discriminant an internal name that cannot
14617 -- conflict with that of visible components.
14618
14619 Set_Chars (New_Compon, New_Internal_Name ('C'));
14620 end if;
14621
14622 -- Set the parent so we have a proper link for freezing etc. This is
14623 -- not a real parent pointer, since of course our parent does not own
14624 -- up to us and reference us, we are an illegitimate child of the
14625 -- original parent.
14626
14627 Set_Parent (New_Compon, Parent (Old_Compon));
14628
14629 -- We do not want this node marked as Comes_From_Source, since
14630 -- otherwise it would get first class status and a separate cross-
14631 -- reference line would be generated. Illegitimate children do not
14632 -- rate such recognition.
14633
14634 Set_Comes_From_Source (New_Compon, False);
14635
14636 -- But it is a real entity, and a birth certificate must be properly
14637 -- registered by entering it into the entity list, and setting its
14638 -- scope to the given subtype. This turns out to be useful for the
14639 -- LLVM code generator, but that scope is not used otherwise.
14640
14641 Enter_Name (New_Compon);
14642 Set_Scope (New_Compon, Subt);
14643
14644 return New_Compon;
14645 end Create_Component;
14646
14647 -----------------------
14648 -- Is_Variant_Record --
14649 -----------------------
14650
14651 function Is_Variant_Record (T : Entity_Id) return Boolean is
14652 begin
14653 return Nkind (Parent (T)) = N_Full_Type_Declaration
14654 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14655 and then Present (Component_List (Type_Definition (Parent (T))))
14656 and then
14657 Present
14658 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14659 end Is_Variant_Record;
14660
14661 -- Start of processing for Create_Constrained_Components
14662
14663 begin
14664 pragma Assert (Subt /= Base_Type (Subt));
14665 pragma Assert (Typ = Base_Type (Typ));
14666
14667 Set_First_Entity (Subt, Empty);
14668 Set_Last_Entity (Subt, Empty);
14669
14670 -- Check whether constraint is fully static, in which case we can
14671 -- optimize the list of components.
14672
14673 Discr_Val := First_Elmt (Constraints);
14674 while Present (Discr_Val) loop
14675 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14676 Is_Static := False;
14677 exit;
14678 end if;
14679
14680 Next_Elmt (Discr_Val);
14681 end loop;
14682
14683 Set_Has_Static_Discriminants (Subt, Is_Static);
14684
14685 Push_Scope (Subt);
14686
14687 -- Inherit the discriminants of the parent type
14688
14689 Add_Discriminants : declare
14690 Num_Disc : Nat;
14691 Num_Gird : Nat;
14692
14693 begin
14694 Num_Disc := 0;
14695 Old_C := First_Discriminant (Typ);
14696
14697 while Present (Old_C) loop
14698 Num_Disc := Num_Disc + 1;
14699 New_C := Create_Component (Old_C);
14700 Set_Is_Public (New_C, Is_Public (Subt));
14701 Next_Discriminant (Old_C);
14702 end loop;
14703
14704 -- For an untagged derived subtype, the number of discriminants may
14705 -- be smaller than the number of inherited discriminants, because
14706 -- several of them may be renamed by a single new discriminant or
14707 -- constrained. In this case, add the hidden discriminants back into
14708 -- the subtype, because they need to be present if the optimizer of
14709 -- the GCC 4.x back-end decides to break apart assignments between
14710 -- objects using the parent view into member-wise assignments.
14711
14712 Num_Gird := 0;
14713
14714 if Is_Derived_Type (Typ)
14715 and then not Is_Tagged_Type (Typ)
14716 then
14717 Old_C := First_Stored_Discriminant (Typ);
14718
14719 while Present (Old_C) loop
14720 Num_Gird := Num_Gird + 1;
14721 Next_Stored_Discriminant (Old_C);
14722 end loop;
14723 end if;
14724
14725 if Num_Gird > Num_Disc then
14726
14727 -- Find out multiple uses of new discriminants, and add hidden
14728 -- components for the extra renamed discriminants. We recognize
14729 -- multiple uses through the Corresponding_Discriminant of a
14730 -- new discriminant: if it constrains several old discriminants,
14731 -- this field points to the last one in the parent type. The
14732 -- stored discriminants of the derived type have the same name
14733 -- as those of the parent.
14734
14735 declare
14736 Constr : Elmt_Id;
14737 New_Discr : Entity_Id;
14738 Old_Discr : Entity_Id;
14739
14740 begin
14741 Constr := First_Elmt (Stored_Constraint (Typ));
14742 Old_Discr := First_Stored_Discriminant (Typ);
14743 while Present (Constr) loop
14744 if Is_Entity_Name (Node (Constr))
14745 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14746 then
14747 New_Discr := Entity (Node (Constr));
14748
14749 if Chars (Corresponding_Discriminant (New_Discr)) /=
14750 Chars (Old_Discr)
14751 then
14752 -- The new discriminant has been used to rename a
14753 -- subsequent old discriminant. Introduce a shadow
14754 -- component for the current old discriminant.
14755
14756 New_C := Create_Component (Old_Discr);
14757 Set_Original_Record_Component (New_C, Old_Discr);
14758 end if;
14759
14760 else
14761 -- The constraint has eliminated the old discriminant.
14762 -- Introduce a shadow component.
14763
14764 New_C := Create_Component (Old_Discr);
14765 Set_Original_Record_Component (New_C, Old_Discr);
14766 end if;
14767
14768 Next_Elmt (Constr);
14769 Next_Stored_Discriminant (Old_Discr);
14770 end loop;
14771 end;
14772 end if;
14773 end Add_Discriminants;
14774
14775 if Is_Static
14776 and then Is_Variant_Record (Typ)
14777 then
14778 Collect_Fixed_Components (Typ);
14779
14780 Gather_Components (
14781 Typ,
14782 Component_List (Type_Definition (Parent (Typ))),
14783 Governed_By => Assoc_List,
14784 Into => Comp_List,
14785 Report_Errors => Errors);
14786 pragma Assert (not Errors
14787 or else Serious_Errors_Detected > 0);
14788
14789 Create_All_Components;
14790
14791 -- If the subtype declaration is created for a tagged type derivation
14792 -- with constraints, we retrieve the record definition of the parent
14793 -- type to select the components of the proper variant.
14794
14795 elsif Is_Static
14796 and then Is_Tagged_Type (Typ)
14797 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14798 and then
14799 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14800 and then Is_Variant_Record (Parent_Type)
14801 then
14802 Collect_Fixed_Components (Typ);
14803
14804 Gather_Components
14805 (Typ,
14806 Component_List (Type_Definition (Parent (Parent_Type))),
14807 Governed_By => Assoc_List,
14808 Into => Comp_List,
14809 Report_Errors => Errors);
14810
14811 -- Note: previously there was a check at this point that no errors
14812 -- were detected. As a consequence of AI05-220 there may be an error
14813 -- if an inherited discriminant that controls a variant has a non-
14814 -- static constraint.
14815
14816 -- If the tagged derivation has a type extension, collect all the
14817 -- new components therein.
14818
14819 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14820 then
14821 Old_C := First_Component (Typ);
14822 while Present (Old_C) loop
14823 if Original_Record_Component (Old_C) = Old_C
14824 and then Chars (Old_C) /= Name_uTag
14825 and then Chars (Old_C) /= Name_uParent
14826 then
14827 Append_Elmt (Old_C, Comp_List);
14828 end if;
14829
14830 Next_Component (Old_C);
14831 end loop;
14832 end if;
14833
14834 Create_All_Components;
14835
14836 else
14837 -- If discriminants are not static, or if this is a multi-level type
14838 -- extension, we have to include all components of the parent type.
14839
14840 Old_C := First_Component (Typ);
14841 while Present (Old_C) loop
14842 New_C := Create_Component (Old_C);
14843
14844 Set_Etype
14845 (New_C,
14846 Constrain_Component_Type
14847 (Old_C, Subt, Decl_Node, Typ, Constraints));
14848 Set_Is_Public (New_C, Is_Public (Subt));
14849
14850 Next_Component (Old_C);
14851 end loop;
14852 end if;
14853
14854 End_Scope;
14855 end Create_Constrained_Components;
14856
14857 ------------------------------------------
14858 -- Decimal_Fixed_Point_Type_Declaration --
14859 ------------------------------------------
14860
14861 procedure Decimal_Fixed_Point_Type_Declaration
14862 (T : Entity_Id;
14863 Def : Node_Id)
14864 is
14865 Loc : constant Source_Ptr := Sloc (Def);
14866 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14867 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14868 Implicit_Base : Entity_Id;
14869 Digs_Val : Uint;
14870 Delta_Val : Ureal;
14871 Scale_Val : Uint;
14872 Bound_Val : Ureal;
14873
14874 begin
14875 Check_SPARK_05_Restriction
14876 ("decimal fixed point type is not allowed", Def);
14877 Check_Restriction (No_Fixed_Point, Def);
14878
14879 -- Create implicit base type
14880
14881 Implicit_Base :=
14882 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14883 Set_Etype (Implicit_Base, Implicit_Base);
14884
14885 -- Analyze and process delta expression
14886
14887 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14888
14889 Check_Delta_Expression (Delta_Expr);
14890 Delta_Val := Expr_Value_R (Delta_Expr);
14891
14892 -- Check delta is power of 10, and determine scale value from it
14893
14894 declare
14895 Val : Ureal;
14896
14897 begin
14898 Scale_Val := Uint_0;
14899 Val := Delta_Val;
14900
14901 if Val < Ureal_1 then
14902 while Val < Ureal_1 loop
14903 Val := Val * Ureal_10;
14904 Scale_Val := Scale_Val + 1;
14905 end loop;
14906
14907 if Scale_Val > 18 then
14908 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14909 Scale_Val := UI_From_Int (+18);
14910 end if;
14911
14912 else
14913 while Val > Ureal_1 loop
14914 Val := Val / Ureal_10;
14915 Scale_Val := Scale_Val - 1;
14916 end loop;
14917
14918 if Scale_Val < -18 then
14919 Error_Msg_N ("scale is less than minimum value of -18", Def);
14920 Scale_Val := UI_From_Int (-18);
14921 end if;
14922 end if;
14923
14924 if Val /= Ureal_1 then
14925 Error_Msg_N ("delta expression must be a power of 10", Def);
14926 Delta_Val := Ureal_10 ** (-Scale_Val);
14927 end if;
14928 end;
14929
14930 -- Set delta, scale and small (small = delta for decimal type)
14931
14932 Set_Delta_Value (Implicit_Base, Delta_Val);
14933 Set_Scale_Value (Implicit_Base, Scale_Val);
14934 Set_Small_Value (Implicit_Base, Delta_Val);
14935
14936 -- Analyze and process digits expression
14937
14938 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14939 Check_Digits_Expression (Digs_Expr);
14940 Digs_Val := Expr_Value (Digs_Expr);
14941
14942 if Digs_Val > 18 then
14943 Digs_Val := UI_From_Int (+18);
14944 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14945 end if;
14946
14947 Set_Digits_Value (Implicit_Base, Digs_Val);
14948 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14949
14950 -- Set range of base type from digits value for now. This will be
14951 -- expanded to represent the true underlying base range by Freeze.
14952
14953 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14954
14955 -- Note: We leave size as zero for now, size will be set at freeze
14956 -- time. We have to do this for ordinary fixed-point, because the size
14957 -- depends on the specified small, and we might as well do the same for
14958 -- decimal fixed-point.
14959
14960 pragma Assert (Esize (Implicit_Base) = Uint_0);
14961
14962 -- If there are bounds given in the declaration use them as the
14963 -- bounds of the first named subtype.
14964
14965 if Present (Real_Range_Specification (Def)) then
14966 declare
14967 RRS : constant Node_Id := Real_Range_Specification (Def);
14968 Low : constant Node_Id := Low_Bound (RRS);
14969 High : constant Node_Id := High_Bound (RRS);
14970 Low_Val : Ureal;
14971 High_Val : Ureal;
14972
14973 begin
14974 Analyze_And_Resolve (Low, Any_Real);
14975 Analyze_And_Resolve (High, Any_Real);
14976 Check_Real_Bound (Low);
14977 Check_Real_Bound (High);
14978 Low_Val := Expr_Value_R (Low);
14979 High_Val := Expr_Value_R (High);
14980
14981 if Low_Val < (-Bound_Val) then
14982 Error_Msg_N
14983 ("range low bound too small for digits value", Low);
14984 Low_Val := -Bound_Val;
14985 end if;
14986
14987 if High_Val > Bound_Val then
14988 Error_Msg_N
14989 ("range high bound too large for digits value", High);
14990 High_Val := Bound_Val;
14991 end if;
14992
14993 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14994 end;
14995
14996 -- If no explicit range, use range that corresponds to given
14997 -- digits value. This will end up as the final range for the
14998 -- first subtype.
14999
15000 else
15001 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15002 end if;
15003
15004 -- Complete entity for first subtype. The inheritance of the rep item
15005 -- chain ensures that SPARK-related pragmas are not clobbered when the
15006 -- decimal fixed point type acts as a full view of a private type.
15007
15008 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15009 Set_Etype (T, Implicit_Base);
15010 Set_Size_Info (T, Implicit_Base);
15011 Inherit_Rep_Item_Chain (T, Implicit_Base);
15012 Set_Digits_Value (T, Digs_Val);
15013 Set_Delta_Value (T, Delta_Val);
15014 Set_Small_Value (T, Delta_Val);
15015 Set_Scale_Value (T, Scale_Val);
15016 Set_Is_Constrained (T);
15017 end Decimal_Fixed_Point_Type_Declaration;
15018
15019 -----------------------------------
15020 -- Derive_Progenitor_Subprograms --
15021 -----------------------------------
15022
15023 procedure Derive_Progenitor_Subprograms
15024 (Parent_Type : Entity_Id;
15025 Tagged_Type : Entity_Id)
15026 is
15027 E : Entity_Id;
15028 Elmt : Elmt_Id;
15029 Iface : Entity_Id;
15030 Iface_Alias : Entity_Id;
15031 Iface_Elmt : Elmt_Id;
15032 Iface_Subp : Entity_Id;
15033 New_Subp : Entity_Id := Empty;
15034 Prim_Elmt : Elmt_Id;
15035 Subp : Entity_Id;
15036 Typ : Entity_Id;
15037
15038 begin
15039 pragma Assert (Ada_Version >= Ada_2005
15040 and then Is_Record_Type (Tagged_Type)
15041 and then Is_Tagged_Type (Tagged_Type)
15042 and then Has_Interfaces (Tagged_Type));
15043
15044 -- Step 1: Transfer to the full-view primitives associated with the
15045 -- partial-view that cover interface primitives. Conceptually this
15046 -- work should be done later by Process_Full_View; done here to
15047 -- simplify its implementation at later stages. It can be safely
15048 -- done here because interfaces must be visible in the partial and
15049 -- private view (RM 7.3(7.3/2)).
15050
15051 -- Small optimization: This work is only required if the parent may
15052 -- have entities whose Alias attribute reference an interface primitive.
15053 -- Such a situation may occur if the parent is an abstract type and the
15054 -- primitive has not been yet overridden or if the parent is a generic
15055 -- formal type covering interfaces.
15056
15057 -- If the tagged type is not abstract, it cannot have abstract
15058 -- primitives (the only entities in the list of primitives of
15059 -- non-abstract tagged types that can reference abstract primitives
15060 -- through its Alias attribute are the internal entities that have
15061 -- attribute Interface_Alias, and these entities are generated later
15062 -- by Add_Internal_Interface_Entities).
15063
15064 if In_Private_Part (Current_Scope)
15065 and then (Is_Abstract_Type (Parent_Type)
15066 or else
15067 Is_Generic_Type (Parent_Type))
15068 then
15069 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15070 while Present (Elmt) loop
15071 Subp := Node (Elmt);
15072
15073 -- At this stage it is not possible to have entities in the list
15074 -- of primitives that have attribute Interface_Alias.
15075
15076 pragma Assert (No (Interface_Alias (Subp)));
15077
15078 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15079
15080 if Is_Interface (Typ) then
15081 E := Find_Primitive_Covering_Interface
15082 (Tagged_Type => Tagged_Type,
15083 Iface_Prim => Subp);
15084
15085 if Present (E)
15086 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15087 then
15088 Replace_Elmt (Elmt, E);
15089 Remove_Homonym (Subp);
15090 end if;
15091 end if;
15092
15093 Next_Elmt (Elmt);
15094 end loop;
15095 end if;
15096
15097 -- Step 2: Add primitives of progenitors that are not implemented by
15098 -- parents of Tagged_Type.
15099
15100 if Present (Interfaces (Base_Type (Tagged_Type))) then
15101 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15102 while Present (Iface_Elmt) loop
15103 Iface := Node (Iface_Elmt);
15104
15105 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15106 while Present (Prim_Elmt) loop
15107 Iface_Subp := Node (Prim_Elmt);
15108 Iface_Alias := Ultimate_Alias (Iface_Subp);
15109
15110 -- Exclude derivation of predefined primitives except those
15111 -- that come from source, or are inherited from one that comes
15112 -- from source. Required to catch declarations of equality
15113 -- operators of interfaces. For example:
15114
15115 -- type Iface is interface;
15116 -- function "=" (Left, Right : Iface) return Boolean;
15117
15118 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15119 or else Comes_From_Source (Iface_Alias)
15120 then
15121 E :=
15122 Find_Primitive_Covering_Interface
15123 (Tagged_Type => Tagged_Type,
15124 Iface_Prim => Iface_Subp);
15125
15126 -- If not found we derive a new primitive leaving its alias
15127 -- attribute referencing the interface primitive.
15128
15129 if No (E) then
15130 Derive_Subprogram
15131 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15132
15133 -- Ada 2012 (AI05-0197): If the covering primitive's name
15134 -- differs from the name of the interface primitive then it
15135 -- is a private primitive inherited from a parent type. In
15136 -- such case, given that Tagged_Type covers the interface,
15137 -- the inherited private primitive becomes visible. For such
15138 -- purpose we add a new entity that renames the inherited
15139 -- private primitive.
15140
15141 elsif Chars (E) /= Chars (Iface_Subp) then
15142 pragma Assert (Has_Suffix (E, 'P'));
15143 Derive_Subprogram
15144 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15145 Set_Alias (New_Subp, E);
15146 Set_Is_Abstract_Subprogram (New_Subp,
15147 Is_Abstract_Subprogram (E));
15148
15149 -- Propagate to the full view interface entities associated
15150 -- with the partial view.
15151
15152 elsif In_Private_Part (Current_Scope)
15153 and then Present (Alias (E))
15154 and then Alias (E) = Iface_Subp
15155 and then
15156 List_Containing (Parent (E)) /=
15157 Private_Declarations
15158 (Specification
15159 (Unit_Declaration_Node (Current_Scope)))
15160 then
15161 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15162 end if;
15163 end if;
15164
15165 Next_Elmt (Prim_Elmt);
15166 end loop;
15167
15168 Next_Elmt (Iface_Elmt);
15169 end loop;
15170 end if;
15171 end Derive_Progenitor_Subprograms;
15172
15173 -----------------------
15174 -- Derive_Subprogram --
15175 -----------------------
15176
15177 procedure Derive_Subprogram
15178 (New_Subp : out Entity_Id;
15179 Parent_Subp : Entity_Id;
15180 Derived_Type : Entity_Id;
15181 Parent_Type : Entity_Id;
15182 Actual_Subp : Entity_Id := Empty)
15183 is
15184 Formal : Entity_Id;
15185 -- Formal parameter of parent primitive operation
15186
15187 Formal_Of_Actual : Entity_Id;
15188 -- Formal parameter of actual operation, when the derivation is to
15189 -- create a renaming for a primitive operation of an actual in an
15190 -- instantiation.
15191
15192 New_Formal : Entity_Id;
15193 -- Formal of inherited operation
15194
15195 Visible_Subp : Entity_Id := Parent_Subp;
15196
15197 function Is_Private_Overriding return Boolean;
15198 -- If Subp is a private overriding of a visible operation, the inherited
15199 -- operation derives from the overridden op (even though its body is the
15200 -- overriding one) and the inherited operation is visible now. See
15201 -- sem_disp to see the full details of the handling of the overridden
15202 -- subprogram, which is removed from the list of primitive operations of
15203 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15204 -- and used to diagnose abstract operations that need overriding in the
15205 -- derived type.
15206
15207 procedure Replace_Type (Id, New_Id : Entity_Id);
15208 -- When the type is an anonymous access type, create a new access type
15209 -- designating the derived type.
15210
15211 procedure Set_Derived_Name;
15212 -- This procedure sets the appropriate Chars name for New_Subp. This
15213 -- is normally just a copy of the parent name. An exception arises for
15214 -- type support subprograms, where the name is changed to reflect the
15215 -- name of the derived type, e.g. if type foo is derived from type bar,
15216 -- then a procedure barDA is derived with a name fooDA.
15217
15218 ---------------------------
15219 -- Is_Private_Overriding --
15220 ---------------------------
15221
15222 function Is_Private_Overriding return Boolean is
15223 Prev : Entity_Id;
15224
15225 begin
15226 -- If the parent is not a dispatching operation there is no
15227 -- need to investigate overridings
15228
15229 if not Is_Dispatching_Operation (Parent_Subp) then
15230 return False;
15231 end if;
15232
15233 -- The visible operation that is overridden is a homonym of the
15234 -- parent subprogram. We scan the homonym chain to find the one
15235 -- whose alias is the subprogram we are deriving.
15236
15237 Prev := Current_Entity (Parent_Subp);
15238 while Present (Prev) loop
15239 if Ekind (Prev) = Ekind (Parent_Subp)
15240 and then Alias (Prev) = Parent_Subp
15241 and then Scope (Parent_Subp) = Scope (Prev)
15242 and then not Is_Hidden (Prev)
15243 then
15244 Visible_Subp := Prev;
15245 return True;
15246 end if;
15247
15248 Prev := Homonym (Prev);
15249 end loop;
15250
15251 return False;
15252 end Is_Private_Overriding;
15253
15254 ------------------
15255 -- Replace_Type --
15256 ------------------
15257
15258 procedure Replace_Type (Id, New_Id : Entity_Id) is
15259 Id_Type : constant Entity_Id := Etype (Id);
15260 Acc_Type : Entity_Id;
15261 Par : constant Node_Id := Parent (Derived_Type);
15262
15263 begin
15264 -- When the type is an anonymous access type, create a new access
15265 -- type designating the derived type. This itype must be elaborated
15266 -- at the point of the derivation, not on subsequent calls that may
15267 -- be out of the proper scope for Gigi, so we insert a reference to
15268 -- it after the derivation.
15269
15270 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15271 declare
15272 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15273
15274 begin
15275 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15276 and then Present (Full_View (Desig_Typ))
15277 and then not Is_Private_Type (Parent_Type)
15278 then
15279 Desig_Typ := Full_View (Desig_Typ);
15280 end if;
15281
15282 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15283
15284 -- Ada 2005 (AI-251): Handle also derivations of abstract
15285 -- interface primitives.
15286
15287 or else (Is_Interface (Desig_Typ)
15288 and then not Is_Class_Wide_Type (Desig_Typ))
15289 then
15290 Acc_Type := New_Copy (Id_Type);
15291 Set_Etype (Acc_Type, Acc_Type);
15292 Set_Scope (Acc_Type, New_Subp);
15293
15294 -- Set size of anonymous access type. If we have an access
15295 -- to an unconstrained array, this is a fat pointer, so it
15296 -- is sizes at twice addtress size.
15297
15298 if Is_Array_Type (Desig_Typ)
15299 and then not Is_Constrained (Desig_Typ)
15300 then
15301 Init_Size (Acc_Type, 2 * System_Address_Size);
15302
15303 -- Other cases use a thin pointer
15304
15305 else
15306 Init_Size (Acc_Type, System_Address_Size);
15307 end if;
15308
15309 -- Set remaining characterstics of anonymous access type
15310
15311 Init_Alignment (Acc_Type);
15312 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15313
15314 Set_Etype (New_Id, Acc_Type);
15315 Set_Scope (New_Id, New_Subp);
15316
15317 -- Create a reference to it
15318
15319 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15320
15321 else
15322 Set_Etype (New_Id, Id_Type);
15323 end if;
15324 end;
15325
15326 -- In Ada2012, a formal may have an incomplete type but the type
15327 -- derivation that inherits the primitive follows the full view.
15328
15329 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15330 or else
15331 (Ekind (Id_Type) = E_Record_Type_With_Private
15332 and then Present (Full_View (Id_Type))
15333 and then
15334 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15335 or else
15336 (Ada_Version >= Ada_2012
15337 and then Ekind (Id_Type) = E_Incomplete_Type
15338 and then Full_View (Id_Type) = Parent_Type)
15339 then
15340 -- Constraint checks on formals are generated during expansion,
15341 -- based on the signature of the original subprogram. The bounds
15342 -- of the derived type are not relevant, and thus we can use
15343 -- the base type for the formals. However, the return type may be
15344 -- used in a context that requires that the proper static bounds
15345 -- be used (a case statement, for example) and for those cases
15346 -- we must use the derived type (first subtype), not its base.
15347
15348 -- If the derived_type_definition has no constraints, we know that
15349 -- the derived type has the same constraints as the first subtype
15350 -- of the parent, and we can also use it rather than its base,
15351 -- which can lead to more efficient code.
15352
15353 if Etype (Id) = Parent_Type then
15354 if Is_Scalar_Type (Parent_Type)
15355 and then
15356 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15357 then
15358 Set_Etype (New_Id, Derived_Type);
15359
15360 elsif Nkind (Par) = N_Full_Type_Declaration
15361 and then
15362 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15363 and then
15364 Is_Entity_Name
15365 (Subtype_Indication (Type_Definition (Par)))
15366 then
15367 Set_Etype (New_Id, Derived_Type);
15368
15369 else
15370 Set_Etype (New_Id, Base_Type (Derived_Type));
15371 end if;
15372
15373 else
15374 Set_Etype (New_Id, Base_Type (Derived_Type));
15375 end if;
15376
15377 else
15378 Set_Etype (New_Id, Etype (Id));
15379 end if;
15380 end Replace_Type;
15381
15382 ----------------------
15383 -- Set_Derived_Name --
15384 ----------------------
15385
15386 procedure Set_Derived_Name is
15387 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15388 begin
15389 if Nm = TSS_Null then
15390 Set_Chars (New_Subp, Chars (Parent_Subp));
15391 else
15392 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15393 end if;
15394 end Set_Derived_Name;
15395
15396 -- Start of processing for Derive_Subprogram
15397
15398 begin
15399 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15400 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15401
15402 -- Check whether the inherited subprogram is a private operation that
15403 -- should be inherited but not yet made visible. Such subprograms can
15404 -- become visible at a later point (e.g., the private part of a public
15405 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15406 -- following predicate is true, then this is not such a private
15407 -- operation and the subprogram simply inherits the name of the parent
15408 -- subprogram. Note the special check for the names of controlled
15409 -- operations, which are currently exempted from being inherited with
15410 -- a hidden name because they must be findable for generation of
15411 -- implicit run-time calls.
15412
15413 if not Is_Hidden (Parent_Subp)
15414 or else Is_Internal (Parent_Subp)
15415 or else Is_Private_Overriding
15416 or else Is_Internal_Name (Chars (Parent_Subp))
15417 or else (Is_Controlled (Parent_Type)
15418 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15419 Name_Finalize,
15420 Name_Initialize))
15421 then
15422 Set_Derived_Name;
15423
15424 -- An inherited dispatching equality will be overridden by an internally
15425 -- generated one, or by an explicit one, so preserve its name and thus
15426 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15427 -- private operation it may become invisible if the full view has
15428 -- progenitors, and the dispatch table will be malformed.
15429 -- We check that the type is limited to handle the anomalous declaration
15430 -- of Limited_Controlled, which is derived from a non-limited type, and
15431 -- which is handled specially elsewhere as well.
15432
15433 elsif Chars (Parent_Subp) = Name_Op_Eq
15434 and then Is_Dispatching_Operation (Parent_Subp)
15435 and then Etype (Parent_Subp) = Standard_Boolean
15436 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15437 and then
15438 Etype (First_Formal (Parent_Subp)) =
15439 Etype (Next_Formal (First_Formal (Parent_Subp)))
15440 then
15441 Set_Derived_Name;
15442
15443 -- If parent is hidden, this can be a regular derivation if the
15444 -- parent is immediately visible in a non-instantiating context,
15445 -- or if we are in the private part of an instance. This test
15446 -- should still be refined ???
15447
15448 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15449 -- operation as a non-visible operation in cases where the parent
15450 -- subprogram might not be visible now, but was visible within the
15451 -- original generic, so it would be wrong to make the inherited
15452 -- subprogram non-visible now. (Not clear if this test is fully
15453 -- correct; are there any cases where we should declare the inherited
15454 -- operation as not visible to avoid it being overridden, e.g., when
15455 -- the parent type is a generic actual with private primitives ???)
15456
15457 -- (they should be treated the same as other private inherited
15458 -- subprograms, but it's not clear how to do this cleanly). ???
15459
15460 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15461 and then Is_Immediately_Visible (Parent_Subp)
15462 and then not In_Instance)
15463 or else In_Instance_Not_Visible
15464 then
15465 Set_Derived_Name;
15466
15467 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15468 -- overrides an interface primitive because interface primitives
15469 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15470
15471 elsif Ada_Version >= Ada_2005
15472 and then Is_Dispatching_Operation (Parent_Subp)
15473 and then Present (Covered_Interface_Op (Parent_Subp))
15474 then
15475 Set_Derived_Name;
15476
15477 -- Otherwise, the type is inheriting a private operation, so enter it
15478 -- with a special name so it can't be overridden.
15479
15480 else
15481 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15482 end if;
15483
15484 Set_Parent (New_Subp, Parent (Derived_Type));
15485
15486 if Present (Actual_Subp) then
15487 Replace_Type (Actual_Subp, New_Subp);
15488 else
15489 Replace_Type (Parent_Subp, New_Subp);
15490 end if;
15491
15492 Conditional_Delay (New_Subp, Parent_Subp);
15493
15494 -- If we are creating a renaming for a primitive operation of an
15495 -- actual of a generic derived type, we must examine the signature
15496 -- of the actual primitive, not that of the generic formal, which for
15497 -- example may be an interface. However the name and initial value
15498 -- of the inherited operation are those of the formal primitive.
15499
15500 Formal := First_Formal (Parent_Subp);
15501
15502 if Present (Actual_Subp) then
15503 Formal_Of_Actual := First_Formal (Actual_Subp);
15504 else
15505 Formal_Of_Actual := Empty;
15506 end if;
15507
15508 while Present (Formal) loop
15509 New_Formal := New_Copy (Formal);
15510
15511 -- Normally we do not go copying parents, but in the case of
15512 -- formals, we need to link up to the declaration (which is the
15513 -- parameter specification), and it is fine to link up to the
15514 -- original formal's parameter specification in this case.
15515
15516 Set_Parent (New_Formal, Parent (Formal));
15517 Append_Entity (New_Formal, New_Subp);
15518
15519 if Present (Formal_Of_Actual) then
15520 Replace_Type (Formal_Of_Actual, New_Formal);
15521 Next_Formal (Formal_Of_Actual);
15522 else
15523 Replace_Type (Formal, New_Formal);
15524 end if;
15525
15526 Next_Formal (Formal);
15527 end loop;
15528
15529 -- If this derivation corresponds to a tagged generic actual, then
15530 -- primitive operations rename those of the actual. Otherwise the
15531 -- primitive operations rename those of the parent type, If the parent
15532 -- renames an intrinsic operator, so does the new subprogram. We except
15533 -- concatenation, which is always properly typed, and does not get
15534 -- expanded as other intrinsic operations.
15535
15536 if No (Actual_Subp) then
15537 if Is_Intrinsic_Subprogram (Parent_Subp) then
15538 Set_Is_Intrinsic_Subprogram (New_Subp);
15539
15540 if Present (Alias (Parent_Subp))
15541 and then Chars (Parent_Subp) /= Name_Op_Concat
15542 then
15543 Set_Alias (New_Subp, Alias (Parent_Subp));
15544 else
15545 Set_Alias (New_Subp, Parent_Subp);
15546 end if;
15547
15548 else
15549 Set_Alias (New_Subp, Parent_Subp);
15550 end if;
15551
15552 else
15553 Set_Alias (New_Subp, Actual_Subp);
15554 end if;
15555
15556 -- Derived subprograms of a tagged type must inherit the convention
15557 -- of the parent subprogram (a requirement of AI-117). Derived
15558 -- subprograms of untagged types simply get convention Ada by default.
15559
15560 -- If the derived type is a tagged generic formal type with unknown
15561 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15562
15563 -- However, if the type is derived from a generic formal, the further
15564 -- inherited subprogram has the convention of the non-generic ancestor.
15565 -- Otherwise there would be no way to override the operation.
15566 -- (This is subject to forthcoming ARG discussions).
15567
15568 if Is_Tagged_Type (Derived_Type) then
15569 if Is_Generic_Type (Derived_Type)
15570 and then Has_Unknown_Discriminants (Derived_Type)
15571 then
15572 Set_Convention (New_Subp, Convention_Intrinsic);
15573
15574 else
15575 if Is_Generic_Type (Parent_Type)
15576 and then Has_Unknown_Discriminants (Parent_Type)
15577 then
15578 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15579 else
15580 Set_Convention (New_Subp, Convention (Parent_Subp));
15581 end if;
15582 end if;
15583 end if;
15584
15585 -- Predefined controlled operations retain their name even if the parent
15586 -- is hidden (see above), but they are not primitive operations if the
15587 -- ancestor is not visible, for example if the parent is a private
15588 -- extension completed with a controlled extension. Note that a full
15589 -- type that is controlled can break privacy: the flag Is_Controlled is
15590 -- set on both views of the type.
15591
15592 if Is_Controlled (Parent_Type)
15593 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15594 Name_Adjust,
15595 Name_Finalize)
15596 and then Is_Hidden (Parent_Subp)
15597 and then not Is_Visibly_Controlled (Parent_Type)
15598 then
15599 Set_Is_Hidden (New_Subp);
15600 end if;
15601
15602 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15603 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15604
15605 if Ekind (Parent_Subp) = E_Procedure then
15606 Set_Is_Valued_Procedure
15607 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15608 else
15609 Set_Has_Controlling_Result
15610 (New_Subp, Has_Controlling_Result (Parent_Subp));
15611 end if;
15612
15613 -- No_Return must be inherited properly. If this is overridden in the
15614 -- case of a dispatching operation, then a check is made in Sem_Disp
15615 -- that the overriding operation is also No_Return (no such check is
15616 -- required for the case of non-dispatching operation.
15617
15618 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15619
15620 -- A derived function with a controlling result is abstract. If the
15621 -- Derived_Type is a nonabstract formal generic derived type, then
15622 -- inherited operations are not abstract: the required check is done at
15623 -- instantiation time. If the derivation is for a generic actual, the
15624 -- function is not abstract unless the actual is.
15625
15626 if Is_Generic_Type (Derived_Type)
15627 and then not Is_Abstract_Type (Derived_Type)
15628 then
15629 null;
15630
15631 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15632 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15633
15634 -- A subprogram subject to pragma Extensions_Visible with value False
15635 -- requires overriding if the subprogram has at least one controlling
15636 -- OUT parameter (SPARK RM 6.1.7(6)).
15637
15638 elsif Ada_Version >= Ada_2005
15639 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15640 or else (Is_Tagged_Type (Derived_Type)
15641 and then Etype (New_Subp) = Derived_Type
15642 and then not Is_Null_Extension (Derived_Type))
15643 or else (Is_Tagged_Type (Derived_Type)
15644 and then Ekind (Etype (New_Subp)) =
15645 E_Anonymous_Access_Type
15646 and then Designated_Type (Etype (New_Subp)) =
15647 Derived_Type
15648 and then not Is_Null_Extension (Derived_Type))
15649 or else (Comes_From_Source (Alias (New_Subp))
15650 and then Is_EVF_Procedure (Alias (New_Subp))))
15651 and then No (Actual_Subp)
15652 then
15653 if not Is_Tagged_Type (Derived_Type)
15654 or else Is_Abstract_Type (Derived_Type)
15655 or else Is_Abstract_Subprogram (Alias (New_Subp))
15656 then
15657 Set_Is_Abstract_Subprogram (New_Subp);
15658 else
15659 Set_Requires_Overriding (New_Subp);
15660 end if;
15661
15662 elsif Ada_Version < Ada_2005
15663 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15664 or else (Is_Tagged_Type (Derived_Type)
15665 and then Etype (New_Subp) = Derived_Type
15666 and then No (Actual_Subp)))
15667 then
15668 Set_Is_Abstract_Subprogram (New_Subp);
15669
15670 -- AI05-0097 : an inherited operation that dispatches on result is
15671 -- abstract if the derived type is abstract, even if the parent type
15672 -- is concrete and the derived type is a null extension.
15673
15674 elsif Has_Controlling_Result (Alias (New_Subp))
15675 and then Is_Abstract_Type (Etype (New_Subp))
15676 then
15677 Set_Is_Abstract_Subprogram (New_Subp);
15678
15679 -- Finally, if the parent type is abstract we must verify that all
15680 -- inherited operations are either non-abstract or overridden, or that
15681 -- the derived type itself is abstract (this check is performed at the
15682 -- end of a package declaration, in Check_Abstract_Overriding). A
15683 -- private overriding in the parent type will not be visible in the
15684 -- derivation if we are not in an inner package or in a child unit of
15685 -- the parent type, in which case the abstractness of the inherited
15686 -- operation is carried to the new subprogram.
15687
15688 elsif Is_Abstract_Type (Parent_Type)
15689 and then not In_Open_Scopes (Scope (Parent_Type))
15690 and then Is_Private_Overriding
15691 and then Is_Abstract_Subprogram (Visible_Subp)
15692 then
15693 if No (Actual_Subp) then
15694 Set_Alias (New_Subp, Visible_Subp);
15695 Set_Is_Abstract_Subprogram (New_Subp, True);
15696
15697 else
15698 -- If this is a derivation for an instance of a formal derived
15699 -- type, abstractness comes from the primitive operation of the
15700 -- actual, not from the operation inherited from the ancestor.
15701
15702 Set_Is_Abstract_Subprogram
15703 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15704 end if;
15705 end if;
15706
15707 New_Overloaded_Entity (New_Subp, Derived_Type);
15708
15709 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15710 -- preconditions and the derived type is abstract, the derived operation
15711 -- is abstract as well if parent subprogram is not abstract or null.
15712
15713 if Is_Abstract_Type (Derived_Type)
15714 and then Has_Non_Trivial_Precondition (Parent_Subp)
15715 and then Present (Interfaces (Derived_Type))
15716 then
15717
15718 -- Add useful attributes of subprogram before the freeze point,
15719 -- in case freezing is delayed or there are previous errors.
15720
15721 Set_Is_Dispatching_Operation (New_Subp);
15722
15723 declare
15724 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15725
15726 begin
15727 if Present (Iface_Prim)
15728 and then Has_Non_Trivial_Precondition (Iface_Prim)
15729 then
15730 Set_Is_Abstract_Subprogram (New_Subp);
15731 end if;
15732 end;
15733 end if;
15734
15735 -- Check for case of a derived subprogram for the instantiation of a
15736 -- formal derived tagged type, if so mark the subprogram as dispatching
15737 -- and inherit the dispatching attributes of the actual subprogram. The
15738 -- derived subprogram is effectively renaming of the actual subprogram,
15739 -- so it needs to have the same attributes as the actual.
15740
15741 if Present (Actual_Subp)
15742 and then Is_Dispatching_Operation (Actual_Subp)
15743 then
15744 Set_Is_Dispatching_Operation (New_Subp);
15745
15746 if Present (DTC_Entity (Actual_Subp)) then
15747 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15748 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15749 end if;
15750 end if;
15751
15752 -- Indicate that a derived subprogram does not require a body and that
15753 -- it does not require processing of default expressions.
15754
15755 Set_Has_Completion (New_Subp);
15756 Set_Default_Expressions_Processed (New_Subp);
15757
15758 if Ekind (New_Subp) = E_Function then
15759 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15760 end if;
15761 end Derive_Subprogram;
15762
15763 ------------------------
15764 -- Derive_Subprograms --
15765 ------------------------
15766
15767 procedure Derive_Subprograms
15768 (Parent_Type : Entity_Id;
15769 Derived_Type : Entity_Id;
15770 Generic_Actual : Entity_Id := Empty)
15771 is
15772 Op_List : constant Elist_Id :=
15773 Collect_Primitive_Operations (Parent_Type);
15774
15775 function Check_Derived_Type return Boolean;
15776 -- Check that all the entities derived from Parent_Type are found in
15777 -- the list of primitives of Derived_Type exactly in the same order.
15778
15779 procedure Derive_Interface_Subprogram
15780 (New_Subp : out Entity_Id;
15781 Subp : Entity_Id;
15782 Actual_Subp : Entity_Id);
15783 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15784 -- (which is an interface primitive). If Generic_Actual is present then
15785 -- Actual_Subp is the actual subprogram corresponding with the generic
15786 -- subprogram Subp.
15787
15788 ------------------------
15789 -- Check_Derived_Type --
15790 ------------------------
15791
15792 function Check_Derived_Type return Boolean is
15793 E : Entity_Id;
15794 Elmt : Elmt_Id;
15795 List : Elist_Id;
15796 New_Subp : Entity_Id;
15797 Op_Elmt : Elmt_Id;
15798 Subp : Entity_Id;
15799
15800 begin
15801 -- Traverse list of entities in the current scope searching for
15802 -- an incomplete type whose full-view is derived type.
15803
15804 E := First_Entity (Scope (Derived_Type));
15805 while Present (E) and then E /= Derived_Type loop
15806 if Ekind (E) = E_Incomplete_Type
15807 and then Present (Full_View (E))
15808 and then Full_View (E) = Derived_Type
15809 then
15810 -- Disable this test if Derived_Type completes an incomplete
15811 -- type because in such case more primitives can be added
15812 -- later to the list of primitives of Derived_Type by routine
15813 -- Process_Incomplete_Dependents
15814
15815 return True;
15816 end if;
15817
15818 E := Next_Entity (E);
15819 end loop;
15820
15821 List := Collect_Primitive_Operations (Derived_Type);
15822 Elmt := First_Elmt (List);
15823
15824 Op_Elmt := First_Elmt (Op_List);
15825 while Present (Op_Elmt) loop
15826 Subp := Node (Op_Elmt);
15827 New_Subp := Node (Elmt);
15828
15829 -- At this early stage Derived_Type has no entities with attribute
15830 -- Interface_Alias. In addition, such primitives are always
15831 -- located at the end of the list of primitives of Parent_Type.
15832 -- Therefore, if found we can safely stop processing pending
15833 -- entities.
15834
15835 exit when Present (Interface_Alias (Subp));
15836
15837 -- Handle hidden entities
15838
15839 if not Is_Predefined_Dispatching_Operation (Subp)
15840 and then Is_Hidden (Subp)
15841 then
15842 if Present (New_Subp)
15843 and then Primitive_Names_Match (Subp, New_Subp)
15844 then
15845 Next_Elmt (Elmt);
15846 end if;
15847
15848 else
15849 if not Present (New_Subp)
15850 or else Ekind (Subp) /= Ekind (New_Subp)
15851 or else not Primitive_Names_Match (Subp, New_Subp)
15852 then
15853 return False;
15854 end if;
15855
15856 Next_Elmt (Elmt);
15857 end if;
15858
15859 Next_Elmt (Op_Elmt);
15860 end loop;
15861
15862 return True;
15863 end Check_Derived_Type;
15864
15865 ---------------------------------
15866 -- Derive_Interface_Subprogram --
15867 ---------------------------------
15868
15869 procedure Derive_Interface_Subprogram
15870 (New_Subp : out Entity_Id;
15871 Subp : Entity_Id;
15872 Actual_Subp : Entity_Id)
15873 is
15874 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15875 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15876
15877 begin
15878 pragma Assert (Is_Interface (Iface_Type));
15879
15880 Derive_Subprogram
15881 (New_Subp => New_Subp,
15882 Parent_Subp => Iface_Subp,
15883 Derived_Type => Derived_Type,
15884 Parent_Type => Iface_Type,
15885 Actual_Subp => Actual_Subp);
15886
15887 -- Given that this new interface entity corresponds with a primitive
15888 -- of the parent that was not overridden we must leave it associated
15889 -- with its parent primitive to ensure that it will share the same
15890 -- dispatch table slot when overridden. We must set the Alias to Subp
15891 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15892 -- (in case we inherited Subp from Iface_Type via a nonabstract
15893 -- generic formal type).
15894
15895 if No (Actual_Subp) then
15896 Set_Alias (New_Subp, Subp);
15897
15898 declare
15899 T : Entity_Id := Find_Dispatching_Type (Subp);
15900 begin
15901 while Etype (T) /= T loop
15902 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15903 Set_Is_Abstract_Subprogram (New_Subp, False);
15904 exit;
15905 end if;
15906
15907 T := Etype (T);
15908 end loop;
15909 end;
15910
15911 -- For instantiations this is not needed since the previous call to
15912 -- Derive_Subprogram leaves the entity well decorated.
15913
15914 else
15915 pragma Assert (Alias (New_Subp) = Actual_Subp);
15916 null;
15917 end if;
15918 end Derive_Interface_Subprogram;
15919
15920 -- Local variables
15921
15922 Alias_Subp : Entity_Id;
15923 Act_List : Elist_Id;
15924 Act_Elmt : Elmt_Id;
15925 Act_Subp : Entity_Id := Empty;
15926 Elmt : Elmt_Id;
15927 Need_Search : Boolean := False;
15928 New_Subp : Entity_Id := Empty;
15929 Parent_Base : Entity_Id;
15930 Subp : Entity_Id;
15931
15932 -- Start of processing for Derive_Subprograms
15933
15934 begin
15935 if Ekind (Parent_Type) = E_Record_Type_With_Private
15936 and then Has_Discriminants (Parent_Type)
15937 and then Present (Full_View (Parent_Type))
15938 then
15939 Parent_Base := Full_View (Parent_Type);
15940 else
15941 Parent_Base := Parent_Type;
15942 end if;
15943
15944 if Present (Generic_Actual) then
15945 Act_List := Collect_Primitive_Operations (Generic_Actual);
15946 Act_Elmt := First_Elmt (Act_List);
15947 else
15948 Act_List := No_Elist;
15949 Act_Elmt := No_Elmt;
15950 end if;
15951
15952 -- Derive primitives inherited from the parent. Note that if the generic
15953 -- actual is present, this is not really a type derivation, it is a
15954 -- completion within an instance.
15955
15956 -- Case 1: Derived_Type does not implement interfaces
15957
15958 if not Is_Tagged_Type (Derived_Type)
15959 or else (not Has_Interfaces (Derived_Type)
15960 and then not (Present (Generic_Actual)
15961 and then Has_Interfaces (Generic_Actual)))
15962 then
15963 Elmt := First_Elmt (Op_List);
15964 while Present (Elmt) loop
15965 Subp := Node (Elmt);
15966
15967 -- Literals are derived earlier in the process of building the
15968 -- derived type, and are skipped here.
15969
15970 if Ekind (Subp) = E_Enumeration_Literal then
15971 null;
15972
15973 -- The actual is a direct descendant and the common primitive
15974 -- operations appear in the same order.
15975
15976 -- If the generic parent type is present, the derived type is an
15977 -- instance of a formal derived type, and within the instance its
15978 -- operations are those of the actual. We derive from the formal
15979 -- type but make the inherited operations aliases of the
15980 -- corresponding operations of the actual.
15981
15982 else
15983 pragma Assert (No (Node (Act_Elmt))
15984 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15985 and then
15986 Type_Conformant
15987 (Subp, Node (Act_Elmt),
15988 Skip_Controlling_Formals => True)));
15989
15990 Derive_Subprogram
15991 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15992
15993 if Present (Act_Elmt) then
15994 Next_Elmt (Act_Elmt);
15995 end if;
15996 end if;
15997
15998 Next_Elmt (Elmt);
15999 end loop;
16000
16001 -- Case 2: Derived_Type implements interfaces
16002
16003 else
16004 -- If the parent type has no predefined primitives we remove
16005 -- predefined primitives from the list of primitives of generic
16006 -- actual to simplify the complexity of this algorithm.
16007
16008 if Present (Generic_Actual) then
16009 declare
16010 Has_Predefined_Primitives : Boolean := False;
16011
16012 begin
16013 -- Check if the parent type has predefined primitives
16014
16015 Elmt := First_Elmt (Op_List);
16016 while Present (Elmt) loop
16017 Subp := Node (Elmt);
16018
16019 if Is_Predefined_Dispatching_Operation (Subp)
16020 and then not Comes_From_Source (Ultimate_Alias (Subp))
16021 then
16022 Has_Predefined_Primitives := True;
16023 exit;
16024 end if;
16025
16026 Next_Elmt (Elmt);
16027 end loop;
16028
16029 -- Remove predefined primitives of Generic_Actual. We must use
16030 -- an auxiliary list because in case of tagged types the value
16031 -- returned by Collect_Primitive_Operations is the value stored
16032 -- in its Primitive_Operations attribute (and we don't want to
16033 -- modify its current contents).
16034
16035 if not Has_Predefined_Primitives then
16036 declare
16037 Aux_List : constant Elist_Id := New_Elmt_List;
16038
16039 begin
16040 Elmt := First_Elmt (Act_List);
16041 while Present (Elmt) loop
16042 Subp := Node (Elmt);
16043
16044 if not Is_Predefined_Dispatching_Operation (Subp)
16045 or else Comes_From_Source (Subp)
16046 then
16047 Append_Elmt (Subp, Aux_List);
16048 end if;
16049
16050 Next_Elmt (Elmt);
16051 end loop;
16052
16053 Act_List := Aux_List;
16054 end;
16055 end if;
16056
16057 Act_Elmt := First_Elmt (Act_List);
16058 Act_Subp := Node (Act_Elmt);
16059 end;
16060 end if;
16061
16062 -- Stage 1: If the generic actual is not present we derive the
16063 -- primitives inherited from the parent type. If the generic parent
16064 -- type is present, the derived type is an instance of a formal
16065 -- derived type, and within the instance its operations are those of
16066 -- the actual. We derive from the formal type but make the inherited
16067 -- operations aliases of the corresponding operations of the actual.
16068
16069 Elmt := First_Elmt (Op_List);
16070 while Present (Elmt) loop
16071 Subp := Node (Elmt);
16072 Alias_Subp := Ultimate_Alias (Subp);
16073
16074 -- Do not derive internal entities of the parent that link
16075 -- interface primitives with their covering primitive. These
16076 -- entities will be added to this type when frozen.
16077
16078 if Present (Interface_Alias (Subp)) then
16079 goto Continue;
16080 end if;
16081
16082 -- If the generic actual is present find the corresponding
16083 -- operation in the generic actual. If the parent type is a
16084 -- direct ancestor of the derived type then, even if it is an
16085 -- interface, the operations are inherited from the primary
16086 -- dispatch table and are in the proper order. If we detect here
16087 -- that primitives are not in the same order we traverse the list
16088 -- of primitive operations of the actual to find the one that
16089 -- implements the interface primitive.
16090
16091 if Need_Search
16092 or else
16093 (Present (Generic_Actual)
16094 and then Present (Act_Subp)
16095 and then not
16096 (Primitive_Names_Match (Subp, Act_Subp)
16097 and then
16098 Type_Conformant (Subp, Act_Subp,
16099 Skip_Controlling_Formals => True)))
16100 then
16101 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16102 Use_Full_View => True));
16103
16104 -- Remember that we need searching for all pending primitives
16105
16106 Need_Search := True;
16107
16108 -- Handle entities associated with interface primitives
16109
16110 if Present (Alias_Subp)
16111 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16112 and then not Is_Predefined_Dispatching_Operation (Subp)
16113 then
16114 -- Search for the primitive in the homonym chain
16115
16116 Act_Subp :=
16117 Find_Primitive_Covering_Interface
16118 (Tagged_Type => Generic_Actual,
16119 Iface_Prim => Alias_Subp);
16120
16121 -- Previous search may not locate primitives covering
16122 -- interfaces defined in generics units or instantiations.
16123 -- (it fails if the covering primitive has formals whose
16124 -- type is also defined in generics or instantiations).
16125 -- In such case we search in the list of primitives of the
16126 -- generic actual for the internal entity that links the
16127 -- interface primitive and the covering primitive.
16128
16129 if No (Act_Subp)
16130 and then Is_Generic_Type (Parent_Type)
16131 then
16132 -- This code has been designed to handle only generic
16133 -- formals that implement interfaces that are defined
16134 -- in a generic unit or instantiation. If this code is
16135 -- needed for other cases we must review it because
16136 -- (given that it relies on Original_Location to locate
16137 -- the primitive of Generic_Actual that covers the
16138 -- interface) it could leave linked through attribute
16139 -- Alias entities of unrelated instantiations).
16140
16141 pragma Assert
16142 (Is_Generic_Unit
16143 (Scope (Find_Dispatching_Type (Alias_Subp)))
16144 or else
16145 Instantiation_Depth
16146 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16147
16148 declare
16149 Iface_Prim_Loc : constant Source_Ptr :=
16150 Original_Location (Sloc (Alias_Subp));
16151
16152 Elmt : Elmt_Id;
16153 Prim : Entity_Id;
16154
16155 begin
16156 Elmt :=
16157 First_Elmt (Primitive_Operations (Generic_Actual));
16158
16159 Search : while Present (Elmt) loop
16160 Prim := Node (Elmt);
16161
16162 if Present (Interface_Alias (Prim))
16163 and then Original_Location
16164 (Sloc (Interface_Alias (Prim))) =
16165 Iface_Prim_Loc
16166 then
16167 Act_Subp := Alias (Prim);
16168 exit Search;
16169 end if;
16170
16171 Next_Elmt (Elmt);
16172 end loop Search;
16173 end;
16174 end if;
16175
16176 pragma Assert (Present (Act_Subp)
16177 or else Is_Abstract_Type (Generic_Actual)
16178 or else Serious_Errors_Detected > 0);
16179
16180 -- Handle predefined primitives plus the rest of user-defined
16181 -- primitives
16182
16183 else
16184 Act_Elmt := First_Elmt (Act_List);
16185 while Present (Act_Elmt) loop
16186 Act_Subp := Node (Act_Elmt);
16187
16188 exit when Primitive_Names_Match (Subp, Act_Subp)
16189 and then Type_Conformant
16190 (Subp, Act_Subp,
16191 Skip_Controlling_Formals => True)
16192 and then No (Interface_Alias (Act_Subp));
16193
16194 Next_Elmt (Act_Elmt);
16195 end loop;
16196
16197 if No (Act_Elmt) then
16198 Act_Subp := Empty;
16199 end if;
16200 end if;
16201 end if;
16202
16203 -- Case 1: If the parent is a limited interface then it has the
16204 -- predefined primitives of synchronized interfaces. However, the
16205 -- actual type may be a non-limited type and hence it does not
16206 -- have such primitives.
16207
16208 if Present (Generic_Actual)
16209 and then not Present (Act_Subp)
16210 and then Is_Limited_Interface (Parent_Base)
16211 and then Is_Predefined_Interface_Primitive (Subp)
16212 then
16213 null;
16214
16215 -- Case 2: Inherit entities associated with interfaces that were
16216 -- not covered by the parent type. We exclude here null interface
16217 -- primitives because they do not need special management.
16218
16219 -- We also exclude interface operations that are renamings. If the
16220 -- subprogram is an explicit renaming of an interface primitive,
16221 -- it is a regular primitive operation, and the presence of its
16222 -- alias is not relevant: it has to be derived like any other
16223 -- primitive.
16224
16225 elsif Present (Alias (Subp))
16226 and then Nkind (Unit_Declaration_Node (Subp)) /=
16227 N_Subprogram_Renaming_Declaration
16228 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16229 and then not
16230 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16231 and then Null_Present (Parent (Alias_Subp)))
16232 then
16233 -- If this is an abstract private type then we transfer the
16234 -- derivation of the interface primitive from the partial view
16235 -- to the full view. This is safe because all the interfaces
16236 -- must be visible in the partial view. Done to avoid adding
16237 -- a new interface derivation to the private part of the
16238 -- enclosing package; otherwise this new derivation would be
16239 -- decorated as hidden when the analysis of the enclosing
16240 -- package completes.
16241
16242 if Is_Abstract_Type (Derived_Type)
16243 and then In_Private_Part (Current_Scope)
16244 and then Has_Private_Declaration (Derived_Type)
16245 then
16246 declare
16247 Partial_View : Entity_Id;
16248 Elmt : Elmt_Id;
16249 Ent : Entity_Id;
16250
16251 begin
16252 Partial_View := First_Entity (Current_Scope);
16253 loop
16254 exit when No (Partial_View)
16255 or else (Has_Private_Declaration (Partial_View)
16256 and then
16257 Full_View (Partial_View) = Derived_Type);
16258
16259 Next_Entity (Partial_View);
16260 end loop;
16261
16262 -- If the partial view was not found then the source code
16263 -- has errors and the derivation is not needed.
16264
16265 if Present (Partial_View) then
16266 Elmt :=
16267 First_Elmt (Primitive_Operations (Partial_View));
16268 while Present (Elmt) loop
16269 Ent := Node (Elmt);
16270
16271 if Present (Alias (Ent))
16272 and then Ultimate_Alias (Ent) = Alias (Subp)
16273 then
16274 Append_Elmt
16275 (Ent, Primitive_Operations (Derived_Type));
16276 exit;
16277 end if;
16278
16279 Next_Elmt (Elmt);
16280 end loop;
16281
16282 -- If the interface primitive was not found in the
16283 -- partial view then this interface primitive was
16284 -- overridden. We add a derivation to activate in
16285 -- Derive_Progenitor_Subprograms the machinery to
16286 -- search for it.
16287
16288 if No (Elmt) then
16289 Derive_Interface_Subprogram
16290 (New_Subp => New_Subp,
16291 Subp => Subp,
16292 Actual_Subp => Act_Subp);
16293 end if;
16294 end if;
16295 end;
16296 else
16297 Derive_Interface_Subprogram
16298 (New_Subp => New_Subp,
16299 Subp => Subp,
16300 Actual_Subp => Act_Subp);
16301 end if;
16302
16303 -- Case 3: Common derivation
16304
16305 else
16306 Derive_Subprogram
16307 (New_Subp => New_Subp,
16308 Parent_Subp => Subp,
16309 Derived_Type => Derived_Type,
16310 Parent_Type => Parent_Base,
16311 Actual_Subp => Act_Subp);
16312 end if;
16313
16314 -- No need to update Act_Elm if we must search for the
16315 -- corresponding operation in the generic actual
16316
16317 if not Need_Search
16318 and then Present (Act_Elmt)
16319 then
16320 Next_Elmt (Act_Elmt);
16321 Act_Subp := Node (Act_Elmt);
16322 end if;
16323
16324 <<Continue>>
16325 Next_Elmt (Elmt);
16326 end loop;
16327
16328 -- Inherit additional operations from progenitors. If the derived
16329 -- type is a generic actual, there are not new primitive operations
16330 -- for the type because it has those of the actual, and therefore
16331 -- nothing needs to be done. The renamings generated above are not
16332 -- primitive operations, and their purpose is simply to make the
16333 -- proper operations visible within an instantiation.
16334
16335 if No (Generic_Actual) then
16336 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16337 end if;
16338 end if;
16339
16340 -- Final check: Direct descendants must have their primitives in the
16341 -- same order. We exclude from this test untagged types and instances
16342 -- of formal derived types. We skip this test if we have already
16343 -- reported serious errors in the sources.
16344
16345 pragma Assert (not Is_Tagged_Type (Derived_Type)
16346 or else Present (Generic_Actual)
16347 or else Serious_Errors_Detected > 0
16348 or else Check_Derived_Type);
16349 end Derive_Subprograms;
16350
16351 --------------------------------
16352 -- Derived_Standard_Character --
16353 --------------------------------
16354
16355 procedure Derived_Standard_Character
16356 (N : Node_Id;
16357 Parent_Type : Entity_Id;
16358 Derived_Type : Entity_Id)
16359 is
16360 Loc : constant Source_Ptr := Sloc (N);
16361 Def : constant Node_Id := Type_Definition (N);
16362 Indic : constant Node_Id := Subtype_Indication (Def);
16363 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16364 Implicit_Base : constant Entity_Id :=
16365 Create_Itype
16366 (E_Enumeration_Type, N, Derived_Type, 'B');
16367
16368 Lo : Node_Id;
16369 Hi : Node_Id;
16370
16371 begin
16372 Discard_Node (Process_Subtype (Indic, N));
16373
16374 Set_Etype (Implicit_Base, Parent_Base);
16375 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16376 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16377
16378 Set_Is_Character_Type (Implicit_Base, True);
16379 Set_Has_Delayed_Freeze (Implicit_Base);
16380
16381 -- The bounds of the implicit base are the bounds of the parent base.
16382 -- Note that their type is the parent base.
16383
16384 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16385 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16386
16387 Set_Scalar_Range (Implicit_Base,
16388 Make_Range (Loc,
16389 Low_Bound => Lo,
16390 High_Bound => Hi));
16391
16392 Conditional_Delay (Derived_Type, Parent_Type);
16393
16394 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16395 Set_Etype (Derived_Type, Implicit_Base);
16396 Set_Size_Info (Derived_Type, Parent_Type);
16397
16398 if Unknown_RM_Size (Derived_Type) then
16399 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16400 end if;
16401
16402 Set_Is_Character_Type (Derived_Type, True);
16403
16404 if Nkind (Indic) /= N_Subtype_Indication then
16405
16406 -- If no explicit constraint, the bounds are those
16407 -- of the parent type.
16408
16409 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16410 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16411 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16412 end if;
16413
16414 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16415
16416 -- Because the implicit base is used in the conversion of the bounds, we
16417 -- have to freeze it now. This is similar to what is done for numeric
16418 -- types, and it equally suspicious, but otherwise a non-static bound
16419 -- will have a reference to an unfrozen type, which is rejected by Gigi
16420 -- (???). This requires specific care for definition of stream
16421 -- attributes. For details, see comments at the end of
16422 -- Build_Derived_Numeric_Type.
16423
16424 Freeze_Before (N, Implicit_Base);
16425 end Derived_Standard_Character;
16426
16427 ------------------------------
16428 -- Derived_Type_Declaration --
16429 ------------------------------
16430
16431 procedure Derived_Type_Declaration
16432 (T : Entity_Id;
16433 N : Node_Id;
16434 Is_Completion : Boolean)
16435 is
16436 Parent_Type : Entity_Id;
16437
16438 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16439 -- Check whether the parent type is a generic formal, or derives
16440 -- directly or indirectly from one.
16441
16442 ------------------------
16443 -- Comes_From_Generic --
16444 ------------------------
16445
16446 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16447 begin
16448 if Is_Generic_Type (Typ) then
16449 return True;
16450
16451 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16452 return True;
16453
16454 elsif Is_Private_Type (Typ)
16455 and then Present (Full_View (Typ))
16456 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16457 then
16458 return True;
16459
16460 elsif Is_Generic_Actual_Type (Typ) then
16461 return True;
16462
16463 else
16464 return False;
16465 end if;
16466 end Comes_From_Generic;
16467
16468 -- Local variables
16469
16470 Def : constant Node_Id := Type_Definition (N);
16471 Iface_Def : Node_Id;
16472 Indic : constant Node_Id := Subtype_Indication (Def);
16473 Extension : constant Node_Id := Record_Extension_Part (Def);
16474 Parent_Node : Node_Id;
16475 Taggd : Boolean;
16476
16477 -- Start of processing for Derived_Type_Declaration
16478
16479 begin
16480 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16481
16482 if SPARK_Mode = On
16483 and then Is_Tagged_Type (Parent_Type)
16484 then
16485 declare
16486 Partial_View : constant Entity_Id :=
16487 Incomplete_Or_Partial_View (Parent_Type);
16488
16489 begin
16490 -- If the partial view was not found then the parent type is not
16491 -- a private type. Otherwise check if the partial view is a tagged
16492 -- private type.
16493
16494 if Present (Partial_View)
16495 and then Is_Private_Type (Partial_View)
16496 and then not Is_Tagged_Type (Partial_View)
16497 then
16498 Error_Msg_NE
16499 ("cannot derive from & declared as untagged private "
16500 & "(SPARK RM 3.4(1))", N, Partial_View);
16501 end if;
16502 end;
16503 end if;
16504
16505 -- Ada 2005 (AI-251): In case of interface derivation check that the
16506 -- parent is also an interface.
16507
16508 if Interface_Present (Def) then
16509 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16510
16511 if not Is_Interface (Parent_Type) then
16512 Diagnose_Interface (Indic, Parent_Type);
16513
16514 else
16515 Parent_Node := Parent (Base_Type (Parent_Type));
16516 Iface_Def := Type_Definition (Parent_Node);
16517
16518 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16519 -- other limited interfaces.
16520
16521 if Limited_Present (Def) then
16522 if Limited_Present (Iface_Def) then
16523 null;
16524
16525 elsif Protected_Present (Iface_Def) then
16526 Error_Msg_NE
16527 ("descendant of & must be declared as a protected "
16528 & "interface", N, Parent_Type);
16529
16530 elsif Synchronized_Present (Iface_Def) then
16531 Error_Msg_NE
16532 ("descendant of & must be declared as a synchronized "
16533 & "interface", N, Parent_Type);
16534
16535 elsif Task_Present (Iface_Def) then
16536 Error_Msg_NE
16537 ("descendant of & must be declared as a task interface",
16538 N, Parent_Type);
16539
16540 else
16541 Error_Msg_N
16542 ("(Ada 2005) limited interface cannot inherit from "
16543 & "non-limited interface", Indic);
16544 end if;
16545
16546 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16547 -- from non-limited or limited interfaces.
16548
16549 elsif not Protected_Present (Def)
16550 and then not Synchronized_Present (Def)
16551 and then not Task_Present (Def)
16552 then
16553 if Limited_Present (Iface_Def) then
16554 null;
16555
16556 elsif Protected_Present (Iface_Def) then
16557 Error_Msg_NE
16558 ("descendant of & must be declared as a protected "
16559 & "interface", N, Parent_Type);
16560
16561 elsif Synchronized_Present (Iface_Def) then
16562 Error_Msg_NE
16563 ("descendant of & must be declared as a synchronized "
16564 & "interface", N, Parent_Type);
16565
16566 elsif Task_Present (Iface_Def) then
16567 Error_Msg_NE
16568 ("descendant of & must be declared as a task interface",
16569 N, Parent_Type);
16570 else
16571 null;
16572 end if;
16573 end if;
16574 end if;
16575 end if;
16576
16577 if Is_Tagged_Type (Parent_Type)
16578 and then Is_Concurrent_Type (Parent_Type)
16579 and then not Is_Interface (Parent_Type)
16580 then
16581 Error_Msg_N
16582 ("parent type of a record extension cannot be a synchronized "
16583 & "tagged type (RM 3.9.1 (3/1))", N);
16584 Set_Etype (T, Any_Type);
16585 return;
16586 end if;
16587
16588 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16589 -- interfaces
16590
16591 if Is_Tagged_Type (Parent_Type)
16592 and then Is_Non_Empty_List (Interface_List (Def))
16593 then
16594 declare
16595 Intf : Node_Id;
16596 T : Entity_Id;
16597
16598 begin
16599 Intf := First (Interface_List (Def));
16600 while Present (Intf) loop
16601 T := Find_Type_Of_Subtype_Indic (Intf);
16602
16603 if not Is_Interface (T) then
16604 Diagnose_Interface (Intf, T);
16605
16606 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16607 -- a limited type from having a nonlimited progenitor.
16608
16609 elsif (Limited_Present (Def)
16610 or else (not Is_Interface (Parent_Type)
16611 and then Is_Limited_Type (Parent_Type)))
16612 and then not Is_Limited_Interface (T)
16613 then
16614 Error_Msg_NE
16615 ("progenitor interface& of limited type must be limited",
16616 N, T);
16617 end if;
16618
16619 Next (Intf);
16620 end loop;
16621 end;
16622 end if;
16623
16624 if Parent_Type = Any_Type
16625 or else Etype (Parent_Type) = Any_Type
16626 or else (Is_Class_Wide_Type (Parent_Type)
16627 and then Etype (Parent_Type) = T)
16628 then
16629 -- If Parent_Type is undefined or illegal, make new type into a
16630 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16631 -- errors. If this is a self-definition, emit error now.
16632
16633 if T = Parent_Type or else T = Etype (Parent_Type) then
16634 Error_Msg_N ("type cannot be used in its own definition", Indic);
16635 end if;
16636
16637 Set_Ekind (T, Ekind (Parent_Type));
16638 Set_Etype (T, Any_Type);
16639 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16640
16641 if Is_Tagged_Type (T)
16642 and then Is_Record_Type (T)
16643 then
16644 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16645 end if;
16646
16647 return;
16648 end if;
16649
16650 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16651 -- an interface is special because the list of interfaces in the full
16652 -- view can be given in any order. For example:
16653
16654 -- type A is interface;
16655 -- type B is interface and A;
16656 -- type D is new B with private;
16657 -- private
16658 -- type D is new A and B with null record; -- 1 --
16659
16660 -- In this case we perform the following transformation of -1-:
16661
16662 -- type D is new B and A with null record;
16663
16664 -- If the parent of the full-view covers the parent of the partial-view
16665 -- we have two possible cases:
16666
16667 -- 1) They have the same parent
16668 -- 2) The parent of the full-view implements some further interfaces
16669
16670 -- In both cases we do not need to perform the transformation. In the
16671 -- first case the source program is correct and the transformation is
16672 -- not needed; in the second case the source program does not fulfill
16673 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16674 -- later.
16675
16676 -- This transformation not only simplifies the rest of the analysis of
16677 -- this type declaration but also simplifies the correct generation of
16678 -- the object layout to the expander.
16679
16680 if In_Private_Part (Current_Scope)
16681 and then Is_Interface (Parent_Type)
16682 then
16683 declare
16684 Iface : Node_Id;
16685 Partial_View : Entity_Id;
16686 Partial_View_Parent : Entity_Id;
16687 New_Iface : Node_Id;
16688
16689 begin
16690 -- Look for the associated private type declaration
16691
16692 Partial_View := Incomplete_Or_Partial_View (T);
16693
16694 -- If the partial view was not found then the source code has
16695 -- errors and the transformation is not needed.
16696
16697 if Present (Partial_View) then
16698 Partial_View_Parent := Etype (Partial_View);
16699
16700 -- If the parent of the full-view covers the parent of the
16701 -- partial-view we have nothing else to do.
16702
16703 if Interface_Present_In_Ancestor
16704 (Parent_Type, Partial_View_Parent)
16705 then
16706 null;
16707
16708 -- Traverse the list of interfaces of the full-view to look
16709 -- for the parent of the partial-view and perform the tree
16710 -- transformation.
16711
16712 else
16713 Iface := First (Interface_List (Def));
16714 while Present (Iface) loop
16715 if Etype (Iface) = Etype (Partial_View) then
16716 Rewrite (Subtype_Indication (Def),
16717 New_Copy (Subtype_Indication
16718 (Parent (Partial_View))));
16719
16720 New_Iface :=
16721 Make_Identifier (Sloc (N), Chars (Parent_Type));
16722 Append (New_Iface, Interface_List (Def));
16723
16724 -- Analyze the transformed code
16725
16726 Derived_Type_Declaration (T, N, Is_Completion);
16727 return;
16728 end if;
16729
16730 Next (Iface);
16731 end loop;
16732 end if;
16733 end if;
16734 end;
16735 end if;
16736
16737 -- Only composite types other than array types are allowed to have
16738 -- discriminants.
16739
16740 if Present (Discriminant_Specifications (N)) then
16741 if (Is_Elementary_Type (Parent_Type)
16742 or else
16743 Is_Array_Type (Parent_Type))
16744 and then not Error_Posted (N)
16745 then
16746 Error_Msg_N
16747 ("elementary or array type cannot have discriminants",
16748 Defining_Identifier (First (Discriminant_Specifications (N))));
16749
16750 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16751 -- only if we are not already processing a malformed syntax tree.
16752
16753 if Is_Type (T) then
16754 Set_Has_Discriminants (T, False);
16755 end if;
16756
16757 -- The type is allowed to have discriminants
16758
16759 else
16760 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16761 end if;
16762 end if;
16763
16764 -- In Ada 83, a derived type defined in a package specification cannot
16765 -- be used for further derivation until the end of its visible part.
16766 -- Note that derivation in the private part of the package is allowed.
16767
16768 if Ada_Version = Ada_83
16769 and then Is_Derived_Type (Parent_Type)
16770 and then In_Visible_Part (Scope (Parent_Type))
16771 then
16772 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16773 Error_Msg_N
16774 ("(Ada 83): premature use of type for derivation", Indic);
16775 end if;
16776 end if;
16777
16778 -- Check for early use of incomplete or private type
16779
16780 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16781 Error_Msg_N ("premature derivation of incomplete type", Indic);
16782 return;
16783
16784 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16785 and then not Comes_From_Generic (Parent_Type))
16786 or else Has_Private_Component (Parent_Type)
16787 then
16788 -- The ancestor type of a formal type can be incomplete, in which
16789 -- case only the operations of the partial view are available in the
16790 -- generic. Subsequent checks may be required when the full view is
16791 -- analyzed to verify that a derivation from a tagged type has an
16792 -- extension.
16793
16794 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16795 null;
16796
16797 elsif No (Underlying_Type (Parent_Type))
16798 or else Has_Private_Component (Parent_Type)
16799 then
16800 Error_Msg_N
16801 ("premature derivation of derived or private type", Indic);
16802
16803 -- Flag the type itself as being in error, this prevents some
16804 -- nasty problems with subsequent uses of the malformed type.
16805
16806 Set_Error_Posted (T);
16807
16808 -- Check that within the immediate scope of an untagged partial
16809 -- view it's illegal to derive from the partial view if the
16810 -- full view is tagged. (7.3(7))
16811
16812 -- We verify that the Parent_Type is a partial view by checking
16813 -- that it is not a Full_Type_Declaration (i.e. a private type or
16814 -- private extension declaration), to distinguish a partial view
16815 -- from a derivation from a private type which also appears as
16816 -- E_Private_Type. If the parent base type is not declared in an
16817 -- enclosing scope there is no need to check.
16818
16819 elsif Present (Full_View (Parent_Type))
16820 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16821 and then not Is_Tagged_Type (Parent_Type)
16822 and then Is_Tagged_Type (Full_View (Parent_Type))
16823 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16824 then
16825 Error_Msg_N
16826 ("premature derivation from type with tagged full view",
16827 Indic);
16828 end if;
16829 end if;
16830
16831 -- Check that form of derivation is appropriate
16832
16833 Taggd := Is_Tagged_Type (Parent_Type);
16834
16835 -- Set the parent type to the class-wide type's specific type in this
16836 -- case to prevent cascading errors
16837
16838 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16839 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16840 Set_Etype (T, Etype (Parent_Type));
16841 return;
16842 end if;
16843
16844 if Present (Extension) and then not Taggd then
16845 Error_Msg_N
16846 ("type derived from untagged type cannot have extension", Indic);
16847
16848 elsif No (Extension) and then Taggd then
16849
16850 -- If this declaration is within a private part (or body) of a
16851 -- generic instantiation then the derivation is allowed (the parent
16852 -- type can only appear tagged in this case if it's a generic actual
16853 -- type, since it would otherwise have been rejected in the analysis
16854 -- of the generic template).
16855
16856 if not Is_Generic_Actual_Type (Parent_Type)
16857 or else In_Visible_Part (Scope (Parent_Type))
16858 then
16859 if Is_Class_Wide_Type (Parent_Type) then
16860 Error_Msg_N
16861 ("parent type must not be a class-wide type", Indic);
16862
16863 -- Use specific type to prevent cascaded errors.
16864
16865 Parent_Type := Etype (Parent_Type);
16866
16867 else
16868 Error_Msg_N
16869 ("type derived from tagged type must have extension", Indic);
16870 end if;
16871 end if;
16872 end if;
16873
16874 -- AI-443: Synchronized formal derived types require a private
16875 -- extension. There is no point in checking the ancestor type or
16876 -- the progenitors since the construct is wrong to begin with.
16877
16878 if Ada_Version >= Ada_2005
16879 and then Is_Generic_Type (T)
16880 and then Present (Original_Node (N))
16881 then
16882 declare
16883 Decl : constant Node_Id := Original_Node (N);
16884
16885 begin
16886 if Nkind (Decl) = N_Formal_Type_Declaration
16887 and then Nkind (Formal_Type_Definition (Decl)) =
16888 N_Formal_Derived_Type_Definition
16889 and then Synchronized_Present (Formal_Type_Definition (Decl))
16890 and then No (Extension)
16891
16892 -- Avoid emitting a duplicate error message
16893
16894 and then not Error_Posted (Indic)
16895 then
16896 Error_Msg_N
16897 ("synchronized derived type must have extension", N);
16898 end if;
16899 end;
16900 end if;
16901
16902 if Null_Exclusion_Present (Def)
16903 and then not Is_Access_Type (Parent_Type)
16904 then
16905 Error_Msg_N ("null exclusion can only apply to an access type", N);
16906 end if;
16907
16908 -- Avoid deriving parent primitives of underlying record views
16909
16910 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16911 Derive_Subps => not Is_Underlying_Record_View (T));
16912
16913 -- AI-419: The parent type of an explicitly limited derived type must
16914 -- be a limited type or a limited interface.
16915
16916 if Limited_Present (Def) then
16917 Set_Is_Limited_Record (T);
16918
16919 if Is_Interface (T) then
16920 Set_Is_Limited_Interface (T);
16921 end if;
16922
16923 if not Is_Limited_Type (Parent_Type)
16924 and then
16925 (not Is_Interface (Parent_Type)
16926 or else not Is_Limited_Interface (Parent_Type))
16927 then
16928 -- AI05-0096: a derivation in the private part of an instance is
16929 -- legal if the generic formal is untagged limited, and the actual
16930 -- is non-limited.
16931
16932 if Is_Generic_Actual_Type (Parent_Type)
16933 and then In_Private_Part (Current_Scope)
16934 and then
16935 not Is_Tagged_Type
16936 (Generic_Parent_Type (Parent (Parent_Type)))
16937 then
16938 null;
16939
16940 else
16941 Error_Msg_NE
16942 ("parent type& of limited type must be limited",
16943 N, Parent_Type);
16944 end if;
16945 end if;
16946 end if;
16947
16948 -- In SPARK, there are no derived type definitions other than type
16949 -- extensions of tagged record types.
16950
16951 if No (Extension) then
16952 Check_SPARK_05_Restriction
16953 ("derived type is not allowed", Original_Node (N));
16954 end if;
16955 end Derived_Type_Declaration;
16956
16957 ------------------------
16958 -- Diagnose_Interface --
16959 ------------------------
16960
16961 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16962 begin
16963 if not Is_Interface (E) and then E /= Any_Type then
16964 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16965 end if;
16966 end Diagnose_Interface;
16967
16968 ----------------------------------
16969 -- Enumeration_Type_Declaration --
16970 ----------------------------------
16971
16972 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16973 Ev : Uint;
16974 L : Node_Id;
16975 R_Node : Node_Id;
16976 B_Node : Node_Id;
16977
16978 begin
16979 -- Create identifier node representing lower bound
16980
16981 B_Node := New_Node (N_Identifier, Sloc (Def));
16982 L := First (Literals (Def));
16983 Set_Chars (B_Node, Chars (L));
16984 Set_Entity (B_Node, L);
16985 Set_Etype (B_Node, T);
16986 Set_Is_Static_Expression (B_Node, True);
16987
16988 R_Node := New_Node (N_Range, Sloc (Def));
16989 Set_Low_Bound (R_Node, B_Node);
16990
16991 Set_Ekind (T, E_Enumeration_Type);
16992 Set_First_Literal (T, L);
16993 Set_Etype (T, T);
16994 Set_Is_Constrained (T);
16995
16996 Ev := Uint_0;
16997
16998 -- Loop through literals of enumeration type setting pos and rep values
16999 -- except that if the Ekind is already set, then it means the literal
17000 -- was already constructed (case of a derived type declaration and we
17001 -- should not disturb the Pos and Rep values.
17002
17003 while Present (L) loop
17004 if Ekind (L) /= E_Enumeration_Literal then
17005 Set_Ekind (L, E_Enumeration_Literal);
17006 Set_Enumeration_Pos (L, Ev);
17007 Set_Enumeration_Rep (L, Ev);
17008 Set_Is_Known_Valid (L, True);
17009 end if;
17010
17011 Set_Etype (L, T);
17012 New_Overloaded_Entity (L);
17013 Generate_Definition (L);
17014 Set_Convention (L, Convention_Intrinsic);
17015
17016 -- Case of character literal
17017
17018 if Nkind (L) = N_Defining_Character_Literal then
17019 Set_Is_Character_Type (T, True);
17020
17021 -- Check violation of No_Wide_Characters
17022
17023 if Restriction_Check_Required (No_Wide_Characters) then
17024 Get_Name_String (Chars (L));
17025
17026 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17027 Check_Restriction (No_Wide_Characters, L);
17028 end if;
17029 end if;
17030 end if;
17031
17032 Ev := Ev + 1;
17033 Next (L);
17034 end loop;
17035
17036 -- Now create a node representing upper bound
17037
17038 B_Node := New_Node (N_Identifier, Sloc (Def));
17039 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17040 Set_Entity (B_Node, Last (Literals (Def)));
17041 Set_Etype (B_Node, T);
17042 Set_Is_Static_Expression (B_Node, True);
17043
17044 Set_High_Bound (R_Node, B_Node);
17045
17046 -- Initialize various fields of the type. Some of this information
17047 -- may be overwritten later through rep.clauses.
17048
17049 Set_Scalar_Range (T, R_Node);
17050 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17051 Set_Enum_Esize (T);
17052 Set_Enum_Pos_To_Rep (T, Empty);
17053
17054 -- Set Discard_Names if configuration pragma set, or if there is
17055 -- a parameterless pragma in the current declarative region
17056
17057 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17058 Set_Discard_Names (T);
17059 end if;
17060
17061 -- Process end label if there is one
17062
17063 if Present (Def) then
17064 Process_End_Label (Def, 'e', T);
17065 end if;
17066 end Enumeration_Type_Declaration;
17067
17068 ---------------------------------
17069 -- Expand_To_Stored_Constraint --
17070 ---------------------------------
17071
17072 function Expand_To_Stored_Constraint
17073 (Typ : Entity_Id;
17074 Constraint : Elist_Id) return Elist_Id
17075 is
17076 Explicitly_Discriminated_Type : Entity_Id;
17077 Expansion : Elist_Id;
17078 Discriminant : Entity_Id;
17079
17080 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17081 -- Find the nearest type that actually specifies discriminants
17082
17083 ---------------------------------
17084 -- Type_With_Explicit_Discrims --
17085 ---------------------------------
17086
17087 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17088 Typ : constant E := Base_Type (Id);
17089
17090 begin
17091 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17092 if Present (Full_View (Typ)) then
17093 return Type_With_Explicit_Discrims (Full_View (Typ));
17094 end if;
17095
17096 else
17097 if Has_Discriminants (Typ) then
17098 return Typ;
17099 end if;
17100 end if;
17101
17102 if Etype (Typ) = Typ then
17103 return Empty;
17104 elsif Has_Discriminants (Typ) then
17105 return Typ;
17106 else
17107 return Type_With_Explicit_Discrims (Etype (Typ));
17108 end if;
17109
17110 end Type_With_Explicit_Discrims;
17111
17112 -- Start of processing for Expand_To_Stored_Constraint
17113
17114 begin
17115 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17116 return No_Elist;
17117 end if;
17118
17119 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17120
17121 if No (Explicitly_Discriminated_Type) then
17122 return No_Elist;
17123 end if;
17124
17125 Expansion := New_Elmt_List;
17126
17127 Discriminant :=
17128 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17129 while Present (Discriminant) loop
17130 Append_Elmt
17131 (Get_Discriminant_Value
17132 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17133 To => Expansion);
17134 Next_Stored_Discriminant (Discriminant);
17135 end loop;
17136
17137 return Expansion;
17138 end Expand_To_Stored_Constraint;
17139
17140 ---------------------------
17141 -- Find_Hidden_Interface --
17142 ---------------------------
17143
17144 function Find_Hidden_Interface
17145 (Src : Elist_Id;
17146 Dest : Elist_Id) return Entity_Id
17147 is
17148 Iface : Entity_Id;
17149 Iface_Elmt : Elmt_Id;
17150
17151 begin
17152 if Present (Src) and then Present (Dest) then
17153 Iface_Elmt := First_Elmt (Src);
17154 while Present (Iface_Elmt) loop
17155 Iface := Node (Iface_Elmt);
17156
17157 if Is_Interface (Iface)
17158 and then not Contain_Interface (Iface, Dest)
17159 then
17160 return Iface;
17161 end if;
17162
17163 Next_Elmt (Iface_Elmt);
17164 end loop;
17165 end if;
17166
17167 return Empty;
17168 end Find_Hidden_Interface;
17169
17170 --------------------
17171 -- Find_Type_Name --
17172 --------------------
17173
17174 function Find_Type_Name (N : Node_Id) return Entity_Id is
17175 Id : constant Entity_Id := Defining_Identifier (N);
17176 New_Id : Entity_Id;
17177 Prev : Entity_Id;
17178 Prev_Par : Node_Id;
17179
17180 procedure Check_Duplicate_Aspects;
17181 -- Check that aspects specified in a completion have not been specified
17182 -- already in the partial view.
17183
17184 procedure Tag_Mismatch;
17185 -- Diagnose a tagged partial view whose full view is untagged. We post
17186 -- the message on the full view, with a reference to the previous
17187 -- partial view. The partial view can be private or incomplete, and
17188 -- these are handled in a different manner, so we determine the position
17189 -- of the error message from the respective slocs of both.
17190
17191 -----------------------------
17192 -- Check_Duplicate_Aspects --
17193 -----------------------------
17194
17195 procedure Check_Duplicate_Aspects is
17196 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17197 -- Return the corresponding aspect of the partial view which matches
17198 -- the aspect id of Asp. Return Empty is no such aspect exists.
17199
17200 -----------------------------
17201 -- Get_Partial_View_Aspect --
17202 -----------------------------
17203
17204 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17205 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17206 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17207 Prev_Asp : Node_Id;
17208
17209 begin
17210 if Present (Prev_Asps) then
17211 Prev_Asp := First (Prev_Asps);
17212 while Present (Prev_Asp) loop
17213 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17214 return Prev_Asp;
17215 end if;
17216
17217 Next (Prev_Asp);
17218 end loop;
17219 end if;
17220
17221 return Empty;
17222 end Get_Partial_View_Aspect;
17223
17224 -- Local variables
17225
17226 Full_Asps : constant List_Id := Aspect_Specifications (N);
17227 Full_Asp : Node_Id;
17228 Part_Asp : Node_Id;
17229
17230 -- Start of processing for Check_Duplicate_Aspects
17231
17232 begin
17233 if Present (Full_Asps) then
17234 Full_Asp := First (Full_Asps);
17235 while Present (Full_Asp) loop
17236 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17237
17238 -- An aspect and its class-wide counterpart are two distinct
17239 -- aspects and may apply to both views of an entity.
17240
17241 if Present (Part_Asp)
17242 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17243 then
17244 Error_Msg_N
17245 ("aspect already specified in private declaration",
17246 Full_Asp);
17247
17248 Remove (Full_Asp);
17249 return;
17250 end if;
17251
17252 if Has_Discriminants (Prev)
17253 and then not Has_Unknown_Discriminants (Prev)
17254 and then Get_Aspect_Id (Full_Asp) =
17255 Aspect_Implicit_Dereference
17256 then
17257 Error_Msg_N
17258 ("cannot specify aspect if partial view has known "
17259 & "discriminants", Full_Asp);
17260 end if;
17261
17262 Next (Full_Asp);
17263 end loop;
17264 end if;
17265 end Check_Duplicate_Aspects;
17266
17267 ------------------
17268 -- Tag_Mismatch --
17269 ------------------
17270
17271 procedure Tag_Mismatch is
17272 begin
17273 if Sloc (Prev) < Sloc (Id) then
17274 if Ada_Version >= Ada_2012
17275 and then Nkind (N) = N_Private_Type_Declaration
17276 then
17277 Error_Msg_NE
17278 ("declaration of private } must be a tagged type ", Id, Prev);
17279 else
17280 Error_Msg_NE
17281 ("full declaration of } must be a tagged type ", Id, Prev);
17282 end if;
17283
17284 else
17285 if Ada_Version >= Ada_2012
17286 and then Nkind (N) = N_Private_Type_Declaration
17287 then
17288 Error_Msg_NE
17289 ("declaration of private } must be a tagged type ", Prev, Id);
17290 else
17291 Error_Msg_NE
17292 ("full declaration of } must be a tagged type ", Prev, Id);
17293 end if;
17294 end if;
17295 end Tag_Mismatch;
17296
17297 -- Start of processing for Find_Type_Name
17298
17299 begin
17300 -- Find incomplete declaration, if one was given
17301
17302 Prev := Current_Entity_In_Scope (Id);
17303
17304 -- New type declaration
17305
17306 if No (Prev) then
17307 Enter_Name (Id);
17308 return Id;
17309
17310 -- Previous declaration exists
17311
17312 else
17313 Prev_Par := Parent (Prev);
17314
17315 -- Error if not incomplete/private case except if previous
17316 -- declaration is implicit, etc. Enter_Name will emit error if
17317 -- appropriate.
17318
17319 if not Is_Incomplete_Or_Private_Type (Prev) then
17320 Enter_Name (Id);
17321 New_Id := Id;
17322
17323 -- Check invalid completion of private or incomplete type
17324
17325 elsif not Nkind_In (N, N_Full_Type_Declaration,
17326 N_Task_Type_Declaration,
17327 N_Protected_Type_Declaration)
17328 and then
17329 (Ada_Version < Ada_2012
17330 or else not Is_Incomplete_Type (Prev)
17331 or else not Nkind_In (N, N_Private_Type_Declaration,
17332 N_Private_Extension_Declaration))
17333 then
17334 -- Completion must be a full type declarations (RM 7.3(4))
17335
17336 Error_Msg_Sloc := Sloc (Prev);
17337 Error_Msg_NE ("invalid completion of }", Id, Prev);
17338
17339 -- Set scope of Id to avoid cascaded errors. Entity is never
17340 -- examined again, except when saving globals in generics.
17341
17342 Set_Scope (Id, Current_Scope);
17343 New_Id := Id;
17344
17345 -- If this is a repeated incomplete declaration, no further
17346 -- checks are possible.
17347
17348 if Nkind (N) = N_Incomplete_Type_Declaration then
17349 return Prev;
17350 end if;
17351
17352 -- Case of full declaration of incomplete type
17353
17354 elsif Ekind (Prev) = E_Incomplete_Type
17355 and then (Ada_Version < Ada_2012
17356 or else No (Full_View (Prev))
17357 or else not Is_Private_Type (Full_View (Prev)))
17358 then
17359 -- Indicate that the incomplete declaration has a matching full
17360 -- declaration. The defining occurrence of the incomplete
17361 -- declaration remains the visible one, and the procedure
17362 -- Get_Full_View dereferences it whenever the type is used.
17363
17364 if Present (Full_View (Prev)) then
17365 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17366 end if;
17367
17368 Set_Full_View (Prev, Id);
17369 Append_Entity (Id, Current_Scope);
17370 Set_Is_Public (Id, Is_Public (Prev));
17371 Set_Is_Internal (Id);
17372 New_Id := Prev;
17373
17374 -- If the incomplete view is tagged, a class_wide type has been
17375 -- created already. Use it for the private type as well, in order
17376 -- to prevent multiple incompatible class-wide types that may be
17377 -- created for self-referential anonymous access components.
17378
17379 if Is_Tagged_Type (Prev)
17380 and then Present (Class_Wide_Type (Prev))
17381 then
17382 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17383 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17384
17385 -- Type of the class-wide type is the current Id. Previously
17386 -- this was not done for private declarations because of order-
17387 -- of-elaboration issues in the back end, but gigi now handles
17388 -- this properly.
17389
17390 Set_Etype (Class_Wide_Type (Id), Id);
17391 end if;
17392
17393 -- Case of full declaration of private type
17394
17395 else
17396 -- If the private type was a completion of an incomplete type then
17397 -- update Prev to reference the private type
17398
17399 if Ada_Version >= Ada_2012
17400 and then Ekind (Prev) = E_Incomplete_Type
17401 and then Present (Full_View (Prev))
17402 and then Is_Private_Type (Full_View (Prev))
17403 then
17404 Prev := Full_View (Prev);
17405 Prev_Par := Parent (Prev);
17406 end if;
17407
17408 if Nkind (N) = N_Full_Type_Declaration
17409 and then Nkind_In
17410 (Type_Definition (N), N_Record_Definition,
17411 N_Derived_Type_Definition)
17412 and then Interface_Present (Type_Definition (N))
17413 then
17414 Error_Msg_N
17415 ("completion of private type cannot be an interface", N);
17416 end if;
17417
17418 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17419 if Etype (Prev) /= Prev then
17420
17421 -- Prev is a private subtype or a derived type, and needs
17422 -- no completion.
17423
17424 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17425 New_Id := Id;
17426
17427 elsif Ekind (Prev) = E_Private_Type
17428 and then Nkind_In (N, N_Task_Type_Declaration,
17429 N_Protected_Type_Declaration)
17430 then
17431 Error_Msg_N
17432 ("completion of nonlimited type cannot be limited", N);
17433
17434 elsif Ekind (Prev) = E_Record_Type_With_Private
17435 and then Nkind_In (N, N_Task_Type_Declaration,
17436 N_Protected_Type_Declaration)
17437 then
17438 if not Is_Limited_Record (Prev) then
17439 Error_Msg_N
17440 ("completion of nonlimited type cannot be limited", N);
17441
17442 elsif No (Interface_List (N)) then
17443 Error_Msg_N
17444 ("completion of tagged private type must be tagged",
17445 N);
17446 end if;
17447 end if;
17448
17449 -- Ada 2005 (AI-251): Private extension declaration of a task
17450 -- type or a protected type. This case arises when covering
17451 -- interface types.
17452
17453 elsif Nkind_In (N, N_Task_Type_Declaration,
17454 N_Protected_Type_Declaration)
17455 then
17456 null;
17457
17458 elsif Nkind (N) /= N_Full_Type_Declaration
17459 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17460 then
17461 Error_Msg_N
17462 ("full view of private extension must be an extension", N);
17463
17464 elsif not (Abstract_Present (Parent (Prev)))
17465 and then Abstract_Present (Type_Definition (N))
17466 then
17467 Error_Msg_N
17468 ("full view of non-abstract extension cannot be abstract", N);
17469 end if;
17470
17471 if not In_Private_Part (Current_Scope) then
17472 Error_Msg_N
17473 ("declaration of full view must appear in private part", N);
17474 end if;
17475
17476 if Ada_Version >= Ada_2012 then
17477 Check_Duplicate_Aspects;
17478 end if;
17479
17480 Copy_And_Swap (Prev, Id);
17481 Set_Has_Private_Declaration (Prev);
17482 Set_Has_Private_Declaration (Id);
17483
17484 -- AI12-0133: Indicate whether we have a partial view with
17485 -- unknown discriminants, in which case initialization of objects
17486 -- of the type do not receive an invariant check.
17487
17488 Set_Partial_View_Has_Unknown_Discr
17489 (Prev, Has_Unknown_Discriminants (Id));
17490
17491 -- Preserve aspect and iterator flags that may have been set on
17492 -- the partial view.
17493
17494 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17495 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17496
17497 -- If no error, propagate freeze_node from private to full view.
17498 -- It may have been generated for an early operational item.
17499
17500 if Present (Freeze_Node (Id))
17501 and then Serious_Errors_Detected = 0
17502 and then No (Full_View (Id))
17503 then
17504 Set_Freeze_Node (Prev, Freeze_Node (Id));
17505 Set_Freeze_Node (Id, Empty);
17506 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17507 end if;
17508
17509 Set_Full_View (Id, Prev);
17510 New_Id := Prev;
17511 end if;
17512
17513 -- Verify that full declaration conforms to partial one
17514
17515 if Is_Incomplete_Or_Private_Type (Prev)
17516 and then Present (Discriminant_Specifications (Prev_Par))
17517 then
17518 if Present (Discriminant_Specifications (N)) then
17519 if Ekind (Prev) = E_Incomplete_Type then
17520 Check_Discriminant_Conformance (N, Prev, Prev);
17521 else
17522 Check_Discriminant_Conformance (N, Prev, Id);
17523 end if;
17524
17525 else
17526 Error_Msg_N
17527 ("missing discriminants in full type declaration", N);
17528
17529 -- To avoid cascaded errors on subsequent use, share the
17530 -- discriminants of the partial view.
17531
17532 Set_Discriminant_Specifications (N,
17533 Discriminant_Specifications (Prev_Par));
17534 end if;
17535 end if;
17536
17537 -- A prior untagged partial view can have an associated class-wide
17538 -- type due to use of the class attribute, and in this case the full
17539 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17540 -- of incomplete tagged declarations, but we check for it.
17541
17542 if Is_Type (Prev)
17543 and then (Is_Tagged_Type (Prev)
17544 or else Present (Class_Wide_Type (Prev)))
17545 then
17546 -- Ada 2012 (AI05-0162): A private type may be the completion of
17547 -- an incomplete type.
17548
17549 if Ada_Version >= Ada_2012
17550 and then Is_Incomplete_Type (Prev)
17551 and then Nkind_In (N, N_Private_Type_Declaration,
17552 N_Private_Extension_Declaration)
17553 then
17554 -- No need to check private extensions since they are tagged
17555
17556 if Nkind (N) = N_Private_Type_Declaration
17557 and then not Tagged_Present (N)
17558 then
17559 Tag_Mismatch;
17560 end if;
17561
17562 -- The full declaration is either a tagged type (including
17563 -- a synchronized type that implements interfaces) or a
17564 -- type extension, otherwise this is an error.
17565
17566 elsif Nkind_In (N, N_Task_Type_Declaration,
17567 N_Protected_Type_Declaration)
17568 then
17569 if No (Interface_List (N)) and then not Error_Posted (N) then
17570 Tag_Mismatch;
17571 end if;
17572
17573 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17574
17575 -- Indicate that the previous declaration (tagged incomplete
17576 -- or private declaration) requires the same on the full one.
17577
17578 if not Tagged_Present (Type_Definition (N)) then
17579 Tag_Mismatch;
17580 Set_Is_Tagged_Type (Id);
17581 end if;
17582
17583 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17584 if No (Record_Extension_Part (Type_Definition (N))) then
17585 Error_Msg_NE
17586 ("full declaration of } must be a record extension",
17587 Prev, Id);
17588
17589 -- Set some attributes to produce a usable full view
17590
17591 Set_Is_Tagged_Type (Id);
17592 end if;
17593
17594 else
17595 Tag_Mismatch;
17596 end if;
17597 end if;
17598
17599 if Present (Prev)
17600 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17601 and then Present (Premature_Use (Parent (Prev)))
17602 then
17603 Error_Msg_Sloc := Sloc (N);
17604 Error_Msg_N
17605 ("\full declaration #", Premature_Use (Parent (Prev)));
17606 end if;
17607
17608 return New_Id;
17609 end if;
17610 end Find_Type_Name;
17611
17612 -------------------------
17613 -- Find_Type_Of_Object --
17614 -------------------------
17615
17616 function Find_Type_Of_Object
17617 (Obj_Def : Node_Id;
17618 Related_Nod : Node_Id) return Entity_Id
17619 is
17620 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17621 P : Node_Id := Parent (Obj_Def);
17622 T : Entity_Id;
17623 Nam : Name_Id;
17624
17625 begin
17626 -- If the parent is a component_definition node we climb to the
17627 -- component_declaration node
17628
17629 if Nkind (P) = N_Component_Definition then
17630 P := Parent (P);
17631 end if;
17632
17633 -- Case of an anonymous array subtype
17634
17635 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17636 N_Unconstrained_Array_Definition)
17637 then
17638 T := Empty;
17639 Array_Type_Declaration (T, Obj_Def);
17640
17641 -- Create an explicit subtype whenever possible
17642
17643 elsif Nkind (P) /= N_Component_Declaration
17644 and then Def_Kind = N_Subtype_Indication
17645 then
17646 -- Base name of subtype on object name, which will be unique in
17647 -- the current scope.
17648
17649 -- If this is a duplicate declaration, return base type, to avoid
17650 -- generating duplicate anonymous types.
17651
17652 if Error_Posted (P) then
17653 Analyze (Subtype_Mark (Obj_Def));
17654 return Entity (Subtype_Mark (Obj_Def));
17655 end if;
17656
17657 Nam :=
17658 New_External_Name
17659 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17660
17661 T := Make_Defining_Identifier (Sloc (P), Nam);
17662
17663 Insert_Action (Obj_Def,
17664 Make_Subtype_Declaration (Sloc (P),
17665 Defining_Identifier => T,
17666 Subtype_Indication => Relocate_Node (Obj_Def)));
17667
17668 -- This subtype may need freezing, and this will not be done
17669 -- automatically if the object declaration is not in declarative
17670 -- part. Since this is an object declaration, the type cannot always
17671 -- be frozen here. Deferred constants do not freeze their type
17672 -- (which often enough will be private).
17673
17674 if Nkind (P) = N_Object_Declaration
17675 and then Constant_Present (P)
17676 and then No (Expression (P))
17677 then
17678 null;
17679
17680 -- Here we freeze the base type of object type to catch premature use
17681 -- of discriminated private type without a full view.
17682
17683 else
17684 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17685 end if;
17686
17687 -- Ada 2005 AI-406: the object definition in an object declaration
17688 -- can be an access definition.
17689
17690 elsif Def_Kind = N_Access_Definition then
17691 T := Access_Definition (Related_Nod, Obj_Def);
17692
17693 Set_Is_Local_Anonymous_Access
17694 (T,
17695 V => (Ada_Version < Ada_2012)
17696 or else (Nkind (P) /= N_Object_Declaration)
17697 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17698
17699 -- Otherwise, the object definition is just a subtype_mark
17700
17701 else
17702 T := Process_Subtype (Obj_Def, Related_Nod);
17703
17704 -- If expansion is disabled an object definition that is an aggregate
17705 -- will not get expanded and may lead to scoping problems in the back
17706 -- end, if the object is referenced in an inner scope. In that case
17707 -- create an itype reference for the object definition now. This
17708 -- may be redundant in some cases, but harmless.
17709
17710 if Is_Itype (T)
17711 and then Nkind (Related_Nod) = N_Object_Declaration
17712 and then ASIS_Mode
17713 then
17714 Build_Itype_Reference (T, Related_Nod);
17715 end if;
17716 end if;
17717
17718 return T;
17719 end Find_Type_Of_Object;
17720
17721 --------------------------------
17722 -- Find_Type_Of_Subtype_Indic --
17723 --------------------------------
17724
17725 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17726 Typ : Entity_Id;
17727
17728 begin
17729 -- Case of subtype mark with a constraint
17730
17731 if Nkind (S) = N_Subtype_Indication then
17732 Find_Type (Subtype_Mark (S));
17733 Typ := Entity (Subtype_Mark (S));
17734
17735 if not
17736 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17737 then
17738 Error_Msg_N
17739 ("incorrect constraint for this kind of type", Constraint (S));
17740 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17741 end if;
17742
17743 -- Otherwise we have a subtype mark without a constraint
17744
17745 elsif Error_Posted (S) then
17746 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17747 return Any_Type;
17748
17749 else
17750 Find_Type (S);
17751 Typ := Entity (S);
17752 end if;
17753
17754 -- Check No_Wide_Characters restriction
17755
17756 Check_Wide_Character_Restriction (Typ, S);
17757
17758 return Typ;
17759 end Find_Type_Of_Subtype_Indic;
17760
17761 -------------------------------------
17762 -- Floating_Point_Type_Declaration --
17763 -------------------------------------
17764
17765 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17766 Digs : constant Node_Id := Digits_Expression (Def);
17767 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17768 Digs_Val : Uint;
17769 Base_Typ : Entity_Id;
17770 Implicit_Base : Entity_Id;
17771 Bound : Node_Id;
17772
17773 function Can_Derive_From (E : Entity_Id) return Boolean;
17774 -- Find if given digits value, and possibly a specified range, allows
17775 -- derivation from specified type
17776
17777 function Find_Base_Type return Entity_Id;
17778 -- Find a predefined base type that Def can derive from, or generate
17779 -- an error and substitute Long_Long_Float if none exists.
17780
17781 ---------------------
17782 -- Can_Derive_From --
17783 ---------------------
17784
17785 function Can_Derive_From (E : Entity_Id) return Boolean is
17786 Spec : constant Entity_Id := Real_Range_Specification (Def);
17787
17788 begin
17789 -- Check specified "digits" constraint
17790
17791 if Digs_Val > Digits_Value (E) then
17792 return False;
17793 end if;
17794
17795 -- Check for matching range, if specified
17796
17797 if Present (Spec) then
17798 if Expr_Value_R (Type_Low_Bound (E)) >
17799 Expr_Value_R (Low_Bound (Spec))
17800 then
17801 return False;
17802 end if;
17803
17804 if Expr_Value_R (Type_High_Bound (E)) <
17805 Expr_Value_R (High_Bound (Spec))
17806 then
17807 return False;
17808 end if;
17809 end if;
17810
17811 return True;
17812 end Can_Derive_From;
17813
17814 --------------------
17815 -- Find_Base_Type --
17816 --------------------
17817
17818 function Find_Base_Type return Entity_Id is
17819 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17820
17821 begin
17822 -- Iterate over the predefined types in order, returning the first
17823 -- one that Def can derive from.
17824
17825 while Present (Choice) loop
17826 if Can_Derive_From (Node (Choice)) then
17827 return Node (Choice);
17828 end if;
17829
17830 Next_Elmt (Choice);
17831 end loop;
17832
17833 -- If we can't derive from any existing type, use Long_Long_Float
17834 -- and give appropriate message explaining the problem.
17835
17836 if Digs_Val > Max_Digs_Val then
17837 -- It might be the case that there is a type with the requested
17838 -- range, just not the combination of digits and range.
17839
17840 Error_Msg_N
17841 ("no predefined type has requested range and precision",
17842 Real_Range_Specification (Def));
17843
17844 else
17845 Error_Msg_N
17846 ("range too large for any predefined type",
17847 Real_Range_Specification (Def));
17848 end if;
17849
17850 return Standard_Long_Long_Float;
17851 end Find_Base_Type;
17852
17853 -- Start of processing for Floating_Point_Type_Declaration
17854
17855 begin
17856 Check_Restriction (No_Floating_Point, Def);
17857
17858 -- Create an implicit base type
17859
17860 Implicit_Base :=
17861 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17862
17863 -- Analyze and verify digits value
17864
17865 Analyze_And_Resolve (Digs, Any_Integer);
17866 Check_Digits_Expression (Digs);
17867 Digs_Val := Expr_Value (Digs);
17868
17869 -- Process possible range spec and find correct type to derive from
17870
17871 Process_Real_Range_Specification (Def);
17872
17873 -- Check that requested number of digits is not too high.
17874
17875 if Digs_Val > Max_Digs_Val then
17876
17877 -- The check for Max_Base_Digits may be somewhat expensive, as it
17878 -- requires reading System, so only do it when necessary.
17879
17880 declare
17881 Max_Base_Digits : constant Uint :=
17882 Expr_Value
17883 (Expression
17884 (Parent (RTE (RE_Max_Base_Digits))));
17885
17886 begin
17887 if Digs_Val > Max_Base_Digits then
17888 Error_Msg_Uint_1 := Max_Base_Digits;
17889 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17890
17891 elsif No (Real_Range_Specification (Def)) then
17892 Error_Msg_Uint_1 := Max_Digs_Val;
17893 Error_Msg_N ("types with more than ^ digits need range spec "
17894 & "(RM 3.5.7(6))", Digs);
17895 end if;
17896 end;
17897 end if;
17898
17899 -- Find a suitable type to derive from or complain and use a substitute
17900
17901 Base_Typ := Find_Base_Type;
17902
17903 -- If there are bounds given in the declaration use them as the bounds
17904 -- of the type, otherwise use the bounds of the predefined base type
17905 -- that was chosen based on the Digits value.
17906
17907 if Present (Real_Range_Specification (Def)) then
17908 Set_Scalar_Range (T, Real_Range_Specification (Def));
17909 Set_Is_Constrained (T);
17910
17911 -- The bounds of this range must be converted to machine numbers
17912 -- in accordance with RM 4.9(38).
17913
17914 Bound := Type_Low_Bound (T);
17915
17916 if Nkind (Bound) = N_Real_Literal then
17917 Set_Realval
17918 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17919 Set_Is_Machine_Number (Bound);
17920 end if;
17921
17922 Bound := Type_High_Bound (T);
17923
17924 if Nkind (Bound) = N_Real_Literal then
17925 Set_Realval
17926 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17927 Set_Is_Machine_Number (Bound);
17928 end if;
17929
17930 else
17931 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17932 end if;
17933
17934 -- Complete definition of implicit base and declared first subtype. The
17935 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17936 -- are not clobbered when the floating point type acts as a full view of
17937 -- a private type.
17938
17939 Set_Etype (Implicit_Base, Base_Typ);
17940 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17941 Set_Size_Info (Implicit_Base, Base_Typ);
17942 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17943 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17944 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17945 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17946
17947 Set_Ekind (T, E_Floating_Point_Subtype);
17948 Set_Etype (T, Implicit_Base);
17949 Set_Size_Info (T, Implicit_Base);
17950 Set_RM_Size (T, RM_Size (Implicit_Base));
17951 Inherit_Rep_Item_Chain (T, Implicit_Base);
17952 Set_Digits_Value (T, Digs_Val);
17953 end Floating_Point_Type_Declaration;
17954
17955 ----------------------------
17956 -- Get_Discriminant_Value --
17957 ----------------------------
17958
17959 -- This is the situation:
17960
17961 -- There is a non-derived type
17962
17963 -- type T0 (Dx, Dy, Dz...)
17964
17965 -- There are zero or more levels of derivation, with each derivation
17966 -- either purely inheriting the discriminants, or defining its own.
17967
17968 -- type Ti is new Ti-1
17969 -- or
17970 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17971 -- or
17972 -- subtype Ti is ...
17973
17974 -- The subtype issue is avoided by the use of Original_Record_Component,
17975 -- and the fact that derived subtypes also derive the constraints.
17976
17977 -- This chain leads back from
17978
17979 -- Typ_For_Constraint
17980
17981 -- Typ_For_Constraint has discriminants, and the value for each
17982 -- discriminant is given by its corresponding Elmt of Constraints.
17983
17984 -- Discriminant is some discriminant in this hierarchy
17985
17986 -- We need to return its value
17987
17988 -- We do this by recursively searching each level, and looking for
17989 -- Discriminant. Once we get to the bottom, we start backing up
17990 -- returning the value for it which may in turn be a discriminant
17991 -- further up, so on the backup we continue the substitution.
17992
17993 function Get_Discriminant_Value
17994 (Discriminant : Entity_Id;
17995 Typ_For_Constraint : Entity_Id;
17996 Constraint : Elist_Id) return Node_Id
17997 is
17998 function Root_Corresponding_Discriminant
17999 (Discr : Entity_Id) return Entity_Id;
18000 -- Given a discriminant, traverse the chain of inherited discriminants
18001 -- and return the topmost discriminant.
18002
18003 function Search_Derivation_Levels
18004 (Ti : Entity_Id;
18005 Discrim_Values : Elist_Id;
18006 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18007 -- This is the routine that performs the recursive search of levels
18008 -- as described above.
18009
18010 -------------------------------------
18011 -- Root_Corresponding_Discriminant --
18012 -------------------------------------
18013
18014 function Root_Corresponding_Discriminant
18015 (Discr : Entity_Id) return Entity_Id
18016 is
18017 D : Entity_Id;
18018
18019 begin
18020 D := Discr;
18021 while Present (Corresponding_Discriminant (D)) loop
18022 D := Corresponding_Discriminant (D);
18023 end loop;
18024
18025 return D;
18026 end Root_Corresponding_Discriminant;
18027
18028 ------------------------------
18029 -- Search_Derivation_Levels --
18030 ------------------------------
18031
18032 function Search_Derivation_Levels
18033 (Ti : Entity_Id;
18034 Discrim_Values : Elist_Id;
18035 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18036 is
18037 Assoc : Elmt_Id;
18038 Disc : Entity_Id;
18039 Result : Node_Or_Entity_Id;
18040 Result_Entity : Node_Id;
18041
18042 begin
18043 -- If inappropriate type, return Error, this happens only in
18044 -- cascaded error situations, and we want to avoid a blow up.
18045
18046 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18047 return Error;
18048 end if;
18049
18050 -- Look deeper if possible. Use Stored_Constraints only for
18051 -- untagged types. For tagged types use the given constraint.
18052 -- This asymmetry needs explanation???
18053
18054 if not Stored_Discrim_Values
18055 and then Present (Stored_Constraint (Ti))
18056 and then not Is_Tagged_Type (Ti)
18057 then
18058 Result :=
18059 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18060
18061 else
18062 declare
18063 Td : Entity_Id := Etype (Ti);
18064
18065 begin
18066 -- If the parent type is private, the full view may include
18067 -- renamed discriminants, and it is those stored values that
18068 -- may be needed (the partial view never has more information
18069 -- than the full view).
18070
18071 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18072 Td := Full_View (Td);
18073 end if;
18074
18075 if Td = Ti then
18076 Result := Discriminant;
18077
18078 else
18079 if Present (Stored_Constraint (Ti)) then
18080 Result :=
18081 Search_Derivation_Levels
18082 (Td, Stored_Constraint (Ti), True);
18083 else
18084 Result :=
18085 Search_Derivation_Levels
18086 (Td, Discrim_Values, Stored_Discrim_Values);
18087 end if;
18088 end if;
18089 end;
18090 end if;
18091
18092 -- Extra underlying places to search, if not found above. For
18093 -- concurrent types, the relevant discriminant appears in the
18094 -- corresponding record. For a type derived from a private type
18095 -- without discriminant, the full view inherits the discriminants
18096 -- of the full view of the parent.
18097
18098 if Result = Discriminant then
18099 if Is_Concurrent_Type (Ti)
18100 and then Present (Corresponding_Record_Type (Ti))
18101 then
18102 Result :=
18103 Search_Derivation_Levels (
18104 Corresponding_Record_Type (Ti),
18105 Discrim_Values,
18106 Stored_Discrim_Values);
18107
18108 elsif Is_Private_Type (Ti)
18109 and then not Has_Discriminants (Ti)
18110 and then Present (Full_View (Ti))
18111 and then Etype (Full_View (Ti)) /= Ti
18112 then
18113 Result :=
18114 Search_Derivation_Levels (
18115 Full_View (Ti),
18116 Discrim_Values,
18117 Stored_Discrim_Values);
18118 end if;
18119 end if;
18120
18121 -- If Result is not a (reference to a) discriminant, return it,
18122 -- otherwise set Result_Entity to the discriminant.
18123
18124 if Nkind (Result) = N_Defining_Identifier then
18125 pragma Assert (Result = Discriminant);
18126 Result_Entity := Result;
18127
18128 else
18129 if not Denotes_Discriminant (Result) then
18130 return Result;
18131 end if;
18132
18133 Result_Entity := Entity (Result);
18134 end if;
18135
18136 -- See if this level of derivation actually has discriminants because
18137 -- tagged derivations can add them, hence the lower levels need not
18138 -- have any.
18139
18140 if not Has_Discriminants (Ti) then
18141 return Result;
18142 end if;
18143
18144 -- Scan Ti's discriminants for Result_Entity, and return its
18145 -- corresponding value, if any.
18146
18147 Result_Entity := Original_Record_Component (Result_Entity);
18148
18149 Assoc := First_Elmt (Discrim_Values);
18150
18151 if Stored_Discrim_Values then
18152 Disc := First_Stored_Discriminant (Ti);
18153 else
18154 Disc := First_Discriminant (Ti);
18155 end if;
18156
18157 while Present (Disc) loop
18158
18159 -- If no further associations return the discriminant, value will
18160 -- be found on the second pass.
18161
18162 if No (Assoc) then
18163 return Result;
18164 end if;
18165
18166 if Original_Record_Component (Disc) = Result_Entity then
18167 return Node (Assoc);
18168 end if;
18169
18170 Next_Elmt (Assoc);
18171
18172 if Stored_Discrim_Values then
18173 Next_Stored_Discriminant (Disc);
18174 else
18175 Next_Discriminant (Disc);
18176 end if;
18177 end loop;
18178
18179 -- Could not find it
18180
18181 return Result;
18182 end Search_Derivation_Levels;
18183
18184 -- Local Variables
18185
18186 Result : Node_Or_Entity_Id;
18187
18188 -- Start of processing for Get_Discriminant_Value
18189
18190 begin
18191 -- ??? This routine is a gigantic mess and will be deleted. For the
18192 -- time being just test for the trivial case before calling recurse.
18193
18194 -- We are now celebrating the 20th anniversary of this comment!
18195
18196 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18197 declare
18198 D : Entity_Id;
18199 E : Elmt_Id;
18200
18201 begin
18202 D := First_Discriminant (Typ_For_Constraint);
18203 E := First_Elmt (Constraint);
18204 while Present (D) loop
18205 if Chars (D) = Chars (Discriminant) then
18206 return Node (E);
18207 end if;
18208
18209 Next_Discriminant (D);
18210 Next_Elmt (E);
18211 end loop;
18212 end;
18213 end if;
18214
18215 Result := Search_Derivation_Levels
18216 (Typ_For_Constraint, Constraint, False);
18217
18218 -- ??? hack to disappear when this routine is gone
18219
18220 if Nkind (Result) = N_Defining_Identifier then
18221 declare
18222 D : Entity_Id;
18223 E : Elmt_Id;
18224
18225 begin
18226 D := First_Discriminant (Typ_For_Constraint);
18227 E := First_Elmt (Constraint);
18228 while Present (D) loop
18229 if Root_Corresponding_Discriminant (D) = Discriminant then
18230 return Node (E);
18231 end if;
18232
18233 Next_Discriminant (D);
18234 Next_Elmt (E);
18235 end loop;
18236 end;
18237 end if;
18238
18239 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18240 return Result;
18241 end Get_Discriminant_Value;
18242
18243 --------------------------
18244 -- Has_Range_Constraint --
18245 --------------------------
18246
18247 function Has_Range_Constraint (N : Node_Id) return Boolean is
18248 C : constant Node_Id := Constraint (N);
18249
18250 begin
18251 if Nkind (C) = N_Range_Constraint then
18252 return True;
18253
18254 elsif Nkind (C) = N_Digits_Constraint then
18255 return
18256 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18257 or else Present (Range_Constraint (C));
18258
18259 elsif Nkind (C) = N_Delta_Constraint then
18260 return Present (Range_Constraint (C));
18261
18262 else
18263 return False;
18264 end if;
18265 end Has_Range_Constraint;
18266
18267 ------------------------
18268 -- Inherit_Components --
18269 ------------------------
18270
18271 function Inherit_Components
18272 (N : Node_Id;
18273 Parent_Base : Entity_Id;
18274 Derived_Base : Entity_Id;
18275 Is_Tagged : Boolean;
18276 Inherit_Discr : Boolean;
18277 Discs : Elist_Id) return Elist_Id
18278 is
18279 Assoc_List : constant Elist_Id := New_Elmt_List;
18280
18281 procedure Inherit_Component
18282 (Old_C : Entity_Id;
18283 Plain_Discrim : Boolean := False;
18284 Stored_Discrim : Boolean := False);
18285 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18286 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18287 -- True, Old_C is a stored discriminant. If they are both false then
18288 -- Old_C is a regular component.
18289
18290 -----------------------
18291 -- Inherit_Component --
18292 -----------------------
18293
18294 procedure Inherit_Component
18295 (Old_C : Entity_Id;
18296 Plain_Discrim : Boolean := False;
18297 Stored_Discrim : Boolean := False)
18298 is
18299 procedure Set_Anonymous_Type (Id : Entity_Id);
18300 -- Id denotes the entity of an access discriminant or anonymous
18301 -- access component. Set the type of Id to either the same type of
18302 -- Old_C or create a new one depending on whether the parent and
18303 -- the child types are in the same scope.
18304
18305 ------------------------
18306 -- Set_Anonymous_Type --
18307 ------------------------
18308
18309 procedure Set_Anonymous_Type (Id : Entity_Id) is
18310 Old_Typ : constant Entity_Id := Etype (Old_C);
18311
18312 begin
18313 if Scope (Parent_Base) = Scope (Derived_Base) then
18314 Set_Etype (Id, Old_Typ);
18315
18316 -- The parent and the derived type are in two different scopes.
18317 -- Reuse the type of the original discriminant / component by
18318 -- copying it in order to preserve all attributes.
18319
18320 else
18321 declare
18322 Typ : constant Entity_Id := New_Copy (Old_Typ);
18323
18324 begin
18325 Set_Etype (Id, Typ);
18326
18327 -- Since we do not generate component declarations for
18328 -- inherited components, associate the itype with the
18329 -- derived type.
18330
18331 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18332 Set_Scope (Typ, Derived_Base);
18333 end;
18334 end if;
18335 end Set_Anonymous_Type;
18336
18337 -- Local variables and constants
18338
18339 New_C : constant Entity_Id := New_Copy (Old_C);
18340
18341 Corr_Discrim : Entity_Id;
18342 Discrim : Entity_Id;
18343
18344 -- Start of processing for Inherit_Component
18345
18346 begin
18347 pragma Assert (not Is_Tagged or not Stored_Discrim);
18348
18349 Set_Parent (New_C, Parent (Old_C));
18350
18351 -- Regular discriminants and components must be inserted in the scope
18352 -- of the Derived_Base. Do it here.
18353
18354 if not Stored_Discrim then
18355 Enter_Name (New_C);
18356 end if;
18357
18358 -- For tagged types the Original_Record_Component must point to
18359 -- whatever this field was pointing to in the parent type. This has
18360 -- already been achieved by the call to New_Copy above.
18361
18362 if not Is_Tagged then
18363 Set_Original_Record_Component (New_C, New_C);
18364 Set_Corresponding_Record_Component (New_C, Old_C);
18365 end if;
18366
18367 -- Set the proper type of an access discriminant
18368
18369 if Ekind (New_C) = E_Discriminant
18370 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18371 then
18372 Set_Anonymous_Type (New_C);
18373 end if;
18374
18375 -- If we have inherited a component then see if its Etype contains
18376 -- references to Parent_Base discriminants. In this case, replace
18377 -- these references with the constraints given in Discs. We do not
18378 -- do this for the partial view of private types because this is
18379 -- not needed (only the components of the full view will be used
18380 -- for code generation) and cause problem. We also avoid this
18381 -- transformation in some error situations.
18382
18383 if Ekind (New_C) = E_Component then
18384
18385 -- Set the proper type of an anonymous access component
18386
18387 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18388 Set_Anonymous_Type (New_C);
18389
18390 elsif (Is_Private_Type (Derived_Base)
18391 and then not Is_Generic_Type (Derived_Base))
18392 or else (Is_Empty_Elmt_List (Discs)
18393 and then not Expander_Active)
18394 then
18395 Set_Etype (New_C, Etype (Old_C));
18396
18397 else
18398 -- The current component introduces a circularity of the
18399 -- following kind:
18400
18401 -- limited with Pack_2;
18402 -- package Pack_1 is
18403 -- type T_1 is tagged record
18404 -- Comp : access Pack_2.T_2;
18405 -- ...
18406 -- end record;
18407 -- end Pack_1;
18408
18409 -- with Pack_1;
18410 -- package Pack_2 is
18411 -- type T_2 is new Pack_1.T_1 with ...;
18412 -- end Pack_2;
18413
18414 Set_Etype
18415 (New_C,
18416 Constrain_Component_Type
18417 (Old_C, Derived_Base, N, Parent_Base, Discs));
18418 end if;
18419 end if;
18420
18421 -- In derived tagged types it is illegal to reference a non
18422 -- discriminant component in the parent type. To catch this, mark
18423 -- these components with an Ekind of E_Void. This will be reset in
18424 -- Record_Type_Definition after processing the record extension of
18425 -- the derived type.
18426
18427 -- If the declaration is a private extension, there is no further
18428 -- record extension to process, and the components retain their
18429 -- current kind, because they are visible at this point.
18430
18431 if Is_Tagged and then Ekind (New_C) = E_Component
18432 and then Nkind (N) /= N_Private_Extension_Declaration
18433 then
18434 Set_Ekind (New_C, E_Void);
18435 end if;
18436
18437 if Plain_Discrim then
18438 Set_Corresponding_Discriminant (New_C, Old_C);
18439 Build_Discriminal (New_C);
18440
18441 -- If we are explicitly inheriting a stored discriminant it will be
18442 -- completely hidden.
18443
18444 elsif Stored_Discrim then
18445 Set_Corresponding_Discriminant (New_C, Empty);
18446 Set_Discriminal (New_C, Empty);
18447 Set_Is_Completely_Hidden (New_C);
18448
18449 -- Set the Original_Record_Component of each discriminant in the
18450 -- derived base to point to the corresponding stored that we just
18451 -- created.
18452
18453 Discrim := First_Discriminant (Derived_Base);
18454 while Present (Discrim) loop
18455 Corr_Discrim := Corresponding_Discriminant (Discrim);
18456
18457 -- Corr_Discrim could be missing in an error situation
18458
18459 if Present (Corr_Discrim)
18460 and then Original_Record_Component (Corr_Discrim) = Old_C
18461 then
18462 Set_Original_Record_Component (Discrim, New_C);
18463 Set_Corresponding_Record_Component (Discrim, Empty);
18464 end if;
18465
18466 Next_Discriminant (Discrim);
18467 end loop;
18468
18469 Append_Entity (New_C, Derived_Base);
18470 end if;
18471
18472 if not Is_Tagged then
18473 Append_Elmt (Old_C, Assoc_List);
18474 Append_Elmt (New_C, Assoc_List);
18475 end if;
18476 end Inherit_Component;
18477
18478 -- Variables local to Inherit_Component
18479
18480 Loc : constant Source_Ptr := Sloc (N);
18481
18482 Parent_Discrim : Entity_Id;
18483 Stored_Discrim : Entity_Id;
18484 D : Entity_Id;
18485 Component : Entity_Id;
18486
18487 -- Start of processing for Inherit_Components
18488
18489 begin
18490 if not Is_Tagged then
18491 Append_Elmt (Parent_Base, Assoc_List);
18492 Append_Elmt (Derived_Base, Assoc_List);
18493 end if;
18494
18495 -- Inherit parent discriminants if needed
18496
18497 if Inherit_Discr then
18498 Parent_Discrim := First_Discriminant (Parent_Base);
18499 while Present (Parent_Discrim) loop
18500 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18501 Next_Discriminant (Parent_Discrim);
18502 end loop;
18503 end if;
18504
18505 -- Create explicit stored discrims for untagged types when necessary
18506
18507 if not Has_Unknown_Discriminants (Derived_Base)
18508 and then Has_Discriminants (Parent_Base)
18509 and then not Is_Tagged
18510 and then
18511 (not Inherit_Discr
18512 or else First_Discriminant (Parent_Base) /=
18513 First_Stored_Discriminant (Parent_Base))
18514 then
18515 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18516 while Present (Stored_Discrim) loop
18517 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18518 Next_Stored_Discriminant (Stored_Discrim);
18519 end loop;
18520 end if;
18521
18522 -- See if we can apply the second transformation for derived types, as
18523 -- explained in point 6. in the comments above Build_Derived_Record_Type
18524 -- This is achieved by appending Derived_Base discriminants into Discs,
18525 -- which has the side effect of returning a non empty Discs list to the
18526 -- caller of Inherit_Components, which is what we want. This must be
18527 -- done for private derived types if there are explicit stored
18528 -- discriminants, to ensure that we can retrieve the values of the
18529 -- constraints provided in the ancestors.
18530
18531 if Inherit_Discr
18532 and then Is_Empty_Elmt_List (Discs)
18533 and then Present (First_Discriminant (Derived_Base))
18534 and then
18535 (not Is_Private_Type (Derived_Base)
18536 or else Is_Completely_Hidden
18537 (First_Stored_Discriminant (Derived_Base))
18538 or else Is_Generic_Type (Derived_Base))
18539 then
18540 D := First_Discriminant (Derived_Base);
18541 while Present (D) loop
18542 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18543 Next_Discriminant (D);
18544 end loop;
18545 end if;
18546
18547 -- Finally, inherit non-discriminant components unless they are not
18548 -- visible because defined or inherited from the full view of the
18549 -- parent. Don't inherit the _parent field of the parent type.
18550
18551 Component := First_Entity (Parent_Base);
18552 while Present (Component) loop
18553
18554 -- Ada 2005 (AI-251): Do not inherit components associated with
18555 -- secondary tags of the parent.
18556
18557 if Ekind (Component) = E_Component
18558 and then Present (Related_Type (Component))
18559 then
18560 null;
18561
18562 elsif Ekind (Component) /= E_Component
18563 or else Chars (Component) = Name_uParent
18564 then
18565 null;
18566
18567 -- If the derived type is within the parent type's declarative
18568 -- region, then the components can still be inherited even though
18569 -- they aren't visible at this point. This can occur for cases
18570 -- such as within public child units where the components must
18571 -- become visible upon entering the child unit's private part.
18572
18573 elsif not Is_Visible_Component (Component)
18574 and then not In_Open_Scopes (Scope (Parent_Base))
18575 then
18576 null;
18577
18578 elsif Ekind_In (Derived_Base, E_Private_Type,
18579 E_Limited_Private_Type)
18580 then
18581 null;
18582
18583 else
18584 Inherit_Component (Component);
18585 end if;
18586
18587 Next_Entity (Component);
18588 end loop;
18589
18590 -- For tagged derived types, inherited discriminants cannot be used in
18591 -- component declarations of the record extension part. To achieve this
18592 -- we mark the inherited discriminants as not visible.
18593
18594 if Is_Tagged and then Inherit_Discr then
18595 D := First_Discriminant (Derived_Base);
18596 while Present (D) loop
18597 Set_Is_Immediately_Visible (D, False);
18598 Next_Discriminant (D);
18599 end loop;
18600 end if;
18601
18602 return Assoc_List;
18603 end Inherit_Components;
18604
18605 -----------------------------
18606 -- Inherit_Predicate_Flags --
18607 -----------------------------
18608
18609 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18610 begin
18611 if Present (Predicate_Function (Subt)) then
18612 return;
18613 end if;
18614
18615 Set_Has_Predicates (Subt, Has_Predicates (Par));
18616 Set_Has_Static_Predicate_Aspect
18617 (Subt, Has_Static_Predicate_Aspect (Par));
18618 Set_Has_Dynamic_Predicate_Aspect
18619 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18620
18621 -- A named subtype does not inherit the predicate function of its
18622 -- parent but an itype declared for a loop index needs the discrete
18623 -- predicate information of its parent to execute the loop properly.
18624 -- A non-discrete type may has a static predicate (for example True)
18625 -- but has no static_discrete_predicate.
18626
18627 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18628 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18629
18630 if Has_Static_Predicate (Par) and then Is_Discrete_Type (Par) then
18631 Set_Static_Discrete_Predicate
18632 (Subt, Static_Discrete_Predicate (Par));
18633 end if;
18634 end if;
18635 end Inherit_Predicate_Flags;
18636
18637 ----------------------
18638 -- Is_EVF_Procedure --
18639 ----------------------
18640
18641 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18642 Formal : Entity_Id;
18643
18644 begin
18645 -- Examine the formals of an Extensions_Visible False procedure looking
18646 -- for a controlling OUT parameter.
18647
18648 if Ekind (Subp) = E_Procedure
18649 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18650 then
18651 Formal := First_Formal (Subp);
18652 while Present (Formal) loop
18653 if Ekind (Formal) = E_Out_Parameter
18654 and then Is_Controlling_Formal (Formal)
18655 then
18656 return True;
18657 end if;
18658
18659 Next_Formal (Formal);
18660 end loop;
18661 end if;
18662
18663 return False;
18664 end Is_EVF_Procedure;
18665
18666 -----------------------
18667 -- Is_Null_Extension --
18668 -----------------------
18669
18670 function Is_Null_Extension (T : Entity_Id) return Boolean is
18671 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18672 Comp_List : Node_Id;
18673 Comp : Node_Id;
18674
18675 begin
18676 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18677 or else not Is_Tagged_Type (T)
18678 or else Nkind (Type_Definition (Type_Decl)) /=
18679 N_Derived_Type_Definition
18680 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18681 then
18682 return False;
18683 end if;
18684
18685 Comp_List :=
18686 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18687
18688 if Present (Discriminant_Specifications (Type_Decl)) then
18689 return False;
18690
18691 elsif Present (Comp_List)
18692 and then Is_Non_Empty_List (Component_Items (Comp_List))
18693 then
18694 Comp := First (Component_Items (Comp_List));
18695
18696 -- Only user-defined components are relevant. The component list
18697 -- may also contain a parent component and internal components
18698 -- corresponding to secondary tags, but these do not determine
18699 -- whether this is a null extension.
18700
18701 while Present (Comp) loop
18702 if Comes_From_Source (Comp) then
18703 return False;
18704 end if;
18705
18706 Next (Comp);
18707 end loop;
18708
18709 return True;
18710
18711 else
18712 return True;
18713 end if;
18714 end Is_Null_Extension;
18715
18716 ------------------------------
18717 -- Is_Valid_Constraint_Kind --
18718 ------------------------------
18719
18720 function Is_Valid_Constraint_Kind
18721 (T_Kind : Type_Kind;
18722 Constraint_Kind : Node_Kind) return Boolean
18723 is
18724 begin
18725 case T_Kind is
18726 when Enumeration_Kind
18727 | Integer_Kind
18728 =>
18729 return Constraint_Kind = N_Range_Constraint;
18730
18731 when Decimal_Fixed_Point_Kind =>
18732 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18733 N_Range_Constraint);
18734
18735 when Ordinary_Fixed_Point_Kind =>
18736 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18737 N_Range_Constraint);
18738
18739 when Float_Kind =>
18740 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18741 N_Range_Constraint);
18742
18743 when Access_Kind
18744 | Array_Kind
18745 | Class_Wide_Kind
18746 | Concurrent_Kind
18747 | Private_Kind
18748 | E_Incomplete_Type
18749 | E_Record_Subtype
18750 | E_Record_Type
18751 =>
18752 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18753
18754 when others =>
18755 return True; -- Error will be detected later
18756 end case;
18757 end Is_Valid_Constraint_Kind;
18758
18759 --------------------------
18760 -- Is_Visible_Component --
18761 --------------------------
18762
18763 function Is_Visible_Component
18764 (C : Entity_Id;
18765 N : Node_Id := Empty) return Boolean
18766 is
18767 Original_Comp : Entity_Id := Empty;
18768 Original_Type : Entity_Id;
18769 Type_Scope : Entity_Id;
18770
18771 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18772 -- Check whether parent type of inherited component is declared locally,
18773 -- possibly within a nested package or instance. The current scope is
18774 -- the derived record itself.
18775
18776 -------------------
18777 -- Is_Local_Type --
18778 -------------------
18779
18780 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18781 Scop : Entity_Id;
18782
18783 begin
18784 Scop := Scope (Typ);
18785 while Present (Scop)
18786 and then Scop /= Standard_Standard
18787 loop
18788 if Scop = Scope (Current_Scope) then
18789 return True;
18790 end if;
18791
18792 Scop := Scope (Scop);
18793 end loop;
18794
18795 return False;
18796 end Is_Local_Type;
18797
18798 -- Start of processing for Is_Visible_Component
18799
18800 begin
18801 if Ekind_In (C, E_Component, E_Discriminant) then
18802 Original_Comp := Original_Record_Component (C);
18803 end if;
18804
18805 if No (Original_Comp) then
18806
18807 -- Premature usage, or previous error
18808
18809 return False;
18810
18811 else
18812 Original_Type := Scope (Original_Comp);
18813 Type_Scope := Scope (Base_Type (Scope (C)));
18814 end if;
18815
18816 -- This test only concerns tagged types
18817
18818 if not Is_Tagged_Type (Original_Type) then
18819
18820 -- Check if this is a renamed discriminant (hidden either by the
18821 -- derived type or by some ancestor), unless we are analyzing code
18822 -- generated by the expander since it may reference such components
18823 -- (for example see the expansion of Deep_Adjust).
18824
18825 if Ekind (C) = E_Discriminant and then Present (N) then
18826 return
18827 not Comes_From_Source (N)
18828 or else not Is_Completely_Hidden (C);
18829 else
18830 return True;
18831 end if;
18832
18833 -- If it is _Parent or _Tag, there is no visibility issue
18834
18835 elsif not Comes_From_Source (Original_Comp) then
18836 return True;
18837
18838 -- Discriminants are visible unless the (private) type has unknown
18839 -- discriminants. If the discriminant reference is inserted for a
18840 -- discriminant check on a full view it is also visible.
18841
18842 elsif Ekind (Original_Comp) = E_Discriminant
18843 and then
18844 (not Has_Unknown_Discriminants (Original_Type)
18845 or else (Present (N)
18846 and then Nkind (N) = N_Selected_Component
18847 and then Nkind (Prefix (N)) = N_Type_Conversion
18848 and then not Comes_From_Source (Prefix (N))))
18849 then
18850 return True;
18851
18852 -- In the body of an instantiation, check the visibility of a component
18853 -- in case it has a homograph that is a primitive operation of a private
18854 -- type which was not visible in the generic unit.
18855
18856 -- Should Is_Prefixed_Call be propagated from template to instance???
18857
18858 elsif In_Instance_Body then
18859 if not Is_Tagged_Type (Original_Type)
18860 or else not Is_Private_Type (Original_Type)
18861 then
18862 return True;
18863
18864 else
18865 declare
18866 Subp_Elmt : Elmt_Id;
18867
18868 begin
18869 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18870 while Present (Subp_Elmt) loop
18871
18872 -- The component is hidden by a primitive operation
18873
18874 if Chars (Node (Subp_Elmt)) = Chars (C) then
18875 return False;
18876 end if;
18877
18878 Next_Elmt (Subp_Elmt);
18879 end loop;
18880
18881 return True;
18882 end;
18883 end if;
18884
18885 -- If the component has been declared in an ancestor which is currently
18886 -- a private type, then it is not visible. The same applies if the
18887 -- component's containing type is not in an open scope and the original
18888 -- component's enclosing type is a visible full view of a private type
18889 -- (which can occur in cases where an attempt is being made to reference
18890 -- a component in a sibling package that is inherited from a visible
18891 -- component of a type in an ancestor package; the component in the
18892 -- sibling package should not be visible even though the component it
18893 -- inherited from is visible). This does not apply however in the case
18894 -- where the scope of the type is a private child unit, or when the
18895 -- parent comes from a local package in which the ancestor is currently
18896 -- visible. The latter suppression of visibility is needed for cases
18897 -- that are tested in B730006.
18898
18899 elsif Is_Private_Type (Original_Type)
18900 or else
18901 (not Is_Private_Descendant (Type_Scope)
18902 and then not In_Open_Scopes (Type_Scope)
18903 and then Has_Private_Declaration (Original_Type))
18904 then
18905 -- If the type derives from an entity in a formal package, there
18906 -- are no additional visible components.
18907
18908 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18909 N_Formal_Package_Declaration
18910 then
18911 return False;
18912
18913 -- if we are not in the private part of the current package, there
18914 -- are no additional visible components.
18915
18916 elsif Ekind (Scope (Current_Scope)) = E_Package
18917 and then not In_Private_Part (Scope (Current_Scope))
18918 then
18919 return False;
18920 else
18921 return
18922 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18923 and then In_Open_Scopes (Scope (Original_Type))
18924 and then Is_Local_Type (Type_Scope);
18925 end if;
18926
18927 -- There is another weird way in which a component may be invisible when
18928 -- the private and the full view are not derived from the same ancestor.
18929 -- Here is an example :
18930
18931 -- type A1 is tagged record F1 : integer; end record;
18932 -- type A2 is new A1 with record F2 : integer; end record;
18933 -- type T is new A1 with private;
18934 -- private
18935 -- type T is new A2 with null record;
18936
18937 -- In this case, the full view of T inherits F1 and F2 but the private
18938 -- view inherits only F1
18939
18940 else
18941 declare
18942 Ancestor : Entity_Id := Scope (C);
18943
18944 begin
18945 loop
18946 if Ancestor = Original_Type then
18947 return True;
18948
18949 -- The ancestor may have a partial view of the original type,
18950 -- but if the full view is in scope, as in a child body, the
18951 -- component is visible.
18952
18953 elsif In_Private_Part (Scope (Original_Type))
18954 and then Full_View (Ancestor) = Original_Type
18955 then
18956 return True;
18957
18958 elsif Ancestor = Etype (Ancestor) then
18959
18960 -- No further ancestors to examine
18961
18962 return False;
18963 end if;
18964
18965 Ancestor := Etype (Ancestor);
18966 end loop;
18967 end;
18968 end if;
18969 end Is_Visible_Component;
18970
18971 --------------------------
18972 -- Make_Class_Wide_Type --
18973 --------------------------
18974
18975 procedure Make_Class_Wide_Type (T : Entity_Id) is
18976 CW_Type : Entity_Id;
18977 CW_Name : Name_Id;
18978 Next_E : Entity_Id;
18979 Prev_E : Entity_Id;
18980
18981 begin
18982 if Present (Class_Wide_Type (T)) then
18983
18984 -- The class-wide type is a partially decorated entity created for a
18985 -- unanalyzed tagged type referenced through a limited with clause.
18986 -- When the tagged type is analyzed, its class-wide type needs to be
18987 -- redecorated. Note that we reuse the entity created by Decorate_
18988 -- Tagged_Type in order to preserve all links.
18989
18990 if Materialize_Entity (Class_Wide_Type (T)) then
18991 CW_Type := Class_Wide_Type (T);
18992 Set_Materialize_Entity (CW_Type, False);
18993
18994 -- The class wide type can have been defined by the partial view, in
18995 -- which case everything is already done.
18996
18997 else
18998 return;
18999 end if;
19000
19001 -- Default case, we need to create a new class-wide type
19002
19003 else
19004 CW_Type :=
19005 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19006 end if;
19007
19008 -- Inherit root type characteristics
19009
19010 CW_Name := Chars (CW_Type);
19011 Next_E := Next_Entity (CW_Type);
19012 Prev_E := Prev_Entity (CW_Type);
19013 Copy_Node (T, CW_Type);
19014 Set_Comes_From_Source (CW_Type, False);
19015 Set_Chars (CW_Type, CW_Name);
19016 Set_Parent (CW_Type, Parent (T));
19017 Set_Prev_Entity (CW_Type, Prev_E);
19018 Set_Next_Entity (CW_Type, Next_E);
19019
19020 -- Ensure we have a new freeze node for the class-wide type. The partial
19021 -- view may have freeze action of its own, requiring a proper freeze
19022 -- node, and the same freeze node cannot be shared between the two
19023 -- types.
19024
19025 Set_Has_Delayed_Freeze (CW_Type);
19026 Set_Freeze_Node (CW_Type, Empty);
19027
19028 -- Customize the class-wide type: It has no prim. op., it cannot be
19029 -- abstract, its Etype points back to the specific root type, and it
19030 -- cannot have any invariants.
19031
19032 Set_Ekind (CW_Type, E_Class_Wide_Type);
19033 Set_Is_Tagged_Type (CW_Type, True);
19034 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19035 Set_Is_Abstract_Type (CW_Type, False);
19036 Set_Is_Constrained (CW_Type, False);
19037 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19038 Set_Default_SSO (CW_Type);
19039 Set_Has_Inheritable_Invariants (CW_Type, False);
19040 Set_Has_Inherited_Invariants (CW_Type, False);
19041 Set_Has_Own_Invariants (CW_Type, False);
19042
19043 if Ekind (T) = E_Class_Wide_Subtype then
19044 Set_Etype (CW_Type, Etype (Base_Type (T)));
19045 else
19046 Set_Etype (CW_Type, T);
19047 end if;
19048
19049 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19050
19051 -- If this is the class_wide type of a constrained subtype, it does
19052 -- not have discriminants.
19053
19054 Set_Has_Discriminants (CW_Type,
19055 Has_Discriminants (T) and then not Is_Constrained (T));
19056
19057 Set_Has_Unknown_Discriminants (CW_Type, True);
19058 Set_Class_Wide_Type (T, CW_Type);
19059 Set_Equivalent_Type (CW_Type, Empty);
19060
19061 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19062
19063 Set_Class_Wide_Type (CW_Type, CW_Type);
19064 end Make_Class_Wide_Type;
19065
19066 ----------------
19067 -- Make_Index --
19068 ----------------
19069
19070 procedure Make_Index
19071 (N : Node_Id;
19072 Related_Nod : Node_Id;
19073 Related_Id : Entity_Id := Empty;
19074 Suffix_Index : Nat := 1;
19075 In_Iter_Schm : Boolean := False)
19076 is
19077 R : Node_Id;
19078 T : Entity_Id;
19079 Def_Id : Entity_Id := Empty;
19080 Found : Boolean := False;
19081
19082 begin
19083 -- For a discrete range used in a constrained array definition and
19084 -- defined by a range, an implicit conversion to the predefined type
19085 -- INTEGER is assumed if each bound is either a numeric literal, a named
19086 -- number, or an attribute, and the type of both bounds (prior to the
19087 -- implicit conversion) is the type universal_integer. Otherwise, both
19088 -- bounds must be of the same discrete type, other than universal
19089 -- integer; this type must be determinable independently of the
19090 -- context, but using the fact that the type must be discrete and that
19091 -- both bounds must have the same type.
19092
19093 -- Character literals also have a universal type in the absence of
19094 -- of additional context, and are resolved to Standard_Character.
19095
19096 if Nkind (N) = N_Range then
19097
19098 -- The index is given by a range constraint. The bounds are known
19099 -- to be of a consistent type.
19100
19101 if not Is_Overloaded (N) then
19102 T := Etype (N);
19103
19104 -- For universal bounds, choose the specific predefined type
19105
19106 if T = Universal_Integer then
19107 T := Standard_Integer;
19108
19109 elsif T = Any_Character then
19110 Ambiguous_Character (Low_Bound (N));
19111
19112 T := Standard_Character;
19113 end if;
19114
19115 -- The node may be overloaded because some user-defined operators
19116 -- are available, but if a universal interpretation exists it is
19117 -- also the selected one.
19118
19119 elsif Universal_Interpretation (N) = Universal_Integer then
19120 T := Standard_Integer;
19121
19122 else
19123 T := Any_Type;
19124
19125 declare
19126 Ind : Interp_Index;
19127 It : Interp;
19128
19129 begin
19130 Get_First_Interp (N, Ind, It);
19131 while Present (It.Typ) loop
19132 if Is_Discrete_Type (It.Typ) then
19133
19134 if Found
19135 and then not Covers (It.Typ, T)
19136 and then not Covers (T, It.Typ)
19137 then
19138 Error_Msg_N ("ambiguous bounds in discrete range", N);
19139 exit;
19140 else
19141 T := It.Typ;
19142 Found := True;
19143 end if;
19144 end if;
19145
19146 Get_Next_Interp (Ind, It);
19147 end loop;
19148
19149 if T = Any_Type then
19150 Error_Msg_N ("discrete type required for range", N);
19151 Set_Etype (N, Any_Type);
19152 return;
19153
19154 elsif T = Universal_Integer then
19155 T := Standard_Integer;
19156 end if;
19157 end;
19158 end if;
19159
19160 if not Is_Discrete_Type (T) then
19161 Error_Msg_N ("discrete type required for range", N);
19162 Set_Etype (N, Any_Type);
19163 return;
19164 end if;
19165
19166 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19167 and then Attribute_Name (Low_Bound (N)) = Name_First
19168 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19169 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19170 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19171 then
19172 -- The type of the index will be the type of the prefix, as long
19173 -- as the upper bound is 'Last of the same type.
19174
19175 Def_Id := Entity (Prefix (Low_Bound (N)));
19176
19177 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19178 or else Attribute_Name (High_Bound (N)) /= Name_Last
19179 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19180 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19181 then
19182 Def_Id := Empty;
19183 end if;
19184 end if;
19185
19186 R := N;
19187 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19188
19189 elsif Nkind (N) = N_Subtype_Indication then
19190
19191 -- The index is given by a subtype with a range constraint
19192
19193 T := Base_Type (Entity (Subtype_Mark (N)));
19194
19195 if not Is_Discrete_Type (T) then
19196 Error_Msg_N ("discrete type required for range", N);
19197 Set_Etype (N, Any_Type);
19198 return;
19199 end if;
19200
19201 R := Range_Expression (Constraint (N));
19202
19203 Resolve (R, T);
19204 Process_Range_Expr_In_Decl
19205 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19206
19207 elsif Nkind (N) = N_Attribute_Reference then
19208
19209 -- Catch beginner's error (use of attribute other than 'Range)
19210
19211 if Attribute_Name (N) /= Name_Range then
19212 Error_Msg_N ("expect attribute ''Range", N);
19213 Set_Etype (N, Any_Type);
19214 return;
19215 end if;
19216
19217 -- If the node denotes the range of a type mark, that is also the
19218 -- resulting type, and we do not need to create an Itype for it.
19219
19220 if Is_Entity_Name (Prefix (N))
19221 and then Comes_From_Source (N)
19222 and then Is_Type (Entity (Prefix (N)))
19223 and then Is_Discrete_Type (Entity (Prefix (N)))
19224 then
19225 Def_Id := Entity (Prefix (N));
19226 end if;
19227
19228 Analyze_And_Resolve (N);
19229 T := Etype (N);
19230 R := N;
19231
19232 -- If none of the above, must be a subtype. We convert this to a
19233 -- range attribute reference because in the case of declared first
19234 -- named subtypes, the types in the range reference can be different
19235 -- from the type of the entity. A range attribute normalizes the
19236 -- reference and obtains the correct types for the bounds.
19237
19238 -- This transformation is in the nature of an expansion, is only
19239 -- done if expansion is active. In particular, it is not done on
19240 -- formal generic types, because we need to retain the name of the
19241 -- original index for instantiation purposes.
19242
19243 else
19244 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19245 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19246 Set_Etype (N, Any_Integer);
19247 return;
19248
19249 else
19250 -- The type mark may be that of an incomplete type. It is only
19251 -- now that we can get the full view, previous analysis does
19252 -- not look specifically for a type mark.
19253
19254 Set_Entity (N, Get_Full_View (Entity (N)));
19255 Set_Etype (N, Entity (N));
19256 Def_Id := Entity (N);
19257
19258 if not Is_Discrete_Type (Def_Id) then
19259 Error_Msg_N ("discrete type required for index", N);
19260 Set_Etype (N, Any_Type);
19261 return;
19262 end if;
19263 end if;
19264
19265 if Expander_Active then
19266 Rewrite (N,
19267 Make_Attribute_Reference (Sloc (N),
19268 Attribute_Name => Name_Range,
19269 Prefix => Relocate_Node (N)));
19270
19271 -- The original was a subtype mark that does not freeze. This
19272 -- means that the rewritten version must not freeze either.
19273
19274 Set_Must_Not_Freeze (N);
19275 Set_Must_Not_Freeze (Prefix (N));
19276 Analyze_And_Resolve (N);
19277 T := Etype (N);
19278 R := N;
19279
19280 -- If expander is inactive, type is legal, nothing else to construct
19281
19282 else
19283 return;
19284 end if;
19285 end if;
19286
19287 if not Is_Discrete_Type (T) then
19288 Error_Msg_N ("discrete type required for range", N);
19289 Set_Etype (N, Any_Type);
19290 return;
19291
19292 elsif T = Any_Type then
19293 Set_Etype (N, Any_Type);
19294 return;
19295 end if;
19296
19297 -- We will now create the appropriate Itype to describe the range, but
19298 -- first a check. If we originally had a subtype, then we just label
19299 -- the range with this subtype. Not only is there no need to construct
19300 -- a new subtype, but it is wrong to do so for two reasons:
19301
19302 -- 1. A legality concern, if we have a subtype, it must not freeze,
19303 -- and the Itype would cause freezing incorrectly
19304
19305 -- 2. An efficiency concern, if we created an Itype, it would not be
19306 -- recognized as the same type for the purposes of eliminating
19307 -- checks in some circumstances.
19308
19309 -- We signal this case by setting the subtype entity in Def_Id
19310
19311 if No (Def_Id) then
19312 Def_Id :=
19313 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19314 Set_Etype (Def_Id, Base_Type (T));
19315
19316 if Is_Signed_Integer_Type (T) then
19317 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19318
19319 elsif Is_Modular_Integer_Type (T) then
19320 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19321
19322 else
19323 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19324 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19325 Set_First_Literal (Def_Id, First_Literal (T));
19326 end if;
19327
19328 Set_Size_Info (Def_Id, (T));
19329 Set_RM_Size (Def_Id, RM_Size (T));
19330 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19331
19332 Set_Scalar_Range (Def_Id, R);
19333 Conditional_Delay (Def_Id, T);
19334
19335 if Nkind (N) = N_Subtype_Indication then
19336 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19337 end if;
19338
19339 -- In the subtype indication case, if the immediate parent of the
19340 -- new subtype is non-static, then the subtype we create is non-
19341 -- static, even if its bounds are static.
19342
19343 if Nkind (N) = N_Subtype_Indication
19344 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19345 then
19346 Set_Is_Non_Static_Subtype (Def_Id);
19347 end if;
19348 end if;
19349
19350 -- Final step is to label the index with this constructed type
19351
19352 Set_Etype (N, Def_Id);
19353 end Make_Index;
19354
19355 ------------------------------
19356 -- Modular_Type_Declaration --
19357 ------------------------------
19358
19359 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19360 Mod_Expr : constant Node_Id := Expression (Def);
19361 M_Val : Uint;
19362
19363 procedure Set_Modular_Size (Bits : Int);
19364 -- Sets RM_Size to Bits, and Esize to normal word size above this
19365
19366 ----------------------
19367 -- Set_Modular_Size --
19368 ----------------------
19369
19370 procedure Set_Modular_Size (Bits : Int) is
19371 begin
19372 Set_RM_Size (T, UI_From_Int (Bits));
19373
19374 if Bits <= 8 then
19375 Init_Esize (T, 8);
19376
19377 elsif Bits <= 16 then
19378 Init_Esize (T, 16);
19379
19380 elsif Bits <= 32 then
19381 Init_Esize (T, 32);
19382
19383 else
19384 Init_Esize (T, System_Max_Binary_Modulus_Power);
19385 end if;
19386
19387 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19388 Set_Is_Known_Valid (T);
19389 end if;
19390 end Set_Modular_Size;
19391
19392 -- Start of processing for Modular_Type_Declaration
19393
19394 begin
19395 -- If the mod expression is (exactly) 2 * literal, where literal is
19396 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19397
19398 if Warn_On_Suspicious_Modulus_Value
19399 and then Nkind (Mod_Expr) = N_Op_Multiply
19400 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19401 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19402 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19403 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19404 then
19405 Error_Msg_N
19406 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19407 end if;
19408
19409 -- Proceed with analysis of mod expression
19410
19411 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19412 Set_Etype (T, T);
19413 Set_Ekind (T, E_Modular_Integer_Type);
19414 Init_Alignment (T);
19415 Set_Is_Constrained (T);
19416
19417 if not Is_OK_Static_Expression (Mod_Expr) then
19418 Flag_Non_Static_Expr
19419 ("non-static expression used for modular type bound!", Mod_Expr);
19420 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19421 else
19422 M_Val := Expr_Value (Mod_Expr);
19423 end if;
19424
19425 if M_Val < 1 then
19426 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19427 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19428 end if;
19429
19430 if M_Val > 2 ** Standard_Long_Integer_Size then
19431 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19432 end if;
19433
19434 Set_Modulus (T, M_Val);
19435
19436 -- Create bounds for the modular type based on the modulus given in
19437 -- the type declaration and then analyze and resolve those bounds.
19438
19439 Set_Scalar_Range (T,
19440 Make_Range (Sloc (Mod_Expr),
19441 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19442 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19443
19444 -- Properly analyze the literals for the range. We do this manually
19445 -- because we can't go calling Resolve, since we are resolving these
19446 -- bounds with the type, and this type is certainly not complete yet.
19447
19448 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19449 Set_Etype (High_Bound (Scalar_Range (T)), T);
19450 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19451 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19452
19453 -- Loop through powers of two to find number of bits required
19454
19455 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19456
19457 -- Binary case
19458
19459 if M_Val = 2 ** Bits then
19460 Set_Modular_Size (Bits);
19461 return;
19462
19463 -- Nonbinary case
19464
19465 elsif M_Val < 2 ** Bits then
19466 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19467 Set_Non_Binary_Modulus (T);
19468
19469 if Bits > System_Max_Nonbinary_Modulus_Power then
19470 Error_Msg_Uint_1 :=
19471 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19472 Error_Msg_F
19473 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19474 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19475 return;
19476
19477 else
19478 -- In the nonbinary case, set size as per RM 13.3(55)
19479
19480 Set_Modular_Size (Bits);
19481 return;
19482 end if;
19483 end if;
19484
19485 end loop;
19486
19487 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19488 -- so we just signal an error and set the maximum size.
19489
19490 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19491 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19492
19493 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19494 Init_Alignment (T);
19495
19496 end Modular_Type_Declaration;
19497
19498 --------------------------
19499 -- New_Concatenation_Op --
19500 --------------------------
19501
19502 procedure New_Concatenation_Op (Typ : Entity_Id) is
19503 Loc : constant Source_Ptr := Sloc (Typ);
19504 Op : Entity_Id;
19505
19506 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19507 -- Create abbreviated declaration for the formal of a predefined
19508 -- Operator 'Op' of type 'Typ'
19509
19510 --------------------
19511 -- Make_Op_Formal --
19512 --------------------
19513
19514 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19515 Formal : Entity_Id;
19516 begin
19517 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19518 Set_Etype (Formal, Typ);
19519 Set_Mechanism (Formal, Default_Mechanism);
19520 return Formal;
19521 end Make_Op_Formal;
19522
19523 -- Start of processing for New_Concatenation_Op
19524
19525 begin
19526 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19527
19528 Set_Ekind (Op, E_Operator);
19529 Set_Scope (Op, Current_Scope);
19530 Set_Etype (Op, Typ);
19531 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19532 Set_Is_Immediately_Visible (Op);
19533 Set_Is_Intrinsic_Subprogram (Op);
19534 Set_Has_Completion (Op);
19535 Append_Entity (Op, Current_Scope);
19536
19537 Set_Name_Entity_Id (Name_Op_Concat, Op);
19538
19539 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19540 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19541 end New_Concatenation_Op;
19542
19543 -------------------------
19544 -- OK_For_Limited_Init --
19545 -------------------------
19546
19547 -- ???Check all calls of this, and compare the conditions under which it's
19548 -- called.
19549
19550 function OK_For_Limited_Init
19551 (Typ : Entity_Id;
19552 Exp : Node_Id) return Boolean
19553 is
19554 begin
19555 return Is_CPP_Constructor_Call (Exp)
19556 or else (Ada_Version >= Ada_2005
19557 and then not Debug_Flag_Dot_L
19558 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19559 end OK_For_Limited_Init;
19560
19561 -------------------------------
19562 -- OK_For_Limited_Init_In_05 --
19563 -------------------------------
19564
19565 function OK_For_Limited_Init_In_05
19566 (Typ : Entity_Id;
19567 Exp : Node_Id) return Boolean
19568 is
19569 begin
19570 -- An object of a limited interface type can be initialized with any
19571 -- expression of a nonlimited descendant type. However this does not
19572 -- apply if this is a view conversion of some other expression. This
19573 -- is checked below.
19574
19575 if Is_Class_Wide_Type (Typ)
19576 and then Is_Limited_Interface (Typ)
19577 and then not Is_Limited_Type (Etype (Exp))
19578 and then Nkind (Exp) /= N_Type_Conversion
19579 then
19580 return True;
19581 end if;
19582
19583 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19584 -- case of limited aggregates (including extension aggregates), and
19585 -- function calls. The function call may have been given in prefixed
19586 -- notation, in which case the original node is an indexed component.
19587 -- If the function is parameterless, the original node was an explicit
19588 -- dereference. The function may also be parameterless, in which case
19589 -- the source node is just an identifier.
19590
19591 -- A branch of a conditional expression may have been removed if the
19592 -- condition is statically known. This happens during expansion, and
19593 -- thus will not happen if previous errors were encountered. The check
19594 -- will have been performed on the chosen branch, which replaces the
19595 -- original conditional expression.
19596
19597 if No (Exp) then
19598 return True;
19599 end if;
19600
19601 case Nkind (Original_Node (Exp)) is
19602 when N_Aggregate
19603 | N_Extension_Aggregate
19604 | N_Function_Call
19605 | N_Op
19606 =>
19607 return True;
19608
19609 when N_Identifier =>
19610 return Present (Entity (Original_Node (Exp)))
19611 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19612
19613 when N_Qualified_Expression =>
19614 return
19615 OK_For_Limited_Init_In_05
19616 (Typ, Expression (Original_Node (Exp)));
19617
19618 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19619 -- with a function call, the expander has rewritten the call into an
19620 -- N_Type_Conversion node to force displacement of the pointer to
19621 -- reference the component containing the secondary dispatch table.
19622 -- Otherwise a type conversion is not a legal context.
19623 -- A return statement for a build-in-place function returning a
19624 -- synchronized type also introduces an unchecked conversion.
19625
19626 when N_Type_Conversion
19627 | N_Unchecked_Type_Conversion
19628 =>
19629 return not Comes_From_Source (Exp)
19630 and then
19631 OK_For_Limited_Init_In_05
19632 (Typ, Expression (Original_Node (Exp)));
19633
19634 when N_Explicit_Dereference
19635 | N_Indexed_Component
19636 | N_Selected_Component
19637 =>
19638 return Nkind (Exp) = N_Function_Call;
19639
19640 -- A use of 'Input is a function call, hence allowed. Normally the
19641 -- attribute will be changed to a call, but the attribute by itself
19642 -- can occur with -gnatc.
19643
19644 when N_Attribute_Reference =>
19645 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19646
19647 -- "return raise ..." is OK
19648
19649 when N_Raise_Expression =>
19650 return True;
19651
19652 -- For a case expression, all dependent expressions must be legal
19653
19654 when N_Case_Expression =>
19655 declare
19656 Alt : Node_Id;
19657
19658 begin
19659 Alt := First (Alternatives (Original_Node (Exp)));
19660 while Present (Alt) loop
19661 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19662 return False;
19663 end if;
19664
19665 Next (Alt);
19666 end loop;
19667
19668 return True;
19669 end;
19670
19671 -- For an if expression, all dependent expressions must be legal
19672
19673 when N_If_Expression =>
19674 declare
19675 Then_Expr : constant Node_Id :=
19676 Next (First (Expressions (Original_Node (Exp))));
19677 Else_Expr : constant Node_Id := Next (Then_Expr);
19678 begin
19679 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19680 and then
19681 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19682 end;
19683
19684 when others =>
19685 return False;
19686 end case;
19687 end OK_For_Limited_Init_In_05;
19688
19689 -------------------------------------------
19690 -- Ordinary_Fixed_Point_Type_Declaration --
19691 -------------------------------------------
19692
19693 procedure Ordinary_Fixed_Point_Type_Declaration
19694 (T : Entity_Id;
19695 Def : Node_Id)
19696 is
19697 Loc : constant Source_Ptr := Sloc (Def);
19698 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19699 RRS : constant Node_Id := Real_Range_Specification (Def);
19700 Implicit_Base : Entity_Id;
19701 Delta_Val : Ureal;
19702 Small_Val : Ureal;
19703 Low_Val : Ureal;
19704 High_Val : Ureal;
19705
19706 begin
19707 Check_Restriction (No_Fixed_Point, Def);
19708
19709 -- Create implicit base type
19710
19711 Implicit_Base :=
19712 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19713 Set_Etype (Implicit_Base, Implicit_Base);
19714
19715 -- Analyze and process delta expression
19716
19717 Analyze_And_Resolve (Delta_Expr, Any_Real);
19718
19719 Check_Delta_Expression (Delta_Expr);
19720 Delta_Val := Expr_Value_R (Delta_Expr);
19721
19722 Set_Delta_Value (Implicit_Base, Delta_Val);
19723
19724 -- Compute default small from given delta, which is the largest power
19725 -- of two that does not exceed the given delta value.
19726
19727 declare
19728 Tmp : Ureal;
19729 Scale : Int;
19730
19731 begin
19732 Tmp := Ureal_1;
19733 Scale := 0;
19734
19735 if Delta_Val < Ureal_1 then
19736 while Delta_Val < Tmp loop
19737 Tmp := Tmp / Ureal_2;
19738 Scale := Scale + 1;
19739 end loop;
19740
19741 else
19742 loop
19743 Tmp := Tmp * Ureal_2;
19744 exit when Tmp > Delta_Val;
19745 Scale := Scale - 1;
19746 end loop;
19747 end if;
19748
19749 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19750 end;
19751
19752 Set_Small_Value (Implicit_Base, Small_Val);
19753
19754 -- If no range was given, set a dummy range
19755
19756 if RRS <= Empty_Or_Error then
19757 Low_Val := -Small_Val;
19758 High_Val := Small_Val;
19759
19760 -- Otherwise analyze and process given range
19761
19762 else
19763 declare
19764 Low : constant Node_Id := Low_Bound (RRS);
19765 High : constant Node_Id := High_Bound (RRS);
19766
19767 begin
19768 Analyze_And_Resolve (Low, Any_Real);
19769 Analyze_And_Resolve (High, Any_Real);
19770 Check_Real_Bound (Low);
19771 Check_Real_Bound (High);
19772
19773 -- Obtain and set the range
19774
19775 Low_Val := Expr_Value_R (Low);
19776 High_Val := Expr_Value_R (High);
19777
19778 if Low_Val > High_Val then
19779 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19780 end if;
19781 end;
19782 end if;
19783
19784 -- The range for both the implicit base and the declared first subtype
19785 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19786 -- set a temporary range in place. Note that the bounds of the base
19787 -- type will be widened to be symmetrical and to fill the available
19788 -- bits when the type is frozen.
19789
19790 -- We could do this with all discrete types, and probably should, but
19791 -- we absolutely have to do it for fixed-point, since the end-points
19792 -- of the range and the size are determined by the small value, which
19793 -- could be reset before the freeze point.
19794
19795 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19796 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19797
19798 -- Complete definition of first subtype. The inheritance of the rep item
19799 -- chain ensures that SPARK-related pragmas are not clobbered when the
19800 -- ordinary fixed point type acts as a full view of a private type.
19801
19802 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19803 Set_Etype (T, Implicit_Base);
19804 Init_Size_Align (T);
19805 Inherit_Rep_Item_Chain (T, Implicit_Base);
19806 Set_Small_Value (T, Small_Val);
19807 Set_Delta_Value (T, Delta_Val);
19808 Set_Is_Constrained (T);
19809 end Ordinary_Fixed_Point_Type_Declaration;
19810
19811 ----------------------------------
19812 -- Preanalyze_Assert_Expression --
19813 ----------------------------------
19814
19815 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19816 begin
19817 In_Assertion_Expr := In_Assertion_Expr + 1;
19818 Preanalyze_Spec_Expression (N, T);
19819 In_Assertion_Expr := In_Assertion_Expr - 1;
19820 end Preanalyze_Assert_Expression;
19821
19822 -----------------------------------
19823 -- Preanalyze_Default_Expression --
19824 -----------------------------------
19825
19826 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19827 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19828 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19829
19830 begin
19831 In_Default_Expr := True;
19832 In_Spec_Expression := True;
19833
19834 Preanalyze_With_Freezing_And_Resolve (N, T);
19835
19836 In_Default_Expr := Save_In_Default_Expr;
19837 In_Spec_Expression := Save_In_Spec_Expression;
19838 end Preanalyze_Default_Expression;
19839
19840 --------------------------------
19841 -- Preanalyze_Spec_Expression --
19842 --------------------------------
19843
19844 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19845 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19846 begin
19847 In_Spec_Expression := True;
19848 Preanalyze_And_Resolve (N, T);
19849 In_Spec_Expression := Save_In_Spec_Expression;
19850 end Preanalyze_Spec_Expression;
19851
19852 ----------------------------------------
19853 -- Prepare_Private_Subtype_Completion --
19854 ----------------------------------------
19855
19856 procedure Prepare_Private_Subtype_Completion
19857 (Id : Entity_Id;
19858 Related_Nod : Node_Id)
19859 is
19860 Id_B : constant Entity_Id := Base_Type (Id);
19861 Full_B : Entity_Id := Full_View (Id_B);
19862 Full : Entity_Id;
19863
19864 begin
19865 if Present (Full_B) then
19866
19867 -- Get to the underlying full view if necessary
19868
19869 if Is_Private_Type (Full_B)
19870 and then Present (Underlying_Full_View (Full_B))
19871 then
19872 Full_B := Underlying_Full_View (Full_B);
19873 end if;
19874
19875 -- The Base_Type is already completed, we can complete the subtype
19876 -- now. We have to create a new entity with the same name, Thus we
19877 -- can't use Create_Itype.
19878
19879 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19880 Set_Is_Itype (Full);
19881 Set_Associated_Node_For_Itype (Full, Related_Nod);
19882 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19883 end if;
19884
19885 -- The parent subtype may be private, but the base might not, in some
19886 -- nested instances. In that case, the subtype does not need to be
19887 -- exchanged. It would still be nice to make private subtypes and their
19888 -- bases consistent at all times ???
19889
19890 if Is_Private_Type (Id_B) then
19891 Append_Elmt (Id, Private_Dependents (Id_B));
19892 end if;
19893 end Prepare_Private_Subtype_Completion;
19894
19895 ---------------------------
19896 -- Process_Discriminants --
19897 ---------------------------
19898
19899 procedure Process_Discriminants
19900 (N : Node_Id;
19901 Prev : Entity_Id := Empty)
19902 is
19903 Elist : constant Elist_Id := New_Elmt_List;
19904 Id : Node_Id;
19905 Discr : Node_Id;
19906 Discr_Number : Uint;
19907 Discr_Type : Entity_Id;
19908 Default_Present : Boolean := False;
19909 Default_Not_Present : Boolean := False;
19910
19911 begin
19912 -- A composite type other than an array type can have discriminants.
19913 -- On entry, the current scope is the composite type.
19914
19915 -- The discriminants are initially entered into the scope of the type
19916 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19917 -- use, as explained at the end of this procedure.
19918
19919 Discr := First (Discriminant_Specifications (N));
19920 while Present (Discr) loop
19921 Enter_Name (Defining_Identifier (Discr));
19922
19923 -- For navigation purposes we add a reference to the discriminant
19924 -- in the entity for the type. If the current declaration is a
19925 -- completion, place references on the partial view. Otherwise the
19926 -- type is the current scope.
19927
19928 if Present (Prev) then
19929
19930 -- The references go on the partial view, if present. If the
19931 -- partial view has discriminants, the references have been
19932 -- generated already.
19933
19934 if not Has_Discriminants (Prev) then
19935 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19936 end if;
19937 else
19938 Generate_Reference
19939 (Current_Scope, Defining_Identifier (Discr), 'd');
19940 end if;
19941
19942 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19943 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19944
19945 -- Ada 2005 (AI-254)
19946
19947 if Present (Access_To_Subprogram_Definition
19948 (Discriminant_Type (Discr)))
19949 and then Protected_Present (Access_To_Subprogram_Definition
19950 (Discriminant_Type (Discr)))
19951 then
19952 Discr_Type :=
19953 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19954 end if;
19955
19956 else
19957 Find_Type (Discriminant_Type (Discr));
19958 Discr_Type := Etype (Discriminant_Type (Discr));
19959
19960 if Error_Posted (Discriminant_Type (Discr)) then
19961 Discr_Type := Any_Type;
19962 end if;
19963 end if;
19964
19965 -- Handling of discriminants that are access types
19966
19967 if Is_Access_Type (Discr_Type) then
19968
19969 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19970 -- limited record types
19971
19972 if Ada_Version < Ada_2005 then
19973 Check_Access_Discriminant_Requires_Limited
19974 (Discr, Discriminant_Type (Discr));
19975 end if;
19976
19977 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19978 Error_Msg_N
19979 ("(Ada 83) access discriminant not allowed", Discr);
19980 end if;
19981
19982 -- If not access type, must be a discrete type
19983
19984 elsif not Is_Discrete_Type (Discr_Type) then
19985 Error_Msg_N
19986 ("discriminants must have a discrete or access type",
19987 Discriminant_Type (Discr));
19988 end if;
19989
19990 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19991
19992 -- If a discriminant specification includes the assignment compound
19993 -- delimiter followed by an expression, the expression is the default
19994 -- expression of the discriminant; the default expression must be of
19995 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19996 -- a default expression, we do the special preanalysis, since this
19997 -- expression does not freeze (see section "Handling of Default and
19998 -- Per-Object Expressions" in spec of package Sem).
19999
20000 if Present (Expression (Discr)) then
20001 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
20002
20003 -- Legaity checks
20004
20005 if Nkind (N) = N_Formal_Type_Declaration then
20006 Error_Msg_N
20007 ("discriminant defaults not allowed for formal type",
20008 Expression (Discr));
20009
20010 -- Flag an error for a tagged type with defaulted discriminants,
20011 -- excluding limited tagged types when compiling for Ada 2012
20012 -- (see AI05-0214).
20013
20014 elsif Is_Tagged_Type (Current_Scope)
20015 and then (not Is_Limited_Type (Current_Scope)
20016 or else Ada_Version < Ada_2012)
20017 and then Comes_From_Source (N)
20018 then
20019 -- Note: see similar test in Check_Or_Process_Discriminants, to
20020 -- handle the (illegal) case of the completion of an untagged
20021 -- view with discriminants with defaults by a tagged full view.
20022 -- We skip the check if Discr does not come from source, to
20023 -- account for the case of an untagged derived type providing
20024 -- defaults for a renamed discriminant from a private untagged
20025 -- ancestor with a tagged full view (ACATS B460006).
20026
20027 if Ada_Version >= Ada_2012 then
20028 Error_Msg_N
20029 ("discriminants of nonlimited tagged type cannot have"
20030 & " defaults",
20031 Expression (Discr));
20032 else
20033 Error_Msg_N
20034 ("discriminants of tagged type cannot have defaults",
20035 Expression (Discr));
20036 end if;
20037
20038 else
20039 Default_Present := True;
20040 Append_Elmt (Expression (Discr), Elist);
20041
20042 -- Tag the defining identifiers for the discriminants with
20043 -- their corresponding default expressions from the tree.
20044
20045 Set_Discriminant_Default_Value
20046 (Defining_Identifier (Discr), Expression (Discr));
20047 end if;
20048
20049 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20050 -- gets set unless we can be sure that no range check is required.
20051
20052 if (GNATprove_Mode or not Expander_Active)
20053 and then not
20054 Is_In_Range
20055 (Expression (Discr), Discr_Type, Assume_Valid => True)
20056 then
20057 Set_Do_Range_Check (Expression (Discr));
20058 end if;
20059
20060 -- No default discriminant value given
20061
20062 else
20063 Default_Not_Present := True;
20064 end if;
20065
20066 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20067 -- Discr_Type but with the null-exclusion attribute
20068
20069 if Ada_Version >= Ada_2005 then
20070
20071 -- Ada 2005 (AI-231): Static checks
20072
20073 if Can_Never_Be_Null (Discr_Type) then
20074 Null_Exclusion_Static_Checks (Discr);
20075
20076 elsif Is_Access_Type (Discr_Type)
20077 and then Null_Exclusion_Present (Discr)
20078
20079 -- No need to check itypes because in their case this check
20080 -- was done at their point of creation
20081
20082 and then not Is_Itype (Discr_Type)
20083 then
20084 if Can_Never_Be_Null (Discr_Type) then
20085 Error_Msg_NE
20086 ("`NOT NULL` not allowed (& already excludes null)",
20087 Discr,
20088 Discr_Type);
20089 end if;
20090
20091 Set_Etype (Defining_Identifier (Discr),
20092 Create_Null_Excluding_Itype
20093 (T => Discr_Type,
20094 Related_Nod => Discr));
20095
20096 -- Check for improper null exclusion if the type is otherwise
20097 -- legal for a discriminant.
20098
20099 elsif Null_Exclusion_Present (Discr)
20100 and then Is_Discrete_Type (Discr_Type)
20101 then
20102 Error_Msg_N
20103 ("null exclusion can only apply to an access type", Discr);
20104 end if;
20105
20106 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20107 -- can't have defaults. Synchronized types, or types that are
20108 -- explicitly limited are fine, but special tests apply to derived
20109 -- types in generics: in a generic body we have to assume the
20110 -- worst, and therefore defaults are not allowed if the parent is
20111 -- a generic formal private type (see ACATS B370001).
20112
20113 if Is_Access_Type (Discr_Type) and then Default_Present then
20114 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20115 or else Is_Limited_Record (Current_Scope)
20116 or else Is_Concurrent_Type (Current_Scope)
20117 or else Is_Concurrent_Record_Type (Current_Scope)
20118 or else Ekind (Current_Scope) = E_Limited_Private_Type
20119 then
20120 if not Is_Derived_Type (Current_Scope)
20121 or else not Is_Generic_Type (Etype (Current_Scope))
20122 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20123 or else Limited_Present
20124 (Type_Definition (Parent (Current_Scope)))
20125 then
20126 null;
20127
20128 else
20129 Error_Msg_N
20130 ("access discriminants of nonlimited types cannot "
20131 & "have defaults", Expression (Discr));
20132 end if;
20133
20134 elsif Present (Expression (Discr)) then
20135 Error_Msg_N
20136 ("(Ada 2005) access discriminants of nonlimited types "
20137 & "cannot have defaults", Expression (Discr));
20138 end if;
20139 end if;
20140 end if;
20141
20142 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20143 -- This check is relevant only when SPARK_Mode is on as it is not a
20144 -- standard Ada legality rule.
20145
20146 if SPARK_Mode = On
20147 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20148 then
20149 Error_Msg_N ("discriminant cannot be volatile", Discr);
20150 end if;
20151
20152 Next (Discr);
20153 end loop;
20154
20155 -- An element list consisting of the default expressions of the
20156 -- discriminants is constructed in the above loop and used to set
20157 -- the Discriminant_Constraint attribute for the type. If an object
20158 -- is declared of this (record or task) type without any explicit
20159 -- discriminant constraint given, this element list will form the
20160 -- actual parameters for the corresponding initialization procedure
20161 -- for the type.
20162
20163 Set_Discriminant_Constraint (Current_Scope, Elist);
20164 Set_Stored_Constraint (Current_Scope, No_Elist);
20165
20166 -- Default expressions must be provided either for all or for none
20167 -- of the discriminants of a discriminant part. (RM 3.7.1)
20168
20169 if Default_Present and then Default_Not_Present then
20170 Error_Msg_N
20171 ("incomplete specification of defaults for discriminants", N);
20172 end if;
20173
20174 -- The use of the name of a discriminant is not allowed in default
20175 -- expressions of a discriminant part if the specification of the
20176 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20177
20178 -- To detect this, the discriminant names are entered initially with an
20179 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20180 -- attempt to use a void entity (for example in an expression that is
20181 -- type-checked) produces the error message: premature usage. Now after
20182 -- completing the semantic analysis of the discriminant part, we can set
20183 -- the Ekind of all the discriminants appropriately.
20184
20185 Discr := First (Discriminant_Specifications (N));
20186 Discr_Number := Uint_1;
20187 while Present (Discr) loop
20188 Id := Defining_Identifier (Discr);
20189 Set_Ekind (Id, E_Discriminant);
20190 Init_Component_Location (Id);
20191 Init_Esize (Id);
20192 Set_Discriminant_Number (Id, Discr_Number);
20193
20194 -- Make sure this is always set, even in illegal programs
20195
20196 Set_Corresponding_Discriminant (Id, Empty);
20197
20198 -- Initialize the Original_Record_Component to the entity itself.
20199 -- Inherit_Components will propagate the right value to
20200 -- discriminants in derived record types.
20201
20202 Set_Original_Record_Component (Id, Id);
20203
20204 -- Create the discriminal for the discriminant
20205
20206 Build_Discriminal (Id);
20207
20208 Next (Discr);
20209 Discr_Number := Discr_Number + 1;
20210 end loop;
20211
20212 Set_Has_Discriminants (Current_Scope);
20213 end Process_Discriminants;
20214
20215 -----------------------
20216 -- Process_Full_View --
20217 -----------------------
20218
20219 -- WARNING: This routine manages Ghost regions. Return statements must be
20220 -- replaced by gotos which jump to the end of the routine and restore the
20221 -- Ghost mode.
20222
20223 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20224 procedure Collect_Implemented_Interfaces
20225 (Typ : Entity_Id;
20226 Ifaces : Elist_Id);
20227 -- Ada 2005: Gather all the interfaces that Typ directly or
20228 -- inherently implements. Duplicate entries are not added to
20229 -- the list Ifaces.
20230
20231 ------------------------------------
20232 -- Collect_Implemented_Interfaces --
20233 ------------------------------------
20234
20235 procedure Collect_Implemented_Interfaces
20236 (Typ : Entity_Id;
20237 Ifaces : Elist_Id)
20238 is
20239 Iface : Entity_Id;
20240 Iface_Elmt : Elmt_Id;
20241
20242 begin
20243 -- Abstract interfaces are only associated with tagged record types
20244
20245 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20246 return;
20247 end if;
20248
20249 -- Recursively climb to the ancestors
20250
20251 if Etype (Typ) /= Typ
20252
20253 -- Protect the frontend against wrong cyclic declarations like:
20254
20255 -- type B is new A with private;
20256 -- type C is new A with private;
20257 -- private
20258 -- type B is new C with null record;
20259 -- type C is new B with null record;
20260
20261 and then Etype (Typ) /= Priv_T
20262 and then Etype (Typ) /= Full_T
20263 then
20264 -- Keep separate the management of private type declarations
20265
20266 if Ekind (Typ) = E_Record_Type_With_Private then
20267
20268 -- Handle the following illegal usage:
20269 -- type Private_Type is tagged private;
20270 -- private
20271 -- type Private_Type is new Type_Implementing_Iface;
20272
20273 if Present (Full_View (Typ))
20274 and then Etype (Typ) /= Full_View (Typ)
20275 then
20276 if Is_Interface (Etype (Typ)) then
20277 Append_Unique_Elmt (Etype (Typ), Ifaces);
20278 end if;
20279
20280 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20281 end if;
20282
20283 -- Non-private types
20284
20285 else
20286 if Is_Interface (Etype (Typ)) then
20287 Append_Unique_Elmt (Etype (Typ), Ifaces);
20288 end if;
20289
20290 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20291 end if;
20292 end if;
20293
20294 -- Handle entities in the list of abstract interfaces
20295
20296 if Present (Interfaces (Typ)) then
20297 Iface_Elmt := First_Elmt (Interfaces (Typ));
20298 while Present (Iface_Elmt) loop
20299 Iface := Node (Iface_Elmt);
20300
20301 pragma Assert (Is_Interface (Iface));
20302
20303 if not Contain_Interface (Iface, Ifaces) then
20304 Append_Elmt (Iface, Ifaces);
20305 Collect_Implemented_Interfaces (Iface, Ifaces);
20306 end if;
20307
20308 Next_Elmt (Iface_Elmt);
20309 end loop;
20310 end if;
20311 end Collect_Implemented_Interfaces;
20312
20313 -- Local variables
20314
20315 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20316 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20317 -- Save the Ghost-related attributes to restore on exit
20318
20319 Full_Indic : Node_Id;
20320 Full_Parent : Entity_Id;
20321 Priv_Parent : Entity_Id;
20322
20323 -- Start of processing for Process_Full_View
20324
20325 begin
20326 Mark_And_Set_Ghost_Completion (N, Priv_T);
20327
20328 -- First some sanity checks that must be done after semantic
20329 -- decoration of the full view and thus cannot be placed with other
20330 -- similar checks in Find_Type_Name
20331
20332 if not Is_Limited_Type (Priv_T)
20333 and then (Is_Limited_Type (Full_T)
20334 or else Is_Limited_Composite (Full_T))
20335 then
20336 if In_Instance then
20337 null;
20338 else
20339 Error_Msg_N
20340 ("completion of nonlimited type cannot be limited", Full_T);
20341 Explain_Limited_Type (Full_T, Full_T);
20342 end if;
20343
20344 elsif Is_Abstract_Type (Full_T)
20345 and then not Is_Abstract_Type (Priv_T)
20346 then
20347 Error_Msg_N
20348 ("completion of nonabstract type cannot be abstract", Full_T);
20349
20350 elsif Is_Tagged_Type (Priv_T)
20351 and then Is_Limited_Type (Priv_T)
20352 and then not Is_Limited_Type (Full_T)
20353 then
20354 -- If pragma CPP_Class was applied to the private declaration
20355 -- propagate the limitedness to the full-view
20356
20357 if Is_CPP_Class (Priv_T) then
20358 Set_Is_Limited_Record (Full_T);
20359
20360 -- GNAT allow its own definition of Limited_Controlled to disobey
20361 -- this rule in order in ease the implementation. This test is safe
20362 -- because Root_Controlled is defined in a child of System that
20363 -- normal programs are not supposed to use.
20364
20365 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20366 Set_Is_Limited_Composite (Full_T);
20367 else
20368 Error_Msg_N
20369 ("completion of limited tagged type must be limited", Full_T);
20370 end if;
20371
20372 elsif Is_Generic_Type (Priv_T) then
20373 Error_Msg_N ("generic type cannot have a completion", Full_T);
20374 end if;
20375
20376 -- Check that ancestor interfaces of private and full views are
20377 -- consistent. We omit this check for synchronized types because
20378 -- they are performed on the corresponding record type when frozen.
20379
20380 if Ada_Version >= Ada_2005
20381 and then Is_Tagged_Type (Priv_T)
20382 and then Is_Tagged_Type (Full_T)
20383 and then not Is_Concurrent_Type (Full_T)
20384 then
20385 declare
20386 Iface : Entity_Id;
20387 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20388 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20389
20390 begin
20391 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20392 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20393
20394 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20395 -- an interface type if and only if the full type is descendant
20396 -- of the interface type (AARM 7.3 (7.3/2)).
20397
20398 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20399
20400 if Present (Iface) then
20401 Error_Msg_NE
20402 ("interface in partial view& not implemented by full type "
20403 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20404 end if;
20405
20406 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20407
20408 if Present (Iface) then
20409 Error_Msg_NE
20410 ("interface & not implemented by partial view "
20411 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20412 end if;
20413 end;
20414 end if;
20415
20416 if Is_Tagged_Type (Priv_T)
20417 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20418 and then Is_Derived_Type (Full_T)
20419 then
20420 Priv_Parent := Etype (Priv_T);
20421
20422 -- The full view of a private extension may have been transformed
20423 -- into an unconstrained derived type declaration and a subtype
20424 -- declaration (see build_derived_record_type for details).
20425
20426 if Nkind (N) = N_Subtype_Declaration then
20427 Full_Indic := Subtype_Indication (N);
20428 Full_Parent := Etype (Base_Type (Full_T));
20429 else
20430 Full_Indic := Subtype_Indication (Type_Definition (N));
20431 Full_Parent := Etype (Full_T);
20432 end if;
20433
20434 -- Check that the parent type of the full type is a descendant of
20435 -- the ancestor subtype given in the private extension. If either
20436 -- entity has an Etype equal to Any_Type then we had some previous
20437 -- error situation [7.3(8)].
20438
20439 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20440 goto Leave;
20441
20442 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20443 -- any order. Therefore we don't have to check that its parent must
20444 -- be a descendant of the parent of the private type declaration.
20445
20446 elsif Is_Interface (Priv_Parent)
20447 and then Is_Interface (Full_Parent)
20448 then
20449 null;
20450
20451 -- Ada 2005 (AI-251): If the parent of the private type declaration
20452 -- is an interface there is no need to check that it is an ancestor
20453 -- of the associated full type declaration. The required tests for
20454 -- this case are performed by Build_Derived_Record_Type.
20455
20456 elsif not Is_Interface (Base_Type (Priv_Parent))
20457 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20458 then
20459 Error_Msg_N
20460 ("parent of full type must descend from parent of private "
20461 & "extension", Full_Indic);
20462
20463 -- First check a formal restriction, and then proceed with checking
20464 -- Ada rules. Since the formal restriction is not a serious error, we
20465 -- don't prevent further error detection for this check, hence the
20466 -- ELSE.
20467
20468 else
20469 -- In formal mode, when completing a private extension the type
20470 -- named in the private part must be exactly the same as that
20471 -- named in the visible part.
20472
20473 if Priv_Parent /= Full_Parent then
20474 Error_Msg_Name_1 := Chars (Priv_Parent);
20475 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20476 end if;
20477
20478 -- Check the rules of 7.3(10): if the private extension inherits
20479 -- known discriminants, then the full type must also inherit those
20480 -- discriminants from the same (ancestor) type, and the parent
20481 -- subtype of the full type must be constrained if and only if
20482 -- the ancestor subtype of the private extension is constrained.
20483
20484 if No (Discriminant_Specifications (Parent (Priv_T)))
20485 and then not Has_Unknown_Discriminants (Priv_T)
20486 and then Has_Discriminants (Base_Type (Priv_Parent))
20487 then
20488 declare
20489 Priv_Indic : constant Node_Id :=
20490 Subtype_Indication (Parent (Priv_T));
20491
20492 Priv_Constr : constant Boolean :=
20493 Is_Constrained (Priv_Parent)
20494 or else
20495 Nkind (Priv_Indic) = N_Subtype_Indication
20496 or else
20497 Is_Constrained (Entity (Priv_Indic));
20498
20499 Full_Constr : constant Boolean :=
20500 Is_Constrained (Full_Parent)
20501 or else
20502 Nkind (Full_Indic) = N_Subtype_Indication
20503 or else
20504 Is_Constrained (Entity (Full_Indic));
20505
20506 Priv_Discr : Entity_Id;
20507 Full_Discr : Entity_Id;
20508
20509 begin
20510 Priv_Discr := First_Discriminant (Priv_Parent);
20511 Full_Discr := First_Discriminant (Full_Parent);
20512 while Present (Priv_Discr) and then Present (Full_Discr) loop
20513 if Original_Record_Component (Priv_Discr) =
20514 Original_Record_Component (Full_Discr)
20515 or else
20516 Corresponding_Discriminant (Priv_Discr) =
20517 Corresponding_Discriminant (Full_Discr)
20518 then
20519 null;
20520 else
20521 exit;
20522 end if;
20523
20524 Next_Discriminant (Priv_Discr);
20525 Next_Discriminant (Full_Discr);
20526 end loop;
20527
20528 if Present (Priv_Discr) or else Present (Full_Discr) then
20529 Error_Msg_N
20530 ("full view must inherit discriminants of the parent "
20531 & "type used in the private extension", Full_Indic);
20532
20533 elsif Priv_Constr and then not Full_Constr then
20534 Error_Msg_N
20535 ("parent subtype of full type must be constrained",
20536 Full_Indic);
20537
20538 elsif Full_Constr and then not Priv_Constr then
20539 Error_Msg_N
20540 ("parent subtype of full type must be unconstrained",
20541 Full_Indic);
20542 end if;
20543 end;
20544
20545 -- Check the rules of 7.3(12): if a partial view has neither
20546 -- known or unknown discriminants, then the full type
20547 -- declaration shall define a definite subtype.
20548
20549 elsif not Has_Unknown_Discriminants (Priv_T)
20550 and then not Has_Discriminants (Priv_T)
20551 and then not Is_Constrained (Full_T)
20552 then
20553 Error_Msg_N
20554 ("full view must define a constrained type if partial view "
20555 & "has no discriminants", Full_T);
20556 end if;
20557
20558 -- ??????? Do we implement the following properly ?????
20559 -- If the ancestor subtype of a private extension has constrained
20560 -- discriminants, then the parent subtype of the full view shall
20561 -- impose a statically matching constraint on those discriminants
20562 -- [7.3(13)].
20563 end if;
20564
20565 else
20566 -- For untagged types, verify that a type without discriminants is
20567 -- not completed with an unconstrained type. A separate error message
20568 -- is produced if the full type has defaulted discriminants.
20569
20570 if Is_Definite_Subtype (Priv_T)
20571 and then not Is_Definite_Subtype (Full_T)
20572 then
20573 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20574 Error_Msg_NE
20575 ("full view of& not compatible with declaration#",
20576 Full_T, Priv_T);
20577
20578 if not Is_Tagged_Type (Full_T) then
20579 Error_Msg_N
20580 ("\one is constrained, the other unconstrained", Full_T);
20581 end if;
20582 end if;
20583 end if;
20584
20585 -- AI-419: verify that the use of "limited" is consistent
20586
20587 declare
20588 Orig_Decl : constant Node_Id := Original_Node (N);
20589
20590 begin
20591 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20592 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20593 and then Nkind
20594 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20595 then
20596 if not Limited_Present (Parent (Priv_T))
20597 and then not Synchronized_Present (Parent (Priv_T))
20598 and then Limited_Present (Type_Definition (Orig_Decl))
20599 then
20600 Error_Msg_N
20601 ("full view of non-limited extension cannot be limited", N);
20602
20603 -- Conversely, if the partial view carries the limited keyword,
20604 -- the full view must as well, even if it may be redundant.
20605
20606 elsif Limited_Present (Parent (Priv_T))
20607 and then not Limited_Present (Type_Definition (Orig_Decl))
20608 then
20609 Error_Msg_N
20610 ("full view of limited extension must be explicitly limited",
20611 N);
20612 end if;
20613 end if;
20614 end;
20615
20616 -- Ada 2005 (AI-443): A synchronized private extension must be
20617 -- completed by a task or protected type.
20618
20619 if Ada_Version >= Ada_2005
20620 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20621 and then Synchronized_Present (Parent (Priv_T))
20622 and then not Is_Concurrent_Type (Full_T)
20623 then
20624 Error_Msg_N ("full view of synchronized extension must " &
20625 "be synchronized type", N);
20626 end if;
20627
20628 -- Ada 2005 AI-363: if the full view has discriminants with
20629 -- defaults, it is illegal to declare constrained access subtypes
20630 -- whose designated type is the current type. This allows objects
20631 -- of the type that are declared in the heap to be unconstrained.
20632
20633 if not Has_Unknown_Discriminants (Priv_T)
20634 and then not Has_Discriminants (Priv_T)
20635 and then Has_Discriminants (Full_T)
20636 and then
20637 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20638 then
20639 Set_Has_Constrained_Partial_View (Full_T);
20640 Set_Has_Constrained_Partial_View (Priv_T);
20641 end if;
20642
20643 -- Create a full declaration for all its subtypes recorded in
20644 -- Private_Dependents and swap them similarly to the base type. These
20645 -- are subtypes that have been define before the full declaration of
20646 -- the private type. We also swap the entry in Private_Dependents list
20647 -- so we can properly restore the private view on exit from the scope.
20648
20649 declare
20650 Priv_Elmt : Elmt_Id;
20651 Priv_Scop : Entity_Id;
20652 Priv : Entity_Id;
20653 Full : Entity_Id;
20654
20655 begin
20656 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20657 while Present (Priv_Elmt) loop
20658 Priv := Node (Priv_Elmt);
20659 Priv_Scop := Scope (Priv);
20660
20661 if Ekind_In (Priv, E_Private_Subtype,
20662 E_Limited_Private_Subtype,
20663 E_Record_Subtype_With_Private)
20664 then
20665 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20666 Set_Is_Itype (Full);
20667 Set_Parent (Full, Parent (Priv));
20668 Set_Associated_Node_For_Itype (Full, N);
20669
20670 -- Now we need to complete the private subtype, but since the
20671 -- base type has already been swapped, we must also swap the
20672 -- subtypes (and thus, reverse the arguments in the call to
20673 -- Complete_Private_Subtype). Also note that we may need to
20674 -- re-establish the scope of the private subtype.
20675
20676 Copy_And_Swap (Priv, Full);
20677
20678 if not In_Open_Scopes (Priv_Scop) then
20679 Push_Scope (Priv_Scop);
20680
20681 else
20682 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20683
20684 Priv_Scop := Empty;
20685 end if;
20686
20687 Complete_Private_Subtype (Full, Priv, Full_T, N);
20688
20689 if Present (Priv_Scop) then
20690 Pop_Scope;
20691 end if;
20692
20693 Replace_Elmt (Priv_Elmt, Full);
20694 end if;
20695
20696 Next_Elmt (Priv_Elmt);
20697 end loop;
20698 end;
20699
20700 -- If the private view was tagged, copy the new primitive operations
20701 -- from the private view to the full view.
20702
20703 if Is_Tagged_Type (Full_T) then
20704 declare
20705 Disp_Typ : Entity_Id;
20706 Full_List : Elist_Id;
20707 Prim : Entity_Id;
20708 Prim_Elmt : Elmt_Id;
20709 Priv_List : Elist_Id;
20710
20711 function Contains
20712 (E : Entity_Id;
20713 L : Elist_Id) return Boolean;
20714 -- Determine whether list L contains element E
20715
20716 --------------
20717 -- Contains --
20718 --------------
20719
20720 function Contains
20721 (E : Entity_Id;
20722 L : Elist_Id) return Boolean
20723 is
20724 List_Elmt : Elmt_Id;
20725
20726 begin
20727 List_Elmt := First_Elmt (L);
20728 while Present (List_Elmt) loop
20729 if Node (List_Elmt) = E then
20730 return True;
20731 end if;
20732
20733 Next_Elmt (List_Elmt);
20734 end loop;
20735
20736 return False;
20737 end Contains;
20738
20739 -- Start of processing
20740
20741 begin
20742 if Is_Tagged_Type (Priv_T) then
20743 Priv_List := Primitive_Operations (Priv_T);
20744 Prim_Elmt := First_Elmt (Priv_List);
20745
20746 -- In the case of a concurrent type completing a private tagged
20747 -- type, primitives may have been declared in between the two
20748 -- views. These subprograms need to be wrapped the same way
20749 -- entries and protected procedures are handled because they
20750 -- cannot be directly shared by the two views.
20751
20752 if Is_Concurrent_Type (Full_T) then
20753 declare
20754 Conc_Typ : constant Entity_Id :=
20755 Corresponding_Record_Type (Full_T);
20756 Curr_Nod : Node_Id := Parent (Conc_Typ);
20757 Wrap_Spec : Node_Id;
20758
20759 begin
20760 while Present (Prim_Elmt) loop
20761 Prim := Node (Prim_Elmt);
20762
20763 if Comes_From_Source (Prim)
20764 and then not Is_Abstract_Subprogram (Prim)
20765 then
20766 Wrap_Spec :=
20767 Make_Subprogram_Declaration (Sloc (Prim),
20768 Specification =>
20769 Build_Wrapper_Spec
20770 (Subp_Id => Prim,
20771 Obj_Typ => Conc_Typ,
20772 Formals =>
20773 Parameter_Specifications
20774 (Parent (Prim))));
20775
20776 Insert_After (Curr_Nod, Wrap_Spec);
20777 Curr_Nod := Wrap_Spec;
20778
20779 Analyze (Wrap_Spec);
20780
20781 -- Remove the wrapper from visibility to avoid
20782 -- spurious conflict with the wrapped entity.
20783
20784 Set_Is_Immediately_Visible
20785 (Defining_Entity (Specification (Wrap_Spec)),
20786 False);
20787 end if;
20788
20789 Next_Elmt (Prim_Elmt);
20790 end loop;
20791
20792 goto Leave;
20793 end;
20794
20795 -- For non-concurrent types, transfer explicit primitives, but
20796 -- omit those inherited from the parent of the private view
20797 -- since they will be re-inherited later on.
20798
20799 else
20800 Full_List := Primitive_Operations (Full_T);
20801 while Present (Prim_Elmt) loop
20802 Prim := Node (Prim_Elmt);
20803
20804 if Comes_From_Source (Prim)
20805 and then not Contains (Prim, Full_List)
20806 then
20807 Append_Elmt (Prim, Full_List);
20808 end if;
20809
20810 Next_Elmt (Prim_Elmt);
20811 end loop;
20812 end if;
20813
20814 -- Untagged private view
20815
20816 else
20817 Full_List := Primitive_Operations (Full_T);
20818
20819 -- In this case the partial view is untagged, so here we locate
20820 -- all of the earlier primitives that need to be treated as
20821 -- dispatching (those that appear between the two views). Note
20822 -- that these additional operations must all be new operations
20823 -- (any earlier operations that override inherited operations
20824 -- of the full view will already have been inserted in the
20825 -- primitives list, marked by Check_Operation_From_Private_View
20826 -- as dispatching. Note that implicit "/=" operators are
20827 -- excluded from being added to the primitives list since they
20828 -- shouldn't be treated as dispatching (tagged "/=" is handled
20829 -- specially).
20830
20831 Prim := Next_Entity (Full_T);
20832 while Present (Prim) and then Prim /= Priv_T loop
20833 if Ekind_In (Prim, E_Procedure, E_Function) then
20834 Disp_Typ := Find_Dispatching_Type (Prim);
20835
20836 if Disp_Typ = Full_T
20837 and then (Chars (Prim) /= Name_Op_Ne
20838 or else Comes_From_Source (Prim))
20839 then
20840 Check_Controlling_Formals (Full_T, Prim);
20841
20842 if Is_Suitable_Primitive (Prim)
20843 and then not Is_Dispatching_Operation (Prim)
20844 then
20845 Append_Elmt (Prim, Full_List);
20846 Set_Is_Dispatching_Operation (Prim);
20847 Set_DT_Position_Value (Prim, No_Uint);
20848 end if;
20849
20850 elsif Is_Dispatching_Operation (Prim)
20851 and then Disp_Typ /= Full_T
20852 then
20853 -- Verify that it is not otherwise controlled by a
20854 -- formal or a return value of type T.
20855
20856 Check_Controlling_Formals (Disp_Typ, Prim);
20857 end if;
20858 end if;
20859
20860 Next_Entity (Prim);
20861 end loop;
20862 end if;
20863
20864 -- For the tagged case, the two views can share the same primitive
20865 -- operations list and the same class-wide type. Update attributes
20866 -- of the class-wide type which depend on the full declaration.
20867
20868 if Is_Tagged_Type (Priv_T) then
20869 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20870 Set_Class_Wide_Type
20871 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20872
20873 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20874 end if;
20875 end;
20876 end if;
20877
20878 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20879
20880 if Known_To_Have_Preelab_Init (Priv_T) then
20881
20882 -- Case where there is a pragma Preelaborable_Initialization. We
20883 -- always allow this in predefined units, which is cheating a bit,
20884 -- but it means we don't have to struggle to meet the requirements in
20885 -- the RM for having Preelaborable Initialization. Otherwise we
20886 -- require that the type meets the RM rules. But we can't check that
20887 -- yet, because of the rule about overriding Initialize, so we simply
20888 -- set a flag that will be checked at freeze time.
20889
20890 if not In_Predefined_Unit (Full_T) then
20891 Set_Must_Have_Preelab_Init (Full_T);
20892 end if;
20893 end if;
20894
20895 -- If pragma CPP_Class was applied to the private type declaration,
20896 -- propagate it now to the full type declaration.
20897
20898 if Is_CPP_Class (Priv_T) then
20899 Set_Is_CPP_Class (Full_T);
20900 Set_Convention (Full_T, Convention_CPP);
20901
20902 -- Check that components of imported CPP types do not have default
20903 -- expressions.
20904
20905 Check_CPP_Type_Has_No_Defaults (Full_T);
20906 end if;
20907
20908 -- If the private view has user specified stream attributes, then so has
20909 -- the full view.
20910
20911 -- Why the test, how could these flags be already set in Full_T ???
20912
20913 if Has_Specified_Stream_Read (Priv_T) then
20914 Set_Has_Specified_Stream_Read (Full_T);
20915 end if;
20916
20917 if Has_Specified_Stream_Write (Priv_T) then
20918 Set_Has_Specified_Stream_Write (Full_T);
20919 end if;
20920
20921 if Has_Specified_Stream_Input (Priv_T) then
20922 Set_Has_Specified_Stream_Input (Full_T);
20923 end if;
20924
20925 if Has_Specified_Stream_Output (Priv_T) then
20926 Set_Has_Specified_Stream_Output (Full_T);
20927 end if;
20928
20929 -- Propagate Default_Initial_Condition-related attributes from the
20930 -- partial view to the full view and its base type.
20931
20932 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20933 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20934
20935 -- Propagate invariant-related attributes from the partial view to the
20936 -- full view and its base type.
20937
20938 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20939 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20940
20941 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20942 -- in the full view without advertising the inheritance in the partial
20943 -- view. This can only occur when the partial view has no parent type
20944 -- and the full view has an interface as a parent. Any other scenarios
20945 -- are illegal because implemented interfaces must match between the
20946 -- two views.
20947
20948 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20949 declare
20950 Full_Par : constant Entity_Id := Etype (Full_T);
20951 Priv_Par : constant Entity_Id := Etype (Priv_T);
20952
20953 begin
20954 if not Is_Interface (Priv_Par)
20955 and then Is_Interface (Full_Par)
20956 and then Has_Inheritable_Invariants (Full_Par)
20957 then
20958 Error_Msg_N
20959 ("hidden inheritance of class-wide type invariants not "
20960 & "allowed", N);
20961 end if;
20962 end;
20963 end if;
20964
20965 -- Propagate predicates to full type, and predicate function if already
20966 -- defined. It is not clear that this can actually happen? the partial
20967 -- view cannot be frozen yet, and the predicate function has not been
20968 -- built. Still it is a cheap check and seems safer to make it.
20969
20970 if Has_Predicates (Priv_T) then
20971 Set_Has_Predicates (Full_T);
20972
20973 if Present (Predicate_Function (Priv_T)) then
20974 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20975 end if;
20976 end if;
20977
20978 <<Leave>>
20979 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20980 end Process_Full_View;
20981
20982 -----------------------------------
20983 -- Process_Incomplete_Dependents --
20984 -----------------------------------
20985
20986 procedure Process_Incomplete_Dependents
20987 (N : Node_Id;
20988 Full_T : Entity_Id;
20989 Inc_T : Entity_Id)
20990 is
20991 Inc_Elmt : Elmt_Id;
20992 Priv_Dep : Entity_Id;
20993 New_Subt : Entity_Id;
20994
20995 Disc_Constraint : Elist_Id;
20996
20997 begin
20998 if No (Private_Dependents (Inc_T)) then
20999 return;
21000 end if;
21001
21002 -- Itypes that may be generated by the completion of an incomplete
21003 -- subtype are not used by the back-end and not attached to the tree.
21004 -- They are created only for constraint-checking purposes.
21005
21006 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21007 while Present (Inc_Elmt) loop
21008 Priv_Dep := Node (Inc_Elmt);
21009
21010 if Ekind (Priv_Dep) = E_Subprogram_Type then
21011
21012 -- An Access_To_Subprogram type may have a return type or a
21013 -- parameter type that is incomplete. Replace with the full view.
21014
21015 if Etype (Priv_Dep) = Inc_T then
21016 Set_Etype (Priv_Dep, Full_T);
21017 end if;
21018
21019 declare
21020 Formal : Entity_Id;
21021
21022 begin
21023 Formal := First_Formal (Priv_Dep);
21024 while Present (Formal) loop
21025 if Etype (Formal) = Inc_T then
21026 Set_Etype (Formal, Full_T);
21027 end if;
21028
21029 Next_Formal (Formal);
21030 end loop;
21031 end;
21032
21033 elsif Is_Overloadable (Priv_Dep) then
21034
21035 -- If a subprogram in the incomplete dependents list is primitive
21036 -- for a tagged full type then mark it as a dispatching operation,
21037 -- check whether it overrides an inherited subprogram, and check
21038 -- restrictions on its controlling formals. Note that a protected
21039 -- operation is never dispatching: only its wrapper operation
21040 -- (which has convention Ada) is.
21041
21042 if Is_Tagged_Type (Full_T)
21043 and then Is_Primitive (Priv_Dep)
21044 and then Convention (Priv_Dep) /= Convention_Protected
21045 then
21046 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21047 Set_Is_Dispatching_Operation (Priv_Dep);
21048 Check_Controlling_Formals (Full_T, Priv_Dep);
21049 end if;
21050
21051 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21052
21053 -- Can happen during processing of a body before the completion
21054 -- of a TA type. Ignore, because spec is also on dependent list.
21055
21056 return;
21057
21058 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21059 -- corresponding subtype of the full view.
21060
21061 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21062 and then Comes_From_Source (Priv_Dep)
21063 then
21064 Set_Subtype_Indication
21065 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21066 Set_Etype (Priv_Dep, Full_T);
21067 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21068 Set_Analyzed (Parent (Priv_Dep), False);
21069
21070 -- Reanalyze the declaration, suppressing the call to Enter_Name
21071 -- to avoid duplicate names.
21072
21073 Analyze_Subtype_Declaration
21074 (N => Parent (Priv_Dep),
21075 Skip => True);
21076
21077 -- Dependent is a subtype
21078
21079 else
21080 -- We build a new subtype indication using the full view of the
21081 -- incomplete parent. The discriminant constraints have been
21082 -- elaborated already at the point of the subtype declaration.
21083
21084 New_Subt := Create_Itype (E_Void, N);
21085
21086 if Has_Discriminants (Full_T) then
21087 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21088 else
21089 Disc_Constraint := No_Elist;
21090 end if;
21091
21092 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21093 Set_Full_View (Priv_Dep, New_Subt);
21094 end if;
21095
21096 Next_Elmt (Inc_Elmt);
21097 end loop;
21098 end Process_Incomplete_Dependents;
21099
21100 --------------------------------
21101 -- Process_Range_Expr_In_Decl --
21102 --------------------------------
21103
21104 procedure Process_Range_Expr_In_Decl
21105 (R : Node_Id;
21106 T : Entity_Id;
21107 Subtyp : Entity_Id := Empty;
21108 Check_List : List_Id := Empty_List;
21109 R_Check_Off : Boolean := False;
21110 In_Iter_Schm : Boolean := False)
21111 is
21112 Lo, Hi : Node_Id;
21113 R_Checks : Check_Result;
21114 Insert_Node : Node_Id;
21115 Def_Id : Entity_Id;
21116
21117 begin
21118 Analyze_And_Resolve (R, Base_Type (T));
21119
21120 if Nkind (R) = N_Range then
21121
21122 -- In SPARK, all ranges should be static, with the exception of the
21123 -- discrete type definition of a loop parameter specification.
21124
21125 if not In_Iter_Schm
21126 and then not Is_OK_Static_Range (R)
21127 then
21128 Check_SPARK_05_Restriction ("range should be static", R);
21129 end if;
21130
21131 Lo := Low_Bound (R);
21132 Hi := High_Bound (R);
21133
21134 -- Validity checks on the range of a quantified expression are
21135 -- delayed until the construct is transformed into a loop.
21136
21137 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21138 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21139 then
21140 null;
21141
21142 -- We need to ensure validity of the bounds here, because if we
21143 -- go ahead and do the expansion, then the expanded code will get
21144 -- analyzed with range checks suppressed and we miss the check.
21145
21146 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21147 -- the temporaries generated by routine Remove_Side_Effects by means
21148 -- of validity checks must use the same names. When a range appears
21149 -- in the parent of a generic, the range is processed with checks
21150 -- disabled as part of the generic context and with checks enabled
21151 -- for code generation purposes. This leads to link issues as the
21152 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21153 -- template sees the temporaries generated by Remove_Side_Effects.
21154
21155 else
21156 Validity_Check_Range (R, Subtyp);
21157 end if;
21158
21159 -- If there were errors in the declaration, try and patch up some
21160 -- common mistakes in the bounds. The cases handled are literals
21161 -- which are Integer where the expected type is Real and vice versa.
21162 -- These corrections allow the compilation process to proceed further
21163 -- along since some basic assumptions of the format of the bounds
21164 -- are guaranteed.
21165
21166 if Etype (R) = Any_Type then
21167 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21168 Rewrite (Lo,
21169 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21170
21171 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21172 Rewrite (Hi,
21173 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21174
21175 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21176 Rewrite (Lo,
21177 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21178
21179 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21180 Rewrite (Hi,
21181 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21182 end if;
21183
21184 Set_Etype (Lo, T);
21185 Set_Etype (Hi, T);
21186 end if;
21187
21188 -- If the bounds of the range have been mistakenly given as string
21189 -- literals (perhaps in place of character literals), then an error
21190 -- has already been reported, but we rewrite the string literal as a
21191 -- bound of the range's type to avoid blowups in later processing
21192 -- that looks at static values.
21193
21194 if Nkind (Lo) = N_String_Literal then
21195 Rewrite (Lo,
21196 Make_Attribute_Reference (Sloc (Lo),
21197 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21198 Attribute_Name => Name_First));
21199 Analyze_And_Resolve (Lo);
21200 end if;
21201
21202 if Nkind (Hi) = N_String_Literal then
21203 Rewrite (Hi,
21204 Make_Attribute_Reference (Sloc (Hi),
21205 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21206 Attribute_Name => Name_First));
21207 Analyze_And_Resolve (Hi);
21208 end if;
21209
21210 -- If bounds aren't scalar at this point then exit, avoiding
21211 -- problems with further processing of the range in this procedure.
21212
21213 if not Is_Scalar_Type (Etype (Lo)) then
21214 return;
21215 end if;
21216
21217 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21218 -- then range of the base type. Here we check whether the bounds
21219 -- are in the range of the subtype itself. Note that if the bounds
21220 -- represent the null range the Constraint_Error exception should
21221 -- not be raised.
21222
21223 -- ??? The following code should be cleaned up as follows
21224
21225 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21226 -- is done in the call to Range_Check (R, T); below
21227
21228 -- 2. The use of R_Check_Off should be investigated and possibly
21229 -- removed, this would clean up things a bit.
21230
21231 if Is_Null_Range (Lo, Hi) then
21232 null;
21233
21234 else
21235 -- Capture values of bounds and generate temporaries for them
21236 -- if needed, before applying checks, since checks may cause
21237 -- duplication of the expression without forcing evaluation.
21238
21239 -- The forced evaluation removes side effects from expressions,
21240 -- which should occur also in GNATprove mode. Otherwise, we end up
21241 -- with unexpected insertions of actions at places where this is
21242 -- not supposed to occur, e.g. on default parameters of a call.
21243
21244 if Expander_Active or GNATprove_Mode then
21245
21246 -- Call Force_Evaluation to create declarations as needed to
21247 -- deal with side effects, and also create typ_FIRST/LAST
21248 -- entities for bounds if we have a subtype name.
21249
21250 -- Note: we do this transformation even if expansion is not
21251 -- active if we are in GNATprove_Mode since the transformation
21252 -- is in general required to ensure that the resulting tree has
21253 -- proper Ada semantics.
21254
21255 Force_Evaluation
21256 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21257 Force_Evaluation
21258 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21259 end if;
21260
21261 -- We use a flag here instead of suppressing checks on the type
21262 -- because the type we check against isn't necessarily the place
21263 -- where we put the check.
21264
21265 if not R_Check_Off then
21266 R_Checks := Get_Range_Checks (R, T);
21267
21268 -- Look up tree to find an appropriate insertion point. We
21269 -- can't just use insert_actions because later processing
21270 -- depends on the insertion node. Prior to Ada 2012 the
21271 -- insertion point could only be a declaration or a loop, but
21272 -- quantified expressions can appear within any context in an
21273 -- expression, and the insertion point can be any statement,
21274 -- pragma, or declaration.
21275
21276 Insert_Node := Parent (R);
21277 while Present (Insert_Node) loop
21278 exit when
21279 Nkind (Insert_Node) in N_Declaration
21280 and then
21281 not Nkind_In
21282 (Insert_Node, N_Component_Declaration,
21283 N_Loop_Parameter_Specification,
21284 N_Function_Specification,
21285 N_Procedure_Specification);
21286
21287 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21288 or else Nkind (Insert_Node) in
21289 N_Statement_Other_Than_Procedure_Call
21290 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21291 N_Pragma);
21292
21293 Insert_Node := Parent (Insert_Node);
21294 end loop;
21295
21296 -- Why would Type_Decl not be present??? Without this test,
21297 -- short regression tests fail.
21298
21299 if Present (Insert_Node) then
21300
21301 -- Case of loop statement. Verify that the range is part
21302 -- of the subtype indication of the iteration scheme.
21303
21304 if Nkind (Insert_Node) = N_Loop_Statement then
21305 declare
21306 Indic : Node_Id;
21307
21308 begin
21309 Indic := Parent (R);
21310 while Present (Indic)
21311 and then Nkind (Indic) /= N_Subtype_Indication
21312 loop
21313 Indic := Parent (Indic);
21314 end loop;
21315
21316 if Present (Indic) then
21317 Def_Id := Etype (Subtype_Mark (Indic));
21318
21319 Insert_Range_Checks
21320 (R_Checks,
21321 Insert_Node,
21322 Def_Id,
21323 Sloc (Insert_Node),
21324 R,
21325 Do_Before => True);
21326 end if;
21327 end;
21328
21329 -- Insertion before a declaration. If the declaration
21330 -- includes discriminants, the list of applicable checks
21331 -- is given by the caller.
21332
21333 elsif Nkind (Insert_Node) in N_Declaration then
21334 Def_Id := Defining_Identifier (Insert_Node);
21335
21336 if (Ekind (Def_Id) = E_Record_Type
21337 and then Depends_On_Discriminant (R))
21338 or else
21339 (Ekind (Def_Id) = E_Protected_Type
21340 and then Has_Discriminants (Def_Id))
21341 then
21342 Append_Range_Checks
21343 (R_Checks,
21344 Check_List, Def_Id, Sloc (Insert_Node), R);
21345
21346 else
21347 Insert_Range_Checks
21348 (R_Checks,
21349 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21350
21351 end if;
21352
21353 -- Insertion before a statement. Range appears in the
21354 -- context of a quantified expression. Insertion will
21355 -- take place when expression is expanded.
21356
21357 else
21358 null;
21359 end if;
21360 end if;
21361 end if;
21362 end if;
21363
21364 -- Case of other than an explicit N_Range node
21365
21366 -- The forced evaluation removes side effects from expressions, which
21367 -- should occur also in GNATprove mode. Otherwise, we end up with
21368 -- unexpected insertions of actions at places where this is not
21369 -- supposed to occur, e.g. on default parameters of a call.
21370
21371 elsif Expander_Active or GNATprove_Mode then
21372 Get_Index_Bounds (R, Lo, Hi);
21373 Force_Evaluation (Lo);
21374 Force_Evaluation (Hi);
21375 end if;
21376 end Process_Range_Expr_In_Decl;
21377
21378 --------------------------------------
21379 -- Process_Real_Range_Specification --
21380 --------------------------------------
21381
21382 procedure Process_Real_Range_Specification (Def : Node_Id) is
21383 Spec : constant Node_Id := Real_Range_Specification (Def);
21384 Lo : Node_Id;
21385 Hi : Node_Id;
21386 Err : Boolean := False;
21387
21388 procedure Analyze_Bound (N : Node_Id);
21389 -- Analyze and check one bound
21390
21391 -------------------
21392 -- Analyze_Bound --
21393 -------------------
21394
21395 procedure Analyze_Bound (N : Node_Id) is
21396 begin
21397 Analyze_And_Resolve (N, Any_Real);
21398
21399 if not Is_OK_Static_Expression (N) then
21400 Flag_Non_Static_Expr
21401 ("bound in real type definition is not static!", N);
21402 Err := True;
21403 end if;
21404 end Analyze_Bound;
21405
21406 -- Start of processing for Process_Real_Range_Specification
21407
21408 begin
21409 if Present (Spec) then
21410 Lo := Low_Bound (Spec);
21411 Hi := High_Bound (Spec);
21412 Analyze_Bound (Lo);
21413 Analyze_Bound (Hi);
21414
21415 -- If error, clear away junk range specification
21416
21417 if Err then
21418 Set_Real_Range_Specification (Def, Empty);
21419 end if;
21420 end if;
21421 end Process_Real_Range_Specification;
21422
21423 ---------------------
21424 -- Process_Subtype --
21425 ---------------------
21426
21427 function Process_Subtype
21428 (S : Node_Id;
21429 Related_Nod : Node_Id;
21430 Related_Id : Entity_Id := Empty;
21431 Suffix : Character := ' ') return Entity_Id
21432 is
21433 P : Node_Id;
21434 Def_Id : Entity_Id;
21435 Error_Node : Node_Id;
21436 Full_View_Id : Entity_Id;
21437 Subtype_Mark_Id : Entity_Id;
21438
21439 May_Have_Null_Exclusion : Boolean;
21440
21441 procedure Check_Incomplete (T : Node_Id);
21442 -- Called to verify that an incomplete type is not used prematurely
21443
21444 ----------------------
21445 -- Check_Incomplete --
21446 ----------------------
21447
21448 procedure Check_Incomplete (T : Node_Id) is
21449 begin
21450 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21451
21452 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21453 and then
21454 not (Ada_Version >= Ada_2005
21455 and then
21456 (Nkind (Parent (T)) = N_Subtype_Declaration
21457 or else (Nkind (Parent (T)) = N_Subtype_Indication
21458 and then Nkind (Parent (Parent (T))) =
21459 N_Subtype_Declaration)))
21460 then
21461 Error_Msg_N ("invalid use of type before its full declaration", T);
21462 end if;
21463 end Check_Incomplete;
21464
21465 -- Start of processing for Process_Subtype
21466
21467 begin
21468 -- Case of no constraints present
21469
21470 if Nkind (S) /= N_Subtype_Indication then
21471 Find_Type (S);
21472
21473 -- No way to proceed if the subtype indication is malformed. This
21474 -- will happen for example when the subtype indication in an object
21475 -- declaration is missing altogether and the expression is analyzed
21476 -- as if it were that indication.
21477
21478 if not Is_Entity_Name (S) then
21479 return Any_Type;
21480 end if;
21481
21482 Check_Incomplete (S);
21483 P := Parent (S);
21484
21485 -- Ada 2005 (AI-231): Static check
21486
21487 if Ada_Version >= Ada_2005
21488 and then Present (P)
21489 and then Null_Exclusion_Present (P)
21490 and then Nkind (P) /= N_Access_To_Object_Definition
21491 and then not Is_Access_Type (Entity (S))
21492 then
21493 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21494 end if;
21495
21496 -- The following is ugly, can't we have a range or even a flag???
21497
21498 May_Have_Null_Exclusion :=
21499 Nkind_In (P, N_Access_Definition,
21500 N_Access_Function_Definition,
21501 N_Access_Procedure_Definition,
21502 N_Access_To_Object_Definition,
21503 N_Allocator,
21504 N_Component_Definition)
21505 or else
21506 Nkind_In (P, N_Derived_Type_Definition,
21507 N_Discriminant_Specification,
21508 N_Formal_Object_Declaration,
21509 N_Object_Declaration,
21510 N_Object_Renaming_Declaration,
21511 N_Parameter_Specification,
21512 N_Subtype_Declaration);
21513
21514 -- Create an Itype that is a duplicate of Entity (S) but with the
21515 -- null-exclusion attribute.
21516
21517 if May_Have_Null_Exclusion
21518 and then Is_Access_Type (Entity (S))
21519 and then Null_Exclusion_Present (P)
21520
21521 -- No need to check the case of an access to object definition.
21522 -- It is correct to define double not-null pointers.
21523
21524 -- Example:
21525 -- type Not_Null_Int_Ptr is not null access Integer;
21526 -- type Acc is not null access Not_Null_Int_Ptr;
21527
21528 and then Nkind (P) /= N_Access_To_Object_Definition
21529 then
21530 if Can_Never_Be_Null (Entity (S)) then
21531 case Nkind (Related_Nod) is
21532 when N_Full_Type_Declaration =>
21533 if Nkind (Type_Definition (Related_Nod))
21534 in N_Array_Type_Definition
21535 then
21536 Error_Node :=
21537 Subtype_Indication
21538 (Component_Definition
21539 (Type_Definition (Related_Nod)));
21540 else
21541 Error_Node :=
21542 Subtype_Indication (Type_Definition (Related_Nod));
21543 end if;
21544
21545 when N_Subtype_Declaration =>
21546 Error_Node := Subtype_Indication (Related_Nod);
21547
21548 when N_Object_Declaration =>
21549 Error_Node := Object_Definition (Related_Nod);
21550
21551 when N_Component_Declaration =>
21552 Error_Node :=
21553 Subtype_Indication (Component_Definition (Related_Nod));
21554
21555 when N_Allocator =>
21556 Error_Node := Expression (Related_Nod);
21557
21558 when others =>
21559 pragma Assert (False);
21560 Error_Node := Related_Nod;
21561 end case;
21562
21563 Error_Msg_NE
21564 ("`NOT NULL` not allowed (& already excludes null)",
21565 Error_Node,
21566 Entity (S));
21567 end if;
21568
21569 Set_Etype (S,
21570 Create_Null_Excluding_Itype
21571 (T => Entity (S),
21572 Related_Nod => P));
21573 Set_Entity (S, Etype (S));
21574 end if;
21575
21576 return Entity (S);
21577
21578 -- Case of constraint present, so that we have an N_Subtype_Indication
21579 -- node (this node is created only if constraints are present).
21580
21581 else
21582 Find_Type (Subtype_Mark (S));
21583
21584 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21585 and then not
21586 (Nkind (Parent (S)) = N_Subtype_Declaration
21587 and then Is_Itype (Defining_Identifier (Parent (S))))
21588 then
21589 Check_Incomplete (Subtype_Mark (S));
21590 end if;
21591
21592 P := Parent (S);
21593 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21594
21595 -- Explicit subtype declaration case
21596
21597 if Nkind (P) = N_Subtype_Declaration then
21598 Def_Id := Defining_Identifier (P);
21599
21600 -- Explicit derived type definition case
21601
21602 elsif Nkind (P) = N_Derived_Type_Definition then
21603 Def_Id := Defining_Identifier (Parent (P));
21604
21605 -- Implicit case, the Def_Id must be created as an implicit type.
21606 -- The one exception arises in the case of concurrent types, array
21607 -- and access types, where other subsidiary implicit types may be
21608 -- created and must appear before the main implicit type. In these
21609 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21610 -- has not yet been called to create Def_Id.
21611
21612 else
21613 if Is_Array_Type (Subtype_Mark_Id)
21614 or else Is_Concurrent_Type (Subtype_Mark_Id)
21615 or else Is_Access_Type (Subtype_Mark_Id)
21616 then
21617 Def_Id := Empty;
21618
21619 -- For the other cases, we create a new unattached Itype,
21620 -- and set the indication to ensure it gets attached later.
21621
21622 else
21623 Def_Id :=
21624 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21625 end if;
21626 end if;
21627
21628 -- If the kind of constraint is invalid for this kind of type,
21629 -- then give an error, and then pretend no constraint was given.
21630
21631 if not Is_Valid_Constraint_Kind
21632 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21633 then
21634 Error_Msg_N
21635 ("incorrect constraint for this kind of type", Constraint (S));
21636
21637 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21638
21639 -- Set Ekind of orphan itype, to prevent cascaded errors
21640
21641 if Present (Def_Id) then
21642 Set_Ekind (Def_Id, Ekind (Any_Type));
21643 end if;
21644
21645 -- Make recursive call, having got rid of the bogus constraint
21646
21647 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21648 end if;
21649
21650 -- Remaining processing depends on type. Select on Base_Type kind to
21651 -- ensure getting to the concrete type kind in the case of a private
21652 -- subtype (needed when only doing semantic analysis).
21653
21654 case Ekind (Base_Type (Subtype_Mark_Id)) is
21655 when Access_Kind =>
21656
21657 -- If this is a constraint on a class-wide type, discard it.
21658 -- There is currently no way to express a partial discriminant
21659 -- constraint on a type with unknown discriminants. This is
21660 -- a pathology that the ACATS wisely decides not to test.
21661
21662 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21663 if Comes_From_Source (S) then
21664 Error_Msg_N
21665 ("constraint on class-wide type ignored??",
21666 Constraint (S));
21667 end if;
21668
21669 if Nkind (P) = N_Subtype_Declaration then
21670 Set_Subtype_Indication (P,
21671 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21672 end if;
21673
21674 return Subtype_Mark_Id;
21675 end if;
21676
21677 Constrain_Access (Def_Id, S, Related_Nod);
21678
21679 if Expander_Active
21680 and then Is_Itype (Designated_Type (Def_Id))
21681 and then Nkind (Related_Nod) = N_Subtype_Declaration
21682 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21683 then
21684 Build_Itype_Reference
21685 (Designated_Type (Def_Id), Related_Nod);
21686 end if;
21687
21688 when Array_Kind =>
21689 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21690
21691 when Decimal_Fixed_Point_Kind =>
21692 Constrain_Decimal (Def_Id, S);
21693
21694 when Enumeration_Kind =>
21695 Constrain_Enumeration (Def_Id, S);
21696
21697 when Ordinary_Fixed_Point_Kind =>
21698 Constrain_Ordinary_Fixed (Def_Id, S);
21699
21700 when Float_Kind =>
21701 Constrain_Float (Def_Id, S);
21702
21703 when Integer_Kind =>
21704 Constrain_Integer (Def_Id, S);
21705
21706 when Class_Wide_Kind
21707 | E_Incomplete_Type
21708 | E_Record_Subtype
21709 | E_Record_Type
21710 =>
21711 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21712
21713 if Ekind (Def_Id) = E_Incomplete_Type then
21714 Set_Private_Dependents (Def_Id, New_Elmt_List);
21715 end if;
21716
21717 when Private_Kind =>
21718
21719 -- A private type with unknown discriminants may be completed
21720 -- by an unconstrained array type.
21721
21722 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21723 and then Present (Full_View (Subtype_Mark_Id))
21724 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21725 then
21726 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21727
21728 -- ... but more commonly is completed by a discriminated record
21729 -- type.
21730
21731 else
21732 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21733 end if;
21734
21735 -- The base type may be private but Def_Id may be a full view
21736 -- in an instance.
21737
21738 if Is_Private_Type (Def_Id) then
21739 Set_Private_Dependents (Def_Id, New_Elmt_List);
21740 end if;
21741
21742 -- In case of an invalid constraint prevent further processing
21743 -- since the type constructed is missing expected fields.
21744
21745 if Etype (Def_Id) = Any_Type then
21746 return Def_Id;
21747 end if;
21748
21749 -- If the full view is that of a task with discriminants,
21750 -- we must constrain both the concurrent type and its
21751 -- corresponding record type. Otherwise we will just propagate
21752 -- the constraint to the full view, if available.
21753
21754 if Present (Full_View (Subtype_Mark_Id))
21755 and then Has_Discriminants (Subtype_Mark_Id)
21756 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21757 then
21758 Full_View_Id :=
21759 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21760
21761 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21762 Constrain_Concurrent (Full_View_Id, S,
21763 Related_Nod, Related_Id, Suffix);
21764 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21765 Set_Full_View (Def_Id, Full_View_Id);
21766
21767 -- Introduce an explicit reference to the private subtype,
21768 -- to prevent scope anomalies in gigi if first use appears
21769 -- in a nested context, e.g. a later function body.
21770 -- Should this be generated in other contexts than a full
21771 -- type declaration?
21772
21773 if Is_Itype (Def_Id)
21774 and then
21775 Nkind (Parent (P)) = N_Full_Type_Declaration
21776 then
21777 Build_Itype_Reference (Def_Id, Parent (P));
21778 end if;
21779
21780 else
21781 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21782 end if;
21783
21784 when Concurrent_Kind =>
21785 Constrain_Concurrent (Def_Id, S,
21786 Related_Nod, Related_Id, Suffix);
21787
21788 when others =>
21789 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21790 end case;
21791
21792 -- Size, Alignment, Representation aspects and Convention are always
21793 -- inherited from the base type.
21794
21795 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21796 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21797 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21798
21799 -- The anonymous subtype created for the subtype indication
21800 -- inherits the predicates of the parent.
21801
21802 if Has_Predicates (Subtype_Mark_Id) then
21803 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21804
21805 -- Indicate where the predicate function may be found
21806
21807 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21808 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21809 end if;
21810 end if;
21811
21812 return Def_Id;
21813 end if;
21814 end Process_Subtype;
21815
21816 -----------------------------
21817 -- Record_Type_Declaration --
21818 -----------------------------
21819
21820 procedure Record_Type_Declaration
21821 (T : Entity_Id;
21822 N : Node_Id;
21823 Prev : Entity_Id)
21824 is
21825 Def : constant Node_Id := Type_Definition (N);
21826 Is_Tagged : Boolean;
21827 Tag_Comp : Entity_Id;
21828
21829 begin
21830 -- These flags must be initialized before calling Process_Discriminants
21831 -- because this routine makes use of them.
21832
21833 Set_Ekind (T, E_Record_Type);
21834 Set_Etype (T, T);
21835 Init_Size_Align (T);
21836 Set_Interfaces (T, No_Elist);
21837 Set_Stored_Constraint (T, No_Elist);
21838 Set_Default_SSO (T);
21839 Set_No_Reordering (T, No_Component_Reordering);
21840
21841 -- Normal case
21842
21843 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21844 if Limited_Present (Def) then
21845 Check_SPARK_05_Restriction ("limited is not allowed", N);
21846 end if;
21847
21848 if Abstract_Present (Def) then
21849 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21850 end if;
21851
21852 -- The flag Is_Tagged_Type might have already been set by
21853 -- Find_Type_Name if it detected an error for declaration T. This
21854 -- arises in the case of private tagged types where the full view
21855 -- omits the word tagged.
21856
21857 Is_Tagged :=
21858 Tagged_Present (Def)
21859 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21860
21861 Set_Is_Limited_Record (T, Limited_Present (Def));
21862
21863 if Is_Tagged then
21864 Set_Is_Tagged_Type (T, True);
21865 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21866 end if;
21867
21868 -- Type is abstract if full declaration carries keyword, or if
21869 -- previous partial view did.
21870
21871 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21872 or else Abstract_Present (Def));
21873
21874 else
21875 Check_SPARK_05_Restriction ("interface is not allowed", N);
21876
21877 Is_Tagged := True;
21878 Analyze_Interface_Declaration (T, Def);
21879
21880 if Present (Discriminant_Specifications (N)) then
21881 Error_Msg_N
21882 ("interface types cannot have discriminants",
21883 Defining_Identifier
21884 (First (Discriminant_Specifications (N))));
21885 end if;
21886 end if;
21887
21888 -- First pass: if there are self-referential access components,
21889 -- create the required anonymous access type declarations, and if
21890 -- need be an incomplete type declaration for T itself.
21891
21892 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21893
21894 if Ada_Version >= Ada_2005
21895 and then Present (Interface_List (Def))
21896 then
21897 Check_Interfaces (N, Def);
21898
21899 declare
21900 Ifaces_List : Elist_Id;
21901
21902 begin
21903 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21904 -- already in the parents.
21905
21906 Collect_Interfaces
21907 (T => T,
21908 Ifaces_List => Ifaces_List,
21909 Exclude_Parents => True);
21910
21911 Set_Interfaces (T, Ifaces_List);
21912 end;
21913 end if;
21914
21915 -- Records constitute a scope for the component declarations within.
21916 -- The scope is created prior to the processing of these declarations.
21917 -- Discriminants are processed first, so that they are visible when
21918 -- processing the other components. The Ekind of the record type itself
21919 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21920
21921 -- Enter record scope
21922
21923 Push_Scope (T);
21924
21925 -- If an incomplete or private type declaration was already given for
21926 -- the type, then this scope already exists, and the discriminants have
21927 -- been declared within. We must verify that the full declaration
21928 -- matches the incomplete one.
21929
21930 Check_Or_Process_Discriminants (N, T, Prev);
21931
21932 Set_Is_Constrained (T, not Has_Discriminants (T));
21933 Set_Has_Delayed_Freeze (T, True);
21934
21935 -- For tagged types add a manually analyzed component corresponding
21936 -- to the component _tag, the corresponding piece of tree will be
21937 -- expanded as part of the freezing actions if it is not a CPP_Class.
21938
21939 if Is_Tagged then
21940
21941 -- Do not add the tag unless we are in expansion mode
21942
21943 if Expander_Active then
21944 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21945 Enter_Name (Tag_Comp);
21946
21947 Set_Ekind (Tag_Comp, E_Component);
21948 Set_Is_Tag (Tag_Comp);
21949 Set_Is_Aliased (Tag_Comp);
21950 Set_Etype (Tag_Comp, RTE (RE_Tag));
21951 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21952 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21953 Init_Component_Location (Tag_Comp);
21954
21955 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21956 -- implemented interfaces.
21957
21958 if Has_Interfaces (T) then
21959 Add_Interface_Tag_Components (N, T);
21960 end if;
21961 end if;
21962
21963 Make_Class_Wide_Type (T);
21964 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21965 end if;
21966
21967 -- We must suppress range checks when processing record components in
21968 -- the presence of discriminants, since we don't want spurious checks to
21969 -- be generated during their analysis, but Suppress_Range_Checks flags
21970 -- must be reset the after processing the record definition.
21971
21972 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21973 -- couldn't we just use the normal range check suppression method here.
21974 -- That would seem cleaner ???
21975
21976 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21977 Set_Kill_Range_Checks (T, True);
21978 Record_Type_Definition (Def, Prev);
21979 Set_Kill_Range_Checks (T, False);
21980 else
21981 Record_Type_Definition (Def, Prev);
21982 end if;
21983
21984 -- Exit from record scope
21985
21986 End_Scope;
21987
21988 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21989 -- the implemented interfaces and associate them an aliased entity.
21990
21991 if Is_Tagged
21992 and then not Is_Empty_List (Interface_List (Def))
21993 then
21994 Derive_Progenitor_Subprograms (T, T);
21995 end if;
21996
21997 Check_Function_Writable_Actuals (N);
21998 end Record_Type_Declaration;
21999
22000 ----------------------------
22001 -- Record_Type_Definition --
22002 ----------------------------
22003
22004 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22005 Component : Entity_Id;
22006 Ctrl_Components : Boolean := False;
22007 Final_Storage_Only : Boolean;
22008 T : Entity_Id;
22009
22010 begin
22011 if Ekind (Prev_T) = E_Incomplete_Type then
22012 T := Full_View (Prev_T);
22013 else
22014 T := Prev_T;
22015 end if;
22016
22017 -- In SPARK, tagged types and type extensions may only be declared in
22018 -- the specification of library unit packages.
22019
22020 if Present (Def) and then Is_Tagged_Type (T) then
22021 declare
22022 Typ : Node_Id;
22023 Ctxt : Node_Id;
22024
22025 begin
22026 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22027 Typ := Parent (Def);
22028 else
22029 pragma Assert
22030 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22031 Typ := Parent (Parent (Def));
22032 end if;
22033
22034 Ctxt := Parent (Typ);
22035
22036 if Nkind (Ctxt) = N_Package_Body
22037 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22038 then
22039 Check_SPARK_05_Restriction
22040 ("type should be defined in package specification", Typ);
22041
22042 elsif Nkind (Ctxt) /= N_Package_Specification
22043 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22044 then
22045 Check_SPARK_05_Restriction
22046 ("type should be defined in library unit package", Typ);
22047 end if;
22048 end;
22049 end if;
22050
22051 Final_Storage_Only := not Is_Controlled (T);
22052
22053 -- Ada 2005: Check whether an explicit Limited is present in a derived
22054 -- type declaration.
22055
22056 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22057 and then Limited_Present (Parent (Def))
22058 then
22059 Set_Is_Limited_Record (T);
22060 end if;
22061
22062 -- If the component list of a record type is defined by the reserved
22063 -- word null and there is no discriminant part, then the record type has
22064 -- no components and all records of the type are null records (RM 3.7)
22065 -- This procedure is also called to process the extension part of a
22066 -- record extension, in which case the current scope may have inherited
22067 -- components.
22068
22069 if No (Def)
22070 or else No (Component_List (Def))
22071 or else Null_Present (Component_List (Def))
22072 then
22073 if not Is_Tagged_Type (T) then
22074 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22075 end if;
22076
22077 else
22078 Analyze_Declarations (Component_Items (Component_List (Def)));
22079
22080 if Present (Variant_Part (Component_List (Def))) then
22081 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22082 Analyze (Variant_Part (Component_List (Def)));
22083 end if;
22084 end if;
22085
22086 -- After completing the semantic analysis of the record definition,
22087 -- record components, both new and inherited, are accessible. Set their
22088 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22089 -- whose Ekind may be void.
22090
22091 Component := First_Entity (Current_Scope);
22092 while Present (Component) loop
22093 if Ekind (Component) = E_Void
22094 and then not Is_Itype (Component)
22095 then
22096 Set_Ekind (Component, E_Component);
22097 Init_Component_Location (Component);
22098 end if;
22099
22100 Propagate_Concurrent_Flags (T, Etype (Component));
22101
22102 if Ekind (Component) /= E_Component then
22103 null;
22104
22105 -- Do not set Has_Controlled_Component on a class-wide equivalent
22106 -- type. See Make_CW_Equivalent_Type.
22107
22108 elsif not Is_Class_Wide_Equivalent_Type (T)
22109 and then (Has_Controlled_Component (Etype (Component))
22110 or else (Chars (Component) /= Name_uParent
22111 and then Is_Controlled (Etype (Component))))
22112 then
22113 Set_Has_Controlled_Component (T, True);
22114 Final_Storage_Only :=
22115 Final_Storage_Only
22116 and then Finalize_Storage_Only (Etype (Component));
22117 Ctrl_Components := True;
22118 end if;
22119
22120 Next_Entity (Component);
22121 end loop;
22122
22123 -- A Type is Finalize_Storage_Only only if all its controlled components
22124 -- are also.
22125
22126 if Ctrl_Components then
22127 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22128 end if;
22129
22130 -- Place reference to end record on the proper entity, which may
22131 -- be a partial view.
22132
22133 if Present (Def) then
22134 Process_End_Label (Def, 'e', Prev_T);
22135 end if;
22136 end Record_Type_Definition;
22137
22138 ------------------------
22139 -- Replace_Components --
22140 ------------------------
22141
22142 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
22143 function Process (N : Node_Id) return Traverse_Result;
22144
22145 -------------
22146 -- Process --
22147 -------------
22148
22149 function Process (N : Node_Id) return Traverse_Result is
22150 Comp : Entity_Id;
22151
22152 begin
22153 if Nkind (N) = N_Discriminant_Specification then
22154 Comp := First_Discriminant (Typ);
22155 while Present (Comp) loop
22156 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22157 Set_Defining_Identifier (N, Comp);
22158 exit;
22159 end if;
22160
22161 Next_Discriminant (Comp);
22162 end loop;
22163
22164 elsif Nkind (N) = N_Variant_Part then
22165 Comp := First_Discriminant (Typ);
22166 while Present (Comp) loop
22167 if Chars (Comp) = Chars (Name (N)) then
22168 Set_Entity (Name (N), Comp);
22169 exit;
22170 end if;
22171
22172 Next_Discriminant (Comp);
22173 end loop;
22174
22175 elsif Nkind (N) = N_Component_Declaration then
22176 Comp := First_Component (Typ);
22177 while Present (Comp) loop
22178 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22179 Set_Defining_Identifier (N, Comp);
22180 exit;
22181 end if;
22182
22183 Next_Component (Comp);
22184 end loop;
22185 end if;
22186
22187 return OK;
22188 end Process;
22189
22190 procedure Replace is new Traverse_Proc (Process);
22191
22192 -- Start of processing for Replace_Components
22193
22194 begin
22195 Replace (Decl);
22196 end Replace_Components;
22197
22198 -------------------------------
22199 -- Set_Completion_Referenced --
22200 -------------------------------
22201
22202 procedure Set_Completion_Referenced (E : Entity_Id) is
22203 begin
22204 -- If in main unit, mark entity that is a completion as referenced,
22205 -- warnings go on the partial view when needed.
22206
22207 if In_Extended_Main_Source_Unit (E) then
22208 Set_Referenced (E);
22209 end if;
22210 end Set_Completion_Referenced;
22211
22212 ---------------------
22213 -- Set_Default_SSO --
22214 ---------------------
22215
22216 procedure Set_Default_SSO (T : Entity_Id) is
22217 begin
22218 case Opt.Default_SSO is
22219 when ' ' =>
22220 null;
22221 when 'L' =>
22222 Set_SSO_Set_Low_By_Default (T, True);
22223 when 'H' =>
22224 Set_SSO_Set_High_By_Default (T, True);
22225 when others =>
22226 raise Program_Error;
22227 end case;
22228 end Set_Default_SSO;
22229
22230 ---------------------
22231 -- Set_Fixed_Range --
22232 ---------------------
22233
22234 -- The range for fixed-point types is complicated by the fact that we
22235 -- do not know the exact end points at the time of the declaration. This
22236 -- is true for three reasons:
22237
22238 -- A size clause may affect the fudging of the end-points.
22239 -- A small clause may affect the values of the end-points.
22240 -- We try to include the end-points if it does not affect the size.
22241
22242 -- This means that the actual end-points must be established at the
22243 -- point when the type is frozen. Meanwhile, we first narrow the range
22244 -- as permitted (so that it will fit if necessary in a small specified
22245 -- size), and then build a range subtree with these narrowed bounds.
22246 -- Set_Fixed_Range constructs the range from real literal values, and
22247 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22248
22249 -- The parent of this range is set to point to the entity so that it is
22250 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22251 -- other scalar types, which are just pointers to the range in the
22252 -- original tree, this would otherwise be an orphan).
22253
22254 -- The tree is left unanalyzed. When the type is frozen, the processing
22255 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22256 -- analyzed, and uses this as an indication that it should complete
22257 -- work on the range (it will know the final small and size values).
22258
22259 procedure Set_Fixed_Range
22260 (E : Entity_Id;
22261 Loc : Source_Ptr;
22262 Lo : Ureal;
22263 Hi : Ureal)
22264 is
22265 S : constant Node_Id :=
22266 Make_Range (Loc,
22267 Low_Bound => Make_Real_Literal (Loc, Lo),
22268 High_Bound => Make_Real_Literal (Loc, Hi));
22269 begin
22270 Set_Scalar_Range (E, S);
22271 Set_Parent (S, E);
22272
22273 -- Before the freeze point, the bounds of a fixed point are universal
22274 -- and carry the corresponding type.
22275
22276 Set_Etype (Low_Bound (S), Universal_Real);
22277 Set_Etype (High_Bound (S), Universal_Real);
22278 end Set_Fixed_Range;
22279
22280 ----------------------------------
22281 -- Set_Scalar_Range_For_Subtype --
22282 ----------------------------------
22283
22284 procedure Set_Scalar_Range_For_Subtype
22285 (Def_Id : Entity_Id;
22286 R : Node_Id;
22287 Subt : Entity_Id)
22288 is
22289 Kind : constant Entity_Kind := Ekind (Def_Id);
22290
22291 begin
22292 -- Defend against previous error
22293
22294 if Nkind (R) = N_Error then
22295 return;
22296 end if;
22297
22298 Set_Scalar_Range (Def_Id, R);
22299
22300 -- We need to link the range into the tree before resolving it so
22301 -- that types that are referenced, including importantly the subtype
22302 -- itself, are properly frozen (Freeze_Expression requires that the
22303 -- expression be properly linked into the tree). Of course if it is
22304 -- already linked in, then we do not disturb the current link.
22305
22306 if No (Parent (R)) then
22307 Set_Parent (R, Def_Id);
22308 end if;
22309
22310 -- Reset the kind of the subtype during analysis of the range, to
22311 -- catch possible premature use in the bounds themselves.
22312
22313 Set_Ekind (Def_Id, E_Void);
22314 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22315 Set_Ekind (Def_Id, Kind);
22316 end Set_Scalar_Range_For_Subtype;
22317
22318 --------------------------------------------------------
22319 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22320 --------------------------------------------------------
22321
22322 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22323 (E : Entity_Id)
22324 is
22325 begin
22326 -- Make sure set if encountered during Expand_To_Stored_Constraint
22327
22328 Set_Stored_Constraint (E, No_Elist);
22329
22330 -- Give it the right value
22331
22332 if Is_Constrained (E) and then Has_Discriminants (E) then
22333 Set_Stored_Constraint (E,
22334 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22335 end if;
22336 end Set_Stored_Constraint_From_Discriminant_Constraint;
22337
22338 -------------------------------------
22339 -- Signed_Integer_Type_Declaration --
22340 -------------------------------------
22341
22342 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22343 Implicit_Base : Entity_Id;
22344 Base_Typ : Entity_Id;
22345 Lo_Val : Uint;
22346 Hi_Val : Uint;
22347 Errs : Boolean := False;
22348 Lo : Node_Id;
22349 Hi : Node_Id;
22350
22351 function Can_Derive_From (E : Entity_Id) return Boolean;
22352 -- Determine whether given bounds allow derivation from specified type
22353
22354 procedure Check_Bound (Expr : Node_Id);
22355 -- Check bound to make sure it is integral and static. If not, post
22356 -- appropriate error message and set Errs flag
22357
22358 ---------------------
22359 -- Can_Derive_From --
22360 ---------------------
22361
22362 -- Note we check both bounds against both end values, to deal with
22363 -- strange types like ones with a range of 0 .. -12341234.
22364
22365 function Can_Derive_From (E : Entity_Id) return Boolean is
22366 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22367 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22368 begin
22369 return Lo <= Lo_Val and then Lo_Val <= Hi
22370 and then
22371 Lo <= Hi_Val and then Hi_Val <= Hi;
22372 end Can_Derive_From;
22373
22374 -----------------
22375 -- Check_Bound --
22376 -----------------
22377
22378 procedure Check_Bound (Expr : Node_Id) is
22379 begin
22380 -- If a range constraint is used as an integer type definition, each
22381 -- bound of the range must be defined by a static expression of some
22382 -- integer type, but the two bounds need not have the same integer
22383 -- type (Negative bounds are allowed.) (RM 3.5.4)
22384
22385 if not Is_Integer_Type (Etype (Expr)) then
22386 Error_Msg_N
22387 ("integer type definition bounds must be of integer type", Expr);
22388 Errs := True;
22389
22390 elsif not Is_OK_Static_Expression (Expr) then
22391 Flag_Non_Static_Expr
22392 ("non-static expression used for integer type bound!", Expr);
22393 Errs := True;
22394
22395 -- The bounds are folded into literals, and we set their type to be
22396 -- universal, to avoid typing difficulties: we cannot set the type
22397 -- of the literal to the new type, because this would be a forward
22398 -- reference for the back end, and if the original type is user-
22399 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22400
22401 else
22402 if Is_Entity_Name (Expr) then
22403 Fold_Uint (Expr, Expr_Value (Expr), True);
22404 end if;
22405
22406 Set_Etype (Expr, Universal_Integer);
22407 end if;
22408 end Check_Bound;
22409
22410 -- Start of processing for Signed_Integer_Type_Declaration
22411
22412 begin
22413 -- Create an anonymous base type
22414
22415 Implicit_Base :=
22416 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22417
22418 -- Analyze and check the bounds, they can be of any integer type
22419
22420 Lo := Low_Bound (Def);
22421 Hi := High_Bound (Def);
22422
22423 -- Arbitrarily use Integer as the type if either bound had an error
22424
22425 if Hi = Error or else Lo = Error then
22426 Base_Typ := Any_Integer;
22427 Set_Error_Posted (T, True);
22428
22429 -- Here both bounds are OK expressions
22430
22431 else
22432 Analyze_And_Resolve (Lo, Any_Integer);
22433 Analyze_And_Resolve (Hi, Any_Integer);
22434
22435 Check_Bound (Lo);
22436 Check_Bound (Hi);
22437
22438 if Errs then
22439 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22440 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22441 end if;
22442
22443 -- Find type to derive from
22444
22445 Lo_Val := Expr_Value (Lo);
22446 Hi_Val := Expr_Value (Hi);
22447
22448 if Can_Derive_From (Standard_Short_Short_Integer) then
22449 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22450
22451 elsif Can_Derive_From (Standard_Short_Integer) then
22452 Base_Typ := Base_Type (Standard_Short_Integer);
22453
22454 elsif Can_Derive_From (Standard_Integer) then
22455 Base_Typ := Base_Type (Standard_Integer);
22456
22457 elsif Can_Derive_From (Standard_Long_Integer) then
22458 Base_Typ := Base_Type (Standard_Long_Integer);
22459
22460 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22461 Check_Restriction (No_Long_Long_Integers, Def);
22462 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22463
22464 else
22465 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22466 Error_Msg_N ("integer type definition bounds out of range", Def);
22467 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22468 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22469 end if;
22470 end if;
22471
22472 -- Complete both implicit base and declared first subtype entities. The
22473 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22474 -- are not clobbered when the signed integer type acts as a full view of
22475 -- a private type.
22476
22477 Set_Etype (Implicit_Base, Base_Typ);
22478 Set_Size_Info (Implicit_Base, Base_Typ);
22479 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22480 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22481 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22482
22483 Set_Ekind (T, E_Signed_Integer_Subtype);
22484 Set_Etype (T, Implicit_Base);
22485 Set_Size_Info (T, Implicit_Base);
22486 Inherit_Rep_Item_Chain (T, Implicit_Base);
22487 Set_Scalar_Range (T, Def);
22488 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22489 Set_Is_Constrained (T);
22490 end Signed_Integer_Type_Declaration;
22491
22492 end Sem_Ch3;