[multiple changes]
[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-2017, 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_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_Smem; use Sem_Smem;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn; use Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
81
82 package body Sem_Ch3 is
83
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
87
88 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
89 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
90 -- abstract interface types implemented by a record type or a derived
91 -- record type.
92
93 procedure Build_Derived_Type
94 (N : Node_Id;
95 Parent_Type : Entity_Id;
96 Derived_Type : Entity_Id;
97 Is_Completion : Boolean;
98 Derive_Subps : Boolean := True);
99 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
100 -- the N_Full_Type_Declaration node containing the derived type definition.
101 -- Parent_Type is the entity for the parent type in the derived type
102 -- definition and Derived_Type the actual derived type. Is_Completion must
103 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
104 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
105 -- completion of a private type declaration. If Is_Completion is set to
106 -- True, N is the completion of a private type declaration and Derived_Type
107 -- is different from the defining identifier inside N (i.e. Derived_Type /=
108 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
109 -- subprograms should be derived. The only case where this parameter is
110 -- False is when Build_Derived_Type is recursively called to process an
111 -- implicit derived full type for a type derived from a private type (in
112 -- that case the subprograms must only be derived for the private view of
113 -- the type).
114 --
115 -- ??? These flags need a bit of re-examination and re-documentation:
116 -- ??? are they both necessary (both seem related to the recursion)?
117
118 procedure Build_Derived_Access_Type
119 (N : Node_Id;
120 Parent_Type : Entity_Id;
121 Derived_Type : Entity_Id);
122 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
123 -- create an implicit base if the parent type is constrained or if the
124 -- subtype indication has a constraint.
125
126 procedure Build_Derived_Array_Type
127 (N : Node_Id;
128 Parent_Type : Entity_Id;
129 Derived_Type : Entity_Id);
130 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
131 -- create an implicit base if the parent type is constrained or if the
132 -- subtype indication has a constraint.
133
134 procedure Build_Derived_Concurrent_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
139 -- protected type, inherit entries and protected subprograms, check
140 -- legality of discriminant constraints if any.
141
142 procedure Build_Derived_Enumeration_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
147 -- type, we must create a new list of literals. Types derived from
148 -- Character and [Wide_]Wide_Character are special-cased.
149
150 procedure Build_Derived_Numeric_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
155 -- an anonymous base type, and propagate constraint to subtype if needed.
156
157 procedure Build_Derived_Private_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id;
161 Is_Completion : Boolean;
162 Derive_Subps : Boolean := True);
163 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
164 -- because the parent may or may not have a completion, and the derivation
165 -- may itself be a completion.
166
167 procedure Build_Derived_Record_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id;
171 Derive_Subps : Boolean := True);
172 -- Subsidiary procedure used for tagged and untagged record types
173 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
174 -- All parameters are as in Build_Derived_Type except that N, in
175 -- addition to being an N_Full_Type_Declaration node, can also be an
176 -- N_Private_Extension_Declaration node. See the definition of this routine
177 -- for much more info. Derive_Subps indicates whether subprograms should be
178 -- derived from the parent type. The only case where Derive_Subps is False
179 -- is for an implicit derived full type for a type derived from a private
180 -- type (see Build_Derived_Type).
181
182 procedure Build_Discriminal (Discrim : Entity_Id);
183 -- Create the discriminal corresponding to discriminant Discrim, that is
184 -- the parameter corresponding to Discrim to be used in initialization
185 -- procedures for the type where Discrim is a discriminant. Discriminals
186 -- are not used during semantic analysis, and are not fully defined
187 -- entities until expansion. Thus they are not given a scope until
188 -- initialization procedures are built.
189
190 function Build_Discriminant_Constraints
191 (T : Entity_Id;
192 Def : Node_Id;
193 Derived_Def : Boolean := False) return Elist_Id;
194 -- Validate discriminant constraints and return the list of the constraints
195 -- in order of discriminant declarations, where T is the discriminated
196 -- unconstrained type. Def is the N_Subtype_Indication node where the
197 -- discriminants constraints for T are specified. Derived_Def is True
198 -- when building the discriminant constraints in a derived type definition
199 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
200 -- type and Def is the constraint "(xxx)" on T and this routine sets the
201 -- Corresponding_Discriminant field of the discriminants in the derived
202 -- type D to point to the corresponding discriminants in the parent type T.
203
204 procedure Build_Discriminated_Subtype
205 (T : Entity_Id;
206 Def_Id : Entity_Id;
207 Elist : Elist_Id;
208 Related_Nod : Node_Id;
209 For_Access : Boolean := False);
210 -- Subsidiary procedure to Constrain_Discriminated_Type and to
211 -- Process_Incomplete_Dependents. Given
212 --
213 -- T (a possibly discriminated base type)
214 -- Def_Id (a very partially built subtype for T),
215 --
216 -- the call completes Def_Id to be the appropriate E_*_Subtype.
217 --
218 -- The Elist is the list of discriminant constraints if any (it is set
219 -- to No_Elist if T is not a discriminated type, and to an empty list if
220 -- T has discriminants but there are no discriminant constraints). The
221 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
222 -- The For_Access says whether or not this subtype is really constraining
223 -- an access type. That is its sole purpose is the designated type of an
224 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
225 -- is built to avoid freezing T when the access subtype is frozen.
226
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
235
236 procedure Build_Underlying_Full_View
237 (N : Node_Id;
238 Typ : Entity_Id;
239 Par : Entity_Id);
240 -- If the completion of a private type is itself derived from a private
241 -- type, or if the full view of a private subtype is itself private, the
242 -- back-end has no way to compute the actual size of this type. We build
243 -- an internal subtype declaration of the proper parent type to convey
244 -- this information. This extra mechanism is needed because a full
245 -- view cannot itself have a full view (it would get clobbered during
246 -- view exchanges).
247
248 procedure Check_Access_Discriminant_Requires_Limited
249 (D : Node_Id;
250 Loc : Node_Id);
251 -- Check the restriction that the type to which an access discriminant
252 -- belongs must be a concurrent type or a descendant of a type with
253 -- the reserved word 'limited' in its declaration.
254
255 procedure Check_Anonymous_Access_Components
256 (Typ_Decl : Node_Id;
257 Typ : Entity_Id;
258 Prev : Entity_Id;
259 Comp_List : Node_Id);
260 -- Ada 2005 AI-382: an access component in a record definition can refer to
261 -- the enclosing record, in which case it denotes the type itself, and not
262 -- the current instance of the type. We create an anonymous access type for
263 -- the component, and flag it as an access to a component, so accessibility
264 -- checks are properly performed on it. The declaration of the access type
265 -- is placed ahead of that of the record to prevent order-of-elaboration
266 -- circularity issues in Gigi. We create an incomplete type for the record
267 -- declaration, which is the designated type of the anonymous access.
268
269 procedure Check_Delta_Expression (E : Node_Id);
270 -- Check that the expression represented by E is suitable for use as a
271 -- delta expression, i.e. it is of real type and is static.
272
273 procedure Check_Digits_Expression (E : Node_Id);
274 -- Check that the expression represented by E is suitable for use as a
275 -- digits expression, i.e. it is of integer type, positive and static.
276
277 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
278 -- Validate the initialization of an object declaration. T is the required
279 -- type, and Exp is the initialization expression.
280
281 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
282 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
283
284 procedure Check_Or_Process_Discriminants
285 (N : Node_Id;
286 T : Entity_Id;
287 Prev : Entity_Id := Empty);
288 -- If N is the full declaration of the completion T of an incomplete or
289 -- private type, check its discriminants (which are already known to be
290 -- conformant with those of the partial view, see Find_Type_Name),
291 -- otherwise process them. Prev is the entity of the partial declaration,
292 -- if any.
293
294 procedure Check_Real_Bound (Bound : Node_Id);
295 -- Check given bound for being of real type and static. If not, post an
296 -- appropriate message, and rewrite the bound with the real literal zero.
297
298 procedure Constant_Redeclaration
299 (Id : Entity_Id;
300 N : Node_Id;
301 T : out Entity_Id);
302 -- Various checks on legality of full declaration of deferred constant.
303 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
304 -- node. The caller has not yet set any attributes of this entity.
305
306 function Contain_Interface
307 (Iface : Entity_Id;
308 Ifaces : Elist_Id) return Boolean;
309 -- Ada 2005: Determine whether Iface is present in the list Ifaces
310
311 procedure Convert_Scalar_Bounds
312 (N : Node_Id;
313 Parent_Type : Entity_Id;
314 Derived_Type : Entity_Id;
315 Loc : Source_Ptr);
316 -- For derived scalar types, convert the bounds in the type definition to
317 -- the derived type, and complete their analysis. Given a constraint of the
318 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
319 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
320 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
321 -- subtype are conversions of those bounds to the derived_type, so that
322 -- their typing is consistent.
323
324 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
325 -- Copies attributes from array base type T2 to array base type T1. Copies
326 -- only attributes that apply to base types, but not subtypes.
327
328 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
329 -- Copies attributes from array subtype T2 to array subtype T1. Copies
330 -- attributes that apply to both subtypes and base types.
331
332 procedure Create_Constrained_Components
333 (Subt : Entity_Id;
334 Decl_Node : Node_Id;
335 Typ : Entity_Id;
336 Constraints : Elist_Id);
337 -- Build the list of entities for a constrained discriminated record
338 -- subtype. If a component depends on a discriminant, replace its subtype
339 -- using the discriminant values in the discriminant constraint. Subt
340 -- is the defining identifier for the subtype whose list of constrained
341 -- entities we will create. Decl_Node is the type declaration node where
342 -- we will attach all the itypes created. Typ is the base discriminated
343 -- type for the subtype Subt. Constraints is the list of discriminant
344 -- constraints for Typ.
345
346 function Constrain_Component_Type
347 (Comp : Entity_Id;
348 Constrained_Typ : Entity_Id;
349 Related_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id) return Entity_Id;
352 -- Given a discriminated base type Typ, a list of discriminant constraints,
353 -- Constraints, for Typ and a component Comp of Typ, create and return the
354 -- type corresponding to Etype (Comp) where all discriminant references
355 -- are replaced with the corresponding constraint. If Etype (Comp) contains
356 -- no discriminant references then it is returned as-is. Constrained_Typ
357 -- is the final constrained subtype to which the constrained component
358 -- belongs. Related_Node is the node where we attach all created itypes.
359
360 procedure Constrain_Access
361 (Def_Id : in out Entity_Id;
362 S : Node_Id;
363 Related_Nod : Node_Id);
364 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
365 -- an anonymous type created for a subtype indication. In that case it is
366 -- created in the procedure and attached to Related_Nod.
367
368 procedure Constrain_Array
369 (Def_Id : in out Entity_Id;
370 SI : Node_Id;
371 Related_Nod : Node_Id;
372 Related_Id : Entity_Id;
373 Suffix : Character);
374 -- Apply a list of index constraints to an unconstrained array type. The
375 -- first parameter is the entity for the resulting subtype. A value of
376 -- Empty for Def_Id indicates that an implicit type must be created, but
377 -- creation is delayed (and must be done by this procedure) because other
378 -- subsidiary implicit types must be created first (which is why Def_Id
379 -- is an in/out parameter). The second parameter is a subtype indication
380 -- node for the constrained array to be created (e.g. something of the
381 -- form string (1 .. 10)). Related_Nod gives the place where this type
382 -- has to be inserted in the tree. The Related_Id and Suffix parameters
383 -- are used to build the associated Implicit type name.
384
385 procedure Constrain_Concurrent
386 (Def_Id : in out Entity_Id;
387 SI : Node_Id;
388 Related_Nod : Node_Id;
389 Related_Id : Entity_Id;
390 Suffix : Character);
391 -- Apply list of discriminant constraints to an unconstrained concurrent
392 -- type.
393 --
394 -- SI is the N_Subtype_Indication node containing the constraint and
395 -- the unconstrained type to constrain.
396 --
397 -- Def_Id is the entity for the resulting constrained subtype. A value
398 -- of Empty for Def_Id indicates that an implicit type must be created,
399 -- but creation is delayed (and must be done by this procedure) because
400 -- other subsidiary implicit types must be created first (which is why
401 -- Def_Id is an in/out parameter).
402 --
403 -- Related_Nod gives the place where this type has to be inserted
404 -- in the tree.
405 --
406 -- The last two arguments are used to create its external name if needed.
407
408 function Constrain_Corresponding_Record
409 (Prot_Subt : Entity_Id;
410 Corr_Rec : Entity_Id;
411 Related_Nod : Node_Id) return Entity_Id;
412 -- When constraining a protected type or task type with discriminants,
413 -- constrain the corresponding record with the same discriminant values.
414
415 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
416 -- Constrain a decimal fixed point type with a digits constraint and/or a
417 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
418
419 procedure Constrain_Discriminated_Type
420 (Def_Id : Entity_Id;
421 S : Node_Id;
422 Related_Nod : Node_Id;
423 For_Access : Boolean := False);
424 -- Process discriminant constraints of composite type. Verify that values
425 -- have been provided for all discriminants, that the original type is
426 -- unconstrained, and that the types of the supplied expressions match
427 -- the discriminant types. The first three parameters are like in routine
428 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
429 -- of For_Access.
430
431 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
432 -- Constrain an enumeration type with a range constraint. This is identical
433 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
434
435 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain a floating point type with either a digits constraint
437 -- and/or a range constraint, building a E_Floating_Point_Subtype.
438
439 procedure Constrain_Index
440 (Index : Node_Id;
441 S : Node_Id;
442 Related_Nod : Node_Id;
443 Related_Id : Entity_Id;
444 Suffix : Character;
445 Suffix_Index : Nat);
446 -- Process an index constraint S in a constrained array declaration. The
447 -- constraint can be a subtype name, or a range with or without an explicit
448 -- subtype mark. The index is the corresponding index of the unconstrained
449 -- array. The Related_Id and Suffix parameters are used to build the
450 -- associated Implicit type name.
451
452 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
453 -- Build subtype of a signed or modular integer type
454
455 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
456 -- Constrain an ordinary fixed point type with a range constraint, and
457 -- build an E_Ordinary_Fixed_Point_Subtype entity.
458
459 procedure Copy_And_Swap (Priv, Full : Entity_Id);
460 -- Copy the Priv entity into the entity of its full declaration then swap
461 -- the two entities in such a manner that the former private type is now
462 -- seen as a full type.
463
464 procedure Decimal_Fixed_Point_Type_Declaration
465 (T : Entity_Id;
466 Def : Node_Id);
467 -- Create a new decimal fixed point type, and apply the constraint to
468 -- obtain a subtype of this new type.
469
470 procedure Complete_Private_Subtype
471 (Priv : Entity_Id;
472 Full : Entity_Id;
473 Full_Base : Entity_Id;
474 Related_Nod : Node_Id);
475 -- Complete the implicit full view of a private subtype by setting the
476 -- appropriate semantic fields. If the full view of the parent is a record
477 -- type, build constrained components of subtype.
478
479 procedure Derive_Progenitor_Subprograms
480 (Parent_Type : Entity_Id;
481 Tagged_Type : Entity_Id);
482 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
483 -- operations of progenitors of Tagged_Type, and replace the subsidiary
484 -- subtypes with Tagged_Type, to build the specs of the inherited interface
485 -- primitives. The derived primitives are aliased to those of the
486 -- interface. This routine takes care also of transferring to the full view
487 -- subprograms associated with the partial view of Tagged_Type that cover
488 -- interface primitives.
489
490 procedure Derived_Standard_Character
491 (N : Node_Id;
492 Parent_Type : Entity_Id;
493 Derived_Type : Entity_Id);
494 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
495 -- derivations from types Standard.Character and Standard.Wide_Character.
496
497 procedure Derived_Type_Declaration
498 (T : Entity_Id;
499 N : Node_Id;
500 Is_Completion : Boolean);
501 -- Process a derived type declaration. Build_Derived_Type is invoked
502 -- to process the actual derived type definition. Parameters N and
503 -- Is_Completion have the same meaning as in Build_Derived_Type.
504 -- T is the N_Defining_Identifier for the entity defined in the
505 -- N_Full_Type_Declaration node N, that is T is the derived type.
506
507 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
508 -- Insert each literal in symbol table, as an overloadable identifier. Each
509 -- enumeration type is mapped into a sequence of integers, and each literal
510 -- is defined as a constant with integer value. If any of the literals are
511 -- character literals, the type is a character type, which means that
512 -- strings are legal aggregates for arrays of components of the type.
513
514 function Expand_To_Stored_Constraint
515 (Typ : Entity_Id;
516 Constraint : Elist_Id) return Elist_Id;
517 -- Given a constraint (i.e. a list of expressions) on the discriminants of
518 -- Typ, expand it into a constraint on the stored discriminants and return
519 -- the new list of expressions constraining the stored discriminants.
520
521 function Find_Type_Of_Object
522 (Obj_Def : Node_Id;
523 Related_Nod : Node_Id) return Entity_Id;
524 -- Get type entity for object referenced by Obj_Def, attaching the implicit
525 -- types generated to Related_Nod.
526
527 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
528 -- Create a new float and apply the constraint to obtain subtype of it
529
530 function Has_Range_Constraint (N : Node_Id) return Boolean;
531 -- Given an N_Subtype_Indication node N, return True if a range constraint
532 -- is present, either directly, or as part of a digits or delta constraint.
533 -- In addition, a digits constraint in the decimal case returns True, since
534 -- it establishes a default range if no explicit range is present.
535
536 function Inherit_Components
537 (N : Node_Id;
538 Parent_Base : Entity_Id;
539 Derived_Base : Entity_Id;
540 Is_Tagged : Boolean;
541 Inherit_Discr : Boolean;
542 Discs : Elist_Id) return Elist_Id;
543 -- Called from Build_Derived_Record_Type to inherit the components of
544 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
545 -- For more information on derived types and component inheritance please
546 -- consult the comment above the body of Build_Derived_Record_Type.
547 --
548 -- N is the original derived type declaration
549 --
550 -- Is_Tagged is set if we are dealing with tagged types
551 --
552 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
553 -- Parent_Base, otherwise no discriminants are inherited.
554 --
555 -- Discs gives the list of constraints that apply to Parent_Base in the
556 -- derived type declaration. If Discs is set to No_Elist, then we have
557 -- the following situation:
558 --
559 -- type Parent (D1..Dn : ..) is [tagged] record ...;
560 -- type Derived is new Parent [with ...];
561 --
562 -- which gets treated as
563 --
564 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
565 --
566 -- For untagged types the returned value is an association list. The list
567 -- starts from the association (Parent_Base => Derived_Base), and then it
568 -- contains a sequence of the associations of the form
569 --
570 -- (Old_Component => New_Component),
571 --
572 -- where Old_Component is the Entity_Id of a component in Parent_Base and
573 -- New_Component is the Entity_Id of the corresponding component in
574 -- Derived_Base. For untagged records, this association list is needed when
575 -- copying the record declaration for the derived base. In the tagged case
576 -- the value returned is irrelevant.
577
578 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
579 -- Propagate static and dynamic predicate flags from a parent to the
580 -- subtype in a subtype declaration with and without constraints.
581
582 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
583 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
584 -- Determine whether subprogram Subp is a procedure subject to pragma
585 -- Extensions_Visible with value False and has at least one controlling
586 -- parameter of mode OUT.
587
588 function Is_Valid_Constraint_Kind
589 (T_Kind : Type_Kind;
590 Constraint_Kind : Node_Kind) return Boolean;
591 -- Returns True if it is legal to apply the given kind of constraint to the
592 -- given kind of type (index constraint to an array type, for example).
593
594 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
595 -- Create new modular type. Verify that modulus is in bounds
596
597 procedure New_Concatenation_Op (Typ : Entity_Id);
598 -- Create an abbreviated declaration for an operator in order to
599 -- materialize concatenation on array types.
600
601 procedure Ordinary_Fixed_Point_Type_Declaration
602 (T : Entity_Id;
603 Def : Node_Id);
604 -- Create a new ordinary fixed point type, and apply the constraint to
605 -- obtain subtype of it.
606
607 procedure Prepare_Private_Subtype_Completion
608 (Id : Entity_Id;
609 Related_Nod : Node_Id);
610 -- Id is a subtype of some private type. Creates the full declaration
611 -- associated with Id whenever possible, i.e. when the full declaration
612 -- of the base type is already known. Records each subtype into
613 -- Private_Dependents of the base type.
614
615 procedure Process_Incomplete_Dependents
616 (N : Node_Id;
617 Full_T : Entity_Id;
618 Inc_T : Entity_Id);
619 -- Process all entities that depend on an incomplete type. There include
620 -- subtypes, subprogram types that mention the incomplete type in their
621 -- profiles, and subprogram with access parameters that designate the
622 -- incomplete type.
623
624 -- Inc_T is the defining identifier of an incomplete type declaration, its
625 -- Ekind is E_Incomplete_Type.
626 --
627 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
628 --
629 -- Full_T is N's defining identifier.
630 --
631 -- Subtypes of incomplete types with discriminants are completed when the
632 -- parent type is. This is simpler than private subtypes, because they can
633 -- only appear in the same scope, and there is no need to exchange views.
634 -- Similarly, access_to_subprogram types may have a parameter or a return
635 -- type that is an incomplete type, and that must be replaced with the
636 -- full type.
637 --
638 -- If the full type is tagged, subprogram with access parameters that
639 -- designated the incomplete may be primitive operations of the full type,
640 -- and have to be processed accordingly.
641
642 procedure Process_Real_Range_Specification (Def : Node_Id);
643 -- Given the type definition for a real type, this procedure processes and
644 -- checks the real range specification of this type definition if one is
645 -- present. If errors are found, error messages are posted, and the
646 -- Real_Range_Specification of Def is reset to Empty.
647
648 procedure Record_Type_Declaration
649 (T : Entity_Id;
650 N : Node_Id;
651 Prev : Entity_Id);
652 -- Process a record type declaration (for both untagged and tagged
653 -- records). Parameters T and N are exactly like in procedure
654 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
655 -- for this routine. If this is the completion of an incomplete type
656 -- declaration, Prev is the entity of the incomplete declaration, used for
657 -- cross-referencing. Otherwise Prev = T.
658
659 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
660 -- This routine is used to process the actual record type definition (both
661 -- for untagged and tagged records). Def is a record type definition node.
662 -- This procedure analyzes the components in this record type definition.
663 -- Prev_T is the entity for the enclosing record type. It is provided so
664 -- that its Has_Task flag can be set if any of the component have Has_Task
665 -- set. If the declaration is the completion of an incomplete type
666 -- declaration, Prev_T is the original incomplete type, whose full view is
667 -- the record type.
668
669 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
670 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
671 -- build a copy of the declaration tree of the parent, and we create
672 -- independently the list of components for the derived type. Semantic
673 -- information uses the component entities, but record representation
674 -- clauses are validated on the declaration tree. This procedure replaces
675 -- discriminants and components in the declaration with those that have
676 -- been created by Inherit_Components.
677
678 procedure Set_Fixed_Range
679 (E : Entity_Id;
680 Loc : Source_Ptr;
681 Lo : Ureal;
682 Hi : Ureal);
683 -- Build a range node with the given bounds and set it as the Scalar_Range
684 -- of the given fixed-point type entity. Loc is the source location used
685 -- for the constructed range. See body for further details.
686
687 procedure Set_Scalar_Range_For_Subtype
688 (Def_Id : Entity_Id;
689 R : Node_Id;
690 Subt : Entity_Id);
691 -- This routine is used to set the scalar range field for a subtype given
692 -- Def_Id, the entity for the subtype, and R, the range expression for the
693 -- scalar range. Subt provides the parent subtype to be used to analyze,
694 -- resolve, and check the given range.
695
696 procedure Set_Default_SSO (T : Entity_Id);
697 -- T is the entity for an array or record being declared. This procedure
698 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
699 -- to the setting of Opt.Default_SSO.
700
701 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
702 -- Create a new signed integer entity, and apply the constraint to obtain
703 -- the required first named subtype of this type.
704
705 procedure Set_Stored_Constraint_From_Discriminant_Constraint
706 (E : Entity_Id);
707 -- E is some record type. This routine computes E's Stored_Constraint
708 -- from its Discriminant_Constraint.
709
710 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
711 -- Check that an entity in a list of progenitors is an interface,
712 -- emit error otherwise.
713
714 -----------------------
715 -- Access_Definition --
716 -----------------------
717
718 function Access_Definition
719 (Related_Nod : Node_Id;
720 N : Node_Id) return Entity_Id
721 is
722 Anon_Type : Entity_Id;
723 Anon_Scope : Entity_Id;
724 Desig_Type : Entity_Id;
725 Enclosing_Prot_Type : Entity_Id := Empty;
726
727 begin
728 Check_SPARK_05_Restriction ("access type is not allowed", N);
729
730 if Is_Entry (Current_Scope)
731 and then Is_Task_Type (Etype (Scope (Current_Scope)))
732 then
733 Error_Msg_N ("task entries cannot have access parameters", N);
734 return Empty;
735 end if;
736
737 -- Ada 2005: For an object declaration the corresponding anonymous
738 -- type is declared in the current scope.
739
740 -- If the access definition is the return type of another access to
741 -- function, scope is the current one, because it is the one of the
742 -- current type declaration, except for the pathological case below.
743
744 if Nkind_In (Related_Nod, N_Object_Declaration,
745 N_Access_Function_Definition)
746 then
747 Anon_Scope := Current_Scope;
748
749 -- A pathological case: function returning access functions that
750 -- return access functions, etc. Each anonymous access type created
751 -- is in the enclosing scope of the outermost function.
752
753 declare
754 Par : Node_Id;
755
756 begin
757 Par := Related_Nod;
758 while Nkind_In (Par, N_Access_Function_Definition,
759 N_Access_Definition)
760 loop
761 Par := Parent (Par);
762 end loop;
763
764 if Nkind (Par) = N_Function_Specification then
765 Anon_Scope := Scope (Defining_Entity (Par));
766 end if;
767 end;
768
769 -- For the anonymous function result case, retrieve the scope of the
770 -- function specification's associated entity rather than using the
771 -- current scope. The current scope will be the function itself if the
772 -- formal part is currently being analyzed, but will be the parent scope
773 -- in the case of a parameterless function, and we always want to use
774 -- the function's parent scope. Finally, if the function is a child
775 -- unit, we must traverse the tree to retrieve the proper entity.
776
777 elsif Nkind (Related_Nod) = N_Function_Specification
778 and then Nkind (Parent (N)) /= N_Parameter_Specification
779 then
780 -- If the current scope is a protected type, the anonymous access
781 -- is associated with one of the protected operations, and must
782 -- be available in the scope that encloses the protected declaration.
783 -- Otherwise the type is in the scope enclosing the subprogram.
784
785 -- If the function has formals, The return type of a subprogram
786 -- declaration is analyzed in the scope of the subprogram (see
787 -- Process_Formals) and thus the protected type, if present, is
788 -- the scope of the current function scope.
789
790 if Ekind (Current_Scope) = E_Protected_Type then
791 Enclosing_Prot_Type := Current_Scope;
792
793 elsif Ekind (Current_Scope) = E_Function
794 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
795 then
796 Enclosing_Prot_Type := Scope (Current_Scope);
797 end if;
798
799 if Present (Enclosing_Prot_Type) then
800 Anon_Scope := Scope (Enclosing_Prot_Type);
801
802 else
803 Anon_Scope := Scope (Defining_Entity (Related_Nod));
804 end if;
805
806 -- For an access type definition, if the current scope is a child
807 -- unit it is the scope of the type.
808
809 elsif Is_Compilation_Unit (Current_Scope) then
810 Anon_Scope := Current_Scope;
811
812 -- For access formals, access components, and access discriminants, the
813 -- scope is that of the enclosing declaration,
814
815 else
816 Anon_Scope := Scope (Current_Scope);
817 end if;
818
819 Anon_Type :=
820 Create_Itype
821 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
822
823 if All_Present (N)
824 and then Ada_Version >= Ada_2005
825 then
826 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
827 end if;
828
829 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
830 -- the corresponding semantic routine
831
832 if Present (Access_To_Subprogram_Definition (N)) then
833
834 -- Compiler runtime units are compiled in Ada 2005 mode when building
835 -- the runtime library but must also be compilable in Ada 95 mode
836 -- (when bootstrapping the compiler).
837
838 Check_Compiler_Unit ("anonymous access to subprogram", N);
839
840 Access_Subprogram_Declaration
841 (T_Name => Anon_Type,
842 T_Def => Access_To_Subprogram_Definition (N));
843
844 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
845 Set_Ekind
846 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
847 else
848 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
849 end if;
850
851 Set_Can_Use_Internal_Rep
852 (Anon_Type, not Always_Compatible_Rep_On_Target);
853
854 -- If the anonymous access is associated with a protected operation,
855 -- create a reference to it after the enclosing protected definition
856 -- because the itype will be used in the subsequent bodies.
857
858 -- If the anonymous access itself is protected, a full type
859 -- declaratiton will be created for it, so that the equivalent
860 -- record type can be constructed. For further details, see
861 -- Replace_Anonymous_Access_To_Protected-Subprogram.
862
863 if Ekind (Current_Scope) = E_Protected_Type
864 and then not Protected_Present (Access_To_Subprogram_Definition (N))
865 then
866 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
867 end if;
868
869 return Anon_Type;
870 end if;
871
872 Find_Type (Subtype_Mark (N));
873 Desig_Type := Entity (Subtype_Mark (N));
874
875 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
876 Set_Etype (Anon_Type, Anon_Type);
877
878 -- Make sure the anonymous access type has size and alignment fields
879 -- set, as required by gigi. This is necessary in the case of the
880 -- Task_Body_Procedure.
881
882 if not Has_Private_Component (Desig_Type) then
883 Layout_Type (Anon_Type);
884 end if;
885
886 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
887 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
888 -- the null value is allowed. In Ada 95 the null value is never allowed.
889
890 if Ada_Version >= Ada_2005 then
891 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
892 else
893 Set_Can_Never_Be_Null (Anon_Type, True);
894 end if;
895
896 -- The anonymous access type is as public as the discriminated type or
897 -- subprogram that defines it. It is imported (for back-end purposes)
898 -- if the designated type is.
899
900 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
901
902 -- Ada 2005 (AI-231): Propagate the access-constant attribute
903
904 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
905
906 -- The context is either a subprogram declaration, object declaration,
907 -- or an access discriminant, in a private or a full type declaration.
908 -- In the case of a subprogram, if the designated type is incomplete,
909 -- the operation will be a primitive operation of the full type, to be
910 -- updated subsequently. If the type is imported through a limited_with
911 -- clause, the subprogram is not a primitive operation of the type
912 -- (which is declared elsewhere in some other scope).
913
914 if Ekind (Desig_Type) = E_Incomplete_Type
915 and then not From_Limited_With (Desig_Type)
916 and then Is_Overloadable (Current_Scope)
917 then
918 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
919 Set_Has_Delayed_Freeze (Current_Scope);
920 end if;
921
922 -- Ada 2005: If the designated type is an interface that may contain
923 -- tasks, create a Master entity for the declaration. This must be done
924 -- before expansion of the full declaration, because the declaration may
925 -- include an expression that is an allocator, whose expansion needs the
926 -- proper Master for the created tasks.
927
928 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
929 then
930 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
931 then
932 Build_Class_Wide_Master (Anon_Type);
933
934 -- Similarly, if the type is an anonymous access that designates
935 -- tasks, create a master entity for it in the current context.
936
937 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
938 then
939 Build_Master_Entity (Defining_Identifier (Related_Nod));
940 Build_Master_Renaming (Anon_Type);
941 end if;
942 end if;
943
944 -- For a private component of a protected type, it is imperative that
945 -- the back-end elaborate the type immediately after the protected
946 -- declaration, because this type will be used in the declarations
947 -- created for the component within each protected body, so we must
948 -- create an itype reference for it now.
949
950 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
951 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
952
953 -- Similarly, if the access definition is the return result of a
954 -- function, create an itype reference for it because it will be used
955 -- within the function body. For a regular function that is not a
956 -- compilation unit, insert reference after the declaration. For a
957 -- protected operation, insert it after the enclosing protected type
958 -- declaration. In either case, do not create a reference for a type
959 -- obtained through a limited_with clause, because this would introduce
960 -- semantic dependencies.
961
962 -- Similarly, do not create a reference if the designated type is a
963 -- generic formal, because no use of it will reach the backend.
964
965 elsif Nkind (Related_Nod) = N_Function_Specification
966 and then not From_Limited_With (Desig_Type)
967 and then not Is_Generic_Type (Desig_Type)
968 then
969 if Present (Enclosing_Prot_Type) then
970 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
971
972 elsif Is_List_Member (Parent (Related_Nod))
973 and then Nkind (Parent (N)) /= N_Parameter_Specification
974 then
975 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
976 end if;
977
978 -- Finally, create an itype reference for an object declaration of an
979 -- anonymous access type. This is strictly necessary only for deferred
980 -- constants, but in any case will avoid out-of-scope problems in the
981 -- back-end.
982
983 elsif Nkind (Related_Nod) = N_Object_Declaration then
984 Build_Itype_Reference (Anon_Type, Related_Nod);
985 end if;
986
987 return Anon_Type;
988 end Access_Definition;
989
990 -----------------------------------
991 -- Access_Subprogram_Declaration --
992 -----------------------------------
993
994 procedure Access_Subprogram_Declaration
995 (T_Name : Entity_Id;
996 T_Def : Node_Id)
997 is
998 procedure Check_For_Premature_Usage (Def : Node_Id);
999 -- Check that type T_Name is not used, directly or recursively, as a
1000 -- parameter or a return type in Def. Def is either a subtype, an
1001 -- access_definition, or an access_to_subprogram_definition.
1002
1003 -------------------------------
1004 -- Check_For_Premature_Usage --
1005 -------------------------------
1006
1007 procedure Check_For_Premature_Usage (Def : Node_Id) is
1008 Param : Node_Id;
1009
1010 begin
1011 -- Check for a subtype mark
1012
1013 if Nkind (Def) in N_Has_Etype then
1014 if Etype (Def) = T_Name then
1015 Error_Msg_N
1016 ("type& cannot be used before end of its declaration", Def);
1017 end if;
1018
1019 -- If this is not a subtype, then this is an access_definition
1020
1021 elsif Nkind (Def) = N_Access_Definition then
1022 if Present (Access_To_Subprogram_Definition (Def)) then
1023 Check_For_Premature_Usage
1024 (Access_To_Subprogram_Definition (Def));
1025 else
1026 Check_For_Premature_Usage (Subtype_Mark (Def));
1027 end if;
1028
1029 -- The only cases left are N_Access_Function_Definition and
1030 -- N_Access_Procedure_Definition.
1031
1032 else
1033 if Present (Parameter_Specifications (Def)) then
1034 Param := First (Parameter_Specifications (Def));
1035 while Present (Param) loop
1036 Check_For_Premature_Usage (Parameter_Type (Param));
1037 Param := Next (Param);
1038 end loop;
1039 end if;
1040
1041 if Nkind (Def) = N_Access_Function_Definition then
1042 Check_For_Premature_Usage (Result_Definition (Def));
1043 end if;
1044 end if;
1045 end Check_For_Premature_Usage;
1046
1047 -- Local variables
1048
1049 Formals : constant List_Id := Parameter_Specifications (T_Def);
1050 Formal : Entity_Id;
1051 D_Ityp : Node_Id;
1052 Desig_Type : constant Entity_Id :=
1053 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1054
1055 -- Start of processing for Access_Subprogram_Declaration
1056
1057 begin
1058 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1059
1060 -- Associate the Itype node with the inner full-type declaration or
1061 -- subprogram spec or entry body. This is required to handle nested
1062 -- anonymous declarations. For example:
1063
1064 -- procedure P
1065 -- (X : access procedure
1066 -- (Y : access procedure
1067 -- (Z : access T)))
1068
1069 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1070 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1071 N_Private_Type_Declaration,
1072 N_Private_Extension_Declaration,
1073 N_Procedure_Specification,
1074 N_Function_Specification,
1075 N_Entry_Body)
1076
1077 or else
1078 Nkind_In (D_Ityp, N_Object_Declaration,
1079 N_Object_Renaming_Declaration,
1080 N_Formal_Object_Declaration,
1081 N_Formal_Type_Declaration,
1082 N_Task_Type_Declaration,
1083 N_Protected_Type_Declaration))
1084 loop
1085 D_Ityp := Parent (D_Ityp);
1086 pragma Assert (D_Ityp /= Empty);
1087 end loop;
1088
1089 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1090
1091 if Nkind_In (D_Ityp, N_Procedure_Specification,
1092 N_Function_Specification)
1093 then
1094 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1095
1096 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1097 N_Object_Declaration,
1098 N_Object_Renaming_Declaration,
1099 N_Formal_Type_Declaration)
1100 then
1101 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1102 end if;
1103
1104 if Nkind (T_Def) = N_Access_Function_Definition then
1105 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1106 declare
1107 Acc : constant Node_Id := Result_Definition (T_Def);
1108
1109 begin
1110 if Present (Access_To_Subprogram_Definition (Acc))
1111 and then
1112 Protected_Present (Access_To_Subprogram_Definition (Acc))
1113 then
1114 Set_Etype
1115 (Desig_Type,
1116 Replace_Anonymous_Access_To_Protected_Subprogram
1117 (T_Def));
1118
1119 else
1120 Set_Etype
1121 (Desig_Type,
1122 Access_Definition (T_Def, Result_Definition (T_Def)));
1123 end if;
1124 end;
1125
1126 else
1127 Analyze (Result_Definition (T_Def));
1128
1129 declare
1130 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1131
1132 begin
1133 -- If a null exclusion is imposed on the result type, then
1134 -- create a null-excluding itype (an access subtype) and use
1135 -- it as the function's Etype.
1136
1137 if Is_Access_Type (Typ)
1138 and then Null_Exclusion_In_Return_Present (T_Def)
1139 then
1140 Set_Etype (Desig_Type,
1141 Create_Null_Excluding_Itype
1142 (T => Typ,
1143 Related_Nod => T_Def,
1144 Scope_Id => Current_Scope));
1145
1146 else
1147 if From_Limited_With (Typ) then
1148
1149 -- AI05-151: Incomplete types are allowed in all basic
1150 -- declarations, including access to subprograms.
1151
1152 if Ada_Version >= Ada_2012 then
1153 null;
1154
1155 else
1156 Error_Msg_NE
1157 ("illegal use of incomplete type&",
1158 Result_Definition (T_Def), Typ);
1159 end if;
1160
1161 elsif Ekind (Current_Scope) = E_Package
1162 and then In_Private_Part (Current_Scope)
1163 then
1164 if Ekind (Typ) = E_Incomplete_Type then
1165 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1166
1167 elsif Is_Class_Wide_Type (Typ)
1168 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1169 then
1170 Append_Elmt
1171 (Desig_Type, Private_Dependents (Etype (Typ)));
1172 end if;
1173 end if;
1174
1175 Set_Etype (Desig_Type, Typ);
1176 end if;
1177 end;
1178 end if;
1179
1180 if not (Is_Type (Etype (Desig_Type))) then
1181 Error_Msg_N
1182 ("expect type in function specification",
1183 Result_Definition (T_Def));
1184 end if;
1185
1186 else
1187 Set_Etype (Desig_Type, Standard_Void_Type);
1188 end if;
1189
1190 if Present (Formals) then
1191 Push_Scope (Desig_Type);
1192
1193 -- Some special tests here. These special tests can be removed
1194 -- if and when Itypes always have proper parent pointers to their
1195 -- declarations???
1196
1197 -- Special test 1) Link defining_identifier of formals. Required by
1198 -- First_Formal to provide its functionality.
1199
1200 declare
1201 F : Node_Id;
1202
1203 begin
1204 F := First (Formals);
1205
1206 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1207 -- when it is part of an unconstrained type and subtype expansion
1208 -- is disabled. To avoid back-end problems with shared profiles,
1209 -- use previous subprogram type as the designated type, and then
1210 -- remove scope added above.
1211
1212 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1213 then
1214 Set_Etype (T_Name, T_Name);
1215 Init_Size_Align (T_Name);
1216 Set_Directly_Designated_Type (T_Name,
1217 Scope (Defining_Identifier (F)));
1218 End_Scope;
1219 return;
1220 end if;
1221
1222 while Present (F) loop
1223 if No (Parent (Defining_Identifier (F))) then
1224 Set_Parent (Defining_Identifier (F), F);
1225 end if;
1226
1227 Next (F);
1228 end loop;
1229 end;
1230
1231 Process_Formals (Formals, Parent (T_Def));
1232
1233 -- Special test 2) End_Scope requires that the parent pointer be set
1234 -- to something reasonable, but Itypes don't have parent pointers. So
1235 -- we set it and then unset it ???
1236
1237 Set_Parent (Desig_Type, T_Name);
1238 End_Scope;
1239 Set_Parent (Desig_Type, Empty);
1240 end if;
1241
1242 -- Check for premature usage of the type being defined
1243
1244 Check_For_Premature_Usage (T_Def);
1245
1246 -- The return type and/or any parameter type may be incomplete. Mark the
1247 -- subprogram_type as depending on the incomplete type, so that it can
1248 -- be updated when the full type declaration is seen. This only applies
1249 -- to incomplete types declared in some enclosing scope, not to limited
1250 -- views from other packages.
1251
1252 -- Prior to Ada 2012, access to functions can only have in_parameters.
1253
1254 if Present (Formals) then
1255 Formal := First_Formal (Desig_Type);
1256 while Present (Formal) loop
1257 if Ekind (Formal) /= E_In_Parameter
1258 and then Nkind (T_Def) = N_Access_Function_Definition
1259 and then Ada_Version < Ada_2012
1260 then
1261 Error_Msg_N ("functions can only have IN parameters", Formal);
1262 end if;
1263
1264 if Ekind (Etype (Formal)) = E_Incomplete_Type
1265 and then In_Open_Scopes (Scope (Etype (Formal)))
1266 then
1267 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1268 Set_Has_Delayed_Freeze (Desig_Type);
1269 end if;
1270
1271 Next_Formal (Formal);
1272 end loop;
1273 end if;
1274
1275 -- Check whether an indirect call without actuals may be possible. This
1276 -- is used when resolving calls whose result is then indexed.
1277
1278 May_Need_Actuals (Desig_Type);
1279
1280 -- If the return type is incomplete, this is legal as long as the type
1281 -- is declared in the current scope and will be completed in it (rather
1282 -- than being part of limited view).
1283
1284 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1285 and then not Has_Delayed_Freeze (Desig_Type)
1286 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1287 then
1288 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1289 Set_Has_Delayed_Freeze (Desig_Type);
1290 end if;
1291
1292 Check_Delayed_Subprogram (Desig_Type);
1293
1294 if Protected_Present (T_Def) then
1295 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1296 Set_Convention (Desig_Type, Convention_Protected);
1297 else
1298 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1299 end if;
1300
1301 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1302
1303 Set_Etype (T_Name, T_Name);
1304 Init_Size_Align (T_Name);
1305 Set_Directly_Designated_Type (T_Name, Desig_Type);
1306
1307 Generate_Reference_To_Formals (T_Name);
1308
1309 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1310
1311 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1312
1313 Check_Restriction (No_Access_Subprograms, T_Def);
1314 end Access_Subprogram_Declaration;
1315
1316 ----------------------------
1317 -- Access_Type_Declaration --
1318 ----------------------------
1319
1320 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1321 P : constant Node_Id := Parent (Def);
1322 S : constant Node_Id := Subtype_Indication (Def);
1323
1324 Full_Desig : Entity_Id;
1325
1326 begin
1327 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1328
1329 -- Check for permissible use of incomplete type
1330
1331 if Nkind (S) /= N_Subtype_Indication then
1332 Analyze (S);
1333
1334 if Present (Entity (S))
1335 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1336 then
1337 Set_Directly_Designated_Type (T, Entity (S));
1338
1339 -- If the designated type is a limited view, we cannot tell if
1340 -- the full view contains tasks, and there is no way to handle
1341 -- that full view in a client. We create a master entity for the
1342 -- scope, which will be used when a client determines that one
1343 -- is needed.
1344
1345 if From_Limited_With (Entity (S))
1346 and then not Is_Class_Wide_Type (Entity (S))
1347 then
1348 Set_Ekind (T, E_Access_Type);
1349 Build_Master_Entity (T);
1350 Build_Master_Renaming (T);
1351 end if;
1352
1353 else
1354 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1355 end if;
1356
1357 -- If the access definition is of the form: ACCESS NOT NULL ..
1358 -- the subtype indication must be of an access type. Create
1359 -- a null-excluding subtype of it.
1360
1361 if Null_Excluding_Subtype (Def) then
1362 if not Is_Access_Type (Entity (S)) then
1363 Error_Msg_N ("null exclusion must apply to access type", Def);
1364
1365 else
1366 declare
1367 Loc : constant Source_Ptr := Sloc (S);
1368 Decl : Node_Id;
1369 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1370
1371 begin
1372 Decl :=
1373 Make_Subtype_Declaration (Loc,
1374 Defining_Identifier => Nam,
1375 Subtype_Indication =>
1376 New_Occurrence_Of (Entity (S), Loc));
1377 Set_Null_Exclusion_Present (Decl);
1378 Insert_Before (Parent (Def), Decl);
1379 Analyze (Decl);
1380 Set_Entity (S, Nam);
1381 end;
1382 end if;
1383 end if;
1384
1385 else
1386 Set_Directly_Designated_Type (T,
1387 Process_Subtype (S, P, T, 'P'));
1388 end if;
1389
1390 if All_Present (Def) or Constant_Present (Def) then
1391 Set_Ekind (T, E_General_Access_Type);
1392 else
1393 Set_Ekind (T, E_Access_Type);
1394 end if;
1395
1396 Full_Desig := Designated_Type (T);
1397
1398 if Base_Type (Full_Desig) = T then
1399 Error_Msg_N ("access type cannot designate itself", S);
1400
1401 -- In Ada 2005, the type may have a limited view through some unit in
1402 -- its own context, allowing the following circularity that cannot be
1403 -- detected earlier.
1404
1405 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1406 then
1407 Error_Msg_N
1408 ("access type cannot designate its own class-wide type", S);
1409
1410 -- Clean up indication of tagged status to prevent cascaded errors
1411
1412 Set_Is_Tagged_Type (T, False);
1413 end if;
1414
1415 Set_Etype (T, T);
1416
1417 -- If the type has appeared already in a with_type clause, it is frozen
1418 -- and the pointer size is already set. Else, initialize.
1419
1420 if not From_Limited_With (T) then
1421 Init_Size_Align (T);
1422 end if;
1423
1424 -- Note that Has_Task is always false, since the access type itself
1425 -- is not a task type. See Einfo for more description on this point.
1426 -- Exactly the same consideration applies to Has_Controlled_Component
1427 -- and to Has_Protected.
1428
1429 Set_Has_Task (T, False);
1430 Set_Has_Protected (T, False);
1431 Set_Has_Timing_Event (T, False);
1432 Set_Has_Controlled_Component (T, False);
1433
1434 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1435 -- problems where an incomplete view of this entity has been previously
1436 -- established by a limited with and an overlaid version of this field
1437 -- (Stored_Constraint) was initialized for the incomplete view.
1438
1439 -- This reset is performed in most cases except where the access type
1440 -- has been created for the purposes of allocating or deallocating a
1441 -- build-in-place object. Such access types have explicitly set pools
1442 -- and finalization masters.
1443
1444 if No (Associated_Storage_Pool (T)) then
1445 Set_Finalization_Master (T, Empty);
1446 end if;
1447
1448 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1449 -- attributes
1450
1451 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1452 Set_Is_Access_Constant (T, Constant_Present (Def));
1453 end Access_Type_Declaration;
1454
1455 ----------------------------------
1456 -- Add_Interface_Tag_Components --
1457 ----------------------------------
1458
1459 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1460 Loc : constant Source_Ptr := Sloc (N);
1461 L : List_Id;
1462 Last_Tag : Node_Id;
1463
1464 procedure Add_Tag (Iface : Entity_Id);
1465 -- Add tag for one of the progenitor interfaces
1466
1467 -------------
1468 -- Add_Tag --
1469 -------------
1470
1471 procedure Add_Tag (Iface : Entity_Id) is
1472 Decl : Node_Id;
1473 Def : Node_Id;
1474 Tag : Entity_Id;
1475 Offset : Entity_Id;
1476
1477 begin
1478 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1479
1480 -- This is a reasonable place to propagate predicates
1481
1482 if Has_Predicates (Iface) then
1483 Set_Has_Predicates (Typ);
1484 end if;
1485
1486 Def :=
1487 Make_Component_Definition (Loc,
1488 Aliased_Present => True,
1489 Subtype_Indication =>
1490 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1491
1492 Tag := Make_Temporary (Loc, 'V');
1493
1494 Decl :=
1495 Make_Component_Declaration (Loc,
1496 Defining_Identifier => Tag,
1497 Component_Definition => Def);
1498
1499 Analyze_Component_Declaration (Decl);
1500
1501 Set_Analyzed (Decl);
1502 Set_Ekind (Tag, E_Component);
1503 Set_Is_Tag (Tag);
1504 Set_Is_Aliased (Tag);
1505 Set_Related_Type (Tag, Iface);
1506 Init_Component_Location (Tag);
1507
1508 pragma Assert (Is_Frozen (Iface));
1509
1510 Set_DT_Entry_Count (Tag,
1511 DT_Entry_Count (First_Entity (Iface)));
1512
1513 if No (Last_Tag) then
1514 Prepend (Decl, L);
1515 else
1516 Insert_After (Last_Tag, Decl);
1517 end if;
1518
1519 Last_Tag := Decl;
1520
1521 -- If the ancestor has discriminants we need to give special support
1522 -- to store the offset_to_top value of the secondary dispatch tables.
1523 -- For this purpose we add a supplementary component just after the
1524 -- field that contains the tag associated with each secondary DT.
1525
1526 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1527 Def :=
1528 Make_Component_Definition (Loc,
1529 Subtype_Indication =>
1530 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1531
1532 Offset := Make_Temporary (Loc, 'V');
1533
1534 Decl :=
1535 Make_Component_Declaration (Loc,
1536 Defining_Identifier => Offset,
1537 Component_Definition => Def);
1538
1539 Analyze_Component_Declaration (Decl);
1540
1541 Set_Analyzed (Decl);
1542 Set_Ekind (Offset, E_Component);
1543 Set_Is_Aliased (Offset);
1544 Set_Related_Type (Offset, Iface);
1545 Init_Component_Location (Offset);
1546 Insert_After (Last_Tag, Decl);
1547 Last_Tag := Decl;
1548 end if;
1549 end Add_Tag;
1550
1551 -- Local variables
1552
1553 Elmt : Elmt_Id;
1554 Ext : Node_Id;
1555 Comp : Node_Id;
1556
1557 -- Start of processing for Add_Interface_Tag_Components
1558
1559 begin
1560 if not RTE_Available (RE_Interface_Tag) then
1561 Error_Msg
1562 ("(Ada 2005) interface types not supported by this run-time!",
1563 Sloc (N));
1564 return;
1565 end if;
1566
1567 if Ekind (Typ) /= E_Record_Type
1568 or else (Is_Concurrent_Record_Type (Typ)
1569 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1570 or else (not Is_Concurrent_Record_Type (Typ)
1571 and then No (Interfaces (Typ))
1572 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1573 then
1574 return;
1575 end if;
1576
1577 -- Find the current last tag
1578
1579 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1580 Ext := Record_Extension_Part (Type_Definition (N));
1581 else
1582 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1583 Ext := Type_Definition (N);
1584 end if;
1585
1586 Last_Tag := Empty;
1587
1588 if not (Present (Component_List (Ext))) then
1589 Set_Null_Present (Ext, False);
1590 L := New_List;
1591 Set_Component_List (Ext,
1592 Make_Component_List (Loc,
1593 Component_Items => L,
1594 Null_Present => False));
1595 else
1596 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1597 L := Component_Items
1598 (Component_List
1599 (Record_Extension_Part
1600 (Type_Definition (N))));
1601 else
1602 L := Component_Items
1603 (Component_List
1604 (Type_Definition (N)));
1605 end if;
1606
1607 -- Find the last tag component
1608
1609 Comp := First (L);
1610 while Present (Comp) loop
1611 if Nkind (Comp) = N_Component_Declaration
1612 and then Is_Tag (Defining_Identifier (Comp))
1613 then
1614 Last_Tag := Comp;
1615 end if;
1616
1617 Next (Comp);
1618 end loop;
1619 end if;
1620
1621 -- At this point L references the list of components and Last_Tag
1622 -- references the current last tag (if any). Now we add the tag
1623 -- corresponding with all the interfaces that are not implemented
1624 -- by the parent.
1625
1626 if Present (Interfaces (Typ)) then
1627 Elmt := First_Elmt (Interfaces (Typ));
1628 while Present (Elmt) loop
1629 Add_Tag (Node (Elmt));
1630 Next_Elmt (Elmt);
1631 end loop;
1632 end if;
1633 end Add_Interface_Tag_Components;
1634
1635 -------------------------------------
1636 -- Add_Internal_Interface_Entities --
1637 -------------------------------------
1638
1639 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1640 Elmt : Elmt_Id;
1641 Iface : Entity_Id;
1642 Iface_Elmt : Elmt_Id;
1643 Iface_Prim : Entity_Id;
1644 Ifaces_List : Elist_Id;
1645 New_Subp : Entity_Id := Empty;
1646 Prim : Entity_Id;
1647 Restore_Scope : Boolean := False;
1648
1649 begin
1650 pragma Assert (Ada_Version >= Ada_2005
1651 and then Is_Record_Type (Tagged_Type)
1652 and then Is_Tagged_Type (Tagged_Type)
1653 and then Has_Interfaces (Tagged_Type)
1654 and then not Is_Interface (Tagged_Type));
1655
1656 -- Ensure that the internal entities are added to the scope of the type
1657
1658 if Scope (Tagged_Type) /= Current_Scope then
1659 Push_Scope (Scope (Tagged_Type));
1660 Restore_Scope := True;
1661 end if;
1662
1663 Collect_Interfaces (Tagged_Type, Ifaces_List);
1664
1665 Iface_Elmt := First_Elmt (Ifaces_List);
1666 while Present (Iface_Elmt) loop
1667 Iface := Node (Iface_Elmt);
1668
1669 -- Originally we excluded here from this processing interfaces that
1670 -- are parents of Tagged_Type because their primitives are located
1671 -- in the primary dispatch table (and hence no auxiliary internal
1672 -- entities are required to handle secondary dispatch tables in such
1673 -- case). However, these auxiliary entities are also required to
1674 -- handle derivations of interfaces in formals of generics (see
1675 -- Derive_Subprograms).
1676
1677 Elmt := First_Elmt (Primitive_Operations (Iface));
1678 while Present (Elmt) loop
1679 Iface_Prim := Node (Elmt);
1680
1681 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1682 Prim :=
1683 Find_Primitive_Covering_Interface
1684 (Tagged_Type => Tagged_Type,
1685 Iface_Prim => Iface_Prim);
1686
1687 if No (Prim) and then Serious_Errors_Detected > 0 then
1688 goto Continue;
1689 end if;
1690
1691 pragma Assert (Present (Prim));
1692
1693 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1694 -- differs from the name of the interface primitive then it is
1695 -- a private primitive inherited from a parent type. In such
1696 -- case, given that Tagged_Type covers the interface, the
1697 -- inherited private primitive becomes visible. For such
1698 -- purpose we add a new entity that renames the inherited
1699 -- private primitive.
1700
1701 if Chars (Prim) /= Chars (Iface_Prim) then
1702 pragma Assert (Has_Suffix (Prim, 'P'));
1703 Derive_Subprogram
1704 (New_Subp => New_Subp,
1705 Parent_Subp => Iface_Prim,
1706 Derived_Type => Tagged_Type,
1707 Parent_Type => Iface);
1708 Set_Alias (New_Subp, Prim);
1709 Set_Is_Abstract_Subprogram
1710 (New_Subp, Is_Abstract_Subprogram (Prim));
1711 end if;
1712
1713 Derive_Subprogram
1714 (New_Subp => New_Subp,
1715 Parent_Subp => Iface_Prim,
1716 Derived_Type => Tagged_Type,
1717 Parent_Type => Iface);
1718
1719 declare
1720 Anc : Entity_Id;
1721 begin
1722 if Is_Inherited_Operation (Prim)
1723 and then Present (Alias (Prim))
1724 then
1725 Anc := Alias (Prim);
1726 else
1727 Anc := Overridden_Operation (Prim);
1728 end if;
1729
1730 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1731 -- nonconforming preconditions in both an ancestor and
1732 -- a progenitor operation.
1733
1734 if Present (Anc)
1735 and then Has_Non_Trivial_Precondition (Anc)
1736 and then Has_Non_Trivial_Precondition (Iface_Prim)
1737 then
1738 if Is_Abstract_Subprogram (Prim)
1739 or else
1740 (Ekind (Prim) = E_Procedure
1741 and then Nkind (Parent (Prim)) =
1742 N_Procedure_Specification
1743 and then Null_Present (Parent (Prim)))
1744 then
1745 null;
1746
1747 -- The inherited operation must be overridden
1748
1749 elsif not Comes_From_Source (Prim) then
1750 Error_Msg_NE
1751 ("&inherits non-conforming preconditions and must "
1752 & "be overridden (RM 6.1.1 (10-16)",
1753 Parent (Tagged_Type), Prim);
1754 end if;
1755 end if;
1756 end;
1757
1758 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1759 -- associated with interface types. These entities are
1760 -- only registered in the list of primitives of its
1761 -- corresponding tagged type because they are only used
1762 -- to fill the contents of the secondary dispatch tables.
1763 -- Therefore they are removed from the homonym chains.
1764
1765 Set_Is_Hidden (New_Subp);
1766 Set_Is_Internal (New_Subp);
1767 Set_Alias (New_Subp, Prim);
1768 Set_Is_Abstract_Subprogram
1769 (New_Subp, Is_Abstract_Subprogram (Prim));
1770 Set_Interface_Alias (New_Subp, Iface_Prim);
1771
1772 -- If the returned type is an interface then propagate it to
1773 -- the returned type. Needed by the thunk to generate the code
1774 -- which displaces "this" to reference the corresponding
1775 -- secondary dispatch table in the returned object.
1776
1777 if Is_Interface (Etype (Iface_Prim)) then
1778 Set_Etype (New_Subp, Etype (Iface_Prim));
1779 end if;
1780
1781 -- Internal entities associated with interface types are only
1782 -- registered in the list of primitives of the tagged type.
1783 -- They are only used to fill the contents of the secondary
1784 -- dispatch tables. Therefore they are not needed in the
1785 -- homonym chains.
1786
1787 Remove_Homonym (New_Subp);
1788
1789 -- Hidden entities associated with interfaces must have set
1790 -- the Has_Delay_Freeze attribute to ensure that, in case
1791 -- of locally defined tagged types (or compiling with static
1792 -- dispatch tables generation disabled) the corresponding
1793 -- entry of the secondary dispatch table is filled when such
1794 -- an entity is frozen. This is an expansion activity that must
1795 -- be suppressed for ASIS because it leads to gigi elaboration
1796 -- issues in annotate mode.
1797
1798 if not ASIS_Mode then
1799 Set_Has_Delayed_Freeze (New_Subp);
1800 end if;
1801 end if;
1802
1803 <<Continue>>
1804 Next_Elmt (Elmt);
1805 end loop;
1806
1807 Next_Elmt (Iface_Elmt);
1808 end loop;
1809
1810 if Restore_Scope then
1811 Pop_Scope;
1812 end if;
1813 end Add_Internal_Interface_Entities;
1814
1815 -----------------------------------
1816 -- Analyze_Component_Declaration --
1817 -----------------------------------
1818
1819 procedure Analyze_Component_Declaration (N : Node_Id) is
1820 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1821 Id : constant Entity_Id := Defining_Identifier (N);
1822 E : constant Node_Id := Expression (N);
1823 Typ : constant Node_Id :=
1824 Subtype_Indication (Component_Definition (N));
1825 T : Entity_Id;
1826 P : Entity_Id;
1827
1828 function Contains_POC (Constr : Node_Id) return Boolean;
1829 -- Determines whether a constraint uses the discriminant of a record
1830 -- type thus becoming a per-object constraint (POC).
1831
1832 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1833 -- Typ is the type of the current component, check whether this type is
1834 -- a limited type. Used to validate declaration against that of
1835 -- enclosing record.
1836
1837 ------------------
1838 -- Contains_POC --
1839 ------------------
1840
1841 function Contains_POC (Constr : Node_Id) return Boolean is
1842 begin
1843 -- Prevent cascaded errors
1844
1845 if Error_Posted (Constr) then
1846 return False;
1847 end if;
1848
1849 case Nkind (Constr) is
1850 when N_Attribute_Reference =>
1851 return Attribute_Name (Constr) = Name_Access
1852 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1853
1854 when N_Discriminant_Association =>
1855 return Denotes_Discriminant (Expression (Constr));
1856
1857 when N_Identifier =>
1858 return Denotes_Discriminant (Constr);
1859
1860 when N_Index_Or_Discriminant_Constraint =>
1861 declare
1862 IDC : Node_Id;
1863
1864 begin
1865 IDC := First (Constraints (Constr));
1866 while Present (IDC) loop
1867
1868 -- One per-object constraint is sufficient
1869
1870 if Contains_POC (IDC) then
1871 return True;
1872 end if;
1873
1874 Next (IDC);
1875 end loop;
1876
1877 return False;
1878 end;
1879
1880 when N_Range =>
1881 return Denotes_Discriminant (Low_Bound (Constr))
1882 or else
1883 Denotes_Discriminant (High_Bound (Constr));
1884
1885 when N_Range_Constraint =>
1886 return Denotes_Discriminant (Range_Expression (Constr));
1887
1888 when others =>
1889 return False;
1890 end case;
1891 end Contains_POC;
1892
1893 ----------------------
1894 -- Is_Known_Limited --
1895 ----------------------
1896
1897 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1898 P : constant Entity_Id := Etype (Typ);
1899 R : constant Entity_Id := Root_Type (Typ);
1900
1901 begin
1902 if Is_Limited_Record (Typ) then
1903 return True;
1904
1905 -- If the root type is limited (and not a limited interface)
1906 -- so is the current type
1907
1908 elsif Is_Limited_Record (R)
1909 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1910 then
1911 return True;
1912
1913 -- Else the type may have a limited interface progenitor, but a
1914 -- limited record parent.
1915
1916 elsif R /= P and then Is_Limited_Record (P) then
1917 return True;
1918
1919 else
1920 return False;
1921 end if;
1922 end Is_Known_Limited;
1923
1924 -- Start of processing for Analyze_Component_Declaration
1925
1926 begin
1927 Generate_Definition (Id);
1928 Enter_Name (Id);
1929
1930 if Present (Typ) then
1931 T := Find_Type_Of_Object
1932 (Subtype_Indication (Component_Definition (N)), N);
1933
1934 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1935 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1936 end if;
1937
1938 -- Ada 2005 (AI-230): Access Definition case
1939
1940 else
1941 pragma Assert (Present
1942 (Access_Definition (Component_Definition (N))));
1943
1944 T := Access_Definition
1945 (Related_Nod => N,
1946 N => Access_Definition (Component_Definition (N)));
1947 Set_Is_Local_Anonymous_Access (T);
1948
1949 -- Ada 2005 (AI-254)
1950
1951 if Present (Access_To_Subprogram_Definition
1952 (Access_Definition (Component_Definition (N))))
1953 and then Protected_Present (Access_To_Subprogram_Definition
1954 (Access_Definition
1955 (Component_Definition (N))))
1956 then
1957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1958 end if;
1959 end if;
1960
1961 -- If the subtype is a constrained subtype of the enclosing record,
1962 -- (which must have a partial view) the back-end does not properly
1963 -- handle the recursion. Rewrite the component declaration with an
1964 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1965 -- the tree directly because side effects have already been removed from
1966 -- discriminant constraints.
1967
1968 if Ekind (T) = E_Access_Subtype
1969 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1970 and then Comes_From_Source (T)
1971 and then Nkind (Parent (T)) = N_Subtype_Declaration
1972 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1973 then
1974 Rewrite
1975 (Subtype_Indication (Component_Definition (N)),
1976 New_Copy_Tree (Subtype_Indication (Parent (T))));
1977 T := Find_Type_Of_Object
1978 (Subtype_Indication (Component_Definition (N)), N);
1979 end if;
1980
1981 -- If the component declaration includes a default expression, then we
1982 -- check that the component is not of a limited type (RM 3.7(5)),
1983 -- and do the special preanalysis of the expression (see section on
1984 -- "Handling of Default and Per-Object Expressions" in the spec of
1985 -- package Sem).
1986
1987 if Present (E) then
1988 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1989 Preanalyze_Default_Expression (E, T);
1990 Check_Initialization (T, E);
1991
1992 if Ada_Version >= Ada_2005
1993 and then Ekind (T) = E_Anonymous_Access_Type
1994 and then Etype (E) /= Any_Type
1995 then
1996 -- Check RM 3.9.2(9): "if the expected type for an expression is
1997 -- an anonymous access-to-specific tagged type, then the object
1998 -- designated by the expression shall not be dynamically tagged
1999 -- unless it is a controlling operand in a call on a dispatching
2000 -- operation"
2001
2002 if Is_Tagged_Type (Directly_Designated_Type (T))
2003 and then
2004 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2005 and then
2006 Ekind (Directly_Designated_Type (Etype (E))) =
2007 E_Class_Wide_Type
2008 then
2009 Error_Msg_N
2010 ("access to specific tagged type required (RM 3.9.2(9))", E);
2011 end if;
2012
2013 -- (Ada 2005: AI-230): Accessibility check for anonymous
2014 -- components
2015
2016 if Type_Access_Level (Etype (E)) >
2017 Deepest_Type_Access_Level (T)
2018 then
2019 Error_Msg_N
2020 ("expression has deeper access level than component " &
2021 "(RM 3.10.2 (12.2))", E);
2022 end if;
2023
2024 -- The initialization expression is a reference to an access
2025 -- discriminant. The type of the discriminant is always deeper
2026 -- than any access type.
2027
2028 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2029 and then Is_Entity_Name (E)
2030 and then Ekind (Entity (E)) = E_In_Parameter
2031 and then Present (Discriminal_Link (Entity (E)))
2032 then
2033 Error_Msg_N
2034 ("discriminant has deeper accessibility level than target",
2035 E);
2036 end if;
2037 end if;
2038 end if;
2039
2040 -- The parent type may be a private view with unknown discriminants,
2041 -- and thus unconstrained. Regular components must be constrained.
2042
2043 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2044 if Is_Class_Wide_Type (T) then
2045 Error_Msg_N
2046 ("class-wide subtype with unknown discriminants" &
2047 " in component declaration",
2048 Subtype_Indication (Component_Definition (N)));
2049 else
2050 Error_Msg_N
2051 ("unconstrained subtype in component declaration",
2052 Subtype_Indication (Component_Definition (N)));
2053 end if;
2054
2055 -- Components cannot be abstract, except for the special case of
2056 -- the _Parent field (case of extending an abstract tagged type)
2057
2058 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2059 Error_Msg_N ("type of a component cannot be abstract", N);
2060 end if;
2061
2062 Set_Etype (Id, T);
2063 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2064
2065 -- The component declaration may have a per-object constraint, set
2066 -- the appropriate flag in the defining identifier of the subtype.
2067
2068 if Present (Subtype_Indication (Component_Definition (N))) then
2069 declare
2070 Sindic : constant Node_Id :=
2071 Subtype_Indication (Component_Definition (N));
2072 begin
2073 if Nkind (Sindic) = N_Subtype_Indication
2074 and then Present (Constraint (Sindic))
2075 and then Contains_POC (Constraint (Sindic))
2076 then
2077 Set_Has_Per_Object_Constraint (Id);
2078 end if;
2079 end;
2080 end if;
2081
2082 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2083 -- out some static checks.
2084
2085 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2086 Null_Exclusion_Static_Checks (N);
2087 end if;
2088
2089 -- If this component is private (or depends on a private type), flag the
2090 -- record type to indicate that some operations are not available.
2091
2092 P := Private_Component (T);
2093
2094 if Present (P) then
2095
2096 -- Check for circular definitions
2097
2098 if P = Any_Type then
2099 Set_Etype (Id, Any_Type);
2100
2101 -- There is a gap in the visibility of operations only if the
2102 -- component type is not defined in the scope of the record type.
2103
2104 elsif Scope (P) = Scope (Current_Scope) then
2105 null;
2106
2107 elsif Is_Limited_Type (P) then
2108 Set_Is_Limited_Composite (Current_Scope);
2109
2110 else
2111 Set_Is_Private_Composite (Current_Scope);
2112 end if;
2113 end if;
2114
2115 if P /= Any_Type
2116 and then Is_Limited_Type (T)
2117 and then Chars (Id) /= Name_uParent
2118 and then Is_Tagged_Type (Current_Scope)
2119 then
2120 if Is_Derived_Type (Current_Scope)
2121 and then not Is_Known_Limited (Current_Scope)
2122 then
2123 Error_Msg_N
2124 ("extension of nonlimited type cannot have limited components",
2125 N);
2126
2127 if Is_Interface (Root_Type (Current_Scope)) then
2128 Error_Msg_N
2129 ("\limitedness is not inherited from limited interface", N);
2130 Error_Msg_N ("\add LIMITED to type indication", N);
2131 end if;
2132
2133 Explain_Limited_Type (T, N);
2134 Set_Etype (Id, Any_Type);
2135 Set_Is_Limited_Composite (Current_Scope, False);
2136
2137 elsif not Is_Derived_Type (Current_Scope)
2138 and then not Is_Limited_Record (Current_Scope)
2139 and then not Is_Concurrent_Type (Current_Scope)
2140 then
2141 Error_Msg_N
2142 ("nonlimited tagged type cannot have limited components", N);
2143 Explain_Limited_Type (T, N);
2144 Set_Etype (Id, Any_Type);
2145 Set_Is_Limited_Composite (Current_Scope, False);
2146 end if;
2147 end if;
2148
2149 -- If the component is an unconstrained task or protected type with
2150 -- discriminants, the component and the enclosing record are limited
2151 -- and the component is constrained by its default values. Compute
2152 -- its actual subtype, else it may be allocated the maximum size by
2153 -- the backend, and possibly overflow.
2154
2155 if Is_Concurrent_Type (T)
2156 and then not Is_Constrained (T)
2157 and then Has_Discriminants (T)
2158 and then not Has_Discriminants (Current_Scope)
2159 then
2160 declare
2161 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2162
2163 begin
2164 Set_Etype (Id, Act_T);
2165
2166 -- Rewrite component definition to use the constrained subtype
2167
2168 Rewrite (Component_Definition (N),
2169 Make_Component_Definition (Loc,
2170 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2171 end;
2172 end if;
2173
2174 Set_Original_Record_Component (Id, Id);
2175
2176 if Has_Aspects (N) then
2177 Analyze_Aspect_Specifications (N, Id);
2178 end if;
2179
2180 Analyze_Dimension (N);
2181 end Analyze_Component_Declaration;
2182
2183 --------------------------
2184 -- Analyze_Declarations --
2185 --------------------------
2186
2187 procedure Analyze_Declarations (L : List_Id) is
2188 Decl : Node_Id;
2189
2190 procedure Adjust_Decl;
2191 -- Adjust Decl not to include implicit label declarations, since these
2192 -- have strange Sloc values that result in elaboration check problems.
2193 -- (They have the sloc of the label as found in the source, and that
2194 -- is ahead of the current declarative part).
2195
2196 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2197 -- Create the subprogram bodies which verify the run-time semantics of
2198 -- the pragmas listed below for each elibigle type found in declarative
2199 -- list Decls. The pragmas are:
2200 --
2201 -- Default_Initial_Condition
2202 -- Invariant
2203 -- Type_Invariant
2204 --
2205 -- Context denotes the owner of the declarative list.
2206
2207 procedure Check_Entry_Contracts;
2208 -- Perform a pre-analysis of the pre- and postconditions of an entry
2209 -- declaration. This must be done before full resolution and creation
2210 -- of the parameter block, etc. to catch illegal uses within the
2211 -- contract expression. Full analysis of the expression is done when
2212 -- the contract is processed.
2213
2214 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2215 -- Determine whether Body_Decl denotes the body of a late controlled
2216 -- primitive (either Initialize, Adjust or Finalize). If this is the
2217 -- case, add a proper spec if the body lacks one. The spec is inserted
2218 -- before Body_Decl and immediately analyzed.
2219
2220 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2221 -- Spec_Id is the entity of a package that may define abstract states,
2222 -- and in the case of a child unit, whose ancestors may define abstract
2223 -- states. If the states have partial visible refinement, remove the
2224 -- partial visibility of each constituent at the end of the package
2225 -- spec and body declarations.
2226
2227 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2228 -- Spec_Id is the entity of a package that may define abstract states.
2229 -- If the states have visible refinement, remove the visibility of each
2230 -- constituent at the end of the package body declaration.
2231
2232 procedure Resolve_Aspects;
2233 -- Utility to resolve the expressions of aspects at the end of a list of
2234 -- declarations.
2235
2236 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean;
2237 -- Check if an inner package has entities within it that rely on library
2238 -- level private types where the full view has not been seen.
2239
2240 -----------------
2241 -- Adjust_Decl --
2242 -----------------
2243
2244 procedure Adjust_Decl is
2245 begin
2246 while Present (Prev (Decl))
2247 and then Nkind (Decl) = N_Implicit_Label_Declaration
2248 loop
2249 Prev (Decl);
2250 end loop;
2251 end Adjust_Decl;
2252
2253 ----------------------------
2254 -- Build_Assertion_Bodies --
2255 ----------------------------
2256
2257 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2258 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2259 -- Create the subprogram bodies which verify the run-time semantics
2260 -- of the pragmas listed below for type Typ. The pragmas are:
2261 --
2262 -- Default_Initial_Condition
2263 -- Invariant
2264 -- Type_Invariant
2265
2266 -------------------------------------
2267 -- Build_Assertion_Bodies_For_Type --
2268 -------------------------------------
2269
2270 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2271 begin
2272 -- Preanalyze and resolve the Default_Initial_Condition assertion
2273 -- expression at the end of the declarations to catch any errors.
2274
2275 if Has_DIC (Typ) then
2276 Build_DIC_Procedure_Body (Typ);
2277 end if;
2278
2279 if Nkind (Context) = N_Package_Specification then
2280
2281 -- Preanalyze and resolve the class-wide invariants of an
2282 -- interface at the end of whichever declarative part has the
2283 -- interface type. Note that an interface may be declared in
2284 -- any non-package declarative part, but reaching the end of
2285 -- such a declarative part will always freeze the type and
2286 -- generate the invariant procedure (see Freeze_Type).
2287
2288 if Is_Interface (Typ) then
2289
2290 -- Interfaces are treated as the partial view of a private
2291 -- type, in order to achieve uniformity with the general
2292 -- case. As a result, an interface receives only a "partial"
2293 -- invariant procedure, which is never called.
2294
2295 if Has_Own_Invariants (Typ) then
2296 Build_Invariant_Procedure_Body
2297 (Typ => Typ,
2298 Partial_Invariant => True);
2299 end if;
2300
2301 -- Preanalyze and resolve the invariants of a private type
2302 -- at the end of the visible declarations to catch potential
2303 -- errors. Inherited class-wide invariants are not included
2304 -- because they have already been resolved.
2305
2306 elsif Decls = Visible_Declarations (Context)
2307 and then Ekind_In (Typ, E_Limited_Private_Type,
2308 E_Private_Type,
2309 E_Record_Type_With_Private)
2310 and then Has_Own_Invariants (Typ)
2311 then
2312 Build_Invariant_Procedure_Body
2313 (Typ => Typ,
2314 Partial_Invariant => True);
2315
2316 -- Preanalyze and resolve the invariants of a private type's
2317 -- full view at the end of the private declarations to catch
2318 -- potential errors.
2319
2320 elsif Decls = Private_Declarations (Context)
2321 and then not Is_Private_Type (Typ)
2322 and then Has_Private_Declaration (Typ)
2323 and then Has_Invariants (Typ)
2324 then
2325 Build_Invariant_Procedure_Body (Typ);
2326 end if;
2327 end if;
2328 end Build_Assertion_Bodies_For_Type;
2329
2330 -- Local variables
2331
2332 Decl : Node_Id;
2333 Decl_Id : Entity_Id;
2334
2335 -- Start of processing for Build_Assertion_Bodies
2336
2337 begin
2338 Decl := First (Decls);
2339 while Present (Decl) loop
2340 if Is_Declaration (Decl) then
2341 Decl_Id := Defining_Entity (Decl);
2342
2343 if Is_Type (Decl_Id) then
2344 Build_Assertion_Bodies_For_Type (Decl_Id);
2345 end if;
2346 end if;
2347
2348 Next (Decl);
2349 end loop;
2350 end Build_Assertion_Bodies;
2351
2352 ---------------------------
2353 -- Check_Entry_Contracts --
2354 ---------------------------
2355
2356 procedure Check_Entry_Contracts is
2357 ASN : Node_Id;
2358 Ent : Entity_Id;
2359 Exp : Node_Id;
2360
2361 begin
2362 Ent := First_Entity (Current_Scope);
2363 while Present (Ent) loop
2364
2365 -- This only concerns entries with pre/postconditions
2366
2367 if Ekind (Ent) = E_Entry
2368 and then Present (Contract (Ent))
2369 and then Present (Pre_Post_Conditions (Contract (Ent)))
2370 then
2371 ASN := Pre_Post_Conditions (Contract (Ent));
2372 Push_Scope (Ent);
2373 Install_Formals (Ent);
2374
2375 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2376 -- is performed on a copy of the pragma expression, to prevent
2377 -- modifying the original expression.
2378
2379 while Present (ASN) loop
2380 if Nkind (ASN) = N_Pragma then
2381 Exp :=
2382 New_Copy_Tree
2383 (Expression
2384 (First (Pragma_Argument_Associations (ASN))));
2385 Set_Parent (Exp, ASN);
2386
2387 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2388 end if;
2389
2390 ASN := Next_Pragma (ASN);
2391 end loop;
2392
2393 End_Scope;
2394 end if;
2395
2396 Next_Entity (Ent);
2397 end loop;
2398 end Check_Entry_Contracts;
2399
2400 --------------------------------------
2401 -- Handle_Late_Controlled_Primitive --
2402 --------------------------------------
2403
2404 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2405 Body_Spec : constant Node_Id := Specification (Body_Decl);
2406 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2407 Loc : constant Source_Ptr := Sloc (Body_Id);
2408 Params : constant List_Id :=
2409 Parameter_Specifications (Body_Spec);
2410 Spec : Node_Id;
2411 Spec_Id : Entity_Id;
2412 Typ : Node_Id;
2413
2414 begin
2415 -- Consider only procedure bodies whose name matches one of the three
2416 -- controlled primitives.
2417
2418 if Nkind (Body_Spec) /= N_Procedure_Specification
2419 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2420 Name_Finalize,
2421 Name_Initialize)
2422 then
2423 return;
2424
2425 -- A controlled primitive must have exactly one formal which is not
2426 -- an anonymous access type.
2427
2428 elsif List_Length (Params) /= 1 then
2429 return;
2430 end if;
2431
2432 Typ := Parameter_Type (First (Params));
2433
2434 if Nkind (Typ) = N_Access_Definition then
2435 return;
2436 end if;
2437
2438 Find_Type (Typ);
2439
2440 -- The type of the formal must be derived from [Limited_]Controlled
2441
2442 if not Is_Controlled (Entity (Typ)) then
2443 return;
2444 end if;
2445
2446 -- Check whether a specification exists for this body. We do not
2447 -- analyze the spec of the body in full, because it will be analyzed
2448 -- again when the body is properly analyzed, and we cannot create
2449 -- duplicate entries in the formals chain. We look for an explicit
2450 -- specification because the body may be an overriding operation and
2451 -- an inherited spec may be present.
2452
2453 Spec_Id := Current_Entity (Body_Id);
2454
2455 while Present (Spec_Id) loop
2456 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2457 and then Scope (Spec_Id) = Current_Scope
2458 and then Present (First_Formal (Spec_Id))
2459 and then No (Next_Formal (First_Formal (Spec_Id)))
2460 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2461 and then Comes_From_Source (Spec_Id)
2462 then
2463 return;
2464 end if;
2465
2466 Spec_Id := Homonym (Spec_Id);
2467 end loop;
2468
2469 -- At this point the body is known to be a late controlled primitive.
2470 -- Generate a matching spec and insert it before the body. Note the
2471 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2472 -- tree in this case.
2473
2474 Spec := Copy_Separate_Tree (Body_Spec);
2475
2476 -- Ensure that the subprogram declaration does not inherit the null
2477 -- indicator from the body as we now have a proper spec/body pair.
2478
2479 Set_Null_Present (Spec, False);
2480
2481 -- Ensure that the freeze node is inserted after the declaration of
2482 -- the primitive since its expansion will freeze the primitive.
2483
2484 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2485
2486 Insert_Before_And_Analyze (Body_Decl, Decl);
2487 end Handle_Late_Controlled_Primitive;
2488
2489 ----------------------------------------
2490 -- Remove_Partial_Visible_Refinements --
2491 ----------------------------------------
2492
2493 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2494 State_Elmt : Elmt_Id;
2495 begin
2496 if Present (Abstract_States (Spec_Id)) then
2497 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2498 while Present (State_Elmt) loop
2499 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2500 Next_Elmt (State_Elmt);
2501 end loop;
2502 end if;
2503
2504 -- For a child unit, also hide the partial state refinement from
2505 -- ancestor packages.
2506
2507 if Is_Child_Unit (Spec_Id) then
2508 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2509 end if;
2510 end Remove_Partial_Visible_Refinements;
2511
2512 --------------------------------
2513 -- Remove_Visible_Refinements --
2514 --------------------------------
2515
2516 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2517 State_Elmt : Elmt_Id;
2518 begin
2519 if Present (Abstract_States (Spec_Id)) then
2520 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2521 while Present (State_Elmt) loop
2522 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2523 Next_Elmt (State_Elmt);
2524 end loop;
2525 end if;
2526 end Remove_Visible_Refinements;
2527
2528 ---------------------
2529 -- Resolve_Aspects --
2530 ---------------------
2531
2532 procedure Resolve_Aspects is
2533 E : Entity_Id;
2534
2535 begin
2536 E := First_Entity (Current_Scope);
2537 while Present (E) loop
2538 Resolve_Aspect_Expressions (E);
2539 Next_Entity (E);
2540 end loop;
2541 end Resolve_Aspects;
2542
2543 -------------------------------
2544 -- Uses_Unseen_Lib_Unit_Priv --
2545 -------------------------------
2546
2547 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean is
2548 Curr : Entity_Id;
2549
2550 begin
2551 -- Avoid looking through scopes that do not meet the precondition of
2552 -- Pkg not being within a library unit spec.
2553
2554 if not Is_Compilation_Unit (Pkg)
2555 and then not Is_Generic_Instance (Pkg)
2556 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2557 then
2558 -- Loop through all entities in the current scope to identify
2559 -- an entity that depends on a private type.
2560
2561 Curr := First_Entity (Pkg);
2562 loop
2563 if Nkind (Curr) in N_Entity
2564 and then Depends_On_Private (Curr)
2565 then
2566 return True;
2567 end if;
2568
2569 exit when Last_Entity (Current_Scope) = Curr;
2570 Curr := Next_Entity (Curr);
2571 end loop;
2572 end if;
2573
2574 return False;
2575 end Uses_Unseen_Lib_Unit_Priv;
2576
2577 -- Local variables
2578
2579 Context : Node_Id := Empty;
2580 Freeze_From : Entity_Id := Empty;
2581 Next_Decl : Node_Id;
2582
2583 Body_Seen : Boolean := False;
2584 -- Flag set when the first body [stub] is encountered
2585
2586 -- Start of processing for Analyze_Declarations
2587
2588 begin
2589 if Restriction_Check_Required (SPARK_05) then
2590 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2591 end if;
2592
2593 Decl := First (L);
2594 while Present (Decl) loop
2595
2596 -- Package spec cannot contain a package declaration in SPARK
2597
2598 if Nkind (Decl) = N_Package_Declaration
2599 and then Nkind (Parent (L)) = N_Package_Specification
2600 then
2601 Check_SPARK_05_Restriction
2602 ("package specification cannot contain a package declaration",
2603 Decl);
2604 end if;
2605
2606 -- Complete analysis of declaration
2607
2608 Analyze (Decl);
2609 Next_Decl := Next (Decl);
2610
2611 if No (Freeze_From) then
2612 Freeze_From := First_Entity (Current_Scope);
2613 end if;
2614
2615 -- At the end of a declarative part, freeze remaining entities
2616 -- declared in it. The end of the visible declarations of package
2617 -- specification is not the end of a declarative part if private
2618 -- declarations are present. The end of a package declaration is a
2619 -- freezing point only if it a library package. A task definition or
2620 -- protected type definition is not a freeze point either. Finally,
2621 -- we do not freeze entities in generic scopes, because there is no
2622 -- code generated for them and freeze nodes will be generated for
2623 -- the instance.
2624
2625 -- The end of a package instantiation is not a freeze point, but
2626 -- for now we make it one, because the generic body is inserted
2627 -- (currently) immediately after. Generic instantiations will not
2628 -- be a freeze point once delayed freezing of bodies is implemented.
2629 -- (This is needed in any case for early instantiations ???).
2630
2631 if No (Next_Decl) then
2632 if Nkind (Parent (L)) = N_Component_List then
2633 null;
2634
2635 elsif Nkind_In (Parent (L), N_Protected_Definition,
2636 N_Task_Definition)
2637 then
2638 Check_Entry_Contracts;
2639
2640 elsif Nkind (Parent (L)) /= N_Package_Specification then
2641 if Nkind (Parent (L)) = N_Package_Body then
2642 Freeze_From := First_Entity (Current_Scope);
2643 end if;
2644
2645 -- There may have been several freezing points previously,
2646 -- for example object declarations or subprogram bodies, but
2647 -- at the end of a declarative part we check freezing from
2648 -- the beginning, even though entities may already be frozen,
2649 -- in order to perform visibility checks on delayed aspects.
2650
2651 Adjust_Decl;
2652 Freeze_All (First_Entity (Current_Scope), Decl);
2653 Freeze_From := Last_Entity (Current_Scope);
2654
2655 -- Current scope is a package specification
2656
2657 elsif Scope (Current_Scope) /= Standard_Standard
2658 and then not Is_Child_Unit (Current_Scope)
2659 and then No (Generic_Parent (Parent (L)))
2660 then
2661 -- This is needed in all cases to catch visibility errors in
2662 -- aspect expressions, but several large user tests are now
2663 -- rejected. Pending notification we restrict this call to
2664 -- ASIS mode.
2665
2666 if ASIS_Mode then
2667 Resolve_Aspects;
2668 end if;
2669
2670 elsif L /= Visible_Declarations (Parent (L))
2671 or else No (Private_Declarations (Parent (L)))
2672 or else Is_Empty_List (Private_Declarations (Parent (L)))
2673 then
2674 Adjust_Decl;
2675
2676 -- End of a package declaration
2677
2678 -- In compilation mode the expansion of freeze node takes care
2679 -- of resolving expressions of all aspects in the list. In ASIS
2680 -- mode this must be done explicitly.
2681
2682 if ASIS_Mode
2683 and then Scope (Current_Scope) = Standard_Standard
2684 then
2685 Resolve_Aspects;
2686 end if;
2687
2688 -- This is a freeze point because it is the end of a
2689 -- compilation unit.
2690
2691 Freeze_All (First_Entity (Current_Scope), Decl);
2692 Freeze_From := Last_Entity (Current_Scope);
2693
2694 -- At the end of the visible declarations the expressions in
2695 -- aspects of all entities declared so far must be resolved.
2696 -- The entities themselves might be frozen later, and the
2697 -- generated pragmas and attribute definition clauses analyzed
2698 -- in full at that point, but name resolution must take place
2699 -- now.
2700 -- In addition to being the proper semantics, this is mandatory
2701 -- within generic units, because global name capture requires
2702 -- those expressions to be analyzed, given that the generated
2703 -- pragmas do not appear in the original generic tree.
2704
2705 elsif Serious_Errors_Detected = 0 then
2706 Resolve_Aspects;
2707 end if;
2708
2709 -- If next node is a body then freeze all types before the body.
2710 -- An exception occurs for some expander-generated bodies. If these
2711 -- are generated at places where in general language rules would not
2712 -- allow a freeze point, then we assume that the expander has
2713 -- explicitly checked that all required types are properly frozen,
2714 -- and we do not cause general freezing here. This special circuit
2715 -- is used when the encountered body is marked as having already
2716 -- been analyzed.
2717
2718 -- In all other cases (bodies that come from source, and expander
2719 -- generated bodies that have not been analyzed yet), freeze all
2720 -- types now. Note that in the latter case, the expander must take
2721 -- care to attach the bodies at a proper place in the tree so as to
2722 -- not cause unwanted freezing at that point.
2723
2724 -- It is also necessary to check for a case where both an expression
2725 -- function is used and the current scope depends on an unseen
2726 -- private type from a library unit, otherwise premature freezing of
2727 -- the private type will occur.
2728
2729 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2730 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2731 or else not Was_Expression_Function (Next_Decl))
2732 or else not Uses_Unseen_Lib_Unit_Priv (Current_Scope))
2733 then
2734 -- When a controlled type is frozen, the expander generates stream
2735 -- and controlled-type support routines. If the freeze is caused
2736 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2737 -- expander will end up using the wrong version of these routines,
2738 -- as the body has not been processed yet. To remedy this, detect
2739 -- a late controlled primitive and create a proper spec for it.
2740 -- This ensures that the primitive will override its inherited
2741 -- counterpart before the freeze takes place.
2742
2743 -- If the declaration we just processed is a body, do not attempt
2744 -- to examine Next_Decl as the late primitive idiom can only apply
2745 -- to the first encountered body.
2746
2747 -- The spec of the late primitive is not generated in ASIS mode to
2748 -- ensure a consistent list of primitives that indicates the true
2749 -- semantic structure of the program (which is not relevant when
2750 -- generating executable code).
2751
2752 -- ??? A cleaner approach may be possible and/or this solution
2753 -- could be extended to general-purpose late primitives, TBD.
2754
2755 if not ASIS_Mode
2756 and then not Body_Seen
2757 and then not Is_Body (Decl)
2758 then
2759 Body_Seen := True;
2760
2761 if Nkind (Next_Decl) = N_Subprogram_Body then
2762 Handle_Late_Controlled_Primitive (Next_Decl);
2763 end if;
2764 end if;
2765
2766 Adjust_Decl;
2767
2768 -- The generated body of an expression function does not freeze,
2769 -- unless it is a completion, in which case only the expression
2770 -- itself freezes. This is handled when the body itself is
2771 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2772
2773 Freeze_All (Freeze_From, Decl);
2774 Freeze_From := Last_Entity (Current_Scope);
2775 end if;
2776
2777 Decl := Next_Decl;
2778 end loop;
2779
2780 -- Post-freezing actions
2781
2782 if Present (L) then
2783 Context := Parent (L);
2784
2785 -- Analyze the contracts of packages and their bodies
2786
2787 if Nkind (Context) = N_Package_Specification then
2788
2789 -- When a package has private declarations, its contract must be
2790 -- analyzed at the end of the said declarations. This way both the
2791 -- analysis and freeze actions are properly synchronized in case
2792 -- of private type use within the contract.
2793
2794 if L = Private_Declarations (Context) then
2795 Analyze_Package_Contract (Defining_Entity (Context));
2796
2797 -- Otherwise the contract is analyzed at the end of the visible
2798 -- declarations.
2799
2800 elsif L = Visible_Declarations (Context)
2801 and then No (Private_Declarations (Context))
2802 then
2803 Analyze_Package_Contract (Defining_Entity (Context));
2804 end if;
2805
2806 elsif Nkind (Context) = N_Package_Body then
2807 Analyze_Package_Body_Contract (Defining_Entity (Context));
2808 end if;
2809
2810 -- Analyze the contracts of various constructs now due to the delayed
2811 -- visibility needs of their aspects and pragmas.
2812
2813 Analyze_Contracts (L);
2814
2815 if Nkind (Context) = N_Package_Body then
2816
2817 -- Ensure that all abstract states and objects declared in the
2818 -- state space of a package body are utilized as constituents.
2819
2820 Check_Unused_Body_States (Defining_Entity (Context));
2821
2822 -- State refinements are visible up to the end of the package body
2823 -- declarations. Hide the state refinements from visibility to
2824 -- restore the original state conditions.
2825
2826 Remove_Visible_Refinements (Corresponding_Spec (Context));
2827 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2828
2829 elsif Nkind (Context) = N_Package_Declaration then
2830
2831 -- Partial state refinements are visible up to the end of the
2832 -- package spec declarations. Hide the partial state refinements
2833 -- from visibility to restore the original state conditions.
2834
2835 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2836 end if;
2837
2838 -- Verify that all abstract states found in any package declared in
2839 -- the input declarative list have proper refinements. The check is
2840 -- performed only when the context denotes a block, entry, package,
2841 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2842
2843 Check_State_Refinements (Context);
2844
2845 -- Create the subprogram bodies which verify the run-time semantics
2846 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2847 -- types within the current declarative list. This ensures that all
2848 -- assertion expressions are preanalyzed and resolved at the end of
2849 -- the declarative part. Note that the resolution happens even when
2850 -- freezing does not take place.
2851
2852 Build_Assertion_Bodies (L, Context);
2853 end if;
2854 end Analyze_Declarations;
2855
2856 -----------------------------------
2857 -- Analyze_Full_Type_Declaration --
2858 -----------------------------------
2859
2860 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2861 Def : constant Node_Id := Type_Definition (N);
2862 Def_Id : constant Entity_Id := Defining_Identifier (N);
2863 T : Entity_Id;
2864 Prev : Entity_Id;
2865
2866 Is_Remote : constant Boolean :=
2867 (Is_Remote_Types (Current_Scope)
2868 or else Is_Remote_Call_Interface (Current_Scope))
2869 and then not (In_Private_Part (Current_Scope)
2870 or else In_Package_Body (Current_Scope));
2871
2872 procedure Check_Nonoverridable_Aspects;
2873 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2874 -- be overridden, and can only be confirmed on derivation.
2875
2876 procedure Check_Ops_From_Incomplete_Type;
2877 -- If there is a tagged incomplete partial view of the type, traverse
2878 -- the primitives of the incomplete view and change the type of any
2879 -- controlling formals and result to indicate the full view. The
2880 -- primitives will be added to the full type's primitive operations
2881 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2882 -- is called from Process_Incomplete_Dependents).
2883
2884 ----------------------------------
2885 -- Check_Nonoverridable_Aspects --
2886 ----------------------------------
2887
2888 procedure Check_Nonoverridable_Aspects is
2889 function Get_Aspect_Spec
2890 (Specs : List_Id;
2891 Aspect_Name : Name_Id) return Node_Id;
2892 -- Check whether a list of aspect specifications includes an entry
2893 -- for a specific aspect. The list is either that of a partial or
2894 -- a full view.
2895
2896 ---------------------
2897 -- Get_Aspect_Spec --
2898 ---------------------
2899
2900 function Get_Aspect_Spec
2901 (Specs : List_Id;
2902 Aspect_Name : Name_Id) return Node_Id
2903 is
2904 Spec : Node_Id;
2905
2906 begin
2907 Spec := First (Specs);
2908 while Present (Spec) loop
2909 if Chars (Identifier (Spec)) = Aspect_Name then
2910 return Spec;
2911 end if;
2912 Next (Spec);
2913 end loop;
2914
2915 return Empty;
2916 end Get_Aspect_Spec;
2917
2918 -- Local variables
2919
2920 Prev_Aspects : constant List_Id :=
2921 Aspect_Specifications (Parent (Def_Id));
2922 Par_Type : Entity_Id;
2923 Prev_Aspect : Node_Id;
2924
2925 -- Start of processing for Check_Nonoverridable_Aspects
2926
2927 begin
2928 -- Get parent type of derived type. Note that Prev is the entity in
2929 -- the partial declaration, but its contents are now those of full
2930 -- view, while Def_Id reflects the partial view.
2931
2932 if Is_Private_Type (Def_Id) then
2933 Par_Type := Etype (Full_View (Def_Id));
2934 else
2935 Par_Type := Etype (Def_Id);
2936 end if;
2937
2938 -- If there is an inherited Implicit_Dereference, verify that it is
2939 -- made explicit in the partial view.
2940
2941 if Has_Discriminants (Base_Type (Par_Type))
2942 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2943 and then Present (Discriminant_Specifications (Parent (Prev)))
2944 and then Present (Get_Reference_Discriminant (Par_Type))
2945 then
2946 Prev_Aspect :=
2947 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2948
2949 if No (Prev_Aspect)
2950 and then Present
2951 (Discriminant_Specifications
2952 (Original_Node (Parent (Prev))))
2953 then
2954 Error_Msg_N
2955 ("type does not inherit implicit dereference", Prev);
2956
2957 else
2958 -- If one of the views has the aspect specified, verify that it
2959 -- is consistent with that of the parent.
2960
2961 declare
2962 Par_Discr : constant Entity_Id :=
2963 Get_Reference_Discriminant (Par_Type);
2964 Cur_Discr : constant Entity_Id :=
2965 Get_Reference_Discriminant (Prev);
2966
2967 begin
2968 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2969 Error_Msg_N ("aspect incosistent with that of parent", N);
2970 end if;
2971
2972 -- Check that specification in partial view matches the
2973 -- inherited aspect. Compare names directly because aspect
2974 -- expression may not be analyzed.
2975
2976 if Present (Prev_Aspect)
2977 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2978 and then Chars (Expression (Prev_Aspect)) /=
2979 Chars (Cur_Discr)
2980 then
2981 Error_Msg_N
2982 ("aspect incosistent with that of parent", N);
2983 end if;
2984 end;
2985 end if;
2986 end if;
2987
2988 -- TBD : other nonoverridable aspects.
2989 end Check_Nonoverridable_Aspects;
2990
2991 ------------------------------------
2992 -- Check_Ops_From_Incomplete_Type --
2993 ------------------------------------
2994
2995 procedure Check_Ops_From_Incomplete_Type is
2996 Elmt : Elmt_Id;
2997 Formal : Entity_Id;
2998 Op : Entity_Id;
2999
3000 begin
3001 if Prev /= T
3002 and then Ekind (Prev) = E_Incomplete_Type
3003 and then Is_Tagged_Type (Prev)
3004 and then Is_Tagged_Type (T)
3005 then
3006 Elmt := First_Elmt (Primitive_Operations (Prev));
3007 while Present (Elmt) loop
3008 Op := Node (Elmt);
3009
3010 Formal := First_Formal (Op);
3011 while Present (Formal) loop
3012 if Etype (Formal) = Prev then
3013 Set_Etype (Formal, T);
3014 end if;
3015
3016 Next_Formal (Formal);
3017 end loop;
3018
3019 if Etype (Op) = Prev then
3020 Set_Etype (Op, T);
3021 end if;
3022
3023 Next_Elmt (Elmt);
3024 end loop;
3025 end if;
3026 end Check_Ops_From_Incomplete_Type;
3027
3028 -- Start of processing for Analyze_Full_Type_Declaration
3029
3030 begin
3031 Prev := Find_Type_Name (N);
3032
3033 -- The full view, if present, now points to the current type. If there
3034 -- is an incomplete partial view, set a link to it, to simplify the
3035 -- retrieval of primitive operations of the type.
3036
3037 -- Ada 2005 (AI-50217): If the type was previously decorated when
3038 -- imported through a LIMITED WITH clause, it appears as incomplete
3039 -- but has no full view.
3040
3041 if Ekind (Prev) = E_Incomplete_Type
3042 and then Present (Full_View (Prev))
3043 then
3044 T := Full_View (Prev);
3045 Set_Incomplete_View (N, Parent (Prev));
3046 else
3047 T := Prev;
3048 end if;
3049
3050 Set_Is_Pure (T, Is_Pure (Current_Scope));
3051
3052 -- We set the flag Is_First_Subtype here. It is needed to set the
3053 -- corresponding flag for the Implicit class-wide-type created
3054 -- during tagged types processing.
3055
3056 Set_Is_First_Subtype (T, True);
3057
3058 -- Only composite types other than array types are allowed to have
3059 -- discriminants.
3060
3061 case Nkind (Def) is
3062
3063 -- For derived types, the rule will be checked once we've figured
3064 -- out the parent type.
3065
3066 when N_Derived_Type_Definition =>
3067 null;
3068
3069 -- For record types, discriminants are allowed, unless we are in
3070 -- SPARK.
3071
3072 when N_Record_Definition =>
3073 if Present (Discriminant_Specifications (N)) then
3074 Check_SPARK_05_Restriction
3075 ("discriminant type is not allowed",
3076 Defining_Identifier
3077 (First (Discriminant_Specifications (N))));
3078 end if;
3079
3080 when others =>
3081 if Present (Discriminant_Specifications (N)) then
3082 Error_Msg_N
3083 ("elementary or array type cannot have discriminants",
3084 Defining_Identifier
3085 (First (Discriminant_Specifications (N))));
3086 end if;
3087 end case;
3088
3089 -- Elaborate the type definition according to kind, and generate
3090 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3091 -- already done (this happens during the reanalysis that follows a call
3092 -- to the high level optimizer).
3093
3094 if not Analyzed (T) then
3095 Set_Analyzed (T);
3096
3097 case Nkind (Def) is
3098 when N_Access_To_Subprogram_Definition =>
3099 Access_Subprogram_Declaration (T, Def);
3100
3101 -- If this is a remote access to subprogram, we must create the
3102 -- equivalent fat pointer type, and related subprograms.
3103
3104 if Is_Remote then
3105 Process_Remote_AST_Declaration (N);
3106 end if;
3107
3108 -- Validate categorization rule against access type declaration
3109 -- usually a violation in Pure unit, Shared_Passive unit.
3110
3111 Validate_Access_Type_Declaration (T, N);
3112
3113 when N_Access_To_Object_Definition =>
3114 Access_Type_Declaration (T, Def);
3115
3116 -- Validate categorization rule against access type declaration
3117 -- usually a violation in Pure unit, Shared_Passive unit.
3118
3119 Validate_Access_Type_Declaration (T, N);
3120
3121 -- If we are in a Remote_Call_Interface package and define a
3122 -- RACW, then calling stubs and specific stream attributes
3123 -- must be added.
3124
3125 if Is_Remote
3126 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3127 then
3128 Add_RACW_Features (Def_Id);
3129 end if;
3130
3131 when N_Array_Type_Definition =>
3132 Array_Type_Declaration (T, Def);
3133
3134 when N_Derived_Type_Definition =>
3135 Derived_Type_Declaration (T, N, T /= Def_Id);
3136
3137 -- Inherit predicates from parent, and protect against
3138 -- illegal derivations.
3139
3140 if Is_Type (T) and then Has_Predicates (T) then
3141 Set_Has_Predicates (Def_Id);
3142 end if;
3143
3144 when N_Enumeration_Type_Definition =>
3145 Enumeration_Type_Declaration (T, Def);
3146
3147 when N_Floating_Point_Definition =>
3148 Floating_Point_Type_Declaration (T, Def);
3149
3150 when N_Decimal_Fixed_Point_Definition =>
3151 Decimal_Fixed_Point_Type_Declaration (T, Def);
3152
3153 when N_Ordinary_Fixed_Point_Definition =>
3154 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3155
3156 when N_Signed_Integer_Type_Definition =>
3157 Signed_Integer_Type_Declaration (T, Def);
3158
3159 when N_Modular_Type_Definition =>
3160 Modular_Type_Declaration (T, Def);
3161
3162 when N_Record_Definition =>
3163 Record_Type_Declaration (T, N, Prev);
3164
3165 -- If declaration has a parse error, nothing to elaborate.
3166
3167 when N_Error =>
3168 null;
3169
3170 when others =>
3171 raise Program_Error;
3172 end case;
3173 end if;
3174
3175 if Etype (T) = Any_Type then
3176 return;
3177 end if;
3178
3179 -- Controlled type is not allowed in SPARK
3180
3181 if Is_Visibly_Controlled (T) then
3182 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3183 end if;
3184
3185 -- Some common processing for all types
3186
3187 Set_Depends_On_Private (T, Has_Private_Component (T));
3188 Check_Ops_From_Incomplete_Type;
3189
3190 -- Both the declared entity, and its anonymous base type if one was
3191 -- created, need freeze nodes allocated.
3192
3193 declare
3194 B : constant Entity_Id := Base_Type (T);
3195
3196 begin
3197 -- In the case where the base type differs from the first subtype, we
3198 -- pre-allocate a freeze node, and set the proper link to the first
3199 -- subtype. Freeze_Entity will use this preallocated freeze node when
3200 -- it freezes the entity.
3201
3202 -- This does not apply if the base type is a generic type, whose
3203 -- declaration is independent of the current derived definition.
3204
3205 if B /= T and then not Is_Generic_Type (B) then
3206 Ensure_Freeze_Node (B);
3207 Set_First_Subtype_Link (Freeze_Node (B), T);
3208 end if;
3209
3210 -- A type that is imported through a limited_with clause cannot
3211 -- generate any code, and thus need not be frozen. However, an access
3212 -- type with an imported designated type needs a finalization list,
3213 -- which may be referenced in some other package that has non-limited
3214 -- visibility on the designated type. Thus we must create the
3215 -- finalization list at the point the access type is frozen, to
3216 -- prevent unsatisfied references at link time.
3217
3218 if not From_Limited_With (T) or else Is_Access_Type (T) then
3219 Set_Has_Delayed_Freeze (T);
3220 end if;
3221 end;
3222
3223 -- Case where T is the full declaration of some private type which has
3224 -- been swapped in Defining_Identifier (N).
3225
3226 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3227 Process_Full_View (N, T, Def_Id);
3228
3229 -- Record the reference. The form of this is a little strange, since
3230 -- the full declaration has been swapped in. So the first parameter
3231 -- here represents the entity to which a reference is made which is
3232 -- the "real" entity, i.e. the one swapped in, and the second
3233 -- parameter provides the reference location.
3234
3235 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3236 -- since we don't want a complaint about the full type being an
3237 -- unwanted reference to the private type
3238
3239 declare
3240 B : constant Boolean := Has_Pragma_Unreferenced (T);
3241 begin
3242 Set_Has_Pragma_Unreferenced (T, False);
3243 Generate_Reference (T, T, 'c');
3244 Set_Has_Pragma_Unreferenced (T, B);
3245 end;
3246
3247 Set_Completion_Referenced (Def_Id);
3248
3249 -- For completion of incomplete type, process incomplete dependents
3250 -- and always mark the full type as referenced (it is the incomplete
3251 -- type that we get for any real reference).
3252
3253 elsif Ekind (Prev) = E_Incomplete_Type then
3254 Process_Incomplete_Dependents (N, T, Prev);
3255 Generate_Reference (Prev, Def_Id, 'c');
3256 Set_Completion_Referenced (Def_Id);
3257
3258 -- If not private type or incomplete type completion, this is a real
3259 -- definition of a new entity, so record it.
3260
3261 else
3262 Generate_Definition (Def_Id);
3263 end if;
3264
3265 -- Propagate any pending access types whose finalization masters need to
3266 -- be fully initialized from the partial to the full view. Guard against
3267 -- an illegal full view that remains unanalyzed.
3268
3269 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3270 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3271 end if;
3272
3273 if Chars (Scope (Def_Id)) = Name_System
3274 and then Chars (Def_Id) = Name_Address
3275 and then In_Predefined_Unit (N)
3276 then
3277 Set_Is_Descendant_Of_Address (Def_Id);
3278 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3279 Set_Is_Descendant_Of_Address (Prev);
3280 end if;
3281
3282 Set_Optimize_Alignment_Flags (Def_Id);
3283 Check_Eliminated (Def_Id);
3284
3285 -- If the declaration is a completion and aspects are present, apply
3286 -- them to the entity for the type which is currently the partial
3287 -- view, but which is the one that will be frozen.
3288
3289 if Has_Aspects (N) then
3290
3291 -- In most cases the partial view is a private type, and both views
3292 -- appear in different declarative parts. In the unusual case where
3293 -- the partial view is incomplete, perform the analysis on the
3294 -- full view, to prevent freezing anomalies with the corresponding
3295 -- class-wide type, which otherwise might be frozen before the
3296 -- dispatch table is built.
3297
3298 if Prev /= Def_Id
3299 and then Ekind (Prev) /= E_Incomplete_Type
3300 then
3301 Analyze_Aspect_Specifications (N, Prev);
3302
3303 -- Normal case
3304
3305 else
3306 Analyze_Aspect_Specifications (N, Def_Id);
3307 end if;
3308 end if;
3309
3310 if Is_Derived_Type (Prev)
3311 and then Def_Id /= Prev
3312 then
3313 Check_Nonoverridable_Aspects;
3314 end if;
3315 end Analyze_Full_Type_Declaration;
3316
3317 ----------------------------------
3318 -- Analyze_Incomplete_Type_Decl --
3319 ----------------------------------
3320
3321 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3322 F : constant Boolean := Is_Pure (Current_Scope);
3323 T : Entity_Id;
3324
3325 begin
3326 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3327
3328 Generate_Definition (Defining_Identifier (N));
3329
3330 -- Process an incomplete declaration. The identifier must not have been
3331 -- declared already in the scope. However, an incomplete declaration may
3332 -- appear in the private part of a package, for a private type that has
3333 -- already been declared.
3334
3335 -- In this case, the discriminants (if any) must match
3336
3337 T := Find_Type_Name (N);
3338
3339 Set_Ekind (T, E_Incomplete_Type);
3340 Init_Size_Align (T);
3341 Set_Is_First_Subtype (T, True);
3342 Set_Etype (T, T);
3343
3344 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3345 -- incomplete types.
3346
3347 if Tagged_Present (N) then
3348 Set_Is_Tagged_Type (T, True);
3349 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3350 Make_Class_Wide_Type (T);
3351 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3352 end if;
3353
3354 Set_Stored_Constraint (T, No_Elist);
3355
3356 if Present (Discriminant_Specifications (N)) then
3357 Push_Scope (T);
3358 Process_Discriminants (N);
3359 End_Scope;
3360 end if;
3361
3362 -- If the type has discriminants, nontrivial subtypes may be declared
3363 -- before the full view of the type. The full views of those subtypes
3364 -- will be built after the full view of the type.
3365
3366 Set_Private_Dependents (T, New_Elmt_List);
3367 Set_Is_Pure (T, F);
3368 end Analyze_Incomplete_Type_Decl;
3369
3370 -----------------------------------
3371 -- Analyze_Interface_Declaration --
3372 -----------------------------------
3373
3374 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3375 CW : constant Entity_Id := Class_Wide_Type (T);
3376
3377 begin
3378 Set_Is_Tagged_Type (T);
3379 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3380
3381 Set_Is_Limited_Record (T, Limited_Present (Def)
3382 or else Task_Present (Def)
3383 or else Protected_Present (Def)
3384 or else Synchronized_Present (Def));
3385
3386 -- Type is abstract if full declaration carries keyword, or if previous
3387 -- partial view did.
3388
3389 Set_Is_Abstract_Type (T);
3390 Set_Is_Interface (T);
3391
3392 -- Type is a limited interface if it includes the keyword limited, task,
3393 -- protected, or synchronized.
3394
3395 Set_Is_Limited_Interface
3396 (T, Limited_Present (Def)
3397 or else Protected_Present (Def)
3398 or else Synchronized_Present (Def)
3399 or else Task_Present (Def));
3400
3401 Set_Interfaces (T, New_Elmt_List);
3402 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3403
3404 -- Complete the decoration of the class-wide entity if it was already
3405 -- built (i.e. during the creation of the limited view)
3406
3407 if Present (CW) then
3408 Set_Is_Interface (CW);
3409 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3410 end if;
3411
3412 -- Check runtime support for synchronized interfaces
3413
3414 if (Is_Task_Interface (T)
3415 or else Is_Protected_Interface (T)
3416 or else Is_Synchronized_Interface (T))
3417 and then not RTE_Available (RE_Select_Specific_Data)
3418 then
3419 Error_Msg_CRT ("synchronized interfaces", T);
3420 end if;
3421 end Analyze_Interface_Declaration;
3422
3423 -----------------------------
3424 -- Analyze_Itype_Reference --
3425 -----------------------------
3426
3427 -- Nothing to do. This node is placed in the tree only for the benefit of
3428 -- back end processing, and has no effect on the semantic processing.
3429
3430 procedure Analyze_Itype_Reference (N : Node_Id) is
3431 begin
3432 pragma Assert (Is_Itype (Itype (N)));
3433 null;
3434 end Analyze_Itype_Reference;
3435
3436 --------------------------------
3437 -- Analyze_Number_Declaration --
3438 --------------------------------
3439
3440 procedure Analyze_Number_Declaration (N : Node_Id) is
3441 E : constant Node_Id := Expression (N);
3442 Id : constant Entity_Id := Defining_Identifier (N);
3443 Index : Interp_Index;
3444 It : Interp;
3445 T : Entity_Id;
3446
3447 begin
3448 Generate_Definition (Id);
3449 Enter_Name (Id);
3450
3451 -- This is an optimization of a common case of an integer literal
3452
3453 if Nkind (E) = N_Integer_Literal then
3454 Set_Is_Static_Expression (E, True);
3455 Set_Etype (E, Universal_Integer);
3456
3457 Set_Etype (Id, Universal_Integer);
3458 Set_Ekind (Id, E_Named_Integer);
3459 Set_Is_Frozen (Id, True);
3460 return;
3461 end if;
3462
3463 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3464
3465 -- Process expression, replacing error by integer zero, to avoid
3466 -- cascaded errors or aborts further along in the processing
3467
3468 -- Replace Error by integer zero, which seems least likely to cause
3469 -- cascaded errors.
3470
3471 if E = Error then
3472 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3473 Set_Error_Posted (E);
3474 end if;
3475
3476 Analyze (E);
3477
3478 -- Verify that the expression is static and numeric. If
3479 -- the expression is overloaded, we apply the preference
3480 -- rule that favors root numeric types.
3481
3482 if not Is_Overloaded (E) then
3483 T := Etype (E);
3484 if Has_Dynamic_Predicate_Aspect (T) then
3485 Error_Msg_N
3486 ("subtype has dynamic predicate, "
3487 & "not allowed in number declaration", N);
3488 end if;
3489
3490 else
3491 T := Any_Type;
3492
3493 Get_First_Interp (E, Index, It);
3494 while Present (It.Typ) loop
3495 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3496 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3497 then
3498 if T = Any_Type then
3499 T := It.Typ;
3500
3501 elsif It.Typ = Universal_Real
3502 or else
3503 It.Typ = Universal_Integer
3504 then
3505 -- Choose universal interpretation over any other
3506
3507 T := It.Typ;
3508 exit;
3509 end if;
3510 end if;
3511
3512 Get_Next_Interp (Index, It);
3513 end loop;
3514 end if;
3515
3516 if Is_Integer_Type (T) then
3517 Resolve (E, T);
3518 Set_Etype (Id, Universal_Integer);
3519 Set_Ekind (Id, E_Named_Integer);
3520
3521 elsif Is_Real_Type (T) then
3522
3523 -- Because the real value is converted to universal_real, this is a
3524 -- legal context for a universal fixed expression.
3525
3526 if T = Universal_Fixed then
3527 declare
3528 Loc : constant Source_Ptr := Sloc (N);
3529 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3530 Subtype_Mark =>
3531 New_Occurrence_Of (Universal_Real, Loc),
3532 Expression => Relocate_Node (E));
3533
3534 begin
3535 Rewrite (E, Conv);
3536 Analyze (E);
3537 end;
3538
3539 elsif T = Any_Fixed then
3540 Error_Msg_N ("illegal context for mixed mode operation", E);
3541
3542 -- Expression is of the form : universal_fixed * integer. Try to
3543 -- resolve as universal_real.
3544
3545 T := Universal_Real;
3546 Set_Etype (E, T);
3547 end if;
3548
3549 Resolve (E, T);
3550 Set_Etype (Id, Universal_Real);
3551 Set_Ekind (Id, E_Named_Real);
3552
3553 else
3554 Wrong_Type (E, Any_Numeric);
3555 Resolve (E, T);
3556
3557 Set_Etype (Id, T);
3558 Set_Ekind (Id, E_Constant);
3559 Set_Never_Set_In_Source (Id, True);
3560 Set_Is_True_Constant (Id, True);
3561 return;
3562 end if;
3563
3564 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3565 Set_Etype (E, Etype (Id));
3566 end if;
3567
3568 if not Is_OK_Static_Expression (E) then
3569 Flag_Non_Static_Expr
3570 ("non-static expression used in number declaration!", E);
3571 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3572 Set_Etype (E, Any_Type);
3573 end if;
3574
3575 Analyze_Dimension (N);
3576 end Analyze_Number_Declaration;
3577
3578 --------------------------------
3579 -- Analyze_Object_Declaration --
3580 --------------------------------
3581
3582 -- WARNING: This routine manages Ghost regions. Return statements must be
3583 -- replaced by gotos which jump to the end of the routine and restore the
3584 -- Ghost mode.
3585
3586 procedure Analyze_Object_Declaration (N : Node_Id) is
3587 Loc : constant Source_Ptr := Sloc (N);
3588 Id : constant Entity_Id := Defining_Identifier (N);
3589 Act_T : Entity_Id;
3590 T : Entity_Id;
3591
3592 E : Node_Id := Expression (N);
3593 -- E is set to Expression (N) throughout this routine. When Expression
3594 -- (N) is modified, E is changed accordingly.
3595
3596 Prev_Entity : Entity_Id := Empty;
3597
3598 procedure Check_Dynamic_Object (Typ : Entity_Id);
3599 -- A library-level object with non-static discriminant constraints may
3600 -- require dynamic allocation. The declaration is illegal if the
3601 -- profile includes the restriction No_Implicit_Heap_Allocations.
3602
3603 procedure Check_For_Null_Excluding_Components
3604 (Obj_Typ : Entity_Id;
3605 Obj_Decl : Node_Id);
3606 -- Verify that each null-excluding component of object declaration
3607 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3608 -- a compile-time warning if this is not the case.
3609
3610 function Count_Tasks (T : Entity_Id) return Uint;
3611 -- This function is called when a non-generic library level object of a
3612 -- task type is declared. Its function is to count the static number of
3613 -- tasks declared within the type (it is only called if Has_Task is set
3614 -- for T). As a side effect, if an array of tasks with non-static bounds
3615 -- or a variant record type is encountered, Check_Restriction is called
3616 -- indicating the count is unknown.
3617
3618 function Delayed_Aspect_Present return Boolean;
3619 -- If the declaration has an expression that is an aggregate, and it
3620 -- has aspects that require delayed analysis, the resolution of the
3621 -- aggregate must be deferred to the freeze point of the objet. This
3622 -- special processing was created for address clauses, but it must
3623 -- also apply to Alignment. This must be done before the aspect
3624 -- specifications are analyzed because we must handle the aggregate
3625 -- before the analysis of the object declaration is complete.
3626
3627 -- Any other relevant delayed aspects on object declarations ???
3628
3629 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3630 Comp : Entity_Id;
3631 Obj_Type : Entity_Id;
3632
3633 begin
3634 Obj_Type := Typ;
3635 if Is_Private_Type (Obj_Type)
3636 and then Present (Full_View (Obj_Type))
3637 then
3638 Obj_Type := Full_View (Obj_Type);
3639 end if;
3640
3641 if Known_Static_Esize (Obj_Type) then
3642 return;
3643 end if;
3644
3645 if Restriction_Active (No_Implicit_Heap_Allocations)
3646 and then Expander_Active
3647 and then Has_Discriminants (Obj_Type)
3648 then
3649 Comp := First_Component (Obj_Type);
3650 while Present (Comp) loop
3651 if Known_Static_Esize (Etype (Comp))
3652 or else Size_Known_At_Compile_Time (Etype (Comp))
3653 then
3654 null;
3655
3656 elsif not Discriminated_Size (Comp)
3657 and then Comes_From_Source (Comp)
3658 then
3659 Error_Msg_NE ("component& of non-static size will violate "
3660 & "restriction No_Implicit_Heap_Allocation?", N, Comp);
3661
3662 elsif Is_Record_Type (Etype (Comp)) then
3663 Check_Dynamic_Object (Etype (Comp));
3664 end if;
3665 Next_Component (Comp);
3666 end loop;
3667 end if;
3668 end Check_Dynamic_Object;
3669
3670 -----------------------------------------
3671 -- Check_For_Null_Excluding_Components --
3672 -----------------------------------------
3673
3674 procedure Check_For_Null_Excluding_Components
3675 (Obj_Typ : Entity_Id;
3676 Obj_Decl : Node_Id)
3677 is
3678 procedure Check_Component
3679 (Comp_Typ : Entity_Id;
3680 Comp_Decl : Node_Id := Empty;
3681 Array_Comp : Boolean := False);
3682 -- Apply a compile-time null-exclusion check on a component denoted
3683 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3684 -- subcomponents (if any).
3685
3686 ---------------------
3687 -- Check_Component --
3688 ---------------------
3689
3690 procedure Check_Component
3691 (Comp_Typ : Entity_Id;
3692 Comp_Decl : Node_Id := Empty;
3693 Array_Comp : Boolean := False)
3694 is
3695 Comp : Entity_Id;
3696 T : Entity_Id;
3697
3698 begin
3699 -- Do not consider internally-generated components or those that
3700 -- are already initialized.
3701
3702 if Present (Comp_Decl)
3703 and then (not Comes_From_Source (Comp_Decl)
3704 or else Present (Expression (Comp_Decl)))
3705 then
3706 return;
3707 end if;
3708
3709 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3710 and then Present (Full_View (Comp_Typ))
3711 then
3712 T := Full_View (Comp_Typ);
3713 else
3714 T := Comp_Typ;
3715 end if;
3716
3717 -- Verify a component of a null-excluding access type
3718
3719 if Is_Access_Type (T)
3720 and then Can_Never_Be_Null (T)
3721 then
3722 if Comp_Decl = Obj_Decl then
3723 Null_Exclusion_Static_Checks (Obj_Decl, Empty, Array_Comp);
3724 else
3725 Null_Exclusion_Static_Checks
3726 (Obj_Decl, Comp_Decl, Array_Comp);
3727 end if;
3728
3729 -- Check array components
3730
3731 elsif Is_Array_Type (T) then
3732
3733 -- There is no suitable component when the object is of an
3734 -- array type. However, a namable component may appear at some
3735 -- point during the recursive inspection, but not at the top
3736 -- level. At the top level just indicate array component case.
3737
3738 if Comp_Decl = Obj_Decl then
3739 Check_Component (Component_Type (T), Array_Comp => True);
3740 else
3741 Check_Component (Component_Type (T), Comp_Decl);
3742 end if;
3743
3744 -- Verify all components of type T
3745
3746 -- Note: No checks are performed on types with discriminants due
3747 -- to complexities involving variants. ???
3748
3749 elsif (Is_Concurrent_Type (T)
3750 or else Is_Incomplete_Or_Private_Type (T)
3751 or else Is_Record_Type (T))
3752 and then not Has_Discriminants (T)
3753 then
3754 Comp := First_Component (T);
3755 while Present (Comp) loop
3756 Check_Component (Etype (Comp), Parent (Comp));
3757
3758 Comp := Next_Component (Comp);
3759 end loop;
3760 end if;
3761 end Check_Component;
3762
3763 -- Start processing for Check_For_Null_Excluding_Components
3764
3765 begin
3766 Check_Component (Obj_Typ, Obj_Decl);
3767 end Check_For_Null_Excluding_Components;
3768
3769 -----------------
3770 -- Count_Tasks --
3771 -----------------
3772
3773 function Count_Tasks (T : Entity_Id) return Uint is
3774 C : Entity_Id;
3775 X : Node_Id;
3776 V : Uint;
3777
3778 begin
3779 if Is_Task_Type (T) then
3780 return Uint_1;
3781
3782 elsif Is_Record_Type (T) then
3783 if Has_Discriminants (T) then
3784 Check_Restriction (Max_Tasks, N);
3785 return Uint_0;
3786
3787 else
3788 V := Uint_0;
3789 C := First_Component (T);
3790 while Present (C) loop
3791 V := V + Count_Tasks (Etype (C));
3792 Next_Component (C);
3793 end loop;
3794
3795 return V;
3796 end if;
3797
3798 elsif Is_Array_Type (T) then
3799 X := First_Index (T);
3800 V := Count_Tasks (Component_Type (T));
3801 while Present (X) loop
3802 C := Etype (X);
3803
3804 if not Is_OK_Static_Subtype (C) then
3805 Check_Restriction (Max_Tasks, N);
3806 return Uint_0;
3807 else
3808 V := V * (UI_Max (Uint_0,
3809 Expr_Value (Type_High_Bound (C)) -
3810 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3811 end if;
3812
3813 Next_Index (X);
3814 end loop;
3815
3816 return V;
3817
3818 else
3819 return Uint_0;
3820 end if;
3821 end Count_Tasks;
3822
3823 ----------------------------
3824 -- Delayed_Aspect_Present --
3825 ----------------------------
3826
3827 function Delayed_Aspect_Present return Boolean is
3828 A : Node_Id;
3829 A_Id : Aspect_Id;
3830
3831 begin
3832 if Present (Aspect_Specifications (N)) then
3833 A := First (Aspect_Specifications (N));
3834 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3835 while Present (A) loop
3836 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3837 return True;
3838 end if;
3839
3840 Next (A);
3841 end loop;
3842 end if;
3843
3844 return False;
3845 end Delayed_Aspect_Present;
3846
3847 -- Local variables
3848
3849 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3850 -- Save the Ghost mode to restore on exit
3851
3852 Related_Id : Entity_Id;
3853
3854 -- Start of processing for Analyze_Object_Declaration
3855
3856 begin
3857 -- There are three kinds of implicit types generated by an
3858 -- object declaration:
3859
3860 -- 1. Those generated by the original Object Definition
3861
3862 -- 2. Those generated by the Expression
3863
3864 -- 3. Those used to constrain the Object Definition with the
3865 -- expression constraints when the definition is unconstrained.
3866
3867 -- They must be generated in this order to avoid order of elaboration
3868 -- issues. Thus the first step (after entering the name) is to analyze
3869 -- the object definition.
3870
3871 if Constant_Present (N) then
3872 Prev_Entity := Current_Entity_In_Scope (Id);
3873
3874 if Present (Prev_Entity)
3875 and then
3876 -- If the homograph is an implicit subprogram, it is overridden
3877 -- by the current declaration.
3878
3879 ((Is_Overloadable (Prev_Entity)
3880 and then Is_Inherited_Operation (Prev_Entity))
3881
3882 -- The current object is a discriminal generated for an entry
3883 -- family index. Even though the index is a constant, in this
3884 -- particular context there is no true constant redeclaration.
3885 -- Enter_Name will handle the visibility.
3886
3887 or else
3888 (Is_Discriminal (Id)
3889 and then Ekind (Discriminal_Link (Id)) =
3890 E_Entry_Index_Parameter)
3891
3892 -- The current object is the renaming for a generic declared
3893 -- within the instance.
3894
3895 or else
3896 (Ekind (Prev_Entity) = E_Package
3897 and then Nkind (Parent (Prev_Entity)) =
3898 N_Package_Renaming_Declaration
3899 and then not Comes_From_Source (Prev_Entity)
3900 and then
3901 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3902
3903 -- The entity may be a homonym of a private component of the
3904 -- enclosing protected object, for which we create a local
3905 -- renaming declaration. The declaration is legal, even if
3906 -- useless when it just captures that component.
3907
3908 or else
3909 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3910 and then Nkind (Parent (Prev_Entity)) =
3911 N_Object_Renaming_Declaration))
3912 then
3913 Prev_Entity := Empty;
3914 end if;
3915 end if;
3916
3917 if Present (Prev_Entity) then
3918
3919 -- The object declaration is Ghost when it completes a deferred Ghost
3920 -- constant.
3921
3922 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3923
3924 Constant_Redeclaration (Id, N, T);
3925
3926 Generate_Reference (Prev_Entity, Id, 'c');
3927 Set_Completion_Referenced (Id);
3928
3929 if Error_Posted (N) then
3930
3931 -- Type mismatch or illegal redeclaration; do not analyze
3932 -- expression to avoid cascaded errors.
3933
3934 T := Find_Type_Of_Object (Object_Definition (N), N);
3935 Set_Etype (Id, T);
3936 Set_Ekind (Id, E_Variable);
3937 goto Leave;
3938 end if;
3939
3940 -- In the normal case, enter identifier at the start to catch premature
3941 -- usage in the initialization expression.
3942
3943 else
3944 Generate_Definition (Id);
3945 Enter_Name (Id);
3946
3947 Mark_Coextensions (N, Object_Definition (N));
3948
3949 T := Find_Type_Of_Object (Object_Definition (N), N);
3950
3951 if Nkind (Object_Definition (N)) = N_Access_Definition
3952 and then Present
3953 (Access_To_Subprogram_Definition (Object_Definition (N)))
3954 and then Protected_Present
3955 (Access_To_Subprogram_Definition (Object_Definition (N)))
3956 then
3957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3958 end if;
3959
3960 if Error_Posted (Id) then
3961 Set_Etype (Id, T);
3962 Set_Ekind (Id, E_Variable);
3963 goto Leave;
3964 end if;
3965 end if;
3966
3967 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3968 -- out some static checks.
3969
3970 if Ada_Version >= Ada_2005 then
3971
3972 -- In case of aggregates we must also take care of the correct
3973 -- initialization of nested aggregates bug this is done at the
3974 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3975
3976 if Can_Never_Be_Null (T) then
3977 if Present (Expression (N))
3978 and then Nkind (Expression (N)) = N_Aggregate
3979 then
3980 null;
3981
3982 else
3983 declare
3984 Save_Typ : constant Entity_Id := Etype (Id);
3985 begin
3986 Set_Etype (Id, T); -- Temp. decoration for static checks
3987 Null_Exclusion_Static_Checks (N);
3988 Set_Etype (Id, Save_Typ);
3989 end;
3990 end if;
3991
3992 -- We might be dealing with an object of a composite type containing
3993 -- null-excluding components without an aggregate, so we must verify
3994 -- that such components have default initialization.
3995
3996 else
3997 Check_For_Null_Excluding_Components (T, N);
3998 end if;
3999 end if;
4000
4001 -- Object is marked pure if it is in a pure scope
4002
4003 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4004
4005 -- If deferred constant, make sure context is appropriate. We detect
4006 -- a deferred constant as a constant declaration with no expression.
4007 -- A deferred constant can appear in a package body if its completion
4008 -- is by means of an interface pragma.
4009
4010 if Constant_Present (N) and then No (E) then
4011
4012 -- A deferred constant may appear in the declarative part of the
4013 -- following constructs:
4014
4015 -- blocks
4016 -- entry bodies
4017 -- extended return statements
4018 -- package specs
4019 -- package bodies
4020 -- subprogram bodies
4021 -- task bodies
4022
4023 -- When declared inside a package spec, a deferred constant must be
4024 -- completed by a full constant declaration or pragma Import. In all
4025 -- other cases, the only proper completion is pragma Import. Extended
4026 -- return statements are flagged as invalid contexts because they do
4027 -- not have a declarative part and so cannot accommodate the pragma.
4028
4029 if Ekind (Current_Scope) = E_Return_Statement then
4030 Error_Msg_N
4031 ("invalid context for deferred constant declaration (RM 7.4)",
4032 N);
4033 Error_Msg_N
4034 ("\declaration requires an initialization expression",
4035 N);
4036 Set_Constant_Present (N, False);
4037
4038 -- In Ada 83, deferred constant must be of private type
4039
4040 elsif not Is_Private_Type (T) then
4041 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4042 Error_Msg_N
4043 ("(Ada 83) deferred constant must be private type", N);
4044 end if;
4045 end if;
4046
4047 -- If not a deferred constant, then the object declaration freezes
4048 -- its type, unless the object is of an anonymous type and has delayed
4049 -- aspects. In that case the type is frozen when the object itself is.
4050
4051 else
4052 Check_Fully_Declared (T, N);
4053
4054 if Has_Delayed_Aspects (Id)
4055 and then Is_Array_Type (T)
4056 and then Is_Itype (T)
4057 then
4058 Set_Has_Delayed_Freeze (T);
4059 else
4060 Freeze_Before (N, T);
4061 end if;
4062 end if;
4063
4064 -- If the object was created by a constrained array definition, then
4065 -- set the link in both the anonymous base type and anonymous subtype
4066 -- that are built to represent the array type to point to the object.
4067
4068 if Nkind (Object_Definition (Declaration_Node (Id))) =
4069 N_Constrained_Array_Definition
4070 then
4071 Set_Related_Array_Object (T, Id);
4072 Set_Related_Array_Object (Base_Type (T), Id);
4073 end if;
4074
4075 -- Special checks for protected objects not at library level
4076
4077 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4078 Check_Restriction (No_Local_Protected_Objects, Id);
4079
4080 -- Protected objects with interrupt handlers must be at library level
4081
4082 -- Ada 2005: This test is not needed (and the corresponding clause
4083 -- in the RM is removed) because accessibility checks are sufficient
4084 -- to make handlers not at the library level illegal.
4085
4086 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4087 -- applies to the '95 version of the language as well.
4088
4089 if Is_Protected_Type (T)
4090 and then Has_Interrupt_Handler (T)
4091 and then Ada_Version < Ada_95
4092 then
4093 Error_Msg_N
4094 ("interrupt object can only be declared at library level", Id);
4095 end if;
4096 end if;
4097
4098 -- Check for violation of No_Local_Timing_Events
4099
4100 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4101 Check_Restriction (No_Local_Timing_Events, Id);
4102 end if;
4103
4104 -- The actual subtype of the object is the nominal subtype, unless
4105 -- the nominal one is unconstrained and obtained from the expression.
4106
4107 Act_T := T;
4108
4109 -- These checks should be performed before the initialization expression
4110 -- is considered, so that the Object_Definition node is still the same
4111 -- as in source code.
4112
4113 -- In SPARK, the nominal subtype is always given by a subtype mark
4114 -- and must not be unconstrained. (The only exception to this is the
4115 -- acceptance of declarations of constants of type String.)
4116
4117 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4118 then
4119 Check_SPARK_05_Restriction
4120 ("subtype mark required", Object_Definition (N));
4121
4122 elsif Is_Array_Type (T)
4123 and then not Is_Constrained (T)
4124 and then T /= Standard_String
4125 then
4126 Check_SPARK_05_Restriction
4127 ("subtype mark of constrained type expected",
4128 Object_Definition (N));
4129 end if;
4130
4131 if Is_Library_Level_Entity (Id) then
4132 Check_Dynamic_Object (T);
4133 end if;
4134
4135 -- There are no aliased objects in SPARK
4136
4137 if Aliased_Present (N) then
4138 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4139 end if;
4140
4141 -- Process initialization expression if present and not in error
4142
4143 if Present (E) and then E /= Error then
4144
4145 -- Generate an error in case of CPP class-wide object initialization.
4146 -- Required because otherwise the expansion of the class-wide
4147 -- assignment would try to use 'size to initialize the object
4148 -- (primitive that is not available in CPP tagged types).
4149
4150 if Is_Class_Wide_Type (Act_T)
4151 and then
4152 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4153 or else
4154 (Present (Full_View (Root_Type (Etype (Act_T))))
4155 and then
4156 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4157 then
4158 Error_Msg_N
4159 ("predefined assignment not available for 'C'P'P tagged types",
4160 E);
4161 end if;
4162
4163 Mark_Coextensions (N, E);
4164 Analyze (E);
4165
4166 -- In case of errors detected in the analysis of the expression,
4167 -- decorate it with the expected type to avoid cascaded errors
4168
4169 if No (Etype (E)) then
4170 Set_Etype (E, T);
4171 end if;
4172
4173 -- If an initialization expression is present, then we set the
4174 -- Is_True_Constant flag. It will be reset if this is a variable
4175 -- and it is indeed modified.
4176
4177 Set_Is_True_Constant (Id, True);
4178
4179 -- If we are analyzing a constant declaration, set its completion
4180 -- flag after analyzing and resolving the expression.
4181
4182 if Constant_Present (N) then
4183 Set_Has_Completion (Id);
4184 end if;
4185
4186 -- Set type and resolve (type may be overridden later on). Note:
4187 -- Ekind (Id) must still be E_Void at this point so that incorrect
4188 -- early usage within E is properly diagnosed.
4189
4190 Set_Etype (Id, T);
4191
4192 -- If the expression is an aggregate we must look ahead to detect
4193 -- the possible presence of an address clause, and defer resolution
4194 -- and expansion of the aggregate to the freeze point of the entity.
4195
4196 -- This is not always legal because the aggregate may contain other
4197 -- references that need freezing, e.g. references to other entities
4198 -- with address clauses. In any case, when compiling with -gnatI the
4199 -- presence of the address clause must be ignored.
4200
4201 if Comes_From_Source (N)
4202 and then Expander_Active
4203 and then Nkind (E) = N_Aggregate
4204 and then
4205 ((Present (Following_Address_Clause (N))
4206 and then not Ignore_Rep_Clauses)
4207 or else Delayed_Aspect_Present)
4208 then
4209 Set_Etype (E, T);
4210
4211 else
4212 Resolve (E, T);
4213 end if;
4214
4215 -- No further action needed if E is a call to an inlined function
4216 -- which returns an unconstrained type and it has been expanded into
4217 -- a procedure call. In that case N has been replaced by an object
4218 -- declaration without initializing expression and it has been
4219 -- analyzed (see Expand_Inlined_Call).
4220
4221 if Back_End_Inlining
4222 and then Expander_Active
4223 and then Nkind (E) = N_Function_Call
4224 and then Nkind (Name (E)) in N_Has_Entity
4225 and then Is_Inlined (Entity (Name (E)))
4226 and then not Is_Constrained (Etype (E))
4227 and then Analyzed (N)
4228 and then No (Expression (N))
4229 then
4230 goto Leave;
4231 end if;
4232
4233 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4234 -- node (which was marked already-analyzed), we need to set the type
4235 -- to something other than Any_Access in order to keep gigi happy.
4236
4237 if Etype (E) = Any_Access then
4238 Set_Etype (E, T);
4239 end if;
4240
4241 -- If the object is an access to variable, the initialization
4242 -- expression cannot be an access to constant.
4243
4244 if Is_Access_Type (T)
4245 and then not Is_Access_Constant (T)
4246 and then Is_Access_Type (Etype (E))
4247 and then Is_Access_Constant (Etype (E))
4248 then
4249 Error_Msg_N
4250 ("access to variable cannot be initialized with an "
4251 & "access-to-constant expression", E);
4252 end if;
4253
4254 if not Assignment_OK (N) then
4255 Check_Initialization (T, E);
4256 end if;
4257
4258 Check_Unset_Reference (E);
4259
4260 -- If this is a variable, then set current value. If this is a
4261 -- declared constant of a scalar type with a static expression,
4262 -- indicate that it is always valid.
4263
4264 if not Constant_Present (N) then
4265 if Compile_Time_Known_Value (E) then
4266 Set_Current_Value (Id, E);
4267 end if;
4268
4269 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4270 Set_Is_Known_Valid (Id);
4271 end if;
4272
4273 -- Deal with setting of null flags
4274
4275 if Is_Access_Type (T) then
4276 if Known_Non_Null (E) then
4277 Set_Is_Known_Non_Null (Id, True);
4278 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4279 Set_Is_Known_Null (Id, True);
4280 end if;
4281 end if;
4282
4283 -- Check incorrect use of dynamically tagged expressions
4284
4285 if Is_Tagged_Type (T) then
4286 Check_Dynamically_Tagged_Expression
4287 (Expr => E,
4288 Typ => T,
4289 Related_Nod => N);
4290 end if;
4291
4292 Apply_Scalar_Range_Check (E, T);
4293 Apply_Static_Length_Check (E, T);
4294
4295 if Nkind (Original_Node (N)) = N_Object_Declaration
4296 and then Comes_From_Source (Original_Node (N))
4297
4298 -- Only call test if needed
4299
4300 and then Restriction_Check_Required (SPARK_05)
4301 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4302 then
4303 Check_SPARK_05_Restriction
4304 ("initialization expression is not appropriate", E);
4305 end if;
4306
4307 -- A formal parameter of a specific tagged type whose related
4308 -- subprogram is subject to pragma Extensions_Visible with value
4309 -- "False" cannot be implicitly converted to a class-wide type by
4310 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4311 -- not consider internally generated expressions.
4312
4313 if Is_Class_Wide_Type (T)
4314 and then Comes_From_Source (E)
4315 and then Is_EVF_Expression (E)
4316 then
4317 Error_Msg_N
4318 ("formal parameter cannot be implicitly converted to "
4319 & "class-wide type when Extensions_Visible is False", E);
4320 end if;
4321 end if;
4322
4323 -- If the No_Streams restriction is set, check that the type of the
4324 -- object is not, and does not contain, any subtype derived from
4325 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4326 -- Has_Stream just for efficiency reasons. There is no point in
4327 -- spending time on a Has_Stream check if the restriction is not set.
4328
4329 if Restriction_Check_Required (No_Streams) then
4330 if Has_Stream (T) then
4331 Check_Restriction (No_Streams, N);
4332 end if;
4333 end if;
4334
4335 -- Deal with predicate check before we start to do major rewriting. It
4336 -- is OK to initialize and then check the initialized value, since the
4337 -- object goes out of scope if we get a predicate failure. Note that we
4338 -- do this in the analyzer and not the expander because the analyzer
4339 -- does some substantial rewriting in some cases.
4340
4341 -- We need a predicate check if the type has predicates that are not
4342 -- ignored, and if either there is an initializing expression, or for
4343 -- default initialization when we have at least one case of an explicit
4344 -- default initial value and then this is not an internal declaration
4345 -- whose initialization comes later (as for an aggregate expansion).
4346
4347 if not Suppress_Assignment_Checks (N)
4348 and then Present (Predicate_Function (T))
4349 and then not Predicates_Ignored (T)
4350 and then not No_Initialization (N)
4351 and then
4352 (Present (E)
4353 or else
4354 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4355 then
4356 -- If the type has a static predicate and the expression is known at
4357 -- compile time, see if the expression satisfies the predicate.
4358
4359 if Present (E) then
4360 Check_Expression_Against_Static_Predicate (E, T);
4361 end if;
4362
4363 -- If the type is a null record and there is no explicit initial
4364 -- expression, no predicate check applies.
4365
4366 if No (E) and then Is_Null_Record_Type (T) then
4367 null;
4368
4369 -- Do not generate a predicate check if the initialization expression
4370 -- is a type conversion because the conversion has been subjected to
4371 -- the same check. This is a small optimization which avoid redundant
4372 -- checks.
4373
4374 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4375 null;
4376
4377 else
4378 Insert_After (N,
4379 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4380 end if;
4381 end if;
4382
4383 -- Case of unconstrained type
4384
4385 if not Is_Definite_Subtype (T) then
4386
4387 -- In SPARK, a declaration of unconstrained type is allowed
4388 -- only for constants of type string.
4389
4390 if Is_String_Type (T) and then not Constant_Present (N) then
4391 Check_SPARK_05_Restriction
4392 ("declaration of object of unconstrained type not allowed", N);
4393 end if;
4394
4395 -- Nothing to do in deferred constant case
4396
4397 if Constant_Present (N) and then No (E) then
4398 null;
4399
4400 -- Case of no initialization present
4401
4402 elsif No (E) then
4403 if No_Initialization (N) then
4404 null;
4405
4406 elsif Is_Class_Wide_Type (T) then
4407 Error_Msg_N
4408 ("initialization required in class-wide declaration ", N);
4409
4410 else
4411 Error_Msg_N
4412 ("unconstrained subtype not allowed (need initialization)",
4413 Object_Definition (N));
4414
4415 if Is_Record_Type (T) and then Has_Discriminants (T) then
4416 Error_Msg_N
4417 ("\provide initial value or explicit discriminant values",
4418 Object_Definition (N));
4419
4420 Error_Msg_NE
4421 ("\or give default discriminant values for type&",
4422 Object_Definition (N), T);
4423
4424 elsif Is_Array_Type (T) then
4425 Error_Msg_N
4426 ("\provide initial value or explicit array bounds",
4427 Object_Definition (N));
4428 end if;
4429 end if;
4430
4431 -- Case of initialization present but in error. Set initial
4432 -- expression as absent (but do not make above complaints)
4433
4434 elsif E = Error then
4435 Set_Expression (N, Empty);
4436 E := Empty;
4437
4438 -- Case of initialization present
4439
4440 else
4441 -- Check restrictions in Ada 83
4442
4443 if not Constant_Present (N) then
4444
4445 -- Unconstrained variables not allowed in Ada 83 mode
4446
4447 if Ada_Version = Ada_83
4448 and then Comes_From_Source (Object_Definition (N))
4449 then
4450 Error_Msg_N
4451 ("(Ada 83) unconstrained variable not allowed",
4452 Object_Definition (N));
4453 end if;
4454 end if;
4455
4456 -- Now we constrain the variable from the initializing expression
4457
4458 -- If the expression is an aggregate, it has been expanded into
4459 -- individual assignments. Retrieve the actual type from the
4460 -- expanded construct.
4461
4462 if Is_Array_Type (T)
4463 and then No_Initialization (N)
4464 and then Nkind (Original_Node (E)) = N_Aggregate
4465 then
4466 Act_T := Etype (E);
4467
4468 -- In case of class-wide interface object declarations we delay
4469 -- the generation of the equivalent record type declarations until
4470 -- its expansion because there are cases in they are not required.
4471
4472 elsif Is_Interface (T) then
4473 null;
4474
4475 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4476 -- we should prevent the generation of another Itype with the
4477 -- same name as the one already generated, or we end up with
4478 -- two identical types in GNATprove.
4479
4480 elsif GNATprove_Mode then
4481 null;
4482
4483 -- If the type is an unchecked union, no subtype can be built from
4484 -- the expression. Rewrite declaration as a renaming, which the
4485 -- back-end can handle properly. This is a rather unusual case,
4486 -- because most unchecked_union declarations have default values
4487 -- for discriminants and are thus not indefinite.
4488
4489 elsif Is_Unchecked_Union (T) then
4490 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4491 Set_Ekind (Id, E_Constant);
4492 else
4493 Set_Ekind (Id, E_Variable);
4494 end if;
4495
4496 Rewrite (N,
4497 Make_Object_Renaming_Declaration (Loc,
4498 Defining_Identifier => Id,
4499 Subtype_Mark => New_Occurrence_Of (T, Loc),
4500 Name => E));
4501
4502 Set_Renamed_Object (Id, E);
4503 Freeze_Before (N, T);
4504 Set_Is_Frozen (Id);
4505 goto Leave;
4506
4507 else
4508 -- Ensure that the generated subtype has a unique external name
4509 -- when the related object is public. This guarantees that the
4510 -- subtype and its bounds will not be affected by switches or
4511 -- pragmas that may offset the internal counter due to extra
4512 -- generated code.
4513
4514 if Is_Public (Id) then
4515 Related_Id := Id;
4516 else
4517 Related_Id := Empty;
4518 end if;
4519
4520 Expand_Subtype_From_Expr
4521 (N => N,
4522 Unc_Type => T,
4523 Subtype_Indic => Object_Definition (N),
4524 Exp => E,
4525 Related_Id => Related_Id);
4526
4527 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4528 end if;
4529
4530 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4531
4532 if Aliased_Present (N) then
4533 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4534 end if;
4535
4536 Freeze_Before (N, Act_T);
4537 Freeze_Before (N, T);
4538 end if;
4539
4540 elsif Is_Array_Type (T)
4541 and then No_Initialization (N)
4542 and then (Nkind (Original_Node (E)) = N_Aggregate
4543 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4544 and then Nkind (Original_Node (Expression
4545 (Original_Node (E)))) = N_Aggregate))
4546 then
4547 if not Is_Entity_Name (Object_Definition (N)) then
4548 Act_T := Etype (E);
4549 Check_Compile_Time_Size (Act_T);
4550
4551 if Aliased_Present (N) then
4552 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4553 end if;
4554 end if;
4555
4556 -- When the given object definition and the aggregate are specified
4557 -- independently, and their lengths might differ do a length check.
4558 -- This cannot happen if the aggregate is of the form (others =>...)
4559
4560 if not Is_Constrained (T) then
4561 null;
4562
4563 elsif Nkind (E) = N_Raise_Constraint_Error then
4564
4565 -- Aggregate is statically illegal. Place back in declaration
4566
4567 Set_Expression (N, E);
4568 Set_No_Initialization (N, False);
4569
4570 elsif T = Etype (E) then
4571 null;
4572
4573 elsif Nkind (E) = N_Aggregate
4574 and then Present (Component_Associations (E))
4575 and then Present (Choice_List (First (Component_Associations (E))))
4576 and then
4577 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4578 N_Others_Choice
4579 then
4580 null;
4581
4582 else
4583 Apply_Length_Check (E, T);
4584 end if;
4585
4586 -- If the type is limited unconstrained with defaulted discriminants and
4587 -- there is no expression, then the object is constrained by the
4588 -- defaults, so it is worthwhile building the corresponding subtype.
4589
4590 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4591 and then not Is_Constrained (T)
4592 and then Has_Discriminants (T)
4593 then
4594 if No (E) then
4595 Act_T := Build_Default_Subtype (T, N);
4596 else
4597 -- Ada 2005: A limited object may be initialized by means of an
4598 -- aggregate. If the type has default discriminants it has an
4599 -- unconstrained nominal type, Its actual subtype will be obtained
4600 -- from the aggregate, and not from the default discriminants.
4601
4602 Act_T := Etype (E);
4603 end if;
4604
4605 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4606
4607 elsif Nkind (E) = N_Function_Call
4608 and then Constant_Present (N)
4609 and then Has_Unconstrained_Elements (Etype (E))
4610 then
4611 -- The back-end has problems with constants of a discriminated type
4612 -- with defaults, if the initial value is a function call. We
4613 -- generate an intermediate temporary that will receive a reference
4614 -- to the result of the call. The initialization expression then
4615 -- becomes a dereference of that temporary.
4616
4617 Remove_Side_Effects (E);
4618
4619 -- If this is a constant declaration of an unconstrained type and
4620 -- the initialization is an aggregate, we can use the subtype of the
4621 -- aggregate for the declared entity because it is immutable.
4622
4623 elsif not Is_Constrained (T)
4624 and then Has_Discriminants (T)
4625 and then Constant_Present (N)
4626 and then not Has_Unchecked_Union (T)
4627 and then Nkind (E) = N_Aggregate
4628 then
4629 Act_T := Etype (E);
4630 end if;
4631
4632 -- Check No_Wide_Characters restriction
4633
4634 Check_Wide_Character_Restriction (T, Object_Definition (N));
4635
4636 -- Indicate this is not set in source. Certainly true for constants, and
4637 -- true for variables so far (will be reset for a variable if and when
4638 -- we encounter a modification in the source).
4639
4640 Set_Never_Set_In_Source (Id);
4641
4642 -- Now establish the proper kind and type of the object
4643
4644 if Constant_Present (N) then
4645 Set_Ekind (Id, E_Constant);
4646 Set_Is_True_Constant (Id);
4647
4648 else
4649 Set_Ekind (Id, E_Variable);
4650
4651 -- A variable is set as shared passive if it appears in a shared
4652 -- passive package, and is at the outer level. This is not done for
4653 -- entities generated during expansion, because those are always
4654 -- manipulated locally.
4655
4656 if Is_Shared_Passive (Current_Scope)
4657 and then Is_Library_Level_Entity (Id)
4658 and then Comes_From_Source (Id)
4659 then
4660 Set_Is_Shared_Passive (Id);
4661 Check_Shared_Var (Id, T, N);
4662 end if;
4663
4664 -- Set Has_Initial_Value if initializing expression present. Note
4665 -- that if there is no initializing expression, we leave the state
4666 -- of this flag unchanged (usually it will be False, but notably in
4667 -- the case of exception choice variables, it will already be true).
4668
4669 if Present (E) then
4670 Set_Has_Initial_Value (Id);
4671 end if;
4672 end if;
4673
4674 -- Initialize alignment and size and capture alignment setting
4675
4676 Init_Alignment (Id);
4677 Init_Esize (Id);
4678 Set_Optimize_Alignment_Flags (Id);
4679
4680 -- Deal with aliased case
4681
4682 if Aliased_Present (N) then
4683 Set_Is_Aliased (Id);
4684
4685 -- If the object is aliased and the type is unconstrained with
4686 -- defaulted discriminants and there is no expression, then the
4687 -- object is constrained by the defaults, so it is worthwhile
4688 -- building the corresponding subtype.
4689
4690 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4691 -- unconstrained, then only establish an actual subtype if the
4692 -- nominal subtype is indefinite. In definite cases the object is
4693 -- unconstrained in Ada 2005.
4694
4695 if No (E)
4696 and then Is_Record_Type (T)
4697 and then not Is_Constrained (T)
4698 and then Has_Discriminants (T)
4699 and then (Ada_Version < Ada_2005
4700 or else not Is_Definite_Subtype (T))
4701 then
4702 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4703 end if;
4704 end if;
4705
4706 -- Now we can set the type of the object
4707
4708 Set_Etype (Id, Act_T);
4709
4710 -- Non-constant object is marked to be treated as volatile if type is
4711 -- volatile and we clear the Current_Value setting that may have been
4712 -- set above. Doing so for constants isn't required and might interfere
4713 -- with possible uses of the object as a static expression in contexts
4714 -- incompatible with volatility (e.g. as a case-statement alternative).
4715
4716 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4717 Set_Treat_As_Volatile (Id);
4718 Set_Current_Value (Id, Empty);
4719 end if;
4720
4721 -- Deal with controlled types
4722
4723 if Has_Controlled_Component (Etype (Id))
4724 or else Is_Controlled (Etype (Id))
4725 then
4726 if not Is_Library_Level_Entity (Id) then
4727 Check_Restriction (No_Nested_Finalization, N);
4728 else
4729 Validate_Controlled_Object (Id);
4730 end if;
4731 end if;
4732
4733 if Has_Task (Etype (Id)) then
4734 Check_Restriction (No_Tasking, N);
4735
4736 -- Deal with counting max tasks
4737
4738 -- Nothing to do if inside a generic
4739
4740 if Inside_A_Generic then
4741 null;
4742
4743 -- If library level entity, then count tasks
4744
4745 elsif Is_Library_Level_Entity (Id) then
4746 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4747
4748 -- If not library level entity, then indicate we don't know max
4749 -- tasks and also check task hierarchy restriction and blocking
4750 -- operation (since starting a task is definitely blocking).
4751
4752 else
4753 Check_Restriction (Max_Tasks, N);
4754 Check_Restriction (No_Task_Hierarchy, N);
4755 Check_Potentially_Blocking_Operation (N);
4756 end if;
4757
4758 -- A rather specialized test. If we see two tasks being declared
4759 -- of the same type in the same object declaration, and the task
4760 -- has an entry with an address clause, we know that program error
4761 -- will be raised at run time since we can't have two tasks with
4762 -- entries at the same address.
4763
4764 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4765 declare
4766 E : Entity_Id;
4767
4768 begin
4769 E := First_Entity (Etype (Id));
4770 while Present (E) loop
4771 if Ekind (E) = E_Entry
4772 and then Present (Get_Attribute_Definition_Clause
4773 (E, Attribute_Address))
4774 then
4775 Error_Msg_Warn := SPARK_Mode /= On;
4776 Error_Msg_N
4777 ("more than one task with same entry address<<", N);
4778 Error_Msg_N ("\Program_Error [<<", N);
4779 Insert_Action (N,
4780 Make_Raise_Program_Error (Loc,
4781 Reason => PE_Duplicated_Entry_Address));
4782 exit;
4783 end if;
4784
4785 Next_Entity (E);
4786 end loop;
4787 end;
4788 end if;
4789 end if;
4790
4791 -- Some simple constant-propagation: if the expression is a constant
4792 -- string initialized with a literal, share the literal. This avoids
4793 -- a run-time copy.
4794
4795 if Present (E)
4796 and then Is_Entity_Name (E)
4797 and then Ekind (Entity (E)) = E_Constant
4798 and then Base_Type (Etype (E)) = Standard_String
4799 then
4800 declare
4801 Val : constant Node_Id := Constant_Value (Entity (E));
4802 begin
4803 if Present (Val) and then Nkind (Val) = N_String_Literal then
4804 Rewrite (E, New_Copy (Val));
4805 end if;
4806 end;
4807 end if;
4808
4809 -- Another optimization: if the nominal subtype is unconstrained and
4810 -- the expression is a function call that returns an unconstrained
4811 -- type, rewrite the declaration as a renaming of the result of the
4812 -- call. The exceptions below are cases where the copy is expected,
4813 -- either by the back end (Aliased case) or by the semantics, as for
4814 -- initializing controlled types or copying tags for class-wide types.
4815
4816 if Present (E)
4817 and then Nkind (E) = N_Explicit_Dereference
4818 and then Nkind (Original_Node (E)) = N_Function_Call
4819 and then not Is_Library_Level_Entity (Id)
4820 and then not Is_Constrained (Underlying_Type (T))
4821 and then not Is_Aliased (Id)
4822 and then not Is_Class_Wide_Type (T)
4823 and then not Is_Controlled_Active (T)
4824 and then not Has_Controlled_Component (Base_Type (T))
4825 and then Expander_Active
4826 then
4827 Rewrite (N,
4828 Make_Object_Renaming_Declaration (Loc,
4829 Defining_Identifier => Id,
4830 Access_Definition => Empty,
4831 Subtype_Mark => New_Occurrence_Of
4832 (Base_Type (Etype (Id)), Loc),
4833 Name => E));
4834
4835 Set_Renamed_Object (Id, E);
4836
4837 -- Force generation of debugging information for the constant and for
4838 -- the renamed function call.
4839
4840 Set_Debug_Info_Needed (Id);
4841 Set_Debug_Info_Needed (Entity (Prefix (E)));
4842 end if;
4843
4844 if Present (Prev_Entity)
4845 and then Is_Frozen (Prev_Entity)
4846 and then not Error_Posted (Id)
4847 then
4848 Error_Msg_N ("full constant declaration appears too late", N);
4849 end if;
4850
4851 Check_Eliminated (Id);
4852
4853 -- Deal with setting In_Private_Part flag if in private part
4854
4855 if Ekind (Scope (Id)) = E_Package
4856 and then In_Private_Part (Scope (Id))
4857 then
4858 Set_In_Private_Part (Id);
4859 end if;
4860
4861 <<Leave>>
4862 -- Initialize the refined state of a variable here because this is a
4863 -- common destination for legal and illegal object declarations.
4864
4865 if Ekind (Id) = E_Variable then
4866 Set_Encapsulating_State (Id, Empty);
4867 end if;
4868
4869 if Has_Aspects (N) then
4870 Analyze_Aspect_Specifications (N, Id);
4871 end if;
4872
4873 Analyze_Dimension (N);
4874
4875 -- Verify whether the object declaration introduces an illegal hidden
4876 -- state within a package subject to a null abstract state.
4877
4878 if Ekind (Id) = E_Variable then
4879 Check_No_Hidden_State (Id);
4880 end if;
4881
4882 Restore_Ghost_Mode (Saved_GM);
4883 end Analyze_Object_Declaration;
4884
4885 ---------------------------
4886 -- Analyze_Others_Choice --
4887 ---------------------------
4888
4889 -- Nothing to do for the others choice node itself, the semantic analysis
4890 -- of the others choice will occur as part of the processing of the parent
4891
4892 procedure Analyze_Others_Choice (N : Node_Id) is
4893 pragma Warnings (Off, N);
4894 begin
4895 null;
4896 end Analyze_Others_Choice;
4897
4898 -------------------------------------------
4899 -- Analyze_Private_Extension_Declaration --
4900 -------------------------------------------
4901
4902 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4903 Indic : constant Node_Id := Subtype_Indication (N);
4904 T : constant Entity_Id := Defining_Identifier (N);
4905 Iface : Entity_Id;
4906 Iface_Elmt : Elmt_Id;
4907 Parent_Base : Entity_Id;
4908 Parent_Type : Entity_Id;
4909
4910 begin
4911 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4912
4913 if Is_Non_Empty_List (Interface_List (N)) then
4914 declare
4915 Intf : Node_Id;
4916 T : Entity_Id;
4917
4918 begin
4919 Intf := First (Interface_List (N));
4920 while Present (Intf) loop
4921 T := Find_Type_Of_Subtype_Indic (Intf);
4922
4923 Diagnose_Interface (Intf, T);
4924 Next (Intf);
4925 end loop;
4926 end;
4927 end if;
4928
4929 Generate_Definition (T);
4930
4931 -- For other than Ada 2012, just enter the name in the current scope
4932
4933 if Ada_Version < Ada_2012 then
4934 Enter_Name (T);
4935
4936 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4937 -- case of private type that completes an incomplete type.
4938
4939 else
4940 declare
4941 Prev : Entity_Id;
4942
4943 begin
4944 Prev := Find_Type_Name (N);
4945
4946 pragma Assert (Prev = T
4947 or else (Ekind (Prev) = E_Incomplete_Type
4948 and then Present (Full_View (Prev))
4949 and then Full_View (Prev) = T));
4950 end;
4951 end if;
4952
4953 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4954 Parent_Base := Base_Type (Parent_Type);
4955
4956 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4957 Set_Ekind (T, Ekind (Parent_Type));
4958 Set_Etype (T, Any_Type);
4959 goto Leave;
4960
4961 elsif not Is_Tagged_Type (Parent_Type) then
4962 Error_Msg_N
4963 ("parent of type extension must be a tagged type ", Indic);
4964 goto Leave;
4965
4966 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4967 Error_Msg_N ("premature derivation of incomplete type", Indic);
4968 goto Leave;
4969
4970 elsif Is_Concurrent_Type (Parent_Type) then
4971 Error_Msg_N
4972 ("parent type of a private extension cannot be a synchronized "
4973 & "tagged type (RM 3.9.1 (3/1))", N);
4974
4975 Set_Etype (T, Any_Type);
4976 Set_Ekind (T, E_Limited_Private_Type);
4977 Set_Private_Dependents (T, New_Elmt_List);
4978 Set_Error_Posted (T);
4979 goto Leave;
4980 end if;
4981
4982 -- Perhaps the parent type should be changed to the class-wide type's
4983 -- specific type in this case to prevent cascading errors ???
4984
4985 if Is_Class_Wide_Type (Parent_Type) then
4986 Error_Msg_N
4987 ("parent of type extension must not be a class-wide type", Indic);
4988 goto Leave;
4989 end if;
4990
4991 if (not Is_Package_Or_Generic_Package (Current_Scope)
4992 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4993 or else In_Private_Part (Current_Scope)
4994 then
4995 Error_Msg_N ("invalid context for private extension", N);
4996 end if;
4997
4998 -- Set common attributes
4999
5000 Set_Is_Pure (T, Is_Pure (Current_Scope));
5001 Set_Scope (T, Current_Scope);
5002 Set_Ekind (T, E_Record_Type_With_Private);
5003 Init_Size_Align (T);
5004 Set_Default_SSO (T);
5005
5006 Set_Etype (T, Parent_Base);
5007 Propagate_Concurrent_Flags (T, Parent_Base);
5008
5009 Set_Convention (T, Convention (Parent_Type));
5010 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5011 Set_Is_First_Subtype (T);
5012 Make_Class_Wide_Type (T);
5013
5014 if Unknown_Discriminants_Present (N) then
5015 Set_Discriminant_Constraint (T, No_Elist);
5016 end if;
5017
5018 Build_Derived_Record_Type (N, Parent_Type, T);
5019
5020 -- A private extension inherits the Default_Initial_Condition pragma
5021 -- coming from any parent type within the derivation chain.
5022
5023 if Has_DIC (Parent_Type) then
5024 Set_Has_Inherited_DIC (T);
5025 end if;
5026
5027 -- A private extension inherits any class-wide invariants coming from a
5028 -- parent type or an interface. Note that the invariant procedure of the
5029 -- parent type should not be inherited because the private extension may
5030 -- define invariants of its own.
5031
5032 if Has_Inherited_Invariants (Parent_Type)
5033 or else Has_Inheritable_Invariants (Parent_Type)
5034 then
5035 Set_Has_Inherited_Invariants (T);
5036
5037 elsif Present (Interfaces (T)) then
5038 Iface_Elmt := First_Elmt (Interfaces (T));
5039 while Present (Iface_Elmt) loop
5040 Iface := Node (Iface_Elmt);
5041
5042 if Has_Inheritable_Invariants (Iface) then
5043 Set_Has_Inherited_Invariants (T);
5044 exit;
5045 end if;
5046
5047 Next_Elmt (Iface_Elmt);
5048 end loop;
5049 end if;
5050
5051 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5052 -- synchronized formal derived type.
5053
5054 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5055 Set_Is_Limited_Record (T);
5056
5057 -- Formal derived type case
5058
5059 if Is_Generic_Type (T) then
5060
5061 -- The parent must be a tagged limited type or a synchronized
5062 -- interface.
5063
5064 if (not Is_Tagged_Type (Parent_Type)
5065 or else not Is_Limited_Type (Parent_Type))
5066 and then
5067 (not Is_Interface (Parent_Type)
5068 or else not Is_Synchronized_Interface (Parent_Type))
5069 then
5070 Error_Msg_NE
5071 ("parent type of & must be tagged limited or synchronized",
5072 N, T);
5073 end if;
5074
5075 -- The progenitors (if any) must be limited or synchronized
5076 -- interfaces.
5077
5078 if Present (Interfaces (T)) then
5079 Iface_Elmt := First_Elmt (Interfaces (T));
5080 while Present (Iface_Elmt) loop
5081 Iface := Node (Iface_Elmt);
5082
5083 if not Is_Limited_Interface (Iface)
5084 and then not Is_Synchronized_Interface (Iface)
5085 then
5086 Error_Msg_NE
5087 ("progenitor & must be limited or synchronized",
5088 N, Iface);
5089 end if;
5090
5091 Next_Elmt (Iface_Elmt);
5092 end loop;
5093 end if;
5094
5095 -- Regular derived extension, the parent must be a limited or
5096 -- synchronized interface.
5097
5098 else
5099 if not Is_Interface (Parent_Type)
5100 or else (not Is_Limited_Interface (Parent_Type)
5101 and then not Is_Synchronized_Interface (Parent_Type))
5102 then
5103 Error_Msg_NE
5104 ("parent type of & must be limited interface", N, T);
5105 end if;
5106 end if;
5107
5108 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5109 -- extension with a synchronized parent must be explicitly declared
5110 -- synchronized, because the full view will be a synchronized type.
5111 -- This must be checked before the check for limited types below,
5112 -- to ensure that types declared limited are not allowed to extend
5113 -- synchronized interfaces.
5114
5115 elsif Is_Interface (Parent_Type)
5116 and then Is_Synchronized_Interface (Parent_Type)
5117 and then not Synchronized_Present (N)
5118 then
5119 Error_Msg_NE
5120 ("private extension of& must be explicitly synchronized",
5121 N, Parent_Type);
5122
5123 elsif Limited_Present (N) then
5124 Set_Is_Limited_Record (T);
5125
5126 if not Is_Limited_Type (Parent_Type)
5127 and then
5128 (not Is_Interface (Parent_Type)
5129 or else not Is_Limited_Interface (Parent_Type))
5130 then
5131 Error_Msg_NE ("parent type& of limited extension must be limited",
5132 N, Parent_Type);
5133 end if;
5134 end if;
5135
5136 -- Remember that its parent type has a private extension. Used to warn
5137 -- on public primitives of the parent type defined after its private
5138 -- extensions (see Check_Dispatching_Operation).
5139
5140 Set_Has_Private_Extension (Parent_Type);
5141
5142 <<Leave>>
5143 if Has_Aspects (N) then
5144 Analyze_Aspect_Specifications (N, T);
5145 end if;
5146 end Analyze_Private_Extension_Declaration;
5147
5148 ---------------------------------
5149 -- Analyze_Subtype_Declaration --
5150 ---------------------------------
5151
5152 procedure Analyze_Subtype_Declaration
5153 (N : Node_Id;
5154 Skip : Boolean := False)
5155 is
5156 Id : constant Entity_Id := Defining_Identifier (N);
5157 R_Checks : Check_Result;
5158 T : Entity_Id;
5159
5160 begin
5161 Generate_Definition (Id);
5162 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5163 Init_Size_Align (Id);
5164
5165 -- The following guard condition on Enter_Name is to handle cases where
5166 -- the defining identifier has already been entered into the scope but
5167 -- the declaration as a whole needs to be analyzed.
5168
5169 -- This case in particular happens for derived enumeration types. The
5170 -- derived enumeration type is processed as an inserted enumeration type
5171 -- declaration followed by a rewritten subtype declaration. The defining
5172 -- identifier, however, is entered into the name scope very early in the
5173 -- processing of the original type declaration and therefore needs to be
5174 -- avoided here, when the created subtype declaration is analyzed. (See
5175 -- Build_Derived_Types)
5176
5177 -- This also happens when the full view of a private type is derived
5178 -- type with constraints. In this case the entity has been introduced
5179 -- in the private declaration.
5180
5181 -- Finally this happens in some complex cases when validity checks are
5182 -- enabled, where the same subtype declaration may be analyzed twice.
5183 -- This can happen if the subtype is created by the pre-analysis of
5184 -- an attribute tht gives the range of a loop statement, and the loop
5185 -- itself appears within an if_statement that will be rewritten during
5186 -- expansion.
5187
5188 if Skip
5189 or else (Present (Etype (Id))
5190 and then (Is_Private_Type (Etype (Id))
5191 or else Is_Task_Type (Etype (Id))
5192 or else Is_Rewrite_Substitution (N)))
5193 then
5194 null;
5195
5196 elsif Current_Entity (Id) = Id then
5197 null;
5198
5199 else
5200 Enter_Name (Id);
5201 end if;
5202
5203 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5204
5205 -- Class-wide equivalent types of records with unknown discriminants
5206 -- involve the generation of an itype which serves as the private view
5207 -- of a constrained record subtype. In such cases the base type of the
5208 -- current subtype we are processing is the private itype. Use the full
5209 -- of the private itype when decorating various attributes.
5210
5211 if Is_Itype (T)
5212 and then Is_Private_Type (T)
5213 and then Present (Full_View (T))
5214 then
5215 T := Full_View (T);
5216 end if;
5217
5218 -- Inherit common attributes
5219
5220 Set_Is_Volatile (Id, Is_Volatile (T));
5221 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5222 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5223 Set_Convention (Id, Convention (T));
5224
5225 -- If ancestor has predicates then so does the subtype, and in addition
5226 -- we must delay the freeze to properly arrange predicate inheritance.
5227
5228 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5229 -- in which T = ID, so the above tests and assignments do nothing???
5230
5231 if Has_Predicates (T)
5232 or else (Present (Ancestor_Subtype (T))
5233 and then Has_Predicates (Ancestor_Subtype (T)))
5234 then
5235 Set_Has_Predicates (Id);
5236 Set_Has_Delayed_Freeze (Id);
5237
5238 -- Generated subtypes inherit the predicate function from the parent
5239 -- (no aspects to examine on the generated declaration).
5240
5241 if not Comes_From_Source (N) then
5242 Set_Ekind (Id, Ekind (T));
5243
5244 if Present (Predicate_Function (T)) then
5245 Set_Predicate_Function (Id, Predicate_Function (T));
5246
5247 elsif Present (Ancestor_Subtype (T))
5248 and then Has_Predicates (Ancestor_Subtype (T))
5249 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5250 then
5251 Set_Predicate_Function (Id,
5252 Predicate_Function (Ancestor_Subtype (T)));
5253 end if;
5254 end if;
5255 end if;
5256
5257 -- Subtype of Boolean cannot have a constraint in SPARK
5258
5259 if Is_Boolean_Type (T)
5260 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5261 then
5262 Check_SPARK_05_Restriction
5263 ("subtype of Boolean cannot have constraint", N);
5264 end if;
5265
5266 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5267 declare
5268 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5269 One_Cstr : Node_Id;
5270 Low : Node_Id;
5271 High : Node_Id;
5272
5273 begin
5274 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5275 One_Cstr := First (Constraints (Cstr));
5276 while Present (One_Cstr) loop
5277
5278 -- Index or discriminant constraint in SPARK must be a
5279 -- subtype mark.
5280
5281 if not
5282 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5283 then
5284 Check_SPARK_05_Restriction
5285 ("subtype mark required", One_Cstr);
5286
5287 -- String subtype must have a lower bound of 1 in SPARK.
5288 -- Note that we do not need to test for the non-static case
5289 -- here, since that was already taken care of in
5290 -- Process_Range_Expr_In_Decl.
5291
5292 elsif Base_Type (T) = Standard_String then
5293 Get_Index_Bounds (One_Cstr, Low, High);
5294
5295 if Is_OK_Static_Expression (Low)
5296 and then Expr_Value (Low) /= 1
5297 then
5298 Check_SPARK_05_Restriction
5299 ("String subtype must have lower bound of 1", N);
5300 end if;
5301 end if;
5302
5303 Next (One_Cstr);
5304 end loop;
5305 end if;
5306 end;
5307 end if;
5308
5309 -- In the case where there is no constraint given in the subtype
5310 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5311 -- semantic attributes must be established here.
5312
5313 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5314 Set_Etype (Id, Base_Type (T));
5315
5316 -- Subtype of unconstrained array without constraint is not allowed
5317 -- in SPARK.
5318
5319 if Is_Array_Type (T) and then not Is_Constrained (T) then
5320 Check_SPARK_05_Restriction
5321 ("subtype of unconstrained array must have constraint", N);
5322 end if;
5323
5324 case Ekind (T) is
5325 when Array_Kind =>
5326 Set_Ekind (Id, E_Array_Subtype);
5327 Copy_Array_Subtype_Attributes (Id, T);
5328
5329 when Decimal_Fixed_Point_Kind =>
5330 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5331 Set_Digits_Value (Id, Digits_Value (T));
5332 Set_Delta_Value (Id, Delta_Value (T));
5333 Set_Scale_Value (Id, Scale_Value (T));
5334 Set_Small_Value (Id, Small_Value (T));
5335 Set_Scalar_Range (Id, Scalar_Range (T));
5336 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5337 Set_Is_Constrained (Id, Is_Constrained (T));
5338 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5339 Set_RM_Size (Id, RM_Size (T));
5340
5341 when Enumeration_Kind =>
5342 Set_Ekind (Id, E_Enumeration_Subtype);
5343 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5344 Set_Scalar_Range (Id, Scalar_Range (T));
5345 Set_Is_Character_Type (Id, Is_Character_Type (T));
5346 Set_Is_Constrained (Id, Is_Constrained (T));
5347 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5348 Set_RM_Size (Id, RM_Size (T));
5349 Inherit_Predicate_Flags (Id, T);
5350
5351 when Ordinary_Fixed_Point_Kind =>
5352 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5353 Set_Scalar_Range (Id, Scalar_Range (T));
5354 Set_Small_Value (Id, Small_Value (T));
5355 Set_Delta_Value (Id, Delta_Value (T));
5356 Set_Is_Constrained (Id, Is_Constrained (T));
5357 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5358 Set_RM_Size (Id, RM_Size (T));
5359
5360 when Float_Kind =>
5361 Set_Ekind (Id, E_Floating_Point_Subtype);
5362 Set_Scalar_Range (Id, Scalar_Range (T));
5363 Set_Digits_Value (Id, Digits_Value (T));
5364 Set_Is_Constrained (Id, Is_Constrained (T));
5365
5366 -- If the floating point type has dimensions, these will be
5367 -- inherited subsequently when Analyze_Dimensions is called.
5368
5369 when Signed_Integer_Kind =>
5370 Set_Ekind (Id, E_Signed_Integer_Subtype);
5371 Set_Scalar_Range (Id, Scalar_Range (T));
5372 Set_Is_Constrained (Id, Is_Constrained (T));
5373 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5374 Set_RM_Size (Id, RM_Size (T));
5375 Inherit_Predicate_Flags (Id, T);
5376
5377 when Modular_Integer_Kind =>
5378 Set_Ekind (Id, E_Modular_Integer_Subtype);
5379 Set_Scalar_Range (Id, Scalar_Range (T));
5380 Set_Is_Constrained (Id, Is_Constrained (T));
5381 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5382 Set_RM_Size (Id, RM_Size (T));
5383 Inherit_Predicate_Flags (Id, T);
5384
5385 when Class_Wide_Kind =>
5386 Set_Ekind (Id, E_Class_Wide_Subtype);
5387 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5388 Set_Cloned_Subtype (Id, T);
5389 Set_Is_Tagged_Type (Id, True);
5390 Set_Has_Unknown_Discriminants
5391 (Id, True);
5392 Set_No_Tagged_Streams_Pragma
5393 (Id, No_Tagged_Streams_Pragma (T));
5394
5395 if Ekind (T) = E_Class_Wide_Subtype then
5396 Set_Equivalent_Type (Id, Equivalent_Type (T));
5397 end if;
5398
5399 when E_Record_Subtype
5400 | E_Record_Type
5401 =>
5402 Set_Ekind (Id, E_Record_Subtype);
5403
5404 if Ekind (T) = E_Record_Subtype
5405 and then Present (Cloned_Subtype (T))
5406 then
5407 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5408 else
5409 Set_Cloned_Subtype (Id, T);
5410 end if;
5411
5412 Set_First_Entity (Id, First_Entity (T));
5413 Set_Last_Entity (Id, Last_Entity (T));
5414 Set_Has_Discriminants (Id, Has_Discriminants (T));
5415 Set_Is_Constrained (Id, Is_Constrained (T));
5416 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5417 Set_Has_Implicit_Dereference
5418 (Id, Has_Implicit_Dereference (T));
5419 Set_Has_Unknown_Discriminants
5420 (Id, Has_Unknown_Discriminants (T));
5421
5422 if Has_Discriminants (T) then
5423 Set_Discriminant_Constraint
5424 (Id, Discriminant_Constraint (T));
5425 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5426
5427 elsif Has_Unknown_Discriminants (Id) then
5428 Set_Discriminant_Constraint (Id, No_Elist);
5429 end if;
5430
5431 if Is_Tagged_Type (T) then
5432 Set_Is_Tagged_Type (Id, True);
5433 Set_No_Tagged_Streams_Pragma
5434 (Id, No_Tagged_Streams_Pragma (T));
5435 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5436 Set_Direct_Primitive_Operations
5437 (Id, Direct_Primitive_Operations (T));
5438 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5439
5440 if Is_Interface (T) then
5441 Set_Is_Interface (Id);
5442 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5443 end if;
5444 end if;
5445
5446 when Private_Kind =>
5447 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5448 Set_Has_Discriminants (Id, Has_Discriminants (T));
5449 Set_Is_Constrained (Id, Is_Constrained (T));
5450 Set_First_Entity (Id, First_Entity (T));
5451 Set_Last_Entity (Id, Last_Entity (T));
5452 Set_Private_Dependents (Id, New_Elmt_List);
5453 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5454 Set_Has_Implicit_Dereference
5455 (Id, Has_Implicit_Dereference (T));
5456 Set_Has_Unknown_Discriminants
5457 (Id, Has_Unknown_Discriminants (T));
5458 Set_Known_To_Have_Preelab_Init
5459 (Id, Known_To_Have_Preelab_Init (T));
5460
5461 if Is_Tagged_Type (T) then
5462 Set_Is_Tagged_Type (Id);
5463 Set_No_Tagged_Streams_Pragma (Id,
5464 No_Tagged_Streams_Pragma (T));
5465 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5466 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5467 Set_Direct_Primitive_Operations (Id,
5468 Direct_Primitive_Operations (T));
5469 end if;
5470
5471 -- In general the attributes of the subtype of a private type
5472 -- are the attributes of the partial view of parent. However,
5473 -- the full view may be a discriminated type, and the subtype
5474 -- must share the discriminant constraint to generate correct
5475 -- calls to initialization procedures.
5476
5477 if Has_Discriminants (T) then
5478 Set_Discriminant_Constraint
5479 (Id, Discriminant_Constraint (T));
5480 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5481
5482 elsif Present (Full_View (T))
5483 and then Has_Discriminants (Full_View (T))
5484 then
5485 Set_Discriminant_Constraint
5486 (Id, Discriminant_Constraint (Full_View (T)));
5487 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5488
5489 -- This would seem semantically correct, but apparently
5490 -- generates spurious errors about missing components ???
5491
5492 -- Set_Has_Discriminants (Id);
5493 end if;
5494
5495 Prepare_Private_Subtype_Completion (Id, N);
5496
5497 -- If this is the subtype of a constrained private type with
5498 -- discriminants that has got a full view and we also have
5499 -- built a completion just above, show that the completion
5500 -- is a clone of the full view to the back-end.
5501
5502 if Has_Discriminants (T)
5503 and then not Has_Unknown_Discriminants (T)
5504 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5505 and then Present (Full_View (T))
5506 and then Present (Full_View (Id))
5507 then
5508 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5509 end if;
5510
5511 when Access_Kind =>
5512 Set_Ekind (Id, E_Access_Subtype);
5513 Set_Is_Constrained (Id, Is_Constrained (T));
5514 Set_Is_Access_Constant
5515 (Id, Is_Access_Constant (T));
5516 Set_Directly_Designated_Type
5517 (Id, Designated_Type (T));
5518 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5519
5520 -- A Pure library_item must not contain the declaration of a
5521 -- named access type, except within a subprogram, generic
5522 -- subprogram, task unit, or protected unit, or if it has
5523 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5524
5525 if Comes_From_Source (Id)
5526 and then In_Pure_Unit
5527 and then not In_Subprogram_Task_Protected_Unit
5528 and then not No_Pool_Assigned (Id)
5529 then
5530 Error_Msg_N
5531 ("named access types not allowed in pure unit", N);
5532 end if;
5533
5534 when Concurrent_Kind =>
5535 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5536 Set_Corresponding_Record_Type (Id,
5537 Corresponding_Record_Type (T));
5538 Set_First_Entity (Id, First_Entity (T));
5539 Set_First_Private_Entity (Id, First_Private_Entity (T));
5540 Set_Has_Discriminants (Id, Has_Discriminants (T));
5541 Set_Is_Constrained (Id, Is_Constrained (T));
5542 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5543 Set_Last_Entity (Id, Last_Entity (T));
5544
5545 if Is_Tagged_Type (T) then
5546 Set_No_Tagged_Streams_Pragma
5547 (Id, No_Tagged_Streams_Pragma (T));
5548 end if;
5549
5550 if Has_Discriminants (T) then
5551 Set_Discriminant_Constraint
5552 (Id, Discriminant_Constraint (T));
5553 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5554 end if;
5555
5556 when Incomplete_Kind =>
5557 if Ada_Version >= Ada_2005 then
5558
5559 -- In Ada 2005 an incomplete type can be explicitly tagged:
5560 -- propagate indication. Note that we also have to include
5561 -- subtypes for Ada 2012 extended use of incomplete types.
5562
5563 Set_Ekind (Id, E_Incomplete_Subtype);
5564 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5565 Set_Private_Dependents (Id, New_Elmt_List);
5566
5567 if Is_Tagged_Type (Id) then
5568 Set_No_Tagged_Streams_Pragma
5569 (Id, No_Tagged_Streams_Pragma (T));
5570 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5571 end if;
5572
5573 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5574 -- incomplete type visible through a limited with clause.
5575
5576 if From_Limited_With (T)
5577 and then Present (Non_Limited_View (T))
5578 then
5579 Set_From_Limited_With (Id);
5580 Set_Non_Limited_View (Id, Non_Limited_View (T));
5581
5582 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5583 -- to the private dependents of the original incomplete
5584 -- type for future transformation.
5585
5586 else
5587 Append_Elmt (Id, Private_Dependents (T));
5588 end if;
5589
5590 -- If the subtype name denotes an incomplete type an error
5591 -- was already reported by Process_Subtype.
5592
5593 else
5594 Set_Etype (Id, Any_Type);
5595 end if;
5596
5597 when others =>
5598 raise Program_Error;
5599 end case;
5600 end if;
5601
5602 if Etype (Id) = Any_Type then
5603 goto Leave;
5604 end if;
5605
5606 -- Some common processing on all types
5607
5608 Set_Size_Info (Id, T);
5609 Set_First_Rep_Item (Id, First_Rep_Item (T));
5610
5611 -- If the parent type is a generic actual, so is the subtype. This may
5612 -- happen in a nested instance. Why Comes_From_Source test???
5613
5614 if not Comes_From_Source (N) then
5615 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5616 end if;
5617
5618 -- If this is a subtype declaration for an actual in an instance,
5619 -- inherit static and dynamic predicates if any.
5620
5621 -- If declaration has no aspect specifications, inherit predicate
5622 -- info as well. Unclear how to handle the case of both specified
5623 -- and inherited predicates ??? Other inherited aspects, such as
5624 -- invariants, should be OK, but the combination with later pragmas
5625 -- may also require special merging.
5626
5627 if Has_Predicates (T)
5628 and then Present (Predicate_Function (T))
5629 and then
5630 ((In_Instance and then not Comes_From_Source (N))
5631 or else No (Aspect_Specifications (N)))
5632 then
5633 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5634
5635 if Has_Static_Predicate (T) then
5636 Set_Has_Static_Predicate (Id);
5637 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5638 end if;
5639 end if;
5640
5641 -- Remaining processing depends on characteristics of base type
5642
5643 T := Etype (Id);
5644
5645 Set_Is_Immediately_Visible (Id, True);
5646 Set_Depends_On_Private (Id, Has_Private_Component (T));
5647 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5648
5649 if Is_Interface (T) then
5650 Set_Is_Interface (Id);
5651 end if;
5652
5653 if Present (Generic_Parent_Type (N))
5654 and then
5655 (Nkind (Parent (Generic_Parent_Type (N))) /=
5656 N_Formal_Type_Declaration
5657 or else Nkind (Formal_Type_Definition
5658 (Parent (Generic_Parent_Type (N)))) /=
5659 N_Formal_Private_Type_Definition)
5660 then
5661 if Is_Tagged_Type (Id) then
5662
5663 -- If this is a generic actual subtype for a synchronized type,
5664 -- the primitive operations are those of the corresponding record
5665 -- for which there is a separate subtype declaration.
5666
5667 if Is_Concurrent_Type (Id) then
5668 null;
5669 elsif Is_Class_Wide_Type (Id) then
5670 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5671 else
5672 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5673 end if;
5674
5675 elsif Scope (Etype (Id)) /= Standard_Standard then
5676 Derive_Subprograms (Generic_Parent_Type (N), Id);
5677 end if;
5678 end if;
5679
5680 if Is_Private_Type (T) and then Present (Full_View (T)) then
5681 Conditional_Delay (Id, Full_View (T));
5682
5683 -- The subtypes of components or subcomponents of protected types
5684 -- do not need freeze nodes, which would otherwise appear in the
5685 -- wrong scope (before the freeze node for the protected type). The
5686 -- proper subtypes are those of the subcomponents of the corresponding
5687 -- record.
5688
5689 elsif Ekind (Scope (Id)) /= E_Protected_Type
5690 and then Present (Scope (Scope (Id))) -- error defense
5691 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5692 then
5693 Conditional_Delay (Id, T);
5694 end if;
5695
5696 -- Check that Constraint_Error is raised for a scalar subtype indication
5697 -- when the lower or upper bound of a non-null range lies outside the
5698 -- range of the type mark.
5699
5700 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5701 if Is_Scalar_Type (Etype (Id))
5702 and then Scalar_Range (Id) /=
5703 Scalar_Range
5704 (Etype (Subtype_Mark (Subtype_Indication (N))))
5705 then
5706 Apply_Range_Check
5707 (Scalar_Range (Id),
5708 Etype (Subtype_Mark (Subtype_Indication (N))));
5709
5710 -- In the array case, check compatibility for each index
5711
5712 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5713 then
5714 -- This really should be a subprogram that finds the indications
5715 -- to check???
5716
5717 declare
5718 Subt_Index : Node_Id := First_Index (Id);
5719 Target_Index : Node_Id :=
5720 First_Index (Etype
5721 (Subtype_Mark (Subtype_Indication (N))));
5722 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5723
5724 begin
5725 while Present (Subt_Index) loop
5726 if ((Nkind (Subt_Index) = N_Identifier
5727 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5728 or else Nkind (Subt_Index) = N_Subtype_Indication)
5729 and then
5730 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5731 then
5732 declare
5733 Target_Typ : constant Entity_Id :=
5734 Etype (Target_Index);
5735 begin
5736 R_Checks :=
5737 Get_Range_Checks
5738 (Scalar_Range (Etype (Subt_Index)),
5739 Target_Typ,
5740 Etype (Subt_Index),
5741 Defining_Identifier (N));
5742
5743 -- Reset Has_Dynamic_Range_Check on the subtype to
5744 -- prevent elision of the index check due to a dynamic
5745 -- check generated for a preceding index (needed since
5746 -- Insert_Range_Checks tries to avoid generating
5747 -- redundant checks on a given declaration).
5748
5749 Set_Has_Dynamic_Range_Check (N, False);
5750
5751 Insert_Range_Checks
5752 (R_Checks,
5753 N,
5754 Target_Typ,
5755 Sloc (Defining_Identifier (N)));
5756
5757 -- Record whether this index involved a dynamic check
5758
5759 Has_Dyn_Chk :=
5760 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5761 end;
5762 end if;
5763
5764 Next_Index (Subt_Index);
5765 Next_Index (Target_Index);
5766 end loop;
5767
5768 -- Finally, mark whether the subtype involves dynamic checks
5769
5770 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5771 end;
5772 end if;
5773 end if;
5774
5775 Set_Optimize_Alignment_Flags (Id);
5776 Check_Eliminated (Id);
5777
5778 <<Leave>>
5779 if Has_Aspects (N) then
5780 Analyze_Aspect_Specifications (N, Id);
5781 end if;
5782
5783 Analyze_Dimension (N);
5784
5785 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5786 -- indications on composite types where the constraints are dynamic.
5787 -- Note that object declarations and aggregates generate implicit
5788 -- subtype declarations, which this covers. One special case is that the
5789 -- implicitly generated "=" for discriminated types includes an
5790 -- offending subtype declaration, which is harmless, so we ignore it
5791 -- here.
5792
5793 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5794 declare
5795 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5796 begin
5797 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5798 and then not (Is_Internal (Id)
5799 and then Is_TSS (Scope (Id),
5800 TSS_Composite_Equality))
5801 and then not Within_Init_Proc
5802 and then not All_Composite_Constraints_Static (Cstr)
5803 then
5804 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5805 end if;
5806 end;
5807 end if;
5808 end Analyze_Subtype_Declaration;
5809
5810 --------------------------------
5811 -- Analyze_Subtype_Indication --
5812 --------------------------------
5813
5814 procedure Analyze_Subtype_Indication (N : Node_Id) is
5815 T : constant Entity_Id := Subtype_Mark (N);
5816 R : constant Node_Id := Range_Expression (Constraint (N));
5817
5818 begin
5819 Analyze (T);
5820
5821 if R /= Error then
5822 Analyze (R);
5823 Set_Etype (N, Etype (R));
5824 Resolve (R, Entity (T));
5825 else
5826 Set_Error_Posted (R);
5827 Set_Error_Posted (T);
5828 end if;
5829 end Analyze_Subtype_Indication;
5830
5831 --------------------------
5832 -- Analyze_Variant_Part --
5833 --------------------------
5834
5835 procedure Analyze_Variant_Part (N : Node_Id) is
5836 Discr_Name : Node_Id;
5837 Discr_Type : Entity_Id;
5838
5839 procedure Process_Variant (A : Node_Id);
5840 -- Analyze declarations for a single variant
5841
5842 package Analyze_Variant_Choices is
5843 new Generic_Analyze_Choices (Process_Variant);
5844 use Analyze_Variant_Choices;
5845
5846 ---------------------
5847 -- Process_Variant --
5848 ---------------------
5849
5850 procedure Process_Variant (A : Node_Id) is
5851 CL : constant Node_Id := Component_List (A);
5852 begin
5853 if not Null_Present (CL) then
5854 Analyze_Declarations (Component_Items (CL));
5855
5856 if Present (Variant_Part (CL)) then
5857 Analyze (Variant_Part (CL));
5858 end if;
5859 end if;
5860 end Process_Variant;
5861
5862 -- Start of processing for Analyze_Variant_Part
5863
5864 begin
5865 Discr_Name := Name (N);
5866 Analyze (Discr_Name);
5867
5868 -- If Discr_Name bad, get out (prevent cascaded errors)
5869
5870 if Etype (Discr_Name) = Any_Type then
5871 return;
5872 end if;
5873
5874 -- Check invalid discriminant in variant part
5875
5876 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5877 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5878 end if;
5879
5880 Discr_Type := Etype (Entity (Discr_Name));
5881
5882 if not Is_Discrete_Type (Discr_Type) then
5883 Error_Msg_N
5884 ("discriminant in a variant part must be of a discrete type",
5885 Name (N));
5886 return;
5887 end if;
5888
5889 -- Now analyze the choices, which also analyzes the declarations that
5890 -- are associated with each choice.
5891
5892 Analyze_Choices (Variants (N), Discr_Type);
5893
5894 -- Note: we used to instantiate and call Check_Choices here to check
5895 -- that the choices covered the discriminant, but it's too early to do
5896 -- that because of statically predicated subtypes, whose analysis may
5897 -- be deferred to their freeze point which may be as late as the freeze
5898 -- point of the containing record. So this call is now to be found in
5899 -- Freeze_Record_Declaration.
5900
5901 end Analyze_Variant_Part;
5902
5903 ----------------------------
5904 -- Array_Type_Declaration --
5905 ----------------------------
5906
5907 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5908 Component_Def : constant Node_Id := Component_Definition (Def);
5909 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5910 P : constant Node_Id := Parent (Def);
5911 Element_Type : Entity_Id;
5912 Implicit_Base : Entity_Id;
5913 Index : Node_Id;
5914 Nb_Index : Nat;
5915 Priv : Entity_Id;
5916 Related_Id : Entity_Id := Empty;
5917
5918 begin
5919 if Nkind (Def) = N_Constrained_Array_Definition then
5920 Index := First (Discrete_Subtype_Definitions (Def));
5921 else
5922 Index := First (Subtype_Marks (Def));
5923 end if;
5924
5925 -- Find proper names for the implicit types which may be public. In case
5926 -- of anonymous arrays we use the name of the first object of that type
5927 -- as prefix.
5928
5929 if No (T) then
5930 Related_Id := Defining_Identifier (P);
5931 else
5932 Related_Id := T;
5933 end if;
5934
5935 Nb_Index := 1;
5936 while Present (Index) loop
5937 Analyze (Index);
5938
5939 -- Test for odd case of trying to index a type by the type itself
5940
5941 if Is_Entity_Name (Index) and then Entity (Index) = T then
5942 Error_Msg_N ("type& cannot be indexed by itself", Index);
5943 Set_Entity (Index, Standard_Boolean);
5944 Set_Etype (Index, Standard_Boolean);
5945 end if;
5946
5947 -- Check SPARK restriction requiring a subtype mark
5948
5949 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5950 Check_SPARK_05_Restriction ("subtype mark required", Index);
5951 end if;
5952
5953 -- Add a subtype declaration for each index of private array type
5954 -- declaration whose etype is also private. For example:
5955
5956 -- package Pkg is
5957 -- type Index is private;
5958 -- private
5959 -- type Table is array (Index) of ...
5960 -- end;
5961
5962 -- This is currently required by the expander for the internally
5963 -- generated equality subprogram of records with variant parts in
5964 -- which the etype of some component is such private type.
5965
5966 if Ekind (Current_Scope) = E_Package
5967 and then In_Private_Part (Current_Scope)
5968 and then Has_Private_Declaration (Etype (Index))
5969 then
5970 declare
5971 Loc : constant Source_Ptr := Sloc (Def);
5972 Decl : Entity_Id;
5973 New_E : Entity_Id;
5974
5975 begin
5976 New_E := Make_Temporary (Loc, 'T');
5977 Set_Is_Internal (New_E);
5978
5979 Decl :=
5980 Make_Subtype_Declaration (Loc,
5981 Defining_Identifier => New_E,
5982 Subtype_Indication =>
5983 New_Occurrence_Of (Etype (Index), Loc));
5984
5985 Insert_Before (Parent (Def), Decl);
5986 Analyze (Decl);
5987 Set_Etype (Index, New_E);
5988
5989 -- If the index is a range the Entity attribute is not
5990 -- available. Example:
5991
5992 -- package Pkg is
5993 -- type T is private;
5994 -- private
5995 -- type T is new Natural;
5996 -- Table : array (T(1) .. T(10)) of Boolean;
5997 -- end Pkg;
5998
5999 if Nkind (Index) /= N_Range then
6000 Set_Entity (Index, New_E);
6001 end if;
6002 end;
6003 end if;
6004
6005 Make_Index (Index, P, Related_Id, Nb_Index);
6006
6007 -- Check error of subtype with predicate for index type
6008
6009 Bad_Predicated_Subtype_Use
6010 ("subtype& has predicate, not allowed as index subtype",
6011 Index, Etype (Index));
6012
6013 -- Move to next index
6014
6015 Next_Index (Index);
6016 Nb_Index := Nb_Index + 1;
6017 end loop;
6018
6019 -- Process subtype indication if one is present
6020
6021 if Present (Component_Typ) then
6022 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6023
6024 Set_Etype (Component_Typ, Element_Type);
6025
6026 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6027 Check_SPARK_05_Restriction
6028 ("subtype mark required", Component_Typ);
6029 end if;
6030
6031 -- Ada 2005 (AI-230): Access Definition case
6032
6033 else pragma Assert (Present (Access_Definition (Component_Def)));
6034
6035 -- Indicate that the anonymous access type is created by the
6036 -- array type declaration.
6037
6038 Element_Type := Access_Definition
6039 (Related_Nod => P,
6040 N => Access_Definition (Component_Def));
6041 Set_Is_Local_Anonymous_Access (Element_Type);
6042
6043 -- Propagate the parent. This field is needed if we have to generate
6044 -- the master_id associated with an anonymous access to task type
6045 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6046
6047 Set_Parent (Element_Type, Parent (T));
6048
6049 -- Ada 2005 (AI-230): In case of components that are anonymous access
6050 -- types the level of accessibility depends on the enclosing type
6051 -- declaration
6052
6053 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6054
6055 -- Ada 2005 (AI-254)
6056
6057 declare
6058 CD : constant Node_Id :=
6059 Access_To_Subprogram_Definition
6060 (Access_Definition (Component_Def));
6061 begin
6062 if Present (CD) and then Protected_Present (CD) then
6063 Element_Type :=
6064 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6065 end if;
6066 end;
6067 end if;
6068
6069 -- Constrained array case
6070
6071 if No (T) then
6072 T := Create_Itype (E_Void, P, Related_Id, 'T');
6073 end if;
6074
6075 if Nkind (Def) = N_Constrained_Array_Definition then
6076
6077 -- Establish Implicit_Base as unconstrained base type
6078
6079 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6080
6081 Set_Etype (Implicit_Base, Implicit_Base);
6082 Set_Scope (Implicit_Base, Current_Scope);
6083 Set_Has_Delayed_Freeze (Implicit_Base);
6084 Set_Default_SSO (Implicit_Base);
6085
6086 -- The constrained array type is a subtype of the unconstrained one
6087
6088 Set_Ekind (T, E_Array_Subtype);
6089 Init_Size_Align (T);
6090 Set_Etype (T, Implicit_Base);
6091 Set_Scope (T, Current_Scope);
6092 Set_Is_Constrained (T);
6093 Set_First_Index (T,
6094 First (Discrete_Subtype_Definitions (Def)));
6095 Set_Has_Delayed_Freeze (T);
6096
6097 -- Complete setup of implicit base type
6098
6099 Set_Component_Size (Implicit_Base, Uint_0);
6100 Set_Component_Type (Implicit_Base, Element_Type);
6101 Set_Finalize_Storage_Only
6102 (Implicit_Base,
6103 Finalize_Storage_Only (Element_Type));
6104 Set_First_Index (Implicit_Base, First_Index (T));
6105 Set_Has_Controlled_Component
6106 (Implicit_Base,
6107 Has_Controlled_Component (Element_Type)
6108 or else Is_Controlled_Active (Element_Type));
6109 Set_Packed_Array_Impl_Type
6110 (Implicit_Base, Empty);
6111
6112 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6113
6114 -- Unconstrained array case
6115
6116 else
6117 Set_Ekind (T, E_Array_Type);
6118 Init_Size_Align (T);
6119 Set_Etype (T, T);
6120 Set_Scope (T, Current_Scope);
6121 Set_Component_Size (T, Uint_0);
6122 Set_Is_Constrained (T, False);
6123 Set_First_Index (T, First (Subtype_Marks (Def)));
6124 Set_Has_Delayed_Freeze (T, True);
6125 Propagate_Concurrent_Flags (T, Element_Type);
6126 Set_Has_Controlled_Component (T, Has_Controlled_Component
6127 (Element_Type)
6128 or else
6129 Is_Controlled_Active (Element_Type));
6130 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6131 (Element_Type));
6132 Set_Default_SSO (T);
6133 end if;
6134
6135 -- Common attributes for both cases
6136
6137 Set_Component_Type (Base_Type (T), Element_Type);
6138 Set_Packed_Array_Impl_Type (T, Empty);
6139
6140 if Aliased_Present (Component_Definition (Def)) then
6141 Check_SPARK_05_Restriction
6142 ("aliased is not allowed", Component_Definition (Def));
6143 Set_Has_Aliased_Components (Etype (T));
6144 end if;
6145
6146 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6147 -- array type to ensure that objects of this type are initialized.
6148
6149 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6150 Set_Can_Never_Be_Null (T);
6151
6152 if Null_Exclusion_Present (Component_Definition (Def))
6153
6154 -- No need to check itypes because in their case this check was
6155 -- done at their point of creation
6156
6157 and then not Is_Itype (Element_Type)
6158 then
6159 Error_Msg_N
6160 ("`NOT NULL` not allowed (null already excluded)",
6161 Subtype_Indication (Component_Definition (Def)));
6162 end if;
6163 end if;
6164
6165 Priv := Private_Component (Element_Type);
6166
6167 if Present (Priv) then
6168
6169 -- Check for circular definitions
6170
6171 if Priv = Any_Type then
6172 Set_Component_Type (Etype (T), Any_Type);
6173
6174 -- There is a gap in the visibility of operations on the composite
6175 -- type only if the component type is defined in a different scope.
6176
6177 elsif Scope (Priv) = Current_Scope then
6178 null;
6179
6180 elsif Is_Limited_Type (Priv) then
6181 Set_Is_Limited_Composite (Etype (T));
6182 Set_Is_Limited_Composite (T);
6183 else
6184 Set_Is_Private_Composite (Etype (T));
6185 Set_Is_Private_Composite (T);
6186 end if;
6187 end if;
6188
6189 -- A syntax error in the declaration itself may lead to an empty index
6190 -- list, in which case do a minimal patch.
6191
6192 if No (First_Index (T)) then
6193 Error_Msg_N ("missing index definition in array type declaration", T);
6194
6195 declare
6196 Indexes : constant List_Id :=
6197 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6198 begin
6199 Set_Discrete_Subtype_Definitions (Def, Indexes);
6200 Set_First_Index (T, First (Indexes));
6201 return;
6202 end;
6203 end if;
6204
6205 -- Create a concatenation operator for the new type. Internal array
6206 -- types created for packed entities do not need such, they are
6207 -- compatible with the user-defined type.
6208
6209 if Number_Dimensions (T) = 1
6210 and then not Is_Packed_Array_Impl_Type (T)
6211 then
6212 New_Concatenation_Op (T);
6213 end if;
6214
6215 -- In the case of an unconstrained array the parser has already verified
6216 -- that all the indexes are unconstrained but we still need to make sure
6217 -- that the element type is constrained.
6218
6219 if not Is_Definite_Subtype (Element_Type) then
6220 Error_Msg_N
6221 ("unconstrained element type in array declaration",
6222 Subtype_Indication (Component_Def));
6223
6224 elsif Is_Abstract_Type (Element_Type) then
6225 Error_Msg_N
6226 ("the type of a component cannot be abstract",
6227 Subtype_Indication (Component_Def));
6228 end if;
6229
6230 -- There may be an invariant declared for the component type, but
6231 -- the construction of the component invariant checking procedure
6232 -- takes place during expansion.
6233 end Array_Type_Declaration;
6234
6235 ------------------------------------------------------
6236 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6237 ------------------------------------------------------
6238
6239 function Replace_Anonymous_Access_To_Protected_Subprogram
6240 (N : Node_Id) return Entity_Id
6241 is
6242 Loc : constant Source_Ptr := Sloc (N);
6243
6244 Curr_Scope : constant Scope_Stack_Entry :=
6245 Scope_Stack.Table (Scope_Stack.Last);
6246
6247 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6248
6249 Acc : Node_Id;
6250 -- Access definition in declaration
6251
6252 Comp : Node_Id;
6253 -- Object definition or formal definition with an access definition
6254
6255 Decl : Node_Id;
6256 -- Declaration of anonymous access to subprogram type
6257
6258 Spec : Node_Id;
6259 -- Original specification in access to subprogram
6260
6261 P : Node_Id;
6262
6263 begin
6264 Set_Is_Internal (Anon);
6265
6266 case Nkind (N) is
6267 when N_Constrained_Array_Definition
6268 | N_Component_Declaration
6269 | N_Unconstrained_Array_Definition
6270 =>
6271 Comp := Component_Definition (N);
6272 Acc := Access_Definition (Comp);
6273
6274 when N_Discriminant_Specification =>
6275 Comp := Discriminant_Type (N);
6276 Acc := Comp;
6277
6278 when N_Parameter_Specification =>
6279 Comp := Parameter_Type (N);
6280 Acc := Comp;
6281
6282 when N_Access_Function_Definition =>
6283 Comp := Result_Definition (N);
6284 Acc := Comp;
6285
6286 when N_Object_Declaration =>
6287 Comp := Object_Definition (N);
6288 Acc := Comp;
6289
6290 when N_Function_Specification =>
6291 Comp := Result_Definition (N);
6292 Acc := Comp;
6293
6294 when others =>
6295 raise Program_Error;
6296 end case;
6297
6298 Spec := Access_To_Subprogram_Definition (Acc);
6299
6300 Decl :=
6301 Make_Full_Type_Declaration (Loc,
6302 Defining_Identifier => Anon,
6303 Type_Definition => Copy_Separate_Tree (Spec));
6304
6305 Mark_Rewrite_Insertion (Decl);
6306
6307 -- In ASIS mode, analyze the profile on the original node, because
6308 -- the separate copy does not provide enough links to recover the
6309 -- original tree. Analysis is limited to type annotations, within
6310 -- a temporary scope that serves as an anonymous subprogram to collect
6311 -- otherwise useless temporaries and itypes.
6312
6313 if ASIS_Mode then
6314 declare
6315 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6316
6317 begin
6318 if Nkind (Spec) = N_Access_Function_Definition then
6319 Set_Ekind (Typ, E_Function);
6320 else
6321 Set_Ekind (Typ, E_Procedure);
6322 end if;
6323
6324 Set_Parent (Typ, N);
6325 Set_Scope (Typ, Current_Scope);
6326 Push_Scope (Typ);
6327
6328 -- Nothing to do if procedure is parameterless
6329
6330 if Present (Parameter_Specifications (Spec)) then
6331 Process_Formals (Parameter_Specifications (Spec), Spec);
6332 end if;
6333
6334 if Nkind (Spec) = N_Access_Function_Definition then
6335 declare
6336 Def : constant Node_Id := Result_Definition (Spec);
6337
6338 begin
6339 -- The result might itself be an anonymous access type, so
6340 -- have to recurse.
6341
6342 if Nkind (Def) = N_Access_Definition then
6343 if Present (Access_To_Subprogram_Definition (Def)) then
6344 Set_Etype
6345 (Def,
6346 Replace_Anonymous_Access_To_Protected_Subprogram
6347 (Spec));
6348 else
6349 Find_Type (Subtype_Mark (Def));
6350 end if;
6351
6352 else
6353 Find_Type (Def);
6354 end if;
6355 end;
6356 end if;
6357
6358 End_Scope;
6359 end;
6360 end if;
6361
6362 -- Insert the new declaration in the nearest enclosing scope. If the
6363 -- parent is a body and N is its return type, the declaration belongs
6364 -- in the enclosing scope. Likewise if N is the type of a parameter.
6365
6366 P := Parent (N);
6367
6368 if Nkind (N) = N_Function_Specification
6369 and then Nkind (P) = N_Subprogram_Body
6370 then
6371 P := Parent (P);
6372 elsif Nkind (N) = N_Parameter_Specification
6373 and then Nkind (P) in N_Subprogram_Specification
6374 and then Nkind (Parent (P)) = N_Subprogram_Body
6375 then
6376 P := Parent (Parent (P));
6377 end if;
6378
6379 while Present (P) and then not Has_Declarations (P) loop
6380 P := Parent (P);
6381 end loop;
6382
6383 pragma Assert (Present (P));
6384
6385 if Nkind (P) = N_Package_Specification then
6386 Prepend (Decl, Visible_Declarations (P));
6387 else
6388 Prepend (Decl, Declarations (P));
6389 end if;
6390
6391 -- Replace the anonymous type with an occurrence of the new declaration.
6392 -- In all cases the rewritten node does not have the null-exclusion
6393 -- attribute because (if present) it was already inherited by the
6394 -- anonymous entity (Anon). Thus, in case of components we do not
6395 -- inherit this attribute.
6396
6397 if Nkind (N) = N_Parameter_Specification then
6398 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6399 Set_Etype (Defining_Identifier (N), Anon);
6400 Set_Null_Exclusion_Present (N, False);
6401
6402 elsif Nkind (N) = N_Object_Declaration then
6403 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6404 Set_Etype (Defining_Identifier (N), Anon);
6405
6406 elsif Nkind (N) = N_Access_Function_Definition then
6407 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6408
6409 elsif Nkind (N) = N_Function_Specification then
6410 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6411 Set_Etype (Defining_Unit_Name (N), Anon);
6412
6413 else
6414 Rewrite (Comp,
6415 Make_Component_Definition (Loc,
6416 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6417 end if;
6418
6419 Mark_Rewrite_Insertion (Comp);
6420
6421 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6422 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6423 and then not Is_Type (Current_Scope))
6424 then
6425
6426 -- Declaration can be analyzed in the current scope.
6427
6428 Analyze (Decl);
6429
6430 else
6431 -- Temporarily remove the current scope (record or subprogram) from
6432 -- the stack to add the new declarations to the enclosing scope.
6433 -- The anonymous entity is an Itype with the proper attributes.
6434
6435 Scope_Stack.Decrement_Last;
6436 Analyze (Decl);
6437 Set_Is_Itype (Anon);
6438 Set_Associated_Node_For_Itype (Anon, N);
6439 Scope_Stack.Append (Curr_Scope);
6440 end if;
6441
6442 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6443 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6444 return Anon;
6445 end Replace_Anonymous_Access_To_Protected_Subprogram;
6446
6447 -------------------------------
6448 -- Build_Derived_Access_Type --
6449 -------------------------------
6450
6451 procedure Build_Derived_Access_Type
6452 (N : Node_Id;
6453 Parent_Type : Entity_Id;
6454 Derived_Type : Entity_Id)
6455 is
6456 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6457
6458 Desig_Type : Entity_Id;
6459 Discr : Entity_Id;
6460 Discr_Con_Elist : Elist_Id;
6461 Discr_Con_El : Elmt_Id;
6462 Subt : Entity_Id;
6463
6464 begin
6465 -- Set the designated type so it is available in case this is an access
6466 -- to a self-referential type, e.g. a standard list type with a next
6467 -- pointer. Will be reset after subtype is built.
6468
6469 Set_Directly_Designated_Type
6470 (Derived_Type, Designated_Type (Parent_Type));
6471
6472 Subt := Process_Subtype (S, N);
6473
6474 if Nkind (S) /= N_Subtype_Indication
6475 and then Subt /= Base_Type (Subt)
6476 then
6477 Set_Ekind (Derived_Type, E_Access_Subtype);
6478 end if;
6479
6480 if Ekind (Derived_Type) = E_Access_Subtype then
6481 declare
6482 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6483 Ibase : constant Entity_Id :=
6484 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6485 Svg_Chars : constant Name_Id := Chars (Ibase);
6486 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6487
6488 begin
6489 Copy_Node (Pbase, Ibase);
6490
6491 -- Restore Itype status after Copy_Node
6492
6493 Set_Is_Itype (Ibase);
6494 Set_Associated_Node_For_Itype (Ibase, N);
6495
6496 Set_Chars (Ibase, Svg_Chars);
6497 Set_Next_Entity (Ibase, Svg_Next_E);
6498 Set_Sloc (Ibase, Sloc (Derived_Type));
6499 Set_Scope (Ibase, Scope (Derived_Type));
6500 Set_Freeze_Node (Ibase, Empty);
6501 Set_Is_Frozen (Ibase, False);
6502 Set_Comes_From_Source (Ibase, False);
6503 Set_Is_First_Subtype (Ibase, False);
6504
6505 Set_Etype (Ibase, Pbase);
6506 Set_Etype (Derived_Type, Ibase);
6507 end;
6508 end if;
6509
6510 Set_Directly_Designated_Type
6511 (Derived_Type, Designated_Type (Subt));
6512
6513 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6514 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6515 Set_Size_Info (Derived_Type, Parent_Type);
6516 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6517 Set_Depends_On_Private (Derived_Type,
6518 Has_Private_Component (Derived_Type));
6519 Conditional_Delay (Derived_Type, Subt);
6520
6521 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6522 -- that it is not redundant.
6523
6524 if Null_Exclusion_Present (Type_Definition (N)) then
6525 Set_Can_Never_Be_Null (Derived_Type);
6526
6527 elsif Can_Never_Be_Null (Parent_Type) then
6528 Set_Can_Never_Be_Null (Derived_Type);
6529 end if;
6530
6531 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6532 -- the root type for this information.
6533
6534 -- Apply range checks to discriminants for derived record case
6535 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6536
6537 Desig_Type := Designated_Type (Derived_Type);
6538
6539 if Is_Composite_Type (Desig_Type)
6540 and then (not Is_Array_Type (Desig_Type))
6541 and then Has_Discriminants (Desig_Type)
6542 and then Base_Type (Desig_Type) /= Desig_Type
6543 then
6544 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6545 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6546
6547 Discr := First_Discriminant (Base_Type (Desig_Type));
6548 while Present (Discr_Con_El) loop
6549 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6550 Next_Elmt (Discr_Con_El);
6551 Next_Discriminant (Discr);
6552 end loop;
6553 end if;
6554 end Build_Derived_Access_Type;
6555
6556 ------------------------------
6557 -- Build_Derived_Array_Type --
6558 ------------------------------
6559
6560 procedure Build_Derived_Array_Type
6561 (N : Node_Id;
6562 Parent_Type : Entity_Id;
6563 Derived_Type : Entity_Id)
6564 is
6565 Loc : constant Source_Ptr := Sloc (N);
6566 Tdef : constant Node_Id := Type_Definition (N);
6567 Indic : constant Node_Id := Subtype_Indication (Tdef);
6568 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6569 Implicit_Base : Entity_Id;
6570 New_Indic : Node_Id;
6571
6572 procedure Make_Implicit_Base;
6573 -- If the parent subtype is constrained, the derived type is a subtype
6574 -- of an implicit base type derived from the parent base.
6575
6576 ------------------------
6577 -- Make_Implicit_Base --
6578 ------------------------
6579
6580 procedure Make_Implicit_Base is
6581 begin
6582 Implicit_Base :=
6583 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6584
6585 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6586 Set_Etype (Implicit_Base, Parent_Base);
6587
6588 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6589 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6590
6591 Set_Has_Delayed_Freeze (Implicit_Base, True);
6592 end Make_Implicit_Base;
6593
6594 -- Start of processing for Build_Derived_Array_Type
6595
6596 begin
6597 if not Is_Constrained (Parent_Type) then
6598 if Nkind (Indic) /= N_Subtype_Indication then
6599 Set_Ekind (Derived_Type, E_Array_Type);
6600
6601 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6602 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6603
6604 Set_Has_Delayed_Freeze (Derived_Type, True);
6605
6606 else
6607 Make_Implicit_Base;
6608 Set_Etype (Derived_Type, Implicit_Base);
6609
6610 New_Indic :=
6611 Make_Subtype_Declaration (Loc,
6612 Defining_Identifier => Derived_Type,
6613 Subtype_Indication =>
6614 Make_Subtype_Indication (Loc,
6615 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6616 Constraint => Constraint (Indic)));
6617
6618 Rewrite (N, New_Indic);
6619 Analyze (N);
6620 end if;
6621
6622 else
6623 if Nkind (Indic) /= N_Subtype_Indication then
6624 Make_Implicit_Base;
6625
6626 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6627 Set_Etype (Derived_Type, Implicit_Base);
6628 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6629
6630 else
6631 Error_Msg_N ("illegal constraint on constrained type", Indic);
6632 end if;
6633 end if;
6634
6635 -- If parent type is not a derived type itself, and is declared in
6636 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6637 -- the new type's concatenation operator since Derive_Subprograms
6638 -- will not inherit the parent's operator. If the parent type is
6639 -- unconstrained, the operator is of the unconstrained base type.
6640
6641 if Number_Dimensions (Parent_Type) = 1
6642 and then not Is_Limited_Type (Parent_Type)
6643 and then not Is_Derived_Type (Parent_Type)
6644 and then not Is_Package_Or_Generic_Package
6645 (Scope (Base_Type (Parent_Type)))
6646 then
6647 if not Is_Constrained (Parent_Type)
6648 and then Is_Constrained (Derived_Type)
6649 then
6650 New_Concatenation_Op (Implicit_Base);
6651 else
6652 New_Concatenation_Op (Derived_Type);
6653 end if;
6654 end if;
6655 end Build_Derived_Array_Type;
6656
6657 -----------------------------------
6658 -- Build_Derived_Concurrent_Type --
6659 -----------------------------------
6660
6661 procedure Build_Derived_Concurrent_Type
6662 (N : Node_Id;
6663 Parent_Type : Entity_Id;
6664 Derived_Type : Entity_Id)
6665 is
6666 Loc : constant Source_Ptr := Sloc (N);
6667
6668 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6669 Corr_Decl : Node_Id;
6670 Corr_Decl_Needed : Boolean;
6671 -- If the derived type has fewer discriminants than its parent, the
6672 -- corresponding record is also a derived type, in order to account for
6673 -- the bound discriminants. We create a full type declaration for it in
6674 -- this case.
6675
6676 Constraint_Present : constant Boolean :=
6677 Nkind (Subtype_Indication (Type_Definition (N))) =
6678 N_Subtype_Indication;
6679
6680 D_Constraint : Node_Id;
6681 New_Constraint : Elist_Id;
6682 Old_Disc : Entity_Id;
6683 New_Disc : Entity_Id;
6684 New_N : Node_Id;
6685
6686 begin
6687 Set_Stored_Constraint (Derived_Type, No_Elist);
6688 Corr_Decl_Needed := False;
6689 Old_Disc := Empty;
6690
6691 if Present (Discriminant_Specifications (N))
6692 and then Constraint_Present
6693 then
6694 Old_Disc := First_Discriminant (Parent_Type);
6695 New_Disc := First (Discriminant_Specifications (N));
6696 while Present (New_Disc) and then Present (Old_Disc) loop
6697 Next_Discriminant (Old_Disc);
6698 Next (New_Disc);
6699 end loop;
6700 end if;
6701
6702 if Present (Old_Disc) and then Expander_Active then
6703
6704 -- The new type has fewer discriminants, so we need to create a new
6705 -- corresponding record, which is derived from the corresponding
6706 -- record of the parent, and has a stored constraint that captures
6707 -- the values of the discriminant constraints. The corresponding
6708 -- record is needed only if expander is active and code generation is
6709 -- enabled.
6710
6711 -- The type declaration for the derived corresponding record has the
6712 -- same discriminant part and constraints as the current declaration.
6713 -- Copy the unanalyzed tree to build declaration.
6714
6715 Corr_Decl_Needed := True;
6716 New_N := Copy_Separate_Tree (N);
6717
6718 Corr_Decl :=
6719 Make_Full_Type_Declaration (Loc,
6720 Defining_Identifier => Corr_Record,
6721 Discriminant_Specifications =>
6722 Discriminant_Specifications (New_N),
6723 Type_Definition =>
6724 Make_Derived_Type_Definition (Loc,
6725 Subtype_Indication =>
6726 Make_Subtype_Indication (Loc,
6727 Subtype_Mark =>
6728 New_Occurrence_Of
6729 (Corresponding_Record_Type (Parent_Type), Loc),
6730 Constraint =>
6731 Constraint
6732 (Subtype_Indication (Type_Definition (New_N))))));
6733 end if;
6734
6735 -- Copy Storage_Size and Relative_Deadline variables if task case
6736
6737 if Is_Task_Type (Parent_Type) then
6738 Set_Storage_Size_Variable (Derived_Type,
6739 Storage_Size_Variable (Parent_Type));
6740 Set_Relative_Deadline_Variable (Derived_Type,
6741 Relative_Deadline_Variable (Parent_Type));
6742 end if;
6743
6744 if Present (Discriminant_Specifications (N)) then
6745 Push_Scope (Derived_Type);
6746 Check_Or_Process_Discriminants (N, Derived_Type);
6747
6748 if Constraint_Present then
6749 New_Constraint :=
6750 Expand_To_Stored_Constraint
6751 (Parent_Type,
6752 Build_Discriminant_Constraints
6753 (Parent_Type,
6754 Subtype_Indication (Type_Definition (N)), True));
6755 end if;
6756
6757 End_Scope;
6758
6759 elsif Constraint_Present then
6760
6761 -- Build constrained subtype, copying the constraint, and derive
6762 -- from it to create a derived constrained type.
6763
6764 declare
6765 Loc : constant Source_Ptr := Sloc (N);
6766 Anon : constant Entity_Id :=
6767 Make_Defining_Identifier (Loc,
6768 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6769 Decl : Node_Id;
6770
6771 begin
6772 Decl :=
6773 Make_Subtype_Declaration (Loc,
6774 Defining_Identifier => Anon,
6775 Subtype_Indication =>
6776 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6777 Insert_Before (N, Decl);
6778 Analyze (Decl);
6779
6780 Rewrite (Subtype_Indication (Type_Definition (N)),
6781 New_Occurrence_Of (Anon, Loc));
6782 Set_Analyzed (Derived_Type, False);
6783 Analyze (N);
6784 return;
6785 end;
6786 end if;
6787
6788 -- By default, operations and private data are inherited from parent.
6789 -- However, in the presence of bound discriminants, a new corresponding
6790 -- record will be created, see below.
6791
6792 Set_Has_Discriminants
6793 (Derived_Type, Has_Discriminants (Parent_Type));
6794 Set_Corresponding_Record_Type
6795 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6796
6797 -- Is_Constrained is set according the parent subtype, but is set to
6798 -- False if the derived type is declared with new discriminants.
6799
6800 Set_Is_Constrained
6801 (Derived_Type,
6802 (Is_Constrained (Parent_Type) or else Constraint_Present)
6803 and then not Present (Discriminant_Specifications (N)));
6804
6805 if Constraint_Present then
6806 if not Has_Discriminants (Parent_Type) then
6807 Error_Msg_N ("untagged parent must have discriminants", N);
6808
6809 elsif Present (Discriminant_Specifications (N)) then
6810
6811 -- Verify that new discriminants are used to constrain old ones
6812
6813 D_Constraint :=
6814 First
6815 (Constraints
6816 (Constraint (Subtype_Indication (Type_Definition (N)))));
6817
6818 Old_Disc := First_Discriminant (Parent_Type);
6819
6820 while Present (D_Constraint) loop
6821 if Nkind (D_Constraint) /= N_Discriminant_Association then
6822
6823 -- Positional constraint. If it is a reference to a new
6824 -- discriminant, it constrains the corresponding old one.
6825
6826 if Nkind (D_Constraint) = N_Identifier then
6827 New_Disc := First_Discriminant (Derived_Type);
6828 while Present (New_Disc) loop
6829 exit when Chars (New_Disc) = Chars (D_Constraint);
6830 Next_Discriminant (New_Disc);
6831 end loop;
6832
6833 if Present (New_Disc) then
6834 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6835 end if;
6836 end if;
6837
6838 Next_Discriminant (Old_Disc);
6839
6840 -- if this is a named constraint, search by name for the old
6841 -- discriminants constrained by the new one.
6842
6843 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6844
6845 -- Find new discriminant with that name
6846
6847 New_Disc := First_Discriminant (Derived_Type);
6848 while Present (New_Disc) loop
6849 exit when
6850 Chars (New_Disc) = Chars (Expression (D_Constraint));
6851 Next_Discriminant (New_Disc);
6852 end loop;
6853
6854 if Present (New_Disc) then
6855
6856 -- Verify that new discriminant renames some discriminant
6857 -- of the parent type, and associate the new discriminant
6858 -- with one or more old ones that it renames.
6859
6860 declare
6861 Selector : Node_Id;
6862
6863 begin
6864 Selector := First (Selector_Names (D_Constraint));
6865 while Present (Selector) loop
6866 Old_Disc := First_Discriminant (Parent_Type);
6867 while Present (Old_Disc) loop
6868 exit when Chars (Old_Disc) = Chars (Selector);
6869 Next_Discriminant (Old_Disc);
6870 end loop;
6871
6872 if Present (Old_Disc) then
6873 Set_Corresponding_Discriminant
6874 (New_Disc, Old_Disc);
6875 end if;
6876
6877 Next (Selector);
6878 end loop;
6879 end;
6880 end if;
6881 end if;
6882
6883 Next (D_Constraint);
6884 end loop;
6885
6886 New_Disc := First_Discriminant (Derived_Type);
6887 while Present (New_Disc) loop
6888 if No (Corresponding_Discriminant (New_Disc)) then
6889 Error_Msg_NE
6890 ("new discriminant& must constrain old one", N, New_Disc);
6891
6892 elsif not
6893 Subtypes_Statically_Compatible
6894 (Etype (New_Disc),
6895 Etype (Corresponding_Discriminant (New_Disc)))
6896 then
6897 Error_Msg_NE
6898 ("& not statically compatible with parent discriminant",
6899 N, New_Disc);
6900 end if;
6901
6902 Next_Discriminant (New_Disc);
6903 end loop;
6904 end if;
6905
6906 elsif Present (Discriminant_Specifications (N)) then
6907 Error_Msg_N
6908 ("missing discriminant constraint in untagged derivation", N);
6909 end if;
6910
6911 -- The entity chain of the derived type includes the new discriminants
6912 -- but shares operations with the parent.
6913
6914 if Present (Discriminant_Specifications (N)) then
6915 Old_Disc := First_Discriminant (Parent_Type);
6916 while Present (Old_Disc) loop
6917 if No (Next_Entity (Old_Disc))
6918 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6919 then
6920 Set_Next_Entity
6921 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6922 exit;
6923 end if;
6924
6925 Next_Discriminant (Old_Disc);
6926 end loop;
6927
6928 else
6929 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6930 if Has_Discriminants (Parent_Type) then
6931 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6932 Set_Discriminant_Constraint (
6933 Derived_Type, Discriminant_Constraint (Parent_Type));
6934 end if;
6935 end if;
6936
6937 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6938
6939 Set_Has_Completion (Derived_Type);
6940
6941 if Corr_Decl_Needed then
6942 Set_Stored_Constraint (Derived_Type, New_Constraint);
6943 Insert_After (N, Corr_Decl);
6944 Analyze (Corr_Decl);
6945 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6946 end if;
6947 end Build_Derived_Concurrent_Type;
6948
6949 ------------------------------------
6950 -- Build_Derived_Enumeration_Type --
6951 ------------------------------------
6952
6953 procedure Build_Derived_Enumeration_Type
6954 (N : Node_Id;
6955 Parent_Type : Entity_Id;
6956 Derived_Type : Entity_Id)
6957 is
6958 Loc : constant Source_Ptr := Sloc (N);
6959 Def : constant Node_Id := Type_Definition (N);
6960 Indic : constant Node_Id := Subtype_Indication (Def);
6961 Implicit_Base : Entity_Id;
6962 Literal : Entity_Id;
6963 New_Lit : Entity_Id;
6964 Literals_List : List_Id;
6965 Type_Decl : Node_Id;
6966 Hi, Lo : Node_Id;
6967 Rang_Expr : Node_Id;
6968
6969 begin
6970 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6971 -- not have explicit literals lists we need to process types derived
6972 -- from them specially. This is handled by Derived_Standard_Character.
6973 -- If the parent type is a generic type, there are no literals either,
6974 -- and we construct the same skeletal representation as for the generic
6975 -- parent type.
6976
6977 if Is_Standard_Character_Type (Parent_Type) then
6978 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6979
6980 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6981 declare
6982 Lo : Node_Id;
6983 Hi : Node_Id;
6984
6985 begin
6986 if Nkind (Indic) /= N_Subtype_Indication then
6987 Lo :=
6988 Make_Attribute_Reference (Loc,
6989 Attribute_Name => Name_First,
6990 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6991 Set_Etype (Lo, Derived_Type);
6992
6993 Hi :=
6994 Make_Attribute_Reference (Loc,
6995 Attribute_Name => Name_Last,
6996 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6997 Set_Etype (Hi, Derived_Type);
6998
6999 Set_Scalar_Range (Derived_Type,
7000 Make_Range (Loc,
7001 Low_Bound => Lo,
7002 High_Bound => Hi));
7003 else
7004
7005 -- Analyze subtype indication and verify compatibility
7006 -- with parent type.
7007
7008 if Base_Type (Process_Subtype (Indic, N)) /=
7009 Base_Type (Parent_Type)
7010 then
7011 Error_Msg_N
7012 ("illegal constraint for formal discrete type", N);
7013 end if;
7014 end if;
7015 end;
7016
7017 else
7018 -- If a constraint is present, analyze the bounds to catch
7019 -- premature usage of the derived literals.
7020
7021 if Nkind (Indic) = N_Subtype_Indication
7022 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7023 then
7024 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7025 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7026 end if;
7027
7028 -- Introduce an implicit base type for the derived type even if there
7029 -- is no constraint attached to it, since this seems closer to the
7030 -- Ada semantics. Build a full type declaration tree for the derived
7031 -- type using the implicit base type as the defining identifier. The
7032 -- build a subtype declaration tree which applies the constraint (if
7033 -- any) have it replace the derived type declaration.
7034
7035 Literal := First_Literal (Parent_Type);
7036 Literals_List := New_List;
7037 while Present (Literal)
7038 and then Ekind (Literal) = E_Enumeration_Literal
7039 loop
7040 -- Literals of the derived type have the same representation as
7041 -- those of the parent type, but this representation can be
7042 -- overridden by an explicit representation clause. Indicate
7043 -- that there is no explicit representation given yet. These
7044 -- derived literals are implicit operations of the new type,
7045 -- and can be overridden by explicit ones.
7046
7047 if Nkind (Literal) = N_Defining_Character_Literal then
7048 New_Lit :=
7049 Make_Defining_Character_Literal (Loc, Chars (Literal));
7050 else
7051 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7052 end if;
7053
7054 Set_Ekind (New_Lit, E_Enumeration_Literal);
7055 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7056 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7057 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7058 Set_Alias (New_Lit, Literal);
7059 Set_Is_Known_Valid (New_Lit, True);
7060
7061 Append (New_Lit, Literals_List);
7062 Next_Literal (Literal);
7063 end loop;
7064
7065 Implicit_Base :=
7066 Make_Defining_Identifier (Sloc (Derived_Type),
7067 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7068
7069 -- Indicate the proper nature of the derived type. This must be done
7070 -- before analysis of the literals, to recognize cases when a literal
7071 -- may be hidden by a previous explicit function definition (cf.
7072 -- c83031a).
7073
7074 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7075 Set_Etype (Derived_Type, Implicit_Base);
7076
7077 Type_Decl :=
7078 Make_Full_Type_Declaration (Loc,
7079 Defining_Identifier => Implicit_Base,
7080 Discriminant_Specifications => No_List,
7081 Type_Definition =>
7082 Make_Enumeration_Type_Definition (Loc, Literals_List));
7083
7084 Mark_Rewrite_Insertion (Type_Decl);
7085 Insert_Before (N, Type_Decl);
7086 Analyze (Type_Decl);
7087
7088 -- The anonymous base now has a full declaration, but this base
7089 -- is not a first subtype.
7090
7091 Set_Is_First_Subtype (Implicit_Base, False);
7092
7093 -- After the implicit base is analyzed its Etype needs to be changed
7094 -- to reflect the fact that it is derived from the parent type which
7095 -- was ignored during analysis. We also set the size at this point.
7096
7097 Set_Etype (Implicit_Base, Parent_Type);
7098
7099 Set_Size_Info (Implicit_Base, Parent_Type);
7100 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7101 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7102
7103 -- Copy other flags from parent type
7104
7105 Set_Has_Non_Standard_Rep
7106 (Implicit_Base, Has_Non_Standard_Rep
7107 (Parent_Type));
7108 Set_Has_Pragma_Ordered
7109 (Implicit_Base, Has_Pragma_Ordered
7110 (Parent_Type));
7111 Set_Has_Delayed_Freeze (Implicit_Base);
7112
7113 -- Process the subtype indication including a validation check on the
7114 -- constraint, if any. If a constraint is given, its bounds must be
7115 -- implicitly converted to the new type.
7116
7117 if Nkind (Indic) = N_Subtype_Indication then
7118 declare
7119 R : constant Node_Id :=
7120 Range_Expression (Constraint (Indic));
7121
7122 begin
7123 if Nkind (R) = N_Range then
7124 Hi := Build_Scalar_Bound
7125 (High_Bound (R), Parent_Type, Implicit_Base);
7126 Lo := Build_Scalar_Bound
7127 (Low_Bound (R), Parent_Type, Implicit_Base);
7128
7129 else
7130 -- Constraint is a Range attribute. Replace with explicit
7131 -- mention of the bounds of the prefix, which must be a
7132 -- subtype.
7133
7134 Analyze (Prefix (R));
7135 Hi :=
7136 Convert_To (Implicit_Base,
7137 Make_Attribute_Reference (Loc,
7138 Attribute_Name => Name_Last,
7139 Prefix =>
7140 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7141
7142 Lo :=
7143 Convert_To (Implicit_Base,
7144 Make_Attribute_Reference (Loc,
7145 Attribute_Name => Name_First,
7146 Prefix =>
7147 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7148 end if;
7149 end;
7150
7151 else
7152 Hi :=
7153 Build_Scalar_Bound
7154 (Type_High_Bound (Parent_Type),
7155 Parent_Type, Implicit_Base);
7156 Lo :=
7157 Build_Scalar_Bound
7158 (Type_Low_Bound (Parent_Type),
7159 Parent_Type, Implicit_Base);
7160 end if;
7161
7162 Rang_Expr :=
7163 Make_Range (Loc,
7164 Low_Bound => Lo,
7165 High_Bound => Hi);
7166
7167 -- If we constructed a default range for the case where no range
7168 -- was given, then the expressions in the range must not freeze
7169 -- since they do not correspond to expressions in the source.
7170 -- However, if the type inherits predicates the expressions will
7171 -- be elaborated earlier and must freeze.
7172
7173 if Nkind (Indic) /= N_Subtype_Indication
7174 and then not Has_Predicates (Derived_Type)
7175 then
7176 Set_Must_Not_Freeze (Lo);
7177 Set_Must_Not_Freeze (Hi);
7178 Set_Must_Not_Freeze (Rang_Expr);
7179 end if;
7180
7181 Rewrite (N,
7182 Make_Subtype_Declaration (Loc,
7183 Defining_Identifier => Derived_Type,
7184 Subtype_Indication =>
7185 Make_Subtype_Indication (Loc,
7186 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7187 Constraint =>
7188 Make_Range_Constraint (Loc,
7189 Range_Expression => Rang_Expr))));
7190
7191 Analyze (N);
7192
7193 -- Propagate the aspects from the original type declaration to the
7194 -- declaration of the implicit base.
7195
7196 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7197
7198 -- Apply a range check. Since this range expression doesn't have an
7199 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7200 -- this right???
7201
7202 if Nkind (Indic) = N_Subtype_Indication then
7203 Apply_Range_Check
7204 (Range_Expression (Constraint (Indic)), Parent_Type,
7205 Source_Typ => Entity (Subtype_Mark (Indic)));
7206 end if;
7207 end if;
7208 end Build_Derived_Enumeration_Type;
7209
7210 --------------------------------
7211 -- Build_Derived_Numeric_Type --
7212 --------------------------------
7213
7214 procedure Build_Derived_Numeric_Type
7215 (N : Node_Id;
7216 Parent_Type : Entity_Id;
7217 Derived_Type : Entity_Id)
7218 is
7219 Loc : constant Source_Ptr := Sloc (N);
7220 Tdef : constant Node_Id := Type_Definition (N);
7221 Indic : constant Node_Id := Subtype_Indication (Tdef);
7222 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7223 No_Constraint : constant Boolean := Nkind (Indic) /=
7224 N_Subtype_Indication;
7225 Implicit_Base : Entity_Id;
7226
7227 Lo : Node_Id;
7228 Hi : Node_Id;
7229
7230 begin
7231 -- Process the subtype indication including a validation check on
7232 -- the constraint if any.
7233
7234 Discard_Node (Process_Subtype (Indic, N));
7235
7236 -- Introduce an implicit base type for the derived type even if there
7237 -- is no constraint attached to it, since this seems closer to the Ada
7238 -- semantics.
7239
7240 Implicit_Base :=
7241 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7242
7243 Set_Etype (Implicit_Base, Parent_Base);
7244 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7245 Set_Size_Info (Implicit_Base, Parent_Base);
7246 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7247 Set_Parent (Implicit_Base, Parent (Derived_Type));
7248 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7249
7250 -- Set RM Size for discrete type or decimal fixed-point type
7251 -- Ordinary fixed-point is excluded, why???
7252
7253 if Is_Discrete_Type (Parent_Base)
7254 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7255 then
7256 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7257 end if;
7258
7259 Set_Has_Delayed_Freeze (Implicit_Base);
7260
7261 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7262 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7263
7264 Set_Scalar_Range (Implicit_Base,
7265 Make_Range (Loc,
7266 Low_Bound => Lo,
7267 High_Bound => Hi));
7268
7269 if Has_Infinities (Parent_Base) then
7270 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7271 end if;
7272
7273 -- The Derived_Type, which is the entity of the declaration, is a
7274 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7275 -- absence of an explicit constraint.
7276
7277 Set_Etype (Derived_Type, Implicit_Base);
7278
7279 -- If we did not have a constraint, then the Ekind is set from the
7280 -- parent type (otherwise Process_Subtype has set the bounds)
7281
7282 if No_Constraint then
7283 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7284 end if;
7285
7286 -- If we did not have a range constraint, then set the range from the
7287 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7288
7289 if No_Constraint or else not Has_Range_Constraint (Indic) then
7290 Set_Scalar_Range (Derived_Type,
7291 Make_Range (Loc,
7292 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7293 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7294 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7295
7296 if Has_Infinities (Parent_Type) then
7297 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7298 end if;
7299
7300 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7301 end if;
7302
7303 Set_Is_Descendant_Of_Address (Derived_Type,
7304 Is_Descendant_Of_Address (Parent_Type));
7305 Set_Is_Descendant_Of_Address (Implicit_Base,
7306 Is_Descendant_Of_Address (Parent_Type));
7307
7308 -- Set remaining type-specific fields, depending on numeric type
7309
7310 if Is_Modular_Integer_Type (Parent_Type) then
7311 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7312
7313 Set_Non_Binary_Modulus
7314 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7315
7316 Set_Is_Known_Valid
7317 (Implicit_Base, Is_Known_Valid (Parent_Base));
7318
7319 elsif Is_Floating_Point_Type (Parent_Type) then
7320
7321 -- Digits of base type is always copied from the digits value of
7322 -- the parent base type, but the digits of the derived type will
7323 -- already have been set if there was a constraint present.
7324
7325 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7326 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7327
7328 if No_Constraint then
7329 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7330 end if;
7331
7332 elsif Is_Fixed_Point_Type (Parent_Type) then
7333
7334 -- Small of base type and derived type are always copied from the
7335 -- parent base type, since smalls never change. The delta of the
7336 -- base type is also copied from the parent base type. However the
7337 -- delta of the derived type will have been set already if a
7338 -- constraint was present.
7339
7340 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7341 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7342 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7343
7344 if No_Constraint then
7345 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7346 end if;
7347
7348 -- The scale and machine radix in the decimal case are always
7349 -- copied from the parent base type.
7350
7351 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7352 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7353 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7354
7355 Set_Machine_Radix_10
7356 (Derived_Type, Machine_Radix_10 (Parent_Base));
7357 Set_Machine_Radix_10
7358 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7359
7360 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7361
7362 if No_Constraint then
7363 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7364
7365 else
7366 -- the analysis of the subtype_indication sets the
7367 -- digits value of the derived type.
7368
7369 null;
7370 end if;
7371 end if;
7372 end if;
7373
7374 if Is_Integer_Type (Parent_Type) then
7375 Set_Has_Shift_Operator
7376 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7377 end if;
7378
7379 -- The type of the bounds is that of the parent type, and they
7380 -- must be converted to the derived type.
7381
7382 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7383
7384 -- The implicit_base should be frozen when the derived type is frozen,
7385 -- but note that it is used in the conversions of the bounds. For fixed
7386 -- types we delay the determination of the bounds until the proper
7387 -- freezing point. For other numeric types this is rejected by GCC, for
7388 -- reasons that are currently unclear (???), so we choose to freeze the
7389 -- implicit base now. In the case of integers and floating point types
7390 -- this is harmless because subsequent representation clauses cannot
7391 -- affect anything, but it is still baffling that we cannot use the
7392 -- same mechanism for all derived numeric types.
7393
7394 -- There is a further complication: actually some representation
7395 -- clauses can affect the implicit base type. For example, attribute
7396 -- definition clauses for stream-oriented attributes need to set the
7397 -- corresponding TSS entries on the base type, and this normally
7398 -- cannot be done after the base type is frozen, so the circuitry in
7399 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7400 -- and not use Set_TSS in this case.
7401
7402 -- There are also consequences for the case of delayed representation
7403 -- aspects for some cases. For example, a Size aspect is delayed and
7404 -- should not be evaluated to the freeze point. This early freezing
7405 -- means that the size attribute evaluation happens too early???
7406
7407 if Is_Fixed_Point_Type (Parent_Type) then
7408 Conditional_Delay (Implicit_Base, Parent_Type);
7409 else
7410 Freeze_Before (N, Implicit_Base);
7411 end if;
7412 end Build_Derived_Numeric_Type;
7413
7414 --------------------------------
7415 -- Build_Derived_Private_Type --
7416 --------------------------------
7417
7418 procedure Build_Derived_Private_Type
7419 (N : Node_Id;
7420 Parent_Type : Entity_Id;
7421 Derived_Type : Entity_Id;
7422 Is_Completion : Boolean;
7423 Derive_Subps : Boolean := True)
7424 is
7425 Loc : constant Source_Ptr := Sloc (N);
7426 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7427 Par_Scope : constant Entity_Id := Scope (Par_Base);
7428 Full_N : constant Node_Id := New_Copy_Tree (N);
7429 Full_Der : Entity_Id := New_Copy (Derived_Type);
7430 Full_P : Entity_Id;
7431
7432 procedure Build_Full_Derivation;
7433 -- Build full derivation, i.e. derive from the full view
7434
7435 procedure Copy_And_Build;
7436 -- Copy derived type declaration, replace parent with its full view,
7437 -- and build derivation
7438
7439 ---------------------------
7440 -- Build_Full_Derivation --
7441 ---------------------------
7442
7443 procedure Build_Full_Derivation is
7444 begin
7445 -- If parent scope is not open, install the declarations
7446
7447 if not In_Open_Scopes (Par_Scope) then
7448 Install_Private_Declarations (Par_Scope);
7449 Install_Visible_Declarations (Par_Scope);
7450 Copy_And_Build;
7451 Uninstall_Declarations (Par_Scope);
7452
7453 -- If parent scope is open and in another unit, and parent has a
7454 -- completion, then the derivation is taking place in the visible
7455 -- part of a child unit. In that case retrieve the full view of
7456 -- the parent momentarily.
7457
7458 elsif not In_Same_Source_Unit (N, Parent_Type) then
7459 Full_P := Full_View (Parent_Type);
7460 Exchange_Declarations (Parent_Type);
7461 Copy_And_Build;
7462 Exchange_Declarations (Full_P);
7463
7464 -- Otherwise it is a local derivation
7465
7466 else
7467 Copy_And_Build;
7468 end if;
7469 end Build_Full_Derivation;
7470
7471 --------------------
7472 -- Copy_And_Build --
7473 --------------------
7474
7475 procedure Copy_And_Build is
7476 Full_Parent : Entity_Id := Parent_Type;
7477
7478 begin
7479 -- If the parent is itself derived from another private type,
7480 -- installing the private declarations has not affected its
7481 -- privacy status, so use its own full view explicitly.
7482
7483 if Is_Private_Type (Full_Parent)
7484 and then Present (Full_View (Full_Parent))
7485 then
7486 Full_Parent := Full_View (Full_Parent);
7487 end if;
7488
7489 -- And its underlying full view if necessary
7490
7491 if Is_Private_Type (Full_Parent)
7492 and then Present (Underlying_Full_View (Full_Parent))
7493 then
7494 Full_Parent := Underlying_Full_View (Full_Parent);
7495 end if;
7496
7497 -- For record, access and most enumeration types, derivation from
7498 -- the full view requires a fully-fledged declaration. In the other
7499 -- cases, just use an itype.
7500
7501 if Ekind (Full_Parent) in Record_Kind
7502 or else Ekind (Full_Parent) in Access_Kind
7503 or else
7504 (Ekind (Full_Parent) in Enumeration_Kind
7505 and then not Is_Standard_Character_Type (Full_Parent)
7506 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7507 then
7508 -- Copy and adjust declaration to provide a completion for what
7509 -- is originally a private declaration. Indicate that full view
7510 -- is internally generated.
7511
7512 Set_Comes_From_Source (Full_N, False);
7513 Set_Comes_From_Source (Full_Der, False);
7514 Set_Parent (Full_Der, Full_N);
7515 Set_Defining_Identifier (Full_N, Full_Der);
7516
7517 -- If there are no constraints, adjust the subtype mark
7518
7519 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7520 N_Subtype_Indication
7521 then
7522 Set_Subtype_Indication
7523 (Type_Definition (Full_N),
7524 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7525 end if;
7526
7527 Insert_After (N, Full_N);
7528
7529 -- Build full view of derived type from full view of parent which
7530 -- is now installed. Subprograms have been derived on the partial
7531 -- view, the completion does not derive them anew.
7532
7533 if Ekind (Full_Parent) in Record_Kind then
7534
7535 -- If parent type is tagged, the completion inherits the proper
7536 -- primitive operations.
7537
7538 if Is_Tagged_Type (Parent_Type) then
7539 Build_Derived_Record_Type
7540 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7541 else
7542 Build_Derived_Record_Type
7543 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7544 end if;
7545
7546 else
7547 Build_Derived_Type
7548 (Full_N, Full_Parent, Full_Der,
7549 Is_Completion => False, Derive_Subps => False);
7550 end if;
7551
7552 -- The full declaration has been introduced into the tree and
7553 -- processed in the step above. It should not be analyzed again
7554 -- (when encountered later in the current list of declarations)
7555 -- to prevent spurious name conflicts. The full entity remains
7556 -- invisible.
7557
7558 Set_Analyzed (Full_N);
7559
7560 else
7561 Full_Der :=
7562 Make_Defining_Identifier (Sloc (Derived_Type),
7563 Chars => Chars (Derived_Type));
7564 Set_Is_Itype (Full_Der);
7565 Set_Associated_Node_For_Itype (Full_Der, N);
7566 Set_Parent (Full_Der, N);
7567 Build_Derived_Type
7568 (N, Full_Parent, Full_Der,
7569 Is_Completion => False, Derive_Subps => False);
7570 end if;
7571
7572 Set_Has_Private_Declaration (Full_Der);
7573 Set_Has_Private_Declaration (Derived_Type);
7574
7575 Set_Scope (Full_Der, Scope (Derived_Type));
7576 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7577 Set_Has_Size_Clause (Full_Der, False);
7578 Set_Has_Alignment_Clause (Full_Der, False);
7579 Set_Has_Delayed_Freeze (Full_Der);
7580 Set_Is_Frozen (Full_Der, False);
7581 Set_Freeze_Node (Full_Der, Empty);
7582 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7583 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7584
7585 -- The convention on the base type may be set in the private part
7586 -- and not propagated to the subtype until later, so we obtain the
7587 -- convention from the base type of the parent.
7588
7589 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7590 end Copy_And_Build;
7591
7592 -- Start of processing for Build_Derived_Private_Type
7593
7594 begin
7595 if Is_Tagged_Type (Parent_Type) then
7596 Full_P := Full_View (Parent_Type);
7597
7598 -- A type extension of a type with unknown discriminants is an
7599 -- indefinite type that the back-end cannot handle directly.
7600 -- We treat it as a private type, and build a completion that is
7601 -- derived from the full view of the parent, and hopefully has
7602 -- known discriminants.
7603
7604 -- If the full view of the parent type has an underlying record view,
7605 -- use it to generate the underlying record view of this derived type
7606 -- (required for chains of derivations with unknown discriminants).
7607
7608 -- Minor optimization: we avoid the generation of useless underlying
7609 -- record view entities if the private type declaration has unknown
7610 -- discriminants but its corresponding full view has no
7611 -- discriminants.
7612
7613 if Has_Unknown_Discriminants (Parent_Type)
7614 and then Present (Full_P)
7615 and then (Has_Discriminants (Full_P)
7616 or else Present (Underlying_Record_View (Full_P)))
7617 and then not In_Open_Scopes (Par_Scope)
7618 and then Expander_Active
7619 then
7620 declare
7621 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7622 New_Ext : constant Node_Id :=
7623 Copy_Separate_Tree
7624 (Record_Extension_Part (Type_Definition (N)));
7625 Decl : Node_Id;
7626
7627 begin
7628 Build_Derived_Record_Type
7629 (N, Parent_Type, Derived_Type, Derive_Subps);
7630
7631 -- Build anonymous completion, as a derivation from the full
7632 -- view of the parent. This is not a completion in the usual
7633 -- sense, because the current type is not private.
7634
7635 Decl :=
7636 Make_Full_Type_Declaration (Loc,
7637 Defining_Identifier => Full_Der,
7638 Type_Definition =>
7639 Make_Derived_Type_Definition (Loc,
7640 Subtype_Indication =>
7641 New_Copy_Tree
7642 (Subtype_Indication (Type_Definition (N))),
7643 Record_Extension_Part => New_Ext));
7644
7645 -- If the parent type has an underlying record view, use it
7646 -- here to build the new underlying record view.
7647
7648 if Present (Underlying_Record_View (Full_P)) then
7649 pragma Assert
7650 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7651 = N_Identifier);
7652 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7653 Underlying_Record_View (Full_P));
7654 end if;
7655
7656 Install_Private_Declarations (Par_Scope);
7657 Install_Visible_Declarations (Par_Scope);
7658 Insert_Before (N, Decl);
7659
7660 -- Mark entity as an underlying record view before analysis,
7661 -- to avoid generating the list of its primitive operations
7662 -- (which is not really required for this entity) and thus
7663 -- prevent spurious errors associated with missing overriding
7664 -- of abstract primitives (overridden only for Derived_Type).
7665
7666 Set_Ekind (Full_Der, E_Record_Type);
7667 Set_Is_Underlying_Record_View (Full_Der);
7668 Set_Default_SSO (Full_Der);
7669
7670 Analyze (Decl);
7671
7672 pragma Assert (Has_Discriminants (Full_Der)
7673 and then not Has_Unknown_Discriminants (Full_Der));
7674
7675 Uninstall_Declarations (Par_Scope);
7676
7677 -- Freeze the underlying record view, to prevent generation of
7678 -- useless dispatching information, which is simply shared with
7679 -- the real derived type.
7680
7681 Set_Is_Frozen (Full_Der);
7682
7683 -- If the derived type has access discriminants, create
7684 -- references to their anonymous types now, to prevent
7685 -- back-end problems when their first use is in generated
7686 -- bodies of primitives.
7687
7688 declare
7689 E : Entity_Id;
7690
7691 begin
7692 E := First_Entity (Full_Der);
7693
7694 while Present (E) loop
7695 if Ekind (E) = E_Discriminant
7696 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7697 then
7698 Build_Itype_Reference (Etype (E), Decl);
7699 end if;
7700
7701 Next_Entity (E);
7702 end loop;
7703 end;
7704
7705 -- Set up links between real entity and underlying record view
7706
7707 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7708 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7709 end;
7710
7711 -- If discriminants are known, build derived record
7712
7713 else
7714 Build_Derived_Record_Type
7715 (N, Parent_Type, Derived_Type, Derive_Subps);
7716 end if;
7717
7718 return;
7719
7720 elsif Has_Discriminants (Parent_Type) then
7721
7722 -- Build partial view of derived type from partial view of parent.
7723 -- This must be done before building the full derivation because the
7724 -- second derivation will modify the discriminants of the first and
7725 -- the discriminants are chained with the rest of the components in
7726 -- the full derivation.
7727
7728 Build_Derived_Record_Type
7729 (N, Parent_Type, Derived_Type, Derive_Subps);
7730
7731 -- Build the full derivation if this is not the anonymous derived
7732 -- base type created by Build_Derived_Record_Type in the constrained
7733 -- case (see point 5. of its head comment) since we build it for the
7734 -- derived subtype. And skip it for protected types altogether, as
7735 -- gigi does not use these types directly.
7736
7737 if Present (Full_View (Parent_Type))
7738 and then not Is_Itype (Derived_Type)
7739 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7740 then
7741 declare
7742 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7743 Discr : Entity_Id;
7744 Last_Discr : Entity_Id;
7745
7746 begin
7747 -- If this is not a completion, construct the implicit full
7748 -- view by deriving from the full view of the parent type.
7749 -- But if this is a completion, the derived private type
7750 -- being built is a full view and the full derivation can
7751 -- only be its underlying full view.
7752
7753 Build_Full_Derivation;
7754
7755 if not Is_Completion then
7756 Set_Full_View (Derived_Type, Full_Der);
7757 else
7758 Set_Underlying_Full_View (Derived_Type, Full_Der);
7759 Set_Is_Underlying_Full_View (Full_Der);
7760 end if;
7761
7762 if not Is_Base_Type (Derived_Type) then
7763 Set_Full_View (Der_Base, Base_Type (Full_Der));
7764 end if;
7765
7766 -- Copy the discriminant list from full view to the partial
7767 -- view (base type and its subtype). Gigi requires that the
7768 -- partial and full views have the same discriminants.
7769
7770 -- Note that since the partial view points to discriminants
7771 -- in the full view, their scope will be that of the full
7772 -- view. This might cause some front end problems and need
7773 -- adjustment???
7774
7775 Discr := First_Discriminant (Base_Type (Full_Der));
7776 Set_First_Entity (Der_Base, Discr);
7777
7778 loop
7779 Last_Discr := Discr;
7780 Next_Discriminant (Discr);
7781 exit when No (Discr);
7782 end loop;
7783
7784 Set_Last_Entity (Der_Base, Last_Discr);
7785 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7786 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7787
7788 Set_Stored_Constraint
7789 (Full_Der, Stored_Constraint (Derived_Type));
7790 end;
7791 end if;
7792
7793 elsif Present (Full_View (Parent_Type))
7794 and then Has_Discriminants (Full_View (Parent_Type))
7795 then
7796 if Has_Unknown_Discriminants (Parent_Type)
7797 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7798 N_Subtype_Indication
7799 then
7800 Error_Msg_N
7801 ("cannot constrain type with unknown discriminants",
7802 Subtype_Indication (Type_Definition (N)));
7803 return;
7804 end if;
7805
7806 -- If this is not a completion, construct the implicit full view by
7807 -- deriving from the full view of the parent type. But if this is a
7808 -- completion, the derived private type being built is a full view
7809 -- and the full derivation can only be its underlying full view.
7810
7811 Build_Full_Derivation;
7812
7813 if not Is_Completion then
7814 Set_Full_View (Derived_Type, Full_Der);
7815 else
7816 Set_Underlying_Full_View (Derived_Type, Full_Der);
7817 Set_Is_Underlying_Full_View (Full_Der);
7818 end if;
7819
7820 -- In any case, the primitive operations are inherited from the
7821 -- parent type, not from the internal full view.
7822
7823 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7824
7825 if Derive_Subps then
7826 Derive_Subprograms (Parent_Type, Derived_Type);
7827 end if;
7828
7829 Set_Stored_Constraint (Derived_Type, No_Elist);
7830 Set_Is_Constrained
7831 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7832
7833 else
7834 -- Untagged type, No discriminants on either view
7835
7836 if Nkind (Subtype_Indication (Type_Definition (N))) =
7837 N_Subtype_Indication
7838 then
7839 Error_Msg_N
7840 ("illegal constraint on type without discriminants", N);
7841 end if;
7842
7843 if Present (Discriminant_Specifications (N))
7844 and then Present (Full_View (Parent_Type))
7845 and then not Is_Tagged_Type (Full_View (Parent_Type))
7846 then
7847 Error_Msg_N ("cannot add discriminants to untagged type", N);
7848 end if;
7849
7850 Set_Stored_Constraint (Derived_Type, No_Elist);
7851 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7852 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7853 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7854 (Parent_Type));
7855 Set_Has_Controlled_Component
7856 (Derived_Type, Has_Controlled_Component
7857 (Parent_Type));
7858
7859 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7860
7861 if not Is_Controlled_Active (Parent_Type) then
7862 Set_Finalize_Storage_Only
7863 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7864 end if;
7865
7866 -- If this is not a completion, construct the implicit full view by
7867 -- deriving from the full view of the parent type.
7868
7869 -- ??? If the parent is untagged private and its completion is
7870 -- tagged, this mechanism will not work because we cannot derive from
7871 -- the tagged full view unless we have an extension.
7872
7873 if Present (Full_View (Parent_Type))
7874 and then not Is_Tagged_Type (Full_View (Parent_Type))
7875 and then not Is_Completion
7876 then
7877 Build_Full_Derivation;
7878 Set_Full_View (Derived_Type, Full_Der);
7879 end if;
7880 end if;
7881
7882 Set_Has_Unknown_Discriminants (Derived_Type,
7883 Has_Unknown_Discriminants (Parent_Type));
7884
7885 if Is_Private_Type (Derived_Type) then
7886 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7887 end if;
7888
7889 -- If the parent base type is in scope, add the derived type to its
7890 -- list of private dependents, because its full view may become
7891 -- visible subsequently (in a nested private part, a body, or in a
7892 -- further child unit).
7893
7894 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7895 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7896
7897 -- Check for unusual case where a type completed by a private
7898 -- derivation occurs within a package nested in a child unit, and
7899 -- the parent is declared in an ancestor.
7900
7901 if Is_Child_Unit (Scope (Current_Scope))
7902 and then Is_Completion
7903 and then In_Private_Part (Current_Scope)
7904 and then Scope (Parent_Type) /= Current_Scope
7905
7906 -- Note that if the parent has a completion in the private part,
7907 -- (which is itself a derivation from some other private type)
7908 -- it is that completion that is visible, there is no full view
7909 -- available, and no special processing is needed.
7910
7911 and then Present (Full_View (Parent_Type))
7912 then
7913 -- In this case, the full view of the parent type will become
7914 -- visible in the body of the enclosing child, and only then will
7915 -- the current type be possibly non-private. Build an underlying
7916 -- full view that will be installed when the enclosing child body
7917 -- is compiled.
7918
7919 if Present (Underlying_Full_View (Derived_Type)) then
7920 Full_Der := Underlying_Full_View (Derived_Type);
7921 else
7922 Build_Full_Derivation;
7923 Set_Underlying_Full_View (Derived_Type, Full_Der);
7924 Set_Is_Underlying_Full_View (Full_Der);
7925 end if;
7926
7927 -- The full view will be used to swap entities on entry/exit to
7928 -- the body, and must appear in the entity list for the package.
7929
7930 Append_Entity (Full_Der, Scope (Derived_Type));
7931 end if;
7932 end if;
7933 end Build_Derived_Private_Type;
7934
7935 -------------------------------
7936 -- Build_Derived_Record_Type --
7937 -------------------------------
7938
7939 -- 1. INTRODUCTION
7940
7941 -- Ideally we would like to use the same model of type derivation for
7942 -- tagged and untagged record types. Unfortunately this is not quite
7943 -- possible because the semantics of representation clauses is different
7944 -- for tagged and untagged records under inheritance. Consider the
7945 -- following:
7946
7947 -- type R (...) is [tagged] record ... end record;
7948 -- type T (...) is new R (...) [with ...];
7949
7950 -- The representation clauses for T can specify a completely different
7951 -- record layout from R's. Hence the same component can be placed in two
7952 -- very different positions in objects of type T and R. If R and T are
7953 -- tagged types, representation clauses for T can only specify the layout
7954 -- of non inherited components, thus components that are common in R and T
7955 -- have the same position in objects of type R and T.
7956
7957 -- This has two implications. The first is that the entire tree for R's
7958 -- declaration needs to be copied for T in the untagged case, so that T
7959 -- can be viewed as a record type of its own with its own representation
7960 -- clauses. The second implication is the way we handle discriminants.
7961 -- Specifically, in the untagged case we need a way to communicate to Gigi
7962 -- what are the real discriminants in the record, while for the semantics
7963 -- we need to consider those introduced by the user to rename the
7964 -- discriminants in the parent type. This is handled by introducing the
7965 -- notion of stored discriminants. See below for more.
7966
7967 -- Fortunately the way regular components are inherited can be handled in
7968 -- the same way in tagged and untagged types.
7969
7970 -- To complicate things a bit more the private view of a private extension
7971 -- cannot be handled in the same way as the full view (for one thing the
7972 -- semantic rules are somewhat different). We will explain what differs
7973 -- below.
7974
7975 -- 2. DISCRIMINANTS UNDER INHERITANCE
7976
7977 -- The semantic rules governing the discriminants of derived types are
7978 -- quite subtle.
7979
7980 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7981 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7982
7983 -- If parent type has discriminants, then the discriminants that are
7984 -- declared in the derived type are [3.4 (11)]:
7985
7986 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7987 -- there is one;
7988
7989 -- o Otherwise, each discriminant of the parent type (implicitly declared
7990 -- in the same order with the same specifications). In this case, the
7991 -- discriminants are said to be "inherited", or if unknown in the parent
7992 -- are also unknown in the derived type.
7993
7994 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7995
7996 -- o The parent subtype must be constrained;
7997
7998 -- o If the parent type is not a tagged type, then each discriminant of
7999 -- the derived type must be used in the constraint defining a parent
8000 -- subtype. [Implementation note: This ensures that the new discriminant
8001 -- can share storage with an existing discriminant.]
8002
8003 -- For the derived type each discriminant of the parent type is either
8004 -- inherited, constrained to equal some new discriminant of the derived
8005 -- type, or constrained to the value of an expression.
8006
8007 -- When inherited or constrained to equal some new discriminant, the
8008 -- parent discriminant and the discriminant of the derived type are said
8009 -- to "correspond".
8010
8011 -- If a discriminant of the parent type is constrained to a specific value
8012 -- in the derived type definition, then the discriminant is said to be
8013 -- "specified" by that derived type definition.
8014
8015 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8016
8017 -- We have spoken about stored discriminants in point 1 (introduction)
8018 -- above. There are two sort of stored discriminants: implicit and
8019 -- explicit. As long as the derived type inherits the same discriminants as
8020 -- the root record type, stored discriminants are the same as regular
8021 -- discriminants, and are said to be implicit. However, if any discriminant
8022 -- in the root type was renamed in the derived type, then the derived
8023 -- type will contain explicit stored discriminants. Explicit stored
8024 -- discriminants are discriminants in addition to the semantically visible
8025 -- discriminants defined for the derived type. Stored discriminants are
8026 -- used by Gigi to figure out what are the physical discriminants in
8027 -- objects of the derived type (see precise definition in einfo.ads).
8028 -- As an example, consider the following:
8029
8030 -- type R (D1, D2, D3 : Int) is record ... end record;
8031 -- type T1 is new R;
8032 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8033 -- type T3 is new T2;
8034 -- type T4 (Y : Int) is new T3 (Y, 99);
8035
8036 -- The following table summarizes the discriminants and stored
8037 -- discriminants in R and T1 through T4.
8038
8039 -- Type Discrim Stored Discrim Comment
8040 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8041 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8042 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8043 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8044 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8045
8046 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8047 -- find the corresponding discriminant in the parent type, while
8048 -- Original_Record_Component (abbreviated ORC below), the actual physical
8049 -- component that is renamed. Finally the field Is_Completely_Hidden
8050 -- (abbreviated ICH below) is set for all explicit stored discriminants
8051 -- (see einfo.ads for more info). For the above example this gives:
8052
8053 -- Discrim CD ORC ICH
8054 -- ^^^^^^^ ^^ ^^^ ^^^
8055 -- D1 in R empty itself no
8056 -- D2 in R empty itself no
8057 -- D3 in R empty itself no
8058
8059 -- D1 in T1 D1 in R itself no
8060 -- D2 in T1 D2 in R itself no
8061 -- D3 in T1 D3 in R itself no
8062
8063 -- X1 in T2 D3 in T1 D3 in T2 no
8064 -- X2 in T2 D1 in T1 D1 in T2 no
8065 -- D1 in T2 empty itself yes
8066 -- D2 in T2 empty itself yes
8067 -- D3 in T2 empty itself yes
8068
8069 -- X1 in T3 X1 in T2 D3 in T3 no
8070 -- X2 in T3 X2 in T2 D1 in T3 no
8071 -- D1 in T3 empty itself yes
8072 -- D2 in T3 empty itself yes
8073 -- D3 in T3 empty itself yes
8074
8075 -- Y in T4 X1 in T3 D3 in T3 no
8076 -- D1 in T3 empty itself yes
8077 -- D2 in T3 empty itself yes
8078 -- D3 in T3 empty itself yes
8079
8080 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8081
8082 -- Type derivation for tagged types is fairly straightforward. If no
8083 -- discriminants are specified by the derived type, these are inherited
8084 -- from the parent. No explicit stored discriminants are ever necessary.
8085 -- The only manipulation that is done to the tree is that of adding a
8086 -- _parent field with parent type and constrained to the same constraint
8087 -- specified for the parent in the derived type definition. For instance:
8088
8089 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8090 -- type T1 is new R with null record;
8091 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8092
8093 -- are changed into:
8094
8095 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8096 -- _parent : R (D1, D2, D3);
8097 -- end record;
8098
8099 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8100 -- _parent : T1 (X2, 88, X1);
8101 -- end record;
8102
8103 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8104 -- ORC and ICH fields are:
8105
8106 -- Discrim CD ORC ICH
8107 -- ^^^^^^^ ^^ ^^^ ^^^
8108 -- D1 in R empty itself no
8109 -- D2 in R empty itself no
8110 -- D3 in R empty itself no
8111
8112 -- D1 in T1 D1 in R D1 in R no
8113 -- D2 in T1 D2 in R D2 in R no
8114 -- D3 in T1 D3 in R D3 in R no
8115
8116 -- X1 in T2 D3 in T1 D3 in R no
8117 -- X2 in T2 D1 in T1 D1 in R no
8118
8119 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8120 --
8121 -- Regardless of whether we dealing with a tagged or untagged type
8122 -- we will transform all derived type declarations of the form
8123 --
8124 -- type T is new R (...) [with ...];
8125 -- or
8126 -- subtype S is R (...);
8127 -- type T is new S [with ...];
8128 -- into
8129 -- type BT is new R [with ...];
8130 -- subtype T is BT (...);
8131 --
8132 -- That is, the base derived type is constrained only if it has no
8133 -- discriminants. The reason for doing this is that GNAT's semantic model
8134 -- assumes that a base type with discriminants is unconstrained.
8135 --
8136 -- Note that, strictly speaking, the above transformation is not always
8137 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8138 --
8139 -- procedure B34011A is
8140 -- type REC (D : integer := 0) is record
8141 -- I : Integer;
8142 -- end record;
8143
8144 -- package P is
8145 -- type T6 is new Rec;
8146 -- function F return T6;
8147 -- end P;
8148
8149 -- use P;
8150 -- package Q6 is
8151 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8152 -- end Q6;
8153 --
8154 -- The definition of Q6.U is illegal. However transforming Q6.U into
8155
8156 -- type BaseU is new T6;
8157 -- subtype U is BaseU (Q6.F.I)
8158
8159 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8160 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8161 -- the transformation described above.
8162
8163 -- There is another instance where the above transformation is incorrect.
8164 -- Consider:
8165
8166 -- package Pack is
8167 -- type Base (D : Integer) is tagged null record;
8168 -- procedure P (X : Base);
8169
8170 -- type Der is new Base (2) with null record;
8171 -- procedure P (X : Der);
8172 -- end Pack;
8173
8174 -- Then the above transformation turns this into
8175
8176 -- type Der_Base is new Base with null record;
8177 -- -- procedure P (X : Base) is implicitly inherited here
8178 -- -- as procedure P (X : Der_Base).
8179
8180 -- subtype Der is Der_Base (2);
8181 -- procedure P (X : Der);
8182 -- -- The overriding of P (X : Der_Base) is illegal since we
8183 -- -- have a parameter conformance problem.
8184
8185 -- To get around this problem, after having semantically processed Der_Base
8186 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8187 -- Discriminant_Constraint from Der so that when parameter conformance is
8188 -- checked when P is overridden, no semantic errors are flagged.
8189
8190 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8191
8192 -- Regardless of whether we are dealing with a tagged or untagged type
8193 -- we will transform all derived type declarations of the form
8194
8195 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8196 -- type T is new R [with ...];
8197 -- into
8198 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8199
8200 -- The reason for such transformation is that it allows us to implement a
8201 -- very clean form of component inheritance as explained below.
8202
8203 -- Note that this transformation is not achieved by direct tree rewriting
8204 -- and manipulation, but rather by redoing the semantic actions that the
8205 -- above transformation will entail. This is done directly in routine
8206 -- Inherit_Components.
8207
8208 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8209
8210 -- In both tagged and untagged derived types, regular non discriminant
8211 -- components are inherited in the derived type from the parent type. In
8212 -- the absence of discriminants component, inheritance is straightforward
8213 -- as components can simply be copied from the parent.
8214
8215 -- If the parent has discriminants, inheriting components constrained with
8216 -- these discriminants requires caution. Consider the following example:
8217
8218 -- type R (D1, D2 : Positive) is [tagged] record
8219 -- S : String (D1 .. D2);
8220 -- end record;
8221
8222 -- type T1 is new R [with null record];
8223 -- type T2 (X : positive) is new R (1, X) [with null record];
8224
8225 -- As explained in 6. above, T1 is rewritten as
8226 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8227 -- which makes the treatment for T1 and T2 identical.
8228
8229 -- What we want when inheriting S, is that references to D1 and D2 in R are
8230 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8231 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8232 -- with either discriminant references in the derived type or expressions.
8233 -- This replacement is achieved as follows: before inheriting R's
8234 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8235 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8236 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8237 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8238 -- by String (1 .. X).
8239
8240 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8241
8242 -- We explain here the rules governing private type extensions relevant to
8243 -- type derivation. These rules are explained on the following example:
8244
8245 -- type D [(...)] is new A [(...)] with private; <-- partial view
8246 -- type D [(...)] is new P [(...)] with null record; <-- full view
8247
8248 -- Type A is called the ancestor subtype of the private extension.
8249 -- Type P is the parent type of the full view of the private extension. It
8250 -- must be A or a type derived from A.
8251
8252 -- The rules concerning the discriminants of private type extensions are
8253 -- [7.3(10-13)]:
8254
8255 -- o If a private extension inherits known discriminants from the ancestor
8256 -- subtype, then the full view must also inherit its discriminants from
8257 -- the ancestor subtype and the parent subtype of the full view must be
8258 -- constrained if and only if the ancestor subtype is constrained.
8259
8260 -- o If a partial view has unknown discriminants, then the full view may
8261 -- define a definite or an indefinite subtype, with or without
8262 -- discriminants.
8263
8264 -- o If a partial view has neither known nor unknown discriminants, then
8265 -- the full view must define a definite subtype.
8266
8267 -- o If the ancestor subtype of a private extension has constrained
8268 -- discriminants, then the parent subtype of the full view must impose a
8269 -- statically matching constraint on those discriminants.
8270
8271 -- This means that only the following forms of private extensions are
8272 -- allowed:
8273
8274 -- type D is new A with private; <-- partial view
8275 -- type D is new P with null record; <-- full view
8276
8277 -- If A has no discriminants than P has no discriminants, otherwise P must
8278 -- inherit A's discriminants.
8279
8280 -- type D is new A (...) with private; <-- partial view
8281 -- type D is new P (:::) with null record; <-- full view
8282
8283 -- P must inherit A's discriminants and (...) and (:::) must statically
8284 -- match.
8285
8286 -- subtype A is R (...);
8287 -- type D is new A with private; <-- partial view
8288 -- type D is new P with null record; <-- full view
8289
8290 -- P must have inherited R's discriminants and must be derived from A or
8291 -- any of its subtypes.
8292
8293 -- type D (..) is new A with private; <-- partial view
8294 -- type D (..) is new P [(:::)] with null record; <-- full view
8295
8296 -- No specific constraints on P's discriminants or constraint (:::).
8297 -- Note that A can be unconstrained, but the parent subtype P must either
8298 -- be constrained or (:::) must be present.
8299
8300 -- type D (..) is new A [(...)] with private; <-- partial view
8301 -- type D (..) is new P [(:::)] with null record; <-- full view
8302
8303 -- P's constraints on A's discriminants must statically match those
8304 -- imposed by (...).
8305
8306 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8307
8308 -- The full view of a private extension is handled exactly as described
8309 -- above. The model chose for the private view of a private extension is
8310 -- the same for what concerns discriminants (i.e. they receive the same
8311 -- treatment as in the tagged case). However, the private view of the
8312 -- private extension always inherits the components of the parent base,
8313 -- without replacing any discriminant reference. Strictly speaking this is
8314 -- incorrect. However, Gigi never uses this view to generate code so this
8315 -- is a purely semantic issue. In theory, a set of transformations similar
8316 -- to those given in 5. and 6. above could be applied to private views of
8317 -- private extensions to have the same model of component inheritance as
8318 -- for non private extensions. However, this is not done because it would
8319 -- further complicate private type processing. Semantically speaking, this
8320 -- leaves us in an uncomfortable situation. As an example consider:
8321
8322 -- package Pack is
8323 -- type R (D : integer) is tagged record
8324 -- S : String (1 .. D);
8325 -- end record;
8326 -- procedure P (X : R);
8327 -- type T is new R (1) with private;
8328 -- private
8329 -- type T is new R (1) with null record;
8330 -- end;
8331
8332 -- This is transformed into:
8333
8334 -- package Pack is
8335 -- type R (D : integer) is tagged record
8336 -- S : String (1 .. D);
8337 -- end record;
8338 -- procedure P (X : R);
8339 -- type T is new R (1) with private;
8340 -- private
8341 -- type BaseT is new R with null record;
8342 -- subtype T is BaseT (1);
8343 -- end;
8344
8345 -- (strictly speaking the above is incorrect Ada)
8346
8347 -- From the semantic standpoint the private view of private extension T
8348 -- should be flagged as constrained since one can clearly have
8349 --
8350 -- Obj : T;
8351 --
8352 -- in a unit withing Pack. However, when deriving subprograms for the
8353 -- private view of private extension T, T must be seen as unconstrained
8354 -- since T has discriminants (this is a constraint of the current
8355 -- subprogram derivation model). Thus, when processing the private view of
8356 -- a private extension such as T, we first mark T as unconstrained, we
8357 -- process it, we perform program derivation and just before returning from
8358 -- Build_Derived_Record_Type we mark T as constrained.
8359
8360 -- ??? Are there are other uncomfortable cases that we will have to
8361 -- deal with.
8362
8363 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8364
8365 -- Types that are derived from a visible record type and have a private
8366 -- extension present other peculiarities. They behave mostly like private
8367 -- types, but if they have primitive operations defined, these will not
8368 -- have the proper signatures for further inheritance, because other
8369 -- primitive operations will use the implicit base that we define for
8370 -- private derivations below. This affect subprogram inheritance (see
8371 -- Derive_Subprograms for details). We also derive the implicit base from
8372 -- the base type of the full view, so that the implicit base is a record
8373 -- type and not another private type, This avoids infinite loops.
8374
8375 procedure Build_Derived_Record_Type
8376 (N : Node_Id;
8377 Parent_Type : Entity_Id;
8378 Derived_Type : Entity_Id;
8379 Derive_Subps : Boolean := True)
8380 is
8381 Discriminant_Specs : constant Boolean :=
8382 Present (Discriminant_Specifications (N));
8383 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8384 Loc : constant Source_Ptr := Sloc (N);
8385 Private_Extension : constant Boolean :=
8386 Nkind (N) = N_Private_Extension_Declaration;
8387 Assoc_List : Elist_Id;
8388 Constraint_Present : Boolean;
8389 Constrs : Elist_Id;
8390 Discrim : Entity_Id;
8391 Indic : Node_Id;
8392 Inherit_Discrims : Boolean := False;
8393 Last_Discrim : Entity_Id;
8394 New_Base : Entity_Id;
8395 New_Decl : Node_Id;
8396 New_Discrs : Elist_Id;
8397 New_Indic : Node_Id;
8398 Parent_Base : Entity_Id;
8399 Save_Etype : Entity_Id;
8400 Save_Discr_Constr : Elist_Id;
8401 Save_Next_Entity : Entity_Id;
8402 Type_Def : Node_Id;
8403
8404 Discs : Elist_Id := New_Elmt_List;
8405 -- An empty Discs list means that there were no constraints in the
8406 -- subtype indication or that there was an error processing it.
8407
8408 begin
8409 if Ekind (Parent_Type) = E_Record_Type_With_Private
8410 and then Present (Full_View (Parent_Type))
8411 and then Has_Discriminants (Parent_Type)
8412 then
8413 Parent_Base := Base_Type (Full_View (Parent_Type));
8414 else
8415 Parent_Base := Base_Type (Parent_Type);
8416 end if;
8417
8418 -- AI05-0115 : if this is a derivation from a private type in some
8419 -- other scope that may lead to invisible components for the derived
8420 -- type, mark it accordingly.
8421
8422 if Is_Private_Type (Parent_Type) then
8423 if Scope (Parent_Type) = Scope (Derived_Type) then
8424 null;
8425
8426 elsif In_Open_Scopes (Scope (Parent_Type))
8427 and then In_Private_Part (Scope (Parent_Type))
8428 then
8429 null;
8430
8431 else
8432 Set_Has_Private_Ancestor (Derived_Type);
8433 end if;
8434
8435 else
8436 Set_Has_Private_Ancestor
8437 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8438 end if;
8439
8440 -- Before we start the previously documented transformations, here is
8441 -- little fix for size and alignment of tagged types. Normally when we
8442 -- derive type D from type P, we copy the size and alignment of P as the
8443 -- default for D, and in the absence of explicit representation clauses
8444 -- for D, the size and alignment are indeed the same as the parent.
8445
8446 -- But this is wrong for tagged types, since fields may be added, and
8447 -- the default size may need to be larger, and the default alignment may
8448 -- need to be larger.
8449
8450 -- We therefore reset the size and alignment fields in the tagged case.
8451 -- Note that the size and alignment will in any case be at least as
8452 -- large as the parent type (since the derived type has a copy of the
8453 -- parent type in the _parent field)
8454
8455 -- The type is also marked as being tagged here, which is needed when
8456 -- processing components with a self-referential anonymous access type
8457 -- in the call to Check_Anonymous_Access_Components below. Note that
8458 -- this flag is also set later on for completeness.
8459
8460 if Is_Tagged then
8461 Set_Is_Tagged_Type (Derived_Type);
8462 Init_Size_Align (Derived_Type);
8463 end if;
8464
8465 -- STEP 0a: figure out what kind of derived type declaration we have
8466
8467 if Private_Extension then
8468 Type_Def := N;
8469 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8470 Set_Default_SSO (Derived_Type);
8471
8472 else
8473 Type_Def := Type_Definition (N);
8474
8475 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8476 -- Parent_Base can be a private type or private extension. However,
8477 -- for tagged types with an extension the newly added fields are
8478 -- visible and hence the Derived_Type is always an E_Record_Type.
8479 -- (except that the parent may have its own private fields).
8480 -- For untagged types we preserve the Ekind of the Parent_Base.
8481
8482 if Present (Record_Extension_Part (Type_Def)) then
8483 Set_Ekind (Derived_Type, E_Record_Type);
8484 Set_Default_SSO (Derived_Type);
8485
8486 -- Create internal access types for components with anonymous
8487 -- access types.
8488
8489 if Ada_Version >= Ada_2005 then
8490 Check_Anonymous_Access_Components
8491 (N, Derived_Type, Derived_Type,
8492 Component_List (Record_Extension_Part (Type_Def)));
8493 end if;
8494
8495 else
8496 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8497 end if;
8498 end if;
8499
8500 -- Indic can either be an N_Identifier if the subtype indication
8501 -- contains no constraint or an N_Subtype_Indication if the subtype
8502 -- indication has a constraint.
8503
8504 Indic := Subtype_Indication (Type_Def);
8505 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8506
8507 -- Check that the type has visible discriminants. The type may be
8508 -- a private type with unknown discriminants whose full view has
8509 -- discriminants which are invisible.
8510
8511 if Constraint_Present then
8512 if not Has_Discriminants (Parent_Base)
8513 or else
8514 (Has_Unknown_Discriminants (Parent_Base)
8515 and then Is_Private_Type (Parent_Base))
8516 then
8517 Error_Msg_N
8518 ("invalid constraint: type has no discriminant",
8519 Constraint (Indic));
8520
8521 Constraint_Present := False;
8522 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8523
8524 elsif Is_Constrained (Parent_Type) then
8525 Error_Msg_N
8526 ("invalid constraint: parent type is already constrained",
8527 Constraint (Indic));
8528
8529 Constraint_Present := False;
8530 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8531 end if;
8532 end if;
8533
8534 -- STEP 0b: If needed, apply transformation given in point 5. above
8535
8536 if not Private_Extension
8537 and then Has_Discriminants (Parent_Type)
8538 and then not Discriminant_Specs
8539 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8540 then
8541 -- First, we must analyze the constraint (see comment in point 5.)
8542 -- The constraint may come from the subtype indication of the full
8543 -- declaration.
8544
8545 if Constraint_Present then
8546 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8547
8548 -- If there is no explicit constraint, there might be one that is
8549 -- inherited from a constrained parent type. In that case verify that
8550 -- it conforms to the constraint in the partial view. In perverse
8551 -- cases the parent subtypes of the partial and full view can have
8552 -- different constraints.
8553
8554 elsif Present (Stored_Constraint (Parent_Type)) then
8555 New_Discrs := Stored_Constraint (Parent_Type);
8556
8557 else
8558 New_Discrs := No_Elist;
8559 end if;
8560
8561 if Has_Discriminants (Derived_Type)
8562 and then Has_Private_Declaration (Derived_Type)
8563 and then Present (Discriminant_Constraint (Derived_Type))
8564 and then Present (New_Discrs)
8565 then
8566 -- Verify that constraints of the full view statically match
8567 -- those given in the partial view.
8568
8569 declare
8570 C1, C2 : Elmt_Id;
8571
8572 begin
8573 C1 := First_Elmt (New_Discrs);
8574 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8575 while Present (C1) and then Present (C2) loop
8576 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8577 or else
8578 (Is_OK_Static_Expression (Node (C1))
8579 and then Is_OK_Static_Expression (Node (C2))
8580 and then
8581 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8582 then
8583 null;
8584
8585 else
8586 if Constraint_Present then
8587 Error_Msg_N
8588 ("constraint not conformant to previous declaration",
8589 Node (C1));
8590 else
8591 Error_Msg_N
8592 ("constraint of full view is incompatible "
8593 & "with partial view", N);
8594 end if;
8595 end if;
8596
8597 Next_Elmt (C1);
8598 Next_Elmt (C2);
8599 end loop;
8600 end;
8601 end if;
8602
8603 -- Insert and analyze the declaration for the unconstrained base type
8604
8605 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8606
8607 New_Decl :=
8608 Make_Full_Type_Declaration (Loc,
8609 Defining_Identifier => New_Base,
8610 Type_Definition =>
8611 Make_Derived_Type_Definition (Loc,
8612 Abstract_Present => Abstract_Present (Type_Def),
8613 Limited_Present => Limited_Present (Type_Def),
8614 Subtype_Indication =>
8615 New_Occurrence_Of (Parent_Base, Loc),
8616 Record_Extension_Part =>
8617 Relocate_Node (Record_Extension_Part (Type_Def)),
8618 Interface_List => Interface_List (Type_Def)));
8619
8620 Set_Parent (New_Decl, Parent (N));
8621 Mark_Rewrite_Insertion (New_Decl);
8622 Insert_Before (N, New_Decl);
8623
8624 -- In the extension case, make sure ancestor is frozen appropriately
8625 -- (see also non-discriminated case below).
8626
8627 if Present (Record_Extension_Part (Type_Def))
8628 or else Is_Interface (Parent_Base)
8629 then
8630 Freeze_Before (New_Decl, Parent_Type);
8631 end if;
8632
8633 -- Note that this call passes False for the Derive_Subps parameter
8634 -- because subprogram derivation is deferred until after creating
8635 -- the subtype (see below).
8636
8637 Build_Derived_Type
8638 (New_Decl, Parent_Base, New_Base,
8639 Is_Completion => False, Derive_Subps => False);
8640
8641 -- ??? This needs re-examination to determine whether the
8642 -- above call can simply be replaced by a call to Analyze.
8643
8644 Set_Analyzed (New_Decl);
8645
8646 -- Insert and analyze the declaration for the constrained subtype
8647
8648 if Constraint_Present then
8649 New_Indic :=
8650 Make_Subtype_Indication (Loc,
8651 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8652 Constraint => Relocate_Node (Constraint (Indic)));
8653
8654 else
8655 declare
8656 Constr_List : constant List_Id := New_List;
8657 C : Elmt_Id;
8658 Expr : Node_Id;
8659
8660 begin
8661 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8662 while Present (C) loop
8663 Expr := Node (C);
8664
8665 -- It is safe here to call New_Copy_Tree since we called
8666 -- Force_Evaluation on each constraint previously
8667 -- in Build_Discriminant_Constraints.
8668
8669 Append (New_Copy_Tree (Expr), To => Constr_List);
8670
8671 Next_Elmt (C);
8672 end loop;
8673
8674 New_Indic :=
8675 Make_Subtype_Indication (Loc,
8676 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8677 Constraint =>
8678 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8679 end;
8680 end if;
8681
8682 Rewrite (N,
8683 Make_Subtype_Declaration (Loc,
8684 Defining_Identifier => Derived_Type,
8685 Subtype_Indication => New_Indic));
8686
8687 Analyze (N);
8688
8689 -- Derivation of subprograms must be delayed until the full subtype
8690 -- has been established, to ensure proper overriding of subprograms
8691 -- inherited by full types. If the derivations occurred as part of
8692 -- the call to Build_Derived_Type above, then the check for type
8693 -- conformance would fail because earlier primitive subprograms
8694 -- could still refer to the full type prior the change to the new
8695 -- subtype and hence would not match the new base type created here.
8696 -- Subprograms are not derived, however, when Derive_Subps is False
8697 -- (since otherwise there could be redundant derivations).
8698
8699 if Derive_Subps then
8700 Derive_Subprograms (Parent_Type, Derived_Type);
8701 end if;
8702
8703 -- For tagged types the Discriminant_Constraint of the new base itype
8704 -- is inherited from the first subtype so that no subtype conformance
8705 -- problem arise when the first subtype overrides primitive
8706 -- operations inherited by the implicit base type.
8707
8708 if Is_Tagged then
8709 Set_Discriminant_Constraint
8710 (New_Base, Discriminant_Constraint (Derived_Type));
8711 end if;
8712
8713 return;
8714 end if;
8715
8716 -- If we get here Derived_Type will have no discriminants or it will be
8717 -- a discriminated unconstrained base type.
8718
8719 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8720
8721 if Is_Tagged then
8722
8723 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8724 -- The declaration of a specific descendant of an interface type
8725 -- freezes the interface type (RM 13.14).
8726
8727 if not Private_Extension or else Is_Interface (Parent_Base) then
8728 Freeze_Before (N, Parent_Type);
8729 end if;
8730
8731 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8732 -- cannot be declared at a deeper level than its parent type is
8733 -- removed. The check on derivation within a generic body is also
8734 -- relaxed, but there's a restriction that a derived tagged type
8735 -- cannot be declared in a generic body if it's derived directly
8736 -- or indirectly from a formal type of that generic.
8737
8738 if Ada_Version >= Ada_2005 then
8739 if Present (Enclosing_Generic_Body (Derived_Type)) then
8740 declare
8741 Ancestor_Type : Entity_Id;
8742
8743 begin
8744 -- Check to see if any ancestor of the derived type is a
8745 -- formal type.
8746
8747 Ancestor_Type := Parent_Type;
8748 while not Is_Generic_Type (Ancestor_Type)
8749 and then Etype (Ancestor_Type) /= Ancestor_Type
8750 loop
8751 Ancestor_Type := Etype (Ancestor_Type);
8752 end loop;
8753
8754 -- If the derived type does have a formal type as an
8755 -- ancestor, then it's an error if the derived type is
8756 -- declared within the body of the generic unit that
8757 -- declares the formal type in its generic formal part. It's
8758 -- sufficient to check whether the ancestor type is declared
8759 -- inside the same generic body as the derived type (such as
8760 -- within a nested generic spec), in which case the
8761 -- derivation is legal. If the formal type is declared
8762 -- outside of that generic body, then it's guaranteed that
8763 -- the derived type is declared within the generic body of
8764 -- the generic unit declaring the formal type.
8765
8766 if Is_Generic_Type (Ancestor_Type)
8767 and then Enclosing_Generic_Body (Ancestor_Type) /=
8768 Enclosing_Generic_Body (Derived_Type)
8769 then
8770 Error_Msg_NE
8771 ("parent type of& must not be descendant of formal type"
8772 & " of an enclosing generic body",
8773 Indic, Derived_Type);
8774 end if;
8775 end;
8776 end if;
8777
8778 elsif Type_Access_Level (Derived_Type) /=
8779 Type_Access_Level (Parent_Type)
8780 and then not Is_Generic_Type (Derived_Type)
8781 then
8782 if Is_Controlled (Parent_Type) then
8783 Error_Msg_N
8784 ("controlled type must be declared at the library level",
8785 Indic);
8786 else
8787 Error_Msg_N
8788 ("type extension at deeper accessibility level than parent",
8789 Indic);
8790 end if;
8791
8792 else
8793 declare
8794 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8795 begin
8796 if Present (GB)
8797 and then GB /= Enclosing_Generic_Body (Parent_Base)
8798 then
8799 Error_Msg_NE
8800 ("parent type of& must not be outside generic body"
8801 & " (RM 3.9.1(4))",
8802 Indic, Derived_Type);
8803 end if;
8804 end;
8805 end if;
8806 end if;
8807
8808 -- Ada 2005 (AI-251)
8809
8810 if Ada_Version >= Ada_2005 and then Is_Tagged then
8811
8812 -- "The declaration of a specific descendant of an interface type
8813 -- freezes the interface type" (RM 13.14).
8814
8815 declare
8816 Iface : Node_Id;
8817 begin
8818 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8819 Iface := First (Interface_List (Type_Def));
8820 while Present (Iface) loop
8821 Freeze_Before (N, Etype (Iface));
8822 Next (Iface);
8823 end loop;
8824 end if;
8825 end;
8826 end if;
8827
8828 -- STEP 1b : preliminary cleanup of the full view of private types
8829
8830 -- If the type is already marked as having discriminants, then it's the
8831 -- completion of a private type or private extension and we need to
8832 -- retain the discriminants from the partial view if the current
8833 -- declaration has Discriminant_Specifications so that we can verify
8834 -- conformance. However, we must remove any existing components that
8835 -- were inherited from the parent (and attached in Copy_And_Swap)
8836 -- because the full type inherits all appropriate components anyway, and
8837 -- we do not want the partial view's components interfering.
8838
8839 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8840 Discrim := First_Discriminant (Derived_Type);
8841 loop
8842 Last_Discrim := Discrim;
8843 Next_Discriminant (Discrim);
8844 exit when No (Discrim);
8845 end loop;
8846
8847 Set_Last_Entity (Derived_Type, Last_Discrim);
8848
8849 -- In all other cases wipe out the list of inherited components (even
8850 -- inherited discriminants), it will be properly rebuilt here.
8851
8852 else
8853 Set_First_Entity (Derived_Type, Empty);
8854 Set_Last_Entity (Derived_Type, Empty);
8855 end if;
8856
8857 -- STEP 1c: Initialize some flags for the Derived_Type
8858
8859 -- The following flags must be initialized here so that
8860 -- Process_Discriminants can check that discriminants of tagged types do
8861 -- not have a default initial value and that access discriminants are
8862 -- only specified for limited records. For completeness, these flags are
8863 -- also initialized along with all the other flags below.
8864
8865 -- AI-419: Limitedness is not inherited from an interface parent, so to
8866 -- be limited in that case the type must be explicitly declared as
8867 -- limited. However, task and protected interfaces are always limited.
8868
8869 if Limited_Present (Type_Def) then
8870 Set_Is_Limited_Record (Derived_Type);
8871
8872 elsif Is_Limited_Record (Parent_Type)
8873 or else (Present (Full_View (Parent_Type))
8874 and then Is_Limited_Record (Full_View (Parent_Type)))
8875 then
8876 if not Is_Interface (Parent_Type)
8877 or else Is_Synchronized_Interface (Parent_Type)
8878 or else Is_Protected_Interface (Parent_Type)
8879 or else Is_Task_Interface (Parent_Type)
8880 then
8881 Set_Is_Limited_Record (Derived_Type);
8882 end if;
8883 end if;
8884
8885 -- STEP 2a: process discriminants of derived type if any
8886
8887 Push_Scope (Derived_Type);
8888
8889 if Discriminant_Specs then
8890 Set_Has_Unknown_Discriminants (Derived_Type, False);
8891
8892 -- The following call initializes fields Has_Discriminants and
8893 -- Discriminant_Constraint, unless we are processing the completion
8894 -- of a private type declaration.
8895
8896 Check_Or_Process_Discriminants (N, Derived_Type);
8897
8898 -- For untagged types, the constraint on the Parent_Type must be
8899 -- present and is used to rename the discriminants.
8900
8901 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8902 Error_Msg_N ("untagged parent must have discriminants", Indic);
8903
8904 elsif not Is_Tagged and then not Constraint_Present then
8905 Error_Msg_N
8906 ("discriminant constraint needed for derived untagged records",
8907 Indic);
8908
8909 -- Otherwise the parent subtype must be constrained unless we have a
8910 -- private extension.
8911
8912 elsif not Constraint_Present
8913 and then not Private_Extension
8914 and then not Is_Constrained (Parent_Type)
8915 then
8916 Error_Msg_N
8917 ("unconstrained type not allowed in this context", Indic);
8918
8919 elsif Constraint_Present then
8920 -- The following call sets the field Corresponding_Discriminant
8921 -- for the discriminants in the Derived_Type.
8922
8923 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8924
8925 -- For untagged types all new discriminants must rename
8926 -- discriminants in the parent. For private extensions new
8927 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8928
8929 Discrim := First_Discriminant (Derived_Type);
8930 while Present (Discrim) loop
8931 if not Is_Tagged
8932 and then No (Corresponding_Discriminant (Discrim))
8933 then
8934 Error_Msg_N
8935 ("new discriminants must constrain old ones", Discrim);
8936
8937 elsif Private_Extension
8938 and then Present (Corresponding_Discriminant (Discrim))
8939 then
8940 Error_Msg_N
8941 ("only static constraints allowed for parent"
8942 & " discriminants in the partial view", Indic);
8943 exit;
8944 end if;
8945
8946 -- If a new discriminant is used in the constraint, then its
8947 -- subtype must be statically compatible with the parent
8948 -- discriminant's subtype (3.7(15)).
8949
8950 -- However, if the record contains an array constrained by
8951 -- the discriminant but with some different bound, the compiler
8952 -- tries to create a smaller range for the discriminant type.
8953 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8954 -- the discriminant type is a scalar type, the check must use
8955 -- the original discriminant type in the parent declaration.
8956
8957 declare
8958 Corr_Disc : constant Entity_Id :=
8959 Corresponding_Discriminant (Discrim);
8960 Disc_Type : constant Entity_Id := Etype (Discrim);
8961 Corr_Type : Entity_Id;
8962
8963 begin
8964 if Present (Corr_Disc) then
8965 if Is_Scalar_Type (Disc_Type) then
8966 Corr_Type :=
8967 Entity (Discriminant_Type (Parent (Corr_Disc)));
8968 else
8969 Corr_Type := Etype (Corr_Disc);
8970 end if;
8971
8972 if not
8973 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8974 then
8975 Error_Msg_N
8976 ("subtype must be compatible "
8977 & "with parent discriminant",
8978 Discrim);
8979 end if;
8980 end if;
8981 end;
8982
8983 Next_Discriminant (Discrim);
8984 end loop;
8985
8986 -- Check whether the constraints of the full view statically
8987 -- match those imposed by the parent subtype [7.3(13)].
8988
8989 if Present (Stored_Constraint (Derived_Type)) then
8990 declare
8991 C1, C2 : Elmt_Id;
8992
8993 begin
8994 C1 := First_Elmt (Discs);
8995 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8996 while Present (C1) and then Present (C2) loop
8997 if not
8998 Fully_Conformant_Expressions (Node (C1), Node (C2))
8999 then
9000 Error_Msg_N
9001 ("not conformant with previous declaration",
9002 Node (C1));
9003 end if;
9004
9005 Next_Elmt (C1);
9006 Next_Elmt (C2);
9007 end loop;
9008 end;
9009 end if;
9010 end if;
9011
9012 -- STEP 2b: No new discriminants, inherit discriminants if any
9013
9014 else
9015 if Private_Extension then
9016 Set_Has_Unknown_Discriminants
9017 (Derived_Type,
9018 Has_Unknown_Discriminants (Parent_Type)
9019 or else Unknown_Discriminants_Present (N));
9020
9021 -- The partial view of the parent may have unknown discriminants,
9022 -- but if the full view has discriminants and the parent type is
9023 -- in scope they must be inherited.
9024
9025 elsif Has_Unknown_Discriminants (Parent_Type)
9026 and then
9027 (not Has_Discriminants (Parent_Type)
9028 or else not In_Open_Scopes (Scope (Parent_Type)))
9029 then
9030 Set_Has_Unknown_Discriminants (Derived_Type);
9031 end if;
9032
9033 if not Has_Unknown_Discriminants (Derived_Type)
9034 and then not Has_Unknown_Discriminants (Parent_Base)
9035 and then Has_Discriminants (Parent_Type)
9036 then
9037 Inherit_Discrims := True;
9038 Set_Has_Discriminants
9039 (Derived_Type, True);
9040 Set_Discriminant_Constraint
9041 (Derived_Type, Discriminant_Constraint (Parent_Base));
9042 end if;
9043
9044 -- The following test is true for private types (remember
9045 -- transformation 5. is not applied to those) and in an error
9046 -- situation.
9047
9048 if Constraint_Present then
9049 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9050 end if;
9051
9052 -- For now mark a new derived type as constrained only if it has no
9053 -- discriminants. At the end of Build_Derived_Record_Type we properly
9054 -- set this flag in the case of private extensions. See comments in
9055 -- point 9. just before body of Build_Derived_Record_Type.
9056
9057 Set_Is_Constrained
9058 (Derived_Type,
9059 not (Inherit_Discrims
9060 or else Has_Unknown_Discriminants (Derived_Type)));
9061 end if;
9062
9063 -- STEP 3: initialize fields of derived type
9064
9065 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9066 Set_Stored_Constraint (Derived_Type, No_Elist);
9067
9068 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9069 -- but cannot be interfaces
9070
9071 if not Private_Extension
9072 and then Ekind (Derived_Type) /= E_Private_Type
9073 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9074 then
9075 if Interface_Present (Type_Def) then
9076 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9077 end if;
9078
9079 Set_Interfaces (Derived_Type, No_Elist);
9080 end if;
9081
9082 -- Fields inherited from the Parent_Type
9083
9084 Set_Has_Specified_Layout
9085 (Derived_Type, Has_Specified_Layout (Parent_Type));
9086 Set_Is_Limited_Composite
9087 (Derived_Type, Is_Limited_Composite (Parent_Type));
9088 Set_Is_Private_Composite
9089 (Derived_Type, Is_Private_Composite (Parent_Type));
9090
9091 if Is_Tagged_Type (Parent_Type) then
9092 Set_No_Tagged_Streams_Pragma
9093 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9094 end if;
9095
9096 -- Fields inherited from the Parent_Base
9097
9098 Set_Has_Controlled_Component
9099 (Derived_Type, Has_Controlled_Component (Parent_Base));
9100 Set_Has_Non_Standard_Rep
9101 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9102 Set_Has_Primitive_Operations
9103 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9104
9105 -- Fields inherited from the Parent_Base in the non-private case
9106
9107 if Ekind (Derived_Type) = E_Record_Type then
9108 Set_Has_Complex_Representation
9109 (Derived_Type, Has_Complex_Representation (Parent_Base));
9110 end if;
9111
9112 -- Fields inherited from the Parent_Base for record types
9113
9114 if Is_Record_Type (Derived_Type) then
9115 declare
9116 Parent_Full : Entity_Id;
9117
9118 begin
9119 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9120 -- Parent_Base can be a private type or private extension. Go
9121 -- to the full view here to get the E_Record_Type specific flags.
9122
9123 if Present (Full_View (Parent_Base)) then
9124 Parent_Full := Full_View (Parent_Base);
9125 else
9126 Parent_Full := Parent_Base;
9127 end if;
9128
9129 Set_OK_To_Reorder_Components
9130 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
9131 end;
9132 end if;
9133
9134 -- Set fields for private derived types
9135
9136 if Is_Private_Type (Derived_Type) then
9137 Set_Depends_On_Private (Derived_Type, True);
9138 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9139
9140 -- Inherit fields from non private record types. If this is the
9141 -- completion of a derivation from a private type, the parent itself
9142 -- is private, and the attributes come from its full view, which must
9143 -- be present.
9144
9145 else
9146 if Is_Private_Type (Parent_Base)
9147 and then not Is_Record_Type (Parent_Base)
9148 then
9149 Set_Component_Alignment
9150 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
9151 Set_C_Pass_By_Copy
9152 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
9153 else
9154 Set_Component_Alignment
9155 (Derived_Type, Component_Alignment (Parent_Base));
9156 Set_C_Pass_By_Copy
9157 (Derived_Type, C_Pass_By_Copy (Parent_Base));
9158 end if;
9159 end if;
9160
9161 -- Set fields for tagged types
9162
9163 if Is_Tagged then
9164 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9165
9166 -- All tagged types defined in Ada.Finalization are controlled
9167
9168 if Chars (Scope (Derived_Type)) = Name_Finalization
9169 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9170 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9171 then
9172 Set_Is_Controlled (Derived_Type);
9173 else
9174 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
9175 end if;
9176
9177 -- Minor optimization: there is no need to generate the class-wide
9178 -- entity associated with an underlying record view.
9179
9180 if not Is_Underlying_Record_View (Derived_Type) then
9181 Make_Class_Wide_Type (Derived_Type);
9182 end if;
9183
9184 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9185
9186 if Has_Discriminants (Derived_Type)
9187 and then Constraint_Present
9188 then
9189 Set_Stored_Constraint
9190 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9191 end if;
9192
9193 if Ada_Version >= Ada_2005 then
9194 declare
9195 Ifaces_List : Elist_Id;
9196
9197 begin
9198 -- Checks rules 3.9.4 (13/2 and 14/2)
9199
9200 if Comes_From_Source (Derived_Type)
9201 and then not Is_Private_Type (Derived_Type)
9202 and then Is_Interface (Parent_Type)
9203 and then not Is_Interface (Derived_Type)
9204 then
9205 if Is_Task_Interface (Parent_Type) then
9206 Error_Msg_N
9207 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9208 Derived_Type);
9209
9210 elsif Is_Protected_Interface (Parent_Type) then
9211 Error_Msg_N
9212 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9213 Derived_Type);
9214 end if;
9215 end if;
9216
9217 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9218
9219 Check_Interfaces (N, Type_Def);
9220
9221 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9222 -- not already in the parents.
9223
9224 Collect_Interfaces
9225 (T => Derived_Type,
9226 Ifaces_List => Ifaces_List,
9227 Exclude_Parents => True);
9228
9229 Set_Interfaces (Derived_Type, Ifaces_List);
9230
9231 -- If the derived type is the anonymous type created for
9232 -- a declaration whose parent has a constraint, propagate
9233 -- the interface list to the source type. This must be done
9234 -- prior to the completion of the analysis of the source type
9235 -- because the components in the extension may contain current
9236 -- instances whose legality depends on some ancestor.
9237
9238 if Is_Itype (Derived_Type) then
9239 declare
9240 Def : constant Node_Id :=
9241 Associated_Node_For_Itype (Derived_Type);
9242 begin
9243 if Present (Def)
9244 and then Nkind (Def) = N_Full_Type_Declaration
9245 then
9246 Set_Interfaces
9247 (Defining_Identifier (Def), Ifaces_List);
9248 end if;
9249 end;
9250 end if;
9251
9252 -- A type extension is automatically Ghost when one of its
9253 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9254 -- also inherited when the parent type is Ghost, but this is
9255 -- done in Build_Derived_Type as the mechanism also handles
9256 -- untagged derivations.
9257
9258 if Implements_Ghost_Interface (Derived_Type) then
9259 Set_Is_Ghost_Entity (Derived_Type);
9260 end if;
9261 end;
9262 end if;
9263
9264 else
9265 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
9266 Set_Has_Non_Standard_Rep
9267 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9268 end if;
9269
9270 -- STEP 4: Inherit components from the parent base and constrain them.
9271 -- Apply the second transformation described in point 6. above.
9272
9273 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9274 or else not Has_Discriminants (Parent_Type)
9275 or else not Is_Constrained (Parent_Type)
9276 then
9277 Constrs := Discs;
9278 else
9279 Constrs := Discriminant_Constraint (Parent_Type);
9280 end if;
9281
9282 Assoc_List :=
9283 Inherit_Components
9284 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9285
9286 -- STEP 5a: Copy the parent record declaration for untagged types
9287
9288 Set_Has_Implicit_Dereference
9289 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9290
9291 if not Is_Tagged then
9292
9293 -- Discriminant_Constraint (Derived_Type) has been properly
9294 -- constructed. Save it and temporarily set it to Empty because we
9295 -- do not want the call to New_Copy_Tree below to mess this list.
9296
9297 if Has_Discriminants (Derived_Type) then
9298 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9299 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9300 else
9301 Save_Discr_Constr := No_Elist;
9302 end if;
9303
9304 -- Save the Etype field of Derived_Type. It is correctly set now,
9305 -- but the call to New_Copy tree may remap it to point to itself,
9306 -- which is not what we want. Ditto for the Next_Entity field.
9307
9308 Save_Etype := Etype (Derived_Type);
9309 Save_Next_Entity := Next_Entity (Derived_Type);
9310
9311 -- Assoc_List maps all stored discriminants in the Parent_Base to
9312 -- stored discriminants in the Derived_Type. It is fundamental that
9313 -- no types or itypes with discriminants other than the stored
9314 -- discriminants appear in the entities declared inside
9315 -- Derived_Type, since the back end cannot deal with it.
9316
9317 New_Decl :=
9318 New_Copy_Tree
9319 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9320
9321 -- Restore the fields saved prior to the New_Copy_Tree call
9322 -- and compute the stored constraint.
9323
9324 Set_Etype (Derived_Type, Save_Etype);
9325 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9326
9327 if Has_Discriminants (Derived_Type) then
9328 Set_Discriminant_Constraint
9329 (Derived_Type, Save_Discr_Constr);
9330 Set_Stored_Constraint
9331 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9332 Replace_Components (Derived_Type, New_Decl);
9333 end if;
9334
9335 -- Insert the new derived type declaration
9336
9337 Rewrite (N, New_Decl);
9338
9339 -- STEP 5b: Complete the processing for record extensions in generics
9340
9341 -- There is no completion for record extensions declared in the
9342 -- parameter part of a generic, so we need to complete processing for
9343 -- these generic record extensions here. The Record_Type_Definition call
9344 -- will change the Ekind of the components from E_Void to E_Component.
9345
9346 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9347 Record_Type_Definition (Empty, Derived_Type);
9348
9349 -- STEP 5c: Process the record extension for non private tagged types
9350
9351 elsif not Private_Extension then
9352 Expand_Record_Extension (Derived_Type, Type_Def);
9353
9354 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9355 -- derived type to propagate some semantic information. This led
9356 -- to other ASIS failures and has been removed.
9357
9358 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9359 -- implemented interfaces if we are in expansion mode
9360
9361 if Expander_Active
9362 and then Has_Interfaces (Derived_Type)
9363 then
9364 Add_Interface_Tag_Components (N, Derived_Type);
9365 end if;
9366
9367 -- Analyze the record extension
9368
9369 Record_Type_Definition
9370 (Record_Extension_Part (Type_Def), Derived_Type);
9371 end if;
9372
9373 End_Scope;
9374
9375 -- Nothing else to do if there is an error in the derivation.
9376 -- An unusual case: the full view may be derived from a type in an
9377 -- instance, when the partial view was used illegally as an actual
9378 -- in that instance, leading to a circular definition.
9379
9380 if Etype (Derived_Type) = Any_Type
9381 or else Etype (Parent_Type) = Derived_Type
9382 then
9383 return;
9384 end if;
9385
9386 -- Set delayed freeze and then derive subprograms, we need to do
9387 -- this in this order so that derived subprograms inherit the
9388 -- derived freeze if necessary.
9389
9390 Set_Has_Delayed_Freeze (Derived_Type);
9391
9392 if Derive_Subps then
9393 Derive_Subprograms (Parent_Type, Derived_Type);
9394 end if;
9395
9396 -- If we have a private extension which defines a constrained derived
9397 -- type mark as constrained here after we have derived subprograms. See
9398 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9399
9400 if Private_Extension and then Inherit_Discrims then
9401 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9402 Set_Is_Constrained (Derived_Type, True);
9403 Set_Discriminant_Constraint (Derived_Type, Discs);
9404
9405 elsif Is_Constrained (Parent_Type) then
9406 Set_Is_Constrained
9407 (Derived_Type, True);
9408 Set_Discriminant_Constraint
9409 (Derived_Type, Discriminant_Constraint (Parent_Type));
9410 end if;
9411 end if;
9412
9413 -- Update the class-wide type, which shares the now-completed entity
9414 -- list with its specific type. In case of underlying record views,
9415 -- we do not generate the corresponding class wide entity.
9416
9417 if Is_Tagged
9418 and then not Is_Underlying_Record_View (Derived_Type)
9419 then
9420 Set_First_Entity
9421 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9422 Set_Last_Entity
9423 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9424 end if;
9425
9426 Check_Function_Writable_Actuals (N);
9427 end Build_Derived_Record_Type;
9428
9429 ------------------------
9430 -- Build_Derived_Type --
9431 ------------------------
9432
9433 procedure Build_Derived_Type
9434 (N : Node_Id;
9435 Parent_Type : Entity_Id;
9436 Derived_Type : Entity_Id;
9437 Is_Completion : Boolean;
9438 Derive_Subps : Boolean := True)
9439 is
9440 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9441
9442 begin
9443 -- Set common attributes
9444
9445 Set_Scope (Derived_Type, Current_Scope);
9446
9447 Set_Etype (Derived_Type, Parent_Base);
9448 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9449 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9450
9451 Set_Size_Info (Derived_Type, Parent_Type);
9452 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9453 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9454 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9455
9456 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9457 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9458
9459 if Is_Tagged_Type (Derived_Type) then
9460 Set_No_Tagged_Streams_Pragma
9461 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9462 end if;
9463
9464 -- If the parent has primitive routines, set the derived type link
9465
9466 if Has_Primitive_Operations (Parent_Type) then
9467 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9468 end if;
9469
9470 -- If the parent type is a private subtype, the convention on the base
9471 -- type may be set in the private part, and not propagated to the
9472 -- subtype until later, so we obtain the convention from the base type.
9473
9474 Set_Convention (Derived_Type, Convention (Parent_Base));
9475
9476 -- Set SSO default for record or array type
9477
9478 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9479 and then Is_Base_Type (Derived_Type)
9480 then
9481 Set_Default_SSO (Derived_Type);
9482 end if;
9483
9484 -- A derived type inherits the Default_Initial_Condition pragma coming
9485 -- from any parent type within the derivation chain.
9486
9487 if Has_DIC (Parent_Type) then
9488 Set_Has_Inherited_DIC (Derived_Type);
9489 end if;
9490
9491 -- A derived type inherits any class-wide invariants coming from a
9492 -- parent type or an interface. Note that the invariant procedure of
9493 -- the parent type should not be inherited because the derived type may
9494 -- define invariants of its own.
9495
9496 if not Is_Interface (Derived_Type) then
9497 if Has_Inherited_Invariants (Parent_Type)
9498 or else Has_Inheritable_Invariants (Parent_Type)
9499 then
9500 Set_Has_Inherited_Invariants (Derived_Type);
9501
9502 elsif Is_Concurrent_Type (Derived_Type)
9503 or else Is_Tagged_Type (Derived_Type)
9504 then
9505 declare
9506 Iface : Entity_Id;
9507 Ifaces : Elist_Id;
9508 Iface_Elmt : Elmt_Id;
9509
9510 begin
9511 Collect_Interfaces
9512 (T => Derived_Type,
9513 Ifaces_List => Ifaces,
9514 Exclude_Parents => True);
9515
9516 if Present (Ifaces) then
9517 Iface_Elmt := First_Elmt (Ifaces);
9518 while Present (Iface_Elmt) loop
9519 Iface := Node (Iface_Elmt);
9520
9521 if Has_Inheritable_Invariants (Iface) then
9522 Set_Has_Inherited_Invariants (Derived_Type);
9523 exit;
9524 end if;
9525
9526 Next_Elmt (Iface_Elmt);
9527 end loop;
9528 end if;
9529 end;
9530 end if;
9531 end if;
9532
9533 -- We similarly inherit predicates. Note that for scalar derived types
9534 -- the predicate is inherited from the first subtype, and not from its
9535 -- (anonymous) base type.
9536
9537 if Has_Predicates (Parent_Type)
9538 or else Has_Predicates (First_Subtype (Parent_Type))
9539 then
9540 Set_Has_Predicates (Derived_Type);
9541 end if;
9542
9543 -- The derived type inherits the representation clauses of the parent
9544
9545 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9546
9547 -- If the parent type has delayed rep aspects, then mark the derived
9548 -- type as possibly inheriting a delayed rep aspect.
9549
9550 if Has_Delayed_Rep_Aspects (Parent_Type) then
9551 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9552 end if;
9553
9554 -- A derived type becomes Ghost when its parent type is also Ghost
9555 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9556 -- directly inherited because the Ghost policy in effect may differ.
9557
9558 if Is_Ghost_Entity (Parent_Type) then
9559 Set_Is_Ghost_Entity (Derived_Type);
9560 end if;
9561
9562 -- Type dependent processing
9563
9564 case Ekind (Parent_Type) is
9565 when Numeric_Kind =>
9566 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9567
9568 when Array_Kind =>
9569 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9570
9571 when Class_Wide_Kind
9572 | E_Record_Subtype
9573 | E_Record_Type
9574 =>
9575 Build_Derived_Record_Type
9576 (N, Parent_Type, Derived_Type, Derive_Subps);
9577 return;
9578
9579 when Enumeration_Kind =>
9580 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9581
9582 when Access_Kind =>
9583 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9584
9585 when Incomplete_Or_Private_Kind =>
9586 Build_Derived_Private_Type
9587 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9588
9589 -- For discriminated types, the derivation includes deriving
9590 -- primitive operations. For others it is done below.
9591
9592 if Is_Tagged_Type (Parent_Type)
9593 or else Has_Discriminants (Parent_Type)
9594 or else (Present (Full_View (Parent_Type))
9595 and then Has_Discriminants (Full_View (Parent_Type)))
9596 then
9597 return;
9598 end if;
9599
9600 when Concurrent_Kind =>
9601 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9602
9603 when others =>
9604 raise Program_Error;
9605 end case;
9606
9607 -- Nothing more to do if some error occurred
9608
9609 if Etype (Derived_Type) = Any_Type then
9610 return;
9611 end if;
9612
9613 -- Set delayed freeze and then derive subprograms, we need to do this
9614 -- in this order so that derived subprograms inherit the derived freeze
9615 -- if necessary.
9616
9617 Set_Has_Delayed_Freeze (Derived_Type);
9618
9619 if Derive_Subps then
9620 Derive_Subprograms (Parent_Type, Derived_Type);
9621 end if;
9622
9623 Set_Has_Primitive_Operations
9624 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9625 end Build_Derived_Type;
9626
9627 -----------------------
9628 -- Build_Discriminal --
9629 -----------------------
9630
9631 procedure Build_Discriminal (Discrim : Entity_Id) is
9632 D_Minal : Entity_Id;
9633 CR_Disc : Entity_Id;
9634
9635 begin
9636 -- A discriminal has the same name as the discriminant
9637
9638 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9639
9640 Set_Ekind (D_Minal, E_In_Parameter);
9641 Set_Mechanism (D_Minal, Default_Mechanism);
9642 Set_Etype (D_Minal, Etype (Discrim));
9643 Set_Scope (D_Minal, Current_Scope);
9644 Set_Parent (D_Minal, Parent (Discrim));
9645
9646 Set_Discriminal (Discrim, D_Minal);
9647 Set_Discriminal_Link (D_Minal, Discrim);
9648
9649 -- For task types, build at once the discriminants of the corresponding
9650 -- record, which are needed if discriminants are used in entry defaults
9651 -- and in family bounds.
9652
9653 if Is_Concurrent_Type (Current_Scope)
9654 or else
9655 Is_Limited_Type (Current_Scope)
9656 then
9657 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9658
9659 Set_Ekind (CR_Disc, E_In_Parameter);
9660 Set_Mechanism (CR_Disc, Default_Mechanism);
9661 Set_Etype (CR_Disc, Etype (Discrim));
9662 Set_Scope (CR_Disc, Current_Scope);
9663 Set_Discriminal_Link (CR_Disc, Discrim);
9664 Set_CR_Discriminant (Discrim, CR_Disc);
9665 end if;
9666 end Build_Discriminal;
9667
9668 ------------------------------------
9669 -- Build_Discriminant_Constraints --
9670 ------------------------------------
9671
9672 function Build_Discriminant_Constraints
9673 (T : Entity_Id;
9674 Def : Node_Id;
9675 Derived_Def : Boolean := False) return Elist_Id
9676 is
9677 C : constant Node_Id := Constraint (Def);
9678 Nb_Discr : constant Nat := Number_Discriminants (T);
9679
9680 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9681 -- Saves the expression corresponding to a given discriminant in T
9682
9683 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9684 -- Return the Position number within array Discr_Expr of a discriminant
9685 -- D within the discriminant list of the discriminated type T.
9686
9687 procedure Process_Discriminant_Expression
9688 (Expr : Node_Id;
9689 D : Entity_Id);
9690 -- If this is a discriminant constraint on a partial view, do not
9691 -- generate an overflow check on the discriminant expression. The check
9692 -- will be generated when constraining the full view. Otherwise the
9693 -- backend creates duplicate symbols for the temporaries corresponding
9694 -- to the expressions to be checked, causing spurious assembler errors.
9695
9696 ------------------
9697 -- Pos_Of_Discr --
9698 ------------------
9699
9700 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9701 Disc : Entity_Id;
9702
9703 begin
9704 Disc := First_Discriminant (T);
9705 for J in Discr_Expr'Range loop
9706 if Disc = D then
9707 return J;
9708 end if;
9709
9710 Next_Discriminant (Disc);
9711 end loop;
9712
9713 -- Note: Since this function is called on discriminants that are
9714 -- known to belong to the discriminated type, falling through the
9715 -- loop with no match signals an internal compiler error.
9716
9717 raise Program_Error;
9718 end Pos_Of_Discr;
9719
9720 -------------------------------------
9721 -- Process_Discriminant_Expression --
9722 -------------------------------------
9723
9724 procedure Process_Discriminant_Expression
9725 (Expr : Node_Id;
9726 D : Entity_Id)
9727 is
9728 BDT : constant Entity_Id := Base_Type (Etype (D));
9729
9730 begin
9731 -- If this is a discriminant constraint on a partial view, do
9732 -- not generate an overflow on the discriminant expression. The
9733 -- check will be generated when constraining the full view.
9734
9735 if Is_Private_Type (T)
9736 and then Present (Full_View (T))
9737 then
9738 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9739 else
9740 Analyze_And_Resolve (Expr, BDT);
9741 end if;
9742 end Process_Discriminant_Expression;
9743
9744 -- Declarations local to Build_Discriminant_Constraints
9745
9746 Discr : Entity_Id;
9747 E : Entity_Id;
9748 Elist : constant Elist_Id := New_Elmt_List;
9749
9750 Constr : Node_Id;
9751 Expr : Node_Id;
9752 Id : Node_Id;
9753 Position : Nat;
9754 Found : Boolean;
9755
9756 Discrim_Present : Boolean := False;
9757
9758 -- Start of processing for Build_Discriminant_Constraints
9759
9760 begin
9761 -- The following loop will process positional associations only.
9762 -- For a positional association, the (single) discriminant is
9763 -- implicitly specified by position, in textual order (RM 3.7.2).
9764
9765 Discr := First_Discriminant (T);
9766 Constr := First (Constraints (C));
9767 for D in Discr_Expr'Range loop
9768 exit when Nkind (Constr) = N_Discriminant_Association;
9769
9770 if No (Constr) then
9771 Error_Msg_N ("too few discriminants given in constraint", C);
9772 return New_Elmt_List;
9773
9774 elsif Nkind (Constr) = N_Range
9775 or else (Nkind (Constr) = N_Attribute_Reference
9776 and then Attribute_Name (Constr) = Name_Range)
9777 then
9778 Error_Msg_N
9779 ("a range is not a valid discriminant constraint", Constr);
9780 Discr_Expr (D) := Error;
9781
9782 else
9783 Process_Discriminant_Expression (Constr, Discr);
9784 Discr_Expr (D) := Constr;
9785 end if;
9786
9787 Next_Discriminant (Discr);
9788 Next (Constr);
9789 end loop;
9790
9791 if No (Discr) and then Present (Constr) then
9792 Error_Msg_N ("too many discriminants given in constraint", Constr);
9793 return New_Elmt_List;
9794 end if;
9795
9796 -- Named associations can be given in any order, but if both positional
9797 -- and named associations are used in the same discriminant constraint,
9798 -- then positional associations must occur first, at their normal
9799 -- position. Hence once a named association is used, the rest of the
9800 -- discriminant constraint must use only named associations.
9801
9802 while Present (Constr) loop
9803
9804 -- Positional association forbidden after a named association
9805
9806 if Nkind (Constr) /= N_Discriminant_Association then
9807 Error_Msg_N ("positional association follows named one", Constr);
9808 return New_Elmt_List;
9809
9810 -- Otherwise it is a named association
9811
9812 else
9813 -- E records the type of the discriminants in the named
9814 -- association. All the discriminants specified in the same name
9815 -- association must have the same type.
9816
9817 E := Empty;
9818
9819 -- Search the list of discriminants in T to see if the simple name
9820 -- given in the constraint matches any of them.
9821
9822 Id := First (Selector_Names (Constr));
9823 while Present (Id) loop
9824 Found := False;
9825
9826 -- If Original_Discriminant is present, we are processing a
9827 -- generic instantiation and this is an instance node. We need
9828 -- to find the name of the corresponding discriminant in the
9829 -- actual record type T and not the name of the discriminant in
9830 -- the generic formal. Example:
9831
9832 -- generic
9833 -- type G (D : int) is private;
9834 -- package P is
9835 -- subtype W is G (D => 1);
9836 -- end package;
9837 -- type Rec (X : int) is record ... end record;
9838 -- package Q is new P (G => Rec);
9839
9840 -- At the point of the instantiation, formal type G is Rec
9841 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9842 -- which really looks like "subtype W is Rec (D => 1);" at
9843 -- the point of instantiation, we want to find the discriminant
9844 -- that corresponds to D in Rec, i.e. X.
9845
9846 if Present (Original_Discriminant (Id))
9847 and then In_Instance
9848 then
9849 Discr := Find_Corresponding_Discriminant (Id, T);
9850 Found := True;
9851
9852 else
9853 Discr := First_Discriminant (T);
9854 while Present (Discr) loop
9855 if Chars (Discr) = Chars (Id) then
9856 Found := True;
9857 exit;
9858 end if;
9859
9860 Next_Discriminant (Discr);
9861 end loop;
9862
9863 if not Found then
9864 Error_Msg_N ("& does not match any discriminant", Id);
9865 return New_Elmt_List;
9866
9867 -- If the parent type is a generic formal, preserve the
9868 -- name of the discriminant for subsequent instances.
9869 -- see comment at the beginning of this if statement.
9870
9871 elsif Is_Generic_Type (Root_Type (T)) then
9872 Set_Original_Discriminant (Id, Discr);
9873 end if;
9874 end if;
9875
9876 Position := Pos_Of_Discr (T, Discr);
9877
9878 if Present (Discr_Expr (Position)) then
9879 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9880
9881 else
9882 -- Each discriminant specified in the same named association
9883 -- must be associated with a separate copy of the
9884 -- corresponding expression.
9885
9886 if Present (Next (Id)) then
9887 Expr := New_Copy_Tree (Expression (Constr));
9888 Set_Parent (Expr, Parent (Expression (Constr)));
9889 else
9890 Expr := Expression (Constr);
9891 end if;
9892
9893 Discr_Expr (Position) := Expr;
9894 Process_Discriminant_Expression (Expr, Discr);
9895 end if;
9896
9897 -- A discriminant association with more than one discriminant
9898 -- name is only allowed if the named discriminants are all of
9899 -- the same type (RM 3.7.1(8)).
9900
9901 if E = Empty then
9902 E := Base_Type (Etype (Discr));
9903
9904 elsif Base_Type (Etype (Discr)) /= E then
9905 Error_Msg_N
9906 ("all discriminants in an association " &
9907 "must have the same type", Id);
9908 end if;
9909
9910 Next (Id);
9911 end loop;
9912 end if;
9913
9914 Next (Constr);
9915 end loop;
9916
9917 -- A discriminant constraint must provide exactly one value for each
9918 -- discriminant of the type (RM 3.7.1(8)).
9919
9920 for J in Discr_Expr'Range loop
9921 if No (Discr_Expr (J)) then
9922 Error_Msg_N ("too few discriminants given in constraint", C);
9923 return New_Elmt_List;
9924 end if;
9925 end loop;
9926
9927 -- Determine if there are discriminant expressions in the constraint
9928
9929 for J in Discr_Expr'Range loop
9930 if Denotes_Discriminant
9931 (Discr_Expr (J), Check_Concurrent => True)
9932 then
9933 Discrim_Present := True;
9934 end if;
9935 end loop;
9936
9937 -- Build an element list consisting of the expressions given in the
9938 -- discriminant constraint and apply the appropriate checks. The list
9939 -- is constructed after resolving any named discriminant associations
9940 -- and therefore the expressions appear in the textual order of the
9941 -- discriminants.
9942
9943 Discr := First_Discriminant (T);
9944 for J in Discr_Expr'Range loop
9945 if Discr_Expr (J) /= Error then
9946 Append_Elmt (Discr_Expr (J), Elist);
9947
9948 -- If any of the discriminant constraints is given by a
9949 -- discriminant and we are in a derived type declaration we
9950 -- have a discriminant renaming. Establish link between new
9951 -- and old discriminant. The new discriminant has an implicit
9952 -- dereference if the old one does.
9953
9954 if Denotes_Discriminant (Discr_Expr (J)) then
9955 if Derived_Def then
9956 declare
9957 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
9958
9959 begin
9960 Set_Corresponding_Discriminant (New_Discr, Discr);
9961 Set_Has_Implicit_Dereference (New_Discr,
9962 Has_Implicit_Dereference (Discr));
9963 end;
9964 end if;
9965
9966 -- Force the evaluation of non-discriminant expressions.
9967 -- If we have found a discriminant in the constraint 3.4(26)
9968 -- and 3.8(18) demand that no range checks are performed are
9969 -- after evaluation. If the constraint is for a component
9970 -- definition that has a per-object constraint, expressions are
9971 -- evaluated but not checked either. In all other cases perform
9972 -- a range check.
9973
9974 else
9975 if Discrim_Present then
9976 null;
9977
9978 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9979 and then Has_Per_Object_Constraint
9980 (Defining_Identifier (Parent (Parent (Def))))
9981 then
9982 null;
9983
9984 elsif Is_Access_Type (Etype (Discr)) then
9985 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9986
9987 else
9988 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9989 end if;
9990
9991 Force_Evaluation (Discr_Expr (J));
9992 end if;
9993
9994 -- Check that the designated type of an access discriminant's
9995 -- expression is not a class-wide type unless the discriminant's
9996 -- designated type is also class-wide.
9997
9998 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9999 and then not Is_Class_Wide_Type
10000 (Designated_Type (Etype (Discr)))
10001 and then Etype (Discr_Expr (J)) /= Any_Type
10002 and then Is_Class_Wide_Type
10003 (Designated_Type (Etype (Discr_Expr (J))))
10004 then
10005 Wrong_Type (Discr_Expr (J), Etype (Discr));
10006
10007 elsif Is_Access_Type (Etype (Discr))
10008 and then not Is_Access_Constant (Etype (Discr))
10009 and then Is_Access_Type (Etype (Discr_Expr (J)))
10010 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10011 then
10012 Error_Msg_NE
10013 ("constraint for discriminant& must be access to variable",
10014 Def, Discr);
10015 end if;
10016 end if;
10017
10018 Next_Discriminant (Discr);
10019 end loop;
10020
10021 return Elist;
10022 end Build_Discriminant_Constraints;
10023
10024 ---------------------------------
10025 -- Build_Discriminated_Subtype --
10026 ---------------------------------
10027
10028 procedure Build_Discriminated_Subtype
10029 (T : Entity_Id;
10030 Def_Id : Entity_Id;
10031 Elist : Elist_Id;
10032 Related_Nod : Node_Id;
10033 For_Access : Boolean := False)
10034 is
10035 Has_Discrs : constant Boolean := Has_Discriminants (T);
10036 Constrained : constant Boolean :=
10037 (Has_Discrs
10038 and then not Is_Empty_Elmt_List (Elist)
10039 and then not Is_Class_Wide_Type (T))
10040 or else Is_Constrained (T);
10041
10042 begin
10043 if Ekind (T) = E_Record_Type then
10044 if For_Access then
10045 Set_Ekind (Def_Id, E_Private_Subtype);
10046 Set_Is_For_Access_Subtype (Def_Id, True);
10047 else
10048 Set_Ekind (Def_Id, E_Record_Subtype);
10049 end if;
10050
10051 -- Inherit preelaboration flag from base, for types for which it
10052 -- may have been set: records, private types, protected types.
10053
10054 Set_Known_To_Have_Preelab_Init
10055 (Def_Id, Known_To_Have_Preelab_Init (T));
10056
10057 elsif Ekind (T) = E_Task_Type then
10058 Set_Ekind (Def_Id, E_Task_Subtype);
10059
10060 elsif Ekind (T) = E_Protected_Type then
10061 Set_Ekind (Def_Id, E_Protected_Subtype);
10062 Set_Known_To_Have_Preelab_Init
10063 (Def_Id, Known_To_Have_Preelab_Init (T));
10064
10065 elsif Is_Private_Type (T) then
10066 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10067 Set_Known_To_Have_Preelab_Init
10068 (Def_Id, Known_To_Have_Preelab_Init (T));
10069
10070 -- Private subtypes may have private dependents
10071
10072 Set_Private_Dependents (Def_Id, New_Elmt_List);
10073
10074 elsif Is_Class_Wide_Type (T) then
10075 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10076
10077 else
10078 -- Incomplete type. Attach subtype to list of dependents, to be
10079 -- completed with full view of parent type, unless is it the
10080 -- designated subtype of a record component within an init_proc.
10081 -- This last case arises for a component of an access type whose
10082 -- designated type is incomplete (e.g. a Taft Amendment type).
10083 -- The designated subtype is within an inner scope, and needs no
10084 -- elaboration, because only the access type is needed in the
10085 -- initialization procedure.
10086
10087 Set_Ekind (Def_Id, Ekind (T));
10088
10089 if For_Access and then Within_Init_Proc then
10090 null;
10091 else
10092 Append_Elmt (Def_Id, Private_Dependents (T));
10093 end if;
10094 end if;
10095
10096 Set_Etype (Def_Id, T);
10097 Init_Size_Align (Def_Id);
10098 Set_Has_Discriminants (Def_Id, Has_Discrs);
10099 Set_Is_Constrained (Def_Id, Constrained);
10100
10101 Set_First_Entity (Def_Id, First_Entity (T));
10102 Set_Last_Entity (Def_Id, Last_Entity (T));
10103 Set_Has_Implicit_Dereference
10104 (Def_Id, Has_Implicit_Dereference (T));
10105 Set_Has_Pragma_Unreferenced_Objects
10106 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10107
10108 -- If the subtype is the completion of a private declaration, there may
10109 -- have been representation clauses for the partial view, and they must
10110 -- be preserved. Build_Derived_Type chains the inherited clauses with
10111 -- the ones appearing on the extension. If this comes from a subtype
10112 -- declaration, all clauses are inherited.
10113
10114 if No (First_Rep_Item (Def_Id)) then
10115 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10116 end if;
10117
10118 if Is_Tagged_Type (T) then
10119 Set_Is_Tagged_Type (Def_Id);
10120 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10121 Make_Class_Wide_Type (Def_Id);
10122 end if;
10123
10124 Set_Stored_Constraint (Def_Id, No_Elist);
10125
10126 if Has_Discrs then
10127 Set_Discriminant_Constraint (Def_Id, Elist);
10128 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10129 end if;
10130
10131 if Is_Tagged_Type (T) then
10132
10133 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10134 -- concurrent record type (which has the list of primitive
10135 -- operations).
10136
10137 if Ada_Version >= Ada_2005
10138 and then Is_Concurrent_Type (T)
10139 then
10140 Set_Corresponding_Record_Type (Def_Id,
10141 Corresponding_Record_Type (T));
10142 else
10143 Set_Direct_Primitive_Operations (Def_Id,
10144 Direct_Primitive_Operations (T));
10145 end if;
10146
10147 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10148 end if;
10149
10150 -- Subtypes introduced by component declarations do not need to be
10151 -- marked as delayed, and do not get freeze nodes, because the semantics
10152 -- verifies that the parents of the subtypes are frozen before the
10153 -- enclosing record is frozen.
10154
10155 if not Is_Type (Scope (Def_Id)) then
10156 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10157
10158 if Is_Private_Type (T)
10159 and then Present (Full_View (T))
10160 then
10161 Conditional_Delay (Def_Id, Full_View (T));
10162 else
10163 Conditional_Delay (Def_Id, T);
10164 end if;
10165 end if;
10166
10167 if Is_Record_Type (T) then
10168 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10169
10170 if Has_Discrs
10171 and then not Is_Empty_Elmt_List (Elist)
10172 and then not For_Access
10173 then
10174 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10175 elsif not For_Access then
10176 Set_Cloned_Subtype (Def_Id, T);
10177 end if;
10178 end if;
10179 end Build_Discriminated_Subtype;
10180
10181 ---------------------------
10182 -- Build_Itype_Reference --
10183 ---------------------------
10184
10185 procedure Build_Itype_Reference
10186 (Ityp : Entity_Id;
10187 Nod : Node_Id)
10188 is
10189 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10190 begin
10191
10192 -- Itype references are only created for use by the back-end
10193
10194 if Inside_A_Generic then
10195 return;
10196 else
10197 Set_Itype (IR, Ityp);
10198 Insert_After (Nod, IR);
10199 end if;
10200 end Build_Itype_Reference;
10201
10202 ------------------------
10203 -- Build_Scalar_Bound --
10204 ------------------------
10205
10206 function Build_Scalar_Bound
10207 (Bound : Node_Id;
10208 Par_T : Entity_Id;
10209 Der_T : Entity_Id) return Node_Id
10210 is
10211 New_Bound : Entity_Id;
10212
10213 begin
10214 -- Note: not clear why this is needed, how can the original bound
10215 -- be unanalyzed at this point? and if it is, what business do we
10216 -- have messing around with it? and why is the base type of the
10217 -- parent type the right type for the resolution. It probably is
10218 -- not. It is OK for the new bound we are creating, but not for
10219 -- the old one??? Still if it never happens, no problem.
10220
10221 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10222
10223 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10224 New_Bound := New_Copy (Bound);
10225 Set_Etype (New_Bound, Der_T);
10226 Set_Analyzed (New_Bound);
10227
10228 elsif Is_Entity_Name (Bound) then
10229 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10230
10231 -- The following is almost certainly wrong. What business do we have
10232 -- relocating a node (Bound) that is presumably still attached to
10233 -- the tree elsewhere???
10234
10235 else
10236 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10237 end if;
10238
10239 Set_Etype (New_Bound, Der_T);
10240 return New_Bound;
10241 end Build_Scalar_Bound;
10242
10243 --------------------------------
10244 -- Build_Underlying_Full_View --
10245 --------------------------------
10246
10247 procedure Build_Underlying_Full_View
10248 (N : Node_Id;
10249 Typ : Entity_Id;
10250 Par : Entity_Id)
10251 is
10252 Loc : constant Source_Ptr := Sloc (N);
10253 Subt : constant Entity_Id :=
10254 Make_Defining_Identifier
10255 (Loc, New_External_Name (Chars (Typ), 'S'));
10256
10257 Constr : Node_Id;
10258 Indic : Node_Id;
10259 C : Node_Id;
10260 Id : Node_Id;
10261
10262 procedure Set_Discriminant_Name (Id : Node_Id);
10263 -- If the derived type has discriminants, they may rename discriminants
10264 -- of the parent. When building the full view of the parent, we need to
10265 -- recover the names of the original discriminants if the constraint is
10266 -- given by named associations.
10267
10268 ---------------------------
10269 -- Set_Discriminant_Name --
10270 ---------------------------
10271
10272 procedure Set_Discriminant_Name (Id : Node_Id) is
10273 Disc : Entity_Id;
10274
10275 begin
10276 Set_Original_Discriminant (Id, Empty);
10277
10278 if Has_Discriminants (Typ) then
10279 Disc := First_Discriminant (Typ);
10280 while Present (Disc) loop
10281 if Chars (Disc) = Chars (Id)
10282 and then Present (Corresponding_Discriminant (Disc))
10283 then
10284 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10285 end if;
10286 Next_Discriminant (Disc);
10287 end loop;
10288 end if;
10289 end Set_Discriminant_Name;
10290
10291 -- Start of processing for Build_Underlying_Full_View
10292
10293 begin
10294 if Nkind (N) = N_Full_Type_Declaration then
10295 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10296
10297 elsif Nkind (N) = N_Subtype_Declaration then
10298 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10299
10300 elsif Nkind (N) = N_Component_Declaration then
10301 Constr :=
10302 New_Copy_Tree
10303 (Constraint (Subtype_Indication (Component_Definition (N))));
10304
10305 else
10306 raise Program_Error;
10307 end if;
10308
10309 C := First (Constraints (Constr));
10310 while Present (C) loop
10311 if Nkind (C) = N_Discriminant_Association then
10312 Id := First (Selector_Names (C));
10313 while Present (Id) loop
10314 Set_Discriminant_Name (Id);
10315 Next (Id);
10316 end loop;
10317 end if;
10318
10319 Next (C);
10320 end loop;
10321
10322 Indic :=
10323 Make_Subtype_Declaration (Loc,
10324 Defining_Identifier => Subt,
10325 Subtype_Indication =>
10326 Make_Subtype_Indication (Loc,
10327 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10328 Constraint => New_Copy_Tree (Constr)));
10329
10330 -- If this is a component subtype for an outer itype, it is not
10331 -- a list member, so simply set the parent link for analysis: if
10332 -- the enclosing type does not need to be in a declarative list,
10333 -- neither do the components.
10334
10335 if Is_List_Member (N)
10336 and then Nkind (N) /= N_Component_Declaration
10337 then
10338 Insert_Before (N, Indic);
10339 else
10340 Set_Parent (Indic, Parent (N));
10341 end if;
10342
10343 Analyze (Indic);
10344 Set_Underlying_Full_View (Typ, Full_View (Subt));
10345 Set_Is_Underlying_Full_View (Full_View (Subt));
10346 end Build_Underlying_Full_View;
10347
10348 -------------------------------
10349 -- Check_Abstract_Overriding --
10350 -------------------------------
10351
10352 procedure Check_Abstract_Overriding (T : Entity_Id) is
10353 Alias_Subp : Entity_Id;
10354 Elmt : Elmt_Id;
10355 Op_List : Elist_Id;
10356 Subp : Entity_Id;
10357 Type_Def : Node_Id;
10358
10359 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10360 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10361 -- which has pragma Implemented already set. Check whether Subp's entity
10362 -- kind conforms to the implementation kind of the overridden routine.
10363
10364 procedure Check_Pragma_Implemented
10365 (Subp : Entity_Id;
10366 Iface_Subp : Entity_Id);
10367 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10368 -- Iface_Subp and both entities have pragma Implemented already set on
10369 -- them. Check whether the two implementation kinds are conforming.
10370
10371 procedure Inherit_Pragma_Implemented
10372 (Subp : Entity_Id;
10373 Iface_Subp : Entity_Id);
10374 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10375 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10376 -- Propagate the implementation kind of Iface_Subp to Subp.
10377
10378 ------------------------------
10379 -- Check_Pragma_Implemented --
10380 ------------------------------
10381
10382 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10383 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10384 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10385 Subp_Alias : constant Entity_Id := Alias (Subp);
10386 Contr_Typ : Entity_Id;
10387 Impl_Subp : Entity_Id;
10388
10389 begin
10390 -- Subp must have an alias since it is a hidden entity used to link
10391 -- an interface subprogram to its overriding counterpart.
10392
10393 pragma Assert (Present (Subp_Alias));
10394
10395 -- Handle aliases to synchronized wrappers
10396
10397 Impl_Subp := Subp_Alias;
10398
10399 if Is_Primitive_Wrapper (Impl_Subp) then
10400 Impl_Subp := Wrapped_Entity (Impl_Subp);
10401 end if;
10402
10403 -- Extract the type of the controlling formal
10404
10405 Contr_Typ := Etype (First_Formal (Subp_Alias));
10406
10407 if Is_Concurrent_Record_Type (Contr_Typ) then
10408 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10409 end if;
10410
10411 -- An interface subprogram whose implementation kind is By_Entry must
10412 -- be implemented by an entry.
10413
10414 if Impl_Kind = Name_By_Entry
10415 and then Ekind (Impl_Subp) /= E_Entry
10416 then
10417 Error_Msg_Node_2 := Iface_Alias;
10418 Error_Msg_NE
10419 ("type & must implement abstract subprogram & with an entry",
10420 Subp_Alias, Contr_Typ);
10421
10422 elsif Impl_Kind = Name_By_Protected_Procedure then
10423
10424 -- An interface subprogram whose implementation kind is By_
10425 -- Protected_Procedure cannot be implemented by a primitive
10426 -- procedure of a task type.
10427
10428 if Ekind (Contr_Typ) /= E_Protected_Type then
10429 Error_Msg_Node_2 := Contr_Typ;
10430 Error_Msg_NE
10431 ("interface subprogram & cannot be implemented by a " &
10432 "primitive procedure of task type &", Subp_Alias,
10433 Iface_Alias);
10434
10435 -- An interface subprogram whose implementation kind is By_
10436 -- Protected_Procedure must be implemented by a procedure.
10437
10438 elsif Ekind (Impl_Subp) /= E_Procedure then
10439 Error_Msg_Node_2 := Iface_Alias;
10440 Error_Msg_NE
10441 ("type & must implement abstract subprogram & with a " &
10442 "procedure", Subp_Alias, Contr_Typ);
10443
10444 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10445 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10446 then
10447 Error_Msg_Name_1 := Impl_Kind;
10448 Error_Msg_N
10449 ("overriding operation& must have synchronization%",
10450 Subp_Alias);
10451 end if;
10452
10453 -- If primitive has Optional synchronization, overriding operation
10454 -- must match if it has an explicit synchronization..
10455
10456 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10457 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10458 then
10459 Error_Msg_Name_1 := Impl_Kind;
10460 Error_Msg_N
10461 ("overriding operation& must have syncrhonization%",
10462 Subp_Alias);
10463 end if;
10464 end Check_Pragma_Implemented;
10465
10466 ------------------------------
10467 -- Check_Pragma_Implemented --
10468 ------------------------------
10469
10470 procedure Check_Pragma_Implemented
10471 (Subp : Entity_Id;
10472 Iface_Subp : Entity_Id)
10473 is
10474 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10475 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10476
10477 begin
10478 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10479 -- and overriding subprogram are different. In general this is an
10480 -- error except when the implementation kind of the overridden
10481 -- subprograms is By_Any or Optional.
10482
10483 if Iface_Kind /= Subp_Kind
10484 and then Iface_Kind /= Name_By_Any
10485 and then Iface_Kind /= Name_Optional
10486 then
10487 if Iface_Kind = Name_By_Entry then
10488 Error_Msg_N
10489 ("incompatible implementation kind, overridden subprogram " &
10490 "is marked By_Entry", Subp);
10491 else
10492 Error_Msg_N
10493 ("incompatible implementation kind, overridden subprogram " &
10494 "is marked By_Protected_Procedure", Subp);
10495 end if;
10496 end if;
10497 end Check_Pragma_Implemented;
10498
10499 --------------------------------
10500 -- Inherit_Pragma_Implemented --
10501 --------------------------------
10502
10503 procedure Inherit_Pragma_Implemented
10504 (Subp : Entity_Id;
10505 Iface_Subp : Entity_Id)
10506 is
10507 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10508 Loc : constant Source_Ptr := Sloc (Subp);
10509 Impl_Prag : Node_Id;
10510
10511 begin
10512 -- Since the implementation kind is stored as a representation item
10513 -- rather than a flag, create a pragma node.
10514
10515 Impl_Prag :=
10516 Make_Pragma (Loc,
10517 Chars => Name_Implemented,
10518 Pragma_Argument_Associations => New_List (
10519 Make_Pragma_Argument_Association (Loc,
10520 Expression => New_Occurrence_Of (Subp, Loc)),
10521
10522 Make_Pragma_Argument_Association (Loc,
10523 Expression => Make_Identifier (Loc, Iface_Kind))));
10524
10525 -- The pragma doesn't need to be analyzed because it is internally
10526 -- built. It is safe to directly register it as a rep item since we
10527 -- are only interested in the characters of the implementation kind.
10528
10529 Record_Rep_Item (Subp, Impl_Prag);
10530 end Inherit_Pragma_Implemented;
10531
10532 -- Start of processing for Check_Abstract_Overriding
10533
10534 begin
10535 Op_List := Primitive_Operations (T);
10536
10537 -- Loop to check primitive operations
10538
10539 Elmt := First_Elmt (Op_List);
10540 while Present (Elmt) loop
10541 Subp := Node (Elmt);
10542 Alias_Subp := Alias (Subp);
10543
10544 -- Inherited subprograms are identified by the fact that they do not
10545 -- come from source, and the associated source location is the
10546 -- location of the first subtype of the derived type.
10547
10548 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10549 -- subprograms that "require overriding".
10550
10551 -- Special exception, do not complain about failure to override the
10552 -- stream routines _Input and _Output, as well as the primitive
10553 -- operations used in dispatching selects since we always provide
10554 -- automatic overridings for these subprograms.
10555
10556 -- The partial view of T may have been a private extension, for
10557 -- which inherited functions dispatching on result are abstract.
10558 -- If the full view is a null extension, there is no need for
10559 -- overriding in Ada 2005, but wrappers need to be built for them
10560 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10561
10562 if Is_Null_Extension (T)
10563 and then Has_Controlling_Result (Subp)
10564 and then Ada_Version >= Ada_2005
10565 and then Present (Alias_Subp)
10566 and then not Comes_From_Source (Subp)
10567 and then not Is_Abstract_Subprogram (Alias_Subp)
10568 and then not Is_Access_Type (Etype (Subp))
10569 then
10570 null;
10571
10572 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10573 -- processing because this check is done with the aliased
10574 -- entity
10575
10576 elsif Present (Interface_Alias (Subp)) then
10577 null;
10578
10579 elsif (Is_Abstract_Subprogram (Subp)
10580 or else Requires_Overriding (Subp)
10581 or else
10582 (Has_Controlling_Result (Subp)
10583 and then Present (Alias_Subp)
10584 and then not Comes_From_Source (Subp)
10585 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10586 and then not Is_TSS (Subp, TSS_Stream_Input)
10587 and then not Is_TSS (Subp, TSS_Stream_Output)
10588 and then not Is_Abstract_Type (T)
10589 and then not Is_Predefined_Interface_Primitive (Subp)
10590
10591 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10592 -- with abstract interface types because the check will be done
10593 -- with the aliased entity (otherwise we generate a duplicated
10594 -- error message).
10595
10596 and then not Present (Interface_Alias (Subp))
10597 then
10598 if Present (Alias_Subp) then
10599
10600 -- Only perform the check for a derived subprogram when the
10601 -- type has an explicit record extension. This avoids incorrect
10602 -- flagging of abstract subprograms for the case of a type
10603 -- without an extension that is derived from a formal type
10604 -- with a tagged actual (can occur within a private part).
10605
10606 -- Ada 2005 (AI-391): In the case of an inherited function with
10607 -- a controlling result of the type, the rule does not apply if
10608 -- the type is a null extension (unless the parent function
10609 -- itself is abstract, in which case the function must still be
10610 -- be overridden). The expander will generate an overriding
10611 -- wrapper function calling the parent subprogram (see
10612 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10613
10614 Type_Def := Type_Definition (Parent (T));
10615
10616 if Nkind (Type_Def) = N_Derived_Type_Definition
10617 and then Present (Record_Extension_Part (Type_Def))
10618 and then
10619 (Ada_Version < Ada_2005
10620 or else not Is_Null_Extension (T)
10621 or else Ekind (Subp) = E_Procedure
10622 or else not Has_Controlling_Result (Subp)
10623 or else Is_Abstract_Subprogram (Alias_Subp)
10624 or else Requires_Overriding (Subp)
10625 or else Is_Access_Type (Etype (Subp)))
10626 then
10627 -- Avoid reporting error in case of abstract predefined
10628 -- primitive inherited from interface type because the
10629 -- body of internally generated predefined primitives
10630 -- of tagged types are generated later by Freeze_Type
10631
10632 if Is_Interface (Root_Type (T))
10633 and then Is_Abstract_Subprogram (Subp)
10634 and then Is_Predefined_Dispatching_Operation (Subp)
10635 and then not Comes_From_Source (Ultimate_Alias (Subp))
10636 then
10637 null;
10638
10639 -- A null extension is not obliged to override an inherited
10640 -- procedure subject to pragma Extensions_Visible with value
10641 -- False and at least one controlling OUT parameter
10642 -- (SPARK RM 6.1.7(6)).
10643
10644 elsif Is_Null_Extension (T)
10645 and then Is_EVF_Procedure (Subp)
10646 then
10647 null;
10648
10649 else
10650 Error_Msg_NE
10651 ("type must be declared abstract or & overridden",
10652 T, Subp);
10653
10654 -- Traverse the whole chain of aliased subprograms to
10655 -- complete the error notification. This is especially
10656 -- useful for traceability of the chain of entities when
10657 -- the subprogram corresponds with an interface
10658 -- subprogram (which may be defined in another package).
10659
10660 if Present (Alias_Subp) then
10661 declare
10662 E : Entity_Id;
10663
10664 begin
10665 E := Subp;
10666 while Present (Alias (E)) loop
10667
10668 -- Avoid reporting redundant errors on entities
10669 -- inherited from interfaces
10670
10671 if Sloc (E) /= Sloc (T) then
10672 Error_Msg_Sloc := Sloc (E);
10673 Error_Msg_NE
10674 ("\& has been inherited #", T, Subp);
10675 end if;
10676
10677 E := Alias (E);
10678 end loop;
10679
10680 Error_Msg_Sloc := Sloc (E);
10681
10682 -- AI05-0068: report if there is an overriding
10683 -- non-abstract subprogram that is invisible.
10684
10685 if Is_Hidden (E)
10686 and then not Is_Abstract_Subprogram (E)
10687 then
10688 Error_Msg_NE
10689 ("\& subprogram# is not visible",
10690 T, Subp);
10691
10692 -- Clarify the case where a non-null extension must
10693 -- override inherited procedure subject to pragma
10694 -- Extensions_Visible with value False and at least
10695 -- one controlling OUT param.
10696
10697 elsif Is_EVF_Procedure (E) then
10698 Error_Msg_NE
10699 ("\& # is subject to Extensions_Visible False",
10700 T, Subp);
10701
10702 else
10703 Error_Msg_NE
10704 ("\& has been inherited from subprogram #",
10705 T, Subp);
10706 end if;
10707 end;
10708 end if;
10709 end if;
10710
10711 -- Ada 2005 (AI-345): Protected or task type implementing
10712 -- abstract interfaces.
10713
10714 elsif Is_Concurrent_Record_Type (T)
10715 and then Present (Interfaces (T))
10716 then
10717 -- There is no need to check here RM 9.4(11.9/3) since we
10718 -- are processing the corresponding record type and the
10719 -- mode of the overriding subprograms was verified by
10720 -- Check_Conformance when the corresponding concurrent
10721 -- type declaration was analyzed.
10722
10723 Error_Msg_NE
10724 ("interface subprogram & must be overridden", T, Subp);
10725
10726 -- Examine primitive operations of synchronized type to find
10727 -- homonyms that have the wrong profile.
10728
10729 declare
10730 Prim : Entity_Id;
10731
10732 begin
10733 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10734 while Present (Prim) loop
10735 if Chars (Prim) = Chars (Subp) then
10736 Error_Msg_NE
10737 ("profile is not type conformant with prefixed "
10738 & "view profile of inherited operation&",
10739 Prim, Subp);
10740 end if;
10741
10742 Next_Entity (Prim);
10743 end loop;
10744 end;
10745 end if;
10746
10747 else
10748 Error_Msg_Node_2 := T;
10749 Error_Msg_N
10750 ("abstract subprogram& not allowed for type&", Subp);
10751
10752 -- Also post unconditional warning on the type (unconditional
10753 -- so that if there are more than one of these cases, we get
10754 -- them all, and not just the first one).
10755
10756 Error_Msg_Node_2 := Subp;
10757 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10758 end if;
10759
10760 -- A subprogram subject to pragma Extensions_Visible with value
10761 -- "True" cannot override a subprogram subject to the same pragma
10762 -- with value "False" (SPARK RM 6.1.7(5)).
10763
10764 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10765 and then Present (Overridden_Operation (Subp))
10766 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10767 Extensions_Visible_False
10768 then
10769 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10770 Error_Msg_N
10771 ("subprogram & with Extensions_Visible True cannot override "
10772 & "subprogram # with Extensions_Visible False", Subp);
10773 end if;
10774
10775 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10776
10777 -- Subp is an expander-generated procedure which maps an interface
10778 -- alias to a protected wrapper. The interface alias is flagged by
10779 -- pragma Implemented. Ensure that Subp is a procedure when the
10780 -- implementation kind is By_Protected_Procedure or an entry when
10781 -- By_Entry.
10782
10783 if Ada_Version >= Ada_2012
10784 and then Is_Hidden (Subp)
10785 and then Present (Interface_Alias (Subp))
10786 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10787 then
10788 Check_Pragma_Implemented (Subp);
10789 end if;
10790
10791 -- Subp is an interface primitive which overrides another interface
10792 -- primitive marked with pragma Implemented.
10793
10794 if Ada_Version >= Ada_2012
10795 and then Present (Overridden_Operation (Subp))
10796 and then Has_Rep_Pragma
10797 (Overridden_Operation (Subp), Name_Implemented)
10798 then
10799 -- If the overriding routine is also marked by Implemented, check
10800 -- that the two implementation kinds are conforming.
10801
10802 if Has_Rep_Pragma (Subp, Name_Implemented) then
10803 Check_Pragma_Implemented
10804 (Subp => Subp,
10805 Iface_Subp => Overridden_Operation (Subp));
10806
10807 -- Otherwise the overriding routine inherits the implementation
10808 -- kind from the overridden subprogram.
10809
10810 else
10811 Inherit_Pragma_Implemented
10812 (Subp => Subp,
10813 Iface_Subp => Overridden_Operation (Subp));
10814 end if;
10815 end if;
10816
10817 -- If the operation is a wrapper for a synchronized primitive, it
10818 -- may be called indirectly through a dispatching select. We assume
10819 -- that it will be referenced elsewhere indirectly, and suppress
10820 -- warnings about an unused entity.
10821
10822 if Is_Primitive_Wrapper (Subp)
10823 and then Present (Wrapped_Entity (Subp))
10824 then
10825 Set_Referenced (Wrapped_Entity (Subp));
10826 end if;
10827
10828 Next_Elmt (Elmt);
10829 end loop;
10830 end Check_Abstract_Overriding;
10831
10832 ------------------------------------------------
10833 -- Check_Access_Discriminant_Requires_Limited --
10834 ------------------------------------------------
10835
10836 procedure Check_Access_Discriminant_Requires_Limited
10837 (D : Node_Id;
10838 Loc : Node_Id)
10839 is
10840 begin
10841 -- A discriminant_specification for an access discriminant shall appear
10842 -- only in the declaration for a task or protected type, or for a type
10843 -- with the reserved word 'limited' in its definition or in one of its
10844 -- ancestors (RM 3.7(10)).
10845
10846 -- AI-0063: The proper condition is that type must be immutably limited,
10847 -- or else be a partial view.
10848
10849 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10850 if Is_Limited_View (Current_Scope)
10851 or else
10852 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10853 and then Limited_Present (Parent (Current_Scope)))
10854 then
10855 null;
10856
10857 else
10858 Error_Msg_N
10859 ("access discriminants allowed only for limited types", Loc);
10860 end if;
10861 end if;
10862 end Check_Access_Discriminant_Requires_Limited;
10863
10864 -----------------------------------
10865 -- Check_Aliased_Component_Types --
10866 -----------------------------------
10867
10868 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10869 C : Entity_Id;
10870
10871 begin
10872 -- ??? Also need to check components of record extensions, but not
10873 -- components of protected types (which are always limited).
10874
10875 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10876 -- types to be unconstrained. This is safe because it is illegal to
10877 -- create access subtypes to such types with explicit discriminant
10878 -- constraints.
10879
10880 if not Is_Limited_Type (T) then
10881 if Ekind (T) = E_Record_Type then
10882 C := First_Component (T);
10883 while Present (C) loop
10884 if Is_Aliased (C)
10885 and then Has_Discriminants (Etype (C))
10886 and then not Is_Constrained (Etype (C))
10887 and then not In_Instance_Body
10888 and then Ada_Version < Ada_2005
10889 then
10890 Error_Msg_N
10891 ("aliased component must be constrained (RM 3.6(11))",
10892 C);
10893 end if;
10894
10895 Next_Component (C);
10896 end loop;
10897
10898 elsif Ekind (T) = E_Array_Type then
10899 if Has_Aliased_Components (T)
10900 and then Has_Discriminants (Component_Type (T))
10901 and then not Is_Constrained (Component_Type (T))
10902 and then not In_Instance_Body
10903 and then Ada_Version < Ada_2005
10904 then
10905 Error_Msg_N
10906 ("aliased component type must be constrained (RM 3.6(11))",
10907 T);
10908 end if;
10909 end if;
10910 end if;
10911 end Check_Aliased_Component_Types;
10912
10913 ---------------------------------------
10914 -- Check_Anonymous_Access_Components --
10915 ---------------------------------------
10916
10917 procedure Check_Anonymous_Access_Components
10918 (Typ_Decl : Node_Id;
10919 Typ : Entity_Id;
10920 Prev : Entity_Id;
10921 Comp_List : Node_Id)
10922 is
10923 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10924 Anon_Access : Entity_Id;
10925 Acc_Def : Node_Id;
10926 Comp : Node_Id;
10927 Comp_Def : Node_Id;
10928 Decl : Node_Id;
10929 Type_Def : Node_Id;
10930
10931 procedure Build_Incomplete_Type_Declaration;
10932 -- If the record type contains components that include an access to the
10933 -- current record, then create an incomplete type declaration for the
10934 -- record, to be used as the designated type of the anonymous access.
10935 -- This is done only once, and only if there is no previous partial
10936 -- view of the type.
10937
10938 function Designates_T (Subt : Node_Id) return Boolean;
10939 -- Check whether a node designates the enclosing record type, or 'Class
10940 -- of that type
10941
10942 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10943 -- Check whether an access definition includes a reference to
10944 -- the enclosing record type. The reference can be a subtype mark
10945 -- in the access definition itself, a 'Class attribute reference, or
10946 -- recursively a reference appearing in a parameter specification
10947 -- or result definition of an access_to_subprogram definition.
10948
10949 --------------------------------------
10950 -- Build_Incomplete_Type_Declaration --
10951 --------------------------------------
10952
10953 procedure Build_Incomplete_Type_Declaration is
10954 Decl : Node_Id;
10955 Inc_T : Entity_Id;
10956 H : Entity_Id;
10957
10958 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10959 -- it's "is new ... with record" or else "is tagged record ...".
10960
10961 Is_Tagged : constant Boolean :=
10962 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10963 and then
10964 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10965 or else
10966 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10967 and then Tagged_Present (Type_Definition (Typ_Decl)));
10968
10969 begin
10970 -- If there is a previous partial view, no need to create a new one
10971 -- If the partial view, given by Prev, is incomplete, If Prev is
10972 -- a private declaration, full declaration is flagged accordingly.
10973
10974 if Prev /= Typ then
10975 if Is_Tagged then
10976 Make_Class_Wide_Type (Prev);
10977 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10978 Set_Etype (Class_Wide_Type (Typ), Typ);
10979 end if;
10980
10981 return;
10982
10983 elsif Has_Private_Declaration (Typ) then
10984
10985 -- If we refer to T'Class inside T, and T is the completion of a
10986 -- private type, then make sure the class-wide type exists.
10987
10988 if Is_Tagged then
10989 Make_Class_Wide_Type (Typ);
10990 end if;
10991
10992 return;
10993
10994 -- If there was a previous anonymous access type, the incomplete
10995 -- type declaration will have been created already.
10996
10997 elsif Present (Current_Entity (Typ))
10998 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10999 and then Full_View (Current_Entity (Typ)) = Typ
11000 then
11001 if Is_Tagged
11002 and then Comes_From_Source (Current_Entity (Typ))
11003 and then not Is_Tagged_Type (Current_Entity (Typ))
11004 then
11005 Make_Class_Wide_Type (Typ);
11006 Error_Msg_N
11007 ("incomplete view of tagged type should be declared tagged??",
11008 Parent (Current_Entity (Typ)));
11009 end if;
11010 return;
11011
11012 else
11013 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11014 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11015
11016 -- Type has already been inserted into the current scope. Remove
11017 -- it, and add incomplete declaration for type, so that subsequent
11018 -- anonymous access types can use it. The entity is unchained from
11019 -- the homonym list and from immediate visibility. After analysis,
11020 -- the entity in the incomplete declaration becomes immediately
11021 -- visible in the record declaration that follows.
11022
11023 H := Current_Entity (Typ);
11024
11025 if H = Typ then
11026 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11027 else
11028 while Present (H)
11029 and then Homonym (H) /= Typ
11030 loop
11031 H := Homonym (Typ);
11032 end loop;
11033
11034 Set_Homonym (H, Homonym (Typ));
11035 end if;
11036
11037 Insert_Before (Typ_Decl, Decl);
11038 Analyze (Decl);
11039 Set_Full_View (Inc_T, Typ);
11040
11041 if Is_Tagged then
11042
11043 -- Create a common class-wide type for both views, and set the
11044 -- Etype of the class-wide type to the full view.
11045
11046 Make_Class_Wide_Type (Inc_T);
11047 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11048 Set_Etype (Class_Wide_Type (Typ), Typ);
11049 end if;
11050 end if;
11051 end Build_Incomplete_Type_Declaration;
11052
11053 ------------------
11054 -- Designates_T --
11055 ------------------
11056
11057 function Designates_T (Subt : Node_Id) return Boolean is
11058 Type_Id : constant Name_Id := Chars (Typ);
11059
11060 function Names_T (Nam : Node_Id) return Boolean;
11061 -- The record type has not been introduced in the current scope
11062 -- yet, so we must examine the name of the type itself, either
11063 -- an identifier T, or an expanded name of the form P.T, where
11064 -- P denotes the current scope.
11065
11066 -------------
11067 -- Names_T --
11068 -------------
11069
11070 function Names_T (Nam : Node_Id) return Boolean is
11071 begin
11072 if Nkind (Nam) = N_Identifier then
11073 return Chars (Nam) = Type_Id;
11074
11075 elsif Nkind (Nam) = N_Selected_Component then
11076 if Chars (Selector_Name (Nam)) = Type_Id then
11077 if Nkind (Prefix (Nam)) = N_Identifier then
11078 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11079
11080 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11081 return Chars (Selector_Name (Prefix (Nam))) =
11082 Chars (Current_Scope);
11083 else
11084 return False;
11085 end if;
11086
11087 else
11088 return False;
11089 end if;
11090
11091 else
11092 return False;
11093 end if;
11094 end Names_T;
11095
11096 -- Start of processing for Designates_T
11097
11098 begin
11099 if Nkind (Subt) = N_Identifier then
11100 return Chars (Subt) = Type_Id;
11101
11102 -- Reference can be through an expanded name which has not been
11103 -- analyzed yet, and which designates enclosing scopes.
11104
11105 elsif Nkind (Subt) = N_Selected_Component then
11106 if Names_T (Subt) then
11107 return True;
11108
11109 -- Otherwise it must denote an entity that is already visible.
11110 -- The access definition may name a subtype of the enclosing
11111 -- type, if there is a previous incomplete declaration for it.
11112
11113 else
11114 Find_Selected_Component (Subt);
11115 return
11116 Is_Entity_Name (Subt)
11117 and then Scope (Entity (Subt)) = Current_Scope
11118 and then
11119 (Chars (Base_Type (Entity (Subt))) = Type_Id
11120 or else
11121 (Is_Class_Wide_Type (Entity (Subt))
11122 and then
11123 Chars (Etype (Base_Type (Entity (Subt)))) =
11124 Type_Id));
11125 end if;
11126
11127 -- A reference to the current type may appear as the prefix of
11128 -- a 'Class attribute.
11129
11130 elsif Nkind (Subt) = N_Attribute_Reference
11131 and then Attribute_Name (Subt) = Name_Class
11132 then
11133 return Names_T (Prefix (Subt));
11134
11135 else
11136 return False;
11137 end if;
11138 end Designates_T;
11139
11140 ----------------
11141 -- Mentions_T --
11142 ----------------
11143
11144 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11145 Param_Spec : Node_Id;
11146
11147 Acc_Subprg : constant Node_Id :=
11148 Access_To_Subprogram_Definition (Acc_Def);
11149
11150 begin
11151 if No (Acc_Subprg) then
11152 return Designates_T (Subtype_Mark (Acc_Def));
11153 end if;
11154
11155 -- Component is an access_to_subprogram: examine its formals,
11156 -- and result definition in the case of an access_to_function.
11157
11158 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11159 while Present (Param_Spec) loop
11160 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11161 and then Mentions_T (Parameter_Type (Param_Spec))
11162 then
11163 return True;
11164
11165 elsif Designates_T (Parameter_Type (Param_Spec)) then
11166 return True;
11167 end if;
11168
11169 Next (Param_Spec);
11170 end loop;
11171
11172 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11173 if Nkind (Result_Definition (Acc_Subprg)) =
11174 N_Access_Definition
11175 then
11176 return Mentions_T (Result_Definition (Acc_Subprg));
11177 else
11178 return Designates_T (Result_Definition (Acc_Subprg));
11179 end if;
11180 end if;
11181
11182 return False;
11183 end Mentions_T;
11184
11185 -- Start of processing for Check_Anonymous_Access_Components
11186
11187 begin
11188 if No (Comp_List) then
11189 return;
11190 end if;
11191
11192 Comp := First (Component_Items (Comp_List));
11193 while Present (Comp) loop
11194 if Nkind (Comp) = N_Component_Declaration
11195 and then Present
11196 (Access_Definition (Component_Definition (Comp)))
11197 and then
11198 Mentions_T (Access_Definition (Component_Definition (Comp)))
11199 then
11200 Comp_Def := Component_Definition (Comp);
11201 Acc_Def :=
11202 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11203
11204 Build_Incomplete_Type_Declaration;
11205 Anon_Access := Make_Temporary (Loc, 'S');
11206
11207 -- Create a declaration for the anonymous access type: either
11208 -- an access_to_object or an access_to_subprogram.
11209
11210 if Present (Acc_Def) then
11211 if Nkind (Acc_Def) = N_Access_Function_Definition then
11212 Type_Def :=
11213 Make_Access_Function_Definition (Loc,
11214 Parameter_Specifications =>
11215 Parameter_Specifications (Acc_Def),
11216 Result_Definition => Result_Definition (Acc_Def));
11217 else
11218 Type_Def :=
11219 Make_Access_Procedure_Definition (Loc,
11220 Parameter_Specifications =>
11221 Parameter_Specifications (Acc_Def));
11222 end if;
11223
11224 else
11225 Type_Def :=
11226 Make_Access_To_Object_Definition (Loc,
11227 Subtype_Indication =>
11228 Relocate_Node
11229 (Subtype_Mark (Access_Definition (Comp_Def))));
11230
11231 Set_Constant_Present
11232 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11233 Set_All_Present
11234 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11235 end if;
11236
11237 Set_Null_Exclusion_Present
11238 (Type_Def,
11239 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11240
11241 Decl :=
11242 Make_Full_Type_Declaration (Loc,
11243 Defining_Identifier => Anon_Access,
11244 Type_Definition => Type_Def);
11245
11246 Insert_Before (Typ_Decl, Decl);
11247 Analyze (Decl);
11248
11249 -- If an access to subprogram, create the extra formals
11250
11251 if Present (Acc_Def) then
11252 Create_Extra_Formals (Designated_Type (Anon_Access));
11253
11254 -- If an access to object, preserve entity of designated type,
11255 -- for ASIS use, before rewriting the component definition.
11256
11257 else
11258 declare
11259 Desig : Entity_Id;
11260
11261 begin
11262 Desig := Entity (Subtype_Indication (Type_Def));
11263
11264 -- If the access definition is to the current record,
11265 -- the visible entity at this point is an incomplete
11266 -- type. Retrieve the full view to simplify ASIS queries
11267
11268 if Ekind (Desig) = E_Incomplete_Type then
11269 Desig := Full_View (Desig);
11270 end if;
11271
11272 Set_Entity
11273 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11274 end;
11275 end if;
11276
11277 Rewrite (Comp_Def,
11278 Make_Component_Definition (Loc,
11279 Subtype_Indication =>
11280 New_Occurrence_Of (Anon_Access, Loc)));
11281
11282 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11283 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11284 else
11285 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11286 end if;
11287
11288 Set_Is_Local_Anonymous_Access (Anon_Access);
11289 end if;
11290
11291 Next (Comp);
11292 end loop;
11293
11294 if Present (Variant_Part (Comp_List)) then
11295 declare
11296 V : Node_Id;
11297 begin
11298 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11299 while Present (V) loop
11300 Check_Anonymous_Access_Components
11301 (Typ_Decl, Typ, Prev, Component_List (V));
11302 Next_Non_Pragma (V);
11303 end loop;
11304 end;
11305 end if;
11306 end Check_Anonymous_Access_Components;
11307
11308 ----------------------
11309 -- Check_Completion --
11310 ----------------------
11311
11312 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11313 E : Entity_Id;
11314
11315 procedure Post_Error;
11316 -- Post error message for lack of completion for entity E
11317
11318 ----------------
11319 -- Post_Error --
11320 ----------------
11321
11322 procedure Post_Error is
11323 procedure Missing_Body;
11324 -- Output missing body message
11325
11326 ------------------
11327 -- Missing_Body --
11328 ------------------
11329
11330 procedure Missing_Body is
11331 begin
11332 -- Spec is in same unit, so we can post on spec
11333
11334 if In_Same_Source_Unit (Body_Id, E) then
11335 Error_Msg_N ("missing body for &", E);
11336
11337 -- Spec is in a separate unit, so we have to post on the body
11338
11339 else
11340 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11341 end if;
11342 end Missing_Body;
11343
11344 -- Start of processing for Post_Error
11345
11346 begin
11347 if not Comes_From_Source (E) then
11348 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11349
11350 -- It may be an anonymous protected type created for a
11351 -- single variable. Post error on variable, if present.
11352
11353 declare
11354 Var : Entity_Id;
11355
11356 begin
11357 Var := First_Entity (Current_Scope);
11358 while Present (Var) loop
11359 exit when Etype (Var) = E
11360 and then Comes_From_Source (Var);
11361
11362 Next_Entity (Var);
11363 end loop;
11364
11365 if Present (Var) then
11366 E := Var;
11367 end if;
11368 end;
11369 end if;
11370 end if;
11371
11372 -- If a generated entity has no completion, then either previous
11373 -- semantic errors have disabled the expansion phase, or else we had
11374 -- missing subunits, or else we are compiling without expansion,
11375 -- or else something is very wrong.
11376
11377 if not Comes_From_Source (E) then
11378 pragma Assert
11379 (Serious_Errors_Detected > 0
11380 or else Configurable_Run_Time_Violations > 0
11381 or else Subunits_Missing
11382 or else not Expander_Active);
11383 return;
11384
11385 -- Here for source entity
11386
11387 else
11388 -- Here if no body to post the error message, so we post the error
11389 -- on the declaration that has no completion. This is not really
11390 -- the right place to post it, think about this later ???
11391
11392 if No (Body_Id) then
11393 if Is_Type (E) then
11394 Error_Msg_NE
11395 ("missing full declaration for }", Parent (E), E);
11396 else
11397 Error_Msg_NE ("missing body for &", Parent (E), E);
11398 end if;
11399
11400 -- Package body has no completion for a declaration that appears
11401 -- in the corresponding spec. Post error on the body, with a
11402 -- reference to the non-completed declaration.
11403
11404 else
11405 Error_Msg_Sloc := Sloc (E);
11406
11407 if Is_Type (E) then
11408 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11409
11410 elsif Is_Overloadable (E)
11411 and then Current_Entity_In_Scope (E) /= E
11412 then
11413 -- It may be that the completion is mistyped and appears as
11414 -- a distinct overloading of the entity.
11415
11416 declare
11417 Candidate : constant Entity_Id :=
11418 Current_Entity_In_Scope (E);
11419 Decl : constant Node_Id :=
11420 Unit_Declaration_Node (Candidate);
11421
11422 begin
11423 if Is_Overloadable (Candidate)
11424 and then Ekind (Candidate) = Ekind (E)
11425 and then Nkind (Decl) = N_Subprogram_Body
11426 and then Acts_As_Spec (Decl)
11427 then
11428 Check_Type_Conformant (Candidate, E);
11429
11430 else
11431 Missing_Body;
11432 end if;
11433 end;
11434
11435 else
11436 Missing_Body;
11437 end if;
11438 end if;
11439 end if;
11440 end Post_Error;
11441
11442 -- Local variables
11443
11444 Pack_Id : constant Entity_Id := Current_Scope;
11445
11446 -- Start of processing for Check_Completion
11447
11448 begin
11449 E := First_Entity (Pack_Id);
11450 while Present (E) loop
11451 if Is_Intrinsic_Subprogram (E) then
11452 null;
11453
11454 -- The following situation requires special handling: a child unit
11455 -- that appears in the context clause of the body of its parent:
11456
11457 -- procedure Parent.Child (...);
11458
11459 -- with Parent.Child;
11460 -- package body Parent is
11461
11462 -- Here Parent.Child appears as a local entity, but should not be
11463 -- flagged as requiring completion, because it is a compilation
11464 -- unit.
11465
11466 -- Ignore missing completion for a subprogram that does not come from
11467 -- source (including the _Call primitive operation of RAS types,
11468 -- which has to have the flag Comes_From_Source for other purposes):
11469 -- we assume that the expander will provide the missing completion.
11470 -- In case of previous errors, other expansion actions that provide
11471 -- bodies for null procedures with not be invoked, so inhibit message
11472 -- in those cases.
11473
11474 -- Note that E_Operator is not in the list that follows, because
11475 -- this kind is reserved for predefined operators, that are
11476 -- intrinsic and do not need completion.
11477
11478 elsif Ekind_In (E, E_Function,
11479 E_Procedure,
11480 E_Generic_Function,
11481 E_Generic_Procedure)
11482 then
11483 if Has_Completion (E) then
11484 null;
11485
11486 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11487 null;
11488
11489 elsif Is_Subprogram (E)
11490 and then (not Comes_From_Source (E)
11491 or else Chars (E) = Name_uCall)
11492 then
11493 null;
11494
11495 elsif
11496 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11497 then
11498 null;
11499
11500 elsif Nkind (Parent (E)) = N_Procedure_Specification
11501 and then Null_Present (Parent (E))
11502 and then Serious_Errors_Detected > 0
11503 then
11504 null;
11505
11506 else
11507 Post_Error;
11508 end if;
11509
11510 elsif Is_Entry (E) then
11511 if not Has_Completion (E) and then
11512 (Ekind (Scope (E)) = E_Protected_Object
11513 or else Ekind (Scope (E)) = E_Protected_Type)
11514 then
11515 Post_Error;
11516 end if;
11517
11518 elsif Is_Package_Or_Generic_Package (E) then
11519 if Unit_Requires_Body (E) then
11520 if not Has_Completion (E)
11521 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11522 N_Compilation_Unit
11523 then
11524 Post_Error;
11525 end if;
11526
11527 elsif not Is_Child_Unit (E) then
11528 May_Need_Implicit_Body (E);
11529 end if;
11530
11531 -- A formal incomplete type (Ada 2012) does not require a completion;
11532 -- other incomplete type declarations do.
11533
11534 elsif Ekind (E) = E_Incomplete_Type
11535 and then No (Underlying_Type (E))
11536 and then not Is_Generic_Type (E)
11537 then
11538 Post_Error;
11539
11540 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11541 and then not Has_Completion (E)
11542 then
11543 Post_Error;
11544
11545 -- A single task declared in the current scope is a constant, verify
11546 -- that the body of its anonymous type is in the same scope. If the
11547 -- task is defined elsewhere, this may be a renaming declaration for
11548 -- which no completion is needed.
11549
11550 elsif Ekind (E) = E_Constant
11551 and then Ekind (Etype (E)) = E_Task_Type
11552 and then not Has_Completion (Etype (E))
11553 and then Scope (Etype (E)) = Current_Scope
11554 then
11555 Post_Error;
11556
11557 elsif Ekind (E) = E_Protected_Object
11558 and then not Has_Completion (Etype (E))
11559 then
11560 Post_Error;
11561
11562 elsif Ekind (E) = E_Record_Type then
11563 if Is_Tagged_Type (E) then
11564 Check_Abstract_Overriding (E);
11565 Check_Conventions (E);
11566 end if;
11567
11568 Check_Aliased_Component_Types (E);
11569
11570 elsif Ekind (E) = E_Array_Type then
11571 Check_Aliased_Component_Types (E);
11572
11573 end if;
11574
11575 Next_Entity (E);
11576 end loop;
11577 end Check_Completion;
11578
11579 ------------------------------------
11580 -- Check_CPP_Type_Has_No_Defaults --
11581 ------------------------------------
11582
11583 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11584 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11585 Clist : Node_Id;
11586 Comp : Node_Id;
11587
11588 begin
11589 -- Obtain the component list
11590
11591 if Nkind (Tdef) = N_Record_Definition then
11592 Clist := Component_List (Tdef);
11593 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11594 Clist := Component_List (Record_Extension_Part (Tdef));
11595 end if;
11596
11597 -- Check all components to ensure no default expressions
11598
11599 if Present (Clist) then
11600 Comp := First (Component_Items (Clist));
11601 while Present (Comp) loop
11602 if Present (Expression (Comp)) then
11603 Error_Msg_N
11604 ("component of imported 'C'P'P type cannot have "
11605 & "default expression", Expression (Comp));
11606 end if;
11607
11608 Next (Comp);
11609 end loop;
11610 end if;
11611 end Check_CPP_Type_Has_No_Defaults;
11612
11613 ----------------------------
11614 -- Check_Delta_Expression --
11615 ----------------------------
11616
11617 procedure Check_Delta_Expression (E : Node_Id) is
11618 begin
11619 if not (Is_Real_Type (Etype (E))) then
11620 Wrong_Type (E, Any_Real);
11621
11622 elsif not Is_OK_Static_Expression (E) then
11623 Flag_Non_Static_Expr
11624 ("non-static expression used for delta value!", E);
11625
11626 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11627 Error_Msg_N ("delta expression must be positive", E);
11628
11629 else
11630 return;
11631 end if;
11632
11633 -- If any of above errors occurred, then replace the incorrect
11634 -- expression by the real 0.1, which should prevent further errors.
11635
11636 Rewrite (E,
11637 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11638 Analyze_And_Resolve (E, Standard_Float);
11639 end Check_Delta_Expression;
11640
11641 -----------------------------
11642 -- Check_Digits_Expression --
11643 -----------------------------
11644
11645 procedure Check_Digits_Expression (E : Node_Id) is
11646 begin
11647 if not (Is_Integer_Type (Etype (E))) then
11648 Wrong_Type (E, Any_Integer);
11649
11650 elsif not Is_OK_Static_Expression (E) then
11651 Flag_Non_Static_Expr
11652 ("non-static expression used for digits value!", E);
11653
11654 elsif Expr_Value (E) <= 0 then
11655 Error_Msg_N ("digits value must be greater than zero", E);
11656
11657 else
11658 return;
11659 end if;
11660
11661 -- If any of above errors occurred, then replace the incorrect
11662 -- expression by the integer 1, which should prevent further errors.
11663
11664 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11665 Analyze_And_Resolve (E, Standard_Integer);
11666
11667 end Check_Digits_Expression;
11668
11669 --------------------------
11670 -- Check_Initialization --
11671 --------------------------
11672
11673 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11674 begin
11675 -- Special processing for limited types
11676
11677 if Is_Limited_Type (T)
11678 and then not In_Instance
11679 and then not In_Inlined_Body
11680 then
11681 if not OK_For_Limited_Init (T, Exp) then
11682
11683 -- In GNAT mode, this is just a warning, to allow it to be evilly
11684 -- turned off. Otherwise it is a real error.
11685
11686 if GNAT_Mode then
11687 Error_Msg_N
11688 ("??cannot initialize entities of limited type!", Exp);
11689
11690 elsif Ada_Version < Ada_2005 then
11691
11692 -- The side effect removal machinery may generate illegal Ada
11693 -- code to avoid the usage of access types and 'reference in
11694 -- SPARK mode. Since this is legal code with respect to theorem
11695 -- proving, do not emit the error.
11696
11697 if GNATprove_Mode
11698 and then Nkind (Exp) = N_Function_Call
11699 and then Nkind (Parent (Exp)) = N_Object_Declaration
11700 and then not Comes_From_Source
11701 (Defining_Identifier (Parent (Exp)))
11702 then
11703 null;
11704
11705 else
11706 Error_Msg_N
11707 ("cannot initialize entities of limited type", Exp);
11708 Explain_Limited_Type (T, Exp);
11709 end if;
11710
11711 else
11712 -- Specialize error message according to kind of illegal
11713 -- initial expression.
11714
11715 if Nkind (Exp) = N_Type_Conversion
11716 and then Nkind (Expression (Exp)) = N_Function_Call
11717 then
11718 Error_Msg_N
11719 ("illegal context for call"
11720 & " to function with limited result", Exp);
11721
11722 else
11723 Error_Msg_N
11724 ("initialization of limited object requires aggregate "
11725 & "or function call", Exp);
11726 end if;
11727 end if;
11728 end if;
11729 end if;
11730
11731 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11732 -- set unless we can be sure that no range check is required.
11733
11734 if (GNATprove_Mode or not Expander_Active)
11735 and then Is_Scalar_Type (T)
11736 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11737 then
11738 Set_Do_Range_Check (Exp);
11739 end if;
11740 end Check_Initialization;
11741
11742 ----------------------
11743 -- Check_Interfaces --
11744 ----------------------
11745
11746 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11747 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11748
11749 Iface : Node_Id;
11750 Iface_Def : Node_Id;
11751 Iface_Typ : Entity_Id;
11752 Parent_Node : Node_Id;
11753
11754 Is_Task : Boolean := False;
11755 -- Set True if parent type or any progenitor is a task interface
11756
11757 Is_Protected : Boolean := False;
11758 -- Set True if parent type or any progenitor is a protected interface
11759
11760 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11761 -- Check that a progenitor is compatible with declaration. If an error
11762 -- message is output, it is posted on Error_Node.
11763
11764 ------------------
11765 -- Check_Ifaces --
11766 ------------------
11767
11768 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11769 Iface_Id : constant Entity_Id :=
11770 Defining_Identifier (Parent (Iface_Def));
11771 Type_Def : Node_Id;
11772
11773 begin
11774 if Nkind (N) = N_Private_Extension_Declaration then
11775 Type_Def := N;
11776 else
11777 Type_Def := Type_Definition (N);
11778 end if;
11779
11780 if Is_Task_Interface (Iface_Id) then
11781 Is_Task := True;
11782
11783 elsif Is_Protected_Interface (Iface_Id) then
11784 Is_Protected := True;
11785 end if;
11786
11787 if Is_Synchronized_Interface (Iface_Id) then
11788
11789 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11790 -- extension derived from a synchronized interface must explicitly
11791 -- be declared synchronized, because the full view will be a
11792 -- synchronized type.
11793
11794 if Nkind (N) = N_Private_Extension_Declaration then
11795 if not Synchronized_Present (N) then
11796 Error_Msg_NE
11797 ("private extension of& must be explicitly synchronized",
11798 N, Iface_Id);
11799 end if;
11800
11801 -- However, by 3.9.4(16/2), a full type that is a record extension
11802 -- is never allowed to derive from a synchronized interface (note
11803 -- that interfaces must be excluded from this check, because those
11804 -- are represented by derived type definitions in some cases).
11805
11806 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11807 and then not Interface_Present (Type_Definition (N))
11808 then
11809 Error_Msg_N ("record extension cannot derive from synchronized "
11810 & "interface", Error_Node);
11811 end if;
11812 end if;
11813
11814 -- Check that the characteristics of the progenitor are compatible
11815 -- with the explicit qualifier in the declaration.
11816 -- The check only applies to qualifiers that come from source.
11817 -- Limited_Present also appears in the declaration of corresponding
11818 -- records, and the check does not apply to them.
11819
11820 if Limited_Present (Type_Def)
11821 and then not
11822 Is_Concurrent_Record_Type (Defining_Identifier (N))
11823 then
11824 if Is_Limited_Interface (Parent_Type)
11825 and then not Is_Limited_Interface (Iface_Id)
11826 then
11827 Error_Msg_NE
11828 ("progenitor & must be limited interface",
11829 Error_Node, Iface_Id);
11830
11831 elsif
11832 (Task_Present (Iface_Def)
11833 or else Protected_Present (Iface_Def)
11834 or else Synchronized_Present (Iface_Def))
11835 and then Nkind (N) /= N_Private_Extension_Declaration
11836 and then not Error_Posted (N)
11837 then
11838 Error_Msg_NE
11839 ("progenitor & must be limited interface",
11840 Error_Node, Iface_Id);
11841 end if;
11842
11843 -- Protected interfaces can only inherit from limited, synchronized
11844 -- or protected interfaces.
11845
11846 elsif Nkind (N) = N_Full_Type_Declaration
11847 and then Protected_Present (Type_Def)
11848 then
11849 if Limited_Present (Iface_Def)
11850 or else Synchronized_Present (Iface_Def)
11851 or else Protected_Present (Iface_Def)
11852 then
11853 null;
11854
11855 elsif Task_Present (Iface_Def) then
11856 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11857 & "from task interface", Error_Node);
11858
11859 else
11860 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11861 & "from non-limited interface", Error_Node);
11862 end if;
11863
11864 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11865 -- limited and synchronized.
11866
11867 elsif Synchronized_Present (Type_Def) then
11868 if Limited_Present (Iface_Def)
11869 or else Synchronized_Present (Iface_Def)
11870 then
11871 null;
11872
11873 elsif Protected_Present (Iface_Def)
11874 and then Nkind (N) /= N_Private_Extension_Declaration
11875 then
11876 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11877 & "from protected interface", Error_Node);
11878
11879 elsif Task_Present (Iface_Def)
11880 and then Nkind (N) /= N_Private_Extension_Declaration
11881 then
11882 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11883 & "from task interface", Error_Node);
11884
11885 elsif not Is_Limited_Interface (Iface_Id) then
11886 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11887 & "from non-limited interface", Error_Node);
11888 end if;
11889
11890 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11891 -- synchronized or task interfaces.
11892
11893 elsif Nkind (N) = N_Full_Type_Declaration
11894 and then Task_Present (Type_Def)
11895 then
11896 if Limited_Present (Iface_Def)
11897 or else Synchronized_Present (Iface_Def)
11898 or else Task_Present (Iface_Def)
11899 then
11900 null;
11901
11902 elsif Protected_Present (Iface_Def) then
11903 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11904 & "protected interface", Error_Node);
11905
11906 else
11907 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11908 & "non-limited interface", Error_Node);
11909 end if;
11910 end if;
11911 end Check_Ifaces;
11912
11913 -- Start of processing for Check_Interfaces
11914
11915 begin
11916 if Is_Interface (Parent_Type) then
11917 if Is_Task_Interface (Parent_Type) then
11918 Is_Task := True;
11919
11920 elsif Is_Protected_Interface (Parent_Type) then
11921 Is_Protected := True;
11922 end if;
11923 end if;
11924
11925 if Nkind (N) = N_Private_Extension_Declaration then
11926
11927 -- Check that progenitors are compatible with declaration
11928
11929 Iface := First (Interface_List (Def));
11930 while Present (Iface) loop
11931 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11932
11933 Parent_Node := Parent (Base_Type (Iface_Typ));
11934 Iface_Def := Type_Definition (Parent_Node);
11935
11936 if not Is_Interface (Iface_Typ) then
11937 Diagnose_Interface (Iface, Iface_Typ);
11938 else
11939 Check_Ifaces (Iface_Def, Iface);
11940 end if;
11941
11942 Next (Iface);
11943 end loop;
11944
11945 if Is_Task and Is_Protected then
11946 Error_Msg_N
11947 ("type cannot derive from task and protected interface", N);
11948 end if;
11949
11950 return;
11951 end if;
11952
11953 -- Full type declaration of derived type.
11954 -- Check compatibility with parent if it is interface type
11955
11956 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11957 and then Is_Interface (Parent_Type)
11958 then
11959 Parent_Node := Parent (Parent_Type);
11960
11961 -- More detailed checks for interface varieties
11962
11963 Check_Ifaces
11964 (Iface_Def => Type_Definition (Parent_Node),
11965 Error_Node => Subtype_Indication (Type_Definition (N)));
11966 end if;
11967
11968 Iface := First (Interface_List (Def));
11969 while Present (Iface) loop
11970 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11971
11972 Parent_Node := Parent (Base_Type (Iface_Typ));
11973 Iface_Def := Type_Definition (Parent_Node);
11974
11975 if not Is_Interface (Iface_Typ) then
11976 Diagnose_Interface (Iface, Iface_Typ);
11977
11978 else
11979 -- "The declaration of a specific descendant of an interface
11980 -- type freezes the interface type" RM 13.14
11981
11982 Freeze_Before (N, Iface_Typ);
11983 Check_Ifaces (Iface_Def, Error_Node => Iface);
11984 end if;
11985
11986 Next (Iface);
11987 end loop;
11988
11989 if Is_Task and Is_Protected then
11990 Error_Msg_N
11991 ("type cannot derive from task and protected interface", N);
11992 end if;
11993 end Check_Interfaces;
11994
11995 ------------------------------------
11996 -- Check_Or_Process_Discriminants --
11997 ------------------------------------
11998
11999 -- If an incomplete or private type declaration was already given for the
12000 -- type, the discriminants may have already been processed if they were
12001 -- present on the incomplete declaration. In this case a full conformance
12002 -- check has been performed in Find_Type_Name, and we then recheck here
12003 -- some properties that can't be checked on the partial view alone.
12004 -- Otherwise we call Process_Discriminants.
12005
12006 procedure Check_Or_Process_Discriminants
12007 (N : Node_Id;
12008 T : Entity_Id;
12009 Prev : Entity_Id := Empty)
12010 is
12011 begin
12012 if Has_Discriminants (T) then
12013
12014 -- Discriminants are already set on T if they were already present
12015 -- on the partial view. Make them visible to component declarations.
12016
12017 declare
12018 D : Entity_Id;
12019 -- Discriminant on T (full view) referencing expr on partial view
12020
12021 Prev_D : Entity_Id;
12022 -- Entity of corresponding discriminant on partial view
12023
12024 New_D : Node_Id;
12025 -- Discriminant specification for full view, expression is
12026 -- the syntactic copy on full view (which has been checked for
12027 -- conformance with partial view), only used here to post error
12028 -- message.
12029
12030 begin
12031 D := First_Discriminant (T);
12032 New_D := First (Discriminant_Specifications (N));
12033 while Present (D) loop
12034 Prev_D := Current_Entity (D);
12035 Set_Current_Entity (D);
12036 Set_Is_Immediately_Visible (D);
12037 Set_Homonym (D, Prev_D);
12038
12039 -- Handle the case where there is an untagged partial view and
12040 -- the full view is tagged: must disallow discriminants with
12041 -- defaults, unless compiling for Ada 2012, which allows a
12042 -- limited tagged type to have defaulted discriminants (see
12043 -- AI05-0214). However, suppress error here if it was already
12044 -- reported on the default expression of the partial view.
12045
12046 if Is_Tagged_Type (T)
12047 and then Present (Expression (Parent (D)))
12048 and then (not Is_Limited_Type (Current_Scope)
12049 or else Ada_Version < Ada_2012)
12050 and then not Error_Posted (Expression (Parent (D)))
12051 then
12052 if Ada_Version >= Ada_2012 then
12053 Error_Msg_N
12054 ("discriminants of nonlimited tagged type cannot have "
12055 & "defaults",
12056 Expression (New_D));
12057 else
12058 Error_Msg_N
12059 ("discriminants of tagged type cannot have defaults",
12060 Expression (New_D));
12061 end if;
12062 end if;
12063
12064 -- Ada 2005 (AI-230): Access discriminant allowed in
12065 -- non-limited record types.
12066
12067 if Ada_Version < Ada_2005 then
12068
12069 -- This restriction gets applied to the full type here. It
12070 -- has already been applied earlier to the partial view.
12071
12072 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12073 end if;
12074
12075 Next_Discriminant (D);
12076 Next (New_D);
12077 end loop;
12078 end;
12079
12080 elsif Present (Discriminant_Specifications (N)) then
12081 Process_Discriminants (N, Prev);
12082 end if;
12083 end Check_Or_Process_Discriminants;
12084
12085 ----------------------
12086 -- Check_Real_Bound --
12087 ----------------------
12088
12089 procedure Check_Real_Bound (Bound : Node_Id) is
12090 begin
12091 if not Is_Real_Type (Etype (Bound)) then
12092 Error_Msg_N
12093 ("bound in real type definition must be of real type", Bound);
12094
12095 elsif not Is_OK_Static_Expression (Bound) then
12096 Flag_Non_Static_Expr
12097 ("non-static expression used for real type bound!", Bound);
12098
12099 else
12100 return;
12101 end if;
12102
12103 Rewrite
12104 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12105 Analyze (Bound);
12106 Resolve (Bound, Standard_Float);
12107 end Check_Real_Bound;
12108
12109 ------------------------------
12110 -- Complete_Private_Subtype --
12111 ------------------------------
12112
12113 procedure Complete_Private_Subtype
12114 (Priv : Entity_Id;
12115 Full : Entity_Id;
12116 Full_Base : Entity_Id;
12117 Related_Nod : Node_Id)
12118 is
12119 Save_Next_Entity : Entity_Id;
12120 Save_Homonym : Entity_Id;
12121
12122 begin
12123 -- Set semantic attributes for (implicit) private subtype completion.
12124 -- If the full type has no discriminants, then it is a copy of the
12125 -- full view of the base. Otherwise, it is a subtype of the base with
12126 -- a possible discriminant constraint. Save and restore the original
12127 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12128 -- not corrupt the entity chain.
12129
12130 -- Note that the type of the full view is the same entity as the type
12131 -- of the partial view. In this fashion, the subtype has access to the
12132 -- correct view of the parent.
12133
12134 Save_Next_Entity := Next_Entity (Full);
12135 Save_Homonym := Homonym (Priv);
12136
12137 case Ekind (Full_Base) is
12138 when Class_Wide_Kind
12139 | Private_Kind
12140 | Protected_Kind
12141 | Task_Kind
12142 | E_Record_Subtype
12143 | E_Record_Type
12144 =>
12145 Copy_Node (Priv, Full);
12146
12147 Set_Has_Discriminants
12148 (Full, Has_Discriminants (Full_Base));
12149 Set_Has_Unknown_Discriminants
12150 (Full, Has_Unknown_Discriminants (Full_Base));
12151 Set_First_Entity (Full, First_Entity (Full_Base));
12152 Set_Last_Entity (Full, Last_Entity (Full_Base));
12153
12154 -- If the underlying base type is constrained, we know that the
12155 -- full view of the subtype is constrained as well (the converse
12156 -- is not necessarily true).
12157
12158 if Is_Constrained (Full_Base) then
12159 Set_Is_Constrained (Full);
12160 end if;
12161
12162 when others =>
12163 Copy_Node (Full_Base, Full);
12164
12165 Set_Chars (Full, Chars (Priv));
12166 Conditional_Delay (Full, Priv);
12167 Set_Sloc (Full, Sloc (Priv));
12168 end case;
12169
12170 Set_Next_Entity (Full, Save_Next_Entity);
12171 Set_Homonym (Full, Save_Homonym);
12172 Set_Associated_Node_For_Itype (Full, Related_Nod);
12173
12174 -- Set common attributes for all subtypes: kind, convention, etc.
12175
12176 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12177 Set_Convention (Full, Convention (Full_Base));
12178
12179 -- The Etype of the full view is inconsistent. Gigi needs to see the
12180 -- structural full view, which is what the current scheme gives: the
12181 -- Etype of the full view is the etype of the full base. However, if the
12182 -- full base is a derived type, the full view then looks like a subtype
12183 -- of the parent, not a subtype of the full base. If instead we write:
12184
12185 -- Set_Etype (Full, Full_Base);
12186
12187 -- then we get inconsistencies in the front-end (confusion between
12188 -- views). Several outstanding bugs are related to this ???
12189
12190 Set_Is_First_Subtype (Full, False);
12191 Set_Scope (Full, Scope (Priv));
12192 Set_Size_Info (Full, Full_Base);
12193 Set_RM_Size (Full, RM_Size (Full_Base));
12194 Set_Is_Itype (Full);
12195
12196 -- A subtype of a private-type-without-discriminants, whose full-view
12197 -- has discriminants with default expressions, is not constrained.
12198
12199 if not Has_Discriminants (Priv) then
12200 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12201
12202 if Has_Discriminants (Full_Base) then
12203 Set_Discriminant_Constraint
12204 (Full, Discriminant_Constraint (Full_Base));
12205
12206 -- The partial view may have been indefinite, the full view
12207 -- might not be.
12208
12209 Set_Has_Unknown_Discriminants
12210 (Full, Has_Unknown_Discriminants (Full_Base));
12211 end if;
12212 end if;
12213
12214 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12215 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12216
12217 -- Freeze the private subtype entity if its parent is delayed, and not
12218 -- already frozen. We skip this processing if the type is an anonymous
12219 -- subtype of a record component, or is the corresponding record of a
12220 -- protected type, since these are processed when the enclosing type
12221 -- is frozen. If the parent type is declared in a nested package then
12222 -- the freezing of the private and full views also happens later.
12223
12224 if not Is_Type (Scope (Full)) then
12225 if Is_Itype (Priv)
12226 and then In_Same_Source_Unit (Full, Full_Base)
12227 and then Scope (Full_Base) /= Scope (Full)
12228 then
12229 Set_Has_Delayed_Freeze (Full);
12230 Set_Has_Delayed_Freeze (Priv);
12231
12232 else
12233 Set_Has_Delayed_Freeze (Full,
12234 Has_Delayed_Freeze (Full_Base)
12235 and then not Is_Frozen (Full_Base));
12236 end if;
12237 end if;
12238
12239 Set_Freeze_Node (Full, Empty);
12240 Set_Is_Frozen (Full, False);
12241 Set_Full_View (Priv, Full);
12242
12243 if Has_Discriminants (Full) then
12244 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12245 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12246
12247 if Has_Unknown_Discriminants (Full) then
12248 Set_Discriminant_Constraint (Full, No_Elist);
12249 end if;
12250 end if;
12251
12252 if Ekind (Full_Base) = E_Record_Type
12253 and then Has_Discriminants (Full_Base)
12254 and then Has_Discriminants (Priv) -- might not, if errors
12255 and then not Has_Unknown_Discriminants (Priv)
12256 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12257 then
12258 Create_Constrained_Components
12259 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12260
12261 -- If the full base is itself derived from private, build a congruent
12262 -- subtype of its underlying type, for use by the back end. For a
12263 -- constrained record component, the declaration cannot be placed on
12264 -- the component list, but it must nevertheless be built an analyzed, to
12265 -- supply enough information for Gigi to compute the size of component.
12266
12267 elsif Ekind (Full_Base) in Private_Kind
12268 and then Is_Derived_Type (Full_Base)
12269 and then Has_Discriminants (Full_Base)
12270 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12271 then
12272 if not Is_Itype (Priv)
12273 and then
12274 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12275 then
12276 Build_Underlying_Full_View
12277 (Parent (Priv), Full, Etype (Full_Base));
12278
12279 elsif Nkind (Related_Nod) = N_Component_Declaration then
12280 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12281 end if;
12282
12283 elsif Is_Record_Type (Full_Base) then
12284
12285 -- Show Full is simply a renaming of Full_Base
12286
12287 Set_Cloned_Subtype (Full, Full_Base);
12288 end if;
12289
12290 -- It is unsafe to share the bounds of a scalar type, because the Itype
12291 -- is elaborated on demand, and if a bound is non-static then different
12292 -- orders of elaboration in different units will lead to different
12293 -- external symbols.
12294
12295 if Is_Scalar_Type (Full_Base) then
12296 Set_Scalar_Range (Full,
12297 Make_Range (Sloc (Related_Nod),
12298 Low_Bound =>
12299 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12300 High_Bound =>
12301 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12302
12303 -- This completion inherits the bounds of the full parent, but if
12304 -- the parent is an unconstrained floating point type, so is the
12305 -- completion.
12306
12307 if Is_Floating_Point_Type (Full_Base) then
12308 Set_Includes_Infinities
12309 (Scalar_Range (Full), Has_Infinities (Full_Base));
12310 end if;
12311 end if;
12312
12313 -- ??? It seems that a lot of fields are missing that should be copied
12314 -- from Full_Base to Full. Here are some that are introduced in a
12315 -- non-disruptive way but a cleanup is necessary.
12316
12317 if Is_Tagged_Type (Full_Base) then
12318 Set_Is_Tagged_Type (Full);
12319 Set_Direct_Primitive_Operations
12320 (Full, Direct_Primitive_Operations (Full_Base));
12321 Set_No_Tagged_Streams_Pragma
12322 (Full, No_Tagged_Streams_Pragma (Full_Base));
12323
12324 -- Inherit class_wide type of full_base in case the partial view was
12325 -- not tagged. Otherwise it has already been created when the private
12326 -- subtype was analyzed.
12327
12328 if No (Class_Wide_Type (Full)) then
12329 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12330 end if;
12331
12332 -- If this is a subtype of a protected or task type, constrain its
12333 -- corresponding record, unless this is a subtype without constraints,
12334 -- i.e. a simple renaming as with an actual subtype in an instance.
12335
12336 elsif Is_Concurrent_Type (Full_Base) then
12337 if Has_Discriminants (Full)
12338 and then Present (Corresponding_Record_Type (Full_Base))
12339 and then
12340 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12341 then
12342 Set_Corresponding_Record_Type (Full,
12343 Constrain_Corresponding_Record
12344 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12345
12346 else
12347 Set_Corresponding_Record_Type (Full,
12348 Corresponding_Record_Type (Full_Base));
12349 end if;
12350 end if;
12351
12352 -- Link rep item chain, and also setting of Has_Predicates from private
12353 -- subtype to full subtype, since we will need these on the full subtype
12354 -- to create the predicate function. Note that the full subtype may
12355 -- already have rep items, inherited from the full view of the base
12356 -- type, so we must be sure not to overwrite these entries.
12357
12358 declare
12359 Append : Boolean;
12360 Item : Node_Id;
12361 Next_Item : Node_Id;
12362 Priv_Item : Node_Id;
12363
12364 begin
12365 Item := First_Rep_Item (Full);
12366 Priv_Item := First_Rep_Item (Priv);
12367
12368 -- If no existing rep items on full type, we can just link directly
12369 -- to the list of items on the private type, if any exist.. Same if
12370 -- the rep items are only those inherited from the base
12371
12372 if (No (Item)
12373 or else Nkind (Item) /= N_Aspect_Specification
12374 or else Entity (Item) = Full_Base)
12375 and then Present (First_Rep_Item (Priv))
12376 then
12377 Set_First_Rep_Item (Full, Priv_Item);
12378
12379 -- Otherwise, search to the end of items currently linked to the full
12380 -- subtype and append the private items to the end. However, if Priv
12381 -- and Full already have the same list of rep items, then the append
12382 -- is not done, as that would create a circularity.
12383 --
12384 -- The partial view may have a predicate and the rep item lists of
12385 -- both views agree when inherited from the same ancestor. In that
12386 -- case, simply propagate the list from one view to the other.
12387 -- A more complex analysis needed here ???
12388
12389 elsif Present (Priv_Item)
12390 and then Item = Next_Rep_Item (Priv_Item)
12391 then
12392 Set_First_Rep_Item (Full, Priv_Item);
12393
12394 elsif Item /= Priv_Item then
12395 Append := True;
12396 loop
12397 Next_Item := Next_Rep_Item (Item);
12398 exit when No (Next_Item);
12399 Item := Next_Item;
12400
12401 -- If the private view has aspect specifications, the full view
12402 -- inherits them. Since these aspects may already have been
12403 -- attached to the full view during derivation, do not append
12404 -- them if already present.
12405
12406 if Item = First_Rep_Item (Priv) then
12407 Append := False;
12408 exit;
12409 end if;
12410 end loop;
12411
12412 -- And link the private type items at the end of the chain
12413
12414 if Append then
12415 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12416 end if;
12417 end if;
12418 end;
12419
12420 -- Make sure Has_Predicates is set on full type if it is set on the
12421 -- private type. Note that it may already be set on the full type and
12422 -- if so, we don't want to unset it. Similarly, propagate information
12423 -- about delayed aspects, because the corresponding pragmas must be
12424 -- analyzed when one of the views is frozen. This last step is needed
12425 -- in particular when the full type is a scalar type for which an
12426 -- anonymous base type is constructed.
12427
12428 -- The predicate functions are generated either at the freeze point
12429 -- of the type or at the end of the visible part, and we must avoid
12430 -- generating them twice.
12431
12432 if Has_Predicates (Priv) then
12433 Set_Has_Predicates (Full);
12434
12435 if Present (Predicate_Function (Priv))
12436 and then No (Predicate_Function (Full))
12437 then
12438 Set_Predicate_Function (Full, Predicate_Function (Priv));
12439 end if;
12440 end if;
12441
12442 if Has_Delayed_Aspects (Priv) then
12443 Set_Has_Delayed_Aspects (Full);
12444 end if;
12445 end Complete_Private_Subtype;
12446
12447 ----------------------------
12448 -- Constant_Redeclaration --
12449 ----------------------------
12450
12451 procedure Constant_Redeclaration
12452 (Id : Entity_Id;
12453 N : Node_Id;
12454 T : out Entity_Id)
12455 is
12456 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12457 Obj_Def : constant Node_Id := Object_Definition (N);
12458 New_T : Entity_Id;
12459
12460 procedure Check_Possible_Deferred_Completion
12461 (Prev_Id : Entity_Id;
12462 Prev_Obj_Def : Node_Id;
12463 Curr_Obj_Def : Node_Id);
12464 -- Determine whether the two object definitions describe the partial
12465 -- and the full view of a constrained deferred constant. Generate
12466 -- a subtype for the full view and verify that it statically matches
12467 -- the subtype of the partial view.
12468
12469 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12470 -- If deferred constant is an access type initialized with an allocator,
12471 -- check whether there is an illegal recursion in the definition,
12472 -- through a default value of some record subcomponent. This is normally
12473 -- detected when generating init procs, but requires this additional
12474 -- mechanism when expansion is disabled.
12475
12476 ----------------------------------------
12477 -- Check_Possible_Deferred_Completion --
12478 ----------------------------------------
12479
12480 procedure Check_Possible_Deferred_Completion
12481 (Prev_Id : Entity_Id;
12482 Prev_Obj_Def : Node_Id;
12483 Curr_Obj_Def : Node_Id)
12484 is
12485 begin
12486 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12487 and then Present (Constraint (Prev_Obj_Def))
12488 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12489 and then Present (Constraint (Curr_Obj_Def))
12490 then
12491 declare
12492 Loc : constant Source_Ptr := Sloc (N);
12493 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12494 Decl : constant Node_Id :=
12495 Make_Subtype_Declaration (Loc,
12496 Defining_Identifier => Def_Id,
12497 Subtype_Indication =>
12498 Relocate_Node (Curr_Obj_Def));
12499
12500 begin
12501 Insert_Before_And_Analyze (N, Decl);
12502 Set_Etype (Id, Def_Id);
12503
12504 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12505 Error_Msg_Sloc := Sloc (Prev_Id);
12506 Error_Msg_N ("subtype does not statically match deferred "
12507 & "declaration #", N);
12508 end if;
12509 end;
12510 end if;
12511 end Check_Possible_Deferred_Completion;
12512
12513 ---------------------------------
12514 -- Check_Recursive_Declaration --
12515 ---------------------------------
12516
12517 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12518 Comp : Entity_Id;
12519
12520 begin
12521 if Is_Record_Type (Typ) then
12522 Comp := First_Component (Typ);
12523 while Present (Comp) loop
12524 if Comes_From_Source (Comp) then
12525 if Present (Expression (Parent (Comp)))
12526 and then Is_Entity_Name (Expression (Parent (Comp)))
12527 and then Entity (Expression (Parent (Comp))) = Prev
12528 then
12529 Error_Msg_Sloc := Sloc (Parent (Comp));
12530 Error_Msg_NE
12531 ("illegal circularity with declaration for & #",
12532 N, Comp);
12533 return;
12534
12535 elsif Is_Record_Type (Etype (Comp)) then
12536 Check_Recursive_Declaration (Etype (Comp));
12537 end if;
12538 end if;
12539
12540 Next_Component (Comp);
12541 end loop;
12542 end if;
12543 end Check_Recursive_Declaration;
12544
12545 -- Start of processing for Constant_Redeclaration
12546
12547 begin
12548 if Nkind (Parent (Prev)) = N_Object_Declaration then
12549 if Nkind (Object_Definition
12550 (Parent (Prev))) = N_Subtype_Indication
12551 then
12552 -- Find type of new declaration. The constraints of the two
12553 -- views must match statically, but there is no point in
12554 -- creating an itype for the full view.
12555
12556 if Nkind (Obj_Def) = N_Subtype_Indication then
12557 Find_Type (Subtype_Mark (Obj_Def));
12558 New_T := Entity (Subtype_Mark (Obj_Def));
12559
12560 else
12561 Find_Type (Obj_Def);
12562 New_T := Entity (Obj_Def);
12563 end if;
12564
12565 T := Etype (Prev);
12566
12567 else
12568 -- The full view may impose a constraint, even if the partial
12569 -- view does not, so construct the subtype.
12570
12571 New_T := Find_Type_Of_Object (Obj_Def, N);
12572 T := New_T;
12573 end if;
12574
12575 else
12576 -- Current declaration is illegal, diagnosed below in Enter_Name
12577
12578 T := Empty;
12579 New_T := Any_Type;
12580 end if;
12581
12582 -- If previous full declaration or a renaming declaration exists, or if
12583 -- a homograph is present, let Enter_Name handle it, either with an
12584 -- error or with the removal of an overridden implicit subprogram.
12585 -- The previous one is a full declaration if it has an expression
12586 -- (which in the case of an aggregate is indicated by the Init flag).
12587
12588 if Ekind (Prev) /= E_Constant
12589 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12590 or else Present (Expression (Parent (Prev)))
12591 or else Has_Init_Expression (Parent (Prev))
12592 or else Present (Full_View (Prev))
12593 then
12594 Enter_Name (Id);
12595
12596 -- Verify that types of both declarations match, or else that both types
12597 -- are anonymous access types whose designated subtypes statically match
12598 -- (as allowed in Ada 2005 by AI-385).
12599
12600 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12601 and then
12602 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12603 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12604 or else Is_Access_Constant (Etype (New_T)) /=
12605 Is_Access_Constant (Etype (Prev))
12606 or else Can_Never_Be_Null (Etype (New_T)) /=
12607 Can_Never_Be_Null (Etype (Prev))
12608 or else Null_Exclusion_Present (Parent (Prev)) /=
12609 Null_Exclusion_Present (Parent (Id))
12610 or else not Subtypes_Statically_Match
12611 (Designated_Type (Etype (Prev)),
12612 Designated_Type (Etype (New_T))))
12613 then
12614 Error_Msg_Sloc := Sloc (Prev);
12615 Error_Msg_N ("type does not match declaration#", N);
12616 Set_Full_View (Prev, Id);
12617 Set_Etype (Id, Any_Type);
12618
12619 -- A deferred constant whose type is an anonymous array is always
12620 -- illegal (unless imported). A detailed error message might be
12621 -- helpful for Ada beginners.
12622
12623 if Nkind (Object_Definition (Parent (Prev)))
12624 = N_Constrained_Array_Definition
12625 and then Nkind (Object_Definition (N))
12626 = N_Constrained_Array_Definition
12627 then
12628 Error_Msg_N ("\each anonymous array is a distinct type", N);
12629 Error_Msg_N ("a deferred constant must have a named type",
12630 Object_Definition (Parent (Prev)));
12631 end if;
12632
12633 elsif
12634 Null_Exclusion_Present (Parent (Prev))
12635 and then not Null_Exclusion_Present (N)
12636 then
12637 Error_Msg_Sloc := Sloc (Prev);
12638 Error_Msg_N ("null-exclusion does not match declaration#", N);
12639 Set_Full_View (Prev, Id);
12640 Set_Etype (Id, Any_Type);
12641
12642 -- If so, process the full constant declaration
12643
12644 else
12645 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12646 -- the deferred declaration is constrained, then the subtype defined
12647 -- by the subtype_indication in the full declaration shall match it
12648 -- statically.
12649
12650 Check_Possible_Deferred_Completion
12651 (Prev_Id => Prev,
12652 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12653 Curr_Obj_Def => Obj_Def);
12654
12655 Set_Full_View (Prev, Id);
12656 Set_Is_Public (Id, Is_Public (Prev));
12657 Set_Is_Internal (Id);
12658 Append_Entity (Id, Current_Scope);
12659
12660 -- Check ALIASED present if present before (RM 7.4(7))
12661
12662 if Is_Aliased (Prev)
12663 and then not Aliased_Present (N)
12664 then
12665 Error_Msg_Sloc := Sloc (Prev);
12666 Error_Msg_N ("ALIASED required (see declaration #)", N);
12667 end if;
12668
12669 -- Check that placement is in private part and that the incomplete
12670 -- declaration appeared in the visible part.
12671
12672 if Ekind (Current_Scope) = E_Package
12673 and then not In_Private_Part (Current_Scope)
12674 then
12675 Error_Msg_Sloc := Sloc (Prev);
12676 Error_Msg_N
12677 ("full constant for declaration # must be in private part", N);
12678
12679 elsif Ekind (Current_Scope) = E_Package
12680 and then
12681 List_Containing (Parent (Prev)) /=
12682 Visible_Declarations (Package_Specification (Current_Scope))
12683 then
12684 Error_Msg_N
12685 ("deferred constant must be declared in visible part",
12686 Parent (Prev));
12687 end if;
12688
12689 if Is_Access_Type (T)
12690 and then Nkind (Expression (N)) = N_Allocator
12691 then
12692 Check_Recursive_Declaration (Designated_Type (T));
12693 end if;
12694
12695 -- A deferred constant is a visible entity. If type has invariants,
12696 -- verify that the initial value satisfies them.
12697
12698 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12699 Insert_After (N,
12700 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12701 end if;
12702 end if;
12703 end Constant_Redeclaration;
12704
12705 ----------------------
12706 -- Constrain_Access --
12707 ----------------------
12708
12709 procedure Constrain_Access
12710 (Def_Id : in out Entity_Id;
12711 S : Node_Id;
12712 Related_Nod : Node_Id)
12713 is
12714 T : constant Entity_Id := Entity (Subtype_Mark (S));
12715 Desig_Type : constant Entity_Id := Designated_Type (T);
12716 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12717 Constraint_OK : Boolean := True;
12718
12719 begin
12720 if Is_Array_Type (Desig_Type) then
12721 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12722
12723 elsif (Is_Record_Type (Desig_Type)
12724 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12725 and then not Is_Constrained (Desig_Type)
12726 then
12727 -- ??? The following code is a temporary bypass to ignore a
12728 -- discriminant constraint on access type if it is constraining
12729 -- the current record. Avoid creating the implicit subtype of the
12730 -- record we are currently compiling since right now, we cannot
12731 -- handle these. For now, just return the access type itself.
12732
12733 if Desig_Type = Current_Scope
12734 and then No (Def_Id)
12735 then
12736 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12737 Def_Id := Entity (Subtype_Mark (S));
12738
12739 -- This call added to ensure that the constraint is analyzed
12740 -- (needed for a B test). Note that we still return early from
12741 -- this procedure to avoid recursive processing. ???
12742
12743 Constrain_Discriminated_Type
12744 (Desig_Subtype, S, Related_Nod, For_Access => True);
12745 return;
12746 end if;
12747
12748 -- Enforce rule that the constraint is illegal if there is an
12749 -- unconstrained view of the designated type. This means that the
12750 -- partial view (either a private type declaration or a derivation
12751 -- from a private type) has no discriminants. (Defect Report
12752 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12753
12754 -- Rule updated for Ada 2005: The private type is said to have
12755 -- a constrained partial view, given that objects of the type
12756 -- can be declared. Furthermore, the rule applies to all access
12757 -- types, unlike the rule concerning default discriminants (see
12758 -- RM 3.7.1(7/3))
12759
12760 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12761 and then Has_Private_Declaration (Desig_Type)
12762 and then In_Open_Scopes (Scope (Desig_Type))
12763 and then Has_Discriminants (Desig_Type)
12764 then
12765 declare
12766 Pack : constant Node_Id :=
12767 Unit_Declaration_Node (Scope (Desig_Type));
12768 Decls : List_Id;
12769 Decl : Node_Id;
12770
12771 begin
12772 if Nkind (Pack) = N_Package_Declaration then
12773 Decls := Visible_Declarations (Specification (Pack));
12774 Decl := First (Decls);
12775 while Present (Decl) loop
12776 if (Nkind (Decl) = N_Private_Type_Declaration
12777 and then Chars (Defining_Identifier (Decl)) =
12778 Chars (Desig_Type))
12779
12780 or else
12781 (Nkind (Decl) = N_Full_Type_Declaration
12782 and then
12783 Chars (Defining_Identifier (Decl)) =
12784 Chars (Desig_Type)
12785 and then Is_Derived_Type (Desig_Type)
12786 and then
12787 Has_Private_Declaration (Etype (Desig_Type)))
12788 then
12789 if No (Discriminant_Specifications (Decl)) then
12790 Error_Msg_N
12791 ("cannot constrain access type if designated "
12792 & "type has constrained partial view", S);
12793 end if;
12794
12795 exit;
12796 end if;
12797
12798 Next (Decl);
12799 end loop;
12800 end if;
12801 end;
12802 end if;
12803
12804 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12805 For_Access => True);
12806
12807 elsif Is_Concurrent_Type (Desig_Type)
12808 and then not Is_Constrained (Desig_Type)
12809 then
12810 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12811
12812 else
12813 Error_Msg_N ("invalid constraint on access type", S);
12814
12815 -- We simply ignore an invalid constraint
12816
12817 Desig_Subtype := Desig_Type;
12818 Constraint_OK := False;
12819 end if;
12820
12821 if No (Def_Id) then
12822 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12823 else
12824 Set_Ekind (Def_Id, E_Access_Subtype);
12825 end if;
12826
12827 if Constraint_OK then
12828 Set_Etype (Def_Id, Base_Type (T));
12829
12830 if Is_Private_Type (Desig_Type) then
12831 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12832 end if;
12833 else
12834 Set_Etype (Def_Id, Any_Type);
12835 end if;
12836
12837 Set_Size_Info (Def_Id, T);
12838 Set_Is_Constrained (Def_Id, Constraint_OK);
12839 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12840 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12841 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12842
12843 Conditional_Delay (Def_Id, T);
12844
12845 -- AI-363 : Subtypes of general access types whose designated types have
12846 -- default discriminants are disallowed. In instances, the rule has to
12847 -- be checked against the actual, of which T is the subtype. In a
12848 -- generic body, the rule is checked assuming that the actual type has
12849 -- defaulted discriminants.
12850
12851 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12852 if Ekind (Base_Type (T)) = E_General_Access_Type
12853 and then Has_Defaulted_Discriminants (Desig_Type)
12854 then
12855 if Ada_Version < Ada_2005 then
12856 Error_Msg_N
12857 ("access subtype of general access type would not " &
12858 "be allowed in Ada 2005?y?", S);
12859 else
12860 Error_Msg_N
12861 ("access subtype of general access type not allowed", S);
12862 end if;
12863
12864 Error_Msg_N ("\discriminants have defaults", S);
12865
12866 elsif Is_Access_Type (T)
12867 and then Is_Generic_Type (Desig_Type)
12868 and then Has_Discriminants (Desig_Type)
12869 and then In_Package_Body (Current_Scope)
12870 then
12871 if Ada_Version < Ada_2005 then
12872 Error_Msg_N
12873 ("access subtype would not be allowed in generic body "
12874 & "in Ada 2005?y?", S);
12875 else
12876 Error_Msg_N
12877 ("access subtype not allowed in generic body", S);
12878 end if;
12879
12880 Error_Msg_N
12881 ("\designated type is a discriminated formal", S);
12882 end if;
12883 end if;
12884 end Constrain_Access;
12885
12886 ---------------------
12887 -- Constrain_Array --
12888 ---------------------
12889
12890 procedure Constrain_Array
12891 (Def_Id : in out Entity_Id;
12892 SI : Node_Id;
12893 Related_Nod : Node_Id;
12894 Related_Id : Entity_Id;
12895 Suffix : Character)
12896 is
12897 C : constant Node_Id := Constraint (SI);
12898 Number_Of_Constraints : Nat := 0;
12899 Index : Node_Id;
12900 S, T : Entity_Id;
12901 Constraint_OK : Boolean := True;
12902
12903 begin
12904 T := Entity (Subtype_Mark (SI));
12905
12906 if Is_Access_Type (T) then
12907 T := Designated_Type (T);
12908 end if;
12909
12910 -- If an index constraint follows a subtype mark in a subtype indication
12911 -- then the type or subtype denoted by the subtype mark must not already
12912 -- impose an index constraint. The subtype mark must denote either an
12913 -- unconstrained array type or an access type whose designated type
12914 -- is such an array type... (RM 3.6.1)
12915
12916 if Is_Constrained (T) then
12917 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12918 Constraint_OK := False;
12919
12920 else
12921 S := First (Constraints (C));
12922 while Present (S) loop
12923 Number_Of_Constraints := Number_Of_Constraints + 1;
12924 Next (S);
12925 end loop;
12926
12927 -- In either case, the index constraint must provide a discrete
12928 -- range for each index of the array type and the type of each
12929 -- discrete range must be the same as that of the corresponding
12930 -- index. (RM 3.6.1)
12931
12932 if Number_Of_Constraints /= Number_Dimensions (T) then
12933 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12934 Constraint_OK := False;
12935
12936 else
12937 S := First (Constraints (C));
12938 Index := First_Index (T);
12939 Analyze (Index);
12940
12941 -- Apply constraints to each index type
12942
12943 for J in 1 .. Number_Of_Constraints loop
12944 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12945 Next (Index);
12946 Next (S);
12947 end loop;
12948
12949 end if;
12950 end if;
12951
12952 if No (Def_Id) then
12953 Def_Id :=
12954 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12955 Set_Parent (Def_Id, Related_Nod);
12956
12957 else
12958 Set_Ekind (Def_Id, E_Array_Subtype);
12959 end if;
12960
12961 Set_Size_Info (Def_Id, (T));
12962 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12963 Set_Etype (Def_Id, Base_Type (T));
12964
12965 if Constraint_OK then
12966 Set_First_Index (Def_Id, First (Constraints (C)));
12967 else
12968 Set_First_Index (Def_Id, First_Index (T));
12969 end if;
12970
12971 Set_Is_Constrained (Def_Id, True);
12972 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12973 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12974
12975 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12976 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12977
12978 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12979 -- We need to initialize the attribute because if Def_Id is previously
12980 -- analyzed through a limited_with clause, it will have the attributes
12981 -- of an incomplete type, one of which is an Elist that overlaps the
12982 -- Packed_Array_Impl_Type field.
12983
12984 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12985
12986 -- Build a freeze node if parent still needs one. Also make sure that
12987 -- the Depends_On_Private status is set because the subtype will need
12988 -- reprocessing at the time the base type does, and also we must set a
12989 -- conditional delay.
12990
12991 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12992 Conditional_Delay (Def_Id, T);
12993 end Constrain_Array;
12994
12995 ------------------------------
12996 -- Constrain_Component_Type --
12997 ------------------------------
12998
12999 function Constrain_Component_Type
13000 (Comp : Entity_Id;
13001 Constrained_Typ : Entity_Id;
13002 Related_Node : Node_Id;
13003 Typ : Entity_Id;
13004 Constraints : Elist_Id) return Entity_Id
13005 is
13006 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13007 Compon_Type : constant Entity_Id := Etype (Comp);
13008
13009 function Build_Constrained_Array_Type
13010 (Old_Type : Entity_Id) return Entity_Id;
13011 -- If Old_Type is an array type, one of whose indexes is constrained
13012 -- by a discriminant, build an Itype whose constraint replaces the
13013 -- discriminant with its value in the constraint.
13014
13015 function Build_Constrained_Discriminated_Type
13016 (Old_Type : Entity_Id) return Entity_Id;
13017 -- Ditto for record components
13018
13019 function Build_Constrained_Access_Type
13020 (Old_Type : Entity_Id) return Entity_Id;
13021 -- Ditto for access types. Makes use of previous two functions, to
13022 -- constrain designated type.
13023
13024 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13025 -- T is an array or discriminated type, C is a list of constraints
13026 -- that apply to T. This routine builds the constrained subtype.
13027
13028 function Is_Discriminant (Expr : Node_Id) return Boolean;
13029 -- Returns True if Expr is a discriminant
13030
13031 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13032 -- Find the value of discriminant Discrim in Constraint
13033
13034 -----------------------------------
13035 -- Build_Constrained_Access_Type --
13036 -----------------------------------
13037
13038 function Build_Constrained_Access_Type
13039 (Old_Type : Entity_Id) return Entity_Id
13040 is
13041 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13042 Itype : Entity_Id;
13043 Desig_Subtype : Entity_Id;
13044 Scop : Entity_Id;
13045
13046 begin
13047 -- if the original access type was not embedded in the enclosing
13048 -- type definition, there is no need to produce a new access
13049 -- subtype. In fact every access type with an explicit constraint
13050 -- generates an itype whose scope is the enclosing record.
13051
13052 if not Is_Type (Scope (Old_Type)) then
13053 return Old_Type;
13054
13055 elsif Is_Array_Type (Desig_Type) then
13056 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13057
13058 elsif Has_Discriminants (Desig_Type) then
13059
13060 -- This may be an access type to an enclosing record type for
13061 -- which we are constructing the constrained components. Return
13062 -- the enclosing record subtype. This is not always correct,
13063 -- but avoids infinite recursion. ???
13064
13065 Desig_Subtype := Any_Type;
13066
13067 for J in reverse 0 .. Scope_Stack.Last loop
13068 Scop := Scope_Stack.Table (J).Entity;
13069
13070 if Is_Type (Scop)
13071 and then Base_Type (Scop) = Base_Type (Desig_Type)
13072 then
13073 Desig_Subtype := Scop;
13074 end if;
13075
13076 exit when not Is_Type (Scop);
13077 end loop;
13078
13079 if Desig_Subtype = Any_Type then
13080 Desig_Subtype :=
13081 Build_Constrained_Discriminated_Type (Desig_Type);
13082 end if;
13083
13084 else
13085 return Old_Type;
13086 end if;
13087
13088 if Desig_Subtype /= Desig_Type then
13089
13090 -- The Related_Node better be here or else we won't be able
13091 -- to attach new itypes to a node in the tree.
13092
13093 pragma Assert (Present (Related_Node));
13094
13095 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13096
13097 Set_Etype (Itype, Base_Type (Old_Type));
13098 Set_Size_Info (Itype, (Old_Type));
13099 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13100 Set_Depends_On_Private (Itype, Has_Private_Component
13101 (Old_Type));
13102 Set_Is_Access_Constant (Itype, Is_Access_Constant
13103 (Old_Type));
13104
13105 -- The new itype needs freezing when it depends on a not frozen
13106 -- type and the enclosing subtype needs freezing.
13107
13108 if Has_Delayed_Freeze (Constrained_Typ)
13109 and then not Is_Frozen (Constrained_Typ)
13110 then
13111 Conditional_Delay (Itype, Base_Type (Old_Type));
13112 end if;
13113
13114 return Itype;
13115
13116 else
13117 return Old_Type;
13118 end if;
13119 end Build_Constrained_Access_Type;
13120
13121 ----------------------------------
13122 -- Build_Constrained_Array_Type --
13123 ----------------------------------
13124
13125 function Build_Constrained_Array_Type
13126 (Old_Type : Entity_Id) return Entity_Id
13127 is
13128 Lo_Expr : Node_Id;
13129 Hi_Expr : Node_Id;
13130 Old_Index : Node_Id;
13131 Range_Node : Node_Id;
13132 Constr_List : List_Id;
13133
13134 Need_To_Create_Itype : Boolean := False;
13135
13136 begin
13137 Old_Index := First_Index (Old_Type);
13138 while Present (Old_Index) loop
13139 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13140
13141 if Is_Discriminant (Lo_Expr)
13142 or else
13143 Is_Discriminant (Hi_Expr)
13144 then
13145 Need_To_Create_Itype := True;
13146 end if;
13147
13148 Next_Index (Old_Index);
13149 end loop;
13150
13151 if Need_To_Create_Itype then
13152 Constr_List := New_List;
13153
13154 Old_Index := First_Index (Old_Type);
13155 while Present (Old_Index) loop
13156 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13157
13158 if Is_Discriminant (Lo_Expr) then
13159 Lo_Expr := Get_Discr_Value (Lo_Expr);
13160 end if;
13161
13162 if Is_Discriminant (Hi_Expr) then
13163 Hi_Expr := Get_Discr_Value (Hi_Expr);
13164 end if;
13165
13166 Range_Node :=
13167 Make_Range
13168 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13169
13170 Append (Range_Node, To => Constr_List);
13171
13172 Next_Index (Old_Index);
13173 end loop;
13174
13175 return Build_Subtype (Old_Type, Constr_List);
13176
13177 else
13178 return Old_Type;
13179 end if;
13180 end Build_Constrained_Array_Type;
13181
13182 ------------------------------------------
13183 -- Build_Constrained_Discriminated_Type --
13184 ------------------------------------------
13185
13186 function Build_Constrained_Discriminated_Type
13187 (Old_Type : Entity_Id) return Entity_Id
13188 is
13189 Expr : Node_Id;
13190 Constr_List : List_Id;
13191 Old_Constraint : Elmt_Id;
13192
13193 Need_To_Create_Itype : Boolean := False;
13194
13195 begin
13196 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13197 while Present (Old_Constraint) loop
13198 Expr := Node (Old_Constraint);
13199
13200 if Is_Discriminant (Expr) then
13201 Need_To_Create_Itype := True;
13202 end if;
13203
13204 Next_Elmt (Old_Constraint);
13205 end loop;
13206
13207 if Need_To_Create_Itype then
13208 Constr_List := New_List;
13209
13210 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13211 while Present (Old_Constraint) loop
13212 Expr := Node (Old_Constraint);
13213
13214 if Is_Discriminant (Expr) then
13215 Expr := Get_Discr_Value (Expr);
13216 end if;
13217
13218 Append (New_Copy_Tree (Expr), To => Constr_List);
13219
13220 Next_Elmt (Old_Constraint);
13221 end loop;
13222
13223 return Build_Subtype (Old_Type, Constr_List);
13224
13225 else
13226 return Old_Type;
13227 end if;
13228 end Build_Constrained_Discriminated_Type;
13229
13230 -------------------
13231 -- Build_Subtype --
13232 -------------------
13233
13234 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13235 Indic : Node_Id;
13236 Subtyp_Decl : Node_Id;
13237 Def_Id : Entity_Id;
13238 Btyp : Entity_Id := Base_Type (T);
13239
13240 begin
13241 -- The Related_Node better be here or else we won't be able to
13242 -- attach new itypes to a node in the tree.
13243
13244 pragma Assert (Present (Related_Node));
13245
13246 -- If the view of the component's type is incomplete or private
13247 -- with unknown discriminants, then the constraint must be applied
13248 -- to the full type.
13249
13250 if Has_Unknown_Discriminants (Btyp)
13251 and then Present (Underlying_Type (Btyp))
13252 then
13253 Btyp := Underlying_Type (Btyp);
13254 end if;
13255
13256 Indic :=
13257 Make_Subtype_Indication (Loc,
13258 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13259 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13260
13261 Def_Id := Create_Itype (Ekind (T), Related_Node);
13262
13263 Subtyp_Decl :=
13264 Make_Subtype_Declaration (Loc,
13265 Defining_Identifier => Def_Id,
13266 Subtype_Indication => Indic);
13267
13268 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13269
13270 -- Itypes must be analyzed with checks off (see package Itypes)
13271
13272 Analyze (Subtyp_Decl, Suppress => All_Checks);
13273
13274 return Def_Id;
13275 end Build_Subtype;
13276
13277 ---------------------
13278 -- Get_Discr_Value --
13279 ---------------------
13280
13281 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13282 D : Entity_Id;
13283 E : Elmt_Id;
13284
13285 begin
13286 -- The discriminant may be declared for the type, in which case we
13287 -- find it by iterating over the list of discriminants. If the
13288 -- discriminant is inherited from a parent type, it appears as the
13289 -- corresponding discriminant of the current type. This will be the
13290 -- case when constraining an inherited component whose constraint is
13291 -- given by a discriminant of the parent.
13292
13293 D := First_Discriminant (Typ);
13294 E := First_Elmt (Constraints);
13295
13296 while Present (D) loop
13297 if D = Entity (Discrim)
13298 or else D = CR_Discriminant (Entity (Discrim))
13299 or else Corresponding_Discriminant (D) = Entity (Discrim)
13300 then
13301 return Node (E);
13302 end if;
13303
13304 Next_Discriminant (D);
13305 Next_Elmt (E);
13306 end loop;
13307
13308 -- The Corresponding_Discriminant mechanism is incomplete, because
13309 -- the correspondence between new and old discriminants is not one
13310 -- to one: one new discriminant can constrain several old ones. In
13311 -- that case, scan sequentially the stored_constraint, the list of
13312 -- discriminants of the parents, and the constraints.
13313
13314 -- Previous code checked for the present of the Stored_Constraint
13315 -- list for the derived type, but did not use it at all. Should it
13316 -- be present when the component is a discriminated task type?
13317
13318 if Is_Derived_Type (Typ)
13319 and then Scope (Entity (Discrim)) = Etype (Typ)
13320 then
13321 D := First_Discriminant (Etype (Typ));
13322 E := First_Elmt (Constraints);
13323 while Present (D) loop
13324 if D = Entity (Discrim) then
13325 return Node (E);
13326 end if;
13327
13328 Next_Discriminant (D);
13329 Next_Elmt (E);
13330 end loop;
13331 end if;
13332
13333 -- Something is wrong if we did not find the value
13334
13335 raise Program_Error;
13336 end Get_Discr_Value;
13337
13338 ---------------------
13339 -- Is_Discriminant --
13340 ---------------------
13341
13342 function Is_Discriminant (Expr : Node_Id) return Boolean is
13343 Discrim_Scope : Entity_Id;
13344
13345 begin
13346 if Denotes_Discriminant (Expr) then
13347 Discrim_Scope := Scope (Entity (Expr));
13348
13349 -- Either we have a reference to one of Typ's discriminants,
13350
13351 pragma Assert (Discrim_Scope = Typ
13352
13353 -- or to the discriminants of the parent type, in the case
13354 -- of a derivation of a tagged type with variants.
13355
13356 or else Discrim_Scope = Etype (Typ)
13357 or else Full_View (Discrim_Scope) = Etype (Typ)
13358
13359 -- or same as above for the case where the discriminants
13360 -- were declared in Typ's private view.
13361
13362 or else (Is_Private_Type (Discrim_Scope)
13363 and then Chars (Discrim_Scope) = Chars (Typ))
13364
13365 -- or else we are deriving from the full view and the
13366 -- discriminant is declared in the private entity.
13367
13368 or else (Is_Private_Type (Typ)
13369 and then Chars (Discrim_Scope) = Chars (Typ))
13370
13371 -- Or we are constrained the corresponding record of a
13372 -- synchronized type that completes a private declaration.
13373
13374 or else (Is_Concurrent_Record_Type (Typ)
13375 and then
13376 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13377
13378 -- or we have a class-wide type, in which case make sure the
13379 -- discriminant found belongs to the root type.
13380
13381 or else (Is_Class_Wide_Type (Typ)
13382 and then Etype (Typ) = Discrim_Scope));
13383
13384 return True;
13385 end if;
13386
13387 -- In all other cases we have something wrong
13388
13389 return False;
13390 end Is_Discriminant;
13391
13392 -- Start of processing for Constrain_Component_Type
13393
13394 begin
13395 if Nkind (Parent (Comp)) = N_Component_Declaration
13396 and then Comes_From_Source (Parent (Comp))
13397 and then Comes_From_Source
13398 (Subtype_Indication (Component_Definition (Parent (Comp))))
13399 and then
13400 Is_Entity_Name
13401 (Subtype_Indication (Component_Definition (Parent (Comp))))
13402 then
13403 return Compon_Type;
13404
13405 elsif Is_Array_Type (Compon_Type) then
13406 return Build_Constrained_Array_Type (Compon_Type);
13407
13408 elsif Has_Discriminants (Compon_Type) then
13409 return Build_Constrained_Discriminated_Type (Compon_Type);
13410
13411 elsif Is_Access_Type (Compon_Type) then
13412 return Build_Constrained_Access_Type (Compon_Type);
13413
13414 else
13415 return Compon_Type;
13416 end if;
13417 end Constrain_Component_Type;
13418
13419 --------------------------
13420 -- Constrain_Concurrent --
13421 --------------------------
13422
13423 -- For concurrent types, the associated record value type carries the same
13424 -- discriminants, so when we constrain a concurrent type, we must constrain
13425 -- the corresponding record type as well.
13426
13427 procedure Constrain_Concurrent
13428 (Def_Id : in out Entity_Id;
13429 SI : Node_Id;
13430 Related_Nod : Node_Id;
13431 Related_Id : Entity_Id;
13432 Suffix : Character)
13433 is
13434 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13435 -- case of a private subtype (needed when only doing semantic analysis).
13436
13437 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13438 T_Val : Entity_Id;
13439
13440 begin
13441 if Is_Access_Type (T_Ent) then
13442 T_Ent := Designated_Type (T_Ent);
13443 end if;
13444
13445 T_Val := Corresponding_Record_Type (T_Ent);
13446
13447 if Present (T_Val) then
13448
13449 if No (Def_Id) then
13450 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13451
13452 -- Elaborate itype now, as it may be used in a subsequent
13453 -- synchronized operation in another scope.
13454
13455 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13456 Build_Itype_Reference (Def_Id, Related_Nod);
13457 end if;
13458 end if;
13459
13460 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13461
13462 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13463 Set_Corresponding_Record_Type (Def_Id,
13464 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13465
13466 else
13467 -- If there is no associated record, expansion is disabled and this
13468 -- is a generic context. Create a subtype in any case, so that
13469 -- semantic analysis can proceed.
13470
13471 if No (Def_Id) then
13472 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13473 end if;
13474
13475 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13476 end if;
13477 end Constrain_Concurrent;
13478
13479 ------------------------------------
13480 -- Constrain_Corresponding_Record --
13481 ------------------------------------
13482
13483 function Constrain_Corresponding_Record
13484 (Prot_Subt : Entity_Id;
13485 Corr_Rec : Entity_Id;
13486 Related_Nod : Node_Id) return Entity_Id
13487 is
13488 T_Sub : constant Entity_Id :=
13489 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13490
13491 begin
13492 Set_Etype (T_Sub, Corr_Rec);
13493 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13494 Set_Is_Constrained (T_Sub, True);
13495 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13496 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13497
13498 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13499 Set_Discriminant_Constraint
13500 (T_Sub, Discriminant_Constraint (Prot_Subt));
13501 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13502 Create_Constrained_Components
13503 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13504 end if;
13505
13506 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13507
13508 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13509 Conditional_Delay (T_Sub, Corr_Rec);
13510
13511 else
13512 -- This is a component subtype: it will be frozen in the context of
13513 -- the enclosing record's init_proc, so that discriminant references
13514 -- are resolved to discriminals. (Note: we used to skip freezing
13515 -- altogether in that case, which caused errors downstream for
13516 -- components of a bit packed array type).
13517
13518 Set_Has_Delayed_Freeze (T_Sub);
13519 end if;
13520
13521 return T_Sub;
13522 end Constrain_Corresponding_Record;
13523
13524 -----------------------
13525 -- Constrain_Decimal --
13526 -----------------------
13527
13528 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13529 T : constant Entity_Id := Entity (Subtype_Mark (S));
13530 C : constant Node_Id := Constraint (S);
13531 Loc : constant Source_Ptr := Sloc (C);
13532 Range_Expr : Node_Id;
13533 Digits_Expr : Node_Id;
13534 Digits_Val : Uint;
13535 Bound_Val : Ureal;
13536
13537 begin
13538 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13539
13540 if Nkind (C) = N_Range_Constraint then
13541 Range_Expr := Range_Expression (C);
13542 Digits_Val := Digits_Value (T);
13543
13544 else
13545 pragma Assert (Nkind (C) = N_Digits_Constraint);
13546
13547 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13548
13549 Digits_Expr := Digits_Expression (C);
13550 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13551
13552 Check_Digits_Expression (Digits_Expr);
13553 Digits_Val := Expr_Value (Digits_Expr);
13554
13555 if Digits_Val > Digits_Value (T) then
13556 Error_Msg_N
13557 ("digits expression is incompatible with subtype", C);
13558 Digits_Val := Digits_Value (T);
13559 end if;
13560
13561 if Present (Range_Constraint (C)) then
13562 Range_Expr := Range_Expression (Range_Constraint (C));
13563 else
13564 Range_Expr := Empty;
13565 end if;
13566 end if;
13567
13568 Set_Etype (Def_Id, Base_Type (T));
13569 Set_Size_Info (Def_Id, (T));
13570 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13571 Set_Delta_Value (Def_Id, Delta_Value (T));
13572 Set_Scale_Value (Def_Id, Scale_Value (T));
13573 Set_Small_Value (Def_Id, Small_Value (T));
13574 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13575 Set_Digits_Value (Def_Id, Digits_Val);
13576
13577 -- Manufacture range from given digits value if no range present
13578
13579 if No (Range_Expr) then
13580 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13581 Range_Expr :=
13582 Make_Range (Loc,
13583 Low_Bound =>
13584 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13585 High_Bound =>
13586 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13587 end if;
13588
13589 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13590 Set_Discrete_RM_Size (Def_Id);
13591
13592 -- Unconditionally delay the freeze, since we cannot set size
13593 -- information in all cases correctly until the freeze point.
13594
13595 Set_Has_Delayed_Freeze (Def_Id);
13596 end Constrain_Decimal;
13597
13598 ----------------------------------
13599 -- Constrain_Discriminated_Type --
13600 ----------------------------------
13601
13602 procedure Constrain_Discriminated_Type
13603 (Def_Id : Entity_Id;
13604 S : Node_Id;
13605 Related_Nod : Node_Id;
13606 For_Access : Boolean := False)
13607 is
13608 E : Entity_Id := Entity (Subtype_Mark (S));
13609 T : Entity_Id;
13610
13611 procedure Fixup_Bad_Constraint;
13612 -- Called after finding a bad constraint, and after having posted an
13613 -- appropriate error message. The goal is to leave type Def_Id in as
13614 -- reasonable state as possible.
13615
13616 --------------------------
13617 -- Fixup_Bad_Constraint --
13618 --------------------------
13619
13620 procedure Fixup_Bad_Constraint is
13621 begin
13622 -- Set a reasonable Ekind for the entity. For an incomplete type,
13623 -- we can't do much, but for other types, we can set the proper
13624 -- corresponding subtype kind.
13625
13626 if Ekind (T) = E_Incomplete_Type then
13627 Set_Ekind (Def_Id, Ekind (T));
13628 else
13629 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13630 end if;
13631
13632 -- Set Etype to the known type, to reduce chances of cascaded errors
13633
13634 Set_Etype (Def_Id, E);
13635 Set_Error_Posted (Def_Id);
13636 end Fixup_Bad_Constraint;
13637
13638 -- Local variables
13639
13640 C : Node_Id;
13641 Constr : Elist_Id := New_Elmt_List;
13642
13643 -- Start of processing for Constrain_Discriminated_Type
13644
13645 begin
13646 C := Constraint (S);
13647
13648 -- A discriminant constraint is only allowed in a subtype indication,
13649 -- after a subtype mark. This subtype mark must denote either a type
13650 -- with discriminants, or an access type whose designated type is a
13651 -- type with discriminants. A discriminant constraint specifies the
13652 -- values of these discriminants (RM 3.7.2(5)).
13653
13654 T := Base_Type (Entity (Subtype_Mark (S)));
13655
13656 if Is_Access_Type (T) then
13657 T := Designated_Type (T);
13658 end if;
13659
13660 -- In an instance it may be necessary to retrieve the full view of a
13661 -- type with unknown discriminants, or a full view with defaulted
13662 -- discriminants. In other contexts the constraint is illegal.
13663
13664 if In_Instance
13665 and then Is_Private_Type (T)
13666 and then Present (Full_View (T))
13667 and then
13668 (Has_Unknown_Discriminants (T)
13669 or else
13670 (not Has_Discriminants (T)
13671 and then Has_Discriminants (Full_View (T))
13672 and then Present (Discriminant_Default_Value
13673 (First_Discriminant (Full_View (T))))))
13674 then
13675 T := Full_View (T);
13676 E := Full_View (E);
13677 end if;
13678
13679 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13680 -- generating an error for access-to-incomplete subtypes.
13681
13682 if Ada_Version >= Ada_2005
13683 and then Ekind (T) = E_Incomplete_Type
13684 and then Nkind (Parent (S)) = N_Subtype_Declaration
13685 and then not Is_Itype (Def_Id)
13686 then
13687 -- A little sanity check: emit an error message if the type has
13688 -- discriminants to begin with. Type T may be a regular incomplete
13689 -- type or imported via a limited with clause.
13690
13691 if Has_Discriminants (T)
13692 or else (From_Limited_With (T)
13693 and then Present (Non_Limited_View (T))
13694 and then Nkind (Parent (Non_Limited_View (T))) =
13695 N_Full_Type_Declaration
13696 and then Present (Discriminant_Specifications
13697 (Parent (Non_Limited_View (T)))))
13698 then
13699 Error_Msg_N
13700 ("(Ada 2005) incomplete subtype may not be constrained", C);
13701 else
13702 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13703 end if;
13704
13705 Fixup_Bad_Constraint;
13706 return;
13707
13708 -- Check that the type has visible discriminants. The type may be
13709 -- a private type with unknown discriminants whose full view has
13710 -- discriminants which are invisible.
13711
13712 elsif not Has_Discriminants (T)
13713 or else
13714 (Has_Unknown_Discriminants (T)
13715 and then Is_Private_Type (T))
13716 then
13717 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13718 Fixup_Bad_Constraint;
13719 return;
13720
13721 elsif Is_Constrained (E)
13722 or else (Ekind (E) = E_Class_Wide_Subtype
13723 and then Present (Discriminant_Constraint (E)))
13724 then
13725 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13726 Fixup_Bad_Constraint;
13727 return;
13728 end if;
13729
13730 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13731 -- applies to the base type.
13732
13733 T := Base_Type (T);
13734
13735 Constr := Build_Discriminant_Constraints (T, S);
13736
13737 -- If the list returned was empty we had an error in building the
13738 -- discriminant constraint. We have also already signalled an error
13739 -- in the incomplete type case
13740
13741 if Is_Empty_Elmt_List (Constr) then
13742 Fixup_Bad_Constraint;
13743 return;
13744 end if;
13745
13746 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13747 end Constrain_Discriminated_Type;
13748
13749 ---------------------------
13750 -- Constrain_Enumeration --
13751 ---------------------------
13752
13753 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13754 T : constant Entity_Id := Entity (Subtype_Mark (S));
13755 C : constant Node_Id := Constraint (S);
13756
13757 begin
13758 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13759
13760 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13761
13762 Set_Etype (Def_Id, Base_Type (T));
13763 Set_Size_Info (Def_Id, (T));
13764 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13765 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13766
13767 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13768
13769 Set_Discrete_RM_Size (Def_Id);
13770 end Constrain_Enumeration;
13771
13772 ----------------------
13773 -- Constrain_Float --
13774 ----------------------
13775
13776 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13777 T : constant Entity_Id := Entity (Subtype_Mark (S));
13778 C : Node_Id;
13779 D : Node_Id;
13780 Rais : Node_Id;
13781
13782 begin
13783 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13784
13785 Set_Etype (Def_Id, Base_Type (T));
13786 Set_Size_Info (Def_Id, (T));
13787 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13788
13789 -- Process the constraint
13790
13791 C := Constraint (S);
13792
13793 -- Digits constraint present
13794
13795 if Nkind (C) = N_Digits_Constraint then
13796
13797 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13798 Check_Restriction (No_Obsolescent_Features, C);
13799
13800 if Warn_On_Obsolescent_Feature then
13801 Error_Msg_N
13802 ("subtype digits constraint is an " &
13803 "obsolescent feature (RM J.3(8))?j?", C);
13804 end if;
13805
13806 D := Digits_Expression (C);
13807 Analyze_And_Resolve (D, Any_Integer);
13808 Check_Digits_Expression (D);
13809 Set_Digits_Value (Def_Id, Expr_Value (D));
13810
13811 -- Check that digits value is in range. Obviously we can do this
13812 -- at compile time, but it is strictly a runtime check, and of
13813 -- course there is an ACVC test that checks this.
13814
13815 if Digits_Value (Def_Id) > Digits_Value (T) then
13816 Error_Msg_Uint_1 := Digits_Value (T);
13817 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13818 Rais :=
13819 Make_Raise_Constraint_Error (Sloc (D),
13820 Reason => CE_Range_Check_Failed);
13821 Insert_Action (Declaration_Node (Def_Id), Rais);
13822 end if;
13823
13824 C := Range_Constraint (C);
13825
13826 -- No digits constraint present
13827
13828 else
13829 Set_Digits_Value (Def_Id, Digits_Value (T));
13830 end if;
13831
13832 -- Range constraint present
13833
13834 if Nkind (C) = N_Range_Constraint then
13835 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13836
13837 -- No range constraint present
13838
13839 else
13840 pragma Assert (No (C));
13841 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13842 end if;
13843
13844 Set_Is_Constrained (Def_Id);
13845 end Constrain_Float;
13846
13847 ---------------------
13848 -- Constrain_Index --
13849 ---------------------
13850
13851 procedure Constrain_Index
13852 (Index : Node_Id;
13853 S : Node_Id;
13854 Related_Nod : Node_Id;
13855 Related_Id : Entity_Id;
13856 Suffix : Character;
13857 Suffix_Index : Nat)
13858 is
13859 Def_Id : Entity_Id;
13860 R : Node_Id := Empty;
13861 T : constant Entity_Id := Etype (Index);
13862
13863 begin
13864 Def_Id :=
13865 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13866 Set_Etype (Def_Id, Base_Type (T));
13867
13868 if Nkind (S) = N_Range
13869 or else
13870 (Nkind (S) = N_Attribute_Reference
13871 and then Attribute_Name (S) = Name_Range)
13872 then
13873 -- A Range attribute will be transformed into N_Range by Resolve
13874
13875 Analyze (S);
13876 Set_Etype (S, T);
13877 R := S;
13878
13879 Process_Range_Expr_In_Decl (R, T);
13880
13881 if not Error_Posted (S)
13882 and then
13883 (Nkind (S) /= N_Range
13884 or else not Covers (T, (Etype (Low_Bound (S))))
13885 or else not Covers (T, (Etype (High_Bound (S)))))
13886 then
13887 if Base_Type (T) /= Any_Type
13888 and then Etype (Low_Bound (S)) /= Any_Type
13889 and then Etype (High_Bound (S)) /= Any_Type
13890 then
13891 Error_Msg_N ("range expected", S);
13892 end if;
13893 end if;
13894
13895 elsif Nkind (S) = N_Subtype_Indication then
13896
13897 -- The parser has verified that this is a discrete indication
13898
13899 Resolve_Discrete_Subtype_Indication (S, T);
13900 Bad_Predicated_Subtype_Use
13901 ("subtype& has predicate, not allowed in index constraint",
13902 S, Entity (Subtype_Mark (S)));
13903
13904 R := Range_Expression (Constraint (S));
13905
13906 -- Capture values of bounds and generate temporaries for them if
13907 -- needed, since checks may cause duplication of the expressions
13908 -- which must not be reevaluated.
13909
13910 -- The forced evaluation removes side effects from expressions, which
13911 -- should occur also in GNATprove mode. Otherwise, we end up with
13912 -- unexpected insertions of actions at places where this is not
13913 -- supposed to occur, e.g. on default parameters of a call.
13914
13915 if Expander_Active or GNATprove_Mode then
13916 Force_Evaluation
13917 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13918 Force_Evaluation
13919 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13920 end if;
13921
13922 elsif Nkind (S) = N_Discriminant_Association then
13923
13924 -- Syntactically valid in subtype indication
13925
13926 Error_Msg_N ("invalid index constraint", S);
13927 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13928 return;
13929
13930 -- Subtype_Mark case, no anonymous subtypes to construct
13931
13932 else
13933 Analyze (S);
13934
13935 if Is_Entity_Name (S) then
13936 if not Is_Type (Entity (S)) then
13937 Error_Msg_N ("expect subtype mark for index constraint", S);
13938
13939 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13940 Wrong_Type (S, Base_Type (T));
13941
13942 -- Check error of subtype with predicate in index constraint
13943
13944 else
13945 Bad_Predicated_Subtype_Use
13946 ("subtype& has predicate, not allowed in index constraint",
13947 S, Entity (S));
13948 end if;
13949
13950 return;
13951
13952 else
13953 Error_Msg_N ("invalid index constraint", S);
13954 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13955 return;
13956 end if;
13957 end if;
13958
13959 -- Complete construction of the Itype
13960
13961 if Is_Modular_Integer_Type (T) then
13962 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13963
13964 elsif Is_Integer_Type (T) then
13965 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13966
13967 else
13968 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13969 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13970 Set_First_Literal (Def_Id, First_Literal (T));
13971 end if;
13972
13973 Set_Size_Info (Def_Id, (T));
13974 Set_RM_Size (Def_Id, RM_Size (T));
13975 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13976
13977 Set_Scalar_Range (Def_Id, R);
13978
13979 Set_Etype (S, Def_Id);
13980 Set_Discrete_RM_Size (Def_Id);
13981 end Constrain_Index;
13982
13983 -----------------------
13984 -- Constrain_Integer --
13985 -----------------------
13986
13987 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13988 T : constant Entity_Id := Entity (Subtype_Mark (S));
13989 C : constant Node_Id := Constraint (S);
13990
13991 begin
13992 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13993
13994 if Is_Modular_Integer_Type (T) then
13995 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13996 else
13997 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13998 end if;
13999
14000 Set_Etype (Def_Id, Base_Type (T));
14001 Set_Size_Info (Def_Id, (T));
14002 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14003 Set_Discrete_RM_Size (Def_Id);
14004 end Constrain_Integer;
14005
14006 ------------------------------
14007 -- Constrain_Ordinary_Fixed --
14008 ------------------------------
14009
14010 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14011 T : constant Entity_Id := Entity (Subtype_Mark (S));
14012 C : Node_Id;
14013 D : Node_Id;
14014 Rais : Node_Id;
14015
14016 begin
14017 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14018 Set_Etype (Def_Id, Base_Type (T));
14019 Set_Size_Info (Def_Id, (T));
14020 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14021 Set_Small_Value (Def_Id, Small_Value (T));
14022
14023 -- Process the constraint
14024
14025 C := Constraint (S);
14026
14027 -- Delta constraint present
14028
14029 if Nkind (C) = N_Delta_Constraint then
14030
14031 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14032 Check_Restriction (No_Obsolescent_Features, C);
14033
14034 if Warn_On_Obsolescent_Feature then
14035 Error_Msg_S
14036 ("subtype delta constraint is an " &
14037 "obsolescent feature (RM J.3(7))?j?");
14038 end if;
14039
14040 D := Delta_Expression (C);
14041 Analyze_And_Resolve (D, Any_Real);
14042 Check_Delta_Expression (D);
14043 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14044
14045 -- Check that delta value is in range. Obviously we can do this
14046 -- at compile time, but it is strictly a runtime check, and of
14047 -- course there is an ACVC test that checks this.
14048
14049 if Delta_Value (Def_Id) < Delta_Value (T) then
14050 Error_Msg_N ("??delta value is too small", D);
14051 Rais :=
14052 Make_Raise_Constraint_Error (Sloc (D),
14053 Reason => CE_Range_Check_Failed);
14054 Insert_Action (Declaration_Node (Def_Id), Rais);
14055 end if;
14056
14057 C := Range_Constraint (C);
14058
14059 -- No delta constraint present
14060
14061 else
14062 Set_Delta_Value (Def_Id, Delta_Value (T));
14063 end if;
14064
14065 -- Range constraint present
14066
14067 if Nkind (C) = N_Range_Constraint then
14068 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14069
14070 -- No range constraint present
14071
14072 else
14073 pragma Assert (No (C));
14074 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14075 end if;
14076
14077 Set_Discrete_RM_Size (Def_Id);
14078
14079 -- Unconditionally delay the freeze, since we cannot set size
14080 -- information in all cases correctly until the freeze point.
14081
14082 Set_Has_Delayed_Freeze (Def_Id);
14083 end Constrain_Ordinary_Fixed;
14084
14085 -----------------------
14086 -- Contain_Interface --
14087 -----------------------
14088
14089 function Contain_Interface
14090 (Iface : Entity_Id;
14091 Ifaces : Elist_Id) return Boolean
14092 is
14093 Iface_Elmt : Elmt_Id;
14094
14095 begin
14096 if Present (Ifaces) then
14097 Iface_Elmt := First_Elmt (Ifaces);
14098 while Present (Iface_Elmt) loop
14099 if Node (Iface_Elmt) = Iface then
14100 return True;
14101 end if;
14102
14103 Next_Elmt (Iface_Elmt);
14104 end loop;
14105 end if;
14106
14107 return False;
14108 end Contain_Interface;
14109
14110 ---------------------------
14111 -- Convert_Scalar_Bounds --
14112 ---------------------------
14113
14114 procedure Convert_Scalar_Bounds
14115 (N : Node_Id;
14116 Parent_Type : Entity_Id;
14117 Derived_Type : Entity_Id;
14118 Loc : Source_Ptr)
14119 is
14120 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14121
14122 Lo : Node_Id;
14123 Hi : Node_Id;
14124 Rng : Node_Id;
14125
14126 begin
14127 -- Defend against previous errors
14128
14129 if No (Scalar_Range (Derived_Type)) then
14130 Check_Error_Detected;
14131 return;
14132 end if;
14133
14134 Lo := Build_Scalar_Bound
14135 (Type_Low_Bound (Derived_Type),
14136 Parent_Type, Implicit_Base);
14137
14138 Hi := Build_Scalar_Bound
14139 (Type_High_Bound (Derived_Type),
14140 Parent_Type, Implicit_Base);
14141
14142 Rng :=
14143 Make_Range (Loc,
14144 Low_Bound => Lo,
14145 High_Bound => Hi);
14146
14147 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14148
14149 Set_Parent (Rng, N);
14150 Set_Scalar_Range (Derived_Type, Rng);
14151
14152 -- Analyze the bounds
14153
14154 Analyze_And_Resolve (Lo, Implicit_Base);
14155 Analyze_And_Resolve (Hi, Implicit_Base);
14156
14157 -- Analyze the range itself, except that we do not analyze it if
14158 -- the bounds are real literals, and we have a fixed-point type.
14159 -- The reason for this is that we delay setting the bounds in this
14160 -- case till we know the final Small and Size values (see circuit
14161 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14162
14163 if Is_Fixed_Point_Type (Parent_Type)
14164 and then Nkind (Lo) = N_Real_Literal
14165 and then Nkind (Hi) = N_Real_Literal
14166 then
14167 return;
14168
14169 -- Here we do the analysis of the range
14170
14171 -- Note: we do this manually, since if we do a normal Analyze and
14172 -- Resolve call, there are problems with the conversions used for
14173 -- the derived type range.
14174
14175 else
14176 Set_Etype (Rng, Implicit_Base);
14177 Set_Analyzed (Rng, True);
14178 end if;
14179 end Convert_Scalar_Bounds;
14180
14181 -------------------
14182 -- Copy_And_Swap --
14183 -------------------
14184
14185 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14186 begin
14187 -- Initialize new full declaration entity by copying the pertinent
14188 -- fields of the corresponding private declaration entity.
14189
14190 -- We temporarily set Ekind to a value appropriate for a type to
14191 -- avoid assert failures in Einfo from checking for setting type
14192 -- attributes on something that is not a type. Ekind (Priv) is an
14193 -- appropriate choice, since it allowed the attributes to be set
14194 -- in the first place. This Ekind value will be modified later.
14195
14196 Set_Ekind (Full, Ekind (Priv));
14197
14198 -- Also set Etype temporarily to Any_Type, again, in the absence
14199 -- of errors, it will be properly reset, and if there are errors,
14200 -- then we want a value of Any_Type to remain.
14201
14202 Set_Etype (Full, Any_Type);
14203
14204 -- Now start copying attributes
14205
14206 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14207
14208 if Has_Discriminants (Full) then
14209 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14210 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14211 end if;
14212
14213 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14214 Set_Homonym (Full, Homonym (Priv));
14215 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14216 Set_Is_Public (Full, Is_Public (Priv));
14217 Set_Is_Pure (Full, Is_Pure (Priv));
14218 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14219 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14220 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14221 Set_Has_Pragma_Unreferenced_Objects
14222 (Full, Has_Pragma_Unreferenced_Objects
14223 (Priv));
14224
14225 Conditional_Delay (Full, Priv);
14226
14227 if Is_Tagged_Type (Full) then
14228 Set_Direct_Primitive_Operations
14229 (Full, Direct_Primitive_Operations (Priv));
14230 Set_No_Tagged_Streams_Pragma
14231 (Full, No_Tagged_Streams_Pragma (Priv));
14232
14233 if Is_Base_Type (Priv) then
14234 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14235 end if;
14236 end if;
14237
14238 Set_Is_Volatile (Full, Is_Volatile (Priv));
14239 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14240 Set_Scope (Full, Scope (Priv));
14241 Set_Next_Entity (Full, Next_Entity (Priv));
14242 Set_First_Entity (Full, First_Entity (Priv));
14243 Set_Last_Entity (Full, Last_Entity (Priv));
14244
14245 -- If access types have been recorded for later handling, keep them in
14246 -- the full view so that they get handled when the full view freeze
14247 -- node is expanded.
14248
14249 if Present (Freeze_Node (Priv))
14250 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14251 then
14252 Ensure_Freeze_Node (Full);
14253 Set_Access_Types_To_Process
14254 (Freeze_Node (Full),
14255 Access_Types_To_Process (Freeze_Node (Priv)));
14256 end if;
14257
14258 -- Swap the two entities. Now Private is the full type entity and Full
14259 -- is the private one. They will be swapped back at the end of the
14260 -- private part. This swapping ensures that the entity that is visible
14261 -- in the private part is the full declaration.
14262
14263 Exchange_Entities (Priv, Full);
14264 Append_Entity (Full, Scope (Full));
14265 end Copy_And_Swap;
14266
14267 -------------------------------------
14268 -- Copy_Array_Base_Type_Attributes --
14269 -------------------------------------
14270
14271 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14272 begin
14273 Set_Component_Alignment (T1, Component_Alignment (T2));
14274 Set_Component_Type (T1, Component_Type (T2));
14275 Set_Component_Size (T1, Component_Size (T2));
14276 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14277 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14278 Propagate_Concurrent_Flags (T1, T2);
14279 Set_Is_Packed (T1, Is_Packed (T2));
14280 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14281 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14282 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14283 end Copy_Array_Base_Type_Attributes;
14284
14285 -----------------------------------
14286 -- Copy_Array_Subtype_Attributes --
14287 -----------------------------------
14288
14289 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14290 begin
14291 Set_Size_Info (T1, T2);
14292
14293 Set_First_Index (T1, First_Index (T2));
14294 Set_Is_Aliased (T1, Is_Aliased (T2));
14295 Set_Is_Volatile (T1, Is_Volatile (T2));
14296 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14297 Set_Is_Constrained (T1, Is_Constrained (T2));
14298 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14299 Inherit_Rep_Item_Chain (T1, T2);
14300 Set_Convention (T1, Convention (T2));
14301 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14302 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14303 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14304 end Copy_Array_Subtype_Attributes;
14305
14306 -----------------------------------
14307 -- Create_Constrained_Components --
14308 -----------------------------------
14309
14310 procedure Create_Constrained_Components
14311 (Subt : Entity_Id;
14312 Decl_Node : Node_Id;
14313 Typ : Entity_Id;
14314 Constraints : Elist_Id)
14315 is
14316 Loc : constant Source_Ptr := Sloc (Subt);
14317 Comp_List : constant Elist_Id := New_Elmt_List;
14318 Parent_Type : constant Entity_Id := Etype (Typ);
14319 Assoc_List : constant List_Id := New_List;
14320 Discr_Val : Elmt_Id;
14321 Errors : Boolean;
14322 New_C : Entity_Id;
14323 Old_C : Entity_Id;
14324 Is_Static : Boolean := True;
14325
14326 procedure Collect_Fixed_Components (Typ : Entity_Id);
14327 -- Collect parent type components that do not appear in a variant part
14328
14329 procedure Create_All_Components;
14330 -- Iterate over Comp_List to create the components of the subtype
14331
14332 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14333 -- Creates a new component from Old_Compon, copying all the fields from
14334 -- it, including its Etype, inserts the new component in the Subt entity
14335 -- chain and returns the new component.
14336
14337 function Is_Variant_Record (T : Entity_Id) return Boolean;
14338 -- If true, and discriminants are static, collect only components from
14339 -- variants selected by discriminant values.
14340
14341 ------------------------------
14342 -- Collect_Fixed_Components --
14343 ------------------------------
14344
14345 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14346 begin
14347 -- Build association list for discriminants, and find components of the
14348 -- variant part selected by the values of the discriminants.
14349
14350 Old_C := First_Discriminant (Typ);
14351 Discr_Val := First_Elmt (Constraints);
14352 while Present (Old_C) loop
14353 Append_To (Assoc_List,
14354 Make_Component_Association (Loc,
14355 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14356 Expression => New_Copy (Node (Discr_Val))));
14357
14358 Next_Elmt (Discr_Val);
14359 Next_Discriminant (Old_C);
14360 end loop;
14361
14362 -- The tag and the possible parent component are unconditionally in
14363 -- the subtype.
14364
14365 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14366 Old_C := First_Component (Typ);
14367 while Present (Old_C) loop
14368 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14369 Append_Elmt (Old_C, Comp_List);
14370 end if;
14371
14372 Next_Component (Old_C);
14373 end loop;
14374 end if;
14375 end Collect_Fixed_Components;
14376
14377 ---------------------------
14378 -- Create_All_Components --
14379 ---------------------------
14380
14381 procedure Create_All_Components is
14382 Comp : Elmt_Id;
14383
14384 begin
14385 Comp := First_Elmt (Comp_List);
14386 while Present (Comp) loop
14387 Old_C := Node (Comp);
14388 New_C := Create_Component (Old_C);
14389
14390 Set_Etype
14391 (New_C,
14392 Constrain_Component_Type
14393 (Old_C, Subt, Decl_Node, Typ, Constraints));
14394 Set_Is_Public (New_C, Is_Public (Subt));
14395
14396 Next_Elmt (Comp);
14397 end loop;
14398 end Create_All_Components;
14399
14400 ----------------------
14401 -- Create_Component --
14402 ----------------------
14403
14404 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14405 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14406
14407 begin
14408 if Ekind (Old_Compon) = E_Discriminant
14409 and then Is_Completely_Hidden (Old_Compon)
14410 then
14411 -- This is a shadow discriminant created for a discriminant of
14412 -- the parent type, which needs to be present in the subtype.
14413 -- Give the shadow discriminant an internal name that cannot
14414 -- conflict with that of visible components.
14415
14416 Set_Chars (New_Compon, New_Internal_Name ('C'));
14417 end if;
14418
14419 -- Set the parent so we have a proper link for freezing etc. This is
14420 -- not a real parent pointer, since of course our parent does not own
14421 -- up to us and reference us, we are an illegitimate child of the
14422 -- original parent.
14423
14424 Set_Parent (New_Compon, Parent (Old_Compon));
14425
14426 -- If the old component's Esize was already determined and is a
14427 -- static value, then the new component simply inherits it. Otherwise
14428 -- the old component's size may require run-time determination, but
14429 -- the new component's size still might be statically determinable
14430 -- (if, for example it has a static constraint). In that case we want
14431 -- Layout_Type to recompute the component's size, so we reset its
14432 -- size and positional fields.
14433
14434 if Frontend_Layout_On_Target
14435 and then not Known_Static_Esize (Old_Compon)
14436 then
14437 Set_Esize (New_Compon, Uint_0);
14438 Init_Normalized_First_Bit (New_Compon);
14439 Init_Normalized_Position (New_Compon);
14440 Init_Normalized_Position_Max (New_Compon);
14441 end if;
14442
14443 -- We do not want this node marked as Comes_From_Source, since
14444 -- otherwise it would get first class status and a separate cross-
14445 -- reference line would be generated. Illegitimate children do not
14446 -- rate such recognition.
14447
14448 Set_Comes_From_Source (New_Compon, False);
14449
14450 -- But it is a real entity, and a birth certificate must be properly
14451 -- registered by entering it into the entity list.
14452
14453 Enter_Name (New_Compon);
14454
14455 return New_Compon;
14456 end Create_Component;
14457
14458 -----------------------
14459 -- Is_Variant_Record --
14460 -----------------------
14461
14462 function Is_Variant_Record (T : Entity_Id) return Boolean is
14463 begin
14464 return Nkind (Parent (T)) = N_Full_Type_Declaration
14465 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14466 and then Present (Component_List (Type_Definition (Parent (T))))
14467 and then
14468 Present
14469 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14470 end Is_Variant_Record;
14471
14472 -- Start of processing for Create_Constrained_Components
14473
14474 begin
14475 pragma Assert (Subt /= Base_Type (Subt));
14476 pragma Assert (Typ = Base_Type (Typ));
14477
14478 Set_First_Entity (Subt, Empty);
14479 Set_Last_Entity (Subt, Empty);
14480
14481 -- Check whether constraint is fully static, in which case we can
14482 -- optimize the list of components.
14483
14484 Discr_Val := First_Elmt (Constraints);
14485 while Present (Discr_Val) loop
14486 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14487 Is_Static := False;
14488 exit;
14489 end if;
14490
14491 Next_Elmt (Discr_Val);
14492 end loop;
14493
14494 Set_Has_Static_Discriminants (Subt, Is_Static);
14495
14496 Push_Scope (Subt);
14497
14498 -- Inherit the discriminants of the parent type
14499
14500 Add_Discriminants : declare
14501 Num_Disc : Nat;
14502 Num_Gird : Nat;
14503
14504 begin
14505 Num_Disc := 0;
14506 Old_C := First_Discriminant (Typ);
14507
14508 while Present (Old_C) loop
14509 Num_Disc := Num_Disc + 1;
14510 New_C := Create_Component (Old_C);
14511 Set_Is_Public (New_C, Is_Public (Subt));
14512 Next_Discriminant (Old_C);
14513 end loop;
14514
14515 -- For an untagged derived subtype, the number of discriminants may
14516 -- be smaller than the number of inherited discriminants, because
14517 -- several of them may be renamed by a single new discriminant or
14518 -- constrained. In this case, add the hidden discriminants back into
14519 -- the subtype, because they need to be present if the optimizer of
14520 -- the GCC 4.x back-end decides to break apart assignments between
14521 -- objects using the parent view into member-wise assignments.
14522
14523 Num_Gird := 0;
14524
14525 if Is_Derived_Type (Typ)
14526 and then not Is_Tagged_Type (Typ)
14527 then
14528 Old_C := First_Stored_Discriminant (Typ);
14529
14530 while Present (Old_C) loop
14531 Num_Gird := Num_Gird + 1;
14532 Next_Stored_Discriminant (Old_C);
14533 end loop;
14534 end if;
14535
14536 if Num_Gird > Num_Disc then
14537
14538 -- Find out multiple uses of new discriminants, and add hidden
14539 -- components for the extra renamed discriminants. We recognize
14540 -- multiple uses through the Corresponding_Discriminant of a
14541 -- new discriminant: if it constrains several old discriminants,
14542 -- this field points to the last one in the parent type. The
14543 -- stored discriminants of the derived type have the same name
14544 -- as those of the parent.
14545
14546 declare
14547 Constr : Elmt_Id;
14548 New_Discr : Entity_Id;
14549 Old_Discr : Entity_Id;
14550
14551 begin
14552 Constr := First_Elmt (Stored_Constraint (Typ));
14553 Old_Discr := First_Stored_Discriminant (Typ);
14554 while Present (Constr) loop
14555 if Is_Entity_Name (Node (Constr))
14556 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14557 then
14558 New_Discr := Entity (Node (Constr));
14559
14560 if Chars (Corresponding_Discriminant (New_Discr)) /=
14561 Chars (Old_Discr)
14562 then
14563 -- The new discriminant has been used to rename a
14564 -- subsequent old discriminant. Introduce a shadow
14565 -- component for the current old discriminant.
14566
14567 New_C := Create_Component (Old_Discr);
14568 Set_Original_Record_Component (New_C, Old_Discr);
14569 end if;
14570
14571 else
14572 -- The constraint has eliminated the old discriminant.
14573 -- Introduce a shadow component.
14574
14575 New_C := Create_Component (Old_Discr);
14576 Set_Original_Record_Component (New_C, Old_Discr);
14577 end if;
14578
14579 Next_Elmt (Constr);
14580 Next_Stored_Discriminant (Old_Discr);
14581 end loop;
14582 end;
14583 end if;
14584 end Add_Discriminants;
14585
14586 if Is_Static
14587 and then Is_Variant_Record (Typ)
14588 then
14589 Collect_Fixed_Components (Typ);
14590
14591 Gather_Components (
14592 Typ,
14593 Component_List (Type_Definition (Parent (Typ))),
14594 Governed_By => Assoc_List,
14595 Into => Comp_List,
14596 Report_Errors => Errors);
14597 pragma Assert (not Errors
14598 or else Serious_Errors_Detected > 0);
14599
14600 Create_All_Components;
14601
14602 -- If the subtype declaration is created for a tagged type derivation
14603 -- with constraints, we retrieve the record definition of the parent
14604 -- type to select the components of the proper variant.
14605
14606 elsif Is_Static
14607 and then Is_Tagged_Type (Typ)
14608 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14609 and then
14610 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14611 and then Is_Variant_Record (Parent_Type)
14612 then
14613 Collect_Fixed_Components (Typ);
14614
14615 Gather_Components
14616 (Typ,
14617 Component_List (Type_Definition (Parent (Parent_Type))),
14618 Governed_By => Assoc_List,
14619 Into => Comp_List,
14620 Report_Errors => Errors);
14621
14622 -- Note: previously there was a check at this point that no errors
14623 -- were detected. As a consequence of AI05-220 there may be an error
14624 -- if an inherited discriminant that controls a variant has a non-
14625 -- static constraint.
14626
14627 -- If the tagged derivation has a type extension, collect all the
14628 -- new components therein.
14629
14630 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14631 then
14632 Old_C := First_Component (Typ);
14633 while Present (Old_C) loop
14634 if Original_Record_Component (Old_C) = Old_C
14635 and then Chars (Old_C) /= Name_uTag
14636 and then Chars (Old_C) /= Name_uParent
14637 then
14638 Append_Elmt (Old_C, Comp_List);
14639 end if;
14640
14641 Next_Component (Old_C);
14642 end loop;
14643 end if;
14644
14645 Create_All_Components;
14646
14647 else
14648 -- If discriminants are not static, or if this is a multi-level type
14649 -- extension, we have to include all components of the parent type.
14650
14651 Old_C := First_Component (Typ);
14652 while Present (Old_C) loop
14653 New_C := Create_Component (Old_C);
14654
14655 Set_Etype
14656 (New_C,
14657 Constrain_Component_Type
14658 (Old_C, Subt, Decl_Node, Typ, Constraints));
14659 Set_Is_Public (New_C, Is_Public (Subt));
14660
14661 Next_Component (Old_C);
14662 end loop;
14663 end if;
14664
14665 End_Scope;
14666 end Create_Constrained_Components;
14667
14668 ------------------------------------------
14669 -- Decimal_Fixed_Point_Type_Declaration --
14670 ------------------------------------------
14671
14672 procedure Decimal_Fixed_Point_Type_Declaration
14673 (T : Entity_Id;
14674 Def : Node_Id)
14675 is
14676 Loc : constant Source_Ptr := Sloc (Def);
14677 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14678 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14679 Implicit_Base : Entity_Id;
14680 Digs_Val : Uint;
14681 Delta_Val : Ureal;
14682 Scale_Val : Uint;
14683 Bound_Val : Ureal;
14684
14685 begin
14686 Check_SPARK_05_Restriction
14687 ("decimal fixed point type is not allowed", Def);
14688 Check_Restriction (No_Fixed_Point, Def);
14689
14690 -- Create implicit base type
14691
14692 Implicit_Base :=
14693 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14694 Set_Etype (Implicit_Base, Implicit_Base);
14695
14696 -- Analyze and process delta expression
14697
14698 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14699
14700 Check_Delta_Expression (Delta_Expr);
14701 Delta_Val := Expr_Value_R (Delta_Expr);
14702
14703 -- Check delta is power of 10, and determine scale value from it
14704
14705 declare
14706 Val : Ureal;
14707
14708 begin
14709 Scale_Val := Uint_0;
14710 Val := Delta_Val;
14711
14712 if Val < Ureal_1 then
14713 while Val < Ureal_1 loop
14714 Val := Val * Ureal_10;
14715 Scale_Val := Scale_Val + 1;
14716 end loop;
14717
14718 if Scale_Val > 18 then
14719 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14720 Scale_Val := UI_From_Int (+18);
14721 end if;
14722
14723 else
14724 while Val > Ureal_1 loop
14725 Val := Val / Ureal_10;
14726 Scale_Val := Scale_Val - 1;
14727 end loop;
14728
14729 if Scale_Val < -18 then
14730 Error_Msg_N ("scale is less than minimum value of -18", Def);
14731 Scale_Val := UI_From_Int (-18);
14732 end if;
14733 end if;
14734
14735 if Val /= Ureal_1 then
14736 Error_Msg_N ("delta expression must be a power of 10", Def);
14737 Delta_Val := Ureal_10 ** (-Scale_Val);
14738 end if;
14739 end;
14740
14741 -- Set delta, scale and small (small = delta for decimal type)
14742
14743 Set_Delta_Value (Implicit_Base, Delta_Val);
14744 Set_Scale_Value (Implicit_Base, Scale_Val);
14745 Set_Small_Value (Implicit_Base, Delta_Val);
14746
14747 -- Analyze and process digits expression
14748
14749 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14750 Check_Digits_Expression (Digs_Expr);
14751 Digs_Val := Expr_Value (Digs_Expr);
14752
14753 if Digs_Val > 18 then
14754 Digs_Val := UI_From_Int (+18);
14755 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14756 end if;
14757
14758 Set_Digits_Value (Implicit_Base, Digs_Val);
14759 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14760
14761 -- Set range of base type from digits value for now. This will be
14762 -- expanded to represent the true underlying base range by Freeze.
14763
14764 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14765
14766 -- Note: We leave size as zero for now, size will be set at freeze
14767 -- time. We have to do this for ordinary fixed-point, because the size
14768 -- depends on the specified small, and we might as well do the same for
14769 -- decimal fixed-point.
14770
14771 pragma Assert (Esize (Implicit_Base) = Uint_0);
14772
14773 -- If there are bounds given in the declaration use them as the
14774 -- bounds of the first named subtype.
14775
14776 if Present (Real_Range_Specification (Def)) then
14777 declare
14778 RRS : constant Node_Id := Real_Range_Specification (Def);
14779 Low : constant Node_Id := Low_Bound (RRS);
14780 High : constant Node_Id := High_Bound (RRS);
14781 Low_Val : Ureal;
14782 High_Val : Ureal;
14783
14784 begin
14785 Analyze_And_Resolve (Low, Any_Real);
14786 Analyze_And_Resolve (High, Any_Real);
14787 Check_Real_Bound (Low);
14788 Check_Real_Bound (High);
14789 Low_Val := Expr_Value_R (Low);
14790 High_Val := Expr_Value_R (High);
14791
14792 if Low_Val < (-Bound_Val) then
14793 Error_Msg_N
14794 ("range low bound too small for digits value", Low);
14795 Low_Val := -Bound_Val;
14796 end if;
14797
14798 if High_Val > Bound_Val then
14799 Error_Msg_N
14800 ("range high bound too large for digits value", High);
14801 High_Val := Bound_Val;
14802 end if;
14803
14804 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14805 end;
14806
14807 -- If no explicit range, use range that corresponds to given
14808 -- digits value. This will end up as the final range for the
14809 -- first subtype.
14810
14811 else
14812 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14813 end if;
14814
14815 -- Complete entity for first subtype. The inheritance of the rep item
14816 -- chain ensures that SPARK-related pragmas are not clobbered when the
14817 -- decimal fixed point type acts as a full view of a private type.
14818
14819 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14820 Set_Etype (T, Implicit_Base);
14821 Set_Size_Info (T, Implicit_Base);
14822 Inherit_Rep_Item_Chain (T, Implicit_Base);
14823 Set_Digits_Value (T, Digs_Val);
14824 Set_Delta_Value (T, Delta_Val);
14825 Set_Small_Value (T, Delta_Val);
14826 Set_Scale_Value (T, Scale_Val);
14827 Set_Is_Constrained (T);
14828 end Decimal_Fixed_Point_Type_Declaration;
14829
14830 -----------------------------------
14831 -- Derive_Progenitor_Subprograms --
14832 -----------------------------------
14833
14834 procedure Derive_Progenitor_Subprograms
14835 (Parent_Type : Entity_Id;
14836 Tagged_Type : Entity_Id)
14837 is
14838 E : Entity_Id;
14839 Elmt : Elmt_Id;
14840 Iface : Entity_Id;
14841 Iface_Elmt : Elmt_Id;
14842 Iface_Subp : Entity_Id;
14843 New_Subp : Entity_Id := Empty;
14844 Prim_Elmt : Elmt_Id;
14845 Subp : Entity_Id;
14846 Typ : Entity_Id;
14847
14848 begin
14849 pragma Assert (Ada_Version >= Ada_2005
14850 and then Is_Record_Type (Tagged_Type)
14851 and then Is_Tagged_Type (Tagged_Type)
14852 and then Has_Interfaces (Tagged_Type));
14853
14854 -- Step 1: Transfer to the full-view primitives associated with the
14855 -- partial-view that cover interface primitives. Conceptually this
14856 -- work should be done later by Process_Full_View; done here to
14857 -- simplify its implementation at later stages. It can be safely
14858 -- done here because interfaces must be visible in the partial and
14859 -- private view (RM 7.3(7.3/2)).
14860
14861 -- Small optimization: This work is only required if the parent may
14862 -- have entities whose Alias attribute reference an interface primitive.
14863 -- Such a situation may occur if the parent is an abstract type and the
14864 -- primitive has not been yet overridden or if the parent is a generic
14865 -- formal type covering interfaces.
14866
14867 -- If the tagged type is not abstract, it cannot have abstract
14868 -- primitives (the only entities in the list of primitives of
14869 -- non-abstract tagged types that can reference abstract primitives
14870 -- through its Alias attribute are the internal entities that have
14871 -- attribute Interface_Alias, and these entities are generated later
14872 -- by Add_Internal_Interface_Entities).
14873
14874 if In_Private_Part (Current_Scope)
14875 and then (Is_Abstract_Type (Parent_Type)
14876 or else
14877 Is_Generic_Type (Parent_Type))
14878 then
14879 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14880 while Present (Elmt) loop
14881 Subp := Node (Elmt);
14882
14883 -- At this stage it is not possible to have entities in the list
14884 -- of primitives that have attribute Interface_Alias.
14885
14886 pragma Assert (No (Interface_Alias (Subp)));
14887
14888 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14889
14890 if Is_Interface (Typ) then
14891 E := Find_Primitive_Covering_Interface
14892 (Tagged_Type => Tagged_Type,
14893 Iface_Prim => Subp);
14894
14895 if Present (E)
14896 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14897 then
14898 Replace_Elmt (Elmt, E);
14899 Remove_Homonym (Subp);
14900 end if;
14901 end if;
14902
14903 Next_Elmt (Elmt);
14904 end loop;
14905 end if;
14906
14907 -- Step 2: Add primitives of progenitors that are not implemented by
14908 -- parents of Tagged_Type.
14909
14910 if Present (Interfaces (Base_Type (Tagged_Type))) then
14911 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14912 while Present (Iface_Elmt) loop
14913 Iface := Node (Iface_Elmt);
14914
14915 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14916 while Present (Prim_Elmt) loop
14917 Iface_Subp := Node (Prim_Elmt);
14918
14919 -- Exclude derivation of predefined primitives except those
14920 -- that come from source, or are inherited from one that comes
14921 -- from source. Required to catch declarations of equality
14922 -- operators of interfaces. For example:
14923
14924 -- type Iface is interface;
14925 -- function "=" (Left, Right : Iface) return Boolean;
14926
14927 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14928 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14929 then
14930 E := Find_Primitive_Covering_Interface
14931 (Tagged_Type => Tagged_Type,
14932 Iface_Prim => Iface_Subp);
14933
14934 -- If not found we derive a new primitive leaving its alias
14935 -- attribute referencing the interface primitive.
14936
14937 if No (E) then
14938 Derive_Subprogram
14939 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14940
14941 -- Ada 2012 (AI05-0197): If the covering primitive's name
14942 -- differs from the name of the interface primitive then it
14943 -- is a private primitive inherited from a parent type. In
14944 -- such case, given that Tagged_Type covers the interface,
14945 -- the inherited private primitive becomes visible. For such
14946 -- purpose we add a new entity that renames the inherited
14947 -- private primitive.
14948
14949 elsif Chars (E) /= Chars (Iface_Subp) then
14950 pragma Assert (Has_Suffix (E, 'P'));
14951 Derive_Subprogram
14952 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14953 Set_Alias (New_Subp, E);
14954 Set_Is_Abstract_Subprogram (New_Subp,
14955 Is_Abstract_Subprogram (E));
14956
14957 -- Propagate to the full view interface entities associated
14958 -- with the partial view.
14959
14960 elsif In_Private_Part (Current_Scope)
14961 and then Present (Alias (E))
14962 and then Alias (E) = Iface_Subp
14963 and then
14964 List_Containing (Parent (E)) /=
14965 Private_Declarations
14966 (Specification
14967 (Unit_Declaration_Node (Current_Scope)))
14968 then
14969 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14970 end if;
14971 end if;
14972
14973 Next_Elmt (Prim_Elmt);
14974 end loop;
14975
14976 Next_Elmt (Iface_Elmt);
14977 end loop;
14978 end if;
14979 end Derive_Progenitor_Subprograms;
14980
14981 -----------------------
14982 -- Derive_Subprogram --
14983 -----------------------
14984
14985 procedure Derive_Subprogram
14986 (New_Subp : out Entity_Id;
14987 Parent_Subp : Entity_Id;
14988 Derived_Type : Entity_Id;
14989 Parent_Type : Entity_Id;
14990 Actual_Subp : Entity_Id := Empty)
14991 is
14992 Formal : Entity_Id;
14993 -- Formal parameter of parent primitive operation
14994
14995 Formal_Of_Actual : Entity_Id;
14996 -- Formal parameter of actual operation, when the derivation is to
14997 -- create a renaming for a primitive operation of an actual in an
14998 -- instantiation.
14999
15000 New_Formal : Entity_Id;
15001 -- Formal of inherited operation
15002
15003 Visible_Subp : Entity_Id := Parent_Subp;
15004
15005 function Is_Private_Overriding return Boolean;
15006 -- If Subp is a private overriding of a visible operation, the inherited
15007 -- operation derives from the overridden op (even though its body is the
15008 -- overriding one) and the inherited operation is visible now. See
15009 -- sem_disp to see the full details of the handling of the overridden
15010 -- subprogram, which is removed from the list of primitive operations of
15011 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15012 -- and used to diagnose abstract operations that need overriding in the
15013 -- derived type.
15014
15015 procedure Replace_Type (Id, New_Id : Entity_Id);
15016 -- When the type is an anonymous access type, create a new access type
15017 -- designating the derived type.
15018
15019 procedure Set_Derived_Name;
15020 -- This procedure sets the appropriate Chars name for New_Subp. This
15021 -- is normally just a copy of the parent name. An exception arises for
15022 -- type support subprograms, where the name is changed to reflect the
15023 -- name of the derived type, e.g. if type foo is derived from type bar,
15024 -- then a procedure barDA is derived with a name fooDA.
15025
15026 ---------------------------
15027 -- Is_Private_Overriding --
15028 ---------------------------
15029
15030 function Is_Private_Overriding return Boolean is
15031 Prev : Entity_Id;
15032
15033 begin
15034 -- If the parent is not a dispatching operation there is no
15035 -- need to investigate overridings
15036
15037 if not Is_Dispatching_Operation (Parent_Subp) then
15038 return False;
15039 end if;
15040
15041 -- The visible operation that is overridden is a homonym of the
15042 -- parent subprogram. We scan the homonym chain to find the one
15043 -- whose alias is the subprogram we are deriving.
15044
15045 Prev := Current_Entity (Parent_Subp);
15046 while Present (Prev) loop
15047 if Ekind (Prev) = Ekind (Parent_Subp)
15048 and then Alias (Prev) = Parent_Subp
15049 and then Scope (Parent_Subp) = Scope (Prev)
15050 and then not Is_Hidden (Prev)
15051 then
15052 Visible_Subp := Prev;
15053 return True;
15054 end if;
15055
15056 Prev := Homonym (Prev);
15057 end loop;
15058
15059 return False;
15060 end Is_Private_Overriding;
15061
15062 ------------------
15063 -- Replace_Type --
15064 ------------------
15065
15066 procedure Replace_Type (Id, New_Id : Entity_Id) is
15067 Id_Type : constant Entity_Id := Etype (Id);
15068 Acc_Type : Entity_Id;
15069 Par : constant Node_Id := Parent (Derived_Type);
15070
15071 begin
15072 -- When the type is an anonymous access type, create a new access
15073 -- type designating the derived type. This itype must be elaborated
15074 -- at the point of the derivation, not on subsequent calls that may
15075 -- be out of the proper scope for Gigi, so we insert a reference to
15076 -- it after the derivation.
15077
15078 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15079 declare
15080 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15081
15082 begin
15083 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15084 and then Present (Full_View (Desig_Typ))
15085 and then not Is_Private_Type (Parent_Type)
15086 then
15087 Desig_Typ := Full_View (Desig_Typ);
15088 end if;
15089
15090 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15091
15092 -- Ada 2005 (AI-251): Handle also derivations of abstract
15093 -- interface primitives.
15094
15095 or else (Is_Interface (Desig_Typ)
15096 and then not Is_Class_Wide_Type (Desig_Typ))
15097 then
15098 Acc_Type := New_Copy (Id_Type);
15099 Set_Etype (Acc_Type, Acc_Type);
15100 Set_Scope (Acc_Type, New_Subp);
15101
15102 -- Set size of anonymous access type. If we have an access
15103 -- to an unconstrained array, this is a fat pointer, so it
15104 -- is sizes at twice addtress size.
15105
15106 if Is_Array_Type (Desig_Typ)
15107 and then not Is_Constrained (Desig_Typ)
15108 then
15109 Init_Size (Acc_Type, 2 * System_Address_Size);
15110
15111 -- Other cases use a thin pointer
15112
15113 else
15114 Init_Size (Acc_Type, System_Address_Size);
15115 end if;
15116
15117 -- Set remaining characterstics of anonymous access type
15118
15119 Init_Alignment (Acc_Type);
15120 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15121
15122 Set_Etype (New_Id, Acc_Type);
15123 Set_Scope (New_Id, New_Subp);
15124
15125 -- Create a reference to it
15126
15127 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15128
15129 else
15130 Set_Etype (New_Id, Id_Type);
15131 end if;
15132 end;
15133
15134 -- In Ada2012, a formal may have an incomplete type but the type
15135 -- derivation that inherits the primitive follows the full view.
15136
15137 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15138 or else
15139 (Ekind (Id_Type) = E_Record_Type_With_Private
15140 and then Present (Full_View (Id_Type))
15141 and then
15142 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15143 or else
15144 (Ada_Version >= Ada_2012
15145 and then Ekind (Id_Type) = E_Incomplete_Type
15146 and then Full_View (Id_Type) = Parent_Type)
15147 then
15148 -- Constraint checks on formals are generated during expansion,
15149 -- based on the signature of the original subprogram. The bounds
15150 -- of the derived type are not relevant, and thus we can use
15151 -- the base type for the formals. However, the return type may be
15152 -- used in a context that requires that the proper static bounds
15153 -- be used (a case statement, for example) and for those cases
15154 -- we must use the derived type (first subtype), not its base.
15155
15156 -- If the derived_type_definition has no constraints, we know that
15157 -- the derived type has the same constraints as the first subtype
15158 -- of the parent, and we can also use it rather than its base,
15159 -- which can lead to more efficient code.
15160
15161 if Etype (Id) = Parent_Type then
15162 if Is_Scalar_Type (Parent_Type)
15163 and then
15164 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15165 then
15166 Set_Etype (New_Id, Derived_Type);
15167
15168 elsif Nkind (Par) = N_Full_Type_Declaration
15169 and then
15170 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15171 and then
15172 Is_Entity_Name
15173 (Subtype_Indication (Type_Definition (Par)))
15174 then
15175 Set_Etype (New_Id, Derived_Type);
15176
15177 else
15178 Set_Etype (New_Id, Base_Type (Derived_Type));
15179 end if;
15180
15181 else
15182 Set_Etype (New_Id, Base_Type (Derived_Type));
15183 end if;
15184
15185 else
15186 Set_Etype (New_Id, Etype (Id));
15187 end if;
15188 end Replace_Type;
15189
15190 ----------------------
15191 -- Set_Derived_Name --
15192 ----------------------
15193
15194 procedure Set_Derived_Name is
15195 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15196 begin
15197 if Nm = TSS_Null then
15198 Set_Chars (New_Subp, Chars (Parent_Subp));
15199 else
15200 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15201 end if;
15202 end Set_Derived_Name;
15203
15204 -- Start of processing for Derive_Subprogram
15205
15206 begin
15207 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15208 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15209
15210 -- Check whether the inherited subprogram is a private operation that
15211 -- should be inherited but not yet made visible. Such subprograms can
15212 -- become visible at a later point (e.g., the private part of a public
15213 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15214 -- following predicate is true, then this is not such a private
15215 -- operation and the subprogram simply inherits the name of the parent
15216 -- subprogram. Note the special check for the names of controlled
15217 -- operations, which are currently exempted from being inherited with
15218 -- a hidden name because they must be findable for generation of
15219 -- implicit run-time calls.
15220
15221 if not Is_Hidden (Parent_Subp)
15222 or else Is_Internal (Parent_Subp)
15223 or else Is_Private_Overriding
15224 or else Is_Internal_Name (Chars (Parent_Subp))
15225 or else (Is_Controlled (Parent_Type)
15226 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15227 Name_Finalize,
15228 Name_Initialize))
15229 then
15230 Set_Derived_Name;
15231
15232 -- An inherited dispatching equality will be overridden by an internally
15233 -- generated one, or by an explicit one, so preserve its name and thus
15234 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15235 -- private operation it may become invisible if the full view has
15236 -- progenitors, and the dispatch table will be malformed.
15237 -- We check that the type is limited to handle the anomalous declaration
15238 -- of Limited_Controlled, which is derived from a non-limited type, and
15239 -- which is handled specially elsewhere as well.
15240
15241 elsif Chars (Parent_Subp) = Name_Op_Eq
15242 and then Is_Dispatching_Operation (Parent_Subp)
15243 and then Etype (Parent_Subp) = Standard_Boolean
15244 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15245 and then
15246 Etype (First_Formal (Parent_Subp)) =
15247 Etype (Next_Formal (First_Formal (Parent_Subp)))
15248 then
15249 Set_Derived_Name;
15250
15251 -- If parent is hidden, this can be a regular derivation if the
15252 -- parent is immediately visible in a non-instantiating context,
15253 -- or if we are in the private part of an instance. This test
15254 -- should still be refined ???
15255
15256 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15257 -- operation as a non-visible operation in cases where the parent
15258 -- subprogram might not be visible now, but was visible within the
15259 -- original generic, so it would be wrong to make the inherited
15260 -- subprogram non-visible now. (Not clear if this test is fully
15261 -- correct; are there any cases where we should declare the inherited
15262 -- operation as not visible to avoid it being overridden, e.g., when
15263 -- the parent type is a generic actual with private primitives ???)
15264
15265 -- (they should be treated the same as other private inherited
15266 -- subprograms, but it's not clear how to do this cleanly). ???
15267
15268 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15269 and then Is_Immediately_Visible (Parent_Subp)
15270 and then not In_Instance)
15271 or else In_Instance_Not_Visible
15272 then
15273 Set_Derived_Name;
15274
15275 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15276 -- overrides an interface primitive because interface primitives
15277 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15278
15279 elsif Ada_Version >= Ada_2005
15280 and then Is_Dispatching_Operation (Parent_Subp)
15281 and then Present (Covered_Interface_Op (Parent_Subp))
15282 then
15283 Set_Derived_Name;
15284
15285 -- Otherwise, the type is inheriting a private operation, so enter it
15286 -- with a special name so it can't be overridden.
15287
15288 else
15289 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15290 end if;
15291
15292 Set_Parent (New_Subp, Parent (Derived_Type));
15293
15294 if Present (Actual_Subp) then
15295 Replace_Type (Actual_Subp, New_Subp);
15296 else
15297 Replace_Type (Parent_Subp, New_Subp);
15298 end if;
15299
15300 Conditional_Delay (New_Subp, Parent_Subp);
15301
15302 -- If we are creating a renaming for a primitive operation of an
15303 -- actual of a generic derived type, we must examine the signature
15304 -- of the actual primitive, not that of the generic formal, which for
15305 -- example may be an interface. However the name and initial value
15306 -- of the inherited operation are those of the formal primitive.
15307
15308 Formal := First_Formal (Parent_Subp);
15309
15310 if Present (Actual_Subp) then
15311 Formal_Of_Actual := First_Formal (Actual_Subp);
15312 else
15313 Formal_Of_Actual := Empty;
15314 end if;
15315
15316 while Present (Formal) loop
15317 New_Formal := New_Copy (Formal);
15318
15319 -- Normally we do not go copying parents, but in the case of
15320 -- formals, we need to link up to the declaration (which is the
15321 -- parameter specification), and it is fine to link up to the
15322 -- original formal's parameter specification in this case.
15323
15324 Set_Parent (New_Formal, Parent (Formal));
15325 Append_Entity (New_Formal, New_Subp);
15326
15327 if Present (Formal_Of_Actual) then
15328 Replace_Type (Formal_Of_Actual, New_Formal);
15329 Next_Formal (Formal_Of_Actual);
15330 else
15331 Replace_Type (Formal, New_Formal);
15332 end if;
15333
15334 Next_Formal (Formal);
15335 end loop;
15336
15337 -- If this derivation corresponds to a tagged generic actual, then
15338 -- primitive operations rename those of the actual. Otherwise the
15339 -- primitive operations rename those of the parent type, If the parent
15340 -- renames an intrinsic operator, so does the new subprogram. We except
15341 -- concatenation, which is always properly typed, and does not get
15342 -- expanded as other intrinsic operations.
15343
15344 if No (Actual_Subp) then
15345 if Is_Intrinsic_Subprogram (Parent_Subp) then
15346 Set_Is_Intrinsic_Subprogram (New_Subp);
15347
15348 if Present (Alias (Parent_Subp))
15349 and then Chars (Parent_Subp) /= Name_Op_Concat
15350 then
15351 Set_Alias (New_Subp, Alias (Parent_Subp));
15352 else
15353 Set_Alias (New_Subp, Parent_Subp);
15354 end if;
15355
15356 else
15357 Set_Alias (New_Subp, Parent_Subp);
15358 end if;
15359
15360 else
15361 Set_Alias (New_Subp, Actual_Subp);
15362 end if;
15363
15364 -- Derived subprograms of a tagged type must inherit the convention
15365 -- of the parent subprogram (a requirement of AI-117). Derived
15366 -- subprograms of untagged types simply get convention Ada by default.
15367
15368 -- If the derived type is a tagged generic formal type with unknown
15369 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15370
15371 -- However, if the type is derived from a generic formal, the further
15372 -- inherited subprogram has the convention of the non-generic ancestor.
15373 -- Otherwise there would be no way to override the operation.
15374 -- (This is subject to forthcoming ARG discussions).
15375
15376 if Is_Tagged_Type (Derived_Type) then
15377 if Is_Generic_Type (Derived_Type)
15378 and then Has_Unknown_Discriminants (Derived_Type)
15379 then
15380 Set_Convention (New_Subp, Convention_Intrinsic);
15381
15382 else
15383 if Is_Generic_Type (Parent_Type)
15384 and then Has_Unknown_Discriminants (Parent_Type)
15385 then
15386 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15387 else
15388 Set_Convention (New_Subp, Convention (Parent_Subp));
15389 end if;
15390 end if;
15391 end if;
15392
15393 -- Predefined controlled operations retain their name even if the parent
15394 -- is hidden (see above), but they are not primitive operations if the
15395 -- ancestor is not visible, for example if the parent is a private
15396 -- extension completed with a controlled extension. Note that a full
15397 -- type that is controlled can break privacy: the flag Is_Controlled is
15398 -- set on both views of the type.
15399
15400 if Is_Controlled (Parent_Type)
15401 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15402 Name_Adjust,
15403 Name_Finalize)
15404 and then Is_Hidden (Parent_Subp)
15405 and then not Is_Visibly_Controlled (Parent_Type)
15406 then
15407 Set_Is_Hidden (New_Subp);
15408 end if;
15409
15410 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15411 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15412
15413 if Ekind (Parent_Subp) = E_Procedure then
15414 Set_Is_Valued_Procedure
15415 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15416 else
15417 Set_Has_Controlling_Result
15418 (New_Subp, Has_Controlling_Result (Parent_Subp));
15419 end if;
15420
15421 -- No_Return must be inherited properly. If this is overridden in the
15422 -- case of a dispatching operation, then a check is made in Sem_Disp
15423 -- that the overriding operation is also No_Return (no such check is
15424 -- required for the case of non-dispatching operation.
15425
15426 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15427
15428 -- A derived function with a controlling result is abstract. If the
15429 -- Derived_Type is a nonabstract formal generic derived type, then
15430 -- inherited operations are not abstract: the required check is done at
15431 -- instantiation time. If the derivation is for a generic actual, the
15432 -- function is not abstract unless the actual is.
15433
15434 if Is_Generic_Type (Derived_Type)
15435 and then not Is_Abstract_Type (Derived_Type)
15436 then
15437 null;
15438
15439 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15440 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15441
15442 -- A subprogram subject to pragma Extensions_Visible with value False
15443 -- requires overriding if the subprogram has at least one controlling
15444 -- OUT parameter (SPARK RM 6.1.7(6)).
15445
15446 elsif Ada_Version >= Ada_2005
15447 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15448 or else (Is_Tagged_Type (Derived_Type)
15449 and then Etype (New_Subp) = Derived_Type
15450 and then not Is_Null_Extension (Derived_Type))
15451 or else (Is_Tagged_Type (Derived_Type)
15452 and then Ekind (Etype (New_Subp)) =
15453 E_Anonymous_Access_Type
15454 and then Designated_Type (Etype (New_Subp)) =
15455 Derived_Type
15456 and then not Is_Null_Extension (Derived_Type))
15457 or else (Comes_From_Source (Alias (New_Subp))
15458 and then Is_EVF_Procedure (Alias (New_Subp))))
15459 and then No (Actual_Subp)
15460 then
15461 if not Is_Tagged_Type (Derived_Type)
15462 or else Is_Abstract_Type (Derived_Type)
15463 or else Is_Abstract_Subprogram (Alias (New_Subp))
15464 then
15465 Set_Is_Abstract_Subprogram (New_Subp);
15466 else
15467 Set_Requires_Overriding (New_Subp);
15468 end if;
15469
15470 elsif Ada_Version < Ada_2005
15471 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15472 or else (Is_Tagged_Type (Derived_Type)
15473 and then Etype (New_Subp) = Derived_Type
15474 and then No (Actual_Subp)))
15475 then
15476 Set_Is_Abstract_Subprogram (New_Subp);
15477
15478 -- AI05-0097 : an inherited operation that dispatches on result is
15479 -- abstract if the derived type is abstract, even if the parent type
15480 -- is concrete and the derived type is a null extension.
15481
15482 elsif Has_Controlling_Result (Alias (New_Subp))
15483 and then Is_Abstract_Type (Etype (New_Subp))
15484 then
15485 Set_Is_Abstract_Subprogram (New_Subp);
15486
15487 -- Finally, if the parent type is abstract we must verify that all
15488 -- inherited operations are either non-abstract or overridden, or that
15489 -- the derived type itself is abstract (this check is performed at the
15490 -- end of a package declaration, in Check_Abstract_Overriding). A
15491 -- private overriding in the parent type will not be visible in the
15492 -- derivation if we are not in an inner package or in a child unit of
15493 -- the parent type, in which case the abstractness of the inherited
15494 -- operation is carried to the new subprogram.
15495
15496 elsif Is_Abstract_Type (Parent_Type)
15497 and then not In_Open_Scopes (Scope (Parent_Type))
15498 and then Is_Private_Overriding
15499 and then Is_Abstract_Subprogram (Visible_Subp)
15500 then
15501 if No (Actual_Subp) then
15502 Set_Alias (New_Subp, Visible_Subp);
15503 Set_Is_Abstract_Subprogram (New_Subp, True);
15504
15505 else
15506 -- If this is a derivation for an instance of a formal derived
15507 -- type, abstractness comes from the primitive operation of the
15508 -- actual, not from the operation inherited from the ancestor.
15509
15510 Set_Is_Abstract_Subprogram
15511 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15512 end if;
15513 end if;
15514
15515 New_Overloaded_Entity (New_Subp, Derived_Type);
15516
15517 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15518 -- preconditions and the derived type is abstract, the derived operation
15519 -- is abstract as well if parent subprogram is not abstract or null.
15520
15521 if Is_Abstract_Type (Derived_Type)
15522 and then Has_Non_Trivial_Precondition (Parent_Subp)
15523 and then Present (Interfaces (Derived_Type))
15524 then
15525
15526 -- Add useful attributes of subprogram before the freeze point,
15527 -- in case freezing is delayed or there are previous errors.
15528
15529 Set_Is_Dispatching_Operation (New_Subp);
15530
15531 declare
15532 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15533
15534 begin
15535 if Present (Iface_Prim)
15536 and then Has_Non_Trivial_Precondition (Iface_Prim)
15537 then
15538 Set_Is_Abstract_Subprogram (New_Subp);
15539 end if;
15540 end;
15541 end if;
15542
15543 -- Check for case of a derived subprogram for the instantiation of a
15544 -- formal derived tagged type, if so mark the subprogram as dispatching
15545 -- and inherit the dispatching attributes of the actual subprogram. The
15546 -- derived subprogram is effectively renaming of the actual subprogram,
15547 -- so it needs to have the same attributes as the actual.
15548
15549 if Present (Actual_Subp)
15550 and then Is_Dispatching_Operation (Actual_Subp)
15551 then
15552 Set_Is_Dispatching_Operation (New_Subp);
15553
15554 if Present (DTC_Entity (Actual_Subp)) then
15555 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15556 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15557 end if;
15558 end if;
15559
15560 -- Indicate that a derived subprogram does not require a body and that
15561 -- it does not require processing of default expressions.
15562
15563 Set_Has_Completion (New_Subp);
15564 Set_Default_Expressions_Processed (New_Subp);
15565
15566 if Ekind (New_Subp) = E_Function then
15567 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15568 end if;
15569 end Derive_Subprogram;
15570
15571 ------------------------
15572 -- Derive_Subprograms --
15573 ------------------------
15574
15575 procedure Derive_Subprograms
15576 (Parent_Type : Entity_Id;
15577 Derived_Type : Entity_Id;
15578 Generic_Actual : Entity_Id := Empty)
15579 is
15580 Op_List : constant Elist_Id :=
15581 Collect_Primitive_Operations (Parent_Type);
15582
15583 function Check_Derived_Type return Boolean;
15584 -- Check that all the entities derived from Parent_Type are found in
15585 -- the list of primitives of Derived_Type exactly in the same order.
15586
15587 procedure Derive_Interface_Subprogram
15588 (New_Subp : out Entity_Id;
15589 Subp : Entity_Id;
15590 Actual_Subp : Entity_Id);
15591 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15592 -- (which is an interface primitive). If Generic_Actual is present then
15593 -- Actual_Subp is the actual subprogram corresponding with the generic
15594 -- subprogram Subp.
15595
15596 ------------------------
15597 -- Check_Derived_Type --
15598 ------------------------
15599
15600 function Check_Derived_Type return Boolean is
15601 E : Entity_Id;
15602 Elmt : Elmt_Id;
15603 List : Elist_Id;
15604 New_Subp : Entity_Id;
15605 Op_Elmt : Elmt_Id;
15606 Subp : Entity_Id;
15607
15608 begin
15609 -- Traverse list of entities in the current scope searching for
15610 -- an incomplete type whose full-view is derived type.
15611
15612 E := First_Entity (Scope (Derived_Type));
15613 while Present (E) and then E /= Derived_Type loop
15614 if Ekind (E) = E_Incomplete_Type
15615 and then Present (Full_View (E))
15616 and then Full_View (E) = Derived_Type
15617 then
15618 -- Disable this test if Derived_Type completes an incomplete
15619 -- type because in such case more primitives can be added
15620 -- later to the list of primitives of Derived_Type by routine
15621 -- Process_Incomplete_Dependents
15622
15623 return True;
15624 end if;
15625
15626 E := Next_Entity (E);
15627 end loop;
15628
15629 List := Collect_Primitive_Operations (Derived_Type);
15630 Elmt := First_Elmt (List);
15631
15632 Op_Elmt := First_Elmt (Op_List);
15633 while Present (Op_Elmt) loop
15634 Subp := Node (Op_Elmt);
15635 New_Subp := Node (Elmt);
15636
15637 -- At this early stage Derived_Type has no entities with attribute
15638 -- Interface_Alias. In addition, such primitives are always
15639 -- located at the end of the list of primitives of Parent_Type.
15640 -- Therefore, if found we can safely stop processing pending
15641 -- entities.
15642
15643 exit when Present (Interface_Alias (Subp));
15644
15645 -- Handle hidden entities
15646
15647 if not Is_Predefined_Dispatching_Operation (Subp)
15648 and then Is_Hidden (Subp)
15649 then
15650 if Present (New_Subp)
15651 and then Primitive_Names_Match (Subp, New_Subp)
15652 then
15653 Next_Elmt (Elmt);
15654 end if;
15655
15656 else
15657 if not Present (New_Subp)
15658 or else Ekind (Subp) /= Ekind (New_Subp)
15659 or else not Primitive_Names_Match (Subp, New_Subp)
15660 then
15661 return False;
15662 end if;
15663
15664 Next_Elmt (Elmt);
15665 end if;
15666
15667 Next_Elmt (Op_Elmt);
15668 end loop;
15669
15670 return True;
15671 end Check_Derived_Type;
15672
15673 ---------------------------------
15674 -- Derive_Interface_Subprogram --
15675 ---------------------------------
15676
15677 procedure Derive_Interface_Subprogram
15678 (New_Subp : out Entity_Id;
15679 Subp : Entity_Id;
15680 Actual_Subp : Entity_Id)
15681 is
15682 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15683 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15684
15685 begin
15686 pragma Assert (Is_Interface (Iface_Type));
15687
15688 Derive_Subprogram
15689 (New_Subp => New_Subp,
15690 Parent_Subp => Iface_Subp,
15691 Derived_Type => Derived_Type,
15692 Parent_Type => Iface_Type,
15693 Actual_Subp => Actual_Subp);
15694
15695 -- Given that this new interface entity corresponds with a primitive
15696 -- of the parent that was not overridden we must leave it associated
15697 -- with its parent primitive to ensure that it will share the same
15698 -- dispatch table slot when overridden. We must set the Alias to Subp
15699 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15700 -- (in case we inherited Subp from Iface_Type via a nonabstract
15701 -- generic formal type).
15702
15703 if No (Actual_Subp) then
15704 Set_Alias (New_Subp, Subp);
15705
15706 declare
15707 T : Entity_Id := Find_Dispatching_Type (Subp);
15708 begin
15709 while Etype (T) /= T loop
15710 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15711 Set_Is_Abstract_Subprogram (New_Subp, False);
15712 exit;
15713 end if;
15714
15715 T := Etype (T);
15716 end loop;
15717 end;
15718
15719 -- For instantiations this is not needed since the previous call to
15720 -- Derive_Subprogram leaves the entity well decorated.
15721
15722 else
15723 pragma Assert (Alias (New_Subp) = Actual_Subp);
15724 null;
15725 end if;
15726 end Derive_Interface_Subprogram;
15727
15728 -- Local variables
15729
15730 Alias_Subp : Entity_Id;
15731 Act_List : Elist_Id;
15732 Act_Elmt : Elmt_Id;
15733 Act_Subp : Entity_Id := Empty;
15734 Elmt : Elmt_Id;
15735 Need_Search : Boolean := False;
15736 New_Subp : Entity_Id := Empty;
15737 Parent_Base : Entity_Id;
15738 Subp : Entity_Id;
15739
15740 -- Start of processing for Derive_Subprograms
15741
15742 begin
15743 if Ekind (Parent_Type) = E_Record_Type_With_Private
15744 and then Has_Discriminants (Parent_Type)
15745 and then Present (Full_View (Parent_Type))
15746 then
15747 Parent_Base := Full_View (Parent_Type);
15748 else
15749 Parent_Base := Parent_Type;
15750 end if;
15751
15752 if Present (Generic_Actual) then
15753 Act_List := Collect_Primitive_Operations (Generic_Actual);
15754 Act_Elmt := First_Elmt (Act_List);
15755 else
15756 Act_List := No_Elist;
15757 Act_Elmt := No_Elmt;
15758 end if;
15759
15760 -- Derive primitives inherited from the parent. Note that if the generic
15761 -- actual is present, this is not really a type derivation, it is a
15762 -- completion within an instance.
15763
15764 -- Case 1: Derived_Type does not implement interfaces
15765
15766 if not Is_Tagged_Type (Derived_Type)
15767 or else (not Has_Interfaces (Derived_Type)
15768 and then not (Present (Generic_Actual)
15769 and then Has_Interfaces (Generic_Actual)))
15770 then
15771 Elmt := First_Elmt (Op_List);
15772 while Present (Elmt) loop
15773 Subp := Node (Elmt);
15774
15775 -- Literals are derived earlier in the process of building the
15776 -- derived type, and are skipped here.
15777
15778 if Ekind (Subp) = E_Enumeration_Literal then
15779 null;
15780
15781 -- The actual is a direct descendant and the common primitive
15782 -- operations appear in the same order.
15783
15784 -- If the generic parent type is present, the derived type is an
15785 -- instance of a formal derived type, and within the instance its
15786 -- operations are those of the actual. We derive from the formal
15787 -- type but make the inherited operations aliases of the
15788 -- corresponding operations of the actual.
15789
15790 else
15791 pragma Assert (No (Node (Act_Elmt))
15792 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15793 and then
15794 Type_Conformant
15795 (Subp, Node (Act_Elmt),
15796 Skip_Controlling_Formals => True)));
15797
15798 Derive_Subprogram
15799 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15800
15801 if Present (Act_Elmt) then
15802 Next_Elmt (Act_Elmt);
15803 end if;
15804 end if;
15805
15806 Next_Elmt (Elmt);
15807 end loop;
15808
15809 -- Case 2: Derived_Type implements interfaces
15810
15811 else
15812 -- If the parent type has no predefined primitives we remove
15813 -- predefined primitives from the list of primitives of generic
15814 -- actual to simplify the complexity of this algorithm.
15815
15816 if Present (Generic_Actual) then
15817 declare
15818 Has_Predefined_Primitives : Boolean := False;
15819
15820 begin
15821 -- Check if the parent type has predefined primitives
15822
15823 Elmt := First_Elmt (Op_List);
15824 while Present (Elmt) loop
15825 Subp := Node (Elmt);
15826
15827 if Is_Predefined_Dispatching_Operation (Subp)
15828 and then not Comes_From_Source (Ultimate_Alias (Subp))
15829 then
15830 Has_Predefined_Primitives := True;
15831 exit;
15832 end if;
15833
15834 Next_Elmt (Elmt);
15835 end loop;
15836
15837 -- Remove predefined primitives of Generic_Actual. We must use
15838 -- an auxiliary list because in case of tagged types the value
15839 -- returned by Collect_Primitive_Operations is the value stored
15840 -- in its Primitive_Operations attribute (and we don't want to
15841 -- modify its current contents).
15842
15843 if not Has_Predefined_Primitives then
15844 declare
15845 Aux_List : constant Elist_Id := New_Elmt_List;
15846
15847 begin
15848 Elmt := First_Elmt (Act_List);
15849 while Present (Elmt) loop
15850 Subp := Node (Elmt);
15851
15852 if not Is_Predefined_Dispatching_Operation (Subp)
15853 or else Comes_From_Source (Subp)
15854 then
15855 Append_Elmt (Subp, Aux_List);
15856 end if;
15857
15858 Next_Elmt (Elmt);
15859 end loop;
15860
15861 Act_List := Aux_List;
15862 end;
15863 end if;
15864
15865 Act_Elmt := First_Elmt (Act_List);
15866 Act_Subp := Node (Act_Elmt);
15867 end;
15868 end if;
15869
15870 -- Stage 1: If the generic actual is not present we derive the
15871 -- primitives inherited from the parent type. If the generic parent
15872 -- type is present, the derived type is an instance of a formal
15873 -- derived type, and within the instance its operations are those of
15874 -- the actual. We derive from the formal type but make the inherited
15875 -- operations aliases of the corresponding operations of the actual.
15876
15877 Elmt := First_Elmt (Op_List);
15878 while Present (Elmt) loop
15879 Subp := Node (Elmt);
15880 Alias_Subp := Ultimate_Alias (Subp);
15881
15882 -- Do not derive internal entities of the parent that link
15883 -- interface primitives with their covering primitive. These
15884 -- entities will be added to this type when frozen.
15885
15886 if Present (Interface_Alias (Subp)) then
15887 goto Continue;
15888 end if;
15889
15890 -- If the generic actual is present find the corresponding
15891 -- operation in the generic actual. If the parent type is a
15892 -- direct ancestor of the derived type then, even if it is an
15893 -- interface, the operations are inherited from the primary
15894 -- dispatch table and are in the proper order. If we detect here
15895 -- that primitives are not in the same order we traverse the list
15896 -- of primitive operations of the actual to find the one that
15897 -- implements the interface primitive.
15898
15899 if Need_Search
15900 or else
15901 (Present (Generic_Actual)
15902 and then Present (Act_Subp)
15903 and then not
15904 (Primitive_Names_Match (Subp, Act_Subp)
15905 and then
15906 Type_Conformant (Subp, Act_Subp,
15907 Skip_Controlling_Formals => True)))
15908 then
15909 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15910 Use_Full_View => True));
15911
15912 -- Remember that we need searching for all pending primitives
15913
15914 Need_Search := True;
15915
15916 -- Handle entities associated with interface primitives
15917
15918 if Present (Alias_Subp)
15919 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15920 and then not Is_Predefined_Dispatching_Operation (Subp)
15921 then
15922 -- Search for the primitive in the homonym chain
15923
15924 Act_Subp :=
15925 Find_Primitive_Covering_Interface
15926 (Tagged_Type => Generic_Actual,
15927 Iface_Prim => Alias_Subp);
15928
15929 -- Previous search may not locate primitives covering
15930 -- interfaces defined in generics units or instantiations.
15931 -- (it fails if the covering primitive has formals whose
15932 -- type is also defined in generics or instantiations).
15933 -- In such case we search in the list of primitives of the
15934 -- generic actual for the internal entity that links the
15935 -- interface primitive and the covering primitive.
15936
15937 if No (Act_Subp)
15938 and then Is_Generic_Type (Parent_Type)
15939 then
15940 -- This code has been designed to handle only generic
15941 -- formals that implement interfaces that are defined
15942 -- in a generic unit or instantiation. If this code is
15943 -- needed for other cases we must review it because
15944 -- (given that it relies on Original_Location to locate
15945 -- the primitive of Generic_Actual that covers the
15946 -- interface) it could leave linked through attribute
15947 -- Alias entities of unrelated instantiations).
15948
15949 pragma Assert
15950 (Is_Generic_Unit
15951 (Scope (Find_Dispatching_Type (Alias_Subp)))
15952 or else
15953 Instantiation_Depth
15954 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15955
15956 declare
15957 Iface_Prim_Loc : constant Source_Ptr :=
15958 Original_Location (Sloc (Alias_Subp));
15959
15960 Elmt : Elmt_Id;
15961 Prim : Entity_Id;
15962
15963 begin
15964 Elmt :=
15965 First_Elmt (Primitive_Operations (Generic_Actual));
15966
15967 Search : while Present (Elmt) loop
15968 Prim := Node (Elmt);
15969
15970 if Present (Interface_Alias (Prim))
15971 and then Original_Location
15972 (Sloc (Interface_Alias (Prim))) =
15973 Iface_Prim_Loc
15974 then
15975 Act_Subp := Alias (Prim);
15976 exit Search;
15977 end if;
15978
15979 Next_Elmt (Elmt);
15980 end loop Search;
15981 end;
15982 end if;
15983
15984 pragma Assert (Present (Act_Subp)
15985 or else Is_Abstract_Type (Generic_Actual)
15986 or else Serious_Errors_Detected > 0);
15987
15988 -- Handle predefined primitives plus the rest of user-defined
15989 -- primitives
15990
15991 else
15992 Act_Elmt := First_Elmt (Act_List);
15993 while Present (Act_Elmt) loop
15994 Act_Subp := Node (Act_Elmt);
15995
15996 exit when Primitive_Names_Match (Subp, Act_Subp)
15997 and then Type_Conformant
15998 (Subp, Act_Subp,
15999 Skip_Controlling_Formals => True)
16000 and then No (Interface_Alias (Act_Subp));
16001
16002 Next_Elmt (Act_Elmt);
16003 end loop;
16004
16005 if No (Act_Elmt) then
16006 Act_Subp := Empty;
16007 end if;
16008 end if;
16009 end if;
16010
16011 -- Case 1: If the parent is a limited interface then it has the
16012 -- predefined primitives of synchronized interfaces. However, the
16013 -- actual type may be a non-limited type and hence it does not
16014 -- have such primitives.
16015
16016 if Present (Generic_Actual)
16017 and then not Present (Act_Subp)
16018 and then Is_Limited_Interface (Parent_Base)
16019 and then Is_Predefined_Interface_Primitive (Subp)
16020 then
16021 null;
16022
16023 -- Case 2: Inherit entities associated with interfaces that were
16024 -- not covered by the parent type. We exclude here null interface
16025 -- primitives because they do not need special management.
16026
16027 -- We also exclude interface operations that are renamings. If the
16028 -- subprogram is an explicit renaming of an interface primitive,
16029 -- it is a regular primitive operation, and the presence of its
16030 -- alias is not relevant: it has to be derived like any other
16031 -- primitive.
16032
16033 elsif Present (Alias (Subp))
16034 and then Nkind (Unit_Declaration_Node (Subp)) /=
16035 N_Subprogram_Renaming_Declaration
16036 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16037 and then not
16038 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16039 and then Null_Present (Parent (Alias_Subp)))
16040 then
16041 -- If this is an abstract private type then we transfer the
16042 -- derivation of the interface primitive from the partial view
16043 -- to the full view. This is safe because all the interfaces
16044 -- must be visible in the partial view. Done to avoid adding
16045 -- a new interface derivation to the private part of the
16046 -- enclosing package; otherwise this new derivation would be
16047 -- decorated as hidden when the analysis of the enclosing
16048 -- package completes.
16049
16050 if Is_Abstract_Type (Derived_Type)
16051 and then In_Private_Part (Current_Scope)
16052 and then Has_Private_Declaration (Derived_Type)
16053 then
16054 declare
16055 Partial_View : Entity_Id;
16056 Elmt : Elmt_Id;
16057 Ent : Entity_Id;
16058
16059 begin
16060 Partial_View := First_Entity (Current_Scope);
16061 loop
16062 exit when No (Partial_View)
16063 or else (Has_Private_Declaration (Partial_View)
16064 and then
16065 Full_View (Partial_View) = Derived_Type);
16066
16067 Next_Entity (Partial_View);
16068 end loop;
16069
16070 -- If the partial view was not found then the source code
16071 -- has errors and the derivation is not needed.
16072
16073 if Present (Partial_View) then
16074 Elmt :=
16075 First_Elmt (Primitive_Operations (Partial_View));
16076 while Present (Elmt) loop
16077 Ent := Node (Elmt);
16078
16079 if Present (Alias (Ent))
16080 and then Ultimate_Alias (Ent) = Alias (Subp)
16081 then
16082 Append_Elmt
16083 (Ent, Primitive_Operations (Derived_Type));
16084 exit;
16085 end if;
16086
16087 Next_Elmt (Elmt);
16088 end loop;
16089
16090 -- If the interface primitive was not found in the
16091 -- partial view then this interface primitive was
16092 -- overridden. We add a derivation to activate in
16093 -- Derive_Progenitor_Subprograms the machinery to
16094 -- search for it.
16095
16096 if No (Elmt) then
16097 Derive_Interface_Subprogram
16098 (New_Subp => New_Subp,
16099 Subp => Subp,
16100 Actual_Subp => Act_Subp);
16101 end if;
16102 end if;
16103 end;
16104 else
16105 Derive_Interface_Subprogram
16106 (New_Subp => New_Subp,
16107 Subp => Subp,
16108 Actual_Subp => Act_Subp);
16109 end if;
16110
16111 -- Case 3: Common derivation
16112
16113 else
16114 Derive_Subprogram
16115 (New_Subp => New_Subp,
16116 Parent_Subp => Subp,
16117 Derived_Type => Derived_Type,
16118 Parent_Type => Parent_Base,
16119 Actual_Subp => Act_Subp);
16120 end if;
16121
16122 -- No need to update Act_Elm if we must search for the
16123 -- corresponding operation in the generic actual
16124
16125 if not Need_Search
16126 and then Present (Act_Elmt)
16127 then
16128 Next_Elmt (Act_Elmt);
16129 Act_Subp := Node (Act_Elmt);
16130 end if;
16131
16132 <<Continue>>
16133 Next_Elmt (Elmt);
16134 end loop;
16135
16136 -- Inherit additional operations from progenitors. If the derived
16137 -- type is a generic actual, there are not new primitive operations
16138 -- for the type because it has those of the actual, and therefore
16139 -- nothing needs to be done. The renamings generated above are not
16140 -- primitive operations, and their purpose is simply to make the
16141 -- proper operations visible within an instantiation.
16142
16143 if No (Generic_Actual) then
16144 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16145 end if;
16146 end if;
16147
16148 -- Final check: Direct descendants must have their primitives in the
16149 -- same order. We exclude from this test untagged types and instances
16150 -- of formal derived types. We skip this test if we have already
16151 -- reported serious errors in the sources.
16152
16153 pragma Assert (not Is_Tagged_Type (Derived_Type)
16154 or else Present (Generic_Actual)
16155 or else Serious_Errors_Detected > 0
16156 or else Check_Derived_Type);
16157 end Derive_Subprograms;
16158
16159 --------------------------------
16160 -- Derived_Standard_Character --
16161 --------------------------------
16162
16163 procedure Derived_Standard_Character
16164 (N : Node_Id;
16165 Parent_Type : Entity_Id;
16166 Derived_Type : Entity_Id)
16167 is
16168 Loc : constant Source_Ptr := Sloc (N);
16169 Def : constant Node_Id := Type_Definition (N);
16170 Indic : constant Node_Id := Subtype_Indication (Def);
16171 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16172 Implicit_Base : constant Entity_Id :=
16173 Create_Itype
16174 (E_Enumeration_Type, N, Derived_Type, 'B');
16175
16176 Lo : Node_Id;
16177 Hi : Node_Id;
16178
16179 begin
16180 Discard_Node (Process_Subtype (Indic, N));
16181
16182 Set_Etype (Implicit_Base, Parent_Base);
16183 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16184 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16185
16186 Set_Is_Character_Type (Implicit_Base, True);
16187 Set_Has_Delayed_Freeze (Implicit_Base);
16188
16189 -- The bounds of the implicit base are the bounds of the parent base.
16190 -- Note that their type is the parent base.
16191
16192 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16193 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16194
16195 Set_Scalar_Range (Implicit_Base,
16196 Make_Range (Loc,
16197 Low_Bound => Lo,
16198 High_Bound => Hi));
16199
16200 Conditional_Delay (Derived_Type, Parent_Type);
16201
16202 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16203 Set_Etype (Derived_Type, Implicit_Base);
16204 Set_Size_Info (Derived_Type, Parent_Type);
16205
16206 if Unknown_RM_Size (Derived_Type) then
16207 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16208 end if;
16209
16210 Set_Is_Character_Type (Derived_Type, True);
16211
16212 if Nkind (Indic) /= N_Subtype_Indication then
16213
16214 -- If no explicit constraint, the bounds are those
16215 -- of the parent type.
16216
16217 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16218 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16219 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16220 end if;
16221
16222 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16223
16224 -- Because the implicit base is used in the conversion of the bounds, we
16225 -- have to freeze it now. This is similar to what is done for numeric
16226 -- types, and it equally suspicious, but otherwise a non-static bound
16227 -- will have a reference to an unfrozen type, which is rejected by Gigi
16228 -- (???). This requires specific care for definition of stream
16229 -- attributes. For details, see comments at the end of
16230 -- Build_Derived_Numeric_Type.
16231
16232 Freeze_Before (N, Implicit_Base);
16233 end Derived_Standard_Character;
16234
16235 ------------------------------
16236 -- Derived_Type_Declaration --
16237 ------------------------------
16238
16239 procedure Derived_Type_Declaration
16240 (T : Entity_Id;
16241 N : Node_Id;
16242 Is_Completion : Boolean)
16243 is
16244 Parent_Type : Entity_Id;
16245
16246 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16247 -- Check whether the parent type is a generic formal, or derives
16248 -- directly or indirectly from one.
16249
16250 ------------------------
16251 -- Comes_From_Generic --
16252 ------------------------
16253
16254 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16255 begin
16256 if Is_Generic_Type (Typ) then
16257 return True;
16258
16259 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16260 return True;
16261
16262 elsif Is_Private_Type (Typ)
16263 and then Present (Full_View (Typ))
16264 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16265 then
16266 return True;
16267
16268 elsif Is_Generic_Actual_Type (Typ) then
16269 return True;
16270
16271 else
16272 return False;
16273 end if;
16274 end Comes_From_Generic;
16275
16276 -- Local variables
16277
16278 Def : constant Node_Id := Type_Definition (N);
16279 Iface_Def : Node_Id;
16280 Indic : constant Node_Id := Subtype_Indication (Def);
16281 Extension : constant Node_Id := Record_Extension_Part (Def);
16282 Parent_Node : Node_Id;
16283 Taggd : Boolean;
16284
16285 -- Start of processing for Derived_Type_Declaration
16286
16287 begin
16288 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16289
16290 -- Ada 2005 (AI-251): In case of interface derivation check that the
16291 -- parent is also an interface.
16292
16293 if Interface_Present (Def) then
16294 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16295
16296 if not Is_Interface (Parent_Type) then
16297 Diagnose_Interface (Indic, Parent_Type);
16298
16299 else
16300 Parent_Node := Parent (Base_Type (Parent_Type));
16301 Iface_Def := Type_Definition (Parent_Node);
16302
16303 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16304 -- other limited interfaces.
16305
16306 if Limited_Present (Def) then
16307 if Limited_Present (Iface_Def) then
16308 null;
16309
16310 elsif Protected_Present (Iface_Def) then
16311 Error_Msg_NE
16312 ("descendant of & must be declared as a protected "
16313 & "interface", N, Parent_Type);
16314
16315 elsif Synchronized_Present (Iface_Def) then
16316 Error_Msg_NE
16317 ("descendant of & must be declared as a synchronized "
16318 & "interface", N, Parent_Type);
16319
16320 elsif Task_Present (Iface_Def) then
16321 Error_Msg_NE
16322 ("descendant of & must be declared as a task interface",
16323 N, Parent_Type);
16324
16325 else
16326 Error_Msg_N
16327 ("(Ada 2005) limited interface cannot inherit from "
16328 & "non-limited interface", Indic);
16329 end if;
16330
16331 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16332 -- from non-limited or limited interfaces.
16333
16334 elsif not Protected_Present (Def)
16335 and then not Synchronized_Present (Def)
16336 and then not Task_Present (Def)
16337 then
16338 if Limited_Present (Iface_Def) then
16339 null;
16340
16341 elsif Protected_Present (Iface_Def) then
16342 Error_Msg_NE
16343 ("descendant of & must be declared as a protected "
16344 & "interface", N, Parent_Type);
16345
16346 elsif Synchronized_Present (Iface_Def) then
16347 Error_Msg_NE
16348 ("descendant of & must be declared as a synchronized "
16349 & "interface", N, Parent_Type);
16350
16351 elsif Task_Present (Iface_Def) then
16352 Error_Msg_NE
16353 ("descendant of & must be declared as a task interface",
16354 N, Parent_Type);
16355 else
16356 null;
16357 end if;
16358 end if;
16359 end if;
16360 end if;
16361
16362 if Is_Tagged_Type (Parent_Type)
16363 and then Is_Concurrent_Type (Parent_Type)
16364 and then not Is_Interface (Parent_Type)
16365 then
16366 Error_Msg_N
16367 ("parent type of a record extension cannot be a synchronized "
16368 & "tagged type (RM 3.9.1 (3/1))", N);
16369 Set_Etype (T, Any_Type);
16370 return;
16371 end if;
16372
16373 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16374 -- interfaces
16375
16376 if Is_Tagged_Type (Parent_Type)
16377 and then Is_Non_Empty_List (Interface_List (Def))
16378 then
16379 declare
16380 Intf : Node_Id;
16381 T : Entity_Id;
16382
16383 begin
16384 Intf := First (Interface_List (Def));
16385 while Present (Intf) loop
16386 T := Find_Type_Of_Subtype_Indic (Intf);
16387
16388 if not Is_Interface (T) then
16389 Diagnose_Interface (Intf, T);
16390
16391 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16392 -- a limited type from having a nonlimited progenitor.
16393
16394 elsif (Limited_Present (Def)
16395 or else (not Is_Interface (Parent_Type)
16396 and then Is_Limited_Type (Parent_Type)))
16397 and then not Is_Limited_Interface (T)
16398 then
16399 Error_Msg_NE
16400 ("progenitor interface& of limited type must be limited",
16401 N, T);
16402 end if;
16403
16404 Next (Intf);
16405 end loop;
16406 end;
16407 end if;
16408
16409 if Parent_Type = Any_Type
16410 or else Etype (Parent_Type) = Any_Type
16411 or else (Is_Class_Wide_Type (Parent_Type)
16412 and then Etype (Parent_Type) = T)
16413 then
16414 -- If Parent_Type is undefined or illegal, make new type into a
16415 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16416 -- errors. If this is a self-definition, emit error now.
16417
16418 if T = Parent_Type or else T = Etype (Parent_Type) then
16419 Error_Msg_N ("type cannot be used in its own definition", Indic);
16420 end if;
16421
16422 Set_Ekind (T, Ekind (Parent_Type));
16423 Set_Etype (T, Any_Type);
16424 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16425
16426 if Is_Tagged_Type (T)
16427 and then Is_Record_Type (T)
16428 then
16429 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16430 end if;
16431
16432 return;
16433 end if;
16434
16435 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16436 -- an interface is special because the list of interfaces in the full
16437 -- view can be given in any order. For example:
16438
16439 -- type A is interface;
16440 -- type B is interface and A;
16441 -- type D is new B with private;
16442 -- private
16443 -- type D is new A and B with null record; -- 1 --
16444
16445 -- In this case we perform the following transformation of -1-:
16446
16447 -- type D is new B and A with null record;
16448
16449 -- If the parent of the full-view covers the parent of the partial-view
16450 -- we have two possible cases:
16451
16452 -- 1) They have the same parent
16453 -- 2) The parent of the full-view implements some further interfaces
16454
16455 -- In both cases we do not need to perform the transformation. In the
16456 -- first case the source program is correct and the transformation is
16457 -- not needed; in the second case the source program does not fulfill
16458 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16459 -- later.
16460
16461 -- This transformation not only simplifies the rest of the analysis of
16462 -- this type declaration but also simplifies the correct generation of
16463 -- the object layout to the expander.
16464
16465 if In_Private_Part (Current_Scope)
16466 and then Is_Interface (Parent_Type)
16467 then
16468 declare
16469 Iface : Node_Id;
16470 Partial_View : Entity_Id;
16471 Partial_View_Parent : Entity_Id;
16472 New_Iface : Node_Id;
16473
16474 begin
16475 -- Look for the associated private type declaration
16476
16477 Partial_View := First_Entity (Current_Scope);
16478 loop
16479 exit when No (Partial_View)
16480 or else (Has_Private_Declaration (Partial_View)
16481 and then Full_View (Partial_View) = T);
16482
16483 Next_Entity (Partial_View);
16484 end loop;
16485
16486 -- If the partial view was not found then the source code has
16487 -- errors and the transformation is not needed.
16488
16489 if Present (Partial_View) then
16490 Partial_View_Parent := Etype (Partial_View);
16491
16492 -- If the parent of the full-view covers the parent of the
16493 -- partial-view we have nothing else to do.
16494
16495 if Interface_Present_In_Ancestor
16496 (Parent_Type, Partial_View_Parent)
16497 then
16498 null;
16499
16500 -- Traverse the list of interfaces of the full-view to look
16501 -- for the parent of the partial-view and perform the tree
16502 -- transformation.
16503
16504 else
16505 Iface := First (Interface_List (Def));
16506 while Present (Iface) loop
16507 if Etype (Iface) = Etype (Partial_View) then
16508 Rewrite (Subtype_Indication (Def),
16509 New_Copy (Subtype_Indication
16510 (Parent (Partial_View))));
16511
16512 New_Iface :=
16513 Make_Identifier (Sloc (N), Chars (Parent_Type));
16514 Append (New_Iface, Interface_List (Def));
16515
16516 -- Analyze the transformed code
16517
16518 Derived_Type_Declaration (T, N, Is_Completion);
16519 return;
16520 end if;
16521
16522 Next (Iface);
16523 end loop;
16524 end if;
16525 end if;
16526 end;
16527 end if;
16528
16529 -- Only composite types other than array types are allowed to have
16530 -- discriminants.
16531
16532 if Present (Discriminant_Specifications (N)) then
16533 if (Is_Elementary_Type (Parent_Type)
16534 or else
16535 Is_Array_Type (Parent_Type))
16536 and then not Error_Posted (N)
16537 then
16538 Error_Msg_N
16539 ("elementary or array type cannot have discriminants",
16540 Defining_Identifier (First (Discriminant_Specifications (N))));
16541 Set_Has_Discriminants (T, False);
16542
16543 -- The type is allowed to have discriminants
16544
16545 else
16546 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16547 end if;
16548 end if;
16549
16550 -- In Ada 83, a derived type defined in a package specification cannot
16551 -- be used for further derivation until the end of its visible part.
16552 -- Note that derivation in the private part of the package is allowed.
16553
16554 if Ada_Version = Ada_83
16555 and then Is_Derived_Type (Parent_Type)
16556 and then In_Visible_Part (Scope (Parent_Type))
16557 then
16558 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16559 Error_Msg_N
16560 ("(Ada 83): premature use of type for derivation", Indic);
16561 end if;
16562 end if;
16563
16564 -- Check for early use of incomplete or private type
16565
16566 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16567 Error_Msg_N ("premature derivation of incomplete type", Indic);
16568 return;
16569
16570 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16571 and then not Comes_From_Generic (Parent_Type))
16572 or else Has_Private_Component (Parent_Type)
16573 then
16574 -- The ancestor type of a formal type can be incomplete, in which
16575 -- case only the operations of the partial view are available in the
16576 -- generic. Subsequent checks may be required when the full view is
16577 -- analyzed to verify that a derivation from a tagged type has an
16578 -- extension.
16579
16580 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16581 null;
16582
16583 elsif No (Underlying_Type (Parent_Type))
16584 or else Has_Private_Component (Parent_Type)
16585 then
16586 Error_Msg_N
16587 ("premature derivation of derived or private type", Indic);
16588
16589 -- Flag the type itself as being in error, this prevents some
16590 -- nasty problems with subsequent uses of the malformed type.
16591
16592 Set_Error_Posted (T);
16593
16594 -- Check that within the immediate scope of an untagged partial
16595 -- view it's illegal to derive from the partial view if the
16596 -- full view is tagged. (7.3(7))
16597
16598 -- We verify that the Parent_Type is a partial view by checking
16599 -- that it is not a Full_Type_Declaration (i.e. a private type or
16600 -- private extension declaration), to distinguish a partial view
16601 -- from a derivation from a private type which also appears as
16602 -- E_Private_Type. If the parent base type is not declared in an
16603 -- enclosing scope there is no need to check.
16604
16605 elsif Present (Full_View (Parent_Type))
16606 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16607 and then not Is_Tagged_Type (Parent_Type)
16608 and then Is_Tagged_Type (Full_View (Parent_Type))
16609 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16610 then
16611 Error_Msg_N
16612 ("premature derivation from type with tagged full view",
16613 Indic);
16614 end if;
16615 end if;
16616
16617 -- Check that form of derivation is appropriate
16618
16619 Taggd := Is_Tagged_Type (Parent_Type);
16620
16621 -- Set the parent type to the class-wide type's specific type in this
16622 -- case to prevent cascading errors
16623
16624 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16625 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16626 Set_Etype (T, Etype (Parent_Type));
16627 return;
16628 end if;
16629
16630 if Present (Extension) and then not Taggd then
16631 Error_Msg_N
16632 ("type derived from untagged type cannot have extension", Indic);
16633
16634 elsif No (Extension) and then Taggd then
16635
16636 -- If this declaration is within a private part (or body) of a
16637 -- generic instantiation then the derivation is allowed (the parent
16638 -- type can only appear tagged in this case if it's a generic actual
16639 -- type, since it would otherwise have been rejected in the analysis
16640 -- of the generic template).
16641
16642 if not Is_Generic_Actual_Type (Parent_Type)
16643 or else In_Visible_Part (Scope (Parent_Type))
16644 then
16645 if Is_Class_Wide_Type (Parent_Type) then
16646 Error_Msg_N
16647 ("parent type must not be a class-wide type", Indic);
16648
16649 -- Use specific type to prevent cascaded errors.
16650
16651 Parent_Type := Etype (Parent_Type);
16652
16653 else
16654 Error_Msg_N
16655 ("type derived from tagged type must have extension", Indic);
16656 end if;
16657 end if;
16658 end if;
16659
16660 -- AI-443: Synchronized formal derived types require a private
16661 -- extension. There is no point in checking the ancestor type or
16662 -- the progenitors since the construct is wrong to begin with.
16663
16664 if Ada_Version >= Ada_2005
16665 and then Is_Generic_Type (T)
16666 and then Present (Original_Node (N))
16667 then
16668 declare
16669 Decl : constant Node_Id := Original_Node (N);
16670
16671 begin
16672 if Nkind (Decl) = N_Formal_Type_Declaration
16673 and then Nkind (Formal_Type_Definition (Decl)) =
16674 N_Formal_Derived_Type_Definition
16675 and then Synchronized_Present (Formal_Type_Definition (Decl))
16676 and then No (Extension)
16677
16678 -- Avoid emitting a duplicate error message
16679
16680 and then not Error_Posted (Indic)
16681 then
16682 Error_Msg_N
16683 ("synchronized derived type must have extension", N);
16684 end if;
16685 end;
16686 end if;
16687
16688 if Null_Exclusion_Present (Def)
16689 and then not Is_Access_Type (Parent_Type)
16690 then
16691 Error_Msg_N ("null exclusion can only apply to an access type", N);
16692 end if;
16693
16694 -- Avoid deriving parent primitives of underlying record views
16695
16696 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16697 Derive_Subps => not Is_Underlying_Record_View (T));
16698
16699 -- AI-419: The parent type of an explicitly limited derived type must
16700 -- be a limited type or a limited interface.
16701
16702 if Limited_Present (Def) then
16703 Set_Is_Limited_Record (T);
16704
16705 if Is_Interface (T) then
16706 Set_Is_Limited_Interface (T);
16707 end if;
16708
16709 if not Is_Limited_Type (Parent_Type)
16710 and then
16711 (not Is_Interface (Parent_Type)
16712 or else not Is_Limited_Interface (Parent_Type))
16713 then
16714 -- AI05-0096: a derivation in the private part of an instance is
16715 -- legal if the generic formal is untagged limited, and the actual
16716 -- is non-limited.
16717
16718 if Is_Generic_Actual_Type (Parent_Type)
16719 and then In_Private_Part (Current_Scope)
16720 and then
16721 not Is_Tagged_Type
16722 (Generic_Parent_Type (Parent (Parent_Type)))
16723 then
16724 null;
16725
16726 else
16727 Error_Msg_NE
16728 ("parent type& of limited type must be limited",
16729 N, Parent_Type);
16730 end if;
16731 end if;
16732 end if;
16733
16734 -- In SPARK, there are no derived type definitions other than type
16735 -- extensions of tagged record types.
16736
16737 if No (Extension) then
16738 Check_SPARK_05_Restriction
16739 ("derived type is not allowed", Original_Node (N));
16740 end if;
16741 end Derived_Type_Declaration;
16742
16743 ------------------------
16744 -- Diagnose_Interface --
16745 ------------------------
16746
16747 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16748 begin
16749 if not Is_Interface (E) and then E /= Any_Type then
16750 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16751 end if;
16752 end Diagnose_Interface;
16753
16754 ----------------------------------
16755 -- Enumeration_Type_Declaration --
16756 ----------------------------------
16757
16758 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16759 Ev : Uint;
16760 L : Node_Id;
16761 R_Node : Node_Id;
16762 B_Node : Node_Id;
16763
16764 begin
16765 -- Create identifier node representing lower bound
16766
16767 B_Node := New_Node (N_Identifier, Sloc (Def));
16768 L := First (Literals (Def));
16769 Set_Chars (B_Node, Chars (L));
16770 Set_Entity (B_Node, L);
16771 Set_Etype (B_Node, T);
16772 Set_Is_Static_Expression (B_Node, True);
16773
16774 R_Node := New_Node (N_Range, Sloc (Def));
16775 Set_Low_Bound (R_Node, B_Node);
16776
16777 Set_Ekind (T, E_Enumeration_Type);
16778 Set_First_Literal (T, L);
16779 Set_Etype (T, T);
16780 Set_Is_Constrained (T);
16781
16782 Ev := Uint_0;
16783
16784 -- Loop through literals of enumeration type setting pos and rep values
16785 -- except that if the Ekind is already set, then it means the literal
16786 -- was already constructed (case of a derived type declaration and we
16787 -- should not disturb the Pos and Rep values.
16788
16789 while Present (L) loop
16790 if Ekind (L) /= E_Enumeration_Literal then
16791 Set_Ekind (L, E_Enumeration_Literal);
16792 Set_Enumeration_Pos (L, Ev);
16793 Set_Enumeration_Rep (L, Ev);
16794 Set_Is_Known_Valid (L, True);
16795 end if;
16796
16797 Set_Etype (L, T);
16798 New_Overloaded_Entity (L);
16799 Generate_Definition (L);
16800 Set_Convention (L, Convention_Intrinsic);
16801
16802 -- Case of character literal
16803
16804 if Nkind (L) = N_Defining_Character_Literal then
16805 Set_Is_Character_Type (T, True);
16806
16807 -- Check violation of No_Wide_Characters
16808
16809 if Restriction_Check_Required (No_Wide_Characters) then
16810 Get_Name_String (Chars (L));
16811
16812 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16813 Check_Restriction (No_Wide_Characters, L);
16814 end if;
16815 end if;
16816 end if;
16817
16818 Ev := Ev + 1;
16819 Next (L);
16820 end loop;
16821
16822 -- Now create a node representing upper bound
16823
16824 B_Node := New_Node (N_Identifier, Sloc (Def));
16825 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16826 Set_Entity (B_Node, Last (Literals (Def)));
16827 Set_Etype (B_Node, T);
16828 Set_Is_Static_Expression (B_Node, True);
16829
16830 Set_High_Bound (R_Node, B_Node);
16831
16832 -- Initialize various fields of the type. Some of this information
16833 -- may be overwritten later through rep.clauses.
16834
16835 Set_Scalar_Range (T, R_Node);
16836 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16837 Set_Enum_Esize (T);
16838 Set_Enum_Pos_To_Rep (T, Empty);
16839
16840 -- Set Discard_Names if configuration pragma set, or if there is
16841 -- a parameterless pragma in the current declarative region
16842
16843 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16844 Set_Discard_Names (T);
16845 end if;
16846
16847 -- Process end label if there is one
16848
16849 if Present (Def) then
16850 Process_End_Label (Def, 'e', T);
16851 end if;
16852 end Enumeration_Type_Declaration;
16853
16854 ---------------------------------
16855 -- Expand_To_Stored_Constraint --
16856 ---------------------------------
16857
16858 function Expand_To_Stored_Constraint
16859 (Typ : Entity_Id;
16860 Constraint : Elist_Id) return Elist_Id
16861 is
16862 Explicitly_Discriminated_Type : Entity_Id;
16863 Expansion : Elist_Id;
16864 Discriminant : Entity_Id;
16865
16866 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16867 -- Find the nearest type that actually specifies discriminants
16868
16869 ---------------------------------
16870 -- Type_With_Explicit_Discrims --
16871 ---------------------------------
16872
16873 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16874 Typ : constant E := Base_Type (Id);
16875
16876 begin
16877 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16878 if Present (Full_View (Typ)) then
16879 return Type_With_Explicit_Discrims (Full_View (Typ));
16880 end if;
16881
16882 else
16883 if Has_Discriminants (Typ) then
16884 return Typ;
16885 end if;
16886 end if;
16887
16888 if Etype (Typ) = Typ then
16889 return Empty;
16890 elsif Has_Discriminants (Typ) then
16891 return Typ;
16892 else
16893 return Type_With_Explicit_Discrims (Etype (Typ));
16894 end if;
16895
16896 end Type_With_Explicit_Discrims;
16897
16898 -- Start of processing for Expand_To_Stored_Constraint
16899
16900 begin
16901 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16902 return No_Elist;
16903 end if;
16904
16905 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16906
16907 if No (Explicitly_Discriminated_Type) then
16908 return No_Elist;
16909 end if;
16910
16911 Expansion := New_Elmt_List;
16912
16913 Discriminant :=
16914 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16915 while Present (Discriminant) loop
16916 Append_Elmt
16917 (Get_Discriminant_Value
16918 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16919 To => Expansion);
16920 Next_Stored_Discriminant (Discriminant);
16921 end loop;
16922
16923 return Expansion;
16924 end Expand_To_Stored_Constraint;
16925
16926 ---------------------------
16927 -- Find_Hidden_Interface --
16928 ---------------------------
16929
16930 function Find_Hidden_Interface
16931 (Src : Elist_Id;
16932 Dest : Elist_Id) return Entity_Id
16933 is
16934 Iface : Entity_Id;
16935 Iface_Elmt : Elmt_Id;
16936
16937 begin
16938 if Present (Src) and then Present (Dest) then
16939 Iface_Elmt := First_Elmt (Src);
16940 while Present (Iface_Elmt) loop
16941 Iface := Node (Iface_Elmt);
16942
16943 if Is_Interface (Iface)
16944 and then not Contain_Interface (Iface, Dest)
16945 then
16946 return Iface;
16947 end if;
16948
16949 Next_Elmt (Iface_Elmt);
16950 end loop;
16951 end if;
16952
16953 return Empty;
16954 end Find_Hidden_Interface;
16955
16956 --------------------
16957 -- Find_Type_Name --
16958 --------------------
16959
16960 function Find_Type_Name (N : Node_Id) return Entity_Id is
16961 Id : constant Entity_Id := Defining_Identifier (N);
16962 New_Id : Entity_Id;
16963 Prev : Entity_Id;
16964 Prev_Par : Node_Id;
16965
16966 procedure Check_Duplicate_Aspects;
16967 -- Check that aspects specified in a completion have not been specified
16968 -- already in the partial view.
16969
16970 procedure Tag_Mismatch;
16971 -- Diagnose a tagged partial view whose full view is untagged. We post
16972 -- the message on the full view, with a reference to the previous
16973 -- partial view. The partial view can be private or incomplete, and
16974 -- these are handled in a different manner, so we determine the position
16975 -- of the error message from the respective slocs of both.
16976
16977 -----------------------------
16978 -- Check_Duplicate_Aspects --
16979 -----------------------------
16980
16981 procedure Check_Duplicate_Aspects is
16982 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16983 -- Return the corresponding aspect of the partial view which matches
16984 -- the aspect id of Asp. Return Empty is no such aspect exists.
16985
16986 -----------------------------
16987 -- Get_Partial_View_Aspect --
16988 -----------------------------
16989
16990 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16991 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16992 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16993 Prev_Asp : Node_Id;
16994
16995 begin
16996 if Present (Prev_Asps) then
16997 Prev_Asp := First (Prev_Asps);
16998 while Present (Prev_Asp) loop
16999 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17000 return Prev_Asp;
17001 end if;
17002
17003 Next (Prev_Asp);
17004 end loop;
17005 end if;
17006
17007 return Empty;
17008 end Get_Partial_View_Aspect;
17009
17010 -- Local variables
17011
17012 Full_Asps : constant List_Id := Aspect_Specifications (N);
17013 Full_Asp : Node_Id;
17014 Part_Asp : Node_Id;
17015
17016 -- Start of processing for Check_Duplicate_Aspects
17017
17018 begin
17019 if Present (Full_Asps) then
17020 Full_Asp := First (Full_Asps);
17021 while Present (Full_Asp) loop
17022 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17023
17024 -- An aspect and its class-wide counterpart are two distinct
17025 -- aspects and may apply to both views of an entity.
17026
17027 if Present (Part_Asp)
17028 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17029 then
17030 Error_Msg_N
17031 ("aspect already specified in private declaration",
17032 Full_Asp);
17033
17034 Remove (Full_Asp);
17035 return;
17036 end if;
17037
17038 if Has_Discriminants (Prev)
17039 and then not Has_Unknown_Discriminants (Prev)
17040 and then Get_Aspect_Id (Full_Asp) =
17041 Aspect_Implicit_Dereference
17042 then
17043 Error_Msg_N
17044 ("cannot specify aspect if partial view has known "
17045 & "discriminants", Full_Asp);
17046 end if;
17047
17048 Next (Full_Asp);
17049 end loop;
17050 end if;
17051 end Check_Duplicate_Aspects;
17052
17053 ------------------
17054 -- Tag_Mismatch --
17055 ------------------
17056
17057 procedure Tag_Mismatch is
17058 begin
17059 if Sloc (Prev) < Sloc (Id) then
17060 if Ada_Version >= Ada_2012
17061 and then Nkind (N) = N_Private_Type_Declaration
17062 then
17063 Error_Msg_NE
17064 ("declaration of private } must be a tagged type ", Id, Prev);
17065 else
17066 Error_Msg_NE
17067 ("full declaration of } must be a tagged type ", Id, Prev);
17068 end if;
17069
17070 else
17071 if Ada_Version >= Ada_2012
17072 and then Nkind (N) = N_Private_Type_Declaration
17073 then
17074 Error_Msg_NE
17075 ("declaration of private } must be a tagged type ", Prev, Id);
17076 else
17077 Error_Msg_NE
17078 ("full declaration of } must be a tagged type ", Prev, Id);
17079 end if;
17080 end if;
17081 end Tag_Mismatch;
17082
17083 -- Start of processing for Find_Type_Name
17084
17085 begin
17086 -- Find incomplete declaration, if one was given
17087
17088 Prev := Current_Entity_In_Scope (Id);
17089
17090 -- New type declaration
17091
17092 if No (Prev) then
17093 Enter_Name (Id);
17094 return Id;
17095
17096 -- Previous declaration exists
17097
17098 else
17099 Prev_Par := Parent (Prev);
17100
17101 -- Error if not incomplete/private case except if previous
17102 -- declaration is implicit, etc. Enter_Name will emit error if
17103 -- appropriate.
17104
17105 if not Is_Incomplete_Or_Private_Type (Prev) then
17106 Enter_Name (Id);
17107 New_Id := Id;
17108
17109 -- Check invalid completion of private or incomplete type
17110
17111 elsif not Nkind_In (N, N_Full_Type_Declaration,
17112 N_Task_Type_Declaration,
17113 N_Protected_Type_Declaration)
17114 and then
17115 (Ada_Version < Ada_2012
17116 or else not Is_Incomplete_Type (Prev)
17117 or else not Nkind_In (N, N_Private_Type_Declaration,
17118 N_Private_Extension_Declaration))
17119 then
17120 -- Completion must be a full type declarations (RM 7.3(4))
17121
17122 Error_Msg_Sloc := Sloc (Prev);
17123 Error_Msg_NE ("invalid completion of }", Id, Prev);
17124
17125 -- Set scope of Id to avoid cascaded errors. Entity is never
17126 -- examined again, except when saving globals in generics.
17127
17128 Set_Scope (Id, Current_Scope);
17129 New_Id := Id;
17130
17131 -- If this is a repeated incomplete declaration, no further
17132 -- checks are possible.
17133
17134 if Nkind (N) = N_Incomplete_Type_Declaration then
17135 return Prev;
17136 end if;
17137
17138 -- Case of full declaration of incomplete type
17139
17140 elsif Ekind (Prev) = E_Incomplete_Type
17141 and then (Ada_Version < Ada_2012
17142 or else No (Full_View (Prev))
17143 or else not Is_Private_Type (Full_View (Prev)))
17144 then
17145 -- Indicate that the incomplete declaration has a matching full
17146 -- declaration. The defining occurrence of the incomplete
17147 -- declaration remains the visible one, and the procedure
17148 -- Get_Full_View dereferences it whenever the type is used.
17149
17150 if Present (Full_View (Prev)) then
17151 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17152 end if;
17153
17154 Set_Full_View (Prev, Id);
17155 Append_Entity (Id, Current_Scope);
17156 Set_Is_Public (Id, Is_Public (Prev));
17157 Set_Is_Internal (Id);
17158 New_Id := Prev;
17159
17160 -- If the incomplete view is tagged, a class_wide type has been
17161 -- created already. Use it for the private type as well, in order
17162 -- to prevent multiple incompatible class-wide types that may be
17163 -- created for self-referential anonymous access components.
17164
17165 if Is_Tagged_Type (Prev)
17166 and then Present (Class_Wide_Type (Prev))
17167 then
17168 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17169 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17170
17171 -- Type of the class-wide type is the current Id. Previously
17172 -- this was not done for private declarations because of order-
17173 -- of-elaboration issues in the back end, but gigi now handles
17174 -- this properly.
17175
17176 Set_Etype (Class_Wide_Type (Id), Id);
17177 end if;
17178
17179 -- Case of full declaration of private type
17180
17181 else
17182 -- If the private type was a completion of an incomplete type then
17183 -- update Prev to reference the private type
17184
17185 if Ada_Version >= Ada_2012
17186 and then Ekind (Prev) = E_Incomplete_Type
17187 and then Present (Full_View (Prev))
17188 and then Is_Private_Type (Full_View (Prev))
17189 then
17190 Prev := Full_View (Prev);
17191 Prev_Par := Parent (Prev);
17192 end if;
17193
17194 if Nkind (N) = N_Full_Type_Declaration
17195 and then Nkind_In
17196 (Type_Definition (N), N_Record_Definition,
17197 N_Derived_Type_Definition)
17198 and then Interface_Present (Type_Definition (N))
17199 then
17200 Error_Msg_N
17201 ("completion of private type cannot be an interface", N);
17202 end if;
17203
17204 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17205 if Etype (Prev) /= Prev then
17206
17207 -- Prev is a private subtype or a derived type, and needs
17208 -- no completion.
17209
17210 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17211 New_Id := Id;
17212
17213 elsif Ekind (Prev) = E_Private_Type
17214 and then Nkind_In (N, N_Task_Type_Declaration,
17215 N_Protected_Type_Declaration)
17216 then
17217 Error_Msg_N
17218 ("completion of nonlimited type cannot be limited", N);
17219
17220 elsif Ekind (Prev) = E_Record_Type_With_Private
17221 and then Nkind_In (N, N_Task_Type_Declaration,
17222 N_Protected_Type_Declaration)
17223 then
17224 if not Is_Limited_Record (Prev) then
17225 Error_Msg_N
17226 ("completion of nonlimited type cannot be limited", N);
17227
17228 elsif No (Interface_List (N)) then
17229 Error_Msg_N
17230 ("completion of tagged private type must be tagged",
17231 N);
17232 end if;
17233 end if;
17234
17235 -- Ada 2005 (AI-251): Private extension declaration of a task
17236 -- type or a protected type. This case arises when covering
17237 -- interface types.
17238
17239 elsif Nkind_In (N, N_Task_Type_Declaration,
17240 N_Protected_Type_Declaration)
17241 then
17242 null;
17243
17244 elsif Nkind (N) /= N_Full_Type_Declaration
17245 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17246 then
17247 Error_Msg_N
17248 ("full view of private extension must be an extension", N);
17249
17250 elsif not (Abstract_Present (Parent (Prev)))
17251 and then Abstract_Present (Type_Definition (N))
17252 then
17253 Error_Msg_N
17254 ("full view of non-abstract extension cannot be abstract", N);
17255 end if;
17256
17257 if not In_Private_Part (Current_Scope) then
17258 Error_Msg_N
17259 ("declaration of full view must appear in private part", N);
17260 end if;
17261
17262 if Ada_Version >= Ada_2012 then
17263 Check_Duplicate_Aspects;
17264 end if;
17265
17266 Copy_And_Swap (Prev, Id);
17267 Set_Has_Private_Declaration (Prev);
17268 Set_Has_Private_Declaration (Id);
17269
17270 -- AI12-0133: Indicate whether we have a partial view with
17271 -- unknown discriminants, in which case initialization of objects
17272 -- of the type do not receive an invariant check.
17273
17274 Set_Partial_View_Has_Unknown_Discr
17275 (Prev, Has_Unknown_Discriminants (Id));
17276
17277 -- Preserve aspect and iterator flags that may have been set on
17278 -- the partial view.
17279
17280 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17281 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17282
17283 -- If no error, propagate freeze_node from private to full view.
17284 -- It may have been generated for an early operational item.
17285
17286 if Present (Freeze_Node (Id))
17287 and then Serious_Errors_Detected = 0
17288 and then No (Full_View (Id))
17289 then
17290 Set_Freeze_Node (Prev, Freeze_Node (Id));
17291 Set_Freeze_Node (Id, Empty);
17292 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17293 end if;
17294
17295 Set_Full_View (Id, Prev);
17296 New_Id := Prev;
17297 end if;
17298
17299 -- Verify that full declaration conforms to partial one
17300
17301 if Is_Incomplete_Or_Private_Type (Prev)
17302 and then Present (Discriminant_Specifications (Prev_Par))
17303 then
17304 if Present (Discriminant_Specifications (N)) then
17305 if Ekind (Prev) = E_Incomplete_Type then
17306 Check_Discriminant_Conformance (N, Prev, Prev);
17307 else
17308 Check_Discriminant_Conformance (N, Prev, Id);
17309 end if;
17310
17311 else
17312 Error_Msg_N
17313 ("missing discriminants in full type declaration", N);
17314
17315 -- To avoid cascaded errors on subsequent use, share the
17316 -- discriminants of the partial view.
17317
17318 Set_Discriminant_Specifications (N,
17319 Discriminant_Specifications (Prev_Par));
17320 end if;
17321 end if;
17322
17323 -- A prior untagged partial view can have an associated class-wide
17324 -- type due to use of the class attribute, and in this case the full
17325 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17326 -- of incomplete tagged declarations, but we check for it.
17327
17328 if Is_Type (Prev)
17329 and then (Is_Tagged_Type (Prev)
17330 or else Present (Class_Wide_Type (Prev)))
17331 then
17332 -- Ada 2012 (AI05-0162): A private type may be the completion of
17333 -- an incomplete type.
17334
17335 if Ada_Version >= Ada_2012
17336 and then Is_Incomplete_Type (Prev)
17337 and then Nkind_In (N, N_Private_Type_Declaration,
17338 N_Private_Extension_Declaration)
17339 then
17340 -- No need to check private extensions since they are tagged
17341
17342 if Nkind (N) = N_Private_Type_Declaration
17343 and then not Tagged_Present (N)
17344 then
17345 Tag_Mismatch;
17346 end if;
17347
17348 -- The full declaration is either a tagged type (including
17349 -- a synchronized type that implements interfaces) or a
17350 -- type extension, otherwise this is an error.
17351
17352 elsif Nkind_In (N, N_Task_Type_Declaration,
17353 N_Protected_Type_Declaration)
17354 then
17355 if No (Interface_List (N)) and then not Error_Posted (N) then
17356 Tag_Mismatch;
17357 end if;
17358
17359 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17360
17361 -- Indicate that the previous declaration (tagged incomplete
17362 -- or private declaration) requires the same on the full one.
17363
17364 if not Tagged_Present (Type_Definition (N)) then
17365 Tag_Mismatch;
17366 Set_Is_Tagged_Type (Id);
17367 end if;
17368
17369 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17370 if No (Record_Extension_Part (Type_Definition (N))) then
17371 Error_Msg_NE
17372 ("full declaration of } must be a record extension",
17373 Prev, Id);
17374
17375 -- Set some attributes to produce a usable full view
17376
17377 Set_Is_Tagged_Type (Id);
17378 end if;
17379
17380 else
17381 Tag_Mismatch;
17382 end if;
17383 end if;
17384
17385 if Present (Prev)
17386 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17387 and then Present (Premature_Use (Parent (Prev)))
17388 then
17389 Error_Msg_Sloc := Sloc (N);
17390 Error_Msg_N
17391 ("\full declaration #", Premature_Use (Parent (Prev)));
17392 end if;
17393
17394 return New_Id;
17395 end if;
17396 end Find_Type_Name;
17397
17398 -------------------------
17399 -- Find_Type_Of_Object --
17400 -------------------------
17401
17402 function Find_Type_Of_Object
17403 (Obj_Def : Node_Id;
17404 Related_Nod : Node_Id) return Entity_Id
17405 is
17406 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17407 P : Node_Id := Parent (Obj_Def);
17408 T : Entity_Id;
17409 Nam : Name_Id;
17410
17411 begin
17412 -- If the parent is a component_definition node we climb to the
17413 -- component_declaration node
17414
17415 if Nkind (P) = N_Component_Definition then
17416 P := Parent (P);
17417 end if;
17418
17419 -- Case of an anonymous array subtype
17420
17421 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17422 N_Unconstrained_Array_Definition)
17423 then
17424 T := Empty;
17425 Array_Type_Declaration (T, Obj_Def);
17426
17427 -- Create an explicit subtype whenever possible
17428
17429 elsif Nkind (P) /= N_Component_Declaration
17430 and then Def_Kind = N_Subtype_Indication
17431 then
17432 -- Base name of subtype on object name, which will be unique in
17433 -- the current scope.
17434
17435 -- If this is a duplicate declaration, return base type, to avoid
17436 -- generating duplicate anonymous types.
17437
17438 if Error_Posted (P) then
17439 Analyze (Subtype_Mark (Obj_Def));
17440 return Entity (Subtype_Mark (Obj_Def));
17441 end if;
17442
17443 Nam :=
17444 New_External_Name
17445 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17446
17447 T := Make_Defining_Identifier (Sloc (P), Nam);
17448
17449 Insert_Action (Obj_Def,
17450 Make_Subtype_Declaration (Sloc (P),
17451 Defining_Identifier => T,
17452 Subtype_Indication => Relocate_Node (Obj_Def)));
17453
17454 -- This subtype may need freezing, and this will not be done
17455 -- automatically if the object declaration is not in declarative
17456 -- part. Since this is an object declaration, the type cannot always
17457 -- be frozen here. Deferred constants do not freeze their type
17458 -- (which often enough will be private).
17459
17460 if Nkind (P) = N_Object_Declaration
17461 and then Constant_Present (P)
17462 and then No (Expression (P))
17463 then
17464 null;
17465
17466 -- Here we freeze the base type of object type to catch premature use
17467 -- of discriminated private type without a full view.
17468
17469 else
17470 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17471 end if;
17472
17473 -- Ada 2005 AI-406: the object definition in an object declaration
17474 -- can be an access definition.
17475
17476 elsif Def_Kind = N_Access_Definition then
17477 T := Access_Definition (Related_Nod, Obj_Def);
17478
17479 Set_Is_Local_Anonymous_Access
17480 (T,
17481 V => (Ada_Version < Ada_2012)
17482 or else (Nkind (P) /= N_Object_Declaration)
17483 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17484
17485 -- Otherwise, the object definition is just a subtype_mark
17486
17487 else
17488 T := Process_Subtype (Obj_Def, Related_Nod);
17489
17490 -- If expansion is disabled an object definition that is an aggregate
17491 -- will not get expanded and may lead to scoping problems in the back
17492 -- end, if the object is referenced in an inner scope. In that case
17493 -- create an itype reference for the object definition now. This
17494 -- may be redundant in some cases, but harmless.
17495
17496 if Is_Itype (T)
17497 and then Nkind (Related_Nod) = N_Object_Declaration
17498 and then ASIS_Mode
17499 then
17500 Build_Itype_Reference (T, Related_Nod);
17501 end if;
17502 end if;
17503
17504 return T;
17505 end Find_Type_Of_Object;
17506
17507 --------------------------------
17508 -- Find_Type_Of_Subtype_Indic --
17509 --------------------------------
17510
17511 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17512 Typ : Entity_Id;
17513
17514 begin
17515 -- Case of subtype mark with a constraint
17516
17517 if Nkind (S) = N_Subtype_Indication then
17518 Find_Type (Subtype_Mark (S));
17519 Typ := Entity (Subtype_Mark (S));
17520
17521 if not
17522 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17523 then
17524 Error_Msg_N
17525 ("incorrect constraint for this kind of type", Constraint (S));
17526 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17527 end if;
17528
17529 -- Otherwise we have a subtype mark without a constraint
17530
17531 elsif Error_Posted (S) then
17532 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17533 return Any_Type;
17534
17535 else
17536 Find_Type (S);
17537 Typ := Entity (S);
17538 end if;
17539
17540 -- Check No_Wide_Characters restriction
17541
17542 Check_Wide_Character_Restriction (Typ, S);
17543
17544 return Typ;
17545 end Find_Type_Of_Subtype_Indic;
17546
17547 -------------------------------------
17548 -- Floating_Point_Type_Declaration --
17549 -------------------------------------
17550
17551 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17552 Digs : constant Node_Id := Digits_Expression (Def);
17553 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17554 Digs_Val : Uint;
17555 Base_Typ : Entity_Id;
17556 Implicit_Base : Entity_Id;
17557 Bound : Node_Id;
17558
17559 function Can_Derive_From (E : Entity_Id) return Boolean;
17560 -- Find if given digits value, and possibly a specified range, allows
17561 -- derivation from specified type
17562
17563 function Find_Base_Type return Entity_Id;
17564 -- Find a predefined base type that Def can derive from, or generate
17565 -- an error and substitute Long_Long_Float if none exists.
17566
17567 ---------------------
17568 -- Can_Derive_From --
17569 ---------------------
17570
17571 function Can_Derive_From (E : Entity_Id) return Boolean is
17572 Spec : constant Entity_Id := Real_Range_Specification (Def);
17573
17574 begin
17575 -- Check specified "digits" constraint
17576
17577 if Digs_Val > Digits_Value (E) then
17578 return False;
17579 end if;
17580
17581 -- Check for matching range, if specified
17582
17583 if Present (Spec) then
17584 if Expr_Value_R (Type_Low_Bound (E)) >
17585 Expr_Value_R (Low_Bound (Spec))
17586 then
17587 return False;
17588 end if;
17589
17590 if Expr_Value_R (Type_High_Bound (E)) <
17591 Expr_Value_R (High_Bound (Spec))
17592 then
17593 return False;
17594 end if;
17595 end if;
17596
17597 return True;
17598 end Can_Derive_From;
17599
17600 --------------------
17601 -- Find_Base_Type --
17602 --------------------
17603
17604 function Find_Base_Type return Entity_Id is
17605 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17606
17607 begin
17608 -- Iterate over the predefined types in order, returning the first
17609 -- one that Def can derive from.
17610
17611 while Present (Choice) loop
17612 if Can_Derive_From (Node (Choice)) then
17613 return Node (Choice);
17614 end if;
17615
17616 Next_Elmt (Choice);
17617 end loop;
17618
17619 -- If we can't derive from any existing type, use Long_Long_Float
17620 -- and give appropriate message explaining the problem.
17621
17622 if Digs_Val > Max_Digs_Val then
17623 -- It might be the case that there is a type with the requested
17624 -- range, just not the combination of digits and range.
17625
17626 Error_Msg_N
17627 ("no predefined type has requested range and precision",
17628 Real_Range_Specification (Def));
17629
17630 else
17631 Error_Msg_N
17632 ("range too large for any predefined type",
17633 Real_Range_Specification (Def));
17634 end if;
17635
17636 return Standard_Long_Long_Float;
17637 end Find_Base_Type;
17638
17639 -- Start of processing for Floating_Point_Type_Declaration
17640
17641 begin
17642 Check_Restriction (No_Floating_Point, Def);
17643
17644 -- Create an implicit base type
17645
17646 Implicit_Base :=
17647 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17648
17649 -- Analyze and verify digits value
17650
17651 Analyze_And_Resolve (Digs, Any_Integer);
17652 Check_Digits_Expression (Digs);
17653 Digs_Val := Expr_Value (Digs);
17654
17655 -- Process possible range spec and find correct type to derive from
17656
17657 Process_Real_Range_Specification (Def);
17658
17659 -- Check that requested number of digits is not too high.
17660
17661 if Digs_Val > Max_Digs_Val then
17662
17663 -- The check for Max_Base_Digits may be somewhat expensive, as it
17664 -- requires reading System, so only do it when necessary.
17665
17666 declare
17667 Max_Base_Digits : constant Uint :=
17668 Expr_Value
17669 (Expression
17670 (Parent (RTE (RE_Max_Base_Digits))));
17671
17672 begin
17673 if Digs_Val > Max_Base_Digits then
17674 Error_Msg_Uint_1 := Max_Base_Digits;
17675 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17676
17677 elsif No (Real_Range_Specification (Def)) then
17678 Error_Msg_Uint_1 := Max_Digs_Val;
17679 Error_Msg_N ("types with more than ^ digits need range spec "
17680 & "(RM 3.5.7(6))", Digs);
17681 end if;
17682 end;
17683 end if;
17684
17685 -- Find a suitable type to derive from or complain and use a substitute
17686
17687 Base_Typ := Find_Base_Type;
17688
17689 -- If there are bounds given in the declaration use them as the bounds
17690 -- of the type, otherwise use the bounds of the predefined base type
17691 -- that was chosen based on the Digits value.
17692
17693 if Present (Real_Range_Specification (Def)) then
17694 Set_Scalar_Range (T, Real_Range_Specification (Def));
17695 Set_Is_Constrained (T);
17696
17697 -- The bounds of this range must be converted to machine numbers
17698 -- in accordance with RM 4.9(38).
17699
17700 Bound := Type_Low_Bound (T);
17701
17702 if Nkind (Bound) = N_Real_Literal then
17703 Set_Realval
17704 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17705 Set_Is_Machine_Number (Bound);
17706 end if;
17707
17708 Bound := Type_High_Bound (T);
17709
17710 if Nkind (Bound) = N_Real_Literal then
17711 Set_Realval
17712 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17713 Set_Is_Machine_Number (Bound);
17714 end if;
17715
17716 else
17717 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17718 end if;
17719
17720 -- Complete definition of implicit base and declared first subtype. The
17721 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17722 -- are not clobbered when the floating point type acts as a full view of
17723 -- a private type.
17724
17725 Set_Etype (Implicit_Base, Base_Typ);
17726 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17727 Set_Size_Info (Implicit_Base, Base_Typ);
17728 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17729 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17730 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17731 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17732
17733 Set_Ekind (T, E_Floating_Point_Subtype);
17734 Set_Etype (T, Implicit_Base);
17735 Set_Size_Info (T, Implicit_Base);
17736 Set_RM_Size (T, RM_Size (Implicit_Base));
17737 Inherit_Rep_Item_Chain (T, Implicit_Base);
17738 Set_Digits_Value (T, Digs_Val);
17739 end Floating_Point_Type_Declaration;
17740
17741 ----------------------------
17742 -- Get_Discriminant_Value --
17743 ----------------------------
17744
17745 -- This is the situation:
17746
17747 -- There is a non-derived type
17748
17749 -- type T0 (Dx, Dy, Dz...)
17750
17751 -- There are zero or more levels of derivation, with each derivation
17752 -- either purely inheriting the discriminants, or defining its own.
17753
17754 -- type Ti is new Ti-1
17755 -- or
17756 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17757 -- or
17758 -- subtype Ti is ...
17759
17760 -- The subtype issue is avoided by the use of Original_Record_Component,
17761 -- and the fact that derived subtypes also derive the constraints.
17762
17763 -- This chain leads back from
17764
17765 -- Typ_For_Constraint
17766
17767 -- Typ_For_Constraint has discriminants, and the value for each
17768 -- discriminant is given by its corresponding Elmt of Constraints.
17769
17770 -- Discriminant is some discriminant in this hierarchy
17771
17772 -- We need to return its value
17773
17774 -- We do this by recursively searching each level, and looking for
17775 -- Discriminant. Once we get to the bottom, we start backing up
17776 -- returning the value for it which may in turn be a discriminant
17777 -- further up, so on the backup we continue the substitution.
17778
17779 function Get_Discriminant_Value
17780 (Discriminant : Entity_Id;
17781 Typ_For_Constraint : Entity_Id;
17782 Constraint : Elist_Id) return Node_Id
17783 is
17784 function Root_Corresponding_Discriminant
17785 (Discr : Entity_Id) return Entity_Id;
17786 -- Given a discriminant, traverse the chain of inherited discriminants
17787 -- and return the topmost discriminant.
17788
17789 function Search_Derivation_Levels
17790 (Ti : Entity_Id;
17791 Discrim_Values : Elist_Id;
17792 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17793 -- This is the routine that performs the recursive search of levels
17794 -- as described above.
17795
17796 -------------------------------------
17797 -- Root_Corresponding_Discriminant --
17798 -------------------------------------
17799
17800 function Root_Corresponding_Discriminant
17801 (Discr : Entity_Id) return Entity_Id
17802 is
17803 D : Entity_Id;
17804
17805 begin
17806 D := Discr;
17807 while Present (Corresponding_Discriminant (D)) loop
17808 D := Corresponding_Discriminant (D);
17809 end loop;
17810
17811 return D;
17812 end Root_Corresponding_Discriminant;
17813
17814 ------------------------------
17815 -- Search_Derivation_Levels --
17816 ------------------------------
17817
17818 function Search_Derivation_Levels
17819 (Ti : Entity_Id;
17820 Discrim_Values : Elist_Id;
17821 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17822 is
17823 Assoc : Elmt_Id;
17824 Disc : Entity_Id;
17825 Result : Node_Or_Entity_Id;
17826 Result_Entity : Node_Id;
17827
17828 begin
17829 -- If inappropriate type, return Error, this happens only in
17830 -- cascaded error situations, and we want to avoid a blow up.
17831
17832 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17833 return Error;
17834 end if;
17835
17836 -- Look deeper if possible. Use Stored_Constraints only for
17837 -- untagged types. For tagged types use the given constraint.
17838 -- This asymmetry needs explanation???
17839
17840 if not Stored_Discrim_Values
17841 and then Present (Stored_Constraint (Ti))
17842 and then not Is_Tagged_Type (Ti)
17843 then
17844 Result :=
17845 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17846 else
17847 declare
17848 Td : constant Entity_Id := Etype (Ti);
17849
17850 begin
17851 if Td = Ti then
17852 Result := Discriminant;
17853
17854 else
17855 if Present (Stored_Constraint (Ti)) then
17856 Result :=
17857 Search_Derivation_Levels
17858 (Td, Stored_Constraint (Ti), True);
17859 else
17860 Result :=
17861 Search_Derivation_Levels
17862 (Td, Discrim_Values, Stored_Discrim_Values);
17863 end if;
17864 end if;
17865 end;
17866 end if;
17867
17868 -- Extra underlying places to search, if not found above. For
17869 -- concurrent types, the relevant discriminant appears in the
17870 -- corresponding record. For a type derived from a private type
17871 -- without discriminant, the full view inherits the discriminants
17872 -- of the full view of the parent.
17873
17874 if Result = Discriminant then
17875 if Is_Concurrent_Type (Ti)
17876 and then Present (Corresponding_Record_Type (Ti))
17877 then
17878 Result :=
17879 Search_Derivation_Levels (
17880 Corresponding_Record_Type (Ti),
17881 Discrim_Values,
17882 Stored_Discrim_Values);
17883
17884 elsif Is_Private_Type (Ti)
17885 and then not Has_Discriminants (Ti)
17886 and then Present (Full_View (Ti))
17887 and then Etype (Full_View (Ti)) /= Ti
17888 then
17889 Result :=
17890 Search_Derivation_Levels (
17891 Full_View (Ti),
17892 Discrim_Values,
17893 Stored_Discrim_Values);
17894 end if;
17895 end if;
17896
17897 -- If Result is not a (reference to a) discriminant, return it,
17898 -- otherwise set Result_Entity to the discriminant.
17899
17900 if Nkind (Result) = N_Defining_Identifier then
17901 pragma Assert (Result = Discriminant);
17902 Result_Entity := Result;
17903
17904 else
17905 if not Denotes_Discriminant (Result) then
17906 return Result;
17907 end if;
17908
17909 Result_Entity := Entity (Result);
17910 end if;
17911
17912 -- See if this level of derivation actually has discriminants because
17913 -- tagged derivations can add them, hence the lower levels need not
17914 -- have any.
17915
17916 if not Has_Discriminants (Ti) then
17917 return Result;
17918 end if;
17919
17920 -- Scan Ti's discriminants for Result_Entity, and return its
17921 -- corresponding value, if any.
17922
17923 Result_Entity := Original_Record_Component (Result_Entity);
17924
17925 Assoc := First_Elmt (Discrim_Values);
17926
17927 if Stored_Discrim_Values then
17928 Disc := First_Stored_Discriminant (Ti);
17929 else
17930 Disc := First_Discriminant (Ti);
17931 end if;
17932
17933 while Present (Disc) loop
17934
17935 -- If no further associations return the discriminant, value will
17936 -- be found on the second pass.
17937
17938 if No (Assoc) then
17939 return Result;
17940 end if;
17941
17942 if Original_Record_Component (Disc) = Result_Entity then
17943 return Node (Assoc);
17944 end if;
17945
17946 Next_Elmt (Assoc);
17947
17948 if Stored_Discrim_Values then
17949 Next_Stored_Discriminant (Disc);
17950 else
17951 Next_Discriminant (Disc);
17952 end if;
17953 end loop;
17954
17955 -- Could not find it
17956
17957 return Result;
17958 end Search_Derivation_Levels;
17959
17960 -- Local Variables
17961
17962 Result : Node_Or_Entity_Id;
17963
17964 -- Start of processing for Get_Discriminant_Value
17965
17966 begin
17967 -- ??? This routine is a gigantic mess and will be deleted. For the
17968 -- time being just test for the trivial case before calling recurse.
17969
17970 -- We are now celebrating the 20th anniversary of this comment!
17971
17972 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17973 declare
17974 D : Entity_Id;
17975 E : Elmt_Id;
17976
17977 begin
17978 D := First_Discriminant (Typ_For_Constraint);
17979 E := First_Elmt (Constraint);
17980 while Present (D) loop
17981 if Chars (D) = Chars (Discriminant) then
17982 return Node (E);
17983 end if;
17984
17985 Next_Discriminant (D);
17986 Next_Elmt (E);
17987 end loop;
17988 end;
17989 end if;
17990
17991 Result := Search_Derivation_Levels
17992 (Typ_For_Constraint, Constraint, False);
17993
17994 -- ??? hack to disappear when this routine is gone
17995
17996 if Nkind (Result) = N_Defining_Identifier then
17997 declare
17998 D : Entity_Id;
17999 E : Elmt_Id;
18000
18001 begin
18002 D := First_Discriminant (Typ_For_Constraint);
18003 E := First_Elmt (Constraint);
18004 while Present (D) loop
18005 if Root_Corresponding_Discriminant (D) = Discriminant then
18006 return Node (E);
18007 end if;
18008
18009 Next_Discriminant (D);
18010 Next_Elmt (E);
18011 end loop;
18012 end;
18013 end if;
18014
18015 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18016 return Result;
18017 end Get_Discriminant_Value;
18018
18019 --------------------------
18020 -- Has_Range_Constraint --
18021 --------------------------
18022
18023 function Has_Range_Constraint (N : Node_Id) return Boolean is
18024 C : constant Node_Id := Constraint (N);
18025
18026 begin
18027 if Nkind (C) = N_Range_Constraint then
18028 return True;
18029
18030 elsif Nkind (C) = N_Digits_Constraint then
18031 return
18032 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18033 or else Present (Range_Constraint (C));
18034
18035 elsif Nkind (C) = N_Delta_Constraint then
18036 return Present (Range_Constraint (C));
18037
18038 else
18039 return False;
18040 end if;
18041 end Has_Range_Constraint;
18042
18043 ------------------------
18044 -- Inherit_Components --
18045 ------------------------
18046
18047 function Inherit_Components
18048 (N : Node_Id;
18049 Parent_Base : Entity_Id;
18050 Derived_Base : Entity_Id;
18051 Is_Tagged : Boolean;
18052 Inherit_Discr : Boolean;
18053 Discs : Elist_Id) return Elist_Id
18054 is
18055 Assoc_List : constant Elist_Id := New_Elmt_List;
18056
18057 procedure Inherit_Component
18058 (Old_C : Entity_Id;
18059 Plain_Discrim : Boolean := False;
18060 Stored_Discrim : Boolean := False);
18061 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18062 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18063 -- True, Old_C is a stored discriminant. If they are both false then
18064 -- Old_C is a regular component.
18065
18066 -----------------------
18067 -- Inherit_Component --
18068 -----------------------
18069
18070 procedure Inherit_Component
18071 (Old_C : Entity_Id;
18072 Plain_Discrim : Boolean := False;
18073 Stored_Discrim : Boolean := False)
18074 is
18075 procedure Set_Anonymous_Type (Id : Entity_Id);
18076 -- Id denotes the entity of an access discriminant or anonymous
18077 -- access component. Set the type of Id to either the same type of
18078 -- Old_C or create a new one depending on whether the parent and
18079 -- the child types are in the same scope.
18080
18081 ------------------------
18082 -- Set_Anonymous_Type --
18083 ------------------------
18084
18085 procedure Set_Anonymous_Type (Id : Entity_Id) is
18086 Old_Typ : constant Entity_Id := Etype (Old_C);
18087
18088 begin
18089 if Scope (Parent_Base) = Scope (Derived_Base) then
18090 Set_Etype (Id, Old_Typ);
18091
18092 -- The parent and the derived type are in two different scopes.
18093 -- Reuse the type of the original discriminant / component by
18094 -- copying it in order to preserve all attributes.
18095
18096 else
18097 declare
18098 Typ : constant Entity_Id := New_Copy (Old_Typ);
18099
18100 begin
18101 Set_Etype (Id, Typ);
18102
18103 -- Since we do not generate component declarations for
18104 -- inherited components, associate the itype with the
18105 -- derived type.
18106
18107 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18108 Set_Scope (Typ, Derived_Base);
18109 end;
18110 end if;
18111 end Set_Anonymous_Type;
18112
18113 -- Local variables and constants
18114
18115 New_C : constant Entity_Id := New_Copy (Old_C);
18116
18117 Corr_Discrim : Entity_Id;
18118 Discrim : Entity_Id;
18119
18120 -- Start of processing for Inherit_Component
18121
18122 begin
18123 pragma Assert (not Is_Tagged or not Stored_Discrim);
18124
18125 Set_Parent (New_C, Parent (Old_C));
18126
18127 -- Regular discriminants and components must be inserted in the scope
18128 -- of the Derived_Base. Do it here.
18129
18130 if not Stored_Discrim then
18131 Enter_Name (New_C);
18132 end if;
18133
18134 -- For tagged types the Original_Record_Component must point to
18135 -- whatever this field was pointing to in the parent type. This has
18136 -- already been achieved by the call to New_Copy above.
18137
18138 if not Is_Tagged then
18139 Set_Original_Record_Component (New_C, New_C);
18140 end if;
18141
18142 -- Set the proper type of an access discriminant
18143
18144 if Ekind (New_C) = E_Discriminant
18145 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18146 then
18147 Set_Anonymous_Type (New_C);
18148 end if;
18149
18150 -- If we have inherited a component then see if its Etype contains
18151 -- references to Parent_Base discriminants. In this case, replace
18152 -- these references with the constraints given in Discs. We do not
18153 -- do this for the partial view of private types because this is
18154 -- not needed (only the components of the full view will be used
18155 -- for code generation) and cause problem. We also avoid this
18156 -- transformation in some error situations.
18157
18158 if Ekind (New_C) = E_Component then
18159
18160 -- Set the proper type of an anonymous access component
18161
18162 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18163 Set_Anonymous_Type (New_C);
18164
18165 elsif (Is_Private_Type (Derived_Base)
18166 and then not Is_Generic_Type (Derived_Base))
18167 or else (Is_Empty_Elmt_List (Discs)
18168 and then not Expander_Active)
18169 then
18170 Set_Etype (New_C, Etype (Old_C));
18171
18172 else
18173 -- The current component introduces a circularity of the
18174 -- following kind:
18175
18176 -- limited with Pack_2;
18177 -- package Pack_1 is
18178 -- type T_1 is tagged record
18179 -- Comp : access Pack_2.T_2;
18180 -- ...
18181 -- end record;
18182 -- end Pack_1;
18183
18184 -- with Pack_1;
18185 -- package Pack_2 is
18186 -- type T_2 is new Pack_1.T_1 with ...;
18187 -- end Pack_2;
18188
18189 Set_Etype
18190 (New_C,
18191 Constrain_Component_Type
18192 (Old_C, Derived_Base, N, Parent_Base, Discs));
18193 end if;
18194 end if;
18195
18196 -- In derived tagged types it is illegal to reference a non
18197 -- discriminant component in the parent type. To catch this, mark
18198 -- these components with an Ekind of E_Void. This will be reset in
18199 -- Record_Type_Definition after processing the record extension of
18200 -- the derived type.
18201
18202 -- If the declaration is a private extension, there is no further
18203 -- record extension to process, and the components retain their
18204 -- current kind, because they are visible at this point.
18205
18206 if Is_Tagged and then Ekind (New_C) = E_Component
18207 and then Nkind (N) /= N_Private_Extension_Declaration
18208 then
18209 Set_Ekind (New_C, E_Void);
18210 end if;
18211
18212 if Plain_Discrim then
18213 Set_Corresponding_Discriminant (New_C, Old_C);
18214 Build_Discriminal (New_C);
18215
18216 -- If we are explicitly inheriting a stored discriminant it will be
18217 -- completely hidden.
18218
18219 elsif Stored_Discrim then
18220 Set_Corresponding_Discriminant (New_C, Empty);
18221 Set_Discriminal (New_C, Empty);
18222 Set_Is_Completely_Hidden (New_C);
18223
18224 -- Set the Original_Record_Component of each discriminant in the
18225 -- derived base to point to the corresponding stored that we just
18226 -- created.
18227
18228 Discrim := First_Discriminant (Derived_Base);
18229 while Present (Discrim) loop
18230 Corr_Discrim := Corresponding_Discriminant (Discrim);
18231
18232 -- Corr_Discrim could be missing in an error situation
18233
18234 if Present (Corr_Discrim)
18235 and then Original_Record_Component (Corr_Discrim) = Old_C
18236 then
18237 Set_Original_Record_Component (Discrim, New_C);
18238 end if;
18239
18240 Next_Discriminant (Discrim);
18241 end loop;
18242
18243 Append_Entity (New_C, Derived_Base);
18244 end if;
18245
18246 if not Is_Tagged then
18247 Append_Elmt (Old_C, Assoc_List);
18248 Append_Elmt (New_C, Assoc_List);
18249 end if;
18250 end Inherit_Component;
18251
18252 -- Variables local to Inherit_Component
18253
18254 Loc : constant Source_Ptr := Sloc (N);
18255
18256 Parent_Discrim : Entity_Id;
18257 Stored_Discrim : Entity_Id;
18258 D : Entity_Id;
18259 Component : Entity_Id;
18260
18261 -- Start of processing for Inherit_Components
18262
18263 begin
18264 if not Is_Tagged then
18265 Append_Elmt (Parent_Base, Assoc_List);
18266 Append_Elmt (Derived_Base, Assoc_List);
18267 end if;
18268
18269 -- Inherit parent discriminants if needed
18270
18271 if Inherit_Discr then
18272 Parent_Discrim := First_Discriminant (Parent_Base);
18273 while Present (Parent_Discrim) loop
18274 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18275 Next_Discriminant (Parent_Discrim);
18276 end loop;
18277 end if;
18278
18279 -- Create explicit stored discrims for untagged types when necessary
18280
18281 if not Has_Unknown_Discriminants (Derived_Base)
18282 and then Has_Discriminants (Parent_Base)
18283 and then not Is_Tagged
18284 and then
18285 (not Inherit_Discr
18286 or else First_Discriminant (Parent_Base) /=
18287 First_Stored_Discriminant (Parent_Base))
18288 then
18289 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18290 while Present (Stored_Discrim) loop
18291 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18292 Next_Stored_Discriminant (Stored_Discrim);
18293 end loop;
18294 end if;
18295
18296 -- See if we can apply the second transformation for derived types, as
18297 -- explained in point 6. in the comments above Build_Derived_Record_Type
18298 -- This is achieved by appending Derived_Base discriminants into Discs,
18299 -- which has the side effect of returning a non empty Discs list to the
18300 -- caller of Inherit_Components, which is what we want. This must be
18301 -- done for private derived types if there are explicit stored
18302 -- discriminants, to ensure that we can retrieve the values of the
18303 -- constraints provided in the ancestors.
18304
18305 if Inherit_Discr
18306 and then Is_Empty_Elmt_List (Discs)
18307 and then Present (First_Discriminant (Derived_Base))
18308 and then
18309 (not Is_Private_Type (Derived_Base)
18310 or else Is_Completely_Hidden
18311 (First_Stored_Discriminant (Derived_Base))
18312 or else Is_Generic_Type (Derived_Base))
18313 then
18314 D := First_Discriminant (Derived_Base);
18315 while Present (D) loop
18316 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18317 Next_Discriminant (D);
18318 end loop;
18319 end if;
18320
18321 -- Finally, inherit non-discriminant components unless they are not
18322 -- visible because defined or inherited from the full view of the
18323 -- parent. Don't inherit the _parent field of the parent type.
18324
18325 Component := First_Entity (Parent_Base);
18326 while Present (Component) loop
18327
18328 -- Ada 2005 (AI-251): Do not inherit components associated with
18329 -- secondary tags of the parent.
18330
18331 if Ekind (Component) = E_Component
18332 and then Present (Related_Type (Component))
18333 then
18334 null;
18335
18336 elsif Ekind (Component) /= E_Component
18337 or else Chars (Component) = Name_uParent
18338 then
18339 null;
18340
18341 -- If the derived type is within the parent type's declarative
18342 -- region, then the components can still be inherited even though
18343 -- they aren't visible at this point. This can occur for cases
18344 -- such as within public child units where the components must
18345 -- become visible upon entering the child unit's private part.
18346
18347 elsif not Is_Visible_Component (Component)
18348 and then not In_Open_Scopes (Scope (Parent_Base))
18349 then
18350 null;
18351
18352 elsif Ekind_In (Derived_Base, E_Private_Type,
18353 E_Limited_Private_Type)
18354 then
18355 null;
18356
18357 else
18358 Inherit_Component (Component);
18359 end if;
18360
18361 Next_Entity (Component);
18362 end loop;
18363
18364 -- For tagged derived types, inherited discriminants cannot be used in
18365 -- component declarations of the record extension part. To achieve this
18366 -- we mark the inherited discriminants as not visible.
18367
18368 if Is_Tagged and then Inherit_Discr then
18369 D := First_Discriminant (Derived_Base);
18370 while Present (D) loop
18371 Set_Is_Immediately_Visible (D, False);
18372 Next_Discriminant (D);
18373 end loop;
18374 end if;
18375
18376 return Assoc_List;
18377 end Inherit_Components;
18378
18379 -----------------------------
18380 -- Inherit_Predicate_Flags --
18381 -----------------------------
18382
18383 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18384 begin
18385 Set_Has_Predicates (Subt, Has_Predicates (Par));
18386 Set_Has_Static_Predicate_Aspect
18387 (Subt, Has_Static_Predicate_Aspect (Par));
18388 Set_Has_Dynamic_Predicate_Aspect
18389 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18390 end Inherit_Predicate_Flags;
18391
18392 ----------------------
18393 -- Is_EVF_Procedure --
18394 ----------------------
18395
18396 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18397 Formal : Entity_Id;
18398
18399 begin
18400 -- Examine the formals of an Extensions_Visible False procedure looking
18401 -- for a controlling OUT parameter.
18402
18403 if Ekind (Subp) = E_Procedure
18404 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18405 then
18406 Formal := First_Formal (Subp);
18407 while Present (Formal) loop
18408 if Ekind (Formal) = E_Out_Parameter
18409 and then Is_Controlling_Formal (Formal)
18410 then
18411 return True;
18412 end if;
18413
18414 Next_Formal (Formal);
18415 end loop;
18416 end if;
18417
18418 return False;
18419 end Is_EVF_Procedure;
18420
18421 -----------------------
18422 -- Is_Null_Extension --
18423 -----------------------
18424
18425 function Is_Null_Extension (T : Entity_Id) return Boolean is
18426 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18427 Comp_List : Node_Id;
18428 Comp : Node_Id;
18429
18430 begin
18431 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18432 or else not Is_Tagged_Type (T)
18433 or else Nkind (Type_Definition (Type_Decl)) /=
18434 N_Derived_Type_Definition
18435 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18436 then
18437 return False;
18438 end if;
18439
18440 Comp_List :=
18441 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18442
18443 if Present (Discriminant_Specifications (Type_Decl)) then
18444 return False;
18445
18446 elsif Present (Comp_List)
18447 and then Is_Non_Empty_List (Component_Items (Comp_List))
18448 then
18449 Comp := First (Component_Items (Comp_List));
18450
18451 -- Only user-defined components are relevant. The component list
18452 -- may also contain a parent component and internal components
18453 -- corresponding to secondary tags, but these do not determine
18454 -- whether this is a null extension.
18455
18456 while Present (Comp) loop
18457 if Comes_From_Source (Comp) then
18458 return False;
18459 end if;
18460
18461 Next (Comp);
18462 end loop;
18463
18464 return True;
18465
18466 else
18467 return True;
18468 end if;
18469 end Is_Null_Extension;
18470
18471 ------------------------------
18472 -- Is_Valid_Constraint_Kind --
18473 ------------------------------
18474
18475 function Is_Valid_Constraint_Kind
18476 (T_Kind : Type_Kind;
18477 Constraint_Kind : Node_Kind) return Boolean
18478 is
18479 begin
18480 case T_Kind is
18481 when Enumeration_Kind
18482 | Integer_Kind
18483 =>
18484 return Constraint_Kind = N_Range_Constraint;
18485
18486 when Decimal_Fixed_Point_Kind =>
18487 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18488 N_Range_Constraint);
18489
18490 when Ordinary_Fixed_Point_Kind =>
18491 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18492 N_Range_Constraint);
18493
18494 when Float_Kind =>
18495 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18496 N_Range_Constraint);
18497
18498 when Access_Kind
18499 | Array_Kind
18500 | Class_Wide_Kind
18501 | Concurrent_Kind
18502 | Private_Kind
18503 | E_Incomplete_Type
18504 | E_Record_Subtype
18505 | E_Record_Type
18506 =>
18507 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18508
18509 when others =>
18510 return True; -- Error will be detected later
18511 end case;
18512 end Is_Valid_Constraint_Kind;
18513
18514 --------------------------
18515 -- Is_Visible_Component --
18516 --------------------------
18517
18518 function Is_Visible_Component
18519 (C : Entity_Id;
18520 N : Node_Id := Empty) return Boolean
18521 is
18522 Original_Comp : Entity_Id := Empty;
18523 Original_Type : Entity_Id;
18524 Type_Scope : Entity_Id;
18525
18526 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18527 -- Check whether parent type of inherited component is declared locally,
18528 -- possibly within a nested package or instance. The current scope is
18529 -- the derived record itself.
18530
18531 -------------------
18532 -- Is_Local_Type --
18533 -------------------
18534
18535 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18536 Scop : Entity_Id;
18537
18538 begin
18539 Scop := Scope (Typ);
18540 while Present (Scop)
18541 and then Scop /= Standard_Standard
18542 loop
18543 if Scop = Scope (Current_Scope) then
18544 return True;
18545 end if;
18546
18547 Scop := Scope (Scop);
18548 end loop;
18549
18550 return False;
18551 end Is_Local_Type;
18552
18553 -- Start of processing for Is_Visible_Component
18554
18555 begin
18556 if Ekind_In (C, E_Component, E_Discriminant) then
18557 Original_Comp := Original_Record_Component (C);
18558 end if;
18559
18560 if No (Original_Comp) then
18561
18562 -- Premature usage, or previous error
18563
18564 return False;
18565
18566 else
18567 Original_Type := Scope (Original_Comp);
18568 Type_Scope := Scope (Base_Type (Scope (C)));
18569 end if;
18570
18571 -- This test only concerns tagged types
18572
18573 if not Is_Tagged_Type (Original_Type) then
18574 return True;
18575
18576 -- If it is _Parent or _Tag, there is no visibility issue
18577
18578 elsif not Comes_From_Source (Original_Comp) then
18579 return True;
18580
18581 -- Discriminants are visible unless the (private) type has unknown
18582 -- discriminants. If the discriminant reference is inserted for a
18583 -- discriminant check on a full view it is also visible.
18584
18585 elsif Ekind (Original_Comp) = E_Discriminant
18586 and then
18587 (not Has_Unknown_Discriminants (Original_Type)
18588 or else (Present (N)
18589 and then Nkind (N) = N_Selected_Component
18590 and then Nkind (Prefix (N)) = N_Type_Conversion
18591 and then not Comes_From_Source (Prefix (N))))
18592 then
18593 return True;
18594
18595 -- In the body of an instantiation, check the visibility of a component
18596 -- in case it has a homograph that is a primitive operation of a private
18597 -- type which was not visible in the generic unit.
18598
18599 -- Should Is_Prefixed_Call be propagated from template to instance???
18600
18601 elsif In_Instance_Body then
18602 if not Is_Tagged_Type (Original_Type)
18603 or else not Is_Private_Type (Original_Type)
18604 then
18605 return True;
18606
18607 else
18608 declare
18609 Subp_Elmt : Elmt_Id;
18610
18611 begin
18612 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18613 while Present (Subp_Elmt) loop
18614
18615 -- The component is hidden by a primitive operation
18616
18617 if Chars (Node (Subp_Elmt)) = Chars (C) then
18618 return False;
18619 end if;
18620
18621 Next_Elmt (Subp_Elmt);
18622 end loop;
18623
18624 return True;
18625 end;
18626 end if;
18627
18628 -- If the component has been declared in an ancestor which is currently
18629 -- a private type, then it is not visible. The same applies if the
18630 -- component's containing type is not in an open scope and the original
18631 -- component's enclosing type is a visible full view of a private type
18632 -- (which can occur in cases where an attempt is being made to reference
18633 -- a component in a sibling package that is inherited from a visible
18634 -- component of a type in an ancestor package; the component in the
18635 -- sibling package should not be visible even though the component it
18636 -- inherited from is visible). This does not apply however in the case
18637 -- where the scope of the type is a private child unit, or when the
18638 -- parent comes from a local package in which the ancestor is currently
18639 -- visible. The latter suppression of visibility is needed for cases
18640 -- that are tested in B730006.
18641
18642 elsif Is_Private_Type (Original_Type)
18643 or else
18644 (not Is_Private_Descendant (Type_Scope)
18645 and then not In_Open_Scopes (Type_Scope)
18646 and then Has_Private_Declaration (Original_Type))
18647 then
18648 -- If the type derives from an entity in a formal package, there
18649 -- are no additional visible components.
18650
18651 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18652 N_Formal_Package_Declaration
18653 then
18654 return False;
18655
18656 -- if we are not in the private part of the current package, there
18657 -- are no additional visible components.
18658
18659 elsif Ekind (Scope (Current_Scope)) = E_Package
18660 and then not In_Private_Part (Scope (Current_Scope))
18661 then
18662 return False;
18663 else
18664 return
18665 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18666 and then In_Open_Scopes (Scope (Original_Type))
18667 and then Is_Local_Type (Type_Scope);
18668 end if;
18669
18670 -- There is another weird way in which a component may be invisible when
18671 -- the private and the full view are not derived from the same ancestor.
18672 -- Here is an example :
18673
18674 -- type A1 is tagged record F1 : integer; end record;
18675 -- type A2 is new A1 with record F2 : integer; end record;
18676 -- type T is new A1 with private;
18677 -- private
18678 -- type T is new A2 with null record;
18679
18680 -- In this case, the full view of T inherits F1 and F2 but the private
18681 -- view inherits only F1
18682
18683 else
18684 declare
18685 Ancestor : Entity_Id := Scope (C);
18686
18687 begin
18688 loop
18689 if Ancestor = Original_Type then
18690 return True;
18691
18692 -- The ancestor may have a partial view of the original type,
18693 -- but if the full view is in scope, as in a child body, the
18694 -- component is visible.
18695
18696 elsif In_Private_Part (Scope (Original_Type))
18697 and then Full_View (Ancestor) = Original_Type
18698 then
18699 return True;
18700
18701 elsif Ancestor = Etype (Ancestor) then
18702
18703 -- No further ancestors to examine
18704
18705 return False;
18706 end if;
18707
18708 Ancestor := Etype (Ancestor);
18709 end loop;
18710 end;
18711 end if;
18712 end Is_Visible_Component;
18713
18714 --------------------------
18715 -- Make_Class_Wide_Type --
18716 --------------------------
18717
18718 procedure Make_Class_Wide_Type (T : Entity_Id) is
18719 CW_Type : Entity_Id;
18720 CW_Name : Name_Id;
18721 Next_E : Entity_Id;
18722
18723 begin
18724 if Present (Class_Wide_Type (T)) then
18725
18726 -- The class-wide type is a partially decorated entity created for a
18727 -- unanalyzed tagged type referenced through a limited with clause.
18728 -- When the tagged type is analyzed, its class-wide type needs to be
18729 -- redecorated. Note that we reuse the entity created by Decorate_
18730 -- Tagged_Type in order to preserve all links.
18731
18732 if Materialize_Entity (Class_Wide_Type (T)) then
18733 CW_Type := Class_Wide_Type (T);
18734 Set_Materialize_Entity (CW_Type, False);
18735
18736 -- The class wide type can have been defined by the partial view, in
18737 -- which case everything is already done.
18738
18739 else
18740 return;
18741 end if;
18742
18743 -- Default case, we need to create a new class-wide type
18744
18745 else
18746 CW_Type :=
18747 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18748 end if;
18749
18750 -- Inherit root type characteristics
18751
18752 CW_Name := Chars (CW_Type);
18753 Next_E := Next_Entity (CW_Type);
18754 Copy_Node (T, CW_Type);
18755 Set_Comes_From_Source (CW_Type, False);
18756 Set_Chars (CW_Type, CW_Name);
18757 Set_Parent (CW_Type, Parent (T));
18758 Set_Next_Entity (CW_Type, Next_E);
18759
18760 -- Ensure we have a new freeze node for the class-wide type. The partial
18761 -- view may have freeze action of its own, requiring a proper freeze
18762 -- node, and the same freeze node cannot be shared between the two
18763 -- types.
18764
18765 Set_Has_Delayed_Freeze (CW_Type);
18766 Set_Freeze_Node (CW_Type, Empty);
18767
18768 -- Customize the class-wide type: It has no prim. op., it cannot be
18769 -- abstract, its Etype points back to the specific root type, and it
18770 -- cannot have any invariants.
18771
18772 Set_Ekind (CW_Type, E_Class_Wide_Type);
18773 Set_Is_Tagged_Type (CW_Type, True);
18774 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18775 Set_Is_Abstract_Type (CW_Type, False);
18776 Set_Is_Constrained (CW_Type, False);
18777 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18778 Set_Default_SSO (CW_Type);
18779 Set_Has_Inheritable_Invariants (CW_Type, False);
18780 Set_Has_Inherited_Invariants (CW_Type, False);
18781 Set_Has_Own_Invariants (CW_Type, False);
18782
18783 if Ekind (T) = E_Class_Wide_Subtype then
18784 Set_Etype (CW_Type, Etype (Base_Type (T)));
18785 else
18786 Set_Etype (CW_Type, T);
18787 end if;
18788
18789 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18790
18791 -- If this is the class_wide type of a constrained subtype, it does
18792 -- not have discriminants.
18793
18794 Set_Has_Discriminants (CW_Type,
18795 Has_Discriminants (T) and then not Is_Constrained (T));
18796
18797 Set_Has_Unknown_Discriminants (CW_Type, True);
18798 Set_Class_Wide_Type (T, CW_Type);
18799 Set_Equivalent_Type (CW_Type, Empty);
18800
18801 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18802
18803 Set_Class_Wide_Type (CW_Type, CW_Type);
18804 end Make_Class_Wide_Type;
18805
18806 ----------------
18807 -- Make_Index --
18808 ----------------
18809
18810 procedure Make_Index
18811 (N : Node_Id;
18812 Related_Nod : Node_Id;
18813 Related_Id : Entity_Id := Empty;
18814 Suffix_Index : Nat := 1;
18815 In_Iter_Schm : Boolean := False)
18816 is
18817 R : Node_Id;
18818 T : Entity_Id;
18819 Def_Id : Entity_Id := Empty;
18820 Found : Boolean := False;
18821
18822 begin
18823 -- For a discrete range used in a constrained array definition and
18824 -- defined by a range, an implicit conversion to the predefined type
18825 -- INTEGER is assumed if each bound is either a numeric literal, a named
18826 -- number, or an attribute, and the type of both bounds (prior to the
18827 -- implicit conversion) is the type universal_integer. Otherwise, both
18828 -- bounds must be of the same discrete type, other than universal
18829 -- integer; this type must be determinable independently of the
18830 -- context, but using the fact that the type must be discrete and that
18831 -- both bounds must have the same type.
18832
18833 -- Character literals also have a universal type in the absence of
18834 -- of additional context, and are resolved to Standard_Character.
18835
18836 if Nkind (N) = N_Range then
18837
18838 -- The index is given by a range constraint. The bounds are known
18839 -- to be of a consistent type.
18840
18841 if not Is_Overloaded (N) then
18842 T := Etype (N);
18843
18844 -- For universal bounds, choose the specific predefined type
18845
18846 if T = Universal_Integer then
18847 T := Standard_Integer;
18848
18849 elsif T = Any_Character then
18850 Ambiguous_Character (Low_Bound (N));
18851
18852 T := Standard_Character;
18853 end if;
18854
18855 -- The node may be overloaded because some user-defined operators
18856 -- are available, but if a universal interpretation exists it is
18857 -- also the selected one.
18858
18859 elsif Universal_Interpretation (N) = Universal_Integer then
18860 T := Standard_Integer;
18861
18862 else
18863 T := Any_Type;
18864
18865 declare
18866 Ind : Interp_Index;
18867 It : Interp;
18868
18869 begin
18870 Get_First_Interp (N, Ind, It);
18871 while Present (It.Typ) loop
18872 if Is_Discrete_Type (It.Typ) then
18873
18874 if Found
18875 and then not Covers (It.Typ, T)
18876 and then not Covers (T, It.Typ)
18877 then
18878 Error_Msg_N ("ambiguous bounds in discrete range", N);
18879 exit;
18880 else
18881 T := It.Typ;
18882 Found := True;
18883 end if;
18884 end if;
18885
18886 Get_Next_Interp (Ind, It);
18887 end loop;
18888
18889 if T = Any_Type then
18890 Error_Msg_N ("discrete type required for range", N);
18891 Set_Etype (N, Any_Type);
18892 return;
18893
18894 elsif T = Universal_Integer then
18895 T := Standard_Integer;
18896 end if;
18897 end;
18898 end if;
18899
18900 if not Is_Discrete_Type (T) then
18901 Error_Msg_N ("discrete type required for range", N);
18902 Set_Etype (N, Any_Type);
18903 return;
18904 end if;
18905
18906 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18907 and then Attribute_Name (Low_Bound (N)) = Name_First
18908 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18909 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18910 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18911 then
18912 -- The type of the index will be the type of the prefix, as long
18913 -- as the upper bound is 'Last of the same type.
18914
18915 Def_Id := Entity (Prefix (Low_Bound (N)));
18916
18917 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18918 or else Attribute_Name (High_Bound (N)) /= Name_Last
18919 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18920 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18921 then
18922 Def_Id := Empty;
18923 end if;
18924 end if;
18925
18926 R := N;
18927 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18928
18929 elsif Nkind (N) = N_Subtype_Indication then
18930
18931 -- The index is given by a subtype with a range constraint
18932
18933 T := Base_Type (Entity (Subtype_Mark (N)));
18934
18935 if not Is_Discrete_Type (T) then
18936 Error_Msg_N ("discrete type required for range", N);
18937 Set_Etype (N, Any_Type);
18938 return;
18939 end if;
18940
18941 R := Range_Expression (Constraint (N));
18942
18943 Resolve (R, T);
18944 Process_Range_Expr_In_Decl
18945 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18946
18947 elsif Nkind (N) = N_Attribute_Reference then
18948
18949 -- Catch beginner's error (use of attribute other than 'Range)
18950
18951 if Attribute_Name (N) /= Name_Range then
18952 Error_Msg_N ("expect attribute ''Range", N);
18953 Set_Etype (N, Any_Type);
18954 return;
18955 end if;
18956
18957 -- If the node denotes the range of a type mark, that is also the
18958 -- resulting type, and we do not need to create an Itype for it.
18959
18960 if Is_Entity_Name (Prefix (N))
18961 and then Comes_From_Source (N)
18962 and then Is_Type (Entity (Prefix (N)))
18963 and then Is_Discrete_Type (Entity (Prefix (N)))
18964 then
18965 Def_Id := Entity (Prefix (N));
18966 end if;
18967
18968 Analyze_And_Resolve (N);
18969 T := Etype (N);
18970 R := N;
18971
18972 -- If none of the above, must be a subtype. We convert this to a
18973 -- range attribute reference because in the case of declared first
18974 -- named subtypes, the types in the range reference can be different
18975 -- from the type of the entity. A range attribute normalizes the
18976 -- reference and obtains the correct types for the bounds.
18977
18978 -- This transformation is in the nature of an expansion, is only
18979 -- done if expansion is active. In particular, it is not done on
18980 -- formal generic types, because we need to retain the name of the
18981 -- original index for instantiation purposes.
18982
18983 else
18984 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18985 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18986 Set_Etype (N, Any_Integer);
18987 return;
18988
18989 else
18990 -- The type mark may be that of an incomplete type. It is only
18991 -- now that we can get the full view, previous analysis does
18992 -- not look specifically for a type mark.
18993
18994 Set_Entity (N, Get_Full_View (Entity (N)));
18995 Set_Etype (N, Entity (N));
18996 Def_Id := Entity (N);
18997
18998 if not Is_Discrete_Type (Def_Id) then
18999 Error_Msg_N ("discrete type required for index", N);
19000 Set_Etype (N, Any_Type);
19001 return;
19002 end if;
19003 end if;
19004
19005 if Expander_Active then
19006 Rewrite (N,
19007 Make_Attribute_Reference (Sloc (N),
19008 Attribute_Name => Name_Range,
19009 Prefix => Relocate_Node (N)));
19010
19011 -- The original was a subtype mark that does not freeze. This
19012 -- means that the rewritten version must not freeze either.
19013
19014 Set_Must_Not_Freeze (N);
19015 Set_Must_Not_Freeze (Prefix (N));
19016 Analyze_And_Resolve (N);
19017 T := Etype (N);
19018 R := N;
19019
19020 -- If expander is inactive, type is legal, nothing else to construct
19021
19022 else
19023 return;
19024 end if;
19025 end if;
19026
19027 if not Is_Discrete_Type (T) then
19028 Error_Msg_N ("discrete type required for range", N);
19029 Set_Etype (N, Any_Type);
19030 return;
19031
19032 elsif T = Any_Type then
19033 Set_Etype (N, Any_Type);
19034 return;
19035 end if;
19036
19037 -- We will now create the appropriate Itype to describe the range, but
19038 -- first a check. If we originally had a subtype, then we just label
19039 -- the range with this subtype. Not only is there no need to construct
19040 -- a new subtype, but it is wrong to do so for two reasons:
19041
19042 -- 1. A legality concern, if we have a subtype, it must not freeze,
19043 -- and the Itype would cause freezing incorrectly
19044
19045 -- 2. An efficiency concern, if we created an Itype, it would not be
19046 -- recognized as the same type for the purposes of eliminating
19047 -- checks in some circumstances.
19048
19049 -- We signal this case by setting the subtype entity in Def_Id
19050
19051 if No (Def_Id) then
19052 Def_Id :=
19053 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19054 Set_Etype (Def_Id, Base_Type (T));
19055
19056 if Is_Signed_Integer_Type (T) then
19057 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19058
19059 elsif Is_Modular_Integer_Type (T) then
19060 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19061
19062 else
19063 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19064 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19065 Set_First_Literal (Def_Id, First_Literal (T));
19066 end if;
19067
19068 Set_Size_Info (Def_Id, (T));
19069 Set_RM_Size (Def_Id, RM_Size (T));
19070 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19071
19072 Set_Scalar_Range (Def_Id, R);
19073 Conditional_Delay (Def_Id, T);
19074
19075 if Nkind (N) = N_Subtype_Indication then
19076 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19077 end if;
19078
19079 -- In the subtype indication case, if the immediate parent of the
19080 -- new subtype is non-static, then the subtype we create is non-
19081 -- static, even if its bounds are static.
19082
19083 if Nkind (N) = N_Subtype_Indication
19084 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19085 then
19086 Set_Is_Non_Static_Subtype (Def_Id);
19087 end if;
19088 end if;
19089
19090 -- Final step is to label the index with this constructed type
19091
19092 Set_Etype (N, Def_Id);
19093 end Make_Index;
19094
19095 ------------------------------
19096 -- Modular_Type_Declaration --
19097 ------------------------------
19098
19099 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19100 Mod_Expr : constant Node_Id := Expression (Def);
19101 M_Val : Uint;
19102
19103 procedure Set_Modular_Size (Bits : Int);
19104 -- Sets RM_Size to Bits, and Esize to normal word size above this
19105
19106 ----------------------
19107 -- Set_Modular_Size --
19108 ----------------------
19109
19110 procedure Set_Modular_Size (Bits : Int) is
19111 begin
19112 Set_RM_Size (T, UI_From_Int (Bits));
19113
19114 if Bits <= 8 then
19115 Init_Esize (T, 8);
19116
19117 elsif Bits <= 16 then
19118 Init_Esize (T, 16);
19119
19120 elsif Bits <= 32 then
19121 Init_Esize (T, 32);
19122
19123 else
19124 Init_Esize (T, System_Max_Binary_Modulus_Power);
19125 end if;
19126
19127 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19128 Set_Is_Known_Valid (T);
19129 end if;
19130 end Set_Modular_Size;
19131
19132 -- Start of processing for Modular_Type_Declaration
19133
19134 begin
19135 -- If the mod expression is (exactly) 2 * literal, where literal is
19136 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19137
19138 if Warn_On_Suspicious_Modulus_Value
19139 and then Nkind (Mod_Expr) = N_Op_Multiply
19140 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19141 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19142 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19143 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19144 then
19145 Error_Msg_N
19146 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19147 end if;
19148
19149 -- Proceed with analysis of mod expression
19150
19151 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19152 Set_Etype (T, T);
19153 Set_Ekind (T, E_Modular_Integer_Type);
19154 Init_Alignment (T);
19155 Set_Is_Constrained (T);
19156
19157 if not Is_OK_Static_Expression (Mod_Expr) then
19158 Flag_Non_Static_Expr
19159 ("non-static expression used for modular type bound!", Mod_Expr);
19160 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19161 else
19162 M_Val := Expr_Value (Mod_Expr);
19163 end if;
19164
19165 if M_Val < 1 then
19166 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19167 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19168 end if;
19169
19170 if M_Val > 2 ** Standard_Long_Integer_Size then
19171 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19172 end if;
19173
19174 Set_Modulus (T, M_Val);
19175
19176 -- Create bounds for the modular type based on the modulus given in
19177 -- the type declaration and then analyze and resolve those bounds.
19178
19179 Set_Scalar_Range (T,
19180 Make_Range (Sloc (Mod_Expr),
19181 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19182 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19183
19184 -- Properly analyze the literals for the range. We do this manually
19185 -- because we can't go calling Resolve, since we are resolving these
19186 -- bounds with the type, and this type is certainly not complete yet.
19187
19188 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19189 Set_Etype (High_Bound (Scalar_Range (T)), T);
19190 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19191 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19192
19193 -- Loop through powers of two to find number of bits required
19194
19195 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19196
19197 -- Binary case
19198
19199 if M_Val = 2 ** Bits then
19200 Set_Modular_Size (Bits);
19201 return;
19202
19203 -- Nonbinary case
19204
19205 elsif M_Val < 2 ** Bits then
19206 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19207 Set_Non_Binary_Modulus (T);
19208
19209 if Bits > System_Max_Nonbinary_Modulus_Power then
19210 Error_Msg_Uint_1 :=
19211 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19212 Error_Msg_F
19213 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19214 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19215 return;
19216
19217 else
19218 -- In the nonbinary case, set size as per RM 13.3(55)
19219
19220 Set_Modular_Size (Bits);
19221 return;
19222 end if;
19223 end if;
19224
19225 end loop;
19226
19227 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19228 -- so we just signal an error and set the maximum size.
19229
19230 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19231 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19232
19233 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19234 Init_Alignment (T);
19235
19236 end Modular_Type_Declaration;
19237
19238 --------------------------
19239 -- New_Concatenation_Op --
19240 --------------------------
19241
19242 procedure New_Concatenation_Op (Typ : Entity_Id) is
19243 Loc : constant Source_Ptr := Sloc (Typ);
19244 Op : Entity_Id;
19245
19246 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19247 -- Create abbreviated declaration for the formal of a predefined
19248 -- Operator 'Op' of type 'Typ'
19249
19250 --------------------
19251 -- Make_Op_Formal --
19252 --------------------
19253
19254 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19255 Formal : Entity_Id;
19256 begin
19257 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19258 Set_Etype (Formal, Typ);
19259 Set_Mechanism (Formal, Default_Mechanism);
19260 return Formal;
19261 end Make_Op_Formal;
19262
19263 -- Start of processing for New_Concatenation_Op
19264
19265 begin
19266 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19267
19268 Set_Ekind (Op, E_Operator);
19269 Set_Scope (Op, Current_Scope);
19270 Set_Etype (Op, Typ);
19271 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19272 Set_Is_Immediately_Visible (Op);
19273 Set_Is_Intrinsic_Subprogram (Op);
19274 Set_Has_Completion (Op);
19275 Append_Entity (Op, Current_Scope);
19276
19277 Set_Name_Entity_Id (Name_Op_Concat, Op);
19278
19279 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19280 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19281 end New_Concatenation_Op;
19282
19283 -------------------------
19284 -- OK_For_Limited_Init --
19285 -------------------------
19286
19287 -- ???Check all calls of this, and compare the conditions under which it's
19288 -- called.
19289
19290 function OK_For_Limited_Init
19291 (Typ : Entity_Id;
19292 Exp : Node_Id) return Boolean
19293 is
19294 begin
19295 return Is_CPP_Constructor_Call (Exp)
19296 or else (Ada_Version >= Ada_2005
19297 and then not Debug_Flag_Dot_L
19298 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19299 end OK_For_Limited_Init;
19300
19301 -------------------------------
19302 -- OK_For_Limited_Init_In_05 --
19303 -------------------------------
19304
19305 function OK_For_Limited_Init_In_05
19306 (Typ : Entity_Id;
19307 Exp : Node_Id) return Boolean
19308 is
19309 begin
19310 -- An object of a limited interface type can be initialized with any
19311 -- expression of a nonlimited descendant type. However this does not
19312 -- apply if this is a view conversion of some other expression. This
19313 -- is checked below.
19314
19315 if Is_Class_Wide_Type (Typ)
19316 and then Is_Limited_Interface (Typ)
19317 and then not Is_Limited_Type (Etype (Exp))
19318 and then Nkind (Exp) /= N_Type_Conversion
19319 then
19320 return True;
19321 end if;
19322
19323 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19324 -- case of limited aggregates (including extension aggregates), and
19325 -- function calls. The function call may have been given in prefixed
19326 -- notation, in which case the original node is an indexed component.
19327 -- If the function is parameterless, the original node was an explicit
19328 -- dereference. The function may also be parameterless, in which case
19329 -- the source node is just an identifier.
19330
19331 -- A branch of a conditional expression may have been removed if the
19332 -- condition is statically known. This happens during expansion, and
19333 -- thus will not happen if previous errors were encountered. The check
19334 -- will have been performed on the chosen branch, which replaces the
19335 -- original conditional expression.
19336
19337 if No (Exp) then
19338 return True;
19339 end if;
19340
19341 case Nkind (Original_Node (Exp)) is
19342 when N_Aggregate
19343 | N_Extension_Aggregate
19344 | N_Function_Call
19345 | N_Op
19346 =>
19347 return True;
19348
19349 when N_Identifier =>
19350 return Present (Entity (Original_Node (Exp)))
19351 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19352
19353 when N_Qualified_Expression =>
19354 return
19355 OK_For_Limited_Init_In_05
19356 (Typ, Expression (Original_Node (Exp)));
19357
19358 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19359 -- with a function call, the expander has rewritten the call into an
19360 -- N_Type_Conversion node to force displacement of the pointer to
19361 -- reference the component containing the secondary dispatch table.
19362 -- Otherwise a type conversion is not a legal context.
19363 -- A return statement for a build-in-place function returning a
19364 -- synchronized type also introduces an unchecked conversion.
19365
19366 when N_Type_Conversion
19367 | N_Unchecked_Type_Conversion
19368 =>
19369 return not Comes_From_Source (Exp)
19370 and then
19371 OK_For_Limited_Init_In_05
19372 (Typ, Expression (Original_Node (Exp)));
19373
19374 when N_Explicit_Dereference
19375 | N_Indexed_Component
19376 | N_Selected_Component
19377 =>
19378 return Nkind (Exp) = N_Function_Call;
19379
19380 -- A use of 'Input is a function call, hence allowed. Normally the
19381 -- attribute will be changed to a call, but the attribute by itself
19382 -- can occur with -gnatc.
19383
19384 when N_Attribute_Reference =>
19385 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19386
19387 -- "return raise ..." is OK
19388
19389 when N_Raise_Expression =>
19390 return True;
19391
19392 -- For a case expression, all dependent expressions must be legal
19393
19394 when N_Case_Expression =>
19395 declare
19396 Alt : Node_Id;
19397
19398 begin
19399 Alt := First (Alternatives (Original_Node (Exp)));
19400 while Present (Alt) loop
19401 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19402 return False;
19403 end if;
19404
19405 Next (Alt);
19406 end loop;
19407
19408 return True;
19409 end;
19410
19411 -- For an if expression, all dependent expressions must be legal
19412
19413 when N_If_Expression =>
19414 declare
19415 Then_Expr : constant Node_Id :=
19416 Next (First (Expressions (Original_Node (Exp))));
19417 Else_Expr : constant Node_Id := Next (Then_Expr);
19418 begin
19419 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19420 and then
19421 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19422 end;
19423
19424 when others =>
19425 return False;
19426 end case;
19427 end OK_For_Limited_Init_In_05;
19428
19429 -------------------------------------------
19430 -- Ordinary_Fixed_Point_Type_Declaration --
19431 -------------------------------------------
19432
19433 procedure Ordinary_Fixed_Point_Type_Declaration
19434 (T : Entity_Id;
19435 Def : Node_Id)
19436 is
19437 Loc : constant Source_Ptr := Sloc (Def);
19438 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19439 RRS : constant Node_Id := Real_Range_Specification (Def);
19440 Implicit_Base : Entity_Id;
19441 Delta_Val : Ureal;
19442 Small_Val : Ureal;
19443 Low_Val : Ureal;
19444 High_Val : Ureal;
19445
19446 begin
19447 Check_Restriction (No_Fixed_Point, Def);
19448
19449 -- Create implicit base type
19450
19451 Implicit_Base :=
19452 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19453 Set_Etype (Implicit_Base, Implicit_Base);
19454
19455 -- Analyze and process delta expression
19456
19457 Analyze_And_Resolve (Delta_Expr, Any_Real);
19458
19459 Check_Delta_Expression (Delta_Expr);
19460 Delta_Val := Expr_Value_R (Delta_Expr);
19461
19462 Set_Delta_Value (Implicit_Base, Delta_Val);
19463
19464 -- Compute default small from given delta, which is the largest power
19465 -- of two that does not exceed the given delta value.
19466
19467 declare
19468 Tmp : Ureal;
19469 Scale : Int;
19470
19471 begin
19472 Tmp := Ureal_1;
19473 Scale := 0;
19474
19475 if Delta_Val < Ureal_1 then
19476 while Delta_Val < Tmp loop
19477 Tmp := Tmp / Ureal_2;
19478 Scale := Scale + 1;
19479 end loop;
19480
19481 else
19482 loop
19483 Tmp := Tmp * Ureal_2;
19484 exit when Tmp > Delta_Val;
19485 Scale := Scale - 1;
19486 end loop;
19487 end if;
19488
19489 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19490 end;
19491
19492 Set_Small_Value (Implicit_Base, Small_Val);
19493
19494 -- If no range was given, set a dummy range
19495
19496 if RRS <= Empty_Or_Error then
19497 Low_Val := -Small_Val;
19498 High_Val := Small_Val;
19499
19500 -- Otherwise analyze and process given range
19501
19502 else
19503 declare
19504 Low : constant Node_Id := Low_Bound (RRS);
19505 High : constant Node_Id := High_Bound (RRS);
19506
19507 begin
19508 Analyze_And_Resolve (Low, Any_Real);
19509 Analyze_And_Resolve (High, Any_Real);
19510 Check_Real_Bound (Low);
19511 Check_Real_Bound (High);
19512
19513 -- Obtain and set the range
19514
19515 Low_Val := Expr_Value_R (Low);
19516 High_Val := Expr_Value_R (High);
19517
19518 if Low_Val > High_Val then
19519 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19520 end if;
19521 end;
19522 end if;
19523
19524 -- The range for both the implicit base and the declared first subtype
19525 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19526 -- set a temporary range in place. Note that the bounds of the base
19527 -- type will be widened to be symmetrical and to fill the available
19528 -- bits when the type is frozen.
19529
19530 -- We could do this with all discrete types, and probably should, but
19531 -- we absolutely have to do it for fixed-point, since the end-points
19532 -- of the range and the size are determined by the small value, which
19533 -- could be reset before the freeze point.
19534
19535 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19536 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19537
19538 -- Complete definition of first subtype. The inheritance of the rep item
19539 -- chain ensures that SPARK-related pragmas are not clobbered when the
19540 -- ordinary fixed point type acts as a full view of a private type.
19541
19542 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19543 Set_Etype (T, Implicit_Base);
19544 Init_Size_Align (T);
19545 Inherit_Rep_Item_Chain (T, Implicit_Base);
19546 Set_Small_Value (T, Small_Val);
19547 Set_Delta_Value (T, Delta_Val);
19548 Set_Is_Constrained (T);
19549 end Ordinary_Fixed_Point_Type_Declaration;
19550
19551 ----------------------------------
19552 -- Preanalyze_Assert_Expression --
19553 ----------------------------------
19554
19555 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19556 begin
19557 In_Assertion_Expr := In_Assertion_Expr + 1;
19558 Preanalyze_Spec_Expression (N, T);
19559 In_Assertion_Expr := In_Assertion_Expr - 1;
19560 end Preanalyze_Assert_Expression;
19561
19562 -----------------------------------
19563 -- Preanalyze_Default_Expression --
19564 -----------------------------------
19565
19566 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19567 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19568 begin
19569 In_Default_Expr := True;
19570 Preanalyze_Spec_Expression (N, T);
19571 In_Default_Expr := Save_In_Default_Expr;
19572 end Preanalyze_Default_Expression;
19573
19574 --------------------------------
19575 -- Preanalyze_Spec_Expression --
19576 --------------------------------
19577
19578 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19579 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19580 begin
19581 In_Spec_Expression := True;
19582 Preanalyze_And_Resolve (N, T);
19583 In_Spec_Expression := Save_In_Spec_Expression;
19584 end Preanalyze_Spec_Expression;
19585
19586 ----------------------------------------
19587 -- Prepare_Private_Subtype_Completion --
19588 ----------------------------------------
19589
19590 procedure Prepare_Private_Subtype_Completion
19591 (Id : Entity_Id;
19592 Related_Nod : Node_Id)
19593 is
19594 Id_B : constant Entity_Id := Base_Type (Id);
19595 Full_B : Entity_Id := Full_View (Id_B);
19596 Full : Entity_Id;
19597
19598 begin
19599 if Present (Full_B) then
19600
19601 -- Get to the underlying full view if necessary
19602
19603 if Is_Private_Type (Full_B)
19604 and then Present (Underlying_Full_View (Full_B))
19605 then
19606 Full_B := Underlying_Full_View (Full_B);
19607 end if;
19608
19609 -- The Base_Type is already completed, we can complete the subtype
19610 -- now. We have to create a new entity with the same name, Thus we
19611 -- can't use Create_Itype.
19612
19613 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19614 Set_Is_Itype (Full);
19615 Set_Associated_Node_For_Itype (Full, Related_Nod);
19616 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19617 end if;
19618
19619 -- The parent subtype may be private, but the base might not, in some
19620 -- nested instances. In that case, the subtype does not need to be
19621 -- exchanged. It would still be nice to make private subtypes and their
19622 -- bases consistent at all times ???
19623
19624 if Is_Private_Type (Id_B) then
19625 Append_Elmt (Id, Private_Dependents (Id_B));
19626 end if;
19627 end Prepare_Private_Subtype_Completion;
19628
19629 ---------------------------
19630 -- Process_Discriminants --
19631 ---------------------------
19632
19633 procedure Process_Discriminants
19634 (N : Node_Id;
19635 Prev : Entity_Id := Empty)
19636 is
19637 Elist : constant Elist_Id := New_Elmt_List;
19638 Id : Node_Id;
19639 Discr : Node_Id;
19640 Discr_Number : Uint;
19641 Discr_Type : Entity_Id;
19642 Default_Present : Boolean := False;
19643 Default_Not_Present : Boolean := False;
19644
19645 begin
19646 -- A composite type other than an array type can have discriminants.
19647 -- On entry, the current scope is the composite type.
19648
19649 -- The discriminants are initially entered into the scope of the type
19650 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19651 -- use, as explained at the end of this procedure.
19652
19653 Discr := First (Discriminant_Specifications (N));
19654 while Present (Discr) loop
19655 Enter_Name (Defining_Identifier (Discr));
19656
19657 -- For navigation purposes we add a reference to the discriminant
19658 -- in the entity for the type. If the current declaration is a
19659 -- completion, place references on the partial view. Otherwise the
19660 -- type is the current scope.
19661
19662 if Present (Prev) then
19663
19664 -- The references go on the partial view, if present. If the
19665 -- partial view has discriminants, the references have been
19666 -- generated already.
19667
19668 if not Has_Discriminants (Prev) then
19669 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19670 end if;
19671 else
19672 Generate_Reference
19673 (Current_Scope, Defining_Identifier (Discr), 'd');
19674 end if;
19675
19676 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19677 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19678
19679 -- Ada 2005 (AI-254)
19680
19681 if Present (Access_To_Subprogram_Definition
19682 (Discriminant_Type (Discr)))
19683 and then Protected_Present (Access_To_Subprogram_Definition
19684 (Discriminant_Type (Discr)))
19685 then
19686 Discr_Type :=
19687 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19688 end if;
19689
19690 else
19691 Find_Type (Discriminant_Type (Discr));
19692 Discr_Type := Etype (Discriminant_Type (Discr));
19693
19694 if Error_Posted (Discriminant_Type (Discr)) then
19695 Discr_Type := Any_Type;
19696 end if;
19697 end if;
19698
19699 -- Handling of discriminants that are access types
19700
19701 if Is_Access_Type (Discr_Type) then
19702
19703 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19704 -- limited record types
19705
19706 if Ada_Version < Ada_2005 then
19707 Check_Access_Discriminant_Requires_Limited
19708 (Discr, Discriminant_Type (Discr));
19709 end if;
19710
19711 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19712 Error_Msg_N
19713 ("(Ada 83) access discriminant not allowed", Discr);
19714 end if;
19715
19716 -- If not access type, must be a discrete type
19717
19718 elsif not Is_Discrete_Type (Discr_Type) then
19719 Error_Msg_N
19720 ("discriminants must have a discrete or access type",
19721 Discriminant_Type (Discr));
19722 end if;
19723
19724 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19725
19726 -- If a discriminant specification includes the assignment compound
19727 -- delimiter followed by an expression, the expression is the default
19728 -- expression of the discriminant; the default expression must be of
19729 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19730 -- a default expression, we do the special preanalysis, since this
19731 -- expression does not freeze (see section "Handling of Default and
19732 -- Per-Object Expressions" in spec of package Sem).
19733
19734 if Present (Expression (Discr)) then
19735 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19736
19737 -- Legaity checks
19738
19739 if Nkind (N) = N_Formal_Type_Declaration then
19740 Error_Msg_N
19741 ("discriminant defaults not allowed for formal type",
19742 Expression (Discr));
19743
19744 -- Flag an error for a tagged type with defaulted discriminants,
19745 -- excluding limited tagged types when compiling for Ada 2012
19746 -- (see AI05-0214).
19747
19748 elsif Is_Tagged_Type (Current_Scope)
19749 and then (not Is_Limited_Type (Current_Scope)
19750 or else Ada_Version < Ada_2012)
19751 and then Comes_From_Source (N)
19752 then
19753 -- Note: see similar test in Check_Or_Process_Discriminants, to
19754 -- handle the (illegal) case of the completion of an untagged
19755 -- view with discriminants with defaults by a tagged full view.
19756 -- We skip the check if Discr does not come from source, to
19757 -- account for the case of an untagged derived type providing
19758 -- defaults for a renamed discriminant from a private untagged
19759 -- ancestor with a tagged full view (ACATS B460006).
19760
19761 if Ada_Version >= Ada_2012 then
19762 Error_Msg_N
19763 ("discriminants of nonlimited tagged type cannot have"
19764 & " defaults",
19765 Expression (Discr));
19766 else
19767 Error_Msg_N
19768 ("discriminants of tagged type cannot have defaults",
19769 Expression (Discr));
19770 end if;
19771
19772 else
19773 Default_Present := True;
19774 Append_Elmt (Expression (Discr), Elist);
19775
19776 -- Tag the defining identifiers for the discriminants with
19777 -- their corresponding default expressions from the tree.
19778
19779 Set_Discriminant_Default_Value
19780 (Defining_Identifier (Discr), Expression (Discr));
19781 end if;
19782
19783 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19784 -- gets set unless we can be sure that no range check is required.
19785
19786 if (GNATprove_Mode or not Expander_Active)
19787 and then not
19788 Is_In_Range
19789 (Expression (Discr), Discr_Type, Assume_Valid => True)
19790 then
19791 Set_Do_Range_Check (Expression (Discr));
19792 end if;
19793
19794 -- No default discriminant value given
19795
19796 else
19797 Default_Not_Present := True;
19798 end if;
19799
19800 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19801 -- Discr_Type but with the null-exclusion attribute
19802
19803 if Ada_Version >= Ada_2005 then
19804
19805 -- Ada 2005 (AI-231): Static checks
19806
19807 if Can_Never_Be_Null (Discr_Type) then
19808 Null_Exclusion_Static_Checks (Discr);
19809
19810 elsif Is_Access_Type (Discr_Type)
19811 and then Null_Exclusion_Present (Discr)
19812
19813 -- No need to check itypes because in their case this check
19814 -- was done at their point of creation
19815
19816 and then not Is_Itype (Discr_Type)
19817 then
19818 if Can_Never_Be_Null (Discr_Type) then
19819 Error_Msg_NE
19820 ("`NOT NULL` not allowed (& already excludes null)",
19821 Discr,
19822 Discr_Type);
19823 end if;
19824
19825 Set_Etype (Defining_Identifier (Discr),
19826 Create_Null_Excluding_Itype
19827 (T => Discr_Type,
19828 Related_Nod => Discr));
19829
19830 -- Check for improper null exclusion if the type is otherwise
19831 -- legal for a discriminant.
19832
19833 elsif Null_Exclusion_Present (Discr)
19834 and then Is_Discrete_Type (Discr_Type)
19835 then
19836 Error_Msg_N
19837 ("null exclusion can only apply to an access type", Discr);
19838 end if;
19839
19840 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19841 -- can't have defaults. Synchronized types, or types that are
19842 -- explicitly limited are fine, but special tests apply to derived
19843 -- types in generics: in a generic body we have to assume the
19844 -- worst, and therefore defaults are not allowed if the parent is
19845 -- a generic formal private type (see ACATS B370001).
19846
19847 if Is_Access_Type (Discr_Type) and then Default_Present then
19848 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19849 or else Is_Limited_Record (Current_Scope)
19850 or else Is_Concurrent_Type (Current_Scope)
19851 or else Is_Concurrent_Record_Type (Current_Scope)
19852 or else Ekind (Current_Scope) = E_Limited_Private_Type
19853 then
19854 if not Is_Derived_Type (Current_Scope)
19855 or else not Is_Generic_Type (Etype (Current_Scope))
19856 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19857 or else Limited_Present
19858 (Type_Definition (Parent (Current_Scope)))
19859 then
19860 null;
19861
19862 else
19863 Error_Msg_N
19864 ("access discriminants of nonlimited types cannot "
19865 & "have defaults", Expression (Discr));
19866 end if;
19867
19868 elsif Present (Expression (Discr)) then
19869 Error_Msg_N
19870 ("(Ada 2005) access discriminants of nonlimited types "
19871 & "cannot have defaults", Expression (Discr));
19872 end if;
19873 end if;
19874 end if;
19875
19876 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19877 -- This check is relevant only when SPARK_Mode is on as it is not a
19878 -- standard Ada legality rule.
19879
19880 if SPARK_Mode = On
19881 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19882 then
19883 Error_Msg_N ("discriminant cannot be volatile", Discr);
19884 end if;
19885
19886 Next (Discr);
19887 end loop;
19888
19889 -- An element list consisting of the default expressions of the
19890 -- discriminants is constructed in the above loop and used to set
19891 -- the Discriminant_Constraint attribute for the type. If an object
19892 -- is declared of this (record or task) type without any explicit
19893 -- discriminant constraint given, this element list will form the
19894 -- actual parameters for the corresponding initialization procedure
19895 -- for the type.
19896
19897 Set_Discriminant_Constraint (Current_Scope, Elist);
19898 Set_Stored_Constraint (Current_Scope, No_Elist);
19899
19900 -- Default expressions must be provided either for all or for none
19901 -- of the discriminants of a discriminant part. (RM 3.7.1)
19902
19903 if Default_Present and then Default_Not_Present then
19904 Error_Msg_N
19905 ("incomplete specification of defaults for discriminants", N);
19906 end if;
19907
19908 -- The use of the name of a discriminant is not allowed in default
19909 -- expressions of a discriminant part if the specification of the
19910 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19911
19912 -- To detect this, the discriminant names are entered initially with an
19913 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19914 -- attempt to use a void entity (for example in an expression that is
19915 -- type-checked) produces the error message: premature usage. Now after
19916 -- completing the semantic analysis of the discriminant part, we can set
19917 -- the Ekind of all the discriminants appropriately.
19918
19919 Discr := First (Discriminant_Specifications (N));
19920 Discr_Number := Uint_1;
19921 while Present (Discr) loop
19922 Id := Defining_Identifier (Discr);
19923 Set_Ekind (Id, E_Discriminant);
19924 Init_Component_Location (Id);
19925 Init_Esize (Id);
19926 Set_Discriminant_Number (Id, Discr_Number);
19927
19928 -- Make sure this is always set, even in illegal programs
19929
19930 Set_Corresponding_Discriminant (Id, Empty);
19931
19932 -- Initialize the Original_Record_Component to the entity itself.
19933 -- Inherit_Components will propagate the right value to
19934 -- discriminants in derived record types.
19935
19936 Set_Original_Record_Component (Id, Id);
19937
19938 -- Create the discriminal for the discriminant
19939
19940 Build_Discriminal (Id);
19941
19942 Next (Discr);
19943 Discr_Number := Discr_Number + 1;
19944 end loop;
19945
19946 Set_Has_Discriminants (Current_Scope);
19947 end Process_Discriminants;
19948
19949 -----------------------
19950 -- Process_Full_View --
19951 -----------------------
19952
19953 -- WARNING: This routine manages Ghost regions. Return statements must be
19954 -- replaced by gotos which jump to the end of the routine and restore the
19955 -- Ghost mode.
19956
19957 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19958 procedure Collect_Implemented_Interfaces
19959 (Typ : Entity_Id;
19960 Ifaces : Elist_Id);
19961 -- Ada 2005: Gather all the interfaces that Typ directly or
19962 -- inherently implements. Duplicate entries are not added to
19963 -- the list Ifaces.
19964
19965 ------------------------------------
19966 -- Collect_Implemented_Interfaces --
19967 ------------------------------------
19968
19969 procedure Collect_Implemented_Interfaces
19970 (Typ : Entity_Id;
19971 Ifaces : Elist_Id)
19972 is
19973 Iface : Entity_Id;
19974 Iface_Elmt : Elmt_Id;
19975
19976 begin
19977 -- Abstract interfaces are only associated with tagged record types
19978
19979 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19980 return;
19981 end if;
19982
19983 -- Recursively climb to the ancestors
19984
19985 if Etype (Typ) /= Typ
19986
19987 -- Protect the frontend against wrong cyclic declarations like:
19988
19989 -- type B is new A with private;
19990 -- type C is new A with private;
19991 -- private
19992 -- type B is new C with null record;
19993 -- type C is new B with null record;
19994
19995 and then Etype (Typ) /= Priv_T
19996 and then Etype (Typ) /= Full_T
19997 then
19998 -- Keep separate the management of private type declarations
19999
20000 if Ekind (Typ) = E_Record_Type_With_Private then
20001
20002 -- Handle the following illegal usage:
20003 -- type Private_Type is tagged private;
20004 -- private
20005 -- type Private_Type is new Type_Implementing_Iface;
20006
20007 if Present (Full_View (Typ))
20008 and then Etype (Typ) /= Full_View (Typ)
20009 then
20010 if Is_Interface (Etype (Typ)) then
20011 Append_Unique_Elmt (Etype (Typ), Ifaces);
20012 end if;
20013
20014 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20015 end if;
20016
20017 -- Non-private types
20018
20019 else
20020 if Is_Interface (Etype (Typ)) then
20021 Append_Unique_Elmt (Etype (Typ), Ifaces);
20022 end if;
20023
20024 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20025 end if;
20026 end if;
20027
20028 -- Handle entities in the list of abstract interfaces
20029
20030 if Present (Interfaces (Typ)) then
20031 Iface_Elmt := First_Elmt (Interfaces (Typ));
20032 while Present (Iface_Elmt) loop
20033 Iface := Node (Iface_Elmt);
20034
20035 pragma Assert (Is_Interface (Iface));
20036
20037 if not Contain_Interface (Iface, Ifaces) then
20038 Append_Elmt (Iface, Ifaces);
20039 Collect_Implemented_Interfaces (Iface, Ifaces);
20040 end if;
20041
20042 Next_Elmt (Iface_Elmt);
20043 end loop;
20044 end if;
20045 end Collect_Implemented_Interfaces;
20046
20047 -- Local variables
20048
20049 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20050
20051 Full_Indic : Node_Id;
20052 Full_Parent : Entity_Id;
20053 Priv_Parent : Entity_Id;
20054
20055 -- Start of processing for Process_Full_View
20056
20057 begin
20058 Mark_And_Set_Ghost_Completion (N, Priv_T);
20059
20060 -- First some sanity checks that must be done after semantic
20061 -- decoration of the full view and thus cannot be placed with other
20062 -- similar checks in Find_Type_Name
20063
20064 if not Is_Limited_Type (Priv_T)
20065 and then (Is_Limited_Type (Full_T)
20066 or else Is_Limited_Composite (Full_T))
20067 then
20068 if In_Instance then
20069 null;
20070 else
20071 Error_Msg_N
20072 ("completion of nonlimited type cannot be limited", Full_T);
20073 Explain_Limited_Type (Full_T, Full_T);
20074 end if;
20075
20076 elsif Is_Abstract_Type (Full_T)
20077 and then not Is_Abstract_Type (Priv_T)
20078 then
20079 Error_Msg_N
20080 ("completion of nonabstract type cannot be abstract", Full_T);
20081
20082 elsif Is_Tagged_Type (Priv_T)
20083 and then Is_Limited_Type (Priv_T)
20084 and then not Is_Limited_Type (Full_T)
20085 then
20086 -- If pragma CPP_Class was applied to the private declaration
20087 -- propagate the limitedness to the full-view
20088
20089 if Is_CPP_Class (Priv_T) then
20090 Set_Is_Limited_Record (Full_T);
20091
20092 -- GNAT allow its own definition of Limited_Controlled to disobey
20093 -- this rule in order in ease the implementation. This test is safe
20094 -- because Root_Controlled is defined in a child of System that
20095 -- normal programs are not supposed to use.
20096
20097 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20098 Set_Is_Limited_Composite (Full_T);
20099 else
20100 Error_Msg_N
20101 ("completion of limited tagged type must be limited", Full_T);
20102 end if;
20103
20104 elsif Is_Generic_Type (Priv_T) then
20105 Error_Msg_N ("generic type cannot have a completion", Full_T);
20106 end if;
20107
20108 -- Check that ancestor interfaces of private and full views are
20109 -- consistent. We omit this check for synchronized types because
20110 -- they are performed on the corresponding record type when frozen.
20111
20112 if Ada_Version >= Ada_2005
20113 and then Is_Tagged_Type (Priv_T)
20114 and then Is_Tagged_Type (Full_T)
20115 and then not Is_Concurrent_Type (Full_T)
20116 then
20117 declare
20118 Iface : Entity_Id;
20119 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20120 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20121
20122 begin
20123 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20124 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20125
20126 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20127 -- an interface type if and only if the full type is descendant
20128 -- of the interface type (AARM 7.3 (7.3/2)).
20129
20130 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20131
20132 if Present (Iface) then
20133 Error_Msg_NE
20134 ("interface in partial view& not implemented by full type "
20135 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20136 end if;
20137
20138 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20139
20140 if Present (Iface) then
20141 Error_Msg_NE
20142 ("interface & not implemented by partial view "
20143 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20144 end if;
20145 end;
20146 end if;
20147
20148 if Is_Tagged_Type (Priv_T)
20149 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20150 and then Is_Derived_Type (Full_T)
20151 then
20152 Priv_Parent := Etype (Priv_T);
20153
20154 -- The full view of a private extension may have been transformed
20155 -- into an unconstrained derived type declaration and a subtype
20156 -- declaration (see build_derived_record_type for details).
20157
20158 if Nkind (N) = N_Subtype_Declaration then
20159 Full_Indic := Subtype_Indication (N);
20160 Full_Parent := Etype (Base_Type (Full_T));
20161 else
20162 Full_Indic := Subtype_Indication (Type_Definition (N));
20163 Full_Parent := Etype (Full_T);
20164 end if;
20165
20166 -- Check that the parent type of the full type is a descendant of
20167 -- the ancestor subtype given in the private extension. If either
20168 -- entity has an Etype equal to Any_Type then we had some previous
20169 -- error situation [7.3(8)].
20170
20171 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20172 goto Leave;
20173
20174 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20175 -- any order. Therefore we don't have to check that its parent must
20176 -- be a descendant of the parent of the private type declaration.
20177
20178 elsif Is_Interface (Priv_Parent)
20179 and then Is_Interface (Full_Parent)
20180 then
20181 null;
20182
20183 -- Ada 2005 (AI-251): If the parent of the private type declaration
20184 -- is an interface there is no need to check that it is an ancestor
20185 -- of the associated full type declaration. The required tests for
20186 -- this case are performed by Build_Derived_Record_Type.
20187
20188 elsif not Is_Interface (Base_Type (Priv_Parent))
20189 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20190 then
20191 Error_Msg_N
20192 ("parent of full type must descend from parent of private "
20193 & "extension", Full_Indic);
20194
20195 -- First check a formal restriction, and then proceed with checking
20196 -- Ada rules. Since the formal restriction is not a serious error, we
20197 -- don't prevent further error detection for this check, hence the
20198 -- ELSE.
20199
20200 else
20201 -- In formal mode, when completing a private extension the type
20202 -- named in the private part must be exactly the same as that
20203 -- named in the visible part.
20204
20205 if Priv_Parent /= Full_Parent then
20206 Error_Msg_Name_1 := Chars (Priv_Parent);
20207 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20208 end if;
20209
20210 -- Check the rules of 7.3(10): if the private extension inherits
20211 -- known discriminants, then the full type must also inherit those
20212 -- discriminants from the same (ancestor) type, and the parent
20213 -- subtype of the full type must be constrained if and only if
20214 -- the ancestor subtype of the private extension is constrained.
20215
20216 if No (Discriminant_Specifications (Parent (Priv_T)))
20217 and then not Has_Unknown_Discriminants (Priv_T)
20218 and then Has_Discriminants (Base_Type (Priv_Parent))
20219 then
20220 declare
20221 Priv_Indic : constant Node_Id :=
20222 Subtype_Indication (Parent (Priv_T));
20223
20224 Priv_Constr : constant Boolean :=
20225 Is_Constrained (Priv_Parent)
20226 or else
20227 Nkind (Priv_Indic) = N_Subtype_Indication
20228 or else
20229 Is_Constrained (Entity (Priv_Indic));
20230
20231 Full_Constr : constant Boolean :=
20232 Is_Constrained (Full_Parent)
20233 or else
20234 Nkind (Full_Indic) = N_Subtype_Indication
20235 or else
20236 Is_Constrained (Entity (Full_Indic));
20237
20238 Priv_Discr : Entity_Id;
20239 Full_Discr : Entity_Id;
20240
20241 begin
20242 Priv_Discr := First_Discriminant (Priv_Parent);
20243 Full_Discr := First_Discriminant (Full_Parent);
20244 while Present (Priv_Discr) and then Present (Full_Discr) loop
20245 if Original_Record_Component (Priv_Discr) =
20246 Original_Record_Component (Full_Discr)
20247 or else
20248 Corresponding_Discriminant (Priv_Discr) =
20249 Corresponding_Discriminant (Full_Discr)
20250 then
20251 null;
20252 else
20253 exit;
20254 end if;
20255
20256 Next_Discriminant (Priv_Discr);
20257 Next_Discriminant (Full_Discr);
20258 end loop;
20259
20260 if Present (Priv_Discr) or else Present (Full_Discr) then
20261 Error_Msg_N
20262 ("full view must inherit discriminants of the parent "
20263 & "type used in the private extension", Full_Indic);
20264
20265 elsif Priv_Constr and then not Full_Constr then
20266 Error_Msg_N
20267 ("parent subtype of full type must be constrained",
20268 Full_Indic);
20269
20270 elsif Full_Constr and then not Priv_Constr then
20271 Error_Msg_N
20272 ("parent subtype of full type must be unconstrained",
20273 Full_Indic);
20274 end if;
20275 end;
20276
20277 -- Check the rules of 7.3(12): if a partial view has neither
20278 -- known or unknown discriminants, then the full type
20279 -- declaration shall define a definite subtype.
20280
20281 elsif not Has_Unknown_Discriminants (Priv_T)
20282 and then not Has_Discriminants (Priv_T)
20283 and then not Is_Constrained (Full_T)
20284 then
20285 Error_Msg_N
20286 ("full view must define a constrained type if partial view "
20287 & "has no discriminants", Full_T);
20288 end if;
20289
20290 -- ??????? Do we implement the following properly ?????
20291 -- If the ancestor subtype of a private extension has constrained
20292 -- discriminants, then the parent subtype of the full view shall
20293 -- impose a statically matching constraint on those discriminants
20294 -- [7.3(13)].
20295 end if;
20296
20297 else
20298 -- For untagged types, verify that a type without discriminants is
20299 -- not completed with an unconstrained type. A separate error message
20300 -- is produced if the full type has defaulted discriminants.
20301
20302 if Is_Definite_Subtype (Priv_T)
20303 and then not Is_Definite_Subtype (Full_T)
20304 then
20305 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20306 Error_Msg_NE
20307 ("full view of& not compatible with declaration#",
20308 Full_T, Priv_T);
20309
20310 if not Is_Tagged_Type (Full_T) then
20311 Error_Msg_N
20312 ("\one is constrained, the other unconstrained", Full_T);
20313 end if;
20314 end if;
20315 end if;
20316
20317 -- AI-419: verify that the use of "limited" is consistent
20318
20319 declare
20320 Orig_Decl : constant Node_Id := Original_Node (N);
20321
20322 begin
20323 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20324 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20325 and then Nkind
20326 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20327 then
20328 if not Limited_Present (Parent (Priv_T))
20329 and then not Synchronized_Present (Parent (Priv_T))
20330 and then Limited_Present (Type_Definition (Orig_Decl))
20331 then
20332 Error_Msg_N
20333 ("full view of non-limited extension cannot be limited", N);
20334
20335 -- Conversely, if the partial view carries the limited keyword,
20336 -- the full view must as well, even if it may be redundant.
20337
20338 elsif Limited_Present (Parent (Priv_T))
20339 and then not Limited_Present (Type_Definition (Orig_Decl))
20340 then
20341 Error_Msg_N
20342 ("full view of limited extension must be explicitly limited",
20343 N);
20344 end if;
20345 end if;
20346 end;
20347
20348 -- Ada 2005 (AI-443): A synchronized private extension must be
20349 -- completed by a task or protected type.
20350
20351 if Ada_Version >= Ada_2005
20352 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20353 and then Synchronized_Present (Parent (Priv_T))
20354 and then not Is_Concurrent_Type (Full_T)
20355 then
20356 Error_Msg_N ("full view of synchronized extension must " &
20357 "be synchronized type", N);
20358 end if;
20359
20360 -- Ada 2005 AI-363: if the full view has discriminants with
20361 -- defaults, it is illegal to declare constrained access subtypes
20362 -- whose designated type is the current type. This allows objects
20363 -- of the type that are declared in the heap to be unconstrained.
20364
20365 if not Has_Unknown_Discriminants (Priv_T)
20366 and then not Has_Discriminants (Priv_T)
20367 and then Has_Discriminants (Full_T)
20368 and then
20369 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20370 then
20371 Set_Has_Constrained_Partial_View (Full_T);
20372 Set_Has_Constrained_Partial_View (Priv_T);
20373 end if;
20374
20375 -- Create a full declaration for all its subtypes recorded in
20376 -- Private_Dependents and swap them similarly to the base type. These
20377 -- are subtypes that have been define before the full declaration of
20378 -- the private type. We also swap the entry in Private_Dependents list
20379 -- so we can properly restore the private view on exit from the scope.
20380
20381 declare
20382 Priv_Elmt : Elmt_Id;
20383 Priv_Scop : Entity_Id;
20384 Priv : Entity_Id;
20385 Full : Entity_Id;
20386
20387 begin
20388 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20389 while Present (Priv_Elmt) loop
20390 Priv := Node (Priv_Elmt);
20391 Priv_Scop := Scope (Priv);
20392
20393 if Ekind_In (Priv, E_Private_Subtype,
20394 E_Limited_Private_Subtype,
20395 E_Record_Subtype_With_Private)
20396 then
20397 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20398 Set_Is_Itype (Full);
20399 Set_Parent (Full, Parent (Priv));
20400 Set_Associated_Node_For_Itype (Full, N);
20401
20402 -- Now we need to complete the private subtype, but since the
20403 -- base type has already been swapped, we must also swap the
20404 -- subtypes (and thus, reverse the arguments in the call to
20405 -- Complete_Private_Subtype). Also note that we may need to
20406 -- re-establish the scope of the private subtype.
20407
20408 Copy_And_Swap (Priv, Full);
20409
20410 if not In_Open_Scopes (Priv_Scop) then
20411 Push_Scope (Priv_Scop);
20412
20413 else
20414 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20415
20416 Priv_Scop := Empty;
20417 end if;
20418
20419 Complete_Private_Subtype (Full, Priv, Full_T, N);
20420
20421 if Present (Priv_Scop) then
20422 Pop_Scope;
20423 end if;
20424
20425 Replace_Elmt (Priv_Elmt, Full);
20426 end if;
20427
20428 Next_Elmt (Priv_Elmt);
20429 end loop;
20430 end;
20431
20432 -- If the private view was tagged, copy the new primitive operations
20433 -- from the private view to the full view.
20434
20435 if Is_Tagged_Type (Full_T) then
20436 declare
20437 Disp_Typ : Entity_Id;
20438 Full_List : Elist_Id;
20439 Prim : Entity_Id;
20440 Prim_Elmt : Elmt_Id;
20441 Priv_List : Elist_Id;
20442
20443 function Contains
20444 (E : Entity_Id;
20445 L : Elist_Id) return Boolean;
20446 -- Determine whether list L contains element E
20447
20448 --------------
20449 -- Contains --
20450 --------------
20451
20452 function Contains
20453 (E : Entity_Id;
20454 L : Elist_Id) return Boolean
20455 is
20456 List_Elmt : Elmt_Id;
20457
20458 begin
20459 List_Elmt := First_Elmt (L);
20460 while Present (List_Elmt) loop
20461 if Node (List_Elmt) = E then
20462 return True;
20463 end if;
20464
20465 Next_Elmt (List_Elmt);
20466 end loop;
20467
20468 return False;
20469 end Contains;
20470
20471 -- Start of processing
20472
20473 begin
20474 if Is_Tagged_Type (Priv_T) then
20475 Priv_List := Primitive_Operations (Priv_T);
20476 Prim_Elmt := First_Elmt (Priv_List);
20477
20478 -- In the case of a concurrent type completing a private tagged
20479 -- type, primitives may have been declared in between the two
20480 -- views. These subprograms need to be wrapped the same way
20481 -- entries and protected procedures are handled because they
20482 -- cannot be directly shared by the two views.
20483
20484 if Is_Concurrent_Type (Full_T) then
20485 declare
20486 Conc_Typ : constant Entity_Id :=
20487 Corresponding_Record_Type (Full_T);
20488 Curr_Nod : Node_Id := Parent (Conc_Typ);
20489 Wrap_Spec : Node_Id;
20490
20491 begin
20492 while Present (Prim_Elmt) loop
20493 Prim := Node (Prim_Elmt);
20494
20495 if Comes_From_Source (Prim)
20496 and then not Is_Abstract_Subprogram (Prim)
20497 then
20498 Wrap_Spec :=
20499 Make_Subprogram_Declaration (Sloc (Prim),
20500 Specification =>
20501 Build_Wrapper_Spec
20502 (Subp_Id => Prim,
20503 Obj_Typ => Conc_Typ,
20504 Formals =>
20505 Parameter_Specifications
20506 (Parent (Prim))));
20507
20508 Insert_After (Curr_Nod, Wrap_Spec);
20509 Curr_Nod := Wrap_Spec;
20510
20511 Analyze (Wrap_Spec);
20512
20513 -- Remove the wrapper from visibility to avoid
20514 -- spurious conflict with the wrapped entity.
20515
20516 Set_Is_Immediately_Visible
20517 (Defining_Entity (Specification (Wrap_Spec)),
20518 False);
20519 end if;
20520
20521 Next_Elmt (Prim_Elmt);
20522 end loop;
20523
20524 goto Leave;
20525 end;
20526
20527 -- For non-concurrent types, transfer explicit primitives, but
20528 -- omit those inherited from the parent of the private view
20529 -- since they will be re-inherited later on.
20530
20531 else
20532 Full_List := Primitive_Operations (Full_T);
20533
20534 while Present (Prim_Elmt) loop
20535 Prim := Node (Prim_Elmt);
20536
20537 if Comes_From_Source (Prim)
20538 and then not Contains (Prim, Full_List)
20539 then
20540 Append_Elmt (Prim, Full_List);
20541 end if;
20542
20543 Next_Elmt (Prim_Elmt);
20544 end loop;
20545 end if;
20546
20547 -- Untagged private view
20548
20549 else
20550 Full_List := Primitive_Operations (Full_T);
20551
20552 -- In this case the partial view is untagged, so here we locate
20553 -- all of the earlier primitives that need to be treated as
20554 -- dispatching (those that appear between the two views). Note
20555 -- that these additional operations must all be new operations
20556 -- (any earlier operations that override inherited operations
20557 -- of the full view will already have been inserted in the
20558 -- primitives list, marked by Check_Operation_From_Private_View
20559 -- as dispatching. Note that implicit "/=" operators are
20560 -- excluded from being added to the primitives list since they
20561 -- shouldn't be treated as dispatching (tagged "/=" is handled
20562 -- specially).
20563
20564 Prim := Next_Entity (Full_T);
20565 while Present (Prim) and then Prim /= Priv_T loop
20566 if Ekind_In (Prim, E_Procedure, E_Function) then
20567 Disp_Typ := Find_Dispatching_Type (Prim);
20568
20569 if Disp_Typ = Full_T
20570 and then (Chars (Prim) /= Name_Op_Ne
20571 or else Comes_From_Source (Prim))
20572 then
20573 Check_Controlling_Formals (Full_T, Prim);
20574
20575 if not Is_Dispatching_Operation (Prim) then
20576 Append_Elmt (Prim, Full_List);
20577 Set_Is_Dispatching_Operation (Prim, True);
20578 Set_DT_Position_Value (Prim, No_Uint);
20579 end if;
20580
20581 elsif Is_Dispatching_Operation (Prim)
20582 and then Disp_Typ /= Full_T
20583 then
20584
20585 -- Verify that it is not otherwise controlled by a
20586 -- formal or a return value of type T.
20587
20588 Check_Controlling_Formals (Disp_Typ, Prim);
20589 end if;
20590 end if;
20591
20592 Next_Entity (Prim);
20593 end loop;
20594 end if;
20595
20596 -- For the tagged case, the two views can share the same primitive
20597 -- operations list and the same class-wide type. Update attributes
20598 -- of the class-wide type which depend on the full declaration.
20599
20600 if Is_Tagged_Type (Priv_T) then
20601 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20602 Set_Class_Wide_Type
20603 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20604
20605 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20606 end if;
20607 end;
20608 end if;
20609
20610 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20611
20612 if Known_To_Have_Preelab_Init (Priv_T) then
20613
20614 -- Case where there is a pragma Preelaborable_Initialization. We
20615 -- always allow this in predefined units, which is cheating a bit,
20616 -- but it means we don't have to struggle to meet the requirements in
20617 -- the RM for having Preelaborable Initialization. Otherwise we
20618 -- require that the type meets the RM rules. But we can't check that
20619 -- yet, because of the rule about overriding Initialize, so we simply
20620 -- set a flag that will be checked at freeze time.
20621
20622 if not In_Predefined_Unit (Full_T) then
20623 Set_Must_Have_Preelab_Init (Full_T);
20624 end if;
20625 end if;
20626
20627 -- If pragma CPP_Class was applied to the private type declaration,
20628 -- propagate it now to the full type declaration.
20629
20630 if Is_CPP_Class (Priv_T) then
20631 Set_Is_CPP_Class (Full_T);
20632 Set_Convention (Full_T, Convention_CPP);
20633
20634 -- Check that components of imported CPP types do not have default
20635 -- expressions.
20636
20637 Check_CPP_Type_Has_No_Defaults (Full_T);
20638 end if;
20639
20640 -- If the private view has user specified stream attributes, then so has
20641 -- the full view.
20642
20643 -- Why the test, how could these flags be already set in Full_T ???
20644
20645 if Has_Specified_Stream_Read (Priv_T) then
20646 Set_Has_Specified_Stream_Read (Full_T);
20647 end if;
20648
20649 if Has_Specified_Stream_Write (Priv_T) then
20650 Set_Has_Specified_Stream_Write (Full_T);
20651 end if;
20652
20653 if Has_Specified_Stream_Input (Priv_T) then
20654 Set_Has_Specified_Stream_Input (Full_T);
20655 end if;
20656
20657 if Has_Specified_Stream_Output (Priv_T) then
20658 Set_Has_Specified_Stream_Output (Full_T);
20659 end if;
20660
20661 -- Propagate Default_Initial_Condition-related attributes from the
20662 -- partial view to the full view and its base type.
20663
20664 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20665 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20666
20667 -- Propagate invariant-related attributes from the partial view to the
20668 -- full view and its base type.
20669
20670 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20671 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20672
20673 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20674 -- in the full view without advertising the inheritance in the partial
20675 -- view. This can only occur when the partial view has no parent type
20676 -- and the full view has an interface as a parent. Any other scenarios
20677 -- are illegal because implemented interfaces must match between the
20678 -- two views.
20679
20680 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20681 declare
20682 Full_Par : constant Entity_Id := Etype (Full_T);
20683 Priv_Par : constant Entity_Id := Etype (Priv_T);
20684
20685 begin
20686 if not Is_Interface (Priv_Par)
20687 and then Is_Interface (Full_Par)
20688 and then Has_Inheritable_Invariants (Full_Par)
20689 then
20690 Error_Msg_N
20691 ("hidden inheritance of class-wide type invariants not "
20692 & "allowed", N);
20693 end if;
20694 end;
20695 end if;
20696
20697 -- Propagate predicates to full type, and predicate function if already
20698 -- defined. It is not clear that this can actually happen? the partial
20699 -- view cannot be frozen yet, and the predicate function has not been
20700 -- built. Still it is a cheap check and seems safer to make it.
20701
20702 if Has_Predicates (Priv_T) then
20703 Set_Has_Predicates (Full_T);
20704
20705 if Present (Predicate_Function (Priv_T)) then
20706 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20707 end if;
20708 end if;
20709
20710 <<Leave>>
20711 Restore_Ghost_Mode (Saved_GM);
20712 end Process_Full_View;
20713
20714 -----------------------------------
20715 -- Process_Incomplete_Dependents --
20716 -----------------------------------
20717
20718 procedure Process_Incomplete_Dependents
20719 (N : Node_Id;
20720 Full_T : Entity_Id;
20721 Inc_T : Entity_Id)
20722 is
20723 Inc_Elmt : Elmt_Id;
20724 Priv_Dep : Entity_Id;
20725 New_Subt : Entity_Id;
20726
20727 Disc_Constraint : Elist_Id;
20728
20729 begin
20730 if No (Private_Dependents (Inc_T)) then
20731 return;
20732 end if;
20733
20734 -- Itypes that may be generated by the completion of an incomplete
20735 -- subtype are not used by the back-end and not attached to the tree.
20736 -- They are created only for constraint-checking purposes.
20737
20738 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20739 while Present (Inc_Elmt) loop
20740 Priv_Dep := Node (Inc_Elmt);
20741
20742 if Ekind (Priv_Dep) = E_Subprogram_Type then
20743
20744 -- An Access_To_Subprogram type may have a return type or a
20745 -- parameter type that is incomplete. Replace with the full view.
20746
20747 if Etype (Priv_Dep) = Inc_T then
20748 Set_Etype (Priv_Dep, Full_T);
20749 end if;
20750
20751 declare
20752 Formal : Entity_Id;
20753
20754 begin
20755 Formal := First_Formal (Priv_Dep);
20756 while Present (Formal) loop
20757 if Etype (Formal) = Inc_T then
20758 Set_Etype (Formal, Full_T);
20759 end if;
20760
20761 Next_Formal (Formal);
20762 end loop;
20763 end;
20764
20765 elsif Is_Overloadable (Priv_Dep) then
20766
20767 -- If a subprogram in the incomplete dependents list is primitive
20768 -- for a tagged full type then mark it as a dispatching operation,
20769 -- check whether it overrides an inherited subprogram, and check
20770 -- restrictions on its controlling formals. Note that a protected
20771 -- operation is never dispatching: only its wrapper operation
20772 -- (which has convention Ada) is.
20773
20774 if Is_Tagged_Type (Full_T)
20775 and then Is_Primitive (Priv_Dep)
20776 and then Convention (Priv_Dep) /= Convention_Protected
20777 then
20778 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20779 Set_Is_Dispatching_Operation (Priv_Dep);
20780 Check_Controlling_Formals (Full_T, Priv_Dep);
20781 end if;
20782
20783 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20784
20785 -- Can happen during processing of a body before the completion
20786 -- of a TA type. Ignore, because spec is also on dependent list.
20787
20788 return;
20789
20790 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20791 -- corresponding subtype of the full view.
20792
20793 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20794 Set_Subtype_Indication
20795 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20796 Set_Etype (Priv_Dep, Full_T);
20797 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20798 Set_Analyzed (Parent (Priv_Dep), False);
20799
20800 -- Reanalyze the declaration, suppressing the call to
20801 -- Enter_Name to avoid duplicate names.
20802
20803 Analyze_Subtype_Declaration
20804 (N => Parent (Priv_Dep),
20805 Skip => True);
20806
20807 -- Dependent is a subtype
20808
20809 else
20810 -- We build a new subtype indication using the full view of the
20811 -- incomplete parent. The discriminant constraints have been
20812 -- elaborated already at the point of the subtype declaration.
20813
20814 New_Subt := Create_Itype (E_Void, N);
20815
20816 if Has_Discriminants (Full_T) then
20817 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20818 else
20819 Disc_Constraint := No_Elist;
20820 end if;
20821
20822 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20823 Set_Full_View (Priv_Dep, New_Subt);
20824 end if;
20825
20826 Next_Elmt (Inc_Elmt);
20827 end loop;
20828 end Process_Incomplete_Dependents;
20829
20830 --------------------------------
20831 -- Process_Range_Expr_In_Decl --
20832 --------------------------------
20833
20834 procedure Process_Range_Expr_In_Decl
20835 (R : Node_Id;
20836 T : Entity_Id;
20837 Subtyp : Entity_Id := Empty;
20838 Check_List : List_Id := Empty_List;
20839 R_Check_Off : Boolean := False;
20840 In_Iter_Schm : Boolean := False)
20841 is
20842 Lo, Hi : Node_Id;
20843 R_Checks : Check_Result;
20844 Insert_Node : Node_Id;
20845 Def_Id : Entity_Id;
20846
20847 begin
20848 Analyze_And_Resolve (R, Base_Type (T));
20849
20850 if Nkind (R) = N_Range then
20851
20852 -- In SPARK, all ranges should be static, with the exception of the
20853 -- discrete type definition of a loop parameter specification.
20854
20855 if not In_Iter_Schm
20856 and then not Is_OK_Static_Range (R)
20857 then
20858 Check_SPARK_05_Restriction ("range should be static", R);
20859 end if;
20860
20861 Lo := Low_Bound (R);
20862 Hi := High_Bound (R);
20863
20864 -- Validity checks on the range of a quantified expression are
20865 -- delayed until the construct is transformed into a loop.
20866
20867 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20868 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20869 then
20870 null;
20871
20872 -- We need to ensure validity of the bounds here, because if we
20873 -- go ahead and do the expansion, then the expanded code will get
20874 -- analyzed with range checks suppressed and we miss the check.
20875
20876 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20877 -- the temporaries generated by routine Remove_Side_Effects by means
20878 -- of validity checks must use the same names. When a range appears
20879 -- in the parent of a generic, the range is processed with checks
20880 -- disabled as part of the generic context and with checks enabled
20881 -- for code generation purposes. This leads to link issues as the
20882 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20883 -- template sees the temporaries generated by Remove_Side_Effects.
20884
20885 else
20886 Validity_Check_Range (R, Subtyp);
20887 end if;
20888
20889 -- If there were errors in the declaration, try and patch up some
20890 -- common mistakes in the bounds. The cases handled are literals
20891 -- which are Integer where the expected type is Real and vice versa.
20892 -- These corrections allow the compilation process to proceed further
20893 -- along since some basic assumptions of the format of the bounds
20894 -- are guaranteed.
20895
20896 if Etype (R) = Any_Type then
20897 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20898 Rewrite (Lo,
20899 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20900
20901 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20902 Rewrite (Hi,
20903 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20904
20905 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20906 Rewrite (Lo,
20907 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20908
20909 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20910 Rewrite (Hi,
20911 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20912 end if;
20913
20914 Set_Etype (Lo, T);
20915 Set_Etype (Hi, T);
20916 end if;
20917
20918 -- If the bounds of the range have been mistakenly given as string
20919 -- literals (perhaps in place of character literals), then an error
20920 -- has already been reported, but we rewrite the string literal as a
20921 -- bound of the range's type to avoid blowups in later processing
20922 -- that looks at static values.
20923
20924 if Nkind (Lo) = N_String_Literal then
20925 Rewrite (Lo,
20926 Make_Attribute_Reference (Sloc (Lo),
20927 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20928 Attribute_Name => Name_First));
20929 Analyze_And_Resolve (Lo);
20930 end if;
20931
20932 if Nkind (Hi) = N_String_Literal then
20933 Rewrite (Hi,
20934 Make_Attribute_Reference (Sloc (Hi),
20935 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20936 Attribute_Name => Name_First));
20937 Analyze_And_Resolve (Hi);
20938 end if;
20939
20940 -- If bounds aren't scalar at this point then exit, avoiding
20941 -- problems with further processing of the range in this procedure.
20942
20943 if not Is_Scalar_Type (Etype (Lo)) then
20944 return;
20945 end if;
20946
20947 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20948 -- then range of the base type. Here we check whether the bounds
20949 -- are in the range of the subtype itself. Note that if the bounds
20950 -- represent the null range the Constraint_Error exception should
20951 -- not be raised.
20952
20953 -- ??? The following code should be cleaned up as follows
20954
20955 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20956 -- is done in the call to Range_Check (R, T); below
20957
20958 -- 2. The use of R_Check_Off should be investigated and possibly
20959 -- removed, this would clean up things a bit.
20960
20961 if Is_Null_Range (Lo, Hi) then
20962 null;
20963
20964 else
20965 -- Capture values of bounds and generate temporaries for them
20966 -- if needed, before applying checks, since checks may cause
20967 -- duplication of the expression without forcing evaluation.
20968
20969 -- The forced evaluation removes side effects from expressions,
20970 -- which should occur also in GNATprove mode. Otherwise, we end up
20971 -- with unexpected insertions of actions at places where this is
20972 -- not supposed to occur, e.g. on default parameters of a call.
20973
20974 if Expander_Active or GNATprove_Mode then
20975
20976 -- Call Force_Evaluation to create declarations as needed to
20977 -- deal with side effects, and also create typ_FIRST/LAST
20978 -- entities for bounds if we have a subtype name.
20979
20980 -- Note: we do this transformation even if expansion is not
20981 -- active if we are in GNATprove_Mode since the transformation
20982 -- is in general required to ensure that the resulting tree has
20983 -- proper Ada semantics.
20984
20985 Force_Evaluation
20986 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20987 Force_Evaluation
20988 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20989 end if;
20990
20991 -- We use a flag here instead of suppressing checks on the type
20992 -- because the type we check against isn't necessarily the place
20993 -- where we put the check.
20994
20995 if not R_Check_Off then
20996 R_Checks := Get_Range_Checks (R, T);
20997
20998 -- Look up tree to find an appropriate insertion point. We
20999 -- can't just use insert_actions because later processing
21000 -- depends on the insertion node. Prior to Ada 2012 the
21001 -- insertion point could only be a declaration or a loop, but
21002 -- quantified expressions can appear within any context in an
21003 -- expression, and the insertion point can be any statement,
21004 -- pragma, or declaration.
21005
21006 Insert_Node := Parent (R);
21007 while Present (Insert_Node) loop
21008 exit when
21009 Nkind (Insert_Node) in N_Declaration
21010 and then
21011 not Nkind_In
21012 (Insert_Node, N_Component_Declaration,
21013 N_Loop_Parameter_Specification,
21014 N_Function_Specification,
21015 N_Procedure_Specification);
21016
21017 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21018 or else Nkind (Insert_Node) in
21019 N_Statement_Other_Than_Procedure_Call
21020 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21021 N_Pragma);
21022
21023 Insert_Node := Parent (Insert_Node);
21024 end loop;
21025
21026 -- Why would Type_Decl not be present??? Without this test,
21027 -- short regression tests fail.
21028
21029 if Present (Insert_Node) then
21030
21031 -- Case of loop statement. Verify that the range is part
21032 -- of the subtype indication of the iteration scheme.
21033
21034 if Nkind (Insert_Node) = N_Loop_Statement then
21035 declare
21036 Indic : Node_Id;
21037
21038 begin
21039 Indic := Parent (R);
21040 while Present (Indic)
21041 and then Nkind (Indic) /= N_Subtype_Indication
21042 loop
21043 Indic := Parent (Indic);
21044 end loop;
21045
21046 if Present (Indic) then
21047 Def_Id := Etype (Subtype_Mark (Indic));
21048
21049 Insert_Range_Checks
21050 (R_Checks,
21051 Insert_Node,
21052 Def_Id,
21053 Sloc (Insert_Node),
21054 R,
21055 Do_Before => True);
21056 end if;
21057 end;
21058
21059 -- Insertion before a declaration. If the declaration
21060 -- includes discriminants, the list of applicable checks
21061 -- is given by the caller.
21062
21063 elsif Nkind (Insert_Node) in N_Declaration then
21064 Def_Id := Defining_Identifier (Insert_Node);
21065
21066 if (Ekind (Def_Id) = E_Record_Type
21067 and then Depends_On_Discriminant (R))
21068 or else
21069 (Ekind (Def_Id) = E_Protected_Type
21070 and then Has_Discriminants (Def_Id))
21071 then
21072 Append_Range_Checks
21073 (R_Checks,
21074 Check_List, Def_Id, Sloc (Insert_Node), R);
21075
21076 else
21077 Insert_Range_Checks
21078 (R_Checks,
21079 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21080
21081 end if;
21082
21083 -- Insertion before a statement. Range appears in the
21084 -- context of a quantified expression. Insertion will
21085 -- take place when expression is expanded.
21086
21087 else
21088 null;
21089 end if;
21090 end if;
21091 end if;
21092 end if;
21093
21094 -- Case of other than an explicit N_Range node
21095
21096 -- The forced evaluation removes side effects from expressions, which
21097 -- should occur also in GNATprove mode. Otherwise, we end up with
21098 -- unexpected insertions of actions at places where this is not
21099 -- supposed to occur, e.g. on default parameters of a call.
21100
21101 elsif Expander_Active or GNATprove_Mode then
21102 Get_Index_Bounds (R, Lo, Hi);
21103 Force_Evaluation (Lo);
21104 Force_Evaluation (Hi);
21105 end if;
21106 end Process_Range_Expr_In_Decl;
21107
21108 --------------------------------------
21109 -- Process_Real_Range_Specification --
21110 --------------------------------------
21111
21112 procedure Process_Real_Range_Specification (Def : Node_Id) is
21113 Spec : constant Node_Id := Real_Range_Specification (Def);
21114 Lo : Node_Id;
21115 Hi : Node_Id;
21116 Err : Boolean := False;
21117
21118 procedure Analyze_Bound (N : Node_Id);
21119 -- Analyze and check one bound
21120
21121 -------------------
21122 -- Analyze_Bound --
21123 -------------------
21124
21125 procedure Analyze_Bound (N : Node_Id) is
21126 begin
21127 Analyze_And_Resolve (N, Any_Real);
21128
21129 if not Is_OK_Static_Expression (N) then
21130 Flag_Non_Static_Expr
21131 ("bound in real type definition is not static!", N);
21132 Err := True;
21133 end if;
21134 end Analyze_Bound;
21135
21136 -- Start of processing for Process_Real_Range_Specification
21137
21138 begin
21139 if Present (Spec) then
21140 Lo := Low_Bound (Spec);
21141 Hi := High_Bound (Spec);
21142 Analyze_Bound (Lo);
21143 Analyze_Bound (Hi);
21144
21145 -- If error, clear away junk range specification
21146
21147 if Err then
21148 Set_Real_Range_Specification (Def, Empty);
21149 end if;
21150 end if;
21151 end Process_Real_Range_Specification;
21152
21153 ---------------------
21154 -- Process_Subtype --
21155 ---------------------
21156
21157 function Process_Subtype
21158 (S : Node_Id;
21159 Related_Nod : Node_Id;
21160 Related_Id : Entity_Id := Empty;
21161 Suffix : Character := ' ') return Entity_Id
21162 is
21163 P : Node_Id;
21164 Def_Id : Entity_Id;
21165 Error_Node : Node_Id;
21166 Full_View_Id : Entity_Id;
21167 Subtype_Mark_Id : Entity_Id;
21168
21169 May_Have_Null_Exclusion : Boolean;
21170
21171 procedure Check_Incomplete (T : Node_Id);
21172 -- Called to verify that an incomplete type is not used prematurely
21173
21174 ----------------------
21175 -- Check_Incomplete --
21176 ----------------------
21177
21178 procedure Check_Incomplete (T : Node_Id) is
21179 begin
21180 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21181
21182 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21183 and then
21184 not (Ada_Version >= Ada_2005
21185 and then
21186 (Nkind (Parent (T)) = N_Subtype_Declaration
21187 or else (Nkind (Parent (T)) = N_Subtype_Indication
21188 and then Nkind (Parent (Parent (T))) =
21189 N_Subtype_Declaration)))
21190 then
21191 Error_Msg_N ("invalid use of type before its full declaration", T);
21192 end if;
21193 end Check_Incomplete;
21194
21195 -- Start of processing for Process_Subtype
21196
21197 begin
21198 -- Case of no constraints present
21199
21200 if Nkind (S) /= N_Subtype_Indication then
21201 Find_Type (S);
21202 Check_Incomplete (S);
21203 P := Parent (S);
21204
21205 -- Ada 2005 (AI-231): Static check
21206
21207 if Ada_Version >= Ada_2005
21208 and then Present (P)
21209 and then Null_Exclusion_Present (P)
21210 and then Nkind (P) /= N_Access_To_Object_Definition
21211 and then not Is_Access_Type (Entity (S))
21212 then
21213 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21214 end if;
21215
21216 -- The following is ugly, can't we have a range or even a flag???
21217
21218 May_Have_Null_Exclusion :=
21219 Nkind_In (P, N_Access_Definition,
21220 N_Access_Function_Definition,
21221 N_Access_Procedure_Definition,
21222 N_Access_To_Object_Definition,
21223 N_Allocator,
21224 N_Component_Definition)
21225 or else
21226 Nkind_In (P, N_Derived_Type_Definition,
21227 N_Discriminant_Specification,
21228 N_Formal_Object_Declaration,
21229 N_Object_Declaration,
21230 N_Object_Renaming_Declaration,
21231 N_Parameter_Specification,
21232 N_Subtype_Declaration);
21233
21234 -- Create an Itype that is a duplicate of Entity (S) but with the
21235 -- null-exclusion attribute.
21236
21237 if May_Have_Null_Exclusion
21238 and then Is_Access_Type (Entity (S))
21239 and then Null_Exclusion_Present (P)
21240
21241 -- No need to check the case of an access to object definition.
21242 -- It is correct to define double not-null pointers.
21243
21244 -- Example:
21245 -- type Not_Null_Int_Ptr is not null access Integer;
21246 -- type Acc is not null access Not_Null_Int_Ptr;
21247
21248 and then Nkind (P) /= N_Access_To_Object_Definition
21249 then
21250 if Can_Never_Be_Null (Entity (S)) then
21251 case Nkind (Related_Nod) is
21252 when N_Full_Type_Declaration =>
21253 if Nkind (Type_Definition (Related_Nod))
21254 in N_Array_Type_Definition
21255 then
21256 Error_Node :=
21257 Subtype_Indication
21258 (Component_Definition
21259 (Type_Definition (Related_Nod)));
21260 else
21261 Error_Node :=
21262 Subtype_Indication (Type_Definition (Related_Nod));
21263 end if;
21264
21265 when N_Subtype_Declaration =>
21266 Error_Node := Subtype_Indication (Related_Nod);
21267
21268 when N_Object_Declaration =>
21269 Error_Node := Object_Definition (Related_Nod);
21270
21271 when N_Component_Declaration =>
21272 Error_Node :=
21273 Subtype_Indication (Component_Definition (Related_Nod));
21274
21275 when N_Allocator =>
21276 Error_Node := Expression (Related_Nod);
21277
21278 when others =>
21279 pragma Assert (False);
21280 Error_Node := Related_Nod;
21281 end case;
21282
21283 Error_Msg_NE
21284 ("`NOT NULL` not allowed (& already excludes null)",
21285 Error_Node,
21286 Entity (S));
21287 end if;
21288
21289 Set_Etype (S,
21290 Create_Null_Excluding_Itype
21291 (T => Entity (S),
21292 Related_Nod => P));
21293 Set_Entity (S, Etype (S));
21294 end if;
21295
21296 return Entity (S);
21297
21298 -- Case of constraint present, so that we have an N_Subtype_Indication
21299 -- node (this node is created only if constraints are present).
21300
21301 else
21302 Find_Type (Subtype_Mark (S));
21303
21304 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21305 and then not
21306 (Nkind (Parent (S)) = N_Subtype_Declaration
21307 and then Is_Itype (Defining_Identifier (Parent (S))))
21308 then
21309 Check_Incomplete (Subtype_Mark (S));
21310 end if;
21311
21312 P := Parent (S);
21313 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21314
21315 -- Explicit subtype declaration case
21316
21317 if Nkind (P) = N_Subtype_Declaration then
21318 Def_Id := Defining_Identifier (P);
21319
21320 -- Explicit derived type definition case
21321
21322 elsif Nkind (P) = N_Derived_Type_Definition then
21323 Def_Id := Defining_Identifier (Parent (P));
21324
21325 -- Implicit case, the Def_Id must be created as an implicit type.
21326 -- The one exception arises in the case of concurrent types, array
21327 -- and access types, where other subsidiary implicit types may be
21328 -- created and must appear before the main implicit type. In these
21329 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21330 -- has not yet been called to create Def_Id.
21331
21332 else
21333 if Is_Array_Type (Subtype_Mark_Id)
21334 or else Is_Concurrent_Type (Subtype_Mark_Id)
21335 or else Is_Access_Type (Subtype_Mark_Id)
21336 then
21337 Def_Id := Empty;
21338
21339 -- For the other cases, we create a new unattached Itype,
21340 -- and set the indication to ensure it gets attached later.
21341
21342 else
21343 Def_Id :=
21344 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21345 end if;
21346 end if;
21347
21348 -- If the kind of constraint is invalid for this kind of type,
21349 -- then give an error, and then pretend no constraint was given.
21350
21351 if not Is_Valid_Constraint_Kind
21352 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21353 then
21354 Error_Msg_N
21355 ("incorrect constraint for this kind of type", Constraint (S));
21356
21357 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21358
21359 -- Set Ekind of orphan itype, to prevent cascaded errors
21360
21361 if Present (Def_Id) then
21362 Set_Ekind (Def_Id, Ekind (Any_Type));
21363 end if;
21364
21365 -- Make recursive call, having got rid of the bogus constraint
21366
21367 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21368 end if;
21369
21370 -- Remaining processing depends on type. Select on Base_Type kind to
21371 -- ensure getting to the concrete type kind in the case of a private
21372 -- subtype (needed when only doing semantic analysis).
21373
21374 case Ekind (Base_Type (Subtype_Mark_Id)) is
21375 when Access_Kind =>
21376
21377 -- If this is a constraint on a class-wide type, discard it.
21378 -- There is currently no way to express a partial discriminant
21379 -- constraint on a type with unknown discriminants. This is
21380 -- a pathology that the ACATS wisely decides not to test.
21381
21382 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21383 if Comes_From_Source (S) then
21384 Error_Msg_N
21385 ("constraint on class-wide type ignored??",
21386 Constraint (S));
21387 end if;
21388
21389 if Nkind (P) = N_Subtype_Declaration then
21390 Set_Subtype_Indication (P,
21391 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21392 end if;
21393
21394 return Subtype_Mark_Id;
21395 end if;
21396
21397 Constrain_Access (Def_Id, S, Related_Nod);
21398
21399 if Expander_Active
21400 and then Is_Itype (Designated_Type (Def_Id))
21401 and then Nkind (Related_Nod) = N_Subtype_Declaration
21402 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21403 then
21404 Build_Itype_Reference
21405 (Designated_Type (Def_Id), Related_Nod);
21406 end if;
21407
21408 when Array_Kind =>
21409 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21410
21411 when Decimal_Fixed_Point_Kind =>
21412 Constrain_Decimal (Def_Id, S);
21413
21414 when Enumeration_Kind =>
21415 Constrain_Enumeration (Def_Id, S);
21416 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21417
21418 when Ordinary_Fixed_Point_Kind =>
21419 Constrain_Ordinary_Fixed (Def_Id, S);
21420
21421 when Float_Kind =>
21422 Constrain_Float (Def_Id, S);
21423
21424 when Integer_Kind =>
21425 Constrain_Integer (Def_Id, S);
21426 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21427
21428 when Class_Wide_Kind
21429 | E_Incomplete_Type
21430 | E_Record_Subtype
21431 | E_Record_Type
21432 =>
21433 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21434
21435 if Ekind (Def_Id) = E_Incomplete_Type then
21436 Set_Private_Dependents (Def_Id, New_Elmt_List);
21437 end if;
21438
21439 when Private_Kind =>
21440 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21441
21442 -- The base type may be private but Def_Id may be a full view
21443 -- in an instance.
21444
21445 if Is_Private_Type (Def_Id) then
21446 Set_Private_Dependents (Def_Id, New_Elmt_List);
21447 end if;
21448
21449 -- In case of an invalid constraint prevent further processing
21450 -- since the type constructed is missing expected fields.
21451
21452 if Etype (Def_Id) = Any_Type then
21453 return Def_Id;
21454 end if;
21455
21456 -- If the full view is that of a task with discriminants,
21457 -- we must constrain both the concurrent type and its
21458 -- corresponding record type. Otherwise we will just propagate
21459 -- the constraint to the full view, if available.
21460
21461 if Present (Full_View (Subtype_Mark_Id))
21462 and then Has_Discriminants (Subtype_Mark_Id)
21463 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21464 then
21465 Full_View_Id :=
21466 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21467
21468 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21469 Constrain_Concurrent (Full_View_Id, S,
21470 Related_Nod, Related_Id, Suffix);
21471 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21472 Set_Full_View (Def_Id, Full_View_Id);
21473
21474 -- Introduce an explicit reference to the private subtype,
21475 -- to prevent scope anomalies in gigi if first use appears
21476 -- in a nested context, e.g. a later function body.
21477 -- Should this be generated in other contexts than a full
21478 -- type declaration?
21479
21480 if Is_Itype (Def_Id)
21481 and then
21482 Nkind (Parent (P)) = N_Full_Type_Declaration
21483 then
21484 Build_Itype_Reference (Def_Id, Parent (P));
21485 end if;
21486
21487 else
21488 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21489 end if;
21490
21491 when Concurrent_Kind =>
21492 Constrain_Concurrent (Def_Id, S,
21493 Related_Nod, Related_Id, Suffix);
21494
21495 when others =>
21496 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21497 end case;
21498
21499 -- Size and Convention are always inherited from the base type
21500
21501 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21502 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21503
21504 return Def_Id;
21505 end if;
21506 end Process_Subtype;
21507
21508 -----------------------------
21509 -- Record_Type_Declaration --
21510 -----------------------------
21511
21512 procedure Record_Type_Declaration
21513 (T : Entity_Id;
21514 N : Node_Id;
21515 Prev : Entity_Id)
21516 is
21517 Def : constant Node_Id := Type_Definition (N);
21518 Is_Tagged : Boolean;
21519 Tag_Comp : Entity_Id;
21520
21521 begin
21522 -- These flags must be initialized before calling Process_Discriminants
21523 -- because this routine makes use of them.
21524
21525 Set_Ekind (T, E_Record_Type);
21526 Set_Etype (T, T);
21527 Init_Size_Align (T);
21528 Set_Interfaces (T, No_Elist);
21529 Set_Stored_Constraint (T, No_Elist);
21530 Set_Default_SSO (T);
21531
21532 -- Normal case
21533
21534 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21535 if Limited_Present (Def) then
21536 Check_SPARK_05_Restriction ("limited is not allowed", N);
21537 end if;
21538
21539 if Abstract_Present (Def) then
21540 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21541 end if;
21542
21543 -- The flag Is_Tagged_Type might have already been set by
21544 -- Find_Type_Name if it detected an error for declaration T. This
21545 -- arises in the case of private tagged types where the full view
21546 -- omits the word tagged.
21547
21548 Is_Tagged :=
21549 Tagged_Present (Def)
21550 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21551
21552 Set_Is_Limited_Record (T, Limited_Present (Def));
21553
21554 if Is_Tagged then
21555 Set_Is_Tagged_Type (T, True);
21556 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21557 end if;
21558
21559 -- Type is abstract if full declaration carries keyword, or if
21560 -- previous partial view did.
21561
21562 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21563 or else Abstract_Present (Def));
21564
21565 else
21566 Check_SPARK_05_Restriction ("interface is not allowed", N);
21567
21568 Is_Tagged := True;
21569 Analyze_Interface_Declaration (T, Def);
21570
21571 if Present (Discriminant_Specifications (N)) then
21572 Error_Msg_N
21573 ("interface types cannot have discriminants",
21574 Defining_Identifier
21575 (First (Discriminant_Specifications (N))));
21576 end if;
21577 end if;
21578
21579 -- First pass: if there are self-referential access components,
21580 -- create the required anonymous access type declarations, and if
21581 -- need be an incomplete type declaration for T itself.
21582
21583 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21584
21585 if Ada_Version >= Ada_2005
21586 and then Present (Interface_List (Def))
21587 then
21588 Check_Interfaces (N, Def);
21589
21590 declare
21591 Ifaces_List : Elist_Id;
21592
21593 begin
21594 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21595 -- already in the parents.
21596
21597 Collect_Interfaces
21598 (T => T,
21599 Ifaces_List => Ifaces_List,
21600 Exclude_Parents => True);
21601
21602 Set_Interfaces (T, Ifaces_List);
21603 end;
21604 end if;
21605
21606 -- Records constitute a scope for the component declarations within.
21607 -- The scope is created prior to the processing of these declarations.
21608 -- Discriminants are processed first, so that they are visible when
21609 -- processing the other components. The Ekind of the record type itself
21610 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21611
21612 -- Enter record scope
21613
21614 Push_Scope (T);
21615
21616 -- If an incomplete or private type declaration was already given for
21617 -- the type, then this scope already exists, and the discriminants have
21618 -- been declared within. We must verify that the full declaration
21619 -- matches the incomplete one.
21620
21621 Check_Or_Process_Discriminants (N, T, Prev);
21622
21623 Set_Is_Constrained (T, not Has_Discriminants (T));
21624 Set_Has_Delayed_Freeze (T, True);
21625
21626 -- For tagged types add a manually analyzed component corresponding
21627 -- to the component _tag, the corresponding piece of tree will be
21628 -- expanded as part of the freezing actions if it is not a CPP_Class.
21629
21630 if Is_Tagged then
21631
21632 -- Do not add the tag unless we are in expansion mode
21633
21634 if Expander_Active then
21635 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21636 Enter_Name (Tag_Comp);
21637
21638 Set_Ekind (Tag_Comp, E_Component);
21639 Set_Is_Tag (Tag_Comp);
21640 Set_Is_Aliased (Tag_Comp);
21641 Set_Etype (Tag_Comp, RTE (RE_Tag));
21642 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21643 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21644 Init_Component_Location (Tag_Comp);
21645
21646 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21647 -- implemented interfaces.
21648
21649 if Has_Interfaces (T) then
21650 Add_Interface_Tag_Components (N, T);
21651 end if;
21652 end if;
21653
21654 Make_Class_Wide_Type (T);
21655 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21656 end if;
21657
21658 -- We must suppress range checks when processing record components in
21659 -- the presence of discriminants, since we don't want spurious checks to
21660 -- be generated during their analysis, but Suppress_Range_Checks flags
21661 -- must be reset the after processing the record definition.
21662
21663 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21664 -- couldn't we just use the normal range check suppression method here.
21665 -- That would seem cleaner ???
21666
21667 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21668 Set_Kill_Range_Checks (T, True);
21669 Record_Type_Definition (Def, Prev);
21670 Set_Kill_Range_Checks (T, False);
21671 else
21672 Record_Type_Definition (Def, Prev);
21673 end if;
21674
21675 -- Exit from record scope
21676
21677 End_Scope;
21678
21679 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21680 -- the implemented interfaces and associate them an aliased entity.
21681
21682 if Is_Tagged
21683 and then not Is_Empty_List (Interface_List (Def))
21684 then
21685 Derive_Progenitor_Subprograms (T, T);
21686 end if;
21687
21688 Check_Function_Writable_Actuals (N);
21689 end Record_Type_Declaration;
21690
21691 ----------------------------
21692 -- Record_Type_Definition --
21693 ----------------------------
21694
21695 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21696 Component : Entity_Id;
21697 Ctrl_Components : Boolean := False;
21698 Final_Storage_Only : Boolean;
21699 T : Entity_Id;
21700
21701 begin
21702 if Ekind (Prev_T) = E_Incomplete_Type then
21703 T := Full_View (Prev_T);
21704 else
21705 T := Prev_T;
21706 end if;
21707
21708 -- In SPARK, tagged types and type extensions may only be declared in
21709 -- the specification of library unit packages.
21710
21711 if Present (Def) and then Is_Tagged_Type (T) then
21712 declare
21713 Typ : Node_Id;
21714 Ctxt : Node_Id;
21715
21716 begin
21717 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21718 Typ := Parent (Def);
21719 else
21720 pragma Assert
21721 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21722 Typ := Parent (Parent (Def));
21723 end if;
21724
21725 Ctxt := Parent (Typ);
21726
21727 if Nkind (Ctxt) = N_Package_Body
21728 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21729 then
21730 Check_SPARK_05_Restriction
21731 ("type should be defined in package specification", Typ);
21732
21733 elsif Nkind (Ctxt) /= N_Package_Specification
21734 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21735 then
21736 Check_SPARK_05_Restriction
21737 ("type should be defined in library unit package", Typ);
21738 end if;
21739 end;
21740 end if;
21741
21742 Final_Storage_Only := not Is_Controlled_Active (T);
21743
21744 -- Ada 2005: Check whether an explicit Limited is present in a derived
21745 -- type declaration.
21746
21747 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21748 and then Limited_Present (Parent (Def))
21749 then
21750 Set_Is_Limited_Record (T);
21751 end if;
21752
21753 -- If the component list of a record type is defined by the reserved
21754 -- word null and there is no discriminant part, then the record type has
21755 -- no components and all records of the type are null records (RM 3.7)
21756 -- This procedure is also called to process the extension part of a
21757 -- record extension, in which case the current scope may have inherited
21758 -- components.
21759
21760 if No (Def)
21761 or else No (Component_List (Def))
21762 or else Null_Present (Component_List (Def))
21763 then
21764 if not Is_Tagged_Type (T) then
21765 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21766 end if;
21767
21768 else
21769 Analyze_Declarations (Component_Items (Component_List (Def)));
21770
21771 if Present (Variant_Part (Component_List (Def))) then
21772 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21773 Analyze (Variant_Part (Component_List (Def)));
21774 end if;
21775 end if;
21776
21777 -- After completing the semantic analysis of the record definition,
21778 -- record components, both new and inherited, are accessible. Set their
21779 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21780 -- whose Ekind may be void.
21781
21782 Component := First_Entity (Current_Scope);
21783 while Present (Component) loop
21784 if Ekind (Component) = E_Void
21785 and then not Is_Itype (Component)
21786 then
21787 Set_Ekind (Component, E_Component);
21788 Init_Component_Location (Component);
21789 end if;
21790
21791 Propagate_Concurrent_Flags (T, Etype (Component));
21792
21793 if Ekind (Component) /= E_Component then
21794 null;
21795
21796 -- Do not set Has_Controlled_Component on a class-wide equivalent
21797 -- type. See Make_CW_Equivalent_Type.
21798
21799 elsif not Is_Class_Wide_Equivalent_Type (T)
21800 and then (Has_Controlled_Component (Etype (Component))
21801 or else (Chars (Component) /= Name_uParent
21802 and then Is_Controlled_Active
21803 (Etype (Component))))
21804 then
21805 Set_Has_Controlled_Component (T, True);
21806 Final_Storage_Only :=
21807 Final_Storage_Only
21808 and then Finalize_Storage_Only (Etype (Component));
21809 Ctrl_Components := True;
21810 end if;
21811
21812 Next_Entity (Component);
21813 end loop;
21814
21815 -- A Type is Finalize_Storage_Only only if all its controlled components
21816 -- are also.
21817
21818 if Ctrl_Components then
21819 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21820 end if;
21821
21822 -- Place reference to end record on the proper entity, which may
21823 -- be a partial view.
21824
21825 if Present (Def) then
21826 Process_End_Label (Def, 'e', Prev_T);
21827 end if;
21828 end Record_Type_Definition;
21829
21830 ------------------------
21831 -- Replace_Components --
21832 ------------------------
21833
21834 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21835 function Process (N : Node_Id) return Traverse_Result;
21836
21837 -------------
21838 -- Process --
21839 -------------
21840
21841 function Process (N : Node_Id) return Traverse_Result is
21842 Comp : Entity_Id;
21843
21844 begin
21845 if Nkind (N) = N_Discriminant_Specification then
21846 Comp := First_Discriminant (Typ);
21847 while Present (Comp) loop
21848 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21849 Set_Defining_Identifier (N, Comp);
21850 exit;
21851 end if;
21852
21853 Next_Discriminant (Comp);
21854 end loop;
21855
21856 elsif Nkind (N) = N_Component_Declaration then
21857 Comp := First_Component (Typ);
21858 while Present (Comp) loop
21859 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21860 Set_Defining_Identifier (N, Comp);
21861 exit;
21862 end if;
21863
21864 Next_Component (Comp);
21865 end loop;
21866 end if;
21867
21868 return OK;
21869 end Process;
21870
21871 procedure Replace is new Traverse_Proc (Process);
21872
21873 -- Start of processing for Replace_Components
21874
21875 begin
21876 Replace (Decl);
21877 end Replace_Components;
21878
21879 -------------------------------
21880 -- Set_Completion_Referenced --
21881 -------------------------------
21882
21883 procedure Set_Completion_Referenced (E : Entity_Id) is
21884 begin
21885 -- If in main unit, mark entity that is a completion as referenced,
21886 -- warnings go on the partial view when needed.
21887
21888 if In_Extended_Main_Source_Unit (E) then
21889 Set_Referenced (E);
21890 end if;
21891 end Set_Completion_Referenced;
21892
21893 ---------------------
21894 -- Set_Default_SSO --
21895 ---------------------
21896
21897 procedure Set_Default_SSO (T : Entity_Id) is
21898 begin
21899 case Opt.Default_SSO is
21900 when ' ' =>
21901 null;
21902 when 'L' =>
21903 Set_SSO_Set_Low_By_Default (T, True);
21904 when 'H' =>
21905 Set_SSO_Set_High_By_Default (T, True);
21906 when others =>
21907 raise Program_Error;
21908 end case;
21909 end Set_Default_SSO;
21910
21911 ---------------------
21912 -- Set_Fixed_Range --
21913 ---------------------
21914
21915 -- The range for fixed-point types is complicated by the fact that we
21916 -- do not know the exact end points at the time of the declaration. This
21917 -- is true for three reasons:
21918
21919 -- A size clause may affect the fudging of the end-points.
21920 -- A small clause may affect the values of the end-points.
21921 -- We try to include the end-points if it does not affect the size.
21922
21923 -- This means that the actual end-points must be established at the
21924 -- point when the type is frozen. Meanwhile, we first narrow the range
21925 -- as permitted (so that it will fit if necessary in a small specified
21926 -- size), and then build a range subtree with these narrowed bounds.
21927 -- Set_Fixed_Range constructs the range from real literal values, and
21928 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21929
21930 -- The parent of this range is set to point to the entity so that it is
21931 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21932 -- other scalar types, which are just pointers to the range in the
21933 -- original tree, this would otherwise be an orphan).
21934
21935 -- The tree is left unanalyzed. When the type is frozen, the processing
21936 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21937 -- analyzed, and uses this as an indication that it should complete
21938 -- work on the range (it will know the final small and size values).
21939
21940 procedure Set_Fixed_Range
21941 (E : Entity_Id;
21942 Loc : Source_Ptr;
21943 Lo : Ureal;
21944 Hi : Ureal)
21945 is
21946 S : constant Node_Id :=
21947 Make_Range (Loc,
21948 Low_Bound => Make_Real_Literal (Loc, Lo),
21949 High_Bound => Make_Real_Literal (Loc, Hi));
21950 begin
21951 Set_Scalar_Range (E, S);
21952 Set_Parent (S, E);
21953
21954 -- Before the freeze point, the bounds of a fixed point are universal
21955 -- and carry the corresponding type.
21956
21957 Set_Etype (Low_Bound (S), Universal_Real);
21958 Set_Etype (High_Bound (S), Universal_Real);
21959 end Set_Fixed_Range;
21960
21961 ----------------------------------
21962 -- Set_Scalar_Range_For_Subtype --
21963 ----------------------------------
21964
21965 procedure Set_Scalar_Range_For_Subtype
21966 (Def_Id : Entity_Id;
21967 R : Node_Id;
21968 Subt : Entity_Id)
21969 is
21970 Kind : constant Entity_Kind := Ekind (Def_Id);
21971
21972 begin
21973 -- Defend against previous error
21974
21975 if Nkind (R) = N_Error then
21976 return;
21977 end if;
21978
21979 Set_Scalar_Range (Def_Id, R);
21980
21981 -- We need to link the range into the tree before resolving it so
21982 -- that types that are referenced, including importantly the subtype
21983 -- itself, are properly frozen (Freeze_Expression requires that the
21984 -- expression be properly linked into the tree). Of course if it is
21985 -- already linked in, then we do not disturb the current link.
21986
21987 if No (Parent (R)) then
21988 Set_Parent (R, Def_Id);
21989 end if;
21990
21991 -- Reset the kind of the subtype during analysis of the range, to
21992 -- catch possible premature use in the bounds themselves.
21993
21994 Set_Ekind (Def_Id, E_Void);
21995 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21996 Set_Ekind (Def_Id, Kind);
21997 end Set_Scalar_Range_For_Subtype;
21998
21999 --------------------------------------------------------
22000 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22001 --------------------------------------------------------
22002
22003 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22004 (E : Entity_Id)
22005 is
22006 begin
22007 -- Make sure set if encountered during Expand_To_Stored_Constraint
22008
22009 Set_Stored_Constraint (E, No_Elist);
22010
22011 -- Give it the right value
22012
22013 if Is_Constrained (E) and then Has_Discriminants (E) then
22014 Set_Stored_Constraint (E,
22015 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22016 end if;
22017 end Set_Stored_Constraint_From_Discriminant_Constraint;
22018
22019 -------------------------------------
22020 -- Signed_Integer_Type_Declaration --
22021 -------------------------------------
22022
22023 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22024 Implicit_Base : Entity_Id;
22025 Base_Typ : Entity_Id;
22026 Lo_Val : Uint;
22027 Hi_Val : Uint;
22028 Errs : Boolean := False;
22029 Lo : Node_Id;
22030 Hi : Node_Id;
22031
22032 function Can_Derive_From (E : Entity_Id) return Boolean;
22033 -- Determine whether given bounds allow derivation from specified type
22034
22035 procedure Check_Bound (Expr : Node_Id);
22036 -- Check bound to make sure it is integral and static. If not, post
22037 -- appropriate error message and set Errs flag
22038
22039 ---------------------
22040 -- Can_Derive_From --
22041 ---------------------
22042
22043 -- Note we check both bounds against both end values, to deal with
22044 -- strange types like ones with a range of 0 .. -12341234.
22045
22046 function Can_Derive_From (E : Entity_Id) return Boolean is
22047 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22048 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22049 begin
22050 return Lo <= Lo_Val and then Lo_Val <= Hi
22051 and then
22052 Lo <= Hi_Val and then Hi_Val <= Hi;
22053 end Can_Derive_From;
22054
22055 -----------------
22056 -- Check_Bound --
22057 -----------------
22058
22059 procedure Check_Bound (Expr : Node_Id) is
22060 begin
22061 -- If a range constraint is used as an integer type definition, each
22062 -- bound of the range must be defined by a static expression of some
22063 -- integer type, but the two bounds need not have the same integer
22064 -- type (Negative bounds are allowed.) (RM 3.5.4)
22065
22066 if not Is_Integer_Type (Etype (Expr)) then
22067 Error_Msg_N
22068 ("integer type definition bounds must be of integer type", Expr);
22069 Errs := True;
22070
22071 elsif not Is_OK_Static_Expression (Expr) then
22072 Flag_Non_Static_Expr
22073 ("non-static expression used for integer type bound!", Expr);
22074 Errs := True;
22075
22076 -- The bounds are folded into literals, and we set their type to be
22077 -- universal, to avoid typing difficulties: we cannot set the type
22078 -- of the literal to the new type, because this would be a forward
22079 -- reference for the back end, and if the original type is user-
22080 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22081
22082 else
22083 if Is_Entity_Name (Expr) then
22084 Fold_Uint (Expr, Expr_Value (Expr), True);
22085 end if;
22086
22087 Set_Etype (Expr, Universal_Integer);
22088 end if;
22089 end Check_Bound;
22090
22091 -- Start of processing for Signed_Integer_Type_Declaration
22092
22093 begin
22094 -- Create an anonymous base type
22095
22096 Implicit_Base :=
22097 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22098
22099 -- Analyze and check the bounds, they can be of any integer type
22100
22101 Lo := Low_Bound (Def);
22102 Hi := High_Bound (Def);
22103
22104 -- Arbitrarily use Integer as the type if either bound had an error
22105
22106 if Hi = Error or else Lo = Error then
22107 Base_Typ := Any_Integer;
22108 Set_Error_Posted (T, True);
22109
22110 -- Here both bounds are OK expressions
22111
22112 else
22113 Analyze_And_Resolve (Lo, Any_Integer);
22114 Analyze_And_Resolve (Hi, Any_Integer);
22115
22116 Check_Bound (Lo);
22117 Check_Bound (Hi);
22118
22119 if Errs then
22120 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22121 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22122 end if;
22123
22124 -- Find type to derive from
22125
22126 Lo_Val := Expr_Value (Lo);
22127 Hi_Val := Expr_Value (Hi);
22128
22129 if Can_Derive_From (Standard_Short_Short_Integer) then
22130 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22131
22132 elsif Can_Derive_From (Standard_Short_Integer) then
22133 Base_Typ := Base_Type (Standard_Short_Integer);
22134
22135 elsif Can_Derive_From (Standard_Integer) then
22136 Base_Typ := Base_Type (Standard_Integer);
22137
22138 elsif Can_Derive_From (Standard_Long_Integer) then
22139 Base_Typ := Base_Type (Standard_Long_Integer);
22140
22141 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22142 Check_Restriction (No_Long_Long_Integers, Def);
22143 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22144
22145 else
22146 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22147 Error_Msg_N ("integer type definition bounds out of range", Def);
22148 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22149 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22150 end if;
22151 end if;
22152
22153 -- Complete both implicit base and declared first subtype entities. The
22154 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22155 -- are not clobbered when the signed integer type acts as a full view of
22156 -- a private type.
22157
22158 Set_Etype (Implicit_Base, Base_Typ);
22159 Set_Size_Info (Implicit_Base, Base_Typ);
22160 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22161 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22162 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22163
22164 Set_Ekind (T, E_Signed_Integer_Subtype);
22165 Set_Etype (T, Implicit_Base);
22166 Set_Size_Info (T, Implicit_Base);
22167 Inherit_Rep_Item_Chain (T, Implicit_Base);
22168 Set_Scalar_Range (T, Def);
22169 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22170 Set_Is_Constrained (T);
22171 end Signed_Integer_Type_Declaration;
22172
22173 end Sem_Ch3;