512615fe4b9858151c9fee69ee6def88222d0022
[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-2016, 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 Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Itypes; use Itypes;
45 with Layout; use Layout;
46 with Lib; use Lib;
47 with Lib.Xref; use Lib.Xref;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
82
83 package body Sem_Ch3 is
84
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
88
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
92 -- record type.
93
94 procedure Build_Derived_Type
95 (N : Node_Id;
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
114 -- the type).
115 --
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
118
119 procedure Build_Derived_Access_Type
120 (N : Node_Id;
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
126
127 procedure Build_Derived_Array_Type
128 (N : Node_Id;
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
134
135 procedure Build_Derived_Concurrent_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
142
143 procedure Build_Derived_Enumeration_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
150
151 procedure Build_Derived_Numeric_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
157
158 procedure Build_Derived_Private_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
167
168 procedure Build_Derived_Record_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
182
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
190
191 function Build_Discriminant_Constraints
192 (T : Entity_Id;
193 Def : Node_Id;
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
204
205 procedure Build_Discriminated_Subtype
206 (T : Entity_Id;
207 Def_Id : Entity_Id;
208 Elist : Elist_Id;
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
213 --
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
216 --
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 --
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
227
228 function Build_Scalar_Bound
229 (Bound : Node_Id;
230 Par_T : Entity_Id;
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
236
237 procedure Build_Underlying_Full_View
238 (N : Node_Id;
239 Typ : Entity_Id;
240 Par : Entity_Id);
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
247 -- view exchanges).
248
249 procedure Check_Access_Discriminant_Requires_Limited
250 (D : Node_Id;
251 Loc : Node_Id);
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
255
256 procedure Check_Anonymous_Access_Components
257 (Typ_Decl : Node_Id;
258 Typ : Entity_Id;
259 Prev : Entity_Id;
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
269
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
273
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
277
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
281
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284
285 procedure Check_Or_Process_Discriminants
286 (N : Node_Id;
287 T : Entity_Id;
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
293 -- if any.
294
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
298
299 procedure Constant_Redeclaration
300 (Id : Entity_Id;
301 N : Node_Id;
302 T : out Entity_Id);
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
306
307 function Contain_Interface
308 (Iface : Entity_Id;
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311
312 procedure Convert_Scalar_Bounds
313 (N : Node_Id;
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
316 Loc : Source_Ptr);
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
324
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
328
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
332
333 procedure Create_Constrained_Components
334 (Subt : Entity_Id;
335 Decl_Node : Node_Id;
336 Typ : Entity_Id;
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
346
347 function Constrain_Component_Type
348 (Comp : Entity_Id;
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
351 Typ : Entity_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
360
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
368
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
385
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
394 --
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
397 --
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
403 --
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree.
406 --
407 -- The last two arguments are used to create its external name if needed.
408
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
415
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419
420 procedure Constrain_Discriminated_Type
421 (Def_Id : Entity_Id;
422 S : Node_Id;
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 -- of For_Access.
431
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439
440 procedure Constrain_Index
441 (Index : Node_Id;
442 S : Node_Id;
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
445 Suffix : Character;
446 Suffix_Index : Nat);
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
452
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
455
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
464
465 procedure Decimal_Fixed_Point_Type_Declaration
466 (T : Entity_Id;
467 Def : Node_Id);
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
470
471 procedure Complete_Private_Subtype
472 (Priv : Entity_Id;
473 Full : Entity_Id;
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
479
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
490
491 procedure Derived_Standard_Character
492 (N : Node_Id;
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
497
498 procedure Derived_Type_Declaration
499 (T : Entity_Id;
500 N : Node_Id;
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
507
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
514
515 function Expand_To_Stored_Constraint
516 (Typ : Entity_Id;
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
521
522 function Find_Type_Of_Object
523 (Obj_Def : Node_Id;
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
527
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
530
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
536
537 function Inherit_Components
538 (N : Node_Id;
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
541 Is_Tagged : Boolean;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
548 --
549 -- N is the original derived type declaration
550 --
551 -- Is_Tagged is set if we are dealing with tagged types
552 --
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
555 --
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
559 --
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
562 --
563 -- which gets treated as
564 --
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 --
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
570 --
571 -- (Old_Component => New_Component),
572 --
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
578
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
582
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
588
589 function Is_Valid_Constraint_Kind
590 (T_Kind : Type_Kind;
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
594
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
597
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
601
602 procedure Ordinary_Fixed_Point_Type_Declaration
603 (T : Entity_Id;
604 Def : Node_Id);
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
607
608 procedure Prepare_Private_Subtype_Completion
609 (Id : Entity_Id;
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
615
616 procedure Process_Incomplete_Dependents
617 (N : Node_Id;
618 Full_T : Entity_Id;
619 Inc_T : Entity_Id);
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
623 -- incomplete type.
624
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
627 --
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 --
630 -- Full_T is N's defining identifier.
631 --
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
637 -- full type.
638 --
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
642
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
648
649 procedure Record_Type_Declaration
650 (T : Entity_Id;
651 N : Node_Id;
652 Prev : Entity_Id);
653 -- Process a record type declaration (for both untagged and tagged
654 -- records). Parameters T and N are exactly like in procedure
655 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
656 -- for this routine. If this is the completion of an incomplete type
657 -- declaration, Prev is the entity of the incomplete declaration, used for
658 -- cross-referencing. Otherwise Prev = T.
659
660 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
661 -- This routine is used to process the actual record type definition (both
662 -- for untagged and tagged records). Def is a record type definition node.
663 -- This procedure analyzes the components in this record type definition.
664 -- Prev_T is the entity for the enclosing record type. It is provided so
665 -- that its Has_Task flag can be set if any of the component have Has_Task
666 -- set. If the declaration is the completion of an incomplete type
667 -- declaration, Prev_T is the original incomplete type, whose full view is
668 -- the record type.
669
670 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
671 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
672 -- build a copy of the declaration tree of the parent, and we create
673 -- independently the list of components for the derived type. Semantic
674 -- information uses the component entities, but record representation
675 -- clauses are validated on the declaration tree. This procedure replaces
676 -- discriminants and components in the declaration with those that have
677 -- been created by Inherit_Components.
678
679 procedure Set_Fixed_Range
680 (E : Entity_Id;
681 Loc : Source_Ptr;
682 Lo : Ureal;
683 Hi : Ureal);
684 -- Build a range node with the given bounds and set it as the Scalar_Range
685 -- of the given fixed-point type entity. Loc is the source location used
686 -- for the constructed range. See body for further details.
687
688 procedure Set_Scalar_Range_For_Subtype
689 (Def_Id : Entity_Id;
690 R : Node_Id;
691 Subt : Entity_Id);
692 -- This routine is used to set the scalar range field for a subtype given
693 -- Def_Id, the entity for the subtype, and R, the range expression for the
694 -- scalar range. Subt provides the parent subtype to be used to analyze,
695 -- resolve, and check the given range.
696
697 procedure Set_Default_SSO (T : Entity_Id);
698 -- T is the entity for an array or record being declared. This procedure
699 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
700 -- to the setting of Opt.Default_SSO.
701
702 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
703 -- Create a new signed integer entity, and apply the constraint to obtain
704 -- the required first named subtype of this type.
705
706 procedure Set_Stored_Constraint_From_Discriminant_Constraint
707 (E : Entity_Id);
708 -- E is some record type. This routine computes E's Stored_Constraint
709 -- from its Discriminant_Constraint.
710
711 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
712 -- Check that an entity in a list of progenitors is an interface,
713 -- emit error otherwise.
714
715 -----------------------
716 -- Access_Definition --
717 -----------------------
718
719 function Access_Definition
720 (Related_Nod : Node_Id;
721 N : Node_Id) return Entity_Id
722 is
723 Anon_Type : Entity_Id;
724 Anon_Scope : Entity_Id;
725 Desig_Type : Entity_Id;
726 Enclosing_Prot_Type : Entity_Id := Empty;
727
728 begin
729 Check_SPARK_05_Restriction ("access type is not allowed", N);
730
731 if Is_Entry (Current_Scope)
732 and then Is_Task_Type (Etype (Scope (Current_Scope)))
733 then
734 Error_Msg_N ("task entries cannot have access parameters", N);
735 return Empty;
736 end if;
737
738 -- Ada 2005: For an object declaration the corresponding anonymous
739 -- type is declared in the current scope.
740
741 -- If the access definition is the return type of another access to
742 -- function, scope is the current one, because it is the one of the
743 -- current type declaration, except for the pathological case below.
744
745 if Nkind_In (Related_Nod, N_Object_Declaration,
746 N_Access_Function_Definition)
747 then
748 Anon_Scope := Current_Scope;
749
750 -- A pathological case: function returning access functions that
751 -- return access functions, etc. Each anonymous access type created
752 -- is in the enclosing scope of the outermost function.
753
754 declare
755 Par : Node_Id;
756
757 begin
758 Par := Related_Nod;
759 while Nkind_In (Par, N_Access_Function_Definition,
760 N_Access_Definition)
761 loop
762 Par := Parent (Par);
763 end loop;
764
765 if Nkind (Par) = N_Function_Specification then
766 Anon_Scope := Scope (Defining_Entity (Par));
767 end if;
768 end;
769
770 -- For the anonymous function result case, retrieve the scope of the
771 -- function specification's associated entity rather than using the
772 -- current scope. The current scope will be the function itself if the
773 -- formal part is currently being analyzed, but will be the parent scope
774 -- in the case of a parameterless function, and we always want to use
775 -- the function's parent scope. Finally, if the function is a child
776 -- unit, we must traverse the tree to retrieve the proper entity.
777
778 elsif Nkind (Related_Nod) = N_Function_Specification
779 and then Nkind (Parent (N)) /= N_Parameter_Specification
780 then
781 -- If the current scope is a protected type, the anonymous access
782 -- is associated with one of the protected operations, and must
783 -- be available in the scope that encloses the protected declaration.
784 -- Otherwise the type is in the scope enclosing the subprogram.
785
786 -- If the function has formals, The return type of a subprogram
787 -- declaration is analyzed in the scope of the subprogram (see
788 -- Process_Formals) and thus the protected type, if present, is
789 -- the scope of the current function scope.
790
791 if Ekind (Current_Scope) = E_Protected_Type then
792 Enclosing_Prot_Type := Current_Scope;
793
794 elsif Ekind (Current_Scope) = E_Function
795 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
796 then
797 Enclosing_Prot_Type := Scope (Current_Scope);
798 end if;
799
800 if Present (Enclosing_Prot_Type) then
801 Anon_Scope := Scope (Enclosing_Prot_Type);
802
803 else
804 Anon_Scope := Scope (Defining_Entity (Related_Nod));
805 end if;
806
807 -- For an access type definition, if the current scope is a child
808 -- unit it is the scope of the type.
809
810 elsif Is_Compilation_Unit (Current_Scope) then
811 Anon_Scope := Current_Scope;
812
813 -- For access formals, access components, and access discriminants, the
814 -- scope is that of the enclosing declaration,
815
816 else
817 Anon_Scope := Scope (Current_Scope);
818 end if;
819
820 Anon_Type :=
821 Create_Itype
822 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823
824 if All_Present (N)
825 and then Ada_Version >= Ada_2005
826 then
827 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
828 end if;
829
830 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
831 -- the corresponding semantic routine
832
833 if Present (Access_To_Subprogram_Definition (N)) then
834
835 -- Compiler runtime units are compiled in Ada 2005 mode when building
836 -- the runtime library but must also be compilable in Ada 95 mode
837 -- (when bootstrapping the compiler).
838
839 Check_Compiler_Unit ("anonymous access to subprogram", N);
840
841 Access_Subprogram_Declaration
842 (T_Name => Anon_Type,
843 T_Def => Access_To_Subprogram_Definition (N));
844
845 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
846 Set_Ekind
847 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
848 else
849 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
850 end if;
851
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
854
855 -- If the anonymous access is associated with a protected operation,
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
858
859 -- If the anonymous access itself is protected, a full type
860 -- declaratiton will be created for it, so that the equivalent
861 -- record type can be constructed. For further details, see
862 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863
864 if Ekind (Current_Scope) = E_Protected_Type
865 and then not Protected_Present (Access_To_Subprogram_Definition (N))
866 then
867 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
868 end if;
869
870 return Anon_Type;
871 end if;
872
873 Find_Type (Subtype_Mark (N));
874 Desig_Type := Entity (Subtype_Mark (N));
875
876 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
877 Set_Etype (Anon_Type, Anon_Type);
878
879 -- Make sure the anonymous access type has size and alignment fields
880 -- set, as required by gigi. This is necessary in the case of the
881 -- Task_Body_Procedure.
882
883 if not Has_Private_Component (Desig_Type) then
884 Layout_Type (Anon_Type);
885 end if;
886
887 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
888 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
889 -- the null value is allowed. In Ada 95 the null value is never allowed.
890
891 if Ada_Version >= Ada_2005 then
892 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
893 else
894 Set_Can_Never_Be_Null (Anon_Type, True);
895 end if;
896
897 -- The anonymous access type is as public as the discriminated type or
898 -- subprogram that defines it. It is imported (for back-end purposes)
899 -- if the designated type is.
900
901 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902
903 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904
905 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906
907 -- The context is either a subprogram declaration, object declaration,
908 -- or an access discriminant, in a private or a full type declaration.
909 -- In the case of a subprogram, if the designated type is incomplete,
910 -- the operation will be a primitive operation of the full type, to be
911 -- updated subsequently. If the type is imported through a limited_with
912 -- clause, the subprogram is not a primitive operation of the type
913 -- (which is declared elsewhere in some other scope).
914
915 if Ekind (Desig_Type) = E_Incomplete_Type
916 and then not From_Limited_With (Desig_Type)
917 and then Is_Overloadable (Current_Scope)
918 then
919 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
920 Set_Has_Delayed_Freeze (Current_Scope);
921 end if;
922
923 -- Ada 2005: If the designated type is an interface that may contain
924 -- tasks, create a Master entity for the declaration. This must be done
925 -- before expansion of the full declaration, because the declaration may
926 -- include an expression that is an allocator, whose expansion needs the
927 -- proper Master for the created tasks.
928
929 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
930 then
931 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
932 then
933 Build_Class_Wide_Master (Anon_Type);
934
935 -- Similarly, if the type is an anonymous access that designates
936 -- tasks, create a master entity for it in the current context.
937
938 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
939 then
940 Build_Master_Entity (Defining_Identifier (Related_Nod));
941 Build_Master_Renaming (Anon_Type);
942 end if;
943 end if;
944
945 -- For a private component of a protected type, it is imperative that
946 -- the back-end elaborate the type immediately after the protected
947 -- declaration, because this type will be used in the declarations
948 -- created for the component within each protected body, so we must
949 -- create an itype reference for it now.
950
951 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
952 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953
954 -- Similarly, if the access definition is the return result of a
955 -- function, create an itype reference for it because it will be used
956 -- within the function body. For a regular function that is not a
957 -- compilation unit, insert reference after the declaration. For a
958 -- protected operation, insert it after the enclosing protected type
959 -- declaration. In either case, do not create a reference for a type
960 -- obtained through a limited_with clause, because this would introduce
961 -- semantic dependencies.
962
963 -- Similarly, do not create a reference if the designated type is a
964 -- generic formal, because no use of it will reach the backend.
965
966 elsif Nkind (Related_Nod) = N_Function_Specification
967 and then not From_Limited_With (Desig_Type)
968 and then not Is_Generic_Type (Desig_Type)
969 then
970 if Present (Enclosing_Prot_Type) then
971 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972
973 elsif Is_List_Member (Parent (Related_Nod))
974 and then Nkind (Parent (N)) /= N_Parameter_Specification
975 then
976 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
977 end if;
978
979 -- Finally, create an itype reference for an object declaration of an
980 -- anonymous access type. This is strictly necessary only for deferred
981 -- constants, but in any case will avoid out-of-scope problems in the
982 -- back-end.
983
984 elsif Nkind (Related_Nod) = N_Object_Declaration then
985 Build_Itype_Reference (Anon_Type, Related_Nod);
986 end if;
987
988 return Anon_Type;
989 end Access_Definition;
990
991 -----------------------------------
992 -- Access_Subprogram_Declaration --
993 -----------------------------------
994
995 procedure Access_Subprogram_Declaration
996 (T_Name : Entity_Id;
997 T_Def : Node_Id)
998 is
999 procedure Check_For_Premature_Usage (Def : Node_Id);
1000 -- Check that type T_Name is not used, directly or recursively, as a
1001 -- parameter or a return type in Def. Def is either a subtype, an
1002 -- access_definition, or an access_to_subprogram_definition.
1003
1004 -------------------------------
1005 -- Check_For_Premature_Usage --
1006 -------------------------------
1007
1008 procedure Check_For_Premature_Usage (Def : Node_Id) is
1009 Param : Node_Id;
1010
1011 begin
1012 -- Check for a subtype mark
1013
1014 if Nkind (Def) in N_Has_Etype then
1015 if Etype (Def) = T_Name then
1016 Error_Msg_N
1017 ("type& cannot be used before end of its declaration", Def);
1018 end if;
1019
1020 -- If this is not a subtype, then this is an access_definition
1021
1022 elsif Nkind (Def) = N_Access_Definition then
1023 if Present (Access_To_Subprogram_Definition (Def)) then
1024 Check_For_Premature_Usage
1025 (Access_To_Subprogram_Definition (Def));
1026 else
1027 Check_For_Premature_Usage (Subtype_Mark (Def));
1028 end if;
1029
1030 -- The only cases left are N_Access_Function_Definition and
1031 -- N_Access_Procedure_Definition.
1032
1033 else
1034 if Present (Parameter_Specifications (Def)) then
1035 Param := First (Parameter_Specifications (Def));
1036 while Present (Param) loop
1037 Check_For_Premature_Usage (Parameter_Type (Param));
1038 Param := Next (Param);
1039 end loop;
1040 end if;
1041
1042 if Nkind (Def) = N_Access_Function_Definition then
1043 Check_For_Premature_Usage (Result_Definition (Def));
1044 end if;
1045 end if;
1046 end Check_For_Premature_Usage;
1047
1048 -- Local variables
1049
1050 Formals : constant List_Id := Parameter_Specifications (T_Def);
1051 Formal : Entity_Id;
1052 D_Ityp : Node_Id;
1053 Desig_Type : constant Entity_Id :=
1054 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055
1056 -- Start of processing for Access_Subprogram_Declaration
1057
1058 begin
1059 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060
1061 -- Associate the Itype node with the inner full-type declaration or
1062 -- subprogram spec or entry body. This is required to handle nested
1063 -- anonymous declarations. For example:
1064
1065 -- procedure P
1066 -- (X : access procedure
1067 -- (Y : access procedure
1068 -- (Z : access T)))
1069
1070 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1071 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1072 N_Private_Type_Declaration,
1073 N_Private_Extension_Declaration,
1074 N_Procedure_Specification,
1075 N_Function_Specification,
1076 N_Entry_Body)
1077
1078 or else
1079 Nkind_In (D_Ityp, N_Object_Declaration,
1080 N_Object_Renaming_Declaration,
1081 N_Formal_Object_Declaration,
1082 N_Formal_Type_Declaration,
1083 N_Task_Type_Declaration,
1084 N_Protected_Type_Declaration))
1085 loop
1086 D_Ityp := Parent (D_Ityp);
1087 pragma Assert (D_Ityp /= Empty);
1088 end loop;
1089
1090 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091
1092 if Nkind_In (D_Ityp, N_Procedure_Specification,
1093 N_Function_Specification)
1094 then
1095 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096
1097 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1098 N_Object_Declaration,
1099 N_Object_Renaming_Declaration,
1100 N_Formal_Type_Declaration)
1101 then
1102 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1103 end if;
1104
1105 if Nkind (T_Def) = N_Access_Function_Definition then
1106 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1107 declare
1108 Acc : constant Node_Id := Result_Definition (T_Def);
1109
1110 begin
1111 if Present (Access_To_Subprogram_Definition (Acc))
1112 and then
1113 Protected_Present (Access_To_Subprogram_Definition (Acc))
1114 then
1115 Set_Etype
1116 (Desig_Type,
1117 Replace_Anonymous_Access_To_Protected_Subprogram
1118 (T_Def));
1119
1120 else
1121 Set_Etype
1122 (Desig_Type,
1123 Access_Definition (T_Def, Result_Definition (T_Def)));
1124 end if;
1125 end;
1126
1127 else
1128 Analyze (Result_Definition (T_Def));
1129
1130 declare
1131 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132
1133 begin
1134 -- If a null exclusion is imposed on the result type, then
1135 -- create a null-excluding itype (an access subtype) and use
1136 -- it as the function's Etype.
1137
1138 if Is_Access_Type (Typ)
1139 and then Null_Exclusion_In_Return_Present (T_Def)
1140 then
1141 Set_Etype (Desig_Type,
1142 Create_Null_Excluding_Itype
1143 (T => Typ,
1144 Related_Nod => T_Def,
1145 Scope_Id => Current_Scope));
1146
1147 else
1148 if From_Limited_With (Typ) then
1149
1150 -- AI05-151: Incomplete types are allowed in all basic
1151 -- declarations, including access to subprograms.
1152
1153 if Ada_Version >= Ada_2012 then
1154 null;
1155
1156 else
1157 Error_Msg_NE
1158 ("illegal use of incomplete type&",
1159 Result_Definition (T_Def), Typ);
1160 end if;
1161
1162 elsif Ekind (Current_Scope) = E_Package
1163 and then In_Private_Part (Current_Scope)
1164 then
1165 if Ekind (Typ) = E_Incomplete_Type then
1166 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167
1168 elsif Is_Class_Wide_Type (Typ)
1169 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1170 then
1171 Append_Elmt
1172 (Desig_Type, Private_Dependents (Etype (Typ)));
1173 end if;
1174 end if;
1175
1176 Set_Etype (Desig_Type, Typ);
1177 end if;
1178 end;
1179 end if;
1180
1181 if not (Is_Type (Etype (Desig_Type))) then
1182 Error_Msg_N
1183 ("expect type in function specification",
1184 Result_Definition (T_Def));
1185 end if;
1186
1187 else
1188 Set_Etype (Desig_Type, Standard_Void_Type);
1189 end if;
1190
1191 if Present (Formals) then
1192 Push_Scope (Desig_Type);
1193
1194 -- Some special tests here. These special tests can be removed
1195 -- if and when Itypes always have proper parent pointers to their
1196 -- declarations???
1197
1198 -- Special test 1) Link defining_identifier of formals. Required by
1199 -- First_Formal to provide its functionality.
1200
1201 declare
1202 F : Node_Id;
1203
1204 begin
1205 F := First (Formals);
1206
1207 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1208 -- when it is part of an unconstrained type and subtype expansion
1209 -- is disabled. To avoid back-end problems with shared profiles,
1210 -- use previous subprogram type as the designated type, and then
1211 -- remove scope added above.
1212
1213 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1214 then
1215 Set_Etype (T_Name, T_Name);
1216 Init_Size_Align (T_Name);
1217 Set_Directly_Designated_Type (T_Name,
1218 Scope (Defining_Identifier (F)));
1219 End_Scope;
1220 return;
1221 end if;
1222
1223 while Present (F) loop
1224 if No (Parent (Defining_Identifier (F))) then
1225 Set_Parent (Defining_Identifier (F), F);
1226 end if;
1227
1228 Next (F);
1229 end loop;
1230 end;
1231
1232 Process_Formals (Formals, Parent (T_Def));
1233
1234 -- Special test 2) End_Scope requires that the parent pointer be set
1235 -- to something reasonable, but Itypes don't have parent pointers. So
1236 -- we set it and then unset it ???
1237
1238 Set_Parent (Desig_Type, T_Name);
1239 End_Scope;
1240 Set_Parent (Desig_Type, Empty);
1241 end if;
1242
1243 -- Check for premature usage of the type being defined
1244
1245 Check_For_Premature_Usage (T_Def);
1246
1247 -- The return type and/or any parameter type may be incomplete. Mark the
1248 -- subprogram_type as depending on the incomplete type, so that it can
1249 -- be updated when the full type declaration is seen. This only applies
1250 -- to incomplete types declared in some enclosing scope, not to limited
1251 -- views from other packages.
1252
1253 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254
1255 if Present (Formals) then
1256 Formal := First_Formal (Desig_Type);
1257 while Present (Formal) loop
1258 if Ekind (Formal) /= E_In_Parameter
1259 and then Nkind (T_Def) = N_Access_Function_Definition
1260 and then Ada_Version < Ada_2012
1261 then
1262 Error_Msg_N ("functions can only have IN parameters", Formal);
1263 end if;
1264
1265 if Ekind (Etype (Formal)) = E_Incomplete_Type
1266 and then In_Open_Scopes (Scope (Etype (Formal)))
1267 then
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1270 end if;
1271
1272 Next_Formal (Formal);
1273 end loop;
1274 end if;
1275
1276 -- Check whether an indirect call without actuals may be possible. This
1277 -- is used when resolving calls whose result is then indexed.
1278
1279 May_Need_Actuals (Desig_Type);
1280
1281 -- If the return type is incomplete, this is legal as long as the type
1282 -- is declared in the current scope and will be completed in it (rather
1283 -- than being part of limited view).
1284
1285 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1286 and then not Has_Delayed_Freeze (Desig_Type)
1287 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1288 then
1289 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1290 Set_Has_Delayed_Freeze (Desig_Type);
1291 end if;
1292
1293 Check_Delayed_Subprogram (Desig_Type);
1294
1295 if Protected_Present (T_Def) then
1296 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1297 Set_Convention (Desig_Type, Convention_Protected);
1298 else
1299 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1300 end if;
1301
1302 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303
1304 Set_Etype (T_Name, T_Name);
1305 Init_Size_Align (T_Name);
1306 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307
1308 Generate_Reference_To_Formals (T_Name);
1309
1310 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1311
1312 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1313
1314 Check_Restriction (No_Access_Subprograms, T_Def);
1315 end Access_Subprogram_Declaration;
1316
1317 ----------------------------
1318 -- Access_Type_Declaration --
1319 ----------------------------
1320
1321 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1322 P : constant Node_Id := Parent (Def);
1323 S : constant Node_Id := Subtype_Indication (Def);
1324
1325 Full_Desig : Entity_Id;
1326
1327 begin
1328 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1329
1330 -- Check for permissible use of incomplete type
1331
1332 if Nkind (S) /= N_Subtype_Indication then
1333 Analyze (S);
1334
1335 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1336 Set_Directly_Designated_Type (T, Entity (S));
1337
1338 -- If the designated type is a limited view, we cannot tell if
1339 -- the full view contains tasks, and there is no way to handle
1340 -- that full view in a client. We create a master entity for the
1341 -- scope, which will be used when a client determines that one
1342 -- is needed.
1343
1344 if From_Limited_With (Entity (S))
1345 and then not Is_Class_Wide_Type (Entity (S))
1346 then
1347 Set_Ekind (T, E_Access_Type);
1348 Build_Master_Entity (T);
1349 Build_Master_Renaming (T);
1350 end if;
1351
1352 else
1353 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1354 end if;
1355
1356 -- If the access definition is of the form: ACCESS NOT NULL ..
1357 -- the subtype indication must be of an access type. Create
1358 -- a null-excluding subtype of it.
1359
1360 if Null_Excluding_Subtype (Def) then
1361 if not Is_Access_Type (Entity (S)) then
1362 Error_Msg_N ("null exclusion must apply to access type", Def);
1363
1364 else
1365 declare
1366 Loc : constant Source_Ptr := Sloc (S);
1367 Decl : Node_Id;
1368 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1369
1370 begin
1371 Decl :=
1372 Make_Subtype_Declaration (Loc,
1373 Defining_Identifier => Nam,
1374 Subtype_Indication =>
1375 New_Occurrence_Of (Entity (S), Loc));
1376 Set_Null_Exclusion_Present (Decl);
1377 Insert_Before (Parent (Def), Decl);
1378 Analyze (Decl);
1379 Set_Entity (S, Nam);
1380 end;
1381 end if;
1382 end if;
1383
1384 else
1385 Set_Directly_Designated_Type (T,
1386 Process_Subtype (S, P, T, 'P'));
1387 end if;
1388
1389 if All_Present (Def) or Constant_Present (Def) then
1390 Set_Ekind (T, E_General_Access_Type);
1391 else
1392 Set_Ekind (T, E_Access_Type);
1393 end if;
1394
1395 Full_Desig := Designated_Type (T);
1396
1397 if Base_Type (Full_Desig) = T then
1398 Error_Msg_N ("access type cannot designate itself", S);
1399
1400 -- In Ada 2005, the type may have a limited view through some unit in
1401 -- its own context, allowing the following circularity that cannot be
1402 -- detected earlier.
1403
1404 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1405 then
1406 Error_Msg_N
1407 ("access type cannot designate its own class-wide type", S);
1408
1409 -- Clean up indication of tagged status to prevent cascaded errors
1410
1411 Set_Is_Tagged_Type (T, False);
1412 end if;
1413
1414 Set_Etype (T, T);
1415
1416 -- If the type has appeared already in a with_type clause, it is frozen
1417 -- and the pointer size is already set. Else, initialize.
1418
1419 if not From_Limited_With (T) then
1420 Init_Size_Align (T);
1421 end if;
1422
1423 -- Note that Has_Task is always false, since the access type itself
1424 -- is not a task type. See Einfo for more description on this point.
1425 -- Exactly the same consideration applies to Has_Controlled_Component
1426 -- and to Has_Protected.
1427
1428 Set_Has_Task (T, False);
1429 Set_Has_Protected (T, False);
1430 Set_Has_Timing_Event (T, False);
1431 Set_Has_Controlled_Component (T, False);
1432
1433 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1434 -- problems where an incomplete view of this entity has been previously
1435 -- established by a limited with and an overlaid version of this field
1436 -- (Stored_Constraint) was initialized for the incomplete view.
1437
1438 -- This reset is performed in most cases except where the access type
1439 -- has been created for the purposes of allocating or deallocating a
1440 -- build-in-place object. Such access types have explicitly set pools
1441 -- and finalization masters.
1442
1443 if No (Associated_Storage_Pool (T)) then
1444 Set_Finalization_Master (T, Empty);
1445 end if;
1446
1447 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1448 -- attributes
1449
1450 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1451 Set_Is_Access_Constant (T, Constant_Present (Def));
1452 end Access_Type_Declaration;
1453
1454 ----------------------------------
1455 -- Add_Interface_Tag_Components --
1456 ----------------------------------
1457
1458 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1459 Loc : constant Source_Ptr := Sloc (N);
1460 L : List_Id;
1461 Last_Tag : Node_Id;
1462
1463 procedure Add_Tag (Iface : Entity_Id);
1464 -- Add tag for one of the progenitor interfaces
1465
1466 -------------
1467 -- Add_Tag --
1468 -------------
1469
1470 procedure Add_Tag (Iface : Entity_Id) is
1471 Decl : Node_Id;
1472 Def : Node_Id;
1473 Tag : Entity_Id;
1474 Offset : Entity_Id;
1475
1476 begin
1477 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1478
1479 -- This is a reasonable place to propagate predicates
1480
1481 if Has_Predicates (Iface) then
1482 Set_Has_Predicates (Typ);
1483 end if;
1484
1485 Def :=
1486 Make_Component_Definition (Loc,
1487 Aliased_Present => True,
1488 Subtype_Indication =>
1489 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1490
1491 Tag := Make_Temporary (Loc, 'V');
1492
1493 Decl :=
1494 Make_Component_Declaration (Loc,
1495 Defining_Identifier => Tag,
1496 Component_Definition => Def);
1497
1498 Analyze_Component_Declaration (Decl);
1499
1500 Set_Analyzed (Decl);
1501 Set_Ekind (Tag, E_Component);
1502 Set_Is_Tag (Tag);
1503 Set_Is_Aliased (Tag);
1504 Set_Related_Type (Tag, Iface);
1505 Init_Component_Location (Tag);
1506
1507 pragma Assert (Is_Frozen (Iface));
1508
1509 Set_DT_Entry_Count (Tag,
1510 DT_Entry_Count (First_Entity (Iface)));
1511
1512 if No (Last_Tag) then
1513 Prepend (Decl, L);
1514 else
1515 Insert_After (Last_Tag, Decl);
1516 end if;
1517
1518 Last_Tag := Decl;
1519
1520 -- If the ancestor has discriminants we need to give special support
1521 -- to store the offset_to_top value of the secondary dispatch tables.
1522 -- For this purpose we add a supplementary component just after the
1523 -- field that contains the tag associated with each secondary DT.
1524
1525 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1526 Def :=
1527 Make_Component_Definition (Loc,
1528 Subtype_Indication =>
1529 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1530
1531 Offset := Make_Temporary (Loc, 'V');
1532
1533 Decl :=
1534 Make_Component_Declaration (Loc,
1535 Defining_Identifier => Offset,
1536 Component_Definition => Def);
1537
1538 Analyze_Component_Declaration (Decl);
1539
1540 Set_Analyzed (Decl);
1541 Set_Ekind (Offset, E_Component);
1542 Set_Is_Aliased (Offset);
1543 Set_Related_Type (Offset, Iface);
1544 Init_Component_Location (Offset);
1545 Insert_After (Last_Tag, Decl);
1546 Last_Tag := Decl;
1547 end if;
1548 end Add_Tag;
1549
1550 -- Local variables
1551
1552 Elmt : Elmt_Id;
1553 Ext : Node_Id;
1554 Comp : Node_Id;
1555
1556 -- Start of processing for Add_Interface_Tag_Components
1557
1558 begin
1559 if not RTE_Available (RE_Interface_Tag) then
1560 Error_Msg
1561 ("(Ada 2005) interface types not supported by this run-time!",
1562 Sloc (N));
1563 return;
1564 end if;
1565
1566 if Ekind (Typ) /= E_Record_Type
1567 or else (Is_Concurrent_Record_Type (Typ)
1568 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1569 or else (not Is_Concurrent_Record_Type (Typ)
1570 and then No (Interfaces (Typ))
1571 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1572 then
1573 return;
1574 end if;
1575
1576 -- Find the current last tag
1577
1578 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1579 Ext := Record_Extension_Part (Type_Definition (N));
1580 else
1581 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1582 Ext := Type_Definition (N);
1583 end if;
1584
1585 Last_Tag := Empty;
1586
1587 if not (Present (Component_List (Ext))) then
1588 Set_Null_Present (Ext, False);
1589 L := New_List;
1590 Set_Component_List (Ext,
1591 Make_Component_List (Loc,
1592 Component_Items => L,
1593 Null_Present => False));
1594 else
1595 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1596 L := Component_Items
1597 (Component_List
1598 (Record_Extension_Part
1599 (Type_Definition (N))));
1600 else
1601 L := Component_Items
1602 (Component_List
1603 (Type_Definition (N)));
1604 end if;
1605
1606 -- Find the last tag component
1607
1608 Comp := First (L);
1609 while Present (Comp) loop
1610 if Nkind (Comp) = N_Component_Declaration
1611 and then Is_Tag (Defining_Identifier (Comp))
1612 then
1613 Last_Tag := Comp;
1614 end if;
1615
1616 Next (Comp);
1617 end loop;
1618 end if;
1619
1620 -- At this point L references the list of components and Last_Tag
1621 -- references the current last tag (if any). Now we add the tag
1622 -- corresponding with all the interfaces that are not implemented
1623 -- by the parent.
1624
1625 if Present (Interfaces (Typ)) then
1626 Elmt := First_Elmt (Interfaces (Typ));
1627 while Present (Elmt) loop
1628 Add_Tag (Node (Elmt));
1629 Next_Elmt (Elmt);
1630 end loop;
1631 end if;
1632 end Add_Interface_Tag_Components;
1633
1634 -------------------------------------
1635 -- Add_Internal_Interface_Entities --
1636 -------------------------------------
1637
1638 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1639 Elmt : Elmt_Id;
1640 Iface : Entity_Id;
1641 Iface_Elmt : Elmt_Id;
1642 Iface_Prim : Entity_Id;
1643 Ifaces_List : Elist_Id;
1644 New_Subp : Entity_Id := Empty;
1645 Prim : Entity_Id;
1646 Restore_Scope : Boolean := False;
1647
1648 begin
1649 pragma Assert (Ada_Version >= Ada_2005
1650 and then Is_Record_Type (Tagged_Type)
1651 and then Is_Tagged_Type (Tagged_Type)
1652 and then Has_Interfaces (Tagged_Type)
1653 and then not Is_Interface (Tagged_Type));
1654
1655 -- Ensure that the internal entities are added to the scope of the type
1656
1657 if Scope (Tagged_Type) /= Current_Scope then
1658 Push_Scope (Scope (Tagged_Type));
1659 Restore_Scope := True;
1660 end if;
1661
1662 Collect_Interfaces (Tagged_Type, Ifaces_List);
1663
1664 Iface_Elmt := First_Elmt (Ifaces_List);
1665 while Present (Iface_Elmt) loop
1666 Iface := Node (Iface_Elmt);
1667
1668 -- Originally we excluded here from this processing interfaces that
1669 -- are parents of Tagged_Type because their primitives are located
1670 -- in the primary dispatch table (and hence no auxiliary internal
1671 -- entities are required to handle secondary dispatch tables in such
1672 -- case). However, these auxiliary entities are also required to
1673 -- handle derivations of interfaces in formals of generics (see
1674 -- Derive_Subprograms).
1675
1676 Elmt := First_Elmt (Primitive_Operations (Iface));
1677 while Present (Elmt) loop
1678 Iface_Prim := Node (Elmt);
1679
1680 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1681 Prim :=
1682 Find_Primitive_Covering_Interface
1683 (Tagged_Type => Tagged_Type,
1684 Iface_Prim => Iface_Prim);
1685
1686 if No (Prim) and then Serious_Errors_Detected > 0 then
1687 goto Continue;
1688 end if;
1689
1690 pragma Assert (Present (Prim));
1691
1692 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1693 -- differs from the name of the interface primitive then it is
1694 -- a private primitive inherited from a parent type. In such
1695 -- case, given that Tagged_Type covers the interface, the
1696 -- inherited private primitive becomes visible. For such
1697 -- purpose we add a new entity that renames the inherited
1698 -- private primitive.
1699
1700 if Chars (Prim) /= Chars (Iface_Prim) then
1701 pragma Assert (Has_Suffix (Prim, 'P'));
1702 Derive_Subprogram
1703 (New_Subp => New_Subp,
1704 Parent_Subp => Iface_Prim,
1705 Derived_Type => Tagged_Type,
1706 Parent_Type => Iface);
1707 Set_Alias (New_Subp, Prim);
1708 Set_Is_Abstract_Subprogram
1709 (New_Subp, Is_Abstract_Subprogram (Prim));
1710 end if;
1711
1712 Derive_Subprogram
1713 (New_Subp => New_Subp,
1714 Parent_Subp => Iface_Prim,
1715 Derived_Type => Tagged_Type,
1716 Parent_Type => Iface);
1717
1718 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1719 -- associated with interface types. These entities are
1720 -- only registered in the list of primitives of its
1721 -- corresponding tagged type because they are only used
1722 -- to fill the contents of the secondary dispatch tables.
1723 -- Therefore they are removed from the homonym chains.
1724
1725 Set_Is_Hidden (New_Subp);
1726 Set_Is_Internal (New_Subp);
1727 Set_Alias (New_Subp, Prim);
1728 Set_Is_Abstract_Subprogram
1729 (New_Subp, Is_Abstract_Subprogram (Prim));
1730 Set_Interface_Alias (New_Subp, Iface_Prim);
1731
1732 -- If the returned type is an interface then propagate it to
1733 -- the returned type. Needed by the thunk to generate the code
1734 -- which displaces "this" to reference the corresponding
1735 -- secondary dispatch table in the returned object.
1736
1737 if Is_Interface (Etype (Iface_Prim)) then
1738 Set_Etype (New_Subp, Etype (Iface_Prim));
1739 end if;
1740
1741 -- Internal entities associated with interface types are only
1742 -- registered in the list of primitives of the tagged type.
1743 -- They are only used to fill the contents of the secondary
1744 -- dispatch tables. Therefore they are not needed in the
1745 -- homonym chains.
1746
1747 Remove_Homonym (New_Subp);
1748
1749 -- Hidden entities associated with interfaces must have set
1750 -- the Has_Delay_Freeze attribute to ensure that, in case
1751 -- of locally defined tagged types (or compiling with static
1752 -- dispatch tables generation disabled) the corresponding
1753 -- entry of the secondary dispatch table is filled when such
1754 -- an entity is frozen. This is an expansion activity that must
1755 -- be suppressed for ASIS because it leads to gigi elaboration
1756 -- issues in annotate mode.
1757
1758 if not ASIS_Mode then
1759 Set_Has_Delayed_Freeze (New_Subp);
1760 end if;
1761 end if;
1762
1763 <<Continue>>
1764 Next_Elmt (Elmt);
1765 end loop;
1766
1767 Next_Elmt (Iface_Elmt);
1768 end loop;
1769
1770 if Restore_Scope then
1771 Pop_Scope;
1772 end if;
1773 end Add_Internal_Interface_Entities;
1774
1775 -----------------------------------
1776 -- Analyze_Component_Declaration --
1777 -----------------------------------
1778
1779 procedure Analyze_Component_Declaration (N : Node_Id) is
1780 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1781 Id : constant Entity_Id := Defining_Identifier (N);
1782 E : constant Node_Id := Expression (N);
1783 Typ : constant Node_Id :=
1784 Subtype_Indication (Component_Definition (N));
1785 T : Entity_Id;
1786 P : Entity_Id;
1787
1788 function Contains_POC (Constr : Node_Id) return Boolean;
1789 -- Determines whether a constraint uses the discriminant of a record
1790 -- type thus becoming a per-object constraint (POC).
1791
1792 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1793 -- Typ is the type of the current component, check whether this type is
1794 -- a limited type. Used to validate declaration against that of
1795 -- enclosing record.
1796
1797 ------------------
1798 -- Contains_POC --
1799 ------------------
1800
1801 function Contains_POC (Constr : Node_Id) return Boolean is
1802 begin
1803 -- Prevent cascaded errors
1804
1805 if Error_Posted (Constr) then
1806 return False;
1807 end if;
1808
1809 case Nkind (Constr) is
1810 when N_Attribute_Reference =>
1811 return Attribute_Name (Constr) = Name_Access
1812 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1813
1814 when N_Discriminant_Association =>
1815 return Denotes_Discriminant (Expression (Constr));
1816
1817 when N_Identifier =>
1818 return Denotes_Discriminant (Constr);
1819
1820 when N_Index_Or_Discriminant_Constraint =>
1821 declare
1822 IDC : Node_Id;
1823
1824 begin
1825 IDC := First (Constraints (Constr));
1826 while Present (IDC) loop
1827
1828 -- One per-object constraint is sufficient
1829
1830 if Contains_POC (IDC) then
1831 return True;
1832 end if;
1833
1834 Next (IDC);
1835 end loop;
1836
1837 return False;
1838 end;
1839
1840 when N_Range =>
1841 return Denotes_Discriminant (Low_Bound (Constr))
1842 or else
1843 Denotes_Discriminant (High_Bound (Constr));
1844
1845 when N_Range_Constraint =>
1846 return Denotes_Discriminant (Range_Expression (Constr));
1847
1848 when others =>
1849 return False;
1850
1851 end case;
1852 end Contains_POC;
1853
1854 ----------------------
1855 -- Is_Known_Limited --
1856 ----------------------
1857
1858 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1859 P : constant Entity_Id := Etype (Typ);
1860 R : constant Entity_Id := Root_Type (Typ);
1861
1862 begin
1863 if Is_Limited_Record (Typ) then
1864 return True;
1865
1866 -- If the root type is limited (and not a limited interface)
1867 -- so is the current type
1868
1869 elsif Is_Limited_Record (R)
1870 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1871 then
1872 return True;
1873
1874 -- Else the type may have a limited interface progenitor, but a
1875 -- limited record parent.
1876
1877 elsif R /= P and then Is_Limited_Record (P) then
1878 return True;
1879
1880 else
1881 return False;
1882 end if;
1883 end Is_Known_Limited;
1884
1885 -- Start of processing for Analyze_Component_Declaration
1886
1887 begin
1888 Generate_Definition (Id);
1889 Enter_Name (Id);
1890
1891 if Present (Typ) then
1892 T := Find_Type_Of_Object
1893 (Subtype_Indication (Component_Definition (N)), N);
1894
1895 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1896 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1897 end if;
1898
1899 -- Ada 2005 (AI-230): Access Definition case
1900
1901 else
1902 pragma Assert (Present
1903 (Access_Definition (Component_Definition (N))));
1904
1905 T := Access_Definition
1906 (Related_Nod => N,
1907 N => Access_Definition (Component_Definition (N)));
1908 Set_Is_Local_Anonymous_Access (T);
1909
1910 -- Ada 2005 (AI-254)
1911
1912 if Present (Access_To_Subprogram_Definition
1913 (Access_Definition (Component_Definition (N))))
1914 and then Protected_Present (Access_To_Subprogram_Definition
1915 (Access_Definition
1916 (Component_Definition (N))))
1917 then
1918 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1919 end if;
1920 end if;
1921
1922 -- If the subtype is a constrained subtype of the enclosing record,
1923 -- (which must have a partial view) the back-end does not properly
1924 -- handle the recursion. Rewrite the component declaration with an
1925 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1926 -- the tree directly because side effects have already been removed from
1927 -- discriminant constraints.
1928
1929 if Ekind (T) = E_Access_Subtype
1930 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1931 and then Comes_From_Source (T)
1932 and then Nkind (Parent (T)) = N_Subtype_Declaration
1933 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1934 then
1935 Rewrite
1936 (Subtype_Indication (Component_Definition (N)),
1937 New_Copy_Tree (Subtype_Indication (Parent (T))));
1938 T := Find_Type_Of_Object
1939 (Subtype_Indication (Component_Definition (N)), N);
1940 end if;
1941
1942 -- If the component declaration includes a default expression, then we
1943 -- check that the component is not of a limited type (RM 3.7(5)),
1944 -- and do the special preanalysis of the expression (see section on
1945 -- "Handling of Default and Per-Object Expressions" in the spec of
1946 -- package Sem).
1947
1948 if Present (E) then
1949 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1950 Preanalyze_Default_Expression (E, T);
1951 Check_Initialization (T, E);
1952
1953 if Ada_Version >= Ada_2005
1954 and then Ekind (T) = E_Anonymous_Access_Type
1955 and then Etype (E) /= Any_Type
1956 then
1957 -- Check RM 3.9.2(9): "if the expected type for an expression is
1958 -- an anonymous access-to-specific tagged type, then the object
1959 -- designated by the expression shall not be dynamically tagged
1960 -- unless it is a controlling operand in a call on a dispatching
1961 -- operation"
1962
1963 if Is_Tagged_Type (Directly_Designated_Type (T))
1964 and then
1965 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1966 and then
1967 Ekind (Directly_Designated_Type (Etype (E))) =
1968 E_Class_Wide_Type
1969 then
1970 Error_Msg_N
1971 ("access to specific tagged type required (RM 3.9.2(9))", E);
1972 end if;
1973
1974 -- (Ada 2005: AI-230): Accessibility check for anonymous
1975 -- components
1976
1977 if Type_Access_Level (Etype (E)) >
1978 Deepest_Type_Access_Level (T)
1979 then
1980 Error_Msg_N
1981 ("expression has deeper access level than component " &
1982 "(RM 3.10.2 (12.2))", E);
1983 end if;
1984
1985 -- The initialization expression is a reference to an access
1986 -- discriminant. The type of the discriminant is always deeper
1987 -- than any access type.
1988
1989 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1990 and then Is_Entity_Name (E)
1991 and then Ekind (Entity (E)) = E_In_Parameter
1992 and then Present (Discriminal_Link (Entity (E)))
1993 then
1994 Error_Msg_N
1995 ("discriminant has deeper accessibility level than target",
1996 E);
1997 end if;
1998 end if;
1999 end if;
2000
2001 -- The parent type may be a private view with unknown discriminants,
2002 -- and thus unconstrained. Regular components must be constrained.
2003
2004 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2005 if Is_Class_Wide_Type (T) then
2006 Error_Msg_N
2007 ("class-wide subtype with unknown discriminants" &
2008 " in component declaration",
2009 Subtype_Indication (Component_Definition (N)));
2010 else
2011 Error_Msg_N
2012 ("unconstrained subtype in component declaration",
2013 Subtype_Indication (Component_Definition (N)));
2014 end if;
2015
2016 -- Components cannot be abstract, except for the special case of
2017 -- the _Parent field (case of extending an abstract tagged type)
2018
2019 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2020 Error_Msg_N ("type of a component cannot be abstract", N);
2021 end if;
2022
2023 Set_Etype (Id, T);
2024 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2025
2026 -- The component declaration may have a per-object constraint, set
2027 -- the appropriate flag in the defining identifier of the subtype.
2028
2029 if Present (Subtype_Indication (Component_Definition (N))) then
2030 declare
2031 Sindic : constant Node_Id :=
2032 Subtype_Indication (Component_Definition (N));
2033 begin
2034 if Nkind (Sindic) = N_Subtype_Indication
2035 and then Present (Constraint (Sindic))
2036 and then Contains_POC (Constraint (Sindic))
2037 then
2038 Set_Has_Per_Object_Constraint (Id);
2039 end if;
2040 end;
2041 end if;
2042
2043 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2044 -- out some static checks.
2045
2046 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2047 Null_Exclusion_Static_Checks (N);
2048 end if;
2049
2050 -- If this component is private (or depends on a private type), flag the
2051 -- record type to indicate that some operations are not available.
2052
2053 P := Private_Component (T);
2054
2055 if Present (P) then
2056
2057 -- Check for circular definitions
2058
2059 if P = Any_Type then
2060 Set_Etype (Id, Any_Type);
2061
2062 -- There is a gap in the visibility of operations only if the
2063 -- component type is not defined in the scope of the record type.
2064
2065 elsif Scope (P) = Scope (Current_Scope) then
2066 null;
2067
2068 elsif Is_Limited_Type (P) then
2069 Set_Is_Limited_Composite (Current_Scope);
2070
2071 else
2072 Set_Is_Private_Composite (Current_Scope);
2073 end if;
2074 end if;
2075
2076 if P /= Any_Type
2077 and then Is_Limited_Type (T)
2078 and then Chars (Id) /= Name_uParent
2079 and then Is_Tagged_Type (Current_Scope)
2080 then
2081 if Is_Derived_Type (Current_Scope)
2082 and then not Is_Known_Limited (Current_Scope)
2083 then
2084 Error_Msg_N
2085 ("extension of nonlimited type cannot have limited components",
2086 N);
2087
2088 if Is_Interface (Root_Type (Current_Scope)) then
2089 Error_Msg_N
2090 ("\limitedness is not inherited from limited interface", N);
2091 Error_Msg_N ("\add LIMITED to type indication", N);
2092 end if;
2093
2094 Explain_Limited_Type (T, N);
2095 Set_Etype (Id, Any_Type);
2096 Set_Is_Limited_Composite (Current_Scope, False);
2097
2098 elsif not Is_Derived_Type (Current_Scope)
2099 and then not Is_Limited_Record (Current_Scope)
2100 and then not Is_Concurrent_Type (Current_Scope)
2101 then
2102 Error_Msg_N
2103 ("nonlimited tagged type cannot have limited components", N);
2104 Explain_Limited_Type (T, N);
2105 Set_Etype (Id, Any_Type);
2106 Set_Is_Limited_Composite (Current_Scope, False);
2107 end if;
2108 end if;
2109
2110 -- If the component is an unconstrained task or protected type with
2111 -- discriminants, the component and the enclosing record are limited
2112 -- and the component is constrained by its default values. Compute
2113 -- its actual subtype, else it may be allocated the maximum size by
2114 -- the backend, and possibly overflow.
2115
2116 if Is_Concurrent_Type (T)
2117 and then not Is_Constrained (T)
2118 and then Has_Discriminants (T)
2119 and then not Has_Discriminants (Current_Scope)
2120 then
2121 declare
2122 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2123
2124 begin
2125 Set_Etype (Id, Act_T);
2126
2127 -- Rewrite component definition to use the constrained subtype
2128
2129 Rewrite (Component_Definition (N),
2130 Make_Component_Definition (Loc,
2131 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2132 end;
2133 end if;
2134
2135 Set_Original_Record_Component (Id, Id);
2136
2137 if Has_Aspects (N) then
2138 Analyze_Aspect_Specifications (N, Id);
2139 end if;
2140
2141 Analyze_Dimension (N);
2142 end Analyze_Component_Declaration;
2143
2144 --------------------------
2145 -- Analyze_Declarations --
2146 --------------------------
2147
2148 procedure Analyze_Declarations (L : List_Id) is
2149 Decl : Node_Id;
2150
2151 procedure Adjust_Decl;
2152 -- Adjust Decl not to include implicit label declarations, since these
2153 -- have strange Sloc values that result in elaboration check problems.
2154 -- (They have the sloc of the label as found in the source, and that
2155 -- is ahead of the current declarative part).
2156
2157 procedure Check_Entry_Contracts;
2158 -- Perform a pre-analysis of the pre- and postconditions of an entry
2159 -- declaration. This must be done before full resolution and creation
2160 -- of the parameter block, etc. to catch illegal uses within the
2161 -- contract expression. Full analysis of the expression is done when
2162 -- the contract is processed.
2163
2164 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2165 -- Determine whether Body_Decl denotes the body of a late controlled
2166 -- primitive (either Initialize, Adjust or Finalize). If this is the
2167 -- case, add a proper spec if the body lacks one. The spec is inserted
2168 -- before Body_Decl and immediately analyzed.
2169
2170 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2171 -- Spec_Id is the entity of a package that may define abstract states,
2172 -- and in the case of a child unit, whose ancestors may define abstract
2173 -- states. If the states have partial visible refinement, remove the
2174 -- partial visibility of each constituent at the end of the package
2175 -- spec and body declarations.
2176
2177 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2178 -- Spec_Id is the entity of a package that may define abstract states.
2179 -- If the states have visible refinement, remove the visibility of each
2180 -- constituent at the end of the package body declaration.
2181
2182 -----------------
2183 -- Adjust_Decl --
2184 -----------------
2185
2186 procedure Adjust_Decl is
2187 begin
2188 while Present (Prev (Decl))
2189 and then Nkind (Decl) = N_Implicit_Label_Declaration
2190 loop
2191 Prev (Decl);
2192 end loop;
2193 end Adjust_Decl;
2194
2195 ---------------------------
2196 -- Check_Entry_Contracts --
2197 ---------------------------
2198
2199 procedure Check_Entry_Contracts is
2200 ASN : Node_Id;
2201 Ent : Entity_Id;
2202 Exp : Node_Id;
2203
2204 begin
2205 Ent := First_Entity (Current_Scope);
2206 while Present (Ent) loop
2207
2208 -- This only concerns entries with pre/postconditions
2209
2210 if Ekind (Ent) = E_Entry
2211 and then Present (Contract (Ent))
2212 and then Present (Pre_Post_Conditions (Contract (Ent)))
2213 then
2214 ASN := Pre_Post_Conditions (Contract (Ent));
2215 Push_Scope (Ent);
2216 Install_Formals (Ent);
2217
2218 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2219 -- is performed on a copy of the pragma expression, to prevent
2220 -- modifying the original expression.
2221
2222 while Present (ASN) loop
2223 if Nkind (ASN) = N_Pragma then
2224 Exp :=
2225 New_Copy_Tree
2226 (Expression
2227 (First (Pragma_Argument_Associations (ASN))));
2228 Set_Parent (Exp, ASN);
2229
2230 -- ??? why not Preanalyze_Assert_Expression
2231
2232 Preanalyze (Exp);
2233 end if;
2234
2235 ASN := Next_Pragma (ASN);
2236 end loop;
2237
2238 End_Scope;
2239 end if;
2240
2241 Next_Entity (Ent);
2242 end loop;
2243 end Check_Entry_Contracts;
2244
2245 --------------------------------------
2246 -- Handle_Late_Controlled_Primitive --
2247 --------------------------------------
2248
2249 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2250 Body_Spec : constant Node_Id := Specification (Body_Decl);
2251 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2252 Loc : constant Source_Ptr := Sloc (Body_Id);
2253 Params : constant List_Id :=
2254 Parameter_Specifications (Body_Spec);
2255 Spec : Node_Id;
2256 Spec_Id : Entity_Id;
2257 Typ : Node_Id;
2258
2259 begin
2260 -- Consider only procedure bodies whose name matches one of the three
2261 -- controlled primitives.
2262
2263 if Nkind (Body_Spec) /= N_Procedure_Specification
2264 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2265 Name_Finalize,
2266 Name_Initialize)
2267 then
2268 return;
2269
2270 -- A controlled primitive must have exactly one formal which is not
2271 -- an anonymous access type.
2272
2273 elsif List_Length (Params) /= 1 then
2274 return;
2275 end if;
2276
2277 Typ := Parameter_Type (First (Params));
2278
2279 if Nkind (Typ) = N_Access_Definition then
2280 return;
2281 end if;
2282
2283 Find_Type (Typ);
2284
2285 -- The type of the formal must be derived from [Limited_]Controlled
2286
2287 if not Is_Controlled (Entity (Typ)) then
2288 return;
2289 end if;
2290
2291 -- Check whether a specification exists for this body. We do not
2292 -- analyze the spec of the body in full, because it will be analyzed
2293 -- again when the body is properly analyzed, and we cannot create
2294 -- duplicate entries in the formals chain. We look for an explicit
2295 -- specification because the body may be an overriding operation and
2296 -- an inherited spec may be present.
2297
2298 Spec_Id := Current_Entity (Body_Id);
2299
2300 while Present (Spec_Id) loop
2301 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2302 and then Scope (Spec_Id) = Current_Scope
2303 and then Present (First_Formal (Spec_Id))
2304 and then No (Next_Formal (First_Formal (Spec_Id)))
2305 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2306 and then Comes_From_Source (Spec_Id)
2307 then
2308 return;
2309 end if;
2310
2311 Spec_Id := Homonym (Spec_Id);
2312 end loop;
2313
2314 -- At this point the body is known to be a late controlled primitive.
2315 -- Generate a matching spec and insert it before the body. Note the
2316 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2317 -- tree in this case.
2318
2319 Spec := Copy_Separate_Tree (Body_Spec);
2320
2321 -- Ensure that the subprogram declaration does not inherit the null
2322 -- indicator from the body as we now have a proper spec/body pair.
2323
2324 Set_Null_Present (Spec, False);
2325
2326 -- Ensure that the freeze node is inserted after the declaration of
2327 -- the primitive since its expansion will freeze the primitive.
2328
2329 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2330
2331 Insert_Before_And_Analyze (Body_Decl, Decl);
2332 end Handle_Late_Controlled_Primitive;
2333
2334 ----------------------------------------
2335 -- Remove_Partial_Visible_Refinements --
2336 ----------------------------------------
2337
2338 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2339 State_Elmt : Elmt_Id;
2340 begin
2341 if Present (Abstract_States (Spec_Id)) then
2342 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2343 while Present (State_Elmt) loop
2344 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2345 Next_Elmt (State_Elmt);
2346 end loop;
2347 end if;
2348
2349 -- For a child unit, also hide the partial state refinement from
2350 -- ancestor packages.
2351
2352 if Is_Child_Unit (Spec_Id) then
2353 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2354 end if;
2355 end Remove_Partial_Visible_Refinements;
2356
2357 --------------------------------
2358 -- Remove_Visible_Refinements --
2359 --------------------------------
2360
2361 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2362 State_Elmt : Elmt_Id;
2363 begin
2364 if Present (Abstract_States (Spec_Id)) then
2365 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2366 while Present (State_Elmt) loop
2367 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2368 Next_Elmt (State_Elmt);
2369 end loop;
2370 end if;
2371 end Remove_Visible_Refinements;
2372
2373 -- Local variables
2374
2375 Context : Node_Id := Empty;
2376 Freeze_From : Entity_Id := Empty;
2377 Next_Decl : Node_Id;
2378
2379 Body_Seen : Boolean := False;
2380 -- Flag set when the first body [stub] is encountered
2381
2382 -- Start of processing for Analyze_Declarations
2383
2384 begin
2385 if Restriction_Check_Required (SPARK_05) then
2386 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2387 end if;
2388
2389 Decl := First (L);
2390 while Present (Decl) loop
2391
2392 -- Package spec cannot contain a package declaration in SPARK
2393
2394 if Nkind (Decl) = N_Package_Declaration
2395 and then Nkind (Parent (L)) = N_Package_Specification
2396 then
2397 Check_SPARK_05_Restriction
2398 ("package specification cannot contain a package declaration",
2399 Decl);
2400 end if;
2401
2402 -- Complete analysis of declaration
2403
2404 Analyze (Decl);
2405 Next_Decl := Next (Decl);
2406
2407 if No (Freeze_From) then
2408 Freeze_From := First_Entity (Current_Scope);
2409 end if;
2410
2411 -- At the end of a declarative part, freeze remaining entities
2412 -- declared in it. The end of the visible declarations of package
2413 -- specification is not the end of a declarative part if private
2414 -- declarations are present. The end of a package declaration is a
2415 -- freezing point only if it a library package. A task definition or
2416 -- protected type definition is not a freeze point either. Finally,
2417 -- we do not freeze entities in generic scopes, because there is no
2418 -- code generated for them and freeze nodes will be generated for
2419 -- the instance.
2420
2421 -- The end of a package instantiation is not a freeze point, but
2422 -- for now we make it one, because the generic body is inserted
2423 -- (currently) immediately after. Generic instantiations will not
2424 -- be a freeze point once delayed freezing of bodies is implemented.
2425 -- (This is needed in any case for early instantiations ???).
2426
2427 if No (Next_Decl) then
2428 if Nkind (Parent (L)) = N_Component_List then
2429 null;
2430
2431 elsif Nkind_In (Parent (L), N_Protected_Definition,
2432 N_Task_Definition)
2433 then
2434 Check_Entry_Contracts;
2435
2436 elsif Nkind (Parent (L)) /= N_Package_Specification then
2437 if Nkind (Parent (L)) = N_Package_Body then
2438 Freeze_From := First_Entity (Current_Scope);
2439 end if;
2440
2441 -- There may have been several freezing points previously,
2442 -- for example object declarations or subprogram bodies, but
2443 -- at the end of a declarative part we check freezing from
2444 -- the beginning, even though entities may already be frozen,
2445 -- in order to perform visibility checks on delayed aspects.
2446
2447 Adjust_Decl;
2448 Freeze_All (First_Entity (Current_Scope), Decl);
2449 Freeze_From := Last_Entity (Current_Scope);
2450
2451 elsif Scope (Current_Scope) /= Standard_Standard
2452 and then not Is_Child_Unit (Current_Scope)
2453 and then No (Generic_Parent (Parent (L)))
2454 then
2455 null;
2456
2457 elsif L /= Visible_Declarations (Parent (L))
2458 or else No (Private_Declarations (Parent (L)))
2459 or else Is_Empty_List (Private_Declarations (Parent (L)))
2460 then
2461 Adjust_Decl;
2462 Freeze_All (First_Entity (Current_Scope), Decl);
2463 Freeze_From := Last_Entity (Current_Scope);
2464
2465 -- At the end of the visible declarations the expressions in
2466 -- aspects of all entities declared so far must be resolved.
2467 -- The entities themselves might be frozen later, and the
2468 -- generated pragmas and attribute definition clauses analyzed
2469 -- in full at that point, but name resolution must take place
2470 -- now.
2471 -- In addition to being the proper semantics, this is mandatory
2472 -- within generic units, because global name capture requires
2473 -- those expressions to be analyzed, given that the generated
2474 -- pragmas do not appear in the original generic tree.
2475
2476 elsif Serious_Errors_Detected = 0 then
2477 declare
2478 E : Entity_Id;
2479
2480 begin
2481 E := First_Entity (Current_Scope);
2482 while Present (E) loop
2483 Resolve_Aspect_Expressions (E);
2484 Next_Entity (E);
2485 end loop;
2486 end;
2487 end if;
2488
2489 -- If next node is a body then freeze all types before the body.
2490 -- An exception occurs for some expander-generated bodies. If these
2491 -- are generated at places where in general language rules would not
2492 -- allow a freeze point, then we assume that the expander has
2493 -- explicitly checked that all required types are properly frozen,
2494 -- and we do not cause general freezing here. This special circuit
2495 -- is used when the encountered body is marked as having already
2496 -- been analyzed.
2497
2498 -- In all other cases (bodies that come from source, and expander
2499 -- generated bodies that have not been analyzed yet), freeze all
2500 -- types now. Note that in the latter case, the expander must take
2501 -- care to attach the bodies at a proper place in the tree so as to
2502 -- not cause unwanted freezing at that point.
2503
2504 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2505
2506 -- When a controlled type is frozen, the expander generates stream
2507 -- and controlled type support routines. If the freeze is caused
2508 -- by the stand alone body of Initialize, Adjust and Finalize, the
2509 -- expander will end up using the wrong version of these routines
2510 -- as the body has not been processed yet. To remedy this, detect
2511 -- a late controlled primitive and create a proper spec for it.
2512 -- This ensures that the primitive will override its inherited
2513 -- counterpart before the freeze takes place.
2514
2515 -- If the declaration we just processed is a body, do not attempt
2516 -- to examine Next_Decl as the late primitive idiom can only apply
2517 -- to the first encountered body.
2518
2519 -- The spec of the late primitive is not generated in ASIS mode to
2520 -- ensure a consistent list of primitives that indicates the true
2521 -- semantic structure of the program (which is not relevant when
2522 -- generating executable code.
2523
2524 -- ??? a cleaner approach may be possible and/or this solution
2525 -- could be extended to general-purpose late primitives, TBD.
2526
2527 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2528 then
2529 Body_Seen := True;
2530
2531 if Nkind (Next_Decl) = N_Subprogram_Body then
2532 Handle_Late_Controlled_Primitive (Next_Decl);
2533 end if;
2534 end if;
2535
2536 Adjust_Decl;
2537 Freeze_All (Freeze_From, Decl);
2538 Freeze_From := Last_Entity (Current_Scope);
2539 end if;
2540
2541 Decl := Next_Decl;
2542 end loop;
2543
2544 -- Analyze the contracts of packages and their bodies
2545
2546 if Present (L) then
2547 Context := Parent (L);
2548
2549 if Nkind (Context) = N_Package_Specification then
2550
2551 -- When a package has private declarations, its contract must be
2552 -- analyzed at the end of the said declarations. This way both the
2553 -- analysis and freeze actions are properly synchronized in case
2554 -- of private type use within the contract.
2555
2556 if L = Private_Declarations (Context) then
2557 Analyze_Package_Contract (Defining_Entity (Context));
2558
2559 -- Otherwise the contract is analyzed at the end of the visible
2560 -- declarations.
2561
2562 elsif L = Visible_Declarations (Context)
2563 and then No (Private_Declarations (Context))
2564 then
2565 Analyze_Package_Contract (Defining_Entity (Context));
2566 end if;
2567
2568 elsif Nkind (Context) = N_Package_Body then
2569 Analyze_Package_Body_Contract (Defining_Entity (Context));
2570 end if;
2571
2572 -- Analyze the contracts of various constructs now due to the delayed
2573 -- visibility needs of their aspects and pragmas.
2574
2575 Analyze_Contracts (L);
2576
2577 if Nkind (Context) = N_Package_Body then
2578
2579 -- Ensure that all abstract states and objects declared in the
2580 -- state space of a package body are utilized as constituents.
2581
2582 Check_Unused_Body_States (Defining_Entity (Context));
2583
2584 -- State refinements are visible up to the end of the package body
2585 -- declarations. Hide the state refinements from visibility to
2586 -- restore the original state conditions.
2587
2588 Remove_Visible_Refinements (Corresponding_Spec (Context));
2589 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2590
2591 elsif Nkind (Context) = N_Package_Declaration then
2592
2593 -- Partial state refinements are visible up to the end of the
2594 -- package spec declarations. Hide the partial state refinements
2595 -- from visibility to restore the original state conditions.
2596
2597 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2598 end if;
2599
2600 -- Verify that all abstract states found in any package declared in
2601 -- the input declarative list have proper refinements. The check is
2602 -- performed only when the context denotes a block, entry, package,
2603 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2604
2605 Check_State_Refinements (Context);
2606 end if;
2607 end Analyze_Declarations;
2608
2609 -----------------------------------
2610 -- Analyze_Full_Type_Declaration --
2611 -----------------------------------
2612
2613 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2614 Def : constant Node_Id := Type_Definition (N);
2615 Def_Id : constant Entity_Id := Defining_Identifier (N);
2616 T : Entity_Id;
2617 Prev : Entity_Id;
2618
2619 Is_Remote : constant Boolean :=
2620 (Is_Remote_Types (Current_Scope)
2621 or else Is_Remote_Call_Interface (Current_Scope))
2622 and then not (In_Private_Part (Current_Scope)
2623 or else In_Package_Body (Current_Scope));
2624
2625 procedure Check_Nonoverridable_Aspects;
2626 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2627 -- be overridden, and can only be confirmed on derivation.
2628
2629 procedure Check_Ops_From_Incomplete_Type;
2630 -- If there is a tagged incomplete partial view of the type, traverse
2631 -- the primitives of the incomplete view and change the type of any
2632 -- controlling formals and result to indicate the full view. The
2633 -- primitives will be added to the full type's primitive operations
2634 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2635 -- is called from Process_Incomplete_Dependents).
2636
2637 ----------------------------------
2638 -- Check_Nonoverridable_Aspects --
2639 ----------------------------------
2640
2641 procedure Check_Nonoverridable_Aspects is
2642 Prev_Aspects : constant List_Id :=
2643 Aspect_Specifications (Parent (Def_Id));
2644 Par_Type : Entity_Id;
2645
2646 function Has_Aspect_Spec
2647 (Specs : List_Id;
2648 Aspect_Name : Name_Id) return Boolean;
2649 -- Check whether a list of aspect specifications includes an entry
2650 -- for a specific aspect. The list is either that of a partial or
2651 -- a full view.
2652
2653 ---------------------
2654 -- Has_Aspect_Spec --
2655 ---------------------
2656
2657 function Has_Aspect_Spec
2658 (Specs : List_Id;
2659 Aspect_Name : Name_Id) return Boolean
2660 is
2661 Spec : Node_Id;
2662 begin
2663 Spec := First (Specs);
2664 while Present (Spec) loop
2665 if Chars (Identifier (Spec)) = Aspect_Name then
2666 return True;
2667 end if;
2668 Next (Spec);
2669 end loop;
2670 return False;
2671 end Has_Aspect_Spec;
2672
2673 -- Start of processing for Check_Nonoverridable_Aspects
2674
2675 begin
2676
2677 -- Get parent type of derived type. Note that Prev is the entity
2678 -- in the partial declaration, but its contents are now those of
2679 -- full view, while Def_Id reflects the partial view.
2680
2681 if Is_Private_Type (Def_Id) then
2682 Par_Type := Etype (Full_View (Def_Id));
2683 else
2684 Par_Type := Etype (Def_Id);
2685 end if;
2686
2687 -- If there is an inherited Implicit_Dereference, verify that it is
2688 -- made explicit in the partial view.
2689
2690 if Has_Discriminants (Base_Type (Par_Type))
2691 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2692 and then Present (Discriminant_Specifications (Parent (Prev)))
2693 and then Present (Get_Reference_Discriminant (Par_Type))
2694 then
2695 if
2696 not Has_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference)
2697 then
2698 Error_Msg_N
2699 ("type does not inherit implicit dereference", Prev);
2700
2701 else
2702 -- If one of the views has the aspect specified, verify that it
2703 -- is consistent with that of the parent.
2704
2705 declare
2706 Par_Discr : constant Entity_Id :=
2707 Get_Reference_Discriminant (Par_Type);
2708 Cur_Discr : constant Entity_Id :=
2709 Get_Reference_Discriminant (Prev);
2710 begin
2711 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2712 Error_Msg_N ("aspect incosistent with that of parent", N);
2713 end if;
2714 end;
2715 end if;
2716 end if;
2717
2718 -- TBD : other nonoverridable aspects.
2719 end Check_Nonoverridable_Aspects;
2720
2721 ------------------------------------
2722 -- Check_Ops_From_Incomplete_Type --
2723 ------------------------------------
2724
2725 procedure Check_Ops_From_Incomplete_Type is
2726 Elmt : Elmt_Id;
2727 Formal : Entity_Id;
2728 Op : Entity_Id;
2729
2730 begin
2731 if Prev /= T
2732 and then Ekind (Prev) = E_Incomplete_Type
2733 and then Is_Tagged_Type (Prev)
2734 and then Is_Tagged_Type (T)
2735 then
2736 Elmt := First_Elmt (Primitive_Operations (Prev));
2737 while Present (Elmt) loop
2738 Op := Node (Elmt);
2739
2740 Formal := First_Formal (Op);
2741 while Present (Formal) loop
2742 if Etype (Formal) = Prev then
2743 Set_Etype (Formal, T);
2744 end if;
2745
2746 Next_Formal (Formal);
2747 end loop;
2748
2749 if Etype (Op) = Prev then
2750 Set_Etype (Op, T);
2751 end if;
2752
2753 Next_Elmt (Elmt);
2754 end loop;
2755 end if;
2756 end Check_Ops_From_Incomplete_Type;
2757
2758 -- Start of processing for Analyze_Full_Type_Declaration
2759
2760 begin
2761 Prev := Find_Type_Name (N);
2762
2763 -- The full view, if present, now points to the current type. If there
2764 -- is an incomplete partial view, set a link to it, to simplify the
2765 -- retrieval of primitive operations of the type.
2766
2767 -- Ada 2005 (AI-50217): If the type was previously decorated when
2768 -- imported through a LIMITED WITH clause, it appears as incomplete
2769 -- but has no full view.
2770
2771 if Ekind (Prev) = E_Incomplete_Type
2772 and then Present (Full_View (Prev))
2773 then
2774 T := Full_View (Prev);
2775 Set_Incomplete_View (N, Parent (Prev));
2776 else
2777 T := Prev;
2778 end if;
2779
2780 Set_Is_Pure (T, Is_Pure (Current_Scope));
2781
2782 -- We set the flag Is_First_Subtype here. It is needed to set the
2783 -- corresponding flag for the Implicit class-wide-type created
2784 -- during tagged types processing.
2785
2786 Set_Is_First_Subtype (T, True);
2787
2788 -- Only composite types other than array types are allowed to have
2789 -- discriminants.
2790
2791 case Nkind (Def) is
2792
2793 -- For derived types, the rule will be checked once we've figured
2794 -- out the parent type.
2795
2796 when N_Derived_Type_Definition =>
2797 null;
2798
2799 -- For record types, discriminants are allowed, unless we are in
2800 -- SPARK.
2801
2802 when N_Record_Definition =>
2803 if Present (Discriminant_Specifications (N)) then
2804 Check_SPARK_05_Restriction
2805 ("discriminant type is not allowed",
2806 Defining_Identifier
2807 (First (Discriminant_Specifications (N))));
2808 end if;
2809
2810 when others =>
2811 if Present (Discriminant_Specifications (N)) then
2812 Error_Msg_N
2813 ("elementary or array type cannot have discriminants",
2814 Defining_Identifier
2815 (First (Discriminant_Specifications (N))));
2816 end if;
2817 end case;
2818
2819 -- Elaborate the type definition according to kind, and generate
2820 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2821 -- already done (this happens during the reanalysis that follows a call
2822 -- to the high level optimizer).
2823
2824 if not Analyzed (T) then
2825 Set_Analyzed (T);
2826
2827 -- A type declared within a Ghost region is automatically Ghost
2828 -- (SPARK RM 6.9(2)).
2829
2830 if Ghost_Mode > None then
2831 Set_Is_Ghost_Entity (T);
2832 end if;
2833
2834 case Nkind (Def) is
2835 when N_Access_To_Subprogram_Definition =>
2836 Access_Subprogram_Declaration (T, Def);
2837
2838 -- If this is a remote access to subprogram, we must create the
2839 -- equivalent fat pointer type, and related subprograms.
2840
2841 if Is_Remote then
2842 Process_Remote_AST_Declaration (N);
2843 end if;
2844
2845 -- Validate categorization rule against access type declaration
2846 -- usually a violation in Pure unit, Shared_Passive unit.
2847
2848 Validate_Access_Type_Declaration (T, N);
2849
2850 when N_Access_To_Object_Definition =>
2851 Access_Type_Declaration (T, Def);
2852
2853 -- Validate categorization rule against access type declaration
2854 -- usually a violation in Pure unit, Shared_Passive unit.
2855
2856 Validate_Access_Type_Declaration (T, N);
2857
2858 -- If we are in a Remote_Call_Interface package and define a
2859 -- RACW, then calling stubs and specific stream attributes
2860 -- must be added.
2861
2862 if Is_Remote
2863 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2864 then
2865 Add_RACW_Features (Def_Id);
2866 end if;
2867
2868 when N_Array_Type_Definition =>
2869 Array_Type_Declaration (T, Def);
2870
2871 when N_Derived_Type_Definition =>
2872 Derived_Type_Declaration (T, N, T /= Def_Id);
2873
2874 when N_Enumeration_Type_Definition =>
2875 Enumeration_Type_Declaration (T, Def);
2876
2877 when N_Floating_Point_Definition =>
2878 Floating_Point_Type_Declaration (T, Def);
2879
2880 when N_Decimal_Fixed_Point_Definition =>
2881 Decimal_Fixed_Point_Type_Declaration (T, Def);
2882
2883 when N_Ordinary_Fixed_Point_Definition =>
2884 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2885
2886 when N_Signed_Integer_Type_Definition =>
2887 Signed_Integer_Type_Declaration (T, Def);
2888
2889 when N_Modular_Type_Definition =>
2890 Modular_Type_Declaration (T, Def);
2891
2892 when N_Record_Definition =>
2893 Record_Type_Declaration (T, N, Prev);
2894
2895 -- If declaration has a parse error, nothing to elaborate.
2896
2897 when N_Error =>
2898 null;
2899
2900 when others =>
2901 raise Program_Error;
2902
2903 end case;
2904 end if;
2905
2906 if Etype (T) = Any_Type then
2907 return;
2908 end if;
2909
2910 -- Controlled type is not allowed in SPARK
2911
2912 if Is_Visibly_Controlled (T) then
2913 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2914 end if;
2915
2916 -- Some common processing for all types
2917
2918 Set_Depends_On_Private (T, Has_Private_Component (T));
2919 Check_Ops_From_Incomplete_Type;
2920
2921 -- Both the declared entity, and its anonymous base type if one was
2922 -- created, need freeze nodes allocated.
2923
2924 declare
2925 B : constant Entity_Id := Base_Type (T);
2926
2927 begin
2928 -- In the case where the base type differs from the first subtype, we
2929 -- pre-allocate a freeze node, and set the proper link to the first
2930 -- subtype. Freeze_Entity will use this preallocated freeze node when
2931 -- it freezes the entity.
2932
2933 -- This does not apply if the base type is a generic type, whose
2934 -- declaration is independent of the current derived definition.
2935
2936 if B /= T and then not Is_Generic_Type (B) then
2937 Ensure_Freeze_Node (B);
2938 Set_First_Subtype_Link (Freeze_Node (B), T);
2939 end if;
2940
2941 -- A type that is imported through a limited_with clause cannot
2942 -- generate any code, and thus need not be frozen. However, an access
2943 -- type with an imported designated type needs a finalization list,
2944 -- which may be referenced in some other package that has non-limited
2945 -- visibility on the designated type. Thus we must create the
2946 -- finalization list at the point the access type is frozen, to
2947 -- prevent unsatisfied references at link time.
2948
2949 if not From_Limited_With (T) or else Is_Access_Type (T) then
2950 Set_Has_Delayed_Freeze (T);
2951 end if;
2952 end;
2953
2954 -- Case where T is the full declaration of some private type which has
2955 -- been swapped in Defining_Identifier (N).
2956
2957 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2958 Process_Full_View (N, T, Def_Id);
2959
2960 -- Record the reference. The form of this is a little strange, since
2961 -- the full declaration has been swapped in. So the first parameter
2962 -- here represents the entity to which a reference is made which is
2963 -- the "real" entity, i.e. the one swapped in, and the second
2964 -- parameter provides the reference location.
2965
2966 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2967 -- since we don't want a complaint about the full type being an
2968 -- unwanted reference to the private type
2969
2970 declare
2971 B : constant Boolean := Has_Pragma_Unreferenced (T);
2972 begin
2973 Set_Has_Pragma_Unreferenced (T, False);
2974 Generate_Reference (T, T, 'c');
2975 Set_Has_Pragma_Unreferenced (T, B);
2976 end;
2977
2978 Set_Completion_Referenced (Def_Id);
2979
2980 -- For completion of incomplete type, process incomplete dependents
2981 -- and always mark the full type as referenced (it is the incomplete
2982 -- type that we get for any real reference).
2983
2984 elsif Ekind (Prev) = E_Incomplete_Type then
2985 Process_Incomplete_Dependents (N, T, Prev);
2986 Generate_Reference (Prev, Def_Id, 'c');
2987 Set_Completion_Referenced (Def_Id);
2988
2989 -- If not private type or incomplete type completion, this is a real
2990 -- definition of a new entity, so record it.
2991
2992 else
2993 Generate_Definition (Def_Id);
2994 end if;
2995
2996 -- Propagate any pending access types whose finalization masters need to
2997 -- be fully initialized from the partial to the full view. Guard against
2998 -- an illegal full view that remains unanalyzed.
2999
3000 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3001 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3002 end if;
3003
3004 if Chars (Scope (Def_Id)) = Name_System
3005 and then Chars (Def_Id) = Name_Address
3006 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3007 then
3008 Set_Is_Descendant_Of_Address (Def_Id);
3009 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3010 Set_Is_Descendant_Of_Address (Prev);
3011 end if;
3012
3013 Set_Optimize_Alignment_Flags (Def_Id);
3014 Check_Eliminated (Def_Id);
3015
3016 -- If the declaration is a completion and aspects are present, apply
3017 -- them to the entity for the type which is currently the partial
3018 -- view, but which is the one that will be frozen.
3019
3020 if Has_Aspects (N) then
3021
3022 -- In most cases the partial view is a private type, and both views
3023 -- appear in different declarative parts. In the unusual case where
3024 -- the partial view is incomplete, perform the analysis on the
3025 -- full view, to prevent freezing anomalies with the corresponding
3026 -- class-wide type, which otherwise might be frozen before the
3027 -- dispatch table is built.
3028
3029 if Prev /= Def_Id
3030 and then Ekind (Prev) /= E_Incomplete_Type
3031 then
3032 Analyze_Aspect_Specifications (N, Prev);
3033
3034 -- Normal case
3035
3036 else
3037 Analyze_Aspect_Specifications (N, Def_Id);
3038 end if;
3039 end if;
3040
3041 if Is_Derived_Type (Prev)
3042 and then Def_Id /= Prev
3043 then
3044 Check_Nonoverridable_Aspects;
3045 end if;
3046 end Analyze_Full_Type_Declaration;
3047
3048 ----------------------------------
3049 -- Analyze_Incomplete_Type_Decl --
3050 ----------------------------------
3051
3052 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3053 F : constant Boolean := Is_Pure (Current_Scope);
3054 T : Entity_Id;
3055
3056 begin
3057 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3058
3059 Generate_Definition (Defining_Identifier (N));
3060
3061 -- Process an incomplete declaration. The identifier must not have been
3062 -- declared already in the scope. However, an incomplete declaration may
3063 -- appear in the private part of a package, for a private type that has
3064 -- already been declared.
3065
3066 -- In this case, the discriminants (if any) must match
3067
3068 T := Find_Type_Name (N);
3069
3070 Set_Ekind (T, E_Incomplete_Type);
3071 Init_Size_Align (T);
3072 Set_Is_First_Subtype (T, True);
3073 Set_Etype (T, T);
3074
3075 -- An incomplete type declared within a Ghost region is automatically
3076 -- Ghost (SPARK RM 6.9(2)).
3077
3078 if Ghost_Mode > None then
3079 Set_Is_Ghost_Entity (T);
3080 end if;
3081
3082 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3083 -- incomplete types.
3084
3085 if Tagged_Present (N) then
3086 Set_Is_Tagged_Type (T, True);
3087 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3088 Make_Class_Wide_Type (T);
3089 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3090 end if;
3091
3092 Set_Stored_Constraint (T, No_Elist);
3093
3094 if Present (Discriminant_Specifications (N)) then
3095 Push_Scope (T);
3096 Process_Discriminants (N);
3097 End_Scope;
3098 end if;
3099
3100 -- If the type has discriminants, nontrivial subtypes may be declared
3101 -- before the full view of the type. The full views of those subtypes
3102 -- will be built after the full view of the type.
3103
3104 Set_Private_Dependents (T, New_Elmt_List);
3105 Set_Is_Pure (T, F);
3106 end Analyze_Incomplete_Type_Decl;
3107
3108 -----------------------------------
3109 -- Analyze_Interface_Declaration --
3110 -----------------------------------
3111
3112 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3113 CW : constant Entity_Id := Class_Wide_Type (T);
3114
3115 begin
3116 Set_Is_Tagged_Type (T);
3117 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3118
3119 Set_Is_Limited_Record (T, Limited_Present (Def)
3120 or else Task_Present (Def)
3121 or else Protected_Present (Def)
3122 or else Synchronized_Present (Def));
3123
3124 -- Type is abstract if full declaration carries keyword, or if previous
3125 -- partial view did.
3126
3127 Set_Is_Abstract_Type (T);
3128 Set_Is_Interface (T);
3129
3130 -- Type is a limited interface if it includes the keyword limited, task,
3131 -- protected, or synchronized.
3132
3133 Set_Is_Limited_Interface
3134 (T, Limited_Present (Def)
3135 or else Protected_Present (Def)
3136 or else Synchronized_Present (Def)
3137 or else Task_Present (Def));
3138
3139 Set_Interfaces (T, New_Elmt_List);
3140 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3141
3142 -- Complete the decoration of the class-wide entity if it was already
3143 -- built (i.e. during the creation of the limited view)
3144
3145 if Present (CW) then
3146 Set_Is_Interface (CW);
3147 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3148 end if;
3149
3150 -- Check runtime support for synchronized interfaces
3151
3152 if (Is_Task_Interface (T)
3153 or else Is_Protected_Interface (T)
3154 or else Is_Synchronized_Interface (T))
3155 and then not RTE_Available (RE_Select_Specific_Data)
3156 then
3157 Error_Msg_CRT ("synchronized interfaces", T);
3158 end if;
3159 end Analyze_Interface_Declaration;
3160
3161 -----------------------------
3162 -- Analyze_Itype_Reference --
3163 -----------------------------
3164
3165 -- Nothing to do. This node is placed in the tree only for the benefit of
3166 -- back end processing, and has no effect on the semantic processing.
3167
3168 procedure Analyze_Itype_Reference (N : Node_Id) is
3169 begin
3170 pragma Assert (Is_Itype (Itype (N)));
3171 null;
3172 end Analyze_Itype_Reference;
3173
3174 --------------------------------
3175 -- Analyze_Number_Declaration --
3176 --------------------------------
3177
3178 procedure Analyze_Number_Declaration (N : Node_Id) is
3179 E : constant Node_Id := Expression (N);
3180 Id : constant Entity_Id := Defining_Identifier (N);
3181 Index : Interp_Index;
3182 It : Interp;
3183 T : Entity_Id;
3184
3185 begin
3186 Generate_Definition (Id);
3187 Enter_Name (Id);
3188
3189 -- A number declared within a Ghost region is automatically Ghost
3190 -- (SPARK RM 6.9(2)).
3191
3192 if Ghost_Mode > None then
3193 Set_Is_Ghost_Entity (Id);
3194 end if;
3195
3196 -- This is an optimization of a common case of an integer literal
3197
3198 if Nkind (E) = N_Integer_Literal then
3199 Set_Is_Static_Expression (E, True);
3200 Set_Etype (E, Universal_Integer);
3201
3202 Set_Etype (Id, Universal_Integer);
3203 Set_Ekind (Id, E_Named_Integer);
3204 Set_Is_Frozen (Id, True);
3205 return;
3206 end if;
3207
3208 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3209
3210 -- Process expression, replacing error by integer zero, to avoid
3211 -- cascaded errors or aborts further along in the processing
3212
3213 -- Replace Error by integer zero, which seems least likely to cause
3214 -- cascaded errors.
3215
3216 if E = Error then
3217 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3218 Set_Error_Posted (E);
3219 end if;
3220
3221 Analyze (E);
3222
3223 -- Verify that the expression is static and numeric. If
3224 -- the expression is overloaded, we apply the preference
3225 -- rule that favors root numeric types.
3226
3227 if not Is_Overloaded (E) then
3228 T := Etype (E);
3229 if Has_Dynamic_Predicate_Aspect (T) then
3230 Error_Msg_N
3231 ("subtype has dynamic predicate, "
3232 & "not allowed in number declaration", N);
3233 end if;
3234
3235 else
3236 T := Any_Type;
3237
3238 Get_First_Interp (E, Index, It);
3239 while Present (It.Typ) loop
3240 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3241 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3242 then
3243 if T = Any_Type then
3244 T := It.Typ;
3245
3246 elsif It.Typ = Universal_Real
3247 or else
3248 It.Typ = Universal_Integer
3249 then
3250 -- Choose universal interpretation over any other
3251
3252 T := It.Typ;
3253 exit;
3254 end if;
3255 end if;
3256
3257 Get_Next_Interp (Index, It);
3258 end loop;
3259 end if;
3260
3261 if Is_Integer_Type (T) then
3262 Resolve (E, T);
3263 Set_Etype (Id, Universal_Integer);
3264 Set_Ekind (Id, E_Named_Integer);
3265
3266 elsif Is_Real_Type (T) then
3267
3268 -- Because the real value is converted to universal_real, this is a
3269 -- legal context for a universal fixed expression.
3270
3271 if T = Universal_Fixed then
3272 declare
3273 Loc : constant Source_Ptr := Sloc (N);
3274 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3275 Subtype_Mark =>
3276 New_Occurrence_Of (Universal_Real, Loc),
3277 Expression => Relocate_Node (E));
3278
3279 begin
3280 Rewrite (E, Conv);
3281 Analyze (E);
3282 end;
3283
3284 elsif T = Any_Fixed then
3285 Error_Msg_N ("illegal context for mixed mode operation", E);
3286
3287 -- Expression is of the form : universal_fixed * integer. Try to
3288 -- resolve as universal_real.
3289
3290 T := Universal_Real;
3291 Set_Etype (E, T);
3292 end if;
3293
3294 Resolve (E, T);
3295 Set_Etype (Id, Universal_Real);
3296 Set_Ekind (Id, E_Named_Real);
3297
3298 else
3299 Wrong_Type (E, Any_Numeric);
3300 Resolve (E, T);
3301
3302 Set_Etype (Id, T);
3303 Set_Ekind (Id, E_Constant);
3304 Set_Never_Set_In_Source (Id, True);
3305 Set_Is_True_Constant (Id, True);
3306 return;
3307 end if;
3308
3309 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3310 Set_Etype (E, Etype (Id));
3311 end if;
3312
3313 if not Is_OK_Static_Expression (E) then
3314 Flag_Non_Static_Expr
3315 ("non-static expression used in number declaration!", E);
3316 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3317 Set_Etype (E, Any_Type);
3318 end if;
3319
3320 Analyze_Dimension (N);
3321 end Analyze_Number_Declaration;
3322
3323 --------------------------------
3324 -- Analyze_Object_Declaration --
3325 --------------------------------
3326
3327 procedure Analyze_Object_Declaration (N : Node_Id) is
3328 Loc : constant Source_Ptr := Sloc (N);
3329 Id : constant Entity_Id := Defining_Identifier (N);
3330 Act_T : Entity_Id;
3331 T : Entity_Id;
3332
3333 E : Node_Id := Expression (N);
3334 -- E is set to Expression (N) throughout this routine. When
3335 -- Expression (N) is modified, E is changed accordingly.
3336
3337 Prev_Entity : Entity_Id := Empty;
3338
3339 function Count_Tasks (T : Entity_Id) return Uint;
3340 -- This function is called when a non-generic library level object of a
3341 -- task type is declared. Its function is to count the static number of
3342 -- tasks declared within the type (it is only called if Has_Task is set
3343 -- for T). As a side effect, if an array of tasks with non-static bounds
3344 -- or a variant record type is encountered, Check_Restriction is called
3345 -- indicating the count is unknown.
3346
3347 function Delayed_Aspect_Present return Boolean;
3348 -- If the declaration has an expression that is an aggregate, and it
3349 -- has aspects that require delayed analysis, the resolution of the
3350 -- aggregate must be deferred to the freeze point of the objet. This
3351 -- special processing was created for address clauses, but it must
3352 -- also apply to Alignment. This must be done before the aspect
3353 -- specifications are analyzed because we must handle the aggregate
3354 -- before the analysis of the object declaration is complete.
3355
3356 -- Any other relevant delayed aspects on object declarations ???
3357
3358 -----------------
3359 -- Count_Tasks --
3360 -----------------
3361
3362 function Count_Tasks (T : Entity_Id) return Uint is
3363 C : Entity_Id;
3364 X : Node_Id;
3365 V : Uint;
3366
3367 begin
3368 if Is_Task_Type (T) then
3369 return Uint_1;
3370
3371 elsif Is_Record_Type (T) then
3372 if Has_Discriminants (T) then
3373 Check_Restriction (Max_Tasks, N);
3374 return Uint_0;
3375
3376 else
3377 V := Uint_0;
3378 C := First_Component (T);
3379 while Present (C) loop
3380 V := V + Count_Tasks (Etype (C));
3381 Next_Component (C);
3382 end loop;
3383
3384 return V;
3385 end if;
3386
3387 elsif Is_Array_Type (T) then
3388 X := First_Index (T);
3389 V := Count_Tasks (Component_Type (T));
3390 while Present (X) loop
3391 C := Etype (X);
3392
3393 if not Is_OK_Static_Subtype (C) then
3394 Check_Restriction (Max_Tasks, N);
3395 return Uint_0;
3396 else
3397 V := V * (UI_Max (Uint_0,
3398 Expr_Value (Type_High_Bound (C)) -
3399 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3400 end if;
3401
3402 Next_Index (X);
3403 end loop;
3404
3405 return V;
3406
3407 else
3408 return Uint_0;
3409 end if;
3410 end Count_Tasks;
3411
3412 ----------------------------
3413 -- Delayed_Aspect_Present --
3414 ----------------------------
3415
3416 function Delayed_Aspect_Present return Boolean is
3417 A : Node_Id;
3418 A_Id : Aspect_Id;
3419
3420 begin
3421 if Present (Aspect_Specifications (N)) then
3422 A := First (Aspect_Specifications (N));
3423 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3424 while Present (A) loop
3425 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3426 return True;
3427 end if;
3428
3429 Next (A);
3430 end loop;
3431 end if;
3432
3433 return False;
3434 end Delayed_Aspect_Present;
3435
3436 -- Local variables
3437
3438 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
3439 Related_Id : Entity_Id;
3440
3441 -- Start of processing for Analyze_Object_Declaration
3442
3443 begin
3444 -- There are three kinds of implicit types generated by an
3445 -- object declaration:
3446
3447 -- 1. Those generated by the original Object Definition
3448
3449 -- 2. Those generated by the Expression
3450
3451 -- 3. Those used to constrain the Object Definition with the
3452 -- expression constraints when the definition is unconstrained.
3453
3454 -- They must be generated in this order to avoid order of elaboration
3455 -- issues. Thus the first step (after entering the name) is to analyze
3456 -- the object definition.
3457
3458 if Constant_Present (N) then
3459 Prev_Entity := Current_Entity_In_Scope (Id);
3460
3461 if Present (Prev_Entity)
3462 and then
3463 -- If the homograph is an implicit subprogram, it is overridden
3464 -- by the current declaration.
3465
3466 ((Is_Overloadable (Prev_Entity)
3467 and then Is_Inherited_Operation (Prev_Entity))
3468
3469 -- The current object is a discriminal generated for an entry
3470 -- family index. Even though the index is a constant, in this
3471 -- particular context there is no true constant redeclaration.
3472 -- Enter_Name will handle the visibility.
3473
3474 or else
3475 (Is_Discriminal (Id)
3476 and then Ekind (Discriminal_Link (Id)) =
3477 E_Entry_Index_Parameter)
3478
3479 -- The current object is the renaming for a generic declared
3480 -- within the instance.
3481
3482 or else
3483 (Ekind (Prev_Entity) = E_Package
3484 and then Nkind (Parent (Prev_Entity)) =
3485 N_Package_Renaming_Declaration
3486 and then not Comes_From_Source (Prev_Entity)
3487 and then
3488 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3489
3490 -- The entity may be a homonym of a private component of the
3491 -- enclosing protected object, for which we create a local
3492 -- renaming declaration. The declaration is legal, even if
3493 -- useless when it just captures that component.
3494
3495 or else
3496 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3497 and then Nkind (Parent (Prev_Entity)) =
3498 N_Object_Renaming_Declaration))
3499 then
3500 Prev_Entity := Empty;
3501 end if;
3502 end if;
3503
3504 -- The object declaration is Ghost when it is subject to pragma Ghost or
3505 -- completes a deferred Ghost constant. Set the mode now to ensure that
3506 -- any nodes generated during analysis and expansion are properly marked
3507 -- as Ghost.
3508
3509 Set_Ghost_Mode (N, Prev_Entity);
3510
3511 if Present (Prev_Entity) then
3512 Constant_Redeclaration (Id, N, T);
3513
3514 Generate_Reference (Prev_Entity, Id, 'c');
3515 Set_Completion_Referenced (Id);
3516
3517 if Error_Posted (N) then
3518
3519 -- Type mismatch or illegal redeclaration; do not analyze
3520 -- expression to avoid cascaded errors.
3521
3522 T := Find_Type_Of_Object (Object_Definition (N), N);
3523 Set_Etype (Id, T);
3524 Set_Ekind (Id, E_Variable);
3525 goto Leave;
3526 end if;
3527
3528 -- In the normal case, enter identifier at the start to catch premature
3529 -- usage in the initialization expression.
3530
3531 else
3532 Generate_Definition (Id);
3533 Enter_Name (Id);
3534
3535 Mark_Coextensions (N, Object_Definition (N));
3536
3537 T := Find_Type_Of_Object (Object_Definition (N), N);
3538
3539 if Nkind (Object_Definition (N)) = N_Access_Definition
3540 and then Present
3541 (Access_To_Subprogram_Definition (Object_Definition (N)))
3542 and then Protected_Present
3543 (Access_To_Subprogram_Definition (Object_Definition (N)))
3544 then
3545 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3546 end if;
3547
3548 if Error_Posted (Id) then
3549 Set_Etype (Id, T);
3550 Set_Ekind (Id, E_Variable);
3551 goto Leave;
3552 end if;
3553 end if;
3554
3555 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3556 -- out some static checks.
3557
3558 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3559
3560 -- In case of aggregates we must also take care of the correct
3561 -- initialization of nested aggregates bug this is done at the
3562 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3563
3564 if Present (Expression (N))
3565 and then Nkind (Expression (N)) = N_Aggregate
3566 then
3567 null;
3568
3569 else
3570 declare
3571 Save_Typ : constant Entity_Id := Etype (Id);
3572 begin
3573 Set_Etype (Id, T); -- Temp. decoration for static checks
3574 Null_Exclusion_Static_Checks (N);
3575 Set_Etype (Id, Save_Typ);
3576 end;
3577 end if;
3578 end if;
3579
3580 -- Object is marked pure if it is in a pure scope
3581
3582 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3583
3584 -- If deferred constant, make sure context is appropriate. We detect
3585 -- a deferred constant as a constant declaration with no expression.
3586 -- A deferred constant can appear in a package body if its completion
3587 -- is by means of an interface pragma.
3588
3589 if Constant_Present (N) and then No (E) then
3590
3591 -- A deferred constant may appear in the declarative part of the
3592 -- following constructs:
3593
3594 -- blocks
3595 -- entry bodies
3596 -- extended return statements
3597 -- package specs
3598 -- package bodies
3599 -- subprogram bodies
3600 -- task bodies
3601
3602 -- When declared inside a package spec, a deferred constant must be
3603 -- completed by a full constant declaration or pragma Import. In all
3604 -- other cases, the only proper completion is pragma Import. Extended
3605 -- return statements are flagged as invalid contexts because they do
3606 -- not have a declarative part and so cannot accommodate the pragma.
3607
3608 if Ekind (Current_Scope) = E_Return_Statement then
3609 Error_Msg_N
3610 ("invalid context for deferred constant declaration (RM 7.4)",
3611 N);
3612 Error_Msg_N
3613 ("\declaration requires an initialization expression",
3614 N);
3615 Set_Constant_Present (N, False);
3616
3617 -- In Ada 83, deferred constant must be of private type
3618
3619 elsif not Is_Private_Type (T) then
3620 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3621 Error_Msg_N
3622 ("(Ada 83) deferred constant must be private type", N);
3623 end if;
3624 end if;
3625
3626 -- If not a deferred constant, then the object declaration freezes
3627 -- its type, unless the object is of an anonymous type and has delayed
3628 -- aspects. In that case the type is frozen when the object itself is.
3629
3630 else
3631 Check_Fully_Declared (T, N);
3632
3633 if Has_Delayed_Aspects (Id)
3634 and then Is_Array_Type (T)
3635 and then Is_Itype (T)
3636 then
3637 Set_Has_Delayed_Freeze (T);
3638 else
3639 Freeze_Before (N, T);
3640 end if;
3641 end if;
3642
3643 -- If the object was created by a constrained array definition, then
3644 -- set the link in both the anonymous base type and anonymous subtype
3645 -- that are built to represent the array type to point to the object.
3646
3647 if Nkind (Object_Definition (Declaration_Node (Id))) =
3648 N_Constrained_Array_Definition
3649 then
3650 Set_Related_Array_Object (T, Id);
3651 Set_Related_Array_Object (Base_Type (T), Id);
3652 end if;
3653
3654 -- Special checks for protected objects not at library level
3655
3656 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3657 Check_Restriction (No_Local_Protected_Objects, Id);
3658
3659 -- Protected objects with interrupt handlers must be at library level
3660
3661 -- Ada 2005: This test is not needed (and the corresponding clause
3662 -- in the RM is removed) because accessibility checks are sufficient
3663 -- to make handlers not at the library level illegal.
3664
3665 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3666 -- applies to the '95 version of the language as well.
3667
3668 if Is_Protected_Type (T)
3669 and then Has_Interrupt_Handler (T)
3670 and then Ada_Version < Ada_95
3671 then
3672 Error_Msg_N
3673 ("interrupt object can only be declared at library level", Id);
3674 end if;
3675 end if;
3676
3677 -- Check for violation of No_Local_Timing_Events
3678
3679 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3680 Check_Restriction (No_Local_Timing_Events, Id);
3681 end if;
3682
3683 -- The actual subtype of the object is the nominal subtype, unless
3684 -- the nominal one is unconstrained and obtained from the expression.
3685
3686 Act_T := T;
3687
3688 -- These checks should be performed before the initialization expression
3689 -- is considered, so that the Object_Definition node is still the same
3690 -- as in source code.
3691
3692 -- In SPARK, the nominal subtype is always given by a subtype mark
3693 -- and must not be unconstrained. (The only exception to this is the
3694 -- acceptance of declarations of constants of type String.)
3695
3696 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3697 then
3698 Check_SPARK_05_Restriction
3699 ("subtype mark required", Object_Definition (N));
3700
3701 elsif Is_Array_Type (T)
3702 and then not Is_Constrained (T)
3703 and then T /= Standard_String
3704 then
3705 Check_SPARK_05_Restriction
3706 ("subtype mark of constrained type expected",
3707 Object_Definition (N));
3708 end if;
3709
3710 -- There are no aliased objects in SPARK
3711
3712 if Aliased_Present (N) then
3713 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3714 end if;
3715
3716 -- Process initialization expression if present and not in error
3717
3718 if Present (E) and then E /= Error then
3719
3720 -- Generate an error in case of CPP class-wide object initialization.
3721 -- Required because otherwise the expansion of the class-wide
3722 -- assignment would try to use 'size to initialize the object
3723 -- (primitive that is not available in CPP tagged types).
3724
3725 if Is_Class_Wide_Type (Act_T)
3726 and then
3727 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3728 or else
3729 (Present (Full_View (Root_Type (Etype (Act_T))))
3730 and then
3731 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3732 then
3733 Error_Msg_N
3734 ("predefined assignment not available for 'C'P'P tagged types",
3735 E);
3736 end if;
3737
3738 Mark_Coextensions (N, E);
3739 Analyze (E);
3740
3741 -- In case of errors detected in the analysis of the expression,
3742 -- decorate it with the expected type to avoid cascaded errors
3743
3744 if No (Etype (E)) then
3745 Set_Etype (E, T);
3746 end if;
3747
3748 -- If an initialization expression is present, then we set the
3749 -- Is_True_Constant flag. It will be reset if this is a variable
3750 -- and it is indeed modified.
3751
3752 Set_Is_True_Constant (Id, True);
3753
3754 -- If we are analyzing a constant declaration, set its completion
3755 -- flag after analyzing and resolving the expression.
3756
3757 if Constant_Present (N) then
3758 Set_Has_Completion (Id);
3759 end if;
3760
3761 -- Set type and resolve (type may be overridden later on). Note:
3762 -- Ekind (Id) must still be E_Void at this point so that incorrect
3763 -- early usage within E is properly diagnosed.
3764
3765 Set_Etype (Id, T);
3766
3767 -- If the expression is an aggregate we must look ahead to detect
3768 -- the possible presence of an address clause, and defer resolution
3769 -- and expansion of the aggregate to the freeze point of the entity.
3770
3771 -- This is not always legal because the aggregate may contain other
3772 -- references that need freezing, e.g. references to other entities
3773 -- with address clauses. In any case, when compiling with -gnatI the
3774 -- presence of the address clause must be ignored.
3775
3776 if Comes_From_Source (N)
3777 and then Expander_Active
3778 and then Nkind (E) = N_Aggregate
3779 and then
3780 ((Present (Following_Address_Clause (N))
3781 and then not Ignore_Rep_Clauses)
3782 or else Delayed_Aspect_Present)
3783 then
3784 Set_Etype (E, T);
3785
3786 else
3787 Resolve (E, T);
3788 end if;
3789
3790 -- No further action needed if E is a call to an inlined function
3791 -- which returns an unconstrained type and it has been expanded into
3792 -- a procedure call. In that case N has been replaced by an object
3793 -- declaration without initializing expression and it has been
3794 -- analyzed (see Expand_Inlined_Call).
3795
3796 if Back_End_Inlining
3797 and then Expander_Active
3798 and then Nkind (E) = N_Function_Call
3799 and then Nkind (Name (E)) in N_Has_Entity
3800 and then Is_Inlined (Entity (Name (E)))
3801 and then not Is_Constrained (Etype (E))
3802 and then Analyzed (N)
3803 and then No (Expression (N))
3804 then
3805 Ghost_Mode := Save_Ghost_Mode;
3806 return;
3807 end if;
3808
3809 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3810 -- node (which was marked already-analyzed), we need to set the type
3811 -- to something other than Any_Access in order to keep gigi happy.
3812
3813 if Etype (E) = Any_Access then
3814 Set_Etype (E, T);
3815 end if;
3816
3817 -- If the object is an access to variable, the initialization
3818 -- expression cannot be an access to constant.
3819
3820 if Is_Access_Type (T)
3821 and then not Is_Access_Constant (T)
3822 and then Is_Access_Type (Etype (E))
3823 and then Is_Access_Constant (Etype (E))
3824 then
3825 Error_Msg_N
3826 ("access to variable cannot be initialized with an "
3827 & "access-to-constant expression", E);
3828 end if;
3829
3830 if not Assignment_OK (N) then
3831 Check_Initialization (T, E);
3832 end if;
3833
3834 Check_Unset_Reference (E);
3835
3836 -- If this is a variable, then set current value. If this is a
3837 -- declared constant of a scalar type with a static expression,
3838 -- indicate that it is always valid.
3839
3840 if not Constant_Present (N) then
3841 if Compile_Time_Known_Value (E) then
3842 Set_Current_Value (Id, E);
3843 end if;
3844
3845 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3846 Set_Is_Known_Valid (Id);
3847 end if;
3848
3849 -- Deal with setting of null flags
3850
3851 if Is_Access_Type (T) then
3852 if Known_Non_Null (E) then
3853 Set_Is_Known_Non_Null (Id, True);
3854 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3855 Set_Is_Known_Null (Id, True);
3856 end if;
3857 end if;
3858
3859 -- Check incorrect use of dynamically tagged expressions
3860
3861 if Is_Tagged_Type (T) then
3862 Check_Dynamically_Tagged_Expression
3863 (Expr => E,
3864 Typ => T,
3865 Related_Nod => N);
3866 end if;
3867
3868 Apply_Scalar_Range_Check (E, T);
3869 Apply_Static_Length_Check (E, T);
3870
3871 if Nkind (Original_Node (N)) = N_Object_Declaration
3872 and then Comes_From_Source (Original_Node (N))
3873
3874 -- Only call test if needed
3875
3876 and then Restriction_Check_Required (SPARK_05)
3877 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3878 then
3879 Check_SPARK_05_Restriction
3880 ("initialization expression is not appropriate", E);
3881 end if;
3882
3883 -- A formal parameter of a specific tagged type whose related
3884 -- subprogram is subject to pragma Extensions_Visible with value
3885 -- "False" cannot be implicitly converted to a class-wide type by
3886 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
3887 -- not consider internally generated expressions.
3888
3889 if Is_Class_Wide_Type (T)
3890 and then Comes_From_Source (E)
3891 and then Is_EVF_Expression (E)
3892 then
3893 Error_Msg_N
3894 ("formal parameter cannot be implicitly converted to "
3895 & "class-wide type when Extensions_Visible is False", E);
3896 end if;
3897 end if;
3898
3899 -- If the No_Streams restriction is set, check that the type of the
3900 -- object is not, and does not contain, any subtype derived from
3901 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3902 -- Has_Stream just for efficiency reasons. There is no point in
3903 -- spending time on a Has_Stream check if the restriction is not set.
3904
3905 if Restriction_Check_Required (No_Streams) then
3906 if Has_Stream (T) then
3907 Check_Restriction (No_Streams, N);
3908 end if;
3909 end if;
3910
3911 -- Deal with predicate check before we start to do major rewriting. It
3912 -- is OK to initialize and then check the initialized value, since the
3913 -- object goes out of scope if we get a predicate failure. Note that we
3914 -- do this in the analyzer and not the expander because the analyzer
3915 -- does some substantial rewriting in some cases.
3916
3917 -- We need a predicate check if the type has predicates that are not
3918 -- ignored, and if either there is an initializing expression, or for
3919 -- default initialization when we have at least one case of an explicit
3920 -- default initial value and then this is not an internal declaration
3921 -- whose initialization comes later (as for an aggregate expansion).
3922
3923 if not Suppress_Assignment_Checks (N)
3924 and then Present (Predicate_Function (T))
3925 and then not Predicates_Ignored (T)
3926 and then not No_Initialization (N)
3927 and then
3928 (Present (E)
3929 or else
3930 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3931 then
3932 -- If the type has a static predicate and the expression is known at
3933 -- compile time, see if the expression satisfies the predicate.
3934
3935 if Present (E) then
3936 Check_Expression_Against_Static_Predicate (E, T);
3937 end if;
3938
3939 -- If the type is a null record and there is no explicit initial
3940 -- expression, no predicate check applies.
3941
3942 if No (E) and then Is_Null_Record_Type (T) then
3943 null;
3944
3945 else
3946 Insert_After (N,
3947 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3948 end if;
3949 end if;
3950
3951 -- Case of unconstrained type
3952
3953 if not Is_Definite_Subtype (T) then
3954
3955 -- In SPARK, a declaration of unconstrained type is allowed
3956 -- only for constants of type string.
3957
3958 if Is_String_Type (T) and then not Constant_Present (N) then
3959 Check_SPARK_05_Restriction
3960 ("declaration of object of unconstrained type not allowed", N);
3961 end if;
3962
3963 -- Nothing to do in deferred constant case
3964
3965 if Constant_Present (N) and then No (E) then
3966 null;
3967
3968 -- Case of no initialization present
3969
3970 elsif No (E) then
3971 if No_Initialization (N) then
3972 null;
3973
3974 elsif Is_Class_Wide_Type (T) then
3975 Error_Msg_N
3976 ("initialization required in class-wide declaration ", N);
3977
3978 else
3979 Error_Msg_N
3980 ("unconstrained subtype not allowed (need initialization)",
3981 Object_Definition (N));
3982
3983 if Is_Record_Type (T) and then Has_Discriminants (T) then
3984 Error_Msg_N
3985 ("\provide initial value or explicit discriminant values",
3986 Object_Definition (N));
3987
3988 Error_Msg_NE
3989 ("\or give default discriminant values for type&",
3990 Object_Definition (N), T);
3991
3992 elsif Is_Array_Type (T) then
3993 Error_Msg_N
3994 ("\provide initial value or explicit array bounds",
3995 Object_Definition (N));
3996 end if;
3997 end if;
3998
3999 -- Case of initialization present but in error. Set initial
4000 -- expression as absent (but do not make above complaints)
4001
4002 elsif E = Error then
4003 Set_Expression (N, Empty);
4004 E := Empty;
4005
4006 -- Case of initialization present
4007
4008 else
4009 -- Check restrictions in Ada 83
4010
4011 if not Constant_Present (N) then
4012
4013 -- Unconstrained variables not allowed in Ada 83 mode
4014
4015 if Ada_Version = Ada_83
4016 and then Comes_From_Source (Object_Definition (N))
4017 then
4018 Error_Msg_N
4019 ("(Ada 83) unconstrained variable not allowed",
4020 Object_Definition (N));
4021 end if;
4022 end if;
4023
4024 -- Now we constrain the variable from the initializing expression
4025
4026 -- If the expression is an aggregate, it has been expanded into
4027 -- individual assignments. Retrieve the actual type from the
4028 -- expanded construct.
4029
4030 if Is_Array_Type (T)
4031 and then No_Initialization (N)
4032 and then Nkind (Original_Node (E)) = N_Aggregate
4033 then
4034 Act_T := Etype (E);
4035
4036 -- In case of class-wide interface object declarations we delay
4037 -- the generation of the equivalent record type declarations until
4038 -- its expansion because there are cases in they are not required.
4039
4040 elsif Is_Interface (T) then
4041 null;
4042
4043 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4044 -- we should prevent the generation of another Itype with the
4045 -- same name as the one already generated, or we end up with
4046 -- two identical types in GNATprove.
4047
4048 elsif GNATprove_Mode then
4049 null;
4050
4051 -- If the type is an unchecked union, no subtype can be built from
4052 -- the expression. Rewrite declaration as a renaming, which the
4053 -- back-end can handle properly. This is a rather unusual case,
4054 -- because most unchecked_union declarations have default values
4055 -- for discriminants and are thus not indefinite.
4056
4057 elsif Is_Unchecked_Union (T) then
4058 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4059 Set_Ekind (Id, E_Constant);
4060 else
4061 Set_Ekind (Id, E_Variable);
4062 end if;
4063
4064 -- An object declared within a Ghost region is automatically
4065 -- Ghost (SPARK RM 6.9(2)).
4066
4067 if Ghost_Mode > None then
4068 Set_Is_Ghost_Entity (Id);
4069
4070 -- The Ghost policy in effect at the point of declaration
4071 -- and at the point of completion must match
4072 -- (SPARK RM 6.9(14)).
4073
4074 if Present (Prev_Entity)
4075 and then Is_Ghost_Entity (Prev_Entity)
4076 then
4077 Check_Ghost_Completion (Prev_Entity, Id);
4078 end if;
4079 end if;
4080
4081 Rewrite (N,
4082 Make_Object_Renaming_Declaration (Loc,
4083 Defining_Identifier => Id,
4084 Subtype_Mark => New_Occurrence_Of (T, Loc),
4085 Name => E));
4086
4087 Set_Renamed_Object (Id, E);
4088 Freeze_Before (N, T);
4089 Set_Is_Frozen (Id);
4090
4091 Ghost_Mode := Save_Ghost_Mode;
4092 return;
4093
4094 else
4095 -- Ensure that the generated subtype has a unique external name
4096 -- when the related object is public. This guarantees that the
4097 -- subtype and its bounds will not be affected by switches or
4098 -- pragmas that may offset the internal counter due to extra
4099 -- generated code.
4100
4101 if Is_Public (Id) then
4102 Related_Id := Id;
4103 else
4104 Related_Id := Empty;
4105 end if;
4106
4107 Expand_Subtype_From_Expr
4108 (N => N,
4109 Unc_Type => T,
4110 Subtype_Indic => Object_Definition (N),
4111 Exp => E,
4112 Related_Id => Related_Id);
4113
4114 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4115 end if;
4116
4117 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4118
4119 if Aliased_Present (N) then
4120 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4121 end if;
4122
4123 Freeze_Before (N, Act_T);
4124 Freeze_Before (N, T);
4125 end if;
4126
4127 elsif Is_Array_Type (T)
4128 and then No_Initialization (N)
4129 and then (Nkind (Original_Node (E)) = N_Aggregate
4130 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4131 and then Nkind (Original_Node (Expression
4132 (Original_Node (E)))) = N_Aggregate))
4133 then
4134 if not Is_Entity_Name (Object_Definition (N)) then
4135 Act_T := Etype (E);
4136 Check_Compile_Time_Size (Act_T);
4137
4138 if Aliased_Present (N) then
4139 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4140 end if;
4141 end if;
4142
4143 -- When the given object definition and the aggregate are specified
4144 -- independently, and their lengths might differ do a length check.
4145 -- This cannot happen if the aggregate is of the form (others =>...)
4146
4147 if not Is_Constrained (T) then
4148 null;
4149
4150 elsif Nkind (E) = N_Raise_Constraint_Error then
4151
4152 -- Aggregate is statically illegal. Place back in declaration
4153
4154 Set_Expression (N, E);
4155 Set_No_Initialization (N, False);
4156
4157 elsif T = Etype (E) then
4158 null;
4159
4160 elsif Nkind (E) = N_Aggregate
4161 and then Present (Component_Associations (E))
4162 and then Present (Choices (First (Component_Associations (E))))
4163 and then Nkind (First
4164 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4165 then
4166 null;
4167
4168 else
4169 Apply_Length_Check (E, T);
4170 end if;
4171
4172 -- If the type is limited unconstrained with defaulted discriminants and
4173 -- there is no expression, then the object is constrained by the
4174 -- defaults, so it is worthwhile building the corresponding subtype.
4175
4176 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4177 and then not Is_Constrained (T)
4178 and then Has_Discriminants (T)
4179 then
4180 if No (E) then
4181 Act_T := Build_Default_Subtype (T, N);
4182 else
4183 -- Ada 2005: A limited object may be initialized by means of an
4184 -- aggregate. If the type has default discriminants it has an
4185 -- unconstrained nominal type, Its actual subtype will be obtained
4186 -- from the aggregate, and not from the default discriminants.
4187
4188 Act_T := Etype (E);
4189 end if;
4190
4191 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4192
4193 elsif Nkind (E) = N_Function_Call
4194 and then Constant_Present (N)
4195 and then Has_Unconstrained_Elements (Etype (E))
4196 then
4197 -- The back-end has problems with constants of a discriminated type
4198 -- with defaults, if the initial value is a function call. We
4199 -- generate an intermediate temporary that will receive a reference
4200 -- to the result of the call. The initialization expression then
4201 -- becomes a dereference of that temporary.
4202
4203 Remove_Side_Effects (E);
4204
4205 -- If this is a constant declaration of an unconstrained type and
4206 -- the initialization is an aggregate, we can use the subtype of the
4207 -- aggregate for the declared entity because it is immutable.
4208
4209 elsif not Is_Constrained (T)
4210 and then Has_Discriminants (T)
4211 and then Constant_Present (N)
4212 and then not Has_Unchecked_Union (T)
4213 and then Nkind (E) = N_Aggregate
4214 then
4215 Act_T := Etype (E);
4216 end if;
4217
4218 -- Check No_Wide_Characters restriction
4219
4220 Check_Wide_Character_Restriction (T, Object_Definition (N));
4221
4222 -- Indicate this is not set in source. Certainly true for constants, and
4223 -- true for variables so far (will be reset for a variable if and when
4224 -- we encounter a modification in the source).
4225
4226 Set_Never_Set_In_Source (Id);
4227
4228 -- Now establish the proper kind and type of the object
4229
4230 if Constant_Present (N) then
4231 Set_Ekind (Id, E_Constant);
4232 Set_Is_True_Constant (Id);
4233
4234 else
4235 Set_Ekind (Id, E_Variable);
4236
4237 -- A variable is set as shared passive if it appears in a shared
4238 -- passive package, and is at the outer level. This is not done for
4239 -- entities generated during expansion, because those are always
4240 -- manipulated locally.
4241
4242 if Is_Shared_Passive (Current_Scope)
4243 and then Is_Library_Level_Entity (Id)
4244 and then Comes_From_Source (Id)
4245 then
4246 Set_Is_Shared_Passive (Id);
4247 Check_Shared_Var (Id, T, N);
4248 end if;
4249
4250 -- Set Has_Initial_Value if initializing expression present. Note
4251 -- that if there is no initializing expression, we leave the state
4252 -- of this flag unchanged (usually it will be False, but notably in
4253 -- the case of exception choice variables, it will already be true).
4254
4255 if Present (E) then
4256 Set_Has_Initial_Value (Id);
4257 end if;
4258 end if;
4259
4260 -- Initialize alignment and size and capture alignment setting
4261
4262 Init_Alignment (Id);
4263 Init_Esize (Id);
4264 Set_Optimize_Alignment_Flags (Id);
4265
4266 -- An object declared within a Ghost region is automatically Ghost
4267 -- (SPARK RM 6.9(2)).
4268
4269 if Ghost_Mode > None
4270 or else (Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity))
4271 then
4272 Set_Is_Ghost_Entity (Id);
4273
4274 -- The Ghost policy in effect at the point of declaration and at the
4275 -- point of completion must match (SPARK RM 6.9(14)).
4276
4277 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4278 Check_Ghost_Completion (Prev_Entity, Id);
4279 end if;
4280 end if;
4281
4282 -- Deal with aliased case
4283
4284 if Aliased_Present (N) then
4285 Set_Is_Aliased (Id);
4286
4287 -- If the object is aliased and the type is unconstrained with
4288 -- defaulted discriminants and there is no expression, then the
4289 -- object is constrained by the defaults, so it is worthwhile
4290 -- building the corresponding subtype.
4291
4292 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4293 -- unconstrained, then only establish an actual subtype if the
4294 -- nominal subtype is indefinite. In definite cases the object is
4295 -- unconstrained in Ada 2005.
4296
4297 if No (E)
4298 and then Is_Record_Type (T)
4299 and then not Is_Constrained (T)
4300 and then Has_Discriminants (T)
4301 and then (Ada_Version < Ada_2005
4302 or else not Is_Definite_Subtype (T))
4303 then
4304 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4305 end if;
4306 end if;
4307
4308 -- Now we can set the type of the object
4309
4310 Set_Etype (Id, Act_T);
4311
4312 -- Non-constant object is marked to be treated as volatile if type is
4313 -- volatile and we clear the Current_Value setting that may have been
4314 -- set above. Doing so for constants isn't required and might interfere
4315 -- with possible uses of the object as a static expression in contexts
4316 -- incompatible with volatility (e.g. as a case-statement alternative).
4317
4318 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4319 Set_Treat_As_Volatile (Id);
4320 Set_Current_Value (Id, Empty);
4321 end if;
4322
4323 -- Deal with controlled types
4324
4325 if Has_Controlled_Component (Etype (Id))
4326 or else Is_Controlled (Etype (Id))
4327 then
4328 if not Is_Library_Level_Entity (Id) then
4329 Check_Restriction (No_Nested_Finalization, N);
4330 else
4331 Validate_Controlled_Object (Id);
4332 end if;
4333 end if;
4334
4335 if Has_Task (Etype (Id)) then
4336 Check_Restriction (No_Tasking, N);
4337
4338 -- Deal with counting max tasks
4339
4340 -- Nothing to do if inside a generic
4341
4342 if Inside_A_Generic then
4343 null;
4344
4345 -- If library level entity, then count tasks
4346
4347 elsif Is_Library_Level_Entity (Id) then
4348 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4349
4350 -- If not library level entity, then indicate we don't know max
4351 -- tasks and also check task hierarchy restriction and blocking
4352 -- operation (since starting a task is definitely blocking).
4353
4354 else
4355 Check_Restriction (Max_Tasks, N);
4356 Check_Restriction (No_Task_Hierarchy, N);
4357 Check_Potentially_Blocking_Operation (N);
4358 end if;
4359
4360 -- A rather specialized test. If we see two tasks being declared
4361 -- of the same type in the same object declaration, and the task
4362 -- has an entry with an address clause, we know that program error
4363 -- will be raised at run time since we can't have two tasks with
4364 -- entries at the same address.
4365
4366 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4367 declare
4368 E : Entity_Id;
4369
4370 begin
4371 E := First_Entity (Etype (Id));
4372 while Present (E) loop
4373 if Ekind (E) = E_Entry
4374 and then Present (Get_Attribute_Definition_Clause
4375 (E, Attribute_Address))
4376 then
4377 Error_Msg_Warn := SPARK_Mode /= On;
4378 Error_Msg_N
4379 ("more than one task with same entry address<<", N);
4380 Error_Msg_N ("\Program_Error [<<", N);
4381 Insert_Action (N,
4382 Make_Raise_Program_Error (Loc,
4383 Reason => PE_Duplicated_Entry_Address));
4384 exit;
4385 end if;
4386
4387 Next_Entity (E);
4388 end loop;
4389 end;
4390 end if;
4391 end if;
4392
4393 -- Some simple constant-propagation: if the expression is a constant
4394 -- string initialized with a literal, share the literal. This avoids
4395 -- a run-time copy.
4396
4397 if Present (E)
4398 and then Is_Entity_Name (E)
4399 and then Ekind (Entity (E)) = E_Constant
4400 and then Base_Type (Etype (E)) = Standard_String
4401 then
4402 declare
4403 Val : constant Node_Id := Constant_Value (Entity (E));
4404 begin
4405 if Present (Val) and then Nkind (Val) = N_String_Literal then
4406 Rewrite (E, New_Copy (Val));
4407 end if;
4408 end;
4409 end if;
4410
4411 -- Another optimization: if the nominal subtype is unconstrained and
4412 -- the expression is a function call that returns an unconstrained
4413 -- type, rewrite the declaration as a renaming of the result of the
4414 -- call. The exceptions below are cases where the copy is expected,
4415 -- either by the back end (Aliased case) or by the semantics, as for
4416 -- initializing controlled types or copying tags for class-wide types.
4417
4418 if Present (E)
4419 and then Nkind (E) = N_Explicit_Dereference
4420 and then Nkind (Original_Node (E)) = N_Function_Call
4421 and then not Is_Library_Level_Entity (Id)
4422 and then not Is_Constrained (Underlying_Type (T))
4423 and then not Is_Aliased (Id)
4424 and then not Is_Class_Wide_Type (T)
4425 and then not Is_Controlled_Active (T)
4426 and then not Has_Controlled_Component (Base_Type (T))
4427 and then Expander_Active
4428 then
4429 Rewrite (N,
4430 Make_Object_Renaming_Declaration (Loc,
4431 Defining_Identifier => Id,
4432 Access_Definition => Empty,
4433 Subtype_Mark => New_Occurrence_Of
4434 (Base_Type (Etype (Id)), Loc),
4435 Name => E));
4436
4437 Set_Renamed_Object (Id, E);
4438
4439 -- Force generation of debugging information for the constant and for
4440 -- the renamed function call.
4441
4442 Set_Debug_Info_Needed (Id);
4443 Set_Debug_Info_Needed (Entity (Prefix (E)));
4444 end if;
4445
4446 if Present (Prev_Entity)
4447 and then Is_Frozen (Prev_Entity)
4448 and then not Error_Posted (Id)
4449 then
4450 Error_Msg_N ("full constant declaration appears too late", N);
4451 end if;
4452
4453 Check_Eliminated (Id);
4454
4455 -- Deal with setting In_Private_Part flag if in private part
4456
4457 if Ekind (Scope (Id)) = E_Package
4458 and then In_Private_Part (Scope (Id))
4459 then
4460 Set_In_Private_Part (Id);
4461 end if;
4462
4463 <<Leave>>
4464 -- Initialize the refined state of a variable here because this is a
4465 -- common destination for legal and illegal object declarations.
4466
4467 if Ekind (Id) = E_Variable then
4468 Set_Encapsulating_State (Id, Empty);
4469 end if;
4470
4471 if Has_Aspects (N) then
4472 Analyze_Aspect_Specifications (N, Id);
4473 end if;
4474
4475 Analyze_Dimension (N);
4476
4477 -- Verify whether the object declaration introduces an illegal hidden
4478 -- state within a package subject to a null abstract state.
4479
4480 if Ekind (Id) = E_Variable then
4481 Check_No_Hidden_State (Id);
4482 end if;
4483
4484 Ghost_Mode := Save_Ghost_Mode;
4485 end Analyze_Object_Declaration;
4486
4487 ---------------------------
4488 -- Analyze_Others_Choice --
4489 ---------------------------
4490
4491 -- Nothing to do for the others choice node itself, the semantic analysis
4492 -- of the others choice will occur as part of the processing of the parent
4493
4494 procedure Analyze_Others_Choice (N : Node_Id) is
4495 pragma Warnings (Off, N);
4496 begin
4497 null;
4498 end Analyze_Others_Choice;
4499
4500 -------------------------------------------
4501 -- Analyze_Private_Extension_Declaration --
4502 -------------------------------------------
4503
4504 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4505 Indic : constant Node_Id := Subtype_Indication (N);
4506 T : constant Entity_Id := Defining_Identifier (N);
4507 Iface : Entity_Id;
4508 Iface_Elmt : Elmt_Id;
4509 Parent_Base : Entity_Id;
4510 Parent_Type : Entity_Id;
4511
4512 begin
4513 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4514
4515 if Is_Non_Empty_List (Interface_List (N)) then
4516 declare
4517 Intf : Node_Id;
4518 T : Entity_Id;
4519
4520 begin
4521 Intf := First (Interface_List (N));
4522 while Present (Intf) loop
4523 T := Find_Type_Of_Subtype_Indic (Intf);
4524
4525 Diagnose_Interface (Intf, T);
4526 Next (Intf);
4527 end loop;
4528 end;
4529 end if;
4530
4531 Generate_Definition (T);
4532
4533 -- For other than Ada 2012, just enter the name in the current scope
4534
4535 if Ada_Version < Ada_2012 then
4536 Enter_Name (T);
4537
4538 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4539 -- case of private type that completes an incomplete type.
4540
4541 else
4542 declare
4543 Prev : Entity_Id;
4544
4545 begin
4546 Prev := Find_Type_Name (N);
4547
4548 pragma Assert (Prev = T
4549 or else (Ekind (Prev) = E_Incomplete_Type
4550 and then Present (Full_View (Prev))
4551 and then Full_View (Prev) = T));
4552 end;
4553 end if;
4554
4555 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4556 Parent_Base := Base_Type (Parent_Type);
4557
4558 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4559 Set_Ekind (T, Ekind (Parent_Type));
4560 Set_Etype (T, Any_Type);
4561 goto Leave;
4562
4563 elsif not Is_Tagged_Type (Parent_Type) then
4564 Error_Msg_N
4565 ("parent of type extension must be a tagged type ", Indic);
4566 goto Leave;
4567
4568 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4569 Error_Msg_N ("premature derivation of incomplete type", Indic);
4570 goto Leave;
4571
4572 elsif Is_Concurrent_Type (Parent_Type) then
4573 Error_Msg_N
4574 ("parent type of a private extension cannot be a synchronized "
4575 & "tagged type (RM 3.9.1 (3/1))", N);
4576
4577 Set_Etype (T, Any_Type);
4578 Set_Ekind (T, E_Limited_Private_Type);
4579 Set_Private_Dependents (T, New_Elmt_List);
4580 Set_Error_Posted (T);
4581 goto Leave;
4582 end if;
4583
4584 -- Perhaps the parent type should be changed to the class-wide type's
4585 -- specific type in this case to prevent cascading errors ???
4586
4587 if Is_Class_Wide_Type (Parent_Type) then
4588 Error_Msg_N
4589 ("parent of type extension must not be a class-wide type", Indic);
4590 goto Leave;
4591 end if;
4592
4593 if (not Is_Package_Or_Generic_Package (Current_Scope)
4594 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4595 or else In_Private_Part (Current_Scope)
4596 then
4597 Error_Msg_N ("invalid context for private extension", N);
4598 end if;
4599
4600 -- Set common attributes
4601
4602 Set_Is_Pure (T, Is_Pure (Current_Scope));
4603 Set_Scope (T, Current_Scope);
4604 Set_Ekind (T, E_Record_Type_With_Private);
4605 Init_Size_Align (T);
4606 Set_Default_SSO (T);
4607
4608 Set_Etype (T, Parent_Base);
4609 Propagate_Concurrent_Flags (T, Parent_Base);
4610
4611 Set_Convention (T, Convention (Parent_Type));
4612 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4613 Set_Is_First_Subtype (T);
4614 Make_Class_Wide_Type (T);
4615
4616 if Unknown_Discriminants_Present (N) then
4617 Set_Discriminant_Constraint (T, No_Elist);
4618 end if;
4619
4620 Build_Derived_Record_Type (N, Parent_Type, T);
4621
4622 -- A private extension inherits the Default_Initial_Condition pragma
4623 -- coming from any parent type within the derivation chain.
4624
4625 if Has_DIC (Parent_Type) then
4626 Set_Has_Inherited_DIC (T);
4627 end if;
4628
4629 -- A private extension inherits any class-wide invariants coming from a
4630 -- parent type or an interface. Note that the invariant procedure of the
4631 -- parent type should not be inherited because the private extension may
4632 -- define invariants of its own.
4633
4634 if Has_Inherited_Invariants (Parent_Type)
4635 or else Has_Inheritable_Invariants (Parent_Type)
4636 then
4637 Set_Has_Inherited_Invariants (T);
4638
4639 elsif Present (Interfaces (T)) then
4640 Iface_Elmt := First_Elmt (Interfaces (T));
4641 while Present (Iface_Elmt) loop
4642 Iface := Node (Iface_Elmt);
4643
4644 if Has_Inheritable_Invariants (Iface) then
4645 Set_Has_Inherited_Invariants (T);
4646 exit;
4647 end if;
4648
4649 Next_Elmt (Iface_Elmt);
4650 end loop;
4651 end if;
4652
4653 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4654 -- synchronized formal derived type.
4655
4656 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4657 Set_Is_Limited_Record (T);
4658
4659 -- Formal derived type case
4660
4661 if Is_Generic_Type (T) then
4662
4663 -- The parent must be a tagged limited type or a synchronized
4664 -- interface.
4665
4666 if (not Is_Tagged_Type (Parent_Type)
4667 or else not Is_Limited_Type (Parent_Type))
4668 and then
4669 (not Is_Interface (Parent_Type)
4670 or else not Is_Synchronized_Interface (Parent_Type))
4671 then
4672 Error_Msg_NE
4673 ("parent type of & must be tagged limited or synchronized",
4674 N, T);
4675 end if;
4676
4677 -- The progenitors (if any) must be limited or synchronized
4678 -- interfaces.
4679
4680 if Present (Interfaces (T)) then
4681 Iface_Elmt := First_Elmt (Interfaces (T));
4682 while Present (Iface_Elmt) loop
4683 Iface := Node (Iface_Elmt);
4684
4685 if not Is_Limited_Interface (Iface)
4686 and then not Is_Synchronized_Interface (Iface)
4687 then
4688 Error_Msg_NE
4689 ("progenitor & must be limited or synchronized",
4690 N, Iface);
4691 end if;
4692
4693 Next_Elmt (Iface_Elmt);
4694 end loop;
4695 end if;
4696
4697 -- Regular derived extension, the parent must be a limited or
4698 -- synchronized interface.
4699
4700 else
4701 if not Is_Interface (Parent_Type)
4702 or else (not Is_Limited_Interface (Parent_Type)
4703 and then not Is_Synchronized_Interface (Parent_Type))
4704 then
4705 Error_Msg_NE
4706 ("parent type of & must be limited interface", N, T);
4707 end if;
4708 end if;
4709
4710 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4711 -- extension with a synchronized parent must be explicitly declared
4712 -- synchronized, because the full view will be a synchronized type.
4713 -- This must be checked before the check for limited types below,
4714 -- to ensure that types declared limited are not allowed to extend
4715 -- synchronized interfaces.
4716
4717 elsif Is_Interface (Parent_Type)
4718 and then Is_Synchronized_Interface (Parent_Type)
4719 and then not Synchronized_Present (N)
4720 then
4721 Error_Msg_NE
4722 ("private extension of& must be explicitly synchronized",
4723 N, Parent_Type);
4724
4725 elsif Limited_Present (N) then
4726 Set_Is_Limited_Record (T);
4727
4728 if not Is_Limited_Type (Parent_Type)
4729 and then
4730 (not Is_Interface (Parent_Type)
4731 or else not Is_Limited_Interface (Parent_Type))
4732 then
4733 Error_Msg_NE ("parent type& of limited extension must be limited",
4734 N, Parent_Type);
4735 end if;
4736 end if;
4737
4738 <<Leave>>
4739 if Has_Aspects (N) then
4740 Analyze_Aspect_Specifications (N, T);
4741 end if;
4742 end Analyze_Private_Extension_Declaration;
4743
4744 ---------------------------------
4745 -- Analyze_Subtype_Declaration --
4746 ---------------------------------
4747
4748 procedure Analyze_Subtype_Declaration
4749 (N : Node_Id;
4750 Skip : Boolean := False)
4751 is
4752 Id : constant Entity_Id := Defining_Identifier (N);
4753 R_Checks : Check_Result;
4754 T : Entity_Id;
4755
4756 begin
4757 Generate_Definition (Id);
4758 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4759 Init_Size_Align (Id);
4760
4761 -- The following guard condition on Enter_Name is to handle cases where
4762 -- the defining identifier has already been entered into the scope but
4763 -- the declaration as a whole needs to be analyzed.
4764
4765 -- This case in particular happens for derived enumeration types. The
4766 -- derived enumeration type is processed as an inserted enumeration type
4767 -- declaration followed by a rewritten subtype declaration. The defining
4768 -- identifier, however, is entered into the name scope very early in the
4769 -- processing of the original type declaration and therefore needs to be
4770 -- avoided here, when the created subtype declaration is analyzed. (See
4771 -- Build_Derived_Types)
4772
4773 -- This also happens when the full view of a private type is derived
4774 -- type with constraints. In this case the entity has been introduced
4775 -- in the private declaration.
4776
4777 -- Finally this happens in some complex cases when validity checks are
4778 -- enabled, where the same subtype declaration may be analyzed twice.
4779 -- This can happen if the subtype is created by the pre-analysis of
4780 -- an attribute tht gives the range of a loop statement, and the loop
4781 -- itself appears within an if_statement that will be rewritten during
4782 -- expansion.
4783
4784 if Skip
4785 or else (Present (Etype (Id))
4786 and then (Is_Private_Type (Etype (Id))
4787 or else Is_Task_Type (Etype (Id))
4788 or else Is_Rewrite_Substitution (N)))
4789 then
4790 null;
4791
4792 elsif Current_Entity (Id) = Id then
4793 null;
4794
4795 else
4796 Enter_Name (Id);
4797 end if;
4798
4799 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4800
4801 -- Class-wide equivalent types of records with unknown discriminants
4802 -- involve the generation of an itype which serves as the private view
4803 -- of a constrained record subtype. In such cases the base type of the
4804 -- current subtype we are processing is the private itype. Use the full
4805 -- of the private itype when decorating various attributes.
4806
4807 if Is_Itype (T)
4808 and then Is_Private_Type (T)
4809 and then Present (Full_View (T))
4810 then
4811 T := Full_View (T);
4812 end if;
4813
4814 -- Inherit common attributes
4815
4816 Set_Is_Volatile (Id, Is_Volatile (T));
4817 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4818 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4819 Set_Convention (Id, Convention (T));
4820
4821 -- If ancestor has predicates then so does the subtype, and in addition
4822 -- we must delay the freeze to properly arrange predicate inheritance.
4823
4824 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4825 -- in which T = ID, so the above tests and assignments do nothing???
4826
4827 if Has_Predicates (T)
4828 or else (Present (Ancestor_Subtype (T))
4829 and then Has_Predicates (Ancestor_Subtype (T)))
4830 then
4831 Set_Has_Predicates (Id);
4832 Set_Has_Delayed_Freeze (Id);
4833
4834 -- Generated subtypes inherit the predicate function from the parent
4835 -- (no aspects to examine on the generated declaration).
4836
4837 if not Comes_From_Source (N) then
4838 Set_Ekind (Id, Ekind (T));
4839
4840 if Present (Predicate_Function (T)) then
4841 Set_Predicate_Function (Id, Predicate_Function (T));
4842
4843 elsif Present (Ancestor_Subtype (T))
4844 and then Has_Predicates (Ancestor_Subtype (T))
4845 and then Present (Predicate_Function (Ancestor_Subtype (T)))
4846 then
4847 Set_Predicate_Function (Id,
4848 Predicate_Function (Ancestor_Subtype (T)));
4849 end if;
4850 end if;
4851 end if;
4852
4853 -- Subtype of Boolean cannot have a constraint in SPARK
4854
4855 if Is_Boolean_Type (T)
4856 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4857 then
4858 Check_SPARK_05_Restriction
4859 ("subtype of Boolean cannot have constraint", N);
4860 end if;
4861
4862 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4863 declare
4864 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4865 One_Cstr : Node_Id;
4866 Low : Node_Id;
4867 High : Node_Id;
4868
4869 begin
4870 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4871 One_Cstr := First (Constraints (Cstr));
4872 while Present (One_Cstr) loop
4873
4874 -- Index or discriminant constraint in SPARK must be a
4875 -- subtype mark.
4876
4877 if not
4878 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4879 then
4880 Check_SPARK_05_Restriction
4881 ("subtype mark required", One_Cstr);
4882
4883 -- String subtype must have a lower bound of 1 in SPARK.
4884 -- Note that we do not need to test for the non-static case
4885 -- here, since that was already taken care of in
4886 -- Process_Range_Expr_In_Decl.
4887
4888 elsif Base_Type (T) = Standard_String then
4889 Get_Index_Bounds (One_Cstr, Low, High);
4890
4891 if Is_OK_Static_Expression (Low)
4892 and then Expr_Value (Low) /= 1
4893 then
4894 Check_SPARK_05_Restriction
4895 ("String subtype must have lower bound of 1", N);
4896 end if;
4897 end if;
4898
4899 Next (One_Cstr);
4900 end loop;
4901 end if;
4902 end;
4903 end if;
4904
4905 -- In the case where there is no constraint given in the subtype
4906 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4907 -- semantic attributes must be established here.
4908
4909 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4910 Set_Etype (Id, Base_Type (T));
4911
4912 -- Subtype of unconstrained array without constraint is not allowed
4913 -- in SPARK.
4914
4915 if Is_Array_Type (T) and then not Is_Constrained (T) then
4916 Check_SPARK_05_Restriction
4917 ("subtype of unconstrained array must have constraint", N);
4918 end if;
4919
4920 case Ekind (T) is
4921 when Array_Kind =>
4922 Set_Ekind (Id, E_Array_Subtype);
4923 Copy_Array_Subtype_Attributes (Id, T);
4924
4925 when Decimal_Fixed_Point_Kind =>
4926 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4927 Set_Digits_Value (Id, Digits_Value (T));
4928 Set_Delta_Value (Id, Delta_Value (T));
4929 Set_Scale_Value (Id, Scale_Value (T));
4930 Set_Small_Value (Id, Small_Value (T));
4931 Set_Scalar_Range (Id, Scalar_Range (T));
4932 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4933 Set_Is_Constrained (Id, Is_Constrained (T));
4934 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4935 Set_RM_Size (Id, RM_Size (T));
4936
4937 when Enumeration_Kind =>
4938 Set_Ekind (Id, E_Enumeration_Subtype);
4939 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4940 Set_Scalar_Range (Id, Scalar_Range (T));
4941 Set_Is_Character_Type (Id, Is_Character_Type (T));
4942 Set_Is_Constrained (Id, Is_Constrained (T));
4943 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4944 Set_RM_Size (Id, RM_Size (T));
4945 Inherit_Predicate_Flags (Id, T);
4946
4947 when Ordinary_Fixed_Point_Kind =>
4948 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4949 Set_Scalar_Range (Id, Scalar_Range (T));
4950 Set_Small_Value (Id, Small_Value (T));
4951 Set_Delta_Value (Id, Delta_Value (T));
4952 Set_Is_Constrained (Id, Is_Constrained (T));
4953 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4954 Set_RM_Size (Id, RM_Size (T));
4955
4956 when Float_Kind =>
4957 Set_Ekind (Id, E_Floating_Point_Subtype);
4958 Set_Scalar_Range (Id, Scalar_Range (T));
4959 Set_Digits_Value (Id, Digits_Value (T));
4960 Set_Is_Constrained (Id, Is_Constrained (T));
4961
4962 -- If the floating point type has dimensions, these will be
4963 -- inherited subsequently when Analyze_Dimensions is called.
4964
4965 when Signed_Integer_Kind =>
4966 Set_Ekind (Id, E_Signed_Integer_Subtype);
4967 Set_Scalar_Range (Id, Scalar_Range (T));
4968 Set_Is_Constrained (Id, Is_Constrained (T));
4969 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4970 Set_RM_Size (Id, RM_Size (T));
4971 Inherit_Predicate_Flags (Id, T);
4972
4973 when Modular_Integer_Kind =>
4974 Set_Ekind (Id, E_Modular_Integer_Subtype);
4975 Set_Scalar_Range (Id, Scalar_Range (T));
4976 Set_Is_Constrained (Id, Is_Constrained (T));
4977 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4978 Set_RM_Size (Id, RM_Size (T));
4979 Inherit_Predicate_Flags (Id, T);
4980
4981 when Class_Wide_Kind =>
4982 Set_Ekind (Id, E_Class_Wide_Subtype);
4983 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4984 Set_Cloned_Subtype (Id, T);
4985 Set_Is_Tagged_Type (Id, True);
4986 Set_Has_Unknown_Discriminants
4987 (Id, True);
4988 Set_No_Tagged_Streams_Pragma
4989 (Id, No_Tagged_Streams_Pragma (T));
4990
4991 if Ekind (T) = E_Class_Wide_Subtype then
4992 Set_Equivalent_Type (Id, Equivalent_Type (T));
4993 end if;
4994
4995 when E_Record_Type | E_Record_Subtype =>
4996 Set_Ekind (Id, E_Record_Subtype);
4997
4998 if Ekind (T) = E_Record_Subtype
4999 and then Present (Cloned_Subtype (T))
5000 then
5001 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5002 else
5003 Set_Cloned_Subtype (Id, T);
5004 end if;
5005
5006 Set_First_Entity (Id, First_Entity (T));
5007 Set_Last_Entity (Id, Last_Entity (T));
5008 Set_Has_Discriminants (Id, Has_Discriminants (T));
5009 Set_Is_Constrained (Id, Is_Constrained (T));
5010 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5011 Set_Has_Implicit_Dereference
5012 (Id, Has_Implicit_Dereference (T));
5013 Set_Has_Unknown_Discriminants
5014 (Id, Has_Unknown_Discriminants (T));
5015
5016 if Has_Discriminants (T) then
5017 Set_Discriminant_Constraint
5018 (Id, Discriminant_Constraint (T));
5019 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5020
5021 elsif Has_Unknown_Discriminants (Id) then
5022 Set_Discriminant_Constraint (Id, No_Elist);
5023 end if;
5024
5025 if Is_Tagged_Type (T) then
5026 Set_Is_Tagged_Type (Id, True);
5027 Set_No_Tagged_Streams_Pragma
5028 (Id, No_Tagged_Streams_Pragma (T));
5029 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5030 Set_Direct_Primitive_Operations
5031 (Id, Direct_Primitive_Operations (T));
5032 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5033
5034 if Is_Interface (T) then
5035 Set_Is_Interface (Id);
5036 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5037 end if;
5038 end if;
5039
5040 when Private_Kind =>
5041 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5042 Set_Has_Discriminants (Id, Has_Discriminants (T));
5043 Set_Is_Constrained (Id, Is_Constrained (T));
5044 Set_First_Entity (Id, First_Entity (T));
5045 Set_Last_Entity (Id, Last_Entity (T));
5046 Set_Private_Dependents (Id, New_Elmt_List);
5047 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5048 Set_Has_Implicit_Dereference
5049 (Id, Has_Implicit_Dereference (T));
5050 Set_Has_Unknown_Discriminants
5051 (Id, Has_Unknown_Discriminants (T));
5052 Set_Known_To_Have_Preelab_Init
5053 (Id, Known_To_Have_Preelab_Init (T));
5054
5055 if Is_Tagged_Type (T) then
5056 Set_Is_Tagged_Type (Id);
5057 Set_No_Tagged_Streams_Pragma (Id,
5058 No_Tagged_Streams_Pragma (T));
5059 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5060 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5061 Set_Direct_Primitive_Operations (Id,
5062 Direct_Primitive_Operations (T));
5063 end if;
5064
5065 -- In general the attributes of the subtype of a private type
5066 -- are the attributes of the partial view of parent. However,
5067 -- the full view may be a discriminated type, and the subtype
5068 -- must share the discriminant constraint to generate correct
5069 -- calls to initialization procedures.
5070
5071 if Has_Discriminants (T) then
5072 Set_Discriminant_Constraint
5073 (Id, Discriminant_Constraint (T));
5074 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5075
5076 elsif Present (Full_View (T))
5077 and then Has_Discriminants (Full_View (T))
5078 then
5079 Set_Discriminant_Constraint
5080 (Id, Discriminant_Constraint (Full_View (T)));
5081 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5082
5083 -- This would seem semantically correct, but apparently
5084 -- generates spurious errors about missing components ???
5085
5086 -- Set_Has_Discriminants (Id);
5087 end if;
5088
5089 Prepare_Private_Subtype_Completion (Id, N);
5090
5091 -- If this is the subtype of a constrained private type with
5092 -- discriminants that has got a full view and we also have
5093 -- built a completion just above, show that the completion
5094 -- is a clone of the full view to the back-end.
5095
5096 if Has_Discriminants (T)
5097 and then not Has_Unknown_Discriminants (T)
5098 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5099 and then Present (Full_View (T))
5100 and then Present (Full_View (Id))
5101 then
5102 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5103 end if;
5104
5105 when Access_Kind =>
5106 Set_Ekind (Id, E_Access_Subtype);
5107 Set_Is_Constrained (Id, Is_Constrained (T));
5108 Set_Is_Access_Constant
5109 (Id, Is_Access_Constant (T));
5110 Set_Directly_Designated_Type
5111 (Id, Designated_Type (T));
5112 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5113
5114 -- A Pure library_item must not contain the declaration of a
5115 -- named access type, except within a subprogram, generic
5116 -- subprogram, task unit, or protected unit, or if it has
5117 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5118
5119 if Comes_From_Source (Id)
5120 and then In_Pure_Unit
5121 and then not In_Subprogram_Task_Protected_Unit
5122 and then not No_Pool_Assigned (Id)
5123 then
5124 Error_Msg_N
5125 ("named access types not allowed in pure unit", N);
5126 end if;
5127
5128 when Concurrent_Kind =>
5129 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5130 Set_Corresponding_Record_Type (Id,
5131 Corresponding_Record_Type (T));
5132 Set_First_Entity (Id, First_Entity (T));
5133 Set_First_Private_Entity (Id, First_Private_Entity (T));
5134 Set_Has_Discriminants (Id, Has_Discriminants (T));
5135 Set_Is_Constrained (Id, Is_Constrained (T));
5136 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5137 Set_Last_Entity (Id, Last_Entity (T));
5138
5139 if Is_Tagged_Type (T) then
5140 Set_No_Tagged_Streams_Pragma
5141 (Id, No_Tagged_Streams_Pragma (T));
5142 end if;
5143
5144 if Has_Discriminants (T) then
5145 Set_Discriminant_Constraint
5146 (Id, Discriminant_Constraint (T));
5147 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5148 end if;
5149
5150 when Incomplete_Kind =>
5151 if Ada_Version >= Ada_2005 then
5152
5153 -- In Ada 2005 an incomplete type can be explicitly tagged:
5154 -- propagate indication. Note that we also have to include
5155 -- subtypes for Ada 2012 extended use of incomplete types.
5156
5157 Set_Ekind (Id, E_Incomplete_Subtype);
5158 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5159 Set_Private_Dependents (Id, New_Elmt_List);
5160
5161 if Is_Tagged_Type (Id) then
5162 Set_No_Tagged_Streams_Pragma
5163 (Id, No_Tagged_Streams_Pragma (T));
5164 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5165 end if;
5166
5167 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5168 -- incomplete type visible through a limited with clause.
5169
5170 if From_Limited_With (T)
5171 and then Present (Non_Limited_View (T))
5172 then
5173 Set_From_Limited_With (Id);
5174 Set_Non_Limited_View (Id, Non_Limited_View (T));
5175
5176 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5177 -- to the private dependents of the original incomplete
5178 -- type for future transformation.
5179
5180 else
5181 Append_Elmt (Id, Private_Dependents (T));
5182 end if;
5183
5184 -- If the subtype name denotes an incomplete type an error
5185 -- was already reported by Process_Subtype.
5186
5187 else
5188 Set_Etype (Id, Any_Type);
5189 end if;
5190
5191 when others =>
5192 raise Program_Error;
5193 end case;
5194 end if;
5195
5196 if Etype (Id) = Any_Type then
5197 goto Leave;
5198 end if;
5199
5200 -- Some common processing on all types
5201
5202 Set_Size_Info (Id, T);
5203 Set_First_Rep_Item (Id, First_Rep_Item (T));
5204
5205 -- If the parent type is a generic actual, so is the subtype. This may
5206 -- happen in a nested instance. Why Comes_From_Source test???
5207
5208 if not Comes_From_Source (N) then
5209 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5210 end if;
5211
5212 -- If this is a subtype declaration for an actual in an instance,
5213 -- inherit static and dynamic predicates if any.
5214
5215 -- If declaration has no aspect specifications, inherit predicate
5216 -- info as well. Unclear how to handle the case of both specified
5217 -- and inherited predicates ??? Other inherited aspects, such as
5218 -- invariants, should be OK, but the combination with later pragmas
5219 -- may also require special merging.
5220
5221 if Has_Predicates (T)
5222 and then Present (Predicate_Function (T))
5223 and then
5224 ((In_Instance and then not Comes_From_Source (N))
5225 or else No (Aspect_Specifications (N)))
5226 then
5227 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5228
5229 if Has_Static_Predicate (T) then
5230 Set_Has_Static_Predicate (Id);
5231 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5232 end if;
5233 end if;
5234
5235 -- Remaining processing depends on characteristics of base type
5236
5237 T := Etype (Id);
5238
5239 Set_Is_Immediately_Visible (Id, True);
5240 Set_Depends_On_Private (Id, Has_Private_Component (T));
5241 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5242
5243 if Is_Interface (T) then
5244 Set_Is_Interface (Id);
5245 end if;
5246
5247 if Present (Generic_Parent_Type (N))
5248 and then
5249 (Nkind (Parent (Generic_Parent_Type (N))) /=
5250 N_Formal_Type_Declaration
5251 or else Nkind (Formal_Type_Definition
5252 (Parent (Generic_Parent_Type (N)))) /=
5253 N_Formal_Private_Type_Definition)
5254 then
5255 if Is_Tagged_Type (Id) then
5256
5257 -- If this is a generic actual subtype for a synchronized type,
5258 -- the primitive operations are those of the corresponding record
5259 -- for which there is a separate subtype declaration.
5260
5261 if Is_Concurrent_Type (Id) then
5262 null;
5263 elsif Is_Class_Wide_Type (Id) then
5264 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5265 else
5266 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5267 end if;
5268
5269 elsif Scope (Etype (Id)) /= Standard_Standard then
5270 Derive_Subprograms (Generic_Parent_Type (N), Id);
5271 end if;
5272 end if;
5273
5274 if Is_Private_Type (T) and then Present (Full_View (T)) then
5275 Conditional_Delay (Id, Full_View (T));
5276
5277 -- The subtypes of components or subcomponents of protected types
5278 -- do not need freeze nodes, which would otherwise appear in the
5279 -- wrong scope (before the freeze node for the protected type). The
5280 -- proper subtypes are those of the subcomponents of the corresponding
5281 -- record.
5282
5283 elsif Ekind (Scope (Id)) /= E_Protected_Type
5284 and then Present (Scope (Scope (Id))) -- error defense
5285 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5286 then
5287 Conditional_Delay (Id, T);
5288 end if;
5289
5290 -- Check that Constraint_Error is raised for a scalar subtype indication
5291 -- when the lower or upper bound of a non-null range lies outside the
5292 -- range of the type mark.
5293
5294 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5295 if Is_Scalar_Type (Etype (Id))
5296 and then Scalar_Range (Id) /=
5297 Scalar_Range
5298 (Etype (Subtype_Mark (Subtype_Indication (N))))
5299 then
5300 Apply_Range_Check
5301 (Scalar_Range (Id),
5302 Etype (Subtype_Mark (Subtype_Indication (N))));
5303
5304 -- In the array case, check compatibility for each index
5305
5306 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5307 then
5308 -- This really should be a subprogram that finds the indications
5309 -- to check???
5310
5311 declare
5312 Subt_Index : Node_Id := First_Index (Id);
5313 Target_Index : Node_Id :=
5314 First_Index (Etype
5315 (Subtype_Mark (Subtype_Indication (N))));
5316 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5317
5318 begin
5319 while Present (Subt_Index) loop
5320 if ((Nkind (Subt_Index) = N_Identifier
5321 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5322 or else Nkind (Subt_Index) = N_Subtype_Indication)
5323 and then
5324 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5325 then
5326 declare
5327 Target_Typ : constant Entity_Id :=
5328 Etype (Target_Index);
5329 begin
5330 R_Checks :=
5331 Get_Range_Checks
5332 (Scalar_Range (Etype (Subt_Index)),
5333 Target_Typ,
5334 Etype (Subt_Index),
5335 Defining_Identifier (N));
5336
5337 -- Reset Has_Dynamic_Range_Check on the subtype to
5338 -- prevent elision of the index check due to a dynamic
5339 -- check generated for a preceding index (needed since
5340 -- Insert_Range_Checks tries to avoid generating
5341 -- redundant checks on a given declaration).
5342
5343 Set_Has_Dynamic_Range_Check (N, False);
5344
5345 Insert_Range_Checks
5346 (R_Checks,
5347 N,
5348 Target_Typ,
5349 Sloc (Defining_Identifier (N)));
5350
5351 -- Record whether this index involved a dynamic check
5352
5353 Has_Dyn_Chk :=
5354 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5355 end;
5356 end if;
5357
5358 Next_Index (Subt_Index);
5359 Next_Index (Target_Index);
5360 end loop;
5361
5362 -- Finally, mark whether the subtype involves dynamic checks
5363
5364 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5365 end;
5366 end if;
5367 end if;
5368
5369 Set_Optimize_Alignment_Flags (Id);
5370 Check_Eliminated (Id);
5371
5372 <<Leave>>
5373 if Has_Aspects (N) then
5374 Analyze_Aspect_Specifications (N, Id);
5375 end if;
5376
5377 Analyze_Dimension (N);
5378
5379 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5380 -- indications on composite types where the constraints are dynamic.
5381 -- Note that object declarations and aggregates generate implicit
5382 -- subtype declarations, which this covers. One special case is that the
5383 -- implicitly generated "=" for discriminated types includes an
5384 -- offending subtype declaration, which is harmless, so we ignore it
5385 -- here.
5386
5387 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5388 declare
5389 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5390 begin
5391 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5392 and then not (Is_Internal (Id)
5393 and then Is_TSS (Scope (Id),
5394 TSS_Composite_Equality))
5395 and then not Within_Init_Proc
5396 and then not All_Composite_Constraints_Static (Cstr)
5397 then
5398 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5399 end if;
5400 end;
5401 end if;
5402 end Analyze_Subtype_Declaration;
5403
5404 --------------------------------
5405 -- Analyze_Subtype_Indication --
5406 --------------------------------
5407
5408 procedure Analyze_Subtype_Indication (N : Node_Id) is
5409 T : constant Entity_Id := Subtype_Mark (N);
5410 R : constant Node_Id := Range_Expression (Constraint (N));
5411
5412 begin
5413 Analyze (T);
5414
5415 if R /= Error then
5416 Analyze (R);
5417 Set_Etype (N, Etype (R));
5418 Resolve (R, Entity (T));
5419 else
5420 Set_Error_Posted (R);
5421 Set_Error_Posted (T);
5422 end if;
5423 end Analyze_Subtype_Indication;
5424
5425 --------------------------
5426 -- Analyze_Variant_Part --
5427 --------------------------
5428
5429 procedure Analyze_Variant_Part (N : Node_Id) is
5430 Discr_Name : Node_Id;
5431 Discr_Type : Entity_Id;
5432
5433 procedure Process_Variant (A : Node_Id);
5434 -- Analyze declarations for a single variant
5435
5436 package Analyze_Variant_Choices is
5437 new Generic_Analyze_Choices (Process_Variant);
5438 use Analyze_Variant_Choices;
5439
5440 ---------------------
5441 -- Process_Variant --
5442 ---------------------
5443
5444 procedure Process_Variant (A : Node_Id) is
5445 CL : constant Node_Id := Component_List (A);
5446 begin
5447 if not Null_Present (CL) then
5448 Analyze_Declarations (Component_Items (CL));
5449
5450 if Present (Variant_Part (CL)) then
5451 Analyze (Variant_Part (CL));
5452 end if;
5453 end if;
5454 end Process_Variant;
5455
5456 -- Start of processing for Analyze_Variant_Part
5457
5458 begin
5459 Discr_Name := Name (N);
5460 Analyze (Discr_Name);
5461
5462 -- If Discr_Name bad, get out (prevent cascaded errors)
5463
5464 if Etype (Discr_Name) = Any_Type then
5465 return;
5466 end if;
5467
5468 -- Check invalid discriminant in variant part
5469
5470 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5471 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5472 end if;
5473
5474 Discr_Type := Etype (Entity (Discr_Name));
5475
5476 if not Is_Discrete_Type (Discr_Type) then
5477 Error_Msg_N
5478 ("discriminant in a variant part must be of a discrete type",
5479 Name (N));
5480 return;
5481 end if;
5482
5483 -- Now analyze the choices, which also analyzes the declarations that
5484 -- are associated with each choice.
5485
5486 Analyze_Choices (Variants (N), Discr_Type);
5487
5488 -- Note: we used to instantiate and call Check_Choices here to check
5489 -- that the choices covered the discriminant, but it's too early to do
5490 -- that because of statically predicated subtypes, whose analysis may
5491 -- be deferred to their freeze point which may be as late as the freeze
5492 -- point of the containing record. So this call is now to be found in
5493 -- Freeze_Record_Declaration.
5494
5495 end Analyze_Variant_Part;
5496
5497 ----------------------------
5498 -- Array_Type_Declaration --
5499 ----------------------------
5500
5501 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5502 Component_Def : constant Node_Id := Component_Definition (Def);
5503 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5504 Element_Type : Entity_Id;
5505 Implicit_Base : Entity_Id;
5506 Index : Node_Id;
5507 Related_Id : Entity_Id := Empty;
5508 Nb_Index : Nat;
5509 P : constant Node_Id := Parent (Def);
5510 Priv : Entity_Id;
5511
5512 begin
5513 if Nkind (Def) = N_Constrained_Array_Definition then
5514 Index := First (Discrete_Subtype_Definitions (Def));
5515 else
5516 Index := First (Subtype_Marks (Def));
5517 end if;
5518
5519 -- Find proper names for the implicit types which may be public. In case
5520 -- of anonymous arrays we use the name of the first object of that type
5521 -- as prefix.
5522
5523 if No (T) then
5524 Related_Id := Defining_Identifier (P);
5525 else
5526 Related_Id := T;
5527 end if;
5528
5529 Nb_Index := 1;
5530 while Present (Index) loop
5531 Analyze (Index);
5532
5533 -- Test for odd case of trying to index a type by the type itself
5534
5535 if Is_Entity_Name (Index) and then Entity (Index) = T then
5536 Error_Msg_N ("type& cannot be indexed by itself", Index);
5537 Set_Entity (Index, Standard_Boolean);
5538 Set_Etype (Index, Standard_Boolean);
5539 end if;
5540
5541 -- Check SPARK restriction requiring a subtype mark
5542
5543 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5544 Check_SPARK_05_Restriction ("subtype mark required", Index);
5545 end if;
5546
5547 -- Add a subtype declaration for each index of private array type
5548 -- declaration whose etype is also private. For example:
5549
5550 -- package Pkg is
5551 -- type Index is private;
5552 -- private
5553 -- type Table is array (Index) of ...
5554 -- end;
5555
5556 -- This is currently required by the expander for the internally
5557 -- generated equality subprogram of records with variant parts in
5558 -- which the etype of some component is such private type.
5559
5560 if Ekind (Current_Scope) = E_Package
5561 and then In_Private_Part (Current_Scope)
5562 and then Has_Private_Declaration (Etype (Index))
5563 then
5564 declare
5565 Loc : constant Source_Ptr := Sloc (Def);
5566 New_E : Entity_Id;
5567 Decl : Entity_Id;
5568
5569 begin
5570 New_E := Make_Temporary (Loc, 'T');
5571 Set_Is_Internal (New_E);
5572
5573 Decl :=
5574 Make_Subtype_Declaration (Loc,
5575 Defining_Identifier => New_E,
5576 Subtype_Indication =>
5577 New_Occurrence_Of (Etype (Index), Loc));
5578
5579 Insert_Before (Parent (Def), Decl);
5580 Analyze (Decl);
5581 Set_Etype (Index, New_E);
5582
5583 -- If the index is a range the Entity attribute is not
5584 -- available. Example:
5585
5586 -- package Pkg is
5587 -- type T is private;
5588 -- private
5589 -- type T is new Natural;
5590 -- Table : array (T(1) .. T(10)) of Boolean;
5591 -- end Pkg;
5592
5593 if Nkind (Index) /= N_Range then
5594 Set_Entity (Index, New_E);
5595 end if;
5596 end;
5597 end if;
5598
5599 Make_Index (Index, P, Related_Id, Nb_Index);
5600
5601 -- Check error of subtype with predicate for index type
5602
5603 Bad_Predicated_Subtype_Use
5604 ("subtype& has predicate, not allowed as index subtype",
5605 Index, Etype (Index));
5606
5607 -- Move to next index
5608
5609 Next_Index (Index);
5610 Nb_Index := Nb_Index + 1;
5611 end loop;
5612
5613 -- Process subtype indication if one is present
5614
5615 if Present (Component_Typ) then
5616 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5617
5618 Set_Etype (Component_Typ, Element_Type);
5619
5620 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5621 Check_SPARK_05_Restriction
5622 ("subtype mark required", Component_Typ);
5623 end if;
5624
5625 -- Ada 2005 (AI-230): Access Definition case
5626
5627 else pragma Assert (Present (Access_Definition (Component_Def)));
5628
5629 -- Indicate that the anonymous access type is created by the
5630 -- array type declaration.
5631
5632 Element_Type := Access_Definition
5633 (Related_Nod => P,
5634 N => Access_Definition (Component_Def));
5635 Set_Is_Local_Anonymous_Access (Element_Type);
5636
5637 -- Propagate the parent. This field is needed if we have to generate
5638 -- the master_id associated with an anonymous access to task type
5639 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5640
5641 Set_Parent (Element_Type, Parent (T));
5642
5643 -- Ada 2005 (AI-230): In case of components that are anonymous access
5644 -- types the level of accessibility depends on the enclosing type
5645 -- declaration
5646
5647 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5648
5649 -- Ada 2005 (AI-254)
5650
5651 declare
5652 CD : constant Node_Id :=
5653 Access_To_Subprogram_Definition
5654 (Access_Definition (Component_Def));
5655 begin
5656 if Present (CD) and then Protected_Present (CD) then
5657 Element_Type :=
5658 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5659 end if;
5660 end;
5661 end if;
5662
5663 -- Constrained array case
5664
5665 if No (T) then
5666 T := Create_Itype (E_Void, P, Related_Id, 'T');
5667 end if;
5668
5669 if Nkind (Def) = N_Constrained_Array_Definition then
5670
5671 -- Establish Implicit_Base as unconstrained base type
5672
5673 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5674
5675 Set_Etype (Implicit_Base, Implicit_Base);
5676 Set_Scope (Implicit_Base, Current_Scope);
5677 Set_Has_Delayed_Freeze (Implicit_Base);
5678 Set_Default_SSO (Implicit_Base);
5679
5680 -- The constrained array type is a subtype of the unconstrained one
5681
5682 Set_Ekind (T, E_Array_Subtype);
5683 Init_Size_Align (T);
5684 Set_Etype (T, Implicit_Base);
5685 Set_Scope (T, Current_Scope);
5686 Set_Is_Constrained (T);
5687 Set_First_Index (T,
5688 First (Discrete_Subtype_Definitions (Def)));
5689 Set_Has_Delayed_Freeze (T);
5690
5691 -- Complete setup of implicit base type
5692
5693 Set_Component_Size (Implicit_Base, Uint_0);
5694 Set_Component_Type (Implicit_Base, Element_Type);
5695 Set_Finalize_Storage_Only
5696 (Implicit_Base,
5697 Finalize_Storage_Only (Element_Type));
5698 Set_First_Index (Implicit_Base, First_Index (T));
5699 Set_Has_Controlled_Component
5700 (Implicit_Base,
5701 Has_Controlled_Component (Element_Type)
5702 or else Is_Controlled_Active (Element_Type));
5703 Set_Packed_Array_Impl_Type
5704 (Implicit_Base, Empty);
5705
5706 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5707
5708 -- Inherit the "ghostness" from the constrained array type
5709
5710 if Ghost_Mode > None or else Is_Ghost_Entity (T) then
5711 Set_Is_Ghost_Entity (Implicit_Base);
5712 end if;
5713
5714 -- Unconstrained array case
5715
5716 else
5717 Set_Ekind (T, E_Array_Type);
5718 Init_Size_Align (T);
5719 Set_Etype (T, T);
5720 Set_Scope (T, Current_Scope);
5721 Set_Component_Size (T, Uint_0);
5722 Set_Is_Constrained (T, False);
5723 Set_First_Index (T, First (Subtype_Marks (Def)));
5724 Set_Has_Delayed_Freeze (T, True);
5725 Propagate_Concurrent_Flags (T, Element_Type);
5726 Set_Has_Controlled_Component (T, Has_Controlled_Component
5727 (Element_Type)
5728 or else
5729 Is_Controlled_Active (Element_Type));
5730 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5731 (Element_Type));
5732 Set_Default_SSO (T);
5733 end if;
5734
5735 -- Common attributes for both cases
5736
5737 Set_Component_Type (Base_Type (T), Element_Type);
5738 Set_Packed_Array_Impl_Type (T, Empty);
5739
5740 if Aliased_Present (Component_Definition (Def)) then
5741 Check_SPARK_05_Restriction
5742 ("aliased is not allowed", Component_Definition (Def));
5743 Set_Has_Aliased_Components (Etype (T));
5744 end if;
5745
5746 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5747 -- array type to ensure that objects of this type are initialized.
5748
5749 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5750 Set_Can_Never_Be_Null (T);
5751
5752 if Null_Exclusion_Present (Component_Definition (Def))
5753
5754 -- No need to check itypes because in their case this check was
5755 -- done at their point of creation
5756
5757 and then not Is_Itype (Element_Type)
5758 then
5759 Error_Msg_N
5760 ("`NOT NULL` not allowed (null already excluded)",
5761 Subtype_Indication (Component_Definition (Def)));
5762 end if;
5763 end if;
5764
5765 Priv := Private_Component (Element_Type);
5766
5767 if Present (Priv) then
5768
5769 -- Check for circular definitions
5770
5771 if Priv = Any_Type then
5772 Set_Component_Type (Etype (T), Any_Type);
5773
5774 -- There is a gap in the visibility of operations on the composite
5775 -- type only if the component type is defined in a different scope.
5776
5777 elsif Scope (Priv) = Current_Scope then
5778 null;
5779
5780 elsif Is_Limited_Type (Priv) then
5781 Set_Is_Limited_Composite (Etype (T));
5782 Set_Is_Limited_Composite (T);
5783 else
5784 Set_Is_Private_Composite (Etype (T));
5785 Set_Is_Private_Composite (T);
5786 end if;
5787 end if;
5788
5789 -- A syntax error in the declaration itself may lead to an empty index
5790 -- list, in which case do a minimal patch.
5791
5792 if No (First_Index (T)) then
5793 Error_Msg_N ("missing index definition in array type declaration", T);
5794
5795 declare
5796 Indexes : constant List_Id :=
5797 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5798 begin
5799 Set_Discrete_Subtype_Definitions (Def, Indexes);
5800 Set_First_Index (T, First (Indexes));
5801 return;
5802 end;
5803 end if;
5804
5805 -- Create a concatenation operator for the new type. Internal array
5806 -- types created for packed entities do not need such, they are
5807 -- compatible with the user-defined type.
5808
5809 if Number_Dimensions (T) = 1
5810 and then not Is_Packed_Array_Impl_Type (T)
5811 then
5812 New_Concatenation_Op (T);
5813 end if;
5814
5815 -- In the case of an unconstrained array the parser has already verified
5816 -- that all the indexes are unconstrained but we still need to make sure
5817 -- that the element type is constrained.
5818
5819 if not Is_Definite_Subtype (Element_Type) then
5820 Error_Msg_N
5821 ("unconstrained element type in array declaration",
5822 Subtype_Indication (Component_Def));
5823
5824 elsif Is_Abstract_Type (Element_Type) then
5825 Error_Msg_N
5826 ("the type of a component cannot be abstract",
5827 Subtype_Indication (Component_Def));
5828 end if;
5829
5830 -- There may be an invariant declared for the component type, but
5831 -- the construction of the component invariant checking procedure
5832 -- takes place during expansion.
5833 end Array_Type_Declaration;
5834
5835 ------------------------------------------------------
5836 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5837 ------------------------------------------------------
5838
5839 function Replace_Anonymous_Access_To_Protected_Subprogram
5840 (N : Node_Id) return Entity_Id
5841 is
5842 Loc : constant Source_Ptr := Sloc (N);
5843
5844 Curr_Scope : constant Scope_Stack_Entry :=
5845 Scope_Stack.Table (Scope_Stack.Last);
5846
5847 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5848
5849 Acc : Node_Id;
5850 -- Access definition in declaration
5851
5852 Comp : Node_Id;
5853 -- Object definition or formal definition with an access definition
5854
5855 Decl : Node_Id;
5856 -- Declaration of anonymous access to subprogram type
5857
5858 Spec : Node_Id;
5859 -- Original specification in access to subprogram
5860
5861 P : Node_Id;
5862
5863 begin
5864 Set_Is_Internal (Anon);
5865
5866 case Nkind (N) is
5867 when N_Component_Declaration |
5868 N_Unconstrained_Array_Definition |
5869 N_Constrained_Array_Definition =>
5870 Comp := Component_Definition (N);
5871 Acc := Access_Definition (Comp);
5872
5873 when N_Discriminant_Specification =>
5874 Comp := Discriminant_Type (N);
5875 Acc := Comp;
5876
5877 when N_Parameter_Specification =>
5878 Comp := Parameter_Type (N);
5879 Acc := Comp;
5880
5881 when N_Access_Function_Definition =>
5882 Comp := Result_Definition (N);
5883 Acc := Comp;
5884
5885 when N_Object_Declaration =>
5886 Comp := Object_Definition (N);
5887 Acc := Comp;
5888
5889 when N_Function_Specification =>
5890 Comp := Result_Definition (N);
5891 Acc := Comp;
5892
5893 when others =>
5894 raise Program_Error;
5895 end case;
5896
5897 Spec := Access_To_Subprogram_Definition (Acc);
5898
5899 Decl :=
5900 Make_Full_Type_Declaration (Loc,
5901 Defining_Identifier => Anon,
5902 Type_Definition => Copy_Separate_Tree (Spec));
5903
5904 Mark_Rewrite_Insertion (Decl);
5905
5906 -- In ASIS mode, analyze the profile on the original node, because
5907 -- the separate copy does not provide enough links to recover the
5908 -- original tree. Analysis is limited to type annotations, within
5909 -- a temporary scope that serves as an anonymous subprogram to collect
5910 -- otherwise useless temporaries and itypes.
5911
5912 if ASIS_Mode then
5913 declare
5914 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5915
5916 begin
5917 if Nkind (Spec) = N_Access_Function_Definition then
5918 Set_Ekind (Typ, E_Function);
5919 else
5920 Set_Ekind (Typ, E_Procedure);
5921 end if;
5922
5923 Set_Parent (Typ, N);
5924 Set_Scope (Typ, Current_Scope);
5925 Push_Scope (Typ);
5926
5927 -- Nothing to do if procedure is parameterless
5928
5929 if Present (Parameter_Specifications (Spec)) then
5930 Process_Formals (Parameter_Specifications (Spec), Spec);
5931 end if;
5932
5933 if Nkind (Spec) = N_Access_Function_Definition then
5934 declare
5935 Def : constant Node_Id := Result_Definition (Spec);
5936
5937 begin
5938 -- The result might itself be an anonymous access type, so
5939 -- have to recurse.
5940
5941 if Nkind (Def) = N_Access_Definition then
5942 if Present (Access_To_Subprogram_Definition (Def)) then
5943 Set_Etype
5944 (Def,
5945 Replace_Anonymous_Access_To_Protected_Subprogram
5946 (Spec));
5947 else
5948 Find_Type (Subtype_Mark (Def));
5949 end if;
5950
5951 else
5952 Find_Type (Def);
5953 end if;
5954 end;
5955 end if;
5956
5957 End_Scope;
5958 end;
5959 end if;
5960
5961 -- Insert the new declaration in the nearest enclosing scope. If the
5962 -- parent is a body and N is its return type, the declaration belongs
5963 -- in the enclosing scope. Likewise if N is the type of a parameter.
5964
5965 P := Parent (N);
5966
5967 if Nkind (N) = N_Function_Specification
5968 and then Nkind (P) = N_Subprogram_Body
5969 then
5970 P := Parent (P);
5971 elsif Nkind (N) = N_Parameter_Specification
5972 and then Nkind (P) in N_Subprogram_Specification
5973 and then Nkind (Parent (P)) = N_Subprogram_Body
5974 then
5975 P := Parent (Parent (P));
5976 end if;
5977
5978 while Present (P) and then not Has_Declarations (P) loop
5979 P := Parent (P);
5980 end loop;
5981
5982 pragma Assert (Present (P));
5983
5984 if Nkind (P) = N_Package_Specification then
5985 Prepend (Decl, Visible_Declarations (P));
5986 else
5987 Prepend (Decl, Declarations (P));
5988 end if;
5989
5990 -- Replace the anonymous type with an occurrence of the new declaration.
5991 -- In all cases the rewritten node does not have the null-exclusion
5992 -- attribute because (if present) it was already inherited by the
5993 -- anonymous entity (Anon). Thus, in case of components we do not
5994 -- inherit this attribute.
5995
5996 if Nkind (N) = N_Parameter_Specification then
5997 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5998 Set_Etype (Defining_Identifier (N), Anon);
5999 Set_Null_Exclusion_Present (N, False);
6000
6001 elsif Nkind (N) = N_Object_Declaration then
6002 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6003 Set_Etype (Defining_Identifier (N), Anon);
6004
6005 elsif Nkind (N) = N_Access_Function_Definition then
6006 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6007
6008 elsif Nkind (N) = N_Function_Specification then
6009 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6010 Set_Etype (Defining_Unit_Name (N), Anon);
6011
6012 else
6013 Rewrite (Comp,
6014 Make_Component_Definition (Loc,
6015 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6016 end if;
6017
6018 Mark_Rewrite_Insertion (Comp);
6019
6020 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6021 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6022 and then not Is_Type (Current_Scope))
6023 then
6024
6025 -- Declaration can be analyzed in the current scope.
6026
6027 Analyze (Decl);
6028
6029 else
6030 -- Temporarily remove the current scope (record or subprogram) from
6031 -- the stack to add the new declarations to the enclosing scope.
6032 -- The anonymous entity is an Itype with the proper attributes.
6033
6034 Scope_Stack.Decrement_Last;
6035 Analyze (Decl);
6036 Set_Is_Itype (Anon);
6037 Set_Associated_Node_For_Itype (Anon, N);
6038 Scope_Stack.Append (Curr_Scope);
6039 end if;
6040
6041 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6042 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6043 return Anon;
6044 end Replace_Anonymous_Access_To_Protected_Subprogram;
6045
6046 -------------------------------
6047 -- Build_Derived_Access_Type --
6048 -------------------------------
6049
6050 procedure Build_Derived_Access_Type
6051 (N : Node_Id;
6052 Parent_Type : Entity_Id;
6053 Derived_Type : Entity_Id)
6054 is
6055 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6056
6057 Desig_Type : Entity_Id;
6058 Discr : Entity_Id;
6059 Discr_Con_Elist : Elist_Id;
6060 Discr_Con_El : Elmt_Id;
6061 Subt : Entity_Id;
6062
6063 begin
6064 -- Set the designated type so it is available in case this is an access
6065 -- to a self-referential type, e.g. a standard list type with a next
6066 -- pointer. Will be reset after subtype is built.
6067
6068 Set_Directly_Designated_Type
6069 (Derived_Type, Designated_Type (Parent_Type));
6070
6071 Subt := Process_Subtype (S, N);
6072
6073 if Nkind (S) /= N_Subtype_Indication
6074 and then Subt /= Base_Type (Subt)
6075 then
6076 Set_Ekind (Derived_Type, E_Access_Subtype);
6077 end if;
6078
6079 if Ekind (Derived_Type) = E_Access_Subtype then
6080 declare
6081 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6082 Ibase : constant Entity_Id :=
6083 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6084 Svg_Chars : constant Name_Id := Chars (Ibase);
6085 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6086
6087 begin
6088 Copy_Node (Pbase, Ibase);
6089
6090 -- Restore Itype status after Copy_Node
6091
6092 Set_Is_Itype (Ibase);
6093 Set_Associated_Node_For_Itype (Ibase, N);
6094
6095 Set_Chars (Ibase, Svg_Chars);
6096 Set_Next_Entity (Ibase, Svg_Next_E);
6097 Set_Sloc (Ibase, Sloc (Derived_Type));
6098 Set_Scope (Ibase, Scope (Derived_Type));
6099 Set_Freeze_Node (Ibase, Empty);
6100 Set_Is_Frozen (Ibase, False);
6101 Set_Comes_From_Source (Ibase, False);
6102 Set_Is_First_Subtype (Ibase, False);
6103
6104 Set_Etype (Ibase, Pbase);
6105 Set_Etype (Derived_Type, Ibase);
6106 end;
6107 end if;
6108
6109 Set_Directly_Designated_Type
6110 (Derived_Type, Designated_Type (Subt));
6111
6112 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6113 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6114 Set_Size_Info (Derived_Type, Parent_Type);
6115 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6116 Set_Depends_On_Private (Derived_Type,
6117 Has_Private_Component (Derived_Type));
6118 Conditional_Delay (Derived_Type, Subt);
6119
6120 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6121 -- that it is not redundant.
6122
6123 if Null_Exclusion_Present (Type_Definition (N)) then
6124 Set_Can_Never_Be_Null (Derived_Type);
6125
6126 elsif Can_Never_Be_Null (Parent_Type) then
6127 Set_Can_Never_Be_Null (Derived_Type);
6128 end if;
6129
6130 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6131 -- the root type for this information.
6132
6133 -- Apply range checks to discriminants for derived record case
6134 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6135
6136 Desig_Type := Designated_Type (Derived_Type);
6137
6138 if Is_Composite_Type (Desig_Type)
6139 and then (not Is_Array_Type (Desig_Type))
6140 and then Has_Discriminants (Desig_Type)
6141 and then Base_Type (Desig_Type) /= Desig_Type
6142 then
6143 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6144 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6145
6146 Discr := First_Discriminant (Base_Type (Desig_Type));
6147 while Present (Discr_Con_El) loop
6148 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6149 Next_Elmt (Discr_Con_El);
6150 Next_Discriminant (Discr);
6151 end loop;
6152 end if;
6153 end Build_Derived_Access_Type;
6154
6155 ------------------------------
6156 -- Build_Derived_Array_Type --
6157 ------------------------------
6158
6159 procedure Build_Derived_Array_Type
6160 (N : Node_Id;
6161 Parent_Type : Entity_Id;
6162 Derived_Type : Entity_Id)
6163 is
6164 Loc : constant Source_Ptr := Sloc (N);
6165 Tdef : constant Node_Id := Type_Definition (N);
6166 Indic : constant Node_Id := Subtype_Indication (Tdef);
6167 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6168 Implicit_Base : Entity_Id;
6169 New_Indic : Node_Id;
6170
6171 procedure Make_Implicit_Base;
6172 -- If the parent subtype is constrained, the derived type is a subtype
6173 -- of an implicit base type derived from the parent base.
6174
6175 ------------------------
6176 -- Make_Implicit_Base --
6177 ------------------------
6178
6179 procedure Make_Implicit_Base is
6180 begin
6181 Implicit_Base :=
6182 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6183
6184 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6185 Set_Etype (Implicit_Base, Parent_Base);
6186
6187 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6188 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6189
6190 Set_Has_Delayed_Freeze (Implicit_Base, True);
6191
6192 -- Inherit the "ghostness" from the parent base type
6193
6194 if Ghost_Mode > None or else Is_Ghost_Entity (Parent_Base) then
6195 Set_Is_Ghost_Entity (Implicit_Base);
6196 end if;
6197 end Make_Implicit_Base;
6198
6199 -- Start of processing for Build_Derived_Array_Type
6200
6201 begin
6202 if not Is_Constrained (Parent_Type) then
6203 if Nkind (Indic) /= N_Subtype_Indication then
6204 Set_Ekind (Derived_Type, E_Array_Type);
6205
6206 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6207 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6208
6209 Set_Has_Delayed_Freeze (Derived_Type, True);
6210
6211 else
6212 Make_Implicit_Base;
6213 Set_Etype (Derived_Type, Implicit_Base);
6214
6215 New_Indic :=
6216 Make_Subtype_Declaration (Loc,
6217 Defining_Identifier => Derived_Type,
6218 Subtype_Indication =>
6219 Make_Subtype_Indication (Loc,
6220 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6221 Constraint => Constraint (Indic)));
6222
6223 Rewrite (N, New_Indic);
6224 Analyze (N);
6225 end if;
6226
6227 else
6228 if Nkind (Indic) /= N_Subtype_Indication then
6229 Make_Implicit_Base;
6230
6231 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6232 Set_Etype (Derived_Type, Implicit_Base);
6233 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6234
6235 else
6236 Error_Msg_N ("illegal constraint on constrained type", Indic);
6237 end if;
6238 end if;
6239
6240 -- If parent type is not a derived type itself, and is declared in
6241 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6242 -- the new type's concatenation operator since Derive_Subprograms
6243 -- will not inherit the parent's operator. If the parent type is
6244 -- unconstrained, the operator is of the unconstrained base type.
6245
6246 if Number_Dimensions (Parent_Type) = 1
6247 and then not Is_Limited_Type (Parent_Type)
6248 and then not Is_Derived_Type (Parent_Type)
6249 and then not Is_Package_Or_Generic_Package
6250 (Scope (Base_Type (Parent_Type)))
6251 then
6252 if not Is_Constrained (Parent_Type)
6253 and then Is_Constrained (Derived_Type)
6254 then
6255 New_Concatenation_Op (Implicit_Base);
6256 else
6257 New_Concatenation_Op (Derived_Type);
6258 end if;
6259 end if;
6260 end Build_Derived_Array_Type;
6261
6262 -----------------------------------
6263 -- Build_Derived_Concurrent_Type --
6264 -----------------------------------
6265
6266 procedure Build_Derived_Concurrent_Type
6267 (N : Node_Id;
6268 Parent_Type : Entity_Id;
6269 Derived_Type : Entity_Id)
6270 is
6271 Loc : constant Source_Ptr := Sloc (N);
6272
6273 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6274 Corr_Decl : Node_Id;
6275 Corr_Decl_Needed : Boolean;
6276 -- If the derived type has fewer discriminants than its parent, the
6277 -- corresponding record is also a derived type, in order to account for
6278 -- the bound discriminants. We create a full type declaration for it in
6279 -- this case.
6280
6281 Constraint_Present : constant Boolean :=
6282 Nkind (Subtype_Indication (Type_Definition (N))) =
6283 N_Subtype_Indication;
6284
6285 D_Constraint : Node_Id;
6286 New_Constraint : Elist_Id;
6287 Old_Disc : Entity_Id;
6288 New_Disc : Entity_Id;
6289 New_N : Node_Id;
6290
6291 begin
6292 Set_Stored_Constraint (Derived_Type, No_Elist);
6293 Corr_Decl_Needed := False;
6294 Old_Disc := Empty;
6295
6296 if Present (Discriminant_Specifications (N))
6297 and then Constraint_Present
6298 then
6299 Old_Disc := First_Discriminant (Parent_Type);
6300 New_Disc := First (Discriminant_Specifications (N));
6301 while Present (New_Disc) and then Present (Old_Disc) loop
6302 Next_Discriminant (Old_Disc);
6303 Next (New_Disc);
6304 end loop;
6305 end if;
6306
6307 if Present (Old_Disc) and then Expander_Active then
6308
6309 -- The new type has fewer discriminants, so we need to create a new
6310 -- corresponding record, which is derived from the corresponding
6311 -- record of the parent, and has a stored constraint that captures
6312 -- the values of the discriminant constraints. The corresponding
6313 -- record is needed only if expander is active and code generation is
6314 -- enabled.
6315
6316 -- The type declaration for the derived corresponding record has the
6317 -- same discriminant part and constraints as the current declaration.
6318 -- Copy the unanalyzed tree to build declaration.
6319
6320 Corr_Decl_Needed := True;
6321 New_N := Copy_Separate_Tree (N);
6322
6323 Corr_Decl :=
6324 Make_Full_Type_Declaration (Loc,
6325 Defining_Identifier => Corr_Record,
6326 Discriminant_Specifications =>
6327 Discriminant_Specifications (New_N),
6328 Type_Definition =>
6329 Make_Derived_Type_Definition (Loc,
6330 Subtype_Indication =>
6331 Make_Subtype_Indication (Loc,
6332 Subtype_Mark =>
6333 New_Occurrence_Of
6334 (Corresponding_Record_Type (Parent_Type), Loc),
6335 Constraint =>
6336 Constraint
6337 (Subtype_Indication (Type_Definition (New_N))))));
6338 end if;
6339
6340 -- Copy Storage_Size and Relative_Deadline variables if task case
6341
6342 if Is_Task_Type (Parent_Type) then
6343 Set_Storage_Size_Variable (Derived_Type,
6344 Storage_Size_Variable (Parent_Type));
6345 Set_Relative_Deadline_Variable (Derived_Type,
6346 Relative_Deadline_Variable (Parent_Type));
6347 end if;
6348
6349 if Present (Discriminant_Specifications (N)) then
6350 Push_Scope (Derived_Type);
6351 Check_Or_Process_Discriminants (N, Derived_Type);
6352
6353 if Constraint_Present then
6354 New_Constraint :=
6355 Expand_To_Stored_Constraint
6356 (Parent_Type,
6357 Build_Discriminant_Constraints
6358 (Parent_Type,
6359 Subtype_Indication (Type_Definition (N)), True));
6360 end if;
6361
6362 End_Scope;
6363
6364 elsif Constraint_Present then
6365
6366 -- Build constrained subtype, copying the constraint, and derive
6367 -- from it to create a derived constrained type.
6368
6369 declare
6370 Loc : constant Source_Ptr := Sloc (N);
6371 Anon : constant Entity_Id :=
6372 Make_Defining_Identifier (Loc,
6373 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6374 Decl : Node_Id;
6375
6376 begin
6377 Decl :=
6378 Make_Subtype_Declaration (Loc,
6379 Defining_Identifier => Anon,
6380 Subtype_Indication =>
6381 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6382 Insert_Before (N, Decl);
6383 Analyze (Decl);
6384
6385 Rewrite (Subtype_Indication (Type_Definition (N)),
6386 New_Occurrence_Of (Anon, Loc));
6387 Set_Analyzed (Derived_Type, False);
6388 Analyze (N);
6389 return;
6390 end;
6391 end if;
6392
6393 -- By default, operations and private data are inherited from parent.
6394 -- However, in the presence of bound discriminants, a new corresponding
6395 -- record will be created, see below.
6396
6397 Set_Has_Discriminants
6398 (Derived_Type, Has_Discriminants (Parent_Type));
6399 Set_Corresponding_Record_Type
6400 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6401
6402 -- Is_Constrained is set according the parent subtype, but is set to
6403 -- False if the derived type is declared with new discriminants.
6404
6405 Set_Is_Constrained
6406 (Derived_Type,
6407 (Is_Constrained (Parent_Type) or else Constraint_Present)
6408 and then not Present (Discriminant_Specifications (N)));
6409
6410 if Constraint_Present then
6411 if not Has_Discriminants (Parent_Type) then
6412 Error_Msg_N ("untagged parent must have discriminants", N);
6413
6414 elsif Present (Discriminant_Specifications (N)) then
6415
6416 -- Verify that new discriminants are used to constrain old ones
6417
6418 D_Constraint :=
6419 First
6420 (Constraints
6421 (Constraint (Subtype_Indication (Type_Definition (N)))));
6422
6423 Old_Disc := First_Discriminant (Parent_Type);
6424
6425 while Present (D_Constraint) loop
6426 if Nkind (D_Constraint) /= N_Discriminant_Association then
6427
6428 -- Positional constraint. If it is a reference to a new
6429 -- discriminant, it constrains the corresponding old one.
6430
6431 if Nkind (D_Constraint) = N_Identifier then
6432 New_Disc := First_Discriminant (Derived_Type);
6433 while Present (New_Disc) loop
6434 exit when Chars (New_Disc) = Chars (D_Constraint);
6435 Next_Discriminant (New_Disc);
6436 end loop;
6437
6438 if Present (New_Disc) then
6439 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6440 end if;
6441 end if;
6442
6443 Next_Discriminant (Old_Disc);
6444
6445 -- if this is a named constraint, search by name for the old
6446 -- discriminants constrained by the new one.
6447
6448 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6449
6450 -- Find new discriminant with that name
6451
6452 New_Disc := First_Discriminant (Derived_Type);
6453 while Present (New_Disc) loop
6454 exit when
6455 Chars (New_Disc) = Chars (Expression (D_Constraint));
6456 Next_Discriminant (New_Disc);
6457 end loop;
6458
6459 if Present (New_Disc) then
6460
6461 -- Verify that new discriminant renames some discriminant
6462 -- of the parent type, and associate the new discriminant
6463 -- with one or more old ones that it renames.
6464
6465 declare
6466 Selector : Node_Id;
6467
6468 begin
6469 Selector := First (Selector_Names (D_Constraint));
6470 while Present (Selector) loop
6471 Old_Disc := First_Discriminant (Parent_Type);
6472 while Present (Old_Disc) loop
6473 exit when Chars (Old_Disc) = Chars (Selector);
6474 Next_Discriminant (Old_Disc);
6475 end loop;
6476
6477 if Present (Old_Disc) then
6478 Set_Corresponding_Discriminant
6479 (New_Disc, Old_Disc);
6480 end if;
6481
6482 Next (Selector);
6483 end loop;
6484 end;
6485 end if;
6486 end if;
6487
6488 Next (D_Constraint);
6489 end loop;
6490
6491 New_Disc := First_Discriminant (Derived_Type);
6492 while Present (New_Disc) loop
6493 if No (Corresponding_Discriminant (New_Disc)) then
6494 Error_Msg_NE
6495 ("new discriminant& must constrain old one", N, New_Disc);
6496
6497 elsif not
6498 Subtypes_Statically_Compatible
6499 (Etype (New_Disc),
6500 Etype (Corresponding_Discriminant (New_Disc)))
6501 then
6502 Error_Msg_NE
6503 ("& not statically compatible with parent discriminant",
6504 N, New_Disc);
6505 end if;
6506
6507 Next_Discriminant (New_Disc);
6508 end loop;
6509 end if;
6510
6511 elsif Present (Discriminant_Specifications (N)) then
6512 Error_Msg_N
6513 ("missing discriminant constraint in untagged derivation", N);
6514 end if;
6515
6516 -- The entity chain of the derived type includes the new discriminants
6517 -- but shares operations with the parent.
6518
6519 if Present (Discriminant_Specifications (N)) then
6520 Old_Disc := First_Discriminant (Parent_Type);
6521 while Present (Old_Disc) loop
6522 if No (Next_Entity (Old_Disc))
6523 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6524 then
6525 Set_Next_Entity
6526 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6527 exit;
6528 end if;
6529
6530 Next_Discriminant (Old_Disc);
6531 end loop;
6532
6533 else
6534 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6535 if Has_Discriminants (Parent_Type) then
6536 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6537 Set_Discriminant_Constraint (
6538 Derived_Type, Discriminant_Constraint (Parent_Type));
6539 end if;
6540 end if;
6541
6542 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6543
6544 Set_Has_Completion (Derived_Type);
6545
6546 if Corr_Decl_Needed then
6547 Set_Stored_Constraint (Derived_Type, New_Constraint);
6548 Insert_After (N, Corr_Decl);
6549 Analyze (Corr_Decl);
6550 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6551 end if;
6552 end Build_Derived_Concurrent_Type;
6553
6554 ------------------------------------
6555 -- Build_Derived_Enumeration_Type --
6556 ------------------------------------
6557
6558 procedure Build_Derived_Enumeration_Type
6559 (N : Node_Id;
6560 Parent_Type : Entity_Id;
6561 Derived_Type : Entity_Id)
6562 is
6563 Loc : constant Source_Ptr := Sloc (N);
6564 Def : constant Node_Id := Type_Definition (N);
6565 Indic : constant Node_Id := Subtype_Indication (Def);
6566 Implicit_Base : Entity_Id;
6567 Literal : Entity_Id;
6568 New_Lit : Entity_Id;
6569 Literals_List : List_Id;
6570 Type_Decl : Node_Id;
6571 Hi, Lo : Node_Id;
6572 Rang_Expr : Node_Id;
6573
6574 begin
6575 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6576 -- not have explicit literals lists we need to process types derived
6577 -- from them specially. This is handled by Derived_Standard_Character.
6578 -- If the parent type is a generic type, there are no literals either,
6579 -- and we construct the same skeletal representation as for the generic
6580 -- parent type.
6581
6582 if Is_Standard_Character_Type (Parent_Type) then
6583 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6584
6585 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6586 declare
6587 Lo : Node_Id;
6588 Hi : Node_Id;
6589
6590 begin
6591 if Nkind (Indic) /= N_Subtype_Indication then
6592 Lo :=
6593 Make_Attribute_Reference (Loc,
6594 Attribute_Name => Name_First,
6595 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6596 Set_Etype (Lo, Derived_Type);
6597
6598 Hi :=
6599 Make_Attribute_Reference (Loc,
6600 Attribute_Name => Name_Last,
6601 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6602 Set_Etype (Hi, Derived_Type);
6603
6604 Set_Scalar_Range (Derived_Type,
6605 Make_Range (Loc,
6606 Low_Bound => Lo,
6607 High_Bound => Hi));
6608 else
6609
6610 -- Analyze subtype indication and verify compatibility
6611 -- with parent type.
6612
6613 if Base_Type (Process_Subtype (Indic, N)) /=
6614 Base_Type (Parent_Type)
6615 then
6616 Error_Msg_N
6617 ("illegal constraint for formal discrete type", N);
6618 end if;
6619 end if;
6620 end;
6621
6622 else
6623 -- If a constraint is present, analyze the bounds to catch
6624 -- premature usage of the derived literals.
6625
6626 if Nkind (Indic) = N_Subtype_Indication
6627 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6628 then
6629 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6630 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6631 end if;
6632
6633 -- Introduce an implicit base type for the derived type even if there
6634 -- is no constraint attached to it, since this seems closer to the
6635 -- Ada semantics. Build a full type declaration tree for the derived
6636 -- type using the implicit base type as the defining identifier. The
6637 -- build a subtype declaration tree which applies the constraint (if
6638 -- any) have it replace the derived type declaration.
6639
6640 Literal := First_Literal (Parent_Type);
6641 Literals_List := New_List;
6642 while Present (Literal)
6643 and then Ekind (Literal) = E_Enumeration_Literal
6644 loop
6645 -- Literals of the derived type have the same representation as
6646 -- those of the parent type, but this representation can be
6647 -- overridden by an explicit representation clause. Indicate
6648 -- that there is no explicit representation given yet. These
6649 -- derived literals are implicit operations of the new type,
6650 -- and can be overridden by explicit ones.
6651
6652 if Nkind (Literal) = N_Defining_Character_Literal then
6653 New_Lit :=
6654 Make_Defining_Character_Literal (Loc, Chars (Literal));
6655 else
6656 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6657 end if;
6658
6659 Set_Ekind (New_Lit, E_Enumeration_Literal);
6660 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6661 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6662 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6663 Set_Alias (New_Lit, Literal);
6664 Set_Is_Known_Valid (New_Lit, True);
6665
6666 Append (New_Lit, Literals_List);
6667 Next_Literal (Literal);
6668 end loop;
6669
6670 Implicit_Base :=
6671 Make_Defining_Identifier (Sloc (Derived_Type),
6672 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6673
6674 -- Indicate the proper nature of the derived type. This must be done
6675 -- before analysis of the literals, to recognize cases when a literal
6676 -- may be hidden by a previous explicit function definition (cf.
6677 -- c83031a).
6678
6679 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6680 Set_Etype (Derived_Type, Implicit_Base);
6681
6682 Type_Decl :=
6683 Make_Full_Type_Declaration (Loc,
6684 Defining_Identifier => Implicit_Base,
6685 Discriminant_Specifications => No_List,
6686 Type_Definition =>
6687 Make_Enumeration_Type_Definition (Loc, Literals_List));
6688
6689 Mark_Rewrite_Insertion (Type_Decl);
6690 Insert_Before (N, Type_Decl);
6691 Analyze (Type_Decl);
6692
6693 -- The anonymous base now has a full declaration, but this base
6694 -- is not a first subtype.
6695
6696 Set_Is_First_Subtype (Implicit_Base, False);
6697
6698 -- After the implicit base is analyzed its Etype needs to be changed
6699 -- to reflect the fact that it is derived from the parent type which
6700 -- was ignored during analysis. We also set the size at this point.
6701
6702 Set_Etype (Implicit_Base, Parent_Type);
6703
6704 Set_Size_Info (Implicit_Base, Parent_Type);
6705 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6706 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6707
6708 -- Copy other flags from parent type
6709
6710 Set_Has_Non_Standard_Rep
6711 (Implicit_Base, Has_Non_Standard_Rep
6712 (Parent_Type));
6713 Set_Has_Pragma_Ordered
6714 (Implicit_Base, Has_Pragma_Ordered
6715 (Parent_Type));
6716 Set_Has_Delayed_Freeze (Implicit_Base);
6717
6718 -- Process the subtype indication including a validation check on the
6719 -- constraint, if any. If a constraint is given, its bounds must be
6720 -- implicitly converted to the new type.
6721
6722 if Nkind (Indic) = N_Subtype_Indication then
6723 declare
6724 R : constant Node_Id :=
6725 Range_Expression (Constraint (Indic));
6726
6727 begin
6728 if Nkind (R) = N_Range then
6729 Hi := Build_Scalar_Bound
6730 (High_Bound (R), Parent_Type, Implicit_Base);
6731 Lo := Build_Scalar_Bound
6732 (Low_Bound (R), Parent_Type, Implicit_Base);
6733
6734 else
6735 -- Constraint is a Range attribute. Replace with explicit
6736 -- mention of the bounds of the prefix, which must be a
6737 -- subtype.
6738
6739 Analyze (Prefix (R));
6740 Hi :=
6741 Convert_To (Implicit_Base,
6742 Make_Attribute_Reference (Loc,
6743 Attribute_Name => Name_Last,
6744 Prefix =>
6745 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6746
6747 Lo :=
6748 Convert_To (Implicit_Base,
6749 Make_Attribute_Reference (Loc,
6750 Attribute_Name => Name_First,
6751 Prefix =>
6752 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6753 end if;
6754 end;
6755
6756 else
6757 Hi :=
6758 Build_Scalar_Bound
6759 (Type_High_Bound (Parent_Type),
6760 Parent_Type, Implicit_Base);
6761 Lo :=
6762 Build_Scalar_Bound
6763 (Type_Low_Bound (Parent_Type),
6764 Parent_Type, Implicit_Base);
6765 end if;
6766
6767 Rang_Expr :=
6768 Make_Range (Loc,
6769 Low_Bound => Lo,
6770 High_Bound => Hi);
6771
6772 -- If we constructed a default range for the case where no range
6773 -- was given, then the expressions in the range must not freeze
6774 -- since they do not correspond to expressions in the source.
6775
6776 if Nkind (Indic) /= N_Subtype_Indication then
6777 Set_Must_Not_Freeze (Lo);
6778 Set_Must_Not_Freeze (Hi);
6779 Set_Must_Not_Freeze (Rang_Expr);
6780 end if;
6781
6782 Rewrite (N,
6783 Make_Subtype_Declaration (Loc,
6784 Defining_Identifier => Derived_Type,
6785 Subtype_Indication =>
6786 Make_Subtype_Indication (Loc,
6787 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6788 Constraint =>
6789 Make_Range_Constraint (Loc,
6790 Range_Expression => Rang_Expr))));
6791
6792 Analyze (N);
6793
6794 -- Propagate the aspects from the original type declaration to the
6795 -- declaration of the implicit base.
6796
6797 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6798
6799 -- Apply a range check. Since this range expression doesn't have an
6800 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6801 -- this right???
6802
6803 if Nkind (Indic) = N_Subtype_Indication then
6804 Apply_Range_Check
6805 (Range_Expression (Constraint (Indic)), Parent_Type,
6806 Source_Typ => Entity (Subtype_Mark (Indic)));
6807 end if;
6808 end if;
6809 end Build_Derived_Enumeration_Type;
6810
6811 --------------------------------
6812 -- Build_Derived_Numeric_Type --
6813 --------------------------------
6814
6815 procedure Build_Derived_Numeric_Type
6816 (N : Node_Id;
6817 Parent_Type : Entity_Id;
6818 Derived_Type : Entity_Id)
6819 is
6820 Loc : constant Source_Ptr := Sloc (N);
6821 Tdef : constant Node_Id := Type_Definition (N);
6822 Indic : constant Node_Id := Subtype_Indication (Tdef);
6823 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6824 No_Constraint : constant Boolean := Nkind (Indic) /=
6825 N_Subtype_Indication;
6826 Implicit_Base : Entity_Id;
6827
6828 Lo : Node_Id;
6829 Hi : Node_Id;
6830
6831 begin
6832 -- Process the subtype indication including a validation check on
6833 -- the constraint if any.
6834
6835 Discard_Node (Process_Subtype (Indic, N));
6836
6837 -- Introduce an implicit base type for the derived type even if there
6838 -- is no constraint attached to it, since this seems closer to the Ada
6839 -- semantics.
6840
6841 Implicit_Base :=
6842 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6843
6844 Set_Etype (Implicit_Base, Parent_Base);
6845 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6846 Set_Size_Info (Implicit_Base, Parent_Base);
6847 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6848 Set_Parent (Implicit_Base, Parent (Derived_Type));
6849 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6850
6851 -- Set RM Size for discrete type or decimal fixed-point type
6852 -- Ordinary fixed-point is excluded, why???
6853
6854 if Is_Discrete_Type (Parent_Base)
6855 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6856 then
6857 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6858 end if;
6859
6860 Set_Has_Delayed_Freeze (Implicit_Base);
6861
6862 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6863 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6864
6865 Set_Scalar_Range (Implicit_Base,
6866 Make_Range (Loc,
6867 Low_Bound => Lo,
6868 High_Bound => Hi));
6869
6870 if Has_Infinities (Parent_Base) then
6871 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6872 end if;
6873
6874 -- The Derived_Type, which is the entity of the declaration, is a
6875 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6876 -- absence of an explicit constraint.
6877
6878 Set_Etype (Derived_Type, Implicit_Base);
6879
6880 -- If we did not have a constraint, then the Ekind is set from the
6881 -- parent type (otherwise Process_Subtype has set the bounds)
6882
6883 if No_Constraint then
6884 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6885 end if;
6886
6887 -- If we did not have a range constraint, then set the range from the
6888 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6889
6890 if No_Constraint or else not Has_Range_Constraint (Indic) then
6891 Set_Scalar_Range (Derived_Type,
6892 Make_Range (Loc,
6893 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6894 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6895 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6896
6897 if Has_Infinities (Parent_Type) then
6898 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6899 end if;
6900
6901 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6902 end if;
6903
6904 Set_Is_Descendant_Of_Address (Derived_Type,
6905 Is_Descendant_Of_Address (Parent_Type));
6906 Set_Is_Descendant_Of_Address (Implicit_Base,
6907 Is_Descendant_Of_Address (Parent_Type));
6908
6909 -- Set remaining type-specific fields, depending on numeric type
6910
6911 if Is_Modular_Integer_Type (Parent_Type) then
6912 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6913
6914 Set_Non_Binary_Modulus
6915 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6916
6917 Set_Is_Known_Valid
6918 (Implicit_Base, Is_Known_Valid (Parent_Base));
6919
6920 elsif Is_Floating_Point_Type (Parent_Type) then
6921
6922 -- Digits of base type is always copied from the digits value of
6923 -- the parent base type, but the digits of the derived type will
6924 -- already have been set if there was a constraint present.
6925
6926 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6927 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6928
6929 if No_Constraint then
6930 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6931 end if;
6932
6933 elsif Is_Fixed_Point_Type (Parent_Type) then
6934
6935 -- Small of base type and derived type are always copied from the
6936 -- parent base type, since smalls never change. The delta of the
6937 -- base type is also copied from the parent base type. However the
6938 -- delta of the derived type will have been set already if a
6939 -- constraint was present.
6940
6941 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6942 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6943 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6944
6945 if No_Constraint then
6946 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6947 end if;
6948
6949 -- The scale and machine radix in the decimal case are always
6950 -- copied from the parent base type.
6951
6952 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6953 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6954 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6955
6956 Set_Machine_Radix_10
6957 (Derived_Type, Machine_Radix_10 (Parent_Base));
6958 Set_Machine_Radix_10
6959 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6960
6961 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6962
6963 if No_Constraint then
6964 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6965
6966 else
6967 -- the analysis of the subtype_indication sets the
6968 -- digits value of the derived type.
6969
6970 null;
6971 end if;
6972 end if;
6973 end if;
6974
6975 if Is_Integer_Type (Parent_Type) then
6976 Set_Has_Shift_Operator
6977 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6978 end if;
6979
6980 -- The type of the bounds is that of the parent type, and they
6981 -- must be converted to the derived type.
6982
6983 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6984
6985 -- The implicit_base should be frozen when the derived type is frozen,
6986 -- but note that it is used in the conversions of the bounds. For fixed
6987 -- types we delay the determination of the bounds until the proper
6988 -- freezing point. For other numeric types this is rejected by GCC, for
6989 -- reasons that are currently unclear (???), so we choose to freeze the
6990 -- implicit base now. In the case of integers and floating point types
6991 -- this is harmless because subsequent representation clauses cannot
6992 -- affect anything, but it is still baffling that we cannot use the
6993 -- same mechanism for all derived numeric types.
6994
6995 -- There is a further complication: actually some representation
6996 -- clauses can affect the implicit base type. For example, attribute
6997 -- definition clauses for stream-oriented attributes need to set the
6998 -- corresponding TSS entries on the base type, and this normally
6999 -- cannot be done after the base type is frozen, so the circuitry in
7000 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7001 -- and not use Set_TSS in this case.
7002
7003 -- There are also consequences for the case of delayed representation
7004 -- aspects for some cases. For example, a Size aspect is delayed and
7005 -- should not be evaluated to the freeze point. This early freezing
7006 -- means that the size attribute evaluation happens too early???
7007
7008 if Is_Fixed_Point_Type (Parent_Type) then
7009 Conditional_Delay (Implicit_Base, Parent_Type);
7010 else
7011 Freeze_Before (N, Implicit_Base);
7012 end if;
7013 end Build_Derived_Numeric_Type;
7014
7015 --------------------------------
7016 -- Build_Derived_Private_Type --
7017 --------------------------------
7018
7019 procedure Build_Derived_Private_Type
7020 (N : Node_Id;
7021 Parent_Type : Entity_Id;
7022 Derived_Type : Entity_Id;
7023 Is_Completion : Boolean;
7024 Derive_Subps : Boolean := True)
7025 is
7026 Loc : constant Source_Ptr := Sloc (N);
7027 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7028 Par_Scope : constant Entity_Id := Scope (Par_Base);
7029 Full_N : constant Node_Id := New_Copy_Tree (N);
7030 Full_Der : Entity_Id := New_Copy (Derived_Type);
7031 Full_P : Entity_Id;
7032
7033 procedure Build_Full_Derivation;
7034 -- Build full derivation, i.e. derive from the full view
7035
7036 procedure Copy_And_Build;
7037 -- Copy derived type declaration, replace parent with its full view,
7038 -- and build derivation
7039
7040 ---------------------------
7041 -- Build_Full_Derivation --
7042 ---------------------------
7043
7044 procedure Build_Full_Derivation is
7045 begin
7046 -- If parent scope is not open, install the declarations
7047
7048 if not In_Open_Scopes (Par_Scope) then
7049 Install_Private_Declarations (Par_Scope);
7050 Install_Visible_Declarations (Par_Scope);
7051 Copy_And_Build;
7052 Uninstall_Declarations (Par_Scope);
7053
7054 -- If parent scope is open and in another unit, and parent has a
7055 -- completion, then the derivation is taking place in the visible
7056 -- part of a child unit. In that case retrieve the full view of
7057 -- the parent momentarily.
7058
7059 elsif not In_Same_Source_Unit (N, Parent_Type) then
7060 Full_P := Full_View (Parent_Type);
7061 Exchange_Declarations (Parent_Type);
7062 Copy_And_Build;
7063 Exchange_Declarations (Full_P);
7064
7065 -- Otherwise it is a local derivation
7066
7067 else
7068 Copy_And_Build;
7069 end if;
7070 end Build_Full_Derivation;
7071
7072 --------------------
7073 -- Copy_And_Build --
7074 --------------------
7075
7076 procedure Copy_And_Build is
7077 Full_Parent : Entity_Id := Parent_Type;
7078
7079 begin
7080 -- If the parent is itself derived from another private type,
7081 -- installing the private declarations has not affected its
7082 -- privacy status, so use its own full view explicitly.
7083
7084 if Is_Private_Type (Full_Parent)
7085 and then Present (Full_View (Full_Parent))
7086 then
7087 Full_Parent := Full_View (Full_Parent);
7088 end if;
7089
7090 -- And its underlying full view if necessary
7091
7092 if Is_Private_Type (Full_Parent)
7093 and then Present (Underlying_Full_View (Full_Parent))
7094 then
7095 Full_Parent := Underlying_Full_View (Full_Parent);
7096 end if;
7097
7098 -- For record, access and most enumeration types, derivation from
7099 -- the full view requires a fully-fledged declaration. In the other
7100 -- cases, just use an itype.
7101
7102 if Ekind (Full_Parent) in Record_Kind
7103 or else Ekind (Full_Parent) in Access_Kind
7104 or else
7105 (Ekind (Full_Parent) in Enumeration_Kind
7106 and then not Is_Standard_Character_Type (Full_Parent)
7107 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7108 then
7109 -- Copy and adjust declaration to provide a completion for what
7110 -- is originally a private declaration. Indicate that full view
7111 -- is internally generated.
7112
7113 Set_Comes_From_Source (Full_N, False);
7114 Set_Comes_From_Source (Full_Der, False);
7115 Set_Parent (Full_Der, Full_N);
7116 Set_Defining_Identifier (Full_N, Full_Der);
7117
7118 -- If there are no constraints, adjust the subtype mark
7119
7120 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7121 N_Subtype_Indication
7122 then
7123 Set_Subtype_Indication
7124 (Type_Definition (Full_N),
7125 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7126 end if;
7127
7128 Insert_After (N, Full_N);
7129
7130 -- Build full view of derived type from full view of parent which
7131 -- is now installed. Subprograms have been derived on the partial
7132 -- view, the completion does not derive them anew.
7133
7134 if Ekind (Full_Parent) in Record_Kind then
7135
7136 -- If parent type is tagged, the completion inherits the proper
7137 -- primitive operations.
7138
7139 if Is_Tagged_Type (Parent_Type) then
7140 Build_Derived_Record_Type
7141 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7142 else
7143 Build_Derived_Record_Type
7144 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7145 end if;
7146
7147 else
7148 Build_Derived_Type
7149 (Full_N, Full_Parent, Full_Der,
7150 Is_Completion => False, Derive_Subps => False);
7151 end if;
7152
7153 -- The full declaration has been introduced into the tree and
7154 -- processed in the step above. It should not be analyzed again
7155 -- (when encountered later in the current list of declarations)
7156 -- to prevent spurious name conflicts. The full entity remains
7157 -- invisible.
7158
7159 Set_Analyzed (Full_N);
7160
7161 else
7162 Full_Der :=
7163 Make_Defining_Identifier (Sloc (Derived_Type),
7164 Chars => Chars (Derived_Type));
7165 Set_Is_Itype (Full_Der);
7166 Set_Associated_Node_For_Itype (Full_Der, N);
7167 Set_Parent (Full_Der, N);
7168 Build_Derived_Type
7169 (N, Full_Parent, Full_Der,
7170 Is_Completion => False, Derive_Subps => False);
7171 end if;
7172
7173 Set_Has_Private_Declaration (Full_Der);
7174 Set_Has_Private_Declaration (Derived_Type);
7175
7176 Set_Scope (Full_Der, Scope (Derived_Type));
7177 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7178 Set_Has_Size_Clause (Full_Der, False);
7179 Set_Has_Alignment_Clause (Full_Der, False);
7180 Set_Has_Delayed_Freeze (Full_Der);
7181 Set_Is_Frozen (Full_Der, False);
7182 Set_Freeze_Node (Full_Der, Empty);
7183 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7184 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7185
7186 -- The convention on the base type may be set in the private part
7187 -- and not propagated to the subtype until later, so we obtain the
7188 -- convention from the base type of the parent.
7189
7190 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7191 end Copy_And_Build;
7192
7193 -- Start of processing for Build_Derived_Private_Type
7194
7195 begin
7196 if Is_Tagged_Type (Parent_Type) then
7197 Full_P := Full_View (Parent_Type);
7198
7199 -- A type extension of a type with unknown discriminants is an
7200 -- indefinite type that the back-end cannot handle directly.
7201 -- We treat it as a private type, and build a completion that is
7202 -- derived from the full view of the parent, and hopefully has
7203 -- known discriminants.
7204
7205 -- If the full view of the parent type has an underlying record view,
7206 -- use it to generate the underlying record view of this derived type
7207 -- (required for chains of derivations with unknown discriminants).
7208
7209 -- Minor optimization: we avoid the generation of useless underlying
7210 -- record view entities if the private type declaration has unknown
7211 -- discriminants but its corresponding full view has no
7212 -- discriminants.
7213
7214 if Has_Unknown_Discriminants (Parent_Type)
7215 and then Present (Full_P)
7216 and then (Has_Discriminants (Full_P)
7217 or else Present (Underlying_Record_View (Full_P)))
7218 and then not In_Open_Scopes (Par_Scope)
7219 and then Expander_Active
7220 then
7221 declare
7222 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7223 New_Ext : constant Node_Id :=
7224 Copy_Separate_Tree
7225 (Record_Extension_Part (Type_Definition (N)));
7226 Decl : Node_Id;
7227
7228 begin
7229 Build_Derived_Record_Type
7230 (N, Parent_Type, Derived_Type, Derive_Subps);
7231
7232 -- Build anonymous completion, as a derivation from the full
7233 -- view of the parent. This is not a completion in the usual
7234 -- sense, because the current type is not private.
7235
7236 Decl :=
7237 Make_Full_Type_Declaration (Loc,
7238 Defining_Identifier => Full_Der,
7239 Type_Definition =>
7240 Make_Derived_Type_Definition (Loc,
7241 Subtype_Indication =>
7242 New_Copy_Tree
7243 (Subtype_Indication (Type_Definition (N))),
7244 Record_Extension_Part => New_Ext));
7245
7246 -- If the parent type has an underlying record view, use it
7247 -- here to build the new underlying record view.
7248
7249 if Present (Underlying_Record_View (Full_P)) then
7250 pragma Assert
7251 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7252 = N_Identifier);
7253 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7254 Underlying_Record_View (Full_P));
7255 end if;
7256
7257 Install_Private_Declarations (Par_Scope);
7258 Install_Visible_Declarations (Par_Scope);
7259 Insert_Before (N, Decl);
7260
7261 -- Mark entity as an underlying record view before analysis,
7262 -- to avoid generating the list of its primitive operations
7263 -- (which is not really required for this entity) and thus
7264 -- prevent spurious errors associated with missing overriding
7265 -- of abstract primitives (overridden only for Derived_Type).
7266
7267 Set_Ekind (Full_Der, E_Record_Type);
7268 Set_Is_Underlying_Record_View (Full_Der);
7269 Set_Default_SSO (Full_Der);
7270
7271 Analyze (Decl);
7272
7273 pragma Assert (Has_Discriminants (Full_Der)
7274 and then not Has_Unknown_Discriminants (Full_Der));
7275
7276 Uninstall_Declarations (Par_Scope);
7277
7278 -- Freeze the underlying record view, to prevent generation of
7279 -- useless dispatching information, which is simply shared with
7280 -- the real derived type.
7281
7282 Set_Is_Frozen (Full_Der);
7283
7284 -- If the derived type has access discriminants, create
7285 -- references to their anonymous types now, to prevent
7286 -- back-end problems when their first use is in generated
7287 -- bodies of primitives.
7288
7289 declare
7290 E : Entity_Id;
7291
7292 begin
7293 E := First_Entity (Full_Der);
7294
7295 while Present (E) loop
7296 if Ekind (E) = E_Discriminant
7297 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7298 then
7299 Build_Itype_Reference (Etype (E), Decl);
7300 end if;
7301
7302 Next_Entity (E);
7303 end loop;
7304 end;
7305
7306 -- Set up links between real entity and underlying record view
7307
7308 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7309 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7310 end;
7311
7312 -- If discriminants are known, build derived record
7313
7314 else
7315 Build_Derived_Record_Type
7316 (N, Parent_Type, Derived_Type, Derive_Subps);
7317 end if;
7318
7319 return;
7320
7321 elsif Has_Discriminants (Parent_Type) then
7322
7323 -- Build partial view of derived type from partial view of parent.
7324 -- This must be done before building the full derivation because the
7325 -- second derivation will modify the discriminants of the first and
7326 -- the discriminants are chained with the rest of the components in
7327 -- the full derivation.
7328
7329 Build_Derived_Record_Type
7330 (N, Parent_Type, Derived_Type, Derive_Subps);
7331
7332 -- Build the full derivation if this is not the anonymous derived
7333 -- base type created by Build_Derived_Record_Type in the constrained
7334 -- case (see point 5. of its head comment) since we build it for the
7335 -- derived subtype. And skip it for protected types altogether, as
7336 -- gigi does not use these types directly.
7337
7338 if Present (Full_View (Parent_Type))
7339 and then not Is_Itype (Derived_Type)
7340 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7341 then
7342 declare
7343 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7344 Discr : Entity_Id;
7345 Last_Discr : Entity_Id;
7346
7347 begin
7348 -- If this is not a completion, construct the implicit full
7349 -- view by deriving from the full view of the parent type.
7350 -- But if this is a completion, the derived private type
7351 -- being built is a full view and the full derivation can
7352 -- only be its underlying full view.
7353
7354 Build_Full_Derivation;
7355
7356 if not Is_Completion then
7357 Set_Full_View (Derived_Type, Full_Der);
7358 else
7359 Set_Underlying_Full_View (Derived_Type, Full_Der);
7360 end if;
7361
7362 if not Is_Base_Type (Derived_Type) then
7363 Set_Full_View (Der_Base, Base_Type (Full_Der));
7364 end if;
7365
7366 -- Copy the discriminant list from full view to the partial
7367 -- view (base type and its subtype). Gigi requires that the
7368 -- partial and full views have the same discriminants.
7369
7370 -- Note that since the partial view points to discriminants
7371 -- in the full view, their scope will be that of the full
7372 -- view. This might cause some front end problems and need
7373 -- adjustment???
7374
7375 Discr := First_Discriminant (Base_Type (Full_Der));
7376 Set_First_Entity (Der_Base, Discr);
7377
7378 loop
7379 Last_Discr := Discr;
7380 Next_Discriminant (Discr);
7381 exit when No (Discr);
7382 end loop;
7383
7384 Set_Last_Entity (Der_Base, Last_Discr);
7385 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7386 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7387
7388 Set_Stored_Constraint
7389 (Full_Der, Stored_Constraint (Derived_Type));
7390 end;
7391 end if;
7392
7393 elsif Present (Full_View (Parent_Type))
7394 and then Has_Discriminants (Full_View (Parent_Type))
7395 then
7396 if Has_Unknown_Discriminants (Parent_Type)
7397 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7398 N_Subtype_Indication
7399 then
7400 Error_Msg_N
7401 ("cannot constrain type with unknown discriminants",
7402 Subtype_Indication (Type_Definition (N)));
7403 return;
7404 end if;
7405
7406 -- If this is not a completion, construct the implicit full view by
7407 -- deriving from the full view of the parent type. But if this is a
7408 -- completion, the derived private type being built is a full view
7409 -- and the full derivation can only be its underlying full view.
7410
7411 Build_Full_Derivation;
7412
7413 if not Is_Completion then
7414 Set_Full_View (Derived_Type, Full_Der);
7415 else
7416 Set_Underlying_Full_View (Derived_Type, Full_Der);
7417 end if;
7418
7419 -- In any case, the primitive operations are inherited from the
7420 -- parent type, not from the internal full view.
7421
7422 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7423
7424 if Derive_Subps then
7425 Derive_Subprograms (Parent_Type, Derived_Type);
7426 end if;
7427
7428 Set_Stored_Constraint (Derived_Type, No_Elist);
7429 Set_Is_Constrained
7430 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7431
7432 else
7433 -- Untagged type, No discriminants on either view
7434
7435 if Nkind (Subtype_Indication (Type_Definition (N))) =
7436 N_Subtype_Indication
7437 then
7438 Error_Msg_N
7439 ("illegal constraint on type without discriminants", N);
7440 end if;
7441
7442 if Present (Discriminant_Specifications (N))
7443 and then Present (Full_View (Parent_Type))
7444 and then not Is_Tagged_Type (Full_View (Parent_Type))
7445 then
7446 Error_Msg_N ("cannot add discriminants to untagged type", N);
7447 end if;
7448
7449 Set_Stored_Constraint (Derived_Type, No_Elist);
7450 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7451 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7452 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7453 (Parent_Type));
7454 Set_Has_Controlled_Component
7455 (Derived_Type, Has_Controlled_Component
7456 (Parent_Type));
7457
7458 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7459
7460 if not Is_Controlled_Active (Parent_Type) then
7461 Set_Finalize_Storage_Only
7462 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7463 end if;
7464
7465 -- If this is not a completion, construct the implicit full view by
7466 -- deriving from the full view of the parent type.
7467
7468 -- ??? If the parent is untagged private and its completion is
7469 -- tagged, this mechanism will not work because we cannot derive from
7470 -- the tagged full view unless we have an extension.
7471
7472 if Present (Full_View (Parent_Type))
7473 and then not Is_Tagged_Type (Full_View (Parent_Type))
7474 and then not Is_Completion
7475 then
7476 Build_Full_Derivation;
7477 Set_Full_View (Derived_Type, Full_Der);
7478 end if;
7479 end if;
7480
7481 Set_Has_Unknown_Discriminants (Derived_Type,
7482 Has_Unknown_Discriminants (Parent_Type));
7483
7484 if Is_Private_Type (Derived_Type) then
7485 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7486 end if;
7487
7488 -- If the parent base type is in scope, add the derived type to its
7489 -- list of private dependents, because its full view may become
7490 -- visible subsequently (in a nested private part, a body, or in a
7491 -- further child unit).
7492
7493 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7494 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7495
7496 -- Check for unusual case where a type completed by a private
7497 -- derivation occurs within a package nested in a child unit, and
7498 -- the parent is declared in an ancestor.
7499
7500 if Is_Child_Unit (Scope (Current_Scope))
7501 and then Is_Completion
7502 and then In_Private_Part (Current_Scope)
7503 and then Scope (Parent_Type) /= Current_Scope
7504
7505 -- Note that if the parent has a completion in the private part,
7506 -- (which is itself a derivation from some other private type)
7507 -- it is that completion that is visible, there is no full view
7508 -- available, and no special processing is needed.
7509
7510 and then Present (Full_View (Parent_Type))
7511 then
7512 -- In this case, the full view of the parent type will become
7513 -- visible in the body of the enclosing child, and only then will
7514 -- the current type be possibly non-private. Build an underlying
7515 -- full view that will be installed when the enclosing child body
7516 -- is compiled.
7517
7518 if Present (Underlying_Full_View (Derived_Type)) then
7519 Full_Der := Underlying_Full_View (Derived_Type);
7520 else
7521 Build_Full_Derivation;
7522 Set_Underlying_Full_View (Derived_Type, Full_Der);
7523 end if;
7524
7525 -- The full view will be used to swap entities on entry/exit to
7526 -- the body, and must appear in the entity list for the package.
7527
7528 Append_Entity (Full_Der, Scope (Derived_Type));
7529 end if;
7530 end if;
7531 end Build_Derived_Private_Type;
7532
7533 -------------------------------
7534 -- Build_Derived_Record_Type --
7535 -------------------------------
7536
7537 -- 1. INTRODUCTION
7538
7539 -- Ideally we would like to use the same model of type derivation for
7540 -- tagged and untagged record types. Unfortunately this is not quite
7541 -- possible because the semantics of representation clauses is different
7542 -- for tagged and untagged records under inheritance. Consider the
7543 -- following:
7544
7545 -- type R (...) is [tagged] record ... end record;
7546 -- type T (...) is new R (...) [with ...];
7547
7548 -- The representation clauses for T can specify a completely different
7549 -- record layout from R's. Hence the same component can be placed in two
7550 -- very different positions in objects of type T and R. If R and T are
7551 -- tagged types, representation clauses for T can only specify the layout
7552 -- of non inherited components, thus components that are common in R and T
7553 -- have the same position in objects of type R and T.
7554
7555 -- This has two implications. The first is that the entire tree for R's
7556 -- declaration needs to be copied for T in the untagged case, so that T
7557 -- can be viewed as a record type of its own with its own representation
7558 -- clauses. The second implication is the way we handle discriminants.
7559 -- Specifically, in the untagged case we need a way to communicate to Gigi
7560 -- what are the real discriminants in the record, while for the semantics
7561 -- we need to consider those introduced by the user to rename the
7562 -- discriminants in the parent type. This is handled by introducing the
7563 -- notion of stored discriminants. See below for more.
7564
7565 -- Fortunately the way regular components are inherited can be handled in
7566 -- the same way in tagged and untagged types.
7567
7568 -- To complicate things a bit more the private view of a private extension
7569 -- cannot be handled in the same way as the full view (for one thing the
7570 -- semantic rules are somewhat different). We will explain what differs
7571 -- below.
7572
7573 -- 2. DISCRIMINANTS UNDER INHERITANCE
7574
7575 -- The semantic rules governing the discriminants of derived types are
7576 -- quite subtle.
7577
7578 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7579 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7580
7581 -- If parent type has discriminants, then the discriminants that are
7582 -- declared in the derived type are [3.4 (11)]:
7583
7584 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7585 -- there is one;
7586
7587 -- o Otherwise, each discriminant of the parent type (implicitly declared
7588 -- in the same order with the same specifications). In this case, the
7589 -- discriminants are said to be "inherited", or if unknown in the parent
7590 -- are also unknown in the derived type.
7591
7592 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7593
7594 -- o The parent subtype must be constrained;
7595
7596 -- o If the parent type is not a tagged type, then each discriminant of
7597 -- the derived type must be used in the constraint defining a parent
7598 -- subtype. [Implementation note: This ensures that the new discriminant
7599 -- can share storage with an existing discriminant.]
7600
7601 -- For the derived type each discriminant of the parent type is either
7602 -- inherited, constrained to equal some new discriminant of the derived
7603 -- type, or constrained to the value of an expression.
7604
7605 -- When inherited or constrained to equal some new discriminant, the
7606 -- parent discriminant and the discriminant of the derived type are said
7607 -- to "correspond".
7608
7609 -- If a discriminant of the parent type is constrained to a specific value
7610 -- in the derived type definition, then the discriminant is said to be
7611 -- "specified" by that derived type definition.
7612
7613 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7614
7615 -- We have spoken about stored discriminants in point 1 (introduction)
7616 -- above. There are two sort of stored discriminants: implicit and
7617 -- explicit. As long as the derived type inherits the same discriminants as
7618 -- the root record type, stored discriminants are the same as regular
7619 -- discriminants, and are said to be implicit. However, if any discriminant
7620 -- in the root type was renamed in the derived type, then the derived
7621 -- type will contain explicit stored discriminants. Explicit stored
7622 -- discriminants are discriminants in addition to the semantically visible
7623 -- discriminants defined for the derived type. Stored discriminants are
7624 -- used by Gigi to figure out what are the physical discriminants in
7625 -- objects of the derived type (see precise definition in einfo.ads).
7626 -- As an example, consider the following:
7627
7628 -- type R (D1, D2, D3 : Int) is record ... end record;
7629 -- type T1 is new R;
7630 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7631 -- type T3 is new T2;
7632 -- type T4 (Y : Int) is new T3 (Y, 99);
7633
7634 -- The following table summarizes the discriminants and stored
7635 -- discriminants in R and T1 through T4.
7636
7637 -- Type Discrim Stored Discrim Comment
7638 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7639 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7640 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7641 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7642 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7643
7644 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7645 -- find the corresponding discriminant in the parent type, while
7646 -- Original_Record_Component (abbreviated ORC below), the actual physical
7647 -- component that is renamed. Finally the field Is_Completely_Hidden
7648 -- (abbreviated ICH below) is set for all explicit stored discriminants
7649 -- (see einfo.ads for more info). For the above example this gives:
7650
7651 -- Discrim CD ORC ICH
7652 -- ^^^^^^^ ^^ ^^^ ^^^
7653 -- D1 in R empty itself no
7654 -- D2 in R empty itself no
7655 -- D3 in R empty itself no
7656
7657 -- D1 in T1 D1 in R itself no
7658 -- D2 in T1 D2 in R itself no
7659 -- D3 in T1 D3 in R itself no
7660
7661 -- X1 in T2 D3 in T1 D3 in T2 no
7662 -- X2 in T2 D1 in T1 D1 in T2 no
7663 -- D1 in T2 empty itself yes
7664 -- D2 in T2 empty itself yes
7665 -- D3 in T2 empty itself yes
7666
7667 -- X1 in T3 X1 in T2 D3 in T3 no
7668 -- X2 in T3 X2 in T2 D1 in T3 no
7669 -- D1 in T3 empty itself yes
7670 -- D2 in T3 empty itself yes
7671 -- D3 in T3 empty itself yes
7672
7673 -- Y in T4 X1 in T3 D3 in T3 no
7674 -- D1 in T3 empty itself yes
7675 -- D2 in T3 empty itself yes
7676 -- D3 in T3 empty itself yes
7677
7678 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7679
7680 -- Type derivation for tagged types is fairly straightforward. If no
7681 -- discriminants are specified by the derived type, these are inherited
7682 -- from the parent. No explicit stored discriminants are ever necessary.
7683 -- The only manipulation that is done to the tree is that of adding a
7684 -- _parent field with parent type and constrained to the same constraint
7685 -- specified for the parent in the derived type definition. For instance:
7686
7687 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7688 -- type T1 is new R with null record;
7689 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7690
7691 -- are changed into:
7692
7693 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7694 -- _parent : R (D1, D2, D3);
7695 -- end record;
7696
7697 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7698 -- _parent : T1 (X2, 88, X1);
7699 -- end record;
7700
7701 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7702 -- ORC and ICH fields are:
7703
7704 -- Discrim CD ORC ICH
7705 -- ^^^^^^^ ^^ ^^^ ^^^
7706 -- D1 in R empty itself no
7707 -- D2 in R empty itself no
7708 -- D3 in R empty itself no
7709
7710 -- D1 in T1 D1 in R D1 in R no
7711 -- D2 in T1 D2 in R D2 in R no
7712 -- D3 in T1 D3 in R D3 in R no
7713
7714 -- X1 in T2 D3 in T1 D3 in R no
7715 -- X2 in T2 D1 in T1 D1 in R no
7716
7717 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7718 --
7719 -- Regardless of whether we dealing with a tagged or untagged type
7720 -- we will transform all derived type declarations of the form
7721 --
7722 -- type T is new R (...) [with ...];
7723 -- or
7724 -- subtype S is R (...);
7725 -- type T is new S [with ...];
7726 -- into
7727 -- type BT is new R [with ...];
7728 -- subtype T is BT (...);
7729 --
7730 -- That is, the base derived type is constrained only if it has no
7731 -- discriminants. The reason for doing this is that GNAT's semantic model
7732 -- assumes that a base type with discriminants is unconstrained.
7733 --
7734 -- Note that, strictly speaking, the above transformation is not always
7735 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7736 --
7737 -- procedure B34011A is
7738 -- type REC (D : integer := 0) is record
7739 -- I : Integer;
7740 -- end record;
7741
7742 -- package P is
7743 -- type T6 is new Rec;
7744 -- function F return T6;
7745 -- end P;
7746
7747 -- use P;
7748 -- package Q6 is
7749 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7750 -- end Q6;
7751 --
7752 -- The definition of Q6.U is illegal. However transforming Q6.U into
7753
7754 -- type BaseU is new T6;
7755 -- subtype U is BaseU (Q6.F.I)
7756
7757 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7758 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7759 -- the transformation described above.
7760
7761 -- There is another instance where the above transformation is incorrect.
7762 -- Consider:
7763
7764 -- package Pack is
7765 -- type Base (D : Integer) is tagged null record;
7766 -- procedure P (X : Base);
7767
7768 -- type Der is new Base (2) with null record;
7769 -- procedure P (X : Der);
7770 -- end Pack;
7771
7772 -- Then the above transformation turns this into
7773
7774 -- type Der_Base is new Base with null record;
7775 -- -- procedure P (X : Base) is implicitly inherited here
7776 -- -- as procedure P (X : Der_Base).
7777
7778 -- subtype Der is Der_Base (2);
7779 -- procedure P (X : Der);
7780 -- -- The overriding of P (X : Der_Base) is illegal since we
7781 -- -- have a parameter conformance problem.
7782
7783 -- To get around this problem, after having semantically processed Der_Base
7784 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7785 -- Discriminant_Constraint from Der so that when parameter conformance is
7786 -- checked when P is overridden, no semantic errors are flagged.
7787
7788 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7789
7790 -- Regardless of whether we are dealing with a tagged or untagged type
7791 -- we will transform all derived type declarations of the form
7792
7793 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7794 -- type T is new R [with ...];
7795 -- into
7796 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7797
7798 -- The reason for such transformation is that it allows us to implement a
7799 -- very clean form of component inheritance as explained below.
7800
7801 -- Note that this transformation is not achieved by direct tree rewriting
7802 -- and manipulation, but rather by redoing the semantic actions that the
7803 -- above transformation will entail. This is done directly in routine
7804 -- Inherit_Components.
7805
7806 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7807
7808 -- In both tagged and untagged derived types, regular non discriminant
7809 -- components are inherited in the derived type from the parent type. In
7810 -- the absence of discriminants component, inheritance is straightforward
7811 -- as components can simply be copied from the parent.
7812
7813 -- If the parent has discriminants, inheriting components constrained with
7814 -- these discriminants requires caution. Consider the following example:
7815
7816 -- type R (D1, D2 : Positive) is [tagged] record
7817 -- S : String (D1 .. D2);
7818 -- end record;
7819
7820 -- type T1 is new R [with null record];
7821 -- type T2 (X : positive) is new R (1, X) [with null record];
7822
7823 -- As explained in 6. above, T1 is rewritten as
7824 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7825 -- which makes the treatment for T1 and T2 identical.
7826
7827 -- What we want when inheriting S, is that references to D1 and D2 in R are
7828 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7829 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7830 -- with either discriminant references in the derived type or expressions.
7831 -- This replacement is achieved as follows: before inheriting R's
7832 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7833 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7834 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7835 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7836 -- by String (1 .. X).
7837
7838 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7839
7840 -- We explain here the rules governing private type extensions relevant to
7841 -- type derivation. These rules are explained on the following example:
7842
7843 -- type D [(...)] is new A [(...)] with private; <-- partial view
7844 -- type D [(...)] is new P [(...)] with null record; <-- full view
7845
7846 -- Type A is called the ancestor subtype of the private extension.
7847 -- Type P is the parent type of the full view of the private extension. It
7848 -- must be A or a type derived from A.
7849
7850 -- The rules concerning the discriminants of private type extensions are
7851 -- [7.3(10-13)]:
7852
7853 -- o If a private extension inherits known discriminants from the ancestor
7854 -- subtype, then the full view must also inherit its discriminants from
7855 -- the ancestor subtype and the parent subtype of the full view must be
7856 -- constrained if and only if the ancestor subtype is constrained.
7857
7858 -- o If a partial view has unknown discriminants, then the full view may
7859 -- define a definite or an indefinite subtype, with or without
7860 -- discriminants.
7861
7862 -- o If a partial view has neither known nor unknown discriminants, then
7863 -- the full view must define a definite subtype.
7864
7865 -- o If the ancestor subtype of a private extension has constrained
7866 -- discriminants, then the parent subtype of the full view must impose a
7867 -- statically matching constraint on those discriminants.
7868
7869 -- This means that only the following forms of private extensions are
7870 -- allowed:
7871
7872 -- type D is new A with private; <-- partial view
7873 -- type D is new P with null record; <-- full view
7874
7875 -- If A has no discriminants than P has no discriminants, otherwise P must
7876 -- inherit A's discriminants.
7877
7878 -- type D is new A (...) with private; <-- partial view
7879 -- type D is new P (:::) with null record; <-- full view
7880
7881 -- P must inherit A's discriminants and (...) and (:::) must statically
7882 -- match.
7883
7884 -- subtype A is R (...);
7885 -- type D is new A with private; <-- partial view
7886 -- type D is new P with null record; <-- full view
7887
7888 -- P must have inherited R's discriminants and must be derived from A or
7889 -- any of its subtypes.
7890
7891 -- type D (..) is new A with private; <-- partial view
7892 -- type D (..) is new P [(:::)] with null record; <-- full view
7893
7894 -- No specific constraints on P's discriminants or constraint (:::).
7895 -- Note that A can be unconstrained, but the parent subtype P must either
7896 -- be constrained or (:::) must be present.
7897
7898 -- type D (..) is new A [(...)] with private; <-- partial view
7899 -- type D (..) is new P [(:::)] with null record; <-- full view
7900
7901 -- P's constraints on A's discriminants must statically match those
7902 -- imposed by (...).
7903
7904 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7905
7906 -- The full view of a private extension is handled exactly as described
7907 -- above. The model chose for the private view of a private extension is
7908 -- the same for what concerns discriminants (i.e. they receive the same
7909 -- treatment as in the tagged case). However, the private view of the
7910 -- private extension always inherits the components of the parent base,
7911 -- without replacing any discriminant reference. Strictly speaking this is
7912 -- incorrect. However, Gigi never uses this view to generate code so this
7913 -- is a purely semantic issue. In theory, a set of transformations similar
7914 -- to those given in 5. and 6. above could be applied to private views of
7915 -- private extensions to have the same model of component inheritance as
7916 -- for non private extensions. However, this is not done because it would
7917 -- further complicate private type processing. Semantically speaking, this
7918 -- leaves us in an uncomfortable situation. As an example consider:
7919
7920 -- package Pack is
7921 -- type R (D : integer) is tagged record
7922 -- S : String (1 .. D);
7923 -- end record;
7924 -- procedure P (X : R);
7925 -- type T is new R (1) with private;
7926 -- private
7927 -- type T is new R (1) with null record;
7928 -- end;
7929
7930 -- This is transformed into:
7931
7932 -- package Pack is
7933 -- type R (D : integer) is tagged record
7934 -- S : String (1 .. D);
7935 -- end record;
7936 -- procedure P (X : R);
7937 -- type T is new R (1) with private;
7938 -- private
7939 -- type BaseT is new R with null record;
7940 -- subtype T is BaseT (1);
7941 -- end;
7942
7943 -- (strictly speaking the above is incorrect Ada)
7944
7945 -- From the semantic standpoint the private view of private extension T
7946 -- should be flagged as constrained since one can clearly have
7947 --
7948 -- Obj : T;
7949 --
7950 -- in a unit withing Pack. However, when deriving subprograms for the
7951 -- private view of private extension T, T must be seen as unconstrained
7952 -- since T has discriminants (this is a constraint of the current
7953 -- subprogram derivation model). Thus, when processing the private view of
7954 -- a private extension such as T, we first mark T as unconstrained, we
7955 -- process it, we perform program derivation and just before returning from
7956 -- Build_Derived_Record_Type we mark T as constrained.
7957
7958 -- ??? Are there are other uncomfortable cases that we will have to
7959 -- deal with.
7960
7961 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7962
7963 -- Types that are derived from a visible record type and have a private
7964 -- extension present other peculiarities. They behave mostly like private
7965 -- types, but if they have primitive operations defined, these will not
7966 -- have the proper signatures for further inheritance, because other
7967 -- primitive operations will use the implicit base that we define for
7968 -- private derivations below. This affect subprogram inheritance (see
7969 -- Derive_Subprograms for details). We also derive the implicit base from
7970 -- the base type of the full view, so that the implicit base is a record
7971 -- type and not another private type, This avoids infinite loops.
7972
7973 procedure Build_Derived_Record_Type
7974 (N : Node_Id;
7975 Parent_Type : Entity_Id;
7976 Derived_Type : Entity_Id;
7977 Derive_Subps : Boolean := True)
7978 is
7979 Discriminant_Specs : constant Boolean :=
7980 Present (Discriminant_Specifications (N));
7981 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7982 Loc : constant Source_Ptr := Sloc (N);
7983 Private_Extension : constant Boolean :=
7984 Nkind (N) = N_Private_Extension_Declaration;
7985 Assoc_List : Elist_Id;
7986 Constraint_Present : Boolean;
7987 Constrs : Elist_Id;
7988 Discrim : Entity_Id;
7989 Indic : Node_Id;
7990 Inherit_Discrims : Boolean := False;
7991 Last_Discrim : Entity_Id;
7992 New_Base : Entity_Id;
7993 New_Decl : Node_Id;
7994 New_Discrs : Elist_Id;
7995 New_Indic : Node_Id;
7996 Parent_Base : Entity_Id;
7997 Save_Etype : Entity_Id;
7998 Save_Discr_Constr : Elist_Id;
7999 Save_Next_Entity : Entity_Id;
8000 Type_Def : Node_Id;
8001
8002 Discs : Elist_Id := New_Elmt_List;
8003 -- An empty Discs list means that there were no constraints in the
8004 -- subtype indication or that there was an error processing it.
8005
8006 begin
8007 if Ekind (Parent_Type) = E_Record_Type_With_Private
8008 and then Present (Full_View (Parent_Type))
8009 and then Has_Discriminants (Parent_Type)
8010 then
8011 Parent_Base := Base_Type (Full_View (Parent_Type));
8012 else
8013 Parent_Base := Base_Type (Parent_Type);
8014 end if;
8015
8016 -- AI05-0115 : if this is a derivation from a private type in some
8017 -- other scope that may lead to invisible components for the derived
8018 -- type, mark it accordingly.
8019
8020 if Is_Private_Type (Parent_Type) then
8021 if Scope (Parent_Type) = Scope (Derived_Type) then
8022 null;
8023
8024 elsif In_Open_Scopes (Scope (Parent_Type))
8025 and then In_Private_Part (Scope (Parent_Type))
8026 then
8027 null;
8028
8029 else
8030 Set_Has_Private_Ancestor (Derived_Type);
8031 end if;
8032
8033 else
8034 Set_Has_Private_Ancestor
8035 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8036 end if;
8037
8038 -- Before we start the previously documented transformations, here is
8039 -- little fix for size and alignment of tagged types. Normally when we
8040 -- derive type D from type P, we copy the size and alignment of P as the
8041 -- default for D, and in the absence of explicit representation clauses
8042 -- for D, the size and alignment are indeed the same as the parent.
8043
8044 -- But this is wrong for tagged types, since fields may be added, and
8045 -- the default size may need to be larger, and the default alignment may
8046 -- need to be larger.
8047
8048 -- We therefore reset the size and alignment fields in the tagged case.
8049 -- Note that the size and alignment will in any case be at least as
8050 -- large as the parent type (since the derived type has a copy of the
8051 -- parent type in the _parent field)
8052
8053 -- The type is also marked as being tagged here, which is needed when
8054 -- processing components with a self-referential anonymous access type
8055 -- in the call to Check_Anonymous_Access_Components below. Note that
8056 -- this flag is also set later on for completeness.
8057
8058 if Is_Tagged then
8059 Set_Is_Tagged_Type (Derived_Type);
8060 Init_Size_Align (Derived_Type);
8061 end if;
8062
8063 -- STEP 0a: figure out what kind of derived type declaration we have
8064
8065 if Private_Extension then
8066 Type_Def := N;
8067 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8068 Set_Default_SSO (Derived_Type);
8069
8070 else
8071 Type_Def := Type_Definition (N);
8072
8073 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8074 -- Parent_Base can be a private type or private extension. However,
8075 -- for tagged types with an extension the newly added fields are
8076 -- visible and hence the Derived_Type is always an E_Record_Type.
8077 -- (except that the parent may have its own private fields).
8078 -- For untagged types we preserve the Ekind of the Parent_Base.
8079
8080 if Present (Record_Extension_Part (Type_Def)) then
8081 Set_Ekind (Derived_Type, E_Record_Type);
8082 Set_Default_SSO (Derived_Type);
8083
8084 -- Create internal access types for components with anonymous
8085 -- access types.
8086
8087 if Ada_Version >= Ada_2005 then
8088 Check_Anonymous_Access_Components
8089 (N, Derived_Type, Derived_Type,
8090 Component_List (Record_Extension_Part (Type_Def)));
8091 end if;
8092
8093 else
8094 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8095 end if;
8096 end if;
8097
8098 -- Indic can either be an N_Identifier if the subtype indication
8099 -- contains no constraint or an N_Subtype_Indication if the subtype
8100 -- indication has a constraint.
8101
8102 Indic := Subtype_Indication (Type_Def);
8103 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8104
8105 -- Check that the type has visible discriminants. The type may be
8106 -- a private type with unknown discriminants whose full view has
8107 -- discriminants which are invisible.
8108
8109 if Constraint_Present then
8110 if not Has_Discriminants (Parent_Base)
8111 or else
8112 (Has_Unknown_Discriminants (Parent_Base)
8113 and then Is_Private_Type (Parent_Base))
8114 then
8115 Error_Msg_N
8116 ("invalid constraint: type has no discriminant",
8117 Constraint (Indic));
8118
8119 Constraint_Present := False;
8120 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8121
8122 elsif Is_Constrained (Parent_Type) then
8123 Error_Msg_N
8124 ("invalid constraint: parent type is already constrained",
8125 Constraint (Indic));
8126
8127 Constraint_Present := False;
8128 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8129 end if;
8130 end if;
8131
8132 -- STEP 0b: If needed, apply transformation given in point 5. above
8133
8134 if not Private_Extension
8135 and then Has_Discriminants (Parent_Type)
8136 and then not Discriminant_Specs
8137 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8138 then
8139 -- First, we must analyze the constraint (see comment in point 5.)
8140 -- The constraint may come from the subtype indication of the full
8141 -- declaration.
8142
8143 if Constraint_Present then
8144 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8145
8146 -- If there is no explicit constraint, there might be one that is
8147 -- inherited from a constrained parent type. In that case verify that
8148 -- it conforms to the constraint in the partial view. In perverse
8149 -- cases the parent subtypes of the partial and full view can have
8150 -- different constraints.
8151
8152 elsif Present (Stored_Constraint (Parent_Type)) then
8153 New_Discrs := Stored_Constraint (Parent_Type);
8154
8155 else
8156 New_Discrs := No_Elist;
8157 end if;
8158
8159 if Has_Discriminants (Derived_Type)
8160 and then Has_Private_Declaration (Derived_Type)
8161 and then Present (Discriminant_Constraint (Derived_Type))
8162 and then Present (New_Discrs)
8163 then
8164 -- Verify that constraints of the full view statically match
8165 -- those given in the partial view.
8166
8167 declare
8168 C1, C2 : Elmt_Id;
8169
8170 begin
8171 C1 := First_Elmt (New_Discrs);
8172 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8173 while Present (C1) and then Present (C2) loop
8174 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8175 or else
8176 (Is_OK_Static_Expression (Node (C1))
8177 and then Is_OK_Static_Expression (Node (C2))
8178 and then
8179 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8180 then
8181 null;
8182
8183 else
8184 if Constraint_Present then
8185 Error_Msg_N
8186 ("constraint not conformant to previous declaration",
8187 Node (C1));
8188 else
8189 Error_Msg_N
8190 ("constraint of full view is incompatible "
8191 & "with partial view", N);
8192 end if;
8193 end if;
8194
8195 Next_Elmt (C1);
8196 Next_Elmt (C2);
8197 end loop;
8198 end;
8199 end if;
8200
8201 -- Insert and analyze the declaration for the unconstrained base type
8202
8203 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8204
8205 New_Decl :=
8206 Make_Full_Type_Declaration (Loc,
8207 Defining_Identifier => New_Base,
8208 Type_Definition =>
8209 Make_Derived_Type_Definition (Loc,
8210 Abstract_Present => Abstract_Present (Type_Def),
8211 Limited_Present => Limited_Present (Type_Def),
8212 Subtype_Indication =>
8213 New_Occurrence_Of (Parent_Base, Loc),
8214 Record_Extension_Part =>
8215 Relocate_Node (Record_Extension_Part (Type_Def)),
8216 Interface_List => Interface_List (Type_Def)));
8217
8218 Set_Parent (New_Decl, Parent (N));
8219 Mark_Rewrite_Insertion (New_Decl);
8220 Insert_Before (N, New_Decl);
8221
8222 -- In the extension case, make sure ancestor is frozen appropriately
8223 -- (see also non-discriminated case below).
8224
8225 if Present (Record_Extension_Part (Type_Def))
8226 or else Is_Interface (Parent_Base)
8227 then
8228 Freeze_Before (New_Decl, Parent_Type);
8229 end if;
8230
8231 -- Note that this call passes False for the Derive_Subps parameter
8232 -- because subprogram derivation is deferred until after creating
8233 -- the subtype (see below).
8234
8235 Build_Derived_Type
8236 (New_Decl, Parent_Base, New_Base,
8237 Is_Completion => False, Derive_Subps => False);
8238
8239 -- ??? This needs re-examination to determine whether the
8240 -- above call can simply be replaced by a call to Analyze.
8241
8242 Set_Analyzed (New_Decl);
8243
8244 -- Insert and analyze the declaration for the constrained subtype
8245
8246 if Constraint_Present then
8247 New_Indic :=
8248 Make_Subtype_Indication (Loc,
8249 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8250 Constraint => Relocate_Node (Constraint (Indic)));
8251
8252 else
8253 declare
8254 Constr_List : constant List_Id := New_List;
8255 C : Elmt_Id;
8256 Expr : Node_Id;
8257
8258 begin
8259 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8260 while Present (C) loop
8261 Expr := Node (C);
8262
8263 -- It is safe here to call New_Copy_Tree since we called
8264 -- Force_Evaluation on each constraint previously
8265 -- in Build_Discriminant_Constraints.
8266
8267 Append (New_Copy_Tree (Expr), To => Constr_List);
8268
8269 Next_Elmt (C);
8270 end loop;
8271
8272 New_Indic :=
8273 Make_Subtype_Indication (Loc,
8274 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8275 Constraint =>
8276 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8277 end;
8278 end if;
8279
8280 Rewrite (N,
8281 Make_Subtype_Declaration (Loc,
8282 Defining_Identifier => Derived_Type,
8283 Subtype_Indication => New_Indic));
8284
8285 Analyze (N);
8286
8287 -- Derivation of subprograms must be delayed until the full subtype
8288 -- has been established, to ensure proper overriding of subprograms
8289 -- inherited by full types. If the derivations occurred as part of
8290 -- the call to Build_Derived_Type above, then the check for type
8291 -- conformance would fail because earlier primitive subprograms
8292 -- could still refer to the full type prior the change to the new
8293 -- subtype and hence would not match the new base type created here.
8294 -- Subprograms are not derived, however, when Derive_Subps is False
8295 -- (since otherwise there could be redundant derivations).
8296
8297 if Derive_Subps then
8298 Derive_Subprograms (Parent_Type, Derived_Type);
8299 end if;
8300
8301 -- For tagged types the Discriminant_Constraint of the new base itype
8302 -- is inherited from the first subtype so that no subtype conformance
8303 -- problem arise when the first subtype overrides primitive
8304 -- operations inherited by the implicit base type.
8305
8306 if Is_Tagged then
8307 Set_Discriminant_Constraint
8308 (New_Base, Discriminant_Constraint (Derived_Type));
8309 end if;
8310
8311 return;
8312 end if;
8313
8314 -- If we get here Derived_Type will have no discriminants or it will be
8315 -- a discriminated unconstrained base type.
8316
8317 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8318
8319 if Is_Tagged then
8320
8321 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8322 -- The declaration of a specific descendant of an interface type
8323 -- freezes the interface type (RM 13.14).
8324
8325 if not Private_Extension or else Is_Interface (Parent_Base) then
8326 Freeze_Before (N, Parent_Type);
8327 end if;
8328
8329 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8330 -- cannot be declared at a deeper level than its parent type is
8331 -- removed. The check on derivation within a generic body is also
8332 -- relaxed, but there's a restriction that a derived tagged type
8333 -- cannot be declared in a generic body if it's derived directly
8334 -- or indirectly from a formal type of that generic.
8335
8336 if Ada_Version >= Ada_2005 then
8337 if Present (Enclosing_Generic_Body (Derived_Type)) then
8338 declare
8339 Ancestor_Type : Entity_Id;
8340
8341 begin
8342 -- Check to see if any ancestor of the derived type is a
8343 -- formal type.
8344
8345 Ancestor_Type := Parent_Type;
8346 while not Is_Generic_Type (Ancestor_Type)
8347 and then Etype (Ancestor_Type) /= Ancestor_Type
8348 loop
8349 Ancestor_Type := Etype (Ancestor_Type);
8350 end loop;
8351
8352 -- If the derived type does have a formal type as an
8353 -- ancestor, then it's an error if the derived type is
8354 -- declared within the body of the generic unit that
8355 -- declares the formal type in its generic formal part. It's
8356 -- sufficient to check whether the ancestor type is declared
8357 -- inside the same generic body as the derived type (such as
8358 -- within a nested generic spec), in which case the
8359 -- derivation is legal. If the formal type is declared
8360 -- outside of that generic body, then it's guaranteed that
8361 -- the derived type is declared within the generic body of
8362 -- the generic unit declaring the formal type.
8363
8364 if Is_Generic_Type (Ancestor_Type)
8365 and then Enclosing_Generic_Body (Ancestor_Type) /=
8366 Enclosing_Generic_Body (Derived_Type)
8367 then
8368 Error_Msg_NE
8369 ("parent type of& must not be descendant of formal type"
8370 & " of an enclosing generic body",
8371 Indic, Derived_Type);
8372 end if;
8373 end;
8374 end if;
8375
8376 elsif Type_Access_Level (Derived_Type) /=
8377 Type_Access_Level (Parent_Type)
8378 and then not Is_Generic_Type (Derived_Type)
8379 then
8380 if Is_Controlled (Parent_Type) then
8381 Error_Msg_N
8382 ("controlled type must be declared at the library level",
8383 Indic);
8384 else
8385 Error_Msg_N
8386 ("type extension at deeper accessibility level than parent",
8387 Indic);
8388 end if;
8389
8390 else
8391 declare
8392 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8393 begin
8394 if Present (GB)
8395 and then GB /= Enclosing_Generic_Body (Parent_Base)
8396 then
8397 Error_Msg_NE
8398 ("parent type of& must not be outside generic body"
8399 & " (RM 3.9.1(4))",
8400 Indic, Derived_Type);
8401 end if;
8402 end;
8403 end if;
8404 end if;
8405
8406 -- Ada 2005 (AI-251)
8407
8408 if Ada_Version >= Ada_2005 and then Is_Tagged then
8409
8410 -- "The declaration of a specific descendant of an interface type
8411 -- freezes the interface type" (RM 13.14).
8412
8413 declare
8414 Iface : Node_Id;
8415 begin
8416 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8417 Iface := First (Interface_List (Type_Def));
8418 while Present (Iface) loop
8419 Freeze_Before (N, Etype (Iface));
8420 Next (Iface);
8421 end loop;
8422 end if;
8423 end;
8424 end if;
8425
8426 -- STEP 1b : preliminary cleanup of the full view of private types
8427
8428 -- If the type is already marked as having discriminants, then it's the
8429 -- completion of a private type or private extension and we need to
8430 -- retain the discriminants from the partial view if the current
8431 -- declaration has Discriminant_Specifications so that we can verify
8432 -- conformance. However, we must remove any existing components that
8433 -- were inherited from the parent (and attached in Copy_And_Swap)
8434 -- because the full type inherits all appropriate components anyway, and
8435 -- we do not want the partial view's components interfering.
8436
8437 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8438 Discrim := First_Discriminant (Derived_Type);
8439 loop
8440 Last_Discrim := Discrim;
8441 Next_Discriminant (Discrim);
8442 exit when No (Discrim);
8443 end loop;
8444
8445 Set_Last_Entity (Derived_Type, Last_Discrim);
8446
8447 -- In all other cases wipe out the list of inherited components (even
8448 -- inherited discriminants), it will be properly rebuilt here.
8449
8450 else
8451 Set_First_Entity (Derived_Type, Empty);
8452 Set_Last_Entity (Derived_Type, Empty);
8453 end if;
8454
8455 -- STEP 1c: Initialize some flags for the Derived_Type
8456
8457 -- The following flags must be initialized here so that
8458 -- Process_Discriminants can check that discriminants of tagged types do
8459 -- not have a default initial value and that access discriminants are
8460 -- only specified for limited records. For completeness, these flags are
8461 -- also initialized along with all the other flags below.
8462
8463 -- AI-419: Limitedness is not inherited from an interface parent, so to
8464 -- be limited in that case the type must be explicitly declared as
8465 -- limited. However, task and protected interfaces are always limited.
8466
8467 if Limited_Present (Type_Def) then
8468 Set_Is_Limited_Record (Derived_Type);
8469
8470 elsif Is_Limited_Record (Parent_Type)
8471 or else (Present (Full_View (Parent_Type))
8472 and then Is_Limited_Record (Full_View (Parent_Type)))
8473 then
8474 if not Is_Interface (Parent_Type)
8475 or else Is_Synchronized_Interface (Parent_Type)
8476 or else Is_Protected_Interface (Parent_Type)
8477 or else Is_Task_Interface (Parent_Type)
8478 then
8479 Set_Is_Limited_Record (Derived_Type);
8480 end if;
8481 end if;
8482
8483 -- STEP 2a: process discriminants of derived type if any
8484
8485 Push_Scope (Derived_Type);
8486
8487 if Discriminant_Specs then
8488 Set_Has_Unknown_Discriminants (Derived_Type, False);
8489
8490 -- The following call initializes fields Has_Discriminants and
8491 -- Discriminant_Constraint, unless we are processing the completion
8492 -- of a private type declaration.
8493
8494 Check_Or_Process_Discriminants (N, Derived_Type);
8495
8496 -- For untagged types, the constraint on the Parent_Type must be
8497 -- present and is used to rename the discriminants.
8498
8499 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8500 Error_Msg_N ("untagged parent must have discriminants", Indic);
8501
8502 elsif not Is_Tagged and then not Constraint_Present then
8503 Error_Msg_N
8504 ("discriminant constraint needed for derived untagged records",
8505 Indic);
8506
8507 -- Otherwise the parent subtype must be constrained unless we have a
8508 -- private extension.
8509
8510 elsif not Constraint_Present
8511 and then not Private_Extension
8512 and then not Is_Constrained (Parent_Type)
8513 then
8514 Error_Msg_N
8515 ("unconstrained type not allowed in this context", Indic);
8516
8517 elsif Constraint_Present then
8518 -- The following call sets the field Corresponding_Discriminant
8519 -- for the discriminants in the Derived_Type.
8520
8521 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8522
8523 -- For untagged types all new discriminants must rename
8524 -- discriminants in the parent. For private extensions new
8525 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8526
8527 Discrim := First_Discriminant (Derived_Type);
8528 while Present (Discrim) loop
8529 if not Is_Tagged
8530 and then No (Corresponding_Discriminant (Discrim))
8531 then
8532 Error_Msg_N
8533 ("new discriminants must constrain old ones", Discrim);
8534
8535 elsif Private_Extension
8536 and then Present (Corresponding_Discriminant (Discrim))
8537 then
8538 Error_Msg_N
8539 ("only static constraints allowed for parent"
8540 & " discriminants in the partial view", Indic);
8541 exit;
8542 end if;
8543
8544 -- If a new discriminant is used in the constraint, then its
8545 -- subtype must be statically compatible with the parent
8546 -- discriminant's subtype (3.7(15)).
8547
8548 -- However, if the record contains an array constrained by
8549 -- the discriminant but with some different bound, the compiler
8550 -- attemps to create a smaller range for the discriminant type.
8551 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8552 -- the discriminant type is a scalar type, the check must use
8553 -- the original discriminant type in the parent declaration.
8554
8555 declare
8556 Corr_Disc : constant Entity_Id :=
8557 Corresponding_Discriminant (Discrim);
8558 Disc_Type : constant Entity_Id := Etype (Discrim);
8559 Corr_Type : Entity_Id;
8560
8561 begin
8562 if Present (Corr_Disc) then
8563 if Is_Scalar_Type (Disc_Type) then
8564 Corr_Type :=
8565 Entity (Discriminant_Type (Parent (Corr_Disc)));
8566 else
8567 Corr_Type := Etype (Corr_Disc);
8568 end if;
8569
8570 if not
8571 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8572 then
8573 Error_Msg_N
8574 ("subtype must be compatible "
8575 & "with parent discriminant",
8576 Discrim);
8577 end if;
8578 end if;
8579 end;
8580
8581 Next_Discriminant (Discrim);
8582 end loop;
8583
8584 -- Check whether the constraints of the full view statically
8585 -- match those imposed by the parent subtype [7.3(13)].
8586
8587 if Present (Stored_Constraint (Derived_Type)) then
8588 declare
8589 C1, C2 : Elmt_Id;
8590
8591 begin
8592 C1 := First_Elmt (Discs);
8593 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8594 while Present (C1) and then Present (C2) loop
8595 if not
8596 Fully_Conformant_Expressions (Node (C1), Node (C2))
8597 then
8598 Error_Msg_N
8599 ("not conformant with previous declaration",
8600 Node (C1));
8601 end if;
8602
8603 Next_Elmt (C1);
8604 Next_Elmt (C2);
8605 end loop;
8606 end;
8607 end if;
8608 end if;
8609
8610 -- STEP 2b: No new discriminants, inherit discriminants if any
8611
8612 else
8613 if Private_Extension then
8614 Set_Has_Unknown_Discriminants
8615 (Derived_Type,
8616 Has_Unknown_Discriminants (Parent_Type)
8617 or else Unknown_Discriminants_Present (N));
8618
8619 -- The partial view of the parent may have unknown discriminants,
8620 -- but if the full view has discriminants and the parent type is
8621 -- in scope they must be inherited.
8622
8623 elsif Has_Unknown_Discriminants (Parent_Type)
8624 and then
8625 (not Has_Discriminants (Parent_Type)
8626 or else not In_Open_Scopes (Scope (Parent_Type)))
8627 then
8628 Set_Has_Unknown_Discriminants (Derived_Type);
8629 end if;
8630
8631 if not Has_Unknown_Discriminants (Derived_Type)
8632 and then not Has_Unknown_Discriminants (Parent_Base)
8633 and then Has_Discriminants (Parent_Type)
8634 then
8635 Inherit_Discrims := True;
8636 Set_Has_Discriminants
8637 (Derived_Type, True);
8638 Set_Discriminant_Constraint
8639 (Derived_Type, Discriminant_Constraint (Parent_Base));
8640 end if;
8641
8642 -- The following test is true for private types (remember
8643 -- transformation 5. is not applied to those) and in an error
8644 -- situation.
8645
8646 if Constraint_Present then
8647 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8648 end if;
8649
8650 -- For now mark a new derived type as constrained only if it has no
8651 -- discriminants. At the end of Build_Derived_Record_Type we properly
8652 -- set this flag in the case of private extensions. See comments in
8653 -- point 9. just before body of Build_Derived_Record_Type.
8654
8655 Set_Is_Constrained
8656 (Derived_Type,
8657 not (Inherit_Discrims
8658 or else Has_Unknown_Discriminants (Derived_Type)));
8659 end if;
8660
8661 -- STEP 3: initialize fields of derived type
8662
8663 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8664 Set_Stored_Constraint (Derived_Type, No_Elist);
8665
8666 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8667 -- but cannot be interfaces
8668
8669 if not Private_Extension
8670 and then Ekind (Derived_Type) /= E_Private_Type
8671 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8672 then
8673 if Interface_Present (Type_Def) then
8674 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8675 end if;
8676
8677 Set_Interfaces (Derived_Type, No_Elist);
8678 end if;
8679
8680 -- Fields inherited from the Parent_Type
8681
8682 Set_Has_Specified_Layout
8683 (Derived_Type, Has_Specified_Layout (Parent_Type));
8684 Set_Is_Limited_Composite
8685 (Derived_Type, Is_Limited_Composite (Parent_Type));
8686 Set_Is_Private_Composite
8687 (Derived_Type, Is_Private_Composite (Parent_Type));
8688
8689 if Is_Tagged_Type (Parent_Type) then
8690 Set_No_Tagged_Streams_Pragma
8691 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8692 end if;
8693
8694 -- Fields inherited from the Parent_Base
8695
8696 Set_Has_Controlled_Component
8697 (Derived_Type, Has_Controlled_Component (Parent_Base));
8698 Set_Has_Non_Standard_Rep
8699 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8700 Set_Has_Primitive_Operations
8701 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8702
8703 -- Fields inherited from the Parent_Base in the non-private case
8704
8705 if Ekind (Derived_Type) = E_Record_Type then
8706 Set_Has_Complex_Representation
8707 (Derived_Type, Has_Complex_Representation (Parent_Base));
8708 end if;
8709
8710 -- Fields inherited from the Parent_Base for record types
8711
8712 if Is_Record_Type (Derived_Type) then
8713 declare
8714 Parent_Full : Entity_Id;
8715
8716 begin
8717 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8718 -- Parent_Base can be a private type or private extension. Go
8719 -- to the full view here to get the E_Record_Type specific flags.
8720
8721 if Present (Full_View (Parent_Base)) then
8722 Parent_Full := Full_View (Parent_Base);
8723 else
8724 Parent_Full := Parent_Base;
8725 end if;
8726
8727 Set_OK_To_Reorder_Components
8728 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8729 end;
8730 end if;
8731
8732 -- Set fields for private derived types
8733
8734 if Is_Private_Type (Derived_Type) then
8735 Set_Depends_On_Private (Derived_Type, True);
8736 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8737
8738 -- Inherit fields from non private record types. If this is the
8739 -- completion of a derivation from a private type, the parent itself
8740 -- is private, and the attributes come from its full view, which must
8741 -- be present.
8742
8743 else
8744 if Is_Private_Type (Parent_Base)
8745 and then not Is_Record_Type (Parent_Base)
8746 then
8747 Set_Component_Alignment
8748 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8749 Set_C_Pass_By_Copy
8750 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8751 else
8752 Set_Component_Alignment
8753 (Derived_Type, Component_Alignment (Parent_Base));
8754 Set_C_Pass_By_Copy
8755 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8756 end if;
8757 end if;
8758
8759 -- Set fields for tagged types
8760
8761 if Is_Tagged then
8762 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8763
8764 -- All tagged types defined in Ada.Finalization are controlled
8765
8766 if Chars (Scope (Derived_Type)) = Name_Finalization
8767 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8768 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8769 then
8770 Set_Is_Controlled (Derived_Type);
8771 else
8772 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8773 end if;
8774
8775 -- Minor optimization: there is no need to generate the class-wide
8776 -- entity associated with an underlying record view.
8777
8778 if not Is_Underlying_Record_View (Derived_Type) then
8779 Make_Class_Wide_Type (Derived_Type);
8780 end if;
8781
8782 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8783
8784 if Has_Discriminants (Derived_Type)
8785 and then Constraint_Present
8786 then
8787 Set_Stored_Constraint
8788 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8789 end if;
8790
8791 if Ada_Version >= Ada_2005 then
8792 declare
8793 Ifaces_List : Elist_Id;
8794
8795 begin
8796 -- Checks rules 3.9.4 (13/2 and 14/2)
8797
8798 if Comes_From_Source (Derived_Type)
8799 and then not Is_Private_Type (Derived_Type)
8800 and then Is_Interface (Parent_Type)
8801 and then not Is_Interface (Derived_Type)
8802 then
8803 if Is_Task_Interface (Parent_Type) then
8804 Error_Msg_N
8805 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8806 Derived_Type);
8807
8808 elsif Is_Protected_Interface (Parent_Type) then
8809 Error_Msg_N
8810 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8811 Derived_Type);
8812 end if;
8813 end if;
8814
8815 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8816
8817 Check_Interfaces (N, Type_Def);
8818
8819 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8820 -- not already in the parents.
8821
8822 Collect_Interfaces
8823 (T => Derived_Type,
8824 Ifaces_List => Ifaces_List,
8825 Exclude_Parents => True);
8826
8827 Set_Interfaces (Derived_Type, Ifaces_List);
8828
8829 -- If the derived type is the anonymous type created for
8830 -- a declaration whose parent has a constraint, propagate
8831 -- the interface list to the source type. This must be done
8832 -- prior to the completion of the analysis of the source type
8833 -- because the components in the extension may contain current
8834 -- instances whose legality depends on some ancestor.
8835
8836 if Is_Itype (Derived_Type) then
8837 declare
8838 Def : constant Node_Id :=
8839 Associated_Node_For_Itype (Derived_Type);
8840 begin
8841 if Present (Def)
8842 and then Nkind (Def) = N_Full_Type_Declaration
8843 then
8844 Set_Interfaces
8845 (Defining_Identifier (Def), Ifaces_List);
8846 end if;
8847 end;
8848 end if;
8849
8850 -- A type extension is automatically Ghost when one of its
8851 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8852 -- also inherited when the parent type is Ghost, but this is
8853 -- done in Build_Derived_Type as the mechanism also handles
8854 -- untagged derivations.
8855
8856 if Implements_Ghost_Interface (Derived_Type) then
8857 Set_Is_Ghost_Entity (Derived_Type);
8858 end if;
8859 end;
8860 end if;
8861
8862 else
8863 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8864 Set_Has_Non_Standard_Rep
8865 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8866 end if;
8867
8868 -- STEP 4: Inherit components from the parent base and constrain them.
8869 -- Apply the second transformation described in point 6. above.
8870
8871 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8872 or else not Has_Discriminants (Parent_Type)
8873 or else not Is_Constrained (Parent_Type)
8874 then
8875 Constrs := Discs;
8876 else
8877 Constrs := Discriminant_Constraint (Parent_Type);
8878 end if;
8879
8880 Assoc_List :=
8881 Inherit_Components
8882 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8883
8884 -- STEP 5a: Copy the parent record declaration for untagged types
8885
8886 if not Is_Tagged then
8887
8888 -- Discriminant_Constraint (Derived_Type) has been properly
8889 -- constructed. Save it and temporarily set it to Empty because we
8890 -- do not want the call to New_Copy_Tree below to mess this list.
8891
8892 if Has_Discriminants (Derived_Type) then
8893 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8894 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8895 else
8896 Save_Discr_Constr := No_Elist;
8897 end if;
8898
8899 -- Save the Etype field of Derived_Type. It is correctly set now,
8900 -- but the call to New_Copy tree may remap it to point to itself,
8901 -- which is not what we want. Ditto for the Next_Entity field.
8902
8903 Save_Etype := Etype (Derived_Type);
8904 Save_Next_Entity := Next_Entity (Derived_Type);
8905
8906 -- Assoc_List maps all stored discriminants in the Parent_Base to
8907 -- stored discriminants in the Derived_Type. It is fundamental that
8908 -- no types or itypes with discriminants other than the stored
8909 -- discriminants appear in the entities declared inside
8910 -- Derived_Type, since the back end cannot deal with it.
8911
8912 New_Decl :=
8913 New_Copy_Tree
8914 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8915
8916 -- Restore the fields saved prior to the New_Copy_Tree call
8917 -- and compute the stored constraint.
8918
8919 Set_Etype (Derived_Type, Save_Etype);
8920 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8921
8922 if Has_Discriminants (Derived_Type) then
8923 Set_Discriminant_Constraint
8924 (Derived_Type, Save_Discr_Constr);
8925 Set_Stored_Constraint
8926 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8927 Replace_Components (Derived_Type, New_Decl);
8928 Set_Has_Implicit_Dereference
8929 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8930 end if;
8931
8932 -- Insert the new derived type declaration
8933
8934 Rewrite (N, New_Decl);
8935
8936 -- STEP 5b: Complete the processing for record extensions in generics
8937
8938 -- There is no completion for record extensions declared in the
8939 -- parameter part of a generic, so we need to complete processing for
8940 -- these generic record extensions here. The Record_Type_Definition call
8941 -- will change the Ekind of the components from E_Void to E_Component.
8942
8943 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8944 Record_Type_Definition (Empty, Derived_Type);
8945
8946 -- STEP 5c: Process the record extension for non private tagged types
8947
8948 elsif not Private_Extension then
8949 Expand_Record_Extension (Derived_Type, Type_Def);
8950
8951 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8952 -- derived type to propagate some semantic information. This led
8953 -- to other ASIS failures and has been removed.
8954
8955 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8956 -- implemented interfaces if we are in expansion mode
8957
8958 if Expander_Active
8959 and then Has_Interfaces (Derived_Type)
8960 then
8961 Add_Interface_Tag_Components (N, Derived_Type);
8962 end if;
8963
8964 -- Analyze the record extension
8965
8966 Record_Type_Definition
8967 (Record_Extension_Part (Type_Def), Derived_Type);
8968 end if;
8969
8970 End_Scope;
8971
8972 -- Nothing else to do if there is an error in the derivation.
8973 -- An unusual case: the full view may be derived from a type in an
8974 -- instance, when the partial view was used illegally as an actual
8975 -- in that instance, leading to a circular definition.
8976
8977 if Etype (Derived_Type) = Any_Type
8978 or else Etype (Parent_Type) = Derived_Type
8979 then
8980 return;
8981 end if;
8982
8983 -- Set delayed freeze and then derive subprograms, we need to do
8984 -- this in this order so that derived subprograms inherit the
8985 -- derived freeze if necessary.
8986
8987 Set_Has_Delayed_Freeze (Derived_Type);
8988
8989 if Derive_Subps then
8990 Derive_Subprograms (Parent_Type, Derived_Type);
8991 end if;
8992
8993 -- If we have a private extension which defines a constrained derived
8994 -- type mark as constrained here after we have derived subprograms. See
8995 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8996
8997 if Private_Extension and then Inherit_Discrims then
8998 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8999 Set_Is_Constrained (Derived_Type, True);
9000 Set_Discriminant_Constraint (Derived_Type, Discs);
9001
9002 elsif Is_Constrained (Parent_Type) then
9003 Set_Is_Constrained
9004 (Derived_Type, True);
9005 Set_Discriminant_Constraint
9006 (Derived_Type, Discriminant_Constraint (Parent_Type));
9007 end if;
9008 end if;
9009
9010 -- Update the class-wide type, which shares the now-completed entity
9011 -- list with its specific type. In case of underlying record views,
9012 -- we do not generate the corresponding class wide entity.
9013
9014 if Is_Tagged
9015 and then not Is_Underlying_Record_View (Derived_Type)
9016 then
9017 Set_First_Entity
9018 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9019 Set_Last_Entity
9020 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9021 end if;
9022
9023 Check_Function_Writable_Actuals (N);
9024 end Build_Derived_Record_Type;
9025
9026 ------------------------
9027 -- Build_Derived_Type --
9028 ------------------------
9029
9030 procedure Build_Derived_Type
9031 (N : Node_Id;
9032 Parent_Type : Entity_Id;
9033 Derived_Type : Entity_Id;
9034 Is_Completion : Boolean;
9035 Derive_Subps : Boolean := True)
9036 is
9037 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9038
9039 begin
9040 -- Set common attributes
9041
9042 Set_Scope (Derived_Type, Current_Scope);
9043
9044 Set_Etype (Derived_Type, Parent_Base);
9045 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9046 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9047
9048 Set_Size_Info (Derived_Type, Parent_Type);
9049 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9050 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9051 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9052
9053 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9054 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9055
9056 if Is_Tagged_Type (Derived_Type) then
9057 Set_No_Tagged_Streams_Pragma
9058 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9059 end if;
9060
9061 -- If the parent has primitive routines, set the derived type link
9062
9063 if Has_Primitive_Operations (Parent_Type) then
9064 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9065 end if;
9066
9067 -- If the parent type is a private subtype, the convention on the base
9068 -- type may be set in the private part, and not propagated to the
9069 -- subtype until later, so we obtain the convention from the base type.
9070
9071 Set_Convention (Derived_Type, Convention (Parent_Base));
9072
9073 -- Set SSO default for record or array type
9074
9075 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9076 and then Is_Base_Type (Derived_Type)
9077 then
9078 Set_Default_SSO (Derived_Type);
9079 end if;
9080
9081 -- A derived type inherits the Default_Initial_Condition pragma coming
9082 -- from any parent type within the derivation chain.
9083
9084 if Has_DIC (Parent_Type) then
9085 Set_Has_Inherited_DIC (Derived_Type);
9086 end if;
9087
9088 -- A derived type inherits any class-wide invariants coming from a
9089 -- parent type or an interface. Note that the invariant procedure of
9090 -- the parent type should not be inherited because the derived type may
9091 -- define invariants of its own.
9092
9093 if not Is_Interface (Derived_Type) then
9094 if Has_Inherited_Invariants (Parent_Type)
9095 or else Has_Inheritable_Invariants (Parent_Type)
9096 then
9097 Set_Has_Inherited_Invariants (Derived_Type);
9098
9099 elsif Is_Concurrent_Type (Derived_Type)
9100 or else Is_Tagged_Type (Derived_Type)
9101 then
9102 declare
9103 Iface : Entity_Id;
9104 Ifaces : Elist_Id;
9105 Iface_Elmt : Elmt_Id;
9106
9107 begin
9108 Collect_Interfaces
9109 (T => Derived_Type,
9110 Ifaces_List => Ifaces,
9111 Exclude_Parents => True);
9112
9113 if Present (Ifaces) then
9114 Iface_Elmt := First_Elmt (Ifaces);
9115 while Present (Iface_Elmt) loop
9116 Iface := Node (Iface_Elmt);
9117
9118 if Has_Inheritable_Invariants (Iface) then
9119 Set_Has_Inherited_Invariants (Derived_Type);
9120 exit;
9121 end if;
9122
9123 Next_Elmt (Iface_Elmt);
9124 end loop;
9125 end if;
9126 end;
9127 end if;
9128 end if;
9129
9130 -- We similarly inherit predicates
9131
9132 if Has_Predicates (Parent_Type) then
9133 Set_Has_Predicates (Derived_Type);
9134 end if;
9135
9136 -- The derived type inherits the representation clauses of the parent
9137
9138 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9139
9140 -- If the parent type has delayed rep aspects, then mark the derived
9141 -- type as possibly inheriting a delayed rep aspect.
9142
9143 if Has_Delayed_Rep_Aspects (Parent_Type) then
9144 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9145 end if;
9146
9147 -- Propagate the attributes related to pragma Ghost from the parent type
9148 -- to the derived type or type extension (SPARK RM 6.9(9)).
9149
9150 if Is_Ghost_Entity (Parent_Type) then
9151 Set_Is_Ghost_Entity (Derived_Type);
9152 end if;
9153
9154 -- Type dependent processing
9155
9156 case Ekind (Parent_Type) is
9157 when Numeric_Kind =>
9158 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9159
9160 when Array_Kind =>
9161 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9162
9163 when E_Record_Type
9164 | E_Record_Subtype
9165 | Class_Wide_Kind =>
9166 Build_Derived_Record_Type
9167 (N, Parent_Type, Derived_Type, Derive_Subps);
9168 return;
9169
9170 when Enumeration_Kind =>
9171 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9172
9173 when Access_Kind =>
9174 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9175
9176 when Incomplete_Or_Private_Kind =>
9177 Build_Derived_Private_Type
9178 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9179
9180 -- For discriminated types, the derivation includes deriving
9181 -- primitive operations. For others it is done below.
9182
9183 if Is_Tagged_Type (Parent_Type)
9184 or else Has_Discriminants (Parent_Type)
9185 or else (Present (Full_View (Parent_Type))
9186 and then Has_Discriminants (Full_View (Parent_Type)))
9187 then
9188 return;
9189 end if;
9190
9191 when Concurrent_Kind =>
9192 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9193
9194 when others =>
9195 raise Program_Error;
9196 end case;
9197
9198 -- Nothing more to do if some error occurred
9199
9200 if Etype (Derived_Type) = Any_Type then
9201 return;
9202 end if;
9203
9204 -- Set delayed freeze and then derive subprograms, we need to do this
9205 -- in this order so that derived subprograms inherit the derived freeze
9206 -- if necessary.
9207
9208 Set_Has_Delayed_Freeze (Derived_Type);
9209
9210 if Derive_Subps then
9211 Derive_Subprograms (Parent_Type, Derived_Type);
9212 end if;
9213
9214 Set_Has_Primitive_Operations
9215 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9216 end Build_Derived_Type;
9217
9218 -----------------------
9219 -- Build_Discriminal --
9220 -----------------------
9221
9222 procedure Build_Discriminal (Discrim : Entity_Id) is
9223 D_Minal : Entity_Id;
9224 CR_Disc : Entity_Id;
9225
9226 begin
9227 -- A discriminal has the same name as the discriminant
9228
9229 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9230
9231 Set_Ekind (D_Minal, E_In_Parameter);
9232 Set_Mechanism (D_Minal, Default_Mechanism);
9233 Set_Etype (D_Minal, Etype (Discrim));
9234 Set_Scope (D_Minal, Current_Scope);
9235 Set_Parent (D_Minal, Parent (Discrim));
9236
9237 Set_Discriminal (Discrim, D_Minal);
9238 Set_Discriminal_Link (D_Minal, Discrim);
9239
9240 -- For task types, build at once the discriminants of the corresponding
9241 -- record, which are needed if discriminants are used in entry defaults
9242 -- and in family bounds.
9243
9244 if Is_Concurrent_Type (Current_Scope)
9245 or else
9246 Is_Limited_Type (Current_Scope)
9247 then
9248 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9249
9250 Set_Ekind (CR_Disc, E_In_Parameter);
9251 Set_Mechanism (CR_Disc, Default_Mechanism);
9252 Set_Etype (CR_Disc, Etype (Discrim));
9253 Set_Scope (CR_Disc, Current_Scope);
9254 Set_Discriminal_Link (CR_Disc, Discrim);
9255 Set_CR_Discriminant (Discrim, CR_Disc);
9256 end if;
9257 end Build_Discriminal;
9258
9259 ------------------------------------
9260 -- Build_Discriminant_Constraints --
9261 ------------------------------------
9262
9263 function Build_Discriminant_Constraints
9264 (T : Entity_Id;
9265 Def : Node_Id;
9266 Derived_Def : Boolean := False) return Elist_Id
9267 is
9268 C : constant Node_Id := Constraint (Def);
9269 Nb_Discr : constant Nat := Number_Discriminants (T);
9270
9271 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9272 -- Saves the expression corresponding to a given discriminant in T
9273
9274 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9275 -- Return the Position number within array Discr_Expr of a discriminant
9276 -- D within the discriminant list of the discriminated type T.
9277
9278 procedure Process_Discriminant_Expression
9279 (Expr : Node_Id;
9280 D : Entity_Id);
9281 -- If this is a discriminant constraint on a partial view, do not
9282 -- generate an overflow check on the discriminant expression. The check
9283 -- will be generated when constraining the full view. Otherwise the
9284 -- backend creates duplicate symbols for the temporaries corresponding
9285 -- to the expressions to be checked, causing spurious assembler errors.
9286
9287 ------------------
9288 -- Pos_Of_Discr --
9289 ------------------
9290
9291 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9292 Disc : Entity_Id;
9293
9294 begin
9295 Disc := First_Discriminant (T);
9296 for J in Discr_Expr'Range loop
9297 if Disc = D then
9298 return J;
9299 end if;
9300
9301 Next_Discriminant (Disc);
9302 end loop;
9303
9304 -- Note: Since this function is called on discriminants that are
9305 -- known to belong to the discriminated type, falling through the
9306 -- loop with no match signals an internal compiler error.
9307
9308 raise Program_Error;
9309 end Pos_Of_Discr;
9310
9311 -------------------------------------
9312 -- Process_Discriminant_Expression --
9313 -------------------------------------
9314
9315 procedure Process_Discriminant_Expression
9316 (Expr : Node_Id;
9317 D : Entity_Id)
9318 is
9319 BDT : constant Entity_Id := Base_Type (Etype (D));
9320
9321 begin
9322 -- If this is a discriminant constraint on a partial view, do
9323 -- not generate an overflow on the discriminant expression. The
9324 -- check will be generated when constraining the full view.
9325
9326 if Is_Private_Type (T)
9327 and then Present (Full_View (T))
9328 then
9329 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9330 else
9331 Analyze_And_Resolve (Expr, BDT);
9332 end if;
9333 end Process_Discriminant_Expression;
9334
9335 -- Declarations local to Build_Discriminant_Constraints
9336
9337 Discr : Entity_Id;
9338 E : Entity_Id;
9339 Elist : constant Elist_Id := New_Elmt_List;
9340
9341 Constr : Node_Id;
9342 Expr : Node_Id;
9343 Id : Node_Id;
9344 Position : Nat;
9345 Found : Boolean;
9346
9347 Discrim_Present : Boolean := False;
9348
9349 -- Start of processing for Build_Discriminant_Constraints
9350
9351 begin
9352 -- The following loop will process positional associations only.
9353 -- For a positional association, the (single) discriminant is
9354 -- implicitly specified by position, in textual order (RM 3.7.2).
9355
9356 Discr := First_Discriminant (T);
9357 Constr := First (Constraints (C));
9358 for D in Discr_Expr'Range loop
9359 exit when Nkind (Constr) = N_Discriminant_Association;
9360
9361 if No (Constr) then
9362 Error_Msg_N ("too few discriminants given in constraint", C);
9363 return New_Elmt_List;
9364
9365 elsif Nkind (Constr) = N_Range
9366 or else (Nkind (Constr) = N_Attribute_Reference
9367 and then Attribute_Name (Constr) = Name_Range)
9368 then
9369 Error_Msg_N
9370 ("a range is not a valid discriminant constraint", Constr);
9371 Discr_Expr (D) := Error;
9372
9373 else
9374 Process_Discriminant_Expression (Constr, Discr);
9375 Discr_Expr (D) := Constr;
9376 end if;
9377
9378 Next_Discriminant (Discr);
9379 Next (Constr);
9380 end loop;
9381
9382 if No (Discr) and then Present (Constr) then
9383 Error_Msg_N ("too many discriminants given in constraint", Constr);
9384 return New_Elmt_List;
9385 end if;
9386
9387 -- Named associations can be given in any order, but if both positional
9388 -- and named associations are used in the same discriminant constraint,
9389 -- then positional associations must occur first, at their normal
9390 -- position. Hence once a named association is used, the rest of the
9391 -- discriminant constraint must use only named associations.
9392
9393 while Present (Constr) loop
9394
9395 -- Positional association forbidden after a named association
9396
9397 if Nkind (Constr) /= N_Discriminant_Association then
9398 Error_Msg_N ("positional association follows named one", Constr);
9399 return New_Elmt_List;
9400
9401 -- Otherwise it is a named association
9402
9403 else
9404 -- E records the type of the discriminants in the named
9405 -- association. All the discriminants specified in the same name
9406 -- association must have the same type.
9407
9408 E := Empty;
9409
9410 -- Search the list of discriminants in T to see if the simple name
9411 -- given in the constraint matches any of them.
9412
9413 Id := First (Selector_Names (Constr));
9414 while Present (Id) loop
9415 Found := False;
9416
9417 -- If Original_Discriminant is present, we are processing a
9418 -- generic instantiation and this is an instance node. We need
9419 -- to find the name of the corresponding discriminant in the
9420 -- actual record type T and not the name of the discriminant in
9421 -- the generic formal. Example:
9422
9423 -- generic
9424 -- type G (D : int) is private;
9425 -- package P is
9426 -- subtype W is G (D => 1);
9427 -- end package;
9428 -- type Rec (X : int) is record ... end record;
9429 -- package Q is new P (G => Rec);
9430
9431 -- At the point of the instantiation, formal type G is Rec
9432 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9433 -- which really looks like "subtype W is Rec (D => 1);" at
9434 -- the point of instantiation, we want to find the discriminant
9435 -- that corresponds to D in Rec, i.e. X.
9436
9437 if Present (Original_Discriminant (Id))
9438 and then In_Instance
9439 then
9440 Discr := Find_Corresponding_Discriminant (Id, T);
9441 Found := True;
9442
9443 else
9444 Discr := First_Discriminant (T);
9445 while Present (Discr) loop
9446 if Chars (Discr) = Chars (Id) then
9447 Found := True;
9448 exit;
9449 end if;
9450
9451 Next_Discriminant (Discr);
9452 end loop;
9453
9454 if not Found then
9455 Error_Msg_N ("& does not match any discriminant", Id);
9456 return New_Elmt_List;
9457
9458 -- If the parent type is a generic formal, preserve the
9459 -- name of the discriminant for subsequent instances.
9460 -- see comment at the beginning of this if statement.
9461
9462 elsif Is_Generic_Type (Root_Type (T)) then
9463 Set_Original_Discriminant (Id, Discr);
9464 end if;
9465 end if;
9466
9467 Position := Pos_Of_Discr (T, Discr);
9468
9469 if Present (Discr_Expr (Position)) then
9470 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9471
9472 else
9473 -- Each discriminant specified in the same named association
9474 -- must be associated with a separate copy of the
9475 -- corresponding expression.
9476
9477 if Present (Next (Id)) then
9478 Expr := New_Copy_Tree (Expression (Constr));
9479 Set_Parent (Expr, Parent (Expression (Constr)));
9480 else
9481 Expr := Expression (Constr);
9482 end if;
9483
9484 Discr_Expr (Position) := Expr;
9485 Process_Discriminant_Expression (Expr, Discr);
9486 end if;
9487
9488 -- A discriminant association with more than one discriminant
9489 -- name is only allowed if the named discriminants are all of
9490 -- the same type (RM 3.7.1(8)).
9491
9492 if E = Empty then
9493 E := Base_Type (Etype (Discr));
9494
9495 elsif Base_Type (Etype (Discr)) /= E then
9496 Error_Msg_N
9497 ("all discriminants in an association " &
9498 "must have the same type", Id);
9499 end if;
9500
9501 Next (Id);
9502 end loop;
9503 end if;
9504
9505 Next (Constr);
9506 end loop;
9507
9508 -- A discriminant constraint must provide exactly one value for each
9509 -- discriminant of the type (RM 3.7.1(8)).
9510
9511 for J in Discr_Expr'Range loop
9512 if No (Discr_Expr (J)) then
9513 Error_Msg_N ("too few discriminants given in constraint", C);
9514 return New_Elmt_List;
9515 end if;
9516 end loop;
9517
9518 -- Determine if there are discriminant expressions in the constraint
9519
9520 for J in Discr_Expr'Range loop
9521 if Denotes_Discriminant
9522 (Discr_Expr (J), Check_Concurrent => True)
9523 then
9524 Discrim_Present := True;
9525 end if;
9526 end loop;
9527
9528 -- Build an element list consisting of the expressions given in the
9529 -- discriminant constraint and apply the appropriate checks. The list
9530 -- is constructed after resolving any named discriminant associations
9531 -- and therefore the expressions appear in the textual order of the
9532 -- discriminants.
9533
9534 Discr := First_Discriminant (T);
9535 for J in Discr_Expr'Range loop
9536 if Discr_Expr (J) /= Error then
9537 Append_Elmt (Discr_Expr (J), Elist);
9538
9539 -- If any of the discriminant constraints is given by a
9540 -- discriminant and we are in a derived type declaration we
9541 -- have a discriminant renaming. Establish link between new
9542 -- and old discriminant.
9543
9544 if Denotes_Discriminant (Discr_Expr (J)) then
9545 if Derived_Def then
9546 Set_Corresponding_Discriminant
9547 (Entity (Discr_Expr (J)), Discr);
9548 end if;
9549
9550 -- Force the evaluation of non-discriminant expressions.
9551 -- If we have found a discriminant in the constraint 3.4(26)
9552 -- and 3.8(18) demand that no range checks are performed are
9553 -- after evaluation. If the constraint is for a component
9554 -- definition that has a per-object constraint, expressions are
9555 -- evaluated but not checked either. In all other cases perform
9556 -- a range check.
9557
9558 else
9559 if Discrim_Present then
9560 null;
9561
9562 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9563 and then
9564 Has_Per_Object_Constraint
9565 (Defining_Identifier (Parent (Parent (Def))))
9566 then
9567 null;
9568
9569 elsif Is_Access_Type (Etype (Discr)) then
9570 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9571
9572 else
9573 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9574 end if;
9575
9576 Force_Evaluation (Discr_Expr (J));
9577 end if;
9578
9579 -- Check that the designated type of an access discriminant's
9580 -- expression is not a class-wide type unless the discriminant's
9581 -- designated type is also class-wide.
9582
9583 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9584 and then not Is_Class_Wide_Type
9585 (Designated_Type (Etype (Discr)))
9586 and then Etype (Discr_Expr (J)) /= Any_Type
9587 and then Is_Class_Wide_Type
9588 (Designated_Type (Etype (Discr_Expr (J))))
9589 then
9590 Wrong_Type (Discr_Expr (J), Etype (Discr));
9591
9592 elsif Is_Access_Type (Etype (Discr))
9593 and then not Is_Access_Constant (Etype (Discr))
9594 and then Is_Access_Type (Etype (Discr_Expr (J)))
9595 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9596 then
9597 Error_Msg_NE
9598 ("constraint for discriminant& must be access to variable",
9599 Def, Discr);
9600 end if;
9601 end if;
9602
9603 Next_Discriminant (Discr);
9604 end loop;
9605
9606 return Elist;
9607 end Build_Discriminant_Constraints;
9608
9609 ---------------------------------
9610 -- Build_Discriminated_Subtype --
9611 ---------------------------------
9612
9613 procedure Build_Discriminated_Subtype
9614 (T : Entity_Id;
9615 Def_Id : Entity_Id;
9616 Elist : Elist_Id;
9617 Related_Nod : Node_Id;
9618 For_Access : Boolean := False)
9619 is
9620 Has_Discrs : constant Boolean := Has_Discriminants (T);
9621 Constrained : constant Boolean :=
9622 (Has_Discrs
9623 and then not Is_Empty_Elmt_List (Elist)
9624 and then not Is_Class_Wide_Type (T))
9625 or else Is_Constrained (T);
9626
9627 begin
9628 if Ekind (T) = E_Record_Type then
9629 if For_Access then
9630 Set_Ekind (Def_Id, E_Private_Subtype);
9631 Set_Is_For_Access_Subtype (Def_Id, True);
9632 else
9633 Set_Ekind (Def_Id, E_Record_Subtype);
9634 end if;
9635
9636 -- Inherit preelaboration flag from base, for types for which it
9637 -- may have been set: records, private types, protected types.
9638
9639 Set_Known_To_Have_Preelab_Init
9640 (Def_Id, Known_To_Have_Preelab_Init (T));
9641
9642 elsif Ekind (T) = E_Task_Type then
9643 Set_Ekind (Def_Id, E_Task_Subtype);
9644
9645 elsif Ekind (T) = E_Protected_Type then
9646 Set_Ekind (Def_Id, E_Protected_Subtype);
9647 Set_Known_To_Have_Preelab_Init
9648 (Def_Id, Known_To_Have_Preelab_Init (T));
9649
9650 elsif Is_Private_Type (T) then
9651 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9652 Set_Known_To_Have_Preelab_Init
9653 (Def_Id, Known_To_Have_Preelab_Init (T));
9654
9655 -- Private subtypes may have private dependents
9656
9657 Set_Private_Dependents (Def_Id, New_Elmt_List);
9658
9659 elsif Is_Class_Wide_Type (T) then
9660 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9661
9662 else
9663 -- Incomplete type. Attach subtype to list of dependents, to be
9664 -- completed with full view of parent type, unless is it the
9665 -- designated subtype of a record component within an init_proc.
9666 -- This last case arises for a component of an access type whose
9667 -- designated type is incomplete (e.g. a Taft Amendment type).
9668 -- The designated subtype is within an inner scope, and needs no
9669 -- elaboration, because only the access type is needed in the
9670 -- initialization procedure.
9671
9672 Set_Ekind (Def_Id, Ekind (T));
9673
9674 if For_Access and then Within_Init_Proc then
9675 null;
9676 else
9677 Append_Elmt (Def_Id, Private_Dependents (T));
9678 end if;
9679 end if;
9680
9681 Set_Etype (Def_Id, T);
9682 Init_Size_Align (Def_Id);
9683 Set_Has_Discriminants (Def_Id, Has_Discrs);
9684 Set_Is_Constrained (Def_Id, Constrained);
9685
9686 Set_First_Entity (Def_Id, First_Entity (T));
9687 Set_Last_Entity (Def_Id, Last_Entity (T));
9688 Set_Has_Implicit_Dereference
9689 (Def_Id, Has_Implicit_Dereference (T));
9690
9691 -- If the subtype is the completion of a private declaration, there may
9692 -- have been representation clauses for the partial view, and they must
9693 -- be preserved. Build_Derived_Type chains the inherited clauses with
9694 -- the ones appearing on the extension. If this comes from a subtype
9695 -- declaration, all clauses are inherited.
9696
9697 if No (First_Rep_Item (Def_Id)) then
9698 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9699 end if;
9700
9701 if Is_Tagged_Type (T) then
9702 Set_Is_Tagged_Type (Def_Id);
9703 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9704 Make_Class_Wide_Type (Def_Id);
9705 end if;
9706
9707 Set_Stored_Constraint (Def_Id, No_Elist);
9708
9709 if Has_Discrs then
9710 Set_Discriminant_Constraint (Def_Id, Elist);
9711 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9712 end if;
9713
9714 if Is_Tagged_Type (T) then
9715
9716 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9717 -- concurrent record type (which has the list of primitive
9718 -- operations).
9719
9720 if Ada_Version >= Ada_2005
9721 and then Is_Concurrent_Type (T)
9722 then
9723 Set_Corresponding_Record_Type (Def_Id,
9724 Corresponding_Record_Type (T));
9725 else
9726 Set_Direct_Primitive_Operations (Def_Id,
9727 Direct_Primitive_Operations (T));
9728 end if;
9729
9730 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9731 end if;
9732
9733 -- Subtypes introduced by component declarations do not need to be
9734 -- marked as delayed, and do not get freeze nodes, because the semantics
9735 -- verifies that the parents of the subtypes are frozen before the
9736 -- enclosing record is frozen.
9737
9738 if not Is_Type (Scope (Def_Id)) then
9739 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9740
9741 if Is_Private_Type (T)
9742 and then Present (Full_View (T))
9743 then
9744 Conditional_Delay (Def_Id, Full_View (T));
9745 else
9746 Conditional_Delay (Def_Id, T);
9747 end if;
9748 end if;
9749
9750 if Is_Record_Type (T) then
9751 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9752
9753 if Has_Discrs
9754 and then not Is_Empty_Elmt_List (Elist)
9755 and then not For_Access
9756 then
9757 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9758 elsif not For_Access then
9759 Set_Cloned_Subtype (Def_Id, T);
9760 end if;
9761 end if;
9762 end Build_Discriminated_Subtype;
9763
9764 ---------------------------
9765 -- Build_Itype_Reference --
9766 ---------------------------
9767
9768 procedure Build_Itype_Reference
9769 (Ityp : Entity_Id;
9770 Nod : Node_Id)
9771 is
9772 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9773 begin
9774
9775 -- Itype references are only created for use by the back-end
9776
9777 if Inside_A_Generic then
9778 return;
9779 else
9780 Set_Itype (IR, Ityp);
9781 Insert_After (Nod, IR);
9782 end if;
9783 end Build_Itype_Reference;
9784
9785 ------------------------
9786 -- Build_Scalar_Bound --
9787 ------------------------
9788
9789 function Build_Scalar_Bound
9790 (Bound : Node_Id;
9791 Par_T : Entity_Id;
9792 Der_T : Entity_Id) return Node_Id
9793 is
9794 New_Bound : Entity_Id;
9795
9796 begin
9797 -- Note: not clear why this is needed, how can the original bound
9798 -- be unanalyzed at this point? and if it is, what business do we
9799 -- have messing around with it? and why is the base type of the
9800 -- parent type the right type for the resolution. It probably is
9801 -- not. It is OK for the new bound we are creating, but not for
9802 -- the old one??? Still if it never happens, no problem.
9803
9804 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9805
9806 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9807 New_Bound := New_Copy (Bound);
9808 Set_Etype (New_Bound, Der_T);
9809 Set_Analyzed (New_Bound);
9810
9811 elsif Is_Entity_Name (Bound) then
9812 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9813
9814 -- The following is almost certainly wrong. What business do we have
9815 -- relocating a node (Bound) that is presumably still attached to
9816 -- the tree elsewhere???
9817
9818 else
9819 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9820 end if;
9821
9822 Set_Etype (New_Bound, Der_T);
9823 return New_Bound;
9824 end Build_Scalar_Bound;
9825
9826 --------------------------------
9827 -- Build_Underlying_Full_View --
9828 --------------------------------
9829
9830 procedure Build_Underlying_Full_View
9831 (N : Node_Id;
9832 Typ : Entity_Id;
9833 Par : Entity_Id)
9834 is
9835 Loc : constant Source_Ptr := Sloc (N);
9836 Subt : constant Entity_Id :=
9837 Make_Defining_Identifier
9838 (Loc, New_External_Name (Chars (Typ), 'S'));
9839
9840 Constr : Node_Id;
9841 Indic : Node_Id;
9842 C : Node_Id;
9843 Id : Node_Id;
9844
9845 procedure Set_Discriminant_Name (Id : Node_Id);
9846 -- If the derived type has discriminants, they may rename discriminants
9847 -- of the parent. When building the full view of the parent, we need to
9848 -- recover the names of the original discriminants if the constraint is
9849 -- given by named associations.
9850
9851 ---------------------------
9852 -- Set_Discriminant_Name --
9853 ---------------------------
9854
9855 procedure Set_Discriminant_Name (Id : Node_Id) is
9856 Disc : Entity_Id;
9857
9858 begin
9859 Set_Original_Discriminant (Id, Empty);
9860
9861 if Has_Discriminants (Typ) then
9862 Disc := First_Discriminant (Typ);
9863 while Present (Disc) loop
9864 if Chars (Disc) = Chars (Id)
9865 and then Present (Corresponding_Discriminant (Disc))
9866 then
9867 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9868 end if;
9869 Next_Discriminant (Disc);
9870 end loop;
9871 end if;
9872 end Set_Discriminant_Name;
9873
9874 -- Start of processing for Build_Underlying_Full_View
9875
9876 begin
9877 if Nkind (N) = N_Full_Type_Declaration then
9878 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9879
9880 elsif Nkind (N) = N_Subtype_Declaration then
9881 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9882
9883 elsif Nkind (N) = N_Component_Declaration then
9884 Constr :=
9885 New_Copy_Tree
9886 (Constraint (Subtype_Indication (Component_Definition (N))));
9887
9888 else
9889 raise Program_Error;
9890 end if;
9891
9892 C := First (Constraints (Constr));
9893 while Present (C) loop
9894 if Nkind (C) = N_Discriminant_Association then
9895 Id := First (Selector_Names (C));
9896 while Present (Id) loop
9897 Set_Discriminant_Name (Id);
9898 Next (Id);
9899 end loop;
9900 end if;
9901
9902 Next (C);
9903 end loop;
9904
9905 Indic :=
9906 Make_Subtype_Declaration (Loc,
9907 Defining_Identifier => Subt,
9908 Subtype_Indication =>
9909 Make_Subtype_Indication (Loc,
9910 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9911 Constraint => New_Copy_Tree (Constr)));
9912
9913 -- If this is a component subtype for an outer itype, it is not
9914 -- a list member, so simply set the parent link for analysis: if
9915 -- the enclosing type does not need to be in a declarative list,
9916 -- neither do the components.
9917
9918 if Is_List_Member (N)
9919 and then Nkind (N) /= N_Component_Declaration
9920 then
9921 Insert_Before (N, Indic);
9922 else
9923 Set_Parent (Indic, Parent (N));
9924 end if;
9925
9926 Analyze (Indic);
9927 Set_Underlying_Full_View (Typ, Full_View (Subt));
9928 end Build_Underlying_Full_View;
9929
9930 -------------------------------
9931 -- Check_Abstract_Overriding --
9932 -------------------------------
9933
9934 procedure Check_Abstract_Overriding (T : Entity_Id) is
9935 Alias_Subp : Entity_Id;
9936 Elmt : Elmt_Id;
9937 Op_List : Elist_Id;
9938 Subp : Entity_Id;
9939 Type_Def : Node_Id;
9940
9941 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9942 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9943 -- which has pragma Implemented already set. Check whether Subp's entity
9944 -- kind conforms to the implementation kind of the overridden routine.
9945
9946 procedure Check_Pragma_Implemented
9947 (Subp : Entity_Id;
9948 Iface_Subp : Entity_Id);
9949 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9950 -- Iface_Subp and both entities have pragma Implemented already set on
9951 -- them. Check whether the two implementation kinds are conforming.
9952
9953 procedure Inherit_Pragma_Implemented
9954 (Subp : Entity_Id;
9955 Iface_Subp : Entity_Id);
9956 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9957 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9958 -- Propagate the implementation kind of Iface_Subp to Subp.
9959
9960 ------------------------------
9961 -- Check_Pragma_Implemented --
9962 ------------------------------
9963
9964 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9965 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9966 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9967 Subp_Alias : constant Entity_Id := Alias (Subp);
9968 Contr_Typ : Entity_Id;
9969 Impl_Subp : Entity_Id;
9970
9971 begin
9972 -- Subp must have an alias since it is a hidden entity used to link
9973 -- an interface subprogram to its overriding counterpart.
9974
9975 pragma Assert (Present (Subp_Alias));
9976
9977 -- Handle aliases to synchronized wrappers
9978
9979 Impl_Subp := Subp_Alias;
9980
9981 if Is_Primitive_Wrapper (Impl_Subp) then
9982 Impl_Subp := Wrapped_Entity (Impl_Subp);
9983 end if;
9984
9985 -- Extract the type of the controlling formal
9986
9987 Contr_Typ := Etype (First_Formal (Subp_Alias));
9988
9989 if Is_Concurrent_Record_Type (Contr_Typ) then
9990 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9991 end if;
9992
9993 -- An interface subprogram whose implementation kind is By_Entry must
9994 -- be implemented by an entry.
9995
9996 if Impl_Kind = Name_By_Entry
9997 and then Ekind (Impl_Subp) /= E_Entry
9998 then
9999 Error_Msg_Node_2 := Iface_Alias;
10000 Error_Msg_NE
10001 ("type & must implement abstract subprogram & with an entry",
10002 Subp_Alias, Contr_Typ);
10003
10004 elsif Impl_Kind = Name_By_Protected_Procedure then
10005
10006 -- An interface subprogram whose implementation kind is By_
10007 -- Protected_Procedure cannot be implemented by a primitive
10008 -- procedure of a task type.
10009
10010 if Ekind (Contr_Typ) /= E_Protected_Type then
10011 Error_Msg_Node_2 := Contr_Typ;
10012 Error_Msg_NE
10013 ("interface subprogram & cannot be implemented by a " &
10014 "primitive procedure of task type &", Subp_Alias,
10015 Iface_Alias);
10016
10017 -- An interface subprogram whose implementation kind is By_
10018 -- Protected_Procedure must be implemented by a procedure.
10019
10020 elsif Ekind (Impl_Subp) /= E_Procedure then
10021 Error_Msg_Node_2 := Iface_Alias;
10022 Error_Msg_NE
10023 ("type & must implement abstract subprogram & with a " &
10024 "procedure", Subp_Alias, Contr_Typ);
10025
10026 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10027 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10028 then
10029 Error_Msg_Name_1 := Impl_Kind;
10030 Error_Msg_N
10031 ("overriding operation& must have synchronization%",
10032 Subp_Alias);
10033 end if;
10034
10035 -- If primitive has Optional synchronization, overriding operation
10036 -- must match if it has an explicit synchronization..
10037
10038 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10039 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10040 then
10041 Error_Msg_Name_1 := Impl_Kind;
10042 Error_Msg_N
10043 ("overriding operation& must have syncrhonization%",
10044 Subp_Alias);
10045 end if;
10046 end Check_Pragma_Implemented;
10047
10048 ------------------------------
10049 -- Check_Pragma_Implemented --
10050 ------------------------------
10051
10052 procedure Check_Pragma_Implemented
10053 (Subp : Entity_Id;
10054 Iface_Subp : Entity_Id)
10055 is
10056 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10057 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10058
10059 begin
10060 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10061 -- and overriding subprogram are different. In general this is an
10062 -- error except when the implementation kind of the overridden
10063 -- subprograms is By_Any or Optional.
10064
10065 if Iface_Kind /= Subp_Kind
10066 and then Iface_Kind /= Name_By_Any
10067 and then Iface_Kind /= Name_Optional
10068 then
10069 if Iface_Kind = Name_By_Entry then
10070 Error_Msg_N
10071 ("incompatible implementation kind, overridden subprogram " &
10072 "is marked By_Entry", Subp);
10073 else
10074 Error_Msg_N
10075 ("incompatible implementation kind, overridden subprogram " &
10076 "is marked By_Protected_Procedure", Subp);
10077 end if;
10078 end if;
10079 end Check_Pragma_Implemented;
10080
10081 --------------------------------
10082 -- Inherit_Pragma_Implemented --
10083 --------------------------------
10084
10085 procedure Inherit_Pragma_Implemented
10086 (Subp : Entity_Id;
10087 Iface_Subp : Entity_Id)
10088 is
10089 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10090 Loc : constant Source_Ptr := Sloc (Subp);
10091 Impl_Prag : Node_Id;
10092
10093 begin
10094 -- Since the implementation kind is stored as a representation item
10095 -- rather than a flag, create a pragma node.
10096
10097 Impl_Prag :=
10098 Make_Pragma (Loc,
10099 Chars => Name_Implemented,
10100 Pragma_Argument_Associations => New_List (
10101 Make_Pragma_Argument_Association (Loc,
10102 Expression => New_Occurrence_Of (Subp, Loc)),
10103
10104 Make_Pragma_Argument_Association (Loc,
10105 Expression => Make_Identifier (Loc, Iface_Kind))));
10106
10107 -- The pragma doesn't need to be analyzed because it is internally
10108 -- built. It is safe to directly register it as a rep item since we
10109 -- are only interested in the characters of the implementation kind.
10110
10111 Record_Rep_Item (Subp, Impl_Prag);
10112 end Inherit_Pragma_Implemented;
10113
10114 -- Start of processing for Check_Abstract_Overriding
10115
10116 begin
10117 Op_List := Primitive_Operations (T);
10118
10119 -- Loop to check primitive operations
10120
10121 Elmt := First_Elmt (Op_List);
10122 while Present (Elmt) loop
10123 Subp := Node (Elmt);
10124 Alias_Subp := Alias (Subp);
10125
10126 -- Inherited subprograms are identified by the fact that they do not
10127 -- come from source, and the associated source location is the
10128 -- location of the first subtype of the derived type.
10129
10130 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10131 -- subprograms that "require overriding".
10132
10133 -- Special exception, do not complain about failure to override the
10134 -- stream routines _Input and _Output, as well as the primitive
10135 -- operations used in dispatching selects since we always provide
10136 -- automatic overridings for these subprograms.
10137
10138 -- The partial view of T may have been a private extension, for
10139 -- which inherited functions dispatching on result are abstract.
10140 -- If the full view is a null extension, there is no need for
10141 -- overriding in Ada 2005, but wrappers need to be built for them
10142 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10143
10144 if Is_Null_Extension (T)
10145 and then Has_Controlling_Result (Subp)
10146 and then Ada_Version >= Ada_2005
10147 and then Present (Alias_Subp)
10148 and then not Comes_From_Source (Subp)
10149 and then not Is_Abstract_Subprogram (Alias_Subp)
10150 and then not Is_Access_Type (Etype (Subp))
10151 then
10152 null;
10153
10154 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10155 -- processing because this check is done with the aliased
10156 -- entity
10157
10158 elsif Present (Interface_Alias (Subp)) then
10159 null;
10160
10161 elsif (Is_Abstract_Subprogram (Subp)
10162 or else Requires_Overriding (Subp)
10163 or else
10164 (Has_Controlling_Result (Subp)
10165 and then Present (Alias_Subp)
10166 and then not Comes_From_Source (Subp)
10167 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10168 and then not Is_TSS (Subp, TSS_Stream_Input)
10169 and then not Is_TSS (Subp, TSS_Stream_Output)
10170 and then not Is_Abstract_Type (T)
10171 and then not Is_Predefined_Interface_Primitive (Subp)
10172
10173 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10174 -- with abstract interface types because the check will be done
10175 -- with the aliased entity (otherwise we generate a duplicated
10176 -- error message).
10177
10178 and then not Present (Interface_Alias (Subp))
10179 then
10180 if Present (Alias_Subp) then
10181
10182 -- Only perform the check for a derived subprogram when the
10183 -- type has an explicit record extension. This avoids incorrect
10184 -- flagging of abstract subprograms for the case of a type
10185 -- without an extension that is derived from a formal type
10186 -- with a tagged actual (can occur within a private part).
10187
10188 -- Ada 2005 (AI-391): In the case of an inherited function with
10189 -- a controlling result of the type, the rule does not apply if
10190 -- the type is a null extension (unless the parent function
10191 -- itself is abstract, in which case the function must still be
10192 -- be overridden). The expander will generate an overriding
10193 -- wrapper function calling the parent subprogram (see
10194 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10195
10196 Type_Def := Type_Definition (Parent (T));
10197
10198 if Nkind (Type_Def) = N_Derived_Type_Definition
10199 and then Present (Record_Extension_Part (Type_Def))
10200 and then
10201 (Ada_Version < Ada_2005
10202 or else not Is_Null_Extension (T)
10203 or else Ekind (Subp) = E_Procedure
10204 or else not Has_Controlling_Result (Subp)
10205 or else Is_Abstract_Subprogram (Alias_Subp)
10206 or else Requires_Overriding (Subp)
10207 or else Is_Access_Type (Etype (Subp)))
10208 then
10209 -- Avoid reporting error in case of abstract predefined
10210 -- primitive inherited from interface type because the
10211 -- body of internally generated predefined primitives
10212 -- of tagged types are generated later by Freeze_Type
10213
10214 if Is_Interface (Root_Type (T))
10215 and then Is_Abstract_Subprogram (Subp)
10216 and then Is_Predefined_Dispatching_Operation (Subp)
10217 and then not Comes_From_Source (Ultimate_Alias (Subp))
10218 then
10219 null;
10220
10221 -- A null extension is not obliged to override an inherited
10222 -- procedure subject to pragma Extensions_Visible with value
10223 -- False and at least one controlling OUT parameter
10224 -- (SPARK RM 6.1.7(6)).
10225
10226 elsif Is_Null_Extension (T)
10227 and then Is_EVF_Procedure (Subp)
10228 then
10229 null;
10230
10231 else
10232 Error_Msg_NE
10233 ("type must be declared abstract or & overridden",
10234 T, Subp);
10235
10236 -- Traverse the whole chain of aliased subprograms to
10237 -- complete the error notification. This is especially
10238 -- useful for traceability of the chain of entities when
10239 -- the subprogram corresponds with an interface
10240 -- subprogram (which may be defined in another package).
10241
10242 if Present (Alias_Subp) then
10243 declare
10244 E : Entity_Id;
10245
10246 begin
10247 E := Subp;
10248 while Present (Alias (E)) loop
10249
10250 -- Avoid reporting redundant errors on entities
10251 -- inherited from interfaces
10252
10253 if Sloc (E) /= Sloc (T) then
10254 Error_Msg_Sloc := Sloc (E);
10255 Error_Msg_NE
10256 ("\& has been inherited #", T, Subp);
10257 end if;
10258
10259 E := Alias (E);
10260 end loop;
10261
10262 Error_Msg_Sloc := Sloc (E);
10263
10264 -- AI05-0068: report if there is an overriding
10265 -- non-abstract subprogram that is invisible.
10266
10267 if Is_Hidden (E)
10268 and then not Is_Abstract_Subprogram (E)
10269 then
10270 Error_Msg_NE
10271 ("\& subprogram# is not visible",
10272 T, Subp);
10273
10274 -- Clarify the case where a non-null extension must
10275 -- override inherited procedure subject to pragma
10276 -- Extensions_Visible with value False and at least
10277 -- one controlling OUT param.
10278
10279 elsif Is_EVF_Procedure (E) then
10280 Error_Msg_NE
10281 ("\& # is subject to Extensions_Visible False",
10282 T, Subp);
10283
10284 else
10285 Error_Msg_NE
10286 ("\& has been inherited from subprogram #",
10287 T, Subp);
10288 end if;
10289 end;
10290 end if;
10291 end if;
10292
10293 -- Ada 2005 (AI-345): Protected or task type implementing
10294 -- abstract interfaces.
10295
10296 elsif Is_Concurrent_Record_Type (T)
10297 and then Present (Interfaces (T))
10298 then
10299 -- There is no need to check here RM 9.4(11.9/3) since we
10300 -- are processing the corresponding record type and the
10301 -- mode of the overriding subprograms was verified by
10302 -- Check_Conformance when the corresponding concurrent
10303 -- type declaration was analyzed.
10304
10305 Error_Msg_NE
10306 ("interface subprogram & must be overridden", T, Subp);
10307
10308 -- Examine primitive operations of synchronized type to find
10309 -- homonyms that have the wrong profile.
10310
10311 declare
10312 Prim : Entity_Id;
10313
10314 begin
10315 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10316 while Present (Prim) loop
10317 if Chars (Prim) = Chars (Subp) then
10318 Error_Msg_NE
10319 ("profile is not type conformant with prefixed "
10320 & "view profile of inherited operation&",
10321 Prim, Subp);
10322 end if;
10323
10324 Next_Entity (Prim);
10325 end loop;
10326 end;
10327 end if;
10328
10329 else
10330 Error_Msg_Node_2 := T;
10331 Error_Msg_N
10332 ("abstract subprogram& not allowed for type&", Subp);
10333
10334 -- Also post unconditional warning on the type (unconditional
10335 -- so that if there are more than one of these cases, we get
10336 -- them all, and not just the first one).
10337
10338 Error_Msg_Node_2 := Subp;
10339 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10340 end if;
10341
10342 -- A subprogram subject to pragma Extensions_Visible with value
10343 -- "True" cannot override a subprogram subject to the same pragma
10344 -- with value "False" (SPARK RM 6.1.7(5)).
10345
10346 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10347 and then Present (Overridden_Operation (Subp))
10348 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10349 Extensions_Visible_False
10350 then
10351 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10352 Error_Msg_N
10353 ("subprogram & with Extensions_Visible True cannot override "
10354 & "subprogram # with Extensions_Visible False", Subp);
10355 end if;
10356
10357 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10358
10359 -- Subp is an expander-generated procedure which maps an interface
10360 -- alias to a protected wrapper. The interface alias is flagged by
10361 -- pragma Implemented. Ensure that Subp is a procedure when the
10362 -- implementation kind is By_Protected_Procedure or an entry when
10363 -- By_Entry.
10364
10365 if Ada_Version >= Ada_2012
10366 and then Is_Hidden (Subp)
10367 and then Present (Interface_Alias (Subp))
10368 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10369 then
10370 Check_Pragma_Implemented (Subp);
10371 end if;
10372
10373 -- Subp is an interface primitive which overrides another interface
10374 -- primitive marked with pragma Implemented.
10375
10376 if Ada_Version >= Ada_2012
10377 and then Present (Overridden_Operation (Subp))
10378 and then Has_Rep_Pragma
10379 (Overridden_Operation (Subp), Name_Implemented)
10380 then
10381 -- If the overriding routine is also marked by Implemented, check
10382 -- that the two implementation kinds are conforming.
10383
10384 if Has_Rep_Pragma (Subp, Name_Implemented) then
10385 Check_Pragma_Implemented
10386 (Subp => Subp,
10387 Iface_Subp => Overridden_Operation (Subp));
10388
10389 -- Otherwise the overriding routine inherits the implementation
10390 -- kind from the overridden subprogram.
10391
10392 else
10393 Inherit_Pragma_Implemented
10394 (Subp => Subp,
10395 Iface_Subp => Overridden_Operation (Subp));
10396 end if;
10397 end if;
10398
10399 -- If the operation is a wrapper for a synchronized primitive, it
10400 -- may be called indirectly through a dispatching select. We assume
10401 -- that it will be referenced elsewhere indirectly, and suppress
10402 -- warnings about an unused entity.
10403
10404 if Is_Primitive_Wrapper (Subp)
10405 and then Present (Wrapped_Entity (Subp))
10406 then
10407 Set_Referenced (Wrapped_Entity (Subp));
10408 end if;
10409
10410 Next_Elmt (Elmt);
10411 end loop;
10412 end Check_Abstract_Overriding;
10413
10414 ------------------------------------------------
10415 -- Check_Access_Discriminant_Requires_Limited --
10416 ------------------------------------------------
10417
10418 procedure Check_Access_Discriminant_Requires_Limited
10419 (D : Node_Id;
10420 Loc : Node_Id)
10421 is
10422 begin
10423 -- A discriminant_specification for an access discriminant shall appear
10424 -- only in the declaration for a task or protected type, or for a type
10425 -- with the reserved word 'limited' in its definition or in one of its
10426 -- ancestors (RM 3.7(10)).
10427
10428 -- AI-0063: The proper condition is that type must be immutably limited,
10429 -- or else be a partial view.
10430
10431 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10432 if Is_Limited_View (Current_Scope)
10433 or else
10434 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10435 and then Limited_Present (Parent (Current_Scope)))
10436 then
10437 null;
10438
10439 else
10440 Error_Msg_N
10441 ("access discriminants allowed only for limited types", Loc);
10442 end if;
10443 end if;
10444 end Check_Access_Discriminant_Requires_Limited;
10445
10446 -----------------------------------
10447 -- Check_Aliased_Component_Types --
10448 -----------------------------------
10449
10450 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10451 C : Entity_Id;
10452
10453 begin
10454 -- ??? Also need to check components of record extensions, but not
10455 -- components of protected types (which are always limited).
10456
10457 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10458 -- types to be unconstrained. This is safe because it is illegal to
10459 -- create access subtypes to such types with explicit discriminant
10460 -- constraints.
10461
10462 if not Is_Limited_Type (T) then
10463 if Ekind (T) = E_Record_Type then
10464 C := First_Component (T);
10465 while Present (C) loop
10466 if Is_Aliased (C)
10467 and then Has_Discriminants (Etype (C))
10468 and then not Is_Constrained (Etype (C))
10469 and then not In_Instance_Body
10470 and then Ada_Version < Ada_2005
10471 then
10472 Error_Msg_N
10473 ("aliased component must be constrained (RM 3.6(11))",
10474 C);
10475 end if;
10476
10477 Next_Component (C);
10478 end loop;
10479
10480 elsif Ekind (T) = E_Array_Type then
10481 if Has_Aliased_Components (T)
10482 and then Has_Discriminants (Component_Type (T))
10483 and then not Is_Constrained (Component_Type (T))
10484 and then not In_Instance_Body
10485 and then Ada_Version < Ada_2005
10486 then
10487 Error_Msg_N
10488 ("aliased component type must be constrained (RM 3.6(11))",
10489 T);
10490 end if;
10491 end if;
10492 end if;
10493 end Check_Aliased_Component_Types;
10494
10495 ---------------------------------------
10496 -- Check_Anonymous_Access_Components --
10497 ---------------------------------------
10498
10499 procedure Check_Anonymous_Access_Components
10500 (Typ_Decl : Node_Id;
10501 Typ : Entity_Id;
10502 Prev : Entity_Id;
10503 Comp_List : Node_Id)
10504 is
10505 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10506 Anon_Access : Entity_Id;
10507 Acc_Def : Node_Id;
10508 Comp : Node_Id;
10509 Comp_Def : Node_Id;
10510 Decl : Node_Id;
10511 Type_Def : Node_Id;
10512
10513 procedure Build_Incomplete_Type_Declaration;
10514 -- If the record type contains components that include an access to the
10515 -- current record, then create an incomplete type declaration for the
10516 -- record, to be used as the designated type of the anonymous access.
10517 -- This is done only once, and only if there is no previous partial
10518 -- view of the type.
10519
10520 function Designates_T (Subt : Node_Id) return Boolean;
10521 -- Check whether a node designates the enclosing record type, or 'Class
10522 -- of that type
10523
10524 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10525 -- Check whether an access definition includes a reference to
10526 -- the enclosing record type. The reference can be a subtype mark
10527 -- in the access definition itself, a 'Class attribute reference, or
10528 -- recursively a reference appearing in a parameter specification
10529 -- or result definition of an access_to_subprogram definition.
10530
10531 --------------------------------------
10532 -- Build_Incomplete_Type_Declaration --
10533 --------------------------------------
10534
10535 procedure Build_Incomplete_Type_Declaration is
10536 Decl : Node_Id;
10537 Inc_T : Entity_Id;
10538 H : Entity_Id;
10539
10540 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10541 -- it's "is new ... with record" or else "is tagged record ...".
10542
10543 Is_Tagged : constant Boolean :=
10544 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10545 and then
10546 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10547 or else
10548 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10549 and then Tagged_Present (Type_Definition (Typ_Decl)));
10550
10551 begin
10552 -- If there is a previous partial view, no need to create a new one
10553 -- If the partial view, given by Prev, is incomplete, If Prev is
10554 -- a private declaration, full declaration is flagged accordingly.
10555
10556 if Prev /= Typ then
10557 if Is_Tagged then
10558 Make_Class_Wide_Type (Prev);
10559 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10560 Set_Etype (Class_Wide_Type (Typ), Typ);
10561 end if;
10562
10563 return;
10564
10565 elsif Has_Private_Declaration (Typ) then
10566
10567 -- If we refer to T'Class inside T, and T is the completion of a
10568 -- private type, then make sure the class-wide type exists.
10569
10570 if Is_Tagged then
10571 Make_Class_Wide_Type (Typ);
10572 end if;
10573
10574 return;
10575
10576 -- If there was a previous anonymous access type, the incomplete
10577 -- type declaration will have been created already.
10578
10579 elsif Present (Current_Entity (Typ))
10580 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10581 and then Full_View (Current_Entity (Typ)) = Typ
10582 then
10583 if Is_Tagged
10584 and then Comes_From_Source (Current_Entity (Typ))
10585 and then not Is_Tagged_Type (Current_Entity (Typ))
10586 then
10587 Make_Class_Wide_Type (Typ);
10588 Error_Msg_N
10589 ("incomplete view of tagged type should be declared tagged??",
10590 Parent (Current_Entity (Typ)));
10591 end if;
10592 return;
10593
10594 else
10595 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10596 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10597
10598 -- Type has already been inserted into the current scope. Remove
10599 -- it, and add incomplete declaration for type, so that subsequent
10600 -- anonymous access types can use it. The entity is unchained from
10601 -- the homonym list and from immediate visibility. After analysis,
10602 -- the entity in the incomplete declaration becomes immediately
10603 -- visible in the record declaration that follows.
10604
10605 H := Current_Entity (Typ);
10606
10607 if H = Typ then
10608 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10609 else
10610 while Present (H)
10611 and then Homonym (H) /= Typ
10612 loop
10613 H := Homonym (Typ);
10614 end loop;
10615
10616 Set_Homonym (H, Homonym (Typ));
10617 end if;
10618
10619 Insert_Before (Typ_Decl, Decl);
10620 Analyze (Decl);
10621 Set_Full_View (Inc_T, Typ);
10622
10623 if Is_Tagged then
10624
10625 -- Create a common class-wide type for both views, and set the
10626 -- Etype of the class-wide type to the full view.
10627
10628 Make_Class_Wide_Type (Inc_T);
10629 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10630 Set_Etype (Class_Wide_Type (Typ), Typ);
10631 end if;
10632 end if;
10633 end Build_Incomplete_Type_Declaration;
10634
10635 ------------------
10636 -- Designates_T --
10637 ------------------
10638
10639 function Designates_T (Subt : Node_Id) return Boolean is
10640 Type_Id : constant Name_Id := Chars (Typ);
10641
10642 function Names_T (Nam : Node_Id) return Boolean;
10643 -- The record type has not been introduced in the current scope
10644 -- yet, so we must examine the name of the type itself, either
10645 -- an identifier T, or an expanded name of the form P.T, where
10646 -- P denotes the current scope.
10647
10648 -------------
10649 -- Names_T --
10650 -------------
10651
10652 function Names_T (Nam : Node_Id) return Boolean is
10653 begin
10654 if Nkind (Nam) = N_Identifier then
10655 return Chars (Nam) = Type_Id;
10656
10657 elsif Nkind (Nam) = N_Selected_Component then
10658 if Chars (Selector_Name (Nam)) = Type_Id then
10659 if Nkind (Prefix (Nam)) = N_Identifier then
10660 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10661
10662 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10663 return Chars (Selector_Name (Prefix (Nam))) =
10664 Chars (Current_Scope);
10665 else
10666 return False;
10667 end if;
10668
10669 else
10670 return False;
10671 end if;
10672
10673 else
10674 return False;
10675 end if;
10676 end Names_T;
10677
10678 -- Start of processing for Designates_T
10679
10680 begin
10681 if Nkind (Subt) = N_Identifier then
10682 return Chars (Subt) = Type_Id;
10683
10684 -- Reference can be through an expanded name which has not been
10685 -- analyzed yet, and which designates enclosing scopes.
10686
10687 elsif Nkind (Subt) = N_Selected_Component then
10688 if Names_T (Subt) then
10689 return True;
10690
10691 -- Otherwise it must denote an entity that is already visible.
10692 -- The access definition may name a subtype of the enclosing
10693 -- type, if there is a previous incomplete declaration for it.
10694
10695 else
10696 Find_Selected_Component (Subt);
10697 return
10698 Is_Entity_Name (Subt)
10699 and then Scope (Entity (Subt)) = Current_Scope
10700 and then
10701 (Chars (Base_Type (Entity (Subt))) = Type_Id
10702 or else
10703 (Is_Class_Wide_Type (Entity (Subt))
10704 and then
10705 Chars (Etype (Base_Type (Entity (Subt)))) =
10706 Type_Id));
10707 end if;
10708
10709 -- A reference to the current type may appear as the prefix of
10710 -- a 'Class attribute.
10711
10712 elsif Nkind (Subt) = N_Attribute_Reference
10713 and then Attribute_Name (Subt) = Name_Class
10714 then
10715 return Names_T (Prefix (Subt));
10716
10717 else
10718 return False;
10719 end if;
10720 end Designates_T;
10721
10722 ----------------
10723 -- Mentions_T --
10724 ----------------
10725
10726 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10727 Param_Spec : Node_Id;
10728
10729 Acc_Subprg : constant Node_Id :=
10730 Access_To_Subprogram_Definition (Acc_Def);
10731
10732 begin
10733 if No (Acc_Subprg) then
10734 return Designates_T (Subtype_Mark (Acc_Def));
10735 end if;
10736
10737 -- Component is an access_to_subprogram: examine its formals,
10738 -- and result definition in the case of an access_to_function.
10739
10740 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10741 while Present (Param_Spec) loop
10742 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10743 and then Mentions_T (Parameter_Type (Param_Spec))
10744 then
10745 return True;
10746
10747 elsif Designates_T (Parameter_Type (Param_Spec)) then
10748 return True;
10749 end if;
10750
10751 Next (Param_Spec);
10752 end loop;
10753
10754 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10755 if Nkind (Result_Definition (Acc_Subprg)) =
10756 N_Access_Definition
10757 then
10758 return Mentions_T (Result_Definition (Acc_Subprg));
10759 else
10760 return Designates_T (Result_Definition (Acc_Subprg));
10761 end if;
10762 end if;
10763
10764 return False;
10765 end Mentions_T;
10766
10767 -- Start of processing for Check_Anonymous_Access_Components
10768
10769 begin
10770 if No (Comp_List) then
10771 return;
10772 end if;
10773
10774 Comp := First (Component_Items (Comp_List));
10775 while Present (Comp) loop
10776 if Nkind (Comp) = N_Component_Declaration
10777 and then Present
10778 (Access_Definition (Component_Definition (Comp)))
10779 and then
10780 Mentions_T (Access_Definition (Component_Definition (Comp)))
10781 then
10782 Comp_Def := Component_Definition (Comp);
10783 Acc_Def :=
10784 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10785
10786 Build_Incomplete_Type_Declaration;
10787 Anon_Access := Make_Temporary (Loc, 'S');
10788
10789 -- Create a declaration for the anonymous access type: either
10790 -- an access_to_object or an access_to_subprogram.
10791
10792 if Present (Acc_Def) then
10793 if Nkind (Acc_Def) = N_Access_Function_Definition then
10794 Type_Def :=
10795 Make_Access_Function_Definition (Loc,
10796 Parameter_Specifications =>
10797 Parameter_Specifications (Acc_Def),
10798 Result_Definition => Result_Definition (Acc_Def));
10799 else
10800 Type_Def :=
10801 Make_Access_Procedure_Definition (Loc,
10802 Parameter_Specifications =>
10803 Parameter_Specifications (Acc_Def));
10804 end if;
10805
10806 else
10807 Type_Def :=
10808 Make_Access_To_Object_Definition (Loc,
10809 Subtype_Indication =>
10810 Relocate_Node
10811 (Subtype_Mark (Access_Definition (Comp_Def))));
10812
10813 Set_Constant_Present
10814 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10815 Set_All_Present
10816 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10817 end if;
10818
10819 Set_Null_Exclusion_Present
10820 (Type_Def,
10821 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10822
10823 Decl :=
10824 Make_Full_Type_Declaration (Loc,
10825 Defining_Identifier => Anon_Access,
10826 Type_Definition => Type_Def);
10827
10828 Insert_Before (Typ_Decl, Decl);
10829 Analyze (Decl);
10830
10831 -- If an access to subprogram, create the extra formals
10832
10833 if Present (Acc_Def) then
10834 Create_Extra_Formals (Designated_Type (Anon_Access));
10835
10836 -- If an access to object, preserve entity of designated type,
10837 -- for ASIS use, before rewriting the component definition.
10838
10839 else
10840 declare
10841 Desig : Entity_Id;
10842
10843 begin
10844 Desig := Entity (Subtype_Indication (Type_Def));
10845
10846 -- If the access definition is to the current record,
10847 -- the visible entity at this point is an incomplete
10848 -- type. Retrieve the full view to simplify ASIS queries
10849
10850 if Ekind (Desig) = E_Incomplete_Type then
10851 Desig := Full_View (Desig);
10852 end if;
10853
10854 Set_Entity
10855 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10856 end;
10857 end if;
10858
10859 Rewrite (Comp_Def,
10860 Make_Component_Definition (Loc,
10861 Subtype_Indication =>
10862 New_Occurrence_Of (Anon_Access, Loc)));
10863
10864 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10865 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10866 else
10867 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10868 end if;
10869
10870 Set_Is_Local_Anonymous_Access (Anon_Access);
10871 end if;
10872
10873 Next (Comp);
10874 end loop;
10875
10876 if Present (Variant_Part (Comp_List)) then
10877 declare
10878 V : Node_Id;
10879 begin
10880 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10881 while Present (V) loop
10882 Check_Anonymous_Access_Components
10883 (Typ_Decl, Typ, Prev, Component_List (V));
10884 Next_Non_Pragma (V);
10885 end loop;
10886 end;
10887 end if;
10888 end Check_Anonymous_Access_Components;
10889
10890 ----------------------
10891 -- Check_Completion --
10892 ----------------------
10893
10894 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10895 E : Entity_Id;
10896
10897 procedure Post_Error;
10898 -- Post error message for lack of completion for entity E
10899
10900 ----------------
10901 -- Post_Error --
10902 ----------------
10903
10904 procedure Post_Error is
10905 procedure Missing_Body;
10906 -- Output missing body message
10907
10908 ------------------
10909 -- Missing_Body --
10910 ------------------
10911
10912 procedure Missing_Body is
10913 begin
10914 -- Spec is in same unit, so we can post on spec
10915
10916 if In_Same_Source_Unit (Body_Id, E) then
10917 Error_Msg_N ("missing body for &", E);
10918
10919 -- Spec is in a separate unit, so we have to post on the body
10920
10921 else
10922 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10923 end if;
10924 end Missing_Body;
10925
10926 -- Start of processing for Post_Error
10927
10928 begin
10929 if not Comes_From_Source (E) then
10930 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10931
10932 -- It may be an anonymous protected type created for a
10933 -- single variable. Post error on variable, if present.
10934
10935 declare
10936 Var : Entity_Id;
10937
10938 begin
10939 Var := First_Entity (Current_Scope);
10940 while Present (Var) loop
10941 exit when Etype (Var) = E
10942 and then Comes_From_Source (Var);
10943
10944 Next_Entity (Var);
10945 end loop;
10946
10947 if Present (Var) then
10948 E := Var;
10949 end if;
10950 end;
10951 end if;
10952 end if;
10953
10954 -- If a generated entity has no completion, then either previous
10955 -- semantic errors have disabled the expansion phase, or else we had
10956 -- missing subunits, or else we are compiling without expansion,
10957 -- or else something is very wrong.
10958
10959 if not Comes_From_Source (E) then
10960 pragma Assert
10961 (Serious_Errors_Detected > 0
10962 or else Configurable_Run_Time_Violations > 0
10963 or else Subunits_Missing
10964 or else not Expander_Active);
10965 return;
10966
10967 -- Here for source entity
10968
10969 else
10970 -- Here if no body to post the error message, so we post the error
10971 -- on the declaration that has no completion. This is not really
10972 -- the right place to post it, think about this later ???
10973
10974 if No (Body_Id) then
10975 if Is_Type (E) then
10976 Error_Msg_NE
10977 ("missing full declaration for }", Parent (E), E);
10978 else
10979 Error_Msg_NE ("missing body for &", Parent (E), E);
10980 end if;
10981
10982 -- Package body has no completion for a declaration that appears
10983 -- in the corresponding spec. Post error on the body, with a
10984 -- reference to the non-completed declaration.
10985
10986 else
10987 Error_Msg_Sloc := Sloc (E);
10988
10989 if Is_Type (E) then
10990 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10991
10992 elsif Is_Overloadable (E)
10993 and then Current_Entity_In_Scope (E) /= E
10994 then
10995 -- It may be that the completion is mistyped and appears as
10996 -- a distinct overloading of the entity.
10997
10998 declare
10999 Candidate : constant Entity_Id :=
11000 Current_Entity_In_Scope (E);
11001 Decl : constant Node_Id :=
11002 Unit_Declaration_Node (Candidate);
11003
11004 begin
11005 if Is_Overloadable (Candidate)
11006 and then Ekind (Candidate) = Ekind (E)
11007 and then Nkind (Decl) = N_Subprogram_Body
11008 and then Acts_As_Spec (Decl)
11009 then
11010 Check_Type_Conformant (Candidate, E);
11011
11012 else
11013 Missing_Body;
11014 end if;
11015 end;
11016
11017 else
11018 Missing_Body;
11019 end if;
11020 end if;
11021 end if;
11022 end Post_Error;
11023
11024 -- Local variables
11025
11026 Pack_Id : constant Entity_Id := Current_Scope;
11027
11028 -- Start of processing for Check_Completion
11029
11030 begin
11031 E := First_Entity (Pack_Id);
11032 while Present (E) loop
11033 if Is_Intrinsic_Subprogram (E) then
11034 null;
11035
11036 -- The following situation requires special handling: a child unit
11037 -- that appears in the context clause of the body of its parent:
11038
11039 -- procedure Parent.Child (...);
11040
11041 -- with Parent.Child;
11042 -- package body Parent is
11043
11044 -- Here Parent.Child appears as a local entity, but should not be
11045 -- flagged as requiring completion, because it is a compilation
11046 -- unit.
11047
11048 -- Ignore missing completion for a subprogram that does not come from
11049 -- source (including the _Call primitive operation of RAS types,
11050 -- which has to have the flag Comes_From_Source for other purposes):
11051 -- we assume that the expander will provide the missing completion.
11052 -- In case of previous errors, other expansion actions that provide
11053 -- bodies for null procedures with not be invoked, so inhibit message
11054 -- in those cases.
11055
11056 -- Note that E_Operator is not in the list that follows, because
11057 -- this kind is reserved for predefined operators, that are
11058 -- intrinsic and do not need completion.
11059
11060 elsif Ekind_In (E, E_Function,
11061 E_Procedure,
11062 E_Generic_Function,
11063 E_Generic_Procedure)
11064 then
11065 if Has_Completion (E) then
11066 null;
11067
11068 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11069 null;
11070
11071 elsif Is_Subprogram (E)
11072 and then (not Comes_From_Source (E)
11073 or else Chars (E) = Name_uCall)
11074 then
11075 null;
11076
11077 elsif
11078 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11079 then
11080 null;
11081
11082 elsif Nkind (Parent (E)) = N_Procedure_Specification
11083 and then Null_Present (Parent (E))
11084 and then Serious_Errors_Detected > 0
11085 then
11086 null;
11087
11088 else
11089 Post_Error;
11090 end if;
11091
11092 elsif Is_Entry (E) then
11093 if not Has_Completion (E) and then
11094 (Ekind (Scope (E)) = E_Protected_Object
11095 or else Ekind (Scope (E)) = E_Protected_Type)
11096 then
11097 Post_Error;
11098 end if;
11099
11100 elsif Is_Package_Or_Generic_Package (E) then
11101 if Unit_Requires_Body (E) then
11102 if not Has_Completion (E)
11103 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11104 N_Compilation_Unit
11105 then
11106 Post_Error;
11107 end if;
11108
11109 elsif not Is_Child_Unit (E) then
11110 May_Need_Implicit_Body (E);
11111 end if;
11112
11113 -- A formal incomplete type (Ada 2012) does not require a completion;
11114 -- other incomplete type declarations do.
11115
11116 elsif Ekind (E) = E_Incomplete_Type
11117 and then No (Underlying_Type (E))
11118 and then not Is_Generic_Type (E)
11119 then
11120 Post_Error;
11121
11122 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11123 and then not Has_Completion (E)
11124 then
11125 Post_Error;
11126
11127 -- A single task declared in the current scope is a constant, verify
11128 -- that the body of its anonymous type is in the same scope. If the
11129 -- task is defined elsewhere, this may be a renaming declaration for
11130 -- which no completion is needed.
11131
11132 elsif Ekind (E) = E_Constant
11133 and then Ekind (Etype (E)) = E_Task_Type
11134 and then not Has_Completion (Etype (E))
11135 and then Scope (Etype (E)) = Current_Scope
11136 then
11137 Post_Error;
11138
11139 elsif Ekind (E) = E_Protected_Object
11140 and then not Has_Completion (Etype (E))
11141 then
11142 Post_Error;
11143
11144 elsif Ekind (E) = E_Record_Type then
11145 if Is_Tagged_Type (E) then
11146 Check_Abstract_Overriding (E);
11147 Check_Conventions (E);
11148 end if;
11149
11150 Check_Aliased_Component_Types (E);
11151
11152 elsif Ekind (E) = E_Array_Type then
11153 Check_Aliased_Component_Types (E);
11154
11155 end if;
11156
11157 Next_Entity (E);
11158 end loop;
11159 end Check_Completion;
11160
11161 ------------------------------------
11162 -- Check_CPP_Type_Has_No_Defaults --
11163 ------------------------------------
11164
11165 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11166 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11167 Clist : Node_Id;
11168 Comp : Node_Id;
11169
11170 begin
11171 -- Obtain the component list
11172
11173 if Nkind (Tdef) = N_Record_Definition then
11174 Clist := Component_List (Tdef);
11175 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11176 Clist := Component_List (Record_Extension_Part (Tdef));
11177 end if;
11178
11179 -- Check all components to ensure no default expressions
11180
11181 if Present (Clist) then
11182 Comp := First (Component_Items (Clist));
11183 while Present (Comp) loop
11184 if Present (Expression (Comp)) then
11185 Error_Msg_N
11186 ("component of imported 'C'P'P type cannot have "
11187 & "default expression", Expression (Comp));
11188 end if;
11189
11190 Next (Comp);
11191 end loop;
11192 end if;
11193 end Check_CPP_Type_Has_No_Defaults;
11194
11195 ----------------------------
11196 -- Check_Delta_Expression --
11197 ----------------------------
11198
11199 procedure Check_Delta_Expression (E : Node_Id) is
11200 begin
11201 if not (Is_Real_Type (Etype (E))) then
11202 Wrong_Type (E, Any_Real);
11203
11204 elsif not Is_OK_Static_Expression (E) then
11205 Flag_Non_Static_Expr
11206 ("non-static expression used for delta value!", E);
11207
11208 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11209 Error_Msg_N ("delta expression must be positive", E);
11210
11211 else
11212 return;
11213 end if;
11214
11215 -- If any of above errors occurred, then replace the incorrect
11216 -- expression by the real 0.1, which should prevent further errors.
11217
11218 Rewrite (E,
11219 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11220 Analyze_And_Resolve (E, Standard_Float);
11221 end Check_Delta_Expression;
11222
11223 -----------------------------
11224 -- Check_Digits_Expression --
11225 -----------------------------
11226
11227 procedure Check_Digits_Expression (E : Node_Id) is
11228 begin
11229 if not (Is_Integer_Type (Etype (E))) then
11230 Wrong_Type (E, Any_Integer);
11231
11232 elsif not Is_OK_Static_Expression (E) then
11233 Flag_Non_Static_Expr
11234 ("non-static expression used for digits value!", E);
11235
11236 elsif Expr_Value (E) <= 0 then
11237 Error_Msg_N ("digits value must be greater than zero", E);
11238
11239 else
11240 return;
11241 end if;
11242
11243 -- If any of above errors occurred, then replace the incorrect
11244 -- expression by the integer 1, which should prevent further errors.
11245
11246 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11247 Analyze_And_Resolve (E, Standard_Integer);
11248
11249 end Check_Digits_Expression;
11250
11251 --------------------------
11252 -- Check_Initialization --
11253 --------------------------
11254
11255 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11256 begin
11257 -- Special processing for limited types
11258
11259 if Is_Limited_Type (T)
11260 and then not In_Instance
11261 and then not In_Inlined_Body
11262 then
11263 if not OK_For_Limited_Init (T, Exp) then
11264
11265 -- In GNAT mode, this is just a warning, to allow it to be evilly
11266 -- turned off. Otherwise it is a real error.
11267
11268 if GNAT_Mode then
11269 Error_Msg_N
11270 ("??cannot initialize entities of limited type!", Exp);
11271
11272 elsif Ada_Version < Ada_2005 then
11273
11274 -- The side effect removal machinery may generate illegal Ada
11275 -- code to avoid the usage of access types and 'reference in
11276 -- SPARK mode. Since this is legal code with respect to theorem
11277 -- proving, do not emit the error.
11278
11279 if GNATprove_Mode
11280 and then Nkind (Exp) = N_Function_Call
11281 and then Nkind (Parent (Exp)) = N_Object_Declaration
11282 and then not Comes_From_Source
11283 (Defining_Identifier (Parent (Exp)))
11284 then
11285 null;
11286
11287 else
11288 Error_Msg_N
11289 ("cannot initialize entities of limited type", Exp);
11290 Explain_Limited_Type (T, Exp);
11291 end if;
11292
11293 else
11294 -- Specialize error message according to kind of illegal
11295 -- initial expression.
11296
11297 if Nkind (Exp) = N_Type_Conversion
11298 and then Nkind (Expression (Exp)) = N_Function_Call
11299 then
11300 Error_Msg_N
11301 ("illegal context for call"
11302 & " to function with limited result", Exp);
11303
11304 else
11305 Error_Msg_N
11306 ("initialization of limited object requires aggregate "
11307 & "or function call", Exp);
11308 end if;
11309 end if;
11310 end if;
11311 end if;
11312
11313 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11314 -- set unless we can be sure that no range check is required.
11315
11316 if (GNATprove_Mode or not Expander_Active)
11317 and then Is_Scalar_Type (T)
11318 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11319 then
11320 Set_Do_Range_Check (Exp);
11321 end if;
11322 end Check_Initialization;
11323
11324 ----------------------
11325 -- Check_Interfaces --
11326 ----------------------
11327
11328 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11329 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11330
11331 Iface : Node_Id;
11332 Iface_Def : Node_Id;
11333 Iface_Typ : Entity_Id;
11334 Parent_Node : Node_Id;
11335
11336 Is_Task : Boolean := False;
11337 -- Set True if parent type or any progenitor is a task interface
11338
11339 Is_Protected : Boolean := False;
11340 -- Set True if parent type or any progenitor is a protected interface
11341
11342 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11343 -- Check that a progenitor is compatible with declaration. If an error
11344 -- message is output, it is posted on Error_Node.
11345
11346 ------------------
11347 -- Check_Ifaces --
11348 ------------------
11349
11350 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11351 Iface_Id : constant Entity_Id :=
11352 Defining_Identifier (Parent (Iface_Def));
11353 Type_Def : Node_Id;
11354
11355 begin
11356 if Nkind (N) = N_Private_Extension_Declaration then
11357 Type_Def := N;
11358 else
11359 Type_Def := Type_Definition (N);
11360 end if;
11361
11362 if Is_Task_Interface (Iface_Id) then
11363 Is_Task := True;
11364
11365 elsif Is_Protected_Interface (Iface_Id) then
11366 Is_Protected := True;
11367 end if;
11368
11369 if Is_Synchronized_Interface (Iface_Id) then
11370
11371 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11372 -- extension derived from a synchronized interface must explicitly
11373 -- be declared synchronized, because the full view will be a
11374 -- synchronized type.
11375
11376 if Nkind (N) = N_Private_Extension_Declaration then
11377 if not Synchronized_Present (N) then
11378 Error_Msg_NE
11379 ("private extension of& must be explicitly synchronized",
11380 N, Iface_Id);
11381 end if;
11382
11383 -- However, by 3.9.4(16/2), a full type that is a record extension
11384 -- is never allowed to derive from a synchronized interface (note
11385 -- that interfaces must be excluded from this check, because those
11386 -- are represented by derived type definitions in some cases).
11387
11388 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11389 and then not Interface_Present (Type_Definition (N))
11390 then
11391 Error_Msg_N ("record extension cannot derive from synchronized "
11392 & "interface", Error_Node);
11393 end if;
11394 end if;
11395
11396 -- Check that the characteristics of the progenitor are compatible
11397 -- with the explicit qualifier in the declaration.
11398 -- The check only applies to qualifiers that come from source.
11399 -- Limited_Present also appears in the declaration of corresponding
11400 -- records, and the check does not apply to them.
11401
11402 if Limited_Present (Type_Def)
11403 and then not
11404 Is_Concurrent_Record_Type (Defining_Identifier (N))
11405 then
11406 if Is_Limited_Interface (Parent_Type)
11407 and then not Is_Limited_Interface (Iface_Id)
11408 then
11409 Error_Msg_NE
11410 ("progenitor & must be limited interface",
11411 Error_Node, Iface_Id);
11412
11413 elsif
11414 (Task_Present (Iface_Def)
11415 or else Protected_Present (Iface_Def)
11416 or else Synchronized_Present (Iface_Def))
11417 and then Nkind (N) /= N_Private_Extension_Declaration
11418 and then not Error_Posted (N)
11419 then
11420 Error_Msg_NE
11421 ("progenitor & must be limited interface",
11422 Error_Node, Iface_Id);
11423 end if;
11424
11425 -- Protected interfaces can only inherit from limited, synchronized
11426 -- or protected interfaces.
11427
11428 elsif Nkind (N) = N_Full_Type_Declaration
11429 and then Protected_Present (Type_Def)
11430 then
11431 if Limited_Present (Iface_Def)
11432 or else Synchronized_Present (Iface_Def)
11433 or else Protected_Present (Iface_Def)
11434 then
11435 null;
11436
11437 elsif Task_Present (Iface_Def) then
11438 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11439 & "from task interface", Error_Node);
11440
11441 else
11442 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11443 & "from non-limited interface", Error_Node);
11444 end if;
11445
11446 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11447 -- limited and synchronized.
11448
11449 elsif Synchronized_Present (Type_Def) then
11450 if Limited_Present (Iface_Def)
11451 or else Synchronized_Present (Iface_Def)
11452 then
11453 null;
11454
11455 elsif Protected_Present (Iface_Def)
11456 and then Nkind (N) /= N_Private_Extension_Declaration
11457 then
11458 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11459 & "from protected interface", Error_Node);
11460
11461 elsif Task_Present (Iface_Def)
11462 and then Nkind (N) /= N_Private_Extension_Declaration
11463 then
11464 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11465 & "from task interface", Error_Node);
11466
11467 elsif not Is_Limited_Interface (Iface_Id) then
11468 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11469 & "from non-limited interface", Error_Node);
11470 end if;
11471
11472 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11473 -- synchronized or task interfaces.
11474
11475 elsif Nkind (N) = N_Full_Type_Declaration
11476 and then Task_Present (Type_Def)
11477 then
11478 if Limited_Present (Iface_Def)
11479 or else Synchronized_Present (Iface_Def)
11480 or else Task_Present (Iface_Def)
11481 then
11482 null;
11483
11484 elsif Protected_Present (Iface_Def) then
11485 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11486 & "protected interface", Error_Node);
11487
11488 else
11489 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11490 & "non-limited interface", Error_Node);
11491 end if;
11492 end if;
11493 end Check_Ifaces;
11494
11495 -- Start of processing for Check_Interfaces
11496
11497 begin
11498 if Is_Interface (Parent_Type) then
11499 if Is_Task_Interface (Parent_Type) then
11500 Is_Task := True;
11501
11502 elsif Is_Protected_Interface (Parent_Type) then
11503 Is_Protected := True;
11504 end if;
11505 end if;
11506
11507 if Nkind (N) = N_Private_Extension_Declaration then
11508
11509 -- Check that progenitors are compatible with declaration
11510
11511 Iface := First (Interface_List (Def));
11512 while Present (Iface) loop
11513 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11514
11515 Parent_Node := Parent (Base_Type (Iface_Typ));
11516 Iface_Def := Type_Definition (Parent_Node);
11517
11518 if not Is_Interface (Iface_Typ) then
11519 Diagnose_Interface (Iface, Iface_Typ);
11520 else
11521 Check_Ifaces (Iface_Def, Iface);
11522 end if;
11523
11524 Next (Iface);
11525 end loop;
11526
11527 if Is_Task and Is_Protected then
11528 Error_Msg_N
11529 ("type cannot derive from task and protected interface", N);
11530 end if;
11531
11532 return;
11533 end if;
11534
11535 -- Full type declaration of derived type.
11536 -- Check compatibility with parent if it is interface type
11537
11538 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11539 and then Is_Interface (Parent_Type)
11540 then
11541 Parent_Node := Parent (Parent_Type);
11542
11543 -- More detailed checks for interface varieties
11544
11545 Check_Ifaces
11546 (Iface_Def => Type_Definition (Parent_Node),
11547 Error_Node => Subtype_Indication (Type_Definition (N)));
11548 end if;
11549
11550 Iface := First (Interface_List (Def));
11551 while Present (Iface) loop
11552 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11553
11554 Parent_Node := Parent (Base_Type (Iface_Typ));
11555 Iface_Def := Type_Definition (Parent_Node);
11556
11557 if not Is_Interface (Iface_Typ) then
11558 Diagnose_Interface (Iface, Iface_Typ);
11559
11560 else
11561 -- "The declaration of a specific descendant of an interface
11562 -- type freezes the interface type" RM 13.14
11563
11564 Freeze_Before (N, Iface_Typ);
11565 Check_Ifaces (Iface_Def, Error_Node => Iface);
11566 end if;
11567
11568 Next (Iface);
11569 end loop;
11570
11571 if Is_Task and Is_Protected then
11572 Error_Msg_N
11573 ("type cannot derive from task and protected interface", N);
11574 end if;
11575 end Check_Interfaces;
11576
11577 ------------------------------------
11578 -- Check_Or_Process_Discriminants --
11579 ------------------------------------
11580
11581 -- If an incomplete or private type declaration was already given for the
11582 -- type, the discriminants may have already been processed if they were
11583 -- present on the incomplete declaration. In this case a full conformance
11584 -- check has been performed in Find_Type_Name, and we then recheck here
11585 -- some properties that can't be checked on the partial view alone.
11586 -- Otherwise we call Process_Discriminants.
11587
11588 procedure Check_Or_Process_Discriminants
11589 (N : Node_Id;
11590 T : Entity_Id;
11591 Prev : Entity_Id := Empty)
11592 is
11593 begin
11594 if Has_Discriminants (T) then
11595
11596 -- Discriminants are already set on T if they were already present
11597 -- on the partial view. Make them visible to component declarations.
11598
11599 declare
11600 D : Entity_Id;
11601 -- Discriminant on T (full view) referencing expr on partial view
11602
11603 Prev_D : Entity_Id;
11604 -- Entity of corresponding discriminant on partial view
11605
11606 New_D : Node_Id;
11607 -- Discriminant specification for full view, expression is
11608 -- the syntactic copy on full view (which has been checked for
11609 -- conformance with partial view), only used here to post error
11610 -- message.
11611
11612 begin
11613 D := First_Discriminant (T);
11614 New_D := First (Discriminant_Specifications (N));
11615 while Present (D) loop
11616 Prev_D := Current_Entity (D);
11617 Set_Current_Entity (D);
11618 Set_Is_Immediately_Visible (D);
11619 Set_Homonym (D, Prev_D);
11620
11621 -- Handle the case where there is an untagged partial view and
11622 -- the full view is tagged: must disallow discriminants with
11623 -- defaults, unless compiling for Ada 2012, which allows a
11624 -- limited tagged type to have defaulted discriminants (see
11625 -- AI05-0214). However, suppress error here if it was already
11626 -- reported on the default expression of the partial view.
11627
11628 if Is_Tagged_Type (T)
11629 and then Present (Expression (Parent (D)))
11630 and then (not Is_Limited_Type (Current_Scope)
11631 or else Ada_Version < Ada_2012)
11632 and then not Error_Posted (Expression (Parent (D)))
11633 then
11634 if Ada_Version >= Ada_2012 then
11635 Error_Msg_N
11636 ("discriminants of nonlimited tagged type cannot have "
11637 & "defaults",
11638 Expression (New_D));
11639 else
11640 Error_Msg_N
11641 ("discriminants of tagged type cannot have defaults",
11642 Expression (New_D));
11643 end if;
11644 end if;
11645
11646 -- Ada 2005 (AI-230): Access discriminant allowed in
11647 -- non-limited record types.
11648
11649 if Ada_Version < Ada_2005 then
11650
11651 -- This restriction gets applied to the full type here. It
11652 -- has already been applied earlier to the partial view.
11653
11654 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11655 end if;
11656
11657 Next_Discriminant (D);
11658 Next (New_D);
11659 end loop;
11660 end;
11661
11662 elsif Present (Discriminant_Specifications (N)) then
11663 Process_Discriminants (N, Prev);
11664 end if;
11665 end Check_Or_Process_Discriminants;
11666
11667 ----------------------
11668 -- Check_Real_Bound --
11669 ----------------------
11670
11671 procedure Check_Real_Bound (Bound : Node_Id) is
11672 begin
11673 if not Is_Real_Type (Etype (Bound)) then
11674 Error_Msg_N
11675 ("bound in real type definition must be of real type", Bound);
11676
11677 elsif not Is_OK_Static_Expression (Bound) then
11678 Flag_Non_Static_Expr
11679 ("non-static expression used for real type bound!", Bound);
11680
11681 else
11682 return;
11683 end if;
11684
11685 Rewrite
11686 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11687 Analyze (Bound);
11688 Resolve (Bound, Standard_Float);
11689 end Check_Real_Bound;
11690
11691 ------------------------------
11692 -- Complete_Private_Subtype --
11693 ------------------------------
11694
11695 procedure Complete_Private_Subtype
11696 (Priv : Entity_Id;
11697 Full : Entity_Id;
11698 Full_Base : Entity_Id;
11699 Related_Nod : Node_Id)
11700 is
11701 Save_Next_Entity : Entity_Id;
11702 Save_Homonym : Entity_Id;
11703
11704 begin
11705 -- Set semantic attributes for (implicit) private subtype completion.
11706 -- If the full type has no discriminants, then it is a copy of the
11707 -- full view of the base. Otherwise, it is a subtype of the base with
11708 -- a possible discriminant constraint. Save and restore the original
11709 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11710 -- not corrupt the entity chain.
11711
11712 -- Note that the type of the full view is the same entity as the type
11713 -- of the partial view. In this fashion, the subtype has access to the
11714 -- correct view of the parent.
11715
11716 Save_Next_Entity := Next_Entity (Full);
11717 Save_Homonym := Homonym (Priv);
11718
11719 case Ekind (Full_Base) is
11720 when E_Record_Type |
11721 E_Record_Subtype |
11722 Class_Wide_Kind |
11723 Private_Kind |
11724 Task_Kind |
11725 Protected_Kind =>
11726 Copy_Node (Priv, Full);
11727
11728 Set_Has_Discriminants
11729 (Full, Has_Discriminants (Full_Base));
11730 Set_Has_Unknown_Discriminants
11731 (Full, Has_Unknown_Discriminants (Full_Base));
11732 Set_First_Entity (Full, First_Entity (Full_Base));
11733 Set_Last_Entity (Full, Last_Entity (Full_Base));
11734
11735 -- If the underlying base type is constrained, we know that the
11736 -- full view of the subtype is constrained as well (the converse
11737 -- is not necessarily true).
11738
11739 if Is_Constrained (Full_Base) then
11740 Set_Is_Constrained (Full);
11741 end if;
11742
11743 when others =>
11744 Copy_Node (Full_Base, Full);
11745
11746 Set_Chars (Full, Chars (Priv));
11747 Conditional_Delay (Full, Priv);
11748 Set_Sloc (Full, Sloc (Priv));
11749 end case;
11750
11751 Set_Next_Entity (Full, Save_Next_Entity);
11752 Set_Homonym (Full, Save_Homonym);
11753 Set_Associated_Node_For_Itype (Full, Related_Nod);
11754
11755 -- Set common attributes for all subtypes: kind, convention, etc.
11756
11757 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11758 Set_Convention (Full, Convention (Full_Base));
11759
11760 -- The Etype of the full view is inconsistent. Gigi needs to see the
11761 -- structural full view, which is what the current scheme gives: the
11762 -- Etype of the full view is the etype of the full base. However, if the
11763 -- full base is a derived type, the full view then looks like a subtype
11764 -- of the parent, not a subtype of the full base. If instead we write:
11765
11766 -- Set_Etype (Full, Full_Base);
11767
11768 -- then we get inconsistencies in the front-end (confusion between
11769 -- views). Several outstanding bugs are related to this ???
11770
11771 Set_Is_First_Subtype (Full, False);
11772 Set_Scope (Full, Scope (Priv));
11773 Set_Size_Info (Full, Full_Base);
11774 Set_RM_Size (Full, RM_Size (Full_Base));
11775 Set_Is_Itype (Full);
11776
11777 -- A subtype of a private-type-without-discriminants, whose full-view
11778 -- has discriminants with default expressions, is not constrained.
11779
11780 if not Has_Discriminants (Priv) then
11781 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11782
11783 if Has_Discriminants (Full_Base) then
11784 Set_Discriminant_Constraint
11785 (Full, Discriminant_Constraint (Full_Base));
11786
11787 -- The partial view may have been indefinite, the full view
11788 -- might not be.
11789
11790 Set_Has_Unknown_Discriminants
11791 (Full, Has_Unknown_Discriminants (Full_Base));
11792 end if;
11793 end if;
11794
11795 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11796 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11797
11798 -- Freeze the private subtype entity if its parent is delayed, and not
11799 -- already frozen. We skip this processing if the type is an anonymous
11800 -- subtype of a record component, or is the corresponding record of a
11801 -- protected type, since these are processed when the enclosing type
11802 -- is frozen.
11803
11804 if not Is_Type (Scope (Full)) then
11805 Set_Has_Delayed_Freeze (Full,
11806 Has_Delayed_Freeze (Full_Base)
11807 and then (not Is_Frozen (Full_Base)));
11808 end if;
11809
11810 Set_Freeze_Node (Full, Empty);
11811 Set_Is_Frozen (Full, False);
11812 Set_Full_View (Priv, Full);
11813
11814 if Has_Discriminants (Full) then
11815 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11816 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11817
11818 if Has_Unknown_Discriminants (Full) then
11819 Set_Discriminant_Constraint (Full, No_Elist);
11820 end if;
11821 end if;
11822
11823 if Ekind (Full_Base) = E_Record_Type
11824 and then Has_Discriminants (Full_Base)
11825 and then Has_Discriminants (Priv) -- might not, if errors
11826 and then not Has_Unknown_Discriminants (Priv)
11827 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11828 then
11829 Create_Constrained_Components
11830 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11831
11832 -- If the full base is itself derived from private, build a congruent
11833 -- subtype of its underlying type, for use by the back end. For a
11834 -- constrained record component, the declaration cannot be placed on
11835 -- the component list, but it must nevertheless be built an analyzed, to
11836 -- supply enough information for Gigi to compute the size of component.
11837
11838 elsif Ekind (Full_Base) in Private_Kind
11839 and then Is_Derived_Type (Full_Base)
11840 and then Has_Discriminants (Full_Base)
11841 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11842 then
11843 if not Is_Itype (Priv)
11844 and then
11845 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11846 then
11847 Build_Underlying_Full_View
11848 (Parent (Priv), Full, Etype (Full_Base));
11849
11850 elsif Nkind (Related_Nod) = N_Component_Declaration then
11851 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11852 end if;
11853
11854 elsif Is_Record_Type (Full_Base) then
11855
11856 -- Show Full is simply a renaming of Full_Base
11857
11858 Set_Cloned_Subtype (Full, Full_Base);
11859 end if;
11860
11861 -- It is unsafe to share the bounds of a scalar type, because the Itype
11862 -- is elaborated on demand, and if a bound is non-static then different
11863 -- orders of elaboration in different units will lead to different
11864 -- external symbols.
11865
11866 if Is_Scalar_Type (Full_Base) then
11867 Set_Scalar_Range (Full,
11868 Make_Range (Sloc (Related_Nod),
11869 Low_Bound =>
11870 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11871 High_Bound =>
11872 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11873
11874 -- This completion inherits the bounds of the full parent, but if
11875 -- the parent is an unconstrained floating point type, so is the
11876 -- completion.
11877
11878 if Is_Floating_Point_Type (Full_Base) then
11879 Set_Includes_Infinities
11880 (Scalar_Range (Full), Has_Infinities (Full_Base));
11881 end if;
11882 end if;
11883
11884 -- ??? It seems that a lot of fields are missing that should be copied
11885 -- from Full_Base to Full. Here are some that are introduced in a
11886 -- non-disruptive way but a cleanup is necessary.
11887
11888 if Is_Tagged_Type (Full_Base) then
11889 Set_Is_Tagged_Type (Full);
11890 Set_Direct_Primitive_Operations
11891 (Full, Direct_Primitive_Operations (Full_Base));
11892 Set_No_Tagged_Streams_Pragma
11893 (Full, No_Tagged_Streams_Pragma (Full_Base));
11894
11895 -- Inherit class_wide type of full_base in case the partial view was
11896 -- not tagged. Otherwise it has already been created when the private
11897 -- subtype was analyzed.
11898
11899 if No (Class_Wide_Type (Full)) then
11900 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11901 end if;
11902
11903 -- If this is a subtype of a protected or task type, constrain its
11904 -- corresponding record, unless this is a subtype without constraints,
11905 -- i.e. a simple renaming as with an actual subtype in an instance.
11906
11907 elsif Is_Concurrent_Type (Full_Base) then
11908 if Has_Discriminants (Full)
11909 and then Present (Corresponding_Record_Type (Full_Base))
11910 and then
11911 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11912 then
11913 Set_Corresponding_Record_Type (Full,
11914 Constrain_Corresponding_Record
11915 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11916
11917 else
11918 Set_Corresponding_Record_Type (Full,
11919 Corresponding_Record_Type (Full_Base));
11920 end if;
11921 end if;
11922
11923 -- Link rep item chain, and also setting of Has_Predicates from private
11924 -- subtype to full subtype, since we will need these on the full subtype
11925 -- to create the predicate function. Note that the full subtype may
11926 -- already have rep items, inherited from the full view of the base
11927 -- type, so we must be sure not to overwrite these entries.
11928
11929 declare
11930 Append : Boolean;
11931 Item : Node_Id;
11932 Next_Item : Node_Id;
11933 Priv_Item : Node_Id;
11934
11935 begin
11936 Item := First_Rep_Item (Full);
11937 Priv_Item := First_Rep_Item (Priv);
11938
11939 -- If no existing rep items on full type, we can just link directly
11940 -- to the list of items on the private type, if any exist.. Same if
11941 -- the rep items are only those inherited from the base
11942
11943 if (No (Item)
11944 or else Nkind (Item) /= N_Aspect_Specification
11945 or else Entity (Item) = Full_Base)
11946 and then Present (First_Rep_Item (Priv))
11947 then
11948 Set_First_Rep_Item (Full, Priv_Item);
11949
11950 -- Otherwise, search to the end of items currently linked to the full
11951 -- subtype and append the private items to the end. However, if Priv
11952 -- and Full already have the same list of rep items, then the append
11953 -- is not done, as that would create a circularity.
11954 --
11955 -- The partial view may have a predicate and the rep item lists of
11956 -- both views agree when inherited from the same ancestor. In that
11957 -- case, simply propagate the list from one view to the other.
11958 -- A more complex analysis needed here ???
11959
11960 elsif Present (Priv_Item)
11961 and then Item = Next_Rep_Item (Priv_Item)
11962 then
11963 Set_First_Rep_Item (Full, Priv_Item);
11964
11965 elsif Item /= Priv_Item then
11966 Append := True;
11967 loop
11968 Next_Item := Next_Rep_Item (Item);
11969 exit when No (Next_Item);
11970 Item := Next_Item;
11971
11972 -- If the private view has aspect specifications, the full view
11973 -- inherits them. Since these aspects may already have been
11974 -- attached to the full view during derivation, do not append
11975 -- them if already present.
11976
11977 if Item = First_Rep_Item (Priv) then
11978 Append := False;
11979 exit;
11980 end if;
11981 end loop;
11982
11983 -- And link the private type items at the end of the chain
11984
11985 if Append then
11986 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11987 end if;
11988 end if;
11989 end;
11990
11991 -- Make sure Has_Predicates is set on full type if it is set on the
11992 -- private type. Note that it may already be set on the full type and
11993 -- if so, we don't want to unset it. Similarly, propagate information
11994 -- about delayed aspects, because the corresponding pragmas must be
11995 -- analyzed when one of the views is frozen. This last step is needed
11996 -- in particular when the full type is a scalar type for which an
11997 -- anonymous base type is constructed.
11998
11999 -- The predicate functions are generated either at the freeze point
12000 -- of the type or at the end of the visible part, and we must avoid
12001 -- generating them twice.
12002
12003 if Has_Predicates (Priv) then
12004 Set_Has_Predicates (Full);
12005
12006 if Present (Predicate_Function (Priv))
12007 and then No (Predicate_Function (Full))
12008 then
12009 Set_Predicate_Function (Full, Predicate_Function (Priv));
12010 end if;
12011 end if;
12012
12013 if Has_Delayed_Aspects (Priv) then
12014 Set_Has_Delayed_Aspects (Full);
12015 end if;
12016 end Complete_Private_Subtype;
12017
12018 ----------------------------
12019 -- Constant_Redeclaration --
12020 ----------------------------
12021
12022 procedure Constant_Redeclaration
12023 (Id : Entity_Id;
12024 N : Node_Id;
12025 T : out Entity_Id)
12026 is
12027 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12028 Obj_Def : constant Node_Id := Object_Definition (N);
12029 New_T : Entity_Id;
12030
12031 procedure Check_Possible_Deferred_Completion
12032 (Prev_Id : Entity_Id;
12033 Prev_Obj_Def : Node_Id;
12034 Curr_Obj_Def : Node_Id);
12035 -- Determine whether the two object definitions describe the partial
12036 -- and the full view of a constrained deferred constant. Generate
12037 -- a subtype for the full view and verify that it statically matches
12038 -- the subtype of the partial view.
12039
12040 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12041 -- If deferred constant is an access type initialized with an allocator,
12042 -- check whether there is an illegal recursion in the definition,
12043 -- through a default value of some record subcomponent. This is normally
12044 -- detected when generating init procs, but requires this additional
12045 -- mechanism when expansion is disabled.
12046
12047 ----------------------------------------
12048 -- Check_Possible_Deferred_Completion --
12049 ----------------------------------------
12050
12051 procedure Check_Possible_Deferred_Completion
12052 (Prev_Id : Entity_Id;
12053 Prev_Obj_Def : Node_Id;
12054 Curr_Obj_Def : Node_Id)
12055 is
12056 begin
12057 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12058 and then Present (Constraint (Prev_Obj_Def))
12059 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12060 and then Present (Constraint (Curr_Obj_Def))
12061 then
12062 declare
12063 Loc : constant Source_Ptr := Sloc (N);
12064 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12065 Decl : constant Node_Id :=
12066 Make_Subtype_Declaration (Loc,
12067 Defining_Identifier => Def_Id,
12068 Subtype_Indication =>
12069 Relocate_Node (Curr_Obj_Def));
12070
12071 begin
12072 Insert_Before_And_Analyze (N, Decl);
12073 Set_Etype (Id, Def_Id);
12074
12075 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12076 Error_Msg_Sloc := Sloc (Prev_Id);
12077 Error_Msg_N ("subtype does not statically match deferred "
12078 & "declaration #", N);
12079 end if;
12080 end;
12081 end if;
12082 end Check_Possible_Deferred_Completion;
12083
12084 ---------------------------------
12085 -- Check_Recursive_Declaration --
12086 ---------------------------------
12087
12088 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12089 Comp : Entity_Id;
12090
12091 begin
12092 if Is_Record_Type (Typ) then
12093 Comp := First_Component (Typ);
12094 while Present (Comp) loop
12095 if Comes_From_Source (Comp) then
12096 if Present (Expression (Parent (Comp)))
12097 and then Is_Entity_Name (Expression (Parent (Comp)))
12098 and then Entity (Expression (Parent (Comp))) = Prev
12099 then
12100 Error_Msg_Sloc := Sloc (Parent (Comp));
12101 Error_Msg_NE
12102 ("illegal circularity with declaration for & #",
12103 N, Comp);
12104 return;
12105
12106 elsif Is_Record_Type (Etype (Comp)) then
12107 Check_Recursive_Declaration (Etype (Comp));
12108 end if;
12109 end if;
12110
12111 Next_Component (Comp);
12112 end loop;
12113 end if;
12114 end Check_Recursive_Declaration;
12115
12116 -- Start of processing for Constant_Redeclaration
12117
12118 begin
12119 if Nkind (Parent (Prev)) = N_Object_Declaration then
12120 if Nkind (Object_Definition
12121 (Parent (Prev))) = N_Subtype_Indication
12122 then
12123 -- Find type of new declaration. The constraints of the two
12124 -- views must match statically, but there is no point in
12125 -- creating an itype for the full view.
12126
12127 if Nkind (Obj_Def) = N_Subtype_Indication then
12128 Find_Type (Subtype_Mark (Obj_Def));
12129 New_T := Entity (Subtype_Mark (Obj_Def));
12130
12131 else
12132 Find_Type (Obj_Def);
12133 New_T := Entity (Obj_Def);
12134 end if;
12135
12136 T := Etype (Prev);
12137
12138 else
12139 -- The full view may impose a constraint, even if the partial
12140 -- view does not, so construct the subtype.
12141
12142 New_T := Find_Type_Of_Object (Obj_Def, N);
12143 T := New_T;
12144 end if;
12145
12146 else
12147 -- Current declaration is illegal, diagnosed below in Enter_Name
12148
12149 T := Empty;
12150 New_T := Any_Type;
12151 end if;
12152
12153 -- If previous full declaration or a renaming declaration exists, or if
12154 -- a homograph is present, let Enter_Name handle it, either with an
12155 -- error or with the removal of an overridden implicit subprogram.
12156 -- The previous one is a full declaration if it has an expression
12157 -- (which in the case of an aggregate is indicated by the Init flag).
12158
12159 if Ekind (Prev) /= E_Constant
12160 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12161 or else Present (Expression (Parent (Prev)))
12162 or else Has_Init_Expression (Parent (Prev))
12163 or else Present (Full_View (Prev))
12164 then
12165 Enter_Name (Id);
12166
12167 -- Verify that types of both declarations match, or else that both types
12168 -- are anonymous access types whose designated subtypes statically match
12169 -- (as allowed in Ada 2005 by AI-385).
12170
12171 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12172 and then
12173 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12174 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12175 or else Is_Access_Constant (Etype (New_T)) /=
12176 Is_Access_Constant (Etype (Prev))
12177 or else Can_Never_Be_Null (Etype (New_T)) /=
12178 Can_Never_Be_Null (Etype (Prev))
12179 or else Null_Exclusion_Present (Parent (Prev)) /=
12180 Null_Exclusion_Present (Parent (Id))
12181 or else not Subtypes_Statically_Match
12182 (Designated_Type (Etype (Prev)),
12183 Designated_Type (Etype (New_T))))
12184 then
12185 Error_Msg_Sloc := Sloc (Prev);
12186 Error_Msg_N ("type does not match declaration#", N);
12187 Set_Full_View (Prev, Id);
12188 Set_Etype (Id, Any_Type);
12189
12190 -- A deferred constant whose type is an anonymous array is always
12191 -- illegal (unless imported). A detailed error message might be
12192 -- helpful for Ada beginners.
12193
12194 if Nkind (Object_Definition (Parent (Prev)))
12195 = N_Constrained_Array_Definition
12196 and then Nkind (Object_Definition (N))
12197 = N_Constrained_Array_Definition
12198 then
12199 Error_Msg_N ("\each anonymous array is a distinct type", N);
12200 Error_Msg_N ("a deferred constant must have a named type",
12201 Object_Definition (Parent (Prev)));
12202 end if;
12203
12204 elsif
12205 Null_Exclusion_Present (Parent (Prev))
12206 and then not Null_Exclusion_Present (N)
12207 then
12208 Error_Msg_Sloc := Sloc (Prev);
12209 Error_Msg_N ("null-exclusion does not match declaration#", N);
12210 Set_Full_View (Prev, Id);
12211 Set_Etype (Id, Any_Type);
12212
12213 -- If so, process the full constant declaration
12214
12215 else
12216 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12217 -- the deferred declaration is constrained, then the subtype defined
12218 -- by the subtype_indication in the full declaration shall match it
12219 -- statically.
12220
12221 Check_Possible_Deferred_Completion
12222 (Prev_Id => Prev,
12223 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12224 Curr_Obj_Def => Obj_Def);
12225
12226 Set_Full_View (Prev, Id);
12227 Set_Is_Public (Id, Is_Public (Prev));
12228 Set_Is_Internal (Id);
12229 Append_Entity (Id, Current_Scope);
12230
12231 -- Check ALIASED present if present before (RM 7.4(7))
12232
12233 if Is_Aliased (Prev)
12234 and then not Aliased_Present (N)
12235 then
12236 Error_Msg_Sloc := Sloc (Prev);
12237 Error_Msg_N ("ALIASED required (see declaration #)", N);
12238 end if;
12239
12240 -- Check that placement is in private part and that the incomplete
12241 -- declaration appeared in the visible part.
12242
12243 if Ekind (Current_Scope) = E_Package
12244 and then not In_Private_Part (Current_Scope)
12245 then
12246 Error_Msg_Sloc := Sloc (Prev);
12247 Error_Msg_N
12248 ("full constant for declaration # must be in private part", N);
12249
12250 elsif Ekind (Current_Scope) = E_Package
12251 and then
12252 List_Containing (Parent (Prev)) /=
12253 Visible_Declarations (Package_Specification (Current_Scope))
12254 then
12255 Error_Msg_N
12256 ("deferred constant must be declared in visible part",
12257 Parent (Prev));
12258 end if;
12259
12260 if Is_Access_Type (T)
12261 and then Nkind (Expression (N)) = N_Allocator
12262 then
12263 Check_Recursive_Declaration (Designated_Type (T));
12264 end if;
12265
12266 -- A deferred constant is a visible entity. If type has invariants,
12267 -- verify that the initial value satisfies them.
12268
12269 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12270 Insert_After (N,
12271 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12272 end if;
12273 end if;
12274 end Constant_Redeclaration;
12275
12276 ----------------------
12277 -- Constrain_Access --
12278 ----------------------
12279
12280 procedure Constrain_Access
12281 (Def_Id : in out Entity_Id;
12282 S : Node_Id;
12283 Related_Nod : Node_Id)
12284 is
12285 T : constant Entity_Id := Entity (Subtype_Mark (S));
12286 Desig_Type : constant Entity_Id := Designated_Type (T);
12287 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12288 Constraint_OK : Boolean := True;
12289
12290 begin
12291 if Is_Array_Type (Desig_Type) then
12292 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12293
12294 elsif (Is_Record_Type (Desig_Type)
12295 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12296 and then not Is_Constrained (Desig_Type)
12297 then
12298 -- ??? The following code is a temporary bypass to ignore a
12299 -- discriminant constraint on access type if it is constraining
12300 -- the current record. Avoid creating the implicit subtype of the
12301 -- record we are currently compiling since right now, we cannot
12302 -- handle these. For now, just return the access type itself.
12303
12304 if Desig_Type = Current_Scope
12305 and then No (Def_Id)
12306 then
12307 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12308 Def_Id := Entity (Subtype_Mark (S));
12309
12310 -- This call added to ensure that the constraint is analyzed
12311 -- (needed for a B test). Note that we still return early from
12312 -- this procedure to avoid recursive processing. ???
12313
12314 Constrain_Discriminated_Type
12315 (Desig_Subtype, S, Related_Nod, For_Access => True);
12316 return;
12317 end if;
12318
12319 -- Enforce rule that the constraint is illegal if there is an
12320 -- unconstrained view of the designated type. This means that the
12321 -- partial view (either a private type declaration or a derivation
12322 -- from a private type) has no discriminants. (Defect Report
12323 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12324
12325 -- Rule updated for Ada 2005: The private type is said to have
12326 -- a constrained partial view, given that objects of the type
12327 -- can be declared. Furthermore, the rule applies to all access
12328 -- types, unlike the rule concerning default discriminants (see
12329 -- RM 3.7.1(7/3))
12330
12331 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12332 and then Has_Private_Declaration (Desig_Type)
12333 and then In_Open_Scopes (Scope (Desig_Type))
12334 and then Has_Discriminants (Desig_Type)
12335 then
12336 declare
12337 Pack : constant Node_Id :=
12338 Unit_Declaration_Node (Scope (Desig_Type));
12339 Decls : List_Id;
12340 Decl : Node_Id;
12341
12342 begin
12343 if Nkind (Pack) = N_Package_Declaration then
12344 Decls := Visible_Declarations (Specification (Pack));
12345 Decl := First (Decls);
12346 while Present (Decl) loop
12347 if (Nkind (Decl) = N_Private_Type_Declaration
12348 and then Chars (Defining_Identifier (Decl)) =
12349 Chars (Desig_Type))
12350
12351 or else
12352 (Nkind (Decl) = N_Full_Type_Declaration
12353 and then
12354 Chars (Defining_Identifier (Decl)) =
12355 Chars (Desig_Type)
12356 and then Is_Derived_Type (Desig_Type)
12357 and then
12358 Has_Private_Declaration (Etype (Desig_Type)))
12359 then
12360 if No (Discriminant_Specifications (Decl)) then
12361 Error_Msg_N
12362 ("cannot constrain access type if designated "
12363 & "type has constrained partial view", S);
12364 end if;
12365
12366 exit;
12367 end if;
12368
12369 Next (Decl);
12370 end loop;
12371 end if;
12372 end;
12373 end if;
12374
12375 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12376 For_Access => True);
12377
12378 elsif Is_Concurrent_Type (Desig_Type)
12379 and then not Is_Constrained (Desig_Type)
12380 then
12381 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12382
12383 else
12384 Error_Msg_N ("invalid constraint on access type", S);
12385
12386 -- We simply ignore an invalid constraint
12387
12388 Desig_Subtype := Desig_Type;
12389 Constraint_OK := False;
12390 end if;
12391
12392 if No (Def_Id) then
12393 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12394 else
12395 Set_Ekind (Def_Id, E_Access_Subtype);
12396 end if;
12397
12398 if Constraint_OK then
12399 Set_Etype (Def_Id, Base_Type (T));
12400
12401 if Is_Private_Type (Desig_Type) then
12402 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12403 end if;
12404 else
12405 Set_Etype (Def_Id, Any_Type);
12406 end if;
12407
12408 Set_Size_Info (Def_Id, T);
12409 Set_Is_Constrained (Def_Id, Constraint_OK);
12410 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12411 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12412 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12413
12414 Conditional_Delay (Def_Id, T);
12415
12416 -- AI-363 : Subtypes of general access types whose designated types have
12417 -- default discriminants are disallowed. In instances, the rule has to
12418 -- be checked against the actual, of which T is the subtype. In a
12419 -- generic body, the rule is checked assuming that the actual type has
12420 -- defaulted discriminants.
12421
12422 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12423 if Ekind (Base_Type (T)) = E_General_Access_Type
12424 and then Has_Defaulted_Discriminants (Desig_Type)
12425 then
12426 if Ada_Version < Ada_2005 then
12427 Error_Msg_N
12428 ("access subtype of general access type would not " &
12429 "be allowed in Ada 2005?y?", S);
12430 else
12431 Error_Msg_N
12432 ("access subtype of general access type not allowed", S);
12433 end if;
12434
12435 Error_Msg_N ("\discriminants have defaults", S);
12436
12437 elsif Is_Access_Type (T)
12438 and then Is_Generic_Type (Desig_Type)
12439 and then Has_Discriminants (Desig_Type)
12440 and then In_Package_Body (Current_Scope)
12441 then
12442 if Ada_Version < Ada_2005 then
12443 Error_Msg_N
12444 ("access subtype would not be allowed in generic body "
12445 & "in Ada 2005?y?", S);
12446 else
12447 Error_Msg_N
12448 ("access subtype not allowed in generic body", S);
12449 end if;
12450
12451 Error_Msg_N
12452 ("\designated type is a discriminated formal", S);
12453 end if;
12454 end if;
12455 end Constrain_Access;
12456
12457 ---------------------
12458 -- Constrain_Array --
12459 ---------------------
12460
12461 procedure Constrain_Array
12462 (Def_Id : in out Entity_Id;
12463 SI : Node_Id;
12464 Related_Nod : Node_Id;
12465 Related_Id : Entity_Id;
12466 Suffix : Character)
12467 is
12468 C : constant Node_Id := Constraint (SI);
12469 Number_Of_Constraints : Nat := 0;
12470 Index : Node_Id;
12471 S, T : Entity_Id;
12472 Constraint_OK : Boolean := True;
12473
12474 begin
12475 T := Entity (Subtype_Mark (SI));
12476
12477 if Is_Access_Type (T) then
12478 T := Designated_Type (T);
12479 end if;
12480
12481 -- If an index constraint follows a subtype mark in a subtype indication
12482 -- then the type or subtype denoted by the subtype mark must not already
12483 -- impose an index constraint. The subtype mark must denote either an
12484 -- unconstrained array type or an access type whose designated type
12485 -- is such an array type... (RM 3.6.1)
12486
12487 if Is_Constrained (T) then
12488 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12489 Constraint_OK := False;
12490
12491 else
12492 S := First (Constraints (C));
12493 while Present (S) loop
12494 Number_Of_Constraints := Number_Of_Constraints + 1;
12495 Next (S);
12496 end loop;
12497
12498 -- In either case, the index constraint must provide a discrete
12499 -- range for each index of the array type and the type of each
12500 -- discrete range must be the same as that of the corresponding
12501 -- index. (RM 3.6.1)
12502
12503 if Number_Of_Constraints /= Number_Dimensions (T) then
12504 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12505 Constraint_OK := False;
12506
12507 else
12508 S := First (Constraints (C));
12509 Index := First_Index (T);
12510 Analyze (Index);
12511
12512 -- Apply constraints to each index type
12513
12514 for J in 1 .. Number_Of_Constraints loop
12515 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12516 Next (Index);
12517 Next (S);
12518 end loop;
12519
12520 end if;
12521 end if;
12522
12523 if No (Def_Id) then
12524 Def_Id :=
12525 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12526 Set_Parent (Def_Id, Related_Nod);
12527
12528 else
12529 Set_Ekind (Def_Id, E_Array_Subtype);
12530 end if;
12531
12532 Set_Size_Info (Def_Id, (T));
12533 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12534 Set_Etype (Def_Id, Base_Type (T));
12535
12536 if Constraint_OK then
12537 Set_First_Index (Def_Id, First (Constraints (C)));
12538 else
12539 Set_First_Index (Def_Id, First_Index (T));
12540 end if;
12541
12542 Set_Is_Constrained (Def_Id, True);
12543 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12544 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12545
12546 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12547 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12548
12549 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12550 -- We need to initialize the attribute because if Def_Id is previously
12551 -- analyzed through a limited_with clause, it will have the attributes
12552 -- of an incomplete type, one of which is an Elist that overlaps the
12553 -- Packed_Array_Impl_Type field.
12554
12555 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12556
12557 -- Build a freeze node if parent still needs one. Also make sure that
12558 -- the Depends_On_Private status is set because the subtype will need
12559 -- reprocessing at the time the base type does, and also we must set a
12560 -- conditional delay.
12561
12562 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12563 Conditional_Delay (Def_Id, T);
12564 end Constrain_Array;
12565
12566 ------------------------------
12567 -- Constrain_Component_Type --
12568 ------------------------------
12569
12570 function Constrain_Component_Type
12571 (Comp : Entity_Id;
12572 Constrained_Typ : Entity_Id;
12573 Related_Node : Node_Id;
12574 Typ : Entity_Id;
12575 Constraints : Elist_Id) return Entity_Id
12576 is
12577 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12578 Compon_Type : constant Entity_Id := Etype (Comp);
12579
12580 function Build_Constrained_Array_Type
12581 (Old_Type : Entity_Id) return Entity_Id;
12582 -- If Old_Type is an array type, one of whose indexes is constrained
12583 -- by a discriminant, build an Itype whose constraint replaces the
12584 -- discriminant with its value in the constraint.
12585
12586 function Build_Constrained_Discriminated_Type
12587 (Old_Type : Entity_Id) return Entity_Id;
12588 -- Ditto for record components
12589
12590 function Build_Constrained_Access_Type
12591 (Old_Type : Entity_Id) return Entity_Id;
12592 -- Ditto for access types. Makes use of previous two functions, to
12593 -- constrain designated type.
12594
12595 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12596 -- T is an array or discriminated type, C is a list of constraints
12597 -- that apply to T. This routine builds the constrained subtype.
12598
12599 function Is_Discriminant (Expr : Node_Id) return Boolean;
12600 -- Returns True if Expr is a discriminant
12601
12602 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12603 -- Find the value of discriminant Discrim in Constraint
12604
12605 -----------------------------------
12606 -- Build_Constrained_Access_Type --
12607 -----------------------------------
12608
12609 function Build_Constrained_Access_Type
12610 (Old_Type : Entity_Id) return Entity_Id
12611 is
12612 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12613 Itype : Entity_Id;
12614 Desig_Subtype : Entity_Id;
12615 Scop : Entity_Id;
12616
12617 begin
12618 -- if the original access type was not embedded in the enclosing
12619 -- type definition, there is no need to produce a new access
12620 -- subtype. In fact every access type with an explicit constraint
12621 -- generates an itype whose scope is the enclosing record.
12622
12623 if not Is_Type (Scope (Old_Type)) then
12624 return Old_Type;
12625
12626 elsif Is_Array_Type (Desig_Type) then
12627 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12628
12629 elsif Has_Discriminants (Desig_Type) then
12630
12631 -- This may be an access type to an enclosing record type for
12632 -- which we are constructing the constrained components. Return
12633 -- the enclosing record subtype. This is not always correct,
12634 -- but avoids infinite recursion. ???
12635
12636 Desig_Subtype := Any_Type;
12637
12638 for J in reverse 0 .. Scope_Stack.Last loop
12639 Scop := Scope_Stack.Table (J).Entity;
12640
12641 if Is_Type (Scop)
12642 and then Base_Type (Scop) = Base_Type (Desig_Type)
12643 then
12644 Desig_Subtype := Scop;
12645 end if;
12646
12647 exit when not Is_Type (Scop);
12648 end loop;
12649
12650 if Desig_Subtype = Any_Type then
12651 Desig_Subtype :=
12652 Build_Constrained_Discriminated_Type (Desig_Type);
12653 end if;
12654
12655 else
12656 return Old_Type;
12657 end if;
12658
12659 if Desig_Subtype /= Desig_Type then
12660
12661 -- The Related_Node better be here or else we won't be able
12662 -- to attach new itypes to a node in the tree.
12663
12664 pragma Assert (Present (Related_Node));
12665
12666 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12667
12668 Set_Etype (Itype, Base_Type (Old_Type));
12669 Set_Size_Info (Itype, (Old_Type));
12670 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12671 Set_Depends_On_Private (Itype, Has_Private_Component
12672 (Old_Type));
12673 Set_Is_Access_Constant (Itype, Is_Access_Constant
12674 (Old_Type));
12675
12676 -- The new itype needs freezing when it depends on a not frozen
12677 -- type and the enclosing subtype needs freezing.
12678
12679 if Has_Delayed_Freeze (Constrained_Typ)
12680 and then not Is_Frozen (Constrained_Typ)
12681 then
12682 Conditional_Delay (Itype, Base_Type (Old_Type));
12683 end if;
12684
12685 return Itype;
12686
12687 else
12688 return Old_Type;
12689 end if;
12690 end Build_Constrained_Access_Type;
12691
12692 ----------------------------------
12693 -- Build_Constrained_Array_Type --
12694 ----------------------------------
12695
12696 function Build_Constrained_Array_Type
12697 (Old_Type : Entity_Id) return Entity_Id
12698 is
12699 Lo_Expr : Node_Id;
12700 Hi_Expr : Node_Id;
12701 Old_Index : Node_Id;
12702 Range_Node : Node_Id;
12703 Constr_List : List_Id;
12704
12705 Need_To_Create_Itype : Boolean := False;
12706
12707 begin
12708 Old_Index := First_Index (Old_Type);
12709 while Present (Old_Index) loop
12710 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12711
12712 if Is_Discriminant (Lo_Expr)
12713 or else
12714 Is_Discriminant (Hi_Expr)
12715 then
12716 Need_To_Create_Itype := True;
12717 end if;
12718
12719 Next_Index (Old_Index);
12720 end loop;
12721
12722 if Need_To_Create_Itype then
12723 Constr_List := New_List;
12724
12725 Old_Index := First_Index (Old_Type);
12726 while Present (Old_Index) loop
12727 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12728
12729 if Is_Discriminant (Lo_Expr) then
12730 Lo_Expr := Get_Discr_Value (Lo_Expr);
12731 end if;
12732
12733 if Is_Discriminant (Hi_Expr) then
12734 Hi_Expr := Get_Discr_Value (Hi_Expr);
12735 end if;
12736
12737 Range_Node :=
12738 Make_Range
12739 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12740
12741 Append (Range_Node, To => Constr_List);
12742
12743 Next_Index (Old_Index);
12744 end loop;
12745
12746 return Build_Subtype (Old_Type, Constr_List);
12747
12748 else
12749 return Old_Type;
12750 end if;
12751 end Build_Constrained_Array_Type;
12752
12753 ------------------------------------------
12754 -- Build_Constrained_Discriminated_Type --
12755 ------------------------------------------
12756
12757 function Build_Constrained_Discriminated_Type
12758 (Old_Type : Entity_Id) return Entity_Id
12759 is
12760 Expr : Node_Id;
12761 Constr_List : List_Id;
12762 Old_Constraint : Elmt_Id;
12763
12764 Need_To_Create_Itype : Boolean := False;
12765
12766 begin
12767 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12768 while Present (Old_Constraint) loop
12769 Expr := Node (Old_Constraint);
12770
12771 if Is_Discriminant (Expr) then
12772 Need_To_Create_Itype := True;
12773 end if;
12774
12775 Next_Elmt (Old_Constraint);
12776 end loop;
12777
12778 if Need_To_Create_Itype then
12779 Constr_List := New_List;
12780
12781 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12782 while Present (Old_Constraint) loop
12783 Expr := Node (Old_Constraint);
12784
12785 if Is_Discriminant (Expr) then
12786 Expr := Get_Discr_Value (Expr);
12787 end if;
12788
12789 Append (New_Copy_Tree (Expr), To => Constr_List);
12790
12791 Next_Elmt (Old_Constraint);
12792 end loop;
12793
12794 return Build_Subtype (Old_Type, Constr_List);
12795
12796 else
12797 return Old_Type;
12798 end if;
12799 end Build_Constrained_Discriminated_Type;
12800
12801 -------------------
12802 -- Build_Subtype --
12803 -------------------
12804
12805 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12806 Indic : Node_Id;
12807 Subtyp_Decl : Node_Id;
12808 Def_Id : Entity_Id;
12809 Btyp : Entity_Id := Base_Type (T);
12810
12811 begin
12812 -- The Related_Node better be here or else we won't be able to
12813 -- attach new itypes to a node in the tree.
12814
12815 pragma Assert (Present (Related_Node));
12816
12817 -- If the view of the component's type is incomplete or private
12818 -- with unknown discriminants, then the constraint must be applied
12819 -- to the full type.
12820
12821 if Has_Unknown_Discriminants (Btyp)
12822 and then Present (Underlying_Type (Btyp))
12823 then
12824 Btyp := Underlying_Type (Btyp);
12825 end if;
12826
12827 Indic :=
12828 Make_Subtype_Indication (Loc,
12829 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12830 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12831
12832 Def_Id := Create_Itype (Ekind (T), Related_Node);
12833
12834 Subtyp_Decl :=
12835 Make_Subtype_Declaration (Loc,
12836 Defining_Identifier => Def_Id,
12837 Subtype_Indication => Indic);
12838
12839 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12840
12841 -- Itypes must be analyzed with checks off (see package Itypes)
12842
12843 Analyze (Subtyp_Decl, Suppress => All_Checks);
12844
12845 return Def_Id;
12846 end Build_Subtype;
12847
12848 ---------------------
12849 -- Get_Discr_Value --
12850 ---------------------
12851
12852 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12853 D : Entity_Id;
12854 E : Elmt_Id;
12855
12856 begin
12857 -- The discriminant may be declared for the type, in which case we
12858 -- find it by iterating over the list of discriminants. If the
12859 -- discriminant is inherited from a parent type, it appears as the
12860 -- corresponding discriminant of the current type. This will be the
12861 -- case when constraining an inherited component whose constraint is
12862 -- given by a discriminant of the parent.
12863
12864 D := First_Discriminant (Typ);
12865 E := First_Elmt (Constraints);
12866
12867 while Present (D) loop
12868 if D = Entity (Discrim)
12869 or else D = CR_Discriminant (Entity (Discrim))
12870 or else Corresponding_Discriminant (D) = Entity (Discrim)
12871 then
12872 return Node (E);
12873 end if;
12874
12875 Next_Discriminant (D);
12876 Next_Elmt (E);
12877 end loop;
12878
12879 -- The Corresponding_Discriminant mechanism is incomplete, because
12880 -- the correspondence between new and old discriminants is not one
12881 -- to one: one new discriminant can constrain several old ones. In
12882 -- that case, scan sequentially the stored_constraint, the list of
12883 -- discriminants of the parents, and the constraints.
12884
12885 -- Previous code checked for the present of the Stored_Constraint
12886 -- list for the derived type, but did not use it at all. Should it
12887 -- be present when the component is a discriminated task type?
12888
12889 if Is_Derived_Type (Typ)
12890 and then Scope (Entity (Discrim)) = Etype (Typ)
12891 then
12892 D := First_Discriminant (Etype (Typ));
12893 E := First_Elmt (Constraints);
12894 while Present (D) loop
12895 if D = Entity (Discrim) then
12896 return Node (E);
12897 end if;
12898
12899 Next_Discriminant (D);
12900 Next_Elmt (E);
12901 end loop;
12902 end if;
12903
12904 -- Something is wrong if we did not find the value
12905
12906 raise Program_Error;
12907 end Get_Discr_Value;
12908
12909 ---------------------
12910 -- Is_Discriminant --
12911 ---------------------
12912
12913 function Is_Discriminant (Expr : Node_Id) return Boolean is
12914 Discrim_Scope : Entity_Id;
12915
12916 begin
12917 if Denotes_Discriminant (Expr) then
12918 Discrim_Scope := Scope (Entity (Expr));
12919
12920 -- Either we have a reference to one of Typ's discriminants,
12921
12922 pragma Assert (Discrim_Scope = Typ
12923
12924 -- or to the discriminants of the parent type, in the case
12925 -- of a derivation of a tagged type with variants.
12926
12927 or else Discrim_Scope = Etype (Typ)
12928 or else Full_View (Discrim_Scope) = Etype (Typ)
12929
12930 -- or same as above for the case where the discriminants
12931 -- were declared in Typ's private view.
12932
12933 or else (Is_Private_Type (Discrim_Scope)
12934 and then Chars (Discrim_Scope) = Chars (Typ))
12935
12936 -- or else we are deriving from the full view and the
12937 -- discriminant is declared in the private entity.
12938
12939 or else (Is_Private_Type (Typ)
12940 and then Chars (Discrim_Scope) = Chars (Typ))
12941
12942 -- Or we are constrained the corresponding record of a
12943 -- synchronized type that completes a private declaration.
12944
12945 or else (Is_Concurrent_Record_Type (Typ)
12946 and then
12947 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12948
12949 -- or we have a class-wide type, in which case make sure the
12950 -- discriminant found belongs to the root type.
12951
12952 or else (Is_Class_Wide_Type (Typ)
12953 and then Etype (Typ) = Discrim_Scope));
12954
12955 return True;
12956 end if;
12957
12958 -- In all other cases we have something wrong
12959
12960 return False;
12961 end Is_Discriminant;
12962
12963 -- Start of processing for Constrain_Component_Type
12964
12965 begin
12966 if Nkind (Parent (Comp)) = N_Component_Declaration
12967 and then Comes_From_Source (Parent (Comp))
12968 and then Comes_From_Source
12969 (Subtype_Indication (Component_Definition (Parent (Comp))))
12970 and then
12971 Is_Entity_Name
12972 (Subtype_Indication (Component_Definition (Parent (Comp))))
12973 then
12974 return Compon_Type;
12975
12976 elsif Is_Array_Type (Compon_Type) then
12977 return Build_Constrained_Array_Type (Compon_Type);
12978
12979 elsif Has_Discriminants (Compon_Type) then
12980 return Build_Constrained_Discriminated_Type (Compon_Type);
12981
12982 elsif Is_Access_Type (Compon_Type) then
12983 return Build_Constrained_Access_Type (Compon_Type);
12984
12985 else
12986 return Compon_Type;
12987 end if;
12988 end Constrain_Component_Type;
12989
12990 --------------------------
12991 -- Constrain_Concurrent --
12992 --------------------------
12993
12994 -- For concurrent types, the associated record value type carries the same
12995 -- discriminants, so when we constrain a concurrent type, we must constrain
12996 -- the corresponding record type as well.
12997
12998 procedure Constrain_Concurrent
12999 (Def_Id : in out Entity_Id;
13000 SI : Node_Id;
13001 Related_Nod : Node_Id;
13002 Related_Id : Entity_Id;
13003 Suffix : Character)
13004 is
13005 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13006 -- case of a private subtype (needed when only doing semantic analysis).
13007
13008 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13009 T_Val : Entity_Id;
13010
13011 begin
13012 if Is_Access_Type (T_Ent) then
13013 T_Ent := Designated_Type (T_Ent);
13014 end if;
13015
13016 T_Val := Corresponding_Record_Type (T_Ent);
13017
13018 if Present (T_Val) then
13019
13020 if No (Def_Id) then
13021 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13022
13023 -- Elaborate itype now, as it may be used in a subsequent
13024 -- synchronized operation in another scope.
13025
13026 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13027 Build_Itype_Reference (Def_Id, Related_Nod);
13028 end if;
13029 end if;
13030
13031 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13032
13033 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13034 Set_Corresponding_Record_Type (Def_Id,
13035 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13036
13037 else
13038 -- If there is no associated record, expansion is disabled and this
13039 -- is a generic context. Create a subtype in any case, so that
13040 -- semantic analysis can proceed.
13041
13042 if No (Def_Id) then
13043 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13044 end if;
13045
13046 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13047 end if;
13048 end Constrain_Concurrent;
13049
13050 ------------------------------------
13051 -- Constrain_Corresponding_Record --
13052 ------------------------------------
13053
13054 function Constrain_Corresponding_Record
13055 (Prot_Subt : Entity_Id;
13056 Corr_Rec : Entity_Id;
13057 Related_Nod : Node_Id) return Entity_Id
13058 is
13059 T_Sub : constant Entity_Id :=
13060 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13061
13062 begin
13063 Set_Etype (T_Sub, Corr_Rec);
13064 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13065 Set_Is_Constrained (T_Sub, True);
13066 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13067 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13068
13069 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13070 Set_Discriminant_Constraint
13071 (T_Sub, Discriminant_Constraint (Prot_Subt));
13072 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13073 Create_Constrained_Components
13074 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13075 end if;
13076
13077 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13078
13079 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13080 Conditional_Delay (T_Sub, Corr_Rec);
13081
13082 else
13083 -- This is a component subtype: it will be frozen in the context of
13084 -- the enclosing record's init_proc, so that discriminant references
13085 -- are resolved to discriminals. (Note: we used to skip freezing
13086 -- altogether in that case, which caused errors downstream for
13087 -- components of a bit packed array type).
13088
13089 Set_Has_Delayed_Freeze (T_Sub);
13090 end if;
13091
13092 return T_Sub;
13093 end Constrain_Corresponding_Record;
13094
13095 -----------------------
13096 -- Constrain_Decimal --
13097 -----------------------
13098
13099 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13100 T : constant Entity_Id := Entity (Subtype_Mark (S));
13101 C : constant Node_Id := Constraint (S);
13102 Loc : constant Source_Ptr := Sloc (C);
13103 Range_Expr : Node_Id;
13104 Digits_Expr : Node_Id;
13105 Digits_Val : Uint;
13106 Bound_Val : Ureal;
13107
13108 begin
13109 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13110
13111 if Nkind (C) = N_Range_Constraint then
13112 Range_Expr := Range_Expression (C);
13113 Digits_Val := Digits_Value (T);
13114
13115 else
13116 pragma Assert (Nkind (C) = N_Digits_Constraint);
13117
13118 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13119
13120 Digits_Expr := Digits_Expression (C);
13121 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13122
13123 Check_Digits_Expression (Digits_Expr);
13124 Digits_Val := Expr_Value (Digits_Expr);
13125
13126 if Digits_Val > Digits_Value (T) then
13127 Error_Msg_N
13128 ("digits expression is incompatible with subtype", C);
13129 Digits_Val := Digits_Value (T);
13130 end if;
13131
13132 if Present (Range_Constraint (C)) then
13133 Range_Expr := Range_Expression (Range_Constraint (C));
13134 else
13135 Range_Expr := Empty;
13136 end if;
13137 end if;
13138
13139 Set_Etype (Def_Id, Base_Type (T));
13140 Set_Size_Info (Def_Id, (T));
13141 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13142 Set_Delta_Value (Def_Id, Delta_Value (T));
13143 Set_Scale_Value (Def_Id, Scale_Value (T));
13144 Set_Small_Value (Def_Id, Small_Value (T));
13145 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13146 Set_Digits_Value (Def_Id, Digits_Val);
13147
13148 -- Manufacture range from given digits value if no range present
13149
13150 if No (Range_Expr) then
13151 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13152 Range_Expr :=
13153 Make_Range (Loc,
13154 Low_Bound =>
13155 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13156 High_Bound =>
13157 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13158 end if;
13159
13160 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13161 Set_Discrete_RM_Size (Def_Id);
13162
13163 -- Unconditionally delay the freeze, since we cannot set size
13164 -- information in all cases correctly until the freeze point.
13165
13166 Set_Has_Delayed_Freeze (Def_Id);
13167 end Constrain_Decimal;
13168
13169 ----------------------------------
13170 -- Constrain_Discriminated_Type --
13171 ----------------------------------
13172
13173 procedure Constrain_Discriminated_Type
13174 (Def_Id : Entity_Id;
13175 S : Node_Id;
13176 Related_Nod : Node_Id;
13177 For_Access : Boolean := False)
13178 is
13179 E : Entity_Id := Entity (Subtype_Mark (S));
13180 T : Entity_Id;
13181
13182 procedure Fixup_Bad_Constraint;
13183 -- Called after finding a bad constraint, and after having posted an
13184 -- appropriate error message. The goal is to leave type Def_Id in as
13185 -- reasonable state as possible.
13186
13187 --------------------------
13188 -- Fixup_Bad_Constraint --
13189 --------------------------
13190
13191 procedure Fixup_Bad_Constraint is
13192 begin
13193 -- Set a reasonable Ekind for the entity. For an incomplete type,
13194 -- we can't do much, but for other types, we can set the proper
13195 -- corresponding subtype kind.
13196
13197 if Ekind (T) = E_Incomplete_Type then
13198 Set_Ekind (Def_Id, Ekind (T));
13199 else
13200 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13201 end if;
13202
13203 -- Set Etype to the known type, to reduce chances of cascaded errors
13204
13205 Set_Etype (Def_Id, E);
13206 Set_Error_Posted (Def_Id);
13207 end Fixup_Bad_Constraint;
13208
13209 -- Local variables
13210
13211 C : Node_Id;
13212 Constr : Elist_Id := New_Elmt_List;
13213
13214 -- Start of processing for Constrain_Discriminated_Type
13215
13216 begin
13217 C := Constraint (S);
13218
13219 -- A discriminant constraint is only allowed in a subtype indication,
13220 -- after a subtype mark. This subtype mark must denote either a type
13221 -- with discriminants, or an access type whose designated type is a
13222 -- type with discriminants. A discriminant constraint specifies the
13223 -- values of these discriminants (RM 3.7.2(5)).
13224
13225 T := Base_Type (Entity (Subtype_Mark (S)));
13226
13227 if Is_Access_Type (T) then
13228 T := Designated_Type (T);
13229 end if;
13230
13231 -- In an instance it may be necessary to retrieve the full view of a
13232 -- type with unknown discriminants, or a full view with defaulted
13233 -- discriminants. In other contexts the constraint is illegal.
13234
13235 if In_Instance
13236 and then Is_Private_Type (T)
13237 and then Present (Full_View (T))
13238 and then
13239 (Has_Unknown_Discriminants (T)
13240 or else
13241 (not Has_Discriminants (T)
13242 and then Has_Discriminants (Full_View (T))
13243 and then Present (Discriminant_Default_Value
13244 (First_Discriminant (Full_View (T))))))
13245 then
13246 T := Full_View (T);
13247 E := Full_View (E);
13248 end if;
13249
13250 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13251 -- generating an error for access-to-incomplete subtypes.
13252
13253 if Ada_Version >= Ada_2005
13254 and then Ekind (T) = E_Incomplete_Type
13255 and then Nkind (Parent (S)) = N_Subtype_Declaration
13256 and then not Is_Itype (Def_Id)
13257 then
13258 -- A little sanity check: emit an error message if the type has
13259 -- discriminants to begin with. Type T may be a regular incomplete
13260 -- type or imported via a limited with clause.
13261
13262 if Has_Discriminants (T)
13263 or else (From_Limited_With (T)
13264 and then Present (Non_Limited_View (T))
13265 and then Nkind (Parent (Non_Limited_View (T))) =
13266 N_Full_Type_Declaration
13267 and then Present (Discriminant_Specifications
13268 (Parent (Non_Limited_View (T)))))
13269 then
13270 Error_Msg_N
13271 ("(Ada 2005) incomplete subtype may not be constrained", C);
13272 else
13273 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13274 end if;
13275
13276 Fixup_Bad_Constraint;
13277 return;
13278
13279 -- Check that the type has visible discriminants. The type may be
13280 -- a private type with unknown discriminants whose full view has
13281 -- discriminants which are invisible.
13282
13283 elsif not Has_Discriminants (T)
13284 or else
13285 (Has_Unknown_Discriminants (T)
13286 and then Is_Private_Type (T))
13287 then
13288 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13289 Fixup_Bad_Constraint;
13290 return;
13291
13292 elsif Is_Constrained (E)
13293 or else (Ekind (E) = E_Class_Wide_Subtype
13294 and then Present (Discriminant_Constraint (E)))
13295 then
13296 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13297 Fixup_Bad_Constraint;
13298 return;
13299 end if;
13300
13301 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13302 -- applies to the base type.
13303
13304 T := Base_Type (T);
13305
13306 Constr := Build_Discriminant_Constraints (T, S);
13307
13308 -- If the list returned was empty we had an error in building the
13309 -- discriminant constraint. We have also already signalled an error
13310 -- in the incomplete type case
13311
13312 if Is_Empty_Elmt_List (Constr) then
13313 Fixup_Bad_Constraint;
13314 return;
13315 end if;
13316
13317 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13318 end Constrain_Discriminated_Type;
13319
13320 ---------------------------
13321 -- Constrain_Enumeration --
13322 ---------------------------
13323
13324 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13325 T : constant Entity_Id := Entity (Subtype_Mark (S));
13326 C : constant Node_Id := Constraint (S);
13327
13328 begin
13329 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13330
13331 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13332
13333 Set_Etype (Def_Id, Base_Type (T));
13334 Set_Size_Info (Def_Id, (T));
13335 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13336 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13337
13338 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13339
13340 Set_Discrete_RM_Size (Def_Id);
13341 end Constrain_Enumeration;
13342
13343 ----------------------
13344 -- Constrain_Float --
13345 ----------------------
13346
13347 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13348 T : constant Entity_Id := Entity (Subtype_Mark (S));
13349 C : Node_Id;
13350 D : Node_Id;
13351 Rais : Node_Id;
13352
13353 begin
13354 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13355
13356 Set_Etype (Def_Id, Base_Type (T));
13357 Set_Size_Info (Def_Id, (T));
13358 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13359
13360 -- Process the constraint
13361
13362 C := Constraint (S);
13363
13364 -- Digits constraint present
13365
13366 if Nkind (C) = N_Digits_Constraint then
13367
13368 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13369 Check_Restriction (No_Obsolescent_Features, C);
13370
13371 if Warn_On_Obsolescent_Feature then
13372 Error_Msg_N
13373 ("subtype digits constraint is an " &
13374 "obsolescent feature (RM J.3(8))?j?", C);
13375 end if;
13376
13377 D := Digits_Expression (C);
13378 Analyze_And_Resolve (D, Any_Integer);
13379 Check_Digits_Expression (D);
13380 Set_Digits_Value (Def_Id, Expr_Value (D));
13381
13382 -- Check that digits value is in range. Obviously we can do this
13383 -- at compile time, but it is strictly a runtime check, and of
13384 -- course there is an ACVC test that checks this.
13385
13386 if Digits_Value (Def_Id) > Digits_Value (T) then
13387 Error_Msg_Uint_1 := Digits_Value (T);
13388 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13389 Rais :=
13390 Make_Raise_Constraint_Error (Sloc (D),
13391 Reason => CE_Range_Check_Failed);
13392 Insert_Action (Declaration_Node (Def_Id), Rais);
13393 end if;
13394
13395 C := Range_Constraint (C);
13396
13397 -- No digits constraint present
13398
13399 else
13400 Set_Digits_Value (Def_Id, Digits_Value (T));
13401 end if;
13402
13403 -- Range constraint present
13404
13405 if Nkind (C) = N_Range_Constraint then
13406 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13407
13408 -- No range constraint present
13409
13410 else
13411 pragma Assert (No (C));
13412 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13413 end if;
13414
13415 Set_Is_Constrained (Def_Id);
13416 end Constrain_Float;
13417
13418 ---------------------
13419 -- Constrain_Index --
13420 ---------------------
13421
13422 procedure Constrain_Index
13423 (Index : Node_Id;
13424 S : Node_Id;
13425 Related_Nod : Node_Id;
13426 Related_Id : Entity_Id;
13427 Suffix : Character;
13428 Suffix_Index : Nat)
13429 is
13430 Def_Id : Entity_Id;
13431 R : Node_Id := Empty;
13432 T : constant Entity_Id := Etype (Index);
13433
13434 begin
13435 Def_Id :=
13436 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13437 Set_Etype (Def_Id, Base_Type (T));
13438
13439 if Nkind (S) = N_Range
13440 or else
13441 (Nkind (S) = N_Attribute_Reference
13442 and then Attribute_Name (S) = Name_Range)
13443 then
13444 -- A Range attribute will be transformed into N_Range by Resolve
13445
13446 Analyze (S);
13447 Set_Etype (S, T);
13448 R := S;
13449
13450 Process_Range_Expr_In_Decl (R, T);
13451
13452 if not Error_Posted (S)
13453 and then
13454 (Nkind (S) /= N_Range
13455 or else not Covers (T, (Etype (Low_Bound (S))))
13456 or else not Covers (T, (Etype (High_Bound (S)))))
13457 then
13458 if Base_Type (T) /= Any_Type
13459 and then Etype (Low_Bound (S)) /= Any_Type
13460 and then Etype (High_Bound (S)) /= Any_Type
13461 then
13462 Error_Msg_N ("range expected", S);
13463 end if;
13464 end if;
13465
13466 elsif Nkind (S) = N_Subtype_Indication then
13467
13468 -- The parser has verified that this is a discrete indication
13469
13470 Resolve_Discrete_Subtype_Indication (S, T);
13471 Bad_Predicated_Subtype_Use
13472 ("subtype& has predicate, not allowed in index constraint",
13473 S, Entity (Subtype_Mark (S)));
13474
13475 R := Range_Expression (Constraint (S));
13476
13477 -- Capture values of bounds and generate temporaries for them if
13478 -- needed, since checks may cause duplication of the expressions
13479 -- which must not be reevaluated.
13480
13481 -- The forced evaluation removes side effects from expressions, which
13482 -- should occur also in GNATprove mode. Otherwise, we end up with
13483 -- unexpected insertions of actions at places where this is not
13484 -- supposed to occur, e.g. on default parameters of a call.
13485
13486 if Expander_Active or GNATprove_Mode then
13487 Force_Evaluation
13488 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13489 Force_Evaluation
13490 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13491 end if;
13492
13493 elsif Nkind (S) = N_Discriminant_Association then
13494
13495 -- Syntactically valid in subtype indication
13496
13497 Error_Msg_N ("invalid index constraint", S);
13498 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13499 return;
13500
13501 -- Subtype_Mark case, no anonymous subtypes to construct
13502
13503 else
13504 Analyze (S);
13505
13506 if Is_Entity_Name (S) then
13507 if not Is_Type (Entity (S)) then
13508 Error_Msg_N ("expect subtype mark for index constraint", S);
13509
13510 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13511 Wrong_Type (S, Base_Type (T));
13512
13513 -- Check error of subtype with predicate in index constraint
13514
13515 else
13516 Bad_Predicated_Subtype_Use
13517 ("subtype& has predicate, not allowed in index constraint",
13518 S, Entity (S));
13519 end if;
13520
13521 return;
13522
13523 else
13524 Error_Msg_N ("invalid index constraint", S);
13525 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13526 return;
13527 end if;
13528 end if;
13529
13530 -- Complete construction of the Itype
13531
13532 if Is_Modular_Integer_Type (T) then
13533 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13534
13535 elsif Is_Integer_Type (T) then
13536 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13537
13538 else
13539 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13540 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13541 Set_First_Literal (Def_Id, First_Literal (T));
13542 end if;
13543
13544 Set_Size_Info (Def_Id, (T));
13545 Set_RM_Size (Def_Id, RM_Size (T));
13546 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13547
13548 Set_Scalar_Range (Def_Id, R);
13549
13550 Set_Etype (S, Def_Id);
13551 Set_Discrete_RM_Size (Def_Id);
13552 end Constrain_Index;
13553
13554 -----------------------
13555 -- Constrain_Integer --
13556 -----------------------
13557
13558 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13559 T : constant Entity_Id := Entity (Subtype_Mark (S));
13560 C : constant Node_Id := Constraint (S);
13561
13562 begin
13563 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13564
13565 if Is_Modular_Integer_Type (T) then
13566 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13567 else
13568 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13569 end if;
13570
13571 Set_Etype (Def_Id, Base_Type (T));
13572 Set_Size_Info (Def_Id, (T));
13573 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13574 Set_Discrete_RM_Size (Def_Id);
13575 end Constrain_Integer;
13576
13577 ------------------------------
13578 -- Constrain_Ordinary_Fixed --
13579 ------------------------------
13580
13581 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13582 T : constant Entity_Id := Entity (Subtype_Mark (S));
13583 C : Node_Id;
13584 D : Node_Id;
13585 Rais : Node_Id;
13586
13587 begin
13588 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13589 Set_Etype (Def_Id, Base_Type (T));
13590 Set_Size_Info (Def_Id, (T));
13591 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13592 Set_Small_Value (Def_Id, Small_Value (T));
13593
13594 -- Process the constraint
13595
13596 C := Constraint (S);
13597
13598 -- Delta constraint present
13599
13600 if Nkind (C) = N_Delta_Constraint then
13601
13602 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13603 Check_Restriction (No_Obsolescent_Features, C);
13604
13605 if Warn_On_Obsolescent_Feature then
13606 Error_Msg_S
13607 ("subtype delta constraint is an " &
13608 "obsolescent feature (RM J.3(7))?j?");
13609 end if;
13610
13611 D := Delta_Expression (C);
13612 Analyze_And_Resolve (D, Any_Real);
13613 Check_Delta_Expression (D);
13614 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13615
13616 -- Check that delta value is in range. Obviously we can do this
13617 -- at compile time, but it is strictly a runtime check, and of
13618 -- course there is an ACVC test that checks this.
13619
13620 if Delta_Value (Def_Id) < Delta_Value (T) then
13621 Error_Msg_N ("??delta value is too small", D);
13622 Rais :=
13623 Make_Raise_Constraint_Error (Sloc (D),
13624 Reason => CE_Range_Check_Failed);
13625 Insert_Action (Declaration_Node (Def_Id), Rais);
13626 end if;
13627
13628 C := Range_Constraint (C);
13629
13630 -- No delta constraint present
13631
13632 else
13633 Set_Delta_Value (Def_Id, Delta_Value (T));
13634 end if;
13635
13636 -- Range constraint present
13637
13638 if Nkind (C) = N_Range_Constraint then
13639 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13640
13641 -- No range constraint present
13642
13643 else
13644 pragma Assert (No (C));
13645 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13646 end if;
13647
13648 Set_Discrete_RM_Size (Def_Id);
13649
13650 -- Unconditionally delay the freeze, since we cannot set size
13651 -- information in all cases correctly until the freeze point.
13652
13653 Set_Has_Delayed_Freeze (Def_Id);
13654 end Constrain_Ordinary_Fixed;
13655
13656 -----------------------
13657 -- Contain_Interface --
13658 -----------------------
13659
13660 function Contain_Interface
13661 (Iface : Entity_Id;
13662 Ifaces : Elist_Id) return Boolean
13663 is
13664 Iface_Elmt : Elmt_Id;
13665
13666 begin
13667 if Present (Ifaces) then
13668 Iface_Elmt := First_Elmt (Ifaces);
13669 while Present (Iface_Elmt) loop
13670 if Node (Iface_Elmt) = Iface then
13671 return True;
13672 end if;
13673
13674 Next_Elmt (Iface_Elmt);
13675 end loop;
13676 end if;
13677
13678 return False;
13679 end Contain_Interface;
13680
13681 ---------------------------
13682 -- Convert_Scalar_Bounds --
13683 ---------------------------
13684
13685 procedure Convert_Scalar_Bounds
13686 (N : Node_Id;
13687 Parent_Type : Entity_Id;
13688 Derived_Type : Entity_Id;
13689 Loc : Source_Ptr)
13690 is
13691 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13692
13693 Lo : Node_Id;
13694 Hi : Node_Id;
13695 Rng : Node_Id;
13696
13697 begin
13698 -- Defend against previous errors
13699
13700 if No (Scalar_Range (Derived_Type)) then
13701 Check_Error_Detected;
13702 return;
13703 end if;
13704
13705 Lo := Build_Scalar_Bound
13706 (Type_Low_Bound (Derived_Type),
13707 Parent_Type, Implicit_Base);
13708
13709 Hi := Build_Scalar_Bound
13710 (Type_High_Bound (Derived_Type),
13711 Parent_Type, Implicit_Base);
13712
13713 Rng :=
13714 Make_Range (Loc,
13715 Low_Bound => Lo,
13716 High_Bound => Hi);
13717
13718 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13719
13720 Set_Parent (Rng, N);
13721 Set_Scalar_Range (Derived_Type, Rng);
13722
13723 -- Analyze the bounds
13724
13725 Analyze_And_Resolve (Lo, Implicit_Base);
13726 Analyze_And_Resolve (Hi, Implicit_Base);
13727
13728 -- Analyze the range itself, except that we do not analyze it if
13729 -- the bounds are real literals, and we have a fixed-point type.
13730 -- The reason for this is that we delay setting the bounds in this
13731 -- case till we know the final Small and Size values (see circuit
13732 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13733
13734 if Is_Fixed_Point_Type (Parent_Type)
13735 and then Nkind (Lo) = N_Real_Literal
13736 and then Nkind (Hi) = N_Real_Literal
13737 then
13738 return;
13739
13740 -- Here we do the analysis of the range
13741
13742 -- Note: we do this manually, since if we do a normal Analyze and
13743 -- Resolve call, there are problems with the conversions used for
13744 -- the derived type range.
13745
13746 else
13747 Set_Etype (Rng, Implicit_Base);
13748 Set_Analyzed (Rng, True);
13749 end if;
13750 end Convert_Scalar_Bounds;
13751
13752 -------------------
13753 -- Copy_And_Swap --
13754 -------------------
13755
13756 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13757 begin
13758 -- Initialize new full declaration entity by copying the pertinent
13759 -- fields of the corresponding private declaration entity.
13760
13761 -- We temporarily set Ekind to a value appropriate for a type to
13762 -- avoid assert failures in Einfo from checking for setting type
13763 -- attributes on something that is not a type. Ekind (Priv) is an
13764 -- appropriate choice, since it allowed the attributes to be set
13765 -- in the first place. This Ekind value will be modified later.
13766
13767 Set_Ekind (Full, Ekind (Priv));
13768
13769 -- Also set Etype temporarily to Any_Type, again, in the absence
13770 -- of errors, it will be properly reset, and if there are errors,
13771 -- then we want a value of Any_Type to remain.
13772
13773 Set_Etype (Full, Any_Type);
13774
13775 -- Now start copying attributes
13776
13777 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13778
13779 if Has_Discriminants (Full) then
13780 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13781 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13782 end if;
13783
13784 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13785 Set_Homonym (Full, Homonym (Priv));
13786 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13787 Set_Is_Public (Full, Is_Public (Priv));
13788 Set_Is_Pure (Full, Is_Pure (Priv));
13789 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13790 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13791 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13792 Set_Has_Pragma_Unreferenced_Objects
13793 (Full, Has_Pragma_Unreferenced_Objects
13794 (Priv));
13795
13796 Conditional_Delay (Full, Priv);
13797
13798 if Is_Tagged_Type (Full) then
13799 Set_Direct_Primitive_Operations
13800 (Full, Direct_Primitive_Operations (Priv));
13801 Set_No_Tagged_Streams_Pragma
13802 (Full, No_Tagged_Streams_Pragma (Priv));
13803
13804 if Is_Base_Type (Priv) then
13805 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13806 end if;
13807 end if;
13808
13809 Set_Is_Volatile (Full, Is_Volatile (Priv));
13810 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13811 Set_Scope (Full, Scope (Priv));
13812 Set_Next_Entity (Full, Next_Entity (Priv));
13813 Set_First_Entity (Full, First_Entity (Priv));
13814 Set_Last_Entity (Full, Last_Entity (Priv));
13815
13816 -- If access types have been recorded for later handling, keep them in
13817 -- the full view so that they get handled when the full view freeze
13818 -- node is expanded.
13819
13820 if Present (Freeze_Node (Priv))
13821 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13822 then
13823 Ensure_Freeze_Node (Full);
13824 Set_Access_Types_To_Process
13825 (Freeze_Node (Full),
13826 Access_Types_To_Process (Freeze_Node (Priv)));
13827 end if;
13828
13829 -- Swap the two entities. Now Private is the full type entity and Full
13830 -- is the private one. They will be swapped back at the end of the
13831 -- private part. This swapping ensures that the entity that is visible
13832 -- in the private part is the full declaration.
13833
13834 Exchange_Entities (Priv, Full);
13835 Append_Entity (Full, Scope (Full));
13836 end Copy_And_Swap;
13837
13838 -------------------------------------
13839 -- Copy_Array_Base_Type_Attributes --
13840 -------------------------------------
13841
13842 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13843 begin
13844 Set_Component_Alignment (T1, Component_Alignment (T2));
13845 Set_Component_Type (T1, Component_Type (T2));
13846 Set_Component_Size (T1, Component_Size (T2));
13847 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13848 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13849 Propagate_Concurrent_Flags (T1, T2);
13850 Set_Is_Packed (T1, Is_Packed (T2));
13851 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13852 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13853 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13854 end Copy_Array_Base_Type_Attributes;
13855
13856 -----------------------------------
13857 -- Copy_Array_Subtype_Attributes --
13858 -----------------------------------
13859
13860 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13861 begin
13862 Set_Size_Info (T1, T2);
13863
13864 Set_First_Index (T1, First_Index (T2));
13865 Set_Is_Aliased (T1, Is_Aliased (T2));
13866 Set_Is_Volatile (T1, Is_Volatile (T2));
13867 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13868 Set_Is_Constrained (T1, Is_Constrained (T2));
13869 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13870 Inherit_Rep_Item_Chain (T1, T2);
13871 Set_Convention (T1, Convention (T2));
13872 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13873 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13874 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13875 end Copy_Array_Subtype_Attributes;
13876
13877 -----------------------------------
13878 -- Create_Constrained_Components --
13879 -----------------------------------
13880
13881 procedure Create_Constrained_Components
13882 (Subt : Entity_Id;
13883 Decl_Node : Node_Id;
13884 Typ : Entity_Id;
13885 Constraints : Elist_Id)
13886 is
13887 Loc : constant Source_Ptr := Sloc (Subt);
13888 Comp_List : constant Elist_Id := New_Elmt_List;
13889 Parent_Type : constant Entity_Id := Etype (Typ);
13890 Assoc_List : constant List_Id := New_List;
13891 Discr_Val : Elmt_Id;
13892 Errors : Boolean;
13893 New_C : Entity_Id;
13894 Old_C : Entity_Id;
13895 Is_Static : Boolean := True;
13896
13897 procedure Collect_Fixed_Components (Typ : Entity_Id);
13898 -- Collect parent type components that do not appear in a variant part
13899
13900 procedure Create_All_Components;
13901 -- Iterate over Comp_List to create the components of the subtype
13902
13903 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13904 -- Creates a new component from Old_Compon, copying all the fields from
13905 -- it, including its Etype, inserts the new component in the Subt entity
13906 -- chain and returns the new component.
13907
13908 function Is_Variant_Record (T : Entity_Id) return Boolean;
13909 -- If true, and discriminants are static, collect only components from
13910 -- variants selected by discriminant values.
13911
13912 ------------------------------
13913 -- Collect_Fixed_Components --
13914 ------------------------------
13915
13916 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13917 begin
13918 -- Build association list for discriminants, and find components of the
13919 -- variant part selected by the values of the discriminants.
13920
13921 Old_C := First_Discriminant (Typ);
13922 Discr_Val := First_Elmt (Constraints);
13923 while Present (Old_C) loop
13924 Append_To (Assoc_List,
13925 Make_Component_Association (Loc,
13926 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13927 Expression => New_Copy (Node (Discr_Val))));
13928
13929 Next_Elmt (Discr_Val);
13930 Next_Discriminant (Old_C);
13931 end loop;
13932
13933 -- The tag and the possible parent component are unconditionally in
13934 -- the subtype.
13935
13936 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13937 Old_C := First_Component (Typ);
13938 while Present (Old_C) loop
13939 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13940 Append_Elmt (Old_C, Comp_List);
13941 end if;
13942
13943 Next_Component (Old_C);
13944 end loop;
13945 end if;
13946 end Collect_Fixed_Components;
13947
13948 ---------------------------
13949 -- Create_All_Components --
13950 ---------------------------
13951
13952 procedure Create_All_Components is
13953 Comp : Elmt_Id;
13954
13955 begin
13956 Comp := First_Elmt (Comp_List);
13957 while Present (Comp) loop
13958 Old_C := Node (Comp);
13959 New_C := Create_Component (Old_C);
13960
13961 Set_Etype
13962 (New_C,
13963 Constrain_Component_Type
13964 (Old_C, Subt, Decl_Node, Typ, Constraints));
13965 Set_Is_Public (New_C, Is_Public (Subt));
13966
13967 Next_Elmt (Comp);
13968 end loop;
13969 end Create_All_Components;
13970
13971 ----------------------
13972 -- Create_Component --
13973 ----------------------
13974
13975 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13976 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13977
13978 begin
13979 if Ekind (Old_Compon) = E_Discriminant
13980 and then Is_Completely_Hidden (Old_Compon)
13981 then
13982 -- This is a shadow discriminant created for a discriminant of
13983 -- the parent type, which needs to be present in the subtype.
13984 -- Give the shadow discriminant an internal name that cannot
13985 -- conflict with that of visible components.
13986
13987 Set_Chars (New_Compon, New_Internal_Name ('C'));
13988 end if;
13989
13990 -- Set the parent so we have a proper link for freezing etc. This is
13991 -- not a real parent pointer, since of course our parent does not own
13992 -- up to us and reference us, we are an illegitimate child of the
13993 -- original parent.
13994
13995 Set_Parent (New_Compon, Parent (Old_Compon));
13996
13997 -- If the old component's Esize was already determined and is a
13998 -- static value, then the new component simply inherits it. Otherwise
13999 -- the old component's size may require run-time determination, but
14000 -- the new component's size still might be statically determinable
14001 -- (if, for example it has a static constraint). In that case we want
14002 -- Layout_Type to recompute the component's size, so we reset its
14003 -- size and positional fields.
14004
14005 if Frontend_Layout_On_Target
14006 and then not Known_Static_Esize (Old_Compon)
14007 then
14008 Set_Esize (New_Compon, Uint_0);
14009 Init_Normalized_First_Bit (New_Compon);
14010 Init_Normalized_Position (New_Compon);
14011 Init_Normalized_Position_Max (New_Compon);
14012 end if;
14013
14014 -- We do not want this node marked as Comes_From_Source, since
14015 -- otherwise it would get first class status and a separate cross-
14016 -- reference line would be generated. Illegitimate children do not
14017 -- rate such recognition.
14018
14019 Set_Comes_From_Source (New_Compon, False);
14020
14021 -- But it is a real entity, and a birth certificate must be properly
14022 -- registered by entering it into the entity list.
14023
14024 Enter_Name (New_Compon);
14025
14026 return New_Compon;
14027 end Create_Component;
14028
14029 -----------------------
14030 -- Is_Variant_Record --
14031 -----------------------
14032
14033 function Is_Variant_Record (T : Entity_Id) return Boolean is
14034 begin
14035 return Nkind (Parent (T)) = N_Full_Type_Declaration
14036 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14037 and then Present (Component_List (Type_Definition (Parent (T))))
14038 and then
14039 Present
14040 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14041 end Is_Variant_Record;
14042
14043 -- Start of processing for Create_Constrained_Components
14044
14045 begin
14046 pragma Assert (Subt /= Base_Type (Subt));
14047 pragma Assert (Typ = Base_Type (Typ));
14048
14049 Set_First_Entity (Subt, Empty);
14050 Set_Last_Entity (Subt, Empty);
14051
14052 -- Check whether constraint is fully static, in which case we can
14053 -- optimize the list of components.
14054
14055 Discr_Val := First_Elmt (Constraints);
14056 while Present (Discr_Val) loop
14057 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14058 Is_Static := False;
14059 exit;
14060 end if;
14061
14062 Next_Elmt (Discr_Val);
14063 end loop;
14064
14065 Set_Has_Static_Discriminants (Subt, Is_Static);
14066
14067 Push_Scope (Subt);
14068
14069 -- Inherit the discriminants of the parent type
14070
14071 Add_Discriminants : declare
14072 Num_Disc : Nat;
14073 Num_Gird : Nat;
14074
14075 begin
14076 Num_Disc := 0;
14077 Old_C := First_Discriminant (Typ);
14078
14079 while Present (Old_C) loop
14080 Num_Disc := Num_Disc + 1;
14081 New_C := Create_Component (Old_C);
14082 Set_Is_Public (New_C, Is_Public (Subt));
14083 Next_Discriminant (Old_C);
14084 end loop;
14085
14086 -- For an untagged derived subtype, the number of discriminants may
14087 -- be smaller than the number of inherited discriminants, because
14088 -- several of them may be renamed by a single new discriminant or
14089 -- constrained. In this case, add the hidden discriminants back into
14090 -- the subtype, because they need to be present if the optimizer of
14091 -- the GCC 4.x back-end decides to break apart assignments between
14092 -- objects using the parent view into member-wise assignments.
14093
14094 Num_Gird := 0;
14095
14096 if Is_Derived_Type (Typ)
14097 and then not Is_Tagged_Type (Typ)
14098 then
14099 Old_C := First_Stored_Discriminant (Typ);
14100
14101 while Present (Old_C) loop
14102 Num_Gird := Num_Gird + 1;
14103 Next_Stored_Discriminant (Old_C);
14104 end loop;
14105 end if;
14106
14107 if Num_Gird > Num_Disc then
14108
14109 -- Find out multiple uses of new discriminants, and add hidden
14110 -- components for the extra renamed discriminants. We recognize
14111 -- multiple uses through the Corresponding_Discriminant of a
14112 -- new discriminant: if it constrains several old discriminants,
14113 -- this field points to the last one in the parent type. The
14114 -- stored discriminants of the derived type have the same name
14115 -- as those of the parent.
14116
14117 declare
14118 Constr : Elmt_Id;
14119 New_Discr : Entity_Id;
14120 Old_Discr : Entity_Id;
14121
14122 begin
14123 Constr := First_Elmt (Stored_Constraint (Typ));
14124 Old_Discr := First_Stored_Discriminant (Typ);
14125 while Present (Constr) loop
14126 if Is_Entity_Name (Node (Constr))
14127 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14128 then
14129 New_Discr := Entity (Node (Constr));
14130
14131 if Chars (Corresponding_Discriminant (New_Discr)) /=
14132 Chars (Old_Discr)
14133 then
14134 -- The new discriminant has been used to rename a
14135 -- subsequent old discriminant. Introduce a shadow
14136 -- component for the current old discriminant.
14137
14138 New_C := Create_Component (Old_Discr);
14139 Set_Original_Record_Component (New_C, Old_Discr);
14140 end if;
14141
14142 else
14143 -- The constraint has eliminated the old discriminant.
14144 -- Introduce a shadow component.
14145
14146 New_C := Create_Component (Old_Discr);
14147 Set_Original_Record_Component (New_C, Old_Discr);
14148 end if;
14149
14150 Next_Elmt (Constr);
14151 Next_Stored_Discriminant (Old_Discr);
14152 end loop;
14153 end;
14154 end if;
14155 end Add_Discriminants;
14156
14157 if Is_Static
14158 and then Is_Variant_Record (Typ)
14159 then
14160 Collect_Fixed_Components (Typ);
14161
14162 Gather_Components (
14163 Typ,
14164 Component_List (Type_Definition (Parent (Typ))),
14165 Governed_By => Assoc_List,
14166 Into => Comp_List,
14167 Report_Errors => Errors);
14168 pragma Assert (not Errors
14169 or else Serious_Errors_Detected > 0);
14170
14171 Create_All_Components;
14172
14173 -- If the subtype declaration is created for a tagged type derivation
14174 -- with constraints, we retrieve the record definition of the parent
14175 -- type to select the components of the proper variant.
14176
14177 elsif Is_Static
14178 and then Is_Tagged_Type (Typ)
14179 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14180 and then
14181 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14182 and then Is_Variant_Record (Parent_Type)
14183 then
14184 Collect_Fixed_Components (Typ);
14185
14186 Gather_Components
14187 (Typ,
14188 Component_List (Type_Definition (Parent (Parent_Type))),
14189 Governed_By => Assoc_List,
14190 Into => Comp_List,
14191 Report_Errors => Errors);
14192
14193 -- Note: previously there was a check at this point that no errors
14194 -- were detected. As a consequence of AI05-220 there may be an error
14195 -- if an inherited discriminant that controls a variant has a non-
14196 -- static constraint.
14197
14198 -- If the tagged derivation has a type extension, collect all the
14199 -- new components therein.
14200
14201 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14202 then
14203 Old_C := First_Component (Typ);
14204 while Present (Old_C) loop
14205 if Original_Record_Component (Old_C) = Old_C
14206 and then Chars (Old_C) /= Name_uTag
14207 and then Chars (Old_C) /= Name_uParent
14208 then
14209 Append_Elmt (Old_C, Comp_List);
14210 end if;
14211
14212 Next_Component (Old_C);
14213 end loop;
14214 end if;
14215
14216 Create_All_Components;
14217
14218 else
14219 -- If discriminants are not static, or if this is a multi-level type
14220 -- extension, we have to include all components of the parent type.
14221
14222 Old_C := First_Component (Typ);
14223 while Present (Old_C) loop
14224 New_C := Create_Component (Old_C);
14225
14226 Set_Etype
14227 (New_C,
14228 Constrain_Component_Type
14229 (Old_C, Subt, Decl_Node, Typ, Constraints));
14230 Set_Is_Public (New_C, Is_Public (Subt));
14231
14232 Next_Component (Old_C);
14233 end loop;
14234 end if;
14235
14236 End_Scope;
14237 end Create_Constrained_Components;
14238
14239 ------------------------------------------
14240 -- Decimal_Fixed_Point_Type_Declaration --
14241 ------------------------------------------
14242
14243 procedure Decimal_Fixed_Point_Type_Declaration
14244 (T : Entity_Id;
14245 Def : Node_Id)
14246 is
14247 Loc : constant Source_Ptr := Sloc (Def);
14248 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14249 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14250 Implicit_Base : Entity_Id;
14251 Digs_Val : Uint;
14252 Delta_Val : Ureal;
14253 Scale_Val : Uint;
14254 Bound_Val : Ureal;
14255
14256 begin
14257 Check_SPARK_05_Restriction
14258 ("decimal fixed point type is not allowed", Def);
14259 Check_Restriction (No_Fixed_Point, Def);
14260
14261 -- Create implicit base type
14262
14263 Implicit_Base :=
14264 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14265 Set_Etype (Implicit_Base, Implicit_Base);
14266
14267 -- Analyze and process delta expression
14268
14269 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14270
14271 Check_Delta_Expression (Delta_Expr);
14272 Delta_Val := Expr_Value_R (Delta_Expr);
14273
14274 -- Check delta is power of 10, and determine scale value from it
14275
14276 declare
14277 Val : Ureal;
14278
14279 begin
14280 Scale_Val := Uint_0;
14281 Val := Delta_Val;
14282
14283 if Val < Ureal_1 then
14284 while Val < Ureal_1 loop
14285 Val := Val * Ureal_10;
14286 Scale_Val := Scale_Val + 1;
14287 end loop;
14288
14289 if Scale_Val > 18 then
14290 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14291 Scale_Val := UI_From_Int (+18);
14292 end if;
14293
14294 else
14295 while Val > Ureal_1 loop
14296 Val := Val / Ureal_10;
14297 Scale_Val := Scale_Val - 1;
14298 end loop;
14299
14300 if Scale_Val < -18 then
14301 Error_Msg_N ("scale is less than minimum value of -18", Def);
14302 Scale_Val := UI_From_Int (-18);
14303 end if;
14304 end if;
14305
14306 if Val /= Ureal_1 then
14307 Error_Msg_N ("delta expression must be a power of 10", Def);
14308 Delta_Val := Ureal_10 ** (-Scale_Val);
14309 end if;
14310 end;
14311
14312 -- Set delta, scale and small (small = delta for decimal type)
14313
14314 Set_Delta_Value (Implicit_Base, Delta_Val);
14315 Set_Scale_Value (Implicit_Base, Scale_Val);
14316 Set_Small_Value (Implicit_Base, Delta_Val);
14317
14318 -- Analyze and process digits expression
14319
14320 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14321 Check_Digits_Expression (Digs_Expr);
14322 Digs_Val := Expr_Value (Digs_Expr);
14323
14324 if Digs_Val > 18 then
14325 Digs_Val := UI_From_Int (+18);
14326 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14327 end if;
14328
14329 Set_Digits_Value (Implicit_Base, Digs_Val);
14330 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14331
14332 -- Set range of base type from digits value for now. This will be
14333 -- expanded to represent the true underlying base range by Freeze.
14334
14335 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14336
14337 -- Note: We leave size as zero for now, size will be set at freeze
14338 -- time. We have to do this for ordinary fixed-point, because the size
14339 -- depends on the specified small, and we might as well do the same for
14340 -- decimal fixed-point.
14341
14342 pragma Assert (Esize (Implicit_Base) = Uint_0);
14343
14344 -- If there are bounds given in the declaration use them as the
14345 -- bounds of the first named subtype.
14346
14347 if Present (Real_Range_Specification (Def)) then
14348 declare
14349 RRS : constant Node_Id := Real_Range_Specification (Def);
14350 Low : constant Node_Id := Low_Bound (RRS);
14351 High : constant Node_Id := High_Bound (RRS);
14352 Low_Val : Ureal;
14353 High_Val : Ureal;
14354
14355 begin
14356 Analyze_And_Resolve (Low, Any_Real);
14357 Analyze_And_Resolve (High, Any_Real);
14358 Check_Real_Bound (Low);
14359 Check_Real_Bound (High);
14360 Low_Val := Expr_Value_R (Low);
14361 High_Val := Expr_Value_R (High);
14362
14363 if Low_Val < (-Bound_Val) then
14364 Error_Msg_N
14365 ("range low bound too small for digits value", Low);
14366 Low_Val := -Bound_Val;
14367 end if;
14368
14369 if High_Val > Bound_Val then
14370 Error_Msg_N
14371 ("range high bound too large for digits value", High);
14372 High_Val := Bound_Val;
14373 end if;
14374
14375 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14376 end;
14377
14378 -- If no explicit range, use range that corresponds to given
14379 -- digits value. This will end up as the final range for the
14380 -- first subtype.
14381
14382 else
14383 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14384 end if;
14385
14386 -- Complete entity for first subtype. The inheritance of the rep item
14387 -- chain ensures that SPARK-related pragmas are not clobbered when the
14388 -- decimal fixed point type acts as a full view of a private type.
14389
14390 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14391 Set_Etype (T, Implicit_Base);
14392 Set_Size_Info (T, Implicit_Base);
14393 Inherit_Rep_Item_Chain (T, Implicit_Base);
14394 Set_Digits_Value (T, Digs_Val);
14395 Set_Delta_Value (T, Delta_Val);
14396 Set_Small_Value (T, Delta_Val);
14397 Set_Scale_Value (T, Scale_Val);
14398 Set_Is_Constrained (T);
14399 end Decimal_Fixed_Point_Type_Declaration;
14400
14401 -----------------------------------
14402 -- Derive_Progenitor_Subprograms --
14403 -----------------------------------
14404
14405 procedure Derive_Progenitor_Subprograms
14406 (Parent_Type : Entity_Id;
14407 Tagged_Type : Entity_Id)
14408 is
14409 E : Entity_Id;
14410 Elmt : Elmt_Id;
14411 Iface : Entity_Id;
14412 Iface_Elmt : Elmt_Id;
14413 Iface_Subp : Entity_Id;
14414 New_Subp : Entity_Id := Empty;
14415 Prim_Elmt : Elmt_Id;
14416 Subp : Entity_Id;
14417 Typ : Entity_Id;
14418
14419 begin
14420 pragma Assert (Ada_Version >= Ada_2005
14421 and then Is_Record_Type (Tagged_Type)
14422 and then Is_Tagged_Type (Tagged_Type)
14423 and then Has_Interfaces (Tagged_Type));
14424
14425 -- Step 1: Transfer to the full-view primitives associated with the
14426 -- partial-view that cover interface primitives. Conceptually this
14427 -- work should be done later by Process_Full_View; done here to
14428 -- simplify its implementation at later stages. It can be safely
14429 -- done here because interfaces must be visible in the partial and
14430 -- private view (RM 7.3(7.3/2)).
14431
14432 -- Small optimization: This work is only required if the parent may
14433 -- have entities whose Alias attribute reference an interface primitive.
14434 -- Such a situation may occur if the parent is an abstract type and the
14435 -- primitive has not been yet overridden or if the parent is a generic
14436 -- formal type covering interfaces.
14437
14438 -- If the tagged type is not abstract, it cannot have abstract
14439 -- primitives (the only entities in the list of primitives of
14440 -- non-abstract tagged types that can reference abstract primitives
14441 -- through its Alias attribute are the internal entities that have
14442 -- attribute Interface_Alias, and these entities are generated later
14443 -- by Add_Internal_Interface_Entities).
14444
14445 if In_Private_Part (Current_Scope)
14446 and then (Is_Abstract_Type (Parent_Type)
14447 or else
14448 Is_Generic_Type (Parent_Type))
14449 then
14450 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14451 while Present (Elmt) loop
14452 Subp := Node (Elmt);
14453
14454 -- At this stage it is not possible to have entities in the list
14455 -- of primitives that have attribute Interface_Alias.
14456
14457 pragma Assert (No (Interface_Alias (Subp)));
14458
14459 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14460
14461 if Is_Interface (Typ) then
14462 E := Find_Primitive_Covering_Interface
14463 (Tagged_Type => Tagged_Type,
14464 Iface_Prim => Subp);
14465
14466 if Present (E)
14467 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14468 then
14469 Replace_Elmt (Elmt, E);
14470 Remove_Homonym (Subp);
14471 end if;
14472 end if;
14473
14474 Next_Elmt (Elmt);
14475 end loop;
14476 end if;
14477
14478 -- Step 2: Add primitives of progenitors that are not implemented by
14479 -- parents of Tagged_Type.
14480
14481 if Present (Interfaces (Base_Type (Tagged_Type))) then
14482 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14483 while Present (Iface_Elmt) loop
14484 Iface := Node (Iface_Elmt);
14485
14486 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14487 while Present (Prim_Elmt) loop
14488 Iface_Subp := Node (Prim_Elmt);
14489
14490 -- Exclude derivation of predefined primitives except those
14491 -- that come from source, or are inherited from one that comes
14492 -- from source. Required to catch declarations of equality
14493 -- operators of interfaces. For example:
14494
14495 -- type Iface is interface;
14496 -- function "=" (Left, Right : Iface) return Boolean;
14497
14498 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14499 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14500 then
14501 E := Find_Primitive_Covering_Interface
14502 (Tagged_Type => Tagged_Type,
14503 Iface_Prim => Iface_Subp);
14504
14505 -- If not found we derive a new primitive leaving its alias
14506 -- attribute referencing the interface primitive.
14507
14508 if No (E) then
14509 Derive_Subprogram
14510 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14511
14512 -- Ada 2012 (AI05-0197): If the covering primitive's name
14513 -- differs from the name of the interface primitive then it
14514 -- is a private primitive inherited from a parent type. In
14515 -- such case, given that Tagged_Type covers the interface,
14516 -- the inherited private primitive becomes visible. For such
14517 -- purpose we add a new entity that renames the inherited
14518 -- private primitive.
14519
14520 elsif Chars (E) /= Chars (Iface_Subp) then
14521 pragma Assert (Has_Suffix (E, 'P'));
14522 Derive_Subprogram
14523 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14524 Set_Alias (New_Subp, E);
14525 Set_Is_Abstract_Subprogram (New_Subp,
14526 Is_Abstract_Subprogram (E));
14527
14528 -- Propagate to the full view interface entities associated
14529 -- with the partial view.
14530
14531 elsif In_Private_Part (Current_Scope)
14532 and then Present (Alias (E))
14533 and then Alias (E) = Iface_Subp
14534 and then
14535 List_Containing (Parent (E)) /=
14536 Private_Declarations
14537 (Specification
14538 (Unit_Declaration_Node (Current_Scope)))
14539 then
14540 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14541 end if;
14542 end if;
14543
14544 Next_Elmt (Prim_Elmt);
14545 end loop;
14546
14547 Next_Elmt (Iface_Elmt);
14548 end loop;
14549 end if;
14550 end Derive_Progenitor_Subprograms;
14551
14552 -----------------------
14553 -- Derive_Subprogram --
14554 -----------------------
14555
14556 procedure Derive_Subprogram
14557 (New_Subp : out Entity_Id;
14558 Parent_Subp : Entity_Id;
14559 Derived_Type : Entity_Id;
14560 Parent_Type : Entity_Id;
14561 Actual_Subp : Entity_Id := Empty)
14562 is
14563 Formal : Entity_Id;
14564 -- Formal parameter of parent primitive operation
14565
14566 Formal_Of_Actual : Entity_Id;
14567 -- Formal parameter of actual operation, when the derivation is to
14568 -- create a renaming for a primitive operation of an actual in an
14569 -- instantiation.
14570
14571 New_Formal : Entity_Id;
14572 -- Formal of inherited operation
14573
14574 Visible_Subp : Entity_Id := Parent_Subp;
14575
14576 function Is_Private_Overriding return Boolean;
14577 -- If Subp is a private overriding of a visible operation, the inherited
14578 -- operation derives from the overridden op (even though its body is the
14579 -- overriding one) and the inherited operation is visible now. See
14580 -- sem_disp to see the full details of the handling of the overridden
14581 -- subprogram, which is removed from the list of primitive operations of
14582 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14583 -- and used to diagnose abstract operations that need overriding in the
14584 -- derived type.
14585
14586 procedure Replace_Type (Id, New_Id : Entity_Id);
14587 -- When the type is an anonymous access type, create a new access type
14588 -- designating the derived type.
14589
14590 procedure Set_Derived_Name;
14591 -- This procedure sets the appropriate Chars name for New_Subp. This
14592 -- is normally just a copy of the parent name. An exception arises for
14593 -- type support subprograms, where the name is changed to reflect the
14594 -- name of the derived type, e.g. if type foo is derived from type bar,
14595 -- then a procedure barDA is derived with a name fooDA.
14596
14597 ---------------------------
14598 -- Is_Private_Overriding --
14599 ---------------------------
14600
14601 function Is_Private_Overriding return Boolean is
14602 Prev : Entity_Id;
14603
14604 begin
14605 -- If the parent is not a dispatching operation there is no
14606 -- need to investigate overridings
14607
14608 if not Is_Dispatching_Operation (Parent_Subp) then
14609 return False;
14610 end if;
14611
14612 -- The visible operation that is overridden is a homonym of the
14613 -- parent subprogram. We scan the homonym chain to find the one
14614 -- whose alias is the subprogram we are deriving.
14615
14616 Prev := Current_Entity (Parent_Subp);
14617 while Present (Prev) loop
14618 if Ekind (Prev) = Ekind (Parent_Subp)
14619 and then Alias (Prev) = Parent_Subp
14620 and then Scope (Parent_Subp) = Scope (Prev)
14621 and then not Is_Hidden (Prev)
14622 then
14623 Visible_Subp := Prev;
14624 return True;
14625 end if;
14626
14627 Prev := Homonym (Prev);
14628 end loop;
14629
14630 return False;
14631 end Is_Private_Overriding;
14632
14633 ------------------
14634 -- Replace_Type --
14635 ------------------
14636
14637 procedure Replace_Type (Id, New_Id : Entity_Id) is
14638 Id_Type : constant Entity_Id := Etype (Id);
14639 Acc_Type : Entity_Id;
14640 Par : constant Node_Id := Parent (Derived_Type);
14641
14642 begin
14643 -- When the type is an anonymous access type, create a new access
14644 -- type designating the derived type. This itype must be elaborated
14645 -- at the point of the derivation, not on subsequent calls that may
14646 -- be out of the proper scope for Gigi, so we insert a reference to
14647 -- it after the derivation.
14648
14649 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14650 declare
14651 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14652
14653 begin
14654 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14655 and then Present (Full_View (Desig_Typ))
14656 and then not Is_Private_Type (Parent_Type)
14657 then
14658 Desig_Typ := Full_View (Desig_Typ);
14659 end if;
14660
14661 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14662
14663 -- Ada 2005 (AI-251): Handle also derivations of abstract
14664 -- interface primitives.
14665
14666 or else (Is_Interface (Desig_Typ)
14667 and then not Is_Class_Wide_Type (Desig_Typ))
14668 then
14669 Acc_Type := New_Copy (Id_Type);
14670 Set_Etype (Acc_Type, Acc_Type);
14671 Set_Scope (Acc_Type, New_Subp);
14672
14673 -- Set size of anonymous access type. If we have an access
14674 -- to an unconstrained array, this is a fat pointer, so it
14675 -- is sizes at twice addtress size.
14676
14677 if Is_Array_Type (Desig_Typ)
14678 and then not Is_Constrained (Desig_Typ)
14679 then
14680 Init_Size (Acc_Type, 2 * System_Address_Size);
14681
14682 -- Other cases use a thin pointer
14683
14684 else
14685 Init_Size (Acc_Type, System_Address_Size);
14686 end if;
14687
14688 -- Set remaining characterstics of anonymous access type
14689
14690 Init_Alignment (Acc_Type);
14691 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14692
14693 Set_Etype (New_Id, Acc_Type);
14694 Set_Scope (New_Id, New_Subp);
14695
14696 -- Create a reference to it
14697
14698 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14699
14700 else
14701 Set_Etype (New_Id, Id_Type);
14702 end if;
14703 end;
14704
14705 -- In Ada2012, a formal may have an incomplete type but the type
14706 -- derivation that inherits the primitive follows the full view.
14707
14708 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14709 or else
14710 (Ekind (Id_Type) = E_Record_Type_With_Private
14711 and then Present (Full_View (Id_Type))
14712 and then
14713 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14714 or else
14715 (Ada_Version >= Ada_2012
14716 and then Ekind (Id_Type) = E_Incomplete_Type
14717 and then Full_View (Id_Type) = Parent_Type)
14718 then
14719 -- Constraint checks on formals are generated during expansion,
14720 -- based on the signature of the original subprogram. The bounds
14721 -- of the derived type are not relevant, and thus we can use
14722 -- the base type for the formals. However, the return type may be
14723 -- used in a context that requires that the proper static bounds
14724 -- be used (a case statement, for example) and for those cases
14725 -- we must use the derived type (first subtype), not its base.
14726
14727 -- If the derived_type_definition has no constraints, we know that
14728 -- the derived type has the same constraints as the first subtype
14729 -- of the parent, and we can also use it rather than its base,
14730 -- which can lead to more efficient code.
14731
14732 if Etype (Id) = Parent_Type then
14733 if Is_Scalar_Type (Parent_Type)
14734 and then
14735 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14736 then
14737 Set_Etype (New_Id, Derived_Type);
14738
14739 elsif Nkind (Par) = N_Full_Type_Declaration
14740 and then
14741 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14742 and then
14743 Is_Entity_Name
14744 (Subtype_Indication (Type_Definition (Par)))
14745 then
14746 Set_Etype (New_Id, Derived_Type);
14747
14748 else
14749 Set_Etype (New_Id, Base_Type (Derived_Type));
14750 end if;
14751
14752 else
14753 Set_Etype (New_Id, Base_Type (Derived_Type));
14754 end if;
14755
14756 else
14757 Set_Etype (New_Id, Etype (Id));
14758 end if;
14759 end Replace_Type;
14760
14761 ----------------------
14762 -- Set_Derived_Name --
14763 ----------------------
14764
14765 procedure Set_Derived_Name is
14766 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14767 begin
14768 if Nm = TSS_Null then
14769 Set_Chars (New_Subp, Chars (Parent_Subp));
14770 else
14771 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14772 end if;
14773 end Set_Derived_Name;
14774
14775 -- Start of processing for Derive_Subprogram
14776
14777 begin
14778 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14779 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14780
14781 -- Check whether the inherited subprogram is a private operation that
14782 -- should be inherited but not yet made visible. Such subprograms can
14783 -- become visible at a later point (e.g., the private part of a public
14784 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14785 -- following predicate is true, then this is not such a private
14786 -- operation and the subprogram simply inherits the name of the parent
14787 -- subprogram. Note the special check for the names of controlled
14788 -- operations, which are currently exempted from being inherited with
14789 -- a hidden name because they must be findable for generation of
14790 -- implicit run-time calls.
14791
14792 if not Is_Hidden (Parent_Subp)
14793 or else Is_Internal (Parent_Subp)
14794 or else Is_Private_Overriding
14795 or else Is_Internal_Name (Chars (Parent_Subp))
14796 or else (Is_Controlled (Parent_Type)
14797 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
14798 Name_Finalize,
14799 Name_Initialize))
14800 then
14801 Set_Derived_Name;
14802
14803 -- An inherited dispatching equality will be overridden by an internally
14804 -- generated one, or by an explicit one, so preserve its name and thus
14805 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14806 -- private operation it may become invisible if the full view has
14807 -- progenitors, and the dispatch table will be malformed.
14808 -- We check that the type is limited to handle the anomalous declaration
14809 -- of Limited_Controlled, which is derived from a non-limited type, and
14810 -- which is handled specially elsewhere as well.
14811
14812 elsif Chars (Parent_Subp) = Name_Op_Eq
14813 and then Is_Dispatching_Operation (Parent_Subp)
14814 and then Etype (Parent_Subp) = Standard_Boolean
14815 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14816 and then
14817 Etype (First_Formal (Parent_Subp)) =
14818 Etype (Next_Formal (First_Formal (Parent_Subp)))
14819 then
14820 Set_Derived_Name;
14821
14822 -- If parent is hidden, this can be a regular derivation if the
14823 -- parent is immediately visible in a non-instantiating context,
14824 -- or if we are in the private part of an instance. This test
14825 -- should still be refined ???
14826
14827 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14828 -- operation as a non-visible operation in cases where the parent
14829 -- subprogram might not be visible now, but was visible within the
14830 -- original generic, so it would be wrong to make the inherited
14831 -- subprogram non-visible now. (Not clear if this test is fully
14832 -- correct; are there any cases where we should declare the inherited
14833 -- operation as not visible to avoid it being overridden, e.g., when
14834 -- the parent type is a generic actual with private primitives ???)
14835
14836 -- (they should be treated the same as other private inherited
14837 -- subprograms, but it's not clear how to do this cleanly). ???
14838
14839 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14840 and then Is_Immediately_Visible (Parent_Subp)
14841 and then not In_Instance)
14842 or else In_Instance_Not_Visible
14843 then
14844 Set_Derived_Name;
14845
14846 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14847 -- overrides an interface primitive because interface primitives
14848 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14849
14850 elsif Ada_Version >= Ada_2005
14851 and then Is_Dispatching_Operation (Parent_Subp)
14852 and then Covers_Some_Interface (Parent_Subp)
14853 then
14854 Set_Derived_Name;
14855
14856 -- Otherwise, the type is inheriting a private operation, so enter it
14857 -- with a special name so it can't be overridden.
14858
14859 else
14860 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14861 end if;
14862
14863 Set_Parent (New_Subp, Parent (Derived_Type));
14864
14865 if Present (Actual_Subp) then
14866 Replace_Type (Actual_Subp, New_Subp);
14867 else
14868 Replace_Type (Parent_Subp, New_Subp);
14869 end if;
14870
14871 Conditional_Delay (New_Subp, Parent_Subp);
14872
14873 -- If we are creating a renaming for a primitive operation of an
14874 -- actual of a generic derived type, we must examine the signature
14875 -- of the actual primitive, not that of the generic formal, which for
14876 -- example may be an interface. However the name and initial value
14877 -- of the inherited operation are those of the formal primitive.
14878
14879 Formal := First_Formal (Parent_Subp);
14880
14881 if Present (Actual_Subp) then
14882 Formal_Of_Actual := First_Formal (Actual_Subp);
14883 else
14884 Formal_Of_Actual := Empty;
14885 end if;
14886
14887 while Present (Formal) loop
14888 New_Formal := New_Copy (Formal);
14889
14890 -- Normally we do not go copying parents, but in the case of
14891 -- formals, we need to link up to the declaration (which is the
14892 -- parameter specification), and it is fine to link up to the
14893 -- original formal's parameter specification in this case.
14894
14895 Set_Parent (New_Formal, Parent (Formal));
14896 Append_Entity (New_Formal, New_Subp);
14897
14898 if Present (Formal_Of_Actual) then
14899 Replace_Type (Formal_Of_Actual, New_Formal);
14900 Next_Formal (Formal_Of_Actual);
14901 else
14902 Replace_Type (Formal, New_Formal);
14903 end if;
14904
14905 Next_Formal (Formal);
14906 end loop;
14907
14908 -- If this derivation corresponds to a tagged generic actual, then
14909 -- primitive operations rename those of the actual. Otherwise the
14910 -- primitive operations rename those of the parent type, If the parent
14911 -- renames an intrinsic operator, so does the new subprogram. We except
14912 -- concatenation, which is always properly typed, and does not get
14913 -- expanded as other intrinsic operations.
14914
14915 if No (Actual_Subp) then
14916 if Is_Intrinsic_Subprogram (Parent_Subp) then
14917 Set_Is_Intrinsic_Subprogram (New_Subp);
14918
14919 if Present (Alias (Parent_Subp))
14920 and then Chars (Parent_Subp) /= Name_Op_Concat
14921 then
14922 Set_Alias (New_Subp, Alias (Parent_Subp));
14923 else
14924 Set_Alias (New_Subp, Parent_Subp);
14925 end if;
14926
14927 else
14928 Set_Alias (New_Subp, Parent_Subp);
14929 end if;
14930
14931 else
14932 Set_Alias (New_Subp, Actual_Subp);
14933 end if;
14934
14935 -- Inherit the "ghostness" from the parent subprogram
14936
14937 if Is_Ghost_Entity (Alias (New_Subp)) then
14938 Set_Is_Ghost_Entity (New_Subp);
14939 end if;
14940
14941 -- Derived subprograms of a tagged type must inherit the convention
14942 -- of the parent subprogram (a requirement of AI-117). Derived
14943 -- subprograms of untagged types simply get convention Ada by default.
14944
14945 -- If the derived type is a tagged generic formal type with unknown
14946 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14947
14948 -- However, if the type is derived from a generic formal, the further
14949 -- inherited subprogram has the convention of the non-generic ancestor.
14950 -- Otherwise there would be no way to override the operation.
14951 -- (This is subject to forthcoming ARG discussions).
14952
14953 if Is_Tagged_Type (Derived_Type) then
14954 if Is_Generic_Type (Derived_Type)
14955 and then Has_Unknown_Discriminants (Derived_Type)
14956 then
14957 Set_Convention (New_Subp, Convention_Intrinsic);
14958
14959 else
14960 if Is_Generic_Type (Parent_Type)
14961 and then Has_Unknown_Discriminants (Parent_Type)
14962 then
14963 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14964 else
14965 Set_Convention (New_Subp, Convention (Parent_Subp));
14966 end if;
14967 end if;
14968 end if;
14969
14970 -- Predefined controlled operations retain their name even if the parent
14971 -- is hidden (see above), but they are not primitive operations if the
14972 -- ancestor is not visible, for example if the parent is a private
14973 -- extension completed with a controlled extension. Note that a full
14974 -- type that is controlled can break privacy: the flag Is_Controlled is
14975 -- set on both views of the type.
14976
14977 if Is_Controlled (Parent_Type)
14978 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14979 Name_Adjust,
14980 Name_Finalize)
14981 and then Is_Hidden (Parent_Subp)
14982 and then not Is_Visibly_Controlled (Parent_Type)
14983 then
14984 Set_Is_Hidden (New_Subp);
14985 end if;
14986
14987 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14988 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14989
14990 if Ekind (Parent_Subp) = E_Procedure then
14991 Set_Is_Valued_Procedure
14992 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14993 else
14994 Set_Has_Controlling_Result
14995 (New_Subp, Has_Controlling_Result (Parent_Subp));
14996 end if;
14997
14998 -- No_Return must be inherited properly. If this is overridden in the
14999 -- case of a dispatching operation, then a check is made in Sem_Disp
15000 -- that the overriding operation is also No_Return (no such check is
15001 -- required for the case of non-dispatching operation.
15002
15003 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15004
15005 -- A derived function with a controlling result is abstract. If the
15006 -- Derived_Type is a nonabstract formal generic derived type, then
15007 -- inherited operations are not abstract: the required check is done at
15008 -- instantiation time. If the derivation is for a generic actual, the
15009 -- function is not abstract unless the actual is.
15010
15011 if Is_Generic_Type (Derived_Type)
15012 and then not Is_Abstract_Type (Derived_Type)
15013 then
15014 null;
15015
15016 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15017 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15018
15019 -- A subprogram subject to pragma Extensions_Visible with value False
15020 -- requires overriding if the subprogram has at least one controlling
15021 -- OUT parameter (SPARK RM 6.1.7(6)).
15022
15023 elsif Ada_Version >= Ada_2005
15024 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15025 or else (Is_Tagged_Type (Derived_Type)
15026 and then Etype (New_Subp) = Derived_Type
15027 and then not Is_Null_Extension (Derived_Type))
15028 or else (Is_Tagged_Type (Derived_Type)
15029 and then Ekind (Etype (New_Subp)) =
15030 E_Anonymous_Access_Type
15031 and then Designated_Type (Etype (New_Subp)) =
15032 Derived_Type
15033 and then not Is_Null_Extension (Derived_Type))
15034 or else (Comes_From_Source (Alias (New_Subp))
15035 and then Is_EVF_Procedure (Alias (New_Subp))))
15036 and then No (Actual_Subp)
15037 then
15038 if not Is_Tagged_Type (Derived_Type)
15039 or else Is_Abstract_Type (Derived_Type)
15040 or else Is_Abstract_Subprogram (Alias (New_Subp))
15041 then
15042 Set_Is_Abstract_Subprogram (New_Subp);
15043 else
15044 Set_Requires_Overriding (New_Subp);
15045 end if;
15046
15047 elsif Ada_Version < Ada_2005
15048 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15049 or else (Is_Tagged_Type (Derived_Type)
15050 and then Etype (New_Subp) = Derived_Type
15051 and then No (Actual_Subp)))
15052 then
15053 Set_Is_Abstract_Subprogram (New_Subp);
15054
15055 -- AI05-0097 : an inherited operation that dispatches on result is
15056 -- abstract if the derived type is abstract, even if the parent type
15057 -- is concrete and the derived type is a null extension.
15058
15059 elsif Has_Controlling_Result (Alias (New_Subp))
15060 and then Is_Abstract_Type (Etype (New_Subp))
15061 then
15062 Set_Is_Abstract_Subprogram (New_Subp);
15063
15064 -- Finally, if the parent type is abstract we must verify that all
15065 -- inherited operations are either non-abstract or overridden, or that
15066 -- the derived type itself is abstract (this check is performed at the
15067 -- end of a package declaration, in Check_Abstract_Overriding). A
15068 -- private overriding in the parent type will not be visible in the
15069 -- derivation if we are not in an inner package or in a child unit of
15070 -- the parent type, in which case the abstractness of the inherited
15071 -- operation is carried to the new subprogram.
15072
15073 elsif Is_Abstract_Type (Parent_Type)
15074 and then not In_Open_Scopes (Scope (Parent_Type))
15075 and then Is_Private_Overriding
15076 and then Is_Abstract_Subprogram (Visible_Subp)
15077 then
15078 if No (Actual_Subp) then
15079 Set_Alias (New_Subp, Visible_Subp);
15080 Set_Is_Abstract_Subprogram (New_Subp, True);
15081
15082 else
15083 -- If this is a derivation for an instance of a formal derived
15084 -- type, abstractness comes from the primitive operation of the
15085 -- actual, not from the operation inherited from the ancestor.
15086
15087 Set_Is_Abstract_Subprogram
15088 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15089 end if;
15090 end if;
15091
15092 New_Overloaded_Entity (New_Subp, Derived_Type);
15093
15094 -- Check for case of a derived subprogram for the instantiation of a
15095 -- formal derived tagged type, if so mark the subprogram as dispatching
15096 -- and inherit the dispatching attributes of the actual subprogram. The
15097 -- derived subprogram is effectively renaming of the actual subprogram,
15098 -- so it needs to have the same attributes as the actual.
15099
15100 if Present (Actual_Subp)
15101 and then Is_Dispatching_Operation (Actual_Subp)
15102 then
15103 Set_Is_Dispatching_Operation (New_Subp);
15104
15105 if Present (DTC_Entity (Actual_Subp)) then
15106 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15107 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15108 end if;
15109 end if;
15110
15111 -- Indicate that a derived subprogram does not require a body and that
15112 -- it does not require processing of default expressions.
15113
15114 Set_Has_Completion (New_Subp);
15115 Set_Default_Expressions_Processed (New_Subp);
15116
15117 if Ekind (New_Subp) = E_Function then
15118 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15119 end if;
15120 end Derive_Subprogram;
15121
15122 ------------------------
15123 -- Derive_Subprograms --
15124 ------------------------
15125
15126 procedure Derive_Subprograms
15127 (Parent_Type : Entity_Id;
15128 Derived_Type : Entity_Id;
15129 Generic_Actual : Entity_Id := Empty)
15130 is
15131 Op_List : constant Elist_Id :=
15132 Collect_Primitive_Operations (Parent_Type);
15133
15134 function Check_Derived_Type return Boolean;
15135 -- Check that all the entities derived from Parent_Type are found in
15136 -- the list of primitives of Derived_Type exactly in the same order.
15137
15138 procedure Derive_Interface_Subprogram
15139 (New_Subp : out Entity_Id;
15140 Subp : Entity_Id;
15141 Actual_Subp : Entity_Id);
15142 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15143 -- (which is an interface primitive). If Generic_Actual is present then
15144 -- Actual_Subp is the actual subprogram corresponding with the generic
15145 -- subprogram Subp.
15146
15147 ------------------------
15148 -- Check_Derived_Type --
15149 ------------------------
15150
15151 function Check_Derived_Type return Boolean is
15152 E : Entity_Id;
15153 Elmt : Elmt_Id;
15154 List : Elist_Id;
15155 New_Subp : Entity_Id;
15156 Op_Elmt : Elmt_Id;
15157 Subp : Entity_Id;
15158
15159 begin
15160 -- Traverse list of entities in the current scope searching for
15161 -- an incomplete type whose full-view is derived type.
15162
15163 E := First_Entity (Scope (Derived_Type));
15164 while Present (E) and then E /= Derived_Type loop
15165 if Ekind (E) = E_Incomplete_Type
15166 and then Present (Full_View (E))
15167 and then Full_View (E) = Derived_Type
15168 then
15169 -- Disable this test if Derived_Type completes an incomplete
15170 -- type because in such case more primitives can be added
15171 -- later to the list of primitives of Derived_Type by routine
15172 -- Process_Incomplete_Dependents
15173
15174 return True;
15175 end if;
15176
15177 E := Next_Entity (E);
15178 end loop;
15179
15180 List := Collect_Primitive_Operations (Derived_Type);
15181 Elmt := First_Elmt (List);
15182
15183 Op_Elmt := First_Elmt (Op_List);
15184 while Present (Op_Elmt) loop
15185 Subp := Node (Op_Elmt);
15186 New_Subp := Node (Elmt);
15187
15188 -- At this early stage Derived_Type has no entities with attribute
15189 -- Interface_Alias. In addition, such primitives are always
15190 -- located at the end of the list of primitives of Parent_Type.
15191 -- Therefore, if found we can safely stop processing pending
15192 -- entities.
15193
15194 exit when Present (Interface_Alias (Subp));
15195
15196 -- Handle hidden entities
15197
15198 if not Is_Predefined_Dispatching_Operation (Subp)
15199 and then Is_Hidden (Subp)
15200 then
15201 if Present (New_Subp)
15202 and then Primitive_Names_Match (Subp, New_Subp)
15203 then
15204 Next_Elmt (Elmt);
15205 end if;
15206
15207 else
15208 if not Present (New_Subp)
15209 or else Ekind (Subp) /= Ekind (New_Subp)
15210 or else not Primitive_Names_Match (Subp, New_Subp)
15211 then
15212 return False;
15213 end if;
15214
15215 Next_Elmt (Elmt);
15216 end if;
15217
15218 Next_Elmt (Op_Elmt);
15219 end loop;
15220
15221 return True;
15222 end Check_Derived_Type;
15223
15224 ---------------------------------
15225 -- Derive_Interface_Subprogram --
15226 ---------------------------------
15227
15228 procedure Derive_Interface_Subprogram
15229 (New_Subp : out Entity_Id;
15230 Subp : Entity_Id;
15231 Actual_Subp : Entity_Id)
15232 is
15233 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15234 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15235
15236 begin
15237 pragma Assert (Is_Interface (Iface_Type));
15238
15239 Derive_Subprogram
15240 (New_Subp => New_Subp,
15241 Parent_Subp => Iface_Subp,
15242 Derived_Type => Derived_Type,
15243 Parent_Type => Iface_Type,
15244 Actual_Subp => Actual_Subp);
15245
15246 -- Given that this new interface entity corresponds with a primitive
15247 -- of the parent that was not overridden we must leave it associated
15248 -- with its parent primitive to ensure that it will share the same
15249 -- dispatch table slot when overridden. We must set the Alias to Subp
15250 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15251 -- (in case we inherited Subp from Iface_Type via a nonabstract
15252 -- generic formal type).
15253
15254 if No (Actual_Subp) then
15255 Set_Alias (New_Subp, Subp);
15256
15257 declare
15258 T : Entity_Id := Find_Dispatching_Type (Subp);
15259 begin
15260 while Etype (T) /= T loop
15261 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15262 Set_Is_Abstract_Subprogram (New_Subp, False);
15263 exit;
15264 end if;
15265
15266 T := Etype (T);
15267 end loop;
15268 end;
15269
15270 -- For instantiations this is not needed since the previous call to
15271 -- Derive_Subprogram leaves the entity well decorated.
15272
15273 else
15274 pragma Assert (Alias (New_Subp) = Actual_Subp);
15275 null;
15276 end if;
15277 end Derive_Interface_Subprogram;
15278
15279 -- Local variables
15280
15281 Alias_Subp : Entity_Id;
15282 Act_List : Elist_Id;
15283 Act_Elmt : Elmt_Id;
15284 Act_Subp : Entity_Id := Empty;
15285 Elmt : Elmt_Id;
15286 Need_Search : Boolean := False;
15287 New_Subp : Entity_Id := Empty;
15288 Parent_Base : Entity_Id;
15289 Subp : Entity_Id;
15290
15291 -- Start of processing for Derive_Subprograms
15292
15293 begin
15294 if Ekind (Parent_Type) = E_Record_Type_With_Private
15295 and then Has_Discriminants (Parent_Type)
15296 and then Present (Full_View (Parent_Type))
15297 then
15298 Parent_Base := Full_View (Parent_Type);
15299 else
15300 Parent_Base := Parent_Type;
15301 end if;
15302
15303 if Present (Generic_Actual) then
15304 Act_List := Collect_Primitive_Operations (Generic_Actual);
15305 Act_Elmt := First_Elmt (Act_List);
15306 else
15307 Act_List := No_Elist;
15308 Act_Elmt := No_Elmt;
15309 end if;
15310
15311 -- Derive primitives inherited from the parent. Note that if the generic
15312 -- actual is present, this is not really a type derivation, it is a
15313 -- completion within an instance.
15314
15315 -- Case 1: Derived_Type does not implement interfaces
15316
15317 if not Is_Tagged_Type (Derived_Type)
15318 or else (not Has_Interfaces (Derived_Type)
15319 and then not (Present (Generic_Actual)
15320 and then Has_Interfaces (Generic_Actual)))
15321 then
15322 Elmt := First_Elmt (Op_List);
15323 while Present (Elmt) loop
15324 Subp := Node (Elmt);
15325
15326 -- Literals are derived earlier in the process of building the
15327 -- derived type, and are skipped here.
15328
15329 if Ekind (Subp) = E_Enumeration_Literal then
15330 null;
15331
15332 -- The actual is a direct descendant and the common primitive
15333 -- operations appear in the same order.
15334
15335 -- If the generic parent type is present, the derived type is an
15336 -- instance of a formal derived type, and within the instance its
15337 -- operations are those of the actual. We derive from the formal
15338 -- type but make the inherited operations aliases of the
15339 -- corresponding operations of the actual.
15340
15341 else
15342 pragma Assert (No (Node (Act_Elmt))
15343 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15344 and then
15345 Type_Conformant
15346 (Subp, Node (Act_Elmt),
15347 Skip_Controlling_Formals => True)));
15348
15349 Derive_Subprogram
15350 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15351
15352 if Present (Act_Elmt) then
15353 Next_Elmt (Act_Elmt);
15354 end if;
15355 end if;
15356
15357 Next_Elmt (Elmt);
15358 end loop;
15359
15360 -- Case 2: Derived_Type implements interfaces
15361
15362 else
15363 -- If the parent type has no predefined primitives we remove
15364 -- predefined primitives from the list of primitives of generic
15365 -- actual to simplify the complexity of this algorithm.
15366
15367 if Present (Generic_Actual) then
15368 declare
15369 Has_Predefined_Primitives : Boolean := False;
15370
15371 begin
15372 -- Check if the parent type has predefined primitives
15373
15374 Elmt := First_Elmt (Op_List);
15375 while Present (Elmt) loop
15376 Subp := Node (Elmt);
15377
15378 if Is_Predefined_Dispatching_Operation (Subp)
15379 and then not Comes_From_Source (Ultimate_Alias (Subp))
15380 then
15381 Has_Predefined_Primitives := True;
15382 exit;
15383 end if;
15384
15385 Next_Elmt (Elmt);
15386 end loop;
15387
15388 -- Remove predefined primitives of Generic_Actual. We must use
15389 -- an auxiliary list because in case of tagged types the value
15390 -- returned by Collect_Primitive_Operations is the value stored
15391 -- in its Primitive_Operations attribute (and we don't want to
15392 -- modify its current contents).
15393
15394 if not Has_Predefined_Primitives then
15395 declare
15396 Aux_List : constant Elist_Id := New_Elmt_List;
15397
15398 begin
15399 Elmt := First_Elmt (Act_List);
15400 while Present (Elmt) loop
15401 Subp := Node (Elmt);
15402
15403 if not Is_Predefined_Dispatching_Operation (Subp)
15404 or else Comes_From_Source (Subp)
15405 then
15406 Append_Elmt (Subp, Aux_List);
15407 end if;
15408
15409 Next_Elmt (Elmt);
15410 end loop;
15411
15412 Act_List := Aux_List;
15413 end;
15414 end if;
15415
15416 Act_Elmt := First_Elmt (Act_List);
15417 Act_Subp := Node (Act_Elmt);
15418 end;
15419 end if;
15420
15421 -- Stage 1: If the generic actual is not present we derive the
15422 -- primitives inherited from the parent type. If the generic parent
15423 -- type is present, the derived type is an instance of a formal
15424 -- derived type, and within the instance its operations are those of
15425 -- the actual. We derive from the formal type but make the inherited
15426 -- operations aliases of the corresponding operations of the actual.
15427
15428 Elmt := First_Elmt (Op_List);
15429 while Present (Elmt) loop
15430 Subp := Node (Elmt);
15431 Alias_Subp := Ultimate_Alias (Subp);
15432
15433 -- Do not derive internal entities of the parent that link
15434 -- interface primitives with their covering primitive. These
15435 -- entities will be added to this type when frozen.
15436
15437 if Present (Interface_Alias (Subp)) then
15438 goto Continue;
15439 end if;
15440
15441 -- If the generic actual is present find the corresponding
15442 -- operation in the generic actual. If the parent type is a
15443 -- direct ancestor of the derived type then, even if it is an
15444 -- interface, the operations are inherited from the primary
15445 -- dispatch table and are in the proper order. If we detect here
15446 -- that primitives are not in the same order we traverse the list
15447 -- of primitive operations of the actual to find the one that
15448 -- implements the interface primitive.
15449
15450 if Need_Search
15451 or else
15452 (Present (Generic_Actual)
15453 and then Present (Act_Subp)
15454 and then not
15455 (Primitive_Names_Match (Subp, Act_Subp)
15456 and then
15457 Type_Conformant (Subp, Act_Subp,
15458 Skip_Controlling_Formals => True)))
15459 then
15460 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15461 Use_Full_View => True));
15462
15463 -- Remember that we need searching for all pending primitives
15464
15465 Need_Search := True;
15466
15467 -- Handle entities associated with interface primitives
15468
15469 if Present (Alias_Subp)
15470 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15471 and then not Is_Predefined_Dispatching_Operation (Subp)
15472 then
15473 -- Search for the primitive in the homonym chain
15474
15475 Act_Subp :=
15476 Find_Primitive_Covering_Interface
15477 (Tagged_Type => Generic_Actual,
15478 Iface_Prim => Alias_Subp);
15479
15480 -- Previous search may not locate primitives covering
15481 -- interfaces defined in generics units or instantiations.
15482 -- (it fails if the covering primitive has formals whose
15483 -- type is also defined in generics or instantiations).
15484 -- In such case we search in the list of primitives of the
15485 -- generic actual for the internal entity that links the
15486 -- interface primitive and the covering primitive.
15487
15488 if No (Act_Subp)
15489 and then Is_Generic_Type (Parent_Type)
15490 then
15491 -- This code has been designed to handle only generic
15492 -- formals that implement interfaces that are defined
15493 -- in a generic unit or instantiation. If this code is
15494 -- needed for other cases we must review it because
15495 -- (given that it relies on Original_Location to locate
15496 -- the primitive of Generic_Actual that covers the
15497 -- interface) it could leave linked through attribute
15498 -- Alias entities of unrelated instantiations).
15499
15500 pragma Assert
15501 (Is_Generic_Unit
15502 (Scope (Find_Dispatching_Type (Alias_Subp)))
15503 or else
15504 Instantiation_Depth
15505 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15506
15507 declare
15508 Iface_Prim_Loc : constant Source_Ptr :=
15509 Original_Location (Sloc (Alias_Subp));
15510
15511 Elmt : Elmt_Id;
15512 Prim : Entity_Id;
15513
15514 begin
15515 Elmt :=
15516 First_Elmt (Primitive_Operations (Generic_Actual));
15517
15518 Search : while Present (Elmt) loop
15519 Prim := Node (Elmt);
15520
15521 if Present (Interface_Alias (Prim))
15522 and then Original_Location
15523 (Sloc (Interface_Alias (Prim))) =
15524 Iface_Prim_Loc
15525 then
15526 Act_Subp := Alias (Prim);
15527 exit Search;
15528 end if;
15529
15530 Next_Elmt (Elmt);
15531 end loop Search;
15532 end;
15533 end if;
15534
15535 pragma Assert (Present (Act_Subp)
15536 or else Is_Abstract_Type (Generic_Actual)
15537 or else Serious_Errors_Detected > 0);
15538
15539 -- Handle predefined primitives plus the rest of user-defined
15540 -- primitives
15541
15542 else
15543 Act_Elmt := First_Elmt (Act_List);
15544 while Present (Act_Elmt) loop
15545 Act_Subp := Node (Act_Elmt);
15546
15547 exit when Primitive_Names_Match (Subp, Act_Subp)
15548 and then Type_Conformant
15549 (Subp, Act_Subp,
15550 Skip_Controlling_Formals => True)
15551 and then No (Interface_Alias (Act_Subp));
15552
15553 Next_Elmt (Act_Elmt);
15554 end loop;
15555
15556 if No (Act_Elmt) then
15557 Act_Subp := Empty;
15558 end if;
15559 end if;
15560 end if;
15561
15562 -- Case 1: If the parent is a limited interface then it has the
15563 -- predefined primitives of synchronized interfaces. However, the
15564 -- actual type may be a non-limited type and hence it does not
15565 -- have such primitives.
15566
15567 if Present (Generic_Actual)
15568 and then not Present (Act_Subp)
15569 and then Is_Limited_Interface (Parent_Base)
15570 and then Is_Predefined_Interface_Primitive (Subp)
15571 then
15572 null;
15573
15574 -- Case 2: Inherit entities associated with interfaces that were
15575 -- not covered by the parent type. We exclude here null interface
15576 -- primitives because they do not need special management.
15577
15578 -- We also exclude interface operations that are renamings. If the
15579 -- subprogram is an explicit renaming of an interface primitive,
15580 -- it is a regular primitive operation, and the presence of its
15581 -- alias is not relevant: it has to be derived like any other
15582 -- primitive.
15583
15584 elsif Present (Alias (Subp))
15585 and then Nkind (Unit_Declaration_Node (Subp)) /=
15586 N_Subprogram_Renaming_Declaration
15587 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15588 and then not
15589 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15590 and then Null_Present (Parent (Alias_Subp)))
15591 then
15592 -- If this is an abstract private type then we transfer the
15593 -- derivation of the interface primitive from the partial view
15594 -- to the full view. This is safe because all the interfaces
15595 -- must be visible in the partial view. Done to avoid adding
15596 -- a new interface derivation to the private part of the
15597 -- enclosing package; otherwise this new derivation would be
15598 -- decorated as hidden when the analysis of the enclosing
15599 -- package completes.
15600
15601 if Is_Abstract_Type (Derived_Type)
15602 and then In_Private_Part (Current_Scope)
15603 and then Has_Private_Declaration (Derived_Type)
15604 then
15605 declare
15606 Partial_View : Entity_Id;
15607 Elmt : Elmt_Id;
15608 Ent : Entity_Id;
15609
15610 begin
15611 Partial_View := First_Entity (Current_Scope);
15612 loop
15613 exit when No (Partial_View)
15614 or else (Has_Private_Declaration (Partial_View)
15615 and then
15616 Full_View (Partial_View) = Derived_Type);
15617
15618 Next_Entity (Partial_View);
15619 end loop;
15620
15621 -- If the partial view was not found then the source code
15622 -- has errors and the derivation is not needed.
15623
15624 if Present (Partial_View) then
15625 Elmt :=
15626 First_Elmt (Primitive_Operations (Partial_View));
15627 while Present (Elmt) loop
15628 Ent := Node (Elmt);
15629
15630 if Present (Alias (Ent))
15631 and then Ultimate_Alias (Ent) = Alias (Subp)
15632 then
15633 Append_Elmt
15634 (Ent, Primitive_Operations (Derived_Type));
15635 exit;
15636 end if;
15637
15638 Next_Elmt (Elmt);
15639 end loop;
15640
15641 -- If the interface primitive was not found in the
15642 -- partial view then this interface primitive was
15643 -- overridden. We add a derivation to activate in
15644 -- Derive_Progenitor_Subprograms the machinery to
15645 -- search for it.
15646
15647 if No (Elmt) then
15648 Derive_Interface_Subprogram
15649 (New_Subp => New_Subp,
15650 Subp => Subp,
15651 Actual_Subp => Act_Subp);
15652 end if;
15653 end if;
15654 end;
15655 else
15656 Derive_Interface_Subprogram
15657 (New_Subp => New_Subp,
15658 Subp => Subp,
15659 Actual_Subp => Act_Subp);
15660 end if;
15661
15662 -- Case 3: Common derivation
15663
15664 else
15665 Derive_Subprogram
15666 (New_Subp => New_Subp,
15667 Parent_Subp => Subp,
15668 Derived_Type => Derived_Type,
15669 Parent_Type => Parent_Base,
15670 Actual_Subp => Act_Subp);
15671 end if;
15672
15673 -- No need to update Act_Elm if we must search for the
15674 -- corresponding operation in the generic actual
15675
15676 if not Need_Search
15677 and then Present (Act_Elmt)
15678 then
15679 Next_Elmt (Act_Elmt);
15680 Act_Subp := Node (Act_Elmt);
15681 end if;
15682
15683 <<Continue>>
15684 Next_Elmt (Elmt);
15685 end loop;
15686
15687 -- Inherit additional operations from progenitors. If the derived
15688 -- type is a generic actual, there are not new primitive operations
15689 -- for the type because it has those of the actual, and therefore
15690 -- nothing needs to be done. The renamings generated above are not
15691 -- primitive operations, and their purpose is simply to make the
15692 -- proper operations visible within an instantiation.
15693
15694 if No (Generic_Actual) then
15695 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15696 end if;
15697 end if;
15698
15699 -- Final check: Direct descendants must have their primitives in the
15700 -- same order. We exclude from this test untagged types and instances
15701 -- of formal derived types. We skip this test if we have already
15702 -- reported serious errors in the sources.
15703
15704 pragma Assert (not Is_Tagged_Type (Derived_Type)
15705 or else Present (Generic_Actual)
15706 or else Serious_Errors_Detected > 0
15707 or else Check_Derived_Type);
15708 end Derive_Subprograms;
15709
15710 --------------------------------
15711 -- Derived_Standard_Character --
15712 --------------------------------
15713
15714 procedure Derived_Standard_Character
15715 (N : Node_Id;
15716 Parent_Type : Entity_Id;
15717 Derived_Type : Entity_Id)
15718 is
15719 Loc : constant Source_Ptr := Sloc (N);
15720 Def : constant Node_Id := Type_Definition (N);
15721 Indic : constant Node_Id := Subtype_Indication (Def);
15722 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15723 Implicit_Base : constant Entity_Id :=
15724 Create_Itype
15725 (E_Enumeration_Type, N, Derived_Type, 'B');
15726
15727 Lo : Node_Id;
15728 Hi : Node_Id;
15729
15730 begin
15731 Discard_Node (Process_Subtype (Indic, N));
15732
15733 Set_Etype (Implicit_Base, Parent_Base);
15734 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15735 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15736
15737 Set_Is_Character_Type (Implicit_Base, True);
15738 Set_Has_Delayed_Freeze (Implicit_Base);
15739
15740 -- The bounds of the implicit base are the bounds of the parent base.
15741 -- Note that their type is the parent base.
15742
15743 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15744 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15745
15746 Set_Scalar_Range (Implicit_Base,
15747 Make_Range (Loc,
15748 Low_Bound => Lo,
15749 High_Bound => Hi));
15750
15751 Conditional_Delay (Derived_Type, Parent_Type);
15752
15753 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15754 Set_Etype (Derived_Type, Implicit_Base);
15755 Set_Size_Info (Derived_Type, Parent_Type);
15756
15757 if Unknown_RM_Size (Derived_Type) then
15758 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15759 end if;
15760
15761 Set_Is_Character_Type (Derived_Type, True);
15762
15763 if Nkind (Indic) /= N_Subtype_Indication then
15764
15765 -- If no explicit constraint, the bounds are those
15766 -- of the parent type.
15767
15768 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15769 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15770 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15771 end if;
15772
15773 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15774
15775 -- Because the implicit base is used in the conversion of the bounds, we
15776 -- have to freeze it now. This is similar to what is done for numeric
15777 -- types, and it equally suspicious, but otherwise a non-static bound
15778 -- will have a reference to an unfrozen type, which is rejected by Gigi
15779 -- (???). This requires specific care for definition of stream
15780 -- attributes. For details, see comments at the end of
15781 -- Build_Derived_Numeric_Type.
15782
15783 Freeze_Before (N, Implicit_Base);
15784 end Derived_Standard_Character;
15785
15786 ------------------------------
15787 -- Derived_Type_Declaration --
15788 ------------------------------
15789
15790 procedure Derived_Type_Declaration
15791 (T : Entity_Id;
15792 N : Node_Id;
15793 Is_Completion : Boolean)
15794 is
15795 Parent_Type : Entity_Id;
15796
15797 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15798 -- Check whether the parent type is a generic formal, or derives
15799 -- directly or indirectly from one.
15800
15801 ------------------------
15802 -- Comes_From_Generic --
15803 ------------------------
15804
15805 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15806 begin
15807 if Is_Generic_Type (Typ) then
15808 return True;
15809
15810 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15811 return True;
15812
15813 elsif Is_Private_Type (Typ)
15814 and then Present (Full_View (Typ))
15815 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15816 then
15817 return True;
15818
15819 elsif Is_Generic_Actual_Type (Typ) then
15820 return True;
15821
15822 else
15823 return False;
15824 end if;
15825 end Comes_From_Generic;
15826
15827 -- Local variables
15828
15829 Def : constant Node_Id := Type_Definition (N);
15830 Iface_Def : Node_Id;
15831 Indic : constant Node_Id := Subtype_Indication (Def);
15832 Extension : constant Node_Id := Record_Extension_Part (Def);
15833 Parent_Node : Node_Id;
15834 Taggd : Boolean;
15835
15836 -- Start of processing for Derived_Type_Declaration
15837
15838 begin
15839 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15840
15841 -- Ada 2005 (AI-251): In case of interface derivation check that the
15842 -- parent is also an interface.
15843
15844 if Interface_Present (Def) then
15845 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15846
15847 if not Is_Interface (Parent_Type) then
15848 Diagnose_Interface (Indic, Parent_Type);
15849
15850 else
15851 Parent_Node := Parent (Base_Type (Parent_Type));
15852 Iface_Def := Type_Definition (Parent_Node);
15853
15854 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15855 -- other limited interfaces.
15856
15857 if Limited_Present (Def) then
15858 if Limited_Present (Iface_Def) then
15859 null;
15860
15861 elsif Protected_Present (Iface_Def) then
15862 Error_Msg_NE
15863 ("descendant of & must be declared as a protected "
15864 & "interface", N, Parent_Type);
15865
15866 elsif Synchronized_Present (Iface_Def) then
15867 Error_Msg_NE
15868 ("descendant of & must be declared as a synchronized "
15869 & "interface", N, Parent_Type);
15870
15871 elsif Task_Present (Iface_Def) then
15872 Error_Msg_NE
15873 ("descendant of & must be declared as a task interface",
15874 N, Parent_Type);
15875
15876 else
15877 Error_Msg_N
15878 ("(Ada 2005) limited interface cannot inherit from "
15879 & "non-limited interface", Indic);
15880 end if;
15881
15882 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15883 -- from non-limited or limited interfaces.
15884
15885 elsif not Protected_Present (Def)
15886 and then not Synchronized_Present (Def)
15887 and then not Task_Present (Def)
15888 then
15889 if Limited_Present (Iface_Def) then
15890 null;
15891
15892 elsif Protected_Present (Iface_Def) then
15893 Error_Msg_NE
15894 ("descendant of & must be declared as a protected "
15895 & "interface", N, Parent_Type);
15896
15897 elsif Synchronized_Present (Iface_Def) then
15898 Error_Msg_NE
15899 ("descendant of & must be declared as a synchronized "
15900 & "interface", N, Parent_Type);
15901
15902 elsif Task_Present (Iface_Def) then
15903 Error_Msg_NE
15904 ("descendant of & must be declared as a task interface",
15905 N, Parent_Type);
15906 else
15907 null;
15908 end if;
15909 end if;
15910 end if;
15911 end if;
15912
15913 if Is_Tagged_Type (Parent_Type)
15914 and then Is_Concurrent_Type (Parent_Type)
15915 and then not Is_Interface (Parent_Type)
15916 then
15917 Error_Msg_N
15918 ("parent type of a record extension cannot be a synchronized "
15919 & "tagged type (RM 3.9.1 (3/1))", N);
15920 Set_Etype (T, Any_Type);
15921 return;
15922 end if;
15923
15924 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15925 -- interfaces
15926
15927 if Is_Tagged_Type (Parent_Type)
15928 and then Is_Non_Empty_List (Interface_List (Def))
15929 then
15930 declare
15931 Intf : Node_Id;
15932 T : Entity_Id;
15933
15934 begin
15935 Intf := First (Interface_List (Def));
15936 while Present (Intf) loop
15937 T := Find_Type_Of_Subtype_Indic (Intf);
15938
15939 if not Is_Interface (T) then
15940 Diagnose_Interface (Intf, T);
15941
15942 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15943 -- a limited type from having a nonlimited progenitor.
15944
15945 elsif (Limited_Present (Def)
15946 or else (not Is_Interface (Parent_Type)
15947 and then Is_Limited_Type (Parent_Type)))
15948 and then not Is_Limited_Interface (T)
15949 then
15950 Error_Msg_NE
15951 ("progenitor interface& of limited type must be limited",
15952 N, T);
15953 end if;
15954
15955 Next (Intf);
15956 end loop;
15957 end;
15958 end if;
15959
15960 if Parent_Type = Any_Type
15961 or else Etype (Parent_Type) = Any_Type
15962 or else (Is_Class_Wide_Type (Parent_Type)
15963 and then Etype (Parent_Type) = T)
15964 then
15965 -- If Parent_Type is undefined or illegal, make new type into a
15966 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15967 -- errors. If this is a self-definition, emit error now.
15968
15969 if T = Parent_Type or else T = Etype (Parent_Type) then
15970 Error_Msg_N ("type cannot be used in its own definition", Indic);
15971 end if;
15972
15973 Set_Ekind (T, Ekind (Parent_Type));
15974 Set_Etype (T, Any_Type);
15975 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15976
15977 if Is_Tagged_Type (T)
15978 and then Is_Record_Type (T)
15979 then
15980 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15981 end if;
15982
15983 return;
15984 end if;
15985
15986 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15987 -- an interface is special because the list of interfaces in the full
15988 -- view can be given in any order. For example:
15989
15990 -- type A is interface;
15991 -- type B is interface and A;
15992 -- type D is new B with private;
15993 -- private
15994 -- type D is new A and B with null record; -- 1 --
15995
15996 -- In this case we perform the following transformation of -1-:
15997
15998 -- type D is new B and A with null record;
15999
16000 -- If the parent of the full-view covers the parent of the partial-view
16001 -- we have two possible cases:
16002
16003 -- 1) They have the same parent
16004 -- 2) The parent of the full-view implements some further interfaces
16005
16006 -- In both cases we do not need to perform the transformation. In the
16007 -- first case the source program is correct and the transformation is
16008 -- not needed; in the second case the source program does not fulfill
16009 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16010 -- later.
16011
16012 -- This transformation not only simplifies the rest of the analysis of
16013 -- this type declaration but also simplifies the correct generation of
16014 -- the object layout to the expander.
16015
16016 if In_Private_Part (Current_Scope)
16017 and then Is_Interface (Parent_Type)
16018 then
16019 declare
16020 Iface : Node_Id;
16021 Partial_View : Entity_Id;
16022 Partial_View_Parent : Entity_Id;
16023 New_Iface : Node_Id;
16024
16025 begin
16026 -- Look for the associated private type declaration
16027
16028 Partial_View := First_Entity (Current_Scope);
16029 loop
16030 exit when No (Partial_View)
16031 or else (Has_Private_Declaration (Partial_View)
16032 and then Full_View (Partial_View) = T);
16033
16034 Next_Entity (Partial_View);
16035 end loop;
16036
16037 -- If the partial view was not found then the source code has
16038 -- errors and the transformation is not needed.
16039
16040 if Present (Partial_View) then
16041 Partial_View_Parent := Etype (Partial_View);
16042
16043 -- If the parent of the full-view covers the parent of the
16044 -- partial-view we have nothing else to do.
16045
16046 if Interface_Present_In_Ancestor
16047 (Parent_Type, Partial_View_Parent)
16048 then
16049 null;
16050
16051 -- Traverse the list of interfaces of the full-view to look
16052 -- for the parent of the partial-view and perform the tree
16053 -- transformation.
16054
16055 else
16056 Iface := First (Interface_List (Def));
16057 while Present (Iface) loop
16058 if Etype (Iface) = Etype (Partial_View) then
16059 Rewrite (Subtype_Indication (Def),
16060 New_Copy (Subtype_Indication
16061 (Parent (Partial_View))));
16062
16063 New_Iface :=
16064 Make_Identifier (Sloc (N), Chars (Parent_Type));
16065 Append (New_Iface, Interface_List (Def));
16066
16067 -- Analyze the transformed code
16068
16069 Derived_Type_Declaration (T, N, Is_Completion);
16070 return;
16071 end if;
16072
16073 Next (Iface);
16074 end loop;
16075 end if;
16076 end if;
16077 end;
16078 end if;
16079
16080 -- Only composite types other than array types are allowed to have
16081 -- discriminants.
16082
16083 if Present (Discriminant_Specifications (N)) then
16084 if (Is_Elementary_Type (Parent_Type)
16085 or else
16086 Is_Array_Type (Parent_Type))
16087 and then not Error_Posted (N)
16088 then
16089 Error_Msg_N
16090 ("elementary or array type cannot have discriminants",
16091 Defining_Identifier (First (Discriminant_Specifications (N))));
16092 Set_Has_Discriminants (T, False);
16093
16094 -- The type is allowed to have discriminants
16095
16096 else
16097 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16098 end if;
16099 end if;
16100
16101 -- In Ada 83, a derived type defined in a package specification cannot
16102 -- be used for further derivation until the end of its visible part.
16103 -- Note that derivation in the private part of the package is allowed.
16104
16105 if Ada_Version = Ada_83
16106 and then Is_Derived_Type (Parent_Type)
16107 and then In_Visible_Part (Scope (Parent_Type))
16108 then
16109 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16110 Error_Msg_N
16111 ("(Ada 83): premature use of type for derivation", Indic);
16112 end if;
16113 end if;
16114
16115 -- Check for early use of incomplete or private type
16116
16117 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16118 Error_Msg_N ("premature derivation of incomplete type", Indic);
16119 return;
16120
16121 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16122 and then not Comes_From_Generic (Parent_Type))
16123 or else Has_Private_Component (Parent_Type)
16124 then
16125 -- The ancestor type of a formal type can be incomplete, in which
16126 -- case only the operations of the partial view are available in the
16127 -- generic. Subsequent checks may be required when the full view is
16128 -- analyzed to verify that a derivation from a tagged type has an
16129 -- extension.
16130
16131 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16132 null;
16133
16134 elsif No (Underlying_Type (Parent_Type))
16135 or else Has_Private_Component (Parent_Type)
16136 then
16137 Error_Msg_N
16138 ("premature derivation of derived or private type", Indic);
16139
16140 -- Flag the type itself as being in error, this prevents some
16141 -- nasty problems with subsequent uses of the malformed type.
16142
16143 Set_Error_Posted (T);
16144
16145 -- Check that within the immediate scope of an untagged partial
16146 -- view it's illegal to derive from the partial view if the
16147 -- full view is tagged. (7.3(7))
16148
16149 -- We verify that the Parent_Type is a partial view by checking
16150 -- that it is not a Full_Type_Declaration (i.e. a private type or
16151 -- private extension declaration), to distinguish a partial view
16152 -- from a derivation from a private type which also appears as
16153 -- E_Private_Type. If the parent base type is not declared in an
16154 -- enclosing scope there is no need to check.
16155
16156 elsif Present (Full_View (Parent_Type))
16157 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16158 and then not Is_Tagged_Type (Parent_Type)
16159 and then Is_Tagged_Type (Full_View (Parent_Type))
16160 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16161 then
16162 Error_Msg_N
16163 ("premature derivation from type with tagged full view",
16164 Indic);
16165 end if;
16166 end if;
16167
16168 -- Check that form of derivation is appropriate
16169
16170 Taggd := Is_Tagged_Type (Parent_Type);
16171
16172 -- Set the parent type to the class-wide type's specific type in this
16173 -- case to prevent cascading errors
16174
16175 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16176 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16177 Set_Etype (T, Etype (Parent_Type));
16178 return;
16179 end if;
16180
16181 if Present (Extension) and then not Taggd then
16182 Error_Msg_N
16183 ("type derived from untagged type cannot have extension", Indic);
16184
16185 elsif No (Extension) and then Taggd then
16186
16187 -- If this declaration is within a private part (or body) of a
16188 -- generic instantiation then the derivation is allowed (the parent
16189 -- type can only appear tagged in this case if it's a generic actual
16190 -- type, since it would otherwise have been rejected in the analysis
16191 -- of the generic template).
16192
16193 if not Is_Generic_Actual_Type (Parent_Type)
16194 or else In_Visible_Part (Scope (Parent_Type))
16195 then
16196 if Is_Class_Wide_Type (Parent_Type) then
16197 Error_Msg_N
16198 ("parent type must not be a class-wide type", Indic);
16199
16200 -- Use specific type to prevent cascaded errors.
16201
16202 Parent_Type := Etype (Parent_Type);
16203
16204 else
16205 Error_Msg_N
16206 ("type derived from tagged type must have extension", Indic);
16207 end if;
16208 end if;
16209 end if;
16210
16211 -- AI-443: Synchronized formal derived types require a private
16212 -- extension. There is no point in checking the ancestor type or
16213 -- the progenitors since the construct is wrong to begin with.
16214
16215 if Ada_Version >= Ada_2005
16216 and then Is_Generic_Type (T)
16217 and then Present (Original_Node (N))
16218 then
16219 declare
16220 Decl : constant Node_Id := Original_Node (N);
16221
16222 begin
16223 if Nkind (Decl) = N_Formal_Type_Declaration
16224 and then Nkind (Formal_Type_Definition (Decl)) =
16225 N_Formal_Derived_Type_Definition
16226 and then Synchronized_Present (Formal_Type_Definition (Decl))
16227 and then No (Extension)
16228
16229 -- Avoid emitting a duplicate error message
16230
16231 and then not Error_Posted (Indic)
16232 then
16233 Error_Msg_N
16234 ("synchronized derived type must have extension", N);
16235 end if;
16236 end;
16237 end if;
16238
16239 if Null_Exclusion_Present (Def)
16240 and then not Is_Access_Type (Parent_Type)
16241 then
16242 Error_Msg_N ("null exclusion can only apply to an access type", N);
16243 end if;
16244
16245 -- Avoid deriving parent primitives of underlying record views
16246
16247 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16248 Derive_Subps => not Is_Underlying_Record_View (T));
16249
16250 -- AI-419: The parent type of an explicitly limited derived type must
16251 -- be a limited type or a limited interface.
16252
16253 if Limited_Present (Def) then
16254 Set_Is_Limited_Record (T);
16255
16256 if Is_Interface (T) then
16257 Set_Is_Limited_Interface (T);
16258 end if;
16259
16260 if not Is_Limited_Type (Parent_Type)
16261 and then
16262 (not Is_Interface (Parent_Type)
16263 or else not Is_Limited_Interface (Parent_Type))
16264 then
16265 -- AI05-0096: a derivation in the private part of an instance is
16266 -- legal if the generic formal is untagged limited, and the actual
16267 -- is non-limited.
16268
16269 if Is_Generic_Actual_Type (Parent_Type)
16270 and then In_Private_Part (Current_Scope)
16271 and then
16272 not Is_Tagged_Type
16273 (Generic_Parent_Type (Parent (Parent_Type)))
16274 then
16275 null;
16276
16277 else
16278 Error_Msg_NE
16279 ("parent type& of limited type must be limited",
16280 N, Parent_Type);
16281 end if;
16282 end if;
16283 end if;
16284
16285 -- In SPARK, there are no derived type definitions other than type
16286 -- extensions of tagged record types.
16287
16288 if No (Extension) then
16289 Check_SPARK_05_Restriction
16290 ("derived type is not allowed", Original_Node (N));
16291 end if;
16292 end Derived_Type_Declaration;
16293
16294 ------------------------
16295 -- Diagnose_Interface --
16296 ------------------------
16297
16298 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16299 begin
16300 if not Is_Interface (E) and then E /= Any_Type then
16301 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16302 end if;
16303 end Diagnose_Interface;
16304
16305 ----------------------------------
16306 -- Enumeration_Type_Declaration --
16307 ----------------------------------
16308
16309 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16310 Ev : Uint;
16311 L : Node_Id;
16312 R_Node : Node_Id;
16313 B_Node : Node_Id;
16314
16315 begin
16316 -- Create identifier node representing lower bound
16317
16318 B_Node := New_Node (N_Identifier, Sloc (Def));
16319 L := First (Literals (Def));
16320 Set_Chars (B_Node, Chars (L));
16321 Set_Entity (B_Node, L);
16322 Set_Etype (B_Node, T);
16323 Set_Is_Static_Expression (B_Node, True);
16324
16325 R_Node := New_Node (N_Range, Sloc (Def));
16326 Set_Low_Bound (R_Node, B_Node);
16327
16328 Set_Ekind (T, E_Enumeration_Type);
16329 Set_First_Literal (T, L);
16330 Set_Etype (T, T);
16331 Set_Is_Constrained (T);
16332
16333 Ev := Uint_0;
16334
16335 -- Loop through literals of enumeration type setting pos and rep values
16336 -- except that if the Ekind is already set, then it means the literal
16337 -- was already constructed (case of a derived type declaration and we
16338 -- should not disturb the Pos and Rep values.
16339
16340 while Present (L) loop
16341 if Ekind (L) /= E_Enumeration_Literal then
16342 Set_Ekind (L, E_Enumeration_Literal);
16343 Set_Enumeration_Pos (L, Ev);
16344 Set_Enumeration_Rep (L, Ev);
16345 Set_Is_Known_Valid (L, True);
16346 end if;
16347
16348 Set_Etype (L, T);
16349 New_Overloaded_Entity (L);
16350 Generate_Definition (L);
16351 Set_Convention (L, Convention_Intrinsic);
16352
16353 -- Case of character literal
16354
16355 if Nkind (L) = N_Defining_Character_Literal then
16356 Set_Is_Character_Type (T, True);
16357
16358 -- Check violation of No_Wide_Characters
16359
16360 if Restriction_Check_Required (No_Wide_Characters) then
16361 Get_Name_String (Chars (L));
16362
16363 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16364 Check_Restriction (No_Wide_Characters, L);
16365 end if;
16366 end if;
16367 end if;
16368
16369 Ev := Ev + 1;
16370 Next (L);
16371 end loop;
16372
16373 -- Now create a node representing upper bound
16374
16375 B_Node := New_Node (N_Identifier, Sloc (Def));
16376 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16377 Set_Entity (B_Node, Last (Literals (Def)));
16378 Set_Etype (B_Node, T);
16379 Set_Is_Static_Expression (B_Node, True);
16380
16381 Set_High_Bound (R_Node, B_Node);
16382
16383 -- Initialize various fields of the type. Some of this information
16384 -- may be overwritten later through rep.clauses.
16385
16386 Set_Scalar_Range (T, R_Node);
16387 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16388 Set_Enum_Esize (T);
16389 Set_Enum_Pos_To_Rep (T, Empty);
16390
16391 -- Set Discard_Names if configuration pragma set, or if there is
16392 -- a parameterless pragma in the current declarative region
16393
16394 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16395 Set_Discard_Names (T);
16396 end if;
16397
16398 -- Process end label if there is one
16399
16400 if Present (Def) then
16401 Process_End_Label (Def, 'e', T);
16402 end if;
16403 end Enumeration_Type_Declaration;
16404
16405 ---------------------------------
16406 -- Expand_To_Stored_Constraint --
16407 ---------------------------------
16408
16409 function Expand_To_Stored_Constraint
16410 (Typ : Entity_Id;
16411 Constraint : Elist_Id) return Elist_Id
16412 is
16413 Explicitly_Discriminated_Type : Entity_Id;
16414 Expansion : Elist_Id;
16415 Discriminant : Entity_Id;
16416
16417 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16418 -- Find the nearest type that actually specifies discriminants
16419
16420 ---------------------------------
16421 -- Type_With_Explicit_Discrims --
16422 ---------------------------------
16423
16424 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16425 Typ : constant E := Base_Type (Id);
16426
16427 begin
16428 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16429 if Present (Full_View (Typ)) then
16430 return Type_With_Explicit_Discrims (Full_View (Typ));
16431 end if;
16432
16433 else
16434 if Has_Discriminants (Typ) then
16435 return Typ;
16436 end if;
16437 end if;
16438
16439 if Etype (Typ) = Typ then
16440 return Empty;
16441 elsif Has_Discriminants (Typ) then
16442 return Typ;
16443 else
16444 return Type_With_Explicit_Discrims (Etype (Typ));
16445 end if;
16446
16447 end Type_With_Explicit_Discrims;
16448
16449 -- Start of processing for Expand_To_Stored_Constraint
16450
16451 begin
16452 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16453 return No_Elist;
16454 end if;
16455
16456 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16457
16458 if No (Explicitly_Discriminated_Type) then
16459 return No_Elist;
16460 end if;
16461
16462 Expansion := New_Elmt_List;
16463
16464 Discriminant :=
16465 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16466 while Present (Discriminant) loop
16467 Append_Elmt
16468 (Get_Discriminant_Value
16469 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16470 To => Expansion);
16471 Next_Stored_Discriminant (Discriminant);
16472 end loop;
16473
16474 return Expansion;
16475 end Expand_To_Stored_Constraint;
16476
16477 ---------------------------
16478 -- Find_Hidden_Interface --
16479 ---------------------------
16480
16481 function Find_Hidden_Interface
16482 (Src : Elist_Id;
16483 Dest : Elist_Id) return Entity_Id
16484 is
16485 Iface : Entity_Id;
16486 Iface_Elmt : Elmt_Id;
16487
16488 begin
16489 if Present (Src) and then Present (Dest) then
16490 Iface_Elmt := First_Elmt (Src);
16491 while Present (Iface_Elmt) loop
16492 Iface := Node (Iface_Elmt);
16493
16494 if Is_Interface (Iface)
16495 and then not Contain_Interface (Iface, Dest)
16496 then
16497 return Iface;
16498 end if;
16499
16500 Next_Elmt (Iface_Elmt);
16501 end loop;
16502 end if;
16503
16504 return Empty;
16505 end Find_Hidden_Interface;
16506
16507 --------------------
16508 -- Find_Type_Name --
16509 --------------------
16510
16511 function Find_Type_Name (N : Node_Id) return Entity_Id is
16512 Id : constant Entity_Id := Defining_Identifier (N);
16513 New_Id : Entity_Id;
16514 Prev : Entity_Id;
16515 Prev_Par : Node_Id;
16516
16517 procedure Check_Duplicate_Aspects;
16518 -- Check that aspects specified in a completion have not been specified
16519 -- already in the partial view.
16520
16521 procedure Tag_Mismatch;
16522 -- Diagnose a tagged partial view whose full view is untagged. We post
16523 -- the message on the full view, with a reference to the previous
16524 -- partial view. The partial view can be private or incomplete, and
16525 -- these are handled in a different manner, so we determine the position
16526 -- of the error message from the respective slocs of both.
16527
16528 -----------------------------
16529 -- Check_Duplicate_Aspects --
16530 -----------------------------
16531
16532 procedure Check_Duplicate_Aspects is
16533 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16534 -- Return the corresponding aspect of the partial view which matches
16535 -- the aspect id of Asp. Return Empty is no such aspect exists.
16536
16537 -----------------------------
16538 -- Get_Partial_View_Aspect --
16539 -----------------------------
16540
16541 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16542 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16543 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16544 Prev_Asp : Node_Id;
16545
16546 begin
16547 if Present (Prev_Asps) then
16548 Prev_Asp := First (Prev_Asps);
16549 while Present (Prev_Asp) loop
16550 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16551 return Prev_Asp;
16552 end if;
16553
16554 Next (Prev_Asp);
16555 end loop;
16556 end if;
16557
16558 return Empty;
16559 end Get_Partial_View_Aspect;
16560
16561 -- Local variables
16562
16563 Full_Asps : constant List_Id := Aspect_Specifications (N);
16564 Full_Asp : Node_Id;
16565 Part_Asp : Node_Id;
16566
16567 -- Start of processing for Check_Duplicate_Aspects
16568
16569 begin
16570 if Present (Full_Asps) then
16571 Full_Asp := First (Full_Asps);
16572 while Present (Full_Asp) loop
16573 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16574
16575 -- An aspect and its class-wide counterpart are two distinct
16576 -- aspects and may apply to both views of an entity.
16577
16578 if Present (Part_Asp)
16579 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16580 then
16581 Error_Msg_N
16582 ("aspect already specified in private declaration",
16583 Full_Asp);
16584
16585 Remove (Full_Asp);
16586 return;
16587 end if;
16588
16589 if Has_Discriminants (Prev)
16590 and then not Has_Unknown_Discriminants (Prev)
16591 and then Get_Aspect_Id (Full_Asp) =
16592 Aspect_Implicit_Dereference
16593 then
16594 Error_Msg_N
16595 ("cannot specify aspect if partial view has known "
16596 & "discriminants", Full_Asp);
16597 end if;
16598
16599 Next (Full_Asp);
16600 end loop;
16601 end if;
16602 end Check_Duplicate_Aspects;
16603
16604 ------------------
16605 -- Tag_Mismatch --
16606 ------------------
16607
16608 procedure Tag_Mismatch is
16609 begin
16610 if Sloc (Prev) < Sloc (Id) then
16611 if Ada_Version >= Ada_2012
16612 and then Nkind (N) = N_Private_Type_Declaration
16613 then
16614 Error_Msg_NE
16615 ("declaration of private } must be a tagged type ", Id, Prev);
16616 else
16617 Error_Msg_NE
16618 ("full declaration of } must be a tagged type ", Id, Prev);
16619 end if;
16620
16621 else
16622 if Ada_Version >= Ada_2012
16623 and then Nkind (N) = N_Private_Type_Declaration
16624 then
16625 Error_Msg_NE
16626 ("declaration of private } must be a tagged type ", Prev, Id);
16627 else
16628 Error_Msg_NE
16629 ("full declaration of } must be a tagged type ", Prev, Id);
16630 end if;
16631 end if;
16632 end Tag_Mismatch;
16633
16634 -- Start of processing for Find_Type_Name
16635
16636 begin
16637 -- Find incomplete declaration, if one was given
16638
16639 Prev := Current_Entity_In_Scope (Id);
16640
16641 -- New type declaration
16642
16643 if No (Prev) then
16644 Enter_Name (Id);
16645 return Id;
16646
16647 -- Previous declaration exists
16648
16649 else
16650 Prev_Par := Parent (Prev);
16651
16652 -- Error if not incomplete/private case except if previous
16653 -- declaration is implicit, etc. Enter_Name will emit error if
16654 -- appropriate.
16655
16656 if not Is_Incomplete_Or_Private_Type (Prev) then
16657 Enter_Name (Id);
16658 New_Id := Id;
16659
16660 -- Check invalid completion of private or incomplete type
16661
16662 elsif not Nkind_In (N, N_Full_Type_Declaration,
16663 N_Task_Type_Declaration,
16664 N_Protected_Type_Declaration)
16665 and then
16666 (Ada_Version < Ada_2012
16667 or else not Is_Incomplete_Type (Prev)
16668 or else not Nkind_In (N, N_Private_Type_Declaration,
16669 N_Private_Extension_Declaration))
16670 then
16671 -- Completion must be a full type declarations (RM 7.3(4))
16672
16673 Error_Msg_Sloc := Sloc (Prev);
16674 Error_Msg_NE ("invalid completion of }", Id, Prev);
16675
16676 -- Set scope of Id to avoid cascaded errors. Entity is never
16677 -- examined again, except when saving globals in generics.
16678
16679 Set_Scope (Id, Current_Scope);
16680 New_Id := Id;
16681
16682 -- If this is a repeated incomplete declaration, no further
16683 -- checks are possible.
16684
16685 if Nkind (N) = N_Incomplete_Type_Declaration then
16686 return Prev;
16687 end if;
16688
16689 -- Case of full declaration of incomplete type
16690
16691 elsif Ekind (Prev) = E_Incomplete_Type
16692 and then (Ada_Version < Ada_2012
16693 or else No (Full_View (Prev))
16694 or else not Is_Private_Type (Full_View (Prev)))
16695 then
16696 -- Indicate that the incomplete declaration has a matching full
16697 -- declaration. The defining occurrence of the incomplete
16698 -- declaration remains the visible one, and the procedure
16699 -- Get_Full_View dereferences it whenever the type is used.
16700
16701 if Present (Full_View (Prev)) then
16702 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16703 end if;
16704
16705 Set_Full_View (Prev, Id);
16706 Append_Entity (Id, Current_Scope);
16707 Set_Is_Public (Id, Is_Public (Prev));
16708 Set_Is_Internal (Id);
16709 New_Id := Prev;
16710
16711 -- If the incomplete view is tagged, a class_wide type has been
16712 -- created already. Use it for the private type as well, in order
16713 -- to prevent multiple incompatible class-wide types that may be
16714 -- created for self-referential anonymous access components.
16715
16716 if Is_Tagged_Type (Prev)
16717 and then Present (Class_Wide_Type (Prev))
16718 then
16719 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16720 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16721
16722 -- Type of the class-wide type is the current Id. Previously
16723 -- this was not done for private declarations because of order-
16724 -- of-elaboration issues in the back end, but gigi now handles
16725 -- this properly.
16726
16727 Set_Etype (Class_Wide_Type (Id), Id);
16728 end if;
16729
16730 -- Case of full declaration of private type
16731
16732 else
16733 -- If the private type was a completion of an incomplete type then
16734 -- update Prev to reference the private type
16735
16736 if Ada_Version >= Ada_2012
16737 and then Ekind (Prev) = E_Incomplete_Type
16738 and then Present (Full_View (Prev))
16739 and then Is_Private_Type (Full_View (Prev))
16740 then
16741 Prev := Full_View (Prev);
16742 Prev_Par := Parent (Prev);
16743 end if;
16744
16745 if Nkind (N) = N_Full_Type_Declaration
16746 and then Nkind_In
16747 (Type_Definition (N), N_Record_Definition,
16748 N_Derived_Type_Definition)
16749 and then Interface_Present (Type_Definition (N))
16750 then
16751 Error_Msg_N
16752 ("completion of private type cannot be an interface", N);
16753 end if;
16754
16755 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16756 if Etype (Prev) /= Prev then
16757
16758 -- Prev is a private subtype or a derived type, and needs
16759 -- no completion.
16760
16761 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16762 New_Id := Id;
16763
16764 elsif Ekind (Prev) = E_Private_Type
16765 and then Nkind_In (N, N_Task_Type_Declaration,
16766 N_Protected_Type_Declaration)
16767 then
16768 Error_Msg_N
16769 ("completion of nonlimited type cannot be limited", N);
16770
16771 elsif Ekind (Prev) = E_Record_Type_With_Private
16772 and then Nkind_In (N, N_Task_Type_Declaration,
16773 N_Protected_Type_Declaration)
16774 then
16775 if not Is_Limited_Record (Prev) then
16776 Error_Msg_N
16777 ("completion of nonlimited type cannot be limited", N);
16778
16779 elsif No (Interface_List (N)) then
16780 Error_Msg_N
16781 ("completion of tagged private type must be tagged",
16782 N);
16783 end if;
16784 end if;
16785
16786 -- Ada 2005 (AI-251): Private extension declaration of a task
16787 -- type or a protected type. This case arises when covering
16788 -- interface types.
16789
16790 elsif Nkind_In (N, N_Task_Type_Declaration,
16791 N_Protected_Type_Declaration)
16792 then
16793 null;
16794
16795 elsif Nkind (N) /= N_Full_Type_Declaration
16796 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16797 then
16798 Error_Msg_N
16799 ("full view of private extension must be an extension", N);
16800
16801 elsif not (Abstract_Present (Parent (Prev)))
16802 and then Abstract_Present (Type_Definition (N))
16803 then
16804 Error_Msg_N
16805 ("full view of non-abstract extension cannot be abstract", N);
16806 end if;
16807
16808 if not In_Private_Part (Current_Scope) then
16809 Error_Msg_N
16810 ("declaration of full view must appear in private part", N);
16811 end if;
16812
16813 if Ada_Version >= Ada_2012 then
16814 Check_Duplicate_Aspects;
16815 end if;
16816
16817 Copy_And_Swap (Prev, Id);
16818 Set_Has_Private_Declaration (Prev);
16819 Set_Has_Private_Declaration (Id);
16820
16821 -- AI12-0133: Indicate whether we have a partial view with
16822 -- unknown discriminants, in which case initialization of objects
16823 -- of the type do not receive an invariant check.
16824
16825 Set_Partial_View_Has_Unknown_Discr
16826 (Prev, Has_Unknown_Discriminants (Id));
16827
16828 -- Preserve aspect and iterator flags that may have been set on
16829 -- the partial view.
16830
16831 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16832 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16833
16834 -- If no error, propagate freeze_node from private to full view.
16835 -- It may have been generated for an early operational item.
16836
16837 if Present (Freeze_Node (Id))
16838 and then Serious_Errors_Detected = 0
16839 and then No (Full_View (Id))
16840 then
16841 Set_Freeze_Node (Prev, Freeze_Node (Id));
16842 Set_Freeze_Node (Id, Empty);
16843 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16844 end if;
16845
16846 Set_Full_View (Id, Prev);
16847 New_Id := Prev;
16848 end if;
16849
16850 -- Verify that full declaration conforms to partial one
16851
16852 if Is_Incomplete_Or_Private_Type (Prev)
16853 and then Present (Discriminant_Specifications (Prev_Par))
16854 then
16855 if Present (Discriminant_Specifications (N)) then
16856 if Ekind (Prev) = E_Incomplete_Type then
16857 Check_Discriminant_Conformance (N, Prev, Prev);
16858 else
16859 Check_Discriminant_Conformance (N, Prev, Id);
16860 end if;
16861
16862 else
16863 Error_Msg_N
16864 ("missing discriminants in full type declaration", N);
16865
16866 -- To avoid cascaded errors on subsequent use, share the
16867 -- discriminants of the partial view.
16868
16869 Set_Discriminant_Specifications (N,
16870 Discriminant_Specifications (Prev_Par));
16871 end if;
16872 end if;
16873
16874 -- A prior untagged partial view can have an associated class-wide
16875 -- type due to use of the class attribute, and in this case the full
16876 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16877 -- of incomplete tagged declarations, but we check for it.
16878
16879 if Is_Type (Prev)
16880 and then (Is_Tagged_Type (Prev)
16881 or else Present (Class_Wide_Type (Prev)))
16882 then
16883 -- Ada 2012 (AI05-0162): A private type may be the completion of
16884 -- an incomplete type.
16885
16886 if Ada_Version >= Ada_2012
16887 and then Is_Incomplete_Type (Prev)
16888 and then Nkind_In (N, N_Private_Type_Declaration,
16889 N_Private_Extension_Declaration)
16890 then
16891 -- No need to check private extensions since they are tagged
16892
16893 if Nkind (N) = N_Private_Type_Declaration
16894 and then not Tagged_Present (N)
16895 then
16896 Tag_Mismatch;
16897 end if;
16898
16899 -- The full declaration is either a tagged type (including
16900 -- a synchronized type that implements interfaces) or a
16901 -- type extension, otherwise this is an error.
16902
16903 elsif Nkind_In (N, N_Task_Type_Declaration,
16904 N_Protected_Type_Declaration)
16905 then
16906 if No (Interface_List (N)) and then not Error_Posted (N) then
16907 Tag_Mismatch;
16908 end if;
16909
16910 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16911
16912 -- Indicate that the previous declaration (tagged incomplete
16913 -- or private declaration) requires the same on the full one.
16914
16915 if not Tagged_Present (Type_Definition (N)) then
16916 Tag_Mismatch;
16917 Set_Is_Tagged_Type (Id);
16918 end if;
16919
16920 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16921 if No (Record_Extension_Part (Type_Definition (N))) then
16922 Error_Msg_NE
16923 ("full declaration of } must be a record extension",
16924 Prev, Id);
16925
16926 -- Set some attributes to produce a usable full view
16927
16928 Set_Is_Tagged_Type (Id);
16929 end if;
16930
16931 else
16932 Tag_Mismatch;
16933 end if;
16934 end if;
16935
16936 if Present (Prev)
16937 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16938 and then Present (Premature_Use (Parent (Prev)))
16939 then
16940 Error_Msg_Sloc := Sloc (N);
16941 Error_Msg_N
16942 ("\full declaration #", Premature_Use (Parent (Prev)));
16943 end if;
16944
16945 return New_Id;
16946 end if;
16947 end Find_Type_Name;
16948
16949 -------------------------
16950 -- Find_Type_Of_Object --
16951 -------------------------
16952
16953 function Find_Type_Of_Object
16954 (Obj_Def : Node_Id;
16955 Related_Nod : Node_Id) return Entity_Id
16956 is
16957 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16958 P : Node_Id := Parent (Obj_Def);
16959 T : Entity_Id;
16960 Nam : Name_Id;
16961
16962 begin
16963 -- If the parent is a component_definition node we climb to the
16964 -- component_declaration node
16965
16966 if Nkind (P) = N_Component_Definition then
16967 P := Parent (P);
16968 end if;
16969
16970 -- Case of an anonymous array subtype
16971
16972 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16973 N_Unconstrained_Array_Definition)
16974 then
16975 T := Empty;
16976 Array_Type_Declaration (T, Obj_Def);
16977
16978 -- Create an explicit subtype whenever possible
16979
16980 elsif Nkind (P) /= N_Component_Declaration
16981 and then Def_Kind = N_Subtype_Indication
16982 then
16983 -- Base name of subtype on object name, which will be unique in
16984 -- the current scope.
16985
16986 -- If this is a duplicate declaration, return base type, to avoid
16987 -- generating duplicate anonymous types.
16988
16989 if Error_Posted (P) then
16990 Analyze (Subtype_Mark (Obj_Def));
16991 return Entity (Subtype_Mark (Obj_Def));
16992 end if;
16993
16994 Nam :=
16995 New_External_Name
16996 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16997
16998 T := Make_Defining_Identifier (Sloc (P), Nam);
16999
17000 Insert_Action (Obj_Def,
17001 Make_Subtype_Declaration (Sloc (P),
17002 Defining_Identifier => T,
17003 Subtype_Indication => Relocate_Node (Obj_Def)));
17004
17005 -- This subtype may need freezing, and this will not be done
17006 -- automatically if the object declaration is not in declarative
17007 -- part. Since this is an object declaration, the type cannot always
17008 -- be frozen here. Deferred constants do not freeze their type
17009 -- (which often enough will be private).
17010
17011 if Nkind (P) = N_Object_Declaration
17012 and then Constant_Present (P)
17013 and then No (Expression (P))
17014 then
17015 null;
17016
17017 -- Here we freeze the base type of object type to catch premature use
17018 -- of discriminated private type without a full view.
17019
17020 else
17021 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17022 end if;
17023
17024 -- Ada 2005 AI-406: the object definition in an object declaration
17025 -- can be an access definition.
17026
17027 elsif Def_Kind = N_Access_Definition then
17028 T := Access_Definition (Related_Nod, Obj_Def);
17029
17030 Set_Is_Local_Anonymous_Access
17031 (T,
17032 V => (Ada_Version < Ada_2012)
17033 or else (Nkind (P) /= N_Object_Declaration)
17034 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17035
17036 -- Otherwise, the object definition is just a subtype_mark
17037
17038 else
17039 T := Process_Subtype (Obj_Def, Related_Nod);
17040
17041 -- If expansion is disabled an object definition that is an aggregate
17042 -- will not get expanded and may lead to scoping problems in the back
17043 -- end, if the object is referenced in an inner scope. In that case
17044 -- create an itype reference for the object definition now. This
17045 -- may be redundant in some cases, but harmless.
17046
17047 if Is_Itype (T)
17048 and then Nkind (Related_Nod) = N_Object_Declaration
17049 and then ASIS_Mode
17050 then
17051 Build_Itype_Reference (T, Related_Nod);
17052 end if;
17053 end if;
17054
17055 return T;
17056 end Find_Type_Of_Object;
17057
17058 --------------------------------
17059 -- Find_Type_Of_Subtype_Indic --
17060 --------------------------------
17061
17062 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17063 Typ : Entity_Id;
17064
17065 begin
17066 -- Case of subtype mark with a constraint
17067
17068 if Nkind (S) = N_Subtype_Indication then
17069 Find_Type (Subtype_Mark (S));
17070 Typ := Entity (Subtype_Mark (S));
17071
17072 if not
17073 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17074 then
17075 Error_Msg_N
17076 ("incorrect constraint for this kind of type", Constraint (S));
17077 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17078 end if;
17079
17080 -- Otherwise we have a subtype mark without a constraint
17081
17082 elsif Error_Posted (S) then
17083 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17084 return Any_Type;
17085
17086 else
17087 Find_Type (S);
17088 Typ := Entity (S);
17089 end if;
17090
17091 -- Check No_Wide_Characters restriction
17092
17093 Check_Wide_Character_Restriction (Typ, S);
17094
17095 return Typ;
17096 end Find_Type_Of_Subtype_Indic;
17097
17098 -------------------------------------
17099 -- Floating_Point_Type_Declaration --
17100 -------------------------------------
17101
17102 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17103 Digs : constant Node_Id := Digits_Expression (Def);
17104 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17105 Digs_Val : Uint;
17106 Base_Typ : Entity_Id;
17107 Implicit_Base : Entity_Id;
17108 Bound : Node_Id;
17109
17110 function Can_Derive_From (E : Entity_Id) return Boolean;
17111 -- Find if given digits value, and possibly a specified range, allows
17112 -- derivation from specified type
17113
17114 function Find_Base_Type return Entity_Id;
17115 -- Find a predefined base type that Def can derive from, or generate
17116 -- an error and substitute Long_Long_Float if none exists.
17117
17118 ---------------------
17119 -- Can_Derive_From --
17120 ---------------------
17121
17122 function Can_Derive_From (E : Entity_Id) return Boolean is
17123 Spec : constant Entity_Id := Real_Range_Specification (Def);
17124
17125 begin
17126 -- Check specified "digits" constraint
17127
17128 if Digs_Val > Digits_Value (E) then
17129 return False;
17130 end if;
17131
17132 -- Check for matching range, if specified
17133
17134 if Present (Spec) then
17135 if Expr_Value_R (Type_Low_Bound (E)) >
17136 Expr_Value_R (Low_Bound (Spec))
17137 then
17138 return False;
17139 end if;
17140
17141 if Expr_Value_R (Type_High_Bound (E)) <
17142 Expr_Value_R (High_Bound (Spec))
17143 then
17144 return False;
17145 end if;
17146 end if;
17147
17148 return True;
17149 end Can_Derive_From;
17150
17151 --------------------
17152 -- Find_Base_Type --
17153 --------------------
17154
17155 function Find_Base_Type return Entity_Id is
17156 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17157
17158 begin
17159 -- Iterate over the predefined types in order, returning the first
17160 -- one that Def can derive from.
17161
17162 while Present (Choice) loop
17163 if Can_Derive_From (Node (Choice)) then
17164 return Node (Choice);
17165 end if;
17166
17167 Next_Elmt (Choice);
17168 end loop;
17169
17170 -- If we can't derive from any existing type, use Long_Long_Float
17171 -- and give appropriate message explaining the problem.
17172
17173 if Digs_Val > Max_Digs_Val then
17174 -- It might be the case that there is a type with the requested
17175 -- range, just not the combination of digits and range.
17176
17177 Error_Msg_N
17178 ("no predefined type has requested range and precision",
17179 Real_Range_Specification (Def));
17180
17181 else
17182 Error_Msg_N
17183 ("range too large for any predefined type",
17184 Real_Range_Specification (Def));
17185 end if;
17186
17187 return Standard_Long_Long_Float;
17188 end Find_Base_Type;
17189
17190 -- Start of processing for Floating_Point_Type_Declaration
17191
17192 begin
17193 Check_Restriction (No_Floating_Point, Def);
17194
17195 -- Create an implicit base type
17196
17197 Implicit_Base :=
17198 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17199
17200 -- Analyze and verify digits value
17201
17202 Analyze_And_Resolve (Digs, Any_Integer);
17203 Check_Digits_Expression (Digs);
17204 Digs_Val := Expr_Value (Digs);
17205
17206 -- Process possible range spec and find correct type to derive from
17207
17208 Process_Real_Range_Specification (Def);
17209
17210 -- Check that requested number of digits is not too high.
17211
17212 if Digs_Val > Max_Digs_Val then
17213
17214 -- The check for Max_Base_Digits may be somewhat expensive, as it
17215 -- requires reading System, so only do it when necessary.
17216
17217 declare
17218 Max_Base_Digits : constant Uint :=
17219 Expr_Value
17220 (Expression
17221 (Parent (RTE (RE_Max_Base_Digits))));
17222
17223 begin
17224 if Digs_Val > Max_Base_Digits then
17225 Error_Msg_Uint_1 := Max_Base_Digits;
17226 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17227
17228 elsif No (Real_Range_Specification (Def)) then
17229 Error_Msg_Uint_1 := Max_Digs_Val;
17230 Error_Msg_N ("types with more than ^ digits need range spec "
17231 & "(RM 3.5.7(6))", Digs);
17232 end if;
17233 end;
17234 end if;
17235
17236 -- Find a suitable type to derive from or complain and use a substitute
17237
17238 Base_Typ := Find_Base_Type;
17239
17240 -- If there are bounds given in the declaration use them as the bounds
17241 -- of the type, otherwise use the bounds of the predefined base type
17242 -- that was chosen based on the Digits value.
17243
17244 if Present (Real_Range_Specification (Def)) then
17245 Set_Scalar_Range (T, Real_Range_Specification (Def));
17246 Set_Is_Constrained (T);
17247
17248 -- The bounds of this range must be converted to machine numbers
17249 -- in accordance with RM 4.9(38).
17250
17251 Bound := Type_Low_Bound (T);
17252
17253 if Nkind (Bound) = N_Real_Literal then
17254 Set_Realval
17255 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17256 Set_Is_Machine_Number (Bound);
17257 end if;
17258
17259 Bound := Type_High_Bound (T);
17260
17261 if Nkind (Bound) = N_Real_Literal then
17262 Set_Realval
17263 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17264 Set_Is_Machine_Number (Bound);
17265 end if;
17266
17267 else
17268 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17269 end if;
17270
17271 -- Complete definition of implicit base and declared first subtype. The
17272 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17273 -- are not clobbered when the floating point type acts as a full view of
17274 -- a private type.
17275
17276 Set_Etype (Implicit_Base, Base_Typ);
17277 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17278 Set_Size_Info (Implicit_Base, Base_Typ);
17279 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17280 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17281 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17282 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17283
17284 Set_Ekind (T, E_Floating_Point_Subtype);
17285 Set_Etype (T, Implicit_Base);
17286 Set_Size_Info (T, Implicit_Base);
17287 Set_RM_Size (T, RM_Size (Implicit_Base));
17288 Inherit_Rep_Item_Chain (T, Implicit_Base);
17289 Set_Digits_Value (T, Digs_Val);
17290 end Floating_Point_Type_Declaration;
17291
17292 ----------------------------
17293 -- Get_Discriminant_Value --
17294 ----------------------------
17295
17296 -- This is the situation:
17297
17298 -- There is a non-derived type
17299
17300 -- type T0 (Dx, Dy, Dz...)
17301
17302 -- There are zero or more levels of derivation, with each derivation
17303 -- either purely inheriting the discriminants, or defining its own.
17304
17305 -- type Ti is new Ti-1
17306 -- or
17307 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17308 -- or
17309 -- subtype Ti is ...
17310
17311 -- The subtype issue is avoided by the use of Original_Record_Component,
17312 -- and the fact that derived subtypes also derive the constraints.
17313
17314 -- This chain leads back from
17315
17316 -- Typ_For_Constraint
17317
17318 -- Typ_For_Constraint has discriminants, and the value for each
17319 -- discriminant is given by its corresponding Elmt of Constraints.
17320
17321 -- Discriminant is some discriminant in this hierarchy
17322
17323 -- We need to return its value
17324
17325 -- We do this by recursively searching each level, and looking for
17326 -- Discriminant. Once we get to the bottom, we start backing up
17327 -- returning the value for it which may in turn be a discriminant
17328 -- further up, so on the backup we continue the substitution.
17329
17330 function Get_Discriminant_Value
17331 (Discriminant : Entity_Id;
17332 Typ_For_Constraint : Entity_Id;
17333 Constraint : Elist_Id) return Node_Id
17334 is
17335 function Root_Corresponding_Discriminant
17336 (Discr : Entity_Id) return Entity_Id;
17337 -- Given a discriminant, traverse the chain of inherited discriminants
17338 -- and return the topmost discriminant.
17339
17340 function Search_Derivation_Levels
17341 (Ti : Entity_Id;
17342 Discrim_Values : Elist_Id;
17343 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17344 -- This is the routine that performs the recursive search of levels
17345 -- as described above.
17346
17347 -------------------------------------
17348 -- Root_Corresponding_Discriminant --
17349 -------------------------------------
17350
17351 function Root_Corresponding_Discriminant
17352 (Discr : Entity_Id) return Entity_Id
17353 is
17354 D : Entity_Id;
17355
17356 begin
17357 D := Discr;
17358 while Present (Corresponding_Discriminant (D)) loop
17359 D := Corresponding_Discriminant (D);
17360 end loop;
17361
17362 return D;
17363 end Root_Corresponding_Discriminant;
17364
17365 ------------------------------
17366 -- Search_Derivation_Levels --
17367 ------------------------------
17368
17369 function Search_Derivation_Levels
17370 (Ti : Entity_Id;
17371 Discrim_Values : Elist_Id;
17372 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17373 is
17374 Assoc : Elmt_Id;
17375 Disc : Entity_Id;
17376 Result : Node_Or_Entity_Id;
17377 Result_Entity : Node_Id;
17378
17379 begin
17380 -- If inappropriate type, return Error, this happens only in
17381 -- cascaded error situations, and we want to avoid a blow up.
17382
17383 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17384 return Error;
17385 end if;
17386
17387 -- Look deeper if possible. Use Stored_Constraints only for
17388 -- untagged types. For tagged types use the given constraint.
17389 -- This asymmetry needs explanation???
17390
17391 if not Stored_Discrim_Values
17392 and then Present (Stored_Constraint (Ti))
17393 and then not Is_Tagged_Type (Ti)
17394 then
17395 Result :=
17396 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17397 else
17398 declare
17399 Td : constant Entity_Id := Etype (Ti);
17400
17401 begin
17402 if Td = Ti then
17403 Result := Discriminant;
17404
17405 else
17406 if Present (Stored_Constraint (Ti)) then
17407 Result :=
17408 Search_Derivation_Levels
17409 (Td, Stored_Constraint (Ti), True);
17410 else
17411 Result :=
17412 Search_Derivation_Levels
17413 (Td, Discrim_Values, Stored_Discrim_Values);
17414 end if;
17415 end if;
17416 end;
17417 end if;
17418
17419 -- Extra underlying places to search, if not found above. For
17420 -- concurrent types, the relevant discriminant appears in the
17421 -- corresponding record. For a type derived from a private type
17422 -- without discriminant, the full view inherits the discriminants
17423 -- of the full view of the parent.
17424
17425 if Result = Discriminant then
17426 if Is_Concurrent_Type (Ti)
17427 and then Present (Corresponding_Record_Type (Ti))
17428 then
17429 Result :=
17430 Search_Derivation_Levels (
17431 Corresponding_Record_Type (Ti),
17432 Discrim_Values,
17433 Stored_Discrim_Values);
17434
17435 elsif Is_Private_Type (Ti)
17436 and then not Has_Discriminants (Ti)
17437 and then Present (Full_View (Ti))
17438 and then Etype (Full_View (Ti)) /= Ti
17439 then
17440 Result :=
17441 Search_Derivation_Levels (
17442 Full_View (Ti),
17443 Discrim_Values,
17444 Stored_Discrim_Values);
17445 end if;
17446 end if;
17447
17448 -- If Result is not a (reference to a) discriminant, return it,
17449 -- otherwise set Result_Entity to the discriminant.
17450
17451 if Nkind (Result) = N_Defining_Identifier then
17452 pragma Assert (Result = Discriminant);
17453 Result_Entity := Result;
17454
17455 else
17456 if not Denotes_Discriminant (Result) then
17457 return Result;
17458 end if;
17459
17460 Result_Entity := Entity (Result);
17461 end if;
17462
17463 -- See if this level of derivation actually has discriminants because
17464 -- tagged derivations can add them, hence the lower levels need not
17465 -- have any.
17466
17467 if not Has_Discriminants (Ti) then
17468 return Result;
17469 end if;
17470
17471 -- Scan Ti's discriminants for Result_Entity, and return its
17472 -- corresponding value, if any.
17473
17474 Result_Entity := Original_Record_Component (Result_Entity);
17475
17476 Assoc := First_Elmt (Discrim_Values);
17477
17478 if Stored_Discrim_Values then
17479 Disc := First_Stored_Discriminant (Ti);
17480 else
17481 Disc := First_Discriminant (Ti);
17482 end if;
17483
17484 while Present (Disc) loop
17485 pragma Assert (Present (Assoc));
17486
17487 if Original_Record_Component (Disc) = Result_Entity then
17488 return Node (Assoc);
17489 end if;
17490
17491 Next_Elmt (Assoc);
17492
17493 if Stored_Discrim_Values then
17494 Next_Stored_Discriminant (Disc);
17495 else
17496 Next_Discriminant (Disc);
17497 end if;
17498 end loop;
17499
17500 -- Could not find it
17501
17502 return Result;
17503 end Search_Derivation_Levels;
17504
17505 -- Local Variables
17506
17507 Result : Node_Or_Entity_Id;
17508
17509 -- Start of processing for Get_Discriminant_Value
17510
17511 begin
17512 -- ??? This routine is a gigantic mess and will be deleted. For the
17513 -- time being just test for the trivial case before calling recurse.
17514
17515 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17516 declare
17517 D : Entity_Id;
17518 E : Elmt_Id;
17519
17520 begin
17521 D := First_Discriminant (Typ_For_Constraint);
17522 E := First_Elmt (Constraint);
17523 while Present (D) loop
17524 if Chars (D) = Chars (Discriminant) then
17525 return Node (E);
17526 end if;
17527
17528 Next_Discriminant (D);
17529 Next_Elmt (E);
17530 end loop;
17531 end;
17532 end if;
17533
17534 Result := Search_Derivation_Levels
17535 (Typ_For_Constraint, Constraint, False);
17536
17537 -- ??? hack to disappear when this routine is gone
17538
17539 if Nkind (Result) = N_Defining_Identifier then
17540 declare
17541 D : Entity_Id;
17542 E : Elmt_Id;
17543
17544 begin
17545 D := First_Discriminant (Typ_For_Constraint);
17546 E := First_Elmt (Constraint);
17547 while Present (D) loop
17548 if Root_Corresponding_Discriminant (D) = Discriminant then
17549 return Node (E);
17550 end if;
17551
17552 Next_Discriminant (D);
17553 Next_Elmt (E);
17554 end loop;
17555 end;
17556 end if;
17557
17558 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17559 return Result;
17560 end Get_Discriminant_Value;
17561
17562 --------------------------
17563 -- Has_Range_Constraint --
17564 --------------------------
17565
17566 function Has_Range_Constraint (N : Node_Id) return Boolean is
17567 C : constant Node_Id := Constraint (N);
17568
17569 begin
17570 if Nkind (C) = N_Range_Constraint then
17571 return True;
17572
17573 elsif Nkind (C) = N_Digits_Constraint then
17574 return
17575 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17576 or else Present (Range_Constraint (C));
17577
17578 elsif Nkind (C) = N_Delta_Constraint then
17579 return Present (Range_Constraint (C));
17580
17581 else
17582 return False;
17583 end if;
17584 end Has_Range_Constraint;
17585
17586 ------------------------
17587 -- Inherit_Components --
17588 ------------------------
17589
17590 function Inherit_Components
17591 (N : Node_Id;
17592 Parent_Base : Entity_Id;
17593 Derived_Base : Entity_Id;
17594 Is_Tagged : Boolean;
17595 Inherit_Discr : Boolean;
17596 Discs : Elist_Id) return Elist_Id
17597 is
17598 Assoc_List : constant Elist_Id := New_Elmt_List;
17599
17600 procedure Inherit_Component
17601 (Old_C : Entity_Id;
17602 Plain_Discrim : Boolean := False;
17603 Stored_Discrim : Boolean := False);
17604 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17605 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17606 -- True, Old_C is a stored discriminant. If they are both false then
17607 -- Old_C is a regular component.
17608
17609 -----------------------
17610 -- Inherit_Component --
17611 -----------------------
17612
17613 procedure Inherit_Component
17614 (Old_C : Entity_Id;
17615 Plain_Discrim : Boolean := False;
17616 Stored_Discrim : Boolean := False)
17617 is
17618 procedure Set_Anonymous_Type (Id : Entity_Id);
17619 -- Id denotes the entity of an access discriminant or anonymous
17620 -- access component. Set the type of Id to either the same type of
17621 -- Old_C or create a new one depending on whether the parent and
17622 -- the child types are in the same scope.
17623
17624 ------------------------
17625 -- Set_Anonymous_Type --
17626 ------------------------
17627
17628 procedure Set_Anonymous_Type (Id : Entity_Id) is
17629 Old_Typ : constant Entity_Id := Etype (Old_C);
17630
17631 begin
17632 if Scope (Parent_Base) = Scope (Derived_Base) then
17633 Set_Etype (Id, Old_Typ);
17634
17635 -- The parent and the derived type are in two different scopes.
17636 -- Reuse the type of the original discriminant / component by
17637 -- copying it in order to preserve all attributes.
17638
17639 else
17640 declare
17641 Typ : constant Entity_Id := New_Copy (Old_Typ);
17642
17643 begin
17644 Set_Etype (Id, Typ);
17645
17646 -- Since we do not generate component declarations for
17647 -- inherited components, associate the itype with the
17648 -- derived type.
17649
17650 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17651 Set_Scope (Typ, Derived_Base);
17652 end;
17653 end if;
17654 end Set_Anonymous_Type;
17655
17656 -- Local variables and constants
17657
17658 New_C : constant Entity_Id := New_Copy (Old_C);
17659
17660 Corr_Discrim : Entity_Id;
17661 Discrim : Entity_Id;
17662
17663 -- Start of processing for Inherit_Component
17664
17665 begin
17666 pragma Assert (not Is_Tagged or not Stored_Discrim);
17667
17668 Set_Parent (New_C, Parent (Old_C));
17669
17670 -- Regular discriminants and components must be inserted in the scope
17671 -- of the Derived_Base. Do it here.
17672
17673 if not Stored_Discrim then
17674 Enter_Name (New_C);
17675 end if;
17676
17677 -- For tagged types the Original_Record_Component must point to
17678 -- whatever this field was pointing to in the parent type. This has
17679 -- already been achieved by the call to New_Copy above.
17680
17681 if not Is_Tagged then
17682 Set_Original_Record_Component (New_C, New_C);
17683 end if;
17684
17685 -- Set the proper type of an access discriminant
17686
17687 if Ekind (New_C) = E_Discriminant
17688 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17689 then
17690 Set_Anonymous_Type (New_C);
17691 end if;
17692
17693 -- If we have inherited a component then see if its Etype contains
17694 -- references to Parent_Base discriminants. In this case, replace
17695 -- these references with the constraints given in Discs. We do not
17696 -- do this for the partial view of private types because this is
17697 -- not needed (only the components of the full view will be used
17698 -- for code generation) and cause problem. We also avoid this
17699 -- transformation in some error situations.
17700
17701 if Ekind (New_C) = E_Component then
17702
17703 -- Set the proper type of an anonymous access component
17704
17705 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17706 Set_Anonymous_Type (New_C);
17707
17708 elsif (Is_Private_Type (Derived_Base)
17709 and then not Is_Generic_Type (Derived_Base))
17710 or else (Is_Empty_Elmt_List (Discs)
17711 and then not Expander_Active)
17712 then
17713 Set_Etype (New_C, Etype (Old_C));
17714
17715 else
17716 -- The current component introduces a circularity of the
17717 -- following kind:
17718
17719 -- limited with Pack_2;
17720 -- package Pack_1 is
17721 -- type T_1 is tagged record
17722 -- Comp : access Pack_2.T_2;
17723 -- ...
17724 -- end record;
17725 -- end Pack_1;
17726
17727 -- with Pack_1;
17728 -- package Pack_2 is
17729 -- type T_2 is new Pack_1.T_1 with ...;
17730 -- end Pack_2;
17731
17732 Set_Etype
17733 (New_C,
17734 Constrain_Component_Type
17735 (Old_C, Derived_Base, N, Parent_Base, Discs));
17736 end if;
17737 end if;
17738
17739 -- In derived tagged types it is illegal to reference a non
17740 -- discriminant component in the parent type. To catch this, mark
17741 -- these components with an Ekind of E_Void. This will be reset in
17742 -- Record_Type_Definition after processing the record extension of
17743 -- the derived type.
17744
17745 -- If the declaration is a private extension, there is no further
17746 -- record extension to process, and the components retain their
17747 -- current kind, because they are visible at this point.
17748
17749 if Is_Tagged and then Ekind (New_C) = E_Component
17750 and then Nkind (N) /= N_Private_Extension_Declaration
17751 then
17752 Set_Ekind (New_C, E_Void);
17753 end if;
17754
17755 if Plain_Discrim then
17756 Set_Corresponding_Discriminant (New_C, Old_C);
17757 Build_Discriminal (New_C);
17758
17759 -- If we are explicitly inheriting a stored discriminant it will be
17760 -- completely hidden.
17761
17762 elsif Stored_Discrim then
17763 Set_Corresponding_Discriminant (New_C, Empty);
17764 Set_Discriminal (New_C, Empty);
17765 Set_Is_Completely_Hidden (New_C);
17766
17767 -- Set the Original_Record_Component of each discriminant in the
17768 -- derived base to point to the corresponding stored that we just
17769 -- created.
17770
17771 Discrim := First_Discriminant (Derived_Base);
17772 while Present (Discrim) loop
17773 Corr_Discrim := Corresponding_Discriminant (Discrim);
17774
17775 -- Corr_Discrim could be missing in an error situation
17776
17777 if Present (Corr_Discrim)
17778 and then Original_Record_Component (Corr_Discrim) = Old_C
17779 then
17780 Set_Original_Record_Component (Discrim, New_C);
17781 end if;
17782
17783 Next_Discriminant (Discrim);
17784 end loop;
17785
17786 Append_Entity (New_C, Derived_Base);
17787 end if;
17788
17789 if not Is_Tagged then
17790 Append_Elmt (Old_C, Assoc_List);
17791 Append_Elmt (New_C, Assoc_List);
17792 end if;
17793 end Inherit_Component;
17794
17795 -- Variables local to Inherit_Component
17796
17797 Loc : constant Source_Ptr := Sloc (N);
17798
17799 Parent_Discrim : Entity_Id;
17800 Stored_Discrim : Entity_Id;
17801 D : Entity_Id;
17802 Component : Entity_Id;
17803
17804 -- Start of processing for Inherit_Components
17805
17806 begin
17807 if not Is_Tagged then
17808 Append_Elmt (Parent_Base, Assoc_List);
17809 Append_Elmt (Derived_Base, Assoc_List);
17810 end if;
17811
17812 -- Inherit parent discriminants if needed
17813
17814 if Inherit_Discr then
17815 Parent_Discrim := First_Discriminant (Parent_Base);
17816 while Present (Parent_Discrim) loop
17817 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17818 Next_Discriminant (Parent_Discrim);
17819 end loop;
17820 end if;
17821
17822 -- Create explicit stored discrims for untagged types when necessary
17823
17824 if not Has_Unknown_Discriminants (Derived_Base)
17825 and then Has_Discriminants (Parent_Base)
17826 and then not Is_Tagged
17827 and then
17828 (not Inherit_Discr
17829 or else First_Discriminant (Parent_Base) /=
17830 First_Stored_Discriminant (Parent_Base))
17831 then
17832 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17833 while Present (Stored_Discrim) loop
17834 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17835 Next_Stored_Discriminant (Stored_Discrim);
17836 end loop;
17837 end if;
17838
17839 -- See if we can apply the second transformation for derived types, as
17840 -- explained in point 6. in the comments above Build_Derived_Record_Type
17841 -- This is achieved by appending Derived_Base discriminants into Discs,
17842 -- which has the side effect of returning a non empty Discs list to the
17843 -- caller of Inherit_Components, which is what we want. This must be
17844 -- done for private derived types if there are explicit stored
17845 -- discriminants, to ensure that we can retrieve the values of the
17846 -- constraints provided in the ancestors.
17847
17848 if Inherit_Discr
17849 and then Is_Empty_Elmt_List (Discs)
17850 and then Present (First_Discriminant (Derived_Base))
17851 and then
17852 (not Is_Private_Type (Derived_Base)
17853 or else Is_Completely_Hidden
17854 (First_Stored_Discriminant (Derived_Base))
17855 or else Is_Generic_Type (Derived_Base))
17856 then
17857 D := First_Discriminant (Derived_Base);
17858 while Present (D) loop
17859 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17860 Next_Discriminant (D);
17861 end loop;
17862 end if;
17863
17864 -- Finally, inherit non-discriminant components unless they are not
17865 -- visible because defined or inherited from the full view of the
17866 -- parent. Don't inherit the _parent field of the parent type.
17867
17868 Component := First_Entity (Parent_Base);
17869 while Present (Component) loop
17870
17871 -- Ada 2005 (AI-251): Do not inherit components associated with
17872 -- secondary tags of the parent.
17873
17874 if Ekind (Component) = E_Component
17875 and then Present (Related_Type (Component))
17876 then
17877 null;
17878
17879 elsif Ekind (Component) /= E_Component
17880 or else Chars (Component) = Name_uParent
17881 then
17882 null;
17883
17884 -- If the derived type is within the parent type's declarative
17885 -- region, then the components can still be inherited even though
17886 -- they aren't visible at this point. This can occur for cases
17887 -- such as within public child units where the components must
17888 -- become visible upon entering the child unit's private part.
17889
17890 elsif not Is_Visible_Component (Component)
17891 and then not In_Open_Scopes (Scope (Parent_Base))
17892 then
17893 null;
17894
17895 elsif Ekind_In (Derived_Base, E_Private_Type,
17896 E_Limited_Private_Type)
17897 then
17898 null;
17899
17900 else
17901 Inherit_Component (Component);
17902 end if;
17903
17904 Next_Entity (Component);
17905 end loop;
17906
17907 -- For tagged derived types, inherited discriminants cannot be used in
17908 -- component declarations of the record extension part. To achieve this
17909 -- we mark the inherited discriminants as not visible.
17910
17911 if Is_Tagged and then Inherit_Discr then
17912 D := First_Discriminant (Derived_Base);
17913 while Present (D) loop
17914 Set_Is_Immediately_Visible (D, False);
17915 Next_Discriminant (D);
17916 end loop;
17917 end if;
17918
17919 return Assoc_List;
17920 end Inherit_Components;
17921
17922 -----------------------------
17923 -- Inherit_Predicate_Flags --
17924 -----------------------------
17925
17926 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17927 begin
17928 Set_Has_Predicates (Subt, Has_Predicates (Par));
17929 Set_Has_Static_Predicate_Aspect
17930 (Subt, Has_Static_Predicate_Aspect (Par));
17931 Set_Has_Dynamic_Predicate_Aspect
17932 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17933 end Inherit_Predicate_Flags;
17934
17935 ----------------------
17936 -- Is_EVF_Procedure --
17937 ----------------------
17938
17939 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17940 Formal : Entity_Id;
17941
17942 begin
17943 -- Examine the formals of an Extensions_Visible False procedure looking
17944 -- for a controlling OUT parameter.
17945
17946 if Ekind (Subp) = E_Procedure
17947 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17948 then
17949 Formal := First_Formal (Subp);
17950 while Present (Formal) loop
17951 if Ekind (Formal) = E_Out_Parameter
17952 and then Is_Controlling_Formal (Formal)
17953 then
17954 return True;
17955 end if;
17956
17957 Next_Formal (Formal);
17958 end loop;
17959 end if;
17960
17961 return False;
17962 end Is_EVF_Procedure;
17963
17964 -----------------------
17965 -- Is_Null_Extension --
17966 -----------------------
17967
17968 function Is_Null_Extension (T : Entity_Id) return Boolean is
17969 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17970 Comp_List : Node_Id;
17971 Comp : Node_Id;
17972
17973 begin
17974 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17975 or else not Is_Tagged_Type (T)
17976 or else Nkind (Type_Definition (Type_Decl)) /=
17977 N_Derived_Type_Definition
17978 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17979 then
17980 return False;
17981 end if;
17982
17983 Comp_List :=
17984 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17985
17986 if Present (Discriminant_Specifications (Type_Decl)) then
17987 return False;
17988
17989 elsif Present (Comp_List)
17990 and then Is_Non_Empty_List (Component_Items (Comp_List))
17991 then
17992 Comp := First (Component_Items (Comp_List));
17993
17994 -- Only user-defined components are relevant. The component list
17995 -- may also contain a parent component and internal components
17996 -- corresponding to secondary tags, but these do not determine
17997 -- whether this is a null extension.
17998
17999 while Present (Comp) loop
18000 if Comes_From_Source (Comp) then
18001 return False;
18002 end if;
18003
18004 Next (Comp);
18005 end loop;
18006
18007 return True;
18008
18009 else
18010 return True;
18011 end if;
18012 end Is_Null_Extension;
18013
18014 ------------------------------
18015 -- Is_Valid_Constraint_Kind --
18016 ------------------------------
18017
18018 function Is_Valid_Constraint_Kind
18019 (T_Kind : Type_Kind;
18020 Constraint_Kind : Node_Kind) return Boolean
18021 is
18022 begin
18023 case T_Kind is
18024 when Enumeration_Kind |
18025 Integer_Kind =>
18026 return Constraint_Kind = N_Range_Constraint;
18027
18028 when Decimal_Fixed_Point_Kind =>
18029 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18030 N_Range_Constraint);
18031
18032 when Ordinary_Fixed_Point_Kind =>
18033 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18034 N_Range_Constraint);
18035
18036 when Float_Kind =>
18037 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18038 N_Range_Constraint);
18039
18040 when Access_Kind |
18041 Array_Kind |
18042 E_Record_Type |
18043 E_Record_Subtype |
18044 Class_Wide_Kind |
18045 E_Incomplete_Type |
18046 Private_Kind |
18047 Concurrent_Kind =>
18048 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18049
18050 when others =>
18051 return True; -- Error will be detected later
18052 end case;
18053 end Is_Valid_Constraint_Kind;
18054
18055 --------------------------
18056 -- Is_Visible_Component --
18057 --------------------------
18058
18059 function Is_Visible_Component
18060 (C : Entity_Id;
18061 N : Node_Id := Empty) return Boolean
18062 is
18063 Original_Comp : Entity_Id := Empty;
18064 Original_Type : Entity_Id;
18065 Type_Scope : Entity_Id;
18066
18067 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18068 -- Check whether parent type of inherited component is declared locally,
18069 -- possibly within a nested package or instance. The current scope is
18070 -- the derived record itself.
18071
18072 -------------------
18073 -- Is_Local_Type --
18074 -------------------
18075
18076 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18077 Scop : Entity_Id;
18078
18079 begin
18080 Scop := Scope (Typ);
18081 while Present (Scop)
18082 and then Scop /= Standard_Standard
18083 loop
18084 if Scop = Scope (Current_Scope) then
18085 return True;
18086 end if;
18087
18088 Scop := Scope (Scop);
18089 end loop;
18090
18091 return False;
18092 end Is_Local_Type;
18093
18094 -- Start of processing for Is_Visible_Component
18095
18096 begin
18097 if Ekind_In (C, E_Component, E_Discriminant) then
18098 Original_Comp := Original_Record_Component (C);
18099 end if;
18100
18101 if No (Original_Comp) then
18102
18103 -- Premature usage, or previous error
18104
18105 return False;
18106
18107 else
18108 Original_Type := Scope (Original_Comp);
18109 Type_Scope := Scope (Base_Type (Scope (C)));
18110 end if;
18111
18112 -- This test only concerns tagged types
18113
18114 if not Is_Tagged_Type (Original_Type) then
18115 return True;
18116
18117 -- If it is _Parent or _Tag, there is no visibility issue
18118
18119 elsif not Comes_From_Source (Original_Comp) then
18120 return True;
18121
18122 -- Discriminants are visible unless the (private) type has unknown
18123 -- discriminants. If the discriminant reference is inserted for a
18124 -- discriminant check on a full view it is also visible.
18125
18126 elsif Ekind (Original_Comp) = E_Discriminant
18127 and then
18128 (not Has_Unknown_Discriminants (Original_Type)
18129 or else (Present (N)
18130 and then Nkind (N) = N_Selected_Component
18131 and then Nkind (Prefix (N)) = N_Type_Conversion
18132 and then not Comes_From_Source (Prefix (N))))
18133 then
18134 return True;
18135
18136 -- In the body of an instantiation, check the visibility of a component
18137 -- in case it has a homograph that is a primitive operation of a private
18138 -- type which was not visible in the generic unit.
18139
18140 -- Should Is_Prefixed_Call be propagated from template to instance???
18141
18142 elsif In_Instance_Body then
18143 if not Is_Tagged_Type (Original_Type)
18144 or else not Is_Private_Type (Original_Type)
18145 then
18146 return True;
18147
18148 else
18149 declare
18150 Subp_Elmt : Elmt_Id;
18151
18152 begin
18153 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18154 while Present (Subp_Elmt) loop
18155
18156 -- The component is hidden by a primitive operation
18157
18158 if Chars (Node (Subp_Elmt)) = Chars (C) then
18159 return False;
18160 end if;
18161
18162 Next_Elmt (Subp_Elmt);
18163 end loop;
18164
18165 return True;
18166 end;
18167 end if;
18168
18169 -- If the component has been declared in an ancestor which is currently
18170 -- a private type, then it is not visible. The same applies if the
18171 -- component's containing type is not in an open scope and the original
18172 -- component's enclosing type is a visible full view of a private type
18173 -- (which can occur in cases where an attempt is being made to reference
18174 -- a component in a sibling package that is inherited from a visible
18175 -- component of a type in an ancestor package; the component in the
18176 -- sibling package should not be visible even though the component it
18177 -- inherited from is visible). This does not apply however in the case
18178 -- where the scope of the type is a private child unit, or when the
18179 -- parent comes from a local package in which the ancestor is currently
18180 -- visible. The latter suppression of visibility is needed for cases
18181 -- that are tested in B730006.
18182
18183 elsif Is_Private_Type (Original_Type)
18184 or else
18185 (not Is_Private_Descendant (Type_Scope)
18186 and then not In_Open_Scopes (Type_Scope)
18187 and then Has_Private_Declaration (Original_Type))
18188 then
18189 -- If the type derives from an entity in a formal package, there
18190 -- are no additional visible components.
18191
18192 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18193 N_Formal_Package_Declaration
18194 then
18195 return False;
18196
18197 -- if we are not in the private part of the current package, there
18198 -- are no additional visible components.
18199
18200 elsif Ekind (Scope (Current_Scope)) = E_Package
18201 and then not In_Private_Part (Scope (Current_Scope))
18202 then
18203 return False;
18204 else
18205 return
18206 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18207 and then In_Open_Scopes (Scope (Original_Type))
18208 and then Is_Local_Type (Type_Scope);
18209 end if;
18210
18211 -- There is another weird way in which a component may be invisible when
18212 -- the private and the full view are not derived from the same ancestor.
18213 -- Here is an example :
18214
18215 -- type A1 is tagged record F1 : integer; end record;
18216 -- type A2 is new A1 with record F2 : integer; end record;
18217 -- type T is new A1 with private;
18218 -- private
18219 -- type T is new A2 with null record;
18220
18221 -- In this case, the full view of T inherits F1 and F2 but the private
18222 -- view inherits only F1
18223
18224 else
18225 declare
18226 Ancestor : Entity_Id := Scope (C);
18227
18228 begin
18229 loop
18230 if Ancestor = Original_Type then
18231 return True;
18232
18233 -- The ancestor may have a partial view of the original type,
18234 -- but if the full view is in scope, as in a child body, the
18235 -- component is visible.
18236
18237 elsif In_Private_Part (Scope (Original_Type))
18238 and then Full_View (Ancestor) = Original_Type
18239 then
18240 return True;
18241
18242 elsif Ancestor = Etype (Ancestor) then
18243
18244 -- No further ancestors to examine
18245
18246 return False;
18247 end if;
18248
18249 Ancestor := Etype (Ancestor);
18250 end loop;
18251 end;
18252 end if;
18253 end Is_Visible_Component;
18254
18255 --------------------------
18256 -- Make_Class_Wide_Type --
18257 --------------------------
18258
18259 procedure Make_Class_Wide_Type (T : Entity_Id) is
18260 CW_Type : Entity_Id;
18261 CW_Name : Name_Id;
18262 Next_E : Entity_Id;
18263
18264 begin
18265 if Present (Class_Wide_Type (T)) then
18266
18267 -- The class-wide type is a partially decorated entity created for a
18268 -- unanalyzed tagged type referenced through a limited with clause.
18269 -- When the tagged type is analyzed, its class-wide type needs to be
18270 -- redecorated. Note that we reuse the entity created by Decorate_
18271 -- Tagged_Type in order to preserve all links.
18272
18273 if Materialize_Entity (Class_Wide_Type (T)) then
18274 CW_Type := Class_Wide_Type (T);
18275 Set_Materialize_Entity (CW_Type, False);
18276
18277 -- The class wide type can have been defined by the partial view, in
18278 -- which case everything is already done.
18279
18280 else
18281 return;
18282 end if;
18283
18284 -- Default case, we need to create a new class-wide type
18285
18286 else
18287 CW_Type :=
18288 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18289 end if;
18290
18291 -- Inherit root type characteristics
18292
18293 CW_Name := Chars (CW_Type);
18294 Next_E := Next_Entity (CW_Type);
18295 Copy_Node (T, CW_Type);
18296 Set_Comes_From_Source (CW_Type, False);
18297 Set_Chars (CW_Type, CW_Name);
18298 Set_Parent (CW_Type, Parent (T));
18299 Set_Next_Entity (CW_Type, Next_E);
18300
18301 -- Ensure we have a new freeze node for the class-wide type. The partial
18302 -- view may have freeze action of its own, requiring a proper freeze
18303 -- node, and the same freeze node cannot be shared between the two
18304 -- types.
18305
18306 Set_Has_Delayed_Freeze (CW_Type);
18307 Set_Freeze_Node (CW_Type, Empty);
18308
18309 -- Customize the class-wide type: It has no prim. op., it cannot be
18310 -- abstract, its Etype points back to the specific root type, and it
18311 -- cannot have any invariants.
18312
18313 Set_Ekind (CW_Type, E_Class_Wide_Type);
18314 Set_Is_Tagged_Type (CW_Type, True);
18315 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18316 Set_Is_Abstract_Type (CW_Type, False);
18317 Set_Is_Constrained (CW_Type, False);
18318 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18319 Set_Default_SSO (CW_Type);
18320 Set_Has_Inheritable_Invariants (CW_Type, False);
18321 Set_Has_Inherited_Invariants (CW_Type, False);
18322 Set_Has_Own_Invariants (CW_Type, False);
18323
18324 if Ekind (T) = E_Class_Wide_Subtype then
18325 Set_Etype (CW_Type, Etype (Base_Type (T)));
18326 else
18327 Set_Etype (CW_Type, T);
18328 end if;
18329
18330 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18331
18332 -- If this is the class_wide type of a constrained subtype, it does
18333 -- not have discriminants.
18334
18335 Set_Has_Discriminants (CW_Type,
18336 Has_Discriminants (T) and then not Is_Constrained (T));
18337
18338 Set_Has_Unknown_Discriminants (CW_Type, True);
18339 Set_Class_Wide_Type (T, CW_Type);
18340 Set_Equivalent_Type (CW_Type, Empty);
18341
18342 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18343
18344 Set_Class_Wide_Type (CW_Type, CW_Type);
18345
18346 -- Inherit the "ghostness" from the root tagged type
18347
18348 if Ghost_Mode > None or else Is_Ghost_Entity (T) then
18349 Set_Is_Ghost_Entity (CW_Type);
18350 end if;
18351 end Make_Class_Wide_Type;
18352
18353 ----------------
18354 -- Make_Index --
18355 ----------------
18356
18357 procedure Make_Index
18358 (N : Node_Id;
18359 Related_Nod : Node_Id;
18360 Related_Id : Entity_Id := Empty;
18361 Suffix_Index : Nat := 1;
18362 In_Iter_Schm : Boolean := False)
18363 is
18364 R : Node_Id;
18365 T : Entity_Id;
18366 Def_Id : Entity_Id := Empty;
18367 Found : Boolean := False;
18368
18369 begin
18370 -- For a discrete range used in a constrained array definition and
18371 -- defined by a range, an implicit conversion to the predefined type
18372 -- INTEGER is assumed if each bound is either a numeric literal, a named
18373 -- number, or an attribute, and the type of both bounds (prior to the
18374 -- implicit conversion) is the type universal_integer. Otherwise, both
18375 -- bounds must be of the same discrete type, other than universal
18376 -- integer; this type must be determinable independently of the
18377 -- context, but using the fact that the type must be discrete and that
18378 -- both bounds must have the same type.
18379
18380 -- Character literals also have a universal type in the absence of
18381 -- of additional context, and are resolved to Standard_Character.
18382
18383 if Nkind (N) = N_Range then
18384
18385 -- The index is given by a range constraint. The bounds are known
18386 -- to be of a consistent type.
18387
18388 if not Is_Overloaded (N) then
18389 T := Etype (N);
18390
18391 -- For universal bounds, choose the specific predefined type
18392
18393 if T = Universal_Integer then
18394 T := Standard_Integer;
18395
18396 elsif T = Any_Character then
18397 Ambiguous_Character (Low_Bound (N));
18398
18399 T := Standard_Character;
18400 end if;
18401
18402 -- The node may be overloaded because some user-defined operators
18403 -- are available, but if a universal interpretation exists it is
18404 -- also the selected one.
18405
18406 elsif Universal_Interpretation (N) = Universal_Integer then
18407 T := Standard_Integer;
18408
18409 else
18410 T := Any_Type;
18411
18412 declare
18413 Ind : Interp_Index;
18414 It : Interp;
18415
18416 begin
18417 Get_First_Interp (N, Ind, It);
18418 while Present (It.Typ) loop
18419 if Is_Discrete_Type (It.Typ) then
18420
18421 if Found
18422 and then not Covers (It.Typ, T)
18423 and then not Covers (T, It.Typ)
18424 then
18425 Error_Msg_N ("ambiguous bounds in discrete range", N);
18426 exit;
18427 else
18428 T := It.Typ;
18429 Found := True;
18430 end if;
18431 end if;
18432
18433 Get_Next_Interp (Ind, It);
18434 end loop;
18435
18436 if T = Any_Type then
18437 Error_Msg_N ("discrete type required for range", N);
18438 Set_Etype (N, Any_Type);
18439 return;
18440
18441 elsif T = Universal_Integer then
18442 T := Standard_Integer;
18443 end if;
18444 end;
18445 end if;
18446
18447 if not Is_Discrete_Type (T) then
18448 Error_Msg_N ("discrete type required for range", N);
18449 Set_Etype (N, Any_Type);
18450 return;
18451 end if;
18452
18453 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18454 and then Attribute_Name (Low_Bound (N)) = Name_First
18455 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18456 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18457 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18458 then
18459 -- The type of the index will be the type of the prefix, as long
18460 -- as the upper bound is 'Last of the same type.
18461
18462 Def_Id := Entity (Prefix (Low_Bound (N)));
18463
18464 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18465 or else Attribute_Name (High_Bound (N)) /= Name_Last
18466 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18467 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18468 then
18469 Def_Id := Empty;
18470 end if;
18471 end if;
18472
18473 R := N;
18474 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18475
18476 elsif Nkind (N) = N_Subtype_Indication then
18477
18478 -- The index is given by a subtype with a range constraint
18479
18480 T := Base_Type (Entity (Subtype_Mark (N)));
18481
18482 if not Is_Discrete_Type (T) then
18483 Error_Msg_N ("discrete type required for range", N);
18484 Set_Etype (N, Any_Type);
18485 return;
18486 end if;
18487
18488 R := Range_Expression (Constraint (N));
18489
18490 Resolve (R, T);
18491 Process_Range_Expr_In_Decl
18492 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18493
18494 elsif Nkind (N) = N_Attribute_Reference then
18495
18496 -- Catch beginner's error (use of attribute other than 'Range)
18497
18498 if Attribute_Name (N) /= Name_Range then
18499 Error_Msg_N ("expect attribute ''Range", N);
18500 Set_Etype (N, Any_Type);
18501 return;
18502 end if;
18503
18504 -- If the node denotes the range of a type mark, that is also the
18505 -- resulting type, and we do not need to create an Itype for it.
18506
18507 if Is_Entity_Name (Prefix (N))
18508 and then Comes_From_Source (N)
18509 and then Is_Type (Entity (Prefix (N)))
18510 and then Is_Discrete_Type (Entity (Prefix (N)))
18511 then
18512 Def_Id := Entity (Prefix (N));
18513 end if;
18514
18515 Analyze_And_Resolve (N);
18516 T := Etype (N);
18517 R := N;
18518
18519 -- If none of the above, must be a subtype. We convert this to a
18520 -- range attribute reference because in the case of declared first
18521 -- named subtypes, the types in the range reference can be different
18522 -- from the type of the entity. A range attribute normalizes the
18523 -- reference and obtains the correct types for the bounds.
18524
18525 -- This transformation is in the nature of an expansion, is only
18526 -- done if expansion is active. In particular, it is not done on
18527 -- formal generic types, because we need to retain the name of the
18528 -- original index for instantiation purposes.
18529
18530 else
18531 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18532 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18533 Set_Etype (N, Any_Integer);
18534 return;
18535
18536 else
18537 -- The type mark may be that of an incomplete type. It is only
18538 -- now that we can get the full view, previous analysis does
18539 -- not look specifically for a type mark.
18540
18541 Set_Entity (N, Get_Full_View (Entity (N)));
18542 Set_Etype (N, Entity (N));
18543 Def_Id := Entity (N);
18544
18545 if not Is_Discrete_Type (Def_Id) then
18546 Error_Msg_N ("discrete type required for index", N);
18547 Set_Etype (N, Any_Type);
18548 return;
18549 end if;
18550 end if;
18551
18552 if Expander_Active then
18553 Rewrite (N,
18554 Make_Attribute_Reference (Sloc (N),
18555 Attribute_Name => Name_Range,
18556 Prefix => Relocate_Node (N)));
18557
18558 -- The original was a subtype mark that does not freeze. This
18559 -- means that the rewritten version must not freeze either.
18560
18561 Set_Must_Not_Freeze (N);
18562 Set_Must_Not_Freeze (Prefix (N));
18563 Analyze_And_Resolve (N);
18564 T := Etype (N);
18565 R := N;
18566
18567 -- If expander is inactive, type is legal, nothing else to construct
18568
18569 else
18570 return;
18571 end if;
18572 end if;
18573
18574 if not Is_Discrete_Type (T) then
18575 Error_Msg_N ("discrete type required for range", N);
18576 Set_Etype (N, Any_Type);
18577 return;
18578
18579 elsif T = Any_Type then
18580 Set_Etype (N, Any_Type);
18581 return;
18582 end if;
18583
18584 -- We will now create the appropriate Itype to describe the range, but
18585 -- first a check. If we originally had a subtype, then we just label
18586 -- the range with this subtype. Not only is there no need to construct
18587 -- a new subtype, but it is wrong to do so for two reasons:
18588
18589 -- 1. A legality concern, if we have a subtype, it must not freeze,
18590 -- and the Itype would cause freezing incorrectly
18591
18592 -- 2. An efficiency concern, if we created an Itype, it would not be
18593 -- recognized as the same type for the purposes of eliminating
18594 -- checks in some circumstances.
18595
18596 -- We signal this case by setting the subtype entity in Def_Id
18597
18598 if No (Def_Id) then
18599 Def_Id :=
18600 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18601 Set_Etype (Def_Id, Base_Type (T));
18602
18603 if Is_Signed_Integer_Type (T) then
18604 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18605
18606 elsif Is_Modular_Integer_Type (T) then
18607 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18608
18609 else
18610 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18611 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18612 Set_First_Literal (Def_Id, First_Literal (T));
18613 end if;
18614
18615 Set_Size_Info (Def_Id, (T));
18616 Set_RM_Size (Def_Id, RM_Size (T));
18617 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18618
18619 Set_Scalar_Range (Def_Id, R);
18620 Conditional_Delay (Def_Id, T);
18621
18622 if Nkind (N) = N_Subtype_Indication then
18623 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18624 end if;
18625
18626 -- In the subtype indication case, if the immediate parent of the
18627 -- new subtype is non-static, then the subtype we create is non-
18628 -- static, even if its bounds are static.
18629
18630 if Nkind (N) = N_Subtype_Indication
18631 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18632 then
18633 Set_Is_Non_Static_Subtype (Def_Id);
18634 end if;
18635 end if;
18636
18637 -- Final step is to label the index with this constructed type
18638
18639 Set_Etype (N, Def_Id);
18640 end Make_Index;
18641
18642 ------------------------------
18643 -- Modular_Type_Declaration --
18644 ------------------------------
18645
18646 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18647 Mod_Expr : constant Node_Id := Expression (Def);
18648 M_Val : Uint;
18649
18650 procedure Set_Modular_Size (Bits : Int);
18651 -- Sets RM_Size to Bits, and Esize to normal word size above this
18652
18653 ----------------------
18654 -- Set_Modular_Size --
18655 ----------------------
18656
18657 procedure Set_Modular_Size (Bits : Int) is
18658 begin
18659 Set_RM_Size (T, UI_From_Int (Bits));
18660
18661 if Bits <= 8 then
18662 Init_Esize (T, 8);
18663
18664 elsif Bits <= 16 then
18665 Init_Esize (T, 16);
18666
18667 elsif Bits <= 32 then
18668 Init_Esize (T, 32);
18669
18670 else
18671 Init_Esize (T, System_Max_Binary_Modulus_Power);
18672 end if;
18673
18674 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18675 Set_Is_Known_Valid (T);
18676 end if;
18677 end Set_Modular_Size;
18678
18679 -- Start of processing for Modular_Type_Declaration
18680
18681 begin
18682 -- If the mod expression is (exactly) 2 * literal, where literal is
18683 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18684
18685 if Warn_On_Suspicious_Modulus_Value
18686 and then Nkind (Mod_Expr) = N_Op_Multiply
18687 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18688 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18689 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18690 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18691 then
18692 Error_Msg_N
18693 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18694 end if;
18695
18696 -- Proceed with analysis of mod expression
18697
18698 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18699 Set_Etype (T, T);
18700 Set_Ekind (T, E_Modular_Integer_Type);
18701 Init_Alignment (T);
18702 Set_Is_Constrained (T);
18703
18704 if not Is_OK_Static_Expression (Mod_Expr) then
18705 Flag_Non_Static_Expr
18706 ("non-static expression used for modular type bound!", Mod_Expr);
18707 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18708 else
18709 M_Val := Expr_Value (Mod_Expr);
18710 end if;
18711
18712 if M_Val < 1 then
18713 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18714 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18715 end if;
18716
18717 if M_Val > 2 ** Standard_Long_Integer_Size then
18718 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18719 end if;
18720
18721 Set_Modulus (T, M_Val);
18722
18723 -- Create bounds for the modular type based on the modulus given in
18724 -- the type declaration and then analyze and resolve those bounds.
18725
18726 Set_Scalar_Range (T,
18727 Make_Range (Sloc (Mod_Expr),
18728 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18729 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18730
18731 -- Properly analyze the literals for the range. We do this manually
18732 -- because we can't go calling Resolve, since we are resolving these
18733 -- bounds with the type, and this type is certainly not complete yet.
18734
18735 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18736 Set_Etype (High_Bound (Scalar_Range (T)), T);
18737 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18738 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18739
18740 -- Loop through powers of two to find number of bits required
18741
18742 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18743
18744 -- Binary case
18745
18746 if M_Val = 2 ** Bits then
18747 Set_Modular_Size (Bits);
18748 return;
18749
18750 -- Nonbinary case
18751
18752 elsif M_Val < 2 ** Bits then
18753 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18754 Set_Non_Binary_Modulus (T);
18755
18756 if Bits > System_Max_Nonbinary_Modulus_Power then
18757 Error_Msg_Uint_1 :=
18758 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18759 Error_Msg_F
18760 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18761 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18762 return;
18763
18764 else
18765 -- In the nonbinary case, set size as per RM 13.3(55)
18766
18767 Set_Modular_Size (Bits);
18768 return;
18769 end if;
18770 end if;
18771
18772 end loop;
18773
18774 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18775 -- so we just signal an error and set the maximum size.
18776
18777 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18778 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18779
18780 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18781 Init_Alignment (T);
18782
18783 end Modular_Type_Declaration;
18784
18785 --------------------------
18786 -- New_Concatenation_Op --
18787 --------------------------
18788
18789 procedure New_Concatenation_Op (Typ : Entity_Id) is
18790 Loc : constant Source_Ptr := Sloc (Typ);
18791 Op : Entity_Id;
18792
18793 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18794 -- Create abbreviated declaration for the formal of a predefined
18795 -- Operator 'Op' of type 'Typ'
18796
18797 --------------------
18798 -- Make_Op_Formal --
18799 --------------------
18800
18801 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18802 Formal : Entity_Id;
18803 begin
18804 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18805 Set_Etype (Formal, Typ);
18806 Set_Mechanism (Formal, Default_Mechanism);
18807 return Formal;
18808 end Make_Op_Formal;
18809
18810 -- Start of processing for New_Concatenation_Op
18811
18812 begin
18813 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18814
18815 Set_Ekind (Op, E_Operator);
18816 Set_Scope (Op, Current_Scope);
18817 Set_Etype (Op, Typ);
18818 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18819 Set_Is_Immediately_Visible (Op);
18820 Set_Is_Intrinsic_Subprogram (Op);
18821 Set_Has_Completion (Op);
18822 Append_Entity (Op, Current_Scope);
18823
18824 Set_Name_Entity_Id (Name_Op_Concat, Op);
18825
18826 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18827 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18828 end New_Concatenation_Op;
18829
18830 -------------------------
18831 -- OK_For_Limited_Init --
18832 -------------------------
18833
18834 -- ???Check all calls of this, and compare the conditions under which it's
18835 -- called.
18836
18837 function OK_For_Limited_Init
18838 (Typ : Entity_Id;
18839 Exp : Node_Id) return Boolean
18840 is
18841 begin
18842 return Is_CPP_Constructor_Call (Exp)
18843 or else (Ada_Version >= Ada_2005
18844 and then not Debug_Flag_Dot_L
18845 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18846 end OK_For_Limited_Init;
18847
18848 -------------------------------
18849 -- OK_For_Limited_Init_In_05 --
18850 -------------------------------
18851
18852 function OK_For_Limited_Init_In_05
18853 (Typ : Entity_Id;
18854 Exp : Node_Id) return Boolean
18855 is
18856 begin
18857 -- An object of a limited interface type can be initialized with any
18858 -- expression of a nonlimited descendant type. However this does not
18859 -- apply if this is a view conversion of some other expression. This
18860 -- is checked below.
18861
18862 if Is_Class_Wide_Type (Typ)
18863 and then Is_Limited_Interface (Typ)
18864 and then not Is_Limited_Type (Etype (Exp))
18865 and then Nkind (Exp) /= N_Type_Conversion
18866 then
18867 return True;
18868 end if;
18869
18870 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18871 -- case of limited aggregates (including extension aggregates), and
18872 -- function calls. The function call may have been given in prefixed
18873 -- notation, in which case the original node is an indexed component.
18874 -- If the function is parameterless, the original node was an explicit
18875 -- dereference. The function may also be parameterless, in which case
18876 -- the source node is just an identifier.
18877
18878 -- A branch of a conditional expression may have been removed if the
18879 -- condition is statically known. This happens during expansion, and
18880 -- thus will not happen if previous errors were encountered. The check
18881 -- will have been performed on the chosen branch, which replaces the
18882 -- original conditional expression.
18883
18884 if No (Exp) then
18885 return True;
18886 end if;
18887
18888 case Nkind (Original_Node (Exp)) is
18889 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18890 return True;
18891
18892 when N_Identifier =>
18893 return Present (Entity (Original_Node (Exp)))
18894 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18895
18896 when N_Qualified_Expression =>
18897 return
18898 OK_For_Limited_Init_In_05
18899 (Typ, Expression (Original_Node (Exp)));
18900
18901 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18902 -- with a function call, the expander has rewritten the call into an
18903 -- N_Type_Conversion node to force displacement of the pointer to
18904 -- reference the component containing the secondary dispatch table.
18905 -- Otherwise a type conversion is not a legal context.
18906 -- A return statement for a build-in-place function returning a
18907 -- synchronized type also introduces an unchecked conversion.
18908
18909 when N_Type_Conversion |
18910 N_Unchecked_Type_Conversion =>
18911 return not Comes_From_Source (Exp)
18912 and then
18913 OK_For_Limited_Init_In_05
18914 (Typ, Expression (Original_Node (Exp)));
18915
18916 when N_Indexed_Component |
18917 N_Selected_Component |
18918 N_Explicit_Dereference =>
18919 return Nkind (Exp) = N_Function_Call;
18920
18921 -- A use of 'Input is a function call, hence allowed. Normally the
18922 -- attribute will be changed to a call, but the attribute by itself
18923 -- can occur with -gnatc.
18924
18925 when N_Attribute_Reference =>
18926 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18927
18928 -- For a case expression, all dependent expressions must be legal
18929
18930 when N_Case_Expression =>
18931 declare
18932 Alt : Node_Id;
18933
18934 begin
18935 Alt := First (Alternatives (Original_Node (Exp)));
18936 while Present (Alt) loop
18937 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18938 return False;
18939 end if;
18940
18941 Next (Alt);
18942 end loop;
18943
18944 return True;
18945 end;
18946
18947 -- For an if expression, all dependent expressions must be legal
18948
18949 when N_If_Expression =>
18950 declare
18951 Then_Expr : constant Node_Id :=
18952 Next (First (Expressions (Original_Node (Exp))));
18953 Else_Expr : constant Node_Id := Next (Then_Expr);
18954 begin
18955 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18956 and then
18957 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18958 end;
18959
18960 when others =>
18961 return False;
18962 end case;
18963 end OK_For_Limited_Init_In_05;
18964
18965 -------------------------------------------
18966 -- Ordinary_Fixed_Point_Type_Declaration --
18967 -------------------------------------------
18968
18969 procedure Ordinary_Fixed_Point_Type_Declaration
18970 (T : Entity_Id;
18971 Def : Node_Id)
18972 is
18973 Loc : constant Source_Ptr := Sloc (Def);
18974 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18975 RRS : constant Node_Id := Real_Range_Specification (Def);
18976 Implicit_Base : Entity_Id;
18977 Delta_Val : Ureal;
18978 Small_Val : Ureal;
18979 Low_Val : Ureal;
18980 High_Val : Ureal;
18981
18982 begin
18983 Check_Restriction (No_Fixed_Point, Def);
18984
18985 -- Create implicit base type
18986
18987 Implicit_Base :=
18988 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18989 Set_Etype (Implicit_Base, Implicit_Base);
18990
18991 -- Analyze and process delta expression
18992
18993 Analyze_And_Resolve (Delta_Expr, Any_Real);
18994
18995 Check_Delta_Expression (Delta_Expr);
18996 Delta_Val := Expr_Value_R (Delta_Expr);
18997
18998 Set_Delta_Value (Implicit_Base, Delta_Val);
18999
19000 -- Compute default small from given delta, which is the largest power
19001 -- of two that does not exceed the given delta value.
19002
19003 declare
19004 Tmp : Ureal;
19005 Scale : Int;
19006
19007 begin
19008 Tmp := Ureal_1;
19009 Scale := 0;
19010
19011 if Delta_Val < Ureal_1 then
19012 while Delta_Val < Tmp loop
19013 Tmp := Tmp / Ureal_2;
19014 Scale := Scale + 1;
19015 end loop;
19016
19017 else
19018 loop
19019 Tmp := Tmp * Ureal_2;
19020 exit when Tmp > Delta_Val;
19021 Scale := Scale - 1;
19022 end loop;
19023 end if;
19024
19025 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19026 end;
19027
19028 Set_Small_Value (Implicit_Base, Small_Val);
19029
19030 -- If no range was given, set a dummy range
19031
19032 if RRS <= Empty_Or_Error then
19033 Low_Val := -Small_Val;
19034 High_Val := Small_Val;
19035
19036 -- Otherwise analyze and process given range
19037
19038 else
19039 declare
19040 Low : constant Node_Id := Low_Bound (RRS);
19041 High : constant Node_Id := High_Bound (RRS);
19042
19043 begin
19044 Analyze_And_Resolve (Low, Any_Real);
19045 Analyze_And_Resolve (High, Any_Real);
19046 Check_Real_Bound (Low);
19047 Check_Real_Bound (High);
19048
19049 -- Obtain and set the range
19050
19051 Low_Val := Expr_Value_R (Low);
19052 High_Val := Expr_Value_R (High);
19053
19054 if Low_Val > High_Val then
19055 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19056 end if;
19057 end;
19058 end if;
19059
19060 -- The range for both the implicit base and the declared first subtype
19061 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19062 -- set a temporary range in place. Note that the bounds of the base
19063 -- type will be widened to be symmetrical and to fill the available
19064 -- bits when the type is frozen.
19065
19066 -- We could do this with all discrete types, and probably should, but
19067 -- we absolutely have to do it for fixed-point, since the end-points
19068 -- of the range and the size are determined by the small value, which
19069 -- could be reset before the freeze point.
19070
19071 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19072 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19073
19074 -- Complete definition of first subtype. The inheritance of the rep item
19075 -- chain ensures that SPARK-related pragmas are not clobbered when the
19076 -- ordinary fixed point type acts as a full view of a private type.
19077
19078 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19079 Set_Etype (T, Implicit_Base);
19080 Init_Size_Align (T);
19081 Inherit_Rep_Item_Chain (T, Implicit_Base);
19082 Set_Small_Value (T, Small_Val);
19083 Set_Delta_Value (T, Delta_Val);
19084 Set_Is_Constrained (T);
19085 end Ordinary_Fixed_Point_Type_Declaration;
19086
19087 ----------------------------------
19088 -- Preanalyze_Assert_Expression --
19089 ----------------------------------
19090
19091 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19092 begin
19093 In_Assertion_Expr := In_Assertion_Expr + 1;
19094 Preanalyze_Spec_Expression (N, T);
19095 In_Assertion_Expr := In_Assertion_Expr - 1;
19096 end Preanalyze_Assert_Expression;
19097
19098 -----------------------------------
19099 -- Preanalyze_Default_Expression --
19100 -----------------------------------
19101
19102 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19103 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19104 begin
19105 In_Default_Expr := True;
19106 Preanalyze_Spec_Expression (N, T);
19107 In_Default_Expr := Save_In_Default_Expr;
19108 end Preanalyze_Default_Expression;
19109
19110 --------------------------------
19111 -- Preanalyze_Spec_Expression --
19112 --------------------------------
19113
19114 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19115 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19116 begin
19117 In_Spec_Expression := True;
19118 Preanalyze_And_Resolve (N, T);
19119 In_Spec_Expression := Save_In_Spec_Expression;
19120 end Preanalyze_Spec_Expression;
19121
19122 ----------------------------------------
19123 -- Prepare_Private_Subtype_Completion --
19124 ----------------------------------------
19125
19126 procedure Prepare_Private_Subtype_Completion
19127 (Id : Entity_Id;
19128 Related_Nod : Node_Id)
19129 is
19130 Id_B : constant Entity_Id := Base_Type (Id);
19131 Full_B : Entity_Id := Full_View (Id_B);
19132 Full : Entity_Id;
19133
19134 begin
19135 if Present (Full_B) then
19136
19137 -- Get to the underlying full view if necessary
19138
19139 if Is_Private_Type (Full_B)
19140 and then Present (Underlying_Full_View (Full_B))
19141 then
19142 Full_B := Underlying_Full_View (Full_B);
19143 end if;
19144
19145 -- The Base_Type is already completed, we can complete the subtype
19146 -- now. We have to create a new entity with the same name, Thus we
19147 -- can't use Create_Itype.
19148
19149 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19150 Set_Is_Itype (Full);
19151 Set_Associated_Node_For_Itype (Full, Related_Nod);
19152 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19153 end if;
19154
19155 -- The parent subtype may be private, but the base might not, in some
19156 -- nested instances. In that case, the subtype does not need to be
19157 -- exchanged. It would still be nice to make private subtypes and their
19158 -- bases consistent at all times ???
19159
19160 if Is_Private_Type (Id_B) then
19161 Append_Elmt (Id, Private_Dependents (Id_B));
19162 end if;
19163 end Prepare_Private_Subtype_Completion;
19164
19165 ---------------------------
19166 -- Process_Discriminants --
19167 ---------------------------
19168
19169 procedure Process_Discriminants
19170 (N : Node_Id;
19171 Prev : Entity_Id := Empty)
19172 is
19173 Elist : constant Elist_Id := New_Elmt_List;
19174 Id : Node_Id;
19175 Discr : Node_Id;
19176 Discr_Number : Uint;
19177 Discr_Type : Entity_Id;
19178 Default_Present : Boolean := False;
19179 Default_Not_Present : Boolean := False;
19180
19181 begin
19182 -- A composite type other than an array type can have discriminants.
19183 -- On entry, the current scope is the composite type.
19184
19185 -- The discriminants are initially entered into the scope of the type
19186 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19187 -- use, as explained at the end of this procedure.
19188
19189 Discr := First (Discriminant_Specifications (N));
19190 while Present (Discr) loop
19191 Enter_Name (Defining_Identifier (Discr));
19192
19193 -- For navigation purposes we add a reference to the discriminant
19194 -- in the entity for the type. If the current declaration is a
19195 -- completion, place references on the partial view. Otherwise the
19196 -- type is the current scope.
19197
19198 if Present (Prev) then
19199
19200 -- The references go on the partial view, if present. If the
19201 -- partial view has discriminants, the references have been
19202 -- generated already.
19203
19204 if not Has_Discriminants (Prev) then
19205 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19206 end if;
19207 else
19208 Generate_Reference
19209 (Current_Scope, Defining_Identifier (Discr), 'd');
19210 end if;
19211
19212 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19213 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19214
19215 -- Ada 2005 (AI-254)
19216
19217 if Present (Access_To_Subprogram_Definition
19218 (Discriminant_Type (Discr)))
19219 and then Protected_Present (Access_To_Subprogram_Definition
19220 (Discriminant_Type (Discr)))
19221 then
19222 Discr_Type :=
19223 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19224 end if;
19225
19226 else
19227 Find_Type (Discriminant_Type (Discr));
19228 Discr_Type := Etype (Discriminant_Type (Discr));
19229
19230 if Error_Posted (Discriminant_Type (Discr)) then
19231 Discr_Type := Any_Type;
19232 end if;
19233 end if;
19234
19235 -- Handling of discriminants that are access types
19236
19237 if Is_Access_Type (Discr_Type) then
19238
19239 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19240 -- limited record types
19241
19242 if Ada_Version < Ada_2005 then
19243 Check_Access_Discriminant_Requires_Limited
19244 (Discr, Discriminant_Type (Discr));
19245 end if;
19246
19247 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19248 Error_Msg_N
19249 ("(Ada 83) access discriminant not allowed", Discr);
19250 end if;
19251
19252 -- If not access type, must be a discrete type
19253
19254 elsif not Is_Discrete_Type (Discr_Type) then
19255 Error_Msg_N
19256 ("discriminants must have a discrete or access type",
19257 Discriminant_Type (Discr));
19258 end if;
19259
19260 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19261
19262 -- If a discriminant specification includes the assignment compound
19263 -- delimiter followed by an expression, the expression is the default
19264 -- expression of the discriminant; the default expression must be of
19265 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19266 -- a default expression, we do the special preanalysis, since this
19267 -- expression does not freeze (see section "Handling of Default and
19268 -- Per-Object Expressions" in spec of package Sem).
19269
19270 if Present (Expression (Discr)) then
19271 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19272
19273 -- Legaity checks
19274
19275 if Nkind (N) = N_Formal_Type_Declaration then
19276 Error_Msg_N
19277 ("discriminant defaults not allowed for formal type",
19278 Expression (Discr));
19279
19280 -- Flag an error for a tagged type with defaulted discriminants,
19281 -- excluding limited tagged types when compiling for Ada 2012
19282 -- (see AI05-0214).
19283
19284 elsif Is_Tagged_Type (Current_Scope)
19285 and then (not Is_Limited_Type (Current_Scope)
19286 or else Ada_Version < Ada_2012)
19287 and then Comes_From_Source (N)
19288 then
19289 -- Note: see similar test in Check_Or_Process_Discriminants, to
19290 -- handle the (illegal) case of the completion of an untagged
19291 -- view with discriminants with defaults by a tagged full view.
19292 -- We skip the check if Discr does not come from source, to
19293 -- account for the case of an untagged derived type providing
19294 -- defaults for a renamed discriminant from a private untagged
19295 -- ancestor with a tagged full view (ACATS B460006).
19296
19297 if Ada_Version >= Ada_2012 then
19298 Error_Msg_N
19299 ("discriminants of nonlimited tagged type cannot have"
19300 & " defaults",
19301 Expression (Discr));
19302 else
19303 Error_Msg_N
19304 ("discriminants of tagged type cannot have defaults",
19305 Expression (Discr));
19306 end if;
19307
19308 else
19309 Default_Present := True;
19310 Append_Elmt (Expression (Discr), Elist);
19311
19312 -- Tag the defining identifiers for the discriminants with
19313 -- their corresponding default expressions from the tree.
19314
19315 Set_Discriminant_Default_Value
19316 (Defining_Identifier (Discr), Expression (Discr));
19317 end if;
19318
19319 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19320 -- gets set unless we can be sure that no range check is required.
19321
19322 if (GNATprove_Mode or not Expander_Active)
19323 and then not
19324 Is_In_Range
19325 (Expression (Discr), Discr_Type, Assume_Valid => True)
19326 then
19327 Set_Do_Range_Check (Expression (Discr));
19328 end if;
19329
19330 -- No default discriminant value given
19331
19332 else
19333 Default_Not_Present := True;
19334 end if;
19335
19336 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19337 -- Discr_Type but with the null-exclusion attribute
19338
19339 if Ada_Version >= Ada_2005 then
19340
19341 -- Ada 2005 (AI-231): Static checks
19342
19343 if Can_Never_Be_Null (Discr_Type) then
19344 Null_Exclusion_Static_Checks (Discr);
19345
19346 elsif Is_Access_Type (Discr_Type)
19347 and then Null_Exclusion_Present (Discr)
19348
19349 -- No need to check itypes because in their case this check
19350 -- was done at their point of creation
19351
19352 and then not Is_Itype (Discr_Type)
19353 then
19354 if Can_Never_Be_Null (Discr_Type) then
19355 Error_Msg_NE
19356 ("`NOT NULL` not allowed (& already excludes null)",
19357 Discr,
19358 Discr_Type);
19359 end if;
19360
19361 Set_Etype (Defining_Identifier (Discr),
19362 Create_Null_Excluding_Itype
19363 (T => Discr_Type,
19364 Related_Nod => Discr));
19365
19366 -- Check for improper null exclusion if the type is otherwise
19367 -- legal for a discriminant.
19368
19369 elsif Null_Exclusion_Present (Discr)
19370 and then Is_Discrete_Type (Discr_Type)
19371 then
19372 Error_Msg_N
19373 ("null exclusion can only apply to an access type", Discr);
19374 end if;
19375
19376 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19377 -- can't have defaults. Synchronized types, or types that are
19378 -- explicitly limited are fine, but special tests apply to derived
19379 -- types in generics: in a generic body we have to assume the
19380 -- worst, and therefore defaults are not allowed if the parent is
19381 -- a generic formal private type (see ACATS B370001).
19382
19383 if Is_Access_Type (Discr_Type) and then Default_Present then
19384 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19385 or else Is_Limited_Record (Current_Scope)
19386 or else Is_Concurrent_Type (Current_Scope)
19387 or else Is_Concurrent_Record_Type (Current_Scope)
19388 or else Ekind (Current_Scope) = E_Limited_Private_Type
19389 then
19390 if not Is_Derived_Type (Current_Scope)
19391 or else not Is_Generic_Type (Etype (Current_Scope))
19392 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19393 or else Limited_Present
19394 (Type_Definition (Parent (Current_Scope)))
19395 then
19396 null;
19397
19398 else
19399 Error_Msg_N
19400 ("access discriminants of nonlimited types cannot "
19401 & "have defaults", Expression (Discr));
19402 end if;
19403
19404 elsif Present (Expression (Discr)) then
19405 Error_Msg_N
19406 ("(Ada 2005) access discriminants of nonlimited types "
19407 & "cannot have defaults", Expression (Discr));
19408 end if;
19409 end if;
19410 end if;
19411
19412 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19413 -- This check is relevant only when SPARK_Mode is on as it is not a
19414 -- standard Ada legality rule.
19415
19416 if SPARK_Mode = On
19417 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19418 then
19419 Error_Msg_N ("discriminant cannot be volatile", Discr);
19420 end if;
19421
19422 Next (Discr);
19423 end loop;
19424
19425 -- An element list consisting of the default expressions of the
19426 -- discriminants is constructed in the above loop and used to set
19427 -- the Discriminant_Constraint attribute for the type. If an object
19428 -- is declared of this (record or task) type without any explicit
19429 -- discriminant constraint given, this element list will form the
19430 -- actual parameters for the corresponding initialization procedure
19431 -- for the type.
19432
19433 Set_Discriminant_Constraint (Current_Scope, Elist);
19434 Set_Stored_Constraint (Current_Scope, No_Elist);
19435
19436 -- Default expressions must be provided either for all or for none
19437 -- of the discriminants of a discriminant part. (RM 3.7.1)
19438
19439 if Default_Present and then Default_Not_Present then
19440 Error_Msg_N
19441 ("incomplete specification of defaults for discriminants", N);
19442 end if;
19443
19444 -- The use of the name of a discriminant is not allowed in default
19445 -- expressions of a discriminant part if the specification of the
19446 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19447
19448 -- To detect this, the discriminant names are entered initially with an
19449 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19450 -- attempt to use a void entity (for example in an expression that is
19451 -- type-checked) produces the error message: premature usage. Now after
19452 -- completing the semantic analysis of the discriminant part, we can set
19453 -- the Ekind of all the discriminants appropriately.
19454
19455 Discr := First (Discriminant_Specifications (N));
19456 Discr_Number := Uint_1;
19457 while Present (Discr) loop
19458 Id := Defining_Identifier (Discr);
19459 Set_Ekind (Id, E_Discriminant);
19460 Init_Component_Location (Id);
19461 Init_Esize (Id);
19462 Set_Discriminant_Number (Id, Discr_Number);
19463
19464 -- Make sure this is always set, even in illegal programs
19465
19466 Set_Corresponding_Discriminant (Id, Empty);
19467
19468 -- Initialize the Original_Record_Component to the entity itself.
19469 -- Inherit_Components will propagate the right value to
19470 -- discriminants in derived record types.
19471
19472 Set_Original_Record_Component (Id, Id);
19473
19474 -- Create the discriminal for the discriminant
19475
19476 Build_Discriminal (Id);
19477
19478 Next (Discr);
19479 Discr_Number := Discr_Number + 1;
19480 end loop;
19481
19482 Set_Has_Discriminants (Current_Scope);
19483 end Process_Discriminants;
19484
19485 -----------------------
19486 -- Process_Full_View --
19487 -----------------------
19488
19489 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19490 procedure Collect_Implemented_Interfaces
19491 (Typ : Entity_Id;
19492 Ifaces : Elist_Id);
19493 -- Ada 2005: Gather all the interfaces that Typ directly or
19494 -- inherently implements. Duplicate entries are not added to
19495 -- the list Ifaces.
19496
19497 ------------------------------------
19498 -- Collect_Implemented_Interfaces --
19499 ------------------------------------
19500
19501 procedure Collect_Implemented_Interfaces
19502 (Typ : Entity_Id;
19503 Ifaces : Elist_Id)
19504 is
19505 Iface : Entity_Id;
19506 Iface_Elmt : Elmt_Id;
19507
19508 begin
19509 -- Abstract interfaces are only associated with tagged record types
19510
19511 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19512 return;
19513 end if;
19514
19515 -- Recursively climb to the ancestors
19516
19517 if Etype (Typ) /= Typ
19518
19519 -- Protect the frontend against wrong cyclic declarations like:
19520
19521 -- type B is new A with private;
19522 -- type C is new A with private;
19523 -- private
19524 -- type B is new C with null record;
19525 -- type C is new B with null record;
19526
19527 and then Etype (Typ) /= Priv_T
19528 and then Etype (Typ) /= Full_T
19529 then
19530 -- Keep separate the management of private type declarations
19531
19532 if Ekind (Typ) = E_Record_Type_With_Private then
19533
19534 -- Handle the following illegal usage:
19535 -- type Private_Type is tagged private;
19536 -- private
19537 -- type Private_Type is new Type_Implementing_Iface;
19538
19539 if Present (Full_View (Typ))
19540 and then Etype (Typ) /= Full_View (Typ)
19541 then
19542 if Is_Interface (Etype (Typ)) then
19543 Append_Unique_Elmt (Etype (Typ), Ifaces);
19544 end if;
19545
19546 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19547 end if;
19548
19549 -- Non-private types
19550
19551 else
19552 if Is_Interface (Etype (Typ)) then
19553 Append_Unique_Elmt (Etype (Typ), Ifaces);
19554 end if;
19555
19556 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19557 end if;
19558 end if;
19559
19560 -- Handle entities in the list of abstract interfaces
19561
19562 if Present (Interfaces (Typ)) then
19563 Iface_Elmt := First_Elmt (Interfaces (Typ));
19564 while Present (Iface_Elmt) loop
19565 Iface := Node (Iface_Elmt);
19566
19567 pragma Assert (Is_Interface (Iface));
19568
19569 if not Contain_Interface (Iface, Ifaces) then
19570 Append_Elmt (Iface, Ifaces);
19571 Collect_Implemented_Interfaces (Iface, Ifaces);
19572 end if;
19573
19574 Next_Elmt (Iface_Elmt);
19575 end loop;
19576 end if;
19577 end Collect_Implemented_Interfaces;
19578
19579 -- Local variables
19580
19581 Full_Indic : Node_Id;
19582 Full_Parent : Entity_Id;
19583 Priv_Parent : Entity_Id;
19584
19585 -- Start of processing for Process_Full_View
19586
19587 begin
19588 -- First some sanity checks that must be done after semantic
19589 -- decoration of the full view and thus cannot be placed with other
19590 -- similar checks in Find_Type_Name
19591
19592 if not Is_Limited_Type (Priv_T)
19593 and then (Is_Limited_Type (Full_T)
19594 or else Is_Limited_Composite (Full_T))
19595 then
19596 if In_Instance then
19597 null;
19598 else
19599 Error_Msg_N
19600 ("completion of nonlimited type cannot be limited", Full_T);
19601 Explain_Limited_Type (Full_T, Full_T);
19602 end if;
19603
19604 elsif Is_Abstract_Type (Full_T)
19605 and then not Is_Abstract_Type (Priv_T)
19606 then
19607 Error_Msg_N
19608 ("completion of nonabstract type cannot be abstract", Full_T);
19609
19610 elsif Is_Tagged_Type (Priv_T)
19611 and then Is_Limited_Type (Priv_T)
19612 and then not Is_Limited_Type (Full_T)
19613 then
19614 -- If pragma CPP_Class was applied to the private declaration
19615 -- propagate the limitedness to the full-view
19616
19617 if Is_CPP_Class (Priv_T) then
19618 Set_Is_Limited_Record (Full_T);
19619
19620 -- GNAT allow its own definition of Limited_Controlled to disobey
19621 -- this rule in order in ease the implementation. This test is safe
19622 -- because Root_Controlled is defined in a child of System that
19623 -- normal programs are not supposed to use.
19624
19625 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19626 Set_Is_Limited_Composite (Full_T);
19627 else
19628 Error_Msg_N
19629 ("completion of limited tagged type must be limited", Full_T);
19630 end if;
19631
19632 elsif Is_Generic_Type (Priv_T) then
19633 Error_Msg_N ("generic type cannot have a completion", Full_T);
19634 end if;
19635
19636 -- Check that ancestor interfaces of private and full views are
19637 -- consistent. We omit this check for synchronized types because
19638 -- they are performed on the corresponding record type when frozen.
19639
19640 if Ada_Version >= Ada_2005
19641 and then Is_Tagged_Type (Priv_T)
19642 and then Is_Tagged_Type (Full_T)
19643 and then not Is_Concurrent_Type (Full_T)
19644 then
19645 declare
19646 Iface : Entity_Id;
19647 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19648 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19649
19650 begin
19651 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19652 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19653
19654 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19655 -- an interface type if and only if the full type is descendant
19656 -- of the interface type (AARM 7.3 (7.3/2)).
19657
19658 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19659
19660 if Present (Iface) then
19661 Error_Msg_NE
19662 ("interface in partial view& not implemented by full type "
19663 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19664 end if;
19665
19666 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19667
19668 if Present (Iface) then
19669 Error_Msg_NE
19670 ("interface & not implemented by partial view "
19671 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19672 end if;
19673 end;
19674 end if;
19675
19676 if Is_Tagged_Type (Priv_T)
19677 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19678 and then Is_Derived_Type (Full_T)
19679 then
19680 Priv_Parent := Etype (Priv_T);
19681
19682 -- The full view of a private extension may have been transformed
19683 -- into an unconstrained derived type declaration and a subtype
19684 -- declaration (see build_derived_record_type for details).
19685
19686 if Nkind (N) = N_Subtype_Declaration then
19687 Full_Indic := Subtype_Indication (N);
19688 Full_Parent := Etype (Base_Type (Full_T));
19689 else
19690 Full_Indic := Subtype_Indication (Type_Definition (N));
19691 Full_Parent := Etype (Full_T);
19692 end if;
19693
19694 -- Check that the parent type of the full type is a descendant of
19695 -- the ancestor subtype given in the private extension. If either
19696 -- entity has an Etype equal to Any_Type then we had some previous
19697 -- error situation [7.3(8)].
19698
19699 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19700 return;
19701
19702 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19703 -- any order. Therefore we don't have to check that its parent must
19704 -- be a descendant of the parent of the private type declaration.
19705
19706 elsif Is_Interface (Priv_Parent)
19707 and then Is_Interface (Full_Parent)
19708 then
19709 null;
19710
19711 -- Ada 2005 (AI-251): If the parent of the private type declaration
19712 -- is an interface there is no need to check that it is an ancestor
19713 -- of the associated full type declaration. The required tests for
19714 -- this case are performed by Build_Derived_Record_Type.
19715
19716 elsif not Is_Interface (Base_Type (Priv_Parent))
19717 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19718 then
19719 Error_Msg_N
19720 ("parent of full type must descend from parent of private "
19721 & "extension", Full_Indic);
19722
19723 -- First check a formal restriction, and then proceed with checking
19724 -- Ada rules. Since the formal restriction is not a serious error, we
19725 -- don't prevent further error detection for this check, hence the
19726 -- ELSE.
19727
19728 else
19729 -- In formal mode, when completing a private extension the type
19730 -- named in the private part must be exactly the same as that
19731 -- named in the visible part.
19732
19733 if Priv_Parent /= Full_Parent then
19734 Error_Msg_Name_1 := Chars (Priv_Parent);
19735 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19736 end if;
19737
19738 -- Check the rules of 7.3(10): if the private extension inherits
19739 -- known discriminants, then the full type must also inherit those
19740 -- discriminants from the same (ancestor) type, and the parent
19741 -- subtype of the full type must be constrained if and only if
19742 -- the ancestor subtype of the private extension is constrained.
19743
19744 if No (Discriminant_Specifications (Parent (Priv_T)))
19745 and then not Has_Unknown_Discriminants (Priv_T)
19746 and then Has_Discriminants (Base_Type (Priv_Parent))
19747 then
19748 declare
19749 Priv_Indic : constant Node_Id :=
19750 Subtype_Indication (Parent (Priv_T));
19751
19752 Priv_Constr : constant Boolean :=
19753 Is_Constrained (Priv_Parent)
19754 or else
19755 Nkind (Priv_Indic) = N_Subtype_Indication
19756 or else
19757 Is_Constrained (Entity (Priv_Indic));
19758
19759 Full_Constr : constant Boolean :=
19760 Is_Constrained (Full_Parent)
19761 or else
19762 Nkind (Full_Indic) = N_Subtype_Indication
19763 or else
19764 Is_Constrained (Entity (Full_Indic));
19765
19766 Priv_Discr : Entity_Id;
19767 Full_Discr : Entity_Id;
19768
19769 begin
19770 Priv_Discr := First_Discriminant (Priv_Parent);
19771 Full_Discr := First_Discriminant (Full_Parent);
19772 while Present (Priv_Discr) and then Present (Full_Discr) loop
19773 if Original_Record_Component (Priv_Discr) =
19774 Original_Record_Component (Full_Discr)
19775 or else
19776 Corresponding_Discriminant (Priv_Discr) =
19777 Corresponding_Discriminant (Full_Discr)
19778 then
19779 null;
19780 else
19781 exit;
19782 end if;
19783
19784 Next_Discriminant (Priv_Discr);
19785 Next_Discriminant (Full_Discr);
19786 end loop;
19787
19788 if Present (Priv_Discr) or else Present (Full_Discr) then
19789 Error_Msg_N
19790 ("full view must inherit discriminants of the parent "
19791 & "type used in the private extension", Full_Indic);
19792
19793 elsif Priv_Constr and then not Full_Constr then
19794 Error_Msg_N
19795 ("parent subtype of full type must be constrained",
19796 Full_Indic);
19797
19798 elsif Full_Constr and then not Priv_Constr then
19799 Error_Msg_N
19800 ("parent subtype of full type must be unconstrained",
19801 Full_Indic);
19802 end if;
19803 end;
19804
19805 -- Check the rules of 7.3(12): if a partial view has neither
19806 -- known or unknown discriminants, then the full type
19807 -- declaration shall define a definite subtype.
19808
19809 elsif not Has_Unknown_Discriminants (Priv_T)
19810 and then not Has_Discriminants (Priv_T)
19811 and then not Is_Constrained (Full_T)
19812 then
19813 Error_Msg_N
19814 ("full view must define a constrained type if partial view "
19815 & "has no discriminants", Full_T);
19816 end if;
19817
19818 -- ??????? Do we implement the following properly ?????
19819 -- If the ancestor subtype of a private extension has constrained
19820 -- discriminants, then the parent subtype of the full view shall
19821 -- impose a statically matching constraint on those discriminants
19822 -- [7.3(13)].
19823 end if;
19824
19825 else
19826 -- For untagged types, verify that a type without discriminants is
19827 -- not completed with an unconstrained type. A separate error message
19828 -- is produced if the full type has defaulted discriminants.
19829
19830 if Is_Definite_Subtype (Priv_T)
19831 and then not Is_Definite_Subtype (Full_T)
19832 then
19833 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19834 Error_Msg_NE
19835 ("full view of& not compatible with declaration#",
19836 Full_T, Priv_T);
19837
19838 if not Is_Tagged_Type (Full_T) then
19839 Error_Msg_N
19840 ("\one is constrained, the other unconstrained", Full_T);
19841 end if;
19842 end if;
19843 end if;
19844
19845 -- AI-419: verify that the use of "limited" is consistent
19846
19847 declare
19848 Orig_Decl : constant Node_Id := Original_Node (N);
19849
19850 begin
19851 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19852 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19853 and then Nkind
19854 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19855 then
19856 if not Limited_Present (Parent (Priv_T))
19857 and then not Synchronized_Present (Parent (Priv_T))
19858 and then Limited_Present (Type_Definition (Orig_Decl))
19859 then
19860 Error_Msg_N
19861 ("full view of non-limited extension cannot be limited", N);
19862
19863 -- Conversely, if the partial view carries the limited keyword,
19864 -- the full view must as well, even if it may be redundant.
19865
19866 elsif Limited_Present (Parent (Priv_T))
19867 and then not Limited_Present (Type_Definition (Orig_Decl))
19868 then
19869 Error_Msg_N
19870 ("full view of limited extension must be explicitly limited",
19871 N);
19872 end if;
19873 end if;
19874 end;
19875
19876 -- Ada 2005 (AI-443): A synchronized private extension must be
19877 -- completed by a task or protected type.
19878
19879 if Ada_Version >= Ada_2005
19880 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19881 and then Synchronized_Present (Parent (Priv_T))
19882 and then not Is_Concurrent_Type (Full_T)
19883 then
19884 Error_Msg_N ("full view of synchronized extension must " &
19885 "be synchronized type", N);
19886 end if;
19887
19888 -- Ada 2005 AI-363: if the full view has discriminants with
19889 -- defaults, it is illegal to declare constrained access subtypes
19890 -- whose designated type is the current type. This allows objects
19891 -- of the type that are declared in the heap to be unconstrained.
19892
19893 if not Has_Unknown_Discriminants (Priv_T)
19894 and then not Has_Discriminants (Priv_T)
19895 and then Has_Discriminants (Full_T)
19896 and then
19897 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19898 then
19899 Set_Has_Constrained_Partial_View (Full_T);
19900 Set_Has_Constrained_Partial_View (Priv_T);
19901 end if;
19902
19903 -- Create a full declaration for all its subtypes recorded in
19904 -- Private_Dependents and swap them similarly to the base type. These
19905 -- are subtypes that have been define before the full declaration of
19906 -- the private type. We also swap the entry in Private_Dependents list
19907 -- so we can properly restore the private view on exit from the scope.
19908
19909 declare
19910 Priv_Elmt : Elmt_Id;
19911 Priv_Scop : Entity_Id;
19912 Priv : Entity_Id;
19913 Full : Entity_Id;
19914
19915 begin
19916 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19917 while Present (Priv_Elmt) loop
19918 Priv := Node (Priv_Elmt);
19919 Priv_Scop := Scope (Priv);
19920
19921 if Ekind_In (Priv, E_Private_Subtype,
19922 E_Limited_Private_Subtype,
19923 E_Record_Subtype_With_Private)
19924 then
19925 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19926 Set_Is_Itype (Full);
19927 Set_Parent (Full, Parent (Priv));
19928 Set_Associated_Node_For_Itype (Full, N);
19929
19930 -- Now we need to complete the private subtype, but since the
19931 -- base type has already been swapped, we must also swap the
19932 -- subtypes (and thus, reverse the arguments in the call to
19933 -- Complete_Private_Subtype). Also note that we may need to
19934 -- re-establish the scope of the private subtype.
19935
19936 Copy_And_Swap (Priv, Full);
19937
19938 if not In_Open_Scopes (Priv_Scop) then
19939 Push_Scope (Priv_Scop);
19940
19941 else
19942 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19943
19944 Priv_Scop := Empty;
19945 end if;
19946
19947 Complete_Private_Subtype (Full, Priv, Full_T, N);
19948
19949 if Present (Priv_Scop) then
19950 Pop_Scope;
19951 end if;
19952
19953 Replace_Elmt (Priv_Elmt, Full);
19954 end if;
19955
19956 Next_Elmt (Priv_Elmt);
19957 end loop;
19958 end;
19959
19960 -- If the private view was tagged, copy the new primitive operations
19961 -- from the private view to the full view.
19962
19963 if Is_Tagged_Type (Full_T) then
19964 declare
19965 Disp_Typ : Entity_Id;
19966 Full_List : Elist_Id;
19967 Prim : Entity_Id;
19968 Prim_Elmt : Elmt_Id;
19969 Priv_List : Elist_Id;
19970
19971 function Contains
19972 (E : Entity_Id;
19973 L : Elist_Id) return Boolean;
19974 -- Determine whether list L contains element E
19975
19976 --------------
19977 -- Contains --
19978 --------------
19979
19980 function Contains
19981 (E : Entity_Id;
19982 L : Elist_Id) return Boolean
19983 is
19984 List_Elmt : Elmt_Id;
19985
19986 begin
19987 List_Elmt := First_Elmt (L);
19988 while Present (List_Elmt) loop
19989 if Node (List_Elmt) = E then
19990 return True;
19991 end if;
19992
19993 Next_Elmt (List_Elmt);
19994 end loop;
19995
19996 return False;
19997 end Contains;
19998
19999 -- Start of processing
20000
20001 begin
20002 if Is_Tagged_Type (Priv_T) then
20003 Priv_List := Primitive_Operations (Priv_T);
20004 Prim_Elmt := First_Elmt (Priv_List);
20005
20006 -- In the case of a concurrent type completing a private tagged
20007 -- type, primitives may have been declared in between the two
20008 -- views. These subprograms need to be wrapped the same way
20009 -- entries and protected procedures are handled because they
20010 -- cannot be directly shared by the two views.
20011
20012 if Is_Concurrent_Type (Full_T) then
20013 declare
20014 Conc_Typ : constant Entity_Id :=
20015 Corresponding_Record_Type (Full_T);
20016 Curr_Nod : Node_Id := Parent (Conc_Typ);
20017 Wrap_Spec : Node_Id;
20018
20019 begin
20020 while Present (Prim_Elmt) loop
20021 Prim := Node (Prim_Elmt);
20022
20023 if Comes_From_Source (Prim)
20024 and then not Is_Abstract_Subprogram (Prim)
20025 then
20026 Wrap_Spec :=
20027 Make_Subprogram_Declaration (Sloc (Prim),
20028 Specification =>
20029 Build_Wrapper_Spec
20030 (Subp_Id => Prim,
20031 Obj_Typ => Conc_Typ,
20032 Formals =>
20033 Parameter_Specifications
20034 (Parent (Prim))));
20035
20036 Insert_After (Curr_Nod, Wrap_Spec);
20037 Curr_Nod := Wrap_Spec;
20038
20039 Analyze (Wrap_Spec);
20040
20041 -- Remove the wrapper from visibility to avoid
20042 -- spurious conflict with the wrapped entity.
20043
20044 Set_Is_Immediately_Visible
20045 (Defining_Entity (Specification (Wrap_Spec)),
20046 False);
20047 end if;
20048
20049 Next_Elmt (Prim_Elmt);
20050 end loop;
20051
20052 return;
20053 end;
20054
20055 -- For non-concurrent types, transfer explicit primitives, but
20056 -- omit those inherited from the parent of the private view
20057 -- since they will be re-inherited later on.
20058
20059 else
20060 Full_List := Primitive_Operations (Full_T);
20061
20062 while Present (Prim_Elmt) loop
20063 Prim := Node (Prim_Elmt);
20064
20065 if Comes_From_Source (Prim)
20066 and then not Contains (Prim, Full_List)
20067 then
20068 Append_Elmt (Prim, Full_List);
20069 end if;
20070
20071 Next_Elmt (Prim_Elmt);
20072 end loop;
20073 end if;
20074
20075 -- Untagged private view
20076
20077 else
20078 Full_List := Primitive_Operations (Full_T);
20079
20080 -- In this case the partial view is untagged, so here we locate
20081 -- all of the earlier primitives that need to be treated as
20082 -- dispatching (those that appear between the two views). Note
20083 -- that these additional operations must all be new operations
20084 -- (any earlier operations that override inherited operations
20085 -- of the full view will already have been inserted in the
20086 -- primitives list, marked by Check_Operation_From_Private_View
20087 -- as dispatching. Note that implicit "/=" operators are
20088 -- excluded from being added to the primitives list since they
20089 -- shouldn't be treated as dispatching (tagged "/=" is handled
20090 -- specially).
20091
20092 Prim := Next_Entity (Full_T);
20093 while Present (Prim) and then Prim /= Priv_T loop
20094 if Ekind_In (Prim, E_Procedure, E_Function) then
20095 Disp_Typ := Find_Dispatching_Type (Prim);
20096
20097 if Disp_Typ = Full_T
20098 and then (Chars (Prim) /= Name_Op_Ne
20099 or else Comes_From_Source (Prim))
20100 then
20101 Check_Controlling_Formals (Full_T, Prim);
20102
20103 if not Is_Dispatching_Operation (Prim) then
20104 Append_Elmt (Prim, Full_List);
20105 Set_Is_Dispatching_Operation (Prim, True);
20106 Set_DT_Position_Value (Prim, No_Uint);
20107 end if;
20108
20109 elsif Is_Dispatching_Operation (Prim)
20110 and then Disp_Typ /= Full_T
20111 then
20112
20113 -- Verify that it is not otherwise controlled by a
20114 -- formal or a return value of type T.
20115
20116 Check_Controlling_Formals (Disp_Typ, Prim);
20117 end if;
20118 end if;
20119
20120 Next_Entity (Prim);
20121 end loop;
20122 end if;
20123
20124 -- For the tagged case, the two views can share the same primitive
20125 -- operations list and the same class-wide type. Update attributes
20126 -- of the class-wide type which depend on the full declaration.
20127
20128 if Is_Tagged_Type (Priv_T) then
20129 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20130 Set_Class_Wide_Type
20131 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20132
20133 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20134 end if;
20135 end;
20136 end if;
20137
20138 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20139
20140 if Known_To_Have_Preelab_Init (Priv_T) then
20141
20142 -- Case where there is a pragma Preelaborable_Initialization. We
20143 -- always allow this in predefined units, which is cheating a bit,
20144 -- but it means we don't have to struggle to meet the requirements in
20145 -- the RM for having Preelaborable Initialization. Otherwise we
20146 -- require that the type meets the RM rules. But we can't check that
20147 -- yet, because of the rule about overriding Initialize, so we simply
20148 -- set a flag that will be checked at freeze time.
20149
20150 if not In_Predefined_Unit (Full_T) then
20151 Set_Must_Have_Preelab_Init (Full_T);
20152 end if;
20153 end if;
20154
20155 -- If pragma CPP_Class was applied to the private type declaration,
20156 -- propagate it now to the full type declaration.
20157
20158 if Is_CPP_Class (Priv_T) then
20159 Set_Is_CPP_Class (Full_T);
20160 Set_Convention (Full_T, Convention_CPP);
20161
20162 -- Check that components of imported CPP types do not have default
20163 -- expressions.
20164
20165 Check_CPP_Type_Has_No_Defaults (Full_T);
20166 end if;
20167
20168 -- If the private view has user specified stream attributes, then so has
20169 -- the full view.
20170
20171 -- Why the test, how could these flags be already set in Full_T ???
20172
20173 if Has_Specified_Stream_Read (Priv_T) then
20174 Set_Has_Specified_Stream_Read (Full_T);
20175 end if;
20176
20177 if Has_Specified_Stream_Write (Priv_T) then
20178 Set_Has_Specified_Stream_Write (Full_T);
20179 end if;
20180
20181 if Has_Specified_Stream_Input (Priv_T) then
20182 Set_Has_Specified_Stream_Input (Full_T);
20183 end if;
20184
20185 if Has_Specified_Stream_Output (Priv_T) then
20186 Set_Has_Specified_Stream_Output (Full_T);
20187 end if;
20188
20189 if Is_Ghost_Entity (Priv_T) then
20190
20191 -- The Ghost policy in effect at the point of declaration and at the
20192 -- point of completion must match (SPARK RM 6.9(14)).
20193
20194 Check_Ghost_Completion (Priv_T, Full_T);
20195
20196 -- Propagate the attributes related to pragma Ghost from the private
20197 -- to the full view.
20198
20199 Mark_Full_View_As_Ghost (Priv_T, Full_T);
20200 end if;
20201
20202 -- Propagate Default_Initial_Condition-related attributes from the
20203 -- partial view to the full view and its base type.
20204
20205 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20206 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20207
20208 -- Propagate invariant-related attributes from the partial view to the
20209 -- full view and its base type.
20210
20211 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20212 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20213
20214 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20215 -- in the full view without advertising the inheritance in the partial
20216 -- view. This can only occur when the partial view has no parent type
20217 -- and the full view has an interface as a parent. Any other scenarios
20218 -- are illegal because implemented interfaces must match between the
20219 -- two views.
20220
20221 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20222 declare
20223 Full_Par : constant Entity_Id := Etype (Full_T);
20224 Priv_Par : constant Entity_Id := Etype (Priv_T);
20225
20226 begin
20227 if not Is_Interface (Priv_Par)
20228 and then Is_Interface (Full_Par)
20229 and then Has_Inheritable_Invariants (Full_Par)
20230 then
20231 Error_Msg_N
20232 ("hidden inheritance of class-wide type invariants not "
20233 & "allowed", N);
20234 end if;
20235 end;
20236 end if;
20237
20238 -- Propagate predicates to full type, and predicate function if already
20239 -- defined. It is not clear that this can actually happen? the partial
20240 -- view cannot be frozen yet, and the predicate function has not been
20241 -- built. Still it is a cheap check and seems safer to make it.
20242
20243 if Has_Predicates (Priv_T) then
20244 Set_Has_Predicates (Full_T);
20245
20246 if Present (Predicate_Function (Priv_T)) then
20247 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20248 end if;
20249 end if;
20250 end Process_Full_View;
20251
20252 -----------------------------------
20253 -- Process_Incomplete_Dependents --
20254 -----------------------------------
20255
20256 procedure Process_Incomplete_Dependents
20257 (N : Node_Id;
20258 Full_T : Entity_Id;
20259 Inc_T : Entity_Id)
20260 is
20261 Inc_Elmt : Elmt_Id;
20262 Priv_Dep : Entity_Id;
20263 New_Subt : Entity_Id;
20264
20265 Disc_Constraint : Elist_Id;
20266
20267 begin
20268 if No (Private_Dependents (Inc_T)) then
20269 return;
20270 end if;
20271
20272 -- Itypes that may be generated by the completion of an incomplete
20273 -- subtype are not used by the back-end and not attached to the tree.
20274 -- They are created only for constraint-checking purposes.
20275
20276 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20277 while Present (Inc_Elmt) loop
20278 Priv_Dep := Node (Inc_Elmt);
20279
20280 if Ekind (Priv_Dep) = E_Subprogram_Type then
20281
20282 -- An Access_To_Subprogram type may have a return type or a
20283 -- parameter type that is incomplete. Replace with the full view.
20284
20285 if Etype (Priv_Dep) = Inc_T then
20286 Set_Etype (Priv_Dep, Full_T);
20287 end if;
20288
20289 declare
20290 Formal : Entity_Id;
20291
20292 begin
20293 Formal := First_Formal (Priv_Dep);
20294 while Present (Formal) loop
20295 if Etype (Formal) = Inc_T then
20296 Set_Etype (Formal, Full_T);
20297 end if;
20298
20299 Next_Formal (Formal);
20300 end loop;
20301 end;
20302
20303 elsif Is_Overloadable (Priv_Dep) then
20304
20305 -- If a subprogram in the incomplete dependents list is primitive
20306 -- for a tagged full type then mark it as a dispatching operation,
20307 -- check whether it overrides an inherited subprogram, and check
20308 -- restrictions on its controlling formals. Note that a protected
20309 -- operation is never dispatching: only its wrapper operation
20310 -- (which has convention Ada) is.
20311
20312 if Is_Tagged_Type (Full_T)
20313 and then Is_Primitive (Priv_Dep)
20314 and then Convention (Priv_Dep) /= Convention_Protected
20315 then
20316 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20317 Set_Is_Dispatching_Operation (Priv_Dep);
20318 Check_Controlling_Formals (Full_T, Priv_Dep);
20319 end if;
20320
20321 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20322
20323 -- Can happen during processing of a body before the completion
20324 -- of a TA type. Ignore, because spec is also on dependent list.
20325
20326 return;
20327
20328 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20329 -- corresponding subtype of the full view.
20330
20331 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20332 Set_Subtype_Indication
20333 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20334 Set_Etype (Priv_Dep, Full_T);
20335 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20336 Set_Analyzed (Parent (Priv_Dep), False);
20337
20338 -- Reanalyze the declaration, suppressing the call to
20339 -- Enter_Name to avoid duplicate names.
20340
20341 Analyze_Subtype_Declaration
20342 (N => Parent (Priv_Dep),
20343 Skip => True);
20344
20345 -- Dependent is a subtype
20346
20347 else
20348 -- We build a new subtype indication using the full view of the
20349 -- incomplete parent. The discriminant constraints have been
20350 -- elaborated already at the point of the subtype declaration.
20351
20352 New_Subt := Create_Itype (E_Void, N);
20353
20354 if Has_Discriminants (Full_T) then
20355 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20356 else
20357 Disc_Constraint := No_Elist;
20358 end if;
20359
20360 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20361 Set_Full_View (Priv_Dep, New_Subt);
20362 end if;
20363
20364 Next_Elmt (Inc_Elmt);
20365 end loop;
20366 end Process_Incomplete_Dependents;
20367
20368 --------------------------------
20369 -- Process_Range_Expr_In_Decl --
20370 --------------------------------
20371
20372 procedure Process_Range_Expr_In_Decl
20373 (R : Node_Id;
20374 T : Entity_Id;
20375 Subtyp : Entity_Id := Empty;
20376 Check_List : List_Id := Empty_List;
20377 R_Check_Off : Boolean := False;
20378 In_Iter_Schm : Boolean := False)
20379 is
20380 Lo, Hi : Node_Id;
20381 R_Checks : Check_Result;
20382 Insert_Node : Node_Id;
20383 Def_Id : Entity_Id;
20384
20385 begin
20386 Analyze_And_Resolve (R, Base_Type (T));
20387
20388 if Nkind (R) = N_Range then
20389
20390 -- In SPARK, all ranges should be static, with the exception of the
20391 -- discrete type definition of a loop parameter specification.
20392
20393 if not In_Iter_Schm
20394 and then not Is_OK_Static_Range (R)
20395 then
20396 Check_SPARK_05_Restriction ("range should be static", R);
20397 end if;
20398
20399 Lo := Low_Bound (R);
20400 Hi := High_Bound (R);
20401
20402 -- Validity checks on the range of a quantified expression are
20403 -- delayed until the construct is transformed into a loop.
20404
20405 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20406 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20407 then
20408 null;
20409
20410 -- We need to ensure validity of the bounds here, because if we
20411 -- go ahead and do the expansion, then the expanded code will get
20412 -- analyzed with range checks suppressed and we miss the check.
20413
20414 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20415 -- the temporaries generated by routine Remove_Side_Effects by means
20416 -- of validity checks must use the same names. When a range appears
20417 -- in the parent of a generic, the range is processed with checks
20418 -- disabled as part of the generic context and with checks enabled
20419 -- for code generation purposes. This leads to link issues as the
20420 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20421 -- template sees the temporaries generated by Remove_Side_Effects.
20422
20423 else
20424 Validity_Check_Range (R, Subtyp);
20425 end if;
20426
20427 -- If there were errors in the declaration, try and patch up some
20428 -- common mistakes in the bounds. The cases handled are literals
20429 -- which are Integer where the expected type is Real and vice versa.
20430 -- These corrections allow the compilation process to proceed further
20431 -- along since some basic assumptions of the format of the bounds
20432 -- are guaranteed.
20433
20434 if Etype (R) = Any_Type then
20435 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20436 Rewrite (Lo,
20437 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20438
20439 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20440 Rewrite (Hi,
20441 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20442
20443 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20444 Rewrite (Lo,
20445 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20446
20447 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20448 Rewrite (Hi,
20449 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20450 end if;
20451
20452 Set_Etype (Lo, T);
20453 Set_Etype (Hi, T);
20454 end if;
20455
20456 -- If the bounds of the range have been mistakenly given as string
20457 -- literals (perhaps in place of character literals), then an error
20458 -- has already been reported, but we rewrite the string literal as a
20459 -- bound of the range's type to avoid blowups in later processing
20460 -- that looks at static values.
20461
20462 if Nkind (Lo) = N_String_Literal then
20463 Rewrite (Lo,
20464 Make_Attribute_Reference (Sloc (Lo),
20465 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20466 Attribute_Name => Name_First));
20467 Analyze_And_Resolve (Lo);
20468 end if;
20469
20470 if Nkind (Hi) = N_String_Literal then
20471 Rewrite (Hi,
20472 Make_Attribute_Reference (Sloc (Hi),
20473 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20474 Attribute_Name => Name_First));
20475 Analyze_And_Resolve (Hi);
20476 end if;
20477
20478 -- If bounds aren't scalar at this point then exit, avoiding
20479 -- problems with further processing of the range in this procedure.
20480
20481 if not Is_Scalar_Type (Etype (Lo)) then
20482 return;
20483 end if;
20484
20485 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20486 -- then range of the base type. Here we check whether the bounds
20487 -- are in the range of the subtype itself. Note that if the bounds
20488 -- represent the null range the Constraint_Error exception should
20489 -- not be raised.
20490
20491 -- ??? The following code should be cleaned up as follows
20492
20493 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20494 -- is done in the call to Range_Check (R, T); below
20495
20496 -- 2. The use of R_Check_Off should be investigated and possibly
20497 -- removed, this would clean up things a bit.
20498
20499 if Is_Null_Range (Lo, Hi) then
20500 null;
20501
20502 else
20503 -- Capture values of bounds and generate temporaries for them
20504 -- if needed, before applying checks, since checks may cause
20505 -- duplication of the expression without forcing evaluation.
20506
20507 -- The forced evaluation removes side effects from expressions,
20508 -- which should occur also in GNATprove mode. Otherwise, we end up
20509 -- with unexpected insertions of actions at places where this is
20510 -- not supposed to occur, e.g. on default parameters of a call.
20511
20512 if Expander_Active or GNATprove_Mode then
20513
20514 -- Call Force_Evaluation to create declarations as needed to
20515 -- deal with side effects, and also create typ_FIRST/LAST
20516 -- entities for bounds if we have a subtype name.
20517
20518 -- Note: we do this transformation even if expansion is not
20519 -- active if we are in GNATprove_Mode since the transformation
20520 -- is in general required to ensure that the resulting tree has
20521 -- proper Ada semantics.
20522
20523 Force_Evaluation
20524 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20525 Force_Evaluation
20526 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20527 end if;
20528
20529 -- We use a flag here instead of suppressing checks on the type
20530 -- because the type we check against isn't necessarily the place
20531 -- where we put the check.
20532
20533 if not R_Check_Off then
20534 R_Checks := Get_Range_Checks (R, T);
20535
20536 -- Look up tree to find an appropriate insertion point. We
20537 -- can't just use insert_actions because later processing
20538 -- depends on the insertion node. Prior to Ada 2012 the
20539 -- insertion point could only be a declaration or a loop, but
20540 -- quantified expressions can appear within any context in an
20541 -- expression, and the insertion point can be any statement,
20542 -- pragma, or declaration.
20543
20544 Insert_Node := Parent (R);
20545 while Present (Insert_Node) loop
20546 exit when
20547 Nkind (Insert_Node) in N_Declaration
20548 and then
20549 not Nkind_In
20550 (Insert_Node, N_Component_Declaration,
20551 N_Loop_Parameter_Specification,
20552 N_Function_Specification,
20553 N_Procedure_Specification);
20554
20555 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20556 or else Nkind (Insert_Node) in
20557 N_Statement_Other_Than_Procedure_Call
20558 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20559 N_Pragma);
20560
20561 Insert_Node := Parent (Insert_Node);
20562 end loop;
20563
20564 -- Why would Type_Decl not be present??? Without this test,
20565 -- short regression tests fail.
20566
20567 if Present (Insert_Node) then
20568
20569 -- Case of loop statement. Verify that the range is part
20570 -- of the subtype indication of the iteration scheme.
20571
20572 if Nkind (Insert_Node) = N_Loop_Statement then
20573 declare
20574 Indic : Node_Id;
20575
20576 begin
20577 Indic := Parent (R);
20578 while Present (Indic)
20579 and then Nkind (Indic) /= N_Subtype_Indication
20580 loop
20581 Indic := Parent (Indic);
20582 end loop;
20583
20584 if Present (Indic) then
20585 Def_Id := Etype (Subtype_Mark (Indic));
20586
20587 Insert_Range_Checks
20588 (R_Checks,
20589 Insert_Node,
20590 Def_Id,
20591 Sloc (Insert_Node),
20592 R,
20593 Do_Before => True);
20594 end if;
20595 end;
20596
20597 -- Insertion before a declaration. If the declaration
20598 -- includes discriminants, the list of applicable checks
20599 -- is given by the caller.
20600
20601 elsif Nkind (Insert_Node) in N_Declaration then
20602 Def_Id := Defining_Identifier (Insert_Node);
20603
20604 if (Ekind (Def_Id) = E_Record_Type
20605 and then Depends_On_Discriminant (R))
20606 or else
20607 (Ekind (Def_Id) = E_Protected_Type
20608 and then Has_Discriminants (Def_Id))
20609 then
20610 Append_Range_Checks
20611 (R_Checks,
20612 Check_List, Def_Id, Sloc (Insert_Node), R);
20613
20614 else
20615 Insert_Range_Checks
20616 (R_Checks,
20617 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20618
20619 end if;
20620
20621 -- Insertion before a statement. Range appears in the
20622 -- context of a quantified expression. Insertion will
20623 -- take place when expression is expanded.
20624
20625 else
20626 null;
20627 end if;
20628 end if;
20629 end if;
20630 end if;
20631
20632 -- Case of other than an explicit N_Range node
20633
20634 -- The forced evaluation removes side effects from expressions, which
20635 -- should occur also in GNATprove mode. Otherwise, we end up with
20636 -- unexpected insertions of actions at places where this is not
20637 -- supposed to occur, e.g. on default parameters of a call.
20638
20639 elsif Expander_Active or GNATprove_Mode then
20640 Get_Index_Bounds (R, Lo, Hi);
20641 Force_Evaluation (Lo);
20642 Force_Evaluation (Hi);
20643 end if;
20644 end Process_Range_Expr_In_Decl;
20645
20646 --------------------------------------
20647 -- Process_Real_Range_Specification --
20648 --------------------------------------
20649
20650 procedure Process_Real_Range_Specification (Def : Node_Id) is
20651 Spec : constant Node_Id := Real_Range_Specification (Def);
20652 Lo : Node_Id;
20653 Hi : Node_Id;
20654 Err : Boolean := False;
20655
20656 procedure Analyze_Bound (N : Node_Id);
20657 -- Analyze and check one bound
20658
20659 -------------------
20660 -- Analyze_Bound --
20661 -------------------
20662
20663 procedure Analyze_Bound (N : Node_Id) is
20664 begin
20665 Analyze_And_Resolve (N, Any_Real);
20666
20667 if not Is_OK_Static_Expression (N) then
20668 Flag_Non_Static_Expr
20669 ("bound in real type definition is not static!", N);
20670 Err := True;
20671 end if;
20672 end Analyze_Bound;
20673
20674 -- Start of processing for Process_Real_Range_Specification
20675
20676 begin
20677 if Present (Spec) then
20678 Lo := Low_Bound (Spec);
20679 Hi := High_Bound (Spec);
20680 Analyze_Bound (Lo);
20681 Analyze_Bound (Hi);
20682
20683 -- If error, clear away junk range specification
20684
20685 if Err then
20686 Set_Real_Range_Specification (Def, Empty);
20687 end if;
20688 end if;
20689 end Process_Real_Range_Specification;
20690
20691 ---------------------
20692 -- Process_Subtype --
20693 ---------------------
20694
20695 function Process_Subtype
20696 (S : Node_Id;
20697 Related_Nod : Node_Id;
20698 Related_Id : Entity_Id := Empty;
20699 Suffix : Character := ' ') return Entity_Id
20700 is
20701 P : Node_Id;
20702 Def_Id : Entity_Id;
20703 Error_Node : Node_Id;
20704 Full_View_Id : Entity_Id;
20705 Subtype_Mark_Id : Entity_Id;
20706
20707 May_Have_Null_Exclusion : Boolean;
20708
20709 procedure Check_Incomplete (T : Node_Id);
20710 -- Called to verify that an incomplete type is not used prematurely
20711
20712 ----------------------
20713 -- Check_Incomplete --
20714 ----------------------
20715
20716 procedure Check_Incomplete (T : Node_Id) is
20717 begin
20718 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20719
20720 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20721 and then
20722 not (Ada_Version >= Ada_2005
20723 and then
20724 (Nkind (Parent (T)) = N_Subtype_Declaration
20725 or else (Nkind (Parent (T)) = N_Subtype_Indication
20726 and then Nkind (Parent (Parent (T))) =
20727 N_Subtype_Declaration)))
20728 then
20729 Error_Msg_N ("invalid use of type before its full declaration", T);
20730 end if;
20731 end Check_Incomplete;
20732
20733 -- Start of processing for Process_Subtype
20734
20735 begin
20736 -- Case of no constraints present
20737
20738 if Nkind (S) /= N_Subtype_Indication then
20739 Find_Type (S);
20740 Check_Incomplete (S);
20741 P := Parent (S);
20742
20743 -- Ada 2005 (AI-231): Static check
20744
20745 if Ada_Version >= Ada_2005
20746 and then Present (P)
20747 and then Null_Exclusion_Present (P)
20748 and then Nkind (P) /= N_Access_To_Object_Definition
20749 and then not Is_Access_Type (Entity (S))
20750 then
20751 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20752 end if;
20753
20754 -- The following is ugly, can't we have a range or even a flag???
20755
20756 May_Have_Null_Exclusion :=
20757 Nkind_In (P, N_Access_Definition,
20758 N_Access_Function_Definition,
20759 N_Access_Procedure_Definition,
20760 N_Access_To_Object_Definition,
20761 N_Allocator,
20762 N_Component_Definition)
20763 or else
20764 Nkind_In (P, N_Derived_Type_Definition,
20765 N_Discriminant_Specification,
20766 N_Formal_Object_Declaration,
20767 N_Object_Declaration,
20768 N_Object_Renaming_Declaration,
20769 N_Parameter_Specification,
20770 N_Subtype_Declaration);
20771
20772 -- Create an Itype that is a duplicate of Entity (S) but with the
20773 -- null-exclusion attribute.
20774
20775 if May_Have_Null_Exclusion
20776 and then Is_Access_Type (Entity (S))
20777 and then Null_Exclusion_Present (P)
20778
20779 -- No need to check the case of an access to object definition.
20780 -- It is correct to define double not-null pointers.
20781
20782 -- Example:
20783 -- type Not_Null_Int_Ptr is not null access Integer;
20784 -- type Acc is not null access Not_Null_Int_Ptr;
20785
20786 and then Nkind (P) /= N_Access_To_Object_Definition
20787 then
20788 if Can_Never_Be_Null (Entity (S)) then
20789 case Nkind (Related_Nod) is
20790 when N_Full_Type_Declaration =>
20791 if Nkind (Type_Definition (Related_Nod))
20792 in N_Array_Type_Definition
20793 then
20794 Error_Node :=
20795 Subtype_Indication
20796 (Component_Definition
20797 (Type_Definition (Related_Nod)));
20798 else
20799 Error_Node :=
20800 Subtype_Indication (Type_Definition (Related_Nod));
20801 end if;
20802
20803 when N_Subtype_Declaration =>
20804 Error_Node := Subtype_Indication (Related_Nod);
20805
20806 when N_Object_Declaration =>
20807 Error_Node := Object_Definition (Related_Nod);
20808
20809 when N_Component_Declaration =>
20810 Error_Node :=
20811 Subtype_Indication (Component_Definition (Related_Nod));
20812
20813 when N_Allocator =>
20814 Error_Node := Expression (Related_Nod);
20815
20816 when others =>
20817 pragma Assert (False);
20818 Error_Node := Related_Nod;
20819 end case;
20820
20821 Error_Msg_NE
20822 ("`NOT NULL` not allowed (& already excludes null)",
20823 Error_Node,
20824 Entity (S));
20825 end if;
20826
20827 Set_Etype (S,
20828 Create_Null_Excluding_Itype
20829 (T => Entity (S),
20830 Related_Nod => P));
20831 Set_Entity (S, Etype (S));
20832 end if;
20833
20834 return Entity (S);
20835
20836 -- Case of constraint present, so that we have an N_Subtype_Indication
20837 -- node (this node is created only if constraints are present).
20838
20839 else
20840 Find_Type (Subtype_Mark (S));
20841
20842 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20843 and then not
20844 (Nkind (Parent (S)) = N_Subtype_Declaration
20845 and then Is_Itype (Defining_Identifier (Parent (S))))
20846 then
20847 Check_Incomplete (Subtype_Mark (S));
20848 end if;
20849
20850 P := Parent (S);
20851 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20852
20853 -- Explicit subtype declaration case
20854
20855 if Nkind (P) = N_Subtype_Declaration then
20856 Def_Id := Defining_Identifier (P);
20857
20858 -- Explicit derived type definition case
20859
20860 elsif Nkind (P) = N_Derived_Type_Definition then
20861 Def_Id := Defining_Identifier (Parent (P));
20862
20863 -- Implicit case, the Def_Id must be created as an implicit type.
20864 -- The one exception arises in the case of concurrent types, array
20865 -- and access types, where other subsidiary implicit types may be
20866 -- created and must appear before the main implicit type. In these
20867 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20868 -- has not yet been called to create Def_Id.
20869
20870 else
20871 if Is_Array_Type (Subtype_Mark_Id)
20872 or else Is_Concurrent_Type (Subtype_Mark_Id)
20873 or else Is_Access_Type (Subtype_Mark_Id)
20874 then
20875 Def_Id := Empty;
20876
20877 -- For the other cases, we create a new unattached Itype,
20878 -- and set the indication to ensure it gets attached later.
20879
20880 else
20881 Def_Id :=
20882 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20883 end if;
20884 end if;
20885
20886 -- If the kind of constraint is invalid for this kind of type,
20887 -- then give an error, and then pretend no constraint was given.
20888
20889 if not Is_Valid_Constraint_Kind
20890 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20891 then
20892 Error_Msg_N
20893 ("incorrect constraint for this kind of type", Constraint (S));
20894
20895 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20896
20897 -- Set Ekind of orphan itype, to prevent cascaded errors
20898
20899 if Present (Def_Id) then
20900 Set_Ekind (Def_Id, Ekind (Any_Type));
20901 end if;
20902
20903 -- Make recursive call, having got rid of the bogus constraint
20904
20905 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20906 end if;
20907
20908 -- Remaining processing depends on type. Select on Base_Type kind to
20909 -- ensure getting to the concrete type kind in the case of a private
20910 -- subtype (needed when only doing semantic analysis).
20911
20912 case Ekind (Base_Type (Subtype_Mark_Id)) is
20913 when Access_Kind =>
20914
20915 -- If this is a constraint on a class-wide type, discard it.
20916 -- There is currently no way to express a partial discriminant
20917 -- constraint on a type with unknown discriminants. This is
20918 -- a pathology that the ACATS wisely decides not to test.
20919
20920 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20921 if Comes_From_Source (S) then
20922 Error_Msg_N
20923 ("constraint on class-wide type ignored??",
20924 Constraint (S));
20925 end if;
20926
20927 if Nkind (P) = N_Subtype_Declaration then
20928 Set_Subtype_Indication (P,
20929 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20930 end if;
20931
20932 return Subtype_Mark_Id;
20933 end if;
20934
20935 Constrain_Access (Def_Id, S, Related_Nod);
20936
20937 if Expander_Active
20938 and then Is_Itype (Designated_Type (Def_Id))
20939 and then Nkind (Related_Nod) = N_Subtype_Declaration
20940 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20941 then
20942 Build_Itype_Reference
20943 (Designated_Type (Def_Id), Related_Nod);
20944 end if;
20945
20946 when Array_Kind =>
20947 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20948
20949 when Decimal_Fixed_Point_Kind =>
20950 Constrain_Decimal (Def_Id, S);
20951
20952 when Enumeration_Kind =>
20953 Constrain_Enumeration (Def_Id, S);
20954 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20955
20956 when Ordinary_Fixed_Point_Kind =>
20957 Constrain_Ordinary_Fixed (Def_Id, S);
20958
20959 when Float_Kind =>
20960 Constrain_Float (Def_Id, S);
20961
20962 when Integer_Kind =>
20963 Constrain_Integer (Def_Id, S);
20964 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20965
20966 when E_Record_Type |
20967 E_Record_Subtype |
20968 Class_Wide_Kind |
20969 E_Incomplete_Type =>
20970 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20971
20972 if Ekind (Def_Id) = E_Incomplete_Type then
20973 Set_Private_Dependents (Def_Id, New_Elmt_List);
20974 end if;
20975
20976 when Private_Kind =>
20977 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20978
20979 -- The base type may be private but Def_Id may be a full view
20980 -- in an instance.
20981
20982 if Is_Private_Type (Def_Id) then
20983 Set_Private_Dependents (Def_Id, New_Elmt_List);
20984 end if;
20985
20986 -- In case of an invalid constraint prevent further processing
20987 -- since the type constructed is missing expected fields.
20988
20989 if Etype (Def_Id) = Any_Type then
20990 return Def_Id;
20991 end if;
20992
20993 -- If the full view is that of a task with discriminants,
20994 -- we must constrain both the concurrent type and its
20995 -- corresponding record type. Otherwise we will just propagate
20996 -- the constraint to the full view, if available.
20997
20998 if Present (Full_View (Subtype_Mark_Id))
20999 and then Has_Discriminants (Subtype_Mark_Id)
21000 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21001 then
21002 Full_View_Id :=
21003 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21004
21005 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21006 Constrain_Concurrent (Full_View_Id, S,
21007 Related_Nod, Related_Id, Suffix);
21008 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21009 Set_Full_View (Def_Id, Full_View_Id);
21010
21011 -- Introduce an explicit reference to the private subtype,
21012 -- to prevent scope anomalies in gigi if first use appears
21013 -- in a nested context, e.g. a later function body.
21014 -- Should this be generated in other contexts than a full
21015 -- type declaration?
21016
21017 if Is_Itype (Def_Id)
21018 and then
21019 Nkind (Parent (P)) = N_Full_Type_Declaration
21020 then
21021 Build_Itype_Reference (Def_Id, Parent (P));
21022 end if;
21023
21024 else
21025 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21026 end if;
21027
21028 when Concurrent_Kind =>
21029 Constrain_Concurrent (Def_Id, S,
21030 Related_Nod, Related_Id, Suffix);
21031
21032 when others =>
21033 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21034 end case;
21035
21036 -- Size and Convention are always inherited from the base type
21037
21038 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21039 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21040
21041 return Def_Id;
21042 end if;
21043 end Process_Subtype;
21044
21045 -----------------------------
21046 -- Record_Type_Declaration --
21047 -----------------------------
21048
21049 procedure Record_Type_Declaration
21050 (T : Entity_Id;
21051 N : Node_Id;
21052 Prev : Entity_Id)
21053 is
21054 Def : constant Node_Id := Type_Definition (N);
21055 Is_Tagged : Boolean;
21056 Tag_Comp : Entity_Id;
21057
21058 begin
21059 -- These flags must be initialized before calling Process_Discriminants
21060 -- because this routine makes use of them.
21061
21062 Set_Ekind (T, E_Record_Type);
21063 Set_Etype (T, T);
21064 Init_Size_Align (T);
21065 Set_Interfaces (T, No_Elist);
21066 Set_Stored_Constraint (T, No_Elist);
21067 Set_Default_SSO (T);
21068
21069 -- Normal case
21070
21071 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21072 if Limited_Present (Def) then
21073 Check_SPARK_05_Restriction ("limited is not allowed", N);
21074 end if;
21075
21076 if Abstract_Present (Def) then
21077 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21078 end if;
21079
21080 -- The flag Is_Tagged_Type might have already been set by
21081 -- Find_Type_Name if it detected an error for declaration T. This
21082 -- arises in the case of private tagged types where the full view
21083 -- omits the word tagged.
21084
21085 Is_Tagged :=
21086 Tagged_Present (Def)
21087 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21088
21089 Set_Is_Limited_Record (T, Limited_Present (Def));
21090
21091 if Is_Tagged then
21092 Set_Is_Tagged_Type (T, True);
21093 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21094 end if;
21095
21096 -- Type is abstract if full declaration carries keyword, or if
21097 -- previous partial view did.
21098
21099 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21100 or else Abstract_Present (Def));
21101
21102 else
21103 Check_SPARK_05_Restriction ("interface is not allowed", N);
21104
21105 Is_Tagged := True;
21106 Analyze_Interface_Declaration (T, Def);
21107
21108 if Present (Discriminant_Specifications (N)) then
21109 Error_Msg_N
21110 ("interface types cannot have discriminants",
21111 Defining_Identifier
21112 (First (Discriminant_Specifications (N))));
21113 end if;
21114 end if;
21115
21116 -- First pass: if there are self-referential access components,
21117 -- create the required anonymous access type declarations, and if
21118 -- need be an incomplete type declaration for T itself.
21119
21120 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21121
21122 if Ada_Version >= Ada_2005
21123 and then Present (Interface_List (Def))
21124 then
21125 Check_Interfaces (N, Def);
21126
21127 declare
21128 Ifaces_List : Elist_Id;
21129
21130 begin
21131 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21132 -- already in the parents.
21133
21134 Collect_Interfaces
21135 (T => T,
21136 Ifaces_List => Ifaces_List,
21137 Exclude_Parents => True);
21138
21139 Set_Interfaces (T, Ifaces_List);
21140 end;
21141 end if;
21142
21143 -- Records constitute a scope for the component declarations within.
21144 -- The scope is created prior to the processing of these declarations.
21145 -- Discriminants are processed first, so that they are visible when
21146 -- processing the other components. The Ekind of the record type itself
21147 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21148
21149 -- Enter record scope
21150
21151 Push_Scope (T);
21152
21153 -- If an incomplete or private type declaration was already given for
21154 -- the type, then this scope already exists, and the discriminants have
21155 -- been declared within. We must verify that the full declaration
21156 -- matches the incomplete one.
21157
21158 Check_Or_Process_Discriminants (N, T, Prev);
21159
21160 Set_Is_Constrained (T, not Has_Discriminants (T));
21161 Set_Has_Delayed_Freeze (T, True);
21162
21163 -- For tagged types add a manually analyzed component corresponding
21164 -- to the component _tag, the corresponding piece of tree will be
21165 -- expanded as part of the freezing actions if it is not a CPP_Class.
21166
21167 if Is_Tagged then
21168
21169 -- Do not add the tag unless we are in expansion mode
21170
21171 if Expander_Active then
21172 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21173 Enter_Name (Tag_Comp);
21174
21175 Set_Ekind (Tag_Comp, E_Component);
21176 Set_Is_Tag (Tag_Comp);
21177 Set_Is_Aliased (Tag_Comp);
21178 Set_Etype (Tag_Comp, RTE (RE_Tag));
21179 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21180 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21181 Init_Component_Location (Tag_Comp);
21182
21183 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21184 -- implemented interfaces.
21185
21186 if Has_Interfaces (T) then
21187 Add_Interface_Tag_Components (N, T);
21188 end if;
21189 end if;
21190
21191 Make_Class_Wide_Type (T);
21192 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21193 end if;
21194
21195 -- We must suppress range checks when processing record components in
21196 -- the presence of discriminants, since we don't want spurious checks to
21197 -- be generated during their analysis, but Suppress_Range_Checks flags
21198 -- must be reset the after processing the record definition.
21199
21200 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21201 -- couldn't we just use the normal range check suppression method here.
21202 -- That would seem cleaner ???
21203
21204 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21205 Set_Kill_Range_Checks (T, True);
21206 Record_Type_Definition (Def, Prev);
21207 Set_Kill_Range_Checks (T, False);
21208 else
21209 Record_Type_Definition (Def, Prev);
21210 end if;
21211
21212 -- Exit from record scope
21213
21214 End_Scope;
21215
21216 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21217 -- the implemented interfaces and associate them an aliased entity.
21218
21219 if Is_Tagged
21220 and then not Is_Empty_List (Interface_List (Def))
21221 then
21222 Derive_Progenitor_Subprograms (T, T);
21223 end if;
21224
21225 Check_Function_Writable_Actuals (N);
21226 end Record_Type_Declaration;
21227
21228 ----------------------------
21229 -- Record_Type_Definition --
21230 ----------------------------
21231
21232 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21233 Component : Entity_Id;
21234 Ctrl_Components : Boolean := False;
21235 Final_Storage_Only : Boolean;
21236 T : Entity_Id;
21237
21238 begin
21239 if Ekind (Prev_T) = E_Incomplete_Type then
21240 T := Full_View (Prev_T);
21241 else
21242 T := Prev_T;
21243 end if;
21244
21245 -- In SPARK, tagged types and type extensions may only be declared in
21246 -- the specification of library unit packages.
21247
21248 if Present (Def) and then Is_Tagged_Type (T) then
21249 declare
21250 Typ : Node_Id;
21251 Ctxt : Node_Id;
21252
21253 begin
21254 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21255 Typ := Parent (Def);
21256 else
21257 pragma Assert
21258 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21259 Typ := Parent (Parent (Def));
21260 end if;
21261
21262 Ctxt := Parent (Typ);
21263
21264 if Nkind (Ctxt) = N_Package_Body
21265 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21266 then
21267 Check_SPARK_05_Restriction
21268 ("type should be defined in package specification", Typ);
21269
21270 elsif Nkind (Ctxt) /= N_Package_Specification
21271 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21272 then
21273 Check_SPARK_05_Restriction
21274 ("type should be defined in library unit package", Typ);
21275 end if;
21276 end;
21277 end if;
21278
21279 Final_Storage_Only := not Is_Controlled_Active (T);
21280
21281 -- Ada 2005: Check whether an explicit Limited is present in a derived
21282 -- type declaration.
21283
21284 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21285 and then Limited_Present (Parent (Def))
21286 then
21287 Set_Is_Limited_Record (T);
21288 end if;
21289
21290 -- If the component list of a record type is defined by the reserved
21291 -- word null and there is no discriminant part, then the record type has
21292 -- no components and all records of the type are null records (RM 3.7)
21293 -- This procedure is also called to process the extension part of a
21294 -- record extension, in which case the current scope may have inherited
21295 -- components.
21296
21297 if No (Def)
21298 or else No (Component_List (Def))
21299 or else Null_Present (Component_List (Def))
21300 then
21301 if not Is_Tagged_Type (T) then
21302 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21303 end if;
21304
21305 else
21306 Analyze_Declarations (Component_Items (Component_List (Def)));
21307
21308 if Present (Variant_Part (Component_List (Def))) then
21309 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21310 Analyze (Variant_Part (Component_List (Def)));
21311 end if;
21312 end if;
21313
21314 -- After completing the semantic analysis of the record definition,
21315 -- record components, both new and inherited, are accessible. Set their
21316 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21317 -- whose Ekind may be void.
21318
21319 Component := First_Entity (Current_Scope);
21320 while Present (Component) loop
21321 if Ekind (Component) = E_Void
21322 and then not Is_Itype (Component)
21323 then
21324 Set_Ekind (Component, E_Component);
21325 Init_Component_Location (Component);
21326 end if;
21327
21328 Propagate_Concurrent_Flags (T, Etype (Component));
21329
21330 if Ekind (Component) /= E_Component then
21331 null;
21332
21333 -- Do not set Has_Controlled_Component on a class-wide equivalent
21334 -- type. See Make_CW_Equivalent_Type.
21335
21336 elsif not Is_Class_Wide_Equivalent_Type (T)
21337 and then (Has_Controlled_Component (Etype (Component))
21338 or else (Chars (Component) /= Name_uParent
21339 and then Is_Controlled_Active
21340 (Etype (Component))))
21341 then
21342 Set_Has_Controlled_Component (T, True);
21343 Final_Storage_Only :=
21344 Final_Storage_Only
21345 and then Finalize_Storage_Only (Etype (Component));
21346 Ctrl_Components := True;
21347 end if;
21348
21349 Next_Entity (Component);
21350 end loop;
21351
21352 -- A Type is Finalize_Storage_Only only if all its controlled components
21353 -- are also.
21354
21355 if Ctrl_Components then
21356 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21357 end if;
21358
21359 -- Place reference to end record on the proper entity, which may
21360 -- be a partial view.
21361
21362 if Present (Def) then
21363 Process_End_Label (Def, 'e', Prev_T);
21364 end if;
21365 end Record_Type_Definition;
21366
21367 ------------------------
21368 -- Replace_Components --
21369 ------------------------
21370
21371 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21372 function Process (N : Node_Id) return Traverse_Result;
21373
21374 -------------
21375 -- Process --
21376 -------------
21377
21378 function Process (N : Node_Id) return Traverse_Result is
21379 Comp : Entity_Id;
21380
21381 begin
21382 if Nkind (N) = N_Discriminant_Specification then
21383 Comp := First_Discriminant (Typ);
21384 while Present (Comp) loop
21385 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21386 Set_Defining_Identifier (N, Comp);
21387 exit;
21388 end if;
21389
21390 Next_Discriminant (Comp);
21391 end loop;
21392
21393 elsif Nkind (N) = N_Component_Declaration then
21394 Comp := First_Component (Typ);
21395 while Present (Comp) loop
21396 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21397 Set_Defining_Identifier (N, Comp);
21398 exit;
21399 end if;
21400
21401 Next_Component (Comp);
21402 end loop;
21403 end if;
21404
21405 return OK;
21406 end Process;
21407
21408 procedure Replace is new Traverse_Proc (Process);
21409
21410 -- Start of processing for Replace_Components
21411
21412 begin
21413 Replace (Decl);
21414 end Replace_Components;
21415
21416 -------------------------------
21417 -- Set_Completion_Referenced --
21418 -------------------------------
21419
21420 procedure Set_Completion_Referenced (E : Entity_Id) is
21421 begin
21422 -- If in main unit, mark entity that is a completion as referenced,
21423 -- warnings go on the partial view when needed.
21424
21425 if In_Extended_Main_Source_Unit (E) then
21426 Set_Referenced (E);
21427 end if;
21428 end Set_Completion_Referenced;
21429
21430 ---------------------
21431 -- Set_Default_SSO --
21432 ---------------------
21433
21434 procedure Set_Default_SSO (T : Entity_Id) is
21435 begin
21436 case Opt.Default_SSO is
21437 when ' ' =>
21438 null;
21439 when 'L' =>
21440 Set_SSO_Set_Low_By_Default (T, True);
21441 when 'H' =>
21442 Set_SSO_Set_High_By_Default (T, True);
21443 when others =>
21444 raise Program_Error;
21445 end case;
21446 end Set_Default_SSO;
21447
21448 ---------------------
21449 -- Set_Fixed_Range --
21450 ---------------------
21451
21452 -- The range for fixed-point types is complicated by the fact that we
21453 -- do not know the exact end points at the time of the declaration. This
21454 -- is true for three reasons:
21455
21456 -- A size clause may affect the fudging of the end-points.
21457 -- A small clause may affect the values of the end-points.
21458 -- We try to include the end-points if it does not affect the size.
21459
21460 -- This means that the actual end-points must be established at the
21461 -- point when the type is frozen. Meanwhile, we first narrow the range
21462 -- as permitted (so that it will fit if necessary in a small specified
21463 -- size), and then build a range subtree with these narrowed bounds.
21464 -- Set_Fixed_Range constructs the range from real literal values, and
21465 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21466
21467 -- The parent of this range is set to point to the entity so that it is
21468 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21469 -- other scalar types, which are just pointers to the range in the
21470 -- original tree, this would otherwise be an orphan).
21471
21472 -- The tree is left unanalyzed. When the type is frozen, the processing
21473 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21474 -- analyzed, and uses this as an indication that it should complete
21475 -- work on the range (it will know the final small and size values).
21476
21477 procedure Set_Fixed_Range
21478 (E : Entity_Id;
21479 Loc : Source_Ptr;
21480 Lo : Ureal;
21481 Hi : Ureal)
21482 is
21483 S : constant Node_Id :=
21484 Make_Range (Loc,
21485 Low_Bound => Make_Real_Literal (Loc, Lo),
21486 High_Bound => Make_Real_Literal (Loc, Hi));
21487 begin
21488 Set_Scalar_Range (E, S);
21489 Set_Parent (S, E);
21490
21491 -- Before the freeze point, the bounds of a fixed point are universal
21492 -- and carry the corresponding type.
21493
21494 Set_Etype (Low_Bound (S), Universal_Real);
21495 Set_Etype (High_Bound (S), Universal_Real);
21496 end Set_Fixed_Range;
21497
21498 ----------------------------------
21499 -- Set_Scalar_Range_For_Subtype --
21500 ----------------------------------
21501
21502 procedure Set_Scalar_Range_For_Subtype
21503 (Def_Id : Entity_Id;
21504 R : Node_Id;
21505 Subt : Entity_Id)
21506 is
21507 Kind : constant Entity_Kind := Ekind (Def_Id);
21508
21509 begin
21510 -- Defend against previous error
21511
21512 if Nkind (R) = N_Error then
21513 return;
21514 end if;
21515
21516 Set_Scalar_Range (Def_Id, R);
21517
21518 -- We need to link the range into the tree before resolving it so
21519 -- that types that are referenced, including importantly the subtype
21520 -- itself, are properly frozen (Freeze_Expression requires that the
21521 -- expression be properly linked into the tree). Of course if it is
21522 -- already linked in, then we do not disturb the current link.
21523
21524 if No (Parent (R)) then
21525 Set_Parent (R, Def_Id);
21526 end if;
21527
21528 -- Reset the kind of the subtype during analysis of the range, to
21529 -- catch possible premature use in the bounds themselves.
21530
21531 Set_Ekind (Def_Id, E_Void);
21532 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21533 Set_Ekind (Def_Id, Kind);
21534 end Set_Scalar_Range_For_Subtype;
21535
21536 --------------------------------------------------------
21537 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21538 --------------------------------------------------------
21539
21540 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21541 (E : Entity_Id)
21542 is
21543 begin
21544 -- Make sure set if encountered during Expand_To_Stored_Constraint
21545
21546 Set_Stored_Constraint (E, No_Elist);
21547
21548 -- Give it the right value
21549
21550 if Is_Constrained (E) and then Has_Discriminants (E) then
21551 Set_Stored_Constraint (E,
21552 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21553 end if;
21554 end Set_Stored_Constraint_From_Discriminant_Constraint;
21555
21556 -------------------------------------
21557 -- Signed_Integer_Type_Declaration --
21558 -------------------------------------
21559
21560 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21561 Implicit_Base : Entity_Id;
21562 Base_Typ : Entity_Id;
21563 Lo_Val : Uint;
21564 Hi_Val : Uint;
21565 Errs : Boolean := False;
21566 Lo : Node_Id;
21567 Hi : Node_Id;
21568
21569 function Can_Derive_From (E : Entity_Id) return Boolean;
21570 -- Determine whether given bounds allow derivation from specified type
21571
21572 procedure Check_Bound (Expr : Node_Id);
21573 -- Check bound to make sure it is integral and static. If not, post
21574 -- appropriate error message and set Errs flag
21575
21576 ---------------------
21577 -- Can_Derive_From --
21578 ---------------------
21579
21580 -- Note we check both bounds against both end values, to deal with
21581 -- strange types like ones with a range of 0 .. -12341234.
21582
21583 function Can_Derive_From (E : Entity_Id) return Boolean is
21584 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21585 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21586 begin
21587 return Lo <= Lo_Val and then Lo_Val <= Hi
21588 and then
21589 Lo <= Hi_Val and then Hi_Val <= Hi;
21590 end Can_Derive_From;
21591
21592 -----------------
21593 -- Check_Bound --
21594 -----------------
21595
21596 procedure Check_Bound (Expr : Node_Id) is
21597 begin
21598 -- If a range constraint is used as an integer type definition, each
21599 -- bound of the range must be defined by a static expression of some
21600 -- integer type, but the two bounds need not have the same integer
21601 -- type (Negative bounds are allowed.) (RM 3.5.4)
21602
21603 if not Is_Integer_Type (Etype (Expr)) then
21604 Error_Msg_N
21605 ("integer type definition bounds must be of integer type", Expr);
21606 Errs := True;
21607
21608 elsif not Is_OK_Static_Expression (Expr) then
21609 Flag_Non_Static_Expr
21610 ("non-static expression used for integer type bound!", Expr);
21611 Errs := True;
21612
21613 -- The bounds are folded into literals, and we set their type to be
21614 -- universal, to avoid typing difficulties: we cannot set the type
21615 -- of the literal to the new type, because this would be a forward
21616 -- reference for the back end, and if the original type is user-
21617 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21618
21619 else
21620 if Is_Entity_Name (Expr) then
21621 Fold_Uint (Expr, Expr_Value (Expr), True);
21622 end if;
21623
21624 Set_Etype (Expr, Universal_Integer);
21625 end if;
21626 end Check_Bound;
21627
21628 -- Start of processing for Signed_Integer_Type_Declaration
21629
21630 begin
21631 -- Create an anonymous base type
21632
21633 Implicit_Base :=
21634 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21635
21636 -- Analyze and check the bounds, they can be of any integer type
21637
21638 Lo := Low_Bound (Def);
21639 Hi := High_Bound (Def);
21640
21641 -- Arbitrarily use Integer as the type if either bound had an error
21642
21643 if Hi = Error or else Lo = Error then
21644 Base_Typ := Any_Integer;
21645 Set_Error_Posted (T, True);
21646
21647 -- Here both bounds are OK expressions
21648
21649 else
21650 Analyze_And_Resolve (Lo, Any_Integer);
21651 Analyze_And_Resolve (Hi, Any_Integer);
21652
21653 Check_Bound (Lo);
21654 Check_Bound (Hi);
21655
21656 if Errs then
21657 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21658 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21659 end if;
21660
21661 -- Find type to derive from
21662
21663 Lo_Val := Expr_Value (Lo);
21664 Hi_Val := Expr_Value (Hi);
21665
21666 if Can_Derive_From (Standard_Short_Short_Integer) then
21667 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21668
21669 elsif Can_Derive_From (Standard_Short_Integer) then
21670 Base_Typ := Base_Type (Standard_Short_Integer);
21671
21672 elsif Can_Derive_From (Standard_Integer) then
21673 Base_Typ := Base_Type (Standard_Integer);
21674
21675 elsif Can_Derive_From (Standard_Long_Integer) then
21676 Base_Typ := Base_Type (Standard_Long_Integer);
21677
21678 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21679 Check_Restriction (No_Long_Long_Integers, Def);
21680 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21681
21682 else
21683 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21684 Error_Msg_N ("integer type definition bounds out of range", Def);
21685 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21686 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21687 end if;
21688 end if;
21689
21690 -- Complete both implicit base and declared first subtype entities. The
21691 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21692 -- are not clobbered when the signed integer type acts as a full view of
21693 -- a private type.
21694
21695 Set_Etype (Implicit_Base, Base_Typ);
21696 Set_Size_Info (Implicit_Base, Base_Typ);
21697 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21698 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21699 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21700
21701 Set_Ekind (T, E_Signed_Integer_Subtype);
21702 Set_Etype (T, Implicit_Base);
21703 Set_Size_Info (T, Implicit_Base);
21704 Inherit_Rep_Item_Chain (T, Implicit_Base);
21705 Set_Scalar_Range (T, Def);
21706 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21707 Set_Is_Constrained (T);
21708 end Signed_Integer_Type_Declaration;
21709
21710 end Sem_Ch3;