1e6abf24cec7e33c99c2ec159521a010e42de3e9
[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-2013, 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 Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Pakd; use Exp_Pakd;
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 Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_Smem; use Sem_Smem;
69 with Sem_Type; use Sem_Type;
70 with Sem_Util; use Sem_Util;
71 with Sem_Warn; use Sem_Warn;
72 with Stand; use Stand;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Ttypes; use Ttypes;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
81
82 package body Sem_Ch3 is
83
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
87
88 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
89 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
90 -- abstract interface types implemented by a record type or a derived
91 -- record type.
92
93 procedure Build_Derived_Type
94 (N : Node_Id;
95 Parent_Type : Entity_Id;
96 Derived_Type : Entity_Id;
97 Is_Completion : Boolean;
98 Derive_Subps : Boolean := True);
99 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
100 -- the N_Full_Type_Declaration node containing the derived type definition.
101 -- Parent_Type is the entity for the parent type in the derived type
102 -- definition and Derived_Type the actual derived type. Is_Completion must
103 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
104 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
105 -- completion of a private type declaration. If Is_Completion is set to
106 -- True, N is the completion of a private type declaration and Derived_Type
107 -- is different from the defining identifier inside N (i.e. Derived_Type /=
108 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
109 -- subprograms should be derived. The only case where this parameter is
110 -- False is when Build_Derived_Type is recursively called to process an
111 -- implicit derived full type for a type derived from a private type (in
112 -- that case the subprograms must only be derived for the private view of
113 -- the type).
114 --
115 -- ??? These flags need a bit of re-examination and re-documentation:
116 -- ??? are they both necessary (both seem related to the recursion)?
117
118 procedure Build_Derived_Access_Type
119 (N : Node_Id;
120 Parent_Type : Entity_Id;
121 Derived_Type : Entity_Id);
122 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
123 -- create an implicit base if the parent type is constrained or if the
124 -- subtype indication has a constraint.
125
126 procedure Build_Derived_Array_Type
127 (N : Node_Id;
128 Parent_Type : Entity_Id;
129 Derived_Type : Entity_Id);
130 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
131 -- create an implicit base if the parent type is constrained or if the
132 -- subtype indication has a constraint.
133
134 procedure Build_Derived_Concurrent_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
139 -- protected type, inherit entries and protected subprograms, check
140 -- legality of discriminant constraints if any.
141
142 procedure Build_Derived_Enumeration_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
147 -- type, we must create a new list of literals. Types derived from
148 -- Character and [Wide_]Wide_Character are special-cased.
149
150 procedure Build_Derived_Numeric_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
155 -- an anonymous base type, and propagate constraint to subtype if needed.
156
157 procedure Build_Derived_Private_Type
158 (N : Node_Id;
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id;
161 Is_Completion : Boolean;
162 Derive_Subps : Boolean := True);
163 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
164 -- because the parent may or may not have a completion, and the derivation
165 -- may itself be a completion.
166
167 procedure Build_Derived_Record_Type
168 (N : Node_Id;
169 Parent_Type : Entity_Id;
170 Derived_Type : Entity_Id;
171 Derive_Subps : Boolean := True);
172 -- Subsidiary procedure used for tagged and untagged record types
173 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
174 -- All parameters are as in Build_Derived_Type except that N, in
175 -- addition to being an N_Full_Type_Declaration node, can also be an
176 -- N_Private_Extension_Declaration node. See the definition of this routine
177 -- for much more info. Derive_Subps indicates whether subprograms should be
178 -- derived from the parent type. The only case where Derive_Subps is False
179 -- is for an implicit derived full type for a type derived from a private
180 -- type (see Build_Derived_Type).
181
182 procedure Build_Discriminal (Discrim : Entity_Id);
183 -- Create the discriminal corresponding to discriminant Discrim, that is
184 -- the parameter corresponding to Discrim to be used in initialization
185 -- procedures for the type where Discrim is a discriminant. Discriminals
186 -- are not used during semantic analysis, and are not fully defined
187 -- entities until expansion. Thus they are not given a scope until
188 -- initialization procedures are built.
189
190 function Build_Discriminant_Constraints
191 (T : Entity_Id;
192 Def : Node_Id;
193 Derived_Def : Boolean := False) return Elist_Id;
194 -- Validate discriminant constraints and return the list of the constraints
195 -- in order of discriminant declarations, where T is the discriminated
196 -- unconstrained type. Def is the N_Subtype_Indication node where the
197 -- discriminants constraints for T are specified. Derived_Def is True
198 -- when building the discriminant constraints in a derived type definition
199 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
200 -- type and Def is the constraint "(xxx)" on T and this routine sets the
201 -- Corresponding_Discriminant field of the discriminants in the derived
202 -- type D to point to the corresponding discriminants in the parent type T.
203
204 procedure Build_Discriminated_Subtype
205 (T : Entity_Id;
206 Def_Id : Entity_Id;
207 Elist : Elist_Id;
208 Related_Nod : Node_Id;
209 For_Access : Boolean := False);
210 -- Subsidiary procedure to Constrain_Discriminated_Type and to
211 -- Process_Incomplete_Dependents. Given
212 --
213 -- T (a possibly discriminated base type)
214 -- Def_Id (a very partially built subtype for T),
215 --
216 -- the call completes Def_Id to be the appropriate E_*_Subtype.
217 --
218 -- The Elist is the list of discriminant constraints if any (it is set
219 -- to No_Elist if T is not a discriminated type, and to an empty list if
220 -- T has discriminants but there are no discriminant constraints). The
221 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
222 -- The For_Access says whether or not this subtype is really constraining
223 -- an access type. That is its sole purpose is the designated type of an
224 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
225 -- is built to avoid freezing T when the access subtype is frozen.
226
227 function Build_Scalar_Bound
228 (Bound : Node_Id;
229 Par_T : Entity_Id;
230 Der_T : Entity_Id) return Node_Id;
231 -- The bounds of a derived scalar type are conversions of the bounds of
232 -- the parent type. Optimize the representation if the bounds are literals.
233 -- Needs a more complete spec--what are the parameters exactly, and what
234 -- exactly is the returned value, and how is Bound affected???
235
236 procedure Build_Underlying_Full_View
237 (N : Node_Id;
238 Typ : Entity_Id;
239 Par : Entity_Id);
240 -- If the completion of a private type is itself derived from a private
241 -- type, or if the full view of a private subtype is itself private, the
242 -- back-end has no way to compute the actual size of this type. We build
243 -- an internal subtype declaration of the proper parent type to convey
244 -- this information. This extra mechanism is needed because a full
245 -- view cannot itself have a full view (it would get clobbered during
246 -- view exchanges).
247
248 procedure Check_Access_Discriminant_Requires_Limited
249 (D : Node_Id;
250 Loc : Node_Id);
251 -- Check the restriction that the type to which an access discriminant
252 -- belongs must be a concurrent type or a descendant of a type with
253 -- the reserved word 'limited' in its declaration.
254
255 procedure Check_Anonymous_Access_Components
256 (Typ_Decl : Node_Id;
257 Typ : Entity_Id;
258 Prev : Entity_Id;
259 Comp_List : Node_Id);
260 -- Ada 2005 AI-382: an access component in a record definition can refer to
261 -- the enclosing record, in which case it denotes the type itself, and not
262 -- the current instance of the type. We create an anonymous access type for
263 -- the component, and flag it as an access to a component, so accessibility
264 -- checks are properly performed on it. The declaration of the access type
265 -- is placed ahead of that of the record to prevent order-of-elaboration
266 -- circularity issues in Gigi. We create an incomplete type for the record
267 -- declaration, which is the designated type of the anonymous access.
268
269 procedure Check_Delta_Expression (E : Node_Id);
270 -- Check that the expression represented by E is suitable for use as a
271 -- delta expression, i.e. it is of real type and is static.
272
273 procedure Check_Digits_Expression (E : Node_Id);
274 -- Check that the expression represented by E is suitable for use as a
275 -- digits expression, i.e. it is of integer type, positive and static.
276
277 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
278 -- Validate the initialization of an object declaration. T is the required
279 -- type, and Exp is the initialization expression.
280
281 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
282 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
283
284 procedure Check_Or_Process_Discriminants
285 (N : Node_Id;
286 T : Entity_Id;
287 Prev : Entity_Id := Empty);
288 -- If N is the full declaration of the completion T of an incomplete or
289 -- private type, check its discriminants (which are already known to be
290 -- conformant with those of the partial view, see Find_Type_Name),
291 -- otherwise process them. Prev is the entity of the partial declaration,
292 -- if any.
293
294 procedure Check_Real_Bound (Bound : Node_Id);
295 -- Check given bound for being of real type and static. If not, post an
296 -- appropriate message, and rewrite the bound with the real literal zero.
297
298 procedure Constant_Redeclaration
299 (Id : Entity_Id;
300 N : Node_Id;
301 T : out Entity_Id);
302 -- Various checks on legality of full declaration of deferred constant.
303 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
304 -- node. The caller has not yet set any attributes of this entity.
305
306 function Contain_Interface
307 (Iface : Entity_Id;
308 Ifaces : Elist_Id) return Boolean;
309 -- Ada 2005: Determine whether Iface is present in the list Ifaces
310
311 procedure Convert_Scalar_Bounds
312 (N : Node_Id;
313 Parent_Type : Entity_Id;
314 Derived_Type : Entity_Id;
315 Loc : Source_Ptr);
316 -- For derived scalar types, convert the bounds in the type definition to
317 -- the derived type, and complete their analysis. Given a constraint of the
318 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
319 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
320 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
321 -- subtype are conversions of those bounds to the derived_type, so that
322 -- their typing is consistent.
323
324 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
325 -- Copies attributes from array base type T2 to array base type T1. Copies
326 -- only attributes that apply to base types, but not subtypes.
327
328 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
329 -- Copies attributes from array subtype T2 to array subtype T1. Copies
330 -- attributes that apply to both subtypes and base types.
331
332 procedure Create_Constrained_Components
333 (Subt : Entity_Id;
334 Decl_Node : Node_Id;
335 Typ : Entity_Id;
336 Constraints : Elist_Id);
337 -- Build the list of entities for a constrained discriminated record
338 -- subtype. If a component depends on a discriminant, replace its subtype
339 -- using the discriminant values in the discriminant constraint. Subt
340 -- is the defining identifier for the subtype whose list of constrained
341 -- entities we will create. Decl_Node is the type declaration node where
342 -- we will attach all the itypes created. Typ is the base discriminated
343 -- type for the subtype Subt. Constraints is the list of discriminant
344 -- constraints for Typ.
345
346 function Constrain_Component_Type
347 (Comp : Entity_Id;
348 Constrained_Typ : Entity_Id;
349 Related_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id) return Entity_Id;
352 -- Given a discriminated base type Typ, a list of discriminant constraint
353 -- Constraints for Typ and a component of Typ, with type Compon_Type,
354 -- create and return the type corresponding to Compon_type where all
355 -- discriminant references are replaced with the corresponding constraint.
356 -- If no discriminant references occur in Compon_Typ then return it as is.
357 -- Constrained_Typ is the final constrained subtype to which the
358 -- constrained Compon_Type belongs. Related_Node is the node where we will
359 -- attach all the itypes created.
360 --
361 -- Above description is confused, what is Compon_Type???
362
363 procedure Constrain_Access
364 (Def_Id : in out Entity_Id;
365 S : Node_Id;
366 Related_Nod : Node_Id);
367 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
368 -- an anonymous type created for a subtype indication. In that case it is
369 -- created in the procedure and attached to Related_Nod.
370
371 procedure Constrain_Array
372 (Def_Id : in out Entity_Id;
373 SI : Node_Id;
374 Related_Nod : Node_Id;
375 Related_Id : Entity_Id;
376 Suffix : Character);
377 -- Apply a list of index constraints to an unconstrained array type. The
378 -- first parameter is the entity for the resulting subtype. A value of
379 -- Empty for Def_Id indicates that an implicit type must be created, but
380 -- creation is delayed (and must be done by this procedure) because other
381 -- subsidiary implicit types must be created first (which is why Def_Id
382 -- is an in/out parameter). The second parameter is a subtype indication
383 -- node for the constrained array to be created (e.g. something of the
384 -- form string (1 .. 10)). Related_Nod gives the place where this type
385 -- has to be inserted in the tree. The Related_Id and Suffix parameters
386 -- are used to build the associated Implicit type name.
387
388 procedure Constrain_Concurrent
389 (Def_Id : in out Entity_Id;
390 SI : Node_Id;
391 Related_Nod : Node_Id;
392 Related_Id : Entity_Id;
393 Suffix : Character);
394 -- Apply list of discriminant constraints to an unconstrained concurrent
395 -- type.
396 --
397 -- SI is the N_Subtype_Indication node containing the constraint and
398 -- the unconstrained type to constrain.
399 --
400 -- Def_Id is the entity for the resulting constrained subtype. A value
401 -- of Empty for Def_Id indicates that an implicit type must be created,
402 -- but creation is delayed (and must be done by this procedure) because
403 -- other subsidiary implicit types must be created first (which is why
404 -- Def_Id is an in/out parameter).
405 --
406 -- Related_Nod gives the place where this type has to be inserted
407 -- in the tree
408 --
409 -- The last two arguments are used to create its external name if needed.
410
411 function Constrain_Corresponding_Record
412 (Prot_Subt : Entity_Id;
413 Corr_Rec : Entity_Id;
414 Related_Nod : Node_Id;
415 Related_Id : Entity_Id) return Entity_Id;
416 -- When constraining a protected type or task type with discriminants,
417 -- constrain the corresponding record with the same discriminant values.
418
419 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
420 -- Constrain a decimal fixed point type with a digits constraint and/or a
421 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
422
423 procedure Constrain_Discriminated_Type
424 (Def_Id : Entity_Id;
425 S : Node_Id;
426 Related_Nod : Node_Id;
427 For_Access : Boolean := False);
428 -- Process discriminant constraints of composite type. Verify that values
429 -- have been provided for all discriminants, that the original type is
430 -- unconstrained, and that the types of the supplied expressions match
431 -- the discriminant types. The first three parameters are like in routine
432 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
433 -- of For_Access.
434
435 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
436 -- Constrain an enumeration type with a range constraint. This is identical
437 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
438
439 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
440 -- Constrain a floating point type with either a digits constraint
441 -- and/or a range constraint, building a E_Floating_Point_Subtype.
442
443 procedure Constrain_Index
444 (Index : Node_Id;
445 S : Node_Id;
446 Related_Nod : Node_Id;
447 Related_Id : Entity_Id;
448 Suffix : Character;
449 Suffix_Index : Nat);
450 -- Process an index constraint S in a constrained array declaration. The
451 -- constraint can be a subtype name, or a range with or without an explicit
452 -- subtype mark. The index is the corresponding index of the unconstrained
453 -- array. The Related_Id and Suffix parameters are used to build the
454 -- associated Implicit type name.
455
456 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
457 -- Build subtype of a signed or modular integer type
458
459 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
460 -- Constrain an ordinary fixed point type with a range constraint, and
461 -- build an E_Ordinary_Fixed_Point_Subtype entity.
462
463 procedure Copy_And_Swap (Priv, Full : Entity_Id);
464 -- Copy the Priv entity into the entity of its full declaration then swap
465 -- the two entities in such a manner that the former private type is now
466 -- seen as a full type.
467
468 procedure Decimal_Fixed_Point_Type_Declaration
469 (T : Entity_Id;
470 Def : Node_Id);
471 -- Create a new decimal fixed point type, and apply the constraint to
472 -- obtain a subtype of this new type.
473
474 procedure Complete_Private_Subtype
475 (Priv : Entity_Id;
476 Full : Entity_Id;
477 Full_Base : Entity_Id;
478 Related_Nod : Node_Id);
479 -- Complete the implicit full view of a private subtype by setting the
480 -- appropriate semantic fields. If the full view of the parent is a record
481 -- type, build constrained components of subtype.
482
483 procedure Derive_Progenitor_Subprograms
484 (Parent_Type : Entity_Id;
485 Tagged_Type : Entity_Id);
486 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
487 -- operations of progenitors of Tagged_Type, and replace the subsidiary
488 -- subtypes with Tagged_Type, to build the specs of the inherited interface
489 -- primitives. The derived primitives are aliased to those of the
490 -- interface. This routine takes care also of transferring to the full view
491 -- subprograms associated with the partial view of Tagged_Type that cover
492 -- interface primitives.
493
494 procedure Derived_Standard_Character
495 (N : Node_Id;
496 Parent_Type : Entity_Id;
497 Derived_Type : Entity_Id);
498 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
499 -- derivations from types Standard.Character and Standard.Wide_Character.
500
501 procedure Derived_Type_Declaration
502 (T : Entity_Id;
503 N : Node_Id;
504 Is_Completion : Boolean);
505 -- Process a derived type declaration. Build_Derived_Type is invoked
506 -- to process the actual derived type definition. Parameters N and
507 -- Is_Completion have the same meaning as in Build_Derived_Type.
508 -- T is the N_Defining_Identifier for the entity defined in the
509 -- N_Full_Type_Declaration node N, that is T is the derived type.
510
511 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
512 -- Insert each literal in symbol table, as an overloadable identifier. Each
513 -- enumeration type is mapped into a sequence of integers, and each literal
514 -- is defined as a constant with integer value. If any of the literals are
515 -- character literals, the type is a character type, which means that
516 -- strings are legal aggregates for arrays of components of the type.
517
518 function Expand_To_Stored_Constraint
519 (Typ : Entity_Id;
520 Constraint : Elist_Id) return Elist_Id;
521 -- Given a constraint (i.e. a list of expressions) on the discriminants of
522 -- Typ, expand it into a constraint on the stored discriminants and return
523 -- the new list of expressions constraining the stored discriminants.
524
525 function Find_Type_Of_Object
526 (Obj_Def : Node_Id;
527 Related_Nod : Node_Id) return Entity_Id;
528 -- Get type entity for object referenced by Obj_Def, attaching the
529 -- implicit types generated to Related_Nod
530
531 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
532 -- Create a new float and apply the constraint to obtain subtype of it
533
534 function Has_Range_Constraint (N : Node_Id) return Boolean;
535 -- Given an N_Subtype_Indication node N, return True if a range constraint
536 -- is present, either directly, or as part of a digits or delta constraint.
537 -- In addition, a digits constraint in the decimal case returns True, since
538 -- it establishes a default range if no explicit range is present.
539
540 function Inherit_Components
541 (N : Node_Id;
542 Parent_Base : Entity_Id;
543 Derived_Base : Entity_Id;
544 Is_Tagged : Boolean;
545 Inherit_Discr : Boolean;
546 Discs : Elist_Id) return Elist_Id;
547 -- Called from Build_Derived_Record_Type to inherit the components of
548 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
549 -- For more information on derived types and component inheritance please
550 -- consult the comment above the body of Build_Derived_Record_Type.
551 --
552 -- N is the original derived type declaration
553 --
554 -- Is_Tagged is set if we are dealing with tagged types
555 --
556 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
557 -- Parent_Base, otherwise no discriminants are inherited.
558 --
559 -- Discs gives the list of constraints that apply to Parent_Base in the
560 -- derived type declaration. If Discs is set to No_Elist, then we have
561 -- the following situation:
562 --
563 -- type Parent (D1..Dn : ..) is [tagged] record ...;
564 -- type Derived is new Parent [with ...];
565 --
566 -- which gets treated as
567 --
568 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
569 --
570 -- For untagged types the returned value is an association list. The list
571 -- starts from the association (Parent_Base => Derived_Base), and then it
572 -- contains a sequence of the associations of the form
573 --
574 -- (Old_Component => New_Component),
575 --
576 -- where Old_Component is the Entity_Id of a component in Parent_Base and
577 -- New_Component is the Entity_Id of the corresponding component in
578 -- Derived_Base. For untagged records, this association list is needed when
579 -- copying the record declaration for the derived base. In the tagged case
580 -- the value returned is irrelevant.
581
582 function Is_Valid_Constraint_Kind
583 (T_Kind : Type_Kind;
584 Constraint_Kind : Node_Kind) return Boolean;
585 -- Returns True if it is legal to apply the given kind of constraint to the
586 -- given kind of type (index constraint to an array type, for example).
587
588 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
589 -- Create new modular type. Verify that modulus is in bounds
590
591 procedure New_Concatenation_Op (Typ : Entity_Id);
592 -- Create an abbreviated declaration for an operator in order to
593 -- materialize concatenation on array types.
594
595 procedure Ordinary_Fixed_Point_Type_Declaration
596 (T : Entity_Id;
597 Def : Node_Id);
598 -- Create a new ordinary fixed point type, and apply the constraint to
599 -- obtain subtype of it.
600
601 procedure Prepare_Private_Subtype_Completion
602 (Id : Entity_Id;
603 Related_Nod : Node_Id);
604 -- Id is a subtype of some private type. Creates the full declaration
605 -- associated with Id whenever possible, i.e. when the full declaration
606 -- of the base type is already known. Records each subtype into
607 -- Private_Dependents of the base type.
608
609 procedure Process_Incomplete_Dependents
610 (N : Node_Id;
611 Full_T : Entity_Id;
612 Inc_T : Entity_Id);
613 -- Process all entities that depend on an incomplete type. There include
614 -- subtypes, subprogram types that mention the incomplete type in their
615 -- profiles, and subprogram with access parameters that designate the
616 -- incomplete type.
617
618 -- Inc_T is the defining identifier of an incomplete type declaration, its
619 -- Ekind is E_Incomplete_Type.
620 --
621 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
622 --
623 -- Full_T is N's defining identifier.
624 --
625 -- Subtypes of incomplete types with discriminants are completed when the
626 -- parent type is. This is simpler than private subtypes, because they can
627 -- only appear in the same scope, and there is no need to exchange views.
628 -- Similarly, access_to_subprogram types may have a parameter or a return
629 -- type that is an incomplete type, and that must be replaced with the
630 -- full type.
631 --
632 -- If the full type is tagged, subprogram with access parameters that
633 -- designated the incomplete may be primitive operations of the full type,
634 -- and have to be processed accordingly.
635
636 procedure Process_Real_Range_Specification (Def : Node_Id);
637 -- Given the type definition for a real type, this procedure processes and
638 -- checks the real range specification of this type definition if one is
639 -- present. If errors are found, error messages are posted, and the
640 -- Real_Range_Specification of Def is reset to Empty.
641
642 procedure Record_Type_Declaration
643 (T : Entity_Id;
644 N : Node_Id;
645 Prev : Entity_Id);
646 -- Process a record type declaration (for both untagged and tagged
647 -- records). Parameters T and N are exactly like in procedure
648 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
649 -- for this routine. If this is the completion of an incomplete type
650 -- declaration, Prev is the entity of the incomplete declaration, used for
651 -- cross-referencing. Otherwise Prev = T.
652
653 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
654 -- This routine is used to process the actual record type definition (both
655 -- for untagged and tagged records). Def is a record type definition node.
656 -- This procedure analyzes the components in this record type definition.
657 -- Prev_T is the entity for the enclosing record type. It is provided so
658 -- that its Has_Task flag can be set if any of the component have Has_Task
659 -- set. If the declaration is the completion of an incomplete type
660 -- declaration, Prev_T is the original incomplete type, whose full view is
661 -- the record type.
662
663 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
664 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
665 -- build a copy of the declaration tree of the parent, and we create
666 -- independently the list of components for the derived type. Semantic
667 -- information uses the component entities, but record representation
668 -- clauses are validated on the declaration tree. This procedure replaces
669 -- discriminants and components in the declaration with those that have
670 -- been created by Inherit_Components.
671
672 procedure Set_Fixed_Range
673 (E : Entity_Id;
674 Loc : Source_Ptr;
675 Lo : Ureal;
676 Hi : Ureal);
677 -- Build a range node with the given bounds and set it as the Scalar_Range
678 -- of the given fixed-point type entity. Loc is the source location used
679 -- for the constructed range. See body for further details.
680
681 procedure Set_Scalar_Range_For_Subtype
682 (Def_Id : Entity_Id;
683 R : Node_Id;
684 Subt : Entity_Id);
685 -- This routine is used to set the scalar range field for a subtype given
686 -- Def_Id, the entity for the subtype, and R, the range expression for the
687 -- scalar range. Subt provides the parent subtype to be used to analyze,
688 -- resolve, and check the given range.
689
690 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
691 -- Create a new signed integer entity, and apply the constraint to obtain
692 -- the required first named subtype of this type.
693
694 procedure Set_Stored_Constraint_From_Discriminant_Constraint
695 (E : Entity_Id);
696 -- E is some record type. This routine computes E's Stored_Constraint
697 -- from its Discriminant_Constraint.
698
699 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
700 -- Check that an entity in a list of progenitors is an interface,
701 -- emit error otherwise.
702
703 -----------------------
704 -- Access_Definition --
705 -----------------------
706
707 function Access_Definition
708 (Related_Nod : Node_Id;
709 N : Node_Id) return Entity_Id
710 is
711 Anon_Type : Entity_Id;
712 Anon_Scope : Entity_Id;
713 Desig_Type : Entity_Id;
714 Enclosing_Prot_Type : Entity_Id := Empty;
715
716 begin
717 Check_SPARK_Restriction ("access type is not allowed", N);
718
719 if Is_Entry (Current_Scope)
720 and then Is_Task_Type (Etype (Scope (Current_Scope)))
721 then
722 Error_Msg_N ("task entries cannot have access parameters", N);
723 return Empty;
724 end if;
725
726 -- Ada 2005: for an object declaration the corresponding anonymous
727 -- type is declared in the current scope.
728
729 -- If the access definition is the return type of another access to
730 -- function, scope is the current one, because it is the one of the
731 -- current type declaration, except for the pathological case below.
732
733 if Nkind_In (Related_Nod, N_Object_Declaration,
734 N_Access_Function_Definition)
735 then
736 Anon_Scope := Current_Scope;
737
738 -- A pathological case: function returning access functions that
739 -- return access functions, etc. Each anonymous access type created
740 -- is in the enclosing scope of the outermost function.
741
742 declare
743 Par : Node_Id;
744
745 begin
746 Par := Related_Nod;
747 while Nkind_In (Par, N_Access_Function_Definition,
748 N_Access_Definition)
749 loop
750 Par := Parent (Par);
751 end loop;
752
753 if Nkind (Par) = N_Function_Specification then
754 Anon_Scope := Scope (Defining_Entity (Par));
755 end if;
756 end;
757
758 -- For the anonymous function result case, retrieve the scope of the
759 -- function specification's associated entity rather than using the
760 -- current scope. The current scope will be the function itself if the
761 -- formal part is currently being analyzed, but will be the parent scope
762 -- in the case of a parameterless function, and we always want to use
763 -- the function's parent scope. Finally, if the function is a child
764 -- unit, we must traverse the tree to retrieve the proper entity.
765
766 elsif Nkind (Related_Nod) = N_Function_Specification
767 and then Nkind (Parent (N)) /= N_Parameter_Specification
768 then
769 -- If the current scope is a protected type, the anonymous access
770 -- is associated with one of the protected operations, and must
771 -- be available in the scope that encloses the protected declaration.
772 -- Otherwise the type is in the scope enclosing the subprogram.
773
774 -- If the function has formals, The return type of a subprogram
775 -- declaration is analyzed in the scope of the subprogram (see
776 -- Process_Formals) and thus the protected type, if present, is
777 -- the scope of the current function scope.
778
779 if Ekind (Current_Scope) = E_Protected_Type then
780 Enclosing_Prot_Type := Current_Scope;
781
782 elsif Ekind (Current_Scope) = E_Function
783 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
784 then
785 Enclosing_Prot_Type := Scope (Current_Scope);
786 end if;
787
788 if Present (Enclosing_Prot_Type) then
789 Anon_Scope := Scope (Enclosing_Prot_Type);
790
791 else
792 Anon_Scope := Scope (Defining_Entity (Related_Nod));
793 end if;
794
795 -- For an access type definition, if the current scope is a child
796 -- unit it is the scope of the type.
797
798 elsif Is_Compilation_Unit (Current_Scope) then
799 Anon_Scope := Current_Scope;
800
801 -- For access formals, access components, and access discriminants, the
802 -- scope is that of the enclosing declaration,
803
804 else
805 Anon_Scope := Scope (Current_Scope);
806 end if;
807
808 Anon_Type :=
809 Create_Itype
810 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
811
812 if All_Present (N)
813 and then Ada_Version >= Ada_2005
814 then
815 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
816 end if;
817
818 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
819 -- the corresponding semantic routine
820
821 if Present (Access_To_Subprogram_Definition (N)) then
822
823 -- Compiler runtime units are compiled in Ada 2005 mode when building
824 -- the runtime library but must also be compilable in Ada 95 mode
825 -- (when bootstrapping the compiler).
826
827 Check_Compiler_Unit (N);
828
829 Access_Subprogram_Declaration
830 (T_Name => Anon_Type,
831 T_Def => Access_To_Subprogram_Definition (N));
832
833 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
834 Set_Ekind
835 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
836 else
837 Set_Ekind
838 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
839 end if;
840
841 Set_Can_Use_Internal_Rep
842 (Anon_Type, not Always_Compatible_Rep_On_Target);
843
844 -- If the anonymous access is associated with a protected operation,
845 -- create a reference to it after the enclosing protected definition
846 -- because the itype will be used in the subsequent bodies.
847
848 if Ekind (Current_Scope) = E_Protected_Type then
849 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
850 end if;
851
852 return Anon_Type;
853 end if;
854
855 Find_Type (Subtype_Mark (N));
856 Desig_Type := Entity (Subtype_Mark (N));
857
858 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
859 Set_Etype (Anon_Type, Anon_Type);
860
861 -- Make sure the anonymous access type has size and alignment fields
862 -- set, as required by gigi. This is necessary in the case of the
863 -- Task_Body_Procedure.
864
865 if not Has_Private_Component (Desig_Type) then
866 Layout_Type (Anon_Type);
867 end if;
868
869 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
870 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
871 -- the null value is allowed. In Ada 95 the null value is never allowed.
872
873 if Ada_Version >= Ada_2005 then
874 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
875 else
876 Set_Can_Never_Be_Null (Anon_Type, True);
877 end if;
878
879 -- The anonymous access type is as public as the discriminated type or
880 -- subprogram that defines it. It is imported (for back-end purposes)
881 -- if the designated type is.
882
883 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
884
885 -- Ada 2005 (AI-231): Propagate the access-constant attribute
886
887 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
888
889 -- The context is either a subprogram declaration, object declaration,
890 -- or an access discriminant, in a private or a full type declaration.
891 -- In the case of a subprogram, if the designated type is incomplete,
892 -- the operation will be a primitive operation of the full type, to be
893 -- updated subsequently. If the type is imported through a limited_with
894 -- clause, the subprogram is not a primitive operation of the type
895 -- (which is declared elsewhere in some other scope).
896
897 if Ekind (Desig_Type) = E_Incomplete_Type
898 and then not From_With_Type (Desig_Type)
899 and then Is_Overloadable (Current_Scope)
900 then
901 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
902 Set_Has_Delayed_Freeze (Current_Scope);
903 end if;
904
905 -- Ada 2005: if the designated type is an interface that may contain
906 -- tasks, create a Master entity for the declaration. This must be done
907 -- before expansion of the full declaration, because the declaration may
908 -- include an expression that is an allocator, whose expansion needs the
909 -- proper Master for the created tasks.
910
911 if Nkind (Related_Nod) = N_Object_Declaration
912 and then Expander_Active
913 then
914 if Is_Interface (Desig_Type)
915 and then Is_Limited_Record (Desig_Type)
916 then
917 Build_Class_Wide_Master (Anon_Type);
918
919 -- Similarly, if the type is an anonymous access that designates
920 -- tasks, create a master entity for it in the current context.
921
922 elsif Has_Task (Desig_Type)
923 and then Comes_From_Source (Related_Nod)
924 then
925 Build_Master_Entity (Defining_Identifier (Related_Nod));
926 Build_Master_Renaming (Anon_Type);
927 end if;
928 end if;
929
930 -- For a private component of a protected type, it is imperative that
931 -- the back-end elaborate the type immediately after the protected
932 -- declaration, because this type will be used in the declarations
933 -- created for the component within each protected body, so we must
934 -- create an itype reference for it now.
935
936 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
937 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
938
939 -- Similarly, if the access definition is the return result of a
940 -- function, create an itype reference for it because it will be used
941 -- within the function body. For a regular function that is not a
942 -- compilation unit, insert reference after the declaration. For a
943 -- protected operation, insert it after the enclosing protected type
944 -- declaration. In either case, do not create a reference for a type
945 -- obtained through a limited_with clause, because this would introduce
946 -- semantic dependencies.
947
948 -- Similarly, do not create a reference if the designated type is a
949 -- generic formal, because no use of it will reach the backend.
950
951 elsif Nkind (Related_Nod) = N_Function_Specification
952 and then not From_With_Type (Desig_Type)
953 and then not Is_Generic_Type (Desig_Type)
954 then
955 if Present (Enclosing_Prot_Type) then
956 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
957
958 elsif Is_List_Member (Parent (Related_Nod))
959 and then Nkind (Parent (N)) /= N_Parameter_Specification
960 then
961 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
962 end if;
963
964 -- Finally, create an itype reference for an object declaration of an
965 -- anonymous access type. This is strictly necessary only for deferred
966 -- constants, but in any case will avoid out-of-scope problems in the
967 -- back-end.
968
969 elsif Nkind (Related_Nod) = N_Object_Declaration then
970 Build_Itype_Reference (Anon_Type, Related_Nod);
971 end if;
972
973 return Anon_Type;
974 end Access_Definition;
975
976 -----------------------------------
977 -- Access_Subprogram_Declaration --
978 -----------------------------------
979
980 procedure Access_Subprogram_Declaration
981 (T_Name : Entity_Id;
982 T_Def : Node_Id)
983 is
984
985 procedure Check_For_Premature_Usage (Def : Node_Id);
986 -- Check that type T_Name is not used, directly or recursively, as a
987 -- parameter or a return type in Def. Def is either a subtype, an
988 -- access_definition, or an access_to_subprogram_definition.
989
990 -------------------------------
991 -- Check_For_Premature_Usage --
992 -------------------------------
993
994 procedure Check_For_Premature_Usage (Def : Node_Id) is
995 Param : Node_Id;
996
997 begin
998 -- Check for a subtype mark
999
1000 if Nkind (Def) in N_Has_Etype then
1001 if Etype (Def) = T_Name then
1002 Error_Msg_N
1003 ("type& cannot be used before end of its declaration", Def);
1004 end if;
1005
1006 -- If this is not a subtype, then this is an access_definition
1007
1008 elsif Nkind (Def) = N_Access_Definition then
1009 if Present (Access_To_Subprogram_Definition (Def)) then
1010 Check_For_Premature_Usage
1011 (Access_To_Subprogram_Definition (Def));
1012 else
1013 Check_For_Premature_Usage (Subtype_Mark (Def));
1014 end if;
1015
1016 -- The only cases left are N_Access_Function_Definition and
1017 -- N_Access_Procedure_Definition.
1018
1019 else
1020 if Present (Parameter_Specifications (Def)) then
1021 Param := First (Parameter_Specifications (Def));
1022 while Present (Param) loop
1023 Check_For_Premature_Usage (Parameter_Type (Param));
1024 Param := Next (Param);
1025 end loop;
1026 end if;
1027
1028 if Nkind (Def) = N_Access_Function_Definition then
1029 Check_For_Premature_Usage (Result_Definition (Def));
1030 end if;
1031 end if;
1032 end Check_For_Premature_Usage;
1033
1034 -- Local variables
1035
1036 Formals : constant List_Id := Parameter_Specifications (T_Def);
1037 Formal : Entity_Id;
1038 D_Ityp : Node_Id;
1039 Desig_Type : constant Entity_Id :=
1040 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1041
1042 -- Start of processing for Access_Subprogram_Declaration
1043
1044 begin
1045 Check_SPARK_Restriction ("access type is not allowed", T_Def);
1046
1047 -- Associate the Itype node with the inner full-type declaration or
1048 -- subprogram spec or entry body. This is required to handle nested
1049 -- anonymous declarations. For example:
1050
1051 -- procedure P
1052 -- (X : access procedure
1053 -- (Y : access procedure
1054 -- (Z : access T)))
1055
1056 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1057 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1058 N_Private_Type_Declaration,
1059 N_Private_Extension_Declaration,
1060 N_Procedure_Specification,
1061 N_Function_Specification,
1062 N_Entry_Body)
1063
1064 or else
1065 Nkind_In (D_Ityp, N_Object_Declaration,
1066 N_Object_Renaming_Declaration,
1067 N_Formal_Object_Declaration,
1068 N_Formal_Type_Declaration,
1069 N_Task_Type_Declaration,
1070 N_Protected_Type_Declaration))
1071 loop
1072 D_Ityp := Parent (D_Ityp);
1073 pragma Assert (D_Ityp /= Empty);
1074 end loop;
1075
1076 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1077
1078 if Nkind_In (D_Ityp, N_Procedure_Specification,
1079 N_Function_Specification)
1080 then
1081 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1082
1083 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1084 N_Object_Declaration,
1085 N_Object_Renaming_Declaration,
1086 N_Formal_Type_Declaration)
1087 then
1088 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1089 end if;
1090
1091 if Nkind (T_Def) = N_Access_Function_Definition then
1092 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1093 declare
1094 Acc : constant Node_Id := Result_Definition (T_Def);
1095
1096 begin
1097 if Present (Access_To_Subprogram_Definition (Acc))
1098 and then
1099 Protected_Present (Access_To_Subprogram_Definition (Acc))
1100 then
1101 Set_Etype
1102 (Desig_Type,
1103 Replace_Anonymous_Access_To_Protected_Subprogram
1104 (T_Def));
1105
1106 else
1107 Set_Etype
1108 (Desig_Type,
1109 Access_Definition (T_Def, Result_Definition (T_Def)));
1110 end if;
1111 end;
1112
1113 else
1114 Analyze (Result_Definition (T_Def));
1115
1116 declare
1117 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1118
1119 begin
1120 -- If a null exclusion is imposed on the result type, then
1121 -- create a null-excluding itype (an access subtype) and use
1122 -- it as the function's Etype.
1123
1124 if Is_Access_Type (Typ)
1125 and then Null_Exclusion_In_Return_Present (T_Def)
1126 then
1127 Set_Etype (Desig_Type,
1128 Create_Null_Excluding_Itype
1129 (T => Typ,
1130 Related_Nod => T_Def,
1131 Scope_Id => Current_Scope));
1132
1133 else
1134 if From_With_Type (Typ) then
1135
1136 -- AI05-151: Incomplete types are allowed in all basic
1137 -- declarations, including access to subprograms.
1138
1139 if Ada_Version >= Ada_2012 then
1140 null;
1141
1142 else
1143 Error_Msg_NE
1144 ("illegal use of incomplete type&",
1145 Result_Definition (T_Def), Typ);
1146 end if;
1147
1148 elsif Ekind (Current_Scope) = E_Package
1149 and then In_Private_Part (Current_Scope)
1150 then
1151 if Ekind (Typ) = E_Incomplete_Type then
1152 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1153
1154 elsif Is_Class_Wide_Type (Typ)
1155 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1156 then
1157 Append_Elmt
1158 (Desig_Type, Private_Dependents (Etype (Typ)));
1159 end if;
1160 end if;
1161
1162 Set_Etype (Desig_Type, Typ);
1163 end if;
1164 end;
1165 end if;
1166
1167 if not (Is_Type (Etype (Desig_Type))) then
1168 Error_Msg_N
1169 ("expect type in function specification",
1170 Result_Definition (T_Def));
1171 end if;
1172
1173 else
1174 Set_Etype (Desig_Type, Standard_Void_Type);
1175 end if;
1176
1177 if Present (Formals) then
1178 Push_Scope (Desig_Type);
1179
1180 -- A bit of a kludge here. These kludges will be removed when Itypes
1181 -- have proper parent pointers to their declarations???
1182
1183 -- Kludge 1) Link defining_identifier of formals. Required by
1184 -- First_Formal to provide its functionality.
1185
1186 declare
1187 F : Node_Id;
1188
1189 begin
1190 F := First (Formals);
1191
1192 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1193 -- when it is part of an unconstrained type and subtype expansion
1194 -- is disabled. To avoid back-end problems with shared profiles,
1195 -- use previous subprogram type as the designated type, and then
1196 -- remove scope added above.
1197
1198 if ASIS_Mode
1199 and then Present (Scope (Defining_Identifier (F)))
1200 then
1201 Set_Etype (T_Name, T_Name);
1202 Init_Size_Align (T_Name);
1203 Set_Directly_Designated_Type (T_Name,
1204 Scope (Defining_Identifier (F)));
1205 End_Scope;
1206 return;
1207 end if;
1208
1209 while Present (F) loop
1210 if No (Parent (Defining_Identifier (F))) then
1211 Set_Parent (Defining_Identifier (F), F);
1212 end if;
1213
1214 Next (F);
1215 end loop;
1216 end;
1217
1218 Process_Formals (Formals, Parent (T_Def));
1219
1220 -- Kludge 2) End_Scope requires that the parent pointer be set to
1221 -- something reasonable, but Itypes don't have parent pointers. So
1222 -- we set it and then unset it ???
1223
1224 Set_Parent (Desig_Type, T_Name);
1225 End_Scope;
1226 Set_Parent (Desig_Type, Empty);
1227 end if;
1228
1229 -- Check for premature usage of the type being defined
1230
1231 Check_For_Premature_Usage (T_Def);
1232
1233 -- The return type and/or any parameter type may be incomplete. Mark the
1234 -- subprogram_type as depending on the incomplete type, so that it can
1235 -- be updated when the full type declaration is seen. This only applies
1236 -- to incomplete types declared in some enclosing scope, not to limited
1237 -- views from other packages.
1238
1239 if Present (Formals) then
1240 Formal := First_Formal (Desig_Type);
1241 while Present (Formal) loop
1242 if Ekind (Formal) /= E_In_Parameter
1243 and then Nkind (T_Def) = N_Access_Function_Definition
1244 then
1245 Error_Msg_N ("functions can only have IN parameters", Formal);
1246 end if;
1247
1248 if Ekind (Etype (Formal)) = E_Incomplete_Type
1249 and then In_Open_Scopes (Scope (Etype (Formal)))
1250 then
1251 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1252 Set_Has_Delayed_Freeze (Desig_Type);
1253 end if;
1254
1255 Next_Formal (Formal);
1256 end loop;
1257 end if;
1258
1259 -- Check whether an indirect call without actuals may be possible. This
1260 -- is used when resolving calls whose result is then indexed.
1261
1262 May_Need_Actuals (Desig_Type);
1263
1264 -- If the return type is incomplete, this is legal as long as the type
1265 -- is declared in the current scope and will be completed in it (rather
1266 -- than being part of limited view).
1267
1268 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1269 and then not Has_Delayed_Freeze (Desig_Type)
1270 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1271 then
1272 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1273 Set_Has_Delayed_Freeze (Desig_Type);
1274 end if;
1275
1276 Check_Delayed_Subprogram (Desig_Type);
1277
1278 if Protected_Present (T_Def) then
1279 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1280 Set_Convention (Desig_Type, Convention_Protected);
1281 else
1282 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1283 end if;
1284
1285 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1286
1287 Set_Etype (T_Name, T_Name);
1288 Init_Size_Align (T_Name);
1289 Set_Directly_Designated_Type (T_Name, Desig_Type);
1290
1291 Generate_Reference_To_Formals (T_Name);
1292
1293 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1294
1295 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1296
1297 Check_Restriction (No_Access_Subprograms, T_Def);
1298 end Access_Subprogram_Declaration;
1299
1300 ----------------------------
1301 -- Access_Type_Declaration --
1302 ----------------------------
1303
1304 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1305 P : constant Node_Id := Parent (Def);
1306 S : constant Node_Id := Subtype_Indication (Def);
1307
1308 Full_Desig : Entity_Id;
1309
1310 begin
1311 Check_SPARK_Restriction ("access type is not allowed", Def);
1312
1313 -- Check for permissible use of incomplete type
1314
1315 if Nkind (S) /= N_Subtype_Indication then
1316 Analyze (S);
1317
1318 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1319 Set_Directly_Designated_Type (T, Entity (S));
1320 else
1321 Set_Directly_Designated_Type (T,
1322 Process_Subtype (S, P, T, 'P'));
1323 end if;
1324
1325 else
1326 Set_Directly_Designated_Type (T,
1327 Process_Subtype (S, P, T, 'P'));
1328 end if;
1329
1330 if All_Present (Def) or Constant_Present (Def) then
1331 Set_Ekind (T, E_General_Access_Type);
1332 else
1333 Set_Ekind (T, E_Access_Type);
1334 end if;
1335
1336 Full_Desig := Designated_Type (T);
1337
1338 if Base_Type (Full_Desig) = T then
1339 Error_Msg_N ("access type cannot designate itself", S);
1340
1341 -- In Ada 2005, the type may have a limited view through some unit in
1342 -- its own context, allowing the following circularity that cannot be
1343 -- detected earlier
1344
1345 elsif Is_Class_Wide_Type (Full_Desig)
1346 and then Etype (Full_Desig) = T
1347 then
1348 Error_Msg_N
1349 ("access type cannot designate its own classwide type", S);
1350
1351 -- Clean up indication of tagged status to prevent cascaded errors
1352
1353 Set_Is_Tagged_Type (T, False);
1354 end if;
1355
1356 Set_Etype (T, T);
1357
1358 -- If the type has appeared already in a with_type clause, it is frozen
1359 -- and the pointer size is already set. Else, initialize.
1360
1361 if not From_With_Type (T) then
1362 Init_Size_Align (T);
1363 end if;
1364
1365 -- Note that Has_Task is always false, since the access type itself
1366 -- is not a task type. See Einfo for more description on this point.
1367 -- Exactly the same consideration applies to Has_Controlled_Component.
1368
1369 Set_Has_Task (T, False);
1370 Set_Has_Controlled_Component (T, False);
1371
1372 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1373 -- problems where an incomplete view of this entity has been previously
1374 -- established by a limited with and an overlaid version of this field
1375 -- (Stored_Constraint) was initialized for the incomplete view.
1376
1377 -- This reset is performed in most cases except where the access type
1378 -- has been created for the purposes of allocating or deallocating a
1379 -- build-in-place object. Such access types have explicitly set pools
1380 -- and finalization masters.
1381
1382 if No (Associated_Storage_Pool (T)) then
1383 Set_Finalization_Master (T, Empty);
1384 end if;
1385
1386 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1387 -- attributes
1388
1389 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1390 Set_Is_Access_Constant (T, Constant_Present (Def));
1391 end Access_Type_Declaration;
1392
1393 ----------------------------------
1394 -- Add_Interface_Tag_Components --
1395 ----------------------------------
1396
1397 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1398 Loc : constant Source_Ptr := Sloc (N);
1399 L : List_Id;
1400 Last_Tag : Node_Id;
1401
1402 procedure Add_Tag (Iface : Entity_Id);
1403 -- Add tag for one of the progenitor interfaces
1404
1405 -------------
1406 -- Add_Tag --
1407 -------------
1408
1409 procedure Add_Tag (Iface : Entity_Id) is
1410 Decl : Node_Id;
1411 Def : Node_Id;
1412 Tag : Entity_Id;
1413 Offset : Entity_Id;
1414
1415 begin
1416 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1417
1418 -- This is a reasonable place to propagate predicates
1419
1420 if Has_Predicates (Iface) then
1421 Set_Has_Predicates (Typ);
1422 end if;
1423
1424 Def :=
1425 Make_Component_Definition (Loc,
1426 Aliased_Present => True,
1427 Subtype_Indication =>
1428 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1429
1430 Tag := Make_Temporary (Loc, 'V');
1431
1432 Decl :=
1433 Make_Component_Declaration (Loc,
1434 Defining_Identifier => Tag,
1435 Component_Definition => Def);
1436
1437 Analyze_Component_Declaration (Decl);
1438
1439 Set_Analyzed (Decl);
1440 Set_Ekind (Tag, E_Component);
1441 Set_Is_Tag (Tag);
1442 Set_Is_Aliased (Tag);
1443 Set_Related_Type (Tag, Iface);
1444 Init_Component_Location (Tag);
1445
1446 pragma Assert (Is_Frozen (Iface));
1447
1448 Set_DT_Entry_Count (Tag,
1449 DT_Entry_Count (First_Entity (Iface)));
1450
1451 if No (Last_Tag) then
1452 Prepend (Decl, L);
1453 else
1454 Insert_After (Last_Tag, Decl);
1455 end if;
1456
1457 Last_Tag := Decl;
1458
1459 -- If the ancestor has discriminants we need to give special support
1460 -- to store the offset_to_top value of the secondary dispatch tables.
1461 -- For this purpose we add a supplementary component just after the
1462 -- field that contains the tag associated with each secondary DT.
1463
1464 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1465 Def :=
1466 Make_Component_Definition (Loc,
1467 Subtype_Indication =>
1468 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1469
1470 Offset := Make_Temporary (Loc, 'V');
1471
1472 Decl :=
1473 Make_Component_Declaration (Loc,
1474 Defining_Identifier => Offset,
1475 Component_Definition => Def);
1476
1477 Analyze_Component_Declaration (Decl);
1478
1479 Set_Analyzed (Decl);
1480 Set_Ekind (Offset, E_Component);
1481 Set_Is_Aliased (Offset);
1482 Set_Related_Type (Offset, Iface);
1483 Init_Component_Location (Offset);
1484 Insert_After (Last_Tag, Decl);
1485 Last_Tag := Decl;
1486 end if;
1487 end Add_Tag;
1488
1489 -- Local variables
1490
1491 Elmt : Elmt_Id;
1492 Ext : Node_Id;
1493 Comp : Node_Id;
1494
1495 -- Start of processing for Add_Interface_Tag_Components
1496
1497 begin
1498 if not RTE_Available (RE_Interface_Tag) then
1499 Error_Msg
1500 ("(Ada 2005) interface types not supported by this run-time!",
1501 Sloc (N));
1502 return;
1503 end if;
1504
1505 if Ekind (Typ) /= E_Record_Type
1506 or else (Is_Concurrent_Record_Type (Typ)
1507 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1508 or else (not Is_Concurrent_Record_Type (Typ)
1509 and then No (Interfaces (Typ))
1510 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1511 then
1512 return;
1513 end if;
1514
1515 -- Find the current last tag
1516
1517 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1518 Ext := Record_Extension_Part (Type_Definition (N));
1519 else
1520 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1521 Ext := Type_Definition (N);
1522 end if;
1523
1524 Last_Tag := Empty;
1525
1526 if not (Present (Component_List (Ext))) then
1527 Set_Null_Present (Ext, False);
1528 L := New_List;
1529 Set_Component_List (Ext,
1530 Make_Component_List (Loc,
1531 Component_Items => L,
1532 Null_Present => False));
1533 else
1534 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1535 L := Component_Items
1536 (Component_List
1537 (Record_Extension_Part
1538 (Type_Definition (N))));
1539 else
1540 L := Component_Items
1541 (Component_List
1542 (Type_Definition (N)));
1543 end if;
1544
1545 -- Find the last tag component
1546
1547 Comp := First (L);
1548 while Present (Comp) loop
1549 if Nkind (Comp) = N_Component_Declaration
1550 and then Is_Tag (Defining_Identifier (Comp))
1551 then
1552 Last_Tag := Comp;
1553 end if;
1554
1555 Next (Comp);
1556 end loop;
1557 end if;
1558
1559 -- At this point L references the list of components and Last_Tag
1560 -- references the current last tag (if any). Now we add the tag
1561 -- corresponding with all the interfaces that are not implemented
1562 -- by the parent.
1563
1564 if Present (Interfaces (Typ)) then
1565 Elmt := First_Elmt (Interfaces (Typ));
1566 while Present (Elmt) loop
1567 Add_Tag (Node (Elmt));
1568 Next_Elmt (Elmt);
1569 end loop;
1570 end if;
1571 end Add_Interface_Tag_Components;
1572
1573 -------------------------------------
1574 -- Add_Internal_Interface_Entities --
1575 -------------------------------------
1576
1577 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1578 Elmt : Elmt_Id;
1579 Iface : Entity_Id;
1580 Iface_Elmt : Elmt_Id;
1581 Iface_Prim : Entity_Id;
1582 Ifaces_List : Elist_Id;
1583 New_Subp : Entity_Id := Empty;
1584 Prim : Entity_Id;
1585 Restore_Scope : Boolean := False;
1586
1587 begin
1588 pragma Assert (Ada_Version >= Ada_2005
1589 and then Is_Record_Type (Tagged_Type)
1590 and then Is_Tagged_Type (Tagged_Type)
1591 and then Has_Interfaces (Tagged_Type)
1592 and then not Is_Interface (Tagged_Type));
1593
1594 -- Ensure that the internal entities are added to the scope of the type
1595
1596 if Scope (Tagged_Type) /= Current_Scope then
1597 Push_Scope (Scope (Tagged_Type));
1598 Restore_Scope := True;
1599 end if;
1600
1601 Collect_Interfaces (Tagged_Type, Ifaces_List);
1602
1603 Iface_Elmt := First_Elmt (Ifaces_List);
1604 while Present (Iface_Elmt) loop
1605 Iface := Node (Iface_Elmt);
1606
1607 -- Originally we excluded here from this processing interfaces that
1608 -- are parents of Tagged_Type because their primitives are located
1609 -- in the primary dispatch table (and hence no auxiliary internal
1610 -- entities are required to handle secondary dispatch tables in such
1611 -- case). However, these auxiliary entities are also required to
1612 -- handle derivations of interfaces in formals of generics (see
1613 -- Derive_Subprograms).
1614
1615 Elmt := First_Elmt (Primitive_Operations (Iface));
1616 while Present (Elmt) loop
1617 Iface_Prim := Node (Elmt);
1618
1619 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1620 Prim :=
1621 Find_Primitive_Covering_Interface
1622 (Tagged_Type => Tagged_Type,
1623 Iface_Prim => Iface_Prim);
1624
1625 if No (Prim) and then Serious_Errors_Detected > 0 then
1626 goto Continue;
1627 end if;
1628
1629 pragma Assert (Present (Prim));
1630
1631 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1632 -- differs from the name of the interface primitive then it is
1633 -- a private primitive inherited from a parent type. In such
1634 -- case, given that Tagged_Type covers the interface, the
1635 -- inherited private primitive becomes visible. For such
1636 -- purpose we add a new entity that renames the inherited
1637 -- private primitive.
1638
1639 if Chars (Prim) /= Chars (Iface_Prim) then
1640 pragma Assert (Has_Suffix (Prim, 'P'));
1641 Derive_Subprogram
1642 (New_Subp => New_Subp,
1643 Parent_Subp => Iface_Prim,
1644 Derived_Type => Tagged_Type,
1645 Parent_Type => Iface);
1646 Set_Alias (New_Subp, Prim);
1647 Set_Is_Abstract_Subprogram
1648 (New_Subp, Is_Abstract_Subprogram (Prim));
1649 end if;
1650
1651 Derive_Subprogram
1652 (New_Subp => New_Subp,
1653 Parent_Subp => Iface_Prim,
1654 Derived_Type => Tagged_Type,
1655 Parent_Type => Iface);
1656
1657 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1658 -- associated with interface types. These entities are
1659 -- only registered in the list of primitives of its
1660 -- corresponding tagged type because they are only used
1661 -- to fill the contents of the secondary dispatch tables.
1662 -- Therefore they are removed from the homonym chains.
1663
1664 Set_Is_Hidden (New_Subp);
1665 Set_Is_Internal (New_Subp);
1666 Set_Alias (New_Subp, Prim);
1667 Set_Is_Abstract_Subprogram
1668 (New_Subp, Is_Abstract_Subprogram (Prim));
1669 Set_Interface_Alias (New_Subp, Iface_Prim);
1670
1671 -- If the returned type is an interface then propagate it to
1672 -- the returned type. Needed by the thunk to generate the code
1673 -- which displaces "this" to reference the corresponding
1674 -- secondary dispatch table in the returned object.
1675
1676 if Is_Interface (Etype (Iface_Prim)) then
1677 Set_Etype (New_Subp, Etype (Iface_Prim));
1678 end if;
1679
1680 -- Internal entities associated with interface types are
1681 -- only registered in the list of primitives of the tagged
1682 -- type. They are only used to fill the contents of the
1683 -- secondary dispatch tables. Therefore they are not needed
1684 -- in the homonym chains.
1685
1686 Remove_Homonym (New_Subp);
1687
1688 -- Hidden entities associated with interfaces must have set
1689 -- the Has_Delay_Freeze attribute to ensure that, in case of
1690 -- locally defined tagged types (or compiling with static
1691 -- dispatch tables generation disabled) the corresponding
1692 -- entry of the secondary dispatch table is filled when
1693 -- such an entity is frozen.
1694
1695 Set_Has_Delayed_Freeze (New_Subp);
1696 end if;
1697
1698 <<Continue>>
1699 Next_Elmt (Elmt);
1700 end loop;
1701
1702 Next_Elmt (Iface_Elmt);
1703 end loop;
1704
1705 if Restore_Scope then
1706 Pop_Scope;
1707 end if;
1708 end Add_Internal_Interface_Entities;
1709
1710 -----------------------------------
1711 -- Analyze_Component_Declaration --
1712 -----------------------------------
1713
1714 procedure Analyze_Component_Declaration (N : Node_Id) is
1715 Id : constant Entity_Id := Defining_Identifier (N);
1716 E : constant Node_Id := Expression (N);
1717 Typ : constant Node_Id :=
1718 Subtype_Indication (Component_Definition (N));
1719 T : Entity_Id;
1720 P : Entity_Id;
1721
1722 function Contains_POC (Constr : Node_Id) return Boolean;
1723 -- Determines whether a constraint uses the discriminant of a record
1724 -- type thus becoming a per-object constraint (POC).
1725
1726 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1727 -- Typ is the type of the current component, check whether this type is
1728 -- a limited type. Used to validate declaration against that of
1729 -- enclosing record.
1730
1731 ------------------
1732 -- Contains_POC --
1733 ------------------
1734
1735 function Contains_POC (Constr : Node_Id) return Boolean is
1736 begin
1737 -- Prevent cascaded errors
1738
1739 if Error_Posted (Constr) then
1740 return False;
1741 end if;
1742
1743 case Nkind (Constr) is
1744 when N_Attribute_Reference =>
1745 return
1746 Attribute_Name (Constr) = Name_Access
1747 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1748
1749 when N_Discriminant_Association =>
1750 return Denotes_Discriminant (Expression (Constr));
1751
1752 when N_Identifier =>
1753 return Denotes_Discriminant (Constr);
1754
1755 when N_Index_Or_Discriminant_Constraint =>
1756 declare
1757 IDC : Node_Id;
1758
1759 begin
1760 IDC := First (Constraints (Constr));
1761 while Present (IDC) loop
1762
1763 -- One per-object constraint is sufficient
1764
1765 if Contains_POC (IDC) then
1766 return True;
1767 end if;
1768
1769 Next (IDC);
1770 end loop;
1771
1772 return False;
1773 end;
1774
1775 when N_Range =>
1776 return Denotes_Discriminant (Low_Bound (Constr))
1777 or else
1778 Denotes_Discriminant (High_Bound (Constr));
1779
1780 when N_Range_Constraint =>
1781 return Denotes_Discriminant (Range_Expression (Constr));
1782
1783 when others =>
1784 return False;
1785
1786 end case;
1787 end Contains_POC;
1788
1789 ----------------------
1790 -- Is_Known_Limited --
1791 ----------------------
1792
1793 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1794 P : constant Entity_Id := Etype (Typ);
1795 R : constant Entity_Id := Root_Type (Typ);
1796
1797 begin
1798 if Is_Limited_Record (Typ) then
1799 return True;
1800
1801 -- If the root type is limited (and not a limited interface)
1802 -- so is the current type
1803
1804 elsif Is_Limited_Record (R)
1805 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1806 then
1807 return True;
1808
1809 -- Else the type may have a limited interface progenitor, but a
1810 -- limited record parent.
1811
1812 elsif R /= P and then Is_Limited_Record (P) then
1813 return True;
1814
1815 else
1816 return False;
1817 end if;
1818 end Is_Known_Limited;
1819
1820 -- Start of processing for Analyze_Component_Declaration
1821
1822 begin
1823 Generate_Definition (Id);
1824 Enter_Name (Id);
1825
1826 if Present (Typ) then
1827 T := Find_Type_Of_Object
1828 (Subtype_Indication (Component_Definition (N)), N);
1829
1830 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1831 Check_SPARK_Restriction ("subtype mark required", Typ);
1832 end if;
1833
1834 -- Ada 2005 (AI-230): Access Definition case
1835
1836 else
1837 pragma Assert (Present
1838 (Access_Definition (Component_Definition (N))));
1839
1840 T := Access_Definition
1841 (Related_Nod => N,
1842 N => Access_Definition (Component_Definition (N)));
1843 Set_Is_Local_Anonymous_Access (T);
1844
1845 -- Ada 2005 (AI-254)
1846
1847 if Present (Access_To_Subprogram_Definition
1848 (Access_Definition (Component_Definition (N))))
1849 and then Protected_Present (Access_To_Subprogram_Definition
1850 (Access_Definition
1851 (Component_Definition (N))))
1852 then
1853 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1854 end if;
1855 end if;
1856
1857 -- If the subtype is a constrained subtype of the enclosing record,
1858 -- (which must have a partial view) the back-end does not properly
1859 -- handle the recursion. Rewrite the component declaration with an
1860 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1861 -- the tree directly because side effects have already been removed from
1862 -- discriminant constraints.
1863
1864 if Ekind (T) = E_Access_Subtype
1865 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1866 and then Comes_From_Source (T)
1867 and then Nkind (Parent (T)) = N_Subtype_Declaration
1868 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1869 then
1870 Rewrite
1871 (Subtype_Indication (Component_Definition (N)),
1872 New_Copy_Tree (Subtype_Indication (Parent (T))));
1873 T := Find_Type_Of_Object
1874 (Subtype_Indication (Component_Definition (N)), N);
1875 end if;
1876
1877 -- If the component declaration includes a default expression, then we
1878 -- check that the component is not of a limited type (RM 3.7(5)),
1879 -- and do the special preanalysis of the expression (see section on
1880 -- "Handling of Default and Per-Object Expressions" in the spec of
1881 -- package Sem).
1882
1883 if Present (E) then
1884 Check_SPARK_Restriction ("default expression is not allowed", E);
1885 Preanalyze_Spec_Expression (E, T);
1886 Check_Initialization (T, E);
1887
1888 if Ada_Version >= Ada_2005
1889 and then Ekind (T) = E_Anonymous_Access_Type
1890 and then Etype (E) /= Any_Type
1891 then
1892 -- Check RM 3.9.2(9): "if the expected type for an expression is
1893 -- an anonymous access-to-specific tagged type, then the object
1894 -- designated by the expression shall not be dynamically tagged
1895 -- unless it is a controlling operand in a call on a dispatching
1896 -- operation"
1897
1898 if Is_Tagged_Type (Directly_Designated_Type (T))
1899 and then
1900 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1901 and then
1902 Ekind (Directly_Designated_Type (Etype (E))) =
1903 E_Class_Wide_Type
1904 then
1905 Error_Msg_N
1906 ("access to specific tagged type required (RM 3.9.2(9))", E);
1907 end if;
1908
1909 -- (Ada 2005: AI-230): Accessibility check for anonymous
1910 -- components
1911
1912 if Type_Access_Level (Etype (E)) >
1913 Deepest_Type_Access_Level (T)
1914 then
1915 Error_Msg_N
1916 ("expression has deeper access level than component " &
1917 "(RM 3.10.2 (12.2))", E);
1918 end if;
1919
1920 -- The initialization expression is a reference to an access
1921 -- discriminant. The type of the discriminant is always deeper
1922 -- than any access type.
1923
1924 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1925 and then Is_Entity_Name (E)
1926 and then Ekind (Entity (E)) = E_In_Parameter
1927 and then Present (Discriminal_Link (Entity (E)))
1928 then
1929 Error_Msg_N
1930 ("discriminant has deeper accessibility level than target",
1931 E);
1932 end if;
1933 end if;
1934 end if;
1935
1936 -- The parent type may be a private view with unknown discriminants,
1937 -- and thus unconstrained. Regular components must be constrained.
1938
1939 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1940 if Is_Class_Wide_Type (T) then
1941 Error_Msg_N
1942 ("class-wide subtype with unknown discriminants" &
1943 " in component declaration",
1944 Subtype_Indication (Component_Definition (N)));
1945 else
1946 Error_Msg_N
1947 ("unconstrained subtype in component declaration",
1948 Subtype_Indication (Component_Definition (N)));
1949 end if;
1950
1951 -- Components cannot be abstract, except for the special case of
1952 -- the _Parent field (case of extending an abstract tagged type)
1953
1954 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1955 Error_Msg_N ("type of a component cannot be abstract", N);
1956 end if;
1957
1958 Set_Etype (Id, T);
1959 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1960
1961 -- The component declaration may have a per-object constraint, set
1962 -- the appropriate flag in the defining identifier of the subtype.
1963
1964 if Present (Subtype_Indication (Component_Definition (N))) then
1965 declare
1966 Sindic : constant Node_Id :=
1967 Subtype_Indication (Component_Definition (N));
1968 begin
1969 if Nkind (Sindic) = N_Subtype_Indication
1970 and then Present (Constraint (Sindic))
1971 and then Contains_POC (Constraint (Sindic))
1972 then
1973 Set_Has_Per_Object_Constraint (Id);
1974 end if;
1975 end;
1976 end if;
1977
1978 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1979 -- out some static checks.
1980
1981 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
1982 Null_Exclusion_Static_Checks (N);
1983 end if;
1984
1985 -- If this component is private (or depends on a private type), flag the
1986 -- record type to indicate that some operations are not available.
1987
1988 P := Private_Component (T);
1989
1990 if Present (P) then
1991
1992 -- Check for circular definitions
1993
1994 if P = Any_Type then
1995 Set_Etype (Id, Any_Type);
1996
1997 -- There is a gap in the visibility of operations only if the
1998 -- component type is not defined in the scope of the record type.
1999
2000 elsif Scope (P) = Scope (Current_Scope) then
2001 null;
2002
2003 elsif Is_Limited_Type (P) then
2004 Set_Is_Limited_Composite (Current_Scope);
2005
2006 else
2007 Set_Is_Private_Composite (Current_Scope);
2008 end if;
2009 end if;
2010
2011 if P /= Any_Type
2012 and then Is_Limited_Type (T)
2013 and then Chars (Id) /= Name_uParent
2014 and then Is_Tagged_Type (Current_Scope)
2015 then
2016 if Is_Derived_Type (Current_Scope)
2017 and then not Is_Known_Limited (Current_Scope)
2018 then
2019 Error_Msg_N
2020 ("extension of nonlimited type cannot have limited components",
2021 N);
2022
2023 if Is_Interface (Root_Type (Current_Scope)) then
2024 Error_Msg_N
2025 ("\limitedness is not inherited from limited interface", N);
2026 Error_Msg_N ("\add LIMITED to type indication", N);
2027 end if;
2028
2029 Explain_Limited_Type (T, N);
2030 Set_Etype (Id, Any_Type);
2031 Set_Is_Limited_Composite (Current_Scope, False);
2032
2033 elsif not Is_Derived_Type (Current_Scope)
2034 and then not Is_Limited_Record (Current_Scope)
2035 and then not Is_Concurrent_Type (Current_Scope)
2036 then
2037 Error_Msg_N
2038 ("nonlimited tagged type cannot have limited components", N);
2039 Explain_Limited_Type (T, N);
2040 Set_Etype (Id, Any_Type);
2041 Set_Is_Limited_Composite (Current_Scope, False);
2042 end if;
2043 end if;
2044
2045 Set_Original_Record_Component (Id, Id);
2046
2047 if Has_Aspects (N) then
2048 Analyze_Aspect_Specifications (N, Id);
2049 end if;
2050
2051 Analyze_Dimension (N);
2052 end Analyze_Component_Declaration;
2053
2054 --------------------------
2055 -- Analyze_Declarations --
2056 --------------------------
2057
2058 procedure Analyze_Declarations (L : List_Id) is
2059 Decl : Node_Id;
2060
2061 procedure Adjust_Decl;
2062 -- Adjust Decl not to include implicit label declarations, since these
2063 -- have strange Sloc values that result in elaboration check problems.
2064 -- (They have the sloc of the label as found in the source, and that
2065 -- is ahead of the current declarative part).
2066
2067 -----------------
2068 -- Adjust_Decl --
2069 -----------------
2070
2071 procedure Adjust_Decl is
2072 begin
2073 while Present (Prev (Decl))
2074 and then Nkind (Decl) = N_Implicit_Label_Declaration
2075 loop
2076 Prev (Decl);
2077 end loop;
2078 end Adjust_Decl;
2079
2080 -- Local variables
2081
2082 Freeze_From : Entity_Id := Empty;
2083 Next_Decl : Node_Id;
2084
2085 -- Start of processing for Analyze_Declarations
2086
2087 begin
2088 if Restriction_Check_Required (SPARK_05) then
2089 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2090 end if;
2091
2092 Decl := First (L);
2093 while Present (Decl) loop
2094
2095 -- Package spec cannot contain a package declaration in SPARK
2096
2097 if Nkind (Decl) = N_Package_Declaration
2098 and then Nkind (Parent (L)) = N_Package_Specification
2099 then
2100 Check_SPARK_Restriction
2101 ("package specification cannot contain a package declaration",
2102 Decl);
2103 end if;
2104
2105 -- Complete analysis of declaration
2106
2107 Analyze (Decl);
2108 Next_Decl := Next (Decl);
2109
2110 if No (Freeze_From) then
2111 Freeze_From := First_Entity (Current_Scope);
2112 end if;
2113
2114 -- At the end of a declarative part, freeze remaining entities
2115 -- declared in it. The end of the visible declarations of package
2116 -- specification is not the end of a declarative part if private
2117 -- declarations are present. The end of a package declaration is a
2118 -- freezing point only if it a library package. A task definition or
2119 -- protected type definition is not a freeze point either. Finally,
2120 -- we do not freeze entities in generic scopes, because there is no
2121 -- code generated for them and freeze nodes will be generated for
2122 -- the instance.
2123
2124 -- The end of a package instantiation is not a freeze point, but
2125 -- for now we make it one, because the generic body is inserted
2126 -- (currently) immediately after. Generic instantiations will not
2127 -- be a freeze point once delayed freezing of bodies is implemented.
2128 -- (This is needed in any case for early instantiations ???).
2129
2130 if No (Next_Decl) then
2131 if Nkind_In (Parent (L), N_Component_List,
2132 N_Task_Definition,
2133 N_Protected_Definition)
2134 then
2135 null;
2136
2137 elsif Nkind (Parent (L)) /= N_Package_Specification then
2138 if Nkind (Parent (L)) = N_Package_Body then
2139 Freeze_From := First_Entity (Current_Scope);
2140 end if;
2141
2142 Adjust_Decl;
2143 Freeze_All (Freeze_From, Decl);
2144 Freeze_From := Last_Entity (Current_Scope);
2145
2146 elsif Scope (Current_Scope) /= Standard_Standard
2147 and then not Is_Child_Unit (Current_Scope)
2148 and then No (Generic_Parent (Parent (L)))
2149 then
2150 null;
2151
2152 elsif L /= Visible_Declarations (Parent (L))
2153 or else No (Private_Declarations (Parent (L)))
2154 or else Is_Empty_List (Private_Declarations (Parent (L)))
2155 then
2156 Adjust_Decl;
2157 Freeze_All (Freeze_From, Decl);
2158 Freeze_From := Last_Entity (Current_Scope);
2159 end if;
2160
2161 -- If next node is a body then freeze all types before the body.
2162 -- An exception occurs for some expander-generated bodies. If these
2163 -- are generated at places where in general language rules would not
2164 -- allow a freeze point, then we assume that the expander has
2165 -- explicitly checked that all required types are properly frozen,
2166 -- and we do not cause general freezing here. This special circuit
2167 -- is used when the encountered body is marked as having already
2168 -- been analyzed.
2169
2170 -- In all other cases (bodies that come from source, and expander
2171 -- generated bodies that have not been analyzed yet), freeze all
2172 -- types now. Note that in the latter case, the expander must take
2173 -- care to attach the bodies at a proper place in the tree so as to
2174 -- not cause unwanted freezing at that point.
2175
2176 elsif not Analyzed (Next_Decl)
2177 and then (Nkind_In (Next_Decl, N_Subprogram_Body,
2178 N_Entry_Body,
2179 N_Package_Body,
2180 N_Protected_Body,
2181 N_Task_Body)
2182 or else
2183 Nkind (Next_Decl) in N_Body_Stub)
2184 then
2185 Adjust_Decl;
2186 Freeze_All (Freeze_From, Decl);
2187 Freeze_From := Last_Entity (Current_Scope);
2188 end if;
2189
2190 Decl := Next_Decl;
2191 end loop;
2192
2193 -- Analyze the contracts of a subprogram declaration or a body now due
2194 -- to delayed visibility requirements of aspects.
2195
2196 Decl := First (L);
2197 while Present (Decl) loop
2198 if Nkind (Decl) = N_Subprogram_Body then
2199 Analyze_Subprogram_Body_Contract
2200 (Defining_Unit_Name (Specification (Decl)));
2201
2202 elsif Nkind (Decl) = N_Subprogram_Declaration then
2203 Analyze_Subprogram_Contract
2204 (Defining_Unit_Name (Specification (Decl)));
2205 end if;
2206
2207 Next (Decl);
2208 end loop;
2209 end Analyze_Declarations;
2210
2211 -----------------------------------
2212 -- Analyze_Full_Type_Declaration --
2213 -----------------------------------
2214
2215 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2216 Def : constant Node_Id := Type_Definition (N);
2217 Def_Id : constant Entity_Id := Defining_Identifier (N);
2218 T : Entity_Id;
2219 Prev : Entity_Id;
2220
2221 Is_Remote : constant Boolean :=
2222 (Is_Remote_Types (Current_Scope)
2223 or else Is_Remote_Call_Interface (Current_Scope))
2224 and then not (In_Private_Part (Current_Scope)
2225 or else In_Package_Body (Current_Scope));
2226
2227 procedure Check_Ops_From_Incomplete_Type;
2228 -- If there is a tagged incomplete partial view of the type, traverse
2229 -- the primitives of the incomplete view and change the type of any
2230 -- controlling formals and result to indicate the full view. The
2231 -- primitives will be added to the full type's primitive operations
2232 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2233 -- is called from Process_Incomplete_Dependents).
2234
2235 ------------------------------------
2236 -- Check_Ops_From_Incomplete_Type --
2237 ------------------------------------
2238
2239 procedure Check_Ops_From_Incomplete_Type is
2240 Elmt : Elmt_Id;
2241 Formal : Entity_Id;
2242 Op : Entity_Id;
2243
2244 begin
2245 if Prev /= T
2246 and then Ekind (Prev) = E_Incomplete_Type
2247 and then Is_Tagged_Type (Prev)
2248 and then Is_Tagged_Type (T)
2249 then
2250 Elmt := First_Elmt (Primitive_Operations (Prev));
2251 while Present (Elmt) loop
2252 Op := Node (Elmt);
2253
2254 Formal := First_Formal (Op);
2255 while Present (Formal) loop
2256 if Etype (Formal) = Prev then
2257 Set_Etype (Formal, T);
2258 end if;
2259
2260 Next_Formal (Formal);
2261 end loop;
2262
2263 if Etype (Op) = Prev then
2264 Set_Etype (Op, T);
2265 end if;
2266
2267 Next_Elmt (Elmt);
2268 end loop;
2269 end if;
2270 end Check_Ops_From_Incomplete_Type;
2271
2272 -- Start of processing for Analyze_Full_Type_Declaration
2273
2274 begin
2275 Prev := Find_Type_Name (N);
2276
2277 -- The full view, if present, now points to the current type
2278
2279 -- Ada 2005 (AI-50217): If the type was previously decorated when
2280 -- imported through a LIMITED WITH clause, it appears as incomplete
2281 -- but has no full view.
2282
2283 if Ekind (Prev) = E_Incomplete_Type
2284 and then Present (Full_View (Prev))
2285 then
2286 T := Full_View (Prev);
2287 else
2288 T := Prev;
2289 end if;
2290
2291 Set_Is_Pure (T, Is_Pure (Current_Scope));
2292
2293 -- We set the flag Is_First_Subtype here. It is needed to set the
2294 -- corresponding flag for the Implicit class-wide-type created
2295 -- during tagged types processing.
2296
2297 Set_Is_First_Subtype (T, True);
2298
2299 -- Only composite types other than array types are allowed to have
2300 -- discriminants.
2301
2302 case Nkind (Def) is
2303
2304 -- For derived types, the rule will be checked once we've figured
2305 -- out the parent type.
2306
2307 when N_Derived_Type_Definition =>
2308 null;
2309
2310 -- For record types, discriminants are allowed, unless we are in
2311 -- SPARK.
2312
2313 when N_Record_Definition =>
2314 if Present (Discriminant_Specifications (N)) then
2315 Check_SPARK_Restriction
2316 ("discriminant type is not allowed",
2317 Defining_Identifier
2318 (First (Discriminant_Specifications (N))));
2319 end if;
2320
2321 when others =>
2322 if Present (Discriminant_Specifications (N)) then
2323 Error_Msg_N
2324 ("elementary or array type cannot have discriminants",
2325 Defining_Identifier
2326 (First (Discriminant_Specifications (N))));
2327 end if;
2328 end case;
2329
2330 -- Elaborate the type definition according to kind, and generate
2331 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2332 -- already done (this happens during the reanalysis that follows a call
2333 -- to the high level optimizer).
2334
2335 if not Analyzed (T) then
2336 Set_Analyzed (T);
2337
2338 case Nkind (Def) is
2339
2340 when N_Access_To_Subprogram_Definition =>
2341 Access_Subprogram_Declaration (T, Def);
2342
2343 -- If this is a remote access to subprogram, we must create the
2344 -- equivalent fat pointer type, and related subprograms.
2345
2346 if Is_Remote then
2347 Process_Remote_AST_Declaration (N);
2348 end if;
2349
2350 -- Validate categorization rule against access type declaration
2351 -- usually a violation in Pure unit, Shared_Passive unit.
2352
2353 Validate_Access_Type_Declaration (T, N);
2354
2355 when N_Access_To_Object_Definition =>
2356 Access_Type_Declaration (T, Def);
2357
2358 -- Validate categorization rule against access type declaration
2359 -- usually a violation in Pure unit, Shared_Passive unit.
2360
2361 Validate_Access_Type_Declaration (T, N);
2362
2363 -- If we are in a Remote_Call_Interface package and define a
2364 -- RACW, then calling stubs and specific stream attributes
2365 -- must be added.
2366
2367 if Is_Remote
2368 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2369 then
2370 Add_RACW_Features (Def_Id);
2371 end if;
2372
2373 -- Set no strict aliasing flag if config pragma seen
2374
2375 if Opt.No_Strict_Aliasing then
2376 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2377 end if;
2378
2379 when N_Array_Type_Definition =>
2380 Array_Type_Declaration (T, Def);
2381
2382 when N_Derived_Type_Definition =>
2383 Derived_Type_Declaration (T, N, T /= Def_Id);
2384
2385 when N_Enumeration_Type_Definition =>
2386 Enumeration_Type_Declaration (T, Def);
2387
2388 when N_Floating_Point_Definition =>
2389 Floating_Point_Type_Declaration (T, Def);
2390
2391 when N_Decimal_Fixed_Point_Definition =>
2392 Decimal_Fixed_Point_Type_Declaration (T, Def);
2393
2394 when N_Ordinary_Fixed_Point_Definition =>
2395 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2396
2397 when N_Signed_Integer_Type_Definition =>
2398 Signed_Integer_Type_Declaration (T, Def);
2399
2400 when N_Modular_Type_Definition =>
2401 Modular_Type_Declaration (T, Def);
2402
2403 when N_Record_Definition =>
2404 Record_Type_Declaration (T, N, Prev);
2405
2406 -- If declaration has a parse error, nothing to elaborate.
2407
2408 when N_Error =>
2409 null;
2410
2411 when others =>
2412 raise Program_Error;
2413
2414 end case;
2415 end if;
2416
2417 if Etype (T) = Any_Type then
2418 return;
2419 end if;
2420
2421 -- Controlled type is not allowed in SPARK
2422
2423 if Is_Visibly_Controlled (T) then
2424 Check_SPARK_Restriction ("controlled type is not allowed", N);
2425 end if;
2426
2427 -- Some common processing for all types
2428
2429 Set_Depends_On_Private (T, Has_Private_Component (T));
2430 Check_Ops_From_Incomplete_Type;
2431
2432 -- Both the declared entity, and its anonymous base type if one
2433 -- was created, need freeze nodes allocated.
2434
2435 declare
2436 B : constant Entity_Id := Base_Type (T);
2437
2438 begin
2439 -- In the case where the base type differs from the first subtype, we
2440 -- pre-allocate a freeze node, and set the proper link to the first
2441 -- subtype. Freeze_Entity will use this preallocated freeze node when
2442 -- it freezes the entity.
2443
2444 -- This does not apply if the base type is a generic type, whose
2445 -- declaration is independent of the current derived definition.
2446
2447 if B /= T and then not Is_Generic_Type (B) then
2448 Ensure_Freeze_Node (B);
2449 Set_First_Subtype_Link (Freeze_Node (B), T);
2450 end if;
2451
2452 -- A type that is imported through a limited_with clause cannot
2453 -- generate any code, and thus need not be frozen. However, an access
2454 -- type with an imported designated type needs a finalization list,
2455 -- which may be referenced in some other package that has non-limited
2456 -- visibility on the designated type. Thus we must create the
2457 -- finalization list at the point the access type is frozen, to
2458 -- prevent unsatisfied references at link time.
2459
2460 if not From_With_Type (T) or else Is_Access_Type (T) then
2461 Set_Has_Delayed_Freeze (T);
2462 end if;
2463 end;
2464
2465 -- Case where T is the full declaration of some private type which has
2466 -- been swapped in Defining_Identifier (N).
2467
2468 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2469 Process_Full_View (N, T, Def_Id);
2470
2471 -- Record the reference. The form of this is a little strange, since
2472 -- the full declaration has been swapped in. So the first parameter
2473 -- here represents the entity to which a reference is made which is
2474 -- the "real" entity, i.e. the one swapped in, and the second
2475 -- parameter provides the reference location.
2476
2477 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2478 -- since we don't want a complaint about the full type being an
2479 -- unwanted reference to the private type
2480
2481 declare
2482 B : constant Boolean := Has_Pragma_Unreferenced (T);
2483 begin
2484 Set_Has_Pragma_Unreferenced (T, False);
2485 Generate_Reference (T, T, 'c');
2486 Set_Has_Pragma_Unreferenced (T, B);
2487 end;
2488
2489 Set_Completion_Referenced (Def_Id);
2490
2491 -- For completion of incomplete type, process incomplete dependents
2492 -- and always mark the full type as referenced (it is the incomplete
2493 -- type that we get for any real reference).
2494
2495 elsif Ekind (Prev) = E_Incomplete_Type then
2496 Process_Incomplete_Dependents (N, T, Prev);
2497 Generate_Reference (Prev, Def_Id, 'c');
2498 Set_Completion_Referenced (Def_Id);
2499
2500 -- If not private type or incomplete type completion, this is a real
2501 -- definition of a new entity, so record it.
2502
2503 else
2504 Generate_Definition (Def_Id);
2505 end if;
2506
2507 if Chars (Scope (Def_Id)) = Name_System
2508 and then Chars (Def_Id) = Name_Address
2509 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2510 then
2511 Set_Is_Descendent_Of_Address (Def_Id);
2512 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2513 Set_Is_Descendent_Of_Address (Prev);
2514 end if;
2515
2516 Set_Optimize_Alignment_Flags (Def_Id);
2517 Check_Eliminated (Def_Id);
2518
2519 -- If the declaration is a completion and aspects are present, apply
2520 -- them to the entity for the type which is currently the partial
2521 -- view, but which is the one that will be frozen.
2522
2523 if Has_Aspects (N) then
2524 if Prev /= Def_Id then
2525 Analyze_Aspect_Specifications (N, Prev);
2526 else
2527 Analyze_Aspect_Specifications (N, Def_Id);
2528 end if;
2529 end if;
2530 end Analyze_Full_Type_Declaration;
2531
2532 ----------------------------------
2533 -- Analyze_Incomplete_Type_Decl --
2534 ----------------------------------
2535
2536 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2537 F : constant Boolean := Is_Pure (Current_Scope);
2538 T : Entity_Id;
2539
2540 begin
2541 Check_SPARK_Restriction ("incomplete type is not allowed", N);
2542
2543 Generate_Definition (Defining_Identifier (N));
2544
2545 -- Process an incomplete declaration. The identifier must not have been
2546 -- declared already in the scope. However, an incomplete declaration may
2547 -- appear in the private part of a package, for a private type that has
2548 -- already been declared.
2549
2550 -- In this case, the discriminants (if any) must match
2551
2552 T := Find_Type_Name (N);
2553
2554 Set_Ekind (T, E_Incomplete_Type);
2555 Init_Size_Align (T);
2556 Set_Is_First_Subtype (T, True);
2557 Set_Etype (T, T);
2558
2559 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2560 -- incomplete types.
2561
2562 if Tagged_Present (N) then
2563 Set_Is_Tagged_Type (T);
2564 Make_Class_Wide_Type (T);
2565 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2566 end if;
2567
2568 Push_Scope (T);
2569
2570 Set_Stored_Constraint (T, No_Elist);
2571
2572 if Present (Discriminant_Specifications (N)) then
2573 Process_Discriminants (N);
2574 end if;
2575
2576 End_Scope;
2577
2578 -- If the type has discriminants, non-trivial subtypes may be
2579 -- declared before the full view of the type. The full views of those
2580 -- subtypes will be built after the full view of the type.
2581
2582 Set_Private_Dependents (T, New_Elmt_List);
2583 Set_Is_Pure (T, F);
2584 end Analyze_Incomplete_Type_Decl;
2585
2586 -----------------------------------
2587 -- Analyze_Interface_Declaration --
2588 -----------------------------------
2589
2590 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2591 CW : constant Entity_Id := Class_Wide_Type (T);
2592
2593 begin
2594 Set_Is_Tagged_Type (T);
2595
2596 Set_Is_Limited_Record (T, Limited_Present (Def)
2597 or else Task_Present (Def)
2598 or else Protected_Present (Def)
2599 or else Synchronized_Present (Def));
2600
2601 -- Type is abstract if full declaration carries keyword, or if previous
2602 -- partial view did.
2603
2604 Set_Is_Abstract_Type (T);
2605 Set_Is_Interface (T);
2606
2607 -- Type is a limited interface if it includes the keyword limited, task,
2608 -- protected, or synchronized.
2609
2610 Set_Is_Limited_Interface
2611 (T, Limited_Present (Def)
2612 or else Protected_Present (Def)
2613 or else Synchronized_Present (Def)
2614 or else Task_Present (Def));
2615
2616 Set_Interfaces (T, New_Elmt_List);
2617 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2618
2619 -- Complete the decoration of the class-wide entity if it was already
2620 -- built (i.e. during the creation of the limited view)
2621
2622 if Present (CW) then
2623 Set_Is_Interface (CW);
2624 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2625 end if;
2626
2627 -- Check runtime support for synchronized interfaces
2628
2629 if VM_Target = No_VM
2630 and then (Is_Task_Interface (T)
2631 or else Is_Protected_Interface (T)
2632 or else Is_Synchronized_Interface (T))
2633 and then not RTE_Available (RE_Select_Specific_Data)
2634 then
2635 Error_Msg_CRT ("synchronized interfaces", T);
2636 end if;
2637 end Analyze_Interface_Declaration;
2638
2639 -----------------------------
2640 -- Analyze_Itype_Reference --
2641 -----------------------------
2642
2643 -- Nothing to do. This node is placed in the tree only for the benefit of
2644 -- back end processing, and has no effect on the semantic processing.
2645
2646 procedure Analyze_Itype_Reference (N : Node_Id) is
2647 begin
2648 pragma Assert (Is_Itype (Itype (N)));
2649 null;
2650 end Analyze_Itype_Reference;
2651
2652 --------------------------------
2653 -- Analyze_Number_Declaration --
2654 --------------------------------
2655
2656 procedure Analyze_Number_Declaration (N : Node_Id) is
2657 Id : constant Entity_Id := Defining_Identifier (N);
2658 E : constant Node_Id := Expression (N);
2659 T : Entity_Id;
2660 Index : Interp_Index;
2661 It : Interp;
2662
2663 begin
2664 Generate_Definition (Id);
2665 Enter_Name (Id);
2666
2667 -- This is an optimization of a common case of an integer literal
2668
2669 if Nkind (E) = N_Integer_Literal then
2670 Set_Is_Static_Expression (E, True);
2671 Set_Etype (E, Universal_Integer);
2672
2673 Set_Etype (Id, Universal_Integer);
2674 Set_Ekind (Id, E_Named_Integer);
2675 Set_Is_Frozen (Id, True);
2676 return;
2677 end if;
2678
2679 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2680
2681 -- Process expression, replacing error by integer zero, to avoid
2682 -- cascaded errors or aborts further along in the processing
2683
2684 -- Replace Error by integer zero, which seems least likely to cause
2685 -- cascaded errors.
2686
2687 if E = Error then
2688 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2689 Set_Error_Posted (E);
2690 end if;
2691
2692 Analyze (E);
2693
2694 -- Verify that the expression is static and numeric. If
2695 -- the expression is overloaded, we apply the preference
2696 -- rule that favors root numeric types.
2697
2698 if not Is_Overloaded (E) then
2699 T := Etype (E);
2700
2701 else
2702 T := Any_Type;
2703
2704 Get_First_Interp (E, Index, It);
2705 while Present (It.Typ) loop
2706 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
2707 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2708 then
2709 if T = Any_Type then
2710 T := It.Typ;
2711
2712 elsif It.Typ = Universal_Real
2713 or else It.Typ = Universal_Integer
2714 then
2715 -- Choose universal interpretation over any other
2716
2717 T := It.Typ;
2718 exit;
2719 end if;
2720 end if;
2721
2722 Get_Next_Interp (Index, It);
2723 end loop;
2724 end if;
2725
2726 if Is_Integer_Type (T) then
2727 Resolve (E, T);
2728 Set_Etype (Id, Universal_Integer);
2729 Set_Ekind (Id, E_Named_Integer);
2730
2731 elsif Is_Real_Type (T) then
2732
2733 -- Because the real value is converted to universal_real, this is a
2734 -- legal context for a universal fixed expression.
2735
2736 if T = Universal_Fixed then
2737 declare
2738 Loc : constant Source_Ptr := Sloc (N);
2739 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2740 Subtype_Mark =>
2741 New_Occurrence_Of (Universal_Real, Loc),
2742 Expression => Relocate_Node (E));
2743
2744 begin
2745 Rewrite (E, Conv);
2746 Analyze (E);
2747 end;
2748
2749 elsif T = Any_Fixed then
2750 Error_Msg_N ("illegal context for mixed mode operation", E);
2751
2752 -- Expression is of the form : universal_fixed * integer. Try to
2753 -- resolve as universal_real.
2754
2755 T := Universal_Real;
2756 Set_Etype (E, T);
2757 end if;
2758
2759 Resolve (E, T);
2760 Set_Etype (Id, Universal_Real);
2761 Set_Ekind (Id, E_Named_Real);
2762
2763 else
2764 Wrong_Type (E, Any_Numeric);
2765 Resolve (E, T);
2766
2767 Set_Etype (Id, T);
2768 Set_Ekind (Id, E_Constant);
2769 Set_Never_Set_In_Source (Id, True);
2770 Set_Is_True_Constant (Id, True);
2771 return;
2772 end if;
2773
2774 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2775 Set_Etype (E, Etype (Id));
2776 end if;
2777
2778 if not Is_OK_Static_Expression (E) then
2779 Flag_Non_Static_Expr
2780 ("non-static expression used in number declaration!", E);
2781 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2782 Set_Etype (E, Any_Type);
2783 end if;
2784 end Analyze_Number_Declaration;
2785
2786 --------------------------------
2787 -- Analyze_Object_Declaration --
2788 --------------------------------
2789
2790 procedure Analyze_Object_Declaration (N : Node_Id) is
2791 Loc : constant Source_Ptr := Sloc (N);
2792 Id : constant Entity_Id := Defining_Identifier (N);
2793 T : Entity_Id;
2794 Act_T : Entity_Id;
2795
2796 E : Node_Id := Expression (N);
2797 -- E is set to Expression (N) throughout this routine. When
2798 -- Expression (N) is modified, E is changed accordingly.
2799
2800 Prev_Entity : Entity_Id := Empty;
2801
2802 function Count_Tasks (T : Entity_Id) return Uint;
2803 -- This function is called when a non-generic library level object of a
2804 -- task type is declared. Its function is to count the static number of
2805 -- tasks declared within the type (it is only called if Has_Tasks is set
2806 -- for T). As a side effect, if an array of tasks with non-static bounds
2807 -- or a variant record type is encountered, Check_Restrictions is called
2808 -- indicating the count is unknown.
2809
2810 -----------------
2811 -- Count_Tasks --
2812 -----------------
2813
2814 function Count_Tasks (T : Entity_Id) return Uint is
2815 C : Entity_Id;
2816 X : Node_Id;
2817 V : Uint;
2818
2819 begin
2820 if Is_Task_Type (T) then
2821 return Uint_1;
2822
2823 elsif Is_Record_Type (T) then
2824 if Has_Discriminants (T) then
2825 Check_Restriction (Max_Tasks, N);
2826 return Uint_0;
2827
2828 else
2829 V := Uint_0;
2830 C := First_Component (T);
2831 while Present (C) loop
2832 V := V + Count_Tasks (Etype (C));
2833 Next_Component (C);
2834 end loop;
2835
2836 return V;
2837 end if;
2838
2839 elsif Is_Array_Type (T) then
2840 X := First_Index (T);
2841 V := Count_Tasks (Component_Type (T));
2842 while Present (X) loop
2843 C := Etype (X);
2844
2845 if not Is_Static_Subtype (C) then
2846 Check_Restriction (Max_Tasks, N);
2847 return Uint_0;
2848 else
2849 V := V * (UI_Max (Uint_0,
2850 Expr_Value (Type_High_Bound (C)) -
2851 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2852 end if;
2853
2854 Next_Index (X);
2855 end loop;
2856
2857 return V;
2858
2859 else
2860 return Uint_0;
2861 end if;
2862 end Count_Tasks;
2863
2864 -- Start of processing for Analyze_Object_Declaration
2865
2866 begin
2867 -- There are three kinds of implicit types generated by an
2868 -- object declaration:
2869
2870 -- 1. Those generated by the original Object Definition
2871
2872 -- 2. Those generated by the Expression
2873
2874 -- 3. Those used to constrain the Object Definition with the
2875 -- expression constraints when the definition is unconstrained.
2876
2877 -- They must be generated in this order to avoid order of elaboration
2878 -- issues. Thus the first step (after entering the name) is to analyze
2879 -- the object definition.
2880
2881 if Constant_Present (N) then
2882 Prev_Entity := Current_Entity_In_Scope (Id);
2883
2884 if Present (Prev_Entity)
2885 and then
2886
2887 -- If the homograph is an implicit subprogram, it is overridden
2888 -- by the current declaration.
2889
2890 ((Is_Overloadable (Prev_Entity)
2891 and then Is_Inherited_Operation (Prev_Entity))
2892
2893 -- The current object is a discriminal generated for an entry
2894 -- family index. Even though the index is a constant, in this
2895 -- particular context there is no true constant redeclaration.
2896 -- Enter_Name will handle the visibility.
2897
2898 or else
2899 (Is_Discriminal (Id)
2900 and then Ekind (Discriminal_Link (Id)) =
2901 E_Entry_Index_Parameter)
2902
2903 -- The current object is the renaming for a generic declared
2904 -- within the instance.
2905
2906 or else
2907 (Ekind (Prev_Entity) = E_Package
2908 and then Nkind (Parent (Prev_Entity)) =
2909 N_Package_Renaming_Declaration
2910 and then not Comes_From_Source (Prev_Entity)
2911 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2912 then
2913 Prev_Entity := Empty;
2914 end if;
2915 end if;
2916
2917 if Present (Prev_Entity) then
2918 Constant_Redeclaration (Id, N, T);
2919
2920 Generate_Reference (Prev_Entity, Id, 'c');
2921 Set_Completion_Referenced (Id);
2922
2923 if Error_Posted (N) then
2924
2925 -- Type mismatch or illegal redeclaration, Do not analyze
2926 -- expression to avoid cascaded errors.
2927
2928 T := Find_Type_Of_Object (Object_Definition (N), N);
2929 Set_Etype (Id, T);
2930 Set_Ekind (Id, E_Variable);
2931 goto Leave;
2932 end if;
2933
2934 -- In the normal case, enter identifier at the start to catch premature
2935 -- usage in the initialization expression.
2936
2937 else
2938 Generate_Definition (Id);
2939 Enter_Name (Id);
2940
2941 Mark_Coextensions (N, Object_Definition (N));
2942
2943 T := Find_Type_Of_Object (Object_Definition (N), N);
2944
2945 if Nkind (Object_Definition (N)) = N_Access_Definition
2946 and then Present
2947 (Access_To_Subprogram_Definition (Object_Definition (N)))
2948 and then Protected_Present
2949 (Access_To_Subprogram_Definition (Object_Definition (N)))
2950 then
2951 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2952 end if;
2953
2954 if Error_Posted (Id) then
2955 Set_Etype (Id, T);
2956 Set_Ekind (Id, E_Variable);
2957 goto Leave;
2958 end if;
2959 end if;
2960
2961 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2962 -- out some static checks
2963
2964 if Ada_Version >= Ada_2005
2965 and then Can_Never_Be_Null (T)
2966 then
2967 -- In case of aggregates we must also take care of the correct
2968 -- initialization of nested aggregates bug this is done at the
2969 -- point of the analysis of the aggregate (see sem_aggr.adb)
2970
2971 if Present (Expression (N))
2972 and then Nkind (Expression (N)) = N_Aggregate
2973 then
2974 null;
2975
2976 else
2977 declare
2978 Save_Typ : constant Entity_Id := Etype (Id);
2979 begin
2980 Set_Etype (Id, T); -- Temp. decoration for static checks
2981 Null_Exclusion_Static_Checks (N);
2982 Set_Etype (Id, Save_Typ);
2983 end;
2984 end if;
2985 end if;
2986
2987 -- Object is marked pure if it is in a pure scope
2988
2989 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2990
2991 -- If deferred constant, make sure context is appropriate. We detect
2992 -- a deferred constant as a constant declaration with no expression.
2993 -- A deferred constant can appear in a package body if its completion
2994 -- is by means of an interface pragma.
2995
2996 if Constant_Present (N) and then No (E) then
2997
2998 -- A deferred constant may appear in the declarative part of the
2999 -- following constructs:
3000
3001 -- blocks
3002 -- entry bodies
3003 -- extended return statements
3004 -- package specs
3005 -- package bodies
3006 -- subprogram bodies
3007 -- task bodies
3008
3009 -- When declared inside a package spec, a deferred constant must be
3010 -- completed by a full constant declaration or pragma Import. In all
3011 -- other cases, the only proper completion is pragma Import. Extended
3012 -- return statements are flagged as invalid contexts because they do
3013 -- not have a declarative part and so cannot accommodate the pragma.
3014
3015 if Ekind (Current_Scope) = E_Return_Statement then
3016 Error_Msg_N
3017 ("invalid context for deferred constant declaration (RM 7.4)",
3018 N);
3019 Error_Msg_N
3020 ("\declaration requires an initialization expression",
3021 N);
3022 Set_Constant_Present (N, False);
3023
3024 -- In Ada 83, deferred constant must be of private type
3025
3026 elsif not Is_Private_Type (T) then
3027 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3028 Error_Msg_N
3029 ("(Ada 83) deferred constant must be private type", N);
3030 end if;
3031 end if;
3032
3033 -- If not a deferred constant, then object declaration freezes its type
3034
3035 else
3036 Check_Fully_Declared (T, N);
3037 Freeze_Before (N, T);
3038 end if;
3039
3040 -- If the object was created by a constrained array definition, then
3041 -- set the link in both the anonymous base type and anonymous subtype
3042 -- that are built to represent the array type to point to the object.
3043
3044 if Nkind (Object_Definition (Declaration_Node (Id))) =
3045 N_Constrained_Array_Definition
3046 then
3047 Set_Related_Array_Object (T, Id);
3048 Set_Related_Array_Object (Base_Type (T), Id);
3049 end if;
3050
3051 -- Special checks for protected objects not at library level
3052
3053 if Is_Protected_Type (T)
3054 and then not Is_Library_Level_Entity (Id)
3055 then
3056 Check_Restriction (No_Local_Protected_Objects, Id);
3057
3058 -- Protected objects with interrupt handlers must be at library level
3059
3060 -- Ada 2005: this test is not needed (and the corresponding clause
3061 -- in the RM is removed) because accessibility checks are sufficient
3062 -- to make handlers not at the library level illegal.
3063
3064 -- AI05-0303: the AI is in fact a binding interpretation, and thus
3065 -- applies to the '95 version of the language as well.
3066
3067 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3068 Error_Msg_N
3069 ("interrupt object can only be declared at library level", Id);
3070 end if;
3071 end if;
3072
3073 -- The actual subtype of the object is the nominal subtype, unless
3074 -- the nominal one is unconstrained and obtained from the expression.
3075
3076 Act_T := T;
3077
3078 -- These checks should be performed before the initialization expression
3079 -- is considered, so that the Object_Definition node is still the same
3080 -- as in source code.
3081
3082 -- In SPARK, the nominal subtype shall be given by a subtype mark and
3083 -- shall not be unconstrained. (The only exception to this is the
3084 -- admission of declarations of constants of type String.)
3085
3086 if not
3087 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
3088 then
3089 Check_SPARK_Restriction
3090 ("subtype mark required", Object_Definition (N));
3091
3092 elsif Is_Array_Type (T)
3093 and then not Is_Constrained (T)
3094 and then T /= Standard_String
3095 then
3096 Check_SPARK_Restriction
3097 ("subtype mark of constrained type expected",
3098 Object_Definition (N));
3099 end if;
3100
3101 -- There are no aliased objects in SPARK
3102
3103 if Aliased_Present (N) then
3104 Check_SPARK_Restriction ("aliased object is not allowed", N);
3105 end if;
3106
3107 -- Process initialization expression if present and not in error
3108
3109 if Present (E) and then E /= Error then
3110
3111 -- Generate an error in case of CPP class-wide object initialization.
3112 -- Required because otherwise the expansion of the class-wide
3113 -- assignment would try to use 'size to initialize the object
3114 -- (primitive that is not available in CPP tagged types).
3115
3116 if Is_Class_Wide_Type (Act_T)
3117 and then
3118 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3119 or else
3120 (Present (Full_View (Root_Type (Etype (Act_T))))
3121 and then
3122 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3123 then
3124 Error_Msg_N
3125 ("predefined assignment not available for 'C'P'P tagged types",
3126 E);
3127 end if;
3128
3129 Mark_Coextensions (N, E);
3130 Analyze (E);
3131
3132 -- In case of errors detected in the analysis of the expression,
3133 -- decorate it with the expected type to avoid cascaded errors
3134
3135 if No (Etype (E)) then
3136 Set_Etype (E, T);
3137 end if;
3138
3139 -- If an initialization expression is present, then we set the
3140 -- Is_True_Constant flag. It will be reset if this is a variable
3141 -- and it is indeed modified.
3142
3143 Set_Is_True_Constant (Id, True);
3144
3145 -- If we are analyzing a constant declaration, set its completion
3146 -- flag after analyzing and resolving the expression.
3147
3148 if Constant_Present (N) then
3149 Set_Has_Completion (Id);
3150 end if;
3151
3152 -- Set type and resolve (type may be overridden later on). Note:
3153 -- Ekind (Id) must still be E_Void at this point so that incorrect
3154 -- early usage within E is properly diagnosed.
3155
3156 Set_Etype (Id, T);
3157 Resolve (E, T);
3158
3159 -- No further action needed if E is a call to an inlined function
3160 -- which returns an unconstrained type and it has been expanded into
3161 -- a procedure call. In that case N has been replaced by an object
3162 -- declaration without initializing expression and it has been
3163 -- analyzed (see Expand_Inlined_Call).
3164
3165 if Debug_Flag_Dot_K
3166 and then Expander_Active
3167 and then Nkind (E) = N_Function_Call
3168 and then Nkind (Name (E)) in N_Has_Entity
3169 and then Is_Inlined (Entity (Name (E)))
3170 and then not Is_Constrained (Etype (E))
3171 and then Analyzed (N)
3172 and then No (Expression (N))
3173 then
3174 return;
3175 end if;
3176
3177 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3178 -- node (which was marked already-analyzed), we need to set the type
3179 -- to something other than Any_Access in order to keep gigi happy.
3180
3181 if Etype (E) = Any_Access then
3182 Set_Etype (E, T);
3183 end if;
3184
3185 -- If the object is an access to variable, the initialization
3186 -- expression cannot be an access to constant.
3187
3188 if Is_Access_Type (T)
3189 and then not Is_Access_Constant (T)
3190 and then Is_Access_Type (Etype (E))
3191 and then Is_Access_Constant (Etype (E))
3192 then
3193 Error_Msg_N
3194 ("access to variable cannot be initialized "
3195 & "with an access-to-constant expression", E);
3196 end if;
3197
3198 if not Assignment_OK (N) then
3199 Check_Initialization (T, E);
3200 end if;
3201
3202 Check_Unset_Reference (E);
3203
3204 -- If this is a variable, then set current value. If this is a
3205 -- declared constant of a scalar type with a static expression,
3206 -- indicate that it is always valid.
3207
3208 if not Constant_Present (N) then
3209 if Compile_Time_Known_Value (E) then
3210 Set_Current_Value (Id, E);
3211 end if;
3212
3213 elsif Is_Scalar_Type (T)
3214 and then Is_OK_Static_Expression (E)
3215 then
3216 Set_Is_Known_Valid (Id);
3217 end if;
3218
3219 -- Deal with setting of null flags
3220
3221 if Is_Access_Type (T) then
3222 if Known_Non_Null (E) then
3223 Set_Is_Known_Non_Null (Id, True);
3224 elsif Known_Null (E)
3225 and then not Can_Never_Be_Null (Id)
3226 then
3227 Set_Is_Known_Null (Id, True);
3228 end if;
3229 end if;
3230
3231 -- Check incorrect use of dynamically tagged expressions.
3232
3233 if Is_Tagged_Type (T) then
3234 Check_Dynamically_Tagged_Expression
3235 (Expr => E,
3236 Typ => T,
3237 Related_Nod => N);
3238 end if;
3239
3240 Apply_Scalar_Range_Check (E, T);
3241 Apply_Static_Length_Check (E, T);
3242
3243 if Nkind (Original_Node (N)) = N_Object_Declaration
3244 and then Comes_From_Source (Original_Node (N))
3245
3246 -- Only call test if needed
3247
3248 and then Restriction_Check_Required (SPARK_05)
3249 and then not Is_SPARK_Initialization_Expr (E)
3250 then
3251 Check_SPARK_Restriction
3252 ("initialization expression is not appropriate", E);
3253 end if;
3254 end if;
3255
3256 -- If the No_Streams restriction is set, check that the type of the
3257 -- object is not, and does not contain, any subtype derived from
3258 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3259 -- Has_Stream just for efficiency reasons. There is no point in
3260 -- spending time on a Has_Stream check if the restriction is not set.
3261
3262 if Restriction_Check_Required (No_Streams) then
3263 if Has_Stream (T) then
3264 Check_Restriction (No_Streams, N);
3265 end if;
3266 end if;
3267
3268 -- Deal with predicate check before we start to do major rewriting. It
3269 -- is OK to initialize and then check the initialized value, since the
3270 -- object goes out of scope if we get a predicate failure. Note that we
3271 -- do this in the analyzer and not the expander because the analyzer
3272 -- does some substantial rewriting in some cases.
3273
3274 -- We need a predicate check if the type has predicates, and if either
3275 -- there is an initializing expression, or for default initialization
3276 -- when we have at least one case of an explicit default initial value.
3277
3278 if not Suppress_Assignment_Checks (N)
3279 and then Present (Predicate_Function (T))
3280 and then
3281 (Present (E)
3282 or else
3283 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3284 then
3285 -- If the type has a static predicate and the expression is known at
3286 -- compile time, see if the expression satisfies the predicate.
3287
3288 if Present (E) then
3289 Check_Expression_Against_Static_Predicate (E, T);
3290 end if;
3291
3292 Insert_After (N,
3293 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3294 end if;
3295
3296 -- Case of unconstrained type
3297
3298 if Is_Indefinite_Subtype (T) then
3299
3300 -- In SPARK, a declaration of unconstrained type is allowed
3301 -- only for constants of type string.
3302
3303 if Is_String_Type (T) and then not Constant_Present (N) then
3304 Check_SPARK_Restriction
3305 ("declaration of object of unconstrained type not allowed", N);
3306 end if;
3307
3308 -- Nothing to do in deferred constant case
3309
3310 if Constant_Present (N) and then No (E) then
3311 null;
3312
3313 -- Case of no initialization present
3314
3315 elsif No (E) then
3316 if No_Initialization (N) then
3317 null;
3318
3319 elsif Is_Class_Wide_Type (T) then
3320 Error_Msg_N
3321 ("initialization required in class-wide declaration ", N);
3322
3323 else
3324 Error_Msg_N
3325 ("unconstrained subtype not allowed (need initialization)",
3326 Object_Definition (N));
3327
3328 if Is_Record_Type (T) and then Has_Discriminants (T) then
3329 Error_Msg_N
3330 ("\provide initial value or explicit discriminant values",
3331 Object_Definition (N));
3332
3333 Error_Msg_NE
3334 ("\or give default discriminant values for type&",
3335 Object_Definition (N), T);
3336
3337 elsif Is_Array_Type (T) then
3338 Error_Msg_N
3339 ("\provide initial value or explicit array bounds",
3340 Object_Definition (N));
3341 end if;
3342 end if;
3343
3344 -- Case of initialization present but in error. Set initial
3345 -- expression as absent (but do not make above complaints)
3346
3347 elsif E = Error then
3348 Set_Expression (N, Empty);
3349 E := Empty;
3350
3351 -- Case of initialization present
3352
3353 else
3354 -- Check restrictions in Ada 83
3355
3356 if not Constant_Present (N) then
3357
3358 -- Unconstrained variables not allowed in Ada 83 mode
3359
3360 if Ada_Version = Ada_83
3361 and then Comes_From_Source (Object_Definition (N))
3362 then
3363 Error_Msg_N
3364 ("(Ada 83) unconstrained variable not allowed",
3365 Object_Definition (N));
3366 end if;
3367 end if;
3368
3369 -- Now we constrain the variable from the initializing expression
3370
3371 -- If the expression is an aggregate, it has been expanded into
3372 -- individual assignments. Retrieve the actual type from the
3373 -- expanded construct.
3374
3375 if Is_Array_Type (T)
3376 and then No_Initialization (N)
3377 and then Nkind (Original_Node (E)) = N_Aggregate
3378 then
3379 Act_T := Etype (E);
3380
3381 -- In case of class-wide interface object declarations we delay
3382 -- the generation of the equivalent record type declarations until
3383 -- its expansion because there are cases in they are not required.
3384
3385 elsif Is_Interface (T) then
3386 null;
3387
3388 else
3389 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3390 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3391 end if;
3392
3393 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3394
3395 if Aliased_Present (N) then
3396 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3397 end if;
3398
3399 Freeze_Before (N, Act_T);
3400 Freeze_Before (N, T);
3401 end if;
3402
3403 elsif Is_Array_Type (T)
3404 and then No_Initialization (N)
3405 and then Nkind (Original_Node (E)) = N_Aggregate
3406 then
3407 if not Is_Entity_Name (Object_Definition (N)) then
3408 Act_T := Etype (E);
3409 Check_Compile_Time_Size (Act_T);
3410
3411 if Aliased_Present (N) then
3412 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3413 end if;
3414 end if;
3415
3416 -- When the given object definition and the aggregate are specified
3417 -- independently, and their lengths might differ do a length check.
3418 -- This cannot happen if the aggregate is of the form (others =>...)
3419
3420 if not Is_Constrained (T) then
3421 null;
3422
3423 elsif Nkind (E) = N_Raise_Constraint_Error then
3424
3425 -- Aggregate is statically illegal. Place back in declaration
3426
3427 Set_Expression (N, E);
3428 Set_No_Initialization (N, False);
3429
3430 elsif T = Etype (E) then
3431 null;
3432
3433 elsif Nkind (E) = N_Aggregate
3434 and then Present (Component_Associations (E))
3435 and then Present (Choices (First (Component_Associations (E))))
3436 and then Nkind (First
3437 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3438 then
3439 null;
3440
3441 else
3442 Apply_Length_Check (E, T);
3443 end if;
3444
3445 -- If the type is limited unconstrained with defaulted discriminants and
3446 -- there is no expression, then the object is constrained by the
3447 -- defaults, so it is worthwhile building the corresponding subtype.
3448
3449 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3450 and then not Is_Constrained (T)
3451 and then Has_Discriminants (T)
3452 then
3453 if No (E) then
3454 Act_T := Build_Default_Subtype (T, N);
3455 else
3456 -- Ada 2005: a limited object may be initialized by means of an
3457 -- aggregate. If the type has default discriminants it has an
3458 -- unconstrained nominal type, Its actual subtype will be obtained
3459 -- from the aggregate, and not from the default discriminants.
3460
3461 Act_T := Etype (E);
3462 end if;
3463
3464 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3465
3466 elsif Present (Underlying_Type (T))
3467 and then not Is_Constrained (Underlying_Type (T))
3468 and then Has_Discriminants (Underlying_Type (T))
3469 and then Nkind (E) = N_Function_Call
3470 and then Constant_Present (N)
3471 then
3472 -- The back-end has problems with constants of a discriminated type
3473 -- with defaults, if the initial value is a function call. We
3474 -- generate an intermediate temporary for the result of the call.
3475 -- It is unclear why this should make it acceptable to gcc. ???
3476
3477 Remove_Side_Effects (E);
3478
3479 -- If this is a constant declaration of an unconstrained type and
3480 -- the initialization is an aggregate, we can use the subtype of the
3481 -- aggregate for the declared entity because it is immutable.
3482
3483 elsif not Is_Constrained (T)
3484 and then Has_Discriminants (T)
3485 and then Constant_Present (N)
3486 and then not Has_Unchecked_Union (T)
3487 and then Nkind (E) = N_Aggregate
3488 then
3489 Act_T := Etype (E);
3490 end if;
3491
3492 -- Check No_Wide_Characters restriction
3493
3494 Check_Wide_Character_Restriction (T, Object_Definition (N));
3495
3496 -- Indicate this is not set in source. Certainly true for constants, and
3497 -- true for variables so far (will be reset for a variable if and when
3498 -- we encounter a modification in the source).
3499
3500 Set_Never_Set_In_Source (Id, True);
3501
3502 -- Now establish the proper kind and type of the object
3503
3504 if Constant_Present (N) then
3505 Set_Ekind (Id, E_Constant);
3506 Set_Is_True_Constant (Id, True);
3507
3508 else
3509 Set_Ekind (Id, E_Variable);
3510
3511 -- A variable is set as shared passive if it appears in a shared
3512 -- passive package, and is at the outer level. This is not done for
3513 -- entities generated during expansion, because those are always
3514 -- manipulated locally.
3515
3516 if Is_Shared_Passive (Current_Scope)
3517 and then Is_Library_Level_Entity (Id)
3518 and then Comes_From_Source (Id)
3519 then
3520 Set_Is_Shared_Passive (Id);
3521 Check_Shared_Var (Id, T, N);
3522 end if;
3523
3524 -- Set Has_Initial_Value if initializing expression present. Note
3525 -- that if there is no initializing expression, we leave the state
3526 -- of this flag unchanged (usually it will be False, but notably in
3527 -- the case of exception choice variables, it will already be true).
3528
3529 if Present (E) then
3530 Set_Has_Initial_Value (Id, True);
3531 end if;
3532 end if;
3533
3534 -- Initialize alignment and size and capture alignment setting
3535
3536 Init_Alignment (Id);
3537 Init_Esize (Id);
3538 Set_Optimize_Alignment_Flags (Id);
3539
3540 -- Deal with aliased case
3541
3542 if Aliased_Present (N) then
3543 Set_Is_Aliased (Id);
3544
3545 -- If the object is aliased and the type is unconstrained with
3546 -- defaulted discriminants and there is no expression, then the
3547 -- object is constrained by the defaults, so it is worthwhile
3548 -- building the corresponding subtype.
3549
3550 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3551 -- unconstrained, then only establish an actual subtype if the
3552 -- nominal subtype is indefinite. In definite cases the object is
3553 -- unconstrained in Ada 2005.
3554
3555 if No (E)
3556 and then Is_Record_Type (T)
3557 and then not Is_Constrained (T)
3558 and then Has_Discriminants (T)
3559 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3560 then
3561 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3562 end if;
3563 end if;
3564
3565 -- Now we can set the type of the object
3566
3567 Set_Etype (Id, Act_T);
3568
3569 -- Object is marked to be treated as volatile if type is volatile and
3570 -- we clear the Current_Value setting that may have been set above.
3571
3572 if Treat_As_Volatile (Etype (Id)) then
3573 Set_Treat_As_Volatile (Id);
3574 Set_Current_Value (Id, Empty);
3575 end if;
3576
3577 -- Deal with controlled types
3578
3579 if Has_Controlled_Component (Etype (Id))
3580 or else Is_Controlled (Etype (Id))
3581 then
3582 if not Is_Library_Level_Entity (Id) then
3583 Check_Restriction (No_Nested_Finalization, N);
3584 else
3585 Validate_Controlled_Object (Id);
3586 end if;
3587 end if;
3588
3589 if Has_Task (Etype (Id)) then
3590 Check_Restriction (No_Tasking, N);
3591
3592 -- Deal with counting max tasks
3593
3594 -- Nothing to do if inside a generic
3595
3596 if Inside_A_Generic then
3597 null;
3598
3599 -- If library level entity, then count tasks
3600
3601 elsif Is_Library_Level_Entity (Id) then
3602 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3603
3604 -- If not library level entity, then indicate we don't know max
3605 -- tasks and also check task hierarchy restriction and blocking
3606 -- operation (since starting a task is definitely blocking!)
3607
3608 else
3609 Check_Restriction (Max_Tasks, N);
3610 Check_Restriction (No_Task_Hierarchy, N);
3611 Check_Potentially_Blocking_Operation (N);
3612 end if;
3613
3614 -- A rather specialized test. If we see two tasks being declared
3615 -- of the same type in the same object declaration, and the task
3616 -- has an entry with an address clause, we know that program error
3617 -- will be raised at run time since we can't have two tasks with
3618 -- entries at the same address.
3619
3620 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3621 declare
3622 E : Entity_Id;
3623
3624 begin
3625 E := First_Entity (Etype (Id));
3626 while Present (E) loop
3627 if Ekind (E) = E_Entry
3628 and then Present (Get_Attribute_Definition_Clause
3629 (E, Attribute_Address))
3630 then
3631 Error_Msg_N
3632 ("??more than one task with same entry address", N);
3633 Error_Msg_N
3634 ("\??Program_Error will be raised at run time", N);
3635 Insert_Action (N,
3636 Make_Raise_Program_Error (Loc,
3637 Reason => PE_Duplicated_Entry_Address));
3638 exit;
3639 end if;
3640
3641 Next_Entity (E);
3642 end loop;
3643 end;
3644 end if;
3645 end if;
3646
3647 -- Some simple constant-propagation: if the expression is a constant
3648 -- string initialized with a literal, share the literal. This avoids
3649 -- a run-time copy.
3650
3651 if Present (E)
3652 and then Is_Entity_Name (E)
3653 and then Ekind (Entity (E)) = E_Constant
3654 and then Base_Type (Etype (E)) = Standard_String
3655 then
3656 declare
3657 Val : constant Node_Id := Constant_Value (Entity (E));
3658 begin
3659 if Present (Val)
3660 and then Nkind (Val) = N_String_Literal
3661 then
3662 Rewrite (E, New_Copy (Val));
3663 end if;
3664 end;
3665 end if;
3666
3667 -- Another optimization: if the nominal subtype is unconstrained and
3668 -- the expression is a function call that returns an unconstrained
3669 -- type, rewrite the declaration as a renaming of the result of the
3670 -- call. The exceptions below are cases where the copy is expected,
3671 -- either by the back end (Aliased case) or by the semantics, as for
3672 -- initializing controlled types or copying tags for classwide types.
3673
3674 if Present (E)
3675 and then Nkind (E) = N_Explicit_Dereference
3676 and then Nkind (Original_Node (E)) = N_Function_Call
3677 and then not Is_Library_Level_Entity (Id)
3678 and then not Is_Constrained (Underlying_Type (T))
3679 and then not Is_Aliased (Id)
3680 and then not Is_Class_Wide_Type (T)
3681 and then not Is_Controlled (T)
3682 and then not Has_Controlled_Component (Base_Type (T))
3683 and then Expander_Active
3684 then
3685 Rewrite (N,
3686 Make_Object_Renaming_Declaration (Loc,
3687 Defining_Identifier => Id,
3688 Access_Definition => Empty,
3689 Subtype_Mark => New_Occurrence_Of
3690 (Base_Type (Etype (Id)), Loc),
3691 Name => E));
3692
3693 Set_Renamed_Object (Id, E);
3694
3695 -- Force generation of debugging information for the constant and for
3696 -- the renamed function call.
3697
3698 Set_Debug_Info_Needed (Id);
3699 Set_Debug_Info_Needed (Entity (Prefix (E)));
3700 end if;
3701
3702 if Present (Prev_Entity)
3703 and then Is_Frozen (Prev_Entity)
3704 and then not Error_Posted (Id)
3705 then
3706 Error_Msg_N ("full constant declaration appears too late", N);
3707 end if;
3708
3709 Check_Eliminated (Id);
3710
3711 -- Deal with setting In_Private_Part flag if in private part
3712
3713 if Ekind (Scope (Id)) = E_Package
3714 and then In_Private_Part (Scope (Id))
3715 then
3716 Set_In_Private_Part (Id);
3717 end if;
3718
3719 -- Check for violation of No_Local_Timing_Events
3720
3721 if Restriction_Check_Required (No_Local_Timing_Events)
3722 and then not Is_Library_Level_Entity (Id)
3723 and then Is_RTE (Etype (Id), RE_Timing_Event)
3724 then
3725 Check_Restriction (No_Local_Timing_Events, N);
3726 end if;
3727
3728 <<Leave>>
3729 if Has_Aspects (N) then
3730 Analyze_Aspect_Specifications (N, Id);
3731 end if;
3732
3733 Analyze_Dimension (N);
3734
3735 -- Verify whether the object declaration introduces an illegal hidden
3736 -- state within a package subject to a null abstract state.
3737
3738 if Formal_Extensions and then Ekind (Id) = E_Variable then
3739 Check_No_Hidden_State (Id);
3740 end if;
3741 end Analyze_Object_Declaration;
3742
3743 ---------------------------
3744 -- Analyze_Others_Choice --
3745 ---------------------------
3746
3747 -- Nothing to do for the others choice node itself, the semantic analysis
3748 -- of the others choice will occur as part of the processing of the parent
3749
3750 procedure Analyze_Others_Choice (N : Node_Id) is
3751 pragma Warnings (Off, N);
3752 begin
3753 null;
3754 end Analyze_Others_Choice;
3755
3756 -------------------------------------------
3757 -- Analyze_Private_Extension_Declaration --
3758 -------------------------------------------
3759
3760 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3761 T : constant Entity_Id := Defining_Identifier (N);
3762 Indic : constant Node_Id := Subtype_Indication (N);
3763 Parent_Type : Entity_Id;
3764 Parent_Base : Entity_Id;
3765
3766 begin
3767 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3768
3769 if Is_Non_Empty_List (Interface_List (N)) then
3770 declare
3771 Intf : Node_Id;
3772 T : Entity_Id;
3773
3774 begin
3775 Intf := First (Interface_List (N));
3776 while Present (Intf) loop
3777 T := Find_Type_Of_Subtype_Indic (Intf);
3778
3779 Diagnose_Interface (Intf, T);
3780 Next (Intf);
3781 end loop;
3782 end;
3783 end if;
3784
3785 Generate_Definition (T);
3786
3787 -- For other than Ada 2012, just enter the name in the current scope
3788
3789 if Ada_Version < Ada_2012 then
3790 Enter_Name (T);
3791
3792 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3793 -- case of private type that completes an incomplete type.
3794
3795 else
3796 declare
3797 Prev : Entity_Id;
3798
3799 begin
3800 Prev := Find_Type_Name (N);
3801
3802 pragma Assert (Prev = T
3803 or else (Ekind (Prev) = E_Incomplete_Type
3804 and then Present (Full_View (Prev))
3805 and then Full_View (Prev) = T));
3806 end;
3807 end if;
3808
3809 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3810 Parent_Base := Base_Type (Parent_Type);
3811
3812 if Parent_Type = Any_Type
3813 or else Etype (Parent_Type) = Any_Type
3814 then
3815 Set_Ekind (T, Ekind (Parent_Type));
3816 Set_Etype (T, Any_Type);
3817 goto Leave;
3818
3819 elsif not Is_Tagged_Type (Parent_Type) then
3820 Error_Msg_N
3821 ("parent of type extension must be a tagged type ", Indic);
3822 goto Leave;
3823
3824 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3825 Error_Msg_N ("premature derivation of incomplete type", Indic);
3826 goto Leave;
3827
3828 elsif Is_Concurrent_Type (Parent_Type) then
3829 Error_Msg_N
3830 ("parent type of a private extension cannot be "
3831 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3832
3833 Set_Etype (T, Any_Type);
3834 Set_Ekind (T, E_Limited_Private_Type);
3835 Set_Private_Dependents (T, New_Elmt_List);
3836 Set_Error_Posted (T);
3837 goto Leave;
3838 end if;
3839
3840 -- Perhaps the parent type should be changed to the class-wide type's
3841 -- specific type in this case to prevent cascading errors ???
3842
3843 if Is_Class_Wide_Type (Parent_Type) then
3844 Error_Msg_N
3845 ("parent of type extension must not be a class-wide type", Indic);
3846 goto Leave;
3847 end if;
3848
3849 if (not Is_Package_Or_Generic_Package (Current_Scope)
3850 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3851 or else In_Private_Part (Current_Scope)
3852
3853 then
3854 Error_Msg_N ("invalid context for private extension", N);
3855 end if;
3856
3857 -- Set common attributes
3858
3859 Set_Is_Pure (T, Is_Pure (Current_Scope));
3860 Set_Scope (T, Current_Scope);
3861 Set_Ekind (T, E_Record_Type_With_Private);
3862 Init_Size_Align (T);
3863
3864 Set_Etype (T, Parent_Base);
3865 Set_Has_Task (T, Has_Task (Parent_Base));
3866
3867 Set_Convention (T, Convention (Parent_Type));
3868 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3869 Set_Is_First_Subtype (T);
3870 Make_Class_Wide_Type (T);
3871
3872 if Unknown_Discriminants_Present (N) then
3873 Set_Discriminant_Constraint (T, No_Elist);
3874 end if;
3875
3876 Build_Derived_Record_Type (N, Parent_Type, T);
3877
3878 -- Propagate inherited invariant information. The new type has
3879 -- invariants, if the parent type has inheritable invariants,
3880 -- and these invariants can in turn be inherited.
3881
3882 if Has_Inheritable_Invariants (Parent_Type) then
3883 Set_Has_Inheritable_Invariants (T);
3884 Set_Has_Invariants (T);
3885 end if;
3886
3887 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3888 -- synchronized formal derived type.
3889
3890 if Ada_Version >= Ada_2005
3891 and then Synchronized_Present (N)
3892 then
3893 Set_Is_Limited_Record (T);
3894
3895 -- Formal derived type case
3896
3897 if Is_Generic_Type (T) then
3898
3899 -- The parent must be a tagged limited type or a synchronized
3900 -- interface.
3901
3902 if (not Is_Tagged_Type (Parent_Type)
3903 or else not Is_Limited_Type (Parent_Type))
3904 and then
3905 (not Is_Interface (Parent_Type)
3906 or else not Is_Synchronized_Interface (Parent_Type))
3907 then
3908 Error_Msg_NE ("parent type of & must be tagged limited " &
3909 "or synchronized", N, T);
3910 end if;
3911
3912 -- The progenitors (if any) must be limited or synchronized
3913 -- interfaces.
3914
3915 if Present (Interfaces (T)) then
3916 declare
3917 Iface : Entity_Id;
3918 Iface_Elmt : Elmt_Id;
3919
3920 begin
3921 Iface_Elmt := First_Elmt (Interfaces (T));
3922 while Present (Iface_Elmt) loop
3923 Iface := Node (Iface_Elmt);
3924
3925 if not Is_Limited_Interface (Iface)
3926 and then not Is_Synchronized_Interface (Iface)
3927 then
3928 Error_Msg_NE ("progenitor & must be limited " &
3929 "or synchronized", N, Iface);
3930 end if;
3931
3932 Next_Elmt (Iface_Elmt);
3933 end loop;
3934 end;
3935 end if;
3936
3937 -- Regular derived extension, the parent must be a limited or
3938 -- synchronized interface.
3939
3940 else
3941 if not Is_Interface (Parent_Type)
3942 or else (not Is_Limited_Interface (Parent_Type)
3943 and then
3944 not Is_Synchronized_Interface (Parent_Type))
3945 then
3946 Error_Msg_NE
3947 ("parent type of & must be limited interface", N, T);
3948 end if;
3949 end if;
3950
3951 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3952 -- extension with a synchronized parent must be explicitly declared
3953 -- synchronized, because the full view will be a synchronized type.
3954 -- This must be checked before the check for limited types below,
3955 -- to ensure that types declared limited are not allowed to extend
3956 -- synchronized interfaces.
3957
3958 elsif Is_Interface (Parent_Type)
3959 and then Is_Synchronized_Interface (Parent_Type)
3960 and then not Synchronized_Present (N)
3961 then
3962 Error_Msg_NE
3963 ("private extension of& must be explicitly synchronized",
3964 N, Parent_Type);
3965
3966 elsif Limited_Present (N) then
3967 Set_Is_Limited_Record (T);
3968
3969 if not Is_Limited_Type (Parent_Type)
3970 and then
3971 (not Is_Interface (Parent_Type)
3972 or else not Is_Limited_Interface (Parent_Type))
3973 then
3974 Error_Msg_NE ("parent type& of limited extension must be limited",
3975 N, Parent_Type);
3976 end if;
3977 end if;
3978
3979 <<Leave>>
3980 if Has_Aspects (N) then
3981 Analyze_Aspect_Specifications (N, T);
3982 end if;
3983 end Analyze_Private_Extension_Declaration;
3984
3985 ---------------------------------
3986 -- Analyze_Subtype_Declaration --
3987 ---------------------------------
3988
3989 procedure Analyze_Subtype_Declaration
3990 (N : Node_Id;
3991 Skip : Boolean := False)
3992 is
3993 Id : constant Entity_Id := Defining_Identifier (N);
3994 T : Entity_Id;
3995 R_Checks : Check_Result;
3996
3997 begin
3998 Generate_Definition (Id);
3999 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4000 Init_Size_Align (Id);
4001
4002 -- The following guard condition on Enter_Name is to handle cases where
4003 -- the defining identifier has already been entered into the scope but
4004 -- the declaration as a whole needs to be analyzed.
4005
4006 -- This case in particular happens for derived enumeration types. The
4007 -- derived enumeration type is processed as an inserted enumeration type
4008 -- declaration followed by a rewritten subtype declaration. The defining
4009 -- identifier, however, is entered into the name scope very early in the
4010 -- processing of the original type declaration and therefore needs to be
4011 -- avoided here, when the created subtype declaration is analyzed. (See
4012 -- Build_Derived_Types)
4013
4014 -- This also happens when the full view of a private type is derived
4015 -- type with constraints. In this case the entity has been introduced
4016 -- in the private declaration.
4017
4018 if Skip
4019 or else (Present (Etype (Id))
4020 and then (Is_Private_Type (Etype (Id))
4021 or else Is_Task_Type (Etype (Id))
4022 or else Is_Rewrite_Substitution (N)))
4023 then
4024 null;
4025
4026 else
4027 Enter_Name (Id);
4028 end if;
4029
4030 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4031
4032 -- Class-wide equivalent types of records with unknown discriminants
4033 -- involve the generation of an itype which serves as the private view
4034 -- of a constrained record subtype. In such cases the base type of the
4035 -- current subtype we are processing is the private itype. Use the full
4036 -- of the private itype when decorating various attributes.
4037
4038 if Is_Itype (T)
4039 and then Is_Private_Type (T)
4040 and then Present (Full_View (T))
4041 then
4042 T := Full_View (T);
4043 end if;
4044
4045 -- Inherit common attributes
4046
4047 Set_Is_Volatile (Id, Is_Volatile (T));
4048 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4049 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4050 Set_Convention (Id, Convention (T));
4051
4052 -- If ancestor has predicates then so does the subtype, and in addition
4053 -- we must delay the freeze to properly arrange predicate inheritance.
4054
4055 -- The Ancestor_Type test is a big kludge, there seem to be cases in
4056 -- which T = ID, so the above tests and assignments do nothing???
4057
4058 if Has_Predicates (T)
4059 or else (Present (Ancestor_Subtype (T))
4060 and then Has_Predicates (Ancestor_Subtype (T)))
4061 then
4062 Set_Has_Predicates (Id);
4063 Set_Has_Delayed_Freeze (Id);
4064 end if;
4065
4066 -- Subtype of Boolean cannot have a constraint in SPARK
4067
4068 if Is_Boolean_Type (T)
4069 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4070 then
4071 Check_SPARK_Restriction
4072 ("subtype of Boolean cannot have constraint", N);
4073 end if;
4074
4075 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4076 declare
4077 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4078 One_Cstr : Node_Id;
4079 Low : Node_Id;
4080 High : Node_Id;
4081
4082 begin
4083 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4084 One_Cstr := First (Constraints (Cstr));
4085 while Present (One_Cstr) loop
4086
4087 -- Index or discriminant constraint in SPARK must be a
4088 -- subtype mark.
4089
4090 if not
4091 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4092 then
4093 Check_SPARK_Restriction
4094 ("subtype mark required", One_Cstr);
4095
4096 -- String subtype must have a lower bound of 1 in SPARK.
4097 -- Note that we do not need to test for the non-static case
4098 -- here, since that was already taken care of in
4099 -- Process_Range_Expr_In_Decl.
4100
4101 elsif Base_Type (T) = Standard_String then
4102 Get_Index_Bounds (One_Cstr, Low, High);
4103
4104 if Is_OK_Static_Expression (Low)
4105 and then Expr_Value (Low) /= 1
4106 then
4107 Check_SPARK_Restriction
4108 ("String subtype must have lower bound of 1", N);
4109 end if;
4110 end if;
4111
4112 Next (One_Cstr);
4113 end loop;
4114 end if;
4115 end;
4116 end if;
4117
4118 -- In the case where there is no constraint given in the subtype
4119 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4120 -- semantic attributes must be established here.
4121
4122 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4123 Set_Etype (Id, Base_Type (T));
4124
4125 -- Subtype of unconstrained array without constraint is not allowed
4126 -- in SPARK.
4127
4128 if Is_Array_Type (T)
4129 and then not Is_Constrained (T)
4130 then
4131 Check_SPARK_Restriction
4132 ("subtype of unconstrained array must have constraint", N);
4133 end if;
4134
4135 case Ekind (T) is
4136 when Array_Kind =>
4137 Set_Ekind (Id, E_Array_Subtype);
4138 Copy_Array_Subtype_Attributes (Id, T);
4139
4140 when Decimal_Fixed_Point_Kind =>
4141 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4142 Set_Digits_Value (Id, Digits_Value (T));
4143 Set_Delta_Value (Id, Delta_Value (T));
4144 Set_Scale_Value (Id, Scale_Value (T));
4145 Set_Small_Value (Id, Small_Value (T));
4146 Set_Scalar_Range (Id, Scalar_Range (T));
4147 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4148 Set_Is_Constrained (Id, Is_Constrained (T));
4149 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4150 Set_RM_Size (Id, RM_Size (T));
4151
4152 when Enumeration_Kind =>
4153 Set_Ekind (Id, E_Enumeration_Subtype);
4154 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4155 Set_Scalar_Range (Id, Scalar_Range (T));
4156 Set_Is_Character_Type (Id, Is_Character_Type (T));
4157 Set_Is_Constrained (Id, Is_Constrained (T));
4158 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4159 Set_RM_Size (Id, RM_Size (T));
4160
4161 when Ordinary_Fixed_Point_Kind =>
4162 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4163 Set_Scalar_Range (Id, Scalar_Range (T));
4164 Set_Small_Value (Id, Small_Value (T));
4165 Set_Delta_Value (Id, Delta_Value (T));
4166 Set_Is_Constrained (Id, Is_Constrained (T));
4167 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4168 Set_RM_Size (Id, RM_Size (T));
4169
4170 when Float_Kind =>
4171 Set_Ekind (Id, E_Floating_Point_Subtype);
4172 Set_Scalar_Range (Id, Scalar_Range (T));
4173 Set_Digits_Value (Id, Digits_Value (T));
4174 Set_Is_Constrained (Id, Is_Constrained (T));
4175
4176 when Signed_Integer_Kind =>
4177 Set_Ekind (Id, E_Signed_Integer_Subtype);
4178 Set_Scalar_Range (Id, Scalar_Range (T));
4179 Set_Is_Constrained (Id, Is_Constrained (T));
4180 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4181 Set_RM_Size (Id, RM_Size (T));
4182
4183 when Modular_Integer_Kind =>
4184 Set_Ekind (Id, E_Modular_Integer_Subtype);
4185 Set_Scalar_Range (Id, Scalar_Range (T));
4186 Set_Is_Constrained (Id, Is_Constrained (T));
4187 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4188 Set_RM_Size (Id, RM_Size (T));
4189
4190 when Class_Wide_Kind =>
4191 Set_Ekind (Id, E_Class_Wide_Subtype);
4192 Set_First_Entity (Id, First_Entity (T));
4193 Set_Last_Entity (Id, Last_Entity (T));
4194 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4195 Set_Cloned_Subtype (Id, T);
4196 Set_Is_Tagged_Type (Id, True);
4197 Set_Has_Unknown_Discriminants
4198 (Id, True);
4199
4200 if Ekind (T) = E_Class_Wide_Subtype then
4201 Set_Equivalent_Type (Id, Equivalent_Type (T));
4202 end if;
4203
4204 when E_Record_Type | E_Record_Subtype =>
4205 Set_Ekind (Id, E_Record_Subtype);
4206
4207 if Ekind (T) = E_Record_Subtype
4208 and then Present (Cloned_Subtype (T))
4209 then
4210 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4211 else
4212 Set_Cloned_Subtype (Id, T);
4213 end if;
4214
4215 Set_First_Entity (Id, First_Entity (T));
4216 Set_Last_Entity (Id, Last_Entity (T));
4217 Set_Has_Discriminants (Id, Has_Discriminants (T));
4218 Set_Is_Constrained (Id, Is_Constrained (T));
4219 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4220 Set_Has_Implicit_Dereference
4221 (Id, Has_Implicit_Dereference (T));
4222 Set_Has_Unknown_Discriminants
4223 (Id, Has_Unknown_Discriminants (T));
4224
4225 if Has_Discriminants (T) then
4226 Set_Discriminant_Constraint
4227 (Id, Discriminant_Constraint (T));
4228 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4229
4230 elsif Has_Unknown_Discriminants (Id) then
4231 Set_Discriminant_Constraint (Id, No_Elist);
4232 end if;
4233
4234 if Is_Tagged_Type (T) then
4235 Set_Is_Tagged_Type (Id);
4236 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4237 Set_Direct_Primitive_Operations
4238 (Id, Direct_Primitive_Operations (T));
4239 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4240
4241 if Is_Interface (T) then
4242 Set_Is_Interface (Id);
4243 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4244 end if;
4245 end if;
4246
4247 when Private_Kind =>
4248 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4249 Set_Has_Discriminants (Id, Has_Discriminants (T));
4250 Set_Is_Constrained (Id, Is_Constrained (T));
4251 Set_First_Entity (Id, First_Entity (T));
4252 Set_Last_Entity (Id, Last_Entity (T));
4253 Set_Private_Dependents (Id, New_Elmt_List);
4254 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4255 Set_Has_Implicit_Dereference
4256 (Id, Has_Implicit_Dereference (T));
4257 Set_Has_Unknown_Discriminants
4258 (Id, Has_Unknown_Discriminants (T));
4259 Set_Known_To_Have_Preelab_Init
4260 (Id, Known_To_Have_Preelab_Init (T));
4261
4262 if Is_Tagged_Type (T) then
4263 Set_Is_Tagged_Type (Id);
4264 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4265 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4266 Set_Direct_Primitive_Operations (Id,
4267 Direct_Primitive_Operations (T));
4268 end if;
4269
4270 -- In general the attributes of the subtype of a private type
4271 -- are the attributes of the partial view of parent. However,
4272 -- the full view may be a discriminated type, and the subtype
4273 -- must share the discriminant constraint to generate correct
4274 -- calls to initialization procedures.
4275
4276 if Has_Discriminants (T) then
4277 Set_Discriminant_Constraint
4278 (Id, Discriminant_Constraint (T));
4279 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4280
4281 elsif Present (Full_View (T))
4282 and then Has_Discriminants (Full_View (T))
4283 then
4284 Set_Discriminant_Constraint
4285 (Id, Discriminant_Constraint (Full_View (T)));
4286 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4287
4288 -- This would seem semantically correct, but apparently
4289 -- generates spurious errors about missing components ???
4290
4291 -- Set_Has_Discriminants (Id);
4292 end if;
4293
4294 Prepare_Private_Subtype_Completion (Id, N);
4295
4296 -- If this is the subtype of a constrained private type with
4297 -- discriminants that has got a full view and we also have
4298 -- built a completion just above, show that the completion
4299 -- is a clone of the full view to the back-end.
4300
4301 if Has_Discriminants (T)
4302 and then not Has_Unknown_Discriminants (T)
4303 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4304 and then Present (Full_View (T))
4305 and then Present (Full_View (Id))
4306 then
4307 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4308 end if;
4309
4310 when Access_Kind =>
4311 Set_Ekind (Id, E_Access_Subtype);
4312 Set_Is_Constrained (Id, Is_Constrained (T));
4313 Set_Is_Access_Constant
4314 (Id, Is_Access_Constant (T));
4315 Set_Directly_Designated_Type
4316 (Id, Designated_Type (T));
4317 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4318
4319 -- A Pure library_item must not contain the declaration of a
4320 -- named access type, except within a subprogram, generic
4321 -- subprogram, task unit, or protected unit, or if it has
4322 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4323
4324 if Comes_From_Source (Id)
4325 and then In_Pure_Unit
4326 and then not In_Subprogram_Task_Protected_Unit
4327 and then not No_Pool_Assigned (Id)
4328 then
4329 Error_Msg_N
4330 ("named access types not allowed in pure unit", N);
4331 end if;
4332
4333 when Concurrent_Kind =>
4334 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4335 Set_Corresponding_Record_Type (Id,
4336 Corresponding_Record_Type (T));
4337 Set_First_Entity (Id, First_Entity (T));
4338 Set_First_Private_Entity (Id, First_Private_Entity (T));
4339 Set_Has_Discriminants (Id, Has_Discriminants (T));
4340 Set_Is_Constrained (Id, Is_Constrained (T));
4341 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4342 Set_Last_Entity (Id, Last_Entity (T));
4343
4344 if Has_Discriminants (T) then
4345 Set_Discriminant_Constraint (Id,
4346 Discriminant_Constraint (T));
4347 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4348 end if;
4349
4350 when E_Incomplete_Type =>
4351 if Ada_Version >= Ada_2005 then
4352
4353 -- In Ada 2005 an incomplete type can be explicitly tagged:
4354 -- propagate indication.
4355
4356 Set_Ekind (Id, E_Incomplete_Subtype);
4357 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4358 Set_Private_Dependents (Id, New_Elmt_List);
4359
4360 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
4361 -- incomplete type visible through a limited with clause.
4362
4363 if From_With_Type (T)
4364 and then Present (Non_Limited_View (T))
4365 then
4366 Set_From_With_Type (Id);
4367 Set_Non_Limited_View (Id, Non_Limited_View (T));
4368
4369 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4370 -- to the private dependents of the original incomplete
4371 -- type for future transformation.
4372
4373 else
4374 Append_Elmt (Id, Private_Dependents (T));
4375 end if;
4376
4377 -- If the subtype name denotes an incomplete type an error
4378 -- was already reported by Process_Subtype.
4379
4380 else
4381 Set_Etype (Id, Any_Type);
4382 end if;
4383
4384 when others =>
4385 raise Program_Error;
4386 end case;
4387 end if;
4388
4389 if Etype (Id) = Any_Type then
4390 goto Leave;
4391 end if;
4392
4393 -- Some common processing on all types
4394
4395 Set_Size_Info (Id, T);
4396 Set_First_Rep_Item (Id, First_Rep_Item (T));
4397
4398 -- If the parent type is a generic actual, so is the subtype. This may
4399 -- happen in a nested instance. Why Comes_From_Source test???
4400
4401 if not Comes_From_Source (N) then
4402 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
4403 end if;
4404
4405 T := Etype (Id);
4406
4407 Set_Is_Immediately_Visible (Id, True);
4408 Set_Depends_On_Private (Id, Has_Private_Component (T));
4409 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4410
4411 if Is_Interface (T) then
4412 Set_Is_Interface (Id);
4413 end if;
4414
4415 if Present (Generic_Parent_Type (N))
4416 and then
4417 (Nkind
4418 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4419 or else Nkind
4420 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4421 /= N_Formal_Private_Type_Definition)
4422 then
4423 if Is_Tagged_Type (Id) then
4424
4425 -- If this is a generic actual subtype for a synchronized type,
4426 -- the primitive operations are those of the corresponding record
4427 -- for which there is a separate subtype declaration.
4428
4429 if Is_Concurrent_Type (Id) then
4430 null;
4431 elsif Is_Class_Wide_Type (Id) then
4432 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4433 else
4434 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4435 end if;
4436
4437 elsif Scope (Etype (Id)) /= Standard_Standard then
4438 Derive_Subprograms (Generic_Parent_Type (N), Id);
4439 end if;
4440 end if;
4441
4442 if Is_Private_Type (T)
4443 and then Present (Full_View (T))
4444 then
4445 Conditional_Delay (Id, Full_View (T));
4446
4447 -- The subtypes of components or subcomponents of protected types
4448 -- do not need freeze nodes, which would otherwise appear in the
4449 -- wrong scope (before the freeze node for the protected type). The
4450 -- proper subtypes are those of the subcomponents of the corresponding
4451 -- record.
4452
4453 elsif Ekind (Scope (Id)) /= E_Protected_Type
4454 and then Present (Scope (Scope (Id))) -- error defense!
4455 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4456 then
4457 Conditional_Delay (Id, T);
4458 end if;
4459
4460 -- Check that Constraint_Error is raised for a scalar subtype indication
4461 -- when the lower or upper bound of a non-null range lies outside the
4462 -- range of the type mark.
4463
4464 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4465 if Is_Scalar_Type (Etype (Id))
4466 and then Scalar_Range (Id) /=
4467 Scalar_Range (Etype (Subtype_Mark
4468 (Subtype_Indication (N))))
4469 then
4470 Apply_Range_Check
4471 (Scalar_Range (Id),
4472 Etype (Subtype_Mark (Subtype_Indication (N))));
4473
4474 -- In the array case, check compatibility for each index
4475
4476 elsif Is_Array_Type (Etype (Id))
4477 and then Present (First_Index (Id))
4478 then
4479 -- This really should be a subprogram that finds the indications
4480 -- to check???
4481
4482 declare
4483 Subt_Index : Node_Id := First_Index (Id);
4484 Target_Index : Node_Id :=
4485 First_Index (Etype
4486 (Subtype_Mark (Subtype_Indication (N))));
4487 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4488
4489 begin
4490 while Present (Subt_Index) loop
4491 if ((Nkind (Subt_Index) = N_Identifier
4492 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4493 or else Nkind (Subt_Index) = N_Subtype_Indication)
4494 and then
4495 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4496 then
4497 declare
4498 Target_Typ : constant Entity_Id :=
4499 Etype (Target_Index);
4500 begin
4501 R_Checks :=
4502 Get_Range_Checks
4503 (Scalar_Range (Etype (Subt_Index)),
4504 Target_Typ,
4505 Etype (Subt_Index),
4506 Defining_Identifier (N));
4507
4508 -- Reset Has_Dynamic_Range_Check on the subtype to
4509 -- prevent elision of the index check due to a dynamic
4510 -- check generated for a preceding index (needed since
4511 -- Insert_Range_Checks tries to avoid generating
4512 -- redundant checks on a given declaration).
4513
4514 Set_Has_Dynamic_Range_Check (N, False);
4515
4516 Insert_Range_Checks
4517 (R_Checks,
4518 N,
4519 Target_Typ,
4520 Sloc (Defining_Identifier (N)));
4521
4522 -- Record whether this index involved a dynamic check
4523
4524 Has_Dyn_Chk :=
4525 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4526 end;
4527 end if;
4528
4529 Next_Index (Subt_Index);
4530 Next_Index (Target_Index);
4531 end loop;
4532
4533 -- Finally, mark whether the subtype involves dynamic checks
4534
4535 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4536 end;
4537 end if;
4538 end if;
4539
4540 -- Make sure that generic actual types are properly frozen. The subtype
4541 -- is marked as a generic actual type when the enclosing instance is
4542 -- analyzed, so here we identify the subtype from the tree structure.
4543
4544 if Expander_Active
4545 and then Is_Generic_Actual_Type (Id)
4546 and then In_Instance
4547 and then not Comes_From_Source (N)
4548 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4549 and then Is_Frozen (T)
4550 then
4551 Freeze_Before (N, Id);
4552 end if;
4553
4554 Set_Optimize_Alignment_Flags (Id);
4555 Check_Eliminated (Id);
4556
4557 <<Leave>>
4558 if Has_Aspects (N) then
4559 Analyze_Aspect_Specifications (N, Id);
4560 end if;
4561
4562 Analyze_Dimension (N);
4563 end Analyze_Subtype_Declaration;
4564
4565 --------------------------------
4566 -- Analyze_Subtype_Indication --
4567 --------------------------------
4568
4569 procedure Analyze_Subtype_Indication (N : Node_Id) is
4570 T : constant Entity_Id := Subtype_Mark (N);
4571 R : constant Node_Id := Range_Expression (Constraint (N));
4572
4573 begin
4574 Analyze (T);
4575
4576 if R /= Error then
4577 Analyze (R);
4578 Set_Etype (N, Etype (R));
4579 Resolve (R, Entity (T));
4580 else
4581 Set_Error_Posted (R);
4582 Set_Error_Posted (T);
4583 end if;
4584 end Analyze_Subtype_Indication;
4585
4586 --------------------------
4587 -- Analyze_Variant_Part --
4588 --------------------------
4589
4590 procedure Analyze_Variant_Part (N : Node_Id) is
4591 Discr_Name : Node_Id;
4592 Discr_Type : Entity_Id;
4593
4594 procedure Process_Variant (A : Node_Id);
4595 -- Analyze declarations for a single variant
4596
4597 package Analyze_Variant_Choices is
4598 new Generic_Analyze_Choices (Process_Variant);
4599 use Analyze_Variant_Choices;
4600
4601 ---------------------
4602 -- Process_Variant --
4603 ---------------------
4604
4605 procedure Process_Variant (A : Node_Id) is
4606 CL : constant Node_Id := Component_List (A);
4607 begin
4608 if not Null_Present (CL) then
4609 Analyze_Declarations (Component_Items (CL));
4610
4611 if Present (Variant_Part (CL)) then
4612 Analyze (Variant_Part (CL));
4613 end if;
4614 end if;
4615 end Process_Variant;
4616
4617 -- Start of processing for Analyze_Variant_Part
4618
4619 begin
4620 Discr_Name := Name (N);
4621 Analyze (Discr_Name);
4622
4623 -- If Discr_Name bad, get out (prevent cascaded errors)
4624
4625 if Etype (Discr_Name) = Any_Type then
4626 return;
4627 end if;
4628
4629 -- Check invalid discriminant in variant part
4630
4631 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4632 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4633 end if;
4634
4635 Discr_Type := Etype (Entity (Discr_Name));
4636
4637 if not Is_Discrete_Type (Discr_Type) then
4638 Error_Msg_N
4639 ("discriminant in a variant part must be of a discrete type",
4640 Name (N));
4641 return;
4642 end if;
4643
4644 -- Now analyze the choices, which also analyzes the declarations that
4645 -- are associated with each choice.
4646
4647 Analyze_Choices (Variants (N), Discr_Type);
4648
4649 -- Note: we used to instantiate and call Check_Choices here to check
4650 -- that the choices covered the discriminant, but it's too early to do
4651 -- that because of statically predicated subtypes, whose analysis may
4652 -- be deferred to their freeze point which may be as late as the freeze
4653 -- point of the containing record. So this call is now to be found in
4654 -- Freeze_Record_Declaration.
4655
4656 end Analyze_Variant_Part;
4657
4658 ----------------------------
4659 -- Array_Type_Declaration --
4660 ----------------------------
4661
4662 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4663 Component_Def : constant Node_Id := Component_Definition (Def);
4664 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4665 Element_Type : Entity_Id;
4666 Implicit_Base : Entity_Id;
4667 Index : Node_Id;
4668 Related_Id : Entity_Id := Empty;
4669 Nb_Index : Nat;
4670 P : constant Node_Id := Parent (Def);
4671 Priv : Entity_Id;
4672
4673 begin
4674 if Nkind (Def) = N_Constrained_Array_Definition then
4675 Index := First (Discrete_Subtype_Definitions (Def));
4676 else
4677 Index := First (Subtype_Marks (Def));
4678 end if;
4679
4680 -- Find proper names for the implicit types which may be public. In case
4681 -- of anonymous arrays we use the name of the first object of that type
4682 -- as prefix.
4683
4684 if No (T) then
4685 Related_Id := Defining_Identifier (P);
4686 else
4687 Related_Id := T;
4688 end if;
4689
4690 Nb_Index := 1;
4691 while Present (Index) loop
4692 Analyze (Index);
4693
4694 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
4695 Check_SPARK_Restriction ("subtype mark required", Index);
4696 end if;
4697
4698 -- Add a subtype declaration for each index of private array type
4699 -- declaration whose etype is also private. For example:
4700
4701 -- package Pkg is
4702 -- type Index is private;
4703 -- private
4704 -- type Table is array (Index) of ...
4705 -- end;
4706
4707 -- This is currently required by the expander for the internally
4708 -- generated equality subprogram of records with variant parts in
4709 -- which the etype of some component is such private type.
4710
4711 if Ekind (Current_Scope) = E_Package
4712 and then In_Private_Part (Current_Scope)
4713 and then Has_Private_Declaration (Etype (Index))
4714 then
4715 declare
4716 Loc : constant Source_Ptr := Sloc (Def);
4717 New_E : Entity_Id;
4718 Decl : Entity_Id;
4719
4720 begin
4721 New_E := Make_Temporary (Loc, 'T');
4722 Set_Is_Internal (New_E);
4723
4724 Decl :=
4725 Make_Subtype_Declaration (Loc,
4726 Defining_Identifier => New_E,
4727 Subtype_Indication =>
4728 New_Occurrence_Of (Etype (Index), Loc));
4729
4730 Insert_Before (Parent (Def), Decl);
4731 Analyze (Decl);
4732 Set_Etype (Index, New_E);
4733
4734 -- If the index is a range the Entity attribute is not
4735 -- available. Example:
4736
4737 -- package Pkg is
4738 -- type T is private;
4739 -- private
4740 -- type T is new Natural;
4741 -- Table : array (T(1) .. T(10)) of Boolean;
4742 -- end Pkg;
4743
4744 if Nkind (Index) /= N_Range then
4745 Set_Entity (Index, New_E);
4746 end if;
4747 end;
4748 end if;
4749
4750 Make_Index (Index, P, Related_Id, Nb_Index);
4751
4752 -- Check error of subtype with predicate for index type
4753
4754 Bad_Predicated_Subtype_Use
4755 ("subtype& has predicate, not allowed as index subtype",
4756 Index, Etype (Index));
4757
4758 -- Move to next index
4759
4760 Next_Index (Index);
4761 Nb_Index := Nb_Index + 1;
4762 end loop;
4763
4764 -- Process subtype indication if one is present
4765
4766 if Present (Component_Typ) then
4767 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4768
4769 Set_Etype (Component_Typ, Element_Type);
4770
4771 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
4772 Check_SPARK_Restriction ("subtype mark required", Component_Typ);
4773 end if;
4774
4775 -- Ada 2005 (AI-230): Access Definition case
4776
4777 else pragma Assert (Present (Access_Definition (Component_Def)));
4778
4779 -- Indicate that the anonymous access type is created by the
4780 -- array type declaration.
4781
4782 Element_Type := Access_Definition
4783 (Related_Nod => P,
4784 N => Access_Definition (Component_Def));
4785 Set_Is_Local_Anonymous_Access (Element_Type);
4786
4787 -- Propagate the parent. This field is needed if we have to generate
4788 -- the master_id associated with an anonymous access to task type
4789 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4790
4791 Set_Parent (Element_Type, Parent (T));
4792
4793 -- Ada 2005 (AI-230): In case of components that are anonymous access
4794 -- types the level of accessibility depends on the enclosing type
4795 -- declaration
4796
4797 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4798
4799 -- Ada 2005 (AI-254)
4800
4801 declare
4802 CD : constant Node_Id :=
4803 Access_To_Subprogram_Definition
4804 (Access_Definition (Component_Def));
4805 begin
4806 if Present (CD) and then Protected_Present (CD) then
4807 Element_Type :=
4808 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4809 end if;
4810 end;
4811 end if;
4812
4813 -- Constrained array case
4814
4815 if No (T) then
4816 T := Create_Itype (E_Void, P, Related_Id, 'T');
4817 end if;
4818
4819 if Nkind (Def) = N_Constrained_Array_Definition then
4820
4821 -- Establish Implicit_Base as unconstrained base type
4822
4823 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4824
4825 Set_Etype (Implicit_Base, Implicit_Base);
4826 Set_Scope (Implicit_Base, Current_Scope);
4827 Set_Has_Delayed_Freeze (Implicit_Base);
4828
4829 -- The constrained array type is a subtype of the unconstrained one
4830
4831 Set_Ekind (T, E_Array_Subtype);
4832 Init_Size_Align (T);
4833 Set_Etype (T, Implicit_Base);
4834 Set_Scope (T, Current_Scope);
4835 Set_Is_Constrained (T, True);
4836 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4837 Set_Has_Delayed_Freeze (T);
4838
4839 -- Complete setup of implicit base type
4840
4841 Set_First_Index (Implicit_Base, First_Index (T));
4842 Set_Component_Type (Implicit_Base, Element_Type);
4843 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4844 Set_Component_Size (Implicit_Base, Uint_0);
4845 Set_Packed_Array_Type (Implicit_Base, Empty);
4846 Set_Has_Controlled_Component
4847 (Implicit_Base, Has_Controlled_Component
4848 (Element_Type)
4849 or else Is_Controlled
4850 (Element_Type));
4851 Set_Finalize_Storage_Only
4852 (Implicit_Base, Finalize_Storage_Only
4853 (Element_Type));
4854
4855 -- Unconstrained array case
4856
4857 else
4858 Set_Ekind (T, E_Array_Type);
4859 Init_Size_Align (T);
4860 Set_Etype (T, T);
4861 Set_Scope (T, Current_Scope);
4862 Set_Component_Size (T, Uint_0);
4863 Set_Is_Constrained (T, False);
4864 Set_First_Index (T, First (Subtype_Marks (Def)));
4865 Set_Has_Delayed_Freeze (T, True);
4866 Set_Has_Task (T, Has_Task (Element_Type));
4867 Set_Has_Controlled_Component (T, Has_Controlled_Component
4868 (Element_Type)
4869 or else
4870 Is_Controlled (Element_Type));
4871 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4872 (Element_Type));
4873 end if;
4874
4875 -- Common attributes for both cases
4876
4877 Set_Component_Type (Base_Type (T), Element_Type);
4878 Set_Packed_Array_Type (T, Empty);
4879
4880 if Aliased_Present (Component_Definition (Def)) then
4881 Check_SPARK_Restriction
4882 ("aliased is not allowed", Component_Definition (Def));
4883 Set_Has_Aliased_Components (Etype (T));
4884 end if;
4885
4886 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4887 -- array type to ensure that objects of this type are initialized.
4888
4889 if Ada_Version >= Ada_2005
4890 and then Can_Never_Be_Null (Element_Type)
4891 then
4892 Set_Can_Never_Be_Null (T);
4893
4894 if Null_Exclusion_Present (Component_Definition (Def))
4895
4896 -- No need to check itypes because in their case this check was
4897 -- done at their point of creation
4898
4899 and then not Is_Itype (Element_Type)
4900 then
4901 Error_Msg_N
4902 ("`NOT NULL` not allowed (null already excluded)",
4903 Subtype_Indication (Component_Definition (Def)));
4904 end if;
4905 end if;
4906
4907 Priv := Private_Component (Element_Type);
4908
4909 if Present (Priv) then
4910
4911 -- Check for circular definitions
4912
4913 if Priv = Any_Type then
4914 Set_Component_Type (Etype (T), Any_Type);
4915
4916 -- There is a gap in the visibility of operations on the composite
4917 -- type only if the component type is defined in a different scope.
4918
4919 elsif Scope (Priv) = Current_Scope then
4920 null;
4921
4922 elsif Is_Limited_Type (Priv) then
4923 Set_Is_Limited_Composite (Etype (T));
4924 Set_Is_Limited_Composite (T);
4925 else
4926 Set_Is_Private_Composite (Etype (T));
4927 Set_Is_Private_Composite (T);
4928 end if;
4929 end if;
4930
4931 -- A syntax error in the declaration itself may lead to an empty index
4932 -- list, in which case do a minimal patch.
4933
4934 if No (First_Index (T)) then
4935 Error_Msg_N ("missing index definition in array type declaration", T);
4936
4937 declare
4938 Indexes : constant List_Id :=
4939 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4940 begin
4941 Set_Discrete_Subtype_Definitions (Def, Indexes);
4942 Set_First_Index (T, First (Indexes));
4943 return;
4944 end;
4945 end if;
4946
4947 -- Create a concatenation operator for the new type. Internal array
4948 -- types created for packed entities do not need such, they are
4949 -- compatible with the user-defined type.
4950
4951 if Number_Dimensions (T) = 1
4952 and then not Is_Packed_Array_Type (T)
4953 then
4954 New_Concatenation_Op (T);
4955 end if;
4956
4957 -- In the case of an unconstrained array the parser has already verified
4958 -- that all the indexes are unconstrained but we still need to make sure
4959 -- that the element type is constrained.
4960
4961 if Is_Indefinite_Subtype (Element_Type) then
4962 Error_Msg_N
4963 ("unconstrained element type in array declaration",
4964 Subtype_Indication (Component_Def));
4965
4966 elsif Is_Abstract_Type (Element_Type) then
4967 Error_Msg_N
4968 ("the type of a component cannot be abstract",
4969 Subtype_Indication (Component_Def));
4970 end if;
4971
4972 -- There may be an invariant declared for the component type, but
4973 -- the construction of the component invariant checking procedure
4974 -- takes place during expansion.
4975 end Array_Type_Declaration;
4976
4977 ------------------------------------------------------
4978 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4979 ------------------------------------------------------
4980
4981 function Replace_Anonymous_Access_To_Protected_Subprogram
4982 (N : Node_Id) return Entity_Id
4983 is
4984 Loc : constant Source_Ptr := Sloc (N);
4985
4986 Curr_Scope : constant Scope_Stack_Entry :=
4987 Scope_Stack.Table (Scope_Stack.Last);
4988
4989 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4990
4991 Acc : Node_Id;
4992 -- Access definition in declaration
4993
4994 Comp : Node_Id;
4995 -- Object definition or formal definition with an access definition
4996
4997 Decl : Node_Id;
4998 -- Declaration of anonymous access to subprogram type
4999
5000 Spec : Node_Id;
5001 -- Original specification in access to subprogram
5002
5003 P : Node_Id;
5004
5005 begin
5006 Set_Is_Internal (Anon);
5007
5008 case Nkind (N) is
5009 when N_Component_Declaration |
5010 N_Unconstrained_Array_Definition |
5011 N_Constrained_Array_Definition =>
5012 Comp := Component_Definition (N);
5013 Acc := Access_Definition (Comp);
5014
5015 when N_Discriminant_Specification =>
5016 Comp := Discriminant_Type (N);
5017 Acc := Comp;
5018
5019 when N_Parameter_Specification =>
5020 Comp := Parameter_Type (N);
5021 Acc := Comp;
5022
5023 when N_Access_Function_Definition =>
5024 Comp := Result_Definition (N);
5025 Acc := Comp;
5026
5027 when N_Object_Declaration =>
5028 Comp := Object_Definition (N);
5029 Acc := Comp;
5030
5031 when N_Function_Specification =>
5032 Comp := Result_Definition (N);
5033 Acc := Comp;
5034
5035 when others =>
5036 raise Program_Error;
5037 end case;
5038
5039 Spec := Access_To_Subprogram_Definition (Acc);
5040
5041 Decl :=
5042 Make_Full_Type_Declaration (Loc,
5043 Defining_Identifier => Anon,
5044 Type_Definition => Copy_Separate_Tree (Spec));
5045
5046 Mark_Rewrite_Insertion (Decl);
5047
5048 -- In ASIS mode, analyze the profile on the original node, because
5049 -- the separate copy does not provide enough links to recover the
5050 -- original tree. Analysis is limited to type annotations, within
5051 -- a temporary scope that serves as an anonymous subprogram to collect
5052 -- otherwise useless temporaries and itypes.
5053
5054 if ASIS_Mode then
5055 declare
5056 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5057
5058 begin
5059 if Nkind (Spec) = N_Access_Function_Definition then
5060 Set_Ekind (Typ, E_Function);
5061 else
5062 Set_Ekind (Typ, E_Procedure);
5063 end if;
5064
5065 Set_Parent (Typ, N);
5066 Set_Scope (Typ, Current_Scope);
5067 Push_Scope (Typ);
5068
5069 Process_Formals (Parameter_Specifications (Spec), Spec);
5070
5071 if Nkind (Spec) = N_Access_Function_Definition then
5072 declare
5073 Def : constant Node_Id := Result_Definition (Spec);
5074
5075 begin
5076 -- The result might itself be an anonymous access type, so
5077 -- have to recurse.
5078
5079 if Nkind (Def) = N_Access_Definition then
5080 if Present (Access_To_Subprogram_Definition (Def)) then
5081 Set_Etype
5082 (Def,
5083 Replace_Anonymous_Access_To_Protected_Subprogram
5084 (Spec));
5085 else
5086 Find_Type (Subtype_Mark (Def));
5087 end if;
5088
5089 else
5090 Find_Type (Def);
5091 end if;
5092 end;
5093 end if;
5094
5095 End_Scope;
5096 end;
5097 end if;
5098
5099 -- Insert the new declaration in the nearest enclosing scope. If the
5100 -- node is a body and N is its return type, the declaration belongs in
5101 -- the enclosing scope.
5102
5103 P := Parent (N);
5104
5105 if Nkind (P) = N_Subprogram_Body
5106 and then Nkind (N) = N_Function_Specification
5107 then
5108 P := Parent (P);
5109 end if;
5110
5111 while Present (P) and then not Has_Declarations (P) loop
5112 P := Parent (P);
5113 end loop;
5114
5115 pragma Assert (Present (P));
5116
5117 if Nkind (P) = N_Package_Specification then
5118 Prepend (Decl, Visible_Declarations (P));
5119 else
5120 Prepend (Decl, Declarations (P));
5121 end if;
5122
5123 -- Replace the anonymous type with an occurrence of the new declaration.
5124 -- In all cases the rewritten node does not have the null-exclusion
5125 -- attribute because (if present) it was already inherited by the
5126 -- anonymous entity (Anon). Thus, in case of components we do not
5127 -- inherit this attribute.
5128
5129 if Nkind (N) = N_Parameter_Specification then
5130 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5131 Set_Etype (Defining_Identifier (N), Anon);
5132 Set_Null_Exclusion_Present (N, False);
5133
5134 elsif Nkind (N) = N_Object_Declaration then
5135 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5136 Set_Etype (Defining_Identifier (N), Anon);
5137
5138 elsif Nkind (N) = N_Access_Function_Definition then
5139 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5140
5141 elsif Nkind (N) = N_Function_Specification then
5142 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5143 Set_Etype (Defining_Unit_Name (N), Anon);
5144
5145 else
5146 Rewrite (Comp,
5147 Make_Component_Definition (Loc,
5148 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5149 end if;
5150
5151 Mark_Rewrite_Insertion (Comp);
5152
5153 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5154 Analyze (Decl);
5155
5156 else
5157 -- Temporarily remove the current scope (record or subprogram) from
5158 -- the stack to add the new declarations to the enclosing scope.
5159
5160 Scope_Stack.Decrement_Last;
5161 Analyze (Decl);
5162 Set_Is_Itype (Anon);
5163 Scope_Stack.Append (Curr_Scope);
5164 end if;
5165
5166 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5167 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5168 return Anon;
5169 end Replace_Anonymous_Access_To_Protected_Subprogram;
5170
5171 -------------------------------
5172 -- Build_Derived_Access_Type --
5173 -------------------------------
5174
5175 procedure Build_Derived_Access_Type
5176 (N : Node_Id;
5177 Parent_Type : Entity_Id;
5178 Derived_Type : Entity_Id)
5179 is
5180 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5181
5182 Desig_Type : Entity_Id;
5183 Discr : Entity_Id;
5184 Discr_Con_Elist : Elist_Id;
5185 Discr_Con_El : Elmt_Id;
5186 Subt : Entity_Id;
5187
5188 begin
5189 -- Set the designated type so it is available in case this is an access
5190 -- to a self-referential type, e.g. a standard list type with a next
5191 -- pointer. Will be reset after subtype is built.
5192
5193 Set_Directly_Designated_Type
5194 (Derived_Type, Designated_Type (Parent_Type));
5195
5196 Subt := Process_Subtype (S, N);
5197
5198 if Nkind (S) /= N_Subtype_Indication
5199 and then Subt /= Base_Type (Subt)
5200 then
5201 Set_Ekind (Derived_Type, E_Access_Subtype);
5202 end if;
5203
5204 if Ekind (Derived_Type) = E_Access_Subtype then
5205 declare
5206 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5207 Ibase : constant Entity_Id :=
5208 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5209 Svg_Chars : constant Name_Id := Chars (Ibase);
5210 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5211
5212 begin
5213 Copy_Node (Pbase, Ibase);
5214
5215 Set_Chars (Ibase, Svg_Chars);
5216 Set_Next_Entity (Ibase, Svg_Next_E);
5217 Set_Sloc (Ibase, Sloc (Derived_Type));
5218 Set_Scope (Ibase, Scope (Derived_Type));
5219 Set_Freeze_Node (Ibase, Empty);
5220 Set_Is_Frozen (Ibase, False);
5221 Set_Comes_From_Source (Ibase, False);
5222 Set_Is_First_Subtype (Ibase, False);
5223
5224 Set_Etype (Ibase, Pbase);
5225 Set_Etype (Derived_Type, Ibase);
5226 end;
5227 end if;
5228
5229 Set_Directly_Designated_Type
5230 (Derived_Type, Designated_Type (Subt));
5231
5232 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5233 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5234 Set_Size_Info (Derived_Type, Parent_Type);
5235 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5236 Set_Depends_On_Private (Derived_Type,
5237 Has_Private_Component (Derived_Type));
5238 Conditional_Delay (Derived_Type, Subt);
5239
5240 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5241 -- that it is not redundant.
5242
5243 if Null_Exclusion_Present (Type_Definition (N)) then
5244 Set_Can_Never_Be_Null (Derived_Type);
5245
5246 if Can_Never_Be_Null (Parent_Type)
5247 and then False
5248 then
5249 Error_Msg_NE
5250 ("`NOT NULL` not allowed (& already excludes null)",
5251 N, Parent_Type);
5252 end if;
5253
5254 elsif Can_Never_Be_Null (Parent_Type) then
5255 Set_Can_Never_Be_Null (Derived_Type);
5256 end if;
5257
5258 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5259 -- the root type for this information.
5260
5261 -- Apply range checks to discriminants for derived record case
5262 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5263
5264 Desig_Type := Designated_Type (Derived_Type);
5265 if Is_Composite_Type (Desig_Type)
5266 and then (not Is_Array_Type (Desig_Type))
5267 and then Has_Discriminants (Desig_Type)
5268 and then Base_Type (Desig_Type) /= Desig_Type
5269 then
5270 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5271 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5272
5273 Discr := First_Discriminant (Base_Type (Desig_Type));
5274 while Present (Discr_Con_El) loop
5275 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5276 Next_Elmt (Discr_Con_El);
5277 Next_Discriminant (Discr);
5278 end loop;
5279 end if;
5280 end Build_Derived_Access_Type;
5281
5282 ------------------------------
5283 -- Build_Derived_Array_Type --
5284 ------------------------------
5285
5286 procedure Build_Derived_Array_Type
5287 (N : Node_Id;
5288 Parent_Type : Entity_Id;
5289 Derived_Type : Entity_Id)
5290 is
5291 Loc : constant Source_Ptr := Sloc (N);
5292 Tdef : constant Node_Id := Type_Definition (N);
5293 Indic : constant Node_Id := Subtype_Indication (Tdef);
5294 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5295 Implicit_Base : Entity_Id;
5296 New_Indic : Node_Id;
5297
5298 procedure Make_Implicit_Base;
5299 -- If the parent subtype is constrained, the derived type is a subtype
5300 -- of an implicit base type derived from the parent base.
5301
5302 ------------------------
5303 -- Make_Implicit_Base --
5304 ------------------------
5305
5306 procedure Make_Implicit_Base is
5307 begin
5308 Implicit_Base :=
5309 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5310
5311 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5312 Set_Etype (Implicit_Base, Parent_Base);
5313
5314 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5315 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5316
5317 Set_Has_Delayed_Freeze (Implicit_Base, True);
5318 end Make_Implicit_Base;
5319
5320 -- Start of processing for Build_Derived_Array_Type
5321
5322 begin
5323 if not Is_Constrained (Parent_Type) then
5324 if Nkind (Indic) /= N_Subtype_Indication then
5325 Set_Ekind (Derived_Type, E_Array_Type);
5326
5327 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5328 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5329
5330 Set_Has_Delayed_Freeze (Derived_Type, True);
5331
5332 else
5333 Make_Implicit_Base;
5334 Set_Etype (Derived_Type, Implicit_Base);
5335
5336 New_Indic :=
5337 Make_Subtype_Declaration (Loc,
5338 Defining_Identifier => Derived_Type,
5339 Subtype_Indication =>
5340 Make_Subtype_Indication (Loc,
5341 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5342 Constraint => Constraint (Indic)));
5343
5344 Rewrite (N, New_Indic);
5345 Analyze (N);
5346 end if;
5347
5348 else
5349 if Nkind (Indic) /= N_Subtype_Indication then
5350 Make_Implicit_Base;
5351
5352 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5353 Set_Etype (Derived_Type, Implicit_Base);
5354 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5355
5356 else
5357 Error_Msg_N ("illegal constraint on constrained type", Indic);
5358 end if;
5359 end if;
5360
5361 -- If parent type is not a derived type itself, and is declared in
5362 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5363 -- the new type's concatenation operator since Derive_Subprograms
5364 -- will not inherit the parent's operator. If the parent type is
5365 -- unconstrained, the operator is of the unconstrained base type.
5366
5367 if Number_Dimensions (Parent_Type) = 1
5368 and then not Is_Limited_Type (Parent_Type)
5369 and then not Is_Derived_Type (Parent_Type)
5370 and then not Is_Package_Or_Generic_Package
5371 (Scope (Base_Type (Parent_Type)))
5372 then
5373 if not Is_Constrained (Parent_Type)
5374 and then Is_Constrained (Derived_Type)
5375 then
5376 New_Concatenation_Op (Implicit_Base);
5377 else
5378 New_Concatenation_Op (Derived_Type);
5379 end if;
5380 end if;
5381 end Build_Derived_Array_Type;
5382
5383 -----------------------------------
5384 -- Build_Derived_Concurrent_Type --
5385 -----------------------------------
5386
5387 procedure Build_Derived_Concurrent_Type
5388 (N : Node_Id;
5389 Parent_Type : Entity_Id;
5390 Derived_Type : Entity_Id)
5391 is
5392 Loc : constant Source_Ptr := Sloc (N);
5393
5394 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5395 Corr_Decl : Node_Id;
5396 Corr_Decl_Needed : Boolean;
5397 -- If the derived type has fewer discriminants than its parent, the
5398 -- corresponding record is also a derived type, in order to account for
5399 -- the bound discriminants. We create a full type declaration for it in
5400 -- this case.
5401
5402 Constraint_Present : constant Boolean :=
5403 Nkind (Subtype_Indication (Type_Definition (N))) =
5404 N_Subtype_Indication;
5405
5406 D_Constraint : Node_Id;
5407 New_Constraint : Elist_Id;
5408 Old_Disc : Entity_Id;
5409 New_Disc : Entity_Id;
5410 New_N : Node_Id;
5411
5412 begin
5413 Set_Stored_Constraint (Derived_Type, No_Elist);
5414 Corr_Decl_Needed := False;
5415 Old_Disc := Empty;
5416
5417 if Present (Discriminant_Specifications (N))
5418 and then Constraint_Present
5419 then
5420 Old_Disc := First_Discriminant (Parent_Type);
5421 New_Disc := First (Discriminant_Specifications (N));
5422 while Present (New_Disc) and then Present (Old_Disc) loop
5423 Next_Discriminant (Old_Disc);
5424 Next (New_Disc);
5425 end loop;
5426 end if;
5427
5428 if Present (Old_Disc) and then Expander_Active then
5429
5430 -- The new type has fewer discriminants, so we need to create a new
5431 -- corresponding record, which is derived from the corresponding
5432 -- record of the parent, and has a stored constraint that captures
5433 -- the values of the discriminant constraints. The corresponding
5434 -- record is needed only if expander is active and code generation is
5435 -- enabled.
5436
5437 -- The type declaration for the derived corresponding record has the
5438 -- same discriminant part and constraints as the current declaration.
5439 -- Copy the unanalyzed tree to build declaration.
5440
5441 Corr_Decl_Needed := True;
5442 New_N := Copy_Separate_Tree (N);
5443
5444 Corr_Decl :=
5445 Make_Full_Type_Declaration (Loc,
5446 Defining_Identifier => Corr_Record,
5447 Discriminant_Specifications =>
5448 Discriminant_Specifications (New_N),
5449 Type_Definition =>
5450 Make_Derived_Type_Definition (Loc,
5451 Subtype_Indication =>
5452 Make_Subtype_Indication (Loc,
5453 Subtype_Mark =>
5454 New_Occurrence_Of
5455 (Corresponding_Record_Type (Parent_Type), Loc),
5456 Constraint =>
5457 Constraint
5458 (Subtype_Indication (Type_Definition (New_N))))));
5459 end if;
5460
5461 -- Copy Storage_Size and Relative_Deadline variables if task case
5462
5463 if Is_Task_Type (Parent_Type) then
5464 Set_Storage_Size_Variable (Derived_Type,
5465 Storage_Size_Variable (Parent_Type));
5466 Set_Relative_Deadline_Variable (Derived_Type,
5467 Relative_Deadline_Variable (Parent_Type));
5468 end if;
5469
5470 if Present (Discriminant_Specifications (N)) then
5471 Push_Scope (Derived_Type);
5472 Check_Or_Process_Discriminants (N, Derived_Type);
5473
5474 if Constraint_Present then
5475 New_Constraint :=
5476 Expand_To_Stored_Constraint
5477 (Parent_Type,
5478 Build_Discriminant_Constraints
5479 (Parent_Type,
5480 Subtype_Indication (Type_Definition (N)), True));
5481 end if;
5482
5483 End_Scope;
5484
5485 elsif Constraint_Present then
5486
5487 -- Build constrained subtype, copying the constraint, and derive
5488 -- from it to create a derived constrained type.
5489
5490 declare
5491 Loc : constant Source_Ptr := Sloc (N);
5492 Anon : constant Entity_Id :=
5493 Make_Defining_Identifier (Loc,
5494 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5495 Decl : Node_Id;
5496
5497 begin
5498 Decl :=
5499 Make_Subtype_Declaration (Loc,
5500 Defining_Identifier => Anon,
5501 Subtype_Indication =>
5502 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
5503 Insert_Before (N, Decl);
5504 Analyze (Decl);
5505
5506 Rewrite (Subtype_Indication (Type_Definition (N)),
5507 New_Occurrence_Of (Anon, Loc));
5508 Set_Analyzed (Derived_Type, False);
5509 Analyze (N);
5510 return;
5511 end;
5512 end if;
5513
5514 -- By default, operations and private data are inherited from parent.
5515 -- However, in the presence of bound discriminants, a new corresponding
5516 -- record will be created, see below.
5517
5518 Set_Has_Discriminants
5519 (Derived_Type, Has_Discriminants (Parent_Type));
5520 Set_Corresponding_Record_Type
5521 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5522
5523 -- Is_Constrained is set according the parent subtype, but is set to
5524 -- False if the derived type is declared with new discriminants.
5525
5526 Set_Is_Constrained
5527 (Derived_Type,
5528 (Is_Constrained (Parent_Type) or else Constraint_Present)
5529 and then not Present (Discriminant_Specifications (N)));
5530
5531 if Constraint_Present then
5532 if not Has_Discriminants (Parent_Type) then
5533 Error_Msg_N ("untagged parent must have discriminants", N);
5534
5535 elsif Present (Discriminant_Specifications (N)) then
5536
5537 -- Verify that new discriminants are used to constrain old ones
5538
5539 D_Constraint :=
5540 First
5541 (Constraints
5542 (Constraint (Subtype_Indication (Type_Definition (N)))));
5543
5544 Old_Disc := First_Discriminant (Parent_Type);
5545
5546 while Present (D_Constraint) loop
5547 if Nkind (D_Constraint) /= N_Discriminant_Association then
5548
5549 -- Positional constraint. If it is a reference to a new
5550 -- discriminant, it constrains the corresponding old one.
5551
5552 if Nkind (D_Constraint) = N_Identifier then
5553 New_Disc := First_Discriminant (Derived_Type);
5554 while Present (New_Disc) loop
5555 exit when Chars (New_Disc) = Chars (D_Constraint);
5556 Next_Discriminant (New_Disc);
5557 end loop;
5558
5559 if Present (New_Disc) then
5560 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5561 end if;
5562 end if;
5563
5564 Next_Discriminant (Old_Disc);
5565
5566 -- if this is a named constraint, search by name for the old
5567 -- discriminants constrained by the new one.
5568
5569 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5570
5571 -- Find new discriminant with that name
5572
5573 New_Disc := First_Discriminant (Derived_Type);
5574 while Present (New_Disc) loop
5575 exit when
5576 Chars (New_Disc) = Chars (Expression (D_Constraint));
5577 Next_Discriminant (New_Disc);
5578 end loop;
5579
5580 if Present (New_Disc) then
5581
5582 -- Verify that new discriminant renames some discriminant
5583 -- of the parent type, and associate the new discriminant
5584 -- with one or more old ones that it renames.
5585
5586 declare
5587 Selector : Node_Id;
5588
5589 begin
5590 Selector := First (Selector_Names (D_Constraint));
5591 while Present (Selector) loop
5592 Old_Disc := First_Discriminant (Parent_Type);
5593 while Present (Old_Disc) loop
5594 exit when Chars (Old_Disc) = Chars (Selector);
5595 Next_Discriminant (Old_Disc);
5596 end loop;
5597
5598 if Present (Old_Disc) then
5599 Set_Corresponding_Discriminant
5600 (New_Disc, Old_Disc);
5601 end if;
5602
5603 Next (Selector);
5604 end loop;
5605 end;
5606 end if;
5607 end if;
5608
5609 Next (D_Constraint);
5610 end loop;
5611
5612 New_Disc := First_Discriminant (Derived_Type);
5613 while Present (New_Disc) loop
5614 if No (Corresponding_Discriminant (New_Disc)) then
5615 Error_Msg_NE
5616 ("new discriminant& must constrain old one", N, New_Disc);
5617
5618 elsif not
5619 Subtypes_Statically_Compatible
5620 (Etype (New_Disc),
5621 Etype (Corresponding_Discriminant (New_Disc)))
5622 then
5623 Error_Msg_NE
5624 ("& not statically compatible with parent discriminant",
5625 N, New_Disc);
5626 end if;
5627
5628 Next_Discriminant (New_Disc);
5629 end loop;
5630 end if;
5631
5632 elsif Present (Discriminant_Specifications (N)) then
5633 Error_Msg_N
5634 ("missing discriminant constraint in untagged derivation", N);
5635 end if;
5636
5637 -- The entity chain of the derived type includes the new discriminants
5638 -- but shares operations with the parent.
5639
5640 if Present (Discriminant_Specifications (N)) then
5641 Old_Disc := First_Discriminant (Parent_Type);
5642 while Present (Old_Disc) loop
5643 if No (Next_Entity (Old_Disc))
5644 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5645 then
5646 Set_Next_Entity
5647 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5648 exit;
5649 end if;
5650
5651 Next_Discriminant (Old_Disc);
5652 end loop;
5653
5654 else
5655 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5656 if Has_Discriminants (Parent_Type) then
5657 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5658 Set_Discriminant_Constraint (
5659 Derived_Type, Discriminant_Constraint (Parent_Type));
5660 end if;
5661 end if;
5662
5663 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5664
5665 Set_Has_Completion (Derived_Type);
5666
5667 if Corr_Decl_Needed then
5668 Set_Stored_Constraint (Derived_Type, New_Constraint);
5669 Insert_After (N, Corr_Decl);
5670 Analyze (Corr_Decl);
5671 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5672 end if;
5673 end Build_Derived_Concurrent_Type;
5674
5675 ------------------------------------
5676 -- Build_Derived_Enumeration_Type --
5677 ------------------------------------
5678
5679 procedure Build_Derived_Enumeration_Type
5680 (N : Node_Id;
5681 Parent_Type : Entity_Id;
5682 Derived_Type : Entity_Id)
5683 is
5684 Loc : constant Source_Ptr := Sloc (N);
5685 Def : constant Node_Id := Type_Definition (N);
5686 Indic : constant Node_Id := Subtype_Indication (Def);
5687 Implicit_Base : Entity_Id;
5688 Literal : Entity_Id;
5689 New_Lit : Entity_Id;
5690 Literals_List : List_Id;
5691 Type_Decl : Node_Id;
5692 Hi, Lo : Node_Id;
5693 Rang_Expr : Node_Id;
5694
5695 begin
5696 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5697 -- not have explicit literals lists we need to process types derived
5698 -- from them specially. This is handled by Derived_Standard_Character.
5699 -- If the parent type is a generic type, there are no literals either,
5700 -- and we construct the same skeletal representation as for the generic
5701 -- parent type.
5702
5703 if Is_Standard_Character_Type (Parent_Type) then
5704 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5705
5706 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5707 declare
5708 Lo : Node_Id;
5709 Hi : Node_Id;
5710
5711 begin
5712 if Nkind (Indic) /= N_Subtype_Indication then
5713 Lo :=
5714 Make_Attribute_Reference (Loc,
5715 Attribute_Name => Name_First,
5716 Prefix => New_Reference_To (Derived_Type, Loc));
5717 Set_Etype (Lo, Derived_Type);
5718
5719 Hi :=
5720 Make_Attribute_Reference (Loc,
5721 Attribute_Name => Name_Last,
5722 Prefix => New_Reference_To (Derived_Type, Loc));
5723 Set_Etype (Hi, Derived_Type);
5724
5725 Set_Scalar_Range (Derived_Type,
5726 Make_Range (Loc,
5727 Low_Bound => Lo,
5728 High_Bound => Hi));
5729 else
5730
5731 -- Analyze subtype indication and verify compatibility
5732 -- with parent type.
5733
5734 if Base_Type (Process_Subtype (Indic, N)) /=
5735 Base_Type (Parent_Type)
5736 then
5737 Error_Msg_N
5738 ("illegal constraint for formal discrete type", N);
5739 end if;
5740 end if;
5741 end;
5742
5743 else
5744 -- If a constraint is present, analyze the bounds to catch
5745 -- premature usage of the derived literals.
5746
5747 if Nkind (Indic) = N_Subtype_Indication
5748 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5749 then
5750 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5751 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5752 end if;
5753
5754 -- Introduce an implicit base type for the derived type even if there
5755 -- is no constraint attached to it, since this seems closer to the
5756 -- Ada semantics. Build a full type declaration tree for the derived
5757 -- type using the implicit base type as the defining identifier. The
5758 -- build a subtype declaration tree which applies the constraint (if
5759 -- any) have it replace the derived type declaration.
5760
5761 Literal := First_Literal (Parent_Type);
5762 Literals_List := New_List;
5763 while Present (Literal)
5764 and then Ekind (Literal) = E_Enumeration_Literal
5765 loop
5766 -- Literals of the derived type have the same representation as
5767 -- those of the parent type, but this representation can be
5768 -- overridden by an explicit representation clause. Indicate
5769 -- that there is no explicit representation given yet. These
5770 -- derived literals are implicit operations of the new type,
5771 -- and can be overridden by explicit ones.
5772
5773 if Nkind (Literal) = N_Defining_Character_Literal then
5774 New_Lit :=
5775 Make_Defining_Character_Literal (Loc, Chars (Literal));
5776 else
5777 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5778 end if;
5779
5780 Set_Ekind (New_Lit, E_Enumeration_Literal);
5781 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5782 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5783 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5784 Set_Alias (New_Lit, Literal);
5785 Set_Is_Known_Valid (New_Lit, True);
5786
5787 Append (New_Lit, Literals_List);
5788 Next_Literal (Literal);
5789 end loop;
5790
5791 Implicit_Base :=
5792 Make_Defining_Identifier (Sloc (Derived_Type),
5793 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5794
5795 -- Indicate the proper nature of the derived type. This must be done
5796 -- before analysis of the literals, to recognize cases when a literal
5797 -- may be hidden by a previous explicit function definition (cf.
5798 -- c83031a).
5799
5800 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5801 Set_Etype (Derived_Type, Implicit_Base);
5802
5803 Type_Decl :=
5804 Make_Full_Type_Declaration (Loc,
5805 Defining_Identifier => Implicit_Base,
5806 Discriminant_Specifications => No_List,
5807 Type_Definition =>
5808 Make_Enumeration_Type_Definition (Loc, Literals_List));
5809
5810 Mark_Rewrite_Insertion (Type_Decl);
5811 Insert_Before (N, Type_Decl);
5812 Analyze (Type_Decl);
5813
5814 -- After the implicit base is analyzed its Etype needs to be changed
5815 -- to reflect the fact that it is derived from the parent type which
5816 -- was ignored during analysis. We also set the size at this point.
5817
5818 Set_Etype (Implicit_Base, Parent_Type);
5819
5820 Set_Size_Info (Implicit_Base, Parent_Type);
5821 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5822 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5823
5824 -- Copy other flags from parent type
5825
5826 Set_Has_Non_Standard_Rep
5827 (Implicit_Base, Has_Non_Standard_Rep
5828 (Parent_Type));
5829 Set_Has_Pragma_Ordered
5830 (Implicit_Base, Has_Pragma_Ordered
5831 (Parent_Type));
5832 Set_Has_Delayed_Freeze (Implicit_Base);
5833
5834 -- Process the subtype indication including a validation check on the
5835 -- constraint, if any. If a constraint is given, its bounds must be
5836 -- implicitly converted to the new type.
5837
5838 if Nkind (Indic) = N_Subtype_Indication then
5839 declare
5840 R : constant Node_Id :=
5841 Range_Expression (Constraint (Indic));
5842
5843 begin
5844 if Nkind (R) = N_Range then
5845 Hi := Build_Scalar_Bound
5846 (High_Bound (R), Parent_Type, Implicit_Base);
5847 Lo := Build_Scalar_Bound
5848 (Low_Bound (R), Parent_Type, Implicit_Base);
5849
5850 else
5851 -- Constraint is a Range attribute. Replace with explicit
5852 -- mention of the bounds of the prefix, which must be a
5853 -- subtype.
5854
5855 Analyze (Prefix (R));
5856 Hi :=
5857 Convert_To (Implicit_Base,
5858 Make_Attribute_Reference (Loc,
5859 Attribute_Name => Name_Last,
5860 Prefix =>
5861 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5862
5863 Lo :=
5864 Convert_To (Implicit_Base,
5865 Make_Attribute_Reference (Loc,
5866 Attribute_Name => Name_First,
5867 Prefix =>
5868 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5869 end if;
5870 end;
5871
5872 else
5873 Hi :=
5874 Build_Scalar_Bound
5875 (Type_High_Bound (Parent_Type),
5876 Parent_Type, Implicit_Base);
5877 Lo :=
5878 Build_Scalar_Bound
5879 (Type_Low_Bound (Parent_Type),
5880 Parent_Type, Implicit_Base);
5881 end if;
5882
5883 Rang_Expr :=
5884 Make_Range (Loc,
5885 Low_Bound => Lo,
5886 High_Bound => Hi);
5887
5888 -- If we constructed a default range for the case where no range
5889 -- was given, then the expressions in the range must not freeze
5890 -- since they do not correspond to expressions in the source.
5891
5892 if Nkind (Indic) /= N_Subtype_Indication then
5893 Set_Must_Not_Freeze (Lo);
5894 Set_Must_Not_Freeze (Hi);
5895 Set_Must_Not_Freeze (Rang_Expr);
5896 end if;
5897
5898 Rewrite (N,
5899 Make_Subtype_Declaration (Loc,
5900 Defining_Identifier => Derived_Type,
5901 Subtype_Indication =>
5902 Make_Subtype_Indication (Loc,
5903 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5904 Constraint =>
5905 Make_Range_Constraint (Loc,
5906 Range_Expression => Rang_Expr))));
5907
5908 Analyze (N);
5909
5910 -- Apply a range check. Since this range expression doesn't have an
5911 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5912 -- this right???
5913
5914 if Nkind (Indic) = N_Subtype_Indication then
5915 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5916 Parent_Type,
5917 Source_Typ => Entity (Subtype_Mark (Indic)));
5918 end if;
5919 end if;
5920 end Build_Derived_Enumeration_Type;
5921
5922 --------------------------------
5923 -- Build_Derived_Numeric_Type --
5924 --------------------------------
5925
5926 procedure Build_Derived_Numeric_Type
5927 (N : Node_Id;
5928 Parent_Type : Entity_Id;
5929 Derived_Type : Entity_Id)
5930 is
5931 Loc : constant Source_Ptr := Sloc (N);
5932 Tdef : constant Node_Id := Type_Definition (N);
5933 Indic : constant Node_Id := Subtype_Indication (Tdef);
5934 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5935 No_Constraint : constant Boolean := Nkind (Indic) /=
5936 N_Subtype_Indication;
5937 Implicit_Base : Entity_Id;
5938
5939 Lo : Node_Id;
5940 Hi : Node_Id;
5941
5942 begin
5943 -- Process the subtype indication including a validation check on
5944 -- the constraint if any.
5945
5946 Discard_Node (Process_Subtype (Indic, N));
5947
5948 -- Introduce an implicit base type for the derived type even if there
5949 -- is no constraint attached to it, since this seems closer to the Ada
5950 -- semantics.
5951
5952 Implicit_Base :=
5953 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5954
5955 Set_Etype (Implicit_Base, Parent_Base);
5956 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5957 Set_Size_Info (Implicit_Base, Parent_Base);
5958 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5959 Set_Parent (Implicit_Base, Parent (Derived_Type));
5960 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5961
5962 -- Set RM Size for discrete type or decimal fixed-point type
5963 -- Ordinary fixed-point is excluded, why???
5964
5965 if Is_Discrete_Type (Parent_Base)
5966 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5967 then
5968 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5969 end if;
5970
5971 Set_Has_Delayed_Freeze (Implicit_Base);
5972
5973 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5974 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5975
5976 Set_Scalar_Range (Implicit_Base,
5977 Make_Range (Loc,
5978 Low_Bound => Lo,
5979 High_Bound => Hi));
5980
5981 if Has_Infinities (Parent_Base) then
5982 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5983 end if;
5984
5985 -- The Derived_Type, which is the entity of the declaration, is a
5986 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5987 -- absence of an explicit constraint.
5988
5989 Set_Etype (Derived_Type, Implicit_Base);
5990
5991 -- If we did not have a constraint, then the Ekind is set from the
5992 -- parent type (otherwise Process_Subtype has set the bounds)
5993
5994 if No_Constraint then
5995 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5996 end if;
5997
5998 -- If we did not have a range constraint, then set the range from the
5999 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6000
6001 if No_Constraint
6002 or else not Has_Range_Constraint (Indic)
6003 then
6004 Set_Scalar_Range (Derived_Type,
6005 Make_Range (Loc,
6006 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6007 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6008 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6009
6010 if Has_Infinities (Parent_Type) then
6011 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6012 end if;
6013
6014 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6015 end if;
6016
6017 Set_Is_Descendent_Of_Address (Derived_Type,
6018 Is_Descendent_Of_Address (Parent_Type));
6019 Set_Is_Descendent_Of_Address (Implicit_Base,
6020 Is_Descendent_Of_Address (Parent_Type));
6021
6022 -- Set remaining type-specific fields, depending on numeric type
6023
6024 if Is_Modular_Integer_Type (Parent_Type) then
6025 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6026
6027 Set_Non_Binary_Modulus
6028 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6029
6030 Set_Is_Known_Valid
6031 (Implicit_Base, Is_Known_Valid (Parent_Base));
6032
6033 elsif Is_Floating_Point_Type (Parent_Type) then
6034
6035 -- Digits of base type is always copied from the digits value of
6036 -- the parent base type, but the digits of the derived type will
6037 -- already have been set if there was a constraint present.
6038
6039 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6040 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6041
6042 if No_Constraint then
6043 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6044 end if;
6045
6046 elsif Is_Fixed_Point_Type (Parent_Type) then
6047
6048 -- Small of base type and derived type are always copied from the
6049 -- parent base type, since smalls never change. The delta of the
6050 -- base type is also copied from the parent base type. However the
6051 -- delta of the derived type will have been set already if a
6052 -- constraint was present.
6053
6054 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6055 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6056 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6057
6058 if No_Constraint then
6059 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6060 end if;
6061
6062 -- The scale and machine radix in the decimal case are always
6063 -- copied from the parent base type.
6064
6065 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6066 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6067 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6068
6069 Set_Machine_Radix_10
6070 (Derived_Type, Machine_Radix_10 (Parent_Base));
6071 Set_Machine_Radix_10
6072 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6073
6074 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6075
6076 if No_Constraint then
6077 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6078
6079 else
6080 -- the analysis of the subtype_indication sets the
6081 -- digits value of the derived type.
6082
6083 null;
6084 end if;
6085 end if;
6086 end if;
6087
6088 -- The type of the bounds is that of the parent type, and they
6089 -- must be converted to the derived type.
6090
6091 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6092
6093 -- The implicit_base should be frozen when the derived type is frozen,
6094 -- but note that it is used in the conversions of the bounds. For fixed
6095 -- types we delay the determination of the bounds until the proper
6096 -- freezing point. For other numeric types this is rejected by GCC, for
6097 -- reasons that are currently unclear (???), so we choose to freeze the
6098 -- implicit base now. In the case of integers and floating point types
6099 -- this is harmless because subsequent representation clauses cannot
6100 -- affect anything, but it is still baffling that we cannot use the
6101 -- same mechanism for all derived numeric types.
6102
6103 -- There is a further complication: actually some representation
6104 -- clauses can affect the implicit base type. For example, attribute
6105 -- definition clauses for stream-oriented attributes need to set the
6106 -- corresponding TSS entries on the base type, and this normally
6107 -- cannot be done after the base type is frozen, so the circuitry in
6108 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6109 -- and not use Set_TSS in this case.
6110
6111 -- There are also consequences for the case of delayed representation
6112 -- aspects for some cases. For example, a Size aspect is delayed and
6113 -- should not be evaluated to the freeze point. This early freezing
6114 -- means that the size attribute evaluation happens too early???
6115
6116 if Is_Fixed_Point_Type (Parent_Type) then
6117 Conditional_Delay (Implicit_Base, Parent_Type);
6118 else
6119 Freeze_Before (N, Implicit_Base);
6120 end if;
6121 end Build_Derived_Numeric_Type;
6122
6123 --------------------------------
6124 -- Build_Derived_Private_Type --
6125 --------------------------------
6126
6127 procedure Build_Derived_Private_Type
6128 (N : Node_Id;
6129 Parent_Type : Entity_Id;
6130 Derived_Type : Entity_Id;
6131 Is_Completion : Boolean;
6132 Derive_Subps : Boolean := True)
6133 is
6134 Loc : constant Source_Ptr := Sloc (N);
6135 Der_Base : Entity_Id;
6136 Discr : Entity_Id;
6137 Full_Decl : Node_Id := Empty;
6138 Full_Der : Entity_Id;
6139 Full_P : Entity_Id;
6140 Last_Discr : Entity_Id;
6141 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
6142 Swapped : Boolean := False;
6143
6144 procedure Copy_And_Build;
6145 -- Copy derived type declaration, replace parent with its full view,
6146 -- and analyze new declaration.
6147
6148 --------------------
6149 -- Copy_And_Build --
6150 --------------------
6151
6152 procedure Copy_And_Build is
6153 Full_N : Node_Id;
6154
6155 begin
6156 if Ekind (Parent_Type) in Record_Kind
6157 or else
6158 (Ekind (Parent_Type) in Enumeration_Kind
6159 and then not Is_Standard_Character_Type (Parent_Type)
6160 and then not Is_Generic_Type (Root_Type (Parent_Type)))
6161 then
6162 Full_N := New_Copy_Tree (N);
6163 Insert_After (N, Full_N);
6164 Build_Derived_Type (
6165 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
6166
6167 else
6168 Build_Derived_Type (
6169 N, Parent_Type, Full_Der, True, Derive_Subps => False);
6170 end if;
6171 end Copy_And_Build;
6172
6173 -- Start of processing for Build_Derived_Private_Type
6174
6175 begin
6176 if Is_Tagged_Type (Parent_Type) then
6177 Full_P := Full_View (Parent_Type);
6178
6179 -- A type extension of a type with unknown discriminants is an
6180 -- indefinite type that the back-end cannot handle directly.
6181 -- We treat it as a private type, and build a completion that is
6182 -- derived from the full view of the parent, and hopefully has
6183 -- known discriminants.
6184
6185 -- If the full view of the parent type has an underlying record view,
6186 -- use it to generate the underlying record view of this derived type
6187 -- (required for chains of derivations with unknown discriminants).
6188
6189 -- Minor optimization: we avoid the generation of useless underlying
6190 -- record view entities if the private type declaration has unknown
6191 -- discriminants but its corresponding full view has no
6192 -- discriminants.
6193
6194 if Has_Unknown_Discriminants (Parent_Type)
6195 and then Present (Full_P)
6196 and then (Has_Discriminants (Full_P)
6197 or else Present (Underlying_Record_View (Full_P)))
6198 and then not In_Open_Scopes (Par_Scope)
6199 and then Expander_Active
6200 then
6201 declare
6202 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6203 New_Ext : constant Node_Id :=
6204 Copy_Separate_Tree
6205 (Record_Extension_Part (Type_Definition (N)));
6206 Decl : Node_Id;
6207
6208 begin
6209 Build_Derived_Record_Type
6210 (N, Parent_Type, Derived_Type, Derive_Subps);
6211
6212 -- Build anonymous completion, as a derivation from the full
6213 -- view of the parent. This is not a completion in the usual
6214 -- sense, because the current type is not private.
6215
6216 Decl :=
6217 Make_Full_Type_Declaration (Loc,
6218 Defining_Identifier => Full_Der,
6219 Type_Definition =>
6220 Make_Derived_Type_Definition (Loc,
6221 Subtype_Indication =>
6222 New_Copy_Tree
6223 (Subtype_Indication (Type_Definition (N))),
6224 Record_Extension_Part => New_Ext));
6225
6226 -- If the parent type has an underlying record view, use it
6227 -- here to build the new underlying record view.
6228
6229 if Present (Underlying_Record_View (Full_P)) then
6230 pragma Assert
6231 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6232 = N_Identifier);
6233 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6234 Underlying_Record_View (Full_P));
6235 end if;
6236
6237 Install_Private_Declarations (Par_Scope);
6238 Install_Visible_Declarations (Par_Scope);
6239 Insert_Before (N, Decl);
6240
6241 -- Mark entity as an underlying record view before analysis,
6242 -- to avoid generating the list of its primitive operations
6243 -- (which is not really required for this entity) and thus
6244 -- prevent spurious errors associated with missing overriding
6245 -- of abstract primitives (overridden only for Derived_Type).
6246
6247 Set_Ekind (Full_Der, E_Record_Type);
6248 Set_Is_Underlying_Record_View (Full_Der);
6249
6250 Analyze (Decl);
6251
6252 pragma Assert (Has_Discriminants (Full_Der)
6253 and then not Has_Unknown_Discriminants (Full_Der));
6254
6255 Uninstall_Declarations (Par_Scope);
6256
6257 -- Freeze the underlying record view, to prevent generation of
6258 -- useless dispatching information, which is simply shared with
6259 -- the real derived type.
6260
6261 Set_Is_Frozen (Full_Der);
6262
6263 -- Set up links between real entity and underlying record view
6264
6265 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6266 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6267 end;
6268
6269 -- If discriminants are known, build derived record
6270
6271 else
6272 Build_Derived_Record_Type
6273 (N, Parent_Type, Derived_Type, Derive_Subps);
6274 end if;
6275
6276 return;
6277
6278 elsif Has_Discriminants (Parent_Type) then
6279 if Present (Full_View (Parent_Type)) then
6280 if not Is_Completion then
6281
6282 -- Copy declaration for subsequent analysis, to provide a
6283 -- completion for what is a private declaration. Indicate that
6284 -- the full type is internally generated.
6285
6286 Full_Decl := New_Copy_Tree (N);
6287 Full_Der := New_Copy (Derived_Type);
6288 Set_Comes_From_Source (Full_Decl, False);
6289 Set_Comes_From_Source (Full_Der, False);
6290 Set_Parent (Full_Der, Full_Decl);
6291
6292 Insert_After (N, Full_Decl);
6293
6294 else
6295 -- If this is a completion, the full view being built is itself
6296 -- private. We build a subtype of the parent with the same
6297 -- constraints as this full view, to convey to the back end the
6298 -- constrained components and the size of this subtype. If the
6299 -- parent is constrained, its full view can serve as the
6300 -- underlying full view of the derived type.
6301
6302 if No (Discriminant_Specifications (N)) then
6303 if Nkind (Subtype_Indication (Type_Definition (N))) =
6304 N_Subtype_Indication
6305 then
6306 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6307
6308 elsif Is_Constrained (Full_View (Parent_Type)) then
6309 Set_Underlying_Full_View
6310 (Derived_Type, Full_View (Parent_Type));
6311 end if;
6312
6313 else
6314 -- If there are new discriminants, the parent subtype is
6315 -- constrained by them, but it is not clear how to build
6316 -- the Underlying_Full_View in this case???
6317
6318 null;
6319 end if;
6320 end if;
6321 end if;
6322
6323 -- Build partial view of derived type from partial view of parent
6324
6325 Build_Derived_Record_Type
6326 (N, Parent_Type, Derived_Type, Derive_Subps);
6327
6328 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6329 if not In_Open_Scopes (Par_Scope)
6330 or else not In_Same_Source_Unit (N, Parent_Type)
6331 then
6332 -- Swap partial and full views temporarily
6333
6334 Install_Private_Declarations (Par_Scope);
6335 Install_Visible_Declarations (Par_Scope);
6336 Swapped := True;
6337 end if;
6338
6339 -- Build full view of derived type from full view of parent which
6340 -- is now installed. Subprograms have been derived on the partial
6341 -- view, the completion does not derive them anew.
6342
6343 if not Is_Tagged_Type (Parent_Type) then
6344
6345 -- If the parent is itself derived from another private type,
6346 -- installing the private declarations has not affected its
6347 -- privacy status, so use its own full view explicitly.
6348
6349 if Is_Private_Type (Parent_Type) then
6350 Build_Derived_Record_Type
6351 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6352 else
6353 Build_Derived_Record_Type
6354 (Full_Decl, Parent_Type, Full_Der, False);
6355 end if;
6356
6357 else
6358 -- If full view of parent is tagged, the completion inherits
6359 -- the proper primitive operations.
6360
6361 Set_Defining_Identifier (Full_Decl, Full_Der);
6362 Build_Derived_Record_Type
6363 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6364 end if;
6365
6366 -- The full declaration has been introduced into the tree and
6367 -- processed in the step above. It should not be analyzed again
6368 -- (when encountered later in the current list of declarations)
6369 -- to prevent spurious name conflicts. The full entity remains
6370 -- invisible.
6371
6372 Set_Analyzed (Full_Decl);
6373
6374 if Swapped then
6375 Uninstall_Declarations (Par_Scope);
6376
6377 if In_Open_Scopes (Par_Scope) then
6378 Install_Visible_Declarations (Par_Scope);
6379 end if;
6380 end if;
6381
6382 Der_Base := Base_Type (Derived_Type);
6383 Set_Full_View (Derived_Type, Full_Der);
6384 Set_Full_View (Der_Base, Base_Type (Full_Der));
6385
6386 -- Copy the discriminant list from full view to the partial views
6387 -- (base type and its subtype). Gigi requires that the partial and
6388 -- full views have the same discriminants.
6389
6390 -- Note that since the partial view is pointing to discriminants
6391 -- in the full view, their scope will be that of the full view.
6392 -- This might cause some front end problems and need adjustment???
6393
6394 Discr := First_Discriminant (Base_Type (Full_Der));
6395 Set_First_Entity (Der_Base, Discr);
6396
6397 loop
6398 Last_Discr := Discr;
6399 Next_Discriminant (Discr);
6400 exit when No (Discr);
6401 end loop;
6402
6403 Set_Last_Entity (Der_Base, Last_Discr);
6404
6405 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6406 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6407 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6408
6409 else
6410 -- If this is a completion, the derived type stays private and
6411 -- there is no need to create a further full view, except in the
6412 -- unusual case when the derivation is nested within a child unit,
6413 -- see below.
6414
6415 null;
6416 end if;
6417
6418 elsif Present (Full_View (Parent_Type))
6419 and then Has_Discriminants (Full_View (Parent_Type))
6420 then
6421 if Has_Unknown_Discriminants (Parent_Type)
6422 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6423 N_Subtype_Indication
6424 then
6425 Error_Msg_N
6426 ("cannot constrain type with unknown discriminants",
6427 Subtype_Indication (Type_Definition (N)));
6428 return;
6429 end if;
6430
6431 -- If full view of parent is a record type, build full view as a
6432 -- derivation from the parent's full view. Partial view remains
6433 -- private. For code generation and linking, the full view must have
6434 -- the same public status as the partial one. This full view is only
6435 -- needed if the parent type is in an enclosing scope, so that the
6436 -- full view may actually become visible, e.g. in a child unit. This
6437 -- is both more efficient, and avoids order of freezing problems with
6438 -- the added entities.
6439
6440 if not Is_Private_Type (Full_View (Parent_Type))
6441 and then (In_Open_Scopes (Scope (Parent_Type)))
6442 then
6443 Full_Der :=
6444 Make_Defining_Identifier (Sloc (Derived_Type),
6445 Chars => Chars (Derived_Type));
6446
6447 Set_Is_Itype (Full_Der);
6448 Set_Has_Private_Declaration (Full_Der);
6449 Set_Has_Private_Declaration (Derived_Type);
6450 Set_Associated_Node_For_Itype (Full_Der, N);
6451 Set_Parent (Full_Der, Parent (Derived_Type));
6452 Set_Full_View (Derived_Type, Full_Der);
6453 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6454 Full_P := Full_View (Parent_Type);
6455 Exchange_Declarations (Parent_Type);
6456 Copy_And_Build;
6457 Exchange_Declarations (Full_P);
6458
6459 else
6460 Build_Derived_Record_Type
6461 (N, Full_View (Parent_Type), Derived_Type,
6462 Derive_Subps => False);
6463
6464 -- Except in the context of the full view of the parent, there
6465 -- are no non-extension aggregates for the derived type.
6466
6467 Set_Has_Private_Ancestor (Derived_Type);
6468 end if;
6469
6470 -- In any case, the primitive operations are inherited from the
6471 -- parent type, not from the internal full view.
6472
6473 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6474
6475 if Derive_Subps then
6476 Derive_Subprograms (Parent_Type, Derived_Type);
6477 end if;
6478
6479 else
6480 -- Untagged type, No discriminants on either view
6481
6482 if Nkind (Subtype_Indication (Type_Definition (N))) =
6483 N_Subtype_Indication
6484 then
6485 Error_Msg_N
6486 ("illegal constraint on type without discriminants", N);
6487 end if;
6488
6489 if Present (Discriminant_Specifications (N))
6490 and then Present (Full_View (Parent_Type))
6491 and then not Is_Tagged_Type (Full_View (Parent_Type))
6492 then
6493 Error_Msg_N ("cannot add discriminants to untagged type", N);
6494 end if;
6495
6496 Set_Stored_Constraint (Derived_Type, No_Elist);
6497 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6498 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6499 Set_Has_Controlled_Component
6500 (Derived_Type, Has_Controlled_Component
6501 (Parent_Type));
6502
6503 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6504
6505 if not Is_Controlled (Parent_Type) then
6506 Set_Finalize_Storage_Only
6507 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6508 end if;
6509
6510 -- Construct the implicit full view by deriving from full view of the
6511 -- parent type. In order to get proper visibility, we install the
6512 -- parent scope and its declarations.
6513
6514 -- ??? If the parent is untagged private and its completion is
6515 -- tagged, this mechanism will not work because we cannot derive from
6516 -- the tagged full view unless we have an extension.
6517
6518 if Present (Full_View (Parent_Type))
6519 and then not Is_Tagged_Type (Full_View (Parent_Type))
6520 and then not Is_Completion
6521 then
6522 Full_Der :=
6523 Make_Defining_Identifier
6524 (Sloc (Derived_Type), Chars (Derived_Type));
6525 Set_Is_Itype (Full_Der);
6526 Set_Has_Private_Declaration (Full_Der);
6527 Set_Has_Private_Declaration (Derived_Type);
6528 Set_Associated_Node_For_Itype (Full_Der, N);
6529 Set_Parent (Full_Der, Parent (Derived_Type));
6530 Set_Full_View (Derived_Type, Full_Der);
6531
6532 if not In_Open_Scopes (Par_Scope) then
6533 Install_Private_Declarations (Par_Scope);
6534 Install_Visible_Declarations (Par_Scope);
6535 Copy_And_Build;
6536 Uninstall_Declarations (Par_Scope);
6537
6538 -- If parent scope is open and in another unit, and parent has a
6539 -- completion, then the derivation is taking place in the visible
6540 -- part of a child unit. In that case retrieve the full view of
6541 -- the parent momentarily.
6542
6543 elsif not In_Same_Source_Unit (N, Parent_Type) then
6544 Full_P := Full_View (Parent_Type);
6545 Exchange_Declarations (Parent_Type);
6546 Copy_And_Build;
6547 Exchange_Declarations (Full_P);
6548
6549 -- Otherwise it is a local derivation
6550
6551 else
6552 Copy_And_Build;
6553 end if;
6554
6555 Set_Scope (Full_Der, Current_Scope);
6556 Set_Is_First_Subtype (Full_Der,
6557 Is_First_Subtype (Derived_Type));
6558 Set_Has_Size_Clause (Full_Der, False);
6559 Set_Has_Alignment_Clause (Full_Der, False);
6560 Set_Next_Entity (Full_Der, Empty);
6561 Set_Has_Delayed_Freeze (Full_Der);
6562 Set_Is_Frozen (Full_Der, False);
6563 Set_Freeze_Node (Full_Der, Empty);
6564 Set_Depends_On_Private (Full_Der,
6565 Has_Private_Component (Full_Der));
6566 Set_Public_Status (Full_Der);
6567 end if;
6568 end if;
6569
6570 Set_Has_Unknown_Discriminants (Derived_Type,
6571 Has_Unknown_Discriminants (Parent_Type));
6572
6573 if Is_Private_Type (Derived_Type) then
6574 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6575 end if;
6576
6577 if Is_Private_Type (Parent_Type)
6578 and then Base_Type (Parent_Type) = Parent_Type
6579 and then In_Open_Scopes (Scope (Parent_Type))
6580 then
6581 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6582
6583 -- Check for unusual case where a type completed by a private
6584 -- derivation occurs within a package nested in a child unit, and
6585 -- the parent is declared in an ancestor.
6586
6587 if Is_Child_Unit (Scope (Current_Scope))
6588 and then Is_Completion
6589 and then In_Private_Part (Current_Scope)
6590 and then Scope (Parent_Type) /= Current_Scope
6591
6592 -- Note that if the parent has a completion in the private part,
6593 -- (which is itself a derivation from some other private type)
6594 -- it is that completion that is visible, there is no full view
6595 -- available, and no special processing is needed.
6596
6597 and then Present (Full_View (Parent_Type))
6598 then
6599 -- In this case, the full view of the parent type will become
6600 -- visible in the body of the enclosing child, and only then will
6601 -- the current type be possibly non-private. We build an
6602 -- underlying full view that will be installed when the enclosing
6603 -- child body is compiled.
6604
6605 Full_Der :=
6606 Make_Defining_Identifier
6607 (Sloc (Derived_Type), Chars (Derived_Type));
6608 Set_Is_Itype (Full_Der);
6609 Build_Itype_Reference (Full_Der, N);
6610
6611 -- The full view will be used to swap entities on entry/exit to
6612 -- the body, and must appear in the entity list for the package.
6613
6614 Append_Entity (Full_Der, Scope (Derived_Type));
6615 Set_Has_Private_Declaration (Full_Der);
6616 Set_Has_Private_Declaration (Derived_Type);
6617 Set_Associated_Node_For_Itype (Full_Der, N);
6618 Set_Parent (Full_Der, Parent (Derived_Type));
6619 Full_P := Full_View (Parent_Type);
6620 Exchange_Declarations (Parent_Type);
6621 Copy_And_Build;
6622 Exchange_Declarations (Full_P);
6623 Set_Underlying_Full_View (Derived_Type, Full_Der);
6624 end if;
6625 end if;
6626 end Build_Derived_Private_Type;
6627
6628 -------------------------------
6629 -- Build_Derived_Record_Type --
6630 -------------------------------
6631
6632 -- 1. INTRODUCTION
6633
6634 -- Ideally we would like to use the same model of type derivation for
6635 -- tagged and untagged record types. Unfortunately this is not quite
6636 -- possible because the semantics of representation clauses is different
6637 -- for tagged and untagged records under inheritance. Consider the
6638 -- following:
6639
6640 -- type R (...) is [tagged] record ... end record;
6641 -- type T (...) is new R (...) [with ...];
6642
6643 -- The representation clauses for T can specify a completely different
6644 -- record layout from R's. Hence the same component can be placed in two
6645 -- very different positions in objects of type T and R. If R and T are
6646 -- tagged types, representation clauses for T can only specify the layout
6647 -- of non inherited components, thus components that are common in R and T
6648 -- have the same position in objects of type R and T.
6649
6650 -- This has two implications. The first is that the entire tree for R's
6651 -- declaration needs to be copied for T in the untagged case, so that T
6652 -- can be viewed as a record type of its own with its own representation
6653 -- clauses. The second implication is the way we handle discriminants.
6654 -- Specifically, in the untagged case we need a way to communicate to Gigi
6655 -- what are the real discriminants in the record, while for the semantics
6656 -- we need to consider those introduced by the user to rename the
6657 -- discriminants in the parent type. This is handled by introducing the
6658 -- notion of stored discriminants. See below for more.
6659
6660 -- Fortunately the way regular components are inherited can be handled in
6661 -- the same way in tagged and untagged types.
6662
6663 -- To complicate things a bit more the private view of a private extension
6664 -- cannot be handled in the same way as the full view (for one thing the
6665 -- semantic rules are somewhat different). We will explain what differs
6666 -- below.
6667
6668 -- 2. DISCRIMINANTS UNDER INHERITANCE
6669
6670 -- The semantic rules governing the discriminants of derived types are
6671 -- quite subtle.
6672
6673 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6674 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6675
6676 -- If parent type has discriminants, then the discriminants that are
6677 -- declared in the derived type are [3.4 (11)]:
6678
6679 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6680 -- there is one;
6681
6682 -- o Otherwise, each discriminant of the parent type (implicitly declared
6683 -- in the same order with the same specifications). In this case, the
6684 -- discriminants are said to be "inherited", or if unknown in the parent
6685 -- are also unknown in the derived type.
6686
6687 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6688
6689 -- o The parent subtype shall be constrained;
6690
6691 -- o If the parent type is not a tagged type, then each discriminant of
6692 -- the derived type shall be used in the constraint defining a parent
6693 -- subtype. [Implementation note: This ensures that the new discriminant
6694 -- can share storage with an existing discriminant.]
6695
6696 -- For the derived type each discriminant of the parent type is either
6697 -- inherited, constrained to equal some new discriminant of the derived
6698 -- type, or constrained to the value of an expression.
6699
6700 -- When inherited or constrained to equal some new discriminant, the
6701 -- parent discriminant and the discriminant of the derived type are said
6702 -- to "correspond".
6703
6704 -- If a discriminant of the parent type is constrained to a specific value
6705 -- in the derived type definition, then the discriminant is said to be
6706 -- "specified" by that derived type definition.
6707
6708 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6709
6710 -- We have spoken about stored discriminants in point 1 (introduction)
6711 -- above. There are two sort of stored discriminants: implicit and
6712 -- explicit. As long as the derived type inherits the same discriminants as
6713 -- the root record type, stored discriminants are the same as regular
6714 -- discriminants, and are said to be implicit. However, if any discriminant
6715 -- in the root type was renamed in the derived type, then the derived
6716 -- type will contain explicit stored discriminants. Explicit stored
6717 -- discriminants are discriminants in addition to the semantically visible
6718 -- discriminants defined for the derived type. Stored discriminants are
6719 -- used by Gigi to figure out what are the physical discriminants in
6720 -- objects of the derived type (see precise definition in einfo.ads).
6721 -- As an example, consider the following:
6722
6723 -- type R (D1, D2, D3 : Int) is record ... end record;
6724 -- type T1 is new R;
6725 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6726 -- type T3 is new T2;
6727 -- type T4 (Y : Int) is new T3 (Y, 99);
6728
6729 -- The following table summarizes the discriminants and stored
6730 -- discriminants in R and T1 through T4.
6731
6732 -- Type Discrim Stored Discrim Comment
6733 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6734 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6735 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6736 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6737 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6738
6739 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6740 -- find the corresponding discriminant in the parent type, while
6741 -- Original_Record_Component (abbreviated ORC below), the actual physical
6742 -- component that is renamed. Finally the field Is_Completely_Hidden
6743 -- (abbreviated ICH below) is set for all explicit stored discriminants
6744 -- (see einfo.ads for more info). For the above example this gives:
6745
6746 -- Discrim CD ORC ICH
6747 -- ^^^^^^^ ^^ ^^^ ^^^
6748 -- D1 in R empty itself no
6749 -- D2 in R empty itself no
6750 -- D3 in R empty itself no
6751
6752 -- D1 in T1 D1 in R itself no
6753 -- D2 in T1 D2 in R itself no
6754 -- D3 in T1 D3 in R itself no
6755
6756 -- X1 in T2 D3 in T1 D3 in T2 no
6757 -- X2 in T2 D1 in T1 D1 in T2 no
6758 -- D1 in T2 empty itself yes
6759 -- D2 in T2 empty itself yes
6760 -- D3 in T2 empty itself yes
6761
6762 -- X1 in T3 X1 in T2 D3 in T3 no
6763 -- X2 in T3 X2 in T2 D1 in T3 no
6764 -- D1 in T3 empty itself yes
6765 -- D2 in T3 empty itself yes
6766 -- D3 in T3 empty itself yes
6767
6768 -- Y in T4 X1 in T3 D3 in T3 no
6769 -- D1 in T3 empty itself yes
6770 -- D2 in T3 empty itself yes
6771 -- D3 in T3 empty itself yes
6772
6773 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6774
6775 -- Type derivation for tagged types is fairly straightforward. If no
6776 -- discriminants are specified by the derived type, these are inherited
6777 -- from the parent. No explicit stored discriminants are ever necessary.
6778 -- The only manipulation that is done to the tree is that of adding a
6779 -- _parent field with parent type and constrained to the same constraint
6780 -- specified for the parent in the derived type definition. For instance:
6781
6782 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6783 -- type T1 is new R with null record;
6784 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6785
6786 -- are changed into:
6787
6788 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6789 -- _parent : R (D1, D2, D3);
6790 -- end record;
6791
6792 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6793 -- _parent : T1 (X2, 88, X1);
6794 -- end record;
6795
6796 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6797 -- ORC and ICH fields are:
6798
6799 -- Discrim CD ORC ICH
6800 -- ^^^^^^^ ^^ ^^^ ^^^
6801 -- D1 in R empty itself no
6802 -- D2 in R empty itself no
6803 -- D3 in R empty itself no
6804
6805 -- D1 in T1 D1 in R D1 in R no
6806 -- D2 in T1 D2 in R D2 in R no
6807 -- D3 in T1 D3 in R D3 in R no
6808
6809 -- X1 in T2 D3 in T1 D3 in R no
6810 -- X2 in T2 D1 in T1 D1 in R no
6811
6812 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6813 --
6814 -- Regardless of whether we dealing with a tagged or untagged type
6815 -- we will transform all derived type declarations of the form
6816 --
6817 -- type T is new R (...) [with ...];
6818 -- or
6819 -- subtype S is R (...);
6820 -- type T is new S [with ...];
6821 -- into
6822 -- type BT is new R [with ...];
6823 -- subtype T is BT (...);
6824 --
6825 -- That is, the base derived type is constrained only if it has no
6826 -- discriminants. The reason for doing this is that GNAT's semantic model
6827 -- assumes that a base type with discriminants is unconstrained.
6828 --
6829 -- Note that, strictly speaking, the above transformation is not always
6830 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6831 --
6832 -- procedure B34011A is
6833 -- type REC (D : integer := 0) is record
6834 -- I : Integer;
6835 -- end record;
6836
6837 -- package P is
6838 -- type T6 is new Rec;
6839 -- function F return T6;
6840 -- end P;
6841
6842 -- use P;
6843 -- package Q6 is
6844 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6845 -- end Q6;
6846 --
6847 -- The definition of Q6.U is illegal. However transforming Q6.U into
6848
6849 -- type BaseU is new T6;
6850 -- subtype U is BaseU (Q6.F.I)
6851
6852 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6853 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6854 -- the transformation described above.
6855
6856 -- There is another instance where the above transformation is incorrect.
6857 -- Consider:
6858
6859 -- package Pack is
6860 -- type Base (D : Integer) is tagged null record;
6861 -- procedure P (X : Base);
6862
6863 -- type Der is new Base (2) with null record;
6864 -- procedure P (X : Der);
6865 -- end Pack;
6866
6867 -- Then the above transformation turns this into
6868
6869 -- type Der_Base is new Base with null record;
6870 -- -- procedure P (X : Base) is implicitly inherited here
6871 -- -- as procedure P (X : Der_Base).
6872
6873 -- subtype Der is Der_Base (2);
6874 -- procedure P (X : Der);
6875 -- -- The overriding of P (X : Der_Base) is illegal since we
6876 -- -- have a parameter conformance problem.
6877
6878 -- To get around this problem, after having semantically processed Der_Base
6879 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6880 -- Discriminant_Constraint from Der so that when parameter conformance is
6881 -- checked when P is overridden, no semantic errors are flagged.
6882
6883 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6884
6885 -- Regardless of whether we are dealing with a tagged or untagged type
6886 -- we will transform all derived type declarations of the form
6887
6888 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6889 -- type T is new R [with ...];
6890 -- into
6891 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6892
6893 -- The reason for such transformation is that it allows us to implement a
6894 -- very clean form of component inheritance as explained below.
6895
6896 -- Note that this transformation is not achieved by direct tree rewriting
6897 -- and manipulation, but rather by redoing the semantic actions that the
6898 -- above transformation will entail. This is done directly in routine
6899 -- Inherit_Components.
6900
6901 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6902
6903 -- In both tagged and untagged derived types, regular non discriminant
6904 -- components are inherited in the derived type from the parent type. In
6905 -- the absence of discriminants component, inheritance is straightforward
6906 -- as components can simply be copied from the parent.
6907
6908 -- If the parent has discriminants, inheriting components constrained with
6909 -- these discriminants requires caution. Consider the following example:
6910
6911 -- type R (D1, D2 : Positive) is [tagged] record
6912 -- S : String (D1 .. D2);
6913 -- end record;
6914
6915 -- type T1 is new R [with null record];
6916 -- type T2 (X : positive) is new R (1, X) [with null record];
6917
6918 -- As explained in 6. above, T1 is rewritten as
6919 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6920 -- which makes the treatment for T1 and T2 identical.
6921
6922 -- What we want when inheriting S, is that references to D1 and D2 in R are
6923 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6924 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6925 -- with either discriminant references in the derived type or expressions.
6926 -- This replacement is achieved as follows: before inheriting R's
6927 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6928 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6929 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6930 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6931 -- by String (1 .. X).
6932
6933 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6934
6935 -- We explain here the rules governing private type extensions relevant to
6936 -- type derivation. These rules are explained on the following example:
6937
6938 -- type D [(...)] is new A [(...)] with private; <-- partial view
6939 -- type D [(...)] is new P [(...)] with null record; <-- full view
6940
6941 -- Type A is called the ancestor subtype of the private extension.
6942 -- Type P is the parent type of the full view of the private extension. It
6943 -- must be A or a type derived from A.
6944
6945 -- The rules concerning the discriminants of private type extensions are
6946 -- [7.3(10-13)]:
6947
6948 -- o If a private extension inherits known discriminants from the ancestor
6949 -- subtype, then the full view shall also inherit its discriminants from
6950 -- the ancestor subtype and the parent subtype of the full view shall be
6951 -- constrained if and only if the ancestor subtype is constrained.
6952
6953 -- o If a partial view has unknown discriminants, then the full view may
6954 -- define a definite or an indefinite subtype, with or without
6955 -- discriminants.
6956
6957 -- o If a partial view has neither known nor unknown discriminants, then
6958 -- the full view shall define a definite subtype.
6959
6960 -- o If the ancestor subtype of a private extension has constrained
6961 -- discriminants, then the parent subtype of the full view shall impose a
6962 -- statically matching constraint on those discriminants.
6963
6964 -- This means that only the following forms of private extensions are
6965 -- allowed:
6966
6967 -- type D is new A with private; <-- partial view
6968 -- type D is new P with null record; <-- full view
6969
6970 -- If A has no discriminants than P has no discriminants, otherwise P must
6971 -- inherit A's discriminants.
6972
6973 -- type D is new A (...) with private; <-- partial view
6974 -- type D is new P (:::) with null record; <-- full view
6975
6976 -- P must inherit A's discriminants and (...) and (:::) must statically
6977 -- match.
6978
6979 -- subtype A is R (...);
6980 -- type D is new A with private; <-- partial view
6981 -- type D is new P with null record; <-- full view
6982
6983 -- P must have inherited R's discriminants and must be derived from A or
6984 -- any of its subtypes.
6985
6986 -- type D (..) is new A with private; <-- partial view
6987 -- type D (..) is new P [(:::)] with null record; <-- full view
6988
6989 -- No specific constraints on P's discriminants or constraint (:::).
6990 -- Note that A can be unconstrained, but the parent subtype P must either
6991 -- be constrained or (:::) must be present.
6992
6993 -- type D (..) is new A [(...)] with private; <-- partial view
6994 -- type D (..) is new P [(:::)] with null record; <-- full view
6995
6996 -- P's constraints on A's discriminants must statically match those
6997 -- imposed by (...).
6998
6999 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7000
7001 -- The full view of a private extension is handled exactly as described
7002 -- above. The model chose for the private view of a private extension is
7003 -- the same for what concerns discriminants (i.e. they receive the same
7004 -- treatment as in the tagged case). However, the private view of the
7005 -- private extension always inherits the components of the parent base,
7006 -- without replacing any discriminant reference. Strictly speaking this is
7007 -- incorrect. However, Gigi never uses this view to generate code so this
7008 -- is a purely semantic issue. In theory, a set of transformations similar
7009 -- to those given in 5. and 6. above could be applied to private views of
7010 -- private extensions to have the same model of component inheritance as
7011 -- for non private extensions. However, this is not done because it would
7012 -- further complicate private type processing. Semantically speaking, this
7013 -- leaves us in an uncomfortable situation. As an example consider:
7014
7015 -- package Pack is
7016 -- type R (D : integer) is tagged record
7017 -- S : String (1 .. D);
7018 -- end record;
7019 -- procedure P (X : R);
7020 -- type T is new R (1) with private;
7021 -- private
7022 -- type T is new R (1) with null record;
7023 -- end;
7024
7025 -- This is transformed into:
7026
7027 -- package Pack is
7028 -- type R (D : integer) is tagged record
7029 -- S : String (1 .. D);
7030 -- end record;
7031 -- procedure P (X : R);
7032 -- type T is new R (1) with private;
7033 -- private
7034 -- type BaseT is new R with null record;
7035 -- subtype T is BaseT (1);
7036 -- end;
7037
7038 -- (strictly speaking the above is incorrect Ada)
7039
7040 -- From the semantic standpoint the private view of private extension T
7041 -- should be flagged as constrained since one can clearly have
7042 --
7043 -- Obj : T;
7044 --
7045 -- in a unit withing Pack. However, when deriving subprograms for the
7046 -- private view of private extension T, T must be seen as unconstrained
7047 -- since T has discriminants (this is a constraint of the current
7048 -- subprogram derivation model). Thus, when processing the private view of
7049 -- a private extension such as T, we first mark T as unconstrained, we
7050 -- process it, we perform program derivation and just before returning from
7051 -- Build_Derived_Record_Type we mark T as constrained.
7052
7053 -- ??? Are there are other uncomfortable cases that we will have to
7054 -- deal with.
7055
7056 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7057
7058 -- Types that are derived from a visible record type and have a private
7059 -- extension present other peculiarities. They behave mostly like private
7060 -- types, but if they have primitive operations defined, these will not
7061 -- have the proper signatures for further inheritance, because other
7062 -- primitive operations will use the implicit base that we define for
7063 -- private derivations below. This affect subprogram inheritance (see
7064 -- Derive_Subprograms for details). We also derive the implicit base from
7065 -- the base type of the full view, so that the implicit base is a record
7066 -- type and not another private type, This avoids infinite loops.
7067
7068 procedure Build_Derived_Record_Type
7069 (N : Node_Id;
7070 Parent_Type : Entity_Id;
7071 Derived_Type : Entity_Id;
7072 Derive_Subps : Boolean := True)
7073 is
7074 Discriminant_Specs : constant Boolean :=
7075 Present (Discriminant_Specifications (N));
7076 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7077 Loc : constant Source_Ptr := Sloc (N);
7078 Private_Extension : constant Boolean :=
7079 Nkind (N) = N_Private_Extension_Declaration;
7080 Assoc_List : Elist_Id;
7081 Constraint_Present : Boolean;
7082 Constrs : Elist_Id;
7083 Discrim : Entity_Id;
7084 Indic : Node_Id;
7085 Inherit_Discrims : Boolean := False;
7086 Last_Discrim : Entity_Id;
7087 New_Base : Entity_Id;
7088 New_Decl : Node_Id;
7089 New_Discrs : Elist_Id;
7090 New_Indic : Node_Id;
7091 Parent_Base : Entity_Id;
7092 Save_Etype : Entity_Id;
7093 Save_Discr_Constr : Elist_Id;
7094 Save_Next_Entity : Entity_Id;
7095 Type_Def : Node_Id;
7096
7097 Discs : Elist_Id := New_Elmt_List;
7098 -- An empty Discs list means that there were no constraints in the
7099 -- subtype indication or that there was an error processing it.
7100
7101 begin
7102 if Ekind (Parent_Type) = E_Record_Type_With_Private
7103 and then Present (Full_View (Parent_Type))
7104 and then Has_Discriminants (Parent_Type)
7105 then
7106 Parent_Base := Base_Type (Full_View (Parent_Type));
7107 else
7108 Parent_Base := Base_Type (Parent_Type);
7109 end if;
7110
7111 -- AI05-0115 : if this is a derivation from a private type in some
7112 -- other scope that may lead to invisible components for the derived
7113 -- type, mark it accordingly.
7114
7115 if Is_Private_Type (Parent_Type) then
7116 if Scope (Parent_Type) = Scope (Derived_Type) then
7117 null;
7118
7119 elsif In_Open_Scopes (Scope (Parent_Type))
7120 and then In_Private_Part (Scope (Parent_Type))
7121 then
7122 null;
7123
7124 else
7125 Set_Has_Private_Ancestor (Derived_Type);
7126 end if;
7127
7128 else
7129 Set_Has_Private_Ancestor
7130 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7131 end if;
7132
7133 -- Before we start the previously documented transformations, here is
7134 -- little fix for size and alignment of tagged types. Normally when we
7135 -- derive type D from type P, we copy the size and alignment of P as the
7136 -- default for D, and in the absence of explicit representation clauses
7137 -- for D, the size and alignment are indeed the same as the parent.
7138
7139 -- But this is wrong for tagged types, since fields may be added, and
7140 -- the default size may need to be larger, and the default alignment may
7141 -- need to be larger.
7142
7143 -- We therefore reset the size and alignment fields in the tagged case.
7144 -- Note that the size and alignment will in any case be at least as
7145 -- large as the parent type (since the derived type has a copy of the
7146 -- parent type in the _parent field)
7147
7148 -- The type is also marked as being tagged here, which is needed when
7149 -- processing components with a self-referential anonymous access type
7150 -- in the call to Check_Anonymous_Access_Components below. Note that
7151 -- this flag is also set later on for completeness.
7152
7153 if Is_Tagged then
7154 Set_Is_Tagged_Type (Derived_Type);
7155 Init_Size_Align (Derived_Type);
7156 end if;
7157
7158 -- STEP 0a: figure out what kind of derived type declaration we have
7159
7160 if Private_Extension then
7161 Type_Def := N;
7162 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7163
7164 else
7165 Type_Def := Type_Definition (N);
7166
7167 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7168 -- Parent_Base can be a private type or private extension. However,
7169 -- for tagged types with an extension the newly added fields are
7170 -- visible and hence the Derived_Type is always an E_Record_Type.
7171 -- (except that the parent may have its own private fields).
7172 -- For untagged types we preserve the Ekind of the Parent_Base.
7173
7174 if Present (Record_Extension_Part (Type_Def)) then
7175 Set_Ekind (Derived_Type, E_Record_Type);
7176
7177 -- Create internal access types for components with anonymous
7178 -- access types.
7179
7180 if Ada_Version >= Ada_2005 then
7181 Check_Anonymous_Access_Components
7182 (N, Derived_Type, Derived_Type,
7183 Component_List (Record_Extension_Part (Type_Def)));
7184 end if;
7185
7186 else
7187 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7188 end if;
7189 end if;
7190
7191 -- Indic can either be an N_Identifier if the subtype indication
7192 -- contains no constraint or an N_Subtype_Indication if the subtype
7193 -- indication has a constraint.
7194
7195 Indic := Subtype_Indication (Type_Def);
7196 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7197
7198 -- Check that the type has visible discriminants. The type may be
7199 -- a private type with unknown discriminants whose full view has
7200 -- discriminants which are invisible.
7201
7202 if Constraint_Present then
7203 if not Has_Discriminants (Parent_Base)
7204 or else
7205 (Has_Unknown_Discriminants (Parent_Base)
7206 and then Is_Private_Type (Parent_Base))
7207 then
7208 Error_Msg_N
7209 ("invalid constraint: type has no discriminant",
7210 Constraint (Indic));
7211
7212 Constraint_Present := False;
7213 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7214
7215 elsif Is_Constrained (Parent_Type) then
7216 Error_Msg_N
7217 ("invalid constraint: parent type is already constrained",
7218 Constraint (Indic));
7219
7220 Constraint_Present := False;
7221 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7222 end if;
7223 end if;
7224
7225 -- STEP 0b: If needed, apply transformation given in point 5. above
7226
7227 if not Private_Extension
7228 and then Has_Discriminants (Parent_Type)
7229 and then not Discriminant_Specs
7230 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7231 then
7232 -- First, we must analyze the constraint (see comment in point 5.)
7233
7234 if Constraint_Present then
7235 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7236
7237 if Has_Discriminants (Derived_Type)
7238 and then Has_Private_Declaration (Derived_Type)
7239 and then Present (Discriminant_Constraint (Derived_Type))
7240 then
7241 -- Verify that constraints of the full view statically match
7242 -- those given in the partial view.
7243
7244 declare
7245 C1, C2 : Elmt_Id;
7246
7247 begin
7248 C1 := First_Elmt (New_Discrs);
7249 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7250 while Present (C1) and then Present (C2) loop
7251 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7252 or else
7253 (Is_OK_Static_Expression (Node (C1))
7254 and then
7255 Is_OK_Static_Expression (Node (C2))
7256 and then
7257 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7258 then
7259 null;
7260
7261 else
7262 Error_Msg_N (
7263 "constraint not conformant to previous declaration",
7264 Node (C1));
7265 end if;
7266
7267 Next_Elmt (C1);
7268 Next_Elmt (C2);
7269 end loop;
7270 end;
7271 end if;
7272 end if;
7273
7274 -- Insert and analyze the declaration for the unconstrained base type
7275
7276 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7277
7278 New_Decl :=
7279 Make_Full_Type_Declaration (Loc,
7280 Defining_Identifier => New_Base,
7281 Type_Definition =>
7282 Make_Derived_Type_Definition (Loc,
7283 Abstract_Present => Abstract_Present (Type_Def),
7284 Limited_Present => Limited_Present (Type_Def),
7285 Subtype_Indication =>
7286 New_Occurrence_Of (Parent_Base, Loc),
7287 Record_Extension_Part =>
7288 Relocate_Node (Record_Extension_Part (Type_Def)),
7289 Interface_List => Interface_List (Type_Def)));
7290
7291 Set_Parent (New_Decl, Parent (N));
7292 Mark_Rewrite_Insertion (New_Decl);
7293 Insert_Before (N, New_Decl);
7294
7295 -- In the extension case, make sure ancestor is frozen appropriately
7296 -- (see also non-discriminated case below).
7297
7298 if Present (Record_Extension_Part (Type_Def))
7299 or else Is_Interface (Parent_Base)
7300 then
7301 Freeze_Before (New_Decl, Parent_Type);
7302 end if;
7303
7304 -- Note that this call passes False for the Derive_Subps parameter
7305 -- because subprogram derivation is deferred until after creating
7306 -- the subtype (see below).
7307
7308 Build_Derived_Type
7309 (New_Decl, Parent_Base, New_Base,
7310 Is_Completion => True, Derive_Subps => False);
7311
7312 -- ??? This needs re-examination to determine whether the
7313 -- above call can simply be replaced by a call to Analyze.
7314
7315 Set_Analyzed (New_Decl);
7316
7317 -- Insert and analyze the declaration for the constrained subtype
7318
7319 if Constraint_Present then
7320 New_Indic :=
7321 Make_Subtype_Indication (Loc,
7322 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7323 Constraint => Relocate_Node (Constraint (Indic)));
7324
7325 else
7326 declare
7327 Constr_List : constant List_Id := New_List;
7328 C : Elmt_Id;
7329 Expr : Node_Id;
7330
7331 begin
7332 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7333 while Present (C) loop
7334 Expr := Node (C);
7335
7336 -- It is safe here to call New_Copy_Tree since
7337 -- Force_Evaluation was called on each constraint in
7338 -- Build_Discriminant_Constraints.
7339
7340 Append (New_Copy_Tree (Expr), To => Constr_List);
7341
7342 Next_Elmt (C);
7343 end loop;
7344
7345 New_Indic :=
7346 Make_Subtype_Indication (Loc,
7347 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7348 Constraint =>
7349 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7350 end;
7351 end if;
7352
7353 Rewrite (N,
7354 Make_Subtype_Declaration (Loc,
7355 Defining_Identifier => Derived_Type,
7356 Subtype_Indication => New_Indic));
7357
7358 Analyze (N);
7359
7360 -- Derivation of subprograms must be delayed until the full subtype
7361 -- has been established, to ensure proper overriding of subprograms
7362 -- inherited by full types. If the derivations occurred as part of
7363 -- the call to Build_Derived_Type above, then the check for type
7364 -- conformance would fail because earlier primitive subprograms
7365 -- could still refer to the full type prior the change to the new
7366 -- subtype and hence would not match the new base type created here.
7367 -- Subprograms are not derived, however, when Derive_Subps is False
7368 -- (since otherwise there could be redundant derivations).
7369
7370 if Derive_Subps then
7371 Derive_Subprograms (Parent_Type, Derived_Type);
7372 end if;
7373
7374 -- For tagged types the Discriminant_Constraint of the new base itype
7375 -- is inherited from the first subtype so that no subtype conformance
7376 -- problem arise when the first subtype overrides primitive
7377 -- operations inherited by the implicit base type.
7378
7379 if Is_Tagged then
7380 Set_Discriminant_Constraint
7381 (New_Base, Discriminant_Constraint (Derived_Type));
7382 end if;
7383
7384 return;
7385 end if;
7386
7387 -- If we get here Derived_Type will have no discriminants or it will be
7388 -- a discriminated unconstrained base type.
7389
7390 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7391
7392 if Is_Tagged then
7393
7394 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7395 -- The declaration of a specific descendant of an interface type
7396 -- freezes the interface type (RM 13.14).
7397
7398 if not Private_Extension or else Is_Interface (Parent_Base) then
7399 Freeze_Before (N, Parent_Type);
7400 end if;
7401
7402 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7403 -- cannot be declared at a deeper level than its parent type is
7404 -- removed. The check on derivation within a generic body is also
7405 -- relaxed, but there's a restriction that a derived tagged type
7406 -- cannot be declared in a generic body if it's derived directly
7407 -- or indirectly from a formal type of that generic.
7408
7409 if Ada_Version >= Ada_2005 then
7410 if Present (Enclosing_Generic_Body (Derived_Type)) then
7411 declare
7412 Ancestor_Type : Entity_Id;
7413
7414 begin
7415 -- Check to see if any ancestor of the derived type is a
7416 -- formal type.
7417
7418 Ancestor_Type := Parent_Type;
7419 while not Is_Generic_Type (Ancestor_Type)
7420 and then Etype (Ancestor_Type) /= Ancestor_Type
7421 loop
7422 Ancestor_Type := Etype (Ancestor_Type);
7423 end loop;
7424
7425 -- If the derived type does have a formal type as an
7426 -- ancestor, then it's an error if the derived type is
7427 -- declared within the body of the generic unit that
7428 -- declares the formal type in its generic formal part. It's
7429 -- sufficient to check whether the ancestor type is declared
7430 -- inside the same generic body as the derived type (such as
7431 -- within a nested generic spec), in which case the
7432 -- derivation is legal. If the formal type is declared
7433 -- outside of that generic body, then it's guaranteed that
7434 -- the derived type is declared within the generic body of
7435 -- the generic unit declaring the formal type.
7436
7437 if Is_Generic_Type (Ancestor_Type)
7438 and then Enclosing_Generic_Body (Ancestor_Type) /=
7439 Enclosing_Generic_Body (Derived_Type)
7440 then
7441 Error_Msg_NE
7442 ("parent type of& must not be descendant of formal type"
7443 & " of an enclosing generic body",
7444 Indic, Derived_Type);
7445 end if;
7446 end;
7447 end if;
7448
7449 elsif Type_Access_Level (Derived_Type) /=
7450 Type_Access_Level (Parent_Type)
7451 and then not Is_Generic_Type (Derived_Type)
7452 then
7453 if Is_Controlled (Parent_Type) then
7454 Error_Msg_N
7455 ("controlled type must be declared at the library level",
7456 Indic);
7457 else
7458 Error_Msg_N
7459 ("type extension at deeper accessibility level than parent",
7460 Indic);
7461 end if;
7462
7463 else
7464 declare
7465 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7466
7467 begin
7468 if Present (GB)
7469 and then GB /= Enclosing_Generic_Body (Parent_Base)
7470 then
7471 Error_Msg_NE
7472 ("parent type of& must not be outside generic body"
7473 & " (RM 3.9.1(4))",
7474 Indic, Derived_Type);
7475 end if;
7476 end;
7477 end if;
7478 end if;
7479
7480 -- Ada 2005 (AI-251)
7481
7482 if Ada_Version >= Ada_2005 and then Is_Tagged then
7483
7484 -- "The declaration of a specific descendant of an interface type
7485 -- freezes the interface type" (RM 13.14).
7486
7487 declare
7488 Iface : Node_Id;
7489 begin
7490 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7491 Iface := First (Interface_List (Type_Def));
7492 while Present (Iface) loop
7493 Freeze_Before (N, Etype (Iface));
7494 Next (Iface);
7495 end loop;
7496 end if;
7497 end;
7498 end if;
7499
7500 -- STEP 1b : preliminary cleanup of the full view of private types
7501
7502 -- If the type is already marked as having discriminants, then it's the
7503 -- completion of a private type or private extension and we need to
7504 -- retain the discriminants from the partial view if the current
7505 -- declaration has Discriminant_Specifications so that we can verify
7506 -- conformance. However, we must remove any existing components that
7507 -- were inherited from the parent (and attached in Copy_And_Swap)
7508 -- because the full type inherits all appropriate components anyway, and
7509 -- we do not want the partial view's components interfering.
7510
7511 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7512 Discrim := First_Discriminant (Derived_Type);
7513 loop
7514 Last_Discrim := Discrim;
7515 Next_Discriminant (Discrim);
7516 exit when No (Discrim);
7517 end loop;
7518
7519 Set_Last_Entity (Derived_Type, Last_Discrim);
7520
7521 -- In all other cases wipe out the list of inherited components (even
7522 -- inherited discriminants), it will be properly rebuilt here.
7523
7524 else
7525 Set_First_Entity (Derived_Type, Empty);
7526 Set_Last_Entity (Derived_Type, Empty);
7527 end if;
7528
7529 -- STEP 1c: Initialize some flags for the Derived_Type
7530
7531 -- The following flags must be initialized here so that
7532 -- Process_Discriminants can check that discriminants of tagged types do
7533 -- not have a default initial value and that access discriminants are
7534 -- only specified for limited records. For completeness, these flags are
7535 -- also initialized along with all the other flags below.
7536
7537 -- AI-419: Limitedness is not inherited from an interface parent, so to
7538 -- be limited in that case the type must be explicitly declared as
7539 -- limited. However, task and protected interfaces are always limited.
7540
7541 if Limited_Present (Type_Def) then
7542 Set_Is_Limited_Record (Derived_Type);
7543
7544 elsif Is_Limited_Record (Parent_Type)
7545 or else (Present (Full_View (Parent_Type))
7546 and then Is_Limited_Record (Full_View (Parent_Type)))
7547 then
7548 if not Is_Interface (Parent_Type)
7549 or else Is_Synchronized_Interface (Parent_Type)
7550 or else Is_Protected_Interface (Parent_Type)
7551 or else Is_Task_Interface (Parent_Type)
7552 then
7553 Set_Is_Limited_Record (Derived_Type);
7554 end if;
7555 end if;
7556
7557 -- STEP 2a: process discriminants of derived type if any
7558
7559 Push_Scope (Derived_Type);
7560
7561 if Discriminant_Specs then
7562 Set_Has_Unknown_Discriminants (Derived_Type, False);
7563
7564 -- The following call initializes fields Has_Discriminants and
7565 -- Discriminant_Constraint, unless we are processing the completion
7566 -- of a private type declaration.
7567
7568 Check_Or_Process_Discriminants (N, Derived_Type);
7569
7570 -- For untagged types, the constraint on the Parent_Type must be
7571 -- present and is used to rename the discriminants.
7572
7573 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7574 Error_Msg_N ("untagged parent must have discriminants", Indic);
7575
7576 elsif not Is_Tagged and then not Constraint_Present then
7577 Error_Msg_N
7578 ("discriminant constraint needed for derived untagged records",
7579 Indic);
7580
7581 -- Otherwise the parent subtype must be constrained unless we have a
7582 -- private extension.
7583
7584 elsif not Constraint_Present
7585 and then not Private_Extension
7586 and then not Is_Constrained (Parent_Type)
7587 then
7588 Error_Msg_N
7589 ("unconstrained type not allowed in this context", Indic);
7590
7591 elsif Constraint_Present then
7592 -- The following call sets the field Corresponding_Discriminant
7593 -- for the discriminants in the Derived_Type.
7594
7595 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7596
7597 -- For untagged types all new discriminants must rename
7598 -- discriminants in the parent. For private extensions new
7599 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7600
7601 Discrim := First_Discriminant (Derived_Type);
7602 while Present (Discrim) loop
7603 if not Is_Tagged
7604 and then No (Corresponding_Discriminant (Discrim))
7605 then
7606 Error_Msg_N
7607 ("new discriminants must constrain old ones", Discrim);
7608
7609 elsif Private_Extension
7610 and then Present (Corresponding_Discriminant (Discrim))
7611 then
7612 Error_Msg_N
7613 ("only static constraints allowed for parent"
7614 & " discriminants in the partial view", Indic);
7615 exit;
7616 end if;
7617
7618 -- If a new discriminant is used in the constraint, then its
7619 -- subtype must be statically compatible with the parent
7620 -- discriminant's subtype (3.7(15)).
7621
7622 -- However, if the record contains an array constrained by
7623 -- the discriminant but with some different bound, the compiler
7624 -- attemps to create a smaller range for the discriminant type.
7625 -- (See exp_ch3.Adjust_Discriminants). In this case, where
7626 -- the discriminant type is a scalar type, the check must use
7627 -- the original discriminant type in the parent declaration.
7628
7629 declare
7630 Corr_Disc : constant Entity_Id :=
7631 Corresponding_Discriminant (Discrim);
7632 Disc_Type : constant Entity_Id := Etype (Discrim);
7633 Corr_Type : Entity_Id;
7634
7635 begin
7636 if Present (Corr_Disc) then
7637 if Is_Scalar_Type (Disc_Type) then
7638 Corr_Type :=
7639 Entity (Discriminant_Type (Parent (Corr_Disc)));
7640 else
7641 Corr_Type := Etype (Corr_Disc);
7642 end if;
7643
7644 if not
7645 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
7646 then
7647 Error_Msg_N
7648 ("subtype must be compatible "
7649 & "with parent discriminant",
7650 Discrim);
7651 end if;
7652 end if;
7653 end;
7654
7655 Next_Discriminant (Discrim);
7656 end loop;
7657
7658 -- Check whether the constraints of the full view statically
7659 -- match those imposed by the parent subtype [7.3(13)].
7660
7661 if Present (Stored_Constraint (Derived_Type)) then
7662 declare
7663 C1, C2 : Elmt_Id;
7664
7665 begin
7666 C1 := First_Elmt (Discs);
7667 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7668 while Present (C1) and then Present (C2) loop
7669 if not
7670 Fully_Conformant_Expressions (Node (C1), Node (C2))
7671 then
7672 Error_Msg_N
7673 ("not conformant with previous declaration",
7674 Node (C1));
7675 end if;
7676
7677 Next_Elmt (C1);
7678 Next_Elmt (C2);
7679 end loop;
7680 end;
7681 end if;
7682 end if;
7683
7684 -- STEP 2b: No new discriminants, inherit discriminants if any
7685
7686 else
7687 if Private_Extension then
7688 Set_Has_Unknown_Discriminants
7689 (Derived_Type,
7690 Has_Unknown_Discriminants (Parent_Type)
7691 or else Unknown_Discriminants_Present (N));
7692
7693 -- The partial view of the parent may have unknown discriminants,
7694 -- but if the full view has discriminants and the parent type is
7695 -- in scope they must be inherited.
7696
7697 elsif Has_Unknown_Discriminants (Parent_Type)
7698 and then
7699 (not Has_Discriminants (Parent_Type)
7700 or else not In_Open_Scopes (Scope (Parent_Type)))
7701 then
7702 Set_Has_Unknown_Discriminants (Derived_Type);
7703 end if;
7704
7705 if not Has_Unknown_Discriminants (Derived_Type)
7706 and then not Has_Unknown_Discriminants (Parent_Base)
7707 and then Has_Discriminants (Parent_Type)
7708 then
7709 Inherit_Discrims := True;
7710 Set_Has_Discriminants
7711 (Derived_Type, True);
7712 Set_Discriminant_Constraint
7713 (Derived_Type, Discriminant_Constraint (Parent_Base));
7714 end if;
7715
7716 -- The following test is true for private types (remember
7717 -- transformation 5. is not applied to those) and in an error
7718 -- situation.
7719
7720 if Constraint_Present then
7721 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7722 end if;
7723
7724 -- For now mark a new derived type as constrained only if it has no
7725 -- discriminants. At the end of Build_Derived_Record_Type we properly
7726 -- set this flag in the case of private extensions. See comments in
7727 -- point 9. just before body of Build_Derived_Record_Type.
7728
7729 Set_Is_Constrained
7730 (Derived_Type,
7731 not (Inherit_Discrims
7732 or else Has_Unknown_Discriminants (Derived_Type)));
7733 end if;
7734
7735 -- STEP 3: initialize fields of derived type
7736
7737 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7738 Set_Stored_Constraint (Derived_Type, No_Elist);
7739
7740 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7741 -- but cannot be interfaces
7742
7743 if not Private_Extension
7744 and then Ekind (Derived_Type) /= E_Private_Type
7745 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7746 then
7747 if Interface_Present (Type_Def) then
7748 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7749 end if;
7750
7751 Set_Interfaces (Derived_Type, No_Elist);
7752 end if;
7753
7754 -- Fields inherited from the Parent_Type
7755
7756 Set_Has_Specified_Layout
7757 (Derived_Type, Has_Specified_Layout (Parent_Type));
7758 Set_Is_Limited_Composite
7759 (Derived_Type, Is_Limited_Composite (Parent_Type));
7760 Set_Is_Private_Composite
7761 (Derived_Type, Is_Private_Composite (Parent_Type));
7762
7763 -- Fields inherited from the Parent_Base
7764
7765 Set_Has_Controlled_Component
7766 (Derived_Type, Has_Controlled_Component (Parent_Base));
7767 Set_Has_Non_Standard_Rep
7768 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7769 Set_Has_Primitive_Operations
7770 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7771
7772 -- Fields inherited from the Parent_Base in the non-private case
7773
7774 if Ekind (Derived_Type) = E_Record_Type then
7775 Set_Has_Complex_Representation
7776 (Derived_Type, Has_Complex_Representation (Parent_Base));
7777 end if;
7778
7779 -- Fields inherited from the Parent_Base for record types
7780
7781 if Is_Record_Type (Derived_Type) then
7782
7783 declare
7784 Parent_Full : Entity_Id;
7785
7786 begin
7787 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7788 -- Parent_Base can be a private type or private extension. Go
7789 -- to the full view here to get the E_Record_Type specific flags.
7790
7791 if Present (Full_View (Parent_Base)) then
7792 Parent_Full := Full_View (Parent_Base);
7793 else
7794 Parent_Full := Parent_Base;
7795 end if;
7796
7797 Set_OK_To_Reorder_Components
7798 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
7799 end;
7800 end if;
7801
7802 -- Set fields for private derived types
7803
7804 if Is_Private_Type (Derived_Type) then
7805 Set_Depends_On_Private (Derived_Type, True);
7806 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7807
7808 -- Inherit fields from non private record types. If this is the
7809 -- completion of a derivation from a private type, the parent itself
7810 -- is private, and the attributes come from its full view, which must
7811 -- be present.
7812
7813 else
7814 if Is_Private_Type (Parent_Base)
7815 and then not Is_Record_Type (Parent_Base)
7816 then
7817 Set_Component_Alignment
7818 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7819 Set_C_Pass_By_Copy
7820 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7821 else
7822 Set_Component_Alignment
7823 (Derived_Type, Component_Alignment (Parent_Base));
7824 Set_C_Pass_By_Copy
7825 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7826 end if;
7827 end if;
7828
7829 -- Set fields for tagged types
7830
7831 if Is_Tagged then
7832 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7833
7834 -- All tagged types defined in Ada.Finalization are controlled
7835
7836 if Chars (Scope (Derived_Type)) = Name_Finalization
7837 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7838 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7839 then
7840 Set_Is_Controlled (Derived_Type);
7841 else
7842 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7843 end if;
7844
7845 -- Minor optimization: there is no need to generate the class-wide
7846 -- entity associated with an underlying record view.
7847
7848 if not Is_Underlying_Record_View (Derived_Type) then
7849 Make_Class_Wide_Type (Derived_Type);
7850 end if;
7851
7852 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7853
7854 if Has_Discriminants (Derived_Type)
7855 and then Constraint_Present
7856 then
7857 Set_Stored_Constraint
7858 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7859 end if;
7860
7861 if Ada_Version >= Ada_2005 then
7862 declare
7863 Ifaces_List : Elist_Id;
7864
7865 begin
7866 -- Checks rules 3.9.4 (13/2 and 14/2)
7867
7868 if Comes_From_Source (Derived_Type)
7869 and then not Is_Private_Type (Derived_Type)
7870 and then Is_Interface (Parent_Type)
7871 and then not Is_Interface (Derived_Type)
7872 then
7873 if Is_Task_Interface (Parent_Type) then
7874 Error_Msg_N
7875 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7876 Derived_Type);
7877
7878 elsif Is_Protected_Interface (Parent_Type) then
7879 Error_Msg_N
7880 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7881 Derived_Type);
7882 end if;
7883 end if;
7884
7885 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7886
7887 Check_Interfaces (N, Type_Def);
7888
7889 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7890 -- not already in the parents.
7891
7892 Collect_Interfaces
7893 (T => Derived_Type,
7894 Ifaces_List => Ifaces_List,
7895 Exclude_Parents => True);
7896
7897 Set_Interfaces (Derived_Type, Ifaces_List);
7898
7899 -- If the derived type is the anonymous type created for
7900 -- a declaration whose parent has a constraint, propagate
7901 -- the interface list to the source type. This must be done
7902 -- prior to the completion of the analysis of the source type
7903 -- because the components in the extension may contain current
7904 -- instances whose legality depends on some ancestor.
7905
7906 if Is_Itype (Derived_Type) then
7907 declare
7908 Def : constant Node_Id :=
7909 Associated_Node_For_Itype (Derived_Type);
7910 begin
7911 if Present (Def)
7912 and then Nkind (Def) = N_Full_Type_Declaration
7913 then
7914 Set_Interfaces
7915 (Defining_Identifier (Def), Ifaces_List);
7916 end if;
7917 end;
7918 end if;
7919 end;
7920 end if;
7921
7922 else
7923 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7924 Set_Has_Non_Standard_Rep
7925 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7926 end if;
7927
7928 -- STEP 4: Inherit components from the parent base and constrain them.
7929 -- Apply the second transformation described in point 6. above.
7930
7931 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7932 or else not Has_Discriminants (Parent_Type)
7933 or else not Is_Constrained (Parent_Type)
7934 then
7935 Constrs := Discs;
7936 else
7937 Constrs := Discriminant_Constraint (Parent_Type);
7938 end if;
7939
7940 Assoc_List :=
7941 Inherit_Components
7942 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7943
7944 -- STEP 5a: Copy the parent record declaration for untagged types
7945
7946 if not Is_Tagged then
7947
7948 -- Discriminant_Constraint (Derived_Type) has been properly
7949 -- constructed. Save it and temporarily set it to Empty because we
7950 -- do not want the call to New_Copy_Tree below to mess this list.
7951
7952 if Has_Discriminants (Derived_Type) then
7953 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7954 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7955 else
7956 Save_Discr_Constr := No_Elist;
7957 end if;
7958
7959 -- Save the Etype field of Derived_Type. It is correctly set now,
7960 -- but the call to New_Copy tree may remap it to point to itself,
7961 -- which is not what we want. Ditto for the Next_Entity field.
7962
7963 Save_Etype := Etype (Derived_Type);
7964 Save_Next_Entity := Next_Entity (Derived_Type);
7965
7966 -- Assoc_List maps all stored discriminants in the Parent_Base to
7967 -- stored discriminants in the Derived_Type. It is fundamental that
7968 -- no types or itypes with discriminants other than the stored
7969 -- discriminants appear in the entities declared inside
7970 -- Derived_Type, since the back end cannot deal with it.
7971
7972 New_Decl :=
7973 New_Copy_Tree
7974 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7975
7976 -- Restore the fields saved prior to the New_Copy_Tree call
7977 -- and compute the stored constraint.
7978
7979 Set_Etype (Derived_Type, Save_Etype);
7980 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7981
7982 if Has_Discriminants (Derived_Type) then
7983 Set_Discriminant_Constraint
7984 (Derived_Type, Save_Discr_Constr);
7985 Set_Stored_Constraint
7986 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7987 Replace_Components (Derived_Type, New_Decl);
7988 Set_Has_Implicit_Dereference
7989 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
7990 end if;
7991
7992 -- Insert the new derived type declaration
7993
7994 Rewrite (N, New_Decl);
7995
7996 -- STEP 5b: Complete the processing for record extensions in generics
7997
7998 -- There is no completion for record extensions declared in the
7999 -- parameter part of a generic, so we need to complete processing for
8000 -- these generic record extensions here. The Record_Type_Definition call
8001 -- will change the Ekind of the components from E_Void to E_Component.
8002
8003 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8004 Record_Type_Definition (Empty, Derived_Type);
8005
8006 -- STEP 5c: Process the record extension for non private tagged types
8007
8008 elsif not Private_Extension then
8009
8010 -- Add the _parent field in the derived type
8011
8012 Expand_Record_Extension (Derived_Type, Type_Def);
8013
8014 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8015 -- implemented interfaces if we are in expansion mode
8016
8017 if Expander_Active
8018 and then Has_Interfaces (Derived_Type)
8019 then
8020 Add_Interface_Tag_Components (N, Derived_Type);
8021 end if;
8022
8023 -- Analyze the record extension
8024
8025 Record_Type_Definition
8026 (Record_Extension_Part (Type_Def), Derived_Type);
8027 end if;
8028
8029 End_Scope;
8030
8031 -- Nothing else to do if there is an error in the derivation.
8032 -- An unusual case: the full view may be derived from a type in an
8033 -- instance, when the partial view was used illegally as an actual
8034 -- in that instance, leading to a circular definition.
8035
8036 if Etype (Derived_Type) = Any_Type
8037 or else Etype (Parent_Type) = Derived_Type
8038 then
8039 return;
8040 end if;
8041
8042 -- Set delayed freeze and then derive subprograms, we need to do
8043 -- this in this order so that derived subprograms inherit the
8044 -- derived freeze if necessary.
8045
8046 Set_Has_Delayed_Freeze (Derived_Type);
8047
8048 if Derive_Subps then
8049 Derive_Subprograms (Parent_Type, Derived_Type);
8050 end if;
8051
8052 -- If we have a private extension which defines a constrained derived
8053 -- type mark as constrained here after we have derived subprograms. See
8054 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8055
8056 if Private_Extension and then Inherit_Discrims then
8057 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8058 Set_Is_Constrained (Derived_Type, True);
8059 Set_Discriminant_Constraint (Derived_Type, Discs);
8060
8061 elsif Is_Constrained (Parent_Type) then
8062 Set_Is_Constrained
8063 (Derived_Type, True);
8064 Set_Discriminant_Constraint
8065 (Derived_Type, Discriminant_Constraint (Parent_Type));
8066 end if;
8067 end if;
8068
8069 -- Update the class-wide type, which shares the now-completed entity
8070 -- list with its specific type. In case of underlying record views,
8071 -- we do not generate the corresponding class wide entity.
8072
8073 if Is_Tagged
8074 and then not Is_Underlying_Record_View (Derived_Type)
8075 then
8076 Set_First_Entity
8077 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8078 Set_Last_Entity
8079 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8080 end if;
8081
8082 Check_Function_Writable_Actuals (N);
8083 end Build_Derived_Record_Type;
8084
8085 ------------------------
8086 -- Build_Derived_Type --
8087 ------------------------
8088
8089 procedure Build_Derived_Type
8090 (N : Node_Id;
8091 Parent_Type : Entity_Id;
8092 Derived_Type : Entity_Id;
8093 Is_Completion : Boolean;
8094 Derive_Subps : Boolean := True)
8095 is
8096 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8097
8098 begin
8099 -- Set common attributes
8100
8101 Set_Scope (Derived_Type, Current_Scope);
8102
8103 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8104 Set_Etype (Derived_Type, Parent_Base);
8105 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8106
8107 Set_Size_Info (Derived_Type, Parent_Type);
8108 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8109 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8110 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8111
8112 -- If the parent type is a private subtype, the convention on the base
8113 -- type may be set in the private part, and not propagated to the
8114 -- subtype until later, so we obtain the convention from the base type.
8115
8116 Set_Convention (Derived_Type, Convention (Parent_Base));
8117
8118 -- Propagate invariant information. The new type has invariants if
8119 -- they are inherited from the parent type, and these invariants can
8120 -- be further inherited, so both flags are set.
8121
8122 -- We similarly inherit predicates
8123
8124 if Has_Predicates (Parent_Type) then
8125 Set_Has_Predicates (Derived_Type);
8126 end if;
8127
8128 -- The derived type inherits the representation clauses of the parent.
8129 -- However, for a private type that is completed by a derivation, there
8130 -- may be operation attributes that have been specified already (stream
8131 -- attributes and External_Tag) and those must be provided. Finally,
8132 -- if the partial view is a private extension, the representation items
8133 -- of the parent have been inherited already, and should not be chained
8134 -- twice to the derived type.
8135
8136 if Is_Tagged_Type (Parent_Type)
8137 and then Present (First_Rep_Item (Derived_Type))
8138 then
8139 -- The existing items are either operational items or items inherited
8140 -- from a private extension declaration.
8141
8142 declare
8143 Rep : Node_Id;
8144 -- Used to iterate over representation items of the derived type
8145
8146 Last_Rep : Node_Id;
8147 -- Last representation item of the (non-empty) representation
8148 -- item list of the derived type.
8149
8150 Found : Boolean := False;
8151
8152 begin
8153 Rep := First_Rep_Item (Derived_Type);
8154 Last_Rep := Rep;
8155 while Present (Rep) loop
8156 if Rep = First_Rep_Item (Parent_Type) then
8157 Found := True;
8158 exit;
8159
8160 else
8161 Rep := Next_Rep_Item (Rep);
8162
8163 if Present (Rep) then
8164 Last_Rep := Rep;
8165 end if;
8166 end if;
8167 end loop;
8168
8169 -- Here if we either encountered the parent type's first rep
8170 -- item on the derived type's rep item list (in which case
8171 -- Found is True, and we have nothing else to do), or if we
8172 -- reached the last rep item of the derived type, which is
8173 -- Last_Rep, in which case we further chain the parent type's
8174 -- rep items to those of the derived type.
8175
8176 if not Found then
8177 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
8178 end if;
8179 end;
8180
8181 else
8182 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8183 end if;
8184
8185 -- If the parent type has delayed rep aspects, then mark the derived
8186 -- type as possibly inheriting a delayed rep aspect.
8187
8188 if Has_Delayed_Rep_Aspects (Parent_Type) then
8189 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8190 end if;
8191
8192 -- Type dependent processing
8193
8194 case Ekind (Parent_Type) is
8195 when Numeric_Kind =>
8196 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8197
8198 when Array_Kind =>
8199 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8200
8201 when E_Record_Type
8202 | E_Record_Subtype
8203 | Class_Wide_Kind =>
8204 Build_Derived_Record_Type
8205 (N, Parent_Type, Derived_Type, Derive_Subps);
8206 return;
8207
8208 when Enumeration_Kind =>
8209 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8210
8211 when Access_Kind =>
8212 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8213
8214 when Incomplete_Or_Private_Kind =>
8215 Build_Derived_Private_Type
8216 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8217
8218 -- For discriminated types, the derivation includes deriving
8219 -- primitive operations. For others it is done below.
8220
8221 if Is_Tagged_Type (Parent_Type)
8222 or else Has_Discriminants (Parent_Type)
8223 or else (Present (Full_View (Parent_Type))
8224 and then Has_Discriminants (Full_View (Parent_Type)))
8225 then
8226 return;
8227 end if;
8228
8229 when Concurrent_Kind =>
8230 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8231
8232 when others =>
8233 raise Program_Error;
8234 end case;
8235
8236 -- Nothing more to do if some error occurred
8237
8238 if Etype (Derived_Type) = Any_Type then
8239 return;
8240 end if;
8241
8242 -- Set delayed freeze and then derive subprograms, we need to do this
8243 -- in this order so that derived subprograms inherit the derived freeze
8244 -- if necessary.
8245
8246 Set_Has_Delayed_Freeze (Derived_Type);
8247
8248 if Derive_Subps then
8249 Derive_Subprograms (Parent_Type, Derived_Type);
8250 end if;
8251
8252 Set_Has_Primitive_Operations
8253 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8254 end Build_Derived_Type;
8255
8256 -----------------------
8257 -- Build_Discriminal --
8258 -----------------------
8259
8260 procedure Build_Discriminal (Discrim : Entity_Id) is
8261 D_Minal : Entity_Id;
8262 CR_Disc : Entity_Id;
8263
8264 begin
8265 -- A discriminal has the same name as the discriminant
8266
8267 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8268
8269 Set_Ekind (D_Minal, E_In_Parameter);
8270 Set_Mechanism (D_Minal, Default_Mechanism);
8271 Set_Etype (D_Minal, Etype (Discrim));
8272 Set_Scope (D_Minal, Current_Scope);
8273
8274 Set_Discriminal (Discrim, D_Minal);
8275 Set_Discriminal_Link (D_Minal, Discrim);
8276
8277 -- For task types, build at once the discriminants of the corresponding
8278 -- record, which are needed if discriminants are used in entry defaults
8279 -- and in family bounds.
8280
8281 if Is_Concurrent_Type (Current_Scope)
8282 or else Is_Limited_Type (Current_Scope)
8283 then
8284 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8285
8286 Set_Ekind (CR_Disc, E_In_Parameter);
8287 Set_Mechanism (CR_Disc, Default_Mechanism);
8288 Set_Etype (CR_Disc, Etype (Discrim));
8289 Set_Scope (CR_Disc, Current_Scope);
8290 Set_Discriminal_Link (CR_Disc, Discrim);
8291 Set_CR_Discriminant (Discrim, CR_Disc);
8292 end if;
8293 end Build_Discriminal;
8294
8295 ------------------------------------
8296 -- Build_Discriminant_Constraints --
8297 ------------------------------------
8298
8299 function Build_Discriminant_Constraints
8300 (T : Entity_Id;
8301 Def : Node_Id;
8302 Derived_Def : Boolean := False) return Elist_Id
8303 is
8304 C : constant Node_Id := Constraint (Def);
8305 Nb_Discr : constant Nat := Number_Discriminants (T);
8306
8307 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8308 -- Saves the expression corresponding to a given discriminant in T
8309
8310 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8311 -- Return the Position number within array Discr_Expr of a discriminant
8312 -- D within the discriminant list of the discriminated type T.
8313
8314 procedure Process_Discriminant_Expression
8315 (Expr : Node_Id;
8316 D : Entity_Id);
8317 -- If this is a discriminant constraint on a partial view, do not
8318 -- generate an overflow check on the discriminant expression. The check
8319 -- will be generated when constraining the full view. Otherwise the
8320 -- backend creates duplicate symbols for the temporaries corresponding
8321 -- to the expressions to be checked, causing spurious assembler errors.
8322
8323 ------------------
8324 -- Pos_Of_Discr --
8325 ------------------
8326
8327 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8328 Disc : Entity_Id;
8329
8330 begin
8331 Disc := First_Discriminant (T);
8332 for J in Discr_Expr'Range loop
8333 if Disc = D then
8334 return J;
8335 end if;
8336
8337 Next_Discriminant (Disc);
8338 end loop;
8339
8340 -- Note: Since this function is called on discriminants that are
8341 -- known to belong to the discriminated type, falling through the
8342 -- loop with no match signals an internal compiler error.
8343
8344 raise Program_Error;
8345 end Pos_Of_Discr;
8346
8347 -------------------------------------
8348 -- Process_Discriminant_Expression --
8349 -------------------------------------
8350
8351 procedure Process_Discriminant_Expression
8352 (Expr : Node_Id;
8353 D : Entity_Id)
8354 is
8355 BDT : constant Entity_Id := Base_Type (Etype (D));
8356
8357 begin
8358 -- If this is a discriminant constraint on a partial view, do
8359 -- not generate an overflow on the discriminant expression. The
8360 -- check will be generated when constraining the full view.
8361
8362 if Is_Private_Type (T)
8363 and then Present (Full_View (T))
8364 then
8365 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
8366 else
8367 Analyze_And_Resolve (Expr, BDT);
8368 end if;
8369 end Process_Discriminant_Expression;
8370
8371 -- Declarations local to Build_Discriminant_Constraints
8372
8373 Discr : Entity_Id;
8374 E : Entity_Id;
8375 Elist : constant Elist_Id := New_Elmt_List;
8376
8377 Constr : Node_Id;
8378 Expr : Node_Id;
8379 Id : Node_Id;
8380 Position : Nat;
8381 Found : Boolean;
8382
8383 Discrim_Present : Boolean := False;
8384
8385 -- Start of processing for Build_Discriminant_Constraints
8386
8387 begin
8388 -- The following loop will process positional associations only.
8389 -- For a positional association, the (single) discriminant is
8390 -- implicitly specified by position, in textual order (RM 3.7.2).
8391
8392 Discr := First_Discriminant (T);
8393 Constr := First (Constraints (C));
8394 for D in Discr_Expr'Range loop
8395 exit when Nkind (Constr) = N_Discriminant_Association;
8396
8397 if No (Constr) then
8398 Error_Msg_N ("too few discriminants given in constraint", C);
8399 return New_Elmt_List;
8400
8401 elsif Nkind (Constr) = N_Range
8402 or else (Nkind (Constr) = N_Attribute_Reference
8403 and then
8404 Attribute_Name (Constr) = Name_Range)
8405 then
8406 Error_Msg_N
8407 ("a range is not a valid discriminant constraint", Constr);
8408 Discr_Expr (D) := Error;
8409
8410 else
8411 Process_Discriminant_Expression (Constr, Discr);
8412 Discr_Expr (D) := Constr;
8413 end if;
8414
8415 Next_Discriminant (Discr);
8416 Next (Constr);
8417 end loop;
8418
8419 if No (Discr) and then Present (Constr) then
8420 Error_Msg_N ("too many discriminants given in constraint", Constr);
8421 return New_Elmt_List;
8422 end if;
8423
8424 -- Named associations can be given in any order, but if both positional
8425 -- and named associations are used in the same discriminant constraint,
8426 -- then positional associations must occur first, at their normal
8427 -- position. Hence once a named association is used, the rest of the
8428 -- discriminant constraint must use only named associations.
8429
8430 while Present (Constr) loop
8431
8432 -- Positional association forbidden after a named association
8433
8434 if Nkind (Constr) /= N_Discriminant_Association then
8435 Error_Msg_N ("positional association follows named one", Constr);
8436 return New_Elmt_List;
8437
8438 -- Otherwise it is a named association
8439
8440 else
8441 -- E records the type of the discriminants in the named
8442 -- association. All the discriminants specified in the same name
8443 -- association must have the same type.
8444
8445 E := Empty;
8446
8447 -- Search the list of discriminants in T to see if the simple name
8448 -- given in the constraint matches any of them.
8449
8450 Id := First (Selector_Names (Constr));
8451 while Present (Id) loop
8452 Found := False;
8453
8454 -- If Original_Discriminant is present, we are processing a
8455 -- generic instantiation and this is an instance node. We need
8456 -- to find the name of the corresponding discriminant in the
8457 -- actual record type T and not the name of the discriminant in
8458 -- the generic formal. Example:
8459
8460 -- generic
8461 -- type G (D : int) is private;
8462 -- package P is
8463 -- subtype W is G (D => 1);
8464 -- end package;
8465 -- type Rec (X : int) is record ... end record;
8466 -- package Q is new P (G => Rec);
8467
8468 -- At the point of the instantiation, formal type G is Rec
8469 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8470 -- which really looks like "subtype W is Rec (D => 1);" at
8471 -- the point of instantiation, we want to find the discriminant
8472 -- that corresponds to D in Rec, i.e. X.
8473
8474 if Present (Original_Discriminant (Id))
8475 and then In_Instance
8476 then
8477 Discr := Find_Corresponding_Discriminant (Id, T);
8478 Found := True;
8479
8480 else
8481 Discr := First_Discriminant (T);
8482 while Present (Discr) loop
8483 if Chars (Discr) = Chars (Id) then
8484 Found := True;
8485 exit;
8486 end if;
8487
8488 Next_Discriminant (Discr);
8489 end loop;
8490
8491 if not Found then
8492 Error_Msg_N ("& does not match any discriminant", Id);
8493 return New_Elmt_List;
8494
8495 -- If the parent type is a generic formal, preserve the
8496 -- name of the discriminant for subsequent instances.
8497 -- see comment at the beginning of this if statement.
8498
8499 elsif Is_Generic_Type (Root_Type (T)) then
8500 Set_Original_Discriminant (Id, Discr);
8501 end if;
8502 end if;
8503
8504 Position := Pos_Of_Discr (T, Discr);
8505
8506 if Present (Discr_Expr (Position)) then
8507 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8508
8509 else
8510 -- Each discriminant specified in the same named association
8511 -- must be associated with a separate copy of the
8512 -- corresponding expression.
8513
8514 if Present (Next (Id)) then
8515 Expr := New_Copy_Tree (Expression (Constr));
8516 Set_Parent (Expr, Parent (Expression (Constr)));
8517 else
8518 Expr := Expression (Constr);
8519 end if;
8520
8521 Discr_Expr (Position) := Expr;
8522 Process_Discriminant_Expression (Expr, Discr);
8523 end if;
8524
8525 -- A discriminant association with more than one discriminant
8526 -- name is only allowed if the named discriminants are all of
8527 -- the same type (RM 3.7.1(8)).
8528
8529 if E = Empty then
8530 E := Base_Type (Etype (Discr));
8531
8532 elsif Base_Type (Etype (Discr)) /= E then
8533 Error_Msg_N
8534 ("all discriminants in an association " &
8535 "must have the same type", Id);
8536 end if;
8537
8538 Next (Id);
8539 end loop;
8540 end if;
8541
8542 Next (Constr);
8543 end loop;
8544
8545 -- A discriminant constraint must provide exactly one value for each
8546 -- discriminant of the type (RM 3.7.1(8)).
8547
8548 for J in Discr_Expr'Range loop
8549 if No (Discr_Expr (J)) then
8550 Error_Msg_N ("too few discriminants given in constraint", C);
8551 return New_Elmt_List;
8552 end if;
8553 end loop;
8554
8555 -- Determine if there are discriminant expressions in the constraint
8556
8557 for J in Discr_Expr'Range loop
8558 if Denotes_Discriminant
8559 (Discr_Expr (J), Check_Concurrent => True)
8560 then
8561 Discrim_Present := True;
8562 end if;
8563 end loop;
8564
8565 -- Build an element list consisting of the expressions given in the
8566 -- discriminant constraint and apply the appropriate checks. The list
8567 -- is constructed after resolving any named discriminant associations
8568 -- and therefore the expressions appear in the textual order of the
8569 -- discriminants.
8570
8571 Discr := First_Discriminant (T);
8572 for J in Discr_Expr'Range loop
8573 if Discr_Expr (J) /= Error then
8574 Append_Elmt (Discr_Expr (J), Elist);
8575
8576 -- If any of the discriminant constraints is given by a
8577 -- discriminant and we are in a derived type declaration we
8578 -- have a discriminant renaming. Establish link between new
8579 -- and old discriminant.
8580
8581 if Denotes_Discriminant (Discr_Expr (J)) then
8582 if Derived_Def then
8583 Set_Corresponding_Discriminant
8584 (Entity (Discr_Expr (J)), Discr);
8585 end if;
8586
8587 -- Force the evaluation of non-discriminant expressions.
8588 -- If we have found a discriminant in the constraint 3.4(26)
8589 -- and 3.8(18) demand that no range checks are performed are
8590 -- after evaluation. If the constraint is for a component
8591 -- definition that has a per-object constraint, expressions are
8592 -- evaluated but not checked either. In all other cases perform
8593 -- a range check.
8594
8595 else
8596 if Discrim_Present then
8597 null;
8598
8599 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8600 and then
8601 Has_Per_Object_Constraint
8602 (Defining_Identifier (Parent (Parent (Def))))
8603 then
8604 null;
8605
8606 elsif Is_Access_Type (Etype (Discr)) then
8607 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8608
8609 else
8610 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8611 end if;
8612
8613 Force_Evaluation (Discr_Expr (J));
8614 end if;
8615
8616 -- Check that the designated type of an access discriminant's
8617 -- expression is not a class-wide type unless the discriminant's
8618 -- designated type is also class-wide.
8619
8620 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8621 and then not Is_Class_Wide_Type
8622 (Designated_Type (Etype (Discr)))
8623 and then Etype (Discr_Expr (J)) /= Any_Type
8624 and then Is_Class_Wide_Type
8625 (Designated_Type (Etype (Discr_Expr (J))))
8626 then
8627 Wrong_Type (Discr_Expr (J), Etype (Discr));
8628
8629 elsif Is_Access_Type (Etype (Discr))
8630 and then not Is_Access_Constant (Etype (Discr))
8631 and then Is_Access_Type (Etype (Discr_Expr (J)))
8632 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8633 then
8634 Error_Msg_NE
8635 ("constraint for discriminant& must be access to variable",
8636 Def, Discr);
8637 end if;
8638 end if;
8639
8640 Next_Discriminant (Discr);
8641 end loop;
8642
8643 return Elist;
8644 end Build_Discriminant_Constraints;
8645
8646 ---------------------------------
8647 -- Build_Discriminated_Subtype --
8648 ---------------------------------
8649
8650 procedure Build_Discriminated_Subtype
8651 (T : Entity_Id;
8652 Def_Id : Entity_Id;
8653 Elist : Elist_Id;
8654 Related_Nod : Node_Id;
8655 For_Access : Boolean := False)
8656 is
8657 Has_Discrs : constant Boolean := Has_Discriminants (T);
8658 Constrained : constant Boolean :=
8659 (Has_Discrs
8660 and then not Is_Empty_Elmt_List (Elist)
8661 and then not Is_Class_Wide_Type (T))
8662 or else Is_Constrained (T);
8663
8664 begin
8665 if Ekind (T) = E_Record_Type then
8666 if For_Access then
8667 Set_Ekind (Def_Id, E_Private_Subtype);
8668 Set_Is_For_Access_Subtype (Def_Id, True);
8669 else
8670 Set_Ekind (Def_Id, E_Record_Subtype);
8671 end if;
8672
8673 -- Inherit preelaboration flag from base, for types for which it
8674 -- may have been set: records, private types, protected types.
8675
8676 Set_Known_To_Have_Preelab_Init
8677 (Def_Id, Known_To_Have_Preelab_Init (T));
8678
8679 elsif Ekind (T) = E_Task_Type then
8680 Set_Ekind (Def_Id, E_Task_Subtype);
8681
8682 elsif Ekind (T) = E_Protected_Type then
8683 Set_Ekind (Def_Id, E_Protected_Subtype);
8684 Set_Known_To_Have_Preelab_Init
8685 (Def_Id, Known_To_Have_Preelab_Init (T));
8686
8687 elsif Is_Private_Type (T) then
8688 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8689 Set_Known_To_Have_Preelab_Init
8690 (Def_Id, Known_To_Have_Preelab_Init (T));
8691
8692 -- Private subtypes may have private dependents
8693
8694 Set_Private_Dependents (Def_Id, New_Elmt_List);
8695
8696 elsif Is_Class_Wide_Type (T) then
8697 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8698
8699 else
8700 -- Incomplete type. Attach subtype to list of dependents, to be
8701 -- completed with full view of parent type, unless is it the
8702 -- designated subtype of a record component within an init_proc.
8703 -- This last case arises for a component of an access type whose
8704 -- designated type is incomplete (e.g. a Taft Amendment type).
8705 -- The designated subtype is within an inner scope, and needs no
8706 -- elaboration, because only the access type is needed in the
8707 -- initialization procedure.
8708
8709 Set_Ekind (Def_Id, Ekind (T));
8710
8711 if For_Access and then Within_Init_Proc then
8712 null;
8713 else
8714 Append_Elmt (Def_Id, Private_Dependents (T));
8715 end if;
8716 end if;
8717
8718 Set_Etype (Def_Id, T);
8719 Init_Size_Align (Def_Id);
8720 Set_Has_Discriminants (Def_Id, Has_Discrs);
8721 Set_Is_Constrained (Def_Id, Constrained);
8722
8723 Set_First_Entity (Def_Id, First_Entity (T));
8724 Set_Last_Entity (Def_Id, Last_Entity (T));
8725 Set_Has_Implicit_Dereference
8726 (Def_Id, Has_Implicit_Dereference (T));
8727
8728 -- If the subtype is the completion of a private declaration, there may
8729 -- have been representation clauses for the partial view, and they must
8730 -- be preserved. Build_Derived_Type chains the inherited clauses with
8731 -- the ones appearing on the extension. If this comes from a subtype
8732 -- declaration, all clauses are inherited.
8733
8734 if No (First_Rep_Item (Def_Id)) then
8735 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8736 end if;
8737
8738 if Is_Tagged_Type (T) then
8739 Set_Is_Tagged_Type (Def_Id);
8740 Make_Class_Wide_Type (Def_Id);
8741 end if;
8742
8743 Set_Stored_Constraint (Def_Id, No_Elist);
8744
8745 if Has_Discrs then
8746 Set_Discriminant_Constraint (Def_Id, Elist);
8747 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8748 end if;
8749
8750 if Is_Tagged_Type (T) then
8751
8752 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8753 -- concurrent record type (which has the list of primitive
8754 -- operations).
8755
8756 if Ada_Version >= Ada_2005
8757 and then Is_Concurrent_Type (T)
8758 then
8759 Set_Corresponding_Record_Type (Def_Id,
8760 Corresponding_Record_Type (T));
8761 else
8762 Set_Direct_Primitive_Operations (Def_Id,
8763 Direct_Primitive_Operations (T));
8764 end if;
8765
8766 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8767 end if;
8768
8769 -- Subtypes introduced by component declarations do not need to be
8770 -- marked as delayed, and do not get freeze nodes, because the semantics
8771 -- verifies that the parents of the subtypes are frozen before the
8772 -- enclosing record is frozen.
8773
8774 if not Is_Type (Scope (Def_Id)) then
8775 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8776
8777 if Is_Private_Type (T)
8778 and then Present (Full_View (T))
8779 then
8780 Conditional_Delay (Def_Id, Full_View (T));
8781 else
8782 Conditional_Delay (Def_Id, T);
8783 end if;
8784 end if;
8785
8786 if Is_Record_Type (T) then
8787 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8788
8789 if Has_Discrs
8790 and then not Is_Empty_Elmt_List (Elist)
8791 and then not For_Access
8792 then
8793 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8794 elsif not For_Access then
8795 Set_Cloned_Subtype (Def_Id, T);
8796 end if;
8797 end if;
8798 end Build_Discriminated_Subtype;
8799
8800 ---------------------------
8801 -- Build_Itype_Reference --
8802 ---------------------------
8803
8804 procedure Build_Itype_Reference
8805 (Ityp : Entity_Id;
8806 Nod : Node_Id)
8807 is
8808 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8809 begin
8810
8811 -- Itype references are only created for use by the back-end
8812
8813 if Inside_A_Generic then
8814 return;
8815 else
8816 Set_Itype (IR, Ityp);
8817 Insert_After (Nod, IR);
8818 end if;
8819 end Build_Itype_Reference;
8820
8821 ------------------------
8822 -- Build_Scalar_Bound --
8823 ------------------------
8824
8825 function Build_Scalar_Bound
8826 (Bound : Node_Id;
8827 Par_T : Entity_Id;
8828 Der_T : Entity_Id) return Node_Id
8829 is
8830 New_Bound : Entity_Id;
8831
8832 begin
8833 -- Note: not clear why this is needed, how can the original bound
8834 -- be unanalyzed at this point? and if it is, what business do we
8835 -- have messing around with it? and why is the base type of the
8836 -- parent type the right type for the resolution. It probably is
8837 -- not! It is OK for the new bound we are creating, but not for
8838 -- the old one??? Still if it never happens, no problem!
8839
8840 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8841
8842 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8843 New_Bound := New_Copy (Bound);
8844 Set_Etype (New_Bound, Der_T);
8845 Set_Analyzed (New_Bound);
8846
8847 elsif Is_Entity_Name (Bound) then
8848 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8849
8850 -- The following is almost certainly wrong. What business do we have
8851 -- relocating a node (Bound) that is presumably still attached to
8852 -- the tree elsewhere???
8853
8854 else
8855 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8856 end if;
8857
8858 Set_Etype (New_Bound, Der_T);
8859 return New_Bound;
8860 end Build_Scalar_Bound;
8861
8862 --------------------------------
8863 -- Build_Underlying_Full_View --
8864 --------------------------------
8865
8866 procedure Build_Underlying_Full_View
8867 (N : Node_Id;
8868 Typ : Entity_Id;
8869 Par : Entity_Id)
8870 is
8871 Loc : constant Source_Ptr := Sloc (N);
8872 Subt : constant Entity_Id :=
8873 Make_Defining_Identifier
8874 (Loc, New_External_Name (Chars (Typ), 'S'));
8875
8876 Constr : Node_Id;
8877 Indic : Node_Id;
8878 C : Node_Id;
8879 Id : Node_Id;
8880
8881 procedure Set_Discriminant_Name (Id : Node_Id);
8882 -- If the derived type has discriminants, they may rename discriminants
8883 -- of the parent. When building the full view of the parent, we need to
8884 -- recover the names of the original discriminants if the constraint is
8885 -- given by named associations.
8886
8887 ---------------------------
8888 -- Set_Discriminant_Name --
8889 ---------------------------
8890
8891 procedure Set_Discriminant_Name (Id : Node_Id) is
8892 Disc : Entity_Id;
8893
8894 begin
8895 Set_Original_Discriminant (Id, Empty);
8896
8897 if Has_Discriminants (Typ) then
8898 Disc := First_Discriminant (Typ);
8899 while Present (Disc) loop
8900 if Chars (Disc) = Chars (Id)
8901 and then Present (Corresponding_Discriminant (Disc))
8902 then
8903 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8904 end if;
8905 Next_Discriminant (Disc);
8906 end loop;
8907 end if;
8908 end Set_Discriminant_Name;
8909
8910 -- Start of processing for Build_Underlying_Full_View
8911
8912 begin
8913 if Nkind (N) = N_Full_Type_Declaration then
8914 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8915
8916 elsif Nkind (N) = N_Subtype_Declaration then
8917 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8918
8919 elsif Nkind (N) = N_Component_Declaration then
8920 Constr :=
8921 New_Copy_Tree
8922 (Constraint (Subtype_Indication (Component_Definition (N))));
8923
8924 else
8925 raise Program_Error;
8926 end if;
8927
8928 C := First (Constraints (Constr));
8929 while Present (C) loop
8930 if Nkind (C) = N_Discriminant_Association then
8931 Id := First (Selector_Names (C));
8932 while Present (Id) loop
8933 Set_Discriminant_Name (Id);
8934 Next (Id);
8935 end loop;
8936 end if;
8937
8938 Next (C);
8939 end loop;
8940
8941 Indic :=
8942 Make_Subtype_Declaration (Loc,
8943 Defining_Identifier => Subt,
8944 Subtype_Indication =>
8945 Make_Subtype_Indication (Loc,
8946 Subtype_Mark => New_Reference_To (Par, Loc),
8947 Constraint => New_Copy_Tree (Constr)));
8948
8949 -- If this is a component subtype for an outer itype, it is not
8950 -- a list member, so simply set the parent link for analysis: if
8951 -- the enclosing type does not need to be in a declarative list,
8952 -- neither do the components.
8953
8954 if Is_List_Member (N)
8955 and then Nkind (N) /= N_Component_Declaration
8956 then
8957 Insert_Before (N, Indic);
8958 else
8959 Set_Parent (Indic, Parent (N));
8960 end if;
8961
8962 Analyze (Indic);
8963 Set_Underlying_Full_View (Typ, Full_View (Subt));
8964 end Build_Underlying_Full_View;
8965
8966 -------------------------------
8967 -- Check_Abstract_Overriding --
8968 -------------------------------
8969
8970 procedure Check_Abstract_Overriding (T : Entity_Id) is
8971 Alias_Subp : Entity_Id;
8972 Elmt : Elmt_Id;
8973 Op_List : Elist_Id;
8974 Subp : Entity_Id;
8975 Type_Def : Node_Id;
8976
8977 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8978 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8979 -- which has pragma Implemented already set. Check whether Subp's entity
8980 -- kind conforms to the implementation kind of the overridden routine.
8981
8982 procedure Check_Pragma_Implemented
8983 (Subp : Entity_Id;
8984 Iface_Subp : Entity_Id);
8985 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8986 -- Iface_Subp and both entities have pragma Implemented already set on
8987 -- them. Check whether the two implementation kinds are conforming.
8988
8989 procedure Inherit_Pragma_Implemented
8990 (Subp : Entity_Id;
8991 Iface_Subp : Entity_Id);
8992 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8993 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8994 -- Propagate the implementation kind of Iface_Subp to Subp.
8995
8996 ------------------------------
8997 -- Check_Pragma_Implemented --
8998 ------------------------------
8999
9000 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9001 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9002 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9003 Subp_Alias : constant Entity_Id := Alias (Subp);
9004 Contr_Typ : Entity_Id;
9005 Impl_Subp : Entity_Id;
9006
9007 begin
9008 -- Subp must have an alias since it is a hidden entity used to link
9009 -- an interface subprogram to its overriding counterpart.
9010
9011 pragma Assert (Present (Subp_Alias));
9012
9013 -- Handle aliases to synchronized wrappers
9014
9015 Impl_Subp := Subp_Alias;
9016
9017 if Is_Primitive_Wrapper (Impl_Subp) then
9018 Impl_Subp := Wrapped_Entity (Impl_Subp);
9019 end if;
9020
9021 -- Extract the type of the controlling formal
9022
9023 Contr_Typ := Etype (First_Formal (Subp_Alias));
9024
9025 if Is_Concurrent_Record_Type (Contr_Typ) then
9026 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9027 end if;
9028
9029 -- An interface subprogram whose implementation kind is By_Entry must
9030 -- be implemented by an entry.
9031
9032 if Impl_Kind = Name_By_Entry
9033 and then Ekind (Impl_Subp) /= E_Entry
9034 then
9035 Error_Msg_Node_2 := Iface_Alias;
9036 Error_Msg_NE
9037 ("type & must implement abstract subprogram & with an entry",
9038 Subp_Alias, Contr_Typ);
9039
9040 elsif Impl_Kind = Name_By_Protected_Procedure then
9041
9042 -- An interface subprogram whose implementation kind is By_
9043 -- Protected_Procedure cannot be implemented by a primitive
9044 -- procedure of a task type.
9045
9046 if Ekind (Contr_Typ) /= E_Protected_Type then
9047 Error_Msg_Node_2 := Contr_Typ;
9048 Error_Msg_NE
9049 ("interface subprogram & cannot be implemented by a " &
9050 "primitive procedure of task type &", Subp_Alias,
9051 Iface_Alias);
9052
9053 -- An interface subprogram whose implementation kind is By_
9054 -- Protected_Procedure must be implemented by a procedure.
9055
9056 elsif Ekind (Impl_Subp) /= E_Procedure then
9057 Error_Msg_Node_2 := Iface_Alias;
9058 Error_Msg_NE
9059 ("type & must implement abstract subprogram & with a " &
9060 "procedure", Subp_Alias, Contr_Typ);
9061 end if;
9062 end if;
9063 end Check_Pragma_Implemented;
9064
9065 ------------------------------
9066 -- Check_Pragma_Implemented --
9067 ------------------------------
9068
9069 procedure Check_Pragma_Implemented
9070 (Subp : Entity_Id;
9071 Iface_Subp : Entity_Id)
9072 is
9073 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9074 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9075
9076 begin
9077 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9078 -- and overriding subprogram are different. In general this is an
9079 -- error except when the implementation kind of the overridden
9080 -- subprograms is By_Any or Optional.
9081
9082 if Iface_Kind /= Subp_Kind
9083 and then Iface_Kind /= Name_By_Any
9084 and then Iface_Kind /= Name_Optional
9085 then
9086 if Iface_Kind = Name_By_Entry then
9087 Error_Msg_N
9088 ("incompatible implementation kind, overridden subprogram " &
9089 "is marked By_Entry", Subp);
9090 else
9091 Error_Msg_N
9092 ("incompatible implementation kind, overridden subprogram " &
9093 "is marked By_Protected_Procedure", Subp);
9094 end if;
9095 end if;
9096 end Check_Pragma_Implemented;
9097
9098 --------------------------------
9099 -- Inherit_Pragma_Implemented --
9100 --------------------------------
9101
9102 procedure Inherit_Pragma_Implemented
9103 (Subp : Entity_Id;
9104 Iface_Subp : Entity_Id)
9105 is
9106 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9107 Loc : constant Source_Ptr := Sloc (Subp);
9108 Impl_Prag : Node_Id;
9109
9110 begin
9111 -- Since the implementation kind is stored as a representation item
9112 -- rather than a flag, create a pragma node.
9113
9114 Impl_Prag :=
9115 Make_Pragma (Loc,
9116 Chars => Name_Implemented,
9117 Pragma_Argument_Associations => New_List (
9118 Make_Pragma_Argument_Association (Loc,
9119 Expression => New_Reference_To (Subp, Loc)),
9120
9121 Make_Pragma_Argument_Association (Loc,
9122 Expression => Make_Identifier (Loc, Iface_Kind))));
9123
9124 -- The pragma doesn't need to be analyzed because it is internally
9125 -- built. It is safe to directly register it as a rep item since we
9126 -- are only interested in the characters of the implementation kind.
9127
9128 Record_Rep_Item (Subp, Impl_Prag);
9129 end Inherit_Pragma_Implemented;
9130
9131 -- Start of processing for Check_Abstract_Overriding
9132
9133 begin
9134 Op_List := Primitive_Operations (T);
9135
9136 -- Loop to check primitive operations
9137
9138 Elmt := First_Elmt (Op_List);
9139 while Present (Elmt) loop
9140 Subp := Node (Elmt);
9141 Alias_Subp := Alias (Subp);
9142
9143 -- Inherited subprograms are identified by the fact that they do not
9144 -- come from source, and the associated source location is the
9145 -- location of the first subtype of the derived type.
9146
9147 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9148 -- subprograms that "require overriding".
9149
9150 -- Special exception, do not complain about failure to override the
9151 -- stream routines _Input and _Output, as well as the primitive
9152 -- operations used in dispatching selects since we always provide
9153 -- automatic overridings for these subprograms.
9154
9155 -- Also ignore this rule for convention CIL since .NET libraries
9156 -- do bizarre things with interfaces???
9157
9158 -- The partial view of T may have been a private extension, for
9159 -- which inherited functions dispatching on result are abstract.
9160 -- If the full view is a null extension, there is no need for
9161 -- overriding in Ada 2005, but wrappers need to be built for them
9162 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9163
9164 if Is_Null_Extension (T)
9165 and then Has_Controlling_Result (Subp)
9166 and then Ada_Version >= Ada_2005
9167 and then Present (Alias_Subp)
9168 and then not Comes_From_Source (Subp)
9169 and then not Is_Abstract_Subprogram (Alias_Subp)
9170 and then not Is_Access_Type (Etype (Subp))
9171 then
9172 null;
9173
9174 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9175 -- processing because this check is done with the aliased
9176 -- entity
9177
9178 elsif Present (Interface_Alias (Subp)) then
9179 null;
9180
9181 elsif (Is_Abstract_Subprogram (Subp)
9182 or else Requires_Overriding (Subp)
9183 or else
9184 (Has_Controlling_Result (Subp)
9185 and then Present (Alias_Subp)
9186 and then not Comes_From_Source (Subp)
9187 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9188 and then not Is_TSS (Subp, TSS_Stream_Input)
9189 and then not Is_TSS (Subp, TSS_Stream_Output)
9190 and then not Is_Abstract_Type (T)
9191 and then Convention (T) /= Convention_CIL
9192 and then not Is_Predefined_Interface_Primitive (Subp)
9193
9194 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9195 -- with abstract interface types because the check will be done
9196 -- with the aliased entity (otherwise we generate a duplicated
9197 -- error message).
9198
9199 and then not Present (Interface_Alias (Subp))
9200 then
9201 if Present (Alias_Subp) then
9202
9203 -- Only perform the check for a derived subprogram when the
9204 -- type has an explicit record extension. This avoids incorrect
9205 -- flagging of abstract subprograms for the case of a type
9206 -- without an extension that is derived from a formal type
9207 -- with a tagged actual (can occur within a private part).
9208
9209 -- Ada 2005 (AI-391): In the case of an inherited function with
9210 -- a controlling result of the type, the rule does not apply if
9211 -- the type is a null extension (unless the parent function
9212 -- itself is abstract, in which case the function must still be
9213 -- be overridden). The expander will generate an overriding
9214 -- wrapper function calling the parent subprogram (see
9215 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9216
9217 Type_Def := Type_Definition (Parent (T));
9218
9219 if Nkind (Type_Def) = N_Derived_Type_Definition
9220 and then Present (Record_Extension_Part (Type_Def))
9221 and then
9222 (Ada_Version < Ada_2005
9223 or else not Is_Null_Extension (T)
9224 or else Ekind (Subp) = E_Procedure
9225 or else not Has_Controlling_Result (Subp)
9226 or else Is_Abstract_Subprogram (Alias_Subp)
9227 or else Requires_Overriding (Subp)
9228 or else Is_Access_Type (Etype (Subp)))
9229 then
9230 -- Avoid reporting error in case of abstract predefined
9231 -- primitive inherited from interface type because the
9232 -- body of internally generated predefined primitives
9233 -- of tagged types are generated later by Freeze_Type
9234
9235 if Is_Interface (Root_Type (T))
9236 and then Is_Abstract_Subprogram (Subp)
9237 and then Is_Predefined_Dispatching_Operation (Subp)
9238 and then not Comes_From_Source (Ultimate_Alias (Subp))
9239 then
9240 null;
9241
9242 else
9243 Error_Msg_NE
9244 ("type must be declared abstract or & overridden",
9245 T, Subp);
9246
9247 -- Traverse the whole chain of aliased subprograms to
9248 -- complete the error notification. This is especially
9249 -- useful for traceability of the chain of entities when
9250 -- the subprogram corresponds with an interface
9251 -- subprogram (which may be defined in another package).
9252
9253 if Present (Alias_Subp) then
9254 declare
9255 E : Entity_Id;
9256
9257 begin
9258 E := Subp;
9259 while Present (Alias (E)) loop
9260
9261 -- Avoid reporting redundant errors on entities
9262 -- inherited from interfaces
9263
9264 if Sloc (E) /= Sloc (T) then
9265 Error_Msg_Sloc := Sloc (E);
9266 Error_Msg_NE
9267 ("\& has been inherited #", T, Subp);
9268 end if;
9269
9270 E := Alias (E);
9271 end loop;
9272
9273 Error_Msg_Sloc := Sloc (E);
9274
9275 -- AI05-0068: report if there is an overriding
9276 -- non-abstract subprogram that is invisible.
9277
9278 if Is_Hidden (E)
9279 and then not Is_Abstract_Subprogram (E)
9280 then
9281 Error_Msg_NE
9282 ("\& subprogram# is not visible",
9283 T, Subp);
9284
9285 else
9286 Error_Msg_NE
9287 ("\& has been inherited from subprogram #",
9288 T, Subp);
9289 end if;
9290 end;
9291 end if;
9292 end if;
9293
9294 -- Ada 2005 (AI-345): Protected or task type implementing
9295 -- abstract interfaces.
9296
9297 elsif Is_Concurrent_Record_Type (T)
9298 and then Present (Interfaces (T))
9299 then
9300 -- The controlling formal of Subp must be of mode "out",
9301 -- "in out" or an access-to-variable to be overridden.
9302
9303 if Ekind (First_Formal (Subp)) = E_In_Parameter
9304 and then Ekind (Subp) /= E_Function
9305 then
9306 if not Is_Predefined_Dispatching_Operation (Subp)
9307 and then Is_Protected_Type
9308 (Corresponding_Concurrent_Type (T))
9309 then
9310 Error_Msg_PT (T, Subp);
9311 end if;
9312
9313 -- Some other kind of overriding failure
9314
9315 else
9316 Error_Msg_NE
9317 ("interface subprogram & must be overridden",
9318 T, Subp);
9319
9320 -- Examine primitive operations of synchronized type,
9321 -- to find homonyms that have the wrong profile.
9322
9323 declare
9324 Prim : Entity_Id;
9325
9326 begin
9327 Prim :=
9328 First_Entity (Corresponding_Concurrent_Type (T));
9329 while Present (Prim) loop
9330 if Chars (Prim) = Chars (Subp) then
9331 Error_Msg_NE
9332 ("profile is not type conformant with "
9333 & "prefixed view profile of "
9334 & "inherited operation&", Prim, Subp);
9335 end if;
9336
9337 Next_Entity (Prim);
9338 end loop;
9339 end;
9340 end if;
9341 end if;
9342
9343 else
9344 Error_Msg_Node_2 := T;
9345 Error_Msg_N
9346 ("abstract subprogram& not allowed for type&", Subp);
9347
9348 -- Also post unconditional warning on the type (unconditional
9349 -- so that if there are more than one of these cases, we get
9350 -- them all, and not just the first one).
9351
9352 Error_Msg_Node_2 := Subp;
9353 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9354 end if;
9355 end if;
9356
9357 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9358 -- Implemented
9359
9360 -- Subp is an expander-generated procedure which maps an interface
9361 -- alias to a protected wrapper. The interface alias is flagged by
9362 -- pragma Implemented. Ensure that Subp is a procedure when the
9363 -- implementation kind is By_Protected_Procedure or an entry when
9364 -- By_Entry.
9365
9366 if Ada_Version >= Ada_2012
9367 and then Is_Hidden (Subp)
9368 and then Present (Interface_Alias (Subp))
9369 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9370 then
9371 Check_Pragma_Implemented (Subp);
9372 end if;
9373
9374 -- Subp is an interface primitive which overrides another interface
9375 -- primitive marked with pragma Implemented.
9376
9377 if Ada_Version >= Ada_2012
9378 and then Present (Overridden_Operation (Subp))
9379 and then Has_Rep_Pragma
9380 (Overridden_Operation (Subp), Name_Implemented)
9381 then
9382 -- If the overriding routine is also marked by Implemented, check
9383 -- that the two implementation kinds are conforming.
9384
9385 if Has_Rep_Pragma (Subp, Name_Implemented) then
9386 Check_Pragma_Implemented
9387 (Subp => Subp,
9388 Iface_Subp => Overridden_Operation (Subp));
9389
9390 -- Otherwise the overriding routine inherits the implementation
9391 -- kind from the overridden subprogram.
9392
9393 else
9394 Inherit_Pragma_Implemented
9395 (Subp => Subp,
9396 Iface_Subp => Overridden_Operation (Subp));
9397 end if;
9398 end if;
9399
9400 Next_Elmt (Elmt);
9401 end loop;
9402 end Check_Abstract_Overriding;
9403
9404 ------------------------------------------------
9405 -- Check_Access_Discriminant_Requires_Limited --
9406 ------------------------------------------------
9407
9408 procedure Check_Access_Discriminant_Requires_Limited
9409 (D : Node_Id;
9410 Loc : Node_Id)
9411 is
9412 begin
9413 -- A discriminant_specification for an access discriminant shall appear
9414 -- only in the declaration for a task or protected type, or for a type
9415 -- with the reserved word 'limited' in its definition or in one of its
9416 -- ancestors (RM 3.7(10)).
9417
9418 -- AI-0063: The proper condition is that type must be immutably limited,
9419 -- or else be a partial view.
9420
9421 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9422 if Is_Immutably_Limited_Type (Current_Scope)
9423 or else
9424 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9425 and then Limited_Present (Parent (Current_Scope)))
9426 then
9427 null;
9428
9429 else
9430 Error_Msg_N
9431 ("access discriminants allowed only for limited types", Loc);
9432 end if;
9433 end if;
9434 end Check_Access_Discriminant_Requires_Limited;
9435
9436 -----------------------------------
9437 -- Check_Aliased_Component_Types --
9438 -----------------------------------
9439
9440 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9441 C : Entity_Id;
9442
9443 begin
9444 -- ??? Also need to check components of record extensions, but not
9445 -- components of protected types (which are always limited).
9446
9447 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9448 -- types to be unconstrained. This is safe because it is illegal to
9449 -- create access subtypes to such types with explicit discriminant
9450 -- constraints.
9451
9452 if not Is_Limited_Type (T) then
9453 if Ekind (T) = E_Record_Type then
9454 C := First_Component (T);
9455 while Present (C) loop
9456 if Is_Aliased (C)
9457 and then Has_Discriminants (Etype (C))
9458 and then not Is_Constrained (Etype (C))
9459 and then not In_Instance_Body
9460 and then Ada_Version < Ada_2005
9461 then
9462 Error_Msg_N
9463 ("aliased component must be constrained (RM 3.6(11))",
9464 C);
9465 end if;
9466
9467 Next_Component (C);
9468 end loop;
9469
9470 elsif Ekind (T) = E_Array_Type then
9471 if Has_Aliased_Components (T)
9472 and then Has_Discriminants (Component_Type (T))
9473 and then not Is_Constrained (Component_Type (T))
9474 and then not In_Instance_Body
9475 and then Ada_Version < Ada_2005
9476 then
9477 Error_Msg_N
9478 ("aliased component type must be constrained (RM 3.6(11))",
9479 T);
9480 end if;
9481 end if;
9482 end if;
9483 end Check_Aliased_Component_Types;
9484
9485 ----------------------
9486 -- Check_Completion --
9487 ----------------------
9488
9489 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9490 E : Entity_Id;
9491
9492 procedure Post_Error;
9493 -- Post error message for lack of completion for entity E
9494
9495 ----------------
9496 -- Post_Error --
9497 ----------------
9498
9499 procedure Post_Error is
9500
9501 procedure Missing_Body;
9502 -- Output missing body message
9503
9504 ------------------
9505 -- Missing_Body --
9506 ------------------
9507
9508 procedure Missing_Body is
9509 begin
9510 -- Spec is in same unit, so we can post on spec
9511
9512 if In_Same_Source_Unit (Body_Id, E) then
9513 Error_Msg_N ("missing body for &", E);
9514
9515 -- Spec is in a separate unit, so we have to post on the body
9516
9517 else
9518 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9519 end if;
9520 end Missing_Body;
9521
9522 -- Start of processing for Post_Error
9523
9524 begin
9525 if not Comes_From_Source (E) then
9526
9527 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9528 -- It may be an anonymous protected type created for a
9529 -- single variable. Post error on variable, if present.
9530
9531 declare
9532 Var : Entity_Id;
9533
9534 begin
9535 Var := First_Entity (Current_Scope);
9536 while Present (Var) loop
9537 exit when Etype (Var) = E
9538 and then Comes_From_Source (Var);
9539
9540 Next_Entity (Var);
9541 end loop;
9542
9543 if Present (Var) then
9544 E := Var;
9545 end if;
9546 end;
9547 end if;
9548 end if;
9549
9550 -- If a generated entity has no completion, then either previous
9551 -- semantic errors have disabled the expansion phase, or else we had
9552 -- missing subunits, or else we are compiling without expansion,
9553 -- or else something is very wrong.
9554
9555 if not Comes_From_Source (E) then
9556 pragma Assert
9557 (Serious_Errors_Detected > 0
9558 or else Configurable_Run_Time_Violations > 0
9559 or else Subunits_Missing
9560 or else not Expander_Active);
9561 return;
9562
9563 -- Here for source entity
9564
9565 else
9566 -- Here if no body to post the error message, so we post the error
9567 -- on the declaration that has no completion. This is not really
9568 -- the right place to post it, think about this later ???
9569
9570 if No (Body_Id) then
9571 if Is_Type (E) then
9572 Error_Msg_NE
9573 ("missing full declaration for }", Parent (E), E);
9574 else
9575 Error_Msg_NE ("missing body for &", Parent (E), E);
9576 end if;
9577
9578 -- Package body has no completion for a declaration that appears
9579 -- in the corresponding spec. Post error on the body, with a
9580 -- reference to the non-completed declaration.
9581
9582 else
9583 Error_Msg_Sloc := Sloc (E);
9584
9585 if Is_Type (E) then
9586 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9587
9588 elsif Is_Overloadable (E)
9589 and then Current_Entity_In_Scope (E) /= E
9590 then
9591 -- It may be that the completion is mistyped and appears as
9592 -- a distinct overloading of the entity.
9593
9594 declare
9595 Candidate : constant Entity_Id :=
9596 Current_Entity_In_Scope (E);
9597 Decl : constant Node_Id :=
9598 Unit_Declaration_Node (Candidate);
9599
9600 begin
9601 if Is_Overloadable (Candidate)
9602 and then Ekind (Candidate) = Ekind (E)
9603 and then Nkind (Decl) = N_Subprogram_Body
9604 and then Acts_As_Spec (Decl)
9605 then
9606 Check_Type_Conformant (Candidate, E);
9607
9608 else
9609 Missing_Body;
9610 end if;
9611 end;
9612
9613 else
9614 Missing_Body;
9615 end if;
9616 end if;
9617 end if;
9618 end Post_Error;
9619
9620 -- Start of processing for Check_Completion
9621
9622 begin
9623 E := First_Entity (Current_Scope);
9624 while Present (E) loop
9625 if Is_Intrinsic_Subprogram (E) then
9626 null;
9627
9628 -- The following situation requires special handling: a child unit
9629 -- that appears in the context clause of the body of its parent:
9630
9631 -- procedure Parent.Child (...);
9632
9633 -- with Parent.Child;
9634 -- package body Parent is
9635
9636 -- Here Parent.Child appears as a local entity, but should not be
9637 -- flagged as requiring completion, because it is a compilation
9638 -- unit.
9639
9640 -- Ignore missing completion for a subprogram that does not come from
9641 -- source (including the _Call primitive operation of RAS types,
9642 -- which has to have the flag Comes_From_Source for other purposes):
9643 -- we assume that the expander will provide the missing completion.
9644 -- In case of previous errors, other expansion actions that provide
9645 -- bodies for null procedures with not be invoked, so inhibit message
9646 -- in those cases.
9647
9648 -- Note that E_Operator is not in the list that follows, because
9649 -- this kind is reserved for predefined operators, that are
9650 -- intrinsic and do not need completion.
9651
9652 elsif Ekind (E) = E_Function
9653 or else Ekind (E) = E_Procedure
9654 or else Ekind (E) = E_Generic_Function
9655 or else Ekind (E) = E_Generic_Procedure
9656 then
9657 if Has_Completion (E) then
9658 null;
9659
9660 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9661 null;
9662
9663 elsif Is_Subprogram (E)
9664 and then (not Comes_From_Source (E)
9665 or else Chars (E) = Name_uCall)
9666 then
9667 null;
9668
9669 elsif
9670 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9671 then
9672 null;
9673
9674 elsif Nkind (Parent (E)) = N_Procedure_Specification
9675 and then Null_Present (Parent (E))
9676 and then Serious_Errors_Detected > 0
9677 then
9678 null;
9679
9680 else
9681 Post_Error;
9682 end if;
9683
9684 elsif Is_Entry (E) then
9685 if not Has_Completion (E) and then
9686 (Ekind (Scope (E)) = E_Protected_Object
9687 or else Ekind (Scope (E)) = E_Protected_Type)
9688 then
9689 Post_Error;
9690 end if;
9691
9692 elsif Is_Package_Or_Generic_Package (E) then
9693 if Unit_Requires_Body (E) then
9694 if not Has_Completion (E)
9695 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9696 N_Compilation_Unit
9697 then
9698 Post_Error;
9699 end if;
9700
9701 elsif not Is_Child_Unit (E) then
9702 May_Need_Implicit_Body (E);
9703 end if;
9704
9705 -- A formal incomplete type (Ada 2012) does not require a completion;
9706 -- other incomplete type declarations do.
9707
9708 elsif Ekind (E) = E_Incomplete_Type
9709 and then No (Underlying_Type (E))
9710 and then not Is_Generic_Type (E)
9711 then
9712 Post_Error;
9713
9714 elsif (Ekind (E) = E_Task_Type or else
9715 Ekind (E) = E_Protected_Type)
9716 and then not Has_Completion (E)
9717 then
9718 Post_Error;
9719
9720 -- A single task declared in the current scope is a constant, verify
9721 -- that the body of its anonymous type is in the same scope. If the
9722 -- task is defined elsewhere, this may be a renaming declaration for
9723 -- which no completion is needed.
9724
9725 elsif Ekind (E) = E_Constant
9726 and then Ekind (Etype (E)) = E_Task_Type
9727 and then not Has_Completion (Etype (E))
9728 and then Scope (Etype (E)) = Current_Scope
9729 then
9730 Post_Error;
9731
9732 elsif Ekind (E) = E_Protected_Object
9733 and then not Has_Completion (Etype (E))
9734 then
9735 Post_Error;
9736
9737 elsif Ekind (E) = E_Record_Type then
9738 if Is_Tagged_Type (E) then
9739 Check_Abstract_Overriding (E);
9740 Check_Conventions (E);
9741 end if;
9742
9743 Check_Aliased_Component_Types (E);
9744
9745 elsif Ekind (E) = E_Array_Type then
9746 Check_Aliased_Component_Types (E);
9747
9748 end if;
9749
9750 Next_Entity (E);
9751 end loop;
9752 end Check_Completion;
9753
9754 ------------------------------------
9755 -- Check_CPP_Type_Has_No_Defaults --
9756 ------------------------------------
9757
9758 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
9759 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
9760 Clist : Node_Id;
9761 Comp : Node_Id;
9762
9763 begin
9764 -- Obtain the component list
9765
9766 if Nkind (Tdef) = N_Record_Definition then
9767 Clist := Component_List (Tdef);
9768 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
9769 Clist := Component_List (Record_Extension_Part (Tdef));
9770 end if;
9771
9772 -- Check all components to ensure no default expressions
9773
9774 if Present (Clist) then
9775 Comp := First (Component_Items (Clist));
9776 while Present (Comp) loop
9777 if Present (Expression (Comp)) then
9778 Error_Msg_N
9779 ("component of imported 'C'P'P type cannot have "
9780 & "default expression", Expression (Comp));
9781 end if;
9782
9783 Next (Comp);
9784 end loop;
9785 end if;
9786 end Check_CPP_Type_Has_No_Defaults;
9787
9788 ----------------------------
9789 -- Check_Delta_Expression --
9790 ----------------------------
9791
9792 procedure Check_Delta_Expression (E : Node_Id) is
9793 begin
9794 if not (Is_Real_Type (Etype (E))) then
9795 Wrong_Type (E, Any_Real);
9796
9797 elsif not Is_OK_Static_Expression (E) then
9798 Flag_Non_Static_Expr
9799 ("non-static expression used for delta value!", E);
9800
9801 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9802 Error_Msg_N ("delta expression must be positive", E);
9803
9804 else
9805 return;
9806 end if;
9807
9808 -- If any of above errors occurred, then replace the incorrect
9809 -- expression by the real 0.1, which should prevent further errors.
9810
9811 Rewrite (E,
9812 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9813 Analyze_And_Resolve (E, Standard_Float);
9814 end Check_Delta_Expression;
9815
9816 -----------------------------
9817 -- Check_Digits_Expression --
9818 -----------------------------
9819
9820 procedure Check_Digits_Expression (E : Node_Id) is
9821 begin
9822 if not (Is_Integer_Type (Etype (E))) then
9823 Wrong_Type (E, Any_Integer);
9824
9825 elsif not Is_OK_Static_Expression (E) then
9826 Flag_Non_Static_Expr
9827 ("non-static expression used for digits value!", E);
9828
9829 elsif Expr_Value (E) <= 0 then
9830 Error_Msg_N ("digits value must be greater than zero", E);
9831
9832 else
9833 return;
9834 end if;
9835
9836 -- If any of above errors occurred, then replace the incorrect
9837 -- expression by the integer 1, which should prevent further errors.
9838
9839 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9840 Analyze_And_Resolve (E, Standard_Integer);
9841
9842 end Check_Digits_Expression;
9843
9844 --------------------------
9845 -- Check_Initialization --
9846 --------------------------
9847
9848 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9849 begin
9850 if Is_Limited_Type (T)
9851 and then not In_Instance
9852 and then not In_Inlined_Body
9853 then
9854 if not OK_For_Limited_Init (T, Exp) then
9855
9856 -- In GNAT mode, this is just a warning, to allow it to be evilly
9857 -- turned off. Otherwise it is a real error.
9858
9859 if GNAT_Mode then
9860 Error_Msg_N
9861 ("?cannot initialize entities of limited type!", Exp);
9862
9863 elsif Ada_Version < Ada_2005 then
9864
9865 -- The side effect removal machinery may generate illegal Ada
9866 -- code to avoid the usage of access types and 'reference in
9867 -- SPARK mode. Since this is legal code with respect to theorem
9868 -- proving, do not emit the error.
9869
9870 if SPARK_Mode
9871 and then Nkind (Exp) = N_Function_Call
9872 and then Nkind (Parent (Exp)) = N_Object_Declaration
9873 and then not Comes_From_Source
9874 (Defining_Identifier (Parent (Exp)))
9875 then
9876 null;
9877
9878 else
9879 Error_Msg_N
9880 ("cannot initialize entities of limited type", Exp);
9881 Explain_Limited_Type (T, Exp);
9882 end if;
9883
9884 else
9885 -- Specialize error message according to kind of illegal
9886 -- initial expression.
9887
9888 if Nkind (Exp) = N_Type_Conversion
9889 and then Nkind (Expression (Exp)) = N_Function_Call
9890 then
9891 Error_Msg_N
9892 ("illegal context for call"
9893 & " to function with limited result", Exp);
9894
9895 else
9896 Error_Msg_N
9897 ("initialization of limited object requires aggregate "
9898 & "or function call", Exp);
9899 end if;
9900 end if;
9901 end if;
9902 end if;
9903 end Check_Initialization;
9904
9905 ----------------------
9906 -- Check_Interfaces --
9907 ----------------------
9908
9909 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9910 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9911
9912 Iface : Node_Id;
9913 Iface_Def : Node_Id;
9914 Iface_Typ : Entity_Id;
9915 Parent_Node : Node_Id;
9916
9917 Is_Task : Boolean := False;
9918 -- Set True if parent type or any progenitor is a task interface
9919
9920 Is_Protected : Boolean := False;
9921 -- Set True if parent type or any progenitor is a protected interface
9922
9923 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9924 -- Check that a progenitor is compatible with declaration.
9925 -- Error is posted on Error_Node.
9926
9927 ------------------
9928 -- Check_Ifaces --
9929 ------------------
9930
9931 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9932 Iface_Id : constant Entity_Id :=
9933 Defining_Identifier (Parent (Iface_Def));
9934 Type_Def : Node_Id;
9935
9936 begin
9937 if Nkind (N) = N_Private_Extension_Declaration then
9938 Type_Def := N;
9939 else
9940 Type_Def := Type_Definition (N);
9941 end if;
9942
9943 if Is_Task_Interface (Iface_Id) then
9944 Is_Task := True;
9945
9946 elsif Is_Protected_Interface (Iface_Id) then
9947 Is_Protected := True;
9948 end if;
9949
9950 if Is_Synchronized_Interface (Iface_Id) then
9951
9952 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9953 -- extension derived from a synchronized interface must explicitly
9954 -- be declared synchronized, because the full view will be a
9955 -- synchronized type.
9956
9957 if Nkind (N) = N_Private_Extension_Declaration then
9958 if not Synchronized_Present (N) then
9959 Error_Msg_NE
9960 ("private extension of& must be explicitly synchronized",
9961 N, Iface_Id);
9962 end if;
9963
9964 -- However, by 3.9.4(16/2), a full type that is a record extension
9965 -- is never allowed to derive from a synchronized interface (note
9966 -- that interfaces must be excluded from this check, because those
9967 -- are represented by derived type definitions in some cases).
9968
9969 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9970 and then not Interface_Present (Type_Definition (N))
9971 then
9972 Error_Msg_N ("record extension cannot derive from synchronized"
9973 & " interface", Error_Node);
9974 end if;
9975 end if;
9976
9977 -- Check that the characteristics of the progenitor are compatible
9978 -- with the explicit qualifier in the declaration.
9979 -- The check only applies to qualifiers that come from source.
9980 -- Limited_Present also appears in the declaration of corresponding
9981 -- records, and the check does not apply to them.
9982
9983 if Limited_Present (Type_Def)
9984 and then not
9985 Is_Concurrent_Record_Type (Defining_Identifier (N))
9986 then
9987 if Is_Limited_Interface (Parent_Type)
9988 and then not Is_Limited_Interface (Iface_Id)
9989 then
9990 Error_Msg_NE
9991 ("progenitor& must be limited interface",
9992 Error_Node, Iface_Id);
9993
9994 elsif
9995 (Task_Present (Iface_Def)
9996 or else Protected_Present (Iface_Def)
9997 or else Synchronized_Present (Iface_Def))
9998 and then Nkind (N) /= N_Private_Extension_Declaration
9999 and then not Error_Posted (N)
10000 then
10001 Error_Msg_NE
10002 ("progenitor& must be limited interface",
10003 Error_Node, Iface_Id);
10004 end if;
10005
10006 -- Protected interfaces can only inherit from limited, synchronized
10007 -- or protected interfaces.
10008
10009 elsif Nkind (N) = N_Full_Type_Declaration
10010 and then Protected_Present (Type_Def)
10011 then
10012 if Limited_Present (Iface_Def)
10013 or else Synchronized_Present (Iface_Def)
10014 or else Protected_Present (Iface_Def)
10015 then
10016 null;
10017
10018 elsif Task_Present (Iface_Def) then
10019 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10020 & " from task interface", Error_Node);
10021
10022 else
10023 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10024 & " from non-limited interface", Error_Node);
10025 end if;
10026
10027 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
10028 -- limited and synchronized.
10029
10030 elsif Synchronized_Present (Type_Def) then
10031 if Limited_Present (Iface_Def)
10032 or else Synchronized_Present (Iface_Def)
10033 then
10034 null;
10035
10036 elsif Protected_Present (Iface_Def)
10037 and then Nkind (N) /= N_Private_Extension_Declaration
10038 then
10039 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10040 & " from protected interface", Error_Node);
10041
10042 elsif Task_Present (Iface_Def)
10043 and then Nkind (N) /= N_Private_Extension_Declaration
10044 then
10045 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10046 & " from task interface", Error_Node);
10047
10048 elsif not Is_Limited_Interface (Iface_Id) then
10049 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10050 & " from non-limited interface", Error_Node);
10051 end if;
10052
10053 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
10054 -- synchronized or task interfaces.
10055
10056 elsif Nkind (N) = N_Full_Type_Declaration
10057 and then Task_Present (Type_Def)
10058 then
10059 if Limited_Present (Iface_Def)
10060 or else Synchronized_Present (Iface_Def)
10061 or else Task_Present (Iface_Def)
10062 then
10063 null;
10064
10065 elsif Protected_Present (Iface_Def) then
10066 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10067 & " protected interface", Error_Node);
10068
10069 else
10070 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10071 & " non-limited interface", Error_Node);
10072 end if;
10073 end if;
10074 end Check_Ifaces;
10075
10076 -- Start of processing for Check_Interfaces
10077
10078 begin
10079 if Is_Interface (Parent_Type) then
10080 if Is_Task_Interface (Parent_Type) then
10081 Is_Task := True;
10082
10083 elsif Is_Protected_Interface (Parent_Type) then
10084 Is_Protected := True;
10085 end if;
10086 end if;
10087
10088 if Nkind (N) = N_Private_Extension_Declaration then
10089
10090 -- Check that progenitors are compatible with declaration
10091
10092 Iface := First (Interface_List (Def));
10093 while Present (Iface) loop
10094 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10095
10096 Parent_Node := Parent (Base_Type (Iface_Typ));
10097 Iface_Def := Type_Definition (Parent_Node);
10098
10099 if not Is_Interface (Iface_Typ) then
10100 Diagnose_Interface (Iface, Iface_Typ);
10101
10102 else
10103 Check_Ifaces (Iface_Def, Iface);
10104 end if;
10105
10106 Next (Iface);
10107 end loop;
10108
10109 if Is_Task and Is_Protected then
10110 Error_Msg_N
10111 ("type cannot derive from task and protected interface", N);
10112 end if;
10113
10114 return;
10115 end if;
10116
10117 -- Full type declaration of derived type.
10118 -- Check compatibility with parent if it is interface type
10119
10120 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
10121 and then Is_Interface (Parent_Type)
10122 then
10123 Parent_Node := Parent (Parent_Type);
10124
10125 -- More detailed checks for interface varieties
10126
10127 Check_Ifaces
10128 (Iface_Def => Type_Definition (Parent_Node),
10129 Error_Node => Subtype_Indication (Type_Definition (N)));
10130 end if;
10131
10132 Iface := First (Interface_List (Def));
10133 while Present (Iface) loop
10134 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10135
10136 Parent_Node := Parent (Base_Type (Iface_Typ));
10137 Iface_Def := Type_Definition (Parent_Node);
10138
10139 if not Is_Interface (Iface_Typ) then
10140 Diagnose_Interface (Iface, Iface_Typ);
10141
10142 else
10143 -- "The declaration of a specific descendant of an interface
10144 -- type freezes the interface type" RM 13.14
10145
10146 Freeze_Before (N, Iface_Typ);
10147 Check_Ifaces (Iface_Def, Error_Node => Iface);
10148 end if;
10149
10150 Next (Iface);
10151 end loop;
10152
10153 if Is_Task and Is_Protected then
10154 Error_Msg_N
10155 ("type cannot derive from task and protected interface", N);
10156 end if;
10157 end Check_Interfaces;
10158
10159 ------------------------------------
10160 -- Check_Or_Process_Discriminants --
10161 ------------------------------------
10162
10163 -- If an incomplete or private type declaration was already given for the
10164 -- type, the discriminants may have already been processed if they were
10165 -- present on the incomplete declaration. In this case a full conformance
10166 -- check has been performed in Find_Type_Name, and we then recheck here
10167 -- some properties that can't be checked on the partial view alone.
10168 -- Otherwise we call Process_Discriminants.
10169
10170 procedure Check_Or_Process_Discriminants
10171 (N : Node_Id;
10172 T : Entity_Id;
10173 Prev : Entity_Id := Empty)
10174 is
10175 begin
10176 if Has_Discriminants (T) then
10177
10178 -- Discriminants are already set on T if they were already present
10179 -- on the partial view. Make them visible to component declarations.
10180
10181 declare
10182 D : Entity_Id;
10183 -- Discriminant on T (full view) referencing expr on partial view
10184
10185 Prev_D : Entity_Id;
10186 -- Entity of corresponding discriminant on partial view
10187
10188 New_D : Node_Id;
10189 -- Discriminant specification for full view, expression is the
10190 -- syntactic copy on full view (which has been checked for
10191 -- conformance with partial view), only used here to post error
10192 -- message.
10193
10194 begin
10195 D := First_Discriminant (T);
10196 New_D := First (Discriminant_Specifications (N));
10197 while Present (D) loop
10198 Prev_D := Current_Entity (D);
10199 Set_Current_Entity (D);
10200 Set_Is_Immediately_Visible (D);
10201 Set_Homonym (D, Prev_D);
10202
10203 -- Handle the case where there is an untagged partial view and
10204 -- the full view is tagged: must disallow discriminants with
10205 -- defaults, unless compiling for Ada 2012, which allows a
10206 -- limited tagged type to have defaulted discriminants (see
10207 -- AI05-0214). However, suppress the error here if it was
10208 -- already reported on the default expression of the partial
10209 -- view.
10210
10211 if Is_Tagged_Type (T)
10212 and then Present (Expression (Parent (D)))
10213 and then (not Is_Limited_Type (Current_Scope)
10214 or else Ada_Version < Ada_2012)
10215 and then not Error_Posted (Expression (Parent (D)))
10216 then
10217 if Ada_Version >= Ada_2012 then
10218 Error_Msg_N
10219 ("discriminants of nonlimited tagged type cannot have"
10220 & " defaults",
10221 Expression (New_D));
10222 else
10223 Error_Msg_N
10224 ("discriminants of tagged type cannot have defaults",
10225 Expression (New_D));
10226 end if;
10227 end if;
10228
10229 -- Ada 2005 (AI-230): Access discriminant allowed in
10230 -- non-limited record types.
10231
10232 if Ada_Version < Ada_2005 then
10233
10234 -- This restriction gets applied to the full type here. It
10235 -- has already been applied earlier to the partial view.
10236
10237 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10238 end if;
10239
10240 Next_Discriminant (D);
10241 Next (New_D);
10242 end loop;
10243 end;
10244
10245 elsif Present (Discriminant_Specifications (N)) then
10246 Process_Discriminants (N, Prev);
10247 end if;
10248 end Check_Or_Process_Discriminants;
10249
10250 ----------------------
10251 -- Check_Real_Bound --
10252 ----------------------
10253
10254 procedure Check_Real_Bound (Bound : Node_Id) is
10255 begin
10256 if not Is_Real_Type (Etype (Bound)) then
10257 Error_Msg_N
10258 ("bound in real type definition must be of real type", Bound);
10259
10260 elsif not Is_OK_Static_Expression (Bound) then
10261 Flag_Non_Static_Expr
10262 ("non-static expression used for real type bound!", Bound);
10263
10264 else
10265 return;
10266 end if;
10267
10268 Rewrite
10269 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10270 Analyze (Bound);
10271 Resolve (Bound, Standard_Float);
10272 end Check_Real_Bound;
10273
10274 ------------------------------
10275 -- Complete_Private_Subtype --
10276 ------------------------------
10277
10278 procedure Complete_Private_Subtype
10279 (Priv : Entity_Id;
10280 Full : Entity_Id;
10281 Full_Base : Entity_Id;
10282 Related_Nod : Node_Id)
10283 is
10284 Save_Next_Entity : Entity_Id;
10285 Save_Homonym : Entity_Id;
10286
10287 begin
10288 -- Set semantic attributes for (implicit) private subtype completion.
10289 -- If the full type has no discriminants, then it is a copy of the full
10290 -- view of the base. Otherwise, it is a subtype of the base with a
10291 -- possible discriminant constraint. Save and restore the original
10292 -- Next_Entity field of full to ensure that the calls to Copy_Node
10293 -- do not corrupt the entity chain.
10294
10295 -- Note that the type of the full view is the same entity as the type of
10296 -- the partial view. In this fashion, the subtype has access to the
10297 -- correct view of the parent.
10298
10299 Save_Next_Entity := Next_Entity (Full);
10300 Save_Homonym := Homonym (Priv);
10301
10302 case Ekind (Full_Base) is
10303 when E_Record_Type |
10304 E_Record_Subtype |
10305 Class_Wide_Kind |
10306 Private_Kind |
10307 Task_Kind |
10308 Protected_Kind =>
10309 Copy_Node (Priv, Full);
10310
10311 Set_Has_Discriminants
10312 (Full, Has_Discriminants (Full_Base));
10313 Set_Has_Unknown_Discriminants
10314 (Full, Has_Unknown_Discriminants (Full_Base));
10315 Set_First_Entity (Full, First_Entity (Full_Base));
10316 Set_Last_Entity (Full, Last_Entity (Full_Base));
10317
10318 when others =>
10319 Copy_Node (Full_Base, Full);
10320
10321 Set_Chars (Full, Chars (Priv));
10322 Conditional_Delay (Full, Priv);
10323 Set_Sloc (Full, Sloc (Priv));
10324 end case;
10325
10326 Set_Next_Entity (Full, Save_Next_Entity);
10327 Set_Homonym (Full, Save_Homonym);
10328 Set_Associated_Node_For_Itype (Full, Related_Nod);
10329
10330 -- Set common attributes for all subtypes: kind, convention, etc.
10331
10332 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
10333 Set_Convention (Full, Convention (Full_Base));
10334
10335 -- The Etype of the full view is inconsistent. Gigi needs to see the
10336 -- structural full view, which is what the current scheme gives:
10337 -- the Etype of the full view is the etype of the full base. However,
10338 -- if the full base is a derived type, the full view then looks like
10339 -- a subtype of the parent, not a subtype of the full base. If instead
10340 -- we write:
10341
10342 -- Set_Etype (Full, Full_Base);
10343
10344 -- then we get inconsistencies in the front-end (confusion between
10345 -- views). Several outstanding bugs are related to this ???
10346
10347 Set_Is_First_Subtype (Full, False);
10348 Set_Scope (Full, Scope (Priv));
10349 Set_Size_Info (Full, Full_Base);
10350 Set_RM_Size (Full, RM_Size (Full_Base));
10351 Set_Is_Itype (Full);
10352
10353 -- A subtype of a private-type-without-discriminants, whose full-view
10354 -- has discriminants with default expressions, is not constrained!
10355
10356 if not Has_Discriminants (Priv) then
10357 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10358
10359 if Has_Discriminants (Full_Base) then
10360 Set_Discriminant_Constraint
10361 (Full, Discriminant_Constraint (Full_Base));
10362
10363 -- The partial view may have been indefinite, the full view
10364 -- might not be.
10365
10366 Set_Has_Unknown_Discriminants
10367 (Full, Has_Unknown_Discriminants (Full_Base));
10368 end if;
10369 end if;
10370
10371 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10372 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10373
10374 -- Freeze the private subtype entity if its parent is delayed, and not
10375 -- already frozen. We skip this processing if the type is an anonymous
10376 -- subtype of a record component, or is the corresponding record of a
10377 -- protected type, since ???
10378
10379 if not Is_Type (Scope (Full)) then
10380 Set_Has_Delayed_Freeze (Full,
10381 Has_Delayed_Freeze (Full_Base)
10382 and then (not Is_Frozen (Full_Base)));
10383 end if;
10384
10385 Set_Freeze_Node (Full, Empty);
10386 Set_Is_Frozen (Full, False);
10387 Set_Full_View (Priv, Full);
10388
10389 if Has_Discriminants (Full) then
10390 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10391 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10392
10393 if Has_Unknown_Discriminants (Full) then
10394 Set_Discriminant_Constraint (Full, No_Elist);
10395 end if;
10396 end if;
10397
10398 if Ekind (Full_Base) = E_Record_Type
10399 and then Has_Discriminants (Full_Base)
10400 and then Has_Discriminants (Priv) -- might not, if errors
10401 and then not Has_Unknown_Discriminants (Priv)
10402 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10403 then
10404 Create_Constrained_Components
10405 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10406
10407 -- If the full base is itself derived from private, build a congruent
10408 -- subtype of its underlying type, for use by the back end. For a
10409 -- constrained record component, the declaration cannot be placed on
10410 -- the component list, but it must nevertheless be built an analyzed, to
10411 -- supply enough information for Gigi to compute the size of component.
10412
10413 elsif Ekind (Full_Base) in Private_Kind
10414 and then Is_Derived_Type (Full_Base)
10415 and then Has_Discriminants (Full_Base)
10416 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10417 then
10418 if not Is_Itype (Priv)
10419 and then
10420 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10421 then
10422 Build_Underlying_Full_View
10423 (Parent (Priv), Full, Etype (Full_Base));
10424
10425 elsif Nkind (Related_Nod) = N_Component_Declaration then
10426 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10427 end if;
10428
10429 elsif Is_Record_Type (Full_Base) then
10430
10431 -- Show Full is simply a renaming of Full_Base
10432
10433 Set_Cloned_Subtype (Full, Full_Base);
10434 end if;
10435
10436 -- It is unsafe to share the bounds of a scalar type, because the Itype
10437 -- is elaborated on demand, and if a bound is non-static then different
10438 -- orders of elaboration in different units will lead to different
10439 -- external symbols.
10440
10441 if Is_Scalar_Type (Full_Base) then
10442 Set_Scalar_Range (Full,
10443 Make_Range (Sloc (Related_Nod),
10444 Low_Bound =>
10445 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10446 High_Bound =>
10447 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10448
10449 -- This completion inherits the bounds of the full parent, but if
10450 -- the parent is an unconstrained floating point type, so is the
10451 -- completion.
10452
10453 if Is_Floating_Point_Type (Full_Base) then
10454 Set_Includes_Infinities
10455 (Scalar_Range (Full), Has_Infinities (Full_Base));
10456 end if;
10457 end if;
10458
10459 -- ??? It seems that a lot of fields are missing that should be copied
10460 -- from Full_Base to Full. Here are some that are introduced in a
10461 -- non-disruptive way but a cleanup is necessary.
10462
10463 if Is_Tagged_Type (Full_Base) then
10464 Set_Is_Tagged_Type (Full);
10465 Set_Direct_Primitive_Operations (Full,
10466 Direct_Primitive_Operations (Full_Base));
10467
10468 -- Inherit class_wide type of full_base in case the partial view was
10469 -- not tagged. Otherwise it has already been created when the private
10470 -- subtype was analyzed.
10471
10472 if No (Class_Wide_Type (Full)) then
10473 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10474 end if;
10475
10476 -- If this is a subtype of a protected or task type, constrain its
10477 -- corresponding record, unless this is a subtype without constraints,
10478 -- i.e. a simple renaming as with an actual subtype in an instance.
10479
10480 elsif Is_Concurrent_Type (Full_Base) then
10481 if Has_Discriminants (Full)
10482 and then Present (Corresponding_Record_Type (Full_Base))
10483 and then
10484 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10485 then
10486 Set_Corresponding_Record_Type (Full,
10487 Constrain_Corresponding_Record
10488 (Full, Corresponding_Record_Type (Full_Base),
10489 Related_Nod, Full_Base));
10490
10491 else
10492 Set_Corresponding_Record_Type (Full,
10493 Corresponding_Record_Type (Full_Base));
10494 end if;
10495 end if;
10496
10497 -- Link rep item chain, and also setting of Has_Predicates from private
10498 -- subtype to full subtype, since we will need these on the full subtype
10499 -- to create the predicate function. Note that the full subtype may
10500 -- already have rep items, inherited from the full view of the base
10501 -- type, so we must be sure not to overwrite these entries.
10502
10503 declare
10504 Append : Boolean;
10505 Item : Node_Id;
10506 Next_Item : Node_Id;
10507
10508 begin
10509 Item := First_Rep_Item (Full);
10510
10511 -- If no existing rep items on full type, we can just link directly
10512 -- to the list of items on the private type.
10513
10514 if No (Item) then
10515 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10516
10517 -- Otherwise, search to the end of items currently linked to the full
10518 -- subtype and append the private items to the end. However, if Priv
10519 -- and Full already have the same list of rep items, then the append
10520 -- is not done, as that would create a circularity.
10521
10522 elsif Item /= First_Rep_Item (Priv) then
10523 Append := True;
10524
10525 loop
10526 Next_Item := Next_Rep_Item (Item);
10527 exit when No (Next_Item);
10528 Item := Next_Item;
10529
10530 -- If the private view has aspect specifications, the full view
10531 -- inherits them. Since these aspects may already have been
10532 -- attached to the full view during derivation, do not append
10533 -- them if already present.
10534
10535 if Item = First_Rep_Item (Priv) then
10536 Append := False;
10537 exit;
10538 end if;
10539 end loop;
10540
10541 -- And link the private type items at the end of the chain
10542
10543 if Append then
10544 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10545 end if;
10546 end if;
10547 end;
10548
10549 -- Make sure Has_Predicates is set on full type if it is set on the
10550 -- private type. Note that it may already be set on the full type and
10551 -- if so, we don't want to unset it.
10552
10553 if Has_Predicates (Priv) then
10554 Set_Has_Predicates (Full);
10555 end if;
10556 end Complete_Private_Subtype;
10557
10558 ----------------------------
10559 -- Constant_Redeclaration --
10560 ----------------------------
10561
10562 procedure Constant_Redeclaration
10563 (Id : Entity_Id;
10564 N : Node_Id;
10565 T : out Entity_Id)
10566 is
10567 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10568 Obj_Def : constant Node_Id := Object_Definition (N);
10569 New_T : Entity_Id;
10570
10571 procedure Check_Possible_Deferred_Completion
10572 (Prev_Id : Entity_Id;
10573 Prev_Obj_Def : Node_Id;
10574 Curr_Obj_Def : Node_Id);
10575 -- Determine whether the two object definitions describe the partial
10576 -- and the full view of a constrained deferred constant. Generate
10577 -- a subtype for the full view and verify that it statically matches
10578 -- the subtype of the partial view.
10579
10580 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10581 -- If deferred constant is an access type initialized with an allocator,
10582 -- check whether there is an illegal recursion in the definition,
10583 -- through a default value of some record subcomponent. This is normally
10584 -- detected when generating init procs, but requires this additional
10585 -- mechanism when expansion is disabled.
10586
10587 ----------------------------------------
10588 -- Check_Possible_Deferred_Completion --
10589 ----------------------------------------
10590
10591 procedure Check_Possible_Deferred_Completion
10592 (Prev_Id : Entity_Id;
10593 Prev_Obj_Def : Node_Id;
10594 Curr_Obj_Def : Node_Id)
10595 is
10596 begin
10597 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10598 and then Present (Constraint (Prev_Obj_Def))
10599 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10600 and then Present (Constraint (Curr_Obj_Def))
10601 then
10602 declare
10603 Loc : constant Source_Ptr := Sloc (N);
10604 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10605 Decl : constant Node_Id :=
10606 Make_Subtype_Declaration (Loc,
10607 Defining_Identifier => Def_Id,
10608 Subtype_Indication =>
10609 Relocate_Node (Curr_Obj_Def));
10610
10611 begin
10612 Insert_Before_And_Analyze (N, Decl);
10613 Set_Etype (Id, Def_Id);
10614
10615 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10616 Error_Msg_Sloc := Sloc (Prev_Id);
10617 Error_Msg_N ("subtype does not statically match deferred " &
10618 "declaration#", N);
10619 end if;
10620 end;
10621 end if;
10622 end Check_Possible_Deferred_Completion;
10623
10624 ---------------------------------
10625 -- Check_Recursive_Declaration --
10626 ---------------------------------
10627
10628 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10629 Comp : Entity_Id;
10630
10631 begin
10632 if Is_Record_Type (Typ) then
10633 Comp := First_Component (Typ);
10634 while Present (Comp) loop
10635 if Comes_From_Source (Comp) then
10636 if Present (Expression (Parent (Comp)))
10637 and then Is_Entity_Name (Expression (Parent (Comp)))
10638 and then Entity (Expression (Parent (Comp))) = Prev
10639 then
10640 Error_Msg_Sloc := Sloc (Parent (Comp));
10641 Error_Msg_NE
10642 ("illegal circularity with declaration for&#",
10643 N, Comp);
10644 return;
10645
10646 elsif Is_Record_Type (Etype (Comp)) then
10647 Check_Recursive_Declaration (Etype (Comp));
10648 end if;
10649 end if;
10650
10651 Next_Component (Comp);
10652 end loop;
10653 end if;
10654 end Check_Recursive_Declaration;
10655
10656 -- Start of processing for Constant_Redeclaration
10657
10658 begin
10659 if Nkind (Parent (Prev)) = N_Object_Declaration then
10660 if Nkind (Object_Definition
10661 (Parent (Prev))) = N_Subtype_Indication
10662 then
10663 -- Find type of new declaration. The constraints of the two
10664 -- views must match statically, but there is no point in
10665 -- creating an itype for the full view.
10666
10667 if Nkind (Obj_Def) = N_Subtype_Indication then
10668 Find_Type (Subtype_Mark (Obj_Def));
10669 New_T := Entity (Subtype_Mark (Obj_Def));
10670
10671 else
10672 Find_Type (Obj_Def);
10673 New_T := Entity (Obj_Def);
10674 end if;
10675
10676 T := Etype (Prev);
10677
10678 else
10679 -- The full view may impose a constraint, even if the partial
10680 -- view does not, so construct the subtype.
10681
10682 New_T := Find_Type_Of_Object (Obj_Def, N);
10683 T := New_T;
10684 end if;
10685
10686 else
10687 -- Current declaration is illegal, diagnosed below in Enter_Name
10688
10689 T := Empty;
10690 New_T := Any_Type;
10691 end if;
10692
10693 -- If previous full declaration or a renaming declaration exists, or if
10694 -- a homograph is present, let Enter_Name handle it, either with an
10695 -- error or with the removal of an overridden implicit subprogram.
10696
10697 if Ekind (Prev) /= E_Constant
10698 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10699 or else Present (Expression (Parent (Prev)))
10700 or else Present (Full_View (Prev))
10701 then
10702 Enter_Name (Id);
10703
10704 -- Verify that types of both declarations match, or else that both types
10705 -- are anonymous access types whose designated subtypes statically match
10706 -- (as allowed in Ada 2005 by AI-385).
10707
10708 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10709 and then
10710 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10711 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10712 or else Is_Access_Constant (Etype (New_T)) /=
10713 Is_Access_Constant (Etype (Prev))
10714 or else Can_Never_Be_Null (Etype (New_T)) /=
10715 Can_Never_Be_Null (Etype (Prev))
10716 or else Null_Exclusion_Present (Parent (Prev)) /=
10717 Null_Exclusion_Present (Parent (Id))
10718 or else not Subtypes_Statically_Match
10719 (Designated_Type (Etype (Prev)),
10720 Designated_Type (Etype (New_T))))
10721 then
10722 Error_Msg_Sloc := Sloc (Prev);
10723 Error_Msg_N ("type does not match declaration#", N);
10724 Set_Full_View (Prev, Id);
10725 Set_Etype (Id, Any_Type);
10726
10727 elsif
10728 Null_Exclusion_Present (Parent (Prev))
10729 and then not Null_Exclusion_Present (N)
10730 then
10731 Error_Msg_Sloc := Sloc (Prev);
10732 Error_Msg_N ("null-exclusion does not match declaration#", N);
10733 Set_Full_View (Prev, Id);
10734 Set_Etype (Id, Any_Type);
10735
10736 -- If so, process the full constant declaration
10737
10738 else
10739 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10740 -- the deferred declaration is constrained, then the subtype defined
10741 -- by the subtype_indication in the full declaration shall match it
10742 -- statically.
10743
10744 Check_Possible_Deferred_Completion
10745 (Prev_Id => Prev,
10746 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10747 Curr_Obj_Def => Obj_Def);
10748
10749 Set_Full_View (Prev, Id);
10750 Set_Is_Public (Id, Is_Public (Prev));
10751 Set_Is_Internal (Id);
10752 Append_Entity (Id, Current_Scope);
10753
10754 -- Check ALIASED present if present before (RM 7.4(7))
10755
10756 if Is_Aliased (Prev)
10757 and then not Aliased_Present (N)
10758 then
10759 Error_Msg_Sloc := Sloc (Prev);
10760 Error_Msg_N ("ALIASED required (see declaration#)", N);
10761 end if;
10762
10763 -- Check that placement is in private part and that the incomplete
10764 -- declaration appeared in the visible part.
10765
10766 if Ekind (Current_Scope) = E_Package
10767 and then not In_Private_Part (Current_Scope)
10768 then
10769 Error_Msg_Sloc := Sloc (Prev);
10770 Error_Msg_N
10771 ("full constant for declaration#"
10772 & " must be in private part", N);
10773
10774 elsif Ekind (Current_Scope) = E_Package
10775 and then
10776 List_Containing (Parent (Prev)) /=
10777 Visible_Declarations
10778 (Specification (Unit_Declaration_Node (Current_Scope)))
10779 then
10780 Error_Msg_N
10781 ("deferred constant must be declared in visible part",
10782 Parent (Prev));
10783 end if;
10784
10785 if Is_Access_Type (T)
10786 and then Nkind (Expression (N)) = N_Allocator
10787 then
10788 Check_Recursive_Declaration (Designated_Type (T));
10789 end if;
10790
10791 -- A deferred constant is a visible entity. If type has invariants,
10792 -- verify that the initial value satisfies them.
10793
10794 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
10795 Insert_After (N,
10796 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
10797 end if;
10798 end if;
10799 end Constant_Redeclaration;
10800
10801 ----------------------
10802 -- Constrain_Access --
10803 ----------------------
10804
10805 procedure Constrain_Access
10806 (Def_Id : in out Entity_Id;
10807 S : Node_Id;
10808 Related_Nod : Node_Id)
10809 is
10810 T : constant Entity_Id := Entity (Subtype_Mark (S));
10811 Desig_Type : constant Entity_Id := Designated_Type (T);
10812 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10813 Constraint_OK : Boolean := True;
10814
10815 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10816 -- Simple predicate to test for defaulted discriminants
10817 -- Shouldn't this be in sem_util???
10818
10819 ---------------------------------
10820 -- Has_Defaulted_Discriminants --
10821 ---------------------------------
10822
10823 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10824 begin
10825 return Has_Discriminants (Typ)
10826 and then Present (First_Discriminant (Typ))
10827 and then Present
10828 (Discriminant_Default_Value (First_Discriminant (Typ)));
10829 end Has_Defaulted_Discriminants;
10830
10831 -- Start of processing for Constrain_Access
10832
10833 begin
10834 if Is_Array_Type (Desig_Type) then
10835 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10836
10837 elsif (Is_Record_Type (Desig_Type)
10838 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10839 and then not Is_Constrained (Desig_Type)
10840 then
10841 -- ??? The following code is a temporary kludge to ignore a
10842 -- discriminant constraint on access type if it is constraining
10843 -- the current record. Avoid creating the implicit subtype of the
10844 -- record we are currently compiling since right now, we cannot
10845 -- handle these. For now, just return the access type itself.
10846
10847 if Desig_Type = Current_Scope
10848 and then No (Def_Id)
10849 then
10850 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10851 Def_Id := Entity (Subtype_Mark (S));
10852
10853 -- This call added to ensure that the constraint is analyzed
10854 -- (needed for a B test). Note that we still return early from
10855 -- this procedure to avoid recursive processing. ???
10856
10857 Constrain_Discriminated_Type
10858 (Desig_Subtype, S, Related_Nod, For_Access => True);
10859 return;
10860 end if;
10861
10862 -- Enforce rule that the constraint is illegal if there is an
10863 -- unconstrained view of the designated type. This means that the
10864 -- partial view (either a private type declaration or a derivation
10865 -- from a private type) has no discriminants. (Defect Report
10866 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
10867
10868 -- Rule updated for Ada 2005: the private type is said to have
10869 -- a constrained partial view, given that objects of the type
10870 -- can be declared. Furthermore, the rule applies to all access
10871 -- types, unlike the rule concerning default discriminants (see
10872 -- RM 3.7.1(7/3))
10873
10874 if (Ekind (T) = E_General_Access_Type
10875 or else Ada_Version >= Ada_2005)
10876 and then Has_Private_Declaration (Desig_Type)
10877 and then In_Open_Scopes (Scope (Desig_Type))
10878 and then Has_Discriminants (Desig_Type)
10879 then
10880 declare
10881 Pack : constant Node_Id :=
10882 Unit_Declaration_Node (Scope (Desig_Type));
10883 Decls : List_Id;
10884 Decl : Node_Id;
10885
10886 begin
10887 if Nkind (Pack) = N_Package_Declaration then
10888 Decls := Visible_Declarations (Specification (Pack));
10889 Decl := First (Decls);
10890 while Present (Decl) loop
10891 if (Nkind (Decl) = N_Private_Type_Declaration
10892 and then
10893 Chars (Defining_Identifier (Decl)) =
10894 Chars (Desig_Type))
10895
10896 or else
10897 (Nkind (Decl) = N_Full_Type_Declaration
10898 and then
10899 Chars (Defining_Identifier (Decl)) =
10900 Chars (Desig_Type)
10901 and then Is_Derived_Type (Desig_Type)
10902 and then
10903 Has_Private_Declaration (Etype (Desig_Type)))
10904 then
10905 if No (Discriminant_Specifications (Decl)) then
10906 Error_Msg_N
10907 ("cannot constrain access type if designated " &
10908 "type has constrained partial view", S);
10909 end if;
10910
10911 exit;
10912 end if;
10913
10914 Next (Decl);
10915 end loop;
10916 end if;
10917 end;
10918 end if;
10919
10920 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10921 For_Access => True);
10922
10923 elsif (Is_Task_Type (Desig_Type)
10924 or else Is_Protected_Type (Desig_Type))
10925 and then not Is_Constrained (Desig_Type)
10926 then
10927 Constrain_Concurrent
10928 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10929
10930 else
10931 Error_Msg_N ("invalid constraint on access type", S);
10932 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10933 Constraint_OK := False;
10934 end if;
10935
10936 if No (Def_Id) then
10937 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10938 else
10939 Set_Ekind (Def_Id, E_Access_Subtype);
10940 end if;
10941
10942 if Constraint_OK then
10943 Set_Etype (Def_Id, Base_Type (T));
10944
10945 if Is_Private_Type (Desig_Type) then
10946 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10947 end if;
10948 else
10949 Set_Etype (Def_Id, Any_Type);
10950 end if;
10951
10952 Set_Size_Info (Def_Id, T);
10953 Set_Is_Constrained (Def_Id, Constraint_OK);
10954 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10955 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10956 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10957
10958 Conditional_Delay (Def_Id, T);
10959
10960 -- AI-363 : Subtypes of general access types whose designated types have
10961 -- default discriminants are disallowed. In instances, the rule has to
10962 -- be checked against the actual, of which T is the subtype. In a
10963 -- generic body, the rule is checked assuming that the actual type has
10964 -- defaulted discriminants.
10965
10966 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10967 if Ekind (Base_Type (T)) = E_General_Access_Type
10968 and then Has_Defaulted_Discriminants (Desig_Type)
10969 then
10970 if Ada_Version < Ada_2005 then
10971 Error_Msg_N
10972 ("access subtype of general access type would not " &
10973 "be allowed in Ada 2005?y?", S);
10974 else
10975 Error_Msg_N
10976 ("access subtype of general access type not allowed", S);
10977 end if;
10978
10979 Error_Msg_N ("\discriminants have defaults", S);
10980
10981 elsif Is_Access_Type (T)
10982 and then Is_Generic_Type (Desig_Type)
10983 and then Has_Discriminants (Desig_Type)
10984 and then In_Package_Body (Current_Scope)
10985 then
10986 if Ada_Version < Ada_2005 then
10987 Error_Msg_N
10988 ("access subtype would not be allowed in generic body " &
10989 "in Ada 2005?y?", S);
10990 else
10991 Error_Msg_N
10992 ("access subtype not allowed in generic body", S);
10993 end if;
10994
10995 Error_Msg_N
10996 ("\designated type is a discriminated formal", S);
10997 end if;
10998 end if;
10999 end Constrain_Access;
11000
11001 ---------------------
11002 -- Constrain_Array --
11003 ---------------------
11004
11005 procedure Constrain_Array
11006 (Def_Id : in out Entity_Id;
11007 SI : Node_Id;
11008 Related_Nod : Node_Id;
11009 Related_Id : Entity_Id;
11010 Suffix : Character)
11011 is
11012 C : constant Node_Id := Constraint (SI);
11013 Number_Of_Constraints : Nat := 0;
11014 Index : Node_Id;
11015 S, T : Entity_Id;
11016 Constraint_OK : Boolean := True;
11017
11018 begin
11019 T := Entity (Subtype_Mark (SI));
11020
11021 if Ekind (T) in Access_Kind then
11022 T := Designated_Type (T);
11023 end if;
11024
11025 -- If an index constraint follows a subtype mark in a subtype indication
11026 -- then the type or subtype denoted by the subtype mark must not already
11027 -- impose an index constraint. The subtype mark must denote either an
11028 -- unconstrained array type or an access type whose designated type
11029 -- is such an array type... (RM 3.6.1)
11030
11031 if Is_Constrained (T) then
11032 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
11033 Constraint_OK := False;
11034
11035 else
11036 S := First (Constraints (C));
11037 while Present (S) loop
11038 Number_Of_Constraints := Number_Of_Constraints + 1;
11039 Next (S);
11040 end loop;
11041
11042 -- In either case, the index constraint must provide a discrete
11043 -- range for each index of the array type and the type of each
11044 -- discrete range must be the same as that of the corresponding
11045 -- index. (RM 3.6.1)
11046
11047 if Number_Of_Constraints /= Number_Dimensions (T) then
11048 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
11049 Constraint_OK := False;
11050
11051 else
11052 S := First (Constraints (C));
11053 Index := First_Index (T);
11054 Analyze (Index);
11055
11056 -- Apply constraints to each index type
11057
11058 for J in 1 .. Number_Of_Constraints loop
11059 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
11060 Next (Index);
11061 Next (S);
11062 end loop;
11063
11064 end if;
11065 end if;
11066
11067 if No (Def_Id) then
11068 Def_Id :=
11069 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
11070 Set_Parent (Def_Id, Related_Nod);
11071
11072 else
11073 Set_Ekind (Def_Id, E_Array_Subtype);
11074 end if;
11075
11076 Set_Size_Info (Def_Id, (T));
11077 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11078 Set_Etype (Def_Id, Base_Type (T));
11079
11080 if Constraint_OK then
11081 Set_First_Index (Def_Id, First (Constraints (C)));
11082 else
11083 Set_First_Index (Def_Id, First_Index (T));
11084 end if;
11085
11086 Set_Is_Constrained (Def_Id, True);
11087 Set_Is_Aliased (Def_Id, Is_Aliased (T));
11088 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11089
11090 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
11091 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
11092
11093 -- A subtype does not inherit the packed_array_type of is parent. We
11094 -- need to initialize the attribute because if Def_Id is previously
11095 -- analyzed through a limited_with clause, it will have the attributes
11096 -- of an incomplete type, one of which is an Elist that overlaps the
11097 -- Packed_Array_Type field.
11098
11099 Set_Packed_Array_Type (Def_Id, Empty);
11100
11101 -- Build a freeze node if parent still needs one. Also make sure that
11102 -- the Depends_On_Private status is set because the subtype will need
11103 -- reprocessing at the time the base type does, and also we must set a
11104 -- conditional delay.
11105
11106 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
11107 Conditional_Delay (Def_Id, T);
11108 end Constrain_Array;
11109
11110 ------------------------------
11111 -- Constrain_Component_Type --
11112 ------------------------------
11113
11114 function Constrain_Component_Type
11115 (Comp : Entity_Id;
11116 Constrained_Typ : Entity_Id;
11117 Related_Node : Node_Id;
11118 Typ : Entity_Id;
11119 Constraints : Elist_Id) return Entity_Id
11120 is
11121 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
11122 Compon_Type : constant Entity_Id := Etype (Comp);
11123 Array_Comp : Node_Id;
11124
11125 function Build_Constrained_Array_Type
11126 (Old_Type : Entity_Id) return Entity_Id;
11127 -- If Old_Type is an array type, one of whose indexes is constrained
11128 -- by a discriminant, build an Itype whose constraint replaces the
11129 -- discriminant with its value in the constraint.
11130
11131 function Build_Constrained_Discriminated_Type
11132 (Old_Type : Entity_Id) return Entity_Id;
11133 -- Ditto for record components
11134
11135 function Build_Constrained_Access_Type
11136 (Old_Type : Entity_Id) return Entity_Id;
11137 -- Ditto for access types. Makes use of previous two functions, to
11138 -- constrain designated type.
11139
11140 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
11141 -- T is an array or discriminated type, C is a list of constraints
11142 -- that apply to T. This routine builds the constrained subtype.
11143
11144 function Is_Discriminant (Expr : Node_Id) return Boolean;
11145 -- Returns True if Expr is a discriminant
11146
11147 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
11148 -- Find the value of discriminant Discrim in Constraint
11149
11150 -----------------------------------
11151 -- Build_Constrained_Access_Type --
11152 -----------------------------------
11153
11154 function Build_Constrained_Access_Type
11155 (Old_Type : Entity_Id) return Entity_Id
11156 is
11157 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
11158 Itype : Entity_Id;
11159 Desig_Subtype : Entity_Id;
11160 Scop : Entity_Id;
11161
11162 begin
11163 -- if the original access type was not embedded in the enclosing
11164 -- type definition, there is no need to produce a new access
11165 -- subtype. In fact every access type with an explicit constraint
11166 -- generates an itype whose scope is the enclosing record.
11167
11168 if not Is_Type (Scope (Old_Type)) then
11169 return Old_Type;
11170
11171 elsif Is_Array_Type (Desig_Type) then
11172 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
11173
11174 elsif Has_Discriminants (Desig_Type) then
11175
11176 -- This may be an access type to an enclosing record type for
11177 -- which we are constructing the constrained components. Return
11178 -- the enclosing record subtype. This is not always correct,
11179 -- but avoids infinite recursion. ???
11180
11181 Desig_Subtype := Any_Type;
11182
11183 for J in reverse 0 .. Scope_Stack.Last loop
11184 Scop := Scope_Stack.Table (J).Entity;
11185
11186 if Is_Type (Scop)
11187 and then Base_Type (Scop) = Base_Type (Desig_Type)
11188 then
11189 Desig_Subtype := Scop;
11190 end if;
11191
11192 exit when not Is_Type (Scop);
11193 end loop;
11194
11195 if Desig_Subtype = Any_Type then
11196 Desig_Subtype :=
11197 Build_Constrained_Discriminated_Type (Desig_Type);
11198 end if;
11199
11200 else
11201 return Old_Type;
11202 end if;
11203
11204 if Desig_Subtype /= Desig_Type then
11205
11206 -- The Related_Node better be here or else we won't be able
11207 -- to attach new itypes to a node in the tree.
11208
11209 pragma Assert (Present (Related_Node));
11210
11211 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11212
11213 Set_Etype (Itype, Base_Type (Old_Type));
11214 Set_Size_Info (Itype, (Old_Type));
11215 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11216 Set_Depends_On_Private (Itype, Has_Private_Component
11217 (Old_Type));
11218 Set_Is_Access_Constant (Itype, Is_Access_Constant
11219 (Old_Type));
11220
11221 -- The new itype needs freezing when it depends on a not frozen
11222 -- type and the enclosing subtype needs freezing.
11223
11224 if Has_Delayed_Freeze (Constrained_Typ)
11225 and then not Is_Frozen (Constrained_Typ)
11226 then
11227 Conditional_Delay (Itype, Base_Type (Old_Type));
11228 end if;
11229
11230 return Itype;
11231
11232 else
11233 return Old_Type;
11234 end if;
11235 end Build_Constrained_Access_Type;
11236
11237 ----------------------------------
11238 -- Build_Constrained_Array_Type --
11239 ----------------------------------
11240
11241 function Build_Constrained_Array_Type
11242 (Old_Type : Entity_Id) return Entity_Id
11243 is
11244 Lo_Expr : Node_Id;
11245 Hi_Expr : Node_Id;
11246 Old_Index : Node_Id;
11247 Range_Node : Node_Id;
11248 Constr_List : List_Id;
11249
11250 Need_To_Create_Itype : Boolean := False;
11251
11252 begin
11253 Old_Index := First_Index (Old_Type);
11254 while Present (Old_Index) loop
11255 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11256
11257 if Is_Discriminant (Lo_Expr)
11258 or else Is_Discriminant (Hi_Expr)
11259 then
11260 Need_To_Create_Itype := True;
11261 end if;
11262
11263 Next_Index (Old_Index);
11264 end loop;
11265
11266 if Need_To_Create_Itype then
11267 Constr_List := New_List;
11268
11269 Old_Index := First_Index (Old_Type);
11270 while Present (Old_Index) loop
11271 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11272
11273 if Is_Discriminant (Lo_Expr) then
11274 Lo_Expr := Get_Discr_Value (Lo_Expr);
11275 end if;
11276
11277 if Is_Discriminant (Hi_Expr) then
11278 Hi_Expr := Get_Discr_Value (Hi_Expr);
11279 end if;
11280
11281 Range_Node :=
11282 Make_Range
11283 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11284
11285 Append (Range_Node, To => Constr_List);
11286
11287 Next_Index (Old_Index);
11288 end loop;
11289
11290 return Build_Subtype (Old_Type, Constr_List);
11291
11292 else
11293 return Old_Type;
11294 end if;
11295 end Build_Constrained_Array_Type;
11296
11297 ------------------------------------------
11298 -- Build_Constrained_Discriminated_Type --
11299 ------------------------------------------
11300
11301 function Build_Constrained_Discriminated_Type
11302 (Old_Type : Entity_Id) return Entity_Id
11303 is
11304 Expr : Node_Id;
11305 Constr_List : List_Id;
11306 Old_Constraint : Elmt_Id;
11307
11308 Need_To_Create_Itype : Boolean := False;
11309
11310 begin
11311 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11312 while Present (Old_Constraint) loop
11313 Expr := Node (Old_Constraint);
11314
11315 if Is_Discriminant (Expr) then
11316 Need_To_Create_Itype := True;
11317 end if;
11318
11319 Next_Elmt (Old_Constraint);
11320 end loop;
11321
11322 if Need_To_Create_Itype then
11323 Constr_List := New_List;
11324
11325 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11326 while Present (Old_Constraint) loop
11327 Expr := Node (Old_Constraint);
11328
11329 if Is_Discriminant (Expr) then
11330 Expr := Get_Discr_Value (Expr);
11331 end if;
11332
11333 Append (New_Copy_Tree (Expr), To => Constr_List);
11334
11335 Next_Elmt (Old_Constraint);
11336 end loop;
11337
11338 return Build_Subtype (Old_Type, Constr_List);
11339
11340 else
11341 return Old_Type;
11342 end if;
11343 end Build_Constrained_Discriminated_Type;
11344
11345 -------------------
11346 -- Build_Subtype --
11347 -------------------
11348
11349 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11350 Indic : Node_Id;
11351 Subtyp_Decl : Node_Id;
11352 Def_Id : Entity_Id;
11353 Btyp : Entity_Id := Base_Type (T);
11354
11355 begin
11356 -- The Related_Node better be here or else we won't be able to
11357 -- attach new itypes to a node in the tree.
11358
11359 pragma Assert (Present (Related_Node));
11360
11361 -- If the view of the component's type is incomplete or private
11362 -- with unknown discriminants, then the constraint must be applied
11363 -- to the full type.
11364
11365 if Has_Unknown_Discriminants (Btyp)
11366 and then Present (Underlying_Type (Btyp))
11367 then
11368 Btyp := Underlying_Type (Btyp);
11369 end if;
11370
11371 Indic :=
11372 Make_Subtype_Indication (Loc,
11373 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11374 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11375
11376 Def_Id := Create_Itype (Ekind (T), Related_Node);
11377
11378 Subtyp_Decl :=
11379 Make_Subtype_Declaration (Loc,
11380 Defining_Identifier => Def_Id,
11381 Subtype_Indication => Indic);
11382
11383 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11384
11385 -- Itypes must be analyzed with checks off (see package Itypes)
11386
11387 Analyze (Subtyp_Decl, Suppress => All_Checks);
11388
11389 return Def_Id;
11390 end Build_Subtype;
11391
11392 ---------------------
11393 -- Get_Discr_Value --
11394 ---------------------
11395
11396 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11397 D : Entity_Id;
11398 E : Elmt_Id;
11399
11400 begin
11401 -- The discriminant may be declared for the type, in which case we
11402 -- find it by iterating over the list of discriminants. If the
11403 -- discriminant is inherited from a parent type, it appears as the
11404 -- corresponding discriminant of the current type. This will be the
11405 -- case when constraining an inherited component whose constraint is
11406 -- given by a discriminant of the parent.
11407
11408 D := First_Discriminant (Typ);
11409 E := First_Elmt (Constraints);
11410
11411 while Present (D) loop
11412 if D = Entity (Discrim)
11413 or else D = CR_Discriminant (Entity (Discrim))
11414 or else Corresponding_Discriminant (D) = Entity (Discrim)
11415 then
11416 return Node (E);
11417 end if;
11418
11419 Next_Discriminant (D);
11420 Next_Elmt (E);
11421 end loop;
11422
11423 -- The Corresponding_Discriminant mechanism is incomplete, because
11424 -- the correspondence between new and old discriminants is not one
11425 -- to one: one new discriminant can constrain several old ones. In
11426 -- that case, scan sequentially the stored_constraint, the list of
11427 -- discriminants of the parents, and the constraints.
11428
11429 -- Previous code checked for the present of the Stored_Constraint
11430 -- list for the derived type, but did not use it at all. Should it
11431 -- be present when the component is a discriminated task type?
11432
11433 if Is_Derived_Type (Typ)
11434 and then Scope (Entity (Discrim)) = Etype (Typ)
11435 then
11436 D := First_Discriminant (Etype (Typ));
11437 E := First_Elmt (Constraints);
11438 while Present (D) loop
11439 if D = Entity (Discrim) then
11440 return Node (E);
11441 end if;
11442
11443 Next_Discriminant (D);
11444 Next_Elmt (E);
11445 end loop;
11446 end if;
11447
11448 -- Something is wrong if we did not find the value
11449
11450 raise Program_Error;
11451 end Get_Discr_Value;
11452
11453 ---------------------
11454 -- Is_Discriminant --
11455 ---------------------
11456
11457 function Is_Discriminant (Expr : Node_Id) return Boolean is
11458 Discrim_Scope : Entity_Id;
11459
11460 begin
11461 if Denotes_Discriminant (Expr) then
11462 Discrim_Scope := Scope (Entity (Expr));
11463
11464 -- Either we have a reference to one of Typ's discriminants,
11465
11466 pragma Assert (Discrim_Scope = Typ
11467
11468 -- or to the discriminants of the parent type, in the case
11469 -- of a derivation of a tagged type with variants.
11470
11471 or else Discrim_Scope = Etype (Typ)
11472 or else Full_View (Discrim_Scope) = Etype (Typ)
11473
11474 -- or same as above for the case where the discriminants
11475 -- were declared in Typ's private view.
11476
11477 or else (Is_Private_Type (Discrim_Scope)
11478 and then Chars (Discrim_Scope) = Chars (Typ))
11479
11480 -- or else we are deriving from the full view and the
11481 -- discriminant is declared in the private entity.
11482
11483 or else (Is_Private_Type (Typ)
11484 and then Chars (Discrim_Scope) = Chars (Typ))
11485
11486 -- Or we are constrained the corresponding record of a
11487 -- synchronized type that completes a private declaration.
11488
11489 or else (Is_Concurrent_Record_Type (Typ)
11490 and then
11491 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11492
11493 -- or we have a class-wide type, in which case make sure the
11494 -- discriminant found belongs to the root type.
11495
11496 or else (Is_Class_Wide_Type (Typ)
11497 and then Etype (Typ) = Discrim_Scope));
11498
11499 return True;
11500 end if;
11501
11502 -- In all other cases we have something wrong
11503
11504 return False;
11505 end Is_Discriminant;
11506
11507 -- Start of processing for Constrain_Component_Type
11508
11509 begin
11510 if Nkind (Parent (Comp)) = N_Component_Declaration
11511 and then Comes_From_Source (Parent (Comp))
11512 and then Comes_From_Source
11513 (Subtype_Indication (Component_Definition (Parent (Comp))))
11514 and then
11515 Is_Entity_Name
11516 (Subtype_Indication (Component_Definition (Parent (Comp))))
11517 then
11518 return Compon_Type;
11519
11520 elsif Is_Array_Type (Compon_Type) then
11521 Array_Comp := Build_Constrained_Array_Type (Compon_Type);
11522
11523 -- If the component of the parent is packed, and the record type is
11524 -- already frozen, as is the case for an itype, the component type
11525 -- itself will not be frozen, and the packed array type for it must
11526 -- be constructed explicitly. Since the creation of packed types is
11527 -- an expansion activity, we only do this if expansion is active.
11528
11529 if Expander_Active
11530 and then Is_Packed (Compon_Type)
11531 and then Is_Frozen (Current_Scope)
11532 then
11533 Create_Packed_Array_Type (Array_Comp);
11534 end if;
11535
11536 return Array_Comp;
11537
11538 elsif Has_Discriminants (Compon_Type) then
11539 return Build_Constrained_Discriminated_Type (Compon_Type);
11540
11541 elsif Is_Access_Type (Compon_Type) then
11542 return Build_Constrained_Access_Type (Compon_Type);
11543
11544 else
11545 return Compon_Type;
11546 end if;
11547 end Constrain_Component_Type;
11548
11549 --------------------------
11550 -- Constrain_Concurrent --
11551 --------------------------
11552
11553 -- For concurrent types, the associated record value type carries the same
11554 -- discriminants, so when we constrain a concurrent type, we must constrain
11555 -- the corresponding record type as well.
11556
11557 procedure Constrain_Concurrent
11558 (Def_Id : in out Entity_Id;
11559 SI : Node_Id;
11560 Related_Nod : Node_Id;
11561 Related_Id : Entity_Id;
11562 Suffix : Character)
11563 is
11564 -- Retrieve Base_Type to ensure getting to the concurrent type in the
11565 -- case of a private subtype (needed when only doing semantic analysis).
11566
11567 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
11568 T_Val : Entity_Id;
11569
11570 begin
11571 if Ekind (T_Ent) in Access_Kind then
11572 T_Ent := Designated_Type (T_Ent);
11573 end if;
11574
11575 T_Val := Corresponding_Record_Type (T_Ent);
11576
11577 if Present (T_Val) then
11578
11579 if No (Def_Id) then
11580 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11581 end if;
11582
11583 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11584
11585 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11586 Set_Corresponding_Record_Type (Def_Id,
11587 Constrain_Corresponding_Record
11588 (Def_Id, T_Val, Related_Nod, Related_Id));
11589
11590 else
11591 -- If there is no associated record, expansion is disabled and this
11592 -- is a generic context. Create a subtype in any case, so that
11593 -- semantic analysis can proceed.
11594
11595 if No (Def_Id) then
11596 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11597 end if;
11598
11599 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11600 end if;
11601 end Constrain_Concurrent;
11602
11603 ------------------------------------
11604 -- Constrain_Corresponding_Record --
11605 ------------------------------------
11606
11607 function Constrain_Corresponding_Record
11608 (Prot_Subt : Entity_Id;
11609 Corr_Rec : Entity_Id;
11610 Related_Nod : Node_Id;
11611 Related_Id : Entity_Id) return Entity_Id
11612 is
11613 T_Sub : constant Entity_Id :=
11614 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11615
11616 begin
11617 Set_Etype (T_Sub, Corr_Rec);
11618 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11619 Set_Is_Constrained (T_Sub, True);
11620 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11621 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11622
11623 -- As elsewhere, we do not want to create a freeze node for this itype
11624 -- if it is created for a constrained component of an enclosing record
11625 -- because references to outer discriminants will appear out of scope.
11626
11627 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11628 Conditional_Delay (T_Sub, Corr_Rec);
11629 else
11630 Set_Is_Frozen (T_Sub);
11631 end if;
11632
11633 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11634 Set_Discriminant_Constraint
11635 (T_Sub, Discriminant_Constraint (Prot_Subt));
11636 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11637 Create_Constrained_Components
11638 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11639 end if;
11640
11641 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11642
11643 return T_Sub;
11644 end Constrain_Corresponding_Record;
11645
11646 -----------------------
11647 -- Constrain_Decimal --
11648 -----------------------
11649
11650 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11651 T : constant Entity_Id := Entity (Subtype_Mark (S));
11652 C : constant Node_Id := Constraint (S);
11653 Loc : constant Source_Ptr := Sloc (C);
11654 Range_Expr : Node_Id;
11655 Digits_Expr : Node_Id;
11656 Digits_Val : Uint;
11657 Bound_Val : Ureal;
11658
11659 begin
11660 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11661
11662 if Nkind (C) = N_Range_Constraint then
11663 Range_Expr := Range_Expression (C);
11664 Digits_Val := Digits_Value (T);
11665
11666 else
11667 pragma Assert (Nkind (C) = N_Digits_Constraint);
11668
11669 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11670
11671 Digits_Expr := Digits_Expression (C);
11672 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11673
11674 Check_Digits_Expression (Digits_Expr);
11675 Digits_Val := Expr_Value (Digits_Expr);
11676
11677 if Digits_Val > Digits_Value (T) then
11678 Error_Msg_N
11679 ("digits expression is incompatible with subtype", C);
11680 Digits_Val := Digits_Value (T);
11681 end if;
11682
11683 if Present (Range_Constraint (C)) then
11684 Range_Expr := Range_Expression (Range_Constraint (C));
11685 else
11686 Range_Expr := Empty;
11687 end if;
11688 end if;
11689
11690 Set_Etype (Def_Id, Base_Type (T));
11691 Set_Size_Info (Def_Id, (T));
11692 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11693 Set_Delta_Value (Def_Id, Delta_Value (T));
11694 Set_Scale_Value (Def_Id, Scale_Value (T));
11695 Set_Small_Value (Def_Id, Small_Value (T));
11696 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11697 Set_Digits_Value (Def_Id, Digits_Val);
11698
11699 -- Manufacture range from given digits value if no range present
11700
11701 if No (Range_Expr) then
11702 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11703 Range_Expr :=
11704 Make_Range (Loc,
11705 Low_Bound =>
11706 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11707 High_Bound =>
11708 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11709 end if;
11710
11711 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11712 Set_Discrete_RM_Size (Def_Id);
11713
11714 -- Unconditionally delay the freeze, since we cannot set size
11715 -- information in all cases correctly until the freeze point.
11716
11717 Set_Has_Delayed_Freeze (Def_Id);
11718 end Constrain_Decimal;
11719
11720 ----------------------------------
11721 -- Constrain_Discriminated_Type --
11722 ----------------------------------
11723
11724 procedure Constrain_Discriminated_Type
11725 (Def_Id : Entity_Id;
11726 S : Node_Id;
11727 Related_Nod : Node_Id;
11728 For_Access : Boolean := False)
11729 is
11730 E : constant Entity_Id := Entity (Subtype_Mark (S));
11731 T : Entity_Id;
11732 C : Node_Id;
11733 Elist : Elist_Id := New_Elmt_List;
11734
11735 procedure Fixup_Bad_Constraint;
11736 -- This is called after finding a bad constraint, and after having
11737 -- posted an appropriate error message. The mission is to leave the
11738 -- entity T in as reasonable state as possible!
11739
11740 --------------------------
11741 -- Fixup_Bad_Constraint --
11742 --------------------------
11743
11744 procedure Fixup_Bad_Constraint is
11745 begin
11746 -- Set a reasonable Ekind for the entity. For an incomplete type,
11747 -- we can't do much, but for other types, we can set the proper
11748 -- corresponding subtype kind.
11749
11750 if Ekind (T) = E_Incomplete_Type then
11751 Set_Ekind (Def_Id, Ekind (T));
11752 else
11753 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11754 end if;
11755
11756 -- Set Etype to the known type, to reduce chances of cascaded errors
11757
11758 Set_Etype (Def_Id, E);
11759 Set_Error_Posted (Def_Id);
11760 end Fixup_Bad_Constraint;
11761
11762 -- Start of processing for Constrain_Discriminated_Type
11763
11764 begin
11765 C := Constraint (S);
11766
11767 -- A discriminant constraint is only allowed in a subtype indication,
11768 -- after a subtype mark. This subtype mark must denote either a type
11769 -- with discriminants, or an access type whose designated type is a
11770 -- type with discriminants. A discriminant constraint specifies the
11771 -- values of these discriminants (RM 3.7.2(5)).
11772
11773 T := Base_Type (Entity (Subtype_Mark (S)));
11774
11775 if Ekind (T) in Access_Kind then
11776 T := Designated_Type (T);
11777 end if;
11778
11779 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11780 -- Avoid generating an error for access-to-incomplete subtypes.
11781
11782 if Ada_Version >= Ada_2005
11783 and then Ekind (T) = E_Incomplete_Type
11784 and then Nkind (Parent (S)) = N_Subtype_Declaration
11785 and then not Is_Itype (Def_Id)
11786 then
11787 -- A little sanity check, emit an error message if the type
11788 -- has discriminants to begin with. Type T may be a regular
11789 -- incomplete type or imported via a limited with clause.
11790
11791 if Has_Discriminants (T)
11792 or else
11793 (From_With_Type (T)
11794 and then Present (Non_Limited_View (T))
11795 and then Nkind (Parent (Non_Limited_View (T))) =
11796 N_Full_Type_Declaration
11797 and then Present (Discriminant_Specifications
11798 (Parent (Non_Limited_View (T)))))
11799 then
11800 Error_Msg_N
11801 ("(Ada 2005) incomplete subtype may not be constrained", C);
11802 else
11803 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11804 end if;
11805
11806 Fixup_Bad_Constraint;
11807 return;
11808
11809 -- Check that the type has visible discriminants. The type may be
11810 -- a private type with unknown discriminants whose full view has
11811 -- discriminants which are invisible.
11812
11813 elsif not Has_Discriminants (T)
11814 or else
11815 (Has_Unknown_Discriminants (T)
11816 and then Is_Private_Type (T))
11817 then
11818 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11819 Fixup_Bad_Constraint;
11820 return;
11821
11822 elsif Is_Constrained (E)
11823 or else (Ekind (E) = E_Class_Wide_Subtype
11824 and then Present (Discriminant_Constraint (E)))
11825 then
11826 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11827 Fixup_Bad_Constraint;
11828 return;
11829 end if;
11830
11831 -- T may be an unconstrained subtype (e.g. a generic actual).
11832 -- Constraint applies to the base type.
11833
11834 T := Base_Type (T);
11835
11836 Elist := Build_Discriminant_Constraints (T, S);
11837
11838 -- If the list returned was empty we had an error in building the
11839 -- discriminant constraint. We have also already signalled an error
11840 -- in the incomplete type case
11841
11842 if Is_Empty_Elmt_List (Elist) then
11843 Fixup_Bad_Constraint;
11844 return;
11845 end if;
11846
11847 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11848 end Constrain_Discriminated_Type;
11849
11850 ---------------------------
11851 -- Constrain_Enumeration --
11852 ---------------------------
11853
11854 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11855 T : constant Entity_Id := Entity (Subtype_Mark (S));
11856 C : constant Node_Id := Constraint (S);
11857
11858 begin
11859 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11860
11861 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11862
11863 Set_Etype (Def_Id, Base_Type (T));
11864 Set_Size_Info (Def_Id, (T));
11865 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11866 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11867
11868 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11869
11870 Set_Discrete_RM_Size (Def_Id);
11871 end Constrain_Enumeration;
11872
11873 ----------------------
11874 -- Constrain_Float --
11875 ----------------------
11876
11877 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11878 T : constant Entity_Id := Entity (Subtype_Mark (S));
11879 C : Node_Id;
11880 D : Node_Id;
11881 Rais : Node_Id;
11882
11883 begin
11884 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11885
11886 Set_Etype (Def_Id, Base_Type (T));
11887 Set_Size_Info (Def_Id, (T));
11888 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11889
11890 -- Process the constraint
11891
11892 C := Constraint (S);
11893
11894 -- Digits constraint present
11895
11896 if Nkind (C) = N_Digits_Constraint then
11897
11898 Check_SPARK_Restriction ("digits constraint is not allowed", S);
11899 Check_Restriction (No_Obsolescent_Features, C);
11900
11901 if Warn_On_Obsolescent_Feature then
11902 Error_Msg_N
11903 ("subtype digits constraint is an " &
11904 "obsolescent feature (RM J.3(8))?j?", C);
11905 end if;
11906
11907 D := Digits_Expression (C);
11908 Analyze_And_Resolve (D, Any_Integer);
11909 Check_Digits_Expression (D);
11910 Set_Digits_Value (Def_Id, Expr_Value (D));
11911
11912 -- Check that digits value is in range. Obviously we can do this
11913 -- at compile time, but it is strictly a runtime check, and of
11914 -- course there is an ACVC test that checks this!
11915
11916 if Digits_Value (Def_Id) > Digits_Value (T) then
11917 Error_Msg_Uint_1 := Digits_Value (T);
11918 Error_Msg_N ("??digits value is too large, maximum is ^", D);
11919 Rais :=
11920 Make_Raise_Constraint_Error (Sloc (D),
11921 Reason => CE_Range_Check_Failed);
11922 Insert_Action (Declaration_Node (Def_Id), Rais);
11923 end if;
11924
11925 C := Range_Constraint (C);
11926
11927 -- No digits constraint present
11928
11929 else
11930 Set_Digits_Value (Def_Id, Digits_Value (T));
11931 end if;
11932
11933 -- Range constraint present
11934
11935 if Nkind (C) = N_Range_Constraint then
11936 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11937
11938 -- No range constraint present
11939
11940 else
11941 pragma Assert (No (C));
11942 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11943 end if;
11944
11945 Set_Is_Constrained (Def_Id);
11946 end Constrain_Float;
11947
11948 ---------------------
11949 -- Constrain_Index --
11950 ---------------------
11951
11952 procedure Constrain_Index
11953 (Index : Node_Id;
11954 S : Node_Id;
11955 Related_Nod : Node_Id;
11956 Related_Id : Entity_Id;
11957 Suffix : Character;
11958 Suffix_Index : Nat)
11959 is
11960 Def_Id : Entity_Id;
11961 R : Node_Id := Empty;
11962 T : constant Entity_Id := Etype (Index);
11963
11964 begin
11965 if Nkind (S) = N_Range
11966 or else
11967 (Nkind (S) = N_Attribute_Reference
11968 and then Attribute_Name (S) = Name_Range)
11969 then
11970 -- A Range attribute will be transformed into N_Range by Resolve
11971
11972 Analyze (S);
11973 Set_Etype (S, T);
11974 R := S;
11975
11976 Process_Range_Expr_In_Decl (R, T, Empty_List);
11977
11978 if not Error_Posted (S)
11979 and then
11980 (Nkind (S) /= N_Range
11981 or else not Covers (T, (Etype (Low_Bound (S))))
11982 or else not Covers (T, (Etype (High_Bound (S)))))
11983 then
11984 if Base_Type (T) /= Any_Type
11985 and then Etype (Low_Bound (S)) /= Any_Type
11986 and then Etype (High_Bound (S)) /= Any_Type
11987 then
11988 Error_Msg_N ("range expected", S);
11989 end if;
11990 end if;
11991
11992 elsif Nkind (S) = N_Subtype_Indication then
11993
11994 -- The parser has verified that this is a discrete indication
11995
11996 Resolve_Discrete_Subtype_Indication (S, T);
11997 R := Range_Expression (Constraint (S));
11998
11999 -- Capture values of bounds and generate temporaries for them if
12000 -- needed, since checks may cause duplication of the expressions
12001 -- which must not be reevaluated.
12002
12003 -- The forced evaluation removes side effects from expressions,
12004 -- which should occur also in SPARK mode. Otherwise, we end up with
12005 -- unexpected insertions of actions at places where this is not
12006 -- supposed to occur, e.g. on default parameters of a call.
12007
12008 if Expander_Active then
12009 Force_Evaluation (Low_Bound (R));
12010 Force_Evaluation (High_Bound (R));
12011 end if;
12012
12013 elsif Nkind (S) = N_Discriminant_Association then
12014
12015 -- Syntactically valid in subtype indication
12016
12017 Error_Msg_N ("invalid index constraint", S);
12018 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12019 return;
12020
12021 -- Subtype_Mark case, no anonymous subtypes to construct
12022
12023 else
12024 Analyze (S);
12025
12026 if Is_Entity_Name (S) then
12027 if not Is_Type (Entity (S)) then
12028 Error_Msg_N ("expect subtype mark for index constraint", S);
12029
12030 elsif Base_Type (Entity (S)) /= Base_Type (T) then
12031 Wrong_Type (S, Base_Type (T));
12032
12033 -- Check error of subtype with predicate in index constraint
12034
12035 else
12036 Bad_Predicated_Subtype_Use
12037 ("subtype& has predicate, not allowed in index constraint",
12038 S, Entity (S));
12039 end if;
12040
12041 return;
12042
12043 else
12044 Error_Msg_N ("invalid index constraint", S);
12045 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12046 return;
12047 end if;
12048 end if;
12049
12050 Def_Id :=
12051 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
12052
12053 Set_Etype (Def_Id, Base_Type (T));
12054
12055 if Is_Modular_Integer_Type (T) then
12056 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12057
12058 elsif Is_Integer_Type (T) then
12059 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12060
12061 else
12062 Set_Ekind (Def_Id, E_Enumeration_Subtype);
12063 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
12064 Set_First_Literal (Def_Id, First_Literal (T));
12065 end if;
12066
12067 Set_Size_Info (Def_Id, (T));
12068 Set_RM_Size (Def_Id, RM_Size (T));
12069 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12070
12071 Set_Scalar_Range (Def_Id, R);
12072
12073 Set_Etype (S, Def_Id);
12074 Set_Discrete_RM_Size (Def_Id);
12075 end Constrain_Index;
12076
12077 -----------------------
12078 -- Constrain_Integer --
12079 -----------------------
12080
12081 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
12082 T : constant Entity_Id := Entity (Subtype_Mark (S));
12083 C : constant Node_Id := Constraint (S);
12084
12085 begin
12086 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12087
12088 if Is_Modular_Integer_Type (T) then
12089 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12090 else
12091 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12092 end if;
12093
12094 Set_Etype (Def_Id, Base_Type (T));
12095 Set_Size_Info (Def_Id, (T));
12096 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12097 Set_Discrete_RM_Size (Def_Id);
12098 end Constrain_Integer;
12099
12100 ------------------------------
12101 -- Constrain_Ordinary_Fixed --
12102 ------------------------------
12103
12104 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
12105 T : constant Entity_Id := Entity (Subtype_Mark (S));
12106 C : Node_Id;
12107 D : Node_Id;
12108 Rais : Node_Id;
12109
12110 begin
12111 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
12112 Set_Etype (Def_Id, Base_Type (T));
12113 Set_Size_Info (Def_Id, (T));
12114 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12115 Set_Small_Value (Def_Id, Small_Value (T));
12116
12117 -- Process the constraint
12118
12119 C := Constraint (S);
12120
12121 -- Delta constraint present
12122
12123 if Nkind (C) = N_Delta_Constraint then
12124
12125 Check_SPARK_Restriction ("delta constraint is not allowed", S);
12126 Check_Restriction (No_Obsolescent_Features, C);
12127
12128 if Warn_On_Obsolescent_Feature then
12129 Error_Msg_S
12130 ("subtype delta constraint is an " &
12131 "obsolescent feature (RM J.3(7))?j?");
12132 end if;
12133
12134 D := Delta_Expression (C);
12135 Analyze_And_Resolve (D, Any_Real);
12136 Check_Delta_Expression (D);
12137 Set_Delta_Value (Def_Id, Expr_Value_R (D));
12138
12139 -- Check that delta value is in range. Obviously we can do this
12140 -- at compile time, but it is strictly a runtime check, and of
12141 -- course there is an ACVC test that checks this!
12142
12143 if Delta_Value (Def_Id) < Delta_Value (T) then
12144 Error_Msg_N ("??delta value is too small", D);
12145 Rais :=
12146 Make_Raise_Constraint_Error (Sloc (D),
12147 Reason => CE_Range_Check_Failed);
12148 Insert_Action (Declaration_Node (Def_Id), Rais);
12149 end if;
12150
12151 C := Range_Constraint (C);
12152
12153 -- No delta constraint present
12154
12155 else
12156 Set_Delta_Value (Def_Id, Delta_Value (T));
12157 end if;
12158
12159 -- Range constraint present
12160
12161 if Nkind (C) = N_Range_Constraint then
12162 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12163
12164 -- No range constraint present
12165
12166 else
12167 pragma Assert (No (C));
12168 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12169
12170 end if;
12171
12172 Set_Discrete_RM_Size (Def_Id);
12173
12174 -- Unconditionally delay the freeze, since we cannot set size
12175 -- information in all cases correctly until the freeze point.
12176
12177 Set_Has_Delayed_Freeze (Def_Id);
12178 end Constrain_Ordinary_Fixed;
12179
12180 -----------------------
12181 -- Contain_Interface --
12182 -----------------------
12183
12184 function Contain_Interface
12185 (Iface : Entity_Id;
12186 Ifaces : Elist_Id) return Boolean
12187 is
12188 Iface_Elmt : Elmt_Id;
12189
12190 begin
12191 if Present (Ifaces) then
12192 Iface_Elmt := First_Elmt (Ifaces);
12193 while Present (Iface_Elmt) loop
12194 if Node (Iface_Elmt) = Iface then
12195 return True;
12196 end if;
12197
12198 Next_Elmt (Iface_Elmt);
12199 end loop;
12200 end if;
12201
12202 return False;
12203 end Contain_Interface;
12204
12205 ---------------------------
12206 -- Convert_Scalar_Bounds --
12207 ---------------------------
12208
12209 procedure Convert_Scalar_Bounds
12210 (N : Node_Id;
12211 Parent_Type : Entity_Id;
12212 Derived_Type : Entity_Id;
12213 Loc : Source_Ptr)
12214 is
12215 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
12216
12217 Lo : Node_Id;
12218 Hi : Node_Id;
12219 Rng : Node_Id;
12220
12221 begin
12222 -- Defend against previous errors
12223
12224 if No (Scalar_Range (Derived_Type)) then
12225 Check_Error_Detected;
12226 return;
12227 end if;
12228
12229 Lo := Build_Scalar_Bound
12230 (Type_Low_Bound (Derived_Type),
12231 Parent_Type, Implicit_Base);
12232
12233 Hi := Build_Scalar_Bound
12234 (Type_High_Bound (Derived_Type),
12235 Parent_Type, Implicit_Base);
12236
12237 Rng :=
12238 Make_Range (Loc,
12239 Low_Bound => Lo,
12240 High_Bound => Hi);
12241
12242 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12243
12244 Set_Parent (Rng, N);
12245 Set_Scalar_Range (Derived_Type, Rng);
12246
12247 -- Analyze the bounds
12248
12249 Analyze_And_Resolve (Lo, Implicit_Base);
12250 Analyze_And_Resolve (Hi, Implicit_Base);
12251
12252 -- Analyze the range itself, except that we do not analyze it if
12253 -- the bounds are real literals, and we have a fixed-point type.
12254 -- The reason for this is that we delay setting the bounds in this
12255 -- case till we know the final Small and Size values (see circuit
12256 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12257
12258 if Is_Fixed_Point_Type (Parent_Type)
12259 and then Nkind (Lo) = N_Real_Literal
12260 and then Nkind (Hi) = N_Real_Literal
12261 then
12262 return;
12263
12264 -- Here we do the analysis of the range
12265
12266 -- Note: we do this manually, since if we do a normal Analyze and
12267 -- Resolve call, there are problems with the conversions used for
12268 -- the derived type range.
12269
12270 else
12271 Set_Etype (Rng, Implicit_Base);
12272 Set_Analyzed (Rng, True);
12273 end if;
12274 end Convert_Scalar_Bounds;
12275
12276 -------------------
12277 -- Copy_And_Swap --
12278 -------------------
12279
12280 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
12281 begin
12282 -- Initialize new full declaration entity by copying the pertinent
12283 -- fields of the corresponding private declaration entity.
12284
12285 -- We temporarily set Ekind to a value appropriate for a type to
12286 -- avoid assert failures in Einfo from checking for setting type
12287 -- attributes on something that is not a type. Ekind (Priv) is an
12288 -- appropriate choice, since it allowed the attributes to be set
12289 -- in the first place. This Ekind value will be modified later.
12290
12291 Set_Ekind (Full, Ekind (Priv));
12292
12293 -- Also set Etype temporarily to Any_Type, again, in the absence
12294 -- of errors, it will be properly reset, and if there are errors,
12295 -- then we want a value of Any_Type to remain.
12296
12297 Set_Etype (Full, Any_Type);
12298
12299 -- Now start copying attributes
12300
12301 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12302
12303 if Has_Discriminants (Full) then
12304 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
12305 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
12306 end if;
12307
12308 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
12309 Set_Homonym (Full, Homonym (Priv));
12310 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12311 Set_Is_Public (Full, Is_Public (Priv));
12312 Set_Is_Pure (Full, Is_Pure (Priv));
12313 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
12314 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
12315 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12316 Set_Has_Pragma_Unreferenced_Objects
12317 (Full, Has_Pragma_Unreferenced_Objects
12318 (Priv));
12319
12320 Conditional_Delay (Full, Priv);
12321
12322 if Is_Tagged_Type (Full) then
12323 Set_Direct_Primitive_Operations (Full,
12324 Direct_Primitive_Operations (Priv));
12325
12326 if Is_Base_Type (Priv) then
12327 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12328 end if;
12329 end if;
12330
12331 Set_Is_Volatile (Full, Is_Volatile (Priv));
12332 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
12333 Set_Scope (Full, Scope (Priv));
12334 Set_Next_Entity (Full, Next_Entity (Priv));
12335 Set_First_Entity (Full, First_Entity (Priv));
12336 Set_Last_Entity (Full, Last_Entity (Priv));
12337
12338 -- If access types have been recorded for later handling, keep them in
12339 -- the full view so that they get handled when the full view freeze
12340 -- node is expanded.
12341
12342 if Present (Freeze_Node (Priv))
12343 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12344 then
12345 Ensure_Freeze_Node (Full);
12346 Set_Access_Types_To_Process
12347 (Freeze_Node (Full),
12348 Access_Types_To_Process (Freeze_Node (Priv)));
12349 end if;
12350
12351 -- Swap the two entities. Now Private is the full type entity and Full
12352 -- is the private one. They will be swapped back at the end of the
12353 -- private part. This swapping ensures that the entity that is visible
12354 -- in the private part is the full declaration.
12355
12356 Exchange_Entities (Priv, Full);
12357 Append_Entity (Full, Scope (Full));
12358 end Copy_And_Swap;
12359
12360 -------------------------------------
12361 -- Copy_Array_Base_Type_Attributes --
12362 -------------------------------------
12363
12364 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12365 begin
12366 Set_Component_Alignment (T1, Component_Alignment (T2));
12367 Set_Component_Type (T1, Component_Type (T2));
12368 Set_Component_Size (T1, Component_Size (T2));
12369 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
12370 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12371 Set_Has_Task (T1, Has_Task (T2));
12372 Set_Is_Packed (T1, Is_Packed (T2));
12373 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12374 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12375 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12376 end Copy_Array_Base_Type_Attributes;
12377
12378 -----------------------------------
12379 -- Copy_Array_Subtype_Attributes --
12380 -----------------------------------
12381
12382 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12383 begin
12384 Set_Size_Info (T1, T2);
12385
12386 Set_First_Index (T1, First_Index (T2));
12387 Set_Is_Aliased (T1, Is_Aliased (T2));
12388 Set_Is_Volatile (T1, Is_Volatile (T2));
12389 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12390 Set_Is_Constrained (T1, Is_Constrained (T2));
12391 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12392 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12393 Set_Convention (T1, Convention (T2));
12394 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12395 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12396 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12397 end Copy_Array_Subtype_Attributes;
12398
12399 -----------------------------------
12400 -- Create_Constrained_Components --
12401 -----------------------------------
12402
12403 procedure Create_Constrained_Components
12404 (Subt : Entity_Id;
12405 Decl_Node : Node_Id;
12406 Typ : Entity_Id;
12407 Constraints : Elist_Id)
12408 is
12409 Loc : constant Source_Ptr := Sloc (Subt);
12410 Comp_List : constant Elist_Id := New_Elmt_List;
12411 Parent_Type : constant Entity_Id := Etype (Typ);
12412 Assoc_List : constant List_Id := New_List;
12413 Discr_Val : Elmt_Id;
12414 Errors : Boolean;
12415 New_C : Entity_Id;
12416 Old_C : Entity_Id;
12417 Is_Static : Boolean := True;
12418
12419 procedure Collect_Fixed_Components (Typ : Entity_Id);
12420 -- Collect parent type components that do not appear in a variant part
12421
12422 procedure Create_All_Components;
12423 -- Iterate over Comp_List to create the components of the subtype
12424
12425 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12426 -- Creates a new component from Old_Compon, copying all the fields from
12427 -- it, including its Etype, inserts the new component in the Subt entity
12428 -- chain and returns the new component.
12429
12430 function Is_Variant_Record (T : Entity_Id) return Boolean;
12431 -- If true, and discriminants are static, collect only components from
12432 -- variants selected by discriminant values.
12433
12434 ------------------------------
12435 -- Collect_Fixed_Components --
12436 ------------------------------
12437
12438 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12439 begin
12440 -- Build association list for discriminants, and find components of the
12441 -- variant part selected by the values of the discriminants.
12442
12443 Old_C := First_Discriminant (Typ);
12444 Discr_Val := First_Elmt (Constraints);
12445 while Present (Old_C) loop
12446 Append_To (Assoc_List,
12447 Make_Component_Association (Loc,
12448 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12449 Expression => New_Copy (Node (Discr_Val))));
12450
12451 Next_Elmt (Discr_Val);
12452 Next_Discriminant (Old_C);
12453 end loop;
12454
12455 -- The tag and the possible parent component are unconditionally in
12456 -- the subtype.
12457
12458 if Is_Tagged_Type (Typ)
12459 or else Has_Controlled_Component (Typ)
12460 then
12461 Old_C := First_Component (Typ);
12462 while Present (Old_C) loop
12463 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
12464 Append_Elmt (Old_C, Comp_List);
12465 end if;
12466
12467 Next_Component (Old_C);
12468 end loop;
12469 end if;
12470 end Collect_Fixed_Components;
12471
12472 ---------------------------
12473 -- Create_All_Components --
12474 ---------------------------
12475
12476 procedure Create_All_Components is
12477 Comp : Elmt_Id;
12478
12479 begin
12480 Comp := First_Elmt (Comp_List);
12481 while Present (Comp) loop
12482 Old_C := Node (Comp);
12483 New_C := Create_Component (Old_C);
12484
12485 Set_Etype
12486 (New_C,
12487 Constrain_Component_Type
12488 (Old_C, Subt, Decl_Node, Typ, Constraints));
12489 Set_Is_Public (New_C, Is_Public (Subt));
12490
12491 Next_Elmt (Comp);
12492 end loop;
12493 end Create_All_Components;
12494
12495 ----------------------
12496 -- Create_Component --
12497 ----------------------
12498
12499 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12500 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12501
12502 begin
12503 if Ekind (Old_Compon) = E_Discriminant
12504 and then Is_Completely_Hidden (Old_Compon)
12505 then
12506 -- This is a shadow discriminant created for a discriminant of
12507 -- the parent type, which needs to be present in the subtype.
12508 -- Give the shadow discriminant an internal name that cannot
12509 -- conflict with that of visible components.
12510
12511 Set_Chars (New_Compon, New_Internal_Name ('C'));
12512 end if;
12513
12514 -- Set the parent so we have a proper link for freezing etc. This is
12515 -- not a real parent pointer, since of course our parent does not own
12516 -- up to us and reference us, we are an illegitimate child of the
12517 -- original parent!
12518
12519 Set_Parent (New_Compon, Parent (Old_Compon));
12520
12521 -- If the old component's Esize was already determined and is a
12522 -- static value, then the new component simply inherits it. Otherwise
12523 -- the old component's size may require run-time determination, but
12524 -- the new component's size still might be statically determinable
12525 -- (if, for example it has a static constraint). In that case we want
12526 -- Layout_Type to recompute the component's size, so we reset its
12527 -- size and positional fields.
12528
12529 if Frontend_Layout_On_Target
12530 and then not Known_Static_Esize (Old_Compon)
12531 then
12532 Set_Esize (New_Compon, Uint_0);
12533 Init_Normalized_First_Bit (New_Compon);
12534 Init_Normalized_Position (New_Compon);
12535 Init_Normalized_Position_Max (New_Compon);
12536 end if;
12537
12538 -- We do not want this node marked as Comes_From_Source, since
12539 -- otherwise it would get first class status and a separate cross-
12540 -- reference line would be generated. Illegitimate children do not
12541 -- rate such recognition.
12542
12543 Set_Comes_From_Source (New_Compon, False);
12544
12545 -- But it is a real entity, and a birth certificate must be properly
12546 -- registered by entering it into the entity list.
12547
12548 Enter_Name (New_Compon);
12549
12550 return New_Compon;
12551 end Create_Component;
12552
12553 -----------------------
12554 -- Is_Variant_Record --
12555 -----------------------
12556
12557 function Is_Variant_Record (T : Entity_Id) return Boolean is
12558 begin
12559 return Nkind (Parent (T)) = N_Full_Type_Declaration
12560 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12561 and then Present (Component_List (Type_Definition (Parent (T))))
12562 and then
12563 Present
12564 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12565 end Is_Variant_Record;
12566
12567 -- Start of processing for Create_Constrained_Components
12568
12569 begin
12570 pragma Assert (Subt /= Base_Type (Subt));
12571 pragma Assert (Typ = Base_Type (Typ));
12572
12573 Set_First_Entity (Subt, Empty);
12574 Set_Last_Entity (Subt, Empty);
12575
12576 -- Check whether constraint is fully static, in which case we can
12577 -- optimize the list of components.
12578
12579 Discr_Val := First_Elmt (Constraints);
12580 while Present (Discr_Val) loop
12581 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12582 Is_Static := False;
12583 exit;
12584 end if;
12585
12586 Next_Elmt (Discr_Val);
12587 end loop;
12588
12589 Set_Has_Static_Discriminants (Subt, Is_Static);
12590
12591 Push_Scope (Subt);
12592
12593 -- Inherit the discriminants of the parent type
12594
12595 Add_Discriminants : declare
12596 Num_Disc : Int;
12597 Num_Gird : Int;
12598
12599 begin
12600 Num_Disc := 0;
12601 Old_C := First_Discriminant (Typ);
12602
12603 while Present (Old_C) loop
12604 Num_Disc := Num_Disc + 1;
12605 New_C := Create_Component (Old_C);
12606 Set_Is_Public (New_C, Is_Public (Subt));
12607 Next_Discriminant (Old_C);
12608 end loop;
12609
12610 -- For an untagged derived subtype, the number of discriminants may
12611 -- be smaller than the number of inherited discriminants, because
12612 -- several of them may be renamed by a single new discriminant or
12613 -- constrained. In this case, add the hidden discriminants back into
12614 -- the subtype, because they need to be present if the optimizer of
12615 -- the GCC 4.x back-end decides to break apart assignments between
12616 -- objects using the parent view into member-wise assignments.
12617
12618 Num_Gird := 0;
12619
12620 if Is_Derived_Type (Typ)
12621 and then not Is_Tagged_Type (Typ)
12622 then
12623 Old_C := First_Stored_Discriminant (Typ);
12624
12625 while Present (Old_C) loop
12626 Num_Gird := Num_Gird + 1;
12627 Next_Stored_Discriminant (Old_C);
12628 end loop;
12629 end if;
12630
12631 if Num_Gird > Num_Disc then
12632
12633 -- Find out multiple uses of new discriminants, and add hidden
12634 -- components for the extra renamed discriminants. We recognize
12635 -- multiple uses through the Corresponding_Discriminant of a
12636 -- new discriminant: if it constrains several old discriminants,
12637 -- this field points to the last one in the parent type. The
12638 -- stored discriminants of the derived type have the same name
12639 -- as those of the parent.
12640
12641 declare
12642 Constr : Elmt_Id;
12643 New_Discr : Entity_Id;
12644 Old_Discr : Entity_Id;
12645
12646 begin
12647 Constr := First_Elmt (Stored_Constraint (Typ));
12648 Old_Discr := First_Stored_Discriminant (Typ);
12649 while Present (Constr) loop
12650 if Is_Entity_Name (Node (Constr))
12651 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12652 then
12653 New_Discr := Entity (Node (Constr));
12654
12655 if Chars (Corresponding_Discriminant (New_Discr)) /=
12656 Chars (Old_Discr)
12657 then
12658 -- The new discriminant has been used to rename a
12659 -- subsequent old discriminant. Introduce a shadow
12660 -- component for the current old discriminant.
12661
12662 New_C := Create_Component (Old_Discr);
12663 Set_Original_Record_Component (New_C, Old_Discr);
12664 end if;
12665
12666 else
12667 -- The constraint has eliminated the old discriminant.
12668 -- Introduce a shadow component.
12669
12670 New_C := Create_Component (Old_Discr);
12671 Set_Original_Record_Component (New_C, Old_Discr);
12672 end if;
12673
12674 Next_Elmt (Constr);
12675 Next_Stored_Discriminant (Old_Discr);
12676 end loop;
12677 end;
12678 end if;
12679 end Add_Discriminants;
12680
12681 if Is_Static
12682 and then Is_Variant_Record (Typ)
12683 then
12684 Collect_Fixed_Components (Typ);
12685
12686 Gather_Components (
12687 Typ,
12688 Component_List (Type_Definition (Parent (Typ))),
12689 Governed_By => Assoc_List,
12690 Into => Comp_List,
12691 Report_Errors => Errors);
12692 pragma Assert (not Errors);
12693
12694 Create_All_Components;
12695
12696 -- If the subtype declaration is created for a tagged type derivation
12697 -- with constraints, we retrieve the record definition of the parent
12698 -- type to select the components of the proper variant.
12699
12700 elsif Is_Static
12701 and then Is_Tagged_Type (Typ)
12702 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12703 and then
12704 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12705 and then Is_Variant_Record (Parent_Type)
12706 then
12707 Collect_Fixed_Components (Typ);
12708
12709 Gather_Components (
12710 Typ,
12711 Component_List (Type_Definition (Parent (Parent_Type))),
12712 Governed_By => Assoc_List,
12713 Into => Comp_List,
12714 Report_Errors => Errors);
12715 pragma Assert (not Errors);
12716
12717 -- If the tagged derivation has a type extension, collect all the
12718 -- new components therein.
12719
12720 if Present
12721 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12722 then
12723 Old_C := First_Component (Typ);
12724 while Present (Old_C) loop
12725 if Original_Record_Component (Old_C) = Old_C
12726 and then Chars (Old_C) /= Name_uTag
12727 and then Chars (Old_C) /= Name_uParent
12728 then
12729 Append_Elmt (Old_C, Comp_List);
12730 end if;
12731
12732 Next_Component (Old_C);
12733 end loop;
12734 end if;
12735
12736 Create_All_Components;
12737
12738 else
12739 -- If discriminants are not static, or if this is a multi-level type
12740 -- extension, we have to include all components of the parent type.
12741
12742 Old_C := First_Component (Typ);
12743 while Present (Old_C) loop
12744 New_C := Create_Component (Old_C);
12745
12746 Set_Etype
12747 (New_C,
12748 Constrain_Component_Type
12749 (Old_C, Subt, Decl_Node, Typ, Constraints));
12750 Set_Is_Public (New_C, Is_Public (Subt));
12751
12752 Next_Component (Old_C);
12753 end loop;
12754 end if;
12755
12756 End_Scope;
12757 end Create_Constrained_Components;
12758
12759 ------------------------------------------
12760 -- Decimal_Fixed_Point_Type_Declaration --
12761 ------------------------------------------
12762
12763 procedure Decimal_Fixed_Point_Type_Declaration
12764 (T : Entity_Id;
12765 Def : Node_Id)
12766 is
12767 Loc : constant Source_Ptr := Sloc (Def);
12768 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12769 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12770 Implicit_Base : Entity_Id;
12771 Digs_Val : Uint;
12772 Delta_Val : Ureal;
12773 Scale_Val : Uint;
12774 Bound_Val : Ureal;
12775
12776 begin
12777 Check_SPARK_Restriction
12778 ("decimal fixed point type is not allowed", Def);
12779 Check_Restriction (No_Fixed_Point, Def);
12780
12781 -- Create implicit base type
12782
12783 Implicit_Base :=
12784 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12785 Set_Etype (Implicit_Base, Implicit_Base);
12786
12787 -- Analyze and process delta expression
12788
12789 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12790
12791 Check_Delta_Expression (Delta_Expr);
12792 Delta_Val := Expr_Value_R (Delta_Expr);
12793
12794 -- Check delta is power of 10, and determine scale value from it
12795
12796 declare
12797 Val : Ureal;
12798
12799 begin
12800 Scale_Val := Uint_0;
12801 Val := Delta_Val;
12802
12803 if Val < Ureal_1 then
12804 while Val < Ureal_1 loop
12805 Val := Val * Ureal_10;
12806 Scale_Val := Scale_Val + 1;
12807 end loop;
12808
12809 if Scale_Val > 18 then
12810 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12811 Scale_Val := UI_From_Int (+18);
12812 end if;
12813
12814 else
12815 while Val > Ureal_1 loop
12816 Val := Val / Ureal_10;
12817 Scale_Val := Scale_Val - 1;
12818 end loop;
12819
12820 if Scale_Val < -18 then
12821 Error_Msg_N ("scale is less than minimum value of -18", Def);
12822 Scale_Val := UI_From_Int (-18);
12823 end if;
12824 end if;
12825
12826 if Val /= Ureal_1 then
12827 Error_Msg_N ("delta expression must be a power of 10", Def);
12828 Delta_Val := Ureal_10 ** (-Scale_Val);
12829 end if;
12830 end;
12831
12832 -- Set delta, scale and small (small = delta for decimal type)
12833
12834 Set_Delta_Value (Implicit_Base, Delta_Val);
12835 Set_Scale_Value (Implicit_Base, Scale_Val);
12836 Set_Small_Value (Implicit_Base, Delta_Val);
12837
12838 -- Analyze and process digits expression
12839
12840 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12841 Check_Digits_Expression (Digs_Expr);
12842 Digs_Val := Expr_Value (Digs_Expr);
12843
12844 if Digs_Val > 18 then
12845 Digs_Val := UI_From_Int (+18);
12846 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12847 end if;
12848
12849 Set_Digits_Value (Implicit_Base, Digs_Val);
12850 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12851
12852 -- Set range of base type from digits value for now. This will be
12853 -- expanded to represent the true underlying base range by Freeze.
12854
12855 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12856
12857 -- Note: We leave size as zero for now, size will be set at freeze
12858 -- time. We have to do this for ordinary fixed-point, because the size
12859 -- depends on the specified small, and we might as well do the same for
12860 -- decimal fixed-point.
12861
12862 pragma Assert (Esize (Implicit_Base) = Uint_0);
12863
12864 -- If there are bounds given in the declaration use them as the
12865 -- bounds of the first named subtype.
12866
12867 if Present (Real_Range_Specification (Def)) then
12868 declare
12869 RRS : constant Node_Id := Real_Range_Specification (Def);
12870 Low : constant Node_Id := Low_Bound (RRS);
12871 High : constant Node_Id := High_Bound (RRS);
12872 Low_Val : Ureal;
12873 High_Val : Ureal;
12874
12875 begin
12876 Analyze_And_Resolve (Low, Any_Real);
12877 Analyze_And_Resolve (High, Any_Real);
12878 Check_Real_Bound (Low);
12879 Check_Real_Bound (High);
12880 Low_Val := Expr_Value_R (Low);
12881 High_Val := Expr_Value_R (High);
12882
12883 if Low_Val < (-Bound_Val) then
12884 Error_Msg_N
12885 ("range low bound too small for digits value", Low);
12886 Low_Val := -Bound_Val;
12887 end if;
12888
12889 if High_Val > Bound_Val then
12890 Error_Msg_N
12891 ("range high bound too large for digits value", High);
12892 High_Val := Bound_Val;
12893 end if;
12894
12895 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12896 end;
12897
12898 -- If no explicit range, use range that corresponds to given
12899 -- digits value. This will end up as the final range for the
12900 -- first subtype.
12901
12902 else
12903 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12904 end if;
12905
12906 -- Complete entity for first subtype
12907
12908 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12909 Set_Etype (T, Implicit_Base);
12910 Set_Size_Info (T, Implicit_Base);
12911 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12912 Set_Digits_Value (T, Digs_Val);
12913 Set_Delta_Value (T, Delta_Val);
12914 Set_Small_Value (T, Delta_Val);
12915 Set_Scale_Value (T, Scale_Val);
12916 Set_Is_Constrained (T);
12917 end Decimal_Fixed_Point_Type_Declaration;
12918
12919 -----------------------------------
12920 -- Derive_Progenitor_Subprograms --
12921 -----------------------------------
12922
12923 procedure Derive_Progenitor_Subprograms
12924 (Parent_Type : Entity_Id;
12925 Tagged_Type : Entity_Id)
12926 is
12927 E : Entity_Id;
12928 Elmt : Elmt_Id;
12929 Iface : Entity_Id;
12930 Iface_Elmt : Elmt_Id;
12931 Iface_Subp : Entity_Id;
12932 New_Subp : Entity_Id := Empty;
12933 Prim_Elmt : Elmt_Id;
12934 Subp : Entity_Id;
12935 Typ : Entity_Id;
12936
12937 begin
12938 pragma Assert (Ada_Version >= Ada_2005
12939 and then Is_Record_Type (Tagged_Type)
12940 and then Is_Tagged_Type (Tagged_Type)
12941 and then Has_Interfaces (Tagged_Type));
12942
12943 -- Step 1: Transfer to the full-view primitives associated with the
12944 -- partial-view that cover interface primitives. Conceptually this
12945 -- work should be done later by Process_Full_View; done here to
12946 -- simplify its implementation at later stages. It can be safely
12947 -- done here because interfaces must be visible in the partial and
12948 -- private view (RM 7.3(7.3/2)).
12949
12950 -- Small optimization: This work is only required if the parent may
12951 -- have entities whose Alias attribute reference an interface primitive.
12952 -- Such a situation may occur if the parent is an abstract type and the
12953 -- primitive has not been yet overridden or if the parent is a generic
12954 -- formal type covering interfaces.
12955
12956 -- If the tagged type is not abstract, it cannot have abstract
12957 -- primitives (the only entities in the list of primitives of
12958 -- non-abstract tagged types that can reference abstract primitives
12959 -- through its Alias attribute are the internal entities that have
12960 -- attribute Interface_Alias, and these entities are generated later
12961 -- by Add_Internal_Interface_Entities).
12962
12963 if In_Private_Part (Current_Scope)
12964 and then (Is_Abstract_Type (Parent_Type)
12965 or else
12966 Is_Generic_Type (Parent_Type))
12967 then
12968 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12969 while Present (Elmt) loop
12970 Subp := Node (Elmt);
12971
12972 -- At this stage it is not possible to have entities in the list
12973 -- of primitives that have attribute Interface_Alias.
12974
12975 pragma Assert (No (Interface_Alias (Subp)));
12976
12977 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12978
12979 if Is_Interface (Typ) then
12980 E := Find_Primitive_Covering_Interface
12981 (Tagged_Type => Tagged_Type,
12982 Iface_Prim => Subp);
12983
12984 if Present (E)
12985 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12986 then
12987 Replace_Elmt (Elmt, E);
12988 Remove_Homonym (Subp);
12989 end if;
12990 end if;
12991
12992 Next_Elmt (Elmt);
12993 end loop;
12994 end if;
12995
12996 -- Step 2: Add primitives of progenitors that are not implemented by
12997 -- parents of Tagged_Type.
12998
12999 if Present (Interfaces (Base_Type (Tagged_Type))) then
13000 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
13001 while Present (Iface_Elmt) loop
13002 Iface := Node (Iface_Elmt);
13003
13004 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
13005 while Present (Prim_Elmt) loop
13006 Iface_Subp := Node (Prim_Elmt);
13007
13008 -- Exclude derivation of predefined primitives except those
13009 -- that come from source, or are inherited from one that comes
13010 -- from source. Required to catch declarations of equality
13011 -- operators of interfaces. For example:
13012
13013 -- type Iface is interface;
13014 -- function "=" (Left, Right : Iface) return Boolean;
13015
13016 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
13017 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
13018 then
13019 E := Find_Primitive_Covering_Interface
13020 (Tagged_Type => Tagged_Type,
13021 Iface_Prim => Iface_Subp);
13022
13023 -- If not found we derive a new primitive leaving its alias
13024 -- attribute referencing the interface primitive.
13025
13026 if No (E) then
13027 Derive_Subprogram
13028 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13029
13030 -- Ada 2012 (AI05-0197): If the covering primitive's name
13031 -- differs from the name of the interface primitive then it
13032 -- is a private primitive inherited from a parent type. In
13033 -- such case, given that Tagged_Type covers the interface,
13034 -- the inherited private primitive becomes visible. For such
13035 -- purpose we add a new entity that renames the inherited
13036 -- private primitive.
13037
13038 elsif Chars (E) /= Chars (Iface_Subp) then
13039 pragma Assert (Has_Suffix (E, 'P'));
13040 Derive_Subprogram
13041 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13042 Set_Alias (New_Subp, E);
13043 Set_Is_Abstract_Subprogram (New_Subp,
13044 Is_Abstract_Subprogram (E));
13045
13046 -- Propagate to the full view interface entities associated
13047 -- with the partial view.
13048
13049 elsif In_Private_Part (Current_Scope)
13050 and then Present (Alias (E))
13051 and then Alias (E) = Iface_Subp
13052 and then
13053 List_Containing (Parent (E)) /=
13054 Private_Declarations
13055 (Specification
13056 (Unit_Declaration_Node (Current_Scope)))
13057 then
13058 Append_Elmt (E, Primitive_Operations (Tagged_Type));
13059 end if;
13060 end if;
13061
13062 Next_Elmt (Prim_Elmt);
13063 end loop;
13064
13065 Next_Elmt (Iface_Elmt);
13066 end loop;
13067 end if;
13068 end Derive_Progenitor_Subprograms;
13069
13070 -----------------------
13071 -- Derive_Subprogram --
13072 -----------------------
13073
13074 procedure Derive_Subprogram
13075 (New_Subp : in out Entity_Id;
13076 Parent_Subp : Entity_Id;
13077 Derived_Type : Entity_Id;
13078 Parent_Type : Entity_Id;
13079 Actual_Subp : Entity_Id := Empty)
13080 is
13081 Formal : Entity_Id;
13082 -- Formal parameter of parent primitive operation
13083
13084 Formal_Of_Actual : Entity_Id;
13085 -- Formal parameter of actual operation, when the derivation is to
13086 -- create a renaming for a primitive operation of an actual in an
13087 -- instantiation.
13088
13089 New_Formal : Entity_Id;
13090 -- Formal of inherited operation
13091
13092 Visible_Subp : Entity_Id := Parent_Subp;
13093
13094 function Is_Private_Overriding return Boolean;
13095 -- If Subp is a private overriding of a visible operation, the inherited
13096 -- operation derives from the overridden op (even though its body is the
13097 -- overriding one) and the inherited operation is visible now. See
13098 -- sem_disp to see the full details of the handling of the overridden
13099 -- subprogram, which is removed from the list of primitive operations of
13100 -- the type. The overridden subprogram is saved locally in Visible_Subp,
13101 -- and used to diagnose abstract operations that need overriding in the
13102 -- derived type.
13103
13104 procedure Replace_Type (Id, New_Id : Entity_Id);
13105 -- When the type is an anonymous access type, create a new access type
13106 -- designating the derived type.
13107
13108 procedure Set_Derived_Name;
13109 -- This procedure sets the appropriate Chars name for New_Subp. This
13110 -- is normally just a copy of the parent name. An exception arises for
13111 -- type support subprograms, where the name is changed to reflect the
13112 -- name of the derived type, e.g. if type foo is derived from type bar,
13113 -- then a procedure barDA is derived with a name fooDA.
13114
13115 ---------------------------
13116 -- Is_Private_Overriding --
13117 ---------------------------
13118
13119 function Is_Private_Overriding return Boolean is
13120 Prev : Entity_Id;
13121
13122 begin
13123 -- If the parent is not a dispatching operation there is no
13124 -- need to investigate overridings
13125
13126 if not Is_Dispatching_Operation (Parent_Subp) then
13127 return False;
13128 end if;
13129
13130 -- The visible operation that is overridden is a homonym of the
13131 -- parent subprogram. We scan the homonym chain to find the one
13132 -- whose alias is the subprogram we are deriving.
13133
13134 Prev := Current_Entity (Parent_Subp);
13135 while Present (Prev) loop
13136 if Ekind (Prev) = Ekind (Parent_Subp)
13137 and then Alias (Prev) = Parent_Subp
13138 and then Scope (Parent_Subp) = Scope (Prev)
13139 and then not Is_Hidden (Prev)
13140 then
13141 Visible_Subp := Prev;
13142 return True;
13143 end if;
13144
13145 Prev := Homonym (Prev);
13146 end loop;
13147
13148 return False;
13149 end Is_Private_Overriding;
13150
13151 ------------------
13152 -- Replace_Type --
13153 ------------------
13154
13155 procedure Replace_Type (Id, New_Id : Entity_Id) is
13156 Acc_Type : Entity_Id;
13157 Par : constant Node_Id := Parent (Derived_Type);
13158
13159 begin
13160 -- When the type is an anonymous access type, create a new access
13161 -- type designating the derived type. This itype must be elaborated
13162 -- at the point of the derivation, not on subsequent calls that may
13163 -- be out of the proper scope for Gigi, so we insert a reference to
13164 -- it after the derivation.
13165
13166 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
13167 declare
13168 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
13169
13170 begin
13171 if Ekind (Desig_Typ) = E_Record_Type_With_Private
13172 and then Present (Full_View (Desig_Typ))
13173 and then not Is_Private_Type (Parent_Type)
13174 then
13175 Desig_Typ := Full_View (Desig_Typ);
13176 end if;
13177
13178 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
13179
13180 -- Ada 2005 (AI-251): Handle also derivations of abstract
13181 -- interface primitives.
13182
13183 or else (Is_Interface (Desig_Typ)
13184 and then not Is_Class_Wide_Type (Desig_Typ))
13185 then
13186 Acc_Type := New_Copy (Etype (Id));
13187 Set_Etype (Acc_Type, Acc_Type);
13188 Set_Scope (Acc_Type, New_Subp);
13189
13190 -- Compute size of anonymous access type
13191
13192 if Is_Array_Type (Desig_Typ)
13193 and then not Is_Constrained (Desig_Typ)
13194 then
13195 Init_Size (Acc_Type, 2 * System_Address_Size);
13196 else
13197 Init_Size (Acc_Type, System_Address_Size);
13198 end if;
13199
13200 Init_Alignment (Acc_Type);
13201 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
13202
13203 Set_Etype (New_Id, Acc_Type);
13204 Set_Scope (New_Id, New_Subp);
13205
13206 -- Create a reference to it
13207 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
13208
13209 else
13210 Set_Etype (New_Id, Etype (Id));
13211 end if;
13212 end;
13213
13214 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
13215 or else
13216 (Ekind (Etype (Id)) = E_Record_Type_With_Private
13217 and then Present (Full_View (Etype (Id)))
13218 and then
13219 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
13220 then
13221 -- Constraint checks on formals are generated during expansion,
13222 -- based on the signature of the original subprogram. The bounds
13223 -- of the derived type are not relevant, and thus we can use
13224 -- the base type for the formals. However, the return type may be
13225 -- used in a context that requires that the proper static bounds
13226 -- be used (a case statement, for example) and for those cases
13227 -- we must use the derived type (first subtype), not its base.
13228
13229 -- If the derived_type_definition has no constraints, we know that
13230 -- the derived type has the same constraints as the first subtype
13231 -- of the parent, and we can also use it rather than its base,
13232 -- which can lead to more efficient code.
13233
13234 if Etype (Id) = Parent_Type then
13235 if Is_Scalar_Type (Parent_Type)
13236 and then
13237 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13238 then
13239 Set_Etype (New_Id, Derived_Type);
13240
13241 elsif Nkind (Par) = N_Full_Type_Declaration
13242 and then
13243 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13244 and then
13245 Is_Entity_Name
13246 (Subtype_Indication (Type_Definition (Par)))
13247 then
13248 Set_Etype (New_Id, Derived_Type);
13249
13250 else
13251 Set_Etype (New_Id, Base_Type (Derived_Type));
13252 end if;
13253
13254 else
13255 Set_Etype (New_Id, Base_Type (Derived_Type));
13256 end if;
13257
13258 else
13259 Set_Etype (New_Id, Etype (Id));
13260 end if;
13261 end Replace_Type;
13262
13263 ----------------------
13264 -- Set_Derived_Name --
13265 ----------------------
13266
13267 procedure Set_Derived_Name is
13268 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13269 begin
13270 if Nm = TSS_Null then
13271 Set_Chars (New_Subp, Chars (Parent_Subp));
13272 else
13273 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13274 end if;
13275 end Set_Derived_Name;
13276
13277 -- Start of processing for Derive_Subprogram
13278
13279 begin
13280 New_Subp :=
13281 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13282 Set_Ekind (New_Subp, Ekind (Parent_Subp));
13283 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
13284
13285 -- Check whether the inherited subprogram is a private operation that
13286 -- should be inherited but not yet made visible. Such subprograms can
13287 -- become visible at a later point (e.g., the private part of a public
13288 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13289 -- following predicate is true, then this is not such a private
13290 -- operation and the subprogram simply inherits the name of the parent
13291 -- subprogram. Note the special check for the names of controlled
13292 -- operations, which are currently exempted from being inherited with
13293 -- a hidden name because they must be findable for generation of
13294 -- implicit run-time calls.
13295
13296 if not Is_Hidden (Parent_Subp)
13297 or else Is_Internal (Parent_Subp)
13298 or else Is_Private_Overriding
13299 or else Is_Internal_Name (Chars (Parent_Subp))
13300 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
13301 Name_Adjust,
13302 Name_Finalize)
13303 then
13304 Set_Derived_Name;
13305
13306 -- An inherited dispatching equality will be overridden by an internally
13307 -- generated one, or by an explicit one, so preserve its name and thus
13308 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13309 -- private operation it may become invisible if the full view has
13310 -- progenitors, and the dispatch table will be malformed.
13311 -- We check that the type is limited to handle the anomalous declaration
13312 -- of Limited_Controlled, which is derived from a non-limited type, and
13313 -- which is handled specially elsewhere as well.
13314
13315 elsif Chars (Parent_Subp) = Name_Op_Eq
13316 and then Is_Dispatching_Operation (Parent_Subp)
13317 and then Etype (Parent_Subp) = Standard_Boolean
13318 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
13319 and then
13320 Etype (First_Formal (Parent_Subp)) =
13321 Etype (Next_Formal (First_Formal (Parent_Subp)))
13322 then
13323 Set_Derived_Name;
13324
13325 -- If parent is hidden, this can be a regular derivation if the
13326 -- parent is immediately visible in a non-instantiating context,
13327 -- or if we are in the private part of an instance. This test
13328 -- should still be refined ???
13329
13330 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13331 -- operation as a non-visible operation in cases where the parent
13332 -- subprogram might not be visible now, but was visible within the
13333 -- original generic, so it would be wrong to make the inherited
13334 -- subprogram non-visible now. (Not clear if this test is fully
13335 -- correct; are there any cases where we should declare the inherited
13336 -- operation as not visible to avoid it being overridden, e.g., when
13337 -- the parent type is a generic actual with private primitives ???)
13338
13339 -- (they should be treated the same as other private inherited
13340 -- subprograms, but it's not clear how to do this cleanly). ???
13341
13342 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13343 and then Is_Immediately_Visible (Parent_Subp)
13344 and then not In_Instance)
13345 or else In_Instance_Not_Visible
13346 then
13347 Set_Derived_Name;
13348
13349 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13350 -- overrides an interface primitive because interface primitives
13351 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
13352
13353 elsif Ada_Version >= Ada_2005
13354 and then Is_Dispatching_Operation (Parent_Subp)
13355 and then Covers_Some_Interface (Parent_Subp)
13356 then
13357 Set_Derived_Name;
13358
13359 -- Otherwise, the type is inheriting a private operation, so enter
13360 -- it with a special name so it can't be overridden.
13361
13362 else
13363 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13364 end if;
13365
13366 Set_Parent (New_Subp, Parent (Derived_Type));
13367
13368 if Present (Actual_Subp) then
13369 Replace_Type (Actual_Subp, New_Subp);
13370 else
13371 Replace_Type (Parent_Subp, New_Subp);
13372 end if;
13373
13374 Conditional_Delay (New_Subp, Parent_Subp);
13375
13376 -- If we are creating a renaming for a primitive operation of an
13377 -- actual of a generic derived type, we must examine the signature
13378 -- of the actual primitive, not that of the generic formal, which for
13379 -- example may be an interface. However the name and initial value
13380 -- of the inherited operation are those of the formal primitive.
13381
13382 Formal := First_Formal (Parent_Subp);
13383
13384 if Present (Actual_Subp) then
13385 Formal_Of_Actual := First_Formal (Actual_Subp);
13386 else
13387 Formal_Of_Actual := Empty;
13388 end if;
13389
13390 while Present (Formal) loop
13391 New_Formal := New_Copy (Formal);
13392
13393 -- Normally we do not go copying parents, but in the case of
13394 -- formals, we need to link up to the declaration (which is the
13395 -- parameter specification), and it is fine to link up to the
13396 -- original formal's parameter specification in this case.
13397
13398 Set_Parent (New_Formal, Parent (Formal));
13399 Append_Entity (New_Formal, New_Subp);
13400
13401 if Present (Formal_Of_Actual) then
13402 Replace_Type (Formal_Of_Actual, New_Formal);
13403 Next_Formal (Formal_Of_Actual);
13404 else
13405 Replace_Type (Formal, New_Formal);
13406 end if;
13407
13408 Next_Formal (Formal);
13409 end loop;
13410
13411 -- If this derivation corresponds to a tagged generic actual, then
13412 -- primitive operations rename those of the actual. Otherwise the
13413 -- primitive operations rename those of the parent type, If the parent
13414 -- renames an intrinsic operator, so does the new subprogram. We except
13415 -- concatenation, which is always properly typed, and does not get
13416 -- expanded as other intrinsic operations.
13417
13418 if No (Actual_Subp) then
13419 if Is_Intrinsic_Subprogram (Parent_Subp) then
13420 Set_Is_Intrinsic_Subprogram (New_Subp);
13421
13422 if Present (Alias (Parent_Subp))
13423 and then Chars (Parent_Subp) /= Name_Op_Concat
13424 then
13425 Set_Alias (New_Subp, Alias (Parent_Subp));
13426 else
13427 Set_Alias (New_Subp, Parent_Subp);
13428 end if;
13429
13430 else
13431 Set_Alias (New_Subp, Parent_Subp);
13432 end if;
13433
13434 else
13435 Set_Alias (New_Subp, Actual_Subp);
13436 end if;
13437
13438 -- Derived subprograms of a tagged type must inherit the convention
13439 -- of the parent subprogram (a requirement of AI-117). Derived
13440 -- subprograms of untagged types simply get convention Ada by default.
13441
13442 -- If the derived type is a tagged generic formal type with unknown
13443 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
13444
13445 -- However, if the type is derived from a generic formal, the further
13446 -- inherited subprogram has the convention of the non-generic ancestor.
13447 -- Otherwise there would be no way to override the operation.
13448 -- (This is subject to forthcoming ARG discussions).
13449
13450 if Is_Tagged_Type (Derived_Type) then
13451 if Is_Generic_Type (Derived_Type)
13452 and then Has_Unknown_Discriminants (Derived_Type)
13453 then
13454 Set_Convention (New_Subp, Convention_Intrinsic);
13455
13456 else
13457 if Is_Generic_Type (Parent_Type)
13458 and then Has_Unknown_Discriminants (Parent_Type)
13459 then
13460 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
13461 else
13462 Set_Convention (New_Subp, Convention (Parent_Subp));
13463 end if;
13464 end if;
13465 end if;
13466
13467 -- Predefined controlled operations retain their name even if the parent
13468 -- is hidden (see above), but they are not primitive operations if the
13469 -- ancestor is not visible, for example if the parent is a private
13470 -- extension completed with a controlled extension. Note that a full
13471 -- type that is controlled can break privacy: the flag Is_Controlled is
13472 -- set on both views of the type.
13473
13474 if Is_Controlled (Parent_Type)
13475 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
13476 Name_Adjust,
13477 Name_Finalize)
13478 and then Is_Hidden (Parent_Subp)
13479 and then not Is_Visibly_Controlled (Parent_Type)
13480 then
13481 Set_Is_Hidden (New_Subp);
13482 end if;
13483
13484 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13485 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13486
13487 if Ekind (Parent_Subp) = E_Procedure then
13488 Set_Is_Valued_Procedure
13489 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13490 else
13491 Set_Has_Controlling_Result
13492 (New_Subp, Has_Controlling_Result (Parent_Subp));
13493 end if;
13494
13495 -- No_Return must be inherited properly. If this is overridden in the
13496 -- case of a dispatching operation, then a check is made in Sem_Disp
13497 -- that the overriding operation is also No_Return (no such check is
13498 -- required for the case of non-dispatching operation.
13499
13500 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13501
13502 -- A derived function with a controlling result is abstract. If the
13503 -- Derived_Type is a nonabstract formal generic derived type, then
13504 -- inherited operations are not abstract: the required check is done at
13505 -- instantiation time. If the derivation is for a generic actual, the
13506 -- function is not abstract unless the actual is.
13507
13508 if Is_Generic_Type (Derived_Type)
13509 and then not Is_Abstract_Type (Derived_Type)
13510 then
13511 null;
13512
13513 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13514 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13515
13516 elsif Ada_Version >= Ada_2005
13517 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13518 or else (Is_Tagged_Type (Derived_Type)
13519 and then Etype (New_Subp) = Derived_Type
13520 and then not Is_Null_Extension (Derived_Type))
13521 or else (Is_Tagged_Type (Derived_Type)
13522 and then Ekind (Etype (New_Subp)) =
13523 E_Anonymous_Access_Type
13524 and then Designated_Type (Etype (New_Subp)) =
13525 Derived_Type
13526 and then not Is_Null_Extension (Derived_Type)))
13527 and then No (Actual_Subp)
13528 then
13529 if not Is_Tagged_Type (Derived_Type)
13530 or else Is_Abstract_Type (Derived_Type)
13531 or else Is_Abstract_Subprogram (Alias (New_Subp))
13532 then
13533 Set_Is_Abstract_Subprogram (New_Subp);
13534 else
13535 Set_Requires_Overriding (New_Subp);
13536 end if;
13537
13538 elsif Ada_Version < Ada_2005
13539 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13540 or else (Is_Tagged_Type (Derived_Type)
13541 and then Etype (New_Subp) = Derived_Type
13542 and then No (Actual_Subp)))
13543 then
13544 Set_Is_Abstract_Subprogram (New_Subp);
13545
13546 -- AI05-0097 : an inherited operation that dispatches on result is
13547 -- abstract if the derived type is abstract, even if the parent type
13548 -- is concrete and the derived type is a null extension.
13549
13550 elsif Has_Controlling_Result (Alias (New_Subp))
13551 and then Is_Abstract_Type (Etype (New_Subp))
13552 then
13553 Set_Is_Abstract_Subprogram (New_Subp);
13554
13555 -- Finally, if the parent type is abstract we must verify that all
13556 -- inherited operations are either non-abstract or overridden, or that
13557 -- the derived type itself is abstract (this check is performed at the
13558 -- end of a package declaration, in Check_Abstract_Overriding). A
13559 -- private overriding in the parent type will not be visible in the
13560 -- derivation if we are not in an inner package or in a child unit of
13561 -- the parent type, in which case the abstractness of the inherited
13562 -- operation is carried to the new subprogram.
13563
13564 elsif Is_Abstract_Type (Parent_Type)
13565 and then not In_Open_Scopes (Scope (Parent_Type))
13566 and then Is_Private_Overriding
13567 and then Is_Abstract_Subprogram (Visible_Subp)
13568 then
13569 if No (Actual_Subp) then
13570 Set_Alias (New_Subp, Visible_Subp);
13571 Set_Is_Abstract_Subprogram (New_Subp, True);
13572
13573 else
13574 -- If this is a derivation for an instance of a formal derived
13575 -- type, abstractness comes from the primitive operation of the
13576 -- actual, not from the operation inherited from the ancestor.
13577
13578 Set_Is_Abstract_Subprogram
13579 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13580 end if;
13581 end if;
13582
13583 New_Overloaded_Entity (New_Subp, Derived_Type);
13584
13585 -- Check for case of a derived subprogram for the instantiation of a
13586 -- formal derived tagged type, if so mark the subprogram as dispatching
13587 -- and inherit the dispatching attributes of the actual subprogram. The
13588 -- derived subprogram is effectively renaming of the actual subprogram,
13589 -- so it needs to have the same attributes as the actual.
13590
13591 if Present (Actual_Subp)
13592 and then Is_Dispatching_Operation (Actual_Subp)
13593 then
13594 Set_Is_Dispatching_Operation (New_Subp);
13595
13596 if Present (DTC_Entity (Actual_Subp)) then
13597 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
13598 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
13599 end if;
13600 end if;
13601
13602 -- Indicate that a derived subprogram does not require a body and that
13603 -- it does not require processing of default expressions.
13604
13605 Set_Has_Completion (New_Subp);
13606 Set_Default_Expressions_Processed (New_Subp);
13607
13608 if Ekind (New_Subp) = E_Function then
13609 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13610 end if;
13611 end Derive_Subprogram;
13612
13613 ------------------------
13614 -- Derive_Subprograms --
13615 ------------------------
13616
13617 procedure Derive_Subprograms
13618 (Parent_Type : Entity_Id;
13619 Derived_Type : Entity_Id;
13620 Generic_Actual : Entity_Id := Empty)
13621 is
13622 Op_List : constant Elist_Id :=
13623 Collect_Primitive_Operations (Parent_Type);
13624
13625 function Check_Derived_Type return Boolean;
13626 -- Check that all the entities derived from Parent_Type are found in
13627 -- the list of primitives of Derived_Type exactly in the same order.
13628
13629 procedure Derive_Interface_Subprogram
13630 (New_Subp : in out Entity_Id;
13631 Subp : Entity_Id;
13632 Actual_Subp : Entity_Id);
13633 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13634 -- (which is an interface primitive). If Generic_Actual is present then
13635 -- Actual_Subp is the actual subprogram corresponding with the generic
13636 -- subprogram Subp.
13637
13638 function Check_Derived_Type return Boolean is
13639 E : Entity_Id;
13640 Elmt : Elmt_Id;
13641 List : Elist_Id;
13642 New_Subp : Entity_Id;
13643 Op_Elmt : Elmt_Id;
13644 Subp : Entity_Id;
13645
13646 begin
13647 -- Traverse list of entities in the current scope searching for
13648 -- an incomplete type whose full-view is derived type
13649
13650 E := First_Entity (Scope (Derived_Type));
13651 while Present (E) and then E /= Derived_Type loop
13652 if Ekind (E) = E_Incomplete_Type
13653 and then Present (Full_View (E))
13654 and then Full_View (E) = Derived_Type
13655 then
13656 -- Disable this test if Derived_Type completes an incomplete
13657 -- type because in such case more primitives can be added
13658 -- later to the list of primitives of Derived_Type by routine
13659 -- Process_Incomplete_Dependents
13660
13661 return True;
13662 end if;
13663
13664 E := Next_Entity (E);
13665 end loop;
13666
13667 List := Collect_Primitive_Operations (Derived_Type);
13668 Elmt := First_Elmt (List);
13669
13670 Op_Elmt := First_Elmt (Op_List);
13671 while Present (Op_Elmt) loop
13672 Subp := Node (Op_Elmt);
13673 New_Subp := Node (Elmt);
13674
13675 -- At this early stage Derived_Type has no entities with attribute
13676 -- Interface_Alias. In addition, such primitives are always
13677 -- located at the end of the list of primitives of Parent_Type.
13678 -- Therefore, if found we can safely stop processing pending
13679 -- entities.
13680
13681 exit when Present (Interface_Alias (Subp));
13682
13683 -- Handle hidden entities
13684
13685 if not Is_Predefined_Dispatching_Operation (Subp)
13686 and then Is_Hidden (Subp)
13687 then
13688 if Present (New_Subp)
13689 and then Primitive_Names_Match (Subp, New_Subp)
13690 then
13691 Next_Elmt (Elmt);
13692 end if;
13693
13694 else
13695 if not Present (New_Subp)
13696 or else Ekind (Subp) /= Ekind (New_Subp)
13697 or else not Primitive_Names_Match (Subp, New_Subp)
13698 then
13699 return False;
13700 end if;
13701
13702 Next_Elmt (Elmt);
13703 end if;
13704
13705 Next_Elmt (Op_Elmt);
13706 end loop;
13707
13708 return True;
13709 end Check_Derived_Type;
13710
13711 ---------------------------------
13712 -- Derive_Interface_Subprogram --
13713 ---------------------------------
13714
13715 procedure Derive_Interface_Subprogram
13716 (New_Subp : in out Entity_Id;
13717 Subp : Entity_Id;
13718 Actual_Subp : Entity_Id)
13719 is
13720 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13721 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13722
13723 begin
13724 pragma Assert (Is_Interface (Iface_Type));
13725
13726 Derive_Subprogram
13727 (New_Subp => New_Subp,
13728 Parent_Subp => Iface_Subp,
13729 Derived_Type => Derived_Type,
13730 Parent_Type => Iface_Type,
13731 Actual_Subp => Actual_Subp);
13732
13733 -- Given that this new interface entity corresponds with a primitive
13734 -- of the parent that was not overridden we must leave it associated
13735 -- with its parent primitive to ensure that it will share the same
13736 -- dispatch table slot when overridden.
13737
13738 if No (Actual_Subp) then
13739 Set_Alias (New_Subp, Subp);
13740
13741 -- For instantiations this is not needed since the previous call to
13742 -- Derive_Subprogram leaves the entity well decorated.
13743
13744 else
13745 pragma Assert (Alias (New_Subp) = Actual_Subp);
13746 null;
13747 end if;
13748 end Derive_Interface_Subprogram;
13749
13750 -- Local variables
13751
13752 Alias_Subp : Entity_Id;
13753 Act_List : Elist_Id;
13754 Act_Elmt : Elmt_Id;
13755 Act_Subp : Entity_Id := Empty;
13756 Elmt : Elmt_Id;
13757 Need_Search : Boolean := False;
13758 New_Subp : Entity_Id := Empty;
13759 Parent_Base : Entity_Id;
13760 Subp : Entity_Id;
13761
13762 -- Start of processing for Derive_Subprograms
13763
13764 begin
13765 if Ekind (Parent_Type) = E_Record_Type_With_Private
13766 and then Has_Discriminants (Parent_Type)
13767 and then Present (Full_View (Parent_Type))
13768 then
13769 Parent_Base := Full_View (Parent_Type);
13770 else
13771 Parent_Base := Parent_Type;
13772 end if;
13773
13774 if Present (Generic_Actual) then
13775 Act_List := Collect_Primitive_Operations (Generic_Actual);
13776 Act_Elmt := First_Elmt (Act_List);
13777 else
13778 Act_List := No_Elist;
13779 Act_Elmt := No_Elmt;
13780 end if;
13781
13782 -- Derive primitives inherited from the parent. Note that if the generic
13783 -- actual is present, this is not really a type derivation, it is a
13784 -- completion within an instance.
13785
13786 -- Case 1: Derived_Type does not implement interfaces
13787
13788 if not Is_Tagged_Type (Derived_Type)
13789 or else (not Has_Interfaces (Derived_Type)
13790 and then not (Present (Generic_Actual)
13791 and then Has_Interfaces (Generic_Actual)))
13792 then
13793 Elmt := First_Elmt (Op_List);
13794 while Present (Elmt) loop
13795 Subp := Node (Elmt);
13796
13797 -- Literals are derived earlier in the process of building the
13798 -- derived type, and are skipped here.
13799
13800 if Ekind (Subp) = E_Enumeration_Literal then
13801 null;
13802
13803 -- The actual is a direct descendant and the common primitive
13804 -- operations appear in the same order.
13805
13806 -- If the generic parent type is present, the derived type is an
13807 -- instance of a formal derived type, and within the instance its
13808 -- operations are those of the actual. We derive from the formal
13809 -- type but make the inherited operations aliases of the
13810 -- corresponding operations of the actual.
13811
13812 else
13813 pragma Assert (No (Node (Act_Elmt))
13814 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13815 and then
13816 Type_Conformant
13817 (Subp, Node (Act_Elmt),
13818 Skip_Controlling_Formals => True)));
13819
13820 Derive_Subprogram
13821 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13822
13823 if Present (Act_Elmt) then
13824 Next_Elmt (Act_Elmt);
13825 end if;
13826 end if;
13827
13828 Next_Elmt (Elmt);
13829 end loop;
13830
13831 -- Case 2: Derived_Type implements interfaces
13832
13833 else
13834 -- If the parent type has no predefined primitives we remove
13835 -- predefined primitives from the list of primitives of generic
13836 -- actual to simplify the complexity of this algorithm.
13837
13838 if Present (Generic_Actual) then
13839 declare
13840 Has_Predefined_Primitives : Boolean := False;
13841
13842 begin
13843 -- Check if the parent type has predefined primitives
13844
13845 Elmt := First_Elmt (Op_List);
13846 while Present (Elmt) loop
13847 Subp := Node (Elmt);
13848
13849 if Is_Predefined_Dispatching_Operation (Subp)
13850 and then not Comes_From_Source (Ultimate_Alias (Subp))
13851 then
13852 Has_Predefined_Primitives := True;
13853 exit;
13854 end if;
13855
13856 Next_Elmt (Elmt);
13857 end loop;
13858
13859 -- Remove predefined primitives of Generic_Actual. We must use
13860 -- an auxiliary list because in case of tagged types the value
13861 -- returned by Collect_Primitive_Operations is the value stored
13862 -- in its Primitive_Operations attribute (and we don't want to
13863 -- modify its current contents).
13864
13865 if not Has_Predefined_Primitives then
13866 declare
13867 Aux_List : constant Elist_Id := New_Elmt_List;
13868
13869 begin
13870 Elmt := First_Elmt (Act_List);
13871 while Present (Elmt) loop
13872 Subp := Node (Elmt);
13873
13874 if not Is_Predefined_Dispatching_Operation (Subp)
13875 or else Comes_From_Source (Subp)
13876 then
13877 Append_Elmt (Subp, Aux_List);
13878 end if;
13879
13880 Next_Elmt (Elmt);
13881 end loop;
13882
13883 Act_List := Aux_List;
13884 end;
13885 end if;
13886
13887 Act_Elmt := First_Elmt (Act_List);
13888 Act_Subp := Node (Act_Elmt);
13889 end;
13890 end if;
13891
13892 -- Stage 1: If the generic actual is not present we derive the
13893 -- primitives inherited from the parent type. If the generic parent
13894 -- type is present, the derived type is an instance of a formal
13895 -- derived type, and within the instance its operations are those of
13896 -- the actual. We derive from the formal type but make the inherited
13897 -- operations aliases of the corresponding operations of the actual.
13898
13899 Elmt := First_Elmt (Op_List);
13900 while Present (Elmt) loop
13901 Subp := Node (Elmt);
13902 Alias_Subp := Ultimate_Alias (Subp);
13903
13904 -- Do not derive internal entities of the parent that link
13905 -- interface primitives with their covering primitive. These
13906 -- entities will be added to this type when frozen.
13907
13908 if Present (Interface_Alias (Subp)) then
13909 goto Continue;
13910 end if;
13911
13912 -- If the generic actual is present find the corresponding
13913 -- operation in the generic actual. If the parent type is a
13914 -- direct ancestor of the derived type then, even if it is an
13915 -- interface, the operations are inherited from the primary
13916 -- dispatch table and are in the proper order. If we detect here
13917 -- that primitives are not in the same order we traverse the list
13918 -- of primitive operations of the actual to find the one that
13919 -- implements the interface primitive.
13920
13921 if Need_Search
13922 or else
13923 (Present (Generic_Actual)
13924 and then Present (Act_Subp)
13925 and then not
13926 (Primitive_Names_Match (Subp, Act_Subp)
13927 and then
13928 Type_Conformant (Subp, Act_Subp,
13929 Skip_Controlling_Formals => True)))
13930 then
13931 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
13932 Use_Full_View => True));
13933
13934 -- Remember that we need searching for all pending primitives
13935
13936 Need_Search := True;
13937
13938 -- Handle entities associated with interface primitives
13939
13940 if Present (Alias_Subp)
13941 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13942 and then not Is_Predefined_Dispatching_Operation (Subp)
13943 then
13944 -- Search for the primitive in the homonym chain
13945
13946 Act_Subp :=
13947 Find_Primitive_Covering_Interface
13948 (Tagged_Type => Generic_Actual,
13949 Iface_Prim => Alias_Subp);
13950
13951 -- Previous search may not locate primitives covering
13952 -- interfaces defined in generics units or instantiations.
13953 -- (it fails if the covering primitive has formals whose
13954 -- type is also defined in generics or instantiations).
13955 -- In such case we search in the list of primitives of the
13956 -- generic actual for the internal entity that links the
13957 -- interface primitive and the covering primitive.
13958
13959 if No (Act_Subp)
13960 and then Is_Generic_Type (Parent_Type)
13961 then
13962 -- This code has been designed to handle only generic
13963 -- formals that implement interfaces that are defined
13964 -- in a generic unit or instantiation. If this code is
13965 -- needed for other cases we must review it because
13966 -- (given that it relies on Original_Location to locate
13967 -- the primitive of Generic_Actual that covers the
13968 -- interface) it could leave linked through attribute
13969 -- Alias entities of unrelated instantiations).
13970
13971 pragma Assert
13972 (Is_Generic_Unit
13973 (Scope (Find_Dispatching_Type (Alias_Subp)))
13974 or else
13975 Instantiation_Depth
13976 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13977
13978 declare
13979 Iface_Prim_Loc : constant Source_Ptr :=
13980 Original_Location (Sloc (Alias_Subp));
13981
13982 Elmt : Elmt_Id;
13983 Prim : Entity_Id;
13984
13985 begin
13986 Elmt :=
13987 First_Elmt (Primitive_Operations (Generic_Actual));
13988
13989 Search : while Present (Elmt) loop
13990 Prim := Node (Elmt);
13991
13992 if Present (Interface_Alias (Prim))
13993 and then Original_Location
13994 (Sloc (Interface_Alias (Prim))) =
13995 Iface_Prim_Loc
13996 then
13997 Act_Subp := Alias (Prim);
13998 exit Search;
13999 end if;
14000
14001 Next_Elmt (Elmt);
14002 end loop Search;
14003 end;
14004 end if;
14005
14006 pragma Assert (Present (Act_Subp)
14007 or else Is_Abstract_Type (Generic_Actual)
14008 or else Serious_Errors_Detected > 0);
14009
14010 -- Handle predefined primitives plus the rest of user-defined
14011 -- primitives
14012
14013 else
14014 Act_Elmt := First_Elmt (Act_List);
14015 while Present (Act_Elmt) loop
14016 Act_Subp := Node (Act_Elmt);
14017
14018 exit when Primitive_Names_Match (Subp, Act_Subp)
14019 and then Type_Conformant
14020 (Subp, Act_Subp,
14021 Skip_Controlling_Formals => True)
14022 and then No (Interface_Alias (Act_Subp));
14023
14024 Next_Elmt (Act_Elmt);
14025 end loop;
14026
14027 if No (Act_Elmt) then
14028 Act_Subp := Empty;
14029 end if;
14030 end if;
14031 end if;
14032
14033 -- Case 1: If the parent is a limited interface then it has the
14034 -- predefined primitives of synchronized interfaces. However, the
14035 -- actual type may be a non-limited type and hence it does not
14036 -- have such primitives.
14037
14038 if Present (Generic_Actual)
14039 and then not Present (Act_Subp)
14040 and then Is_Limited_Interface (Parent_Base)
14041 and then Is_Predefined_Interface_Primitive (Subp)
14042 then
14043 null;
14044
14045 -- Case 2: Inherit entities associated with interfaces that were
14046 -- not covered by the parent type. We exclude here null interface
14047 -- primitives because they do not need special management.
14048
14049 -- We also exclude interface operations that are renamings. If the
14050 -- subprogram is an explicit renaming of an interface primitive,
14051 -- it is a regular primitive operation, and the presence of its
14052 -- alias is not relevant: it has to be derived like any other
14053 -- primitive.
14054
14055 elsif Present (Alias (Subp))
14056 and then Nkind (Unit_Declaration_Node (Subp)) /=
14057 N_Subprogram_Renaming_Declaration
14058 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
14059 and then not
14060 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
14061 and then Null_Present (Parent (Alias_Subp)))
14062 then
14063 -- If this is an abstract private type then we transfer the
14064 -- derivation of the interface primitive from the partial view
14065 -- to the full view. This is safe because all the interfaces
14066 -- must be visible in the partial view. Done to avoid adding
14067 -- a new interface derivation to the private part of the
14068 -- enclosing package; otherwise this new derivation would be
14069 -- decorated as hidden when the analysis of the enclosing
14070 -- package completes.
14071
14072 if Is_Abstract_Type (Derived_Type)
14073 and then In_Private_Part (Current_Scope)
14074 and then Has_Private_Declaration (Derived_Type)
14075 then
14076 declare
14077 Partial_View : Entity_Id;
14078 Elmt : Elmt_Id;
14079 Ent : Entity_Id;
14080
14081 begin
14082 Partial_View := First_Entity (Current_Scope);
14083 loop
14084 exit when No (Partial_View)
14085 or else (Has_Private_Declaration (Partial_View)
14086 and then
14087 Full_View (Partial_View) = Derived_Type);
14088
14089 Next_Entity (Partial_View);
14090 end loop;
14091
14092 -- If the partial view was not found then the source code
14093 -- has errors and the derivation is not needed.
14094
14095 if Present (Partial_View) then
14096 Elmt :=
14097 First_Elmt (Primitive_Operations (Partial_View));
14098 while Present (Elmt) loop
14099 Ent := Node (Elmt);
14100
14101 if Present (Alias (Ent))
14102 and then Ultimate_Alias (Ent) = Alias (Subp)
14103 then
14104 Append_Elmt
14105 (Ent, Primitive_Operations (Derived_Type));
14106 exit;
14107 end if;
14108
14109 Next_Elmt (Elmt);
14110 end loop;
14111
14112 -- If the interface primitive was not found in the
14113 -- partial view then this interface primitive was
14114 -- overridden. We add a derivation to activate in
14115 -- Derive_Progenitor_Subprograms the machinery to
14116 -- search for it.
14117
14118 if No (Elmt) then
14119 Derive_Interface_Subprogram
14120 (New_Subp => New_Subp,
14121 Subp => Subp,
14122 Actual_Subp => Act_Subp);
14123 end if;
14124 end if;
14125 end;
14126 else
14127 Derive_Interface_Subprogram
14128 (New_Subp => New_Subp,
14129 Subp => Subp,
14130 Actual_Subp => Act_Subp);
14131 end if;
14132
14133 -- Case 3: Common derivation
14134
14135 else
14136 Derive_Subprogram
14137 (New_Subp => New_Subp,
14138 Parent_Subp => Subp,
14139 Derived_Type => Derived_Type,
14140 Parent_Type => Parent_Base,
14141 Actual_Subp => Act_Subp);
14142 end if;
14143
14144 -- No need to update Act_Elm if we must search for the
14145 -- corresponding operation in the generic actual
14146
14147 if not Need_Search
14148 and then Present (Act_Elmt)
14149 then
14150 Next_Elmt (Act_Elmt);
14151 Act_Subp := Node (Act_Elmt);
14152 end if;
14153
14154 <<Continue>>
14155 Next_Elmt (Elmt);
14156 end loop;
14157
14158 -- Inherit additional operations from progenitors. If the derived
14159 -- type is a generic actual, there are not new primitive operations
14160 -- for the type because it has those of the actual, and therefore
14161 -- nothing needs to be done. The renamings generated above are not
14162 -- primitive operations, and their purpose is simply to make the
14163 -- proper operations visible within an instantiation.
14164
14165 if No (Generic_Actual) then
14166 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
14167 end if;
14168 end if;
14169
14170 -- Final check: Direct descendants must have their primitives in the
14171 -- same order. We exclude from this test untagged types and instances
14172 -- of formal derived types. We skip this test if we have already
14173 -- reported serious errors in the sources.
14174
14175 pragma Assert (not Is_Tagged_Type (Derived_Type)
14176 or else Present (Generic_Actual)
14177 or else Serious_Errors_Detected > 0
14178 or else Check_Derived_Type);
14179 end Derive_Subprograms;
14180
14181 --------------------------------
14182 -- Derived_Standard_Character --
14183 --------------------------------
14184
14185 procedure Derived_Standard_Character
14186 (N : Node_Id;
14187 Parent_Type : Entity_Id;
14188 Derived_Type : Entity_Id)
14189 is
14190 Loc : constant Source_Ptr := Sloc (N);
14191 Def : constant Node_Id := Type_Definition (N);
14192 Indic : constant Node_Id := Subtype_Indication (Def);
14193 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
14194 Implicit_Base : constant Entity_Id :=
14195 Create_Itype
14196 (E_Enumeration_Type, N, Derived_Type, 'B');
14197
14198 Lo : Node_Id;
14199 Hi : Node_Id;
14200
14201 begin
14202 Discard_Node (Process_Subtype (Indic, N));
14203
14204 Set_Etype (Implicit_Base, Parent_Base);
14205 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
14206 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
14207
14208 Set_Is_Character_Type (Implicit_Base, True);
14209 Set_Has_Delayed_Freeze (Implicit_Base);
14210
14211 -- The bounds of the implicit base are the bounds of the parent base.
14212 -- Note that their type is the parent base.
14213
14214 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
14215 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
14216
14217 Set_Scalar_Range (Implicit_Base,
14218 Make_Range (Loc,
14219 Low_Bound => Lo,
14220 High_Bound => Hi));
14221
14222 Conditional_Delay (Derived_Type, Parent_Type);
14223
14224 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
14225 Set_Etype (Derived_Type, Implicit_Base);
14226 Set_Size_Info (Derived_Type, Parent_Type);
14227
14228 if Unknown_RM_Size (Derived_Type) then
14229 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
14230 end if;
14231
14232 Set_Is_Character_Type (Derived_Type, True);
14233
14234 if Nkind (Indic) /= N_Subtype_Indication then
14235
14236 -- If no explicit constraint, the bounds are those
14237 -- of the parent type.
14238
14239 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
14240 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
14241 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
14242 end if;
14243
14244 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
14245
14246 -- Because the implicit base is used in the conversion of the bounds, we
14247 -- have to freeze it now. This is similar to what is done for numeric
14248 -- types, and it equally suspicious, but otherwise a non-static bound
14249 -- will have a reference to an unfrozen type, which is rejected by Gigi
14250 -- (???). This requires specific care for definition of stream
14251 -- attributes. For details, see comments at the end of
14252 -- Build_Derived_Numeric_Type.
14253
14254 Freeze_Before (N, Implicit_Base);
14255 end Derived_Standard_Character;
14256
14257 ------------------------------
14258 -- Derived_Type_Declaration --
14259 ------------------------------
14260
14261 procedure Derived_Type_Declaration
14262 (T : Entity_Id;
14263 N : Node_Id;
14264 Is_Completion : Boolean)
14265 is
14266 Parent_Type : Entity_Id;
14267
14268 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14269 -- Check whether the parent type is a generic formal, or derives
14270 -- directly or indirectly from one.
14271
14272 ------------------------
14273 -- Comes_From_Generic --
14274 ------------------------
14275
14276 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14277 begin
14278 if Is_Generic_Type (Typ) then
14279 return True;
14280
14281 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14282 return True;
14283
14284 elsif Is_Private_Type (Typ)
14285 and then Present (Full_View (Typ))
14286 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14287 then
14288 return True;
14289
14290 elsif Is_Generic_Actual_Type (Typ) then
14291 return True;
14292
14293 else
14294 return False;
14295 end if;
14296 end Comes_From_Generic;
14297
14298 -- Local variables
14299
14300 Def : constant Node_Id := Type_Definition (N);
14301 Iface_Def : Node_Id;
14302 Indic : constant Node_Id := Subtype_Indication (Def);
14303 Extension : constant Node_Id := Record_Extension_Part (Def);
14304 Parent_Node : Node_Id;
14305 Taggd : Boolean;
14306
14307 -- Start of processing for Derived_Type_Declaration
14308
14309 begin
14310 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14311
14312 -- Ada 2005 (AI-251): In case of interface derivation check that the
14313 -- parent is also an interface.
14314
14315 if Interface_Present (Def) then
14316 Check_SPARK_Restriction ("interface is not allowed", Def);
14317
14318 if not Is_Interface (Parent_Type) then
14319 Diagnose_Interface (Indic, Parent_Type);
14320
14321 else
14322 Parent_Node := Parent (Base_Type (Parent_Type));
14323 Iface_Def := Type_Definition (Parent_Node);
14324
14325 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14326 -- other limited interfaces.
14327
14328 if Limited_Present (Def) then
14329 if Limited_Present (Iface_Def) then
14330 null;
14331
14332 elsif Protected_Present (Iface_Def) then
14333 Error_Msg_NE
14334 ("descendant of& must be declared"
14335 & " as a protected interface",
14336 N, Parent_Type);
14337
14338 elsif Synchronized_Present (Iface_Def) then
14339 Error_Msg_NE
14340 ("descendant of& must be declared"
14341 & " as a synchronized interface",
14342 N, Parent_Type);
14343
14344 elsif Task_Present (Iface_Def) then
14345 Error_Msg_NE
14346 ("descendant of& must be declared as a task interface",
14347 N, Parent_Type);
14348
14349 else
14350 Error_Msg_N
14351 ("(Ada 2005) limited interface cannot "
14352 & "inherit from non-limited interface", Indic);
14353 end if;
14354
14355 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14356 -- from non-limited or limited interfaces.
14357
14358 elsif not Protected_Present (Def)
14359 and then not Synchronized_Present (Def)
14360 and then not Task_Present (Def)
14361 then
14362 if Limited_Present (Iface_Def) then
14363 null;
14364
14365 elsif Protected_Present (Iface_Def) then
14366 Error_Msg_NE
14367 ("descendant of& must be declared"
14368 & " as a protected interface",
14369 N, Parent_Type);
14370
14371 elsif Synchronized_Present (Iface_Def) then
14372 Error_Msg_NE
14373 ("descendant of& must be declared"
14374 & " as a synchronized interface",
14375 N, Parent_Type);
14376
14377 elsif Task_Present (Iface_Def) then
14378 Error_Msg_NE
14379 ("descendant of& must be declared as a task interface",
14380 N, Parent_Type);
14381 else
14382 null;
14383 end if;
14384 end if;
14385 end if;
14386 end if;
14387
14388 if Is_Tagged_Type (Parent_Type)
14389 and then Is_Concurrent_Type (Parent_Type)
14390 and then not Is_Interface (Parent_Type)
14391 then
14392 Error_Msg_N
14393 ("parent type of a record extension cannot be "
14394 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14395 Set_Etype (T, Any_Type);
14396 return;
14397 end if;
14398
14399 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14400 -- interfaces
14401
14402 if Is_Tagged_Type (Parent_Type)
14403 and then Is_Non_Empty_List (Interface_List (Def))
14404 then
14405 declare
14406 Intf : Node_Id;
14407 T : Entity_Id;
14408
14409 begin
14410 Intf := First (Interface_List (Def));
14411 while Present (Intf) loop
14412 T := Find_Type_Of_Subtype_Indic (Intf);
14413
14414 if not Is_Interface (T) then
14415 Diagnose_Interface (Intf, T);
14416
14417 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14418 -- a limited type from having a nonlimited progenitor.
14419
14420 elsif (Limited_Present (Def)
14421 or else (not Is_Interface (Parent_Type)
14422 and then Is_Limited_Type (Parent_Type)))
14423 and then not Is_Limited_Interface (T)
14424 then
14425 Error_Msg_NE
14426 ("progenitor interface& of limited type must be limited",
14427 N, T);
14428 end if;
14429
14430 Next (Intf);
14431 end loop;
14432 end;
14433 end if;
14434
14435 if Parent_Type = Any_Type
14436 or else Etype (Parent_Type) = Any_Type
14437 or else (Is_Class_Wide_Type (Parent_Type)
14438 and then Etype (Parent_Type) = T)
14439 then
14440 -- If Parent_Type is undefined or illegal, make new type into a
14441 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14442 -- errors. If this is a self-definition, emit error now.
14443
14444 if T = Parent_Type
14445 or else T = Etype (Parent_Type)
14446 then
14447 Error_Msg_N ("type cannot be used in its own definition", Indic);
14448 end if;
14449
14450 Set_Ekind (T, Ekind (Parent_Type));
14451 Set_Etype (T, Any_Type);
14452 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14453
14454 if Is_Tagged_Type (T)
14455 and then Is_Record_Type (T)
14456 then
14457 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14458 end if;
14459
14460 return;
14461 end if;
14462
14463 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14464 -- an interface is special because the list of interfaces in the full
14465 -- view can be given in any order. For example:
14466
14467 -- type A is interface;
14468 -- type B is interface and A;
14469 -- type D is new B with private;
14470 -- private
14471 -- type D is new A and B with null record; -- 1 --
14472
14473 -- In this case we perform the following transformation of -1-:
14474
14475 -- type D is new B and A with null record;
14476
14477 -- If the parent of the full-view covers the parent of the partial-view
14478 -- we have two possible cases:
14479
14480 -- 1) They have the same parent
14481 -- 2) The parent of the full-view implements some further interfaces
14482
14483 -- In both cases we do not need to perform the transformation. In the
14484 -- first case the source program is correct and the transformation is
14485 -- not needed; in the second case the source program does not fulfill
14486 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14487 -- later.
14488
14489 -- This transformation not only simplifies the rest of the analysis of
14490 -- this type declaration but also simplifies the correct generation of
14491 -- the object layout to the expander.
14492
14493 if In_Private_Part (Current_Scope)
14494 and then Is_Interface (Parent_Type)
14495 then
14496 declare
14497 Iface : Node_Id;
14498 Partial_View : Entity_Id;
14499 Partial_View_Parent : Entity_Id;
14500 New_Iface : Node_Id;
14501
14502 begin
14503 -- Look for the associated private type declaration
14504
14505 Partial_View := First_Entity (Current_Scope);
14506 loop
14507 exit when No (Partial_View)
14508 or else (Has_Private_Declaration (Partial_View)
14509 and then Full_View (Partial_View) = T);
14510
14511 Next_Entity (Partial_View);
14512 end loop;
14513
14514 -- If the partial view was not found then the source code has
14515 -- errors and the transformation is not needed.
14516
14517 if Present (Partial_View) then
14518 Partial_View_Parent := Etype (Partial_View);
14519
14520 -- If the parent of the full-view covers the parent of the
14521 -- partial-view we have nothing else to do.
14522
14523 if Interface_Present_In_Ancestor
14524 (Parent_Type, Partial_View_Parent)
14525 then
14526 null;
14527
14528 -- Traverse the list of interfaces of the full-view to look
14529 -- for the parent of the partial-view and perform the tree
14530 -- transformation.
14531
14532 else
14533 Iface := First (Interface_List (Def));
14534 while Present (Iface) loop
14535 if Etype (Iface) = Etype (Partial_View) then
14536 Rewrite (Subtype_Indication (Def),
14537 New_Copy (Subtype_Indication
14538 (Parent (Partial_View))));
14539
14540 New_Iface :=
14541 Make_Identifier (Sloc (N), Chars (Parent_Type));
14542 Append (New_Iface, Interface_List (Def));
14543
14544 -- Analyze the transformed code
14545
14546 Derived_Type_Declaration (T, N, Is_Completion);
14547 return;
14548 end if;
14549
14550 Next (Iface);
14551 end loop;
14552 end if;
14553 end if;
14554 end;
14555 end if;
14556
14557 -- Only composite types other than array types are allowed to have
14558 -- discriminants. In SPARK, no types are allowed to have discriminants.
14559
14560 if Present (Discriminant_Specifications (N)) then
14561 if (Is_Elementary_Type (Parent_Type)
14562 or else Is_Array_Type (Parent_Type))
14563 and then not Error_Posted (N)
14564 then
14565 Error_Msg_N
14566 ("elementary or array type cannot have discriminants",
14567 Defining_Identifier (First (Discriminant_Specifications (N))));
14568 Set_Has_Discriminants (T, False);
14569 else
14570 Check_SPARK_Restriction ("discriminant type is not allowed", N);
14571 end if;
14572 end if;
14573
14574 -- In Ada 83, a derived type defined in a package specification cannot
14575 -- be used for further derivation until the end of its visible part.
14576 -- Note that derivation in the private part of the package is allowed.
14577
14578 if Ada_Version = Ada_83
14579 and then Is_Derived_Type (Parent_Type)
14580 and then In_Visible_Part (Scope (Parent_Type))
14581 then
14582 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14583 Error_Msg_N
14584 ("(Ada 83): premature use of type for derivation", Indic);
14585 end if;
14586 end if;
14587
14588 -- Check for early use of incomplete or private type
14589
14590 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14591 Error_Msg_N ("premature derivation of incomplete type", Indic);
14592 return;
14593
14594 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14595 and then not Comes_From_Generic (Parent_Type))
14596 or else Has_Private_Component (Parent_Type)
14597 then
14598 -- The ancestor type of a formal type can be incomplete, in which
14599 -- case only the operations of the partial view are available in the
14600 -- generic. Subsequent checks may be required when the full view is
14601 -- analyzed to verify that a derivation from a tagged type has an
14602 -- extension.
14603
14604 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14605 null;
14606
14607 elsif No (Underlying_Type (Parent_Type))
14608 or else Has_Private_Component (Parent_Type)
14609 then
14610 Error_Msg_N
14611 ("premature derivation of derived or private type", Indic);
14612
14613 -- Flag the type itself as being in error, this prevents some
14614 -- nasty problems with subsequent uses of the malformed type.
14615
14616 Set_Error_Posted (T);
14617
14618 -- Check that within the immediate scope of an untagged partial
14619 -- view it's illegal to derive from the partial view if the
14620 -- full view is tagged. (7.3(7))
14621
14622 -- We verify that the Parent_Type is a partial view by checking
14623 -- that it is not a Full_Type_Declaration (i.e. a private type or
14624 -- private extension declaration), to distinguish a partial view
14625 -- from a derivation from a private type which also appears as
14626 -- E_Private_Type. If the parent base type is not declared in an
14627 -- enclosing scope there is no need to check.
14628
14629 elsif Present (Full_View (Parent_Type))
14630 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14631 and then not Is_Tagged_Type (Parent_Type)
14632 and then Is_Tagged_Type (Full_View (Parent_Type))
14633 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14634 then
14635 Error_Msg_N
14636 ("premature derivation from type with tagged full view",
14637 Indic);
14638 end if;
14639 end if;
14640
14641 -- Check that form of derivation is appropriate
14642
14643 Taggd := Is_Tagged_Type (Parent_Type);
14644
14645 -- Perhaps the parent type should be changed to the class-wide type's
14646 -- specific type in this case to prevent cascading errors ???
14647
14648 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14649 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14650 return;
14651 end if;
14652
14653 if Present (Extension) and then not Taggd then
14654 Error_Msg_N
14655 ("type derived from untagged type cannot have extension", Indic);
14656
14657 elsif No (Extension) and then Taggd then
14658
14659 -- If this declaration is within a private part (or body) of a
14660 -- generic instantiation then the derivation is allowed (the parent
14661 -- type can only appear tagged in this case if it's a generic actual
14662 -- type, since it would otherwise have been rejected in the analysis
14663 -- of the generic template).
14664
14665 if not Is_Generic_Actual_Type (Parent_Type)
14666 or else In_Visible_Part (Scope (Parent_Type))
14667 then
14668 if Is_Class_Wide_Type (Parent_Type) then
14669 Error_Msg_N
14670 ("parent type must not be a class-wide type", Indic);
14671
14672 -- Use specific type to prevent cascaded errors.
14673
14674 Parent_Type := Etype (Parent_Type);
14675
14676 else
14677 Error_Msg_N
14678 ("type derived from tagged type must have extension", Indic);
14679 end if;
14680 end if;
14681 end if;
14682
14683 -- AI-443: Synchronized formal derived types require a private
14684 -- extension. There is no point in checking the ancestor type or
14685 -- the progenitors since the construct is wrong to begin with.
14686
14687 if Ada_Version >= Ada_2005
14688 and then Is_Generic_Type (T)
14689 and then Present (Original_Node (N))
14690 then
14691 declare
14692 Decl : constant Node_Id := Original_Node (N);
14693
14694 begin
14695 if Nkind (Decl) = N_Formal_Type_Declaration
14696 and then Nkind (Formal_Type_Definition (Decl)) =
14697 N_Formal_Derived_Type_Definition
14698 and then Synchronized_Present (Formal_Type_Definition (Decl))
14699 and then No (Extension)
14700
14701 -- Avoid emitting a duplicate error message
14702
14703 and then not Error_Posted (Indic)
14704 then
14705 Error_Msg_N
14706 ("synchronized derived type must have extension", N);
14707 end if;
14708 end;
14709 end if;
14710
14711 if Null_Exclusion_Present (Def)
14712 and then not Is_Access_Type (Parent_Type)
14713 then
14714 Error_Msg_N ("null exclusion can only apply to an access type", N);
14715 end if;
14716
14717 -- Avoid deriving parent primitives of underlying record views
14718
14719 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14720 Derive_Subps => not Is_Underlying_Record_View (T));
14721
14722 -- AI-419: The parent type of an explicitly limited derived type must
14723 -- be a limited type or a limited interface.
14724
14725 if Limited_Present (Def) then
14726 Set_Is_Limited_Record (T);
14727
14728 if Is_Interface (T) then
14729 Set_Is_Limited_Interface (T);
14730 end if;
14731
14732 if not Is_Limited_Type (Parent_Type)
14733 and then
14734 (not Is_Interface (Parent_Type)
14735 or else not Is_Limited_Interface (Parent_Type))
14736 then
14737 -- AI05-0096: a derivation in the private part of an instance is
14738 -- legal if the generic formal is untagged limited, and the actual
14739 -- is non-limited.
14740
14741 if Is_Generic_Actual_Type (Parent_Type)
14742 and then In_Private_Part (Current_Scope)
14743 and then
14744 not Is_Tagged_Type
14745 (Generic_Parent_Type (Parent (Parent_Type)))
14746 then
14747 null;
14748
14749 else
14750 Error_Msg_NE
14751 ("parent type& of limited type must be limited",
14752 N, Parent_Type);
14753 end if;
14754 end if;
14755 end if;
14756
14757 -- In SPARK, there are no derived type definitions other than type
14758 -- extensions of tagged record types.
14759
14760 if No (Extension) then
14761 Check_SPARK_Restriction ("derived type is not allowed", N);
14762 end if;
14763 end Derived_Type_Declaration;
14764
14765 ------------------------
14766 -- Diagnose_Interface --
14767 ------------------------
14768
14769 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14770 begin
14771 if not Is_Interface (E)
14772 and then E /= Any_Type
14773 then
14774 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14775 end if;
14776 end Diagnose_Interface;
14777
14778 ----------------------------------
14779 -- Enumeration_Type_Declaration --
14780 ----------------------------------
14781
14782 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14783 Ev : Uint;
14784 L : Node_Id;
14785 R_Node : Node_Id;
14786 B_Node : Node_Id;
14787
14788 begin
14789 -- Create identifier node representing lower bound
14790
14791 B_Node := New_Node (N_Identifier, Sloc (Def));
14792 L := First (Literals (Def));
14793 Set_Chars (B_Node, Chars (L));
14794 Set_Entity (B_Node, L);
14795 Set_Etype (B_Node, T);
14796 Set_Is_Static_Expression (B_Node, True);
14797
14798 R_Node := New_Node (N_Range, Sloc (Def));
14799 Set_Low_Bound (R_Node, B_Node);
14800
14801 Set_Ekind (T, E_Enumeration_Type);
14802 Set_First_Literal (T, L);
14803 Set_Etype (T, T);
14804 Set_Is_Constrained (T);
14805
14806 Ev := Uint_0;
14807
14808 -- Loop through literals of enumeration type setting pos and rep values
14809 -- except that if the Ekind is already set, then it means the literal
14810 -- was already constructed (case of a derived type declaration and we
14811 -- should not disturb the Pos and Rep values.
14812
14813 while Present (L) loop
14814 if Ekind (L) /= E_Enumeration_Literal then
14815 Set_Ekind (L, E_Enumeration_Literal);
14816 Set_Enumeration_Pos (L, Ev);
14817 Set_Enumeration_Rep (L, Ev);
14818 Set_Is_Known_Valid (L, True);
14819 end if;
14820
14821 Set_Etype (L, T);
14822 New_Overloaded_Entity (L);
14823 Generate_Definition (L);
14824 Set_Convention (L, Convention_Intrinsic);
14825
14826 -- Case of character literal
14827
14828 if Nkind (L) = N_Defining_Character_Literal then
14829 Set_Is_Character_Type (T, True);
14830
14831 -- Check violation of No_Wide_Characters
14832
14833 if Restriction_Check_Required (No_Wide_Characters) then
14834 Get_Name_String (Chars (L));
14835
14836 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14837 Check_Restriction (No_Wide_Characters, L);
14838 end if;
14839 end if;
14840 end if;
14841
14842 Ev := Ev + 1;
14843 Next (L);
14844 end loop;
14845
14846 -- Now create a node representing upper bound
14847
14848 B_Node := New_Node (N_Identifier, Sloc (Def));
14849 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14850 Set_Entity (B_Node, Last (Literals (Def)));
14851 Set_Etype (B_Node, T);
14852 Set_Is_Static_Expression (B_Node, True);
14853
14854 Set_High_Bound (R_Node, B_Node);
14855
14856 -- Initialize various fields of the type. Some of this information
14857 -- may be overwritten later through rep.clauses.
14858
14859 Set_Scalar_Range (T, R_Node);
14860 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14861 Set_Enum_Esize (T);
14862 Set_Enum_Pos_To_Rep (T, Empty);
14863
14864 -- Set Discard_Names if configuration pragma set, or if there is
14865 -- a parameterless pragma in the current declarative region
14866
14867 if Global_Discard_Names or else Discard_Names (Scope (T)) then
14868 Set_Discard_Names (T);
14869 end if;
14870
14871 -- Process end label if there is one
14872
14873 if Present (Def) then
14874 Process_End_Label (Def, 'e', T);
14875 end if;
14876 end Enumeration_Type_Declaration;
14877
14878 ---------------------------------
14879 -- Expand_To_Stored_Constraint --
14880 ---------------------------------
14881
14882 function Expand_To_Stored_Constraint
14883 (Typ : Entity_Id;
14884 Constraint : Elist_Id) return Elist_Id
14885 is
14886 Explicitly_Discriminated_Type : Entity_Id;
14887 Expansion : Elist_Id;
14888 Discriminant : Entity_Id;
14889
14890 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14891 -- Find the nearest type that actually specifies discriminants
14892
14893 ---------------------------------
14894 -- Type_With_Explicit_Discrims --
14895 ---------------------------------
14896
14897 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14898 Typ : constant E := Base_Type (Id);
14899
14900 begin
14901 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14902 if Present (Full_View (Typ)) then
14903 return Type_With_Explicit_Discrims (Full_View (Typ));
14904 end if;
14905
14906 else
14907 if Has_Discriminants (Typ) then
14908 return Typ;
14909 end if;
14910 end if;
14911
14912 if Etype (Typ) = Typ then
14913 return Empty;
14914 elsif Has_Discriminants (Typ) then
14915 return Typ;
14916 else
14917 return Type_With_Explicit_Discrims (Etype (Typ));
14918 end if;
14919
14920 end Type_With_Explicit_Discrims;
14921
14922 -- Start of processing for Expand_To_Stored_Constraint
14923
14924 begin
14925 if No (Constraint)
14926 or else Is_Empty_Elmt_List (Constraint)
14927 then
14928 return No_Elist;
14929 end if;
14930
14931 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14932
14933 if No (Explicitly_Discriminated_Type) then
14934 return No_Elist;
14935 end if;
14936
14937 Expansion := New_Elmt_List;
14938
14939 Discriminant :=
14940 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14941 while Present (Discriminant) loop
14942 Append_Elmt (
14943 Get_Discriminant_Value (
14944 Discriminant, Explicitly_Discriminated_Type, Constraint),
14945 Expansion);
14946 Next_Stored_Discriminant (Discriminant);
14947 end loop;
14948
14949 return Expansion;
14950 end Expand_To_Stored_Constraint;
14951
14952 ---------------------------
14953 -- Find_Hidden_Interface --
14954 ---------------------------
14955
14956 function Find_Hidden_Interface
14957 (Src : Elist_Id;
14958 Dest : Elist_Id) return Entity_Id
14959 is
14960 Iface : Entity_Id;
14961 Iface_Elmt : Elmt_Id;
14962
14963 begin
14964 if Present (Src) and then Present (Dest) then
14965 Iface_Elmt := First_Elmt (Src);
14966 while Present (Iface_Elmt) loop
14967 Iface := Node (Iface_Elmt);
14968
14969 if Is_Interface (Iface)
14970 and then not Contain_Interface (Iface, Dest)
14971 then
14972 return Iface;
14973 end if;
14974
14975 Next_Elmt (Iface_Elmt);
14976 end loop;
14977 end if;
14978
14979 return Empty;
14980 end Find_Hidden_Interface;
14981
14982 --------------------
14983 -- Find_Type_Name --
14984 --------------------
14985
14986 function Find_Type_Name (N : Node_Id) return Entity_Id is
14987 Id : constant Entity_Id := Defining_Identifier (N);
14988 Prev : Entity_Id;
14989 New_Id : Entity_Id;
14990 Prev_Par : Node_Id;
14991
14992 procedure Check_Duplicate_Aspects;
14993 -- Check that aspects specified in a completion have not been specified
14994 -- already in the partial view. Type_Invariant and others can be
14995 -- specified on either view but never on both.
14996
14997 procedure Tag_Mismatch;
14998 -- Diagnose a tagged partial view whose full view is untagged.
14999 -- We post the message on the full view, with a reference to
15000 -- the previous partial view. The partial view can be private
15001 -- or incomplete, and these are handled in a different manner,
15002 -- so we determine the position of the error message from the
15003 -- respective slocs of both.
15004
15005 -----------------------------
15006 -- Check_Duplicate_Aspects --
15007 -----------------------------
15008 procedure Check_Duplicate_Aspects is
15009 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
15010 Full_Aspects : constant List_Id := Aspect_Specifications (N);
15011 F_Spec, P_Spec : Node_Id;
15012
15013 begin
15014 if Present (Prev_Aspects) and then Present (Full_Aspects) then
15015 F_Spec := First (Full_Aspects);
15016 while Present (F_Spec) loop
15017 P_Spec := First (Prev_Aspects);
15018 while Present (P_Spec) loop
15019 if
15020 Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
15021 then
15022 Error_Msg_N
15023 ("aspect already specified in private declaration",
15024 F_Spec);
15025 Remove (F_Spec);
15026 return;
15027 end if;
15028
15029 Next (P_Spec);
15030 end loop;
15031
15032 Next (F_Spec);
15033 end loop;
15034 end if;
15035 end Check_Duplicate_Aspects;
15036
15037 ------------------
15038 -- Tag_Mismatch --
15039 ------------------
15040
15041 procedure Tag_Mismatch is
15042 begin
15043 if Sloc (Prev) < Sloc (Id) then
15044 if Ada_Version >= Ada_2012
15045 and then Nkind (N) = N_Private_Type_Declaration
15046 then
15047 Error_Msg_NE
15048 ("declaration of private } must be a tagged type ", Id, Prev);
15049 else
15050 Error_Msg_NE
15051 ("full declaration of } must be a tagged type ", Id, Prev);
15052 end if;
15053 else
15054 if Ada_Version >= Ada_2012
15055 and then Nkind (N) = N_Private_Type_Declaration
15056 then
15057 Error_Msg_NE
15058 ("declaration of private } must be a tagged type ", Prev, Id);
15059 else
15060 Error_Msg_NE
15061 ("full declaration of } must be a tagged type ", Prev, Id);
15062 end if;
15063 end if;
15064 end Tag_Mismatch;
15065
15066 -- Start of processing for Find_Type_Name
15067
15068 begin
15069 -- Find incomplete declaration, if one was given
15070
15071 Prev := Current_Entity_In_Scope (Id);
15072
15073 -- New type declaration
15074
15075 if No (Prev) then
15076 Enter_Name (Id);
15077 return Id;
15078
15079 -- Previous declaration exists
15080
15081 else
15082 Prev_Par := Parent (Prev);
15083
15084 -- Error if not incomplete/private case except if previous
15085 -- declaration is implicit, etc. Enter_Name will emit error if
15086 -- appropriate.
15087
15088 if not Is_Incomplete_Or_Private_Type (Prev) then
15089 Enter_Name (Id);
15090 New_Id := Id;
15091
15092 -- Check invalid completion of private or incomplete type
15093
15094 elsif not Nkind_In (N, N_Full_Type_Declaration,
15095 N_Task_Type_Declaration,
15096 N_Protected_Type_Declaration)
15097 and then
15098 (Ada_Version < Ada_2012
15099 or else not Is_Incomplete_Type (Prev)
15100 or else not Nkind_In (N, N_Private_Type_Declaration,
15101 N_Private_Extension_Declaration))
15102 then
15103 -- Completion must be a full type declarations (RM 7.3(4))
15104
15105 Error_Msg_Sloc := Sloc (Prev);
15106 Error_Msg_NE ("invalid completion of }", Id, Prev);
15107
15108 -- Set scope of Id to avoid cascaded errors. Entity is never
15109 -- examined again, except when saving globals in generics.
15110
15111 Set_Scope (Id, Current_Scope);
15112 New_Id := Id;
15113
15114 -- If this is a repeated incomplete declaration, no further
15115 -- checks are possible.
15116
15117 if Nkind (N) = N_Incomplete_Type_Declaration then
15118 return Prev;
15119 end if;
15120
15121 -- Case of full declaration of incomplete type
15122
15123 elsif Ekind (Prev) = E_Incomplete_Type
15124 and then (Ada_Version < Ada_2012
15125 or else No (Full_View (Prev))
15126 or else not Is_Private_Type (Full_View (Prev)))
15127 then
15128
15129 -- Indicate that the incomplete declaration has a matching full
15130 -- declaration. The defining occurrence of the incomplete
15131 -- declaration remains the visible one, and the procedure
15132 -- Get_Full_View dereferences it whenever the type is used.
15133
15134 if Present (Full_View (Prev)) then
15135 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15136 end if;
15137
15138 Set_Full_View (Prev, Id);
15139 Append_Entity (Id, Current_Scope);
15140 Set_Is_Public (Id, Is_Public (Prev));
15141 Set_Is_Internal (Id);
15142 New_Id := Prev;
15143
15144 -- If the incomplete view is tagged, a class_wide type has been
15145 -- created already. Use it for the private type as well, in order
15146 -- to prevent multiple incompatible class-wide types that may be
15147 -- created for self-referential anonymous access components.
15148
15149 if Is_Tagged_Type (Prev)
15150 and then Present (Class_Wide_Type (Prev))
15151 then
15152 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
15153 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
15154
15155 -- If the incomplete type is completed by a private declaration
15156 -- the class-wide type remains associated with the incomplete
15157 -- type, to prevent order-of-elaboration issues in gigi, else
15158 -- we associate the class-wide type with the known full view.
15159
15160 if Nkind (N) /= N_Private_Type_Declaration then
15161 Set_Etype (Class_Wide_Type (Id), Id);
15162 end if;
15163 end if;
15164
15165 -- Case of full declaration of private type
15166
15167 else
15168 -- If the private type was a completion of an incomplete type then
15169 -- update Prev to reference the private type
15170
15171 if Ada_Version >= Ada_2012
15172 and then Ekind (Prev) = E_Incomplete_Type
15173 and then Present (Full_View (Prev))
15174 and then Is_Private_Type (Full_View (Prev))
15175 then
15176 Prev := Full_View (Prev);
15177 Prev_Par := Parent (Prev);
15178 end if;
15179
15180 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
15181 if Etype (Prev) /= Prev then
15182
15183 -- Prev is a private subtype or a derived type, and needs
15184 -- no completion.
15185
15186 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15187 New_Id := Id;
15188
15189 elsif Ekind (Prev) = E_Private_Type
15190 and then Nkind_In (N, N_Task_Type_Declaration,
15191 N_Protected_Type_Declaration)
15192 then
15193 Error_Msg_N
15194 ("completion of nonlimited type cannot be limited", N);
15195
15196 elsif Ekind (Prev) = E_Record_Type_With_Private
15197 and then Nkind_In (N, N_Task_Type_Declaration,
15198 N_Protected_Type_Declaration)
15199 then
15200 if not Is_Limited_Record (Prev) then
15201 Error_Msg_N
15202 ("completion of nonlimited type cannot be limited", N);
15203
15204 elsif No (Interface_List (N)) then
15205 Error_Msg_N
15206 ("completion of tagged private type must be tagged",
15207 N);
15208 end if;
15209
15210 elsif Nkind (N) = N_Full_Type_Declaration
15211 and then
15212 Nkind (Type_Definition (N)) = N_Record_Definition
15213 and then Interface_Present (Type_Definition (N))
15214 then
15215 Error_Msg_N
15216 ("completion of private type cannot be an interface", N);
15217 end if;
15218
15219 -- Ada 2005 (AI-251): Private extension declaration of a task
15220 -- type or a protected type. This case arises when covering
15221 -- interface types.
15222
15223 elsif Nkind_In (N, N_Task_Type_Declaration,
15224 N_Protected_Type_Declaration)
15225 then
15226 null;
15227
15228 elsif Nkind (N) /= N_Full_Type_Declaration
15229 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
15230 then
15231 Error_Msg_N
15232 ("full view of private extension must be an extension", N);
15233
15234 elsif not (Abstract_Present (Parent (Prev)))
15235 and then Abstract_Present (Type_Definition (N))
15236 then
15237 Error_Msg_N
15238 ("full view of non-abstract extension cannot be abstract", N);
15239 end if;
15240
15241 if not In_Private_Part (Current_Scope) then
15242 Error_Msg_N
15243 ("declaration of full view must appear in private part", N);
15244 end if;
15245
15246 if Ada_Version >= Ada_2012 then
15247 Check_Duplicate_Aspects;
15248 end if;
15249
15250 Copy_And_Swap (Prev, Id);
15251 Set_Has_Private_Declaration (Prev);
15252 Set_Has_Private_Declaration (Id);
15253
15254 -- Preserve aspect and iterator flags that may have been set on
15255 -- the partial view.
15256
15257 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
15258 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
15259
15260 -- If no error, propagate freeze_node from private to full view.
15261 -- It may have been generated for an early operational item.
15262
15263 if Present (Freeze_Node (Id))
15264 and then Serious_Errors_Detected = 0
15265 and then No (Full_View (Id))
15266 then
15267 Set_Freeze_Node (Prev, Freeze_Node (Id));
15268 Set_Freeze_Node (Id, Empty);
15269 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
15270 end if;
15271
15272 Set_Full_View (Id, Prev);
15273 New_Id := Prev;
15274 end if;
15275
15276 -- Verify that full declaration conforms to partial one
15277
15278 if Is_Incomplete_Or_Private_Type (Prev)
15279 and then Present (Discriminant_Specifications (Prev_Par))
15280 then
15281 if Present (Discriminant_Specifications (N)) then
15282 if Ekind (Prev) = E_Incomplete_Type then
15283 Check_Discriminant_Conformance (N, Prev, Prev);
15284 else
15285 Check_Discriminant_Conformance (N, Prev, Id);
15286 end if;
15287
15288 else
15289 Error_Msg_N
15290 ("missing discriminants in full type declaration", N);
15291
15292 -- To avoid cascaded errors on subsequent use, share the
15293 -- discriminants of the partial view.
15294
15295 Set_Discriminant_Specifications (N,
15296 Discriminant_Specifications (Prev_Par));
15297 end if;
15298 end if;
15299
15300 -- A prior untagged partial view can have an associated class-wide
15301 -- type due to use of the class attribute, and in this case the full
15302 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15303 -- of incomplete tagged declarations, but we check for it.
15304
15305 if Is_Type (Prev)
15306 and then (Is_Tagged_Type (Prev)
15307 or else Present (Class_Wide_Type (Prev)))
15308 then
15309 -- Ada 2012 (AI05-0162): A private type may be the completion of
15310 -- an incomplete type
15311
15312 if Ada_Version >= Ada_2012
15313 and then Is_Incomplete_Type (Prev)
15314 and then Nkind_In (N, N_Private_Type_Declaration,
15315 N_Private_Extension_Declaration)
15316 then
15317 -- No need to check private extensions since they are tagged
15318
15319 if Nkind (N) = N_Private_Type_Declaration
15320 and then not Tagged_Present (N)
15321 then
15322 Tag_Mismatch;
15323 end if;
15324
15325 -- The full declaration is either a tagged type (including
15326 -- a synchronized type that implements interfaces) or a
15327 -- type extension, otherwise this is an error.
15328
15329 elsif Nkind_In (N, N_Task_Type_Declaration,
15330 N_Protected_Type_Declaration)
15331 then
15332 if No (Interface_List (N))
15333 and then not Error_Posted (N)
15334 then
15335 Tag_Mismatch;
15336 end if;
15337
15338 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15339
15340 -- Indicate that the previous declaration (tagged incomplete
15341 -- or private declaration) requires the same on the full one.
15342
15343 if not Tagged_Present (Type_Definition (N)) then
15344 Tag_Mismatch;
15345 Set_Is_Tagged_Type (Id);
15346 end if;
15347
15348 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15349 if No (Record_Extension_Part (Type_Definition (N))) then
15350 Error_Msg_NE
15351 ("full declaration of } must be a record extension",
15352 Prev, Id);
15353
15354 -- Set some attributes to produce a usable full view
15355
15356 Set_Is_Tagged_Type (Id);
15357 end if;
15358
15359 else
15360 Tag_Mismatch;
15361 end if;
15362 end if;
15363
15364 if Present (Prev)
15365 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15366 and then Present (Premature_Use (Parent (Prev)))
15367 then
15368 Error_Msg_Sloc := Sloc (N);
15369 Error_Msg_N
15370 ("\full declaration #", Premature_Use (Parent (Prev)));
15371 end if;
15372
15373 return New_Id;
15374 end if;
15375 end Find_Type_Name;
15376
15377 -------------------------
15378 -- Find_Type_Of_Object --
15379 -------------------------
15380
15381 function Find_Type_Of_Object
15382 (Obj_Def : Node_Id;
15383 Related_Nod : Node_Id) return Entity_Id
15384 is
15385 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
15386 P : Node_Id := Parent (Obj_Def);
15387 T : Entity_Id;
15388 Nam : Name_Id;
15389
15390 begin
15391 -- If the parent is a component_definition node we climb to the
15392 -- component_declaration node
15393
15394 if Nkind (P) = N_Component_Definition then
15395 P := Parent (P);
15396 end if;
15397
15398 -- Case of an anonymous array subtype
15399
15400 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15401 N_Unconstrained_Array_Definition)
15402 then
15403 T := Empty;
15404 Array_Type_Declaration (T, Obj_Def);
15405
15406 -- Create an explicit subtype whenever possible
15407
15408 elsif Nkind (P) /= N_Component_Declaration
15409 and then Def_Kind = N_Subtype_Indication
15410 then
15411 -- Base name of subtype on object name, which will be unique in
15412 -- the current scope.
15413
15414 -- If this is a duplicate declaration, return base type, to avoid
15415 -- generating duplicate anonymous types.
15416
15417 if Error_Posted (P) then
15418 Analyze (Subtype_Mark (Obj_Def));
15419 return Entity (Subtype_Mark (Obj_Def));
15420 end if;
15421
15422 Nam :=
15423 New_External_Name
15424 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15425
15426 T := Make_Defining_Identifier (Sloc (P), Nam);
15427
15428 Insert_Action (Obj_Def,
15429 Make_Subtype_Declaration (Sloc (P),
15430 Defining_Identifier => T,
15431 Subtype_Indication => Relocate_Node (Obj_Def)));
15432
15433 -- This subtype may need freezing, and this will not be done
15434 -- automatically if the object declaration is not in declarative
15435 -- part. Since this is an object declaration, the type cannot always
15436 -- be frozen here. Deferred constants do not freeze their type
15437 -- (which often enough will be private).
15438
15439 if Nkind (P) = N_Object_Declaration
15440 and then Constant_Present (P)
15441 and then No (Expression (P))
15442 then
15443 null;
15444 else
15445 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
15446 end if;
15447
15448 -- Ada 2005 AI-406: the object definition in an object declaration
15449 -- can be an access definition.
15450
15451 elsif Def_Kind = N_Access_Definition then
15452 T := Access_Definition (Related_Nod, Obj_Def);
15453
15454 Set_Is_Local_Anonymous_Access
15455 (T,
15456 V => (Ada_Version < Ada_2012)
15457 or else (Nkind (P) /= N_Object_Declaration)
15458 or else Is_Library_Level_Entity (Defining_Identifier (P)));
15459
15460 -- Otherwise, the object definition is just a subtype_mark
15461
15462 else
15463 T := Process_Subtype (Obj_Def, Related_Nod);
15464
15465 -- If expansion is disabled an object definition that is an aggregate
15466 -- will not get expanded and may lead to scoping problems in the back
15467 -- end, if the object is referenced in an inner scope. In that case
15468 -- create an itype reference for the object definition now. This
15469 -- may be redundant in some cases, but harmless.
15470
15471 if Is_Itype (T)
15472 and then Nkind (Related_Nod) = N_Object_Declaration
15473 and then ASIS_Mode
15474 then
15475 Build_Itype_Reference (T, Related_Nod);
15476 end if;
15477 end if;
15478
15479 return T;
15480 end Find_Type_Of_Object;
15481
15482 --------------------------------
15483 -- Find_Type_Of_Subtype_Indic --
15484 --------------------------------
15485
15486 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15487 Typ : Entity_Id;
15488
15489 begin
15490 -- Case of subtype mark with a constraint
15491
15492 if Nkind (S) = N_Subtype_Indication then
15493 Find_Type (Subtype_Mark (S));
15494 Typ := Entity (Subtype_Mark (S));
15495
15496 if not
15497 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15498 then
15499 Error_Msg_N
15500 ("incorrect constraint for this kind of type", Constraint (S));
15501 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15502 end if;
15503
15504 -- Otherwise we have a subtype mark without a constraint
15505
15506 elsif Error_Posted (S) then
15507 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15508 return Any_Type;
15509
15510 else
15511 Find_Type (S);
15512 Typ := Entity (S);
15513 end if;
15514
15515 -- Check No_Wide_Characters restriction
15516
15517 Check_Wide_Character_Restriction (Typ, S);
15518
15519 return Typ;
15520 end Find_Type_Of_Subtype_Indic;
15521
15522 -------------------------------------
15523 -- Floating_Point_Type_Declaration --
15524 -------------------------------------
15525
15526 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15527 Digs : constant Node_Id := Digits_Expression (Def);
15528 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15529 Digs_Val : Uint;
15530 Base_Typ : Entity_Id;
15531 Implicit_Base : Entity_Id;
15532 Bound : Node_Id;
15533
15534 function Can_Derive_From (E : Entity_Id) return Boolean;
15535 -- Find if given digits value, and possibly a specified range, allows
15536 -- derivation from specified type
15537
15538 function Find_Base_Type return Entity_Id;
15539 -- Find a predefined base type that Def can derive from, or generate
15540 -- an error and substitute Long_Long_Float if none exists.
15541
15542 ---------------------
15543 -- Can_Derive_From --
15544 ---------------------
15545
15546 function Can_Derive_From (E : Entity_Id) return Boolean is
15547 Spec : constant Entity_Id := Real_Range_Specification (Def);
15548
15549 begin
15550 -- Check specified "digits" constraint
15551
15552 if Digs_Val > Digits_Value (E) then
15553 return False;
15554 end if;
15555
15556 -- Avoid types not matching pragma Float_Representation, if present
15557
15558 if (Opt.Float_Format = 'I' and then Float_Rep (E) /= IEEE_Binary)
15559 or else
15560 (Opt.Float_Format = 'V' and then Float_Rep (E) /= VAX_Native)
15561 then
15562 return False;
15563 end if;
15564
15565 -- Check for matching range, if specified
15566
15567 if Present (Spec) then
15568 if Expr_Value_R (Type_Low_Bound (E)) >
15569 Expr_Value_R (Low_Bound (Spec))
15570 then
15571 return False;
15572 end if;
15573
15574 if Expr_Value_R (Type_High_Bound (E)) <
15575 Expr_Value_R (High_Bound (Spec))
15576 then
15577 return False;
15578 end if;
15579 end if;
15580
15581 return True;
15582 end Can_Derive_From;
15583
15584 --------------------
15585 -- Find_Base_Type --
15586 --------------------
15587
15588 function Find_Base_Type return Entity_Id is
15589 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
15590
15591 begin
15592 -- Iterate over the predefined types in order, returning the first
15593 -- one that Def can derive from.
15594
15595 while Present (Choice) loop
15596 if Can_Derive_From (Node (Choice)) then
15597 return Node (Choice);
15598 end if;
15599
15600 Next_Elmt (Choice);
15601 end loop;
15602
15603 -- If we can't derive from any existing type, use Long_Long_Float
15604 -- and give appropriate message explaining the problem.
15605
15606 if Digs_Val > Max_Digs_Val then
15607 -- It might be the case that there is a type with the requested
15608 -- range, just not the combination of digits and range.
15609
15610 Error_Msg_N
15611 ("no predefined type has requested range and precision",
15612 Real_Range_Specification (Def));
15613
15614 else
15615 Error_Msg_N
15616 ("range too large for any predefined type",
15617 Real_Range_Specification (Def));
15618 end if;
15619
15620 return Standard_Long_Long_Float;
15621 end Find_Base_Type;
15622
15623 -- Start of processing for Floating_Point_Type_Declaration
15624
15625 begin
15626 Check_Restriction (No_Floating_Point, Def);
15627
15628 -- Create an implicit base type
15629
15630 Implicit_Base :=
15631 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15632
15633 -- Analyze and verify digits value
15634
15635 Analyze_And_Resolve (Digs, Any_Integer);
15636 Check_Digits_Expression (Digs);
15637 Digs_Val := Expr_Value (Digs);
15638
15639 -- Process possible range spec and find correct type to derive from
15640
15641 Process_Real_Range_Specification (Def);
15642
15643 -- Check that requested number of digits is not too high.
15644
15645 if Digs_Val > Max_Digs_Val then
15646 -- The check for Max_Base_Digits may be somewhat expensive, as it
15647 -- requires reading System, so only do it when necessary.
15648
15649 declare
15650 Max_Base_Digits : constant Uint :=
15651 Expr_Value
15652 (Expression
15653 (Parent (RTE (RE_Max_Base_Digits))));
15654
15655 begin
15656 if Digs_Val > Max_Base_Digits then
15657 Error_Msg_Uint_1 := Max_Base_Digits;
15658 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15659
15660 elsif No (Real_Range_Specification (Def)) then
15661 Error_Msg_Uint_1 := Max_Digs_Val;
15662 Error_Msg_N ("types with more than ^ digits need range spec "
15663 & "(RM 3.5.7(6))", Digs);
15664 end if;
15665 end;
15666 end if;
15667
15668 -- Find a suitable type to derive from or complain and use a substitute
15669
15670 Base_Typ := Find_Base_Type;
15671
15672 -- If there are bounds given in the declaration use them as the bounds
15673 -- of the type, otherwise use the bounds of the predefined base type
15674 -- that was chosen based on the Digits value.
15675
15676 if Present (Real_Range_Specification (Def)) then
15677 Set_Scalar_Range (T, Real_Range_Specification (Def));
15678 Set_Is_Constrained (T);
15679
15680 -- The bounds of this range must be converted to machine numbers
15681 -- in accordance with RM 4.9(38).
15682
15683 Bound := Type_Low_Bound (T);
15684
15685 if Nkind (Bound) = N_Real_Literal then
15686 Set_Realval
15687 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15688 Set_Is_Machine_Number (Bound);
15689 end if;
15690
15691 Bound := Type_High_Bound (T);
15692
15693 if Nkind (Bound) = N_Real_Literal then
15694 Set_Realval
15695 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15696 Set_Is_Machine_Number (Bound);
15697 end if;
15698
15699 else
15700 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15701 end if;
15702
15703 -- Complete definition of implicit base and declared first subtype
15704
15705 Set_Etype (Implicit_Base, Base_Typ);
15706
15707 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15708 Set_Size_Info (Implicit_Base, (Base_Typ));
15709 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15710 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15711 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15712 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15713
15714 Set_Ekind (T, E_Floating_Point_Subtype);
15715 Set_Etype (T, Implicit_Base);
15716
15717 Set_Size_Info (T, (Implicit_Base));
15718 Set_RM_Size (T, RM_Size (Implicit_Base));
15719 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15720 Set_Digits_Value (T, Digs_Val);
15721 end Floating_Point_Type_Declaration;
15722
15723 ----------------------------
15724 -- Get_Discriminant_Value --
15725 ----------------------------
15726
15727 -- This is the situation:
15728
15729 -- There is a non-derived type
15730
15731 -- type T0 (Dx, Dy, Dz...)
15732
15733 -- There are zero or more levels of derivation, with each derivation
15734 -- either purely inheriting the discriminants, or defining its own.
15735
15736 -- type Ti is new Ti-1
15737 -- or
15738 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15739 -- or
15740 -- subtype Ti is ...
15741
15742 -- The subtype issue is avoided by the use of Original_Record_Component,
15743 -- and the fact that derived subtypes also derive the constraints.
15744
15745 -- This chain leads back from
15746
15747 -- Typ_For_Constraint
15748
15749 -- Typ_For_Constraint has discriminants, and the value for each
15750 -- discriminant is given by its corresponding Elmt of Constraints.
15751
15752 -- Discriminant is some discriminant in this hierarchy
15753
15754 -- We need to return its value
15755
15756 -- We do this by recursively searching each level, and looking for
15757 -- Discriminant. Once we get to the bottom, we start backing up
15758 -- returning the value for it which may in turn be a discriminant
15759 -- further up, so on the backup we continue the substitution.
15760
15761 function Get_Discriminant_Value
15762 (Discriminant : Entity_Id;
15763 Typ_For_Constraint : Entity_Id;
15764 Constraint : Elist_Id) return Node_Id
15765 is
15766 function Root_Corresponding_Discriminant
15767 (Discr : Entity_Id) return Entity_Id;
15768 -- Given a discriminant, traverse the chain of inherited discriminants
15769 -- and return the topmost discriminant.
15770
15771 function Search_Derivation_Levels
15772 (Ti : Entity_Id;
15773 Discrim_Values : Elist_Id;
15774 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15775 -- This is the routine that performs the recursive search of levels
15776 -- as described above.
15777
15778 -------------------------------------
15779 -- Root_Corresponding_Discriminant --
15780 -------------------------------------
15781
15782 function Root_Corresponding_Discriminant
15783 (Discr : Entity_Id) return Entity_Id
15784 is
15785 D : Entity_Id;
15786
15787 begin
15788 D := Discr;
15789 while Present (Corresponding_Discriminant (D)) loop
15790 D := Corresponding_Discriminant (D);
15791 end loop;
15792
15793 return D;
15794 end Root_Corresponding_Discriminant;
15795
15796 ------------------------------
15797 -- Search_Derivation_Levels --
15798 ------------------------------
15799
15800 function Search_Derivation_Levels
15801 (Ti : Entity_Id;
15802 Discrim_Values : Elist_Id;
15803 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15804 is
15805 Assoc : Elmt_Id;
15806 Disc : Entity_Id;
15807 Result : Node_Or_Entity_Id;
15808 Result_Entity : Node_Id;
15809
15810 begin
15811 -- If inappropriate type, return Error, this happens only in
15812 -- cascaded error situations, and we want to avoid a blow up.
15813
15814 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15815 return Error;
15816 end if;
15817
15818 -- Look deeper if possible. Use Stored_Constraints only for
15819 -- untagged types. For tagged types use the given constraint.
15820 -- This asymmetry needs explanation???
15821
15822 if not Stored_Discrim_Values
15823 and then Present (Stored_Constraint (Ti))
15824 and then not Is_Tagged_Type (Ti)
15825 then
15826 Result :=
15827 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15828 else
15829 declare
15830 Td : constant Entity_Id := Etype (Ti);
15831
15832 begin
15833 if Td = Ti then
15834 Result := Discriminant;
15835
15836 else
15837 if Present (Stored_Constraint (Ti)) then
15838 Result :=
15839 Search_Derivation_Levels
15840 (Td, Stored_Constraint (Ti), True);
15841 else
15842 Result :=
15843 Search_Derivation_Levels
15844 (Td, Discrim_Values, Stored_Discrim_Values);
15845 end if;
15846 end if;
15847 end;
15848 end if;
15849
15850 -- Extra underlying places to search, if not found above. For
15851 -- concurrent types, the relevant discriminant appears in the
15852 -- corresponding record. For a type derived from a private type
15853 -- without discriminant, the full view inherits the discriminants
15854 -- of the full view of the parent.
15855
15856 if Result = Discriminant then
15857 if Is_Concurrent_Type (Ti)
15858 and then Present (Corresponding_Record_Type (Ti))
15859 then
15860 Result :=
15861 Search_Derivation_Levels (
15862 Corresponding_Record_Type (Ti),
15863 Discrim_Values,
15864 Stored_Discrim_Values);
15865
15866 elsif Is_Private_Type (Ti)
15867 and then not Has_Discriminants (Ti)
15868 and then Present (Full_View (Ti))
15869 and then Etype (Full_View (Ti)) /= Ti
15870 then
15871 Result :=
15872 Search_Derivation_Levels (
15873 Full_View (Ti),
15874 Discrim_Values,
15875 Stored_Discrim_Values);
15876 end if;
15877 end if;
15878
15879 -- If Result is not a (reference to a) discriminant, return it,
15880 -- otherwise set Result_Entity to the discriminant.
15881
15882 if Nkind (Result) = N_Defining_Identifier then
15883 pragma Assert (Result = Discriminant);
15884 Result_Entity := Result;
15885
15886 else
15887 if not Denotes_Discriminant (Result) then
15888 return Result;
15889 end if;
15890
15891 Result_Entity := Entity (Result);
15892 end if;
15893
15894 -- See if this level of derivation actually has discriminants
15895 -- because tagged derivations can add them, hence the lower
15896 -- levels need not have any.
15897
15898 if not Has_Discriminants (Ti) then
15899 return Result;
15900 end if;
15901
15902 -- Scan Ti's discriminants for Result_Entity,
15903 -- and return its corresponding value, if any.
15904
15905 Result_Entity := Original_Record_Component (Result_Entity);
15906
15907 Assoc := First_Elmt (Discrim_Values);
15908
15909 if Stored_Discrim_Values then
15910 Disc := First_Stored_Discriminant (Ti);
15911 else
15912 Disc := First_Discriminant (Ti);
15913 end if;
15914
15915 while Present (Disc) loop
15916 pragma Assert (Present (Assoc));
15917
15918 if Original_Record_Component (Disc) = Result_Entity then
15919 return Node (Assoc);
15920 end if;
15921
15922 Next_Elmt (Assoc);
15923
15924 if Stored_Discrim_Values then
15925 Next_Stored_Discriminant (Disc);
15926 else
15927 Next_Discriminant (Disc);
15928 end if;
15929 end loop;
15930
15931 -- Could not find it
15932 --
15933 return Result;
15934 end Search_Derivation_Levels;
15935
15936 -- Local Variables
15937
15938 Result : Node_Or_Entity_Id;
15939
15940 -- Start of processing for Get_Discriminant_Value
15941
15942 begin
15943 -- ??? This routine is a gigantic mess and will be deleted. For the
15944 -- time being just test for the trivial case before calling recurse.
15945
15946 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15947 declare
15948 D : Entity_Id;
15949 E : Elmt_Id;
15950
15951 begin
15952 D := First_Discriminant (Typ_For_Constraint);
15953 E := First_Elmt (Constraint);
15954 while Present (D) loop
15955 if Chars (D) = Chars (Discriminant) then
15956 return Node (E);
15957 end if;
15958
15959 Next_Discriminant (D);
15960 Next_Elmt (E);
15961 end loop;
15962 end;
15963 end if;
15964
15965 Result := Search_Derivation_Levels
15966 (Typ_For_Constraint, Constraint, False);
15967
15968 -- ??? hack to disappear when this routine is gone
15969
15970 if Nkind (Result) = N_Defining_Identifier then
15971 declare
15972 D : Entity_Id;
15973 E : Elmt_Id;
15974
15975 begin
15976 D := First_Discriminant (Typ_For_Constraint);
15977 E := First_Elmt (Constraint);
15978 while Present (D) loop
15979 if Root_Corresponding_Discriminant (D) = Discriminant then
15980 return Node (E);
15981 end if;
15982
15983 Next_Discriminant (D);
15984 Next_Elmt (E);
15985 end loop;
15986 end;
15987 end if;
15988
15989 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15990 return Result;
15991 end Get_Discriminant_Value;
15992
15993 --------------------------
15994 -- Has_Range_Constraint --
15995 --------------------------
15996
15997 function Has_Range_Constraint (N : Node_Id) return Boolean is
15998 C : constant Node_Id := Constraint (N);
15999
16000 begin
16001 if Nkind (C) = N_Range_Constraint then
16002 return True;
16003
16004 elsif Nkind (C) = N_Digits_Constraint then
16005 return
16006 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
16007 or else
16008 Present (Range_Constraint (C));
16009
16010 elsif Nkind (C) = N_Delta_Constraint then
16011 return Present (Range_Constraint (C));
16012
16013 else
16014 return False;
16015 end if;
16016 end Has_Range_Constraint;
16017
16018 ------------------------
16019 -- Inherit_Components --
16020 ------------------------
16021
16022 function Inherit_Components
16023 (N : Node_Id;
16024 Parent_Base : Entity_Id;
16025 Derived_Base : Entity_Id;
16026 Is_Tagged : Boolean;
16027 Inherit_Discr : Boolean;
16028 Discs : Elist_Id) return Elist_Id
16029 is
16030 Assoc_List : constant Elist_Id := New_Elmt_List;
16031
16032 procedure Inherit_Component
16033 (Old_C : Entity_Id;
16034 Plain_Discrim : Boolean := False;
16035 Stored_Discrim : Boolean := False);
16036 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
16037 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
16038 -- True, Old_C is a stored discriminant. If they are both false then
16039 -- Old_C is a regular component.
16040
16041 -----------------------
16042 -- Inherit_Component --
16043 -----------------------
16044
16045 procedure Inherit_Component
16046 (Old_C : Entity_Id;
16047 Plain_Discrim : Boolean := False;
16048 Stored_Discrim : Boolean := False)
16049 is
16050 procedure Set_Anonymous_Type (Id : Entity_Id);
16051 -- Id denotes the entity of an access discriminant or anonymous
16052 -- access component. Set the type of Id to either the same type of
16053 -- Old_C or create a new one depending on whether the parent and
16054 -- the child types are in the same scope.
16055
16056 ------------------------
16057 -- Set_Anonymous_Type --
16058 ------------------------
16059
16060 procedure Set_Anonymous_Type (Id : Entity_Id) is
16061 Old_Typ : constant Entity_Id := Etype (Old_C);
16062
16063 begin
16064 if Scope (Parent_Base) = Scope (Derived_Base) then
16065 Set_Etype (Id, Old_Typ);
16066
16067 -- The parent and the derived type are in two different scopes.
16068 -- Reuse the type of the original discriminant / component by
16069 -- copying it in order to preserve all attributes.
16070
16071 else
16072 declare
16073 Typ : constant Entity_Id := New_Copy (Old_Typ);
16074
16075 begin
16076 Set_Etype (Id, Typ);
16077
16078 -- Since we do not generate component declarations for
16079 -- inherited components, associate the itype with the
16080 -- derived type.
16081
16082 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
16083 Set_Scope (Typ, Derived_Base);
16084 end;
16085 end if;
16086 end Set_Anonymous_Type;
16087
16088 -- Local variables and constants
16089
16090 New_C : constant Entity_Id := New_Copy (Old_C);
16091
16092 Corr_Discrim : Entity_Id;
16093 Discrim : Entity_Id;
16094
16095 -- Start of processing for Inherit_Component
16096
16097 begin
16098 pragma Assert (not Is_Tagged or else not Stored_Discrim);
16099
16100 Set_Parent (New_C, Parent (Old_C));
16101
16102 -- Regular discriminants and components must be inserted in the scope
16103 -- of the Derived_Base. Do it here.
16104
16105 if not Stored_Discrim then
16106 Enter_Name (New_C);
16107 end if;
16108
16109 -- For tagged types the Original_Record_Component must point to
16110 -- whatever this field was pointing to in the parent type. This has
16111 -- already been achieved by the call to New_Copy above.
16112
16113 if not Is_Tagged then
16114 Set_Original_Record_Component (New_C, New_C);
16115 end if;
16116
16117 -- Set the proper type of an access discriminant
16118
16119 if Ekind (New_C) = E_Discriminant
16120 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
16121 then
16122 Set_Anonymous_Type (New_C);
16123 end if;
16124
16125 -- If we have inherited a component then see if its Etype contains
16126 -- references to Parent_Base discriminants. In this case, replace
16127 -- these references with the constraints given in Discs. We do not
16128 -- do this for the partial view of private types because this is
16129 -- not needed (only the components of the full view will be used
16130 -- for code generation) and cause problem. We also avoid this
16131 -- transformation in some error situations.
16132
16133 if Ekind (New_C) = E_Component then
16134
16135 -- Set the proper type of an anonymous access component
16136
16137 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
16138 Set_Anonymous_Type (New_C);
16139
16140 elsif (Is_Private_Type (Derived_Base)
16141 and then not Is_Generic_Type (Derived_Base))
16142 or else (Is_Empty_Elmt_List (Discs)
16143 and then not Expander_Active)
16144 then
16145 Set_Etype (New_C, Etype (Old_C));
16146
16147 else
16148 -- The current component introduces a circularity of the
16149 -- following kind:
16150
16151 -- limited with Pack_2;
16152 -- package Pack_1 is
16153 -- type T_1 is tagged record
16154 -- Comp : access Pack_2.T_2;
16155 -- ...
16156 -- end record;
16157 -- end Pack_1;
16158
16159 -- with Pack_1;
16160 -- package Pack_2 is
16161 -- type T_2 is new Pack_1.T_1 with ...;
16162 -- end Pack_2;
16163
16164 Set_Etype
16165 (New_C,
16166 Constrain_Component_Type
16167 (Old_C, Derived_Base, N, Parent_Base, Discs));
16168 end if;
16169 end if;
16170
16171 -- In derived tagged types it is illegal to reference a non
16172 -- discriminant component in the parent type. To catch this, mark
16173 -- these components with an Ekind of E_Void. This will be reset in
16174 -- Record_Type_Definition after processing the record extension of
16175 -- the derived type.
16176
16177 -- If the declaration is a private extension, there is no further
16178 -- record extension to process, and the components retain their
16179 -- current kind, because they are visible at this point.
16180
16181 if Is_Tagged and then Ekind (New_C) = E_Component
16182 and then Nkind (N) /= N_Private_Extension_Declaration
16183 then
16184 Set_Ekind (New_C, E_Void);
16185 end if;
16186
16187 if Plain_Discrim then
16188 Set_Corresponding_Discriminant (New_C, Old_C);
16189 Build_Discriminal (New_C);
16190
16191 -- If we are explicitly inheriting a stored discriminant it will be
16192 -- completely hidden.
16193
16194 elsif Stored_Discrim then
16195 Set_Corresponding_Discriminant (New_C, Empty);
16196 Set_Discriminal (New_C, Empty);
16197 Set_Is_Completely_Hidden (New_C);
16198
16199 -- Set the Original_Record_Component of each discriminant in the
16200 -- derived base to point to the corresponding stored that we just
16201 -- created.
16202
16203 Discrim := First_Discriminant (Derived_Base);
16204 while Present (Discrim) loop
16205 Corr_Discrim := Corresponding_Discriminant (Discrim);
16206
16207 -- Corr_Discrim could be missing in an error situation
16208
16209 if Present (Corr_Discrim)
16210 and then Original_Record_Component (Corr_Discrim) = Old_C
16211 then
16212 Set_Original_Record_Component (Discrim, New_C);
16213 end if;
16214
16215 Next_Discriminant (Discrim);
16216 end loop;
16217
16218 Append_Entity (New_C, Derived_Base);
16219 end if;
16220
16221 if not Is_Tagged then
16222 Append_Elmt (Old_C, Assoc_List);
16223 Append_Elmt (New_C, Assoc_List);
16224 end if;
16225 end Inherit_Component;
16226
16227 -- Variables local to Inherit_Component
16228
16229 Loc : constant Source_Ptr := Sloc (N);
16230
16231 Parent_Discrim : Entity_Id;
16232 Stored_Discrim : Entity_Id;
16233 D : Entity_Id;
16234 Component : Entity_Id;
16235
16236 -- Start of processing for Inherit_Components
16237
16238 begin
16239 if not Is_Tagged then
16240 Append_Elmt (Parent_Base, Assoc_List);
16241 Append_Elmt (Derived_Base, Assoc_List);
16242 end if;
16243
16244 -- Inherit parent discriminants if needed
16245
16246 if Inherit_Discr then
16247 Parent_Discrim := First_Discriminant (Parent_Base);
16248 while Present (Parent_Discrim) loop
16249 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
16250 Next_Discriminant (Parent_Discrim);
16251 end loop;
16252 end if;
16253
16254 -- Create explicit stored discrims for untagged types when necessary
16255
16256 if not Has_Unknown_Discriminants (Derived_Base)
16257 and then Has_Discriminants (Parent_Base)
16258 and then not Is_Tagged
16259 and then
16260 (not Inherit_Discr
16261 or else First_Discriminant (Parent_Base) /=
16262 First_Stored_Discriminant (Parent_Base))
16263 then
16264 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
16265 while Present (Stored_Discrim) loop
16266 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
16267 Next_Stored_Discriminant (Stored_Discrim);
16268 end loop;
16269 end if;
16270
16271 -- See if we can apply the second transformation for derived types, as
16272 -- explained in point 6. in the comments above Build_Derived_Record_Type
16273 -- This is achieved by appending Derived_Base discriminants into Discs,
16274 -- which has the side effect of returning a non empty Discs list to the
16275 -- caller of Inherit_Components, which is what we want. This must be
16276 -- done for private derived types if there are explicit stored
16277 -- discriminants, to ensure that we can retrieve the values of the
16278 -- constraints provided in the ancestors.
16279
16280 if Inherit_Discr
16281 and then Is_Empty_Elmt_List (Discs)
16282 and then Present (First_Discriminant (Derived_Base))
16283 and then
16284 (not Is_Private_Type (Derived_Base)
16285 or else Is_Completely_Hidden
16286 (First_Stored_Discriminant (Derived_Base))
16287 or else Is_Generic_Type (Derived_Base))
16288 then
16289 D := First_Discriminant (Derived_Base);
16290 while Present (D) loop
16291 Append_Elmt (New_Reference_To (D, Loc), Discs);
16292 Next_Discriminant (D);
16293 end loop;
16294 end if;
16295
16296 -- Finally, inherit non-discriminant components unless they are not
16297 -- visible because defined or inherited from the full view of the
16298 -- parent. Don't inherit the _parent field of the parent type.
16299
16300 Component := First_Entity (Parent_Base);
16301 while Present (Component) loop
16302
16303 -- Ada 2005 (AI-251): Do not inherit components associated with
16304 -- secondary tags of the parent.
16305
16306 if Ekind (Component) = E_Component
16307 and then Present (Related_Type (Component))
16308 then
16309 null;
16310
16311 elsif Ekind (Component) /= E_Component
16312 or else Chars (Component) = Name_uParent
16313 then
16314 null;
16315
16316 -- If the derived type is within the parent type's declarative
16317 -- region, then the components can still be inherited even though
16318 -- they aren't visible at this point. This can occur for cases
16319 -- such as within public child units where the components must
16320 -- become visible upon entering the child unit's private part.
16321
16322 elsif not Is_Visible_Component (Component)
16323 and then not In_Open_Scopes (Scope (Parent_Base))
16324 then
16325 null;
16326
16327 elsif Ekind_In (Derived_Base, E_Private_Type,
16328 E_Limited_Private_Type)
16329 then
16330 null;
16331
16332 else
16333 Inherit_Component (Component);
16334 end if;
16335
16336 Next_Entity (Component);
16337 end loop;
16338
16339 -- For tagged derived types, inherited discriminants cannot be used in
16340 -- component declarations of the record extension part. To achieve this
16341 -- we mark the inherited discriminants as not visible.
16342
16343 if Is_Tagged and then Inherit_Discr then
16344 D := First_Discriminant (Derived_Base);
16345 while Present (D) loop
16346 Set_Is_Immediately_Visible (D, False);
16347 Next_Discriminant (D);
16348 end loop;
16349 end if;
16350
16351 return Assoc_List;
16352 end Inherit_Components;
16353
16354 -----------------------
16355 -- Is_Null_Extension --
16356 -----------------------
16357
16358 function Is_Null_Extension (T : Entity_Id) return Boolean is
16359 Type_Decl : constant Node_Id := Parent (Base_Type (T));
16360 Comp_List : Node_Id;
16361 Comp : Node_Id;
16362
16363 begin
16364 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16365 or else not Is_Tagged_Type (T)
16366 or else Nkind (Type_Definition (Type_Decl)) /=
16367 N_Derived_Type_Definition
16368 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
16369 then
16370 return False;
16371 end if;
16372
16373 Comp_List :=
16374 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
16375
16376 if Present (Discriminant_Specifications (Type_Decl)) then
16377 return False;
16378
16379 elsif Present (Comp_List)
16380 and then Is_Non_Empty_List (Component_Items (Comp_List))
16381 then
16382 Comp := First (Component_Items (Comp_List));
16383
16384 -- Only user-defined components are relevant. The component list
16385 -- may also contain a parent component and internal components
16386 -- corresponding to secondary tags, but these do not determine
16387 -- whether this is a null extension.
16388
16389 while Present (Comp) loop
16390 if Comes_From_Source (Comp) then
16391 return False;
16392 end if;
16393
16394 Next (Comp);
16395 end loop;
16396
16397 return True;
16398 else
16399 return True;
16400 end if;
16401 end Is_Null_Extension;
16402
16403 ------------------------------
16404 -- Is_Valid_Constraint_Kind --
16405 ------------------------------
16406
16407 function Is_Valid_Constraint_Kind
16408 (T_Kind : Type_Kind;
16409 Constraint_Kind : Node_Kind) return Boolean
16410 is
16411 begin
16412 case T_Kind is
16413 when Enumeration_Kind |
16414 Integer_Kind =>
16415 return Constraint_Kind = N_Range_Constraint;
16416
16417 when Decimal_Fixed_Point_Kind =>
16418 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16419 N_Range_Constraint);
16420
16421 when Ordinary_Fixed_Point_Kind =>
16422 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16423 N_Range_Constraint);
16424
16425 when Float_Kind =>
16426 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16427 N_Range_Constraint);
16428
16429 when Access_Kind |
16430 Array_Kind |
16431 E_Record_Type |
16432 E_Record_Subtype |
16433 Class_Wide_Kind |
16434 E_Incomplete_Type |
16435 Private_Kind |
16436 Concurrent_Kind =>
16437 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16438
16439 when others =>
16440 return True; -- Error will be detected later
16441 end case;
16442 end Is_Valid_Constraint_Kind;
16443
16444 --------------------------
16445 -- Is_Visible_Component --
16446 --------------------------
16447
16448 function Is_Visible_Component
16449 (C : Entity_Id;
16450 N : Node_Id := Empty) return Boolean
16451 is
16452 Original_Comp : Entity_Id := Empty;
16453 Original_Scope : Entity_Id;
16454 Type_Scope : Entity_Id;
16455
16456 function Is_Local_Type (Typ : Entity_Id) return Boolean;
16457 -- Check whether parent type of inherited component is declared locally,
16458 -- possibly within a nested package or instance. The current scope is
16459 -- the derived record itself.
16460
16461 -------------------
16462 -- Is_Local_Type --
16463 -------------------
16464
16465 function Is_Local_Type (Typ : Entity_Id) return Boolean is
16466 Scop : Entity_Id;
16467
16468 begin
16469 Scop := Scope (Typ);
16470 while Present (Scop)
16471 and then Scop /= Standard_Standard
16472 loop
16473 if Scop = Scope (Current_Scope) then
16474 return True;
16475 end if;
16476
16477 Scop := Scope (Scop);
16478 end loop;
16479
16480 return False;
16481 end Is_Local_Type;
16482
16483 -- Start of processing for Is_Visible_Component
16484
16485 begin
16486 if Ekind_In (C, E_Component, E_Discriminant) then
16487 Original_Comp := Original_Record_Component (C);
16488 end if;
16489
16490 if No (Original_Comp) then
16491
16492 -- Premature usage, or previous error
16493
16494 return False;
16495
16496 else
16497 Original_Scope := Scope (Original_Comp);
16498 Type_Scope := Scope (Base_Type (Scope (C)));
16499 end if;
16500
16501 -- For an untagged type derived from a private type, the only visible
16502 -- components are new discriminants. In an instance all components are
16503 -- visible (see Analyze_Selected_Component).
16504
16505 if not Is_Tagged_Type (Original_Scope) then
16506 return not Has_Private_Ancestor (Original_Scope)
16507 or else In_Open_Scopes (Scope (Original_Scope))
16508 or else In_Instance
16509 or else (Ekind (Original_Comp) = E_Discriminant
16510 and then Original_Scope = Type_Scope);
16511
16512 -- If it is _Parent or _Tag, there is no visibility issue
16513
16514 elsif not Comes_From_Source (Original_Comp) then
16515 return True;
16516
16517 -- Discriminants are visible unless the (private) type has unknown
16518 -- discriminants. If the discriminant reference is inserted for a
16519 -- discriminant check on a full view it is also visible.
16520
16521 elsif Ekind (Original_Comp) = E_Discriminant
16522 and then
16523 (not Has_Unknown_Discriminants (Original_Scope)
16524 or else (Present (N)
16525 and then Nkind (N) = N_Selected_Component
16526 and then Nkind (Prefix (N)) = N_Type_Conversion
16527 and then not Comes_From_Source (Prefix (N))))
16528 then
16529 return True;
16530
16531 -- In the body of an instantiation, no need to check for the visibility
16532 -- of a component.
16533
16534 elsif In_Instance_Body then
16535 return True;
16536
16537 -- If the component has been declared in an ancestor which is currently
16538 -- a private type, then it is not visible. The same applies if the
16539 -- component's containing type is not in an open scope and the original
16540 -- component's enclosing type is a visible full view of a private type
16541 -- (which can occur in cases where an attempt is being made to reference
16542 -- a component in a sibling package that is inherited from a visible
16543 -- component of a type in an ancestor package; the component in the
16544 -- sibling package should not be visible even though the component it
16545 -- inherited from is visible). This does not apply however in the case
16546 -- where the scope of the type is a private child unit, or when the
16547 -- parent comes from a local package in which the ancestor is currently
16548 -- visible. The latter suppression of visibility is needed for cases
16549 -- that are tested in B730006.
16550
16551 elsif Is_Private_Type (Original_Scope)
16552 or else
16553 (not Is_Private_Descendant (Type_Scope)
16554 and then not In_Open_Scopes (Type_Scope)
16555 and then Has_Private_Declaration (Original_Scope))
16556 then
16557 -- If the type derives from an entity in a formal package, there
16558 -- are no additional visible components.
16559
16560 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
16561 N_Formal_Package_Declaration
16562 then
16563 return False;
16564
16565 -- if we are not in the private part of the current package, there
16566 -- are no additional visible components.
16567
16568 elsif Ekind (Scope (Current_Scope)) = E_Package
16569 and then not In_Private_Part (Scope (Current_Scope))
16570 then
16571 return False;
16572 else
16573 return
16574 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
16575 and then In_Open_Scopes (Scope (Original_Scope))
16576 and then Is_Local_Type (Type_Scope);
16577 end if;
16578
16579 -- There is another weird way in which a component may be invisible when
16580 -- the private and the full view are not derived from the same ancestor.
16581 -- Here is an example :
16582
16583 -- type A1 is tagged record F1 : integer; end record;
16584 -- type A2 is new A1 with record F2 : integer; end record;
16585 -- type T is new A1 with private;
16586 -- private
16587 -- type T is new A2 with null record;
16588
16589 -- In this case, the full view of T inherits F1 and F2 but the private
16590 -- view inherits only F1
16591
16592 else
16593 declare
16594 Ancestor : Entity_Id := Scope (C);
16595
16596 begin
16597 loop
16598 if Ancestor = Original_Scope then
16599 return True;
16600 elsif Ancestor = Etype (Ancestor) then
16601 return False;
16602 end if;
16603
16604 Ancestor := Etype (Ancestor);
16605 end loop;
16606 end;
16607 end if;
16608 end Is_Visible_Component;
16609
16610 --------------------------
16611 -- Make_Class_Wide_Type --
16612 --------------------------
16613
16614 procedure Make_Class_Wide_Type (T : Entity_Id) is
16615 CW_Type : Entity_Id;
16616 CW_Name : Name_Id;
16617 Next_E : Entity_Id;
16618
16619 begin
16620 if Present (Class_Wide_Type (T)) then
16621
16622 -- The class-wide type is a partially decorated entity created for a
16623 -- unanalyzed tagged type referenced through a limited with clause.
16624 -- When the tagged type is analyzed, its class-wide type needs to be
16625 -- redecorated. Note that we reuse the entity created by Decorate_
16626 -- Tagged_Type in order to preserve all links.
16627
16628 if Materialize_Entity (Class_Wide_Type (T)) then
16629 CW_Type := Class_Wide_Type (T);
16630 Set_Materialize_Entity (CW_Type, False);
16631
16632 -- The class wide type can have been defined by the partial view, in
16633 -- which case everything is already done.
16634
16635 else
16636 return;
16637 end if;
16638
16639 -- Default case, we need to create a new class-wide type
16640
16641 else
16642 CW_Type :=
16643 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16644 end if;
16645
16646 -- Inherit root type characteristics
16647
16648 CW_Name := Chars (CW_Type);
16649 Next_E := Next_Entity (CW_Type);
16650 Copy_Node (T, CW_Type);
16651 Set_Comes_From_Source (CW_Type, False);
16652 Set_Chars (CW_Type, CW_Name);
16653 Set_Parent (CW_Type, Parent (T));
16654 Set_Next_Entity (CW_Type, Next_E);
16655
16656 -- Ensure we have a new freeze node for the class-wide type. The partial
16657 -- view may have freeze action of its own, requiring a proper freeze
16658 -- node, and the same freeze node cannot be shared between the two
16659 -- types.
16660
16661 Set_Has_Delayed_Freeze (CW_Type);
16662 Set_Freeze_Node (CW_Type, Empty);
16663
16664 -- Customize the class-wide type: It has no prim. op., it cannot be
16665 -- abstract and its Etype points back to the specific root type.
16666
16667 Set_Ekind (CW_Type, E_Class_Wide_Type);
16668 Set_Is_Tagged_Type (CW_Type, True);
16669 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16670 Set_Is_Abstract_Type (CW_Type, False);
16671 Set_Is_Constrained (CW_Type, False);
16672 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16673
16674 if Ekind (T) = E_Class_Wide_Subtype then
16675 Set_Etype (CW_Type, Etype (Base_Type (T)));
16676 else
16677 Set_Etype (CW_Type, T);
16678 end if;
16679
16680 -- If this is the class_wide type of a constrained subtype, it does
16681 -- not have discriminants.
16682
16683 Set_Has_Discriminants (CW_Type,
16684 Has_Discriminants (T) and then not Is_Constrained (T));
16685
16686 Set_Has_Unknown_Discriminants (CW_Type, True);
16687 Set_Class_Wide_Type (T, CW_Type);
16688 Set_Equivalent_Type (CW_Type, Empty);
16689
16690 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16691
16692 Set_Class_Wide_Type (CW_Type, CW_Type);
16693 end Make_Class_Wide_Type;
16694
16695 ----------------
16696 -- Make_Index --
16697 ----------------
16698
16699 procedure Make_Index
16700 (I : Node_Id;
16701 Related_Nod : Node_Id;
16702 Related_Id : Entity_Id := Empty;
16703 Suffix_Index : Nat := 1;
16704 In_Iter_Schm : Boolean := False)
16705 is
16706 R : Node_Id;
16707 T : Entity_Id;
16708 Def_Id : Entity_Id := Empty;
16709 Found : Boolean := False;
16710
16711 begin
16712 -- For a discrete range used in a constrained array definition and
16713 -- defined by a range, an implicit conversion to the predefined type
16714 -- INTEGER is assumed if each bound is either a numeric literal, a named
16715 -- number, or an attribute, and the type of both bounds (prior to the
16716 -- implicit conversion) is the type universal_integer. Otherwise, both
16717 -- bounds must be of the same discrete type, other than universal
16718 -- integer; this type must be determinable independently of the
16719 -- context, but using the fact that the type must be discrete and that
16720 -- both bounds must have the same type.
16721
16722 -- Character literals also have a universal type in the absence of
16723 -- of additional context, and are resolved to Standard_Character.
16724
16725 if Nkind (I) = N_Range then
16726
16727 -- The index is given by a range constraint. The bounds are known
16728 -- to be of a consistent type.
16729
16730 if not Is_Overloaded (I) then
16731 T := Etype (I);
16732
16733 -- For universal bounds, choose the specific predefined type
16734
16735 if T = Universal_Integer then
16736 T := Standard_Integer;
16737
16738 elsif T = Any_Character then
16739 Ambiguous_Character (Low_Bound (I));
16740
16741 T := Standard_Character;
16742 end if;
16743
16744 -- The node may be overloaded because some user-defined operators
16745 -- are available, but if a universal interpretation exists it is
16746 -- also the selected one.
16747
16748 elsif Universal_Interpretation (I) = Universal_Integer then
16749 T := Standard_Integer;
16750
16751 else
16752 T := Any_Type;
16753
16754 declare
16755 Ind : Interp_Index;
16756 It : Interp;
16757
16758 begin
16759 Get_First_Interp (I, Ind, It);
16760 while Present (It.Typ) loop
16761 if Is_Discrete_Type (It.Typ) then
16762
16763 if Found
16764 and then not Covers (It.Typ, T)
16765 and then not Covers (T, It.Typ)
16766 then
16767 Error_Msg_N ("ambiguous bounds in discrete range", I);
16768 exit;
16769 else
16770 T := It.Typ;
16771 Found := True;
16772 end if;
16773 end if;
16774
16775 Get_Next_Interp (Ind, It);
16776 end loop;
16777
16778 if T = Any_Type then
16779 Error_Msg_N ("discrete type required for range", I);
16780 Set_Etype (I, Any_Type);
16781 return;
16782
16783 elsif T = Universal_Integer then
16784 T := Standard_Integer;
16785 end if;
16786 end;
16787 end if;
16788
16789 if not Is_Discrete_Type (T) then
16790 Error_Msg_N ("discrete type required for range", I);
16791 Set_Etype (I, Any_Type);
16792 return;
16793 end if;
16794
16795 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16796 and then Attribute_Name (Low_Bound (I)) = Name_First
16797 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16798 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16799 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16800 then
16801 -- The type of the index will be the type of the prefix, as long
16802 -- as the upper bound is 'Last of the same type.
16803
16804 Def_Id := Entity (Prefix (Low_Bound (I)));
16805
16806 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16807 or else Attribute_Name (High_Bound (I)) /= Name_Last
16808 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16809 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16810 then
16811 Def_Id := Empty;
16812 end if;
16813 end if;
16814
16815 R := I;
16816 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
16817
16818 elsif Nkind (I) = N_Subtype_Indication then
16819
16820 -- The index is given by a subtype with a range constraint
16821
16822 T := Base_Type (Entity (Subtype_Mark (I)));
16823
16824 if not Is_Discrete_Type (T) then
16825 Error_Msg_N ("discrete type required for range", I);
16826 Set_Etype (I, Any_Type);
16827 return;
16828 end if;
16829
16830 R := Range_Expression (Constraint (I));
16831
16832 Resolve (R, T);
16833 Process_Range_Expr_In_Decl
16834 (R, Entity (Subtype_Mark (I)), In_Iter_Schm => In_Iter_Schm);
16835
16836 elsif Nkind (I) = N_Attribute_Reference then
16837
16838 -- The parser guarantees that the attribute is a RANGE attribute
16839
16840 -- If the node denotes the range of a type mark, that is also the
16841 -- resulting type, and we do no need to create an Itype for it.
16842
16843 if Is_Entity_Name (Prefix (I))
16844 and then Comes_From_Source (I)
16845 and then Is_Type (Entity (Prefix (I)))
16846 and then Is_Discrete_Type (Entity (Prefix (I)))
16847 then
16848 Def_Id := Entity (Prefix (I));
16849 end if;
16850
16851 Analyze_And_Resolve (I);
16852 T := Etype (I);
16853 R := I;
16854
16855 -- If none of the above, must be a subtype. We convert this to a
16856 -- range attribute reference because in the case of declared first
16857 -- named subtypes, the types in the range reference can be different
16858 -- from the type of the entity. A range attribute normalizes the
16859 -- reference and obtains the correct types for the bounds.
16860
16861 -- This transformation is in the nature of an expansion, is only
16862 -- done if expansion is active. In particular, it is not done on
16863 -- formal generic types, because we need to retain the name of the
16864 -- original index for instantiation purposes.
16865
16866 else
16867 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16868 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16869 Set_Etype (I, Any_Integer);
16870 return;
16871
16872 else
16873 -- The type mark may be that of an incomplete type. It is only
16874 -- now that we can get the full view, previous analysis does
16875 -- not look specifically for a type mark.
16876
16877 Set_Entity (I, Get_Full_View (Entity (I)));
16878 Set_Etype (I, Entity (I));
16879 Def_Id := Entity (I);
16880
16881 if not Is_Discrete_Type (Def_Id) then
16882 Error_Msg_N ("discrete type required for index", I);
16883 Set_Etype (I, Any_Type);
16884 return;
16885 end if;
16886 end if;
16887
16888 if Expander_Active then
16889 Rewrite (I,
16890 Make_Attribute_Reference (Sloc (I),
16891 Attribute_Name => Name_Range,
16892 Prefix => Relocate_Node (I)));
16893
16894 -- The original was a subtype mark that does not freeze. This
16895 -- means that the rewritten version must not freeze either.
16896
16897 Set_Must_Not_Freeze (I);
16898 Set_Must_Not_Freeze (Prefix (I));
16899 Analyze_And_Resolve (I);
16900 T := Etype (I);
16901 R := I;
16902
16903 -- If expander is inactive, type is legal, nothing else to construct
16904
16905 else
16906 return;
16907 end if;
16908 end if;
16909
16910 if not Is_Discrete_Type (T) then
16911 Error_Msg_N ("discrete type required for range", I);
16912 Set_Etype (I, Any_Type);
16913 return;
16914
16915 elsif T = Any_Type then
16916 Set_Etype (I, Any_Type);
16917 return;
16918 end if;
16919
16920 -- We will now create the appropriate Itype to describe the range, but
16921 -- first a check. If we originally had a subtype, then we just label
16922 -- the range with this subtype. Not only is there no need to construct
16923 -- a new subtype, but it is wrong to do so for two reasons:
16924
16925 -- 1. A legality concern, if we have a subtype, it must not freeze,
16926 -- and the Itype would cause freezing incorrectly
16927
16928 -- 2. An efficiency concern, if we created an Itype, it would not be
16929 -- recognized as the same type for the purposes of eliminating
16930 -- checks in some circumstances.
16931
16932 -- We signal this case by setting the subtype entity in Def_Id
16933
16934 if No (Def_Id) then
16935 Def_Id :=
16936 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16937 Set_Etype (Def_Id, Base_Type (T));
16938
16939 if Is_Signed_Integer_Type (T) then
16940 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16941
16942 elsif Is_Modular_Integer_Type (T) then
16943 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16944
16945 else
16946 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16947 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16948 Set_First_Literal (Def_Id, First_Literal (T));
16949 end if;
16950
16951 Set_Size_Info (Def_Id, (T));
16952 Set_RM_Size (Def_Id, RM_Size (T));
16953 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16954
16955 Set_Scalar_Range (Def_Id, R);
16956 Conditional_Delay (Def_Id, T);
16957
16958 -- In the subtype indication case, if the immediate parent of the
16959 -- new subtype is non-static, then the subtype we create is non-
16960 -- static, even if its bounds are static.
16961
16962 if Nkind (I) = N_Subtype_Indication
16963 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16964 then
16965 Set_Is_Non_Static_Subtype (Def_Id);
16966 end if;
16967 end if;
16968
16969 -- Final step is to label the index with this constructed type
16970
16971 Set_Etype (I, Def_Id);
16972 end Make_Index;
16973
16974 ------------------------------
16975 -- Modular_Type_Declaration --
16976 ------------------------------
16977
16978 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16979 Mod_Expr : constant Node_Id := Expression (Def);
16980 M_Val : Uint;
16981
16982 procedure Set_Modular_Size (Bits : Int);
16983 -- Sets RM_Size to Bits, and Esize to normal word size above this
16984
16985 ----------------------
16986 -- Set_Modular_Size --
16987 ----------------------
16988
16989 procedure Set_Modular_Size (Bits : Int) is
16990 begin
16991 Set_RM_Size (T, UI_From_Int (Bits));
16992
16993 if Bits <= 8 then
16994 Init_Esize (T, 8);
16995
16996 elsif Bits <= 16 then
16997 Init_Esize (T, 16);
16998
16999 elsif Bits <= 32 then
17000 Init_Esize (T, 32);
17001
17002 else
17003 Init_Esize (T, System_Max_Binary_Modulus_Power);
17004 end if;
17005
17006 if not Non_Binary_Modulus (T)
17007 and then Esize (T) = RM_Size (T)
17008 then
17009 Set_Is_Known_Valid (T);
17010 end if;
17011 end Set_Modular_Size;
17012
17013 -- Start of processing for Modular_Type_Declaration
17014
17015 begin
17016 -- If the mod expression is (exactly) 2 * literal, where literal is
17017 -- 64 or less,then almost certainly the * was meant to be **. Warn!
17018
17019 if Warn_On_Suspicious_Modulus_Value
17020 and then Nkind (Mod_Expr) = N_Op_Multiply
17021 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
17022 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
17023 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
17024 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
17025 then
17026 Error_Msg_N
17027 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
17028 end if;
17029
17030 -- Proceed with analysis of mod expression
17031
17032 Analyze_And_Resolve (Mod_Expr, Any_Integer);
17033 Set_Etype (T, T);
17034 Set_Ekind (T, E_Modular_Integer_Type);
17035 Init_Alignment (T);
17036 Set_Is_Constrained (T);
17037
17038 if not Is_OK_Static_Expression (Mod_Expr) then
17039 Flag_Non_Static_Expr
17040 ("non-static expression used for modular type bound!", Mod_Expr);
17041 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17042 else
17043 M_Val := Expr_Value (Mod_Expr);
17044 end if;
17045
17046 if M_Val < 1 then
17047 Error_Msg_N ("modulus value must be positive", Mod_Expr);
17048 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17049 end if;
17050
17051 Set_Modulus (T, M_Val);
17052
17053 -- Create bounds for the modular type based on the modulus given in
17054 -- the type declaration and then analyze and resolve those bounds.
17055
17056 Set_Scalar_Range (T,
17057 Make_Range (Sloc (Mod_Expr),
17058 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
17059 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
17060
17061 -- Properly analyze the literals for the range. We do this manually
17062 -- because we can't go calling Resolve, since we are resolving these
17063 -- bounds with the type, and this type is certainly not complete yet!
17064
17065 Set_Etype (Low_Bound (Scalar_Range (T)), T);
17066 Set_Etype (High_Bound (Scalar_Range (T)), T);
17067 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
17068 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
17069
17070 -- Loop through powers of two to find number of bits required
17071
17072 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
17073
17074 -- Binary case
17075
17076 if M_Val = 2 ** Bits then
17077 Set_Modular_Size (Bits);
17078 return;
17079
17080 -- Non-binary case
17081
17082 elsif M_Val < 2 ** Bits then
17083 Check_SPARK_Restriction ("modulus should be a power of 2", T);
17084 Set_Non_Binary_Modulus (T);
17085
17086 if Bits > System_Max_Nonbinary_Modulus_Power then
17087 Error_Msg_Uint_1 :=
17088 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
17089 Error_Msg_F
17090 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
17091 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17092 return;
17093
17094 else
17095 -- In the non-binary case, set size as per RM 13.3(55)
17096
17097 Set_Modular_Size (Bits);
17098 return;
17099 end if;
17100 end if;
17101
17102 end loop;
17103
17104 -- If we fall through, then the size exceed System.Max_Binary_Modulus
17105 -- so we just signal an error and set the maximum size.
17106
17107 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
17108 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
17109
17110 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17111 Init_Alignment (T);
17112
17113 end Modular_Type_Declaration;
17114
17115 --------------------------
17116 -- New_Concatenation_Op --
17117 --------------------------
17118
17119 procedure New_Concatenation_Op (Typ : Entity_Id) is
17120 Loc : constant Source_Ptr := Sloc (Typ);
17121 Op : Entity_Id;
17122
17123 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
17124 -- Create abbreviated declaration for the formal of a predefined
17125 -- Operator 'Op' of type 'Typ'
17126
17127 --------------------
17128 -- Make_Op_Formal --
17129 --------------------
17130
17131 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
17132 Formal : Entity_Id;
17133 begin
17134 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
17135 Set_Etype (Formal, Typ);
17136 Set_Mechanism (Formal, Default_Mechanism);
17137 return Formal;
17138 end Make_Op_Formal;
17139
17140 -- Start of processing for New_Concatenation_Op
17141
17142 begin
17143 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
17144
17145 Set_Ekind (Op, E_Operator);
17146 Set_Scope (Op, Current_Scope);
17147 Set_Etype (Op, Typ);
17148 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
17149 Set_Is_Immediately_Visible (Op);
17150 Set_Is_Intrinsic_Subprogram (Op);
17151 Set_Has_Completion (Op);
17152 Append_Entity (Op, Current_Scope);
17153
17154 Set_Name_Entity_Id (Name_Op_Concat, Op);
17155
17156 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17157 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17158 end New_Concatenation_Op;
17159
17160 -------------------------
17161 -- OK_For_Limited_Init --
17162 -------------------------
17163
17164 -- ???Check all calls of this, and compare the conditions under which it's
17165 -- called.
17166
17167 function OK_For_Limited_Init
17168 (Typ : Entity_Id;
17169 Exp : Node_Id) return Boolean
17170 is
17171 begin
17172 return Is_CPP_Constructor_Call (Exp)
17173 or else (Ada_Version >= Ada_2005
17174 and then not Debug_Flag_Dot_L
17175 and then OK_For_Limited_Init_In_05 (Typ, Exp));
17176 end OK_For_Limited_Init;
17177
17178 -------------------------------
17179 -- OK_For_Limited_Init_In_05 --
17180 -------------------------------
17181
17182 function OK_For_Limited_Init_In_05
17183 (Typ : Entity_Id;
17184 Exp : Node_Id) return Boolean
17185 is
17186 begin
17187 -- An object of a limited interface type can be initialized with any
17188 -- expression of a nonlimited descendant type.
17189
17190 if Is_Class_Wide_Type (Typ)
17191 and then Is_Limited_Interface (Typ)
17192 and then not Is_Limited_Type (Etype (Exp))
17193 then
17194 return True;
17195 end if;
17196
17197 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
17198 -- case of limited aggregates (including extension aggregates), and
17199 -- function calls. The function call may have been given in prefixed
17200 -- notation, in which case the original node is an indexed component.
17201 -- If the function is parameterless, the original node was an explicit
17202 -- dereference. The function may also be parameterless, in which case
17203 -- the source node is just an identifier.
17204
17205 case Nkind (Original_Node (Exp)) is
17206 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
17207 return True;
17208
17209 when N_Identifier =>
17210 return Present (Entity (Original_Node (Exp)))
17211 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
17212
17213 when N_Qualified_Expression =>
17214 return
17215 OK_For_Limited_Init_In_05
17216 (Typ, Expression (Original_Node (Exp)));
17217
17218 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
17219 -- with a function call, the expander has rewritten the call into an
17220 -- N_Type_Conversion node to force displacement of the pointer to
17221 -- reference the component containing the secondary dispatch table.
17222 -- Otherwise a type conversion is not a legal context.
17223 -- A return statement for a build-in-place function returning a
17224 -- synchronized type also introduces an unchecked conversion.
17225
17226 when N_Type_Conversion |
17227 N_Unchecked_Type_Conversion =>
17228 return not Comes_From_Source (Exp)
17229 and then
17230 OK_For_Limited_Init_In_05
17231 (Typ, Expression (Original_Node (Exp)));
17232
17233 when N_Indexed_Component |
17234 N_Selected_Component |
17235 N_Explicit_Dereference =>
17236 return Nkind (Exp) = N_Function_Call;
17237
17238 -- A use of 'Input is a function call, hence allowed. Normally the
17239 -- attribute will be changed to a call, but the attribute by itself
17240 -- can occur with -gnatc.
17241
17242 when N_Attribute_Reference =>
17243 return Attribute_Name (Original_Node (Exp)) = Name_Input;
17244
17245 -- For a case expression, all dependent expressions must be legal
17246
17247 when N_Case_Expression =>
17248 declare
17249 Alt : Node_Id;
17250
17251 begin
17252 Alt := First (Alternatives (Original_Node (Exp)));
17253 while Present (Alt) loop
17254 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
17255 return False;
17256 end if;
17257
17258 Next (Alt);
17259 end loop;
17260
17261 return True;
17262 end;
17263
17264 -- For an if expression, all dependent expressions must be legal
17265
17266 when N_If_Expression =>
17267 declare
17268 Then_Expr : constant Node_Id :=
17269 Next (First (Expressions (Original_Node (Exp))));
17270 Else_Expr : constant Node_Id := Next (Then_Expr);
17271 begin
17272 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
17273 and then
17274 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
17275 end;
17276
17277 when others =>
17278 return False;
17279 end case;
17280 end OK_For_Limited_Init_In_05;
17281
17282 -------------------------------------------
17283 -- Ordinary_Fixed_Point_Type_Declaration --
17284 -------------------------------------------
17285
17286 procedure Ordinary_Fixed_Point_Type_Declaration
17287 (T : Entity_Id;
17288 Def : Node_Id)
17289 is
17290 Loc : constant Source_Ptr := Sloc (Def);
17291 Delta_Expr : constant Node_Id := Delta_Expression (Def);
17292 RRS : constant Node_Id := Real_Range_Specification (Def);
17293 Implicit_Base : Entity_Id;
17294 Delta_Val : Ureal;
17295 Small_Val : Ureal;
17296 Low_Val : Ureal;
17297 High_Val : Ureal;
17298
17299 begin
17300 Check_Restriction (No_Fixed_Point, Def);
17301
17302 -- Create implicit base type
17303
17304 Implicit_Base :=
17305 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
17306 Set_Etype (Implicit_Base, Implicit_Base);
17307
17308 -- Analyze and process delta expression
17309
17310 Analyze_And_Resolve (Delta_Expr, Any_Real);
17311
17312 Check_Delta_Expression (Delta_Expr);
17313 Delta_Val := Expr_Value_R (Delta_Expr);
17314
17315 Set_Delta_Value (Implicit_Base, Delta_Val);
17316
17317 -- Compute default small from given delta, which is the largest power
17318 -- of two that does not exceed the given delta value.
17319
17320 declare
17321 Tmp : Ureal;
17322 Scale : Int;
17323
17324 begin
17325 Tmp := Ureal_1;
17326 Scale := 0;
17327
17328 if Delta_Val < Ureal_1 then
17329 while Delta_Val < Tmp loop
17330 Tmp := Tmp / Ureal_2;
17331 Scale := Scale + 1;
17332 end loop;
17333
17334 else
17335 loop
17336 Tmp := Tmp * Ureal_2;
17337 exit when Tmp > Delta_Val;
17338 Scale := Scale - 1;
17339 end loop;
17340 end if;
17341
17342 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
17343 end;
17344
17345 Set_Small_Value (Implicit_Base, Small_Val);
17346
17347 -- If no range was given, set a dummy range
17348
17349 if RRS <= Empty_Or_Error then
17350 Low_Val := -Small_Val;
17351 High_Val := Small_Val;
17352
17353 -- Otherwise analyze and process given range
17354
17355 else
17356 declare
17357 Low : constant Node_Id := Low_Bound (RRS);
17358 High : constant Node_Id := High_Bound (RRS);
17359
17360 begin
17361 Analyze_And_Resolve (Low, Any_Real);
17362 Analyze_And_Resolve (High, Any_Real);
17363 Check_Real_Bound (Low);
17364 Check_Real_Bound (High);
17365
17366 -- Obtain and set the range
17367
17368 Low_Val := Expr_Value_R (Low);
17369 High_Val := Expr_Value_R (High);
17370
17371 if Low_Val > High_Val then
17372 Error_Msg_NE ("??fixed point type& has null range", Def, T);
17373 end if;
17374 end;
17375 end if;
17376
17377 -- The range for both the implicit base and the declared first subtype
17378 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17379 -- set a temporary range in place. Note that the bounds of the base
17380 -- type will be widened to be symmetrical and to fill the available
17381 -- bits when the type is frozen.
17382
17383 -- We could do this with all discrete types, and probably should, but
17384 -- we absolutely have to do it for fixed-point, since the end-points
17385 -- of the range and the size are determined by the small value, which
17386 -- could be reset before the freeze point.
17387
17388 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17389 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17390
17391 -- Complete definition of first subtype
17392
17393 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17394 Set_Etype (T, Implicit_Base);
17395 Init_Size_Align (T);
17396 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17397 Set_Small_Value (T, Small_Val);
17398 Set_Delta_Value (T, Delta_Val);
17399 Set_Is_Constrained (T);
17400
17401 end Ordinary_Fixed_Point_Type_Declaration;
17402
17403 ----------------------------------------
17404 -- Prepare_Private_Subtype_Completion --
17405 ----------------------------------------
17406
17407 procedure Prepare_Private_Subtype_Completion
17408 (Id : Entity_Id;
17409 Related_Nod : Node_Id)
17410 is
17411 Id_B : constant Entity_Id := Base_Type (Id);
17412 Full_B : constant Entity_Id := Full_View (Id_B);
17413 Full : Entity_Id;
17414
17415 begin
17416 if Present (Full_B) then
17417
17418 -- The Base_Type is already completed, we can complete the subtype
17419 -- now. We have to create a new entity with the same name, Thus we
17420 -- can't use Create_Itype.
17421
17422 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17423 Set_Is_Itype (Full);
17424 Set_Associated_Node_For_Itype (Full, Related_Nod);
17425 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17426 end if;
17427
17428 -- The parent subtype may be private, but the base might not, in some
17429 -- nested instances. In that case, the subtype does not need to be
17430 -- exchanged. It would still be nice to make private subtypes and their
17431 -- bases consistent at all times ???
17432
17433 if Is_Private_Type (Id_B) then
17434 Append_Elmt (Id, Private_Dependents (Id_B));
17435 end if;
17436 end Prepare_Private_Subtype_Completion;
17437
17438 ---------------------------
17439 -- Process_Discriminants --
17440 ---------------------------
17441
17442 procedure Process_Discriminants
17443 (N : Node_Id;
17444 Prev : Entity_Id := Empty)
17445 is
17446 Elist : constant Elist_Id := New_Elmt_List;
17447 Id : Node_Id;
17448 Discr : Node_Id;
17449 Discr_Number : Uint;
17450 Discr_Type : Entity_Id;
17451 Default_Present : Boolean := False;
17452 Default_Not_Present : Boolean := False;
17453
17454 begin
17455 -- A composite type other than an array type can have discriminants.
17456 -- On entry, the current scope is the composite type.
17457
17458 -- The discriminants are initially entered into the scope of the type
17459 -- via Enter_Name with the default Ekind of E_Void to prevent premature
17460 -- use, as explained at the end of this procedure.
17461
17462 Discr := First (Discriminant_Specifications (N));
17463 while Present (Discr) loop
17464 Enter_Name (Defining_Identifier (Discr));
17465
17466 -- For navigation purposes we add a reference to the discriminant
17467 -- in the entity for the type. If the current declaration is a
17468 -- completion, place references on the partial view. Otherwise the
17469 -- type is the current scope.
17470
17471 if Present (Prev) then
17472
17473 -- The references go on the partial view, if present. If the
17474 -- partial view has discriminants, the references have been
17475 -- generated already.
17476
17477 if not Has_Discriminants (Prev) then
17478 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
17479 end if;
17480 else
17481 Generate_Reference
17482 (Current_Scope, Defining_Identifier (Discr), 'd');
17483 end if;
17484
17485 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
17486 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
17487
17488 -- Ada 2005 (AI-254)
17489
17490 if Present (Access_To_Subprogram_Definition
17491 (Discriminant_Type (Discr)))
17492 and then Protected_Present (Access_To_Subprogram_Definition
17493 (Discriminant_Type (Discr)))
17494 then
17495 Discr_Type :=
17496 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
17497 end if;
17498
17499 else
17500 Find_Type (Discriminant_Type (Discr));
17501 Discr_Type := Etype (Discriminant_Type (Discr));
17502
17503 if Error_Posted (Discriminant_Type (Discr)) then
17504 Discr_Type := Any_Type;
17505 end if;
17506 end if;
17507
17508 if Is_Access_Type (Discr_Type) then
17509
17510 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
17511 -- record types
17512
17513 if Ada_Version < Ada_2005 then
17514 Check_Access_Discriminant_Requires_Limited
17515 (Discr, Discriminant_Type (Discr));
17516 end if;
17517
17518 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
17519 Error_Msg_N
17520 ("(Ada 83) access discriminant not allowed", Discr);
17521 end if;
17522
17523 elsif not Is_Discrete_Type (Discr_Type) then
17524 Error_Msg_N ("discriminants must have a discrete or access type",
17525 Discriminant_Type (Discr));
17526 end if;
17527
17528 Set_Etype (Defining_Identifier (Discr), Discr_Type);
17529
17530 -- If a discriminant specification includes the assignment compound
17531 -- delimiter followed by an expression, the expression is the default
17532 -- expression of the discriminant; the default expression must be of
17533 -- the type of the discriminant. (RM 3.7.1) Since this expression is
17534 -- a default expression, we do the special preanalysis, since this
17535 -- expression does not freeze (see "Handling of Default and Per-
17536 -- Object Expressions" in spec of package Sem).
17537
17538 if Present (Expression (Discr)) then
17539 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
17540
17541 if Nkind (N) = N_Formal_Type_Declaration then
17542 Error_Msg_N
17543 ("discriminant defaults not allowed for formal type",
17544 Expression (Discr));
17545
17546 -- Flag an error for a tagged type with defaulted discriminants,
17547 -- excluding limited tagged types when compiling for Ada 2012
17548 -- (see AI05-0214).
17549
17550 elsif Is_Tagged_Type (Current_Scope)
17551 and then (not Is_Limited_Type (Current_Scope)
17552 or else Ada_Version < Ada_2012)
17553 and then Comes_From_Source (N)
17554 then
17555 -- Note: see similar test in Check_Or_Process_Discriminants, to
17556 -- handle the (illegal) case of the completion of an untagged
17557 -- view with discriminants with defaults by a tagged full view.
17558 -- We skip the check if Discr does not come from source, to
17559 -- account for the case of an untagged derived type providing
17560 -- defaults for a renamed discriminant from a private untagged
17561 -- ancestor with a tagged full view (ACATS B460006).
17562
17563 if Ada_Version >= Ada_2012 then
17564 Error_Msg_N
17565 ("discriminants of nonlimited tagged type cannot have"
17566 & " defaults",
17567 Expression (Discr));
17568 else
17569 Error_Msg_N
17570 ("discriminants of tagged type cannot have defaults",
17571 Expression (Discr));
17572 end if;
17573
17574 else
17575 Default_Present := True;
17576 Append_Elmt (Expression (Discr), Elist);
17577
17578 -- Tag the defining identifiers for the discriminants with
17579 -- their corresponding default expressions from the tree.
17580
17581 Set_Discriminant_Default_Value
17582 (Defining_Identifier (Discr), Expression (Discr));
17583 end if;
17584
17585 else
17586 Default_Not_Present := True;
17587 end if;
17588
17589 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
17590 -- Discr_Type but with the null-exclusion attribute
17591
17592 if Ada_Version >= Ada_2005 then
17593
17594 -- Ada 2005 (AI-231): Static checks
17595
17596 if Can_Never_Be_Null (Discr_Type) then
17597 Null_Exclusion_Static_Checks (Discr);
17598
17599 elsif Is_Access_Type (Discr_Type)
17600 and then Null_Exclusion_Present (Discr)
17601
17602 -- No need to check itypes because in their case this check
17603 -- was done at their point of creation
17604
17605 and then not Is_Itype (Discr_Type)
17606 then
17607 if Can_Never_Be_Null (Discr_Type) then
17608 Error_Msg_NE
17609 ("`NOT NULL` not allowed (& already excludes null)",
17610 Discr,
17611 Discr_Type);
17612 end if;
17613
17614 Set_Etype (Defining_Identifier (Discr),
17615 Create_Null_Excluding_Itype
17616 (T => Discr_Type,
17617 Related_Nod => Discr));
17618
17619 -- Check for improper null exclusion if the type is otherwise
17620 -- legal for a discriminant.
17621
17622 elsif Null_Exclusion_Present (Discr)
17623 and then Is_Discrete_Type (Discr_Type)
17624 then
17625 Error_Msg_N
17626 ("null exclusion can only apply to an access type", Discr);
17627 end if;
17628
17629 -- Ada 2005 (AI-402): access discriminants of nonlimited types
17630 -- can't have defaults. Synchronized types, or types that are
17631 -- explicitly limited are fine, but special tests apply to derived
17632 -- types in generics: in a generic body we have to assume the
17633 -- worst, and therefore defaults are not allowed if the parent is
17634 -- a generic formal private type (see ACATS B370001).
17635
17636 if Is_Access_Type (Discr_Type) and then Default_Present then
17637 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
17638 or else Is_Limited_Record (Current_Scope)
17639 or else Is_Concurrent_Type (Current_Scope)
17640 or else Is_Concurrent_Record_Type (Current_Scope)
17641 or else Ekind (Current_Scope) = E_Limited_Private_Type
17642 then
17643 if not Is_Derived_Type (Current_Scope)
17644 or else not Is_Generic_Type (Etype (Current_Scope))
17645 or else not In_Package_Body (Scope (Etype (Current_Scope)))
17646 or else Limited_Present
17647 (Type_Definition (Parent (Current_Scope)))
17648 then
17649 null;
17650
17651 else
17652 Error_Msg_N ("access discriminants of nonlimited types",
17653 Expression (Discr));
17654 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17655 end if;
17656
17657 elsif Present (Expression (Discr)) then
17658 Error_Msg_N
17659 ("(Ada 2005) access discriminants of nonlimited types",
17660 Expression (Discr));
17661 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17662 end if;
17663 end if;
17664 end if;
17665
17666 Next (Discr);
17667 end loop;
17668
17669 -- An element list consisting of the default expressions of the
17670 -- discriminants is constructed in the above loop and used to set
17671 -- the Discriminant_Constraint attribute for the type. If an object
17672 -- is declared of this (record or task) type without any explicit
17673 -- discriminant constraint given, this element list will form the
17674 -- actual parameters for the corresponding initialization procedure
17675 -- for the type.
17676
17677 Set_Discriminant_Constraint (Current_Scope, Elist);
17678 Set_Stored_Constraint (Current_Scope, No_Elist);
17679
17680 -- Default expressions must be provided either for all or for none
17681 -- of the discriminants of a discriminant part. (RM 3.7.1)
17682
17683 if Default_Present and then Default_Not_Present then
17684 Error_Msg_N
17685 ("incomplete specification of defaults for discriminants", N);
17686 end if;
17687
17688 -- The use of the name of a discriminant is not allowed in default
17689 -- expressions of a discriminant part if the specification of the
17690 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17691
17692 -- To detect this, the discriminant names are entered initially with an
17693 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17694 -- attempt to use a void entity (for example in an expression that is
17695 -- type-checked) produces the error message: premature usage. Now after
17696 -- completing the semantic analysis of the discriminant part, we can set
17697 -- the Ekind of all the discriminants appropriately.
17698
17699 Discr := First (Discriminant_Specifications (N));
17700 Discr_Number := Uint_1;
17701 while Present (Discr) loop
17702 Id := Defining_Identifier (Discr);
17703 Set_Ekind (Id, E_Discriminant);
17704 Init_Component_Location (Id);
17705 Init_Esize (Id);
17706 Set_Discriminant_Number (Id, Discr_Number);
17707
17708 -- Make sure this is always set, even in illegal programs
17709
17710 Set_Corresponding_Discriminant (Id, Empty);
17711
17712 -- Initialize the Original_Record_Component to the entity itself.
17713 -- Inherit_Components will propagate the right value to
17714 -- discriminants in derived record types.
17715
17716 Set_Original_Record_Component (Id, Id);
17717
17718 -- Create the discriminal for the discriminant
17719
17720 Build_Discriminal (Id);
17721
17722 Next (Discr);
17723 Discr_Number := Discr_Number + 1;
17724 end loop;
17725
17726 Set_Has_Discriminants (Current_Scope);
17727 end Process_Discriminants;
17728
17729 -----------------------
17730 -- Process_Full_View --
17731 -----------------------
17732
17733 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17734 Priv_Parent : Entity_Id;
17735 Full_Parent : Entity_Id;
17736 Full_Indic : Node_Id;
17737
17738 procedure Collect_Implemented_Interfaces
17739 (Typ : Entity_Id;
17740 Ifaces : Elist_Id);
17741 -- Ada 2005: Gather all the interfaces that Typ directly or
17742 -- inherently implements. Duplicate entries are not added to
17743 -- the list Ifaces.
17744
17745 ------------------------------------
17746 -- Collect_Implemented_Interfaces --
17747 ------------------------------------
17748
17749 procedure Collect_Implemented_Interfaces
17750 (Typ : Entity_Id;
17751 Ifaces : Elist_Id)
17752 is
17753 Iface : Entity_Id;
17754 Iface_Elmt : Elmt_Id;
17755
17756 begin
17757 -- Abstract interfaces are only associated with tagged record types
17758
17759 if not Is_Tagged_Type (Typ)
17760 or else not Is_Record_Type (Typ)
17761 then
17762 return;
17763 end if;
17764
17765 -- Recursively climb to the ancestors
17766
17767 if Etype (Typ) /= Typ
17768
17769 -- Protect the frontend against wrong cyclic declarations like:
17770
17771 -- type B is new A with private;
17772 -- type C is new A with private;
17773 -- private
17774 -- type B is new C with null record;
17775 -- type C is new B with null record;
17776
17777 and then Etype (Typ) /= Priv_T
17778 and then Etype (Typ) /= Full_T
17779 then
17780 -- Keep separate the management of private type declarations
17781
17782 if Ekind (Typ) = E_Record_Type_With_Private then
17783
17784 -- Handle the following erroneous case:
17785 -- type Private_Type is tagged private;
17786 -- private
17787 -- type Private_Type is new Type_Implementing_Iface;
17788
17789 if Present (Full_View (Typ))
17790 and then Etype (Typ) /= Full_View (Typ)
17791 then
17792 if Is_Interface (Etype (Typ)) then
17793 Append_Unique_Elmt (Etype (Typ), Ifaces);
17794 end if;
17795
17796 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17797 end if;
17798
17799 -- Non-private types
17800
17801 else
17802 if Is_Interface (Etype (Typ)) then
17803 Append_Unique_Elmt (Etype (Typ), Ifaces);
17804 end if;
17805
17806 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17807 end if;
17808 end if;
17809
17810 -- Handle entities in the list of abstract interfaces
17811
17812 if Present (Interfaces (Typ)) then
17813 Iface_Elmt := First_Elmt (Interfaces (Typ));
17814 while Present (Iface_Elmt) loop
17815 Iface := Node (Iface_Elmt);
17816
17817 pragma Assert (Is_Interface (Iface));
17818
17819 if not Contain_Interface (Iface, Ifaces) then
17820 Append_Elmt (Iface, Ifaces);
17821 Collect_Implemented_Interfaces (Iface, Ifaces);
17822 end if;
17823
17824 Next_Elmt (Iface_Elmt);
17825 end loop;
17826 end if;
17827 end Collect_Implemented_Interfaces;
17828
17829 -- Start of processing for Process_Full_View
17830
17831 begin
17832 -- First some sanity checks that must be done after semantic
17833 -- decoration of the full view and thus cannot be placed with other
17834 -- similar checks in Find_Type_Name
17835
17836 if not Is_Limited_Type (Priv_T)
17837 and then (Is_Limited_Type (Full_T)
17838 or else Is_Limited_Composite (Full_T))
17839 then
17840 if In_Instance then
17841 null;
17842 else
17843 Error_Msg_N
17844 ("completion of nonlimited type cannot be limited", Full_T);
17845 Explain_Limited_Type (Full_T, Full_T);
17846 end if;
17847
17848 elsif Is_Abstract_Type (Full_T)
17849 and then not Is_Abstract_Type (Priv_T)
17850 then
17851 Error_Msg_N
17852 ("completion of nonabstract type cannot be abstract", Full_T);
17853
17854 elsif Is_Tagged_Type (Priv_T)
17855 and then Is_Limited_Type (Priv_T)
17856 and then not Is_Limited_Type (Full_T)
17857 then
17858 -- If pragma CPP_Class was applied to the private declaration
17859 -- propagate the limitedness to the full-view
17860
17861 if Is_CPP_Class (Priv_T) then
17862 Set_Is_Limited_Record (Full_T);
17863
17864 -- GNAT allow its own definition of Limited_Controlled to disobey
17865 -- this rule in order in ease the implementation. This test is safe
17866 -- because Root_Controlled is defined in a child of System that
17867 -- normal programs are not supposed to use.
17868
17869 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
17870 Set_Is_Limited_Composite (Full_T);
17871 else
17872 Error_Msg_N
17873 ("completion of limited tagged type must be limited", Full_T);
17874 end if;
17875
17876 elsif Is_Generic_Type (Priv_T) then
17877 Error_Msg_N ("generic type cannot have a completion", Full_T);
17878 end if;
17879
17880 -- Check that ancestor interfaces of private and full views are
17881 -- consistent. We omit this check for synchronized types because
17882 -- they are performed on the corresponding record type when frozen.
17883
17884 if Ada_Version >= Ada_2005
17885 and then Is_Tagged_Type (Priv_T)
17886 and then Is_Tagged_Type (Full_T)
17887 and then not Is_Concurrent_Type (Full_T)
17888 then
17889 declare
17890 Iface : Entity_Id;
17891 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17892 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17893
17894 begin
17895 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17896 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17897
17898 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17899 -- an interface type if and only if the full type is descendant
17900 -- of the interface type (AARM 7.3 (7.3/2)).
17901
17902 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17903
17904 if Present (Iface) then
17905 Error_Msg_NE
17906 ("interface & not implemented by full type " &
17907 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17908 end if;
17909
17910 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17911
17912 if Present (Iface) then
17913 Error_Msg_NE
17914 ("interface & not implemented by partial view " &
17915 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17916 end if;
17917 end;
17918 end if;
17919
17920 if Is_Tagged_Type (Priv_T)
17921 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17922 and then Is_Derived_Type (Full_T)
17923 then
17924 Priv_Parent := Etype (Priv_T);
17925
17926 -- The full view of a private extension may have been transformed
17927 -- into an unconstrained derived type declaration and a subtype
17928 -- declaration (see build_derived_record_type for details).
17929
17930 if Nkind (N) = N_Subtype_Declaration then
17931 Full_Indic := Subtype_Indication (N);
17932 Full_Parent := Etype (Base_Type (Full_T));
17933 else
17934 Full_Indic := Subtype_Indication (Type_Definition (N));
17935 Full_Parent := Etype (Full_T);
17936 end if;
17937
17938 -- Check that the parent type of the full type is a descendant of
17939 -- the ancestor subtype given in the private extension. If either
17940 -- entity has an Etype equal to Any_Type then we had some previous
17941 -- error situation [7.3(8)].
17942
17943 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17944 return;
17945
17946 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17947 -- any order. Therefore we don't have to check that its parent must
17948 -- be a descendant of the parent of the private type declaration.
17949
17950 elsif Is_Interface (Priv_Parent)
17951 and then Is_Interface (Full_Parent)
17952 then
17953 null;
17954
17955 -- Ada 2005 (AI-251): If the parent of the private type declaration
17956 -- is an interface there is no need to check that it is an ancestor
17957 -- of the associated full type declaration. The required tests for
17958 -- this case are performed by Build_Derived_Record_Type.
17959
17960 elsif not Is_Interface (Base_Type (Priv_Parent))
17961 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17962 then
17963 Error_Msg_N
17964 ("parent of full type must descend from parent"
17965 & " of private extension", Full_Indic);
17966
17967 -- First check a formal restriction, and then proceed with checking
17968 -- Ada rules. Since the formal restriction is not a serious error, we
17969 -- don't prevent further error detection for this check, hence the
17970 -- ELSE.
17971
17972 else
17973
17974 -- In formal mode, when completing a private extension the type
17975 -- named in the private part must be exactly the same as that
17976 -- named in the visible part.
17977
17978 if Priv_Parent /= Full_Parent then
17979 Error_Msg_Name_1 := Chars (Priv_Parent);
17980 Check_SPARK_Restriction ("% expected", Full_Indic);
17981 end if;
17982
17983 -- Check the rules of 7.3(10): if the private extension inherits
17984 -- known discriminants, then the full type must also inherit those
17985 -- discriminants from the same (ancestor) type, and the parent
17986 -- subtype of the full type must be constrained if and only if
17987 -- the ancestor subtype of the private extension is constrained.
17988
17989 if No (Discriminant_Specifications (Parent (Priv_T)))
17990 and then not Has_Unknown_Discriminants (Priv_T)
17991 and then Has_Discriminants (Base_Type (Priv_Parent))
17992 then
17993 declare
17994 Priv_Indic : constant Node_Id :=
17995 Subtype_Indication (Parent (Priv_T));
17996
17997 Priv_Constr : constant Boolean :=
17998 Is_Constrained (Priv_Parent)
17999 or else
18000 Nkind (Priv_Indic) = N_Subtype_Indication
18001 or else
18002 Is_Constrained (Entity (Priv_Indic));
18003
18004 Full_Constr : constant Boolean :=
18005 Is_Constrained (Full_Parent)
18006 or else
18007 Nkind (Full_Indic) = N_Subtype_Indication
18008 or else
18009 Is_Constrained (Entity (Full_Indic));
18010
18011 Priv_Discr : Entity_Id;
18012 Full_Discr : Entity_Id;
18013
18014 begin
18015 Priv_Discr := First_Discriminant (Priv_Parent);
18016 Full_Discr := First_Discriminant (Full_Parent);
18017 while Present (Priv_Discr) and then Present (Full_Discr) loop
18018 if Original_Record_Component (Priv_Discr) =
18019 Original_Record_Component (Full_Discr)
18020 or else
18021 Corresponding_Discriminant (Priv_Discr) =
18022 Corresponding_Discriminant (Full_Discr)
18023 then
18024 null;
18025 else
18026 exit;
18027 end if;
18028
18029 Next_Discriminant (Priv_Discr);
18030 Next_Discriminant (Full_Discr);
18031 end loop;
18032
18033 if Present (Priv_Discr) or else Present (Full_Discr) then
18034 Error_Msg_N
18035 ("full view must inherit discriminants of the parent"
18036 & " type used in the private extension", Full_Indic);
18037
18038 elsif Priv_Constr and then not Full_Constr then
18039 Error_Msg_N
18040 ("parent subtype of full type must be constrained",
18041 Full_Indic);
18042
18043 elsif Full_Constr and then not Priv_Constr then
18044 Error_Msg_N
18045 ("parent subtype of full type must be unconstrained",
18046 Full_Indic);
18047 end if;
18048 end;
18049
18050 -- Check the rules of 7.3(12): if a partial view has neither
18051 -- known or unknown discriminants, then the full type
18052 -- declaration shall define a definite subtype.
18053
18054 elsif not Has_Unknown_Discriminants (Priv_T)
18055 and then not Has_Discriminants (Priv_T)
18056 and then not Is_Constrained (Full_T)
18057 then
18058 Error_Msg_N
18059 ("full view must define a constrained type if partial view"
18060 & " has no discriminants", Full_T);
18061 end if;
18062
18063 -- ??????? Do we implement the following properly ?????
18064 -- If the ancestor subtype of a private extension has constrained
18065 -- discriminants, then the parent subtype of the full view shall
18066 -- impose a statically matching constraint on those discriminants
18067 -- [7.3(13)].
18068 end if;
18069
18070 else
18071 -- For untagged types, verify that a type without discriminants
18072 -- is not completed with an unconstrained type.
18073
18074 if not Is_Indefinite_Subtype (Priv_T)
18075 and then Is_Indefinite_Subtype (Full_T)
18076 then
18077 Error_Msg_N ("full view of type must be definite subtype", Full_T);
18078 end if;
18079 end if;
18080
18081 -- AI-419: verify that the use of "limited" is consistent
18082
18083 declare
18084 Orig_Decl : constant Node_Id := Original_Node (N);
18085
18086 begin
18087 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18088 and then not Limited_Present (Parent (Priv_T))
18089 and then not Synchronized_Present (Parent (Priv_T))
18090 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
18091 and then Nkind
18092 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
18093 and then Limited_Present (Type_Definition (Orig_Decl))
18094 then
18095 Error_Msg_N
18096 ("full view of non-limited extension cannot be limited", N);
18097 end if;
18098 end;
18099
18100 -- Ada 2005 (AI-443): A synchronized private extension must be
18101 -- completed by a task or protected type.
18102
18103 if Ada_Version >= Ada_2005
18104 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18105 and then Synchronized_Present (Parent (Priv_T))
18106 and then not Is_Concurrent_Type (Full_T)
18107 then
18108 Error_Msg_N ("full view of synchronized extension must " &
18109 "be synchronized type", N);
18110 end if;
18111
18112 -- Ada 2005 AI-363: if the full view has discriminants with
18113 -- defaults, it is illegal to declare constrained access subtypes
18114 -- whose designated type is the current type. This allows objects
18115 -- of the type that are declared in the heap to be unconstrained.
18116
18117 if not Has_Unknown_Discriminants (Priv_T)
18118 and then not Has_Discriminants (Priv_T)
18119 and then Has_Discriminants (Full_T)
18120 and then
18121 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
18122 then
18123 Set_Has_Constrained_Partial_View (Full_T);
18124 Set_Has_Constrained_Partial_View (Priv_T);
18125 end if;
18126
18127 -- Create a full declaration for all its subtypes recorded in
18128 -- Private_Dependents and swap them similarly to the base type. These
18129 -- are subtypes that have been define before the full declaration of
18130 -- the private type. We also swap the entry in Private_Dependents list
18131 -- so we can properly restore the private view on exit from the scope.
18132
18133 declare
18134 Priv_Elmt : Elmt_Id;
18135 Priv : Entity_Id;
18136 Full : Entity_Id;
18137
18138 begin
18139 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
18140 while Present (Priv_Elmt) loop
18141 Priv := Node (Priv_Elmt);
18142
18143 if Ekind_In (Priv, E_Private_Subtype,
18144 E_Limited_Private_Subtype,
18145 E_Record_Subtype_With_Private)
18146 then
18147 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
18148 Set_Is_Itype (Full);
18149 Set_Parent (Full, Parent (Priv));
18150 Set_Associated_Node_For_Itype (Full, N);
18151
18152 -- Now we need to complete the private subtype, but since the
18153 -- base type has already been swapped, we must also swap the
18154 -- subtypes (and thus, reverse the arguments in the call to
18155 -- Complete_Private_Subtype).
18156
18157 Copy_And_Swap (Priv, Full);
18158 Complete_Private_Subtype (Full, Priv, Full_T, N);
18159 Replace_Elmt (Priv_Elmt, Full);
18160 end if;
18161
18162 Next_Elmt (Priv_Elmt);
18163 end loop;
18164 end;
18165
18166 -- If the private view was tagged, copy the new primitive operations
18167 -- from the private view to the full view.
18168
18169 if Is_Tagged_Type (Full_T) then
18170 declare
18171 Disp_Typ : Entity_Id;
18172 Full_List : Elist_Id;
18173 Prim : Entity_Id;
18174 Prim_Elmt : Elmt_Id;
18175 Priv_List : Elist_Id;
18176
18177 function Contains
18178 (E : Entity_Id;
18179 L : Elist_Id) return Boolean;
18180 -- Determine whether list L contains element E
18181
18182 --------------
18183 -- Contains --
18184 --------------
18185
18186 function Contains
18187 (E : Entity_Id;
18188 L : Elist_Id) return Boolean
18189 is
18190 List_Elmt : Elmt_Id;
18191
18192 begin
18193 List_Elmt := First_Elmt (L);
18194 while Present (List_Elmt) loop
18195 if Node (List_Elmt) = E then
18196 return True;
18197 end if;
18198
18199 Next_Elmt (List_Elmt);
18200 end loop;
18201
18202 return False;
18203 end Contains;
18204
18205 -- Start of processing
18206
18207 begin
18208 if Is_Tagged_Type (Priv_T) then
18209 Priv_List := Primitive_Operations (Priv_T);
18210 Prim_Elmt := First_Elmt (Priv_List);
18211
18212 -- In the case of a concurrent type completing a private tagged
18213 -- type, primitives may have been declared in between the two
18214 -- views. These subprograms need to be wrapped the same way
18215 -- entries and protected procedures are handled because they
18216 -- cannot be directly shared by the two views.
18217
18218 if Is_Concurrent_Type (Full_T) then
18219 declare
18220 Conc_Typ : constant Entity_Id :=
18221 Corresponding_Record_Type (Full_T);
18222 Curr_Nod : Node_Id := Parent (Conc_Typ);
18223 Wrap_Spec : Node_Id;
18224
18225 begin
18226 while Present (Prim_Elmt) loop
18227 Prim := Node (Prim_Elmt);
18228
18229 if Comes_From_Source (Prim)
18230 and then not Is_Abstract_Subprogram (Prim)
18231 then
18232 Wrap_Spec :=
18233 Make_Subprogram_Declaration (Sloc (Prim),
18234 Specification =>
18235 Build_Wrapper_Spec
18236 (Subp_Id => Prim,
18237 Obj_Typ => Conc_Typ,
18238 Formals =>
18239 Parameter_Specifications (
18240 Parent (Prim))));
18241
18242 Insert_After (Curr_Nod, Wrap_Spec);
18243 Curr_Nod := Wrap_Spec;
18244
18245 Analyze (Wrap_Spec);
18246 end if;
18247
18248 Next_Elmt (Prim_Elmt);
18249 end loop;
18250
18251 return;
18252 end;
18253
18254 -- For non-concurrent types, transfer explicit primitives, but
18255 -- omit those inherited from the parent of the private view
18256 -- since they will be re-inherited later on.
18257
18258 else
18259 Full_List := Primitive_Operations (Full_T);
18260
18261 while Present (Prim_Elmt) loop
18262 Prim := Node (Prim_Elmt);
18263
18264 if Comes_From_Source (Prim)
18265 and then not Contains (Prim, Full_List)
18266 then
18267 Append_Elmt (Prim, Full_List);
18268 end if;
18269
18270 Next_Elmt (Prim_Elmt);
18271 end loop;
18272 end if;
18273
18274 -- Untagged private view
18275
18276 else
18277 Full_List := Primitive_Operations (Full_T);
18278
18279 -- In this case the partial view is untagged, so here we locate
18280 -- all of the earlier primitives that need to be treated as
18281 -- dispatching (those that appear between the two views). Note
18282 -- that these additional operations must all be new operations
18283 -- (any earlier operations that override inherited operations
18284 -- of the full view will already have been inserted in the
18285 -- primitives list, marked by Check_Operation_From_Private_View
18286 -- as dispatching. Note that implicit "/=" operators are
18287 -- excluded from being added to the primitives list since they
18288 -- shouldn't be treated as dispatching (tagged "/=" is handled
18289 -- specially).
18290
18291 Prim := Next_Entity (Full_T);
18292 while Present (Prim) and then Prim /= Priv_T loop
18293 if Ekind_In (Prim, E_Procedure, E_Function) then
18294 Disp_Typ := Find_Dispatching_Type (Prim);
18295
18296 if Disp_Typ = Full_T
18297 and then (Chars (Prim) /= Name_Op_Ne
18298 or else Comes_From_Source (Prim))
18299 then
18300 Check_Controlling_Formals (Full_T, Prim);
18301
18302 if not Is_Dispatching_Operation (Prim) then
18303 Append_Elmt (Prim, Full_List);
18304 Set_Is_Dispatching_Operation (Prim, True);
18305 Set_DT_Position (Prim, No_Uint);
18306 end if;
18307
18308 elsif Is_Dispatching_Operation (Prim)
18309 and then Disp_Typ /= Full_T
18310 then
18311
18312 -- Verify that it is not otherwise controlled by a
18313 -- formal or a return value of type T.
18314
18315 Check_Controlling_Formals (Disp_Typ, Prim);
18316 end if;
18317 end if;
18318
18319 Next_Entity (Prim);
18320 end loop;
18321 end if;
18322
18323 -- For the tagged case, the two views can share the same primitive
18324 -- operations list and the same class-wide type. Update attributes
18325 -- of the class-wide type which depend on the full declaration.
18326
18327 if Is_Tagged_Type (Priv_T) then
18328 Set_Direct_Primitive_Operations (Priv_T, Full_List);
18329 Set_Class_Wide_Type
18330 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
18331
18332 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
18333 end if;
18334 end;
18335 end if;
18336
18337 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
18338
18339 if Known_To_Have_Preelab_Init (Priv_T) then
18340
18341 -- Case where there is a pragma Preelaborable_Initialization. We
18342 -- always allow this in predefined units, which is a bit of a kludge,
18343 -- but it means we don't have to struggle to meet the requirements in
18344 -- the RM for having Preelaborable Initialization. Otherwise we
18345 -- require that the type meets the RM rules. But we can't check that
18346 -- yet, because of the rule about overriding Initialize, so we simply
18347 -- set a flag that will be checked at freeze time.
18348
18349 if not In_Predefined_Unit (Full_T) then
18350 Set_Must_Have_Preelab_Init (Full_T);
18351 end if;
18352 end if;
18353
18354 -- If pragma CPP_Class was applied to the private type declaration,
18355 -- propagate it now to the full type declaration.
18356
18357 if Is_CPP_Class (Priv_T) then
18358 Set_Is_CPP_Class (Full_T);
18359 Set_Convention (Full_T, Convention_CPP);
18360
18361 -- Check that components of imported CPP types do not have default
18362 -- expressions.
18363
18364 Check_CPP_Type_Has_No_Defaults (Full_T);
18365 end if;
18366
18367 -- If the private view has user specified stream attributes, then so has
18368 -- the full view.
18369
18370 -- Why the test, how could these flags be already set in Full_T ???
18371
18372 if Has_Specified_Stream_Read (Priv_T) then
18373 Set_Has_Specified_Stream_Read (Full_T);
18374 end if;
18375
18376 if Has_Specified_Stream_Write (Priv_T) then
18377 Set_Has_Specified_Stream_Write (Full_T);
18378 end if;
18379
18380 if Has_Specified_Stream_Input (Priv_T) then
18381 Set_Has_Specified_Stream_Input (Full_T);
18382 end if;
18383
18384 if Has_Specified_Stream_Output (Priv_T) then
18385 Set_Has_Specified_Stream_Output (Full_T);
18386 end if;
18387
18388 -- Propagate invariants to full type
18389
18390 if Has_Invariants (Priv_T) then
18391 Set_Has_Invariants (Full_T);
18392 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
18393 end if;
18394
18395 if Has_Inheritable_Invariants (Priv_T) then
18396 Set_Has_Inheritable_Invariants (Full_T);
18397 end if;
18398
18399 -- Propagate predicates to full type
18400
18401 if Has_Predicates (Priv_T) then
18402 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
18403 Set_Has_Predicates (Full_T);
18404 end if;
18405 end Process_Full_View;
18406
18407 -----------------------------------
18408 -- Process_Incomplete_Dependents --
18409 -----------------------------------
18410
18411 procedure Process_Incomplete_Dependents
18412 (N : Node_Id;
18413 Full_T : Entity_Id;
18414 Inc_T : Entity_Id)
18415 is
18416 Inc_Elmt : Elmt_Id;
18417 Priv_Dep : Entity_Id;
18418 New_Subt : Entity_Id;
18419
18420 Disc_Constraint : Elist_Id;
18421
18422 begin
18423 if No (Private_Dependents (Inc_T)) then
18424 return;
18425 end if;
18426
18427 -- Itypes that may be generated by the completion of an incomplete
18428 -- subtype are not used by the back-end and not attached to the tree.
18429 -- They are created only for constraint-checking purposes.
18430
18431 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
18432 while Present (Inc_Elmt) loop
18433 Priv_Dep := Node (Inc_Elmt);
18434
18435 if Ekind (Priv_Dep) = E_Subprogram_Type then
18436
18437 -- An Access_To_Subprogram type may have a return type or a
18438 -- parameter type that is incomplete. Replace with the full view.
18439
18440 if Etype (Priv_Dep) = Inc_T then
18441 Set_Etype (Priv_Dep, Full_T);
18442 end if;
18443
18444 declare
18445 Formal : Entity_Id;
18446
18447 begin
18448 Formal := First_Formal (Priv_Dep);
18449 while Present (Formal) loop
18450 if Etype (Formal) = Inc_T then
18451 Set_Etype (Formal, Full_T);
18452 end if;
18453
18454 Next_Formal (Formal);
18455 end loop;
18456 end;
18457
18458 elsif Is_Overloadable (Priv_Dep) then
18459
18460 -- If a subprogram in the incomplete dependents list is primitive
18461 -- for a tagged full type then mark it as a dispatching operation,
18462 -- check whether it overrides an inherited subprogram, and check
18463 -- restrictions on its controlling formals. Note that a protected
18464 -- operation is never dispatching: only its wrapper operation
18465 -- (which has convention Ada) is.
18466
18467 if Is_Tagged_Type (Full_T)
18468 and then Is_Primitive (Priv_Dep)
18469 and then Convention (Priv_Dep) /= Convention_Protected
18470 then
18471 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
18472 Set_Is_Dispatching_Operation (Priv_Dep);
18473 Check_Controlling_Formals (Full_T, Priv_Dep);
18474 end if;
18475
18476 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
18477
18478 -- Can happen during processing of a body before the completion
18479 -- of a TA type. Ignore, because spec is also on dependent list.
18480
18481 return;
18482
18483 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
18484 -- corresponding subtype of the full view.
18485
18486 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
18487 Set_Subtype_Indication
18488 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
18489 Set_Etype (Priv_Dep, Full_T);
18490 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
18491 Set_Analyzed (Parent (Priv_Dep), False);
18492
18493 -- Reanalyze the declaration, suppressing the call to
18494 -- Enter_Name to avoid duplicate names.
18495
18496 Analyze_Subtype_Declaration
18497 (N => Parent (Priv_Dep),
18498 Skip => True);
18499
18500 -- Dependent is a subtype
18501
18502 else
18503 -- We build a new subtype indication using the full view of the
18504 -- incomplete parent. The discriminant constraints have been
18505 -- elaborated already at the point of the subtype declaration.
18506
18507 New_Subt := Create_Itype (E_Void, N);
18508
18509 if Has_Discriminants (Full_T) then
18510 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
18511 else
18512 Disc_Constraint := No_Elist;
18513 end if;
18514
18515 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
18516 Set_Full_View (Priv_Dep, New_Subt);
18517 end if;
18518
18519 Next_Elmt (Inc_Elmt);
18520 end loop;
18521 end Process_Incomplete_Dependents;
18522
18523 --------------------------------
18524 -- Process_Range_Expr_In_Decl --
18525 --------------------------------
18526
18527 procedure Process_Range_Expr_In_Decl
18528 (R : Node_Id;
18529 T : Entity_Id;
18530 Check_List : List_Id := Empty_List;
18531 R_Check_Off : Boolean := False;
18532 In_Iter_Schm : Boolean := False)
18533 is
18534 Lo, Hi : Node_Id;
18535 R_Checks : Check_Result;
18536 Insert_Node : Node_Id;
18537 Def_Id : Entity_Id;
18538
18539 begin
18540 Analyze_And_Resolve (R, Base_Type (T));
18541
18542 if Nkind (R) = N_Range then
18543
18544 -- In SPARK, all ranges should be static, with the exception of the
18545 -- discrete type definition of a loop parameter specification.
18546
18547 if not In_Iter_Schm
18548 and then not Is_Static_Range (R)
18549 then
18550 Check_SPARK_Restriction ("range should be static", R);
18551 end if;
18552
18553 Lo := Low_Bound (R);
18554 Hi := High_Bound (R);
18555
18556 -- We need to ensure validity of the bounds here, because if we
18557 -- go ahead and do the expansion, then the expanded code will get
18558 -- analyzed with range checks suppressed and we miss the check.
18559
18560 Validity_Check_Range (R);
18561
18562 -- If there were errors in the declaration, try and patch up some
18563 -- common mistakes in the bounds. The cases handled are literals
18564 -- which are Integer where the expected type is Real and vice versa.
18565 -- These corrections allow the compilation process to proceed further
18566 -- along since some basic assumptions of the format of the bounds
18567 -- are guaranteed.
18568
18569 if Etype (R) = Any_Type then
18570
18571 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
18572 Rewrite (Lo,
18573 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
18574
18575 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
18576 Rewrite (Hi,
18577 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
18578
18579 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
18580 Rewrite (Lo,
18581 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
18582
18583 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
18584 Rewrite (Hi,
18585 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
18586 end if;
18587
18588 Set_Etype (Lo, T);
18589 Set_Etype (Hi, T);
18590 end if;
18591
18592 -- If the bounds of the range have been mistakenly given as string
18593 -- literals (perhaps in place of character literals), then an error
18594 -- has already been reported, but we rewrite the string literal as a
18595 -- bound of the range's type to avoid blowups in later processing
18596 -- that looks at static values.
18597
18598 if Nkind (Lo) = N_String_Literal then
18599 Rewrite (Lo,
18600 Make_Attribute_Reference (Sloc (Lo),
18601 Attribute_Name => Name_First,
18602 Prefix => New_Reference_To (T, Sloc (Lo))));
18603 Analyze_And_Resolve (Lo);
18604 end if;
18605
18606 if Nkind (Hi) = N_String_Literal then
18607 Rewrite (Hi,
18608 Make_Attribute_Reference (Sloc (Hi),
18609 Attribute_Name => Name_First,
18610 Prefix => New_Reference_To (T, Sloc (Hi))));
18611 Analyze_And_Resolve (Hi);
18612 end if;
18613
18614 -- If bounds aren't scalar at this point then exit, avoiding
18615 -- problems with further processing of the range in this procedure.
18616
18617 if not Is_Scalar_Type (Etype (Lo)) then
18618 return;
18619 end if;
18620
18621 -- Resolve (actually Sem_Eval) has checked that the bounds are in
18622 -- then range of the base type. Here we check whether the bounds
18623 -- are in the range of the subtype itself. Note that if the bounds
18624 -- represent the null range the Constraint_Error exception should
18625 -- not be raised.
18626
18627 -- ??? The following code should be cleaned up as follows
18628
18629 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
18630 -- is done in the call to Range_Check (R, T); below
18631
18632 -- 2. The use of R_Check_Off should be investigated and possibly
18633 -- removed, this would clean up things a bit.
18634
18635 if Is_Null_Range (Lo, Hi) then
18636 null;
18637
18638 else
18639 -- Capture values of bounds and generate temporaries for them
18640 -- if needed, before applying checks, since checks may cause
18641 -- duplication of the expression without forcing evaluation.
18642
18643 -- The forced evaluation removes side effects from expressions,
18644 -- which should occur also in SPARK mode. Otherwise, we end up
18645 -- with unexpected insertions of actions at places where this is
18646 -- not supposed to occur, e.g. on default parameters of a call.
18647
18648 if Expander_Active then
18649 Force_Evaluation (Lo);
18650 Force_Evaluation (Hi);
18651 end if;
18652
18653 -- We use a flag here instead of suppressing checks on the
18654 -- type because the type we check against isn't necessarily
18655 -- the place where we put the check.
18656
18657 if not R_Check_Off then
18658 R_Checks := Get_Range_Checks (R, T);
18659
18660 -- Look up tree to find an appropriate insertion point. We
18661 -- can't just use insert_actions because later processing
18662 -- depends on the insertion node. Prior to Ada 2012 the
18663 -- insertion point could only be a declaration or a loop, but
18664 -- quantified expressions can appear within any context in an
18665 -- expression, and the insertion point can be any statement,
18666 -- pragma, or declaration.
18667
18668 Insert_Node := Parent (R);
18669 while Present (Insert_Node) loop
18670 exit when
18671 Nkind (Insert_Node) in N_Declaration
18672 and then
18673 not Nkind_In
18674 (Insert_Node, N_Component_Declaration,
18675 N_Loop_Parameter_Specification,
18676 N_Function_Specification,
18677 N_Procedure_Specification);
18678
18679 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18680 or else Nkind (Insert_Node) in
18681 N_Statement_Other_Than_Procedure_Call
18682 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18683 N_Pragma);
18684
18685 Insert_Node := Parent (Insert_Node);
18686 end loop;
18687
18688 -- Why would Type_Decl not be present??? Without this test,
18689 -- short regression tests fail.
18690
18691 if Present (Insert_Node) then
18692
18693 -- Case of loop statement. Verify that the range is part
18694 -- of the subtype indication of the iteration scheme.
18695
18696 if Nkind (Insert_Node) = N_Loop_Statement then
18697 declare
18698 Indic : Node_Id;
18699
18700 begin
18701 Indic := Parent (R);
18702 while Present (Indic)
18703 and then Nkind (Indic) /= N_Subtype_Indication
18704 loop
18705 Indic := Parent (Indic);
18706 end loop;
18707
18708 if Present (Indic) then
18709 Def_Id := Etype (Subtype_Mark (Indic));
18710
18711 Insert_Range_Checks
18712 (R_Checks,
18713 Insert_Node,
18714 Def_Id,
18715 Sloc (Insert_Node),
18716 R,
18717 Do_Before => True);
18718 end if;
18719 end;
18720
18721 -- Insertion before a declaration. If the declaration
18722 -- includes discriminants, the list of applicable checks
18723 -- is given by the caller.
18724
18725 elsif Nkind (Insert_Node) in N_Declaration then
18726 Def_Id := Defining_Identifier (Insert_Node);
18727
18728 if (Ekind (Def_Id) = E_Record_Type
18729 and then Depends_On_Discriminant (R))
18730 or else
18731 (Ekind (Def_Id) = E_Protected_Type
18732 and then Has_Discriminants (Def_Id))
18733 then
18734 Append_Range_Checks
18735 (R_Checks,
18736 Check_List, Def_Id, Sloc (Insert_Node), R);
18737
18738 else
18739 Insert_Range_Checks
18740 (R_Checks,
18741 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18742
18743 end if;
18744
18745 -- Insertion before a statement. Range appears in the
18746 -- context of a quantified expression. Insertion will
18747 -- take place when expression is expanded.
18748
18749 else
18750 null;
18751 end if;
18752 end if;
18753 end if;
18754 end if;
18755
18756 -- Case of other than an explicit N_Range node
18757
18758 -- The forced evaluation removes side effects from expressions, which
18759 -- should occur also in SPARK mode. Otherwise, we end up with unexpected
18760 -- insertions of actions at places where this is not supposed to occur,
18761 -- e.g. on default parameters of a call.
18762
18763 elsif Expander_Active then
18764 Get_Index_Bounds (R, Lo, Hi);
18765 Force_Evaluation (Lo);
18766 Force_Evaluation (Hi);
18767 end if;
18768 end Process_Range_Expr_In_Decl;
18769
18770 --------------------------------------
18771 -- Process_Real_Range_Specification --
18772 --------------------------------------
18773
18774 procedure Process_Real_Range_Specification (Def : Node_Id) is
18775 Spec : constant Node_Id := Real_Range_Specification (Def);
18776 Lo : Node_Id;
18777 Hi : Node_Id;
18778 Err : Boolean := False;
18779
18780 procedure Analyze_Bound (N : Node_Id);
18781 -- Analyze and check one bound
18782
18783 -------------------
18784 -- Analyze_Bound --
18785 -------------------
18786
18787 procedure Analyze_Bound (N : Node_Id) is
18788 begin
18789 Analyze_And_Resolve (N, Any_Real);
18790
18791 if not Is_OK_Static_Expression (N) then
18792 Flag_Non_Static_Expr
18793 ("bound in real type definition is not static!", N);
18794 Err := True;
18795 end if;
18796 end Analyze_Bound;
18797
18798 -- Start of processing for Process_Real_Range_Specification
18799
18800 begin
18801 if Present (Spec) then
18802 Lo := Low_Bound (Spec);
18803 Hi := High_Bound (Spec);
18804 Analyze_Bound (Lo);
18805 Analyze_Bound (Hi);
18806
18807 -- If error, clear away junk range specification
18808
18809 if Err then
18810 Set_Real_Range_Specification (Def, Empty);
18811 end if;
18812 end if;
18813 end Process_Real_Range_Specification;
18814
18815 ---------------------
18816 -- Process_Subtype --
18817 ---------------------
18818
18819 function Process_Subtype
18820 (S : Node_Id;
18821 Related_Nod : Node_Id;
18822 Related_Id : Entity_Id := Empty;
18823 Suffix : Character := ' ') return Entity_Id
18824 is
18825 P : Node_Id;
18826 Def_Id : Entity_Id;
18827 Error_Node : Node_Id;
18828 Full_View_Id : Entity_Id;
18829 Subtype_Mark_Id : Entity_Id;
18830
18831 May_Have_Null_Exclusion : Boolean;
18832
18833 procedure Check_Incomplete (T : Entity_Id);
18834 -- Called to verify that an incomplete type is not used prematurely
18835
18836 ----------------------
18837 -- Check_Incomplete --
18838 ----------------------
18839
18840 procedure Check_Incomplete (T : Entity_Id) is
18841 begin
18842 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18843
18844 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18845 and then
18846 not (Ada_Version >= Ada_2005
18847 and then
18848 (Nkind (Parent (T)) = N_Subtype_Declaration
18849 or else
18850 (Nkind (Parent (T)) = N_Subtype_Indication
18851 and then Nkind (Parent (Parent (T))) =
18852 N_Subtype_Declaration)))
18853 then
18854 Error_Msg_N ("invalid use of type before its full declaration", T);
18855 end if;
18856 end Check_Incomplete;
18857
18858 -- Start of processing for Process_Subtype
18859
18860 begin
18861 -- Case of no constraints present
18862
18863 if Nkind (S) /= N_Subtype_Indication then
18864 Find_Type (S);
18865 Check_Incomplete (S);
18866 P := Parent (S);
18867
18868 -- Ada 2005 (AI-231): Static check
18869
18870 if Ada_Version >= Ada_2005
18871 and then Present (P)
18872 and then Null_Exclusion_Present (P)
18873 and then Nkind (P) /= N_Access_To_Object_Definition
18874 and then not Is_Access_Type (Entity (S))
18875 then
18876 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18877 end if;
18878
18879 -- The following is ugly, can't we have a range or even a flag???
18880
18881 May_Have_Null_Exclusion :=
18882 Nkind_In (P, N_Access_Definition,
18883 N_Access_Function_Definition,
18884 N_Access_Procedure_Definition,
18885 N_Access_To_Object_Definition,
18886 N_Allocator,
18887 N_Component_Definition)
18888 or else
18889 Nkind_In (P, N_Derived_Type_Definition,
18890 N_Discriminant_Specification,
18891 N_Formal_Object_Declaration,
18892 N_Object_Declaration,
18893 N_Object_Renaming_Declaration,
18894 N_Parameter_Specification,
18895 N_Subtype_Declaration);
18896
18897 -- Create an Itype that is a duplicate of Entity (S) but with the
18898 -- null-exclusion attribute.
18899
18900 if May_Have_Null_Exclusion
18901 and then Is_Access_Type (Entity (S))
18902 and then Null_Exclusion_Present (P)
18903
18904 -- No need to check the case of an access to object definition.
18905 -- It is correct to define double not-null pointers.
18906
18907 -- Example:
18908 -- type Not_Null_Int_Ptr is not null access Integer;
18909 -- type Acc is not null access Not_Null_Int_Ptr;
18910
18911 and then Nkind (P) /= N_Access_To_Object_Definition
18912 then
18913 if Can_Never_Be_Null (Entity (S)) then
18914 case Nkind (Related_Nod) is
18915 when N_Full_Type_Declaration =>
18916 if Nkind (Type_Definition (Related_Nod))
18917 in N_Array_Type_Definition
18918 then
18919 Error_Node :=
18920 Subtype_Indication
18921 (Component_Definition
18922 (Type_Definition (Related_Nod)));
18923 else
18924 Error_Node :=
18925 Subtype_Indication (Type_Definition (Related_Nod));
18926 end if;
18927
18928 when N_Subtype_Declaration =>
18929 Error_Node := Subtype_Indication (Related_Nod);
18930
18931 when N_Object_Declaration =>
18932 Error_Node := Object_Definition (Related_Nod);
18933
18934 when N_Component_Declaration =>
18935 Error_Node :=
18936 Subtype_Indication (Component_Definition (Related_Nod));
18937
18938 when N_Allocator =>
18939 Error_Node := Expression (Related_Nod);
18940
18941 when others =>
18942 pragma Assert (False);
18943 Error_Node := Related_Nod;
18944 end case;
18945
18946 Error_Msg_NE
18947 ("`NOT NULL` not allowed (& already excludes null)",
18948 Error_Node,
18949 Entity (S));
18950 end if;
18951
18952 Set_Etype (S,
18953 Create_Null_Excluding_Itype
18954 (T => Entity (S),
18955 Related_Nod => P));
18956 Set_Entity (S, Etype (S));
18957 end if;
18958
18959 return Entity (S);
18960
18961 -- Case of constraint present, so that we have an N_Subtype_Indication
18962 -- node (this node is created only if constraints are present).
18963
18964 else
18965 Find_Type (Subtype_Mark (S));
18966
18967 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18968 and then not
18969 (Nkind (Parent (S)) = N_Subtype_Declaration
18970 and then Is_Itype (Defining_Identifier (Parent (S))))
18971 then
18972 Check_Incomplete (Subtype_Mark (S));
18973 end if;
18974
18975 P := Parent (S);
18976 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18977
18978 -- Explicit subtype declaration case
18979
18980 if Nkind (P) = N_Subtype_Declaration then
18981 Def_Id := Defining_Identifier (P);
18982
18983 -- Explicit derived type definition case
18984
18985 elsif Nkind (P) = N_Derived_Type_Definition then
18986 Def_Id := Defining_Identifier (Parent (P));
18987
18988 -- Implicit case, the Def_Id must be created as an implicit type.
18989 -- The one exception arises in the case of concurrent types, array
18990 -- and access types, where other subsidiary implicit types may be
18991 -- created and must appear before the main implicit type. In these
18992 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18993 -- has not yet been called to create Def_Id.
18994
18995 else
18996 if Is_Array_Type (Subtype_Mark_Id)
18997 or else Is_Concurrent_Type (Subtype_Mark_Id)
18998 or else Is_Access_Type (Subtype_Mark_Id)
18999 then
19000 Def_Id := Empty;
19001
19002 -- For the other cases, we create a new unattached Itype,
19003 -- and set the indication to ensure it gets attached later.
19004
19005 else
19006 Def_Id :=
19007 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19008 end if;
19009 end if;
19010
19011 -- If the kind of constraint is invalid for this kind of type,
19012 -- then give an error, and then pretend no constraint was given.
19013
19014 if not Is_Valid_Constraint_Kind
19015 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
19016 then
19017 Error_Msg_N
19018 ("incorrect constraint for this kind of type", Constraint (S));
19019
19020 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
19021
19022 -- Set Ekind of orphan itype, to prevent cascaded errors
19023
19024 if Present (Def_Id) then
19025 Set_Ekind (Def_Id, Ekind (Any_Type));
19026 end if;
19027
19028 -- Make recursive call, having got rid of the bogus constraint
19029
19030 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
19031 end if;
19032
19033 -- Remaining processing depends on type. Select on Base_Type kind to
19034 -- ensure getting to the concrete type kind in the case of a private
19035 -- subtype (needed when only doing semantic analysis).
19036
19037 case Ekind (Base_Type (Subtype_Mark_Id)) is
19038 when Access_Kind =>
19039
19040 -- If this is a constraint on a class-wide type, discard it.
19041 -- There is currently no way to express a partial discriminant
19042 -- constraint on a type with unknown discriminants. This is
19043 -- a pathology that the ACATS wisely decides not to test.
19044
19045 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
19046 if Comes_From_Source (S) then
19047 Error_Msg_N
19048 ("constraint on class-wide type ignored?",
19049 Constraint (S));
19050 end if;
19051
19052 if Nkind (P) = N_Subtype_Declaration then
19053 Set_Subtype_Indication (P,
19054 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
19055 end if;
19056
19057 return Subtype_Mark_Id;
19058 end if;
19059
19060 Constrain_Access (Def_Id, S, Related_Nod);
19061
19062 if Expander_Active
19063 and then Is_Itype (Designated_Type (Def_Id))
19064 and then Nkind (Related_Nod) = N_Subtype_Declaration
19065 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
19066 then
19067 Build_Itype_Reference
19068 (Designated_Type (Def_Id), Related_Nod);
19069 end if;
19070
19071 when Array_Kind =>
19072 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
19073
19074 when Decimal_Fixed_Point_Kind =>
19075 Constrain_Decimal (Def_Id, S);
19076
19077 when Enumeration_Kind =>
19078 Constrain_Enumeration (Def_Id, S);
19079
19080 when Ordinary_Fixed_Point_Kind =>
19081 Constrain_Ordinary_Fixed (Def_Id, S);
19082
19083 when Float_Kind =>
19084 Constrain_Float (Def_Id, S);
19085
19086 when Integer_Kind =>
19087 Constrain_Integer (Def_Id, S);
19088
19089 when E_Record_Type |
19090 E_Record_Subtype |
19091 Class_Wide_Kind |
19092 E_Incomplete_Type =>
19093 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19094
19095 if Ekind (Def_Id) = E_Incomplete_Type then
19096 Set_Private_Dependents (Def_Id, New_Elmt_List);
19097 end if;
19098
19099 when Private_Kind =>
19100 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19101 Set_Private_Dependents (Def_Id, New_Elmt_List);
19102
19103 -- In case of an invalid constraint prevent further processing
19104 -- since the type constructed is missing expected fields.
19105
19106 if Etype (Def_Id) = Any_Type then
19107 return Def_Id;
19108 end if;
19109
19110 -- If the full view is that of a task with discriminants,
19111 -- we must constrain both the concurrent type and its
19112 -- corresponding record type. Otherwise we will just propagate
19113 -- the constraint to the full view, if available.
19114
19115 if Present (Full_View (Subtype_Mark_Id))
19116 and then Has_Discriminants (Subtype_Mark_Id)
19117 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
19118 then
19119 Full_View_Id :=
19120 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19121
19122 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
19123 Constrain_Concurrent (Full_View_Id, S,
19124 Related_Nod, Related_Id, Suffix);
19125 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
19126 Set_Full_View (Def_Id, Full_View_Id);
19127
19128 -- Introduce an explicit reference to the private subtype,
19129 -- to prevent scope anomalies in gigi if first use appears
19130 -- in a nested context, e.g. a later function body.
19131 -- Should this be generated in other contexts than a full
19132 -- type declaration?
19133
19134 if Is_Itype (Def_Id)
19135 and then
19136 Nkind (Parent (P)) = N_Full_Type_Declaration
19137 then
19138 Build_Itype_Reference (Def_Id, Parent (P));
19139 end if;
19140
19141 else
19142 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
19143 end if;
19144
19145 when Concurrent_Kind =>
19146 Constrain_Concurrent (Def_Id, S,
19147 Related_Nod, Related_Id, Suffix);
19148
19149 when others =>
19150 Error_Msg_N ("invalid subtype mark in subtype indication", S);
19151 end case;
19152
19153 -- Size and Convention are always inherited from the base type
19154
19155 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
19156 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
19157
19158 return Def_Id;
19159 end if;
19160 end Process_Subtype;
19161
19162 ---------------------------------------
19163 -- Check_Anonymous_Access_Components --
19164 ---------------------------------------
19165
19166 procedure Check_Anonymous_Access_Components
19167 (Typ_Decl : Node_Id;
19168 Typ : Entity_Id;
19169 Prev : Entity_Id;
19170 Comp_List : Node_Id)
19171 is
19172 Loc : constant Source_Ptr := Sloc (Typ_Decl);
19173 Anon_Access : Entity_Id;
19174 Acc_Def : Node_Id;
19175 Comp : Node_Id;
19176 Comp_Def : Node_Id;
19177 Decl : Node_Id;
19178 Type_Def : Node_Id;
19179
19180 procedure Build_Incomplete_Type_Declaration;
19181 -- If the record type contains components that include an access to the
19182 -- current record, then create an incomplete type declaration for the
19183 -- record, to be used as the designated type of the anonymous access.
19184 -- This is done only once, and only if there is no previous partial
19185 -- view of the type.
19186
19187 function Designates_T (Subt : Node_Id) return Boolean;
19188 -- Check whether a node designates the enclosing record type, or 'Class
19189 -- of that type
19190
19191 function Mentions_T (Acc_Def : Node_Id) return Boolean;
19192 -- Check whether an access definition includes a reference to
19193 -- the enclosing record type. The reference can be a subtype mark
19194 -- in the access definition itself, a 'Class attribute reference, or
19195 -- recursively a reference appearing in a parameter specification
19196 -- or result definition of an access_to_subprogram definition.
19197
19198 --------------------------------------
19199 -- Build_Incomplete_Type_Declaration --
19200 --------------------------------------
19201
19202 procedure Build_Incomplete_Type_Declaration is
19203 Decl : Node_Id;
19204 Inc_T : Entity_Id;
19205 H : Entity_Id;
19206
19207 -- Is_Tagged indicates whether the type is tagged. It is tagged if
19208 -- it's "is new ... with record" or else "is tagged record ...".
19209
19210 Is_Tagged : constant Boolean :=
19211 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
19212 and then
19213 Present
19214 (Record_Extension_Part (Type_Definition (Typ_Decl))))
19215 or else
19216 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
19217 and then Tagged_Present (Type_Definition (Typ_Decl)));
19218
19219 begin
19220 -- If there is a previous partial view, no need to create a new one
19221 -- If the partial view, given by Prev, is incomplete, If Prev is
19222 -- a private declaration, full declaration is flagged accordingly.
19223
19224 if Prev /= Typ then
19225 if Is_Tagged then
19226 Make_Class_Wide_Type (Prev);
19227 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
19228 Set_Etype (Class_Wide_Type (Typ), Typ);
19229 end if;
19230
19231 return;
19232
19233 elsif Has_Private_Declaration (Typ) then
19234
19235 -- If we refer to T'Class inside T, and T is the completion of a
19236 -- private type, then we need to make sure the class-wide type
19237 -- exists.
19238
19239 if Is_Tagged then
19240 Make_Class_Wide_Type (Typ);
19241 end if;
19242
19243 return;
19244
19245 -- If there was a previous anonymous access type, the incomplete
19246 -- type declaration will have been created already.
19247
19248 elsif Present (Current_Entity (Typ))
19249 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
19250 and then Full_View (Current_Entity (Typ)) = Typ
19251 then
19252 if Is_Tagged
19253 and then Comes_From_Source (Current_Entity (Typ))
19254 and then not Is_Tagged_Type (Current_Entity (Typ))
19255 then
19256 Make_Class_Wide_Type (Typ);
19257 Error_Msg_N
19258 ("incomplete view of tagged type should be declared tagged??",
19259 Parent (Current_Entity (Typ)));
19260 end if;
19261 return;
19262
19263 else
19264 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
19265 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
19266
19267 -- Type has already been inserted into the current scope. Remove
19268 -- it, and add incomplete declaration for type, so that subsequent
19269 -- anonymous access types can use it. The entity is unchained from
19270 -- the homonym list and from immediate visibility. After analysis,
19271 -- the entity in the incomplete declaration becomes immediately
19272 -- visible in the record declaration that follows.
19273
19274 H := Current_Entity (Typ);
19275
19276 if H = Typ then
19277 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
19278 else
19279 while Present (H)
19280 and then Homonym (H) /= Typ
19281 loop
19282 H := Homonym (Typ);
19283 end loop;
19284
19285 Set_Homonym (H, Homonym (Typ));
19286 end if;
19287
19288 Insert_Before (Typ_Decl, Decl);
19289 Analyze (Decl);
19290 Set_Full_View (Inc_T, Typ);
19291
19292 if Is_Tagged then
19293
19294 -- Create a common class-wide type for both views, and set the
19295 -- Etype of the class-wide type to the full view.
19296
19297 Make_Class_Wide_Type (Inc_T);
19298 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
19299 Set_Etype (Class_Wide_Type (Typ), Typ);
19300 end if;
19301 end if;
19302 end Build_Incomplete_Type_Declaration;
19303
19304 ------------------
19305 -- Designates_T --
19306 ------------------
19307
19308 function Designates_T (Subt : Node_Id) return Boolean is
19309 Type_Id : constant Name_Id := Chars (Typ);
19310
19311 function Names_T (Nam : Node_Id) return Boolean;
19312 -- The record type has not been introduced in the current scope
19313 -- yet, so we must examine the name of the type itself, either
19314 -- an identifier T, or an expanded name of the form P.T, where
19315 -- P denotes the current scope.
19316
19317 -------------
19318 -- Names_T --
19319 -------------
19320
19321 function Names_T (Nam : Node_Id) return Boolean is
19322 begin
19323 if Nkind (Nam) = N_Identifier then
19324 return Chars (Nam) = Type_Id;
19325
19326 elsif Nkind (Nam) = N_Selected_Component then
19327 if Chars (Selector_Name (Nam)) = Type_Id then
19328 if Nkind (Prefix (Nam)) = N_Identifier then
19329 return Chars (Prefix (Nam)) = Chars (Current_Scope);
19330
19331 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
19332 return Chars (Selector_Name (Prefix (Nam))) =
19333 Chars (Current_Scope);
19334 else
19335 return False;
19336 end if;
19337
19338 else
19339 return False;
19340 end if;
19341
19342 else
19343 return False;
19344 end if;
19345 end Names_T;
19346
19347 -- Start of processing for Designates_T
19348
19349 begin
19350 if Nkind (Subt) = N_Identifier then
19351 return Chars (Subt) = Type_Id;
19352
19353 -- Reference can be through an expanded name which has not been
19354 -- analyzed yet, and which designates enclosing scopes.
19355
19356 elsif Nkind (Subt) = N_Selected_Component then
19357 if Names_T (Subt) then
19358 return True;
19359
19360 -- Otherwise it must denote an entity that is already visible.
19361 -- The access definition may name a subtype of the enclosing
19362 -- type, if there is a previous incomplete declaration for it.
19363
19364 else
19365 Find_Selected_Component (Subt);
19366 return
19367 Is_Entity_Name (Subt)
19368 and then Scope (Entity (Subt)) = Current_Scope
19369 and then
19370 (Chars (Base_Type (Entity (Subt))) = Type_Id
19371 or else
19372 (Is_Class_Wide_Type (Entity (Subt))
19373 and then
19374 Chars (Etype (Base_Type (Entity (Subt)))) =
19375 Type_Id));
19376 end if;
19377
19378 -- A reference to the current type may appear as the prefix of
19379 -- a 'Class attribute.
19380
19381 elsif Nkind (Subt) = N_Attribute_Reference
19382 and then Attribute_Name (Subt) = Name_Class
19383 then
19384 return Names_T (Prefix (Subt));
19385
19386 else
19387 return False;
19388 end if;
19389 end Designates_T;
19390
19391 ----------------
19392 -- Mentions_T --
19393 ----------------
19394
19395 function Mentions_T (Acc_Def : Node_Id) return Boolean is
19396 Param_Spec : Node_Id;
19397
19398 Acc_Subprg : constant Node_Id :=
19399 Access_To_Subprogram_Definition (Acc_Def);
19400
19401 begin
19402 if No (Acc_Subprg) then
19403 return Designates_T (Subtype_Mark (Acc_Def));
19404 end if;
19405
19406 -- Component is an access_to_subprogram: examine its formals,
19407 -- and result definition in the case of an access_to_function.
19408
19409 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
19410 while Present (Param_Spec) loop
19411 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
19412 and then Mentions_T (Parameter_Type (Param_Spec))
19413 then
19414 return True;
19415
19416 elsif Designates_T (Parameter_Type (Param_Spec)) then
19417 return True;
19418 end if;
19419
19420 Next (Param_Spec);
19421 end loop;
19422
19423 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
19424 if Nkind (Result_Definition (Acc_Subprg)) =
19425 N_Access_Definition
19426 then
19427 return Mentions_T (Result_Definition (Acc_Subprg));
19428 else
19429 return Designates_T (Result_Definition (Acc_Subprg));
19430 end if;
19431 end if;
19432
19433 return False;
19434 end Mentions_T;
19435
19436 -- Start of processing for Check_Anonymous_Access_Components
19437
19438 begin
19439 if No (Comp_List) then
19440 return;
19441 end if;
19442
19443 Comp := First (Component_Items (Comp_List));
19444 while Present (Comp) loop
19445 if Nkind (Comp) = N_Component_Declaration
19446 and then Present
19447 (Access_Definition (Component_Definition (Comp)))
19448 and then
19449 Mentions_T (Access_Definition (Component_Definition (Comp)))
19450 then
19451 Comp_Def := Component_Definition (Comp);
19452 Acc_Def :=
19453 Access_To_Subprogram_Definition
19454 (Access_Definition (Comp_Def));
19455
19456 Build_Incomplete_Type_Declaration;
19457 Anon_Access := Make_Temporary (Loc, 'S');
19458
19459 -- Create a declaration for the anonymous access type: either
19460 -- an access_to_object or an access_to_subprogram.
19461
19462 if Present (Acc_Def) then
19463 if Nkind (Acc_Def) = N_Access_Function_Definition then
19464 Type_Def :=
19465 Make_Access_Function_Definition (Loc,
19466 Parameter_Specifications =>
19467 Parameter_Specifications (Acc_Def),
19468 Result_Definition => Result_Definition (Acc_Def));
19469 else
19470 Type_Def :=
19471 Make_Access_Procedure_Definition (Loc,
19472 Parameter_Specifications =>
19473 Parameter_Specifications (Acc_Def));
19474 end if;
19475
19476 else
19477 Type_Def :=
19478 Make_Access_To_Object_Definition (Loc,
19479 Subtype_Indication =>
19480 Relocate_Node
19481 (Subtype_Mark
19482 (Access_Definition (Comp_Def))));
19483
19484 Set_Constant_Present
19485 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
19486 Set_All_Present
19487 (Type_Def, All_Present (Access_Definition (Comp_Def)));
19488 end if;
19489
19490 Set_Null_Exclusion_Present
19491 (Type_Def,
19492 Null_Exclusion_Present (Access_Definition (Comp_Def)));
19493
19494 Decl :=
19495 Make_Full_Type_Declaration (Loc,
19496 Defining_Identifier => Anon_Access,
19497 Type_Definition => Type_Def);
19498
19499 Insert_Before (Typ_Decl, Decl);
19500 Analyze (Decl);
19501
19502 -- If an access to subprogram, create the extra formals
19503
19504 if Present (Acc_Def) then
19505 Create_Extra_Formals (Designated_Type (Anon_Access));
19506
19507 -- If an access to object, preserve entity of designated type,
19508 -- for ASIS use, before rewriting the component definition.
19509
19510 else
19511 declare
19512 Desig : Entity_Id;
19513
19514 begin
19515 Desig := Entity (Subtype_Indication (Type_Def));
19516
19517 -- If the access definition is to the current record,
19518 -- the visible entity at this point is an incomplete
19519 -- type. Retrieve the full view to simplify ASIS queries
19520
19521 if Ekind (Desig) = E_Incomplete_Type then
19522 Desig := Full_View (Desig);
19523 end if;
19524
19525 Set_Entity
19526 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
19527 end;
19528 end if;
19529
19530 Rewrite (Comp_Def,
19531 Make_Component_Definition (Loc,
19532 Subtype_Indication =>
19533 New_Occurrence_Of (Anon_Access, Loc)));
19534
19535 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
19536 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
19537 else
19538 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
19539 end if;
19540
19541 Set_Is_Local_Anonymous_Access (Anon_Access);
19542 end if;
19543
19544 Next (Comp);
19545 end loop;
19546
19547 if Present (Variant_Part (Comp_List)) then
19548 declare
19549 V : Node_Id;
19550 begin
19551 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
19552 while Present (V) loop
19553 Check_Anonymous_Access_Components
19554 (Typ_Decl, Typ, Prev, Component_List (V));
19555 Next_Non_Pragma (V);
19556 end loop;
19557 end;
19558 end if;
19559 end Check_Anonymous_Access_Components;
19560
19561 ----------------------------------
19562 -- Preanalyze_Assert_Expression --
19563 ----------------------------------
19564
19565 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19566 begin
19567 In_Assertion_Expr := In_Assertion_Expr + 1;
19568 Preanalyze_Spec_Expression (N, T);
19569 In_Assertion_Expr := In_Assertion_Expr - 1;
19570 end Preanalyze_Assert_Expression;
19571
19572 --------------------------------
19573 -- Preanalyze_Spec_Expression --
19574 --------------------------------
19575
19576 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19577 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19578 begin
19579 In_Spec_Expression := True;
19580 Preanalyze_And_Resolve (N, T);
19581 In_Spec_Expression := Save_In_Spec_Expression;
19582 end Preanalyze_Spec_Expression;
19583
19584 -----------------------------
19585 -- Record_Type_Declaration --
19586 -----------------------------
19587
19588 procedure Record_Type_Declaration
19589 (T : Entity_Id;
19590 N : Node_Id;
19591 Prev : Entity_Id)
19592 is
19593 Def : constant Node_Id := Type_Definition (N);
19594 Is_Tagged : Boolean;
19595 Tag_Comp : Entity_Id;
19596
19597 begin
19598 -- These flags must be initialized before calling Process_Discriminants
19599 -- because this routine makes use of them.
19600
19601 Set_Ekind (T, E_Record_Type);
19602 Set_Etype (T, T);
19603 Init_Size_Align (T);
19604 Set_Interfaces (T, No_Elist);
19605 Set_Stored_Constraint (T, No_Elist);
19606
19607 -- Normal case
19608
19609 if Ada_Version < Ada_2005
19610 or else not Interface_Present (Def)
19611 then
19612 if Limited_Present (Def) then
19613 Check_SPARK_Restriction ("limited is not allowed", N);
19614 end if;
19615
19616 if Abstract_Present (Def) then
19617 Check_SPARK_Restriction ("abstract is not allowed", N);
19618 end if;
19619
19620 -- The flag Is_Tagged_Type might have already been set by
19621 -- Find_Type_Name if it detected an error for declaration T. This
19622 -- arises in the case of private tagged types where the full view
19623 -- omits the word tagged.
19624
19625 Is_Tagged :=
19626 Tagged_Present (Def)
19627 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
19628
19629 Set_Is_Tagged_Type (T, Is_Tagged);
19630 Set_Is_Limited_Record (T, Limited_Present (Def));
19631
19632 -- Type is abstract if full declaration carries keyword, or if
19633 -- previous partial view did.
19634
19635 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
19636 or else Abstract_Present (Def));
19637
19638 else
19639 Check_SPARK_Restriction ("interface is not allowed", N);
19640
19641 Is_Tagged := True;
19642 Analyze_Interface_Declaration (T, Def);
19643
19644 if Present (Discriminant_Specifications (N)) then
19645 Error_Msg_N
19646 ("interface types cannot have discriminants",
19647 Defining_Identifier
19648 (First (Discriminant_Specifications (N))));
19649 end if;
19650 end if;
19651
19652 -- First pass: if there are self-referential access components,
19653 -- create the required anonymous access type declarations, and if
19654 -- need be an incomplete type declaration for T itself.
19655
19656 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
19657
19658 if Ada_Version >= Ada_2005
19659 and then Present (Interface_List (Def))
19660 then
19661 Check_Interfaces (N, Def);
19662
19663 declare
19664 Ifaces_List : Elist_Id;
19665
19666 begin
19667 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
19668 -- already in the parents.
19669
19670 Collect_Interfaces
19671 (T => T,
19672 Ifaces_List => Ifaces_List,
19673 Exclude_Parents => True);
19674
19675 Set_Interfaces (T, Ifaces_List);
19676 end;
19677 end if;
19678
19679 -- Records constitute a scope for the component declarations within.
19680 -- The scope is created prior to the processing of these declarations.
19681 -- Discriminants are processed first, so that they are visible when
19682 -- processing the other components. The Ekind of the record type itself
19683 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
19684
19685 -- Enter record scope
19686
19687 Push_Scope (T);
19688
19689 -- If an incomplete or private type declaration was already given for
19690 -- the type, then this scope already exists, and the discriminants have
19691 -- been declared within. We must verify that the full declaration
19692 -- matches the incomplete one.
19693
19694 Check_Or_Process_Discriminants (N, T, Prev);
19695
19696 Set_Is_Constrained (T, not Has_Discriminants (T));
19697 Set_Has_Delayed_Freeze (T, True);
19698
19699 -- For tagged types add a manually analyzed component corresponding
19700 -- to the component _tag, the corresponding piece of tree will be
19701 -- expanded as part of the freezing actions if it is not a CPP_Class.
19702
19703 if Is_Tagged then
19704
19705 -- Do not add the tag unless we are in expansion mode
19706
19707 if Expander_Active then
19708 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
19709 Enter_Name (Tag_Comp);
19710
19711 Set_Ekind (Tag_Comp, E_Component);
19712 Set_Is_Tag (Tag_Comp);
19713 Set_Is_Aliased (Tag_Comp);
19714 Set_Etype (Tag_Comp, RTE (RE_Tag));
19715 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19716 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19717 Init_Component_Location (Tag_Comp);
19718
19719 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19720 -- implemented interfaces.
19721
19722 if Has_Interfaces (T) then
19723 Add_Interface_Tag_Components (N, T);
19724 end if;
19725 end if;
19726
19727 Make_Class_Wide_Type (T);
19728 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19729 end if;
19730
19731 -- We must suppress range checks when processing record components in
19732 -- the presence of discriminants, since we don't want spurious checks to
19733 -- be generated during their analysis, but Suppress_Range_Checks flags
19734 -- must be reset the after processing the record definition.
19735
19736 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19737 -- couldn't we just use the normal range check suppression method here.
19738 -- That would seem cleaner ???
19739
19740 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19741 Set_Kill_Range_Checks (T, True);
19742 Record_Type_Definition (Def, Prev);
19743 Set_Kill_Range_Checks (T, False);
19744 else
19745 Record_Type_Definition (Def, Prev);
19746 end if;
19747
19748 -- Exit from record scope
19749
19750 End_Scope;
19751
19752 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19753 -- the implemented interfaces and associate them an aliased entity.
19754
19755 if Is_Tagged
19756 and then not Is_Empty_List (Interface_List (Def))
19757 then
19758 Derive_Progenitor_Subprograms (T, T);
19759 end if;
19760
19761 Check_Function_Writable_Actuals (N);
19762 end Record_Type_Declaration;
19763
19764 ----------------------------
19765 -- Record_Type_Definition --
19766 ----------------------------
19767
19768 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19769 Component : Entity_Id;
19770 Ctrl_Components : Boolean := False;
19771 Final_Storage_Only : Boolean;
19772 T : Entity_Id;
19773
19774 begin
19775 if Ekind (Prev_T) = E_Incomplete_Type then
19776 T := Full_View (Prev_T);
19777 else
19778 T := Prev_T;
19779 end if;
19780
19781 -- In SPARK, tagged types and type extensions may only be declared in
19782 -- the specification of library unit packages.
19783
19784 if Present (Def) and then Is_Tagged_Type (T) then
19785 declare
19786 Typ : Node_Id;
19787 Ctxt : Node_Id;
19788
19789 begin
19790 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19791 Typ := Parent (Def);
19792 else
19793 pragma Assert
19794 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19795 Typ := Parent (Parent (Def));
19796 end if;
19797
19798 Ctxt := Parent (Typ);
19799
19800 if Nkind (Ctxt) = N_Package_Body
19801 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19802 then
19803 Check_SPARK_Restriction
19804 ("type should be defined in package specification", Typ);
19805
19806 elsif Nkind (Ctxt) /= N_Package_Specification
19807 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19808 then
19809 Check_SPARK_Restriction
19810 ("type should be defined in library unit package", Typ);
19811 end if;
19812 end;
19813 end if;
19814
19815 Final_Storage_Only := not Is_Controlled (T);
19816
19817 -- Ada 2005: check whether an explicit Limited is present in a derived
19818 -- type declaration.
19819
19820 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19821 and then Limited_Present (Parent (Def))
19822 then
19823 Set_Is_Limited_Record (T);
19824 end if;
19825
19826 -- If the component list of a record type is defined by the reserved
19827 -- word null and there is no discriminant part, then the record type has
19828 -- no components and all records of the type are null records (RM 3.7)
19829 -- This procedure is also called to process the extension part of a
19830 -- record extension, in which case the current scope may have inherited
19831 -- components.
19832
19833 if No (Def)
19834 or else No (Component_List (Def))
19835 or else Null_Present (Component_List (Def))
19836 then
19837 if not Is_Tagged_Type (T) then
19838 Check_SPARK_Restriction ("non-tagged record cannot be null", Def);
19839 end if;
19840
19841 else
19842 Analyze_Declarations (Component_Items (Component_List (Def)));
19843
19844 if Present (Variant_Part (Component_List (Def))) then
19845 Check_SPARK_Restriction ("variant part is not allowed", Def);
19846 Analyze (Variant_Part (Component_List (Def)));
19847 end if;
19848 end if;
19849
19850 -- After completing the semantic analysis of the record definition,
19851 -- record components, both new and inherited, are accessible. Set their
19852 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19853 -- whose Ekind may be void.
19854
19855 Component := First_Entity (Current_Scope);
19856 while Present (Component) loop
19857 if Ekind (Component) = E_Void
19858 and then not Is_Itype (Component)
19859 then
19860 Set_Ekind (Component, E_Component);
19861 Init_Component_Location (Component);
19862 end if;
19863
19864 if Has_Task (Etype (Component)) then
19865 Set_Has_Task (T);
19866 end if;
19867
19868 if Ekind (Component) /= E_Component then
19869 null;
19870
19871 -- Do not set Has_Controlled_Component on a class-wide equivalent
19872 -- type. See Make_CW_Equivalent_Type.
19873
19874 elsif not Is_Class_Wide_Equivalent_Type (T)
19875 and then (Has_Controlled_Component (Etype (Component))
19876 or else (Chars (Component) /= Name_uParent
19877 and then Is_Controlled (Etype (Component))))
19878 then
19879 Set_Has_Controlled_Component (T, True);
19880 Final_Storage_Only :=
19881 Final_Storage_Only
19882 and then Finalize_Storage_Only (Etype (Component));
19883 Ctrl_Components := True;
19884 end if;
19885
19886 Next_Entity (Component);
19887 end loop;
19888
19889 -- A Type is Finalize_Storage_Only only if all its controlled components
19890 -- are also.
19891
19892 if Ctrl_Components then
19893 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19894 end if;
19895
19896 -- Place reference to end record on the proper entity, which may
19897 -- be a partial view.
19898
19899 if Present (Def) then
19900 Process_End_Label (Def, 'e', Prev_T);
19901 end if;
19902 end Record_Type_Definition;
19903
19904 ------------------------
19905 -- Replace_Components --
19906 ------------------------
19907
19908 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19909 function Process (N : Node_Id) return Traverse_Result;
19910
19911 -------------
19912 -- Process --
19913 -------------
19914
19915 function Process (N : Node_Id) return Traverse_Result is
19916 Comp : Entity_Id;
19917
19918 begin
19919 if Nkind (N) = N_Discriminant_Specification then
19920 Comp := First_Discriminant (Typ);
19921 while Present (Comp) loop
19922 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19923 Set_Defining_Identifier (N, Comp);
19924 exit;
19925 end if;
19926
19927 Next_Discriminant (Comp);
19928 end loop;
19929
19930 elsif Nkind (N) = N_Component_Declaration then
19931 Comp := First_Component (Typ);
19932 while Present (Comp) loop
19933 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19934 Set_Defining_Identifier (N, Comp);
19935 exit;
19936 end if;
19937
19938 Next_Component (Comp);
19939 end loop;
19940 end if;
19941
19942 return OK;
19943 end Process;
19944
19945 procedure Replace is new Traverse_Proc (Process);
19946
19947 -- Start of processing for Replace_Components
19948
19949 begin
19950 Replace (Decl);
19951 end Replace_Components;
19952
19953 -------------------------------
19954 -- Set_Completion_Referenced --
19955 -------------------------------
19956
19957 procedure Set_Completion_Referenced (E : Entity_Id) is
19958 begin
19959 -- If in main unit, mark entity that is a completion as referenced,
19960 -- warnings go on the partial view when needed.
19961
19962 if In_Extended_Main_Source_Unit (E) then
19963 Set_Referenced (E);
19964 end if;
19965 end Set_Completion_Referenced;
19966
19967 ---------------------
19968 -- Set_Fixed_Range --
19969 ---------------------
19970
19971 -- The range for fixed-point types is complicated by the fact that we
19972 -- do not know the exact end points at the time of the declaration. This
19973 -- is true for three reasons:
19974
19975 -- A size clause may affect the fudging of the end-points.
19976 -- A small clause may affect the values of the end-points.
19977 -- We try to include the end-points if it does not affect the size.
19978
19979 -- This means that the actual end-points must be established at the
19980 -- point when the type is frozen. Meanwhile, we first narrow the range
19981 -- as permitted (so that it will fit if necessary in a small specified
19982 -- size), and then build a range subtree with these narrowed bounds.
19983 -- Set_Fixed_Range constructs the range from real literal values, and
19984 -- sets the range as the Scalar_Range of the given fixed-point type entity.
19985
19986 -- The parent of this range is set to point to the entity so that it is
19987 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19988 -- other scalar types, which are just pointers to the range in the
19989 -- original tree, this would otherwise be an orphan).
19990
19991 -- The tree is left unanalyzed. When the type is frozen, the processing
19992 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19993 -- analyzed, and uses this as an indication that it should complete
19994 -- work on the range (it will know the final small and size values).
19995
19996 procedure Set_Fixed_Range
19997 (E : Entity_Id;
19998 Loc : Source_Ptr;
19999 Lo : Ureal;
20000 Hi : Ureal)
20001 is
20002 S : constant Node_Id :=
20003 Make_Range (Loc,
20004 Low_Bound => Make_Real_Literal (Loc, Lo),
20005 High_Bound => Make_Real_Literal (Loc, Hi));
20006 begin
20007 Set_Scalar_Range (E, S);
20008 Set_Parent (S, E);
20009
20010 -- Before the freeze point, the bounds of a fixed point are universal
20011 -- and carry the corresponding type.
20012
20013 Set_Etype (Low_Bound (S), Universal_Real);
20014 Set_Etype (High_Bound (S), Universal_Real);
20015 end Set_Fixed_Range;
20016
20017 ----------------------------------
20018 -- Set_Scalar_Range_For_Subtype --
20019 ----------------------------------
20020
20021 procedure Set_Scalar_Range_For_Subtype
20022 (Def_Id : Entity_Id;
20023 R : Node_Id;
20024 Subt : Entity_Id)
20025 is
20026 Kind : constant Entity_Kind := Ekind (Def_Id);
20027
20028 begin
20029 -- Defend against previous error
20030
20031 if Nkind (R) = N_Error then
20032 return;
20033 end if;
20034
20035 Set_Scalar_Range (Def_Id, R);
20036
20037 -- We need to link the range into the tree before resolving it so
20038 -- that types that are referenced, including importantly the subtype
20039 -- itself, are properly frozen (Freeze_Expression requires that the
20040 -- expression be properly linked into the tree). Of course if it is
20041 -- already linked in, then we do not disturb the current link.
20042
20043 if No (Parent (R)) then
20044 Set_Parent (R, Def_Id);
20045 end if;
20046
20047 -- Reset the kind of the subtype during analysis of the range, to
20048 -- catch possible premature use in the bounds themselves.
20049
20050 Set_Ekind (Def_Id, E_Void);
20051 Process_Range_Expr_In_Decl (R, Subt);
20052 Set_Ekind (Def_Id, Kind);
20053 end Set_Scalar_Range_For_Subtype;
20054
20055 --------------------------------------------------------
20056 -- Set_Stored_Constraint_From_Discriminant_Constraint --
20057 --------------------------------------------------------
20058
20059 procedure Set_Stored_Constraint_From_Discriminant_Constraint
20060 (E : Entity_Id)
20061 is
20062 begin
20063 -- Make sure set if encountered during Expand_To_Stored_Constraint
20064
20065 Set_Stored_Constraint (E, No_Elist);
20066
20067 -- Give it the right value
20068
20069 if Is_Constrained (E) and then Has_Discriminants (E) then
20070 Set_Stored_Constraint (E,
20071 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
20072 end if;
20073 end Set_Stored_Constraint_From_Discriminant_Constraint;
20074
20075 -------------------------------------
20076 -- Signed_Integer_Type_Declaration --
20077 -------------------------------------
20078
20079 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20080 Implicit_Base : Entity_Id;
20081 Base_Typ : Entity_Id;
20082 Lo_Val : Uint;
20083 Hi_Val : Uint;
20084 Errs : Boolean := False;
20085 Lo : Node_Id;
20086 Hi : Node_Id;
20087
20088 function Can_Derive_From (E : Entity_Id) return Boolean;
20089 -- Determine whether given bounds allow derivation from specified type
20090
20091 procedure Check_Bound (Expr : Node_Id);
20092 -- Check bound to make sure it is integral and static. If not, post
20093 -- appropriate error message and set Errs flag
20094
20095 ---------------------
20096 -- Can_Derive_From --
20097 ---------------------
20098
20099 -- Note we check both bounds against both end values, to deal with
20100 -- strange types like ones with a range of 0 .. -12341234.
20101
20102 function Can_Derive_From (E : Entity_Id) return Boolean is
20103 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
20104 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
20105 begin
20106 return Lo <= Lo_Val and then Lo_Val <= Hi
20107 and then
20108 Lo <= Hi_Val and then Hi_Val <= Hi;
20109 end Can_Derive_From;
20110
20111 -----------------
20112 -- Check_Bound --
20113 -----------------
20114
20115 procedure Check_Bound (Expr : Node_Id) is
20116 begin
20117 -- If a range constraint is used as an integer type definition, each
20118 -- bound of the range must be defined by a static expression of some
20119 -- integer type, but the two bounds need not have the same integer
20120 -- type (Negative bounds are allowed.) (RM 3.5.4)
20121
20122 if not Is_Integer_Type (Etype (Expr)) then
20123 Error_Msg_N
20124 ("integer type definition bounds must be of integer type", Expr);
20125 Errs := True;
20126
20127 elsif not Is_OK_Static_Expression (Expr) then
20128 Flag_Non_Static_Expr
20129 ("non-static expression used for integer type bound!", Expr);
20130 Errs := True;
20131
20132 -- The bounds are folded into literals, and we set their type to be
20133 -- universal, to avoid typing difficulties: we cannot set the type
20134 -- of the literal to the new type, because this would be a forward
20135 -- reference for the back end, and if the original type is user-
20136 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
20137
20138 else
20139 if Is_Entity_Name (Expr) then
20140 Fold_Uint (Expr, Expr_Value (Expr), True);
20141 end if;
20142
20143 Set_Etype (Expr, Universal_Integer);
20144 end if;
20145 end Check_Bound;
20146
20147 -- Start of processing for Signed_Integer_Type_Declaration
20148
20149 begin
20150 -- Create an anonymous base type
20151
20152 Implicit_Base :=
20153 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
20154
20155 -- Analyze and check the bounds, they can be of any integer type
20156
20157 Lo := Low_Bound (Def);
20158 Hi := High_Bound (Def);
20159
20160 -- Arbitrarily use Integer as the type if either bound had an error
20161
20162 if Hi = Error or else Lo = Error then
20163 Base_Typ := Any_Integer;
20164 Set_Error_Posted (T, True);
20165
20166 -- Here both bounds are OK expressions
20167
20168 else
20169 Analyze_And_Resolve (Lo, Any_Integer);
20170 Analyze_And_Resolve (Hi, Any_Integer);
20171
20172 Check_Bound (Lo);
20173 Check_Bound (Hi);
20174
20175 if Errs then
20176 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20177 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20178 end if;
20179
20180 -- Find type to derive from
20181
20182 Lo_Val := Expr_Value (Lo);
20183 Hi_Val := Expr_Value (Hi);
20184
20185 if Can_Derive_From (Standard_Short_Short_Integer) then
20186 Base_Typ := Base_Type (Standard_Short_Short_Integer);
20187
20188 elsif Can_Derive_From (Standard_Short_Integer) then
20189 Base_Typ := Base_Type (Standard_Short_Integer);
20190
20191 elsif Can_Derive_From (Standard_Integer) then
20192 Base_Typ := Base_Type (Standard_Integer);
20193
20194 elsif Can_Derive_From (Standard_Long_Integer) then
20195 Base_Typ := Base_Type (Standard_Long_Integer);
20196
20197 elsif Can_Derive_From (Standard_Long_Long_Integer) then
20198 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20199
20200 else
20201 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20202 Error_Msg_N ("integer type definition bounds out of range", Def);
20203 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20204 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20205 end if;
20206 end if;
20207
20208 -- Complete both implicit base and declared first subtype entities
20209
20210 Set_Etype (Implicit_Base, Base_Typ);
20211 Set_Size_Info (Implicit_Base, (Base_Typ));
20212 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
20213 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
20214
20215 Set_Ekind (T, E_Signed_Integer_Subtype);
20216 Set_Etype (T, Implicit_Base);
20217
20218 -- In formal verification mode, restrict the base type's range to the
20219 -- minimum allowed by RM 3.5.4, namely the smallest symmetric range
20220 -- around zero with a possible extra negative value that contains the
20221 -- subtype range. Keep Size, RM_Size and First_Rep_Item info, which
20222 -- should not be relied upon in formal verification.
20223
20224 if SPARK_Strict_Mode then
20225 declare
20226 Sym_Hi_Val : Uint;
20227 Sym_Lo_Val : Uint;
20228 Dloc : constant Source_Ptr := Sloc (Def);
20229 Lbound : Node_Id;
20230 Ubound : Node_Id;
20231 Bounds : Node_Id;
20232
20233 begin
20234 -- If the subtype range is empty, the smallest base type range
20235 -- is the symmetric range around zero containing Lo_Val and
20236 -- Hi_Val.
20237
20238 if UI_Gt (Lo_Val, Hi_Val) then
20239 Sym_Hi_Val := UI_Max (UI_Abs (Lo_Val), UI_Abs (Hi_Val));
20240 Sym_Lo_Val := UI_Negate (Sym_Hi_Val);
20241
20242 -- Otherwise, if the subtype range is not empty and Hi_Val has
20243 -- the largest absolute value, Hi_Val is non negative and the
20244 -- smallest base type range is the symmetric range around zero
20245 -- containing Hi_Val.
20246
20247 elsif UI_Le (UI_Abs (Lo_Val), UI_Abs (Hi_Val)) then
20248 Sym_Hi_Val := Hi_Val;
20249 Sym_Lo_Val := UI_Negate (Hi_Val);
20250
20251 -- Otherwise, the subtype range is not empty, Lo_Val has the
20252 -- strictly largest absolute value, Lo_Val is negative and the
20253 -- smallest base type range is the symmetric range around zero
20254 -- with an extra negative value Lo_Val.
20255
20256 else
20257 Sym_Lo_Val := Lo_Val;
20258 Sym_Hi_Val := UI_Sub (UI_Negate (Lo_Val), Uint_1);
20259 end if;
20260
20261 Lbound := Make_Integer_Literal (Dloc, Sym_Lo_Val);
20262 Ubound := Make_Integer_Literal (Dloc, Sym_Hi_Val);
20263 Set_Is_Static_Expression (Lbound);
20264 Set_Is_Static_Expression (Ubound);
20265 Analyze_And_Resolve (Lbound, Any_Integer);
20266 Analyze_And_Resolve (Ubound, Any_Integer);
20267
20268 Bounds := Make_Range (Dloc, Lbound, Ubound);
20269 Set_Etype (Bounds, Base_Typ);
20270
20271 Set_Scalar_Range (Implicit_Base, Bounds);
20272 end;
20273
20274 else
20275 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
20276 end if;
20277
20278 Set_Size_Info (T, (Implicit_Base));
20279 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
20280 Set_Scalar_Range (T, Def);
20281 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
20282 Set_Is_Constrained (T);
20283 end Signed_Integer_Type_Declaration;
20284
20285 end Sem_Ch3;