[Ada] Introduce a new Needs_Activation_Record entity flag
[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 Prepare_Private_Subtype_Completion
609 (Id : Entity_Id;
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
615
616 procedure Process_Incomplete_Dependents
617 (N : Node_Id;
618 Full_T : Entity_Id;
619 Inc_T : Entity_Id);
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
623 -- incomplete type.
624
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
627 --
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 --
630 -- Full_T is N's defining identifier.
631 --
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
637 -- full type.
638 --
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
642
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
648
649 procedure Record_Type_Declaration
650 (T : Entity_Id;
651 N : Node_Id;
652 Prev : Entity_Id);
653 -- Process a record type declaration (for both untagged and tagged
654 -- records). Parameters T and N are exactly like in procedure
655 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
656 -- for this routine. If this is the completion of an incomplete type
657 -- declaration, Prev is the entity of the incomplete declaration, used for
658 -- cross-referencing. Otherwise Prev = T.
659
660 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
661 -- This routine is used to process the actual record type definition (both
662 -- for untagged and tagged records). Def is a record type definition node.
663 -- This procedure analyzes the components in this record type definition.
664 -- Prev_T is the entity for the enclosing record type. It is provided so
665 -- that its Has_Task flag can be set if any of the component have Has_Task
666 -- set. If the declaration is the completion of an incomplete type
667 -- declaration, Prev_T is the original incomplete type, whose full view is
668 -- the record type.
669
670 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
671 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
672 -- build a copy of the declaration tree of the parent, and we create
673 -- independently the list of components for the derived type. Semantic
674 -- information uses the component entities, but record representation
675 -- clauses are validated on the declaration tree. This procedure replaces
676 -- discriminants and components in the declaration with those that have
677 -- been created by Inherit_Components.
678
679 procedure Set_Fixed_Range
680 (E : Entity_Id;
681 Loc : Source_Ptr;
682 Lo : Ureal;
683 Hi : Ureal);
684 -- Build a range node with the given bounds and set it as the Scalar_Range
685 -- of the given fixed-point type entity. Loc is the source location used
686 -- for the constructed range. See body for further details.
687
688 procedure Set_Scalar_Range_For_Subtype
689 (Def_Id : Entity_Id;
690 R : Node_Id;
691 Subt : Entity_Id);
692 -- This routine is used to set the scalar range field for a subtype given
693 -- Def_Id, the entity for the subtype, and R, the range expression for the
694 -- scalar range. Subt provides the parent subtype to be used to analyze,
695 -- resolve, and check the given range.
696
697 procedure Set_Default_SSO (T : Entity_Id);
698 -- T is the entity for an array or record being declared. This procedure
699 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
700 -- to the setting of Opt.Default_SSO.
701
702 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
703 -- Create a new signed integer entity, and apply the constraint to obtain
704 -- the required first named subtype of this type.
705
706 procedure Set_Stored_Constraint_From_Discriminant_Constraint
707 (E : Entity_Id);
708 -- E is some record type. This routine computes E's Stored_Constraint
709 -- from its Discriminant_Constraint.
710
711 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
712 -- Check that an entity in a list of progenitors is an interface,
713 -- emit error otherwise.
714
715 -----------------------
716 -- Access_Definition --
717 -----------------------
718
719 function Access_Definition
720 (Related_Nod : Node_Id;
721 N : Node_Id) return Entity_Id
722 is
723 Anon_Type : Entity_Id;
724 Anon_Scope : Entity_Id;
725 Desig_Type : Entity_Id;
726 Enclosing_Prot_Type : Entity_Id := Empty;
727
728 begin
729 Check_SPARK_05_Restriction ("access type is not allowed", N);
730
731 if Is_Entry (Current_Scope)
732 and then Is_Task_Type (Etype (Scope (Current_Scope)))
733 then
734 Error_Msg_N ("task entries cannot have access parameters", N);
735 return Empty;
736 end if;
737
738 -- Ada 2005: For an object declaration the corresponding anonymous
739 -- type is declared in the current scope.
740
741 -- If the access definition is the return type of another access to
742 -- function, scope is the current one, because it is the one of the
743 -- current type declaration, except for the pathological case below.
744
745 if Nkind_In (Related_Nod, N_Object_Declaration,
746 N_Access_Function_Definition)
747 then
748 Anon_Scope := Current_Scope;
749
750 -- A pathological case: function returning access functions that
751 -- return access functions, etc. Each anonymous access type created
752 -- is in the enclosing scope of the outermost function.
753
754 declare
755 Par : Node_Id;
756
757 begin
758 Par := Related_Nod;
759 while Nkind_In (Par, N_Access_Function_Definition,
760 N_Access_Definition)
761 loop
762 Par := Parent (Par);
763 end loop;
764
765 if Nkind (Par) = N_Function_Specification then
766 Anon_Scope := Scope (Defining_Entity (Par));
767 end if;
768 end;
769
770 -- For the anonymous function result case, retrieve the scope of the
771 -- function specification's associated entity rather than using the
772 -- current scope. The current scope will be the function itself if the
773 -- formal part is currently being analyzed, but will be the parent scope
774 -- in the case of a parameterless function, and we always want to use
775 -- the function's parent scope. Finally, if the function is a child
776 -- unit, we must traverse the tree to retrieve the proper entity.
777
778 elsif Nkind (Related_Nod) = N_Function_Specification
779 and then Nkind (Parent (N)) /= N_Parameter_Specification
780 then
781 -- If the current scope is a protected type, the anonymous access
782 -- is associated with one of the protected operations, and must
783 -- be available in the scope that encloses the protected declaration.
784 -- Otherwise the type is in the scope enclosing the subprogram.
785
786 -- If the function has formals, The return type of a subprogram
787 -- declaration is analyzed in the scope of the subprogram (see
788 -- Process_Formals) and thus the protected type, if present, is
789 -- the scope of the current function scope.
790
791 if Ekind (Current_Scope) = E_Protected_Type then
792 Enclosing_Prot_Type := Current_Scope;
793
794 elsif Ekind (Current_Scope) = E_Function
795 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
796 then
797 Enclosing_Prot_Type := Scope (Current_Scope);
798 end if;
799
800 if Present (Enclosing_Prot_Type) then
801 Anon_Scope := Scope (Enclosing_Prot_Type);
802
803 else
804 Anon_Scope := Scope (Defining_Entity (Related_Nod));
805 end if;
806
807 -- For an access type definition, if the current scope is a child
808 -- unit it is the scope of the type.
809
810 elsif Is_Compilation_Unit (Current_Scope) then
811 Anon_Scope := Current_Scope;
812
813 -- For access formals, access components, and access discriminants, the
814 -- scope is that of the enclosing declaration,
815
816 else
817 Anon_Scope := Scope (Current_Scope);
818 end if;
819
820 Anon_Type :=
821 Create_Itype
822 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823
824 if All_Present (N)
825 and then Ada_Version >= Ada_2005
826 then
827 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
828 end if;
829
830 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
831 -- the corresponding semantic routine
832
833 if Present (Access_To_Subprogram_Definition (N)) then
834
835 -- Compiler runtime units are compiled in Ada 2005 mode when building
836 -- the runtime library but must also be compilable in Ada 95 mode
837 -- (when bootstrapping the compiler).
838
839 Check_Compiler_Unit ("anonymous access to subprogram", N);
840
841 Access_Subprogram_Declaration
842 (T_Name => Anon_Type,
843 T_Def => Access_To_Subprogram_Definition (N));
844
845 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
846 Set_Ekind
847 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
848 else
849 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
850 end if;
851
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
854
855 -- If the anonymous access is associated with a protected operation,
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
858
859 -- If the anonymous access itself is protected, a full type
860 -- declaratiton will be created for it, so that the equivalent
861 -- record type can be constructed. For further details, see
862 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863
864 if Ekind (Current_Scope) = E_Protected_Type
865 and then not Protected_Present (Access_To_Subprogram_Definition (N))
866 then
867 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
868 end if;
869
870 return Anon_Type;
871 end if;
872
873 Find_Type (Subtype_Mark (N));
874 Desig_Type := Entity (Subtype_Mark (N));
875
876 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
877 Set_Etype (Anon_Type, Anon_Type);
878
879 -- Make sure the anonymous access type has size and alignment fields
880 -- set, as required by gigi. This is necessary in the case of the
881 -- Task_Body_Procedure.
882
883 if not Has_Private_Component (Desig_Type) then
884 Layout_Type (Anon_Type);
885 end if;
886
887 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
888 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
889 -- the null value is allowed. In Ada 95 the null value is never allowed.
890
891 if Ada_Version >= Ada_2005 then
892 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
893 else
894 Set_Can_Never_Be_Null (Anon_Type, True);
895 end if;
896
897 -- The anonymous access type is as public as the discriminated type or
898 -- subprogram that defines it. It is imported (for back-end purposes)
899 -- if the designated type is.
900
901 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902
903 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904
905 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906
907 -- The context is either a subprogram declaration, object declaration,
908 -- or an access discriminant, in a private or a full type declaration.
909 -- In the case of a subprogram, if the designated type is incomplete,
910 -- the operation will be a primitive operation of the full type, to be
911 -- updated subsequently. If the type is imported through a limited_with
912 -- clause, the subprogram is not a primitive operation of the type
913 -- (which is declared elsewhere in some other scope).
914
915 if Ekind (Desig_Type) = E_Incomplete_Type
916 and then not From_Limited_With (Desig_Type)
917 and then Is_Overloadable (Current_Scope)
918 then
919 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
920 Set_Has_Delayed_Freeze (Current_Scope);
921 end if;
922
923 -- Ada 2005: If the designated type is an interface that may contain
924 -- tasks, create a Master entity for the declaration. This must be done
925 -- before expansion of the full declaration, because the declaration may
926 -- include an expression that is an allocator, whose expansion needs the
927 -- proper Master for the created tasks.
928
929 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
930 then
931 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
932 then
933 Build_Class_Wide_Master (Anon_Type);
934
935 -- Similarly, if the type is an anonymous access that designates
936 -- tasks, create a master entity for it in the current context.
937
938 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
939 then
940 Build_Master_Entity (Defining_Identifier (Related_Nod));
941 Build_Master_Renaming (Anon_Type);
942 end if;
943 end if;
944
945 -- For a private component of a protected type, it is imperative that
946 -- the back-end elaborate the type immediately after the protected
947 -- declaration, because this type will be used in the declarations
948 -- created for the component within each protected body, so we must
949 -- create an itype reference for it now.
950
951 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
952 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953
954 -- Similarly, if the access definition is the return result of a
955 -- function, create an itype reference for it because it will be used
956 -- within the function body. For a regular function that is not a
957 -- compilation unit, insert reference after the declaration. For a
958 -- protected operation, insert it after the enclosing protected type
959 -- declaration. In either case, do not create a reference for a type
960 -- obtained through a limited_with clause, because this would introduce
961 -- semantic dependencies.
962
963 -- Similarly, do not create a reference if the designated type is a
964 -- generic formal, because no use of it will reach the backend.
965
966 elsif Nkind (Related_Nod) = N_Function_Specification
967 and then not From_Limited_With (Desig_Type)
968 and then not Is_Generic_Type (Desig_Type)
969 then
970 if Present (Enclosing_Prot_Type) then
971 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972
973 elsif Is_List_Member (Parent (Related_Nod))
974 and then Nkind (Parent (N)) /= N_Parameter_Specification
975 then
976 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
977 end if;
978
979 -- Finally, create an itype reference for an object declaration of an
980 -- anonymous access type. This is strictly necessary only for deferred
981 -- constants, but in any case will avoid out-of-scope problems in the
982 -- back-end.
983
984 elsif Nkind (Related_Nod) = N_Object_Declaration then
985 Build_Itype_Reference (Anon_Type, Related_Nod);
986 end if;
987
988 return Anon_Type;
989 end Access_Definition;
990
991 -----------------------------------
992 -- Access_Subprogram_Declaration --
993 -----------------------------------
994
995 procedure Access_Subprogram_Declaration
996 (T_Name : Entity_Id;
997 T_Def : Node_Id)
998 is
999 procedure Check_For_Premature_Usage (Def : Node_Id);
1000 -- Check that type T_Name is not used, directly or recursively, as a
1001 -- parameter or a return type in Def. Def is either a subtype, an
1002 -- access_definition, or an access_to_subprogram_definition.
1003
1004 -------------------------------
1005 -- Check_For_Premature_Usage --
1006 -------------------------------
1007
1008 procedure Check_For_Premature_Usage (Def : Node_Id) is
1009 Param : Node_Id;
1010
1011 begin
1012 -- Check for a subtype mark
1013
1014 if Nkind (Def) in N_Has_Etype then
1015 if Etype (Def) = T_Name then
1016 Error_Msg_N
1017 ("type& cannot be used before end of its declaration", Def);
1018 end if;
1019
1020 -- If this is not a subtype, then this is an access_definition
1021
1022 elsif Nkind (Def) = N_Access_Definition then
1023 if Present (Access_To_Subprogram_Definition (Def)) then
1024 Check_For_Premature_Usage
1025 (Access_To_Subprogram_Definition (Def));
1026 else
1027 Check_For_Premature_Usage (Subtype_Mark (Def));
1028 end if;
1029
1030 -- The only cases left are N_Access_Function_Definition and
1031 -- N_Access_Procedure_Definition.
1032
1033 else
1034 if Present (Parameter_Specifications (Def)) then
1035 Param := First (Parameter_Specifications (Def));
1036 while Present (Param) loop
1037 Check_For_Premature_Usage (Parameter_Type (Param));
1038 Param := Next (Param);
1039 end loop;
1040 end if;
1041
1042 if Nkind (Def) = N_Access_Function_Definition then
1043 Check_For_Premature_Usage (Result_Definition (Def));
1044 end if;
1045 end if;
1046 end Check_For_Premature_Usage;
1047
1048 -- Local variables
1049
1050 Formals : constant List_Id := Parameter_Specifications (T_Def);
1051 Formal : Entity_Id;
1052 D_Ityp : Node_Id;
1053 Desig_Type : constant Entity_Id :=
1054 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055
1056 -- Start of processing for Access_Subprogram_Declaration
1057
1058 begin
1059 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060
1061 -- Associate the Itype node with the inner full-type declaration or
1062 -- subprogram spec or entry body. This is required to handle nested
1063 -- anonymous declarations. For example:
1064
1065 -- procedure P
1066 -- (X : access procedure
1067 -- (Y : access procedure
1068 -- (Z : access T)))
1069
1070 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1071 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1072 N_Private_Type_Declaration,
1073 N_Private_Extension_Declaration,
1074 N_Procedure_Specification,
1075 N_Function_Specification,
1076 N_Entry_Body)
1077
1078 or else
1079 Nkind_In (D_Ityp, N_Object_Declaration,
1080 N_Object_Renaming_Declaration,
1081 N_Formal_Object_Declaration,
1082 N_Formal_Type_Declaration,
1083 N_Task_Type_Declaration,
1084 N_Protected_Type_Declaration))
1085 loop
1086 D_Ityp := Parent (D_Ityp);
1087 pragma Assert (D_Ityp /= Empty);
1088 end loop;
1089
1090 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091
1092 if Nkind_In (D_Ityp, N_Procedure_Specification,
1093 N_Function_Specification)
1094 then
1095 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096
1097 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1098 N_Object_Declaration,
1099 N_Object_Renaming_Declaration,
1100 N_Formal_Type_Declaration)
1101 then
1102 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1103 end if;
1104
1105 if Nkind (T_Def) = N_Access_Function_Definition then
1106 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1107 declare
1108 Acc : constant Node_Id := Result_Definition (T_Def);
1109
1110 begin
1111 if Present (Access_To_Subprogram_Definition (Acc))
1112 and then
1113 Protected_Present (Access_To_Subprogram_Definition (Acc))
1114 then
1115 Set_Etype
1116 (Desig_Type,
1117 Replace_Anonymous_Access_To_Protected_Subprogram
1118 (T_Def));
1119
1120 else
1121 Set_Etype
1122 (Desig_Type,
1123 Access_Definition (T_Def, Result_Definition (T_Def)));
1124 end if;
1125 end;
1126
1127 else
1128 Analyze (Result_Definition (T_Def));
1129
1130 declare
1131 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132
1133 begin
1134 -- If a null exclusion is imposed on the result type, then
1135 -- create a null-excluding itype (an access subtype) and use
1136 -- it as the function's Etype.
1137
1138 if Is_Access_Type (Typ)
1139 and then Null_Exclusion_In_Return_Present (T_Def)
1140 then
1141 Set_Etype (Desig_Type,
1142 Create_Null_Excluding_Itype
1143 (T => Typ,
1144 Related_Nod => T_Def,
1145 Scope_Id => Current_Scope));
1146
1147 else
1148 if From_Limited_With (Typ) then
1149
1150 -- AI05-151: Incomplete types are allowed in all basic
1151 -- declarations, including access to subprograms.
1152
1153 if Ada_Version >= Ada_2012 then
1154 null;
1155
1156 else
1157 Error_Msg_NE
1158 ("illegal use of incomplete type&",
1159 Result_Definition (T_Def), Typ);
1160 end if;
1161
1162 elsif Ekind (Current_Scope) = E_Package
1163 and then In_Private_Part (Current_Scope)
1164 then
1165 if Ekind (Typ) = E_Incomplete_Type then
1166 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167
1168 elsif Is_Class_Wide_Type (Typ)
1169 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1170 then
1171 Append_Elmt
1172 (Desig_Type, Private_Dependents (Etype (Typ)));
1173 end if;
1174 end if;
1175
1176 Set_Etype (Desig_Type, Typ);
1177 end if;
1178 end;
1179 end if;
1180
1181 if not (Is_Type (Etype (Desig_Type))) then
1182 Error_Msg_N
1183 ("expect type in function specification",
1184 Result_Definition (T_Def));
1185 end if;
1186
1187 else
1188 Set_Etype (Desig_Type, Standard_Void_Type);
1189 end if;
1190
1191 if Present (Formals) then
1192 Push_Scope (Desig_Type);
1193
1194 -- Some special tests here. These special tests can be removed
1195 -- if and when Itypes always have proper parent pointers to their
1196 -- declarations???
1197
1198 -- Special test 1) Link defining_identifier of formals. Required by
1199 -- First_Formal to provide its functionality.
1200
1201 declare
1202 F : Node_Id;
1203
1204 begin
1205 F := First (Formals);
1206
1207 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1208 -- when it is part of an unconstrained type and subtype expansion
1209 -- is disabled. To avoid back-end problems with shared profiles,
1210 -- use previous subprogram type as the designated type, and then
1211 -- remove scope added above.
1212
1213 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1214 then
1215 Set_Etype (T_Name, T_Name);
1216 Init_Size_Align (T_Name);
1217 Set_Directly_Designated_Type (T_Name,
1218 Scope (Defining_Identifier (F)));
1219 End_Scope;
1220 return;
1221 end if;
1222
1223 while Present (F) loop
1224 if No (Parent (Defining_Identifier (F))) then
1225 Set_Parent (Defining_Identifier (F), F);
1226 end if;
1227
1228 Next (F);
1229 end loop;
1230 end;
1231
1232 Process_Formals (Formals, Parent (T_Def));
1233
1234 -- Special test 2) End_Scope requires that the parent pointer be set
1235 -- to something reasonable, but Itypes don't have parent pointers. So
1236 -- we set it and then unset it ???
1237
1238 Set_Parent (Desig_Type, T_Name);
1239 End_Scope;
1240 Set_Parent (Desig_Type, Empty);
1241 end if;
1242
1243 -- Check for premature usage of the type being defined
1244
1245 Check_For_Premature_Usage (T_Def);
1246
1247 -- The return type and/or any parameter type may be incomplete. Mark the
1248 -- subprogram_type as depending on the incomplete type, so that it can
1249 -- be updated when the full type declaration is seen. This only applies
1250 -- to incomplete types declared in some enclosing scope, not to limited
1251 -- views from other packages.
1252
1253 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254
1255 if Present (Formals) then
1256 Formal := First_Formal (Desig_Type);
1257 while Present (Formal) loop
1258 if Ekind (Formal) /= E_In_Parameter
1259 and then Nkind (T_Def) = N_Access_Function_Definition
1260 and then Ada_Version < Ada_2012
1261 then
1262 Error_Msg_N ("functions can only have IN parameters", Formal);
1263 end if;
1264
1265 if Ekind (Etype (Formal)) = E_Incomplete_Type
1266 and then In_Open_Scopes (Scope (Etype (Formal)))
1267 then
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1270 end if;
1271
1272 Next_Formal (Formal);
1273 end loop;
1274 end if;
1275
1276 -- Check whether an indirect call without actuals may be possible. This
1277 -- is used when resolving calls whose result is then indexed.
1278
1279 May_Need_Actuals (Desig_Type);
1280
1281 -- If the return type is incomplete, this is legal as long as the type
1282 -- is declared in the current scope and will be completed in it (rather
1283 -- than being part of limited view).
1284
1285 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1286 and then not Has_Delayed_Freeze (Desig_Type)
1287 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1288 then
1289 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1290 Set_Has_Delayed_Freeze (Desig_Type);
1291 end if;
1292
1293 Check_Delayed_Subprogram (Desig_Type);
1294
1295 if Protected_Present (T_Def) then
1296 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1297 Set_Convention (Desig_Type, Convention_Protected);
1298 else
1299 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1300 end if;
1301
1302 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303
1304 Set_Etype (T_Name, T_Name);
1305 Init_Size_Align (T_Name);
1306 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307
1308 -- If the access_to_subprogram is not declared at the library level,
1309 -- it can only point to subprograms that are at the same or deeper
1310 -- accessibility level. All such might require an activation record
1311 -- when compiling for C.
1312
1313 Set_Needs_Activation_Record (T_Name,
1314 not Is_Library_Level_Entity (T_Name));
1315 Set_Needs_Activation_Record (Desig_Type,
1316 not Is_Library_Level_Entity (T_Name));
1317
1318 Generate_Reference_To_Formals (T_Name);
1319
1320 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1321
1322 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1323
1324 Check_Restriction (No_Access_Subprograms, T_Def);
1325 end Access_Subprogram_Declaration;
1326
1327 ----------------------------
1328 -- Access_Type_Declaration --
1329 ----------------------------
1330
1331 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1332 P : constant Node_Id := Parent (Def);
1333 S : constant Node_Id := Subtype_Indication (Def);
1334
1335 Full_Desig : Entity_Id;
1336
1337 begin
1338 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1339
1340 -- Check for permissible use of incomplete type
1341
1342 if Nkind (S) /= N_Subtype_Indication then
1343 Analyze (S);
1344
1345 if Present (Entity (S))
1346 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1347 then
1348 Set_Directly_Designated_Type (T, Entity (S));
1349
1350 -- If the designated type is a limited view, we cannot tell if
1351 -- the full view contains tasks, and there is no way to handle
1352 -- that full view in a client. We create a master entity for the
1353 -- scope, which will be used when a client determines that one
1354 -- is needed.
1355
1356 if From_Limited_With (Entity (S))
1357 and then not Is_Class_Wide_Type (Entity (S))
1358 then
1359 Set_Ekind (T, E_Access_Type);
1360 Build_Master_Entity (T);
1361 Build_Master_Renaming (T);
1362 end if;
1363
1364 else
1365 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1366 end if;
1367
1368 -- If the access definition is of the form: ACCESS NOT NULL ..
1369 -- the subtype indication must be of an access type. Create
1370 -- a null-excluding subtype of it.
1371
1372 if Null_Excluding_Subtype (Def) then
1373 if not Is_Access_Type (Entity (S)) then
1374 Error_Msg_N ("null exclusion must apply to access type", Def);
1375
1376 else
1377 declare
1378 Loc : constant Source_Ptr := Sloc (S);
1379 Decl : Node_Id;
1380 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1381
1382 begin
1383 Decl :=
1384 Make_Subtype_Declaration (Loc,
1385 Defining_Identifier => Nam,
1386 Subtype_Indication =>
1387 New_Occurrence_Of (Entity (S), Loc));
1388 Set_Null_Exclusion_Present (Decl);
1389 Insert_Before (Parent (Def), Decl);
1390 Analyze (Decl);
1391 Set_Entity (S, Nam);
1392 end;
1393 end if;
1394 end if;
1395
1396 else
1397 Set_Directly_Designated_Type (T,
1398 Process_Subtype (S, P, T, 'P'));
1399 end if;
1400
1401 if All_Present (Def) or Constant_Present (Def) then
1402 Set_Ekind (T, E_General_Access_Type);
1403 else
1404 Set_Ekind (T, E_Access_Type);
1405 end if;
1406
1407 Full_Desig := Designated_Type (T);
1408
1409 if Base_Type (Full_Desig) = T then
1410 Error_Msg_N ("access type cannot designate itself", S);
1411
1412 -- In Ada 2005, the type may have a limited view through some unit in
1413 -- its own context, allowing the following circularity that cannot be
1414 -- detected earlier.
1415
1416 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1417 then
1418 Error_Msg_N
1419 ("access type cannot designate its own class-wide type", S);
1420
1421 -- Clean up indication of tagged status to prevent cascaded errors
1422
1423 Set_Is_Tagged_Type (T, False);
1424 end if;
1425
1426 Set_Etype (T, T);
1427
1428 -- If the type has appeared already in a with_type clause, it is frozen
1429 -- and the pointer size is already set. Else, initialize.
1430
1431 if not From_Limited_With (T) then
1432 Init_Size_Align (T);
1433 end if;
1434
1435 -- Note that Has_Task is always false, since the access type itself
1436 -- is not a task type. See Einfo for more description on this point.
1437 -- Exactly the same consideration applies to Has_Controlled_Component
1438 -- and to Has_Protected.
1439
1440 Set_Has_Task (T, False);
1441 Set_Has_Protected (T, False);
1442 Set_Has_Timing_Event (T, False);
1443 Set_Has_Controlled_Component (T, False);
1444
1445 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1446 -- problems where an incomplete view of this entity has been previously
1447 -- established by a limited with and an overlaid version of this field
1448 -- (Stored_Constraint) was initialized for the incomplete view.
1449
1450 -- This reset is performed in most cases except where the access type
1451 -- has been created for the purposes of allocating or deallocating a
1452 -- build-in-place object. Such access types have explicitly set pools
1453 -- and finalization masters.
1454
1455 if No (Associated_Storage_Pool (T)) then
1456 Set_Finalization_Master (T, Empty);
1457 end if;
1458
1459 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1460 -- attributes
1461
1462 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1463 Set_Is_Access_Constant (T, Constant_Present (Def));
1464 end Access_Type_Declaration;
1465
1466 ----------------------------------
1467 -- Add_Interface_Tag_Components --
1468 ----------------------------------
1469
1470 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1471 Loc : constant Source_Ptr := Sloc (N);
1472 L : List_Id;
1473 Last_Tag : Node_Id;
1474
1475 procedure Add_Tag (Iface : Entity_Id);
1476 -- Add tag for one of the progenitor interfaces
1477
1478 -------------
1479 -- Add_Tag --
1480 -------------
1481
1482 procedure Add_Tag (Iface : Entity_Id) is
1483 Decl : Node_Id;
1484 Def : Node_Id;
1485 Tag : Entity_Id;
1486 Offset : Entity_Id;
1487
1488 begin
1489 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1490
1491 -- This is a reasonable place to propagate predicates
1492
1493 if Has_Predicates (Iface) then
1494 Set_Has_Predicates (Typ);
1495 end if;
1496
1497 Def :=
1498 Make_Component_Definition (Loc,
1499 Aliased_Present => True,
1500 Subtype_Indication =>
1501 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1502
1503 Tag := Make_Temporary (Loc, 'V');
1504
1505 Decl :=
1506 Make_Component_Declaration (Loc,
1507 Defining_Identifier => Tag,
1508 Component_Definition => Def);
1509
1510 Analyze_Component_Declaration (Decl);
1511
1512 Set_Analyzed (Decl);
1513 Set_Ekind (Tag, E_Component);
1514 Set_Is_Tag (Tag);
1515 Set_Is_Aliased (Tag);
1516 Set_Related_Type (Tag, Iface);
1517 Init_Component_Location (Tag);
1518
1519 pragma Assert (Is_Frozen (Iface));
1520
1521 Set_DT_Entry_Count (Tag,
1522 DT_Entry_Count (First_Entity (Iface)));
1523
1524 if No (Last_Tag) then
1525 Prepend (Decl, L);
1526 else
1527 Insert_After (Last_Tag, Decl);
1528 end if;
1529
1530 Last_Tag := Decl;
1531
1532 -- If the ancestor has discriminants we need to give special support
1533 -- to store the offset_to_top value of the secondary dispatch tables.
1534 -- For this purpose we add a supplementary component just after the
1535 -- field that contains the tag associated with each secondary DT.
1536
1537 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1538 Def :=
1539 Make_Component_Definition (Loc,
1540 Subtype_Indication =>
1541 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1542
1543 Offset := Make_Temporary (Loc, 'V');
1544
1545 Decl :=
1546 Make_Component_Declaration (Loc,
1547 Defining_Identifier => Offset,
1548 Component_Definition => Def);
1549
1550 Analyze_Component_Declaration (Decl);
1551
1552 Set_Analyzed (Decl);
1553 Set_Ekind (Offset, E_Component);
1554 Set_Is_Aliased (Offset);
1555 Set_Related_Type (Offset, Iface);
1556 Init_Component_Location (Offset);
1557 Insert_After (Last_Tag, Decl);
1558 Last_Tag := Decl;
1559 end if;
1560 end Add_Tag;
1561
1562 -- Local variables
1563
1564 Elmt : Elmt_Id;
1565 Ext : Node_Id;
1566 Comp : Node_Id;
1567
1568 -- Start of processing for Add_Interface_Tag_Components
1569
1570 begin
1571 if not RTE_Available (RE_Interface_Tag) then
1572 Error_Msg
1573 ("(Ada 2005) interface types not supported by this run-time!",
1574 Sloc (N));
1575 return;
1576 end if;
1577
1578 if Ekind (Typ) /= E_Record_Type
1579 or else (Is_Concurrent_Record_Type (Typ)
1580 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1581 or else (not Is_Concurrent_Record_Type (Typ)
1582 and then No (Interfaces (Typ))
1583 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1584 then
1585 return;
1586 end if;
1587
1588 -- Find the current last tag
1589
1590 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1591 Ext := Record_Extension_Part (Type_Definition (N));
1592 else
1593 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1594 Ext := Type_Definition (N);
1595 end if;
1596
1597 Last_Tag := Empty;
1598
1599 if not (Present (Component_List (Ext))) then
1600 Set_Null_Present (Ext, False);
1601 L := New_List;
1602 Set_Component_List (Ext,
1603 Make_Component_List (Loc,
1604 Component_Items => L,
1605 Null_Present => False));
1606 else
1607 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1608 L := Component_Items
1609 (Component_List
1610 (Record_Extension_Part
1611 (Type_Definition (N))));
1612 else
1613 L := Component_Items
1614 (Component_List
1615 (Type_Definition (N)));
1616 end if;
1617
1618 -- Find the last tag component
1619
1620 Comp := First (L);
1621 while Present (Comp) loop
1622 if Nkind (Comp) = N_Component_Declaration
1623 and then Is_Tag (Defining_Identifier (Comp))
1624 then
1625 Last_Tag := Comp;
1626 end if;
1627
1628 Next (Comp);
1629 end loop;
1630 end if;
1631
1632 -- At this point L references the list of components and Last_Tag
1633 -- references the current last tag (if any). Now we add the tag
1634 -- corresponding with all the interfaces that are not implemented
1635 -- by the parent.
1636
1637 if Present (Interfaces (Typ)) then
1638 Elmt := First_Elmt (Interfaces (Typ));
1639 while Present (Elmt) loop
1640 Add_Tag (Node (Elmt));
1641 Next_Elmt (Elmt);
1642 end loop;
1643 end if;
1644 end Add_Interface_Tag_Components;
1645
1646 -------------------------------------
1647 -- Add_Internal_Interface_Entities --
1648 -------------------------------------
1649
1650 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1651 Elmt : Elmt_Id;
1652 Iface : Entity_Id;
1653 Iface_Elmt : Elmt_Id;
1654 Iface_Prim : Entity_Id;
1655 Ifaces_List : Elist_Id;
1656 New_Subp : Entity_Id := Empty;
1657 Prim : Entity_Id;
1658 Restore_Scope : Boolean := False;
1659
1660 begin
1661 pragma Assert (Ada_Version >= Ada_2005
1662 and then Is_Record_Type (Tagged_Type)
1663 and then Is_Tagged_Type (Tagged_Type)
1664 and then Has_Interfaces (Tagged_Type)
1665 and then not Is_Interface (Tagged_Type));
1666
1667 -- Ensure that the internal entities are added to the scope of the type
1668
1669 if Scope (Tagged_Type) /= Current_Scope then
1670 Push_Scope (Scope (Tagged_Type));
1671 Restore_Scope := True;
1672 end if;
1673
1674 Collect_Interfaces (Tagged_Type, Ifaces_List);
1675
1676 Iface_Elmt := First_Elmt (Ifaces_List);
1677 while Present (Iface_Elmt) loop
1678 Iface := Node (Iface_Elmt);
1679
1680 -- Originally we excluded here from this processing interfaces that
1681 -- are parents of Tagged_Type because their primitives are located
1682 -- in the primary dispatch table (and hence no auxiliary internal
1683 -- entities are required to handle secondary dispatch tables in such
1684 -- case). However, these auxiliary entities are also required to
1685 -- handle derivations of interfaces in formals of generics (see
1686 -- Derive_Subprograms).
1687
1688 Elmt := First_Elmt (Primitive_Operations (Iface));
1689 while Present (Elmt) loop
1690 Iface_Prim := Node (Elmt);
1691
1692 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1693 Prim :=
1694 Find_Primitive_Covering_Interface
1695 (Tagged_Type => Tagged_Type,
1696 Iface_Prim => Iface_Prim);
1697
1698 if No (Prim) and then Serious_Errors_Detected > 0 then
1699 goto Continue;
1700 end if;
1701
1702 pragma Assert (Present (Prim));
1703
1704 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1705 -- differs from the name of the interface primitive then it is
1706 -- a private primitive inherited from a parent type. In such
1707 -- case, given that Tagged_Type covers the interface, the
1708 -- inherited private primitive becomes visible. For such
1709 -- purpose we add a new entity that renames the inherited
1710 -- private primitive.
1711
1712 if Chars (Prim) /= Chars (Iface_Prim) then
1713 pragma Assert (Has_Suffix (Prim, 'P'));
1714 Derive_Subprogram
1715 (New_Subp => New_Subp,
1716 Parent_Subp => Iface_Prim,
1717 Derived_Type => Tagged_Type,
1718 Parent_Type => Iface);
1719 Set_Alias (New_Subp, Prim);
1720 Set_Is_Abstract_Subprogram
1721 (New_Subp, Is_Abstract_Subprogram (Prim));
1722 end if;
1723
1724 Derive_Subprogram
1725 (New_Subp => New_Subp,
1726 Parent_Subp => Iface_Prim,
1727 Derived_Type => Tagged_Type,
1728 Parent_Type => Iface);
1729
1730 declare
1731 Anc : Entity_Id;
1732 begin
1733 if Is_Inherited_Operation (Prim)
1734 and then Present (Alias (Prim))
1735 then
1736 Anc := Alias (Prim);
1737 else
1738 Anc := Overridden_Operation (Prim);
1739 end if;
1740
1741 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1742 -- nonconforming preconditions in both an ancestor and
1743 -- a progenitor operation.
1744
1745 -- If the operation is a primitive wrapper it is an explicit
1746 -- (overriding) operqtion and all is fine.
1747
1748 if Present (Anc)
1749 and then Has_Non_Trivial_Precondition (Anc)
1750 and then Has_Non_Trivial_Precondition (Iface_Prim)
1751 then
1752 if Is_Abstract_Subprogram (Prim)
1753 or else
1754 (Ekind (Prim) = E_Procedure
1755 and then Nkind (Parent (Prim)) =
1756 N_Procedure_Specification
1757 and then Null_Present (Parent (Prim)))
1758 or else Is_Primitive_Wrapper (Prim)
1759 then
1760 null;
1761
1762 -- The operation is inherited and must be overridden
1763
1764 elsif not Comes_From_Source (Prim) then
1765 Error_Msg_NE
1766 ("&inherits non-conforming preconditions and must "
1767 & "be overridden (RM 6.1.1 (10-16)",
1768 Parent (Tagged_Type), Prim);
1769 end if;
1770 end if;
1771 end;
1772
1773 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1774 -- associated with interface types. These entities are
1775 -- only registered in the list of primitives of its
1776 -- corresponding tagged type because they are only used
1777 -- to fill the contents of the secondary dispatch tables.
1778 -- Therefore they are removed from the homonym chains.
1779
1780 Set_Is_Hidden (New_Subp);
1781 Set_Is_Internal (New_Subp);
1782 Set_Alias (New_Subp, Prim);
1783 Set_Is_Abstract_Subprogram
1784 (New_Subp, Is_Abstract_Subprogram (Prim));
1785 Set_Interface_Alias (New_Subp, Iface_Prim);
1786
1787 -- If the returned type is an interface then propagate it to
1788 -- the returned type. Needed by the thunk to generate the code
1789 -- which displaces "this" to reference the corresponding
1790 -- secondary dispatch table in the returned object.
1791
1792 if Is_Interface (Etype (Iface_Prim)) then
1793 Set_Etype (New_Subp, Etype (Iface_Prim));
1794 end if;
1795
1796 -- Internal entities associated with interface types are only
1797 -- registered in the list of primitives of the tagged type.
1798 -- They are only used to fill the contents of the secondary
1799 -- dispatch tables. Therefore they are not needed in the
1800 -- homonym chains.
1801
1802 Remove_Homonym (New_Subp);
1803
1804 -- Hidden entities associated with interfaces must have set
1805 -- the Has_Delay_Freeze attribute to ensure that, in case
1806 -- of locally defined tagged types (or compiling with static
1807 -- dispatch tables generation disabled) the corresponding
1808 -- entry of the secondary dispatch table is filled when such
1809 -- an entity is frozen. This is an expansion activity that must
1810 -- be suppressed for ASIS because it leads to gigi elaboration
1811 -- issues in annotate mode.
1812
1813 if not ASIS_Mode then
1814 Set_Has_Delayed_Freeze (New_Subp);
1815 end if;
1816 end if;
1817
1818 <<Continue>>
1819 Next_Elmt (Elmt);
1820 end loop;
1821
1822 Next_Elmt (Iface_Elmt);
1823 end loop;
1824
1825 if Restore_Scope then
1826 Pop_Scope;
1827 end if;
1828 end Add_Internal_Interface_Entities;
1829
1830 -----------------------------------
1831 -- Analyze_Component_Declaration --
1832 -----------------------------------
1833
1834 procedure Analyze_Component_Declaration (N : Node_Id) is
1835 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1836 Id : constant Entity_Id := Defining_Identifier (N);
1837 E : constant Node_Id := Expression (N);
1838 Typ : constant Node_Id :=
1839 Subtype_Indication (Component_Definition (N));
1840 T : Entity_Id;
1841 P : Entity_Id;
1842
1843 function Contains_POC (Constr : Node_Id) return Boolean;
1844 -- Determines whether a constraint uses the discriminant of a record
1845 -- type thus becoming a per-object constraint (POC).
1846
1847 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1848 -- Typ is the type of the current component, check whether this type is
1849 -- a limited type. Used to validate declaration against that of
1850 -- enclosing record.
1851
1852 ------------------
1853 -- Contains_POC --
1854 ------------------
1855
1856 function Contains_POC (Constr : Node_Id) return Boolean is
1857 begin
1858 -- Prevent cascaded errors
1859
1860 if Error_Posted (Constr) then
1861 return False;
1862 end if;
1863
1864 case Nkind (Constr) is
1865 when N_Attribute_Reference =>
1866 return Attribute_Name (Constr) = Name_Access
1867 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1868
1869 when N_Discriminant_Association =>
1870 return Denotes_Discriminant (Expression (Constr));
1871
1872 when N_Identifier =>
1873 return Denotes_Discriminant (Constr);
1874
1875 when N_Index_Or_Discriminant_Constraint =>
1876 declare
1877 IDC : Node_Id;
1878
1879 begin
1880 IDC := First (Constraints (Constr));
1881 while Present (IDC) loop
1882
1883 -- One per-object constraint is sufficient
1884
1885 if Contains_POC (IDC) then
1886 return True;
1887 end if;
1888
1889 Next (IDC);
1890 end loop;
1891
1892 return False;
1893 end;
1894
1895 when N_Range =>
1896 return Denotes_Discriminant (Low_Bound (Constr))
1897 or else
1898 Denotes_Discriminant (High_Bound (Constr));
1899
1900 when N_Range_Constraint =>
1901 return Denotes_Discriminant (Range_Expression (Constr));
1902
1903 when others =>
1904 return False;
1905 end case;
1906 end Contains_POC;
1907
1908 ----------------------
1909 -- Is_Known_Limited --
1910 ----------------------
1911
1912 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1913 P : constant Entity_Id := Etype (Typ);
1914 R : constant Entity_Id := Root_Type (Typ);
1915
1916 begin
1917 if Is_Limited_Record (Typ) then
1918 return True;
1919
1920 -- If the root type is limited (and not a limited interface)
1921 -- so is the current type
1922
1923 elsif Is_Limited_Record (R)
1924 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1925 then
1926 return True;
1927
1928 -- Else the type may have a limited interface progenitor, but a
1929 -- limited record parent.
1930
1931 elsif R /= P and then Is_Limited_Record (P) then
1932 return True;
1933
1934 else
1935 return False;
1936 end if;
1937 end Is_Known_Limited;
1938
1939 -- Start of processing for Analyze_Component_Declaration
1940
1941 begin
1942 Generate_Definition (Id);
1943 Enter_Name (Id);
1944
1945 if Present (Typ) then
1946 T := Find_Type_Of_Object
1947 (Subtype_Indication (Component_Definition (N)), N);
1948
1949 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1950 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1951 end if;
1952
1953 -- Ada 2005 (AI-230): Access Definition case
1954
1955 else
1956 pragma Assert (Present
1957 (Access_Definition (Component_Definition (N))));
1958
1959 T := Access_Definition
1960 (Related_Nod => N,
1961 N => Access_Definition (Component_Definition (N)));
1962 Set_Is_Local_Anonymous_Access (T);
1963
1964 -- Ada 2005 (AI-254)
1965
1966 if Present (Access_To_Subprogram_Definition
1967 (Access_Definition (Component_Definition (N))))
1968 and then Protected_Present (Access_To_Subprogram_Definition
1969 (Access_Definition
1970 (Component_Definition (N))))
1971 then
1972 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1973 end if;
1974 end if;
1975
1976 -- If the subtype is a constrained subtype of the enclosing record,
1977 -- (which must have a partial view) the back-end does not properly
1978 -- handle the recursion. Rewrite the component declaration with an
1979 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1980 -- the tree directly because side effects have already been removed from
1981 -- discriminant constraints.
1982
1983 if Ekind (T) = E_Access_Subtype
1984 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1985 and then Comes_From_Source (T)
1986 and then Nkind (Parent (T)) = N_Subtype_Declaration
1987 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1988 then
1989 Rewrite
1990 (Subtype_Indication (Component_Definition (N)),
1991 New_Copy_Tree (Subtype_Indication (Parent (T))));
1992 T := Find_Type_Of_Object
1993 (Subtype_Indication (Component_Definition (N)), N);
1994 end if;
1995
1996 -- If the component declaration includes a default expression, then we
1997 -- check that the component is not of a limited type (RM 3.7(5)),
1998 -- and do the special preanalysis of the expression (see section on
1999 -- "Handling of Default and Per-Object Expressions" in the spec of
2000 -- package Sem).
2001
2002 if Present (E) then
2003 Check_SPARK_05_Restriction ("default expression is not allowed", E);
2004 Preanalyze_Default_Expression (E, T);
2005 Check_Initialization (T, E);
2006
2007 if Ada_Version >= Ada_2005
2008 and then Ekind (T) = E_Anonymous_Access_Type
2009 and then Etype (E) /= Any_Type
2010 then
2011 -- Check RM 3.9.2(9): "if the expected type for an expression is
2012 -- an anonymous access-to-specific tagged type, then the object
2013 -- designated by the expression shall not be dynamically tagged
2014 -- unless it is a controlling operand in a call on a dispatching
2015 -- operation"
2016
2017 if Is_Tagged_Type (Directly_Designated_Type (T))
2018 and then
2019 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2020 and then
2021 Ekind (Directly_Designated_Type (Etype (E))) =
2022 E_Class_Wide_Type
2023 then
2024 Error_Msg_N
2025 ("access to specific tagged type required (RM 3.9.2(9))", E);
2026 end if;
2027
2028 -- (Ada 2005: AI-230): Accessibility check for anonymous
2029 -- components
2030
2031 if Type_Access_Level (Etype (E)) >
2032 Deepest_Type_Access_Level (T)
2033 then
2034 Error_Msg_N
2035 ("expression has deeper access level than component " &
2036 "(RM 3.10.2 (12.2))", E);
2037 end if;
2038
2039 -- The initialization expression is a reference to an access
2040 -- discriminant. The type of the discriminant is always deeper
2041 -- than any access type.
2042
2043 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2044 and then Is_Entity_Name (E)
2045 and then Ekind (Entity (E)) = E_In_Parameter
2046 and then Present (Discriminal_Link (Entity (E)))
2047 then
2048 Error_Msg_N
2049 ("discriminant has deeper accessibility level than target",
2050 E);
2051 end if;
2052 end if;
2053 end if;
2054
2055 -- The parent type may be a private view with unknown discriminants,
2056 -- and thus unconstrained. Regular components must be constrained.
2057
2058 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2059 if Is_Class_Wide_Type (T) then
2060 Error_Msg_N
2061 ("class-wide subtype with unknown discriminants" &
2062 " in component declaration",
2063 Subtype_Indication (Component_Definition (N)));
2064 else
2065 Error_Msg_N
2066 ("unconstrained subtype in component declaration",
2067 Subtype_Indication (Component_Definition (N)));
2068 end if;
2069
2070 -- Components cannot be abstract, except for the special case of
2071 -- the _Parent field (case of extending an abstract tagged type)
2072
2073 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2074 Error_Msg_N ("type of a component cannot be abstract", N);
2075 end if;
2076
2077 Set_Etype (Id, T);
2078 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2079
2080 -- The component declaration may have a per-object constraint, set
2081 -- the appropriate flag in the defining identifier of the subtype.
2082
2083 if Present (Subtype_Indication (Component_Definition (N))) then
2084 declare
2085 Sindic : constant Node_Id :=
2086 Subtype_Indication (Component_Definition (N));
2087 begin
2088 if Nkind (Sindic) = N_Subtype_Indication
2089 and then Present (Constraint (Sindic))
2090 and then Contains_POC (Constraint (Sindic))
2091 then
2092 Set_Has_Per_Object_Constraint (Id);
2093 end if;
2094 end;
2095 end if;
2096
2097 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2098 -- out some static checks.
2099
2100 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2101 Null_Exclusion_Static_Checks (N);
2102 end if;
2103
2104 -- If this component is private (or depends on a private type), flag the
2105 -- record type to indicate that some operations are not available.
2106
2107 P := Private_Component (T);
2108
2109 if Present (P) then
2110
2111 -- Check for circular definitions
2112
2113 if P = Any_Type then
2114 Set_Etype (Id, Any_Type);
2115
2116 -- There is a gap in the visibility of operations only if the
2117 -- component type is not defined in the scope of the record type.
2118
2119 elsif Scope (P) = Scope (Current_Scope) then
2120 null;
2121
2122 elsif Is_Limited_Type (P) then
2123 Set_Is_Limited_Composite (Current_Scope);
2124
2125 else
2126 Set_Is_Private_Composite (Current_Scope);
2127 end if;
2128 end if;
2129
2130 if P /= Any_Type
2131 and then Is_Limited_Type (T)
2132 and then Chars (Id) /= Name_uParent
2133 and then Is_Tagged_Type (Current_Scope)
2134 then
2135 if Is_Derived_Type (Current_Scope)
2136 and then not Is_Known_Limited (Current_Scope)
2137 then
2138 Error_Msg_N
2139 ("extension of nonlimited type cannot have limited components",
2140 N);
2141
2142 if Is_Interface (Root_Type (Current_Scope)) then
2143 Error_Msg_N
2144 ("\limitedness is not inherited from limited interface", N);
2145 Error_Msg_N ("\add LIMITED to type indication", N);
2146 end if;
2147
2148 Explain_Limited_Type (T, N);
2149 Set_Etype (Id, Any_Type);
2150 Set_Is_Limited_Composite (Current_Scope, False);
2151
2152 elsif not Is_Derived_Type (Current_Scope)
2153 and then not Is_Limited_Record (Current_Scope)
2154 and then not Is_Concurrent_Type (Current_Scope)
2155 then
2156 Error_Msg_N
2157 ("nonlimited tagged type cannot have limited components", N);
2158 Explain_Limited_Type (T, N);
2159 Set_Etype (Id, Any_Type);
2160 Set_Is_Limited_Composite (Current_Scope, False);
2161 end if;
2162 end if;
2163
2164 -- If the component is an unconstrained task or protected type with
2165 -- discriminants, the component and the enclosing record are limited
2166 -- and the component is constrained by its default values. Compute
2167 -- its actual subtype, else it may be allocated the maximum size by
2168 -- the backend, and possibly overflow.
2169
2170 if Is_Concurrent_Type (T)
2171 and then not Is_Constrained (T)
2172 and then Has_Discriminants (T)
2173 and then not Has_Discriminants (Current_Scope)
2174 then
2175 declare
2176 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2177
2178 begin
2179 Set_Etype (Id, Act_T);
2180
2181 -- Rewrite component definition to use the constrained subtype
2182
2183 Rewrite (Component_Definition (N),
2184 Make_Component_Definition (Loc,
2185 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2186 end;
2187 end if;
2188
2189 Set_Original_Record_Component (Id, Id);
2190
2191 if Has_Aspects (N) then
2192 Analyze_Aspect_Specifications (N, Id);
2193 end if;
2194
2195 Analyze_Dimension (N);
2196 end Analyze_Component_Declaration;
2197
2198 --------------------------
2199 -- Analyze_Declarations --
2200 --------------------------
2201
2202 procedure Analyze_Declarations (L : List_Id) is
2203 Decl : Node_Id;
2204
2205 procedure Adjust_Decl;
2206 -- Adjust Decl not to include implicit label declarations, since these
2207 -- have strange Sloc values that result in elaboration check problems.
2208 -- (They have the sloc of the label as found in the source, and that
2209 -- is ahead of the current declarative part).
2210
2211 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2212 -- Create the subprogram bodies which verify the run-time semantics of
2213 -- the pragmas listed below for each elibigle type found in declarative
2214 -- list Decls. The pragmas are:
2215 --
2216 -- Default_Initial_Condition
2217 -- Invariant
2218 -- Type_Invariant
2219 --
2220 -- Context denotes the owner of the declarative list.
2221
2222 procedure Check_Entry_Contracts;
2223 -- Perform a pre-analysis of the pre- and postconditions of an entry
2224 -- declaration. This must be done before full resolution and creation
2225 -- of the parameter block, etc. to catch illegal uses within the
2226 -- contract expression. Full analysis of the expression is done when
2227 -- the contract is processed.
2228
2229 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2230 -- Check if a nested package has entities within it that rely on library
2231 -- level private types where the full view has not been completed for
2232 -- the purposes of checking if it is acceptable to freeze an expression
2233 -- function at the point of declaration.
2234
2235 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2236 -- Determine whether Body_Decl denotes the body of a late controlled
2237 -- primitive (either Initialize, Adjust or Finalize). If this is the
2238 -- case, add a proper spec if the body lacks one. The spec is inserted
2239 -- before Body_Decl and immediately analyzed.
2240
2241 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2242 -- Spec_Id is the entity of a package that may define abstract states,
2243 -- and in the case of a child unit, whose ancestors may define abstract
2244 -- states. If the states have partial visible refinement, remove the
2245 -- partial visibility of each constituent at the end of the package
2246 -- spec and body declarations.
2247
2248 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2249 -- Spec_Id is the entity of a package that may define abstract states.
2250 -- If the states have visible refinement, remove the visibility of each
2251 -- constituent at the end of the package body declaration.
2252
2253 procedure Resolve_Aspects;
2254 -- Utility to resolve the expressions of aspects at the end of a list of
2255 -- declarations, or before a declaration that freezes previous entities,
2256 -- such as in a subprogram body.
2257
2258 -----------------
2259 -- Adjust_Decl --
2260 -----------------
2261
2262 procedure Adjust_Decl is
2263 begin
2264 while Present (Prev (Decl))
2265 and then Nkind (Decl) = N_Implicit_Label_Declaration
2266 loop
2267 Prev (Decl);
2268 end loop;
2269 end Adjust_Decl;
2270
2271 ----------------------------
2272 -- Build_Assertion_Bodies --
2273 ----------------------------
2274
2275 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2276 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2277 -- Create the subprogram bodies which verify the run-time semantics
2278 -- of the pragmas listed below for type Typ. The pragmas are:
2279 --
2280 -- Default_Initial_Condition
2281 -- Invariant
2282 -- Type_Invariant
2283
2284 -------------------------------------
2285 -- Build_Assertion_Bodies_For_Type --
2286 -------------------------------------
2287
2288 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2289 begin
2290 -- Preanalyze and resolve the Default_Initial_Condition assertion
2291 -- expression at the end of the declarations to catch any errors.
2292
2293 if Has_DIC (Typ) then
2294 Build_DIC_Procedure_Body (Typ);
2295 end if;
2296
2297 if Nkind (Context) = N_Package_Specification then
2298
2299 -- Preanalyze and resolve the class-wide invariants of an
2300 -- interface at the end of whichever declarative part has the
2301 -- interface type. Note that an interface may be declared in
2302 -- any non-package declarative part, but reaching the end of
2303 -- such a declarative part will always freeze the type and
2304 -- generate the invariant procedure (see Freeze_Type).
2305
2306 if Is_Interface (Typ) then
2307
2308 -- Interfaces are treated as the partial view of a private
2309 -- type, in order to achieve uniformity with the general
2310 -- case. As a result, an interface receives only a "partial"
2311 -- invariant procedure, which is never called.
2312
2313 if Has_Own_Invariants (Typ) then
2314 Build_Invariant_Procedure_Body
2315 (Typ => Typ,
2316 Partial_Invariant => True);
2317 end if;
2318
2319 -- Preanalyze and resolve the invariants of a private type
2320 -- at the end of the visible declarations to catch potential
2321 -- errors. Inherited class-wide invariants are not included
2322 -- because they have already been resolved.
2323
2324 elsif Decls = Visible_Declarations (Context)
2325 and then Ekind_In (Typ, E_Limited_Private_Type,
2326 E_Private_Type,
2327 E_Record_Type_With_Private)
2328 and then Has_Own_Invariants (Typ)
2329 then
2330 Build_Invariant_Procedure_Body
2331 (Typ => Typ,
2332 Partial_Invariant => True);
2333
2334 -- Preanalyze and resolve the invariants of a private type's
2335 -- full view at the end of the private declarations to catch
2336 -- potential errors.
2337
2338 elsif Decls = Private_Declarations (Context)
2339 and then not Is_Private_Type (Typ)
2340 and then Has_Private_Declaration (Typ)
2341 and then Has_Invariants (Typ)
2342 then
2343 Build_Invariant_Procedure_Body (Typ);
2344 end if;
2345 end if;
2346 end Build_Assertion_Bodies_For_Type;
2347
2348 -- Local variables
2349
2350 Decl : Node_Id;
2351 Decl_Id : Entity_Id;
2352
2353 -- Start of processing for Build_Assertion_Bodies
2354
2355 begin
2356 Decl := First (Decls);
2357 while Present (Decl) loop
2358 if Is_Declaration (Decl) then
2359 Decl_Id := Defining_Entity (Decl);
2360
2361 if Is_Type (Decl_Id) then
2362 Build_Assertion_Bodies_For_Type (Decl_Id);
2363 end if;
2364 end if;
2365
2366 Next (Decl);
2367 end loop;
2368 end Build_Assertion_Bodies;
2369
2370 ---------------------------
2371 -- Check_Entry_Contracts --
2372 ---------------------------
2373
2374 procedure Check_Entry_Contracts is
2375 ASN : Node_Id;
2376 Ent : Entity_Id;
2377 Exp : Node_Id;
2378
2379 begin
2380 Ent := First_Entity (Current_Scope);
2381 while Present (Ent) loop
2382
2383 -- This only concerns entries with pre/postconditions
2384
2385 if Ekind (Ent) = E_Entry
2386 and then Present (Contract (Ent))
2387 and then Present (Pre_Post_Conditions (Contract (Ent)))
2388 then
2389 ASN := Pre_Post_Conditions (Contract (Ent));
2390 Push_Scope (Ent);
2391 Install_Formals (Ent);
2392
2393 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2394 -- is performed on a copy of the pragma expression, to prevent
2395 -- modifying the original expression.
2396
2397 while Present (ASN) loop
2398 if Nkind (ASN) = N_Pragma then
2399 Exp :=
2400 New_Copy_Tree
2401 (Expression
2402 (First (Pragma_Argument_Associations (ASN))));
2403 Set_Parent (Exp, ASN);
2404
2405 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2406 end if;
2407
2408 ASN := Next_Pragma (ASN);
2409 end loop;
2410
2411 End_Scope;
2412 end if;
2413
2414 Next_Entity (Ent);
2415 end loop;
2416 end Check_Entry_Contracts;
2417
2418 ----------------------------------
2419 -- Contains_Lib_Incomplete_Type --
2420 ----------------------------------
2421
2422 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2423 Curr : Entity_Id;
2424
2425 begin
2426 -- Avoid looking through scopes that do not meet the precondition of
2427 -- Pkg not being within a library unit spec.
2428
2429 if not Is_Compilation_Unit (Pkg)
2430 and then not Is_Generic_Instance (Pkg)
2431 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2432 then
2433 -- Loop through all entities in the current scope to identify
2434 -- an entity that depends on a private type.
2435
2436 Curr := First_Entity (Pkg);
2437 loop
2438 if Nkind (Curr) in N_Entity
2439 and then Depends_On_Private (Curr)
2440 then
2441 return True;
2442 end if;
2443
2444 exit when Last_Entity (Current_Scope) = Curr;
2445 Curr := Next_Entity (Curr);
2446 end loop;
2447 end if;
2448
2449 return False;
2450 end Contains_Lib_Incomplete_Type;
2451
2452 --------------------------------------
2453 -- Handle_Late_Controlled_Primitive --
2454 --------------------------------------
2455
2456 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2457 Body_Spec : constant Node_Id := Specification (Body_Decl);
2458 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2459 Loc : constant Source_Ptr := Sloc (Body_Id);
2460 Params : constant List_Id :=
2461 Parameter_Specifications (Body_Spec);
2462 Spec : Node_Id;
2463 Spec_Id : Entity_Id;
2464 Typ : Node_Id;
2465
2466 begin
2467 -- Consider only procedure bodies whose name matches one of the three
2468 -- controlled primitives.
2469
2470 if Nkind (Body_Spec) /= N_Procedure_Specification
2471 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2472 Name_Finalize,
2473 Name_Initialize)
2474 then
2475 return;
2476
2477 -- A controlled primitive must have exactly one formal which is not
2478 -- an anonymous access type.
2479
2480 elsif List_Length (Params) /= 1 then
2481 return;
2482 end if;
2483
2484 Typ := Parameter_Type (First (Params));
2485
2486 if Nkind (Typ) = N_Access_Definition then
2487 return;
2488 end if;
2489
2490 Find_Type (Typ);
2491
2492 -- The type of the formal must be derived from [Limited_]Controlled
2493
2494 if not Is_Controlled (Entity (Typ)) then
2495 return;
2496 end if;
2497
2498 -- Check whether a specification exists for this body. We do not
2499 -- analyze the spec of the body in full, because it will be analyzed
2500 -- again when the body is properly analyzed, and we cannot create
2501 -- duplicate entries in the formals chain. We look for an explicit
2502 -- specification because the body may be an overriding operation and
2503 -- an inherited spec may be present.
2504
2505 Spec_Id := Current_Entity (Body_Id);
2506
2507 while Present (Spec_Id) loop
2508 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2509 and then Scope (Spec_Id) = Current_Scope
2510 and then Present (First_Formal (Spec_Id))
2511 and then No (Next_Formal (First_Formal (Spec_Id)))
2512 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2513 and then Comes_From_Source (Spec_Id)
2514 then
2515 return;
2516 end if;
2517
2518 Spec_Id := Homonym (Spec_Id);
2519 end loop;
2520
2521 -- At this point the body is known to be a late controlled primitive.
2522 -- Generate a matching spec and insert it before the body. Note the
2523 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2524 -- tree in this case.
2525
2526 Spec := Copy_Separate_Tree (Body_Spec);
2527
2528 -- Ensure that the subprogram declaration does not inherit the null
2529 -- indicator from the body as we now have a proper spec/body pair.
2530
2531 Set_Null_Present (Spec, False);
2532
2533 -- Ensure that the freeze node is inserted after the declaration of
2534 -- the primitive since its expansion will freeze the primitive.
2535
2536 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2537
2538 Insert_Before_And_Analyze (Body_Decl, Decl);
2539 end Handle_Late_Controlled_Primitive;
2540
2541 ----------------------------------------
2542 -- Remove_Partial_Visible_Refinements --
2543 ----------------------------------------
2544
2545 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2546 State_Elmt : Elmt_Id;
2547 begin
2548 if Present (Abstract_States (Spec_Id)) then
2549 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2550 while Present (State_Elmt) loop
2551 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2552 Next_Elmt (State_Elmt);
2553 end loop;
2554 end if;
2555
2556 -- For a child unit, also hide the partial state refinement from
2557 -- ancestor packages.
2558
2559 if Is_Child_Unit (Spec_Id) then
2560 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2561 end if;
2562 end Remove_Partial_Visible_Refinements;
2563
2564 --------------------------------
2565 -- Remove_Visible_Refinements --
2566 --------------------------------
2567
2568 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2569 State_Elmt : Elmt_Id;
2570 begin
2571 if Present (Abstract_States (Spec_Id)) then
2572 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2573 while Present (State_Elmt) loop
2574 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2575 Next_Elmt (State_Elmt);
2576 end loop;
2577 end if;
2578 end Remove_Visible_Refinements;
2579
2580 ---------------------
2581 -- Resolve_Aspects --
2582 ---------------------
2583
2584 procedure Resolve_Aspects is
2585 E : Entity_Id;
2586
2587 begin
2588 E := First_Entity (Current_Scope);
2589 while Present (E) loop
2590 Resolve_Aspect_Expressions (E);
2591 Next_Entity (E);
2592 end loop;
2593 end Resolve_Aspects;
2594
2595 -- Local variables
2596
2597 Context : Node_Id := Empty;
2598 Freeze_From : Entity_Id := Empty;
2599 Next_Decl : Node_Id;
2600
2601 Body_Seen : Boolean := False;
2602 -- Flag set when the first body [stub] is encountered
2603
2604 -- Start of processing for Analyze_Declarations
2605
2606 begin
2607 if Restriction_Check_Required (SPARK_05) then
2608 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2609 end if;
2610
2611 Decl := First (L);
2612 while Present (Decl) loop
2613
2614 -- Package spec cannot contain a package declaration in SPARK
2615
2616 if Nkind (Decl) = N_Package_Declaration
2617 and then Nkind (Parent (L)) = N_Package_Specification
2618 then
2619 Check_SPARK_05_Restriction
2620 ("package specification cannot contain a package declaration",
2621 Decl);
2622 end if;
2623
2624 -- Complete analysis of declaration
2625
2626 Analyze (Decl);
2627 Next_Decl := Next (Decl);
2628
2629 if No (Freeze_From) then
2630 Freeze_From := First_Entity (Current_Scope);
2631 end if;
2632
2633 -- At the end of a declarative part, freeze remaining entities
2634 -- declared in it. The end of the visible declarations of package
2635 -- specification is not the end of a declarative part if private
2636 -- declarations are present. The end of a package declaration is a
2637 -- freezing point only if it a library package. A task definition or
2638 -- protected type definition is not a freeze point either. Finally,
2639 -- we do not freeze entities in generic scopes, because there is no
2640 -- code generated for them and freeze nodes will be generated for
2641 -- the instance.
2642
2643 -- The end of a package instantiation is not a freeze point, but
2644 -- for now we make it one, because the generic body is inserted
2645 -- (currently) immediately after. Generic instantiations will not
2646 -- be a freeze point once delayed freezing of bodies is implemented.
2647 -- (This is needed in any case for early instantiations ???).
2648
2649 if No (Next_Decl) then
2650 if Nkind (Parent (L)) = N_Component_List then
2651 null;
2652
2653 elsif Nkind_In (Parent (L), N_Protected_Definition,
2654 N_Task_Definition)
2655 then
2656 Check_Entry_Contracts;
2657
2658 elsif Nkind (Parent (L)) /= N_Package_Specification then
2659 if Nkind (Parent (L)) = N_Package_Body then
2660 Freeze_From := First_Entity (Current_Scope);
2661 end if;
2662
2663 -- There may have been several freezing points previously,
2664 -- for example object declarations or subprogram bodies, but
2665 -- at the end of a declarative part we check freezing from
2666 -- the beginning, even though entities may already be frozen,
2667 -- in order to perform visibility checks on delayed aspects.
2668
2669 Adjust_Decl;
2670
2671 -- If the current scope is a generic subprogram body. Skip the
2672 -- generic formal parameters that are not frozen here.
2673
2674 if Is_Subprogram (Current_Scope)
2675 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2676 N_Generic_Subprogram_Declaration
2677 and then Present (First_Entity (Current_Scope))
2678 then
2679 while Is_Generic_Formal (Freeze_From) loop
2680 Freeze_From := Next_Entity (Freeze_From);
2681 end loop;
2682
2683 Freeze_All (Freeze_From, Decl);
2684 Freeze_From := Last_Entity (Current_Scope);
2685
2686 else
2687 -- For declarations in a subprogram body there is no issue
2688 -- with name resolution in aspect specifications, but in
2689 -- ASIS mode we need to preanalyze aspect specifications
2690 -- that may otherwise only be analyzed during expansion
2691 -- (e.g. during generation of a related subprogram).
2692
2693 if ASIS_Mode then
2694 Resolve_Aspects;
2695 end if;
2696
2697 Freeze_All (First_Entity (Current_Scope), Decl);
2698 Freeze_From := Last_Entity (Current_Scope);
2699 end if;
2700
2701 -- Current scope is a package specification
2702
2703 elsif Scope (Current_Scope) /= Standard_Standard
2704 and then not Is_Child_Unit (Current_Scope)
2705 and then No (Generic_Parent (Parent (L)))
2706 then
2707 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2708 -- resolved at the end of the immediately enclosing declaration
2709 -- list (AI05-0183-1).
2710
2711 Resolve_Aspects;
2712
2713 elsif L /= Visible_Declarations (Parent (L))
2714 or else No (Private_Declarations (Parent (L)))
2715 or else Is_Empty_List (Private_Declarations (Parent (L)))
2716 then
2717 Adjust_Decl;
2718
2719 -- End of a package declaration
2720
2721 -- In compilation mode the expansion of freeze node takes care
2722 -- of resolving expressions of all aspects in the list. In ASIS
2723 -- mode this must be done explicitly.
2724
2725 if ASIS_Mode
2726 and then Scope (Current_Scope) = Standard_Standard
2727 then
2728 Resolve_Aspects;
2729 end if;
2730
2731 -- This is a freeze point because it is the end of a
2732 -- compilation unit.
2733
2734 Freeze_All (First_Entity (Current_Scope), Decl);
2735 Freeze_From := Last_Entity (Current_Scope);
2736
2737 -- At the end of the visible declarations the expressions in
2738 -- aspects of all entities declared so far must be resolved.
2739 -- The entities themselves might be frozen later, and the
2740 -- generated pragmas and attribute definition clauses analyzed
2741 -- in full at that point, but name resolution must take place
2742 -- now.
2743 -- In addition to being the proper semantics, this is mandatory
2744 -- within generic units, because global name capture requires
2745 -- those expressions to be analyzed, given that the generated
2746 -- pragmas do not appear in the original generic tree.
2747
2748 elsif Serious_Errors_Detected = 0 then
2749 Resolve_Aspects;
2750 end if;
2751
2752 -- If next node is a body then freeze all types before the body.
2753 -- An exception occurs for some expander-generated bodies. If these
2754 -- are generated at places where in general language rules would not
2755 -- allow a freeze point, then we assume that the expander has
2756 -- explicitly checked that all required types are properly frozen,
2757 -- and we do not cause general freezing here. This special circuit
2758 -- is used when the encountered body is marked as having already
2759 -- been analyzed.
2760
2761 -- In all other cases (bodies that come from source, and expander
2762 -- generated bodies that have not been analyzed yet), freeze all
2763 -- types now. Note that in the latter case, the expander must take
2764 -- care to attach the bodies at a proper place in the tree so as to
2765 -- not cause unwanted freezing at that point.
2766
2767 -- It is also necessary to check for a case where both an expression
2768 -- function is used and the current scope depends on an incomplete
2769 -- private type from a library unit, otherwise premature freezing of
2770 -- the private type will occur.
2771
2772 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2773 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2774 or else not Was_Expression_Function (Next_Decl))
2775 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2776 and then not Contains_Lib_Incomplete_Type
2777 (Current_Scope)))
2778 then
2779 -- When a controlled type is frozen, the expander generates stream
2780 -- and controlled-type support routines. If the freeze is caused
2781 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2782 -- expander will end up using the wrong version of these routines,
2783 -- as the body has not been processed yet. To remedy this, detect
2784 -- a late controlled primitive and create a proper spec for it.
2785 -- This ensures that the primitive will override its inherited
2786 -- counterpart before the freeze takes place.
2787
2788 -- If the declaration we just processed is a body, do not attempt
2789 -- to examine Next_Decl as the late primitive idiom can only apply
2790 -- to the first encountered body.
2791
2792 -- The spec of the late primitive is not generated in ASIS mode to
2793 -- ensure a consistent list of primitives that indicates the true
2794 -- semantic structure of the program (which is not relevant when
2795 -- generating executable code).
2796
2797 -- ??? A cleaner approach may be possible and/or this solution
2798 -- could be extended to general-purpose late primitives, TBD.
2799
2800 if not ASIS_Mode
2801 and then not Body_Seen
2802 and then not Is_Body (Decl)
2803 then
2804 Body_Seen := True;
2805
2806 if Nkind (Next_Decl) = N_Subprogram_Body then
2807 Handle_Late_Controlled_Primitive (Next_Decl);
2808 end if;
2809
2810 else
2811 -- In ASIS mode, if the next declaration is a body, complete
2812 -- the analysis of declarations so far.
2813
2814 Resolve_Aspects;
2815 end if;
2816
2817 Adjust_Decl;
2818
2819 -- The generated body of an expression function does not freeze,
2820 -- unless it is a completion, in which case only the expression
2821 -- itself freezes. This is handled when the body itself is
2822 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2823
2824 Freeze_All (Freeze_From, Decl);
2825 Freeze_From := Last_Entity (Current_Scope);
2826 end if;
2827
2828 Decl := Next_Decl;
2829 end loop;
2830
2831 -- Post-freezing actions
2832
2833 if Present (L) then
2834 Context := Parent (L);
2835
2836 -- Certain contract annocations have forward visibility semantics and
2837 -- must be analyzed after all declarative items have been processed.
2838 -- This timing ensures that entities referenced by such contracts are
2839 -- visible.
2840
2841 -- Analyze the contract of an immediately enclosing package spec or
2842 -- body first because other contracts may depend on its information.
2843
2844 if Nkind (Context) = N_Package_Body then
2845 Analyze_Package_Body_Contract (Defining_Entity (Context));
2846
2847 elsif Nkind (Context) = N_Package_Specification then
2848 Analyze_Package_Contract (Defining_Entity (Context));
2849 end if;
2850
2851 -- Analyze the contracts of various constructs in the declarative
2852 -- list.
2853
2854 Analyze_Contracts (L);
2855
2856 if Nkind (Context) = N_Package_Body then
2857
2858 -- Ensure that all abstract states and objects declared in the
2859 -- state space of a package body are utilized as constituents.
2860
2861 Check_Unused_Body_States (Defining_Entity (Context));
2862
2863 -- State refinements are visible up to the end of the package body
2864 -- declarations. Hide the state refinements from visibility to
2865 -- restore the original state conditions.
2866
2867 Remove_Visible_Refinements (Corresponding_Spec (Context));
2868 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2869
2870 elsif Nkind (Context) = N_Package_Specification then
2871
2872 -- Partial state refinements are visible up to the end of the
2873 -- package spec declarations. Hide the partial state refinements
2874 -- from visibility to restore the original state conditions.
2875
2876 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2877 end if;
2878
2879 -- Verify that all abstract states found in any package declared in
2880 -- the input declarative list have proper refinements. The check is
2881 -- performed only when the context denotes a block, entry, package,
2882 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2883
2884 Check_State_Refinements (Context);
2885
2886 -- Create the subprogram bodies which verify the run-time semantics
2887 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2888 -- types within the current declarative list. This ensures that all
2889 -- assertion expressions are preanalyzed and resolved at the end of
2890 -- the declarative part. Note that the resolution happens even when
2891 -- freezing does not take place.
2892
2893 Build_Assertion_Bodies (L, Context);
2894 end if;
2895 end Analyze_Declarations;
2896
2897 -----------------------------------
2898 -- Analyze_Full_Type_Declaration --
2899 -----------------------------------
2900
2901 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2902 Def : constant Node_Id := Type_Definition (N);
2903 Def_Id : constant Entity_Id := Defining_Identifier (N);
2904 T : Entity_Id;
2905 Prev : Entity_Id;
2906
2907 Is_Remote : constant Boolean :=
2908 (Is_Remote_Types (Current_Scope)
2909 or else Is_Remote_Call_Interface (Current_Scope))
2910 and then not (In_Private_Part (Current_Scope)
2911 or else In_Package_Body (Current_Scope));
2912
2913 procedure Check_Nonoverridable_Aspects;
2914 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2915 -- be overridden, and can only be confirmed on derivation.
2916
2917 procedure Check_Ops_From_Incomplete_Type;
2918 -- If there is a tagged incomplete partial view of the type, traverse
2919 -- the primitives of the incomplete view and change the type of any
2920 -- controlling formals and result to indicate the full view. The
2921 -- primitives will be added to the full type's primitive operations
2922 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2923 -- is called from Process_Incomplete_Dependents).
2924
2925 ----------------------------------
2926 -- Check_Nonoverridable_Aspects --
2927 ----------------------------------
2928
2929 procedure Check_Nonoverridable_Aspects is
2930 function Get_Aspect_Spec
2931 (Specs : List_Id;
2932 Aspect_Name : Name_Id) return Node_Id;
2933 -- Check whether a list of aspect specifications includes an entry
2934 -- for a specific aspect. The list is either that of a partial or
2935 -- a full view.
2936
2937 ---------------------
2938 -- Get_Aspect_Spec --
2939 ---------------------
2940
2941 function Get_Aspect_Spec
2942 (Specs : List_Id;
2943 Aspect_Name : Name_Id) return Node_Id
2944 is
2945 Spec : Node_Id;
2946
2947 begin
2948 Spec := First (Specs);
2949 while Present (Spec) loop
2950 if Chars (Identifier (Spec)) = Aspect_Name then
2951 return Spec;
2952 end if;
2953 Next (Spec);
2954 end loop;
2955
2956 return Empty;
2957 end Get_Aspect_Spec;
2958
2959 -- Local variables
2960
2961 Prev_Aspects : constant List_Id :=
2962 Aspect_Specifications (Parent (Def_Id));
2963 Par_Type : Entity_Id;
2964 Prev_Aspect : Node_Id;
2965
2966 -- Start of processing for Check_Nonoverridable_Aspects
2967
2968 begin
2969 -- Get parent type of derived type. Note that Prev is the entity in
2970 -- the partial declaration, but its contents are now those of full
2971 -- view, while Def_Id reflects the partial view.
2972
2973 if Is_Private_Type (Def_Id) then
2974 Par_Type := Etype (Full_View (Def_Id));
2975 else
2976 Par_Type := Etype (Def_Id);
2977 end if;
2978
2979 -- If there is an inherited Implicit_Dereference, verify that it is
2980 -- made explicit in the partial view.
2981
2982 if Has_Discriminants (Base_Type (Par_Type))
2983 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2984 and then Present (Discriminant_Specifications (Parent (Prev)))
2985 and then Present (Get_Reference_Discriminant (Par_Type))
2986 then
2987 Prev_Aspect :=
2988 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2989
2990 if No (Prev_Aspect)
2991 and then Present
2992 (Discriminant_Specifications
2993 (Original_Node (Parent (Prev))))
2994 then
2995 Error_Msg_N
2996 ("type does not inherit implicit dereference", Prev);
2997
2998 else
2999 -- If one of the views has the aspect specified, verify that it
3000 -- is consistent with that of the parent.
3001
3002 declare
3003 Par_Discr : constant Entity_Id :=
3004 Get_Reference_Discriminant (Par_Type);
3005 Cur_Discr : constant Entity_Id :=
3006 Get_Reference_Discriminant (Prev);
3007
3008 begin
3009 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3010 Error_Msg_N ("aspect incosistent with that of parent", N);
3011 end if;
3012
3013 -- Check that specification in partial view matches the
3014 -- inherited aspect. Compare names directly because aspect
3015 -- expression may not be analyzed.
3016
3017 if Present (Prev_Aspect)
3018 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3019 and then Chars (Expression (Prev_Aspect)) /=
3020 Chars (Cur_Discr)
3021 then
3022 Error_Msg_N
3023 ("aspect incosistent with that of parent", N);
3024 end if;
3025 end;
3026 end if;
3027 end if;
3028
3029 -- TBD : other nonoverridable aspects.
3030 end Check_Nonoverridable_Aspects;
3031
3032 ------------------------------------
3033 -- Check_Ops_From_Incomplete_Type --
3034 ------------------------------------
3035
3036 procedure Check_Ops_From_Incomplete_Type is
3037 Elmt : Elmt_Id;
3038 Formal : Entity_Id;
3039 Op : Entity_Id;
3040
3041 begin
3042 if Prev /= T
3043 and then Ekind (Prev) = E_Incomplete_Type
3044 and then Is_Tagged_Type (Prev)
3045 and then Is_Tagged_Type (T)
3046 then
3047 Elmt := First_Elmt (Primitive_Operations (Prev));
3048 while Present (Elmt) loop
3049 Op := Node (Elmt);
3050
3051 Formal := First_Formal (Op);
3052 while Present (Formal) loop
3053 if Etype (Formal) = Prev then
3054 Set_Etype (Formal, T);
3055 end if;
3056
3057 Next_Formal (Formal);
3058 end loop;
3059
3060 if Etype (Op) = Prev then
3061 Set_Etype (Op, T);
3062 end if;
3063
3064 Next_Elmt (Elmt);
3065 end loop;
3066 end if;
3067 end Check_Ops_From_Incomplete_Type;
3068
3069 -- Start of processing for Analyze_Full_Type_Declaration
3070
3071 begin
3072 Prev := Find_Type_Name (N);
3073
3074 -- The full view, if present, now points to the current type. If there
3075 -- is an incomplete partial view, set a link to it, to simplify the
3076 -- retrieval of primitive operations of the type.
3077
3078 -- Ada 2005 (AI-50217): If the type was previously decorated when
3079 -- imported through a LIMITED WITH clause, it appears as incomplete
3080 -- but has no full view.
3081
3082 if Ekind (Prev) = E_Incomplete_Type
3083 and then Present (Full_View (Prev))
3084 then
3085 T := Full_View (Prev);
3086 Set_Incomplete_View (N, Parent (Prev));
3087 else
3088 T := Prev;
3089 end if;
3090
3091 Set_Is_Pure (T, Is_Pure (Current_Scope));
3092
3093 -- We set the flag Is_First_Subtype here. It is needed to set the
3094 -- corresponding flag for the Implicit class-wide-type created
3095 -- during tagged types processing.
3096
3097 Set_Is_First_Subtype (T, True);
3098
3099 -- Only composite types other than array types are allowed to have
3100 -- discriminants.
3101
3102 case Nkind (Def) is
3103
3104 -- For derived types, the rule will be checked once we've figured
3105 -- out the parent type.
3106
3107 when N_Derived_Type_Definition =>
3108 null;
3109
3110 -- For record types, discriminants are allowed, unless we are in
3111 -- SPARK.
3112
3113 when N_Record_Definition =>
3114 if Present (Discriminant_Specifications (N)) then
3115 Check_SPARK_05_Restriction
3116 ("discriminant type is not allowed",
3117 Defining_Identifier
3118 (First (Discriminant_Specifications (N))));
3119 end if;
3120
3121 when others =>
3122 if Present (Discriminant_Specifications (N)) then
3123 Error_Msg_N
3124 ("elementary or array type cannot have discriminants",
3125 Defining_Identifier
3126 (First (Discriminant_Specifications (N))));
3127 end if;
3128 end case;
3129
3130 -- Elaborate the type definition according to kind, and generate
3131 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3132 -- already done (this happens during the reanalysis that follows a call
3133 -- to the high level optimizer).
3134
3135 if not Analyzed (T) then
3136 Set_Analyzed (T);
3137
3138 -- Set the SPARK mode from the current context
3139
3140 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3141 Set_SPARK_Pragma_Inherited (T);
3142
3143 case Nkind (Def) is
3144 when N_Access_To_Subprogram_Definition =>
3145 Access_Subprogram_Declaration (T, Def);
3146
3147 -- If this is a remote access to subprogram, we must create the
3148 -- equivalent fat pointer type, and related subprograms.
3149
3150 if Is_Remote then
3151 Process_Remote_AST_Declaration (N);
3152 end if;
3153
3154 -- Validate categorization rule against access type declaration
3155 -- usually a violation in Pure unit, Shared_Passive unit.
3156
3157 Validate_Access_Type_Declaration (T, N);
3158
3159 when N_Access_To_Object_Definition =>
3160 Access_Type_Declaration (T, Def);
3161
3162 -- Validate categorization rule against access type declaration
3163 -- usually a violation in Pure unit, Shared_Passive unit.
3164
3165 Validate_Access_Type_Declaration (T, N);
3166
3167 -- If we are in a Remote_Call_Interface package and define a
3168 -- RACW, then calling stubs and specific stream attributes
3169 -- must be added.
3170
3171 if Is_Remote
3172 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3173 then
3174 Add_RACW_Features (Def_Id);
3175 end if;
3176
3177 when N_Array_Type_Definition =>
3178 Array_Type_Declaration (T, Def);
3179
3180 when N_Derived_Type_Definition =>
3181 Derived_Type_Declaration (T, N, T /= Def_Id);
3182
3183 -- Inherit predicates from parent, and protect against illegal
3184 -- derivations.
3185
3186 if Is_Type (T) and then Has_Predicates (T) then
3187 Set_Has_Predicates (Def_Id);
3188 end if;
3189
3190 -- Save the scenario for examination by the ABE Processing
3191 -- phase.
3192
3193 Record_Elaboration_Scenario (N);
3194
3195 when N_Enumeration_Type_Definition =>
3196 Enumeration_Type_Declaration (T, Def);
3197
3198 when N_Floating_Point_Definition =>
3199 Floating_Point_Type_Declaration (T, Def);
3200
3201 when N_Decimal_Fixed_Point_Definition =>
3202 Decimal_Fixed_Point_Type_Declaration (T, Def);
3203
3204 when N_Ordinary_Fixed_Point_Definition =>
3205 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3206
3207 when N_Signed_Integer_Type_Definition =>
3208 Signed_Integer_Type_Declaration (T, Def);
3209
3210 when N_Modular_Type_Definition =>
3211 Modular_Type_Declaration (T, Def);
3212
3213 when N_Record_Definition =>
3214 Record_Type_Declaration (T, N, Prev);
3215
3216 -- If declaration has a parse error, nothing to elaborate.
3217
3218 when N_Error =>
3219 null;
3220
3221 when others =>
3222 raise Program_Error;
3223 end case;
3224 end if;
3225
3226 if Etype (T) = Any_Type then
3227 return;
3228 end if;
3229
3230 -- Controlled type is not allowed in SPARK
3231
3232 if Is_Visibly_Controlled (T) then
3233 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3234 end if;
3235
3236 -- Some common processing for all types
3237
3238 Set_Depends_On_Private (T, Has_Private_Component (T));
3239 Check_Ops_From_Incomplete_Type;
3240
3241 -- Both the declared entity, and its anonymous base type if one was
3242 -- created, need freeze nodes allocated.
3243
3244 declare
3245 B : constant Entity_Id := Base_Type (T);
3246
3247 begin
3248 -- In the case where the base type differs from the first subtype, we
3249 -- pre-allocate a freeze node, and set the proper link to the first
3250 -- subtype. Freeze_Entity will use this preallocated freeze node when
3251 -- it freezes the entity.
3252
3253 -- This does not apply if the base type is a generic type, whose
3254 -- declaration is independent of the current derived definition.
3255
3256 if B /= T and then not Is_Generic_Type (B) then
3257 Ensure_Freeze_Node (B);
3258 Set_First_Subtype_Link (Freeze_Node (B), T);
3259 end if;
3260
3261 -- A type that is imported through a limited_with clause cannot
3262 -- generate any code, and thus need not be frozen. However, an access
3263 -- type with an imported designated type needs a finalization list,
3264 -- which may be referenced in some other package that has non-limited
3265 -- visibility on the designated type. Thus we must create the
3266 -- finalization list at the point the access type is frozen, to
3267 -- prevent unsatisfied references at link time.
3268
3269 if not From_Limited_With (T) or else Is_Access_Type (T) then
3270 Set_Has_Delayed_Freeze (T);
3271 end if;
3272 end;
3273
3274 -- Case where T is the full declaration of some private type which has
3275 -- been swapped in Defining_Identifier (N).
3276
3277 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3278 Process_Full_View (N, T, Def_Id);
3279
3280 -- Record the reference. The form of this is a little strange, since
3281 -- the full declaration has been swapped in. So the first parameter
3282 -- here represents the entity to which a reference is made which is
3283 -- the "real" entity, i.e. the one swapped in, and the second
3284 -- parameter provides the reference location.
3285
3286 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3287 -- since we don't want a complaint about the full type being an
3288 -- unwanted reference to the private type
3289
3290 declare
3291 B : constant Boolean := Has_Pragma_Unreferenced (T);
3292 begin
3293 Set_Has_Pragma_Unreferenced (T, False);
3294 Generate_Reference (T, T, 'c');
3295 Set_Has_Pragma_Unreferenced (T, B);
3296 end;
3297
3298 Set_Completion_Referenced (Def_Id);
3299
3300 -- For completion of incomplete type, process incomplete dependents
3301 -- and always mark the full type as referenced (it is the incomplete
3302 -- type that we get for any real reference).
3303
3304 elsif Ekind (Prev) = E_Incomplete_Type then
3305 Process_Incomplete_Dependents (N, T, Prev);
3306 Generate_Reference (Prev, Def_Id, 'c');
3307 Set_Completion_Referenced (Def_Id);
3308
3309 -- If not private type or incomplete type completion, this is a real
3310 -- definition of a new entity, so record it.
3311
3312 else
3313 Generate_Definition (Def_Id);
3314 end if;
3315
3316 -- Propagate any pending access types whose finalization masters need to
3317 -- be fully initialized from the partial to the full view. Guard against
3318 -- an illegal full view that remains unanalyzed.
3319
3320 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3321 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3322 end if;
3323
3324 if Chars (Scope (Def_Id)) = Name_System
3325 and then Chars (Def_Id) = Name_Address
3326 and then In_Predefined_Unit (N)
3327 then
3328 Set_Is_Descendant_Of_Address (Def_Id);
3329 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3330 Set_Is_Descendant_Of_Address (Prev);
3331 end if;
3332
3333 Set_Optimize_Alignment_Flags (Def_Id);
3334 Check_Eliminated (Def_Id);
3335
3336 -- If the declaration is a completion and aspects are present, apply
3337 -- them to the entity for the type which is currently the partial
3338 -- view, but which is the one that will be frozen.
3339
3340 if Has_Aspects (N) then
3341
3342 -- In most cases the partial view is a private type, and both views
3343 -- appear in different declarative parts. In the unusual case where
3344 -- the partial view is incomplete, perform the analysis on the
3345 -- full view, to prevent freezing anomalies with the corresponding
3346 -- class-wide type, which otherwise might be frozen before the
3347 -- dispatch table is built.
3348
3349 if Prev /= Def_Id
3350 and then Ekind (Prev) /= E_Incomplete_Type
3351 then
3352 Analyze_Aspect_Specifications (N, Prev);
3353
3354 -- Normal case
3355
3356 else
3357 Analyze_Aspect_Specifications (N, Def_Id);
3358 end if;
3359 end if;
3360
3361 if Is_Derived_Type (Prev)
3362 and then Def_Id /= Prev
3363 then
3364 Check_Nonoverridable_Aspects;
3365 end if;
3366 end Analyze_Full_Type_Declaration;
3367
3368 ----------------------------------
3369 -- Analyze_Incomplete_Type_Decl --
3370 ----------------------------------
3371
3372 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3373 F : constant Boolean := Is_Pure (Current_Scope);
3374 T : Entity_Id;
3375
3376 begin
3377 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3378
3379 Generate_Definition (Defining_Identifier (N));
3380
3381 -- Process an incomplete declaration. The identifier must not have been
3382 -- declared already in the scope. However, an incomplete declaration may
3383 -- appear in the private part of a package, for a private type that has
3384 -- already been declared.
3385
3386 -- In this case, the discriminants (if any) must match
3387
3388 T := Find_Type_Name (N);
3389
3390 Set_Ekind (T, E_Incomplete_Type);
3391 Set_Etype (T, T);
3392 Set_Is_First_Subtype (T);
3393 Init_Size_Align (T);
3394
3395 -- Set the SPARK mode from the current context
3396
3397 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3398 Set_SPARK_Pragma_Inherited (T);
3399
3400 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3401 -- incomplete types.
3402
3403 if Tagged_Present (N) then
3404 Set_Is_Tagged_Type (T, True);
3405 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3406 Make_Class_Wide_Type (T);
3407 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3408 end if;
3409
3410 Set_Stored_Constraint (T, No_Elist);
3411
3412 if Present (Discriminant_Specifications (N)) then
3413 Push_Scope (T);
3414 Process_Discriminants (N);
3415 End_Scope;
3416 end if;
3417
3418 -- If the type has discriminants, nontrivial subtypes may be declared
3419 -- before the full view of the type. The full views of those subtypes
3420 -- will be built after the full view of the type.
3421
3422 Set_Private_Dependents (T, New_Elmt_List);
3423 Set_Is_Pure (T, F);
3424 end Analyze_Incomplete_Type_Decl;
3425
3426 -----------------------------------
3427 -- Analyze_Interface_Declaration --
3428 -----------------------------------
3429
3430 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3431 CW : constant Entity_Id := Class_Wide_Type (T);
3432
3433 begin
3434 Set_Is_Tagged_Type (T);
3435 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3436
3437 Set_Is_Limited_Record (T, Limited_Present (Def)
3438 or else Task_Present (Def)
3439 or else Protected_Present (Def)
3440 or else Synchronized_Present (Def));
3441
3442 -- Type is abstract if full declaration carries keyword, or if previous
3443 -- partial view did.
3444
3445 Set_Is_Abstract_Type (T);
3446 Set_Is_Interface (T);
3447
3448 -- Type is a limited interface if it includes the keyword limited, task,
3449 -- protected, or synchronized.
3450
3451 Set_Is_Limited_Interface
3452 (T, Limited_Present (Def)
3453 or else Protected_Present (Def)
3454 or else Synchronized_Present (Def)
3455 or else Task_Present (Def));
3456
3457 Set_Interfaces (T, New_Elmt_List);
3458 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3459
3460 -- Complete the decoration of the class-wide entity if it was already
3461 -- built (i.e. during the creation of the limited view)
3462
3463 if Present (CW) then
3464 Set_Is_Interface (CW);
3465 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3466 end if;
3467
3468 -- Check runtime support for synchronized interfaces
3469
3470 if (Is_Task_Interface (T)
3471 or else Is_Protected_Interface (T)
3472 or else Is_Synchronized_Interface (T))
3473 and then not RTE_Available (RE_Select_Specific_Data)
3474 then
3475 Error_Msg_CRT ("synchronized interfaces", T);
3476 end if;
3477 end Analyze_Interface_Declaration;
3478
3479 -----------------------------
3480 -- Analyze_Itype_Reference --
3481 -----------------------------
3482
3483 -- Nothing to do. This node is placed in the tree only for the benefit of
3484 -- back end processing, and has no effect on the semantic processing.
3485
3486 procedure Analyze_Itype_Reference (N : Node_Id) is
3487 begin
3488 pragma Assert (Is_Itype (Itype (N)));
3489 null;
3490 end Analyze_Itype_Reference;
3491
3492 --------------------------------
3493 -- Analyze_Number_Declaration --
3494 --------------------------------
3495
3496 procedure Analyze_Number_Declaration (N : Node_Id) is
3497 E : constant Node_Id := Expression (N);
3498 Id : constant Entity_Id := Defining_Identifier (N);
3499 Index : Interp_Index;
3500 It : Interp;
3501 T : Entity_Id;
3502
3503 begin
3504 Generate_Definition (Id);
3505 Enter_Name (Id);
3506
3507 -- This is an optimization of a common case of an integer literal
3508
3509 if Nkind (E) = N_Integer_Literal then
3510 Set_Is_Static_Expression (E, True);
3511 Set_Etype (E, Universal_Integer);
3512
3513 Set_Etype (Id, Universal_Integer);
3514 Set_Ekind (Id, E_Named_Integer);
3515 Set_Is_Frozen (Id, True);
3516 return;
3517 end if;
3518
3519 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3520
3521 -- Process expression, replacing error by integer zero, to avoid
3522 -- cascaded errors or aborts further along in the processing
3523
3524 -- Replace Error by integer zero, which seems least likely to cause
3525 -- cascaded errors.
3526
3527 if E = Error then
3528 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3529 Set_Error_Posted (E);
3530 end if;
3531
3532 Analyze (E);
3533
3534 -- Verify that the expression is static and numeric. If
3535 -- the expression is overloaded, we apply the preference
3536 -- rule that favors root numeric types.
3537
3538 if not Is_Overloaded (E) then
3539 T := Etype (E);
3540 if Has_Dynamic_Predicate_Aspect (T) then
3541 Error_Msg_N
3542 ("subtype has dynamic predicate, "
3543 & "not allowed in number declaration", N);
3544 end if;
3545
3546 else
3547 T := Any_Type;
3548
3549 Get_First_Interp (E, Index, It);
3550 while Present (It.Typ) loop
3551 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3552 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3553 then
3554 if T = Any_Type then
3555 T := It.Typ;
3556
3557 elsif It.Typ = Universal_Real
3558 or else
3559 It.Typ = Universal_Integer
3560 then
3561 -- Choose universal interpretation over any other
3562
3563 T := It.Typ;
3564 exit;
3565 end if;
3566 end if;
3567
3568 Get_Next_Interp (Index, It);
3569 end loop;
3570 end if;
3571
3572 if Is_Integer_Type (T) then
3573 Resolve (E, T);
3574 Set_Etype (Id, Universal_Integer);
3575 Set_Ekind (Id, E_Named_Integer);
3576
3577 elsif Is_Real_Type (T) then
3578
3579 -- Because the real value is converted to universal_real, this is a
3580 -- legal context for a universal fixed expression.
3581
3582 if T = Universal_Fixed then
3583 declare
3584 Loc : constant Source_Ptr := Sloc (N);
3585 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3586 Subtype_Mark =>
3587 New_Occurrence_Of (Universal_Real, Loc),
3588 Expression => Relocate_Node (E));
3589
3590 begin
3591 Rewrite (E, Conv);
3592 Analyze (E);
3593 end;
3594
3595 elsif T = Any_Fixed then
3596 Error_Msg_N ("illegal context for mixed mode operation", E);
3597
3598 -- Expression is of the form : universal_fixed * integer. Try to
3599 -- resolve as universal_real.
3600
3601 T := Universal_Real;
3602 Set_Etype (E, T);
3603 end if;
3604
3605 Resolve (E, T);
3606 Set_Etype (Id, Universal_Real);
3607 Set_Ekind (Id, E_Named_Real);
3608
3609 else
3610 Wrong_Type (E, Any_Numeric);
3611 Resolve (E, T);
3612
3613 Set_Etype (Id, T);
3614 Set_Ekind (Id, E_Constant);
3615 Set_Never_Set_In_Source (Id, True);
3616 Set_Is_True_Constant (Id, True);
3617 return;
3618 end if;
3619
3620 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3621 Set_Etype (E, Etype (Id));
3622 end if;
3623
3624 if not Is_OK_Static_Expression (E) then
3625 Flag_Non_Static_Expr
3626 ("non-static expression used in number declaration!", E);
3627 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3628 Set_Etype (E, Any_Type);
3629 end if;
3630
3631 Analyze_Dimension (N);
3632 end Analyze_Number_Declaration;
3633
3634 --------------------------------
3635 -- Analyze_Object_Declaration --
3636 --------------------------------
3637
3638 -- WARNING: This routine manages Ghost regions. Return statements must be
3639 -- replaced by gotos which jump to the end of the routine and restore the
3640 -- Ghost mode.
3641
3642 procedure Analyze_Object_Declaration (N : Node_Id) is
3643 Loc : constant Source_Ptr := Sloc (N);
3644 Id : constant Entity_Id := Defining_Identifier (N);
3645 Act_T : Entity_Id;
3646 T : Entity_Id;
3647
3648 E : Node_Id := Expression (N);
3649 -- E is set to Expression (N) throughout this routine. When Expression
3650 -- (N) is modified, E is changed accordingly.
3651
3652 Prev_Entity : Entity_Id := Empty;
3653
3654 procedure Check_Dynamic_Object (Typ : Entity_Id);
3655 -- A library-level object with non-static discriminant constraints may
3656 -- require dynamic allocation. The declaration is illegal if the
3657 -- profile includes the restriction No_Implicit_Heap_Allocations.
3658
3659 procedure Check_For_Null_Excluding_Components
3660 (Obj_Typ : Entity_Id;
3661 Obj_Decl : Node_Id);
3662 -- Verify that each null-excluding component of object declaration
3663 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3664 -- a compile-time warning if this is not the case.
3665
3666 function Count_Tasks (T : Entity_Id) return Uint;
3667 -- This function is called when a non-generic library level object of a
3668 -- task type is declared. Its function is to count the static number of
3669 -- tasks declared within the type (it is only called if Has_Task is set
3670 -- for T). As a side effect, if an array of tasks with non-static bounds
3671 -- or a variant record type is encountered, Check_Restriction is called
3672 -- indicating the count is unknown.
3673
3674 function Delayed_Aspect_Present return Boolean;
3675 -- If the declaration has an expression that is an aggregate, and it
3676 -- has aspects that require delayed analysis, the resolution of the
3677 -- aggregate must be deferred to the freeze point of the object. This
3678 -- special processing was created for address clauses, but it must
3679 -- also apply to Alignment. This must be done before the aspect
3680 -- specifications are analyzed because we must handle the aggregate
3681 -- before the analysis of the object declaration is complete.
3682
3683 -- Any other relevant delayed aspects on object declarations ???
3684
3685 --------------------------
3686 -- Check_Dynamic_Object --
3687 --------------------------
3688
3689 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3690 Comp : Entity_Id;
3691 Obj_Type : Entity_Id;
3692
3693 begin
3694 Obj_Type := Typ;
3695
3696 if Is_Private_Type (Obj_Type)
3697 and then Present (Full_View (Obj_Type))
3698 then
3699 Obj_Type := Full_View (Obj_Type);
3700 end if;
3701
3702 if Known_Static_Esize (Obj_Type) then
3703 return;
3704 end if;
3705
3706 if Restriction_Active (No_Implicit_Heap_Allocations)
3707 and then Expander_Active
3708 and then Has_Discriminants (Obj_Type)
3709 then
3710 Comp := First_Component (Obj_Type);
3711 while Present (Comp) loop
3712 if Known_Static_Esize (Etype (Comp))
3713 or else Size_Known_At_Compile_Time (Etype (Comp))
3714 then
3715 null;
3716
3717 elsif not Discriminated_Size (Comp)
3718 and then Comes_From_Source (Comp)
3719 then
3720 Error_Msg_NE
3721 ("component& of non-static size will violate restriction "
3722 & "No_Implicit_Heap_Allocation?", N, Comp);
3723
3724 elsif Is_Record_Type (Etype (Comp)) then
3725 Check_Dynamic_Object (Etype (Comp));
3726 end if;
3727
3728 Next_Component (Comp);
3729 end loop;
3730 end if;
3731 end Check_Dynamic_Object;
3732
3733 -----------------------------------------
3734 -- Check_For_Null_Excluding_Components --
3735 -----------------------------------------
3736
3737 procedure Check_For_Null_Excluding_Components
3738 (Obj_Typ : Entity_Id;
3739 Obj_Decl : Node_Id)
3740 is
3741 procedure Check_Component
3742 (Comp_Typ : Entity_Id;
3743 Comp_Decl : Node_Id := Empty;
3744 Array_Comp : Boolean := False);
3745 -- Apply a compile-time null-exclusion check on a component denoted
3746 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3747 -- subcomponents (if any).
3748
3749 ---------------------
3750 -- Check_Component --
3751 ---------------------
3752
3753 procedure Check_Component
3754 (Comp_Typ : Entity_Id;
3755 Comp_Decl : Node_Id := Empty;
3756 Array_Comp : Boolean := False)
3757 is
3758 Comp : Entity_Id;
3759 T : Entity_Id;
3760
3761 begin
3762 -- Do not consider internally-generated components or those that
3763 -- are already initialized.
3764
3765 if Present (Comp_Decl)
3766 and then (not Comes_From_Source (Comp_Decl)
3767 or else Present (Expression (Comp_Decl)))
3768 then
3769 return;
3770 end if;
3771
3772 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3773 and then Present (Full_View (Comp_Typ))
3774 then
3775 T := Full_View (Comp_Typ);
3776 else
3777 T := Comp_Typ;
3778 end if;
3779
3780 -- Verify a component of a null-excluding access type
3781
3782 if Is_Access_Type (T)
3783 and then Can_Never_Be_Null (T)
3784 then
3785 if Comp_Decl = Obj_Decl then
3786 Null_Exclusion_Static_Checks
3787 (N => Obj_Decl,
3788 Comp => Empty,
3789 Array_Comp => Array_Comp);
3790
3791 else
3792 Null_Exclusion_Static_Checks
3793 (N => Obj_Decl,
3794 Comp => Comp_Decl,
3795 Array_Comp => Array_Comp);
3796 end if;
3797
3798 -- Check array components
3799
3800 elsif Is_Array_Type (T) then
3801
3802 -- There is no suitable component when the object is of an
3803 -- array type. However, a namable component may appear at some
3804 -- point during the recursive inspection, but not at the top
3805 -- level. At the top level just indicate array component case.
3806
3807 if Comp_Decl = Obj_Decl then
3808 Check_Component (Component_Type (T), Array_Comp => True);
3809 else
3810 Check_Component (Component_Type (T), Comp_Decl);
3811 end if;
3812
3813 -- Verify all components of type T
3814
3815 -- Note: No checks are performed on types with discriminants due
3816 -- to complexities involving variants. ???
3817
3818 elsif (Is_Concurrent_Type (T)
3819 or else Is_Incomplete_Or_Private_Type (T)
3820 or else Is_Record_Type (T))
3821 and then not Has_Discriminants (T)
3822 then
3823 Comp := First_Component (T);
3824 while Present (Comp) loop
3825 Check_Component (Etype (Comp), Parent (Comp));
3826
3827 Comp := Next_Component (Comp);
3828 end loop;
3829 end if;
3830 end Check_Component;
3831
3832 -- Start processing for Check_For_Null_Excluding_Components
3833
3834 begin
3835 Check_Component (Obj_Typ, Obj_Decl);
3836 end Check_For_Null_Excluding_Components;
3837
3838 -----------------
3839 -- Count_Tasks --
3840 -----------------
3841
3842 function Count_Tasks (T : Entity_Id) return Uint is
3843 C : Entity_Id;
3844 X : Node_Id;
3845 V : Uint;
3846
3847 begin
3848 if Is_Task_Type (T) then
3849 return Uint_1;
3850
3851 elsif Is_Record_Type (T) then
3852 if Has_Discriminants (T) then
3853 Check_Restriction (Max_Tasks, N);
3854 return Uint_0;
3855
3856 else
3857 V := Uint_0;
3858 C := First_Component (T);
3859 while Present (C) loop
3860 V := V + Count_Tasks (Etype (C));
3861 Next_Component (C);
3862 end loop;
3863
3864 return V;
3865 end if;
3866
3867 elsif Is_Array_Type (T) then
3868 X := First_Index (T);
3869 V := Count_Tasks (Component_Type (T));
3870 while Present (X) loop
3871 C := Etype (X);
3872
3873 if not Is_OK_Static_Subtype (C) then
3874 Check_Restriction (Max_Tasks, N);
3875 return Uint_0;
3876 else
3877 V := V * (UI_Max (Uint_0,
3878 Expr_Value (Type_High_Bound (C)) -
3879 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3880 end if;
3881
3882 Next_Index (X);
3883 end loop;
3884
3885 return V;
3886
3887 else
3888 return Uint_0;
3889 end if;
3890 end Count_Tasks;
3891
3892 ----------------------------
3893 -- Delayed_Aspect_Present --
3894 ----------------------------
3895
3896 function Delayed_Aspect_Present return Boolean is
3897 A : Node_Id;
3898 A_Id : Aspect_Id;
3899
3900 begin
3901 if Present (Aspect_Specifications (N)) then
3902 A := First (Aspect_Specifications (N));
3903 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3904 while Present (A) loop
3905 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3906 return True;
3907 end if;
3908
3909 Next (A);
3910 end loop;
3911 end if;
3912
3913 return False;
3914 end Delayed_Aspect_Present;
3915
3916 -- Local variables
3917
3918 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3919 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3920 -- Save the Ghost-related attributes to restore on exit
3921
3922 Related_Id : Entity_Id;
3923
3924 -- Start of processing for Analyze_Object_Declaration
3925
3926 begin
3927 -- There are three kinds of implicit types generated by an
3928 -- object declaration:
3929
3930 -- 1. Those generated by the original Object Definition
3931
3932 -- 2. Those generated by the Expression
3933
3934 -- 3. Those used to constrain the Object Definition with the
3935 -- expression constraints when the definition is unconstrained.
3936
3937 -- They must be generated in this order to avoid order of elaboration
3938 -- issues. Thus the first step (after entering the name) is to analyze
3939 -- the object definition.
3940
3941 if Constant_Present (N) then
3942 Prev_Entity := Current_Entity_In_Scope (Id);
3943
3944 if Present (Prev_Entity)
3945 and then
3946 -- If the homograph is an implicit subprogram, it is overridden
3947 -- by the current declaration.
3948
3949 ((Is_Overloadable (Prev_Entity)
3950 and then Is_Inherited_Operation (Prev_Entity))
3951
3952 -- The current object is a discriminal generated for an entry
3953 -- family index. Even though the index is a constant, in this
3954 -- particular context there is no true constant redeclaration.
3955 -- Enter_Name will handle the visibility.
3956
3957 or else
3958 (Is_Discriminal (Id)
3959 and then Ekind (Discriminal_Link (Id)) =
3960 E_Entry_Index_Parameter)
3961
3962 -- The current object is the renaming for a generic declared
3963 -- within the instance.
3964
3965 or else
3966 (Ekind (Prev_Entity) = E_Package
3967 and then Nkind (Parent (Prev_Entity)) =
3968 N_Package_Renaming_Declaration
3969 and then not Comes_From_Source (Prev_Entity)
3970 and then
3971 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3972
3973 -- The entity may be a homonym of a private component of the
3974 -- enclosing protected object, for which we create a local
3975 -- renaming declaration. The declaration is legal, even if
3976 -- useless when it just captures that component.
3977
3978 or else
3979 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3980 and then Nkind (Parent (Prev_Entity)) =
3981 N_Object_Renaming_Declaration))
3982 then
3983 Prev_Entity := Empty;
3984 end if;
3985 end if;
3986
3987 if Present (Prev_Entity) then
3988
3989 -- The object declaration is Ghost when it completes a deferred Ghost
3990 -- constant.
3991
3992 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3993
3994 Constant_Redeclaration (Id, N, T);
3995
3996 Generate_Reference (Prev_Entity, Id, 'c');
3997 Set_Completion_Referenced (Id);
3998
3999 if Error_Posted (N) then
4000
4001 -- Type mismatch or illegal redeclaration; do not analyze
4002 -- expression to avoid cascaded errors.
4003
4004 T := Find_Type_Of_Object (Object_Definition (N), N);
4005 Set_Etype (Id, T);
4006 Set_Ekind (Id, E_Variable);
4007 goto Leave;
4008 end if;
4009
4010 -- In the normal case, enter identifier at the start to catch premature
4011 -- usage in the initialization expression.
4012
4013 else
4014 Generate_Definition (Id);
4015 Enter_Name (Id);
4016
4017 Mark_Coextensions (N, Object_Definition (N));
4018
4019 T := Find_Type_Of_Object (Object_Definition (N), N);
4020
4021 if Nkind (Object_Definition (N)) = N_Access_Definition
4022 and then Present
4023 (Access_To_Subprogram_Definition (Object_Definition (N)))
4024 and then Protected_Present
4025 (Access_To_Subprogram_Definition (Object_Definition (N)))
4026 then
4027 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4028 end if;
4029
4030 if Error_Posted (Id) then
4031 Set_Etype (Id, T);
4032 Set_Ekind (Id, E_Variable);
4033 goto Leave;
4034 end if;
4035 end if;
4036
4037 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4038 -- out some static checks.
4039
4040 if Ada_Version >= Ada_2005 then
4041
4042 -- In case of aggregates we must also take care of the correct
4043 -- initialization of nested aggregates bug this is done at the
4044 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4045
4046 if Can_Never_Be_Null (T) then
4047 if Present (Expression (N))
4048 and then Nkind (Expression (N)) = N_Aggregate
4049 then
4050 null;
4051
4052 else
4053 declare
4054 Save_Typ : constant Entity_Id := Etype (Id);
4055 begin
4056 Set_Etype (Id, T); -- Temp. decoration for static checks
4057 Null_Exclusion_Static_Checks (N);
4058 Set_Etype (Id, Save_Typ);
4059 end;
4060 end if;
4061
4062 -- We might be dealing with an object of a composite type containing
4063 -- null-excluding components without an aggregate, so we must verify
4064 -- that such components have default initialization.
4065
4066 else
4067 Check_For_Null_Excluding_Components (T, N);
4068 end if;
4069 end if;
4070
4071 -- Object is marked pure if it is in a pure scope
4072
4073 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4074
4075 -- If deferred constant, make sure context is appropriate. We detect
4076 -- a deferred constant as a constant declaration with no expression.
4077 -- A deferred constant can appear in a package body if its completion
4078 -- is by means of an interface pragma.
4079
4080 if Constant_Present (N) and then No (E) then
4081
4082 -- A deferred constant may appear in the declarative part of the
4083 -- following constructs:
4084
4085 -- blocks
4086 -- entry bodies
4087 -- extended return statements
4088 -- package specs
4089 -- package bodies
4090 -- subprogram bodies
4091 -- task bodies
4092
4093 -- When declared inside a package spec, a deferred constant must be
4094 -- completed by a full constant declaration or pragma Import. In all
4095 -- other cases, the only proper completion is pragma Import. Extended
4096 -- return statements are flagged as invalid contexts because they do
4097 -- not have a declarative part and so cannot accommodate the pragma.
4098
4099 if Ekind (Current_Scope) = E_Return_Statement then
4100 Error_Msg_N
4101 ("invalid context for deferred constant declaration (RM 7.4)",
4102 N);
4103 Error_Msg_N
4104 ("\declaration requires an initialization expression",
4105 N);
4106 Set_Constant_Present (N, False);
4107
4108 -- In Ada 83, deferred constant must be of private type
4109
4110 elsif not Is_Private_Type (T) then
4111 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4112 Error_Msg_N
4113 ("(Ada 83) deferred constant must be private type", N);
4114 end if;
4115 end if;
4116
4117 -- If not a deferred constant, then the object declaration freezes
4118 -- its type, unless the object is of an anonymous type and has delayed
4119 -- aspects. In that case the type is frozen when the object itself is.
4120
4121 else
4122 Check_Fully_Declared (T, N);
4123
4124 if Has_Delayed_Aspects (Id)
4125 and then Is_Array_Type (T)
4126 and then Is_Itype (T)
4127 then
4128 Set_Has_Delayed_Freeze (T);
4129 else
4130 Freeze_Before (N, T);
4131 end if;
4132 end if;
4133
4134 -- If the object was created by a constrained array definition, then
4135 -- set the link in both the anonymous base type and anonymous subtype
4136 -- that are built to represent the array type to point to the object.
4137
4138 if Nkind (Object_Definition (Declaration_Node (Id))) =
4139 N_Constrained_Array_Definition
4140 then
4141 Set_Related_Array_Object (T, Id);
4142 Set_Related_Array_Object (Base_Type (T), Id);
4143 end if;
4144
4145 -- Special checks for protected objects not at library level
4146
4147 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4148 Check_Restriction (No_Local_Protected_Objects, Id);
4149
4150 -- Protected objects with interrupt handlers must be at library level
4151
4152 -- Ada 2005: This test is not needed (and the corresponding clause
4153 -- in the RM is removed) because accessibility checks are sufficient
4154 -- to make handlers not at the library level illegal.
4155
4156 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4157 -- applies to the '95 version of the language as well.
4158
4159 if Is_Protected_Type (T)
4160 and then Has_Interrupt_Handler (T)
4161 and then Ada_Version < Ada_95
4162 then
4163 Error_Msg_N
4164 ("interrupt object can only be declared at library level", Id);
4165 end if;
4166 end if;
4167
4168 -- Check for violation of No_Local_Timing_Events
4169
4170 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4171 Check_Restriction (No_Local_Timing_Events, Id);
4172 end if;
4173
4174 -- The actual subtype of the object is the nominal subtype, unless
4175 -- the nominal one is unconstrained and obtained from the expression.
4176
4177 Act_T := T;
4178
4179 -- These checks should be performed before the initialization expression
4180 -- is considered, so that the Object_Definition node is still the same
4181 -- as in source code.
4182
4183 -- In SPARK, the nominal subtype is always given by a subtype mark
4184 -- and must not be unconstrained. (The only exception to this is the
4185 -- acceptance of declarations of constants of type String.)
4186
4187 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4188 then
4189 Check_SPARK_05_Restriction
4190 ("subtype mark required", Object_Definition (N));
4191
4192 elsif Is_Array_Type (T)
4193 and then not Is_Constrained (T)
4194 and then T /= Standard_String
4195 then
4196 Check_SPARK_05_Restriction
4197 ("subtype mark of constrained type expected",
4198 Object_Definition (N));
4199 end if;
4200
4201 if Is_Library_Level_Entity (Id) then
4202 Check_Dynamic_Object (T);
4203 end if;
4204
4205 -- There are no aliased objects in SPARK
4206
4207 if Aliased_Present (N) then
4208 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4209 end if;
4210
4211 -- Process initialization expression if present and not in error
4212
4213 if Present (E) and then E /= Error then
4214
4215 -- Generate an error in case of CPP class-wide object initialization.
4216 -- Required because otherwise the expansion of the class-wide
4217 -- assignment would try to use 'size to initialize the object
4218 -- (primitive that is not available in CPP tagged types).
4219
4220 if Is_Class_Wide_Type (Act_T)
4221 and then
4222 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4223 or else
4224 (Present (Full_View (Root_Type (Etype (Act_T))))
4225 and then
4226 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4227 then
4228 Error_Msg_N
4229 ("predefined assignment not available for 'C'P'P tagged types",
4230 E);
4231 end if;
4232
4233 Mark_Coextensions (N, E);
4234 Analyze (E);
4235
4236 -- In case of errors detected in the analysis of the expression,
4237 -- decorate it with the expected type to avoid cascaded errors
4238
4239 if No (Etype (E)) then
4240 Set_Etype (E, T);
4241 end if;
4242
4243 -- If an initialization expression is present, then we set the
4244 -- Is_True_Constant flag. It will be reset if this is a variable
4245 -- and it is indeed modified.
4246
4247 Set_Is_True_Constant (Id, True);
4248
4249 -- If we are analyzing a constant declaration, set its completion
4250 -- flag after analyzing and resolving the expression.
4251
4252 if Constant_Present (N) then
4253 Set_Has_Completion (Id);
4254 end if;
4255
4256 -- Set type and resolve (type may be overridden later on). Note:
4257 -- Ekind (Id) must still be E_Void at this point so that incorrect
4258 -- early usage within E is properly diagnosed.
4259
4260 Set_Etype (Id, T);
4261
4262 -- If the expression is an aggregate we must look ahead to detect
4263 -- the possible presence of an address clause, and defer resolution
4264 -- and expansion of the aggregate to the freeze point of the entity.
4265
4266 -- This is not always legal because the aggregate may contain other
4267 -- references that need freezing, e.g. references to other entities
4268 -- with address clauses. In any case, when compiling with -gnatI the
4269 -- presence of the address clause must be ignored.
4270
4271 if Comes_From_Source (N)
4272 and then Expander_Active
4273 and then Nkind (E) = N_Aggregate
4274 and then
4275 ((Present (Following_Address_Clause (N))
4276 and then not Ignore_Rep_Clauses)
4277 or else Delayed_Aspect_Present)
4278 then
4279 Set_Etype (E, T);
4280
4281 else
4282
4283 -- If the expression is a formal that is a "subprogram pointer"
4284 -- this is illegal in accessibility terms. Add an explicit
4285 -- conversion to force the corresponding check, as is done for
4286 -- assignments.
4287
4288 if Comes_From_Source (N)
4289 and then Is_Entity_Name (E)
4290 and then Present (Entity (E))
4291 and then Is_Formal (Entity (E))
4292 and then
4293 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4294 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4295 then
4296 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4297 end if;
4298
4299 Resolve (E, T);
4300 end if;
4301
4302 -- No further action needed if E is a call to an inlined function
4303 -- which returns an unconstrained type and it has been expanded into
4304 -- a procedure call. In that case N has been replaced by an object
4305 -- declaration without initializing expression and it has been
4306 -- analyzed (see Expand_Inlined_Call).
4307
4308 if Back_End_Inlining
4309 and then Expander_Active
4310 and then Nkind (E) = N_Function_Call
4311 and then Nkind (Name (E)) in N_Has_Entity
4312 and then Is_Inlined (Entity (Name (E)))
4313 and then not Is_Constrained (Etype (E))
4314 and then Analyzed (N)
4315 and then No (Expression (N))
4316 then
4317 goto Leave;
4318 end if;
4319
4320 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4321 -- node (which was marked already-analyzed), we need to set the type
4322 -- to something other than Any_Access in order to keep gigi happy.
4323
4324 if Etype (E) = Any_Access then
4325 Set_Etype (E, T);
4326 end if;
4327
4328 -- If the object is an access to variable, the initialization
4329 -- expression cannot be an access to constant.
4330
4331 if Is_Access_Type (T)
4332 and then not Is_Access_Constant (T)
4333 and then Is_Access_Type (Etype (E))
4334 and then Is_Access_Constant (Etype (E))
4335 then
4336 Error_Msg_N
4337 ("access to variable cannot be initialized with an "
4338 & "access-to-constant expression", E);
4339 end if;
4340
4341 if not Assignment_OK (N) then
4342 Check_Initialization (T, E);
4343 end if;
4344
4345 Check_Unset_Reference (E);
4346
4347 -- If this is a variable, then set current value. If this is a
4348 -- declared constant of a scalar type with a static expression,
4349 -- indicate that it is always valid.
4350
4351 if not Constant_Present (N) then
4352 if Compile_Time_Known_Value (E) then
4353 Set_Current_Value (Id, E);
4354 end if;
4355
4356 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4357 Set_Is_Known_Valid (Id);
4358 end if;
4359
4360 -- Deal with setting of null flags
4361
4362 if Is_Access_Type (T) then
4363 if Known_Non_Null (E) then
4364 Set_Is_Known_Non_Null (Id, True);
4365 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4366 Set_Is_Known_Null (Id, True);
4367 end if;
4368 end if;
4369
4370 -- Check incorrect use of dynamically tagged expressions
4371
4372 if Is_Tagged_Type (T) then
4373 Check_Dynamically_Tagged_Expression
4374 (Expr => E,
4375 Typ => T,
4376 Related_Nod => N);
4377 end if;
4378
4379 Apply_Scalar_Range_Check (E, T);
4380 Apply_Static_Length_Check (E, T);
4381
4382 if Nkind (Original_Node (N)) = N_Object_Declaration
4383 and then Comes_From_Source (Original_Node (N))
4384
4385 -- Only call test if needed
4386
4387 and then Restriction_Check_Required (SPARK_05)
4388 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4389 then
4390 Check_SPARK_05_Restriction
4391 ("initialization expression is not appropriate", E);
4392 end if;
4393
4394 -- A formal parameter of a specific tagged type whose related
4395 -- subprogram is subject to pragma Extensions_Visible with value
4396 -- "False" cannot be implicitly converted to a class-wide type by
4397 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4398 -- not consider internally generated expressions.
4399
4400 if Is_Class_Wide_Type (T)
4401 and then Comes_From_Source (E)
4402 and then Is_EVF_Expression (E)
4403 then
4404 Error_Msg_N
4405 ("formal parameter cannot be implicitly converted to "
4406 & "class-wide type when Extensions_Visible is False", E);
4407 end if;
4408 end if;
4409
4410 -- If the No_Streams restriction is set, check that the type of the
4411 -- object is not, and does not contain, any subtype derived from
4412 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4413 -- Has_Stream just for efficiency reasons. There is no point in
4414 -- spending time on a Has_Stream check if the restriction is not set.
4415
4416 if Restriction_Check_Required (No_Streams) then
4417 if Has_Stream (T) then
4418 Check_Restriction (No_Streams, N);
4419 end if;
4420 end if;
4421
4422 -- Deal with predicate check before we start to do major rewriting. It
4423 -- is OK to initialize and then check the initialized value, since the
4424 -- object goes out of scope if we get a predicate failure. Note that we
4425 -- do this in the analyzer and not the expander because the analyzer
4426 -- does some substantial rewriting in some cases.
4427
4428 -- We need a predicate check if the type has predicates that are not
4429 -- ignored, and if either there is an initializing expression, or for
4430 -- default initialization when we have at least one case of an explicit
4431 -- default initial value and then this is not an internal declaration
4432 -- whose initialization comes later (as for an aggregate expansion).
4433
4434 if not Suppress_Assignment_Checks (N)
4435 and then Present (Predicate_Function (T))
4436 and then not Predicates_Ignored (T)
4437 and then not No_Initialization (N)
4438 and then
4439 (Present (E)
4440 or else
4441 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4442 then
4443 -- If the type has a static predicate and the expression is known at
4444 -- compile time, see if the expression satisfies the predicate.
4445
4446 if Present (E) then
4447 Check_Expression_Against_Static_Predicate (E, T);
4448 end if;
4449
4450 -- If the type is a null record and there is no explicit initial
4451 -- expression, no predicate check applies.
4452
4453 if No (E) and then Is_Null_Record_Type (T) then
4454 null;
4455
4456 -- Do not generate a predicate check if the initialization expression
4457 -- is a type conversion because the conversion has been subjected to
4458 -- the same check. This is a small optimization which avoid redundant
4459 -- checks.
4460
4461 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4462 null;
4463
4464 else
4465 Insert_After (N,
4466 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4467 end if;
4468 end if;
4469
4470 -- Case of unconstrained type
4471
4472 if not Is_Definite_Subtype (T) then
4473
4474 -- In SPARK, a declaration of unconstrained type is allowed
4475 -- only for constants of type string.
4476
4477 if Is_String_Type (T) and then not Constant_Present (N) then
4478 Check_SPARK_05_Restriction
4479 ("declaration of object of unconstrained type not allowed", N);
4480 end if;
4481
4482 -- Nothing to do in deferred constant case
4483
4484 if Constant_Present (N) and then No (E) then
4485 null;
4486
4487 -- Case of no initialization present
4488
4489 elsif No (E) then
4490 if No_Initialization (N) then
4491 null;
4492
4493 elsif Is_Class_Wide_Type (T) then
4494 Error_Msg_N
4495 ("initialization required in class-wide declaration ", N);
4496
4497 else
4498 Error_Msg_N
4499 ("unconstrained subtype not allowed (need initialization)",
4500 Object_Definition (N));
4501
4502 if Is_Record_Type (T) and then Has_Discriminants (T) then
4503 Error_Msg_N
4504 ("\provide initial value or explicit discriminant values",
4505 Object_Definition (N));
4506
4507 Error_Msg_NE
4508 ("\or give default discriminant values for type&",
4509 Object_Definition (N), T);
4510
4511 elsif Is_Array_Type (T) then
4512 Error_Msg_N
4513 ("\provide initial value or explicit array bounds",
4514 Object_Definition (N));
4515 end if;
4516 end if;
4517
4518 -- Case of initialization present but in error. Set initial
4519 -- expression as absent (but do not make above complaints)
4520
4521 elsif E = Error then
4522 Set_Expression (N, Empty);
4523 E := Empty;
4524
4525 -- Case of initialization present
4526
4527 else
4528 -- Check restrictions in Ada 83
4529
4530 if not Constant_Present (N) then
4531
4532 -- Unconstrained variables not allowed in Ada 83 mode
4533
4534 if Ada_Version = Ada_83
4535 and then Comes_From_Source (Object_Definition (N))
4536 then
4537 Error_Msg_N
4538 ("(Ada 83) unconstrained variable not allowed",
4539 Object_Definition (N));
4540 end if;
4541 end if;
4542
4543 -- Now we constrain the variable from the initializing expression
4544
4545 -- If the expression is an aggregate, it has been expanded into
4546 -- individual assignments. Retrieve the actual type from the
4547 -- expanded construct.
4548
4549 if Is_Array_Type (T)
4550 and then No_Initialization (N)
4551 and then Nkind (Original_Node (E)) = N_Aggregate
4552 then
4553 Act_T := Etype (E);
4554
4555 -- In case of class-wide interface object declarations we delay
4556 -- the generation of the equivalent record type declarations until
4557 -- its expansion because there are cases in they are not required.
4558
4559 elsif Is_Interface (T) then
4560 null;
4561
4562 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4563 -- we should prevent the generation of another Itype with the
4564 -- same name as the one already generated, or we end up with
4565 -- two identical types in GNATprove.
4566
4567 elsif GNATprove_Mode then
4568 null;
4569
4570 -- If the type is an unchecked union, no subtype can be built from
4571 -- the expression. Rewrite declaration as a renaming, which the
4572 -- back-end can handle properly. This is a rather unusual case,
4573 -- because most unchecked_union declarations have default values
4574 -- for discriminants and are thus not indefinite.
4575
4576 elsif Is_Unchecked_Union (T) then
4577 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4578 Set_Ekind (Id, E_Constant);
4579 else
4580 Set_Ekind (Id, E_Variable);
4581 end if;
4582
4583 Rewrite (N,
4584 Make_Object_Renaming_Declaration (Loc,
4585 Defining_Identifier => Id,
4586 Subtype_Mark => New_Occurrence_Of (T, Loc),
4587 Name => E));
4588
4589 Set_Renamed_Object (Id, E);
4590 Freeze_Before (N, T);
4591 Set_Is_Frozen (Id);
4592 goto Leave;
4593
4594 else
4595 -- Ensure that the generated subtype has a unique external name
4596 -- when the related object is public. This guarantees that the
4597 -- subtype and its bounds will not be affected by switches or
4598 -- pragmas that may offset the internal counter due to extra
4599 -- generated code.
4600
4601 if Is_Public (Id) then
4602 Related_Id := Id;
4603 else
4604 Related_Id := Empty;
4605 end if;
4606
4607 Expand_Subtype_From_Expr
4608 (N => N,
4609 Unc_Type => T,
4610 Subtype_Indic => Object_Definition (N),
4611 Exp => E,
4612 Related_Id => Related_Id);
4613
4614 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4615 end if;
4616
4617 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4618
4619 if Aliased_Present (N) then
4620 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4621 end if;
4622
4623 Freeze_Before (N, Act_T);
4624 Freeze_Before (N, T);
4625 end if;
4626
4627 elsif Is_Array_Type (T)
4628 and then No_Initialization (N)
4629 and then (Nkind (Original_Node (E)) = N_Aggregate
4630 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4631 and then Nkind (Original_Node (Expression
4632 (Original_Node (E)))) = N_Aggregate))
4633 then
4634 if not Is_Entity_Name (Object_Definition (N)) then
4635 Act_T := Etype (E);
4636 Check_Compile_Time_Size (Act_T);
4637
4638 if Aliased_Present (N) then
4639 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4640 end if;
4641 end if;
4642
4643 -- When the given object definition and the aggregate are specified
4644 -- independently, and their lengths might differ do a length check.
4645 -- This cannot happen if the aggregate is of the form (others =>...)
4646
4647 if not Is_Constrained (T) then
4648 null;
4649
4650 elsif Nkind (E) = N_Raise_Constraint_Error then
4651
4652 -- Aggregate is statically illegal. Place back in declaration
4653
4654 Set_Expression (N, E);
4655 Set_No_Initialization (N, False);
4656
4657 elsif T = Etype (E) then
4658 null;
4659
4660 elsif Nkind (E) = N_Aggregate
4661 and then Present (Component_Associations (E))
4662 and then Present (Choice_List (First (Component_Associations (E))))
4663 and then
4664 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4665 N_Others_Choice
4666 then
4667 null;
4668
4669 else
4670 Apply_Length_Check (E, T);
4671 end if;
4672
4673 -- If the type is limited unconstrained with defaulted discriminants and
4674 -- there is no expression, then the object is constrained by the
4675 -- defaults, so it is worthwhile building the corresponding subtype.
4676
4677 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4678 and then not Is_Constrained (T)
4679 and then Has_Discriminants (T)
4680 then
4681 if No (E) then
4682 Act_T := Build_Default_Subtype (T, N);
4683 else
4684 -- Ada 2005: A limited object may be initialized by means of an
4685 -- aggregate. If the type has default discriminants it has an
4686 -- unconstrained nominal type, Its actual subtype will be obtained
4687 -- from the aggregate, and not from the default discriminants.
4688
4689 Act_T := Etype (E);
4690 end if;
4691
4692 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4693
4694 elsif Nkind (E) = N_Function_Call
4695 and then Constant_Present (N)
4696 and then Has_Unconstrained_Elements (Etype (E))
4697 then
4698 -- The back-end has problems with constants of a discriminated type
4699 -- with defaults, if the initial value is a function call. We
4700 -- generate an intermediate temporary that will receive a reference
4701 -- to the result of the call. The initialization expression then
4702 -- becomes a dereference of that temporary.
4703
4704 Remove_Side_Effects (E);
4705
4706 -- If this is a constant declaration of an unconstrained type and
4707 -- the initialization is an aggregate, we can use the subtype of the
4708 -- aggregate for the declared entity because it is immutable.
4709
4710 elsif not Is_Constrained (T)
4711 and then Has_Discriminants (T)
4712 and then Constant_Present (N)
4713 and then not Has_Unchecked_Union (T)
4714 and then Nkind (E) = N_Aggregate
4715 then
4716 Act_T := Etype (E);
4717 end if;
4718
4719 -- Check No_Wide_Characters restriction
4720
4721 Check_Wide_Character_Restriction (T, Object_Definition (N));
4722
4723 -- Indicate this is not set in source. Certainly true for constants, and
4724 -- true for variables so far (will be reset for a variable if and when
4725 -- we encounter a modification in the source).
4726
4727 Set_Never_Set_In_Source (Id);
4728
4729 -- Now establish the proper kind and type of the object
4730
4731 if Constant_Present (N) then
4732 Set_Ekind (Id, E_Constant);
4733 Set_Is_True_Constant (Id);
4734
4735 else
4736 Set_Ekind (Id, E_Variable);
4737
4738 -- A variable is set as shared passive if it appears in a shared
4739 -- passive package, and is at the outer level. This is not done for
4740 -- entities generated during expansion, because those are always
4741 -- manipulated locally.
4742
4743 if Is_Shared_Passive (Current_Scope)
4744 and then Is_Library_Level_Entity (Id)
4745 and then Comes_From_Source (Id)
4746 then
4747 Set_Is_Shared_Passive (Id);
4748 Check_Shared_Var (Id, T, N);
4749 end if;
4750
4751 -- Set Has_Initial_Value if initializing expression present. Note
4752 -- that if there is no initializing expression, we leave the state
4753 -- of this flag unchanged (usually it will be False, but notably in
4754 -- the case of exception choice variables, it will already be true).
4755
4756 if Present (E) then
4757 Set_Has_Initial_Value (Id);
4758 end if;
4759 end if;
4760
4761 -- Set the SPARK mode from the current context (may be overwritten later
4762 -- with explicit pragma).
4763
4764 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4765 Set_SPARK_Pragma_Inherited (Id);
4766
4767 -- Preserve relevant elaboration-related attributes of the context which
4768 -- are no longer available or very expensive to recompute once analysis,
4769 -- resolution, and expansion are over.
4770
4771 Mark_Elaboration_Attributes
4772 (N_Id => Id,
4773 Checks => True,
4774 Warnings => True);
4775
4776 -- Initialize alignment and size and capture alignment setting
4777
4778 Init_Alignment (Id);
4779 Init_Esize (Id);
4780 Set_Optimize_Alignment_Flags (Id);
4781
4782 -- Deal with aliased case
4783
4784 if Aliased_Present (N) then
4785 Set_Is_Aliased (Id);
4786
4787 -- If the object is aliased and the type is unconstrained with
4788 -- defaulted discriminants and there is no expression, then the
4789 -- object is constrained by the defaults, so it is worthwhile
4790 -- building the corresponding subtype.
4791
4792 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4793 -- unconstrained, then only establish an actual subtype if the
4794 -- nominal subtype is indefinite. In definite cases the object is
4795 -- unconstrained in Ada 2005.
4796
4797 if No (E)
4798 and then Is_Record_Type (T)
4799 and then not Is_Constrained (T)
4800 and then Has_Discriminants (T)
4801 and then (Ada_Version < Ada_2005
4802 or else not Is_Definite_Subtype (T))
4803 then
4804 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4805 end if;
4806 end if;
4807
4808 -- Now we can set the type of the object
4809
4810 Set_Etype (Id, Act_T);
4811
4812 -- Non-constant object is marked to be treated as volatile if type is
4813 -- volatile and we clear the Current_Value setting that may have been
4814 -- set above. Doing so for constants isn't required and might interfere
4815 -- with possible uses of the object as a static expression in contexts
4816 -- incompatible with volatility (e.g. as a case-statement alternative).
4817
4818 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4819 Set_Treat_As_Volatile (Id);
4820 Set_Current_Value (Id, Empty);
4821 end if;
4822
4823 -- Deal with controlled types
4824
4825 if Has_Controlled_Component (Etype (Id))
4826 or else Is_Controlled (Etype (Id))
4827 then
4828 if not Is_Library_Level_Entity (Id) then
4829 Check_Restriction (No_Nested_Finalization, N);
4830 else
4831 Validate_Controlled_Object (Id);
4832 end if;
4833 end if;
4834
4835 if Has_Task (Etype (Id)) then
4836 Check_Restriction (No_Tasking, N);
4837
4838 -- Deal with counting max tasks
4839
4840 -- Nothing to do if inside a generic
4841
4842 if Inside_A_Generic then
4843 null;
4844
4845 -- If library level entity, then count tasks
4846
4847 elsif Is_Library_Level_Entity (Id) then
4848 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4849
4850 -- If not library level entity, then indicate we don't know max
4851 -- tasks and also check task hierarchy restriction and blocking
4852 -- operation (since starting a task is definitely blocking).
4853
4854 else
4855 Check_Restriction (Max_Tasks, N);
4856 Check_Restriction (No_Task_Hierarchy, N);
4857 Check_Potentially_Blocking_Operation (N);
4858 end if;
4859
4860 -- A rather specialized test. If we see two tasks being declared
4861 -- of the same type in the same object declaration, and the task
4862 -- has an entry with an address clause, we know that program error
4863 -- will be raised at run time since we can't have two tasks with
4864 -- entries at the same address.
4865
4866 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4867 declare
4868 E : Entity_Id;
4869
4870 begin
4871 E := First_Entity (Etype (Id));
4872 while Present (E) loop
4873 if Ekind (E) = E_Entry
4874 and then Present (Get_Attribute_Definition_Clause
4875 (E, Attribute_Address))
4876 then
4877 Error_Msg_Warn := SPARK_Mode /= On;
4878 Error_Msg_N
4879 ("more than one task with same entry address<<", N);
4880 Error_Msg_N ("\Program_Error [<<", N);
4881 Insert_Action (N,
4882 Make_Raise_Program_Error (Loc,
4883 Reason => PE_Duplicated_Entry_Address));
4884 exit;
4885 end if;
4886
4887 Next_Entity (E);
4888 end loop;
4889 end;
4890 end if;
4891 end if;
4892
4893 -- Some simple constant-propagation: if the expression is a constant
4894 -- string initialized with a literal, share the literal. This avoids
4895 -- a run-time copy.
4896
4897 if Present (E)
4898 and then Is_Entity_Name (E)
4899 and then Ekind (Entity (E)) = E_Constant
4900 and then Base_Type (Etype (E)) = Standard_String
4901 then
4902 declare
4903 Val : constant Node_Id := Constant_Value (Entity (E));
4904 begin
4905 if Present (Val) and then Nkind (Val) = N_String_Literal then
4906 Rewrite (E, New_Copy (Val));
4907 end if;
4908 end;
4909 end if;
4910
4911 -- Another optimization: if the nominal subtype is unconstrained and
4912 -- the expression is a function call that returns an unconstrained
4913 -- type, rewrite the declaration as a renaming of the result of the
4914 -- call. The exceptions below are cases where the copy is expected,
4915 -- either by the back end (Aliased case) or by the semantics, as for
4916 -- initializing controlled types or copying tags for class-wide types.
4917
4918 if Present (E)
4919 and then Nkind (E) = N_Explicit_Dereference
4920 and then Nkind (Original_Node (E)) = N_Function_Call
4921 and then not Is_Library_Level_Entity (Id)
4922 and then not Is_Constrained (Underlying_Type (T))
4923 and then not Is_Aliased (Id)
4924 and then not Is_Class_Wide_Type (T)
4925 and then not Is_Controlled (T)
4926 and then not Has_Controlled_Component (Base_Type (T))
4927 and then Expander_Active
4928 then
4929 Rewrite (N,
4930 Make_Object_Renaming_Declaration (Loc,
4931 Defining_Identifier => Id,
4932 Access_Definition => Empty,
4933 Subtype_Mark => New_Occurrence_Of
4934 (Base_Type (Etype (Id)), Loc),
4935 Name => E));
4936
4937 Set_Renamed_Object (Id, E);
4938
4939 -- Force generation of debugging information for the constant and for
4940 -- the renamed function call.
4941
4942 Set_Debug_Info_Needed (Id);
4943 Set_Debug_Info_Needed (Entity (Prefix (E)));
4944 end if;
4945
4946 if Present (Prev_Entity)
4947 and then Is_Frozen (Prev_Entity)
4948 and then not Error_Posted (Id)
4949 then
4950 Error_Msg_N ("full constant declaration appears too late", N);
4951 end if;
4952
4953 Check_Eliminated (Id);
4954
4955 -- Deal with setting In_Private_Part flag if in private part
4956
4957 if Ekind (Scope (Id)) = E_Package
4958 and then In_Private_Part (Scope (Id))
4959 then
4960 Set_In_Private_Part (Id);
4961 end if;
4962
4963 <<Leave>>
4964 -- Initialize the refined state of a variable here because this is a
4965 -- common destination for legal and illegal object declarations.
4966
4967 if Ekind (Id) = E_Variable then
4968 Set_Encapsulating_State (Id, Empty);
4969 end if;
4970
4971 if Has_Aspects (N) then
4972 Analyze_Aspect_Specifications (N, Id);
4973 end if;
4974
4975 Analyze_Dimension (N);
4976
4977 -- Verify whether the object declaration introduces an illegal hidden
4978 -- state within a package subject to a null abstract state.
4979
4980 if Ekind (Id) = E_Variable then
4981 Check_No_Hidden_State (Id);
4982 end if;
4983
4984 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4985 end Analyze_Object_Declaration;
4986
4987 ---------------------------
4988 -- Analyze_Others_Choice --
4989 ---------------------------
4990
4991 -- Nothing to do for the others choice node itself, the semantic analysis
4992 -- of the others choice will occur as part of the processing of the parent
4993
4994 procedure Analyze_Others_Choice (N : Node_Id) is
4995 pragma Warnings (Off, N);
4996 begin
4997 null;
4998 end Analyze_Others_Choice;
4999
5000 -------------------------------------------
5001 -- Analyze_Private_Extension_Declaration --
5002 -------------------------------------------
5003
5004 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5005 Indic : constant Node_Id := Subtype_Indication (N);
5006 T : constant Entity_Id := Defining_Identifier (N);
5007 Iface : Entity_Id;
5008 Iface_Elmt : Elmt_Id;
5009 Parent_Base : Entity_Id;
5010 Parent_Type : Entity_Id;
5011
5012 begin
5013 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5014
5015 if Is_Non_Empty_List (Interface_List (N)) then
5016 declare
5017 Intf : Node_Id;
5018 T : Entity_Id;
5019
5020 begin
5021 Intf := First (Interface_List (N));
5022 while Present (Intf) loop
5023 T := Find_Type_Of_Subtype_Indic (Intf);
5024
5025 Diagnose_Interface (Intf, T);
5026 Next (Intf);
5027 end loop;
5028 end;
5029 end if;
5030
5031 Generate_Definition (T);
5032
5033 -- For other than Ada 2012, just enter the name in the current scope
5034
5035 if Ada_Version < Ada_2012 then
5036 Enter_Name (T);
5037
5038 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5039 -- case of private type that completes an incomplete type.
5040
5041 else
5042 declare
5043 Prev : Entity_Id;
5044
5045 begin
5046 Prev := Find_Type_Name (N);
5047
5048 pragma Assert (Prev = T
5049 or else (Ekind (Prev) = E_Incomplete_Type
5050 and then Present (Full_View (Prev))
5051 and then Full_View (Prev) = T));
5052 end;
5053 end if;
5054
5055 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5056 Parent_Base := Base_Type (Parent_Type);
5057
5058 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5059 Set_Ekind (T, Ekind (Parent_Type));
5060 Set_Etype (T, Any_Type);
5061 goto Leave;
5062
5063 elsif not Is_Tagged_Type (Parent_Type) then
5064 Error_Msg_N
5065 ("parent of type extension must be a tagged type ", Indic);
5066 goto Leave;
5067
5068 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5069 Error_Msg_N ("premature derivation of incomplete type", Indic);
5070 goto Leave;
5071
5072 elsif Is_Concurrent_Type (Parent_Type) then
5073 Error_Msg_N
5074 ("parent type of a private extension cannot be a synchronized "
5075 & "tagged type (RM 3.9.1 (3/1))", N);
5076
5077 Set_Etype (T, Any_Type);
5078 Set_Ekind (T, E_Limited_Private_Type);
5079 Set_Private_Dependents (T, New_Elmt_List);
5080 Set_Error_Posted (T);
5081 goto Leave;
5082 end if;
5083
5084 -- Perhaps the parent type should be changed to the class-wide type's
5085 -- specific type in this case to prevent cascading errors ???
5086
5087 if Is_Class_Wide_Type (Parent_Type) then
5088 Error_Msg_N
5089 ("parent of type extension must not be a class-wide type", Indic);
5090 goto Leave;
5091 end if;
5092
5093 if (not Is_Package_Or_Generic_Package (Current_Scope)
5094 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5095 or else In_Private_Part (Current_Scope)
5096 then
5097 Error_Msg_N ("invalid context for private extension", N);
5098 end if;
5099
5100 -- Set common attributes
5101
5102 Set_Is_Pure (T, Is_Pure (Current_Scope));
5103 Set_Scope (T, Current_Scope);
5104 Set_Ekind (T, E_Record_Type_With_Private);
5105 Init_Size_Align (T);
5106 Set_Default_SSO (T);
5107 Set_No_Reordering (T, No_Component_Reordering);
5108
5109 Set_Etype (T, Parent_Base);
5110 Propagate_Concurrent_Flags (T, Parent_Base);
5111
5112 Set_Convention (T, Convention (Parent_Type));
5113 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5114 Set_Is_First_Subtype (T);
5115 Make_Class_Wide_Type (T);
5116
5117 -- Set the SPARK mode from the current context
5118
5119 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5120 Set_SPARK_Pragma_Inherited (T);
5121
5122 if Unknown_Discriminants_Present (N) then
5123 Set_Discriminant_Constraint (T, No_Elist);
5124 end if;
5125
5126 Build_Derived_Record_Type (N, Parent_Type, T);
5127
5128 -- A private extension inherits the Default_Initial_Condition pragma
5129 -- coming from any parent type within the derivation chain.
5130
5131 if Has_DIC (Parent_Type) then
5132 Set_Has_Inherited_DIC (T);
5133 end if;
5134
5135 -- A private extension inherits any class-wide invariants coming from a
5136 -- parent type or an interface. Note that the invariant procedure of the
5137 -- parent type should not be inherited because the private extension may
5138 -- define invariants of its own.
5139
5140 if Has_Inherited_Invariants (Parent_Type)
5141 or else Has_Inheritable_Invariants (Parent_Type)
5142 then
5143 Set_Has_Inherited_Invariants (T);
5144
5145 elsif Present (Interfaces (T)) then
5146 Iface_Elmt := First_Elmt (Interfaces (T));
5147 while Present (Iface_Elmt) loop
5148 Iface := Node (Iface_Elmt);
5149
5150 if Has_Inheritable_Invariants (Iface) then
5151 Set_Has_Inherited_Invariants (T);
5152 exit;
5153 end if;
5154
5155 Next_Elmt (Iface_Elmt);
5156 end loop;
5157 end if;
5158
5159 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5160 -- synchronized formal derived type.
5161
5162 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5163 Set_Is_Limited_Record (T);
5164
5165 -- Formal derived type case
5166
5167 if Is_Generic_Type (T) then
5168
5169 -- The parent must be a tagged limited type or a synchronized
5170 -- interface.
5171
5172 if (not Is_Tagged_Type (Parent_Type)
5173 or else not Is_Limited_Type (Parent_Type))
5174 and then
5175 (not Is_Interface (Parent_Type)
5176 or else not Is_Synchronized_Interface (Parent_Type))
5177 then
5178 Error_Msg_NE
5179 ("parent type of & must be tagged limited or synchronized",
5180 N, T);
5181 end if;
5182
5183 -- The progenitors (if any) must be limited or synchronized
5184 -- interfaces.
5185
5186 if Present (Interfaces (T)) then
5187 Iface_Elmt := First_Elmt (Interfaces (T));
5188 while Present (Iface_Elmt) loop
5189 Iface := Node (Iface_Elmt);
5190
5191 if not Is_Limited_Interface (Iface)
5192 and then not Is_Synchronized_Interface (Iface)
5193 then
5194 Error_Msg_NE
5195 ("progenitor & must be limited or synchronized",
5196 N, Iface);
5197 end if;
5198
5199 Next_Elmt (Iface_Elmt);
5200 end loop;
5201 end if;
5202
5203 -- Regular derived extension, the parent must be a limited or
5204 -- synchronized interface.
5205
5206 else
5207 if not Is_Interface (Parent_Type)
5208 or else (not Is_Limited_Interface (Parent_Type)
5209 and then not Is_Synchronized_Interface (Parent_Type))
5210 then
5211 Error_Msg_NE
5212 ("parent type of & must be limited interface", N, T);
5213 end if;
5214 end if;
5215
5216 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5217 -- extension with a synchronized parent must be explicitly declared
5218 -- synchronized, because the full view will be a synchronized type.
5219 -- This must be checked before the check for limited types below,
5220 -- to ensure that types declared limited are not allowed to extend
5221 -- synchronized interfaces.
5222
5223 elsif Is_Interface (Parent_Type)
5224 and then Is_Synchronized_Interface (Parent_Type)
5225 and then not Synchronized_Present (N)
5226 then
5227 Error_Msg_NE
5228 ("private extension of& must be explicitly synchronized",
5229 N, Parent_Type);
5230
5231 elsif Limited_Present (N) then
5232 Set_Is_Limited_Record (T);
5233
5234 if not Is_Limited_Type (Parent_Type)
5235 and then
5236 (not Is_Interface (Parent_Type)
5237 or else not Is_Limited_Interface (Parent_Type))
5238 then
5239 Error_Msg_NE ("parent type& of limited extension must be limited",
5240 N, Parent_Type);
5241 end if;
5242 end if;
5243
5244 -- Remember that its parent type has a private extension. Used to warn
5245 -- on public primitives of the parent type defined after its private
5246 -- extensions (see Check_Dispatching_Operation).
5247
5248 Set_Has_Private_Extension (Parent_Type);
5249
5250 <<Leave>>
5251 if Has_Aspects (N) then
5252 Analyze_Aspect_Specifications (N, T);
5253 end if;
5254 end Analyze_Private_Extension_Declaration;
5255
5256 ---------------------------------
5257 -- Analyze_Subtype_Declaration --
5258 ---------------------------------
5259
5260 procedure Analyze_Subtype_Declaration
5261 (N : Node_Id;
5262 Skip : Boolean := False)
5263 is
5264 Id : constant Entity_Id := Defining_Identifier (N);
5265 R_Checks : Check_Result;
5266 T : Entity_Id;
5267
5268 begin
5269 Generate_Definition (Id);
5270 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5271 Init_Size_Align (Id);
5272
5273 -- The following guard condition on Enter_Name is to handle cases where
5274 -- the defining identifier has already been entered into the scope but
5275 -- the declaration as a whole needs to be analyzed.
5276
5277 -- This case in particular happens for derived enumeration types. The
5278 -- derived enumeration type is processed as an inserted enumeration type
5279 -- declaration followed by a rewritten subtype declaration. The defining
5280 -- identifier, however, is entered into the name scope very early in the
5281 -- processing of the original type declaration and therefore needs to be
5282 -- avoided here, when the created subtype declaration is analyzed. (See
5283 -- Build_Derived_Types)
5284
5285 -- This also happens when the full view of a private type is derived
5286 -- type with constraints. In this case the entity has been introduced
5287 -- in the private declaration.
5288
5289 -- Finally this happens in some complex cases when validity checks are
5290 -- enabled, where the same subtype declaration may be analyzed twice.
5291 -- This can happen if the subtype is created by the pre-analysis of
5292 -- an attribute tht gives the range of a loop statement, and the loop
5293 -- itself appears within an if_statement that will be rewritten during
5294 -- expansion.
5295
5296 if Skip
5297 or else (Present (Etype (Id))
5298 and then (Is_Private_Type (Etype (Id))
5299 or else Is_Task_Type (Etype (Id))
5300 or else Is_Rewrite_Substitution (N)))
5301 then
5302 null;
5303
5304 elsif Current_Entity (Id) = Id then
5305 null;
5306
5307 else
5308 Enter_Name (Id);
5309 end if;
5310
5311 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5312
5313 -- Class-wide equivalent types of records with unknown discriminants
5314 -- involve the generation of an itype which serves as the private view
5315 -- of a constrained record subtype. In such cases the base type of the
5316 -- current subtype we are processing is the private itype. Use the full
5317 -- of the private itype when decorating various attributes.
5318
5319 if Is_Itype (T)
5320 and then Is_Private_Type (T)
5321 and then Present (Full_View (T))
5322 then
5323 T := Full_View (T);
5324 end if;
5325
5326 -- Inherit common attributes
5327
5328 Set_Is_Volatile (Id, Is_Volatile (T));
5329 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5330 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5331 Set_Convention (Id, Convention (T));
5332
5333 -- If ancestor has predicates then so does the subtype, and in addition
5334 -- we must delay the freeze to properly arrange predicate inheritance.
5335
5336 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5337 -- in which T = ID, so the above tests and assignments do nothing???
5338
5339 if Has_Predicates (T)
5340 or else (Present (Ancestor_Subtype (T))
5341 and then Has_Predicates (Ancestor_Subtype (T)))
5342 then
5343 Set_Has_Predicates (Id);
5344 Set_Has_Delayed_Freeze (Id);
5345
5346 -- Generated subtypes inherit the predicate function from the parent
5347 -- (no aspects to examine on the generated declaration).
5348
5349 if not Comes_From_Source (N) then
5350 Set_Ekind (Id, Ekind (T));
5351
5352 if Present (Predicate_Function (Id)) then
5353 null;
5354
5355 elsif Present (Predicate_Function (T)) then
5356 Set_Predicate_Function (Id, Predicate_Function (T));
5357
5358 elsif Present (Ancestor_Subtype (T))
5359 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5360 then
5361 Set_Predicate_Function (Id,
5362 Predicate_Function (Ancestor_Subtype (T)));
5363 end if;
5364 end if;
5365 end if;
5366
5367 -- Subtype of Boolean cannot have a constraint in SPARK
5368
5369 if Is_Boolean_Type (T)
5370 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5371 then
5372 Check_SPARK_05_Restriction
5373 ("subtype of Boolean cannot have constraint", N);
5374 end if;
5375
5376 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5377 declare
5378 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5379 One_Cstr : Node_Id;
5380 Low : Node_Id;
5381 High : Node_Id;
5382
5383 begin
5384 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5385 One_Cstr := First (Constraints (Cstr));
5386 while Present (One_Cstr) loop
5387
5388 -- Index or discriminant constraint in SPARK must be a
5389 -- subtype mark.
5390
5391 if not
5392 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5393 then
5394 Check_SPARK_05_Restriction
5395 ("subtype mark required", One_Cstr);
5396
5397 -- String subtype must have a lower bound of 1 in SPARK.
5398 -- Note that we do not need to test for the non-static case
5399 -- here, since that was already taken care of in
5400 -- Process_Range_Expr_In_Decl.
5401
5402 elsif Base_Type (T) = Standard_String then
5403 Get_Index_Bounds (One_Cstr, Low, High);
5404
5405 if Is_OK_Static_Expression (Low)
5406 and then Expr_Value (Low) /= 1
5407 then
5408 Check_SPARK_05_Restriction
5409 ("String subtype must have lower bound of 1", N);
5410 end if;
5411 end if;
5412
5413 Next (One_Cstr);
5414 end loop;
5415 end if;
5416 end;
5417 end if;
5418
5419 -- In the case where there is no constraint given in the subtype
5420 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5421 -- semantic attributes must be established here.
5422
5423 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5424 Set_Etype (Id, Base_Type (T));
5425
5426 -- Subtype of unconstrained array without constraint is not allowed
5427 -- in SPARK.
5428
5429 if Is_Array_Type (T) and then not Is_Constrained (T) then
5430 Check_SPARK_05_Restriction
5431 ("subtype of unconstrained array must have constraint", N);
5432 end if;
5433
5434 case Ekind (T) is
5435 when Array_Kind =>
5436 Set_Ekind (Id, E_Array_Subtype);
5437 Copy_Array_Subtype_Attributes (Id, T);
5438
5439 when Decimal_Fixed_Point_Kind =>
5440 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5441 Set_Digits_Value (Id, Digits_Value (T));
5442 Set_Delta_Value (Id, Delta_Value (T));
5443 Set_Scale_Value (Id, Scale_Value (T));
5444 Set_Small_Value (Id, Small_Value (T));
5445 Set_Scalar_Range (Id, Scalar_Range (T));
5446 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5447 Set_Is_Constrained (Id, Is_Constrained (T));
5448 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5449 Set_RM_Size (Id, RM_Size (T));
5450
5451 when Enumeration_Kind =>
5452 Set_Ekind (Id, E_Enumeration_Subtype);
5453 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5454 Set_Scalar_Range (Id, Scalar_Range (T));
5455 Set_Is_Character_Type (Id, Is_Character_Type (T));
5456 Set_Is_Constrained (Id, Is_Constrained (T));
5457 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5458 Set_RM_Size (Id, RM_Size (T));
5459
5460 when Ordinary_Fixed_Point_Kind =>
5461 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5462 Set_Scalar_Range (Id, Scalar_Range (T));
5463 Set_Small_Value (Id, Small_Value (T));
5464 Set_Delta_Value (Id, Delta_Value (T));
5465 Set_Is_Constrained (Id, Is_Constrained (T));
5466 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5467 Set_RM_Size (Id, RM_Size (T));
5468
5469 when Float_Kind =>
5470 Set_Ekind (Id, E_Floating_Point_Subtype);
5471 Set_Scalar_Range (Id, Scalar_Range (T));
5472 Set_Digits_Value (Id, Digits_Value (T));
5473 Set_Is_Constrained (Id, Is_Constrained (T));
5474
5475 -- If the floating point type has dimensions, these will be
5476 -- inherited subsequently when Analyze_Dimensions is called.
5477
5478 when Signed_Integer_Kind =>
5479 Set_Ekind (Id, E_Signed_Integer_Subtype);
5480 Set_Scalar_Range (Id, Scalar_Range (T));
5481 Set_Is_Constrained (Id, Is_Constrained (T));
5482 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5483 Set_RM_Size (Id, RM_Size (T));
5484
5485 when Modular_Integer_Kind =>
5486 Set_Ekind (Id, E_Modular_Integer_Subtype);
5487 Set_Scalar_Range (Id, Scalar_Range (T));
5488 Set_Is_Constrained (Id, Is_Constrained (T));
5489 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5490 Set_RM_Size (Id, RM_Size (T));
5491
5492 when Class_Wide_Kind =>
5493 Set_Ekind (Id, E_Class_Wide_Subtype);
5494 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5495 Set_Cloned_Subtype (Id, T);
5496 Set_Is_Tagged_Type (Id, True);
5497 Set_Has_Unknown_Discriminants
5498 (Id, True);
5499 Set_No_Tagged_Streams_Pragma
5500 (Id, No_Tagged_Streams_Pragma (T));
5501
5502 if Ekind (T) = E_Class_Wide_Subtype then
5503 Set_Equivalent_Type (Id, Equivalent_Type (T));
5504 end if;
5505
5506 when E_Record_Subtype
5507 | E_Record_Type
5508 =>
5509 Set_Ekind (Id, E_Record_Subtype);
5510
5511 if Ekind (T) = E_Record_Subtype
5512 and then Present (Cloned_Subtype (T))
5513 then
5514 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5515 else
5516 Set_Cloned_Subtype (Id, T);
5517 end if;
5518
5519 Set_First_Entity (Id, First_Entity (T));
5520 Set_Last_Entity (Id, Last_Entity (T));
5521 Set_Has_Discriminants (Id, Has_Discriminants (T));
5522 Set_Is_Constrained (Id, Is_Constrained (T));
5523 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5524 Set_Has_Implicit_Dereference
5525 (Id, Has_Implicit_Dereference (T));
5526 Set_Has_Unknown_Discriminants
5527 (Id, Has_Unknown_Discriminants (T));
5528
5529 if Has_Discriminants (T) then
5530 Set_Discriminant_Constraint
5531 (Id, Discriminant_Constraint (T));
5532 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5533
5534 elsif Has_Unknown_Discriminants (Id) then
5535 Set_Discriminant_Constraint (Id, No_Elist);
5536 end if;
5537
5538 if Is_Tagged_Type (T) then
5539 Set_Is_Tagged_Type (Id, True);
5540 Set_No_Tagged_Streams_Pragma
5541 (Id, No_Tagged_Streams_Pragma (T));
5542 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5543 Set_Direct_Primitive_Operations
5544 (Id, Direct_Primitive_Operations (T));
5545 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5546
5547 if Is_Interface (T) then
5548 Set_Is_Interface (Id);
5549 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5550 end if;
5551 end if;
5552
5553 when Private_Kind =>
5554 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5555 Set_Has_Discriminants (Id, Has_Discriminants (T));
5556 Set_Is_Constrained (Id, Is_Constrained (T));
5557 Set_First_Entity (Id, First_Entity (T));
5558 Set_Last_Entity (Id, Last_Entity (T));
5559 Set_Private_Dependents (Id, New_Elmt_List);
5560 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5561 Set_Has_Implicit_Dereference
5562 (Id, Has_Implicit_Dereference (T));
5563 Set_Has_Unknown_Discriminants
5564 (Id, Has_Unknown_Discriminants (T));
5565 Set_Known_To_Have_Preelab_Init
5566 (Id, Known_To_Have_Preelab_Init (T));
5567
5568 if Is_Tagged_Type (T) then
5569 Set_Is_Tagged_Type (Id);
5570 Set_No_Tagged_Streams_Pragma (Id,
5571 No_Tagged_Streams_Pragma (T));
5572 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5573 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5574 Set_Direct_Primitive_Operations (Id,
5575 Direct_Primitive_Operations (T));
5576 end if;
5577
5578 -- In general the attributes of the subtype of a private type
5579 -- are the attributes of the partial view of parent. However,
5580 -- the full view may be a discriminated type, and the subtype
5581 -- must share the discriminant constraint to generate correct
5582 -- calls to initialization procedures.
5583
5584 if Has_Discriminants (T) then
5585 Set_Discriminant_Constraint
5586 (Id, Discriminant_Constraint (T));
5587 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5588
5589 elsif Present (Full_View (T))
5590 and then Has_Discriminants (Full_View (T))
5591 then
5592 Set_Discriminant_Constraint
5593 (Id, Discriminant_Constraint (Full_View (T)));
5594 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5595
5596 -- This would seem semantically correct, but apparently
5597 -- generates spurious errors about missing components ???
5598
5599 -- Set_Has_Discriminants (Id);
5600 end if;
5601
5602 Prepare_Private_Subtype_Completion (Id, N);
5603
5604 -- If this is the subtype of a constrained private type with
5605 -- discriminants that has got a full view and we also have
5606 -- built a completion just above, show that the completion
5607 -- is a clone of the full view to the back-end.
5608
5609 if Has_Discriminants (T)
5610 and then not Has_Unknown_Discriminants (T)
5611 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5612 and then Present (Full_View (T))
5613 and then Present (Full_View (Id))
5614 then
5615 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5616 end if;
5617
5618 when Access_Kind =>
5619 Set_Ekind (Id, E_Access_Subtype);
5620 Set_Is_Constrained (Id, Is_Constrained (T));
5621 Set_Is_Access_Constant
5622 (Id, Is_Access_Constant (T));
5623 Set_Directly_Designated_Type
5624 (Id, Designated_Type (T));
5625 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5626
5627 -- A Pure library_item must not contain the declaration of a
5628 -- named access type, except within a subprogram, generic
5629 -- subprogram, task unit, or protected unit, or if it has
5630 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5631
5632 if Comes_From_Source (Id)
5633 and then In_Pure_Unit
5634 and then not In_Subprogram_Task_Protected_Unit
5635 and then not No_Pool_Assigned (Id)
5636 then
5637 Error_Msg_N
5638 ("named access types not allowed in pure unit", N);
5639 end if;
5640
5641 when Concurrent_Kind =>
5642 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5643 Set_Corresponding_Record_Type (Id,
5644 Corresponding_Record_Type (T));
5645 Set_First_Entity (Id, First_Entity (T));
5646 Set_First_Private_Entity (Id, First_Private_Entity (T));
5647 Set_Has_Discriminants (Id, Has_Discriminants (T));
5648 Set_Is_Constrained (Id, Is_Constrained (T));
5649 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5650 Set_Last_Entity (Id, Last_Entity (T));
5651
5652 if Is_Tagged_Type (T) then
5653 Set_No_Tagged_Streams_Pragma
5654 (Id, No_Tagged_Streams_Pragma (T));
5655 end if;
5656
5657 if Has_Discriminants (T) then
5658 Set_Discriminant_Constraint
5659 (Id, Discriminant_Constraint (T));
5660 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5661 end if;
5662
5663 when Incomplete_Kind =>
5664 if Ada_Version >= Ada_2005 then
5665
5666 -- In Ada 2005 an incomplete type can be explicitly tagged:
5667 -- propagate indication. Note that we also have to include
5668 -- subtypes for Ada 2012 extended use of incomplete types.
5669
5670 Set_Ekind (Id, E_Incomplete_Subtype);
5671 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5672 Set_Private_Dependents (Id, New_Elmt_List);
5673
5674 if Is_Tagged_Type (Id) then
5675 Set_No_Tagged_Streams_Pragma
5676 (Id, No_Tagged_Streams_Pragma (T));
5677 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5678 end if;
5679
5680 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5681 -- incomplete type visible through a limited with clause.
5682
5683 if From_Limited_With (T)
5684 and then Present (Non_Limited_View (T))
5685 then
5686 Set_From_Limited_With (Id);
5687 Set_Non_Limited_View (Id, Non_Limited_View (T));
5688
5689 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5690 -- to the private dependents of the original incomplete
5691 -- type for future transformation.
5692
5693 else
5694 Append_Elmt (Id, Private_Dependents (T));
5695 end if;
5696
5697 -- If the subtype name denotes an incomplete type an error
5698 -- was already reported by Process_Subtype.
5699
5700 else
5701 Set_Etype (Id, Any_Type);
5702 end if;
5703
5704 when others =>
5705 raise Program_Error;
5706 end case;
5707
5708 -- If there is no constraint in the subtype indication, the
5709 -- declared entity inherits predicates from the parent.
5710
5711 Inherit_Predicate_Flags (Id, T);
5712 end if;
5713
5714 if Etype (Id) = Any_Type then
5715 goto Leave;
5716 end if;
5717
5718 -- Some common processing on all types
5719
5720 Set_Size_Info (Id, T);
5721 Set_First_Rep_Item (Id, First_Rep_Item (T));
5722
5723 -- If the parent type is a generic actual, so is the subtype. This may
5724 -- happen in a nested instance. Why Comes_From_Source test???
5725
5726 if not Comes_From_Source (N) then
5727 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5728 end if;
5729
5730 -- If this is a subtype declaration for an actual in an instance,
5731 -- inherit static and dynamic predicates if any.
5732
5733 -- If declaration has no aspect specifications, inherit predicate
5734 -- info as well. Unclear how to handle the case of both specified
5735 -- and inherited predicates ??? Other inherited aspects, such as
5736 -- invariants, should be OK, but the combination with later pragmas
5737 -- may also require special merging.
5738
5739 if Has_Predicates (T)
5740 and then Present (Predicate_Function (T))
5741 and then
5742 ((In_Instance and then not Comes_From_Source (N))
5743 or else No (Aspect_Specifications (N)))
5744 then
5745 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5746
5747 if Has_Static_Predicate (T) then
5748 Set_Has_Static_Predicate (Id);
5749 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5750 end if;
5751 end if;
5752
5753 -- Remaining processing depends on characteristics of base type
5754
5755 T := Etype (Id);
5756
5757 Set_Is_Immediately_Visible (Id, True);
5758 Set_Depends_On_Private (Id, Has_Private_Component (T));
5759 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5760
5761 if Is_Interface (T) then
5762 Set_Is_Interface (Id);
5763 end if;
5764
5765 if Present (Generic_Parent_Type (N))
5766 and then
5767 (Nkind (Parent (Generic_Parent_Type (N))) /=
5768 N_Formal_Type_Declaration
5769 or else Nkind (Formal_Type_Definition
5770 (Parent (Generic_Parent_Type (N)))) /=
5771 N_Formal_Private_Type_Definition)
5772 then
5773 if Is_Tagged_Type (Id) then
5774
5775 -- If this is a generic actual subtype for a synchronized type,
5776 -- the primitive operations are those of the corresponding record
5777 -- for which there is a separate subtype declaration.
5778
5779 if Is_Concurrent_Type (Id) then
5780 null;
5781 elsif Is_Class_Wide_Type (Id) then
5782 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5783 else
5784 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5785 end if;
5786
5787 elsif Scope (Etype (Id)) /= Standard_Standard then
5788 Derive_Subprograms (Generic_Parent_Type (N), Id);
5789 end if;
5790 end if;
5791
5792 if Is_Private_Type (T) and then Present (Full_View (T)) then
5793 Conditional_Delay (Id, Full_View (T));
5794
5795 -- The subtypes of components or subcomponents of protected types
5796 -- do not need freeze nodes, which would otherwise appear in the
5797 -- wrong scope (before the freeze node for the protected type). The
5798 -- proper subtypes are those of the subcomponents of the corresponding
5799 -- record.
5800
5801 elsif Ekind (Scope (Id)) /= E_Protected_Type
5802 and then Present (Scope (Scope (Id))) -- error defense
5803 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5804 then
5805 Conditional_Delay (Id, T);
5806 end if;
5807
5808 -- If we have a subtype of an incomplete type whose full type is a
5809 -- derived numeric type, we need to have a freeze node for the subtype.
5810 -- Otherwise gigi will complain while computing the (static) bounds of
5811 -- the subtype.
5812
5813 if Is_Itype (T)
5814 and then Is_Elementary_Type (Id)
5815 and then Etype (Id) /= Id
5816 then
5817 declare
5818 Partial : constant Entity_Id :=
5819 Incomplete_Or_Partial_View (First_Subtype (Id));
5820 begin
5821 if Present (Partial)
5822 and then Ekind (Partial) = E_Incomplete_Type
5823 then
5824 Set_Has_Delayed_Freeze (Id);
5825 end if;
5826 end;
5827 end if;
5828
5829 -- Check that Constraint_Error is raised for a scalar subtype indication
5830 -- when the lower or upper bound of a non-null range lies outside the
5831 -- range of the type mark.
5832
5833 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5834 if Is_Scalar_Type (Etype (Id))
5835 and then Scalar_Range (Id) /=
5836 Scalar_Range
5837 (Etype (Subtype_Mark (Subtype_Indication (N))))
5838 then
5839 Apply_Range_Check
5840 (Scalar_Range (Id),
5841 Etype (Subtype_Mark (Subtype_Indication (N))));
5842
5843 -- In the array case, check compatibility for each index
5844
5845 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5846 then
5847 -- This really should be a subprogram that finds the indications
5848 -- to check???
5849
5850 declare
5851 Subt_Index : Node_Id := First_Index (Id);
5852 Target_Index : Node_Id :=
5853 First_Index (Etype
5854 (Subtype_Mark (Subtype_Indication (N))));
5855 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5856
5857 begin
5858 while Present (Subt_Index) loop
5859 if ((Nkind (Subt_Index) = N_Identifier
5860 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5861 or else Nkind (Subt_Index) = N_Subtype_Indication)
5862 and then
5863 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5864 then
5865 declare
5866 Target_Typ : constant Entity_Id :=
5867 Etype (Target_Index);
5868 begin
5869 R_Checks :=
5870 Get_Range_Checks
5871 (Scalar_Range (Etype (Subt_Index)),
5872 Target_Typ,
5873 Etype (Subt_Index),
5874 Defining_Identifier (N));
5875
5876 -- Reset Has_Dynamic_Range_Check on the subtype to
5877 -- prevent elision of the index check due to a dynamic
5878 -- check generated for a preceding index (needed since
5879 -- Insert_Range_Checks tries to avoid generating
5880 -- redundant checks on a given declaration).
5881
5882 Set_Has_Dynamic_Range_Check (N, False);
5883
5884 Insert_Range_Checks
5885 (R_Checks,
5886 N,
5887 Target_Typ,
5888 Sloc (Defining_Identifier (N)));
5889
5890 -- Record whether this index involved a dynamic check
5891
5892 Has_Dyn_Chk :=
5893 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5894 end;
5895 end if;
5896
5897 Next_Index (Subt_Index);
5898 Next_Index (Target_Index);
5899 end loop;
5900
5901 -- Finally, mark whether the subtype involves dynamic checks
5902
5903 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5904 end;
5905 end if;
5906 end if;
5907
5908 Set_Optimize_Alignment_Flags (Id);
5909 Check_Eliminated (Id);
5910
5911 <<Leave>>
5912 if Has_Aspects (N) then
5913 Analyze_Aspect_Specifications (N, Id);
5914 end if;
5915
5916 Analyze_Dimension (N);
5917
5918 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5919 -- indications on composite types where the constraints are dynamic.
5920 -- Note that object declarations and aggregates generate implicit
5921 -- subtype declarations, which this covers. One special case is that the
5922 -- implicitly generated "=" for discriminated types includes an
5923 -- offending subtype declaration, which is harmless, so we ignore it
5924 -- here.
5925
5926 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5927 declare
5928 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5929 begin
5930 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5931 and then not (Is_Internal (Id)
5932 and then Is_TSS (Scope (Id),
5933 TSS_Composite_Equality))
5934 and then not Within_Init_Proc
5935 and then not All_Composite_Constraints_Static (Cstr)
5936 then
5937 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5938 end if;
5939 end;
5940 end if;
5941 end Analyze_Subtype_Declaration;
5942
5943 --------------------------------
5944 -- Analyze_Subtype_Indication --
5945 --------------------------------
5946
5947 procedure Analyze_Subtype_Indication (N : Node_Id) is
5948 T : constant Entity_Id := Subtype_Mark (N);
5949 R : constant Node_Id := Range_Expression (Constraint (N));
5950
5951 begin
5952 Analyze (T);
5953
5954 if R /= Error then
5955 Analyze (R);
5956 Set_Etype (N, Etype (R));
5957 Resolve (R, Entity (T));
5958 else
5959 Set_Error_Posted (R);
5960 Set_Error_Posted (T);
5961 end if;
5962 end Analyze_Subtype_Indication;
5963
5964 --------------------------
5965 -- Analyze_Variant_Part --
5966 --------------------------
5967
5968 procedure Analyze_Variant_Part (N : Node_Id) is
5969 Discr_Name : Node_Id;
5970 Discr_Type : Entity_Id;
5971
5972 procedure Process_Variant (A : Node_Id);
5973 -- Analyze declarations for a single variant
5974
5975 package Analyze_Variant_Choices is
5976 new Generic_Analyze_Choices (Process_Variant);
5977 use Analyze_Variant_Choices;
5978
5979 ---------------------
5980 -- Process_Variant --
5981 ---------------------
5982
5983 procedure Process_Variant (A : Node_Id) is
5984 CL : constant Node_Id := Component_List (A);
5985 begin
5986 if not Null_Present (CL) then
5987 Analyze_Declarations (Component_Items (CL));
5988
5989 if Present (Variant_Part (CL)) then
5990 Analyze (Variant_Part (CL));
5991 end if;
5992 end if;
5993 end Process_Variant;
5994
5995 -- Start of processing for Analyze_Variant_Part
5996
5997 begin
5998 Discr_Name := Name (N);
5999 Analyze (Discr_Name);
6000
6001 -- If Discr_Name bad, get out (prevent cascaded errors)
6002
6003 if Etype (Discr_Name) = Any_Type then
6004 return;
6005 end if;
6006
6007 -- Check invalid discriminant in variant part
6008
6009 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6010 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6011 end if;
6012
6013 Discr_Type := Etype (Entity (Discr_Name));
6014
6015 if not Is_Discrete_Type (Discr_Type) then
6016 Error_Msg_N
6017 ("discriminant in a variant part must be of a discrete type",
6018 Name (N));
6019 return;
6020 end if;
6021
6022 -- Now analyze the choices, which also analyzes the declarations that
6023 -- are associated with each choice.
6024
6025 Analyze_Choices (Variants (N), Discr_Type);
6026
6027 -- Note: we used to instantiate and call Check_Choices here to check
6028 -- that the choices covered the discriminant, but it's too early to do
6029 -- that because of statically predicated subtypes, whose analysis may
6030 -- be deferred to their freeze point which may be as late as the freeze
6031 -- point of the containing record. So this call is now to be found in
6032 -- Freeze_Record_Declaration.
6033
6034 end Analyze_Variant_Part;
6035
6036 ----------------------------
6037 -- Array_Type_Declaration --
6038 ----------------------------
6039
6040 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6041 Component_Def : constant Node_Id := Component_Definition (Def);
6042 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6043 P : constant Node_Id := Parent (Def);
6044 Element_Type : Entity_Id;
6045 Implicit_Base : Entity_Id;
6046 Index : Node_Id;
6047 Nb_Index : Nat;
6048 Priv : Entity_Id;
6049 Related_Id : Entity_Id := Empty;
6050
6051 begin
6052 if Nkind (Def) = N_Constrained_Array_Definition then
6053 Index := First (Discrete_Subtype_Definitions (Def));
6054 else
6055 Index := First (Subtype_Marks (Def));
6056 end if;
6057
6058 -- Find proper names for the implicit types which may be public. In case
6059 -- of anonymous arrays we use the name of the first object of that type
6060 -- as prefix.
6061
6062 if No (T) then
6063 Related_Id := Defining_Identifier (P);
6064 else
6065 Related_Id := T;
6066 end if;
6067
6068 Nb_Index := 1;
6069 while Present (Index) loop
6070 Analyze (Index);
6071
6072 -- Test for odd case of trying to index a type by the type itself
6073
6074 if Is_Entity_Name (Index) and then Entity (Index) = T then
6075 Error_Msg_N ("type& cannot be indexed by itself", Index);
6076 Set_Entity (Index, Standard_Boolean);
6077 Set_Etype (Index, Standard_Boolean);
6078 end if;
6079
6080 -- Check SPARK restriction requiring a subtype mark
6081
6082 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6083 Check_SPARK_05_Restriction ("subtype mark required", Index);
6084 end if;
6085
6086 -- Add a subtype declaration for each index of private array type
6087 -- declaration whose etype is also private. For example:
6088
6089 -- package Pkg is
6090 -- type Index is private;
6091 -- private
6092 -- type Table is array (Index) of ...
6093 -- end;
6094
6095 -- This is currently required by the expander for the internally
6096 -- generated equality subprogram of records with variant parts in
6097 -- which the etype of some component is such private type.
6098
6099 if Ekind (Current_Scope) = E_Package
6100 and then In_Private_Part (Current_Scope)
6101 and then Has_Private_Declaration (Etype (Index))
6102 then
6103 declare
6104 Loc : constant Source_Ptr := Sloc (Def);
6105 Decl : Entity_Id;
6106 New_E : Entity_Id;
6107
6108 begin
6109 New_E := Make_Temporary (Loc, 'T');
6110 Set_Is_Internal (New_E);
6111
6112 Decl :=
6113 Make_Subtype_Declaration (Loc,
6114 Defining_Identifier => New_E,
6115 Subtype_Indication =>
6116 New_Occurrence_Of (Etype (Index), Loc));
6117
6118 Insert_Before (Parent (Def), Decl);
6119 Analyze (Decl);
6120 Set_Etype (Index, New_E);
6121
6122 -- If the index is a range or a subtype indication it carries
6123 -- no entity. Example:
6124
6125 -- package Pkg is
6126 -- type T is private;
6127 -- private
6128 -- type T is new Natural;
6129 -- Table : array (T(1) .. T(10)) of Boolean;
6130 -- end Pkg;
6131
6132 -- Otherwise the type of the reference is its entity.
6133
6134 if Is_Entity_Name (Index) then
6135 Set_Entity (Index, New_E);
6136 end if;
6137 end;
6138 end if;
6139
6140 Make_Index (Index, P, Related_Id, Nb_Index);
6141
6142 -- Check error of subtype with predicate for index type
6143
6144 Bad_Predicated_Subtype_Use
6145 ("subtype& has predicate, not allowed as index subtype",
6146 Index, Etype (Index));
6147
6148 -- Move to next index
6149
6150 Next_Index (Index);
6151 Nb_Index := Nb_Index + 1;
6152 end loop;
6153
6154 -- Process subtype indication if one is present
6155
6156 if Present (Component_Typ) then
6157 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6158
6159 Set_Etype (Component_Typ, Element_Type);
6160
6161 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6162 Check_SPARK_05_Restriction
6163 ("subtype mark required", Component_Typ);
6164 end if;
6165
6166 -- Ada 2005 (AI-230): Access Definition case
6167
6168 else pragma Assert (Present (Access_Definition (Component_Def)));
6169
6170 -- Indicate that the anonymous access type is created by the
6171 -- array type declaration.
6172
6173 Element_Type := Access_Definition
6174 (Related_Nod => P,
6175 N => Access_Definition (Component_Def));
6176 Set_Is_Local_Anonymous_Access (Element_Type);
6177
6178 -- Propagate the parent. This field is needed if we have to generate
6179 -- the master_id associated with an anonymous access to task type
6180 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6181
6182 Set_Parent (Element_Type, Parent (T));
6183
6184 -- Ada 2005 (AI-230): In case of components that are anonymous access
6185 -- types the level of accessibility depends on the enclosing type
6186 -- declaration
6187
6188 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6189
6190 -- Ada 2005 (AI-254)
6191
6192 declare
6193 CD : constant Node_Id :=
6194 Access_To_Subprogram_Definition
6195 (Access_Definition (Component_Def));
6196 begin
6197 if Present (CD) and then Protected_Present (CD) then
6198 Element_Type :=
6199 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6200 end if;
6201 end;
6202 end if;
6203
6204 -- Constrained array case
6205
6206 if No (T) then
6207 T := Create_Itype (E_Void, P, Related_Id, 'T');
6208 end if;
6209
6210 if Nkind (Def) = N_Constrained_Array_Definition then
6211
6212 -- Establish Implicit_Base as unconstrained base type
6213
6214 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6215
6216 Set_Etype (Implicit_Base, Implicit_Base);
6217 Set_Scope (Implicit_Base, Current_Scope);
6218 Set_Has_Delayed_Freeze (Implicit_Base);
6219 Set_Default_SSO (Implicit_Base);
6220
6221 -- The constrained array type is a subtype of the unconstrained one
6222
6223 Set_Ekind (T, E_Array_Subtype);
6224 Init_Size_Align (T);
6225 Set_Etype (T, Implicit_Base);
6226 Set_Scope (T, Current_Scope);
6227 Set_Is_Constrained (T);
6228 Set_First_Index (T,
6229 First (Discrete_Subtype_Definitions (Def)));
6230 Set_Has_Delayed_Freeze (T);
6231
6232 -- Complete setup of implicit base type
6233
6234 Set_Component_Size (Implicit_Base, Uint_0);
6235 Set_Component_Type (Implicit_Base, Element_Type);
6236 Set_Finalize_Storage_Only
6237 (Implicit_Base,
6238 Finalize_Storage_Only (Element_Type));
6239 Set_First_Index (Implicit_Base, First_Index (T));
6240 Set_Has_Controlled_Component
6241 (Implicit_Base,
6242 Has_Controlled_Component (Element_Type)
6243 or else Is_Controlled (Element_Type));
6244 Set_Packed_Array_Impl_Type
6245 (Implicit_Base, Empty);
6246
6247 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6248
6249 -- Unconstrained array case
6250
6251 else
6252 Set_Ekind (T, E_Array_Type);
6253 Init_Size_Align (T);
6254 Set_Etype (T, T);
6255 Set_Scope (T, Current_Scope);
6256 Set_Component_Size (T, Uint_0);
6257 Set_Is_Constrained (T, False);
6258 Set_First_Index (T, First (Subtype_Marks (Def)));
6259 Set_Has_Delayed_Freeze (T, True);
6260 Propagate_Concurrent_Flags (T, Element_Type);
6261 Set_Has_Controlled_Component (T, Has_Controlled_Component
6262 (Element_Type)
6263 or else
6264 Is_Controlled (Element_Type));
6265 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6266 (Element_Type));
6267 Set_Default_SSO (T);
6268 end if;
6269
6270 -- Common attributes for both cases
6271
6272 Set_Component_Type (Base_Type (T), Element_Type);
6273 Set_Packed_Array_Impl_Type (T, Empty);
6274
6275 if Aliased_Present (Component_Definition (Def)) then
6276 Check_SPARK_05_Restriction
6277 ("aliased is not allowed", Component_Definition (Def));
6278 Set_Has_Aliased_Components (Etype (T));
6279 end if;
6280
6281 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6282 -- array type to ensure that objects of this type are initialized.
6283
6284 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6285 Set_Can_Never_Be_Null (T);
6286
6287 if Null_Exclusion_Present (Component_Definition (Def))
6288
6289 -- No need to check itypes because in their case this check was
6290 -- done at their point of creation
6291
6292 and then not Is_Itype (Element_Type)
6293 then
6294 Error_Msg_N
6295 ("`NOT NULL` not allowed (null already excluded)",
6296 Subtype_Indication (Component_Definition (Def)));
6297 end if;
6298 end if;
6299
6300 Priv := Private_Component (Element_Type);
6301
6302 if Present (Priv) then
6303
6304 -- Check for circular definitions
6305
6306 if Priv = Any_Type then
6307 Set_Component_Type (Etype (T), Any_Type);
6308
6309 -- There is a gap in the visibility of operations on the composite
6310 -- type only if the component type is defined in a different scope.
6311
6312 elsif Scope (Priv) = Current_Scope then
6313 null;
6314
6315 elsif Is_Limited_Type (Priv) then
6316 Set_Is_Limited_Composite (Etype (T));
6317 Set_Is_Limited_Composite (T);
6318 else
6319 Set_Is_Private_Composite (Etype (T));
6320 Set_Is_Private_Composite (T);
6321 end if;
6322 end if;
6323
6324 -- A syntax error in the declaration itself may lead to an empty index
6325 -- list, in which case do a minimal patch.
6326
6327 if No (First_Index (T)) then
6328 Error_Msg_N ("missing index definition in array type declaration", T);
6329
6330 declare
6331 Indexes : constant List_Id :=
6332 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6333 begin
6334 Set_Discrete_Subtype_Definitions (Def, Indexes);
6335 Set_First_Index (T, First (Indexes));
6336 return;
6337 end;
6338 end if;
6339
6340 -- Create a concatenation operator for the new type. Internal array
6341 -- types created for packed entities do not need such, they are
6342 -- compatible with the user-defined type.
6343
6344 if Number_Dimensions (T) = 1
6345 and then not Is_Packed_Array_Impl_Type (T)
6346 then
6347 New_Concatenation_Op (T);
6348 end if;
6349
6350 -- In the case of an unconstrained array the parser has already verified
6351 -- that all the indexes are unconstrained but we still need to make sure
6352 -- that the element type is constrained.
6353
6354 if not Is_Definite_Subtype (Element_Type) then
6355 Error_Msg_N
6356 ("unconstrained element type in array declaration",
6357 Subtype_Indication (Component_Def));
6358
6359 elsif Is_Abstract_Type (Element_Type) then
6360 Error_Msg_N
6361 ("the type of a component cannot be abstract",
6362 Subtype_Indication (Component_Def));
6363 end if;
6364
6365 -- There may be an invariant declared for the component type, but
6366 -- the construction of the component invariant checking procedure
6367 -- takes place during expansion.
6368 end Array_Type_Declaration;
6369
6370 ------------------------------------------------------
6371 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6372 ------------------------------------------------------
6373
6374 function Replace_Anonymous_Access_To_Protected_Subprogram
6375 (N : Node_Id) return Entity_Id
6376 is
6377 Loc : constant Source_Ptr := Sloc (N);
6378
6379 Curr_Scope : constant Scope_Stack_Entry :=
6380 Scope_Stack.Table (Scope_Stack.Last);
6381
6382 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6383
6384 Acc : Node_Id;
6385 -- Access definition in declaration
6386
6387 Comp : Node_Id;
6388 -- Object definition or formal definition with an access definition
6389
6390 Decl : Node_Id;
6391 -- Declaration of anonymous access to subprogram type
6392
6393 Spec : Node_Id;
6394 -- Original specification in access to subprogram
6395
6396 P : Node_Id;
6397
6398 begin
6399 Set_Is_Internal (Anon);
6400
6401 case Nkind (N) is
6402 when N_Constrained_Array_Definition
6403 | N_Component_Declaration
6404 | N_Unconstrained_Array_Definition
6405 =>
6406 Comp := Component_Definition (N);
6407 Acc := Access_Definition (Comp);
6408
6409 when N_Discriminant_Specification =>
6410 Comp := Discriminant_Type (N);
6411 Acc := Comp;
6412
6413 when N_Parameter_Specification =>
6414 Comp := Parameter_Type (N);
6415 Acc := Comp;
6416
6417 when N_Access_Function_Definition =>
6418 Comp := Result_Definition (N);
6419 Acc := Comp;
6420
6421 when N_Object_Declaration =>
6422 Comp := Object_Definition (N);
6423 Acc := Comp;
6424
6425 when N_Function_Specification =>
6426 Comp := Result_Definition (N);
6427 Acc := Comp;
6428
6429 when others =>
6430 raise Program_Error;
6431 end case;
6432
6433 Spec := Access_To_Subprogram_Definition (Acc);
6434
6435 Decl :=
6436 Make_Full_Type_Declaration (Loc,
6437 Defining_Identifier => Anon,
6438 Type_Definition => Copy_Separate_Tree (Spec));
6439
6440 Mark_Rewrite_Insertion (Decl);
6441
6442 -- In ASIS mode, analyze the profile on the original node, because
6443 -- the separate copy does not provide enough links to recover the
6444 -- original tree. Analysis is limited to type annotations, within
6445 -- a temporary scope that serves as an anonymous subprogram to collect
6446 -- otherwise useless temporaries and itypes.
6447
6448 if ASIS_Mode then
6449 declare
6450 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6451
6452 begin
6453 if Nkind (Spec) = N_Access_Function_Definition then
6454 Set_Ekind (Typ, E_Function);
6455 else
6456 Set_Ekind (Typ, E_Procedure);
6457 end if;
6458
6459 Set_Parent (Typ, N);
6460 Set_Scope (Typ, Current_Scope);
6461 Push_Scope (Typ);
6462
6463 -- Nothing to do if procedure is parameterless
6464
6465 if Present (Parameter_Specifications (Spec)) then
6466 Process_Formals (Parameter_Specifications (Spec), Spec);
6467 end if;
6468
6469 if Nkind (Spec) = N_Access_Function_Definition then
6470 declare
6471 Def : constant Node_Id := Result_Definition (Spec);
6472
6473 begin
6474 -- The result might itself be an anonymous access type, so
6475 -- have to recurse.
6476
6477 if Nkind (Def) = N_Access_Definition then
6478 if Present (Access_To_Subprogram_Definition (Def)) then
6479 Set_Etype
6480 (Def,
6481 Replace_Anonymous_Access_To_Protected_Subprogram
6482 (Spec));
6483 else
6484 Find_Type (Subtype_Mark (Def));
6485 end if;
6486
6487 else
6488 Find_Type (Def);
6489 end if;
6490 end;
6491 end if;
6492
6493 End_Scope;
6494 end;
6495 end if;
6496
6497 -- Insert the new declaration in the nearest enclosing scope. If the
6498 -- parent is a body and N is its return type, the declaration belongs
6499 -- in the enclosing scope. Likewise if N is the type of a parameter.
6500
6501 P := Parent (N);
6502
6503 if Nkind (N) = N_Function_Specification
6504 and then Nkind (P) = N_Subprogram_Body
6505 then
6506 P := Parent (P);
6507 elsif Nkind (N) = N_Parameter_Specification
6508 and then Nkind (P) in N_Subprogram_Specification
6509 and then Nkind (Parent (P)) = N_Subprogram_Body
6510 then
6511 P := Parent (Parent (P));
6512 end if;
6513
6514 while Present (P) and then not Has_Declarations (P) loop
6515 P := Parent (P);
6516 end loop;
6517
6518 pragma Assert (Present (P));
6519
6520 if Nkind (P) = N_Package_Specification then
6521 Prepend (Decl, Visible_Declarations (P));
6522 else
6523 Prepend (Decl, Declarations (P));
6524 end if;
6525
6526 -- Replace the anonymous type with an occurrence of the new declaration.
6527 -- In all cases the rewritten node does not have the null-exclusion
6528 -- attribute because (if present) it was already inherited by the
6529 -- anonymous entity (Anon). Thus, in case of components we do not
6530 -- inherit this attribute.
6531
6532 if Nkind (N) = N_Parameter_Specification then
6533 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6534 Set_Etype (Defining_Identifier (N), Anon);
6535 Set_Null_Exclusion_Present (N, False);
6536
6537 elsif Nkind (N) = N_Object_Declaration then
6538 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6539 Set_Etype (Defining_Identifier (N), Anon);
6540
6541 elsif Nkind (N) = N_Access_Function_Definition then
6542 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6543
6544 elsif Nkind (N) = N_Function_Specification then
6545 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6546 Set_Etype (Defining_Unit_Name (N), Anon);
6547
6548 else
6549 Rewrite (Comp,
6550 Make_Component_Definition (Loc,
6551 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6552 end if;
6553
6554 Mark_Rewrite_Insertion (Comp);
6555
6556 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6557 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6558 and then not Is_Type (Current_Scope))
6559 then
6560
6561 -- Declaration can be analyzed in the current scope.
6562
6563 Analyze (Decl);
6564
6565 else
6566 -- Temporarily remove the current scope (record or subprogram) from
6567 -- the stack to add the new declarations to the enclosing scope.
6568 -- The anonymous entity is an Itype with the proper attributes.
6569
6570 Scope_Stack.Decrement_Last;
6571 Analyze (Decl);
6572 Set_Is_Itype (Anon);
6573 Set_Associated_Node_For_Itype (Anon, N);
6574 Scope_Stack.Append (Curr_Scope);
6575 end if;
6576
6577 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6578 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6579 return Anon;
6580 end Replace_Anonymous_Access_To_Protected_Subprogram;
6581
6582 -------------------------------
6583 -- Build_Derived_Access_Type --
6584 -------------------------------
6585
6586 procedure Build_Derived_Access_Type
6587 (N : Node_Id;
6588 Parent_Type : Entity_Id;
6589 Derived_Type : Entity_Id)
6590 is
6591 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6592
6593 Desig_Type : Entity_Id;
6594 Discr : Entity_Id;
6595 Discr_Con_Elist : Elist_Id;
6596 Discr_Con_El : Elmt_Id;
6597 Subt : Entity_Id;
6598
6599 begin
6600 -- Set the designated type so it is available in case this is an access
6601 -- to a self-referential type, e.g. a standard list type with a next
6602 -- pointer. Will be reset after subtype is built.
6603
6604 Set_Directly_Designated_Type
6605 (Derived_Type, Designated_Type (Parent_Type));
6606
6607 Subt := Process_Subtype (S, N);
6608
6609 if Nkind (S) /= N_Subtype_Indication
6610 and then Subt /= Base_Type (Subt)
6611 then
6612 Set_Ekind (Derived_Type, E_Access_Subtype);
6613 end if;
6614
6615 if Ekind (Derived_Type) = E_Access_Subtype then
6616 declare
6617 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6618 Ibase : constant Entity_Id :=
6619 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6620 Svg_Chars : constant Name_Id := Chars (Ibase);
6621 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6622 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6623
6624 begin
6625 Copy_Node (Pbase, Ibase);
6626
6627 -- Restore Itype status after Copy_Node
6628
6629 Set_Is_Itype (Ibase);
6630 Set_Associated_Node_For_Itype (Ibase, N);
6631
6632 Set_Chars (Ibase, Svg_Chars);
6633 Set_Prev_Entity (Ibase, Svg_Prev_E);
6634 Set_Next_Entity (Ibase, Svg_Next_E);
6635 Set_Sloc (Ibase, Sloc (Derived_Type));
6636 Set_Scope (Ibase, Scope (Derived_Type));
6637 Set_Freeze_Node (Ibase, Empty);
6638 Set_Is_Frozen (Ibase, False);
6639 Set_Comes_From_Source (Ibase, False);
6640 Set_Is_First_Subtype (Ibase, False);
6641
6642 Set_Etype (Ibase, Pbase);
6643 Set_Etype (Derived_Type, Ibase);
6644 end;
6645 end if;
6646
6647 Set_Directly_Designated_Type
6648 (Derived_Type, Designated_Type (Subt));
6649
6650 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6651 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6652 Set_Size_Info (Derived_Type, Parent_Type);
6653 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6654 Set_Depends_On_Private (Derived_Type,
6655 Has_Private_Component (Derived_Type));
6656 Conditional_Delay (Derived_Type, Subt);
6657
6658 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6659 -- that it is not redundant.
6660
6661 if Null_Exclusion_Present (Type_Definition (N)) then
6662 Set_Can_Never_Be_Null (Derived_Type);
6663
6664 elsif Can_Never_Be_Null (Parent_Type) then
6665 Set_Can_Never_Be_Null (Derived_Type);
6666 end if;
6667
6668 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6669 -- the root type for this information.
6670
6671 -- Apply range checks to discriminants for derived record case
6672 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6673
6674 Desig_Type := Designated_Type (Derived_Type);
6675
6676 if Is_Composite_Type (Desig_Type)
6677 and then (not Is_Array_Type (Desig_Type))
6678 and then Has_Discriminants (Desig_Type)
6679 and then Base_Type (Desig_Type) /= Desig_Type
6680 then
6681 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6682 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6683
6684 Discr := First_Discriminant (Base_Type (Desig_Type));
6685 while Present (Discr_Con_El) loop
6686 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6687 Next_Elmt (Discr_Con_El);
6688 Next_Discriminant (Discr);
6689 end loop;
6690 end if;
6691 end Build_Derived_Access_Type;
6692
6693 ------------------------------
6694 -- Build_Derived_Array_Type --
6695 ------------------------------
6696
6697 procedure Build_Derived_Array_Type
6698 (N : Node_Id;
6699 Parent_Type : Entity_Id;
6700 Derived_Type : Entity_Id)
6701 is
6702 Loc : constant Source_Ptr := Sloc (N);
6703 Tdef : constant Node_Id := Type_Definition (N);
6704 Indic : constant Node_Id := Subtype_Indication (Tdef);
6705 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6706 Implicit_Base : Entity_Id := Empty;
6707 New_Indic : Node_Id;
6708
6709 procedure Make_Implicit_Base;
6710 -- If the parent subtype is constrained, the derived type is a subtype
6711 -- of an implicit base type derived from the parent base.
6712
6713 ------------------------
6714 -- Make_Implicit_Base --
6715 ------------------------
6716
6717 procedure Make_Implicit_Base is
6718 begin
6719 Implicit_Base :=
6720 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6721
6722 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6723 Set_Etype (Implicit_Base, Parent_Base);
6724
6725 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6726 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6727
6728 Set_Has_Delayed_Freeze (Implicit_Base, True);
6729 end Make_Implicit_Base;
6730
6731 -- Start of processing for Build_Derived_Array_Type
6732
6733 begin
6734 if not Is_Constrained (Parent_Type) then
6735 if Nkind (Indic) /= N_Subtype_Indication then
6736 Set_Ekind (Derived_Type, E_Array_Type);
6737
6738 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6739 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6740
6741 Set_Has_Delayed_Freeze (Derived_Type, True);
6742
6743 else
6744 Make_Implicit_Base;
6745 Set_Etype (Derived_Type, Implicit_Base);
6746
6747 New_Indic :=
6748 Make_Subtype_Declaration (Loc,
6749 Defining_Identifier => Derived_Type,
6750 Subtype_Indication =>
6751 Make_Subtype_Indication (Loc,
6752 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6753 Constraint => Constraint (Indic)));
6754
6755 Rewrite (N, New_Indic);
6756 Analyze (N);
6757 end if;
6758
6759 else
6760 if Nkind (Indic) /= N_Subtype_Indication then
6761 Make_Implicit_Base;
6762
6763 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6764 Set_Etype (Derived_Type, Implicit_Base);
6765 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6766
6767 else
6768 Error_Msg_N ("illegal constraint on constrained type", Indic);
6769 end if;
6770 end if;
6771
6772 -- If parent type is not a derived type itself, and is declared in
6773 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6774 -- the new type's concatenation operator since Derive_Subprograms
6775 -- will not inherit the parent's operator. If the parent type is
6776 -- unconstrained, the operator is of the unconstrained base type.
6777
6778 if Number_Dimensions (Parent_Type) = 1
6779 and then not Is_Limited_Type (Parent_Type)
6780 and then not Is_Derived_Type (Parent_Type)
6781 and then not Is_Package_Or_Generic_Package
6782 (Scope (Base_Type (Parent_Type)))
6783 then
6784 if not Is_Constrained (Parent_Type)
6785 and then Is_Constrained (Derived_Type)
6786 then
6787 New_Concatenation_Op (Implicit_Base);
6788 else
6789 New_Concatenation_Op (Derived_Type);
6790 end if;
6791 end if;
6792 end Build_Derived_Array_Type;
6793
6794 -----------------------------------
6795 -- Build_Derived_Concurrent_Type --
6796 -----------------------------------
6797
6798 procedure Build_Derived_Concurrent_Type
6799 (N : Node_Id;
6800 Parent_Type : Entity_Id;
6801 Derived_Type : Entity_Id)
6802 is
6803 Loc : constant Source_Ptr := Sloc (N);
6804
6805 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6806 Corr_Decl : Node_Id;
6807 Corr_Decl_Needed : Boolean;
6808 -- If the derived type has fewer discriminants than its parent, the
6809 -- corresponding record is also a derived type, in order to account for
6810 -- the bound discriminants. We create a full type declaration for it in
6811 -- this case.
6812
6813 Constraint_Present : constant Boolean :=
6814 Nkind (Subtype_Indication (Type_Definition (N))) =
6815 N_Subtype_Indication;
6816
6817 D_Constraint : Node_Id;
6818 New_Constraint : Elist_Id := No_Elist;
6819 Old_Disc : Entity_Id;
6820 New_Disc : Entity_Id;
6821 New_N : Node_Id;
6822
6823 begin
6824 Set_Stored_Constraint (Derived_Type, No_Elist);
6825 Corr_Decl_Needed := False;
6826 Old_Disc := Empty;
6827
6828 if Present (Discriminant_Specifications (N))
6829 and then Constraint_Present
6830 then
6831 Old_Disc := First_Discriminant (Parent_Type);
6832 New_Disc := First (Discriminant_Specifications (N));
6833 while Present (New_Disc) and then Present (Old_Disc) loop
6834 Next_Discriminant (Old_Disc);
6835 Next (New_Disc);
6836 end loop;
6837 end if;
6838
6839 if Present (Old_Disc) and then Expander_Active then
6840
6841 -- The new type has fewer discriminants, so we need to create a new
6842 -- corresponding record, which is derived from the corresponding
6843 -- record of the parent, and has a stored constraint that captures
6844 -- the values of the discriminant constraints. The corresponding
6845 -- record is needed only if expander is active and code generation is
6846 -- enabled.
6847
6848 -- The type declaration for the derived corresponding record has the
6849 -- same discriminant part and constraints as the current declaration.
6850 -- Copy the unanalyzed tree to build declaration.
6851
6852 Corr_Decl_Needed := True;
6853 New_N := Copy_Separate_Tree (N);
6854
6855 Corr_Decl :=
6856 Make_Full_Type_Declaration (Loc,
6857 Defining_Identifier => Corr_Record,
6858 Discriminant_Specifications =>
6859 Discriminant_Specifications (New_N),
6860 Type_Definition =>
6861 Make_Derived_Type_Definition (Loc,
6862 Subtype_Indication =>
6863 Make_Subtype_Indication (Loc,
6864 Subtype_Mark =>
6865 New_Occurrence_Of
6866 (Corresponding_Record_Type (Parent_Type), Loc),
6867 Constraint =>
6868 Constraint
6869 (Subtype_Indication (Type_Definition (New_N))))));
6870 end if;
6871
6872 -- Copy Storage_Size and Relative_Deadline variables if task case
6873
6874 if Is_Task_Type (Parent_Type) then
6875 Set_Storage_Size_Variable (Derived_Type,
6876 Storage_Size_Variable (Parent_Type));
6877 Set_Relative_Deadline_Variable (Derived_Type,
6878 Relative_Deadline_Variable (Parent_Type));
6879 end if;
6880
6881 if Present (Discriminant_Specifications (N)) then
6882 Push_Scope (Derived_Type);
6883 Check_Or_Process_Discriminants (N, Derived_Type);
6884
6885 if Constraint_Present then
6886 New_Constraint :=
6887 Expand_To_Stored_Constraint
6888 (Parent_Type,
6889 Build_Discriminant_Constraints
6890 (Parent_Type,
6891 Subtype_Indication (Type_Definition (N)), True));
6892 end if;
6893
6894 End_Scope;
6895
6896 elsif Constraint_Present then
6897
6898 -- Build constrained subtype, copying the constraint, and derive
6899 -- from it to create a derived constrained type.
6900
6901 declare
6902 Loc : constant Source_Ptr := Sloc (N);
6903 Anon : constant Entity_Id :=
6904 Make_Defining_Identifier (Loc,
6905 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6906 Decl : Node_Id;
6907
6908 begin
6909 Decl :=
6910 Make_Subtype_Declaration (Loc,
6911 Defining_Identifier => Anon,
6912 Subtype_Indication =>
6913 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6914 Insert_Before (N, Decl);
6915 Analyze (Decl);
6916
6917 Rewrite (Subtype_Indication (Type_Definition (N)),
6918 New_Occurrence_Of (Anon, Loc));
6919 Set_Analyzed (Derived_Type, False);
6920 Analyze (N);
6921 return;
6922 end;
6923 end if;
6924
6925 -- By default, operations and private data are inherited from parent.
6926 -- However, in the presence of bound discriminants, a new corresponding
6927 -- record will be created, see below.
6928
6929 Set_Has_Discriminants
6930 (Derived_Type, Has_Discriminants (Parent_Type));
6931 Set_Corresponding_Record_Type
6932 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6933
6934 -- Is_Constrained is set according the parent subtype, but is set to
6935 -- False if the derived type is declared with new discriminants.
6936
6937 Set_Is_Constrained
6938 (Derived_Type,
6939 (Is_Constrained (Parent_Type) or else Constraint_Present)
6940 and then not Present (Discriminant_Specifications (N)));
6941
6942 if Constraint_Present then
6943 if not Has_Discriminants (Parent_Type) then
6944 Error_Msg_N ("untagged parent must have discriminants", N);
6945
6946 elsif Present (Discriminant_Specifications (N)) then
6947
6948 -- Verify that new discriminants are used to constrain old ones
6949
6950 D_Constraint :=
6951 First
6952 (Constraints
6953 (Constraint (Subtype_Indication (Type_Definition (N)))));
6954
6955 Old_Disc := First_Discriminant (Parent_Type);
6956
6957 while Present (D_Constraint) loop
6958 if Nkind (D_Constraint) /= N_Discriminant_Association then
6959
6960 -- Positional constraint. If it is a reference to a new
6961 -- discriminant, it constrains the corresponding old one.
6962
6963 if Nkind (D_Constraint) = N_Identifier then
6964 New_Disc := First_Discriminant (Derived_Type);
6965 while Present (New_Disc) loop
6966 exit when Chars (New_Disc) = Chars (D_Constraint);
6967 Next_Discriminant (New_Disc);
6968 end loop;
6969
6970 if Present (New_Disc) then
6971 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6972 end if;
6973 end if;
6974
6975 Next_Discriminant (Old_Disc);
6976
6977 -- if this is a named constraint, search by name for the old
6978 -- discriminants constrained by the new one.
6979
6980 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6981
6982 -- Find new discriminant with that name
6983
6984 New_Disc := First_Discriminant (Derived_Type);
6985 while Present (New_Disc) loop
6986 exit when
6987 Chars (New_Disc) = Chars (Expression (D_Constraint));
6988 Next_Discriminant (New_Disc);
6989 end loop;
6990
6991 if Present (New_Disc) then
6992
6993 -- Verify that new discriminant renames some discriminant
6994 -- of the parent type, and associate the new discriminant
6995 -- with one or more old ones that it renames.
6996
6997 declare
6998 Selector : Node_Id;
6999
7000 begin
7001 Selector := First (Selector_Names (D_Constraint));
7002 while Present (Selector) loop
7003 Old_Disc := First_Discriminant (Parent_Type);
7004 while Present (Old_Disc) loop
7005 exit when Chars (Old_Disc) = Chars (Selector);
7006 Next_Discriminant (Old_Disc);
7007 end loop;
7008
7009 if Present (Old_Disc) then
7010 Set_Corresponding_Discriminant
7011 (New_Disc, Old_Disc);
7012 end if;
7013
7014 Next (Selector);
7015 end loop;
7016 end;
7017 end if;
7018 end if;
7019
7020 Next (D_Constraint);
7021 end loop;
7022
7023 New_Disc := First_Discriminant (Derived_Type);
7024 while Present (New_Disc) loop
7025 if No (Corresponding_Discriminant (New_Disc)) then
7026 Error_Msg_NE
7027 ("new discriminant& must constrain old one", N, New_Disc);
7028
7029 elsif not
7030 Subtypes_Statically_Compatible
7031 (Etype (New_Disc),
7032 Etype (Corresponding_Discriminant (New_Disc)))
7033 then
7034 Error_Msg_NE
7035 ("& not statically compatible with parent discriminant",
7036 N, New_Disc);
7037 end if;
7038
7039 Next_Discriminant (New_Disc);
7040 end loop;
7041 end if;
7042
7043 elsif Present (Discriminant_Specifications (N)) then
7044 Error_Msg_N
7045 ("missing discriminant constraint in untagged derivation", N);
7046 end if;
7047
7048 -- The entity chain of the derived type includes the new discriminants
7049 -- but shares operations with the parent.
7050
7051 if Present (Discriminant_Specifications (N)) then
7052 Old_Disc := First_Discriminant (Parent_Type);
7053 while Present (Old_Disc) loop
7054 if No (Next_Entity (Old_Disc))
7055 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7056 then
7057 Link_Entities
7058 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7059 exit;
7060 end if;
7061
7062 Next_Discriminant (Old_Disc);
7063 end loop;
7064
7065 else
7066 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7067 if Has_Discriminants (Parent_Type) then
7068 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7069 Set_Discriminant_Constraint (
7070 Derived_Type, Discriminant_Constraint (Parent_Type));
7071 end if;
7072 end if;
7073
7074 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7075
7076 Set_Has_Completion (Derived_Type);
7077
7078 if Corr_Decl_Needed then
7079 Set_Stored_Constraint (Derived_Type, New_Constraint);
7080 Insert_After (N, Corr_Decl);
7081 Analyze (Corr_Decl);
7082 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7083 end if;
7084 end Build_Derived_Concurrent_Type;
7085
7086 ------------------------------------
7087 -- Build_Derived_Enumeration_Type --
7088 ------------------------------------
7089
7090 procedure Build_Derived_Enumeration_Type
7091 (N : Node_Id;
7092 Parent_Type : Entity_Id;
7093 Derived_Type : Entity_Id)
7094 is
7095 Loc : constant Source_Ptr := Sloc (N);
7096 Def : constant Node_Id := Type_Definition (N);
7097 Indic : constant Node_Id := Subtype_Indication (Def);
7098 Implicit_Base : Entity_Id;
7099 Literal : Entity_Id;
7100 New_Lit : Entity_Id;
7101 Literals_List : List_Id;
7102 Type_Decl : Node_Id;
7103 Hi, Lo : Node_Id;
7104 Rang_Expr : Node_Id;
7105
7106 begin
7107 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7108 -- not have explicit literals lists we need to process types derived
7109 -- from them specially. This is handled by Derived_Standard_Character.
7110 -- If the parent type is a generic type, there are no literals either,
7111 -- and we construct the same skeletal representation as for the generic
7112 -- parent type.
7113
7114 if Is_Standard_Character_Type (Parent_Type) then
7115 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7116
7117 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7118 declare
7119 Lo : Node_Id;
7120 Hi : Node_Id;
7121
7122 begin
7123 if Nkind (Indic) /= N_Subtype_Indication then
7124 Lo :=
7125 Make_Attribute_Reference (Loc,
7126 Attribute_Name => Name_First,
7127 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7128 Set_Etype (Lo, Derived_Type);
7129
7130 Hi :=
7131 Make_Attribute_Reference (Loc,
7132 Attribute_Name => Name_Last,
7133 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7134 Set_Etype (Hi, Derived_Type);
7135
7136 Set_Scalar_Range (Derived_Type,
7137 Make_Range (Loc,
7138 Low_Bound => Lo,
7139 High_Bound => Hi));
7140 else
7141
7142 -- Analyze subtype indication and verify compatibility
7143 -- with parent type.
7144
7145 if Base_Type (Process_Subtype (Indic, N)) /=
7146 Base_Type (Parent_Type)
7147 then
7148 Error_Msg_N
7149 ("illegal constraint for formal discrete type", N);
7150 end if;
7151 end if;
7152 end;
7153
7154 else
7155 -- If a constraint is present, analyze the bounds to catch
7156 -- premature usage of the derived literals.
7157
7158 if Nkind (Indic) = N_Subtype_Indication
7159 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7160 then
7161 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7162 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7163 end if;
7164
7165 -- Introduce an implicit base type for the derived type even if there
7166 -- is no constraint attached to it, since this seems closer to the
7167 -- Ada semantics. Build a full type declaration tree for the derived
7168 -- type using the implicit base type as the defining identifier. The
7169 -- build a subtype declaration tree which applies the constraint (if
7170 -- any) have it replace the derived type declaration.
7171
7172 Literal := First_Literal (Parent_Type);
7173 Literals_List := New_List;
7174 while Present (Literal)
7175 and then Ekind (Literal) = E_Enumeration_Literal
7176 loop
7177 -- Literals of the derived type have the same representation as
7178 -- those of the parent type, but this representation can be
7179 -- overridden by an explicit representation clause. Indicate
7180 -- that there is no explicit representation given yet. These
7181 -- derived literals are implicit operations of the new type,
7182 -- and can be overridden by explicit ones.
7183
7184 if Nkind (Literal) = N_Defining_Character_Literal then
7185 New_Lit :=
7186 Make_Defining_Character_Literal (Loc, Chars (Literal));
7187 else
7188 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7189 end if;
7190
7191 Set_Ekind (New_Lit, E_Enumeration_Literal);
7192 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7193 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7194 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7195 Set_Alias (New_Lit, Literal);
7196 Set_Is_Known_Valid (New_Lit, True);
7197
7198 Append (New_Lit, Literals_List);
7199 Next_Literal (Literal);
7200 end loop;
7201
7202 Implicit_Base :=
7203 Make_Defining_Identifier (Sloc (Derived_Type),
7204 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7205
7206 -- Indicate the proper nature of the derived type. This must be done
7207 -- before analysis of the literals, to recognize cases when a literal
7208 -- may be hidden by a previous explicit function definition (cf.
7209 -- c83031a).
7210
7211 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7212 Set_Etype (Derived_Type, Implicit_Base);
7213
7214 Type_Decl :=
7215 Make_Full_Type_Declaration (Loc,
7216 Defining_Identifier => Implicit_Base,
7217 Discriminant_Specifications => No_List,
7218 Type_Definition =>
7219 Make_Enumeration_Type_Definition (Loc, Literals_List));
7220
7221 Mark_Rewrite_Insertion (Type_Decl);
7222 Insert_Before (N, Type_Decl);
7223 Analyze (Type_Decl);
7224
7225 -- The anonymous base now has a full declaration, but this base
7226 -- is not a first subtype.
7227
7228 Set_Is_First_Subtype (Implicit_Base, False);
7229
7230 -- After the implicit base is analyzed its Etype needs to be changed
7231 -- to reflect the fact that it is derived from the parent type which
7232 -- was ignored during analysis. We also set the size at this point.
7233
7234 Set_Etype (Implicit_Base, Parent_Type);
7235
7236 Set_Size_Info (Implicit_Base, Parent_Type);
7237 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7238 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7239
7240 -- Copy other flags from parent type
7241
7242 Set_Has_Non_Standard_Rep
7243 (Implicit_Base, Has_Non_Standard_Rep
7244 (Parent_Type));
7245 Set_Has_Pragma_Ordered
7246 (Implicit_Base, Has_Pragma_Ordered
7247 (Parent_Type));
7248 Set_Has_Delayed_Freeze (Implicit_Base);
7249
7250 -- Process the subtype indication including a validation check on the
7251 -- constraint, if any. If a constraint is given, its bounds must be
7252 -- implicitly converted to the new type.
7253
7254 if Nkind (Indic) = N_Subtype_Indication then
7255 declare
7256 R : constant Node_Id :=
7257 Range_Expression (Constraint (Indic));
7258
7259 begin
7260 if Nkind (R) = N_Range then
7261 Hi := Build_Scalar_Bound
7262 (High_Bound (R), Parent_Type, Implicit_Base);
7263 Lo := Build_Scalar_Bound
7264 (Low_Bound (R), Parent_Type, Implicit_Base);
7265
7266 else
7267 -- Constraint is a Range attribute. Replace with explicit
7268 -- mention of the bounds of the prefix, which must be a
7269 -- subtype.
7270
7271 Analyze (Prefix (R));
7272 Hi :=
7273 Convert_To (Implicit_Base,
7274 Make_Attribute_Reference (Loc,
7275 Attribute_Name => Name_Last,
7276 Prefix =>
7277 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7278
7279 Lo :=
7280 Convert_To (Implicit_Base,
7281 Make_Attribute_Reference (Loc,
7282 Attribute_Name => Name_First,
7283 Prefix =>
7284 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7285 end if;
7286 end;
7287
7288 else
7289 Hi :=
7290 Build_Scalar_Bound
7291 (Type_High_Bound (Parent_Type),
7292 Parent_Type, Implicit_Base);
7293 Lo :=
7294 Build_Scalar_Bound
7295 (Type_Low_Bound (Parent_Type),
7296 Parent_Type, Implicit_Base);
7297 end if;
7298
7299 Rang_Expr :=
7300 Make_Range (Loc,
7301 Low_Bound => Lo,
7302 High_Bound => Hi);
7303
7304 -- If we constructed a default range for the case where no range
7305 -- was given, then the expressions in the range must not freeze
7306 -- since they do not correspond to expressions in the source.
7307 -- However, if the type inherits predicates the expressions will
7308 -- be elaborated earlier and must freeze.
7309
7310 if Nkind (Indic) /= N_Subtype_Indication
7311 and then not Has_Predicates (Derived_Type)
7312 then
7313 Set_Must_Not_Freeze (Lo);
7314 Set_Must_Not_Freeze (Hi);
7315 Set_Must_Not_Freeze (Rang_Expr);
7316 end if;
7317
7318 Rewrite (N,
7319 Make_Subtype_Declaration (Loc,
7320 Defining_Identifier => Derived_Type,
7321 Subtype_Indication =>
7322 Make_Subtype_Indication (Loc,
7323 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7324 Constraint =>
7325 Make_Range_Constraint (Loc,
7326 Range_Expression => Rang_Expr))));
7327
7328 Analyze (N);
7329
7330 -- Propagate the aspects from the original type declaration to the
7331 -- declaration of the implicit base.
7332
7333 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7334
7335 -- Apply a range check. Since this range expression doesn't have an
7336 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7337 -- this right???
7338
7339 if Nkind (Indic) = N_Subtype_Indication then
7340 Apply_Range_Check
7341 (Range_Expression (Constraint (Indic)), Parent_Type,
7342 Source_Typ => Entity (Subtype_Mark (Indic)));
7343 end if;
7344 end if;
7345 end Build_Derived_Enumeration_Type;
7346
7347 --------------------------------
7348 -- Build_Derived_Numeric_Type --
7349 --------------------------------
7350
7351 procedure Build_Derived_Numeric_Type
7352 (N : Node_Id;
7353 Parent_Type : Entity_Id;
7354 Derived_Type : Entity_Id)
7355 is
7356 Loc : constant Source_Ptr := Sloc (N);
7357 Tdef : constant Node_Id := Type_Definition (N);
7358 Indic : constant Node_Id := Subtype_Indication (Tdef);
7359 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7360 No_Constraint : constant Boolean := Nkind (Indic) /=
7361 N_Subtype_Indication;
7362 Implicit_Base : Entity_Id;
7363
7364 Lo : Node_Id;
7365 Hi : Node_Id;
7366
7367 begin
7368 -- Process the subtype indication including a validation check on
7369 -- the constraint if any.
7370
7371 Discard_Node (Process_Subtype (Indic, N));
7372
7373 -- Introduce an implicit base type for the derived type even if there
7374 -- is no constraint attached to it, since this seems closer to the Ada
7375 -- semantics.
7376
7377 Implicit_Base :=
7378 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7379
7380 Set_Etype (Implicit_Base, Parent_Base);
7381 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7382 Set_Size_Info (Implicit_Base, Parent_Base);
7383 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7384 Set_Parent (Implicit_Base, Parent (Derived_Type));
7385 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7386
7387 -- Set RM Size for discrete type or decimal fixed-point type
7388 -- Ordinary fixed-point is excluded, why???
7389
7390 if Is_Discrete_Type (Parent_Base)
7391 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7392 then
7393 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7394 end if;
7395
7396 Set_Has_Delayed_Freeze (Implicit_Base);
7397
7398 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7399 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7400
7401 Set_Scalar_Range (Implicit_Base,
7402 Make_Range (Loc,
7403 Low_Bound => Lo,
7404 High_Bound => Hi));
7405
7406 if Has_Infinities (Parent_Base) then
7407 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7408 end if;
7409
7410 -- The Derived_Type, which is the entity of the declaration, is a
7411 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7412 -- absence of an explicit constraint.
7413
7414 Set_Etype (Derived_Type, Implicit_Base);
7415
7416 -- If we did not have a constraint, then the Ekind is set from the
7417 -- parent type (otherwise Process_Subtype has set the bounds)
7418
7419 if No_Constraint then
7420 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7421 end if;
7422
7423 -- If we did not have a range constraint, then set the range from the
7424 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7425
7426 if No_Constraint or else not Has_Range_Constraint (Indic) then
7427 Set_Scalar_Range (Derived_Type,
7428 Make_Range (Loc,
7429 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7430 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7431 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7432
7433 if Has_Infinities (Parent_Type) then
7434 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7435 end if;
7436
7437 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7438 end if;
7439
7440 Set_Is_Descendant_Of_Address (Derived_Type,
7441 Is_Descendant_Of_Address (Parent_Type));
7442 Set_Is_Descendant_Of_Address (Implicit_Base,
7443 Is_Descendant_Of_Address (Parent_Type));
7444
7445 -- Set remaining type-specific fields, depending on numeric type
7446
7447 if Is_Modular_Integer_Type (Parent_Type) then
7448 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7449
7450 Set_Non_Binary_Modulus
7451 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7452
7453 Set_Is_Known_Valid
7454 (Implicit_Base, Is_Known_Valid (Parent_Base));
7455
7456 elsif Is_Floating_Point_Type (Parent_Type) then
7457
7458 -- Digits of base type is always copied from the digits value of
7459 -- the parent base type, but the digits of the derived type will
7460 -- already have been set if there was a constraint present.
7461
7462 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7463 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7464
7465 if No_Constraint then
7466 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7467 end if;
7468
7469 elsif Is_Fixed_Point_Type (Parent_Type) then
7470
7471 -- Small of base type and derived type are always copied from the
7472 -- parent base type, since smalls never change. The delta of the
7473 -- base type is also copied from the parent base type. However the
7474 -- delta of the derived type will have been set already if a
7475 -- constraint was present.
7476
7477 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7478 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7479 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7480
7481 if No_Constraint then
7482 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7483 end if;
7484
7485 -- The scale and machine radix in the decimal case are always
7486 -- copied from the parent base type.
7487
7488 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7489 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7490 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7491
7492 Set_Machine_Radix_10
7493 (Derived_Type, Machine_Radix_10 (Parent_Base));
7494 Set_Machine_Radix_10
7495 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7496
7497 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7498
7499 if No_Constraint then
7500 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7501
7502 else
7503 -- the analysis of the subtype_indication sets the
7504 -- digits value of the derived type.
7505
7506 null;
7507 end if;
7508 end if;
7509 end if;
7510
7511 if Is_Integer_Type (Parent_Type) then
7512 Set_Has_Shift_Operator
7513 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7514 end if;
7515
7516 -- The type of the bounds is that of the parent type, and they
7517 -- must be converted to the derived type.
7518
7519 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7520
7521 -- The implicit_base should be frozen when the derived type is frozen,
7522 -- but note that it is used in the conversions of the bounds. For fixed
7523 -- types we delay the determination of the bounds until the proper
7524 -- freezing point. For other numeric types this is rejected by GCC, for
7525 -- reasons that are currently unclear (???), so we choose to freeze the
7526 -- implicit base now. In the case of integers and floating point types
7527 -- this is harmless because subsequent representation clauses cannot
7528 -- affect anything, but it is still baffling that we cannot use the
7529 -- same mechanism for all derived numeric types.
7530
7531 -- There is a further complication: actually some representation
7532 -- clauses can affect the implicit base type. For example, attribute
7533 -- definition clauses for stream-oriented attributes need to set the
7534 -- corresponding TSS entries on the base type, and this normally
7535 -- cannot be done after the base type is frozen, so the circuitry in
7536 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7537 -- and not use Set_TSS in this case.
7538
7539 -- There are also consequences for the case of delayed representation
7540 -- aspects for some cases. For example, a Size aspect is delayed and
7541 -- should not be evaluated to the freeze point. This early freezing
7542 -- means that the size attribute evaluation happens too early???
7543
7544 if Is_Fixed_Point_Type (Parent_Type) then
7545 Conditional_Delay (Implicit_Base, Parent_Type);
7546 else
7547 Freeze_Before (N, Implicit_Base);
7548 end if;
7549 end Build_Derived_Numeric_Type;
7550
7551 --------------------------------
7552 -- Build_Derived_Private_Type --
7553 --------------------------------
7554
7555 procedure Build_Derived_Private_Type
7556 (N : Node_Id;
7557 Parent_Type : Entity_Id;
7558 Derived_Type : Entity_Id;
7559 Is_Completion : Boolean;
7560 Derive_Subps : Boolean := True)
7561 is
7562 Loc : constant Source_Ptr := Sloc (N);
7563 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7564 Par_Scope : constant Entity_Id := Scope (Par_Base);
7565 Full_N : constant Node_Id := New_Copy_Tree (N);
7566 Full_Der : Entity_Id := New_Copy (Derived_Type);
7567 Full_P : Entity_Id;
7568
7569 procedure Build_Full_Derivation;
7570 -- Build full derivation, i.e. derive from the full view
7571
7572 procedure Copy_And_Build;
7573 -- Copy derived type declaration, replace parent with its full view,
7574 -- and build derivation
7575
7576 ---------------------------
7577 -- Build_Full_Derivation --
7578 ---------------------------
7579
7580 procedure Build_Full_Derivation is
7581 begin
7582 -- If parent scope is not open, install the declarations
7583
7584 if not In_Open_Scopes (Par_Scope) then
7585 Install_Private_Declarations (Par_Scope);
7586 Install_Visible_Declarations (Par_Scope);
7587 Copy_And_Build;
7588 Uninstall_Declarations (Par_Scope);
7589
7590 -- If parent scope is open and in another unit, and parent has a
7591 -- completion, then the derivation is taking place in the visible
7592 -- part of a child unit. In that case retrieve the full view of
7593 -- the parent momentarily.
7594
7595 elsif not In_Same_Source_Unit (N, Parent_Type) then
7596 Full_P := Full_View (Parent_Type);
7597 Exchange_Declarations (Parent_Type);
7598 Copy_And_Build;
7599 Exchange_Declarations (Full_P);
7600
7601 -- Otherwise it is a local derivation
7602
7603 else
7604 Copy_And_Build;
7605 end if;
7606 end Build_Full_Derivation;
7607
7608 --------------------
7609 -- Copy_And_Build --
7610 --------------------
7611
7612 procedure Copy_And_Build is
7613 Full_Parent : Entity_Id := Parent_Type;
7614
7615 begin
7616 -- If the parent is itself derived from another private type,
7617 -- installing the private declarations has not affected its
7618 -- privacy status, so use its own full view explicitly.
7619
7620 if Is_Private_Type (Full_Parent)
7621 and then Present (Full_View (Full_Parent))
7622 then
7623 Full_Parent := Full_View (Full_Parent);
7624 end if;
7625
7626 -- And its underlying full view if necessary
7627
7628 if Is_Private_Type (Full_Parent)
7629 and then Present (Underlying_Full_View (Full_Parent))
7630 then
7631 Full_Parent := Underlying_Full_View (Full_Parent);
7632 end if;
7633
7634 -- For record, access and most enumeration types, derivation from
7635 -- the full view requires a fully-fledged declaration. In the other
7636 -- cases, just use an itype.
7637
7638 if Ekind (Full_Parent) in Record_Kind
7639 or else Ekind (Full_Parent) in Access_Kind
7640 or else
7641 (Ekind (Full_Parent) in Enumeration_Kind
7642 and then not Is_Standard_Character_Type (Full_Parent)
7643 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7644 then
7645 -- Copy and adjust declaration to provide a completion for what
7646 -- is originally a private declaration. Indicate that full view
7647 -- is internally generated.
7648
7649 Set_Comes_From_Source (Full_N, False);
7650 Set_Comes_From_Source (Full_Der, False);
7651 Set_Parent (Full_Der, Full_N);
7652 Set_Defining_Identifier (Full_N, Full_Der);
7653
7654 -- If there are no constraints, adjust the subtype mark
7655
7656 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7657 N_Subtype_Indication
7658 then
7659 Set_Subtype_Indication
7660 (Type_Definition (Full_N),
7661 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7662 end if;
7663
7664 Insert_After (N, Full_N);
7665
7666 -- Build full view of derived type from full view of parent which
7667 -- is now installed. Subprograms have been derived on the partial
7668 -- view, the completion does not derive them anew.
7669
7670 if Ekind (Full_Parent) in Record_Kind then
7671
7672 -- If parent type is tagged, the completion inherits the proper
7673 -- primitive operations.
7674
7675 if Is_Tagged_Type (Parent_Type) then
7676 Build_Derived_Record_Type
7677 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7678 else
7679 Build_Derived_Record_Type
7680 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7681 end if;
7682
7683 else
7684 Build_Derived_Type
7685 (Full_N, Full_Parent, Full_Der,
7686 Is_Completion => False, Derive_Subps => False);
7687 end if;
7688
7689 -- The full declaration has been introduced into the tree and
7690 -- processed in the step above. It should not be analyzed again
7691 -- (when encountered later in the current list of declarations)
7692 -- to prevent spurious name conflicts. The full entity remains
7693 -- invisible.
7694
7695 Set_Analyzed (Full_N);
7696
7697 else
7698 Full_Der :=
7699 Make_Defining_Identifier (Sloc (Derived_Type),
7700 Chars => Chars (Derived_Type));
7701 Set_Is_Itype (Full_Der);
7702 Set_Associated_Node_For_Itype (Full_Der, N);
7703 Set_Parent (Full_Der, N);
7704 Build_Derived_Type
7705 (N, Full_Parent, Full_Der,
7706 Is_Completion => False, Derive_Subps => False);
7707 end if;
7708
7709 Set_Has_Private_Declaration (Full_Der);
7710 Set_Has_Private_Declaration (Derived_Type);
7711
7712 Set_Scope (Full_Der, Scope (Derived_Type));
7713 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7714 Set_Has_Size_Clause (Full_Der, False);
7715 Set_Has_Alignment_Clause (Full_Der, False);
7716 Set_Has_Delayed_Freeze (Full_Der);
7717 Set_Is_Frozen (Full_Der, False);
7718 Set_Freeze_Node (Full_Der, Empty);
7719 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7720 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7721
7722 -- The convention on the base type may be set in the private part
7723 -- and not propagated to the subtype until later, so we obtain the
7724 -- convention from the base type of the parent.
7725
7726 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7727 end Copy_And_Build;
7728
7729 -- Start of processing for Build_Derived_Private_Type
7730
7731 begin
7732 if Is_Tagged_Type (Parent_Type) then
7733 Full_P := Full_View (Parent_Type);
7734
7735 -- A type extension of a type with unknown discriminants is an
7736 -- indefinite type that the back-end cannot handle directly.
7737 -- We treat it as a private type, and build a completion that is
7738 -- derived from the full view of the parent, and hopefully has
7739 -- known discriminants.
7740
7741 -- If the full view of the parent type has an underlying record view,
7742 -- use it to generate the underlying record view of this derived type
7743 -- (required for chains of derivations with unknown discriminants).
7744
7745 -- Minor optimization: we avoid the generation of useless underlying
7746 -- record view entities if the private type declaration has unknown
7747 -- discriminants but its corresponding full view has no
7748 -- discriminants.
7749
7750 if Has_Unknown_Discriminants (Parent_Type)
7751 and then Present (Full_P)
7752 and then (Has_Discriminants (Full_P)
7753 or else Present (Underlying_Record_View (Full_P)))
7754 and then not In_Open_Scopes (Par_Scope)
7755 and then Expander_Active
7756 then
7757 declare
7758 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7759 New_Ext : constant Node_Id :=
7760 Copy_Separate_Tree
7761 (Record_Extension_Part (Type_Definition (N)));
7762 Decl : Node_Id;
7763
7764 begin
7765 Build_Derived_Record_Type
7766 (N, Parent_Type, Derived_Type, Derive_Subps);
7767
7768 -- Build anonymous completion, as a derivation from the full
7769 -- view of the parent. This is not a completion in the usual
7770 -- sense, because the current type is not private.
7771
7772 Decl :=
7773 Make_Full_Type_Declaration (Loc,
7774 Defining_Identifier => Full_Der,
7775 Type_Definition =>
7776 Make_Derived_Type_Definition (Loc,
7777 Subtype_Indication =>
7778 New_Copy_Tree
7779 (Subtype_Indication (Type_Definition (N))),
7780 Record_Extension_Part => New_Ext));
7781
7782 -- If the parent type has an underlying record view, use it
7783 -- here to build the new underlying record view.
7784
7785 if Present (Underlying_Record_View (Full_P)) then
7786 pragma Assert
7787 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7788 = N_Identifier);
7789 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7790 Underlying_Record_View (Full_P));
7791 end if;
7792
7793 Install_Private_Declarations (Par_Scope);
7794 Install_Visible_Declarations (Par_Scope);
7795 Insert_Before (N, Decl);
7796
7797 -- Mark entity as an underlying record view before analysis,
7798 -- to avoid generating the list of its primitive operations
7799 -- (which is not really required for this entity) and thus
7800 -- prevent spurious errors associated with missing overriding
7801 -- of abstract primitives (overridden only for Derived_Type).
7802
7803 Set_Ekind (Full_Der, E_Record_Type);
7804 Set_Is_Underlying_Record_View (Full_Der);
7805 Set_Default_SSO (Full_Der);
7806 Set_No_Reordering (Full_Der, No_Component_Reordering);
7807
7808 Analyze (Decl);
7809
7810 pragma Assert (Has_Discriminants (Full_Der)
7811 and then not Has_Unknown_Discriminants (Full_Der));
7812
7813 Uninstall_Declarations (Par_Scope);
7814
7815 -- Freeze the underlying record view, to prevent generation of
7816 -- useless dispatching information, which is simply shared with
7817 -- the real derived type.
7818
7819 Set_Is_Frozen (Full_Der);
7820
7821 -- If the derived type has access discriminants, create
7822 -- references to their anonymous types now, to prevent
7823 -- back-end problems when their first use is in generated
7824 -- bodies of primitives.
7825
7826 declare
7827 E : Entity_Id;
7828
7829 begin
7830 E := First_Entity (Full_Der);
7831
7832 while Present (E) loop
7833 if Ekind (E) = E_Discriminant
7834 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7835 then
7836 Build_Itype_Reference (Etype (E), Decl);
7837 end if;
7838
7839 Next_Entity (E);
7840 end loop;
7841 end;
7842
7843 -- Set up links between real entity and underlying record view
7844
7845 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7846 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7847 end;
7848
7849 -- If discriminants are known, build derived record
7850
7851 else
7852 Build_Derived_Record_Type
7853 (N, Parent_Type, Derived_Type, Derive_Subps);
7854 end if;
7855
7856 return;
7857
7858 elsif Has_Discriminants (Parent_Type) then
7859
7860 -- Build partial view of derived type from partial view of parent.
7861 -- This must be done before building the full derivation because the
7862 -- second derivation will modify the discriminants of the first and
7863 -- the discriminants are chained with the rest of the components in
7864 -- the full derivation.
7865
7866 Build_Derived_Record_Type
7867 (N, Parent_Type, Derived_Type, Derive_Subps);
7868
7869 -- Build the full derivation if this is not the anonymous derived
7870 -- base type created by Build_Derived_Record_Type in the constrained
7871 -- case (see point 5. of its head comment) since we build it for the
7872 -- derived subtype. And skip it for synchronized types altogether, as
7873 -- gigi does not use these types directly.
7874
7875 if Present (Full_View (Parent_Type))
7876 and then not Is_Itype (Derived_Type)
7877 and then not Is_Concurrent_Type (Full_View (Parent_Type))
7878 then
7879 declare
7880 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7881 Discr : Entity_Id;
7882 Last_Discr : Entity_Id;
7883
7884 begin
7885 -- If this is not a completion, construct the implicit full
7886 -- view by deriving from the full view of the parent type.
7887 -- But if this is a completion, the derived private type
7888 -- being built is a full view and the full derivation can
7889 -- only be its underlying full view.
7890
7891 Build_Full_Derivation;
7892
7893 if not Is_Completion then
7894 Set_Full_View (Derived_Type, Full_Der);
7895 else
7896 Set_Underlying_Full_View (Derived_Type, Full_Der);
7897 Set_Is_Underlying_Full_View (Full_Der);
7898 end if;
7899
7900 if not Is_Base_Type (Derived_Type) then
7901 Set_Full_View (Der_Base, Base_Type (Full_Der));
7902 end if;
7903
7904 -- Copy the discriminant list from full view to the partial
7905 -- view (base type and its subtype). Gigi requires that the
7906 -- partial and full views have the same discriminants.
7907
7908 -- Note that since the partial view points to discriminants
7909 -- in the full view, their scope will be that of the full
7910 -- view. This might cause some front end problems and need
7911 -- adjustment???
7912
7913 Discr := First_Discriminant (Base_Type (Full_Der));
7914 Set_First_Entity (Der_Base, Discr);
7915
7916 loop
7917 Last_Discr := Discr;
7918 Next_Discriminant (Discr);
7919 exit when No (Discr);
7920 end loop;
7921
7922 Set_Last_Entity (Der_Base, Last_Discr);
7923 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7924 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7925 end;
7926 end if;
7927
7928 elsif Present (Full_View (Parent_Type))
7929 and then Has_Discriminants (Full_View (Parent_Type))
7930 then
7931 if Has_Unknown_Discriminants (Parent_Type)
7932 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7933 N_Subtype_Indication
7934 then
7935 Error_Msg_N
7936 ("cannot constrain type with unknown discriminants",
7937 Subtype_Indication (Type_Definition (N)));
7938 return;
7939 end if;
7940
7941 -- If this is not a completion, construct the implicit full view by
7942 -- deriving from the full view of the parent type. But if this is a
7943 -- completion, the derived private type being built is a full view
7944 -- and the full derivation can only be its underlying full view.
7945
7946 Build_Full_Derivation;
7947
7948 if not Is_Completion then
7949 Set_Full_View (Derived_Type, Full_Der);
7950 else
7951 Set_Underlying_Full_View (Derived_Type, Full_Der);
7952 Set_Is_Underlying_Full_View (Full_Der);
7953 end if;
7954
7955 -- In any case, the primitive operations are inherited from the
7956 -- parent type, not from the internal full view.
7957
7958 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7959
7960 if Derive_Subps then
7961 Derive_Subprograms (Parent_Type, Derived_Type);
7962 end if;
7963
7964 Set_Stored_Constraint (Derived_Type, No_Elist);
7965 Set_Is_Constrained
7966 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7967
7968 else
7969 -- Untagged type, No discriminants on either view
7970
7971 if Nkind (Subtype_Indication (Type_Definition (N))) =
7972 N_Subtype_Indication
7973 then
7974 Error_Msg_N
7975 ("illegal constraint on type without discriminants", N);
7976 end if;
7977
7978 if Present (Discriminant_Specifications (N))
7979 and then Present (Full_View (Parent_Type))
7980 and then not Is_Tagged_Type (Full_View (Parent_Type))
7981 then
7982 Error_Msg_N ("cannot add discriminants to untagged type", N);
7983 end if;
7984
7985 Set_Stored_Constraint (Derived_Type, No_Elist);
7986 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7987
7988 Set_Is_Controlled_Active
7989 (Derived_Type, Is_Controlled_Active (Parent_Type));
7990
7991 Set_Disable_Controlled
7992 (Derived_Type, Disable_Controlled (Parent_Type));
7993
7994 Set_Has_Controlled_Component
7995 (Derived_Type, Has_Controlled_Component (Parent_Type));
7996
7997 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7998
7999 if not Is_Controlled (Parent_Type) then
8000 Set_Finalize_Storage_Only
8001 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8002 end if;
8003
8004 -- If this is not a completion, construct the implicit full view by
8005 -- deriving from the full view of the parent type.
8006
8007 -- ??? If the parent is untagged private and its completion is
8008 -- tagged, this mechanism will not work because we cannot derive from
8009 -- the tagged full view unless we have an extension.
8010
8011 if Present (Full_View (Parent_Type))
8012 and then not Is_Tagged_Type (Full_View (Parent_Type))
8013 and then not Is_Completion
8014 then
8015 Build_Full_Derivation;
8016 Set_Full_View (Derived_Type, Full_Der);
8017 end if;
8018 end if;
8019
8020 Set_Has_Unknown_Discriminants (Derived_Type,
8021 Has_Unknown_Discriminants (Parent_Type));
8022
8023 if Is_Private_Type (Derived_Type) then
8024 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8025 end if;
8026
8027 -- If the parent base type is in scope, add the derived type to its
8028 -- list of private dependents, because its full view may become
8029 -- visible subsequently (in a nested private part, a body, or in a
8030 -- further child unit).
8031
8032 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8033 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8034
8035 -- Check for unusual case where a type completed by a private
8036 -- derivation occurs within a package nested in a child unit, and
8037 -- the parent is declared in an ancestor.
8038
8039 if Is_Child_Unit (Scope (Current_Scope))
8040 and then Is_Completion
8041 and then In_Private_Part (Current_Scope)
8042 and then Scope (Parent_Type) /= Current_Scope
8043
8044 -- Note that if the parent has a completion in the private part,
8045 -- (which is itself a derivation from some other private type)
8046 -- it is that completion that is visible, there is no full view
8047 -- available, and no special processing is needed.
8048
8049 and then Present (Full_View (Parent_Type))
8050 then
8051 -- In this case, the full view of the parent type will become
8052 -- visible in the body of the enclosing child, and only then will
8053 -- the current type be possibly non-private. Build an underlying
8054 -- full view that will be installed when the enclosing child body
8055 -- is compiled.
8056
8057 if Present (Underlying_Full_View (Derived_Type)) then
8058 Full_Der := Underlying_Full_View (Derived_Type);
8059 else
8060 Build_Full_Derivation;
8061 Set_Underlying_Full_View (Derived_Type, Full_Der);
8062 Set_Is_Underlying_Full_View (Full_Der);
8063 end if;
8064
8065 -- The full view will be used to swap entities on entry/exit to
8066 -- the body, and must appear in the entity list for the package.
8067
8068 Append_Entity (Full_Der, Scope (Derived_Type));
8069 end if;
8070 end if;
8071 end Build_Derived_Private_Type;
8072
8073 -------------------------------
8074 -- Build_Derived_Record_Type --
8075 -------------------------------
8076
8077 -- 1. INTRODUCTION
8078
8079 -- Ideally we would like to use the same model of type derivation for
8080 -- tagged and untagged record types. Unfortunately this is not quite
8081 -- possible because the semantics of representation clauses is different
8082 -- for tagged and untagged records under inheritance. Consider the
8083 -- following:
8084
8085 -- type R (...) is [tagged] record ... end record;
8086 -- type T (...) is new R (...) [with ...];
8087
8088 -- The representation clauses for T can specify a completely different
8089 -- record layout from R's. Hence the same component can be placed in two
8090 -- very different positions in objects of type T and R. If R and T are
8091 -- tagged types, representation clauses for T can only specify the layout
8092 -- of non inherited components, thus components that are common in R and T
8093 -- have the same position in objects of type R and T.
8094
8095 -- This has two implications. The first is that the entire tree for R's
8096 -- declaration needs to be copied for T in the untagged case, so that T
8097 -- can be viewed as a record type of its own with its own representation
8098 -- clauses. The second implication is the way we handle discriminants.
8099 -- Specifically, in the untagged case we need a way to communicate to Gigi
8100 -- what are the real discriminants in the record, while for the semantics
8101 -- we need to consider those introduced by the user to rename the
8102 -- discriminants in the parent type. This is handled by introducing the
8103 -- notion of stored discriminants. See below for more.
8104
8105 -- Fortunately the way regular components are inherited can be handled in
8106 -- the same way in tagged and untagged types.
8107
8108 -- To complicate things a bit more the private view of a private extension
8109 -- cannot be handled in the same way as the full view (for one thing the
8110 -- semantic rules are somewhat different). We will explain what differs
8111 -- below.
8112
8113 -- 2. DISCRIMINANTS UNDER INHERITANCE
8114
8115 -- The semantic rules governing the discriminants of derived types are
8116 -- quite subtle.
8117
8118 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8119 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8120
8121 -- If parent type has discriminants, then the discriminants that are
8122 -- declared in the derived type are [3.4 (11)]:
8123
8124 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8125 -- there is one;
8126
8127 -- o Otherwise, each discriminant of the parent type (implicitly declared
8128 -- in the same order with the same specifications). In this case, the
8129 -- discriminants are said to be "inherited", or if unknown in the parent
8130 -- are also unknown in the derived type.
8131
8132 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8133
8134 -- o The parent subtype must be constrained;
8135
8136 -- o If the parent type is not a tagged type, then each discriminant of
8137 -- the derived type must be used in the constraint defining a parent
8138 -- subtype. [Implementation note: This ensures that the new discriminant
8139 -- can share storage with an existing discriminant.]
8140
8141 -- For the derived type each discriminant of the parent type is either
8142 -- inherited, constrained to equal some new discriminant of the derived
8143 -- type, or constrained to the value of an expression.
8144
8145 -- When inherited or constrained to equal some new discriminant, the
8146 -- parent discriminant and the discriminant of the derived type are said
8147 -- to "correspond".
8148
8149 -- If a discriminant of the parent type is constrained to a specific value
8150 -- in the derived type definition, then the discriminant is said to be
8151 -- "specified" by that derived type definition.
8152
8153 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8154
8155 -- We have spoken about stored discriminants in point 1 (introduction)
8156 -- above. There are two sorts of stored discriminants: implicit and
8157 -- explicit. As long as the derived type inherits the same discriminants as
8158 -- the root record type, stored discriminants are the same as regular
8159 -- discriminants, and are said to be implicit. However, if any discriminant
8160 -- in the root type was renamed in the derived type, then the derived
8161 -- type will contain explicit stored discriminants. Explicit stored
8162 -- discriminants are discriminants in addition to the semantically visible
8163 -- discriminants defined for the derived type. Stored discriminants are
8164 -- used by Gigi to figure out what are the physical discriminants in
8165 -- objects of the derived type (see precise definition in einfo.ads).
8166 -- As an example, consider the following:
8167
8168 -- type R (D1, D2, D3 : Int) is record ... end record;
8169 -- type T1 is new R;
8170 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8171 -- type T3 is new T2;
8172 -- type T4 (Y : Int) is new T3 (Y, 99);
8173
8174 -- The following table summarizes the discriminants and stored
8175 -- discriminants in R and T1 through T4:
8176
8177 -- Type Discrim Stored Discrim Comment
8178 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8179 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8180 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8181 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8182 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8183
8184 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8185 -- find the corresponding discriminant in the parent type, while
8186 -- Original_Record_Component (abbreviated ORC below) the actual physical
8187 -- component that is renamed. Finally the field Is_Completely_Hidden
8188 -- (abbreviated ICH below) is set for all explicit stored discriminants
8189 -- (see einfo.ads for more info). For the above example this gives:
8190
8191 -- Discrim CD ORC ICH
8192 -- ^^^^^^^ ^^ ^^^ ^^^
8193 -- D1 in R empty itself no
8194 -- D2 in R empty itself no
8195 -- D3 in R empty itself no
8196
8197 -- D1 in T1 D1 in R itself no
8198 -- D2 in T1 D2 in R itself no
8199 -- D3 in T1 D3 in R itself no
8200
8201 -- X1 in T2 D3 in T1 D3 in T2 no
8202 -- X2 in T2 D1 in T1 D1 in T2 no
8203 -- D1 in T2 empty itself yes
8204 -- D2 in T2 empty itself yes
8205 -- D3 in T2 empty itself yes
8206
8207 -- X1 in T3 X1 in T2 D3 in T3 no
8208 -- X2 in T3 X2 in T2 D1 in T3 no
8209 -- D1 in T3 empty itself yes
8210 -- D2 in T3 empty itself yes
8211 -- D3 in T3 empty itself yes
8212
8213 -- Y in T4 X1 in T3 D3 in T4 no
8214 -- D1 in T4 empty itself yes
8215 -- D2 in T4 empty itself yes
8216 -- D3 in T4 empty itself yes
8217
8218 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8219
8220 -- Type derivation for tagged types is fairly straightforward. If no
8221 -- discriminants are specified by the derived type, these are inherited
8222 -- from the parent. No explicit stored discriminants are ever necessary.
8223 -- The only manipulation that is done to the tree is that of adding a
8224 -- _parent field with parent type and constrained to the same constraint
8225 -- specified for the parent in the derived type definition. For instance:
8226
8227 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8228 -- type T1 is new R with null record;
8229 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8230
8231 -- are changed into:
8232
8233 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8234 -- _parent : R (D1, D2, D3);
8235 -- end record;
8236
8237 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8238 -- _parent : T1 (X2, 88, X1);
8239 -- end record;
8240
8241 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8242 -- ORC and ICH fields are:
8243
8244 -- Discrim CD ORC ICH
8245 -- ^^^^^^^ ^^ ^^^ ^^^
8246 -- D1 in R empty itself no
8247 -- D2 in R empty itself no
8248 -- D3 in R empty itself no
8249
8250 -- D1 in T1 D1 in R D1 in R no
8251 -- D2 in T1 D2 in R D2 in R no
8252 -- D3 in T1 D3 in R D3 in R no
8253
8254 -- X1 in T2 D3 in T1 D3 in R no
8255 -- X2 in T2 D1 in T1 D1 in R no
8256
8257 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8258 --
8259 -- Regardless of whether we dealing with a tagged or untagged type
8260 -- we will transform all derived type declarations of the form
8261 --
8262 -- type T is new R (...) [with ...];
8263 -- or
8264 -- subtype S is R (...);
8265 -- type T is new S [with ...];
8266 -- into
8267 -- type BT is new R [with ...];
8268 -- subtype T is BT (...);
8269 --
8270 -- That is, the base derived type is constrained only if it has no
8271 -- discriminants. The reason for doing this is that GNAT's semantic model
8272 -- assumes that a base type with discriminants is unconstrained.
8273 --
8274 -- Note that, strictly speaking, the above transformation is not always
8275 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8276 --
8277 -- procedure B34011A is
8278 -- type REC (D : integer := 0) is record
8279 -- I : Integer;
8280 -- end record;
8281
8282 -- package P is
8283 -- type T6 is new Rec;
8284 -- function F return T6;
8285 -- end P;
8286
8287 -- use P;
8288 -- package Q6 is
8289 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8290 -- end Q6;
8291 --
8292 -- The definition of Q6.U is illegal. However transforming Q6.U into
8293
8294 -- type BaseU is new T6;
8295 -- subtype U is BaseU (Q6.F.I)
8296
8297 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8298 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8299 -- the transformation described above.
8300
8301 -- There is another instance where the above transformation is incorrect.
8302 -- Consider:
8303
8304 -- package Pack is
8305 -- type Base (D : Integer) is tagged null record;
8306 -- procedure P (X : Base);
8307
8308 -- type Der is new Base (2) with null record;
8309 -- procedure P (X : Der);
8310 -- end Pack;
8311
8312 -- Then the above transformation turns this into
8313
8314 -- type Der_Base is new Base with null record;
8315 -- -- procedure P (X : Base) is implicitly inherited here
8316 -- -- as procedure P (X : Der_Base).
8317
8318 -- subtype Der is Der_Base (2);
8319 -- procedure P (X : Der);
8320 -- -- The overriding of P (X : Der_Base) is illegal since we
8321 -- -- have a parameter conformance problem.
8322
8323 -- To get around this problem, after having semantically processed Der_Base
8324 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8325 -- Discriminant_Constraint from Der so that when parameter conformance is
8326 -- checked when P is overridden, no semantic errors are flagged.
8327
8328 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8329
8330 -- Regardless of whether we are dealing with a tagged or untagged type
8331 -- we will transform all derived type declarations of the form
8332
8333 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8334 -- type T is new R [with ...];
8335 -- into
8336 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8337
8338 -- The reason for such transformation is that it allows us to implement a
8339 -- very clean form of component inheritance as explained below.
8340
8341 -- Note that this transformation is not achieved by direct tree rewriting
8342 -- and manipulation, but rather by redoing the semantic actions that the
8343 -- above transformation will entail. This is done directly in routine
8344 -- Inherit_Components.
8345
8346 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8347
8348 -- In both tagged and untagged derived types, regular non discriminant
8349 -- components are inherited in the derived type from the parent type. In
8350 -- the absence of discriminants component, inheritance is straightforward
8351 -- as components can simply be copied from the parent.
8352
8353 -- If the parent has discriminants, inheriting components constrained with
8354 -- these discriminants requires caution. Consider the following example:
8355
8356 -- type R (D1, D2 : Positive) is [tagged] record
8357 -- S : String (D1 .. D2);
8358 -- end record;
8359
8360 -- type T1 is new R [with null record];
8361 -- type T2 (X : positive) is new R (1, X) [with null record];
8362
8363 -- As explained in 6. above, T1 is rewritten as
8364 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8365 -- which makes the treatment for T1 and T2 identical.
8366
8367 -- What we want when inheriting S, is that references to D1 and D2 in R are
8368 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8369 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8370 -- with either discriminant references in the derived type or expressions.
8371 -- This replacement is achieved as follows: before inheriting R's
8372 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8373 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8374 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8375 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8376 -- by String (1 .. X).
8377
8378 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8379
8380 -- We explain here the rules governing private type extensions relevant to
8381 -- type derivation. These rules are explained on the following example:
8382
8383 -- type D [(...)] is new A [(...)] with private; <-- partial view
8384 -- type D [(...)] is new P [(...)] with null record; <-- full view
8385
8386 -- Type A is called the ancestor subtype of the private extension.
8387 -- Type P is the parent type of the full view of the private extension. It
8388 -- must be A or a type derived from A.
8389
8390 -- The rules concerning the discriminants of private type extensions are
8391 -- [7.3(10-13)]:
8392
8393 -- o If a private extension inherits known discriminants from the ancestor
8394 -- subtype, then the full view must also inherit its discriminants from
8395 -- the ancestor subtype and the parent subtype of the full view must be
8396 -- constrained if and only if the ancestor subtype is constrained.
8397
8398 -- o If a partial view has unknown discriminants, then the full view may
8399 -- define a definite or an indefinite subtype, with or without
8400 -- discriminants.
8401
8402 -- o If a partial view has neither known nor unknown discriminants, then
8403 -- the full view must define a definite subtype.
8404
8405 -- o If the ancestor subtype of a private extension has constrained
8406 -- discriminants, then the parent subtype of the full view must impose a
8407 -- statically matching constraint on those discriminants.
8408
8409 -- This means that only the following forms of private extensions are
8410 -- allowed:
8411
8412 -- type D is new A with private; <-- partial view
8413 -- type D is new P with null record; <-- full view
8414
8415 -- If A has no discriminants than P has no discriminants, otherwise P must
8416 -- inherit A's discriminants.
8417
8418 -- type D is new A (...) with private; <-- partial view
8419 -- type D is new P (:::) with null record; <-- full view
8420
8421 -- P must inherit A's discriminants and (...) and (:::) must statically
8422 -- match.
8423
8424 -- subtype A is R (...);
8425 -- type D is new A with private; <-- partial view
8426 -- type D is new P with null record; <-- full view
8427
8428 -- P must have inherited R's discriminants and must be derived from A or
8429 -- any of its subtypes.
8430
8431 -- type D (..) is new A with private; <-- partial view
8432 -- type D (..) is new P [(:::)] with null record; <-- full view
8433
8434 -- No specific constraints on P's discriminants or constraint (:::).
8435 -- Note that A can be unconstrained, but the parent subtype P must either
8436 -- be constrained or (:::) must be present.
8437
8438 -- type D (..) is new A [(...)] with private; <-- partial view
8439 -- type D (..) is new P [(:::)] with null record; <-- full view
8440
8441 -- P's constraints on A's discriminants must statically match those
8442 -- imposed by (...).
8443
8444 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8445
8446 -- The full view of a private extension is handled exactly as described
8447 -- above. The model chose for the private view of a private extension is
8448 -- the same for what concerns discriminants (i.e. they receive the same
8449 -- treatment as in the tagged case). However, the private view of the
8450 -- private extension always inherits the components of the parent base,
8451 -- without replacing any discriminant reference. Strictly speaking this is
8452 -- incorrect. However, Gigi never uses this view to generate code so this
8453 -- is a purely semantic issue. In theory, a set of transformations similar
8454 -- to those given in 5. and 6. above could be applied to private views of
8455 -- private extensions to have the same model of component inheritance as
8456 -- for non private extensions. However, this is not done because it would
8457 -- further complicate private type processing. Semantically speaking, this
8458 -- leaves us in an uncomfortable situation. As an example consider:
8459
8460 -- package Pack is
8461 -- type R (D : integer) is tagged record
8462 -- S : String (1 .. D);
8463 -- end record;
8464 -- procedure P (X : R);
8465 -- type T is new R (1) with private;
8466 -- private
8467 -- type T is new R (1) with null record;
8468 -- end;
8469
8470 -- This is transformed into:
8471
8472 -- package Pack is
8473 -- type R (D : integer) is tagged record
8474 -- S : String (1 .. D);
8475 -- end record;
8476 -- procedure P (X : R);
8477 -- type T is new R (1) with private;
8478 -- private
8479 -- type BaseT is new R with null record;
8480 -- subtype T is BaseT (1);
8481 -- end;
8482
8483 -- (strictly speaking the above is incorrect Ada)
8484
8485 -- From the semantic standpoint the private view of private extension T
8486 -- should be flagged as constrained since one can clearly have
8487 --
8488 -- Obj : T;
8489 --
8490 -- in a unit withing Pack. However, when deriving subprograms for the
8491 -- private view of private extension T, T must be seen as unconstrained
8492 -- since T has discriminants (this is a constraint of the current
8493 -- subprogram derivation model). Thus, when processing the private view of
8494 -- a private extension such as T, we first mark T as unconstrained, we
8495 -- process it, we perform program derivation and just before returning from
8496 -- Build_Derived_Record_Type we mark T as constrained.
8497
8498 -- ??? Are there are other uncomfortable cases that we will have to
8499 -- deal with.
8500
8501 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8502
8503 -- Types that are derived from a visible record type and have a private
8504 -- extension present other peculiarities. They behave mostly like private
8505 -- types, but if they have primitive operations defined, these will not
8506 -- have the proper signatures for further inheritance, because other
8507 -- primitive operations will use the implicit base that we define for
8508 -- private derivations below. This affect subprogram inheritance (see
8509 -- Derive_Subprograms for details). We also derive the implicit base from
8510 -- the base type of the full view, so that the implicit base is a record
8511 -- type and not another private type, This avoids infinite loops.
8512
8513 procedure Build_Derived_Record_Type
8514 (N : Node_Id;
8515 Parent_Type : Entity_Id;
8516 Derived_Type : Entity_Id;
8517 Derive_Subps : Boolean := True)
8518 is
8519 Discriminant_Specs : constant Boolean :=
8520 Present (Discriminant_Specifications (N));
8521 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8522 Loc : constant Source_Ptr := Sloc (N);
8523 Private_Extension : constant Boolean :=
8524 Nkind (N) = N_Private_Extension_Declaration;
8525 Assoc_List : Elist_Id;
8526 Constraint_Present : Boolean;
8527 Constrs : Elist_Id;
8528 Discrim : Entity_Id;
8529 Indic : Node_Id;
8530 Inherit_Discrims : Boolean := False;
8531 Last_Discrim : Entity_Id;
8532 New_Base : Entity_Id;
8533 New_Decl : Node_Id;
8534 New_Discrs : Elist_Id;
8535 New_Indic : Node_Id;
8536 Parent_Base : Entity_Id;
8537 Save_Etype : Entity_Id;
8538 Save_Discr_Constr : Elist_Id;
8539 Save_Next_Entity : Entity_Id;
8540 Type_Def : Node_Id;
8541
8542 Discs : Elist_Id := New_Elmt_List;
8543 -- An empty Discs list means that there were no constraints in the
8544 -- subtype indication or that there was an error processing it.
8545
8546 begin
8547 if Ekind (Parent_Type) = E_Record_Type_With_Private
8548 and then Present (Full_View (Parent_Type))
8549 and then Has_Discriminants (Parent_Type)
8550 then
8551 Parent_Base := Base_Type (Full_View (Parent_Type));
8552 else
8553 Parent_Base := Base_Type (Parent_Type);
8554 end if;
8555
8556 -- AI05-0115: if this is a derivation from a private type in some
8557 -- other scope that may lead to invisible components for the derived
8558 -- type, mark it accordingly.
8559
8560 if Is_Private_Type (Parent_Type) then
8561 if Scope (Parent_Base) = Scope (Derived_Type) then
8562 null;
8563
8564 elsif In_Open_Scopes (Scope (Parent_Base))
8565 and then In_Private_Part (Scope (Parent_Base))
8566 then
8567 null;
8568
8569 else
8570 Set_Has_Private_Ancestor (Derived_Type);
8571 end if;
8572
8573 else
8574 Set_Has_Private_Ancestor
8575 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8576 end if;
8577
8578 -- Before we start the previously documented transformations, here is
8579 -- little fix for size and alignment of tagged types. Normally when we
8580 -- derive type D from type P, we copy the size and alignment of P as the
8581 -- default for D, and in the absence of explicit representation clauses
8582 -- for D, the size and alignment are indeed the same as the parent.
8583
8584 -- But this is wrong for tagged types, since fields may be added, and
8585 -- the default size may need to be larger, and the default alignment may
8586 -- need to be larger.
8587
8588 -- We therefore reset the size and alignment fields in the tagged case.
8589 -- Note that the size and alignment will in any case be at least as
8590 -- large as the parent type (since the derived type has a copy of the
8591 -- parent type in the _parent field)
8592
8593 -- The type is also marked as being tagged here, which is needed when
8594 -- processing components with a self-referential anonymous access type
8595 -- in the call to Check_Anonymous_Access_Components below. Note that
8596 -- this flag is also set later on for completeness.
8597
8598 if Is_Tagged then
8599 Set_Is_Tagged_Type (Derived_Type);
8600 Init_Size_Align (Derived_Type);
8601 end if;
8602
8603 -- STEP 0a: figure out what kind of derived type declaration we have
8604
8605 if Private_Extension then
8606 Type_Def := N;
8607 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8608 Set_Default_SSO (Derived_Type);
8609 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8610
8611 else
8612 Type_Def := Type_Definition (N);
8613
8614 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8615 -- Parent_Base can be a private type or private extension. However,
8616 -- for tagged types with an extension the newly added fields are
8617 -- visible and hence the Derived_Type is always an E_Record_Type.
8618 -- (except that the parent may have its own private fields).
8619 -- For untagged types we preserve the Ekind of the Parent_Base.
8620
8621 if Present (Record_Extension_Part (Type_Def)) then
8622 Set_Ekind (Derived_Type, E_Record_Type);
8623 Set_Default_SSO (Derived_Type);
8624 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8625
8626 -- Create internal access types for components with anonymous
8627 -- access types.
8628
8629 if Ada_Version >= Ada_2005 then
8630 Check_Anonymous_Access_Components
8631 (N, Derived_Type, Derived_Type,
8632 Component_List (Record_Extension_Part (Type_Def)));
8633 end if;
8634
8635 else
8636 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8637 end if;
8638 end if;
8639
8640 -- Indic can either be an N_Identifier if the subtype indication
8641 -- contains no constraint or an N_Subtype_Indication if the subtype
8642 -- indication has a constraint.
8643
8644 Indic := Subtype_Indication (Type_Def);
8645 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8646
8647 -- Check that the type has visible discriminants. The type may be
8648 -- a private type with unknown discriminants whose full view has
8649 -- discriminants which are invisible.
8650
8651 if Constraint_Present then
8652 if not Has_Discriminants (Parent_Base)
8653 or else
8654 (Has_Unknown_Discriminants (Parent_Base)
8655 and then Is_Private_Type (Parent_Base))
8656 then
8657 Error_Msg_N
8658 ("invalid constraint: type has no discriminant",
8659 Constraint (Indic));
8660
8661 Constraint_Present := False;
8662 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8663
8664 elsif Is_Constrained (Parent_Type) then
8665 Error_Msg_N
8666 ("invalid constraint: parent type is already constrained",
8667 Constraint (Indic));
8668
8669 Constraint_Present := False;
8670 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8671 end if;
8672 end if;
8673
8674 -- STEP 0b: If needed, apply transformation given in point 5. above
8675
8676 if not Private_Extension
8677 and then Has_Discriminants (Parent_Type)
8678 and then not Discriminant_Specs
8679 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8680 then
8681 -- First, we must analyze the constraint (see comment in point 5.)
8682 -- The constraint may come from the subtype indication of the full
8683 -- declaration.
8684
8685 if Constraint_Present then
8686 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8687
8688 -- If there is no explicit constraint, there might be one that is
8689 -- inherited from a constrained parent type. In that case verify that
8690 -- it conforms to the constraint in the partial view. In perverse
8691 -- cases the parent subtypes of the partial and full view can have
8692 -- different constraints.
8693
8694 elsif Present (Stored_Constraint (Parent_Type)) then
8695 New_Discrs := Stored_Constraint (Parent_Type);
8696
8697 else
8698 New_Discrs := No_Elist;
8699 end if;
8700
8701 if Has_Discriminants (Derived_Type)
8702 and then Has_Private_Declaration (Derived_Type)
8703 and then Present (Discriminant_Constraint (Derived_Type))
8704 and then Present (New_Discrs)
8705 then
8706 -- Verify that constraints of the full view statically match
8707 -- those given in the partial view.
8708
8709 declare
8710 C1, C2 : Elmt_Id;
8711
8712 begin
8713 C1 := First_Elmt (New_Discrs);
8714 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8715 while Present (C1) and then Present (C2) loop
8716 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8717 or else
8718 (Is_OK_Static_Expression (Node (C1))
8719 and then Is_OK_Static_Expression (Node (C2))
8720 and then
8721 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8722 then
8723 null;
8724
8725 else
8726 if Constraint_Present then
8727 Error_Msg_N
8728 ("constraint not conformant to previous declaration",
8729 Node (C1));
8730 else
8731 Error_Msg_N
8732 ("constraint of full view is incompatible "
8733 & "with partial view", N);
8734 end if;
8735 end if;
8736
8737 Next_Elmt (C1);
8738 Next_Elmt (C2);
8739 end loop;
8740 end;
8741 end if;
8742
8743 -- Insert and analyze the declaration for the unconstrained base type
8744
8745 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8746
8747 New_Decl :=
8748 Make_Full_Type_Declaration (Loc,
8749 Defining_Identifier => New_Base,
8750 Type_Definition =>
8751 Make_Derived_Type_Definition (Loc,
8752 Abstract_Present => Abstract_Present (Type_Def),
8753 Limited_Present => Limited_Present (Type_Def),
8754 Subtype_Indication =>
8755 New_Occurrence_Of (Parent_Base, Loc),
8756 Record_Extension_Part =>
8757 Relocate_Node (Record_Extension_Part (Type_Def)),
8758 Interface_List => Interface_List (Type_Def)));
8759
8760 Set_Parent (New_Decl, Parent (N));
8761 Mark_Rewrite_Insertion (New_Decl);
8762 Insert_Before (N, New_Decl);
8763
8764 -- In the extension case, make sure ancestor is frozen appropriately
8765 -- (see also non-discriminated case below).
8766
8767 if Present (Record_Extension_Part (Type_Def))
8768 or else Is_Interface (Parent_Base)
8769 then
8770 Freeze_Before (New_Decl, Parent_Type);
8771 end if;
8772
8773 -- Note that this call passes False for the Derive_Subps parameter
8774 -- because subprogram derivation is deferred until after creating
8775 -- the subtype (see below).
8776
8777 Build_Derived_Type
8778 (New_Decl, Parent_Base, New_Base,
8779 Is_Completion => False, Derive_Subps => False);
8780
8781 -- ??? This needs re-examination to determine whether the
8782 -- above call can simply be replaced by a call to Analyze.
8783
8784 Set_Analyzed (New_Decl);
8785
8786 -- Insert and analyze the declaration for the constrained subtype
8787
8788 if Constraint_Present then
8789 New_Indic :=
8790 Make_Subtype_Indication (Loc,
8791 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8792 Constraint => Relocate_Node (Constraint (Indic)));
8793
8794 else
8795 declare
8796 Constr_List : constant List_Id := New_List;
8797 C : Elmt_Id;
8798 Expr : Node_Id;
8799
8800 begin
8801 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8802 while Present (C) loop
8803 Expr := Node (C);
8804
8805 -- It is safe here to call New_Copy_Tree since we called
8806 -- Force_Evaluation on each constraint previously
8807 -- in Build_Discriminant_Constraints.
8808
8809 Append (New_Copy_Tree (Expr), To => Constr_List);
8810
8811 Next_Elmt (C);
8812 end loop;
8813
8814 New_Indic :=
8815 Make_Subtype_Indication (Loc,
8816 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8817 Constraint =>
8818 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8819 end;
8820 end if;
8821
8822 Rewrite (N,
8823 Make_Subtype_Declaration (Loc,
8824 Defining_Identifier => Derived_Type,
8825 Subtype_Indication => New_Indic));
8826
8827 Analyze (N);
8828
8829 -- Derivation of subprograms must be delayed until the full subtype
8830 -- has been established, to ensure proper overriding of subprograms
8831 -- inherited by full types. If the derivations occurred as part of
8832 -- the call to Build_Derived_Type above, then the check for type
8833 -- conformance would fail because earlier primitive subprograms
8834 -- could still refer to the full type prior the change to the new
8835 -- subtype and hence would not match the new base type created here.
8836 -- Subprograms are not derived, however, when Derive_Subps is False
8837 -- (since otherwise there could be redundant derivations).
8838
8839 if Derive_Subps then
8840 Derive_Subprograms (Parent_Type, Derived_Type);
8841 end if;
8842
8843 -- For tagged types the Discriminant_Constraint of the new base itype
8844 -- is inherited from the first subtype so that no subtype conformance
8845 -- problem arise when the first subtype overrides primitive
8846 -- operations inherited by the implicit base type.
8847
8848 if Is_Tagged then
8849 Set_Discriminant_Constraint
8850 (New_Base, Discriminant_Constraint (Derived_Type));
8851 end if;
8852
8853 return;
8854 end if;
8855
8856 -- If we get here Derived_Type will have no discriminants or it will be
8857 -- a discriminated unconstrained base type.
8858
8859 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8860
8861 if Is_Tagged then
8862
8863 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8864 -- The declaration of a specific descendant of an interface type
8865 -- freezes the interface type (RM 13.14).
8866
8867 if not Private_Extension or else Is_Interface (Parent_Base) then
8868 Freeze_Before (N, Parent_Type);
8869 end if;
8870
8871 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8872 -- cannot be declared at a deeper level than its parent type is
8873 -- removed. The check on derivation within a generic body is also
8874 -- relaxed, but there's a restriction that a derived tagged type
8875 -- cannot be declared in a generic body if it's derived directly
8876 -- or indirectly from a formal type of that generic.
8877
8878 if Ada_Version >= Ada_2005 then
8879 if Present (Enclosing_Generic_Body (Derived_Type)) then
8880 declare
8881 Ancestor_Type : Entity_Id;
8882
8883 begin
8884 -- Check to see if any ancestor of the derived type is a
8885 -- formal type.
8886
8887 Ancestor_Type := Parent_Type;
8888 while not Is_Generic_Type (Ancestor_Type)
8889 and then Etype (Ancestor_Type) /= Ancestor_Type
8890 loop
8891 Ancestor_Type := Etype (Ancestor_Type);
8892 end loop;
8893
8894 -- If the derived type does have a formal type as an
8895 -- ancestor, then it's an error if the derived type is
8896 -- declared within the body of the generic unit that
8897 -- declares the formal type in its generic formal part. It's
8898 -- sufficient to check whether the ancestor type is declared
8899 -- inside the same generic body as the derived type (such as
8900 -- within a nested generic spec), in which case the
8901 -- derivation is legal. If the formal type is declared
8902 -- outside of that generic body, then it's guaranteed that
8903 -- the derived type is declared within the generic body of
8904 -- the generic unit declaring the formal type.
8905
8906 if Is_Generic_Type (Ancestor_Type)
8907 and then Enclosing_Generic_Body (Ancestor_Type) /=
8908 Enclosing_Generic_Body (Derived_Type)
8909 then
8910 Error_Msg_NE
8911 ("parent type of& must not be descendant of formal type"
8912 & " of an enclosing generic body",
8913 Indic, Derived_Type);
8914 end if;
8915 end;
8916 end if;
8917
8918 elsif Type_Access_Level (Derived_Type) /=
8919 Type_Access_Level (Parent_Type)
8920 and then not Is_Generic_Type (Derived_Type)
8921 then
8922 if Is_Controlled (Parent_Type) then
8923 Error_Msg_N
8924 ("controlled type must be declared at the library level",
8925 Indic);
8926 else
8927 Error_Msg_N
8928 ("type extension at deeper accessibility level than parent",
8929 Indic);
8930 end if;
8931
8932 else
8933 declare
8934 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8935 begin
8936 if Present (GB)
8937 and then GB /= Enclosing_Generic_Body (Parent_Base)
8938 then
8939 Error_Msg_NE
8940 ("parent type of& must not be outside generic body"
8941 & " (RM 3.9.1(4))",
8942 Indic, Derived_Type);
8943 end if;
8944 end;
8945 end if;
8946 end if;
8947
8948 -- Ada 2005 (AI-251)
8949
8950 if Ada_Version >= Ada_2005 and then Is_Tagged then
8951
8952 -- "The declaration of a specific descendant of an interface type
8953 -- freezes the interface type" (RM 13.14).
8954
8955 declare
8956 Iface : Node_Id;
8957 begin
8958 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8959 Iface := First (Interface_List (Type_Def));
8960 while Present (Iface) loop
8961 Freeze_Before (N, Etype (Iface));
8962 Next (Iface);
8963 end loop;
8964 end if;
8965 end;
8966 end if;
8967
8968 -- STEP 1b : preliminary cleanup of the full view of private types
8969
8970 -- If the type is already marked as having discriminants, then it's the
8971 -- completion of a private type or private extension and we need to
8972 -- retain the discriminants from the partial view if the current
8973 -- declaration has Discriminant_Specifications so that we can verify
8974 -- conformance. However, we must remove any existing components that
8975 -- were inherited from the parent (and attached in Copy_And_Swap)
8976 -- because the full type inherits all appropriate components anyway, and
8977 -- we do not want the partial view's components interfering.
8978
8979 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8980 Discrim := First_Discriminant (Derived_Type);
8981 loop
8982 Last_Discrim := Discrim;
8983 Next_Discriminant (Discrim);
8984 exit when No (Discrim);
8985 end loop;
8986
8987 Set_Last_Entity (Derived_Type, Last_Discrim);
8988
8989 -- In all other cases wipe out the list of inherited components (even
8990 -- inherited discriminants), it will be properly rebuilt here.
8991
8992 else
8993 Set_First_Entity (Derived_Type, Empty);
8994 Set_Last_Entity (Derived_Type, Empty);
8995 end if;
8996
8997 -- STEP 1c: Initialize some flags for the Derived_Type
8998
8999 -- The following flags must be initialized here so that
9000 -- Process_Discriminants can check that discriminants of tagged types do
9001 -- not have a default initial value and that access discriminants are
9002 -- only specified for limited records. For completeness, these flags are
9003 -- also initialized along with all the other flags below.
9004
9005 -- AI-419: Limitedness is not inherited from an interface parent, so to
9006 -- be limited in that case the type must be explicitly declared as
9007 -- limited. However, task and protected interfaces are always limited.
9008
9009 if Limited_Present (Type_Def) then
9010 Set_Is_Limited_Record (Derived_Type);
9011
9012 elsif Is_Limited_Record (Parent_Type)
9013 or else (Present (Full_View (Parent_Type))
9014 and then Is_Limited_Record (Full_View (Parent_Type)))
9015 then
9016 if not Is_Interface (Parent_Type)
9017 or else Is_Synchronized_Interface (Parent_Type)
9018 or else Is_Protected_Interface (Parent_Type)
9019 or else Is_Task_Interface (Parent_Type)
9020 then
9021 Set_Is_Limited_Record (Derived_Type);
9022 end if;
9023 end if;
9024
9025 -- STEP 2a: process discriminants of derived type if any
9026
9027 Push_Scope (Derived_Type);
9028
9029 if Discriminant_Specs then
9030 Set_Has_Unknown_Discriminants (Derived_Type, False);
9031
9032 -- The following call initializes fields Has_Discriminants and
9033 -- Discriminant_Constraint, unless we are processing the completion
9034 -- of a private type declaration.
9035
9036 Check_Or_Process_Discriminants (N, Derived_Type);
9037
9038 -- For untagged types, the constraint on the Parent_Type must be
9039 -- present and is used to rename the discriminants.
9040
9041 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9042 Error_Msg_N ("untagged parent must have discriminants", Indic);
9043
9044 elsif not Is_Tagged and then not Constraint_Present then
9045 Error_Msg_N
9046 ("discriminant constraint needed for derived untagged records",
9047 Indic);
9048
9049 -- Otherwise the parent subtype must be constrained unless we have a
9050 -- private extension.
9051
9052 elsif not Constraint_Present
9053 and then not Private_Extension
9054 and then not Is_Constrained (Parent_Type)
9055 then
9056 Error_Msg_N
9057 ("unconstrained type not allowed in this context", Indic);
9058
9059 elsif Constraint_Present then
9060 -- The following call sets the field Corresponding_Discriminant
9061 -- for the discriminants in the Derived_Type.
9062
9063 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9064
9065 -- For untagged types all new discriminants must rename
9066 -- discriminants in the parent. For private extensions new
9067 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9068
9069 Discrim := First_Discriminant (Derived_Type);
9070 while Present (Discrim) loop
9071 if not Is_Tagged
9072 and then No (Corresponding_Discriminant (Discrim))
9073 then
9074 Error_Msg_N
9075 ("new discriminants must constrain old ones", Discrim);
9076
9077 elsif Private_Extension
9078 and then Present (Corresponding_Discriminant (Discrim))
9079 then
9080 Error_Msg_N
9081 ("only static constraints allowed for parent"
9082 & " discriminants in the partial view", Indic);
9083 exit;
9084 end if;
9085
9086 -- If a new discriminant is used in the constraint, then its
9087 -- subtype must be statically compatible with the parent
9088 -- discriminant's subtype (3.7(15)).
9089
9090 -- However, if the record contains an array constrained by
9091 -- the discriminant but with some different bound, the compiler
9092 -- tries to create a smaller range for the discriminant type.
9093 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9094 -- the discriminant type is a scalar type, the check must use
9095 -- the original discriminant type in the parent declaration.
9096
9097 declare
9098 Corr_Disc : constant Entity_Id :=
9099 Corresponding_Discriminant (Discrim);
9100 Disc_Type : constant Entity_Id := Etype (Discrim);
9101 Corr_Type : Entity_Id;
9102
9103 begin
9104 if Present (Corr_Disc) then
9105 if Is_Scalar_Type (Disc_Type) then
9106 Corr_Type :=
9107 Entity (Discriminant_Type (Parent (Corr_Disc)));
9108 else
9109 Corr_Type := Etype (Corr_Disc);
9110 end if;
9111
9112 if not
9113 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9114 then
9115 Error_Msg_N
9116 ("subtype must be compatible "
9117 & "with parent discriminant",
9118 Discrim);
9119 end if;
9120 end if;
9121 end;
9122
9123 Next_Discriminant (Discrim);
9124 end loop;
9125
9126 -- Check whether the constraints of the full view statically
9127 -- match those imposed by the parent subtype [7.3(13)].
9128
9129 if Present (Stored_Constraint (Derived_Type)) then
9130 declare
9131 C1, C2 : Elmt_Id;
9132
9133 begin
9134 C1 := First_Elmt (Discs);
9135 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9136 while Present (C1) and then Present (C2) loop
9137 if not
9138 Fully_Conformant_Expressions (Node (C1), Node (C2))
9139 then
9140 Error_Msg_N
9141 ("not conformant with previous declaration",
9142 Node (C1));
9143 end if;
9144
9145 Next_Elmt (C1);
9146 Next_Elmt (C2);
9147 end loop;
9148 end;
9149 end if;
9150 end if;
9151
9152 -- STEP 2b: No new discriminants, inherit discriminants if any
9153
9154 else
9155 if Private_Extension then
9156 Set_Has_Unknown_Discriminants
9157 (Derived_Type,
9158 Has_Unknown_Discriminants (Parent_Type)
9159 or else Unknown_Discriminants_Present (N));
9160
9161 -- The partial view of the parent may have unknown discriminants,
9162 -- but if the full view has discriminants and the parent type is
9163 -- in scope they must be inherited.
9164
9165 elsif Has_Unknown_Discriminants (Parent_Type)
9166 and then
9167 (not Has_Discriminants (Parent_Type)
9168 or else not In_Open_Scopes (Scope (Parent_Base)))
9169 then
9170 Set_Has_Unknown_Discriminants (Derived_Type);
9171 end if;
9172
9173 if not Has_Unknown_Discriminants (Derived_Type)
9174 and then not Has_Unknown_Discriminants (Parent_Base)
9175 and then Has_Discriminants (Parent_Type)
9176 then
9177 Inherit_Discrims := True;
9178 Set_Has_Discriminants
9179 (Derived_Type, True);
9180 Set_Discriminant_Constraint
9181 (Derived_Type, Discriminant_Constraint (Parent_Base));
9182 end if;
9183
9184 -- The following test is true for private types (remember
9185 -- transformation 5. is not applied to those) and in an error
9186 -- situation.
9187
9188 if Constraint_Present then
9189 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9190 end if;
9191
9192 -- For now mark a new derived type as constrained only if it has no
9193 -- discriminants. At the end of Build_Derived_Record_Type we properly
9194 -- set this flag in the case of private extensions. See comments in
9195 -- point 9. just before body of Build_Derived_Record_Type.
9196
9197 Set_Is_Constrained
9198 (Derived_Type,
9199 not (Inherit_Discrims
9200 or else Has_Unknown_Discriminants (Derived_Type)));
9201 end if;
9202
9203 -- STEP 3: initialize fields of derived type
9204
9205 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9206 Set_Stored_Constraint (Derived_Type, No_Elist);
9207
9208 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9209 -- but cannot be interfaces
9210
9211 if not Private_Extension
9212 and then Ekind (Derived_Type) /= E_Private_Type
9213 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9214 then
9215 if Interface_Present (Type_Def) then
9216 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9217 end if;
9218
9219 Set_Interfaces (Derived_Type, No_Elist);
9220 end if;
9221
9222 -- Fields inherited from the Parent_Type
9223
9224 Set_Has_Specified_Layout
9225 (Derived_Type, Has_Specified_Layout (Parent_Type));
9226 Set_Is_Limited_Composite
9227 (Derived_Type, Is_Limited_Composite (Parent_Type));
9228 Set_Is_Private_Composite
9229 (Derived_Type, Is_Private_Composite (Parent_Type));
9230
9231 if Is_Tagged_Type (Parent_Type) then
9232 Set_No_Tagged_Streams_Pragma
9233 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9234 end if;
9235
9236 -- Fields inherited from the Parent_Base
9237
9238 Set_Has_Controlled_Component
9239 (Derived_Type, Has_Controlled_Component (Parent_Base));
9240 Set_Has_Non_Standard_Rep
9241 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9242 Set_Has_Primitive_Operations
9243 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9244
9245 -- Set fields for private derived types
9246
9247 if Is_Private_Type (Derived_Type) then
9248 Set_Depends_On_Private (Derived_Type, True);
9249 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9250 end if;
9251
9252 -- Inherit fields for non-private types. If this is the completion of a
9253 -- derivation from a private type, the parent itself is private and the
9254 -- attributes come from its full view, which must be present.
9255
9256 if Is_Record_Type (Derived_Type) then
9257 declare
9258 Parent_Full : Entity_Id;
9259
9260 begin
9261 if Is_Private_Type (Parent_Base)
9262 and then not Is_Record_Type (Parent_Base)
9263 then
9264 Parent_Full := Full_View (Parent_Base);
9265 else
9266 Parent_Full := Parent_Base;
9267 end if;
9268
9269 Set_Component_Alignment
9270 (Derived_Type, Component_Alignment (Parent_Full));
9271 Set_C_Pass_By_Copy
9272 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9273 Set_Has_Complex_Representation
9274 (Derived_Type, Has_Complex_Representation (Parent_Full));
9275
9276 -- For untagged types, inherit the layout by default to avoid
9277 -- costly changes of representation for type conversions.
9278
9279 if not Is_Tagged then
9280 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9281 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9282 end if;
9283 end;
9284 end if;
9285
9286 -- Set fields for tagged types
9287
9288 if Is_Tagged then
9289 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9290
9291 -- All tagged types defined in Ada.Finalization are controlled
9292
9293 if Chars (Scope (Derived_Type)) = Name_Finalization
9294 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9295 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9296 then
9297 Set_Is_Controlled_Active (Derived_Type);
9298 else
9299 Set_Is_Controlled_Active
9300 (Derived_Type, Is_Controlled_Active (Parent_Base));
9301 end if;
9302
9303 -- Minor optimization: there is no need to generate the class-wide
9304 -- entity associated with an underlying record view.
9305
9306 if not Is_Underlying_Record_View (Derived_Type) then
9307 Make_Class_Wide_Type (Derived_Type);
9308 end if;
9309
9310 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9311
9312 if Has_Discriminants (Derived_Type)
9313 and then Constraint_Present
9314 then
9315 Set_Stored_Constraint
9316 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9317 end if;
9318
9319 if Ada_Version >= Ada_2005 then
9320 declare
9321 Ifaces_List : Elist_Id;
9322
9323 begin
9324 -- Checks rules 3.9.4 (13/2 and 14/2)
9325
9326 if Comes_From_Source (Derived_Type)
9327 and then not Is_Private_Type (Derived_Type)
9328 and then Is_Interface (Parent_Type)
9329 and then not Is_Interface (Derived_Type)
9330 then
9331 if Is_Task_Interface (Parent_Type) then
9332 Error_Msg_N
9333 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9334 Derived_Type);
9335
9336 elsif Is_Protected_Interface (Parent_Type) then
9337 Error_Msg_N
9338 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9339 Derived_Type);
9340 end if;
9341 end if;
9342
9343 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9344
9345 Check_Interfaces (N, Type_Def);
9346
9347 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9348 -- not already in the parents.
9349
9350 Collect_Interfaces
9351 (T => Derived_Type,
9352 Ifaces_List => Ifaces_List,
9353 Exclude_Parents => True);
9354
9355 Set_Interfaces (Derived_Type, Ifaces_List);
9356
9357 -- If the derived type is the anonymous type created for
9358 -- a declaration whose parent has a constraint, propagate
9359 -- the interface list to the source type. This must be done
9360 -- prior to the completion of the analysis of the source type
9361 -- because the components in the extension may contain current
9362 -- instances whose legality depends on some ancestor.
9363
9364 if Is_Itype (Derived_Type) then
9365 declare
9366 Def : constant Node_Id :=
9367 Associated_Node_For_Itype (Derived_Type);
9368 begin
9369 if Present (Def)
9370 and then Nkind (Def) = N_Full_Type_Declaration
9371 then
9372 Set_Interfaces
9373 (Defining_Identifier (Def), Ifaces_List);
9374 end if;
9375 end;
9376 end if;
9377
9378 -- A type extension is automatically Ghost when one of its
9379 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9380 -- also inherited when the parent type is Ghost, but this is
9381 -- done in Build_Derived_Type as the mechanism also handles
9382 -- untagged derivations.
9383
9384 if Implements_Ghost_Interface (Derived_Type) then
9385 Set_Is_Ghost_Entity (Derived_Type);
9386 end if;
9387 end;
9388 end if;
9389 end if;
9390
9391 -- STEP 4: Inherit components from the parent base and constrain them.
9392 -- Apply the second transformation described in point 6. above.
9393
9394 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9395 or else not Has_Discriminants (Parent_Type)
9396 or else not Is_Constrained (Parent_Type)
9397 then
9398 Constrs := Discs;
9399 else
9400 Constrs := Discriminant_Constraint (Parent_Type);
9401 end if;
9402
9403 Assoc_List :=
9404 Inherit_Components
9405 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9406
9407 -- STEP 5a: Copy the parent record declaration for untagged types
9408
9409 Set_Has_Implicit_Dereference
9410 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9411
9412 if not Is_Tagged then
9413
9414 -- Discriminant_Constraint (Derived_Type) has been properly
9415 -- constructed. Save it and temporarily set it to Empty because we
9416 -- do not want the call to New_Copy_Tree below to mess this list.
9417
9418 if Has_Discriminants (Derived_Type) then
9419 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9420 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9421 else
9422 Save_Discr_Constr := No_Elist;
9423 end if;
9424
9425 -- Save the Etype field of Derived_Type. It is correctly set now,
9426 -- but the call to New_Copy tree may remap it to point to itself,
9427 -- which is not what we want. Ditto for the Next_Entity field.
9428
9429 Save_Etype := Etype (Derived_Type);
9430 Save_Next_Entity := Next_Entity (Derived_Type);
9431
9432 -- Assoc_List maps all stored discriminants in the Parent_Base to
9433 -- stored discriminants in the Derived_Type. It is fundamental that
9434 -- no types or itypes with discriminants other than the stored
9435 -- discriminants appear in the entities declared inside
9436 -- Derived_Type, since the back end cannot deal with it.
9437
9438 New_Decl :=
9439 New_Copy_Tree
9440 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9441 Copy_Dimensions_Of_Components (Derived_Type);
9442
9443 -- Restore the fields saved prior to the New_Copy_Tree call
9444 -- and compute the stored constraint.
9445
9446 Set_Etype (Derived_Type, Save_Etype);
9447 Link_Entities (Derived_Type, Save_Next_Entity);
9448
9449 if Has_Discriminants (Derived_Type) then
9450 Set_Discriminant_Constraint
9451 (Derived_Type, Save_Discr_Constr);
9452 Set_Stored_Constraint
9453 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9454 Replace_Components (Derived_Type, New_Decl);
9455 end if;
9456
9457 -- Insert the new derived type declaration
9458
9459 Rewrite (N, New_Decl);
9460
9461 -- STEP 5b: Complete the processing for record extensions in generics
9462
9463 -- There is no completion for record extensions declared in the
9464 -- parameter part of a generic, so we need to complete processing for
9465 -- these generic record extensions here. The Record_Type_Definition call
9466 -- will change the Ekind of the components from E_Void to E_Component.
9467
9468 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9469 Record_Type_Definition (Empty, Derived_Type);
9470
9471 -- STEP 5c: Process the record extension for non private tagged types
9472
9473 elsif not Private_Extension then
9474 Expand_Record_Extension (Derived_Type, Type_Def);
9475
9476 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9477 -- derived type to propagate some semantic information. This led
9478 -- to other ASIS failures and has been removed.
9479
9480 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9481 -- implemented interfaces if we are in expansion mode
9482
9483 if Expander_Active
9484 and then Has_Interfaces (Derived_Type)
9485 then
9486 Add_Interface_Tag_Components (N, Derived_Type);
9487 end if;
9488
9489 -- Analyze the record extension
9490
9491 Record_Type_Definition
9492 (Record_Extension_Part (Type_Def), Derived_Type);
9493 end if;
9494
9495 End_Scope;
9496
9497 -- Nothing else to do if there is an error in the derivation.
9498 -- An unusual case: the full view may be derived from a type in an
9499 -- instance, when the partial view was used illegally as an actual
9500 -- in that instance, leading to a circular definition.
9501
9502 if Etype (Derived_Type) = Any_Type
9503 or else Etype (Parent_Type) = Derived_Type
9504 then
9505 return;
9506 end if;
9507
9508 -- Set delayed freeze and then derive subprograms, we need to do
9509 -- this in this order so that derived subprograms inherit the
9510 -- derived freeze if necessary.
9511
9512 Set_Has_Delayed_Freeze (Derived_Type);
9513
9514 if Derive_Subps then
9515 Derive_Subprograms (Parent_Type, Derived_Type);
9516 end if;
9517
9518 -- If we have a private extension which defines a constrained derived
9519 -- type mark as constrained here after we have derived subprograms. See
9520 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9521
9522 if Private_Extension and then Inherit_Discrims then
9523 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9524 Set_Is_Constrained (Derived_Type, True);
9525 Set_Discriminant_Constraint (Derived_Type, Discs);
9526
9527 elsif Is_Constrained (Parent_Type) then
9528 Set_Is_Constrained
9529 (Derived_Type, True);
9530 Set_Discriminant_Constraint
9531 (Derived_Type, Discriminant_Constraint (Parent_Type));
9532 end if;
9533 end if;
9534
9535 -- Update the class-wide type, which shares the now-completed entity
9536 -- list with its specific type. In case of underlying record views,
9537 -- we do not generate the corresponding class wide entity.
9538
9539 if Is_Tagged
9540 and then not Is_Underlying_Record_View (Derived_Type)
9541 then
9542 Set_First_Entity
9543 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9544 Set_Last_Entity
9545 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9546 end if;
9547
9548 Check_Function_Writable_Actuals (N);
9549 end Build_Derived_Record_Type;
9550
9551 ------------------------
9552 -- Build_Derived_Type --
9553 ------------------------
9554
9555 procedure Build_Derived_Type
9556 (N : Node_Id;
9557 Parent_Type : Entity_Id;
9558 Derived_Type : Entity_Id;
9559 Is_Completion : Boolean;
9560 Derive_Subps : Boolean := True)
9561 is
9562 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9563
9564 begin
9565 -- Set common attributes
9566
9567 Set_Scope (Derived_Type, Current_Scope);
9568 Set_Etype (Derived_Type, Parent_Base);
9569 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9570 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9571
9572 Set_Size_Info (Derived_Type, Parent_Type);
9573 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9574
9575 Set_Is_Controlled_Active
9576 (Derived_Type, Is_Controlled_Active (Parent_Type));
9577
9578 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9579 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9580 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9581
9582 if Is_Tagged_Type (Derived_Type) then
9583 Set_No_Tagged_Streams_Pragma
9584 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9585 end if;
9586
9587 -- If the parent has primitive routines, set the derived type link
9588
9589 if Has_Primitive_Operations (Parent_Type) then
9590 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9591 end if;
9592
9593 -- If the parent type is a private subtype, the convention on the base
9594 -- type may be set in the private part, and not propagated to the
9595 -- subtype until later, so we obtain the convention from the base type.
9596
9597 Set_Convention (Derived_Type, Convention (Parent_Base));
9598
9599 -- Set SSO default for record or array type
9600
9601 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9602 and then Is_Base_Type (Derived_Type)
9603 then
9604 Set_Default_SSO (Derived_Type);
9605 end if;
9606
9607 -- A derived type inherits the Default_Initial_Condition pragma coming
9608 -- from any parent type within the derivation chain.
9609
9610 if Has_DIC (Parent_Type) then
9611 Set_Has_Inherited_DIC (Derived_Type);
9612 end if;
9613
9614 -- A derived type inherits any class-wide invariants coming from a
9615 -- parent type or an interface. Note that the invariant procedure of
9616 -- the parent type should not be inherited because the derived type may
9617 -- define invariants of its own.
9618
9619 if not Is_Interface (Derived_Type) then
9620 if Has_Inherited_Invariants (Parent_Type)
9621 or else Has_Inheritable_Invariants (Parent_Type)
9622 then
9623 Set_Has_Inherited_Invariants (Derived_Type);
9624
9625 elsif Is_Concurrent_Type (Derived_Type)
9626 or else Is_Tagged_Type (Derived_Type)
9627 then
9628 declare
9629 Iface : Entity_Id;
9630 Ifaces : Elist_Id;
9631 Iface_Elmt : Elmt_Id;
9632
9633 begin
9634 Collect_Interfaces
9635 (T => Derived_Type,
9636 Ifaces_List => Ifaces,
9637 Exclude_Parents => True);
9638
9639 if Present (Ifaces) then
9640 Iface_Elmt := First_Elmt (Ifaces);
9641 while Present (Iface_Elmt) loop
9642 Iface := Node (Iface_Elmt);
9643
9644 if Has_Inheritable_Invariants (Iface) then
9645 Set_Has_Inherited_Invariants (Derived_Type);
9646 exit;
9647 end if;
9648
9649 Next_Elmt (Iface_Elmt);
9650 end loop;
9651 end if;
9652 end;
9653 end if;
9654 end if;
9655
9656 -- We similarly inherit predicates. Note that for scalar derived types
9657 -- the predicate is inherited from the first subtype, and not from its
9658 -- (anonymous) base type.
9659
9660 if Has_Predicates (Parent_Type)
9661 or else Has_Predicates (First_Subtype (Parent_Type))
9662 then
9663 Set_Has_Predicates (Derived_Type);
9664 end if;
9665
9666 -- The derived type inherits representation clauses from the parent
9667 -- type, and from any interfaces.
9668
9669 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9670
9671 declare
9672 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9673 begin
9674 while Present (Iface) loop
9675 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9676 Next (Iface);
9677 end loop;
9678 end;
9679
9680 -- If the parent type has delayed rep aspects, then mark the derived
9681 -- type as possibly inheriting a delayed rep aspect.
9682
9683 if Has_Delayed_Rep_Aspects (Parent_Type) then
9684 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9685 end if;
9686
9687 -- A derived type becomes Ghost when its parent type is also Ghost
9688 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9689 -- directly inherited because the Ghost policy in effect may differ.
9690
9691 if Is_Ghost_Entity (Parent_Type) then
9692 Set_Is_Ghost_Entity (Derived_Type);
9693 end if;
9694
9695 -- Type dependent processing
9696
9697 case Ekind (Parent_Type) is
9698 when Numeric_Kind =>
9699 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9700
9701 when Array_Kind =>
9702 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9703
9704 when Class_Wide_Kind
9705 | E_Record_Subtype
9706 | E_Record_Type
9707 =>
9708 Build_Derived_Record_Type
9709 (N, Parent_Type, Derived_Type, Derive_Subps);
9710 return;
9711
9712 when Enumeration_Kind =>
9713 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9714
9715 when Access_Kind =>
9716 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9717
9718 when Incomplete_Or_Private_Kind =>
9719 Build_Derived_Private_Type
9720 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9721
9722 -- For discriminated types, the derivation includes deriving
9723 -- primitive operations. For others it is done below.
9724
9725 if Is_Tagged_Type (Parent_Type)
9726 or else Has_Discriminants (Parent_Type)
9727 or else (Present (Full_View (Parent_Type))
9728 and then Has_Discriminants (Full_View (Parent_Type)))
9729 then
9730 return;
9731 end if;
9732
9733 when Concurrent_Kind =>
9734 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9735
9736 when others =>
9737 raise Program_Error;
9738 end case;
9739
9740 -- Nothing more to do if some error occurred
9741
9742 if Etype (Derived_Type) = Any_Type then
9743 return;
9744 end if;
9745
9746 -- Set delayed freeze and then derive subprograms, we need to do this
9747 -- in this order so that derived subprograms inherit the derived freeze
9748 -- if necessary.
9749
9750 Set_Has_Delayed_Freeze (Derived_Type);
9751
9752 if Derive_Subps then
9753 Derive_Subprograms (Parent_Type, Derived_Type);
9754 end if;
9755
9756 Set_Has_Primitive_Operations
9757 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9758 end Build_Derived_Type;
9759
9760 -----------------------
9761 -- Build_Discriminal --
9762 -----------------------
9763
9764 procedure Build_Discriminal (Discrim : Entity_Id) is
9765 D_Minal : Entity_Id;
9766 CR_Disc : Entity_Id;
9767
9768 begin
9769 -- A discriminal has the same name as the discriminant
9770
9771 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9772
9773 Set_Ekind (D_Minal, E_In_Parameter);
9774 Set_Mechanism (D_Minal, Default_Mechanism);
9775 Set_Etype (D_Minal, Etype (Discrim));
9776 Set_Scope (D_Minal, Current_Scope);
9777 Set_Parent (D_Minal, Parent (Discrim));
9778
9779 Set_Discriminal (Discrim, D_Minal);
9780 Set_Discriminal_Link (D_Minal, Discrim);
9781
9782 -- For task types, build at once the discriminants of the corresponding
9783 -- record, which are needed if discriminants are used in entry defaults
9784 -- and in family bounds.
9785
9786 if Is_Concurrent_Type (Current_Scope)
9787 or else
9788 Is_Limited_Type (Current_Scope)
9789 then
9790 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9791
9792 Set_Ekind (CR_Disc, E_In_Parameter);
9793 Set_Mechanism (CR_Disc, Default_Mechanism);
9794 Set_Etype (CR_Disc, Etype (Discrim));
9795 Set_Scope (CR_Disc, Current_Scope);
9796 Set_Discriminal_Link (CR_Disc, Discrim);
9797 Set_CR_Discriminant (Discrim, CR_Disc);
9798 end if;
9799 end Build_Discriminal;
9800
9801 ------------------------------------
9802 -- Build_Discriminant_Constraints --
9803 ------------------------------------
9804
9805 function Build_Discriminant_Constraints
9806 (T : Entity_Id;
9807 Def : Node_Id;
9808 Derived_Def : Boolean := False) return Elist_Id
9809 is
9810 C : constant Node_Id := Constraint (Def);
9811 Nb_Discr : constant Nat := Number_Discriminants (T);
9812
9813 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9814 -- Saves the expression corresponding to a given discriminant in T
9815
9816 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9817 -- Return the Position number within array Discr_Expr of a discriminant
9818 -- D within the discriminant list of the discriminated type T.
9819
9820 procedure Process_Discriminant_Expression
9821 (Expr : Node_Id;
9822 D : Entity_Id);
9823 -- If this is a discriminant constraint on a partial view, do not
9824 -- generate an overflow check on the discriminant expression. The check
9825 -- will be generated when constraining the full view. Otherwise the
9826 -- backend creates duplicate symbols for the temporaries corresponding
9827 -- to the expressions to be checked, causing spurious assembler errors.
9828
9829 ------------------
9830 -- Pos_Of_Discr --
9831 ------------------
9832
9833 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9834 Disc : Entity_Id;
9835
9836 begin
9837 Disc := First_Discriminant (T);
9838 for J in Discr_Expr'Range loop
9839 if Disc = D then
9840 return J;
9841 end if;
9842
9843 Next_Discriminant (Disc);
9844 end loop;
9845
9846 -- Note: Since this function is called on discriminants that are
9847 -- known to belong to the discriminated type, falling through the
9848 -- loop with no match signals an internal compiler error.
9849
9850 raise Program_Error;
9851 end Pos_Of_Discr;
9852
9853 -------------------------------------
9854 -- Process_Discriminant_Expression --
9855 -------------------------------------
9856
9857 procedure Process_Discriminant_Expression
9858 (Expr : Node_Id;
9859 D : Entity_Id)
9860 is
9861 BDT : constant Entity_Id := Base_Type (Etype (D));
9862
9863 begin
9864 -- If this is a discriminant constraint on a partial view, do
9865 -- not generate an overflow on the discriminant expression. The
9866 -- check will be generated when constraining the full view.
9867
9868 if Is_Private_Type (T)
9869 and then Present (Full_View (T))
9870 then
9871 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9872 else
9873 Analyze_And_Resolve (Expr, BDT);
9874 end if;
9875 end Process_Discriminant_Expression;
9876
9877 -- Declarations local to Build_Discriminant_Constraints
9878
9879 Discr : Entity_Id;
9880 E : Entity_Id;
9881 Elist : constant Elist_Id := New_Elmt_List;
9882
9883 Constr : Node_Id;
9884 Expr : Node_Id;
9885 Id : Node_Id;
9886 Position : Nat;
9887 Found : Boolean;
9888
9889 Discrim_Present : Boolean := False;
9890
9891 -- Start of processing for Build_Discriminant_Constraints
9892
9893 begin
9894 -- The following loop will process positional associations only.
9895 -- For a positional association, the (single) discriminant is
9896 -- implicitly specified by position, in textual order (RM 3.7.2).
9897
9898 Discr := First_Discriminant (T);
9899 Constr := First (Constraints (C));
9900 for D in Discr_Expr'Range loop
9901 exit when Nkind (Constr) = N_Discriminant_Association;
9902
9903 if No (Constr) then
9904 Error_Msg_N ("too few discriminants given in constraint", C);
9905 return New_Elmt_List;
9906
9907 elsif Nkind (Constr) = N_Range
9908 or else (Nkind (Constr) = N_Attribute_Reference
9909 and then Attribute_Name (Constr) = Name_Range)
9910 then
9911 Error_Msg_N
9912 ("a range is not a valid discriminant constraint", Constr);
9913 Discr_Expr (D) := Error;
9914
9915 elsif Nkind (Constr) = N_Subtype_Indication then
9916 Error_Msg_N
9917 ("a subtype indication is not a valid discriminant constraint",
9918 Constr);
9919 Discr_Expr (D) := Error;
9920
9921 else
9922 Process_Discriminant_Expression (Constr, Discr);
9923 Discr_Expr (D) := Constr;
9924 end if;
9925
9926 Next_Discriminant (Discr);
9927 Next (Constr);
9928 end loop;
9929
9930 if No (Discr) and then Present (Constr) then
9931 Error_Msg_N ("too many discriminants given in constraint", Constr);
9932 return New_Elmt_List;
9933 end if;
9934
9935 -- Named associations can be given in any order, but if both positional
9936 -- and named associations are used in the same discriminant constraint,
9937 -- then positional associations must occur first, at their normal
9938 -- position. Hence once a named association is used, the rest of the
9939 -- discriminant constraint must use only named associations.
9940
9941 while Present (Constr) loop
9942
9943 -- Positional association forbidden after a named association
9944
9945 if Nkind (Constr) /= N_Discriminant_Association then
9946 Error_Msg_N ("positional association follows named one", Constr);
9947 return New_Elmt_List;
9948
9949 -- Otherwise it is a named association
9950
9951 else
9952 -- E records the type of the discriminants in the named
9953 -- association. All the discriminants specified in the same name
9954 -- association must have the same type.
9955
9956 E := Empty;
9957
9958 -- Search the list of discriminants in T to see if the simple name
9959 -- given in the constraint matches any of them.
9960
9961 Id := First (Selector_Names (Constr));
9962 while Present (Id) loop
9963 Found := False;
9964
9965 -- If Original_Discriminant is present, we are processing a
9966 -- generic instantiation and this is an instance node. We need
9967 -- to find the name of the corresponding discriminant in the
9968 -- actual record type T and not the name of the discriminant in
9969 -- the generic formal. Example:
9970
9971 -- generic
9972 -- type G (D : int) is private;
9973 -- package P is
9974 -- subtype W is G (D => 1);
9975 -- end package;
9976 -- type Rec (X : int) is record ... end record;
9977 -- package Q is new P (G => Rec);
9978
9979 -- At the point of the instantiation, formal type G is Rec
9980 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9981 -- which really looks like "subtype W is Rec (D => 1);" at
9982 -- the point of instantiation, we want to find the discriminant
9983 -- that corresponds to D in Rec, i.e. X.
9984
9985 if Present (Original_Discriminant (Id))
9986 and then In_Instance
9987 then
9988 Discr := Find_Corresponding_Discriminant (Id, T);
9989 Found := True;
9990
9991 else
9992 Discr := First_Discriminant (T);
9993 while Present (Discr) loop
9994 if Chars (Discr) = Chars (Id) then
9995 Found := True;
9996 exit;
9997 end if;
9998
9999 Next_Discriminant (Discr);
10000 end loop;
10001
10002 if not Found then
10003 Error_Msg_N ("& does not match any discriminant", Id);
10004 return New_Elmt_List;
10005
10006 -- If the parent type is a generic formal, preserve the
10007 -- name of the discriminant for subsequent instances.
10008 -- see comment at the beginning of this if statement.
10009
10010 elsif Is_Generic_Type (Root_Type (T)) then
10011 Set_Original_Discriminant (Id, Discr);
10012 end if;
10013 end if;
10014
10015 Position := Pos_Of_Discr (T, Discr);
10016
10017 if Present (Discr_Expr (Position)) then
10018 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10019
10020 else
10021 -- Each discriminant specified in the same named association
10022 -- must be associated with a separate copy of the
10023 -- corresponding expression.
10024
10025 if Present (Next (Id)) then
10026 Expr := New_Copy_Tree (Expression (Constr));
10027 Set_Parent (Expr, Parent (Expression (Constr)));
10028 else
10029 Expr := Expression (Constr);
10030 end if;
10031
10032 Discr_Expr (Position) := Expr;
10033 Process_Discriminant_Expression (Expr, Discr);
10034 end if;
10035
10036 -- A discriminant association with more than one discriminant
10037 -- name is only allowed if the named discriminants are all of
10038 -- the same type (RM 3.7.1(8)).
10039
10040 if E = Empty then
10041 E := Base_Type (Etype (Discr));
10042
10043 elsif Base_Type (Etype (Discr)) /= E then
10044 Error_Msg_N
10045 ("all discriminants in an association " &
10046 "must have the same type", Id);
10047 end if;
10048
10049 Next (Id);
10050 end loop;
10051 end if;
10052
10053 Next (Constr);
10054 end loop;
10055
10056 -- A discriminant constraint must provide exactly one value for each
10057 -- discriminant of the type (RM 3.7.1(8)).
10058
10059 for J in Discr_Expr'Range loop
10060 if No (Discr_Expr (J)) then
10061 Error_Msg_N ("too few discriminants given in constraint", C);
10062 return New_Elmt_List;
10063 end if;
10064 end loop;
10065
10066 -- Determine if there are discriminant expressions in the constraint
10067
10068 for J in Discr_Expr'Range loop
10069 if Denotes_Discriminant
10070 (Discr_Expr (J), Check_Concurrent => True)
10071 then
10072 Discrim_Present := True;
10073 end if;
10074 end loop;
10075
10076 -- Build an element list consisting of the expressions given in the
10077 -- discriminant constraint and apply the appropriate checks. The list
10078 -- is constructed after resolving any named discriminant associations
10079 -- and therefore the expressions appear in the textual order of the
10080 -- discriminants.
10081
10082 Discr := First_Discriminant (T);
10083 for J in Discr_Expr'Range loop
10084 if Discr_Expr (J) /= Error then
10085 Append_Elmt (Discr_Expr (J), Elist);
10086
10087 -- If any of the discriminant constraints is given by a
10088 -- discriminant and we are in a derived type declaration we
10089 -- have a discriminant renaming. Establish link between new
10090 -- and old discriminant. The new discriminant has an implicit
10091 -- dereference if the old one does.
10092
10093 if Denotes_Discriminant (Discr_Expr (J)) then
10094 if Derived_Def then
10095 declare
10096 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10097
10098 begin
10099 Set_Corresponding_Discriminant (New_Discr, Discr);
10100 Set_Has_Implicit_Dereference (New_Discr,
10101 Has_Implicit_Dereference (Discr));
10102 end;
10103 end if;
10104
10105 -- Force the evaluation of non-discriminant expressions.
10106 -- If we have found a discriminant in the constraint 3.4(26)
10107 -- and 3.8(18) demand that no range checks are performed are
10108 -- after evaluation. If the constraint is for a component
10109 -- definition that has a per-object constraint, expressions are
10110 -- evaluated but not checked either. In all other cases perform
10111 -- a range check.
10112
10113 else
10114 if Discrim_Present then
10115 null;
10116
10117 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10118 and then Has_Per_Object_Constraint
10119 (Defining_Identifier (Parent (Parent (Def))))
10120 then
10121 null;
10122
10123 elsif Is_Access_Type (Etype (Discr)) then
10124 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10125
10126 else
10127 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10128 end if;
10129
10130 Force_Evaluation (Discr_Expr (J));
10131 end if;
10132
10133 -- Check that the designated type of an access discriminant's
10134 -- expression is not a class-wide type unless the discriminant's
10135 -- designated type is also class-wide.
10136
10137 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10138 and then not Is_Class_Wide_Type
10139 (Designated_Type (Etype (Discr)))
10140 and then Etype (Discr_Expr (J)) /= Any_Type
10141 and then Is_Class_Wide_Type
10142 (Designated_Type (Etype (Discr_Expr (J))))
10143 then
10144 Wrong_Type (Discr_Expr (J), Etype (Discr));
10145
10146 elsif Is_Access_Type (Etype (Discr))
10147 and then not Is_Access_Constant (Etype (Discr))
10148 and then Is_Access_Type (Etype (Discr_Expr (J)))
10149 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10150 then
10151 Error_Msg_NE
10152 ("constraint for discriminant& must be access to variable",
10153 Def, Discr);
10154 end if;
10155 end if;
10156
10157 Next_Discriminant (Discr);
10158 end loop;
10159
10160 return Elist;
10161 end Build_Discriminant_Constraints;
10162
10163 ---------------------------------
10164 -- Build_Discriminated_Subtype --
10165 ---------------------------------
10166
10167 procedure Build_Discriminated_Subtype
10168 (T : Entity_Id;
10169 Def_Id : Entity_Id;
10170 Elist : Elist_Id;
10171 Related_Nod : Node_Id;
10172 For_Access : Boolean := False)
10173 is
10174 Has_Discrs : constant Boolean := Has_Discriminants (T);
10175 Constrained : constant Boolean :=
10176 (Has_Discrs
10177 and then not Is_Empty_Elmt_List (Elist)
10178 and then not Is_Class_Wide_Type (T))
10179 or else Is_Constrained (T);
10180
10181 begin
10182 if Ekind (T) = E_Record_Type then
10183 if For_Access then
10184 Set_Ekind (Def_Id, E_Private_Subtype);
10185 Set_Is_For_Access_Subtype (Def_Id, True);
10186 else
10187 Set_Ekind (Def_Id, E_Record_Subtype);
10188 end if;
10189
10190 -- Inherit preelaboration flag from base, for types for which it
10191 -- may have been set: records, private types, protected types.
10192
10193 Set_Known_To_Have_Preelab_Init
10194 (Def_Id, Known_To_Have_Preelab_Init (T));
10195
10196 elsif Ekind (T) = E_Task_Type then
10197 Set_Ekind (Def_Id, E_Task_Subtype);
10198
10199 elsif Ekind (T) = E_Protected_Type then
10200 Set_Ekind (Def_Id, E_Protected_Subtype);
10201 Set_Known_To_Have_Preelab_Init
10202 (Def_Id, Known_To_Have_Preelab_Init (T));
10203
10204 elsif Is_Private_Type (T) then
10205 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10206 Set_Known_To_Have_Preelab_Init
10207 (Def_Id, Known_To_Have_Preelab_Init (T));
10208
10209 -- Private subtypes may have private dependents
10210
10211 Set_Private_Dependents (Def_Id, New_Elmt_List);
10212
10213 elsif Is_Class_Wide_Type (T) then
10214 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10215
10216 else
10217 -- Incomplete type. Attach subtype to list of dependents, to be
10218 -- completed with full view of parent type, unless is it the
10219 -- designated subtype of a record component within an init_proc.
10220 -- This last case arises for a component of an access type whose
10221 -- designated type is incomplete (e.g. a Taft Amendment type).
10222 -- The designated subtype is within an inner scope, and needs no
10223 -- elaboration, because only the access type is needed in the
10224 -- initialization procedure.
10225
10226 if Ekind (T) = E_Incomplete_Type then
10227 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10228 else
10229 Set_Ekind (Def_Id, Ekind (T));
10230 end if;
10231
10232 if For_Access and then Within_Init_Proc then
10233 null;
10234 else
10235 Append_Elmt (Def_Id, Private_Dependents (T));
10236 end if;
10237 end if;
10238
10239 Set_Etype (Def_Id, T);
10240 Init_Size_Align (Def_Id);
10241 Set_Has_Discriminants (Def_Id, Has_Discrs);
10242 Set_Is_Constrained (Def_Id, Constrained);
10243
10244 Set_First_Entity (Def_Id, First_Entity (T));
10245 Set_Last_Entity (Def_Id, Last_Entity (T));
10246 Set_Has_Implicit_Dereference
10247 (Def_Id, Has_Implicit_Dereference (T));
10248 Set_Has_Pragma_Unreferenced_Objects
10249 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10250
10251 -- If the subtype is the completion of a private declaration, there may
10252 -- have been representation clauses for the partial view, and they must
10253 -- be preserved. Build_Derived_Type chains the inherited clauses with
10254 -- the ones appearing on the extension. If this comes from a subtype
10255 -- declaration, all clauses are inherited.
10256
10257 if No (First_Rep_Item (Def_Id)) then
10258 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10259 end if;
10260
10261 if Is_Tagged_Type (T) then
10262 Set_Is_Tagged_Type (Def_Id);
10263 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10264 Make_Class_Wide_Type (Def_Id);
10265 end if;
10266
10267 Set_Stored_Constraint (Def_Id, No_Elist);
10268
10269 if Has_Discrs then
10270 Set_Discriminant_Constraint (Def_Id, Elist);
10271 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10272 end if;
10273
10274 if Is_Tagged_Type (T) then
10275
10276 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10277 -- concurrent record type (which has the list of primitive
10278 -- operations).
10279
10280 if Ada_Version >= Ada_2005
10281 and then Is_Concurrent_Type (T)
10282 then
10283 Set_Corresponding_Record_Type (Def_Id,
10284 Corresponding_Record_Type (T));
10285 else
10286 Set_Direct_Primitive_Operations (Def_Id,
10287 Direct_Primitive_Operations (T));
10288 end if;
10289
10290 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10291 end if;
10292
10293 -- Subtypes introduced by component declarations do not need to be
10294 -- marked as delayed, and do not get freeze nodes, because the semantics
10295 -- verifies that the parents of the subtypes are frozen before the
10296 -- enclosing record is frozen.
10297
10298 if not Is_Type (Scope (Def_Id)) then
10299 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10300
10301 if Is_Private_Type (T)
10302 and then Present (Full_View (T))
10303 then
10304 Conditional_Delay (Def_Id, Full_View (T));
10305 else
10306 Conditional_Delay (Def_Id, T);
10307 end if;
10308 end if;
10309
10310 if Is_Record_Type (T) then
10311 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10312
10313 if Has_Discrs
10314 and then not Is_Empty_Elmt_List (Elist)
10315 and then not For_Access
10316 then
10317 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10318
10319 elsif not For_Access then
10320 Set_Cloned_Subtype (Def_Id, T);
10321 end if;
10322 end if;
10323 end Build_Discriminated_Subtype;
10324
10325 ---------------------------
10326 -- Build_Itype_Reference --
10327 ---------------------------
10328
10329 procedure Build_Itype_Reference
10330 (Ityp : Entity_Id;
10331 Nod : Node_Id)
10332 is
10333 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10334 begin
10335
10336 -- Itype references are only created for use by the back-end
10337
10338 if Inside_A_Generic then
10339 return;
10340 else
10341 Set_Itype (IR, Ityp);
10342
10343 -- If Nod is a library unit entity, then Insert_After won't work,
10344 -- because Nod is not a member of any list. Therefore, we use
10345 -- Add_Global_Declaration in this case. This can happen if we have a
10346 -- build-in-place library function.
10347
10348 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10349 or else
10350 (Nkind (Nod) = N_Defining_Program_Unit_Name
10351 and then Is_Compilation_Unit (Defining_Identifier (Nod)))
10352 then
10353 Add_Global_Declaration (IR);
10354 else
10355 Insert_After (Nod, IR);
10356 end if;
10357 end if;
10358 end Build_Itype_Reference;
10359
10360 ------------------------
10361 -- Build_Scalar_Bound --
10362 ------------------------
10363
10364 function Build_Scalar_Bound
10365 (Bound : Node_Id;
10366 Par_T : Entity_Id;
10367 Der_T : Entity_Id) return Node_Id
10368 is
10369 New_Bound : Entity_Id;
10370
10371 begin
10372 -- Note: not clear why this is needed, how can the original bound
10373 -- be unanalyzed at this point? and if it is, what business do we
10374 -- have messing around with it? and why is the base type of the
10375 -- parent type the right type for the resolution. It probably is
10376 -- not. It is OK for the new bound we are creating, but not for
10377 -- the old one??? Still if it never happens, no problem.
10378
10379 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10380
10381 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10382 New_Bound := New_Copy (Bound);
10383 Set_Etype (New_Bound, Der_T);
10384 Set_Analyzed (New_Bound);
10385
10386 elsif Is_Entity_Name (Bound) then
10387 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10388
10389 -- The following is almost certainly wrong. What business do we have
10390 -- relocating a node (Bound) that is presumably still attached to
10391 -- the tree elsewhere???
10392
10393 else
10394 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10395 end if;
10396
10397 Set_Etype (New_Bound, Der_T);
10398 return New_Bound;
10399 end Build_Scalar_Bound;
10400
10401 --------------------------------
10402 -- Build_Underlying_Full_View --
10403 --------------------------------
10404
10405 procedure Build_Underlying_Full_View
10406 (N : Node_Id;
10407 Typ : Entity_Id;
10408 Par : Entity_Id)
10409 is
10410 Loc : constant Source_Ptr := Sloc (N);
10411 Subt : constant Entity_Id :=
10412 Make_Defining_Identifier
10413 (Loc, New_External_Name (Chars (Typ), 'S'));
10414
10415 Constr : Node_Id;
10416 Indic : Node_Id;
10417 C : Node_Id;
10418 Id : Node_Id;
10419
10420 procedure Set_Discriminant_Name (Id : Node_Id);
10421 -- If the derived type has discriminants, they may rename discriminants
10422 -- of the parent. When building the full view of the parent, we need to
10423 -- recover the names of the original discriminants if the constraint is
10424 -- given by named associations.
10425
10426 ---------------------------
10427 -- Set_Discriminant_Name --
10428 ---------------------------
10429
10430 procedure Set_Discriminant_Name (Id : Node_Id) is
10431 Disc : Entity_Id;
10432
10433 begin
10434 Set_Original_Discriminant (Id, Empty);
10435
10436 if Has_Discriminants (Typ) then
10437 Disc := First_Discriminant (Typ);
10438 while Present (Disc) loop
10439 if Chars (Disc) = Chars (Id)
10440 and then Present (Corresponding_Discriminant (Disc))
10441 then
10442 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10443 end if;
10444 Next_Discriminant (Disc);
10445 end loop;
10446 end if;
10447 end Set_Discriminant_Name;
10448
10449 -- Start of processing for Build_Underlying_Full_View
10450
10451 begin
10452 if Nkind (N) = N_Full_Type_Declaration then
10453 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10454
10455 elsif Nkind (N) = N_Subtype_Declaration then
10456 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10457
10458 elsif Nkind (N) = N_Component_Declaration then
10459 Constr :=
10460 New_Copy_Tree
10461 (Constraint (Subtype_Indication (Component_Definition (N))));
10462
10463 else
10464 raise Program_Error;
10465 end if;
10466
10467 C := First (Constraints (Constr));
10468 while Present (C) loop
10469 if Nkind (C) = N_Discriminant_Association then
10470 Id := First (Selector_Names (C));
10471 while Present (Id) loop
10472 Set_Discriminant_Name (Id);
10473 Next (Id);
10474 end loop;
10475 end if;
10476
10477 Next (C);
10478 end loop;
10479
10480 Indic :=
10481 Make_Subtype_Declaration (Loc,
10482 Defining_Identifier => Subt,
10483 Subtype_Indication =>
10484 Make_Subtype_Indication (Loc,
10485 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10486 Constraint => New_Copy_Tree (Constr)));
10487
10488 -- If this is a component subtype for an outer itype, it is not
10489 -- a list member, so simply set the parent link for analysis: if
10490 -- the enclosing type does not need to be in a declarative list,
10491 -- neither do the components.
10492
10493 if Is_List_Member (N)
10494 and then Nkind (N) /= N_Component_Declaration
10495 then
10496 Insert_Before (N, Indic);
10497 else
10498 Set_Parent (Indic, Parent (N));
10499 end if;
10500
10501 Analyze (Indic);
10502 Set_Underlying_Full_View (Typ, Full_View (Subt));
10503 Set_Is_Underlying_Full_View (Full_View (Subt));
10504 end Build_Underlying_Full_View;
10505
10506 -------------------------------
10507 -- Check_Abstract_Overriding --
10508 -------------------------------
10509
10510 procedure Check_Abstract_Overriding (T : Entity_Id) is
10511 Alias_Subp : Entity_Id;
10512 Elmt : Elmt_Id;
10513 Op_List : Elist_Id;
10514 Subp : Entity_Id;
10515 Type_Def : Node_Id;
10516
10517 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10518 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10519 -- which has pragma Implemented already set. Check whether Subp's entity
10520 -- kind conforms to the implementation kind of the overridden routine.
10521
10522 procedure Check_Pragma_Implemented
10523 (Subp : Entity_Id;
10524 Iface_Subp : Entity_Id);
10525 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10526 -- Iface_Subp and both entities have pragma Implemented already set on
10527 -- them. Check whether the two implementation kinds are conforming.
10528
10529 procedure Inherit_Pragma_Implemented
10530 (Subp : Entity_Id;
10531 Iface_Subp : Entity_Id);
10532 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10533 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10534 -- Propagate the implementation kind of Iface_Subp to Subp.
10535
10536 ------------------------------
10537 -- Check_Pragma_Implemented --
10538 ------------------------------
10539
10540 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10541 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10542 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10543 Subp_Alias : constant Entity_Id := Alias (Subp);
10544 Contr_Typ : Entity_Id;
10545 Impl_Subp : Entity_Id;
10546
10547 begin
10548 -- Subp must have an alias since it is a hidden entity used to link
10549 -- an interface subprogram to its overriding counterpart.
10550
10551 pragma Assert (Present (Subp_Alias));
10552
10553 -- Handle aliases to synchronized wrappers
10554
10555 Impl_Subp := Subp_Alias;
10556
10557 if Is_Primitive_Wrapper (Impl_Subp) then
10558 Impl_Subp := Wrapped_Entity (Impl_Subp);
10559 end if;
10560
10561 -- Extract the type of the controlling formal
10562
10563 Contr_Typ := Etype (First_Formal (Subp_Alias));
10564
10565 if Is_Concurrent_Record_Type (Contr_Typ) then
10566 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10567 end if;
10568
10569 -- An interface subprogram whose implementation kind is By_Entry must
10570 -- be implemented by an entry.
10571
10572 if Impl_Kind = Name_By_Entry
10573 and then Ekind (Impl_Subp) /= E_Entry
10574 then
10575 Error_Msg_Node_2 := Iface_Alias;
10576 Error_Msg_NE
10577 ("type & must implement abstract subprogram & with an entry",
10578 Subp_Alias, Contr_Typ);
10579
10580 elsif Impl_Kind = Name_By_Protected_Procedure then
10581
10582 -- An interface subprogram whose implementation kind is By_
10583 -- Protected_Procedure cannot be implemented by a primitive
10584 -- procedure of a task type.
10585
10586 if Ekind (Contr_Typ) /= E_Protected_Type then
10587 Error_Msg_Node_2 := Contr_Typ;
10588 Error_Msg_NE
10589 ("interface subprogram & cannot be implemented by a " &
10590 "primitive procedure of task type &", Subp_Alias,
10591 Iface_Alias);
10592
10593 -- An interface subprogram whose implementation kind is By_
10594 -- Protected_Procedure must be implemented by a procedure.
10595
10596 elsif Ekind (Impl_Subp) /= E_Procedure then
10597 Error_Msg_Node_2 := Iface_Alias;
10598 Error_Msg_NE
10599 ("type & must implement abstract subprogram & with a " &
10600 "procedure", Subp_Alias, Contr_Typ);
10601
10602 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10603 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10604 then
10605 Error_Msg_Name_1 := Impl_Kind;
10606 Error_Msg_N
10607 ("overriding operation& must have synchronization%",
10608 Subp_Alias);
10609 end if;
10610
10611 -- If primitive has Optional synchronization, overriding operation
10612 -- must match if it has an explicit synchronization..
10613
10614 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10615 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10616 then
10617 Error_Msg_Name_1 := Impl_Kind;
10618 Error_Msg_N
10619 ("overriding operation& must have syncrhonization%",
10620 Subp_Alias);
10621 end if;
10622 end Check_Pragma_Implemented;
10623
10624 ------------------------------
10625 -- Check_Pragma_Implemented --
10626 ------------------------------
10627
10628 procedure Check_Pragma_Implemented
10629 (Subp : Entity_Id;
10630 Iface_Subp : Entity_Id)
10631 is
10632 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10633 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10634
10635 begin
10636 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10637 -- and overriding subprogram are different. In general this is an
10638 -- error except when the implementation kind of the overridden
10639 -- subprograms is By_Any or Optional.
10640
10641 if Iface_Kind /= Subp_Kind
10642 and then Iface_Kind /= Name_By_Any
10643 and then Iface_Kind /= Name_Optional
10644 then
10645 if Iface_Kind = Name_By_Entry then
10646 Error_Msg_N
10647 ("incompatible implementation kind, overridden subprogram " &
10648 "is marked By_Entry", Subp);
10649 else
10650 Error_Msg_N
10651 ("incompatible implementation kind, overridden subprogram " &
10652 "is marked By_Protected_Procedure", Subp);
10653 end if;
10654 end if;
10655 end Check_Pragma_Implemented;
10656
10657 --------------------------------
10658 -- Inherit_Pragma_Implemented --
10659 --------------------------------
10660
10661 procedure Inherit_Pragma_Implemented
10662 (Subp : Entity_Id;
10663 Iface_Subp : Entity_Id)
10664 is
10665 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10666 Loc : constant Source_Ptr := Sloc (Subp);
10667 Impl_Prag : Node_Id;
10668
10669 begin
10670 -- Since the implementation kind is stored as a representation item
10671 -- rather than a flag, create a pragma node.
10672
10673 Impl_Prag :=
10674 Make_Pragma (Loc,
10675 Chars => Name_Implemented,
10676 Pragma_Argument_Associations => New_List (
10677 Make_Pragma_Argument_Association (Loc,
10678 Expression => New_Occurrence_Of (Subp, Loc)),
10679
10680 Make_Pragma_Argument_Association (Loc,
10681 Expression => Make_Identifier (Loc, Iface_Kind))));
10682
10683 -- The pragma doesn't need to be analyzed because it is internally
10684 -- built. It is safe to directly register it as a rep item since we
10685 -- are only interested in the characters of the implementation kind.
10686
10687 Record_Rep_Item (Subp, Impl_Prag);
10688 end Inherit_Pragma_Implemented;
10689
10690 -- Start of processing for Check_Abstract_Overriding
10691
10692 begin
10693 Op_List := Primitive_Operations (T);
10694
10695 -- Loop to check primitive operations
10696
10697 Elmt := First_Elmt (Op_List);
10698 while Present (Elmt) loop
10699 Subp := Node (Elmt);
10700 Alias_Subp := Alias (Subp);
10701
10702 -- Inherited subprograms are identified by the fact that they do not
10703 -- come from source, and the associated source location is the
10704 -- location of the first subtype of the derived type.
10705
10706 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10707 -- subprograms that "require overriding".
10708
10709 -- Special exception, do not complain about failure to override the
10710 -- stream routines _Input and _Output, as well as the primitive
10711 -- operations used in dispatching selects since we always provide
10712 -- automatic overridings for these subprograms.
10713
10714 -- The partial view of T may have been a private extension, for
10715 -- which inherited functions dispatching on result are abstract.
10716 -- If the full view is a null extension, there is no need for
10717 -- overriding in Ada 2005, but wrappers need to be built for them
10718 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10719
10720 if Is_Null_Extension (T)
10721 and then Has_Controlling_Result (Subp)
10722 and then Ada_Version >= Ada_2005
10723 and then Present (Alias_Subp)
10724 and then not Comes_From_Source (Subp)
10725 and then not Is_Abstract_Subprogram (Alias_Subp)
10726 and then not Is_Access_Type (Etype (Subp))
10727 then
10728 null;
10729
10730 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10731 -- processing because this check is done with the aliased
10732 -- entity
10733
10734 elsif Present (Interface_Alias (Subp)) then
10735 null;
10736
10737 elsif (Is_Abstract_Subprogram (Subp)
10738 or else Requires_Overriding (Subp)
10739 or else
10740 (Has_Controlling_Result (Subp)
10741 and then Present (Alias_Subp)
10742 and then not Comes_From_Source (Subp)
10743 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10744 and then not Is_TSS (Subp, TSS_Stream_Input)
10745 and then not Is_TSS (Subp, TSS_Stream_Output)
10746 and then not Is_Abstract_Type (T)
10747 and then not Is_Predefined_Interface_Primitive (Subp)
10748
10749 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10750 -- with abstract interface types because the check will be done
10751 -- with the aliased entity (otherwise we generate a duplicated
10752 -- error message).
10753
10754 and then not Present (Interface_Alias (Subp))
10755 then
10756 if Present (Alias_Subp) then
10757
10758 -- Only perform the check for a derived subprogram when the
10759 -- type has an explicit record extension. This avoids incorrect
10760 -- flagging of abstract subprograms for the case of a type
10761 -- without an extension that is derived from a formal type
10762 -- with a tagged actual (can occur within a private part).
10763
10764 -- Ada 2005 (AI-391): In the case of an inherited function with
10765 -- a controlling result of the type, the rule does not apply if
10766 -- the type is a null extension (unless the parent function
10767 -- itself is abstract, in which case the function must still be
10768 -- be overridden). The expander will generate an overriding
10769 -- wrapper function calling the parent subprogram (see
10770 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10771
10772 Type_Def := Type_Definition (Parent (T));
10773
10774 if Nkind (Type_Def) = N_Derived_Type_Definition
10775 and then Present (Record_Extension_Part (Type_Def))
10776 and then
10777 (Ada_Version < Ada_2005
10778 or else not Is_Null_Extension (T)
10779 or else Ekind (Subp) = E_Procedure
10780 or else not Has_Controlling_Result (Subp)
10781 or else Is_Abstract_Subprogram (Alias_Subp)
10782 or else Requires_Overriding (Subp)
10783 or else Is_Access_Type (Etype (Subp)))
10784 then
10785 -- Avoid reporting error in case of abstract predefined
10786 -- primitive inherited from interface type because the
10787 -- body of internally generated predefined primitives
10788 -- of tagged types are generated later by Freeze_Type
10789
10790 if Is_Interface (Root_Type (T))
10791 and then Is_Abstract_Subprogram (Subp)
10792 and then Is_Predefined_Dispatching_Operation (Subp)
10793 and then not Comes_From_Source (Ultimate_Alias (Subp))
10794 then
10795 null;
10796
10797 -- A null extension is not obliged to override an inherited
10798 -- procedure subject to pragma Extensions_Visible with value
10799 -- False and at least one controlling OUT parameter
10800 -- (SPARK RM 6.1.7(6)).
10801
10802 elsif Is_Null_Extension (T)
10803 and then Is_EVF_Procedure (Subp)
10804 then
10805 null;
10806
10807 else
10808 Error_Msg_NE
10809 ("type must be declared abstract or & overridden",
10810 T, Subp);
10811
10812 -- Traverse the whole chain of aliased subprograms to
10813 -- complete the error notification. This is especially
10814 -- useful for traceability of the chain of entities when
10815 -- the subprogram corresponds with an interface
10816 -- subprogram (which may be defined in another package).
10817
10818 if Present (Alias_Subp) then
10819 declare
10820 E : Entity_Id;
10821
10822 begin
10823 E := Subp;
10824 while Present (Alias (E)) loop
10825
10826 -- Avoid reporting redundant errors on entities
10827 -- inherited from interfaces
10828
10829 if Sloc (E) /= Sloc (T) then
10830 Error_Msg_Sloc := Sloc (E);
10831 Error_Msg_NE
10832 ("\& has been inherited #", T, Subp);
10833 end if;
10834
10835 E := Alias (E);
10836 end loop;
10837
10838 Error_Msg_Sloc := Sloc (E);
10839
10840 -- AI05-0068: report if there is an overriding
10841 -- non-abstract subprogram that is invisible.
10842
10843 if Is_Hidden (E)
10844 and then not Is_Abstract_Subprogram (E)
10845 then
10846 Error_Msg_NE
10847 ("\& subprogram# is not visible",
10848 T, Subp);
10849
10850 -- Clarify the case where a non-null extension must
10851 -- override inherited procedure subject to pragma
10852 -- Extensions_Visible with value False and at least
10853 -- one controlling OUT param.
10854
10855 elsif Is_EVF_Procedure (E) then
10856 Error_Msg_NE
10857 ("\& # is subject to Extensions_Visible False",
10858 T, Subp);
10859
10860 else
10861 Error_Msg_NE
10862 ("\& has been inherited from subprogram #",
10863 T, Subp);
10864 end if;
10865 end;
10866 end if;
10867 end if;
10868
10869 -- Ada 2005 (AI-345): Protected or task type implementing
10870 -- abstract interfaces.
10871
10872 elsif Is_Concurrent_Record_Type (T)
10873 and then Present (Interfaces (T))
10874 then
10875 -- There is no need to check here RM 9.4(11.9/3) since we
10876 -- are processing the corresponding record type and the
10877 -- mode of the overriding subprograms was verified by
10878 -- Check_Conformance when the corresponding concurrent
10879 -- type declaration was analyzed.
10880
10881 Error_Msg_NE
10882 ("interface subprogram & must be overridden", T, Subp);
10883
10884 -- Examine primitive operations of synchronized type to find
10885 -- homonyms that have the wrong profile.
10886
10887 declare
10888 Prim : Entity_Id;
10889
10890 begin
10891 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10892 while Present (Prim) loop
10893 if Chars (Prim) = Chars (Subp) then
10894 Error_Msg_NE
10895 ("profile is not type conformant with prefixed "
10896 & "view profile of inherited operation&",
10897 Prim, Subp);
10898 end if;
10899
10900 Next_Entity (Prim);
10901 end loop;
10902 end;
10903 end if;
10904
10905 else
10906 Error_Msg_Node_2 := T;
10907 Error_Msg_N
10908 ("abstract subprogram& not allowed for type&", Subp);
10909
10910 -- Also post unconditional warning on the type (unconditional
10911 -- so that if there are more than one of these cases, we get
10912 -- them all, and not just the first one).
10913
10914 Error_Msg_Node_2 := Subp;
10915 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10916 end if;
10917
10918 -- A subprogram subject to pragma Extensions_Visible with value
10919 -- "True" cannot override a subprogram subject to the same pragma
10920 -- with value "False" (SPARK RM 6.1.7(5)).
10921
10922 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10923 and then Present (Overridden_Operation (Subp))
10924 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10925 Extensions_Visible_False
10926 then
10927 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10928 Error_Msg_N
10929 ("subprogram & with Extensions_Visible True cannot override "
10930 & "subprogram # with Extensions_Visible False", Subp);
10931 end if;
10932
10933 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10934
10935 -- Subp is an expander-generated procedure which maps an interface
10936 -- alias to a protected wrapper. The interface alias is flagged by
10937 -- pragma Implemented. Ensure that Subp is a procedure when the
10938 -- implementation kind is By_Protected_Procedure or an entry when
10939 -- By_Entry.
10940
10941 if Ada_Version >= Ada_2012
10942 and then Is_Hidden (Subp)
10943 and then Present (Interface_Alias (Subp))
10944 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10945 then
10946 Check_Pragma_Implemented (Subp);
10947 end if;
10948
10949 -- Subp is an interface primitive which overrides another interface
10950 -- primitive marked with pragma Implemented.
10951
10952 if Ada_Version >= Ada_2012
10953 and then Present (Overridden_Operation (Subp))
10954 and then Has_Rep_Pragma
10955 (Overridden_Operation (Subp), Name_Implemented)
10956 then
10957 -- If the overriding routine is also marked by Implemented, check
10958 -- that the two implementation kinds are conforming.
10959
10960 if Has_Rep_Pragma (Subp, Name_Implemented) then
10961 Check_Pragma_Implemented
10962 (Subp => Subp,
10963 Iface_Subp => Overridden_Operation (Subp));
10964
10965 -- Otherwise the overriding routine inherits the implementation
10966 -- kind from the overridden subprogram.
10967
10968 else
10969 Inherit_Pragma_Implemented
10970 (Subp => Subp,
10971 Iface_Subp => Overridden_Operation (Subp));
10972 end if;
10973 end if;
10974
10975 -- If the operation is a wrapper for a synchronized primitive, it
10976 -- may be called indirectly through a dispatching select. We assume
10977 -- that it will be referenced elsewhere indirectly, and suppress
10978 -- warnings about an unused entity.
10979
10980 if Is_Primitive_Wrapper (Subp)
10981 and then Present (Wrapped_Entity (Subp))
10982 then
10983 Set_Referenced (Wrapped_Entity (Subp));
10984 end if;
10985
10986 Next_Elmt (Elmt);
10987 end loop;
10988 end Check_Abstract_Overriding;
10989
10990 ------------------------------------------------
10991 -- Check_Access_Discriminant_Requires_Limited --
10992 ------------------------------------------------
10993
10994 procedure Check_Access_Discriminant_Requires_Limited
10995 (D : Node_Id;
10996 Loc : Node_Id)
10997 is
10998 begin
10999 -- A discriminant_specification for an access discriminant shall appear
11000 -- only in the declaration for a task or protected type, or for a type
11001 -- with the reserved word 'limited' in its definition or in one of its
11002 -- ancestors (RM 3.7(10)).
11003
11004 -- AI-0063: The proper condition is that type must be immutably limited,
11005 -- or else be a partial view.
11006
11007 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11008 if Is_Limited_View (Current_Scope)
11009 or else
11010 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11011 and then Limited_Present (Parent (Current_Scope)))
11012 then
11013 null;
11014
11015 else
11016 Error_Msg_N
11017 ("access discriminants allowed only for limited types", Loc);
11018 end if;
11019 end if;
11020 end Check_Access_Discriminant_Requires_Limited;
11021
11022 -----------------------------------
11023 -- Check_Aliased_Component_Types --
11024 -----------------------------------
11025
11026 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11027 C : Entity_Id;
11028
11029 begin
11030 -- ??? Also need to check components of record extensions, but not
11031 -- components of protected types (which are always limited).
11032
11033 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11034 -- types to be unconstrained. This is safe because it is illegal to
11035 -- create access subtypes to such types with explicit discriminant
11036 -- constraints.
11037
11038 if not Is_Limited_Type (T) then
11039 if Ekind (T) = E_Record_Type then
11040 C := First_Component (T);
11041 while Present (C) loop
11042 if Is_Aliased (C)
11043 and then Has_Discriminants (Etype (C))
11044 and then not Is_Constrained (Etype (C))
11045 and then not In_Instance_Body
11046 and then Ada_Version < Ada_2005
11047 then
11048 Error_Msg_N
11049 ("aliased component must be constrained (RM 3.6(11))",
11050 C);
11051 end if;
11052
11053 Next_Component (C);
11054 end loop;
11055
11056 elsif Ekind (T) = E_Array_Type then
11057 if Has_Aliased_Components (T)
11058 and then Has_Discriminants (Component_Type (T))
11059 and then not Is_Constrained (Component_Type (T))
11060 and then not In_Instance_Body
11061 and then Ada_Version < Ada_2005
11062 then
11063 Error_Msg_N
11064 ("aliased component type must be constrained (RM 3.6(11))",
11065 T);
11066 end if;
11067 end if;
11068 end if;
11069 end Check_Aliased_Component_Types;
11070
11071 ---------------------------------------
11072 -- Check_Anonymous_Access_Components --
11073 ---------------------------------------
11074
11075 procedure Check_Anonymous_Access_Components
11076 (Typ_Decl : Node_Id;
11077 Typ : Entity_Id;
11078 Prev : Entity_Id;
11079 Comp_List : Node_Id)
11080 is
11081 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11082 Anon_Access : Entity_Id;
11083 Acc_Def : Node_Id;
11084 Comp : Node_Id;
11085 Comp_Def : Node_Id;
11086 Decl : Node_Id;
11087 Type_Def : Node_Id;
11088
11089 procedure Build_Incomplete_Type_Declaration;
11090 -- If the record type contains components that include an access to the
11091 -- current record, then create an incomplete type declaration for the
11092 -- record, to be used as the designated type of the anonymous access.
11093 -- This is done only once, and only if there is no previous partial
11094 -- view of the type.
11095
11096 function Designates_T (Subt : Node_Id) return Boolean;
11097 -- Check whether a node designates the enclosing record type, or 'Class
11098 -- of that type
11099
11100 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11101 -- Check whether an access definition includes a reference to
11102 -- the enclosing record type. The reference can be a subtype mark
11103 -- in the access definition itself, a 'Class attribute reference, or
11104 -- recursively a reference appearing in a parameter specification
11105 -- or result definition of an access_to_subprogram definition.
11106
11107 --------------------------------------
11108 -- Build_Incomplete_Type_Declaration --
11109 --------------------------------------
11110
11111 procedure Build_Incomplete_Type_Declaration is
11112 Decl : Node_Id;
11113 Inc_T : Entity_Id;
11114 H : Entity_Id;
11115
11116 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11117 -- it's "is new ... with record" or else "is tagged record ...".
11118
11119 Is_Tagged : constant Boolean :=
11120 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11121 and then
11122 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11123 or else
11124 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11125 and then Tagged_Present (Type_Definition (Typ_Decl)));
11126
11127 begin
11128 -- If there is a previous partial view, no need to create a new one
11129 -- If the partial view, given by Prev, is incomplete, If Prev is
11130 -- a private declaration, full declaration is flagged accordingly.
11131
11132 if Prev /= Typ then
11133 if Is_Tagged then
11134 Make_Class_Wide_Type (Prev);
11135 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11136 Set_Etype (Class_Wide_Type (Typ), Typ);
11137 end if;
11138
11139 return;
11140
11141 elsif Has_Private_Declaration (Typ) then
11142
11143 -- If we refer to T'Class inside T, and T is the completion of a
11144 -- private type, then make sure the class-wide type exists.
11145
11146 if Is_Tagged then
11147 Make_Class_Wide_Type (Typ);
11148 end if;
11149
11150 return;
11151
11152 -- If there was a previous anonymous access type, the incomplete
11153 -- type declaration will have been created already.
11154
11155 elsif Present (Current_Entity (Typ))
11156 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11157 and then Full_View (Current_Entity (Typ)) = Typ
11158 then
11159 if Is_Tagged
11160 and then Comes_From_Source (Current_Entity (Typ))
11161 and then not Is_Tagged_Type (Current_Entity (Typ))
11162 then
11163 Make_Class_Wide_Type (Typ);
11164 Error_Msg_N
11165 ("incomplete view of tagged type should be declared tagged??",
11166 Parent (Current_Entity (Typ)));
11167 end if;
11168 return;
11169
11170 else
11171 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11172 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11173
11174 -- Type has already been inserted into the current scope. Remove
11175 -- it, and add incomplete declaration for type, so that subsequent
11176 -- anonymous access types can use it. The entity is unchained from
11177 -- the homonym list and from immediate visibility. After analysis,
11178 -- the entity in the incomplete declaration becomes immediately
11179 -- visible in the record declaration that follows.
11180
11181 H := Current_Entity (Typ);
11182
11183 if H = Typ then
11184 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11185 else
11186 while Present (H)
11187 and then Homonym (H) /= Typ
11188 loop
11189 H := Homonym (Typ);
11190 end loop;
11191
11192 Set_Homonym (H, Homonym (Typ));
11193 end if;
11194
11195 Insert_Before (Typ_Decl, Decl);
11196 Analyze (Decl);
11197 Set_Full_View (Inc_T, Typ);
11198
11199 if Is_Tagged then
11200
11201 -- Create a common class-wide type for both views, and set the
11202 -- Etype of the class-wide type to the full view.
11203
11204 Make_Class_Wide_Type (Inc_T);
11205 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11206 Set_Etype (Class_Wide_Type (Typ), Typ);
11207 end if;
11208 end if;
11209 end Build_Incomplete_Type_Declaration;
11210
11211 ------------------
11212 -- Designates_T --
11213 ------------------
11214
11215 function Designates_T (Subt : Node_Id) return Boolean is
11216 Type_Id : constant Name_Id := Chars (Typ);
11217
11218 function Names_T (Nam : Node_Id) return Boolean;
11219 -- The record type has not been introduced in the current scope
11220 -- yet, so we must examine the name of the type itself, either
11221 -- an identifier T, or an expanded name of the form P.T, where
11222 -- P denotes the current scope.
11223
11224 -------------
11225 -- Names_T --
11226 -------------
11227
11228 function Names_T (Nam : Node_Id) return Boolean is
11229 begin
11230 if Nkind (Nam) = N_Identifier then
11231 return Chars (Nam) = Type_Id;
11232
11233 elsif Nkind (Nam) = N_Selected_Component then
11234 if Chars (Selector_Name (Nam)) = Type_Id then
11235 if Nkind (Prefix (Nam)) = N_Identifier then
11236 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11237
11238 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11239 return Chars (Selector_Name (Prefix (Nam))) =
11240 Chars (Current_Scope);
11241 else
11242 return False;
11243 end if;
11244
11245 else
11246 return False;
11247 end if;
11248
11249 else
11250 return False;
11251 end if;
11252 end Names_T;
11253
11254 -- Start of processing for Designates_T
11255
11256 begin
11257 if Nkind (Subt) = N_Identifier then
11258 return Chars (Subt) = Type_Id;
11259
11260 -- Reference can be through an expanded name which has not been
11261 -- analyzed yet, and which designates enclosing scopes.
11262
11263 elsif Nkind (Subt) = N_Selected_Component then
11264 if Names_T (Subt) then
11265 return True;
11266
11267 -- Otherwise it must denote an entity that is already visible.
11268 -- The access definition may name a subtype of the enclosing
11269 -- type, if there is a previous incomplete declaration for it.
11270
11271 else
11272 Find_Selected_Component (Subt);
11273 return
11274 Is_Entity_Name (Subt)
11275 and then Scope (Entity (Subt)) = Current_Scope
11276 and then
11277 (Chars (Base_Type (Entity (Subt))) = Type_Id
11278 or else
11279 (Is_Class_Wide_Type (Entity (Subt))
11280 and then
11281 Chars (Etype (Base_Type (Entity (Subt)))) =
11282 Type_Id));
11283 end if;
11284
11285 -- A reference to the current type may appear as the prefix of
11286 -- a 'Class attribute.
11287
11288 elsif Nkind (Subt) = N_Attribute_Reference
11289 and then Attribute_Name (Subt) = Name_Class
11290 then
11291 return Names_T (Prefix (Subt));
11292
11293 else
11294 return False;
11295 end if;
11296 end Designates_T;
11297
11298 ----------------
11299 -- Mentions_T --
11300 ----------------
11301
11302 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11303 Param_Spec : Node_Id;
11304
11305 Acc_Subprg : constant Node_Id :=
11306 Access_To_Subprogram_Definition (Acc_Def);
11307
11308 begin
11309 if No (Acc_Subprg) then
11310 return Designates_T (Subtype_Mark (Acc_Def));
11311 end if;
11312
11313 -- Component is an access_to_subprogram: examine its formals,
11314 -- and result definition in the case of an access_to_function.
11315
11316 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11317 while Present (Param_Spec) loop
11318 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11319 and then Mentions_T (Parameter_Type (Param_Spec))
11320 then
11321 return True;
11322
11323 elsif Designates_T (Parameter_Type (Param_Spec)) then
11324 return True;
11325 end if;
11326
11327 Next (Param_Spec);
11328 end loop;
11329
11330 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11331 if Nkind (Result_Definition (Acc_Subprg)) =
11332 N_Access_Definition
11333 then
11334 return Mentions_T (Result_Definition (Acc_Subprg));
11335 else
11336 return Designates_T (Result_Definition (Acc_Subprg));
11337 end if;
11338 end if;
11339
11340 return False;
11341 end Mentions_T;
11342
11343 -- Start of processing for Check_Anonymous_Access_Components
11344
11345 begin
11346 if No (Comp_List) then
11347 return;
11348 end if;
11349
11350 Comp := First (Component_Items (Comp_List));
11351 while Present (Comp) loop
11352 if Nkind (Comp) = N_Component_Declaration
11353 and then Present
11354 (Access_Definition (Component_Definition (Comp)))
11355 and then
11356 Mentions_T (Access_Definition (Component_Definition (Comp)))
11357 then
11358 Comp_Def := Component_Definition (Comp);
11359 Acc_Def :=
11360 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11361
11362 Build_Incomplete_Type_Declaration;
11363 Anon_Access := Make_Temporary (Loc, 'S');
11364
11365 -- Create a declaration for the anonymous access type: either
11366 -- an access_to_object or an access_to_subprogram.
11367
11368 if Present (Acc_Def) then
11369 if Nkind (Acc_Def) = N_Access_Function_Definition then
11370 Type_Def :=
11371 Make_Access_Function_Definition (Loc,
11372 Parameter_Specifications =>
11373 Parameter_Specifications (Acc_Def),
11374 Result_Definition => Result_Definition (Acc_Def));
11375 else
11376 Type_Def :=
11377 Make_Access_Procedure_Definition (Loc,
11378 Parameter_Specifications =>
11379 Parameter_Specifications (Acc_Def));
11380 end if;
11381
11382 else
11383 Type_Def :=
11384 Make_Access_To_Object_Definition (Loc,
11385 Subtype_Indication =>
11386 Relocate_Node
11387 (Subtype_Mark (Access_Definition (Comp_Def))));
11388
11389 Set_Constant_Present
11390 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11391 Set_All_Present
11392 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11393 end if;
11394
11395 Set_Null_Exclusion_Present
11396 (Type_Def,
11397 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11398
11399 Decl :=
11400 Make_Full_Type_Declaration (Loc,
11401 Defining_Identifier => Anon_Access,
11402 Type_Definition => Type_Def);
11403
11404 Insert_Before (Typ_Decl, Decl);
11405 Analyze (Decl);
11406
11407 -- If an access to subprogram, create the extra formals
11408
11409 if Present (Acc_Def) then
11410 Create_Extra_Formals (Designated_Type (Anon_Access));
11411
11412 -- If an access to object, preserve entity of designated type,
11413 -- for ASIS use, before rewriting the component definition.
11414
11415 else
11416 declare
11417 Desig : Entity_Id;
11418
11419 begin
11420 Desig := Entity (Subtype_Indication (Type_Def));
11421
11422 -- If the access definition is to the current record,
11423 -- the visible entity at this point is an incomplete
11424 -- type. Retrieve the full view to simplify ASIS queries
11425
11426 if Ekind (Desig) = E_Incomplete_Type then
11427 Desig := Full_View (Desig);
11428 end if;
11429
11430 Set_Entity
11431 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11432 end;
11433 end if;
11434
11435 Rewrite (Comp_Def,
11436 Make_Component_Definition (Loc,
11437 Subtype_Indication =>
11438 New_Occurrence_Of (Anon_Access, Loc)));
11439
11440 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11441 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11442 else
11443 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11444 end if;
11445
11446 Set_Is_Local_Anonymous_Access (Anon_Access);
11447 end if;
11448
11449 Next (Comp);
11450 end loop;
11451
11452 if Present (Variant_Part (Comp_List)) then
11453 declare
11454 V : Node_Id;
11455 begin
11456 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11457 while Present (V) loop
11458 Check_Anonymous_Access_Components
11459 (Typ_Decl, Typ, Prev, Component_List (V));
11460 Next_Non_Pragma (V);
11461 end loop;
11462 end;
11463 end if;
11464 end Check_Anonymous_Access_Components;
11465
11466 ----------------------
11467 -- Check_Completion --
11468 ----------------------
11469
11470 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11471 E : Entity_Id;
11472
11473 procedure Post_Error;
11474 -- Post error message for lack of completion for entity E
11475
11476 ----------------
11477 -- Post_Error --
11478 ----------------
11479
11480 procedure Post_Error is
11481 procedure Missing_Body;
11482 -- Output missing body message
11483
11484 ------------------
11485 -- Missing_Body --
11486 ------------------
11487
11488 procedure Missing_Body is
11489 begin
11490 -- Spec is in same unit, so we can post on spec
11491
11492 if In_Same_Source_Unit (Body_Id, E) then
11493 Error_Msg_N ("missing body for &", E);
11494
11495 -- Spec is in a separate unit, so we have to post on the body
11496
11497 else
11498 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11499 end if;
11500 end Missing_Body;
11501
11502 -- Start of processing for Post_Error
11503
11504 begin
11505 if not Comes_From_Source (E) then
11506 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11507
11508 -- It may be an anonymous protected type created for a
11509 -- single variable. Post error on variable, if present.
11510
11511 declare
11512 Var : Entity_Id;
11513
11514 begin
11515 Var := First_Entity (Current_Scope);
11516 while Present (Var) loop
11517 exit when Etype (Var) = E
11518 and then Comes_From_Source (Var);
11519
11520 Next_Entity (Var);
11521 end loop;
11522
11523 if Present (Var) then
11524 E := Var;
11525 end if;
11526 end;
11527 end if;
11528 end if;
11529
11530 -- If a generated entity has no completion, then either previous
11531 -- semantic errors have disabled the expansion phase, or else we had
11532 -- missing subunits, or else we are compiling without expansion,
11533 -- or else something is very wrong.
11534
11535 if not Comes_From_Source (E) then
11536 pragma Assert
11537 (Serious_Errors_Detected > 0
11538 or else Configurable_Run_Time_Violations > 0
11539 or else Subunits_Missing
11540 or else not Expander_Active);
11541 return;
11542
11543 -- Here for source entity
11544
11545 else
11546 -- Here if no body to post the error message, so we post the error
11547 -- on the declaration that has no completion. This is not really
11548 -- the right place to post it, think about this later ???
11549
11550 if No (Body_Id) then
11551 if Is_Type (E) then
11552 Error_Msg_NE
11553 ("missing full declaration for }", Parent (E), E);
11554 else
11555 Error_Msg_NE ("missing body for &", Parent (E), E);
11556 end if;
11557
11558 -- Package body has no completion for a declaration that appears
11559 -- in the corresponding spec. Post error on the body, with a
11560 -- reference to the non-completed declaration.
11561
11562 else
11563 Error_Msg_Sloc := Sloc (E);
11564
11565 if Is_Type (E) then
11566 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11567
11568 elsif Is_Overloadable (E)
11569 and then Current_Entity_In_Scope (E) /= E
11570 then
11571 -- It may be that the completion is mistyped and appears as
11572 -- a distinct overloading of the entity.
11573
11574 declare
11575 Candidate : constant Entity_Id :=
11576 Current_Entity_In_Scope (E);
11577 Decl : constant Node_Id :=
11578 Unit_Declaration_Node (Candidate);
11579
11580 begin
11581 if Is_Overloadable (Candidate)
11582 and then Ekind (Candidate) = Ekind (E)
11583 and then Nkind (Decl) = N_Subprogram_Body
11584 and then Acts_As_Spec (Decl)
11585 then
11586 Check_Type_Conformant (Candidate, E);
11587
11588 else
11589 Missing_Body;
11590 end if;
11591 end;
11592
11593 else
11594 Missing_Body;
11595 end if;
11596 end if;
11597 end if;
11598 end Post_Error;
11599
11600 -- Local variables
11601
11602 Pack_Id : constant Entity_Id := Current_Scope;
11603
11604 -- Start of processing for Check_Completion
11605
11606 begin
11607 E := First_Entity (Pack_Id);
11608 while Present (E) loop
11609 if Is_Intrinsic_Subprogram (E) then
11610 null;
11611
11612 -- The following situation requires special handling: a child unit
11613 -- that appears in the context clause of the body of its parent:
11614
11615 -- procedure Parent.Child (...);
11616
11617 -- with Parent.Child;
11618 -- package body Parent is
11619
11620 -- Here Parent.Child appears as a local entity, but should not be
11621 -- flagged as requiring completion, because it is a compilation
11622 -- unit.
11623
11624 -- Ignore missing completion for a subprogram that does not come from
11625 -- source (including the _Call primitive operation of RAS types,
11626 -- which has to have the flag Comes_From_Source for other purposes):
11627 -- we assume that the expander will provide the missing completion.
11628 -- In case of previous errors, other expansion actions that provide
11629 -- bodies for null procedures with not be invoked, so inhibit message
11630 -- in those cases.
11631
11632 -- Note that E_Operator is not in the list that follows, because
11633 -- this kind is reserved for predefined operators, that are
11634 -- intrinsic and do not need completion.
11635
11636 elsif Ekind_In (E, E_Function,
11637 E_Procedure,
11638 E_Generic_Function,
11639 E_Generic_Procedure)
11640 then
11641 if Has_Completion (E) then
11642 null;
11643
11644 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11645 null;
11646
11647 elsif Is_Subprogram (E)
11648 and then (not Comes_From_Source (E)
11649 or else Chars (E) = Name_uCall)
11650 then
11651 null;
11652
11653 elsif
11654 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11655 then
11656 null;
11657
11658 elsif Nkind (Parent (E)) = N_Procedure_Specification
11659 and then Null_Present (Parent (E))
11660 and then Serious_Errors_Detected > 0
11661 then
11662 null;
11663
11664 else
11665 Post_Error;
11666 end if;
11667
11668 elsif Is_Entry (E) then
11669 if not Has_Completion (E) and then
11670 (Ekind (Scope (E)) = E_Protected_Object
11671 or else Ekind (Scope (E)) = E_Protected_Type)
11672 then
11673 Post_Error;
11674 end if;
11675
11676 elsif Is_Package_Or_Generic_Package (E) then
11677 if Unit_Requires_Body (E) then
11678 if not Has_Completion (E)
11679 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11680 N_Compilation_Unit
11681 then
11682 Post_Error;
11683 end if;
11684
11685 elsif not Is_Child_Unit (E) then
11686 May_Need_Implicit_Body (E);
11687 end if;
11688
11689 -- A formal incomplete type (Ada 2012) does not require a completion;
11690 -- other incomplete type declarations do.
11691
11692 elsif Ekind (E) = E_Incomplete_Type
11693 and then No (Underlying_Type (E))
11694 and then not Is_Generic_Type (E)
11695 then
11696 Post_Error;
11697
11698 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11699 and then not Has_Completion (E)
11700 then
11701 Post_Error;
11702
11703 -- A single task declared in the current scope is a constant, verify
11704 -- that the body of its anonymous type is in the same scope. If the
11705 -- task is defined elsewhere, this may be a renaming declaration for
11706 -- which no completion is needed.
11707
11708 elsif Ekind (E) = E_Constant
11709 and then Ekind (Etype (E)) = E_Task_Type
11710 and then not Has_Completion (Etype (E))
11711 and then Scope (Etype (E)) = Current_Scope
11712 then
11713 Post_Error;
11714
11715 elsif Ekind (E) = E_Protected_Object
11716 and then not Has_Completion (Etype (E))
11717 then
11718 Post_Error;
11719
11720 elsif Ekind (E) = E_Record_Type then
11721 if Is_Tagged_Type (E) then
11722 Check_Abstract_Overriding (E);
11723 Check_Conventions (E);
11724 end if;
11725
11726 Check_Aliased_Component_Types (E);
11727
11728 elsif Ekind (E) = E_Array_Type then
11729 Check_Aliased_Component_Types (E);
11730
11731 end if;
11732
11733 Next_Entity (E);
11734 end loop;
11735 end Check_Completion;
11736
11737 ------------------------------------
11738 -- Check_CPP_Type_Has_No_Defaults --
11739 ------------------------------------
11740
11741 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11742 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11743 Clist : Node_Id;
11744 Comp : Node_Id;
11745
11746 begin
11747 -- Obtain the component list
11748
11749 if Nkind (Tdef) = N_Record_Definition then
11750 Clist := Component_List (Tdef);
11751 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11752 Clist := Component_List (Record_Extension_Part (Tdef));
11753 end if;
11754
11755 -- Check all components to ensure no default expressions
11756
11757 if Present (Clist) then
11758 Comp := First (Component_Items (Clist));
11759 while Present (Comp) loop
11760 if Present (Expression (Comp)) then
11761 Error_Msg_N
11762 ("component of imported 'C'P'P type cannot have "
11763 & "default expression", Expression (Comp));
11764 end if;
11765
11766 Next (Comp);
11767 end loop;
11768 end if;
11769 end Check_CPP_Type_Has_No_Defaults;
11770
11771 ----------------------------
11772 -- Check_Delta_Expression --
11773 ----------------------------
11774
11775 procedure Check_Delta_Expression (E : Node_Id) is
11776 begin
11777 if not (Is_Real_Type (Etype (E))) then
11778 Wrong_Type (E, Any_Real);
11779
11780 elsif not Is_OK_Static_Expression (E) then
11781 Flag_Non_Static_Expr
11782 ("non-static expression used for delta value!", E);
11783
11784 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11785 Error_Msg_N ("delta expression must be positive", E);
11786
11787 else
11788 return;
11789 end if;
11790
11791 -- If any of above errors occurred, then replace the incorrect
11792 -- expression by the real 0.1, which should prevent further errors.
11793
11794 Rewrite (E,
11795 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11796 Analyze_And_Resolve (E, Standard_Float);
11797 end Check_Delta_Expression;
11798
11799 -----------------------------
11800 -- Check_Digits_Expression --
11801 -----------------------------
11802
11803 procedure Check_Digits_Expression (E : Node_Id) is
11804 begin
11805 if not (Is_Integer_Type (Etype (E))) then
11806 Wrong_Type (E, Any_Integer);
11807
11808 elsif not Is_OK_Static_Expression (E) then
11809 Flag_Non_Static_Expr
11810 ("non-static expression used for digits value!", E);
11811
11812 elsif Expr_Value (E) <= 0 then
11813 Error_Msg_N ("digits value must be greater than zero", E);
11814
11815 else
11816 return;
11817 end if;
11818
11819 -- If any of above errors occurred, then replace the incorrect
11820 -- expression by the integer 1, which should prevent further errors.
11821
11822 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11823 Analyze_And_Resolve (E, Standard_Integer);
11824
11825 end Check_Digits_Expression;
11826
11827 --------------------------
11828 -- Check_Initialization --
11829 --------------------------
11830
11831 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11832 begin
11833 -- Special processing for limited types
11834
11835 if Is_Limited_Type (T)
11836 and then not In_Instance
11837 and then not In_Inlined_Body
11838 then
11839 if not OK_For_Limited_Init (T, Exp) then
11840
11841 -- In GNAT mode, this is just a warning, to allow it to be evilly
11842 -- turned off. Otherwise it is a real error.
11843
11844 if GNAT_Mode then
11845 Error_Msg_N
11846 ("??cannot initialize entities of limited type!", Exp);
11847
11848 elsif Ada_Version < Ada_2005 then
11849
11850 -- The side effect removal machinery may generate illegal Ada
11851 -- code to avoid the usage of access types and 'reference in
11852 -- SPARK mode. Since this is legal code with respect to theorem
11853 -- proving, do not emit the error.
11854
11855 if GNATprove_Mode
11856 and then Nkind (Exp) = N_Function_Call
11857 and then Nkind (Parent (Exp)) = N_Object_Declaration
11858 and then not Comes_From_Source
11859 (Defining_Identifier (Parent (Exp)))
11860 then
11861 null;
11862
11863 else
11864 Error_Msg_N
11865 ("cannot initialize entities of limited type", Exp);
11866 Explain_Limited_Type (T, Exp);
11867 end if;
11868
11869 else
11870 -- Specialize error message according to kind of illegal
11871 -- initial expression.
11872
11873 if Nkind (Exp) = N_Type_Conversion
11874 and then Nkind (Expression (Exp)) = N_Function_Call
11875 then
11876 -- No error for internally-generated object declarations,
11877 -- which can come from build-in-place assignment statements.
11878
11879 if Nkind (Parent (Exp)) = N_Object_Declaration
11880 and then not Comes_From_Source
11881 (Defining_Identifier (Parent (Exp)))
11882 then
11883 null;
11884
11885 else
11886 Error_Msg_N
11887 ("illegal context for call to function with limited "
11888 & "result", Exp);
11889 end if;
11890
11891 else
11892 Error_Msg_N
11893 ("initialization of limited object requires aggregate or "
11894 & "function call", Exp);
11895 end if;
11896 end if;
11897 end if;
11898 end if;
11899
11900 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11901 -- set unless we can be sure that no range check is required.
11902
11903 if (GNATprove_Mode or not Expander_Active)
11904 and then Is_Scalar_Type (T)
11905 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11906 then
11907 Set_Do_Range_Check (Exp);
11908 end if;
11909 end Check_Initialization;
11910
11911 ----------------------
11912 -- Check_Interfaces --
11913 ----------------------
11914
11915 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11916 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11917
11918 Iface : Node_Id;
11919 Iface_Def : Node_Id;
11920 Iface_Typ : Entity_Id;
11921 Parent_Node : Node_Id;
11922
11923 Is_Task : Boolean := False;
11924 -- Set True if parent type or any progenitor is a task interface
11925
11926 Is_Protected : Boolean := False;
11927 -- Set True if parent type or any progenitor is a protected interface
11928
11929 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11930 -- Check that a progenitor is compatible with declaration. If an error
11931 -- message is output, it is posted on Error_Node.
11932
11933 ------------------
11934 -- Check_Ifaces --
11935 ------------------
11936
11937 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11938 Iface_Id : constant Entity_Id :=
11939 Defining_Identifier (Parent (Iface_Def));
11940 Type_Def : Node_Id;
11941
11942 begin
11943 if Nkind (N) = N_Private_Extension_Declaration then
11944 Type_Def := N;
11945 else
11946 Type_Def := Type_Definition (N);
11947 end if;
11948
11949 if Is_Task_Interface (Iface_Id) then
11950 Is_Task := True;
11951
11952 elsif Is_Protected_Interface (Iface_Id) then
11953 Is_Protected := True;
11954 end if;
11955
11956 if Is_Synchronized_Interface (Iface_Id) then
11957
11958 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11959 -- extension derived from a synchronized interface must explicitly
11960 -- be declared synchronized, because the full view will be a
11961 -- synchronized type.
11962
11963 if Nkind (N) = N_Private_Extension_Declaration then
11964 if not Synchronized_Present (N) then
11965 Error_Msg_NE
11966 ("private extension of& must be explicitly synchronized",
11967 N, Iface_Id);
11968 end if;
11969
11970 -- However, by 3.9.4(16/2), a full type that is a record extension
11971 -- is never allowed to derive from a synchronized interface (note
11972 -- that interfaces must be excluded from this check, because those
11973 -- are represented by derived type definitions in some cases).
11974
11975 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11976 and then not Interface_Present (Type_Definition (N))
11977 then
11978 Error_Msg_N ("record extension cannot derive from synchronized "
11979 & "interface", Error_Node);
11980 end if;
11981 end if;
11982
11983 -- Check that the characteristics of the progenitor are compatible
11984 -- with the explicit qualifier in the declaration.
11985 -- The check only applies to qualifiers that come from source.
11986 -- Limited_Present also appears in the declaration of corresponding
11987 -- records, and the check does not apply to them.
11988
11989 if Limited_Present (Type_Def)
11990 and then not
11991 Is_Concurrent_Record_Type (Defining_Identifier (N))
11992 then
11993 if Is_Limited_Interface (Parent_Type)
11994 and then not Is_Limited_Interface (Iface_Id)
11995 then
11996 Error_Msg_NE
11997 ("progenitor & must be limited interface",
11998 Error_Node, Iface_Id);
11999
12000 elsif
12001 (Task_Present (Iface_Def)
12002 or else Protected_Present (Iface_Def)
12003 or else Synchronized_Present (Iface_Def))
12004 and then Nkind (N) /= N_Private_Extension_Declaration
12005 and then not Error_Posted (N)
12006 then
12007 Error_Msg_NE
12008 ("progenitor & must be limited interface",
12009 Error_Node, Iface_Id);
12010 end if;
12011
12012 -- Protected interfaces can only inherit from limited, synchronized
12013 -- or protected interfaces.
12014
12015 elsif Nkind (N) = N_Full_Type_Declaration
12016 and then Protected_Present (Type_Def)
12017 then
12018 if Limited_Present (Iface_Def)
12019 or else Synchronized_Present (Iface_Def)
12020 or else Protected_Present (Iface_Def)
12021 then
12022 null;
12023
12024 elsif Task_Present (Iface_Def) then
12025 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12026 & "from task interface", Error_Node);
12027
12028 else
12029 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12030 & "from non-limited interface", Error_Node);
12031 end if;
12032
12033 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12034 -- limited and synchronized.
12035
12036 elsif Synchronized_Present (Type_Def) then
12037 if Limited_Present (Iface_Def)
12038 or else Synchronized_Present (Iface_Def)
12039 then
12040 null;
12041
12042 elsif Protected_Present (Iface_Def)
12043 and then Nkind (N) /= N_Private_Extension_Declaration
12044 then
12045 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12046 & "from protected interface", Error_Node);
12047
12048 elsif Task_Present (Iface_Def)
12049 and then Nkind (N) /= N_Private_Extension_Declaration
12050 then
12051 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12052 & "from task interface", Error_Node);
12053
12054 elsif not Is_Limited_Interface (Iface_Id) then
12055 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12056 & "from non-limited interface", Error_Node);
12057 end if;
12058
12059 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12060 -- synchronized or task interfaces.
12061
12062 elsif Nkind (N) = N_Full_Type_Declaration
12063 and then Task_Present (Type_Def)
12064 then
12065 if Limited_Present (Iface_Def)
12066 or else Synchronized_Present (Iface_Def)
12067 or else Task_Present (Iface_Def)
12068 then
12069 null;
12070
12071 elsif Protected_Present (Iface_Def) then
12072 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12073 & "protected interface", Error_Node);
12074
12075 else
12076 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12077 & "non-limited interface", Error_Node);
12078 end if;
12079 end if;
12080 end Check_Ifaces;
12081
12082 -- Start of processing for Check_Interfaces
12083
12084 begin
12085 if Is_Interface (Parent_Type) then
12086 if Is_Task_Interface (Parent_Type) then
12087 Is_Task := True;
12088
12089 elsif Is_Protected_Interface (Parent_Type) then
12090 Is_Protected := True;
12091 end if;
12092 end if;
12093
12094 if Nkind (N) = N_Private_Extension_Declaration then
12095
12096 -- Check that progenitors are compatible with declaration
12097
12098 Iface := First (Interface_List (Def));
12099 while Present (Iface) loop
12100 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12101
12102 Parent_Node := Parent (Base_Type (Iface_Typ));
12103 Iface_Def := Type_Definition (Parent_Node);
12104
12105 if not Is_Interface (Iface_Typ) then
12106 Diagnose_Interface (Iface, Iface_Typ);
12107 else
12108 Check_Ifaces (Iface_Def, Iface);
12109 end if;
12110
12111 Next (Iface);
12112 end loop;
12113
12114 if Is_Task and Is_Protected then
12115 Error_Msg_N
12116 ("type cannot derive from task and protected interface", N);
12117 end if;
12118
12119 return;
12120 end if;
12121
12122 -- Full type declaration of derived type.
12123 -- Check compatibility with parent if it is interface type
12124
12125 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12126 and then Is_Interface (Parent_Type)
12127 then
12128 Parent_Node := Parent (Parent_Type);
12129
12130 -- More detailed checks for interface varieties
12131
12132 Check_Ifaces
12133 (Iface_Def => Type_Definition (Parent_Node),
12134 Error_Node => Subtype_Indication (Type_Definition (N)));
12135 end if;
12136
12137 Iface := First (Interface_List (Def));
12138 while Present (Iface) loop
12139 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12140
12141 Parent_Node := Parent (Base_Type (Iface_Typ));
12142 Iface_Def := Type_Definition (Parent_Node);
12143
12144 if not Is_Interface (Iface_Typ) then
12145 Diagnose_Interface (Iface, Iface_Typ);
12146
12147 else
12148 -- "The declaration of a specific descendant of an interface
12149 -- type freezes the interface type" RM 13.14
12150
12151 Freeze_Before (N, Iface_Typ);
12152 Check_Ifaces (Iface_Def, Error_Node => Iface);
12153 end if;
12154
12155 Next (Iface);
12156 end loop;
12157
12158 if Is_Task and Is_Protected then
12159 Error_Msg_N
12160 ("type cannot derive from task and protected interface", N);
12161 end if;
12162 end Check_Interfaces;
12163
12164 ------------------------------------
12165 -- Check_Or_Process_Discriminants --
12166 ------------------------------------
12167
12168 -- If an incomplete or private type declaration was already given for the
12169 -- type, the discriminants may have already been processed if they were
12170 -- present on the incomplete declaration. In this case a full conformance
12171 -- check has been performed in Find_Type_Name, and we then recheck here
12172 -- some properties that can't be checked on the partial view alone.
12173 -- Otherwise we call Process_Discriminants.
12174
12175 procedure Check_Or_Process_Discriminants
12176 (N : Node_Id;
12177 T : Entity_Id;
12178 Prev : Entity_Id := Empty)
12179 is
12180 begin
12181 if Has_Discriminants (T) then
12182
12183 -- Discriminants are already set on T if they were already present
12184 -- on the partial view. Make them visible to component declarations.
12185
12186 declare
12187 D : Entity_Id;
12188 -- Discriminant on T (full view) referencing expr on partial view
12189
12190 Prev_D : Entity_Id;
12191 -- Entity of corresponding discriminant on partial view
12192
12193 New_D : Node_Id;
12194 -- Discriminant specification for full view, expression is
12195 -- the syntactic copy on full view (which has been checked for
12196 -- conformance with partial view), only used here to post error
12197 -- message.
12198
12199 begin
12200 D := First_Discriminant (T);
12201 New_D := First (Discriminant_Specifications (N));
12202 while Present (D) loop
12203 Prev_D := Current_Entity (D);
12204 Set_Current_Entity (D);
12205 Set_Is_Immediately_Visible (D);
12206 Set_Homonym (D, Prev_D);
12207
12208 -- Handle the case where there is an untagged partial view and
12209 -- the full view is tagged: must disallow discriminants with
12210 -- defaults, unless compiling for Ada 2012, which allows a
12211 -- limited tagged type to have defaulted discriminants (see
12212 -- AI05-0214). However, suppress error here if it was already
12213 -- reported on the default expression of the partial view.
12214
12215 if Is_Tagged_Type (T)
12216 and then Present (Expression (Parent (D)))
12217 and then (not Is_Limited_Type (Current_Scope)
12218 or else Ada_Version < Ada_2012)
12219 and then not Error_Posted (Expression (Parent (D)))
12220 then
12221 if Ada_Version >= Ada_2012 then
12222 Error_Msg_N
12223 ("discriminants of nonlimited tagged type cannot have "
12224 & "defaults",
12225 Expression (New_D));
12226 else
12227 Error_Msg_N
12228 ("discriminants of tagged type cannot have defaults",
12229 Expression (New_D));
12230 end if;
12231 end if;
12232
12233 -- Ada 2005 (AI-230): Access discriminant allowed in
12234 -- non-limited record types.
12235
12236 if Ada_Version < Ada_2005 then
12237
12238 -- This restriction gets applied to the full type here. It
12239 -- has already been applied earlier to the partial view.
12240
12241 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12242 end if;
12243
12244 Next_Discriminant (D);
12245 Next (New_D);
12246 end loop;
12247 end;
12248
12249 elsif Present (Discriminant_Specifications (N)) then
12250 Process_Discriminants (N, Prev);
12251 end if;
12252 end Check_Or_Process_Discriminants;
12253
12254 ----------------------
12255 -- Check_Real_Bound --
12256 ----------------------
12257
12258 procedure Check_Real_Bound (Bound : Node_Id) is
12259 begin
12260 if not Is_Real_Type (Etype (Bound)) then
12261 Error_Msg_N
12262 ("bound in real type definition must be of real type", Bound);
12263
12264 elsif not Is_OK_Static_Expression (Bound) then
12265 Flag_Non_Static_Expr
12266 ("non-static expression used for real type bound!", Bound);
12267
12268 else
12269 return;
12270 end if;
12271
12272 Rewrite
12273 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12274 Analyze (Bound);
12275 Resolve (Bound, Standard_Float);
12276 end Check_Real_Bound;
12277
12278 ------------------------------
12279 -- Complete_Private_Subtype --
12280 ------------------------------
12281
12282 procedure Complete_Private_Subtype
12283 (Priv : Entity_Id;
12284 Full : Entity_Id;
12285 Full_Base : Entity_Id;
12286 Related_Nod : Node_Id)
12287 is
12288 Save_Next_Entity : Entity_Id;
12289 Save_Homonym : Entity_Id;
12290
12291 begin
12292 -- Set semantic attributes for (implicit) private subtype completion.
12293 -- If the full type has no discriminants, then it is a copy of the
12294 -- full view of the base. Otherwise, it is a subtype of the base with
12295 -- a possible discriminant constraint. Save and restore the original
12296 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12297 -- not corrupt the entity chain.
12298
12299 -- Note that the type of the full view is the same entity as the type
12300 -- of the partial view. In this fashion, the subtype has access to the
12301 -- correct view of the parent.
12302
12303 Save_Next_Entity := Next_Entity (Full);
12304 Save_Homonym := Homonym (Priv);
12305
12306 case Ekind (Full_Base) is
12307 when Class_Wide_Kind
12308 | Private_Kind
12309 | Protected_Kind
12310 | Task_Kind
12311 | E_Record_Subtype
12312 | E_Record_Type
12313 =>
12314 Copy_Node (Priv, Full);
12315
12316 Set_Has_Discriminants
12317 (Full, Has_Discriminants (Full_Base));
12318 Set_Has_Unknown_Discriminants
12319 (Full, Has_Unknown_Discriminants (Full_Base));
12320 Set_First_Entity (Full, First_Entity (Full_Base));
12321 Set_Last_Entity (Full, Last_Entity (Full_Base));
12322
12323 -- If the underlying base type is constrained, we know that the
12324 -- full view of the subtype is constrained as well (the converse
12325 -- is not necessarily true).
12326
12327 if Is_Constrained (Full_Base) then
12328 Set_Is_Constrained (Full);
12329 end if;
12330
12331 when others =>
12332 Copy_Node (Full_Base, Full);
12333
12334 Set_Chars (Full, Chars (Priv));
12335 Conditional_Delay (Full, Priv);
12336 Set_Sloc (Full, Sloc (Priv));
12337 end case;
12338
12339 Link_Entities (Full, Save_Next_Entity);
12340 Set_Homonym (Full, Save_Homonym);
12341 Set_Associated_Node_For_Itype (Full, Related_Nod);
12342
12343 -- Set common attributes for all subtypes: kind, convention, etc.
12344
12345 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12346 Set_Convention (Full, Convention (Full_Base));
12347
12348 -- The Etype of the full view is inconsistent. Gigi needs to see the
12349 -- structural full view, which is what the current scheme gives: the
12350 -- Etype of the full view is the etype of the full base. However, if the
12351 -- full base is a derived type, the full view then looks like a subtype
12352 -- of the parent, not a subtype of the full base. If instead we write:
12353
12354 -- Set_Etype (Full, Full_Base);
12355
12356 -- then we get inconsistencies in the front-end (confusion between
12357 -- views). Several outstanding bugs are related to this ???
12358
12359 Set_Is_First_Subtype (Full, False);
12360 Set_Scope (Full, Scope (Priv));
12361 Set_Size_Info (Full, Full_Base);
12362 Set_RM_Size (Full, RM_Size (Full_Base));
12363 Set_Is_Itype (Full);
12364
12365 -- For the unusual case of a type with unknown discriminants whose
12366 -- completion is an array, use the proper full base.
12367
12368 if Is_Array_Type (Full_Base)
12369 and then Has_Unknown_Discriminants (Priv)
12370 then
12371 Set_Etype (Full, Full_Base);
12372 end if;
12373
12374 -- A subtype of a private-type-without-discriminants, whose full-view
12375 -- has discriminants with default expressions, is not constrained.
12376
12377 if not Has_Discriminants (Priv) then
12378 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12379
12380 if Has_Discriminants (Full_Base) then
12381 Set_Discriminant_Constraint
12382 (Full, Discriminant_Constraint (Full_Base));
12383
12384 -- The partial view may have been indefinite, the full view
12385 -- might not be.
12386
12387 Set_Has_Unknown_Discriminants
12388 (Full, Has_Unknown_Discriminants (Full_Base));
12389 end if;
12390 end if;
12391
12392 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12393 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12394
12395 -- Freeze the private subtype entity if its parent is delayed, and not
12396 -- already frozen. We skip this processing if the type is an anonymous
12397 -- subtype of a record component, or is the corresponding record of a
12398 -- protected type, since these are processed when the enclosing type
12399 -- is frozen. If the parent type is declared in a nested package then
12400 -- the freezing of the private and full views also happens later.
12401
12402 if not Is_Type (Scope (Full)) then
12403 if Is_Itype (Priv)
12404 and then In_Same_Source_Unit (Full, Full_Base)
12405 and then Scope (Full_Base) /= Scope (Full)
12406 then
12407 Set_Has_Delayed_Freeze (Full);
12408 Set_Has_Delayed_Freeze (Priv);
12409
12410 else
12411 Set_Has_Delayed_Freeze (Full,
12412 Has_Delayed_Freeze (Full_Base)
12413 and then not Is_Frozen (Full_Base));
12414 end if;
12415 end if;
12416
12417 Set_Freeze_Node (Full, Empty);
12418 Set_Is_Frozen (Full, False);
12419 Set_Full_View (Priv, Full);
12420
12421 if Has_Discriminants (Full) then
12422 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12423 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12424
12425 if Has_Unknown_Discriminants (Full) then
12426 Set_Discriminant_Constraint (Full, No_Elist);
12427 end if;
12428 end if;
12429
12430 if Ekind (Full_Base) = E_Record_Type
12431 and then Has_Discriminants (Full_Base)
12432 and then Has_Discriminants (Priv) -- might not, if errors
12433 and then not Has_Unknown_Discriminants (Priv)
12434 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12435 then
12436 Create_Constrained_Components
12437 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12438
12439 -- If the full base is itself derived from private, build a congruent
12440 -- subtype of its underlying type, for use by the back end. For a
12441 -- constrained record component, the declaration cannot be placed on
12442 -- the component list, but it must nevertheless be built an analyzed, to
12443 -- supply enough information for Gigi to compute the size of component.
12444
12445 elsif Ekind (Full_Base) in Private_Kind
12446 and then Is_Derived_Type (Full_Base)
12447 and then Has_Discriminants (Full_Base)
12448 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12449 then
12450 if not Is_Itype (Priv)
12451 and then
12452 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12453 then
12454 Build_Underlying_Full_View
12455 (Parent (Priv), Full, Etype (Full_Base));
12456
12457 elsif Nkind (Related_Nod) = N_Component_Declaration then
12458 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12459 end if;
12460
12461 elsif Is_Record_Type (Full_Base) then
12462
12463 -- Show Full is simply a renaming of Full_Base
12464
12465 Set_Cloned_Subtype (Full, Full_Base);
12466 end if;
12467
12468 -- It is unsafe to share the bounds of a scalar type, because the Itype
12469 -- is elaborated on demand, and if a bound is non-static then different
12470 -- orders of elaboration in different units will lead to different
12471 -- external symbols.
12472
12473 if Is_Scalar_Type (Full_Base) then
12474 Set_Scalar_Range (Full,
12475 Make_Range (Sloc (Related_Nod),
12476 Low_Bound =>
12477 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12478 High_Bound =>
12479 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12480
12481 -- This completion inherits the bounds of the full parent, but if
12482 -- the parent is an unconstrained floating point type, so is the
12483 -- completion.
12484
12485 if Is_Floating_Point_Type (Full_Base) then
12486 Set_Includes_Infinities
12487 (Scalar_Range (Full), Has_Infinities (Full_Base));
12488 end if;
12489 end if;
12490
12491 -- ??? It seems that a lot of fields are missing that should be copied
12492 -- from Full_Base to Full. Here are some that are introduced in a
12493 -- non-disruptive way but a cleanup is necessary.
12494
12495 if Is_Tagged_Type (Full_Base) then
12496 Set_Is_Tagged_Type (Full);
12497 Set_Direct_Primitive_Operations
12498 (Full, Direct_Primitive_Operations (Full_Base));
12499 Set_No_Tagged_Streams_Pragma
12500 (Full, No_Tagged_Streams_Pragma (Full_Base));
12501
12502 -- Inherit class_wide type of full_base in case the partial view was
12503 -- not tagged. Otherwise it has already been created when the private
12504 -- subtype was analyzed.
12505
12506 if No (Class_Wide_Type (Full)) then
12507 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12508 end if;
12509
12510 -- If this is a subtype of a protected or task type, constrain its
12511 -- corresponding record, unless this is a subtype without constraints,
12512 -- i.e. a simple renaming as with an actual subtype in an instance.
12513
12514 elsif Is_Concurrent_Type (Full_Base) then
12515 if Has_Discriminants (Full)
12516 and then Present (Corresponding_Record_Type (Full_Base))
12517 and then
12518 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12519 then
12520 Set_Corresponding_Record_Type (Full,
12521 Constrain_Corresponding_Record
12522 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12523
12524 else
12525 Set_Corresponding_Record_Type (Full,
12526 Corresponding_Record_Type (Full_Base));
12527 end if;
12528 end if;
12529
12530 -- Link rep item chain, and also setting of Has_Predicates from private
12531 -- subtype to full subtype, since we will need these on the full subtype
12532 -- to create the predicate function. Note that the full subtype may
12533 -- already have rep items, inherited from the full view of the base
12534 -- type, so we must be sure not to overwrite these entries.
12535
12536 declare
12537 Append : Boolean;
12538 Item : Node_Id;
12539 Next_Item : Node_Id;
12540 Priv_Item : Node_Id;
12541
12542 begin
12543 Item := First_Rep_Item (Full);
12544 Priv_Item := First_Rep_Item (Priv);
12545
12546 -- If no existing rep items on full type, we can just link directly
12547 -- to the list of items on the private type, if any exist.. Same if
12548 -- the rep items are only those inherited from the base
12549
12550 if (No (Item)
12551 or else Nkind (Item) /= N_Aspect_Specification
12552 or else Entity (Item) = Full_Base)
12553 and then Present (First_Rep_Item (Priv))
12554 then
12555 Set_First_Rep_Item (Full, Priv_Item);
12556
12557 -- Otherwise, search to the end of items currently linked to the full
12558 -- subtype and append the private items to the end. However, if Priv
12559 -- and Full already have the same list of rep items, then the append
12560 -- is not done, as that would create a circularity.
12561 --
12562 -- The partial view may have a predicate and the rep item lists of
12563 -- both views agree when inherited from the same ancestor. In that
12564 -- case, simply propagate the list from one view to the other.
12565 -- A more complex analysis needed here ???
12566
12567 elsif Present (Priv_Item)
12568 and then Item = Next_Rep_Item (Priv_Item)
12569 then
12570 Set_First_Rep_Item (Full, Priv_Item);
12571
12572 elsif Item /= Priv_Item then
12573 Append := True;
12574 loop
12575 Next_Item := Next_Rep_Item (Item);
12576 exit when No (Next_Item);
12577 Item := Next_Item;
12578
12579 -- If the private view has aspect specifications, the full view
12580 -- inherits them. Since these aspects may already have been
12581 -- attached to the full view during derivation, do not append
12582 -- them if already present.
12583
12584 if Item = First_Rep_Item (Priv) then
12585 Append := False;
12586 exit;
12587 end if;
12588 end loop;
12589
12590 -- And link the private type items at the end of the chain
12591
12592 if Append then
12593 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12594 end if;
12595 end if;
12596 end;
12597
12598 -- Make sure Has_Predicates is set on full type if it is set on the
12599 -- private type. Note that it may already be set on the full type and
12600 -- if so, we don't want to unset it. Similarly, propagate information
12601 -- about delayed aspects, because the corresponding pragmas must be
12602 -- analyzed when one of the views is frozen. This last step is needed
12603 -- in particular when the full type is a scalar type for which an
12604 -- anonymous base type is constructed.
12605
12606 -- The predicate functions are generated either at the freeze point
12607 -- of the type or at the end of the visible part, and we must avoid
12608 -- generating them twice.
12609
12610 if Has_Predicates (Priv) then
12611 Set_Has_Predicates (Full);
12612
12613 if Present (Predicate_Function (Priv))
12614 and then No (Predicate_Function (Full))
12615 then
12616 Set_Predicate_Function (Full, Predicate_Function (Priv));
12617 end if;
12618 end if;
12619
12620 if Has_Delayed_Aspects (Priv) then
12621 Set_Has_Delayed_Aspects (Full);
12622 end if;
12623 end Complete_Private_Subtype;
12624
12625 ----------------------------
12626 -- Constant_Redeclaration --
12627 ----------------------------
12628
12629 procedure Constant_Redeclaration
12630 (Id : Entity_Id;
12631 N : Node_Id;
12632 T : out Entity_Id)
12633 is
12634 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12635 Obj_Def : constant Node_Id := Object_Definition (N);
12636 New_T : Entity_Id;
12637
12638 procedure Check_Possible_Deferred_Completion
12639 (Prev_Id : Entity_Id;
12640 Prev_Obj_Def : Node_Id;
12641 Curr_Obj_Def : Node_Id);
12642 -- Determine whether the two object definitions describe the partial
12643 -- and the full view of a constrained deferred constant. Generate
12644 -- a subtype for the full view and verify that it statically matches
12645 -- the subtype of the partial view.
12646
12647 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12648 -- If deferred constant is an access type initialized with an allocator,
12649 -- check whether there is an illegal recursion in the definition,
12650 -- through a default value of some record subcomponent. This is normally
12651 -- detected when generating init procs, but requires this additional
12652 -- mechanism when expansion is disabled.
12653
12654 ----------------------------------------
12655 -- Check_Possible_Deferred_Completion --
12656 ----------------------------------------
12657
12658 procedure Check_Possible_Deferred_Completion
12659 (Prev_Id : Entity_Id;
12660 Prev_Obj_Def : Node_Id;
12661 Curr_Obj_Def : Node_Id)
12662 is
12663 begin
12664 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12665 and then Present (Constraint (Prev_Obj_Def))
12666 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12667 and then Present (Constraint (Curr_Obj_Def))
12668 then
12669 declare
12670 Loc : constant Source_Ptr := Sloc (N);
12671 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12672 Decl : constant Node_Id :=
12673 Make_Subtype_Declaration (Loc,
12674 Defining_Identifier => Def_Id,
12675 Subtype_Indication =>
12676 Relocate_Node (Curr_Obj_Def));
12677
12678 begin
12679 Insert_Before_And_Analyze (N, Decl);
12680 Set_Etype (Id, Def_Id);
12681
12682 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12683 Error_Msg_Sloc := Sloc (Prev_Id);
12684 Error_Msg_N ("subtype does not statically match deferred "
12685 & "declaration #", N);
12686 end if;
12687 end;
12688 end if;
12689 end Check_Possible_Deferred_Completion;
12690
12691 ---------------------------------
12692 -- Check_Recursive_Declaration --
12693 ---------------------------------
12694
12695 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12696 Comp : Entity_Id;
12697
12698 begin
12699 if Is_Record_Type (Typ) then
12700 Comp := First_Component (Typ);
12701 while Present (Comp) loop
12702 if Comes_From_Source (Comp) then
12703 if Present (Expression (Parent (Comp)))
12704 and then Is_Entity_Name (Expression (Parent (Comp)))
12705 and then Entity (Expression (Parent (Comp))) = Prev
12706 then
12707 Error_Msg_Sloc := Sloc (Parent (Comp));
12708 Error_Msg_NE
12709 ("illegal circularity with declaration for & #",
12710 N, Comp);
12711 return;
12712
12713 elsif Is_Record_Type (Etype (Comp)) then
12714 Check_Recursive_Declaration (Etype (Comp));
12715 end if;
12716 end if;
12717
12718 Next_Component (Comp);
12719 end loop;
12720 end if;
12721 end Check_Recursive_Declaration;
12722
12723 -- Start of processing for Constant_Redeclaration
12724
12725 begin
12726 if Nkind (Parent (Prev)) = N_Object_Declaration then
12727 if Nkind (Object_Definition
12728 (Parent (Prev))) = N_Subtype_Indication
12729 then
12730 -- Find type of new declaration. The constraints of the two
12731 -- views must match statically, but there is no point in
12732 -- creating an itype for the full view.
12733
12734 if Nkind (Obj_Def) = N_Subtype_Indication then
12735 Find_Type (Subtype_Mark (Obj_Def));
12736 New_T := Entity (Subtype_Mark (Obj_Def));
12737
12738 else
12739 Find_Type (Obj_Def);
12740 New_T := Entity (Obj_Def);
12741 end if;
12742
12743 T := Etype (Prev);
12744
12745 else
12746 -- The full view may impose a constraint, even if the partial
12747 -- view does not, so construct the subtype.
12748
12749 New_T := Find_Type_Of_Object (Obj_Def, N);
12750 T := New_T;
12751 end if;
12752
12753 else
12754 -- Current declaration is illegal, diagnosed below in Enter_Name
12755
12756 T := Empty;
12757 New_T := Any_Type;
12758 end if;
12759
12760 -- If previous full declaration or a renaming declaration exists, or if
12761 -- a homograph is present, let Enter_Name handle it, either with an
12762 -- error or with the removal of an overridden implicit subprogram.
12763 -- The previous one is a full declaration if it has an expression
12764 -- (which in the case of an aggregate is indicated by the Init flag).
12765
12766 if Ekind (Prev) /= E_Constant
12767 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12768 or else Present (Expression (Parent (Prev)))
12769 or else Has_Init_Expression (Parent (Prev))
12770 or else Present (Full_View (Prev))
12771 then
12772 Enter_Name (Id);
12773
12774 -- Verify that types of both declarations match, or else that both types
12775 -- are anonymous access types whose designated subtypes statically match
12776 -- (as allowed in Ada 2005 by AI-385).
12777
12778 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12779 and then
12780 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12781 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12782 or else Is_Access_Constant (Etype (New_T)) /=
12783 Is_Access_Constant (Etype (Prev))
12784 or else Can_Never_Be_Null (Etype (New_T)) /=
12785 Can_Never_Be_Null (Etype (Prev))
12786 or else Null_Exclusion_Present (Parent (Prev)) /=
12787 Null_Exclusion_Present (Parent (Id))
12788 or else not Subtypes_Statically_Match
12789 (Designated_Type (Etype (Prev)),
12790 Designated_Type (Etype (New_T))))
12791 then
12792 Error_Msg_Sloc := Sloc (Prev);
12793 Error_Msg_N ("type does not match declaration#", N);
12794 Set_Full_View (Prev, Id);
12795 Set_Etype (Id, Any_Type);
12796
12797 -- A deferred constant whose type is an anonymous array is always
12798 -- illegal (unless imported). A detailed error message might be
12799 -- helpful for Ada beginners.
12800
12801 if Nkind (Object_Definition (Parent (Prev)))
12802 = N_Constrained_Array_Definition
12803 and then Nkind (Object_Definition (N))
12804 = N_Constrained_Array_Definition
12805 then
12806 Error_Msg_N ("\each anonymous array is a distinct type", N);
12807 Error_Msg_N ("a deferred constant must have a named type",
12808 Object_Definition (Parent (Prev)));
12809 end if;
12810
12811 elsif
12812 Null_Exclusion_Present (Parent (Prev))
12813 and then not Null_Exclusion_Present (N)
12814 then
12815 Error_Msg_Sloc := Sloc (Prev);
12816 Error_Msg_N ("null-exclusion does not match declaration#", N);
12817 Set_Full_View (Prev, Id);
12818 Set_Etype (Id, Any_Type);
12819
12820 -- If so, process the full constant declaration
12821
12822 else
12823 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12824 -- the deferred declaration is constrained, then the subtype defined
12825 -- by the subtype_indication in the full declaration shall match it
12826 -- statically.
12827
12828 Check_Possible_Deferred_Completion
12829 (Prev_Id => Prev,
12830 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12831 Curr_Obj_Def => Obj_Def);
12832
12833 Set_Full_View (Prev, Id);
12834 Set_Is_Public (Id, Is_Public (Prev));
12835 Set_Is_Internal (Id);
12836 Append_Entity (Id, Current_Scope);
12837
12838 -- Check ALIASED present if present before (RM 7.4(7))
12839
12840 if Is_Aliased (Prev)
12841 and then not Aliased_Present (N)
12842 then
12843 Error_Msg_Sloc := Sloc (Prev);
12844 Error_Msg_N ("ALIASED required (see declaration #)", N);
12845 end if;
12846
12847 -- Check that placement is in private part and that the incomplete
12848 -- declaration appeared in the visible part.
12849
12850 if Ekind (Current_Scope) = E_Package
12851 and then not In_Private_Part (Current_Scope)
12852 then
12853 Error_Msg_Sloc := Sloc (Prev);
12854 Error_Msg_N
12855 ("full constant for declaration # must be in private part", N);
12856
12857 elsif Ekind (Current_Scope) = E_Package
12858 and then
12859 List_Containing (Parent (Prev)) /=
12860 Visible_Declarations (Package_Specification (Current_Scope))
12861 then
12862 Error_Msg_N
12863 ("deferred constant must be declared in visible part",
12864 Parent (Prev));
12865 end if;
12866
12867 if Is_Access_Type (T)
12868 and then Nkind (Expression (N)) = N_Allocator
12869 then
12870 Check_Recursive_Declaration (Designated_Type (T));
12871 end if;
12872
12873 -- A deferred constant is a visible entity. If type has invariants,
12874 -- verify that the initial value satisfies them. This is not done in
12875 -- GNATprove mode, as GNATprove handles invariant checks itself.
12876
12877 if Has_Invariants (T)
12878 and then Present (Invariant_Procedure (T))
12879 and then not GNATprove_Mode
12880 then
12881 Insert_After (N,
12882 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12883 end if;
12884 end if;
12885 end Constant_Redeclaration;
12886
12887 ----------------------
12888 -- Constrain_Access --
12889 ----------------------
12890
12891 procedure Constrain_Access
12892 (Def_Id : in out Entity_Id;
12893 S : Node_Id;
12894 Related_Nod : Node_Id)
12895 is
12896 T : constant Entity_Id := Entity (Subtype_Mark (S));
12897 Desig_Type : constant Entity_Id := Designated_Type (T);
12898 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12899 Constraint_OK : Boolean := True;
12900
12901 begin
12902 if Is_Array_Type (Desig_Type) then
12903 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12904
12905 elsif (Is_Record_Type (Desig_Type)
12906 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12907 and then not Is_Constrained (Desig_Type)
12908 then
12909 -- ??? The following code is a temporary bypass to ignore a
12910 -- discriminant constraint on access type if it is constraining
12911 -- the current record. Avoid creating the implicit subtype of the
12912 -- record we are currently compiling since right now, we cannot
12913 -- handle these. For now, just return the access type itself.
12914
12915 if Desig_Type = Current_Scope
12916 and then No (Def_Id)
12917 then
12918 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12919 Def_Id := Entity (Subtype_Mark (S));
12920
12921 -- This call added to ensure that the constraint is analyzed
12922 -- (needed for a B test). Note that we still return early from
12923 -- this procedure to avoid recursive processing. ???
12924
12925 Constrain_Discriminated_Type
12926 (Desig_Subtype, S, Related_Nod, For_Access => True);
12927 return;
12928 end if;
12929
12930 -- Enforce rule that the constraint is illegal if there is an
12931 -- unconstrained view of the designated type. This means that the
12932 -- partial view (either a private type declaration or a derivation
12933 -- from a private type) has no discriminants. (Defect Report
12934 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12935
12936 -- Rule updated for Ada 2005: The private type is said to have
12937 -- a constrained partial view, given that objects of the type
12938 -- can be declared. Furthermore, the rule applies to all access
12939 -- types, unlike the rule concerning default discriminants (see
12940 -- RM 3.7.1(7/3))
12941
12942 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12943 and then Has_Private_Declaration (Desig_Type)
12944 and then In_Open_Scopes (Scope (Desig_Type))
12945 and then Has_Discriminants (Desig_Type)
12946 then
12947 declare
12948 Pack : constant Node_Id :=
12949 Unit_Declaration_Node (Scope (Desig_Type));
12950 Decls : List_Id;
12951 Decl : Node_Id;
12952
12953 begin
12954 if Nkind (Pack) = N_Package_Declaration then
12955 Decls := Visible_Declarations (Specification (Pack));
12956 Decl := First (Decls);
12957 while Present (Decl) loop
12958 if (Nkind (Decl) = N_Private_Type_Declaration
12959 and then Chars (Defining_Identifier (Decl)) =
12960 Chars (Desig_Type))
12961
12962 or else
12963 (Nkind (Decl) = N_Full_Type_Declaration
12964 and then
12965 Chars (Defining_Identifier (Decl)) =
12966 Chars (Desig_Type)
12967 and then Is_Derived_Type (Desig_Type)
12968 and then
12969 Has_Private_Declaration (Etype (Desig_Type)))
12970 then
12971 if No (Discriminant_Specifications (Decl)) then
12972 Error_Msg_N
12973 ("cannot constrain access type if designated "
12974 & "type has constrained partial view", S);
12975 end if;
12976
12977 exit;
12978 end if;
12979
12980 Next (Decl);
12981 end loop;
12982 end if;
12983 end;
12984 end if;
12985
12986 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12987 For_Access => True);
12988
12989 elsif Is_Concurrent_Type (Desig_Type)
12990 and then not Is_Constrained (Desig_Type)
12991 then
12992 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12993
12994 else
12995 Error_Msg_N ("invalid constraint on access type", S);
12996
12997 -- We simply ignore an invalid constraint
12998
12999 Desig_Subtype := Desig_Type;
13000 Constraint_OK := False;
13001 end if;
13002
13003 if No (Def_Id) then
13004 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13005 else
13006 Set_Ekind (Def_Id, E_Access_Subtype);
13007 end if;
13008
13009 if Constraint_OK then
13010 Set_Etype (Def_Id, Base_Type (T));
13011
13012 if Is_Private_Type (Desig_Type) then
13013 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13014 end if;
13015 else
13016 Set_Etype (Def_Id, Any_Type);
13017 end if;
13018
13019 Set_Size_Info (Def_Id, T);
13020 Set_Is_Constrained (Def_Id, Constraint_OK);
13021 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13022 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13023 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13024
13025 Conditional_Delay (Def_Id, T);
13026
13027 -- AI-363 : Subtypes of general access types whose designated types have
13028 -- default discriminants are disallowed. In instances, the rule has to
13029 -- be checked against the actual, of which T is the subtype. In a
13030 -- generic body, the rule is checked assuming that the actual type has
13031 -- defaulted discriminants.
13032
13033 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13034 if Ekind (Base_Type (T)) = E_General_Access_Type
13035 and then Has_Defaulted_Discriminants (Desig_Type)
13036 then
13037 if Ada_Version < Ada_2005 then
13038 Error_Msg_N
13039 ("access subtype of general access type would not " &
13040 "be allowed in Ada 2005?y?", S);
13041 else
13042 Error_Msg_N
13043 ("access subtype of general access type not allowed", S);
13044 end if;
13045
13046 Error_Msg_N ("\discriminants have defaults", S);
13047
13048 elsif Is_Access_Type (T)
13049 and then Is_Generic_Type (Desig_Type)
13050 and then Has_Discriminants (Desig_Type)
13051 and then In_Package_Body (Current_Scope)
13052 then
13053 if Ada_Version < Ada_2005 then
13054 Error_Msg_N
13055 ("access subtype would not be allowed in generic body "
13056 & "in Ada 2005?y?", S);
13057 else
13058 Error_Msg_N
13059 ("access subtype not allowed in generic body", S);
13060 end if;
13061
13062 Error_Msg_N
13063 ("\designated type is a discriminated formal", S);
13064 end if;
13065 end if;
13066 end Constrain_Access;
13067
13068 ---------------------
13069 -- Constrain_Array --
13070 ---------------------
13071
13072 procedure Constrain_Array
13073 (Def_Id : in out Entity_Id;
13074 SI : Node_Id;
13075 Related_Nod : Node_Id;
13076 Related_Id : Entity_Id;
13077 Suffix : Character)
13078 is
13079 C : constant Node_Id := Constraint (SI);
13080 Number_Of_Constraints : Nat := 0;
13081 Index : Node_Id;
13082 S, T : Entity_Id;
13083 Constraint_OK : Boolean := True;
13084
13085 begin
13086 T := Entity (Subtype_Mark (SI));
13087
13088 if Is_Access_Type (T) then
13089 T := Designated_Type (T);
13090 end if;
13091
13092 -- If an index constraint follows a subtype mark in a subtype indication
13093 -- then the type or subtype denoted by the subtype mark must not already
13094 -- impose an index constraint. The subtype mark must denote either an
13095 -- unconstrained array type or an access type whose designated type
13096 -- is such an array type... (RM 3.6.1)
13097
13098 if Is_Constrained (T) then
13099 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13100 Constraint_OK := False;
13101
13102 else
13103 S := First (Constraints (C));
13104 while Present (S) loop
13105 Number_Of_Constraints := Number_Of_Constraints + 1;
13106 Next (S);
13107 end loop;
13108
13109 -- In either case, the index constraint must provide a discrete
13110 -- range for each index of the array type and the type of each
13111 -- discrete range must be the same as that of the corresponding
13112 -- index. (RM 3.6.1)
13113
13114 if Number_Of_Constraints /= Number_Dimensions (T) then
13115 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13116 Constraint_OK := False;
13117
13118 else
13119 S := First (Constraints (C));
13120 Index := First_Index (T);
13121 Analyze (Index);
13122
13123 -- Apply constraints to each index type
13124
13125 for J in 1 .. Number_Of_Constraints loop
13126 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13127 Next (Index);
13128 Next (S);
13129 end loop;
13130
13131 end if;
13132 end if;
13133
13134 if No (Def_Id) then
13135 Def_Id :=
13136 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13137 Set_Parent (Def_Id, Related_Nod);
13138
13139 else
13140 Set_Ekind (Def_Id, E_Array_Subtype);
13141 end if;
13142
13143 Set_Size_Info (Def_Id, (T));
13144 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13145 Set_Etype (Def_Id, Base_Type (T));
13146
13147 if Constraint_OK then
13148 Set_First_Index (Def_Id, First (Constraints (C)));
13149 else
13150 Set_First_Index (Def_Id, First_Index (T));
13151 end if;
13152
13153 Set_Is_Constrained (Def_Id, True);
13154 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13155 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13156
13157 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13158 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13159
13160 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13161 -- We need to initialize the attribute because if Def_Id is previously
13162 -- analyzed through a limited_with clause, it will have the attributes
13163 -- of an incomplete type, one of which is an Elist that overlaps the
13164 -- Packed_Array_Impl_Type field.
13165
13166 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13167
13168 -- Build a freeze node if parent still needs one. Also make sure that
13169 -- the Depends_On_Private status is set because the subtype will need
13170 -- reprocessing at the time the base type does, and also we must set a
13171 -- conditional delay.
13172
13173 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13174 Conditional_Delay (Def_Id, T);
13175 end Constrain_Array;
13176
13177 ------------------------------
13178 -- Constrain_Component_Type --
13179 ------------------------------
13180
13181 function Constrain_Component_Type
13182 (Comp : Entity_Id;
13183 Constrained_Typ : Entity_Id;
13184 Related_Node : Node_Id;
13185 Typ : Entity_Id;
13186 Constraints : Elist_Id) return Entity_Id
13187 is
13188 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13189 Compon_Type : constant Entity_Id := Etype (Comp);
13190
13191 function Build_Constrained_Array_Type
13192 (Old_Type : Entity_Id) return Entity_Id;
13193 -- If Old_Type is an array type, one of whose indexes is constrained
13194 -- by a discriminant, build an Itype whose constraint replaces the
13195 -- discriminant with its value in the constraint.
13196
13197 function Build_Constrained_Discriminated_Type
13198 (Old_Type : Entity_Id) return Entity_Id;
13199 -- Ditto for record components
13200
13201 function Build_Constrained_Access_Type
13202 (Old_Type : Entity_Id) return Entity_Id;
13203 -- Ditto for access types. Makes use of previous two functions, to
13204 -- constrain designated type.
13205
13206 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13207 -- T is an array or discriminated type, C is a list of constraints
13208 -- that apply to T. This routine builds the constrained subtype.
13209
13210 function Is_Discriminant (Expr : Node_Id) return Boolean;
13211 -- Returns True if Expr is a discriminant
13212
13213 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13214 -- Find the value of discriminant Discrim in Constraint
13215
13216 -----------------------------------
13217 -- Build_Constrained_Access_Type --
13218 -----------------------------------
13219
13220 function Build_Constrained_Access_Type
13221 (Old_Type : Entity_Id) return Entity_Id
13222 is
13223 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13224 Itype : Entity_Id;
13225 Desig_Subtype : Entity_Id;
13226 Scop : Entity_Id;
13227
13228 begin
13229 -- if the original access type was not embedded in the enclosing
13230 -- type definition, there is no need to produce a new access
13231 -- subtype. In fact every access type with an explicit constraint
13232 -- generates an itype whose scope is the enclosing record.
13233
13234 if not Is_Type (Scope (Old_Type)) then
13235 return Old_Type;
13236
13237 elsif Is_Array_Type (Desig_Type) then
13238 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13239
13240 elsif Has_Discriminants (Desig_Type) then
13241
13242 -- This may be an access type to an enclosing record type for
13243 -- which we are constructing the constrained components. Return
13244 -- the enclosing record subtype. This is not always correct,
13245 -- but avoids infinite recursion. ???
13246
13247 Desig_Subtype := Any_Type;
13248
13249 for J in reverse 0 .. Scope_Stack.Last loop
13250 Scop := Scope_Stack.Table (J).Entity;
13251
13252 if Is_Type (Scop)
13253 and then Base_Type (Scop) = Base_Type (Desig_Type)
13254 then
13255 Desig_Subtype := Scop;
13256 end if;
13257
13258 exit when not Is_Type (Scop);
13259 end loop;
13260
13261 if Desig_Subtype = Any_Type then
13262 Desig_Subtype :=
13263 Build_Constrained_Discriminated_Type (Desig_Type);
13264 end if;
13265
13266 else
13267 return Old_Type;
13268 end if;
13269
13270 if Desig_Subtype /= Desig_Type then
13271
13272 -- The Related_Node better be here or else we won't be able
13273 -- to attach new itypes to a node in the tree.
13274
13275 pragma Assert (Present (Related_Node));
13276
13277 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13278
13279 Set_Etype (Itype, Base_Type (Old_Type));
13280 Set_Size_Info (Itype, (Old_Type));
13281 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13282 Set_Depends_On_Private (Itype, Has_Private_Component
13283 (Old_Type));
13284 Set_Is_Access_Constant (Itype, Is_Access_Constant
13285 (Old_Type));
13286
13287 -- The new itype needs freezing when it depends on a not frozen
13288 -- type and the enclosing subtype needs freezing.
13289
13290 if Has_Delayed_Freeze (Constrained_Typ)
13291 and then not Is_Frozen (Constrained_Typ)
13292 then
13293 Conditional_Delay (Itype, Base_Type (Old_Type));
13294 end if;
13295
13296 return Itype;
13297
13298 else
13299 return Old_Type;
13300 end if;
13301 end Build_Constrained_Access_Type;
13302
13303 ----------------------------------
13304 -- Build_Constrained_Array_Type --
13305 ----------------------------------
13306
13307 function Build_Constrained_Array_Type
13308 (Old_Type : Entity_Id) return Entity_Id
13309 is
13310 Lo_Expr : Node_Id;
13311 Hi_Expr : Node_Id;
13312 Old_Index : Node_Id;
13313 Range_Node : Node_Id;
13314 Constr_List : List_Id;
13315
13316 Need_To_Create_Itype : Boolean := False;
13317
13318 begin
13319 Old_Index := First_Index (Old_Type);
13320 while Present (Old_Index) loop
13321 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13322
13323 if Is_Discriminant (Lo_Expr)
13324 or else
13325 Is_Discriminant (Hi_Expr)
13326 then
13327 Need_To_Create_Itype := True;
13328 end if;
13329
13330 Next_Index (Old_Index);
13331 end loop;
13332
13333 if Need_To_Create_Itype then
13334 Constr_List := New_List;
13335
13336 Old_Index := First_Index (Old_Type);
13337 while Present (Old_Index) loop
13338 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13339
13340 if Is_Discriminant (Lo_Expr) then
13341 Lo_Expr := Get_Discr_Value (Lo_Expr);
13342 end if;
13343
13344 if Is_Discriminant (Hi_Expr) then
13345 Hi_Expr := Get_Discr_Value (Hi_Expr);
13346 end if;
13347
13348 Range_Node :=
13349 Make_Range
13350 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13351
13352 Append (Range_Node, To => Constr_List);
13353
13354 Next_Index (Old_Index);
13355 end loop;
13356
13357 return Build_Subtype (Old_Type, Constr_List);
13358
13359 else
13360 return Old_Type;
13361 end if;
13362 end Build_Constrained_Array_Type;
13363
13364 ------------------------------------------
13365 -- Build_Constrained_Discriminated_Type --
13366 ------------------------------------------
13367
13368 function Build_Constrained_Discriminated_Type
13369 (Old_Type : Entity_Id) return Entity_Id
13370 is
13371 Expr : Node_Id;
13372 Constr_List : List_Id;
13373 Old_Constraint : Elmt_Id;
13374
13375 Need_To_Create_Itype : Boolean := False;
13376
13377 begin
13378 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13379 while Present (Old_Constraint) loop
13380 Expr := Node (Old_Constraint);
13381
13382 if Is_Discriminant (Expr) then
13383 Need_To_Create_Itype := True;
13384 end if;
13385
13386 Next_Elmt (Old_Constraint);
13387 end loop;
13388
13389 if Need_To_Create_Itype then
13390 Constr_List := New_List;
13391
13392 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13393 while Present (Old_Constraint) loop
13394 Expr := Node (Old_Constraint);
13395
13396 if Is_Discriminant (Expr) then
13397 Expr := Get_Discr_Value (Expr);
13398 end if;
13399
13400 Append (New_Copy_Tree (Expr), To => Constr_List);
13401
13402 Next_Elmt (Old_Constraint);
13403 end loop;
13404
13405 return Build_Subtype (Old_Type, Constr_List);
13406
13407 else
13408 return Old_Type;
13409 end if;
13410 end Build_Constrained_Discriminated_Type;
13411
13412 -------------------
13413 -- Build_Subtype --
13414 -------------------
13415
13416 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13417 Indic : Node_Id;
13418 Subtyp_Decl : Node_Id;
13419 Def_Id : Entity_Id;
13420 Btyp : Entity_Id := Base_Type (T);
13421
13422 begin
13423 -- The Related_Node better be here or else we won't be able to
13424 -- attach new itypes to a node in the tree.
13425
13426 pragma Assert (Present (Related_Node));
13427
13428 -- If the view of the component's type is incomplete or private
13429 -- with unknown discriminants, then the constraint must be applied
13430 -- to the full type.
13431
13432 if Has_Unknown_Discriminants (Btyp)
13433 and then Present (Underlying_Type (Btyp))
13434 then
13435 Btyp := Underlying_Type (Btyp);
13436 end if;
13437
13438 Indic :=
13439 Make_Subtype_Indication (Loc,
13440 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13441 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13442
13443 Def_Id := Create_Itype (Ekind (T), Related_Node);
13444
13445 Subtyp_Decl :=
13446 Make_Subtype_Declaration (Loc,
13447 Defining_Identifier => Def_Id,
13448 Subtype_Indication => Indic);
13449
13450 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13451
13452 -- Itypes must be analyzed with checks off (see package Itypes)
13453
13454 Analyze (Subtyp_Decl, Suppress => All_Checks);
13455
13456 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13457 Inherit_Predicate_Flags (Def_Id, T);
13458
13459 -- Indicate where the predicate function may be found
13460
13461 if Is_Itype (T) then
13462 if Present (Predicate_Function (Def_Id)) then
13463 null;
13464
13465 elsif Present (Predicate_Function (T)) then
13466 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13467
13468 else
13469 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13470 end if;
13471
13472 elsif No (Predicate_Function (Def_Id)) then
13473 Set_Predicated_Parent (Def_Id, T);
13474 end if;
13475 end if;
13476
13477 return Def_Id;
13478 end Build_Subtype;
13479
13480 ---------------------
13481 -- Get_Discr_Value --
13482 ---------------------
13483
13484 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13485 D : Entity_Id;
13486 E : Elmt_Id;
13487
13488 begin
13489 -- The discriminant may be declared for the type, in which case we
13490 -- find it by iterating over the list of discriminants. If the
13491 -- discriminant is inherited from a parent type, it appears as the
13492 -- corresponding discriminant of the current type. This will be the
13493 -- case when constraining an inherited component whose constraint is
13494 -- given by a discriminant of the parent.
13495
13496 D := First_Discriminant (Typ);
13497 E := First_Elmt (Constraints);
13498
13499 while Present (D) loop
13500 if D = Entity (Discrim)
13501 or else D = CR_Discriminant (Entity (Discrim))
13502 or else Corresponding_Discriminant (D) = Entity (Discrim)
13503 then
13504 return Node (E);
13505 end if;
13506
13507 Next_Discriminant (D);
13508 Next_Elmt (E);
13509 end loop;
13510
13511 -- The Corresponding_Discriminant mechanism is incomplete, because
13512 -- the correspondence between new and old discriminants is not one
13513 -- to one: one new discriminant can constrain several old ones. In
13514 -- that case, scan sequentially the stored_constraint, the list of
13515 -- discriminants of the parents, and the constraints.
13516
13517 -- Previous code checked for the present of the Stored_Constraint
13518 -- list for the derived type, but did not use it at all. Should it
13519 -- be present when the component is a discriminated task type?
13520
13521 if Is_Derived_Type (Typ)
13522 and then Scope (Entity (Discrim)) = Etype (Typ)
13523 then
13524 D := First_Discriminant (Etype (Typ));
13525 E := First_Elmt (Constraints);
13526 while Present (D) loop
13527 if D = Entity (Discrim) then
13528 return Node (E);
13529 end if;
13530
13531 Next_Discriminant (D);
13532 Next_Elmt (E);
13533 end loop;
13534 end if;
13535
13536 -- Something is wrong if we did not find the value
13537
13538 raise Program_Error;
13539 end Get_Discr_Value;
13540
13541 ---------------------
13542 -- Is_Discriminant --
13543 ---------------------
13544
13545 function Is_Discriminant (Expr : Node_Id) return Boolean is
13546 Discrim_Scope : Entity_Id;
13547
13548 begin
13549 if Denotes_Discriminant (Expr) then
13550 Discrim_Scope := Scope (Entity (Expr));
13551
13552 -- Either we have a reference to one of Typ's discriminants,
13553
13554 pragma Assert (Discrim_Scope = Typ
13555
13556 -- or to the discriminants of the parent type, in the case
13557 -- of a derivation of a tagged type with variants.
13558
13559 or else Discrim_Scope = Etype (Typ)
13560 or else Full_View (Discrim_Scope) = Etype (Typ)
13561
13562 -- or same as above for the case where the discriminants
13563 -- were declared in Typ's private view.
13564
13565 or else (Is_Private_Type (Discrim_Scope)
13566 and then Chars (Discrim_Scope) = Chars (Typ))
13567
13568 -- or else we are deriving from the full view and the
13569 -- discriminant is declared in the private entity.
13570
13571 or else (Is_Private_Type (Typ)
13572 and then Chars (Discrim_Scope) = Chars (Typ))
13573
13574 -- Or we are constrained the corresponding record of a
13575 -- synchronized type that completes a private declaration.
13576
13577 or else (Is_Concurrent_Record_Type (Typ)
13578 and then
13579 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13580
13581 -- or we have a class-wide type, in which case make sure the
13582 -- discriminant found belongs to the root type.
13583
13584 or else (Is_Class_Wide_Type (Typ)
13585 and then Etype (Typ) = Discrim_Scope));
13586
13587 return True;
13588 end if;
13589
13590 -- In all other cases we have something wrong
13591
13592 return False;
13593 end Is_Discriminant;
13594
13595 -- Start of processing for Constrain_Component_Type
13596
13597 begin
13598 if Nkind (Parent (Comp)) = N_Component_Declaration
13599 and then Comes_From_Source (Parent (Comp))
13600 and then Comes_From_Source
13601 (Subtype_Indication (Component_Definition (Parent (Comp))))
13602 and then
13603 Is_Entity_Name
13604 (Subtype_Indication (Component_Definition (Parent (Comp))))
13605 then
13606 return Compon_Type;
13607
13608 elsif Is_Array_Type (Compon_Type) then
13609 return Build_Constrained_Array_Type (Compon_Type);
13610
13611 elsif Has_Discriminants (Compon_Type) then
13612 return Build_Constrained_Discriminated_Type (Compon_Type);
13613
13614 elsif Is_Access_Type (Compon_Type) then
13615 return Build_Constrained_Access_Type (Compon_Type);
13616
13617 else
13618 return Compon_Type;
13619 end if;
13620 end Constrain_Component_Type;
13621
13622 --------------------------
13623 -- Constrain_Concurrent --
13624 --------------------------
13625
13626 -- For concurrent types, the associated record value type carries the same
13627 -- discriminants, so when we constrain a concurrent type, we must constrain
13628 -- the corresponding record type as well.
13629
13630 procedure Constrain_Concurrent
13631 (Def_Id : in out Entity_Id;
13632 SI : Node_Id;
13633 Related_Nod : Node_Id;
13634 Related_Id : Entity_Id;
13635 Suffix : Character)
13636 is
13637 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13638 -- case of a private subtype (needed when only doing semantic analysis).
13639
13640 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13641 T_Val : Entity_Id;
13642
13643 begin
13644 if Is_Access_Type (T_Ent) then
13645 T_Ent := Designated_Type (T_Ent);
13646 end if;
13647
13648 T_Val := Corresponding_Record_Type (T_Ent);
13649
13650 if Present (T_Val) then
13651
13652 if No (Def_Id) then
13653 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13654
13655 -- Elaborate itype now, as it may be used in a subsequent
13656 -- synchronized operation in another scope.
13657
13658 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13659 Build_Itype_Reference (Def_Id, Related_Nod);
13660 end if;
13661 end if;
13662
13663 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13664 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13665
13666 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13667 Set_Corresponding_Record_Type (Def_Id,
13668 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13669
13670 else
13671 -- If there is no associated record, expansion is disabled and this
13672 -- is a generic context. Create a subtype in any case, so that
13673 -- semantic analysis can proceed.
13674
13675 if No (Def_Id) then
13676 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13677 end if;
13678
13679 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13680 end if;
13681 end Constrain_Concurrent;
13682
13683 ------------------------------------
13684 -- Constrain_Corresponding_Record --
13685 ------------------------------------
13686
13687 function Constrain_Corresponding_Record
13688 (Prot_Subt : Entity_Id;
13689 Corr_Rec : Entity_Id;
13690 Related_Nod : Node_Id) return Entity_Id
13691 is
13692 T_Sub : constant Entity_Id :=
13693 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13694
13695 begin
13696 Set_Etype (T_Sub, Corr_Rec);
13697 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13698 Set_Is_Constrained (T_Sub, True);
13699 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13700 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13701
13702 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13703 Set_Discriminant_Constraint
13704 (T_Sub, Discriminant_Constraint (Prot_Subt));
13705 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13706 Create_Constrained_Components
13707 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13708 end if;
13709
13710 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13711
13712 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13713 Conditional_Delay (T_Sub, Corr_Rec);
13714
13715 else
13716 -- This is a component subtype: it will be frozen in the context of
13717 -- the enclosing record's init_proc, so that discriminant references
13718 -- are resolved to discriminals. (Note: we used to skip freezing
13719 -- altogether in that case, which caused errors downstream for
13720 -- components of a bit packed array type).
13721
13722 Set_Has_Delayed_Freeze (T_Sub);
13723 end if;
13724
13725 return T_Sub;
13726 end Constrain_Corresponding_Record;
13727
13728 -----------------------
13729 -- Constrain_Decimal --
13730 -----------------------
13731
13732 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13733 T : constant Entity_Id := Entity (Subtype_Mark (S));
13734 C : constant Node_Id := Constraint (S);
13735 Loc : constant Source_Ptr := Sloc (C);
13736 Range_Expr : Node_Id;
13737 Digits_Expr : Node_Id;
13738 Digits_Val : Uint;
13739 Bound_Val : Ureal;
13740
13741 begin
13742 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13743
13744 if Nkind (C) = N_Range_Constraint then
13745 Range_Expr := Range_Expression (C);
13746 Digits_Val := Digits_Value (T);
13747
13748 else
13749 pragma Assert (Nkind (C) = N_Digits_Constraint);
13750
13751 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13752
13753 Digits_Expr := Digits_Expression (C);
13754 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13755
13756 Check_Digits_Expression (Digits_Expr);
13757 Digits_Val := Expr_Value (Digits_Expr);
13758
13759 if Digits_Val > Digits_Value (T) then
13760 Error_Msg_N
13761 ("digits expression is incompatible with subtype", C);
13762 Digits_Val := Digits_Value (T);
13763 end if;
13764
13765 if Present (Range_Constraint (C)) then
13766 Range_Expr := Range_Expression (Range_Constraint (C));
13767 else
13768 Range_Expr := Empty;
13769 end if;
13770 end if;
13771
13772 Set_Etype (Def_Id, Base_Type (T));
13773 Set_Size_Info (Def_Id, (T));
13774 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13775 Set_Delta_Value (Def_Id, Delta_Value (T));
13776 Set_Scale_Value (Def_Id, Scale_Value (T));
13777 Set_Small_Value (Def_Id, Small_Value (T));
13778 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13779 Set_Digits_Value (Def_Id, Digits_Val);
13780
13781 -- Manufacture range from given digits value if no range present
13782
13783 if No (Range_Expr) then
13784 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13785 Range_Expr :=
13786 Make_Range (Loc,
13787 Low_Bound =>
13788 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13789 High_Bound =>
13790 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13791 end if;
13792
13793 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13794 Set_Discrete_RM_Size (Def_Id);
13795
13796 -- Unconditionally delay the freeze, since we cannot set size
13797 -- information in all cases correctly until the freeze point.
13798
13799 Set_Has_Delayed_Freeze (Def_Id);
13800 end Constrain_Decimal;
13801
13802 ----------------------------------
13803 -- Constrain_Discriminated_Type --
13804 ----------------------------------
13805
13806 procedure Constrain_Discriminated_Type
13807 (Def_Id : Entity_Id;
13808 S : Node_Id;
13809 Related_Nod : Node_Id;
13810 For_Access : Boolean := False)
13811 is
13812 E : Entity_Id := Entity (Subtype_Mark (S));
13813 T : Entity_Id;
13814
13815 procedure Fixup_Bad_Constraint;
13816 -- Called after finding a bad constraint, and after having posted an
13817 -- appropriate error message. The goal is to leave type Def_Id in as
13818 -- reasonable state as possible.
13819
13820 --------------------------
13821 -- Fixup_Bad_Constraint --
13822 --------------------------
13823
13824 procedure Fixup_Bad_Constraint is
13825 begin
13826 -- Set a reasonable Ekind for the entity, including incomplete types.
13827
13828 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13829
13830 -- Set Etype to the known type, to reduce chances of cascaded errors
13831
13832 Set_Etype (Def_Id, E);
13833 Set_Error_Posted (Def_Id);
13834 end Fixup_Bad_Constraint;
13835
13836 -- Local variables
13837
13838 C : Node_Id;
13839 Constr : Elist_Id := New_Elmt_List;
13840
13841 -- Start of processing for Constrain_Discriminated_Type
13842
13843 begin
13844 C := Constraint (S);
13845
13846 -- A discriminant constraint is only allowed in a subtype indication,
13847 -- after a subtype mark. This subtype mark must denote either a type
13848 -- with discriminants, or an access type whose designated type is a
13849 -- type with discriminants. A discriminant constraint specifies the
13850 -- values of these discriminants (RM 3.7.2(5)).
13851
13852 T := Base_Type (Entity (Subtype_Mark (S)));
13853
13854 if Is_Access_Type (T) then
13855 T := Designated_Type (T);
13856 end if;
13857
13858 -- In an instance it may be necessary to retrieve the full view of a
13859 -- type with unknown discriminants, or a full view with defaulted
13860 -- discriminants. In other contexts the constraint is illegal.
13861
13862 if In_Instance
13863 and then Is_Private_Type (T)
13864 and then Present (Full_View (T))
13865 and then
13866 (Has_Unknown_Discriminants (T)
13867 or else
13868 (not Has_Discriminants (T)
13869 and then Has_Discriminants (Full_View (T))
13870 and then Present (Discriminant_Default_Value
13871 (First_Discriminant (Full_View (T))))))
13872 then
13873 T := Full_View (T);
13874 E := Full_View (E);
13875 end if;
13876
13877 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13878 -- generating an error for access-to-incomplete subtypes.
13879
13880 if Ada_Version >= Ada_2005
13881 and then Ekind (T) = E_Incomplete_Type
13882 and then Nkind (Parent (S)) = N_Subtype_Declaration
13883 and then not Is_Itype (Def_Id)
13884 then
13885 -- A little sanity check: emit an error message if the type has
13886 -- discriminants to begin with. Type T may be a regular incomplete
13887 -- type or imported via a limited with clause.
13888
13889 if Has_Discriminants (T)
13890 or else (From_Limited_With (T)
13891 and then Present (Non_Limited_View (T))
13892 and then Nkind (Parent (Non_Limited_View (T))) =
13893 N_Full_Type_Declaration
13894 and then Present (Discriminant_Specifications
13895 (Parent (Non_Limited_View (T)))))
13896 then
13897 Error_Msg_N
13898 ("(Ada 2005) incomplete subtype may not be constrained", C);
13899 else
13900 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13901 end if;
13902
13903 Fixup_Bad_Constraint;
13904 return;
13905
13906 -- Check that the type has visible discriminants. The type may be
13907 -- a private type with unknown discriminants whose full view has
13908 -- discriminants which are invisible.
13909
13910 elsif not Has_Discriminants (T)
13911 or else
13912 (Has_Unknown_Discriminants (T)
13913 and then Is_Private_Type (T))
13914 then
13915 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13916 Fixup_Bad_Constraint;
13917 return;
13918
13919 elsif Is_Constrained (E)
13920 or else (Ekind (E) = E_Class_Wide_Subtype
13921 and then Present (Discriminant_Constraint (E)))
13922 then
13923 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13924 Fixup_Bad_Constraint;
13925 return;
13926 end if;
13927
13928 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13929 -- applies to the base type.
13930
13931 T := Base_Type (T);
13932
13933 Constr := Build_Discriminant_Constraints (T, S);
13934
13935 -- If the list returned was empty we had an error in building the
13936 -- discriminant constraint. We have also already signalled an error
13937 -- in the incomplete type case
13938
13939 if Is_Empty_Elmt_List (Constr) then
13940 Fixup_Bad_Constraint;
13941 return;
13942 end if;
13943
13944 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13945 end Constrain_Discriminated_Type;
13946
13947 ---------------------------
13948 -- Constrain_Enumeration --
13949 ---------------------------
13950
13951 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13952 T : constant Entity_Id := Entity (Subtype_Mark (S));
13953 C : constant Node_Id := Constraint (S);
13954
13955 begin
13956 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13957
13958 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13959
13960 Set_Etype (Def_Id, Base_Type (T));
13961 Set_Size_Info (Def_Id, (T));
13962 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13963 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13964
13965 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13966
13967 Set_Discrete_RM_Size (Def_Id);
13968 end Constrain_Enumeration;
13969
13970 ----------------------
13971 -- Constrain_Float --
13972 ----------------------
13973
13974 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13975 T : constant Entity_Id := Entity (Subtype_Mark (S));
13976 C : Node_Id;
13977 D : Node_Id;
13978 Rais : Node_Id;
13979
13980 begin
13981 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13982
13983 Set_Etype (Def_Id, Base_Type (T));
13984 Set_Size_Info (Def_Id, (T));
13985 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13986
13987 -- Process the constraint
13988
13989 C := Constraint (S);
13990
13991 -- Digits constraint present
13992
13993 if Nkind (C) = N_Digits_Constraint then
13994
13995 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13996 Check_Restriction (No_Obsolescent_Features, C);
13997
13998 if Warn_On_Obsolescent_Feature then
13999 Error_Msg_N
14000 ("subtype digits constraint is an " &
14001 "obsolescent feature (RM J.3(8))?j?", C);
14002 end if;
14003
14004 D := Digits_Expression (C);
14005 Analyze_And_Resolve (D, Any_Integer);
14006 Check_Digits_Expression (D);
14007 Set_Digits_Value (Def_Id, Expr_Value (D));
14008
14009 -- Check that digits value is in range. Obviously we can do this
14010 -- at compile time, but it is strictly a runtime check, and of
14011 -- course there is an ACVC test that checks this.
14012
14013 if Digits_Value (Def_Id) > Digits_Value (T) then
14014 Error_Msg_Uint_1 := Digits_Value (T);
14015 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14016 Rais :=
14017 Make_Raise_Constraint_Error (Sloc (D),
14018 Reason => CE_Range_Check_Failed);
14019 Insert_Action (Declaration_Node (Def_Id), Rais);
14020 end if;
14021
14022 C := Range_Constraint (C);
14023
14024 -- No digits constraint present
14025
14026 else
14027 Set_Digits_Value (Def_Id, Digits_Value (T));
14028 end if;
14029
14030 -- Range constraint present
14031
14032 if Nkind (C) = N_Range_Constraint then
14033 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14034
14035 -- No range constraint present
14036
14037 else
14038 pragma Assert (No (C));
14039 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14040 end if;
14041
14042 Set_Is_Constrained (Def_Id);
14043 end Constrain_Float;
14044
14045 ---------------------
14046 -- Constrain_Index --
14047 ---------------------
14048
14049 procedure Constrain_Index
14050 (Index : Node_Id;
14051 S : Node_Id;
14052 Related_Nod : Node_Id;
14053 Related_Id : Entity_Id;
14054 Suffix : Character;
14055 Suffix_Index : Nat)
14056 is
14057 Def_Id : Entity_Id;
14058 R : Node_Id := Empty;
14059 T : constant Entity_Id := Etype (Index);
14060
14061 begin
14062 Def_Id :=
14063 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14064 Set_Etype (Def_Id, Base_Type (T));
14065
14066 if Nkind (S) = N_Range
14067 or else
14068 (Nkind (S) = N_Attribute_Reference
14069 and then Attribute_Name (S) = Name_Range)
14070 then
14071 -- A Range attribute will be transformed into N_Range by Resolve
14072
14073 Analyze (S);
14074 Set_Etype (S, T);
14075 R := S;
14076
14077 Process_Range_Expr_In_Decl (R, T);
14078
14079 if not Error_Posted (S)
14080 and then
14081 (Nkind (S) /= N_Range
14082 or else not Covers (T, (Etype (Low_Bound (S))))
14083 or else not Covers (T, (Etype (High_Bound (S)))))
14084 then
14085 if Base_Type (T) /= Any_Type
14086 and then Etype (Low_Bound (S)) /= Any_Type
14087 and then Etype (High_Bound (S)) /= Any_Type
14088 then
14089 Error_Msg_N ("range expected", S);
14090 end if;
14091 end if;
14092
14093 elsif Nkind (S) = N_Subtype_Indication then
14094
14095 -- The parser has verified that this is a discrete indication
14096
14097 Resolve_Discrete_Subtype_Indication (S, T);
14098 Bad_Predicated_Subtype_Use
14099 ("subtype& has predicate, not allowed in index constraint",
14100 S, Entity (Subtype_Mark (S)));
14101
14102 R := Range_Expression (Constraint (S));
14103
14104 -- Capture values of bounds and generate temporaries for them if
14105 -- needed, since checks may cause duplication of the expressions
14106 -- which must not be reevaluated.
14107
14108 -- The forced evaluation removes side effects from expressions, which
14109 -- should occur also in GNATprove mode. Otherwise, we end up with
14110 -- unexpected insertions of actions at places where this is not
14111 -- supposed to occur, e.g. on default parameters of a call.
14112
14113 if Expander_Active or GNATprove_Mode then
14114 Force_Evaluation
14115 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14116 Force_Evaluation
14117 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14118 end if;
14119
14120 elsif Nkind (S) = N_Discriminant_Association then
14121
14122 -- Syntactically valid in subtype indication
14123
14124 Error_Msg_N ("invalid index constraint", S);
14125 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14126 return;
14127
14128 -- Subtype_Mark case, no anonymous subtypes to construct
14129
14130 else
14131 Analyze (S);
14132
14133 if Is_Entity_Name (S) then
14134 if not Is_Type (Entity (S)) then
14135 Error_Msg_N ("expect subtype mark for index constraint", S);
14136
14137 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14138 Wrong_Type (S, Base_Type (T));
14139
14140 -- Check error of subtype with predicate in index constraint
14141
14142 else
14143 Bad_Predicated_Subtype_Use
14144 ("subtype& has predicate, not allowed in index constraint",
14145 S, Entity (S));
14146 end if;
14147
14148 return;
14149
14150 else
14151 Error_Msg_N ("invalid index constraint", S);
14152 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14153 return;
14154 end if;
14155 end if;
14156
14157 -- Complete construction of the Itype
14158
14159 if Is_Modular_Integer_Type (T) then
14160 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14161
14162 elsif Is_Integer_Type (T) then
14163 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14164
14165 else
14166 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14167 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14168 Set_First_Literal (Def_Id, First_Literal (T));
14169 end if;
14170
14171 Set_Size_Info (Def_Id, (T));
14172 Set_RM_Size (Def_Id, RM_Size (T));
14173 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14174
14175 Set_Scalar_Range (Def_Id, R);
14176
14177 Set_Etype (S, Def_Id);
14178 Set_Discrete_RM_Size (Def_Id);
14179 end Constrain_Index;
14180
14181 -----------------------
14182 -- Constrain_Integer --
14183 -----------------------
14184
14185 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14186 T : constant Entity_Id := Entity (Subtype_Mark (S));
14187 C : constant Node_Id := Constraint (S);
14188
14189 begin
14190 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14191
14192 if Is_Modular_Integer_Type (T) then
14193 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14194 else
14195 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14196 end if;
14197
14198 Set_Etype (Def_Id, Base_Type (T));
14199 Set_Size_Info (Def_Id, (T));
14200 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14201 Set_Discrete_RM_Size (Def_Id);
14202 end Constrain_Integer;
14203
14204 ------------------------------
14205 -- Constrain_Ordinary_Fixed --
14206 ------------------------------
14207
14208 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14209 T : constant Entity_Id := Entity (Subtype_Mark (S));
14210 C : Node_Id;
14211 D : Node_Id;
14212 Rais : Node_Id;
14213
14214 begin
14215 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14216 Set_Etype (Def_Id, Base_Type (T));
14217 Set_Size_Info (Def_Id, (T));
14218 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14219 Set_Small_Value (Def_Id, Small_Value (T));
14220
14221 -- Process the constraint
14222
14223 C := Constraint (S);
14224
14225 -- Delta constraint present
14226
14227 if Nkind (C) = N_Delta_Constraint then
14228
14229 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14230 Check_Restriction (No_Obsolescent_Features, C);
14231
14232 if Warn_On_Obsolescent_Feature then
14233 Error_Msg_S
14234 ("subtype delta constraint is an " &
14235 "obsolescent feature (RM J.3(7))?j?");
14236 end if;
14237
14238 D := Delta_Expression (C);
14239 Analyze_And_Resolve (D, Any_Real);
14240 Check_Delta_Expression (D);
14241 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14242
14243 -- Check that delta value is in range. Obviously we can do this
14244 -- at compile time, but it is strictly a runtime check, and of
14245 -- course there is an ACVC test that checks this.
14246
14247 if Delta_Value (Def_Id) < Delta_Value (T) then
14248 Error_Msg_N ("??delta value is too small", D);
14249 Rais :=
14250 Make_Raise_Constraint_Error (Sloc (D),
14251 Reason => CE_Range_Check_Failed);
14252 Insert_Action (Declaration_Node (Def_Id), Rais);
14253 end if;
14254
14255 C := Range_Constraint (C);
14256
14257 -- No delta constraint present
14258
14259 else
14260 Set_Delta_Value (Def_Id, Delta_Value (T));
14261 end if;
14262
14263 -- Range constraint present
14264
14265 if Nkind (C) = N_Range_Constraint then
14266 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14267
14268 -- No range constraint present
14269
14270 else
14271 pragma Assert (No (C));
14272 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14273 end if;
14274
14275 Set_Discrete_RM_Size (Def_Id);
14276
14277 -- Unconditionally delay the freeze, since we cannot set size
14278 -- information in all cases correctly until the freeze point.
14279
14280 Set_Has_Delayed_Freeze (Def_Id);
14281 end Constrain_Ordinary_Fixed;
14282
14283 -----------------------
14284 -- Contain_Interface --
14285 -----------------------
14286
14287 function Contain_Interface
14288 (Iface : Entity_Id;
14289 Ifaces : Elist_Id) return Boolean
14290 is
14291 Iface_Elmt : Elmt_Id;
14292
14293 begin
14294 if Present (Ifaces) then
14295 Iface_Elmt := First_Elmt (Ifaces);
14296 while Present (Iface_Elmt) loop
14297 if Node (Iface_Elmt) = Iface then
14298 return True;
14299 end if;
14300
14301 Next_Elmt (Iface_Elmt);
14302 end loop;
14303 end if;
14304
14305 return False;
14306 end Contain_Interface;
14307
14308 ---------------------------
14309 -- Convert_Scalar_Bounds --
14310 ---------------------------
14311
14312 procedure Convert_Scalar_Bounds
14313 (N : Node_Id;
14314 Parent_Type : Entity_Id;
14315 Derived_Type : Entity_Id;
14316 Loc : Source_Ptr)
14317 is
14318 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14319
14320 Lo : Node_Id;
14321 Hi : Node_Id;
14322 Rng : Node_Id;
14323
14324 begin
14325 -- Defend against previous errors
14326
14327 if No (Scalar_Range (Derived_Type)) then
14328 Check_Error_Detected;
14329 return;
14330 end if;
14331
14332 Lo := Build_Scalar_Bound
14333 (Type_Low_Bound (Derived_Type),
14334 Parent_Type, Implicit_Base);
14335
14336 Hi := Build_Scalar_Bound
14337 (Type_High_Bound (Derived_Type),
14338 Parent_Type, Implicit_Base);
14339
14340 Rng :=
14341 Make_Range (Loc,
14342 Low_Bound => Lo,
14343 High_Bound => Hi);
14344
14345 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14346
14347 Set_Parent (Rng, N);
14348 Set_Scalar_Range (Derived_Type, Rng);
14349
14350 -- Analyze the bounds
14351
14352 Analyze_And_Resolve (Lo, Implicit_Base);
14353 Analyze_And_Resolve (Hi, Implicit_Base);
14354
14355 -- Analyze the range itself, except that we do not analyze it if
14356 -- the bounds are real literals, and we have a fixed-point type.
14357 -- The reason for this is that we delay setting the bounds in this
14358 -- case till we know the final Small and Size values (see circuit
14359 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14360
14361 if Is_Fixed_Point_Type (Parent_Type)
14362 and then Nkind (Lo) = N_Real_Literal
14363 and then Nkind (Hi) = N_Real_Literal
14364 then
14365 return;
14366
14367 -- Here we do the analysis of the range
14368
14369 -- Note: we do this manually, since if we do a normal Analyze and
14370 -- Resolve call, there are problems with the conversions used for
14371 -- the derived type range.
14372
14373 else
14374 Set_Etype (Rng, Implicit_Base);
14375 Set_Analyzed (Rng, True);
14376 end if;
14377 end Convert_Scalar_Bounds;
14378
14379 -------------------
14380 -- Copy_And_Swap --
14381 -------------------
14382
14383 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14384 begin
14385 -- Initialize new full declaration entity by copying the pertinent
14386 -- fields of the corresponding private declaration entity.
14387
14388 -- We temporarily set Ekind to a value appropriate for a type to
14389 -- avoid assert failures in Einfo from checking for setting type
14390 -- attributes on something that is not a type. Ekind (Priv) is an
14391 -- appropriate choice, since it allowed the attributes to be set
14392 -- in the first place. This Ekind value will be modified later.
14393
14394 Set_Ekind (Full, Ekind (Priv));
14395
14396 -- Also set Etype temporarily to Any_Type, again, in the absence
14397 -- of errors, it will be properly reset, and if there are errors,
14398 -- then we want a value of Any_Type to remain.
14399
14400 Set_Etype (Full, Any_Type);
14401
14402 -- Now start copying attributes
14403
14404 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14405
14406 if Has_Discriminants (Full) then
14407 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14408 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14409 end if;
14410
14411 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14412 Set_Homonym (Full, Homonym (Priv));
14413 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14414 Set_Is_Public (Full, Is_Public (Priv));
14415 Set_Is_Pure (Full, Is_Pure (Priv));
14416 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14417 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14418 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14419 Set_Has_Pragma_Unreferenced_Objects
14420 (Full, Has_Pragma_Unreferenced_Objects
14421 (Priv));
14422
14423 Conditional_Delay (Full, Priv);
14424
14425 if Is_Tagged_Type (Full) then
14426 Set_Direct_Primitive_Operations
14427 (Full, Direct_Primitive_Operations (Priv));
14428 Set_No_Tagged_Streams_Pragma
14429 (Full, No_Tagged_Streams_Pragma (Priv));
14430
14431 if Is_Base_Type (Priv) then
14432 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14433 end if;
14434 end if;
14435
14436 Set_Is_Volatile (Full, Is_Volatile (Priv));
14437 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14438 Set_Scope (Full, Scope (Priv));
14439 Set_Prev_Entity (Full, Prev_Entity (Priv));
14440 Set_Next_Entity (Full, Next_Entity (Priv));
14441 Set_First_Entity (Full, First_Entity (Priv));
14442 Set_Last_Entity (Full, Last_Entity (Priv));
14443
14444 -- If access types have been recorded for later handling, keep them in
14445 -- the full view so that they get handled when the full view freeze
14446 -- node is expanded.
14447
14448 if Present (Freeze_Node (Priv))
14449 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14450 then
14451 Ensure_Freeze_Node (Full);
14452 Set_Access_Types_To_Process
14453 (Freeze_Node (Full),
14454 Access_Types_To_Process (Freeze_Node (Priv)));
14455 end if;
14456
14457 -- Swap the two entities. Now Private is the full type entity and Full
14458 -- is the private one. They will be swapped back at the end of the
14459 -- private part. This swapping ensures that the entity that is visible
14460 -- in the private part is the full declaration.
14461
14462 Exchange_Entities (Priv, Full);
14463 Append_Entity (Full, Scope (Full));
14464 end Copy_And_Swap;
14465
14466 -------------------------------------
14467 -- Copy_Array_Base_Type_Attributes --
14468 -------------------------------------
14469
14470 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14471 begin
14472 Set_Component_Alignment (T1, Component_Alignment (T2));
14473 Set_Component_Type (T1, Component_Type (T2));
14474 Set_Component_Size (T1, Component_Size (T2));
14475 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14476 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14477 Propagate_Concurrent_Flags (T1, T2);
14478 Set_Is_Packed (T1, Is_Packed (T2));
14479 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14480 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14481 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14482 end Copy_Array_Base_Type_Attributes;
14483
14484 -----------------------------------
14485 -- Copy_Array_Subtype_Attributes --
14486 -----------------------------------
14487
14488 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14489 begin
14490 Set_Size_Info (T1, T2);
14491
14492 Set_First_Index (T1, First_Index (T2));
14493 Set_Is_Aliased (T1, Is_Aliased (T2));
14494 Set_Is_Volatile (T1, Is_Volatile (T2));
14495 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14496 Set_Is_Constrained (T1, Is_Constrained (T2));
14497 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14498 Inherit_Rep_Item_Chain (T1, T2);
14499 Set_Convention (T1, Convention (T2));
14500 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14501 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14502 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14503 end Copy_Array_Subtype_Attributes;
14504
14505 -----------------------------------
14506 -- Create_Constrained_Components --
14507 -----------------------------------
14508
14509 procedure Create_Constrained_Components
14510 (Subt : Entity_Id;
14511 Decl_Node : Node_Id;
14512 Typ : Entity_Id;
14513 Constraints : Elist_Id)
14514 is
14515 Loc : constant Source_Ptr := Sloc (Subt);
14516 Comp_List : constant Elist_Id := New_Elmt_List;
14517 Parent_Type : constant Entity_Id := Etype (Typ);
14518 Assoc_List : constant List_Id := New_List;
14519 Discr_Val : Elmt_Id;
14520 Errors : Boolean;
14521 New_C : Entity_Id;
14522 Old_C : Entity_Id;
14523 Is_Static : Boolean := True;
14524
14525 procedure Collect_Fixed_Components (Typ : Entity_Id);
14526 -- Collect parent type components that do not appear in a variant part
14527
14528 procedure Create_All_Components;
14529 -- Iterate over Comp_List to create the components of the subtype
14530
14531 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14532 -- Creates a new component from Old_Compon, copying all the fields from
14533 -- it, including its Etype, inserts the new component in the Subt entity
14534 -- chain and returns the new component.
14535
14536 function Is_Variant_Record (T : Entity_Id) return Boolean;
14537 -- If true, and discriminants are static, collect only components from
14538 -- variants selected by discriminant values.
14539
14540 ------------------------------
14541 -- Collect_Fixed_Components --
14542 ------------------------------
14543
14544 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14545 begin
14546 -- Build association list for discriminants, and find components of the
14547 -- variant part selected by the values of the discriminants.
14548
14549 Old_C := First_Discriminant (Typ);
14550 Discr_Val := First_Elmt (Constraints);
14551 while Present (Old_C) loop
14552 Append_To (Assoc_List,
14553 Make_Component_Association (Loc,
14554 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14555 Expression => New_Copy (Node (Discr_Val))));
14556
14557 Next_Elmt (Discr_Val);
14558 Next_Discriminant (Old_C);
14559 end loop;
14560
14561 -- The tag and the possible parent component are unconditionally in
14562 -- the subtype.
14563
14564 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14565 Old_C := First_Component (Typ);
14566 while Present (Old_C) loop
14567 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14568 Append_Elmt (Old_C, Comp_List);
14569 end if;
14570
14571 Next_Component (Old_C);
14572 end loop;
14573 end if;
14574 end Collect_Fixed_Components;
14575
14576 ---------------------------
14577 -- Create_All_Components --
14578 ---------------------------
14579
14580 procedure Create_All_Components is
14581 Comp : Elmt_Id;
14582
14583 begin
14584 Comp := First_Elmt (Comp_List);
14585 while Present (Comp) loop
14586 Old_C := Node (Comp);
14587 New_C := Create_Component (Old_C);
14588
14589 Set_Etype
14590 (New_C,
14591 Constrain_Component_Type
14592 (Old_C, Subt, Decl_Node, Typ, Constraints));
14593 Set_Is_Public (New_C, Is_Public (Subt));
14594
14595 Next_Elmt (Comp);
14596 end loop;
14597 end Create_All_Components;
14598
14599 ----------------------
14600 -- Create_Component --
14601 ----------------------
14602
14603 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14604 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14605
14606 begin
14607 if Ekind (Old_Compon) = E_Discriminant
14608 and then Is_Completely_Hidden (Old_Compon)
14609 then
14610 -- This is a shadow discriminant created for a discriminant of
14611 -- the parent type, which needs to be present in the subtype.
14612 -- Give the shadow discriminant an internal name that cannot
14613 -- conflict with that of visible components.
14614
14615 Set_Chars (New_Compon, New_Internal_Name ('C'));
14616 end if;
14617
14618 -- Set the parent so we have a proper link for freezing etc. This is
14619 -- not a real parent pointer, since of course our parent does not own
14620 -- up to us and reference us, we are an illegitimate child of the
14621 -- original parent.
14622
14623 Set_Parent (New_Compon, Parent (Old_Compon));
14624
14625 -- We do not want this node marked as Comes_From_Source, since
14626 -- otherwise it would get first class status and a separate cross-
14627 -- reference line would be generated. Illegitimate children do not
14628 -- rate such recognition.
14629
14630 Set_Comes_From_Source (New_Compon, False);
14631
14632 -- But it is a real entity, and a birth certificate must be properly
14633 -- registered by entering it into the entity list.
14634
14635 Enter_Name (New_Compon);
14636
14637 return New_Compon;
14638 end Create_Component;
14639
14640 -----------------------
14641 -- Is_Variant_Record --
14642 -----------------------
14643
14644 function Is_Variant_Record (T : Entity_Id) return Boolean is
14645 begin
14646 return Nkind (Parent (T)) = N_Full_Type_Declaration
14647 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14648 and then Present (Component_List (Type_Definition (Parent (T))))
14649 and then
14650 Present
14651 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14652 end Is_Variant_Record;
14653
14654 -- Start of processing for Create_Constrained_Components
14655
14656 begin
14657 pragma Assert (Subt /= Base_Type (Subt));
14658 pragma Assert (Typ = Base_Type (Typ));
14659
14660 Set_First_Entity (Subt, Empty);
14661 Set_Last_Entity (Subt, Empty);
14662
14663 -- Check whether constraint is fully static, in which case we can
14664 -- optimize the list of components.
14665
14666 Discr_Val := First_Elmt (Constraints);
14667 while Present (Discr_Val) loop
14668 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14669 Is_Static := False;
14670 exit;
14671 end if;
14672
14673 Next_Elmt (Discr_Val);
14674 end loop;
14675
14676 Set_Has_Static_Discriminants (Subt, Is_Static);
14677
14678 Push_Scope (Subt);
14679
14680 -- Inherit the discriminants of the parent type
14681
14682 Add_Discriminants : declare
14683 Num_Disc : Nat;
14684 Num_Gird : Nat;
14685
14686 begin
14687 Num_Disc := 0;
14688 Old_C := First_Discriminant (Typ);
14689
14690 while Present (Old_C) loop
14691 Num_Disc := Num_Disc + 1;
14692 New_C := Create_Component (Old_C);
14693 Set_Is_Public (New_C, Is_Public (Subt));
14694 Next_Discriminant (Old_C);
14695 end loop;
14696
14697 -- For an untagged derived subtype, the number of discriminants may
14698 -- be smaller than the number of inherited discriminants, because
14699 -- several of them may be renamed by a single new discriminant or
14700 -- constrained. In this case, add the hidden discriminants back into
14701 -- the subtype, because they need to be present if the optimizer of
14702 -- the GCC 4.x back-end decides to break apart assignments between
14703 -- objects using the parent view into member-wise assignments.
14704
14705 Num_Gird := 0;
14706
14707 if Is_Derived_Type (Typ)
14708 and then not Is_Tagged_Type (Typ)
14709 then
14710 Old_C := First_Stored_Discriminant (Typ);
14711
14712 while Present (Old_C) loop
14713 Num_Gird := Num_Gird + 1;
14714 Next_Stored_Discriminant (Old_C);
14715 end loop;
14716 end if;
14717
14718 if Num_Gird > Num_Disc then
14719
14720 -- Find out multiple uses of new discriminants, and add hidden
14721 -- components for the extra renamed discriminants. We recognize
14722 -- multiple uses through the Corresponding_Discriminant of a
14723 -- new discriminant: if it constrains several old discriminants,
14724 -- this field points to the last one in the parent type. The
14725 -- stored discriminants of the derived type have the same name
14726 -- as those of the parent.
14727
14728 declare
14729 Constr : Elmt_Id;
14730 New_Discr : Entity_Id;
14731 Old_Discr : Entity_Id;
14732
14733 begin
14734 Constr := First_Elmt (Stored_Constraint (Typ));
14735 Old_Discr := First_Stored_Discriminant (Typ);
14736 while Present (Constr) loop
14737 if Is_Entity_Name (Node (Constr))
14738 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14739 then
14740 New_Discr := Entity (Node (Constr));
14741
14742 if Chars (Corresponding_Discriminant (New_Discr)) /=
14743 Chars (Old_Discr)
14744 then
14745 -- The new discriminant has been used to rename a
14746 -- subsequent old discriminant. Introduce a shadow
14747 -- component for the current old discriminant.
14748
14749 New_C := Create_Component (Old_Discr);
14750 Set_Original_Record_Component (New_C, Old_Discr);
14751 end if;
14752
14753 else
14754 -- The constraint has eliminated the old discriminant.
14755 -- Introduce a shadow component.
14756
14757 New_C := Create_Component (Old_Discr);
14758 Set_Original_Record_Component (New_C, Old_Discr);
14759 end if;
14760
14761 Next_Elmt (Constr);
14762 Next_Stored_Discriminant (Old_Discr);
14763 end loop;
14764 end;
14765 end if;
14766 end Add_Discriminants;
14767
14768 if Is_Static
14769 and then Is_Variant_Record (Typ)
14770 then
14771 Collect_Fixed_Components (Typ);
14772
14773 Gather_Components (
14774 Typ,
14775 Component_List (Type_Definition (Parent (Typ))),
14776 Governed_By => Assoc_List,
14777 Into => Comp_List,
14778 Report_Errors => Errors);
14779 pragma Assert (not Errors
14780 or else Serious_Errors_Detected > 0);
14781
14782 Create_All_Components;
14783
14784 -- If the subtype declaration is created for a tagged type derivation
14785 -- with constraints, we retrieve the record definition of the parent
14786 -- type to select the components of the proper variant.
14787
14788 elsif Is_Static
14789 and then Is_Tagged_Type (Typ)
14790 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14791 and then
14792 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14793 and then Is_Variant_Record (Parent_Type)
14794 then
14795 Collect_Fixed_Components (Typ);
14796
14797 Gather_Components
14798 (Typ,
14799 Component_List (Type_Definition (Parent (Parent_Type))),
14800 Governed_By => Assoc_List,
14801 Into => Comp_List,
14802 Report_Errors => Errors);
14803
14804 -- Note: previously there was a check at this point that no errors
14805 -- were detected. As a consequence of AI05-220 there may be an error
14806 -- if an inherited discriminant that controls a variant has a non-
14807 -- static constraint.
14808
14809 -- If the tagged derivation has a type extension, collect all the
14810 -- new components therein.
14811
14812 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14813 then
14814 Old_C := First_Component (Typ);
14815 while Present (Old_C) loop
14816 if Original_Record_Component (Old_C) = Old_C
14817 and then Chars (Old_C) /= Name_uTag
14818 and then Chars (Old_C) /= Name_uParent
14819 then
14820 Append_Elmt (Old_C, Comp_List);
14821 end if;
14822
14823 Next_Component (Old_C);
14824 end loop;
14825 end if;
14826
14827 Create_All_Components;
14828
14829 else
14830 -- If discriminants are not static, or if this is a multi-level type
14831 -- extension, we have to include all components of the parent type.
14832
14833 Old_C := First_Component (Typ);
14834 while Present (Old_C) loop
14835 New_C := Create_Component (Old_C);
14836
14837 Set_Etype
14838 (New_C,
14839 Constrain_Component_Type
14840 (Old_C, Subt, Decl_Node, Typ, Constraints));
14841 Set_Is_Public (New_C, Is_Public (Subt));
14842
14843 Next_Component (Old_C);
14844 end loop;
14845 end if;
14846
14847 End_Scope;
14848 end Create_Constrained_Components;
14849
14850 ------------------------------------------
14851 -- Decimal_Fixed_Point_Type_Declaration --
14852 ------------------------------------------
14853
14854 procedure Decimal_Fixed_Point_Type_Declaration
14855 (T : Entity_Id;
14856 Def : Node_Id)
14857 is
14858 Loc : constant Source_Ptr := Sloc (Def);
14859 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14860 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14861 Implicit_Base : Entity_Id;
14862 Digs_Val : Uint;
14863 Delta_Val : Ureal;
14864 Scale_Val : Uint;
14865 Bound_Val : Ureal;
14866
14867 begin
14868 Check_SPARK_05_Restriction
14869 ("decimal fixed point type is not allowed", Def);
14870 Check_Restriction (No_Fixed_Point, Def);
14871
14872 -- Create implicit base type
14873
14874 Implicit_Base :=
14875 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14876 Set_Etype (Implicit_Base, Implicit_Base);
14877
14878 -- Analyze and process delta expression
14879
14880 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14881
14882 Check_Delta_Expression (Delta_Expr);
14883 Delta_Val := Expr_Value_R (Delta_Expr);
14884
14885 -- Check delta is power of 10, and determine scale value from it
14886
14887 declare
14888 Val : Ureal;
14889
14890 begin
14891 Scale_Val := Uint_0;
14892 Val := Delta_Val;
14893
14894 if Val < Ureal_1 then
14895 while Val < Ureal_1 loop
14896 Val := Val * Ureal_10;
14897 Scale_Val := Scale_Val + 1;
14898 end loop;
14899
14900 if Scale_Val > 18 then
14901 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14902 Scale_Val := UI_From_Int (+18);
14903 end if;
14904
14905 else
14906 while Val > Ureal_1 loop
14907 Val := Val / Ureal_10;
14908 Scale_Val := Scale_Val - 1;
14909 end loop;
14910
14911 if Scale_Val < -18 then
14912 Error_Msg_N ("scale is less than minimum value of -18", Def);
14913 Scale_Val := UI_From_Int (-18);
14914 end if;
14915 end if;
14916
14917 if Val /= Ureal_1 then
14918 Error_Msg_N ("delta expression must be a power of 10", Def);
14919 Delta_Val := Ureal_10 ** (-Scale_Val);
14920 end if;
14921 end;
14922
14923 -- Set delta, scale and small (small = delta for decimal type)
14924
14925 Set_Delta_Value (Implicit_Base, Delta_Val);
14926 Set_Scale_Value (Implicit_Base, Scale_Val);
14927 Set_Small_Value (Implicit_Base, Delta_Val);
14928
14929 -- Analyze and process digits expression
14930
14931 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14932 Check_Digits_Expression (Digs_Expr);
14933 Digs_Val := Expr_Value (Digs_Expr);
14934
14935 if Digs_Val > 18 then
14936 Digs_Val := UI_From_Int (+18);
14937 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14938 end if;
14939
14940 Set_Digits_Value (Implicit_Base, Digs_Val);
14941 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14942
14943 -- Set range of base type from digits value for now. This will be
14944 -- expanded to represent the true underlying base range by Freeze.
14945
14946 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14947
14948 -- Note: We leave size as zero for now, size will be set at freeze
14949 -- time. We have to do this for ordinary fixed-point, because the size
14950 -- depends on the specified small, and we might as well do the same for
14951 -- decimal fixed-point.
14952
14953 pragma Assert (Esize (Implicit_Base) = Uint_0);
14954
14955 -- If there are bounds given in the declaration use them as the
14956 -- bounds of the first named subtype.
14957
14958 if Present (Real_Range_Specification (Def)) then
14959 declare
14960 RRS : constant Node_Id := Real_Range_Specification (Def);
14961 Low : constant Node_Id := Low_Bound (RRS);
14962 High : constant Node_Id := High_Bound (RRS);
14963 Low_Val : Ureal;
14964 High_Val : Ureal;
14965
14966 begin
14967 Analyze_And_Resolve (Low, Any_Real);
14968 Analyze_And_Resolve (High, Any_Real);
14969 Check_Real_Bound (Low);
14970 Check_Real_Bound (High);
14971 Low_Val := Expr_Value_R (Low);
14972 High_Val := Expr_Value_R (High);
14973
14974 if Low_Val < (-Bound_Val) then
14975 Error_Msg_N
14976 ("range low bound too small for digits value", Low);
14977 Low_Val := -Bound_Val;
14978 end if;
14979
14980 if High_Val > Bound_Val then
14981 Error_Msg_N
14982 ("range high bound too large for digits value", High);
14983 High_Val := Bound_Val;
14984 end if;
14985
14986 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14987 end;
14988
14989 -- If no explicit range, use range that corresponds to given
14990 -- digits value. This will end up as the final range for the
14991 -- first subtype.
14992
14993 else
14994 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14995 end if;
14996
14997 -- Complete entity for first subtype. The inheritance of the rep item
14998 -- chain ensures that SPARK-related pragmas are not clobbered when the
14999 -- decimal fixed point type acts as a full view of a private type.
15000
15001 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15002 Set_Etype (T, Implicit_Base);
15003 Set_Size_Info (T, Implicit_Base);
15004 Inherit_Rep_Item_Chain (T, Implicit_Base);
15005 Set_Digits_Value (T, Digs_Val);
15006 Set_Delta_Value (T, Delta_Val);
15007 Set_Small_Value (T, Delta_Val);
15008 Set_Scale_Value (T, Scale_Val);
15009 Set_Is_Constrained (T);
15010 end Decimal_Fixed_Point_Type_Declaration;
15011
15012 -----------------------------------
15013 -- Derive_Progenitor_Subprograms --
15014 -----------------------------------
15015
15016 procedure Derive_Progenitor_Subprograms
15017 (Parent_Type : Entity_Id;
15018 Tagged_Type : Entity_Id)
15019 is
15020 E : Entity_Id;
15021 Elmt : Elmt_Id;
15022 Iface : Entity_Id;
15023 Iface_Alias : Entity_Id;
15024 Iface_Elmt : Elmt_Id;
15025 Iface_Subp : Entity_Id;
15026 New_Subp : Entity_Id := Empty;
15027 Prim_Elmt : Elmt_Id;
15028 Subp : Entity_Id;
15029 Typ : Entity_Id;
15030
15031 begin
15032 pragma Assert (Ada_Version >= Ada_2005
15033 and then Is_Record_Type (Tagged_Type)
15034 and then Is_Tagged_Type (Tagged_Type)
15035 and then Has_Interfaces (Tagged_Type));
15036
15037 -- Step 1: Transfer to the full-view primitives associated with the
15038 -- partial-view that cover interface primitives. Conceptually this
15039 -- work should be done later by Process_Full_View; done here to
15040 -- simplify its implementation at later stages. It can be safely
15041 -- done here because interfaces must be visible in the partial and
15042 -- private view (RM 7.3(7.3/2)).
15043
15044 -- Small optimization: This work is only required if the parent may
15045 -- have entities whose Alias attribute reference an interface primitive.
15046 -- Such a situation may occur if the parent is an abstract type and the
15047 -- primitive has not been yet overridden or if the parent is a generic
15048 -- formal type covering interfaces.
15049
15050 -- If the tagged type is not abstract, it cannot have abstract
15051 -- primitives (the only entities in the list of primitives of
15052 -- non-abstract tagged types that can reference abstract primitives
15053 -- through its Alias attribute are the internal entities that have
15054 -- attribute Interface_Alias, and these entities are generated later
15055 -- by Add_Internal_Interface_Entities).
15056
15057 if In_Private_Part (Current_Scope)
15058 and then (Is_Abstract_Type (Parent_Type)
15059 or else
15060 Is_Generic_Type (Parent_Type))
15061 then
15062 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15063 while Present (Elmt) loop
15064 Subp := Node (Elmt);
15065
15066 -- At this stage it is not possible to have entities in the list
15067 -- of primitives that have attribute Interface_Alias.
15068
15069 pragma Assert (No (Interface_Alias (Subp)));
15070
15071 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15072
15073 if Is_Interface (Typ) then
15074 E := Find_Primitive_Covering_Interface
15075 (Tagged_Type => Tagged_Type,
15076 Iface_Prim => Subp);
15077
15078 if Present (E)
15079 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15080 then
15081 Replace_Elmt (Elmt, E);
15082 Remove_Homonym (Subp);
15083 end if;
15084 end if;
15085
15086 Next_Elmt (Elmt);
15087 end loop;
15088 end if;
15089
15090 -- Step 2: Add primitives of progenitors that are not implemented by
15091 -- parents of Tagged_Type.
15092
15093 if Present (Interfaces (Base_Type (Tagged_Type))) then
15094 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15095 while Present (Iface_Elmt) loop
15096 Iface := Node (Iface_Elmt);
15097
15098 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15099 while Present (Prim_Elmt) loop
15100 Iface_Subp := Node (Prim_Elmt);
15101 Iface_Alias := Ultimate_Alias (Iface_Subp);
15102
15103 -- Exclude derivation of predefined primitives except those
15104 -- that come from source, or are inherited from one that comes
15105 -- from source. Required to catch declarations of equality
15106 -- operators of interfaces. For example:
15107
15108 -- type Iface is interface;
15109 -- function "=" (Left, Right : Iface) return Boolean;
15110
15111 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15112 or else Comes_From_Source (Iface_Alias)
15113 then
15114 E :=
15115 Find_Primitive_Covering_Interface
15116 (Tagged_Type => Tagged_Type,
15117 Iface_Prim => Iface_Subp);
15118
15119 -- If not found we derive a new primitive leaving its alias
15120 -- attribute referencing the interface primitive.
15121
15122 if No (E) then
15123 Derive_Subprogram
15124 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15125
15126 -- Ada 2012 (AI05-0197): If the covering primitive's name
15127 -- differs from the name of the interface primitive then it
15128 -- is a private primitive inherited from a parent type. In
15129 -- such case, given that Tagged_Type covers the interface,
15130 -- the inherited private primitive becomes visible. For such
15131 -- purpose we add a new entity that renames the inherited
15132 -- private primitive.
15133
15134 elsif Chars (E) /= Chars (Iface_Subp) then
15135 pragma Assert (Has_Suffix (E, 'P'));
15136 Derive_Subprogram
15137 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15138 Set_Alias (New_Subp, E);
15139 Set_Is_Abstract_Subprogram (New_Subp,
15140 Is_Abstract_Subprogram (E));
15141
15142 -- Propagate to the full view interface entities associated
15143 -- with the partial view.
15144
15145 elsif In_Private_Part (Current_Scope)
15146 and then Present (Alias (E))
15147 and then Alias (E) = Iface_Subp
15148 and then
15149 List_Containing (Parent (E)) /=
15150 Private_Declarations
15151 (Specification
15152 (Unit_Declaration_Node (Current_Scope)))
15153 then
15154 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15155 end if;
15156 end if;
15157
15158 Next_Elmt (Prim_Elmt);
15159 end loop;
15160
15161 Next_Elmt (Iface_Elmt);
15162 end loop;
15163 end if;
15164 end Derive_Progenitor_Subprograms;
15165
15166 -----------------------
15167 -- Derive_Subprogram --
15168 -----------------------
15169
15170 procedure Derive_Subprogram
15171 (New_Subp : out Entity_Id;
15172 Parent_Subp : Entity_Id;
15173 Derived_Type : Entity_Id;
15174 Parent_Type : Entity_Id;
15175 Actual_Subp : Entity_Id := Empty)
15176 is
15177 Formal : Entity_Id;
15178 -- Formal parameter of parent primitive operation
15179
15180 Formal_Of_Actual : Entity_Id;
15181 -- Formal parameter of actual operation, when the derivation is to
15182 -- create a renaming for a primitive operation of an actual in an
15183 -- instantiation.
15184
15185 New_Formal : Entity_Id;
15186 -- Formal of inherited operation
15187
15188 Visible_Subp : Entity_Id := Parent_Subp;
15189
15190 function Is_Private_Overriding return Boolean;
15191 -- If Subp is a private overriding of a visible operation, the inherited
15192 -- operation derives from the overridden op (even though its body is the
15193 -- overriding one) and the inherited operation is visible now. See
15194 -- sem_disp to see the full details of the handling of the overridden
15195 -- subprogram, which is removed from the list of primitive operations of
15196 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15197 -- and used to diagnose abstract operations that need overriding in the
15198 -- derived type.
15199
15200 procedure Replace_Type (Id, New_Id : Entity_Id);
15201 -- When the type is an anonymous access type, create a new access type
15202 -- designating the derived type.
15203
15204 procedure Set_Derived_Name;
15205 -- This procedure sets the appropriate Chars name for New_Subp. This
15206 -- is normally just a copy of the parent name. An exception arises for
15207 -- type support subprograms, where the name is changed to reflect the
15208 -- name of the derived type, e.g. if type foo is derived from type bar,
15209 -- then a procedure barDA is derived with a name fooDA.
15210
15211 ---------------------------
15212 -- Is_Private_Overriding --
15213 ---------------------------
15214
15215 function Is_Private_Overriding return Boolean is
15216 Prev : Entity_Id;
15217
15218 begin
15219 -- If the parent is not a dispatching operation there is no
15220 -- need to investigate overridings
15221
15222 if not Is_Dispatching_Operation (Parent_Subp) then
15223 return False;
15224 end if;
15225
15226 -- The visible operation that is overridden is a homonym of the
15227 -- parent subprogram. We scan the homonym chain to find the one
15228 -- whose alias is the subprogram we are deriving.
15229
15230 Prev := Current_Entity (Parent_Subp);
15231 while Present (Prev) loop
15232 if Ekind (Prev) = Ekind (Parent_Subp)
15233 and then Alias (Prev) = Parent_Subp
15234 and then Scope (Parent_Subp) = Scope (Prev)
15235 and then not Is_Hidden (Prev)
15236 then
15237 Visible_Subp := Prev;
15238 return True;
15239 end if;
15240
15241 Prev := Homonym (Prev);
15242 end loop;
15243
15244 return False;
15245 end Is_Private_Overriding;
15246
15247 ------------------
15248 -- Replace_Type --
15249 ------------------
15250
15251 procedure Replace_Type (Id, New_Id : Entity_Id) is
15252 Id_Type : constant Entity_Id := Etype (Id);
15253 Acc_Type : Entity_Id;
15254 Par : constant Node_Id := Parent (Derived_Type);
15255
15256 begin
15257 -- When the type is an anonymous access type, create a new access
15258 -- type designating the derived type. This itype must be elaborated
15259 -- at the point of the derivation, not on subsequent calls that may
15260 -- be out of the proper scope for Gigi, so we insert a reference to
15261 -- it after the derivation.
15262
15263 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15264 declare
15265 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15266
15267 begin
15268 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15269 and then Present (Full_View (Desig_Typ))
15270 and then not Is_Private_Type (Parent_Type)
15271 then
15272 Desig_Typ := Full_View (Desig_Typ);
15273 end if;
15274
15275 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15276
15277 -- Ada 2005 (AI-251): Handle also derivations of abstract
15278 -- interface primitives.
15279
15280 or else (Is_Interface (Desig_Typ)
15281 and then not Is_Class_Wide_Type (Desig_Typ))
15282 then
15283 Acc_Type := New_Copy (Id_Type);
15284 Set_Etype (Acc_Type, Acc_Type);
15285 Set_Scope (Acc_Type, New_Subp);
15286
15287 -- Set size of anonymous access type. If we have an access
15288 -- to an unconstrained array, this is a fat pointer, so it
15289 -- is sizes at twice addtress size.
15290
15291 if Is_Array_Type (Desig_Typ)
15292 and then not Is_Constrained (Desig_Typ)
15293 then
15294 Init_Size (Acc_Type, 2 * System_Address_Size);
15295
15296 -- Other cases use a thin pointer
15297
15298 else
15299 Init_Size (Acc_Type, System_Address_Size);
15300 end if;
15301
15302 -- Set remaining characterstics of anonymous access type
15303
15304 Init_Alignment (Acc_Type);
15305 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15306
15307 Set_Etype (New_Id, Acc_Type);
15308 Set_Scope (New_Id, New_Subp);
15309
15310 -- Create a reference to it
15311
15312 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15313
15314 else
15315 Set_Etype (New_Id, Id_Type);
15316 end if;
15317 end;
15318
15319 -- In Ada2012, a formal may have an incomplete type but the type
15320 -- derivation that inherits the primitive follows the full view.
15321
15322 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15323 or else
15324 (Ekind (Id_Type) = E_Record_Type_With_Private
15325 and then Present (Full_View (Id_Type))
15326 and then
15327 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15328 or else
15329 (Ada_Version >= Ada_2012
15330 and then Ekind (Id_Type) = E_Incomplete_Type
15331 and then Full_View (Id_Type) = Parent_Type)
15332 then
15333 -- Constraint checks on formals are generated during expansion,
15334 -- based on the signature of the original subprogram. The bounds
15335 -- of the derived type are not relevant, and thus we can use
15336 -- the base type for the formals. However, the return type may be
15337 -- used in a context that requires that the proper static bounds
15338 -- be used (a case statement, for example) and for those cases
15339 -- we must use the derived type (first subtype), not its base.
15340
15341 -- If the derived_type_definition has no constraints, we know that
15342 -- the derived type has the same constraints as the first subtype
15343 -- of the parent, and we can also use it rather than its base,
15344 -- which can lead to more efficient code.
15345
15346 if Etype (Id) = Parent_Type then
15347 if Is_Scalar_Type (Parent_Type)
15348 and then
15349 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15350 then
15351 Set_Etype (New_Id, Derived_Type);
15352
15353 elsif Nkind (Par) = N_Full_Type_Declaration
15354 and then
15355 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15356 and then
15357 Is_Entity_Name
15358 (Subtype_Indication (Type_Definition (Par)))
15359 then
15360 Set_Etype (New_Id, Derived_Type);
15361
15362 else
15363 Set_Etype (New_Id, Base_Type (Derived_Type));
15364 end if;
15365
15366 else
15367 Set_Etype (New_Id, Base_Type (Derived_Type));
15368 end if;
15369
15370 else
15371 Set_Etype (New_Id, Etype (Id));
15372 end if;
15373 end Replace_Type;
15374
15375 ----------------------
15376 -- Set_Derived_Name --
15377 ----------------------
15378
15379 procedure Set_Derived_Name is
15380 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15381 begin
15382 if Nm = TSS_Null then
15383 Set_Chars (New_Subp, Chars (Parent_Subp));
15384 else
15385 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15386 end if;
15387 end Set_Derived_Name;
15388
15389 -- Start of processing for Derive_Subprogram
15390
15391 begin
15392 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15393 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15394
15395 -- Check whether the inherited subprogram is a private operation that
15396 -- should be inherited but not yet made visible. Such subprograms can
15397 -- become visible at a later point (e.g., the private part of a public
15398 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15399 -- following predicate is true, then this is not such a private
15400 -- operation and the subprogram simply inherits the name of the parent
15401 -- subprogram. Note the special check for the names of controlled
15402 -- operations, which are currently exempted from being inherited with
15403 -- a hidden name because they must be findable for generation of
15404 -- implicit run-time calls.
15405
15406 if not Is_Hidden (Parent_Subp)
15407 or else Is_Internal (Parent_Subp)
15408 or else Is_Private_Overriding
15409 or else Is_Internal_Name (Chars (Parent_Subp))
15410 or else (Is_Controlled (Parent_Type)
15411 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15412 Name_Finalize,
15413 Name_Initialize))
15414 then
15415 Set_Derived_Name;
15416
15417 -- An inherited dispatching equality will be overridden by an internally
15418 -- generated one, or by an explicit one, so preserve its name and thus
15419 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15420 -- private operation it may become invisible if the full view has
15421 -- progenitors, and the dispatch table will be malformed.
15422 -- We check that the type is limited to handle the anomalous declaration
15423 -- of Limited_Controlled, which is derived from a non-limited type, and
15424 -- which is handled specially elsewhere as well.
15425
15426 elsif Chars (Parent_Subp) = Name_Op_Eq
15427 and then Is_Dispatching_Operation (Parent_Subp)
15428 and then Etype (Parent_Subp) = Standard_Boolean
15429 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15430 and then
15431 Etype (First_Formal (Parent_Subp)) =
15432 Etype (Next_Formal (First_Formal (Parent_Subp)))
15433 then
15434 Set_Derived_Name;
15435
15436 -- If parent is hidden, this can be a regular derivation if the
15437 -- parent is immediately visible in a non-instantiating context,
15438 -- or if we are in the private part of an instance. This test
15439 -- should still be refined ???
15440
15441 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15442 -- operation as a non-visible operation in cases where the parent
15443 -- subprogram might not be visible now, but was visible within the
15444 -- original generic, so it would be wrong to make the inherited
15445 -- subprogram non-visible now. (Not clear if this test is fully
15446 -- correct; are there any cases where we should declare the inherited
15447 -- operation as not visible to avoid it being overridden, e.g., when
15448 -- the parent type is a generic actual with private primitives ???)
15449
15450 -- (they should be treated the same as other private inherited
15451 -- subprograms, but it's not clear how to do this cleanly). ???
15452
15453 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15454 and then Is_Immediately_Visible (Parent_Subp)
15455 and then not In_Instance)
15456 or else In_Instance_Not_Visible
15457 then
15458 Set_Derived_Name;
15459
15460 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15461 -- overrides an interface primitive because interface primitives
15462 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15463
15464 elsif Ada_Version >= Ada_2005
15465 and then Is_Dispatching_Operation (Parent_Subp)
15466 and then Present (Covered_Interface_Op (Parent_Subp))
15467 then
15468 Set_Derived_Name;
15469
15470 -- Otherwise, the type is inheriting a private operation, so enter it
15471 -- with a special name so it can't be overridden.
15472
15473 else
15474 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15475 end if;
15476
15477 Set_Parent (New_Subp, Parent (Derived_Type));
15478
15479 if Present (Actual_Subp) then
15480 Replace_Type (Actual_Subp, New_Subp);
15481 else
15482 Replace_Type (Parent_Subp, New_Subp);
15483 end if;
15484
15485 Conditional_Delay (New_Subp, Parent_Subp);
15486
15487 -- If we are creating a renaming for a primitive operation of an
15488 -- actual of a generic derived type, we must examine the signature
15489 -- of the actual primitive, not that of the generic formal, which for
15490 -- example may be an interface. However the name and initial value
15491 -- of the inherited operation are those of the formal primitive.
15492
15493 Formal := First_Formal (Parent_Subp);
15494
15495 if Present (Actual_Subp) then
15496 Formal_Of_Actual := First_Formal (Actual_Subp);
15497 else
15498 Formal_Of_Actual := Empty;
15499 end if;
15500
15501 while Present (Formal) loop
15502 New_Formal := New_Copy (Formal);
15503
15504 -- Normally we do not go copying parents, but in the case of
15505 -- formals, we need to link up to the declaration (which is the
15506 -- parameter specification), and it is fine to link up to the
15507 -- original formal's parameter specification in this case.
15508
15509 Set_Parent (New_Formal, Parent (Formal));
15510 Append_Entity (New_Formal, New_Subp);
15511
15512 if Present (Formal_Of_Actual) then
15513 Replace_Type (Formal_Of_Actual, New_Formal);
15514 Next_Formal (Formal_Of_Actual);
15515 else
15516 Replace_Type (Formal, New_Formal);
15517 end if;
15518
15519 Next_Formal (Formal);
15520 end loop;
15521
15522 -- If this derivation corresponds to a tagged generic actual, then
15523 -- primitive operations rename those of the actual. Otherwise the
15524 -- primitive operations rename those of the parent type, If the parent
15525 -- renames an intrinsic operator, so does the new subprogram. We except
15526 -- concatenation, which is always properly typed, and does not get
15527 -- expanded as other intrinsic operations.
15528
15529 if No (Actual_Subp) then
15530 if Is_Intrinsic_Subprogram (Parent_Subp) then
15531 Set_Is_Intrinsic_Subprogram (New_Subp);
15532
15533 if Present (Alias (Parent_Subp))
15534 and then Chars (Parent_Subp) /= Name_Op_Concat
15535 then
15536 Set_Alias (New_Subp, Alias (Parent_Subp));
15537 else
15538 Set_Alias (New_Subp, Parent_Subp);
15539 end if;
15540
15541 else
15542 Set_Alias (New_Subp, Parent_Subp);
15543 end if;
15544
15545 else
15546 Set_Alias (New_Subp, Actual_Subp);
15547 end if;
15548
15549 -- Derived subprograms of a tagged type must inherit the convention
15550 -- of the parent subprogram (a requirement of AI-117). Derived
15551 -- subprograms of untagged types simply get convention Ada by default.
15552
15553 -- If the derived type is a tagged generic formal type with unknown
15554 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15555
15556 -- However, if the type is derived from a generic formal, the further
15557 -- inherited subprogram has the convention of the non-generic ancestor.
15558 -- Otherwise there would be no way to override the operation.
15559 -- (This is subject to forthcoming ARG discussions).
15560
15561 if Is_Tagged_Type (Derived_Type) then
15562 if Is_Generic_Type (Derived_Type)
15563 and then Has_Unknown_Discriminants (Derived_Type)
15564 then
15565 Set_Convention (New_Subp, Convention_Intrinsic);
15566
15567 else
15568 if Is_Generic_Type (Parent_Type)
15569 and then Has_Unknown_Discriminants (Parent_Type)
15570 then
15571 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15572 else
15573 Set_Convention (New_Subp, Convention (Parent_Subp));
15574 end if;
15575 end if;
15576 end if;
15577
15578 -- Predefined controlled operations retain their name even if the parent
15579 -- is hidden (see above), but they are not primitive operations if the
15580 -- ancestor is not visible, for example if the parent is a private
15581 -- extension completed with a controlled extension. Note that a full
15582 -- type that is controlled can break privacy: the flag Is_Controlled is
15583 -- set on both views of the type.
15584
15585 if Is_Controlled (Parent_Type)
15586 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15587 Name_Adjust,
15588 Name_Finalize)
15589 and then Is_Hidden (Parent_Subp)
15590 and then not Is_Visibly_Controlled (Parent_Type)
15591 then
15592 Set_Is_Hidden (New_Subp);
15593 end if;
15594
15595 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15596 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15597
15598 if Ekind (Parent_Subp) = E_Procedure then
15599 Set_Is_Valued_Procedure
15600 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15601 else
15602 Set_Has_Controlling_Result
15603 (New_Subp, Has_Controlling_Result (Parent_Subp));
15604 end if;
15605
15606 -- No_Return must be inherited properly. If this is overridden in the
15607 -- case of a dispatching operation, then a check is made in Sem_Disp
15608 -- that the overriding operation is also No_Return (no such check is
15609 -- required for the case of non-dispatching operation.
15610
15611 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15612
15613 -- A derived function with a controlling result is abstract. If the
15614 -- Derived_Type is a nonabstract formal generic derived type, then
15615 -- inherited operations are not abstract: the required check is done at
15616 -- instantiation time. If the derivation is for a generic actual, the
15617 -- function is not abstract unless the actual is.
15618
15619 if Is_Generic_Type (Derived_Type)
15620 and then not Is_Abstract_Type (Derived_Type)
15621 then
15622 null;
15623
15624 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15625 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15626
15627 -- A subprogram subject to pragma Extensions_Visible with value False
15628 -- requires overriding if the subprogram has at least one controlling
15629 -- OUT parameter (SPARK RM 6.1.7(6)).
15630
15631 elsif Ada_Version >= Ada_2005
15632 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15633 or else (Is_Tagged_Type (Derived_Type)
15634 and then Etype (New_Subp) = Derived_Type
15635 and then not Is_Null_Extension (Derived_Type))
15636 or else (Is_Tagged_Type (Derived_Type)
15637 and then Ekind (Etype (New_Subp)) =
15638 E_Anonymous_Access_Type
15639 and then Designated_Type (Etype (New_Subp)) =
15640 Derived_Type
15641 and then not Is_Null_Extension (Derived_Type))
15642 or else (Comes_From_Source (Alias (New_Subp))
15643 and then Is_EVF_Procedure (Alias (New_Subp))))
15644 and then No (Actual_Subp)
15645 then
15646 if not Is_Tagged_Type (Derived_Type)
15647 or else Is_Abstract_Type (Derived_Type)
15648 or else Is_Abstract_Subprogram (Alias (New_Subp))
15649 then
15650 Set_Is_Abstract_Subprogram (New_Subp);
15651 else
15652 Set_Requires_Overriding (New_Subp);
15653 end if;
15654
15655 elsif Ada_Version < Ada_2005
15656 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15657 or else (Is_Tagged_Type (Derived_Type)
15658 and then Etype (New_Subp) = Derived_Type
15659 and then No (Actual_Subp)))
15660 then
15661 Set_Is_Abstract_Subprogram (New_Subp);
15662
15663 -- AI05-0097 : an inherited operation that dispatches on result is
15664 -- abstract if the derived type is abstract, even if the parent type
15665 -- is concrete and the derived type is a null extension.
15666
15667 elsif Has_Controlling_Result (Alias (New_Subp))
15668 and then Is_Abstract_Type (Etype (New_Subp))
15669 then
15670 Set_Is_Abstract_Subprogram (New_Subp);
15671
15672 -- Finally, if the parent type is abstract we must verify that all
15673 -- inherited operations are either non-abstract or overridden, or that
15674 -- the derived type itself is abstract (this check is performed at the
15675 -- end of a package declaration, in Check_Abstract_Overriding). A
15676 -- private overriding in the parent type will not be visible in the
15677 -- derivation if we are not in an inner package or in a child unit of
15678 -- the parent type, in which case the abstractness of the inherited
15679 -- operation is carried to the new subprogram.
15680
15681 elsif Is_Abstract_Type (Parent_Type)
15682 and then not In_Open_Scopes (Scope (Parent_Type))
15683 and then Is_Private_Overriding
15684 and then Is_Abstract_Subprogram (Visible_Subp)
15685 then
15686 if No (Actual_Subp) then
15687 Set_Alias (New_Subp, Visible_Subp);
15688 Set_Is_Abstract_Subprogram (New_Subp, True);
15689
15690 else
15691 -- If this is a derivation for an instance of a formal derived
15692 -- type, abstractness comes from the primitive operation of the
15693 -- actual, not from the operation inherited from the ancestor.
15694
15695 Set_Is_Abstract_Subprogram
15696 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15697 end if;
15698 end if;
15699
15700 New_Overloaded_Entity (New_Subp, Derived_Type);
15701
15702 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15703 -- preconditions and the derived type is abstract, the derived operation
15704 -- is abstract as well if parent subprogram is not abstract or null.
15705
15706 if Is_Abstract_Type (Derived_Type)
15707 and then Has_Non_Trivial_Precondition (Parent_Subp)
15708 and then Present (Interfaces (Derived_Type))
15709 then
15710
15711 -- Add useful attributes of subprogram before the freeze point,
15712 -- in case freezing is delayed or there are previous errors.
15713
15714 Set_Is_Dispatching_Operation (New_Subp);
15715
15716 declare
15717 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15718
15719 begin
15720 if Present (Iface_Prim)
15721 and then Has_Non_Trivial_Precondition (Iface_Prim)
15722 then
15723 Set_Is_Abstract_Subprogram (New_Subp);
15724 end if;
15725 end;
15726 end if;
15727
15728 -- Check for case of a derived subprogram for the instantiation of a
15729 -- formal derived tagged type, if so mark the subprogram as dispatching
15730 -- and inherit the dispatching attributes of the actual subprogram. The
15731 -- derived subprogram is effectively renaming of the actual subprogram,
15732 -- so it needs to have the same attributes as the actual.
15733
15734 if Present (Actual_Subp)
15735 and then Is_Dispatching_Operation (Actual_Subp)
15736 then
15737 Set_Is_Dispatching_Operation (New_Subp);
15738
15739 if Present (DTC_Entity (Actual_Subp)) then
15740 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15741 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15742 end if;
15743 end if;
15744
15745 -- Indicate that a derived subprogram does not require a body and that
15746 -- it does not require processing of default expressions.
15747
15748 Set_Has_Completion (New_Subp);
15749 Set_Default_Expressions_Processed (New_Subp);
15750
15751 if Ekind (New_Subp) = E_Function then
15752 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15753 end if;
15754 end Derive_Subprogram;
15755
15756 ------------------------
15757 -- Derive_Subprograms --
15758 ------------------------
15759
15760 procedure Derive_Subprograms
15761 (Parent_Type : Entity_Id;
15762 Derived_Type : Entity_Id;
15763 Generic_Actual : Entity_Id := Empty)
15764 is
15765 Op_List : constant Elist_Id :=
15766 Collect_Primitive_Operations (Parent_Type);
15767
15768 function Check_Derived_Type return Boolean;
15769 -- Check that all the entities derived from Parent_Type are found in
15770 -- the list of primitives of Derived_Type exactly in the same order.
15771
15772 procedure Derive_Interface_Subprogram
15773 (New_Subp : out Entity_Id;
15774 Subp : Entity_Id;
15775 Actual_Subp : Entity_Id);
15776 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15777 -- (which is an interface primitive). If Generic_Actual is present then
15778 -- Actual_Subp is the actual subprogram corresponding with the generic
15779 -- subprogram Subp.
15780
15781 ------------------------
15782 -- Check_Derived_Type --
15783 ------------------------
15784
15785 function Check_Derived_Type return Boolean is
15786 E : Entity_Id;
15787 Elmt : Elmt_Id;
15788 List : Elist_Id;
15789 New_Subp : Entity_Id;
15790 Op_Elmt : Elmt_Id;
15791 Subp : Entity_Id;
15792
15793 begin
15794 -- Traverse list of entities in the current scope searching for
15795 -- an incomplete type whose full-view is derived type.
15796
15797 E := First_Entity (Scope (Derived_Type));
15798 while Present (E) and then E /= Derived_Type loop
15799 if Ekind (E) = E_Incomplete_Type
15800 and then Present (Full_View (E))
15801 and then Full_View (E) = Derived_Type
15802 then
15803 -- Disable this test if Derived_Type completes an incomplete
15804 -- type because in such case more primitives can be added
15805 -- later to the list of primitives of Derived_Type by routine
15806 -- Process_Incomplete_Dependents
15807
15808 return True;
15809 end if;
15810
15811 E := Next_Entity (E);
15812 end loop;
15813
15814 List := Collect_Primitive_Operations (Derived_Type);
15815 Elmt := First_Elmt (List);
15816
15817 Op_Elmt := First_Elmt (Op_List);
15818 while Present (Op_Elmt) loop
15819 Subp := Node (Op_Elmt);
15820 New_Subp := Node (Elmt);
15821
15822 -- At this early stage Derived_Type has no entities with attribute
15823 -- Interface_Alias. In addition, such primitives are always
15824 -- located at the end of the list of primitives of Parent_Type.
15825 -- Therefore, if found we can safely stop processing pending
15826 -- entities.
15827
15828 exit when Present (Interface_Alias (Subp));
15829
15830 -- Handle hidden entities
15831
15832 if not Is_Predefined_Dispatching_Operation (Subp)
15833 and then Is_Hidden (Subp)
15834 then
15835 if Present (New_Subp)
15836 and then Primitive_Names_Match (Subp, New_Subp)
15837 then
15838 Next_Elmt (Elmt);
15839 end if;
15840
15841 else
15842 if not Present (New_Subp)
15843 or else Ekind (Subp) /= Ekind (New_Subp)
15844 or else not Primitive_Names_Match (Subp, New_Subp)
15845 then
15846 return False;
15847 end if;
15848
15849 Next_Elmt (Elmt);
15850 end if;
15851
15852 Next_Elmt (Op_Elmt);
15853 end loop;
15854
15855 return True;
15856 end Check_Derived_Type;
15857
15858 ---------------------------------
15859 -- Derive_Interface_Subprogram --
15860 ---------------------------------
15861
15862 procedure Derive_Interface_Subprogram
15863 (New_Subp : out Entity_Id;
15864 Subp : Entity_Id;
15865 Actual_Subp : Entity_Id)
15866 is
15867 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15868 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15869
15870 begin
15871 pragma Assert (Is_Interface (Iface_Type));
15872
15873 Derive_Subprogram
15874 (New_Subp => New_Subp,
15875 Parent_Subp => Iface_Subp,
15876 Derived_Type => Derived_Type,
15877 Parent_Type => Iface_Type,
15878 Actual_Subp => Actual_Subp);
15879
15880 -- Given that this new interface entity corresponds with a primitive
15881 -- of the parent that was not overridden we must leave it associated
15882 -- with its parent primitive to ensure that it will share the same
15883 -- dispatch table slot when overridden. We must set the Alias to Subp
15884 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15885 -- (in case we inherited Subp from Iface_Type via a nonabstract
15886 -- generic formal type).
15887
15888 if No (Actual_Subp) then
15889 Set_Alias (New_Subp, Subp);
15890
15891 declare
15892 T : Entity_Id := Find_Dispatching_Type (Subp);
15893 begin
15894 while Etype (T) /= T loop
15895 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15896 Set_Is_Abstract_Subprogram (New_Subp, False);
15897 exit;
15898 end if;
15899
15900 T := Etype (T);
15901 end loop;
15902 end;
15903
15904 -- For instantiations this is not needed since the previous call to
15905 -- Derive_Subprogram leaves the entity well decorated.
15906
15907 else
15908 pragma Assert (Alias (New_Subp) = Actual_Subp);
15909 null;
15910 end if;
15911 end Derive_Interface_Subprogram;
15912
15913 -- Local variables
15914
15915 Alias_Subp : Entity_Id;
15916 Act_List : Elist_Id;
15917 Act_Elmt : Elmt_Id;
15918 Act_Subp : Entity_Id := Empty;
15919 Elmt : Elmt_Id;
15920 Need_Search : Boolean := False;
15921 New_Subp : Entity_Id := Empty;
15922 Parent_Base : Entity_Id;
15923 Subp : Entity_Id;
15924
15925 -- Start of processing for Derive_Subprograms
15926
15927 begin
15928 if Ekind (Parent_Type) = E_Record_Type_With_Private
15929 and then Has_Discriminants (Parent_Type)
15930 and then Present (Full_View (Parent_Type))
15931 then
15932 Parent_Base := Full_View (Parent_Type);
15933 else
15934 Parent_Base := Parent_Type;
15935 end if;
15936
15937 if Present (Generic_Actual) then
15938 Act_List := Collect_Primitive_Operations (Generic_Actual);
15939 Act_Elmt := First_Elmt (Act_List);
15940 else
15941 Act_List := No_Elist;
15942 Act_Elmt := No_Elmt;
15943 end if;
15944
15945 -- Derive primitives inherited from the parent. Note that if the generic
15946 -- actual is present, this is not really a type derivation, it is a
15947 -- completion within an instance.
15948
15949 -- Case 1: Derived_Type does not implement interfaces
15950
15951 if not Is_Tagged_Type (Derived_Type)
15952 or else (not Has_Interfaces (Derived_Type)
15953 and then not (Present (Generic_Actual)
15954 and then Has_Interfaces (Generic_Actual)))
15955 then
15956 Elmt := First_Elmt (Op_List);
15957 while Present (Elmt) loop
15958 Subp := Node (Elmt);
15959
15960 -- Literals are derived earlier in the process of building the
15961 -- derived type, and are skipped here.
15962
15963 if Ekind (Subp) = E_Enumeration_Literal then
15964 null;
15965
15966 -- The actual is a direct descendant and the common primitive
15967 -- operations appear in the same order.
15968
15969 -- If the generic parent type is present, the derived type is an
15970 -- instance of a formal derived type, and within the instance its
15971 -- operations are those of the actual. We derive from the formal
15972 -- type but make the inherited operations aliases of the
15973 -- corresponding operations of the actual.
15974
15975 else
15976 pragma Assert (No (Node (Act_Elmt))
15977 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15978 and then
15979 Type_Conformant
15980 (Subp, Node (Act_Elmt),
15981 Skip_Controlling_Formals => True)));
15982
15983 Derive_Subprogram
15984 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15985
15986 if Present (Act_Elmt) then
15987 Next_Elmt (Act_Elmt);
15988 end if;
15989 end if;
15990
15991 Next_Elmt (Elmt);
15992 end loop;
15993
15994 -- Case 2: Derived_Type implements interfaces
15995
15996 else
15997 -- If the parent type has no predefined primitives we remove
15998 -- predefined primitives from the list of primitives of generic
15999 -- actual to simplify the complexity of this algorithm.
16000
16001 if Present (Generic_Actual) then
16002 declare
16003 Has_Predefined_Primitives : Boolean := False;
16004
16005 begin
16006 -- Check if the parent type has predefined primitives
16007
16008 Elmt := First_Elmt (Op_List);
16009 while Present (Elmt) loop
16010 Subp := Node (Elmt);
16011
16012 if Is_Predefined_Dispatching_Operation (Subp)
16013 and then not Comes_From_Source (Ultimate_Alias (Subp))
16014 then
16015 Has_Predefined_Primitives := True;
16016 exit;
16017 end if;
16018
16019 Next_Elmt (Elmt);
16020 end loop;
16021
16022 -- Remove predefined primitives of Generic_Actual. We must use
16023 -- an auxiliary list because in case of tagged types the value
16024 -- returned by Collect_Primitive_Operations is the value stored
16025 -- in its Primitive_Operations attribute (and we don't want to
16026 -- modify its current contents).
16027
16028 if not Has_Predefined_Primitives then
16029 declare
16030 Aux_List : constant Elist_Id := New_Elmt_List;
16031
16032 begin
16033 Elmt := First_Elmt (Act_List);
16034 while Present (Elmt) loop
16035 Subp := Node (Elmt);
16036
16037 if not Is_Predefined_Dispatching_Operation (Subp)
16038 or else Comes_From_Source (Subp)
16039 then
16040 Append_Elmt (Subp, Aux_List);
16041 end if;
16042
16043 Next_Elmt (Elmt);
16044 end loop;
16045
16046 Act_List := Aux_List;
16047 end;
16048 end if;
16049
16050 Act_Elmt := First_Elmt (Act_List);
16051 Act_Subp := Node (Act_Elmt);
16052 end;
16053 end if;
16054
16055 -- Stage 1: If the generic actual is not present we derive the
16056 -- primitives inherited from the parent type. If the generic parent
16057 -- type is present, the derived type is an instance of a formal
16058 -- derived type, and within the instance its operations are those of
16059 -- the actual. We derive from the formal type but make the inherited
16060 -- operations aliases of the corresponding operations of the actual.
16061
16062 Elmt := First_Elmt (Op_List);
16063 while Present (Elmt) loop
16064 Subp := Node (Elmt);
16065 Alias_Subp := Ultimate_Alias (Subp);
16066
16067 -- Do not derive internal entities of the parent that link
16068 -- interface primitives with their covering primitive. These
16069 -- entities will be added to this type when frozen.
16070
16071 if Present (Interface_Alias (Subp)) then
16072 goto Continue;
16073 end if;
16074
16075 -- If the generic actual is present find the corresponding
16076 -- operation in the generic actual. If the parent type is a
16077 -- direct ancestor of the derived type then, even if it is an
16078 -- interface, the operations are inherited from the primary
16079 -- dispatch table and are in the proper order. If we detect here
16080 -- that primitives are not in the same order we traverse the list
16081 -- of primitive operations of the actual to find the one that
16082 -- implements the interface primitive.
16083
16084 if Need_Search
16085 or else
16086 (Present (Generic_Actual)
16087 and then Present (Act_Subp)
16088 and then not
16089 (Primitive_Names_Match (Subp, Act_Subp)
16090 and then
16091 Type_Conformant (Subp, Act_Subp,
16092 Skip_Controlling_Formals => True)))
16093 then
16094 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16095 Use_Full_View => True));
16096
16097 -- Remember that we need searching for all pending primitives
16098
16099 Need_Search := True;
16100
16101 -- Handle entities associated with interface primitives
16102
16103 if Present (Alias_Subp)
16104 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16105 and then not Is_Predefined_Dispatching_Operation (Subp)
16106 then
16107 -- Search for the primitive in the homonym chain
16108
16109 Act_Subp :=
16110 Find_Primitive_Covering_Interface
16111 (Tagged_Type => Generic_Actual,
16112 Iface_Prim => Alias_Subp);
16113
16114 -- Previous search may not locate primitives covering
16115 -- interfaces defined in generics units or instantiations.
16116 -- (it fails if the covering primitive has formals whose
16117 -- type is also defined in generics or instantiations).
16118 -- In such case we search in the list of primitives of the
16119 -- generic actual for the internal entity that links the
16120 -- interface primitive and the covering primitive.
16121
16122 if No (Act_Subp)
16123 and then Is_Generic_Type (Parent_Type)
16124 then
16125 -- This code has been designed to handle only generic
16126 -- formals that implement interfaces that are defined
16127 -- in a generic unit or instantiation. If this code is
16128 -- needed for other cases we must review it because
16129 -- (given that it relies on Original_Location to locate
16130 -- the primitive of Generic_Actual that covers the
16131 -- interface) it could leave linked through attribute
16132 -- Alias entities of unrelated instantiations).
16133
16134 pragma Assert
16135 (Is_Generic_Unit
16136 (Scope (Find_Dispatching_Type (Alias_Subp)))
16137 or else
16138 Instantiation_Depth
16139 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16140
16141 declare
16142 Iface_Prim_Loc : constant Source_Ptr :=
16143 Original_Location (Sloc (Alias_Subp));
16144
16145 Elmt : Elmt_Id;
16146 Prim : Entity_Id;
16147
16148 begin
16149 Elmt :=
16150 First_Elmt (Primitive_Operations (Generic_Actual));
16151
16152 Search : while Present (Elmt) loop
16153 Prim := Node (Elmt);
16154
16155 if Present (Interface_Alias (Prim))
16156 and then Original_Location
16157 (Sloc (Interface_Alias (Prim))) =
16158 Iface_Prim_Loc
16159 then
16160 Act_Subp := Alias (Prim);
16161 exit Search;
16162 end if;
16163
16164 Next_Elmt (Elmt);
16165 end loop Search;
16166 end;
16167 end if;
16168
16169 pragma Assert (Present (Act_Subp)
16170 or else Is_Abstract_Type (Generic_Actual)
16171 or else Serious_Errors_Detected > 0);
16172
16173 -- Handle predefined primitives plus the rest of user-defined
16174 -- primitives
16175
16176 else
16177 Act_Elmt := First_Elmt (Act_List);
16178 while Present (Act_Elmt) loop
16179 Act_Subp := Node (Act_Elmt);
16180
16181 exit when Primitive_Names_Match (Subp, Act_Subp)
16182 and then Type_Conformant
16183 (Subp, Act_Subp,
16184 Skip_Controlling_Formals => True)
16185 and then No (Interface_Alias (Act_Subp));
16186
16187 Next_Elmt (Act_Elmt);
16188 end loop;
16189
16190 if No (Act_Elmt) then
16191 Act_Subp := Empty;
16192 end if;
16193 end if;
16194 end if;
16195
16196 -- Case 1: If the parent is a limited interface then it has the
16197 -- predefined primitives of synchronized interfaces. However, the
16198 -- actual type may be a non-limited type and hence it does not
16199 -- have such primitives.
16200
16201 if Present (Generic_Actual)
16202 and then not Present (Act_Subp)
16203 and then Is_Limited_Interface (Parent_Base)
16204 and then Is_Predefined_Interface_Primitive (Subp)
16205 then
16206 null;
16207
16208 -- Case 2: Inherit entities associated with interfaces that were
16209 -- not covered by the parent type. We exclude here null interface
16210 -- primitives because they do not need special management.
16211
16212 -- We also exclude interface operations that are renamings. If the
16213 -- subprogram is an explicit renaming of an interface primitive,
16214 -- it is a regular primitive operation, and the presence of its
16215 -- alias is not relevant: it has to be derived like any other
16216 -- primitive.
16217
16218 elsif Present (Alias (Subp))
16219 and then Nkind (Unit_Declaration_Node (Subp)) /=
16220 N_Subprogram_Renaming_Declaration
16221 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16222 and then not
16223 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16224 and then Null_Present (Parent (Alias_Subp)))
16225 then
16226 -- If this is an abstract private type then we transfer the
16227 -- derivation of the interface primitive from the partial view
16228 -- to the full view. This is safe because all the interfaces
16229 -- must be visible in the partial view. Done to avoid adding
16230 -- a new interface derivation to the private part of the
16231 -- enclosing package; otherwise this new derivation would be
16232 -- decorated as hidden when the analysis of the enclosing
16233 -- package completes.
16234
16235 if Is_Abstract_Type (Derived_Type)
16236 and then In_Private_Part (Current_Scope)
16237 and then Has_Private_Declaration (Derived_Type)
16238 then
16239 declare
16240 Partial_View : Entity_Id;
16241 Elmt : Elmt_Id;
16242 Ent : Entity_Id;
16243
16244 begin
16245 Partial_View := First_Entity (Current_Scope);
16246 loop
16247 exit when No (Partial_View)
16248 or else (Has_Private_Declaration (Partial_View)
16249 and then
16250 Full_View (Partial_View) = Derived_Type);
16251
16252 Next_Entity (Partial_View);
16253 end loop;
16254
16255 -- If the partial view was not found then the source code
16256 -- has errors and the derivation is not needed.
16257
16258 if Present (Partial_View) then
16259 Elmt :=
16260 First_Elmt (Primitive_Operations (Partial_View));
16261 while Present (Elmt) loop
16262 Ent := Node (Elmt);
16263
16264 if Present (Alias (Ent))
16265 and then Ultimate_Alias (Ent) = Alias (Subp)
16266 then
16267 Append_Elmt
16268 (Ent, Primitive_Operations (Derived_Type));
16269 exit;
16270 end if;
16271
16272 Next_Elmt (Elmt);
16273 end loop;
16274
16275 -- If the interface primitive was not found in the
16276 -- partial view then this interface primitive was
16277 -- overridden. We add a derivation to activate in
16278 -- Derive_Progenitor_Subprograms the machinery to
16279 -- search for it.
16280
16281 if No (Elmt) then
16282 Derive_Interface_Subprogram
16283 (New_Subp => New_Subp,
16284 Subp => Subp,
16285 Actual_Subp => Act_Subp);
16286 end if;
16287 end if;
16288 end;
16289 else
16290 Derive_Interface_Subprogram
16291 (New_Subp => New_Subp,
16292 Subp => Subp,
16293 Actual_Subp => Act_Subp);
16294 end if;
16295
16296 -- Case 3: Common derivation
16297
16298 else
16299 Derive_Subprogram
16300 (New_Subp => New_Subp,
16301 Parent_Subp => Subp,
16302 Derived_Type => Derived_Type,
16303 Parent_Type => Parent_Base,
16304 Actual_Subp => Act_Subp);
16305 end if;
16306
16307 -- No need to update Act_Elm if we must search for the
16308 -- corresponding operation in the generic actual
16309
16310 if not Need_Search
16311 and then Present (Act_Elmt)
16312 then
16313 Next_Elmt (Act_Elmt);
16314 Act_Subp := Node (Act_Elmt);
16315 end if;
16316
16317 <<Continue>>
16318 Next_Elmt (Elmt);
16319 end loop;
16320
16321 -- Inherit additional operations from progenitors. If the derived
16322 -- type is a generic actual, there are not new primitive operations
16323 -- for the type because it has those of the actual, and therefore
16324 -- nothing needs to be done. The renamings generated above are not
16325 -- primitive operations, and their purpose is simply to make the
16326 -- proper operations visible within an instantiation.
16327
16328 if No (Generic_Actual) then
16329 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16330 end if;
16331 end if;
16332
16333 -- Final check: Direct descendants must have their primitives in the
16334 -- same order. We exclude from this test untagged types and instances
16335 -- of formal derived types. We skip this test if we have already
16336 -- reported serious errors in the sources.
16337
16338 pragma Assert (not Is_Tagged_Type (Derived_Type)
16339 or else Present (Generic_Actual)
16340 or else Serious_Errors_Detected > 0
16341 or else Check_Derived_Type);
16342 end Derive_Subprograms;
16343
16344 --------------------------------
16345 -- Derived_Standard_Character --
16346 --------------------------------
16347
16348 procedure Derived_Standard_Character
16349 (N : Node_Id;
16350 Parent_Type : Entity_Id;
16351 Derived_Type : Entity_Id)
16352 is
16353 Loc : constant Source_Ptr := Sloc (N);
16354 Def : constant Node_Id := Type_Definition (N);
16355 Indic : constant Node_Id := Subtype_Indication (Def);
16356 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16357 Implicit_Base : constant Entity_Id :=
16358 Create_Itype
16359 (E_Enumeration_Type, N, Derived_Type, 'B');
16360
16361 Lo : Node_Id;
16362 Hi : Node_Id;
16363
16364 begin
16365 Discard_Node (Process_Subtype (Indic, N));
16366
16367 Set_Etype (Implicit_Base, Parent_Base);
16368 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16369 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16370
16371 Set_Is_Character_Type (Implicit_Base, True);
16372 Set_Has_Delayed_Freeze (Implicit_Base);
16373
16374 -- The bounds of the implicit base are the bounds of the parent base.
16375 -- Note that their type is the parent base.
16376
16377 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16378 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16379
16380 Set_Scalar_Range (Implicit_Base,
16381 Make_Range (Loc,
16382 Low_Bound => Lo,
16383 High_Bound => Hi));
16384
16385 Conditional_Delay (Derived_Type, Parent_Type);
16386
16387 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16388 Set_Etype (Derived_Type, Implicit_Base);
16389 Set_Size_Info (Derived_Type, Parent_Type);
16390
16391 if Unknown_RM_Size (Derived_Type) then
16392 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16393 end if;
16394
16395 Set_Is_Character_Type (Derived_Type, True);
16396
16397 if Nkind (Indic) /= N_Subtype_Indication then
16398
16399 -- If no explicit constraint, the bounds are those
16400 -- of the parent type.
16401
16402 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16403 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16404 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16405 end if;
16406
16407 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16408
16409 -- Because the implicit base is used in the conversion of the bounds, we
16410 -- have to freeze it now. This is similar to what is done for numeric
16411 -- types, and it equally suspicious, but otherwise a non-static bound
16412 -- will have a reference to an unfrozen type, which is rejected by Gigi
16413 -- (???). This requires specific care for definition of stream
16414 -- attributes. For details, see comments at the end of
16415 -- Build_Derived_Numeric_Type.
16416
16417 Freeze_Before (N, Implicit_Base);
16418 end Derived_Standard_Character;
16419
16420 ------------------------------
16421 -- Derived_Type_Declaration --
16422 ------------------------------
16423
16424 procedure Derived_Type_Declaration
16425 (T : Entity_Id;
16426 N : Node_Id;
16427 Is_Completion : Boolean)
16428 is
16429 Parent_Type : Entity_Id;
16430
16431 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16432 -- Check whether the parent type is a generic formal, or derives
16433 -- directly or indirectly from one.
16434
16435 ------------------------
16436 -- Comes_From_Generic --
16437 ------------------------
16438
16439 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16440 begin
16441 if Is_Generic_Type (Typ) then
16442 return True;
16443
16444 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16445 return True;
16446
16447 elsif Is_Private_Type (Typ)
16448 and then Present (Full_View (Typ))
16449 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16450 then
16451 return True;
16452
16453 elsif Is_Generic_Actual_Type (Typ) then
16454 return True;
16455
16456 else
16457 return False;
16458 end if;
16459 end Comes_From_Generic;
16460
16461 -- Local variables
16462
16463 Def : constant Node_Id := Type_Definition (N);
16464 Iface_Def : Node_Id;
16465 Indic : constant Node_Id := Subtype_Indication (Def);
16466 Extension : constant Node_Id := Record_Extension_Part (Def);
16467 Parent_Node : Node_Id;
16468 Taggd : Boolean;
16469
16470 -- Start of processing for Derived_Type_Declaration
16471
16472 begin
16473 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16474
16475 if SPARK_Mode = On
16476 and then Is_Tagged_Type (Parent_Type)
16477 then
16478 declare
16479 Partial_View : constant Entity_Id :=
16480 Incomplete_Or_Partial_View (Parent_Type);
16481
16482 begin
16483 -- If the partial view was not found then the parent type is not
16484 -- a private type. Otherwise check if the partial view is a tagged
16485 -- private type.
16486
16487 if Present (Partial_View)
16488 and then Is_Private_Type (Partial_View)
16489 and then not Is_Tagged_Type (Partial_View)
16490 then
16491 Error_Msg_NE
16492 ("cannot derive from & declared as untagged private "
16493 & "(SPARK RM 3.4(1))", N, Partial_View);
16494 end if;
16495 end;
16496 end if;
16497
16498 -- Ada 2005 (AI-251): In case of interface derivation check that the
16499 -- parent is also an interface.
16500
16501 if Interface_Present (Def) then
16502 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16503
16504 if not Is_Interface (Parent_Type) then
16505 Diagnose_Interface (Indic, Parent_Type);
16506
16507 else
16508 Parent_Node := Parent (Base_Type (Parent_Type));
16509 Iface_Def := Type_Definition (Parent_Node);
16510
16511 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16512 -- other limited interfaces.
16513
16514 if Limited_Present (Def) then
16515 if Limited_Present (Iface_Def) then
16516 null;
16517
16518 elsif Protected_Present (Iface_Def) then
16519 Error_Msg_NE
16520 ("descendant of & must be declared as a protected "
16521 & "interface", N, Parent_Type);
16522
16523 elsif Synchronized_Present (Iface_Def) then
16524 Error_Msg_NE
16525 ("descendant of & must be declared as a synchronized "
16526 & "interface", N, Parent_Type);
16527
16528 elsif Task_Present (Iface_Def) then
16529 Error_Msg_NE
16530 ("descendant of & must be declared as a task interface",
16531 N, Parent_Type);
16532
16533 else
16534 Error_Msg_N
16535 ("(Ada 2005) limited interface cannot inherit from "
16536 & "non-limited interface", Indic);
16537 end if;
16538
16539 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16540 -- from non-limited or limited interfaces.
16541
16542 elsif not Protected_Present (Def)
16543 and then not Synchronized_Present (Def)
16544 and then not Task_Present (Def)
16545 then
16546 if Limited_Present (Iface_Def) then
16547 null;
16548
16549 elsif Protected_Present (Iface_Def) then
16550 Error_Msg_NE
16551 ("descendant of & must be declared as a protected "
16552 & "interface", N, Parent_Type);
16553
16554 elsif Synchronized_Present (Iface_Def) then
16555 Error_Msg_NE
16556 ("descendant of & must be declared as a synchronized "
16557 & "interface", N, Parent_Type);
16558
16559 elsif Task_Present (Iface_Def) then
16560 Error_Msg_NE
16561 ("descendant of & must be declared as a task interface",
16562 N, Parent_Type);
16563 else
16564 null;
16565 end if;
16566 end if;
16567 end if;
16568 end if;
16569
16570 if Is_Tagged_Type (Parent_Type)
16571 and then Is_Concurrent_Type (Parent_Type)
16572 and then not Is_Interface (Parent_Type)
16573 then
16574 Error_Msg_N
16575 ("parent type of a record extension cannot be a synchronized "
16576 & "tagged type (RM 3.9.1 (3/1))", N);
16577 Set_Etype (T, Any_Type);
16578 return;
16579 end if;
16580
16581 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16582 -- interfaces
16583
16584 if Is_Tagged_Type (Parent_Type)
16585 and then Is_Non_Empty_List (Interface_List (Def))
16586 then
16587 declare
16588 Intf : Node_Id;
16589 T : Entity_Id;
16590
16591 begin
16592 Intf := First (Interface_List (Def));
16593 while Present (Intf) loop
16594 T := Find_Type_Of_Subtype_Indic (Intf);
16595
16596 if not Is_Interface (T) then
16597 Diagnose_Interface (Intf, T);
16598
16599 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16600 -- a limited type from having a nonlimited progenitor.
16601
16602 elsif (Limited_Present (Def)
16603 or else (not Is_Interface (Parent_Type)
16604 and then Is_Limited_Type (Parent_Type)))
16605 and then not Is_Limited_Interface (T)
16606 then
16607 Error_Msg_NE
16608 ("progenitor interface& of limited type must be limited",
16609 N, T);
16610 end if;
16611
16612 Next (Intf);
16613 end loop;
16614 end;
16615 end if;
16616
16617 if Parent_Type = Any_Type
16618 or else Etype (Parent_Type) = Any_Type
16619 or else (Is_Class_Wide_Type (Parent_Type)
16620 and then Etype (Parent_Type) = T)
16621 then
16622 -- If Parent_Type is undefined or illegal, make new type into a
16623 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16624 -- errors. If this is a self-definition, emit error now.
16625
16626 if T = Parent_Type or else T = Etype (Parent_Type) then
16627 Error_Msg_N ("type cannot be used in its own definition", Indic);
16628 end if;
16629
16630 Set_Ekind (T, Ekind (Parent_Type));
16631 Set_Etype (T, Any_Type);
16632 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16633
16634 if Is_Tagged_Type (T)
16635 and then Is_Record_Type (T)
16636 then
16637 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16638 end if;
16639
16640 return;
16641 end if;
16642
16643 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16644 -- an interface is special because the list of interfaces in the full
16645 -- view can be given in any order. For example:
16646
16647 -- type A is interface;
16648 -- type B is interface and A;
16649 -- type D is new B with private;
16650 -- private
16651 -- type D is new A and B with null record; -- 1 --
16652
16653 -- In this case we perform the following transformation of -1-:
16654
16655 -- type D is new B and A with null record;
16656
16657 -- If the parent of the full-view covers the parent of the partial-view
16658 -- we have two possible cases:
16659
16660 -- 1) They have the same parent
16661 -- 2) The parent of the full-view implements some further interfaces
16662
16663 -- In both cases we do not need to perform the transformation. In the
16664 -- first case the source program is correct and the transformation is
16665 -- not needed; in the second case the source program does not fulfill
16666 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16667 -- later.
16668
16669 -- This transformation not only simplifies the rest of the analysis of
16670 -- this type declaration but also simplifies the correct generation of
16671 -- the object layout to the expander.
16672
16673 if In_Private_Part (Current_Scope)
16674 and then Is_Interface (Parent_Type)
16675 then
16676 declare
16677 Iface : Node_Id;
16678 Partial_View : Entity_Id;
16679 Partial_View_Parent : Entity_Id;
16680 New_Iface : Node_Id;
16681
16682 begin
16683 -- Look for the associated private type declaration
16684
16685 Partial_View := Incomplete_Or_Partial_View (T);
16686
16687 -- If the partial view was not found then the source code has
16688 -- errors and the transformation is not needed.
16689
16690 if Present (Partial_View) then
16691 Partial_View_Parent := Etype (Partial_View);
16692
16693 -- If the parent of the full-view covers the parent of the
16694 -- partial-view we have nothing else to do.
16695
16696 if Interface_Present_In_Ancestor
16697 (Parent_Type, Partial_View_Parent)
16698 then
16699 null;
16700
16701 -- Traverse the list of interfaces of the full-view to look
16702 -- for the parent of the partial-view and perform the tree
16703 -- transformation.
16704
16705 else
16706 Iface := First (Interface_List (Def));
16707 while Present (Iface) loop
16708 if Etype (Iface) = Etype (Partial_View) then
16709 Rewrite (Subtype_Indication (Def),
16710 New_Copy (Subtype_Indication
16711 (Parent (Partial_View))));
16712
16713 New_Iface :=
16714 Make_Identifier (Sloc (N), Chars (Parent_Type));
16715 Append (New_Iface, Interface_List (Def));
16716
16717 -- Analyze the transformed code
16718
16719 Derived_Type_Declaration (T, N, Is_Completion);
16720 return;
16721 end if;
16722
16723 Next (Iface);
16724 end loop;
16725 end if;
16726 end if;
16727 end;
16728 end if;
16729
16730 -- Only composite types other than array types are allowed to have
16731 -- discriminants.
16732
16733 if Present (Discriminant_Specifications (N)) then
16734 if (Is_Elementary_Type (Parent_Type)
16735 or else
16736 Is_Array_Type (Parent_Type))
16737 and then not Error_Posted (N)
16738 then
16739 Error_Msg_N
16740 ("elementary or array type cannot have discriminants",
16741 Defining_Identifier (First (Discriminant_Specifications (N))));
16742
16743 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16744 -- only if we are not already processing a malformed syntax tree.
16745
16746 if Is_Type (T) then
16747 Set_Has_Discriminants (T, False);
16748 end if;
16749
16750 -- The type is allowed to have discriminants
16751
16752 else
16753 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16754 end if;
16755 end if;
16756
16757 -- In Ada 83, a derived type defined in a package specification cannot
16758 -- be used for further derivation until the end of its visible part.
16759 -- Note that derivation in the private part of the package is allowed.
16760
16761 if Ada_Version = Ada_83
16762 and then Is_Derived_Type (Parent_Type)
16763 and then In_Visible_Part (Scope (Parent_Type))
16764 then
16765 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16766 Error_Msg_N
16767 ("(Ada 83): premature use of type for derivation", Indic);
16768 end if;
16769 end if;
16770
16771 -- Check for early use of incomplete or private type
16772
16773 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16774 Error_Msg_N ("premature derivation of incomplete type", Indic);
16775 return;
16776
16777 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16778 and then not Comes_From_Generic (Parent_Type))
16779 or else Has_Private_Component (Parent_Type)
16780 then
16781 -- The ancestor type of a formal type can be incomplete, in which
16782 -- case only the operations of the partial view are available in the
16783 -- generic. Subsequent checks may be required when the full view is
16784 -- analyzed to verify that a derivation from a tagged type has an
16785 -- extension.
16786
16787 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16788 null;
16789
16790 elsif No (Underlying_Type (Parent_Type))
16791 or else Has_Private_Component (Parent_Type)
16792 then
16793 Error_Msg_N
16794 ("premature derivation of derived or private type", Indic);
16795
16796 -- Flag the type itself as being in error, this prevents some
16797 -- nasty problems with subsequent uses of the malformed type.
16798
16799 Set_Error_Posted (T);
16800
16801 -- Check that within the immediate scope of an untagged partial
16802 -- view it's illegal to derive from the partial view if the
16803 -- full view is tagged. (7.3(7))
16804
16805 -- We verify that the Parent_Type is a partial view by checking
16806 -- that it is not a Full_Type_Declaration (i.e. a private type or
16807 -- private extension declaration), to distinguish a partial view
16808 -- from a derivation from a private type which also appears as
16809 -- E_Private_Type. If the parent base type is not declared in an
16810 -- enclosing scope there is no need to check.
16811
16812 elsif Present (Full_View (Parent_Type))
16813 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16814 and then not Is_Tagged_Type (Parent_Type)
16815 and then Is_Tagged_Type (Full_View (Parent_Type))
16816 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16817 then
16818 Error_Msg_N
16819 ("premature derivation from type with tagged full view",
16820 Indic);
16821 end if;
16822 end if;
16823
16824 -- Check that form of derivation is appropriate
16825
16826 Taggd := Is_Tagged_Type (Parent_Type);
16827
16828 -- Set the parent type to the class-wide type's specific type in this
16829 -- case to prevent cascading errors
16830
16831 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16832 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16833 Set_Etype (T, Etype (Parent_Type));
16834 return;
16835 end if;
16836
16837 if Present (Extension) and then not Taggd then
16838 Error_Msg_N
16839 ("type derived from untagged type cannot have extension", Indic);
16840
16841 elsif No (Extension) and then Taggd then
16842
16843 -- If this declaration is within a private part (or body) of a
16844 -- generic instantiation then the derivation is allowed (the parent
16845 -- type can only appear tagged in this case if it's a generic actual
16846 -- type, since it would otherwise have been rejected in the analysis
16847 -- of the generic template).
16848
16849 if not Is_Generic_Actual_Type (Parent_Type)
16850 or else In_Visible_Part (Scope (Parent_Type))
16851 then
16852 if Is_Class_Wide_Type (Parent_Type) then
16853 Error_Msg_N
16854 ("parent type must not be a class-wide type", Indic);
16855
16856 -- Use specific type to prevent cascaded errors.
16857
16858 Parent_Type := Etype (Parent_Type);
16859
16860 else
16861 Error_Msg_N
16862 ("type derived from tagged type must have extension", Indic);
16863 end if;
16864 end if;
16865 end if;
16866
16867 -- AI-443: Synchronized formal derived types require a private
16868 -- extension. There is no point in checking the ancestor type or
16869 -- the progenitors since the construct is wrong to begin with.
16870
16871 if Ada_Version >= Ada_2005
16872 and then Is_Generic_Type (T)
16873 and then Present (Original_Node (N))
16874 then
16875 declare
16876 Decl : constant Node_Id := Original_Node (N);
16877
16878 begin
16879 if Nkind (Decl) = N_Formal_Type_Declaration
16880 and then Nkind (Formal_Type_Definition (Decl)) =
16881 N_Formal_Derived_Type_Definition
16882 and then Synchronized_Present (Formal_Type_Definition (Decl))
16883 and then No (Extension)
16884
16885 -- Avoid emitting a duplicate error message
16886
16887 and then not Error_Posted (Indic)
16888 then
16889 Error_Msg_N
16890 ("synchronized derived type must have extension", N);
16891 end if;
16892 end;
16893 end if;
16894
16895 if Null_Exclusion_Present (Def)
16896 and then not Is_Access_Type (Parent_Type)
16897 then
16898 Error_Msg_N ("null exclusion can only apply to an access type", N);
16899 end if;
16900
16901 -- Avoid deriving parent primitives of underlying record views
16902
16903 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16904 Derive_Subps => not Is_Underlying_Record_View (T));
16905
16906 -- AI-419: The parent type of an explicitly limited derived type must
16907 -- be a limited type or a limited interface.
16908
16909 if Limited_Present (Def) then
16910 Set_Is_Limited_Record (T);
16911
16912 if Is_Interface (T) then
16913 Set_Is_Limited_Interface (T);
16914 end if;
16915
16916 if not Is_Limited_Type (Parent_Type)
16917 and then
16918 (not Is_Interface (Parent_Type)
16919 or else not Is_Limited_Interface (Parent_Type))
16920 then
16921 -- AI05-0096: a derivation in the private part of an instance is
16922 -- legal if the generic formal is untagged limited, and the actual
16923 -- is non-limited.
16924
16925 if Is_Generic_Actual_Type (Parent_Type)
16926 and then In_Private_Part (Current_Scope)
16927 and then
16928 not Is_Tagged_Type
16929 (Generic_Parent_Type (Parent (Parent_Type)))
16930 then
16931 null;
16932
16933 else
16934 Error_Msg_NE
16935 ("parent type& of limited type must be limited",
16936 N, Parent_Type);
16937 end if;
16938 end if;
16939 end if;
16940
16941 -- In SPARK, there are no derived type definitions other than type
16942 -- extensions of tagged record types.
16943
16944 if No (Extension) then
16945 Check_SPARK_05_Restriction
16946 ("derived type is not allowed", Original_Node (N));
16947 end if;
16948 end Derived_Type_Declaration;
16949
16950 ------------------------
16951 -- Diagnose_Interface --
16952 ------------------------
16953
16954 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16955 begin
16956 if not Is_Interface (E) and then E /= Any_Type then
16957 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16958 end if;
16959 end Diagnose_Interface;
16960
16961 ----------------------------------
16962 -- Enumeration_Type_Declaration --
16963 ----------------------------------
16964
16965 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16966 Ev : Uint;
16967 L : Node_Id;
16968 R_Node : Node_Id;
16969 B_Node : Node_Id;
16970
16971 begin
16972 -- Create identifier node representing lower bound
16973
16974 B_Node := New_Node (N_Identifier, Sloc (Def));
16975 L := First (Literals (Def));
16976 Set_Chars (B_Node, Chars (L));
16977 Set_Entity (B_Node, L);
16978 Set_Etype (B_Node, T);
16979 Set_Is_Static_Expression (B_Node, True);
16980
16981 R_Node := New_Node (N_Range, Sloc (Def));
16982 Set_Low_Bound (R_Node, B_Node);
16983
16984 Set_Ekind (T, E_Enumeration_Type);
16985 Set_First_Literal (T, L);
16986 Set_Etype (T, T);
16987 Set_Is_Constrained (T);
16988
16989 Ev := Uint_0;
16990
16991 -- Loop through literals of enumeration type setting pos and rep values
16992 -- except that if the Ekind is already set, then it means the literal
16993 -- was already constructed (case of a derived type declaration and we
16994 -- should not disturb the Pos and Rep values.
16995
16996 while Present (L) loop
16997 if Ekind (L) /= E_Enumeration_Literal then
16998 Set_Ekind (L, E_Enumeration_Literal);
16999 Set_Enumeration_Pos (L, Ev);
17000 Set_Enumeration_Rep (L, Ev);
17001 Set_Is_Known_Valid (L, True);
17002 end if;
17003
17004 Set_Etype (L, T);
17005 New_Overloaded_Entity (L);
17006 Generate_Definition (L);
17007 Set_Convention (L, Convention_Intrinsic);
17008
17009 -- Case of character literal
17010
17011 if Nkind (L) = N_Defining_Character_Literal then
17012 Set_Is_Character_Type (T, True);
17013
17014 -- Check violation of No_Wide_Characters
17015
17016 if Restriction_Check_Required (No_Wide_Characters) then
17017 Get_Name_String (Chars (L));
17018
17019 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17020 Check_Restriction (No_Wide_Characters, L);
17021 end if;
17022 end if;
17023 end if;
17024
17025 Ev := Ev + 1;
17026 Next (L);
17027 end loop;
17028
17029 -- Now create a node representing upper bound
17030
17031 B_Node := New_Node (N_Identifier, Sloc (Def));
17032 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17033 Set_Entity (B_Node, Last (Literals (Def)));
17034 Set_Etype (B_Node, T);
17035 Set_Is_Static_Expression (B_Node, True);
17036
17037 Set_High_Bound (R_Node, B_Node);
17038
17039 -- Initialize various fields of the type. Some of this information
17040 -- may be overwritten later through rep.clauses.
17041
17042 Set_Scalar_Range (T, R_Node);
17043 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17044 Set_Enum_Esize (T);
17045 Set_Enum_Pos_To_Rep (T, Empty);
17046
17047 -- Set Discard_Names if configuration pragma set, or if there is
17048 -- a parameterless pragma in the current declarative region
17049
17050 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17051 Set_Discard_Names (T);
17052 end if;
17053
17054 -- Process end label if there is one
17055
17056 if Present (Def) then
17057 Process_End_Label (Def, 'e', T);
17058 end if;
17059 end Enumeration_Type_Declaration;
17060
17061 ---------------------------------
17062 -- Expand_To_Stored_Constraint --
17063 ---------------------------------
17064
17065 function Expand_To_Stored_Constraint
17066 (Typ : Entity_Id;
17067 Constraint : Elist_Id) return Elist_Id
17068 is
17069 Explicitly_Discriminated_Type : Entity_Id;
17070 Expansion : Elist_Id;
17071 Discriminant : Entity_Id;
17072
17073 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17074 -- Find the nearest type that actually specifies discriminants
17075
17076 ---------------------------------
17077 -- Type_With_Explicit_Discrims --
17078 ---------------------------------
17079
17080 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17081 Typ : constant E := Base_Type (Id);
17082
17083 begin
17084 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17085 if Present (Full_View (Typ)) then
17086 return Type_With_Explicit_Discrims (Full_View (Typ));
17087 end if;
17088
17089 else
17090 if Has_Discriminants (Typ) then
17091 return Typ;
17092 end if;
17093 end if;
17094
17095 if Etype (Typ) = Typ then
17096 return Empty;
17097 elsif Has_Discriminants (Typ) then
17098 return Typ;
17099 else
17100 return Type_With_Explicit_Discrims (Etype (Typ));
17101 end if;
17102
17103 end Type_With_Explicit_Discrims;
17104
17105 -- Start of processing for Expand_To_Stored_Constraint
17106
17107 begin
17108 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17109 return No_Elist;
17110 end if;
17111
17112 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17113
17114 if No (Explicitly_Discriminated_Type) then
17115 return No_Elist;
17116 end if;
17117
17118 Expansion := New_Elmt_List;
17119
17120 Discriminant :=
17121 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17122 while Present (Discriminant) loop
17123 Append_Elmt
17124 (Get_Discriminant_Value
17125 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17126 To => Expansion);
17127 Next_Stored_Discriminant (Discriminant);
17128 end loop;
17129
17130 return Expansion;
17131 end Expand_To_Stored_Constraint;
17132
17133 ---------------------------
17134 -- Find_Hidden_Interface --
17135 ---------------------------
17136
17137 function Find_Hidden_Interface
17138 (Src : Elist_Id;
17139 Dest : Elist_Id) return Entity_Id
17140 is
17141 Iface : Entity_Id;
17142 Iface_Elmt : Elmt_Id;
17143
17144 begin
17145 if Present (Src) and then Present (Dest) then
17146 Iface_Elmt := First_Elmt (Src);
17147 while Present (Iface_Elmt) loop
17148 Iface := Node (Iface_Elmt);
17149
17150 if Is_Interface (Iface)
17151 and then not Contain_Interface (Iface, Dest)
17152 then
17153 return Iface;
17154 end if;
17155
17156 Next_Elmt (Iface_Elmt);
17157 end loop;
17158 end if;
17159
17160 return Empty;
17161 end Find_Hidden_Interface;
17162
17163 --------------------
17164 -- Find_Type_Name --
17165 --------------------
17166
17167 function Find_Type_Name (N : Node_Id) return Entity_Id is
17168 Id : constant Entity_Id := Defining_Identifier (N);
17169 New_Id : Entity_Id;
17170 Prev : Entity_Id;
17171 Prev_Par : Node_Id;
17172
17173 procedure Check_Duplicate_Aspects;
17174 -- Check that aspects specified in a completion have not been specified
17175 -- already in the partial view.
17176
17177 procedure Tag_Mismatch;
17178 -- Diagnose a tagged partial view whose full view is untagged. We post
17179 -- the message on the full view, with a reference to the previous
17180 -- partial view. The partial view can be private or incomplete, and
17181 -- these are handled in a different manner, so we determine the position
17182 -- of the error message from the respective slocs of both.
17183
17184 -----------------------------
17185 -- Check_Duplicate_Aspects --
17186 -----------------------------
17187
17188 procedure Check_Duplicate_Aspects is
17189 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17190 -- Return the corresponding aspect of the partial view which matches
17191 -- the aspect id of Asp. Return Empty is no such aspect exists.
17192
17193 -----------------------------
17194 -- Get_Partial_View_Aspect --
17195 -----------------------------
17196
17197 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17198 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17199 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17200 Prev_Asp : Node_Id;
17201
17202 begin
17203 if Present (Prev_Asps) then
17204 Prev_Asp := First (Prev_Asps);
17205 while Present (Prev_Asp) loop
17206 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17207 return Prev_Asp;
17208 end if;
17209
17210 Next (Prev_Asp);
17211 end loop;
17212 end if;
17213
17214 return Empty;
17215 end Get_Partial_View_Aspect;
17216
17217 -- Local variables
17218
17219 Full_Asps : constant List_Id := Aspect_Specifications (N);
17220 Full_Asp : Node_Id;
17221 Part_Asp : Node_Id;
17222
17223 -- Start of processing for Check_Duplicate_Aspects
17224
17225 begin
17226 if Present (Full_Asps) then
17227 Full_Asp := First (Full_Asps);
17228 while Present (Full_Asp) loop
17229 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17230
17231 -- An aspect and its class-wide counterpart are two distinct
17232 -- aspects and may apply to both views of an entity.
17233
17234 if Present (Part_Asp)
17235 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17236 then
17237 Error_Msg_N
17238 ("aspect already specified in private declaration",
17239 Full_Asp);
17240
17241 Remove (Full_Asp);
17242 return;
17243 end if;
17244
17245 if Has_Discriminants (Prev)
17246 and then not Has_Unknown_Discriminants (Prev)
17247 and then Get_Aspect_Id (Full_Asp) =
17248 Aspect_Implicit_Dereference
17249 then
17250 Error_Msg_N
17251 ("cannot specify aspect if partial view has known "
17252 & "discriminants", Full_Asp);
17253 end if;
17254
17255 Next (Full_Asp);
17256 end loop;
17257 end if;
17258 end Check_Duplicate_Aspects;
17259
17260 ------------------
17261 -- Tag_Mismatch --
17262 ------------------
17263
17264 procedure Tag_Mismatch is
17265 begin
17266 if Sloc (Prev) < Sloc (Id) then
17267 if Ada_Version >= Ada_2012
17268 and then Nkind (N) = N_Private_Type_Declaration
17269 then
17270 Error_Msg_NE
17271 ("declaration of private } must be a tagged type ", Id, Prev);
17272 else
17273 Error_Msg_NE
17274 ("full declaration of } must be a tagged type ", Id, Prev);
17275 end if;
17276
17277 else
17278 if Ada_Version >= Ada_2012
17279 and then Nkind (N) = N_Private_Type_Declaration
17280 then
17281 Error_Msg_NE
17282 ("declaration of private } must be a tagged type ", Prev, Id);
17283 else
17284 Error_Msg_NE
17285 ("full declaration of } must be a tagged type ", Prev, Id);
17286 end if;
17287 end if;
17288 end Tag_Mismatch;
17289
17290 -- Start of processing for Find_Type_Name
17291
17292 begin
17293 -- Find incomplete declaration, if one was given
17294
17295 Prev := Current_Entity_In_Scope (Id);
17296
17297 -- New type declaration
17298
17299 if No (Prev) then
17300 Enter_Name (Id);
17301 return Id;
17302
17303 -- Previous declaration exists
17304
17305 else
17306 Prev_Par := Parent (Prev);
17307
17308 -- Error if not incomplete/private case except if previous
17309 -- declaration is implicit, etc. Enter_Name will emit error if
17310 -- appropriate.
17311
17312 if not Is_Incomplete_Or_Private_Type (Prev) then
17313 Enter_Name (Id);
17314 New_Id := Id;
17315
17316 -- Check invalid completion of private or incomplete type
17317
17318 elsif not Nkind_In (N, N_Full_Type_Declaration,
17319 N_Task_Type_Declaration,
17320 N_Protected_Type_Declaration)
17321 and then
17322 (Ada_Version < Ada_2012
17323 or else not Is_Incomplete_Type (Prev)
17324 or else not Nkind_In (N, N_Private_Type_Declaration,
17325 N_Private_Extension_Declaration))
17326 then
17327 -- Completion must be a full type declarations (RM 7.3(4))
17328
17329 Error_Msg_Sloc := Sloc (Prev);
17330 Error_Msg_NE ("invalid completion of }", Id, Prev);
17331
17332 -- Set scope of Id to avoid cascaded errors. Entity is never
17333 -- examined again, except when saving globals in generics.
17334
17335 Set_Scope (Id, Current_Scope);
17336 New_Id := Id;
17337
17338 -- If this is a repeated incomplete declaration, no further
17339 -- checks are possible.
17340
17341 if Nkind (N) = N_Incomplete_Type_Declaration then
17342 return Prev;
17343 end if;
17344
17345 -- Case of full declaration of incomplete type
17346
17347 elsif Ekind (Prev) = E_Incomplete_Type
17348 and then (Ada_Version < Ada_2012
17349 or else No (Full_View (Prev))
17350 or else not Is_Private_Type (Full_View (Prev)))
17351 then
17352 -- Indicate that the incomplete declaration has a matching full
17353 -- declaration. The defining occurrence of the incomplete
17354 -- declaration remains the visible one, and the procedure
17355 -- Get_Full_View dereferences it whenever the type is used.
17356
17357 if Present (Full_View (Prev)) then
17358 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17359 end if;
17360
17361 Set_Full_View (Prev, Id);
17362 Append_Entity (Id, Current_Scope);
17363 Set_Is_Public (Id, Is_Public (Prev));
17364 Set_Is_Internal (Id);
17365 New_Id := Prev;
17366
17367 -- If the incomplete view is tagged, a class_wide type has been
17368 -- created already. Use it for the private type as well, in order
17369 -- to prevent multiple incompatible class-wide types that may be
17370 -- created for self-referential anonymous access components.
17371
17372 if Is_Tagged_Type (Prev)
17373 and then Present (Class_Wide_Type (Prev))
17374 then
17375 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17376 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17377
17378 -- Type of the class-wide type is the current Id. Previously
17379 -- this was not done for private declarations because of order-
17380 -- of-elaboration issues in the back end, but gigi now handles
17381 -- this properly.
17382
17383 Set_Etype (Class_Wide_Type (Id), Id);
17384 end if;
17385
17386 -- Case of full declaration of private type
17387
17388 else
17389 -- If the private type was a completion of an incomplete type then
17390 -- update Prev to reference the private type
17391
17392 if Ada_Version >= Ada_2012
17393 and then Ekind (Prev) = E_Incomplete_Type
17394 and then Present (Full_View (Prev))
17395 and then Is_Private_Type (Full_View (Prev))
17396 then
17397 Prev := Full_View (Prev);
17398 Prev_Par := Parent (Prev);
17399 end if;
17400
17401 if Nkind (N) = N_Full_Type_Declaration
17402 and then Nkind_In
17403 (Type_Definition (N), N_Record_Definition,
17404 N_Derived_Type_Definition)
17405 and then Interface_Present (Type_Definition (N))
17406 then
17407 Error_Msg_N
17408 ("completion of private type cannot be an interface", N);
17409 end if;
17410
17411 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17412 if Etype (Prev) /= Prev then
17413
17414 -- Prev is a private subtype or a derived type, and needs
17415 -- no completion.
17416
17417 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17418 New_Id := Id;
17419
17420 elsif Ekind (Prev) = E_Private_Type
17421 and then Nkind_In (N, N_Task_Type_Declaration,
17422 N_Protected_Type_Declaration)
17423 then
17424 Error_Msg_N
17425 ("completion of nonlimited type cannot be limited", N);
17426
17427 elsif Ekind (Prev) = E_Record_Type_With_Private
17428 and then Nkind_In (N, N_Task_Type_Declaration,
17429 N_Protected_Type_Declaration)
17430 then
17431 if not Is_Limited_Record (Prev) then
17432 Error_Msg_N
17433 ("completion of nonlimited type cannot be limited", N);
17434
17435 elsif No (Interface_List (N)) then
17436 Error_Msg_N
17437 ("completion of tagged private type must be tagged",
17438 N);
17439 end if;
17440 end if;
17441
17442 -- Ada 2005 (AI-251): Private extension declaration of a task
17443 -- type or a protected type. This case arises when covering
17444 -- interface types.
17445
17446 elsif Nkind_In (N, N_Task_Type_Declaration,
17447 N_Protected_Type_Declaration)
17448 then
17449 null;
17450
17451 elsif Nkind (N) /= N_Full_Type_Declaration
17452 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17453 then
17454 Error_Msg_N
17455 ("full view of private extension must be an extension", N);
17456
17457 elsif not (Abstract_Present (Parent (Prev)))
17458 and then Abstract_Present (Type_Definition (N))
17459 then
17460 Error_Msg_N
17461 ("full view of non-abstract extension cannot be abstract", N);
17462 end if;
17463
17464 if not In_Private_Part (Current_Scope) then
17465 Error_Msg_N
17466 ("declaration of full view must appear in private part", N);
17467 end if;
17468
17469 if Ada_Version >= Ada_2012 then
17470 Check_Duplicate_Aspects;
17471 end if;
17472
17473 Copy_And_Swap (Prev, Id);
17474 Set_Has_Private_Declaration (Prev);
17475 Set_Has_Private_Declaration (Id);
17476
17477 -- AI12-0133: Indicate whether we have a partial view with
17478 -- unknown discriminants, in which case initialization of objects
17479 -- of the type do not receive an invariant check.
17480
17481 Set_Partial_View_Has_Unknown_Discr
17482 (Prev, Has_Unknown_Discriminants (Id));
17483
17484 -- Preserve aspect and iterator flags that may have been set on
17485 -- the partial view.
17486
17487 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17488 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17489
17490 -- If no error, propagate freeze_node from private to full view.
17491 -- It may have been generated for an early operational item.
17492
17493 if Present (Freeze_Node (Id))
17494 and then Serious_Errors_Detected = 0
17495 and then No (Full_View (Id))
17496 then
17497 Set_Freeze_Node (Prev, Freeze_Node (Id));
17498 Set_Freeze_Node (Id, Empty);
17499 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17500 end if;
17501
17502 Set_Full_View (Id, Prev);
17503 New_Id := Prev;
17504 end if;
17505
17506 -- Verify that full declaration conforms to partial one
17507
17508 if Is_Incomplete_Or_Private_Type (Prev)
17509 and then Present (Discriminant_Specifications (Prev_Par))
17510 then
17511 if Present (Discriminant_Specifications (N)) then
17512 if Ekind (Prev) = E_Incomplete_Type then
17513 Check_Discriminant_Conformance (N, Prev, Prev);
17514 else
17515 Check_Discriminant_Conformance (N, Prev, Id);
17516 end if;
17517
17518 else
17519 Error_Msg_N
17520 ("missing discriminants in full type declaration", N);
17521
17522 -- To avoid cascaded errors on subsequent use, share the
17523 -- discriminants of the partial view.
17524
17525 Set_Discriminant_Specifications (N,
17526 Discriminant_Specifications (Prev_Par));
17527 end if;
17528 end if;
17529
17530 -- A prior untagged partial view can have an associated class-wide
17531 -- type due to use of the class attribute, and in this case the full
17532 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17533 -- of incomplete tagged declarations, but we check for it.
17534
17535 if Is_Type (Prev)
17536 and then (Is_Tagged_Type (Prev)
17537 or else Present (Class_Wide_Type (Prev)))
17538 then
17539 -- Ada 2012 (AI05-0162): A private type may be the completion of
17540 -- an incomplete type.
17541
17542 if Ada_Version >= Ada_2012
17543 and then Is_Incomplete_Type (Prev)
17544 and then Nkind_In (N, N_Private_Type_Declaration,
17545 N_Private_Extension_Declaration)
17546 then
17547 -- No need to check private extensions since they are tagged
17548
17549 if Nkind (N) = N_Private_Type_Declaration
17550 and then not Tagged_Present (N)
17551 then
17552 Tag_Mismatch;
17553 end if;
17554
17555 -- The full declaration is either a tagged type (including
17556 -- a synchronized type that implements interfaces) or a
17557 -- type extension, otherwise this is an error.
17558
17559 elsif Nkind_In (N, N_Task_Type_Declaration,
17560 N_Protected_Type_Declaration)
17561 then
17562 if No (Interface_List (N)) and then not Error_Posted (N) then
17563 Tag_Mismatch;
17564 end if;
17565
17566 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17567
17568 -- Indicate that the previous declaration (tagged incomplete
17569 -- or private declaration) requires the same on the full one.
17570
17571 if not Tagged_Present (Type_Definition (N)) then
17572 Tag_Mismatch;
17573 Set_Is_Tagged_Type (Id);
17574 end if;
17575
17576 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17577 if No (Record_Extension_Part (Type_Definition (N))) then
17578 Error_Msg_NE
17579 ("full declaration of } must be a record extension",
17580 Prev, Id);
17581
17582 -- Set some attributes to produce a usable full view
17583
17584 Set_Is_Tagged_Type (Id);
17585 end if;
17586
17587 else
17588 Tag_Mismatch;
17589 end if;
17590 end if;
17591
17592 if Present (Prev)
17593 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17594 and then Present (Premature_Use (Parent (Prev)))
17595 then
17596 Error_Msg_Sloc := Sloc (N);
17597 Error_Msg_N
17598 ("\full declaration #", Premature_Use (Parent (Prev)));
17599 end if;
17600
17601 return New_Id;
17602 end if;
17603 end Find_Type_Name;
17604
17605 -------------------------
17606 -- Find_Type_Of_Object --
17607 -------------------------
17608
17609 function Find_Type_Of_Object
17610 (Obj_Def : Node_Id;
17611 Related_Nod : Node_Id) return Entity_Id
17612 is
17613 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17614 P : Node_Id := Parent (Obj_Def);
17615 T : Entity_Id;
17616 Nam : Name_Id;
17617
17618 begin
17619 -- If the parent is a component_definition node we climb to the
17620 -- component_declaration node
17621
17622 if Nkind (P) = N_Component_Definition then
17623 P := Parent (P);
17624 end if;
17625
17626 -- Case of an anonymous array subtype
17627
17628 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17629 N_Unconstrained_Array_Definition)
17630 then
17631 T := Empty;
17632 Array_Type_Declaration (T, Obj_Def);
17633
17634 -- Create an explicit subtype whenever possible
17635
17636 elsif Nkind (P) /= N_Component_Declaration
17637 and then Def_Kind = N_Subtype_Indication
17638 then
17639 -- Base name of subtype on object name, which will be unique in
17640 -- the current scope.
17641
17642 -- If this is a duplicate declaration, return base type, to avoid
17643 -- generating duplicate anonymous types.
17644
17645 if Error_Posted (P) then
17646 Analyze (Subtype_Mark (Obj_Def));
17647 return Entity (Subtype_Mark (Obj_Def));
17648 end if;
17649
17650 Nam :=
17651 New_External_Name
17652 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17653
17654 T := Make_Defining_Identifier (Sloc (P), Nam);
17655
17656 Insert_Action (Obj_Def,
17657 Make_Subtype_Declaration (Sloc (P),
17658 Defining_Identifier => T,
17659 Subtype_Indication => Relocate_Node (Obj_Def)));
17660
17661 -- This subtype may need freezing, and this will not be done
17662 -- automatically if the object declaration is not in declarative
17663 -- part. Since this is an object declaration, the type cannot always
17664 -- be frozen here. Deferred constants do not freeze their type
17665 -- (which often enough will be private).
17666
17667 if Nkind (P) = N_Object_Declaration
17668 and then Constant_Present (P)
17669 and then No (Expression (P))
17670 then
17671 null;
17672
17673 -- Here we freeze the base type of object type to catch premature use
17674 -- of discriminated private type without a full view.
17675
17676 else
17677 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17678 end if;
17679
17680 -- Ada 2005 AI-406: the object definition in an object declaration
17681 -- can be an access definition.
17682
17683 elsif Def_Kind = N_Access_Definition then
17684 T := Access_Definition (Related_Nod, Obj_Def);
17685
17686 Set_Is_Local_Anonymous_Access
17687 (T,
17688 V => (Ada_Version < Ada_2012)
17689 or else (Nkind (P) /= N_Object_Declaration)
17690 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17691
17692 -- Otherwise, the object definition is just a subtype_mark
17693
17694 else
17695 T := Process_Subtype (Obj_Def, Related_Nod);
17696
17697 -- If expansion is disabled an object definition that is an aggregate
17698 -- will not get expanded and may lead to scoping problems in the back
17699 -- end, if the object is referenced in an inner scope. In that case
17700 -- create an itype reference for the object definition now. This
17701 -- may be redundant in some cases, but harmless.
17702
17703 if Is_Itype (T)
17704 and then Nkind (Related_Nod) = N_Object_Declaration
17705 and then ASIS_Mode
17706 then
17707 Build_Itype_Reference (T, Related_Nod);
17708 end if;
17709 end if;
17710
17711 return T;
17712 end Find_Type_Of_Object;
17713
17714 --------------------------------
17715 -- Find_Type_Of_Subtype_Indic --
17716 --------------------------------
17717
17718 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17719 Typ : Entity_Id;
17720
17721 begin
17722 -- Case of subtype mark with a constraint
17723
17724 if Nkind (S) = N_Subtype_Indication then
17725 Find_Type (Subtype_Mark (S));
17726 Typ := Entity (Subtype_Mark (S));
17727
17728 if not
17729 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17730 then
17731 Error_Msg_N
17732 ("incorrect constraint for this kind of type", Constraint (S));
17733 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17734 end if;
17735
17736 -- Otherwise we have a subtype mark without a constraint
17737
17738 elsif Error_Posted (S) then
17739 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17740 return Any_Type;
17741
17742 else
17743 Find_Type (S);
17744 Typ := Entity (S);
17745 end if;
17746
17747 -- Check No_Wide_Characters restriction
17748
17749 Check_Wide_Character_Restriction (Typ, S);
17750
17751 return Typ;
17752 end Find_Type_Of_Subtype_Indic;
17753
17754 -------------------------------------
17755 -- Floating_Point_Type_Declaration --
17756 -------------------------------------
17757
17758 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17759 Digs : constant Node_Id := Digits_Expression (Def);
17760 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17761 Digs_Val : Uint;
17762 Base_Typ : Entity_Id;
17763 Implicit_Base : Entity_Id;
17764 Bound : Node_Id;
17765
17766 function Can_Derive_From (E : Entity_Id) return Boolean;
17767 -- Find if given digits value, and possibly a specified range, allows
17768 -- derivation from specified type
17769
17770 function Find_Base_Type return Entity_Id;
17771 -- Find a predefined base type that Def can derive from, or generate
17772 -- an error and substitute Long_Long_Float if none exists.
17773
17774 ---------------------
17775 -- Can_Derive_From --
17776 ---------------------
17777
17778 function Can_Derive_From (E : Entity_Id) return Boolean is
17779 Spec : constant Entity_Id := Real_Range_Specification (Def);
17780
17781 begin
17782 -- Check specified "digits" constraint
17783
17784 if Digs_Val > Digits_Value (E) then
17785 return False;
17786 end if;
17787
17788 -- Check for matching range, if specified
17789
17790 if Present (Spec) then
17791 if Expr_Value_R (Type_Low_Bound (E)) >
17792 Expr_Value_R (Low_Bound (Spec))
17793 then
17794 return False;
17795 end if;
17796
17797 if Expr_Value_R (Type_High_Bound (E)) <
17798 Expr_Value_R (High_Bound (Spec))
17799 then
17800 return False;
17801 end if;
17802 end if;
17803
17804 return True;
17805 end Can_Derive_From;
17806
17807 --------------------
17808 -- Find_Base_Type --
17809 --------------------
17810
17811 function Find_Base_Type return Entity_Id is
17812 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17813
17814 begin
17815 -- Iterate over the predefined types in order, returning the first
17816 -- one that Def can derive from.
17817
17818 while Present (Choice) loop
17819 if Can_Derive_From (Node (Choice)) then
17820 return Node (Choice);
17821 end if;
17822
17823 Next_Elmt (Choice);
17824 end loop;
17825
17826 -- If we can't derive from any existing type, use Long_Long_Float
17827 -- and give appropriate message explaining the problem.
17828
17829 if Digs_Val > Max_Digs_Val then
17830 -- It might be the case that there is a type with the requested
17831 -- range, just not the combination of digits and range.
17832
17833 Error_Msg_N
17834 ("no predefined type has requested range and precision",
17835 Real_Range_Specification (Def));
17836
17837 else
17838 Error_Msg_N
17839 ("range too large for any predefined type",
17840 Real_Range_Specification (Def));
17841 end if;
17842
17843 return Standard_Long_Long_Float;
17844 end Find_Base_Type;
17845
17846 -- Start of processing for Floating_Point_Type_Declaration
17847
17848 begin
17849 Check_Restriction (No_Floating_Point, Def);
17850
17851 -- Create an implicit base type
17852
17853 Implicit_Base :=
17854 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17855
17856 -- Analyze and verify digits value
17857
17858 Analyze_And_Resolve (Digs, Any_Integer);
17859 Check_Digits_Expression (Digs);
17860 Digs_Val := Expr_Value (Digs);
17861
17862 -- Process possible range spec and find correct type to derive from
17863
17864 Process_Real_Range_Specification (Def);
17865
17866 -- Check that requested number of digits is not too high.
17867
17868 if Digs_Val > Max_Digs_Val then
17869
17870 -- The check for Max_Base_Digits may be somewhat expensive, as it
17871 -- requires reading System, so only do it when necessary.
17872
17873 declare
17874 Max_Base_Digits : constant Uint :=
17875 Expr_Value
17876 (Expression
17877 (Parent (RTE (RE_Max_Base_Digits))));
17878
17879 begin
17880 if Digs_Val > Max_Base_Digits then
17881 Error_Msg_Uint_1 := Max_Base_Digits;
17882 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17883
17884 elsif No (Real_Range_Specification (Def)) then
17885 Error_Msg_Uint_1 := Max_Digs_Val;
17886 Error_Msg_N ("types with more than ^ digits need range spec "
17887 & "(RM 3.5.7(6))", Digs);
17888 end if;
17889 end;
17890 end if;
17891
17892 -- Find a suitable type to derive from or complain and use a substitute
17893
17894 Base_Typ := Find_Base_Type;
17895
17896 -- If there are bounds given in the declaration use them as the bounds
17897 -- of the type, otherwise use the bounds of the predefined base type
17898 -- that was chosen based on the Digits value.
17899
17900 if Present (Real_Range_Specification (Def)) then
17901 Set_Scalar_Range (T, Real_Range_Specification (Def));
17902 Set_Is_Constrained (T);
17903
17904 -- The bounds of this range must be converted to machine numbers
17905 -- in accordance with RM 4.9(38).
17906
17907 Bound := Type_Low_Bound (T);
17908
17909 if Nkind (Bound) = N_Real_Literal then
17910 Set_Realval
17911 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17912 Set_Is_Machine_Number (Bound);
17913 end if;
17914
17915 Bound := Type_High_Bound (T);
17916
17917 if Nkind (Bound) = N_Real_Literal then
17918 Set_Realval
17919 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17920 Set_Is_Machine_Number (Bound);
17921 end if;
17922
17923 else
17924 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17925 end if;
17926
17927 -- Complete definition of implicit base and declared first subtype. The
17928 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17929 -- are not clobbered when the floating point type acts as a full view of
17930 -- a private type.
17931
17932 Set_Etype (Implicit_Base, Base_Typ);
17933 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17934 Set_Size_Info (Implicit_Base, Base_Typ);
17935 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17936 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17937 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17938 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17939
17940 Set_Ekind (T, E_Floating_Point_Subtype);
17941 Set_Etype (T, Implicit_Base);
17942 Set_Size_Info (T, Implicit_Base);
17943 Set_RM_Size (T, RM_Size (Implicit_Base));
17944 Inherit_Rep_Item_Chain (T, Implicit_Base);
17945 Set_Digits_Value (T, Digs_Val);
17946 end Floating_Point_Type_Declaration;
17947
17948 ----------------------------
17949 -- Get_Discriminant_Value --
17950 ----------------------------
17951
17952 -- This is the situation:
17953
17954 -- There is a non-derived type
17955
17956 -- type T0 (Dx, Dy, Dz...)
17957
17958 -- There are zero or more levels of derivation, with each derivation
17959 -- either purely inheriting the discriminants, or defining its own.
17960
17961 -- type Ti is new Ti-1
17962 -- or
17963 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17964 -- or
17965 -- subtype Ti is ...
17966
17967 -- The subtype issue is avoided by the use of Original_Record_Component,
17968 -- and the fact that derived subtypes also derive the constraints.
17969
17970 -- This chain leads back from
17971
17972 -- Typ_For_Constraint
17973
17974 -- Typ_For_Constraint has discriminants, and the value for each
17975 -- discriminant is given by its corresponding Elmt of Constraints.
17976
17977 -- Discriminant is some discriminant in this hierarchy
17978
17979 -- We need to return its value
17980
17981 -- We do this by recursively searching each level, and looking for
17982 -- Discriminant. Once we get to the bottom, we start backing up
17983 -- returning the value for it which may in turn be a discriminant
17984 -- further up, so on the backup we continue the substitution.
17985
17986 function Get_Discriminant_Value
17987 (Discriminant : Entity_Id;
17988 Typ_For_Constraint : Entity_Id;
17989 Constraint : Elist_Id) return Node_Id
17990 is
17991 function Root_Corresponding_Discriminant
17992 (Discr : Entity_Id) return Entity_Id;
17993 -- Given a discriminant, traverse the chain of inherited discriminants
17994 -- and return the topmost discriminant.
17995
17996 function Search_Derivation_Levels
17997 (Ti : Entity_Id;
17998 Discrim_Values : Elist_Id;
17999 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18000 -- This is the routine that performs the recursive search of levels
18001 -- as described above.
18002
18003 -------------------------------------
18004 -- Root_Corresponding_Discriminant --
18005 -------------------------------------
18006
18007 function Root_Corresponding_Discriminant
18008 (Discr : Entity_Id) return Entity_Id
18009 is
18010 D : Entity_Id;
18011
18012 begin
18013 D := Discr;
18014 while Present (Corresponding_Discriminant (D)) loop
18015 D := Corresponding_Discriminant (D);
18016 end loop;
18017
18018 return D;
18019 end Root_Corresponding_Discriminant;
18020
18021 ------------------------------
18022 -- Search_Derivation_Levels --
18023 ------------------------------
18024
18025 function Search_Derivation_Levels
18026 (Ti : Entity_Id;
18027 Discrim_Values : Elist_Id;
18028 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18029 is
18030 Assoc : Elmt_Id;
18031 Disc : Entity_Id;
18032 Result : Node_Or_Entity_Id;
18033 Result_Entity : Node_Id;
18034
18035 begin
18036 -- If inappropriate type, return Error, this happens only in
18037 -- cascaded error situations, and we want to avoid a blow up.
18038
18039 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18040 return Error;
18041 end if;
18042
18043 -- Look deeper if possible. Use Stored_Constraints only for
18044 -- untagged types. For tagged types use the given constraint.
18045 -- This asymmetry needs explanation???
18046
18047 if not Stored_Discrim_Values
18048 and then Present (Stored_Constraint (Ti))
18049 and then not Is_Tagged_Type (Ti)
18050 then
18051 Result :=
18052 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18053
18054 else
18055 declare
18056 Td : Entity_Id := Etype (Ti);
18057
18058 begin
18059 -- If the parent type is private, the full view may include
18060 -- renamed discriminants, and it is those stored values that
18061 -- may be needed (the partial view never has more information
18062 -- than the full view).
18063
18064 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18065 Td := Full_View (Td);
18066 end if;
18067
18068 if Td = Ti then
18069 Result := Discriminant;
18070
18071 else
18072 if Present (Stored_Constraint (Ti)) then
18073 Result :=
18074 Search_Derivation_Levels
18075 (Td, Stored_Constraint (Ti), True);
18076 else
18077 Result :=
18078 Search_Derivation_Levels
18079 (Td, Discrim_Values, Stored_Discrim_Values);
18080 end if;
18081 end if;
18082 end;
18083 end if;
18084
18085 -- Extra underlying places to search, if not found above. For
18086 -- concurrent types, the relevant discriminant appears in the
18087 -- corresponding record. For a type derived from a private type
18088 -- without discriminant, the full view inherits the discriminants
18089 -- of the full view of the parent.
18090
18091 if Result = Discriminant then
18092 if Is_Concurrent_Type (Ti)
18093 and then Present (Corresponding_Record_Type (Ti))
18094 then
18095 Result :=
18096 Search_Derivation_Levels (
18097 Corresponding_Record_Type (Ti),
18098 Discrim_Values,
18099 Stored_Discrim_Values);
18100
18101 elsif Is_Private_Type (Ti)
18102 and then not Has_Discriminants (Ti)
18103 and then Present (Full_View (Ti))
18104 and then Etype (Full_View (Ti)) /= Ti
18105 then
18106 Result :=
18107 Search_Derivation_Levels (
18108 Full_View (Ti),
18109 Discrim_Values,
18110 Stored_Discrim_Values);
18111 end if;
18112 end if;
18113
18114 -- If Result is not a (reference to a) discriminant, return it,
18115 -- otherwise set Result_Entity to the discriminant.
18116
18117 if Nkind (Result) = N_Defining_Identifier then
18118 pragma Assert (Result = Discriminant);
18119 Result_Entity := Result;
18120
18121 else
18122 if not Denotes_Discriminant (Result) then
18123 return Result;
18124 end if;
18125
18126 Result_Entity := Entity (Result);
18127 end if;
18128
18129 -- See if this level of derivation actually has discriminants because
18130 -- tagged derivations can add them, hence the lower levels need not
18131 -- have any.
18132
18133 if not Has_Discriminants (Ti) then
18134 return Result;
18135 end if;
18136
18137 -- Scan Ti's discriminants for Result_Entity, and return its
18138 -- corresponding value, if any.
18139
18140 Result_Entity := Original_Record_Component (Result_Entity);
18141
18142 Assoc := First_Elmt (Discrim_Values);
18143
18144 if Stored_Discrim_Values then
18145 Disc := First_Stored_Discriminant (Ti);
18146 else
18147 Disc := First_Discriminant (Ti);
18148 end if;
18149
18150 while Present (Disc) loop
18151
18152 -- If no further associations return the discriminant, value will
18153 -- be found on the second pass.
18154
18155 if No (Assoc) then
18156 return Result;
18157 end if;
18158
18159 if Original_Record_Component (Disc) = Result_Entity then
18160 return Node (Assoc);
18161 end if;
18162
18163 Next_Elmt (Assoc);
18164
18165 if Stored_Discrim_Values then
18166 Next_Stored_Discriminant (Disc);
18167 else
18168 Next_Discriminant (Disc);
18169 end if;
18170 end loop;
18171
18172 -- Could not find it
18173
18174 return Result;
18175 end Search_Derivation_Levels;
18176
18177 -- Local Variables
18178
18179 Result : Node_Or_Entity_Id;
18180
18181 -- Start of processing for Get_Discriminant_Value
18182
18183 begin
18184 -- ??? This routine is a gigantic mess and will be deleted. For the
18185 -- time being just test for the trivial case before calling recurse.
18186
18187 -- We are now celebrating the 20th anniversary of this comment!
18188
18189 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18190 declare
18191 D : Entity_Id;
18192 E : Elmt_Id;
18193
18194 begin
18195 D := First_Discriminant (Typ_For_Constraint);
18196 E := First_Elmt (Constraint);
18197 while Present (D) loop
18198 if Chars (D) = Chars (Discriminant) then
18199 return Node (E);
18200 end if;
18201
18202 Next_Discriminant (D);
18203 Next_Elmt (E);
18204 end loop;
18205 end;
18206 end if;
18207
18208 Result := Search_Derivation_Levels
18209 (Typ_For_Constraint, Constraint, False);
18210
18211 -- ??? hack to disappear when this routine is gone
18212
18213 if Nkind (Result) = N_Defining_Identifier then
18214 declare
18215 D : Entity_Id;
18216 E : Elmt_Id;
18217
18218 begin
18219 D := First_Discriminant (Typ_For_Constraint);
18220 E := First_Elmt (Constraint);
18221 while Present (D) loop
18222 if Root_Corresponding_Discriminant (D) = Discriminant then
18223 return Node (E);
18224 end if;
18225
18226 Next_Discriminant (D);
18227 Next_Elmt (E);
18228 end loop;
18229 end;
18230 end if;
18231
18232 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18233 return Result;
18234 end Get_Discriminant_Value;
18235
18236 --------------------------
18237 -- Has_Range_Constraint --
18238 --------------------------
18239
18240 function Has_Range_Constraint (N : Node_Id) return Boolean is
18241 C : constant Node_Id := Constraint (N);
18242
18243 begin
18244 if Nkind (C) = N_Range_Constraint then
18245 return True;
18246
18247 elsif Nkind (C) = N_Digits_Constraint then
18248 return
18249 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18250 or else Present (Range_Constraint (C));
18251
18252 elsif Nkind (C) = N_Delta_Constraint then
18253 return Present (Range_Constraint (C));
18254
18255 else
18256 return False;
18257 end if;
18258 end Has_Range_Constraint;
18259
18260 ------------------------
18261 -- Inherit_Components --
18262 ------------------------
18263
18264 function Inherit_Components
18265 (N : Node_Id;
18266 Parent_Base : Entity_Id;
18267 Derived_Base : Entity_Id;
18268 Is_Tagged : Boolean;
18269 Inherit_Discr : Boolean;
18270 Discs : Elist_Id) return Elist_Id
18271 is
18272 Assoc_List : constant Elist_Id := New_Elmt_List;
18273
18274 procedure Inherit_Component
18275 (Old_C : Entity_Id;
18276 Plain_Discrim : Boolean := False;
18277 Stored_Discrim : Boolean := False);
18278 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18279 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18280 -- True, Old_C is a stored discriminant. If they are both false then
18281 -- Old_C is a regular component.
18282
18283 -----------------------
18284 -- Inherit_Component --
18285 -----------------------
18286
18287 procedure Inherit_Component
18288 (Old_C : Entity_Id;
18289 Plain_Discrim : Boolean := False;
18290 Stored_Discrim : Boolean := False)
18291 is
18292 procedure Set_Anonymous_Type (Id : Entity_Id);
18293 -- Id denotes the entity of an access discriminant or anonymous
18294 -- access component. Set the type of Id to either the same type of
18295 -- Old_C or create a new one depending on whether the parent and
18296 -- the child types are in the same scope.
18297
18298 ------------------------
18299 -- Set_Anonymous_Type --
18300 ------------------------
18301
18302 procedure Set_Anonymous_Type (Id : Entity_Id) is
18303 Old_Typ : constant Entity_Id := Etype (Old_C);
18304
18305 begin
18306 if Scope (Parent_Base) = Scope (Derived_Base) then
18307 Set_Etype (Id, Old_Typ);
18308
18309 -- The parent and the derived type are in two different scopes.
18310 -- Reuse the type of the original discriminant / component by
18311 -- copying it in order to preserve all attributes.
18312
18313 else
18314 declare
18315 Typ : constant Entity_Id := New_Copy (Old_Typ);
18316
18317 begin
18318 Set_Etype (Id, Typ);
18319
18320 -- Since we do not generate component declarations for
18321 -- inherited components, associate the itype with the
18322 -- derived type.
18323
18324 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18325 Set_Scope (Typ, Derived_Base);
18326 end;
18327 end if;
18328 end Set_Anonymous_Type;
18329
18330 -- Local variables and constants
18331
18332 New_C : constant Entity_Id := New_Copy (Old_C);
18333
18334 Corr_Discrim : Entity_Id;
18335 Discrim : Entity_Id;
18336
18337 -- Start of processing for Inherit_Component
18338
18339 begin
18340 pragma Assert (not Is_Tagged or not Stored_Discrim);
18341
18342 Set_Parent (New_C, Parent (Old_C));
18343
18344 -- Regular discriminants and components must be inserted in the scope
18345 -- of the Derived_Base. Do it here.
18346
18347 if not Stored_Discrim then
18348 Enter_Name (New_C);
18349 end if;
18350
18351 -- For tagged types the Original_Record_Component must point to
18352 -- whatever this field was pointing to in the parent type. This has
18353 -- already been achieved by the call to New_Copy above.
18354
18355 if not Is_Tagged then
18356 Set_Original_Record_Component (New_C, New_C);
18357 Set_Corresponding_Record_Component (New_C, Old_C);
18358 end if;
18359
18360 -- Set the proper type of an access discriminant
18361
18362 if Ekind (New_C) = E_Discriminant
18363 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18364 then
18365 Set_Anonymous_Type (New_C);
18366 end if;
18367
18368 -- If we have inherited a component then see if its Etype contains
18369 -- references to Parent_Base discriminants. In this case, replace
18370 -- these references with the constraints given in Discs. We do not
18371 -- do this for the partial view of private types because this is
18372 -- not needed (only the components of the full view will be used
18373 -- for code generation) and cause problem. We also avoid this
18374 -- transformation in some error situations.
18375
18376 if Ekind (New_C) = E_Component then
18377
18378 -- Set the proper type of an anonymous access component
18379
18380 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18381 Set_Anonymous_Type (New_C);
18382
18383 elsif (Is_Private_Type (Derived_Base)
18384 and then not Is_Generic_Type (Derived_Base))
18385 or else (Is_Empty_Elmt_List (Discs)
18386 and then not Expander_Active)
18387 then
18388 Set_Etype (New_C, Etype (Old_C));
18389
18390 else
18391 -- The current component introduces a circularity of the
18392 -- following kind:
18393
18394 -- limited with Pack_2;
18395 -- package Pack_1 is
18396 -- type T_1 is tagged record
18397 -- Comp : access Pack_2.T_2;
18398 -- ...
18399 -- end record;
18400 -- end Pack_1;
18401
18402 -- with Pack_1;
18403 -- package Pack_2 is
18404 -- type T_2 is new Pack_1.T_1 with ...;
18405 -- end Pack_2;
18406
18407 Set_Etype
18408 (New_C,
18409 Constrain_Component_Type
18410 (Old_C, Derived_Base, N, Parent_Base, Discs));
18411 end if;
18412 end if;
18413
18414 -- In derived tagged types it is illegal to reference a non
18415 -- discriminant component in the parent type. To catch this, mark
18416 -- these components with an Ekind of E_Void. This will be reset in
18417 -- Record_Type_Definition after processing the record extension of
18418 -- the derived type.
18419
18420 -- If the declaration is a private extension, there is no further
18421 -- record extension to process, and the components retain their
18422 -- current kind, because they are visible at this point.
18423
18424 if Is_Tagged and then Ekind (New_C) = E_Component
18425 and then Nkind (N) /= N_Private_Extension_Declaration
18426 then
18427 Set_Ekind (New_C, E_Void);
18428 end if;
18429
18430 if Plain_Discrim then
18431 Set_Corresponding_Discriminant (New_C, Old_C);
18432 Build_Discriminal (New_C);
18433
18434 -- If we are explicitly inheriting a stored discriminant it will be
18435 -- completely hidden.
18436
18437 elsif Stored_Discrim then
18438 Set_Corresponding_Discriminant (New_C, Empty);
18439 Set_Discriminal (New_C, Empty);
18440 Set_Is_Completely_Hidden (New_C);
18441
18442 -- Set the Original_Record_Component of each discriminant in the
18443 -- derived base to point to the corresponding stored that we just
18444 -- created.
18445
18446 Discrim := First_Discriminant (Derived_Base);
18447 while Present (Discrim) loop
18448 Corr_Discrim := Corresponding_Discriminant (Discrim);
18449
18450 -- Corr_Discrim could be missing in an error situation
18451
18452 if Present (Corr_Discrim)
18453 and then Original_Record_Component (Corr_Discrim) = Old_C
18454 then
18455 Set_Original_Record_Component (Discrim, New_C);
18456 Set_Corresponding_Record_Component (Discrim, Empty);
18457 end if;
18458
18459 Next_Discriminant (Discrim);
18460 end loop;
18461
18462 Append_Entity (New_C, Derived_Base);
18463 end if;
18464
18465 if not Is_Tagged then
18466 Append_Elmt (Old_C, Assoc_List);
18467 Append_Elmt (New_C, Assoc_List);
18468 end if;
18469 end Inherit_Component;
18470
18471 -- Variables local to Inherit_Component
18472
18473 Loc : constant Source_Ptr := Sloc (N);
18474
18475 Parent_Discrim : Entity_Id;
18476 Stored_Discrim : Entity_Id;
18477 D : Entity_Id;
18478 Component : Entity_Id;
18479
18480 -- Start of processing for Inherit_Components
18481
18482 begin
18483 if not Is_Tagged then
18484 Append_Elmt (Parent_Base, Assoc_List);
18485 Append_Elmt (Derived_Base, Assoc_List);
18486 end if;
18487
18488 -- Inherit parent discriminants if needed
18489
18490 if Inherit_Discr then
18491 Parent_Discrim := First_Discriminant (Parent_Base);
18492 while Present (Parent_Discrim) loop
18493 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18494 Next_Discriminant (Parent_Discrim);
18495 end loop;
18496 end if;
18497
18498 -- Create explicit stored discrims for untagged types when necessary
18499
18500 if not Has_Unknown_Discriminants (Derived_Base)
18501 and then Has_Discriminants (Parent_Base)
18502 and then not Is_Tagged
18503 and then
18504 (not Inherit_Discr
18505 or else First_Discriminant (Parent_Base) /=
18506 First_Stored_Discriminant (Parent_Base))
18507 then
18508 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18509 while Present (Stored_Discrim) loop
18510 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18511 Next_Stored_Discriminant (Stored_Discrim);
18512 end loop;
18513 end if;
18514
18515 -- See if we can apply the second transformation for derived types, as
18516 -- explained in point 6. in the comments above Build_Derived_Record_Type
18517 -- This is achieved by appending Derived_Base discriminants into Discs,
18518 -- which has the side effect of returning a non empty Discs list to the
18519 -- caller of Inherit_Components, which is what we want. This must be
18520 -- done for private derived types if there are explicit stored
18521 -- discriminants, to ensure that we can retrieve the values of the
18522 -- constraints provided in the ancestors.
18523
18524 if Inherit_Discr
18525 and then Is_Empty_Elmt_List (Discs)
18526 and then Present (First_Discriminant (Derived_Base))
18527 and then
18528 (not Is_Private_Type (Derived_Base)
18529 or else Is_Completely_Hidden
18530 (First_Stored_Discriminant (Derived_Base))
18531 or else Is_Generic_Type (Derived_Base))
18532 then
18533 D := First_Discriminant (Derived_Base);
18534 while Present (D) loop
18535 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18536 Next_Discriminant (D);
18537 end loop;
18538 end if;
18539
18540 -- Finally, inherit non-discriminant components unless they are not
18541 -- visible because defined or inherited from the full view of the
18542 -- parent. Don't inherit the _parent field of the parent type.
18543
18544 Component := First_Entity (Parent_Base);
18545 while Present (Component) loop
18546
18547 -- Ada 2005 (AI-251): Do not inherit components associated with
18548 -- secondary tags of the parent.
18549
18550 if Ekind (Component) = E_Component
18551 and then Present (Related_Type (Component))
18552 then
18553 null;
18554
18555 elsif Ekind (Component) /= E_Component
18556 or else Chars (Component) = Name_uParent
18557 then
18558 null;
18559
18560 -- If the derived type is within the parent type's declarative
18561 -- region, then the components can still be inherited even though
18562 -- they aren't visible at this point. This can occur for cases
18563 -- such as within public child units where the components must
18564 -- become visible upon entering the child unit's private part.
18565
18566 elsif not Is_Visible_Component (Component)
18567 and then not In_Open_Scopes (Scope (Parent_Base))
18568 then
18569 null;
18570
18571 elsif Ekind_In (Derived_Base, E_Private_Type,
18572 E_Limited_Private_Type)
18573 then
18574 null;
18575
18576 else
18577 Inherit_Component (Component);
18578 end if;
18579
18580 Next_Entity (Component);
18581 end loop;
18582
18583 -- For tagged derived types, inherited discriminants cannot be used in
18584 -- component declarations of the record extension part. To achieve this
18585 -- we mark the inherited discriminants as not visible.
18586
18587 if Is_Tagged and then Inherit_Discr then
18588 D := First_Discriminant (Derived_Base);
18589 while Present (D) loop
18590 Set_Is_Immediately_Visible (D, False);
18591 Next_Discriminant (D);
18592 end loop;
18593 end if;
18594
18595 return Assoc_List;
18596 end Inherit_Components;
18597
18598 -----------------------------
18599 -- Inherit_Predicate_Flags --
18600 -----------------------------
18601
18602 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18603 begin
18604 if Present (Predicate_Function (Subt)) then
18605 return;
18606 end if;
18607
18608 Set_Has_Predicates (Subt, Has_Predicates (Par));
18609 Set_Has_Static_Predicate_Aspect
18610 (Subt, Has_Static_Predicate_Aspect (Par));
18611 Set_Has_Dynamic_Predicate_Aspect
18612 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18613
18614 -- A named subtype does not inherit the predicate function of its
18615 -- parent but an itype declared for a loop index needs the discrete
18616 -- predicate information of its parent to execute the loop properly.
18617
18618 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18619 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18620
18621 if Has_Static_Predicate (Par) then
18622 Set_Static_Discrete_Predicate
18623 (Subt, Static_Discrete_Predicate (Par));
18624 end if;
18625 end if;
18626 end Inherit_Predicate_Flags;
18627
18628 ----------------------
18629 -- Is_EVF_Procedure --
18630 ----------------------
18631
18632 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18633 Formal : Entity_Id;
18634
18635 begin
18636 -- Examine the formals of an Extensions_Visible False procedure looking
18637 -- for a controlling OUT parameter.
18638
18639 if Ekind (Subp) = E_Procedure
18640 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18641 then
18642 Formal := First_Formal (Subp);
18643 while Present (Formal) loop
18644 if Ekind (Formal) = E_Out_Parameter
18645 and then Is_Controlling_Formal (Formal)
18646 then
18647 return True;
18648 end if;
18649
18650 Next_Formal (Formal);
18651 end loop;
18652 end if;
18653
18654 return False;
18655 end Is_EVF_Procedure;
18656
18657 -----------------------
18658 -- Is_Null_Extension --
18659 -----------------------
18660
18661 function Is_Null_Extension (T : Entity_Id) return Boolean is
18662 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18663 Comp_List : Node_Id;
18664 Comp : Node_Id;
18665
18666 begin
18667 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18668 or else not Is_Tagged_Type (T)
18669 or else Nkind (Type_Definition (Type_Decl)) /=
18670 N_Derived_Type_Definition
18671 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18672 then
18673 return False;
18674 end if;
18675
18676 Comp_List :=
18677 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18678
18679 if Present (Discriminant_Specifications (Type_Decl)) then
18680 return False;
18681
18682 elsif Present (Comp_List)
18683 and then Is_Non_Empty_List (Component_Items (Comp_List))
18684 then
18685 Comp := First (Component_Items (Comp_List));
18686
18687 -- Only user-defined components are relevant. The component list
18688 -- may also contain a parent component and internal components
18689 -- corresponding to secondary tags, but these do not determine
18690 -- whether this is a null extension.
18691
18692 while Present (Comp) loop
18693 if Comes_From_Source (Comp) then
18694 return False;
18695 end if;
18696
18697 Next (Comp);
18698 end loop;
18699
18700 return True;
18701
18702 else
18703 return True;
18704 end if;
18705 end Is_Null_Extension;
18706
18707 ------------------------------
18708 -- Is_Valid_Constraint_Kind --
18709 ------------------------------
18710
18711 function Is_Valid_Constraint_Kind
18712 (T_Kind : Type_Kind;
18713 Constraint_Kind : Node_Kind) return Boolean
18714 is
18715 begin
18716 case T_Kind is
18717 when Enumeration_Kind
18718 | Integer_Kind
18719 =>
18720 return Constraint_Kind = N_Range_Constraint;
18721
18722 when Decimal_Fixed_Point_Kind =>
18723 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18724 N_Range_Constraint);
18725
18726 when Ordinary_Fixed_Point_Kind =>
18727 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18728 N_Range_Constraint);
18729
18730 when Float_Kind =>
18731 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18732 N_Range_Constraint);
18733
18734 when Access_Kind
18735 | Array_Kind
18736 | Class_Wide_Kind
18737 | Concurrent_Kind
18738 | Private_Kind
18739 | E_Incomplete_Type
18740 | E_Record_Subtype
18741 | E_Record_Type
18742 =>
18743 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18744
18745 when others =>
18746 return True; -- Error will be detected later
18747 end case;
18748 end Is_Valid_Constraint_Kind;
18749
18750 --------------------------
18751 -- Is_Visible_Component --
18752 --------------------------
18753
18754 function Is_Visible_Component
18755 (C : Entity_Id;
18756 N : Node_Id := Empty) return Boolean
18757 is
18758 Original_Comp : Entity_Id := Empty;
18759 Original_Type : Entity_Id;
18760 Type_Scope : Entity_Id;
18761
18762 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18763 -- Check whether parent type of inherited component is declared locally,
18764 -- possibly within a nested package or instance. The current scope is
18765 -- the derived record itself.
18766
18767 -------------------
18768 -- Is_Local_Type --
18769 -------------------
18770
18771 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18772 Scop : Entity_Id;
18773
18774 begin
18775 Scop := Scope (Typ);
18776 while Present (Scop)
18777 and then Scop /= Standard_Standard
18778 loop
18779 if Scop = Scope (Current_Scope) then
18780 return True;
18781 end if;
18782
18783 Scop := Scope (Scop);
18784 end loop;
18785
18786 return False;
18787 end Is_Local_Type;
18788
18789 -- Start of processing for Is_Visible_Component
18790
18791 begin
18792 if Ekind_In (C, E_Component, E_Discriminant) then
18793 Original_Comp := Original_Record_Component (C);
18794 end if;
18795
18796 if No (Original_Comp) then
18797
18798 -- Premature usage, or previous error
18799
18800 return False;
18801
18802 else
18803 Original_Type := Scope (Original_Comp);
18804 Type_Scope := Scope (Base_Type (Scope (C)));
18805 end if;
18806
18807 -- This test only concerns tagged types
18808
18809 if not Is_Tagged_Type (Original_Type) then
18810
18811 -- Check if this is a renamed discriminant (hidden either by the
18812 -- derived type or by some ancestor), unless we are analyzing code
18813 -- generated by the expander since it may reference such components
18814 -- (for example see the expansion of Deep_Adjust).
18815
18816 if Ekind (C) = E_Discriminant and then Present (N) then
18817 return
18818 not Comes_From_Source (N)
18819 or else not Is_Completely_Hidden (C);
18820 else
18821 return True;
18822 end if;
18823
18824 -- If it is _Parent or _Tag, there is no visibility issue
18825
18826 elsif not Comes_From_Source (Original_Comp) then
18827 return True;
18828
18829 -- Discriminants are visible unless the (private) type has unknown
18830 -- discriminants. If the discriminant reference is inserted for a
18831 -- discriminant check on a full view it is also visible.
18832
18833 elsif Ekind (Original_Comp) = E_Discriminant
18834 and then
18835 (not Has_Unknown_Discriminants (Original_Type)
18836 or else (Present (N)
18837 and then Nkind (N) = N_Selected_Component
18838 and then Nkind (Prefix (N)) = N_Type_Conversion
18839 and then not Comes_From_Source (Prefix (N))))
18840 then
18841 return True;
18842
18843 -- In the body of an instantiation, check the visibility of a component
18844 -- in case it has a homograph that is a primitive operation of a private
18845 -- type which was not visible in the generic unit.
18846
18847 -- Should Is_Prefixed_Call be propagated from template to instance???
18848
18849 elsif In_Instance_Body then
18850 if not Is_Tagged_Type (Original_Type)
18851 or else not Is_Private_Type (Original_Type)
18852 then
18853 return True;
18854
18855 else
18856 declare
18857 Subp_Elmt : Elmt_Id;
18858
18859 begin
18860 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18861 while Present (Subp_Elmt) loop
18862
18863 -- The component is hidden by a primitive operation
18864
18865 if Chars (Node (Subp_Elmt)) = Chars (C) then
18866 return False;
18867 end if;
18868
18869 Next_Elmt (Subp_Elmt);
18870 end loop;
18871
18872 return True;
18873 end;
18874 end if;
18875
18876 -- If the component has been declared in an ancestor which is currently
18877 -- a private type, then it is not visible. The same applies if the
18878 -- component's containing type is not in an open scope and the original
18879 -- component's enclosing type is a visible full view of a private type
18880 -- (which can occur in cases where an attempt is being made to reference
18881 -- a component in a sibling package that is inherited from a visible
18882 -- component of a type in an ancestor package; the component in the
18883 -- sibling package should not be visible even though the component it
18884 -- inherited from is visible). This does not apply however in the case
18885 -- where the scope of the type is a private child unit, or when the
18886 -- parent comes from a local package in which the ancestor is currently
18887 -- visible. The latter suppression of visibility is needed for cases
18888 -- that are tested in B730006.
18889
18890 elsif Is_Private_Type (Original_Type)
18891 or else
18892 (not Is_Private_Descendant (Type_Scope)
18893 and then not In_Open_Scopes (Type_Scope)
18894 and then Has_Private_Declaration (Original_Type))
18895 then
18896 -- If the type derives from an entity in a formal package, there
18897 -- are no additional visible components.
18898
18899 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18900 N_Formal_Package_Declaration
18901 then
18902 return False;
18903
18904 -- if we are not in the private part of the current package, there
18905 -- are no additional visible components.
18906
18907 elsif Ekind (Scope (Current_Scope)) = E_Package
18908 and then not In_Private_Part (Scope (Current_Scope))
18909 then
18910 return False;
18911 else
18912 return
18913 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18914 and then In_Open_Scopes (Scope (Original_Type))
18915 and then Is_Local_Type (Type_Scope);
18916 end if;
18917
18918 -- There is another weird way in which a component may be invisible when
18919 -- the private and the full view are not derived from the same ancestor.
18920 -- Here is an example :
18921
18922 -- type A1 is tagged record F1 : integer; end record;
18923 -- type A2 is new A1 with record F2 : integer; end record;
18924 -- type T is new A1 with private;
18925 -- private
18926 -- type T is new A2 with null record;
18927
18928 -- In this case, the full view of T inherits F1 and F2 but the private
18929 -- view inherits only F1
18930
18931 else
18932 declare
18933 Ancestor : Entity_Id := Scope (C);
18934
18935 begin
18936 loop
18937 if Ancestor = Original_Type then
18938 return True;
18939
18940 -- The ancestor may have a partial view of the original type,
18941 -- but if the full view is in scope, as in a child body, the
18942 -- component is visible.
18943
18944 elsif In_Private_Part (Scope (Original_Type))
18945 and then Full_View (Ancestor) = Original_Type
18946 then
18947 return True;
18948
18949 elsif Ancestor = Etype (Ancestor) then
18950
18951 -- No further ancestors to examine
18952
18953 return False;
18954 end if;
18955
18956 Ancestor := Etype (Ancestor);
18957 end loop;
18958 end;
18959 end if;
18960 end Is_Visible_Component;
18961
18962 --------------------------
18963 -- Make_Class_Wide_Type --
18964 --------------------------
18965
18966 procedure Make_Class_Wide_Type (T : Entity_Id) is
18967 CW_Type : Entity_Id;
18968 CW_Name : Name_Id;
18969 Next_E : Entity_Id;
18970 Prev_E : Entity_Id;
18971
18972 begin
18973 if Present (Class_Wide_Type (T)) then
18974
18975 -- The class-wide type is a partially decorated entity created for a
18976 -- unanalyzed tagged type referenced through a limited with clause.
18977 -- When the tagged type is analyzed, its class-wide type needs to be
18978 -- redecorated. Note that we reuse the entity created by Decorate_
18979 -- Tagged_Type in order to preserve all links.
18980
18981 if Materialize_Entity (Class_Wide_Type (T)) then
18982 CW_Type := Class_Wide_Type (T);
18983 Set_Materialize_Entity (CW_Type, False);
18984
18985 -- The class wide type can have been defined by the partial view, in
18986 -- which case everything is already done.
18987
18988 else
18989 return;
18990 end if;
18991
18992 -- Default case, we need to create a new class-wide type
18993
18994 else
18995 CW_Type :=
18996 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18997 end if;
18998
18999 -- Inherit root type characteristics
19000
19001 CW_Name := Chars (CW_Type);
19002 Next_E := Next_Entity (CW_Type);
19003 Prev_E := Prev_Entity (CW_Type);
19004 Copy_Node (T, CW_Type);
19005 Set_Comes_From_Source (CW_Type, False);
19006 Set_Chars (CW_Type, CW_Name);
19007 Set_Parent (CW_Type, Parent (T));
19008 Set_Prev_Entity (CW_Type, Prev_E);
19009 Set_Next_Entity (CW_Type, Next_E);
19010
19011 -- Ensure we have a new freeze node for the class-wide type. The partial
19012 -- view may have freeze action of its own, requiring a proper freeze
19013 -- node, and the same freeze node cannot be shared between the two
19014 -- types.
19015
19016 Set_Has_Delayed_Freeze (CW_Type);
19017 Set_Freeze_Node (CW_Type, Empty);
19018
19019 -- Customize the class-wide type: It has no prim. op., it cannot be
19020 -- abstract, its Etype points back to the specific root type, and it
19021 -- cannot have any invariants.
19022
19023 Set_Ekind (CW_Type, E_Class_Wide_Type);
19024 Set_Is_Tagged_Type (CW_Type, True);
19025 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19026 Set_Is_Abstract_Type (CW_Type, False);
19027 Set_Is_Constrained (CW_Type, False);
19028 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19029 Set_Default_SSO (CW_Type);
19030 Set_Has_Inheritable_Invariants (CW_Type, False);
19031 Set_Has_Inherited_Invariants (CW_Type, False);
19032 Set_Has_Own_Invariants (CW_Type, False);
19033
19034 if Ekind (T) = E_Class_Wide_Subtype then
19035 Set_Etype (CW_Type, Etype (Base_Type (T)));
19036 else
19037 Set_Etype (CW_Type, T);
19038 end if;
19039
19040 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19041
19042 -- If this is the class_wide type of a constrained subtype, it does
19043 -- not have discriminants.
19044
19045 Set_Has_Discriminants (CW_Type,
19046 Has_Discriminants (T) and then not Is_Constrained (T));
19047
19048 Set_Has_Unknown_Discriminants (CW_Type, True);
19049 Set_Class_Wide_Type (T, CW_Type);
19050 Set_Equivalent_Type (CW_Type, Empty);
19051
19052 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19053
19054 Set_Class_Wide_Type (CW_Type, CW_Type);
19055 end Make_Class_Wide_Type;
19056
19057 ----------------
19058 -- Make_Index --
19059 ----------------
19060
19061 procedure Make_Index
19062 (N : Node_Id;
19063 Related_Nod : Node_Id;
19064 Related_Id : Entity_Id := Empty;
19065 Suffix_Index : Nat := 1;
19066 In_Iter_Schm : Boolean := False)
19067 is
19068 R : Node_Id;
19069 T : Entity_Id;
19070 Def_Id : Entity_Id := Empty;
19071 Found : Boolean := False;
19072
19073 begin
19074 -- For a discrete range used in a constrained array definition and
19075 -- defined by a range, an implicit conversion to the predefined type
19076 -- INTEGER is assumed if each bound is either a numeric literal, a named
19077 -- number, or an attribute, and the type of both bounds (prior to the
19078 -- implicit conversion) is the type universal_integer. Otherwise, both
19079 -- bounds must be of the same discrete type, other than universal
19080 -- integer; this type must be determinable independently of the
19081 -- context, but using the fact that the type must be discrete and that
19082 -- both bounds must have the same type.
19083
19084 -- Character literals also have a universal type in the absence of
19085 -- of additional context, and are resolved to Standard_Character.
19086
19087 if Nkind (N) = N_Range then
19088
19089 -- The index is given by a range constraint. The bounds are known
19090 -- to be of a consistent type.
19091
19092 if not Is_Overloaded (N) then
19093 T := Etype (N);
19094
19095 -- For universal bounds, choose the specific predefined type
19096
19097 if T = Universal_Integer then
19098 T := Standard_Integer;
19099
19100 elsif T = Any_Character then
19101 Ambiguous_Character (Low_Bound (N));
19102
19103 T := Standard_Character;
19104 end if;
19105
19106 -- The node may be overloaded because some user-defined operators
19107 -- are available, but if a universal interpretation exists it is
19108 -- also the selected one.
19109
19110 elsif Universal_Interpretation (N) = Universal_Integer then
19111 T := Standard_Integer;
19112
19113 else
19114 T := Any_Type;
19115
19116 declare
19117 Ind : Interp_Index;
19118 It : Interp;
19119
19120 begin
19121 Get_First_Interp (N, Ind, It);
19122 while Present (It.Typ) loop
19123 if Is_Discrete_Type (It.Typ) then
19124
19125 if Found
19126 and then not Covers (It.Typ, T)
19127 and then not Covers (T, It.Typ)
19128 then
19129 Error_Msg_N ("ambiguous bounds in discrete range", N);
19130 exit;
19131 else
19132 T := It.Typ;
19133 Found := True;
19134 end if;
19135 end if;
19136
19137 Get_Next_Interp (Ind, It);
19138 end loop;
19139
19140 if T = Any_Type then
19141 Error_Msg_N ("discrete type required for range", N);
19142 Set_Etype (N, Any_Type);
19143 return;
19144
19145 elsif T = Universal_Integer then
19146 T := Standard_Integer;
19147 end if;
19148 end;
19149 end if;
19150
19151 if not Is_Discrete_Type (T) then
19152 Error_Msg_N ("discrete type required for range", N);
19153 Set_Etype (N, Any_Type);
19154 return;
19155 end if;
19156
19157 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19158 and then Attribute_Name (Low_Bound (N)) = Name_First
19159 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19160 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19161 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19162 then
19163 -- The type of the index will be the type of the prefix, as long
19164 -- as the upper bound is 'Last of the same type.
19165
19166 Def_Id := Entity (Prefix (Low_Bound (N)));
19167
19168 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19169 or else Attribute_Name (High_Bound (N)) /= Name_Last
19170 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19171 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19172 then
19173 Def_Id := Empty;
19174 end if;
19175 end if;
19176
19177 R := N;
19178 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19179
19180 elsif Nkind (N) = N_Subtype_Indication then
19181
19182 -- The index is given by a subtype with a range constraint
19183
19184 T := Base_Type (Entity (Subtype_Mark (N)));
19185
19186 if not Is_Discrete_Type (T) then
19187 Error_Msg_N ("discrete type required for range", N);
19188 Set_Etype (N, Any_Type);
19189 return;
19190 end if;
19191
19192 R := Range_Expression (Constraint (N));
19193
19194 Resolve (R, T);
19195 Process_Range_Expr_In_Decl
19196 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19197
19198 elsif Nkind (N) = N_Attribute_Reference then
19199
19200 -- Catch beginner's error (use of attribute other than 'Range)
19201
19202 if Attribute_Name (N) /= Name_Range then
19203 Error_Msg_N ("expect attribute ''Range", N);
19204 Set_Etype (N, Any_Type);
19205 return;
19206 end if;
19207
19208 -- If the node denotes the range of a type mark, that is also the
19209 -- resulting type, and we do not need to create an Itype for it.
19210
19211 if Is_Entity_Name (Prefix (N))
19212 and then Comes_From_Source (N)
19213 and then Is_Type (Entity (Prefix (N)))
19214 and then Is_Discrete_Type (Entity (Prefix (N)))
19215 then
19216 Def_Id := Entity (Prefix (N));
19217 end if;
19218
19219 Analyze_And_Resolve (N);
19220 T := Etype (N);
19221 R := N;
19222
19223 -- If none of the above, must be a subtype. We convert this to a
19224 -- range attribute reference because in the case of declared first
19225 -- named subtypes, the types in the range reference can be different
19226 -- from the type of the entity. A range attribute normalizes the
19227 -- reference and obtains the correct types for the bounds.
19228
19229 -- This transformation is in the nature of an expansion, is only
19230 -- done if expansion is active. In particular, it is not done on
19231 -- formal generic types, because we need to retain the name of the
19232 -- original index for instantiation purposes.
19233
19234 else
19235 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19236 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19237 Set_Etype (N, Any_Integer);
19238 return;
19239
19240 else
19241 -- The type mark may be that of an incomplete type. It is only
19242 -- now that we can get the full view, previous analysis does
19243 -- not look specifically for a type mark.
19244
19245 Set_Entity (N, Get_Full_View (Entity (N)));
19246 Set_Etype (N, Entity (N));
19247 Def_Id := Entity (N);
19248
19249 if not Is_Discrete_Type (Def_Id) then
19250 Error_Msg_N ("discrete type required for index", N);
19251 Set_Etype (N, Any_Type);
19252 return;
19253 end if;
19254 end if;
19255
19256 if Expander_Active then
19257 Rewrite (N,
19258 Make_Attribute_Reference (Sloc (N),
19259 Attribute_Name => Name_Range,
19260 Prefix => Relocate_Node (N)));
19261
19262 -- The original was a subtype mark that does not freeze. This
19263 -- means that the rewritten version must not freeze either.
19264
19265 Set_Must_Not_Freeze (N);
19266 Set_Must_Not_Freeze (Prefix (N));
19267 Analyze_And_Resolve (N);
19268 T := Etype (N);
19269 R := N;
19270
19271 -- If expander is inactive, type is legal, nothing else to construct
19272
19273 else
19274 return;
19275 end if;
19276 end if;
19277
19278 if not Is_Discrete_Type (T) then
19279 Error_Msg_N ("discrete type required for range", N);
19280 Set_Etype (N, Any_Type);
19281 return;
19282
19283 elsif T = Any_Type then
19284 Set_Etype (N, Any_Type);
19285 return;
19286 end if;
19287
19288 -- We will now create the appropriate Itype to describe the range, but
19289 -- first a check. If we originally had a subtype, then we just label
19290 -- the range with this subtype. Not only is there no need to construct
19291 -- a new subtype, but it is wrong to do so for two reasons:
19292
19293 -- 1. A legality concern, if we have a subtype, it must not freeze,
19294 -- and the Itype would cause freezing incorrectly
19295
19296 -- 2. An efficiency concern, if we created an Itype, it would not be
19297 -- recognized as the same type for the purposes of eliminating
19298 -- checks in some circumstances.
19299
19300 -- We signal this case by setting the subtype entity in Def_Id
19301
19302 if No (Def_Id) then
19303 Def_Id :=
19304 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19305 Set_Etype (Def_Id, Base_Type (T));
19306
19307 if Is_Signed_Integer_Type (T) then
19308 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19309
19310 elsif Is_Modular_Integer_Type (T) then
19311 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19312
19313 else
19314 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19315 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19316 Set_First_Literal (Def_Id, First_Literal (T));
19317 end if;
19318
19319 Set_Size_Info (Def_Id, (T));
19320 Set_RM_Size (Def_Id, RM_Size (T));
19321 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19322
19323 Set_Scalar_Range (Def_Id, R);
19324 Conditional_Delay (Def_Id, T);
19325
19326 if Nkind (N) = N_Subtype_Indication then
19327 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19328 end if;
19329
19330 -- In the subtype indication case, if the immediate parent of the
19331 -- new subtype is non-static, then the subtype we create is non-
19332 -- static, even if its bounds are static.
19333
19334 if Nkind (N) = N_Subtype_Indication
19335 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19336 then
19337 Set_Is_Non_Static_Subtype (Def_Id);
19338 end if;
19339 end if;
19340
19341 -- Final step is to label the index with this constructed type
19342
19343 Set_Etype (N, Def_Id);
19344 end Make_Index;
19345
19346 ------------------------------
19347 -- Modular_Type_Declaration --
19348 ------------------------------
19349
19350 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19351 Mod_Expr : constant Node_Id := Expression (Def);
19352 M_Val : Uint;
19353
19354 procedure Set_Modular_Size (Bits : Int);
19355 -- Sets RM_Size to Bits, and Esize to normal word size above this
19356
19357 ----------------------
19358 -- Set_Modular_Size --
19359 ----------------------
19360
19361 procedure Set_Modular_Size (Bits : Int) is
19362 begin
19363 Set_RM_Size (T, UI_From_Int (Bits));
19364
19365 if Bits <= 8 then
19366 Init_Esize (T, 8);
19367
19368 elsif Bits <= 16 then
19369 Init_Esize (T, 16);
19370
19371 elsif Bits <= 32 then
19372 Init_Esize (T, 32);
19373
19374 else
19375 Init_Esize (T, System_Max_Binary_Modulus_Power);
19376 end if;
19377
19378 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19379 Set_Is_Known_Valid (T);
19380 end if;
19381 end Set_Modular_Size;
19382
19383 -- Start of processing for Modular_Type_Declaration
19384
19385 begin
19386 -- If the mod expression is (exactly) 2 * literal, where literal is
19387 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19388
19389 if Warn_On_Suspicious_Modulus_Value
19390 and then Nkind (Mod_Expr) = N_Op_Multiply
19391 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19392 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19393 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19394 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19395 then
19396 Error_Msg_N
19397 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19398 end if;
19399
19400 -- Proceed with analysis of mod expression
19401
19402 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19403 Set_Etype (T, T);
19404 Set_Ekind (T, E_Modular_Integer_Type);
19405 Init_Alignment (T);
19406 Set_Is_Constrained (T);
19407
19408 if not Is_OK_Static_Expression (Mod_Expr) then
19409 Flag_Non_Static_Expr
19410 ("non-static expression used for modular type bound!", Mod_Expr);
19411 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19412 else
19413 M_Val := Expr_Value (Mod_Expr);
19414 end if;
19415
19416 if M_Val < 1 then
19417 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19418 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19419 end if;
19420
19421 if M_Val > 2 ** Standard_Long_Integer_Size then
19422 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19423 end if;
19424
19425 Set_Modulus (T, M_Val);
19426
19427 -- Create bounds for the modular type based on the modulus given in
19428 -- the type declaration and then analyze and resolve those bounds.
19429
19430 Set_Scalar_Range (T,
19431 Make_Range (Sloc (Mod_Expr),
19432 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19433 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19434
19435 -- Properly analyze the literals for the range. We do this manually
19436 -- because we can't go calling Resolve, since we are resolving these
19437 -- bounds with the type, and this type is certainly not complete yet.
19438
19439 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19440 Set_Etype (High_Bound (Scalar_Range (T)), T);
19441 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19442 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19443
19444 -- Loop through powers of two to find number of bits required
19445
19446 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19447
19448 -- Binary case
19449
19450 if M_Val = 2 ** Bits then
19451 Set_Modular_Size (Bits);
19452 return;
19453
19454 -- Nonbinary case
19455
19456 elsif M_Val < 2 ** Bits then
19457 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19458 Set_Non_Binary_Modulus (T);
19459
19460 if Bits > System_Max_Nonbinary_Modulus_Power then
19461 Error_Msg_Uint_1 :=
19462 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19463 Error_Msg_F
19464 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19465 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19466 return;
19467
19468 else
19469 -- In the nonbinary case, set size as per RM 13.3(55)
19470
19471 Set_Modular_Size (Bits);
19472 return;
19473 end if;
19474 end if;
19475
19476 end loop;
19477
19478 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19479 -- so we just signal an error and set the maximum size.
19480
19481 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19482 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19483
19484 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19485 Init_Alignment (T);
19486
19487 end Modular_Type_Declaration;
19488
19489 --------------------------
19490 -- New_Concatenation_Op --
19491 --------------------------
19492
19493 procedure New_Concatenation_Op (Typ : Entity_Id) is
19494 Loc : constant Source_Ptr := Sloc (Typ);
19495 Op : Entity_Id;
19496
19497 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19498 -- Create abbreviated declaration for the formal of a predefined
19499 -- Operator 'Op' of type 'Typ'
19500
19501 --------------------
19502 -- Make_Op_Formal --
19503 --------------------
19504
19505 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19506 Formal : Entity_Id;
19507 begin
19508 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19509 Set_Etype (Formal, Typ);
19510 Set_Mechanism (Formal, Default_Mechanism);
19511 return Formal;
19512 end Make_Op_Formal;
19513
19514 -- Start of processing for New_Concatenation_Op
19515
19516 begin
19517 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19518
19519 Set_Ekind (Op, E_Operator);
19520 Set_Scope (Op, Current_Scope);
19521 Set_Etype (Op, Typ);
19522 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19523 Set_Is_Immediately_Visible (Op);
19524 Set_Is_Intrinsic_Subprogram (Op);
19525 Set_Has_Completion (Op);
19526 Append_Entity (Op, Current_Scope);
19527
19528 Set_Name_Entity_Id (Name_Op_Concat, Op);
19529
19530 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19531 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19532 end New_Concatenation_Op;
19533
19534 -------------------------
19535 -- OK_For_Limited_Init --
19536 -------------------------
19537
19538 -- ???Check all calls of this, and compare the conditions under which it's
19539 -- called.
19540
19541 function OK_For_Limited_Init
19542 (Typ : Entity_Id;
19543 Exp : Node_Id) return Boolean
19544 is
19545 begin
19546 return Is_CPP_Constructor_Call (Exp)
19547 or else (Ada_Version >= Ada_2005
19548 and then not Debug_Flag_Dot_L
19549 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19550 end OK_For_Limited_Init;
19551
19552 -------------------------------
19553 -- OK_For_Limited_Init_In_05 --
19554 -------------------------------
19555
19556 function OK_For_Limited_Init_In_05
19557 (Typ : Entity_Id;
19558 Exp : Node_Id) return Boolean
19559 is
19560 begin
19561 -- An object of a limited interface type can be initialized with any
19562 -- expression of a nonlimited descendant type. However this does not
19563 -- apply if this is a view conversion of some other expression. This
19564 -- is checked below.
19565
19566 if Is_Class_Wide_Type (Typ)
19567 and then Is_Limited_Interface (Typ)
19568 and then not Is_Limited_Type (Etype (Exp))
19569 and then Nkind (Exp) /= N_Type_Conversion
19570 then
19571 return True;
19572 end if;
19573
19574 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19575 -- case of limited aggregates (including extension aggregates), and
19576 -- function calls. The function call may have been given in prefixed
19577 -- notation, in which case the original node is an indexed component.
19578 -- If the function is parameterless, the original node was an explicit
19579 -- dereference. The function may also be parameterless, in which case
19580 -- the source node is just an identifier.
19581
19582 -- A branch of a conditional expression may have been removed if the
19583 -- condition is statically known. This happens during expansion, and
19584 -- thus will not happen if previous errors were encountered. The check
19585 -- will have been performed on the chosen branch, which replaces the
19586 -- original conditional expression.
19587
19588 if No (Exp) then
19589 return True;
19590 end if;
19591
19592 case Nkind (Original_Node (Exp)) is
19593 when N_Aggregate
19594 | N_Extension_Aggregate
19595 | N_Function_Call
19596 | N_Op
19597 =>
19598 return True;
19599
19600 when N_Identifier =>
19601 return Present (Entity (Original_Node (Exp)))
19602 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19603
19604 when N_Qualified_Expression =>
19605 return
19606 OK_For_Limited_Init_In_05
19607 (Typ, Expression (Original_Node (Exp)));
19608
19609 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19610 -- with a function call, the expander has rewritten the call into an
19611 -- N_Type_Conversion node to force displacement of the pointer to
19612 -- reference the component containing the secondary dispatch table.
19613 -- Otherwise a type conversion is not a legal context.
19614 -- A return statement for a build-in-place function returning a
19615 -- synchronized type also introduces an unchecked conversion.
19616
19617 when N_Type_Conversion
19618 | N_Unchecked_Type_Conversion
19619 =>
19620 return not Comes_From_Source (Exp)
19621 and then
19622 OK_For_Limited_Init_In_05
19623 (Typ, Expression (Original_Node (Exp)));
19624
19625 when N_Explicit_Dereference
19626 | N_Indexed_Component
19627 | N_Selected_Component
19628 =>
19629 return Nkind (Exp) = N_Function_Call;
19630
19631 -- A use of 'Input is a function call, hence allowed. Normally the
19632 -- attribute will be changed to a call, but the attribute by itself
19633 -- can occur with -gnatc.
19634
19635 when N_Attribute_Reference =>
19636 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19637
19638 -- "return raise ..." is OK
19639
19640 when N_Raise_Expression =>
19641 return True;
19642
19643 -- For a case expression, all dependent expressions must be legal
19644
19645 when N_Case_Expression =>
19646 declare
19647 Alt : Node_Id;
19648
19649 begin
19650 Alt := First (Alternatives (Original_Node (Exp)));
19651 while Present (Alt) loop
19652 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19653 return False;
19654 end if;
19655
19656 Next (Alt);
19657 end loop;
19658
19659 return True;
19660 end;
19661
19662 -- For an if expression, all dependent expressions must be legal
19663
19664 when N_If_Expression =>
19665 declare
19666 Then_Expr : constant Node_Id :=
19667 Next (First (Expressions (Original_Node (Exp))));
19668 Else_Expr : constant Node_Id := Next (Then_Expr);
19669 begin
19670 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19671 and then
19672 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19673 end;
19674
19675 when others =>
19676 return False;
19677 end case;
19678 end OK_For_Limited_Init_In_05;
19679
19680 -------------------------------------------
19681 -- Ordinary_Fixed_Point_Type_Declaration --
19682 -------------------------------------------
19683
19684 procedure Ordinary_Fixed_Point_Type_Declaration
19685 (T : Entity_Id;
19686 Def : Node_Id)
19687 is
19688 Loc : constant Source_Ptr := Sloc (Def);
19689 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19690 RRS : constant Node_Id := Real_Range_Specification (Def);
19691 Implicit_Base : Entity_Id;
19692 Delta_Val : Ureal;
19693 Small_Val : Ureal;
19694 Low_Val : Ureal;
19695 High_Val : Ureal;
19696
19697 begin
19698 Check_Restriction (No_Fixed_Point, Def);
19699
19700 -- Create implicit base type
19701
19702 Implicit_Base :=
19703 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19704 Set_Etype (Implicit_Base, Implicit_Base);
19705
19706 -- Analyze and process delta expression
19707
19708 Analyze_And_Resolve (Delta_Expr, Any_Real);
19709
19710 Check_Delta_Expression (Delta_Expr);
19711 Delta_Val := Expr_Value_R (Delta_Expr);
19712
19713 Set_Delta_Value (Implicit_Base, Delta_Val);
19714
19715 -- Compute default small from given delta, which is the largest power
19716 -- of two that does not exceed the given delta value.
19717
19718 declare
19719 Tmp : Ureal;
19720 Scale : Int;
19721
19722 begin
19723 Tmp := Ureal_1;
19724 Scale := 0;
19725
19726 if Delta_Val < Ureal_1 then
19727 while Delta_Val < Tmp loop
19728 Tmp := Tmp / Ureal_2;
19729 Scale := Scale + 1;
19730 end loop;
19731
19732 else
19733 loop
19734 Tmp := Tmp * Ureal_2;
19735 exit when Tmp > Delta_Val;
19736 Scale := Scale - 1;
19737 end loop;
19738 end if;
19739
19740 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19741 end;
19742
19743 Set_Small_Value (Implicit_Base, Small_Val);
19744
19745 -- If no range was given, set a dummy range
19746
19747 if RRS <= Empty_Or_Error then
19748 Low_Val := -Small_Val;
19749 High_Val := Small_Val;
19750
19751 -- Otherwise analyze and process given range
19752
19753 else
19754 declare
19755 Low : constant Node_Id := Low_Bound (RRS);
19756 High : constant Node_Id := High_Bound (RRS);
19757
19758 begin
19759 Analyze_And_Resolve (Low, Any_Real);
19760 Analyze_And_Resolve (High, Any_Real);
19761 Check_Real_Bound (Low);
19762 Check_Real_Bound (High);
19763
19764 -- Obtain and set the range
19765
19766 Low_Val := Expr_Value_R (Low);
19767 High_Val := Expr_Value_R (High);
19768
19769 if Low_Val > High_Val then
19770 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19771 end if;
19772 end;
19773 end if;
19774
19775 -- The range for both the implicit base and the declared first subtype
19776 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19777 -- set a temporary range in place. Note that the bounds of the base
19778 -- type will be widened to be symmetrical and to fill the available
19779 -- bits when the type is frozen.
19780
19781 -- We could do this with all discrete types, and probably should, but
19782 -- we absolutely have to do it for fixed-point, since the end-points
19783 -- of the range and the size are determined by the small value, which
19784 -- could be reset before the freeze point.
19785
19786 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19787 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19788
19789 -- Complete definition of first subtype. The inheritance of the rep item
19790 -- chain ensures that SPARK-related pragmas are not clobbered when the
19791 -- ordinary fixed point type acts as a full view of a private type.
19792
19793 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19794 Set_Etype (T, Implicit_Base);
19795 Init_Size_Align (T);
19796 Inherit_Rep_Item_Chain (T, Implicit_Base);
19797 Set_Small_Value (T, Small_Val);
19798 Set_Delta_Value (T, Delta_Val);
19799 Set_Is_Constrained (T);
19800 end Ordinary_Fixed_Point_Type_Declaration;
19801
19802 ----------------------------------
19803 -- Preanalyze_Assert_Expression --
19804 ----------------------------------
19805
19806 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19807 begin
19808 In_Assertion_Expr := In_Assertion_Expr + 1;
19809 Preanalyze_Spec_Expression (N, T);
19810 In_Assertion_Expr := In_Assertion_Expr - 1;
19811 end Preanalyze_Assert_Expression;
19812
19813 -----------------------------------
19814 -- Preanalyze_Default_Expression --
19815 -----------------------------------
19816
19817 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19818 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19819 begin
19820 In_Default_Expr := True;
19821 Preanalyze_Spec_Expression (N, T);
19822 In_Default_Expr := Save_In_Default_Expr;
19823 end Preanalyze_Default_Expression;
19824
19825 --------------------------------
19826 -- Preanalyze_Spec_Expression --
19827 --------------------------------
19828
19829 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19830 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19831 begin
19832 In_Spec_Expression := True;
19833 Preanalyze_And_Resolve (N, T);
19834 In_Spec_Expression := Save_In_Spec_Expression;
19835 end Preanalyze_Spec_Expression;
19836
19837 ----------------------------------------
19838 -- Prepare_Private_Subtype_Completion --
19839 ----------------------------------------
19840
19841 procedure Prepare_Private_Subtype_Completion
19842 (Id : Entity_Id;
19843 Related_Nod : Node_Id)
19844 is
19845 Id_B : constant Entity_Id := Base_Type (Id);
19846 Full_B : Entity_Id := Full_View (Id_B);
19847 Full : Entity_Id;
19848
19849 begin
19850 if Present (Full_B) then
19851
19852 -- Get to the underlying full view if necessary
19853
19854 if Is_Private_Type (Full_B)
19855 and then Present (Underlying_Full_View (Full_B))
19856 then
19857 Full_B := Underlying_Full_View (Full_B);
19858 end if;
19859
19860 -- The Base_Type is already completed, we can complete the subtype
19861 -- now. We have to create a new entity with the same name, Thus we
19862 -- can't use Create_Itype.
19863
19864 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19865 Set_Is_Itype (Full);
19866 Set_Associated_Node_For_Itype (Full, Related_Nod);
19867 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19868 end if;
19869
19870 -- The parent subtype may be private, but the base might not, in some
19871 -- nested instances. In that case, the subtype does not need to be
19872 -- exchanged. It would still be nice to make private subtypes and their
19873 -- bases consistent at all times ???
19874
19875 if Is_Private_Type (Id_B) then
19876 Append_Elmt (Id, Private_Dependents (Id_B));
19877 end if;
19878 end Prepare_Private_Subtype_Completion;
19879
19880 ---------------------------
19881 -- Process_Discriminants --
19882 ---------------------------
19883
19884 procedure Process_Discriminants
19885 (N : Node_Id;
19886 Prev : Entity_Id := Empty)
19887 is
19888 Elist : constant Elist_Id := New_Elmt_List;
19889 Id : Node_Id;
19890 Discr : Node_Id;
19891 Discr_Number : Uint;
19892 Discr_Type : Entity_Id;
19893 Default_Present : Boolean := False;
19894 Default_Not_Present : Boolean := False;
19895
19896 begin
19897 -- A composite type other than an array type can have discriminants.
19898 -- On entry, the current scope is the composite type.
19899
19900 -- The discriminants are initially entered into the scope of the type
19901 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19902 -- use, as explained at the end of this procedure.
19903
19904 Discr := First (Discriminant_Specifications (N));
19905 while Present (Discr) loop
19906 Enter_Name (Defining_Identifier (Discr));
19907
19908 -- For navigation purposes we add a reference to the discriminant
19909 -- in the entity for the type. If the current declaration is a
19910 -- completion, place references on the partial view. Otherwise the
19911 -- type is the current scope.
19912
19913 if Present (Prev) then
19914
19915 -- The references go on the partial view, if present. If the
19916 -- partial view has discriminants, the references have been
19917 -- generated already.
19918
19919 if not Has_Discriminants (Prev) then
19920 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19921 end if;
19922 else
19923 Generate_Reference
19924 (Current_Scope, Defining_Identifier (Discr), 'd');
19925 end if;
19926
19927 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19928 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19929
19930 -- Ada 2005 (AI-254)
19931
19932 if Present (Access_To_Subprogram_Definition
19933 (Discriminant_Type (Discr)))
19934 and then Protected_Present (Access_To_Subprogram_Definition
19935 (Discriminant_Type (Discr)))
19936 then
19937 Discr_Type :=
19938 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19939 end if;
19940
19941 else
19942 Find_Type (Discriminant_Type (Discr));
19943 Discr_Type := Etype (Discriminant_Type (Discr));
19944
19945 if Error_Posted (Discriminant_Type (Discr)) then
19946 Discr_Type := Any_Type;
19947 end if;
19948 end if;
19949
19950 -- Handling of discriminants that are access types
19951
19952 if Is_Access_Type (Discr_Type) then
19953
19954 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19955 -- limited record types
19956
19957 if Ada_Version < Ada_2005 then
19958 Check_Access_Discriminant_Requires_Limited
19959 (Discr, Discriminant_Type (Discr));
19960 end if;
19961
19962 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19963 Error_Msg_N
19964 ("(Ada 83) access discriminant not allowed", Discr);
19965 end if;
19966
19967 -- If not access type, must be a discrete type
19968
19969 elsif not Is_Discrete_Type (Discr_Type) then
19970 Error_Msg_N
19971 ("discriminants must have a discrete or access type",
19972 Discriminant_Type (Discr));
19973 end if;
19974
19975 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19976
19977 -- If a discriminant specification includes the assignment compound
19978 -- delimiter followed by an expression, the expression is the default
19979 -- expression of the discriminant; the default expression must be of
19980 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19981 -- a default expression, we do the special preanalysis, since this
19982 -- expression does not freeze (see section "Handling of Default and
19983 -- Per-Object Expressions" in spec of package Sem).
19984
19985 if Present (Expression (Discr)) then
19986 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19987
19988 -- Legaity checks
19989
19990 if Nkind (N) = N_Formal_Type_Declaration then
19991 Error_Msg_N
19992 ("discriminant defaults not allowed for formal type",
19993 Expression (Discr));
19994
19995 -- Flag an error for a tagged type with defaulted discriminants,
19996 -- excluding limited tagged types when compiling for Ada 2012
19997 -- (see AI05-0214).
19998
19999 elsif Is_Tagged_Type (Current_Scope)
20000 and then (not Is_Limited_Type (Current_Scope)
20001 or else Ada_Version < Ada_2012)
20002 and then Comes_From_Source (N)
20003 then
20004 -- Note: see similar test in Check_Or_Process_Discriminants, to
20005 -- handle the (illegal) case of the completion of an untagged
20006 -- view with discriminants with defaults by a tagged full view.
20007 -- We skip the check if Discr does not come from source, to
20008 -- account for the case of an untagged derived type providing
20009 -- defaults for a renamed discriminant from a private untagged
20010 -- ancestor with a tagged full view (ACATS B460006).
20011
20012 if Ada_Version >= Ada_2012 then
20013 Error_Msg_N
20014 ("discriminants of nonlimited tagged type cannot have"
20015 & " defaults",
20016 Expression (Discr));
20017 else
20018 Error_Msg_N
20019 ("discriminants of tagged type cannot have defaults",
20020 Expression (Discr));
20021 end if;
20022
20023 else
20024 Default_Present := True;
20025 Append_Elmt (Expression (Discr), Elist);
20026
20027 -- Tag the defining identifiers for the discriminants with
20028 -- their corresponding default expressions from the tree.
20029
20030 Set_Discriminant_Default_Value
20031 (Defining_Identifier (Discr), Expression (Discr));
20032 end if;
20033
20034 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20035 -- gets set unless we can be sure that no range check is required.
20036
20037 if (GNATprove_Mode or not Expander_Active)
20038 and then not
20039 Is_In_Range
20040 (Expression (Discr), Discr_Type, Assume_Valid => True)
20041 then
20042 Set_Do_Range_Check (Expression (Discr));
20043 end if;
20044
20045 -- No default discriminant value given
20046
20047 else
20048 Default_Not_Present := True;
20049 end if;
20050
20051 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20052 -- Discr_Type but with the null-exclusion attribute
20053
20054 if Ada_Version >= Ada_2005 then
20055
20056 -- Ada 2005 (AI-231): Static checks
20057
20058 if Can_Never_Be_Null (Discr_Type) then
20059 Null_Exclusion_Static_Checks (Discr);
20060
20061 elsif Is_Access_Type (Discr_Type)
20062 and then Null_Exclusion_Present (Discr)
20063
20064 -- No need to check itypes because in their case this check
20065 -- was done at their point of creation
20066
20067 and then not Is_Itype (Discr_Type)
20068 then
20069 if Can_Never_Be_Null (Discr_Type) then
20070 Error_Msg_NE
20071 ("`NOT NULL` not allowed (& already excludes null)",
20072 Discr,
20073 Discr_Type);
20074 end if;
20075
20076 Set_Etype (Defining_Identifier (Discr),
20077 Create_Null_Excluding_Itype
20078 (T => Discr_Type,
20079 Related_Nod => Discr));
20080
20081 -- Check for improper null exclusion if the type is otherwise
20082 -- legal for a discriminant.
20083
20084 elsif Null_Exclusion_Present (Discr)
20085 and then Is_Discrete_Type (Discr_Type)
20086 then
20087 Error_Msg_N
20088 ("null exclusion can only apply to an access type", Discr);
20089 end if;
20090
20091 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20092 -- can't have defaults. Synchronized types, or types that are
20093 -- explicitly limited are fine, but special tests apply to derived
20094 -- types in generics: in a generic body we have to assume the
20095 -- worst, and therefore defaults are not allowed if the parent is
20096 -- a generic formal private type (see ACATS B370001).
20097
20098 if Is_Access_Type (Discr_Type) and then Default_Present then
20099 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20100 or else Is_Limited_Record (Current_Scope)
20101 or else Is_Concurrent_Type (Current_Scope)
20102 or else Is_Concurrent_Record_Type (Current_Scope)
20103 or else Ekind (Current_Scope) = E_Limited_Private_Type
20104 then
20105 if not Is_Derived_Type (Current_Scope)
20106 or else not Is_Generic_Type (Etype (Current_Scope))
20107 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20108 or else Limited_Present
20109 (Type_Definition (Parent (Current_Scope)))
20110 then
20111 null;
20112
20113 else
20114 Error_Msg_N
20115 ("access discriminants of nonlimited types cannot "
20116 & "have defaults", Expression (Discr));
20117 end if;
20118
20119 elsif Present (Expression (Discr)) then
20120 Error_Msg_N
20121 ("(Ada 2005) access discriminants of nonlimited types "
20122 & "cannot have defaults", Expression (Discr));
20123 end if;
20124 end if;
20125 end if;
20126
20127 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
20128 -- This check is relevant only when SPARK_Mode is on as it is not a
20129 -- standard Ada legality rule.
20130
20131 if SPARK_Mode = On
20132 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20133 then
20134 Error_Msg_N ("discriminant cannot be volatile", Discr);
20135 end if;
20136
20137 Next (Discr);
20138 end loop;
20139
20140 -- An element list consisting of the default expressions of the
20141 -- discriminants is constructed in the above loop and used to set
20142 -- the Discriminant_Constraint attribute for the type. If an object
20143 -- is declared of this (record or task) type without any explicit
20144 -- discriminant constraint given, this element list will form the
20145 -- actual parameters for the corresponding initialization procedure
20146 -- for the type.
20147
20148 Set_Discriminant_Constraint (Current_Scope, Elist);
20149 Set_Stored_Constraint (Current_Scope, No_Elist);
20150
20151 -- Default expressions must be provided either for all or for none
20152 -- of the discriminants of a discriminant part. (RM 3.7.1)
20153
20154 if Default_Present and then Default_Not_Present then
20155 Error_Msg_N
20156 ("incomplete specification of defaults for discriminants", N);
20157 end if;
20158
20159 -- The use of the name of a discriminant is not allowed in default
20160 -- expressions of a discriminant part if the specification of the
20161 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20162
20163 -- To detect this, the discriminant names are entered initially with an
20164 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20165 -- attempt to use a void entity (for example in an expression that is
20166 -- type-checked) produces the error message: premature usage. Now after
20167 -- completing the semantic analysis of the discriminant part, we can set
20168 -- the Ekind of all the discriminants appropriately.
20169
20170 Discr := First (Discriminant_Specifications (N));
20171 Discr_Number := Uint_1;
20172 while Present (Discr) loop
20173 Id := Defining_Identifier (Discr);
20174 Set_Ekind (Id, E_Discriminant);
20175 Init_Component_Location (Id);
20176 Init_Esize (Id);
20177 Set_Discriminant_Number (Id, Discr_Number);
20178
20179 -- Make sure this is always set, even in illegal programs
20180
20181 Set_Corresponding_Discriminant (Id, Empty);
20182
20183 -- Initialize the Original_Record_Component to the entity itself.
20184 -- Inherit_Components will propagate the right value to
20185 -- discriminants in derived record types.
20186
20187 Set_Original_Record_Component (Id, Id);
20188
20189 -- Create the discriminal for the discriminant
20190
20191 Build_Discriminal (Id);
20192
20193 Next (Discr);
20194 Discr_Number := Discr_Number + 1;
20195 end loop;
20196
20197 Set_Has_Discriminants (Current_Scope);
20198 end Process_Discriminants;
20199
20200 -----------------------
20201 -- Process_Full_View --
20202 -----------------------
20203
20204 -- WARNING: This routine manages Ghost regions. Return statements must be
20205 -- replaced by gotos which jump to the end of the routine and restore the
20206 -- Ghost mode.
20207
20208 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20209 procedure Collect_Implemented_Interfaces
20210 (Typ : Entity_Id;
20211 Ifaces : Elist_Id);
20212 -- Ada 2005: Gather all the interfaces that Typ directly or
20213 -- inherently implements. Duplicate entries are not added to
20214 -- the list Ifaces.
20215
20216 ------------------------------------
20217 -- Collect_Implemented_Interfaces --
20218 ------------------------------------
20219
20220 procedure Collect_Implemented_Interfaces
20221 (Typ : Entity_Id;
20222 Ifaces : Elist_Id)
20223 is
20224 Iface : Entity_Id;
20225 Iface_Elmt : Elmt_Id;
20226
20227 begin
20228 -- Abstract interfaces are only associated with tagged record types
20229
20230 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20231 return;
20232 end if;
20233
20234 -- Recursively climb to the ancestors
20235
20236 if Etype (Typ) /= Typ
20237
20238 -- Protect the frontend against wrong cyclic declarations like:
20239
20240 -- type B is new A with private;
20241 -- type C is new A with private;
20242 -- private
20243 -- type B is new C with null record;
20244 -- type C is new B with null record;
20245
20246 and then Etype (Typ) /= Priv_T
20247 and then Etype (Typ) /= Full_T
20248 then
20249 -- Keep separate the management of private type declarations
20250
20251 if Ekind (Typ) = E_Record_Type_With_Private then
20252
20253 -- Handle the following illegal usage:
20254 -- type Private_Type is tagged private;
20255 -- private
20256 -- type Private_Type is new Type_Implementing_Iface;
20257
20258 if Present (Full_View (Typ))
20259 and then Etype (Typ) /= Full_View (Typ)
20260 then
20261 if Is_Interface (Etype (Typ)) then
20262 Append_Unique_Elmt (Etype (Typ), Ifaces);
20263 end if;
20264
20265 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20266 end if;
20267
20268 -- Non-private types
20269
20270 else
20271 if Is_Interface (Etype (Typ)) then
20272 Append_Unique_Elmt (Etype (Typ), Ifaces);
20273 end if;
20274
20275 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20276 end if;
20277 end if;
20278
20279 -- Handle entities in the list of abstract interfaces
20280
20281 if Present (Interfaces (Typ)) then
20282 Iface_Elmt := First_Elmt (Interfaces (Typ));
20283 while Present (Iface_Elmt) loop
20284 Iface := Node (Iface_Elmt);
20285
20286 pragma Assert (Is_Interface (Iface));
20287
20288 if not Contain_Interface (Iface, Ifaces) then
20289 Append_Elmt (Iface, Ifaces);
20290 Collect_Implemented_Interfaces (Iface, Ifaces);
20291 end if;
20292
20293 Next_Elmt (Iface_Elmt);
20294 end loop;
20295 end if;
20296 end Collect_Implemented_Interfaces;
20297
20298 -- Local variables
20299
20300 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20301 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20302 -- Save the Ghost-related attributes to restore on exit
20303
20304 Full_Indic : Node_Id;
20305 Full_Parent : Entity_Id;
20306 Priv_Parent : Entity_Id;
20307
20308 -- Start of processing for Process_Full_View
20309
20310 begin
20311 Mark_And_Set_Ghost_Completion (N, Priv_T);
20312
20313 -- First some sanity checks that must be done after semantic
20314 -- decoration of the full view and thus cannot be placed with other
20315 -- similar checks in Find_Type_Name
20316
20317 if not Is_Limited_Type (Priv_T)
20318 and then (Is_Limited_Type (Full_T)
20319 or else Is_Limited_Composite (Full_T))
20320 then
20321 if In_Instance then
20322 null;
20323 else
20324 Error_Msg_N
20325 ("completion of nonlimited type cannot be limited", Full_T);
20326 Explain_Limited_Type (Full_T, Full_T);
20327 end if;
20328
20329 elsif Is_Abstract_Type (Full_T)
20330 and then not Is_Abstract_Type (Priv_T)
20331 then
20332 Error_Msg_N
20333 ("completion of nonabstract type cannot be abstract", Full_T);
20334
20335 elsif Is_Tagged_Type (Priv_T)
20336 and then Is_Limited_Type (Priv_T)
20337 and then not Is_Limited_Type (Full_T)
20338 then
20339 -- If pragma CPP_Class was applied to the private declaration
20340 -- propagate the limitedness to the full-view
20341
20342 if Is_CPP_Class (Priv_T) then
20343 Set_Is_Limited_Record (Full_T);
20344
20345 -- GNAT allow its own definition of Limited_Controlled to disobey
20346 -- this rule in order in ease the implementation. This test is safe
20347 -- because Root_Controlled is defined in a child of System that
20348 -- normal programs are not supposed to use.
20349
20350 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20351 Set_Is_Limited_Composite (Full_T);
20352 else
20353 Error_Msg_N
20354 ("completion of limited tagged type must be limited", Full_T);
20355 end if;
20356
20357 elsif Is_Generic_Type (Priv_T) then
20358 Error_Msg_N ("generic type cannot have a completion", Full_T);
20359 end if;
20360
20361 -- Check that ancestor interfaces of private and full views are
20362 -- consistent. We omit this check for synchronized types because
20363 -- they are performed on the corresponding record type when frozen.
20364
20365 if Ada_Version >= Ada_2005
20366 and then Is_Tagged_Type (Priv_T)
20367 and then Is_Tagged_Type (Full_T)
20368 and then not Is_Concurrent_Type (Full_T)
20369 then
20370 declare
20371 Iface : Entity_Id;
20372 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20373 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20374
20375 begin
20376 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20377 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20378
20379 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20380 -- an interface type if and only if the full type is descendant
20381 -- of the interface type (AARM 7.3 (7.3/2)).
20382
20383 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20384
20385 if Present (Iface) then
20386 Error_Msg_NE
20387 ("interface in partial view& not implemented by full type "
20388 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20389 end if;
20390
20391 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20392
20393 if Present (Iface) then
20394 Error_Msg_NE
20395 ("interface & not implemented by partial view "
20396 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20397 end if;
20398 end;
20399 end if;
20400
20401 if Is_Tagged_Type (Priv_T)
20402 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20403 and then Is_Derived_Type (Full_T)
20404 then
20405 Priv_Parent := Etype (Priv_T);
20406
20407 -- The full view of a private extension may have been transformed
20408 -- into an unconstrained derived type declaration and a subtype
20409 -- declaration (see build_derived_record_type for details).
20410
20411 if Nkind (N) = N_Subtype_Declaration then
20412 Full_Indic := Subtype_Indication (N);
20413 Full_Parent := Etype (Base_Type (Full_T));
20414 else
20415 Full_Indic := Subtype_Indication (Type_Definition (N));
20416 Full_Parent := Etype (Full_T);
20417 end if;
20418
20419 -- Check that the parent type of the full type is a descendant of
20420 -- the ancestor subtype given in the private extension. If either
20421 -- entity has an Etype equal to Any_Type then we had some previous
20422 -- error situation [7.3(8)].
20423
20424 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20425 goto Leave;
20426
20427 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20428 -- any order. Therefore we don't have to check that its parent must
20429 -- be a descendant of the parent of the private type declaration.
20430
20431 elsif Is_Interface (Priv_Parent)
20432 and then Is_Interface (Full_Parent)
20433 then
20434 null;
20435
20436 -- Ada 2005 (AI-251): If the parent of the private type declaration
20437 -- is an interface there is no need to check that it is an ancestor
20438 -- of the associated full type declaration. The required tests for
20439 -- this case are performed by Build_Derived_Record_Type.
20440
20441 elsif not Is_Interface (Base_Type (Priv_Parent))
20442 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20443 then
20444 Error_Msg_N
20445 ("parent of full type must descend from parent of private "
20446 & "extension", Full_Indic);
20447
20448 -- First check a formal restriction, and then proceed with checking
20449 -- Ada rules. Since the formal restriction is not a serious error, we
20450 -- don't prevent further error detection for this check, hence the
20451 -- ELSE.
20452
20453 else
20454 -- In formal mode, when completing a private extension the type
20455 -- named in the private part must be exactly the same as that
20456 -- named in the visible part.
20457
20458 if Priv_Parent /= Full_Parent then
20459 Error_Msg_Name_1 := Chars (Priv_Parent);
20460 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20461 end if;
20462
20463 -- Check the rules of 7.3(10): if the private extension inherits
20464 -- known discriminants, then the full type must also inherit those
20465 -- discriminants from the same (ancestor) type, and the parent
20466 -- subtype of the full type must be constrained if and only if
20467 -- the ancestor subtype of the private extension is constrained.
20468
20469 if No (Discriminant_Specifications (Parent (Priv_T)))
20470 and then not Has_Unknown_Discriminants (Priv_T)
20471 and then Has_Discriminants (Base_Type (Priv_Parent))
20472 then
20473 declare
20474 Priv_Indic : constant Node_Id :=
20475 Subtype_Indication (Parent (Priv_T));
20476
20477 Priv_Constr : constant Boolean :=
20478 Is_Constrained (Priv_Parent)
20479 or else
20480 Nkind (Priv_Indic) = N_Subtype_Indication
20481 or else
20482 Is_Constrained (Entity (Priv_Indic));
20483
20484 Full_Constr : constant Boolean :=
20485 Is_Constrained (Full_Parent)
20486 or else
20487 Nkind (Full_Indic) = N_Subtype_Indication
20488 or else
20489 Is_Constrained (Entity (Full_Indic));
20490
20491 Priv_Discr : Entity_Id;
20492 Full_Discr : Entity_Id;
20493
20494 begin
20495 Priv_Discr := First_Discriminant (Priv_Parent);
20496 Full_Discr := First_Discriminant (Full_Parent);
20497 while Present (Priv_Discr) and then Present (Full_Discr) loop
20498 if Original_Record_Component (Priv_Discr) =
20499 Original_Record_Component (Full_Discr)
20500 or else
20501 Corresponding_Discriminant (Priv_Discr) =
20502 Corresponding_Discriminant (Full_Discr)
20503 then
20504 null;
20505 else
20506 exit;
20507 end if;
20508
20509 Next_Discriminant (Priv_Discr);
20510 Next_Discriminant (Full_Discr);
20511 end loop;
20512
20513 if Present (Priv_Discr) or else Present (Full_Discr) then
20514 Error_Msg_N
20515 ("full view must inherit discriminants of the parent "
20516 & "type used in the private extension", Full_Indic);
20517
20518 elsif Priv_Constr and then not Full_Constr then
20519 Error_Msg_N
20520 ("parent subtype of full type must be constrained",
20521 Full_Indic);
20522
20523 elsif Full_Constr and then not Priv_Constr then
20524 Error_Msg_N
20525 ("parent subtype of full type must be unconstrained",
20526 Full_Indic);
20527 end if;
20528 end;
20529
20530 -- Check the rules of 7.3(12): if a partial view has neither
20531 -- known or unknown discriminants, then the full type
20532 -- declaration shall define a definite subtype.
20533
20534 elsif not Has_Unknown_Discriminants (Priv_T)
20535 and then not Has_Discriminants (Priv_T)
20536 and then not Is_Constrained (Full_T)
20537 then
20538 Error_Msg_N
20539 ("full view must define a constrained type if partial view "
20540 & "has no discriminants", Full_T);
20541 end if;
20542
20543 -- ??????? Do we implement the following properly ?????
20544 -- If the ancestor subtype of a private extension has constrained
20545 -- discriminants, then the parent subtype of the full view shall
20546 -- impose a statically matching constraint on those discriminants
20547 -- [7.3(13)].
20548 end if;
20549
20550 else
20551 -- For untagged types, verify that a type without discriminants is
20552 -- not completed with an unconstrained type. A separate error message
20553 -- is produced if the full type has defaulted discriminants.
20554
20555 if Is_Definite_Subtype (Priv_T)
20556 and then not Is_Definite_Subtype (Full_T)
20557 then
20558 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20559 Error_Msg_NE
20560 ("full view of& not compatible with declaration#",
20561 Full_T, Priv_T);
20562
20563 if not Is_Tagged_Type (Full_T) then
20564 Error_Msg_N
20565 ("\one is constrained, the other unconstrained", Full_T);
20566 end if;
20567 end if;
20568 end if;
20569
20570 -- AI-419: verify that the use of "limited" is consistent
20571
20572 declare
20573 Orig_Decl : constant Node_Id := Original_Node (N);
20574
20575 begin
20576 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20577 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20578 and then Nkind
20579 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20580 then
20581 if not Limited_Present (Parent (Priv_T))
20582 and then not Synchronized_Present (Parent (Priv_T))
20583 and then Limited_Present (Type_Definition (Orig_Decl))
20584 then
20585 Error_Msg_N
20586 ("full view of non-limited extension cannot be limited", N);
20587
20588 -- Conversely, if the partial view carries the limited keyword,
20589 -- the full view must as well, even if it may be redundant.
20590
20591 elsif Limited_Present (Parent (Priv_T))
20592 and then not Limited_Present (Type_Definition (Orig_Decl))
20593 then
20594 Error_Msg_N
20595 ("full view of limited extension must be explicitly limited",
20596 N);
20597 end if;
20598 end if;
20599 end;
20600
20601 -- Ada 2005 (AI-443): A synchronized private extension must be
20602 -- completed by a task or protected type.
20603
20604 if Ada_Version >= Ada_2005
20605 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20606 and then Synchronized_Present (Parent (Priv_T))
20607 and then not Is_Concurrent_Type (Full_T)
20608 then
20609 Error_Msg_N ("full view of synchronized extension must " &
20610 "be synchronized type", N);
20611 end if;
20612
20613 -- Ada 2005 AI-363: if the full view has discriminants with
20614 -- defaults, it is illegal to declare constrained access subtypes
20615 -- whose designated type is the current type. This allows objects
20616 -- of the type that are declared in the heap to be unconstrained.
20617
20618 if not Has_Unknown_Discriminants (Priv_T)
20619 and then not Has_Discriminants (Priv_T)
20620 and then Has_Discriminants (Full_T)
20621 and then
20622 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20623 then
20624 Set_Has_Constrained_Partial_View (Full_T);
20625 Set_Has_Constrained_Partial_View (Priv_T);
20626 end if;
20627
20628 -- Create a full declaration for all its subtypes recorded in
20629 -- Private_Dependents and swap them similarly to the base type. These
20630 -- are subtypes that have been define before the full declaration of
20631 -- the private type. We also swap the entry in Private_Dependents list
20632 -- so we can properly restore the private view on exit from the scope.
20633
20634 declare
20635 Priv_Elmt : Elmt_Id;
20636 Priv_Scop : Entity_Id;
20637 Priv : Entity_Id;
20638 Full : Entity_Id;
20639
20640 begin
20641 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20642 while Present (Priv_Elmt) loop
20643 Priv := Node (Priv_Elmt);
20644 Priv_Scop := Scope (Priv);
20645
20646 if Ekind_In (Priv, E_Private_Subtype,
20647 E_Limited_Private_Subtype,
20648 E_Record_Subtype_With_Private)
20649 then
20650 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20651 Set_Is_Itype (Full);
20652 Set_Parent (Full, Parent (Priv));
20653 Set_Associated_Node_For_Itype (Full, N);
20654
20655 -- Now we need to complete the private subtype, but since the
20656 -- base type has already been swapped, we must also swap the
20657 -- subtypes (and thus, reverse the arguments in the call to
20658 -- Complete_Private_Subtype). Also note that we may need to
20659 -- re-establish the scope of the private subtype.
20660
20661 Copy_And_Swap (Priv, Full);
20662
20663 if not In_Open_Scopes (Priv_Scop) then
20664 Push_Scope (Priv_Scop);
20665
20666 else
20667 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20668
20669 Priv_Scop := Empty;
20670 end if;
20671
20672 Complete_Private_Subtype (Full, Priv, Full_T, N);
20673
20674 if Present (Priv_Scop) then
20675 Pop_Scope;
20676 end if;
20677
20678 Replace_Elmt (Priv_Elmt, Full);
20679 end if;
20680
20681 Next_Elmt (Priv_Elmt);
20682 end loop;
20683 end;
20684
20685 -- If the private view was tagged, copy the new primitive operations
20686 -- from the private view to the full view.
20687
20688 if Is_Tagged_Type (Full_T) then
20689 declare
20690 Disp_Typ : Entity_Id;
20691 Full_List : Elist_Id;
20692 Prim : Entity_Id;
20693 Prim_Elmt : Elmt_Id;
20694 Priv_List : Elist_Id;
20695
20696 function Contains
20697 (E : Entity_Id;
20698 L : Elist_Id) return Boolean;
20699 -- Determine whether list L contains element E
20700
20701 --------------
20702 -- Contains --
20703 --------------
20704
20705 function Contains
20706 (E : Entity_Id;
20707 L : Elist_Id) return Boolean
20708 is
20709 List_Elmt : Elmt_Id;
20710
20711 begin
20712 List_Elmt := First_Elmt (L);
20713 while Present (List_Elmt) loop
20714 if Node (List_Elmt) = E then
20715 return True;
20716 end if;
20717
20718 Next_Elmt (List_Elmt);
20719 end loop;
20720
20721 return False;
20722 end Contains;
20723
20724 -- Start of processing
20725
20726 begin
20727 if Is_Tagged_Type (Priv_T) then
20728 Priv_List := Primitive_Operations (Priv_T);
20729 Prim_Elmt := First_Elmt (Priv_List);
20730
20731 -- In the case of a concurrent type completing a private tagged
20732 -- type, primitives may have been declared in between the two
20733 -- views. These subprograms need to be wrapped the same way
20734 -- entries and protected procedures are handled because they
20735 -- cannot be directly shared by the two views.
20736
20737 if Is_Concurrent_Type (Full_T) then
20738 declare
20739 Conc_Typ : constant Entity_Id :=
20740 Corresponding_Record_Type (Full_T);
20741 Curr_Nod : Node_Id := Parent (Conc_Typ);
20742 Wrap_Spec : Node_Id;
20743
20744 begin
20745 while Present (Prim_Elmt) loop
20746 Prim := Node (Prim_Elmt);
20747
20748 if Comes_From_Source (Prim)
20749 and then not Is_Abstract_Subprogram (Prim)
20750 then
20751 Wrap_Spec :=
20752 Make_Subprogram_Declaration (Sloc (Prim),
20753 Specification =>
20754 Build_Wrapper_Spec
20755 (Subp_Id => Prim,
20756 Obj_Typ => Conc_Typ,
20757 Formals =>
20758 Parameter_Specifications
20759 (Parent (Prim))));
20760
20761 Insert_After (Curr_Nod, Wrap_Spec);
20762 Curr_Nod := Wrap_Spec;
20763
20764 Analyze (Wrap_Spec);
20765
20766 -- Remove the wrapper from visibility to avoid
20767 -- spurious conflict with the wrapped entity.
20768
20769 Set_Is_Immediately_Visible
20770 (Defining_Entity (Specification (Wrap_Spec)),
20771 False);
20772 end if;
20773
20774 Next_Elmt (Prim_Elmt);
20775 end loop;
20776
20777 goto Leave;
20778 end;
20779
20780 -- For non-concurrent types, transfer explicit primitives, but
20781 -- omit those inherited from the parent of the private view
20782 -- since they will be re-inherited later on.
20783
20784 else
20785 Full_List := Primitive_Operations (Full_T);
20786 while Present (Prim_Elmt) loop
20787 Prim := Node (Prim_Elmt);
20788
20789 if Comes_From_Source (Prim)
20790 and then not Contains (Prim, Full_List)
20791 then
20792 Append_Elmt (Prim, Full_List);
20793 end if;
20794
20795 Next_Elmt (Prim_Elmt);
20796 end loop;
20797 end if;
20798
20799 -- Untagged private view
20800
20801 else
20802 Full_List := Primitive_Operations (Full_T);
20803
20804 -- In this case the partial view is untagged, so here we locate
20805 -- all of the earlier primitives that need to be treated as
20806 -- dispatching (those that appear between the two views). Note
20807 -- that these additional operations must all be new operations
20808 -- (any earlier operations that override inherited operations
20809 -- of the full view will already have been inserted in the
20810 -- primitives list, marked by Check_Operation_From_Private_View
20811 -- as dispatching. Note that implicit "/=" operators are
20812 -- excluded from being added to the primitives list since they
20813 -- shouldn't be treated as dispatching (tagged "/=" is handled
20814 -- specially).
20815
20816 Prim := Next_Entity (Full_T);
20817 while Present (Prim) and then Prim /= Priv_T loop
20818 if Ekind_In (Prim, E_Procedure, E_Function) then
20819 Disp_Typ := Find_Dispatching_Type (Prim);
20820
20821 if Disp_Typ = Full_T
20822 and then (Chars (Prim) /= Name_Op_Ne
20823 or else Comes_From_Source (Prim))
20824 then
20825 Check_Controlling_Formals (Full_T, Prim);
20826
20827 if Is_Suitable_Primitive (Prim)
20828 and then not Is_Dispatching_Operation (Prim)
20829 then
20830 Append_Elmt (Prim, Full_List);
20831 Set_Is_Dispatching_Operation (Prim);
20832 Set_DT_Position_Value (Prim, No_Uint);
20833 end if;
20834
20835 elsif Is_Dispatching_Operation (Prim)
20836 and then Disp_Typ /= Full_T
20837 then
20838 -- Verify that it is not otherwise controlled by a
20839 -- formal or a return value of type T.
20840
20841 Check_Controlling_Formals (Disp_Typ, Prim);
20842 end if;
20843 end if;
20844
20845 Next_Entity (Prim);
20846 end loop;
20847 end if;
20848
20849 -- For the tagged case, the two views can share the same primitive
20850 -- operations list and the same class-wide type. Update attributes
20851 -- of the class-wide type which depend on the full declaration.
20852
20853 if Is_Tagged_Type (Priv_T) then
20854 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20855 Set_Class_Wide_Type
20856 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20857
20858 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20859 end if;
20860 end;
20861 end if;
20862
20863 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20864
20865 if Known_To_Have_Preelab_Init (Priv_T) then
20866
20867 -- Case where there is a pragma Preelaborable_Initialization. We
20868 -- always allow this in predefined units, which is cheating a bit,
20869 -- but it means we don't have to struggle to meet the requirements in
20870 -- the RM for having Preelaborable Initialization. Otherwise we
20871 -- require that the type meets the RM rules. But we can't check that
20872 -- yet, because of the rule about overriding Initialize, so we simply
20873 -- set a flag that will be checked at freeze time.
20874
20875 if not In_Predefined_Unit (Full_T) then
20876 Set_Must_Have_Preelab_Init (Full_T);
20877 end if;
20878 end if;
20879
20880 -- If pragma CPP_Class was applied to the private type declaration,
20881 -- propagate it now to the full type declaration.
20882
20883 if Is_CPP_Class (Priv_T) then
20884 Set_Is_CPP_Class (Full_T);
20885 Set_Convention (Full_T, Convention_CPP);
20886
20887 -- Check that components of imported CPP types do not have default
20888 -- expressions.
20889
20890 Check_CPP_Type_Has_No_Defaults (Full_T);
20891 end if;
20892
20893 -- If the private view has user specified stream attributes, then so has
20894 -- the full view.
20895
20896 -- Why the test, how could these flags be already set in Full_T ???
20897
20898 if Has_Specified_Stream_Read (Priv_T) then
20899 Set_Has_Specified_Stream_Read (Full_T);
20900 end if;
20901
20902 if Has_Specified_Stream_Write (Priv_T) then
20903 Set_Has_Specified_Stream_Write (Full_T);
20904 end if;
20905
20906 if Has_Specified_Stream_Input (Priv_T) then
20907 Set_Has_Specified_Stream_Input (Full_T);
20908 end if;
20909
20910 if Has_Specified_Stream_Output (Priv_T) then
20911 Set_Has_Specified_Stream_Output (Full_T);
20912 end if;
20913
20914 -- Propagate Default_Initial_Condition-related attributes from the
20915 -- partial view to the full view and its base type.
20916
20917 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20918 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20919
20920 -- Propagate invariant-related attributes from the partial view to the
20921 -- full view and its base type.
20922
20923 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20924 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20925
20926 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20927 -- in the full view without advertising the inheritance in the partial
20928 -- view. This can only occur when the partial view has no parent type
20929 -- and the full view has an interface as a parent. Any other scenarios
20930 -- are illegal because implemented interfaces must match between the
20931 -- two views.
20932
20933 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20934 declare
20935 Full_Par : constant Entity_Id := Etype (Full_T);
20936 Priv_Par : constant Entity_Id := Etype (Priv_T);
20937
20938 begin
20939 if not Is_Interface (Priv_Par)
20940 and then Is_Interface (Full_Par)
20941 and then Has_Inheritable_Invariants (Full_Par)
20942 then
20943 Error_Msg_N
20944 ("hidden inheritance of class-wide type invariants not "
20945 & "allowed", N);
20946 end if;
20947 end;
20948 end if;
20949
20950 -- Propagate predicates to full type, and predicate function if already
20951 -- defined. It is not clear that this can actually happen? the partial
20952 -- view cannot be frozen yet, and the predicate function has not been
20953 -- built. Still it is a cheap check and seems safer to make it.
20954
20955 if Has_Predicates (Priv_T) then
20956 Set_Has_Predicates (Full_T);
20957
20958 if Present (Predicate_Function (Priv_T)) then
20959 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20960 end if;
20961 end if;
20962
20963 <<Leave>>
20964 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20965 end Process_Full_View;
20966
20967 -----------------------------------
20968 -- Process_Incomplete_Dependents --
20969 -----------------------------------
20970
20971 procedure Process_Incomplete_Dependents
20972 (N : Node_Id;
20973 Full_T : Entity_Id;
20974 Inc_T : Entity_Id)
20975 is
20976 Inc_Elmt : Elmt_Id;
20977 Priv_Dep : Entity_Id;
20978 New_Subt : Entity_Id;
20979
20980 Disc_Constraint : Elist_Id;
20981
20982 begin
20983 if No (Private_Dependents (Inc_T)) then
20984 return;
20985 end if;
20986
20987 -- Itypes that may be generated by the completion of an incomplete
20988 -- subtype are not used by the back-end and not attached to the tree.
20989 -- They are created only for constraint-checking purposes.
20990
20991 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20992 while Present (Inc_Elmt) loop
20993 Priv_Dep := Node (Inc_Elmt);
20994
20995 if Ekind (Priv_Dep) = E_Subprogram_Type then
20996
20997 -- An Access_To_Subprogram type may have a return type or a
20998 -- parameter type that is incomplete. Replace with the full view.
20999
21000 if Etype (Priv_Dep) = Inc_T then
21001 Set_Etype (Priv_Dep, Full_T);
21002 end if;
21003
21004 declare
21005 Formal : Entity_Id;
21006
21007 begin
21008 Formal := First_Formal (Priv_Dep);
21009 while Present (Formal) loop
21010 if Etype (Formal) = Inc_T then
21011 Set_Etype (Formal, Full_T);
21012 end if;
21013
21014 Next_Formal (Formal);
21015 end loop;
21016 end;
21017
21018 elsif Is_Overloadable (Priv_Dep) then
21019
21020 -- If a subprogram in the incomplete dependents list is primitive
21021 -- for a tagged full type then mark it as a dispatching operation,
21022 -- check whether it overrides an inherited subprogram, and check
21023 -- restrictions on its controlling formals. Note that a protected
21024 -- operation is never dispatching: only its wrapper operation
21025 -- (which has convention Ada) is.
21026
21027 if Is_Tagged_Type (Full_T)
21028 and then Is_Primitive (Priv_Dep)
21029 and then Convention (Priv_Dep) /= Convention_Protected
21030 then
21031 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21032 Set_Is_Dispatching_Operation (Priv_Dep);
21033 Check_Controlling_Formals (Full_T, Priv_Dep);
21034 end if;
21035
21036 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21037
21038 -- Can happen during processing of a body before the completion
21039 -- of a TA type. Ignore, because spec is also on dependent list.
21040
21041 return;
21042
21043 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21044 -- corresponding subtype of the full view.
21045
21046 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21047 and then Comes_From_Source (Priv_Dep)
21048 then
21049 Set_Subtype_Indication
21050 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21051 Set_Etype (Priv_Dep, Full_T);
21052 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21053 Set_Analyzed (Parent (Priv_Dep), False);
21054
21055 -- Reanalyze the declaration, suppressing the call to Enter_Name
21056 -- to avoid duplicate names.
21057
21058 Analyze_Subtype_Declaration
21059 (N => Parent (Priv_Dep),
21060 Skip => True);
21061
21062 -- Dependent is a subtype
21063
21064 else
21065 -- We build a new subtype indication using the full view of the
21066 -- incomplete parent. The discriminant constraints have been
21067 -- elaborated already at the point of the subtype declaration.
21068
21069 New_Subt := Create_Itype (E_Void, N);
21070
21071 if Has_Discriminants (Full_T) then
21072 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21073 else
21074 Disc_Constraint := No_Elist;
21075 end if;
21076
21077 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21078 Set_Full_View (Priv_Dep, New_Subt);
21079 end if;
21080
21081 Next_Elmt (Inc_Elmt);
21082 end loop;
21083 end Process_Incomplete_Dependents;
21084
21085 --------------------------------
21086 -- Process_Range_Expr_In_Decl --
21087 --------------------------------
21088
21089 procedure Process_Range_Expr_In_Decl
21090 (R : Node_Id;
21091 T : Entity_Id;
21092 Subtyp : Entity_Id := Empty;
21093 Check_List : List_Id := Empty_List;
21094 R_Check_Off : Boolean := False;
21095 In_Iter_Schm : Boolean := False)
21096 is
21097 Lo, Hi : Node_Id;
21098 R_Checks : Check_Result;
21099 Insert_Node : Node_Id;
21100 Def_Id : Entity_Id;
21101
21102 begin
21103 Analyze_And_Resolve (R, Base_Type (T));
21104
21105 if Nkind (R) = N_Range then
21106
21107 -- In SPARK, all ranges should be static, with the exception of the
21108 -- discrete type definition of a loop parameter specification.
21109
21110 if not In_Iter_Schm
21111 and then not Is_OK_Static_Range (R)
21112 then
21113 Check_SPARK_05_Restriction ("range should be static", R);
21114 end if;
21115
21116 Lo := Low_Bound (R);
21117 Hi := High_Bound (R);
21118
21119 -- Validity checks on the range of a quantified expression are
21120 -- delayed until the construct is transformed into a loop.
21121
21122 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21123 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21124 then
21125 null;
21126
21127 -- We need to ensure validity of the bounds here, because if we
21128 -- go ahead and do the expansion, then the expanded code will get
21129 -- analyzed with range checks suppressed and we miss the check.
21130
21131 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21132 -- the temporaries generated by routine Remove_Side_Effects by means
21133 -- of validity checks must use the same names. When a range appears
21134 -- in the parent of a generic, the range is processed with checks
21135 -- disabled as part of the generic context and with checks enabled
21136 -- for code generation purposes. This leads to link issues as the
21137 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21138 -- template sees the temporaries generated by Remove_Side_Effects.
21139
21140 else
21141 Validity_Check_Range (R, Subtyp);
21142 end if;
21143
21144 -- If there were errors in the declaration, try and patch up some
21145 -- common mistakes in the bounds. The cases handled are literals
21146 -- which are Integer where the expected type is Real and vice versa.
21147 -- These corrections allow the compilation process to proceed further
21148 -- along since some basic assumptions of the format of the bounds
21149 -- are guaranteed.
21150
21151 if Etype (R) = Any_Type then
21152 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21153 Rewrite (Lo,
21154 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21155
21156 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21157 Rewrite (Hi,
21158 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21159
21160 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21161 Rewrite (Lo,
21162 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21163
21164 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21165 Rewrite (Hi,
21166 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21167 end if;
21168
21169 Set_Etype (Lo, T);
21170 Set_Etype (Hi, T);
21171 end if;
21172
21173 -- If the bounds of the range have been mistakenly given as string
21174 -- literals (perhaps in place of character literals), then an error
21175 -- has already been reported, but we rewrite the string literal as a
21176 -- bound of the range's type to avoid blowups in later processing
21177 -- that looks at static values.
21178
21179 if Nkind (Lo) = N_String_Literal then
21180 Rewrite (Lo,
21181 Make_Attribute_Reference (Sloc (Lo),
21182 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21183 Attribute_Name => Name_First));
21184 Analyze_And_Resolve (Lo);
21185 end if;
21186
21187 if Nkind (Hi) = N_String_Literal then
21188 Rewrite (Hi,
21189 Make_Attribute_Reference (Sloc (Hi),
21190 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21191 Attribute_Name => Name_First));
21192 Analyze_And_Resolve (Hi);
21193 end if;
21194
21195 -- If bounds aren't scalar at this point then exit, avoiding
21196 -- problems with further processing of the range in this procedure.
21197
21198 if not Is_Scalar_Type (Etype (Lo)) then
21199 return;
21200 end if;
21201
21202 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21203 -- then range of the base type. Here we check whether the bounds
21204 -- are in the range of the subtype itself. Note that if the bounds
21205 -- represent the null range the Constraint_Error exception should
21206 -- not be raised.
21207
21208 -- ??? The following code should be cleaned up as follows
21209
21210 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21211 -- is done in the call to Range_Check (R, T); below
21212
21213 -- 2. The use of R_Check_Off should be investigated and possibly
21214 -- removed, this would clean up things a bit.
21215
21216 if Is_Null_Range (Lo, Hi) then
21217 null;
21218
21219 else
21220 -- Capture values of bounds and generate temporaries for them
21221 -- if needed, before applying checks, since checks may cause
21222 -- duplication of the expression without forcing evaluation.
21223
21224 -- The forced evaluation removes side effects from expressions,
21225 -- which should occur also in GNATprove mode. Otherwise, we end up
21226 -- with unexpected insertions of actions at places where this is
21227 -- not supposed to occur, e.g. on default parameters of a call.
21228
21229 if Expander_Active or GNATprove_Mode then
21230
21231 -- Call Force_Evaluation to create declarations as needed to
21232 -- deal with side effects, and also create typ_FIRST/LAST
21233 -- entities for bounds if we have a subtype name.
21234
21235 -- Note: we do this transformation even if expansion is not
21236 -- active if we are in GNATprove_Mode since the transformation
21237 -- is in general required to ensure that the resulting tree has
21238 -- proper Ada semantics.
21239
21240 Force_Evaluation
21241 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21242 Force_Evaluation
21243 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21244 end if;
21245
21246 -- We use a flag here instead of suppressing checks on the type
21247 -- because the type we check against isn't necessarily the place
21248 -- where we put the check.
21249
21250 if not R_Check_Off then
21251 R_Checks := Get_Range_Checks (R, T);
21252
21253 -- Look up tree to find an appropriate insertion point. We
21254 -- can't just use insert_actions because later processing
21255 -- depends on the insertion node. Prior to Ada 2012 the
21256 -- insertion point could only be a declaration or a loop, but
21257 -- quantified expressions can appear within any context in an
21258 -- expression, and the insertion point can be any statement,
21259 -- pragma, or declaration.
21260
21261 Insert_Node := Parent (R);
21262 while Present (Insert_Node) loop
21263 exit when
21264 Nkind (Insert_Node) in N_Declaration
21265 and then
21266 not Nkind_In
21267 (Insert_Node, N_Component_Declaration,
21268 N_Loop_Parameter_Specification,
21269 N_Function_Specification,
21270 N_Procedure_Specification);
21271
21272 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21273 or else Nkind (Insert_Node) in
21274 N_Statement_Other_Than_Procedure_Call
21275 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21276 N_Pragma);
21277
21278 Insert_Node := Parent (Insert_Node);
21279 end loop;
21280
21281 -- Why would Type_Decl not be present??? Without this test,
21282 -- short regression tests fail.
21283
21284 if Present (Insert_Node) then
21285
21286 -- Case of loop statement. Verify that the range is part
21287 -- of the subtype indication of the iteration scheme.
21288
21289 if Nkind (Insert_Node) = N_Loop_Statement then
21290 declare
21291 Indic : Node_Id;
21292
21293 begin
21294 Indic := Parent (R);
21295 while Present (Indic)
21296 and then Nkind (Indic) /= N_Subtype_Indication
21297 loop
21298 Indic := Parent (Indic);
21299 end loop;
21300
21301 if Present (Indic) then
21302 Def_Id := Etype (Subtype_Mark (Indic));
21303
21304 Insert_Range_Checks
21305 (R_Checks,
21306 Insert_Node,
21307 Def_Id,
21308 Sloc (Insert_Node),
21309 R,
21310 Do_Before => True);
21311 end if;
21312 end;
21313
21314 -- Insertion before a declaration. If the declaration
21315 -- includes discriminants, the list of applicable checks
21316 -- is given by the caller.
21317
21318 elsif Nkind (Insert_Node) in N_Declaration then
21319 Def_Id := Defining_Identifier (Insert_Node);
21320
21321 if (Ekind (Def_Id) = E_Record_Type
21322 and then Depends_On_Discriminant (R))
21323 or else
21324 (Ekind (Def_Id) = E_Protected_Type
21325 and then Has_Discriminants (Def_Id))
21326 then
21327 Append_Range_Checks
21328 (R_Checks,
21329 Check_List, Def_Id, Sloc (Insert_Node), R);
21330
21331 else
21332 Insert_Range_Checks
21333 (R_Checks,
21334 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21335
21336 end if;
21337
21338 -- Insertion before a statement. Range appears in the
21339 -- context of a quantified expression. Insertion will
21340 -- take place when expression is expanded.
21341
21342 else
21343 null;
21344 end if;
21345 end if;
21346 end if;
21347 end if;
21348
21349 -- Case of other than an explicit N_Range node
21350
21351 -- The forced evaluation removes side effects from expressions, which
21352 -- should occur also in GNATprove mode. Otherwise, we end up with
21353 -- unexpected insertions of actions at places where this is not
21354 -- supposed to occur, e.g. on default parameters of a call.
21355
21356 elsif Expander_Active or GNATprove_Mode then
21357 Get_Index_Bounds (R, Lo, Hi);
21358 Force_Evaluation (Lo);
21359 Force_Evaluation (Hi);
21360 end if;
21361 end Process_Range_Expr_In_Decl;
21362
21363 --------------------------------------
21364 -- Process_Real_Range_Specification --
21365 --------------------------------------
21366
21367 procedure Process_Real_Range_Specification (Def : Node_Id) is
21368 Spec : constant Node_Id := Real_Range_Specification (Def);
21369 Lo : Node_Id;
21370 Hi : Node_Id;
21371 Err : Boolean := False;
21372
21373 procedure Analyze_Bound (N : Node_Id);
21374 -- Analyze and check one bound
21375
21376 -------------------
21377 -- Analyze_Bound --
21378 -------------------
21379
21380 procedure Analyze_Bound (N : Node_Id) is
21381 begin
21382 Analyze_And_Resolve (N, Any_Real);
21383
21384 if not Is_OK_Static_Expression (N) then
21385 Flag_Non_Static_Expr
21386 ("bound in real type definition is not static!", N);
21387 Err := True;
21388 end if;
21389 end Analyze_Bound;
21390
21391 -- Start of processing for Process_Real_Range_Specification
21392
21393 begin
21394 if Present (Spec) then
21395 Lo := Low_Bound (Spec);
21396 Hi := High_Bound (Spec);
21397 Analyze_Bound (Lo);
21398 Analyze_Bound (Hi);
21399
21400 -- If error, clear away junk range specification
21401
21402 if Err then
21403 Set_Real_Range_Specification (Def, Empty);
21404 end if;
21405 end if;
21406 end Process_Real_Range_Specification;
21407
21408 ---------------------
21409 -- Process_Subtype --
21410 ---------------------
21411
21412 function Process_Subtype
21413 (S : Node_Id;
21414 Related_Nod : Node_Id;
21415 Related_Id : Entity_Id := Empty;
21416 Suffix : Character := ' ') return Entity_Id
21417 is
21418 P : Node_Id;
21419 Def_Id : Entity_Id;
21420 Error_Node : Node_Id;
21421 Full_View_Id : Entity_Id;
21422 Subtype_Mark_Id : Entity_Id;
21423
21424 May_Have_Null_Exclusion : Boolean;
21425
21426 procedure Check_Incomplete (T : Node_Id);
21427 -- Called to verify that an incomplete type is not used prematurely
21428
21429 ----------------------
21430 -- Check_Incomplete --
21431 ----------------------
21432
21433 procedure Check_Incomplete (T : Node_Id) is
21434 begin
21435 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21436
21437 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21438 and then
21439 not (Ada_Version >= Ada_2005
21440 and then
21441 (Nkind (Parent (T)) = N_Subtype_Declaration
21442 or else (Nkind (Parent (T)) = N_Subtype_Indication
21443 and then Nkind (Parent (Parent (T))) =
21444 N_Subtype_Declaration)))
21445 then
21446 Error_Msg_N ("invalid use of type before its full declaration", T);
21447 end if;
21448 end Check_Incomplete;
21449
21450 -- Start of processing for Process_Subtype
21451
21452 begin
21453 -- Case of no constraints present
21454
21455 if Nkind (S) /= N_Subtype_Indication then
21456 Find_Type (S);
21457
21458 -- No way to proceed if the subtype indication is malformed. This
21459 -- will happen for example when the subtype indication in an object
21460 -- declaration is missing altogether and the expression is analyzed
21461 -- as if it were that indication.
21462
21463 if not Is_Entity_Name (S) then
21464 return Any_Type;
21465 end if;
21466
21467 Check_Incomplete (S);
21468 P := Parent (S);
21469
21470 -- Ada 2005 (AI-231): Static check
21471
21472 if Ada_Version >= Ada_2005
21473 and then Present (P)
21474 and then Null_Exclusion_Present (P)
21475 and then Nkind (P) /= N_Access_To_Object_Definition
21476 and then not Is_Access_Type (Entity (S))
21477 then
21478 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21479 end if;
21480
21481 -- The following is ugly, can't we have a range or even a flag???
21482
21483 May_Have_Null_Exclusion :=
21484 Nkind_In (P, N_Access_Definition,
21485 N_Access_Function_Definition,
21486 N_Access_Procedure_Definition,
21487 N_Access_To_Object_Definition,
21488 N_Allocator,
21489 N_Component_Definition)
21490 or else
21491 Nkind_In (P, N_Derived_Type_Definition,
21492 N_Discriminant_Specification,
21493 N_Formal_Object_Declaration,
21494 N_Object_Declaration,
21495 N_Object_Renaming_Declaration,
21496 N_Parameter_Specification,
21497 N_Subtype_Declaration);
21498
21499 -- Create an Itype that is a duplicate of Entity (S) but with the
21500 -- null-exclusion attribute.
21501
21502 if May_Have_Null_Exclusion
21503 and then Is_Access_Type (Entity (S))
21504 and then Null_Exclusion_Present (P)
21505
21506 -- No need to check the case of an access to object definition.
21507 -- It is correct to define double not-null pointers.
21508
21509 -- Example:
21510 -- type Not_Null_Int_Ptr is not null access Integer;
21511 -- type Acc is not null access Not_Null_Int_Ptr;
21512
21513 and then Nkind (P) /= N_Access_To_Object_Definition
21514 then
21515 if Can_Never_Be_Null (Entity (S)) then
21516 case Nkind (Related_Nod) is
21517 when N_Full_Type_Declaration =>
21518 if Nkind (Type_Definition (Related_Nod))
21519 in N_Array_Type_Definition
21520 then
21521 Error_Node :=
21522 Subtype_Indication
21523 (Component_Definition
21524 (Type_Definition (Related_Nod)));
21525 else
21526 Error_Node :=
21527 Subtype_Indication (Type_Definition (Related_Nod));
21528 end if;
21529
21530 when N_Subtype_Declaration =>
21531 Error_Node := Subtype_Indication (Related_Nod);
21532
21533 when N_Object_Declaration =>
21534 Error_Node := Object_Definition (Related_Nod);
21535
21536 when N_Component_Declaration =>
21537 Error_Node :=
21538 Subtype_Indication (Component_Definition (Related_Nod));
21539
21540 when N_Allocator =>
21541 Error_Node := Expression (Related_Nod);
21542
21543 when others =>
21544 pragma Assert (False);
21545 Error_Node := Related_Nod;
21546 end case;
21547
21548 Error_Msg_NE
21549 ("`NOT NULL` not allowed (& already excludes null)",
21550 Error_Node,
21551 Entity (S));
21552 end if;
21553
21554 Set_Etype (S,
21555 Create_Null_Excluding_Itype
21556 (T => Entity (S),
21557 Related_Nod => P));
21558 Set_Entity (S, Etype (S));
21559 end if;
21560
21561 return Entity (S);
21562
21563 -- Case of constraint present, so that we have an N_Subtype_Indication
21564 -- node (this node is created only if constraints are present).
21565
21566 else
21567 Find_Type (Subtype_Mark (S));
21568
21569 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21570 and then not
21571 (Nkind (Parent (S)) = N_Subtype_Declaration
21572 and then Is_Itype (Defining_Identifier (Parent (S))))
21573 then
21574 Check_Incomplete (Subtype_Mark (S));
21575 end if;
21576
21577 P := Parent (S);
21578 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21579
21580 -- Explicit subtype declaration case
21581
21582 if Nkind (P) = N_Subtype_Declaration then
21583 Def_Id := Defining_Identifier (P);
21584
21585 -- Explicit derived type definition case
21586
21587 elsif Nkind (P) = N_Derived_Type_Definition then
21588 Def_Id := Defining_Identifier (Parent (P));
21589
21590 -- Implicit case, the Def_Id must be created as an implicit type.
21591 -- The one exception arises in the case of concurrent types, array
21592 -- and access types, where other subsidiary implicit types may be
21593 -- created and must appear before the main implicit type. In these
21594 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21595 -- has not yet been called to create Def_Id.
21596
21597 else
21598 if Is_Array_Type (Subtype_Mark_Id)
21599 or else Is_Concurrent_Type (Subtype_Mark_Id)
21600 or else Is_Access_Type (Subtype_Mark_Id)
21601 then
21602 Def_Id := Empty;
21603
21604 -- For the other cases, we create a new unattached Itype,
21605 -- and set the indication to ensure it gets attached later.
21606
21607 else
21608 Def_Id :=
21609 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21610 end if;
21611 end if;
21612
21613 -- If the kind of constraint is invalid for this kind of type,
21614 -- then give an error, and then pretend no constraint was given.
21615
21616 if not Is_Valid_Constraint_Kind
21617 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21618 then
21619 Error_Msg_N
21620 ("incorrect constraint for this kind of type", Constraint (S));
21621
21622 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21623
21624 -- Set Ekind of orphan itype, to prevent cascaded errors
21625
21626 if Present (Def_Id) then
21627 Set_Ekind (Def_Id, Ekind (Any_Type));
21628 end if;
21629
21630 -- Make recursive call, having got rid of the bogus constraint
21631
21632 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21633 end if;
21634
21635 -- Remaining processing depends on type. Select on Base_Type kind to
21636 -- ensure getting to the concrete type kind in the case of a private
21637 -- subtype (needed when only doing semantic analysis).
21638
21639 case Ekind (Base_Type (Subtype_Mark_Id)) is
21640 when Access_Kind =>
21641
21642 -- If this is a constraint on a class-wide type, discard it.
21643 -- There is currently no way to express a partial discriminant
21644 -- constraint on a type with unknown discriminants. This is
21645 -- a pathology that the ACATS wisely decides not to test.
21646
21647 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21648 if Comes_From_Source (S) then
21649 Error_Msg_N
21650 ("constraint on class-wide type ignored??",
21651 Constraint (S));
21652 end if;
21653
21654 if Nkind (P) = N_Subtype_Declaration then
21655 Set_Subtype_Indication (P,
21656 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21657 end if;
21658
21659 return Subtype_Mark_Id;
21660 end if;
21661
21662 Constrain_Access (Def_Id, S, Related_Nod);
21663
21664 if Expander_Active
21665 and then Is_Itype (Designated_Type (Def_Id))
21666 and then Nkind (Related_Nod) = N_Subtype_Declaration
21667 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21668 then
21669 Build_Itype_Reference
21670 (Designated_Type (Def_Id), Related_Nod);
21671 end if;
21672
21673 when Array_Kind =>
21674 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21675
21676 when Decimal_Fixed_Point_Kind =>
21677 Constrain_Decimal (Def_Id, S);
21678
21679 when Enumeration_Kind =>
21680 Constrain_Enumeration (Def_Id, S);
21681
21682 when Ordinary_Fixed_Point_Kind =>
21683 Constrain_Ordinary_Fixed (Def_Id, S);
21684
21685 when Float_Kind =>
21686 Constrain_Float (Def_Id, S);
21687
21688 when Integer_Kind =>
21689 Constrain_Integer (Def_Id, S);
21690
21691 when Class_Wide_Kind
21692 | E_Incomplete_Type
21693 | E_Record_Subtype
21694 | E_Record_Type
21695 =>
21696 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21697
21698 if Ekind (Def_Id) = E_Incomplete_Type then
21699 Set_Private_Dependents (Def_Id, New_Elmt_List);
21700 end if;
21701
21702 when Private_Kind =>
21703
21704 -- A private type with unknown discriminants may be completed
21705 -- by an unconstrained array type.
21706
21707 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21708 and then Present (Full_View (Subtype_Mark_Id))
21709 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21710 then
21711 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21712
21713 -- ... but more commonly is completed by a discriminated record
21714 -- type.
21715
21716 else
21717 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21718 end if;
21719
21720 -- The base type may be private but Def_Id may be a full view
21721 -- in an instance.
21722
21723 if Is_Private_Type (Def_Id) then
21724 Set_Private_Dependents (Def_Id, New_Elmt_List);
21725 end if;
21726
21727 -- In case of an invalid constraint prevent further processing
21728 -- since the type constructed is missing expected fields.
21729
21730 if Etype (Def_Id) = Any_Type then
21731 return Def_Id;
21732 end if;
21733
21734 -- If the full view is that of a task with discriminants,
21735 -- we must constrain both the concurrent type and its
21736 -- corresponding record type. Otherwise we will just propagate
21737 -- the constraint to the full view, if available.
21738
21739 if Present (Full_View (Subtype_Mark_Id))
21740 and then Has_Discriminants (Subtype_Mark_Id)
21741 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21742 then
21743 Full_View_Id :=
21744 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21745
21746 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21747 Constrain_Concurrent (Full_View_Id, S,
21748 Related_Nod, Related_Id, Suffix);
21749 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21750 Set_Full_View (Def_Id, Full_View_Id);
21751
21752 -- Introduce an explicit reference to the private subtype,
21753 -- to prevent scope anomalies in gigi if first use appears
21754 -- in a nested context, e.g. a later function body.
21755 -- Should this be generated in other contexts than a full
21756 -- type declaration?
21757
21758 if Is_Itype (Def_Id)
21759 and then
21760 Nkind (Parent (P)) = N_Full_Type_Declaration
21761 then
21762 Build_Itype_Reference (Def_Id, Parent (P));
21763 end if;
21764
21765 else
21766 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21767 end if;
21768
21769 when Concurrent_Kind =>
21770 Constrain_Concurrent (Def_Id, S,
21771 Related_Nod, Related_Id, Suffix);
21772
21773 when others =>
21774 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21775 end case;
21776
21777 -- Size, Alignment, Representation aspects and Convention are always
21778 -- inherited from the base type.
21779
21780 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21781 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21782 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21783
21784 -- The anonymous subtype created for the subtype indication
21785 -- inherits the predicates of the parent.
21786
21787 if Has_Predicates (Subtype_Mark_Id) then
21788 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21789
21790 -- Indicate where the predicate function may be found
21791
21792 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21793 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21794 end if;
21795 end if;
21796
21797 return Def_Id;
21798 end if;
21799 end Process_Subtype;
21800
21801 -----------------------------
21802 -- Record_Type_Declaration --
21803 -----------------------------
21804
21805 procedure Record_Type_Declaration
21806 (T : Entity_Id;
21807 N : Node_Id;
21808 Prev : Entity_Id)
21809 is
21810 Def : constant Node_Id := Type_Definition (N);
21811 Is_Tagged : Boolean;
21812 Tag_Comp : Entity_Id;
21813
21814 begin
21815 -- These flags must be initialized before calling Process_Discriminants
21816 -- because this routine makes use of them.
21817
21818 Set_Ekind (T, E_Record_Type);
21819 Set_Etype (T, T);
21820 Init_Size_Align (T);
21821 Set_Interfaces (T, No_Elist);
21822 Set_Stored_Constraint (T, No_Elist);
21823 Set_Default_SSO (T);
21824 Set_No_Reordering (T, No_Component_Reordering);
21825
21826 -- Normal case
21827
21828 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21829 if Limited_Present (Def) then
21830 Check_SPARK_05_Restriction ("limited is not allowed", N);
21831 end if;
21832
21833 if Abstract_Present (Def) then
21834 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21835 end if;
21836
21837 -- The flag Is_Tagged_Type might have already been set by
21838 -- Find_Type_Name if it detected an error for declaration T. This
21839 -- arises in the case of private tagged types where the full view
21840 -- omits the word tagged.
21841
21842 Is_Tagged :=
21843 Tagged_Present (Def)
21844 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21845
21846 Set_Is_Limited_Record (T, Limited_Present (Def));
21847
21848 if Is_Tagged then
21849 Set_Is_Tagged_Type (T, True);
21850 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21851 end if;
21852
21853 -- Type is abstract if full declaration carries keyword, or if
21854 -- previous partial view did.
21855
21856 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21857 or else Abstract_Present (Def));
21858
21859 else
21860 Check_SPARK_05_Restriction ("interface is not allowed", N);
21861
21862 Is_Tagged := True;
21863 Analyze_Interface_Declaration (T, Def);
21864
21865 if Present (Discriminant_Specifications (N)) then
21866 Error_Msg_N
21867 ("interface types cannot have discriminants",
21868 Defining_Identifier
21869 (First (Discriminant_Specifications (N))));
21870 end if;
21871 end if;
21872
21873 -- First pass: if there are self-referential access components,
21874 -- create the required anonymous access type declarations, and if
21875 -- need be an incomplete type declaration for T itself.
21876
21877 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21878
21879 if Ada_Version >= Ada_2005
21880 and then Present (Interface_List (Def))
21881 then
21882 Check_Interfaces (N, Def);
21883
21884 declare
21885 Ifaces_List : Elist_Id;
21886
21887 begin
21888 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21889 -- already in the parents.
21890
21891 Collect_Interfaces
21892 (T => T,
21893 Ifaces_List => Ifaces_List,
21894 Exclude_Parents => True);
21895
21896 Set_Interfaces (T, Ifaces_List);
21897 end;
21898 end if;
21899
21900 -- Records constitute a scope for the component declarations within.
21901 -- The scope is created prior to the processing of these declarations.
21902 -- Discriminants are processed first, so that they are visible when
21903 -- processing the other components. The Ekind of the record type itself
21904 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21905
21906 -- Enter record scope
21907
21908 Push_Scope (T);
21909
21910 -- If an incomplete or private type declaration was already given for
21911 -- the type, then this scope already exists, and the discriminants have
21912 -- been declared within. We must verify that the full declaration
21913 -- matches the incomplete one.
21914
21915 Check_Or_Process_Discriminants (N, T, Prev);
21916
21917 Set_Is_Constrained (T, not Has_Discriminants (T));
21918 Set_Has_Delayed_Freeze (T, True);
21919
21920 -- For tagged types add a manually analyzed component corresponding
21921 -- to the component _tag, the corresponding piece of tree will be
21922 -- expanded as part of the freezing actions if it is not a CPP_Class.
21923
21924 if Is_Tagged then
21925
21926 -- Do not add the tag unless we are in expansion mode
21927
21928 if Expander_Active then
21929 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21930 Enter_Name (Tag_Comp);
21931
21932 Set_Ekind (Tag_Comp, E_Component);
21933 Set_Is_Tag (Tag_Comp);
21934 Set_Is_Aliased (Tag_Comp);
21935 Set_Etype (Tag_Comp, RTE (RE_Tag));
21936 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21937 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21938 Init_Component_Location (Tag_Comp);
21939
21940 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21941 -- implemented interfaces.
21942
21943 if Has_Interfaces (T) then
21944 Add_Interface_Tag_Components (N, T);
21945 end if;
21946 end if;
21947
21948 Make_Class_Wide_Type (T);
21949 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21950 end if;
21951
21952 -- We must suppress range checks when processing record components in
21953 -- the presence of discriminants, since we don't want spurious checks to
21954 -- be generated during their analysis, but Suppress_Range_Checks flags
21955 -- must be reset the after processing the record definition.
21956
21957 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21958 -- couldn't we just use the normal range check suppression method here.
21959 -- That would seem cleaner ???
21960
21961 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21962 Set_Kill_Range_Checks (T, True);
21963 Record_Type_Definition (Def, Prev);
21964 Set_Kill_Range_Checks (T, False);
21965 else
21966 Record_Type_Definition (Def, Prev);
21967 end if;
21968
21969 -- Exit from record scope
21970
21971 End_Scope;
21972
21973 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21974 -- the implemented interfaces and associate them an aliased entity.
21975
21976 if Is_Tagged
21977 and then not Is_Empty_List (Interface_List (Def))
21978 then
21979 Derive_Progenitor_Subprograms (T, T);
21980 end if;
21981
21982 Check_Function_Writable_Actuals (N);
21983 end Record_Type_Declaration;
21984
21985 ----------------------------
21986 -- Record_Type_Definition --
21987 ----------------------------
21988
21989 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21990 Component : Entity_Id;
21991 Ctrl_Components : Boolean := False;
21992 Final_Storage_Only : Boolean;
21993 T : Entity_Id;
21994
21995 begin
21996 if Ekind (Prev_T) = E_Incomplete_Type then
21997 T := Full_View (Prev_T);
21998 else
21999 T := Prev_T;
22000 end if;
22001
22002 -- In SPARK, tagged types and type extensions may only be declared in
22003 -- the specification of library unit packages.
22004
22005 if Present (Def) and then Is_Tagged_Type (T) then
22006 declare
22007 Typ : Node_Id;
22008 Ctxt : Node_Id;
22009
22010 begin
22011 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22012 Typ := Parent (Def);
22013 else
22014 pragma Assert
22015 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22016 Typ := Parent (Parent (Def));
22017 end if;
22018
22019 Ctxt := Parent (Typ);
22020
22021 if Nkind (Ctxt) = N_Package_Body
22022 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22023 then
22024 Check_SPARK_05_Restriction
22025 ("type should be defined in package specification", Typ);
22026
22027 elsif Nkind (Ctxt) /= N_Package_Specification
22028 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22029 then
22030 Check_SPARK_05_Restriction
22031 ("type should be defined in library unit package", Typ);
22032 end if;
22033 end;
22034 end if;
22035
22036 Final_Storage_Only := not Is_Controlled (T);
22037
22038 -- Ada 2005: Check whether an explicit Limited is present in a derived
22039 -- type declaration.
22040
22041 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22042 and then Limited_Present (Parent (Def))
22043 then
22044 Set_Is_Limited_Record (T);
22045 end if;
22046
22047 -- If the component list of a record type is defined by the reserved
22048 -- word null and there is no discriminant part, then the record type has
22049 -- no components and all records of the type are null records (RM 3.7)
22050 -- This procedure is also called to process the extension part of a
22051 -- record extension, in which case the current scope may have inherited
22052 -- components.
22053
22054 if No (Def)
22055 or else No (Component_List (Def))
22056 or else Null_Present (Component_List (Def))
22057 then
22058 if not Is_Tagged_Type (T) then
22059 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22060 end if;
22061
22062 else
22063 Analyze_Declarations (Component_Items (Component_List (Def)));
22064
22065 if Present (Variant_Part (Component_List (Def))) then
22066 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22067 Analyze (Variant_Part (Component_List (Def)));
22068 end if;
22069 end if;
22070
22071 -- After completing the semantic analysis of the record definition,
22072 -- record components, both new and inherited, are accessible. Set their
22073 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22074 -- whose Ekind may be void.
22075
22076 Component := First_Entity (Current_Scope);
22077 while Present (Component) loop
22078 if Ekind (Component) = E_Void
22079 and then not Is_Itype (Component)
22080 then
22081 Set_Ekind (Component, E_Component);
22082 Init_Component_Location (Component);
22083 end if;
22084
22085 Propagate_Concurrent_Flags (T, Etype (Component));
22086
22087 if Ekind (Component) /= E_Component then
22088 null;
22089
22090 -- Do not set Has_Controlled_Component on a class-wide equivalent
22091 -- type. See Make_CW_Equivalent_Type.
22092
22093 elsif not Is_Class_Wide_Equivalent_Type (T)
22094 and then (Has_Controlled_Component (Etype (Component))
22095 or else (Chars (Component) /= Name_uParent
22096 and then Is_Controlled (Etype (Component))))
22097 then
22098 Set_Has_Controlled_Component (T, True);
22099 Final_Storage_Only :=
22100 Final_Storage_Only
22101 and then Finalize_Storage_Only (Etype (Component));
22102 Ctrl_Components := True;
22103 end if;
22104
22105 Next_Entity (Component);
22106 end loop;
22107
22108 -- A Type is Finalize_Storage_Only only if all its controlled components
22109 -- are also.
22110
22111 if Ctrl_Components then
22112 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22113 end if;
22114
22115 -- Place reference to end record on the proper entity, which may
22116 -- be a partial view.
22117
22118 if Present (Def) then
22119 Process_End_Label (Def, 'e', Prev_T);
22120 end if;
22121 end Record_Type_Definition;
22122
22123 ------------------------
22124 -- Replace_Components --
22125 ------------------------
22126
22127 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
22128 function Process (N : Node_Id) return Traverse_Result;
22129
22130 -------------
22131 -- Process --
22132 -------------
22133
22134 function Process (N : Node_Id) return Traverse_Result is
22135 Comp : Entity_Id;
22136
22137 begin
22138 if Nkind (N) = N_Discriminant_Specification then
22139 Comp := First_Discriminant (Typ);
22140 while Present (Comp) loop
22141 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22142 Set_Defining_Identifier (N, Comp);
22143 exit;
22144 end if;
22145
22146 Next_Discriminant (Comp);
22147 end loop;
22148
22149 elsif Nkind (N) = N_Variant_Part then
22150 Comp := First_Discriminant (Typ);
22151 while Present (Comp) loop
22152 if Chars (Comp) = Chars (Name (N)) then
22153 Set_Entity (Name (N), Comp);
22154 exit;
22155 end if;
22156
22157 Next_Discriminant (Comp);
22158 end loop;
22159
22160 elsif Nkind (N) = N_Component_Declaration then
22161 Comp := First_Component (Typ);
22162 while Present (Comp) loop
22163 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22164 Set_Defining_Identifier (N, Comp);
22165 exit;
22166 end if;
22167
22168 Next_Component (Comp);
22169 end loop;
22170 end if;
22171
22172 return OK;
22173 end Process;
22174
22175 procedure Replace is new Traverse_Proc (Process);
22176
22177 -- Start of processing for Replace_Components
22178
22179 begin
22180 Replace (Decl);
22181 end Replace_Components;
22182
22183 -------------------------------
22184 -- Set_Completion_Referenced --
22185 -------------------------------
22186
22187 procedure Set_Completion_Referenced (E : Entity_Id) is
22188 begin
22189 -- If in main unit, mark entity that is a completion as referenced,
22190 -- warnings go on the partial view when needed.
22191
22192 if In_Extended_Main_Source_Unit (E) then
22193 Set_Referenced (E);
22194 end if;
22195 end Set_Completion_Referenced;
22196
22197 ---------------------
22198 -- Set_Default_SSO --
22199 ---------------------
22200
22201 procedure Set_Default_SSO (T : Entity_Id) is
22202 begin
22203 case Opt.Default_SSO is
22204 when ' ' =>
22205 null;
22206 when 'L' =>
22207 Set_SSO_Set_Low_By_Default (T, True);
22208 when 'H' =>
22209 Set_SSO_Set_High_By_Default (T, True);
22210 when others =>
22211 raise Program_Error;
22212 end case;
22213 end Set_Default_SSO;
22214
22215 ---------------------
22216 -- Set_Fixed_Range --
22217 ---------------------
22218
22219 -- The range for fixed-point types is complicated by the fact that we
22220 -- do not know the exact end points at the time of the declaration. This
22221 -- is true for three reasons:
22222
22223 -- A size clause may affect the fudging of the end-points.
22224 -- A small clause may affect the values of the end-points.
22225 -- We try to include the end-points if it does not affect the size.
22226
22227 -- This means that the actual end-points must be established at the
22228 -- point when the type is frozen. Meanwhile, we first narrow the range
22229 -- as permitted (so that it will fit if necessary in a small specified
22230 -- size), and then build a range subtree with these narrowed bounds.
22231 -- Set_Fixed_Range constructs the range from real literal values, and
22232 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22233
22234 -- The parent of this range is set to point to the entity so that it is
22235 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22236 -- other scalar types, which are just pointers to the range in the
22237 -- original tree, this would otherwise be an orphan).
22238
22239 -- The tree is left unanalyzed. When the type is frozen, the processing
22240 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22241 -- analyzed, and uses this as an indication that it should complete
22242 -- work on the range (it will know the final small and size values).
22243
22244 procedure Set_Fixed_Range
22245 (E : Entity_Id;
22246 Loc : Source_Ptr;
22247 Lo : Ureal;
22248 Hi : Ureal)
22249 is
22250 S : constant Node_Id :=
22251 Make_Range (Loc,
22252 Low_Bound => Make_Real_Literal (Loc, Lo),
22253 High_Bound => Make_Real_Literal (Loc, Hi));
22254 begin
22255 Set_Scalar_Range (E, S);
22256 Set_Parent (S, E);
22257
22258 -- Before the freeze point, the bounds of a fixed point are universal
22259 -- and carry the corresponding type.
22260
22261 Set_Etype (Low_Bound (S), Universal_Real);
22262 Set_Etype (High_Bound (S), Universal_Real);
22263 end Set_Fixed_Range;
22264
22265 ----------------------------------
22266 -- Set_Scalar_Range_For_Subtype --
22267 ----------------------------------
22268
22269 procedure Set_Scalar_Range_For_Subtype
22270 (Def_Id : Entity_Id;
22271 R : Node_Id;
22272 Subt : Entity_Id)
22273 is
22274 Kind : constant Entity_Kind := Ekind (Def_Id);
22275
22276 begin
22277 -- Defend against previous error
22278
22279 if Nkind (R) = N_Error then
22280 return;
22281 end if;
22282
22283 Set_Scalar_Range (Def_Id, R);
22284
22285 -- We need to link the range into the tree before resolving it so
22286 -- that types that are referenced, including importantly the subtype
22287 -- itself, are properly frozen (Freeze_Expression requires that the
22288 -- expression be properly linked into the tree). Of course if it is
22289 -- already linked in, then we do not disturb the current link.
22290
22291 if No (Parent (R)) then
22292 Set_Parent (R, Def_Id);
22293 end if;
22294
22295 -- Reset the kind of the subtype during analysis of the range, to
22296 -- catch possible premature use in the bounds themselves.
22297
22298 Set_Ekind (Def_Id, E_Void);
22299 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22300 Set_Ekind (Def_Id, Kind);
22301 end Set_Scalar_Range_For_Subtype;
22302
22303 --------------------------------------------------------
22304 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22305 --------------------------------------------------------
22306
22307 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22308 (E : Entity_Id)
22309 is
22310 begin
22311 -- Make sure set if encountered during Expand_To_Stored_Constraint
22312
22313 Set_Stored_Constraint (E, No_Elist);
22314
22315 -- Give it the right value
22316
22317 if Is_Constrained (E) and then Has_Discriminants (E) then
22318 Set_Stored_Constraint (E,
22319 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22320 end if;
22321 end Set_Stored_Constraint_From_Discriminant_Constraint;
22322
22323 -------------------------------------
22324 -- Signed_Integer_Type_Declaration --
22325 -------------------------------------
22326
22327 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22328 Implicit_Base : Entity_Id;
22329 Base_Typ : Entity_Id;
22330 Lo_Val : Uint;
22331 Hi_Val : Uint;
22332 Errs : Boolean := False;
22333 Lo : Node_Id;
22334 Hi : Node_Id;
22335
22336 function Can_Derive_From (E : Entity_Id) return Boolean;
22337 -- Determine whether given bounds allow derivation from specified type
22338
22339 procedure Check_Bound (Expr : Node_Id);
22340 -- Check bound to make sure it is integral and static. If not, post
22341 -- appropriate error message and set Errs flag
22342
22343 ---------------------
22344 -- Can_Derive_From --
22345 ---------------------
22346
22347 -- Note we check both bounds against both end values, to deal with
22348 -- strange types like ones with a range of 0 .. -12341234.
22349
22350 function Can_Derive_From (E : Entity_Id) return Boolean is
22351 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22352 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22353 begin
22354 return Lo <= Lo_Val and then Lo_Val <= Hi
22355 and then
22356 Lo <= Hi_Val and then Hi_Val <= Hi;
22357 end Can_Derive_From;
22358
22359 -----------------
22360 -- Check_Bound --
22361 -----------------
22362
22363 procedure Check_Bound (Expr : Node_Id) is
22364 begin
22365 -- If a range constraint is used as an integer type definition, each
22366 -- bound of the range must be defined by a static expression of some
22367 -- integer type, but the two bounds need not have the same integer
22368 -- type (Negative bounds are allowed.) (RM 3.5.4)
22369
22370 if not Is_Integer_Type (Etype (Expr)) then
22371 Error_Msg_N
22372 ("integer type definition bounds must be of integer type", Expr);
22373 Errs := True;
22374
22375 elsif not Is_OK_Static_Expression (Expr) then
22376 Flag_Non_Static_Expr
22377 ("non-static expression used for integer type bound!", Expr);
22378 Errs := True;
22379
22380 -- The bounds are folded into literals, and we set their type to be
22381 -- universal, to avoid typing difficulties: we cannot set the type
22382 -- of the literal to the new type, because this would be a forward
22383 -- reference for the back end, and if the original type is user-
22384 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22385
22386 else
22387 if Is_Entity_Name (Expr) then
22388 Fold_Uint (Expr, Expr_Value (Expr), True);
22389 end if;
22390
22391 Set_Etype (Expr, Universal_Integer);
22392 end if;
22393 end Check_Bound;
22394
22395 -- Start of processing for Signed_Integer_Type_Declaration
22396
22397 begin
22398 -- Create an anonymous base type
22399
22400 Implicit_Base :=
22401 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22402
22403 -- Analyze and check the bounds, they can be of any integer type
22404
22405 Lo := Low_Bound (Def);
22406 Hi := High_Bound (Def);
22407
22408 -- Arbitrarily use Integer as the type if either bound had an error
22409
22410 if Hi = Error or else Lo = Error then
22411 Base_Typ := Any_Integer;
22412 Set_Error_Posted (T, True);
22413
22414 -- Here both bounds are OK expressions
22415
22416 else
22417 Analyze_And_Resolve (Lo, Any_Integer);
22418 Analyze_And_Resolve (Hi, Any_Integer);
22419
22420 Check_Bound (Lo);
22421 Check_Bound (Hi);
22422
22423 if Errs then
22424 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22425 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22426 end if;
22427
22428 -- Find type to derive from
22429
22430 Lo_Val := Expr_Value (Lo);
22431 Hi_Val := Expr_Value (Hi);
22432
22433 if Can_Derive_From (Standard_Short_Short_Integer) then
22434 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22435
22436 elsif Can_Derive_From (Standard_Short_Integer) then
22437 Base_Typ := Base_Type (Standard_Short_Integer);
22438
22439 elsif Can_Derive_From (Standard_Integer) then
22440 Base_Typ := Base_Type (Standard_Integer);
22441
22442 elsif Can_Derive_From (Standard_Long_Integer) then
22443 Base_Typ := Base_Type (Standard_Long_Integer);
22444
22445 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22446 Check_Restriction (No_Long_Long_Integers, Def);
22447 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22448
22449 else
22450 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22451 Error_Msg_N ("integer type definition bounds out of range", Def);
22452 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22453 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22454 end if;
22455 end if;
22456
22457 -- Complete both implicit base and declared first subtype entities. The
22458 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22459 -- are not clobbered when the signed integer type acts as a full view of
22460 -- a private type.
22461
22462 Set_Etype (Implicit_Base, Base_Typ);
22463 Set_Size_Info (Implicit_Base, Base_Typ);
22464 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22465 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22466 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22467
22468 Set_Ekind (T, E_Signed_Integer_Subtype);
22469 Set_Etype (T, Implicit_Base);
22470 Set_Size_Info (T, Implicit_Base);
22471 Inherit_Rep_Item_Chain (T, Implicit_Base);
22472 Set_Scalar_Range (T, Def);
22473 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22474 Set_Is_Constrained (T);
22475 end Signed_Integer_Type_Declaration;
22476
22477 end Sem_Ch3;