[Ada] Minor reformatting
[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,
1303 not Always_Compatible_Rep_On_Target);
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, and setting its
14634 -- scope to the given subtype. This turns out to be useful for the
14635 -- LLVM code generator, but that scope is not used otherwise.
14636
14637 Enter_Name (New_Compon);
14638 Set_Scope (New_Compon, Subt);
14639
14640 return New_Compon;
14641 end Create_Component;
14642
14643 -----------------------
14644 -- Is_Variant_Record --
14645 -----------------------
14646
14647 function Is_Variant_Record (T : Entity_Id) return Boolean is
14648 begin
14649 return Nkind (Parent (T)) = N_Full_Type_Declaration
14650 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14651 and then Present (Component_List (Type_Definition (Parent (T))))
14652 and then
14653 Present
14654 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14655 end Is_Variant_Record;
14656
14657 -- Start of processing for Create_Constrained_Components
14658
14659 begin
14660 pragma Assert (Subt /= Base_Type (Subt));
14661 pragma Assert (Typ = Base_Type (Typ));
14662
14663 Set_First_Entity (Subt, Empty);
14664 Set_Last_Entity (Subt, Empty);
14665
14666 -- Check whether constraint is fully static, in which case we can
14667 -- optimize the list of components.
14668
14669 Discr_Val := First_Elmt (Constraints);
14670 while Present (Discr_Val) loop
14671 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14672 Is_Static := False;
14673 exit;
14674 end if;
14675
14676 Next_Elmt (Discr_Val);
14677 end loop;
14678
14679 Set_Has_Static_Discriminants (Subt, Is_Static);
14680
14681 Push_Scope (Subt);
14682
14683 -- Inherit the discriminants of the parent type
14684
14685 Add_Discriminants : declare
14686 Num_Disc : Nat;
14687 Num_Gird : Nat;
14688
14689 begin
14690 Num_Disc := 0;
14691 Old_C := First_Discriminant (Typ);
14692
14693 while Present (Old_C) loop
14694 Num_Disc := Num_Disc + 1;
14695 New_C := Create_Component (Old_C);
14696 Set_Is_Public (New_C, Is_Public (Subt));
14697 Next_Discriminant (Old_C);
14698 end loop;
14699
14700 -- For an untagged derived subtype, the number of discriminants may
14701 -- be smaller than the number of inherited discriminants, because
14702 -- several of them may be renamed by a single new discriminant or
14703 -- constrained. In this case, add the hidden discriminants back into
14704 -- the subtype, because they need to be present if the optimizer of
14705 -- the GCC 4.x back-end decides to break apart assignments between
14706 -- objects using the parent view into member-wise assignments.
14707
14708 Num_Gird := 0;
14709
14710 if Is_Derived_Type (Typ)
14711 and then not Is_Tagged_Type (Typ)
14712 then
14713 Old_C := First_Stored_Discriminant (Typ);
14714
14715 while Present (Old_C) loop
14716 Num_Gird := Num_Gird + 1;
14717 Next_Stored_Discriminant (Old_C);
14718 end loop;
14719 end if;
14720
14721 if Num_Gird > Num_Disc then
14722
14723 -- Find out multiple uses of new discriminants, and add hidden
14724 -- components for the extra renamed discriminants. We recognize
14725 -- multiple uses through the Corresponding_Discriminant of a
14726 -- new discriminant: if it constrains several old discriminants,
14727 -- this field points to the last one in the parent type. The
14728 -- stored discriminants of the derived type have the same name
14729 -- as those of the parent.
14730
14731 declare
14732 Constr : Elmt_Id;
14733 New_Discr : Entity_Id;
14734 Old_Discr : Entity_Id;
14735
14736 begin
14737 Constr := First_Elmt (Stored_Constraint (Typ));
14738 Old_Discr := First_Stored_Discriminant (Typ);
14739 while Present (Constr) loop
14740 if Is_Entity_Name (Node (Constr))
14741 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14742 then
14743 New_Discr := Entity (Node (Constr));
14744
14745 if Chars (Corresponding_Discriminant (New_Discr)) /=
14746 Chars (Old_Discr)
14747 then
14748 -- The new discriminant has been used to rename a
14749 -- subsequent old discriminant. Introduce a shadow
14750 -- component for the current old discriminant.
14751
14752 New_C := Create_Component (Old_Discr);
14753 Set_Original_Record_Component (New_C, Old_Discr);
14754 end if;
14755
14756 else
14757 -- The constraint has eliminated the old discriminant.
14758 -- Introduce a shadow component.
14759
14760 New_C := Create_Component (Old_Discr);
14761 Set_Original_Record_Component (New_C, Old_Discr);
14762 end if;
14763
14764 Next_Elmt (Constr);
14765 Next_Stored_Discriminant (Old_Discr);
14766 end loop;
14767 end;
14768 end if;
14769 end Add_Discriminants;
14770
14771 if Is_Static
14772 and then Is_Variant_Record (Typ)
14773 then
14774 Collect_Fixed_Components (Typ);
14775
14776 Gather_Components (
14777 Typ,
14778 Component_List (Type_Definition (Parent (Typ))),
14779 Governed_By => Assoc_List,
14780 Into => Comp_List,
14781 Report_Errors => Errors);
14782 pragma Assert (not Errors
14783 or else Serious_Errors_Detected > 0);
14784
14785 Create_All_Components;
14786
14787 -- If the subtype declaration is created for a tagged type derivation
14788 -- with constraints, we retrieve the record definition of the parent
14789 -- type to select the components of the proper variant.
14790
14791 elsif Is_Static
14792 and then Is_Tagged_Type (Typ)
14793 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14794 and then
14795 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14796 and then Is_Variant_Record (Parent_Type)
14797 then
14798 Collect_Fixed_Components (Typ);
14799
14800 Gather_Components
14801 (Typ,
14802 Component_List (Type_Definition (Parent (Parent_Type))),
14803 Governed_By => Assoc_List,
14804 Into => Comp_List,
14805 Report_Errors => Errors);
14806
14807 -- Note: previously there was a check at this point that no errors
14808 -- were detected. As a consequence of AI05-220 there may be an error
14809 -- if an inherited discriminant that controls a variant has a non-
14810 -- static constraint.
14811
14812 -- If the tagged derivation has a type extension, collect all the
14813 -- new components therein.
14814
14815 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14816 then
14817 Old_C := First_Component (Typ);
14818 while Present (Old_C) loop
14819 if Original_Record_Component (Old_C) = Old_C
14820 and then Chars (Old_C) /= Name_uTag
14821 and then Chars (Old_C) /= Name_uParent
14822 then
14823 Append_Elmt (Old_C, Comp_List);
14824 end if;
14825
14826 Next_Component (Old_C);
14827 end loop;
14828 end if;
14829
14830 Create_All_Components;
14831
14832 else
14833 -- If discriminants are not static, or if this is a multi-level type
14834 -- extension, we have to include all components of the parent type.
14835
14836 Old_C := First_Component (Typ);
14837 while Present (Old_C) loop
14838 New_C := Create_Component (Old_C);
14839
14840 Set_Etype
14841 (New_C,
14842 Constrain_Component_Type
14843 (Old_C, Subt, Decl_Node, Typ, Constraints));
14844 Set_Is_Public (New_C, Is_Public (Subt));
14845
14846 Next_Component (Old_C);
14847 end loop;
14848 end if;
14849
14850 End_Scope;
14851 end Create_Constrained_Components;
14852
14853 ------------------------------------------
14854 -- Decimal_Fixed_Point_Type_Declaration --
14855 ------------------------------------------
14856
14857 procedure Decimal_Fixed_Point_Type_Declaration
14858 (T : Entity_Id;
14859 Def : Node_Id)
14860 is
14861 Loc : constant Source_Ptr := Sloc (Def);
14862 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14863 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14864 Implicit_Base : Entity_Id;
14865 Digs_Val : Uint;
14866 Delta_Val : Ureal;
14867 Scale_Val : Uint;
14868 Bound_Val : Ureal;
14869
14870 begin
14871 Check_SPARK_05_Restriction
14872 ("decimal fixed point type is not allowed", Def);
14873 Check_Restriction (No_Fixed_Point, Def);
14874
14875 -- Create implicit base type
14876
14877 Implicit_Base :=
14878 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14879 Set_Etype (Implicit_Base, Implicit_Base);
14880
14881 -- Analyze and process delta expression
14882
14883 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14884
14885 Check_Delta_Expression (Delta_Expr);
14886 Delta_Val := Expr_Value_R (Delta_Expr);
14887
14888 -- Check delta is power of 10, and determine scale value from it
14889
14890 declare
14891 Val : Ureal;
14892
14893 begin
14894 Scale_Val := Uint_0;
14895 Val := Delta_Val;
14896
14897 if Val < Ureal_1 then
14898 while Val < Ureal_1 loop
14899 Val := Val * Ureal_10;
14900 Scale_Val := Scale_Val + 1;
14901 end loop;
14902
14903 if Scale_Val > 18 then
14904 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14905 Scale_Val := UI_From_Int (+18);
14906 end if;
14907
14908 else
14909 while Val > Ureal_1 loop
14910 Val := Val / Ureal_10;
14911 Scale_Val := Scale_Val - 1;
14912 end loop;
14913
14914 if Scale_Val < -18 then
14915 Error_Msg_N ("scale is less than minimum value of -18", Def);
14916 Scale_Val := UI_From_Int (-18);
14917 end if;
14918 end if;
14919
14920 if Val /= Ureal_1 then
14921 Error_Msg_N ("delta expression must be a power of 10", Def);
14922 Delta_Val := Ureal_10 ** (-Scale_Val);
14923 end if;
14924 end;
14925
14926 -- Set delta, scale and small (small = delta for decimal type)
14927
14928 Set_Delta_Value (Implicit_Base, Delta_Val);
14929 Set_Scale_Value (Implicit_Base, Scale_Val);
14930 Set_Small_Value (Implicit_Base, Delta_Val);
14931
14932 -- Analyze and process digits expression
14933
14934 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14935 Check_Digits_Expression (Digs_Expr);
14936 Digs_Val := Expr_Value (Digs_Expr);
14937
14938 if Digs_Val > 18 then
14939 Digs_Val := UI_From_Int (+18);
14940 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14941 end if;
14942
14943 Set_Digits_Value (Implicit_Base, Digs_Val);
14944 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14945
14946 -- Set range of base type from digits value for now. This will be
14947 -- expanded to represent the true underlying base range by Freeze.
14948
14949 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14950
14951 -- Note: We leave size as zero for now, size will be set at freeze
14952 -- time. We have to do this for ordinary fixed-point, because the size
14953 -- depends on the specified small, and we might as well do the same for
14954 -- decimal fixed-point.
14955
14956 pragma Assert (Esize (Implicit_Base) = Uint_0);
14957
14958 -- If there are bounds given in the declaration use them as the
14959 -- bounds of the first named subtype.
14960
14961 if Present (Real_Range_Specification (Def)) then
14962 declare
14963 RRS : constant Node_Id := Real_Range_Specification (Def);
14964 Low : constant Node_Id := Low_Bound (RRS);
14965 High : constant Node_Id := High_Bound (RRS);
14966 Low_Val : Ureal;
14967 High_Val : Ureal;
14968
14969 begin
14970 Analyze_And_Resolve (Low, Any_Real);
14971 Analyze_And_Resolve (High, Any_Real);
14972 Check_Real_Bound (Low);
14973 Check_Real_Bound (High);
14974 Low_Val := Expr_Value_R (Low);
14975 High_Val := Expr_Value_R (High);
14976
14977 if Low_Val < (-Bound_Val) then
14978 Error_Msg_N
14979 ("range low bound too small for digits value", Low);
14980 Low_Val := -Bound_Val;
14981 end if;
14982
14983 if High_Val > Bound_Val then
14984 Error_Msg_N
14985 ("range high bound too large for digits value", High);
14986 High_Val := Bound_Val;
14987 end if;
14988
14989 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14990 end;
14991
14992 -- If no explicit range, use range that corresponds to given
14993 -- digits value. This will end up as the final range for the
14994 -- first subtype.
14995
14996 else
14997 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14998 end if;
14999
15000 -- Complete entity for first subtype. The inheritance of the rep item
15001 -- chain ensures that SPARK-related pragmas are not clobbered when the
15002 -- decimal fixed point type acts as a full view of a private type.
15003
15004 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15005 Set_Etype (T, Implicit_Base);
15006 Set_Size_Info (T, Implicit_Base);
15007 Inherit_Rep_Item_Chain (T, Implicit_Base);
15008 Set_Digits_Value (T, Digs_Val);
15009 Set_Delta_Value (T, Delta_Val);
15010 Set_Small_Value (T, Delta_Val);
15011 Set_Scale_Value (T, Scale_Val);
15012 Set_Is_Constrained (T);
15013 end Decimal_Fixed_Point_Type_Declaration;
15014
15015 -----------------------------------
15016 -- Derive_Progenitor_Subprograms --
15017 -----------------------------------
15018
15019 procedure Derive_Progenitor_Subprograms
15020 (Parent_Type : Entity_Id;
15021 Tagged_Type : Entity_Id)
15022 is
15023 E : Entity_Id;
15024 Elmt : Elmt_Id;
15025 Iface : Entity_Id;
15026 Iface_Alias : Entity_Id;
15027 Iface_Elmt : Elmt_Id;
15028 Iface_Subp : Entity_Id;
15029 New_Subp : Entity_Id := Empty;
15030 Prim_Elmt : Elmt_Id;
15031 Subp : Entity_Id;
15032 Typ : Entity_Id;
15033
15034 begin
15035 pragma Assert (Ada_Version >= Ada_2005
15036 and then Is_Record_Type (Tagged_Type)
15037 and then Is_Tagged_Type (Tagged_Type)
15038 and then Has_Interfaces (Tagged_Type));
15039
15040 -- Step 1: Transfer to the full-view primitives associated with the
15041 -- partial-view that cover interface primitives. Conceptually this
15042 -- work should be done later by Process_Full_View; done here to
15043 -- simplify its implementation at later stages. It can be safely
15044 -- done here because interfaces must be visible in the partial and
15045 -- private view (RM 7.3(7.3/2)).
15046
15047 -- Small optimization: This work is only required if the parent may
15048 -- have entities whose Alias attribute reference an interface primitive.
15049 -- Such a situation may occur if the parent is an abstract type and the
15050 -- primitive has not been yet overridden or if the parent is a generic
15051 -- formal type covering interfaces.
15052
15053 -- If the tagged type is not abstract, it cannot have abstract
15054 -- primitives (the only entities in the list of primitives of
15055 -- non-abstract tagged types that can reference abstract primitives
15056 -- through its Alias attribute are the internal entities that have
15057 -- attribute Interface_Alias, and these entities are generated later
15058 -- by Add_Internal_Interface_Entities).
15059
15060 if In_Private_Part (Current_Scope)
15061 and then (Is_Abstract_Type (Parent_Type)
15062 or else
15063 Is_Generic_Type (Parent_Type))
15064 then
15065 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15066 while Present (Elmt) loop
15067 Subp := Node (Elmt);
15068
15069 -- At this stage it is not possible to have entities in the list
15070 -- of primitives that have attribute Interface_Alias.
15071
15072 pragma Assert (No (Interface_Alias (Subp)));
15073
15074 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15075
15076 if Is_Interface (Typ) then
15077 E := Find_Primitive_Covering_Interface
15078 (Tagged_Type => Tagged_Type,
15079 Iface_Prim => Subp);
15080
15081 if Present (E)
15082 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15083 then
15084 Replace_Elmt (Elmt, E);
15085 Remove_Homonym (Subp);
15086 end if;
15087 end if;
15088
15089 Next_Elmt (Elmt);
15090 end loop;
15091 end if;
15092
15093 -- Step 2: Add primitives of progenitors that are not implemented by
15094 -- parents of Tagged_Type.
15095
15096 if Present (Interfaces (Base_Type (Tagged_Type))) then
15097 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15098 while Present (Iface_Elmt) loop
15099 Iface := Node (Iface_Elmt);
15100
15101 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15102 while Present (Prim_Elmt) loop
15103 Iface_Subp := Node (Prim_Elmt);
15104 Iface_Alias := Ultimate_Alias (Iface_Subp);
15105
15106 -- Exclude derivation of predefined primitives except those
15107 -- that come from source, or are inherited from one that comes
15108 -- from source. Required to catch declarations of equality
15109 -- operators of interfaces. For example:
15110
15111 -- type Iface is interface;
15112 -- function "=" (Left, Right : Iface) return Boolean;
15113
15114 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15115 or else Comes_From_Source (Iface_Alias)
15116 then
15117 E :=
15118 Find_Primitive_Covering_Interface
15119 (Tagged_Type => Tagged_Type,
15120 Iface_Prim => Iface_Subp);
15121
15122 -- If not found we derive a new primitive leaving its alias
15123 -- attribute referencing the interface primitive.
15124
15125 if No (E) then
15126 Derive_Subprogram
15127 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15128
15129 -- Ada 2012 (AI05-0197): If the covering primitive's name
15130 -- differs from the name of the interface primitive then it
15131 -- is a private primitive inherited from a parent type. In
15132 -- such case, given that Tagged_Type covers the interface,
15133 -- the inherited private primitive becomes visible. For such
15134 -- purpose we add a new entity that renames the inherited
15135 -- private primitive.
15136
15137 elsif Chars (E) /= Chars (Iface_Subp) then
15138 pragma Assert (Has_Suffix (E, 'P'));
15139 Derive_Subprogram
15140 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15141 Set_Alias (New_Subp, E);
15142 Set_Is_Abstract_Subprogram (New_Subp,
15143 Is_Abstract_Subprogram (E));
15144
15145 -- Propagate to the full view interface entities associated
15146 -- with the partial view.
15147
15148 elsif In_Private_Part (Current_Scope)
15149 and then Present (Alias (E))
15150 and then Alias (E) = Iface_Subp
15151 and then
15152 List_Containing (Parent (E)) /=
15153 Private_Declarations
15154 (Specification
15155 (Unit_Declaration_Node (Current_Scope)))
15156 then
15157 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15158 end if;
15159 end if;
15160
15161 Next_Elmt (Prim_Elmt);
15162 end loop;
15163
15164 Next_Elmt (Iface_Elmt);
15165 end loop;
15166 end if;
15167 end Derive_Progenitor_Subprograms;
15168
15169 -----------------------
15170 -- Derive_Subprogram --
15171 -----------------------
15172
15173 procedure Derive_Subprogram
15174 (New_Subp : out Entity_Id;
15175 Parent_Subp : Entity_Id;
15176 Derived_Type : Entity_Id;
15177 Parent_Type : Entity_Id;
15178 Actual_Subp : Entity_Id := Empty)
15179 is
15180 Formal : Entity_Id;
15181 -- Formal parameter of parent primitive operation
15182
15183 Formal_Of_Actual : Entity_Id;
15184 -- Formal parameter of actual operation, when the derivation is to
15185 -- create a renaming for a primitive operation of an actual in an
15186 -- instantiation.
15187
15188 New_Formal : Entity_Id;
15189 -- Formal of inherited operation
15190
15191 Visible_Subp : Entity_Id := Parent_Subp;
15192
15193 function Is_Private_Overriding return Boolean;
15194 -- If Subp is a private overriding of a visible operation, the inherited
15195 -- operation derives from the overridden op (even though its body is the
15196 -- overriding one) and the inherited operation is visible now. See
15197 -- sem_disp to see the full details of the handling of the overridden
15198 -- subprogram, which is removed from the list of primitive operations of
15199 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15200 -- and used to diagnose abstract operations that need overriding in the
15201 -- derived type.
15202
15203 procedure Replace_Type (Id, New_Id : Entity_Id);
15204 -- When the type is an anonymous access type, create a new access type
15205 -- designating the derived type.
15206
15207 procedure Set_Derived_Name;
15208 -- This procedure sets the appropriate Chars name for New_Subp. This
15209 -- is normally just a copy of the parent name. An exception arises for
15210 -- type support subprograms, where the name is changed to reflect the
15211 -- name of the derived type, e.g. if type foo is derived from type bar,
15212 -- then a procedure barDA is derived with a name fooDA.
15213
15214 ---------------------------
15215 -- Is_Private_Overriding --
15216 ---------------------------
15217
15218 function Is_Private_Overriding return Boolean is
15219 Prev : Entity_Id;
15220
15221 begin
15222 -- If the parent is not a dispatching operation there is no
15223 -- need to investigate overridings
15224
15225 if not Is_Dispatching_Operation (Parent_Subp) then
15226 return False;
15227 end if;
15228
15229 -- The visible operation that is overridden is a homonym of the
15230 -- parent subprogram. We scan the homonym chain to find the one
15231 -- whose alias is the subprogram we are deriving.
15232
15233 Prev := Current_Entity (Parent_Subp);
15234 while Present (Prev) loop
15235 if Ekind (Prev) = Ekind (Parent_Subp)
15236 and then Alias (Prev) = Parent_Subp
15237 and then Scope (Parent_Subp) = Scope (Prev)
15238 and then not Is_Hidden (Prev)
15239 then
15240 Visible_Subp := Prev;
15241 return True;
15242 end if;
15243
15244 Prev := Homonym (Prev);
15245 end loop;
15246
15247 return False;
15248 end Is_Private_Overriding;
15249
15250 ------------------
15251 -- Replace_Type --
15252 ------------------
15253
15254 procedure Replace_Type (Id, New_Id : Entity_Id) is
15255 Id_Type : constant Entity_Id := Etype (Id);
15256 Acc_Type : Entity_Id;
15257 Par : constant Node_Id := Parent (Derived_Type);
15258
15259 begin
15260 -- When the type is an anonymous access type, create a new access
15261 -- type designating the derived type. This itype must be elaborated
15262 -- at the point of the derivation, not on subsequent calls that may
15263 -- be out of the proper scope for Gigi, so we insert a reference to
15264 -- it after the derivation.
15265
15266 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15267 declare
15268 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15269
15270 begin
15271 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15272 and then Present (Full_View (Desig_Typ))
15273 and then not Is_Private_Type (Parent_Type)
15274 then
15275 Desig_Typ := Full_View (Desig_Typ);
15276 end if;
15277
15278 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15279
15280 -- Ada 2005 (AI-251): Handle also derivations of abstract
15281 -- interface primitives.
15282
15283 or else (Is_Interface (Desig_Typ)
15284 and then not Is_Class_Wide_Type (Desig_Typ))
15285 then
15286 Acc_Type := New_Copy (Id_Type);
15287 Set_Etype (Acc_Type, Acc_Type);
15288 Set_Scope (Acc_Type, New_Subp);
15289
15290 -- Set size of anonymous access type. If we have an access
15291 -- to an unconstrained array, this is a fat pointer, so it
15292 -- is sizes at twice addtress size.
15293
15294 if Is_Array_Type (Desig_Typ)
15295 and then not Is_Constrained (Desig_Typ)
15296 then
15297 Init_Size (Acc_Type, 2 * System_Address_Size);
15298
15299 -- Other cases use a thin pointer
15300
15301 else
15302 Init_Size (Acc_Type, System_Address_Size);
15303 end if;
15304
15305 -- Set remaining characterstics of anonymous access type
15306
15307 Init_Alignment (Acc_Type);
15308 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15309
15310 Set_Etype (New_Id, Acc_Type);
15311 Set_Scope (New_Id, New_Subp);
15312
15313 -- Create a reference to it
15314
15315 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15316
15317 else
15318 Set_Etype (New_Id, Id_Type);
15319 end if;
15320 end;
15321
15322 -- In Ada2012, a formal may have an incomplete type but the type
15323 -- derivation that inherits the primitive follows the full view.
15324
15325 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15326 or else
15327 (Ekind (Id_Type) = E_Record_Type_With_Private
15328 and then Present (Full_View (Id_Type))
15329 and then
15330 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15331 or else
15332 (Ada_Version >= Ada_2012
15333 and then Ekind (Id_Type) = E_Incomplete_Type
15334 and then Full_View (Id_Type) = Parent_Type)
15335 then
15336 -- Constraint checks on formals are generated during expansion,
15337 -- based on the signature of the original subprogram. The bounds
15338 -- of the derived type are not relevant, and thus we can use
15339 -- the base type for the formals. However, the return type may be
15340 -- used in a context that requires that the proper static bounds
15341 -- be used (a case statement, for example) and for those cases
15342 -- we must use the derived type (first subtype), not its base.
15343
15344 -- If the derived_type_definition has no constraints, we know that
15345 -- the derived type has the same constraints as the first subtype
15346 -- of the parent, and we can also use it rather than its base,
15347 -- which can lead to more efficient code.
15348
15349 if Etype (Id) = Parent_Type then
15350 if Is_Scalar_Type (Parent_Type)
15351 and then
15352 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15353 then
15354 Set_Etype (New_Id, Derived_Type);
15355
15356 elsif Nkind (Par) = N_Full_Type_Declaration
15357 and then
15358 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15359 and then
15360 Is_Entity_Name
15361 (Subtype_Indication (Type_Definition (Par)))
15362 then
15363 Set_Etype (New_Id, Derived_Type);
15364
15365 else
15366 Set_Etype (New_Id, Base_Type (Derived_Type));
15367 end if;
15368
15369 else
15370 Set_Etype (New_Id, Base_Type (Derived_Type));
15371 end if;
15372
15373 else
15374 Set_Etype (New_Id, Etype (Id));
15375 end if;
15376 end Replace_Type;
15377
15378 ----------------------
15379 -- Set_Derived_Name --
15380 ----------------------
15381
15382 procedure Set_Derived_Name is
15383 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15384 begin
15385 if Nm = TSS_Null then
15386 Set_Chars (New_Subp, Chars (Parent_Subp));
15387 else
15388 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15389 end if;
15390 end Set_Derived_Name;
15391
15392 -- Start of processing for Derive_Subprogram
15393
15394 begin
15395 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15396 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15397
15398 -- Check whether the inherited subprogram is a private operation that
15399 -- should be inherited but not yet made visible. Such subprograms can
15400 -- become visible at a later point (e.g., the private part of a public
15401 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15402 -- following predicate is true, then this is not such a private
15403 -- operation and the subprogram simply inherits the name of the parent
15404 -- subprogram. Note the special check for the names of controlled
15405 -- operations, which are currently exempted from being inherited with
15406 -- a hidden name because they must be findable for generation of
15407 -- implicit run-time calls.
15408
15409 if not Is_Hidden (Parent_Subp)
15410 or else Is_Internal (Parent_Subp)
15411 or else Is_Private_Overriding
15412 or else Is_Internal_Name (Chars (Parent_Subp))
15413 or else (Is_Controlled (Parent_Type)
15414 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15415 Name_Finalize,
15416 Name_Initialize))
15417 then
15418 Set_Derived_Name;
15419
15420 -- An inherited dispatching equality will be overridden by an internally
15421 -- generated one, or by an explicit one, so preserve its name and thus
15422 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15423 -- private operation it may become invisible if the full view has
15424 -- progenitors, and the dispatch table will be malformed.
15425 -- We check that the type is limited to handle the anomalous declaration
15426 -- of Limited_Controlled, which is derived from a non-limited type, and
15427 -- which is handled specially elsewhere as well.
15428
15429 elsif Chars (Parent_Subp) = Name_Op_Eq
15430 and then Is_Dispatching_Operation (Parent_Subp)
15431 and then Etype (Parent_Subp) = Standard_Boolean
15432 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15433 and then
15434 Etype (First_Formal (Parent_Subp)) =
15435 Etype (Next_Formal (First_Formal (Parent_Subp)))
15436 then
15437 Set_Derived_Name;
15438
15439 -- If parent is hidden, this can be a regular derivation if the
15440 -- parent is immediately visible in a non-instantiating context,
15441 -- or if we are in the private part of an instance. This test
15442 -- should still be refined ???
15443
15444 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15445 -- operation as a non-visible operation in cases where the parent
15446 -- subprogram might not be visible now, but was visible within the
15447 -- original generic, so it would be wrong to make the inherited
15448 -- subprogram non-visible now. (Not clear if this test is fully
15449 -- correct; are there any cases where we should declare the inherited
15450 -- operation as not visible to avoid it being overridden, e.g., when
15451 -- the parent type is a generic actual with private primitives ???)
15452
15453 -- (they should be treated the same as other private inherited
15454 -- subprograms, but it's not clear how to do this cleanly). ???
15455
15456 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15457 and then Is_Immediately_Visible (Parent_Subp)
15458 and then not In_Instance)
15459 or else In_Instance_Not_Visible
15460 then
15461 Set_Derived_Name;
15462
15463 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15464 -- overrides an interface primitive because interface primitives
15465 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15466
15467 elsif Ada_Version >= Ada_2005
15468 and then Is_Dispatching_Operation (Parent_Subp)
15469 and then Present (Covered_Interface_Op (Parent_Subp))
15470 then
15471 Set_Derived_Name;
15472
15473 -- Otherwise, the type is inheriting a private operation, so enter it
15474 -- with a special name so it can't be overridden.
15475
15476 else
15477 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15478 end if;
15479
15480 Set_Parent (New_Subp, Parent (Derived_Type));
15481
15482 if Present (Actual_Subp) then
15483 Replace_Type (Actual_Subp, New_Subp);
15484 else
15485 Replace_Type (Parent_Subp, New_Subp);
15486 end if;
15487
15488 Conditional_Delay (New_Subp, Parent_Subp);
15489
15490 -- If we are creating a renaming for a primitive operation of an
15491 -- actual of a generic derived type, we must examine the signature
15492 -- of the actual primitive, not that of the generic formal, which for
15493 -- example may be an interface. However the name and initial value
15494 -- of the inherited operation are those of the formal primitive.
15495
15496 Formal := First_Formal (Parent_Subp);
15497
15498 if Present (Actual_Subp) then
15499 Formal_Of_Actual := First_Formal (Actual_Subp);
15500 else
15501 Formal_Of_Actual := Empty;
15502 end if;
15503
15504 while Present (Formal) loop
15505 New_Formal := New_Copy (Formal);
15506
15507 -- Normally we do not go copying parents, but in the case of
15508 -- formals, we need to link up to the declaration (which is the
15509 -- parameter specification), and it is fine to link up to the
15510 -- original formal's parameter specification in this case.
15511
15512 Set_Parent (New_Formal, Parent (Formal));
15513 Append_Entity (New_Formal, New_Subp);
15514
15515 if Present (Formal_Of_Actual) then
15516 Replace_Type (Formal_Of_Actual, New_Formal);
15517 Next_Formal (Formal_Of_Actual);
15518 else
15519 Replace_Type (Formal, New_Formal);
15520 end if;
15521
15522 Next_Formal (Formal);
15523 end loop;
15524
15525 -- If this derivation corresponds to a tagged generic actual, then
15526 -- primitive operations rename those of the actual. Otherwise the
15527 -- primitive operations rename those of the parent type, If the parent
15528 -- renames an intrinsic operator, so does the new subprogram. We except
15529 -- concatenation, which is always properly typed, and does not get
15530 -- expanded as other intrinsic operations.
15531
15532 if No (Actual_Subp) then
15533 if Is_Intrinsic_Subprogram (Parent_Subp) then
15534 Set_Is_Intrinsic_Subprogram (New_Subp);
15535
15536 if Present (Alias (Parent_Subp))
15537 and then Chars (Parent_Subp) /= Name_Op_Concat
15538 then
15539 Set_Alias (New_Subp, Alias (Parent_Subp));
15540 else
15541 Set_Alias (New_Subp, Parent_Subp);
15542 end if;
15543
15544 else
15545 Set_Alias (New_Subp, Parent_Subp);
15546 end if;
15547
15548 else
15549 Set_Alias (New_Subp, Actual_Subp);
15550 end if;
15551
15552 -- Derived subprograms of a tagged type must inherit the convention
15553 -- of the parent subprogram (a requirement of AI-117). Derived
15554 -- subprograms of untagged types simply get convention Ada by default.
15555
15556 -- If the derived type is a tagged generic formal type with unknown
15557 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15558
15559 -- However, if the type is derived from a generic formal, the further
15560 -- inherited subprogram has the convention of the non-generic ancestor.
15561 -- Otherwise there would be no way to override the operation.
15562 -- (This is subject to forthcoming ARG discussions).
15563
15564 if Is_Tagged_Type (Derived_Type) then
15565 if Is_Generic_Type (Derived_Type)
15566 and then Has_Unknown_Discriminants (Derived_Type)
15567 then
15568 Set_Convention (New_Subp, Convention_Intrinsic);
15569
15570 else
15571 if Is_Generic_Type (Parent_Type)
15572 and then Has_Unknown_Discriminants (Parent_Type)
15573 then
15574 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15575 else
15576 Set_Convention (New_Subp, Convention (Parent_Subp));
15577 end if;
15578 end if;
15579 end if;
15580
15581 -- Predefined controlled operations retain their name even if the parent
15582 -- is hidden (see above), but they are not primitive operations if the
15583 -- ancestor is not visible, for example if the parent is a private
15584 -- extension completed with a controlled extension. Note that a full
15585 -- type that is controlled can break privacy: the flag Is_Controlled is
15586 -- set on both views of the type.
15587
15588 if Is_Controlled (Parent_Type)
15589 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15590 Name_Adjust,
15591 Name_Finalize)
15592 and then Is_Hidden (Parent_Subp)
15593 and then not Is_Visibly_Controlled (Parent_Type)
15594 then
15595 Set_Is_Hidden (New_Subp);
15596 end if;
15597
15598 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15599 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15600
15601 if Ekind (Parent_Subp) = E_Procedure then
15602 Set_Is_Valued_Procedure
15603 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15604 else
15605 Set_Has_Controlling_Result
15606 (New_Subp, Has_Controlling_Result (Parent_Subp));
15607 end if;
15608
15609 -- No_Return must be inherited properly. If this is overridden in the
15610 -- case of a dispatching operation, then a check is made in Sem_Disp
15611 -- that the overriding operation is also No_Return (no such check is
15612 -- required for the case of non-dispatching operation.
15613
15614 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15615
15616 -- A derived function with a controlling result is abstract. If the
15617 -- Derived_Type is a nonabstract formal generic derived type, then
15618 -- inherited operations are not abstract: the required check is done at
15619 -- instantiation time. If the derivation is for a generic actual, the
15620 -- function is not abstract unless the actual is.
15621
15622 if Is_Generic_Type (Derived_Type)
15623 and then not Is_Abstract_Type (Derived_Type)
15624 then
15625 null;
15626
15627 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15628 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15629
15630 -- A subprogram subject to pragma Extensions_Visible with value False
15631 -- requires overriding if the subprogram has at least one controlling
15632 -- OUT parameter (SPARK RM 6.1.7(6)).
15633
15634 elsif Ada_Version >= Ada_2005
15635 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15636 or else (Is_Tagged_Type (Derived_Type)
15637 and then Etype (New_Subp) = Derived_Type
15638 and then not Is_Null_Extension (Derived_Type))
15639 or else (Is_Tagged_Type (Derived_Type)
15640 and then Ekind (Etype (New_Subp)) =
15641 E_Anonymous_Access_Type
15642 and then Designated_Type (Etype (New_Subp)) =
15643 Derived_Type
15644 and then not Is_Null_Extension (Derived_Type))
15645 or else (Comes_From_Source (Alias (New_Subp))
15646 and then Is_EVF_Procedure (Alias (New_Subp))))
15647 and then No (Actual_Subp)
15648 then
15649 if not Is_Tagged_Type (Derived_Type)
15650 or else Is_Abstract_Type (Derived_Type)
15651 or else Is_Abstract_Subprogram (Alias (New_Subp))
15652 then
15653 Set_Is_Abstract_Subprogram (New_Subp);
15654 else
15655 Set_Requires_Overriding (New_Subp);
15656 end if;
15657
15658 elsif Ada_Version < Ada_2005
15659 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15660 or else (Is_Tagged_Type (Derived_Type)
15661 and then Etype (New_Subp) = Derived_Type
15662 and then No (Actual_Subp)))
15663 then
15664 Set_Is_Abstract_Subprogram (New_Subp);
15665
15666 -- AI05-0097 : an inherited operation that dispatches on result is
15667 -- abstract if the derived type is abstract, even if the parent type
15668 -- is concrete and the derived type is a null extension.
15669
15670 elsif Has_Controlling_Result (Alias (New_Subp))
15671 and then Is_Abstract_Type (Etype (New_Subp))
15672 then
15673 Set_Is_Abstract_Subprogram (New_Subp);
15674
15675 -- Finally, if the parent type is abstract we must verify that all
15676 -- inherited operations are either non-abstract or overridden, or that
15677 -- the derived type itself is abstract (this check is performed at the
15678 -- end of a package declaration, in Check_Abstract_Overriding). A
15679 -- private overriding in the parent type will not be visible in the
15680 -- derivation if we are not in an inner package or in a child unit of
15681 -- the parent type, in which case the abstractness of the inherited
15682 -- operation is carried to the new subprogram.
15683
15684 elsif Is_Abstract_Type (Parent_Type)
15685 and then not In_Open_Scopes (Scope (Parent_Type))
15686 and then Is_Private_Overriding
15687 and then Is_Abstract_Subprogram (Visible_Subp)
15688 then
15689 if No (Actual_Subp) then
15690 Set_Alias (New_Subp, Visible_Subp);
15691 Set_Is_Abstract_Subprogram (New_Subp, True);
15692
15693 else
15694 -- If this is a derivation for an instance of a formal derived
15695 -- type, abstractness comes from the primitive operation of the
15696 -- actual, not from the operation inherited from the ancestor.
15697
15698 Set_Is_Abstract_Subprogram
15699 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15700 end if;
15701 end if;
15702
15703 New_Overloaded_Entity (New_Subp, Derived_Type);
15704
15705 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15706 -- preconditions and the derived type is abstract, the derived operation
15707 -- is abstract as well if parent subprogram is not abstract or null.
15708
15709 if Is_Abstract_Type (Derived_Type)
15710 and then Has_Non_Trivial_Precondition (Parent_Subp)
15711 and then Present (Interfaces (Derived_Type))
15712 then
15713
15714 -- Add useful attributes of subprogram before the freeze point,
15715 -- in case freezing is delayed or there are previous errors.
15716
15717 Set_Is_Dispatching_Operation (New_Subp);
15718
15719 declare
15720 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15721
15722 begin
15723 if Present (Iface_Prim)
15724 and then Has_Non_Trivial_Precondition (Iface_Prim)
15725 then
15726 Set_Is_Abstract_Subprogram (New_Subp);
15727 end if;
15728 end;
15729 end if;
15730
15731 -- Check for case of a derived subprogram for the instantiation of a
15732 -- formal derived tagged type, if so mark the subprogram as dispatching
15733 -- and inherit the dispatching attributes of the actual subprogram. The
15734 -- derived subprogram is effectively renaming of the actual subprogram,
15735 -- so it needs to have the same attributes as the actual.
15736
15737 if Present (Actual_Subp)
15738 and then Is_Dispatching_Operation (Actual_Subp)
15739 then
15740 Set_Is_Dispatching_Operation (New_Subp);
15741
15742 if Present (DTC_Entity (Actual_Subp)) then
15743 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15744 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15745 end if;
15746 end if;
15747
15748 -- Indicate that a derived subprogram does not require a body and that
15749 -- it does not require processing of default expressions.
15750
15751 Set_Has_Completion (New_Subp);
15752 Set_Default_Expressions_Processed (New_Subp);
15753
15754 if Ekind (New_Subp) = E_Function then
15755 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15756 end if;
15757 end Derive_Subprogram;
15758
15759 ------------------------
15760 -- Derive_Subprograms --
15761 ------------------------
15762
15763 procedure Derive_Subprograms
15764 (Parent_Type : Entity_Id;
15765 Derived_Type : Entity_Id;
15766 Generic_Actual : Entity_Id := Empty)
15767 is
15768 Op_List : constant Elist_Id :=
15769 Collect_Primitive_Operations (Parent_Type);
15770
15771 function Check_Derived_Type return Boolean;
15772 -- Check that all the entities derived from Parent_Type are found in
15773 -- the list of primitives of Derived_Type exactly in the same order.
15774
15775 procedure Derive_Interface_Subprogram
15776 (New_Subp : out Entity_Id;
15777 Subp : Entity_Id;
15778 Actual_Subp : Entity_Id);
15779 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15780 -- (which is an interface primitive). If Generic_Actual is present then
15781 -- Actual_Subp is the actual subprogram corresponding with the generic
15782 -- subprogram Subp.
15783
15784 ------------------------
15785 -- Check_Derived_Type --
15786 ------------------------
15787
15788 function Check_Derived_Type return Boolean is
15789 E : Entity_Id;
15790 Elmt : Elmt_Id;
15791 List : Elist_Id;
15792 New_Subp : Entity_Id;
15793 Op_Elmt : Elmt_Id;
15794 Subp : Entity_Id;
15795
15796 begin
15797 -- Traverse list of entities in the current scope searching for
15798 -- an incomplete type whose full-view is derived type.
15799
15800 E := First_Entity (Scope (Derived_Type));
15801 while Present (E) and then E /= Derived_Type loop
15802 if Ekind (E) = E_Incomplete_Type
15803 and then Present (Full_View (E))
15804 and then Full_View (E) = Derived_Type
15805 then
15806 -- Disable this test if Derived_Type completes an incomplete
15807 -- type because in such case more primitives can be added
15808 -- later to the list of primitives of Derived_Type by routine
15809 -- Process_Incomplete_Dependents
15810
15811 return True;
15812 end if;
15813
15814 E := Next_Entity (E);
15815 end loop;
15816
15817 List := Collect_Primitive_Operations (Derived_Type);
15818 Elmt := First_Elmt (List);
15819
15820 Op_Elmt := First_Elmt (Op_List);
15821 while Present (Op_Elmt) loop
15822 Subp := Node (Op_Elmt);
15823 New_Subp := Node (Elmt);
15824
15825 -- At this early stage Derived_Type has no entities with attribute
15826 -- Interface_Alias. In addition, such primitives are always
15827 -- located at the end of the list of primitives of Parent_Type.
15828 -- Therefore, if found we can safely stop processing pending
15829 -- entities.
15830
15831 exit when Present (Interface_Alias (Subp));
15832
15833 -- Handle hidden entities
15834
15835 if not Is_Predefined_Dispatching_Operation (Subp)
15836 and then Is_Hidden (Subp)
15837 then
15838 if Present (New_Subp)
15839 and then Primitive_Names_Match (Subp, New_Subp)
15840 then
15841 Next_Elmt (Elmt);
15842 end if;
15843
15844 else
15845 if not Present (New_Subp)
15846 or else Ekind (Subp) /= Ekind (New_Subp)
15847 or else not Primitive_Names_Match (Subp, New_Subp)
15848 then
15849 return False;
15850 end if;
15851
15852 Next_Elmt (Elmt);
15853 end if;
15854
15855 Next_Elmt (Op_Elmt);
15856 end loop;
15857
15858 return True;
15859 end Check_Derived_Type;
15860
15861 ---------------------------------
15862 -- Derive_Interface_Subprogram --
15863 ---------------------------------
15864
15865 procedure Derive_Interface_Subprogram
15866 (New_Subp : out Entity_Id;
15867 Subp : Entity_Id;
15868 Actual_Subp : Entity_Id)
15869 is
15870 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15871 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15872
15873 begin
15874 pragma Assert (Is_Interface (Iface_Type));
15875
15876 Derive_Subprogram
15877 (New_Subp => New_Subp,
15878 Parent_Subp => Iface_Subp,
15879 Derived_Type => Derived_Type,
15880 Parent_Type => Iface_Type,
15881 Actual_Subp => Actual_Subp);
15882
15883 -- Given that this new interface entity corresponds with a primitive
15884 -- of the parent that was not overridden we must leave it associated
15885 -- with its parent primitive to ensure that it will share the same
15886 -- dispatch table slot when overridden. We must set the Alias to Subp
15887 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15888 -- (in case we inherited Subp from Iface_Type via a nonabstract
15889 -- generic formal type).
15890
15891 if No (Actual_Subp) then
15892 Set_Alias (New_Subp, Subp);
15893
15894 declare
15895 T : Entity_Id := Find_Dispatching_Type (Subp);
15896 begin
15897 while Etype (T) /= T loop
15898 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15899 Set_Is_Abstract_Subprogram (New_Subp, False);
15900 exit;
15901 end if;
15902
15903 T := Etype (T);
15904 end loop;
15905 end;
15906
15907 -- For instantiations this is not needed since the previous call to
15908 -- Derive_Subprogram leaves the entity well decorated.
15909
15910 else
15911 pragma Assert (Alias (New_Subp) = Actual_Subp);
15912 null;
15913 end if;
15914 end Derive_Interface_Subprogram;
15915
15916 -- Local variables
15917
15918 Alias_Subp : Entity_Id;
15919 Act_List : Elist_Id;
15920 Act_Elmt : Elmt_Id;
15921 Act_Subp : Entity_Id := Empty;
15922 Elmt : Elmt_Id;
15923 Need_Search : Boolean := False;
15924 New_Subp : Entity_Id := Empty;
15925 Parent_Base : Entity_Id;
15926 Subp : Entity_Id;
15927
15928 -- Start of processing for Derive_Subprograms
15929
15930 begin
15931 if Ekind (Parent_Type) = E_Record_Type_With_Private
15932 and then Has_Discriminants (Parent_Type)
15933 and then Present (Full_View (Parent_Type))
15934 then
15935 Parent_Base := Full_View (Parent_Type);
15936 else
15937 Parent_Base := Parent_Type;
15938 end if;
15939
15940 if Present (Generic_Actual) then
15941 Act_List := Collect_Primitive_Operations (Generic_Actual);
15942 Act_Elmt := First_Elmt (Act_List);
15943 else
15944 Act_List := No_Elist;
15945 Act_Elmt := No_Elmt;
15946 end if;
15947
15948 -- Derive primitives inherited from the parent. Note that if the generic
15949 -- actual is present, this is not really a type derivation, it is a
15950 -- completion within an instance.
15951
15952 -- Case 1: Derived_Type does not implement interfaces
15953
15954 if not Is_Tagged_Type (Derived_Type)
15955 or else (not Has_Interfaces (Derived_Type)
15956 and then not (Present (Generic_Actual)
15957 and then Has_Interfaces (Generic_Actual)))
15958 then
15959 Elmt := First_Elmt (Op_List);
15960 while Present (Elmt) loop
15961 Subp := Node (Elmt);
15962
15963 -- Literals are derived earlier in the process of building the
15964 -- derived type, and are skipped here.
15965
15966 if Ekind (Subp) = E_Enumeration_Literal then
15967 null;
15968
15969 -- The actual is a direct descendant and the common primitive
15970 -- operations appear in the same order.
15971
15972 -- If the generic parent type is present, the derived type is an
15973 -- instance of a formal derived type, and within the instance its
15974 -- operations are those of the actual. We derive from the formal
15975 -- type but make the inherited operations aliases of the
15976 -- corresponding operations of the actual.
15977
15978 else
15979 pragma Assert (No (Node (Act_Elmt))
15980 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15981 and then
15982 Type_Conformant
15983 (Subp, Node (Act_Elmt),
15984 Skip_Controlling_Formals => True)));
15985
15986 Derive_Subprogram
15987 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15988
15989 if Present (Act_Elmt) then
15990 Next_Elmt (Act_Elmt);
15991 end if;
15992 end if;
15993
15994 Next_Elmt (Elmt);
15995 end loop;
15996
15997 -- Case 2: Derived_Type implements interfaces
15998
15999 else
16000 -- If the parent type has no predefined primitives we remove
16001 -- predefined primitives from the list of primitives of generic
16002 -- actual to simplify the complexity of this algorithm.
16003
16004 if Present (Generic_Actual) then
16005 declare
16006 Has_Predefined_Primitives : Boolean := False;
16007
16008 begin
16009 -- Check if the parent type has predefined primitives
16010
16011 Elmt := First_Elmt (Op_List);
16012 while Present (Elmt) loop
16013 Subp := Node (Elmt);
16014
16015 if Is_Predefined_Dispatching_Operation (Subp)
16016 and then not Comes_From_Source (Ultimate_Alias (Subp))
16017 then
16018 Has_Predefined_Primitives := True;
16019 exit;
16020 end if;
16021
16022 Next_Elmt (Elmt);
16023 end loop;
16024
16025 -- Remove predefined primitives of Generic_Actual. We must use
16026 -- an auxiliary list because in case of tagged types the value
16027 -- returned by Collect_Primitive_Operations is the value stored
16028 -- in its Primitive_Operations attribute (and we don't want to
16029 -- modify its current contents).
16030
16031 if not Has_Predefined_Primitives then
16032 declare
16033 Aux_List : constant Elist_Id := New_Elmt_List;
16034
16035 begin
16036 Elmt := First_Elmt (Act_List);
16037 while Present (Elmt) loop
16038 Subp := Node (Elmt);
16039
16040 if not Is_Predefined_Dispatching_Operation (Subp)
16041 or else Comes_From_Source (Subp)
16042 then
16043 Append_Elmt (Subp, Aux_List);
16044 end if;
16045
16046 Next_Elmt (Elmt);
16047 end loop;
16048
16049 Act_List := Aux_List;
16050 end;
16051 end if;
16052
16053 Act_Elmt := First_Elmt (Act_List);
16054 Act_Subp := Node (Act_Elmt);
16055 end;
16056 end if;
16057
16058 -- Stage 1: If the generic actual is not present we derive the
16059 -- primitives inherited from the parent type. If the generic parent
16060 -- type is present, the derived type is an instance of a formal
16061 -- derived type, and within the instance its operations are those of
16062 -- the actual. We derive from the formal type but make the inherited
16063 -- operations aliases of the corresponding operations of the actual.
16064
16065 Elmt := First_Elmt (Op_List);
16066 while Present (Elmt) loop
16067 Subp := Node (Elmt);
16068 Alias_Subp := Ultimate_Alias (Subp);
16069
16070 -- Do not derive internal entities of the parent that link
16071 -- interface primitives with their covering primitive. These
16072 -- entities will be added to this type when frozen.
16073
16074 if Present (Interface_Alias (Subp)) then
16075 goto Continue;
16076 end if;
16077
16078 -- If the generic actual is present find the corresponding
16079 -- operation in the generic actual. If the parent type is a
16080 -- direct ancestor of the derived type then, even if it is an
16081 -- interface, the operations are inherited from the primary
16082 -- dispatch table and are in the proper order. If we detect here
16083 -- that primitives are not in the same order we traverse the list
16084 -- of primitive operations of the actual to find the one that
16085 -- implements the interface primitive.
16086
16087 if Need_Search
16088 or else
16089 (Present (Generic_Actual)
16090 and then Present (Act_Subp)
16091 and then not
16092 (Primitive_Names_Match (Subp, Act_Subp)
16093 and then
16094 Type_Conformant (Subp, Act_Subp,
16095 Skip_Controlling_Formals => True)))
16096 then
16097 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16098 Use_Full_View => True));
16099
16100 -- Remember that we need searching for all pending primitives
16101
16102 Need_Search := True;
16103
16104 -- Handle entities associated with interface primitives
16105
16106 if Present (Alias_Subp)
16107 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16108 and then not Is_Predefined_Dispatching_Operation (Subp)
16109 then
16110 -- Search for the primitive in the homonym chain
16111
16112 Act_Subp :=
16113 Find_Primitive_Covering_Interface
16114 (Tagged_Type => Generic_Actual,
16115 Iface_Prim => Alias_Subp);
16116
16117 -- Previous search may not locate primitives covering
16118 -- interfaces defined in generics units or instantiations.
16119 -- (it fails if the covering primitive has formals whose
16120 -- type is also defined in generics or instantiations).
16121 -- In such case we search in the list of primitives of the
16122 -- generic actual for the internal entity that links the
16123 -- interface primitive and the covering primitive.
16124
16125 if No (Act_Subp)
16126 and then Is_Generic_Type (Parent_Type)
16127 then
16128 -- This code has been designed to handle only generic
16129 -- formals that implement interfaces that are defined
16130 -- in a generic unit or instantiation. If this code is
16131 -- needed for other cases we must review it because
16132 -- (given that it relies on Original_Location to locate
16133 -- the primitive of Generic_Actual that covers the
16134 -- interface) it could leave linked through attribute
16135 -- Alias entities of unrelated instantiations).
16136
16137 pragma Assert
16138 (Is_Generic_Unit
16139 (Scope (Find_Dispatching_Type (Alias_Subp)))
16140 or else
16141 Instantiation_Depth
16142 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16143
16144 declare
16145 Iface_Prim_Loc : constant Source_Ptr :=
16146 Original_Location (Sloc (Alias_Subp));
16147
16148 Elmt : Elmt_Id;
16149 Prim : Entity_Id;
16150
16151 begin
16152 Elmt :=
16153 First_Elmt (Primitive_Operations (Generic_Actual));
16154
16155 Search : while Present (Elmt) loop
16156 Prim := Node (Elmt);
16157
16158 if Present (Interface_Alias (Prim))
16159 and then Original_Location
16160 (Sloc (Interface_Alias (Prim))) =
16161 Iface_Prim_Loc
16162 then
16163 Act_Subp := Alias (Prim);
16164 exit Search;
16165 end if;
16166
16167 Next_Elmt (Elmt);
16168 end loop Search;
16169 end;
16170 end if;
16171
16172 pragma Assert (Present (Act_Subp)
16173 or else Is_Abstract_Type (Generic_Actual)
16174 or else Serious_Errors_Detected > 0);
16175
16176 -- Handle predefined primitives plus the rest of user-defined
16177 -- primitives
16178
16179 else
16180 Act_Elmt := First_Elmt (Act_List);
16181 while Present (Act_Elmt) loop
16182 Act_Subp := Node (Act_Elmt);
16183
16184 exit when Primitive_Names_Match (Subp, Act_Subp)
16185 and then Type_Conformant
16186 (Subp, Act_Subp,
16187 Skip_Controlling_Formals => True)
16188 and then No (Interface_Alias (Act_Subp));
16189
16190 Next_Elmt (Act_Elmt);
16191 end loop;
16192
16193 if No (Act_Elmt) then
16194 Act_Subp := Empty;
16195 end if;
16196 end if;
16197 end if;
16198
16199 -- Case 1: If the parent is a limited interface then it has the
16200 -- predefined primitives of synchronized interfaces. However, the
16201 -- actual type may be a non-limited type and hence it does not
16202 -- have such primitives.
16203
16204 if Present (Generic_Actual)
16205 and then not Present (Act_Subp)
16206 and then Is_Limited_Interface (Parent_Base)
16207 and then Is_Predefined_Interface_Primitive (Subp)
16208 then
16209 null;
16210
16211 -- Case 2: Inherit entities associated with interfaces that were
16212 -- not covered by the parent type. We exclude here null interface
16213 -- primitives because they do not need special management.
16214
16215 -- We also exclude interface operations that are renamings. If the
16216 -- subprogram is an explicit renaming of an interface primitive,
16217 -- it is a regular primitive operation, and the presence of its
16218 -- alias is not relevant: it has to be derived like any other
16219 -- primitive.
16220
16221 elsif Present (Alias (Subp))
16222 and then Nkind (Unit_Declaration_Node (Subp)) /=
16223 N_Subprogram_Renaming_Declaration
16224 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16225 and then not
16226 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16227 and then Null_Present (Parent (Alias_Subp)))
16228 then
16229 -- If this is an abstract private type then we transfer the
16230 -- derivation of the interface primitive from the partial view
16231 -- to the full view. This is safe because all the interfaces
16232 -- must be visible in the partial view. Done to avoid adding
16233 -- a new interface derivation to the private part of the
16234 -- enclosing package; otherwise this new derivation would be
16235 -- decorated as hidden when the analysis of the enclosing
16236 -- package completes.
16237
16238 if Is_Abstract_Type (Derived_Type)
16239 and then In_Private_Part (Current_Scope)
16240 and then Has_Private_Declaration (Derived_Type)
16241 then
16242 declare
16243 Partial_View : Entity_Id;
16244 Elmt : Elmt_Id;
16245 Ent : Entity_Id;
16246
16247 begin
16248 Partial_View := First_Entity (Current_Scope);
16249 loop
16250 exit when No (Partial_View)
16251 or else (Has_Private_Declaration (Partial_View)
16252 and then
16253 Full_View (Partial_View) = Derived_Type);
16254
16255 Next_Entity (Partial_View);
16256 end loop;
16257
16258 -- If the partial view was not found then the source code
16259 -- has errors and the derivation is not needed.
16260
16261 if Present (Partial_View) then
16262 Elmt :=
16263 First_Elmt (Primitive_Operations (Partial_View));
16264 while Present (Elmt) loop
16265 Ent := Node (Elmt);
16266
16267 if Present (Alias (Ent))
16268 and then Ultimate_Alias (Ent) = Alias (Subp)
16269 then
16270 Append_Elmt
16271 (Ent, Primitive_Operations (Derived_Type));
16272 exit;
16273 end if;
16274
16275 Next_Elmt (Elmt);
16276 end loop;
16277
16278 -- If the interface primitive was not found in the
16279 -- partial view then this interface primitive was
16280 -- overridden. We add a derivation to activate in
16281 -- Derive_Progenitor_Subprograms the machinery to
16282 -- search for it.
16283
16284 if No (Elmt) then
16285 Derive_Interface_Subprogram
16286 (New_Subp => New_Subp,
16287 Subp => Subp,
16288 Actual_Subp => Act_Subp);
16289 end if;
16290 end if;
16291 end;
16292 else
16293 Derive_Interface_Subprogram
16294 (New_Subp => New_Subp,
16295 Subp => Subp,
16296 Actual_Subp => Act_Subp);
16297 end if;
16298
16299 -- Case 3: Common derivation
16300
16301 else
16302 Derive_Subprogram
16303 (New_Subp => New_Subp,
16304 Parent_Subp => Subp,
16305 Derived_Type => Derived_Type,
16306 Parent_Type => Parent_Base,
16307 Actual_Subp => Act_Subp);
16308 end if;
16309
16310 -- No need to update Act_Elm if we must search for the
16311 -- corresponding operation in the generic actual
16312
16313 if not Need_Search
16314 and then Present (Act_Elmt)
16315 then
16316 Next_Elmt (Act_Elmt);
16317 Act_Subp := Node (Act_Elmt);
16318 end if;
16319
16320 <<Continue>>
16321 Next_Elmt (Elmt);
16322 end loop;
16323
16324 -- Inherit additional operations from progenitors. If the derived
16325 -- type is a generic actual, there are not new primitive operations
16326 -- for the type because it has those of the actual, and therefore
16327 -- nothing needs to be done. The renamings generated above are not
16328 -- primitive operations, and their purpose is simply to make the
16329 -- proper operations visible within an instantiation.
16330
16331 if No (Generic_Actual) then
16332 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16333 end if;
16334 end if;
16335
16336 -- Final check: Direct descendants must have their primitives in the
16337 -- same order. We exclude from this test untagged types and instances
16338 -- of formal derived types. We skip this test if we have already
16339 -- reported serious errors in the sources.
16340
16341 pragma Assert (not Is_Tagged_Type (Derived_Type)
16342 or else Present (Generic_Actual)
16343 or else Serious_Errors_Detected > 0
16344 or else Check_Derived_Type);
16345 end Derive_Subprograms;
16346
16347 --------------------------------
16348 -- Derived_Standard_Character --
16349 --------------------------------
16350
16351 procedure Derived_Standard_Character
16352 (N : Node_Id;
16353 Parent_Type : Entity_Id;
16354 Derived_Type : Entity_Id)
16355 is
16356 Loc : constant Source_Ptr := Sloc (N);
16357 Def : constant Node_Id := Type_Definition (N);
16358 Indic : constant Node_Id := Subtype_Indication (Def);
16359 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16360 Implicit_Base : constant Entity_Id :=
16361 Create_Itype
16362 (E_Enumeration_Type, N, Derived_Type, 'B');
16363
16364 Lo : Node_Id;
16365 Hi : Node_Id;
16366
16367 begin
16368 Discard_Node (Process_Subtype (Indic, N));
16369
16370 Set_Etype (Implicit_Base, Parent_Base);
16371 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16372 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16373
16374 Set_Is_Character_Type (Implicit_Base, True);
16375 Set_Has_Delayed_Freeze (Implicit_Base);
16376
16377 -- The bounds of the implicit base are the bounds of the parent base.
16378 -- Note that their type is the parent base.
16379
16380 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16381 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16382
16383 Set_Scalar_Range (Implicit_Base,
16384 Make_Range (Loc,
16385 Low_Bound => Lo,
16386 High_Bound => Hi));
16387
16388 Conditional_Delay (Derived_Type, Parent_Type);
16389
16390 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16391 Set_Etype (Derived_Type, Implicit_Base);
16392 Set_Size_Info (Derived_Type, Parent_Type);
16393
16394 if Unknown_RM_Size (Derived_Type) then
16395 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16396 end if;
16397
16398 Set_Is_Character_Type (Derived_Type, True);
16399
16400 if Nkind (Indic) /= N_Subtype_Indication then
16401
16402 -- If no explicit constraint, the bounds are those
16403 -- of the parent type.
16404
16405 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16406 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16407 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16408 end if;
16409
16410 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16411
16412 -- Because the implicit base is used in the conversion of the bounds, we
16413 -- have to freeze it now. This is similar to what is done for numeric
16414 -- types, and it equally suspicious, but otherwise a non-static bound
16415 -- will have a reference to an unfrozen type, which is rejected by Gigi
16416 -- (???). This requires specific care for definition of stream
16417 -- attributes. For details, see comments at the end of
16418 -- Build_Derived_Numeric_Type.
16419
16420 Freeze_Before (N, Implicit_Base);
16421 end Derived_Standard_Character;
16422
16423 ------------------------------
16424 -- Derived_Type_Declaration --
16425 ------------------------------
16426
16427 procedure Derived_Type_Declaration
16428 (T : Entity_Id;
16429 N : Node_Id;
16430 Is_Completion : Boolean)
16431 is
16432 Parent_Type : Entity_Id;
16433
16434 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16435 -- Check whether the parent type is a generic formal, or derives
16436 -- directly or indirectly from one.
16437
16438 ------------------------
16439 -- Comes_From_Generic --
16440 ------------------------
16441
16442 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16443 begin
16444 if Is_Generic_Type (Typ) then
16445 return True;
16446
16447 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16448 return True;
16449
16450 elsif Is_Private_Type (Typ)
16451 and then Present (Full_View (Typ))
16452 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16453 then
16454 return True;
16455
16456 elsif Is_Generic_Actual_Type (Typ) then
16457 return True;
16458
16459 else
16460 return False;
16461 end if;
16462 end Comes_From_Generic;
16463
16464 -- Local variables
16465
16466 Def : constant Node_Id := Type_Definition (N);
16467 Iface_Def : Node_Id;
16468 Indic : constant Node_Id := Subtype_Indication (Def);
16469 Extension : constant Node_Id := Record_Extension_Part (Def);
16470 Parent_Node : Node_Id;
16471 Taggd : Boolean;
16472
16473 -- Start of processing for Derived_Type_Declaration
16474
16475 begin
16476 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16477
16478 if SPARK_Mode = On
16479 and then Is_Tagged_Type (Parent_Type)
16480 then
16481 declare
16482 Partial_View : constant Entity_Id :=
16483 Incomplete_Or_Partial_View (Parent_Type);
16484
16485 begin
16486 -- If the partial view was not found then the parent type is not
16487 -- a private type. Otherwise check if the partial view is a tagged
16488 -- private type.
16489
16490 if Present (Partial_View)
16491 and then Is_Private_Type (Partial_View)
16492 and then not Is_Tagged_Type (Partial_View)
16493 then
16494 Error_Msg_NE
16495 ("cannot derive from & declared as untagged private "
16496 & "(SPARK RM 3.4(1))", N, Partial_View);
16497 end if;
16498 end;
16499 end if;
16500
16501 -- Ada 2005 (AI-251): In case of interface derivation check that the
16502 -- parent is also an interface.
16503
16504 if Interface_Present (Def) then
16505 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16506
16507 if not Is_Interface (Parent_Type) then
16508 Diagnose_Interface (Indic, Parent_Type);
16509
16510 else
16511 Parent_Node := Parent (Base_Type (Parent_Type));
16512 Iface_Def := Type_Definition (Parent_Node);
16513
16514 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16515 -- other limited interfaces.
16516
16517 if Limited_Present (Def) then
16518 if Limited_Present (Iface_Def) then
16519 null;
16520
16521 elsif Protected_Present (Iface_Def) then
16522 Error_Msg_NE
16523 ("descendant of & must be declared as a protected "
16524 & "interface", N, Parent_Type);
16525
16526 elsif Synchronized_Present (Iface_Def) then
16527 Error_Msg_NE
16528 ("descendant of & must be declared as a synchronized "
16529 & "interface", N, Parent_Type);
16530
16531 elsif Task_Present (Iface_Def) then
16532 Error_Msg_NE
16533 ("descendant of & must be declared as a task interface",
16534 N, Parent_Type);
16535
16536 else
16537 Error_Msg_N
16538 ("(Ada 2005) limited interface cannot inherit from "
16539 & "non-limited interface", Indic);
16540 end if;
16541
16542 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16543 -- from non-limited or limited interfaces.
16544
16545 elsif not Protected_Present (Def)
16546 and then not Synchronized_Present (Def)
16547 and then not Task_Present (Def)
16548 then
16549 if Limited_Present (Iface_Def) then
16550 null;
16551
16552 elsif Protected_Present (Iface_Def) then
16553 Error_Msg_NE
16554 ("descendant of & must be declared as a protected "
16555 & "interface", N, Parent_Type);
16556
16557 elsif Synchronized_Present (Iface_Def) then
16558 Error_Msg_NE
16559 ("descendant of & must be declared as a synchronized "
16560 & "interface", N, Parent_Type);
16561
16562 elsif Task_Present (Iface_Def) then
16563 Error_Msg_NE
16564 ("descendant of & must be declared as a task interface",
16565 N, Parent_Type);
16566 else
16567 null;
16568 end if;
16569 end if;
16570 end if;
16571 end if;
16572
16573 if Is_Tagged_Type (Parent_Type)
16574 and then Is_Concurrent_Type (Parent_Type)
16575 and then not Is_Interface (Parent_Type)
16576 then
16577 Error_Msg_N
16578 ("parent type of a record extension cannot be a synchronized "
16579 & "tagged type (RM 3.9.1 (3/1))", N);
16580 Set_Etype (T, Any_Type);
16581 return;
16582 end if;
16583
16584 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16585 -- interfaces
16586
16587 if Is_Tagged_Type (Parent_Type)
16588 and then Is_Non_Empty_List (Interface_List (Def))
16589 then
16590 declare
16591 Intf : Node_Id;
16592 T : Entity_Id;
16593
16594 begin
16595 Intf := First (Interface_List (Def));
16596 while Present (Intf) loop
16597 T := Find_Type_Of_Subtype_Indic (Intf);
16598
16599 if not Is_Interface (T) then
16600 Diagnose_Interface (Intf, T);
16601
16602 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16603 -- a limited type from having a nonlimited progenitor.
16604
16605 elsif (Limited_Present (Def)
16606 or else (not Is_Interface (Parent_Type)
16607 and then Is_Limited_Type (Parent_Type)))
16608 and then not Is_Limited_Interface (T)
16609 then
16610 Error_Msg_NE
16611 ("progenitor interface& of limited type must be limited",
16612 N, T);
16613 end if;
16614
16615 Next (Intf);
16616 end loop;
16617 end;
16618 end if;
16619
16620 if Parent_Type = Any_Type
16621 or else Etype (Parent_Type) = Any_Type
16622 or else (Is_Class_Wide_Type (Parent_Type)
16623 and then Etype (Parent_Type) = T)
16624 then
16625 -- If Parent_Type is undefined or illegal, make new type into a
16626 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16627 -- errors. If this is a self-definition, emit error now.
16628
16629 if T = Parent_Type or else T = Etype (Parent_Type) then
16630 Error_Msg_N ("type cannot be used in its own definition", Indic);
16631 end if;
16632
16633 Set_Ekind (T, Ekind (Parent_Type));
16634 Set_Etype (T, Any_Type);
16635 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16636
16637 if Is_Tagged_Type (T)
16638 and then Is_Record_Type (T)
16639 then
16640 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16641 end if;
16642
16643 return;
16644 end if;
16645
16646 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16647 -- an interface is special because the list of interfaces in the full
16648 -- view can be given in any order. For example:
16649
16650 -- type A is interface;
16651 -- type B is interface and A;
16652 -- type D is new B with private;
16653 -- private
16654 -- type D is new A and B with null record; -- 1 --
16655
16656 -- In this case we perform the following transformation of -1-:
16657
16658 -- type D is new B and A with null record;
16659
16660 -- If the parent of the full-view covers the parent of the partial-view
16661 -- we have two possible cases:
16662
16663 -- 1) They have the same parent
16664 -- 2) The parent of the full-view implements some further interfaces
16665
16666 -- In both cases we do not need to perform the transformation. In the
16667 -- first case the source program is correct and the transformation is
16668 -- not needed; in the second case the source program does not fulfill
16669 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16670 -- later.
16671
16672 -- This transformation not only simplifies the rest of the analysis of
16673 -- this type declaration but also simplifies the correct generation of
16674 -- the object layout to the expander.
16675
16676 if In_Private_Part (Current_Scope)
16677 and then Is_Interface (Parent_Type)
16678 then
16679 declare
16680 Iface : Node_Id;
16681 Partial_View : Entity_Id;
16682 Partial_View_Parent : Entity_Id;
16683 New_Iface : Node_Id;
16684
16685 begin
16686 -- Look for the associated private type declaration
16687
16688 Partial_View := Incomplete_Or_Partial_View (T);
16689
16690 -- If the partial view was not found then the source code has
16691 -- errors and the transformation is not needed.
16692
16693 if Present (Partial_View) then
16694 Partial_View_Parent := Etype (Partial_View);
16695
16696 -- If the parent of the full-view covers the parent of the
16697 -- partial-view we have nothing else to do.
16698
16699 if Interface_Present_In_Ancestor
16700 (Parent_Type, Partial_View_Parent)
16701 then
16702 null;
16703
16704 -- Traverse the list of interfaces of the full-view to look
16705 -- for the parent of the partial-view and perform the tree
16706 -- transformation.
16707
16708 else
16709 Iface := First (Interface_List (Def));
16710 while Present (Iface) loop
16711 if Etype (Iface) = Etype (Partial_View) then
16712 Rewrite (Subtype_Indication (Def),
16713 New_Copy (Subtype_Indication
16714 (Parent (Partial_View))));
16715
16716 New_Iface :=
16717 Make_Identifier (Sloc (N), Chars (Parent_Type));
16718 Append (New_Iface, Interface_List (Def));
16719
16720 -- Analyze the transformed code
16721
16722 Derived_Type_Declaration (T, N, Is_Completion);
16723 return;
16724 end if;
16725
16726 Next (Iface);
16727 end loop;
16728 end if;
16729 end if;
16730 end;
16731 end if;
16732
16733 -- Only composite types other than array types are allowed to have
16734 -- discriminants.
16735
16736 if Present (Discriminant_Specifications (N)) then
16737 if (Is_Elementary_Type (Parent_Type)
16738 or else
16739 Is_Array_Type (Parent_Type))
16740 and then not Error_Posted (N)
16741 then
16742 Error_Msg_N
16743 ("elementary or array type cannot have discriminants",
16744 Defining_Identifier (First (Discriminant_Specifications (N))));
16745
16746 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16747 -- only if we are not already processing a malformed syntax tree.
16748
16749 if Is_Type (T) then
16750 Set_Has_Discriminants (T, False);
16751 end if;
16752
16753 -- The type is allowed to have discriminants
16754
16755 else
16756 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16757 end if;
16758 end if;
16759
16760 -- In Ada 83, a derived type defined in a package specification cannot
16761 -- be used for further derivation until the end of its visible part.
16762 -- Note that derivation in the private part of the package is allowed.
16763
16764 if Ada_Version = Ada_83
16765 and then Is_Derived_Type (Parent_Type)
16766 and then In_Visible_Part (Scope (Parent_Type))
16767 then
16768 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16769 Error_Msg_N
16770 ("(Ada 83): premature use of type for derivation", Indic);
16771 end if;
16772 end if;
16773
16774 -- Check for early use of incomplete or private type
16775
16776 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16777 Error_Msg_N ("premature derivation of incomplete type", Indic);
16778 return;
16779
16780 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16781 and then not Comes_From_Generic (Parent_Type))
16782 or else Has_Private_Component (Parent_Type)
16783 then
16784 -- The ancestor type of a formal type can be incomplete, in which
16785 -- case only the operations of the partial view are available in the
16786 -- generic. Subsequent checks may be required when the full view is
16787 -- analyzed to verify that a derivation from a tagged type has an
16788 -- extension.
16789
16790 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16791 null;
16792
16793 elsif No (Underlying_Type (Parent_Type))
16794 or else Has_Private_Component (Parent_Type)
16795 then
16796 Error_Msg_N
16797 ("premature derivation of derived or private type", Indic);
16798
16799 -- Flag the type itself as being in error, this prevents some
16800 -- nasty problems with subsequent uses of the malformed type.
16801
16802 Set_Error_Posted (T);
16803
16804 -- Check that within the immediate scope of an untagged partial
16805 -- view it's illegal to derive from the partial view if the
16806 -- full view is tagged. (7.3(7))
16807
16808 -- We verify that the Parent_Type is a partial view by checking
16809 -- that it is not a Full_Type_Declaration (i.e. a private type or
16810 -- private extension declaration), to distinguish a partial view
16811 -- from a derivation from a private type which also appears as
16812 -- E_Private_Type. If the parent base type is not declared in an
16813 -- enclosing scope there is no need to check.
16814
16815 elsif Present (Full_View (Parent_Type))
16816 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16817 and then not Is_Tagged_Type (Parent_Type)
16818 and then Is_Tagged_Type (Full_View (Parent_Type))
16819 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16820 then
16821 Error_Msg_N
16822 ("premature derivation from type with tagged full view",
16823 Indic);
16824 end if;
16825 end if;
16826
16827 -- Check that form of derivation is appropriate
16828
16829 Taggd := Is_Tagged_Type (Parent_Type);
16830
16831 -- Set the parent type to the class-wide type's specific type in this
16832 -- case to prevent cascading errors
16833
16834 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16835 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16836 Set_Etype (T, Etype (Parent_Type));
16837 return;
16838 end if;
16839
16840 if Present (Extension) and then not Taggd then
16841 Error_Msg_N
16842 ("type derived from untagged type cannot have extension", Indic);
16843
16844 elsif No (Extension) and then Taggd then
16845
16846 -- If this declaration is within a private part (or body) of a
16847 -- generic instantiation then the derivation is allowed (the parent
16848 -- type can only appear tagged in this case if it's a generic actual
16849 -- type, since it would otherwise have been rejected in the analysis
16850 -- of the generic template).
16851
16852 if not Is_Generic_Actual_Type (Parent_Type)
16853 or else In_Visible_Part (Scope (Parent_Type))
16854 then
16855 if Is_Class_Wide_Type (Parent_Type) then
16856 Error_Msg_N
16857 ("parent type must not be a class-wide type", Indic);
16858
16859 -- Use specific type to prevent cascaded errors.
16860
16861 Parent_Type := Etype (Parent_Type);
16862
16863 else
16864 Error_Msg_N
16865 ("type derived from tagged type must have extension", Indic);
16866 end if;
16867 end if;
16868 end if;
16869
16870 -- AI-443: Synchronized formal derived types require a private
16871 -- extension. There is no point in checking the ancestor type or
16872 -- the progenitors since the construct is wrong to begin with.
16873
16874 if Ada_Version >= Ada_2005
16875 and then Is_Generic_Type (T)
16876 and then Present (Original_Node (N))
16877 then
16878 declare
16879 Decl : constant Node_Id := Original_Node (N);
16880
16881 begin
16882 if Nkind (Decl) = N_Formal_Type_Declaration
16883 and then Nkind (Formal_Type_Definition (Decl)) =
16884 N_Formal_Derived_Type_Definition
16885 and then Synchronized_Present (Formal_Type_Definition (Decl))
16886 and then No (Extension)
16887
16888 -- Avoid emitting a duplicate error message
16889
16890 and then not Error_Posted (Indic)
16891 then
16892 Error_Msg_N
16893 ("synchronized derived type must have extension", N);
16894 end if;
16895 end;
16896 end if;
16897
16898 if Null_Exclusion_Present (Def)
16899 and then not Is_Access_Type (Parent_Type)
16900 then
16901 Error_Msg_N ("null exclusion can only apply to an access type", N);
16902 end if;
16903
16904 -- Avoid deriving parent primitives of underlying record views
16905
16906 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16907 Derive_Subps => not Is_Underlying_Record_View (T));
16908
16909 -- AI-419: The parent type of an explicitly limited derived type must
16910 -- be a limited type or a limited interface.
16911
16912 if Limited_Present (Def) then
16913 Set_Is_Limited_Record (T);
16914
16915 if Is_Interface (T) then
16916 Set_Is_Limited_Interface (T);
16917 end if;
16918
16919 if not Is_Limited_Type (Parent_Type)
16920 and then
16921 (not Is_Interface (Parent_Type)
16922 or else not Is_Limited_Interface (Parent_Type))
16923 then
16924 -- AI05-0096: a derivation in the private part of an instance is
16925 -- legal if the generic formal is untagged limited, and the actual
16926 -- is non-limited.
16927
16928 if Is_Generic_Actual_Type (Parent_Type)
16929 and then In_Private_Part (Current_Scope)
16930 and then
16931 not Is_Tagged_Type
16932 (Generic_Parent_Type (Parent (Parent_Type)))
16933 then
16934 null;
16935
16936 else
16937 Error_Msg_NE
16938 ("parent type& of limited type must be limited",
16939 N, Parent_Type);
16940 end if;
16941 end if;
16942 end if;
16943
16944 -- In SPARK, there are no derived type definitions other than type
16945 -- extensions of tagged record types.
16946
16947 if No (Extension) then
16948 Check_SPARK_05_Restriction
16949 ("derived type is not allowed", Original_Node (N));
16950 end if;
16951 end Derived_Type_Declaration;
16952
16953 ------------------------
16954 -- Diagnose_Interface --
16955 ------------------------
16956
16957 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16958 begin
16959 if not Is_Interface (E) and then E /= Any_Type then
16960 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16961 end if;
16962 end Diagnose_Interface;
16963
16964 ----------------------------------
16965 -- Enumeration_Type_Declaration --
16966 ----------------------------------
16967
16968 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16969 Ev : Uint;
16970 L : Node_Id;
16971 R_Node : Node_Id;
16972 B_Node : Node_Id;
16973
16974 begin
16975 -- Create identifier node representing lower bound
16976
16977 B_Node := New_Node (N_Identifier, Sloc (Def));
16978 L := First (Literals (Def));
16979 Set_Chars (B_Node, Chars (L));
16980 Set_Entity (B_Node, L);
16981 Set_Etype (B_Node, T);
16982 Set_Is_Static_Expression (B_Node, True);
16983
16984 R_Node := New_Node (N_Range, Sloc (Def));
16985 Set_Low_Bound (R_Node, B_Node);
16986
16987 Set_Ekind (T, E_Enumeration_Type);
16988 Set_First_Literal (T, L);
16989 Set_Etype (T, T);
16990 Set_Is_Constrained (T);
16991
16992 Ev := Uint_0;
16993
16994 -- Loop through literals of enumeration type setting pos and rep values
16995 -- except that if the Ekind is already set, then it means the literal
16996 -- was already constructed (case of a derived type declaration and we
16997 -- should not disturb the Pos and Rep values.
16998
16999 while Present (L) loop
17000 if Ekind (L) /= E_Enumeration_Literal then
17001 Set_Ekind (L, E_Enumeration_Literal);
17002 Set_Enumeration_Pos (L, Ev);
17003 Set_Enumeration_Rep (L, Ev);
17004 Set_Is_Known_Valid (L, True);
17005 end if;
17006
17007 Set_Etype (L, T);
17008 New_Overloaded_Entity (L);
17009 Generate_Definition (L);
17010 Set_Convention (L, Convention_Intrinsic);
17011
17012 -- Case of character literal
17013
17014 if Nkind (L) = N_Defining_Character_Literal then
17015 Set_Is_Character_Type (T, True);
17016
17017 -- Check violation of No_Wide_Characters
17018
17019 if Restriction_Check_Required (No_Wide_Characters) then
17020 Get_Name_String (Chars (L));
17021
17022 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17023 Check_Restriction (No_Wide_Characters, L);
17024 end if;
17025 end if;
17026 end if;
17027
17028 Ev := Ev + 1;
17029 Next (L);
17030 end loop;
17031
17032 -- Now create a node representing upper bound
17033
17034 B_Node := New_Node (N_Identifier, Sloc (Def));
17035 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17036 Set_Entity (B_Node, Last (Literals (Def)));
17037 Set_Etype (B_Node, T);
17038 Set_Is_Static_Expression (B_Node, True);
17039
17040 Set_High_Bound (R_Node, B_Node);
17041
17042 -- Initialize various fields of the type. Some of this information
17043 -- may be overwritten later through rep.clauses.
17044
17045 Set_Scalar_Range (T, R_Node);
17046 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17047 Set_Enum_Esize (T);
17048 Set_Enum_Pos_To_Rep (T, Empty);
17049
17050 -- Set Discard_Names if configuration pragma set, or if there is
17051 -- a parameterless pragma in the current declarative region
17052
17053 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17054 Set_Discard_Names (T);
17055 end if;
17056
17057 -- Process end label if there is one
17058
17059 if Present (Def) then
17060 Process_End_Label (Def, 'e', T);
17061 end if;
17062 end Enumeration_Type_Declaration;
17063
17064 ---------------------------------
17065 -- Expand_To_Stored_Constraint --
17066 ---------------------------------
17067
17068 function Expand_To_Stored_Constraint
17069 (Typ : Entity_Id;
17070 Constraint : Elist_Id) return Elist_Id
17071 is
17072 Explicitly_Discriminated_Type : Entity_Id;
17073 Expansion : Elist_Id;
17074 Discriminant : Entity_Id;
17075
17076 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17077 -- Find the nearest type that actually specifies discriminants
17078
17079 ---------------------------------
17080 -- Type_With_Explicit_Discrims --
17081 ---------------------------------
17082
17083 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17084 Typ : constant E := Base_Type (Id);
17085
17086 begin
17087 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17088 if Present (Full_View (Typ)) then
17089 return Type_With_Explicit_Discrims (Full_View (Typ));
17090 end if;
17091
17092 else
17093 if Has_Discriminants (Typ) then
17094 return Typ;
17095 end if;
17096 end if;
17097
17098 if Etype (Typ) = Typ then
17099 return Empty;
17100 elsif Has_Discriminants (Typ) then
17101 return Typ;
17102 else
17103 return Type_With_Explicit_Discrims (Etype (Typ));
17104 end if;
17105
17106 end Type_With_Explicit_Discrims;
17107
17108 -- Start of processing for Expand_To_Stored_Constraint
17109
17110 begin
17111 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17112 return No_Elist;
17113 end if;
17114
17115 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17116
17117 if No (Explicitly_Discriminated_Type) then
17118 return No_Elist;
17119 end if;
17120
17121 Expansion := New_Elmt_List;
17122
17123 Discriminant :=
17124 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17125 while Present (Discriminant) loop
17126 Append_Elmt
17127 (Get_Discriminant_Value
17128 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17129 To => Expansion);
17130 Next_Stored_Discriminant (Discriminant);
17131 end loop;
17132
17133 return Expansion;
17134 end Expand_To_Stored_Constraint;
17135
17136 ---------------------------
17137 -- Find_Hidden_Interface --
17138 ---------------------------
17139
17140 function Find_Hidden_Interface
17141 (Src : Elist_Id;
17142 Dest : Elist_Id) return Entity_Id
17143 is
17144 Iface : Entity_Id;
17145 Iface_Elmt : Elmt_Id;
17146
17147 begin
17148 if Present (Src) and then Present (Dest) then
17149 Iface_Elmt := First_Elmt (Src);
17150 while Present (Iface_Elmt) loop
17151 Iface := Node (Iface_Elmt);
17152
17153 if Is_Interface (Iface)
17154 and then not Contain_Interface (Iface, Dest)
17155 then
17156 return Iface;
17157 end if;
17158
17159 Next_Elmt (Iface_Elmt);
17160 end loop;
17161 end if;
17162
17163 return Empty;
17164 end Find_Hidden_Interface;
17165
17166 --------------------
17167 -- Find_Type_Name --
17168 --------------------
17169
17170 function Find_Type_Name (N : Node_Id) return Entity_Id is
17171 Id : constant Entity_Id := Defining_Identifier (N);
17172 New_Id : Entity_Id;
17173 Prev : Entity_Id;
17174 Prev_Par : Node_Id;
17175
17176 procedure Check_Duplicate_Aspects;
17177 -- Check that aspects specified in a completion have not been specified
17178 -- already in the partial view.
17179
17180 procedure Tag_Mismatch;
17181 -- Diagnose a tagged partial view whose full view is untagged. We post
17182 -- the message on the full view, with a reference to the previous
17183 -- partial view. The partial view can be private or incomplete, and
17184 -- these are handled in a different manner, so we determine the position
17185 -- of the error message from the respective slocs of both.
17186
17187 -----------------------------
17188 -- Check_Duplicate_Aspects --
17189 -----------------------------
17190
17191 procedure Check_Duplicate_Aspects is
17192 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17193 -- Return the corresponding aspect of the partial view which matches
17194 -- the aspect id of Asp. Return Empty is no such aspect exists.
17195
17196 -----------------------------
17197 -- Get_Partial_View_Aspect --
17198 -----------------------------
17199
17200 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17201 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17202 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17203 Prev_Asp : Node_Id;
17204
17205 begin
17206 if Present (Prev_Asps) then
17207 Prev_Asp := First (Prev_Asps);
17208 while Present (Prev_Asp) loop
17209 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17210 return Prev_Asp;
17211 end if;
17212
17213 Next (Prev_Asp);
17214 end loop;
17215 end if;
17216
17217 return Empty;
17218 end Get_Partial_View_Aspect;
17219
17220 -- Local variables
17221
17222 Full_Asps : constant List_Id := Aspect_Specifications (N);
17223 Full_Asp : Node_Id;
17224 Part_Asp : Node_Id;
17225
17226 -- Start of processing for Check_Duplicate_Aspects
17227
17228 begin
17229 if Present (Full_Asps) then
17230 Full_Asp := First (Full_Asps);
17231 while Present (Full_Asp) loop
17232 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17233
17234 -- An aspect and its class-wide counterpart are two distinct
17235 -- aspects and may apply to both views of an entity.
17236
17237 if Present (Part_Asp)
17238 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17239 then
17240 Error_Msg_N
17241 ("aspect already specified in private declaration",
17242 Full_Asp);
17243
17244 Remove (Full_Asp);
17245 return;
17246 end if;
17247
17248 if Has_Discriminants (Prev)
17249 and then not Has_Unknown_Discriminants (Prev)
17250 and then Get_Aspect_Id (Full_Asp) =
17251 Aspect_Implicit_Dereference
17252 then
17253 Error_Msg_N
17254 ("cannot specify aspect if partial view has known "
17255 & "discriminants", Full_Asp);
17256 end if;
17257
17258 Next (Full_Asp);
17259 end loop;
17260 end if;
17261 end Check_Duplicate_Aspects;
17262
17263 ------------------
17264 -- Tag_Mismatch --
17265 ------------------
17266
17267 procedure Tag_Mismatch is
17268 begin
17269 if Sloc (Prev) < Sloc (Id) then
17270 if Ada_Version >= Ada_2012
17271 and then Nkind (N) = N_Private_Type_Declaration
17272 then
17273 Error_Msg_NE
17274 ("declaration of private } must be a tagged type ", Id, Prev);
17275 else
17276 Error_Msg_NE
17277 ("full declaration of } must be a tagged type ", Id, Prev);
17278 end if;
17279
17280 else
17281 if Ada_Version >= Ada_2012
17282 and then Nkind (N) = N_Private_Type_Declaration
17283 then
17284 Error_Msg_NE
17285 ("declaration of private } must be a tagged type ", Prev, Id);
17286 else
17287 Error_Msg_NE
17288 ("full declaration of } must be a tagged type ", Prev, Id);
17289 end if;
17290 end if;
17291 end Tag_Mismatch;
17292
17293 -- Start of processing for Find_Type_Name
17294
17295 begin
17296 -- Find incomplete declaration, if one was given
17297
17298 Prev := Current_Entity_In_Scope (Id);
17299
17300 -- New type declaration
17301
17302 if No (Prev) then
17303 Enter_Name (Id);
17304 return Id;
17305
17306 -- Previous declaration exists
17307
17308 else
17309 Prev_Par := Parent (Prev);
17310
17311 -- Error if not incomplete/private case except if previous
17312 -- declaration is implicit, etc. Enter_Name will emit error if
17313 -- appropriate.
17314
17315 if not Is_Incomplete_Or_Private_Type (Prev) then
17316 Enter_Name (Id);
17317 New_Id := Id;
17318
17319 -- Check invalid completion of private or incomplete type
17320
17321 elsif not Nkind_In (N, N_Full_Type_Declaration,
17322 N_Task_Type_Declaration,
17323 N_Protected_Type_Declaration)
17324 and then
17325 (Ada_Version < Ada_2012
17326 or else not Is_Incomplete_Type (Prev)
17327 or else not Nkind_In (N, N_Private_Type_Declaration,
17328 N_Private_Extension_Declaration))
17329 then
17330 -- Completion must be a full type declarations (RM 7.3(4))
17331
17332 Error_Msg_Sloc := Sloc (Prev);
17333 Error_Msg_NE ("invalid completion of }", Id, Prev);
17334
17335 -- Set scope of Id to avoid cascaded errors. Entity is never
17336 -- examined again, except when saving globals in generics.
17337
17338 Set_Scope (Id, Current_Scope);
17339 New_Id := Id;
17340
17341 -- If this is a repeated incomplete declaration, no further
17342 -- checks are possible.
17343
17344 if Nkind (N) = N_Incomplete_Type_Declaration then
17345 return Prev;
17346 end if;
17347
17348 -- Case of full declaration of incomplete type
17349
17350 elsif Ekind (Prev) = E_Incomplete_Type
17351 and then (Ada_Version < Ada_2012
17352 or else No (Full_View (Prev))
17353 or else not Is_Private_Type (Full_View (Prev)))
17354 then
17355 -- Indicate that the incomplete declaration has a matching full
17356 -- declaration. The defining occurrence of the incomplete
17357 -- declaration remains the visible one, and the procedure
17358 -- Get_Full_View dereferences it whenever the type is used.
17359
17360 if Present (Full_View (Prev)) then
17361 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17362 end if;
17363
17364 Set_Full_View (Prev, Id);
17365 Append_Entity (Id, Current_Scope);
17366 Set_Is_Public (Id, Is_Public (Prev));
17367 Set_Is_Internal (Id);
17368 New_Id := Prev;
17369
17370 -- If the incomplete view is tagged, a class_wide type has been
17371 -- created already. Use it for the private type as well, in order
17372 -- to prevent multiple incompatible class-wide types that may be
17373 -- created for self-referential anonymous access components.
17374
17375 if Is_Tagged_Type (Prev)
17376 and then Present (Class_Wide_Type (Prev))
17377 then
17378 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17379 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17380
17381 -- Type of the class-wide type is the current Id. Previously
17382 -- this was not done for private declarations because of order-
17383 -- of-elaboration issues in the back end, but gigi now handles
17384 -- this properly.
17385
17386 Set_Etype (Class_Wide_Type (Id), Id);
17387 end if;
17388
17389 -- Case of full declaration of private type
17390
17391 else
17392 -- If the private type was a completion of an incomplete type then
17393 -- update Prev to reference the private type
17394
17395 if Ada_Version >= Ada_2012
17396 and then Ekind (Prev) = E_Incomplete_Type
17397 and then Present (Full_View (Prev))
17398 and then Is_Private_Type (Full_View (Prev))
17399 then
17400 Prev := Full_View (Prev);
17401 Prev_Par := Parent (Prev);
17402 end if;
17403
17404 if Nkind (N) = N_Full_Type_Declaration
17405 and then Nkind_In
17406 (Type_Definition (N), N_Record_Definition,
17407 N_Derived_Type_Definition)
17408 and then Interface_Present (Type_Definition (N))
17409 then
17410 Error_Msg_N
17411 ("completion of private type cannot be an interface", N);
17412 end if;
17413
17414 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17415 if Etype (Prev) /= Prev then
17416
17417 -- Prev is a private subtype or a derived type, and needs
17418 -- no completion.
17419
17420 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17421 New_Id := Id;
17422
17423 elsif Ekind (Prev) = E_Private_Type
17424 and then Nkind_In (N, N_Task_Type_Declaration,
17425 N_Protected_Type_Declaration)
17426 then
17427 Error_Msg_N
17428 ("completion of nonlimited type cannot be limited", N);
17429
17430 elsif Ekind (Prev) = E_Record_Type_With_Private
17431 and then Nkind_In (N, N_Task_Type_Declaration,
17432 N_Protected_Type_Declaration)
17433 then
17434 if not Is_Limited_Record (Prev) then
17435 Error_Msg_N
17436 ("completion of nonlimited type cannot be limited", N);
17437
17438 elsif No (Interface_List (N)) then
17439 Error_Msg_N
17440 ("completion of tagged private type must be tagged",
17441 N);
17442 end if;
17443 end if;
17444
17445 -- Ada 2005 (AI-251): Private extension declaration of a task
17446 -- type or a protected type. This case arises when covering
17447 -- interface types.
17448
17449 elsif Nkind_In (N, N_Task_Type_Declaration,
17450 N_Protected_Type_Declaration)
17451 then
17452 null;
17453
17454 elsif Nkind (N) /= N_Full_Type_Declaration
17455 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17456 then
17457 Error_Msg_N
17458 ("full view of private extension must be an extension", N);
17459
17460 elsif not (Abstract_Present (Parent (Prev)))
17461 and then Abstract_Present (Type_Definition (N))
17462 then
17463 Error_Msg_N
17464 ("full view of non-abstract extension cannot be abstract", N);
17465 end if;
17466
17467 if not In_Private_Part (Current_Scope) then
17468 Error_Msg_N
17469 ("declaration of full view must appear in private part", N);
17470 end if;
17471
17472 if Ada_Version >= Ada_2012 then
17473 Check_Duplicate_Aspects;
17474 end if;
17475
17476 Copy_And_Swap (Prev, Id);
17477 Set_Has_Private_Declaration (Prev);
17478 Set_Has_Private_Declaration (Id);
17479
17480 -- AI12-0133: Indicate whether we have a partial view with
17481 -- unknown discriminants, in which case initialization of objects
17482 -- of the type do not receive an invariant check.
17483
17484 Set_Partial_View_Has_Unknown_Discr
17485 (Prev, Has_Unknown_Discriminants (Id));
17486
17487 -- Preserve aspect and iterator flags that may have been set on
17488 -- the partial view.
17489
17490 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17491 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17492
17493 -- If no error, propagate freeze_node from private to full view.
17494 -- It may have been generated for an early operational item.
17495
17496 if Present (Freeze_Node (Id))
17497 and then Serious_Errors_Detected = 0
17498 and then No (Full_View (Id))
17499 then
17500 Set_Freeze_Node (Prev, Freeze_Node (Id));
17501 Set_Freeze_Node (Id, Empty);
17502 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17503 end if;
17504
17505 Set_Full_View (Id, Prev);
17506 New_Id := Prev;
17507 end if;
17508
17509 -- Verify that full declaration conforms to partial one
17510
17511 if Is_Incomplete_Or_Private_Type (Prev)
17512 and then Present (Discriminant_Specifications (Prev_Par))
17513 then
17514 if Present (Discriminant_Specifications (N)) then
17515 if Ekind (Prev) = E_Incomplete_Type then
17516 Check_Discriminant_Conformance (N, Prev, Prev);
17517 else
17518 Check_Discriminant_Conformance (N, Prev, Id);
17519 end if;
17520
17521 else
17522 Error_Msg_N
17523 ("missing discriminants in full type declaration", N);
17524
17525 -- To avoid cascaded errors on subsequent use, share the
17526 -- discriminants of the partial view.
17527
17528 Set_Discriminant_Specifications (N,
17529 Discriminant_Specifications (Prev_Par));
17530 end if;
17531 end if;
17532
17533 -- A prior untagged partial view can have an associated class-wide
17534 -- type due to use of the class attribute, and in this case the full
17535 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17536 -- of incomplete tagged declarations, but we check for it.
17537
17538 if Is_Type (Prev)
17539 and then (Is_Tagged_Type (Prev)
17540 or else Present (Class_Wide_Type (Prev)))
17541 then
17542 -- Ada 2012 (AI05-0162): A private type may be the completion of
17543 -- an incomplete type.
17544
17545 if Ada_Version >= Ada_2012
17546 and then Is_Incomplete_Type (Prev)
17547 and then Nkind_In (N, N_Private_Type_Declaration,
17548 N_Private_Extension_Declaration)
17549 then
17550 -- No need to check private extensions since they are tagged
17551
17552 if Nkind (N) = N_Private_Type_Declaration
17553 and then not Tagged_Present (N)
17554 then
17555 Tag_Mismatch;
17556 end if;
17557
17558 -- The full declaration is either a tagged type (including
17559 -- a synchronized type that implements interfaces) or a
17560 -- type extension, otherwise this is an error.
17561
17562 elsif Nkind_In (N, N_Task_Type_Declaration,
17563 N_Protected_Type_Declaration)
17564 then
17565 if No (Interface_List (N)) and then not Error_Posted (N) then
17566 Tag_Mismatch;
17567 end if;
17568
17569 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17570
17571 -- Indicate that the previous declaration (tagged incomplete
17572 -- or private declaration) requires the same on the full one.
17573
17574 if not Tagged_Present (Type_Definition (N)) then
17575 Tag_Mismatch;
17576 Set_Is_Tagged_Type (Id);
17577 end if;
17578
17579 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17580 if No (Record_Extension_Part (Type_Definition (N))) then
17581 Error_Msg_NE
17582 ("full declaration of } must be a record extension",
17583 Prev, Id);
17584
17585 -- Set some attributes to produce a usable full view
17586
17587 Set_Is_Tagged_Type (Id);
17588 end if;
17589
17590 else
17591 Tag_Mismatch;
17592 end if;
17593 end if;
17594
17595 if Present (Prev)
17596 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17597 and then Present (Premature_Use (Parent (Prev)))
17598 then
17599 Error_Msg_Sloc := Sloc (N);
17600 Error_Msg_N
17601 ("\full declaration #", Premature_Use (Parent (Prev)));
17602 end if;
17603
17604 return New_Id;
17605 end if;
17606 end Find_Type_Name;
17607
17608 -------------------------
17609 -- Find_Type_Of_Object --
17610 -------------------------
17611
17612 function Find_Type_Of_Object
17613 (Obj_Def : Node_Id;
17614 Related_Nod : Node_Id) return Entity_Id
17615 is
17616 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17617 P : Node_Id := Parent (Obj_Def);
17618 T : Entity_Id;
17619 Nam : Name_Id;
17620
17621 begin
17622 -- If the parent is a component_definition node we climb to the
17623 -- component_declaration node
17624
17625 if Nkind (P) = N_Component_Definition then
17626 P := Parent (P);
17627 end if;
17628
17629 -- Case of an anonymous array subtype
17630
17631 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17632 N_Unconstrained_Array_Definition)
17633 then
17634 T := Empty;
17635 Array_Type_Declaration (T, Obj_Def);
17636
17637 -- Create an explicit subtype whenever possible
17638
17639 elsif Nkind (P) /= N_Component_Declaration
17640 and then Def_Kind = N_Subtype_Indication
17641 then
17642 -- Base name of subtype on object name, which will be unique in
17643 -- the current scope.
17644
17645 -- If this is a duplicate declaration, return base type, to avoid
17646 -- generating duplicate anonymous types.
17647
17648 if Error_Posted (P) then
17649 Analyze (Subtype_Mark (Obj_Def));
17650 return Entity (Subtype_Mark (Obj_Def));
17651 end if;
17652
17653 Nam :=
17654 New_External_Name
17655 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17656
17657 T := Make_Defining_Identifier (Sloc (P), Nam);
17658
17659 Insert_Action (Obj_Def,
17660 Make_Subtype_Declaration (Sloc (P),
17661 Defining_Identifier => T,
17662 Subtype_Indication => Relocate_Node (Obj_Def)));
17663
17664 -- This subtype may need freezing, and this will not be done
17665 -- automatically if the object declaration is not in declarative
17666 -- part. Since this is an object declaration, the type cannot always
17667 -- be frozen here. Deferred constants do not freeze their type
17668 -- (which often enough will be private).
17669
17670 if Nkind (P) = N_Object_Declaration
17671 and then Constant_Present (P)
17672 and then No (Expression (P))
17673 then
17674 null;
17675
17676 -- Here we freeze the base type of object type to catch premature use
17677 -- of discriminated private type without a full view.
17678
17679 else
17680 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17681 end if;
17682
17683 -- Ada 2005 AI-406: the object definition in an object declaration
17684 -- can be an access definition.
17685
17686 elsif Def_Kind = N_Access_Definition then
17687 T := Access_Definition (Related_Nod, Obj_Def);
17688
17689 Set_Is_Local_Anonymous_Access
17690 (T,
17691 V => (Ada_Version < Ada_2012)
17692 or else (Nkind (P) /= N_Object_Declaration)
17693 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17694
17695 -- Otherwise, the object definition is just a subtype_mark
17696
17697 else
17698 T := Process_Subtype (Obj_Def, Related_Nod);
17699
17700 -- If expansion is disabled an object definition that is an aggregate
17701 -- will not get expanded and may lead to scoping problems in the back
17702 -- end, if the object is referenced in an inner scope. In that case
17703 -- create an itype reference for the object definition now. This
17704 -- may be redundant in some cases, but harmless.
17705
17706 if Is_Itype (T)
17707 and then Nkind (Related_Nod) = N_Object_Declaration
17708 and then ASIS_Mode
17709 then
17710 Build_Itype_Reference (T, Related_Nod);
17711 end if;
17712 end if;
17713
17714 return T;
17715 end Find_Type_Of_Object;
17716
17717 --------------------------------
17718 -- Find_Type_Of_Subtype_Indic --
17719 --------------------------------
17720
17721 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17722 Typ : Entity_Id;
17723
17724 begin
17725 -- Case of subtype mark with a constraint
17726
17727 if Nkind (S) = N_Subtype_Indication then
17728 Find_Type (Subtype_Mark (S));
17729 Typ := Entity (Subtype_Mark (S));
17730
17731 if not
17732 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17733 then
17734 Error_Msg_N
17735 ("incorrect constraint for this kind of type", Constraint (S));
17736 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17737 end if;
17738
17739 -- Otherwise we have a subtype mark without a constraint
17740
17741 elsif Error_Posted (S) then
17742 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17743 return Any_Type;
17744
17745 else
17746 Find_Type (S);
17747 Typ := Entity (S);
17748 end if;
17749
17750 -- Check No_Wide_Characters restriction
17751
17752 Check_Wide_Character_Restriction (Typ, S);
17753
17754 return Typ;
17755 end Find_Type_Of_Subtype_Indic;
17756
17757 -------------------------------------
17758 -- Floating_Point_Type_Declaration --
17759 -------------------------------------
17760
17761 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17762 Digs : constant Node_Id := Digits_Expression (Def);
17763 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17764 Digs_Val : Uint;
17765 Base_Typ : Entity_Id;
17766 Implicit_Base : Entity_Id;
17767 Bound : Node_Id;
17768
17769 function Can_Derive_From (E : Entity_Id) return Boolean;
17770 -- Find if given digits value, and possibly a specified range, allows
17771 -- derivation from specified type
17772
17773 function Find_Base_Type return Entity_Id;
17774 -- Find a predefined base type that Def can derive from, or generate
17775 -- an error and substitute Long_Long_Float if none exists.
17776
17777 ---------------------
17778 -- Can_Derive_From --
17779 ---------------------
17780
17781 function Can_Derive_From (E : Entity_Id) return Boolean is
17782 Spec : constant Entity_Id := Real_Range_Specification (Def);
17783
17784 begin
17785 -- Check specified "digits" constraint
17786
17787 if Digs_Val > Digits_Value (E) then
17788 return False;
17789 end if;
17790
17791 -- Check for matching range, if specified
17792
17793 if Present (Spec) then
17794 if Expr_Value_R (Type_Low_Bound (E)) >
17795 Expr_Value_R (Low_Bound (Spec))
17796 then
17797 return False;
17798 end if;
17799
17800 if Expr_Value_R (Type_High_Bound (E)) <
17801 Expr_Value_R (High_Bound (Spec))
17802 then
17803 return False;
17804 end if;
17805 end if;
17806
17807 return True;
17808 end Can_Derive_From;
17809
17810 --------------------
17811 -- Find_Base_Type --
17812 --------------------
17813
17814 function Find_Base_Type return Entity_Id is
17815 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17816
17817 begin
17818 -- Iterate over the predefined types in order, returning the first
17819 -- one that Def can derive from.
17820
17821 while Present (Choice) loop
17822 if Can_Derive_From (Node (Choice)) then
17823 return Node (Choice);
17824 end if;
17825
17826 Next_Elmt (Choice);
17827 end loop;
17828
17829 -- If we can't derive from any existing type, use Long_Long_Float
17830 -- and give appropriate message explaining the problem.
17831
17832 if Digs_Val > Max_Digs_Val then
17833 -- It might be the case that there is a type with the requested
17834 -- range, just not the combination of digits and range.
17835
17836 Error_Msg_N
17837 ("no predefined type has requested range and precision",
17838 Real_Range_Specification (Def));
17839
17840 else
17841 Error_Msg_N
17842 ("range too large for any predefined type",
17843 Real_Range_Specification (Def));
17844 end if;
17845
17846 return Standard_Long_Long_Float;
17847 end Find_Base_Type;
17848
17849 -- Start of processing for Floating_Point_Type_Declaration
17850
17851 begin
17852 Check_Restriction (No_Floating_Point, Def);
17853
17854 -- Create an implicit base type
17855
17856 Implicit_Base :=
17857 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17858
17859 -- Analyze and verify digits value
17860
17861 Analyze_And_Resolve (Digs, Any_Integer);
17862 Check_Digits_Expression (Digs);
17863 Digs_Val := Expr_Value (Digs);
17864
17865 -- Process possible range spec and find correct type to derive from
17866
17867 Process_Real_Range_Specification (Def);
17868
17869 -- Check that requested number of digits is not too high.
17870
17871 if Digs_Val > Max_Digs_Val then
17872
17873 -- The check for Max_Base_Digits may be somewhat expensive, as it
17874 -- requires reading System, so only do it when necessary.
17875
17876 declare
17877 Max_Base_Digits : constant Uint :=
17878 Expr_Value
17879 (Expression
17880 (Parent (RTE (RE_Max_Base_Digits))));
17881
17882 begin
17883 if Digs_Val > Max_Base_Digits then
17884 Error_Msg_Uint_1 := Max_Base_Digits;
17885 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17886
17887 elsif No (Real_Range_Specification (Def)) then
17888 Error_Msg_Uint_1 := Max_Digs_Val;
17889 Error_Msg_N ("types with more than ^ digits need range spec "
17890 & "(RM 3.5.7(6))", Digs);
17891 end if;
17892 end;
17893 end if;
17894
17895 -- Find a suitable type to derive from or complain and use a substitute
17896
17897 Base_Typ := Find_Base_Type;
17898
17899 -- If there are bounds given in the declaration use them as the bounds
17900 -- of the type, otherwise use the bounds of the predefined base type
17901 -- that was chosen based on the Digits value.
17902
17903 if Present (Real_Range_Specification (Def)) then
17904 Set_Scalar_Range (T, Real_Range_Specification (Def));
17905 Set_Is_Constrained (T);
17906
17907 -- The bounds of this range must be converted to machine numbers
17908 -- in accordance with RM 4.9(38).
17909
17910 Bound := Type_Low_Bound (T);
17911
17912 if Nkind (Bound) = N_Real_Literal then
17913 Set_Realval
17914 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17915 Set_Is_Machine_Number (Bound);
17916 end if;
17917
17918 Bound := Type_High_Bound (T);
17919
17920 if Nkind (Bound) = N_Real_Literal then
17921 Set_Realval
17922 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17923 Set_Is_Machine_Number (Bound);
17924 end if;
17925
17926 else
17927 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17928 end if;
17929
17930 -- Complete definition of implicit base and declared first subtype. The
17931 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17932 -- are not clobbered when the floating point type acts as a full view of
17933 -- a private type.
17934
17935 Set_Etype (Implicit_Base, Base_Typ);
17936 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17937 Set_Size_Info (Implicit_Base, Base_Typ);
17938 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17939 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17940 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17941 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17942
17943 Set_Ekind (T, E_Floating_Point_Subtype);
17944 Set_Etype (T, Implicit_Base);
17945 Set_Size_Info (T, Implicit_Base);
17946 Set_RM_Size (T, RM_Size (Implicit_Base));
17947 Inherit_Rep_Item_Chain (T, Implicit_Base);
17948 Set_Digits_Value (T, Digs_Val);
17949 end Floating_Point_Type_Declaration;
17950
17951 ----------------------------
17952 -- Get_Discriminant_Value --
17953 ----------------------------
17954
17955 -- This is the situation:
17956
17957 -- There is a non-derived type
17958
17959 -- type T0 (Dx, Dy, Dz...)
17960
17961 -- There are zero or more levels of derivation, with each derivation
17962 -- either purely inheriting the discriminants, or defining its own.
17963
17964 -- type Ti is new Ti-1
17965 -- or
17966 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17967 -- or
17968 -- subtype Ti is ...
17969
17970 -- The subtype issue is avoided by the use of Original_Record_Component,
17971 -- and the fact that derived subtypes also derive the constraints.
17972
17973 -- This chain leads back from
17974
17975 -- Typ_For_Constraint
17976
17977 -- Typ_For_Constraint has discriminants, and the value for each
17978 -- discriminant is given by its corresponding Elmt of Constraints.
17979
17980 -- Discriminant is some discriminant in this hierarchy
17981
17982 -- We need to return its value
17983
17984 -- We do this by recursively searching each level, and looking for
17985 -- Discriminant. Once we get to the bottom, we start backing up
17986 -- returning the value for it which may in turn be a discriminant
17987 -- further up, so on the backup we continue the substitution.
17988
17989 function Get_Discriminant_Value
17990 (Discriminant : Entity_Id;
17991 Typ_For_Constraint : Entity_Id;
17992 Constraint : Elist_Id) return Node_Id
17993 is
17994 function Root_Corresponding_Discriminant
17995 (Discr : Entity_Id) return Entity_Id;
17996 -- Given a discriminant, traverse the chain of inherited discriminants
17997 -- and return the topmost discriminant.
17998
17999 function Search_Derivation_Levels
18000 (Ti : Entity_Id;
18001 Discrim_Values : Elist_Id;
18002 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18003 -- This is the routine that performs the recursive search of levels
18004 -- as described above.
18005
18006 -------------------------------------
18007 -- Root_Corresponding_Discriminant --
18008 -------------------------------------
18009
18010 function Root_Corresponding_Discriminant
18011 (Discr : Entity_Id) return Entity_Id
18012 is
18013 D : Entity_Id;
18014
18015 begin
18016 D := Discr;
18017 while Present (Corresponding_Discriminant (D)) loop
18018 D := Corresponding_Discriminant (D);
18019 end loop;
18020
18021 return D;
18022 end Root_Corresponding_Discriminant;
18023
18024 ------------------------------
18025 -- Search_Derivation_Levels --
18026 ------------------------------
18027
18028 function Search_Derivation_Levels
18029 (Ti : Entity_Id;
18030 Discrim_Values : Elist_Id;
18031 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18032 is
18033 Assoc : Elmt_Id;
18034 Disc : Entity_Id;
18035 Result : Node_Or_Entity_Id;
18036 Result_Entity : Node_Id;
18037
18038 begin
18039 -- If inappropriate type, return Error, this happens only in
18040 -- cascaded error situations, and we want to avoid a blow up.
18041
18042 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18043 return Error;
18044 end if;
18045
18046 -- Look deeper if possible. Use Stored_Constraints only for
18047 -- untagged types. For tagged types use the given constraint.
18048 -- This asymmetry needs explanation???
18049
18050 if not Stored_Discrim_Values
18051 and then Present (Stored_Constraint (Ti))
18052 and then not Is_Tagged_Type (Ti)
18053 then
18054 Result :=
18055 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18056
18057 else
18058 declare
18059 Td : Entity_Id := Etype (Ti);
18060
18061 begin
18062 -- If the parent type is private, the full view may include
18063 -- renamed discriminants, and it is those stored values that
18064 -- may be needed (the partial view never has more information
18065 -- than the full view).
18066
18067 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18068 Td := Full_View (Td);
18069 end if;
18070
18071 if Td = Ti then
18072 Result := Discriminant;
18073
18074 else
18075 if Present (Stored_Constraint (Ti)) then
18076 Result :=
18077 Search_Derivation_Levels
18078 (Td, Stored_Constraint (Ti), True);
18079 else
18080 Result :=
18081 Search_Derivation_Levels
18082 (Td, Discrim_Values, Stored_Discrim_Values);
18083 end if;
18084 end if;
18085 end;
18086 end if;
18087
18088 -- Extra underlying places to search, if not found above. For
18089 -- concurrent types, the relevant discriminant appears in the
18090 -- corresponding record. For a type derived from a private type
18091 -- without discriminant, the full view inherits the discriminants
18092 -- of the full view of the parent.
18093
18094 if Result = Discriminant then
18095 if Is_Concurrent_Type (Ti)
18096 and then Present (Corresponding_Record_Type (Ti))
18097 then
18098 Result :=
18099 Search_Derivation_Levels (
18100 Corresponding_Record_Type (Ti),
18101 Discrim_Values,
18102 Stored_Discrim_Values);
18103
18104 elsif Is_Private_Type (Ti)
18105 and then not Has_Discriminants (Ti)
18106 and then Present (Full_View (Ti))
18107 and then Etype (Full_View (Ti)) /= Ti
18108 then
18109 Result :=
18110 Search_Derivation_Levels (
18111 Full_View (Ti),
18112 Discrim_Values,
18113 Stored_Discrim_Values);
18114 end if;
18115 end if;
18116
18117 -- If Result is not a (reference to a) discriminant, return it,
18118 -- otherwise set Result_Entity to the discriminant.
18119
18120 if Nkind (Result) = N_Defining_Identifier then
18121 pragma Assert (Result = Discriminant);
18122 Result_Entity := Result;
18123
18124 else
18125 if not Denotes_Discriminant (Result) then
18126 return Result;
18127 end if;
18128
18129 Result_Entity := Entity (Result);
18130 end if;
18131
18132 -- See if this level of derivation actually has discriminants because
18133 -- tagged derivations can add them, hence the lower levels need not
18134 -- have any.
18135
18136 if not Has_Discriminants (Ti) then
18137 return Result;
18138 end if;
18139
18140 -- Scan Ti's discriminants for Result_Entity, and return its
18141 -- corresponding value, if any.
18142
18143 Result_Entity := Original_Record_Component (Result_Entity);
18144
18145 Assoc := First_Elmt (Discrim_Values);
18146
18147 if Stored_Discrim_Values then
18148 Disc := First_Stored_Discriminant (Ti);
18149 else
18150 Disc := First_Discriminant (Ti);
18151 end if;
18152
18153 while Present (Disc) loop
18154
18155 -- If no further associations return the discriminant, value will
18156 -- be found on the second pass.
18157
18158 if No (Assoc) then
18159 return Result;
18160 end if;
18161
18162 if Original_Record_Component (Disc) = Result_Entity then
18163 return Node (Assoc);
18164 end if;
18165
18166 Next_Elmt (Assoc);
18167
18168 if Stored_Discrim_Values then
18169 Next_Stored_Discriminant (Disc);
18170 else
18171 Next_Discriminant (Disc);
18172 end if;
18173 end loop;
18174
18175 -- Could not find it
18176
18177 return Result;
18178 end Search_Derivation_Levels;
18179
18180 -- Local Variables
18181
18182 Result : Node_Or_Entity_Id;
18183
18184 -- Start of processing for Get_Discriminant_Value
18185
18186 begin
18187 -- ??? This routine is a gigantic mess and will be deleted. For the
18188 -- time being just test for the trivial case before calling recurse.
18189
18190 -- We are now celebrating the 20th anniversary of this comment!
18191
18192 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18193 declare
18194 D : Entity_Id;
18195 E : Elmt_Id;
18196
18197 begin
18198 D := First_Discriminant (Typ_For_Constraint);
18199 E := First_Elmt (Constraint);
18200 while Present (D) loop
18201 if Chars (D) = Chars (Discriminant) then
18202 return Node (E);
18203 end if;
18204
18205 Next_Discriminant (D);
18206 Next_Elmt (E);
18207 end loop;
18208 end;
18209 end if;
18210
18211 Result := Search_Derivation_Levels
18212 (Typ_For_Constraint, Constraint, False);
18213
18214 -- ??? hack to disappear when this routine is gone
18215
18216 if Nkind (Result) = N_Defining_Identifier then
18217 declare
18218 D : Entity_Id;
18219 E : Elmt_Id;
18220
18221 begin
18222 D := First_Discriminant (Typ_For_Constraint);
18223 E := First_Elmt (Constraint);
18224 while Present (D) loop
18225 if Root_Corresponding_Discriminant (D) = Discriminant then
18226 return Node (E);
18227 end if;
18228
18229 Next_Discriminant (D);
18230 Next_Elmt (E);
18231 end loop;
18232 end;
18233 end if;
18234
18235 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18236 return Result;
18237 end Get_Discriminant_Value;
18238
18239 --------------------------
18240 -- Has_Range_Constraint --
18241 --------------------------
18242
18243 function Has_Range_Constraint (N : Node_Id) return Boolean is
18244 C : constant Node_Id := Constraint (N);
18245
18246 begin
18247 if Nkind (C) = N_Range_Constraint then
18248 return True;
18249
18250 elsif Nkind (C) = N_Digits_Constraint then
18251 return
18252 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18253 or else Present (Range_Constraint (C));
18254
18255 elsif Nkind (C) = N_Delta_Constraint then
18256 return Present (Range_Constraint (C));
18257
18258 else
18259 return False;
18260 end if;
18261 end Has_Range_Constraint;
18262
18263 ------------------------
18264 -- Inherit_Components --
18265 ------------------------
18266
18267 function Inherit_Components
18268 (N : Node_Id;
18269 Parent_Base : Entity_Id;
18270 Derived_Base : Entity_Id;
18271 Is_Tagged : Boolean;
18272 Inherit_Discr : Boolean;
18273 Discs : Elist_Id) return Elist_Id
18274 is
18275 Assoc_List : constant Elist_Id := New_Elmt_List;
18276
18277 procedure Inherit_Component
18278 (Old_C : Entity_Id;
18279 Plain_Discrim : Boolean := False;
18280 Stored_Discrim : Boolean := False);
18281 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18282 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18283 -- True, Old_C is a stored discriminant. If they are both false then
18284 -- Old_C is a regular component.
18285
18286 -----------------------
18287 -- Inherit_Component --
18288 -----------------------
18289
18290 procedure Inherit_Component
18291 (Old_C : Entity_Id;
18292 Plain_Discrim : Boolean := False;
18293 Stored_Discrim : Boolean := False)
18294 is
18295 procedure Set_Anonymous_Type (Id : Entity_Id);
18296 -- Id denotes the entity of an access discriminant or anonymous
18297 -- access component. Set the type of Id to either the same type of
18298 -- Old_C or create a new one depending on whether the parent and
18299 -- the child types are in the same scope.
18300
18301 ------------------------
18302 -- Set_Anonymous_Type --
18303 ------------------------
18304
18305 procedure Set_Anonymous_Type (Id : Entity_Id) is
18306 Old_Typ : constant Entity_Id := Etype (Old_C);
18307
18308 begin
18309 if Scope (Parent_Base) = Scope (Derived_Base) then
18310 Set_Etype (Id, Old_Typ);
18311
18312 -- The parent and the derived type are in two different scopes.
18313 -- Reuse the type of the original discriminant / component by
18314 -- copying it in order to preserve all attributes.
18315
18316 else
18317 declare
18318 Typ : constant Entity_Id := New_Copy (Old_Typ);
18319
18320 begin
18321 Set_Etype (Id, Typ);
18322
18323 -- Since we do not generate component declarations for
18324 -- inherited components, associate the itype with the
18325 -- derived type.
18326
18327 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18328 Set_Scope (Typ, Derived_Base);
18329 end;
18330 end if;
18331 end Set_Anonymous_Type;
18332
18333 -- Local variables and constants
18334
18335 New_C : constant Entity_Id := New_Copy (Old_C);
18336
18337 Corr_Discrim : Entity_Id;
18338 Discrim : Entity_Id;
18339
18340 -- Start of processing for Inherit_Component
18341
18342 begin
18343 pragma Assert (not Is_Tagged or not Stored_Discrim);
18344
18345 Set_Parent (New_C, Parent (Old_C));
18346
18347 -- Regular discriminants and components must be inserted in the scope
18348 -- of the Derived_Base. Do it here.
18349
18350 if not Stored_Discrim then
18351 Enter_Name (New_C);
18352 end if;
18353
18354 -- For tagged types the Original_Record_Component must point to
18355 -- whatever this field was pointing to in the parent type. This has
18356 -- already been achieved by the call to New_Copy above.
18357
18358 if not Is_Tagged then
18359 Set_Original_Record_Component (New_C, New_C);
18360 Set_Corresponding_Record_Component (New_C, Old_C);
18361 end if;
18362
18363 -- Set the proper type of an access discriminant
18364
18365 if Ekind (New_C) = E_Discriminant
18366 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18367 then
18368 Set_Anonymous_Type (New_C);
18369 end if;
18370
18371 -- If we have inherited a component then see if its Etype contains
18372 -- references to Parent_Base discriminants. In this case, replace
18373 -- these references with the constraints given in Discs. We do not
18374 -- do this for the partial view of private types because this is
18375 -- not needed (only the components of the full view will be used
18376 -- for code generation) and cause problem. We also avoid this
18377 -- transformation in some error situations.
18378
18379 if Ekind (New_C) = E_Component then
18380
18381 -- Set the proper type of an anonymous access component
18382
18383 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18384 Set_Anonymous_Type (New_C);
18385
18386 elsif (Is_Private_Type (Derived_Base)
18387 and then not Is_Generic_Type (Derived_Base))
18388 or else (Is_Empty_Elmt_List (Discs)
18389 and then not Expander_Active)
18390 then
18391 Set_Etype (New_C, Etype (Old_C));
18392
18393 else
18394 -- The current component introduces a circularity of the
18395 -- following kind:
18396
18397 -- limited with Pack_2;
18398 -- package Pack_1 is
18399 -- type T_1 is tagged record
18400 -- Comp : access Pack_2.T_2;
18401 -- ...
18402 -- end record;
18403 -- end Pack_1;
18404
18405 -- with Pack_1;
18406 -- package Pack_2 is
18407 -- type T_2 is new Pack_1.T_1 with ...;
18408 -- end Pack_2;
18409
18410 Set_Etype
18411 (New_C,
18412 Constrain_Component_Type
18413 (Old_C, Derived_Base, N, Parent_Base, Discs));
18414 end if;
18415 end if;
18416
18417 -- In derived tagged types it is illegal to reference a non
18418 -- discriminant component in the parent type. To catch this, mark
18419 -- these components with an Ekind of E_Void. This will be reset in
18420 -- Record_Type_Definition after processing the record extension of
18421 -- the derived type.
18422
18423 -- If the declaration is a private extension, there is no further
18424 -- record extension to process, and the components retain their
18425 -- current kind, because they are visible at this point.
18426
18427 if Is_Tagged and then Ekind (New_C) = E_Component
18428 and then Nkind (N) /= N_Private_Extension_Declaration
18429 then
18430 Set_Ekind (New_C, E_Void);
18431 end if;
18432
18433 if Plain_Discrim then
18434 Set_Corresponding_Discriminant (New_C, Old_C);
18435 Build_Discriminal (New_C);
18436
18437 -- If we are explicitly inheriting a stored discriminant it will be
18438 -- completely hidden.
18439
18440 elsif Stored_Discrim then
18441 Set_Corresponding_Discriminant (New_C, Empty);
18442 Set_Discriminal (New_C, Empty);
18443 Set_Is_Completely_Hidden (New_C);
18444
18445 -- Set the Original_Record_Component of each discriminant in the
18446 -- derived base to point to the corresponding stored that we just
18447 -- created.
18448
18449 Discrim := First_Discriminant (Derived_Base);
18450 while Present (Discrim) loop
18451 Corr_Discrim := Corresponding_Discriminant (Discrim);
18452
18453 -- Corr_Discrim could be missing in an error situation
18454
18455 if Present (Corr_Discrim)
18456 and then Original_Record_Component (Corr_Discrim) = Old_C
18457 then
18458 Set_Original_Record_Component (Discrim, New_C);
18459 Set_Corresponding_Record_Component (Discrim, Empty);
18460 end if;
18461
18462 Next_Discriminant (Discrim);
18463 end loop;
18464
18465 Append_Entity (New_C, Derived_Base);
18466 end if;
18467
18468 if not Is_Tagged then
18469 Append_Elmt (Old_C, Assoc_List);
18470 Append_Elmt (New_C, Assoc_List);
18471 end if;
18472 end Inherit_Component;
18473
18474 -- Variables local to Inherit_Component
18475
18476 Loc : constant Source_Ptr := Sloc (N);
18477
18478 Parent_Discrim : Entity_Id;
18479 Stored_Discrim : Entity_Id;
18480 D : Entity_Id;
18481 Component : Entity_Id;
18482
18483 -- Start of processing for Inherit_Components
18484
18485 begin
18486 if not Is_Tagged then
18487 Append_Elmt (Parent_Base, Assoc_List);
18488 Append_Elmt (Derived_Base, Assoc_List);
18489 end if;
18490
18491 -- Inherit parent discriminants if needed
18492
18493 if Inherit_Discr then
18494 Parent_Discrim := First_Discriminant (Parent_Base);
18495 while Present (Parent_Discrim) loop
18496 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18497 Next_Discriminant (Parent_Discrim);
18498 end loop;
18499 end if;
18500
18501 -- Create explicit stored discrims for untagged types when necessary
18502
18503 if not Has_Unknown_Discriminants (Derived_Base)
18504 and then Has_Discriminants (Parent_Base)
18505 and then not Is_Tagged
18506 and then
18507 (not Inherit_Discr
18508 or else First_Discriminant (Parent_Base) /=
18509 First_Stored_Discriminant (Parent_Base))
18510 then
18511 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18512 while Present (Stored_Discrim) loop
18513 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18514 Next_Stored_Discriminant (Stored_Discrim);
18515 end loop;
18516 end if;
18517
18518 -- See if we can apply the second transformation for derived types, as
18519 -- explained in point 6. in the comments above Build_Derived_Record_Type
18520 -- This is achieved by appending Derived_Base discriminants into Discs,
18521 -- which has the side effect of returning a non empty Discs list to the
18522 -- caller of Inherit_Components, which is what we want. This must be
18523 -- done for private derived types if there are explicit stored
18524 -- discriminants, to ensure that we can retrieve the values of the
18525 -- constraints provided in the ancestors.
18526
18527 if Inherit_Discr
18528 and then Is_Empty_Elmt_List (Discs)
18529 and then Present (First_Discriminant (Derived_Base))
18530 and then
18531 (not Is_Private_Type (Derived_Base)
18532 or else Is_Completely_Hidden
18533 (First_Stored_Discriminant (Derived_Base))
18534 or else Is_Generic_Type (Derived_Base))
18535 then
18536 D := First_Discriminant (Derived_Base);
18537 while Present (D) loop
18538 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18539 Next_Discriminant (D);
18540 end loop;
18541 end if;
18542
18543 -- Finally, inherit non-discriminant components unless they are not
18544 -- visible because defined or inherited from the full view of the
18545 -- parent. Don't inherit the _parent field of the parent type.
18546
18547 Component := First_Entity (Parent_Base);
18548 while Present (Component) loop
18549
18550 -- Ada 2005 (AI-251): Do not inherit components associated with
18551 -- secondary tags of the parent.
18552
18553 if Ekind (Component) = E_Component
18554 and then Present (Related_Type (Component))
18555 then
18556 null;
18557
18558 elsif Ekind (Component) /= E_Component
18559 or else Chars (Component) = Name_uParent
18560 then
18561 null;
18562
18563 -- If the derived type is within the parent type's declarative
18564 -- region, then the components can still be inherited even though
18565 -- they aren't visible at this point. This can occur for cases
18566 -- such as within public child units where the components must
18567 -- become visible upon entering the child unit's private part.
18568
18569 elsif not Is_Visible_Component (Component)
18570 and then not In_Open_Scopes (Scope (Parent_Base))
18571 then
18572 null;
18573
18574 elsif Ekind_In (Derived_Base, E_Private_Type,
18575 E_Limited_Private_Type)
18576 then
18577 null;
18578
18579 else
18580 Inherit_Component (Component);
18581 end if;
18582
18583 Next_Entity (Component);
18584 end loop;
18585
18586 -- For tagged derived types, inherited discriminants cannot be used in
18587 -- component declarations of the record extension part. To achieve this
18588 -- we mark the inherited discriminants as not visible.
18589
18590 if Is_Tagged and then Inherit_Discr then
18591 D := First_Discriminant (Derived_Base);
18592 while Present (D) loop
18593 Set_Is_Immediately_Visible (D, False);
18594 Next_Discriminant (D);
18595 end loop;
18596 end if;
18597
18598 return Assoc_List;
18599 end Inherit_Components;
18600
18601 -----------------------------
18602 -- Inherit_Predicate_Flags --
18603 -----------------------------
18604
18605 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18606 begin
18607 if Present (Predicate_Function (Subt)) then
18608 return;
18609 end if;
18610
18611 Set_Has_Predicates (Subt, Has_Predicates (Par));
18612 Set_Has_Static_Predicate_Aspect
18613 (Subt, Has_Static_Predicate_Aspect (Par));
18614 Set_Has_Dynamic_Predicate_Aspect
18615 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18616
18617 -- A named subtype does not inherit the predicate function of its
18618 -- parent but an itype declared for a loop index needs the discrete
18619 -- predicate information of its parent to execute the loop properly.
18620
18621 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18622 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18623
18624 if Has_Static_Predicate (Par) then
18625 Set_Static_Discrete_Predicate
18626 (Subt, Static_Discrete_Predicate (Par));
18627 end if;
18628 end if;
18629 end Inherit_Predicate_Flags;
18630
18631 ----------------------
18632 -- Is_EVF_Procedure --
18633 ----------------------
18634
18635 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18636 Formal : Entity_Id;
18637
18638 begin
18639 -- Examine the formals of an Extensions_Visible False procedure looking
18640 -- for a controlling OUT parameter.
18641
18642 if Ekind (Subp) = E_Procedure
18643 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18644 then
18645 Formal := First_Formal (Subp);
18646 while Present (Formal) loop
18647 if Ekind (Formal) = E_Out_Parameter
18648 and then Is_Controlling_Formal (Formal)
18649 then
18650 return True;
18651 end if;
18652
18653 Next_Formal (Formal);
18654 end loop;
18655 end if;
18656
18657 return False;
18658 end Is_EVF_Procedure;
18659
18660 -----------------------
18661 -- Is_Null_Extension --
18662 -----------------------
18663
18664 function Is_Null_Extension (T : Entity_Id) return Boolean is
18665 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18666 Comp_List : Node_Id;
18667 Comp : Node_Id;
18668
18669 begin
18670 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18671 or else not Is_Tagged_Type (T)
18672 or else Nkind (Type_Definition (Type_Decl)) /=
18673 N_Derived_Type_Definition
18674 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18675 then
18676 return False;
18677 end if;
18678
18679 Comp_List :=
18680 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18681
18682 if Present (Discriminant_Specifications (Type_Decl)) then
18683 return False;
18684
18685 elsif Present (Comp_List)
18686 and then Is_Non_Empty_List (Component_Items (Comp_List))
18687 then
18688 Comp := First (Component_Items (Comp_List));
18689
18690 -- Only user-defined components are relevant. The component list
18691 -- may also contain a parent component and internal components
18692 -- corresponding to secondary tags, but these do not determine
18693 -- whether this is a null extension.
18694
18695 while Present (Comp) loop
18696 if Comes_From_Source (Comp) then
18697 return False;
18698 end if;
18699
18700 Next (Comp);
18701 end loop;
18702
18703 return True;
18704
18705 else
18706 return True;
18707 end if;
18708 end Is_Null_Extension;
18709
18710 ------------------------------
18711 -- Is_Valid_Constraint_Kind --
18712 ------------------------------
18713
18714 function Is_Valid_Constraint_Kind
18715 (T_Kind : Type_Kind;
18716 Constraint_Kind : Node_Kind) return Boolean
18717 is
18718 begin
18719 case T_Kind is
18720 when Enumeration_Kind
18721 | Integer_Kind
18722 =>
18723 return Constraint_Kind = N_Range_Constraint;
18724
18725 when Decimal_Fixed_Point_Kind =>
18726 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18727 N_Range_Constraint);
18728
18729 when Ordinary_Fixed_Point_Kind =>
18730 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18731 N_Range_Constraint);
18732
18733 when Float_Kind =>
18734 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18735 N_Range_Constraint);
18736
18737 when Access_Kind
18738 | Array_Kind
18739 | Class_Wide_Kind
18740 | Concurrent_Kind
18741 | Private_Kind
18742 | E_Incomplete_Type
18743 | E_Record_Subtype
18744 | E_Record_Type
18745 =>
18746 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18747
18748 when others =>
18749 return True; -- Error will be detected later
18750 end case;
18751 end Is_Valid_Constraint_Kind;
18752
18753 --------------------------
18754 -- Is_Visible_Component --
18755 --------------------------
18756
18757 function Is_Visible_Component
18758 (C : Entity_Id;
18759 N : Node_Id := Empty) return Boolean
18760 is
18761 Original_Comp : Entity_Id := Empty;
18762 Original_Type : Entity_Id;
18763 Type_Scope : Entity_Id;
18764
18765 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18766 -- Check whether parent type of inherited component is declared locally,
18767 -- possibly within a nested package or instance. The current scope is
18768 -- the derived record itself.
18769
18770 -------------------
18771 -- Is_Local_Type --
18772 -------------------
18773
18774 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18775 Scop : Entity_Id;
18776
18777 begin
18778 Scop := Scope (Typ);
18779 while Present (Scop)
18780 and then Scop /= Standard_Standard
18781 loop
18782 if Scop = Scope (Current_Scope) then
18783 return True;
18784 end if;
18785
18786 Scop := Scope (Scop);
18787 end loop;
18788
18789 return False;
18790 end Is_Local_Type;
18791
18792 -- Start of processing for Is_Visible_Component
18793
18794 begin
18795 if Ekind_In (C, E_Component, E_Discriminant) then
18796 Original_Comp := Original_Record_Component (C);
18797 end if;
18798
18799 if No (Original_Comp) then
18800
18801 -- Premature usage, or previous error
18802
18803 return False;
18804
18805 else
18806 Original_Type := Scope (Original_Comp);
18807 Type_Scope := Scope (Base_Type (Scope (C)));
18808 end if;
18809
18810 -- This test only concerns tagged types
18811
18812 if not Is_Tagged_Type (Original_Type) then
18813
18814 -- Check if this is a renamed discriminant (hidden either by the
18815 -- derived type or by some ancestor), unless we are analyzing code
18816 -- generated by the expander since it may reference such components
18817 -- (for example see the expansion of Deep_Adjust).
18818
18819 if Ekind (C) = E_Discriminant and then Present (N) then
18820 return
18821 not Comes_From_Source (N)
18822 or else not Is_Completely_Hidden (C);
18823 else
18824 return True;
18825 end if;
18826
18827 -- If it is _Parent or _Tag, there is no visibility issue
18828
18829 elsif not Comes_From_Source (Original_Comp) then
18830 return True;
18831
18832 -- Discriminants are visible unless the (private) type has unknown
18833 -- discriminants. If the discriminant reference is inserted for a
18834 -- discriminant check on a full view it is also visible.
18835
18836 elsif Ekind (Original_Comp) = E_Discriminant
18837 and then
18838 (not Has_Unknown_Discriminants (Original_Type)
18839 or else (Present (N)
18840 and then Nkind (N) = N_Selected_Component
18841 and then Nkind (Prefix (N)) = N_Type_Conversion
18842 and then not Comes_From_Source (Prefix (N))))
18843 then
18844 return True;
18845
18846 -- In the body of an instantiation, check the visibility of a component
18847 -- in case it has a homograph that is a primitive operation of a private
18848 -- type which was not visible in the generic unit.
18849
18850 -- Should Is_Prefixed_Call be propagated from template to instance???
18851
18852 elsif In_Instance_Body then
18853 if not Is_Tagged_Type (Original_Type)
18854 or else not Is_Private_Type (Original_Type)
18855 then
18856 return True;
18857
18858 else
18859 declare
18860 Subp_Elmt : Elmt_Id;
18861
18862 begin
18863 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18864 while Present (Subp_Elmt) loop
18865
18866 -- The component is hidden by a primitive operation
18867
18868 if Chars (Node (Subp_Elmt)) = Chars (C) then
18869 return False;
18870 end if;
18871
18872 Next_Elmt (Subp_Elmt);
18873 end loop;
18874
18875 return True;
18876 end;
18877 end if;
18878
18879 -- If the component has been declared in an ancestor which is currently
18880 -- a private type, then it is not visible. The same applies if the
18881 -- component's containing type is not in an open scope and the original
18882 -- component's enclosing type is a visible full view of a private type
18883 -- (which can occur in cases where an attempt is being made to reference
18884 -- a component in a sibling package that is inherited from a visible
18885 -- component of a type in an ancestor package; the component in the
18886 -- sibling package should not be visible even though the component it
18887 -- inherited from is visible). This does not apply however in the case
18888 -- where the scope of the type is a private child unit, or when the
18889 -- parent comes from a local package in which the ancestor is currently
18890 -- visible. The latter suppression of visibility is needed for cases
18891 -- that are tested in B730006.
18892
18893 elsif Is_Private_Type (Original_Type)
18894 or else
18895 (not Is_Private_Descendant (Type_Scope)
18896 and then not In_Open_Scopes (Type_Scope)
18897 and then Has_Private_Declaration (Original_Type))
18898 then
18899 -- If the type derives from an entity in a formal package, there
18900 -- are no additional visible components.
18901
18902 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18903 N_Formal_Package_Declaration
18904 then
18905 return False;
18906
18907 -- if we are not in the private part of the current package, there
18908 -- are no additional visible components.
18909
18910 elsif Ekind (Scope (Current_Scope)) = E_Package
18911 and then not In_Private_Part (Scope (Current_Scope))
18912 then
18913 return False;
18914 else
18915 return
18916 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18917 and then In_Open_Scopes (Scope (Original_Type))
18918 and then Is_Local_Type (Type_Scope);
18919 end if;
18920
18921 -- There is another weird way in which a component may be invisible when
18922 -- the private and the full view are not derived from the same ancestor.
18923 -- Here is an example :
18924
18925 -- type A1 is tagged record F1 : integer; end record;
18926 -- type A2 is new A1 with record F2 : integer; end record;
18927 -- type T is new A1 with private;
18928 -- private
18929 -- type T is new A2 with null record;
18930
18931 -- In this case, the full view of T inherits F1 and F2 but the private
18932 -- view inherits only F1
18933
18934 else
18935 declare
18936 Ancestor : Entity_Id := Scope (C);
18937
18938 begin
18939 loop
18940 if Ancestor = Original_Type then
18941 return True;
18942
18943 -- The ancestor may have a partial view of the original type,
18944 -- but if the full view is in scope, as in a child body, the
18945 -- component is visible.
18946
18947 elsif In_Private_Part (Scope (Original_Type))
18948 and then Full_View (Ancestor) = Original_Type
18949 then
18950 return True;
18951
18952 elsif Ancestor = Etype (Ancestor) then
18953
18954 -- No further ancestors to examine
18955
18956 return False;
18957 end if;
18958
18959 Ancestor := Etype (Ancestor);
18960 end loop;
18961 end;
18962 end if;
18963 end Is_Visible_Component;
18964
18965 --------------------------
18966 -- Make_Class_Wide_Type --
18967 --------------------------
18968
18969 procedure Make_Class_Wide_Type (T : Entity_Id) is
18970 CW_Type : Entity_Id;
18971 CW_Name : Name_Id;
18972 Next_E : Entity_Id;
18973 Prev_E : Entity_Id;
18974
18975 begin
18976 if Present (Class_Wide_Type (T)) then
18977
18978 -- The class-wide type is a partially decorated entity created for a
18979 -- unanalyzed tagged type referenced through a limited with clause.
18980 -- When the tagged type is analyzed, its class-wide type needs to be
18981 -- redecorated. Note that we reuse the entity created by Decorate_
18982 -- Tagged_Type in order to preserve all links.
18983
18984 if Materialize_Entity (Class_Wide_Type (T)) then
18985 CW_Type := Class_Wide_Type (T);
18986 Set_Materialize_Entity (CW_Type, False);
18987
18988 -- The class wide type can have been defined by the partial view, in
18989 -- which case everything is already done.
18990
18991 else
18992 return;
18993 end if;
18994
18995 -- Default case, we need to create a new class-wide type
18996
18997 else
18998 CW_Type :=
18999 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19000 end if;
19001
19002 -- Inherit root type characteristics
19003
19004 CW_Name := Chars (CW_Type);
19005 Next_E := Next_Entity (CW_Type);
19006 Prev_E := Prev_Entity (CW_Type);
19007 Copy_Node (T, CW_Type);
19008 Set_Comes_From_Source (CW_Type, False);
19009 Set_Chars (CW_Type, CW_Name);
19010 Set_Parent (CW_Type, Parent (T));
19011 Set_Prev_Entity (CW_Type, Prev_E);
19012 Set_Next_Entity (CW_Type, Next_E);
19013
19014 -- Ensure we have a new freeze node for the class-wide type. The partial
19015 -- view may have freeze action of its own, requiring a proper freeze
19016 -- node, and the same freeze node cannot be shared between the two
19017 -- types.
19018
19019 Set_Has_Delayed_Freeze (CW_Type);
19020 Set_Freeze_Node (CW_Type, Empty);
19021
19022 -- Customize the class-wide type: It has no prim. op., it cannot be
19023 -- abstract, its Etype points back to the specific root type, and it
19024 -- cannot have any invariants.
19025
19026 Set_Ekind (CW_Type, E_Class_Wide_Type);
19027 Set_Is_Tagged_Type (CW_Type, True);
19028 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19029 Set_Is_Abstract_Type (CW_Type, False);
19030 Set_Is_Constrained (CW_Type, False);
19031 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19032 Set_Default_SSO (CW_Type);
19033 Set_Has_Inheritable_Invariants (CW_Type, False);
19034 Set_Has_Inherited_Invariants (CW_Type, False);
19035 Set_Has_Own_Invariants (CW_Type, False);
19036
19037 if Ekind (T) = E_Class_Wide_Subtype then
19038 Set_Etype (CW_Type, Etype (Base_Type (T)));
19039 else
19040 Set_Etype (CW_Type, T);
19041 end if;
19042
19043 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19044
19045 -- If this is the class_wide type of a constrained subtype, it does
19046 -- not have discriminants.
19047
19048 Set_Has_Discriminants (CW_Type,
19049 Has_Discriminants (T) and then not Is_Constrained (T));
19050
19051 Set_Has_Unknown_Discriminants (CW_Type, True);
19052 Set_Class_Wide_Type (T, CW_Type);
19053 Set_Equivalent_Type (CW_Type, Empty);
19054
19055 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19056
19057 Set_Class_Wide_Type (CW_Type, CW_Type);
19058 end Make_Class_Wide_Type;
19059
19060 ----------------
19061 -- Make_Index --
19062 ----------------
19063
19064 procedure Make_Index
19065 (N : Node_Id;
19066 Related_Nod : Node_Id;
19067 Related_Id : Entity_Id := Empty;
19068 Suffix_Index : Nat := 1;
19069 In_Iter_Schm : Boolean := False)
19070 is
19071 R : Node_Id;
19072 T : Entity_Id;
19073 Def_Id : Entity_Id := Empty;
19074 Found : Boolean := False;
19075
19076 begin
19077 -- For a discrete range used in a constrained array definition and
19078 -- defined by a range, an implicit conversion to the predefined type
19079 -- INTEGER is assumed if each bound is either a numeric literal, a named
19080 -- number, or an attribute, and the type of both bounds (prior to the
19081 -- implicit conversion) is the type universal_integer. Otherwise, both
19082 -- bounds must be of the same discrete type, other than universal
19083 -- integer; this type must be determinable independently of the
19084 -- context, but using the fact that the type must be discrete and that
19085 -- both bounds must have the same type.
19086
19087 -- Character literals also have a universal type in the absence of
19088 -- of additional context, and are resolved to Standard_Character.
19089
19090 if Nkind (N) = N_Range then
19091
19092 -- The index is given by a range constraint. The bounds are known
19093 -- to be of a consistent type.
19094
19095 if not Is_Overloaded (N) then
19096 T := Etype (N);
19097
19098 -- For universal bounds, choose the specific predefined type
19099
19100 if T = Universal_Integer then
19101 T := Standard_Integer;
19102
19103 elsif T = Any_Character then
19104 Ambiguous_Character (Low_Bound (N));
19105
19106 T := Standard_Character;
19107 end if;
19108
19109 -- The node may be overloaded because some user-defined operators
19110 -- are available, but if a universal interpretation exists it is
19111 -- also the selected one.
19112
19113 elsif Universal_Interpretation (N) = Universal_Integer then
19114 T := Standard_Integer;
19115
19116 else
19117 T := Any_Type;
19118
19119 declare
19120 Ind : Interp_Index;
19121 It : Interp;
19122
19123 begin
19124 Get_First_Interp (N, Ind, It);
19125 while Present (It.Typ) loop
19126 if Is_Discrete_Type (It.Typ) then
19127
19128 if Found
19129 and then not Covers (It.Typ, T)
19130 and then not Covers (T, It.Typ)
19131 then
19132 Error_Msg_N ("ambiguous bounds in discrete range", N);
19133 exit;
19134 else
19135 T := It.Typ;
19136 Found := True;
19137 end if;
19138 end if;
19139
19140 Get_Next_Interp (Ind, It);
19141 end loop;
19142
19143 if T = Any_Type then
19144 Error_Msg_N ("discrete type required for range", N);
19145 Set_Etype (N, Any_Type);
19146 return;
19147
19148 elsif T = Universal_Integer then
19149 T := Standard_Integer;
19150 end if;
19151 end;
19152 end if;
19153
19154 if not Is_Discrete_Type (T) then
19155 Error_Msg_N ("discrete type required for range", N);
19156 Set_Etype (N, Any_Type);
19157 return;
19158 end if;
19159
19160 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19161 and then Attribute_Name (Low_Bound (N)) = Name_First
19162 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19163 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19164 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19165 then
19166 -- The type of the index will be the type of the prefix, as long
19167 -- as the upper bound is 'Last of the same type.
19168
19169 Def_Id := Entity (Prefix (Low_Bound (N)));
19170
19171 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19172 or else Attribute_Name (High_Bound (N)) /= Name_Last
19173 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19174 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19175 then
19176 Def_Id := Empty;
19177 end if;
19178 end if;
19179
19180 R := N;
19181 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19182
19183 elsif Nkind (N) = N_Subtype_Indication then
19184
19185 -- The index is given by a subtype with a range constraint
19186
19187 T := Base_Type (Entity (Subtype_Mark (N)));
19188
19189 if not Is_Discrete_Type (T) then
19190 Error_Msg_N ("discrete type required for range", N);
19191 Set_Etype (N, Any_Type);
19192 return;
19193 end if;
19194
19195 R := Range_Expression (Constraint (N));
19196
19197 Resolve (R, T);
19198 Process_Range_Expr_In_Decl
19199 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19200
19201 elsif Nkind (N) = N_Attribute_Reference then
19202
19203 -- Catch beginner's error (use of attribute other than 'Range)
19204
19205 if Attribute_Name (N) /= Name_Range then
19206 Error_Msg_N ("expect attribute ''Range", N);
19207 Set_Etype (N, Any_Type);
19208 return;
19209 end if;
19210
19211 -- If the node denotes the range of a type mark, that is also the
19212 -- resulting type, and we do not need to create an Itype for it.
19213
19214 if Is_Entity_Name (Prefix (N))
19215 and then Comes_From_Source (N)
19216 and then Is_Type (Entity (Prefix (N)))
19217 and then Is_Discrete_Type (Entity (Prefix (N)))
19218 then
19219 Def_Id := Entity (Prefix (N));
19220 end if;
19221
19222 Analyze_And_Resolve (N);
19223 T := Etype (N);
19224 R := N;
19225
19226 -- If none of the above, must be a subtype. We convert this to a
19227 -- range attribute reference because in the case of declared first
19228 -- named subtypes, the types in the range reference can be different
19229 -- from the type of the entity. A range attribute normalizes the
19230 -- reference and obtains the correct types for the bounds.
19231
19232 -- This transformation is in the nature of an expansion, is only
19233 -- done if expansion is active. In particular, it is not done on
19234 -- formal generic types, because we need to retain the name of the
19235 -- original index for instantiation purposes.
19236
19237 else
19238 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19239 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19240 Set_Etype (N, Any_Integer);
19241 return;
19242
19243 else
19244 -- The type mark may be that of an incomplete type. It is only
19245 -- now that we can get the full view, previous analysis does
19246 -- not look specifically for a type mark.
19247
19248 Set_Entity (N, Get_Full_View (Entity (N)));
19249 Set_Etype (N, Entity (N));
19250 Def_Id := Entity (N);
19251
19252 if not Is_Discrete_Type (Def_Id) then
19253 Error_Msg_N ("discrete type required for index", N);
19254 Set_Etype (N, Any_Type);
19255 return;
19256 end if;
19257 end if;
19258
19259 if Expander_Active then
19260 Rewrite (N,
19261 Make_Attribute_Reference (Sloc (N),
19262 Attribute_Name => Name_Range,
19263 Prefix => Relocate_Node (N)));
19264
19265 -- The original was a subtype mark that does not freeze. This
19266 -- means that the rewritten version must not freeze either.
19267
19268 Set_Must_Not_Freeze (N);
19269 Set_Must_Not_Freeze (Prefix (N));
19270 Analyze_And_Resolve (N);
19271 T := Etype (N);
19272 R := N;
19273
19274 -- If expander is inactive, type is legal, nothing else to construct
19275
19276 else
19277 return;
19278 end if;
19279 end if;
19280
19281 if not Is_Discrete_Type (T) then
19282 Error_Msg_N ("discrete type required for range", N);
19283 Set_Etype (N, Any_Type);
19284 return;
19285
19286 elsif T = Any_Type then
19287 Set_Etype (N, Any_Type);
19288 return;
19289 end if;
19290
19291 -- We will now create the appropriate Itype to describe the range, but
19292 -- first a check. If we originally had a subtype, then we just label
19293 -- the range with this subtype. Not only is there no need to construct
19294 -- a new subtype, but it is wrong to do so for two reasons:
19295
19296 -- 1. A legality concern, if we have a subtype, it must not freeze,
19297 -- and the Itype would cause freezing incorrectly
19298
19299 -- 2. An efficiency concern, if we created an Itype, it would not be
19300 -- recognized as the same type for the purposes of eliminating
19301 -- checks in some circumstances.
19302
19303 -- We signal this case by setting the subtype entity in Def_Id
19304
19305 if No (Def_Id) then
19306 Def_Id :=
19307 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19308 Set_Etype (Def_Id, Base_Type (T));
19309
19310 if Is_Signed_Integer_Type (T) then
19311 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19312
19313 elsif Is_Modular_Integer_Type (T) then
19314 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19315
19316 else
19317 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19318 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19319 Set_First_Literal (Def_Id, First_Literal (T));
19320 end if;
19321
19322 Set_Size_Info (Def_Id, (T));
19323 Set_RM_Size (Def_Id, RM_Size (T));
19324 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19325
19326 Set_Scalar_Range (Def_Id, R);
19327 Conditional_Delay (Def_Id, T);
19328
19329 if Nkind (N) = N_Subtype_Indication then
19330 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19331 end if;
19332
19333 -- In the subtype indication case, if the immediate parent of the
19334 -- new subtype is non-static, then the subtype we create is non-
19335 -- static, even if its bounds are static.
19336
19337 if Nkind (N) = N_Subtype_Indication
19338 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19339 then
19340 Set_Is_Non_Static_Subtype (Def_Id);
19341 end if;
19342 end if;
19343
19344 -- Final step is to label the index with this constructed type
19345
19346 Set_Etype (N, Def_Id);
19347 end Make_Index;
19348
19349 ------------------------------
19350 -- Modular_Type_Declaration --
19351 ------------------------------
19352
19353 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19354 Mod_Expr : constant Node_Id := Expression (Def);
19355 M_Val : Uint;
19356
19357 procedure Set_Modular_Size (Bits : Int);
19358 -- Sets RM_Size to Bits, and Esize to normal word size above this
19359
19360 ----------------------
19361 -- Set_Modular_Size --
19362 ----------------------
19363
19364 procedure Set_Modular_Size (Bits : Int) is
19365 begin
19366 Set_RM_Size (T, UI_From_Int (Bits));
19367
19368 if Bits <= 8 then
19369 Init_Esize (T, 8);
19370
19371 elsif Bits <= 16 then
19372 Init_Esize (T, 16);
19373
19374 elsif Bits <= 32 then
19375 Init_Esize (T, 32);
19376
19377 else
19378 Init_Esize (T, System_Max_Binary_Modulus_Power);
19379 end if;
19380
19381 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19382 Set_Is_Known_Valid (T);
19383 end if;
19384 end Set_Modular_Size;
19385
19386 -- Start of processing for Modular_Type_Declaration
19387
19388 begin
19389 -- If the mod expression is (exactly) 2 * literal, where literal is
19390 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19391
19392 if Warn_On_Suspicious_Modulus_Value
19393 and then Nkind (Mod_Expr) = N_Op_Multiply
19394 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19395 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19396 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19397 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19398 then
19399 Error_Msg_N
19400 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19401 end if;
19402
19403 -- Proceed with analysis of mod expression
19404
19405 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19406 Set_Etype (T, T);
19407 Set_Ekind (T, E_Modular_Integer_Type);
19408 Init_Alignment (T);
19409 Set_Is_Constrained (T);
19410
19411 if not Is_OK_Static_Expression (Mod_Expr) then
19412 Flag_Non_Static_Expr
19413 ("non-static expression used for modular type bound!", Mod_Expr);
19414 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19415 else
19416 M_Val := Expr_Value (Mod_Expr);
19417 end if;
19418
19419 if M_Val < 1 then
19420 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19421 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19422 end if;
19423
19424 if M_Val > 2 ** Standard_Long_Integer_Size then
19425 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19426 end if;
19427
19428 Set_Modulus (T, M_Val);
19429
19430 -- Create bounds for the modular type based on the modulus given in
19431 -- the type declaration and then analyze and resolve those bounds.
19432
19433 Set_Scalar_Range (T,
19434 Make_Range (Sloc (Mod_Expr),
19435 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19436 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19437
19438 -- Properly analyze the literals for the range. We do this manually
19439 -- because we can't go calling Resolve, since we are resolving these
19440 -- bounds with the type, and this type is certainly not complete yet.
19441
19442 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19443 Set_Etype (High_Bound (Scalar_Range (T)), T);
19444 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19445 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19446
19447 -- Loop through powers of two to find number of bits required
19448
19449 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19450
19451 -- Binary case
19452
19453 if M_Val = 2 ** Bits then
19454 Set_Modular_Size (Bits);
19455 return;
19456
19457 -- Nonbinary case
19458
19459 elsif M_Val < 2 ** Bits then
19460 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19461 Set_Non_Binary_Modulus (T);
19462
19463 if Bits > System_Max_Nonbinary_Modulus_Power then
19464 Error_Msg_Uint_1 :=
19465 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19466 Error_Msg_F
19467 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19468 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19469 return;
19470
19471 else
19472 -- In the nonbinary case, set size as per RM 13.3(55)
19473
19474 Set_Modular_Size (Bits);
19475 return;
19476 end if;
19477 end if;
19478
19479 end loop;
19480
19481 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19482 -- so we just signal an error and set the maximum size.
19483
19484 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19485 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19486
19487 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19488 Init_Alignment (T);
19489
19490 end Modular_Type_Declaration;
19491
19492 --------------------------
19493 -- New_Concatenation_Op --
19494 --------------------------
19495
19496 procedure New_Concatenation_Op (Typ : Entity_Id) is
19497 Loc : constant Source_Ptr := Sloc (Typ);
19498 Op : Entity_Id;
19499
19500 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19501 -- Create abbreviated declaration for the formal of a predefined
19502 -- Operator 'Op' of type 'Typ'
19503
19504 --------------------
19505 -- Make_Op_Formal --
19506 --------------------
19507
19508 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19509 Formal : Entity_Id;
19510 begin
19511 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19512 Set_Etype (Formal, Typ);
19513 Set_Mechanism (Formal, Default_Mechanism);
19514 return Formal;
19515 end Make_Op_Formal;
19516
19517 -- Start of processing for New_Concatenation_Op
19518
19519 begin
19520 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19521
19522 Set_Ekind (Op, E_Operator);
19523 Set_Scope (Op, Current_Scope);
19524 Set_Etype (Op, Typ);
19525 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19526 Set_Is_Immediately_Visible (Op);
19527 Set_Is_Intrinsic_Subprogram (Op);
19528 Set_Has_Completion (Op);
19529 Append_Entity (Op, Current_Scope);
19530
19531 Set_Name_Entity_Id (Name_Op_Concat, Op);
19532
19533 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19534 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19535 end New_Concatenation_Op;
19536
19537 -------------------------
19538 -- OK_For_Limited_Init --
19539 -------------------------
19540
19541 -- ???Check all calls of this, and compare the conditions under which it's
19542 -- called.
19543
19544 function OK_For_Limited_Init
19545 (Typ : Entity_Id;
19546 Exp : Node_Id) return Boolean
19547 is
19548 begin
19549 return Is_CPP_Constructor_Call (Exp)
19550 or else (Ada_Version >= Ada_2005
19551 and then not Debug_Flag_Dot_L
19552 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19553 end OK_For_Limited_Init;
19554
19555 -------------------------------
19556 -- OK_For_Limited_Init_In_05 --
19557 -------------------------------
19558
19559 function OK_For_Limited_Init_In_05
19560 (Typ : Entity_Id;
19561 Exp : Node_Id) return Boolean
19562 is
19563 begin
19564 -- An object of a limited interface type can be initialized with any
19565 -- expression of a nonlimited descendant type. However this does not
19566 -- apply if this is a view conversion of some other expression. This
19567 -- is checked below.
19568
19569 if Is_Class_Wide_Type (Typ)
19570 and then Is_Limited_Interface (Typ)
19571 and then not Is_Limited_Type (Etype (Exp))
19572 and then Nkind (Exp) /= N_Type_Conversion
19573 then
19574 return True;
19575 end if;
19576
19577 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19578 -- case of limited aggregates (including extension aggregates), and
19579 -- function calls. The function call may have been given in prefixed
19580 -- notation, in which case the original node is an indexed component.
19581 -- If the function is parameterless, the original node was an explicit
19582 -- dereference. The function may also be parameterless, in which case
19583 -- the source node is just an identifier.
19584
19585 -- A branch of a conditional expression may have been removed if the
19586 -- condition is statically known. This happens during expansion, and
19587 -- thus will not happen if previous errors were encountered. The check
19588 -- will have been performed on the chosen branch, which replaces the
19589 -- original conditional expression.
19590
19591 if No (Exp) then
19592 return True;
19593 end if;
19594
19595 case Nkind (Original_Node (Exp)) is
19596 when N_Aggregate
19597 | N_Extension_Aggregate
19598 | N_Function_Call
19599 | N_Op
19600 =>
19601 return True;
19602
19603 when N_Identifier =>
19604 return Present (Entity (Original_Node (Exp)))
19605 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19606
19607 when N_Qualified_Expression =>
19608 return
19609 OK_For_Limited_Init_In_05
19610 (Typ, Expression (Original_Node (Exp)));
19611
19612 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19613 -- with a function call, the expander has rewritten the call into an
19614 -- N_Type_Conversion node to force displacement of the pointer to
19615 -- reference the component containing the secondary dispatch table.
19616 -- Otherwise a type conversion is not a legal context.
19617 -- A return statement for a build-in-place function returning a
19618 -- synchronized type also introduces an unchecked conversion.
19619
19620 when N_Type_Conversion
19621 | N_Unchecked_Type_Conversion
19622 =>
19623 return not Comes_From_Source (Exp)
19624 and then
19625 OK_For_Limited_Init_In_05
19626 (Typ, Expression (Original_Node (Exp)));
19627
19628 when N_Explicit_Dereference
19629 | N_Indexed_Component
19630 | N_Selected_Component
19631 =>
19632 return Nkind (Exp) = N_Function_Call;
19633
19634 -- A use of 'Input is a function call, hence allowed. Normally the
19635 -- attribute will be changed to a call, but the attribute by itself
19636 -- can occur with -gnatc.
19637
19638 when N_Attribute_Reference =>
19639 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19640
19641 -- "return raise ..." is OK
19642
19643 when N_Raise_Expression =>
19644 return True;
19645
19646 -- For a case expression, all dependent expressions must be legal
19647
19648 when N_Case_Expression =>
19649 declare
19650 Alt : Node_Id;
19651
19652 begin
19653 Alt := First (Alternatives (Original_Node (Exp)));
19654 while Present (Alt) loop
19655 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19656 return False;
19657 end if;
19658
19659 Next (Alt);
19660 end loop;
19661
19662 return True;
19663 end;
19664
19665 -- For an if expression, all dependent expressions must be legal
19666
19667 when N_If_Expression =>
19668 declare
19669 Then_Expr : constant Node_Id :=
19670 Next (First (Expressions (Original_Node (Exp))));
19671 Else_Expr : constant Node_Id := Next (Then_Expr);
19672 begin
19673 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19674 and then
19675 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19676 end;
19677
19678 when others =>
19679 return False;
19680 end case;
19681 end OK_For_Limited_Init_In_05;
19682
19683 -------------------------------------------
19684 -- Ordinary_Fixed_Point_Type_Declaration --
19685 -------------------------------------------
19686
19687 procedure Ordinary_Fixed_Point_Type_Declaration
19688 (T : Entity_Id;
19689 Def : Node_Id)
19690 is
19691 Loc : constant Source_Ptr := Sloc (Def);
19692 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19693 RRS : constant Node_Id := Real_Range_Specification (Def);
19694 Implicit_Base : Entity_Id;
19695 Delta_Val : Ureal;
19696 Small_Val : Ureal;
19697 Low_Val : Ureal;
19698 High_Val : Ureal;
19699
19700 begin
19701 Check_Restriction (No_Fixed_Point, Def);
19702
19703 -- Create implicit base type
19704
19705 Implicit_Base :=
19706 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19707 Set_Etype (Implicit_Base, Implicit_Base);
19708
19709 -- Analyze and process delta expression
19710
19711 Analyze_And_Resolve (Delta_Expr, Any_Real);
19712
19713 Check_Delta_Expression (Delta_Expr);
19714 Delta_Val := Expr_Value_R (Delta_Expr);
19715
19716 Set_Delta_Value (Implicit_Base, Delta_Val);
19717
19718 -- Compute default small from given delta, which is the largest power
19719 -- of two that does not exceed the given delta value.
19720
19721 declare
19722 Tmp : Ureal;
19723 Scale : Int;
19724
19725 begin
19726 Tmp := Ureal_1;
19727 Scale := 0;
19728
19729 if Delta_Val < Ureal_1 then
19730 while Delta_Val < Tmp loop
19731 Tmp := Tmp / Ureal_2;
19732 Scale := Scale + 1;
19733 end loop;
19734
19735 else
19736 loop
19737 Tmp := Tmp * Ureal_2;
19738 exit when Tmp > Delta_Val;
19739 Scale := Scale - 1;
19740 end loop;
19741 end if;
19742
19743 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19744 end;
19745
19746 Set_Small_Value (Implicit_Base, Small_Val);
19747
19748 -- If no range was given, set a dummy range
19749
19750 if RRS <= Empty_Or_Error then
19751 Low_Val := -Small_Val;
19752 High_Val := Small_Val;
19753
19754 -- Otherwise analyze and process given range
19755
19756 else
19757 declare
19758 Low : constant Node_Id := Low_Bound (RRS);
19759 High : constant Node_Id := High_Bound (RRS);
19760
19761 begin
19762 Analyze_And_Resolve (Low, Any_Real);
19763 Analyze_And_Resolve (High, Any_Real);
19764 Check_Real_Bound (Low);
19765 Check_Real_Bound (High);
19766
19767 -- Obtain and set the range
19768
19769 Low_Val := Expr_Value_R (Low);
19770 High_Val := Expr_Value_R (High);
19771
19772 if Low_Val > High_Val then
19773 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19774 end if;
19775 end;
19776 end if;
19777
19778 -- The range for both the implicit base and the declared first subtype
19779 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19780 -- set a temporary range in place. Note that the bounds of the base
19781 -- type will be widened to be symmetrical and to fill the available
19782 -- bits when the type is frozen.
19783
19784 -- We could do this with all discrete types, and probably should, but
19785 -- we absolutely have to do it for fixed-point, since the end-points
19786 -- of the range and the size are determined by the small value, which
19787 -- could be reset before the freeze point.
19788
19789 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19790 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19791
19792 -- Complete definition of first subtype. The inheritance of the rep item
19793 -- chain ensures that SPARK-related pragmas are not clobbered when the
19794 -- ordinary fixed point type acts as a full view of a private type.
19795
19796 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19797 Set_Etype (T, Implicit_Base);
19798 Init_Size_Align (T);
19799 Inherit_Rep_Item_Chain (T, Implicit_Base);
19800 Set_Small_Value (T, Small_Val);
19801 Set_Delta_Value (T, Delta_Val);
19802 Set_Is_Constrained (T);
19803 end Ordinary_Fixed_Point_Type_Declaration;
19804
19805 ----------------------------------
19806 -- Preanalyze_Assert_Expression --
19807 ----------------------------------
19808
19809 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19810 begin
19811 In_Assertion_Expr := In_Assertion_Expr + 1;
19812 Preanalyze_Spec_Expression (N, T);
19813 In_Assertion_Expr := In_Assertion_Expr - 1;
19814 end Preanalyze_Assert_Expression;
19815
19816 -----------------------------------
19817 -- Preanalyze_Default_Expression --
19818 -----------------------------------
19819
19820 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19821 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19822 begin
19823 In_Default_Expr := True;
19824 Preanalyze_Spec_Expression (N, T);
19825 In_Default_Expr := Save_In_Default_Expr;
19826 end Preanalyze_Default_Expression;
19827
19828 --------------------------------
19829 -- Preanalyze_Spec_Expression --
19830 --------------------------------
19831
19832 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19833 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19834 begin
19835 In_Spec_Expression := True;
19836 Preanalyze_And_Resolve (N, T);
19837 In_Spec_Expression := Save_In_Spec_Expression;
19838 end Preanalyze_Spec_Expression;
19839
19840 ----------------------------------------
19841 -- Prepare_Private_Subtype_Completion --
19842 ----------------------------------------
19843
19844 procedure Prepare_Private_Subtype_Completion
19845 (Id : Entity_Id;
19846 Related_Nod : Node_Id)
19847 is
19848 Id_B : constant Entity_Id := Base_Type (Id);
19849 Full_B : Entity_Id := Full_View (Id_B);
19850 Full : Entity_Id;
19851
19852 begin
19853 if Present (Full_B) then
19854
19855 -- Get to the underlying full view if necessary
19856
19857 if Is_Private_Type (Full_B)
19858 and then Present (Underlying_Full_View (Full_B))
19859 then
19860 Full_B := Underlying_Full_View (Full_B);
19861 end if;
19862
19863 -- The Base_Type is already completed, we can complete the subtype
19864 -- now. We have to create a new entity with the same name, Thus we
19865 -- can't use Create_Itype.
19866
19867 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19868 Set_Is_Itype (Full);
19869 Set_Associated_Node_For_Itype (Full, Related_Nod);
19870 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19871 end if;
19872
19873 -- The parent subtype may be private, but the base might not, in some
19874 -- nested instances. In that case, the subtype does not need to be
19875 -- exchanged. It would still be nice to make private subtypes and their
19876 -- bases consistent at all times ???
19877
19878 if Is_Private_Type (Id_B) then
19879 Append_Elmt (Id, Private_Dependents (Id_B));
19880 end if;
19881 end Prepare_Private_Subtype_Completion;
19882
19883 ---------------------------
19884 -- Process_Discriminants --
19885 ---------------------------
19886
19887 procedure Process_Discriminants
19888 (N : Node_Id;
19889 Prev : Entity_Id := Empty)
19890 is
19891 Elist : constant Elist_Id := New_Elmt_List;
19892 Id : Node_Id;
19893 Discr : Node_Id;
19894 Discr_Number : Uint;
19895 Discr_Type : Entity_Id;
19896 Default_Present : Boolean := False;
19897 Default_Not_Present : Boolean := False;
19898
19899 begin
19900 -- A composite type other than an array type can have discriminants.
19901 -- On entry, the current scope is the composite type.
19902
19903 -- The discriminants are initially entered into the scope of the type
19904 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19905 -- use, as explained at the end of this procedure.
19906
19907 Discr := First (Discriminant_Specifications (N));
19908 while Present (Discr) loop
19909 Enter_Name (Defining_Identifier (Discr));
19910
19911 -- For navigation purposes we add a reference to the discriminant
19912 -- in the entity for the type. If the current declaration is a
19913 -- completion, place references on the partial view. Otherwise the
19914 -- type is the current scope.
19915
19916 if Present (Prev) then
19917
19918 -- The references go on the partial view, if present. If the
19919 -- partial view has discriminants, the references have been
19920 -- generated already.
19921
19922 if not Has_Discriminants (Prev) then
19923 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19924 end if;
19925 else
19926 Generate_Reference
19927 (Current_Scope, Defining_Identifier (Discr), 'd');
19928 end if;
19929
19930 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19931 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19932
19933 -- Ada 2005 (AI-254)
19934
19935 if Present (Access_To_Subprogram_Definition
19936 (Discriminant_Type (Discr)))
19937 and then Protected_Present (Access_To_Subprogram_Definition
19938 (Discriminant_Type (Discr)))
19939 then
19940 Discr_Type :=
19941 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19942 end if;
19943
19944 else
19945 Find_Type (Discriminant_Type (Discr));
19946 Discr_Type := Etype (Discriminant_Type (Discr));
19947
19948 if Error_Posted (Discriminant_Type (Discr)) then
19949 Discr_Type := Any_Type;
19950 end if;
19951 end if;
19952
19953 -- Handling of discriminants that are access types
19954
19955 if Is_Access_Type (Discr_Type) then
19956
19957 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19958 -- limited record types
19959
19960 if Ada_Version < Ada_2005 then
19961 Check_Access_Discriminant_Requires_Limited
19962 (Discr, Discriminant_Type (Discr));
19963 end if;
19964
19965 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19966 Error_Msg_N
19967 ("(Ada 83) access discriminant not allowed", Discr);
19968 end if;
19969
19970 -- If not access type, must be a discrete type
19971
19972 elsif not Is_Discrete_Type (Discr_Type) then
19973 Error_Msg_N
19974 ("discriminants must have a discrete or access type",
19975 Discriminant_Type (Discr));
19976 end if;
19977
19978 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19979
19980 -- If a discriminant specification includes the assignment compound
19981 -- delimiter followed by an expression, the expression is the default
19982 -- expression of the discriminant; the default expression must be of
19983 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19984 -- a default expression, we do the special preanalysis, since this
19985 -- expression does not freeze (see section "Handling of Default and
19986 -- Per-Object Expressions" in spec of package Sem).
19987
19988 if Present (Expression (Discr)) then
19989 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19990
19991 -- Legaity checks
19992
19993 if Nkind (N) = N_Formal_Type_Declaration then
19994 Error_Msg_N
19995 ("discriminant defaults not allowed for formal type",
19996 Expression (Discr));
19997
19998 -- Flag an error for a tagged type with defaulted discriminants,
19999 -- excluding limited tagged types when compiling for Ada 2012
20000 -- (see AI05-0214).
20001
20002 elsif Is_Tagged_Type (Current_Scope)
20003 and then (not Is_Limited_Type (Current_Scope)
20004 or else Ada_Version < Ada_2012)
20005 and then Comes_From_Source (N)
20006 then
20007 -- Note: see similar test in Check_Or_Process_Discriminants, to
20008 -- handle the (illegal) case of the completion of an untagged
20009 -- view with discriminants with defaults by a tagged full view.
20010 -- We skip the check if Discr does not come from source, to
20011 -- account for the case of an untagged derived type providing
20012 -- defaults for a renamed discriminant from a private untagged
20013 -- ancestor with a tagged full view (ACATS B460006).
20014
20015 if Ada_Version >= Ada_2012 then
20016 Error_Msg_N
20017 ("discriminants of nonlimited tagged type cannot have"
20018 & " defaults",
20019 Expression (Discr));
20020 else
20021 Error_Msg_N
20022 ("discriminants of tagged type cannot have defaults",
20023 Expression (Discr));
20024 end if;
20025
20026 else
20027 Default_Present := True;
20028 Append_Elmt (Expression (Discr), Elist);
20029
20030 -- Tag the defining identifiers for the discriminants with
20031 -- their corresponding default expressions from the tree.
20032
20033 Set_Discriminant_Default_Value
20034 (Defining_Identifier (Discr), Expression (Discr));
20035 end if;
20036
20037 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20038 -- gets set unless we can be sure that no range check is required.
20039
20040 if (GNATprove_Mode or not Expander_Active)
20041 and then not
20042 Is_In_Range
20043 (Expression (Discr), Discr_Type, Assume_Valid => True)
20044 then
20045 Set_Do_Range_Check (Expression (Discr));
20046 end if;
20047
20048 -- No default discriminant value given
20049
20050 else
20051 Default_Not_Present := True;
20052 end if;
20053
20054 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20055 -- Discr_Type but with the null-exclusion attribute
20056
20057 if Ada_Version >= Ada_2005 then
20058
20059 -- Ada 2005 (AI-231): Static checks
20060
20061 if Can_Never_Be_Null (Discr_Type) then
20062 Null_Exclusion_Static_Checks (Discr);
20063
20064 elsif Is_Access_Type (Discr_Type)
20065 and then Null_Exclusion_Present (Discr)
20066
20067 -- No need to check itypes because in their case this check
20068 -- was done at their point of creation
20069
20070 and then not Is_Itype (Discr_Type)
20071 then
20072 if Can_Never_Be_Null (Discr_Type) then
20073 Error_Msg_NE
20074 ("`NOT NULL` not allowed (& already excludes null)",
20075 Discr,
20076 Discr_Type);
20077 end if;
20078
20079 Set_Etype (Defining_Identifier (Discr),
20080 Create_Null_Excluding_Itype
20081 (T => Discr_Type,
20082 Related_Nod => Discr));
20083
20084 -- Check for improper null exclusion if the type is otherwise
20085 -- legal for a discriminant.
20086
20087 elsif Null_Exclusion_Present (Discr)
20088 and then Is_Discrete_Type (Discr_Type)
20089 then
20090 Error_Msg_N
20091 ("null exclusion can only apply to an access type", Discr);
20092 end if;
20093
20094 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20095 -- can't have defaults. Synchronized types, or types that are
20096 -- explicitly limited are fine, but special tests apply to derived
20097 -- types in generics: in a generic body we have to assume the
20098 -- worst, and therefore defaults are not allowed if the parent is
20099 -- a generic formal private type (see ACATS B370001).
20100
20101 if Is_Access_Type (Discr_Type) and then Default_Present then
20102 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20103 or else Is_Limited_Record (Current_Scope)
20104 or else Is_Concurrent_Type (Current_Scope)
20105 or else Is_Concurrent_Record_Type (Current_Scope)
20106 or else Ekind (Current_Scope) = E_Limited_Private_Type
20107 then
20108 if not Is_Derived_Type (Current_Scope)
20109 or else not Is_Generic_Type (Etype (Current_Scope))
20110 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20111 or else Limited_Present
20112 (Type_Definition (Parent (Current_Scope)))
20113 then
20114 null;
20115
20116 else
20117 Error_Msg_N
20118 ("access discriminants of nonlimited types cannot "
20119 & "have defaults", Expression (Discr));
20120 end if;
20121
20122 elsif Present (Expression (Discr)) then
20123 Error_Msg_N
20124 ("(Ada 2005) access discriminants of nonlimited types "
20125 & "cannot have defaults", Expression (Discr));
20126 end if;
20127 end if;
20128 end if;
20129
20130 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
20131 -- This check is relevant only when SPARK_Mode is on as it is not a
20132 -- standard Ada legality rule.
20133
20134 if SPARK_Mode = On
20135 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20136 then
20137 Error_Msg_N ("discriminant cannot be volatile", Discr);
20138 end if;
20139
20140 Next (Discr);
20141 end loop;
20142
20143 -- An element list consisting of the default expressions of the
20144 -- discriminants is constructed in the above loop and used to set
20145 -- the Discriminant_Constraint attribute for the type. If an object
20146 -- is declared of this (record or task) type without any explicit
20147 -- discriminant constraint given, this element list will form the
20148 -- actual parameters for the corresponding initialization procedure
20149 -- for the type.
20150
20151 Set_Discriminant_Constraint (Current_Scope, Elist);
20152 Set_Stored_Constraint (Current_Scope, No_Elist);
20153
20154 -- Default expressions must be provided either for all or for none
20155 -- of the discriminants of a discriminant part. (RM 3.7.1)
20156
20157 if Default_Present and then Default_Not_Present then
20158 Error_Msg_N
20159 ("incomplete specification of defaults for discriminants", N);
20160 end if;
20161
20162 -- The use of the name of a discriminant is not allowed in default
20163 -- expressions of a discriminant part if the specification of the
20164 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20165
20166 -- To detect this, the discriminant names are entered initially with an
20167 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20168 -- attempt to use a void entity (for example in an expression that is
20169 -- type-checked) produces the error message: premature usage. Now after
20170 -- completing the semantic analysis of the discriminant part, we can set
20171 -- the Ekind of all the discriminants appropriately.
20172
20173 Discr := First (Discriminant_Specifications (N));
20174 Discr_Number := Uint_1;
20175 while Present (Discr) loop
20176 Id := Defining_Identifier (Discr);
20177 Set_Ekind (Id, E_Discriminant);
20178 Init_Component_Location (Id);
20179 Init_Esize (Id);
20180 Set_Discriminant_Number (Id, Discr_Number);
20181
20182 -- Make sure this is always set, even in illegal programs
20183
20184 Set_Corresponding_Discriminant (Id, Empty);
20185
20186 -- Initialize the Original_Record_Component to the entity itself.
20187 -- Inherit_Components will propagate the right value to
20188 -- discriminants in derived record types.
20189
20190 Set_Original_Record_Component (Id, Id);
20191
20192 -- Create the discriminal for the discriminant
20193
20194 Build_Discriminal (Id);
20195
20196 Next (Discr);
20197 Discr_Number := Discr_Number + 1;
20198 end loop;
20199
20200 Set_Has_Discriminants (Current_Scope);
20201 end Process_Discriminants;
20202
20203 -----------------------
20204 -- Process_Full_View --
20205 -----------------------
20206
20207 -- WARNING: This routine manages Ghost regions. Return statements must be
20208 -- replaced by gotos which jump to the end of the routine and restore the
20209 -- Ghost mode.
20210
20211 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20212 procedure Collect_Implemented_Interfaces
20213 (Typ : Entity_Id;
20214 Ifaces : Elist_Id);
20215 -- Ada 2005: Gather all the interfaces that Typ directly or
20216 -- inherently implements. Duplicate entries are not added to
20217 -- the list Ifaces.
20218
20219 ------------------------------------
20220 -- Collect_Implemented_Interfaces --
20221 ------------------------------------
20222
20223 procedure Collect_Implemented_Interfaces
20224 (Typ : Entity_Id;
20225 Ifaces : Elist_Id)
20226 is
20227 Iface : Entity_Id;
20228 Iface_Elmt : Elmt_Id;
20229
20230 begin
20231 -- Abstract interfaces are only associated with tagged record types
20232
20233 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20234 return;
20235 end if;
20236
20237 -- Recursively climb to the ancestors
20238
20239 if Etype (Typ) /= Typ
20240
20241 -- Protect the frontend against wrong cyclic declarations like:
20242
20243 -- type B is new A with private;
20244 -- type C is new A with private;
20245 -- private
20246 -- type B is new C with null record;
20247 -- type C is new B with null record;
20248
20249 and then Etype (Typ) /= Priv_T
20250 and then Etype (Typ) /= Full_T
20251 then
20252 -- Keep separate the management of private type declarations
20253
20254 if Ekind (Typ) = E_Record_Type_With_Private then
20255
20256 -- Handle the following illegal usage:
20257 -- type Private_Type is tagged private;
20258 -- private
20259 -- type Private_Type is new Type_Implementing_Iface;
20260
20261 if Present (Full_View (Typ))
20262 and then Etype (Typ) /= Full_View (Typ)
20263 then
20264 if Is_Interface (Etype (Typ)) then
20265 Append_Unique_Elmt (Etype (Typ), Ifaces);
20266 end if;
20267
20268 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20269 end if;
20270
20271 -- Non-private types
20272
20273 else
20274 if Is_Interface (Etype (Typ)) then
20275 Append_Unique_Elmt (Etype (Typ), Ifaces);
20276 end if;
20277
20278 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20279 end if;
20280 end if;
20281
20282 -- Handle entities in the list of abstract interfaces
20283
20284 if Present (Interfaces (Typ)) then
20285 Iface_Elmt := First_Elmt (Interfaces (Typ));
20286 while Present (Iface_Elmt) loop
20287 Iface := Node (Iface_Elmt);
20288
20289 pragma Assert (Is_Interface (Iface));
20290
20291 if not Contain_Interface (Iface, Ifaces) then
20292 Append_Elmt (Iface, Ifaces);
20293 Collect_Implemented_Interfaces (Iface, Ifaces);
20294 end if;
20295
20296 Next_Elmt (Iface_Elmt);
20297 end loop;
20298 end if;
20299 end Collect_Implemented_Interfaces;
20300
20301 -- Local variables
20302
20303 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20304 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20305 -- Save the Ghost-related attributes to restore on exit
20306
20307 Full_Indic : Node_Id;
20308 Full_Parent : Entity_Id;
20309 Priv_Parent : Entity_Id;
20310
20311 -- Start of processing for Process_Full_View
20312
20313 begin
20314 Mark_And_Set_Ghost_Completion (N, Priv_T);
20315
20316 -- First some sanity checks that must be done after semantic
20317 -- decoration of the full view and thus cannot be placed with other
20318 -- similar checks in Find_Type_Name
20319
20320 if not Is_Limited_Type (Priv_T)
20321 and then (Is_Limited_Type (Full_T)
20322 or else Is_Limited_Composite (Full_T))
20323 then
20324 if In_Instance then
20325 null;
20326 else
20327 Error_Msg_N
20328 ("completion of nonlimited type cannot be limited", Full_T);
20329 Explain_Limited_Type (Full_T, Full_T);
20330 end if;
20331
20332 elsif Is_Abstract_Type (Full_T)
20333 and then not Is_Abstract_Type (Priv_T)
20334 then
20335 Error_Msg_N
20336 ("completion of nonabstract type cannot be abstract", Full_T);
20337
20338 elsif Is_Tagged_Type (Priv_T)
20339 and then Is_Limited_Type (Priv_T)
20340 and then not Is_Limited_Type (Full_T)
20341 then
20342 -- If pragma CPP_Class was applied to the private declaration
20343 -- propagate the limitedness to the full-view
20344
20345 if Is_CPP_Class (Priv_T) then
20346 Set_Is_Limited_Record (Full_T);
20347
20348 -- GNAT allow its own definition of Limited_Controlled to disobey
20349 -- this rule in order in ease the implementation. This test is safe
20350 -- because Root_Controlled is defined in a child of System that
20351 -- normal programs are not supposed to use.
20352
20353 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20354 Set_Is_Limited_Composite (Full_T);
20355 else
20356 Error_Msg_N
20357 ("completion of limited tagged type must be limited", Full_T);
20358 end if;
20359
20360 elsif Is_Generic_Type (Priv_T) then
20361 Error_Msg_N ("generic type cannot have a completion", Full_T);
20362 end if;
20363
20364 -- Check that ancestor interfaces of private and full views are
20365 -- consistent. We omit this check for synchronized types because
20366 -- they are performed on the corresponding record type when frozen.
20367
20368 if Ada_Version >= Ada_2005
20369 and then Is_Tagged_Type (Priv_T)
20370 and then Is_Tagged_Type (Full_T)
20371 and then not Is_Concurrent_Type (Full_T)
20372 then
20373 declare
20374 Iface : Entity_Id;
20375 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20376 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20377
20378 begin
20379 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20380 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20381
20382 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20383 -- an interface type if and only if the full type is descendant
20384 -- of the interface type (AARM 7.3 (7.3/2)).
20385
20386 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20387
20388 if Present (Iface) then
20389 Error_Msg_NE
20390 ("interface in partial view& not implemented by full type "
20391 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20392 end if;
20393
20394 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20395
20396 if Present (Iface) then
20397 Error_Msg_NE
20398 ("interface & not implemented by partial view "
20399 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20400 end if;
20401 end;
20402 end if;
20403
20404 if Is_Tagged_Type (Priv_T)
20405 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20406 and then Is_Derived_Type (Full_T)
20407 then
20408 Priv_Parent := Etype (Priv_T);
20409
20410 -- The full view of a private extension may have been transformed
20411 -- into an unconstrained derived type declaration and a subtype
20412 -- declaration (see build_derived_record_type for details).
20413
20414 if Nkind (N) = N_Subtype_Declaration then
20415 Full_Indic := Subtype_Indication (N);
20416 Full_Parent := Etype (Base_Type (Full_T));
20417 else
20418 Full_Indic := Subtype_Indication (Type_Definition (N));
20419 Full_Parent := Etype (Full_T);
20420 end if;
20421
20422 -- Check that the parent type of the full type is a descendant of
20423 -- the ancestor subtype given in the private extension. If either
20424 -- entity has an Etype equal to Any_Type then we had some previous
20425 -- error situation [7.3(8)].
20426
20427 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20428 goto Leave;
20429
20430 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20431 -- any order. Therefore we don't have to check that its parent must
20432 -- be a descendant of the parent of the private type declaration.
20433
20434 elsif Is_Interface (Priv_Parent)
20435 and then Is_Interface (Full_Parent)
20436 then
20437 null;
20438
20439 -- Ada 2005 (AI-251): If the parent of the private type declaration
20440 -- is an interface there is no need to check that it is an ancestor
20441 -- of the associated full type declaration. The required tests for
20442 -- this case are performed by Build_Derived_Record_Type.
20443
20444 elsif not Is_Interface (Base_Type (Priv_Parent))
20445 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20446 then
20447 Error_Msg_N
20448 ("parent of full type must descend from parent of private "
20449 & "extension", Full_Indic);
20450
20451 -- First check a formal restriction, and then proceed with checking
20452 -- Ada rules. Since the formal restriction is not a serious error, we
20453 -- don't prevent further error detection for this check, hence the
20454 -- ELSE.
20455
20456 else
20457 -- In formal mode, when completing a private extension the type
20458 -- named in the private part must be exactly the same as that
20459 -- named in the visible part.
20460
20461 if Priv_Parent /= Full_Parent then
20462 Error_Msg_Name_1 := Chars (Priv_Parent);
20463 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20464 end if;
20465
20466 -- Check the rules of 7.3(10): if the private extension inherits
20467 -- known discriminants, then the full type must also inherit those
20468 -- discriminants from the same (ancestor) type, and the parent
20469 -- subtype of the full type must be constrained if and only if
20470 -- the ancestor subtype of the private extension is constrained.
20471
20472 if No (Discriminant_Specifications (Parent (Priv_T)))
20473 and then not Has_Unknown_Discriminants (Priv_T)
20474 and then Has_Discriminants (Base_Type (Priv_Parent))
20475 then
20476 declare
20477 Priv_Indic : constant Node_Id :=
20478 Subtype_Indication (Parent (Priv_T));
20479
20480 Priv_Constr : constant Boolean :=
20481 Is_Constrained (Priv_Parent)
20482 or else
20483 Nkind (Priv_Indic) = N_Subtype_Indication
20484 or else
20485 Is_Constrained (Entity (Priv_Indic));
20486
20487 Full_Constr : constant Boolean :=
20488 Is_Constrained (Full_Parent)
20489 or else
20490 Nkind (Full_Indic) = N_Subtype_Indication
20491 or else
20492 Is_Constrained (Entity (Full_Indic));
20493
20494 Priv_Discr : Entity_Id;
20495 Full_Discr : Entity_Id;
20496
20497 begin
20498 Priv_Discr := First_Discriminant (Priv_Parent);
20499 Full_Discr := First_Discriminant (Full_Parent);
20500 while Present (Priv_Discr) and then Present (Full_Discr) loop
20501 if Original_Record_Component (Priv_Discr) =
20502 Original_Record_Component (Full_Discr)
20503 or else
20504 Corresponding_Discriminant (Priv_Discr) =
20505 Corresponding_Discriminant (Full_Discr)
20506 then
20507 null;
20508 else
20509 exit;
20510 end if;
20511
20512 Next_Discriminant (Priv_Discr);
20513 Next_Discriminant (Full_Discr);
20514 end loop;
20515
20516 if Present (Priv_Discr) or else Present (Full_Discr) then
20517 Error_Msg_N
20518 ("full view must inherit discriminants of the parent "
20519 & "type used in the private extension", Full_Indic);
20520
20521 elsif Priv_Constr and then not Full_Constr then
20522 Error_Msg_N
20523 ("parent subtype of full type must be constrained",
20524 Full_Indic);
20525
20526 elsif Full_Constr and then not Priv_Constr then
20527 Error_Msg_N
20528 ("parent subtype of full type must be unconstrained",
20529 Full_Indic);
20530 end if;
20531 end;
20532
20533 -- Check the rules of 7.3(12): if a partial view has neither
20534 -- known or unknown discriminants, then the full type
20535 -- declaration shall define a definite subtype.
20536
20537 elsif not Has_Unknown_Discriminants (Priv_T)
20538 and then not Has_Discriminants (Priv_T)
20539 and then not Is_Constrained (Full_T)
20540 then
20541 Error_Msg_N
20542 ("full view must define a constrained type if partial view "
20543 & "has no discriminants", Full_T);
20544 end if;
20545
20546 -- ??????? Do we implement the following properly ?????
20547 -- If the ancestor subtype of a private extension has constrained
20548 -- discriminants, then the parent subtype of the full view shall
20549 -- impose a statically matching constraint on those discriminants
20550 -- [7.3(13)].
20551 end if;
20552
20553 else
20554 -- For untagged types, verify that a type without discriminants is
20555 -- not completed with an unconstrained type. A separate error message
20556 -- is produced if the full type has defaulted discriminants.
20557
20558 if Is_Definite_Subtype (Priv_T)
20559 and then not Is_Definite_Subtype (Full_T)
20560 then
20561 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20562 Error_Msg_NE
20563 ("full view of& not compatible with declaration#",
20564 Full_T, Priv_T);
20565
20566 if not Is_Tagged_Type (Full_T) then
20567 Error_Msg_N
20568 ("\one is constrained, the other unconstrained", Full_T);
20569 end if;
20570 end if;
20571 end if;
20572
20573 -- AI-419: verify that the use of "limited" is consistent
20574
20575 declare
20576 Orig_Decl : constant Node_Id := Original_Node (N);
20577
20578 begin
20579 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20580 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20581 and then Nkind
20582 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20583 then
20584 if not Limited_Present (Parent (Priv_T))
20585 and then not Synchronized_Present (Parent (Priv_T))
20586 and then Limited_Present (Type_Definition (Orig_Decl))
20587 then
20588 Error_Msg_N
20589 ("full view of non-limited extension cannot be limited", N);
20590
20591 -- Conversely, if the partial view carries the limited keyword,
20592 -- the full view must as well, even if it may be redundant.
20593
20594 elsif Limited_Present (Parent (Priv_T))
20595 and then not Limited_Present (Type_Definition (Orig_Decl))
20596 then
20597 Error_Msg_N
20598 ("full view of limited extension must be explicitly limited",
20599 N);
20600 end if;
20601 end if;
20602 end;
20603
20604 -- Ada 2005 (AI-443): A synchronized private extension must be
20605 -- completed by a task or protected type.
20606
20607 if Ada_Version >= Ada_2005
20608 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20609 and then Synchronized_Present (Parent (Priv_T))
20610 and then not Is_Concurrent_Type (Full_T)
20611 then
20612 Error_Msg_N ("full view of synchronized extension must " &
20613 "be synchronized type", N);
20614 end if;
20615
20616 -- Ada 2005 AI-363: if the full view has discriminants with
20617 -- defaults, it is illegal to declare constrained access subtypes
20618 -- whose designated type is the current type. This allows objects
20619 -- of the type that are declared in the heap to be unconstrained.
20620
20621 if not Has_Unknown_Discriminants (Priv_T)
20622 and then not Has_Discriminants (Priv_T)
20623 and then Has_Discriminants (Full_T)
20624 and then
20625 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20626 then
20627 Set_Has_Constrained_Partial_View (Full_T);
20628 Set_Has_Constrained_Partial_View (Priv_T);
20629 end if;
20630
20631 -- Create a full declaration for all its subtypes recorded in
20632 -- Private_Dependents and swap them similarly to the base type. These
20633 -- are subtypes that have been define before the full declaration of
20634 -- the private type. We also swap the entry in Private_Dependents list
20635 -- so we can properly restore the private view on exit from the scope.
20636
20637 declare
20638 Priv_Elmt : Elmt_Id;
20639 Priv_Scop : Entity_Id;
20640 Priv : Entity_Id;
20641 Full : Entity_Id;
20642
20643 begin
20644 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20645 while Present (Priv_Elmt) loop
20646 Priv := Node (Priv_Elmt);
20647 Priv_Scop := Scope (Priv);
20648
20649 if Ekind_In (Priv, E_Private_Subtype,
20650 E_Limited_Private_Subtype,
20651 E_Record_Subtype_With_Private)
20652 then
20653 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20654 Set_Is_Itype (Full);
20655 Set_Parent (Full, Parent (Priv));
20656 Set_Associated_Node_For_Itype (Full, N);
20657
20658 -- Now we need to complete the private subtype, but since the
20659 -- base type has already been swapped, we must also swap the
20660 -- subtypes (and thus, reverse the arguments in the call to
20661 -- Complete_Private_Subtype). Also note that we may need to
20662 -- re-establish the scope of the private subtype.
20663
20664 Copy_And_Swap (Priv, Full);
20665
20666 if not In_Open_Scopes (Priv_Scop) then
20667 Push_Scope (Priv_Scop);
20668
20669 else
20670 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20671
20672 Priv_Scop := Empty;
20673 end if;
20674
20675 Complete_Private_Subtype (Full, Priv, Full_T, N);
20676
20677 if Present (Priv_Scop) then
20678 Pop_Scope;
20679 end if;
20680
20681 Replace_Elmt (Priv_Elmt, Full);
20682 end if;
20683
20684 Next_Elmt (Priv_Elmt);
20685 end loop;
20686 end;
20687
20688 -- If the private view was tagged, copy the new primitive operations
20689 -- from the private view to the full view.
20690
20691 if Is_Tagged_Type (Full_T) then
20692 declare
20693 Disp_Typ : Entity_Id;
20694 Full_List : Elist_Id;
20695 Prim : Entity_Id;
20696 Prim_Elmt : Elmt_Id;
20697 Priv_List : Elist_Id;
20698
20699 function Contains
20700 (E : Entity_Id;
20701 L : Elist_Id) return Boolean;
20702 -- Determine whether list L contains element E
20703
20704 --------------
20705 -- Contains --
20706 --------------
20707
20708 function Contains
20709 (E : Entity_Id;
20710 L : Elist_Id) return Boolean
20711 is
20712 List_Elmt : Elmt_Id;
20713
20714 begin
20715 List_Elmt := First_Elmt (L);
20716 while Present (List_Elmt) loop
20717 if Node (List_Elmt) = E then
20718 return True;
20719 end if;
20720
20721 Next_Elmt (List_Elmt);
20722 end loop;
20723
20724 return False;
20725 end Contains;
20726
20727 -- Start of processing
20728
20729 begin
20730 if Is_Tagged_Type (Priv_T) then
20731 Priv_List := Primitive_Operations (Priv_T);
20732 Prim_Elmt := First_Elmt (Priv_List);
20733
20734 -- In the case of a concurrent type completing a private tagged
20735 -- type, primitives may have been declared in between the two
20736 -- views. These subprograms need to be wrapped the same way
20737 -- entries and protected procedures are handled because they
20738 -- cannot be directly shared by the two views.
20739
20740 if Is_Concurrent_Type (Full_T) then
20741 declare
20742 Conc_Typ : constant Entity_Id :=
20743 Corresponding_Record_Type (Full_T);
20744 Curr_Nod : Node_Id := Parent (Conc_Typ);
20745 Wrap_Spec : Node_Id;
20746
20747 begin
20748 while Present (Prim_Elmt) loop
20749 Prim := Node (Prim_Elmt);
20750
20751 if Comes_From_Source (Prim)
20752 and then not Is_Abstract_Subprogram (Prim)
20753 then
20754 Wrap_Spec :=
20755 Make_Subprogram_Declaration (Sloc (Prim),
20756 Specification =>
20757 Build_Wrapper_Spec
20758 (Subp_Id => Prim,
20759 Obj_Typ => Conc_Typ,
20760 Formals =>
20761 Parameter_Specifications
20762 (Parent (Prim))));
20763
20764 Insert_After (Curr_Nod, Wrap_Spec);
20765 Curr_Nod := Wrap_Spec;
20766
20767 Analyze (Wrap_Spec);
20768
20769 -- Remove the wrapper from visibility to avoid
20770 -- spurious conflict with the wrapped entity.
20771
20772 Set_Is_Immediately_Visible
20773 (Defining_Entity (Specification (Wrap_Spec)),
20774 False);
20775 end if;
20776
20777 Next_Elmt (Prim_Elmt);
20778 end loop;
20779
20780 goto Leave;
20781 end;
20782
20783 -- For non-concurrent types, transfer explicit primitives, but
20784 -- omit those inherited from the parent of the private view
20785 -- since they will be re-inherited later on.
20786
20787 else
20788 Full_List := Primitive_Operations (Full_T);
20789 while Present (Prim_Elmt) loop
20790 Prim := Node (Prim_Elmt);
20791
20792 if Comes_From_Source (Prim)
20793 and then not Contains (Prim, Full_List)
20794 then
20795 Append_Elmt (Prim, Full_List);
20796 end if;
20797
20798 Next_Elmt (Prim_Elmt);
20799 end loop;
20800 end if;
20801
20802 -- Untagged private view
20803
20804 else
20805 Full_List := Primitive_Operations (Full_T);
20806
20807 -- In this case the partial view is untagged, so here we locate
20808 -- all of the earlier primitives that need to be treated as
20809 -- dispatching (those that appear between the two views). Note
20810 -- that these additional operations must all be new operations
20811 -- (any earlier operations that override inherited operations
20812 -- of the full view will already have been inserted in the
20813 -- primitives list, marked by Check_Operation_From_Private_View
20814 -- as dispatching. Note that implicit "/=" operators are
20815 -- excluded from being added to the primitives list since they
20816 -- shouldn't be treated as dispatching (tagged "/=" is handled
20817 -- specially).
20818
20819 Prim := Next_Entity (Full_T);
20820 while Present (Prim) and then Prim /= Priv_T loop
20821 if Ekind_In (Prim, E_Procedure, E_Function) then
20822 Disp_Typ := Find_Dispatching_Type (Prim);
20823
20824 if Disp_Typ = Full_T
20825 and then (Chars (Prim) /= Name_Op_Ne
20826 or else Comes_From_Source (Prim))
20827 then
20828 Check_Controlling_Formals (Full_T, Prim);
20829
20830 if Is_Suitable_Primitive (Prim)
20831 and then not Is_Dispatching_Operation (Prim)
20832 then
20833 Append_Elmt (Prim, Full_List);
20834 Set_Is_Dispatching_Operation (Prim);
20835 Set_DT_Position_Value (Prim, No_Uint);
20836 end if;
20837
20838 elsif Is_Dispatching_Operation (Prim)
20839 and then Disp_Typ /= Full_T
20840 then
20841 -- Verify that it is not otherwise controlled by a
20842 -- formal or a return value of type T.
20843
20844 Check_Controlling_Formals (Disp_Typ, Prim);
20845 end if;
20846 end if;
20847
20848 Next_Entity (Prim);
20849 end loop;
20850 end if;
20851
20852 -- For the tagged case, the two views can share the same primitive
20853 -- operations list and the same class-wide type. Update attributes
20854 -- of the class-wide type which depend on the full declaration.
20855
20856 if Is_Tagged_Type (Priv_T) then
20857 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20858 Set_Class_Wide_Type
20859 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20860
20861 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20862 end if;
20863 end;
20864 end if;
20865
20866 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20867
20868 if Known_To_Have_Preelab_Init (Priv_T) then
20869
20870 -- Case where there is a pragma Preelaborable_Initialization. We
20871 -- always allow this in predefined units, which is cheating a bit,
20872 -- but it means we don't have to struggle to meet the requirements in
20873 -- the RM for having Preelaborable Initialization. Otherwise we
20874 -- require that the type meets the RM rules. But we can't check that
20875 -- yet, because of the rule about overriding Initialize, so we simply
20876 -- set a flag that will be checked at freeze time.
20877
20878 if not In_Predefined_Unit (Full_T) then
20879 Set_Must_Have_Preelab_Init (Full_T);
20880 end if;
20881 end if;
20882
20883 -- If pragma CPP_Class was applied to the private type declaration,
20884 -- propagate it now to the full type declaration.
20885
20886 if Is_CPP_Class (Priv_T) then
20887 Set_Is_CPP_Class (Full_T);
20888 Set_Convention (Full_T, Convention_CPP);
20889
20890 -- Check that components of imported CPP types do not have default
20891 -- expressions.
20892
20893 Check_CPP_Type_Has_No_Defaults (Full_T);
20894 end if;
20895
20896 -- If the private view has user specified stream attributes, then so has
20897 -- the full view.
20898
20899 -- Why the test, how could these flags be already set in Full_T ???
20900
20901 if Has_Specified_Stream_Read (Priv_T) then
20902 Set_Has_Specified_Stream_Read (Full_T);
20903 end if;
20904
20905 if Has_Specified_Stream_Write (Priv_T) then
20906 Set_Has_Specified_Stream_Write (Full_T);
20907 end if;
20908
20909 if Has_Specified_Stream_Input (Priv_T) then
20910 Set_Has_Specified_Stream_Input (Full_T);
20911 end if;
20912
20913 if Has_Specified_Stream_Output (Priv_T) then
20914 Set_Has_Specified_Stream_Output (Full_T);
20915 end if;
20916
20917 -- Propagate Default_Initial_Condition-related attributes from the
20918 -- partial view to the full view and its base type.
20919
20920 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20921 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20922
20923 -- Propagate invariant-related attributes from the partial view to the
20924 -- full view and its base type.
20925
20926 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20927 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20928
20929 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20930 -- in the full view without advertising the inheritance in the partial
20931 -- view. This can only occur when the partial view has no parent type
20932 -- and the full view has an interface as a parent. Any other scenarios
20933 -- are illegal because implemented interfaces must match between the
20934 -- two views.
20935
20936 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20937 declare
20938 Full_Par : constant Entity_Id := Etype (Full_T);
20939 Priv_Par : constant Entity_Id := Etype (Priv_T);
20940
20941 begin
20942 if not Is_Interface (Priv_Par)
20943 and then Is_Interface (Full_Par)
20944 and then Has_Inheritable_Invariants (Full_Par)
20945 then
20946 Error_Msg_N
20947 ("hidden inheritance of class-wide type invariants not "
20948 & "allowed", N);
20949 end if;
20950 end;
20951 end if;
20952
20953 -- Propagate predicates to full type, and predicate function if already
20954 -- defined. It is not clear that this can actually happen? the partial
20955 -- view cannot be frozen yet, and the predicate function has not been
20956 -- built. Still it is a cheap check and seems safer to make it.
20957
20958 if Has_Predicates (Priv_T) then
20959 Set_Has_Predicates (Full_T);
20960
20961 if Present (Predicate_Function (Priv_T)) then
20962 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20963 end if;
20964 end if;
20965
20966 <<Leave>>
20967 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20968 end Process_Full_View;
20969
20970 -----------------------------------
20971 -- Process_Incomplete_Dependents --
20972 -----------------------------------
20973
20974 procedure Process_Incomplete_Dependents
20975 (N : Node_Id;
20976 Full_T : Entity_Id;
20977 Inc_T : Entity_Id)
20978 is
20979 Inc_Elmt : Elmt_Id;
20980 Priv_Dep : Entity_Id;
20981 New_Subt : Entity_Id;
20982
20983 Disc_Constraint : Elist_Id;
20984
20985 begin
20986 if No (Private_Dependents (Inc_T)) then
20987 return;
20988 end if;
20989
20990 -- Itypes that may be generated by the completion of an incomplete
20991 -- subtype are not used by the back-end and not attached to the tree.
20992 -- They are created only for constraint-checking purposes.
20993
20994 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20995 while Present (Inc_Elmt) loop
20996 Priv_Dep := Node (Inc_Elmt);
20997
20998 if Ekind (Priv_Dep) = E_Subprogram_Type then
20999
21000 -- An Access_To_Subprogram type may have a return type or a
21001 -- parameter type that is incomplete. Replace with the full view.
21002
21003 if Etype (Priv_Dep) = Inc_T then
21004 Set_Etype (Priv_Dep, Full_T);
21005 end if;
21006
21007 declare
21008 Formal : Entity_Id;
21009
21010 begin
21011 Formal := First_Formal (Priv_Dep);
21012 while Present (Formal) loop
21013 if Etype (Formal) = Inc_T then
21014 Set_Etype (Formal, Full_T);
21015 end if;
21016
21017 Next_Formal (Formal);
21018 end loop;
21019 end;
21020
21021 elsif Is_Overloadable (Priv_Dep) then
21022
21023 -- If a subprogram in the incomplete dependents list is primitive
21024 -- for a tagged full type then mark it as a dispatching operation,
21025 -- check whether it overrides an inherited subprogram, and check
21026 -- restrictions on its controlling formals. Note that a protected
21027 -- operation is never dispatching: only its wrapper operation
21028 -- (which has convention Ada) is.
21029
21030 if Is_Tagged_Type (Full_T)
21031 and then Is_Primitive (Priv_Dep)
21032 and then Convention (Priv_Dep) /= Convention_Protected
21033 then
21034 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21035 Set_Is_Dispatching_Operation (Priv_Dep);
21036 Check_Controlling_Formals (Full_T, Priv_Dep);
21037 end if;
21038
21039 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21040
21041 -- Can happen during processing of a body before the completion
21042 -- of a TA type. Ignore, because spec is also on dependent list.
21043
21044 return;
21045
21046 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21047 -- corresponding subtype of the full view.
21048
21049 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21050 and then Comes_From_Source (Priv_Dep)
21051 then
21052 Set_Subtype_Indication
21053 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21054 Set_Etype (Priv_Dep, Full_T);
21055 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21056 Set_Analyzed (Parent (Priv_Dep), False);
21057
21058 -- Reanalyze the declaration, suppressing the call to Enter_Name
21059 -- to avoid duplicate names.
21060
21061 Analyze_Subtype_Declaration
21062 (N => Parent (Priv_Dep),
21063 Skip => True);
21064
21065 -- Dependent is a subtype
21066
21067 else
21068 -- We build a new subtype indication using the full view of the
21069 -- incomplete parent. The discriminant constraints have been
21070 -- elaborated already at the point of the subtype declaration.
21071
21072 New_Subt := Create_Itype (E_Void, N);
21073
21074 if Has_Discriminants (Full_T) then
21075 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21076 else
21077 Disc_Constraint := No_Elist;
21078 end if;
21079
21080 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21081 Set_Full_View (Priv_Dep, New_Subt);
21082 end if;
21083
21084 Next_Elmt (Inc_Elmt);
21085 end loop;
21086 end Process_Incomplete_Dependents;
21087
21088 --------------------------------
21089 -- Process_Range_Expr_In_Decl --
21090 --------------------------------
21091
21092 procedure Process_Range_Expr_In_Decl
21093 (R : Node_Id;
21094 T : Entity_Id;
21095 Subtyp : Entity_Id := Empty;
21096 Check_List : List_Id := Empty_List;
21097 R_Check_Off : Boolean := False;
21098 In_Iter_Schm : Boolean := False)
21099 is
21100 Lo, Hi : Node_Id;
21101 R_Checks : Check_Result;
21102 Insert_Node : Node_Id;
21103 Def_Id : Entity_Id;
21104
21105 begin
21106 Analyze_And_Resolve (R, Base_Type (T));
21107
21108 if Nkind (R) = N_Range then
21109
21110 -- In SPARK, all ranges should be static, with the exception of the
21111 -- discrete type definition of a loop parameter specification.
21112
21113 if not In_Iter_Schm
21114 and then not Is_OK_Static_Range (R)
21115 then
21116 Check_SPARK_05_Restriction ("range should be static", R);
21117 end if;
21118
21119 Lo := Low_Bound (R);
21120 Hi := High_Bound (R);
21121
21122 -- Validity checks on the range of a quantified expression are
21123 -- delayed until the construct is transformed into a loop.
21124
21125 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21126 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21127 then
21128 null;
21129
21130 -- We need to ensure validity of the bounds here, because if we
21131 -- go ahead and do the expansion, then the expanded code will get
21132 -- analyzed with range checks suppressed and we miss the check.
21133
21134 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21135 -- the temporaries generated by routine Remove_Side_Effects by means
21136 -- of validity checks must use the same names. When a range appears
21137 -- in the parent of a generic, the range is processed with checks
21138 -- disabled as part of the generic context and with checks enabled
21139 -- for code generation purposes. This leads to link issues as the
21140 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21141 -- template sees the temporaries generated by Remove_Side_Effects.
21142
21143 else
21144 Validity_Check_Range (R, Subtyp);
21145 end if;
21146
21147 -- If there were errors in the declaration, try and patch up some
21148 -- common mistakes in the bounds. The cases handled are literals
21149 -- which are Integer where the expected type is Real and vice versa.
21150 -- These corrections allow the compilation process to proceed further
21151 -- along since some basic assumptions of the format of the bounds
21152 -- are guaranteed.
21153
21154 if Etype (R) = Any_Type then
21155 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21156 Rewrite (Lo,
21157 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21158
21159 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21160 Rewrite (Hi,
21161 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21162
21163 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21164 Rewrite (Lo,
21165 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21166
21167 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21168 Rewrite (Hi,
21169 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21170 end if;
21171
21172 Set_Etype (Lo, T);
21173 Set_Etype (Hi, T);
21174 end if;
21175
21176 -- If the bounds of the range have been mistakenly given as string
21177 -- literals (perhaps in place of character literals), then an error
21178 -- has already been reported, but we rewrite the string literal as a
21179 -- bound of the range's type to avoid blowups in later processing
21180 -- that looks at static values.
21181
21182 if Nkind (Lo) = N_String_Literal then
21183 Rewrite (Lo,
21184 Make_Attribute_Reference (Sloc (Lo),
21185 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21186 Attribute_Name => Name_First));
21187 Analyze_And_Resolve (Lo);
21188 end if;
21189
21190 if Nkind (Hi) = N_String_Literal then
21191 Rewrite (Hi,
21192 Make_Attribute_Reference (Sloc (Hi),
21193 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21194 Attribute_Name => Name_First));
21195 Analyze_And_Resolve (Hi);
21196 end if;
21197
21198 -- If bounds aren't scalar at this point then exit, avoiding
21199 -- problems with further processing of the range in this procedure.
21200
21201 if not Is_Scalar_Type (Etype (Lo)) then
21202 return;
21203 end if;
21204
21205 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21206 -- then range of the base type. Here we check whether the bounds
21207 -- are in the range of the subtype itself. Note that if the bounds
21208 -- represent the null range the Constraint_Error exception should
21209 -- not be raised.
21210
21211 -- ??? The following code should be cleaned up as follows
21212
21213 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21214 -- is done in the call to Range_Check (R, T); below
21215
21216 -- 2. The use of R_Check_Off should be investigated and possibly
21217 -- removed, this would clean up things a bit.
21218
21219 if Is_Null_Range (Lo, Hi) then
21220 null;
21221
21222 else
21223 -- Capture values of bounds and generate temporaries for them
21224 -- if needed, before applying checks, since checks may cause
21225 -- duplication of the expression without forcing evaluation.
21226
21227 -- The forced evaluation removes side effects from expressions,
21228 -- which should occur also in GNATprove mode. Otherwise, we end up
21229 -- with unexpected insertions of actions at places where this is
21230 -- not supposed to occur, e.g. on default parameters of a call.
21231
21232 if Expander_Active or GNATprove_Mode then
21233
21234 -- Call Force_Evaluation to create declarations as needed to
21235 -- deal with side effects, and also create typ_FIRST/LAST
21236 -- entities for bounds if we have a subtype name.
21237
21238 -- Note: we do this transformation even if expansion is not
21239 -- active if we are in GNATprove_Mode since the transformation
21240 -- is in general required to ensure that the resulting tree has
21241 -- proper Ada semantics.
21242
21243 Force_Evaluation
21244 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21245 Force_Evaluation
21246 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21247 end if;
21248
21249 -- We use a flag here instead of suppressing checks on the type
21250 -- because the type we check against isn't necessarily the place
21251 -- where we put the check.
21252
21253 if not R_Check_Off then
21254 R_Checks := Get_Range_Checks (R, T);
21255
21256 -- Look up tree to find an appropriate insertion point. We
21257 -- can't just use insert_actions because later processing
21258 -- depends on the insertion node. Prior to Ada 2012 the
21259 -- insertion point could only be a declaration or a loop, but
21260 -- quantified expressions can appear within any context in an
21261 -- expression, and the insertion point can be any statement,
21262 -- pragma, or declaration.
21263
21264 Insert_Node := Parent (R);
21265 while Present (Insert_Node) loop
21266 exit when
21267 Nkind (Insert_Node) in N_Declaration
21268 and then
21269 not Nkind_In
21270 (Insert_Node, N_Component_Declaration,
21271 N_Loop_Parameter_Specification,
21272 N_Function_Specification,
21273 N_Procedure_Specification);
21274
21275 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21276 or else Nkind (Insert_Node) in
21277 N_Statement_Other_Than_Procedure_Call
21278 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21279 N_Pragma);
21280
21281 Insert_Node := Parent (Insert_Node);
21282 end loop;
21283
21284 -- Why would Type_Decl not be present??? Without this test,
21285 -- short regression tests fail.
21286
21287 if Present (Insert_Node) then
21288
21289 -- Case of loop statement. Verify that the range is part
21290 -- of the subtype indication of the iteration scheme.
21291
21292 if Nkind (Insert_Node) = N_Loop_Statement then
21293 declare
21294 Indic : Node_Id;
21295
21296 begin
21297 Indic := Parent (R);
21298 while Present (Indic)
21299 and then Nkind (Indic) /= N_Subtype_Indication
21300 loop
21301 Indic := Parent (Indic);
21302 end loop;
21303
21304 if Present (Indic) then
21305 Def_Id := Etype (Subtype_Mark (Indic));
21306
21307 Insert_Range_Checks
21308 (R_Checks,
21309 Insert_Node,
21310 Def_Id,
21311 Sloc (Insert_Node),
21312 R,
21313 Do_Before => True);
21314 end if;
21315 end;
21316
21317 -- Insertion before a declaration. If the declaration
21318 -- includes discriminants, the list of applicable checks
21319 -- is given by the caller.
21320
21321 elsif Nkind (Insert_Node) in N_Declaration then
21322 Def_Id := Defining_Identifier (Insert_Node);
21323
21324 if (Ekind (Def_Id) = E_Record_Type
21325 and then Depends_On_Discriminant (R))
21326 or else
21327 (Ekind (Def_Id) = E_Protected_Type
21328 and then Has_Discriminants (Def_Id))
21329 then
21330 Append_Range_Checks
21331 (R_Checks,
21332 Check_List, Def_Id, Sloc (Insert_Node), R);
21333
21334 else
21335 Insert_Range_Checks
21336 (R_Checks,
21337 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21338
21339 end if;
21340
21341 -- Insertion before a statement. Range appears in the
21342 -- context of a quantified expression. Insertion will
21343 -- take place when expression is expanded.
21344
21345 else
21346 null;
21347 end if;
21348 end if;
21349 end if;
21350 end if;
21351
21352 -- Case of other than an explicit N_Range node
21353
21354 -- The forced evaluation removes side effects from expressions, which
21355 -- should occur also in GNATprove mode. Otherwise, we end up with
21356 -- unexpected insertions of actions at places where this is not
21357 -- supposed to occur, e.g. on default parameters of a call.
21358
21359 elsif Expander_Active or GNATprove_Mode then
21360 Get_Index_Bounds (R, Lo, Hi);
21361 Force_Evaluation (Lo);
21362 Force_Evaluation (Hi);
21363 end if;
21364 end Process_Range_Expr_In_Decl;
21365
21366 --------------------------------------
21367 -- Process_Real_Range_Specification --
21368 --------------------------------------
21369
21370 procedure Process_Real_Range_Specification (Def : Node_Id) is
21371 Spec : constant Node_Id := Real_Range_Specification (Def);
21372 Lo : Node_Id;
21373 Hi : Node_Id;
21374 Err : Boolean := False;
21375
21376 procedure Analyze_Bound (N : Node_Id);
21377 -- Analyze and check one bound
21378
21379 -------------------
21380 -- Analyze_Bound --
21381 -------------------
21382
21383 procedure Analyze_Bound (N : Node_Id) is
21384 begin
21385 Analyze_And_Resolve (N, Any_Real);
21386
21387 if not Is_OK_Static_Expression (N) then
21388 Flag_Non_Static_Expr
21389 ("bound in real type definition is not static!", N);
21390 Err := True;
21391 end if;
21392 end Analyze_Bound;
21393
21394 -- Start of processing for Process_Real_Range_Specification
21395
21396 begin
21397 if Present (Spec) then
21398 Lo := Low_Bound (Spec);
21399 Hi := High_Bound (Spec);
21400 Analyze_Bound (Lo);
21401 Analyze_Bound (Hi);
21402
21403 -- If error, clear away junk range specification
21404
21405 if Err then
21406 Set_Real_Range_Specification (Def, Empty);
21407 end if;
21408 end if;
21409 end Process_Real_Range_Specification;
21410
21411 ---------------------
21412 -- Process_Subtype --
21413 ---------------------
21414
21415 function Process_Subtype
21416 (S : Node_Id;
21417 Related_Nod : Node_Id;
21418 Related_Id : Entity_Id := Empty;
21419 Suffix : Character := ' ') return Entity_Id
21420 is
21421 P : Node_Id;
21422 Def_Id : Entity_Id;
21423 Error_Node : Node_Id;
21424 Full_View_Id : Entity_Id;
21425 Subtype_Mark_Id : Entity_Id;
21426
21427 May_Have_Null_Exclusion : Boolean;
21428
21429 procedure Check_Incomplete (T : Node_Id);
21430 -- Called to verify that an incomplete type is not used prematurely
21431
21432 ----------------------
21433 -- Check_Incomplete --
21434 ----------------------
21435
21436 procedure Check_Incomplete (T : Node_Id) is
21437 begin
21438 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21439
21440 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21441 and then
21442 not (Ada_Version >= Ada_2005
21443 and then
21444 (Nkind (Parent (T)) = N_Subtype_Declaration
21445 or else (Nkind (Parent (T)) = N_Subtype_Indication
21446 and then Nkind (Parent (Parent (T))) =
21447 N_Subtype_Declaration)))
21448 then
21449 Error_Msg_N ("invalid use of type before its full declaration", T);
21450 end if;
21451 end Check_Incomplete;
21452
21453 -- Start of processing for Process_Subtype
21454
21455 begin
21456 -- Case of no constraints present
21457
21458 if Nkind (S) /= N_Subtype_Indication then
21459 Find_Type (S);
21460
21461 -- No way to proceed if the subtype indication is malformed. This
21462 -- will happen for example when the subtype indication in an object
21463 -- declaration is missing altogether and the expression is analyzed
21464 -- as if it were that indication.
21465
21466 if not Is_Entity_Name (S) then
21467 return Any_Type;
21468 end if;
21469
21470 Check_Incomplete (S);
21471 P := Parent (S);
21472
21473 -- Ada 2005 (AI-231): Static check
21474
21475 if Ada_Version >= Ada_2005
21476 and then Present (P)
21477 and then Null_Exclusion_Present (P)
21478 and then Nkind (P) /= N_Access_To_Object_Definition
21479 and then not Is_Access_Type (Entity (S))
21480 then
21481 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21482 end if;
21483
21484 -- The following is ugly, can't we have a range or even a flag???
21485
21486 May_Have_Null_Exclusion :=
21487 Nkind_In (P, N_Access_Definition,
21488 N_Access_Function_Definition,
21489 N_Access_Procedure_Definition,
21490 N_Access_To_Object_Definition,
21491 N_Allocator,
21492 N_Component_Definition)
21493 or else
21494 Nkind_In (P, N_Derived_Type_Definition,
21495 N_Discriminant_Specification,
21496 N_Formal_Object_Declaration,
21497 N_Object_Declaration,
21498 N_Object_Renaming_Declaration,
21499 N_Parameter_Specification,
21500 N_Subtype_Declaration);
21501
21502 -- Create an Itype that is a duplicate of Entity (S) but with the
21503 -- null-exclusion attribute.
21504
21505 if May_Have_Null_Exclusion
21506 and then Is_Access_Type (Entity (S))
21507 and then Null_Exclusion_Present (P)
21508
21509 -- No need to check the case of an access to object definition.
21510 -- It is correct to define double not-null pointers.
21511
21512 -- Example:
21513 -- type Not_Null_Int_Ptr is not null access Integer;
21514 -- type Acc is not null access Not_Null_Int_Ptr;
21515
21516 and then Nkind (P) /= N_Access_To_Object_Definition
21517 then
21518 if Can_Never_Be_Null (Entity (S)) then
21519 case Nkind (Related_Nod) is
21520 when N_Full_Type_Declaration =>
21521 if Nkind (Type_Definition (Related_Nod))
21522 in N_Array_Type_Definition
21523 then
21524 Error_Node :=
21525 Subtype_Indication
21526 (Component_Definition
21527 (Type_Definition (Related_Nod)));
21528 else
21529 Error_Node :=
21530 Subtype_Indication (Type_Definition (Related_Nod));
21531 end if;
21532
21533 when N_Subtype_Declaration =>
21534 Error_Node := Subtype_Indication (Related_Nod);
21535
21536 when N_Object_Declaration =>
21537 Error_Node := Object_Definition (Related_Nod);
21538
21539 when N_Component_Declaration =>
21540 Error_Node :=
21541 Subtype_Indication (Component_Definition (Related_Nod));
21542
21543 when N_Allocator =>
21544 Error_Node := Expression (Related_Nod);
21545
21546 when others =>
21547 pragma Assert (False);
21548 Error_Node := Related_Nod;
21549 end case;
21550
21551 Error_Msg_NE
21552 ("`NOT NULL` not allowed (& already excludes null)",
21553 Error_Node,
21554 Entity (S));
21555 end if;
21556
21557 Set_Etype (S,
21558 Create_Null_Excluding_Itype
21559 (T => Entity (S),
21560 Related_Nod => P));
21561 Set_Entity (S, Etype (S));
21562 end if;
21563
21564 return Entity (S);
21565
21566 -- Case of constraint present, so that we have an N_Subtype_Indication
21567 -- node (this node is created only if constraints are present).
21568
21569 else
21570 Find_Type (Subtype_Mark (S));
21571
21572 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21573 and then not
21574 (Nkind (Parent (S)) = N_Subtype_Declaration
21575 and then Is_Itype (Defining_Identifier (Parent (S))))
21576 then
21577 Check_Incomplete (Subtype_Mark (S));
21578 end if;
21579
21580 P := Parent (S);
21581 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21582
21583 -- Explicit subtype declaration case
21584
21585 if Nkind (P) = N_Subtype_Declaration then
21586 Def_Id := Defining_Identifier (P);
21587
21588 -- Explicit derived type definition case
21589
21590 elsif Nkind (P) = N_Derived_Type_Definition then
21591 Def_Id := Defining_Identifier (Parent (P));
21592
21593 -- Implicit case, the Def_Id must be created as an implicit type.
21594 -- The one exception arises in the case of concurrent types, array
21595 -- and access types, where other subsidiary implicit types may be
21596 -- created and must appear before the main implicit type. In these
21597 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21598 -- has not yet been called to create Def_Id.
21599
21600 else
21601 if Is_Array_Type (Subtype_Mark_Id)
21602 or else Is_Concurrent_Type (Subtype_Mark_Id)
21603 or else Is_Access_Type (Subtype_Mark_Id)
21604 then
21605 Def_Id := Empty;
21606
21607 -- For the other cases, we create a new unattached Itype,
21608 -- and set the indication to ensure it gets attached later.
21609
21610 else
21611 Def_Id :=
21612 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21613 end if;
21614 end if;
21615
21616 -- If the kind of constraint is invalid for this kind of type,
21617 -- then give an error, and then pretend no constraint was given.
21618
21619 if not Is_Valid_Constraint_Kind
21620 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21621 then
21622 Error_Msg_N
21623 ("incorrect constraint for this kind of type", Constraint (S));
21624
21625 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21626
21627 -- Set Ekind of orphan itype, to prevent cascaded errors
21628
21629 if Present (Def_Id) then
21630 Set_Ekind (Def_Id, Ekind (Any_Type));
21631 end if;
21632
21633 -- Make recursive call, having got rid of the bogus constraint
21634
21635 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21636 end if;
21637
21638 -- Remaining processing depends on type. Select on Base_Type kind to
21639 -- ensure getting to the concrete type kind in the case of a private
21640 -- subtype (needed when only doing semantic analysis).
21641
21642 case Ekind (Base_Type (Subtype_Mark_Id)) is
21643 when Access_Kind =>
21644
21645 -- If this is a constraint on a class-wide type, discard it.
21646 -- There is currently no way to express a partial discriminant
21647 -- constraint on a type with unknown discriminants. This is
21648 -- a pathology that the ACATS wisely decides not to test.
21649
21650 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21651 if Comes_From_Source (S) then
21652 Error_Msg_N
21653 ("constraint on class-wide type ignored??",
21654 Constraint (S));
21655 end if;
21656
21657 if Nkind (P) = N_Subtype_Declaration then
21658 Set_Subtype_Indication (P,
21659 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21660 end if;
21661
21662 return Subtype_Mark_Id;
21663 end if;
21664
21665 Constrain_Access (Def_Id, S, Related_Nod);
21666
21667 if Expander_Active
21668 and then Is_Itype (Designated_Type (Def_Id))
21669 and then Nkind (Related_Nod) = N_Subtype_Declaration
21670 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21671 then
21672 Build_Itype_Reference
21673 (Designated_Type (Def_Id), Related_Nod);
21674 end if;
21675
21676 when Array_Kind =>
21677 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21678
21679 when Decimal_Fixed_Point_Kind =>
21680 Constrain_Decimal (Def_Id, S);
21681
21682 when Enumeration_Kind =>
21683 Constrain_Enumeration (Def_Id, S);
21684
21685 when Ordinary_Fixed_Point_Kind =>
21686 Constrain_Ordinary_Fixed (Def_Id, S);
21687
21688 when Float_Kind =>
21689 Constrain_Float (Def_Id, S);
21690
21691 when Integer_Kind =>
21692 Constrain_Integer (Def_Id, S);
21693
21694 when Class_Wide_Kind
21695 | E_Incomplete_Type
21696 | E_Record_Subtype
21697 | E_Record_Type
21698 =>
21699 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21700
21701 if Ekind (Def_Id) = E_Incomplete_Type then
21702 Set_Private_Dependents (Def_Id, New_Elmt_List);
21703 end if;
21704
21705 when Private_Kind =>
21706
21707 -- A private type with unknown discriminants may be completed
21708 -- by an unconstrained array type.
21709
21710 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21711 and then Present (Full_View (Subtype_Mark_Id))
21712 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21713 then
21714 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21715
21716 -- ... but more commonly is completed by a discriminated record
21717 -- type.
21718
21719 else
21720 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21721 end if;
21722
21723 -- The base type may be private but Def_Id may be a full view
21724 -- in an instance.
21725
21726 if Is_Private_Type (Def_Id) then
21727 Set_Private_Dependents (Def_Id, New_Elmt_List);
21728 end if;
21729
21730 -- In case of an invalid constraint prevent further processing
21731 -- since the type constructed is missing expected fields.
21732
21733 if Etype (Def_Id) = Any_Type then
21734 return Def_Id;
21735 end if;
21736
21737 -- If the full view is that of a task with discriminants,
21738 -- we must constrain both the concurrent type and its
21739 -- corresponding record type. Otherwise we will just propagate
21740 -- the constraint to the full view, if available.
21741
21742 if Present (Full_View (Subtype_Mark_Id))
21743 and then Has_Discriminants (Subtype_Mark_Id)
21744 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21745 then
21746 Full_View_Id :=
21747 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21748
21749 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21750 Constrain_Concurrent (Full_View_Id, S,
21751 Related_Nod, Related_Id, Suffix);
21752 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21753 Set_Full_View (Def_Id, Full_View_Id);
21754
21755 -- Introduce an explicit reference to the private subtype,
21756 -- to prevent scope anomalies in gigi if first use appears
21757 -- in a nested context, e.g. a later function body.
21758 -- Should this be generated in other contexts than a full
21759 -- type declaration?
21760
21761 if Is_Itype (Def_Id)
21762 and then
21763 Nkind (Parent (P)) = N_Full_Type_Declaration
21764 then
21765 Build_Itype_Reference (Def_Id, Parent (P));
21766 end if;
21767
21768 else
21769 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21770 end if;
21771
21772 when Concurrent_Kind =>
21773 Constrain_Concurrent (Def_Id, S,
21774 Related_Nod, Related_Id, Suffix);
21775
21776 when others =>
21777 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21778 end case;
21779
21780 -- Size, Alignment, Representation aspects and Convention are always
21781 -- inherited from the base type.
21782
21783 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21784 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21785 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21786
21787 -- The anonymous subtype created for the subtype indication
21788 -- inherits the predicates of the parent.
21789
21790 if Has_Predicates (Subtype_Mark_Id) then
21791 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21792
21793 -- Indicate where the predicate function may be found
21794
21795 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21796 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21797 end if;
21798 end if;
21799
21800 return Def_Id;
21801 end if;
21802 end Process_Subtype;
21803
21804 -----------------------------
21805 -- Record_Type_Declaration --
21806 -----------------------------
21807
21808 procedure Record_Type_Declaration
21809 (T : Entity_Id;
21810 N : Node_Id;
21811 Prev : Entity_Id)
21812 is
21813 Def : constant Node_Id := Type_Definition (N);
21814 Is_Tagged : Boolean;
21815 Tag_Comp : Entity_Id;
21816
21817 begin
21818 -- These flags must be initialized before calling Process_Discriminants
21819 -- because this routine makes use of them.
21820
21821 Set_Ekind (T, E_Record_Type);
21822 Set_Etype (T, T);
21823 Init_Size_Align (T);
21824 Set_Interfaces (T, No_Elist);
21825 Set_Stored_Constraint (T, No_Elist);
21826 Set_Default_SSO (T);
21827 Set_No_Reordering (T, No_Component_Reordering);
21828
21829 -- Normal case
21830
21831 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21832 if Limited_Present (Def) then
21833 Check_SPARK_05_Restriction ("limited is not allowed", N);
21834 end if;
21835
21836 if Abstract_Present (Def) then
21837 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21838 end if;
21839
21840 -- The flag Is_Tagged_Type might have already been set by
21841 -- Find_Type_Name if it detected an error for declaration T. This
21842 -- arises in the case of private tagged types where the full view
21843 -- omits the word tagged.
21844
21845 Is_Tagged :=
21846 Tagged_Present (Def)
21847 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21848
21849 Set_Is_Limited_Record (T, Limited_Present (Def));
21850
21851 if Is_Tagged then
21852 Set_Is_Tagged_Type (T, True);
21853 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21854 end if;
21855
21856 -- Type is abstract if full declaration carries keyword, or if
21857 -- previous partial view did.
21858
21859 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21860 or else Abstract_Present (Def));
21861
21862 else
21863 Check_SPARK_05_Restriction ("interface is not allowed", N);
21864
21865 Is_Tagged := True;
21866 Analyze_Interface_Declaration (T, Def);
21867
21868 if Present (Discriminant_Specifications (N)) then
21869 Error_Msg_N
21870 ("interface types cannot have discriminants",
21871 Defining_Identifier
21872 (First (Discriminant_Specifications (N))));
21873 end if;
21874 end if;
21875
21876 -- First pass: if there are self-referential access components,
21877 -- create the required anonymous access type declarations, and if
21878 -- need be an incomplete type declaration for T itself.
21879
21880 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21881
21882 if Ada_Version >= Ada_2005
21883 and then Present (Interface_List (Def))
21884 then
21885 Check_Interfaces (N, Def);
21886
21887 declare
21888 Ifaces_List : Elist_Id;
21889
21890 begin
21891 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21892 -- already in the parents.
21893
21894 Collect_Interfaces
21895 (T => T,
21896 Ifaces_List => Ifaces_List,
21897 Exclude_Parents => True);
21898
21899 Set_Interfaces (T, Ifaces_List);
21900 end;
21901 end if;
21902
21903 -- Records constitute a scope for the component declarations within.
21904 -- The scope is created prior to the processing of these declarations.
21905 -- Discriminants are processed first, so that they are visible when
21906 -- processing the other components. The Ekind of the record type itself
21907 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21908
21909 -- Enter record scope
21910
21911 Push_Scope (T);
21912
21913 -- If an incomplete or private type declaration was already given for
21914 -- the type, then this scope already exists, and the discriminants have
21915 -- been declared within. We must verify that the full declaration
21916 -- matches the incomplete one.
21917
21918 Check_Or_Process_Discriminants (N, T, Prev);
21919
21920 Set_Is_Constrained (T, not Has_Discriminants (T));
21921 Set_Has_Delayed_Freeze (T, True);
21922
21923 -- For tagged types add a manually analyzed component corresponding
21924 -- to the component _tag, the corresponding piece of tree will be
21925 -- expanded as part of the freezing actions if it is not a CPP_Class.
21926
21927 if Is_Tagged then
21928
21929 -- Do not add the tag unless we are in expansion mode
21930
21931 if Expander_Active then
21932 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21933 Enter_Name (Tag_Comp);
21934
21935 Set_Ekind (Tag_Comp, E_Component);
21936 Set_Is_Tag (Tag_Comp);
21937 Set_Is_Aliased (Tag_Comp);
21938 Set_Etype (Tag_Comp, RTE (RE_Tag));
21939 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21940 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21941 Init_Component_Location (Tag_Comp);
21942
21943 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21944 -- implemented interfaces.
21945
21946 if Has_Interfaces (T) then
21947 Add_Interface_Tag_Components (N, T);
21948 end if;
21949 end if;
21950
21951 Make_Class_Wide_Type (T);
21952 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21953 end if;
21954
21955 -- We must suppress range checks when processing record components in
21956 -- the presence of discriminants, since we don't want spurious checks to
21957 -- be generated during their analysis, but Suppress_Range_Checks flags
21958 -- must be reset the after processing the record definition.
21959
21960 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21961 -- couldn't we just use the normal range check suppression method here.
21962 -- That would seem cleaner ???
21963
21964 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21965 Set_Kill_Range_Checks (T, True);
21966 Record_Type_Definition (Def, Prev);
21967 Set_Kill_Range_Checks (T, False);
21968 else
21969 Record_Type_Definition (Def, Prev);
21970 end if;
21971
21972 -- Exit from record scope
21973
21974 End_Scope;
21975
21976 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21977 -- the implemented interfaces and associate them an aliased entity.
21978
21979 if Is_Tagged
21980 and then not Is_Empty_List (Interface_List (Def))
21981 then
21982 Derive_Progenitor_Subprograms (T, T);
21983 end if;
21984
21985 Check_Function_Writable_Actuals (N);
21986 end Record_Type_Declaration;
21987
21988 ----------------------------
21989 -- Record_Type_Definition --
21990 ----------------------------
21991
21992 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21993 Component : Entity_Id;
21994 Ctrl_Components : Boolean := False;
21995 Final_Storage_Only : Boolean;
21996 T : Entity_Id;
21997
21998 begin
21999 if Ekind (Prev_T) = E_Incomplete_Type then
22000 T := Full_View (Prev_T);
22001 else
22002 T := Prev_T;
22003 end if;
22004
22005 -- In SPARK, tagged types and type extensions may only be declared in
22006 -- the specification of library unit packages.
22007
22008 if Present (Def) and then Is_Tagged_Type (T) then
22009 declare
22010 Typ : Node_Id;
22011 Ctxt : Node_Id;
22012
22013 begin
22014 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22015 Typ := Parent (Def);
22016 else
22017 pragma Assert
22018 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22019 Typ := Parent (Parent (Def));
22020 end if;
22021
22022 Ctxt := Parent (Typ);
22023
22024 if Nkind (Ctxt) = N_Package_Body
22025 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22026 then
22027 Check_SPARK_05_Restriction
22028 ("type should be defined in package specification", Typ);
22029
22030 elsif Nkind (Ctxt) /= N_Package_Specification
22031 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22032 then
22033 Check_SPARK_05_Restriction
22034 ("type should be defined in library unit package", Typ);
22035 end if;
22036 end;
22037 end if;
22038
22039 Final_Storage_Only := not Is_Controlled (T);
22040
22041 -- Ada 2005: Check whether an explicit Limited is present in a derived
22042 -- type declaration.
22043
22044 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22045 and then Limited_Present (Parent (Def))
22046 then
22047 Set_Is_Limited_Record (T);
22048 end if;
22049
22050 -- If the component list of a record type is defined by the reserved
22051 -- word null and there is no discriminant part, then the record type has
22052 -- no components and all records of the type are null records (RM 3.7)
22053 -- This procedure is also called to process the extension part of a
22054 -- record extension, in which case the current scope may have inherited
22055 -- components.
22056
22057 if No (Def)
22058 or else No (Component_List (Def))
22059 or else Null_Present (Component_List (Def))
22060 then
22061 if not Is_Tagged_Type (T) then
22062 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22063 end if;
22064
22065 else
22066 Analyze_Declarations (Component_Items (Component_List (Def)));
22067
22068 if Present (Variant_Part (Component_List (Def))) then
22069 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22070 Analyze (Variant_Part (Component_List (Def)));
22071 end if;
22072 end if;
22073
22074 -- After completing the semantic analysis of the record definition,
22075 -- record components, both new and inherited, are accessible. Set their
22076 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22077 -- whose Ekind may be void.
22078
22079 Component := First_Entity (Current_Scope);
22080 while Present (Component) loop
22081 if Ekind (Component) = E_Void
22082 and then not Is_Itype (Component)
22083 then
22084 Set_Ekind (Component, E_Component);
22085 Init_Component_Location (Component);
22086 end if;
22087
22088 Propagate_Concurrent_Flags (T, Etype (Component));
22089
22090 if Ekind (Component) /= E_Component then
22091 null;
22092
22093 -- Do not set Has_Controlled_Component on a class-wide equivalent
22094 -- type. See Make_CW_Equivalent_Type.
22095
22096 elsif not Is_Class_Wide_Equivalent_Type (T)
22097 and then (Has_Controlled_Component (Etype (Component))
22098 or else (Chars (Component) /= Name_uParent
22099 and then Is_Controlled (Etype (Component))))
22100 then
22101 Set_Has_Controlled_Component (T, True);
22102 Final_Storage_Only :=
22103 Final_Storage_Only
22104 and then Finalize_Storage_Only (Etype (Component));
22105 Ctrl_Components := True;
22106 end if;
22107
22108 Next_Entity (Component);
22109 end loop;
22110
22111 -- A Type is Finalize_Storage_Only only if all its controlled components
22112 -- are also.
22113
22114 if Ctrl_Components then
22115 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22116 end if;
22117
22118 -- Place reference to end record on the proper entity, which may
22119 -- be a partial view.
22120
22121 if Present (Def) then
22122 Process_End_Label (Def, 'e', Prev_T);
22123 end if;
22124 end Record_Type_Definition;
22125
22126 ------------------------
22127 -- Replace_Components --
22128 ------------------------
22129
22130 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
22131 function Process (N : Node_Id) return Traverse_Result;
22132
22133 -------------
22134 -- Process --
22135 -------------
22136
22137 function Process (N : Node_Id) return Traverse_Result is
22138 Comp : Entity_Id;
22139
22140 begin
22141 if Nkind (N) = N_Discriminant_Specification then
22142 Comp := First_Discriminant (Typ);
22143 while Present (Comp) loop
22144 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22145 Set_Defining_Identifier (N, Comp);
22146 exit;
22147 end if;
22148
22149 Next_Discriminant (Comp);
22150 end loop;
22151
22152 elsif Nkind (N) = N_Variant_Part then
22153 Comp := First_Discriminant (Typ);
22154 while Present (Comp) loop
22155 if Chars (Comp) = Chars (Name (N)) then
22156 Set_Entity (Name (N), Comp);
22157 exit;
22158 end if;
22159
22160 Next_Discriminant (Comp);
22161 end loop;
22162
22163 elsif Nkind (N) = N_Component_Declaration then
22164 Comp := First_Component (Typ);
22165 while Present (Comp) loop
22166 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22167 Set_Defining_Identifier (N, Comp);
22168 exit;
22169 end if;
22170
22171 Next_Component (Comp);
22172 end loop;
22173 end if;
22174
22175 return OK;
22176 end Process;
22177
22178 procedure Replace is new Traverse_Proc (Process);
22179
22180 -- Start of processing for Replace_Components
22181
22182 begin
22183 Replace (Decl);
22184 end Replace_Components;
22185
22186 -------------------------------
22187 -- Set_Completion_Referenced --
22188 -------------------------------
22189
22190 procedure Set_Completion_Referenced (E : Entity_Id) is
22191 begin
22192 -- If in main unit, mark entity that is a completion as referenced,
22193 -- warnings go on the partial view when needed.
22194
22195 if In_Extended_Main_Source_Unit (E) then
22196 Set_Referenced (E);
22197 end if;
22198 end Set_Completion_Referenced;
22199
22200 ---------------------
22201 -- Set_Default_SSO --
22202 ---------------------
22203
22204 procedure Set_Default_SSO (T : Entity_Id) is
22205 begin
22206 case Opt.Default_SSO is
22207 when ' ' =>
22208 null;
22209 when 'L' =>
22210 Set_SSO_Set_Low_By_Default (T, True);
22211 when 'H' =>
22212 Set_SSO_Set_High_By_Default (T, True);
22213 when others =>
22214 raise Program_Error;
22215 end case;
22216 end Set_Default_SSO;
22217
22218 ---------------------
22219 -- Set_Fixed_Range --
22220 ---------------------
22221
22222 -- The range for fixed-point types is complicated by the fact that we
22223 -- do not know the exact end points at the time of the declaration. This
22224 -- is true for three reasons:
22225
22226 -- A size clause may affect the fudging of the end-points.
22227 -- A small clause may affect the values of the end-points.
22228 -- We try to include the end-points if it does not affect the size.
22229
22230 -- This means that the actual end-points must be established at the
22231 -- point when the type is frozen. Meanwhile, we first narrow the range
22232 -- as permitted (so that it will fit if necessary in a small specified
22233 -- size), and then build a range subtree with these narrowed bounds.
22234 -- Set_Fixed_Range constructs the range from real literal values, and
22235 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22236
22237 -- The parent of this range is set to point to the entity so that it is
22238 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22239 -- other scalar types, which are just pointers to the range in the
22240 -- original tree, this would otherwise be an orphan).
22241
22242 -- The tree is left unanalyzed. When the type is frozen, the processing
22243 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22244 -- analyzed, and uses this as an indication that it should complete
22245 -- work on the range (it will know the final small and size values).
22246
22247 procedure Set_Fixed_Range
22248 (E : Entity_Id;
22249 Loc : Source_Ptr;
22250 Lo : Ureal;
22251 Hi : Ureal)
22252 is
22253 S : constant Node_Id :=
22254 Make_Range (Loc,
22255 Low_Bound => Make_Real_Literal (Loc, Lo),
22256 High_Bound => Make_Real_Literal (Loc, Hi));
22257 begin
22258 Set_Scalar_Range (E, S);
22259 Set_Parent (S, E);
22260
22261 -- Before the freeze point, the bounds of a fixed point are universal
22262 -- and carry the corresponding type.
22263
22264 Set_Etype (Low_Bound (S), Universal_Real);
22265 Set_Etype (High_Bound (S), Universal_Real);
22266 end Set_Fixed_Range;
22267
22268 ----------------------------------
22269 -- Set_Scalar_Range_For_Subtype --
22270 ----------------------------------
22271
22272 procedure Set_Scalar_Range_For_Subtype
22273 (Def_Id : Entity_Id;
22274 R : Node_Id;
22275 Subt : Entity_Id)
22276 is
22277 Kind : constant Entity_Kind := Ekind (Def_Id);
22278
22279 begin
22280 -- Defend against previous error
22281
22282 if Nkind (R) = N_Error then
22283 return;
22284 end if;
22285
22286 Set_Scalar_Range (Def_Id, R);
22287
22288 -- We need to link the range into the tree before resolving it so
22289 -- that types that are referenced, including importantly the subtype
22290 -- itself, are properly frozen (Freeze_Expression requires that the
22291 -- expression be properly linked into the tree). Of course if it is
22292 -- already linked in, then we do not disturb the current link.
22293
22294 if No (Parent (R)) then
22295 Set_Parent (R, Def_Id);
22296 end if;
22297
22298 -- Reset the kind of the subtype during analysis of the range, to
22299 -- catch possible premature use in the bounds themselves.
22300
22301 Set_Ekind (Def_Id, E_Void);
22302 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22303 Set_Ekind (Def_Id, Kind);
22304 end Set_Scalar_Range_For_Subtype;
22305
22306 --------------------------------------------------------
22307 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22308 --------------------------------------------------------
22309
22310 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22311 (E : Entity_Id)
22312 is
22313 begin
22314 -- Make sure set if encountered during Expand_To_Stored_Constraint
22315
22316 Set_Stored_Constraint (E, No_Elist);
22317
22318 -- Give it the right value
22319
22320 if Is_Constrained (E) and then Has_Discriminants (E) then
22321 Set_Stored_Constraint (E,
22322 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22323 end if;
22324 end Set_Stored_Constraint_From_Discriminant_Constraint;
22325
22326 -------------------------------------
22327 -- Signed_Integer_Type_Declaration --
22328 -------------------------------------
22329
22330 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22331 Implicit_Base : Entity_Id;
22332 Base_Typ : Entity_Id;
22333 Lo_Val : Uint;
22334 Hi_Val : Uint;
22335 Errs : Boolean := False;
22336 Lo : Node_Id;
22337 Hi : Node_Id;
22338
22339 function Can_Derive_From (E : Entity_Id) return Boolean;
22340 -- Determine whether given bounds allow derivation from specified type
22341
22342 procedure Check_Bound (Expr : Node_Id);
22343 -- Check bound to make sure it is integral and static. If not, post
22344 -- appropriate error message and set Errs flag
22345
22346 ---------------------
22347 -- Can_Derive_From --
22348 ---------------------
22349
22350 -- Note we check both bounds against both end values, to deal with
22351 -- strange types like ones with a range of 0 .. -12341234.
22352
22353 function Can_Derive_From (E : Entity_Id) return Boolean is
22354 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22355 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22356 begin
22357 return Lo <= Lo_Val and then Lo_Val <= Hi
22358 and then
22359 Lo <= Hi_Val and then Hi_Val <= Hi;
22360 end Can_Derive_From;
22361
22362 -----------------
22363 -- Check_Bound --
22364 -----------------
22365
22366 procedure Check_Bound (Expr : Node_Id) is
22367 begin
22368 -- If a range constraint is used as an integer type definition, each
22369 -- bound of the range must be defined by a static expression of some
22370 -- integer type, but the two bounds need not have the same integer
22371 -- type (Negative bounds are allowed.) (RM 3.5.4)
22372
22373 if not Is_Integer_Type (Etype (Expr)) then
22374 Error_Msg_N
22375 ("integer type definition bounds must be of integer type", Expr);
22376 Errs := True;
22377
22378 elsif not Is_OK_Static_Expression (Expr) then
22379 Flag_Non_Static_Expr
22380 ("non-static expression used for integer type bound!", Expr);
22381 Errs := True;
22382
22383 -- The bounds are folded into literals, and we set their type to be
22384 -- universal, to avoid typing difficulties: we cannot set the type
22385 -- of the literal to the new type, because this would be a forward
22386 -- reference for the back end, and if the original type is user-
22387 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22388
22389 else
22390 if Is_Entity_Name (Expr) then
22391 Fold_Uint (Expr, Expr_Value (Expr), True);
22392 end if;
22393
22394 Set_Etype (Expr, Universal_Integer);
22395 end if;
22396 end Check_Bound;
22397
22398 -- Start of processing for Signed_Integer_Type_Declaration
22399
22400 begin
22401 -- Create an anonymous base type
22402
22403 Implicit_Base :=
22404 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22405
22406 -- Analyze and check the bounds, they can be of any integer type
22407
22408 Lo := Low_Bound (Def);
22409 Hi := High_Bound (Def);
22410
22411 -- Arbitrarily use Integer as the type if either bound had an error
22412
22413 if Hi = Error or else Lo = Error then
22414 Base_Typ := Any_Integer;
22415 Set_Error_Posted (T, True);
22416
22417 -- Here both bounds are OK expressions
22418
22419 else
22420 Analyze_And_Resolve (Lo, Any_Integer);
22421 Analyze_And_Resolve (Hi, Any_Integer);
22422
22423 Check_Bound (Lo);
22424 Check_Bound (Hi);
22425
22426 if Errs then
22427 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22428 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22429 end if;
22430
22431 -- Find type to derive from
22432
22433 Lo_Val := Expr_Value (Lo);
22434 Hi_Val := Expr_Value (Hi);
22435
22436 if Can_Derive_From (Standard_Short_Short_Integer) then
22437 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22438
22439 elsif Can_Derive_From (Standard_Short_Integer) then
22440 Base_Typ := Base_Type (Standard_Short_Integer);
22441
22442 elsif Can_Derive_From (Standard_Integer) then
22443 Base_Typ := Base_Type (Standard_Integer);
22444
22445 elsif Can_Derive_From (Standard_Long_Integer) then
22446 Base_Typ := Base_Type (Standard_Long_Integer);
22447
22448 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22449 Check_Restriction (No_Long_Long_Integers, Def);
22450 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22451
22452 else
22453 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22454 Error_Msg_N ("integer type definition bounds out of range", Def);
22455 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22456 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22457 end if;
22458 end if;
22459
22460 -- Complete both implicit base and declared first subtype entities. The
22461 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22462 -- are not clobbered when the signed integer type acts as a full view of
22463 -- a private type.
22464
22465 Set_Etype (Implicit_Base, Base_Typ);
22466 Set_Size_Info (Implicit_Base, Base_Typ);
22467 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22468 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22469 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22470
22471 Set_Ekind (T, E_Signed_Integer_Subtype);
22472 Set_Etype (T, Implicit_Base);
22473 Set_Size_Info (T, Implicit_Base);
22474 Inherit_Rep_Item_Chain (T, Implicit_Base);
22475 Set_Scalar_Range (T, Def);
22476 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22477 Set_Is_Constrained (T);
22478 end Signed_Integer_Type_Declaration;
22479
22480 end Sem_Ch3;